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