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