2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char sccsid[] = "@(#)dir.c 8.8 (Berkeley) 4/28/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
54 static struct dirtemplate emptydir = {
55 0, DIRBLKSIZ, DT_UNKNOWN, 0, "",
56 0, 0, DT_UNKNOWN, 0, ""
58 static struct dirtemplate dirhead = {
59 0, 12, DT_DIR, 1, ".",
60 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
63 static int chgino(struct inodesc *);
64 static int dircheck(struct inodesc *, struct bufarea *, struct direct *);
65 static int expanddir(struct inode *ip, char *name);
66 static void freedir(ino_t ino, ino_t parent);
67 static struct direct *fsck_readdir(struct inodesc *);
68 static struct bufarea *getdirblk(ufs2_daddr_t blkno, long size);
69 static int lftempname(char *bufp, ino_t ino);
70 static int mkentry(struct inodesc *);
73 * Propagate connected state through the tree.
78 struct inoinfo **inpp, *inp;
79 struct inoinfo **inpend;
82 inpend = &inpsort[inplast];
85 for (inpp = inpsort; inpp < inpend; inpp++) {
87 if (inp->i_parent == 0)
89 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
90 INO_IS_DUNFOUND(inp->i_number)) {
91 inoinfo(inp->i_number)->ino_state = DFOUND;
99 * Scan each entry in a directory block.
102 dirscan(struct inodesc *idesc)
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 idesc->id_dirp = (struct direct *)dbuf;
127 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
128 bp = getdirblk(idesc->id_blkno, blksiz);
131 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
139 return (idesc->id_filesize > 0 ? KEEPON : STOP);
143 * Get and verify the next entry in a directory.
144 * We also verify that if there is another entry in the block that it is
145 * valid, so if it is not valid it can be subsumed into the current entry.
147 static struct direct *
148 fsck_readdir(struct inodesc *idesc)
150 struct direct *dp, *ndp;
152 long size, blksiz, subsume_ndp;
155 blksiz = idesc->id_numfrags * sblock.fs_fsize;
156 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
158 bp = getdirblk(idesc->id_blkno, blksiz);
161 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
163 * Only need to check current entry if it is the first in the
164 * the block, as later entries will have been checked in the
165 * previous call to this function.
167 if (idesc->id_loc % DIRBLKSIZ != 0 || dircheck(idesc, bp, dp) != 0) {
169 * Current entry is good, update to point at next.
171 idesc->id_loc += dp->d_reclen;
172 idesc->id_filesize -= dp->d_reclen;
174 * If at end of directory block, just return this entry.
176 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz ||
177 idesc->id_loc % DIRBLKSIZ == 0)
180 * If the next entry good, return this entry.
182 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
183 if (dircheck(idesc, bp, ndp) != 0)
186 * The next entry is bad, so subsume it and the remainder
187 * of this directory block into this entry.
192 * Current or next entry is bad. Zap current entry or
193 * subsume next entry into current entry as appropriate.
195 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
196 idesc->id_loc += size;
197 idesc->id_filesize -= size;
198 if (idesc->id_fix == IGNORE)
201 memset(ndp, 0, size);
202 dp->d_reclen += size;
207 if (dofix(idesc, "DIRECTORY CORRUPTED"))
213 * Verify that a directory entry is valid.
214 * This is a superset of the checks made in the kernel.
215 * Also optionally clears padding and unused directory space.
217 * Returns 0 if the entry is bad, 1 if the entry is good.
220 dircheck(struct inodesc *idesc, struct bufarea *bp, struct direct *dp)
225 int spaceleft, modified, unused;
227 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
228 size = DIRSIZ(0, dp);
229 if (dp->d_reclen == 0 ||
230 dp->d_reclen > spaceleft ||
231 dp->d_reclen < size ||
232 idesc->id_filesize < size ||
233 (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
236 if (dp->d_ino == 0) {
237 if (!zflag || fswritefd < 0)
240 * Special case of an unused directory entry. Normally only
241 * occurs at the beginning of a directory block when the block
242 * contains no entries. Other than the first entry in a
243 * directory block, the kernel coalesces unused space with
244 * the previous entry by extending its d_reclen. However,
245 * when cleaning up a directory, fsck may set d_ino to zero
246 * in the middle of a directory block. If we're clearing out
247 * directory cruft (-z flag), then make sure that all directory
248 * space in entries with d_ino == 0 gets fully cleared.
250 if (dp->d_type != 0) {
254 if (dp->d_namlen != 0) {
258 unused = dp->d_reclen - __offsetof(struct direct, d_name);
259 for (cp = dp->d_name; unused > 0; unused--, cp++) {
270 * The d_type field should not be tested here. A bad type is an error
271 * in the entry itself but is not a corruption of the directory
272 * structure itself. So blowing away all the remaining entries in the
273 * directory block is inappropriate. Rather the type error should be
274 * checked in pass1 and fixed there.
276 * The name validation should also be done in pass1 although the
277 * check to see if the name is longer than fits in the space
278 * allocated for it (i.e., the *cp != '\0' fails after exiting the
279 * loop below) then it really is a structural error that requires
280 * the stronger action taken here.
282 namlen = dp->d_namlen;
283 if (namlen == 0 || dp->d_type > 15)
285 for (cp = dp->d_name, size = 0; size < namlen; size++) {
286 if (*cp == '\0' || *cp++ == '/')
291 if (zflag && fswritefd >= 0) {
293 * Clear unused directory entry space, including the d_name
296 /* First figure the number of pad bytes. */
297 unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
299 /* Add in the free space to the end of the record. */
300 unused += dp->d_reclen - DIRSIZ(0, dp);
303 * Now clear out the unused space, keeping track if we actually
306 for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
320 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
321 dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
327 direrror(ino_t ino, const char *errmesg)
330 fileerror(ino, ino, errmesg);
334 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
338 char pathbuf[MAXPATHLEN + 1];
340 pwarn("%s ", errmesg);
341 if (ino < UFS_ROOTINO || ino > maxino) {
342 pfatal("out-of-range inode number %ju", (uintmax_t)ino);
349 getpathname(pathbuf, cwd, ino);
352 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
355 pfatal("NAME=%s\n", pathbuf);
360 adjust(struct inodesc *idesc, int lcnt)
366 ginode(idesc->id_number, &ip);
368 if (DIP(dp, di_nlink) == lcnt) {
370 * If we have not hit any unresolved problems, are running
371 * in preen mode, and are on a file system using soft updates,
372 * then just toss any partially allocated files.
374 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
375 clri(idesc, "UNREF", 1);
380 * The file system can be marked clean even if
381 * a file is not linked up, but is cleared.
382 * Hence, resolved should not be cleared when
383 * linkup is answered no, but clri is answered yes.
385 saveresolved = resolved;
386 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
387 resolved = saveresolved;
388 clri(idesc, "UNREF", 0);
393 * Account for the new reference created by linkup().
399 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
400 ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
402 printf(" COUNT %d SHOULD BE %d",
403 DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
404 if (preen || usedsoftdep) {
407 pfatal("LINK COUNT INCREASING");
410 printf(" (ADJUSTED)\n");
412 if (preen || reply("ADJUST") == 1) {
413 if (bkgrdflag == 0) {
414 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
417 cmd.value = idesc->id_number;
420 printf("adjrefcnt ino %ld amt %lld\n",
422 (long long)cmd.size);
423 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
424 &cmd, sizeof cmd) == -1)
425 rwerror("ADJUST INODE", cmd.value);
433 mkentry(struct inodesc *idesc)
435 struct direct *dirp = idesc->id_dirp;
436 struct direct newent;
439 newent.d_namlen = strlen(idesc->id_name);
440 newlen = DIRSIZ(0, &newent);
441 if (dirp->d_ino != 0)
442 oldlen = DIRSIZ(0, dirp);
445 if (dirp->d_reclen - oldlen < newlen)
447 newent.d_reclen = dirp->d_reclen - oldlen;
448 dirp->d_reclen = oldlen;
449 dirp = (struct direct *)(((char *)dirp) + oldlen);
450 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
451 dirp->d_reclen = newent.d_reclen;
452 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
453 dirp->d_namlen = newent.d_namlen;
454 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
455 return (ALTERED|STOP);
459 chgino(struct inodesc *idesc)
461 struct direct *dirp = idesc->id_dirp;
463 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
465 dirp->d_ino = idesc->id_parent;
466 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
467 return (ALTERED|STOP);
471 linkup(ino_t orphan, ino_t parentdir, char *name)
477 struct inodesc idesc;
478 char tempname[BUFSIZ];
480 memset(&idesc, 0, sizeof(struct inodesc));
483 lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
484 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
487 if (preen && DIP(dp, di_size) == 0) {
492 if (cursnapshot != 0) {
493 pfatal("FILE LINKUP IN SNAPSHOT");
497 printf(" (RECONNECTED)\n");
498 else if (reply("RECONNECT") == 0)
501 ginode(UFS_ROOTINO, &ip);
502 idesc.id_name = strdup(lfname);
503 idesc.id_type = DATA;
504 idesc.id_func = findino;
505 idesc.id_number = UFS_ROOTINO;
506 if ((ckinode(ip.i_dp, &idesc) & FOUND) != 0) {
507 lfdir = idesc.id_parent;
509 pwarn("NO lost+found DIRECTORY");
510 if (preen || reply("CREATE")) {
511 lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode);
513 if (makeentry(UFS_ROOTINO, lfdir,
517 printf(" (CREATED)\n");
519 freedir(lfdir, UFS_ROOTINO);
529 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
536 if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
537 pfatal("lost+found IS NOT A DIRECTORY");
538 if (reply("REALLOCATE") == 0) {
543 if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) {
544 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
548 if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
549 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
553 idesc.id_type = inoinfo(oldlfdir)->ino_idtype;
554 idesc.id_func = freeblock;
555 idesc.id_number = oldlfdir;
556 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
557 inoinfo(oldlfdir)->ino_linkcnt = 0;
563 if (inoinfo(lfdir)->ino_state != DFOUND) {
564 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
568 (void)lftempname(tempname, orphan);
569 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
570 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
575 inoinfo(orphan)->ino_linkcnt--;
577 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
578 parentdir != (ino_t)-1)
579 (void)makeentry(orphan, lfdir, "..");
580 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
582 inoinfo(lfdir)->ino_linkcnt++;
583 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
584 if (parentdir != (ino_t)-1) {
585 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
587 * The parent directory, because of the ordering
588 * guarantees, has had the link count incremented
589 * for the child, but no entry was made. This
590 * fixes the parent link count so that fsck does
591 * not need to be rerun.
593 inoinfo(parentdir)->ino_linkcnt++;
603 * fix an entry in a directory.
606 changeino(ino_t dir, const char *name, ino_t newnum)
608 struct inodesc idesc;
612 memset(&idesc, 0, sizeof(struct inodesc));
613 idesc.id_type = DATA;
614 idesc.id_func = chgino;
615 idesc.id_number = dir;
616 idesc.id_fix = DONTKNOW;
617 idesc.id_name = strdup(name);
618 idesc.id_parent = newnum; /* new value for name */
620 error = ckinode(ip.i_dp, &idesc);
626 * make an entry in a directory
629 makeentry(ino_t parent, ino_t ino, const char *name)
633 struct inodesc idesc;
635 char pathbuf[MAXPATHLEN + 1];
637 if (parent < UFS_ROOTINO || parent >= maxino ||
638 ino < UFS_ROOTINO || ino >= maxino)
640 memset(&idesc, 0, sizeof(struct inodesc));
641 idesc.id_type = DATA;
642 idesc.id_func = mkentry;
643 idesc.id_number = parent;
644 idesc.id_parent = ino; /* this is the inode to enter */
645 idesc.id_fix = DONTKNOW;
646 idesc.id_name = strdup(name);
649 if (DIP(dp, di_size) % DIRBLKSIZ) {
650 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
653 if ((ckinode(dp, &idesc) & ALTERED) != 0) {
657 getpathname(pathbuf, parent, parent);
658 if (expanddir(&ip, pathbuf) == 0) {
662 retval = ckinode(dp, &idesc) & ALTERED;
668 * Attempt to expand the size of a directory
671 expanddir(struct inode *ip, char *name)
673 ufs2_daddr_t lastlbn, oldblk, newblk, indirblk;
674 size_t filesize, lastlbnsize;
675 struct bufarea *bp, *nbp;
676 struct inodesc idesc;
682 indiralloced = newblk = indirblk = 0;
683 pwarn("NO SPACE LEFT IN %s", name);
684 if (!preen && reply("EXPAND") == 0)
687 filesize = DIP(dp, di_size);
688 lastlbn = lblkno(&sblock, filesize);
690 * We only expand lost+found to a single indirect block.
692 if ((DIP(dp, di_mode) & IFMT) != IFDIR || filesize == 0 ||
693 lastlbn >= UFS_NDADDR + NINDIR(&sblock))
696 * If last block is a fragment, expand it to a full size block.
698 lastlbnsize = sblksize(&sblock, filesize, lastlbn);
699 if (lastlbnsize > 0 && lastlbnsize < sblock.fs_bsize) {
700 oldblk = DIP(dp, di_db[lastlbn]);
701 bp = getdirblk(oldblk, lastlbnsize);
704 if ((newblk = allocblk(sblock.fs_frag)) == 0)
706 nbp = getdatablk(newblk, sblock.fs_bsize, BT_DIRDATA);
709 DIP_SET(dp, di_db[lastlbn], newblk);
710 DIP_SET(dp, di_size, filesize + sblock.fs_bsize - lastlbnsize);
711 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) +
712 btodb(sblock.fs_bsize - lastlbnsize));
714 memmove(nbp->b_un.b_buf, bp->b_un.b_buf, lastlbnsize);
715 memset(&nbp->b_un.b_buf[lastlbnsize], 0,
716 sblock.fs_bsize - lastlbnsize);
717 for (cp = &nbp->b_un.b_buf[lastlbnsize];
718 cp < &nbp->b_un.b_buf[sblock.fs_bsize];
720 memmove(cp, &emptydir, sizeof emptydir);
723 idesc.id_blkno = oldblk;
724 idesc.id_numfrags = numfrags(&sblock, lastlbnsize);
725 (void)freeblock(&idesc);
727 printf(" (EXPANDED)\n");
730 if ((newblk = allocblk(sblock.fs_frag)) == 0)
732 bp = getdirblk(newblk, sblock.fs_bsize);
735 memset(bp->b_un.b_buf, 0, sblock.fs_bsize);
736 for (cp = bp->b_un.b_buf;
737 cp < &bp->b_un.b_buf[sblock.fs_bsize];
739 memmove(cp, &emptydir, sizeof emptydir);
741 if (lastlbn < UFS_NDADDR) {
742 DIP_SET(dp, di_db[lastlbn], newblk);
745 * Allocate indirect block if needed.
747 if ((indirblk = DIP(dp, di_ib[0])) == 0) {
748 if ((indirblk = allocblk(sblock.fs_frag)) == 0)
752 nbp = getdatablk(indirblk, sblock.fs_bsize, BT_LEVEL1);
756 memset(nbp->b_un.b_buf, 0, sblock.fs_bsize);
757 DIP_SET(dp, di_ib[0], indirblk);
758 DIP_SET(dp, di_blocks,
759 DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
761 IBLK_SET(nbp, lastlbn - UFS_NDADDR, newblk);
765 DIP_SET(dp, di_size, filesize + sblock.fs_bsize);
766 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
769 printf(" (EXPANDED)\n");
772 pfatal(" (EXPANSION FAILED)\n");
776 idesc.id_blkno = newblk;
777 idesc.id_numfrags = sblock.fs_frag;
778 (void)freeblock(&idesc);
781 idesc.id_blkno = indirblk;
782 idesc.id_numfrags = sblock.fs_frag;
783 (void)freeblock(&idesc);
789 * allocate a new directory
792 allocdir(ino_t parent, ino_t request, int mode)
800 struct dirtemplate *dirp;
802 ino = allocino(request, IFDIR|mode);
807 dirp->dotdot_ino = parent;
810 bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
816 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
817 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
818 cp < &bp->b_un.b_buf[sblock.fs_fsize];
820 memmove(cp, &emptydir, sizeof emptydir);
822 DIP_SET(dp, di_nlink, 2);
824 if (ino == UFS_ROOTINO) {
825 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
830 if (!INO_IS_DVALID(parent)) {
836 inp = getinoinfo(ino);
837 inp->i_parent = parent;
838 inp->i_dotdot = parent;
839 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
840 if (inoinfo(ino)->ino_state == DSTATE) {
841 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
842 inoinfo(parent)->ino_linkcnt++;
847 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
854 * free a directory inode
857 freedir(ino_t ino, ino_t parent)
865 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
873 * generate a temporary name for the lost+found directory.
876 lftempname(char *bufp, ino_t ino)
883 for (in = maxino; in > 0; in /= 10)
889 *--cp = (in % 10) + '0';
897 * Get a directory block.
898 * Insure that it is held until another is requested.
900 static struct bufarea *
901 getdirblk(ufs2_daddr_t blkno, long size)
904 if (pdirbp != NULL && pdirbp->b_errs == 0)
906 pdirbp = getdatablk(blkno, size, BT_DIRDATA);