inode.c revision 1.20 1 /* $NetBSD: inode.c,v 1.20 2003/08/07 10:04:23 agc 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;
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 listmax += 100;
376 inpsort = (struct inoinfo **) realloc((char *) inpsort,
377 (unsigned) listmax * sizeof(struct inoinfo *));
378 if (inpsort == NULL)
379 err(8, "cannot increase directory list\n");
380 }
381 inpsort[inplast++] = inp;
382 }
383
384 /*
385 * Look up an inode cache structure.
386 */
387 struct inoinfo *
388 getinoinfo(ino_t inumber)
389 {
390 register struct inoinfo *inp;
391
392 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
393 if (inp->i_number != inumber)
394 continue;
395 return (inp);
396 }
397 err(8, "cannot find inode %d\n", inumber);
398 return ((struct inoinfo *) 0);
399 }
400
401 /*
402 * Clean up all the inode cache structure.
403 */
404 void
405 inocleanup()
406 {
407 register struct inoinfo **inpp;
408
409 if (inphead == NULL)
410 return;
411 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
412 free((char *) (*inpp));
413 free((char *) inphead);
414 free((char *) inpsort);
415 inphead = inpsort = NULL;
416 }
417
418 void
419 inodirty(struct inode *ip)
420 {
421 ip->i_flag |= IN_MODIFIED;
422 }
423
424 void
425 clri(struct inodesc * idesc, char *type, int flag)
426 {
427 struct ubuf *bp;
428 IFILE *ifp;
429 struct uvnode *vp;
430
431 vp = vget(fs, idesc->id_number);
432 if (flag == 1) {
433 pwarn("%s %s", type,
434 (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
435 pinode(idesc->id_number);
436 }
437 if (flag == 2 || preen || reply("CLEAR") == 1) {
438 if (preen && flag != 2)
439 printf(" (CLEARED)\n");
440 n_files--;
441 (void) ckinode(VTOD(vp), idesc);
442 clearinode(VTOD(vp));
443 statemap[idesc->id_number] = USTATE;
444 inodirty(VTOI(vp));
445
446 /* Send cleared inode to the free list */
447
448 LFS_IENTRY(ifp, fs, idesc->id_number, bp);
449 ifp->if_daddr = LFS_UNUSED_DADDR;
450 ifp->if_nextfree = fs->lfs_freehd;
451 fs->lfs_freehd = idesc->id_number;
452 sbdirty();
453 VOP_BWRITE(bp);
454 }
455 }
456
457 int
458 findname(struct inodesc * idesc)
459 {
460 register struct direct *dirp = idesc->id_dirp;
461
462 if (dirp->d_ino != idesc->id_parent)
463 return (KEEPON);
464 memcpy(idesc->id_name, dirp->d_name, (size_t) dirp->d_namlen + 1);
465 return (STOP | FOUND);
466 }
467
468 int
469 findino(struct inodesc * idesc)
470 {
471 register struct direct *dirp = idesc->id_dirp;
472
473 if (dirp->d_ino == 0)
474 return (KEEPON);
475 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
476 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
477 idesc->id_parent = dirp->d_ino;
478 return (STOP | FOUND);
479 }
480 return (KEEPON);
481 }
482
483 void
484 pinode(ino_t ino)
485 {
486 register struct ufs1_dinode *dp;
487 register char *p;
488 struct passwd *pw;
489 time_t t;
490
491 printf(" I=%u ", ino);
492 if (ino < ROOTINO || ino >= maxino)
493 return;
494 dp = ginode(ino);
495 if (dp) {
496 printf(" OWNER=");
497 #ifndef SMALL
498 if ((pw = getpwuid((int) dp->di_uid)) != 0)
499 printf("%s ", pw->pw_name);
500 else
501 #endif
502 printf("%u ", (unsigned) dp->di_uid);
503 printf("MODE=%o\n", dp->di_mode);
504 if (preen)
505 printf("%s: ", cdevname());
506 printf("SIZE=%llu ", (unsigned long long) dp->di_size);
507 t = dp->di_mtime;
508 p = ctime(&t);
509 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
510 }
511 }
512
513 void
514 blkerror(ino_t ino, char *type, daddr_t blk)
515 {
516
517 pfatal("%lld %s I=%u", (long long) blk, type, ino);
518 printf("\n");
519 if (exitonfail)
520 exit(1);
521 switch (statemap[ino]) {
522
523 case FSTATE:
524 statemap[ino] = FCLEAR;
525 return;
526
527 case DSTATE:
528 statemap[ino] = DCLEAR;
529 return;
530
531 case FCLEAR:
532 case DCLEAR:
533 return;
534
535 default:
536 err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]);
537 /* NOTREACHED */
538 }
539 }
540 /*
541 * allocate an unused inode
542 */
543 ino_t
544 allocino(ino_t request, int type)
545 {
546 ino_t ino;
547 struct ufs1_dinode *dp;
548 time_t t;
549 struct uvnode *vp;
550 struct ubuf *bp;
551
552 if (request == 0)
553 request = ROOTINO;
554 else if (statemap[request] != USTATE)
555 return (0);
556 for (ino = request; ino < maxino; ino++)
557 if (statemap[ino] == USTATE)
558 break;
559 if (ino == maxino)
560 return (0);
561 switch (type & IFMT) {
562 case IFDIR:
563 statemap[ino] = DSTATE;
564 break;
565 case IFREG:
566 case IFLNK:
567 statemap[ino] = FSTATE;
568 break;
569 default:
570 return (0);
571 }
572 vp = vget(fs, ino);
573 dp = (VTOI(vp)->i_din.ffs1_din);
574 bp = getblk(vp, 0, fs->lfs_fsize);
575 VOP_BWRITE(bp);
576 dp->di_mode = type;
577 (void) time(&t);
578 dp->di_atime = t;
579 dp->di_mtime = dp->di_ctime = dp->di_atime;
580 dp->di_size = fs->lfs_fsize;
581 dp->di_blocks = btofsb(fs, fs->lfs_fsize);
582 n_files++;
583 inodirty(VTOI(vp));
584 typemap[ino] = IFTODT(type);
585 return (ino);
586 }
587 /*
588 * deallocate an inode
589 */
590 void
591 freeino(ino_t ino)
592 {
593 struct inodesc idesc;
594 struct uvnode *vp;
595
596 memset(&idesc, 0, sizeof(struct inodesc));
597 idesc.id_type = ADDR;
598 idesc.id_func = pass4check;
599 idesc.id_number = ino;
600 vp = vget(fs, ino);
601 (void) ckinode(VTOD(vp), &idesc);
602 clearinode(VTOI(vp)->i_din.ffs1_din);
603 inodirty(VTOI(vp));
604 statemap[ino] = USTATE;
605
606 n_files--;
607 }
608