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