dir.c (21294B)
1 /* $NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 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 /* 33 * Copyright (c) 1997 Manuel Bouyer. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 #ifndef lint 57 #if 0 58 static char sccsid[] = "@(#)dir.c 8.5 (Berkeley) 12/8/94"; 59 #else 60 __RCSID("$NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 dholland Exp $"); 61 #endif 62 #endif /* not lint */ 63 64 #include <sys/param.h> 65 #include <sys/time.h> 66 #include <ufs/ext2fs/ext2fs_dinode.h> 67 #include <ufs/ext2fs/ext2fs_dir.h> 68 #include <ufs/ext2fs/ext2fs.h> 69 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <err.h> 74 75 #include "fsck.h" 76 #include "fsutil.h" 77 #include "extern.h" 78 79 const char *lfname = "lost+found"; 80 int lfmode = 01700; 81 struct ext2fs_dirtemplate emptydir = { 82 .dot_ino = 0, 83 .dot_reclen = UFS_DIRBLKSIZ, 84 }; 85 struct ext2fs_dirtemplate dirhead = { 86 .dot_ino = 0, 87 .dot_reclen = 12, 88 .dot_namlen = 1, 89 .dot_type = EXT2_FT_DIR, 90 .dot_name = ".", 91 .dotdot_ino = 0, 92 .dotdot_reclen = UFS_DIRBLKSIZ - 12, 93 .dotdot_namlen = 2, 94 .dotdot_type = EXT2_FT_DIR, 95 .dotdot_name = "..", 96 }; 97 #undef UFS_DIRBLKSIZ 98 99 static int expanddir(struct ext2fs_dinode *, char *); 100 static void freedir(ino_t, ino_t); 101 static struct ext2fs_direct *fsck_readdir(struct inodesc *); 102 static struct bufarea *getdirblk(daddr_t, long); 103 static int lftempname(char *, ino_t); 104 static int mkentry(struct inodesc *); 105 static int chgino(struct inodesc *); 106 107 /* 108 * Propagate connected state through the tree. 109 */ 110 void 111 propagate(void) 112 { 113 struct inoinfo **inpp, *inp, *pinp; 114 struct inoinfo **inpend; 115 116 /* 117 * Create a list of children for each directory. 118 */ 119 inpend = &inpsort[inplast]; 120 for (inpp = inpsort; inpp < inpend; inpp++) { 121 inp = *inpp; 122 if (inp->i_parent == 0 || inp->i_number == EXT2_ROOTINO) 123 continue; 124 pinp = getinoinfo(inp->i_parent); 125 inp->i_parentp = pinp; 126 inp->i_sibling = pinp->i_child; 127 pinp->i_child = inp; 128 } 129 inp = getinoinfo(EXT2_ROOTINO); 130 while (inp) { 131 statemap[inp->i_number] = DFOUND; 132 if (inp->i_child && statemap[inp->i_child->i_number] == DSTATE) 133 inp = inp->i_child; 134 else if (inp->i_sibling) 135 inp = inp->i_sibling; 136 else 137 inp = inp->i_parentp; 138 } 139 } 140 141 /* 142 * Scan each entry in a directory block. 143 */ 144 int 145 dirscan(struct inodesc *idesc) 146 { 147 struct ext2fs_direct *dp; 148 struct bufarea *bp; 149 int dsize, n; 150 long blksiz; 151 char *dbuf = NULL; 152 153 if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL) 154 err(8, "Can't allocate directory block"); 155 156 if (idesc->id_type != DATA) 157 errexit("wrong type to dirscan %d", idesc->id_type); 158 if (idesc->id_entryno == 0 && (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0) 159 idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize); 160 blksiz = idesc->id_numfrags * sblock.e2fs_bsize; 161 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) { 162 idesc->id_filesize -= blksiz; 163 free(dbuf); 164 return (SKIP); 165 } 166 idesc->id_loc = 0; 167 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 168 dsize = fs2h16(dp->e2d_reclen); 169 memcpy(dbuf, dp, (size_t)dsize); 170 idesc->id_dirp = (struct ext2fs_direct *)dbuf; 171 if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 172 bp = getdirblk(idesc->id_blkno, blksiz); 173 memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf, (size_t)dsize); 174 dirty(bp); 175 sbdirty(); 176 } 177 if (n & STOP) { 178 free(dbuf); 179 return (n); 180 } 181 } 182 free(dbuf); 183 return (idesc->id_filesize > 0 ? KEEPON : STOP); 184 } 185 186 /* 187 * get next entry in a directory. 188 */ 189 static struct ext2fs_direct * 190 fsck_readdir(struct inodesc *idesc) 191 { 192 struct ext2fs_direct *dp, *ndp; 193 struct bufarea *bp; 194 long size, blksiz, fix, dploc; 195 196 blksiz = idesc->id_numfrags * sblock.e2fs_bsize; 197 bp = getdirblk(idesc->id_blkno, blksiz); 198 if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 && idesc->id_loc < blksiz) { 199 dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); 200 if (dircheck(idesc, dp)) 201 goto dpok; 202 if (idesc->id_fix == IGNORE) 203 return (0); 204 fix = dofix(idesc, "DIRECTORY CORRUPTED"); 205 bp = getdirblk(idesc->id_blkno, blksiz); 206 dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); 207 dp->e2d_reclen = h2fs16(sblock.e2fs_bsize); 208 dp->e2d_ino = 0; 209 dp->e2d_namlen = 0; 210 dp->e2d_type = 0; 211 dp->e2d_name[0] = '\0'; 212 if (fix) 213 dirty(bp); 214 idesc->id_loc += sblock.e2fs_bsize; 215 idesc->id_filesize -= sblock.e2fs_bsize; 216 return (dp); 217 } 218 dpok: 219 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 220 return NULL; 221 dploc = idesc->id_loc; 222 dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc); 223 idesc->id_loc += fs2h16(dp->e2d_reclen); 224 idesc->id_filesize -= fs2h16(dp->e2d_reclen); 225 if ((idesc->id_loc % sblock.e2fs_bsize) == 0) 226 return (dp); 227 ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); 228 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && dircheck(idesc, ndp) == 0) { 229 size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize); 230 idesc->id_loc += size; 231 idesc->id_filesize -= size; 232 if (idesc->id_fix == IGNORE) 233 return (0); 234 fix = dofix(idesc, "DIRECTORY CORRUPTED"); 235 bp = getdirblk(idesc->id_blkno, blksiz); 236 dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc); 237 dp->e2d_reclen = h2fs16(fs2h16(dp->e2d_reclen) + size); 238 if (fix) 239 dirty(bp); 240 } 241 return (dp); 242 } 243 244 /* 245 * Verify that a directory entry is valid. 246 * This is a superset of the checks made in the kernel. 247 */ 248 int 249 dircheck(struct inodesc *idesc, struct ext2fs_direct *dp) 250 { 251 int size; 252 char *cp; 253 int spaceleft; 254 u_int16_t reclen = fs2h16(dp->e2d_reclen); 255 256 spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize); 257 if (fs2h32(dp->e2d_ino) > maxino || reclen == 0 || reclen > spaceleft || (reclen & 0x3) != 0) 258 return (0); 259 if (dp->e2d_ino == 0) 260 return (1); 261 if (sblock.e2fs.e2fs_rev < E2FS_REV1 262 || (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0) 263 if (dp->e2d_type != 0) 264 return (1); 265 size = EXT2FS_DIRSIZ(dp->e2d_namlen); 266 if ( 267 reclen < size || idesc->id_filesize < size /* || 268 dp->e2d_namlen > EXT2FS_MAXNAMLEN */ 269 ) 270 return (0); 271 for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++) 272 if (*cp == '\0' || (*cp++ == '/')) 273 return (0); 274 return (1); 275 } 276 277 void 278 direrror(ino_t ino, const char *errmesg) 279 { 280 fileerror(ino, ino, errmesg); 281 } 282 283 void 284 fileerror(ino_t cwd, ino_t ino, const char *errmesg) 285 { 286 struct ext2fs_dinode *dp; 287 char pathbuf[MAXPATHLEN + 1]; 288 289 pwarn("%s ", errmesg); 290 pinode(ino); 291 printf("\n"); 292 getpathname(pathbuf, sizeof(pathbuf), cwd, ino); 293 if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) { 294 pfatal("NAME=%s\n", pathbuf); 295 return; 296 } 297 dp = ginode(ino); 298 if (ftypeok(dp)) 299 pfatal("%s=%s\n", (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf); 300 else 301 pfatal("NAME=%s\n", pathbuf); 302 } 303 304 void 305 adjust(struct inodesc *idesc, short lcnt) 306 { 307 struct ext2fs_dinode *dp; 308 309 dp = ginode(idesc->id_number); 310 if (fs2h16(dp->e2di_nlink) == lcnt) { 311 if (linkup(idesc->id_number, (ino_t)0) == 0) 312 clri(idesc, "UNREF", 0); 313 } else { 314 pwarn( 315 "LINK COUNT %s", 316 (lfdir == idesc->id_number) ? lfname 317 : ((fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE") 318 ); 319 pinode(idesc->id_number); 320 printf(" COUNT %d SHOULD BE %d", fs2h16(dp->e2di_nlink), fs2h16(dp->e2di_nlink) - lcnt); 321 if (preen) { 322 if (lcnt < 0) { 323 printf("\n"); 324 pfatal("LINK COUNT INCREASING"); 325 } 326 printf(" (ADJUSTED)\n"); 327 } 328 if (preen || reply("ADJUST") == 1) { 329 dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - lcnt); 330 inodirty(); 331 } 332 } 333 } 334 335 static int 336 mkentry(struct inodesc *idesc) 337 { 338 struct ext2fs_direct *dirp = idesc->id_dirp; 339 struct ext2fs_direct newent; 340 int newlen, oldlen; 341 342 newent.e2d_type = 0; /* XXX gcc */ 343 newent.e2d_namlen = strlen(idesc->id_name); 344 if (sblock.e2fs.e2fs_rev > E2FS_REV0 345 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) 346 newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]); 347 newlen = EXT2FS_DIRSIZ(newent.e2d_namlen); 348 if (dirp->e2d_ino != 0) 349 oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen); 350 else 351 oldlen = 0; 352 if (fs2h16(dirp->e2d_reclen) - oldlen < newlen) 353 return (KEEPON); 354 newent.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - oldlen); 355 dirp->e2d_reclen = h2fs16(oldlen); 356 dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen); 357 dirp->e2d_ino = h2fs32(idesc->id_parent); /* ino to be entered is in id_parent */ 358 dirp->e2d_reclen = newent.e2d_reclen; 359 dirp->e2d_namlen = newent.e2d_namlen; 360 dirp->e2d_type = newent.e2d_type; 361 memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen)); 362 return (ALTERED | STOP); 363 } 364 365 static int 366 chgino(struct inodesc *idesc) 367 { 368 struct ext2fs_direct *dirp = idesc->id_dirp; 369 u_int16_t namlen = dirp->e2d_namlen; 370 371 if (strlen(idesc->id_name) != namlen || strncmp(dirp->e2d_name, idesc->id_name, (int)namlen)) 372 return (KEEPON); 373 dirp->e2d_ino = h2fs32(idesc->id_parent); 374 if (sblock.e2fs.e2fs_rev > E2FS_REV0 375 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) 376 dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]); 377 else 378 dirp->e2d_type = 0; 379 return (ALTERED | STOP); 380 } 381 382 int 383 linkup(ino_t orphan, ino_t parentdir) 384 { 385 struct ext2fs_dinode *dp; 386 int lostdir; 387 ino_t oldlfdir; 388 struct inodesc idesc; 389 char tempname[BUFSIZ]; 390 391 memset(&idesc, 0, sizeof(struct inodesc)); 392 dp = ginode(orphan); 393 lostdir = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR; 394 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 395 pinode(orphan); 396 if (preen && inosize(dp) == 0) 397 return (0); 398 if (preen) 399 printf(" (RECONNECTED)\n"); 400 else if (reply("RECONNECT") == 0) 401 return (0); 402 if (lfdir == 0) { 403 dp = ginode(EXT2_ROOTINO); 404 idesc.id_name = lfname; 405 idesc.id_type = DATA; 406 idesc.id_func = findino; 407 idesc.id_number = EXT2_ROOTINO; 408 if ((ckinode(dp, &idesc) & FOUND) != 0) { 409 lfdir = idesc.id_parent; 410 } else { 411 pwarn("NO lost+found DIRECTORY"); 412 if (preen || reply("CREATE")) { 413 lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode); 414 if (lfdir != 0) { 415 if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) { 416 if (preen) 417 printf(" (CREATED)\n"); 418 } else { 419 freedir(lfdir, EXT2_ROOTINO); 420 lfdir = 0; 421 if (preen) 422 printf("\n"); 423 } 424 } 425 } 426 } 427 if (lfdir == 0) { 428 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 429 printf("\n\n"); 430 return (0); 431 } 432 } 433 dp = ginode(lfdir); 434 if ((fs2h16(dp->e2di_mode) & IFMT) != IFDIR) { 435 pfatal("lost+found IS NOT A DIRECTORY"); 436 if (reply("REALLOCATE") == 0) 437 return (0); 438 oldlfdir = lfdir; 439 if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) { 440 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 441 return (0); 442 } 443 if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) { 444 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 445 return (0); 446 } 447 inodirty(); 448 idesc.id_type = ADDR; 449 idesc.id_func = pass4check; 450 idesc.id_number = oldlfdir; 451 adjust(&idesc, lncntp[oldlfdir] + 1); 452 lncntp[oldlfdir] = 0; 453 dp = ginode(lfdir); 454 } 455 if (statemap[lfdir] != DFOUND) { 456 pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 457 return (0); 458 } 459 (void)lftempname(tempname, orphan); 460 if (makeentry(lfdir, orphan, tempname) == 0) { 461 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 462 printf("\n\n"); 463 return (0); 464 } 465 lncntp[orphan]--; 466 if (lostdir) { 467 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && parentdir != (ino_t)-1) 468 (void)makeentry(orphan, lfdir, ".."); 469 dp = ginode(lfdir); 470 dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1); 471 inodirty(); 472 lncntp[lfdir]++; 473 pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan); 474 if (parentdir != (ino_t)-1) 475 printf("PARENT WAS I=%llu\n", (unsigned long long)parentdir); 476 if (preen == 0) 477 printf("\n"); 478 } 479 return (1); 480 } 481 482 /* 483 * fix an entry in a directory. 484 */ 485 int 486 changeino(ino_t dir, const char *name, ino_t newnum) 487 { 488 struct inodesc idesc; 489 490 memset(&idesc, 0, sizeof(struct inodesc)); 491 idesc.id_type = DATA; 492 idesc.id_func = chgino; 493 idesc.id_number = dir; 494 idesc.id_fix = DONTKNOW; 495 idesc.id_name = name; 496 idesc.id_parent = newnum; /* new value for name */ 497 return (ckinode(ginode(dir), &idesc)); 498 } 499 500 /* 501 * make an entry in a directory 502 */ 503 int 504 makeentry(ino_t parent, ino_t ino, const char *name) 505 { 506 struct ext2fs_dinode *dp; 507 struct inodesc idesc; 508 char pathbuf[MAXPATHLEN + 1]; 509 510 if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO) || parent >= maxino 511 || (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino) 512 return (0); 513 memset(&idesc, 0, sizeof(struct inodesc)); 514 idesc.id_type = DATA; 515 idesc.id_func = mkentry; 516 idesc.id_number = parent; 517 idesc.id_parent = ino; /* this is the inode to enter */ 518 idesc.id_fix = DONTKNOW; 519 idesc.id_name = name; 520 dp = ginode(parent); 521 if (inosize(dp) % sblock.e2fs_bsize) { 522 inossize(dp, roundup(inosize(dp), sblock.e2fs_bsize)); 523 inodirty(); 524 } 525 if ((ckinode(dp, &idesc) & ALTERED) != 0) 526 return (1); 527 getpathname(pathbuf, sizeof(pathbuf), parent, parent); 528 dp = ginode(parent); 529 if (expanddir(dp, pathbuf) == 0) 530 return (0); 531 return (ckinode(dp, &idesc) & ALTERED); 532 } 533 534 /* 535 * Attempt to expand the size of a directory 536 */ 537 static int 538 expanddir(struct ext2fs_dinode *dp, char *name) 539 { 540 daddr_t lastbn, newblk; 541 struct bufarea *bp; 542 char *firstblk; 543 544 lastbn = ext2_lblkno(&sblock, inosize(dp)); 545 if (lastbn >= EXT2FS_NDADDR - 1 || fs2h32(dp->e2di_blocks[lastbn]) == 0 || inosize(dp) == 0) { 546 return (0); 547 } 548 if ((newblk = allocblk()) == 0) { 549 return (0); 550 } 551 dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn]; 552 dp->e2di_blocks[lastbn] = h2fs32(newblk); 553 inossize(dp, inosize(dp) + sblock.e2fs_bsize); 554 inosnblock(dp, inonblock(dp) + btodb(sblock.e2fs_bsize)); 555 bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]), sblock.e2fs_bsize); 556 if (bp->b_errs) 557 goto bad; 558 if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL) 559 err(8, "cannot allocate first block"); 560 memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize); 561 bp = getdirblk(newblk, sblock.e2fs_bsize); 562 if (bp->b_errs) { 563 free(firstblk); 564 goto bad; 565 } 566 memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize); 567 free(firstblk); 568 dirty(bp); 569 bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]), sblock.e2fs_bsize); 570 if (bp->b_errs) 571 goto bad; 572 emptydir.dot_reclen = h2fs16(sblock.e2fs_bsize); 573 memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir); 574 pwarn("NO SPACE LEFT IN %s", name); 575 if (preen) 576 printf(" (EXPANDED)\n"); 577 else if (reply("EXPAND") == 0) 578 goto bad; 579 dirty(bp); 580 inodirty(); 581 return (1); 582 bad: 583 dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1]; 584 dp->e2di_blocks[lastbn + 1] = 0; 585 inossize(dp, inosize(dp) - sblock.e2fs_bsize); 586 inosnblock(dp, inonblock(dp) - btodb(sblock.e2fs_bsize)); 587 freeblk(newblk); 588 return (0); 589 } 590 591 /* 592 * allocate a new directory 593 */ 594 int 595 allocdir(ino_t parent, ino_t request, int mode) 596 { 597 ino_t ino; 598 struct ext2fs_dinode *dp; 599 struct bufarea *bp; 600 struct ext2fs_dirtemplate *dirp; 601 602 ino = allocino(request, IFDIR | mode); 603 dirhead.dot_reclen = h2fs16(12); /* XXX */ 604 dirhead.dotdot_reclen = h2fs16(sblock.e2fs_bsize - 12); /* XXX */ 605 dirhead.dot_namlen = 1; 606 if (sblock.e2fs.e2fs_rev > E2FS_REV0 607 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) 608 dirhead.dot_type = EXT2_FT_DIR; 609 else 610 dirhead.dot_type = 0; 611 dirhead.dotdot_namlen = 2; 612 if (sblock.e2fs.e2fs_rev > E2FS_REV0 613 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) 614 dirhead.dotdot_type = EXT2_FT_DIR; 615 else 616 dirhead.dotdot_type = 0; 617 dirp = &dirhead; 618 dirp->dot_ino = h2fs32(ino); 619 dirp->dotdot_ino = h2fs32(parent); 620 dp = ginode(ino); 621 bp = getdirblk(fs2h32(dp->e2di_blocks[0]), sblock.e2fs_bsize); 622 if (bp->b_errs) { 623 freeino(ino); 624 return (0); 625 } 626 memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate)); 627 dirty(bp); 628 dp->e2di_nlink = h2fs16(2); 629 inodirty(); 630 if (ino == EXT2_ROOTINO) { 631 lncntp[ino] = fs2h16(dp->e2di_nlink); 632 cacheino(dp, ino); 633 return (ino); 634 } 635 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 636 freeino(ino); 637 return (0); 638 } 639 cacheino(dp, ino); 640 statemap[ino] = statemap[parent]; 641 if (statemap[ino] == DSTATE) { 642 lncntp[ino] = fs2h16(dp->e2di_nlink); 643 lncntp[parent]++; 644 } 645 dp = ginode(parent); 646 dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1); 647 inodirty(); 648 return (ino); 649 } 650 651 /* 652 * free a directory inode 653 */ 654 static void 655 freedir(ino_t ino, ino_t parent) 656 { 657 struct ext2fs_dinode *dp; 658 659 if (ino != parent) { 660 dp = ginode(parent); 661 dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - 1); 662 inodirty(); 663 } 664 freeino(ino); 665 } 666 667 /* 668 * generate a temporary name for the lost+found directory. 669 */ 670 static int 671 lftempname(char *bufp, ino_t ino) 672 { 673 ino_t in; 674 char *cp; 675 int namlen; 676 677 cp = bufp + 2; 678 for (in = maxino; in > 0; in /= 10) 679 cp++; 680 *--cp = 0; 681 namlen = cp - bufp; 682 in = ino; 683 while (cp > bufp) { 684 *--cp = (in % 10) + '0'; 685 in /= 10; 686 } 687 *cp = '#'; 688 return (namlen); 689 } 690 691 /* 692 * Get a directory block. 693 * Insure that it is held until another is requested. 694 */ 695 static struct bufarea * 696 getdirblk(daddr_t blkno, long size) 697 { 698 if (pdirbp != 0) 699 pdirbp->b_flags &= ~B_INUSE; 700 pdirbp = getdatablk(blkno, size); 701 return (pdirbp); 702 }