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