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
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;
149 blksiz = idesc->id_numfrags * sblock.fs_fsize;
150 bp = getdirblk(idesc->id_blkno, blksiz);
151 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
152 idesc->id_loc < blksiz) {
153 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
154 if (dircheck(idesc, dp))
156 if (idesc->id_fix == IGNORE)
158 fix = dofix(idesc, "DIRECTORY CORRUPTED");
159 bp = getdirblk(idesc->id_blkno, blksiz);
160 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
161 dp->d_reclen = DIRBLKSIZ;
165 dp->d_name[0] = '\0';
168 idesc->id_loc += DIRBLKSIZ;
169 idesc->id_filesize -= DIRBLKSIZ;
173 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
175 dploc = idesc->id_loc;
176 dp = (struct direct *)(bp->b_un.b_buf + dploc);
177 idesc->id_loc += dp->d_reclen;
178 idesc->id_filesize -= dp->d_reclen;
179 if ((idesc->id_loc % DIRBLKSIZ) == 0)
181 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
182 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
183 dircheck(idesc, ndp) == 0) {
184 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
185 idesc->id_loc += size;
186 idesc->id_filesize -= size;
187 if (idesc->id_fix == IGNORE)
189 fix = dofix(idesc, "DIRECTORY CORRUPTED");
190 bp = getdirblk(idesc->id_blkno, blksiz);
191 dp = (struct direct *)(bp->b_un.b_buf + dploc);
192 dp->d_reclen += size;
200 * Verify that a directory entry is valid.
201 * This is a superset of the checks made in the kernel.
204 dircheck(struct inodesc *idesc, struct direct *dp)
212 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
213 if (dp->d_reclen == 0 ||
214 dp->d_reclen > spaceleft ||
215 (dp->d_reclen & 0x3) != 0)
219 size = DIRSIZ(0, dp);
220 namlen = dp->d_namlen;
222 if (dp->d_reclen < size ||
223 idesc->id_filesize < size ||
227 for (cp = dp->d_name, size = 0; size < namlen; size++)
228 if (*cp == '\0' || (*cp++ == '/'))
235 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
236 dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
242 direrror(ino_t ino, const char *errmesg)
245 fileerror(ino, ino, errmesg);
249 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
252 char pathbuf[MAXPATHLEN + 1];
254 pwarn("%s ", errmesg);
257 getpathname(pathbuf, cwd, ino);
258 if (ino < UFS_ROOTINO || ino > maxino) {
259 pfatal("NAME=%s\n", pathbuf);
265 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
268 pfatal("NAME=%s\n", pathbuf);
272 adjust(struct inodesc *idesc, int lcnt)
277 dp = ginode(idesc->id_number);
278 if (DIP(dp, di_nlink) == lcnt) {
280 * If we have not hit any unresolved problems, are running
281 * in preen mode, and are on a file system using soft updates,
282 * then just toss any partially allocated files.
284 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
285 clri(idesc, "UNREF", 1);
289 * The file system can be marked clean even if
290 * a file is not linked up, but is cleared.
291 * Hence, resolved should not be cleared when
292 * linkup is answered no, but clri is answered yes.
294 saveresolved = resolved;
295 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
296 resolved = saveresolved;
297 clri(idesc, "UNREF", 0);
301 * Account for the new reference created by linkup().
303 dp = ginode(idesc->id_number);
308 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
309 ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
310 pinode(idesc->id_number);
311 printf(" COUNT %d SHOULD BE %d",
312 DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
313 if (preen || usedsoftdep) {
316 pfatal("LINK COUNT INCREASING");
319 printf(" (ADJUSTED)\n");
321 if (preen || reply("ADJUST") == 1) {
322 if (bkgrdflag == 0) {
323 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
326 cmd.value = idesc->id_number;
329 printf("adjrefcnt ino %ld amt %lld\n",
331 (long long)cmd.size);
332 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
333 &cmd, sizeof cmd) == -1)
334 rwerror("ADJUST INODE", cmd.value);
341 mkentry(struct inodesc *idesc)
343 struct direct *dirp = idesc->id_dirp;
344 struct direct newent;
347 newent.d_namlen = strlen(idesc->id_name);
348 newlen = DIRSIZ(0, &newent);
349 if (dirp->d_ino != 0)
350 oldlen = DIRSIZ(0, dirp);
353 if (dirp->d_reclen - oldlen < newlen)
355 newent.d_reclen = dirp->d_reclen - oldlen;
356 dirp->d_reclen = oldlen;
357 dirp = (struct direct *)(((char *)dirp) + oldlen);
358 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
359 dirp->d_reclen = newent.d_reclen;
360 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
361 dirp->d_namlen = newent.d_namlen;
362 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
363 return (ALTERED|STOP);
367 chgino(struct inodesc *idesc)
369 struct direct *dirp = idesc->id_dirp;
371 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
373 dirp->d_ino = idesc->id_parent;
374 dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
375 return (ALTERED|STOP);
379 linkup(ino_t orphan, ino_t parentdir, char *name)
384 struct inodesc idesc;
385 char tempname[BUFSIZ];
387 memset(&idesc, 0, sizeof(struct inodesc));
389 lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
390 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
392 if (preen && DIP(dp, di_size) == 0)
394 if (cursnapshot != 0) {
395 pfatal("FILE LINKUP IN SNAPSHOT");
399 printf(" (RECONNECTED)\n");
401 if (reply("RECONNECT") == 0)
404 dp = ginode(UFS_ROOTINO);
405 idesc.id_name = strdup(lfname);
406 idesc.id_type = DATA;
407 idesc.id_func = findino;
408 idesc.id_number = UFS_ROOTINO;
409 if ((ckinode(dp, &idesc) & FOUND) != 0) {
410 lfdir = idesc.id_parent;
412 pwarn("NO lost+found DIRECTORY");
413 if (preen || reply("CREATE")) {
414 lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode);
416 if (makeentry(UFS_ROOTINO, lfdir,
420 printf(" (CREATED)\n");
422 freedir(lfdir, UFS_ROOTINO);
431 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
437 if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
438 pfatal("lost+found IS NOT A DIRECTORY");
439 if (reply("REALLOCATE") == 0)
442 if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) {
443 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
446 if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
447 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
451 idesc.id_type = ADDR;
452 idesc.id_func = pass4check;
453 idesc.id_number = oldlfdir;
454 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
455 inoinfo(oldlfdir)->ino_linkcnt = 0;
458 if (inoinfo(lfdir)->ino_state != DFOUND) {
459 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
462 (void)lftempname(tempname, orphan);
463 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
464 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
468 inoinfo(orphan)->ino_linkcnt--;
470 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
471 parentdir != (ino_t)-1)
472 (void)makeentry(orphan, lfdir, "..");
474 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
476 inoinfo(lfdir)->ino_linkcnt++;
477 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
478 if (parentdir != (ino_t)-1) {
479 printf("PARENT WAS I=%lu\n", (u_long)parentdir);
481 * The parent directory, because of the ordering
482 * guarantees, has had the link count incremented
483 * for the child, but no entry was made. This
484 * fixes the parent link count so that fsck does
485 * not need to be rerun.
487 inoinfo(parentdir)->ino_linkcnt++;
496 * fix an entry in a directory.
499 changeino(ino_t dir, const char *name, ino_t newnum)
501 struct inodesc idesc;
503 memset(&idesc, 0, sizeof(struct inodesc));
504 idesc.id_type = DATA;
505 idesc.id_func = chgino;
506 idesc.id_number = dir;
507 idesc.id_fix = DONTKNOW;
508 idesc.id_name = strdup(name);
509 idesc.id_parent = newnum; /* new value for name */
510 return (ckinode(ginode(dir), &idesc));
514 * make an entry in a directory
517 makeentry(ino_t parent, ino_t ino, const char *name)
520 struct inodesc idesc;
521 char pathbuf[MAXPATHLEN + 1];
523 if (parent < UFS_ROOTINO || parent >= maxino ||
524 ino < UFS_ROOTINO || ino >= maxino)
526 memset(&idesc, 0, sizeof(struct inodesc));
527 idesc.id_type = DATA;
528 idesc.id_func = mkentry;
529 idesc.id_number = parent;
530 idesc.id_parent = ino; /* this is the inode to enter */
531 idesc.id_fix = DONTKNOW;
532 idesc.id_name = strdup(name);
534 if (DIP(dp, di_size) % DIRBLKSIZ) {
535 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
538 if ((ckinode(dp, &idesc) & ALTERED) != 0)
540 getpathname(pathbuf, parent, parent);
542 if (expanddir(dp, pathbuf) == 0)
544 return (ckinode(dp, &idesc) & ALTERED);
548 * Attempt to expand the size of a directory
551 expanddir(union dinode *dp, char *name)
553 ufs2_daddr_t lastbn, newblk;
555 char *cp, firstblk[DIRBLKSIZ];
557 lastbn = lblkno(&sblock, DIP(dp, di_size));
558 if (lastbn >= UFS_NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
559 DIP(dp, di_size) == 0)
561 if ((newblk = allocblk(sblock.fs_frag)) == 0)
563 DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
564 DIP_SET(dp, di_db[lastbn], newblk);
565 DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
566 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
567 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
568 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
571 memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
572 bp = getdirblk(newblk, sblock.fs_bsize);
575 memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
576 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
577 cp < &bp->b_un.b_buf[sblock.fs_bsize];
579 memmove(cp, &emptydir, sizeof emptydir);
581 bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
582 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
585 memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
586 pwarn("NO SPACE LEFT IN %s", name);
588 printf(" (EXPANDED)\n");
589 else if (reply("EXPAND") == 0)
595 DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
596 DIP_SET(dp, di_db[lastbn + 1], 0);
597 DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
598 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
599 freeblk(newblk, sblock.fs_frag);
604 * allocate a new directory
607 allocdir(ino_t parent, ino_t request, int mode)
614 struct dirtemplate *dirp;
616 ino = allocino(request, IFDIR|mode);
619 dirp->dotdot_ino = parent;
621 bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
626 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
627 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
628 cp < &bp->b_un.b_buf[sblock.fs_fsize];
630 memmove(cp, &emptydir, sizeof emptydir);
632 DIP_SET(dp, di_nlink, 2);
634 if (ino == UFS_ROOTINO) {
635 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
639 if (!INO_IS_DVALID(parent)) {
644 inp = getinoinfo(ino);
645 inp->i_parent = parent;
646 inp->i_dotdot = parent;
647 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
648 if (inoinfo(ino)->ino_state == DSTATE) {
649 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
650 inoinfo(parent)->ino_linkcnt++;
653 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
659 * free a directory inode
662 freedir(ino_t ino, ino_t parent)
668 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
675 * generate a temporary name for the lost+found directory.
678 lftempname(char *bufp, ino_t ino)
685 for (in = maxino; in > 0; in /= 10)
691 *--cp = (in % 10) + '0';
699 * Get a directory block.
700 * Insure that it is held until another is requested.
702 static struct bufarea *
703 getdirblk(ufs2_daddr_t blkno, long size)
707 pdirbp->b_flags &= ~B_INUSE;
708 pdirbp = getdatablk(blkno, size, BT_DIRDATA);