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 $
33 #include <sys/param.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/dir.h>
38 #include <vfs/ufs/fs.h>
45 char *lfname = "lost+found";
47 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
48 struct dirtemplate dirhead = {
49 0, 12, DT_DIR, 1, ".",
50 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
52 struct odirtemplate odirhead = {
54 0, DIRBLKSIZ - 12, 2, ".."
57 static int chgino(struct inodesc *);
58 static int dircheck(struct inodesc *, struct direct *);
59 static int expanddir(struct ufs1_dinode *dp, char *name);
60 static void freedir(ufs1_ino_t ino, ufs1_ino_t parent);
61 static struct direct *fsck_readdir(struct inodesc *);
62 static struct bufarea *getdirblk(ufs_daddr_t blkno, long size);
63 static int lftempname(char *bufp, ufs1_ino_t ino);
64 static int mkentry(struct inodesc *);
67 * Propagate connected state through the tree.
72 struct inoinfo **inpp, *inp;
73 struct inoinfo **inpend;
76 inpend = &inpsort[inplast];
79 for (inpp = inpsort; inpp < inpend; inpp++) {
81 if (inp->i_parent == 0)
83 if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
84 inoinfo(inp->i_number)->ino_state == DSTATE) {
85 inoinfo(inp->i_number)->ino_state = DFOUND;
93 * Scan each entry in a directory block.
96 dirscan(struct inodesc *idesc)
100 unsigned int dsize, n;
102 char dbuf[DIRBLKSIZ];
104 if (idesc->id_type != DATA)
105 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
106 if (idesc->id_entryno == 0 &&
107 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
108 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
109 blksiz = idesc->id_numfrags * sblock.fs_fsize;
110 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
111 idesc->id_filesize -= blksiz;
115 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
116 dsize = dp->d_reclen;
117 if (dsize > sizeof(dbuf))
118 dsize = sizeof(dbuf);
119 memmove(dbuf, dp, (size_t)dsize);
120 # if (BYTE_ORDER == LITTLE_ENDIAN)
122 struct direct *tdp = (struct direct *)dbuf;
126 tdp->d_namlen = tdp->d_type;
130 idesc->id_dirp = (struct direct *)dbuf;
131 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
132 # if (BYTE_ORDER == LITTLE_ENDIAN)
133 if (!newinofmt && !doinglevel2) {
137 tdp = (struct direct *)dbuf;
139 tdp->d_namlen = tdp->d_type;
143 bp = getdirblk(idesc->id_blkno, blksiz);
144 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
152 return (idesc->id_filesize > 0 ? KEEPON : STOP);
156 * get next entry in a directory.
158 static struct direct *
159 fsck_readdir(struct inodesc *idesc)
161 struct direct *dp, *ndp;
163 long size, blksiz, fix, dploc;
165 blksiz = idesc->id_numfrags * sblock.fs_fsize;
166 bp = getdirblk(idesc->id_blkno, blksiz);
167 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
168 idesc->id_loc < blksiz) {
169 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
170 if (dircheck(idesc, dp))
172 if (idesc->id_fix == IGNORE)
174 fix = dofix(idesc, "DIRECTORY CORRUPTED");
175 bp = getdirblk(idesc->id_blkno, blksiz);
176 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
177 dp->d_reclen = DIRBLKSIZ;
181 dp->d_name[0] = '\0';
184 idesc->id_loc += DIRBLKSIZ;
185 idesc->id_filesize -= DIRBLKSIZ;
189 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
191 dploc = idesc->id_loc;
192 dp = (struct direct *)(bp->b_un.b_buf + dploc);
193 idesc->id_loc += dp->d_reclen;
194 idesc->id_filesize -= dp->d_reclen;
195 if ((idesc->id_loc % DIRBLKSIZ) == 0)
197 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
198 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
199 dircheck(idesc, ndp) == 0) {
200 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
201 idesc->id_loc += size;
202 idesc->id_filesize -= size;
203 if (idesc->id_fix == IGNORE)
205 fix = dofix(idesc, "DIRECTORY CORRUPTED");
206 bp = getdirblk(idesc->id_blkno, blksiz);
207 dp = (struct direct *)(bp->b_un.b_buf + dploc);
208 dp->d_reclen += size;
216 * Verify that a directory entry is valid.
217 * This is a superset of the checks made in the kernel.
220 dircheck(struct inodesc *idesc, struct direct *dp)
227 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
228 if (dp->d_reclen == 0 ||
229 dp->d_reclen > spaceleft ||
230 (dp->d_reclen & 0x3) != 0)
234 size = DIRSIZ(!newinofmt, dp);
235 # if (BYTE_ORDER == LITTLE_ENDIAN)
240 namlen = dp->d_namlen;
244 namlen = dp->d_namlen;
247 if (dp->d_reclen < size ||
248 idesc->id_filesize < size ||
251 for (cp = dp->d_name, size = 0; size < namlen; size++)
252 if (*cp == '\0' || (*cp++ == '/'))
260 direrror(ufs1_ino_t ino, char *errmesg)
263 fileerror(ino, ino, errmesg);
267 fileerror(ufs1_ino_t cwd, ufs1_ino_t ino, char *errmesg)
269 struct ufs1_dinode *dp;
270 char pathbuf[MAXPATHLEN + 1];
272 pwarn("%s ", errmesg);
275 getpathname(pathbuf, cwd, ino);
276 if (ino < ROOTINO || ino > maxino) {
277 pfatal("NAME=%s\n", pathbuf);
283 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
285 pfatal("NAME=%s\n", pathbuf);
289 adjust(struct inodesc *idesc, int lcnt)
291 struct ufs1_dinode *dp;
294 dp = ginode(idesc->id_number);
295 if (dp->di_nlink == lcnt) {
297 * If we have not hit any unresolved problems, are running
298 * in preen mode, and are on a filesystem using soft updates,
299 * then just toss any partially allocated files.
301 if (resolved && preen && usedsoftdep) {
302 clri(idesc, "UNREF", 1);
306 * The filesystem can be marked clean even if
307 * a file is not linked up, but is cleared.
308 * Hence, resolved should not be cleared when
309 * linkup is answered no, but clri is answered yes.
311 saveresolved = resolved;
312 if (linkup(idesc->id_number, (ufs1_ino_t)0, NULL) == 0) {
313 resolved = saveresolved;
314 clri(idesc, "UNREF", 0);
318 * Account for the new reference created by linkup().
320 dp = ginode(idesc->id_number);
325 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
326 ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
327 pinode(idesc->id_number);
328 printf(" COUNT %d SHOULD BE %d",
329 dp->di_nlink, dp->di_nlink - lcnt);
330 if (preen || usedsoftdep) {
333 pfatal("LINK COUNT INCREASING");
336 printf(" (ADJUSTED)\n");
338 if (preen || reply("ADJUST") == 1) {
339 dp->di_nlink -= lcnt;
346 mkentry(struct inodesc *idesc)
348 struct direct *dirp = idesc->id_dirp;
349 struct direct newent;
352 newent.d_namlen = strlen(idesc->id_name);
353 newlen = DIRSIZ(0, &newent);
354 if (dirp->d_ino != 0)
355 oldlen = DIRSIZ(0, dirp);
358 if (dirp->d_reclen - oldlen < newlen)
360 newent.d_reclen = dirp->d_reclen - oldlen;
361 dirp->d_reclen = oldlen;
362 dirp = (struct direct *)(((char *)dirp) + oldlen);
363 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
364 dirp->d_reclen = newent.d_reclen;
366 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
369 dirp->d_namlen = newent.d_namlen;
370 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
371 # if (BYTE_ORDER == LITTLE_ENDIAN)
373 * If the entry was split, dirscan() will only reverse the byte
374 * order of the original entry, and not the new one, before
375 * writing it back out. So, we reverse the byte order here if
378 if (oldlen != 0 && !newinofmt && !doinglevel2) {
381 tmp = dirp->d_namlen;
382 dirp->d_namlen = dirp->d_type;
386 return (ALTERED|STOP);
390 chgino(struct inodesc *idesc)
392 struct direct *dirp = idesc->id_dirp;
394 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
396 dirp->d_ino = idesc->id_parent;
398 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
401 return (ALTERED|STOP);
405 linkup(ufs1_ino_t orphan, ufs1_ino_t parentdir, char *name)
407 struct ufs1_dinode *dp;
410 struct inodesc idesc;
411 char tempname[BUFSIZ];
413 memset(&idesc, 0, sizeof(struct inodesc));
415 lostdir = (dp->di_mode & IFMT) == IFDIR;
416 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
418 if (preen && dp->di_size == 0)
421 printf(" (RECONNECTED)\n");
423 if (reply("RECONNECT") == 0)
426 dp = ginode(ROOTINO);
427 idesc.id_name = lfname;
428 idesc.id_type = DATA;
429 idesc.id_func = findino;
430 idesc.id_number = ROOTINO;
431 if ((ckinode(dp, &idesc) & FOUND) != 0) {
432 lfdir = idesc.id_parent;
434 pwarn("NO lost+found DIRECTORY");
435 if (preen || reply("CREATE")) {
436 lfdir = allocdir(ROOTINO, (ufs1_ino_t)0, lfmode);
438 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
441 printf(" (CREATED)\n");
443 freedir(lfdir, ROOTINO);
452 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
458 if ((dp->di_mode & IFMT) != IFDIR) {
459 pfatal("lost+found IS NOT A DIRECTORY");
460 if (reply("REALLOCATE") == 0)
463 if ((lfdir = allocdir(ROOTINO, (ufs1_ino_t)0, lfmode)) == 0) {
464 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
467 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
468 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
472 idesc.id_type = ADDR;
473 idesc.id_func = pass4check;
474 idesc.id_number = oldlfdir;
475 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
476 inoinfo(oldlfdir)->ino_linkcnt = 0;
479 if (inoinfo(lfdir)->ino_state != DFOUND) {
480 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
483 lftempname(tempname, orphan);
484 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
485 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
489 inoinfo(orphan)->ino_linkcnt--;
491 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
492 parentdir != (ufs1_ino_t)-1)
493 makeentry(orphan, lfdir, "..");
497 inoinfo(lfdir)->ino_linkcnt++;
498 pwarn("DIR I=%u CONNECTED. ", orphan);
499 if (parentdir != (ufs1_ino_t)-1) {
500 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
502 * The parent directory, because of the ordering
503 * guarantees, has had the link count incremented
504 * for the child, but no entry was made. This
505 * fixes the parent link count so that fsck does
506 * not need to be rerun.
508 inoinfo(parentdir)->ino_linkcnt++;
517 * fix an entry in a directory.
520 changeino(ufs1_ino_t dir, char *name, ufs1_ino_t newnum)
522 struct inodesc idesc;
524 memset(&idesc, 0, sizeof(struct inodesc));
525 idesc.id_type = DATA;
526 idesc.id_func = chgino;
527 idesc.id_number = dir;
528 idesc.id_fix = DONTKNOW;
529 idesc.id_name = name;
530 idesc.id_parent = newnum; /* new value for name */
531 return (ckinode(ginode(dir), &idesc));
535 * make an entry in a directory
538 makeentry(ufs1_ino_t parent, ufs1_ino_t ino, char *name)
540 struct ufs1_dinode *dp;
541 struct inodesc idesc;
542 char pathbuf[MAXPATHLEN + 1];
544 if (parent < ROOTINO || parent >= maxino ||
545 ino < ROOTINO || ino >= maxino)
547 memset(&idesc, 0, sizeof(struct inodesc));
548 idesc.id_type = DATA;
549 idesc.id_func = mkentry;
550 idesc.id_number = parent;
551 idesc.id_parent = ino; /* this is the inode to enter */
552 idesc.id_fix = DONTKNOW;
553 idesc.id_name = name;
555 if (dp->di_size % DIRBLKSIZ) {
556 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
559 if ((ckinode(dp, &idesc) & ALTERED) != 0)
561 getpathname(pathbuf, parent, parent);
563 if (expanddir(dp, pathbuf) == 0)
565 return (ckinode(dp, &idesc) & ALTERED);
569 * Attempt to expand the size of a directory
572 expanddir(struct ufs1_dinode *dp, char *name)
574 ufs_daddr_t lastbn, newblk;
576 char *cp, firstblk[DIRBLKSIZ];
578 lastbn = lblkno(&sblock, dp->di_size);
579 if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
581 if ((newblk = allocblk(sblock.fs_frag)) == 0)
583 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
584 dp->di_db[lastbn] = newblk;
585 dp->di_size += sblock.fs_bsize;
586 dp->di_blocks += btodb(sblock.fs_bsize);
587 bp = getdirblk(dp->di_db[lastbn + 1],
588 (long)dblksize(&sblock, dp, lastbn + 1));
591 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
592 bp = getdirblk(newblk, sblock.fs_bsize);
595 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
596 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
597 cp < &bp->b_un.b_buf[sblock.fs_bsize];
599 memmove(cp, &emptydir, sizeof emptydir);
601 bp = getdirblk(dp->di_db[lastbn + 1],
602 (long)dblksize(&sblock, dp, lastbn + 1));
605 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
606 pwarn("NO SPACE LEFT IN %s", name);
608 printf(" (EXPANDED)\n");
609 else if (reply("EXPAND") == 0)
615 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
616 dp->di_db[lastbn + 1] = 0;
617 dp->di_size -= sblock.fs_bsize;
618 dp->di_blocks -= btodb(sblock.fs_bsize);
619 freeblk(newblk, sblock.fs_frag);
624 * allocate a new directory
627 allocdir(ufs1_ino_t parent, ufs1_ino_t request, int mode)
631 struct ufs1_dinode *dp;
633 struct dirtemplate *dirp;
635 ino = allocino(request, IFDIR|mode);
639 dirp = (struct dirtemplate *)&odirhead;
641 dirp->dotdot_ino = parent;
643 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
648 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
649 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
650 cp < &bp->b_un.b_buf[sblock.fs_fsize];
652 memmove(cp, &emptydir, sizeof emptydir);
656 if (ino == ROOTINO) {
657 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
661 if (inoinfo(parent)->ino_state != DSTATE &&
662 inoinfo(parent)->ino_state != DFOUND) {
667 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
668 if (inoinfo(ino)->ino_state == DSTATE) {
669 inoinfo(ino)->ino_linkcnt = dp->di_nlink;
670 inoinfo(parent)->ino_linkcnt++;
679 * free a directory inode
682 freedir(ufs1_ino_t ino, ufs1_ino_t parent)
684 struct ufs1_dinode *dp;
695 * generate a temporary name for the lost+found directory.
698 lftempname(char *bufp, ufs1_ino_t ino)
705 for (in = maxino; in > 0; in /= 10)
711 *--cp = (in % 10) + '0';
719 * Get a directory block.
720 * Insure that it is held until another is requested.
722 static struct bufarea *
723 getdirblk(ufs_daddr_t blkno, long size)
727 pdirbp->b_flags &= ~B_INUSE;
728 pdirbp = getdatablk(blkno, size);