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