inode.c revision 1.22 1 /* $NetBSD: inode.c,v 1.22 2003/10/03 12:22:15 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
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 /*
33 * Copyright (c) 1997, 1998
34 * Konrad Schroder. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/types.h>
66 #include <sys/param.h>
67 #include <sys/time.h>
68 #include <sys/buf.h>
69 #include <sys/mount.h>
70
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/dir.h>
73 #define vnode uvnode
74 #include <ufs/lfs/lfs.h>
75 #undef vnode
76
77 #include <err.h>
78 #ifndef SMALL
79 #include <pwd.h>
80 #endif
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84
85 #include "bufcache.h"
86 #include "vnode.h"
87 #include "lfs.h"
88
89 #include "fsck.h"
90 #include "fsutil.h"
91 #include "extern.h"
92
93 extern SEGUSE *seg_table;
94 extern ufs_daddr_t *din_table;
95
96 static int iblock(struct inodesc *, long, u_int64_t);
97 int blksreqd(struct lfs *, int);
98 int lfs_maxino(void);
99
100 /*
101 * Get a dinode of a given inum.
102 * XXX combine this function with vget.
103 */
104 struct ufs1_dinode *
105 ginode(ino_t ino)
106 {
107 struct uvnode *vp;
108 struct ubuf *bp;
109 IFILE *ifp;
110
111 vp = vget(fs, ino);
112 if (vp == NULL)
113 return NULL;
114
115 if (din_table[ino] == 0x0) {
116 LFS_IENTRY(ifp, fs, ino, bp);
117 din_table[ino] = ifp->if_daddr;
118 seg_table[dtosn(fs, ifp->if_daddr)].su_nbytes += DINODE1_SIZE;
119 brelse(bp);
120 }
121 return (VTOI(vp)->i_din.ffs1_din);
122 }
123
124 /*
125 * Check validity of held blocks in an inode, recursing through all blocks.
126 */
127 int
128 ckinode(struct ufs1_dinode *dp, struct inodesc *idesc)
129 {
130 ufs_daddr_t *ap, lbn;
131 long ret, n, ndb, offset;
132 struct ufs1_dinode dino;
133 u_int64_t remsize, sizepb;
134 mode_t mode;
135 char pathbuf[MAXPATHLEN + 1];
136 struct uvnode *vp, *thisvp;
137
138 if (idesc->id_fix != IGNORE)
139 idesc->id_fix = DONTKNOW;
140 idesc->id_entryno = 0;
141 idesc->id_filesize = dp->di_size;
142 mode = dp->di_mode & IFMT;
143 if (mode == IFBLK || mode == IFCHR ||
144 (mode == IFLNK && (dp->di_size < fs->lfs_maxsymlinklen ||
145 (fs->lfs_maxsymlinklen == 0 &&
146 dp->di_blocks == 0))))
147 return (KEEPON);
148 dino = *dp;
149 ndb = howmany(dino.di_size, fs->lfs_bsize);
150
151 thisvp = vget(fs, idesc->id_number);
152 for (lbn = 0; lbn < NDADDR; lbn++) {
153 ap = dino.di_db + lbn;
154 if (thisvp)
155 idesc->id_numfrags =
156 numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
157 else {
158 if (--ndb == 0 && (offset = blkoff(fs, dino.di_size)) != 0) {
159 idesc->id_numfrags =
160 numfrags(fs, fragroundup(fs, offset));
161 } else
162 idesc->id_numfrags = fs->lfs_frag;
163 }
164 if (*ap == 0) {
165 if (idesc->id_type == DATA && ndb >= 0) {
166 /* An empty block in a directory XXX */
167 getpathname(pathbuf, sizeof(pathbuf),
168 idesc->id_number, idesc->id_number);
169 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
170 pathbuf);
171 if (reply("ADJUST LENGTH") == 1) {
172 vp = vget(fs, idesc->id_number);
173 dp = VTOD(vp);
174 dp->di_size = (ap - &dino.di_db[0]) *
175 fs->lfs_bsize;
176 printf(
177 "YOU MUST RERUN FSCK AFTERWARDS\n");
178 rerun = 1;
179 inodirty(VTOI(vp));
180 }
181 }
182 continue;
183 }
184 idesc->id_blkno = *ap;
185 idesc->id_lblkno = ap - &dino.di_db[0];
186 if (idesc->id_type == ADDR) {
187 ret = (*idesc->id_func) (idesc);
188 } else
189 ret = dirscan(idesc);
190 if (ret & STOP)
191 return (ret);
192 }
193 idesc->id_numfrags = fs->lfs_frag;
194 remsize = dino.di_size - fs->lfs_bsize * NDADDR;
195 sizepb = fs->lfs_bsize;
196 for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
197 if (*ap) {
198 idesc->id_blkno = *ap;
199 ret = iblock(idesc, n, remsize);
200 if (ret & STOP)
201 return (ret);
202 } else {
203 if (idesc->id_type == DATA && remsize > 0) {
204 /* An empty block in a directory XXX */
205 getpathname(pathbuf, sizeof(pathbuf),
206 idesc->id_number, idesc->id_number);
207 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
208 pathbuf);
209 if (reply("ADJUST LENGTH") == 1) {
210 vp = vget(fs, idesc->id_number);
211 dp = VTOD(vp);
212 dp->di_size -= remsize;
213 remsize = 0;
214 printf(
215 "YOU MUST RERUN FSCK AFTERWARDS\n");
216 rerun = 1;
217 inodirty(VTOI(vp));
218 break;
219 }
220 }
221 }
222 sizepb *= NINDIR(fs);
223 remsize -= sizepb;
224 }
225 return (KEEPON);
226 }
227
228 static int
229 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
230 {
231 ufs_daddr_t *ap, *aplim;
232 struct ubuf *bp;
233 int i, n, (*func) (struct inodesc *), nif;
234 u_int64_t sizepb;
235 char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
236 struct uvnode *devvp, *vp;
237 int diddirty = 0;
238
239 if (idesc->id_type == ADDR) {
240 func = idesc->id_func;
241 n = (*func) (idesc);
242 if ((n & KEEPON) == 0)
243 return (n);
244 } else
245 func = dirscan;
246 if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags)))
247 return (SKIP);
248
249 devvp = fs->lfs_unlockvp;
250 bread(devvp, fsbtodb(fs, idesc->id_blkno), fs->lfs_bsize, NOCRED, &bp);
251 ilevel--;
252 for (sizepb = fs->lfs_bsize, i = 0; i < ilevel; i++)
253 sizepb *= NINDIR(fs);
254 if (isize > sizepb * NINDIR(fs))
255 nif = NINDIR(fs);
256 else
257 nif = howmany(isize, sizepb);
258 if (idesc->id_func == pass1check && nif < NINDIR(fs)) {
259 aplim = ((ufs_daddr_t *) bp->b_data) + NINDIR(fs);
260 for (ap = ((ufs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) {
261 if (*ap == 0)
262 continue;
263 (void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%u",
264 idesc->id_number);
265 if (dofix(idesc, buf)) {
266 *ap = 0;
267 ++diddirty;
268 }
269 }
270 }
271 aplim = ((ufs_daddr_t *) bp->b_data) + nif;
272 for (ap = ((ufs_daddr_t *) bp->b_data); ap < aplim; ap++) {
273 if (*ap) {
274 idesc->id_blkno = *ap;
275 if (ilevel == 0) {
276 /*
277 * dirscan needs lblkno.
278 */
279 idesc->id_lblkno++;
280 n = (*func) (idesc);
281 } else {
282 n = iblock(idesc, ilevel, isize);
283 }
284 if (n & STOP) {
285 if (diddirty)
286 VOP_BWRITE(bp);
287 else
288 brelse(bp);
289 return (n);
290 }
291 } else {
292 if (idesc->id_type == DATA && isize > 0) {
293 /* An empty block in a directory XXX */
294 getpathname(pathbuf, sizeof(pathbuf),
295 idesc->id_number, idesc->id_number);
296 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
297 pathbuf);
298 if (reply("ADJUST LENGTH") == 1) {
299 vp = vget(fs, idesc->id_number);
300 VTOI(vp)->i_ffs1_size -= isize;
301 isize = 0;
302 printf(
303 "YOU MUST RERUN FSCK AFTERWARDS\n");
304 rerun = 1;
305 inodirty(VTOI(vp));
306 if (diddirty)
307 VOP_BWRITE(bp);
308 else
309 brelse(bp);
310 return (STOP);
311 }
312 }
313 }
314 isize -= sizepb;
315 }
316 if (diddirty)
317 VOP_BWRITE(bp);
318 else
319 brelse(bp);
320 return (KEEPON);
321 }
322 /*
323 * Check that a block in a legal block number.
324 * Return 0 if in range, 1 if out of range.
325 */
326 int
327 chkrange(daddr_t blk, int cnt)
328 {
329 if (blk < sntod(fs, 0)) {
330 return (1);
331 }
332 if (blk > maxfsblock) {
333 return (1);
334 }
335 if (blk + cnt < sntod(fs, 0)) {
336 return (1);
337 }
338 if (blk + cnt > maxfsblock) {
339 return (1);
340 }
341 return (0);
342 }
343 /*
344 * Routines to maintain information about directory inodes.
345 * This is built during the first pass and used during the
346 * second and third passes.
347 *
348 * Enter inodes into the cache.
349 */
350 void
351 cacheino(struct ufs1_dinode * dp, ino_t inumber)
352 {
353 struct inoinfo *inp;
354 struct inoinfo **inpp, **ninpsort;
355 unsigned int blks;
356
357 blks = howmany(dp->di_size, fs->lfs_bsize);
358 if (blks > NDADDR)
359 blks = NDADDR + NIADDR;
360 inp = (struct inoinfo *)
361 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
362 if (inp == NULL)
363 return;
364 inpp = &inphead[inumber % numdirs];
365 inp->i_nexthash = *inpp;
366 *inpp = inp;
367 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
368 if (inumber == ROOTINO)
369 inp->i_parent = ROOTINO;
370 else
371 inp->i_parent = (ino_t) 0;
372 inp->i_dotdot = (ino_t) 0;
373 inp->i_number = inumber;
374 inp->i_isize = dp->di_size;
375
376 inp->i_numblks = blks * sizeof(ufs_daddr_t);
377 memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks);
378 if (inplast == listmax) {
379 ninpsort = (struct inoinfo **) realloc((char *) inpsort,
380 (unsigned) (listmax + 100) * sizeof(struct inoinfo *));
381 if (ninpsort == NULL)
382 err(8, "cannot increase directory list\n");
383 inpsort = ninpsort;
384 listmax += 100;
385 }
386 inpsort[inplast++] = inp;
387 }
388
389 /*
390 * Look up an inode cache structure.
391 */
392 struct inoinfo *
393 getinoinfo(ino_t inumber)
394 {
395 register struct inoinfo *inp;
396
397 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
398 if (inp->i_number != inumber)
399 continue;
400 return (inp);
401 }
402 err(8, "cannot find inode %d\n", inumber);
403 return ((struct inoinfo *) 0);
404 }
405
406 /*
407 * Clean up all the inode cache structure.
408 */
409 void
410 inocleanup()
411 {
412 register struct inoinfo **inpp;
413
414 if (inphead == NULL)
415 return;
416 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
417 free((char *) (*inpp));
418 free((char *) inphead);
419 free((char *) inpsort);
420 inphead = inpsort = NULL;
421 }
422
423 void
424 inodirty(struct inode *ip)
425 {
426 ip->i_flag |= IN_MODIFIED;
427 }
428
429 void
430 clri(struct inodesc * idesc, char *type, int flag)
431 {
432 struct ubuf *bp;
433 IFILE *ifp;
434 struct uvnode *vp;
435
436 vp = vget(fs, idesc->id_number);
437 if (flag == 1) {
438 pwarn("%s %s", type,
439 (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
440 pinode(idesc->id_number);
441 }
442 if (flag == 2 || preen || reply("CLEAR") == 1) {
443 if (preen && flag != 2)
444 printf(" (CLEARED)\n");
445 n_files--;
446 (void) ckinode(VTOD(vp), idesc);
447 clearinode(VTOD(vp));
448 statemap[idesc->id_number] = USTATE;
449 inodirty(VTOI(vp));
450
451 /* Send cleared inode to the free list */
452
453 LFS_IENTRY(ifp, fs, idesc->id_number, bp);
454 ifp->if_daddr = LFS_UNUSED_DADDR;
455 ifp->if_nextfree = fs->lfs_freehd;
456 fs->lfs_freehd = idesc->id_number;
457 sbdirty();
458 VOP_BWRITE(bp);
459 }
460 }
461
462 int
463 findname(struct inodesc * idesc)
464 {
465 register struct direct *dirp = idesc->id_dirp;
466
467 if (dirp->d_ino != idesc->id_parent)
468 return (KEEPON);
469 memcpy(idesc->id_name, dirp->d_name, (size_t) dirp->d_namlen + 1);
470 return (STOP | FOUND);
471 }
472
473 int
474 findino(struct inodesc * idesc)
475 {
476 register struct direct *dirp = idesc->id_dirp;
477
478 if (dirp->d_ino == 0)
479 return (KEEPON);
480 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
481 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
482 idesc->id_parent = dirp->d_ino;
483 return (STOP | FOUND);
484 }
485 return (KEEPON);
486 }
487
488 void
489 pinode(ino_t ino)
490 {
491 register struct ufs1_dinode *dp;
492 register char *p;
493 struct passwd *pw;
494 time_t t;
495
496 printf(" I=%u ", ino);
497 if (ino < ROOTINO || ino >= maxino)
498 return;
499 dp = ginode(ino);
500 if (dp) {
501 printf(" OWNER=");
502 #ifndef SMALL
503 if ((pw = getpwuid((int) dp->di_uid)) != 0)
504 printf("%s ", pw->pw_name);
505 else
506 #endif
507 printf("%u ", (unsigned) dp->di_uid);
508 printf("MODE=%o\n", dp->di_mode);
509 if (preen)
510 printf("%s: ", cdevname());
511 printf("SIZE=%llu ", (unsigned long long) dp->di_size);
512 t = dp->di_mtime;
513 p = ctime(&t);
514 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
515 }
516 }
517
518 void
519 blkerror(ino_t ino, char *type, daddr_t blk)
520 {
521
522 pfatal("%lld %s I=%u", (long long) blk, type, ino);
523 printf("\n");
524 if (exitonfail)
525 exit(1);
526 switch (statemap[ino]) {
527
528 case FSTATE:
529 statemap[ino] = FCLEAR;
530 return;
531
532 case DSTATE:
533 statemap[ino] = DCLEAR;
534 return;
535
536 case FCLEAR:
537 case DCLEAR:
538 return;
539
540 default:
541 err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]);
542 /* NOTREACHED */
543 }
544 }
545 /*
546 * allocate an unused inode
547 */
548 ino_t
549 allocino(ino_t request, int type)
550 {
551 ino_t ino;
552 struct ufs1_dinode *dp;
553 time_t t;
554 struct uvnode *vp;
555 struct ubuf *bp;
556
557 if (request == 0)
558 request = ROOTINO;
559 else if (statemap[request] != USTATE)
560 return (0);
561 for (ino = request; ino < maxino; ino++)
562 if (statemap[ino] == USTATE)
563 break;
564 if (ino == maxino)
565 return (0);
566 switch (type & IFMT) {
567 case IFDIR:
568 statemap[ino] = DSTATE;
569 break;
570 case IFREG:
571 case IFLNK:
572 statemap[ino] = FSTATE;
573 break;
574 default:
575 return (0);
576 }
577 vp = vget(fs, ino);
578 dp = (VTOI(vp)->i_din.ffs1_din);
579 bp = getblk(vp, 0, fs->lfs_fsize);
580 VOP_BWRITE(bp);
581 dp->di_mode = type;
582 (void) time(&t);
583 dp->di_atime = t;
584 dp->di_mtime = dp->di_ctime = dp->di_atime;
585 dp->di_size = fs->lfs_fsize;
586 dp->di_blocks = btofsb(fs, fs->lfs_fsize);
587 n_files++;
588 inodirty(VTOI(vp));
589 typemap[ino] = IFTODT(type);
590 return (ino);
591 }
592 /*
593 * deallocate an inode
594 */
595 void
596 freeino(ino_t ino)
597 {
598 struct inodesc idesc;
599 struct uvnode *vp;
600
601 memset(&idesc, 0, sizeof(struct inodesc));
602 idesc.id_type = ADDR;
603 idesc.id_func = pass4check;
604 idesc.id_number = ino;
605 vp = vget(fs, ino);
606 (void) ckinode(VTOD(vp), &idesc);
607 clearinode(VTOI(vp)->i_din.ffs1_din);
608 inodirty(VTOI(vp));
609 statemap[ino] = USTATE;
610
611 n_files--;
612 }
613