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