Import Upstream version 1.8.5
[hcoop/debian/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 #ifdef AFS_HPUX_ENV
22 /* We need the old directory type headers (included below), so don't include
23 * the normal dirent.h, or it will conflict. */
24 # undef HAVE_DIRENT_H
25 # include <sys/inode.h>
26 # define LONGFILENAMES 1
27 # include <sys/sysmacros.h>
28 # include <sys/ino.h>
29 # define DIRSIZ_MACRO
30 # ifdef HAVE_USR_OLD_USR_INCLUDE_NDIR_H
31 # include </usr/old/usr/include/ndir.h>
32 # else
33 # include <ndir.h>
34 # endif
35 #endif
36
37 #include <roken.h>
38
39 #include <ctype.h>
40
41 #define VICE /* allow us to put our changes in at will */
42
43 #ifdef AFS_OSF_ENV
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <ufs/inode.h>
47 #include <ufs/fs.h>
48 #define _BSD
49 #define _KERNEL
50 #include <ufs/dir.h>
51 #undef _KERNEL
52 #undef _BSD
53 #else /* AFS_OSF_ENV */
54 #ifdef AFS_VFSINCL_ENV
55 #define VFS
56 #include <sys/vnode.h>
57 #ifdef AFS_SUN5_ENV
58 #include <sys/fs/ufs_inode.h>
59 #include <sys/fs/ufs_fs.h>
60 #define _KERNEL
61 #include <sys/fs/ufs_fsdir.h>
62 #undef _KERNEL
63 #else
64 #include <ufs/inode.h>
65 #include <ufs/fs.h>
66 #define KERNEL
67 #include <ufs/fsdir.h>
68 #undef KERNEL
69 #endif
70
71 #else /* AFS_VFSINCL_ENV */
72 #include <sys/inode.h>
73 #ifndef AFS_HPUX_ENV
74 #define KERNEL
75 #include <sys/dir.h>
76 #undef KERNEL
77 #endif
78 #include <sys/fs.h>
79 #endif /* AFS_VFSINCL_ENV */
80 #endif /* AFS_OSF_ENV */
81
82 #ifdef AFS_SUN_ENV
83 #ifdef AFS_SUN5_ENV
84 #include <sys/mnttab.h>
85 #include <sys/mntent.h>
86 #else
87 #include <mntent.h>
88 #endif
89 #endif
90 #include "fsck.h"
91
92
93 #ifdef AFS_HPUX_ENV
94 struct dirtemplate_lfn {
95 afs_uint32 dot_ino;
96 short dot_reclen;
97 short dot_namlen;
98 char dot_name[4]; /* must be multiple of 4 */
99 afs_uint32 dotdot_ino;
100 short dotdot_reclen;
101 short dotdot_namlen;
102 char dotdot_name[4]; /* ditto */
103 };
104 #define dirtemplate dirtemplate_lfn
105 #endif
106
107 #define MINDIRSIZE (sizeof (struct dirtemplate))
108
109 char *endpathname = &pathname[BUFSIZ - 2];
110 char *lfname = "lost+found";
111 int lfmode = 01777;
112 struct dirtemplate emptydir = { 0, DIRBLKSIZ };
113 struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
114
115 struct direct *fsck_readdir();
116 struct bufarea *getdirblk();
117
118 descend(parentino, inumber)
119 struct inodesc *parentino;
120 ino_t inumber;
121 {
122 struct dinode *dp;
123 struct inodesc curino;
124
125 memset(&curino, 0, sizeof(struct inodesc));
126 if (statemap[inumber] != DSTATE)
127 errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
128 #if defined(ACLS) && defined(AFS_HPUX_ENV)
129 /*
130 * keep any continuation inode information
131 */
132 if (statemap[inumber] & HASCINODE)
133 statemap[inumber] = HASCINODE | DFOUND;
134 else
135 statemap[inumber] = DFOUND;
136 #else /* no ACLS */
137 statemap[inumber] = DFOUND;
138 #endif /* ACLS */
139 dp = ginode(inumber);
140 if (dp->di_size == 0) {
141 direrror(inumber, "ZERO LENGTH DIRECTORY");
142 if (reply("REMOVE") == 1)
143 #if defined(ACLS) && defined(AFS_HPUX_ENV)
144 /*
145 * keep any continuation inode information
146 */
147 if (statemap[inumber] & HASCINODE)
148 statemap[inumber] = HASCINODE | DCLEAR;
149 else
150 statemap[inumber] = DCLEAR;
151 #else /* no ACLS */
152 statemap[inumber] = DCLEAR;
153 #endif /* ACLS */
154 return;
155 }
156 if (dp->di_size < MINDIRSIZE) {
157 direrror(inumber, "DIRECTORY TOO SHORT");
158 dp->di_size = MINDIRSIZE;
159 if (reply("FIX") == 1)
160 inodirty();
161 }
162 #if !defined(AFS_HPUX_ENV)
163 /* For remaining 4.2 systems. We shouldn't convert
164 * dir size, since Unix 4.2 kernels won't maintain this, and we'll have a
165 * lot of spurious directory conversions scaring people */
166 if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
167 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", pathname,
168 dp->di_size, DIRBLKSIZ);
169 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
170 if (preen)
171 printf(" (ADJUSTED)\n");
172 if (preen || reply("ADJUST") == 1)
173 inodirty();
174 }
175 #endif
176 curino.id_type = DATA;
177 curino.id_func = parentino->id_func;
178 curino.id_parent = parentino->id_number;
179 curino.id_number = inumber;
180 (void)ckinode(dp, &curino);
181 if (curino.id_entryno < 2) {
182 direrror(inumber, "NULL DIRECTORY");
183 if (reply("REMOVE") == 1)
184 statemap[inumber] = DCLEAR;
185 }
186 }
187
188 dirscan(idesc)
189 struct inodesc *idesc;
190 {
191 struct direct *dp;
192 struct bufarea *bp;
193 int dsize, n;
194 long blksiz;
195 char dbuf[DIRBLKSIZ];
196
197 if (idesc->id_type != DATA)
198 errexit("wrong type to dirscan %d\n", idesc->id_type);
199 if (idesc->id_entryno == 0 && (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
200 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
201 blksiz = idesc->id_numfrags * sblock.fs_fsize;
202
203 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
204 idesc->id_filesize -= blksiz;
205 return (SKIP);
206 }
207 idesc->id_loc = 0;
208 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
209 dsize = dp->d_reclen;
210 memcpy(dbuf, (char *)dp, dsize);
211 idesc->id_dirp = (struct direct *)dbuf;
212 if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
213 bp = getdirblk(idesc->id_blkno, blksiz);
214 memcpy((char *)dp, dbuf, dsize);
215 dirty(bp);
216 sbdirty();
217 }
218 if (n & STOP)
219 return (n);
220 }
221 return (idesc->id_filesize > 0 ? KEEPON : STOP);
222 }
223
224 /*
225 * get next entry in a directory.
226 */
227 struct direct *
228 fsck_readdir(idesc)
229 struct inodesc *idesc;
230 {
231 struct direct *dp, *ndp;
232 struct bufarea *bp;
233 long size, blksiz;
234
235 blksiz = idesc->id_numfrags * sblock.fs_fsize;
236 bp = getdirblk(idesc->id_blkno, blksiz);
237 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0
238 && idesc->id_loc < blksiz) {
239 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
240 if (dircheck(idesc, dp)) {
241 goto dpok;
242 }
243 idesc->id_loc += DIRBLKSIZ;
244 idesc->id_filesize -= DIRBLKSIZ;
245 dp->d_reclen = DIRBLKSIZ;
246 dp->d_ino = 0;
247 dp->d_namlen = 0;
248 dp->d_name[0] = '\0';
249 if (dofix(idesc, "DIRECTORY CORRUPTED"))
250 dirty(bp);
251 return (dp);
252 }
253 dpok:
254 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
255 return NULL;
256 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
257 idesc->id_loc += dp->d_reclen;
258 idesc->id_filesize -= dp->d_reclen;
259 if ((idesc->id_loc % DIRBLKSIZ) == 0)
260 return (dp);
261 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
262 if (idesc->id_loc < blksiz && idesc->id_filesize > 0
263 && dircheck(idesc, ndp) == 0) {
264 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
265 dp->d_reclen += size;
266 idesc->id_loc += size;
267 idesc->id_filesize -= size;
268 if (dofix(idesc, "DIRECTORY CORRUPTED"))
269 dirty(bp);
270 }
271 return (dp);
272 }
273
274 /*
275 * Verify that a directory entry is valid.
276 * This is a superset of the checks made in the kernel.
277 */
278 dircheck(idesc, dp)
279 struct inodesc *idesc;
280 struct direct *dp;
281 {
282 int size;
283 char *cp;
284 int spaceleft;
285
286 size = DIRSIZ(dp);
287 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
288 if (dp->d_ino < maxino && dp->d_reclen != 0 && dp->d_reclen <= spaceleft
289 && (dp->d_reclen & 0x3) == 0 && dp->d_reclen >= size
290 && idesc->id_filesize >= size && dp->d_namlen <= MAXNAMLEN) {
291 if (dp->d_ino == 0)
292 return (1);
293 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
294 #if defined(Next) || defined(AFS_SUN5_ENV)
295 if (*cp == 0)
296 return (0);
297 else
298 ++cp;
299 #else
300 if (*cp == 0 || (*cp++ & 0200)) {
301 return (0);
302 }
303 #endif
304 if (*cp == 0)
305 return (1);
306 }
307 return (0);
308 }
309
310 direrror(ino, errmesg)
311 ino_t ino;
312 char *errmesg;
313 {
314 struct dinode *dp;
315
316 pwarn("%s ", errmesg);
317 pinode(ino);
318 printf("\n");
319 if (ino < ROOTINO || ino > maxino) {
320 pfatal("NAME=%s\n", pathname);
321 return;
322 }
323 dp = ginode(ino);
324 if (ftypeok(dp))
325 pfatal("%s=%s\n", (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE",
326 pathname);
327 else
328 pfatal("NAME=%s\n", pathname);
329 }
330
331 adjust(idesc, lcnt)
332 struct inodesc *idesc;
333 short lcnt;
334 {
335 struct dinode *dp;
336
337 dp = ginode(idesc->id_number);
338 if (dp->di_nlink == lcnt) {
339 if (linkup(idesc->id_number, (ino_t) 0) == 0)
340 clri(idesc, "UNREF", 0);
341 } else {
342 pwarn("LINK COUNT %s",
343 (lfdir == idesc->id_number) ? lfname : ((dp->di_mode & IFMT) ==
344 IFDIR ? "DIR" :
345 "FILE"));
346 pinode(idesc->id_number);
347 printf(" COUNT %d SHOULD BE %d", dp->di_nlink, dp->di_nlink - lcnt);
348 if (preen) {
349 if (lcnt < 0) {
350 printf("\n");
351 pfatal("LINK COUNT INCREASING");
352 }
353 printf(" (ADJUSTED)\n");
354 }
355 if (preen || reply("ADJUST") == 1) {
356 dp->di_nlink -= lcnt;
357 inodirty();
358 }
359 }
360 }
361
362 mkentry(idesc)
363 struct inodesc *idesc;
364 {
365 struct direct *dirp = idesc->id_dirp;
366 struct direct newent;
367 int newlen, oldlen;
368
369 newent.d_namlen = 11;
370 newlen = DIRSIZ(&newent);
371 if (dirp->d_ino != 0)
372 oldlen = DIRSIZ(dirp);
373 else
374 oldlen = 0;
375 if (dirp->d_reclen - oldlen < newlen)
376 return (KEEPON);
377 newent.d_reclen = dirp->d_reclen - oldlen;
378 dirp->d_reclen = oldlen;
379 dirp = (struct direct *)(((char *)dirp) + oldlen);
380 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
381 dirp->d_reclen = newent.d_reclen;
382 dirp->d_namlen = strlen(idesc->id_name);
383 memcpy(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1);
384 return (ALTERED | STOP);
385 }
386
387 chgino(idesc)
388 struct inodesc *idesc;
389 {
390 struct direct *dirp = idesc->id_dirp;
391
392 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
393 return (KEEPON);
394 dirp->d_ino = idesc->id_parent;
395 return (ALTERED | STOP);
396 }
397
398 linkup(orphan, parentdir)
399 ino_t orphan;
400 ino_t parentdir;
401 {
402 struct dinode *dp;
403 int lostdir, len;
404 ino_t oldlfdir;
405 struct inodesc idesc;
406 char tempname[BUFSIZ];
407 extern int pass4check();
408
409 memset(&idesc, 0, sizeof(struct inodesc));
410 dp = ginode(orphan);
411 lostdir = (dp->di_mode & IFMT) == IFDIR;
412 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
413 pinode(orphan);
414 if (preen && dp->di_size == 0)
415 return (0);
416 if (preen)
417 printf(" (RECONNECTED)\n");
418 else if (reply("RECONNECT") == 0)
419 return (0);
420 pathp = pathname;
421 *pathp++ = '/';
422 *pathp = '\0';
423 if (lfdir == 0) {
424 dp = ginode(ROOTINO);
425 idesc.id_name = lfname;
426 idesc.id_type = DATA;
427 idesc.id_func = findino;
428 idesc.id_number = ROOTINO;
429 if ((ckinode(dp, &idesc) & FOUND) != 0) {
430 lfdir = idesc.id_parent;
431 } else {
432 pwarn("NO lost+found DIRECTORY");
433 if (preen || reply("CREATE")) {
434 lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
435 if (lfdir != 0) {
436 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
437 if (preen)
438 printf(" (CREATED)\n");
439 } else {
440 freedir(lfdir, ROOTINO);
441 lfdir = 0;
442 if (preen)
443 printf("\n");
444 }
445 }
446 }
447 }
448 if (lfdir == 0) {
449 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
450 printf("\n\n");
451 return (0);
452 }
453 }
454 dp = ginode(lfdir);
455 if ((dp->di_mode & IFMT) != IFDIR) {
456 pfatal("lost+found IS NOT A DIRECTORY");
457 if (reply("REALLOCATE") == 0)
458 return (0);
459 oldlfdir = lfdir;
460 if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
461 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
462 return (0);
463 }
464 idesc.id_type = DATA;
465 idesc.id_func = chgino;
466 idesc.id_number = ROOTINO;
467 idesc.id_parent = lfdir; /* new inumber for lost+found */
468 idesc.id_name = lfname;
469 if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) {
470 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
471 return (0);
472 }
473 inodirty();
474 idesc.id_type = ADDR;
475 idesc.id_func = pass4check;
476 idesc.id_number = oldlfdir;
477 adjust(&idesc, lncntp[oldlfdir] + 1);
478 lncntp[oldlfdir] = 0;
479 dp = ginode(lfdir);
480 }
481 if (statemap[lfdir] != DFOUND) {
482 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
483 return (0);
484 }
485 len = strlen(lfname);
486 memcpy(pathp, lfname, len + 1);
487 pathp += len;
488 len = lftempname(tempname, orphan);
489 if (makeentry(lfdir, orphan, tempname) == 0) {
490 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
491 printf("\n\n");
492 return (0);
493 }
494 lncntp[orphan]--;
495 *pathp++ = '/';
496 memcpy(pathp, tempname, len + 1);
497 pathp += len;
498 if (lostdir) {
499 dp = ginode(orphan);
500 idesc.id_type = DATA;
501 idesc.id_func = chgino;
502 idesc.id_number = orphan;
503 idesc.id_fix = DONTKNOW;
504 idesc.id_name = "..";
505 idesc.id_parent = lfdir; /* new value for ".." */
506 (void)ckinode(dp, &idesc);
507 dp = ginode(lfdir);
508 dp->di_nlink++;
509 inodirty();
510 lncntp[lfdir]++;
511 pwarn("DIR I=%u CONNECTED. ", orphan);
512 printf("PARENT WAS I=%u\n", parentdir);
513 if (preen == 0)
514 printf("\n");
515 }
516 return (1);
517 }
518
519 /*
520 * make an entry in a directory
521 */
522 makeentry(parent, ino, name)
523 ino_t parent, ino;
524 char *name;
525 {
526 struct dinode *dp;
527 struct inodesc idesc;
528
529 if (parent < ROOTINO || parent >= maxino || ino < ROOTINO
530 || ino >= maxino)
531 return (0);
532 memset(&idesc, 0, sizeof(struct inodesc));
533 idesc.id_type = DATA;
534 idesc.id_func = mkentry;
535 idesc.id_number = parent;
536 idesc.id_parent = ino; /* this is the inode to enter */
537 idesc.id_fix = DONTKNOW;
538 idesc.id_name = name;
539 dp = ginode(parent);
540 if (dp->di_size % DIRBLKSIZ) {
541 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
542 inodirty();
543 }
544 if ((ckinode(dp, &idesc) & ALTERED) != 0)
545 return (1);
546 if (expanddir(dp) == 0)
547 return (0);
548 return (ckinode(dp, &idesc) & ALTERED);
549 }
550
551 /*
552 * Attempt to expand the size of a directory
553 */
554 expanddir(dp)
555 struct dinode *dp;
556 {
557 daddr_t lastbn, newblk;
558 struct bufarea *bp;
559 char *cp, firstblk[DIRBLKSIZ];
560
561 lastbn = lblkno(&sblock, dp->di_size);
562 if (lastbn >= NDADDR - 1)
563 return (0);
564 if ((newblk = allocblk(sblock.fs_frag)) == 0)
565 return (0);
566 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
567 dp->di_db[lastbn] = newblk;
568 dp->di_size += (UOFF_T) sblock.fs_bsize;
569 dp->di_blocks += btodb(sblock.fs_bsize);
570 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
571 if (bp->b_errs)
572 goto bad;
573 memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
574 bp = getdirblk(newblk, sblock.fs_bsize);
575 if (bp->b_errs)
576 goto bad;
577 memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
578 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
579 cp < &bp->b_un.b_buf[sblock.fs_bsize]; cp += DIRBLKSIZ)
580 memcpy(cp, (char *)&emptydir, sizeof emptydir);
581 dirty(bp);
582 bp = getdirblk(dp->di_db[lastbn + 1], dblksize(&sblock, dp, lastbn + 1));
583 if (bp->b_errs)
584 goto bad;
585 memcpy(bp->b_un.b_buf, (char *)&emptydir, sizeof emptydir);
586 pwarn("NO SPACE LEFT IN %s", pathname);
587 if (preen)
588 printf(" (EXPANDED)\n");
589 else if (reply("EXPAND") == 0)
590 goto bad;
591 dirty(bp);
592 inodirty();
593 return (1);
594 bad:
595 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
596 dp->di_db[lastbn + 1] = 0;
597 dp->di_size -= (UOFF_T) sblock.fs_bsize;
598 dp->di_blocks -= btodb(sblock.fs_bsize);
599 freeblk(newblk, sblock.fs_frag);
600 return (0);
601 }
602
603 /*
604 * allocate a new directory
605 */
606 allocdir(parent, request, mode)
607 ino_t parent, request;
608 int mode;
609 {
610 ino_t ino;
611 char *cp;
612 struct dinode *dp;
613 struct bufarea *bp;
614
615 ino = allocino(request, IFDIR | mode);
616 dirhead.dot_ino = ino;
617 dirhead.dotdot_ino = parent;
618 dp = ginode(ino);
619 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
620 if (bp->b_errs) {
621 freeino(ino);
622 return (0);
623 }
624 memcpy(bp->b_un.b_buf, (char *)&dirhead, sizeof dirhead);
625 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
626 cp < &bp->b_un.b_buf[sblock.fs_fsize]; cp += DIRBLKSIZ)
627 memcpy(cp, (char *)&emptydir, sizeof emptydir);
628 dirty(bp);
629 dp->di_nlink = 2;
630 inodirty();
631 #ifdef AFS_SUN5_ENVX
632 if (!inocached(ino)) {
633 if (debug)
634 printf("inode %d added to directory cache\n", ino);
635 cacheino(dp, ino);
636 } else {
637 /*
638 * re-using an old directory inode
639 */
640 inp = getinoinfo(ino);
641 inp->i_isize = dp->di_size;
642 inp->i_numblks = dp->di_blocks * sizeof(daddr_t);
643 inp->i_parent = parent;
644 memcpy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
645 (int)inp->i_numblks);
646 }
647 #endif
648 if (ino == ROOTINO) {
649 lncntp[ino] = dp->di_nlink;
650 return (ino);
651 }
652 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
653 freeino(ino);
654 return (0);
655 }
656 statemap[ino] = statemap[parent];
657 if (statemap[ino] == DSTATE) {
658 lncntp[ino] = dp->di_nlink;
659 lncntp[parent]++;
660 }
661 dp = ginode(parent);
662 dp->di_nlink++;
663 inodirty();
664 return (ino);
665 }
666
667 /*
668 * free a directory inode
669 */
670 freedir(ino, parent)
671 ino_t ino, parent;
672 {
673 struct dinode *dp;
674
675 if (ino != parent) {
676 dp = ginode(parent);
677 dp->di_nlink--;
678 inodirty();
679 }
680 freeino(ino);
681 }
682
683 /*
684 * generate a temporary name for the lost+found directory.
685 */
686 lftempname(bufp, ino)
687 char *bufp;
688 ino_t ino;
689 {
690 ino_t in;
691 char *cp;
692 int namlen;
693
694 cp = bufp + 2;
695 for (in = maxino; in > 0; in /= 10)
696 cp++;
697 *--cp = 0;
698 namlen = cp - bufp;
699 in = ino;
700 while (cp > bufp) {
701 *--cp = (in % 10) + '0';
702 in /= 10;
703 }
704 *cp = '#';
705 return (namlen);
706 }
707
708 /*
709 * Get a directory block.
710 * Insure that it is held until another is requested.
711 */
712 struct bufarea *
713 getdirblk(blkno, size)
714 daddr_t blkno;
715 long size;
716 {
717 if (mlk_pbp != 0)
718 mlk_pbp->b_flags &= ~B_INUSE;
719 mlk_pbp = getdatablk(blkno, size);
720 return (mlk_pbp);
721 }