2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)symtab.c 8.3 (Berkeley) 4/28/95";
38 static const char rcsid[] =
39 "$FreeBSD: src/sbin/restore/symtab.c,v 1.7.2.1 2001/12/19 14:54:14 tobez Exp $";
43 * These routines maintain the symbol table which tracks the state
44 * of the file system being restored. They provide lookup by either
45 * name or inode number. They also provide for creation, deletion,
46 * and renaming of entries. Because of the dynamic nature of pathnames,
47 * names should not be saved, but always constructed just before they
48 * are needed, by calling "myname".
51 #include <sys/param.h>
54 #include <ufs/ufs/dinode.h>
67 * The following variables define the inode symbol table.
68 * The primary hash table is dynamically allocated based on
69 * the number of inodes in the file system (maxino), scaled by
70 * HASHFACTOR. The variable "entry" points to the hash table;
71 * the variable "entrytblsize" indicates its size (in entries).
74 static struct entry **entry;
75 static long entrytblsize;
77 static void addino __P((ino_t, struct entry *));
78 static struct entry *lookupparent __P((char *));
79 static void removeentry __P((struct entry *));
82 * Look up an entry by inode number
88 register struct entry *ep;
90 if (inum < WINO || inum >= maxino)
92 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
93 if (ep->e_ino == inum)
99 * Add an entry into the entry table
108 if (inum < WINO || inum >= maxino)
109 panic("addino: out of range %d\n", inum);
110 epp = &entry[inum % entrytblsize];
115 for (np = np->e_next; np != NULL; np = np->e_next)
116 if (np->e_ino == inum)
117 badentry(np, "duplicate inum");
121 * Delete an entry from the entry table
127 register struct entry *next;
130 if (inum < WINO || inum >= maxino)
131 panic("deleteino: out of range %d\n", inum);
132 prev = &entry[inum % entrytblsize];
133 for (next = *prev; next != NULL; next = next->e_next) {
134 if (next->e_ino == inum) {
136 *prev = next->e_next;
139 prev = &next->e_next;
141 panic("deleteino: %d not found\n", inum);
145 * Look up an entry by name
151 register struct entry *ep;
152 register char *np, *cp;
153 char buf[MAXPATHLEN];
156 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
157 for (np = buf; *cp != '/' && *cp != '\0' &&
158 np < &buf[sizeof(buf)]; )
160 if (np == &buf[sizeof(buf)])
163 for ( ; ep != NULL; ep = ep->e_sibling)
164 if (strcmp(ep->e_name, buf) == 0)
175 * Look up the parent of a pathname
177 static struct entry *
184 tailindex = strrchr(name, '/');
185 if (tailindex == NULL)
188 ep = lookupname(name);
192 if (ep->e_type != NODE)
193 panic("%s is not a directory\n", name);
198 * Determine the current pathname of a node or leaf
202 register struct entry *ep;
205 static char namebuf[MAXPATHLEN];
207 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
209 memmove(cp, ep->e_name, (long)ep->e_namlen);
210 if (ep == lookupino(ROOTINO))
215 panic("%s: pathname too long\n", cp);
220 * Unused symbol table entries are linked together on a free list
221 * headed by the following pointer.
223 static struct entry *freelist = NULL;
226 * add an entry to the symbol table
229 addentry(name, inum, type)
234 register struct entry *np, *ep;
236 if (freelist != NULL) {
238 freelist = np->e_next;
239 memset(np, 0, (long)sizeof(struct entry));
241 np = (struct entry *)calloc(1, sizeof(struct entry));
243 panic("no memory to extend symbol table\n");
245 np->e_type = type & ~LINK;
246 ep = lookupparent(name);
248 if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
249 panic("bad name to addentry %s\n", name);
250 np->e_name = savename(name);
251 np->e_namlen = strlen(name);
256 np->e_name = savename(strrchr(name, '/') + 1);
257 np->e_namlen = strlen(np->e_name);
259 np->e_sibling = ep->e_entries;
262 ep = lookupino(inum);
264 panic("link to non-existent name\n");
266 np->e_links = ep->e_links;
268 } else if (inum != 0) {
269 if (lookupino(inum) != NULL)
270 panic("duplicate entry\n");
277 * delete an entry from the symbol table
281 register struct entry *ep;
283 register struct entry *np;
286 if (ep->e_flags != REMOVED)
287 badentry(ep, "not marked REMOVED");
288 if (ep->e_type == NODE) {
289 if (ep->e_links != NULL)
290 badentry(ep, "freeing referenced directory");
291 if (ep->e_entries != NULL)
292 badentry(ep, "freeing non-empty directory");
294 if (ep->e_ino != 0) {
295 np = lookupino(ep->e_ino);
297 badentry(ep, "lookupino failed");
301 if (ep->e_links != NULL)
302 addino(inum, ep->e_links);
304 for (; np != NULL; np = np->e_links) {
305 if (np->e_links == ep) {
306 np->e_links = ep->e_links;
311 badentry(ep, "link not found");
315 freename(ep->e_name);
316 ep->e_next = freelist;
321 * Relocate an entry in the tree structure
324 moveentry(ep, newname)
325 register struct entry *ep;
331 np = lookupparent(newname);
333 badentry(ep, "cannot move ROOT");
334 if (np != ep->e_parent) {
337 ep->e_sibling = np->e_entries;
340 cp = strrchr(newname, '/') + 1;
341 freename(ep->e_name);
342 ep->e_name = savename(cp);
343 ep->e_namlen = strlen(cp);
344 if (strcmp(gentempname(ep), ep->e_name) == 0)
345 ep->e_flags |= TMPNAME;
347 ep->e_flags &= ~TMPNAME;
351 * Remove an entry in the tree structure
355 register struct entry *ep;
357 register struct entry *np;
360 if (np->e_entries == ep) {
361 np->e_entries = ep->e_sibling;
363 for (np = np->e_entries; np != NULL; np = np->e_sibling) {
364 if (np->e_sibling == ep) {
365 np->e_sibling = ep->e_sibling;
370 badentry(ep, "cannot find entry in parent list");
375 * Table of unused string entries, sorted by length.
377 * Entries are allocated in STRTBLINCR sized pieces so that names
378 * of similar lengths can use the same entry. The value of STRTBLINCR
379 * is chosen so that every entry has at least enough space to hold
380 * a "struct strtbl" header. Thus every entry can be linked onto an
381 * appropriate free list.
383 * NB. The macro "allocsize" below assumes that "struct strhdr"
384 * has a size that is a power of two.
390 #define STRTBLINCR (sizeof(struct strhdr))
391 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
393 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
396 * Allocate space for a name. It first looks to see if it already
397 * has an appropriate sized entry, and if not allocates a new one.
410 np = strtblhdr[len / STRTBLINCR].next;
412 strtblhdr[len / STRTBLINCR].next = np->next;
415 cp = malloc((unsigned)allocsize(len));
417 panic("no space for string table\n");
419 (void) strcpy(cp, name);
424 * Free space for a name. The resulting entry is linked onto the
425 * appropriate free list.
431 struct strhdr *tp, *np;
433 tp = &strtblhdr[strlen(name) / STRTBLINCR];
434 np = (struct strhdr *)name;
440 * Useful quantities placed at the end of a dumped symbol table.
442 struct symtableheader {
445 int32_t entrytblsize;
453 * dump a snapshot of the symbol table
456 dumpsymtable(filename, checkpt)
460 register struct entry *ep, *tep;
462 struct entry temp, *tentry;
463 long mynum = 1, stroff = 0;
465 struct symtableheader hdr;
467 vprintf(stdout, "Check pointing the restore\n");
470 if ((fd = fopen(filename, "w")) == NULL) {
471 fprintf(stderr, "fopen: %s\n", strerror(errno));
472 panic("cannot create save file %s for symbol table\n",
478 * Assign indices to each entry
479 * Write out the string entries
481 for (i = WINO; i <= maxino; i++) {
482 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
483 ep->e_index = mynum++;
484 (void) fwrite(ep->e_name, sizeof(char),
485 (int)allocsize(ep->e_namlen), fd);
489 * Convert pointers to indexes, and output
493 for (i = WINO; i <= maxino; i++) {
494 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
495 memmove(tep, ep, (long)sizeof(struct entry));
496 tep->e_name = (char *)stroff;
497 stroff += allocsize(ep->e_namlen);
498 tep->e_parent = (struct entry *)ep->e_parent->e_index;
499 if (ep->e_links != NULL)
501 (struct entry *)ep->e_links->e_index;
502 if (ep->e_sibling != NULL)
504 (struct entry *)ep->e_sibling->e_index;
505 if (ep->e_entries != NULL)
507 (struct entry *)ep->e_entries->e_index;
508 if (ep->e_next != NULL)
510 (struct entry *)ep->e_next->e_index;
511 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
515 * Convert entry pointers to indexes, and output
517 for (i = 0; i < entrytblsize; i++) {
518 if (entry[i] == NULL)
521 tentry = (struct entry *)entry[i]->e_index;
522 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
526 hdr.entrytblsize = entrytblsize;
527 hdr.stringsize = stroff;
528 hdr.dumptime = dumptime;
529 hdr.dumpdate = dumpdate;
531 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
533 fprintf(stderr, "fwrite: %s\n", strerror(errno));
534 panic("output error to file %s writing symbol table\n",
541 * Initialize a symbol table from a file
544 initsymtable(filename)
549 register struct entry *ep;
550 struct entry *baseep, *lep;
551 struct symtableheader hdr;
556 vprintf(stdout, "Initialize symbol table.\n");
557 if (filename == NULL) {
558 entrytblsize = maxino / HASHFACTOR;
559 entry = (struct entry **)
560 calloc((unsigned)entrytblsize, sizeof(struct entry *));
561 if (entry == (struct entry **)NULL)
562 panic("no memory for entry table\n");
563 ep = addentry(".", ROOTINO, NODE);
567 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
568 fprintf(stderr, "open: %s\n", strerror(errno));
569 panic("cannot open symbol table file %s\n", filename);
571 if (fstat(fd, &stbuf) < 0) {
572 fprintf(stderr, "stat: %s\n", strerror(errno));
573 panic("cannot stat symbol table file %s\n", filename);
575 tblsize = stbuf.st_size - sizeof(struct symtableheader);
576 base = calloc(sizeof(char), (unsigned)tblsize);
578 panic("cannot allocate space for symbol table\n");
579 if (read(fd, base, (int)tblsize) < 0 ||
580 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
581 fprintf(stderr, "read: %s\n", strerror(errno));
582 panic("cannot read symbol table file %s\n", filename);
587 * For normal continuation, insure that we are using
588 * the next incremental tape
590 if (hdr.dumpdate != dumptime) {
591 if (hdr.dumpdate < dumptime)
592 fprintf(stderr, "Incremental tape too low\n");
594 fprintf(stderr, "Incremental tape too high\n");
600 * For restart, insure that we are using the same tape
602 curfile.action = SKIP;
603 dumptime = hdr.dumptime;
604 dumpdate = hdr.dumpdate;
606 newtapebuf(hdr.ntrec);
610 panic("initsymtable called from command %c\n", command);
614 entrytblsize = hdr.entrytblsize;
615 entry = (struct entry **)
616 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
617 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
618 lep = (struct entry *)entry;
619 for (i = 0; i < entrytblsize; i++) {
620 if (entry[i] == NULL)
622 entry[i] = &baseep[(long)entry[i]];
624 for (ep = &baseep[1]; ep < lep; ep++) {
625 ep->e_name = base + (long)ep->e_name;
626 ep->e_parent = &baseep[(long)ep->e_parent];
627 if (ep->e_sibling != NULL)
628 ep->e_sibling = &baseep[(long)ep->e_sibling];
629 if (ep->e_links != NULL)
630 ep->e_links = &baseep[(long)ep->e_links];
631 if (ep->e_entries != NULL)
632 ep->e_entries = &baseep[(long)ep->e_entries];
633 if (ep->e_next != NULL)
634 ep->e_next = &baseep[(long)ep->e_next];