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 * 4. 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.
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
32 static const char sccsid[] = "@(#)dir.c 8.8 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
52 static struct dirtemplate emptydir = {
53 0, DIRBLKSIZ, DT_UNKNOWN, 0, "",
54 0, 0, DT_UNKNOWN, 0, ""
56 static struct dirtemplate dirhead = {
57 0, 12, DT_DIR, 1, ".",
58 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
61 static int chgino(struct inodesc *);
62 static int dircheck(struct inodesc *, struct direct *);
63 static int expanddir(union dinode *dp, char *name);
64 static void freedir(ino_t ino, ino_t parent);
65 static struct direct *fsck_readdir(struct inodesc *);
66 static struct bufarea *getdirblk(ufs2_daddr_t blkno, long size);
67 static int lftempname(char *bufp, ino_t ino);
68 static int mkentry(struct inodesc *);
71 * Propagate connected state through the tree.
76 struct inoinfo **inpp, *inp;
77 struct inoinfo **inpend;
80 inpend = &inpsort[inplast];
83 for (inpp = inpsort; inpp < inpend; inpp++) {
85 if (inp->i_parent == 0)
87 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
88 INO_IS_DUNFOUND(inp->i_number)) {
89 inoinfo(inp->i_number)->ino_state = DFOUND;
97 * Scan each entry in a directory block.
100 dirscan(struct inodesc *idesc)
106 char dbuf[DIRBLKSIZ];
108 if (idesc->id_type != DATA)
109 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
110 if (idesc->id_entryno == 0 &&
111 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
112 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
113 blksiz = idesc->id_numfrags * sblock.fs_fsize;
114 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
115 idesc->id_filesize -= blksiz;
119 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
120 dsize = dp->d_reclen;
121 if (dsize > sizeof(dbuf))
122 dsize = sizeof(dbuf);
123 memmove(dbuf, dp, (size_t)dsize);
124 idesc->id_dirp = (struct direct *)dbuf;
125 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
126 bp = getdirblk(idesc->id_blkno, blksiz);
127 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
136 return (idesc->id_filesize > 0 ? KEEPON : STOP);
140 * get next entry in a directory.
142 static struct direct *
143 fsck_readdir(struct inodesc *idesc)
145 struct direct *dp, *ndp;
147 long size, blksiz, fix, dploc;
150 blksiz = idesc->id_numfrags * sblock.fs_fsize;
151 bp = getdirblk(idesc->id_blkno, blksiz);
152 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
153 idesc->id_loc < blksiz) {
154 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
155 if ((dc = dircheck(idesc, dp)) > 0) {
158 * dircheck() cleared unused directory space.
159 * Mark the buffer as dirty to write it out.
165 if (idesc->id_fix == IGNORE)
167 fix = dofix(idesc, "DIRECTORY CORRUPTED");
168 bp = getdirblk(idesc->id_blkno, blksiz);
169 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
170 dp->d_reclen = DIRBLKSIZ;
174 dp->d_name[0] = '\0';
177 idesc->id_loc += DIRBLKSIZ;
178 idesc->id_filesize -= DIRBLKSIZ;
182 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
184 dploc = idesc->id_loc;
185 dp = (struct direct *)(bp->b_un.b_buf + dploc);
186 idesc->id_loc += dp->d_reclen;
187 idesc->id_filesize -= dp->d_reclen;
188 if ((idesc->id_loc % DIRBLKSIZ) == 0)
190 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
191 if (idesc->id_loc < blksiz && idesc->id_filesize > 0) {
192 if ((dc = dircheck(idesc, ndp)) == 0) {
193 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
194 idesc->id_loc += size;
195 idesc->id_filesize -= size;
196 if (idesc->id_fix == IGNORE)
198 fix = dofix(idesc, "DIRECTORY CORRUPTED");
199 bp = getdirblk(idesc->id_blkno, blksiz);
200 dp = (struct direct *)(bp->b_un.b_buf + dploc);
201 dp->d_reclen += size;
204 } else if (dc == 2) {
206 * dircheck() cleared unused directory space.
207 * Mark the buffer as dirty to write it out.
216 * Verify that a directory entry is valid.
217 * This is a superset of the checks made in the kernel.
218 * Also optionally clears padding and unused directory space.
220 * Returns 0 if the entry is bad, 1 if the entry is good and no changes
221 * were made, and 2 if the entry is good but modified to clear out padding
222 * and unused space and needs to be written back to disk.
225 dircheck(struct inodesc *idesc, struct direct *dp)
231 int spaceleft, modified, unused;
234 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
235 if (dp->d_reclen == 0 ||
236 dp->d_reclen > spaceleft ||
237 (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
239 if (dp->d_ino == 0) {
241 * Special case of an unused directory entry. Normally
242 * the kernel would coalesce unused space with the previous
243 * entry by extending its d_reclen, but there are situations
244 * (e.g. fsck) where that doesn't occur.
245 * If we're clearing out directory cruft (-z flag), then make
246 * sure this entry gets fully cleared as well.
248 if (zflag && fswritefd >= 0) {
249 if (dp->d_type != 0) {
253 if (dp->d_namlen != 0) {
257 if (dp->d_name[0] != '\0') {
258 dp->d_name[0] = '\0';
264 size = DIRSIZ(0, dp);
265 namlen = dp->d_namlen;
267 if (dp->d_reclen < size ||
268 idesc->id_filesize < size ||
272 for (cp = dp->d_name, size = 0; size < namlen; size++)
273 if (*cp == '\0' || (*cp++ == '/'))
279 if (zflag && fswritefd >= 0) {
281 * Clear unused directory entry space, including the d_name
284 /* First figure the number of pad bytes. */
285 unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
287 /* Add in the free space to the end of the record. */
288 unused += dp->d_reclen - DIRSIZ(0, dp);
291 * Now clear out the unused space, keeping track if we actually
294 for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
310 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
311 dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
317 direrror(ino_t ino, const char *errmesg)
320 fileerror(ino, ino, errmesg);
324 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
327 char pathbuf[MAXPATHLEN + 1];
329 pwarn("%s ", errmesg);
332 getpathname(pathbuf, cwd, ino);
333 if (ino < ROOTINO || ino > maxino) {
334 pfatal("NAME=%s\n", pathbuf);
340 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
343 pfatal("NAME=%s\n", pathbuf);
347 adjust(struct inodesc *idesc, int lcnt)
352 dp = ginode(idesc->id_number);
353 if (DIP(dp, di_nlink) == lcnt) {
355 * If we have not hit any unresolved problems, are running
356 * in preen mode, and are on a file system using soft updates,
357 * then just toss any partially allocated files.
359 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
360 clri(idesc, "UNREF", 1);
364 * The file system can be marked clean even if
365 * a file is not linked up, but is cleared.
366 * Hence, resolved should not be cleared when
367 * linkup is answered no, but clri is answered yes.
369 saveresolved = resolved;
370 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
371 resolved = saveresolved;
372 clri(idesc, "UNREF", 0);
376 * Account for the new reference created by linkup().
378 dp = ginode(idesc->id_number);
383 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
384 ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
385 pinode(idesc->id_number);
386 printf(" COUNT %d SHOULD BE %d",
387 DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
388 if (preen || usedsoftdep) {
391 pfatal("LINK COUNT INCREASING");
394 printf(" (ADJUSTED)\n");
396 if (preen || reply("ADJUST") == 1) {
397 if (bkgrdflag == 0) {
398 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
401 cmd.value = idesc->id_number;
404 printf("adjrefcnt ino %ld amt %lld\n",
406 (long long)cmd.size);
407 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
408 &cmd, sizeof cmd) == -1)
409 rwerror("ADJUST INODE", cmd.value);
416 mkentry(struct inodesc *idesc)
418 struct direct *dirp = idesc->id_dirp;
419 struct direct newent;
422 newent.d_namlen = strlen(idesc->id_name);
423 newlen = DIRSIZ(0, &newent);
424 if (dirp->d_ino != 0)
425 oldlen = DIRSIZ(0, dirp);
428 if (dirp->d_reclen - oldlen < newlen)
430 newent.d_reclen = dirp->d_reclen - oldlen;
431 dirp->d_reclen = oldlen;
432 dirp = (struct direct *)(((char *)dirp) + oldlen);
433 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
434 dirp->d_reclen = newent.d_reclen;
435 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
436 dirp->d_namlen = newent.d_namlen;
437 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
438 return (ALTERED|STOP);
442 chgino(struct inodesc *idesc)
444 struct direct *dirp = idesc->id_dirp;
446 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
448 dirp->d_ino = idesc->id_parent;
449 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
450 return (ALTERED|STOP);
454 linkup(ino_t orphan, ino_t parentdir, char *name)
459 struct inodesc idesc;
460 char tempname[BUFSIZ];
462 memset(&idesc, 0, sizeof(struct inodesc));
464 lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
465 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
467 if (preen && DIP(dp, di_size) == 0)
469 if (cursnapshot != 0) {
470 pfatal("FILE LINKUP IN SNAPSHOT");
474 printf(" (RECONNECTED)\n");
476 if (reply("RECONNECT") == 0)
479 dp = ginode(ROOTINO);
480 idesc.id_name = strdup(lfname);
481 idesc.id_type = DATA;
482 idesc.id_func = findino;
483 idesc.id_number = ROOTINO;
484 if ((ckinode(dp, &idesc) & FOUND) != 0) {
485 lfdir = idesc.id_parent;
487 pwarn("NO lost+found DIRECTORY");
488 if (preen || reply("CREATE")) {
489 lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
491 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
494 printf(" (CREATED)\n");
496 freedir(lfdir, ROOTINO);
505 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
511 if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
512 pfatal("lost+found IS NOT A DIRECTORY");
513 if (reply("REALLOCATE") == 0)
516 if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
517 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
520 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
521 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
525 idesc.id_type = ADDR;
526 idesc.id_func = pass4check;
527 idesc.id_number = oldlfdir;
528 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
529 inoinfo(oldlfdir)->ino_linkcnt = 0;
532 if (inoinfo(lfdir)->ino_state != DFOUND) {
533 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
536 (void)lftempname(tempname, orphan);
537 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
538 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
542 inoinfo(orphan)->ino_linkcnt--;
544 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
545 parentdir != (ino_t)-1)
546 (void)makeentry(orphan, lfdir, "..");
548 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
550 inoinfo(lfdir)->ino_linkcnt++;
551 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
552 if (parentdir != (ino_t)-1) {
553 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
555 * The parent directory, because of the ordering
556 * guarantees, has had the link count incremented
557 * for the child, but no entry was made. This
558 * fixes the parent link count so that fsck does
559 * not need to be rerun.
561 inoinfo(parentdir)->ino_linkcnt++;
570 * fix an entry in a directory.
573 changeino(ino_t dir, const char *name, ino_t newnum)
575 struct inodesc idesc;
577 memset(&idesc, 0, sizeof(struct inodesc));
578 idesc.id_type = DATA;
579 idesc.id_func = chgino;
580 idesc.id_number = dir;
581 idesc.id_fix = DONTKNOW;
582 idesc.id_name = strdup(name);
583 idesc.id_parent = newnum; /* new value for name */
584 return (ckinode(ginode(dir), &idesc));
588 * make an entry in a directory
591 makeentry(ino_t parent, ino_t ino, const char *name)
594 struct inodesc idesc;
595 char pathbuf[MAXPATHLEN + 1];
597 if (parent < ROOTINO || parent >= maxino ||
598 ino < ROOTINO || ino >= maxino)
600 memset(&idesc, 0, sizeof(struct inodesc));
601 idesc.id_type = DATA;
602 idesc.id_func = mkentry;
603 idesc.id_number = parent;
604 idesc.id_parent = ino; /* this is the inode to enter */
605 idesc.id_fix = DONTKNOW;
606 idesc.id_name = strdup(name);
608 if (DIP(dp, di_size) % DIRBLKSIZ) {
609 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
612 if ((ckinode(dp, &idesc) & ALTERED) != 0)
614 getpathname(pathbuf, parent, parent);
616 if (expanddir(dp, pathbuf) == 0)
618 return (ckinode(dp, &idesc) & ALTERED);
622 * Attempt to expand the size of a directory
625 expanddir(union dinode *dp, char *name)
627 ufs2_daddr_t lastbn, newblk;
629 char *cp, firstblk[DIRBLKSIZ];
631 lastbn = lblkno(&sblock, DIP(dp, di_size));
632 if (lastbn >= NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
633 DIP(dp, di_size) == 0)
635 if ((newblk = allocblk(sblock.fs_frag)) == 0)
637 DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
638 DIP_SET(dp, di_db[lastbn], newblk);
639 DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
640 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
641 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
642 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
645 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
646 bp = getdirblk(newblk, sblock.fs_bsize);
649 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
650 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
651 cp < &bp->b_un.b_buf[sblock.fs_bsize];
653 memmove(cp, &emptydir, sizeof emptydir);
655 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
656 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
659 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
660 pwarn("NO SPACE LEFT IN %s", name);
662 printf(" (EXPANDED)\n");
663 else if (reply("EXPAND") == 0)
669 DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
670 DIP_SET(dp, di_db[lastbn + 1], 0);
671 DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
672 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
673 freeblk(newblk, sblock.fs_frag);
678 * allocate a new directory
681 allocdir(ino_t parent, ino_t request, int mode)
688 struct dirtemplate *dirp;
690 ino = allocino(request, IFDIR|mode);
693 dirp->dotdot_ino = parent;
695 bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
700 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
701 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
702 cp < &bp->b_un.b_buf[sblock.fs_fsize];
704 memmove(cp, &emptydir, sizeof emptydir);
706 DIP_SET(dp, di_nlink, 2);
708 if (ino == ROOTINO) {
709 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
713 if (!INO_IS_DVALID(parent)) {
718 inp = getinoinfo(ino);
719 inp->i_parent = parent;
720 inp->i_dotdot = parent;
721 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
722 if (inoinfo(ino)->ino_state == DSTATE) {
723 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
724 inoinfo(parent)->ino_linkcnt++;
727 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
733 * free a directory inode
736 freedir(ino_t ino, ino_t parent)
742 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
749 * generate a temporary name for the lost+found directory.
752 lftempname(char *bufp, ino_t ino)
759 for (in = maxino; in > 0; in /= 10)
765 *--cp = (in % 10) + '0';
773 * Get a directory block.
774 * Insure that it is held until another is requested.
776 static struct bufarea *
777 getdirblk(ufs2_daddr_t blkno, long size)
781 pdirbp->b_flags &= ~B_INUSE;
782 pdirbp = getdatablk(blkno, size, BT_DIRDATA);