lfs_inode.c revision 1.15 1 /* $NetBSD: lfs_inode.c,v 1.15 2011/08/14 12:13:24 christos 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\
35 The Regents of the University of California. All rights reserved.");
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.15 2011/08/14 12:13:24 christos 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 <fcntl.h>
59 #include <fts.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "dump.h"
65
66 #define MAXIFPB (MAXBSIZE / sizeof(IFILE))
67
68 #define HASDUMPEDFILE 0x1
69 #define HASSUBDIRS 0x2
70
71 struct lfs *sblock;
72
73 int is_ufs2 = 0;
74
75 /*
76 * Read the superblock from disk, and check its magic number.
77 * Determine whether byte-swapping needs to be done on this filesystem.
78 */
79 int
80 fs_read_sblock(char *superblock)
81 {
82 union {
83 char tbuf[LFS_SBPAD];
84 struct lfs lfss;
85 } u;
86
87 int ns = 0;
88 off_t sboff = LFS_LABELPAD;
89
90 sblock = (struct lfs *)superblock;
91 while(1) {
92 rawread(sboff, (char *) sblock, LFS_SBPAD);
93 if (sblock->lfs_magic != LFS_MAGIC) {
94 #ifdef notyet
95 if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
96 lfs_sb_swap(sblock, sblock, 0);
97 ns = 1;
98 } else
99 #endif
100 quit("bad sblock magic number\n");
101 }
102 if (fsbtob(sblock, (off_t)sblock->lfs_sboffs[0]) != sboff) {
103 sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[0]);
104 continue;
105 }
106 break;
107 }
108
109 /*
110 * Read the secondary and take the older of the two
111 */
112 rawread(fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]), u.tbuf,
113 sizeof(u.tbuf));
114 #ifdef notyet
115 if (ns)
116 lfs_sb_swap(u.tbuf, u.tbuf, 0);
117 #endif
118 if (u.lfss.lfs_magic != LFS_MAGIC) {
119 msg("Warning: secondary superblock at 0x%" PRIx64 " bad magic\n",
120 fsbtodb(sblock, (off_t)sblock->lfs_sboffs[1]));
121 } else {
122 if (sblock->lfs_version > 1) {
123 if (u.lfss.lfs_serial < sblock->lfs_serial) {
124 memcpy(sblock, u.tbuf, sizeof(u.tbuf));
125 sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]);
126 }
127 } else {
128 if (u.lfss.lfs_otstamp < sblock->lfs_otstamp) {
129 memcpy(sblock, u.tbuf, sizeof(u.tbuf));
130 sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]);
131 }
132 }
133 }
134 if (sboff != LFS_SBPAD) {
135 msg("Using superblock at alternate location 0x%lx\n",
136 (unsigned long)(btodb(sboff)));
137 }
138
139 return ns;
140 }
141
142 /*
143 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
144 * variables.
145 */
146 struct ufsi *
147 fs_parametrize(void)
148 {
149 static struct ufsi ufsi;
150
151 spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
152
153 ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
154 if (sblock->lfs_version == 1)
155 ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb;
156 ufsi.ufs_bsize = sblock->lfs_bsize;
157 ufsi.ufs_bshift = sblock->lfs_bshift;
158 ufsi.ufs_fsize = sblock->lfs_fsize;
159 ufsi.ufs_frag = sblock->lfs_frag;
160 ufsi.ufs_fsatoda = sblock->lfs_fsbtodb;
161 if (sblock->lfs_version == 1)
162 ufsi.ufs_fsatoda = 0;
163 ufsi.ufs_nindir = sblock->lfs_nindir;
164 ufsi.ufs_inopb = sblock->lfs_inopb;
165 ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
166 ufsi.ufs_bmask = ~(sblock->lfs_bmask);
167 ufsi.ufs_qbmask = sblock->lfs_bmask;
168 ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
169 ufsi.ufs_qfmask = sblock->lfs_ffmask;
170
171 dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb;
172
173 return &ufsi;
174 }
175
176 ino_t
177 fs_maxino(void)
178 {
179 return ((getino(sblock->lfs_ifile)->dp1.di_size
180 - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
181 * sblock->lfs_bsize)
182 / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
183 }
184
185 void
186 fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped)
187 {
188 ino_t ino;
189
190 for (ino = ROOTINO; ino < maxino; ino++)
191 mapfileino(ino, tapesz, anydirskipped);
192 }
193
194 /*
195 * XXX KS - I know there's a better way to do this.
196 */
197 #define BASE_SINDIR (NDADDR)
198 #define BASE_DINDIR (NDADDR+NINDIR(fs))
199 #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
200
201 #define D_UNITS (NINDIR(fs))
202 #define T_UNITS (NINDIR(fs)*NINDIR(fs))
203
204 static daddr_t
205 lfs_bmap(struct lfs *fs, struct ufs1_dinode *idinode, daddr_t lbn)
206 {
207 daddr_t residue, up;
208 int off=0;
209 char bp[MAXBSIZE];
210
211 up = UNASSIGNED; /* XXXGCC -Wunitialized [sh3] */
212
213 if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) {
214 return UNASSIGNED;
215 }
216 /*
217 * Indirect blocks: if it is a first-level indirect, pull its
218 * address from the inode; otherwise, call ourselves to find the
219 * address of the parent indirect block, and load that to find
220 * the desired address.
221 */
222 if(lbn < 0) {
223 lbn *= -1;
224 if(lbn == NDADDR) {
225 /* printf("lbn %d: single indir base\n", -lbn); */
226 return idinode->di_ib[0]; /* single indirect */
227 } else if(lbn == BASE_DINDIR+1) {
228 /* printf("lbn %d: double indir base\n", -lbn); */
229 return idinode->di_ib[1]; /* double indirect */
230 } else if(lbn == BASE_TINDIR+2) {
231 /* printf("lbn %d: triple indir base\n", -lbn); */
232 return idinode->di_ib[2]; /* triple indirect */
233 }
234
235 /*
236 * Find the immediate parent. This is essentially finding the
237 * residue of modulus, and then rounding accordingly.
238 */
239 residue = (lbn-NDADDR) % NINDIR(fs);
240 if(residue == 1) {
241 /* Double indirect. Parent is the triple. */
242 up = idinode->di_ib[2];
243 off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
244 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
245 return UNASSIGNED;
246 /* printf("lbn %d: parent is the triple\n", -lbn); */
247 bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
248 /* XXX ondisk32 */
249 return (daddr_t)((int32_t *)bp)[off];
250 } else /* residue == 0 */ {
251 /* Single indirect. Two cases. */
252 if(lbn < BASE_TINDIR) {
253 /* Parent is the double, simple */
254 up = -(BASE_DINDIR) - 1;
255 off = (lbn-BASE_DINDIR) / D_UNITS;
256 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
257 } else {
258 /* Ancestor is the triple, more complex */
259 up = ((lbn-BASE_TINDIR) / T_UNITS)
260 * T_UNITS + BASE_TINDIR + 1;
261 off = (lbn/D_UNITS) - (up/D_UNITS);
262 up = -up;
263 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
264 }
265 }
266 } else {
267 /* Direct block. Its parent must be a single indirect. */
268 if(lbn < NDADDR)
269 return idinode->di_db[lbn];
270 else {
271 /* Parent is an indirect block. */
272 up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
273 off = (lbn-NDADDR) % D_UNITS;
274 /* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
275 }
276 }
277 up = lfs_bmap(fs,idinode,up);
278 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
279 return UNASSIGNED;
280 bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
281 /* XXX ondisk32 */
282 return (daddr_t)((int32_t *)bp)[off];
283 }
284
285 static struct ifile *
286 lfs_ientry(ino_t ino)
287 {
288 static struct ifile ifileblock[MAXIFPB];
289 static daddr_t ifblkno;
290 daddr_t lbn;
291 daddr_t blkno;
292 union dinode *dp;
293
294 lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
295 dp = getino(sblock->lfs_ifile);
296 blkno = lfs_bmap(sblock, &dp->dp1 ,lbn);
297 if (blkno != ifblkno)
298 bread(fsbtodb(sblock, blkno), (char *)ifileblock,
299 sblock->lfs_bsize);
300 return ifileblock + (ino % sblock->lfs_ifpb);
301 }
302
303 /* Search a block for a specific dinode. */
304 static struct ufs1_dinode *
305 lfs_ifind(struct lfs *fs, ino_t ino, struct ufs1_dinode *dip)
306 {
307 int cnt;
308
309 for(cnt=0;cnt<INOPB(fs);cnt++)
310 if(dip[cnt].di_inumber == ino)
311 return &(dip[cnt]);
312 return NULL;
313 }
314
315 union dinode *
316 getino(ino_t inum)
317 {
318 static daddr_t inoblkno;
319 daddr_t blkno;
320 static struct ufs1_dinode inoblock[MAXBSIZE / sizeof (struct ufs1_dinode)];
321 static struct ufs1_dinode ifile_dinode; /* XXX fill this in */
322 static struct ufs1_dinode empty_dinode; /* Always stays zeroed */
323 struct ufs1_dinode *dp;
324
325 if(inum == sblock->lfs_ifile) {
326 /* Load the ifile inode if not already */
327 if(ifile_dinode.di_u.inumber == 0) {
328 blkno = sblock->lfs_idaddr;
329 bread(fsbtodb(sblock, blkno), (char *)inoblock,
330 (int)sblock->lfs_bsize);
331 dp = lfs_ifind(sblock, inum, inoblock);
332 ifile_dinode = *dp; /* Structure copy */
333 }
334 return (union dinode *)&ifile_dinode;
335 }
336
337 curino = inum;
338 blkno = lfs_ientry(inum)->if_daddr;
339 if(blkno == LFS_UNUSED_DADDR)
340 return (union dinode *)&empty_dinode;
341
342 if(blkno != inoblkno) {
343 bread(fsbtodb(sblock, blkno), (char *)inoblock,
344 (int)sblock->lfs_bsize);
345 #ifdef notyet
346 if (needswap)
347 for (i = 0; i < MAXINOPB; i++)
348 ffs_dinode_swap(&inoblock[i], &inoblock[i]);
349 #endif
350 }
351 return (union dinode *)lfs_ifind(sblock, inum, inoblock);
352 }
353
354 /*
355 * Tell the filesystem not to overwrite any currently dirty segments
356 * until we are finished. (It may still write into clean segments, of course,
357 * since we're not using those.) This is only called when dump_lfs is called
358 * with -X, i.e. we are working on a mounted filesystem.
359 */
360 static int root_fd = -1;
361 char *wrap_mpname;
362
363 int
364 lfs_wrap_stop(char *mpname)
365 {
366 int waitfor = 0;
367
368 root_fd = open(mpname, O_RDONLY, 0);
369 if (root_fd < 0)
370 return -1;
371 wrap_mpname = mpname;
372 fcntl(root_fd, LFCNREWIND, -1); /* Ignore return value */
373 if (fcntl(root_fd, LFCNWRAPSTOP, &waitfor) < 0) {
374 perror("LFCNWRAPSTOP");
375 return -1;
376 }
377 msg("Disabled log wrap on %s\n", mpname);
378 return 0;
379 }
380
381 /*
382 * Allow the filesystem to continue normal operation.
383 * This would happen anyway when we exit; we do it explicitly here
384 * to show the message, for the user's benefit.
385 */
386 void
387 lfs_wrap_go(void)
388 {
389 int waitfor = 0;
390
391 if (root_fd < 0)
392 return;
393
394 fcntl(root_fd, LFCNWRAPGO, &waitfor);
395 close(root_fd);
396 root_fd = -1;
397 msg("Re-enabled log wrap on %s\n", wrap_mpname);
398 }
399