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