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