]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/dir.c
Merge OpenSSL 1.0.2m.
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / dir.c
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)dir.c 8.8 (Berkeley) 4/28/95";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
46
47 #include <err.h>
48 #include <string.h>
49
50 #include "fsck.h"
51
52 static struct   dirtemplate emptydir = {
53         0, DIRBLKSIZ, DT_UNKNOWN, 0, "",
54         0, 0, DT_UNKNOWN, 0, ""
55 };
56 static struct   dirtemplate dirhead = {
57         0, 12, DT_DIR, 1, ".",
58         0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
59 };
60
61 static int chgino(struct inodesc *);
62 static int dircheck(struct inodesc *, struct direct *);
63 static int expanddir(union dinode *dp, char *name);
64 static void freedir(ino_t ino, ino_t parent);
65 static struct direct *fsck_readdir(struct inodesc *);
66 static struct bufarea *getdirblk(ufs2_daddr_t blkno, long size);
67 static int lftempname(char *bufp, ino_t ino);
68 static int mkentry(struct inodesc *);
69
70 /*
71  * Propagate connected state through the tree.
72  */
73 void
74 propagate(void)
75 {
76         struct inoinfo **inpp, *inp;
77         struct inoinfo **inpend;
78         long change;
79
80         inpend = &inpsort[inplast];
81         do {
82                 change = 0;
83                 for (inpp = inpsort; inpp < inpend; inpp++) {
84                         inp = *inpp;
85                         if (inp->i_parent == 0)
86                                 continue;
87                         if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
88                             INO_IS_DUNFOUND(inp->i_number)) {
89                                 inoinfo(inp->i_number)->ino_state = DFOUND;
90                                 change++;
91                         }
92                 }
93         } while (change > 0);
94 }
95
96 /*
97  * Scan each entry in a directory block.
98  */
99 int
100 dirscan(struct inodesc *idesc)
101 {
102         struct direct *dp;
103         struct bufarea *bp;
104         u_int dsize, n;
105         long blksiz;
106         char dbuf[DIRBLKSIZ];
107
108         if (idesc->id_type != DATA)
109                 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
110         if (idesc->id_entryno == 0 &&
111             (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
112                 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
113         blksiz = idesc->id_numfrags * sblock.fs_fsize;
114         if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
115                 idesc->id_filesize -= blksiz;
116                 return (SKIP);
117         }
118         idesc->id_loc = 0;
119         for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
120                 dsize = dp->d_reclen;
121                 if (dsize > sizeof(dbuf))
122                         dsize = sizeof(dbuf);
123                 memmove(dbuf, dp, (size_t)dsize);
124                 idesc->id_dirp = (struct direct *)dbuf;
125                 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
126                         bp = getdirblk(idesc->id_blkno, blksiz);
127                         memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
128                             (size_t)dsize);
129                         dirty(bp);
130                         sbdirty();
131                         rerun = 1;
132                 }
133                 if (n & STOP)
134                         return (n);
135         }
136         return (idesc->id_filesize > 0 ? KEEPON : STOP);
137 }
138
139 /*
140  * get next entry in a directory.
141  */
142 static struct direct *
143 fsck_readdir(struct inodesc *idesc)
144 {
145         struct direct *dp, *ndp;
146         struct bufarea *bp;
147         long size, blksiz, fix, dploc;
148
149         blksiz = idesc->id_numfrags * sblock.fs_fsize;
150         bp = getdirblk(idesc->id_blkno, blksiz);
151         if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
152             idesc->id_loc < blksiz) {
153                 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
154                 if (dircheck(idesc, dp))
155                         goto dpok;
156                 if (idesc->id_fix == IGNORE)
157                         return (0);
158                 fix = dofix(idesc, "DIRECTORY CORRUPTED");
159                 bp = getdirblk(idesc->id_blkno, blksiz);
160                 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
161                 dp->d_reclen = DIRBLKSIZ;
162                 dp->d_ino = 0;
163                 dp->d_type = 0;
164                 dp->d_namlen = 0;
165                 dp->d_name[0] = '\0';
166                 if (fix)
167                         dirty(bp);
168                 idesc->id_loc += DIRBLKSIZ;
169                 idesc->id_filesize -= DIRBLKSIZ;
170                 return (dp);
171         }
172 dpok:
173         if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
174                 return NULL;
175         dploc = idesc->id_loc;
176         dp = (struct direct *)(bp->b_un.b_buf + dploc);
177         idesc->id_loc += dp->d_reclen;
178         idesc->id_filesize -= dp->d_reclen;
179         if ((idesc->id_loc % DIRBLKSIZ) == 0)
180                 return (dp);
181         ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
182         if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
183             dircheck(idesc, ndp) == 0) {
184                 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
185                 idesc->id_loc += size;
186                 idesc->id_filesize -= size;
187                 if (idesc->id_fix == IGNORE)
188                         return (0);
189                 fix = dofix(idesc, "DIRECTORY CORRUPTED");
190                 bp = getdirblk(idesc->id_blkno, blksiz);
191                 dp = (struct direct *)(bp->b_un.b_buf + dploc);
192                 dp->d_reclen += size;
193                 if (fix)
194                         dirty(bp);
195         }
196         return (dp);
197 }
198
199 /*
200  * Verify that a directory entry is valid.
201  * This is a superset of the checks made in the kernel.
202  */
203 static int
204 dircheck(struct inodesc *idesc, struct direct *dp)
205 {
206         size_t size;
207         char *cp;
208         u_char type;
209         u_int8_t namlen;
210         int spaceleft;
211
212         spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
213         if (dp->d_reclen == 0 ||
214             dp->d_reclen > spaceleft ||
215             (dp->d_reclen & 0x3) != 0)
216                 goto bad;
217         if (dp->d_ino == 0)
218                 return (1);
219         size = DIRSIZ(0, dp);
220         namlen = dp->d_namlen;
221         type = dp->d_type;
222         if (dp->d_reclen < size ||
223             idesc->id_filesize < size ||
224             namlen == 0 ||
225             type > 15)
226                 goto bad;
227         for (cp = dp->d_name, size = 0; size < namlen; size++)
228                 if (*cp == '\0' || (*cp++ == '/'))
229                         goto bad;
230         if (*cp != '\0')
231                 goto bad;
232         return (1);
233 bad:
234         if (debug)
235                 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
236                     dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
237                     dp->d_name);
238         return (0);
239 }
240
241 void
242 direrror(ino_t ino, const char *errmesg)
243 {
244
245         fileerror(ino, ino, errmesg);
246 }
247
248 void
249 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
250 {
251         union dinode *dp;
252         char pathbuf[MAXPATHLEN + 1];
253
254         pwarn("%s ", errmesg);
255         pinode(ino);
256         printf("\n");
257         getpathname(pathbuf, cwd, ino);
258         if (ino < UFS_ROOTINO || ino > maxino) {
259                 pfatal("NAME=%s\n", pathbuf);
260                 return;
261         }
262         dp = ginode(ino);
263         if (ftypeok(dp))
264                 pfatal("%s=%s\n",
265                     (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
266                     pathbuf);
267         else
268                 pfatal("NAME=%s\n", pathbuf);
269 }
270
271 void
272 adjust(struct inodesc *idesc, int lcnt)
273 {
274         union dinode *dp;
275         int saveresolved;
276
277         dp = ginode(idesc->id_number);
278         if (DIP(dp, di_nlink) == lcnt) {
279                 /*
280                  * If we have not hit any unresolved problems, are running
281                  * in preen mode, and are on a file system using soft updates,
282                  * then just toss any partially allocated files.
283                  */
284                 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
285                         clri(idesc, "UNREF", 1);
286                         return;
287                 } else {
288                         /*
289                          * The file system can be marked clean even if
290                          * a file is not linked up, but is cleared.
291                          * Hence, resolved should not be cleared when
292                          * linkup is answered no, but clri is answered yes.
293                          */
294                         saveresolved = resolved;
295                         if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
296                                 resolved = saveresolved;
297                                 clri(idesc, "UNREF", 0);
298                                 return;
299                         }
300                         /*
301                          * Account for the new reference created by linkup().
302                          */
303                         dp = ginode(idesc->id_number);
304                         lcnt--;
305                 }
306         }
307         if (lcnt != 0) {
308                 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
309                         ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
310                 pinode(idesc->id_number);
311                 printf(" COUNT %d SHOULD BE %d",
312                         DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
313                 if (preen || usedsoftdep) {
314                         if (lcnt < 0) {
315                                 printf("\n");
316                                 pfatal("LINK COUNT INCREASING");
317                         }
318                         if (preen)
319                                 printf(" (ADJUSTED)\n");
320                 }
321                 if (preen || reply("ADJUST") == 1) {
322                         if (bkgrdflag == 0) {
323                                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
324                                 inodirty();
325                         } else {
326                                 cmd.value = idesc->id_number;
327                                 cmd.size = -lcnt;
328                                 if (debug)
329                                         printf("adjrefcnt ino %ld amt %lld\n",
330                                             (long)cmd.value,
331                                             (long long)cmd.size);
332                                 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
333                                     &cmd, sizeof cmd) == -1)
334                                         rwerror("ADJUST INODE", cmd.value);
335                         }
336                 }
337         }
338 }
339
340 static int
341 mkentry(struct inodesc *idesc)
342 {
343         struct direct *dirp = idesc->id_dirp;
344         struct direct newent;
345         int newlen, oldlen;
346
347         newent.d_namlen = strlen(idesc->id_name);
348         newlen = DIRSIZ(0, &newent);
349         if (dirp->d_ino != 0)
350                 oldlen = DIRSIZ(0, dirp);
351         else
352                 oldlen = 0;
353         if (dirp->d_reclen - oldlen < newlen)
354                 return (KEEPON);
355         newent.d_reclen = dirp->d_reclen - oldlen;
356         dirp->d_reclen = oldlen;
357         dirp = (struct direct *)(((char *)dirp) + oldlen);
358         dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
359         dirp->d_reclen = newent.d_reclen;
360         dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
361         dirp->d_namlen = newent.d_namlen;
362         memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
363         return (ALTERED|STOP);
364 }
365
366 static int
367 chgino(struct inodesc *idesc)
368 {
369         struct direct *dirp = idesc->id_dirp;
370
371         if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
372                 return (KEEPON);
373         dirp->d_ino = idesc->id_parent;
374         dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
375         return (ALTERED|STOP);
376 }
377
378 int
379 linkup(ino_t orphan, ino_t parentdir, char *name)
380 {
381         union dinode *dp;
382         int lostdir;
383         ino_t oldlfdir;
384         struct inodesc idesc;
385         char tempname[BUFSIZ];
386
387         memset(&idesc, 0, sizeof(struct inodesc));
388         dp = ginode(orphan);
389         lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
390         pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
391         pinode(orphan);
392         if (preen && DIP(dp, di_size) == 0)
393                 return (0);
394         if (cursnapshot != 0) {
395                 pfatal("FILE LINKUP IN SNAPSHOT");
396                 return (0);
397         }
398         if (preen)
399                 printf(" (RECONNECTED)\n");
400         else
401                 if (reply("RECONNECT") == 0)
402                         return (0);
403         if (lfdir == 0) {
404                 dp = ginode(UFS_ROOTINO);
405                 idesc.id_name = strdup(lfname);
406                 idesc.id_type = DATA;
407                 idesc.id_func = findino;
408                 idesc.id_number = UFS_ROOTINO;
409                 if ((ckinode(dp, &idesc) & FOUND) != 0) {
410                         lfdir = idesc.id_parent;
411                 } else {
412                         pwarn("NO lost+found DIRECTORY");
413                         if (preen || reply("CREATE")) {
414                                 lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode);
415                                 if (lfdir != 0) {
416                                         if (makeentry(UFS_ROOTINO, lfdir,
417                                             lfname) != 0) {
418                                                 numdirs++;
419                                                 if (preen)
420                                                         printf(" (CREATED)\n");
421                                         } else {
422                                                 freedir(lfdir, UFS_ROOTINO);
423                                                 lfdir = 0;
424                                                 if (preen)
425                                                         printf("\n");
426                                         }
427                                 }
428                         }
429                 }
430                 if (lfdir == 0) {
431                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
432                         printf("\n\n");
433                         return (0);
434                 }
435         }
436         dp = ginode(lfdir);
437         if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
438                 pfatal("lost+found IS NOT A DIRECTORY");
439                 if (reply("REALLOCATE") == 0)
440                         return (0);
441                 oldlfdir = lfdir;
442                 if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) {
443                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
444                         return (0);
445                 }
446                 if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
447                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
448                         return (0);
449                 }
450                 inodirty();
451                 idesc.id_type = ADDR;
452                 idesc.id_func = pass4check;
453                 idesc.id_number = oldlfdir;
454                 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
455                 inoinfo(oldlfdir)->ino_linkcnt = 0;
456                 dp = ginode(lfdir);
457         }
458         if (inoinfo(lfdir)->ino_state != DFOUND) {
459                 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
460                 return (0);
461         }
462         (void)lftempname(tempname, orphan);
463         if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
464                 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
465                 printf("\n\n");
466                 return (0);
467         }
468         inoinfo(orphan)->ino_linkcnt--;
469         if (lostdir) {
470                 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
471                     parentdir != (ino_t)-1)
472                         (void)makeentry(orphan, lfdir, "..");
473                 dp = ginode(lfdir);
474                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
475                 inodirty();
476                 inoinfo(lfdir)->ino_linkcnt++;
477                 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
478                 if (parentdir != (ino_t)-1) {
479                         printf("PARENT WAS I=%lu\n", (u_long)parentdir);
480                         /*
481                          * The parent directory, because of the ordering
482                          * guarantees, has had the link count incremented
483                          * for the child, but no entry was made.  This
484                          * fixes the parent link count so that fsck does
485                          * not need to be rerun.
486                          */
487                         inoinfo(parentdir)->ino_linkcnt++;
488                 }
489                 if (preen == 0)
490                         printf("\n");
491         }
492         return (1);
493 }
494
495 /*
496  * fix an entry in a directory.
497  */
498 int
499 changeino(ino_t dir, const char *name, ino_t newnum)
500 {
501         struct inodesc idesc;
502
503         memset(&idesc, 0, sizeof(struct inodesc));
504         idesc.id_type = DATA;
505         idesc.id_func = chgino;
506         idesc.id_number = dir;
507         idesc.id_fix = DONTKNOW;
508         idesc.id_name = strdup(name);
509         idesc.id_parent = newnum;       /* new value for name */
510         return (ckinode(ginode(dir), &idesc));
511 }
512
513 /*
514  * make an entry in a directory
515  */
516 int
517 makeentry(ino_t parent, ino_t ino, const char *name)
518 {
519         union dinode *dp;
520         struct inodesc idesc;
521         char pathbuf[MAXPATHLEN + 1];
522
523         if (parent < UFS_ROOTINO || parent >= maxino ||
524             ino < UFS_ROOTINO || ino >= maxino)
525                 return (0);
526         memset(&idesc, 0, sizeof(struct inodesc));
527         idesc.id_type = DATA;
528         idesc.id_func = mkentry;
529         idesc.id_number = parent;
530         idesc.id_parent = ino;  /* this is the inode to enter */
531         idesc.id_fix = DONTKNOW;
532         idesc.id_name = strdup(name);
533         dp = ginode(parent);
534         if (DIP(dp, di_size) % DIRBLKSIZ) {
535                 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
536                 inodirty();
537         }
538         if ((ckinode(dp, &idesc) & ALTERED) != 0)
539                 return (1);
540         getpathname(pathbuf, parent, parent);
541         dp = ginode(parent);
542         if (expanddir(dp, pathbuf) == 0)
543                 return (0);
544         return (ckinode(dp, &idesc) & ALTERED);
545 }
546
547 /*
548  * Attempt to expand the size of a directory
549  */
550 static int
551 expanddir(union dinode *dp, char *name)
552 {
553         ufs2_daddr_t lastbn, newblk;
554         struct bufarea *bp;
555         char *cp, firstblk[DIRBLKSIZ];
556
557         lastbn = lblkno(&sblock, DIP(dp, di_size));
558         if (lastbn >= UFS_NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
559             DIP(dp, di_size) == 0)
560                 return (0);
561         if ((newblk = allocblk(sblock.fs_frag)) == 0)
562                 return (0);
563         DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
564         DIP_SET(dp, di_db[lastbn], newblk);
565         DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
566         DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
567         bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
568                 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
569         if (bp->b_errs)
570                 goto bad;
571         memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
572         bp = getdirblk(newblk, sblock.fs_bsize);
573         if (bp->b_errs)
574                 goto bad;
575         memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
576         for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
577              cp < &bp->b_un.b_buf[sblock.fs_bsize];
578              cp += DIRBLKSIZ)
579                 memmove(cp, &emptydir, sizeof emptydir);
580         dirty(bp);
581         bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
582                 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
583         if (bp->b_errs)
584                 goto bad;
585         memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
586         pwarn("NO SPACE LEFT IN %s", name);
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         DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
596         DIP_SET(dp, di_db[lastbn + 1], 0);
597         DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
598         DIP_SET(dp, di_blocks, DIP(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 ino_t
607 allocdir(ino_t parent, ino_t request, int mode)
608 {
609         ino_t ino;
610         char *cp;
611         union dinode *dp;
612         struct bufarea *bp;
613         struct inoinfo *inp;
614         struct dirtemplate *dirp;
615
616         ino = allocino(request, IFDIR|mode);
617         dirp = &dirhead;
618         dirp->dot_ino = ino;
619         dirp->dotdot_ino = parent;
620         dp = ginode(ino);
621         bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
622         if (bp->b_errs) {
623                 freeino(ino);
624                 return (0);
625         }
626         memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
627         for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
628              cp < &bp->b_un.b_buf[sblock.fs_fsize];
629              cp += DIRBLKSIZ)
630                 memmove(cp, &emptydir, sizeof emptydir);
631         dirty(bp);
632         DIP_SET(dp, di_nlink, 2);
633         inodirty();
634         if (ino == UFS_ROOTINO) {
635                 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
636                 cacheino(dp, ino);
637                 return(ino);
638         }
639         if (!INO_IS_DVALID(parent)) {
640                 freeino(ino);
641                 return (0);
642         }
643         cacheino(dp, ino);
644         inp = getinoinfo(ino);
645         inp->i_parent = parent;
646         inp->i_dotdot = parent;
647         inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
648         if (inoinfo(ino)->ino_state == DSTATE) {
649                 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
650                 inoinfo(parent)->ino_linkcnt++;
651         }
652         dp = ginode(parent);
653         DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
654         inodirty();
655         return (ino);
656 }
657
658 /*
659  * free a directory inode
660  */
661 static void
662 freedir(ino_t ino, ino_t parent)
663 {
664         union dinode *dp;
665
666         if (ino != parent) {
667                 dp = ginode(parent);
668                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
669                 inodirty();
670         }
671         freeino(ino);
672 }
673
674 /*
675  * generate a temporary name for the lost+found directory.
676  */
677 static int
678 lftempname(char *bufp, ino_t ino)
679 {
680         ino_t in;
681         char *cp;
682         int namlen;
683
684         cp = bufp + 2;
685         for (in = maxino; in > 0; in /= 10)
686                 cp++;
687         *--cp = 0;
688         namlen = cp - bufp;
689         in = ino;
690         while (cp > bufp) {
691                 *--cp = (in % 10) + '0';
692                 in /= 10;
693         }
694         *cp = '#';
695         return (namlen);
696 }
697
698 /*
699  * Get a directory block.
700  * Insure that it is held until another is requested.
701  */
702 static struct bufarea *
703 getdirblk(ufs2_daddr_t blkno, long size)
704 {
705
706         if (pdirbp != NULL)
707                 pdirbp->b_flags &= ~B_INUSE;
708         pdirbp = getdatablk(blkno, size, BT_DIRDATA);
709         return (pdirbp);
710 }