dir.c revision 1.35 1 /* $NetBSD: dir.c,v 1.35 2015/07/28 05:09:34 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 *, struct ulfs1_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 (VTOI(vp)->i_ffs1_mode & 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 struct ulfs1_dinode *dp;
331
332 vp = vget(fs, idesc->id_number);
333 dp = VTOD(vp);
334 if (dp->di_nlink == lcnt) {
335 if (linkup(idesc->id_number, (ino_t) 0) == 0)
336 clri(idesc, "UNREF", 0);
337 } else {
338 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
339 ((dp->di_mode & LFS_IFMT) == LFS_IFDIR ? "DIR" : "FILE"));
340 pinode(idesc->id_number);
341 printf(" COUNT %d SHOULD BE %d",
342 dp->di_nlink, dp->di_nlink - lcnt);
343 if (preen) {
344 if (lcnt < 0) {
345 printf("\n");
346 pfatal("LINK COUNT INCREASING");
347 }
348 printf(" (ADJUSTED)\n");
349 }
350 if (preen || reply("ADJUST") == 1) {
351 dp->di_nlink -= lcnt;
352 inodirty(VTOI(vp));
353 }
354 }
355 }
356
357 static int
358 mkentry(struct inodesc *idesc)
359 {
360 struct lfs_direct *dirp = idesc->id_dirp;
361 struct lfs_direct newent;
362 int newlen, oldlen;
363
364 newent.d_namlen = strlen(idesc->id_name);
365 newlen = LFS_DIRSIZ(0, &newent, 0);
366 if (dirp->d_ino != 0)
367 oldlen = LFS_DIRSIZ(0, dirp, 0);
368 else
369 oldlen = 0;
370 if (dirp->d_reclen - oldlen < newlen)
371 return (KEEPON);
372 newent.d_reclen = dirp->d_reclen - oldlen;
373 dirp->d_reclen = oldlen;
374 dirp = (struct lfs_direct *) (((char *) dirp) + oldlen);
375 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
376 dirp->d_reclen = newent.d_reclen;
377 dirp->d_type = typemap[idesc->id_parent];
378 dirp->d_namlen = newent.d_namlen;
379 memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
380 return (ALTERED | STOP);
381 }
382
383 static int
384 chgino(struct inodesc *idesc)
385 {
386 struct lfs_direct *dirp = idesc->id_dirp;
387
388 if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
389 return (KEEPON);
390 dirp->d_ino = idesc->id_parent;
391 dirp->d_type = typemap[idesc->id_parent];
392 return (ALTERED | STOP);
393 }
394
395 int
396 linkup(ino_t orphan, ino_t parentdir)
397 {
398 struct ulfs1_dinode *dp;
399 int lostdir;
400 ino_t oldlfdir;
401 struct inodesc idesc;
402 char tempname[BUFSIZ];
403 struct uvnode *vp;
404
405 memset(&idesc, 0, sizeof(struct inodesc));
406 vp = vget(fs, orphan);
407 dp = VTOD(vp);
408 lostdir = (dp->di_mode & LFS_IFMT) == LFS_IFDIR;
409 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
410 pinode(orphan);
411 if (preen && dp->di_size == 0)
412 return (0);
413 if (preen)
414 printf(" (RECONNECTED)\n");
415 else if (reply("RECONNECT") == 0)
416 return (0);
417 if (lfdir == 0) {
418 dp = ginode(ULFS_ROOTINO);
419 idesc.id_name = lfname;
420 idesc.id_type = DATA;
421 idesc.id_func = findino;
422 idesc.id_number = ULFS_ROOTINO;
423 if ((ckinode(dp, &idesc) & FOUND) != 0) {
424 lfdir = idesc.id_parent;
425 } else {
426 pwarn("NO lost+found DIRECTORY");
427 if (preen || reply("CREATE")) {
428 lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode);
429 if (lfdir != 0) {
430 if (makeentry(ULFS_ROOTINO, lfdir, lfname) != 0) {
431 if (preen)
432 printf(" (CREATED)\n");
433 } else {
434 freedir(lfdir, ULFS_ROOTINO);
435 lfdir = 0;
436 if (preen)
437 printf("\n");
438 }
439 }
440 }
441 }
442 if (lfdir == 0) {
443 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
444 printf("\n\n");
445 return (0);
446 }
447 }
448 vp = vget(fs, lfdir);
449 dp = VTOD(vp);
450 if ((dp->di_mode & LFS_IFMT) != LFS_IFDIR) {
451 pfatal("lost+found IS NOT A DIRECTORY");
452 if (reply("REALLOCATE") == 0)
453 return (0);
454 oldlfdir = lfdir;
455 if ((lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode)) == 0) {
456 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
457 return (0);
458 }
459 if ((changeino(ULFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
460 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
461 return (0);
462 }
463 inodirty(VTOI(vp));
464 idesc.id_type = ADDR;
465 idesc.id_func = pass4check;
466 idesc.id_number = oldlfdir;
467 adjust(&idesc, lncntp[oldlfdir] + 1);
468 lncntp[oldlfdir] = 0;
469 vp = vget(fs, lfdir);
470 dp = VTOD(vp);
471 }
472 if (statemap[lfdir] != DFOUND) {
473 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
474 return (0);
475 }
476 (void) lftempname(tempname, orphan);
477 if (makeentry(lfdir, orphan, tempname) == 0) {
478 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
479 printf("\n\n");
480 return (0);
481 }
482 lncntp[orphan]--;
483 if (lostdir) {
484 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
485 parentdir != (ino_t) - 1)
486 (void) makeentry(orphan, lfdir, "..");
487 vp = vget(fs, lfdir);
488 VTOI(vp)->i_ffs1_nlink++;
489 inodirty(VTOI(vp));
490 lncntp[lfdir]++;
491 pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
492 if (parentdir != (ino_t) - 1)
493 printf("PARENT WAS I=%llu\n",
494 (unsigned long long)parentdir);
495 if (preen == 0)
496 printf("\n");
497 }
498 return (1);
499 }
500
501 /*
502 * fix an entry in a directory.
503 */
504 int
505 changeino(ino_t dir, const char *name, ino_t newnum)
506 {
507 struct inodesc idesc;
508
509 memset(&idesc, 0, sizeof(struct inodesc));
510 idesc.id_type = DATA;
511 idesc.id_func = chgino;
512 idesc.id_number = dir;
513 idesc.id_fix = DONTKNOW;
514 idesc.id_name = name;
515 idesc.id_parent = newnum; /* new value for name */
516
517 return (ckinode(ginode(dir), &idesc));
518 }
519
520 /*
521 * make an entry in a directory
522 */
523 int
524 makeentry(ino_t parent, ino_t ino, const char *name)
525 {
526 struct ulfs1_dinode *dp;
527 struct inodesc idesc;
528 char pathbuf[MAXPATHLEN + 1];
529 struct uvnode *vp;
530
531 if (parent < ULFS_ROOTINO || parent >= maxino ||
532 ino < ULFS_ROOTINO || ino >= maxino)
533 return (0);
534 memset(&idesc, 0, sizeof(struct inodesc));
535 idesc.id_type = DATA;
536 idesc.id_func = mkentry;
537 idesc.id_number = parent;
538 idesc.id_parent = ino; /* this is the inode to enter */
539 idesc.id_fix = DONTKNOW;
540 idesc.id_name = name;
541 vp = vget(fs, parent);
542 dp = VTOD(vp);
543 if (dp->di_size % LFS_DIRBLKSIZ) {
544 dp->di_size = roundup(dp->di_size, LFS_DIRBLKSIZ);
545 inodirty(VTOI(vp));
546 }
547 if ((ckinode(dp, &idesc) & ALTERED) != 0)
548 return (1);
549 getpathname(pathbuf, sizeof(pathbuf), parent, parent);
550 vp = vget(fs, parent);
551 dp = VTOD(vp);
552 if (expanddir(vp, dp, pathbuf) == 0)
553 return (0);
554 return (ckinode(dp, &idesc) & ALTERED);
555 }
556
557 /*
558 * Attempt to expand the size of a directory
559 */
560 static int
561 expanddir(struct uvnode *vp, struct ulfs1_dinode *dp, char *name)
562 {
563 daddr_t lastbn;
564 struct ubuf *bp;
565 char *cp, firstblk[LFS_DIRBLKSIZ];
566
567 lastbn = lfs_lblkno(fs, dp->di_size);
568 if (lastbn >= ULFS_NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
569 return (0);
570 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
571 dp->di_db[lastbn] = 0;
572 bp = getblk(vp, lastbn, lfs_sb_getbsize(fs));
573 VOP_BWRITE(bp);
574 dp->di_size += lfs_sb_getbsize(fs);
575 dp->di_blocks += lfs_btofsb(fs, lfs_sb_getbsize(fs));
576 bread(vp, dp->di_db[lastbn + 1],
577 (long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
578 if (bp->b_flags & B_ERROR)
579 goto bad;
580 memcpy(firstblk, bp->b_data, LFS_DIRBLKSIZ);
581 bread(vp, lastbn, lfs_sb_getbsize(fs), 0, &bp);
582 if (bp->b_flags & B_ERROR)
583 goto bad;
584 memcpy(bp->b_data, firstblk, LFS_DIRBLKSIZ);
585 for (cp = &bp->b_data[LFS_DIRBLKSIZ];
586 cp < &bp->b_data[lfs_sb_getbsize(fs)];
587 cp += LFS_DIRBLKSIZ)
588 memcpy(cp, &emptydir, sizeof emptydir);
589 VOP_BWRITE(bp);
590 bread(vp, dp->di_db[lastbn + 1],
591 (long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
592 if (bp->b_flags & B_ERROR)
593 goto bad;
594 memcpy(bp->b_data, &emptydir, sizeof emptydir);
595 pwarn("NO SPACE LEFT IN %s", name);
596 if (preen)
597 printf(" (EXPANDED)\n");
598 else if (reply("EXPAND") == 0)
599 goto bad;
600 VOP_BWRITE(bp);
601 inodirty(VTOI(vp));
602 return (1);
603 bad:
604 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
605 dp->di_db[lastbn + 1] = 0;
606 dp->di_size -= lfs_sb_getbsize(fs);
607 dp->di_blocks -= lfs_btofsb(fs, lfs_sb_getbsize(fs));
608 return (0);
609 }
610
611 /*
612 * allocate a new directory
613 */
614 int
615 allocdir(ino_t parent, ino_t request, int mode)
616 {
617 ino_t ino;
618 char *cp;
619 struct ulfs1_dinode *dp;
620 struct ubuf *bp;
621 struct lfs_dirtemplate *dirp;
622 struct uvnode *vp;
623
624 ino = allocino(request, LFS_IFDIR | mode);
625 dirp = &dirhead;
626 dirp->dot_ino = ino;
627 dirp->dotdot_ino = parent;
628 vp = vget(fs, ino);
629 dp = VTOD(vp);
630 bread(vp, dp->di_db[0], lfs_sb_getfsize(fs), 0, &bp);
631 if (bp->b_flags & B_ERROR) {
632 brelse(bp, 0);
633 freeino(ino);
634 return (0);
635 }
636 memcpy(bp->b_data, dirp, sizeof(struct lfs_dirtemplate));
637 for (cp = &bp->b_data[LFS_DIRBLKSIZ];
638 cp < &bp->b_data[lfs_sb_getfsize(fs)];
639 cp += LFS_DIRBLKSIZ)
640 memcpy(cp, &emptydir, sizeof emptydir);
641 VOP_BWRITE(bp);
642 dp->di_nlink = 2;
643 inodirty(VTOI(vp));
644 if (ino == ULFS_ROOTINO) {
645 lncntp[ino] = dp->di_nlink;
646 cacheino(dp, ino);
647 return (ino);
648 }
649 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
650 freeino(ino);
651 return (0);
652 }
653 cacheino(dp, ino);
654 statemap[ino] = statemap[parent];
655 if (statemap[ino] == DSTATE) {
656 lncntp[ino] = dp->di_nlink;
657 lncntp[parent]++;
658 }
659 vp = vget(fs, parent);
660 dp = VTOD(vp);
661 dp->di_nlink++;
662 inodirty(VTOI(vp));
663 return (ino);
664 }
665
666 /*
667 * free a directory inode
668 */
669 static void
670 freedir(ino_t ino, ino_t parent)
671 {
672 struct uvnode *vp;
673
674 if (ino != parent) {
675 vp = vget(fs, parent);
676 VTOI(vp)->i_ffs1_nlink--;
677 inodirty(VTOI(vp));
678 }
679 freeino(ino);
680 }
681
682 /*
683 * generate a temporary name for the lost+found directory.
684 */
685 static int
686 lftempname(char *bufp, ino_t ino)
687 {
688 ino_t in;
689 char *cp;
690 int namlen;
691
692 cp = bufp + 2;
693 for (in = maxino; in > 0; in /= 10)
694 cp++;
695 *--cp = 0;
696 namlen = cp - bufp;
697 in = ino;
698 while (cp > bufp) {
699 *--cp = (in % 10) + '0';
700 in /= 10;
701 }
702 *cp = '#';
703 return (namlen);
704 }
705