xref: /netbsd-src/sbin/dump_lfs/lfs_inode.c (revision 51508882aa8c0792e7e0cc0b2078738a24c4b585)
1*51508882Smsaitoh /*      $NetBSD: lfs_inode.c,v 1.29 2024/05/12 23:55:57 msaitoh Exp $ */
254fd3dd5Sperseant 
354fd3dd5Sperseant /*-
454fd3dd5Sperseant  * Copyright (c) 1980, 1991, 1993, 1994
554fd3dd5Sperseant  *      The Regents of the University of California.  All rights reserved.
654fd3dd5Sperseant  *
754fd3dd5Sperseant  * Redistribution and use in source and binary forms, with or without
854fd3dd5Sperseant  * modification, are permitted provided that the following conditions
954fd3dd5Sperseant  * are met:
1054fd3dd5Sperseant  * 1. Redistributions of source code must retain the above copyright
1154fd3dd5Sperseant  *    notice, this list of conditions and the following disclaimer.
1254fd3dd5Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
1354fd3dd5Sperseant  *    notice, this list of conditions and the following disclaimer in the
1454fd3dd5Sperseant  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
1654fd3dd5Sperseant  *    may be used to endorse or promote products derived from this software
1754fd3dd5Sperseant  *    without specific prior written permission.
1854fd3dd5Sperseant  *
1954fd3dd5Sperseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2054fd3dd5Sperseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2154fd3dd5Sperseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2254fd3dd5Sperseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2354fd3dd5Sperseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2454fd3dd5Sperseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2554fd3dd5Sperseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2654fd3dd5Sperseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2754fd3dd5Sperseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2854fd3dd5Sperseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2954fd3dd5Sperseant  * SUCH DAMAGE.
3054fd3dd5Sperseant  */
3154fd3dd5Sperseant 
3254fd3dd5Sperseant #include <sys/cdefs.h>
3354fd3dd5Sperseant #ifndef lint
346543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
356543a91fSlukem  The Regents of the University of California.  All rights reserved.");
3654fd3dd5Sperseant #endif /* not lint */
3754fd3dd5Sperseant 
3854fd3dd5Sperseant #ifndef lint
3954fd3dd5Sperseant #if 0
4054fd3dd5Sperseant static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/1/95";
4154fd3dd5Sperseant #else
42*51508882Smsaitoh __RCSID("$NetBSD: lfs_inode.c,v 1.29 2024/05/12 23:55:57 msaitoh Exp $");
4354fd3dd5Sperseant #endif
4454fd3dd5Sperseant #endif /* not lint */
4554fd3dd5Sperseant 
4654fd3dd5Sperseant #include <sys/param.h>
4754fd3dd5Sperseant #include <sys/time.h>
4854fd3dd5Sperseant #include <sys/stat.h>
4954fd3dd5Sperseant #include <sys/mount.h>
5054fd3dd5Sperseant 
5154fd3dd5Sperseant #include <ctype.h>
5254fd3dd5Sperseant #include <errno.h>
531c57171fSperseant #include <fcntl.h>
5454fd3dd5Sperseant #include <fts.h>
5554fd3dd5Sperseant #include <stdio.h>
5654fd3dd5Sperseant #include <string.h>
5754fd3dd5Sperseant #include <unistd.h>
5854fd3dd5Sperseant 
5954fd3dd5Sperseant #include "dump.h"
60ec6fb314Schristos #undef di_inumber
6154fd3dd5Sperseant 
6254fd3dd5Sperseant #define	HASDUMPEDFILE	0x1
6354fd3dd5Sperseant #define	HASSUBDIRS	0x2
6454fd3dd5Sperseant 
6554fd3dd5Sperseant struct lfs *sblock;
6654fd3dd5Sperseant 
6742614ed3Sfvdl int is_ufs2 = 0;
6842614ed3Sfvdl 
6954fd3dd5Sperseant /*
7054fd3dd5Sperseant  * Read the superblock from disk, and check its magic number.
7154fd3dd5Sperseant  * Determine whether byte-swapping needs to be done on this filesystem.
7254fd3dd5Sperseant  */
7354fd3dd5Sperseant int
fs_read_sblock(char * superblock)746c07f34bSlukem fs_read_sblock(char *superblock)
7554fd3dd5Sperseant {
7606de5f4eSdholland 	/*
7706de5f4eSdholland 	 * XXX this should not be assuming that the in-memory
7806de5f4eSdholland 	 * superblock has the on-disk superblock at the front of the
7906de5f4eSdholland 	 * structure.
8006de5f4eSdholland 	 */
816e76d2b8Schristos 	union {
824e3fced9Sperseant 		char tbuf[LFS_SBPAD];
836e76d2b8Schristos 		struct lfs lfss;
846e76d2b8Schristos 	} u;
856e76d2b8Schristos 
864e3fced9Sperseant 	off_t sboff = LFS_LABELPAD;
8754fd3dd5Sperseant 
886c07f34bSlukem 	sblock = (struct lfs *)superblock;
894e3fced9Sperseant 	while(1) {
904e3fced9Sperseant 		rawread(sboff, (char *) sblock, LFS_SBPAD);
9106de5f4eSdholland 		switch (sblock->lfs_dlfs_u.u_32.dlfs_magic) {
9206de5f4eSdholland 		    case LFS_MAGIC:
9306de5f4eSdholland 			sblock->lfs_is64 = false;
9406de5f4eSdholland 			sblock->lfs_dobyteswap = false;
9506de5f4eSdholland 			break;
9606de5f4eSdholland 		    case LFS_MAGIC_SWAPPED:
9706de5f4eSdholland 			sblock->lfs_is64 = false;
9806de5f4eSdholland 			sblock->lfs_dobyteswap = true;
9906de5f4eSdholland 			break;
10006de5f4eSdholland 		    case LFS64_MAGIC:
10106de5f4eSdholland 			sblock->lfs_is64 = true;
10206de5f4eSdholland 			sblock->lfs_dobyteswap = false;
10306de5f4eSdholland 			break;
10406de5f4eSdholland 		    case LFS64_MAGIC_SWAPPED:
10506de5f4eSdholland 			sblock->lfs_is64 = true;
10606de5f4eSdholland 			sblock->lfs_dobyteswap = true;
10706de5f4eSdholland 			break;
10806de5f4eSdholland 		    default:
10954fd3dd5Sperseant 			quit("bad sblock magic number\n");
11006de5f4eSdholland 			break;
11154fd3dd5Sperseant 		}
112adca8af5Sdholland 		if (lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 0)) != sboff) {
113adca8af5Sdholland 			sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 0));
1144e3fced9Sperseant 			continue;
1154e3fced9Sperseant 		}
1164e3fced9Sperseant 		break;
1174e3fced9Sperseant 	}
1184e3fced9Sperseant 
1194e3fced9Sperseant 	/*
1204e3fced9Sperseant 	 * Read the secondary and take the older of the two
1214e3fced9Sperseant 	 */
122adca8af5Sdholland 	rawread(lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1)), u.tbuf,
1236e76d2b8Schristos 	    sizeof(u.tbuf));
12406de5f4eSdholland 
12506de5f4eSdholland 	if (u.lfss.lfs_dlfs_u.u_32.dlfs_magic !=
12606de5f4eSdholland 	    sblock->lfs_dlfs_u.u_32.dlfs_magic) {
12706de5f4eSdholland 		msg("Warning: secondary superblock at 0x%" PRIx64 " mismatched or wrong magic\n",
128adca8af5Sdholland 			LFS_FSBTODB(sblock, (off_t)lfs_sb_getsboff(sblock, 1)));
1294e3fced9Sperseant 	} else {
13006de5f4eSdholland 		u.lfss.lfs_is64 = sblock->lfs_is64;
13106de5f4eSdholland 		u.lfss.lfs_dobyteswap = sblock->lfs_dobyteswap;
13206de5f4eSdholland 
133992b9a23Sdholland 		if (lfs_sb_getversion(sblock) > 1) {
134f59b8f4bSdholland 			if (lfs_sb_getserial(&u.lfss) < lfs_sb_getserial(sblock)) {
1356e76d2b8Schristos 				memcpy(sblock, u.tbuf, sizeof(u.tbuf));
136adca8af5Sdholland 				sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1));
1374e3fced9Sperseant 			}
1384e3fced9Sperseant 		} else {
139f59b8f4bSdholland 			if (lfs_sb_getotstamp(&u.lfss) < lfs_sb_getotstamp(sblock)) {
1406e76d2b8Schristos 				memcpy(sblock, u.tbuf, sizeof(u.tbuf));
141adca8af5Sdholland 				sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1));
1424e3fced9Sperseant 			}
1434e3fced9Sperseant 		}
1444e3fced9Sperseant 	}
1454e3fced9Sperseant 	if (sboff != LFS_SBPAD) {
1464e3fced9Sperseant 		msg("Using superblock at alternate location 0x%lx\n",
1474e3fced9Sperseant 		    (unsigned long)(btodb(sboff)));
1484e3fced9Sperseant 	}
1494e3fced9Sperseant 
15006de5f4eSdholland 	/* ugh */
15106de5f4eSdholland 	is_ufs2 = sblock->lfs_is64;
15206de5f4eSdholland 
15306de5f4eSdholland 	/*
15406de5f4eSdholland 	 * XXX for now dump won't work on lfs64 because of the 64-bit
15506de5f4eSdholland 	 * inodes in directories. dump needs more abstraction.
15606de5f4eSdholland 	 */
15706de5f4eSdholland 	if (sblock->lfs_is64) {
15806de5f4eSdholland 		quit("LFS64 directory entries not supported yet");
15906de5f4eSdholland 	}
16006de5f4eSdholland 
16106de5f4eSdholland 	return sblock->lfs_dobyteswap;
16254fd3dd5Sperseant }
16354fd3dd5Sperseant 
16454fd3dd5Sperseant /*
16554fd3dd5Sperseant  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
16654fd3dd5Sperseant  * variables.
16754fd3dd5Sperseant  */
16854fd3dd5Sperseant struct ufsi *
fs_parametrize(void)16954fd3dd5Sperseant fs_parametrize(void)
17054fd3dd5Sperseant {
17154fd3dd5Sperseant 	static struct ufsi ufsi;
17254fd3dd5Sperseant 
1732763cc19Sperseant 	spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
1742763cc19Sperseant 
175f59b8f4bSdholland 	ufsi.ufs_dsize = LFS_FSBTODB(sblock, lfs_sb_getsize(sblock));
176992b9a23Sdholland 	if (lfs_sb_getversion(sblock) == 1)
177adca8af5Sdholland 		ufsi.ufs_dsize = lfs_sb_getsize(sblock) >> lfs_sb_getblktodb(sblock);
178f59b8f4bSdholland 	ufsi.ufs_bsize = lfs_sb_getbsize(sblock);
179adca8af5Sdholland 	ufsi.ufs_bshift = lfs_sb_getbshift(sblock);
180f59b8f4bSdholland 	ufsi.ufs_fsize = lfs_sb_getfsize(sblock);
181f59b8f4bSdholland 	ufsi.ufs_frag = lfs_sb_getfrag(sblock);
182adca8af5Sdholland 	ufsi.ufs_fsatoda = lfs_sb_getfsbtodb(sblock);
183992b9a23Sdholland 	if (lfs_sb_getversion(sblock) == 1)
18454fd3dd5Sperseant 		ufsi.ufs_fsatoda = 0;
185adca8af5Sdholland 	ufsi.ufs_nindir = lfs_sb_getnindir(sblock);
186adca8af5Sdholland 	ufsi.ufs_inopb = lfs_sb_getinopb(sblock);
187adca8af5Sdholland 	ufsi.ufs_maxsymlinklen = lfs_sb_getmaxsymlinklen(sblock);
188adca8af5Sdholland 	ufsi.ufs_bmask = ~(lfs_sb_getbmask(sblock));
189adca8af5Sdholland 	ufsi.ufs_qbmask = lfs_sb_getbmask(sblock);
190adca8af5Sdholland 	ufsi.ufs_fmask = ~(lfs_sb_getffmask(sblock));
191adca8af5Sdholland 	ufsi.ufs_qfmask = lfs_sb_getffmask(sblock);
19254fd3dd5Sperseant 
193adca8af5Sdholland 	dev_bsize = lfs_sb_getbsize(sblock) >> lfs_sb_getblktodb(sblock);
19454fd3dd5Sperseant 
19554fd3dd5Sperseant 	return &ufsi;
19654fd3dd5Sperseant }
19754fd3dd5Sperseant 
19854fd3dd5Sperseant ino_t
fs_maxino(void)19954fd3dd5Sperseant fs_maxino(void)
20054fd3dd5Sperseant {
201eb2560adSdholland 	return ((getino(LFS_IFILE_INUM)->dp1.di_size
202f59b8f4bSdholland 		   - (lfs_sb_getcleansz(sblock) + lfs_sb_getsegtabsz(sblock))
203f59b8f4bSdholland 		   * lfs_sb_getbsize(sblock))
204f59b8f4bSdholland 		  / lfs_sb_getbsize(sblock)) * lfs_sb_getifpb(sblock) - 1;
20554fd3dd5Sperseant }
20654fd3dd5Sperseant 
20742614ed3Sfvdl void
fs_mapinodes(ino_t maxino,u_int64_t * tapesz,int * anydirskipped)20842614ed3Sfvdl fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped)
20942614ed3Sfvdl {
21042614ed3Sfvdl 	ino_t ino;
21142614ed3Sfvdl 
212dc37d54cSdholland 	for (ino = ULFS_ROOTINO; ino < maxino; ino++)
21342614ed3Sfvdl 		mapfileino(ino, tapesz, anydirskipped);
21442614ed3Sfvdl }
21542614ed3Sfvdl 
21654fd3dd5Sperseant /*
21754fd3dd5Sperseant  * XXX KS - I know there's a better way to do this.
21854fd3dd5Sperseant  */
219dc37d54cSdholland #define BASE_SINDIR (ULFS_NDADDR)
2208c01dce5Sdholland #define BASE_DINDIR (ULFS_NDADDR+LFS_NINDIR(fs))
2218c01dce5Sdholland #define BASE_TINDIR (ULFS_NDADDR+LFS_NINDIR(fs)+LFS_NINDIR(fs)*LFS_NINDIR(fs))
22254fd3dd5Sperseant 
2238c01dce5Sdholland #define D_UNITS (LFS_NINDIR(fs))
2248c01dce5Sdholland #define T_UNITS (LFS_NINDIR(fs)*LFS_NINDIR(fs))
22554fd3dd5Sperseant 
22654fd3dd5Sperseant static daddr_t
lfs_bmap(struct lfs * fs,union lfs_dinode * idinode,daddr_t lbn)227b1828e0bSdholland lfs_bmap(struct lfs *fs, union lfs_dinode *idinode, daddr_t lbn)
22854fd3dd5Sperseant {
2295054440fSlukem 	daddr_t residue, up;
23054fd3dd5Sperseant 	int off=0;
23154fd3dd5Sperseant 	char bp[MAXBSIZE];
23254fd3dd5Sperseant 
233*51508882Smsaitoh 	up = UNASSIGNED;	/* XXXGCC -Wuninitialized [sh3] */
2345054440fSlukem 
235b1828e0bSdholland 	if(lbn > 0 && lbn > lfs_lblkno(fs, lfs_dino_getsize(fs, idinode))) {
23654fd3dd5Sperseant 		return UNASSIGNED;
23754fd3dd5Sperseant 	}
23854fd3dd5Sperseant 	/*
23954fd3dd5Sperseant 	 * Indirect blocks: if it is a first-level indirect, pull its
24054fd3dd5Sperseant 	 * address from the inode; otherwise, call ourselves to find the
24154fd3dd5Sperseant 	 * address of the parent indirect block, and load that to find
24254fd3dd5Sperseant 	 * the desired address.
24354fd3dd5Sperseant 	 */
24454fd3dd5Sperseant 	if(lbn < 0) {
24554fd3dd5Sperseant 		lbn *= -1;
246dc37d54cSdholland 		if (lbn == ULFS_NDADDR) {
24754fd3dd5Sperseant 			/* printf("lbn %d: single indir base\n", -lbn); */
248b1828e0bSdholland 			return lfs_dino_getib(fs, idinode, 0); /* single indirect */
24954fd3dd5Sperseant 		} else if(lbn == BASE_DINDIR+1) {
25054fd3dd5Sperseant 			/* printf("lbn %d: double indir base\n", -lbn); */
251b1828e0bSdholland 			return lfs_dino_getib(fs, idinode, 1); /* double indirect */
25254fd3dd5Sperseant 		} else if(lbn == BASE_TINDIR+2) {
25354fd3dd5Sperseant 			/* printf("lbn %d: triple indir base\n", -lbn); */
254b1828e0bSdholland 			return lfs_dino_getib(fs, idinode, 2); /* triple indirect */
25554fd3dd5Sperseant 		}
25654fd3dd5Sperseant 
25754fd3dd5Sperseant 		/*
25854fd3dd5Sperseant 		 * Find the immediate parent. This is essentially finding the
25954fd3dd5Sperseant 		 * residue of modulus, and then rounding accordingly.
26054fd3dd5Sperseant 		 */
2618c01dce5Sdholland 		residue = (lbn-ULFS_NDADDR) % LFS_NINDIR(fs);
26254fd3dd5Sperseant 		if(residue == 1) {
26354fd3dd5Sperseant 			/* Double indirect.  Parent is the triple. */
264b1828e0bSdholland 			up = lfs_dino_getib(fs, idinode, 2);
2658c01dce5Sdholland 			off = (lbn-2-BASE_TINDIR)/(LFS_NINDIR(fs)*LFS_NINDIR(fs));
26654fd3dd5Sperseant 			if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
26754fd3dd5Sperseant 				return UNASSIGNED;
26854fd3dd5Sperseant 			/* printf("lbn %d: parent is the triple\n", -lbn); */
269f59b8f4bSdholland 			bread(LFS_FSBTODB(sblock, up), bp, lfs_sb_getbsize(sblock));
270cfe9c021Sdholland 			return lfs_iblock_get(fs, bp, off);
27154fd3dd5Sperseant 		} else /* residue == 0 */ {
27254fd3dd5Sperseant 			/* Single indirect.  Two cases. */
27354fd3dd5Sperseant 			if(lbn < BASE_TINDIR) {
27454fd3dd5Sperseant 				/* Parent is the double, simple */
27554fd3dd5Sperseant 				up = -(BASE_DINDIR) - 1;
27654fd3dd5Sperseant 				off = (lbn-BASE_DINDIR) / D_UNITS;
27754fd3dd5Sperseant 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
27854fd3dd5Sperseant 			} else {
27954fd3dd5Sperseant 				/* Ancestor is the triple, more complex */
28054fd3dd5Sperseant 				up = ((lbn-BASE_TINDIR) / T_UNITS)
28154fd3dd5Sperseant 					* T_UNITS + BASE_TINDIR + 1;
28254fd3dd5Sperseant 				off = (lbn/D_UNITS) - (up/D_UNITS);
28354fd3dd5Sperseant 				up = -up;
28454fd3dd5Sperseant 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
28554fd3dd5Sperseant 			}
28654fd3dd5Sperseant 		}
28754fd3dd5Sperseant 	} else {
28854fd3dd5Sperseant 		/* Direct block.  Its parent must be a single indirect. */
289dc37d54cSdholland 		if (lbn < ULFS_NDADDR)
290b1828e0bSdholland 			return lfs_dino_getdb(fs, idinode, lbn);
29154fd3dd5Sperseant 		else {
29254fd3dd5Sperseant 			/* Parent is an indirect block. */
293dc37d54cSdholland 			up = -(((lbn-ULFS_NDADDR) / D_UNITS) * D_UNITS + ULFS_NDADDR);
294dc37d54cSdholland 			off = (lbn-ULFS_NDADDR) % D_UNITS;
29554fd3dd5Sperseant 			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
29654fd3dd5Sperseant 		}
29754fd3dd5Sperseant 	}
29854fd3dd5Sperseant 	up = lfs_bmap(fs,idinode,up);
29954fd3dd5Sperseant 	if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
30054fd3dd5Sperseant 		return UNASSIGNED;
301f59b8f4bSdholland 	bread(LFS_FSBTODB(sblock, up), bp, lfs_sb_getbsize(sblock));
302cfe9c021Sdholland 	return lfs_iblock_get(fs, bp, off);
30354fd3dd5Sperseant }
30454fd3dd5Sperseant 
30532577c4fSdholland static IFILE *
lfs_ientry(ino_t ino)30654fd3dd5Sperseant lfs_ientry(ino_t ino)
30754fd3dd5Sperseant {
30832577c4fSdholland 	static char ifileblock[MAXBSIZE];
30954fd3dd5Sperseant 	static daddr_t ifblkno;
310a3ff3a30Sfvdl 	daddr_t lbn;
31154fd3dd5Sperseant 	daddr_t blkno;
31242614ed3Sfvdl 	union dinode *dp;
313b1828e0bSdholland 	union lfs_dinode *ldp;
31432577c4fSdholland 	unsigned index;
31554fd3dd5Sperseant 
316f59b8f4bSdholland 	lbn = ino/lfs_sb_getifpb(sblock) + lfs_sb_getcleansz(sblock) + lfs_sb_getsegtabsz(sblock);
317eb2560adSdholland 	dp = getino(LFS_IFILE_INUM);
318b1828e0bSdholland 	/* XXX this is foolish */
319b1828e0bSdholland 	if (sblock->lfs_is64) {
320b1828e0bSdholland 		ldp = (union lfs_dinode *)&dp->dlp64;
321b1828e0bSdholland 	} else {
322b1828e0bSdholland 		ldp = (union lfs_dinode *)&dp->dlp32;
323b1828e0bSdholland 	}
324dc37d54cSdholland 	blkno = lfs_bmap(sblock, ldp, lbn);
32554fd3dd5Sperseant 	if (blkno != ifblkno)
32632577c4fSdholland 		bread(LFS_FSBTODB(sblock, blkno), ifileblock,
327f59b8f4bSdholland 		    lfs_sb_getbsize(sblock));
32832577c4fSdholland 	index = ino % lfs_sb_getifpb(sblock);
32932577c4fSdholland 	if (sblock->lfs_is64) {
33032577c4fSdholland 		return (IFILE *) &((IFILE64 *)ifileblock)[index];
33132577c4fSdholland 	} else if (lfs_sb_getversion(sblock) > 1) {
33232577c4fSdholland 		return (IFILE *) &((IFILE32 *)ifileblock)[index];
33332577c4fSdholland 	} else {
33432577c4fSdholland 		return (IFILE *) &((IFILE_V1 *)ifileblock)[index];
33532577c4fSdholland 	}
33654fd3dd5Sperseant }
33754fd3dd5Sperseant 
33854fd3dd5Sperseant /* Search a block for a specific dinode. */
339b1828e0bSdholland static union lfs_dinode *
lfs_ifind(struct lfs * fs,ino_t ino,void * block)340b1828e0bSdholland lfs_ifind(struct lfs *fs, ino_t ino, void *block)
34154fd3dd5Sperseant {
342b1828e0bSdholland 	union lfs_dinode *dip;
343b1828e0bSdholland 	unsigned i, num;
34454fd3dd5Sperseant 
345b1828e0bSdholland 	num = LFS_INOPB(fs);
346b1828e0bSdholland 	for (i = num; i-- > 0; ) {
347b1828e0bSdholland 		dip = DINO_IN_BLOCK(fs, block, i);
348b1828e0bSdholland 		if (lfs_dino_getinumber(fs, dip) == ino)
349b1828e0bSdholland 			return dip;
350b1828e0bSdholland 	}
35154fd3dd5Sperseant 	return NULL;
35254fd3dd5Sperseant }
35354fd3dd5Sperseant 
35442614ed3Sfvdl union dinode *
getino(ino_t inum)35572f1c263Sperry getino(ino_t inum)
35654fd3dd5Sperseant {
35754fd3dd5Sperseant 	static daddr_t inoblkno;
35854fd3dd5Sperseant 	daddr_t blkno;
359b1828e0bSdholland 	static union {
360b1828e0bSdholland 		char space[MAXBSIZE];
361b1828e0bSdholland 		struct lfs64_dinode u_64[MAXBSIZE/sizeof(struct lfs64_dinode)];
362b1828e0bSdholland 		struct lfs32_dinode u_32[MAXBSIZE/sizeof(struct lfs32_dinode)];
363b1828e0bSdholland 	} inoblock;
364ec6fb314Schristos 	static union dinode ifile_dinode; /* XXX fill this in */
365ec6fb314Schristos 	static union dinode empty_dinode; /* Always stays zeroed */
366b1828e0bSdholland 	union lfs_dinode *dp;
367b1828e0bSdholland 	ino_t inum2;
36854fd3dd5Sperseant 
369eb2560adSdholland 	if (inum == LFS_IFILE_INUM) {
37054fd3dd5Sperseant 		/* Load the ifile inode if not already */
371b1828e0bSdholland 		inum2 = sblock->lfs_is64 ?
372b1828e0bSdholland 			ifile_dinode.dlp64.di_inumber :
373b1828e0bSdholland 			ifile_dinode.dlp32.di_inumber;
374b1828e0bSdholland 		if (inum2 == 0) {
375f59b8f4bSdholland 			blkno = lfs_sb_getidaddr(sblock);
376b1828e0bSdholland 			bread(LFS_FSBTODB(sblock, blkno), inoblock.space,
377f59b8f4bSdholland 				(int)lfs_sb_getbsize(sblock));
378b1828e0bSdholland 			dp = lfs_ifind(sblock, inum, inoblock.space);
379b1828e0bSdholland 			/* Structure copy */
380b1828e0bSdholland 			if (sblock->lfs_is64) {
381b1828e0bSdholland 				ifile_dinode.dlp64 = dp->u_64;
382b1828e0bSdholland 			} else {
383b1828e0bSdholland 				ifile_dinode.dlp32 = dp->u_32;
384b1828e0bSdholland 			}
38554fd3dd5Sperseant 		}
386ec6fb314Schristos 		return &ifile_dinode;
38754fd3dd5Sperseant 	}
38854fd3dd5Sperseant 
38954fd3dd5Sperseant 	curino = inum;
39032577c4fSdholland 	blkno = lfs_if_getdaddr(sblock, lfs_ientry(inum));
39154fd3dd5Sperseant 	if(blkno == LFS_UNUSED_DADDR)
392ec6fb314Schristos 		return &empty_dinode;
39354fd3dd5Sperseant 
39454fd3dd5Sperseant 	if(blkno != inoblkno) {
395b1828e0bSdholland 		bread(LFS_FSBTODB(sblock, blkno), inoblock.space,
396f59b8f4bSdholland 			(int)lfs_sb_getbsize(sblock));
39754fd3dd5Sperseant 	}
398b1828e0bSdholland 	return (void *)lfs_ifind(sblock, inum, inoblock.space);
39954fd3dd5Sperseant }
4001c57171fSperseant 
4011c57171fSperseant /*
4021c57171fSperseant  * Tell the filesystem not to overwrite any currently dirty segments
4031c57171fSperseant  * until we are finished.  (It may still write into clean segments, of course,
4041c57171fSperseant  * since we're not using those.)  This is only called when dump_lfs is called
4051c57171fSperseant  * with -X, i.e. we are working on a mounted filesystem.
4061c57171fSperseant  */
4071c57171fSperseant static int root_fd = -1;
4081c57171fSperseant char *wrap_mpname;
4091c57171fSperseant 
4101c57171fSperseant int
lfs_wrap_stop(char * mpname)4111c57171fSperseant lfs_wrap_stop(char *mpname)
4121c57171fSperseant {
4131c57171fSperseant 	int waitfor = 0;
4141c57171fSperseant 
4151c57171fSperseant 	root_fd = open(mpname, O_RDONLY, 0);
4161c57171fSperseant 	if (root_fd < 0)
4171c57171fSperseant 		return -1;
4181c57171fSperseant 	wrap_mpname = mpname;
4191c57171fSperseant 	fcntl(root_fd, LFCNREWIND, -1); /* Ignore return value */
4201c57171fSperseant 	if (fcntl(root_fd, LFCNWRAPSTOP, &waitfor) < 0) {
4211c57171fSperseant 		perror("LFCNWRAPSTOP");
4221c57171fSperseant 		return -1;
4231c57171fSperseant 	}
4241c57171fSperseant 	msg("Disabled log wrap on %s\n", mpname);
4251c57171fSperseant 	return 0;
4261c57171fSperseant }
4271c57171fSperseant 
4281c57171fSperseant /*
4291c57171fSperseant  * Allow the filesystem to continue normal operation.
4301c57171fSperseant  * This would happen anyway when we exit; we do it explicitly here
4311c57171fSperseant  * to show the message, for the user's benefit.
4321c57171fSperseant  */
4331c57171fSperseant void
lfs_wrap_go(void)4341c57171fSperseant lfs_wrap_go(void)
4351c57171fSperseant {
4361c57171fSperseant 	int waitfor = 0;
4371c57171fSperseant 
4381c57171fSperseant 	if (root_fd < 0)
4391c57171fSperseant 		return;
4401c57171fSperseant 
441554662dcSperseant 	fcntl(root_fd, LFCNWRAPGO, &waitfor);
4421c57171fSperseant 	close(root_fd);
4431c57171fSperseant 	root_fd = -1;
4441c57171fSperseant 	msg("Re-enabled log wrap on %s\n", wrap_mpname);
4451c57171fSperseant }
446