death to trailing whitespace
[openafs.git] / src / vfsck / dir.c
1 /*
2  * Copyright (c) 1980, 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
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.
16  */
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21
22 #define VICE                    /* allow us to put our changes in at will */
23 #include <stdio.h>
24
25 #include <sys/param.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28
29 #ifdef  AFS_OSF_ENV
30 #include <sys/vnode.h>
31 #include <sys/mount.h>
32 #include <ufs/inode.h>
33 #include <ufs/fs.h>
34 #define _BSD
35 #define _KERNEL
36 #include <ufs/dir.h>
37 #undef  _KERNEL
38 #undef  _BSD
39 #else /* AFS_OSF_ENV */
40 #ifdef AFS_VFSINCL_ENV
41 #define VFS
42 #include <sys/vnode.h>
43 #ifdef    AFS_SUN5_ENV
44 #include <sys/fs/ufs_inode.h>
45 #include <sys/fs/ufs_fs.h>
46 #define _KERNEL
47 #include <sys/fs/ufs_fsdir.h>
48 #undef _KERNEL
49 #else
50 #include <ufs/inode.h>
51 #include <ufs/fs.h>
52 #define KERNEL
53 #include <ufs/fsdir.h>
54 #undef KERNEL
55 #endif
56
57 #else /* AFS_VFSINCL_ENV */
58 #include <sys/inode.h>
59 #ifdef  AFS_HPUX_ENV
60 #include <ctype.h>
61 #define LONGFILENAMES   1
62 #include <sys/sysmacros.h>
63 #include <sys/ino.h>
64 #define DIRSIZ_MACRO
65 #ifdef HAVE_USR_OLD_USR_INCLUDE_NDIR_H
66 #include </usr/old/usr/include/ndir.h>
67 #else
68 #include <ndir.h>
69 #endif
70 #else
71 #define KERNEL
72 #include <sys/dir.h>
73 #undef KERNEL
74 #endif
75 #include <sys/fs.h>
76 #endif /* AFS_VFSINCL_ENV */
77 #endif /* AFS_OSF_ENV */
78
79 #ifdef AFS_SUN_ENV
80 #ifdef  AFS_SUN5_ENV
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
83 #else
84 #include <mntent.h>
85 #endif
86 #endif
87 #include "fsck.h"
88
89
90 #ifdef  AFS_HPUX_ENV
91 struct dirtemplate_lfn {
92     afs_uint32 dot_ino;
93     short dot_reclen;
94     short dot_namlen;
95     char dot_name[4];           /* must be multiple of 4 */
96     afs_uint32 dotdot_ino;
97     short dotdot_reclen;
98     short dotdot_namlen;
99     char dotdot_name[4];        /* ditto */
100 };
101 #define dirtemplate dirtemplate_lfn
102 #endif
103
104 #define MINDIRSIZE      (sizeof (struct dirtemplate))
105
106 char *endpathname = &pathname[BUFSIZ - 2];
107 char *lfname = "lost+found";
108 int lfmode = 01777;
109 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
110 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
111
112 struct direct *fsck_readdir();
113 struct bufarea *getdirblk();
114
115 descend(parentino, inumber)
116      struct inodesc *parentino;
117      ino_t inumber;
118 {
119     struct dinode *dp;
120     struct inodesc curino;
121
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)
126     /*
127      * keep any continuation inode information
128      */
129     if (statemap[inumber] & HASCINODE)
130         statemap[inumber] = HASCINODE | DFOUND;
131     else
132         statemap[inumber] = DFOUND;
133 #else /* no ACLS */
134     statemap[inumber] = DFOUND;
135 #endif /* ACLS */
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)
141             /*
142              * keep any continuation inode information
143              */
144             if (statemap[inumber] & HASCINODE)
145                 statemap[inumber] = HASCINODE | DCLEAR;
146             else
147                 statemap[inumber] = DCLEAR;
148 #else /* no ACLS */
149             statemap[inumber] = DCLEAR;
150 #endif /* ACLS */
151         return;
152     }
153     if (dp->di_size < MINDIRSIZE) {
154         direrror(inumber, "DIRECTORY TOO SHORT");
155         dp->di_size = MINDIRSIZE;
156         if (reply("FIX") == 1)
157             inodirty();
158     }
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);
167         if (preen)
168             printf(" (ADJUSTED)\n");
169         if (preen || reply("ADJUST") == 1)
170             inodirty();
171     }
172 #endif
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;
182     }
183 }
184
185 dirscan(idesc)
186      struct inodesc *idesc;
187 {
188     struct direct *dp;
189     struct bufarea *bp;
190     int dsize, n;
191     long blksiz;
192     char dbuf[DIRBLKSIZ];
193
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;
199
200     if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
201         idesc->id_filesize -= blksiz;
202         return (SKIP);
203     }
204     idesc->id_loc = 0;
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);
212             dirty(bp);
213             sbdirty();
214         }
215         if (n & STOP)
216             return (n);
217     }
218     return (idesc->id_filesize > 0 ? KEEPON : STOP);
219 }
220
221 /*
222  * get next entry in a directory.
223  */
224 struct direct *
225 fsck_readdir(idesc)
226      struct inodesc *idesc;
227 {
228     struct direct *dp, *ndp;
229     struct bufarea *bp;
230     long size, blksiz;
231
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)) {
238             goto dpok;
239         }
240         idesc->id_loc += DIRBLKSIZ;
241         idesc->id_filesize -= DIRBLKSIZ;
242         dp->d_reclen = DIRBLKSIZ;
243         dp->d_ino = 0;
244         dp->d_namlen = 0;
245         dp->d_name[0] = '\0';
246         if (dofix(idesc, "DIRECTORY CORRUPTED"))
247             dirty(bp);
248         return (dp);
249     }
250   dpok:
251     if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
252         return NULL;
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)
257         return (dp);
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"))
266             dirty(bp);
267     }
268     return (dp);
269 }
270
271 /*
272  * Verify that a directory entry is valid.
273  * This is a superset of the checks made in the kernel.
274  */
275 dircheck(idesc, dp)
276      struct inodesc *idesc;
277      struct direct *dp;
278 {
279     int size;
280     char *cp;
281     int spaceleft;
282
283     size = DIRSIZ(dp);
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) {
288         if (dp->d_ino == 0)
289             return (1);
290         for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
291 #if     defined(Next) || defined(AFS_SUN5_ENV)
292             if (*cp == 0)
293                 return (0);
294             else
295                 ++cp;
296 #else
297             if (*cp == 0 || (*cp++ & 0200)) {
298                 return (0);
299             }
300 #endif
301         if (*cp == 0)
302             return (1);
303     }
304     return (0);
305 }
306
307 direrror(ino, errmesg)
308      ino_t ino;
309      char *errmesg;
310 {
311     struct dinode *dp;
312
313     pwarn("%s ", errmesg);
314     pinode(ino);
315     printf("\n");
316     if (ino < ROOTINO || ino > maxino) {
317         pfatal("NAME=%s\n", pathname);
318         return;
319     }
320     dp = ginode(ino);
321     if (ftypeok(dp))
322         pfatal("%s=%s\n", (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE",
323                pathname);
324     else
325         pfatal("NAME=%s\n", pathname);
326 }
327
328 adjust(idesc, lcnt)
329      struct inodesc *idesc;
330      short lcnt;
331 {
332     struct dinode *dp;
333
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);
338     } else {
339         pwarn("LINK COUNT %s",
340               (lfdir == idesc->id_number) ? lfname : ((dp->di_mode & IFMT) ==
341                                                       IFDIR ? "DIR" :
342                                                       "FILE"));
343         pinode(idesc->id_number);
344         printf(" COUNT %d SHOULD BE %d", dp->di_nlink, dp->di_nlink - lcnt);
345         if (preen) {
346             if (lcnt < 0) {
347                 printf("\n");
348                 pfatal("LINK COUNT INCREASING");
349             }
350             printf(" (ADJUSTED)\n");
351         }
352         if (preen || reply("ADJUST") == 1) {
353             dp->di_nlink -= lcnt;
354             inodirty();
355         }
356     }
357 }
358
359 mkentry(idesc)
360      struct inodesc *idesc;
361 {
362     struct direct *dirp = idesc->id_dirp;
363     struct direct newent;
364     int newlen, oldlen;
365
366     newent.d_namlen = 11;
367     newlen = DIRSIZ(&newent);
368     if (dirp->d_ino != 0)
369         oldlen = DIRSIZ(dirp);
370     else
371         oldlen = 0;
372     if (dirp->d_reclen - oldlen < newlen)
373         return (KEEPON);
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);
382 }
383
384 chgino(idesc)
385      struct inodesc *idesc;
386 {
387     struct direct *dirp = idesc->id_dirp;
388
389     if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
390         return (KEEPON);
391     dirp->d_ino = idesc->id_parent;
392     return (ALTERED | STOP);
393 }
394
395 linkup(orphan, parentdir)
396      ino_t orphan;
397      ino_t parentdir;
398 {
399     struct dinode *dp;
400     int lostdir, len;
401     ino_t oldlfdir;
402     struct inodesc idesc;
403     char tempname[BUFSIZ];
404     extern int pass4check();
405
406     memset(&idesc, 0, sizeof(struct inodesc));
407     dp = ginode(orphan);
408     lostdir = (dp->di_mode & IFMT) == IFDIR;
409     pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
410     pinode(orphan);
411     if (preen && dp->di_size == 0)
412         return (0);
413     if (preen)
414         printf(" (RECONNECTED)\n");
415     else if (reply("RECONNECT") == 0)
416         return (0);
417     pathp = pathname;
418     *pathp++ = '/';
419     *pathp = '\0';
420     if (lfdir == 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;
428         } else {
429             pwarn("NO lost+found DIRECTORY");
430             if (preen || reply("CREATE")) {
431                 lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
432                 if (lfdir != 0) {
433                     if (makeentry(ROOTINO, lfdir, lfname) != 0) {
434                         if (preen)
435                             printf(" (CREATED)\n");
436                     } else {
437                         freedir(lfdir, ROOTINO);
438                         lfdir = 0;
439                         if (preen)
440                             printf("\n");
441                     }
442                 }
443             }
444         }
445         if (lfdir == 0) {
446             pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
447             printf("\n\n");
448             return (0);
449         }
450     }
451     dp = ginode(lfdir);
452     if ((dp->di_mode & IFMT) != IFDIR) {
453         pfatal("lost+found IS NOT A DIRECTORY");
454         if (reply("REALLOCATE") == 0)
455             return (0);
456         oldlfdir = lfdir;
457         if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
458             pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
459             return (0);
460         }
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");
468             return (0);
469         }
470         inodirty();
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;
476         dp = ginode(lfdir);
477     }
478     if (statemap[lfdir] != DFOUND) {
479         pfatal("SORRY. NO lost+found DIRECTORY\n\n");
480         return (0);
481     }
482     len = strlen(lfname);
483     memcpy(pathp, lfname, len + 1);
484     pathp += len;
485     len = lftempname(tempname, orphan);
486     if (makeentry(lfdir, orphan, tempname) == 0) {
487         pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
488         printf("\n\n");
489         return (0);
490     }
491     lncntp[orphan]--;
492     *pathp++ = '/';
493     memcpy(pathp, tempname, len + 1);
494     pathp += len;
495     if (lostdir) {
496         dp = ginode(orphan);
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);
504         dp = ginode(lfdir);
505         dp->di_nlink++;
506         inodirty();
507         lncntp[lfdir]++;
508         pwarn("DIR I=%u CONNECTED. ", orphan);
509         printf("PARENT WAS I=%u\n", parentdir);
510         if (preen == 0)
511             printf("\n");
512     }
513     return (1);
514 }
515
516 /*
517  * make an entry in a directory
518  */
519 makeentry(parent, ino, name)
520      ino_t parent, ino;
521      char *name;
522 {
523     struct dinode *dp;
524     struct inodesc idesc;
525
526     if (parent < ROOTINO || parent >= maxino || ino < ROOTINO
527         || ino >= maxino)
528         return (0);
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;
536     dp = ginode(parent);
537     if (dp->di_size % DIRBLKSIZ) {
538         dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
539         inodirty();
540     }
541     if ((ckinode(dp, &idesc) & ALTERED) != 0)
542         return (1);
543     if (expanddir(dp) == 0)
544         return (0);
545     return (ckinode(dp, &idesc) & ALTERED);
546 }
547
548 /*
549  * Attempt to expand the size of a directory
550  */
551 expanddir(dp)
552      struct dinode *dp;
553 {
554     daddr_t lastbn, newblk;
555     struct bufarea *bp;
556     char *cp, firstblk[DIRBLKSIZ];
557
558     lastbn = lblkno(&sblock, dp->di_size);
559     if (lastbn >= NDADDR - 1)
560         return (0);
561     if ((newblk = allocblk(sblock.fs_frag)) == 0)
562         return (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));
568     if (bp->b_errs)
569         goto bad;
570     memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
571     bp = getdirblk(newblk, sblock.fs_bsize);
572     if (bp->b_errs)
573         goto bad;
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);
578     dirty(bp);
579     bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
580     if (bp->b_errs)
581         goto bad;
582     memcpy(bp->b_un.b_buf, (char *)&emptydir, sizeof emptydir);
583     pwarn("NO SPACE LEFT IN %s", pathname);
584     if (preen)
585         printf(" (EXPANDED)\n");
586     else if (reply("EXPAND") == 0)
587         goto bad;
588     dirty(bp);
589     inodirty();
590     return (1);
591   bad:
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);
597     return (0);
598 }
599
600 /*
601  * allocate a new directory
602  */
603 allocdir(parent, request, mode)
604      ino_t parent, request;
605      int mode;
606 {
607     ino_t ino;
608     char *cp;
609     struct dinode *dp;
610     struct bufarea *bp;
611
612     ino = allocino(request, IFDIR | mode);
613     dirhead.dot_ino = ino;
614     dirhead.dotdot_ino = parent;
615     dp = ginode(ino);
616     bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
617     if (bp->b_errs) {
618         freeino(ino);
619         return (0);
620     }
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);
625     dirty(bp);
626     dp->di_nlink = 2;
627     inodirty();
628 #ifdef  AFS_SUN5_ENVX
629     if (!inocached(ino)) {
630         if (debug)
631             printf("inode %d added to directory cache\n", ino);
632         cacheino(dp, ino);
633     } else {
634         /*
635          * re-using an old directory inode
636          */
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);
643     }
644 #endif
645     if (ino == ROOTINO) {
646         lncntp[ino] = dp->di_nlink;
647         return (ino);
648     }
649     if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
650         freeino(ino);
651         return (0);
652     }
653     statemap[ino] = statemap[parent];
654     if (statemap[ino] == DSTATE) {
655         lncntp[ino] = dp->di_nlink;
656         lncntp[parent]++;
657     }
658     dp = ginode(parent);
659     dp->di_nlink++;
660     inodirty();
661     return (ino);
662 }
663
664 /*
665  * free a directory inode
666  */
667 freedir(ino, parent)
668      ino_t ino, parent;
669 {
670     struct dinode *dp;
671
672     if (ino != parent) {
673         dp = ginode(parent);
674         dp->di_nlink--;
675         inodirty();
676     }
677     freeino(ino);
678 }
679
680 /*
681  * generate a temporary name for the lost+found directory.
682  */
683 lftempname(bufp, ino)
684      char *bufp;
685      ino_t ino;
686 {
687     ino_t in;
688     char *cp;
689     int namlen;
690
691     cp = bufp + 2;
692     for (in = maxino; in > 0; in /= 10)
693         cp++;
694     *--cp = 0;
695     namlen = cp - bufp;
696     in = ino;
697     while (cp > bufp) {
698         *--cp = (in % 10) + '0';
699         in /= 10;
700     }
701     *cp = '#';
702     return (namlen);
703 }
704
705 /*
706  * Get a directory block.
707  * Insure that it is held until another is requested.
708  */
709 struct bufarea *
710 getdirblk(blkno, size)
711      daddr_t blkno;
712      long size;
713 {
714     if (mlk_pbp != 0)
715         mlk_pbp->b_flags &= ~B_INUSE;
716     mlk_pbp = getdatablk(blkno, size);
717     return (mlk_pbp);
718 }