xref: /freebsd-13-stable/sbin/fsck_ffs/setup.c (revision d3d779f6475474bd7fc80d1f9cce91c7b42fc958)
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 #include <sys/param.h>
39 #include <sys/disk.h>
40 #include <sys/stat.h>
41 #define FSTYPENAMES
42 #include <sys/disklabel.h>
43 #include <sys/file.h>
44 #include <sys/sysctl.h>
45 
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
48 
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <limits.h>
53 #include <stdint.h>
54 #include <string.h>
55 #include <libufs.h>
56 
57 #include "fsck.h"
58 
59 struct inohash *inphash;	       /* hash list of directory inode info */
60 struct inoinfo **inpsort;	       /* disk order list of directory inodes */
61 struct inode snaplist[FSMAXSNAP + 1];  /* list of active snapshots */
62 int snapcnt;			       /* number of active snapshots */
63 char *copybuf;			       /* buffer to copy snapshot blocks */
64 
65 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
66 
67 static int calcsb(char *dev, int devfd, struct fs *fs);
68 static void saverecovery(int readfd, int writefd);
69 static int chkrecovery(int devfd);
70 static int getlbnblkno(struct inodesc *);
71 static int checksnapinfo(struct inode *);
72 
73 /*
74  * Read in a superblock finding an alternate if necessary.
75  * Return 1 if successful, 0 if unsuccessful, -1 if file system
76  * is already clean (ckclean and preen mode only).
77  */
78 int
setup(char * dev)79 setup(char *dev)
80 {
81 	long i, cg, bmapsize;
82 	struct inode ip;
83 	struct fs proto;
84 
85 	/*
86 	 * We are expected to have an open file descriptor
87 	 */
88 	if (fsreadfd < 0)
89 		return (0);
90 	/*
91 	 * If we do not yet have a superblock, read it in looking
92 	 * for alternates if necessary.
93 	 */
94 	if (havesb == 0 && readsb(1) == 0) {
95 		skipclean = 0;
96 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
97 			return(0);
98 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
99 			return (0);
100 		for (cg = 0; cg < proto.fs_ncg; cg++) {
101 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
102 			if (readsb(0) != 0)
103 				break;
104 		}
105 		if (cg >= proto.fs_ncg) {
106 			printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
107 			    "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
108 			    "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
109 			    "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
110 			bflag = 0;
111 			return(0);
112 		}
113 		pwarn("USING ALTERNATE SUPERBLOCK AT %jd\n", bflag);
114 		bflag = 0;
115 	}
116 	if (preen == 0)
117 		printf("** %s", dev);
118 	if (bkgrdflag == 0 &&
119 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
120 		fswritefd = -1;
121 		if (preen)
122 			pfatal("NO WRITE ACCESS");
123 		printf(" (NO WRITE)");
124 	}
125 	if (preen == 0)
126 		printf("\n");
127 	if (sbhashfailed != 0) {
128 		pwarn("SUPERBLOCK CHECK HASH FAILED");
129 		if (fswritefd == -1)
130 			pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
131 		else if (preen || reply("CORRECT CHECK HASH") != 0) {
132 			if (preen)
133 				printf(" (CORRECTED)\n");
134 			sblock.fs_clean = 0;
135 			sbdirty();
136 		}
137 	}
138 	if (skipclean && ckclean && sblock.fs_clean) {
139 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
140 		return (-1);
141 	}
142 	maxfsblock = sblock.fs_size;
143 	maxino = sblock.fs_ncg * sblock.fs_ipg;
144 	/*
145 	 * Check and potentially fix certain fields in the super block.
146 	 */
147 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
148 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
149 		if (reply("SET TO DEFAULT") == 1) {
150 			sblock.fs_optim = FS_OPTTIME;
151 			sbdirty();
152 		}
153 	}
154 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
155 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
156 			sblock.fs_minfree);
157 		if (reply("SET TO DEFAULT") == 1) {
158 			sblock.fs_minfree = 10;
159 			sbdirty();
160 		}
161 	}
162 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
163 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
164 		pwarn("Format of file system is too old.\n");
165 		pwarn("Must update to modern format using a version of fsck\n");
166 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
167 		exit(EEXIT);
168 	}
169 	if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
170 	    fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
171 	    reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
172 		saverecovery(fsreadfd, fswritefd);
173 	/*
174 	 * allocate and initialize the necessary maps
175 	 */
176 	bufinit();
177 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
178 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
179 	if (blockmap == NULL) {
180 		printf("cannot alloc %u bytes for blockmap\n",
181 		    (unsigned)bmapsize);
182 		goto badsb;
183 	}
184 	inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
185 	if (inostathead == NULL) {
186 		printf("cannot alloc %u bytes for inostathead\n",
187 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
188 		goto badsb;
189 	}
190 	numdirs = sblock.fs_cstotal.cs_ndir;
191 	dirhash = MAX(numdirs / 2, 1);
192 	inplast = 0;
193 	listmax = numdirs + 10;
194 	inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
195 	inphash = (struct inohash *)Calloc(dirhash, sizeof(struct inohash));
196 	if (inpsort == NULL || inphash == NULL) {
197 		printf("cannot alloc %ju bytes for inphash\n",
198 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
199 		goto badsb;
200 	}
201 	if (sblock.fs_flags & FS_DOSOFTDEP)
202 		usedsoftdep = 1;
203 	else
204 		usedsoftdep = 0;
205 	/*
206 	 * Collect any snapshot inodes so that we can allow them to
207 	 * claim any blocks that we free. The code for doing this is
208 	 * imported here and into inode.c from sys/ufs/ffs/ffs_snapshot.c.
209 	 */
210 	for (snapcnt = 0; snapcnt < FSMAXSNAP; snapcnt++) {
211 		if (sblock.fs_snapinum[snapcnt] == 0)
212 			break;
213 		ginode(sblock.fs_snapinum[snapcnt], &ip);
214 		if ((DIP(ip.i_dp, di_mode) & IFMT) == IFREG &&
215 		    (DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) != 0 &&
216 		    checksnapinfo(&ip)) {
217 			if (debug)
218 				printf("Load snapshot %jd\n",
219 				    (intmax_t)sblock.fs_snapinum[snapcnt]);
220 			snaplist[snapcnt] = ip;
221 			continue;
222 		}
223 		printf("Removing non-snapshot inode %ju from snapshot list\n",
224 		    (uintmax_t)sblock.fs_snapinum[snapcnt]);
225 		irelse(&ip);
226 		for (i = snapcnt + 1; i < FSMAXSNAP; i++) {
227 			if (sblock.fs_snapinum[i] == 0)
228 				break;
229 			sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
230 		}
231 		sblock.fs_snapinum[i - 1] = 0;
232 		snapcnt--;
233 		sbdirty();
234 	}
235 	if (snapcnt > 0 && copybuf == NULL) {
236 		copybuf = Malloc(sblock.fs_bsize);
237 		if (copybuf == NULL)
238 			errx(EEXIT, "cannot allocate space for snapshot "
239 			    "copy buffer");
240 	}
241 	return (1);
242 
243 badsb:
244 	ckfini(0);
245 	return (0);
246 }
247 
248 /*
249  * Check for valid snapshot information.
250  *
251  * Each snapshot has a list of blocks that have been copied. This list
252  * is consulted before checking the snapshot inode. Its purpose is to
253  * speed checking of commonly checked blocks and to avoid recursive
254  * checks of the snapshot inode. In particular, the list must contain
255  * the superblock, the superblock summary information, and all the
256  * cylinder group blocks. The list may contain other commonly checked
257  * pointers such as those of the blocks that contain the snapshot inodes.
258  * The list is sorted into block order to allow binary search lookup.
259  *
260  * The twelve direct direct block pointers of the snapshot are always
261  * copied, so we test for them first before checking the list itself
262  * (i.e., they are not in the list).
263  *
264  * The checksnapinfo() routine needs to ensure that the list contains at
265  * least the super block, its summary information, and the cylinder groups.
266  * Here we check the list first for the superblock, zero or more cylinder
267  * groups up to the location of the superblock summary information, the
268  * summary group information, and any remaining cylinder group maps that
269  * follow it. We skip over any other entries in the list.
270  */
271 #define CHKBLKINLIST(chkblk)						\
272 	/* All UFS_NDADDR blocks are copied */				\
273 	if ((chkblk) >= UFS_NDADDR) {					\
274 		/* Skip over blocks that are not of interest */		\
275 		while (*blkp < (chkblk) && blkp < lastblkp)		\
276 			blkp++;						\
277 		/* Fail if end of list and not all blocks found */	\
278 		if (blkp >= lastblkp) {					\
279 			pwarn("UFS%d snapshot inode %jd failed: "	\
280 			    "improper block list length (%jd)\n",	\
281 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
282 			    (intmax_t)snapip->i_number,			\
283 			    (intmax_t)(lastblkp - &snapblklist[0]));	\
284 			status = 0;					\
285 		}							\
286 		/* Fail if block we seek is missing */			\
287 		else if (*blkp++ != (chkblk)) {				\
288 			pwarn("UFS%d snapshot inode %jd failed: "	\
289 			    "block list (%jd) != %s (%jd)\n",		\
290 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
291 			    (intmax_t)snapip->i_number,			\
292 			    (intmax_t)blkp[-1],	#chkblk,		\
293 			    (intmax_t)chkblk);				\
294 			status = 0;					\
295 		}							\
296 	}
297 
298 static int
checksnapinfo(struct inode * snapip)299 checksnapinfo(struct inode *snapip)
300 {
301 	struct fs *fs;
302 	struct bufarea *bp;
303 	struct inodesc idesc;
304 	daddr_t *snapblklist, *blkp, *lastblkp, csblkno;
305 	int cg, loc, len, status;
306 	ufs_lbn_t lbn;
307 	size_t size;
308 
309 	fs = &sblock;
310 	memset(&idesc, 0, sizeof(struct inodesc));
311 	idesc.id_type = ADDR;
312 	idesc.id_func = getlbnblkno;
313 	idesc.id_number = snapip->i_number;
314 	lbn = howmany(fs->fs_size, fs->fs_frag);
315 	idesc.id_parent = lbn;		/* sought after blkno */
316 	if ((ckinode(snapip->i_dp, &idesc) & FOUND) == 0)
317 		return (0);
318 	size = fragroundup(fs,
319 	    DIP(snapip->i_dp, di_size) - lblktosize(fs, lbn));
320 	bp = getdatablk(idesc.id_parent, size, BT_DATA);
321 	if (bp->b_errs != 0)
322 		return (0);
323 	snapblklist = (daddr_t *)bp->b_un.b_buf;
324 	/*
325 	 * snapblklist[0] is the size of the list
326 	 * snapblklist[1] is the first element of the list
327 	 *
328 	 * We need to be careful to bound the size of the list and verify
329 	 * that we have not run off the end of it if it or its size has
330 	 * been corrupted.
331 	 */
332 	blkp = &snapblklist[1];
333 	lastblkp = &snapblklist[MAX(0,
334 	    MIN(snapblklist[0] + 1, size / sizeof(daddr_t)))];
335 	status = 1;
336 	/* Check that the superblock is listed. */
337 	CHKBLKINLIST(lblkno(fs, fs->fs_sblockloc));
338 	if (status == 0)
339 		goto out;
340 	/*
341 	 * Calculate where the summary information is located.
342 	 * Usually it is in the first cylinder group, but growfs
343 	 * may move it to the first cylinder group that it adds.
344 	 *
345 	 * Check all cylinder groups up to the summary information.
346 	 */
347 	csblkno = fragstoblks(fs, fs->fs_csaddr);
348 	for (cg = 0; cg < fs->fs_ncg; cg++) {
349 		if (fragstoblks(fs, cgtod(fs, cg)) > csblkno)
350 			break;
351 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
352 		if (status == 0)
353 			goto out;
354 	}
355 	/* Check the summary information block(s). */
356 	len = howmany(fs->fs_cssize, fs->fs_bsize);
357 	for (loc = 0; loc < len; loc++) {
358 		CHKBLKINLIST(csblkno + loc);
359 		if (status == 0)
360 			goto out;
361 	}
362 	/* Check the remaining cylinder groups. */
363 	for (; cg < fs->fs_ncg; cg++) {
364 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
365 		if (status == 0)
366 			goto out;
367 	}
368 out:
369 	brelse(bp);
370 	return (status);
371 }
372 
373 /*
374  * Return the block number associated with a specified inode lbn.
375  * Requested lbn is in id_parent. If found, block is returned in
376  * id_parent.
377  */
378 static int
getlbnblkno(struct inodesc * idesc)379 getlbnblkno(struct inodesc *idesc)
380 {
381 
382 	if (idesc->id_lbn < idesc->id_parent)
383 		return (KEEPON);
384 	idesc->id_parent = idesc->id_blkno;
385 	return (STOP | FOUND);
386 }
387 
388 /*
389  * Open a device or file to be checked by fsck.
390  */
391 int
openfilesys(char * dev)392 openfilesys(char *dev)
393 {
394 	struct stat statb;
395 	int saved_fsreadfd;
396 
397 	if (stat(dev, &statb) < 0)
398 		return (0);
399 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
400 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
401 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
402 			pwarn("BACKGROUND FSCK LACKS A SNAPSHOT\n");
403 			return (0);
404 		}
405 		if (bkgrdflag != 0) {
406 			cursnapshot = statb.st_ino;
407 		} else {
408 			pwarn("%s IS NOT A DISK DEVICE\n", dev);
409 			if (preen || reply("CONTINUE") == 0)
410 				return (0);
411 		}
412 	}
413 	saved_fsreadfd = fsreadfd;
414 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
415 		fsreadfd = saved_fsreadfd;
416 		return (0);
417 	}
418 	if (saved_fsreadfd != -1)
419 		close(saved_fsreadfd);
420 	return (1);
421 }
422 
423 /*
424  * Read in the super block and its summary info.
425  */
426 int
readsb(int listerr)427 readsb(int listerr)
428 {
429 	off_t super;
430 	int ret;
431 	struct fs *fs;
432 
433 	super = bflag ? bflag * dev_bsize :
434 	    sbhashfailed ? STDSB_NOHASHFAIL : STDSB;
435 	readcnt[sblk.b_type]++;
436 	while ((ret = sbget(fsreadfd, &fs, super)) != 0) {
437 		switch (ret) {
438 		case EINTEGRITY:
439 			if (bflag || super == STDSB_NOHASHFAIL)
440 				return (0);
441 			super = STDSB_NOHASHFAIL;
442 			sbhashfailed = 1;
443 			continue;
444 		case ENOENT:
445 			if (bflag)
446 				printf("%jd is not a file system "
447 				    "superblock\n", super / dev_bsize);
448 			else
449 				printf("Cannot find file system "
450 				    "superblock\n");
451 			return (0);
452 		case EIO:
453 		default:
454 			printf("I/O error reading %jd\n",
455 			    super / dev_bsize);
456 			return (0);
457 		}
458 	}
459 	memcpy(&sblock, fs, fs->fs_sbsize);
460 	free(fs);
461 	/*
462 	 * Compute block size that the file system is based on,
463 	 * according to fsbtodb, and adjust superblock block number
464 	 * so we can tell if this is an alternate later.
465 	 */
466 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
467 	sblk.b_bno = sblock.fs_sblockactualloc / dev_bsize;
468 	sblk.b_size = SBLOCKSIZE;
469 	/*
470 	 * If not yet done, update UFS1 superblock with new wider fields.
471 	 */
472 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
473 	    sblock.fs_maxbsize != sblock.fs_bsize) {
474 		sblock.fs_maxbsize = sblock.fs_bsize;
475 		sblock.fs_time = sblock.fs_old_time;
476 		sblock.fs_size = sblock.fs_old_size;
477 		sblock.fs_dsize = sblock.fs_old_dsize;
478 		sblock.fs_csaddr = sblock.fs_old_csaddr;
479 		sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
480 		sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
481 		sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
482 		sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
483 	}
484 	havesb = 1;
485 	return (1);
486 }
487 
488 void
sblock_init(void)489 sblock_init(void)
490 {
491 
492 	fsreadfd = -1;
493 	fswritefd = -1;
494 	fsmodified = 0;
495 	lfdir = 0;
496 	initbarea(&sblk, BT_SUPERBLK);
497 	sblk.b_un.b_buf = Malloc(SBLOCKSIZE);
498 	if (sblk.b_un.b_buf == NULL)
499 		errx(EEXIT, "cannot allocate space for superblock");
500 	dev_bsize = secsize = DEV_BSIZE;
501 }
502 
503 /*
504  * Calculate a prototype superblock based on information in the boot area.
505  * When done the cgsblock macro can be calculated and the fs_ncg field
506  * can be used. Do NOT attempt to use other macros without verifying that
507  * their needed information is available!
508  */
509 static int
calcsb(char * dev,int devfd,struct fs * fs)510 calcsb(char *dev, int devfd, struct fs *fs)
511 {
512 	struct fsrecovery *fsr;
513 	char *fsrbuf;
514 	u_int secsize;
515 
516 	/*
517 	 * We need fragments-per-group and the partition-size.
518 	 *
519 	 * Newfs stores these details at the end of the boot block area
520 	 * at the start of the filesystem partition. If they have been
521 	 * overwritten by a boot block, we fail. But usually they are
522 	 * there and we can use them.
523 	 */
524 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1)
525 		return (0);
526 	fsrbuf = Malloc(secsize);
527 	if (fsrbuf == NULL)
528 		errx(EEXIT, "calcsb: cannot allocate recovery buffer");
529 	if (blread(devfd, fsrbuf,
530 	    (SBLOCK_UFS2 - secsize) / dev_bsize, secsize) != 0) {
531 		free(fsrbuf);
532 		return (0);
533 	}
534 	fsr = (struct fsrecovery *)&fsrbuf[secsize - sizeof *fsr];
535 	if (fsr->fsr_magic != FS_UFS2_MAGIC) {
536 		free(fsrbuf);
537 		return (0);
538 	}
539 	memset(fs, 0, sizeof(struct fs));
540 	fs->fs_fpg = fsr->fsr_fpg;
541 	fs->fs_fsbtodb = fsr->fsr_fsbtodb;
542 	fs->fs_sblkno = fsr->fsr_sblkno;
543 	fs->fs_magic = fsr->fsr_magic;
544 	fs->fs_ncg = fsr->fsr_ncg;
545 	free(fsrbuf);
546 	return (1);
547 }
548 
549 /*
550  * Check to see if recovery information exists.
551  * Return 1 if it exists or cannot be created.
552  * Return 0 if it does not exist and can be created.
553  */
554 static int
chkrecovery(int devfd)555 chkrecovery(int devfd)
556 {
557 	struct fsrecovery *fsr;
558 	char *fsrbuf;
559 	u_int secsize, rdsize;
560 
561 	/*
562 	 * Could not determine if backup material exists, so do not
563 	 * offer to create it.
564 	 */
565 	fsrbuf = NULL;
566 	rdsize = sblock.fs_fsize;
567 	if (ioctl(devfd, DIOCGSECTORSIZE, &secsize) == -1 ||
568 	    rdsize % secsize != 0 ||
569 	    (fsrbuf = Malloc(rdsize)) == NULL ||
570 	    blread(devfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
571 	      rdsize) != 0) {
572 		free(fsrbuf);
573 		return (1);
574 	}
575 	/*
576 	 * Recovery material has already been created, so do not
577 	 * need to create it again.
578 	 */
579 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
580 	if (fsr->fsr_magic == FS_UFS2_MAGIC) {
581 		free(fsrbuf);
582 		return (1);
583 	}
584 	/*
585 	 * Recovery material has not been created and can be if desired.
586 	 */
587 	free(fsrbuf);
588 	return (0);
589 }
590 
591 /*
592  * Read the last filesystem-size piece of the boot block, replace the
593  * last 20 bytes with the recovery information, then write it back.
594  * The recovery information only works for UFS2 filesystems.
595  */
596 static void
saverecovery(int readfd,int writefd)597 saverecovery(int readfd, int writefd)
598 {
599 	struct fsrecovery *fsr;
600 	char *fsrbuf;
601 	u_int secsize, rdsize;
602 
603 	fsrbuf = NULL;
604 	rdsize = sblock.fs_fsize;
605 	if (sblock.fs_magic != FS_UFS2_MAGIC ||
606 	    ioctl(readfd, DIOCGSECTORSIZE, &secsize) == -1 ||
607 	    rdsize % secsize != 0 ||
608 	    (fsrbuf = Malloc(rdsize)) == NULL ||
609 	    blread(readfd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize,
610 	      rdsize) != 0) {
611 		printf("RECOVERY DATA COULD NOT BE CREATED\n");
612 		free(fsrbuf);
613 		return;
614 	}
615 	fsr = (struct fsrecovery *)&fsrbuf[rdsize - sizeof *fsr];
616 	fsr->fsr_magic = sblock.fs_magic;
617 	fsr->fsr_fpg = sblock.fs_fpg;
618 	fsr->fsr_fsbtodb = sblock.fs_fsbtodb;
619 	fsr->fsr_sblkno = sblock.fs_sblkno;
620 	fsr->fsr_ncg = sblock.fs_ncg;
621 	blwrite(writefd, fsrbuf, (SBLOCK_UFS2 - rdsize) / dev_bsize, rdsize);
622 	free(fsrbuf);
623 }
624