]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/setup.c
Remove spurious newline
[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 uufsd disk;
62 struct bufarea asblk;
63 #define altsblock (*asblk.b_un.b_fs)
64 #define POWEROF2(num)   (((num) & ((num) - 1)) == 0)
65
66 static int calcsb(char *dev, int devfd, struct fs *fs);
67 static void saverecovery(int readfd, int writefd);
68 static int chkrecovery(int devfd);
69
70 /*
71  * Read in a superblock finding an alternate if necessary.
72  * Return 1 if successful, 0 if unsuccessful, -1 if file system
73  * is already clean (ckclean and preen mode only).
74  */
75 int
76 setup(char *dev)
77 {
78         long cg, asked, i, j;
79         long bmapsize;
80         struct stat statb;
81         struct fs proto;
82         size_t size;
83
84         havesb = 0;
85         fswritefd = -1;
86         cursnapshot = 0;
87         if (stat(dev, &statb) < 0) {
88                 printf("Can't stat %s: %s\n", dev, strerror(errno));
89                 if (bkgrdflag) {
90                         unlink(snapname);
91                         bkgrdflag = 0;
92                 }
93                 return (0);
94         }
95         if ((statb.st_mode & S_IFMT) != S_IFCHR &&
96             (statb.st_mode & S_IFMT) != S_IFBLK) {
97                 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
98                         unlink(snapname);
99                         printf("background fsck lacks a snapshot\n");
100                         exit(EEXIT);
101                 }
102                 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
103                         cursnapshot = statb.st_ino;
104                 } else {
105                         if (cvtlevel == 0 ||
106                             (statb.st_flags & SF_SNAPSHOT) == 0) {
107                                 if (preen && bkgrdflag) {
108                                         unlink(snapname);
109                                         bkgrdflag = 0;
110                                 }
111                                 pfatal("%s is not a disk device", dev);
112                                 if (reply("CONTINUE") == 0) {
113                                         if (bkgrdflag) {
114                                                 unlink(snapname);
115                                                 bkgrdflag = 0;
116                                         }
117                                         return (0);
118                                 }
119                         } else {
120                                 if (bkgrdflag) {
121                                         unlink(snapname);
122                                         bkgrdflag = 0;
123                                 }
124                                 pfatal("cannot convert a snapshot");
125                                 exit(EEXIT);
126                         }
127                 }
128         }
129         if ((fsreadfd = open(dev, O_RDONLY)) < 0 ||
130             ufs_disk_fillout_blank(&disk, dev) < 0) {
131                 if (bkgrdflag) {
132                         unlink(snapname);
133                         bkgrdflag = 0;
134                 }
135                 printf("Can't open %s: %s\n", dev, strerror(errno));
136                 return (0);
137         }
138         if (bkgrdflag) {
139                 unlink(snapname);
140                 size = MIBSIZE;
141                 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
142                     sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
143                     sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
144                     sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
145                     sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
146                     sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
147                         pfatal("kernel lacks background fsck support\n");
148                         exit(EEXIT);
149                 }
150                 /*
151                  * When kernel is lack of runtime bgfsck superblock summary
152                  * adjustment functionality, it does not mean we can not
153                  * continue, as old kernels will recompute the summary at
154                  * mount time.  However, it will be an unexpected softupdates
155                  * inconsistency if it turns out that the summary is still
156                  * incorrect.  Set a flag so subsequent operation can know
157                  * this.
158                  */
159                 bkgrdsumadj = 1;
160                 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
161                     sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
162                     sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
163                     sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
164                     sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) {
165                         bkgrdsumadj = 0;
166                         pwarn("kernel lacks runtime superblock summary adjustment support");
167                 }
168                 cmd.version = FFS_CMD_VERSION;
169                 cmd.handle = fsreadfd;
170                 fswritefd = -1;
171         }
172         if (preen == 0)
173                 printf("** %s", dev);
174         if (bkgrdflag == 0 &&
175             (nflag || ufs_disk_write(&disk) < 0 ||
176              (fswritefd = dup(disk.d_fd)) < 0)) {
177                 fswritefd = -1;
178                 if (preen)
179                         pfatal("NO WRITE ACCESS");
180                 printf(" (NO WRITE)");
181         }
182         if (preen == 0)
183                 printf("\n");
184         /*
185          * Read in the superblock, looking for alternates if necessary
186          */
187         if (readsb(1) == 0) {
188                 skipclean = 0;
189                 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
190                         return(0);
191                 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
192                         return (0);
193                 for (cg = 0; cg < proto.fs_ncg; cg++) {
194                         bflag = fsbtodb(&proto, cgsblock(&proto, cg));
195                         if (readsb(0) != 0)
196                                 break;
197                 }
198                 if (cg >= proto.fs_ncg) {
199                         printf("%s %s\n%s %s\n%s %s\n",
200                                 "SEARCH FOR ALTERNATE SUPER-BLOCK",
201                                 "FAILED. YOU MUST USE THE",
202                                 "-b OPTION TO FSCK TO SPECIFY THE",
203                                 "LOCATION OF AN ALTERNATE",
204                                 "SUPER-BLOCK TO SUPPLY NEEDED",
205                                 "INFORMATION; SEE fsck_ffs(8).");
206                         bflag = 0;
207                         return(0);
208                 }
209                 pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
210                 bflag = 0;
211         }
212         /* Save copy of things needed by libufs */
213         memcpy(&disk.d_fs, &sblock, sblock.fs_sbsize);
214         disk.d_ufs = (sblock.fs_magic == FS_UFS1_MAGIC) ? 1 : 2;
215         disk.d_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
216         disk.d_sblock = sblock.fs_sblockloc / disk.d_bsize;
217         disk.d_sbcsum = sblock.fs_csp;
218
219         if (skipclean && ckclean && sblock.fs_clean) {
220                 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
221                 return (-1);
222         }
223         maxfsblock = sblock.fs_size;
224         maxino = sblock.fs_ncg * sblock.fs_ipg;
225         /*
226          * Check and potentially fix certain fields in the super block.
227          */
228         if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
229                 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
230                 if (reply("SET TO DEFAULT") == 1) {
231                         sblock.fs_optim = FS_OPTTIME;
232                         sbdirty();
233                 }
234         }
235         if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
236                 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
237                         sblock.fs_minfree);
238                 if (reply("SET TO DEFAULT") == 1) {
239                         sblock.fs_minfree = 10;
240                         sbdirty();
241                 }
242         }
243         if (sblock.fs_magic == FS_UFS1_MAGIC &&
244             sblock.fs_old_inodefmt < FS_44INODEFMT) {
245                 pwarn("Format of file system is too old.\n");
246                 pwarn("Must update to modern format using a version of fsck\n");
247                 pfatal("from before 2002 with the command ``fsck -c 2''\n");
248                 exit(EEXIT);
249         }
250         if (asblk.b_dirty && !bflag) {
251                 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
252                 flush(fswritefd, &asblk);
253         }
254         if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
255             fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
256             reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
257                 saverecovery(fsreadfd, fswritefd);
258         /*
259          * read in the summary info.
260          */
261         asked = 0;
262         sblock.fs_csp = Calloc(1, sblock.fs_cssize);
263         if (sblock.fs_csp == NULL) {
264                 printf("cannot alloc %u bytes for cg summary info\n",
265                     (unsigned)sblock.fs_cssize);
266                 goto badsb;
267         }
268         for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
269                 size = MIN(sblock.fs_cssize - i, sblock.fs_bsize);
270                 readcnt[sblk.b_type]++;
271                 if (blread(fsreadfd, (char *)sblock.fs_csp + i,
272                     fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
273                     size) != 0 && !asked) {
274                         pfatal("BAD SUMMARY INFORMATION");
275                         if (reply("CONTINUE") == 0) {
276                                 ckfini(0);
277                                 exit(EEXIT);
278                         }
279                         asked++;
280                 }
281         }
282         /*
283          * allocate and initialize the necessary maps
284          */
285         bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
286         blockmap = Calloc((unsigned)bmapsize, sizeof (char));
287         if (blockmap == NULL) {
288                 printf("cannot alloc %u bytes for blockmap\n",
289                     (unsigned)bmapsize);
290                 goto badsb;
291         }
292         inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
293         if (inostathead == NULL) {
294                 printf("cannot alloc %u bytes for inostathead\n",
295                     (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
296                 goto badsb;
297         }
298         numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128);
299         dirhash = numdirs;
300         inplast = 0;
301         listmax = numdirs + 10;
302         inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
303         inphead = (struct inoinfo **)Calloc(numdirs, sizeof(struct inoinfo *));
304         if (inpsort == NULL || inphead == NULL) {
305                 printf("cannot alloc %ju bytes for inphead\n",
306                     (uintmax_t)numdirs * sizeof(struct inoinfo *));
307                 goto badsb;
308         }
309         bufinit();
310         if (sblock.fs_flags & FS_DOSOFTDEP)
311                 usedsoftdep = 1;
312         else
313                 usedsoftdep = 0;
314         return (1);
315
316 badsb:
317         ckfini(0);
318         return (0);
319 }
320
321 /*
322  * Read in the super block and its summary info.
323  */
324 int
325 readsb(int listerr)
326 {
327         off_t super;
328         int bad, ret;
329         struct fs *fs;
330
331         super = bflag ? bflag * dev_bsize : STDSB;
332         readcnt[sblk.b_type]++;
333         if ((ret = sbget(fsreadfd, &fs, super)) != 0) {
334                 switch (ret) {
335                 case EINVAL:
336                         /* Superblock check-hash failed */
337                         return (0);
338                 case ENOENT:
339                         if (bflag)
340                                 fprintf(stderr, "%jd is not a file system "
341                                     "superblock\n", super / dev_bsize);
342                         else
343                                 fprintf(stderr, "Cannot find file system "
344                                     "superblock\n");
345                         return (0);
346                 case EIO:
347                 default:
348                         fprintf(stderr, "I/O error reading %jd\n",
349                             super / dev_bsize);
350                         return (0);
351                 }
352         }
353         memcpy(&sblock, fs, fs->fs_sbsize);
354         free(fs);
355         /*
356          * Compute block size that the file system is based on,
357          * according to fsbtodb, and adjust superblock block number
358          * so we can tell if this is an alternate later.
359          */
360         dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
361         sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
362         sblk.b_size = SBLOCKSIZE;
363         /*
364          * Compare all fields that should not differ in alternate super block.
365          * When an alternate super-block is specified this check is skipped.
366          */
367         if (bflag)
368                 goto out;
369         getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
370         if (asblk.b_errs)
371                 return (0);
372         bad = 0;
373 #define CHK(x, y)                               \
374         if (altsblock.x != sblock.x) {          \
375                 bad++;                          \
376                 if (listerr && debug)           \
377                         printf("SUPER BLOCK VS ALTERNATE MISMATCH %s: " y " vs " y "\n", \
378                             #x, (intmax_t)sblock.x, (intmax_t)altsblock.x); \
379         }
380         CHK(fs_sblkno, "%jd");
381         CHK(fs_cblkno, "%jd");
382         CHK(fs_iblkno, "%jd");
383         CHK(fs_dblkno, "%jd");
384         CHK(fs_ncg, "%jd");
385         CHK(fs_bsize, "%jd");
386         CHK(fs_fsize, "%jd");
387         CHK(fs_frag, "%jd");
388         CHK(fs_bmask, "%#jx");
389         CHK(fs_fmask, "%#jx");
390         CHK(fs_bshift, "%jd");
391         CHK(fs_fshift, "%jd");
392         CHK(fs_fragshift, "%jd");
393         CHK(fs_fsbtodb, "%jd");
394         CHK(fs_sbsize, "%jd");
395         CHK(fs_nindir, "%jd");
396         CHK(fs_inopb, "%jd");
397         CHK(fs_cssize, "%jd");
398         CHK(fs_ipg, "%jd");
399         CHK(fs_fpg, "%jd");
400         CHK(fs_magic, "%#jx");
401 #undef CHK
402         if (bad) {
403                 if (listerr == 0)
404                         return (0);
405                 if (preen)
406                         printf("%s: ", cdevname);
407                 printf(
408                     "VALUES IN SUPER BLOCK LSB=%jd DISAGREE WITH THOSE IN\n"
409                     "LAST ALTERNATE LSB=%jd\n",
410                     sblk.b_bno, asblk.b_bno);
411                 if (reply("IGNORE ALTERNATE SUPER BLOCK") == 0)
412                         return (0);
413         }
414 out:
415         /*
416          * If not yet done, update UFS1 superblock with new wider fields.
417          */
418         if (sblock.fs_magic == FS_UFS1_MAGIC &&
419             sblock.fs_maxbsize != sblock.fs_bsize) {
420                 sblock.fs_maxbsize = sblock.fs_bsize;
421                 sblock.fs_time = sblock.fs_old_time;
422                 sblock.fs_size = sblock.fs_old_size;
423                 sblock.fs_dsize = sblock.fs_old_dsize;
424                 sblock.fs_csaddr = sblock.fs_old_csaddr;
425                 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
426                 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
427                 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
428                 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
429         }
430         havesb = 1;
431         return (1);
432 }
433
434 void
435 sblock_init(void)
436 {
437
438         fswritefd = -1;
439         fsmodified = 0;
440         lfdir = 0;
441         initbarea(&sblk, BT_SUPERBLK);
442         initbarea(&asblk, BT_SUPERBLK);
443         sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
444         asblk.b_un.b_buf = Malloc(SBLOCKSIZE);
445         if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
446                 errx(EEXIT, "cannot allocate space for superblock");
447         dev_bsize = secsize = DEV_BSIZE;
448 }
449
450 /*
451  * Calculate a prototype superblock based on information in the boot area.
452  * When done the cgsblock macro can be calculated and the fs_ncg field
453  * can be used. Do NOT attempt to use other macros without verifying that
454  * their needed information is available!
455  */
456 static int
457 calcsb(char *dev, int devfd, struct fs *fs)
458 {
459         struct fsrecovery *fsr;
460         char *fsrbuf;
461         u_int secsize;
462
463         /*
464          * We need fragments-per-group and the partition-size.
465          *
466          * Newfs stores these details at the end of the boot block area
467          * at the start of the filesystem partition. If they have been
468          * overwritten by a boot block, we fail. But usually they are
469          * there and we can use them.
470          */
471         if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
472                 return (0);
473         fsrbuf = Malloc(secsize);
474         if (fsrbuf == NULL)
475                 errx(EEXIT, "calcsb: cannot allocate recovery buffer");
476         if (blread(devfd, fsrbuf,
477             (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0)
478                 return (0);
479         fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
480         if (fsr->fsr_magic != FS_UFS2_MAGIC)
481                 return (0);
482         memset(fs, 0, sizeof(struct fs));
483         fs->fs_fpg = fsr->fsr_fpg;
484         fs->fs_fsbtodb = fsr->fsr_fsbtodb;
485         fs->fs_sblkno = fsr->fsr_sblkno;
486         fs->fs_magic = fsr->fsr_magic;
487         fs->fs_ncg = fsr->fsr_ncg;
488         free(fsrbuf);
489         return (1);
490 }
491
492 /*
493  * Check to see if recovery information exists.
494  * Return 1 if it exists or cannot be created.
495  * Return 0 if it does not exist and can be created.
496  */
497 static int
498 chkrecovery(int devfd)
499 {
500         struct fsrecovery *fsr;
501         char *fsrbuf;
502         u_int secsize;
503
504         /*
505          * Could not determine if backup material exists, so do not
506          * offer to create it.
507          */
508         if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
509             (fsrbuf = Malloc(secsize)) == NULL ||
510             blread(devfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
511               secsize) != 0)
512                 return (1);
513         /*
514          * Recovery material has already been created, so do not
515          * need to create it again.
516          */
517         fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
518         if (fsr->fsr_magic == FS_UFS2_MAGIC) {
519                 free(fsrbuf);
520                 return (1);
521         }
522         /*
523          * Recovery material has not been created and can be if desired.
524          */
525         free(fsrbuf);
526         return (0);
527 }
528
529 /*
530  * Read the last sector of the boot block, replace the last
531  * 20 bytes with the recovery information, then write it back.
532  * The recovery information only works for UFS2 filesystems.
533  */
534 static void
535 saverecovery(int readfd, int writefd)
536 {
537         struct fsrecovery *fsr;
538         char *fsrbuf;
539         u_int secsize;
540
541         if (sblock.fs_magic != FS_UFS2_MAGIC ||
542             ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
543             (fsrbuf = Malloc(secsize)) == NULL ||
544             blread(readfd, fsrbuf, (SBLOCK_UFS2 - secsize) / dev_bsize,
545               secsize) != 0) {
546                 printf("RECOVERY DATA COULD NOT BE CREATED\n");
547                 return;
548         }
549         fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
550         fsr->fsr_magic = sblock.fs_magic;
551         fsr->fsr_fpg = sblock.fs_fpg;
552         fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
553         fsr->fsr_sblkno = sblock.fs_sblkno;
554         fsr->fsr_ncg = sblock.fs_ncg;
555         blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - secsize) / secsize, secsize);
556         free(fsrbuf);
557 }