]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/setup.c
Import device-tree files from Linux 5.14
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / setup.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)setup.c       8.10 (Berkeley) 5/9/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/disk.h>
42 #include <sys/stat.h>
43 #define FSTYPENAMES
44 #include <sys/disklabel.h>
45 #include <sys/file.h>
46 #include <sys/sysctl.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <stdint.h>
56 #include <string.h>
57 #include <libufs.h>
58
59 #include "fsck.h"
60
61 struct inoinfo **inphead, **inpsort;    /* info about all inodes */
62
63 #define POWEROF2(num)   (((num) & ((num) - 1)) == 0)
64
65 static int calcsb(char *dev, int devfd, struct fs *fs);
66 static void saverecovery(int readfd, int writefd);
67 static int chkrecovery(int devfd);
68
69 /*
70  * Read in a superblock finding an alternate if necessary.
71  * Return 1 if successful, 0 if unsuccessful, -1 if file system
72  * is already clean (ckclean and preen mode only).
73  */
74 int
75 setup(char *dev)
76 {
77         long cg, bmapsize;
78         struct fs proto;
79
80         /*
81          * We are expected to have an open file descriptor
82          */
83         if (fsreadfd < 0)
84                 return (0);
85         /*
86          * If we do not yet have a superblock, read it in looking
87          * for alternates if necessary.
88          */
89         if (havesb == 0 && readsb(1) == 0) {
90                 skipclean = 0;
91                 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
92                         return(0);
93                 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
94                         return (0);
95                 for (cg = 0; cg < proto.fs_ncg; cg++) {
96                         bflag = fsbtodb(&proto, cgsblock(&proto, cg));
97                         if (readsb(0) != 0)
98                                 break;
99                 }
100                 if (cg >= proto.fs_ncg) {
101                         printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
102                             "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
103                             "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
104                             "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
105                         bflag = 0;
106                         return(0);
107                 }
108                 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
109                 bflag = 0;
110         }
111         if (preen == 0)
112                 printf("** %s", dev);
113         if (bkgrdflag == 0 &&
114             (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
115                 fswritefd = -1;
116                 if (preen)
117                         pfatal("NO WRITE ACCESS");
118                 printf(" (NO WRITE)");
119         }
120         if (preen == 0)
121                 printf("\n");
122         if (sbhashfailed != 0) {
123                 pwarn("SUPERBLOCK CHECK HASH FAILED");
124                 if (fswritefd == -1)
125                         pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
126                 else if (preen || reply("CORRECT CHECK HASH") != 0) {
127                         if (preen)
128                                 printf(" (CORRECTED)\n");
129                         sblock.fs_clean = 0;
130                         sbdirty();
131                 }
132         }
133         if (skipclean && ckclean && sblock.fs_clean) {
134                 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
135                 return (-1);
136         }
137         maxfsblock = sblock.fs_size;
138         maxino = sblock.fs_ncg * sblock.fs_ipg;
139         /*
140          * Check and potentially fix certain fields in the super block.
141          */
142         if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
143                 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
144                 if (reply("SET TO DEFAULT") == 1) {
145                         sblock.fs_optim = FS_OPTTIME;
146                         sbdirty();
147                 }
148         }
149         if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
150                 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
151                         sblock.fs_minfree);
152                 if (reply("SET TO DEFAULT") == 1) {
153                         sblock.fs_minfree = 10;
154                         sbdirty();
155                 }
156         }
157         if (sblock.fs_magic == FS_UFS1_MAGIC &&
158             sblock.fs_old_inodefmt < FS_44INODEFMT) {
159                 pwarn("Format of file system is too old.\n");
160                 pwarn("Must update to modern format using a version of fsck\n");
161                 pfatal("from before 2002 with the command ``fsck -c 2''\n");
162                 exit(EEXIT);
163         }
164         if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
165             fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
166             reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
167                 saverecovery(fsreadfd, fswritefd);
168         /*
169          * allocate and initialize the necessary maps
170          */
171         bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
172         blockmap = Calloc((unsigned)bmapsize, sizeof (char));
173         if (blockmap == NULL) {
174                 printf("cannot alloc %u bytes for blockmap\n",
175                     (unsigned)bmapsize);
176                 goto badsb;
177         }
178         inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
179         if (inostathead == NULL) {
180                 printf("cannot alloc %u bytes for inostathead\n",
181                     (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
182                 goto badsb;
183         }
184         numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
185         dirhash = numdirs;
186         inplast = 0;
187         listmax = numdirs + 10;
188         inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
189         inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
190         if (inpsort == NULL || inphead == NULL) {
191                 printf("cannot alloc %ju bytes for inphead\n",
192                     (uintmax_t)numdirs * sizeof(struct inoinfo *));
193                 goto badsb;
194         }
195         bufinit();
196         if (sblock.fs_flags & FS_DOSOFTDEP)
197                 usedsoftdep = 1;
198         else
199                 usedsoftdep = 0;
200         return (1);
201
202 badsb:
203         ckfini(0);
204         return (0);
205 }
206
207 /*
208  * Open a device or file to be checked by fsck.
209  */
210 int
211 openfilesys(char *dev)
212 {
213         struct stat statb;
214         int saved_fsreadfd;
215
216         if (stat(dev, &statb) < 0)
217                 return (0);
218         if ((statb.st_mode & S_IFMT) != S_IFCHR &&
219             (statb.st_mode & S_IFMT) != S_IFBLK) {
220                 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
221                         pfatal("BACKGROUND FSCK LACKS A SNAPSHOT\n");
222                         exit(EEXIT);
223                 }
224                 if (bkgrdflag != 0) {
225                         cursnapshot = statb.st_ino;
226                 } else {
227                         pfatal("%s IS NOT A DISK DEVICE\n", dev);
228                         if (reply("CONTINUE") == 0)
229                                 return (0);
230                 }
231         }
232         saved_fsreadfd = fsreadfd;
233         if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
234                 fsreadfd = saved_fsreadfd;
235                 return (0);
236         }
237         if (saved_fsreadfd != -1)
238                 close(saved_fsreadfd);
239         return (1);
240 }
241
242 /*
243  * Read in the super block and its summary info.
244  */
245 int
246 readsb(int listerr)
247 {
248         off_t super;
249         int ret, flags;
250         struct fs *fs;
251
252         super = bflag ? bflag * dev_bsize : UFS_STDSB;
253         flags = sbhashfailed ? UFS_NOHASHFAIL | UFS_NOMSG : UFS_NOMSG;
254         readcnt[sblk.b_type]++;
255         while ((ret = sbget(fsreadfd, &fs, super, flags)) != 0) {
256                 switch (ret) {
257                 case EINTEGRITY:
258                         if (bflag || (super == UFS_STDSB &&
259                             flags == (UFS_NOHASHFAIL | UFS_NOMSG)))
260                                 return (0);
261                         super = UFS_STDSB;
262                         flags = UFS_NOHASHFAIL | UFS_NOMSG;
263                         sbhashfailed = 1;
264                         continue;
265                 case ENOENT:
266                         if (bflag)
267                                 printf("%jd is not a file system "
268                                     "superblock\n", super / dev_bsize);
269                         else
270                                 printf("Cannot find file system "
271                                     "superblock\n");
272                         return (0);
273                 case EIO:
274                 default:
275                         printf("I/O error reading %jd\n",
276                             super / dev_bsize);
277                         return (0);
278                 }
279         }
280         memcpy(&sblock, fs, fs->fs_sbsize);
281         free(fs);
282         /*
283          * Compute block size that the file system is based on,
284          * according to fsbtodb, and adjust superblock block number
285          * so we can tell if this is an alternate later.
286          */
287         dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
288         sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
289         sblk.b_size = SBLOCKSIZE;
290         /*
291          * If not yet done, update UFS1 superblock with new wider fields.
292          */
293         if (sblock.fs_magic == FS_UFS1_MAGIC &&
294             sblock.fs_maxbsize != sblock.fs_bsize) {
295                 sblock.fs_maxbsize = sblock.fs_bsize;
296                 sblock.fs_time = sblock.fs_old_time;
297                 sblock.fs_size = sblock.fs_old_size;
298                 sblock.fs_dsize = sblock.fs_old_dsize;
299                 sblock.fs_csaddr = sblock.fs_old_csaddr;
300                 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
301                 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
302                 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
303                 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
304         }
305         havesb = 1;
306         return (1);
307 }
308
309 void
310 sblock_init(void)
311 {
312
313         fsreadfd = -1;
314         fswritefd = -1;
315         fsmodified = 0;
316         lfdir = 0;
317         initbarea(&sblk, BT_SUPERBLK);
318         sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
319         if (sblk.b_un.b_buf == NULL)
320                 errx(EEXIT, "cannot allocate space for superblock");
321         dev_bsize = secsize = DEV_BSIZE;
322 }
323
324 /*
325  * Calculate a prototype superblock based on information in the boot area.
326  * When done the cgsblock macro can be calculated and the fs_ncg field
327  * can be used. Do NOT attempt to use other macros without verifying that
328  * their needed information is available!
329  */
330 static int
331 calcsb(char *dev, int devfd, struct fs *fs)
332 {
333         struct fsrecovery *fsr;
334         char *fsrbuf;
335         u_int secsize;
336
337         /*
338          * We need fragments-per-group and the partition-size.
339          *
340          * Newfs stores these details at the end of the boot block area
341          * at the start of the filesystem partition. If they have been
342          * overwritten by a boot block, we fail. But usually they are
343          * there and we can use them.
344          */
345         if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
346                 return (0);
347         fsrbuf = Malloc(secsize);
348         if (fsrbuf == NULL)
349                 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
350         if (blread(devfd, fsrbuf,
351             (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
352                 free(fsrbuf);
353                 return (0);
354         }
355         fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
356         if (fsr->fsr_magic != FS_UFS2_MAGIC) {
357                 free(fsrbuf);
358                 return (0);
359         }
360         memset(fs, 0, sizeof(struct fs));
361         fs->fs_fpg = fsr->fsr_fpg;
362         fs->fs_fsbtodb = fsr->fsr_fsbtodb;
363         fs->fs_sblkno = fsr->fsr_sblkno;
364         fs->fs_magic = fsr->fsr_magic;
365         fs->fs_ncg = fsr->fsr_ncg;
366         free(fsrbuf);
367         return (1);
368 }
369
370 /*
371  * Check to see if recovery information exists.
372  * Return 1 if it exists or cannot be created.
373  * Return 0 if it does not exist and can be created.
374  */
375 static int
376 chkrecovery(int devfd)
377 {
378         struct fsrecovery *fsr;
379         char *fsrbuf;
380         u_int secsize, rdsize;
381
382         /*
383          * Could not determine if backup material exists, so do not
384          * offer to create it.
385          */
386         fsrbuf = NULL;
387         rdsize = sblock.fs_fsize;
388         if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
389             rdsize % secsize != 0 ||
390             (fsrbuf = Malloc(rdsize)) == NULL ||
391             blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
392               rdsize) != 0) {
393                 free(fsrbuf);
394                 return (1);
395         }
396         /*
397          * Recovery material has already been created, so do not
398          * need to create it again.
399          */
400         fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
401         if (fsr->fsr_magic == FS_UFS2_MAGIC) {
402                 free(fsrbuf);
403                 return (1);
404         }
405         /*
406          * Recovery material has not been created and can be if desired.
407          */
408         free(fsrbuf);
409         return (0);
410 }
411
412 /*
413  * Read the last filesystem-size piece of the boot block, replace the
414  * last 20 bytes with the recovery information, then write it back.
415  * The recovery information only works for UFS2 filesystems.
416  */
417 static void
418 saverecovery(int readfd, int writefd)
419 {
420         struct fsrecovery *fsr;
421         char *fsrbuf;
422         u_int secsize, rdsize;
423
424         fsrbuf = NULL;
425         rdsize = sblock.fs_fsize;
426         if (sblock.fs_magic != FS_UFS2_MAGIC ||
427             ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
428             rdsize % secsize != 0 ||
429             (fsrbuf = Malloc(rdsize)) == NULL ||
430             blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
431               rdsize) != 0) {
432                 printf("RECOVERY DATA COULD NOT BE CREATED\n");
433                 free(fsrbuf);
434                 return;
435         }
436         fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
437         fsr->fsr_magic = sblock.fs_magic;
438         fsr->fsr_fpg = sblock.fs_fpg;
439         fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
440         fsr->fsr_sblkno = sblock.fs_sblkno;
441         fsr->fsr_ncg = sblock.fs_ncg;
442         blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);
443         free(fsrbuf);
444 }