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