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