lfs_inode.c revision 1.5
1/*      $NetBSD: lfs_inode.c,v 1.5 2002/05/25 23:45:13 wiz 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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by the University of
18 *      California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39        The Regents of the University of California.  All rights reserved.\n");
40#endif /* not lint */
41
42#ifndef lint
43#if 0
44static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/1/95";
45#else
46__RCSID("$NetBSD: lfs_inode.c,v 1.5 2002/05/25 23:45:13 wiz Exp $");
47#endif
48#endif /* not lint */
49
50#include <sys/param.h>
51#include <sys/time.h>
52#include <sys/stat.h>
53#include <ufs/ufs/dir.h>
54#include <ufs/ufs/dinode.h>
55#include <sys/mount.h>
56#include <ufs/lfs/lfs.h>
57
58#include <protocols/dumprestore.h>
59
60#include <ctype.h>
61#include <errno.h>
62#include <fts.h>
63#include <stdio.h>
64#include <string.h>
65#include <unistd.h>
66
67#include "dump.h"
68
69#define MAXIFPB        (MAXBSIZE / sizeof(IFILE))
70
71#define	HASDUMPEDFILE	0x1
72#define	HASSUBDIRS	0x2
73
74struct lfs *sblock;
75
76/*
77 * Read the superblock from disk, and check its magic number.
78 * Determine whether byte-swapping needs to be done on this filesystem.
79 */
80int
81fs_read_sblock(char *superblock)
82{
83	char tbuf[LFS_SBPAD];
84	int ns = 0;
85	off_t sboff = LFS_LABELPAD;
86
87	sblock = (struct lfs *)superblock;
88	while(1) {
89		rawread(sboff, (char *) sblock, LFS_SBPAD);
90		if (sblock->lfs_magic != LFS_MAGIC) {
91#ifdef notyet
92			if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
93				lfs_sb_swap(sblock, sblock, 0);
94				ns = 1;
95			} else
96#endif
97				quit("bad sblock magic number\n");
98		}
99		if (fsbtob(sblock, sblock->lfs_sboffs[0]) != sboff) {
100			sboff = fsbtob(sblock, sblock->lfs_sboffs[0]);
101			continue;
102		}
103		break;
104	}
105
106	/*
107	 * Read the secondary and take the older of the two
108	 */
109	rawread(fsbtob(sblock, sblock->lfs_sboffs[1]), tbuf, LFS_SBPAD);
110#ifdef notyet
111	if (ns)
112		lfs_sb_swap(tbuf, tbuf, 0);
113#endif
114	if (((struct lfs *)tbuf)->lfs_magic != LFS_MAGIC) {
115		msg("Warning: secondary superblock at 0x%x bad magic\n",
116			fsbtodb(sblock, sblock->lfs_sboffs[1]));
117	} else {
118		if (sblock->lfs_version > 1) {
119			if (((struct lfs *)tbuf)->lfs_serial < sblock->lfs_serial) {
120				memcpy(sblock, tbuf, LFS_SBPAD);
121				sboff = fsbtob(sblock, sblock->lfs_sboffs[1]);
122			}
123		} else {
124			if (((struct lfs *)tbuf)->lfs_otstamp < sblock->lfs_otstamp) {
125				memcpy(sblock, tbuf, LFS_SBPAD);
126				sboff = fsbtob(sblock, sblock->lfs_sboffs[1]);
127			}
128		}
129	}
130	if (sboff != LFS_SBPAD) {
131		msg("Using superblock at alternate location 0x%lx\n",
132		    (unsigned long)(btodb(sboff)));
133	}
134
135	return ns;
136}
137
138/*
139 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
140 * variables.
141 */
142struct ufsi *
143fs_parametrize(void)
144{
145	static struct ufsi ufsi;
146
147	spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
148
149	ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
150	if (sblock->lfs_version == 1)
151		ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb;
152	ufsi.ufs_bsize = sblock->lfs_bsize;
153	ufsi.ufs_bshift = sblock->lfs_bshift;
154	ufsi.ufs_fsize = sblock->lfs_fsize;
155	ufsi.ufs_frag = sblock->lfs_frag;
156	ufsi.ufs_fsatoda = sblock->lfs_fsbtodb;
157	if (sblock->lfs_version == 1)
158		ufsi.ufs_fsatoda = 0;
159	ufsi.ufs_nindir = sblock->lfs_nindir;
160	ufsi.ufs_inopb = sblock->lfs_inopb;
161	ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
162	ufsi.ufs_bmask = ~(sblock->lfs_bmask);
163	ufsi.ufs_qbmask = sblock->lfs_bmask;
164	ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
165	ufsi.ufs_qfmask = sblock->lfs_ffmask;
166
167	dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb;
168
169	return &ufsi;
170}
171
172ino_t
173fs_maxino(void)
174{
175	return ((getino(sblock->lfs_ifile)->di_size
176		   - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
177		   * sblock->lfs_bsize)
178		  / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
179}
180
181/*
182 * XXX KS - I know there's a better way to do this.
183 */
184#define BASE_SINDIR (NDADDR)
185#define BASE_DINDIR (NDADDR+NINDIR(fs))
186#define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
187
188#define D_UNITS (NINDIR(fs))
189#define T_UNITS (NINDIR(fs)*NINDIR(fs))
190
191static daddr_t
192lfs_bmap(struct lfs *fs, struct dinode *idinode, ufs_daddr_t lbn)
193{
194	ufs_daddr_t residue, up;
195	int off=0;
196	char bp[MAXBSIZE];
197
198	if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) {
199		return UNASSIGNED;
200	}
201	/*
202	 * Indirect blocks: if it is a first-level indirect, pull its
203	 * address from the inode; otherwise, call ourselves to find the
204	 * address of the parent indirect block, and load that to find
205	 * the desired address.
206	 */
207	if(lbn < 0) {
208		lbn *= -1;
209		if(lbn == NDADDR) {
210			/* printf("lbn %d: single indir base\n", -lbn); */
211			return idinode->di_ib[0]; /* single indirect */
212		} else if(lbn == BASE_DINDIR+1) {
213			/* printf("lbn %d: double indir base\n", -lbn); */
214			return idinode->di_ib[1]; /* double indirect */
215		} else if(lbn == BASE_TINDIR+2) {
216			/* printf("lbn %d: triple indir base\n", -lbn); */
217			return idinode->di_ib[2]; /* triple indirect */
218		}
219
220		/*
221		 * Find the immediate parent. This is essentially finding the
222		 * residue of modulus, and then rounding accordingly.
223		 */
224		residue = (lbn-NDADDR) % NINDIR(fs);
225		if(residue == 1) {
226			/* Double indirect.  Parent is the triple. */
227			up = idinode->di_ib[2];
228			off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
229			if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
230				return UNASSIGNED;
231			/* printf("lbn %d: parent is the triple\n", -lbn); */
232			bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
233			return ((daddr_t *)bp)[off];
234		} else /* residue == 0 */ {
235			/* Single indirect.  Two cases. */
236			if(lbn < BASE_TINDIR) {
237				/* Parent is the double, simple */
238				up = -(BASE_DINDIR) - 1;
239				off = (lbn-BASE_DINDIR) / D_UNITS;
240				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
241			} else {
242				/* Ancestor is the triple, more complex */
243				up = ((lbn-BASE_TINDIR) / T_UNITS)
244					* T_UNITS + BASE_TINDIR + 1;
245				off = (lbn/D_UNITS) - (up/D_UNITS);
246				up = -up;
247				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
248			}
249		}
250	} else {
251		/* Direct block.  Its parent must be a single indirect. */
252		if(lbn < NDADDR)
253			return idinode->di_db[lbn];
254		else {
255			/* Parent is an indirect block. */
256			up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
257			off = (lbn-NDADDR) % D_UNITS;
258			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
259		}
260	}
261	up = lfs_bmap(fs,idinode,up);
262	if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
263		return UNASSIGNED;
264	bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
265	return ((daddr_t *)bp)[off];
266}
267
268static struct ifile *
269lfs_ientry(ino_t ino)
270{
271    static struct ifile ifileblock[MAXIFPB];
272    static daddr_t ifblkno;
273    ufs_daddr_t lbn;
274    daddr_t blkno;
275
276    lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
277    blkno = lfs_bmap(sblock,getino(sblock->lfs_ifile),lbn);
278    if(blkno != ifblkno)
279	    bread(fsbtodb(sblock, blkno), (char *)ifileblock,
280		  sblock->lfs_bsize);
281    return ifileblock + (ino%sblock->lfs_ifpb);
282}
283
284/* Search a block for a specific dinode. */
285static struct dinode *
286lfs_ifind(struct lfs *fs, ino_t ino, struct dinode *dip)
287{
288	register int cnt;
289
290	for(cnt=0;cnt<INOPB(fs);cnt++)
291		if(dip[cnt].di_inumber == ino)
292			return &(dip[cnt]);
293	return NULL;
294}
295
296struct dinode *
297getino(inum)
298	ino_t inum;
299{
300	static daddr_t inoblkno;
301	daddr_t blkno;
302	static struct dinode inoblock[MAXINOPB];
303	static struct dinode ifile_dinode; /* XXX fill this in */
304	static struct dinode empty_dinode; /* Always stays zeroed */
305	struct dinode *dp;
306
307	if(inum == sblock->lfs_ifile) {
308		/* Load the ifile inode if not already */
309		if(ifile_dinode.di_u.inumber == 0) {
310			blkno = sblock->lfs_idaddr;
311			bread(fsbtodb(sblock, blkno), (char *)inoblock,
312				(int)sblock->lfs_bsize);
313			dp = lfs_ifind(sblock, inum, inoblock);
314			ifile_dinode = *dp; /* Structure copy */
315		}
316		return &ifile_dinode;
317	}
318
319	curino = inum;
320	blkno = lfs_ientry(inum)->if_daddr;
321	if(blkno == LFS_UNUSED_DADDR)
322		return &empty_dinode;
323
324	if(blkno != inoblkno) {
325		bread(fsbtodb(sblock, blkno), (char *)inoblock,
326			(int)sblock->lfs_bsize);
327#ifdef notyet
328		if (needswap)
329			for (i = 0; i < MAXINOPB; i++)
330				ffs_dinode_swap(&inoblock[i], &inoblock[i]);
331#endif
332	}
333	return lfs_ifind(sblock, inum, inoblock);
334}
335