xref: /netbsd-src/sbin/dump_lfs/lfs_inode.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*      $NetBSD: lfs_inode.c,v 1.8 2003/08/07 10:04:15 agc Exp $ */
2 
3 /*-
4  * Copyright (c) 1980, 1991, 1993, 1994
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/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
35         The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/1/95";
41 #else
42 __RCSID("$NetBSD: lfs_inode.c,v 1.8 2003/08/07 10:04:15 agc Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/stat.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ufs/dinode.h>
51 #include <sys/mount.h>
52 #include <ufs/lfs/lfs.h>
53 
54 #include <protocols/dumprestore.h>
55 
56 #include <ctype.h>
57 #include <errno.h>
58 #include <fts.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include "dump.h"
64 
65 #define MAXIFPB        (MAXBSIZE / sizeof(IFILE))
66 
67 #define	HASDUMPEDFILE	0x1
68 #define	HASSUBDIRS	0x2
69 
70 struct lfs *sblock;
71 
72 int is_ufs2 = 0;
73 
74 /*
75  * Read the superblock from disk, and check its magic number.
76  * Determine whether byte-swapping needs to be done on this filesystem.
77  */
78 int
79 fs_read_sblock(char *superblock)
80 {
81 	char tbuf[LFS_SBPAD];
82 	int ns = 0;
83 	off_t sboff = LFS_LABELPAD;
84 
85 	sblock = (struct lfs *)superblock;
86 	while(1) {
87 		rawread(sboff, (char *) sblock, LFS_SBPAD);
88 		if (sblock->lfs_magic != LFS_MAGIC) {
89 #ifdef notyet
90 			if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
91 				lfs_sb_swap(sblock, sblock, 0);
92 				ns = 1;
93 			} else
94 #endif
95 				quit("bad sblock magic number\n");
96 		}
97 		if (fsbtob(sblock, sblock->lfs_sboffs[0]) != sboff) {
98 			sboff = fsbtob(sblock, sblock->lfs_sboffs[0]);
99 			continue;
100 		}
101 		break;
102 	}
103 
104 	/*
105 	 * Read the secondary and take the older of the two
106 	 */
107 	rawread(fsbtob(sblock, sblock->lfs_sboffs[1]), tbuf, LFS_SBPAD);
108 #ifdef notyet
109 	if (ns)
110 		lfs_sb_swap(tbuf, tbuf, 0);
111 #endif
112 	if (((struct lfs *)tbuf)->lfs_magic != LFS_MAGIC) {
113 		msg("Warning: secondary superblock at 0x%x bad magic\n",
114 			fsbtodb(sblock, sblock->lfs_sboffs[1]));
115 	} else {
116 		if (sblock->lfs_version > 1) {
117 			if (((struct lfs *)tbuf)->lfs_serial < sblock->lfs_serial) {
118 				memcpy(sblock, tbuf, LFS_SBPAD);
119 				sboff = fsbtob(sblock, sblock->lfs_sboffs[1]);
120 			}
121 		} else {
122 			if (((struct lfs *)tbuf)->lfs_otstamp < sblock->lfs_otstamp) {
123 				memcpy(sblock, tbuf, LFS_SBPAD);
124 				sboff = fsbtob(sblock, sblock->lfs_sboffs[1]);
125 			}
126 		}
127 	}
128 	if (sboff != LFS_SBPAD) {
129 		msg("Using superblock at alternate location 0x%lx\n",
130 		    (unsigned long)(btodb(sboff)));
131 	}
132 
133 	return ns;
134 }
135 
136 /*
137  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
138  * variables.
139  */
140 struct ufsi *
141 fs_parametrize(void)
142 {
143 	static struct ufsi ufsi;
144 
145 	spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
146 
147 	ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
148 	if (sblock->lfs_version == 1)
149 		ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb;
150 	ufsi.ufs_bsize = sblock->lfs_bsize;
151 	ufsi.ufs_bshift = sblock->lfs_bshift;
152 	ufsi.ufs_fsize = sblock->lfs_fsize;
153 	ufsi.ufs_frag = sblock->lfs_frag;
154 	ufsi.ufs_fsatoda = sblock->lfs_fsbtodb;
155 	if (sblock->lfs_version == 1)
156 		ufsi.ufs_fsatoda = 0;
157 	ufsi.ufs_nindir = sblock->lfs_nindir;
158 	ufsi.ufs_inopb = sblock->lfs_inopb;
159 	ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
160 	ufsi.ufs_bmask = ~(sblock->lfs_bmask);
161 	ufsi.ufs_qbmask = sblock->lfs_bmask;
162 	ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
163 	ufsi.ufs_qfmask = sblock->lfs_ffmask;
164 
165 	dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb;
166 
167 	return &ufsi;
168 }
169 
170 ino_t
171 fs_maxino(void)
172 {
173 	return ((getino(sblock->lfs_ifile)->dp1.di_size
174 		   - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
175 		   * sblock->lfs_bsize)
176 		  / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
177 }
178 
179 void
180 fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped)
181 {
182 	ino_t ino;
183 
184 	for (ino = ROOTINO; ino < maxino; ino++)
185 		mapfileino(ino, tapesz, anydirskipped);
186 }
187 
188 /*
189  * XXX KS - I know there's a better way to do this.
190  */
191 #define BASE_SINDIR (NDADDR)
192 #define BASE_DINDIR (NDADDR+NINDIR(fs))
193 #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
194 
195 #define D_UNITS (NINDIR(fs))
196 #define T_UNITS (NINDIR(fs)*NINDIR(fs))
197 
198 static daddr_t
199 lfs_bmap(struct lfs *fs, struct ufs1_dinode *idinode, daddr_t lbn)
200 {
201 	daddr_t residue, up;
202 	int off=0;
203 	char bp[MAXBSIZE];
204 
205 	if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) {
206 		return UNASSIGNED;
207 	}
208 	/*
209 	 * Indirect blocks: if it is a first-level indirect, pull its
210 	 * address from the inode; otherwise, call ourselves to find the
211 	 * address of the parent indirect block, and load that to find
212 	 * the desired address.
213 	 */
214 	if(lbn < 0) {
215 		lbn *= -1;
216 		if(lbn == NDADDR) {
217 			/* printf("lbn %d: single indir base\n", -lbn); */
218 			return idinode->di_ib[0]; /* single indirect */
219 		} else if(lbn == BASE_DINDIR+1) {
220 			/* printf("lbn %d: double indir base\n", -lbn); */
221 			return idinode->di_ib[1]; /* double indirect */
222 		} else if(lbn == BASE_TINDIR+2) {
223 			/* printf("lbn %d: triple indir base\n", -lbn); */
224 			return idinode->di_ib[2]; /* triple indirect */
225 		}
226 
227 		/*
228 		 * Find the immediate parent. This is essentially finding the
229 		 * residue of modulus, and then rounding accordingly.
230 		 */
231 		residue = (lbn-NDADDR) % NINDIR(fs);
232 		if(residue == 1) {
233 			/* Double indirect.  Parent is the triple. */
234 			up = idinode->di_ib[2];
235 			off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
236 			if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
237 				return UNASSIGNED;
238 			/* printf("lbn %d: parent is the triple\n", -lbn); */
239 			bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
240 			/* XXX ondisk32 */
241 			return (daddr_t)((int32_t *)bp)[off];
242 		} else /* residue == 0 */ {
243 			/* Single indirect.  Two cases. */
244 			if(lbn < BASE_TINDIR) {
245 				/* Parent is the double, simple */
246 				up = -(BASE_DINDIR) - 1;
247 				off = (lbn-BASE_DINDIR) / D_UNITS;
248 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
249 			} else {
250 				/* Ancestor is the triple, more complex */
251 				up = ((lbn-BASE_TINDIR) / T_UNITS)
252 					* T_UNITS + BASE_TINDIR + 1;
253 				off = (lbn/D_UNITS) - (up/D_UNITS);
254 				up = -up;
255 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
256 			}
257 		}
258 	} else {
259 		/* Direct block.  Its parent must be a single indirect. */
260 		if(lbn < NDADDR)
261 			return idinode->di_db[lbn];
262 		else {
263 			/* Parent is an indirect block. */
264 			up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
265 			off = (lbn-NDADDR) % D_UNITS;
266 			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
267 		}
268 	}
269 	up = lfs_bmap(fs,idinode,up);
270 	if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
271 		return UNASSIGNED;
272 	bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
273 	/* XXX ondisk32 */
274 	return (daddr_t)((int32_t *)bp)[off];
275 }
276 
277 static struct ifile *
278 lfs_ientry(ino_t ino)
279 {
280 	static struct ifile ifileblock[MAXIFPB];
281 	static daddr_t ifblkno;
282 	daddr_t lbn;
283 	daddr_t blkno;
284 	union dinode *dp;
285 
286 	lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
287 	dp = getino(sblock->lfs_ifile);
288 	blkno = lfs_bmap(sblock, &dp->dp1 ,lbn);
289 	if (blkno != ifblkno)
290 		bread(fsbtodb(sblock, blkno), (char *)ifileblock,
291 		    sblock->lfs_bsize);
292 	return ifileblock + (ino % sblock->lfs_ifpb);
293 }
294 
295 /* Search a block for a specific dinode. */
296 static struct ufs1_dinode *
297 lfs_ifind(struct lfs *fs, ino_t ino, struct ufs1_dinode *dip)
298 {
299 	register int cnt;
300 
301 	for(cnt=0;cnt<INOPB(fs);cnt++)
302 		if(dip[cnt].di_inumber == ino)
303 			return &(dip[cnt]);
304 	return NULL;
305 }
306 
307 union dinode *
308 getino(inum)
309 	ino_t inum;
310 {
311 	static daddr_t inoblkno;
312 	daddr_t blkno;
313 	static struct ufs1_dinode inoblock[MAXBSIZE / sizeof (struct ufs1_dinode)];
314 	static struct ufs1_dinode ifile_dinode; /* XXX fill this in */
315 	static struct ufs1_dinode empty_dinode; /* Always stays zeroed */
316 	struct ufs1_dinode *dp;
317 
318 	if(inum == sblock->lfs_ifile) {
319 		/* Load the ifile inode if not already */
320 		if(ifile_dinode.di_u.inumber == 0) {
321 			blkno = sblock->lfs_idaddr;
322 			bread(fsbtodb(sblock, blkno), (char *)inoblock,
323 				(int)sblock->lfs_bsize);
324 			dp = lfs_ifind(sblock, inum, inoblock);
325 			ifile_dinode = *dp; /* Structure copy */
326 		}
327 		return (union dinode *)&ifile_dinode;
328 	}
329 
330 	curino = inum;
331 	blkno = lfs_ientry(inum)->if_daddr;
332 	if(blkno == LFS_UNUSED_DADDR)
333 		return (union dinode *)&empty_dinode;
334 
335 	if(blkno != inoblkno) {
336 		bread(fsbtodb(sblock, blkno), (char *)inoblock,
337 			(int)sblock->lfs_bsize);
338 #ifdef notyet
339 		if (needswap)
340 			for (i = 0; i < MAXINOPB; i++)
341 				ffs_dinode_swap(&inoblock[i], &inoblock[i]);
342 #endif
343 	}
344 	return (union dinode *)lfs_ifind(sblock, inum, inoblock);
345 }
346