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