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>
24 #define VICE /* allow us to put our changes in at will */
27 #include <sys/param.h>
29 #include <sys/types.h>
32 #include <sys/vnode.h>
33 #include <sys/mount.h>
34 #include <ufs/inode.h>
41 #else /* AFS_OSF_ENV */
42 #ifdef AFS_VFSINCL_ENV
44 #include <sys/vnode.h>
46 #include <sys/fs/ufs_inode.h>
47 #include <sys/fs/ufs_fs.h>
49 #include <sys/fs/ufs_fsdir.h>
52 #include <ufs/inode.h>
55 #include <ufs/fsdir.h>
59 #else /* AFS_VFSINCL_ENV */
60 #include <sys/inode.h>
63 #define LONGFILENAMES 1
64 #include <sys/sysmacros.h>
74 #endif /* AFS_VFSINCL_ENV */
75 #endif /* AFS_OSF_ENV */
79 #include <sys/mnttab.h>
80 #include <sys/mntent.h>
89 struct dirtemplate_lfn {
93 char dot_name[4]; /* must be multiple of 4 */
94 afs_uint32 dotdot_ino;
97 char dotdot_name[4]; /* ditto */
99 #define dirtemplate dirtemplate_lfn
102 #define MINDIRSIZE (sizeof (struct dirtemplate))
104 char *endpathname = &pathname[BUFSIZ - 2];
105 char *lfname = "lost+found";
107 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
108 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
110 struct direct *fsck_readdir();
111 struct bufarea *getdirblk();
113 descend(parentino, inumber)
114 struct inodesc *parentino;
117 register struct dinode *dp;
118 struct inodesc curino;
120 memset((char *)&curino, 0, sizeof(struct inodesc));
121 if (statemap[inumber] != DSTATE)
122 errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
123 #if defined(ACLS) && defined(AFS_HPUX_ENV)
125 * keep any continuation inode information
127 if (statemap[inumber] & HASCINODE)
128 statemap[inumber] = HASCINODE | DFOUND;
130 statemap[inumber] = DFOUND;
132 statemap[inumber] = DFOUND;
134 dp = ginode(inumber);
135 if (dp->di_size == 0) {
136 direrror(inumber, "ZERO LENGTH DIRECTORY");
137 if (reply("REMOVE") == 1)
138 #if defined(ACLS) && defined(AFS_HPUX_ENV)
140 * keep any continuation inode information
142 if (statemap[inumber] & HASCINODE)
143 statemap[inumber] = HASCINODE | DCLEAR;
145 statemap[inumber] = DCLEAR;
147 statemap[inumber] = DCLEAR;
151 if (dp->di_size < MINDIRSIZE) {
152 direrror(inumber, "DIRECTORY TOO SHORT");
153 dp->di_size = MINDIRSIZE;
154 if (reply("FIX") == 1)
157 #if !defined(AFS_HPUX_ENV)
158 /* For remaining 4.2 systems. We shouldn't convert
159 * dir size, since Unix 4.2 kernels won't maintain this, and we'll have a
160 * lot of spurious directory conversions scaring people */
161 if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
162 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", pathname,
163 dp->di_size, DIRBLKSIZ);
164 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
166 printf(" (ADJUSTED)\n");
167 if (preen || reply("ADJUST") == 1)
171 curino.id_type = DATA;
172 curino.id_func = parentino->id_func;
173 curino.id_parent = parentino->id_number;
174 curino.id_number = inumber;
175 (void)ckinode(dp, &curino);
176 if (curino.id_entryno < 2) {
177 direrror(inumber, "NULL DIRECTORY");
178 if (reply("REMOVE") == 1)
179 statemap[inumber] = DCLEAR;
184 register struct inodesc *idesc;
186 register struct direct *dp;
187 register struct bufarea *bp;
190 char dbuf[DIRBLKSIZ];
192 if (idesc->id_type != DATA)
193 errexit("wrong type to dirscan %d\n", idesc->id_type);
194 if (idesc->id_entryno == 0 && (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 memcpy(dbuf, (char *)dp, dsize);
206 idesc->id_dirp = (struct direct *)dbuf;
207 if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
208 bp = getdirblk(idesc->id_blkno, blksiz);
209 memcpy((char *)dp, dbuf, 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 && dp->d_reclen != 0 && dp->d_reclen <= spaceleft
284 && (dp->d_reclen & 0x3) == 0 && dp->d_reclen >= size
285 && idesc->id_filesize >= size && dp->d_namlen <= MAXNAMLEN) {
288 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
289 #if defined(Next) || defined(AFS_SUN5_ENV)
295 if (*cp == 0 || (*cp++ & 0200)) {
305 direrror(ino, errmesg)
309 register struct dinode *dp;
311 pwarn("%s ", errmesg);
314 if (ino < ROOTINO || ino > maxino) {
315 pfatal("NAME=%s\n", pathname);
320 pfatal("%s=%s\n", (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE",
323 pfatal("NAME=%s\n", pathname);
327 register struct inodesc *idesc;
330 register struct dinode *dp;
332 dp = ginode(idesc->id_number);
333 if (dp->di_nlink == lcnt) {
334 if (linkup(idesc->id_number, (ino_t) 0) == 0)
335 clri(idesc, "UNREF", 0);
337 pwarn("LINK COUNT %s",
338 (lfdir == idesc->id_number) ? lfname : ((dp->di_mode & IFMT) ==
341 pinode(idesc->id_number);
342 printf(" COUNT %d SHOULD BE %d", dp->di_nlink, dp->di_nlink - lcnt);
346 pfatal("LINK COUNT INCREASING");
348 printf(" (ADJUSTED)\n");
350 if (preen || reply("ADJUST") == 1) {
351 dp->di_nlink -= lcnt;
358 struct inodesc *idesc;
360 register struct direct *dirp = idesc->id_dirp;
361 struct direct newent;
364 newent.d_namlen = 11;
365 newlen = DIRSIZ(&newent);
366 if (dirp->d_ino != 0)
367 oldlen = DIRSIZ(dirp);
370 if (dirp->d_reclen - oldlen < newlen)
372 newent.d_reclen = dirp->d_reclen - oldlen;
373 dirp->d_reclen = oldlen;
374 dirp = (struct direct *)(((char *)dirp) + oldlen);
375 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
376 dirp->d_reclen = newent.d_reclen;
377 dirp->d_namlen = strlen(idesc->id_name);
378 memcpy(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1);
379 return (ALTERED | STOP);
383 struct inodesc *idesc;
385 register struct direct *dirp = idesc->id_dirp;
387 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
389 dirp->d_ino = idesc->id_parent;
390 return (ALTERED | STOP);
393 linkup(orphan, parentdir)
397 register struct dinode *dp;
400 struct inodesc idesc;
401 char tempname[BUFSIZ];
402 extern int pass4check();
404 memset((char *)&idesc, 0, sizeof(struct inodesc));
406 lostdir = (dp->di_mode & IFMT) == IFDIR;
407 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
409 if (preen && dp->di_size == 0)
412 printf(" (RECONNECTED)\n");
413 else if (reply("RECONNECT") == 0)
419 dp = ginode(ROOTINO);
420 idesc.id_name = lfname;
421 idesc.id_type = DATA;
422 idesc.id_func = findino;
423 idesc.id_number = ROOTINO;
424 if ((ckinode(dp, &idesc) & FOUND) != 0) {
425 lfdir = idesc.id_parent;
427 pwarn("NO lost+found DIRECTORY");
428 if (preen || reply("CREATE")) {
429 lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
431 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
433 printf(" (CREATED)\n");
435 freedir(lfdir, ROOTINO);
444 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
450 if ((dp->di_mode & IFMT) != IFDIR) {
451 pfatal("lost+found IS NOT A DIRECTORY");
452 if (reply("REALLOCATE") == 0)
455 if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
456 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
459 idesc.id_type = DATA;
460 idesc.id_func = chgino;
461 idesc.id_number = ROOTINO;
462 idesc.id_parent = lfdir; /* new inumber for lost+found */
463 idesc.id_name = lfname;
464 if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) {
465 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
469 idesc.id_type = ADDR;
470 idesc.id_func = pass4check;
471 idesc.id_number = oldlfdir;
472 adjust(&idesc, lncntp[oldlfdir] + 1);
473 lncntp[oldlfdir] = 0;
476 if (statemap[lfdir] != DFOUND) {
477 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
480 len = strlen(lfname);
481 memcpy(pathp, lfname, len + 1);
483 len = lftempname(tempname, orphan);
484 if (makeentry(lfdir, orphan, tempname) == 0) {
485 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
491 memcpy(pathp, tempname, len + 1);
495 idesc.id_type = DATA;
496 idesc.id_func = chgino;
497 idesc.id_number = orphan;
498 idesc.id_fix = DONTKNOW;
499 idesc.id_name = "..";
500 idesc.id_parent = lfdir; /* new value for ".." */
501 (void)ckinode(dp, &idesc);
506 pwarn("DIR I=%u CONNECTED. ", orphan);
507 printf("PARENT WAS I=%u\n", parentdir);
515 * make an entry in a directory
517 makeentry(parent, ino, name)
522 struct inodesc idesc;
524 if (parent < ROOTINO || parent >= maxino || ino < ROOTINO
527 memset((char *)&idesc, 0, sizeof(struct inodesc));
528 idesc.id_type = DATA;
529 idesc.id_func = mkentry;
530 idesc.id_number = parent;
531 idesc.id_parent = ino; /* this is the inode to enter */
532 idesc.id_fix = DONTKNOW;
533 idesc.id_name = name;
535 if (dp->di_size % DIRBLKSIZ) {
536 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
539 if ((ckinode(dp, &idesc) & ALTERED) != 0)
541 if (expanddir(dp) == 0)
543 return (ckinode(dp, &idesc) & ALTERED);
547 * Attempt to expand the size of a directory
550 register struct dinode *dp;
552 daddr_t lastbn, newblk;
553 register struct bufarea *bp;
554 char *cp, firstblk[DIRBLKSIZ];
556 lastbn = lblkno(&sblock, dp->di_size);
557 if (lastbn >= NDADDR - 1)
559 if ((newblk = allocblk(sblock.fs_frag)) == 0)
561 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
562 dp->di_db[lastbn] = newblk;
563 dp->di_size += (UOFF_T) sblock.fs_bsize;
564 dp->di_blocks += btodb(sblock.fs_bsize);
565 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
568 memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
569 bp = getdirblk(newblk, sblock.fs_bsize);
572 memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
573 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
574 cp < &bp->b_un.b_buf[sblock.fs_bsize]; cp += DIRBLKSIZ)
575 memcpy(cp, (char *)&emptydir, sizeof emptydir);
577 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
580 memcpy(bp->b_un.b_buf, (char *)&emptydir, sizeof emptydir);
581 pwarn("NO SPACE LEFT IN %s", pathname);
583 printf(" (EXPANDED)\n");
584 else if (reply("EXPAND") == 0)
590 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
591 dp->di_db[lastbn + 1] = 0;
592 dp->di_size -= (UOFF_T) sblock.fs_bsize;
593 dp->di_blocks -= btodb(sblock.fs_bsize);
594 freeblk(newblk, sblock.fs_frag);
599 * allocate a new directory
601 allocdir(parent, request, mode)
602 ino_t parent, request;
608 register struct bufarea *bp;
610 ino = allocino(request, IFDIR | mode);
611 dirhead.dot_ino = ino;
612 dirhead.dotdot_ino = parent;
614 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
619 memcpy(bp->b_un.b_buf, (char *)&dirhead, sizeof dirhead);
620 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
621 cp < &bp->b_un.b_buf[sblock.fs_fsize]; cp += DIRBLKSIZ)
622 memcpy(cp, (char *)&emptydir, sizeof emptydir);
627 if (!inocached(ino)) {
629 printf("inode %d added to directory cache\n", ino);
633 * re-using an old directory inode
635 inp = getinoinfo(ino);
636 inp->i_isize = dp->di_size;
637 inp->i_numblks = dp->di_blocks * sizeof(daddr_t);
638 inp->i_parent = parent;
639 memcpy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
640 (int)inp->i_numblks);
643 if (ino == ROOTINO) {
644 lncntp[ino] = dp->di_nlink;
647 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
651 statemap[ino] = statemap[parent];
652 if (statemap[ino] == DSTATE) {
653 lncntp[ino] = dp->di_nlink;
663 * free a directory inode
679 * generate a temporary name for the lost+found directory.
681 lftempname(bufp, ino)
690 for (in = maxino; in > 0; in /= 10)
696 *--cp = (in % 10) + '0';
704 * Get a directory block.
705 * Insure that it is held until another is requested.
708 getdirblk(blkno, size)
713 mlk_pbp->b_flags &= ~B_INUSE;
714 mlk_pbp = getdatablk(blkno, size);