]> git.wh0rd.org - dump.git/blob - restore/symtab.c
Fix maps overflow in restore when doing incremental restores (and MAXINO becomes...
[dump.git] / restore / symtab.c
1 /*
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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: symtab.c,v 1.20 2002/11/15 09:25:42 stelian Exp $";
45 #endif /* not lint */
46
47 /*
48 * These routines maintain the symbol table which tracks the state
49 * of the file system being restored. They provide lookup by either
50 * name or inode number. They also provide for creation, deletion,
51 * and renaming of entries. Because of the dynamic nature of pathnames,
52 * names should not be saved, but always constructed just before they
53 * are needed, by calling "myname".
54 */
55
56 #include <config.h>
57 #include <sys/param.h>
58 #include <sys/stat.h>
59
60 #ifdef __linux__
61 #include <sys/time.h>
62 #include <time.h>
63 #ifdef HAVE_EXT2FS_EXT2_FS_H
64 #include <ext2fs/ext2_fs.h>
65 #else
66 #include <linux/ext2_fs.h>
67 #endif
68 #include <bsdcompat.h>
69 #else /* __linux__ */
70 #include <ufs/ufs/dinode.h>
71 #endif /* __linux__ */
72
73 #include <errno.h>
74 #include <compaterr.h>
75 #include <fcntl.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80
81 #ifdef __linux__
82 #include <ext2fs/ext2fs.h>
83 #endif
84
85 #include "restore.h"
86 #include "extern.h"
87
88 /*
89 * The following variables define the inode symbol table.
90 * The primary hash table is dynamically allocated based on
91 * the number of inodes in the file system (maxino), scaled by
92 * HASHFACTOR. The variable "entry" points to the hash table;
93 * the variable "entrytblsize" indicates its size (in entries).
94 */
95 #define HASHFACTOR 5
96 static struct entry **entry;
97 static long entrytblsize;
98
99 static void addino __P((dump_ino_t, struct entry *));
100 static struct entry *lookupparent __P((char *));
101 static void removeentry __P((struct entry *));
102
103 /*
104 * Look up an entry by inode number
105 */
106 struct entry *
107 lookupino(dump_ino_t inum)
108 {
109 struct entry *ep;
110
111 if (inum < WINO || inum >= maxino)
112 return (NULL);
113 for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
114 if (ep->e_ino == inum)
115 return (ep);
116 return (NULL);
117 }
118
119 /*
120 * Add an entry into the entry table
121 */
122 static void
123 addino(dump_ino_t inum, struct entry *np)
124 {
125 struct entry **epp;
126
127 if (inum < WINO || inum >= maxino)
128 panic("addino: out of range %d\n", inum);
129 epp = &entry[inum % entrytblsize];
130 np->e_ino = inum;
131 np->e_next = *epp;
132 *epp = np;
133 if (dflag)
134 for (np = np->e_next; np != NULL; np = np->e_next)
135 if (np->e_ino == inum)
136 badentry(np, "duplicate inum");
137 }
138
139 /*
140 * Delete an entry from the entry table
141 */
142 void
143 deleteino(dump_ino_t inum)
144 {
145 struct entry *next;
146 struct entry **prev;
147
148 if (inum < WINO || inum >= maxino)
149 panic("deleteino: out of range %d\n", inum);
150 prev = &entry[inum % entrytblsize];
151 for (next = *prev; next != NULL; next = next->e_next) {
152 if (next->e_ino == inum) {
153 next->e_ino = 0;
154 *prev = next->e_next;
155 return;
156 }
157 prev = &next->e_next;
158 }
159 panic("deleteino: %d not found\n", inum);
160 }
161
162 /*
163 * Look up an entry by name
164 */
165 struct entry *
166 lookupname(char *name)
167 {
168 struct entry *ep;
169 char *np, *cp;
170 char buf[MAXPATHLEN];
171
172 cp = name;
173 for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
174 for (np = buf; *cp != '/' && *cp != '\0' &&
175 np < &buf[sizeof(buf)]; )
176 *np++ = *cp++;
177 if (np == &buf[sizeof(buf)])
178 break;
179 *np = '\0';
180 for ( ; ep != NULL; ep = ep->e_sibling)
181 if (strcmp(ep->e_name, buf) == 0)
182 break;
183 if (ep == NULL)
184 break;
185 if (*cp++ == '\0')
186 return (ep);
187 }
188 return (NULL);
189 }
190
191 /*
192 * Look up the parent of a pathname
193 */
194 static struct entry *
195 lookupparent(char *name)
196 {
197 struct entry *ep;
198 char *tailindex;
199
200 tailindex = strrchr(name, '/');
201 if (tailindex == NULL)
202 return (NULL);
203 *tailindex = '\0';
204 ep = lookupname(name);
205 *tailindex = '/';
206 if (ep == NULL)
207 return (NULL);
208 if (ep->e_type != NODE)
209 panic("%s is not a directory\n", name);
210 return (ep);
211 }
212
213 /*
214 * Determine the current pathname of a node or leaf
215 */
216 char *
217 myname(struct entry *ep)
218 {
219 char *cp;
220 static char namebuf[MAXPATHLEN];
221
222 for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
223 cp -= ep->e_namlen;
224 memmove(cp, ep->e_name, (size_t)ep->e_namlen);
225 if (ep == lookupino(ROOTINO))
226 return (cp);
227 *(--cp) = '/';
228 ep = ep->e_parent;
229 }
230 panic("%s: pathname too long\n", cp);
231 return(cp);
232 }
233
234 /*
235 * Unused symbol table entries are linked together on a free list
236 * headed by the following pointer.
237 */
238 static struct entry *freelist = NULL;
239
240 /*
241 * add an entry to the symbol table
242 */
243 struct entry *
244 addentry(char *name, dump_ino_t inum, int type)
245 {
246 struct entry *np, *ep;
247
248 if (freelist != NULL) {
249 np = freelist;
250 freelist = np->e_next;
251 memset(np, 0, sizeof(struct entry));
252 } else {
253 np = (struct entry *)calloc(1, sizeof(struct entry));
254 if (np == NULL)
255 errx(1, "no memory to extend symbol table");
256 }
257 np->e_type = type & ~LINK;
258 ep = lookupparent(name);
259 if (ep == NULL) {
260 if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
261 panic("bad name to addentry %s\n", name);
262 np->e_name = savename(name);
263 np->e_namlen = strlen(name);
264 np->e_parent = np;
265 addino(ROOTINO, np);
266 return (np);
267 }
268 np->e_name = savename(strrchr(name, '/') + 1);
269 np->e_namlen = strlen(np->e_name);
270 np->e_parent = ep;
271 np->e_sibling = ep->e_entries;
272 ep->e_entries = np;
273 if (type & LINK) {
274 ep = lookupino(inum);
275 if (ep == NULL)
276 panic("link to non-existent name\n");
277 np->e_ino = inum;
278 np->e_links = ep->e_links;
279 ep->e_links = np;
280 } else if (inum != 0) {
281 if (lookupino(inum) != NULL)
282 panic("duplicate entry\n");
283 addino(inum, np);
284 }
285 return (np);
286 }
287
288 /*
289 * delete an entry from the symbol table
290 */
291 void
292 freeentry(struct entry *ep)
293 {
294 struct entry *np;
295 dump_ino_t inum;
296
297 if (ep->e_flags != REMOVED)
298 badentry(ep, "not marked REMOVED");
299 if (ep->e_type == NODE) {
300 if (ep->e_links != NULL)
301 badentry(ep, "freeing referenced directory");
302 if (ep->e_entries != NULL)
303 badentry(ep, "freeing non-empty directory");
304 }
305 if (ep->e_ino != 0) {
306 np = lookupino(ep->e_ino);
307 if (np == NULL)
308 badentry(ep, "lookupino failed");
309 if (np == ep) {
310 inum = ep->e_ino;
311 deleteino(inum);
312 if (ep->e_links != NULL)
313 addino(inum, ep->e_links);
314 } else {
315 for (; np != NULL; np = np->e_links) {
316 if (np->e_links == ep) {
317 np->e_links = ep->e_links;
318 break;
319 }
320 }
321 if (np == NULL)
322 badentry(ep, "link not found");
323 }
324 }
325 removeentry(ep);
326 freename(ep->e_name);
327 ep->e_next = freelist;
328 freelist = ep;
329 }
330
331 /*
332 * Relocate an entry in the tree structure
333 */
334 void
335 moveentry(struct entry *ep, char *newname)
336 {
337 struct entry *np;
338 char *cp;
339
340 np = lookupparent(newname);
341 if (np == NULL)
342 badentry(ep, "cannot move ROOT");
343 if (np != ep->e_parent) {
344 removeentry(ep);
345 ep->e_parent = np;
346 ep->e_sibling = np->e_entries;
347 np->e_entries = ep;
348 }
349 cp = strrchr(newname, '/') + 1;
350 freename(ep->e_name);
351 ep->e_name = savename(cp);
352 ep->e_namlen = strlen(cp);
353 if (strcmp(gentempname(ep), ep->e_name) == 0)
354 ep->e_flags |= TMPNAME;
355 else
356 ep->e_flags &= ~TMPNAME;
357 }
358
359 /*
360 * Remove an entry in the tree structure
361 */
362 static void
363 removeentry(struct entry *ep)
364 {
365 struct entry *np;
366
367 np = ep->e_parent;
368 if (np->e_entries == ep) {
369 np->e_entries = ep->e_sibling;
370 } else {
371 for (np = np->e_entries; np != NULL; np = np->e_sibling) {
372 if (np->e_sibling == ep) {
373 np->e_sibling = ep->e_sibling;
374 break;
375 }
376 }
377 if (np == NULL)
378 badentry(ep, "cannot find entry in parent list");
379 }
380 }
381
382 /*
383 * Table of unused string entries, sorted by length.
384 *
385 * Entries are allocated in STRTBLINCR sized pieces so that names
386 * of similar lengths can use the same entry. The value of STRTBLINCR
387 * is chosen so that every entry has at least enough space to hold
388 * a "struct strtbl" header. Thus every entry can be linked onto an
389 * appropriate free list.
390 *
391 * NB. The macro "allocsize" below assumes that "struct strhdr"
392 * has a size that is a power of two.
393 */
394 struct strhdr {
395 struct strhdr *next;
396 };
397
398 #define STRTBLINCR (sizeof(struct strhdr))
399 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
400
401 static struct strhdr strtblhdr[allocsize(MAXNAMLEN) / STRTBLINCR];
402
403 /*
404 * Allocate space for a name. It first looks to see if it already
405 * has an appropriate sized entry, and if not allocates a new one.
406 */
407 char *
408 savename(char *name)
409 {
410 struct strhdr *np;
411 long len;
412 char *cp;
413
414 if (name == NULL)
415 panic("bad name\n");
416 len = strlen(name);
417 np = strtblhdr[len / STRTBLINCR].next;
418 if (np != NULL) {
419 strtblhdr[len / STRTBLINCR].next = np->next;
420 cp = (char *)np;
421 } else {
422 cp = malloc((unsigned)allocsize(len));
423 if (cp == NULL)
424 errx(1, "no space for string table");
425 }
426 (void) strcpy(cp, name);
427 return (cp);
428 }
429
430 /*
431 * Free space for a name. The resulting entry is linked onto the
432 * appropriate free list.
433 */
434 void
435 freename(char *name)
436 {
437 struct strhdr *tp, *np;
438
439 tp = &strtblhdr[strlen(name) / STRTBLINCR];
440 np = (struct strhdr *)name;
441 np->next = tp->next;
442 tp->next = np;
443 }
444
445 /*
446 * Useful quantities placed at the end of a dumped symbol table.
447 */
448 struct symtableheader {
449 int32_t volno;
450 int32_t stringsize;
451 int32_t entrytblsize;
452 time_t dumptime;
453 time_t dumpdate;
454 dump_ino_t maxino;
455 int32_t ntrec;
456 int32_t zflag;
457 };
458
459 /*
460 * dump a snapshot of the symbol table
461 */
462 void
463 dumpsymtable(char *filename, long checkpt)
464 {
465 struct entry *ep, *tep;
466 dump_ino_t i;
467 struct entry temp, *tentry;
468 long mynum = 1, stroff = 0;
469 FILE *fd;
470 struct symtableheader hdr;
471
472 Vprintf(stdout, "Check pointing the restore\n");
473 if (Nflag)
474 return;
475 if ((fd = fopen(filename, "w")) == NULL) {
476 warn("fopen");
477 panic("cannot create save file %s for symbol table\n",
478 filename);
479 }
480 clearerr(fd);
481 /*
482 * Assign indices to each entry
483 * Write out the string entries
484 */
485 for (i = WINO; i <= maxino; i++) {
486 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
487 ep->e_index = mynum++;
488 (void) fwrite(ep->e_name, sizeof(char),
489 (int)allocsize(ep->e_namlen), fd);
490 }
491 }
492 /*
493 * Convert pointers to indexes, and output
494 */
495 tep = &temp;
496 stroff = 0;
497 for (i = WINO; i <= maxino; i++) {
498 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
499 memmove(tep, ep, sizeof(struct entry));
500 tep->e_name = (char *)stroff;
501 stroff += allocsize(ep->e_namlen);
502 tep->e_parent = (struct entry *)ep->e_parent->e_index;
503 if (ep->e_links != NULL)
504 tep->e_links =
505 (struct entry *)ep->e_links->e_index;
506 if (ep->e_sibling != NULL)
507 tep->e_sibling =
508 (struct entry *)ep->e_sibling->e_index;
509 if (ep->e_entries != NULL)
510 tep->e_entries =
511 (struct entry *)ep->e_entries->e_index;
512 if (ep->e_next != NULL)
513 tep->e_next =
514 (struct entry *)ep->e_next->e_index;
515 (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
516 }
517 }
518 /*
519 * Convert entry pointers to indexes, and output
520 */
521 for (i = 0; (long)i < entrytblsize; i++) {
522 if (entry[i] == NULL)
523 tentry = NULL;
524 else
525 tentry = (struct entry *)entry[i]->e_index;
526 (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
527 }
528 hdr.volno = checkpt;
529 hdr.maxino = maxino;
530 hdr.entrytblsize = entrytblsize;
531 hdr.stringsize = stroff;
532 hdr.dumptime = dumptime;
533 hdr.dumpdate = dumpdate;
534 hdr.ntrec = ntrec;
535 hdr.zflag = zflag;
536 (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
537 if (ferror(fd)) {
538 warn("fwrite");
539 panic("output error to file %s writing symbol table\n",
540 filename);
541 }
542 (void) fclose(fd);
543 }
544
545 /*
546 * Initialize a symbol table from a file
547 */
548 void
549 initsymtable(char *filename)
550 {
551 char *base;
552 long tblsize;
553 struct entry *ep;
554 struct entry *baseep, *lep;
555 struct symtableheader hdr;
556 struct stat stbuf;
557 long i;
558 int fd;
559
560 Vprintf(stdout, "Initialize symbol table.\n");
561 if (filename == NULL) {
562 entrytblsize = maxino / HASHFACTOR;
563 entry = (struct entry **)
564 calloc((unsigned)entrytblsize, sizeof(struct entry *));
565 if (entry == (struct entry **)NULL)
566 errx(1, "no memory for entry table");
567 ep = addentry(".", ROOTINO, NODE);
568 ep->e_flags |= NEW;
569 return;
570 }
571 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
572 warn("open");
573 errx(1, "cannot open symbol table file %s", filename);
574 }
575 if (fstat(fd, &stbuf) < 0) {
576 warn("stat");
577 errx(1, "cannot stat symbol table file %s", filename);
578 }
579 tblsize = stbuf.st_size - sizeof(struct symtableheader);
580 base = calloc(sizeof(char), (unsigned)tblsize);
581 if (base == NULL)
582 errx(1, "cannot allocate space for symbol table");
583 if (read(fd, base, (int)tblsize) < 0 ||
584 read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
585 warn("read");
586 errx(1, "cannot read symbol table file %s", filename);
587 }
588 switch (command) {
589 case 'r':
590 /*
591 * For normal continuation, insure that we are using
592 * the next incremental tape
593 */
594 if (hdr.dumpdate != dumptime)
595 errx(1, "Incremental tape too %s",
596 (hdr.dumpdate < dumptime) ? "low" : "high");
597 break;
598 case 'R':
599 /*
600 * For restart, insure that we are using the same tape
601 */
602 curfile.action = SKIP;
603 dumptime = hdr.dumptime;
604 dumpdate = hdr.dumpdate;
605 zflag = hdr.zflag;
606 if (!bflag)
607 newtapebuf(hdr.ntrec);
608 getvol(hdr.volno);
609 break;
610 default:
611 panic("initsymtable called from command %c\n", command);
612 break;
613 }
614 resizemaps(maxino, hdr.maxino);
615 maxino = hdr.maxino;
616 entrytblsize = hdr.entrytblsize;
617 entry = (struct entry **)
618 (base + tblsize - (entrytblsize * sizeof(struct entry *)));
619 baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
620 lep = (struct entry *)entry;
621 for (i = 0; i < entrytblsize; i++) {
622 if (entry[i] == NULL)
623 continue;
624 entry[i] = &baseep[(long)entry[i]];
625 }
626 for (ep = &baseep[1]; ep < lep; ep++) {
627 ep->e_name = base + (long)ep->e_name;
628 ep->e_parent = &baseep[(long)ep->e_parent];
629 if (ep->e_sibling != NULL)
630 ep->e_sibling = &baseep[(long)ep->e_sibling];
631 if (ep->e_links != NULL)
632 ep->e_links = &baseep[(long)ep->e_links];
633 if (ep->e_entries != NULL)
634 ep->e_entries = &baseep[(long)ep->e_entries];
635 if (ep->e_next != NULL)
636 ep->e_next = &baseep[(long)ep->e_next];
637 }
638 }