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