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
35 static char sccsid[] = "@(#)symtab.c 8.3 (Berkeley) 4/28/95";
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".
47 #include <sys/param.h>
50 #include <ufs/ufs/dinode.h>
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).
70 static struct entry **entry;
71 static long entrytblsize;
73 static void addino __P((ino_t, struct entry *));
74 static struct entry *lookupparent __P((char *));
75 static void removeentry __P((struct entry *));
78 * Look up an entry by inode number
84 register struct entry *ep;
86 if (inum < WINO || inum >= maxino)
88 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
89 if (ep->e_ino == inum)
95 * Add an entry into the entry table
104 if (inum < WINO || inum >= maxino)
105 panic("addino: out of range %d\n", inum);
106 epp = &entry[inum % entrytblsize];
111 for (np = np->e_next; np != NULL; np = np->e_next)
112 if (np->e_ino == inum)
113 badentry(np, "duplicate inum");
117 * Delete an entry from the entry table
123 register struct entry *next;
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) {
132 *prev = next->e_next;
135 prev = &next->e_next;
137 panic("deleteino: %d not found\n", inum);
141 * Look up an entry by name
147 register struct entry *ep;
148 register char *np, *cp;
149 char buf[MAXPATHLEN];
152 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
153 for (np = buf; *cp != '/' && *cp != '\0' &&
154 np < &buf[sizeof(buf)]; )
156 if (np == &buf[sizeof(buf)])
159 for ( ; ep != NULL; ep = ep->e_sibling)
160 if (strcmp(ep->e_name, buf) == 0)
171 * Look up the parent of a pathname
173 static struct entry *
180 tailindex = strrchr(name, '/');
181 if (tailindex == NULL)
184 ep = lookupname(name);
188 if (ep->e_type != NODE)
189 panic("%s is not a directory\n", name);
194 * Determine the current pathname of a node or leaf
198 register struct entry *ep;
201 static char namebuf[MAXPATHLEN];
203 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
205 memmove(cp, ep->e_name, (long)ep->e_namlen);
206 if (ep == lookupino(ROOTINO))
211 panic("%s: pathname too long\n", cp);
216 * Unused symbol table entries are linked together on a freelist
217 * headed by the following pointer.
219 static struct entry *freelist = NULL;
222 * add an entry to the symbol table
225 addentry(name, inum, type)
230 register struct entry *np, *ep;
232 if (freelist != NULL) {
234 freelist = np->e_next;
235 memset(np, 0, (long)sizeof(struct entry));
237 np = (struct entry *)calloc(1, sizeof(struct entry));
239 panic("no memory to extend symbol table\n");
241 np->e_type = type & ~LINK;
242 ep = lookupparent(name);
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);
252 np->e_name = savename(strrchr(name, '/') + 1);
253 np->e_namlen = strlen(np->e_name);
255 np->e_sibling = ep->e_entries;
258 ep = lookupino(inum);
260 panic("link to non-existant name\n");
262 np->e_links = ep->e_links;
264 } else if (inum != 0) {
265 if (lookupino(inum) != NULL)
266 panic("duplicate entry\n");
273 * delete an entry from the symbol table
277 register struct entry *ep;
279 register struct entry *np;
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");
290 if (ep->e_ino != 0) {
291 np = lookupino(ep->e_ino);
293 badentry(ep, "lookupino failed");
297 if (ep->e_links != NULL)
298 addino(inum, ep->e_links);
300 for (; np != NULL; np = np->e_links) {
301 if (np->e_links == ep) {
302 np->e_links = ep->e_links;
307 badentry(ep, "link not found");
311 freename(ep->e_name);
312 ep->e_next = freelist;
317 * Relocate an entry in the tree structure
320 moveentry(ep, newname)
321 register struct entry *ep;
327 np = lookupparent(newname);
329 badentry(ep, "cannot move ROOT");
330 if (np != ep->e_parent) {
333 ep->e_sibling = np->e_entries;
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;
343 ep->e_flags &= ~TMPNAME;
347 * Remove an entry in the tree structure
351 register struct entry *ep;
353 register struct entry *np;
356 if (np->e_entries == ep) {
357 np->e_entries = ep->e_sibling;
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;
366 badentry(ep, "cannot find entry in parent list");
371 * Table of unused string entries, sorted by length.
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 * apprpriate free list.
379 * NB. The macro "allocsize" below assumes that "struct strhdr"
380 * has a size that is a power of two.
386 #define STRTBLINCR (sizeof(struct strhdr))
387 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
389 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
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.
406 np = strtblhdr[len / STRTBLINCR].next;
408 strtblhdr[len / STRTBLINCR].next = np->next;
411 cp = malloc((unsigned)allocsize(len));
413 panic("no space for string table\n");
415 (void) strcpy(cp, name);
420 * Free space for a name. The resulting entry is linked onto the
421 * appropriate free list.
427 struct strhdr *tp, *np;
429 tp = &strtblhdr[strlen(name) / STRTBLINCR];
430 np = (struct strhdr *)name;
436 * Useful quantities placed at the end of a dumped symbol table.
438 struct symtableheader {
449 * dump a snapshot of the symbol table
452 dumpsymtable(filename, checkpt)
456 register struct entry *ep, *tep;
458 struct entry temp, *tentry;
459 long mynum = 1, stroff = 0;
461 struct symtableheader hdr;
463 vprintf(stdout, "Check pointing the restore\n");
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",
473 * Assign indicies to each entry
474 * Write out the string entries
476 for (i = WINO; i <= maxino; i++) {
477 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
478 ep->e_index = mynum++;
479 (void) fwrite(ep->e_name, sizeof(char),
480 (int)allocsize(ep->e_namlen), fd);
484 * Convert pointers to indexes, and output
488 for (i = WINO; i <= maxino; i++) {
489 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
490 memmove(tep, ep, (long)sizeof(struct entry));
491 tep->e_name = (char *)stroff;
492 stroff += allocsize(ep->e_namlen);
493 tep->e_parent = (struct entry *)ep->e_parent->e_index;
494 if (ep->e_links != NULL)
496 (struct entry *)ep->e_links->e_index;
497 if (ep->e_sibling != NULL)
499 (struct entry *)ep->e_sibling->e_index;
500 if (ep->e_entries != NULL)
502 (struct entry *)ep->e_entries->e_index;
503 if (ep->e_next != NULL)
505 (struct entry *)ep->e_next->e_index;
506 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
510 * Convert entry pointers to indexes, and output
512 for (i = 0; i < entrytblsize; i++) {
513 if (entry[i] == NULL)
516 tentry = (struct entry *)entry[i]->e_index;
517 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
521 hdr.entrytblsize = entrytblsize;
522 hdr.stringsize = stroff;
523 hdr.dumptime = dumptime;
524 hdr.dumpdate = dumpdate;
526 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
528 fprintf(stderr, "fwrite: %s\n", strerror(errno));
529 panic("output error to file %s writing symbol table\n",
536 * Initialize a symbol table from a file
539 initsymtable(filename)
544 register struct entry *ep;
545 struct entry *baseep, *lep;
546 struct symtableheader hdr;
551 vprintf(stdout, "Initialize symbol table.\n");
552 if (filename == NULL) {
553 entrytblsize = maxino / HASHFACTOR;
554 entry = (struct entry **)
555 calloc((unsigned)entrytblsize, sizeof(struct entry *));
556 if (entry == (struct entry **)NULL)
557 panic("no memory for entry table\n");
558 ep = addentry(".", ROOTINO, NODE);
562 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
563 fprintf(stderr, "open: %s\n", strerror(errno));
564 panic("cannot open symbol table file %s\n", filename);
566 if (fstat(fd, &stbuf) < 0) {
567 fprintf(stderr, "stat: %s\n", strerror(errno));
568 panic("cannot stat symbol table file %s\n", filename);
570 tblsize = stbuf.st_size - sizeof(struct symtableheader);
571 base = calloc(sizeof(char), (unsigned)tblsize);
573 panic("cannot allocate space for symbol table\n");
574 if (read(fd, base, (int)tblsize) < 0 ||
575 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
576 fprintf(stderr, "read: %s\n", strerror(errno));
577 panic("cannot read symbol table file %s\n", filename);
582 * For normal continuation, insure that we are using
583 * the next incremental tape
585 if (hdr.dumpdate != dumptime) {
586 if (hdr.dumpdate < dumptime)
587 fprintf(stderr, "Incremental tape too low\n");
589 fprintf(stderr, "Incremental tape too high\n");
595 * For restart, insure that we are using the same tape
597 curfile.action = SKIP;
598 dumptime = hdr.dumptime;
599 dumpdate = hdr.dumpdate;
601 newtapebuf(hdr.ntrec);
605 panic("initsymtable called from command %c\n", command);
609 entrytblsize = hdr.entrytblsize;
610 entry = (struct entry **)
611 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
612 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
613 lep = (struct entry *)entry;
614 for (i = 0; i < entrytblsize; i++) {
615 if (entry[i] == NULL)
617 entry[i] = &baseep[(long)entry[i]];
619 for (ep = &baseep[1]; ep < lep; ep++) {
620 ep->e_name = base + (long)ep->e_name;
621 ep->e_parent = &baseep[(long)ep->e_parent];
622 if (ep->e_sibling != NULL)
623 ep->e_sibling = &baseep[(long)ep->e_sibling];
624 if (ep->e_links != NULL)
625 ep->e_links = &baseep[(long)ep->e_links];
626 if (ep->e_entries != NULL)
627 ep->e_entries = &baseep[(long)ep->e_entries];
628 if (ep->e_next != NULL)
629 ep->e_next = &baseep[(long)ep->e_next];