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.
19 static char sccsid[] = "@(#)dir.c 5.10 (Berkeley) 2/1/90";
22 #include <afs/param.h>
23 #define VICE /* allow us to put our changes in at will */
26 #include <sys/param.h>
28 #include <sys/types.h>
31 #include <sys/vnode.h>
32 #include <sys/mount.h>
33 #include <ufs/inode.h>
40 #else /* AFS_OSF_ENV */
41 #ifdef AFS_VFSINCL_ENV
43 #include <sys/vnode.h>
45 #include <sys/fs/ufs_inode.h>
46 #include <sys/fs/ufs_fs.h>
48 #include <sys/fs/ufs_fsdir.h>
51 #include <ufs/inode.h>
54 #include <ufs/fsdir.h>
58 #else /* AFS_VFSINCL_ENV */
59 #include <sys/inode.h>
62 #define LONGFILENAMES 1
63 #include <sys/sysmacros.h>
73 #endif /* AFS_VFSINCL_ENV */
74 #endif /* AFS_OSF_ENV */
78 #include <sys/mnttab.h>
79 #include <sys/mntent.h>
88 struct dirtemplate_lfn {
92 char dot_name[4]; /* must be multiple of 4 */
93 afs_uint32 dotdot_ino;
96 char dotdot_name[4]; /* ditto */
98 #define dirtemplate dirtemplate_lfn
101 #define MINDIRSIZE (sizeof (struct dirtemplate))
103 char *endpathname = &pathname[BUFSIZ - 2];
104 char *lfname = "lost+found";
106 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
107 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
109 struct direct *fsck_readdir();
110 struct bufarea *getdirblk();
112 descend(parentino, inumber)
113 struct inodesc *parentino;
116 register struct dinode *dp;
117 struct inodesc curino;
119 bzero((char *)&curino, sizeof(struct inodesc));
120 if (statemap[inumber] != DSTATE)
121 errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
122 #if defined(ACLS) && defined(AFS_HPUX_ENV)
124 * keep any continuation inode information
126 if (statemap[inumber] & HASCINODE)
127 statemap[inumber] = HASCINODE|DFOUND;
129 statemap[inumber] = DFOUND;
131 statemap[inumber] = DFOUND;
133 dp = ginode(inumber);
134 if (dp->di_size == 0) {
135 direrror(inumber, "ZERO LENGTH DIRECTORY");
136 if (reply("REMOVE") == 1)
137 #if defined(ACLS) && defined(AFS_HPUX_ENV)
139 * keep any continuation inode information
141 if (statemap[inumber] & HASCINODE)
142 statemap[inumber] = HASCINODE|DCLEAR;
144 statemap[inumber] = DCLEAR;
146 statemap[inumber] = DCLEAR;
150 if (dp->di_size < MINDIRSIZE) {
151 direrror(inumber, "DIRECTORY TOO SHORT");
152 dp->di_size = MINDIRSIZE;
153 if (reply("FIX") == 1)
156 #if !defined(AFS_HPUX_ENV)
157 /* For remaining 4.2 systems. We shouldn't convert
158 dir size, since Unix 4.2 kernels won't maintain this, and we'll have a
159 lot of spurious directory conversions scaring people */
160 if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
161 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
162 pathname, dp->di_size, DIRBLKSIZ);
163 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
165 printf(" (ADJUSTED)\n");
166 if (preen || reply("ADJUST") == 1)
170 curino.id_type = DATA;
171 curino.id_func = parentino->id_func;
172 curino.id_parent = parentino->id_number;
173 curino.id_number = inumber;
174 (void)ckinode(dp, &curino);
175 if (curino.id_entryno < 2) {
176 direrror(inumber, "NULL DIRECTORY");
177 if (reply("REMOVE") == 1)
178 statemap[inumber] = DCLEAR;
183 register struct inodesc *idesc;
185 register struct direct *dp;
186 register struct bufarea *bp;
189 char dbuf[DIRBLKSIZ];
191 if (idesc->id_type != DATA)
192 errexit("wrong type to dirscan %d\n", idesc->id_type);
193 if (idesc->id_entryno == 0 &&
194 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
195 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
196 blksiz = idesc->id_numfrags * sblock.fs_fsize;
198 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
199 idesc->id_filesize -= blksiz;
203 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
204 dsize = dp->d_reclen;
205 bcopy((char *)dp, dbuf, dsize);
206 idesc->id_dirp = (struct direct *)dbuf;
207 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
208 bp = getdirblk(idesc->id_blkno, blksiz);
209 bcopy(dbuf, (char *)dp, dsize);
216 return (idesc->id_filesize > 0 ? KEEPON : STOP);
220 * get next entry in a directory.
224 register struct inodesc *idesc;
226 register struct direct *dp, *ndp;
227 register struct bufarea *bp;
230 blksiz = idesc->id_numfrags * sblock.fs_fsize;
231 bp = getdirblk(idesc->id_blkno, blksiz);
232 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
233 idesc->id_loc < blksiz) {
234 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
235 if (dircheck(idesc, dp)) {
238 idesc->id_loc += DIRBLKSIZ;
239 idesc->id_filesize -= DIRBLKSIZ;
240 dp->d_reclen = DIRBLKSIZ;
243 dp->d_name[0] = '\0';
244 if (dofix(idesc, "DIRECTORY CORRUPTED"))
249 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
251 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
252 idesc->id_loc += dp->d_reclen;
253 idesc->id_filesize -= dp->d_reclen;
254 if ((idesc->id_loc % DIRBLKSIZ) == 0)
256 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
257 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
258 dircheck(idesc, ndp) == 0) {
259 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
260 dp->d_reclen += size;
261 idesc->id_loc += size;
262 idesc->id_filesize -= size;
263 if (dofix(idesc, "DIRECTORY CORRUPTED"))
270 * Verify that a directory entry is valid.
271 * This is a superset of the checks made in the kernel.
274 struct inodesc *idesc;
275 register struct direct *dp;
282 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
283 if (dp->d_ino < maxino &&
285 dp->d_reclen <= spaceleft &&
286 (dp->d_reclen & 0x3) == 0 &&
287 dp->d_reclen >= size &&
288 idesc->id_filesize >= size &&
289 dp->d_namlen <= MAXNAMLEN) {
292 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
293 #if defined(Next) || defined(AFS_SUN5_ENV)
299 if (*cp == 0 || (*cp++ & 0200)) {
309 direrror(ino, errmesg)
313 register struct dinode *dp;
315 pwarn("%s ", errmesg);
318 if (ino < ROOTINO || ino > maxino) {
319 pfatal("NAME=%s\n", pathname);
325 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathname);
327 pfatal("NAME=%s\n", pathname);
331 register struct inodesc *idesc;
334 register struct dinode *dp;
336 dp = ginode(idesc->id_number);
337 if (dp->di_nlink == lcnt) {
338 if (linkup(idesc->id_number, (ino_t)0) == 0)
339 clri(idesc, "UNREF", 0);
341 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
342 ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
343 pinode(idesc->id_number);
344 printf(" COUNT %d SHOULD BE %d",
345 dp->di_nlink, dp->di_nlink - lcnt);
349 pfatal("LINK COUNT INCREASING");
351 printf(" (ADJUSTED)\n");
353 if (preen || reply("ADJUST") == 1) {
354 dp->di_nlink -= lcnt;
361 struct inodesc *idesc;
363 register struct direct *dirp = idesc->id_dirp;
364 struct direct newent;
367 newent.d_namlen = 11;
368 newlen = DIRSIZ(&newent);
369 if (dirp->d_ino != 0)
370 oldlen = DIRSIZ(dirp);
373 if (dirp->d_reclen - oldlen < newlen)
375 newent.d_reclen = dirp->d_reclen - oldlen;
376 dirp->d_reclen = oldlen;
377 dirp = (struct direct *)(((char *)dirp) + oldlen);
378 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
379 dirp->d_reclen = newent.d_reclen;
380 dirp->d_namlen = strlen(idesc->id_name);
381 bcopy(idesc->id_name, dirp->d_name, (int)dirp->d_namlen + 1);
382 return (ALTERED|STOP);
386 struct inodesc *idesc;
388 register struct direct *dirp = idesc->id_dirp;
390 if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
392 dirp->d_ino = idesc->id_parent;
393 return (ALTERED|STOP);
396 linkup(orphan, parentdir)
400 register struct dinode *dp;
403 struct inodesc idesc;
404 char tempname[BUFSIZ];
405 extern int pass4check();
407 bzero((char *)&idesc, sizeof(struct inodesc));
409 lostdir = (dp->di_mode & IFMT) == IFDIR;
410 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
412 if (preen && dp->di_size == 0)
415 printf(" (RECONNECTED)\n");
417 if (reply("RECONNECT") == 0)
423 dp = ginode(ROOTINO);
424 idesc.id_name = lfname;
425 idesc.id_type = DATA;
426 idesc.id_func = findino;
427 idesc.id_number = ROOTINO;
428 if ((ckinode(dp, &idesc) & FOUND) != 0) {
429 lfdir = idesc.id_parent;
431 pwarn("NO lost+found DIRECTORY");
432 if (preen || reply("CREATE")) {
433 lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
435 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
437 printf(" (CREATED)\n");
439 freedir(lfdir, ROOTINO);
448 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
454 if ((dp->di_mode & IFMT) != IFDIR) {
455 pfatal("lost+found IS NOT A DIRECTORY");
456 if (reply("REALLOCATE") == 0)
459 if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
460 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
463 idesc.id_type = DATA;
464 idesc.id_func = chgino;
465 idesc.id_number = ROOTINO;
466 idesc.id_parent = lfdir; /* new inumber for lost+found */
467 idesc.id_name = lfname;
468 if ((ckinode(ginode(ROOTINO), &idesc) & 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, lncntp[oldlfdir] + 1);
477 lncntp[oldlfdir] = 0;
480 if (statemap[lfdir] != DFOUND) {
481 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
484 len = strlen(lfname);
485 bcopy(lfname, pathp, len + 1);
487 len = lftempname(tempname, orphan);
488 if (makeentry(lfdir, orphan, tempname) == 0) {
489 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
495 bcopy(tempname, pathp, len + 1);
499 idesc.id_type = DATA;
500 idesc.id_func = chgino;
501 idesc.id_number = orphan;
502 idesc.id_fix = DONTKNOW;
503 idesc.id_name = "..";
504 idesc.id_parent = lfdir; /* new value for ".." */
505 (void)ckinode(dp, &idesc);
510 pwarn("DIR I=%u CONNECTED. ", orphan);
511 printf("PARENT WAS I=%u\n", parentdir);
519 * make an entry in a directory
521 makeentry(parent, ino, name)
526 struct inodesc idesc;
528 if (parent < ROOTINO || parent >= maxino ||
529 ino < ROOTINO || ino >= maxino)
531 bzero((char *)&idesc, sizeof(struct inodesc));
532 idesc.id_type = DATA;
533 idesc.id_func = mkentry;
534 idesc.id_number = parent;
535 idesc.id_parent = ino; /* this is the inode to enter */
536 idesc.id_fix = DONTKNOW;
537 idesc.id_name = name;
539 if (dp->di_size % DIRBLKSIZ) {
540 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
543 if ((ckinode(dp, &idesc) & ALTERED) != 0)
545 if (expanddir(dp) == 0)
547 return (ckinode(dp, &idesc) & ALTERED);
551 * Attempt to expand the size of a directory
554 register struct dinode *dp;
556 daddr_t lastbn, newblk;
557 register struct bufarea *bp;
558 char *cp, firstblk[DIRBLKSIZ];
560 lastbn = lblkno(&sblock, dp->di_size);
561 if (lastbn >= NDADDR - 1)
563 if ((newblk = allocblk(sblock.fs_frag)) == 0)
565 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
566 dp->di_db[lastbn] = newblk;
567 dp->di_size += (UOFF_T)sblock.fs_bsize;
568 dp->di_blocks += btodb(sblock.fs_bsize);
569 bp = getdirblk(dp->di_db[lastbn + 1],
570 dblksize(&sblock, dp, lastbn + 1));
573 bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
574 bp = getdirblk(newblk, sblock.fs_bsize);
577 bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
578 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
579 cp < &bp->b_un.b_buf[sblock.fs_bsize];
581 bcopy((char *)&emptydir, cp, sizeof emptydir);
583 bp = getdirblk(dp->di_db[lastbn + 1],
584 dblksize(&sblock, dp, lastbn + 1));
587 bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir);
588 pwarn("NO SPACE LEFT IN %s", pathname);
590 printf(" (EXPANDED)\n");
591 else if (reply("EXPAND") == 0)
597 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
598 dp->di_db[lastbn + 1] = 0;
599 dp->di_size -= (UOFF_T)sblock.fs_bsize;
600 dp->di_blocks -= btodb(sblock.fs_bsize);
601 freeblk(newblk, sblock.fs_frag);
606 * allocate a new directory
608 allocdir(parent, request, mode)
609 ino_t parent, request;
615 register struct bufarea *bp;
617 ino = allocino(request, IFDIR|mode);
618 dirhead.dot_ino = ino;
619 dirhead.dotdot_ino = parent;
621 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
626 bcopy((char *)&dirhead, bp->b_un.b_buf, sizeof dirhead);
627 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
628 cp < &bp->b_un.b_buf[sblock.fs_fsize];
630 bcopy((char *)&emptydir, cp, sizeof emptydir);
635 if (!inocached(ino)) {
637 printf("inode %d added to directory cache\n",
642 * re-using an old directory inode
644 inp = getinoinfo(ino);
645 inp->i_isize = dp->di_size;
646 inp->i_numblks = dp->di_blocks * sizeof(daddr_t);
647 inp->i_parent = parent;
648 bcopy((char *)&dp->di_db[0], (char *)&inp->i_blks[0],
649 (int)inp->i_numblks);
652 if (ino == ROOTINO) {
653 lncntp[ino] = dp->di_nlink;
656 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
660 statemap[ino] = statemap[parent];
661 if (statemap[ino] == DSTATE) {
662 lncntp[ino] = dp->di_nlink;
672 * free a directory inode
688 * generate a temporary name for the lost+found directory.
690 lftempname(bufp, ino)
699 for (in = maxino; in > 0; in /= 10)
705 *--cp = (in % 10) + '0';
713 * Get a directory block.
714 * Insure that it is held until another is requested.
717 getdirblk(blkno, size)
722 mlk_pbp->b_flags &= ~B_INUSE;
723 mlk_pbp = getdatablk(blkno, size);