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