lfs_inode.c revision 1.1 1 /* $NetBSD: lfs_inode.c,v 1.1 1999/09/29 04:57:49 perseant 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
44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
45 #else
46 __RCSID("$NetBSD: lfs_inode.c,v 1.1 1999/09/29 04:57:49 perseant 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 #ifdef __STDC__
65 #include <string.h>
66 #include <unistd.h>
67 #endif
68
69 #include "dump.h"
70
71 #define MAXIFPB (MAXBSIZE / sizeof(IFILE))
72
73 #define HASDUMPEDFILE 0x1
74 #define HASSUBDIRS 0x2
75
76 struct lfs *sblock;
77
78 /*
79 * Read the superblock from disk, and check its magic number.
80 * Determine whether byte-swapping needs to be done on this filesystem.
81 */
82 int
83 fs_read_sblock(char *sblock_buf)
84 {
85 int needswap = 0;
86
87 sblock = (struct lfs *)sblock_buf;
88 rawread(LFS_LABELPAD, (char *) sblock, LFS_SBPAD);
89 if (sblock->lfs_magic != LFS_MAGIC) {
90 #if 0
91 if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
92 lfs_sb_swap(sblock, sblock, 0);
93 needswap = 1;
94 } else
95 #endif
96 quit("bad sblock magic number\n");
97 }
98 return needswap;
99 }
100
101 /*
102 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
103 * variables.
104 */
105 struct ufsi *
106 fs_parametrize(void)
107 {
108 static struct ufsi ufsi;
109
110 ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
111 ufsi.ufs_bsize = sblock->lfs_bsize;
112 ufsi.ufs_bshift = sblock->lfs_bshift;
113 ufsi.ufs_fsize = sblock->lfs_fsize;
114 ufsi.ufs_frag = sblock->lfs_frag;
115 ufsi.ufs_fsatoda = 0;
116 ufsi.ufs_nindir = sblock->lfs_nindir;
117 ufsi.ufs_inopb = sblock->lfs_inopb;
118 ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
119 ufsi.ufs_bmask = ~(sblock->lfs_bmask);
120 ufsi.ufs_qbmask = sblock->lfs_bmask;
121 ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
122 ufsi.ufs_qfmask = sblock->lfs_ffmask;
123
124 dev_bsize = sblock->lfs_bsize >> sblock->lfs_fsbtodb;
125
126 return &ufsi;
127 }
128
129 ino_t
130 fs_maxino(void)
131 {
132 return ((getino(sblock->lfs_ifile)->di_size
133 - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
134 * sblock->lfs_bsize)
135 / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
136 }
137
138 /*
139 * XXX KS - I know there's a better way to do this.
140 */
141 #define BASE_SINDIR (NDADDR)
142 #define BASE_DINDIR (NDADDR+NINDIR(fs))
143 #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
144
145 #define D_UNITS (NINDIR(fs))
146 #define T_UNITS (NINDIR(fs)*NINDIR(fs))
147
148 static daddr_t
149 lfs_bmap(struct lfs *fs, struct dinode *idinode, ufs_daddr_t lbn)
150 {
151 ufs_daddr_t residue, up;
152 int off=0;
153 char bp[MAXBSIZE];
154
155 if(lbn > 0 && lbn > (idinode->di_size-1)/dev_bsize) {
156 return UNASSIGNED;
157 }
158 /*
159 * Indirect blocks: if it is a first-level indirect, pull its
160 * address from the inode; otherwise, call ourselves to find the
161 * address of the parent indirect block, and load that to find
162 * the desired address.
163 */
164 if(lbn < 0) {
165 lbn *= -1;
166 if(lbn == NDADDR) {
167 /* printf("lbn %d: single indir base\n", -lbn); */
168 return idinode->di_ib[0]; /* single indirect */
169 } else if(lbn == BASE_DINDIR+1) {
170 /* printf("lbn %d: double indir base\n", -lbn); */
171 return idinode->di_ib[1]; /* double indirect */
172 } else if(lbn == BASE_TINDIR+2) {
173 /* printf("lbn %d: triple indir base\n", -lbn); */
174 return idinode->di_ib[2]; /* triple indirect */
175 }
176
177 /*
178 * Find the immediate parent. This is essentially finding the
179 * residue of modulus, and then rounding accordingly.
180 */
181 residue = (lbn-NDADDR) % NINDIR(fs);
182 if(residue == 1) {
183 /* Double indirect. Parent is the triple. */
184 up = idinode->di_ib[2];
185 off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
186 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
187 return UNASSIGNED;
188 /* printf("lbn %d: parent is the triple\n", -lbn); */
189 bread(up, bp, sblock->lfs_bsize);
190 return ((daddr_t *)bp)[off];
191 } else /* residue == 0 */ {
192 /* Single indirect. Two cases. */
193 if(lbn < BASE_TINDIR) {
194 /* Parent is the double, simple */
195 up = -(BASE_DINDIR) - 1;
196 off = (lbn-BASE_DINDIR) / D_UNITS;
197 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
198 } else {
199 /* Ancestor is the triple, more complex */
200 up = ((lbn-BASE_TINDIR) / T_UNITS)
201 * T_UNITS + BASE_TINDIR + 1;
202 off = (lbn/D_UNITS) - (up/D_UNITS);
203 up = -up;
204 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
205 }
206 }
207 } else {
208 /* Direct block. Its parent must be a single indirect. */
209 if(lbn < NDADDR)
210 return idinode->di_db[lbn];
211 else {
212 /* Parent is an indirect block. */
213 up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
214 off = (lbn-NDADDR) % D_UNITS;
215 /* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
216 }
217 }
218 up = lfs_bmap(fs,idinode,up);
219 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
220 return UNASSIGNED;
221 bread(up, bp, sblock->lfs_bsize);
222 return ((daddr_t *)bp)[off];
223 }
224
225 static struct ifile *
226 lfs_ientry(ino_t ino)
227 {
228 static struct ifile ifileblock[MAXIFPB];
229 static daddr_t ifblkno;
230 ufs_daddr_t lbn;
231 daddr_t blkno;
232
233 lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
234 blkno = lfs_bmap(sblock,getino(sblock->lfs_ifile),lbn);
235 if(blkno != ifblkno)
236 bread(blkno, (char *)ifileblock, sblock->lfs_bsize);
237 return ifileblock + (ino%sblock->lfs_ifpb);
238 }
239
240 /* Search a block for a specific dinode. */
241 struct dinode *
242 lfs_ifind(struct lfs *fs, ino_t ino, struct dinode *dip)
243 {
244 register int cnt;
245
246 for(cnt=0;cnt<INOPB(fs);cnt++)
247 if(dip[cnt].di_inumber == ino)
248 return &(dip[cnt]);
249 return NULL;
250 }
251
252 struct dinode *
253 getino(inum)
254 ino_t inum;
255 {
256 static daddr_t inoblkno;
257 daddr_t blkno;
258 static struct dinode inoblock[MAXINOPB];
259 static struct dinode ifile_dinode; /* XXX fill this in */
260 static struct dinode empty_dinode; /* Always stays zeroed */
261 struct dinode *dp;
262 int i;
263
264 if(inum == sblock->lfs_ifile) {
265 /* Load the ifile inode if not already */
266 if(ifile_dinode.di_u.inumber == 0) {
267 blkno = sblock->lfs_idaddr;
268 bread(blkno, (char *)inoblock, (int)sblock->lfs_bsize);
269 dp = lfs_ifind(sblock, inum, inoblock);
270 ifile_dinode = *dp; /* Structure copy */
271 }
272 return &ifile_dinode;
273 }
274
275 curino = inum;
276 blkno = lfs_ientry(inum)->if_daddr;
277 if(blkno == LFS_UNUSED_DADDR)
278 return &empty_dinode;
279
280 if(blkno != inoblkno) {
281 bread(blkno, (char *)inoblock, (int)sblock->lfs_bsize);
282 if (needswap)
283 for (i = 0; i < MAXINOPB; i++)
284 ffs_dinode_swap(&inoblock[i], &inoblock[i]);
285 }
286 return lfs_ifind(sblock, inum, inoblock);
287 }
288