1 /* $NetBSD: symtab.c,v 1.8 1995/03/18 14:59:54 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)symtab.c 8.2 (Berkeley) 9/13/94"; 39 #else 40 static char rcsid[] = "$NetBSD: symtab.c,v 1.8 1995/03/18 14:59:54 cgd Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * These routines maintain the symbol table which tracks the state 46 * of the file system being restored. They provide lookup by either 47 * name or inode number. They also provide for creation, deletion, 48 * and renaming of entries. Because of the dynamic nature of pathnames, 49 * names should not be saved, but always constructed just before they 50 * are needed, by calling "myname". 51 */ 52 53 #include <sys/param.h> 54 #include <sys/stat.h> 55 56 #include <ufs/ufs/dinode.h> 57 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "restore.h" 66 #include "extern.h" 67 68 /* 69 * The following variables define the inode symbol table. 70 * The primary hash table is dynamically allocated based on 71 * the number of inodes in the file system (maxino), scaled by 72 * HASHFACTOR. The variable "entry" points to the hash table; 73 * the variable "entrytblsize" indicates its size (in entries). 74 */ 75 #define HASHFACTOR 5 76 static struct entry **entry; 77 static long entrytblsize; 78 79 static void addino __P((ino_t, struct entry *)); 80 static struct entry *lookupparent __P((char *)); 81 static void removeentry __P((struct entry *)); 82 83 /* 84 * Look up an entry by inode number 85 */ 86 struct entry * 87 lookupino(inum) 88 ino_t inum; 89 { 90 register struct entry *ep; 91 92 if (inum < WINO || inum >= maxino) 93 return (NULL); 94 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) 95 if (ep->e_ino == inum) 96 return (ep); 97 return (NULL); 98 } 99 100 /* 101 * Add an entry into the entry table 102 */ 103 static void 104 addino(inum, np) 105 ino_t inum; 106 struct entry *np; 107 { 108 struct entry **epp; 109 110 if (inum < WINO || inum >= maxino) 111 panic("addino: out of range %d\n", inum); 112 epp = &entry[inum % entrytblsize]; 113 np->e_ino = inum; 114 np->e_next = *epp; 115 *epp = np; 116 if (dflag) 117 for (np = np->e_next; np != NULL; np = np->e_next) 118 if (np->e_ino == inum) 119 badentry(np, "duplicate inum"); 120 } 121 122 /* 123 * Delete an entry from the entry table 124 */ 125 void 126 deleteino(inum) 127 ino_t inum; 128 { 129 register struct entry *next; 130 struct entry **prev; 131 132 if (inum < WINO || inum >= maxino) 133 panic("deleteino: out of range %d\n", inum); 134 prev = &entry[inum % entrytblsize]; 135 for (next = *prev; next != NULL; next = next->e_next) { 136 if (next->e_ino == inum) { 137 next->e_ino = 0; 138 *prev = next->e_next; 139 return; 140 } 141 prev = &next->e_next; 142 } 143 panic("deleteino: %d not found\n", inum); 144 } 145 146 /* 147 * Look up an entry by name 148 */ 149 struct entry * 150 lookupname(name) 151 char *name; 152 { 153 register struct entry *ep; 154 register char *np, *cp; 155 char buf[MAXPATHLEN]; 156 157 cp = name; 158 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { 159 for (np = buf; *cp != '/' && *cp != '\0'; ) 160 *np++ = *cp++; 161 *np = '\0'; 162 for ( ; ep != NULL; ep = ep->e_sibling) 163 if (strcmp(ep->e_name, buf) == 0) 164 break; 165 if (ep == NULL) 166 break; 167 if (*cp++ == '\0') 168 return (ep); 169 } 170 return (NULL); 171 } 172 173 /* 174 * Look up the parent of a pathname 175 */ 176 static struct entry * 177 lookupparent(name) 178 char *name; 179 { 180 struct entry *ep; 181 char *tailindex; 182 183 tailindex = strrchr(name, '/'); 184 if (tailindex == NULL) 185 return (NULL); 186 *tailindex = '\0'; 187 ep = lookupname(name); 188 *tailindex = '/'; 189 if (ep == NULL) 190 return (NULL); 191 if (ep->e_type != NODE) 192 panic("%s is not a directory\n", name); 193 return (ep); 194 } 195 196 /* 197 * Determine the current pathname of a node or leaf 198 */ 199 char * 200 myname(ep) 201 register struct entry *ep; 202 { 203 register char *cp; 204 static char namebuf[MAXPATHLEN]; 205 206 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { 207 cp -= ep->e_namlen; 208 memcpy(cp, ep->e_name, (long)ep->e_namlen); 209 if (ep == lookupino(ROOTINO)) 210 return (cp); 211 *(--cp) = '/'; 212 ep = ep->e_parent; 213 } 214 panic("%s: pathname too long\n", cp); 215 return(cp); 216 } 217 218 /* 219 * Unused symbol table entries are linked together on a freelist 220 * headed by the following pointer. 221 */ 222 static struct entry *freelist = NULL; 223 224 /* 225 * add an entry to the symbol table 226 */ 227 struct entry * 228 addentry(name, inum, type) 229 char *name; 230 ino_t inum; 231 int type; 232 { 233 register struct entry *np, *ep; 234 235 if (freelist != NULL) { 236 np = freelist; 237 freelist = np->e_next; 238 memset(np, 0, (long)sizeof(struct entry)); 239 } else { 240 np = (struct entry *)calloc(1, sizeof(struct entry)); 241 if (np == NULL) 242 panic("no memory to extend symbol table\n"); 243 } 244 np->e_type = type & ~LINK; 245 ep = lookupparent(name); 246 if (ep == NULL) { 247 if (inum != ROOTINO || lookupino(ROOTINO) != NULL) 248 panic("bad name to addentry %s\n", name); 249 np->e_name = savename(name); 250 np->e_namlen = strlen(name); 251 np->e_parent = np; 252 addino(ROOTINO, np); 253 return (np); 254 } 255 np->e_name = savename(strrchr(name, '/') + 1); 256 np->e_namlen = strlen(np->e_name); 257 np->e_parent = ep; 258 np->e_sibling = ep->e_entries; 259 ep->e_entries = np; 260 if (type & LINK) { 261 ep = lookupino(inum); 262 if (ep == NULL) 263 panic("link to non-existant name\n"); 264 np->e_ino = inum; 265 np->e_links = ep->e_links; 266 ep->e_links = np; 267 } else if (inum != 0) { 268 if (lookupino(inum) != NULL) 269 panic("duplicate entry\n"); 270 addino(inum, np); 271 } 272 return (np); 273 } 274 275 /* 276 * delete an entry from the symbol table 277 */ 278 void 279 freeentry(ep) 280 register struct entry *ep; 281 { 282 register struct entry *np; 283 ino_t inum; 284 285 if (ep->e_flags != REMOVED) 286 badentry(ep, "not marked REMOVED"); 287 if (ep->e_type == NODE) { 288 if (ep->e_links != NULL) 289 badentry(ep, "freeing referenced directory"); 290 if (ep->e_entries != NULL) 291 badentry(ep, "freeing non-empty directory"); 292 } 293 if (ep->e_ino != 0) { 294 np = lookupino(ep->e_ino); 295 if (np == NULL) 296 badentry(ep, "lookupino failed"); 297 if (np == ep) { 298 inum = ep->e_ino; 299 deleteino(inum); 300 if (ep->e_links != NULL) 301 addino(inum, ep->e_links); 302 } else { 303 for (; np != NULL; np = np->e_links) { 304 if (np->e_links == ep) { 305 np->e_links = ep->e_links; 306 break; 307 } 308 } 309 if (np == NULL) 310 badentry(ep, "link not found"); 311 } 312 } 313 removeentry(ep); 314 freename(ep->e_name); 315 ep->e_next = freelist; 316 freelist = ep; 317 } 318 319 /* 320 * Relocate an entry in the tree structure 321 */ 322 void 323 moveentry(ep, newname) 324 register struct entry *ep; 325 char *newname; 326 { 327 struct entry *np; 328 char *cp; 329 330 np = lookupparent(newname); 331 if (np == NULL) 332 badentry(ep, "cannot move ROOT"); 333 if (np != ep->e_parent) { 334 removeentry(ep); 335 ep->e_parent = np; 336 ep->e_sibling = np->e_entries; 337 np->e_entries = ep; 338 } 339 cp = strrchr(newname, '/') + 1; 340 freename(ep->e_name); 341 ep->e_name = savename(cp); 342 ep->e_namlen = strlen(cp); 343 if (strcmp(gentempname(ep), ep->e_name) == 0) 344 ep->e_flags |= TMPNAME; 345 else 346 ep->e_flags &= ~TMPNAME; 347 } 348 349 /* 350 * Remove an entry in the tree structure 351 */ 352 static void 353 removeentry(ep) 354 register struct entry *ep; 355 { 356 register struct entry *np; 357 358 np = ep->e_parent; 359 if (np->e_entries == ep) { 360 np->e_entries = ep->e_sibling; 361 } else { 362 for (np = np->e_entries; np != NULL; np = np->e_sibling) { 363 if (np->e_sibling == ep) { 364 np->e_sibling = ep->e_sibling; 365 break; 366 } 367 } 368 if (np == NULL) 369 badentry(ep, "cannot find entry in parent list"); 370 } 371 } 372 373 /* 374 * Table of unused string entries, sorted by length. 375 * 376 * Entries are allocated in STRTBLINCR sized pieces so that names 377 * of similar lengths can use the same entry. The value of STRTBLINCR 378 * is chosen so that every entry has at least enough space to hold 379 * a "struct strtbl" header. Thus every entry can be linked onto an 380 * apprpriate free list. 381 * 382 * NB. The macro "allocsize" below assumes that "struct strhdr" 383 * has a size that is a power of two. 384 */ 385 struct strhdr { 386 struct strhdr *next; 387 }; 388 389 #define STRTBLINCR (sizeof(struct strhdr)) 390 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1)) 391 392 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR]; 393 394 /* 395 * Allocate space for a name. It first looks to see if it already 396 * has an appropriate sized entry, and if not allocates a new one. 397 */ 398 char * 399 savename(name) 400 char *name; 401 { 402 struct strhdr *np; 403 long len; 404 char *cp; 405 406 if (name == NULL) 407 panic("bad name\n"); 408 len = strlen(name); 409 np = strtblhdr[len / STRTBLINCR].next; 410 if (np != NULL) { 411 strtblhdr[len / STRTBLINCR].next = np->next; 412 cp = (char *)np; 413 } else { 414 cp = malloc((unsigned)allocsize(len)); 415 if (cp == NULL) 416 panic("no space for string table\n"); 417 } 418 (void) strcpy(cp, name); 419 return (cp); 420 } 421 422 /* 423 * Free space for a name. The resulting entry is linked onto the 424 * appropriate free list. 425 */ 426 void 427 freename(name) 428 char *name; 429 { 430 struct strhdr *tp, *np; 431 432 tp = &strtblhdr[strlen(name) / STRTBLINCR]; 433 np = (struct strhdr *)name; 434 np->next = tp->next; 435 tp->next = np; 436 } 437 438 /* 439 * Useful quantities placed at the end of a dumped symbol table. 440 */ 441 struct symtableheader { 442 long volno; 443 long stringsize; 444 long entrytblsize; 445 time_t dumptime; 446 time_t dumpdate; 447 ino_t maxino; 448 long ntrec; 449 }; 450 451 /* 452 * dump a snapshot of the symbol table 453 */ 454 void 455 dumpsymtable(filename, checkpt) 456 char *filename; 457 long checkpt; 458 { 459 register struct entry *ep, *tep; 460 register ino_t i; 461 struct entry temp, *tentry; 462 long mynum = 1, stroff = 0; 463 FILE *fd; 464 struct symtableheader hdr; 465 466 vprintf(stdout, "Check pointing the restore\n"); 467 if (Nflag) 468 return; 469 if ((fd = fopen(filename, "w")) == NULL) { 470 fprintf(stderr, "fopen: %s\n", strerror(errno)); 471 panic("cannot create save file %s for symbol table\n", 472 filename); 473 } 474 clearerr(fd); 475 /* 476 * Assign indicies to each entry 477 * Write out the string entries 478 */ 479 for (i = WINO; i <= maxino; i++) { 480 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 481 ep->e_index = mynum++; 482 (void) fwrite(ep->e_name, sizeof(char), 483 (int)allocsize(ep->e_namlen), fd); 484 } 485 } 486 /* 487 * Convert pointers to indexes, and output 488 */ 489 tep = &temp; 490 stroff = 0; 491 for (i = WINO; i <= maxino; i++) { 492 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 493 memcpy(tep, ep, (long)sizeof(struct entry)); 494 tep->e_name = (char *)stroff; 495 stroff += allocsize(ep->e_namlen); 496 tep->e_parent = (struct entry *)ep->e_parent->e_index; 497 if (ep->e_links != NULL) 498 tep->e_links = 499 (struct entry *)ep->e_links->e_index; 500 if (ep->e_sibling != NULL) 501 tep->e_sibling = 502 (struct entry *)ep->e_sibling->e_index; 503 if (ep->e_entries != NULL) 504 tep->e_entries = 505 (struct entry *)ep->e_entries->e_index; 506 if (ep->e_next != NULL) 507 tep->e_next = 508 (struct entry *)ep->e_next->e_index; 509 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd); 510 } 511 } 512 /* 513 * Convert entry pointers to indexes, and output 514 */ 515 for (i = 0; i < entrytblsize; i++) { 516 if (entry[i] == NULL) 517 tentry = NULL; 518 else 519 tentry = (struct entry *)entry[i]->e_index; 520 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd); 521 } 522 hdr.volno = checkpt; 523 hdr.maxino = maxino; 524 hdr.entrytblsize = entrytblsize; 525 hdr.stringsize = stroff; 526 hdr.dumptime = dumptime; 527 hdr.dumpdate = dumpdate; 528 hdr.ntrec = ntrec; 529 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd); 530 if (ferror(fd)) { 531 fprintf(stderr, "fwrite: %s\n", strerror(errno)); 532 panic("output error to file %s writing symbol table\n", 533 filename); 534 } 535 (void) fclose(fd); 536 } 537 538 /* 539 * Initialize a symbol table from a file 540 */ 541 void 542 initsymtable(filename) 543 char *filename; 544 { 545 char *base; 546 long tblsize; 547 register struct entry *ep; 548 struct entry *baseep, *lep; 549 struct symtableheader hdr; 550 struct stat stbuf; 551 register long i; 552 int fd; 553 554 vprintf(stdout, "Initialize symbol table.\n"); 555 if (filename == NULL) { 556 entrytblsize = maxino / HASHFACTOR; 557 entry = (struct entry **) 558 calloc((unsigned)entrytblsize, sizeof(struct entry *)); 559 if (entry == (struct entry **)NULL) 560 panic("no memory for entry table\n"); 561 ep = addentry(".", ROOTINO, NODE); 562 ep->e_flags |= NEW; 563 return; 564 } 565 if ((fd = open(filename, O_RDONLY, 0)) < 0) { 566 fprintf(stderr, "open: %s\n", strerror(errno)); 567 panic("cannot open symbol table file %s\n", filename); 568 } 569 if (fstat(fd, &stbuf) < 0) { 570 fprintf(stderr, "stat: %s\n", strerror(errno)); 571 panic("cannot stat symbol table file %s\n", filename); 572 } 573 tblsize = stbuf.st_size - sizeof(struct symtableheader); 574 base = calloc(sizeof(char), (unsigned)tblsize); 575 if (base == NULL) 576 panic("cannot allocate space for symbol table\n"); 577 if (read(fd, base, (int)tblsize) < 0 || 578 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { 579 fprintf(stderr, "read: %s\n", strerror(errno)); 580 panic("cannot read symbol table file %s\n", filename); 581 } 582 switch (command) { 583 case 'r': 584 /* 585 * For normal continuation, insure that we are using 586 * the next incremental tape 587 */ 588 if (hdr.dumpdate != dumptime) { 589 if (hdr.dumpdate < dumptime) 590 fprintf(stderr, "Incremental tape too low\n"); 591 else 592 fprintf(stderr, "Incremental tape too high\n"); 593 exit(1); 594 } 595 break; 596 case 'R': 597 /* 598 * For restart, insure that we are using the same tape 599 */ 600 curfile.action = SKIP; 601 dumptime = hdr.dumptime; 602 dumpdate = hdr.dumpdate; 603 if (!bflag) 604 newtapebuf(hdr.ntrec); 605 getvol(hdr.volno); 606 break; 607 default: 608 panic("initsymtable called from command %c\n", command); 609 break; 610 } 611 maxino = hdr.maxino; 612 entrytblsize = hdr.entrytblsize; 613 entry = (struct entry **) 614 (base + tblsize - (entrytblsize * sizeof(struct entry *))); 615 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry)); 616 lep = (struct entry *)entry; 617 for (i = 0; i < entrytblsize; i++) { 618 if (entry[i] == NULL) 619 continue; 620 entry[i] = &baseep[(long)entry[i]]; 621 } 622 for (ep = &baseep[1]; ep < lep; ep++) { 623 ep->e_name = base + (long)ep->e_name; 624 ep->e_parent = &baseep[(long)ep->e_parent]; 625 if (ep->e_sibling != NULL) 626 ep->e_sibling = &baseep[(long)ep->e_sibling]; 627 if (ep->e_links != NULL) 628 ep->e_links = &baseep[(long)ep->e_links]; 629 if (ep->e_entries != NULL) 630 ep->e_entries = &baseep[(long)ep->e_entries]; 631 if (ep->e_next != NULL) 632 ep->e_next = &baseep[(long)ep->e_next]; 633 } 634 } 635