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