lfs_inode.c revision 1.15 1 1.15 christos /* $NetBSD: lfs_inode.c,v 1.15 2011/08/14 12:13:24 christos Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 1980, 1991, 1993, 1994
5 1.1 perseant * The Regents of the University of California. All rights reserved.
6 1.1 perseant *
7 1.1 perseant * Redistribution and use in source and binary forms, with or without
8 1.1 perseant * modification, are permitted provided that the following conditions
9 1.1 perseant * are met:
10 1.1 perseant * 1. Redistributions of source code must retain the above copyright
11 1.1 perseant * notice, this list of conditions and the following disclaimer.
12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer in the
14 1.1 perseant * documentation and/or other materials provided with the distribution.
15 1.8 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 perseant * may be used to endorse or promote products derived from this software
17 1.1 perseant * without specific prior written permission.
18 1.1 perseant *
19 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 perseant * SUCH DAMAGE.
30 1.1 perseant */
31 1.1 perseant
32 1.1 perseant #include <sys/cdefs.h>
33 1.1 perseant #ifndef lint
34 1.14 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
35 1.14 lukem The Regents of the University of California. All rights reserved.");
36 1.1 perseant #endif /* not lint */
37 1.1 perseant
38 1.1 perseant #ifndef lint
39 1.1 perseant #if 0
40 1.1 perseant static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
41 1.1 perseant #else
42 1.15 christos __RCSID("$NetBSD: lfs_inode.c,v 1.15 2011/08/14 12:13:24 christos Exp $");
43 1.1 perseant #endif
44 1.1 perseant #endif /* not lint */
45 1.1 perseant
46 1.1 perseant #include <sys/param.h>
47 1.1 perseant #include <sys/time.h>
48 1.1 perseant #include <sys/stat.h>
49 1.1 perseant #include <ufs/ufs/dir.h>
50 1.1 perseant #include <ufs/ufs/dinode.h>
51 1.1 perseant #include <sys/mount.h>
52 1.1 perseant #include <ufs/lfs/lfs.h>
53 1.1 perseant
54 1.1 perseant #include <protocols/dumprestore.h>
55 1.1 perseant
56 1.1 perseant #include <ctype.h>
57 1.1 perseant #include <errno.h>
58 1.12 perseant #include <fcntl.h>
59 1.1 perseant #include <fts.h>
60 1.1 perseant #include <stdio.h>
61 1.1 perseant #include <string.h>
62 1.1 perseant #include <unistd.h>
63 1.1 perseant
64 1.1 perseant #include "dump.h"
65 1.1 perseant
66 1.1 perseant #define MAXIFPB (MAXBSIZE / sizeof(IFILE))
67 1.1 perseant
68 1.1 perseant #define HASDUMPEDFILE 0x1
69 1.1 perseant #define HASSUBDIRS 0x2
70 1.1 perseant
71 1.1 perseant struct lfs *sblock;
72 1.1 perseant
73 1.7 fvdl int is_ufs2 = 0;
74 1.7 fvdl
75 1.1 perseant /*
76 1.1 perseant * Read the superblock from disk, and check its magic number.
77 1.1 perseant * Determine whether byte-swapping needs to be done on this filesystem.
78 1.1 perseant */
79 1.1 perseant int
80 1.4 lukem fs_read_sblock(char *superblock)
81 1.1 perseant {
82 1.15 christos union {
83 1.15 christos char tbuf[LFS_SBPAD];
84 1.15 christos struct lfs lfss;
85 1.15 christos } u;
86 1.15 christos
87 1.4 lukem int ns = 0;
88 1.3 perseant off_t sboff = LFS_LABELPAD;
89 1.1 perseant
90 1.4 lukem sblock = (struct lfs *)superblock;
91 1.3 perseant while(1) {
92 1.3 perseant rawread(sboff, (char *) sblock, LFS_SBPAD);
93 1.3 perseant if (sblock->lfs_magic != LFS_MAGIC) {
94 1.2 perseant #ifdef notyet
95 1.3 perseant if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
96 1.3 perseant lfs_sb_swap(sblock, sblock, 0);
97 1.4 lukem ns = 1;
98 1.3 perseant } else
99 1.1 perseant #endif
100 1.3 perseant quit("bad sblock magic number\n");
101 1.3 perseant }
102 1.12 perseant if (fsbtob(sblock, (off_t)sblock->lfs_sboffs[0]) != sboff) {
103 1.12 perseant sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[0]);
104 1.3 perseant continue;
105 1.3 perseant }
106 1.3 perseant break;
107 1.3 perseant }
108 1.3 perseant
109 1.3 perseant /*
110 1.3 perseant * Read the secondary and take the older of the two
111 1.3 perseant */
112 1.15 christos rawread(fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]), u.tbuf,
113 1.15 christos sizeof(u.tbuf));
114 1.3 perseant #ifdef notyet
115 1.4 lukem if (ns)
116 1.15 christos lfs_sb_swap(u.tbuf, u.tbuf, 0);
117 1.3 perseant #endif
118 1.15 christos if (u.lfss.lfs_magic != LFS_MAGIC) {
119 1.12 perseant msg("Warning: secondary superblock at 0x%" PRIx64 " bad magic\n",
120 1.12 perseant fsbtodb(sblock, (off_t)sblock->lfs_sboffs[1]));
121 1.3 perseant } else {
122 1.3 perseant if (sblock->lfs_version > 1) {
123 1.15 christos if (u.lfss.lfs_serial < sblock->lfs_serial) {
124 1.15 christos memcpy(sblock, u.tbuf, sizeof(u.tbuf));
125 1.12 perseant sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]);
126 1.3 perseant }
127 1.3 perseant } else {
128 1.15 christos if (u.lfss.lfs_otstamp < sblock->lfs_otstamp) {
129 1.15 christos memcpy(sblock, u.tbuf, sizeof(u.tbuf));
130 1.12 perseant sboff = fsbtob(sblock, (off_t)sblock->lfs_sboffs[1]);
131 1.3 perseant }
132 1.3 perseant }
133 1.3 perseant }
134 1.3 perseant if (sboff != LFS_SBPAD) {
135 1.3 perseant msg("Using superblock at alternate location 0x%lx\n",
136 1.3 perseant (unsigned long)(btodb(sboff)));
137 1.1 perseant }
138 1.3 perseant
139 1.4 lukem return ns;
140 1.1 perseant }
141 1.1 perseant
142 1.1 perseant /*
143 1.1 perseant * Fill in the ufsi struct, as well as the maxino and dev_bsize global
144 1.1 perseant * variables.
145 1.1 perseant */
146 1.1 perseant struct ufsi *
147 1.1 perseant fs_parametrize(void)
148 1.1 perseant {
149 1.1 perseant static struct ufsi ufsi;
150 1.1 perseant
151 1.2 perseant spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
152 1.2 perseant
153 1.1 perseant ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
154 1.3 perseant if (sblock->lfs_version == 1)
155 1.3 perseant ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb;
156 1.1 perseant ufsi.ufs_bsize = sblock->lfs_bsize;
157 1.1 perseant ufsi.ufs_bshift = sblock->lfs_bshift;
158 1.1 perseant ufsi.ufs_fsize = sblock->lfs_fsize;
159 1.1 perseant ufsi.ufs_frag = sblock->lfs_frag;
160 1.3 perseant ufsi.ufs_fsatoda = sblock->lfs_fsbtodb;
161 1.3 perseant if (sblock->lfs_version == 1)
162 1.3 perseant ufsi.ufs_fsatoda = 0;
163 1.1 perseant ufsi.ufs_nindir = sblock->lfs_nindir;
164 1.1 perseant ufsi.ufs_inopb = sblock->lfs_inopb;
165 1.1 perseant ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
166 1.1 perseant ufsi.ufs_bmask = ~(sblock->lfs_bmask);
167 1.1 perseant ufsi.ufs_qbmask = sblock->lfs_bmask;
168 1.1 perseant ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
169 1.1 perseant ufsi.ufs_qfmask = sblock->lfs_ffmask;
170 1.1 perseant
171 1.3 perseant dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb;
172 1.1 perseant
173 1.1 perseant return &ufsi;
174 1.1 perseant }
175 1.1 perseant
176 1.1 perseant ino_t
177 1.1 perseant fs_maxino(void)
178 1.1 perseant {
179 1.7 fvdl return ((getino(sblock->lfs_ifile)->dp1.di_size
180 1.1 perseant - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
181 1.1 perseant * sblock->lfs_bsize)
182 1.1 perseant / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
183 1.1 perseant }
184 1.1 perseant
185 1.7 fvdl void
186 1.7 fvdl fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped)
187 1.7 fvdl {
188 1.7 fvdl ino_t ino;
189 1.7 fvdl
190 1.7 fvdl for (ino = ROOTINO; ino < maxino; ino++)
191 1.7 fvdl mapfileino(ino, tapesz, anydirskipped);
192 1.7 fvdl }
193 1.7 fvdl
194 1.1 perseant /*
195 1.1 perseant * XXX KS - I know there's a better way to do this.
196 1.1 perseant */
197 1.1 perseant #define BASE_SINDIR (NDADDR)
198 1.1 perseant #define BASE_DINDIR (NDADDR+NINDIR(fs))
199 1.1 perseant #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
200 1.1 perseant
201 1.1 perseant #define D_UNITS (NINDIR(fs))
202 1.1 perseant #define T_UNITS (NINDIR(fs)*NINDIR(fs))
203 1.1 perseant
204 1.1 perseant static daddr_t
205 1.7 fvdl lfs_bmap(struct lfs *fs, struct ufs1_dinode *idinode, daddr_t lbn)
206 1.1 perseant {
207 1.11 lukem daddr_t residue, up;
208 1.1 perseant int off=0;
209 1.1 perseant char bp[MAXBSIZE];
210 1.11 lukem
211 1.11 lukem up = UNASSIGNED; /* XXXGCC -Wunitialized [sh3] */
212 1.1 perseant
213 1.3 perseant if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) {
214 1.1 perseant return UNASSIGNED;
215 1.1 perseant }
216 1.1 perseant /*
217 1.1 perseant * Indirect blocks: if it is a first-level indirect, pull its
218 1.1 perseant * address from the inode; otherwise, call ourselves to find the
219 1.1 perseant * address of the parent indirect block, and load that to find
220 1.1 perseant * the desired address.
221 1.1 perseant */
222 1.1 perseant if(lbn < 0) {
223 1.1 perseant lbn *= -1;
224 1.1 perseant if(lbn == NDADDR) {
225 1.1 perseant /* printf("lbn %d: single indir base\n", -lbn); */
226 1.1 perseant return idinode->di_ib[0]; /* single indirect */
227 1.1 perseant } else if(lbn == BASE_DINDIR+1) {
228 1.1 perseant /* printf("lbn %d: double indir base\n", -lbn); */
229 1.1 perseant return idinode->di_ib[1]; /* double indirect */
230 1.1 perseant } else if(lbn == BASE_TINDIR+2) {
231 1.1 perseant /* printf("lbn %d: triple indir base\n", -lbn); */
232 1.1 perseant return idinode->di_ib[2]; /* triple indirect */
233 1.1 perseant }
234 1.1 perseant
235 1.1 perseant /*
236 1.1 perseant * Find the immediate parent. This is essentially finding the
237 1.1 perseant * residue of modulus, and then rounding accordingly.
238 1.1 perseant */
239 1.1 perseant residue = (lbn-NDADDR) % NINDIR(fs);
240 1.1 perseant if(residue == 1) {
241 1.1 perseant /* Double indirect. Parent is the triple. */
242 1.1 perseant up = idinode->di_ib[2];
243 1.1 perseant off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
244 1.1 perseant if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
245 1.1 perseant return UNASSIGNED;
246 1.1 perseant /* printf("lbn %d: parent is the triple\n", -lbn); */
247 1.3 perseant bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
248 1.6 fvdl /* XXX ondisk32 */
249 1.6 fvdl return (daddr_t)((int32_t *)bp)[off];
250 1.1 perseant } else /* residue == 0 */ {
251 1.1 perseant /* Single indirect. Two cases. */
252 1.1 perseant if(lbn < BASE_TINDIR) {
253 1.1 perseant /* Parent is the double, simple */
254 1.1 perseant up = -(BASE_DINDIR) - 1;
255 1.1 perseant off = (lbn-BASE_DINDIR) / D_UNITS;
256 1.1 perseant /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
257 1.1 perseant } else {
258 1.1 perseant /* Ancestor is the triple, more complex */
259 1.1 perseant up = ((lbn-BASE_TINDIR) / T_UNITS)
260 1.1 perseant * T_UNITS + BASE_TINDIR + 1;
261 1.1 perseant off = (lbn/D_UNITS) - (up/D_UNITS);
262 1.1 perseant up = -up;
263 1.1 perseant /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
264 1.1 perseant }
265 1.1 perseant }
266 1.1 perseant } else {
267 1.1 perseant /* Direct block. Its parent must be a single indirect. */
268 1.1 perseant if(lbn < NDADDR)
269 1.1 perseant return idinode->di_db[lbn];
270 1.1 perseant else {
271 1.1 perseant /* Parent is an indirect block. */
272 1.1 perseant up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
273 1.1 perseant off = (lbn-NDADDR) % D_UNITS;
274 1.1 perseant /* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
275 1.1 perseant }
276 1.1 perseant }
277 1.1 perseant up = lfs_bmap(fs,idinode,up);
278 1.1 perseant if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
279 1.1 perseant return UNASSIGNED;
280 1.3 perseant bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
281 1.6 fvdl /* XXX ondisk32 */
282 1.6 fvdl return (daddr_t)((int32_t *)bp)[off];
283 1.1 perseant }
284 1.1 perseant
285 1.1 perseant static struct ifile *
286 1.1 perseant lfs_ientry(ino_t ino)
287 1.1 perseant {
288 1.7 fvdl static struct ifile ifileblock[MAXIFPB];
289 1.7 fvdl static daddr_t ifblkno;
290 1.7 fvdl daddr_t lbn;
291 1.7 fvdl daddr_t blkno;
292 1.7 fvdl union dinode *dp;
293 1.1 perseant
294 1.7 fvdl lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
295 1.7 fvdl dp = getino(sblock->lfs_ifile);
296 1.7 fvdl blkno = lfs_bmap(sblock, &dp->dp1 ,lbn);
297 1.7 fvdl if (blkno != ifblkno)
298 1.7 fvdl bread(fsbtodb(sblock, blkno), (char *)ifileblock,
299 1.7 fvdl sblock->lfs_bsize);
300 1.7 fvdl return ifileblock + (ino % sblock->lfs_ifpb);
301 1.1 perseant }
302 1.1 perseant
303 1.1 perseant /* Search a block for a specific dinode. */
304 1.7 fvdl static struct ufs1_dinode *
305 1.7 fvdl lfs_ifind(struct lfs *fs, ino_t ino, struct ufs1_dinode *dip)
306 1.1 perseant {
307 1.9 perry int cnt;
308 1.1 perseant
309 1.1 perseant for(cnt=0;cnt<INOPB(fs);cnt++)
310 1.1 perseant if(dip[cnt].di_inumber == ino)
311 1.1 perseant return &(dip[cnt]);
312 1.1 perseant return NULL;
313 1.1 perseant }
314 1.1 perseant
315 1.7 fvdl union dinode *
316 1.9 perry getino(ino_t inum)
317 1.1 perseant {
318 1.1 perseant static daddr_t inoblkno;
319 1.1 perseant daddr_t blkno;
320 1.7 fvdl static struct ufs1_dinode inoblock[MAXBSIZE / sizeof (struct ufs1_dinode)];
321 1.7 fvdl static struct ufs1_dinode ifile_dinode; /* XXX fill this in */
322 1.7 fvdl static struct ufs1_dinode empty_dinode; /* Always stays zeroed */
323 1.7 fvdl struct ufs1_dinode *dp;
324 1.1 perseant
325 1.1 perseant if(inum == sblock->lfs_ifile) {
326 1.1 perseant /* Load the ifile inode if not already */
327 1.1 perseant if(ifile_dinode.di_u.inumber == 0) {
328 1.1 perseant blkno = sblock->lfs_idaddr;
329 1.3 perseant bread(fsbtodb(sblock, blkno), (char *)inoblock,
330 1.3 perseant (int)sblock->lfs_bsize);
331 1.1 perseant dp = lfs_ifind(sblock, inum, inoblock);
332 1.1 perseant ifile_dinode = *dp; /* Structure copy */
333 1.1 perseant }
334 1.7 fvdl return (union dinode *)&ifile_dinode;
335 1.1 perseant }
336 1.1 perseant
337 1.1 perseant curino = inum;
338 1.1 perseant blkno = lfs_ientry(inum)->if_daddr;
339 1.1 perseant if(blkno == LFS_UNUSED_DADDR)
340 1.7 fvdl return (union dinode *)&empty_dinode;
341 1.1 perseant
342 1.1 perseant if(blkno != inoblkno) {
343 1.3 perseant bread(fsbtodb(sblock, blkno), (char *)inoblock,
344 1.3 perseant (int)sblock->lfs_bsize);
345 1.2 perseant #ifdef notyet
346 1.1 perseant if (needswap)
347 1.1 perseant for (i = 0; i < MAXINOPB; i++)
348 1.1 perseant ffs_dinode_swap(&inoblock[i], &inoblock[i]);
349 1.2 perseant #endif
350 1.1 perseant }
351 1.7 fvdl return (union dinode *)lfs_ifind(sblock, inum, inoblock);
352 1.1 perseant }
353 1.12 perseant
354 1.12 perseant /*
355 1.12 perseant * Tell the filesystem not to overwrite any currently dirty segments
356 1.12 perseant * until we are finished. (It may still write into clean segments, of course,
357 1.12 perseant * since we're not using those.) This is only called when dump_lfs is called
358 1.12 perseant * with -X, i.e. we are working on a mounted filesystem.
359 1.12 perseant */
360 1.12 perseant static int root_fd = -1;
361 1.12 perseant char *wrap_mpname;
362 1.12 perseant
363 1.12 perseant int
364 1.12 perseant lfs_wrap_stop(char *mpname)
365 1.12 perseant {
366 1.12 perseant int waitfor = 0;
367 1.12 perseant
368 1.12 perseant root_fd = open(mpname, O_RDONLY, 0);
369 1.12 perseant if (root_fd < 0)
370 1.12 perseant return -1;
371 1.12 perseant wrap_mpname = mpname;
372 1.12 perseant fcntl(root_fd, LFCNREWIND, -1); /* Ignore return value */
373 1.12 perseant if (fcntl(root_fd, LFCNWRAPSTOP, &waitfor) < 0) {
374 1.12 perseant perror("LFCNWRAPSTOP");
375 1.12 perseant return -1;
376 1.12 perseant }
377 1.12 perseant msg("Disabled log wrap on %s\n", mpname);
378 1.12 perseant return 0;
379 1.12 perseant }
380 1.12 perseant
381 1.12 perseant /*
382 1.12 perseant * Allow the filesystem to continue normal operation.
383 1.12 perseant * This would happen anyway when we exit; we do it explicitly here
384 1.12 perseant * to show the message, for the user's benefit.
385 1.12 perseant */
386 1.12 perseant void
387 1.12 perseant lfs_wrap_go(void)
388 1.12 perseant {
389 1.12 perseant int waitfor = 0;
390 1.12 perseant
391 1.12 perseant if (root_fd < 0)
392 1.12 perseant return;
393 1.12 perseant
394 1.13 perseant fcntl(root_fd, LFCNWRAPGO, &waitfor);
395 1.12 perseant close(root_fd);
396 1.12 perseant root_fd = -1;
397 1.12 perseant msg("Re-enabled log wrap on %s\n", wrap_mpname);
398 1.12 perseant }
399