1 /* $OpenBSD: symtab.c,v 1.17 2006/10/18 21:18:59 deraadt Exp $ */ 2 /* $NetBSD: symtab.c,v 1.10 1997/03/19 08:42:54 lukem Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)symtab.c 8.2 (Berkeley) 9/13/94"; 36 #else 37 static const char rcsid[] = "$OpenBSD: symtab.c,v 1.17 2006/10/18 21:18:59 deraadt 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 <err.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(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 < ROOTINO || 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 < ROOTINO || 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 < ROOTINO || 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(char *name) 144 { 145 struct entry *ep; 146 char *np, *cp; 147 char buf[MAXPATHLEN]; 148 149 cp = name; 150 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { 151 for (np = buf; 152 *cp != '/' && *cp != '\0' && np < &buf[sizeof(buf)]; ) 153 *np++ = *cp++; 154 if (np == &buf[sizeof(buf)]) 155 break; 156 *np = '\0'; 157 for ( ; ep != NULL; ep = ep->e_sibling) 158 if (strcmp(ep->e_name, buf) == 0) 159 break; 160 if (ep == NULL) 161 break; 162 if (*cp++ == '\0') 163 return (ep); 164 } 165 return (NULL); 166 } 167 168 /* 169 * Look up the parent of a pathname 170 */ 171 static struct entry * 172 lookupparent(char *name) 173 { 174 struct entry *ep; 175 char *tailindex; 176 177 tailindex = strrchr(name, '/'); 178 if (tailindex == NULL) 179 return (NULL); 180 *tailindex = '\0'; 181 ep = lookupname(name); 182 *tailindex = '/'; 183 if (ep == NULL) 184 return (NULL); 185 if (ep->e_type != NODE) 186 panic("%s is not a directory\n", name); 187 return (ep); 188 } 189 190 /* 191 * Determine the current pathname of a node or leaf 192 */ 193 char * 194 myname(struct entry *ep) 195 { 196 char *cp; 197 static char namebuf[MAXPATHLEN]; 198 199 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { 200 cp -= ep->e_namlen; 201 memcpy(cp, ep->e_name, (long)ep->e_namlen); 202 if (ep == lookupino(ROOTINO)) 203 return (cp); 204 *(--cp) = '/'; 205 ep = ep->e_parent; 206 } 207 panic("%s: pathname too long\n", cp); 208 return(cp); 209 } 210 211 /* 212 * Unused symbol table entries are linked together on a freelist 213 * headed by the following pointer. 214 */ 215 static struct entry *freelist = NULL; 216 217 /* 218 * add an entry to the symbol table 219 */ 220 struct entry * 221 addentry(char *name, ino_t inum, int type) 222 { 223 struct entry *np, *ep; 224 225 if (freelist != NULL) { 226 np = freelist; 227 freelist = np->e_next; 228 memset(np, 0, (long)sizeof(struct entry)); 229 } else { 230 np = (struct entry *)calloc(1, sizeof(struct entry)); 231 if (np == NULL) 232 panic("no memory to extend symbol table\n"); 233 } 234 np->e_type = type & ~LINK; 235 ep = lookupparent(name); 236 if (ep == NULL) { 237 if (inum != ROOTINO || lookupino(ROOTINO) != NULL) 238 panic("bad name to addentry %s\n", name); 239 np->e_name = savename(name); 240 np->e_namlen = strlen(name); 241 np->e_parent = np; 242 addino(ROOTINO, np); 243 return (np); 244 } 245 np->e_name = savename(strrchr(name, '/') + 1); 246 np->e_namlen = strlen(np->e_name); 247 np->e_parent = ep; 248 np->e_sibling = ep->e_entries; 249 ep->e_entries = np; 250 if (type & LINK) { 251 ep = lookupino(inum); 252 if (ep == NULL) 253 panic("link to non-existent name\n"); 254 np->e_ino = inum; 255 np->e_links = ep->e_links; 256 ep->e_links = np; 257 } else if (inum != 0) { 258 if (lookupino(inum) != NULL) 259 panic("duplicate entry\n"); 260 addino(inum, np); 261 } 262 return (np); 263 } 264 265 /* 266 * delete an entry from the symbol table 267 */ 268 void 269 freeentry(struct entry *ep) 270 { 271 struct entry *np; 272 ino_t inum; 273 274 if (ep->e_flags != REMOVED) 275 badentry(ep, "not marked REMOVED"); 276 if (ep->e_type == NODE) { 277 if (ep->e_links != NULL) 278 badentry(ep, "freeing referenced directory"); 279 if (ep->e_entries != NULL) 280 badentry(ep, "freeing non-empty directory"); 281 } 282 if (ep->e_ino != 0) { 283 np = lookupino(ep->e_ino); 284 if (np == NULL) 285 badentry(ep, "lookupino failed"); 286 if (np == ep) { 287 inum = ep->e_ino; 288 deleteino(inum); 289 if (ep->e_links != NULL) 290 addino(inum, ep->e_links); 291 } else { 292 for (; np != NULL; np = np->e_links) { 293 if (np->e_links == ep) { 294 np->e_links = ep->e_links; 295 break; 296 } 297 } 298 if (np == NULL) 299 badentry(ep, "link not found"); 300 } 301 } 302 removeentry(ep); 303 freename(ep->e_name); 304 ep->e_next = freelist; 305 freelist = ep; 306 } 307 308 /* 309 * Relocate an entry in the tree structure 310 */ 311 void 312 moveentry(struct entry *ep, char *newname) 313 { 314 struct entry *np; 315 char *cp; 316 317 np = lookupparent(newname); 318 if (np == NULL) 319 badentry(ep, "cannot move ROOT"); 320 if (np != ep->e_parent) { 321 removeentry(ep); 322 ep->e_parent = np; 323 ep->e_sibling = np->e_entries; 324 np->e_entries = ep; 325 } 326 cp = strrchr(newname, '/') + 1; 327 freename(ep->e_name); 328 ep->e_name = savename(cp); 329 ep->e_namlen = strlen(cp); 330 if (strcmp(gentempname(ep), ep->e_name) == 0) 331 ep->e_flags |= TMPNAME; 332 else 333 ep->e_flags &= ~TMPNAME; 334 } 335 336 /* 337 * Remove an entry in the tree structure 338 */ 339 static void 340 removeentry(struct entry *ep) 341 { 342 struct entry *np; 343 344 np = ep->e_parent; 345 if (np->e_entries == ep) { 346 np->e_entries = ep->e_sibling; 347 } else { 348 for (np = np->e_entries; np != NULL; np = np->e_sibling) { 349 if (np->e_sibling == ep) { 350 np->e_sibling = ep->e_sibling; 351 break; 352 } 353 } 354 if (np == NULL) 355 badentry(ep, "cannot find entry in parent list"); 356 } 357 } 358 359 /* 360 * Table of unused string entries, sorted by length. 361 * 362 * Entries are allocated in STRTBLINCR sized pieces so that names 363 * of similar lengths can use the same entry. The value of STRTBLINCR 364 * is chosen so that every entry has at least enough space to hold 365 * a "struct strtbl" header. Thus every entry can be linked onto an 366 * apprpriate free list. 367 * 368 * NB. The macro "allocsize" below assumes that "struct strhdr" 369 * has a size that is a power of two. 370 */ 371 struct strhdr { 372 struct strhdr *next; 373 }; 374 375 #define STRTBLINCR (sizeof(struct strhdr)) 376 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1)) 377 378 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR]; 379 380 /* 381 * Allocate space for a name. It first looks to see if it already 382 * has an appropriate sized entry, and if not allocates a new one. 383 */ 384 char * 385 savename(char *name) 386 { 387 struct strhdr *np; 388 long len; 389 char *cp; 390 391 if (name == NULL) 392 panic("bad name\n"); 393 len = strlen(name); 394 np = strtblhdr[len / STRTBLINCR].next; 395 if (np != NULL) { 396 strtblhdr[len / STRTBLINCR].next = np->next; 397 cp = (char *)np; 398 } else { 399 cp = malloc((unsigned)allocsize(len)); 400 if (cp == NULL) 401 panic("no space for string table\n"); 402 } 403 (void)strlcpy(cp, name, len + 1); 404 return (cp); 405 } 406 407 /* 408 * Free space for a name. The resulting entry is linked onto the 409 * appropriate free list. 410 */ 411 void 412 freename(char *name) 413 { 414 struct strhdr *tp, *np; 415 416 tp = &strtblhdr[strlen(name) / STRTBLINCR]; 417 np = (struct strhdr *)name; 418 np->next = tp->next; 419 tp->next = np; 420 } 421 422 /* 423 * Useful quantities placed at the end of a dumped symbol table. 424 */ 425 struct symtableheader { 426 int32_t volno; 427 int32_t stringsize; 428 int32_t entrytblsize; 429 time_t dumptime; 430 time_t dumpdate; 431 ino_t maxino; 432 int32_t ntrec; 433 }; 434 435 /* 436 * dump a snapshot of the symbol table 437 */ 438 void 439 dumpsymtable(char *filename, long checkpt) 440 { 441 struct entry *ep, *tep; 442 ino_t i; 443 struct entry temp, *tentry; 444 long mynum = 1, stroff = 0; 445 FILE *fp; 446 struct symtableheader hdr; 447 448 Vprintf(stdout, "Check pointing the restore\n"); 449 if (Nflag) 450 return; 451 if ((fp = fopen(filename, "w")) == NULL) { 452 warn("fopen"); 453 panic("cannot create save file %s for symbol table\n", 454 filename); 455 } 456 clearerr(fp); 457 /* 458 * Assign indices to each entry 459 * Write out the string entries 460 */ 461 for (i = ROOTINO; i <= maxino; i++) { 462 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 463 ep->e_index = mynum++; 464 (void)fwrite(ep->e_name, sizeof(char), 465 (int)allocsize(ep->e_namlen), fp); 466 } 467 } 468 /* 469 * Convert pointers to indexes, and output 470 */ 471 tep = &temp; 472 stroff = 0; 473 for (i = ROOTINO; i <= maxino; i++) { 474 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 475 memcpy(tep, ep, (long)sizeof(struct entry)); 476 tep->e_name = (char *)stroff; 477 stroff += allocsize(ep->e_namlen); 478 tep->e_parent = (struct entry *)ep->e_parent->e_index; 479 if (ep->e_links != NULL) 480 tep->e_links = 481 (struct entry *)ep->e_links->e_index; 482 if (ep->e_sibling != NULL) 483 tep->e_sibling = 484 (struct entry *)ep->e_sibling->e_index; 485 if (ep->e_entries != NULL) 486 tep->e_entries = 487 (struct entry *)ep->e_entries->e_index; 488 if (ep->e_next != NULL) 489 tep->e_next = 490 (struct entry *)ep->e_next->e_index; 491 (void)fwrite((char *)tep, sizeof(struct entry), 1, fp); 492 } 493 } 494 /* 495 * Convert entry pointers to indexes, and output 496 */ 497 for (i = 0; i < entrytblsize; i++) { 498 if (entry[i] == NULL) 499 tentry = NULL; 500 else 501 tentry = (struct entry *)entry[i]->e_index; 502 (void)fwrite((char *)&tentry, sizeof(struct entry *), 1, fp); 503 } 504 hdr.volno = checkpt; 505 hdr.maxino = maxino; 506 hdr.entrytblsize = entrytblsize; 507 hdr.stringsize = stroff; 508 hdr.dumptime = dumptime; 509 hdr.dumpdate = dumpdate; 510 hdr.ntrec = ntrec; 511 (void)fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fp); 512 if (ferror(fp)) { 513 warn("fwrite"); 514 panic("output error to file %s writing symbol table\n", 515 filename); 516 } 517 (void)fclose(fp); 518 } 519 520 /* 521 * Initialize a symbol table from a file 522 */ 523 void 524 initsymtable(char *filename) 525 { 526 char *base; 527 long tblsize; 528 struct entry *ep; 529 struct entry *baseep, *lep; 530 struct symtableheader hdr; 531 struct stat stbuf; 532 long i; 533 int fd; 534 535 Vprintf(stdout, "Initialize symbol table.\n"); 536 if (filename == NULL) { 537 entrytblsize = maxino / HASHFACTOR; 538 entry = (struct entry **) 539 calloc((unsigned)entrytblsize, sizeof(struct entry *)); 540 if (entry == (struct entry **)NULL) 541 panic("no memory for entry table\n"); 542 ep = addentry(".", ROOTINO, NODE); 543 ep->e_flags |= NEW; 544 return; 545 } 546 if ((fd = open(filename, O_RDONLY, 0)) < 0) { 547 warn("open"); 548 panic("cannot open symbol table file %s\n", filename); 549 } 550 if (fstat(fd, &stbuf) < 0) { 551 warn("stat"); 552 panic("cannot stat symbol table file %s\n", filename); 553 } 554 tblsize = stbuf.st_size - sizeof(struct symtableheader); 555 base = calloc((unsigned)tblsize, sizeof(char)); 556 if (base == NULL) 557 panic("cannot allocate space for symbol table\n"); 558 if (read(fd, base, (int)tblsize) < 0 || 559 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { 560 warn("read"); 561 panic("cannot read symbol table file %s\n", filename); 562 } 563 switch (command) { 564 case 'r': 565 /* 566 * For normal continuation, insure that we are using 567 * the next incremental tape 568 */ 569 if (hdr.dumpdate != dumptime) 570 errx(1, "Incremental tape too %s", 571 (hdr.dumpdate < dumptime) ? "low" : "high"); 572 break; 573 case 'R': 574 /* 575 * For restart, insure that we are using the same tape 576 */ 577 curfile.action = SKIP; 578 dumptime = hdr.dumptime; 579 dumpdate = hdr.dumpdate; 580 if (!bflag) 581 newtapebuf(hdr.ntrec); 582 getvol(hdr.volno); 583 break; 584 default: 585 panic("initsymtable called from command %c\n", command); 586 break; 587 } 588 maxino = hdr.maxino; 589 entrytblsize = hdr.entrytblsize; 590 entry = (struct entry **) 591 (base + tblsize - (entrytblsize * sizeof(struct entry *))); 592 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry)); 593 lep = (struct entry *)entry; 594 for (i = 0; i < entrytblsize; i++) { 595 if (entry[i] == NULL) 596 continue; 597 entry[i] = &baseep[(long)entry[i]]; 598 } 599 for (ep = &baseep[1]; ep < lep; ep++) { 600 ep->e_name = base + (long)ep->e_name; 601 ep->e_parent = &baseep[(long)ep->e_parent]; 602 if (ep->e_sibling != NULL) 603 ep->e_sibling = &baseep[(long)ep->e_sibling]; 604 if (ep->e_links != NULL) 605 ep->e_links = &baseep[(long)ep->e_links]; 606 if (ep->e_entries != NULL) 607 ep->e_entries = &baseep[(long)ep->e_entries]; 608 if (ep->e_next != NULL) 609 ep->e_next = &baseep[(long)ep->e_next]; 610 } 611 } 612