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. 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
29 * @(#)dir.c 8.8 (Berkeley) 4/28/95
30 * $FreeBSD: src/sbin/fsck/dir.c,v 1.15 1999/08/28 00:12:45 peter Exp $
31 * $DragonFly: src/sbin/fsck/dir.c,v 1.11 2006/10/19 21:11:13 pavalos Exp $
34 #include <sys/param.h>
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/dir.h>
39 #include <vfs/ufs/fs.h>
46 char *lfname = "lost+found";
48 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
49 struct dirtemplate dirhead = {
50 0, 12, DT_DIR, 1, ".",
51 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
53 struct odirtemplate odirhead = {
55 0, DIRBLKSIZ - 12, 2, ".."
58 static int chgino(struct inodesc *);
59 static int dircheck(struct inodesc *, struct direct *);
60 static int expanddir(struct ufs1_dinode *dp, char *name);
61 static void freedir(ufs1_ino_t ino, ufs1_ino_t parent);
62 static struct direct *fsck_readdir(struct inodesc *);
63 static struct bufarea *getdirblk(ufs_daddr_t blkno, long size);
64 static int lftempname(char *bufp, ufs1_ino_t ino);
65 static int mkentry(struct inodesc *);
68 * Propagate connected state through the tree.
73 struct inoinfo **inpp, *inp;
74 struct inoinfo **inpend;
77 inpend = &inpsort[inplast];
80 for (inpp = inpsort; inpp < inpend; inpp++) {
82 if (inp->i_parent == 0)
84 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
85 inoinfo(inp->i_number)->ino_state == DSTATE) {
86 inoinfo(inp->i_number)->ino_state = DFOUND;
94 * Scan each entry in a directory block.
97 dirscan(struct inodesc *idesc)
101 unsigned int dsize, n;
103 char dbuf[DIRBLKSIZ];
105 if (idesc->id_type != DATA)
106 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
107 if (idesc->id_entryno == 0 &&
108 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
109 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
110 blksiz = idesc->id_numfrags * sblock.fs_fsize;
111 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
112 idesc->id_filesize -= blksiz;
116 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
117 dsize = dp->d_reclen;
118 if (dsize > sizeof(dbuf))
119 dsize = sizeof(dbuf);
120 memmove(dbuf, dp, (size_t)dsize);
121 # if (BYTE_ORDER == LITTLE_ENDIAN)
123 struct direct *tdp = (struct direct *)dbuf;
127 tdp->d_namlen = tdp->d_type;
131 idesc->id_dirp = (struct direct *)dbuf;
132 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
133 # if (BYTE_ORDER == LITTLE_ENDIAN)
134 if (!newinofmt && !doinglevel2) {
138 tdp = (struct direct *)dbuf;
140 tdp->d_namlen = tdp->d_type;
144 bp = getdirblk(idesc->id_blkno, blksiz);
145 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
153 return (idesc->id_filesize > 0 ? KEEPON : STOP);
157 * get next entry in a directory.
159 static struct direct *
160 fsck_readdir(struct inodesc *idesc)
162 struct direct *dp, *ndp;
164 long size, blksiz, fix, dploc;
166 blksiz = idesc->id_numfrags * sblock.fs_fsize;
167 bp = getdirblk(idesc->id_blkno, blksiz);
168 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
169 idesc->id_loc < blksiz) {
170 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
171 if (dircheck(idesc, dp))
173 if (idesc->id_fix == IGNORE)
175 fix = dofix(idesc, "DIRECTORY CORRUPTED");
176 bp = getdirblk(idesc->id_blkno, blksiz);
177 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
178 dp->d_reclen = DIRBLKSIZ;
182 dp->d_name[0] = '\0';
185 idesc->id_loc += DIRBLKSIZ;
186 idesc->id_filesize -= DIRBLKSIZ;
190 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
192 dploc = idesc->id_loc;
193 dp = (struct direct *)(bp->b_un.b_buf + dploc);
194 idesc->id_loc += dp->d_reclen;
195 idesc->id_filesize -= dp->d_reclen;
196 if ((idesc->id_loc % DIRBLKSIZ) == 0)
198 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
199 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
200 dircheck(idesc, ndp) == 0) {
201 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
202 idesc->id_loc += size;
203 idesc->id_filesize -= size;
204 if (idesc->id_fix == IGNORE)
206 fix = dofix(idesc, "DIRECTORY CORRUPTED");
207 bp = getdirblk(idesc->id_blkno, blksiz);
208 dp = (struct direct *)(bp->b_un.b_buf + dploc);
209 dp->d_reclen += size;
217 * Verify that a directory entry is valid.
218 * This is a superset of the checks made in the kernel.
221 dircheck(struct inodesc *idesc, struct direct *dp)
228 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
229 if (dp->d_reclen == 0 ||
230 dp->d_reclen > spaceleft ||
231 (dp->d_reclen & 0x3) != 0)
235 size = DIRSIZ(!newinofmt, dp);
236 # if (BYTE_ORDER == LITTLE_ENDIAN)
241 namlen = dp->d_namlen;
245 namlen = dp->d_namlen;
248 if (dp->d_reclen < size ||
249 idesc->id_filesize < size ||
252 for (cp = dp->d_name, size = 0; size < namlen; size++)
253 if (*cp == '\0' || (*cp++ == '/'))
261 direrror(ufs1_ino_t ino, char *errmesg)
264 fileerror(ino, ino, errmesg);
268 fileerror(ufs1_ino_t cwd, ufs1_ino_t ino, char *errmesg)
270 struct ufs1_dinode *dp;
271 char pathbuf[MAXPATHLEN + 1];
273 pwarn("%s ", errmesg);
276 getpathname(pathbuf, cwd, ino);
277 if (ino < ROOTINO || ino > maxino) {
278 pfatal("NAME=%s\n", pathbuf);
284 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
286 pfatal("NAME=%s\n", pathbuf);
290 adjust(struct inodesc *idesc, int lcnt)
292 struct ufs1_dinode *dp;
295 dp = ginode(idesc->id_number);
296 if (dp->di_nlink == lcnt) {
298 * If we have not hit any unresolved problems, are running
299 * in preen mode, and are on a filesystem using soft updates,
300 * then just toss any partially allocated files.
302 if (resolved && preen && usedsoftdep) {
303 clri(idesc, "UNREF", 1);
307 * The filesystem can be marked clean even if
308 * a file is not linked up, but is cleared.
309 * Hence, resolved should not be cleared when
310 * linkup is answered no, but clri is answered yes.
312 saveresolved = resolved;
313 if (linkup(idesc->id_number, (ufs1_ino_t)0, NULL) == 0) {
314 resolved = saveresolved;
315 clri(idesc, "UNREF", 0);
319 * Account for the new reference created by linkup().
321 dp = ginode(idesc->id_number);
326 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
327 ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
328 pinode(idesc->id_number);
329 printf(" COUNT %d SHOULD BE %d",
330 dp->di_nlink, dp->di_nlink - lcnt);
331 if (preen || usedsoftdep) {
334 pfatal("LINK COUNT INCREASING");
337 printf(" (ADJUSTED)\n");
339 if (preen || reply("ADJUST") == 1) {
340 dp->di_nlink -= lcnt;
347 mkentry(struct inodesc *idesc)
349 struct direct *dirp = idesc->id_dirp;
350 struct direct newent;
353 newent.d_namlen = strlen(idesc->id_name);
354 newlen = DIRSIZ(0, &newent);
355 if (dirp->d_ino != 0)
356 oldlen = DIRSIZ(0, dirp);
359 if (dirp->d_reclen - oldlen < newlen)
361 newent.d_reclen = dirp->d_reclen - oldlen;
362 dirp->d_reclen = oldlen;
363 dirp = (struct direct *)(((char *)dirp) + oldlen);
364 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
365 dirp->d_reclen = newent.d_reclen;
367 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
370 dirp->d_namlen = newent.d_namlen;
371 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
372 # if (BYTE_ORDER == LITTLE_ENDIAN)
374 * If the entry was split, dirscan() will only reverse the byte
375 * order of the original entry, and not the new one, before
376 * writing it back out. So, we reverse the byte order here if
379 if (oldlen != 0 && !newinofmt && !doinglevel2) {
382 tmp = dirp->d_namlen;
383 dirp->d_namlen = dirp->d_type;
387 return (ALTERED|STOP);
391 chgino(struct inodesc *idesc)
393 struct direct *dirp = idesc->id_dirp;
395 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
397 dirp->d_ino = idesc->id_parent;
399 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
402 return (ALTERED|STOP);
406 linkup(ufs1_ino_t orphan, ufs1_ino_t parentdir, char *name)
408 struct ufs1_dinode *dp;
411 struct inodesc idesc;
412 char tempname[BUFSIZ];
414 memset(&idesc, 0, sizeof(struct inodesc));
416 lostdir = (dp->di_mode & IFMT) == IFDIR;
417 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
419 if (preen && dp->di_size == 0)
422 printf(" (RECONNECTED)\n");
424 if (reply("RECONNECT") == 0)
427 dp = ginode(ROOTINO);
428 idesc.id_name = lfname;
429 idesc.id_type = DATA;
430 idesc.id_func = findino;
431 idesc.id_number = ROOTINO;
432 if ((ckinode(dp, &idesc) & FOUND) != 0) {
433 lfdir = idesc.id_parent;
435 pwarn("NO lost+found DIRECTORY");
436 if (preen || reply("CREATE")) {
437 lfdir = allocdir(ROOTINO, (ufs1_ino_t)0, lfmode);
439 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
442 printf(" (CREATED)\n");
444 freedir(lfdir, ROOTINO);
453 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
459 if ((dp->di_mode & IFMT) != IFDIR) {
460 pfatal("lost+found IS NOT A DIRECTORY");
461 if (reply("REALLOCATE") == 0)
464 if ((lfdir = allocdir(ROOTINO, (ufs1_ino_t)0, lfmode)) == 0) {
465 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
468 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
469 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
473 idesc.id_type = ADDR;
474 idesc.id_func = pass4check;
475 idesc.id_number = oldlfdir;
476 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
477 inoinfo(oldlfdir)->ino_linkcnt = 0;
480 if (inoinfo(lfdir)->ino_state != DFOUND) {
481 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
484 lftempname(tempname, orphan);
485 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
486 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
490 inoinfo(orphan)->ino_linkcnt--;
492 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
493 parentdir != (ufs1_ino_t)-1)
494 makeentry(orphan, lfdir, "..");
498 inoinfo(lfdir)->ino_linkcnt++;
499 pwarn("DIR I=%lu CONNECTED. ", orphan);
500 if (parentdir != (ufs1_ino_t)-1) {
501 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
503 * The parent directory, because of the ordering
504 * guarantees, has had the link count incremented
505 * for the child, but no entry was made. This
506 * fixes the parent link count so that fsck does
507 * not need to be rerun.
509 inoinfo(parentdir)->ino_linkcnt++;
518 * fix an entry in a directory.
521 changeino(ufs1_ino_t dir, char *name, ufs1_ino_t newnum)
523 struct inodesc idesc;
525 memset(&idesc, 0, sizeof(struct inodesc));
526 idesc.id_type = DATA;
527 idesc.id_func = chgino;
528 idesc.id_number = dir;
529 idesc.id_fix = DONTKNOW;
530 idesc.id_name = name;
531 idesc.id_parent = newnum; /* new value for name */
532 return (ckinode(ginode(dir), &idesc));
536 * make an entry in a directory
539 makeentry(ufs1_ino_t parent, ufs1_ino_t ino, char *name)
541 struct ufs1_dinode *dp;
542 struct inodesc idesc;
543 char pathbuf[MAXPATHLEN + 1];
545 if (parent < ROOTINO || parent >= maxino ||
546 ino < ROOTINO || ino >= maxino)
548 memset(&idesc, 0, sizeof(struct inodesc));
549 idesc.id_type = DATA;
550 idesc.id_func = mkentry;
551 idesc.id_number = parent;
552 idesc.id_parent = ino; /* this is the inode to enter */
553 idesc.id_fix = DONTKNOW;
554 idesc.id_name = name;
556 if (dp->di_size % DIRBLKSIZ) {
557 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
560 if ((ckinode(dp, &idesc) & ALTERED) != 0)
562 getpathname(pathbuf, parent, parent);
564 if (expanddir(dp, pathbuf) == 0)
566 return (ckinode(dp, &idesc) & ALTERED);
570 * Attempt to expand the size of a directory
573 expanddir(struct ufs1_dinode *dp, char *name)
575 ufs_daddr_t lastbn, newblk;
577 char *cp, firstblk[DIRBLKSIZ];
579 lastbn = lblkno(&sblock, dp->di_size);
580 if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
582 if ((newblk = allocblk(sblock.fs_frag)) == 0)
584 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
585 dp->di_db[lastbn] = newblk;
586 dp->di_size += sblock.fs_bsize;
587 dp->di_blocks += btodb(sblock.fs_bsize);
588 bp = getdirblk(dp->di_db[lastbn + 1],
589 (long)dblksize(&sblock, dp, lastbn + 1));
592 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
593 bp = getdirblk(newblk, sblock.fs_bsize);
596 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
597 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
598 cp < &bp->b_un.b_buf[sblock.fs_bsize];
600 memmove(cp, &emptydir, sizeof emptydir);
602 bp = getdirblk(dp->di_db[lastbn + 1],
603 (long)dblksize(&sblock, dp, lastbn + 1));
606 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
607 pwarn("NO SPACE LEFT IN %s", name);
609 printf(" (EXPANDED)\n");
610 else if (reply("EXPAND") == 0)
616 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
617 dp->di_db[lastbn + 1] = 0;
618 dp->di_size -= sblock.fs_bsize;
619 dp->di_blocks -= btodb(sblock.fs_bsize);
620 freeblk(newblk, sblock.fs_frag);
625 * allocate a new directory
628 allocdir(ufs1_ino_t parent, ufs1_ino_t request, int mode)
632 struct ufs1_dinode *dp;
634 struct dirtemplate *dirp;
636 ino = allocino(request, IFDIR|mode);
640 dirp = (struct dirtemplate *)&odirhead;
642 dirp->dotdot_ino = parent;
644 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
649 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
650 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
651 cp < &bp->b_un.b_buf[sblock.fs_fsize];
653 memmove(cp, &emptydir, sizeof emptydir);
657 if (ino == ROOTINO) {
658 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
662 if (inoinfo(parent)->ino_state != DSTATE &&
663 inoinfo(parent)->ino_state != DFOUND) {
668 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
669 if (inoinfo(ino)->ino_state == DSTATE) {
670 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
671 inoinfo(parent)->ino_linkcnt++;
680 * free a directory inode
683 freedir(ufs1_ino_t ino, ufs1_ino_t parent)
685 struct ufs1_dinode *dp;
696 * generate a temporary name for the lost+found directory.
699 lftempname(char *bufp, ufs1_ino_t ino)
706 for (in = maxino; in > 0; in /= 10)
712 *--cp = (in % 10) + '0';
720 * Get a directory block.
721 * Insure that it is held until another is requested.
723 static struct bufarea *
724 getdirblk(ufs_daddr_t blkno, long size)
728 pdirbp->b_flags &= ~B_INUSE;
729 pdirbp = getdatablk(blkno, size);