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