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.9 (Berkeley) 07/19/92"; 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. If it is a link, 331 * then all names referring to the previous file must be removed 332 * so that the subset of them that remain can be recreated. 333 */ 334 case ONTAPE|INOFND|NAMEFND: 335 if (lookuptype == LINK) { 336 removeleaf(np); 337 freeentry(np); 338 ep = addentry(name, ino, type|LINK); 339 if (type == NODE) 340 newnode(ep); 341 ep->e_flags |= NEW|KEEP; 342 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 343 flagvalues(ep)); 344 break; 345 } 346 if (type == LEAF && lookuptype != LINK) 347 np->e_flags |= EXTRACT; 348 np->e_flags |= KEEP; 349 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 350 flagvalues(np)); 351 break; 352 353 /* 354 * An inode is being reused in a completely different way. 355 * Normally an extract can simply do an "unlink" followed 356 * by a "creat". Here we must do effectively the same 357 * thing. The complications arise because we cannot really 358 * delete a directory since it may still contain files 359 * that we need to rename, so we delete it from the symbol 360 * table, and put it on the list to be deleted eventually. 361 * Conversely if a directory is to be created, it must be 362 * done immediately, rather than waiting until the 363 * extraction phase. 364 */ 365 case ONTAPE|INOFND|MODECHG: 366 case ONTAPE|INOFND|NAMEFND|MODECHG: 367 if (ip->e_flags & KEEP) { 368 badentry(ip, "cannot KEEP and change modes"); 369 break; 370 } 371 if (ip->e_type == LEAF) { 372 /* changing from leaf to node */ 373 removeleaf(ip); 374 freeentry(ip); 375 ip = addentry(name, ino, type); 376 newnode(ip); 377 } else { 378 /* changing from node to leaf */ 379 if ((ip->e_flags & TMPNAME) == 0) 380 mktempname(ip); 381 deleteino(ip->e_ino); 382 ip->e_next = removelist; 383 removelist = ip; 384 ip = addentry(name, ino, type); 385 } 386 ip->e_flags |= NEW|KEEP; 387 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 388 flagvalues(ip)); 389 break; 390 391 /* 392 * A hard link to a diirectory that has been removed. 393 * Ignore it. 394 */ 395 case NAMEFND: 396 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key), 397 name); 398 descend = FAIL; 399 break; 400 401 /* 402 * If we find a directory entry for a file that is not on 403 * the tape, then we must have found a file that was created 404 * while the dump was in progress. Since we have no contents 405 * for it, we discard the name knowing that it will be on the 406 * next incremental tape. 407 */ 408 case NIL: 409 fprintf(stderr, "%s: (inode %d) not found on tape\n", 410 name, ino); 411 break; 412 413 /* 414 * If any of these arise, something is grievously wrong with 415 * the current state of the symbol table. 416 */ 417 case INOFND|NAMEFND|MODECHG: 418 case NAMEFND|MODECHG: 419 case INOFND|MODECHG: 420 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key), 421 name); 422 break; 423 424 /* 425 * These states "cannot" arise for any state of the symbol table. 426 */ 427 case ONTAPE|MODECHG: 428 case MODECHG: 429 default: 430 panic("[%s] %s: impossible state\n", keyval(key), name); 431 break; 432 } 433 return (descend); 434 } 435 436 /* 437 * Calculate the active flags in a key. 438 */ 439 char * 440 keyval(key) 441 int key; 442 { 443 static char keybuf[32]; 444 445 (void) strcpy(keybuf, "|NIL"); 446 keybuf[0] = '\0'; 447 if (key & ONTAPE) 448 (void) strcat(keybuf, "|ONTAPE"); 449 if (key & INOFND) 450 (void) strcat(keybuf, "|INOFND"); 451 if (key & NAMEFND) 452 (void) strcat(keybuf, "|NAMEFND"); 453 if (key & MODECHG) 454 (void) strcat(keybuf, "|MODECHG"); 455 return (&keybuf[1]); 456 } 457 458 /* 459 * Find unreferenced link names. 460 */ 461 findunreflinks() 462 { 463 register struct entry *ep, *np; 464 register ino_t i; 465 466 vprintf(stdout, "Find unreferenced names.\n"); 467 for (i = ROOTINO; i < maxino; i++) { 468 ep = lookupino(i); 469 if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0) 470 continue; 471 for (np = ep->e_entries; np != NIL; np = np->e_sibling) { 472 if (np->e_flags == 0) { 473 dprintf(stdout, 474 "%s: remove unreferenced name\n", 475 myname(np)); 476 removeleaf(np); 477 freeentry(np); 478 } 479 } 480 } 481 /* 482 * Any leaves remaining in removed directories is unreferenced. 483 */ 484 for (ep = removelist; ep != NIL; ep = ep->e_next) { 485 for (np = ep->e_entries; np != NIL; np = np->e_sibling) { 486 if (np->e_type == LEAF) { 487 if (np->e_flags != 0) 488 badentry(np, "unreferenced with flags"); 489 dprintf(stdout, 490 "%s: remove unreferenced name\n", 491 myname(np)); 492 removeleaf(np); 493 freeentry(np); 494 } 495 } 496 } 497 } 498 499 /* 500 * Remove old nodes (directories). 501 * Note that this routine runs in O(N*D) where: 502 * N is the number of directory entries to be removed. 503 * D is the maximum depth of the tree. 504 * If N == D this can be quite slow. If the list were 505 * topologically sorted, the deletion could be done in 506 * time O(N). 507 */ 508 removeoldnodes() 509 { 510 register struct entry *ep, **prev; 511 long change; 512 513 vprintf(stdout, "Remove old nodes (directories).\n"); 514 do { 515 change = 0; 516 prev = &removelist; 517 for (ep = removelist; ep != NIL; ep = *prev) { 518 if (ep->e_entries != NIL) { 519 prev = &ep->e_next; 520 continue; 521 } 522 *prev = ep->e_next; 523 removenode(ep); 524 freeentry(ep); 525 change++; 526 } 527 } while (change); 528 for (ep = removelist; ep != NIL; ep = ep->e_next) 529 badentry(ep, "cannot remove, non-empty"); 530 } 531 532 /* 533 * This is the routine used to extract files for the 'r' command. 534 * Extract new leaves. 535 */ 536 createleaves(symtabfile) 537 char *symtabfile; 538 { 539 register struct entry *ep; 540 ino_t first; 541 long curvol; 542 543 if (command == 'R') { 544 vprintf(stdout, "Continue extraction of new leaves\n"); 545 } else { 546 vprintf(stdout, "Extract new leaves.\n"); 547 dumpsymtable(symtabfile, volno); 548 } 549 first = lowerbnd(ROOTINO); 550 curvol = volno; 551 while (curfile.ino < maxino) { 552 first = lowerbnd(first); 553 /* 554 * If the next available file is not the one which we 555 * expect then we have missed one or more files. Since 556 * we do not request files that were not on the tape, 557 * the lost files must have been due to a tape read error, 558 * or a file that was removed while the dump was in progress. 559 */ 560 while (first < curfile.ino) { 561 ep = lookupino(first); 562 if (ep == NIL) 563 panic("%d: bad first\n", first); 564 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 565 ep->e_flags &= ~(NEW|EXTRACT); 566 first = lowerbnd(first); 567 } 568 /* 569 * If we find files on the tape that have no corresponding 570 * directory entries, then we must have found a file that 571 * was created while the dump was in progress. Since we have 572 * no name for it, we discard it knowing that it will be 573 * on the next incremental tape. 574 */ 575 if (first != curfile.ino) { 576 fprintf(stderr, "expected next file %d, got %d\n", 577 first, curfile.ino); 578 skipfile(); 579 goto next; 580 } 581 ep = lookupino(curfile.ino); 582 if (ep == NIL) 583 panic("unknown file on tape\n"); 584 if ((ep->e_flags & (NEW|EXTRACT)) == 0) 585 badentry(ep, "unexpected file on tape"); 586 /* 587 * If the file is to be extracted, then the old file must 588 * be removed since its type may change from one leaf type 589 * to another (eg "file" to "character special"). 590 */ 591 if ((ep->e_flags & EXTRACT) != 0) { 592 removeleaf(ep); 593 ep->e_flags &= ~REMOVED; 594 } 595 (void) extractfile(myname(ep)); 596 ep->e_flags &= ~(NEW|EXTRACT); 597 /* 598 * We checkpoint the restore after every tape reel, so 599 * as to simplify the amount of work re quired by the 600 * 'R' command. 601 */ 602 next: 603 if (curvol != volno) { 604 dumpsymtable(symtabfile, volno); 605 skipmaps(); 606 curvol = volno; 607 } 608 } 609 } 610 611 /* 612 * This is the routine used to extract files for the 'x' and 'i' commands. 613 * Efficiently extract a subset of the files on a tape. 614 */ 615 createfiles() 616 { 617 register ino_t first, next, last; 618 register struct entry *ep; 619 long curvol; 620 621 vprintf(stdout, "Extract requested files\n"); 622 curfile.action = SKIP; 623 getvol((long)1); 624 skipmaps(); 625 skipdirs(); 626 first = lowerbnd(ROOTINO); 627 last = upperbnd(maxino - 1); 628 for (;;) { 629 first = lowerbnd(first); 630 last = upperbnd(last); 631 /* 632 * Check to see if any files remain to be extracted 633 */ 634 if (first > last) 635 return; 636 /* 637 * Reject any volumes with inodes greater 638 * than the last one needed 639 */ 640 while (curfile.ino > last) { 641 curfile.action = SKIP; 642 getvol((long)0); 643 skipmaps(); 644 skipdirs(); 645 } 646 /* 647 * Decide on the next inode needed. 648 * Skip across the inodes until it is found 649 * or an out of order volume change is encountered 650 */ 651 next = lowerbnd(curfile.ino); 652 do { 653 curvol = volno; 654 while (next > curfile.ino && volno == curvol) 655 skipfile(); 656 skipmaps(); 657 skipdirs(); 658 } while (volno == curvol + 1); 659 /* 660 * If volume change out of order occurred the 661 * current state must be recalculated 662 */ 663 if (volno != curvol) 664 continue; 665 /* 666 * If the current inode is greater than the one we were 667 * looking for then we missed the one we were looking for. 668 * Since we only attempt to extract files listed in the 669 * dump map, the lost files must have been due to a tape 670 * read error, or a file that was removed while the dump 671 * was in progress. Thus we report all requested files 672 * between the one we were looking for, and the one we 673 * found as missing, and delete their request flags. 674 */ 675 while (next < curfile.ino) { 676 ep = lookupino(next); 677 if (ep == NIL) 678 panic("corrupted symbol table\n"); 679 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 680 ep->e_flags &= ~NEW; 681 next = lowerbnd(next); 682 } 683 /* 684 * The current inode is the one that we are looking for, 685 * so extract it per its requested name. 686 */ 687 if (next == curfile.ino && next <= last) { 688 ep = lookupino(next); 689 if (ep == NIL) 690 panic("corrupted symbol table\n"); 691 (void) extractfile(myname(ep)); 692 ep->e_flags &= ~NEW; 693 if (volno != curvol) 694 skipmaps(); 695 } 696 } 697 } 698 699 /* 700 * Add links. 701 */ 702 createlinks() 703 { 704 register struct entry *np, *ep; 705 register ino_t i; 706 char name[BUFSIZ]; 707 708 vprintf(stdout, "Add links\n"); 709 for (i = ROOTINO; i < maxino; i++) { 710 ep = lookupino(i); 711 if (ep == NIL) 712 continue; 713 for (np = ep->e_links; np != NIL; np = np->e_links) { 714 if ((np->e_flags & NEW) == 0) 715 continue; 716 (void) strcpy(name, myname(ep)); 717 if (ep->e_type == NODE) { 718 (void) linkit(name, myname(np), SYMLINK); 719 } else { 720 (void) linkit(name, myname(np), HARDLINK); 721 } 722 np->e_flags &= ~NEW; 723 } 724 } 725 } 726 727 /* 728 * Check the symbol table. 729 * We do this to insure that all the requested work was done, and 730 * that no temporary names remain. 731 */ 732 checkrestore() 733 { 734 register struct entry *ep; 735 register ino_t i; 736 737 vprintf(stdout, "Check the symbol table.\n"); 738 for (i = ROOTINO; i < maxino; i++) { 739 for (ep = lookupino(i); ep != NIL; ep = ep->e_links) { 740 ep->e_flags &= ~KEEP; 741 if (ep->e_type == NODE) 742 ep->e_flags &= ~(NEW|EXISTED); 743 if (ep->e_flags != NULL) 744 badentry(ep, "incomplete operations"); 745 } 746 } 747 } 748 749 /* 750 * Compare with the directory structure on the tape 751 * A paranoid check that things are as they should be. 752 */ 753 long 754 verifyfile(name, ino, type) 755 char *name; 756 ino_t ino; 757 int type; 758 { 759 struct entry *np, *ep; 760 long descend = GOOD; 761 762 ep = lookupname(name); 763 if (ep == NIL) { 764 fprintf(stderr, "Warning: missing name %s\n", name); 765 return (FAIL); 766 } 767 np = lookupino(ino); 768 if (np != ep) 769 descend = FAIL; 770 for ( ; np != NIL; np = np->e_links) 771 if (np == ep) 772 break; 773 if (np == NIL) 774 panic("missing inumber %d\n", ino); 775 if (ep->e_type == LEAF && type != LEAF) 776 badentry(ep, "type should be LEAF"); 777 return (descend); 778 } 779