xref: /freebsd/sbin/fsck_ffs/setup.c (revision 3494f7c0)
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 #include <sys/param.h>
33 #include <sys/disk.h>
34 #include <sys/stat.h>
35 #define FSTYPENAMES
36 #include <sys/disklabel.h>
37 #include <sys/file.h>
38 #include <sys/sysctl.h>
39 
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <stdint.h>
48 #include <string.h>
49 
50 #include "fsck.h"
51 
52 struct inohash *inphash;	       /* hash list of directory inode info */
53 struct inoinfo **inpsort;	       /* disk order list of directory inodes */
54 struct inode snaplist[FSMAXSNAP + 1];  /* list of active snapshots */
55 int snapcnt;			       /* number of active snapshots */
56 char *copybuf;			       /* buffer to copy snapshot blocks */
57 
58 static int sbhashfailed;
59 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
60 
61 static int calcsb(char *dev, int devfd, struct fs *fs);
62 static void saverecovery(int readfd, int writefd);
63 static int chkrecovery(int devfd);
64 static int getlbnblkno(struct inodesc *);
65 static int checksnapinfo(struct inode *);
66 
67 /*
68  * Read in a superblock finding an alternate if necessary.
69  * Return 1 if successful, 0 if unsuccessful, -1 if file system
70  * is already clean (ckclean and preen mode only).
71  */
72 int
73 setup(char *dev)
74 {
75 	long i, bmapsize;
76 	struct inode ip;
77 
78 	/*
79 	 * We are expected to have an open file descriptor and a superblock.
80 	 */
81 	if (fsreadfd < 0 || havesb == 0) {
82 		if (debug) {
83 			if (fsreadfd < 0)
84 				printf("setup: missing fsreadfd\n");
85 			else
86 				printf("setup: missing superblock\n");
87 		}
88 		return (0);
89 	}
90 	if (preen == 0)
91 		printf("** %s", dev);
92 	if (bkgrdflag == 0 &&
93 	    (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
94 		fswritefd = -1;
95 		if (preen)
96 			pfatal("NO WRITE ACCESS");
97 		printf(" (NO WRITE)");
98 	}
99 	if (preen == 0)
100 		printf("\n");
101 	if (sbhashfailed != 0) {
102 		pwarn("SUPERBLOCK CHECK HASH FAILED");
103 		if (fswritefd == -1)
104 			pwarn("OPENED READONLY SO CANNOT CORRECT CHECK HASH\n");
105 		else if (preen || reply("CORRECT CHECK HASH") != 0) {
106 			if (preen)
107 				printf(" (CORRECTED)\n");
108 			sblock.fs_clean = 0;
109 			sbdirty();
110 		}
111 	}
112 	if (skipclean && ckclean && sblock.fs_clean) {
113 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
114 		return (-1);
115 	}
116 	maxfsblock = sblock.fs_size;
117 	maxino = sblock.fs_ncg * sblock.fs_ipg;
118 	/*
119 	 * Check and potentially fix certain fields in the super block.
120 	 */
121 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
122 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
123 		if (reply("SET TO DEFAULT") == 1) {
124 			sblock.fs_optim = FS_OPTTIME;
125 			sbdirty();
126 		}
127 	}
128 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
129 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
130 			sblock.fs_minfree);
131 		if (reply("SET TO DEFAULT") == 1) {
132 			sblock.fs_minfree = 10;
133 			sbdirty();
134 		}
135 	}
136 	if (sblock.fs_magic == FS_UFS1_MAGIC &&
137 	    sblock.fs_old_inodefmt < FS_44INODEFMT) {
138 		pwarn("Format of file system is too old.\n");
139 		pwarn("Must update to modern format using a version of fsck\n");
140 		pfatal("from before 2002 with the command ``fsck -c 2''\n");
141 		exit(EEXIT);
142 	}
143 	if (preen == 0 && yflag == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
144 	    fswritefd != -1 && chkrecovery(fsreadfd) == 0 &&
145 	    reply("SAVE DATA TO FIND ALTERNATE SUPERBLOCKS") != 0)
146 		saverecovery(fsreadfd, fswritefd);
147 	/*
148 	 * allocate and initialize the necessary maps
149 	 */
150 	bufinit();
151 	bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short));
152 	blockmap = Calloc((unsigned)bmapsize, sizeof (char));
153 	if (blockmap == NULL) {
154 		printf("cannot alloc %u bytes for blockmap\n",
155 		    (unsigned)bmapsize);
156 		goto badsb;
157 	}
158 	inostathead = Calloc(sblock.fs_ncg, sizeof(struct inostatlist));
159 	if (inostathead == NULL) {
160 		printf("cannot alloc %u bytes for inostathead\n",
161 		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
162 		goto badsb;
163 	}
164 	numdirs = sblock.fs_cstotal.cs_ndir;
165 	dirhash = MAX(numdirs / 2, 1);
166 	inplast = 0;
167 	listmax = numdirs + 10;
168 	inpsort = (struct inoinfo **)Calloc(listmax, sizeof(struct inoinfo *));
169 	inphash = (struct inohash *)Calloc(dirhash, sizeof(struct inohash));
170 	if (inpsort == NULL || inphash == NULL) {
171 		printf("cannot alloc %ju bytes for inphash\n",
172 		    (uintmax_t)numdirs * sizeof(struct inoinfo *));
173 		goto badsb;
174 	}
175 	if (sblock.fs_flags & FS_DOSOFTDEP)
176 		usedsoftdep = 1;
177 	else
178 		usedsoftdep = 0;
179 	/*
180 	 * Collect any snapshot inodes so that we can allow them to
181 	 * claim any blocks that we free. The code for doing this is
182 	 * imported here and into inode.c from sys/ufs/ffs/ffs_snapshot.c.
183 	 */
184 	for (snapcnt = 0; snapcnt < FSMAXSNAP; snapcnt++) {
185 		if (sblock.fs_snapinum[snapcnt] == 0)
186 			break;
187 		ginode(sblock.fs_snapinum[snapcnt], &ip);
188 		if ((DIP(ip.i_dp, di_mode) & IFMT) == IFREG &&
189 		    (DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) != 0 &&
190 		    checksnapinfo(&ip)) {
191 			if (debug)
192 				printf("Load snapshot %jd\n",
193 				    (intmax_t)sblock.fs_snapinum[snapcnt]);
194 			snaplist[snapcnt] = ip;
195 			continue;
196 		}
197 		printf("Removing non-snapshot inode %ju from snapshot list\n",
198 		    (uintmax_t)sblock.fs_snapinum[snapcnt]);
199 		irelse(&ip);
200 		for (i = snapcnt + 1; i < FSMAXSNAP; i++) {
201 			if (sblock.fs_snapinum[i] == 0)
202 				break;
203 			sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
204 		}
205 		sblock.fs_snapinum[i - 1] = 0;
206 		snapcnt--;
207 		sbdirty();
208 	}
209 	if (snapcnt > 0 && copybuf == NULL) {
210 		copybuf = Balloc(sblock.fs_bsize);
211 		if (copybuf == NULL)
212 			errx(EEXIT, "cannot allocate space for snapshot "
213 			    "copy buffer");
214 	}
215 	return (1);
216 
217 badsb:
218 	ckfini(0);
219 	return (0);
220 }
221 
222 /*
223  * Check for valid snapshot information.
224  *
225  * Each snapshot has a list of blocks that have been copied. This list
226  * is consulted before checking the snapshot inode. Its purpose is to
227  * speed checking of commonly checked blocks and to avoid recursive
228  * checks of the snapshot inode. In particular, the list must contain
229  * the superblock, the superblock summary information, and all the
230  * cylinder group blocks. The list may contain other commonly checked
231  * pointers such as those of the blocks that contain the snapshot inodes.
232  * The list is sorted into block order to allow binary search lookup.
233  *
234  * The twelve direct direct block pointers of the snapshot are always
235  * copied, so we test for them first before checking the list itself
236  * (i.e., they are not in the list).
237  *
238  * The checksnapinfo() routine needs to ensure that the list contains at
239  * least the super block, its summary information, and the cylinder groups.
240  * Here we check the list first for the superblock, zero or more cylinder
241  * groups up to the location of the superblock summary information, the
242  * summary group information, and any remaining cylinder group maps that
243  * follow it. We skip over any other entries in the list.
244  */
245 #define CHKBLKINLIST(chkblk)						\
246 	/* All UFS_NDADDR blocks are copied */				\
247 	if ((chkblk) >= UFS_NDADDR) {					\
248 		/* Skip over blocks that are not of interest */		\
249 		while (*blkp < (chkblk) && blkp < lastblkp)		\
250 			blkp++;						\
251 		/* Fail if end of list and not all blocks found */	\
252 		if (blkp >= lastblkp) {					\
253 			pwarn("UFS%d snapshot inode %jd failed: "	\
254 			    "improper block list length (%jd)\n",	\
255 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
256 			    (intmax_t)snapip->i_number,			\
257 			    (intmax_t)(lastblkp - &snapblklist[0]));	\
258 			status = 0;					\
259 		}							\
260 		/* Fail if block we seek is missing */			\
261 		else if (*blkp++ != (chkblk)) {				\
262 			pwarn("UFS%d snapshot inode %jd failed: "	\
263 			    "block list (%jd) != %s (%jd)\n",		\
264 			    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
265 			    (intmax_t)snapip->i_number,			\
266 			    (intmax_t)blkp[-1],	#chkblk,		\
267 			    (intmax_t)chkblk);				\
268 			status = 0;					\
269 		}							\
270 	}
271 
272 static int
273 checksnapinfo(struct inode *snapip)
274 {
275 	struct fs *fs;
276 	struct bufarea *bp;
277 	struct inodesc idesc;
278 	daddr_t *snapblklist, *blkp, *lastblkp, csblkno;
279 	int cg, loc, len, status;
280 	ufs_lbn_t lbn;
281 	size_t size;
282 
283 	fs = &sblock;
284 	memset(&idesc, 0, sizeof(struct inodesc));
285 	idesc.id_type = ADDR;
286 	idesc.id_func = getlbnblkno;
287 	idesc.id_number = snapip->i_number;
288 	lbn = howmany(fs->fs_size, fs->fs_frag);
289 	idesc.id_parent = lbn;		/* sought after blkno */
290 	if ((ckinode(snapip->i_dp, &idesc) & FOUND) == 0)
291 		return (0);
292 	size = fragroundup(fs,
293 	    DIP(snapip->i_dp, di_size) - lblktosize(fs, lbn));
294 	bp = getdatablk(idesc.id_parent, size, BT_DATA);
295 	if (bp->b_errs != 0)
296 		return (0);
297 	snapblklist = (daddr_t *)bp->b_un.b_buf;
298 	/*
299 	 * snapblklist[0] is the size of the list
300 	 * snapblklist[1] is the first element of the list
301 	 *
302 	 * We need to be careful to bound the size of the list and verify
303 	 * that we have not run off the end of it if it or its size has
304 	 * been corrupted.
305 	 */
306 	blkp = &snapblklist[1];
307 	lastblkp = &snapblklist[MAX(0,
308 	    MIN(snapblklist[0] + 1, size / sizeof(daddr_t)))];
309 	status = 1;
310 	/* Check that the superblock is listed. */
311 	CHKBLKINLIST(lblkno(fs, fs->fs_sblockloc));
312 	if (status == 0)
313 		goto out;
314 	/*
315 	 * Calculate where the summary information is located.
316 	 * Usually it is in the first cylinder group, but growfs
317 	 * may move it to the first cylinder group that it adds.
318 	 *
319 	 * Check all cylinder groups up to the summary information.
320 	 */
321 	csblkno = fragstoblks(fs, fs->fs_csaddr);
322 	for (cg = 0; cg < fs->fs_ncg; cg++) {
323 		if (fragstoblks(fs, cgtod(fs, cg)) > csblkno)
324 			break;
325 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
326 		if (status == 0)
327 			goto out;
328 	}
329 	/* Check the summary information block(s). */
330 	len = howmany(fs->fs_cssize, fs->fs_bsize);
331 	for (loc = 0; loc < len; loc++) {
332 		CHKBLKINLIST(csblkno + loc);
333 		if (status == 0)
334 			goto out;
335 	}
336 	/* Check the remaining cylinder groups. */
337 	for (; cg < fs->fs_ncg; cg++) {
338 		CHKBLKINLIST(fragstoblks(fs, cgtod(fs, cg)));
339 		if (status == 0)
340 			goto out;
341 	}
342 out:
343 	brelse(bp);
344 	return (status);
345 }
346 
347 /*
348  * Return the block number associated with a specified inode lbn.
349  * Requested lbn is in id_parent. If found, block is returned in
350  * id_parent.
351  */
352 static int
353 getlbnblkno(struct inodesc *idesc)
354 {
355 
356 	if (idesc->id_lbn < idesc->id_parent)
357 		return (KEEPON);
358 	idesc->id_parent = idesc->id_blkno;
359 	return (STOP | FOUND);
360 }
361 
362 /*
363  * Open a device or file to be checked by fsck.
364  */
365 int
366 openfilesys(char *dev)
367 {
368 	struct stat statb;
369 	int saved_fsreadfd;
370 
371 	if (stat(dev, &statb) < 0)
372 		return (0);
373 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
374 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
375 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
376 			pwarn("BACKGROUND FSCK LACKS A SNAPSHOT\n");
377 			return (0);
378 		}
379 		if (bkgrdflag != 0) {
380 			cursnapshot = statb.st_ino;
381 		} else {
382 			pwarn("%s IS NOT A DISK DEVICE\n", dev);
383 			if (preen || reply("CONTINUE") == 0)
384 				return (0);
385 		}
386 	}
387 	saved_fsreadfd = fsreadfd;
388 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
389 		fsreadfd = saved_fsreadfd;
390 		return (0);
391 	}
392 	if (saved_fsreadfd != -1)
393 		close(saved_fsreadfd);
394 	return (1);
395 }
396 
397 /*
398  * Read in the super block and its summary info.
399  */
400 int
401 readsb(void)
402 {
403 	struct fs *fs;
404 
405 	sbhashfailed = 0;
406 	readcnt[sblk.b_type]++;
407 	/*
408 	 * If bflag is given, then check just that superblock.
409 	 */
410 	if (bflag) {
411 		switch (sbget(fsreadfd, &fs, bflag * dev_bsize, 0)) {
412 		case 0:
413 			goto goodsb;
414 		case EINTEGRITY:
415 			printf("Check hash failed for superblock at %jd\n",
416 			    bflag);
417 			return (0);
418 		case ENOENT:
419 			printf("%jd is not a file system superblock\n", bflag);
420 			return (0);
421 		case EIO:
422 		default:
423 			printf("I/O error reading %jd\n", bflag);
424 			return (0);
425 		}
426 	}
427 	/*
428 	 * Check for the standard superblock and use it if good.
429 	 */
430 	if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG) == 0)
431 		goto goodsb;
432 	/*
433 	 * Check if the only problem is a check-hash failure.
434 	 */
435 	skipclean = 0;
436 	if (sbget(fsreadfd, &fs, UFS_STDSB, UFS_NOMSG | UFS_NOHASHFAIL) == 0) {
437 		sbhashfailed = 1;
438 		goto goodsb;
439 	}
440 	/*
441 	 * Do an exhaustive search for a usable superblock.
442 	 */
443 	switch (sbsearch(fsreadfd, &fs, 0)) {
444 	case 0:
445 		goto goodsb;
446 	case ENOENT:
447 		printf("SEARCH FOR ALTERNATE SUPER-BLOCK FAILED. "
448 		    "YOU MUST USE THE\n-b OPTION TO FSCK TO SPECIFY "
449 		    "THE LOCATION OF AN ALTERNATE\nSUPER-BLOCK TO "
450 		    "SUPPLY NEEDED INFORMATION; SEE fsck_ffs(8).\n");
451 		return (0);
452 	case EIO:
453 	default:
454 		printf("I/O error reading a usable superblock\n");
455 		return (0);
456 	}
457 
458 goodsb:
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
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 = Balloc(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
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 = Balloc(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
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 = Balloc(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
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 = Balloc(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