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