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 #define VICE /* allow us to put our changes in at will */
25 #include <sys/param.h>
27 #include <sys/types.h>
30 #include <sys/vnode.h>
31 #include <sys/mount.h>
32 #include <ufs/inode.h>
39 #else /* AFS_OSF_ENV */
40 #ifdef AFS_VFSINCL_ENV
42 #include <sys/vnode.h>
44 #include <sys/fs/ufs_inode.h>
45 #include <sys/fs/ufs_fs.h>
47 #include <sys/fs/ufs_fsdir.h>
50 #include <ufs/inode.h>
53 #include <ufs/fsdir.h>
57 #else /* AFS_VFSINCL_ENV */
58 #include <sys/inode.h>
61 #define LONGFILENAMES 1
62 #include <sys/sysmacros.h>
65 #ifdef HAVE_USR_OLD_USR_INCLUDE_NDIR_H
66 #include </usr/old/usr/include/ndir.h>
76 #endif /* AFS_VFSINCL_ENV */
77 #endif /* AFS_OSF_ENV */
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
91 struct dirtemplate_lfn {
95 char dot_name[4]; /* must be multiple of 4 */
96 afs_uint32 dotdot_ino;
99 char dotdot_name[4]; /* ditto */
101 #define dirtemplate dirtemplate_lfn
104 #define MINDIRSIZE (sizeof (struct dirtemplate))
106 char *endpathname = &pathname[BUFSIZ - 2];
107 char *lfname = "lost+found";
109 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
110 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
112 struct direct *fsck_readdir();
113 struct bufarea *getdirblk();
115 descend(parentino, inumber)
116 struct inodesc *parentino;
120 struct inodesc curino;
122 memset(&curino, 0, sizeof(struct inodesc));
123 if (statemap[inumber] != DSTATE)
124 errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
125 #if defined(ACLS) && defined(AFS_HPUX_ENV)
127 * keep any continuation inode information
129 if (statemap[inumber] & HASCINODE)
130 statemap[inumber] = HASCINODE | DFOUND;
132 statemap[inumber] = DFOUND;
134 statemap[inumber] = DFOUND;
136 dp = ginode(inumber);
137 if (dp->di_size == 0) {
138 direrror(inumber, "ZERO LENGTH DIRECTORY");
139 if (reply("REMOVE") == 1)
140 #if defined(ACLS) && defined(AFS_HPUX_ENV)
142 * keep any continuation inode information
144 if (statemap[inumber] & HASCINODE)
145 statemap[inumber] = HASCINODE | DCLEAR;
147 statemap[inumber] = DCLEAR;
149 statemap[inumber] = DCLEAR;
153 if (dp->di_size < MINDIRSIZE) {
154 direrror(inumber, "DIRECTORY TOO SHORT");
155 dp->di_size = MINDIRSIZE;
156 if (reply("FIX") == 1)
159 #if !defined(AFS_HPUX_ENV)
160 /* For remaining 4.2 systems. We shouldn't convert
161 * dir size, since Unix 4.2 kernels won't maintain this, and we'll have a
162 * lot of spurious directory conversions scaring people */
163 if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
164 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", pathname,
165 dp->di_size, DIRBLKSIZ);
166 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
168 printf(" (ADJUSTED)\n");
169 if (preen || reply("ADJUST") == 1)
173 curino.id_type = DATA;
174 curino.id_func = parentino->id_func;
175 curino.id_parent = parentino->id_number;
176 curino.id_number = inumber;
177 (void)ckinode(dp, &curino);
178 if (curino.id_entryno < 2) {
179 direrror(inumber, "NULL DIRECTORY");
180 if (reply("REMOVE") == 1)
181 statemap[inumber] = DCLEAR;
186 struct inodesc *idesc;
192 char dbuf[DIRBLKSIZ];
194 if (idesc->id_type != DATA)
195 errexit("wrong type to dirscan %d\n", idesc->id_type);
196 if (idesc->id_entryno == 0 && (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
197 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
198 blksiz = idesc->id_numfrags * sblock.fs_fsize;
200 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
201 idesc->id_filesize -= blksiz;
205 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
206 dsize = dp->d_reclen;
207 memcpy(dbuf, (char *)dp, dsize);
208 idesc->id_dirp = (struct direct *)dbuf;
209 if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
210 bp = getdirblk(idesc->id_blkno, blksiz);
211 memcpy((char *)dp, dbuf, dsize);
218 return (idesc->id_filesize > 0 ? KEEPON : STOP);
222 * get next entry in a directory.
226 struct inodesc *idesc;
228 struct direct *dp, *ndp;
232 blksiz = idesc->id_numfrags * sblock.fs_fsize;
233 bp = getdirblk(idesc->id_blkno, blksiz);
234 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0
235 && idesc->id_loc < blksiz) {
236 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
237 if (dircheck(idesc, dp)) {
240 idesc->id_loc += DIRBLKSIZ;
241 idesc->id_filesize -= DIRBLKSIZ;
242 dp->d_reclen = DIRBLKSIZ;
245 dp->d_name[0] = '\0';
246 if (dofix(idesc, "DIRECTORY CORRUPTED"))
251 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
253 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
254 idesc->id_loc += dp->d_reclen;
255 idesc->id_filesize -= dp->d_reclen;
256 if ((idesc->id_loc % DIRBLKSIZ) == 0)
258 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
259 if (idesc->id_loc < blksiz && idesc->id_filesize > 0
260 && dircheck(idesc, ndp) == 0) {
261 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
262 dp->d_reclen += size;
263 idesc->id_loc += size;
264 idesc->id_filesize -= size;
265 if (dofix(idesc, "DIRECTORY CORRUPTED"))
272 * Verify that a directory entry is valid.
273 * This is a superset of the checks made in the kernel.
276 struct inodesc *idesc;
284 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
285 if (dp->d_ino < maxino && dp->d_reclen != 0 && dp->d_reclen <= spaceleft
286 && (dp->d_reclen & 0x3) == 0 && dp->d_reclen >= size
287 && idesc->id_filesize >= size && dp->d_namlen <= MAXNAMLEN) {
290 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
291 #if defined(Next) || defined(AFS_SUN5_ENV)
297 if (*cp == 0 || (*cp++ & 0200)) {
307 direrror(ino, errmesg)
313 pwarn("%s ", errmesg);
316 if (ino < ROOTINO || ino > maxino) {
317 pfatal("NAME=%s\n", pathname);
322 pfatal("%s=%s\n", (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE",
325 pfatal("NAME=%s\n", pathname);
329 struct inodesc *idesc;
334 dp = ginode(idesc->id_number);
335 if (dp->di_nlink == lcnt) {
336 if (linkup(idesc->id_number, (ino_t) 0) == 0)
337 clri(idesc, "UNREF", 0);
339 pwarn("LINK COUNT %s",
340 (lfdir == idesc->id_number) ? lfname : ((dp->di_mode & IFMT) ==
343 pinode(idesc->id_number);
344 printf(" COUNT %d SHOULD BE %d", dp->di_nlink, dp->di_nlink - lcnt);
348 pfatal("LINK COUNT INCREASING");
350 printf(" (ADJUSTED)\n");
352 if (preen || reply("ADJUST") == 1) {
353 dp->di_nlink -= lcnt;
360 struct inodesc *idesc;
362 struct direct *dirp = idesc->id_dirp;
363 struct direct newent;
366 newent.d_namlen = 11;
367 newlen = DIRSIZ(&newent);
368 if (dirp->d_ino != 0)
369 oldlen = DIRSIZ(dirp);
372 if (dirp->d_reclen - oldlen < newlen)
374 newent.d_reclen = dirp->d_reclen - oldlen;
375 dirp->d_reclen = oldlen;
376 dirp = (struct direct *)(((char *)dirp) + oldlen);
377 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
378 dirp->d_reclen = newent.d_reclen;
379 dirp->d_namlen = strlen(idesc->id_name);
380 memcpy(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1);
381 return (ALTERED | STOP);
385 struct inodesc *idesc;
387 struct direct *dirp = idesc->id_dirp;
389 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
391 dirp->d_ino = idesc->id_parent;
392 return (ALTERED | STOP);
395 linkup(orphan, parentdir)
402 struct inodesc idesc;
403 char tempname[BUFSIZ];
404 extern int pass4check();
406 memset(&idesc, 0, sizeof(struct inodesc));
408 lostdir = (dp->di_mode & IFMT) == IFDIR;
409 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
411 if (preen && dp->di_size == 0)
414 printf(" (RECONNECTED)\n");
415 else if (reply("RECONNECT") == 0)
421 dp = ginode(ROOTINO);
422 idesc.id_name = lfname;
423 idesc.id_type = DATA;
424 idesc.id_func = findino;
425 idesc.id_number = ROOTINO;
426 if ((ckinode(dp, &idesc) & FOUND) != 0) {
427 lfdir = idesc.id_parent;
429 pwarn("NO lost+found DIRECTORY");
430 if (preen || reply("CREATE")) {
431 lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
433 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
435 printf(" (CREATED)\n");
437 freedir(lfdir, ROOTINO);
446 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
452 if ((dp->di_mode & IFMT) != IFDIR) {
453 pfatal("lost+found IS NOT A DIRECTORY");
454 if (reply("REALLOCATE") == 0)
457 if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
458 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
461 idesc.id_type = DATA;
462 idesc.id_func = chgino;
463 idesc.id_number = ROOTINO;
464 idesc.id_parent = lfdir; /* new inumber for lost+found */
465 idesc.id_name = lfname;
466 if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) {
467 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
471 idesc.id_type = ADDR;
472 idesc.id_func = pass4check;
473 idesc.id_number = oldlfdir;
474 adjust(&idesc, lncntp[oldlfdir] + 1);
475 lncntp[oldlfdir] = 0;
478 if (statemap[lfdir] != DFOUND) {
479 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
482 len = strlen(lfname);
483 memcpy(pathp, lfname, len + 1);
485 len = lftempname(tempname, orphan);
486 if (makeentry(lfdir, orphan, tempname) == 0) {
487 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
493 memcpy(pathp, tempname, len + 1);
497 idesc.id_type = DATA;
498 idesc.id_func = chgino;
499 idesc.id_number = orphan;
500 idesc.id_fix = DONTKNOW;
501 idesc.id_name = "..";
502 idesc.id_parent = lfdir; /* new value for ".." */
503 (void)ckinode(dp, &idesc);
508 pwarn("DIR I=%u CONNECTED. ", orphan);
509 printf("PARENT WAS I=%u\n", parentdir);
517 * make an entry in a directory
519 makeentry(parent, ino, name)
524 struct inodesc idesc;
526 if (parent < ROOTINO || parent >= maxino || ino < ROOTINO
529 memset(&idesc, 0, sizeof(struct inodesc));
530 idesc.id_type = DATA;
531 idesc.id_func = mkentry;
532 idesc.id_number = parent;
533 idesc.id_parent = ino; /* this is the inode to enter */
534 idesc.id_fix = DONTKNOW;
535 idesc.id_name = name;
537 if (dp->di_size % DIRBLKSIZ) {
538 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
541 if ((ckinode(dp, &idesc) & ALTERED) != 0)
543 if (expanddir(dp) == 0)
545 return (ckinode(dp, &idesc) & ALTERED);
549 * Attempt to expand the size of a directory
554 daddr_t lastbn, newblk;
556 char *cp, firstblk[DIRBLKSIZ];
558 lastbn = lblkno(&sblock, dp->di_size);
559 if (lastbn >= NDADDR - 1)
561 if ((newblk = allocblk(sblock.fs_frag)) == 0)
563 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
564 dp->di_db[lastbn] = newblk;
565 dp->di_size += (UOFF_T) sblock.fs_bsize;
566 dp->di_blocks += btodb(sblock.fs_bsize);
567 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
570 memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
571 bp = getdirblk(newblk, sblock.fs_bsize);
574 memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
575 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
576 cp < &bp->b_un.b_buf[sblock.fs_bsize]; cp += DIRBLKSIZ)
577 memcpy(cp, (char *)&emptydir, sizeof emptydir);
579 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
582 memcpy(bp->b_un.b_buf, (char *)&emptydir, sizeof emptydir);
583 pwarn("NO SPACE LEFT IN %s", pathname);
585 printf(" (EXPANDED)\n");
586 else if (reply("EXPAND") == 0)
592 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
593 dp->di_db[lastbn + 1] = 0;
594 dp->di_size -= (UOFF_T) sblock.fs_bsize;
595 dp->di_blocks -= btodb(sblock.fs_bsize);
596 freeblk(newblk, sblock.fs_frag);
601 * allocate a new directory
603 allocdir(parent, request, mode)
604 ino_t parent, request;
612 ino = allocino(request, IFDIR | mode);
613 dirhead.dot_ino = ino;
614 dirhead.dotdot_ino = parent;
616 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
621 memcpy(bp->b_un.b_buf, (char *)&dirhead, sizeof dirhead);
622 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
623 cp < &bp->b_un.b_buf[sblock.fs_fsize]; cp += DIRBLKSIZ)
624 memcpy(cp, (char *)&emptydir, sizeof emptydir);
629 if (!inocached(ino)) {
631 printf("inode %d added to directory cache\n", ino);
635 * re-using an old directory inode
637 inp = getinoinfo(ino);
638 inp->i_isize = dp->di_size;
639 inp->i_numblks = dp->di_blocks * sizeof(daddr_t);
640 inp->i_parent = parent;
641 memcpy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
642 (int)inp->i_numblks);
645 if (ino == ROOTINO) {
646 lncntp[ino] = dp->di_nlink;
649 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
653 statemap[ino] = statemap[parent];
654 if (statemap[ino] == DSTATE) {
655 lncntp[ino] = dp->di_nlink;
665 * free a directory inode
681 * generate a temporary name for the lost+found directory.
683 lftempname(bufp, ino)
692 for (in = maxino; in > 0; in /= 10)
698 *--cp = (in % 10) + '0';
706 * Get a directory block.
707 * Insure that it is held until another is requested.
710 getdirblk(blkno, size)
715 mlk_pbp->b_flags &= ~B_INUSE;
716 mlk_pbp = getdatablk(blkno, size);