2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid[] =
43 "$Id: symtab.c,v 1.8 2000/03/01 10:16:05 stelian Exp $";
47 * These routines maintain the symbol table which tracks the state
48 * of the file system being restored. They provide lookup by either
49 * name or inode number. They also provide for creation, deletion,
50 * and renaming of entries. Because of the dynamic nature of pathnames,
51 * names should not be saved, but always constructed just before they
52 * are needed, by calling "myname".
55 #include <sys/param.h>
60 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
63 #include <ufs/ufs/dinode.h>
64 #endif /* __linux__ */
67 #include <compaterr.h>
75 #include <ext2fs/ext2fs.h>
82 * The following variables define the inode symbol table.
83 * The primary hash table is dynamically allocated based on
84 * the number of inodes in the file system (maxino), scaled by
85 * HASHFACTOR. The variable "entry" points to the hash table;
86 * the variable "entrytblsize" indicates its size (in entries).
89 static struct entry **entry;
90 static long entrytblsize;
92 static void addino __P((ino_t, struct entry *));
93 static struct entry *lookupparent __P((char *));
94 static void removeentry __P((struct entry *));
97 * Look up an entry by inode number
100 lookupino(ino_t inum)
102 register struct entry *ep;
104 if (inum < WINO || inum >= maxino)
106 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
107 if (ep->e_ino == inum)
113 * Add an entry into the entry table
116 addino(ino_t inum, struct entry *np)
120 if (inum < WINO || inum >= maxino)
121 panic("addino: out of range %d\n", inum);
122 epp = &entry[inum % entrytblsize];
127 for (np = np->e_next; np != NULL; np = np->e_next)
128 if (np->e_ino == inum)
129 badentry(np, "duplicate inum");
133 * Delete an entry from the entry table
136 deleteino(ino_t inum)
138 register struct entry *next;
141 if (inum < WINO || inum >= maxino)
142 panic("deleteino: out of range %d\n", inum);
143 prev = &entry[inum % entrytblsize];
144 for (next = *prev; next != NULL; next = next->e_next) {
145 if (next->e_ino == inum) {
147 *prev = next->e_next;
150 prev = &next->e_next;
152 panic("deleteino: %d not found\n", inum);
156 * Look up an entry by name
159 lookupname(char *name)
161 register struct entry *ep;
162 register char *np, *cp;
163 char buf[MAXPATHLEN];
166 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
167 for (np = buf; *cp != '/' && *cp != '\0' &&
168 np < &buf[sizeof(buf)]; )
170 if (np == &buf[sizeof(buf)])
173 for ( ; ep != NULL; ep = ep->e_sibling)
174 if (strcmp(ep->e_name, buf) == 0)
185 * Look up the parent of a pathname
187 static struct entry *
188 lookupparent(char *name)
193 tailindex = strrchr(name, '/');
194 if (tailindex == NULL)
197 ep = lookupname(name);
201 if (ep->e_type != NODE)
202 panic("%s is not a directory\n", name);
207 * Determine the current pathname of a node or leaf
210 myname(struct entry *ep)
213 static char namebuf[MAXPATHLEN];
215 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
217 memmove(cp, ep->e_name, (size_t)ep->e_namlen);
218 if (ep == lookupino(ROOTINO))
223 panic("%s: pathname too long\n", cp);
228 * Unused symbol table entries are linked together on a free list
229 * headed by the following pointer.
231 static struct entry *freelist = NULL;
234 * add an entry to the symbol table
237 addentry(char *name, ino_t inum, int type)
239 register struct entry *np, *ep;
241 if (freelist != NULL) {
243 freelist = np->e_next;
244 memset(np, 0, sizeof(struct entry));
246 np = (struct entry *)calloc(1, sizeof(struct entry));
248 panic("no memory to extend symbol table\n");
250 np->e_type = type & ~LINK;
251 ep = lookupparent(name);
253 if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
254 panic("bad name to addentry %s\n", name);
255 np->e_name = savename(name);
256 np->e_namlen = strlen(name);
261 np->e_name = savename(strrchr(name, '/') + 1);
262 np->e_namlen = strlen(np->e_name);
264 np->e_sibling = ep->e_entries;
267 ep = lookupino(inum);
269 panic("link to non-existent name\n");
271 np->e_links = ep->e_links;
273 } else if (inum != 0) {
274 if (lookupino(inum) != NULL)
275 panic("duplicate entry\n");
282 * delete an entry from the symbol table
285 freeentry(struct entry *ep)
287 register struct entry *np;
290 if (ep->e_flags != REMOVED)
291 badentry(ep, "not marked REMOVED");
292 if (ep->e_type == NODE) {
293 if (ep->e_links != NULL)
294 badentry(ep, "freeing referenced directory");
295 if (ep->e_entries != NULL)
296 badentry(ep, "freeing non-empty directory");
298 if (ep->e_ino != 0) {
299 np = lookupino(ep->e_ino);
301 badentry(ep, "lookupino failed");
305 if (ep->e_links != NULL)
306 addino(inum, ep->e_links);
308 for (; np != NULL; np = np->e_links) {
309 if (np->e_links == ep) {
310 np->e_links = ep->e_links;
315 badentry(ep, "link not found");
319 freename(ep->e_name);
320 ep->e_next = freelist;
325 * Relocate an entry in the tree structure
328 moveentry(struct entry *ep, char *newname)
333 np = lookupparent(newname);
335 badentry(ep, "cannot move ROOT");
336 if (np != ep->e_parent) {
339 ep->e_sibling = np->e_entries;
342 cp = strrchr(newname, '/') + 1;
343 freename(ep->e_name);
344 ep->e_name = savename(cp);
345 ep->e_namlen = strlen(cp);
346 if (strcmp(gentempname(ep), ep->e_name) == 0)
347 ep->e_flags |= TMPNAME;
349 ep->e_flags &= ~TMPNAME;
353 * Remove an entry in the tree structure
356 removeentry(struct entry *ep)
358 register struct entry *np;
361 if (np->e_entries == ep) {
362 np->e_entries = ep->e_sibling;
364 for (np = np->e_entries; np != NULL; np = np->e_sibling) {
365 if (np->e_sibling == ep) {
366 np->e_sibling = ep->e_sibling;
371 badentry(ep, "cannot find entry in parent list");
376 * Table of unused string entries, sorted by length.
378 * Entries are allocated in STRTBLINCR sized pieces so that names
379 * of similar lengths can use the same entry. The value of STRTBLINCR
380 * is chosen so that every entry has at least enough space to hold
381 * a "struct strtbl" header. Thus every entry can be linked onto an
382 * appropriate free list.
384 * NB. The macro "allocsize" below assumes that "struct strhdr"
385 * has a size that is a power of two.
391 #define STRTBLINCR (sizeof(struct strhdr))
392 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
394 static struct strhdr strtblhdr[allocsize(MAXNAMLEN) / STRTBLINCR];
397 * Allocate space for a name. It first looks to see if it already
398 * 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.
430 struct strhdr *tp, *np;
432 tp = &strtblhdr[strlen(name) / STRTBLINCR];
433 np = (struct strhdr *)name;
439 * Useful quantities placed at the end of a dumped symbol table.
441 struct symtableheader {
444 int32_t entrytblsize;
452 * dump a snapshot of the symbol table
455 dumpsymtable(char *filename, long checkpt)
457 register struct entry *ep, *tep;
459 struct entry temp, *tentry;
460 long mynum = 1, stroff = 0;
462 struct symtableheader hdr;
464 Vprintf(stdout, "Check pointing the restore\n");
467 if ((fd = fopen(filename, "w")) == NULL) {
469 panic("cannot create save file %s for symbol table\n",
474 * Assign indices to each entry
475 * Write out the string entries
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);
485 * Convert pointers to indexes, and output
489 for (i = WINO; i <= maxino; i++) {
490 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
491 memmove(tep, ep, 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)
497 (struct entry *)ep->e_links->e_index;
498 if (ep->e_sibling != NULL)
500 (struct entry *)ep->e_sibling->e_index;
501 if (ep->e_entries != NULL)
503 (struct entry *)ep->e_entries->e_index;
504 if (ep->e_next != NULL)
506 (struct entry *)ep->e_next->e_index;
507 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
511 * Convert entry pointers to indexes, and output
513 for (i = 0; i < entrytblsize; i++) {
514 if (entry[i] == NULL)
517 tentry = (struct entry *)entry[i]->e_index;
518 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
522 hdr.entrytblsize = entrytblsize;
523 hdr.stringsize = stroff;
524 hdr.dumptime = dumptime;
525 hdr.dumpdate = dumpdate;
527 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
530 panic("output error to file %s writing symbol table\n",
537 * Initialize a symbol table from a file
540 initsymtable(char *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) {
564 panic("cannot open symbol table file %s\n", filename);
566 if (fstat(fd, &stbuf) < 0) {
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) {
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 errx(1, "Incremental tape too %s",
587 (hdr.dumpdate < dumptime) ? "low" : "high");
591 * For restart, insure that we are using the same tape
593 curfile.action = SKIP;
594 dumptime = hdr.dumptime;
595 dumpdate = hdr.dumpdate;
597 newtapebuf(hdr.ntrec);
601 panic("initsymtable called from command %c\n", command);
605 entrytblsize = hdr.entrytblsize;
606 entry = (struct entry **)
607 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
608 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
609 lep = (struct entry *)entry;
610 for (i = 0; i < entrytblsize; i++) {
611 if (entry[i] == NULL)
613 entry[i] = &baseep[(long)entry[i]];
615 for (ep = &baseep[1]; ep < lep; ep++) {
616 ep->e_name = base + (long)ep->e_name;
617 ep->e_parent = &baseep[(long)ep->e_parent];
618 if (ep->e_sibling != NULL)
619 ep->e_sibling = &baseep[(long)ep->e_sibling];
620 if (ep->e_links != NULL)
621 ep->e_links = &baseep[(long)ep->e_links];
622 if (ep->e_entries != NULL)
623 ep->e_entries = &baseep[(long)ep->e_entries];
624 if (ep->e_next != NULL)
625 ep->e_next = &baseep[(long)ep->e_next];