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