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