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(union dinode *dp, 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);
129 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
138 return (idesc->id_filesize > 0 ? KEEPON : STOP);
142 * Get and verify the next entry in a directory.
143 * We also verify that if there is another entry in the block that it is
144 * valid, so if it is not valid it can be subsumed into the current entry.
146 static struct direct *
147 fsck_readdir(struct inodesc *idesc)
149 struct direct *dp, *ndp;
151 long size, blksiz, subsume_ndp;
154 blksiz = idesc->id_numfrags * sblock.fs_fsize;
155 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
157 bp = getdirblk(idesc->id_blkno, blksiz);
158 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
160 * Only need to check current entry if it is the first in the
161 * the block, as later entries will have been checked in the
162 * previous call to this function.
164 if (idesc->id_loc % DIRBLKSIZ != 0 || dircheck(idesc, bp, dp) != 0) {
166 * Current entry is good, update to point at next.
168 idesc->id_loc += dp->d_reclen;
169 idesc->id_filesize -= dp->d_reclen;
171 * If at end of directory block, just return this entry.
173 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz ||
174 idesc->id_loc % DIRBLKSIZ == 0)
177 * If the next entry good, return this entry.
179 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
180 if (dircheck(idesc, bp, ndp) != 0)
183 * The next entry is bad, so subsume it and the remainder
184 * of this directory block into this entry.
189 * Current or next entry is bad. Zap current entry or
190 * subsume next entry into current entry as appropriate.
192 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
193 idesc->id_loc += size;
194 idesc->id_filesize -= size;
195 if (idesc->id_fix == IGNORE)
198 memset(ndp, 0, size);
199 dp->d_reclen += size;
204 if (dofix(idesc, "DIRECTORY CORRUPTED"))
210 * Verify that a directory entry is valid.
211 * This is a superset of the checks made in the kernel.
212 * Also optionally clears padding and unused directory space.
214 * Returns 0 if the entry is bad, 1 if the entry is good.
217 dircheck(struct inodesc *idesc, struct bufarea *bp, struct direct *dp)
222 int spaceleft, modified, unused;
224 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
225 size = DIRSIZ(0, dp);
226 if (dp->d_reclen == 0 ||
227 dp->d_reclen > spaceleft ||
228 dp->d_reclen < size ||
229 idesc->id_filesize < size ||
230 (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
233 if (dp->d_ino == 0) {
234 if (!zflag || fswritefd < 0)
237 * Special case of an unused directory entry. Normally only
238 * occurs at the beginning of a directory block when the block
239 * contains no entries. Other than the first entry in a
240 * directory block, the kernel coalesces unused space with
241 * the previous entry by extending its d_reclen. However,
242 * when cleaning up a directory, fsck may set d_ino to zero
243 * in the middle of a directory block. If we're clearing out
244 * directory cruft (-z flag), then make sure that all directory
245 * space in entries with d_ino == 0 gets fully cleared.
247 if (dp->d_type != 0) {
251 if (dp->d_namlen != 0) {
255 unused = dp->d_reclen - __offsetof(struct direct, d_name);
256 for (cp = dp->d_name; unused > 0; unused--, cp++) {
267 * The d_type field should not be tested here. A bad type is an error
268 * in the entry itself but is not a corruption of the directory
269 * structure itself. So blowing away all the remaining entries in the
270 * directory block is inappropriate. Rather the type error should be
271 * checked in pass1 and fixed there.
273 * The name validation should also be done in pass1 although the
274 * check to see if the name is longer than fits in the space
275 * allocated for it (i.e., the *cp != '\0' fails after exiting the
276 * loop below) then it really is a structural error that requires
277 * the stronger action taken here.
279 namlen = dp->d_namlen;
280 if (namlen == 0 || dp->d_type > 15)
282 for (cp = dp->d_name, size = 0; size < namlen; size++) {
283 if (*cp == '\0' || *cp++ == '/')
288 if (zflag && fswritefd >= 0) {
290 * Clear unused directory entry space, including the d_name
293 /* First figure the number of pad bytes. */
294 unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
296 /* Add in the free space to the end of the record. */
297 unused += dp->d_reclen - DIRSIZ(0, dp);
300 * Now clear out the unused space, keeping track if we actually
303 for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
317 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
318 dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
324 direrror(ino_t ino, const char *errmesg)
327 fileerror(ino, ino, errmesg);
331 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
334 char pathbuf[MAXPATHLEN + 1];
336 pwarn("%s ", errmesg);
337 if (ino < UFS_ROOTINO || ino > maxino) {
338 pfatal("out-of-range inode number %ju", (uintmax_t)ino);
344 getpathname(pathbuf, cwd, ino);
347 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
350 pfatal("NAME=%s\n", pathbuf);
354 adjust(struct inodesc *idesc, int lcnt)
359 dp = ginode(idesc->id_number);
360 if (DIP(dp, di_nlink) == lcnt) {
362 * If we have not hit any unresolved problems, are running
363 * in preen mode, and are on a file system using soft updates,
364 * then just toss any partially allocated files.
366 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
367 clri(idesc, "UNREF", 1);
371 * The file system can be marked clean even if
372 * a file is not linked up, but is cleared.
373 * Hence, resolved should not be cleared when
374 * linkup is answered no, but clri is answered yes.
376 saveresolved = resolved;
377 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
378 resolved = saveresolved;
379 clri(idesc, "UNREF", 0);
383 * Account for the new reference created by linkup().
385 dp = ginode(idesc->id_number);
390 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
391 ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
392 prtinode(idesc->id_number, dp);
393 printf(" COUNT %d SHOULD BE %d",
394 DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
395 if (preen || usedsoftdep) {
398 pfatal("LINK COUNT INCREASING");
401 printf(" (ADJUSTED)\n");
403 if (preen || reply("ADJUST") == 1) {
404 if (bkgrdflag == 0) {
405 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
408 cmd.value = idesc->id_number;
411 printf("adjrefcnt ino %ld amt %lld\n",
413 (long long)cmd.size);
414 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
415 &cmd, sizeof cmd) == -1)
416 rwerror("ADJUST INODE", cmd.value);
423 mkentry(struct inodesc *idesc)
425 struct direct *dirp = idesc->id_dirp;
426 struct direct newent;
429 newent.d_namlen = strlen(idesc->id_name);
430 newlen = DIRSIZ(0, &newent);
431 if (dirp->d_ino != 0)
432 oldlen = DIRSIZ(0, dirp);
435 if (dirp->d_reclen - oldlen < newlen)
437 newent.d_reclen = dirp->d_reclen - oldlen;
438 dirp->d_reclen = oldlen;
439 dirp = (struct direct *)(((char *)dirp) + oldlen);
440 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
441 dirp->d_reclen = newent.d_reclen;
442 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
443 dirp->d_namlen = newent.d_namlen;
444 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
445 return (ALTERED|STOP);
449 chgino(struct inodesc *idesc)
451 struct direct *dirp = idesc->id_dirp;
453 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
455 dirp->d_ino = idesc->id_parent;
456 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
457 return (ALTERED|STOP);
461 linkup(ino_t orphan, ino_t parentdir, char *name)
466 struct inodesc idesc;
467 char tempname[BUFSIZ];
469 memset(&idesc, 0, sizeof(struct inodesc));
471 lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
472 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
473 prtinode(orphan, dp);
475 if (preen && DIP(dp, di_size) == 0)
477 if (cursnapshot != 0) {
478 pfatal("FILE LINKUP IN SNAPSHOT");
482 printf(" (RECONNECTED)\n");
484 if (reply("RECONNECT") == 0)
487 dp = ginode(UFS_ROOTINO);
488 idesc.id_name = strdup(lfname);
489 idesc.id_type = DATA;
490 idesc.id_func = findino;
491 idesc.id_number = UFS_ROOTINO;
492 if ((ckinode(dp, &idesc) & FOUND) != 0) {
493 lfdir = idesc.id_parent;
495 pwarn("NO lost+found DIRECTORY");
496 if (preen || reply("CREATE")) {
497 lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode);
499 if (makeentry(UFS_ROOTINO, lfdir,
503 printf(" (CREATED)\n");
505 freedir(lfdir, UFS_ROOTINO);
514 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
520 if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
521 pfatal("lost+found IS NOT A DIRECTORY");
522 if (reply("REALLOCATE") == 0)
525 if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) {
526 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
529 if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
530 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
534 idesc.id_type = ADDR;
535 idesc.id_func = pass4check;
536 idesc.id_number = oldlfdir;
537 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
538 inoinfo(oldlfdir)->ino_linkcnt = 0;
541 if (inoinfo(lfdir)->ino_state != DFOUND) {
542 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
545 (void)lftempname(tempname, orphan);
546 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
547 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
551 inoinfo(orphan)->ino_linkcnt--;
553 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
554 parentdir != (ino_t)-1)
555 (void)makeentry(orphan, lfdir, "..");
557 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
559 inoinfo(lfdir)->ino_linkcnt++;
560 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
561 if (parentdir != (ino_t)-1) {
562 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
564 * The parent directory, because of the ordering
565 * guarantees, has had the link count incremented
566 * for the child, but no entry was made. This
567 * fixes the parent link count so that fsck does
568 * not need to be rerun.
570 inoinfo(parentdir)->ino_linkcnt++;
579 * fix an entry in a directory.
582 changeino(ino_t dir, const char *name, ino_t newnum)
584 struct inodesc idesc;
586 memset(&idesc, 0, sizeof(struct inodesc));
587 idesc.id_type = DATA;
588 idesc.id_func = chgino;
589 idesc.id_number = dir;
590 idesc.id_fix = DONTKNOW;
591 idesc.id_name = strdup(name);
592 idesc.id_parent = newnum; /* new value for name */
593 return (ckinode(ginode(dir), &idesc));
597 * make an entry in a directory
600 makeentry(ino_t parent, ino_t ino, const char *name)
603 struct inodesc idesc;
604 char pathbuf[MAXPATHLEN + 1];
606 if (parent < UFS_ROOTINO || parent >= maxino ||
607 ino < UFS_ROOTINO || ino >= maxino)
609 memset(&idesc, 0, sizeof(struct inodesc));
610 idesc.id_type = DATA;
611 idesc.id_func = mkentry;
612 idesc.id_number = parent;
613 idesc.id_parent = ino; /* this is the inode to enter */
614 idesc.id_fix = DONTKNOW;
615 idesc.id_name = strdup(name);
617 if (DIP(dp, di_size) % DIRBLKSIZ) {
618 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
621 if ((ckinode(dp, &idesc) & ALTERED) != 0)
623 getpathname(pathbuf, parent, parent);
625 if (expanddir(dp, pathbuf) == 0)
627 return (ckinode(dp, &idesc) & ALTERED);
631 * Attempt to expand the size of a directory
634 expanddir(union dinode *dp, char *name)
636 ufs2_daddr_t lastbn, newblk;
638 char *cp, firstblk[DIRBLKSIZ];
640 lastbn = lblkno(&sblock, DIP(dp, di_size));
641 if (lastbn >= UFS_NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
642 DIP(dp, di_size) == 0)
644 if ((newblk = allocblk(sblock.fs_frag)) == 0)
646 DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
647 DIP_SET(dp, di_db[lastbn], newblk);
648 DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
649 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
650 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
651 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
654 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
655 bp = getdirblk(newblk, sblock.fs_bsize);
658 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
659 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
660 cp < &bp->b_un.b_buf[sblock.fs_bsize];
662 memmove(cp, &emptydir, sizeof emptydir);
664 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
665 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
668 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
669 pwarn("NO SPACE LEFT IN %s", name);
671 printf(" (EXPANDED)\n");
672 else if (reply("EXPAND") == 0)
678 DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
679 DIP_SET(dp, di_db[lastbn + 1], 0);
680 DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
681 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
682 freeblk(newblk, sblock.fs_frag);
687 * allocate a new directory
690 allocdir(ino_t parent, ino_t request, int mode)
697 struct dirtemplate *dirp;
699 ino = allocino(request, IFDIR|mode);
702 dirp->dotdot_ino = parent;
704 bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
709 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
710 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
711 cp < &bp->b_un.b_buf[sblock.fs_fsize];
713 memmove(cp, &emptydir, sizeof emptydir);
715 DIP_SET(dp, di_nlink, 2);
717 if (ino == UFS_ROOTINO) {
718 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
722 if (!INO_IS_DVALID(parent)) {
727 inp = getinoinfo(ino);
728 inp->i_parent = parent;
729 inp->i_dotdot = parent;
730 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
731 if (inoinfo(ino)->ino_state == DSTATE) {
732 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
733 inoinfo(parent)->ino_linkcnt++;
736 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
742 * free a directory inode
745 freedir(ino_t ino, ino_t parent)
751 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
758 * generate a temporary name for the lost+found directory.
761 lftempname(char *bufp, ino_t ino)
768 for (in = maxino; in > 0; in /= 10)
774 *--cp = (in % 10) + '0';
782 * Get a directory block.
783 * Insure that it is held until another is requested.
785 static struct bufarea *
786 getdirblk(ufs2_daddr_t blkno, long size)
790 pdirbp->b_flags &= ~B_INUSE;
791 pdirbp = getdatablk(blkno, size, BT_DIRDATA);