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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)symtab.c 8.3 (Berkeley) 4/28/95";
34 static const char rcsid[] =
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>
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).
71 static struct entry **entry;
72 static long entrytblsize;
74 static void addino(ino_t, struct entry *);
75 static struct entry *lookupparent(char *);
76 static void removeentry(struct entry *);
79 * Look up an entry by inode number
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
98 addino(ino_t inum, struct entry *np)
102 if (inum < WINO || inum >= maxino)
103 panic("addino: out of range %d\n", inum);
104 epp = &entry[inum % entrytblsize];
109 for (np = np->e_next; np != NULL; np = np->e_next)
110 if (np->e_ino == inum)
111 badentry(np, "duplicate inum");
115 * Delete an entry from the entry table
118 deleteino(ino_t inum)
123 if (inum < WINO || inum >= maxino)
124 panic("deleteino: out of range %d\n", inum);
125 prev = &entry[inum % entrytblsize];
126 for (next = *prev; next != NULL; next = next->e_next) {
127 if (next->e_ino == inum) {
129 *prev = next->e_next;
132 prev = &next->e_next;
134 panic("deleteino: %d not found\n", inum);
138 * Look up an entry by name
141 lookupname(char *name)
145 char buf[MAXPATHLEN];
148 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
149 for (np = buf; *cp != '/' && *cp != '\0' &&
150 np < &buf[sizeof(buf)]; )
152 if (np == &buf[sizeof(buf)])
155 for ( ; ep != NULL; ep = ep->e_sibling)
156 if (strcmp(ep->e_name, buf) == 0)
167 * Look up the parent of a pathname
169 static struct entry *
170 lookupparent(char *name)
175 tailindex = strrchr(name, '/');
176 if (tailindex == NULL)
179 ep = lookupname(name);
183 if (ep->e_type != NODE)
184 panic("%s is not a directory\n", name);
189 * Determine the current pathname of a node or leaf
192 myname(struct entry *ep)
195 static char namebuf[MAXPATHLEN];
197 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
199 memmove(cp, ep->e_name, (long)ep->e_namlen);
200 if (ep == lookupino(ROOTINO))
205 panic("%s: pathname too long\n", cp);
210 * Unused symbol table entries are linked together on a free list
211 * headed by the following pointer.
213 static struct entry *freelist = NULL;
216 * add an entry to the symbol table
219 addentry(char *name, ino_t inum, int type)
221 struct entry *np, *ep;
223 if (freelist != NULL) {
225 freelist = np->e_next;
226 memset(np, 0, (long)sizeof(struct entry));
228 np = (struct entry *)calloc(1, sizeof(struct entry));
230 panic("no memory to extend symbol table\n");
232 np->e_type = type & ~LINK;
233 ep = lookupparent(name);
235 if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
236 panic("bad name to addentry %s\n", name);
237 np->e_name = savename(name);
238 np->e_namlen = strlen(name);
243 np->e_name = savename(strrchr(name, '/') + 1);
244 np->e_namlen = strlen(np->e_name);
246 np->e_sibling = ep->e_entries;
249 ep = lookupino(inum);
251 panic("link to non-existent name\n");
253 np->e_links = ep->e_links;
255 } else if (inum != 0) {
256 if (lookupino(inum) != NULL)
257 panic("duplicate entry\n");
264 * delete an entry from the symbol table
267 freeentry(struct entry *ep)
272 if (ep->e_flags != REMOVED)
273 badentry(ep, "not marked REMOVED");
274 if (ep->e_type == NODE) {
275 if (ep->e_links != NULL)
276 badentry(ep, "freeing referenced directory");
277 if (ep->e_entries != NULL)
278 badentry(ep, "freeing non-empty directory");
280 if (ep->e_ino != 0) {
281 np = lookupino(ep->e_ino);
283 badentry(ep, "lookupino failed");
287 if (ep->e_links != NULL)
288 addino(inum, ep->e_links);
290 for (; np != NULL; np = np->e_links) {
291 if (np->e_links == ep) {
292 np->e_links = ep->e_links;
297 badentry(ep, "link not found");
301 freename(ep->e_name);
302 ep->e_next = freelist;
307 * Relocate an entry in the tree structure
310 moveentry(struct entry *ep, char *newname)
315 np = lookupparent(newname);
317 badentry(ep, "cannot move ROOT");
318 if (np != ep->e_parent) {
321 ep->e_sibling = np->e_entries;
324 cp = strrchr(newname, '/') + 1;
325 freename(ep->e_name);
326 ep->e_name = savename(cp);
327 ep->e_namlen = strlen(cp);
328 if (strcmp(gentempname(ep), ep->e_name) == 0)
329 ep->e_flags |= TMPNAME;
331 ep->e_flags &= ~TMPNAME;
335 * Remove an entry in the tree structure
338 removeentry(struct entry *ep)
343 if (np->e_entries == ep) {
344 np->e_entries = ep->e_sibling;
346 for (np = np->e_entries; np != NULL; np = np->e_sibling) {
347 if (np->e_sibling == ep) {
348 np->e_sibling = ep->e_sibling;
353 badentry(ep, "cannot find entry in parent list");
358 * Table of unused string entries, sorted by length.
360 * Entries are allocated in STRTBLINCR sized pieces so that names
361 * of similar lengths can use the same entry. The value of STRTBLINCR
362 * is chosen so that every entry has at least enough space to hold
363 * a "struct strtbl" header. Thus every entry can be linked onto an
364 * appropriate free list.
366 * NB. The macro "allocsize" below assumes that "struct strhdr"
367 * has a size that is a power of two.
373 #define STRTBLINCR (sizeof(struct strhdr))
374 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
376 static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
379 * Allocate space for a name. It first looks to see if it already
380 * has an appropriate sized entry, and if not allocates a new one.
392 np = strtblhdr[len / STRTBLINCR].next;
394 strtblhdr[len / STRTBLINCR].next = np->next;
397 cp = malloc((unsigned)allocsize(len));
399 panic("no space for string table\n");
401 (void) strcpy(cp, name);
406 * Free space for a name. The resulting entry is linked onto the
407 * appropriate free list.
412 struct strhdr *tp, *np;
414 tp = &strtblhdr[strlen(name) / STRTBLINCR];
415 np = (struct strhdr *)name;
421 * Useful quantities placed at the end of a dumped symbol table.
423 struct symtableheader {
426 int32_t entrytblsize;
434 * dump a snapshot of the symbol table
437 dumpsymtable(char *filename, long checkpt)
439 struct entry *ep, *tep;
441 struct entry temp, *tentry;
442 long mynum = 1, stroff = 0;
444 struct symtableheader hdr;
446 vprintf(stdout, "Checkpointing the restore\n");
449 if ((fd = fopen(filename, "w")) == NULL) {
450 fprintf(stderr, "fopen: %s\n", strerror(errno));
451 panic("cannot create save file %s for symbol table\n",
457 * Assign indices to each entry
458 * Write out the string entries
460 for (i = WINO; i <= maxino; i++) {
461 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
462 ep->e_index = mynum++;
463 (void) fwrite(ep->e_name, sizeof(char),
464 (int)allocsize(ep->e_namlen), fd);
468 * Convert pointers to indexes, and output
472 for (i = WINO; i <= maxino; i++) {
473 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
474 memmove(tep, ep, (long)sizeof(struct entry));
475 tep->e_name = (char *)stroff;
476 stroff += allocsize(ep->e_namlen);
477 tep->e_parent = (struct entry *)ep->e_parent->e_index;
478 if (ep->e_links != NULL)
480 (struct entry *)ep->e_links->e_index;
481 if (ep->e_sibling != NULL)
483 (struct entry *)ep->e_sibling->e_index;
484 if (ep->e_entries != NULL)
486 (struct entry *)ep->e_entries->e_index;
487 if (ep->e_next != NULL)
489 (struct entry *)ep->e_next->e_index;
490 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
494 * Convert entry pointers to indexes, and output
496 for (i = 0; i < entrytblsize; i++) {
497 if (entry[i] == NULL)
500 tentry = (struct entry *)entry[i]->e_index;
501 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
505 hdr.entrytblsize = entrytblsize;
506 hdr.stringsize = stroff;
507 hdr.dumptime = dumptime;
508 hdr.dumpdate = dumpdate;
510 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
512 fprintf(stderr, "fwrite: %s\n", strerror(errno));
513 panic("output error to file %s writing symbol table\n",
520 * Initialize a symbol table from a file
523 initsymtable(char *filename)
528 struct entry *baseep, *lep;
529 struct symtableheader hdr;
534 vprintf(stdout, "Initialize symbol table.\n");
535 if (filename == NULL) {
536 entrytblsize = maxino / HASHFACTOR;
537 entry = (struct entry **)
538 calloc((unsigned)entrytblsize, sizeof(struct entry *));
539 if (entry == (struct entry **)NULL)
540 panic("no memory for entry table\n");
541 ep = addentry(".", ROOTINO, NODE);
545 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
546 fprintf(stderr, "open: %s\n", strerror(errno));
547 panic("cannot open symbol table file %s\n", filename);
549 if (fstat(fd, &stbuf) < 0) {
550 fprintf(stderr, "stat: %s\n", strerror(errno));
551 panic("cannot stat symbol table file %s\n", filename);
553 tblsize = stbuf.st_size - sizeof(struct symtableheader);
554 base = calloc(sizeof(char), (unsigned)tblsize);
556 panic("cannot allocate space for symbol table\n");
557 if (read(fd, base, (int)tblsize) < 0 ||
558 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
559 fprintf(stderr, "read: %s\n", strerror(errno));
560 panic("cannot read symbol table file %s\n", filename);
565 * For normal continuation, insure that we are using
566 * the next incremental tape
568 if (hdr.dumpdate != dumptime) {
569 if (hdr.dumpdate < dumptime)
570 fprintf(stderr, "Incremental tape too low\n");
572 fprintf(stderr, "Incremental tape too high\n");
578 * For restart, insure that we are using the same tape
580 curfile.action = SKIP;
581 dumptime = hdr.dumptime;
582 dumpdate = hdr.dumpdate;
584 newtapebuf(hdr.ntrec);
588 panic("initsymtable called from command %c\n", command);
592 entrytblsize = hdr.entrytblsize;
593 entry = (struct entry **)
594 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
595 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
596 lep = (struct entry *)entry;
597 for (i = 0; i < entrytblsize; i++) {
598 if (entry[i] == NULL)
600 entry[i] = &baseep[(long)entry[i]];
602 for (ep = &baseep[1]; ep < lep; ep++) {
603 ep->e_name = base + (long)ep->e_name;
604 ep->e_parent = &baseep[(long)ep->e_parent];
605 if (ep->e_sibling != NULL)
606 ep->e_sibling = &baseep[(long)ep->e_sibling];
607 if (ep->e_links != NULL)
608 ep->e_links = &baseep[(long)ep->e_links];
609 if (ep->e_entries != NULL)
610 ep->e_entries = &baseep[(long)ep->e_entries];
611 if (ep->e_next != NULL)
612 ep->e_next = &baseep[(long)ep->e_next];