1 /* 2 * Copyright (c) 1983 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)restore.c 5.7 (Berkeley) 06/01/90"; 10 #endif /* not lint */ 11 12 #include "restore.h" 13 14 /* 15 * This implements the 't' option. 16 * List entries on the tape. 17 */ 18 long 19 listfile(name, ino, type) 20 char *name; 21 ino_t ino; 22 int type; 23 { 24 long descend = hflag ? GOOD : FAIL; 25 26 if (BIT(ino, dumpmap) == 0) { 27 return (descend); 28 } 29 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir "); 30 fprintf(stdout, "%10d\t%s\n", ino, name); 31 return (descend); 32 } 33 34 /* 35 * This implements the 'x' option. 36 * Request that new entries be extracted. 37 */ 38 long 39 addfile(name, ino, type) 40 char *name; 41 ino_t ino; 42 int type; 43 { 44 register struct entry *ep; 45 long descend = hflag ? GOOD : FAIL; 46 char buf[100]; 47 48 if (BIT(ino, dumpmap) == 0) { 49 dprintf(stdout, "%s: not on the tape\n", name); 50 return (descend); 51 } 52 if (!mflag) { 53 (void) sprintf(buf, "./%u", ino); 54 name = buf; 55 if (type == NODE) { 56 (void) genliteraldir(name, ino); 57 return (descend); 58 } 59 } 60 ep = lookupino(ino); 61 if (ep != NIL) { 62 if (strcmp(name, myname(ep)) == 0) { 63 ep->e_flags |= NEW; 64 return (descend); 65 } 66 type |= LINK; 67 } 68 ep = addentry(name, ino, type); 69 if (type == NODE) 70 newnode(ep); 71 ep->e_flags |= NEW; 72 return (descend); 73 } 74 75 /* 76 * This is used by the 'i' option to undo previous requests made by addfile. 77 * Delete entries from the request queue. 78 */ 79 /* ARGSUSED */ 80 long 81 deletefile(name, ino, type) 82 char *name; 83 ino_t ino; 84 int type; 85 { 86 long descend = hflag ? GOOD : FAIL; 87 struct entry *ep; 88 89 if (BIT(ino, dumpmap) == 0) { 90 return (descend); 91 } 92 ep = lookupino(ino); 93 if (ep != NIL) 94 ep->e_flags &= ~NEW; 95 return (descend); 96 } 97 98 /* 99 * The following four routines implement the incremental 100 * restore algorithm. The first removes old entries, the second 101 * does renames and calculates the extraction list, the third 102 * cleans up link names missed by the first two, and the final 103 * one deletes old directories. 104 * 105 * Directories cannot be immediately deleted, as they may have 106 * other files in them which need to be moved out first. As 107 * directories to be deleted are found, they are put on the 108 * following deletion list. After all deletions and renames 109 * are done, this list is actually deleted. 110 */ 111 static struct entry *removelist; 112 113 /* 114 * Remove unneeded leaves from the old tree. 115 * Remove directories from the lookup chains. 116 */ 117 removeoldleaves() 118 { 119 register struct entry *ep; 120 register ino_t i; 121 122 vprintf(stdout, "Mark entries to be removed.\n"); 123 for (i = ROOTINO + 1; i < maxino; i++) { 124 ep = lookupino(i); 125 if (ep == NIL) 126 continue; 127 if (BIT(i, clrimap)) 128 continue; 129 for ( ; ep != NIL; ep = ep->e_links) { 130 dprintf(stdout, "%s: REMOVE\n", myname(ep)); 131 if (ep->e_type == LEAF) { 132 removeleaf(ep); 133 freeentry(ep); 134 } else { 135 mktempname(ep); 136 deleteino(ep->e_ino); 137 ep->e_next = removelist; 138 removelist = ep; 139 } 140 } 141 } 142 } 143 144 /* 145 * For each directory entry on the incremental tape, determine which 146 * category it falls into as follows: 147 * KEEP - entries that are to be left alone. 148 * NEW - new entries to be added. 149 * EXTRACT - files that must be updated with new contents. 150 * LINK - new links to be added. 151 * Renames are done at the same time. 152 */ 153 long 154 nodeupdates(name, ino, type) 155 char *name; 156 ino_t ino; 157 int type; 158 { 159 register struct entry *ep, *np, *ip; 160 long descend = GOOD; 161 int lookuptype = 0; 162 int key = 0; 163 /* key values */ 164 # define ONTAPE 0x1 /* inode is on the tape */ 165 # define INOFND 0x2 /* inode already exists */ 166 # define NAMEFND 0x4 /* name already exists */ 167 # define MODECHG 0x8 /* mode of inode changed */ 168 extern char *keyval(); 169 170 /* 171 * This routine is called once for each element in the 172 * directory hierarchy, with a full path name. 173 * The "type" value is incorrectly specified as LEAF for 174 * directories that are not on the dump tape. 175 * 176 * Check to see if the file is on the tape. 177 */ 178 if (BIT(ino, dumpmap)) 179 key |= ONTAPE; 180 /* 181 * Check to see if the name exists, and if the name is a link. 182 */ 183 np = lookupname(name); 184 if (np != NIL) { 185 key |= NAMEFND; 186 ip = lookupino(np->e_ino); 187 if (ip == NULL) 188 panic("corrupted symbol table\n"); 189 if (ip != np) 190 lookuptype = LINK; 191 } 192 /* 193 * Check to see if the inode exists, and if one of its links 194 * corresponds to the name (if one was found). 195 */ 196 ip = lookupino(ino); 197 if (ip != NIL) { 198 key |= INOFND; 199 for (ep = ip->e_links; ep != NIL; ep = ep->e_links) { 200 if (ep == np) { 201 ip = ep; 202 break; 203 } 204 } 205 } 206 /* 207 * If both a name and an inode are found, but they do not 208 * correspond to the same file, then both the inode that has 209 * been found and the inode corresponding to the name that 210 * has been found need to be renamed. The current pathname 211 * is the new name for the inode that has been found. Since 212 * all files to be deleted have already been removed, the 213 * named file is either a now unneeded link, or it must live 214 * under a new name in this dump level. If it is a link, it 215 * can be removed. If it is not a link, it is given a 216 * temporary name in anticipation that it will be renamed 217 * when it is later found by inode number. 218 */ 219 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) { 220 if (lookuptype == LINK) { 221 removeleaf(np); 222 freeentry(np); 223 } else { 224 dprintf(stdout, "name/inode conflict, mktempname %s\n", 225 myname(np)); 226 mktempname(np); 227 } 228 np = NIL; 229 key &= ~NAMEFND; 230 } 231 if ((key & ONTAPE) && 232 (((key & INOFND) && ip->e_type != type) || 233 ((key & NAMEFND) && np->e_type != type))) 234 key |= MODECHG; 235 236 /* 237 * Decide on the disposition of the file based on its flags. 238 * Note that we have already handled the case in which 239 * a name and inode are found that correspond to different files. 240 * Thus if both NAMEFND and INOFND are set then ip == np. 241 */ 242 switch (key) { 243 244 /* 245 * A previously existing file has been found. 246 * Mark it as KEEP so that other links to the inode can be 247 * detected, and so that it will not be reclaimed by the search 248 * for unreferenced names. 249 */ 250 case INOFND|NAMEFND: 251 ip->e_flags |= KEEP; 252 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 253 flagvalues(ip)); 254 break; 255 256 /* 257 * A file on the tape has a name which is the same as a name 258 * corresponding to a different file in the previous dump. 259 * Since all files to be deleted have already been removed, 260 * this file is either a now unneeded link, or it must live 261 * under a new name in this dump level. If it is a link, it 262 * can simply be removed. If it is not a link, it is given a 263 * temporary name in anticipation that it will be renamed 264 * when it is later found by inode number (see INOFND case 265 * below). The entry is then treated as a new file. 266 */ 267 case ONTAPE|NAMEFND: 268 case ONTAPE|NAMEFND|MODECHG: 269 if (lookuptype == LINK) { 270 removeleaf(np); 271 freeentry(np); 272 } else { 273 mktempname(np); 274 } 275 /* fall through */ 276 277 /* 278 * A previously non-existent file. 279 * Add it to the file system, and request its extraction. 280 * If it is a directory, create it immediately. 281 * (Since the name is unused there can be no conflict) 282 */ 283 case ONTAPE: 284 ep = addentry(name, ino, type); 285 if (type == NODE) 286 newnode(ep); 287 ep->e_flags |= NEW|KEEP; 288 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 289 flagvalues(ep)); 290 break; 291 292 /* 293 * A file with the same inode number, but a different 294 * name has been found. If the other name has not already 295 * been found (indicated by the KEEP flag, see above) then 296 * this must be a new name for the file, and it is renamed. 297 * If the other name has been found then this must be a 298 * link to the file. Hard links to directories are not 299 * permitted, and are either deleted or converted to 300 * symbolic links. Finally, if the file is on the tape, 301 * a request is made to extract it. 302 */ 303 case ONTAPE|INOFND: 304 if (type == LEAF && (ip->e_flags & KEEP) == 0) 305 ip->e_flags |= EXTRACT; 306 /* fall through */ 307 case INOFND: 308 if ((ip->e_flags & KEEP) == 0) { 309 renameit(myname(ip), name); 310 moveentry(ip, name); 311 ip->e_flags |= KEEP; 312 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 313 flagvalues(ip)); 314 break; 315 } 316 if (ip->e_type == NODE) { 317 descend = FAIL; 318 fprintf(stderr, 319 "deleted hard link %s to directory %s\n", 320 name, myname(ip)); 321 break; 322 } 323 ep = addentry(name, ino, type|LINK); 324 ep->e_flags |= NEW; 325 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 326 flagvalues(ep)); 327 break; 328 329 /* 330 * A previously known file which is to be updated. 331 */ 332 case ONTAPE|INOFND|NAMEFND: 333 if (type == LEAF && lookuptype != LINK) 334 np->e_flags |= EXTRACT; 335 np->e_flags |= KEEP; 336 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 337 flagvalues(np)); 338 break; 339 340 /* 341 * An inode is being reused in a completely different way. 342 * Normally an extract can simply do an "unlink" followed 343 * by a "creat". Here we must do effectively the same 344 * thing. The complications arise because we cannot really 345 * delete a directory since it may still contain files 346 * that we need to rename, so we delete it from the symbol 347 * table, and put it on the list to be deleted eventually. 348 * Conversely if a directory is to be created, it must be 349 * done immediately, rather than waiting until the 350 * extraction phase. 351 */ 352 case ONTAPE|INOFND|MODECHG: 353 case ONTAPE|INOFND|NAMEFND|MODECHG: 354 if (ip->e_flags & KEEP) { 355 badentry(ip, "cannot KEEP and change modes"); 356 break; 357 } 358 if (ip->e_type == LEAF) { 359 /* changing from leaf to node */ 360 removeleaf(ip); 361 freeentry(ip); 362 ip = addentry(name, ino, type); 363 newnode(ip); 364 } else { 365 /* changing from node to leaf */ 366 if ((ip->e_flags & TMPNAME) == 0) 367 mktempname(ip); 368 deleteino(ip->e_ino); 369 ip->e_next = removelist; 370 removelist = ip; 371 ip = addentry(name, ino, type); 372 } 373 ip->e_flags |= NEW|KEEP; 374 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 375 flagvalues(ip)); 376 break; 377 378 /* 379 * A hard link to a diirectory that has been removed. 380 * Ignore it. 381 */ 382 case NAMEFND: 383 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key), 384 name); 385 descend = FAIL; 386 break; 387 388 /* 389 * If we find a directory entry for a file that is not on 390 * the tape, then we must have found a file that was created 391 * while the dump was in progress. Since we have no contents 392 * for it, we discard the name knowing that it will be on the 393 * next incremental tape. 394 */ 395 case NIL: 396 fprintf(stderr, "%s: (inode %d) not found on tape\n", 397 name, ino); 398 break; 399 400 /* 401 * If any of these arise, something is grievously wrong with 402 * the current state of the symbol table. 403 */ 404 case INOFND|NAMEFND|MODECHG: 405 case NAMEFND|MODECHG: 406 case INOFND|MODECHG: 407 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key), 408 name); 409 break; 410 411 /* 412 * These states "cannot" arise for any state of the symbol table. 413 */ 414 case ONTAPE|MODECHG: 415 case MODECHG: 416 default: 417 panic("[%s] %s: impossible state\n", keyval(key), name); 418 break; 419 } 420 return (descend); 421 } 422 423 /* 424 * Calculate the active flags in a key. 425 */ 426 char * 427 keyval(key) 428 int key; 429 { 430 static char keybuf[32]; 431 432 (void) strcpy(keybuf, "|NIL"); 433 keybuf[0] = '\0'; 434 if (key & ONTAPE) 435 (void) strcat(keybuf, "|ONTAPE"); 436 if (key & INOFND) 437 (void) strcat(keybuf, "|INOFND"); 438 if (key & NAMEFND) 439 (void) strcat(keybuf, "|NAMEFND"); 440 if (key & MODECHG) 441 (void) strcat(keybuf, "|MODECHG"); 442 return (&keybuf[1]); 443 } 444 445 /* 446 * Find unreferenced link names. 447 */ 448 findunreflinks() 449 { 450 register struct entry *ep, *np; 451 register ino_t i; 452 453 vprintf(stdout, "Find unreferenced names.\n"); 454 for (i = ROOTINO; i < maxino; i++) { 455 ep = lookupino(i); 456 if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0) 457 continue; 458 for (np = ep->e_entries; np != NIL; np = np->e_sibling) { 459 if (np->e_flags == 0) { 460 dprintf(stdout, 461 "%s: remove unreferenced name\n", 462 myname(np)); 463 removeleaf(np); 464 freeentry(np); 465 } 466 } 467 } 468 /* 469 * Any leaves remaining in removed directories is unreferenced. 470 */ 471 for (ep = removelist; ep != NIL; ep = ep->e_next) { 472 for (np = ep->e_entries; np != NIL; np = np->e_sibling) { 473 if (np->e_type == LEAF) { 474 if (np->e_flags != 0) 475 badentry(np, "unreferenced with flags"); 476 dprintf(stdout, 477 "%s: remove unreferenced name\n", 478 myname(np)); 479 removeleaf(np); 480 freeentry(np); 481 } 482 } 483 } 484 } 485 486 /* 487 * Remove old nodes (directories). 488 * Note that this routine runs in O(N*D) where: 489 * N is the number of directory entries to be removed. 490 * D is the maximum depth of the tree. 491 * If N == D this can be quite slow. If the list were 492 * topologically sorted, the deletion could be done in 493 * time O(N). 494 */ 495 removeoldnodes() 496 { 497 register struct entry *ep, **prev; 498 long change; 499 500 vprintf(stdout, "Remove old nodes (directories).\n"); 501 do { 502 change = 0; 503 prev = &removelist; 504 for (ep = removelist; ep != NIL; ep = *prev) { 505 if (ep->e_entries != NIL) { 506 prev = &ep->e_next; 507 continue; 508 } 509 *prev = ep->e_next; 510 removenode(ep); 511 freeentry(ep); 512 change++; 513 } 514 } while (change); 515 for (ep = removelist; ep != NIL; ep = ep->e_next) 516 badentry(ep, "cannot remove, non-empty"); 517 } 518 519 /* 520 * This is the routine used to extract files for the 'r' command. 521 * Extract new leaves. 522 */ 523 createleaves(symtabfile) 524 char *symtabfile; 525 { 526 register struct entry *ep; 527 ino_t first; 528 long curvol; 529 530 if (command == 'R') { 531 vprintf(stdout, "Continue extraction of new leaves\n"); 532 } else { 533 vprintf(stdout, "Extract new leaves.\n"); 534 dumpsymtable(symtabfile, volno); 535 } 536 first = lowerbnd(ROOTINO); 537 curvol = volno; 538 while (curfile.ino < maxino) { 539 first = lowerbnd(first); 540 /* 541 * If the next available file is not the one which we 542 * expect then we have missed one or more files. Since 543 * we do not request files that were not on the tape, 544 * the lost files must have been due to a tape read error, 545 * or a file that was removed while the dump was in progress. 546 */ 547 while (first < curfile.ino) { 548 ep = lookupino(first); 549 if (ep == NIL) 550 panic("%d: bad first\n", first); 551 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 552 ep->e_flags &= ~(NEW|EXTRACT); 553 first = lowerbnd(first); 554 } 555 /* 556 * If we find files on the tape that have no corresponding 557 * directory entries, then we must have found a file that 558 * was created while the dump was in progress. Since we have 559 * no name for it, we discard it knowing that it will be 560 * on the next incremental tape. 561 */ 562 if (first != curfile.ino) { 563 fprintf(stderr, "expected next file %d, got %d\n", 564 first, curfile.ino); 565 skipfile(); 566 goto next; 567 } 568 ep = lookupino(curfile.ino); 569 if (ep == NIL) 570 panic("unknown file on tape\n"); 571 if ((ep->e_flags & (NEW|EXTRACT)) == 0) 572 badentry(ep, "unexpected file on tape"); 573 /* 574 * If the file is to be extracted, then the old file must 575 * be removed since its type may change from one leaf type 576 * to another (eg "file" to "character special"). 577 */ 578 if ((ep->e_flags & EXTRACT) != 0) { 579 removeleaf(ep); 580 ep->e_flags &= ~REMOVED; 581 } 582 (void) extractfile(myname(ep)); 583 ep->e_flags &= ~(NEW|EXTRACT); 584 /* 585 * We checkpoint the restore after every tape reel, so 586 * as to simplify the amount of work re quired by the 587 * 'R' command. 588 */ 589 next: 590 if (curvol != volno) { 591 dumpsymtable(symtabfile, volno); 592 skipmaps(); 593 curvol = volno; 594 } 595 } 596 } 597 598 /* 599 * This is the routine used to extract files for the 'x' and 'i' commands. 600 * Efficiently extract a subset of the files on a tape. 601 */ 602 createfiles() 603 { 604 register ino_t first, next, last; 605 register struct entry *ep; 606 long curvol; 607 608 vprintf(stdout, "Extract requested files\n"); 609 curfile.action = SKIP; 610 getvol((long)1); 611 skipmaps(); 612 skipdirs(); 613 first = lowerbnd(ROOTINO); 614 last = upperbnd(maxino - 1); 615 for (;;) { 616 first = lowerbnd(first); 617 last = upperbnd(last); 618 /* 619 * Check to see if any files remain to be extracted 620 */ 621 if (first > last) 622 return; 623 /* 624 * Reject any volumes with inodes greater 625 * than the last one needed 626 */ 627 while (curfile.ino > last) { 628 curfile.action = SKIP; 629 getvol((long)0); 630 skipmaps(); 631 skipdirs(); 632 } 633 /* 634 * Decide on the next inode needed. 635 * Skip across the inodes until it is found 636 * or an out of order volume change is encountered 637 */ 638 next = lowerbnd(curfile.ino); 639 do { 640 curvol = volno; 641 while (next > curfile.ino && volno == curvol) 642 skipfile(); 643 skipmaps(); 644 skipdirs(); 645 } while (volno == curvol + 1); 646 /* 647 * If volume change out of order occurred the 648 * current state must be recalculated 649 */ 650 if (volno != curvol) 651 continue; 652 /* 653 * If the current inode is greater than the one we were 654 * looking for then we missed the one we were looking for. 655 * Since we only attempt to extract files listed in the 656 * dump map, the lost files must have been due to a tape 657 * read error, or a file that was removed while the dump 658 * was in progress. Thus we report all requested files 659 * between the one we were looking for, and the one we 660 * found as missing, and delete their request flags. 661 */ 662 while (next < curfile.ino) { 663 ep = lookupino(next); 664 if (ep == NIL) 665 panic("corrupted symbol table\n"); 666 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 667 ep->e_flags &= ~NEW; 668 next = lowerbnd(next); 669 } 670 /* 671 * The current inode is the one that we are looking for, 672 * so extract it per its requested name. 673 */ 674 if (next == curfile.ino && next <= last) { 675 ep = lookupino(next); 676 if (ep == NIL) 677 panic("corrupted symbol table\n"); 678 (void) extractfile(myname(ep)); 679 ep->e_flags &= ~NEW; 680 if (volno != curvol) 681 skipmaps(); 682 } 683 } 684 } 685 686 /* 687 * Add links. 688 */ 689 createlinks() 690 { 691 register struct entry *np, *ep; 692 register ino_t i; 693 char name[BUFSIZ]; 694 695 vprintf(stdout, "Add links\n"); 696 for (i = ROOTINO; i < maxino; i++) { 697 ep = lookupino(i); 698 if (ep == NIL) 699 continue; 700 for (np = ep->e_links; np != NIL; np = np->e_links) { 701 if ((np->e_flags & NEW) == 0) 702 continue; 703 (void) strcpy(name, myname(ep)); 704 if (ep->e_type == NODE) { 705 (void) linkit(name, myname(np), SYMLINK); 706 } else { 707 (void) linkit(name, myname(np), HARDLINK); 708 } 709 np->e_flags &= ~NEW; 710 } 711 } 712 } 713 714 /* 715 * Check the symbol table. 716 * We do this to insure that all the requested work was done, and 717 * that no temporary names remain. 718 */ 719 checkrestore() 720 { 721 register struct entry *ep; 722 register ino_t i; 723 724 vprintf(stdout, "Check the symbol table.\n"); 725 for (i = ROOTINO; i < maxino; i++) { 726 for (ep = lookupino(i); ep != NIL; ep = ep->e_links) { 727 ep->e_flags &= ~KEEP; 728 if (ep->e_type == NODE) 729 ep->e_flags &= ~(NEW|EXISTED); 730 if (ep->e_flags != NULL) 731 badentry(ep, "incomplete operations"); 732 } 733 } 734 } 735 736 /* 737 * Compare with the directory structure on the tape 738 * A paranoid check that things are as they should be. 739 */ 740 long 741 verifyfile(name, ino, type) 742 char *name; 743 ino_t ino; 744 int type; 745 { 746 struct entry *np, *ep; 747 long descend = GOOD; 748 749 ep = lookupname(name); 750 if (ep == NIL) { 751 fprintf(stderr, "Warning: missing name %s\n", name); 752 return (FAIL); 753 } 754 np = lookupino(ino); 755 if (np != ep) 756 descend = FAIL; 757 for ( ; np != NIL; np = np->e_links) 758 if (np == ep) 759 break; 760 if (np == NIL) 761 panic("missing inumber %d\n", ino); 762 if (ep->e_type == LEAF && type != LEAF) 763 badentry(ep, "type should be LEAF"); 764 return (descend); 765 } 766