xref: /netbsd-src/sbin/fsck_lfs/inode.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: inode.c,v 1.24 2004/07/18 20:51:30 yamt 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@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 uvnode *vp;
436 
437 	vp = vget(fs, idesc->id_number);
438 	if (flag == 1) {
439 		pwarn("%s %s", type,
440 		      (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
441 		pinode(idesc->id_number);
442 	}
443 	if (flag == 2 || preen || reply("CLEAR") == 1) {
444 		if (preen && flag != 2)
445 			printf(" (CLEARED)\n");
446 		n_files--;
447 		(void) ckinode(VTOD(vp), idesc);
448 		clearinode(idesc->id_number);
449 		statemap[idesc->id_number] = USTATE;
450 		vnode_destroy(vp);
451 	}
452 }
453 
454 void
455 clearinode(ino_t inumber)
456 {
457 	struct ubuf *bp;
458 	IFILE *ifp;
459 	daddr_t daddr;
460 
461 	/* Send cleared inode to the free list */
462 
463 	LFS_IENTRY(ifp, fs, inumber, bp);
464 	daddr = ifp->if_daddr;
465 	ifp->if_daddr = LFS_UNUSED_DADDR;
466 	ifp->if_nextfree = fs->lfs_freehd;
467 	fs->lfs_freehd = inumber;
468 	sbdirty();
469 	VOP_BWRITE(bp);
470 
471 	/*
472 	 * update segment usage.
473 	 */
474 	if (daddr != LFS_UNUSED_DADDR) {
475 		SEGUSE *sup;
476 		u_int32_t oldsn = dtosn(fs, daddr);
477 
478 		LFS_SEGENTRY(sup, fs, oldsn, bp);
479 		sup->su_nbytes -= DINODE1_SIZE;
480 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
481 	}
482 }
483 
484 int
485 findname(struct inodesc * idesc)
486 {
487 	register struct direct *dirp = idesc->id_dirp;
488 
489 	if (dirp->d_ino != idesc->id_parent)
490 		return (KEEPON);
491 	memcpy(idesc->id_name, dirp->d_name, (size_t) dirp->d_namlen + 1);
492 	return (STOP | FOUND);
493 }
494 
495 int
496 findino(struct inodesc * idesc)
497 {
498 	register struct direct *dirp = idesc->id_dirp;
499 
500 	if (dirp->d_ino == 0)
501 		return (KEEPON);
502 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
503 	    dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
504 		idesc->id_parent = dirp->d_ino;
505 		return (STOP | FOUND);
506 	}
507 	return (KEEPON);
508 }
509 
510 void
511 pinode(ino_t ino)
512 {
513 	register struct ufs1_dinode *dp;
514 	register char *p;
515 	struct passwd *pw;
516 	time_t t;
517 
518 	printf(" I=%u ", ino);
519 	if (ino < ROOTINO || ino >= maxino)
520 		return;
521 	dp = ginode(ino);
522 	if (dp) {
523 		printf(" OWNER=");
524 #ifndef SMALL
525 		if ((pw = getpwuid((int) dp->di_uid)) != 0)
526 			printf("%s ", pw->pw_name);
527 		else
528 #endif
529 			printf("%u ", (unsigned) dp->di_uid);
530 		printf("MODE=%o\n", dp->di_mode);
531 		if (preen)
532 			printf("%s: ", cdevname());
533 		printf("SIZE=%llu ", (unsigned long long) dp->di_size);
534 		t = dp->di_mtime;
535 		p = ctime(&t);
536 		printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
537 	}
538 }
539 
540 void
541 blkerror(ino_t ino, char *type, daddr_t blk)
542 {
543 
544 	pfatal("%lld %s I=%u", (long long) blk, type, ino);
545 	printf("\n");
546 	if (exitonfail)
547 		exit(1);
548 	switch (statemap[ino]) {
549 
550 	case FSTATE:
551 		statemap[ino] = FCLEAR;
552 		return;
553 
554 	case DSTATE:
555 		statemap[ino] = DCLEAR;
556 		return;
557 
558 	case FCLEAR:
559 	case DCLEAR:
560 		return;
561 
562 	default:
563 		err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]);
564 		/* NOTREACHED */
565 	}
566 }
567 /*
568  * allocate an unused inode
569  */
570 ino_t
571 allocino(ino_t request, int type)
572 {
573 	ino_t ino;
574 	struct ufs1_dinode *dp;
575 	time_t t;
576 	struct uvnode *vp;
577 	struct ubuf *bp;
578 
579 	if (request == 0)
580 		request = ROOTINO;
581 	else if (statemap[request] != USTATE)
582 		return (0);
583 	for (ino = request; ino < maxino; ino++)
584 		if (statemap[ino] == USTATE)
585 			break;
586 	if (ino == maxino)
587 		return (0);
588 	switch (type & IFMT) {
589 	case IFDIR:
590 		statemap[ino] = DSTATE;
591 		break;
592 	case IFREG:
593 	case IFLNK:
594 		statemap[ino] = FSTATE;
595 		break;
596 	default:
597 		return (0);
598 	}
599 	vp = vget(fs, ino);
600 	dp = (VTOI(vp)->i_din.ffs1_din);
601 	bp = getblk(vp, 0, fs->lfs_fsize);
602 	VOP_BWRITE(bp);
603 	dp->di_mode = type;
604 	(void) time(&t);
605 	dp->di_atime = t;
606 	dp->di_mtime = dp->di_ctime = dp->di_atime;
607 	dp->di_size = fs->lfs_fsize;
608 	dp->di_blocks = btofsb(fs, fs->lfs_fsize);
609 	n_files++;
610 	inodirty(VTOI(vp));
611 	typemap[ino] = IFTODT(type);
612 	return (ino);
613 }
614 /*
615  * deallocate an inode
616  */
617 void
618 freeino(ino_t ino)
619 {
620 	struct inodesc idesc;
621 	struct uvnode *vp;
622 
623 	memset(&idesc, 0, sizeof(struct inodesc));
624 	idesc.id_type = ADDR;
625 	idesc.id_func = pass4check;
626 	idesc.id_number = ino;
627 	vp = vget(fs, ino);
628 	(void) ckinode(VTOD(vp), &idesc);
629 	clearinode(ino);
630 	statemap[ino] = USTATE;
631 	vnode_destroy(vp);
632 
633 	n_files--;
634 }
635