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