]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/dir.c
Fix kernel stack disclosure in UFS/FFS.
[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  * 4. 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         int dc;
149
150         blksiz = idesc->id_numfrags * sblock.fs_fsize;
151         bp = getdirblk(idesc->id_blkno, blksiz);
152         if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
153             idesc->id_loc < blksiz) {
154                 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
155                 if ((dc = dircheck(idesc, dp)) > 0) {
156                         if (dc == 2) {
157                                 /*
158                                  * dircheck() cleared unused directory space.
159                                  * Mark the buffer as dirty to write it out.
160                                  */
161                                 dirty(bp);
162                         }
163                         goto dpok;
164                 }
165                 if (idesc->id_fix == IGNORE)
166                         return (0);
167                 fix = dofix(idesc, "DIRECTORY CORRUPTED");
168                 bp = getdirblk(idesc->id_blkno, blksiz);
169                 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
170                 dp->d_reclen = DIRBLKSIZ;
171                 dp->d_ino = 0;
172                 dp->d_type = 0;
173                 dp->d_namlen = 0;
174                 dp->d_name[0] = '\0';
175                 if (fix)
176                         dirty(bp);
177                 idesc->id_loc += DIRBLKSIZ;
178                 idesc->id_filesize -= DIRBLKSIZ;
179                 return (dp);
180         }
181 dpok:
182         if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
183                 return NULL;
184         dploc = idesc->id_loc;
185         dp = (struct direct *)(bp->b_un.b_buf + dploc);
186         idesc->id_loc += dp->d_reclen;
187         idesc->id_filesize -= dp->d_reclen;
188         if ((idesc->id_loc % DIRBLKSIZ) == 0)
189                 return (dp);
190         ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
191         if (idesc->id_loc < blksiz && idesc->id_filesize > 0) {
192                 if ((dc = dircheck(idesc, ndp)) == 0) {
193                         size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
194                         idesc->id_loc += size;
195                         idesc->id_filesize -= size;
196                         if (idesc->id_fix == IGNORE)
197                                 return (0);
198                         fix = dofix(idesc, "DIRECTORY CORRUPTED");
199                         bp = getdirblk(idesc->id_blkno, blksiz);
200                         dp = (struct direct *)(bp->b_un.b_buf + dploc);
201                         dp->d_reclen += size;
202                         if (fix)
203                                 dirty(bp);
204                 } else if (dc == 2) {
205                         /*
206                          * dircheck() cleared unused directory space.
207                          * Mark the buffer as dirty to write it out.
208                          */
209                         dirty(bp);
210                 }
211         }
212         return (dp);
213 }
214
215 /*
216  * Verify that a directory entry is valid.
217  * This is a superset of the checks made in the kernel.
218  * Also optionally clears padding and unused directory space.
219  *
220  * Returns 0 if the entry is bad, 1 if the entry is good and no changes
221  * were made, and 2 if the entry is good but modified to clear out padding
222  * and unused space and needs to be written back to disk.
223  */
224 static int
225 dircheck(struct inodesc *idesc, struct direct *dp)
226 {
227         size_t size;
228         char *cp;
229         u_char type;
230         u_int8_t namlen;
231         int spaceleft, modified, unused;
232
233         modified = 0;
234         spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
235         if (dp->d_reclen == 0 ||
236             dp->d_reclen > spaceleft ||
237             (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0)
238                 goto bad;
239         if (dp->d_ino == 0) {
240                 /*
241                  * Special case of an unused directory entry. Normally
242                  * the kernel would coalesce unused space with the previous
243                  * entry by extending its d_reclen, but there are situations
244                  * (e.g. fsck) where that doesn't occur.
245                  * If we're clearing out directory cruft (-z flag), then make
246                  * sure this entry gets fully cleared as well.
247                  */
248                 if (zflag && fswritefd >= 0) {
249                         if (dp->d_type != 0) {
250                                 dp->d_type = 0;
251                                 modified = 1;
252                         }
253                         if (dp->d_namlen != 0) {
254                                 dp->d_namlen = 0;
255                                 modified = 1;
256                         }
257                         if (dp->d_name[0] != '\0') {
258                                 dp->d_name[0] = '\0';
259                                 modified = 1;
260                         }
261                 }
262                 goto good;
263         }
264         size = DIRSIZ(0, dp);
265         namlen = dp->d_namlen;
266         type = dp->d_type;
267         if (dp->d_reclen < size ||
268             idesc->id_filesize < size ||
269             namlen == 0 ||
270             type > 15)
271                 goto bad;
272         for (cp = dp->d_name, size = 0; size < namlen; size++)
273                 if (*cp == '\0' || (*cp++ == '/'))
274                         goto bad;
275         if (*cp != '\0')
276                 goto bad;
277
278 good:
279         if (zflag && fswritefd >= 0) {
280                 /*
281                  * Clear unused directory entry space, including the d_name
282                  * padding.
283                  */
284                 /* First figure the number of pad bytes. */
285                 unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1);
286
287                 /* Add in the free space to the end of the record. */
288                 unused += dp->d_reclen - DIRSIZ(0, dp);
289
290                 /*
291                  * Now clear out the unused space, keeping track if we actually
292                  * changed anything.
293                  */
294                 for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) {
295                         if (*cp != '\0') {
296                                 *cp = '\0';
297                                 modified = 1;
298                         }
299                 }
300                 
301                 if (modified) {
302                         return 2;
303                 }
304         }
305
306         return (1);
307
308 bad:
309         if (debug)
310                 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
311                     dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
312                     dp->d_name);
313         return (0);
314 }
315
316 void
317 direrror(ino_t ino, const char *errmesg)
318 {
319
320         fileerror(ino, ino, errmesg);
321 }
322
323 void
324 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
325 {
326         union dinode *dp;
327         char pathbuf[MAXPATHLEN + 1];
328
329         pwarn("%s ", errmesg);
330         pinode(ino);
331         printf("\n");
332         getpathname(pathbuf, cwd, ino);
333         if (ino < ROOTINO || ino > maxino) {
334                 pfatal("NAME=%s\n", pathbuf);
335                 return;
336         }
337         dp = ginode(ino);
338         if (ftypeok(dp))
339                 pfatal("%s=%s\n",
340                     (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
341                     pathbuf);
342         else
343                 pfatal("NAME=%s\n", pathbuf);
344 }
345
346 void
347 adjust(struct inodesc *idesc, int lcnt)
348 {
349         union dinode *dp;
350         int saveresolved;
351
352         dp = ginode(idesc->id_number);
353         if (DIP(dp, di_nlink) == lcnt) {
354                 /*
355                  * If we have not hit any unresolved problems, are running
356                  * in preen mode, and are on a file system using soft updates,
357                  * then just toss any partially allocated files.
358                  */
359                 if (resolved && (preen || bkgrdflag) && usedsoftdep) {
360                         clri(idesc, "UNREF", 1);
361                         return;
362                 } else {
363                         /*
364                          * The file system can be marked clean even if
365                          * a file is not linked up, but is cleared.
366                          * Hence, resolved should not be cleared when
367                          * linkup is answered no, but clri is answered yes.
368                          */
369                         saveresolved = resolved;
370                         if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
371                                 resolved = saveresolved;
372                                 clri(idesc, "UNREF", 0);
373                                 return;
374                         }
375                         /*
376                          * Account for the new reference created by linkup().
377                          */
378                         dp = ginode(idesc->id_number);
379                         lcnt--;
380                 }
381         }
382         if (lcnt != 0) {
383                 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
384                         ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
385                 pinode(idesc->id_number);
386                 printf(" COUNT %d SHOULD BE %d",
387                         DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
388                 if (preen || usedsoftdep) {
389                         if (lcnt < 0) {
390                                 printf("\n");
391                                 pfatal("LINK COUNT INCREASING");
392                         }
393                         if (preen)
394                                 printf(" (ADJUSTED)\n");
395                 }
396                 if (preen || reply("ADJUST") == 1) {
397                         if (bkgrdflag == 0) {
398                                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
399                                 inodirty();
400                         } else {
401                                 cmd.value = idesc->id_number;
402                                 cmd.size = -lcnt;
403                                 if (debug)
404                                         printf("adjrefcnt ino %ld amt %lld\n",
405                                             (long)cmd.value,
406                                             (long long)cmd.size);
407                                 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
408                                     &cmd, sizeof cmd) == -1)
409                                         rwerror("ADJUST INODE", cmd.value);
410                         }
411                 }
412         }
413 }
414
415 static int
416 mkentry(struct inodesc *idesc)
417 {
418         struct direct *dirp = idesc->id_dirp;
419         struct direct newent;
420         int newlen, oldlen;
421
422         newent.d_namlen = strlen(idesc->id_name);
423         newlen = DIRSIZ(0, &newent);
424         if (dirp->d_ino != 0)
425                 oldlen = DIRSIZ(0, dirp);
426         else
427                 oldlen = 0;
428         if (dirp->d_reclen - oldlen < newlen)
429                 return (KEEPON);
430         newent.d_reclen = dirp->d_reclen - oldlen;
431         dirp->d_reclen = oldlen;
432         dirp = (struct direct *)(((char *)dirp) + oldlen);
433         dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
434         dirp->d_reclen = newent.d_reclen;
435         dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
436         dirp->d_namlen = newent.d_namlen;
437         memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
438         return (ALTERED|STOP);
439 }
440
441 static int
442 chgino(struct inodesc *idesc)
443 {
444         struct direct *dirp = idesc->id_dirp;
445
446         if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
447                 return (KEEPON);
448         dirp->d_ino = idesc->id_parent;
449         dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
450         return (ALTERED|STOP);
451 }
452
453 int
454 linkup(ino_t orphan, ino_t parentdir, char *name)
455 {
456         union dinode *dp;
457         int lostdir;
458         ino_t oldlfdir;
459         struct inodesc idesc;
460         char tempname[BUFSIZ];
461
462         memset(&idesc, 0, sizeof(struct inodesc));
463         dp = ginode(orphan);
464         lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
465         pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
466         pinode(orphan);
467         if (preen && DIP(dp, di_size) == 0)
468                 return (0);
469         if (cursnapshot != 0) {
470                 pfatal("FILE LINKUP IN SNAPSHOT");
471                 return (0);
472         }
473         if (preen)
474                 printf(" (RECONNECTED)\n");
475         else
476                 if (reply("RECONNECT") == 0)
477                         return (0);
478         if (lfdir == 0) {
479                 dp = ginode(ROOTINO);
480                 idesc.id_name = strdup(lfname);
481                 idesc.id_type = DATA;
482                 idesc.id_func = findino;
483                 idesc.id_number = ROOTINO;
484                 if ((ckinode(dp, &idesc) & FOUND) != 0) {
485                         lfdir = idesc.id_parent;
486                 } else {
487                         pwarn("NO lost+found DIRECTORY");
488                         if (preen || reply("CREATE")) {
489                                 lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
490                                 if (lfdir != 0) {
491                                         if (makeentry(ROOTINO, lfdir, lfname) != 0) {
492                                                 numdirs++;
493                                                 if (preen)
494                                                         printf(" (CREATED)\n");
495                                         } else {
496                                                 freedir(lfdir, ROOTINO);
497                                                 lfdir = 0;
498                                                 if (preen)
499                                                         printf("\n");
500                                         }
501                                 }
502                         }
503                 }
504                 if (lfdir == 0) {
505                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
506                         printf("\n\n");
507                         return (0);
508                 }
509         }
510         dp = ginode(lfdir);
511         if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
512                 pfatal("lost+found IS NOT A DIRECTORY");
513                 if (reply("REALLOCATE") == 0)
514                         return (0);
515                 oldlfdir = lfdir;
516                 if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
517                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
518                         return (0);
519                 }
520                 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
521                         pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
522                         return (0);
523                 }
524                 inodirty();
525                 idesc.id_type = ADDR;
526                 idesc.id_func = pass4check;
527                 idesc.id_number = oldlfdir;
528                 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
529                 inoinfo(oldlfdir)->ino_linkcnt = 0;
530                 dp = ginode(lfdir);
531         }
532         if (inoinfo(lfdir)->ino_state != DFOUND) {
533                 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
534                 return (0);
535         }
536         (void)lftempname(tempname, orphan);
537         if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
538                 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
539                 printf("\n\n");
540                 return (0);
541         }
542         inoinfo(orphan)->ino_linkcnt--;
543         if (lostdir) {
544                 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
545                     parentdir != (ino_t)-1)
546                         (void)makeentry(orphan, lfdir, "..");
547                 dp = ginode(lfdir);
548                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
549                 inodirty();
550                 inoinfo(lfdir)->ino_linkcnt++;
551                 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
552                 if (parentdir != (ino_t)-1) {
553                         printf("PARENT WAS I=%lu\n", (u_long)parentdir);
554                         /*
555                          * The parent directory, because of the ordering
556                          * guarantees, has had the link count incremented
557                          * for the child, but no entry was made.  This
558                          * fixes the parent link count so that fsck does
559                          * not need to be rerun.
560                          */
561                         inoinfo(parentdir)->ino_linkcnt++;
562                 }
563                 if (preen == 0)
564                         printf("\n");
565         }
566         return (1);
567 }
568
569 /*
570  * fix an entry in a directory.
571  */
572 int
573 changeino(ino_t dir, const char *name, ino_t newnum)
574 {
575         struct inodesc idesc;
576
577         memset(&idesc, 0, sizeof(struct inodesc));
578         idesc.id_type = DATA;
579         idesc.id_func = chgino;
580         idesc.id_number = dir;
581         idesc.id_fix = DONTKNOW;
582         idesc.id_name = strdup(name);
583         idesc.id_parent = newnum;       /* new value for name */
584         return (ckinode(ginode(dir), &idesc));
585 }
586
587 /*
588  * make an entry in a directory
589  */
590 int
591 makeentry(ino_t parent, ino_t ino, const char *name)
592 {
593         union dinode *dp;
594         struct inodesc idesc;
595         char pathbuf[MAXPATHLEN + 1];
596
597         if (parent < ROOTINO || parent >= maxino ||
598             ino < ROOTINO || ino >= maxino)
599                 return (0);
600         memset(&idesc, 0, sizeof(struct inodesc));
601         idesc.id_type = DATA;
602         idesc.id_func = mkentry;
603         idesc.id_number = parent;
604         idesc.id_parent = ino;  /* this is the inode to enter */
605         idesc.id_fix = DONTKNOW;
606         idesc.id_name = strdup(name);
607         dp = ginode(parent);
608         if (DIP(dp, di_size) % DIRBLKSIZ) {
609                 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
610                 inodirty();
611         }
612         if ((ckinode(dp, &idesc) & ALTERED) != 0)
613                 return (1);
614         getpathname(pathbuf, parent, parent);
615         dp = ginode(parent);
616         if (expanddir(dp, pathbuf) == 0)
617                 return (0);
618         return (ckinode(dp, &idesc) & ALTERED);
619 }
620
621 /*
622  * Attempt to expand the size of a directory
623  */
624 static int
625 expanddir(union dinode *dp, char *name)
626 {
627         ufs2_daddr_t lastbn, newblk;
628         struct bufarea *bp;
629         char *cp, firstblk[DIRBLKSIZ];
630
631         lastbn = lblkno(&sblock, DIP(dp, di_size));
632         if (lastbn >= NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
633             DIP(dp, di_size) == 0)
634                 return (0);
635         if ((newblk = allocblk(sblock.fs_frag)) == 0)
636                 return (0);
637         DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
638         DIP_SET(dp, di_db[lastbn], newblk);
639         DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
640         DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
641         bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
642                 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
643         if (bp->b_errs)
644                 goto bad;
645         memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
646         bp = getdirblk(newblk, sblock.fs_bsize);
647         if (bp->b_errs)
648                 goto bad;
649         memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
650         for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
651              cp < &bp->b_un.b_buf[sblock.fs_bsize];
652              cp += DIRBLKSIZ)
653                 memmove(cp, &emptydir, sizeof emptydir);
654         dirty(bp);
655         bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
656                 sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
657         if (bp->b_errs)
658                 goto bad;
659         memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
660         pwarn("NO SPACE LEFT IN %s", name);
661         if (preen)
662                 printf(" (EXPANDED)\n");
663         else if (reply("EXPAND") == 0)
664                 goto bad;
665         dirty(bp);
666         inodirty();
667         return (1);
668 bad:
669         DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
670         DIP_SET(dp, di_db[lastbn + 1], 0);
671         DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
672         DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
673         freeblk(newblk, sblock.fs_frag);
674         return (0);
675 }
676
677 /*
678  * allocate a new directory
679  */
680 ino_t
681 allocdir(ino_t parent, ino_t request, int mode)
682 {
683         ino_t ino;
684         char *cp;
685         union dinode *dp;
686         struct bufarea *bp;
687         struct inoinfo *inp;
688         struct dirtemplate *dirp;
689
690         ino = allocino(request, IFDIR|mode);
691         dirp = &dirhead;
692         dirp->dot_ino = ino;
693         dirp->dotdot_ino = parent;
694         dp = ginode(ino);
695         bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
696         if (bp->b_errs) {
697                 freeino(ino);
698                 return (0);
699         }
700         memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
701         for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
702              cp < &bp->b_un.b_buf[sblock.fs_fsize];
703              cp += DIRBLKSIZ)
704                 memmove(cp, &emptydir, sizeof emptydir);
705         dirty(bp);
706         DIP_SET(dp, di_nlink, 2);
707         inodirty();
708         if (ino == ROOTINO) {
709                 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
710                 cacheino(dp, ino);
711                 return(ino);
712         }
713         if (!INO_IS_DVALID(parent)) {
714                 freeino(ino);
715                 return (0);
716         }
717         cacheino(dp, ino);
718         inp = getinoinfo(ino);
719         inp->i_parent = parent;
720         inp->i_dotdot = parent;
721         inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
722         if (inoinfo(ino)->ino_state == DSTATE) {
723                 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
724                 inoinfo(parent)->ino_linkcnt++;
725         }
726         dp = ginode(parent);
727         DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
728         inodirty();
729         return (ino);
730 }
731
732 /*
733  * free a directory inode
734  */
735 static void
736 freedir(ino_t ino, ino_t parent)
737 {
738         union dinode *dp;
739
740         if (ino != parent) {
741                 dp = ginode(parent);
742                 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
743                 inodirty();
744         }
745         freeino(ino);
746 }
747
748 /*
749  * generate a temporary name for the lost+found directory.
750  */
751 static int
752 lftempname(char *bufp, ino_t ino)
753 {
754         ino_t in;
755         char *cp;
756         int namlen;
757
758         cp = bufp + 2;
759         for (in = maxino; in > 0; in /= 10)
760                 cp++;
761         *--cp = 0;
762         namlen = cp - bufp;
763         in = ino;
764         while (cp > bufp) {
765                 *--cp = (in % 10) + '0';
766                 in /= 10;
767         }
768         *cp = '#';
769         return (namlen);
770 }
771
772 /*
773  * Get a directory block.
774  * Insure that it is held until another is requested.
775  */
776 static struct bufarea *
777 getdirblk(ufs2_daddr_t blkno, long size)
778 {
779
780         if (pdirbp != NULL)
781                 pdirbp->b_flags &= ~B_INUSE;
782         pdirbp = getdatablk(blkno, size, BT_DIRDATA);
783         return (pdirbp);
784 }