2 * Copyright (c) 1980, 1986 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 #include <afsconfig.h>
19 #include <afs/param.h>
22 /* We need the old directory type headers (included below), so don't include
23 * the normal dirent.h, or it will conflict. */
25 # include <sys/inode.h>
26 # define LONGFILENAMES 1
27 # include <sys/sysmacros.h>
30 # ifdef HAVE_USR_OLD_USR_INCLUDE_NDIR_H
31 # include </usr/old/usr/include/ndir.h>
41 #define VICE /* allow us to put our changes in at will */
43 #ifdef AFS_VFSINCL_ENV
45 #include <sys/vnode.h>
47 #include <sys/fs/ufs_inode.h>
48 #include <sys/fs/ufs_fs.h>
50 #include <sys/fs/ufs_fsdir.h>
53 #include <ufs/inode.h>
56 #include <ufs/fsdir.h>
60 #else /* AFS_VFSINCL_ENV */
61 #include <sys/inode.h>
68 #endif /* AFS_VFSINCL_ENV */
72 #include <sys/mnttab.h>
73 #include <sys/mntent.h>
82 struct dirtemplate_lfn {
86 char dot_name[4]; /* must be multiple of 4 */
87 afs_uint32 dotdot_ino;
90 char dotdot_name[4]; /* ditto */
92 #define dirtemplate dirtemplate_lfn
95 #define MINDIRSIZE (sizeof (struct dirtemplate))
97 char *endpathname = &pathname[BUFSIZ - 2];
98 char *lfname = "lost+found";
100 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
101 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
103 struct direct *fsck_readdir();
104 struct bufarea *getdirblk();
106 descend(parentino, inumber)
107 struct inodesc *parentino;
111 struct inodesc curino;
113 memset(&curino, 0, sizeof(struct inodesc));
114 if (statemap[inumber] != DSTATE)
115 errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
116 #if defined(ACLS) && defined(AFS_HPUX_ENV)
118 * keep any continuation inode information
120 if (statemap[inumber] & HASCINODE)
121 statemap[inumber] = HASCINODE | DFOUND;
123 statemap[inumber] = DFOUND;
125 statemap[inumber] = DFOUND;
127 dp = ginode(inumber);
128 if (dp->di_size == 0) {
129 direrror(inumber, "ZERO LENGTH DIRECTORY");
130 if (reply("REMOVE") == 1)
131 #if defined(ACLS) && defined(AFS_HPUX_ENV)
133 * keep any continuation inode information
135 if (statemap[inumber] & HASCINODE)
136 statemap[inumber] = HASCINODE | DCLEAR;
138 statemap[inumber] = DCLEAR;
140 statemap[inumber] = DCLEAR;
144 if (dp->di_size < MINDIRSIZE) {
145 direrror(inumber, "DIRECTORY TOO SHORT");
146 dp->di_size = MINDIRSIZE;
147 if (reply("FIX") == 1)
150 #if !defined(AFS_HPUX_ENV)
151 /* For remaining 4.2 systems. We shouldn't convert
152 * dir size, since Unix 4.2 kernels won't maintain this, and we'll have a
153 * lot of spurious directory conversions scaring people */
154 if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
155 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", pathname,
156 dp->di_size, DIRBLKSIZ);
157 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
159 printf(" (ADJUSTED)\n");
160 if (preen || reply("ADJUST") == 1)
164 curino.id_type = DATA;
165 curino.id_func = parentino->id_func;
166 curino.id_parent = parentino->id_number;
167 curino.id_number = inumber;
168 (void)ckinode(dp, &curino);
169 if (curino.id_entryno < 2) {
170 direrror(inumber, "NULL DIRECTORY");
171 if (reply("REMOVE") == 1)
172 statemap[inumber] = DCLEAR;
177 struct inodesc *idesc;
183 char dbuf[DIRBLKSIZ];
185 if (idesc->id_type != DATA)
186 errexit("wrong type to dirscan %d\n", idesc->id_type);
187 if (idesc->id_entryno == 0 && (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
188 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
189 blksiz = idesc->id_numfrags * sblock.fs_fsize;
191 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
192 idesc->id_filesize -= blksiz;
196 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
197 dsize = dp->d_reclen;
198 memcpy(dbuf, (char *)dp, dsize);
199 idesc->id_dirp = (struct direct *)dbuf;
200 if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
201 bp = getdirblk(idesc->id_blkno, blksiz);
202 memcpy((char *)dp, dbuf, dsize);
209 return (idesc->id_filesize > 0 ? KEEPON : STOP);
213 * get next entry in a directory.
217 struct inodesc *idesc;
219 struct direct *dp, *ndp;
223 blksiz = idesc->id_numfrags * sblock.fs_fsize;
224 bp = getdirblk(idesc->id_blkno, blksiz);
225 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0
226 && idesc->id_loc < blksiz) {
227 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
228 if (dircheck(idesc, dp)) {
231 idesc->id_loc += DIRBLKSIZ;
232 idesc->id_filesize -= DIRBLKSIZ;
233 dp->d_reclen = DIRBLKSIZ;
236 dp->d_name[0] = '\0';
237 if (dofix(idesc, "DIRECTORY CORRUPTED"))
242 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
244 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
245 idesc->id_loc += dp->d_reclen;
246 idesc->id_filesize -= dp->d_reclen;
247 if ((idesc->id_loc % DIRBLKSIZ) == 0)
249 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
250 if (idesc->id_loc < blksiz && idesc->id_filesize > 0
251 && dircheck(idesc, ndp) == 0) {
252 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
253 dp->d_reclen += size;
254 idesc->id_loc += size;
255 idesc->id_filesize -= size;
256 if (dofix(idesc, "DIRECTORY CORRUPTED"))
263 * Verify that a directory entry is valid.
264 * This is a superset of the checks made in the kernel.
267 struct inodesc *idesc;
275 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
276 if (dp->d_ino < maxino && dp->d_reclen != 0 && dp->d_reclen <= spaceleft
277 && (dp->d_reclen & 0x3) == 0 && dp->d_reclen >= size
278 && idesc->id_filesize >= size && dp->d_namlen <= MAXNAMLEN) {
281 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
282 #if defined(Next) || defined(AFS_SUN5_ENV)
288 if (*cp == 0 || (*cp++ & 0200)) {
298 direrror(ino, errmesg)
304 pwarn("%s ", errmesg);
307 if (ino < ROOTINO || ino > maxino) {
308 pfatal("NAME=%s\n", pathname);
313 pfatal("%s=%s\n", (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE",
316 pfatal("NAME=%s\n", pathname);
320 struct inodesc *idesc;
325 dp = ginode(idesc->id_number);
326 if (dp->di_nlink == lcnt) {
327 if (linkup(idesc->id_number, (ino_t) 0) == 0)
328 clri(idesc, "UNREF", 0);
330 pwarn("LINK COUNT %s",
331 (lfdir == idesc->id_number) ? lfname : ((dp->di_mode & IFMT) ==
334 pinode(idesc->id_number);
335 printf(" COUNT %d SHOULD BE %d", dp->di_nlink, dp->di_nlink - lcnt);
339 pfatal("LINK COUNT INCREASING");
341 printf(" (ADJUSTED)\n");
343 if (preen || reply("ADJUST") == 1) {
344 dp->di_nlink -= lcnt;
351 struct inodesc *idesc;
353 struct direct *dirp = idesc->id_dirp;
354 struct direct newent;
357 newent.d_namlen = 11;
358 newlen = DIRSIZ(&newent);
359 if (dirp->d_ino != 0)
360 oldlen = DIRSIZ(dirp);
363 if (dirp->d_reclen - oldlen < newlen)
365 newent.d_reclen = dirp->d_reclen - oldlen;
366 dirp->d_reclen = oldlen;
367 dirp = (struct direct *)(((char *)dirp) + oldlen);
368 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
369 dirp->d_reclen = newent.d_reclen;
370 dirp->d_namlen = strlen(idesc->id_name);
371 memcpy(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1);
372 return (ALTERED | STOP);
376 struct inodesc *idesc;
378 struct direct *dirp = idesc->id_dirp;
380 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
382 dirp->d_ino = idesc->id_parent;
383 return (ALTERED | STOP);
386 linkup(orphan, parentdir)
393 struct inodesc idesc;
394 char tempname[BUFSIZ];
395 extern int pass4check();
397 memset(&idesc, 0, sizeof(struct inodesc));
399 lostdir = (dp->di_mode & IFMT) == IFDIR;
400 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
402 if (preen && dp->di_size == 0)
405 printf(" (RECONNECTED)\n");
406 else if (reply("RECONNECT") == 0)
412 dp = ginode(ROOTINO);
413 idesc.id_name = lfname;
414 idesc.id_type = DATA;
415 idesc.id_func = findino;
416 idesc.id_number = ROOTINO;
417 if ((ckinode(dp, &idesc) & FOUND) != 0) {
418 lfdir = idesc.id_parent;
420 pwarn("NO lost+found DIRECTORY");
421 if (preen || reply("CREATE")) {
422 lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
424 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
426 printf(" (CREATED)\n");
428 freedir(lfdir, ROOTINO);
437 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
443 if ((dp->di_mode & IFMT) != IFDIR) {
444 pfatal("lost+found IS NOT A DIRECTORY");
445 if (reply("REALLOCATE") == 0)
448 if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
449 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
452 idesc.id_type = DATA;
453 idesc.id_func = chgino;
454 idesc.id_number = ROOTINO;
455 idesc.id_parent = lfdir; /* new inumber for lost+found */
456 idesc.id_name = lfname;
457 if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) {
458 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
462 idesc.id_type = ADDR;
463 idesc.id_func = pass4check;
464 idesc.id_number = oldlfdir;
465 adjust(&idesc, lncntp[oldlfdir] + 1);
466 lncntp[oldlfdir] = 0;
469 if (statemap[lfdir] != DFOUND) {
470 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
473 len = strlen(lfname);
474 memcpy(pathp, lfname, len + 1);
476 len = lftempname(tempname, orphan);
477 if (makeentry(lfdir, orphan, tempname) == 0) {
478 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
484 memcpy(pathp, tempname, len + 1);
488 idesc.id_type = DATA;
489 idesc.id_func = chgino;
490 idesc.id_number = orphan;
491 idesc.id_fix = DONTKNOW;
492 idesc.id_name = "..";
493 idesc.id_parent = lfdir; /* new value for ".." */
494 (void)ckinode(dp, &idesc);
499 pwarn("DIR I=%u CONNECTED. ", orphan);
500 printf("PARENT WAS I=%u\n", parentdir);
508 * make an entry in a directory
510 makeentry(parent, ino, name)
515 struct inodesc idesc;
517 if (parent < ROOTINO || parent >= maxino || ino < ROOTINO
520 memset(&idesc, 0, sizeof(struct inodesc));
521 idesc.id_type = DATA;
522 idesc.id_func = mkentry;
523 idesc.id_number = parent;
524 idesc.id_parent = ino; /* this is the inode to enter */
525 idesc.id_fix = DONTKNOW;
526 idesc.id_name = name;
528 if (dp->di_size % DIRBLKSIZ) {
529 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
532 if ((ckinode(dp, &idesc) & ALTERED) != 0)
534 if (expanddir(dp) == 0)
536 return (ckinode(dp, &idesc) & ALTERED);
540 * Attempt to expand the size of a directory
545 daddr_t lastbn, newblk;
547 char *cp, firstblk[DIRBLKSIZ];
549 lastbn = lblkno(&sblock, dp->di_size);
550 if (lastbn >= NDADDR - 1)
552 if ((newblk = allocblk(sblock.fs_frag)) == 0)
554 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
555 dp->di_db[lastbn] = newblk;
556 dp->di_size += (UOFF_T) sblock.fs_bsize;
557 dp->di_blocks += btodb(sblock.fs_bsize);
558 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
561 memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
562 bp = getdirblk(newblk, sblock.fs_bsize);
565 memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
566 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
567 cp < &bp->b_un.b_buf[sblock.fs_bsize]; cp += DIRBLKSIZ)
568 memcpy(cp, (char *)&emptydir, sizeof emptydir);
570 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
573 memcpy(bp->b_un.b_buf, (char *)&emptydir, sizeof emptydir);
574 pwarn("NO SPACE LEFT IN %s", pathname);
576 printf(" (EXPANDED)\n");
577 else if (reply("EXPAND") == 0)
583 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
584 dp->di_db[lastbn + 1] = 0;
585 dp->di_size -= (UOFF_T) sblock.fs_bsize;
586 dp->di_blocks -= btodb(sblock.fs_bsize);
587 freeblk(newblk, sblock.fs_frag);
592 * allocate a new directory
594 allocdir(parent, request, mode)
595 ino_t parent, request;
603 ino = allocino(request, IFDIR | mode);
604 dirhead.dot_ino = ino;
605 dirhead.dotdot_ino = parent;
607 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
612 memcpy(bp->b_un.b_buf, (char *)&dirhead, sizeof dirhead);
613 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
614 cp < &bp->b_un.b_buf[sblock.fs_fsize]; cp += DIRBLKSIZ)
615 memcpy(cp, (char *)&emptydir, sizeof emptydir);
620 if (!inocached(ino)) {
622 printf("inode %d added to directory cache\n", ino);
626 * re-using an old directory inode
628 inp = getinoinfo(ino);
629 inp->i_isize = dp->di_size;
630 inp->i_numblks = dp->di_blocks * sizeof(daddr_t);
631 inp->i_parent = parent;
632 memcpy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
633 (int)inp->i_numblks);
636 if (ino == ROOTINO) {
637 lncntp[ino] = dp->di_nlink;
640 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
644 statemap[ino] = statemap[parent];
645 if (statemap[ino] == DSTATE) {
646 lncntp[ino] = dp->di_nlink;
656 * free a directory inode
672 * generate a temporary name for the lost+found directory.
674 lftempname(bufp, ino)
683 for (in = maxino; in > 0; in /= 10)
689 *--cp = (in % 10) + '0';
697 * Get a directory block.
698 * Insure that it is held until another is requested.
701 getdirblk(blkno, size)
706 mlk_pbp->b_flags &= ~B_INUSE;
707 mlk_pbp = getdatablk(blkno, size);