2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)dir.c 8.8 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/fsck/dir.c,v 1.15 1999/08/28 00:12:45 peter Exp $
35 * $DragonFly: src/sbin/fsck/dir.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
38 #include <sys/param.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <ufs/ffs/fs.h>
50 char *lfname = "lost+found";
52 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
53 struct dirtemplate dirhead = {
54 0, 12, DT_DIR, 1, ".",
55 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
57 struct odirtemplate odirhead = {
59 0, DIRBLKSIZ - 12, 2, ".."
62 static int chgino __P((struct inodesc *));
63 static int dircheck __P((struct inodesc *, struct direct *));
64 static int expanddir __P((struct dinode *dp, char *name));
65 static void freedir __P((ino_t ino, ino_t parent));
66 static struct direct *fsck_readdir __P((struct inodesc *));
67 static struct bufarea *getdirblk __P((ufs_daddr_t blkno, long size));
68 static int lftempname __P((char *bufp, ino_t ino));
69 static int mkentry __P((struct inodesc *));
72 * Propagate connected state through the tree.
77 register struct inoinfo **inpp, *inp;
78 struct inoinfo **inpend;
81 inpend = &inpsort[inplast];
84 for (inpp = inpsort; inpp < inpend; inpp++) {
86 if (inp->i_parent == 0)
88 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
89 inoinfo(inp->i_number)->ino_state == DSTATE) {
90 inoinfo(inp->i_number)->ino_state = DFOUND;
98 * Scan each entry in a directory block.
102 register struct inodesc *idesc;
104 register struct direct *dp;
105 register struct bufarea *bp;
108 char dbuf[DIRBLKSIZ];
110 if (idesc->id_type != DATA)
111 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
112 if (idesc->id_entryno == 0 &&
113 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
114 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
115 blksiz = idesc->id_numfrags * sblock.fs_fsize;
116 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
117 idesc->id_filesize -= blksiz;
121 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
122 dsize = dp->d_reclen;
123 if (dsize > sizeof(dbuf))
124 dsize = sizeof(dbuf);
125 memmove(dbuf, dp, (size_t)dsize);
126 # if (BYTE_ORDER == LITTLE_ENDIAN)
128 struct direct *tdp = (struct direct *)dbuf;
132 tdp->d_namlen = tdp->d_type;
136 idesc->id_dirp = (struct direct *)dbuf;
137 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
138 # if (BYTE_ORDER == LITTLE_ENDIAN)
139 if (!newinofmt && !doinglevel2) {
143 tdp = (struct direct *)dbuf;
145 tdp->d_namlen = tdp->d_type;
149 bp = getdirblk(idesc->id_blkno, blksiz);
150 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
158 return (idesc->id_filesize > 0 ? KEEPON : STOP);
162 * get next entry in a directory.
164 static struct direct *
166 register struct inodesc *idesc;
168 register struct direct *dp, *ndp;
169 register struct bufarea *bp;
170 long size, blksiz, fix, dploc;
172 blksiz = idesc->id_numfrags * sblock.fs_fsize;
173 bp = getdirblk(idesc->id_blkno, blksiz);
174 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
175 idesc->id_loc < blksiz) {
176 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
177 if (dircheck(idesc, dp))
179 if (idesc->id_fix == IGNORE)
181 fix = dofix(idesc, "DIRECTORY CORRUPTED");
182 bp = getdirblk(idesc->id_blkno, blksiz);
183 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
184 dp->d_reclen = DIRBLKSIZ;
188 dp->d_name[0] = '\0';
191 idesc->id_loc += DIRBLKSIZ;
192 idesc->id_filesize -= DIRBLKSIZ;
196 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
198 dploc = idesc->id_loc;
199 dp = (struct direct *)(bp->b_un.b_buf + dploc);
200 idesc->id_loc += dp->d_reclen;
201 idesc->id_filesize -= dp->d_reclen;
202 if ((idesc->id_loc % DIRBLKSIZ) == 0)
204 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
205 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
206 dircheck(idesc, ndp) == 0) {
207 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
208 idesc->id_loc += size;
209 idesc->id_filesize -= size;
210 if (idesc->id_fix == IGNORE)
212 fix = dofix(idesc, "DIRECTORY CORRUPTED");
213 bp = getdirblk(idesc->id_blkno, blksiz);
214 dp = (struct direct *)(bp->b_un.b_buf + dploc);
215 dp->d_reclen += size;
223 * Verify that a directory entry is valid.
224 * This is a superset of the checks made in the kernel.
228 struct inodesc *idesc;
229 register struct direct *dp;
236 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
237 if (dp->d_reclen == 0 ||
238 dp->d_reclen > spaceleft ||
239 (dp->d_reclen & 0x3) != 0)
243 size = DIRSIZ(!newinofmt, dp);
244 # if (BYTE_ORDER == LITTLE_ENDIAN)
249 namlen = dp->d_namlen;
253 namlen = dp->d_namlen;
256 if (dp->d_reclen < size ||
257 idesc->id_filesize < size ||
258 namlen > MAXNAMLEN ||
261 for (cp = dp->d_name, size = 0; size < namlen; size++)
262 if (*cp == '\0' || (*cp++ == '/'))
270 direrror(ino, errmesg)
275 fileerror(ino, ino, errmesg);
279 fileerror(cwd, ino, errmesg)
283 register struct dinode *dp;
284 char pathbuf[MAXPATHLEN + 1];
286 pwarn("%s ", errmesg);
289 getpathname(pathbuf, cwd, ino);
290 if (ino < ROOTINO || ino > maxino) {
291 pfatal("NAME=%s\n", pathbuf);
297 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
299 pfatal("NAME=%s\n", pathbuf);
304 register struct inodesc *idesc;
310 dp = ginode(idesc->id_number);
311 if (dp->di_nlink == lcnt) {
313 * If we have not hit any unresolved problems, are running
314 * in preen mode, and are on a filesystem using soft updates,
315 * then just toss any partially allocated files.
317 if (resolved && preen && usedsoftdep) {
318 clri(idesc, "UNREF", 1);
322 * The filesystem can be marked clean even if
323 * a file is not linked up, but is cleared.
324 * Hence, resolved should not be cleared when
325 * linkup is answered no, but clri is answered yes.
327 saveresolved = resolved;
328 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
329 resolved = saveresolved;
330 clri(idesc, "UNREF", 0);
334 * Account for the new reference created by linkup().
336 dp = ginode(idesc->id_number);
341 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
342 ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
343 pinode(idesc->id_number);
344 printf(" COUNT %d SHOULD BE %d",
345 dp->di_nlink, dp->di_nlink - lcnt);
346 if (preen || usedsoftdep) {
349 pfatal("LINK COUNT INCREASING");
352 printf(" (ADJUSTED)\n");
354 if (preen || reply("ADJUST") == 1) {
355 dp->di_nlink -= lcnt;
363 struct inodesc *idesc;
365 register struct direct *dirp = idesc->id_dirp;
366 struct direct newent;
369 newent.d_namlen = strlen(idesc->id_name);
370 newlen = DIRSIZ(0, &newent);
371 if (dirp->d_ino != 0)
372 oldlen = DIRSIZ(0, dirp);
375 if (dirp->d_reclen - oldlen < newlen)
377 newent.d_reclen = dirp->d_reclen - oldlen;
378 dirp->d_reclen = oldlen;
379 dirp = (struct direct *)(((char *)dirp) + oldlen);
380 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
381 dirp->d_reclen = newent.d_reclen;
383 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
386 dirp->d_namlen = newent.d_namlen;
387 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
388 # if (BYTE_ORDER == LITTLE_ENDIAN)
390 * If the entry was split, dirscan() will only reverse the byte
391 * order of the original entry, and not the new one, before
392 * writing it back out. So, we reverse the byte order here if
395 if (oldlen != 0 && !newinofmt && !doinglevel2) {
398 tmp = dirp->d_namlen;
399 dirp->d_namlen = dirp->d_type;
403 return (ALTERED|STOP);
408 struct inodesc *idesc;
410 register struct direct *dirp = idesc->id_dirp;
412 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
414 dirp->d_ino = idesc->id_parent;
416 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
419 return (ALTERED|STOP);
423 linkup(orphan, parentdir, name)
428 register struct dinode *dp;
431 struct inodesc idesc;
432 char tempname[BUFSIZ];
434 memset(&idesc, 0, sizeof(struct inodesc));
436 lostdir = (dp->di_mode & IFMT) == IFDIR;
437 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
439 if (preen && dp->di_size == 0)
442 printf(" (RECONNECTED)\n");
444 if (reply("RECONNECT") == 0)
447 dp = ginode(ROOTINO);
448 idesc.id_name = lfname;
449 idesc.id_type = DATA;
450 idesc.id_func = findino;
451 idesc.id_number = ROOTINO;
452 if ((ckinode(dp, &idesc) & FOUND) != 0) {
453 lfdir = idesc.id_parent;
455 pwarn("NO lost+found DIRECTORY");
456 if (preen || reply("CREATE")) {
457 lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
459 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
462 printf(" (CREATED)\n");
464 freedir(lfdir, ROOTINO);
473 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
479 if ((dp->di_mode & IFMT) != IFDIR) {
480 pfatal("lost+found IS NOT A DIRECTORY");
481 if (reply("REALLOCATE") == 0)
484 if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
485 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
488 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
489 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
493 idesc.id_type = ADDR;
494 idesc.id_func = pass4check;
495 idesc.id_number = oldlfdir;
496 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
497 inoinfo(oldlfdir)->ino_linkcnt = 0;
500 if (inoinfo(lfdir)->ino_state != DFOUND) {
501 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
504 (void)lftempname(tempname, orphan);
505 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
506 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
510 inoinfo(orphan)->ino_linkcnt--;
512 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
513 parentdir != (ino_t)-1)
514 (void)makeentry(orphan, lfdir, "..");
518 inoinfo(lfdir)->ino_linkcnt++;
519 pwarn("DIR I=%lu CONNECTED. ", orphan);
520 if (parentdir != (ino_t)-1) {
521 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
523 * The parent directory, because of the ordering
524 * guarantees, has had the link count incremented
525 * for the child, but no entry was made. This
526 * fixes the parent link count so that fsck does
527 * not need to be rerun.
529 inoinfo(parentdir)->ino_linkcnt++;
538 * fix an entry in a directory.
541 changeino(dir, name, newnum)
546 struct inodesc idesc;
548 memset(&idesc, 0, sizeof(struct inodesc));
549 idesc.id_type = DATA;
550 idesc.id_func = chgino;
551 idesc.id_number = dir;
552 idesc.id_fix = DONTKNOW;
553 idesc.id_name = name;
554 idesc.id_parent = newnum; /* new value for name */
555 return (ckinode(ginode(dir), &idesc));
559 * make an entry in a directory
562 makeentry(parent, ino, name)
567 struct inodesc idesc;
568 char pathbuf[MAXPATHLEN + 1];
570 if (parent < ROOTINO || parent >= maxino ||
571 ino < ROOTINO || ino >= maxino)
573 memset(&idesc, 0, sizeof(struct inodesc));
574 idesc.id_type = DATA;
575 idesc.id_func = mkentry;
576 idesc.id_number = parent;
577 idesc.id_parent = ino; /* this is the inode to enter */
578 idesc.id_fix = DONTKNOW;
579 idesc.id_name = name;
581 if (dp->di_size % DIRBLKSIZ) {
582 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
585 if ((ckinode(dp, &idesc) & ALTERED) != 0)
587 getpathname(pathbuf, parent, parent);
589 if (expanddir(dp, pathbuf) == 0)
591 return (ckinode(dp, &idesc) & ALTERED);
595 * Attempt to expand the size of a directory
599 register struct dinode *dp;
602 ufs_daddr_t lastbn, newblk;
603 register struct bufarea *bp;
604 char *cp, firstblk[DIRBLKSIZ];
606 lastbn = lblkno(&sblock, dp->di_size);
607 if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
609 if ((newblk = allocblk(sblock.fs_frag)) == 0)
611 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
612 dp->di_db[lastbn] = newblk;
613 dp->di_size += sblock.fs_bsize;
614 dp->di_blocks += btodb(sblock.fs_bsize);
615 bp = getdirblk(dp->di_db[lastbn + 1],
616 (long)dblksize(&sblock, dp, lastbn + 1));
619 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
620 bp = getdirblk(newblk, sblock.fs_bsize);
623 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
624 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
625 cp < &bp->b_un.b_buf[sblock.fs_bsize];
627 memmove(cp, &emptydir, sizeof emptydir);
629 bp = getdirblk(dp->di_db[lastbn + 1],
630 (long)dblksize(&sblock, dp, lastbn + 1));
633 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
634 pwarn("NO SPACE LEFT IN %s", name);
636 printf(" (EXPANDED)\n");
637 else if (reply("EXPAND") == 0)
643 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
644 dp->di_db[lastbn + 1] = 0;
645 dp->di_size -= sblock.fs_bsize;
646 dp->di_blocks -= btodb(sblock.fs_bsize);
647 freeblk(newblk, sblock.fs_frag);
652 * allocate a new directory
655 allocdir(parent, request, mode)
656 ino_t parent, request;
662 register struct bufarea *bp;
663 struct dirtemplate *dirp;
665 ino = allocino(request, IFDIR|mode);
669 dirp = (struct dirtemplate *)&odirhead;
671 dirp->dotdot_ino = parent;
673 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
678 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
679 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
680 cp < &bp->b_un.b_buf[sblock.fs_fsize];
682 memmove(cp, &emptydir, sizeof emptydir);
686 if (ino == ROOTINO) {
687 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
691 if (inoinfo(parent)->ino_state != DSTATE &&
692 inoinfo(parent)->ino_state != DFOUND) {
697 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
698 if (inoinfo(ino)->ino_state == DSTATE) {
699 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
700 inoinfo(parent)->ino_linkcnt++;
709 * free a directory inode
726 * generate a temporary name for the lost+found directory.
729 lftempname(bufp, ino)
738 for (in = maxino; in > 0; in /= 10)
744 *--cp = (in % 10) + '0';
752 * Get a directory block.
753 * Insure that it is held until another is requested.
755 static struct bufarea *
756 getdirblk(blkno, size)
762 pdirbp->b_flags &= ~B_INUSE;
763 pdirbp = getdatablk(blkno, size);