xref: /dragonfly/sbin/fsck/inode.c (revision 7c47e3df)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)inode.c	8.8 (Berkeley) 4/28/95
30  * $FreeBSD: src/sbin/fsck/inode.c,v 1.20 2000/02/28 20:02:41 mckusick Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/dir.h>
38 #include <vfs/ufs/fs.h>
39 
40 #include <err.h>
41 #include <pwd.h>
42 #include <string.h>
43 
44 #include "fsck.h"
45 #include "memzone.h"
46 
47 struct bufarea cgblk;		/* cylinder group blocks */
48 struct bufarea *pbp;		/* current inode block */
49 int	newinofmt;		/* filesystem has new inode format */
50 static ufs1_ino_t startinum;
51 char	rerun;			/* rerun fsck. Only used in non-preen mode */
52 struct	ufs1_dinode zino;
53 
54 static int iblock(struct inodesc *, long ilevel, quad_t isize);
55 
56 int
57 ckinode(struct ufs1_dinode *dp, struct inodesc *idesc)
58 {
59 	ufs_daddr_t *ap;
60 	int ret;
61 	long n, ndb, offset;
62 	struct ufs1_dinode dino;
63 	quad_t remsize, sizepb;
64 	mode_t mode;
65 	char pathbuf[MAXPATHLEN + 1];
66 
67 	if (idesc->id_fix != IGNORE)
68 		idesc->id_fix = DONTKNOW;
69 	idesc->id_entryno = 0;
70 	idesc->id_filesize = dp->di_size;
71 	mode = dp->di_mode & IFMT;
72 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
73 	    dp->di_size < (unsigned)sblock.fs_maxsymlinklen))
74 		return (KEEPON);
75 	dino = *dp;
76 	ndb = howmany(dino.di_size, sblock.fs_bsize);
77 	for (ap = &dino.di_db[0]; ap < &dino.di_db[UFS_NDADDR]; ap++) {
78 		if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0)
79 			idesc->id_numfrags =
80 				numfrags(&sblock, fragroundup(&sblock, offset));
81 		else
82 			idesc->id_numfrags = sblock.fs_frag;
83 		if (*ap == 0) {
84 			if (idesc->id_type == DATA && ndb >= 0) {
85 				/* An empty block in a directory XXX */
86 				getpathname(pathbuf, idesc->id_number,
87 						idesc->id_number);
88                         	pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
89 					pathbuf);
90                         	if (reply("ADJUST LENGTH") == 1) {
91 					dp = ginode(idesc->id_number);
92                                 	dp->di_size = (ap - &dino.di_db[0]) *
93 					    sblock.fs_bsize;
94 					printf(
95 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
96 					rerun = 1;
97                                 	inodirty();
98 
99                         	}
100 			}
101 			continue;
102 		}
103 		idesc->id_blkno = *ap;
104 		if (idesc->id_type == ADDR)
105 			ret = (*idesc->id_func)(idesc);
106 		else
107 			ret = dirscan(idesc);
108 		if (ret & STOP)
109 			return (ret);
110 	}
111 	idesc->id_numfrags = sblock.fs_frag;
112 	remsize = dino.di_size - sblock.fs_bsize * UFS_NDADDR;
113 	sizepb = sblock.fs_bsize;
114 	for (ap = &dino.di_ib[0], n = 1; n <= UFS_NIADDR; ap++, n++) {
115 		if (*ap) {
116 			idesc->id_blkno = *ap;
117 			ret = iblock(idesc, n, remsize);
118 			if (ret & STOP)
119 				return (ret);
120 		} else {
121 			if (idesc->id_type == DATA && remsize > 0) {
122 				/* An empty block in a directory XXX */
123 				getpathname(pathbuf, idesc->id_number,
124 						idesc->id_number);
125                         	pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
126 					pathbuf);
127                         	if (reply("ADJUST LENGTH") == 1) {
128 					dp = ginode(idesc->id_number);
129                                 	dp->di_size -= remsize;
130 					remsize = 0;
131 					printf(
132 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
133 					rerun = 1;
134                                 	inodirty();
135 					break;
136                         	}
137 			}
138 		}
139 		sizepb *= NINDIR(&sblock);
140 		remsize -= sizepb;
141 	}
142 	return (KEEPON);
143 }
144 
145 static int
146 iblock(struct inodesc *idesc, long ilevel, quad_t isize)
147 {
148 	ufs_daddr_t *ap;
149 	ufs_daddr_t *aplim;
150 	struct bufarea *bp;
151 	int i, n, (*func)(), nif;
152 	quad_t sizepb;
153 	char buf[BUFSIZ];
154 	char pathbuf[MAXPATHLEN + 1];
155 	struct ufs1_dinode *dp;
156 
157 	if (idesc->id_type == ADDR) {
158 		func = idesc->id_func;
159 		if (((n = (*func)(idesc)) & KEEPON) == 0)
160 			return (n);
161 	} else
162 		func = dirscan;
163 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
164 		return (SKIP);
165 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
166 	ilevel--;
167 	for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
168 		sizepb *= NINDIR(&sblock);
169 	nif = howmany(isize , sizepb);
170 	if (nif > NINDIR(&sblock))
171 		nif = NINDIR(&sblock);
172 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
173 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
174 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
175 			if (*ap == 0)
176 				continue;
177 			sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
178 			    (u_long)idesc->id_number);
179 			if (dofix(idesc, buf)) {
180 				*ap = 0;
181 				dirty(bp);
182 			}
183 		}
184 		flush(fswritefd, bp);
185 	}
186 	aplim = &bp->b_un.b_indir[nif];
187 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
188 		if (*ap) {
189 			idesc->id_blkno = *ap;
190 			if (ilevel == 0)
191 				n = (*func)(idesc);
192 			else
193 				n = iblock(idesc, ilevel, isize);
194 			if (n & STOP) {
195 				bp->b_flags &= ~B_INUSE;
196 				return (n);
197 			}
198 		} else {
199 			if (idesc->id_type == DATA && isize > 0) {
200 				/* An empty block in a directory XXX */
201 				getpathname(pathbuf, idesc->id_number,
202 						idesc->id_number);
203                         	pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
204 					pathbuf);
205                         	if (reply("ADJUST LENGTH") == 1) {
206 					dp = ginode(idesc->id_number);
207                                 	dp->di_size -= isize;
208 					isize = 0;
209 					printf(
210 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
211 					rerun = 1;
212                                 	inodirty();
213 					bp->b_flags &= ~B_INUSE;
214 					return(STOP);
215                         	}
216 			}
217 		}
218 		isize -= sizepb;
219 	}
220 	bp->b_flags &= ~B_INUSE;
221 	return (KEEPON);
222 }
223 
224 /*
225  * Check that a block in a legal block number.
226  * Return 0 if in range, 1 if out of range.
227  */
228 int
229 chkrange(ufs_daddr_t blk, int cnt)
230 {
231 	int c;
232 
233 	if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
234 	    cnt - 1 > maxfsblock - blk)
235 		return (1);
236 	if (cnt > sblock.fs_frag ||
237 	    fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
238 		if (debug)
239 			printf("bad size: blk %ld, offset %d, size %d\n",
240 				(long)blk, fragnum(&sblock, blk), cnt);
241 		return (1);
242 	}
243 	c = dtog(&sblock, blk);
244 	if (blk < cgdmin(&sblock, c)) {
245 		if ((blk + cnt) > cgsblock(&sblock, c)) {
246 			if (debug) {
247 				printf("blk %ld < cgdmin %ld;",
248 				    (long)blk, (long)cgdmin(&sblock, c));
249 				printf(" blk + cnt %ld > cgsbase %ld\n",
250 				    (long)(blk + cnt),
251 				    (long)cgsblock(&sblock, c));
252 			}
253 			return (1);
254 		}
255 	} else {
256 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
257 			if (debug)  {
258 				printf("blk %ld >= cgdmin %ld;",
259 				    (long)blk, (long)cgdmin(&sblock, c));
260 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
261 				    (long)(blk + cnt), (long)sblock.fs_fpg);
262 			}
263 			return (1);
264 		}
265 	}
266 	return (0);
267 }
268 
269 /*
270  * General purpose interface for reading inodes.
271  */
272 struct ufs1_dinode *
273 ginode(ufs1_ino_t inumber)
274 {
275 	ufs_daddr_t iblk;
276 
277 	if (inumber < UFS_ROOTINO || inumber > maxino)
278 		errx(EEXIT, "bad inode number %d to ginode", inumber);
279 	if (startinum == 0 ||
280 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
281 		iblk = ino_to_fsba(&sblock, inumber);
282 		if (pbp != 0)
283 			pbp->b_flags &= ~B_INUSE;
284 		pbp = getdatablk(iblk, sblock.fs_bsize);
285 		startinum = rounddown(inumber, INOPB(&sblock));
286 	}
287 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
288 }
289 
290 /*
291  * Special purpose version of ginode used to optimize first pass
292  * over all the inodes in numerical order.
293  */
294 ufs1_ino_t nextino, lastinum;
295 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
296 struct ufs1_dinode *inodebuf;
297 
298 struct ufs1_dinode *
299 getnextinode(ufs1_ino_t inumber)
300 {
301 	long size;
302 	ufs_daddr_t dblk;
303 	static struct ufs1_dinode *dp;
304 
305 	if (inumber != nextino++ || inumber > maxino)
306 		errx(EEXIT, "bad inode number %d to nextinode", inumber);
307 	if (inumber >= lastinum) {
308 		readcnt++;
309 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
310 		if (readcnt % readpercg == 0) {
311 			size = partialsize;
312 			lastinum += partialcnt;
313 		} else {
314 			size = inobufsize;
315 			lastinum += fullcnt;
316 		}
317 		/*
318 		 * If bread returns an error, it will already have zeroed
319 		 * out the buffer, so we do not need to do so here.
320 		 */
321 		bread(fsreadfd, (char *)inodebuf, dblk, size);
322 		dp = inodebuf;
323 	}
324 	return (dp++);
325 }
326 
327 void
328 setinodebuf(ufs1_ino_t inum)
329 {
330 
331 	if (inum % sblock.fs_ipg != 0)
332 		errx(EEXIT, "bad inode number %d to setinodebuf", inum);
333 	startinum = 0;
334 	nextino = inum;
335 	lastinum = inum;
336 	readcnt = 0;
337 	if (inodebuf != NULL)
338 		return;
339 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
340 	fullcnt = inobufsize / sizeof(struct ufs1_dinode);
341 	readpercg = sblock.fs_ipg / fullcnt;
342 	partialcnt = sblock.fs_ipg % fullcnt;
343 	partialsize = partialcnt * sizeof(struct ufs1_dinode);
344 	if (partialcnt != 0) {
345 		readpercg++;
346 	} else {
347 		partialcnt = fullcnt;
348 		partialsize = inobufsize;
349 	}
350 	if ((inodebuf = (struct ufs1_dinode *)malloc((unsigned)inobufsize)) == NULL)
351 		errx(EEXIT, "cannot allocate space for inode buffer");
352 }
353 
354 void
355 freeinodebuf(void)
356 {
357 
358 	if (inodebuf != NULL)
359 		free((char *)inodebuf);
360 	inodebuf = NULL;
361 }
362 
363 /*
364  * Routines to maintain information about directory inodes.
365  * This is built during the first pass and used during the
366  * second and third passes.
367  *
368  * Enter inodes into the cache.
369  */
370 static struct memzone inoinfo_zone;
371 
372 void
373 cacheino(struct ufs1_dinode *dp, ufs1_ino_t inumber)
374 {
375 	struct inoinfo *inp;
376 	struct inoinfo **inpp;
377 	int blks;
378 
379 	blks = howmany(dp->di_size, sblock.fs_bsize);
380 	if (blks > UFS_NDADDR)
381 		blks = UFS_NDADDR + UFS_NIADDR;
382 	inp = mzalloc(&inoinfo_zone,
383 		      sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
384 	if (inp == NULL)
385 		errx(EEXIT, "cannot increase directory list");
386 	inpp = &inphead[DIRHASH(inumber)];
387 	inp->i_nexthash = *inpp;
388 	*inpp = inp;
389 	inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ufs1_ino_t)0;
390 	inp->i_dotdot = (ufs1_ino_t)0;
391 	inp->i_number = inumber;
392 	inp->i_isize = dp->di_size;
393 	inp->i_numblks = blks * sizeof(ufs_daddr_t);
394 	memmove(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
395 	if (inplast == listmax) {
396 		listmax += 100;
397 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
398 		    (unsigned)listmax * sizeof(struct inoinfo *));
399 		if (inpsort == NULL)
400 			errx(EEXIT, "cannot increase directory list");
401 	}
402 	inpsort[inplast++] = inp;
403 }
404 
405 /*
406  * Look up an inode cache structure.
407  */
408 struct inoinfo *
409 getinoinfo(ufs1_ino_t inumber)
410 {
411 	struct inoinfo *inp;
412 
413 	for (inp = inphead[DIRHASH(inumber)]; inp; inp = inp->i_nexthash) {
414 		if (inp->i_number != inumber)
415 			continue;
416 		return (inp);
417 	}
418 	errx(EEXIT, "cannot find inode %d", inumber);
419 	return (NULL);
420 }
421 
422 /*
423  * Clean up all the inode cache structure.
424  */
425 void
426 inocleanup(void)
427 {
428 	if (inphead == NULL)
429 		return;
430 	mzpurge(&inoinfo_zone);
431 	free((char *)inphead);
432 	free((char *)inpsort);
433 	inphead = inpsort = NULL;
434 }
435 
436 void
437 inodirty(void)
438 {
439 
440 	dirty(pbp);
441 }
442 
443 void
444 clri(struct inodesc *idesc, char *type, int flag)
445 {
446 	struct ufs1_dinode *dp;
447 
448 	dp = ginode(idesc->id_number);
449 	if (flag == 1) {
450 		pwarn("%s %s", type,
451 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
452 		pinode(idesc->id_number);
453 	}
454 	if (preen || reply("CLEAR") == 1) {
455 		if (preen)
456 			printf(" (CLEARED)\n");
457 		n_files--;
458 		ckinode(dp, idesc);
459 		clearinode(dp);
460 		inoinfo(idesc->id_number)->ino_state = USTATE;
461 		inodirty();
462 	}
463 }
464 
465 int
466 findname(struct inodesc *idesc)
467 {
468 	struct direct *dirp = idesc->id_dirp;
469 
470 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
471 		idesc->id_entryno++;
472 		return (KEEPON);
473 	}
474 	memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
475 	return (STOP|FOUND);
476 }
477 
478 int
479 findino(struct inodesc *idesc)
480 {
481 	struct direct *dirp = idesc->id_dirp;
482 
483 	if (dirp->d_ino == 0)
484 		return (KEEPON);
485 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
486 	    dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
487 		idesc->id_parent = dirp->d_ino;
488 		return (STOP|FOUND);
489 	}
490 	return (KEEPON);
491 }
492 
493 int
494 clearentry(struct inodesc *idesc)
495 {
496 	struct direct *dirp = idesc->id_dirp;
497 
498 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
499 		idesc->id_entryno++;
500 		return (KEEPON);
501 	}
502 	dirp->d_ino = 0;
503 	return (STOP|FOUND|ALTERED);
504 }
505 
506 void
507 pinode(ufs1_ino_t ino)
508 {
509 	struct ufs1_dinode *dp;
510 	char *p;
511 	struct passwd *pw;
512 	time_t t;
513 
514 	printf(" I=%lu ", (u_long)ino);
515 	if (ino < UFS_ROOTINO || ino > maxino)
516 		return;
517 	dp = ginode(ino);
518 	printf(" OWNER=");
519 	if ((pw = getpwuid((int)dp->di_uid)) != NULL)
520 		printf("%s ", pw->pw_name);
521 	else
522 		printf("%u ", (unsigned)dp->di_uid);
523 	printf("MODE=%o\n", dp->di_mode);
524 	if (preen)
525 		printf("%s: ", cdevname);
526 	printf("SIZE=%ju ", (uintmax_t)dp->di_size);
527 	t = dp->di_mtime;
528 	p = ctime(&t);
529 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
530 }
531 
532 void
533 blkerror(ufs1_ino_t ino, char *type, ufs_daddr_t blk)
534 {
535 
536 	pfatal("%d %s I=%u", blk, type, ino);
537 	printf("\n");
538 	switch (inoinfo(ino)->ino_state) {
539 
540 	case FSTATE:
541 		inoinfo(ino)->ino_state = FCLEAR;
542 		return;
543 
544 	case DSTATE:
545 		inoinfo(ino)->ino_state = DCLEAR;
546 		return;
547 
548 	case FCLEAR:
549 	case DCLEAR:
550 		return;
551 
552 	default:
553 		errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
554 		/* NOTREACHED */
555 	}
556 }
557 
558 /*
559  * allocate an unused inode
560  */
561 ufs1_ino_t
562 allocino(ufs1_ino_t request, int type)
563 {
564 	ufs1_ino_t ino;
565 	struct ufs1_dinode *dp;
566 	struct cg *cgp = &cgrp;
567 	int cg;
568 
569 	if (request == 0)
570 		request = UFS_ROOTINO;
571 	else if (inoinfo(request)->ino_state != USTATE)
572 		return (0);
573 	for (ino = request; ino < maxino; ino++)
574 		if (inoinfo(ino)->ino_state == USTATE)
575 			break;
576 	if (ino == maxino)
577 		return (0);
578 	cg = ino_to_cg(&sblock, ino);
579 	getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
580 	if (!cg_chkmagic(cgp))
581 		pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
582 	setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
583 	cgp->cg_cs.cs_nifree--;
584 	switch (type & IFMT) {
585 	case IFDIR:
586 		inoinfo(ino)->ino_state = DSTATE;
587 		cgp->cg_cs.cs_ndir++;
588 		break;
589 	case IFREG:
590 	case IFLNK:
591 		inoinfo(ino)->ino_state = FSTATE;
592 		break;
593 	default:
594 		return (0);
595 	}
596 	cgdirty();
597 	dp = ginode(ino);
598 	dp->di_db[0] = allocblk((long)1);
599 	if (dp->di_db[0] == 0) {
600 		inoinfo(ino)->ino_state = USTATE;
601 		return (0);
602 	}
603 	dp->di_mode = type;
604 	dp->di_flags = 0;
605 	dp->di_atime = time(NULL);
606 	dp->di_mtime = dp->di_ctime = dp->di_atime;
607 	dp->di_mtimensec = dp->di_ctimensec = dp->di_atimensec = 0;
608 	dp->di_size = sblock.fs_fsize;
609 	dp->di_blocks = btodb(sblock.fs_fsize);
610 	n_files++;
611 	inodirty();
612 	if (newinofmt)
613 		inoinfo(ino)->ino_type = IFTODT(type);
614 	return (ino);
615 }
616 
617 /*
618  * deallocate an inode
619  */
620 void
621 freeino(ufs1_ino_t ino)
622 {
623 	struct inodesc idesc;
624 	struct ufs1_dinode *dp;
625 
626 	memset(&idesc, 0, sizeof(struct inodesc));
627 	idesc.id_type = ADDR;
628 	idesc.id_func = pass4check;
629 	idesc.id_number = ino;
630 	dp = ginode(ino);
631 	ckinode(dp, &idesc);
632 	clearinode(dp);
633 	inodirty();
634 	inoinfo(ino)->ino_state = USTATE;
635 	n_files--;
636 }
637