inode.c revision 1.23 1 /* $NetBSD: inode.c,v 1.23 2004/03/20 22:31: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.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_unlockvp;
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 * Check that a block in a legal block number.
327 * Return 0 if in range, 1 if out of range.
328 */
329 int
330 chkrange(daddr_t blk, int cnt)
331 {
332 if (blk < sntod(fs, 0)) {
333 return (1);
334 }
335 if (blk > maxfsblock) {
336 return (1);
337 }
338 if (blk + cnt < sntod(fs, 0)) {
339 return (1);
340 }
341 if (blk + cnt > maxfsblock) {
342 return (1);
343 }
344 return (0);
345 }
346 /*
347 * Routines to maintain information about directory inodes.
348 * This is built during the first pass and used during the
349 * second and third passes.
350 *
351 * Enter inodes into the cache.
352 */
353 void
354 cacheino(struct ufs1_dinode * dp, ino_t inumber)
355 {
356 struct inoinfo *inp;
357 struct inoinfo **inpp, **ninpsort;
358 unsigned int blks;
359
360 blks = howmany(dp->di_size, fs->lfs_bsize);
361 if (blks > NDADDR)
362 blks = NDADDR + NIADDR;
363 inp = (struct inoinfo *)
364 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
365 if (inp == NULL)
366 return;
367 inpp = &inphead[inumber % numdirs];
368 inp->i_nexthash = *inpp;
369 *inpp = inp;
370 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
371 if (inumber == ROOTINO)
372 inp->i_parent = ROOTINO;
373 else
374 inp->i_parent = (ino_t) 0;
375 inp->i_dotdot = (ino_t) 0;
376 inp->i_number = inumber;
377 inp->i_isize = dp->di_size;
378
379 inp->i_numblks = blks * sizeof(ufs_daddr_t);
380 memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks);
381 if (inplast == listmax) {
382 ninpsort = (struct inoinfo **) realloc((char *) inpsort,
383 (unsigned) (listmax + 100) * sizeof(struct inoinfo *));
384 if (ninpsort == NULL)
385 err(8, "cannot increase directory list\n");
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 register 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(8, "cannot find inode %d\n", inumber);
406 return ((struct inoinfo *) 0);
407 }
408
409 /*
410 * Clean up all the inode cache structure.
411 */
412 void
413 inocleanup()
414 {
415 register 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, char *type, int flag)
434 {
435 struct ubuf *bp;
436 IFILE *ifp;
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(VTOD(vp));
451 statemap[idesc->id_number] = USTATE;
452 inodirty(VTOI(vp));
453
454 /* Send cleared inode to the free list */
455
456 LFS_IENTRY(ifp, fs, idesc->id_number, bp);
457 ifp->if_daddr = LFS_UNUSED_DADDR;
458 ifp->if_nextfree = fs->lfs_freehd;
459 fs->lfs_freehd = idesc->id_number;
460 sbdirty();
461 VOP_BWRITE(bp);
462 }
463 }
464
465 int
466 findname(struct inodesc * idesc)
467 {
468 register struct direct *dirp = idesc->id_dirp;
469
470 if (dirp->d_ino != idesc->id_parent)
471 return (KEEPON);
472 memcpy(idesc->id_name, dirp->d_name, (size_t) dirp->d_namlen + 1);
473 return (STOP | FOUND);
474 }
475
476 int
477 findino(struct inodesc * idesc)
478 {
479 register struct direct *dirp = idesc->id_dirp;
480
481 if (dirp->d_ino == 0)
482 return (KEEPON);
483 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
484 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
485 idesc->id_parent = dirp->d_ino;
486 return (STOP | FOUND);
487 }
488 return (KEEPON);
489 }
490
491 void
492 pinode(ino_t ino)
493 {
494 register struct ufs1_dinode *dp;
495 register char *p;
496 struct passwd *pw;
497 time_t t;
498
499 printf(" I=%u ", ino);
500 if (ino < ROOTINO || ino >= maxino)
501 return;
502 dp = ginode(ino);
503 if (dp) {
504 printf(" OWNER=");
505 #ifndef SMALL
506 if ((pw = getpwuid((int) dp->di_uid)) != 0)
507 printf("%s ", pw->pw_name);
508 else
509 #endif
510 printf("%u ", (unsigned) dp->di_uid);
511 printf("MODE=%o\n", dp->di_mode);
512 if (preen)
513 printf("%s: ", cdevname());
514 printf("SIZE=%llu ", (unsigned long long) dp->di_size);
515 t = dp->di_mtime;
516 p = ctime(&t);
517 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
518 }
519 }
520
521 void
522 blkerror(ino_t ino, char *type, daddr_t blk)
523 {
524
525 pfatal("%lld %s I=%u", (long long) blk, type, ino);
526 printf("\n");
527 if (exitonfail)
528 exit(1);
529 switch (statemap[ino]) {
530
531 case FSTATE:
532 statemap[ino] = FCLEAR;
533 return;
534
535 case DSTATE:
536 statemap[ino] = DCLEAR;
537 return;
538
539 case FCLEAR:
540 case DCLEAR:
541 return;
542
543 default:
544 err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]);
545 /* NOTREACHED */
546 }
547 }
548 /*
549 * allocate an unused inode
550 */
551 ino_t
552 allocino(ino_t request, int type)
553 {
554 ino_t ino;
555 struct ufs1_dinode *dp;
556 time_t t;
557 struct uvnode *vp;
558 struct ubuf *bp;
559
560 if (request == 0)
561 request = ROOTINO;
562 else if (statemap[request] != USTATE)
563 return (0);
564 for (ino = request; ino < maxino; ino++)
565 if (statemap[ino] == USTATE)
566 break;
567 if (ino == maxino)
568 return (0);
569 switch (type & IFMT) {
570 case IFDIR:
571 statemap[ino] = DSTATE;
572 break;
573 case IFREG:
574 case IFLNK:
575 statemap[ino] = FSTATE;
576 break;
577 default:
578 return (0);
579 }
580 vp = vget(fs, ino);
581 dp = (VTOI(vp)->i_din.ffs1_din);
582 bp = getblk(vp, 0, fs->lfs_fsize);
583 VOP_BWRITE(bp);
584 dp->di_mode = type;
585 (void) time(&t);
586 dp->di_atime = t;
587 dp->di_mtime = dp->di_ctime = dp->di_atime;
588 dp->di_size = fs->lfs_fsize;
589 dp->di_blocks = btofsb(fs, fs->lfs_fsize);
590 n_files++;
591 inodirty(VTOI(vp));
592 typemap[ino] = IFTODT(type);
593 return (ino);
594 }
595 /*
596 * deallocate an inode
597 */
598 void
599 freeino(ino_t ino)
600 {
601 struct inodesc idesc;
602 struct uvnode *vp;
603
604 memset(&idesc, 0, sizeof(struct inodesc));
605 idesc.id_type = ADDR;
606 idesc.id_func = pass4check;
607 idesc.id_number = ino;
608 vp = vget(fs, ino);
609 (void) ckinode(VTOD(vp), &idesc);
610 clearinode(VTOI(vp)->i_din.ffs1_din);
611 inodirty(VTOI(vp));
612 statemap[ino] = USTATE;
613
614 n_files--;
615 }
616