inode.c revision 1.32 1 /* $NetBSD: inode.c,v 1.32 2006/07/18 23:37:13 perseant 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
88 #include "bufcache.h"
89 #include "vnode.h"
90 #include "lfs_user.h"
91
92 #include "fsck.h"
93 #include "fsutil.h"
94 #include "extern.h"
95
96 extern SEGUSE *seg_table;
97 extern ufs_daddr_t *din_table;
98
99 static int iblock(struct inodesc *, long, u_int64_t);
100 int blksreqd(struct lfs *, int);
101 int lfs_maxino(void);
102
103 /*
104 * Get a dinode of a given inum.
105 * XXX combine this function with vget.
106 */
107 struct ufs1_dinode *
108 ginode(ino_t ino)
109 {
110 struct uvnode *vp;
111 struct ubuf *bp;
112 IFILE *ifp;
113
114 vp = vget(fs, ino);
115 if (vp == NULL)
116 return NULL;
117
118 if (din_table[ino] == 0x0) {
119 LFS_IENTRY(ifp, fs, ino, bp);
120 din_table[ino] = ifp->if_daddr;
121 seg_table[dtosn(fs, ifp->if_daddr)].su_nbytes += DINODE1_SIZE;
122 brelse(bp);
123 }
124 return (VTOI(vp)->i_din.ffs1_din);
125 }
126
127 /*
128 * Check validity of held blocks in an inode, recursing through all blocks.
129 */
130 int
131 ckinode(struct ufs1_dinode *dp, struct inodesc *idesc)
132 {
133 ufs_daddr_t *ap, lbn;
134 long ret, n, ndb, offset;
135 struct ufs1_dinode dino;
136 u_int64_t remsize, sizepb;
137 mode_t mode;
138 char pathbuf[MAXPATHLEN + 1];
139 struct uvnode *vp, *thisvp;
140
141 if (idesc->id_fix != IGNORE)
142 idesc->id_fix = DONTKNOW;
143 idesc->id_entryno = 0;
144 idesc->id_filesize = dp->di_size;
145 mode = dp->di_mode & IFMT;
146 if (mode == IFBLK || mode == IFCHR ||
147 (mode == IFLNK && (dp->di_size < fs->lfs_maxsymlinklen ||
148 (fs->lfs_maxsymlinklen == 0 &&
149 dp->di_blocks == 0))))
150 return (KEEPON);
151 dino = *dp;
152 ndb = howmany(dino.di_size, fs->lfs_bsize);
153
154 thisvp = vget(fs, idesc->id_number);
155 for (lbn = 0; lbn < NDADDR; lbn++) {
156 ap = dino.di_db + lbn;
157 if (thisvp)
158 idesc->id_numfrags =
159 numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
160 else {
161 if (--ndb == 0 && (offset = blkoff(fs, dino.di_size)) != 0) {
162 idesc->id_numfrags =
163 numfrags(fs, fragroundup(fs, offset));
164 } else
165 idesc->id_numfrags = fs->lfs_frag;
166 }
167 if (*ap == 0) {
168 if (idesc->id_type == DATA && ndb >= 0) {
169 /* An empty block in a directory XXX */
170 getpathname(pathbuf, sizeof(pathbuf),
171 idesc->id_number, idesc->id_number);
172 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]",
173 pathbuf, (long long)idesc->id_number);
174 if (reply("ADJUST LENGTH") == 1) {
175 vp = vget(fs, idesc->id_number);
176 dp = VTOD(vp);
177 dp->di_size = (ap - &dino.di_db[0]) *
178 fs->lfs_bsize;
179 printf(
180 "YOU MUST RERUN FSCK AFTERWARDS\n");
181 rerun = 1;
182 inodirty(VTOI(vp));
183 } else
184 break;
185 }
186 continue;
187 }
188 idesc->id_blkno = *ap;
189 idesc->id_lblkno = ap - &dino.di_db[0];
190 if (idesc->id_type == ADDR) {
191 ret = (*idesc->id_func) (idesc);
192 } else
193 ret = dirscan(idesc);
194 if (ret & STOP)
195 return (ret);
196 }
197 idesc->id_numfrags = fs->lfs_frag;
198 remsize = dino.di_size - fs->lfs_bsize * NDADDR;
199 sizepb = fs->lfs_bsize;
200 for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
201 if (*ap) {
202 idesc->id_blkno = *ap;
203 ret = iblock(idesc, n, remsize);
204 if (ret & STOP)
205 return (ret);
206 } else {
207 if (idesc->id_type == DATA && remsize > 0) {
208 /* An empty block in a directory XXX */
209 getpathname(pathbuf, sizeof(pathbuf),
210 idesc->id_number, idesc->id_number);
211 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]",
212 pathbuf, (long long)idesc->id_number);
213 if (reply("ADJUST LENGTH") == 1) {
214 vp = vget(fs, idesc->id_number);
215 dp = VTOD(vp);
216 dp->di_size -= remsize;
217 remsize = 0;
218 printf(
219 "YOU MUST RERUN FSCK AFTERWARDS\n");
220 rerun = 1;
221 inodirty(VTOI(vp));
222 break;
223 } else
224 break;
225 }
226 }
227 sizepb *= NINDIR(fs);
228 remsize -= sizepb;
229 }
230 return (KEEPON);
231 }
232
233 static int
234 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
235 {
236 ufs_daddr_t *ap, *aplim;
237 struct ubuf *bp;
238 int i, n, (*func) (struct inodesc *), nif;
239 u_int64_t sizepb;
240 char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
241 struct uvnode *devvp, *vp;
242 int diddirty = 0;
243
244 if (idesc->id_type == ADDR) {
245 func = idesc->id_func;
246 n = (*func) (idesc);
247 if ((n & KEEPON) == 0)
248 return (n);
249 } else
250 func = dirscan;
251 if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags)))
252 return (SKIP);
253
254 devvp = fs->lfs_devvp;
255 bread(devvp, fsbtodb(fs, idesc->id_blkno), fs->lfs_bsize, NOCRED, &bp);
256 ilevel--;
257 for (sizepb = fs->lfs_bsize, i = 0; i < ilevel; i++)
258 sizepb *= NINDIR(fs);
259 if (isize > sizepb * NINDIR(fs))
260 nif = NINDIR(fs);
261 else
262 nif = howmany(isize, sizepb);
263 if (idesc->id_func == pass1check && nif < NINDIR(fs)) {
264 aplim = ((ufs_daddr_t *) bp->b_data) + NINDIR(fs);
265 for (ap = ((ufs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) {
266 if (*ap == 0)
267 continue;
268 (void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%llu",
269 (unsigned long long)idesc->id_number);
270 if (dofix(idesc, buf)) {
271 *ap = 0;
272 ++diddirty;
273 }
274 }
275 }
276 aplim = ((ufs_daddr_t *) bp->b_data) + nif;
277 for (ap = ((ufs_daddr_t *) bp->b_data); ap < aplim; ap++) {
278 if (*ap) {
279 idesc->id_blkno = *ap;
280 if (ilevel == 0) {
281 /*
282 * dirscan needs lblkno.
283 */
284 idesc->id_lblkno++;
285 n = (*func) (idesc);
286 } else {
287 n = iblock(idesc, ilevel, isize);
288 }
289 if (n & STOP) {
290 if (diddirty)
291 VOP_BWRITE(bp);
292 else
293 brelse(bp);
294 return (n);
295 }
296 } else {
297 if (idesc->id_type == DATA && isize > 0) {
298 /* An empty block in a directory XXX */
299 getpathname(pathbuf, sizeof(pathbuf),
300 idesc->id_number, idesc->id_number);
301 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]",
302 pathbuf, (long long)idesc->id_number);
303 if (reply("ADJUST LENGTH") == 1) {
304 vp = vget(fs, idesc->id_number);
305 VTOI(vp)->i_ffs1_size -= isize;
306 isize = 0;
307 printf(
308 "YOU MUST RERUN FSCK AFTERWARDS\n");
309 rerun = 1;
310 inodirty(VTOI(vp));
311 if (diddirty)
312 VOP_BWRITE(bp);
313 else
314 brelse(bp);
315 return (STOP);
316 }
317 }
318 }
319 isize -= sizepb;
320 }
321 if (diddirty)
322 VOP_BWRITE(bp);
323 else
324 brelse(bp);
325 return (KEEPON);
326 }
327
328 /*
329 * Check that a block in a legal block number.
330 * Return 0 if in range, 1 if out of range.
331 */
332 int
333 chkrange(daddr_t blk, int cnt)
334 {
335 if (blk < sntod(fs, 0)) {
336 return (1);
337 }
338 if (blk > maxfsblock) {
339 return (1);
340 }
341 if (blk + cnt < sntod(fs, 0)) {
342 return (1);
343 }
344 if (blk + cnt > maxfsblock) {
345 return (1);
346 }
347 return (0);
348 }
349
350 /*
351 * Routines to maintain information about directory inodes.
352 * This is built during the first pass and used during the
353 * second and third passes.
354 *
355 * Enter inodes into the cache.
356 */
357 void
358 cacheino(struct ufs1_dinode * dp, ino_t inumber)
359 {
360 struct inoinfo *inp;
361 struct inoinfo **inpp, **ninpsort;
362 unsigned int blks;
363
364 blks = howmany(dp->di_size, fs->lfs_bsize);
365 if (blks > NDADDR)
366 blks = NDADDR + NIADDR;
367 inp = (struct inoinfo *)
368 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
369 if (inp == NULL)
370 return;
371 inpp = &inphead[inumber % numdirs];
372 inp->i_nexthash = *inpp;
373 *inpp = inp;
374 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
375 if (inumber == ROOTINO)
376 inp->i_parent = ROOTINO;
377 else
378 inp->i_parent = (ino_t) 0;
379 inp->i_dotdot = (ino_t) 0;
380 inp->i_number = inumber;
381 inp->i_isize = dp->di_size;
382
383 inp->i_numblks = blks * sizeof(ufs_daddr_t);
384 memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks);
385 if (inplast == listmax) {
386 ninpsort = (struct inoinfo **) realloc((char *) inpsort,
387 (unsigned) (listmax + 100) * sizeof(struct inoinfo *));
388 if (ninpsort == NULL)
389 err(8, "cannot increase directory list\n");
390 inpsort = ninpsort;
391 listmax += 100;
392 }
393 inpsort[inplast++] = inp;
394 }
395
396 /*
397 * Look up an inode cache structure.
398 */
399 struct inoinfo *
400 getinoinfo(ino_t inumber)
401 {
402 struct inoinfo *inp;
403
404 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
405 if (inp->i_number != inumber)
406 continue;
407 return (inp);
408 }
409 err(8, "cannot find inode %llu\n", (unsigned long long)inumber);
410 return ((struct inoinfo *) 0);
411 }
412
413 /*
414 * Clean up all the inode cache structure.
415 */
416 void
417 inocleanup(void)
418 {
419 struct inoinfo **inpp;
420
421 if (inphead == NULL)
422 return;
423 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
424 free((char *) (*inpp));
425 free((char *) inphead);
426 free((char *) inpsort);
427 inphead = inpsort = NULL;
428 }
429
430 void
431 inodirty(struct inode *ip)
432 {
433 ip->i_flag |= IN_MODIFIED;
434 }
435
436 void
437 clri(struct inodesc * idesc, const char *type, int flag)
438 {
439 struct uvnode *vp;
440
441 vp = vget(fs, idesc->id_number);
442 if (flag == 1) {
443 pwarn("%s %s", type,
444 (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
445 pinode(idesc->id_number);
446 }
447 if (flag == 2 || preen || reply("CLEAR") == 1) {
448 if (preen && flag != 2)
449 printf(" (CLEARED)\n");
450 n_files--;
451 (void) ckinode(VTOD(vp), idesc);
452 clearinode(idesc->id_number);
453 statemap[idesc->id_number] = USTATE;
454 vnode_destroy(vp);
455 }
456 }
457
458 void
459 clearinode(ino_t inumber)
460 {
461 struct ubuf *bp;
462 IFILE *ifp;
463 daddr_t daddr;
464
465 /* Send cleared inode to the free list */
466
467 LFS_IENTRY(ifp, fs, inumber, bp);
468 daddr = ifp->if_daddr;
469 ifp->if_daddr = LFS_UNUSED_DADDR;
470 ifp->if_nextfree = fs->lfs_freehd;
471 fs->lfs_freehd = inumber;
472 sbdirty();
473 VOP_BWRITE(bp);
474
475 /*
476 * update segment usage.
477 */
478 if (daddr != LFS_UNUSED_DADDR) {
479 SEGUSE *sup;
480 u_int32_t oldsn = dtosn(fs, daddr);
481
482 LFS_SEGENTRY(sup, fs, oldsn, bp);
483 sup->su_nbytes -= DINODE1_SIZE;
484 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
485 }
486 }
487
488 int
489 findname(struct inodesc * idesc)
490 {
491 struct direct *dirp = idesc->id_dirp;
492 size_t len;
493 char *buf;
494
495 if (dirp->d_ino != idesc->id_parent)
496 return (KEEPON);
497 if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
498 /* Truncate it but don't overflow the buffer */
499 len = MAXPATHLEN;
500 }
501 /* this is namebuf with utils.h */
502 buf = __UNCONST(idesc->id_name);
503 (void)memcpy(buf, dirp->d_name, len);
504 return (STOP | FOUND);
505 }
506
507 int
508 findino(struct inodesc * idesc)
509 {
510 struct direct *dirp = idesc->id_dirp;
511
512 if (dirp->d_ino == 0)
513 return (KEEPON);
514 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
515 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
516 idesc->id_parent = dirp->d_ino;
517 return (STOP | FOUND);
518 }
519 return (KEEPON);
520 }
521
522 void
523 pinode(ino_t ino)
524 {
525 struct ufs1_dinode *dp;
526 char *p;
527 struct passwd *pw;
528 time_t t;
529
530 printf(" I=%llu ", (unsigned long long)ino);
531 if (ino < ROOTINO || ino >= maxino)
532 return;
533 dp = ginode(ino);
534 if (dp) {
535 printf(" OWNER=");
536 #ifndef SMALL
537 if ((pw = getpwuid((int) dp->di_uid)) != 0)
538 printf("%s ", pw->pw_name);
539 else
540 #endif
541 printf("%u ", (unsigned) dp->di_uid);
542 printf("MODE=%o\n", dp->di_mode);
543 if (preen)
544 printf("%s: ", cdevname());
545 printf("SIZE=%llu ", (unsigned long long) dp->di_size);
546 t = dp->di_mtime;
547 p = ctime(&t);
548 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
549 }
550 }
551
552 void
553 blkerror(ino_t ino, const char *type, daddr_t blk)
554 {
555
556 pfatal("%lld %s I=%llu", (long long) blk, type,
557 (unsigned long long)ino);
558 printf("\n");
559 if (exitonfail)
560 exit(1);
561 switch (statemap[ino]) {
562
563 case FSTATE:
564 statemap[ino] = FCLEAR;
565 return;
566
567 case DSTATE:
568 statemap[ino] = DCLEAR;
569 return;
570
571 case FCLEAR:
572 case DCLEAR:
573 return;
574
575 default:
576 err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]);
577 /* NOTREACHED */
578 }
579 }
580
581 /*
582 * allocate an unused inode
583 */
584 ino_t
585 allocino(ino_t request, int type)
586 {
587 ino_t ino;
588 struct ufs1_dinode *dp;
589 time_t t;
590 struct uvnode *vp;
591 struct ubuf *bp;
592
593 if (request == 0)
594 request = ROOTINO;
595 else if (statemap[request] != USTATE)
596 return (0);
597 for (ino = request; ino < maxino; ino++)
598 if (statemap[ino] == USTATE)
599 break;
600 if (ino == maxino)
601 extend_ifile(fs);
602
603 switch (type & IFMT) {
604 case IFDIR:
605 statemap[ino] = DSTATE;
606 break;
607 case IFREG:
608 case IFLNK:
609 statemap[ino] = FSTATE;
610 break;
611 default:
612 return (0);
613 }
614 vp = lfs_valloc(fs, ino);
615 if (vp == NULL)
616 return (0);
617 dp = (VTOI(vp)->i_din.ffs1_din);
618 bp = getblk(vp, 0, fs->lfs_fsize);
619 VOP_BWRITE(bp);
620 dp->di_mode = type;
621 (void) time(&t);
622 dp->di_atime = t;
623 dp->di_mtime = dp->di_ctime = dp->di_atime;
624 dp->di_size = fs->lfs_fsize;
625 dp->di_blocks = btofsb(fs, fs->lfs_fsize);
626 n_files++;
627 inodirty(VTOI(vp));
628 typemap[ino] = IFTODT(type);
629 return (ino);
630 }
631
632 /*
633 * deallocate an inode
634 */
635 void
636 freeino(ino_t ino)
637 {
638 struct inodesc idesc;
639 struct uvnode *vp;
640
641 memset(&idesc, 0, sizeof(struct inodesc));
642 idesc.id_type = ADDR;
643 idesc.id_func = pass4check;
644 idesc.id_number = ino;
645 vp = vget(fs, ino);
646 (void) ckinode(VTOD(vp), &idesc);
647 clearinode(ino);
648 statemap[ino] = USTATE;
649 vnode_destroy(vp);
650
651 n_files--;
652 }
653