dir.c revision 1.37 1 /* $NetBSD: dir.c,v 1.37 2015/09/01 06:08:37 dholland Exp $ */
2
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 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/buf.h>
36 #include <sys/mount.h>
37
38 #include <ufs/lfs/lfs.h>
39 #include <ufs/lfs/lfs_accessors.h>
40 #include <ufs/lfs/lfs_inode.h>
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "bufcache.h"
48 #include "vnode.h"
49 #include "lfs_user.h"
50
51 #include "fsck.h"
52 #include "fsutil.h"
53 #include "extern.h"
54
55 const char *lfname = "lost+found";
56 int lfmode = 01700;
57 struct lfs_dirtemplate emptydir = {
58 .dot_ino = 0,
59 .dot_reclen = LFS_DIRBLKSIZ,
60 };
61 struct lfs_dirtemplate dirhead = {
62 .dot_ino = 0,
63 .dot_reclen = 12,
64 .dot_type = LFS_DT_DIR,
65 .dot_namlen = 1,
66 .dot_name = ".",
67 .dotdot_ino = 0,
68 .dotdot_reclen = LFS_DIRBLKSIZ - 12,
69 .dotdot_type = LFS_DT_DIR,
70 .dotdot_namlen = 2,
71 .dotdot_name = ".."
72 };
73 struct lfs_odirtemplate odirhead = {
74 .dot_ino = 0,
75 .dot_reclen = 12,
76 .dot_namlen = 1,
77 .dot_name = ".",
78 .dotdot_ino = 0,
79 .dotdot_reclen = LFS_DIRBLKSIZ - 12,
80 .dotdot_namlen = 2,
81 .dotdot_name = ".."
82 };
83
84 static int expanddir(struct uvnode *, union lfs_dinode *, char *);
85 static void freedir(ino_t, ino_t);
86 static struct lfs_direct *fsck_readdir(struct uvnode *, struct inodesc *);
87 static int lftempname(char *, ino_t);
88 static int mkentry(struct inodesc *);
89 static int chgino(struct inodesc *);
90
91 /*
92 * Propagate connected state through the tree.
93 */
94 void
95 propagate(void)
96 {
97 struct inoinfo **inpp, *inp, *pinp;
98 struct inoinfo **inpend;
99
100 /*
101 * Create a list of children for each directory.
102 */
103 inpend = &inpsort[inplast];
104 for (inpp = inpsort; inpp < inpend; inpp++) {
105 inp = *inpp;
106 if (inp->i_parent == 0 ||
107 inp->i_number == ULFS_ROOTINO)
108 continue;
109 pinp = getinoinfo(inp->i_parent);
110 inp->i_parentp = pinp;
111 inp->i_sibling = pinp->i_child;
112 pinp->i_child = inp;
113 }
114 inp = getinoinfo(ULFS_ROOTINO);
115 while (inp) {
116 statemap[inp->i_number] = DFOUND;
117 if (inp->i_child &&
118 statemap[inp->i_child->i_number] == DSTATE)
119 inp = inp->i_child;
120 else if (inp->i_sibling)
121 inp = inp->i_sibling;
122 else
123 inp = inp->i_parentp;
124 }
125 }
126
127 /*
128 * Scan each entry in a directory block.
129 */
130 int
131 dirscan(struct inodesc *idesc)
132 {
133 struct lfs_direct *dp;
134 struct ubuf *bp;
135 int dsize, n;
136 long blksiz;
137 char dbuf[LFS_DIRBLKSIZ];
138 struct uvnode *vp;
139
140 if (idesc->id_type != DATA)
141 errexit("wrong type to dirscan %d", idesc->id_type);
142 if (idesc->id_entryno == 0 &&
143 (idesc->id_filesize & (LFS_DIRBLKSIZ - 1)) != 0)
144 idesc->id_filesize = roundup(idesc->id_filesize, LFS_DIRBLKSIZ);
145 blksiz = idesc->id_numfrags * lfs_sb_getfsize(fs);
146 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
147 idesc->id_filesize -= blksiz;
148 return (SKIP);
149 }
150 idesc->id_loc = 0;
151
152 vp = vget(fs, idesc->id_number);
153 for (dp = fsck_readdir(vp, idesc); dp != NULL;
154 dp = fsck_readdir(vp, idesc)) {
155 dsize = dp->d_reclen;
156 memcpy(dbuf, dp, (size_t) dsize);
157 idesc->id_dirp = (struct lfs_direct *) dbuf;
158 if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
159 bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
160 memcpy(bp->b_data + idesc->id_loc - dsize, dbuf,
161 (size_t) dsize);
162 VOP_BWRITE(bp);
163 sbdirty();
164 }
165 if (n & STOP)
166 return (n);
167 }
168 return (idesc->id_filesize > 0 ? KEEPON : STOP);
169 }
170
171 /*
172 * get next entry in a directory.
173 */
174 static struct lfs_direct *
175 fsck_readdir(struct uvnode *vp, struct inodesc *idesc)
176 {
177 struct lfs_direct *dp, *ndp;
178 struct ubuf *bp;
179 long size, blksiz, fix, dploc;
180
181 blksiz = idesc->id_numfrags * lfs_sb_getfsize(fs);
182 bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
183 if (idesc->id_loc % LFS_DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
184 idesc->id_loc < blksiz) {
185 dp = (struct lfs_direct *) (bp->b_data + idesc->id_loc);
186 if (dircheck(idesc, dp))
187 goto dpok;
188 brelse(bp, 0);
189 if (idesc->id_fix == IGNORE)
190 return (0);
191 fix = dofix(idesc, "DIRECTORY CORRUPTED");
192 bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
193 dp = (struct lfs_direct *) (bp->b_data + idesc->id_loc);
194 dp->d_reclen = LFS_DIRBLKSIZ;
195 dp->d_ino = 0;
196 dp->d_type = 0;
197 dp->d_namlen = 0;
198 dp->d_name[0] = '\0';
199 if (fix)
200 VOP_BWRITE(bp);
201 else
202 brelse(bp, 0);
203 idesc->id_loc += LFS_DIRBLKSIZ;
204 idesc->id_filesize -= LFS_DIRBLKSIZ;
205 return (dp);
206 }
207 dpok:
208 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) {
209 brelse(bp, 0);
210 return NULL;
211 }
212 dploc = idesc->id_loc;
213 dp = (struct lfs_direct *) (bp->b_data + dploc);
214 idesc->id_loc += dp->d_reclen;
215 idesc->id_filesize -= dp->d_reclen;
216 if ((idesc->id_loc % LFS_DIRBLKSIZ) == 0) {
217 brelse(bp, 0);
218 return dp;
219 }
220 ndp = (struct lfs_direct *) (bp->b_data + idesc->id_loc);
221 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
222 dircheck(idesc, ndp) == 0) {
223 brelse(bp, 0);
224 size = LFS_DIRBLKSIZ - (idesc->id_loc % LFS_DIRBLKSIZ);
225 idesc->id_loc += size;
226 idesc->id_filesize -= size;
227 if (idesc->id_fix == IGNORE)
228 return 0;
229 fix = dofix(idesc, "DIRECTORY CORRUPTED");
230 bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
231 dp = (struct lfs_direct *) (bp->b_data + dploc);
232 dp->d_reclen += size;
233 if (fix)
234 VOP_BWRITE(bp);
235 else
236 brelse(bp, 0);
237 } else
238 brelse(bp, 0);
239
240 return (dp);
241 }
242
243 /*
244 * Verify that a directory entry is valid.
245 * This is a superset of the checks made in the kernel.
246 */
247 int
248 dircheck(struct inodesc *idesc, struct lfs_direct *dp)
249 {
250 int size;
251 char *cp;
252 u_char namlen, type;
253 int spaceleft;
254
255 spaceleft = LFS_DIRBLKSIZ - (idesc->id_loc % LFS_DIRBLKSIZ);
256 if (dp->d_ino >= maxino ||
257 dp->d_reclen == 0 ||
258 dp->d_reclen > spaceleft ||
259 (dp->d_reclen & 0x3) != 0) {
260 pwarn("ino too large, reclen=0, reclen>space, or reclen&3!=0\n");
261 pwarn("dp->d_ino = 0x%x\tdp->d_reclen = 0x%x\n",
262 dp->d_ino, dp->d_reclen);
263 pwarn("maxino = %llu\tspaceleft = 0x%x\n",
264 (unsigned long long)maxino, spaceleft);
265 return (0);
266 }
267 if (dp->d_ino == 0)
268 return (1);
269 size = LFS_DIRSIZ(0, dp, 0);
270 namlen = dp->d_namlen;
271 type = dp->d_type;
272 if (dp->d_reclen < size ||
273 idesc->id_filesize < size ||
274 /* namlen > MAXNAMLEN || */
275 type > 15) {
276 printf("reclen<size, filesize<size, namlen too large, or type>15\n");
277 return (0);
278 }
279 for (cp = dp->d_name, size = 0; size < namlen; size++)
280 if (*cp == '\0' || (*cp++ == '/')) {
281 printf("name contains NUL or /\n");
282 return (0);
283 }
284 if (*cp != '\0') {
285 printf("name size misstated\n");
286 return (0);
287 }
288 return (1);
289 }
290
291 void
292 direrror(ino_t ino, const char *errmesg)
293 {
294
295 fileerror(ino, ino, errmesg);
296 }
297
298 void
299 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
300 {
301 char pathbuf[MAXPATHLEN + 1];
302 struct uvnode *vp;
303
304 pwarn("%s ", errmesg);
305 pinode(ino);
306 printf("\n");
307 pwarn("PARENT=%lld\n", (long long)cwd);
308 getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
309 if (ino < ULFS_ROOTINO || ino >= maxino) {
310 pfatal("NAME=%s\n", pathbuf);
311 return;
312 }
313 vp = vget(fs, ino);
314 if (vp == NULL)
315 pfatal("INO is NULL\n");
316 else {
317 if (ftypeok(VTOD(vp)))
318 pfatal("%s=%s\n",
319 (lfs_dino_getmode(fs, VTOI(vp)->i_din) & LFS_IFMT) == LFS_IFDIR ?
320 "DIR" : "FILE", pathbuf);
321 else
322 pfatal("NAME=%s\n", pathbuf);
323 }
324 }
325
326 void
327 adjust(struct inodesc *idesc, short lcnt)
328 {
329 struct uvnode *vp;
330 union lfs_dinode *dp;
331
332 /*
333 * XXX: (1) since lcnt is apparently a delta, rename it; (2)
334 * why is it a value to *subtract*? that is unnecessarily
335 * confusing.
336 */
337
338 vp = vget(fs, idesc->id_number);
339 dp = VTOD(vp);
340 if (lfs_dino_getnlink(fs, dp) == lcnt) {
341 if (linkup(idesc->id_number, (ino_t) 0) == 0)
342 clri(idesc, "UNREF", 0);
343 } else {
344 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
345 ((lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? "DIR" : "FILE"));
346 pinode(idesc->id_number);
347 printf(" COUNT %d SHOULD BE %d",
348 lfs_dino_getnlink(fs, dp), lfs_dino_getnlink(fs, dp) - lcnt);
349 if (preen) {
350 if (lcnt < 0) {
351 printf("\n");
352 pfatal("LINK COUNT INCREASING");
353 }
354 printf(" (ADJUSTED)\n");
355 }
356 if (preen || reply("ADJUST") == 1) {
357 lfs_dino_setnlink(fs, dp,
358 lfs_dino_getnlink(fs, dp) - lcnt);
359 inodirty(VTOI(vp));
360 }
361 }
362 }
363
364 static int
365 mkentry(struct inodesc *idesc)
366 {
367 struct lfs_direct *dirp = idesc->id_dirp;
368 struct lfs_direct newent;
369 int newlen, oldlen;
370
371 newent.d_namlen = strlen(idesc->id_name);
372 newlen = LFS_DIRSIZ(0, &newent, 0);
373 if (dirp->d_ino != 0)
374 oldlen = LFS_DIRSIZ(0, dirp, 0);
375 else
376 oldlen = 0;
377 if (dirp->d_reclen - oldlen < newlen)
378 return (KEEPON);
379 newent.d_reclen = dirp->d_reclen - oldlen;
380 dirp->d_reclen = oldlen;
381 dirp = (struct lfs_direct *) (((char *) dirp) + oldlen);
382 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
383 dirp->d_reclen = newent.d_reclen;
384 dirp->d_type = typemap[idesc->id_parent];
385 dirp->d_namlen = newent.d_namlen;
386 memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
387 return (ALTERED | STOP);
388 }
389
390 static int
391 chgino(struct inodesc *idesc)
392 {
393 struct lfs_direct *dirp = idesc->id_dirp;
394
395 if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
396 return (KEEPON);
397 dirp->d_ino = idesc->id_parent;
398 dirp->d_type = typemap[idesc->id_parent];
399 return (ALTERED | STOP);
400 }
401
402 int
403 linkup(ino_t orphan, ino_t parentdir)
404 {
405 union lfs_dinode *dp;
406 int lostdir;
407 ino_t oldlfdir;
408 struct inodesc idesc;
409 char tempname[BUFSIZ];
410 struct uvnode *vp;
411
412 memset(&idesc, 0, sizeof(struct inodesc));
413 vp = vget(fs, orphan);
414 dp = VTOD(vp);
415 lostdir = (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR;
416 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
417 pinode(orphan);
418 if (preen && lfs_dino_getsize(fs, dp) == 0)
419 return (0);
420 if (preen)
421 printf(" (RECONNECTED)\n");
422 else if (reply("RECONNECT") == 0)
423 return (0);
424 if (lfdir == 0) {
425 dp = ginode(ULFS_ROOTINO);
426 idesc.id_name = lfname;
427 idesc.id_type = DATA;
428 idesc.id_func = findino;
429 idesc.id_number = ULFS_ROOTINO;
430 if ((ckinode(dp, &idesc) & FOUND) != 0) {
431 lfdir = idesc.id_parent;
432 } else {
433 pwarn("NO lost+found DIRECTORY");
434 if (preen || reply("CREATE")) {
435 lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode);
436 if (lfdir != 0) {
437 if (makeentry(ULFS_ROOTINO, lfdir, lfname) != 0) {
438 if (preen)
439 printf(" (CREATED)\n");
440 } else {
441 freedir(lfdir, ULFS_ROOTINO);
442 lfdir = 0;
443 if (preen)
444 printf("\n");
445 }
446 }
447 }
448 }
449 if (lfdir == 0) {
450 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
451 printf("\n\n");
452 return (0);
453 }
454 }
455 vp = vget(fs, lfdir);
456 dp = VTOD(vp);
457 if ((lfs_dino_getmode(fs, dp) & LFS_IFMT) != LFS_IFDIR) {
458 pfatal("lost+found IS NOT A DIRECTORY");
459 if (reply("REALLOCATE") == 0)
460 return (0);
461 oldlfdir = lfdir;
462 if ((lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode)) == 0) {
463 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
464 return (0);
465 }
466 if ((changeino(ULFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
467 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
468 return (0);
469 }
470 inodirty(VTOI(vp));
471 idesc.id_type = ADDR;
472 idesc.id_func = pass4check;
473 idesc.id_number = oldlfdir;
474 adjust(&idesc, lncntp[oldlfdir] + 1);
475 lncntp[oldlfdir] = 0;
476 vp = vget(fs, lfdir);
477 dp = VTOD(vp);
478 }
479 if (statemap[lfdir] != DFOUND) {
480 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
481 return (0);
482 }
483 (void) lftempname(tempname, orphan);
484 if (makeentry(lfdir, orphan, tempname) == 0) {
485 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
486 printf("\n\n");
487 return (0);
488 }
489 lncntp[orphan]--;
490 if (lostdir) {
491 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
492 parentdir != (ino_t) - 1)
493 (void) makeentry(orphan, lfdir, "..");
494 vp = vget(fs, lfdir);
495 lfs_dino_setnlink(fs, VTOI(vp)->i_din,
496 lfs_dino_getnlink(fs, VTOI(vp)->i_din) + 1);
497 inodirty(VTOI(vp));
498 lncntp[lfdir]++;
499 pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
500 if (parentdir != (ino_t) - 1)
501 printf("PARENT WAS I=%llu\n",
502 (unsigned long long)parentdir);
503 if (preen == 0)
504 printf("\n");
505 }
506 return (1);
507 }
508
509 /*
510 * fix an entry in a directory.
511 */
512 int
513 changeino(ino_t dir, const char *name, ino_t newnum)
514 {
515 struct inodesc idesc;
516
517 memset(&idesc, 0, sizeof(struct inodesc));
518 idesc.id_type = DATA;
519 idesc.id_func = chgino;
520 idesc.id_number = dir;
521 idesc.id_fix = DONTKNOW;
522 idesc.id_name = name;
523 idesc.id_parent = newnum; /* new value for name */
524
525 return (ckinode(ginode(dir), &idesc));
526 }
527
528 /*
529 * make an entry in a directory
530 */
531 int
532 makeentry(ino_t parent, ino_t ino, const char *name)
533 {
534 union lfs_dinode *dp;
535 struct inodesc idesc;
536 char pathbuf[MAXPATHLEN + 1];
537 struct uvnode *vp;
538 uint64_t size;
539
540 if (parent < ULFS_ROOTINO || parent >= maxino ||
541 ino < ULFS_ROOTINO || ino >= maxino)
542 return (0);
543 memset(&idesc, 0, sizeof(struct inodesc));
544 idesc.id_type = DATA;
545 idesc.id_func = mkentry;
546 idesc.id_number = parent;
547 idesc.id_parent = ino; /* this is the inode to enter */
548 idesc.id_fix = DONTKNOW;
549 idesc.id_name = name;
550 vp = vget(fs, parent);
551 dp = VTOD(vp);
552 size = lfs_dino_getsize(fs, dp);
553 if (size % LFS_DIRBLKSIZ) {
554 size = roundup(size, LFS_DIRBLKSIZ);
555 lfs_dino_setsize(fs, dp, size);
556 inodirty(VTOI(vp));
557 }
558 if ((ckinode(dp, &idesc) & ALTERED) != 0)
559 return (1);
560 getpathname(pathbuf, sizeof(pathbuf), parent, parent);
561 vp = vget(fs, parent);
562 dp = VTOD(vp);
563 if (expanddir(vp, dp, pathbuf) == 0)
564 return (0);
565 return (ckinode(dp, &idesc) & ALTERED);
566 }
567
568 /*
569 * Attempt to expand the size of a directory
570 */
571 static int
572 expanddir(struct uvnode *vp, union lfs_dinode *dp, char *name)
573 {
574 daddr_t lastbn;
575 struct ubuf *bp;
576 char *cp, firstblk[LFS_DIRBLKSIZ];
577
578 lastbn = lfs_lblkno(fs, lfs_dino_getsize(fs, dp));
579 if (lastbn >= ULFS_NDADDR - 1 || lfs_dino_getdb(fs, dp, lastbn) == 0 ||
580 lfs_dino_getsize(fs, dp) == 0)
581 return (0);
582 lfs_dino_setdb(fs, dp, lastbn + 1, lfs_dino_getdb(fs, dp, lastbn));
583 lfs_dino_setdb(fs, dp, lastbn, 0);
584 bp = getblk(vp, lastbn, lfs_sb_getbsize(fs));
585 VOP_BWRITE(bp);
586 lfs_dino_setsize(fs, dp,
587 lfs_dino_getsize(fs, dp) + lfs_sb_getbsize(fs));
588 lfs_dino_setblocks(fs, dp,
589 lfs_dino_getblocks(fs, dp) + lfs_btofsb(fs, lfs_sb_getbsize(fs)));
590 bread(vp, lfs_dino_getdb(fs, dp, lastbn + 1),
591 (long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
592 if (bp->b_flags & B_ERROR)
593 goto bad;
594 memcpy(firstblk, bp->b_data, LFS_DIRBLKSIZ);
595 bread(vp, lastbn, lfs_sb_getbsize(fs), 0, &bp);
596 if (bp->b_flags & B_ERROR)
597 goto bad;
598 memcpy(bp->b_data, firstblk, LFS_DIRBLKSIZ);
599 for (cp = &bp->b_data[LFS_DIRBLKSIZ];
600 cp < &bp->b_data[lfs_sb_getbsize(fs)];
601 cp += LFS_DIRBLKSIZ)
602 memcpy(cp, &emptydir, sizeof emptydir);
603 VOP_BWRITE(bp);
604 bread(vp, lfs_dino_getdb(fs, dp, lastbn + 1),
605 (long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
606 if (bp->b_flags & B_ERROR)
607 goto bad;
608 memcpy(bp->b_data, &emptydir, sizeof emptydir);
609 pwarn("NO SPACE LEFT IN %s", name);
610 if (preen)
611 printf(" (EXPANDED)\n");
612 else if (reply("EXPAND") == 0)
613 goto bad;
614 VOP_BWRITE(bp);
615 inodirty(VTOI(vp));
616 return (1);
617 bad:
618 lfs_dino_setdb(fs, dp, lastbn, lfs_dino_getdb(fs, dp, lastbn + 1));
619 lfs_dino_setdb(fs, dp, lastbn + 1, 0);
620 lfs_dino_setsize(fs, dp,
621 lfs_dino_getsize(fs, dp) - lfs_sb_getbsize(fs));
622 lfs_dino_setblocks(fs, dp,
623 lfs_dino_getblocks(fs, dp) - lfs_btofsb(fs, lfs_sb_getbsize(fs)));
624 return (0);
625 }
626
627 /*
628 * allocate a new directory
629 */
630 int
631 allocdir(ino_t parent, ino_t request, int mode)
632 {
633 ino_t ino;
634 char *cp;
635 union lfs_dinode *dp;
636 struct ubuf *bp;
637 struct lfs_dirtemplate *dirp;
638 struct uvnode *vp;
639
640 ino = allocino(request, LFS_IFDIR | mode);
641 dirp = &dirhead;
642 dirp->dot_ino = ino;
643 dirp->dotdot_ino = parent;
644 vp = vget(fs, ino);
645 dp = VTOD(vp);
646 bread(vp, lfs_dino_getdb(fs, dp, 0), lfs_sb_getfsize(fs), 0, &bp);
647 if (bp->b_flags & B_ERROR) {
648 brelse(bp, 0);
649 freeino(ino);
650 return (0);
651 }
652 memcpy(bp->b_data, dirp, sizeof(struct lfs_dirtemplate));
653 for (cp = &bp->b_data[LFS_DIRBLKSIZ];
654 cp < &bp->b_data[lfs_sb_getfsize(fs)];
655 cp += LFS_DIRBLKSIZ)
656 memcpy(cp, &emptydir, sizeof emptydir);
657 VOP_BWRITE(bp);
658 lfs_dino_setnlink(fs, dp, 2);
659 inodirty(VTOI(vp));
660 if (ino == ULFS_ROOTINO) {
661 lncntp[ino] = lfs_dino_getnlink(fs, dp);
662 cacheino(dp, ino);
663 return (ino);
664 }
665 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
666 freeino(ino);
667 return (0);
668 }
669 cacheino(dp, ino);
670 statemap[ino] = statemap[parent];
671 if (statemap[ino] == DSTATE) {
672 lncntp[ino] = lfs_dino_getnlink(fs, dp);
673 lncntp[parent]++;
674 }
675 vp = vget(fs, parent);
676 dp = VTOD(vp);
677 lfs_dino_setnlink(fs, dp, lfs_dino_getnlink(fs, dp) + 1);
678 inodirty(VTOI(vp));
679 return (ino);
680 }
681
682 /*
683 * free a directory inode
684 */
685 static void
686 freedir(ino_t ino, ino_t parent)
687 {
688 struct uvnode *vp;
689
690 if (ino != parent) {
691 vp = vget(fs, parent);
692 lfs_dino_setnlink(fs, VTOI(vp)->i_din,
693 lfs_dino_getnlink(fs, VTOI(vp)->i_din) - 1);
694 inodirty(VTOI(vp));
695 }
696 freeino(ino);
697 }
698
699 /*
700 * generate a temporary name for the lost+found directory.
701 */
702 static int
703 lftempname(char *bufp, ino_t ino)
704 {
705 ino_t in;
706 char *cp;
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