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 * 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.
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
33 * @(#)symtab.c 8.3 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/restore/symtab.c,v 1.7.2.1 2001/12/19 14:54:14 tobez Exp $
35 * $DragonFly: src/sbin/restore/symtab.c,v 1.8 2005/11/06 12:49:25 swildner Exp $
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 <vfs/ufs/dinode.h>
63 * The following variables define the inode symbol table.
64 * The primary hash table is dynamically allocated based on
65 * the number of inodes in the file system (maxino), scaled by
66 * HASHFACTOR. The variable "entry" points to the hash table;
67 * the variable "entrytblsize" indicates its size (in entries).
70 static struct entry
**entry
;
71 static long entrytblsize
;
73 static void addino(ufs1_ino_t
, struct entry
*);
74 static struct entry
*lookupparent(char *);
75 static void removeentry(struct entry
*);
78 * Look up an entry by inode number
81 lookupino(ufs1_ino_t inum
)
85 if (inum
< WINO
|| inum
>= maxino
)
87 for (ep
= entry
[inum
% entrytblsize
]; ep
!= NULL
; ep
= ep
->e_next
)
88 if (ep
->e_ino
== inum
)
94 * Add an entry into the entry table
97 addino(ufs1_ino_t inum
, struct entry
*np
)
101 if (inum
< WINO
|| inum
>= maxino
)
102 panic("addino: out of range %d\n", inum
);
103 epp
= &entry
[inum
% entrytblsize
];
108 for (np
= np
->e_next
; np
!= NULL
; np
= np
->e_next
)
109 if (np
->e_ino
== inum
)
110 badentry(np
, "duplicate inum");
114 * Delete an entry from the entry table
117 deleteino(ufs1_ino_t inum
)
122 if (inum
< WINO
|| inum
>= maxino
)
123 panic("deleteino: out of range %d\n", inum
);
124 prev
= &entry
[inum
% entrytblsize
];
125 for (next
= *prev
; next
!= NULL
; next
= next
->e_next
) {
126 if (next
->e_ino
== inum
) {
128 *prev
= next
->e_next
;
131 prev
= &next
->e_next
;
133 panic("deleteino: %d not found\n", inum
);
137 * Look up an entry by name
140 lookupname(char *name
)
144 char buf
[MAXPATHLEN
];
147 for (ep
= lookupino(ROOTINO
); ep
!= NULL
; ep
= ep
->e_entries
) {
148 for (np
= buf
; *cp
!= '/' && *cp
!= '\0' &&
149 np
< &buf
[sizeof(buf
)]; )
151 if (np
== &buf
[sizeof(buf
)])
154 for ( ; ep
!= NULL
; ep
= ep
->e_sibling
)
155 if (strcmp(ep
->e_name
, buf
) == 0)
166 * Look up the parent of a pathname
168 static struct entry
*
169 lookupparent(char *name
)
174 tailindex
= strrchr(name
, '/');
175 if (tailindex
== NULL
)
178 ep
= lookupname(name
);
182 if (ep
->e_type
!= NODE
)
183 panic("%s is not a directory\n", name
);
188 * Determine the current pathname of a node or leaf
191 myname(struct entry
*ep
)
194 static char namebuf
[MAXPATHLEN
];
196 for (cp
= &namebuf
[MAXPATHLEN
- 2]; cp
> &namebuf
[ep
->e_namlen
]; ) {
198 memmove(cp
, ep
->e_name
, (long)ep
->e_namlen
);
199 if (ep
== lookupino(ROOTINO
))
204 panic("%s: pathname too long\n", cp
);
209 * Unused symbol table entries are linked together on a free list
210 * headed by the following pointer.
212 static struct entry
*freelist
= NULL
;
215 * add an entry to the symbol table
218 addentry(char *name
, ufs1_ino_t inum
, int type
)
220 struct entry
*np
, *ep
;
222 if (freelist
!= NULL
) {
224 freelist
= np
->e_next
;
225 memset(np
, 0, (long)sizeof(struct entry
));
227 np
= (struct entry
*)calloc(1, sizeof(struct entry
));
229 panic("no memory to extend symbol table\n");
231 np
->e_type
= type
& ~LINK
;
232 ep
= lookupparent(name
);
234 if (inum
!= ROOTINO
|| lookupino(ROOTINO
) != NULL
)
235 panic("bad name to addentry %s\n", name
);
236 np
->e_name
= savename(name
);
237 np
->e_namlen
= strlen(name
);
242 np
->e_name
= savename(strrchr(name
, '/') + 1);
243 np
->e_namlen
= strlen(np
->e_name
);
245 np
->e_sibling
= ep
->e_entries
;
248 ep
= lookupino(inum
);
250 panic("link to non-existent name\n");
252 np
->e_links
= ep
->e_links
;
254 } else if (inum
!= 0) {
255 if (lookupino(inum
) != NULL
)
256 panic("duplicate entry\n");
263 * delete an entry from the symbol table
266 freeentry(struct entry
*ep
)
271 if (ep
->e_flags
!= REMOVED
)
272 badentry(ep
, "not marked REMOVED");
273 if (ep
->e_type
== NODE
) {
274 if (ep
->e_links
!= NULL
)
275 badentry(ep
, "freeing referenced directory");
276 if (ep
->e_entries
!= NULL
)
277 badentry(ep
, "freeing non-empty directory");
279 if (ep
->e_ino
!= 0) {
280 np
= lookupino(ep
->e_ino
);
282 badentry(ep
, "lookupino failed");
286 if (ep
->e_links
!= NULL
)
287 addino(inum
, ep
->e_links
);
289 for (; np
!= NULL
; np
= np
->e_links
) {
290 if (np
->e_links
== ep
) {
291 np
->e_links
= ep
->e_links
;
296 badentry(ep
, "link not found");
300 freename(ep
->e_name
);
301 ep
->e_next
= freelist
;
306 * Relocate an entry in the tree structure
309 moveentry(struct entry
*ep
, char *newname
)
314 np
= lookupparent(newname
);
316 badentry(ep
, "cannot move ROOT");
317 if (np
!= ep
->e_parent
) {
320 ep
->e_sibling
= np
->e_entries
;
323 cp
= strrchr(newname
, '/') + 1;
324 freename(ep
->e_name
);
325 ep
->e_name
= savename(cp
);
326 ep
->e_namlen
= strlen(cp
);
327 if (strcmp(gentempname(ep
), ep
->e_name
) == 0)
328 ep
->e_flags
|= TMPNAME
;
330 ep
->e_flags
&= ~TMPNAME
;
334 * Remove an entry in the tree structure
337 removeentry(struct entry
*ep
)
342 if (np
->e_entries
== ep
) {
343 np
->e_entries
= ep
->e_sibling
;
345 for (np
= np
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
346 if (np
->e_sibling
== ep
) {
347 np
->e_sibling
= ep
->e_sibling
;
352 badentry(ep
, "cannot find entry in parent list");
357 * Table of unused string entries, sorted by length.
359 * Entries are allocated in STRTBLINCR sized pieces so that names
360 * of similar lengths can use the same entry. The value of STRTBLINCR
361 * is chosen so that every entry has at least enough space to hold
362 * a "struct strtbl" header. Thus every entry can be linked onto an
363 * appropriate free list.
365 * NB. The macro "allocsize" below assumes that "struct strhdr"
366 * has a size that is a power of two.
372 #define STRTBLINCR (sizeof(struct strhdr))
373 #define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
375 static struct strhdr strtblhdr
[allocsize(NAME_MAX
) / STRTBLINCR
];
378 * Allocate space for a name. It first looks to see if it already
379 * has an appropriate sized entry, and if not allocates a new one.
391 np
= strtblhdr
[len
/ STRTBLINCR
].next
;
393 strtblhdr
[len
/ STRTBLINCR
].next
= np
->next
;
396 cp
= malloc((unsigned)allocsize(len
));
398 panic("no space for string table\n");
405 * Free space for a name. The resulting entry is linked onto the
406 * appropriate free list.
411 struct strhdr
*tp
, *np
;
413 tp
= &strtblhdr
[strlen(name
) / STRTBLINCR
];
414 np
= (struct strhdr
*)name
;
420 * Useful quantities placed at the end of a dumped symbol table.
422 struct symtableheader
{
425 int32_t entrytblsize
;
433 * dump a snapshot of the symbol table
436 dumpsymtable(char *filename
, long checkpt
)
438 struct entry
*ep
, *tep
;
440 struct entry temp
, *tentry
;
441 long mynum
= 1, stroff
= 0;
443 struct symtableheader hdr
;
445 vprintf(stdout
, "Check pointing the restore\n");
448 if ((fd
= fopen(filename
, "w")) == NULL
) {
449 fprintf(stderr
, "fopen: %s\n", strerror(errno
));
450 panic("cannot create save file %s for symbol table\n",
456 * Assign indices to each entry
457 * Write out the string entries
459 for (i
= WINO
; i
<= maxino
; i
++) {
460 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
461 ep
->e_index
= mynum
++;
462 fwrite(ep
->e_name
, sizeof(char),
463 (int)allocsize(ep
->e_namlen
), fd
);
467 * Convert pointers to indexes, and output
471 for (i
= WINO
; i
<= maxino
; i
++) {
472 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
473 memmove(tep
, ep
, (long)sizeof(struct entry
));
474 tep
->e_name
= (char *)stroff
;
475 stroff
+= allocsize(ep
->e_namlen
);
476 tep
->e_parent
= (struct entry
*)ep
->e_parent
->e_index
;
477 if (ep
->e_links
!= NULL
)
479 (struct entry
*)ep
->e_links
->e_index
;
480 if (ep
->e_sibling
!= NULL
)
482 (struct entry
*)ep
->e_sibling
->e_index
;
483 if (ep
->e_entries
!= NULL
)
485 (struct entry
*)ep
->e_entries
->e_index
;
486 if (ep
->e_next
!= NULL
)
488 (struct entry
*)ep
->e_next
->e_index
;
489 fwrite((char *)tep
, sizeof(struct entry
), 1, fd
);
493 * Convert entry pointers to indexes, and output
495 for (i
= 0; i
< entrytblsize
; i
++) {
496 if (entry
[i
] == NULL
)
499 tentry
= (struct entry
*)entry
[i
]->e_index
;
500 fwrite((char *)&tentry
, sizeof(struct entry
*), 1, fd
);
504 hdr
.entrytblsize
= entrytblsize
;
505 hdr
.stringsize
= stroff
;
506 hdr
.dumptime
= dumptime
;
507 hdr
.dumpdate
= dumpdate
;
509 fwrite((char *)&hdr
, sizeof(struct symtableheader
), 1, fd
);
511 fprintf(stderr
, "fwrite: %s\n", strerror(errno
));
512 panic("output error to file %s writing symbol table\n",
519 * Initialize a symbol table from a file
522 initsymtable(char *filename
)
527 struct entry
*baseep
, *lep
;
528 struct symtableheader hdr
;
533 vprintf(stdout
, "Initialize symbol table.\n");
534 if (filename
== NULL
) {
535 entrytblsize
= maxino
/ HASHFACTOR
;
536 entry
= (struct entry
**)
537 calloc((unsigned)entrytblsize
, sizeof(struct entry
*));
539 panic("no memory for entry table\n");
540 ep
= addentry(".", ROOTINO
, NODE
);
544 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0) {
545 fprintf(stderr
, "open: %s\n", strerror(errno
));
546 panic("cannot open symbol table file %s\n", filename
);
548 if (fstat(fd
, &stbuf
) < 0) {
549 fprintf(stderr
, "stat: %s\n", strerror(errno
));
550 panic("cannot stat symbol table file %s\n", filename
);
552 tblsize
= stbuf
.st_size
- sizeof(struct symtableheader
);
553 base
= calloc(sizeof(char), (unsigned)tblsize
);
555 panic("cannot allocate space for symbol table\n");
556 if (read(fd
, base
, (int)tblsize
) < 0 ||
557 read(fd
, (char *)&hdr
, sizeof(struct symtableheader
)) < 0) {
558 fprintf(stderr
, "read: %s\n", strerror(errno
));
559 panic("cannot read symbol table file %s\n", filename
);
564 * For normal continuation, insure that we are using
565 * the next incremental tape
567 if (hdr
.dumpdate
!= dumptime
) {
568 if (hdr
.dumpdate
< dumptime
)
569 fprintf(stderr
, "Incremental tape too low\n");
571 fprintf(stderr
, "Incremental tape too high\n");
577 * For restart, insure that we are using the same tape
579 curfile
.action
= SKIP
;
580 dumptime
= hdr
.dumptime
;
581 dumpdate
= hdr
.dumpdate
;
583 newtapebuf(hdr
.ntrec
);
587 panic("initsymtable called from command %c\n", command
);
591 entrytblsize
= hdr
.entrytblsize
;
592 entry
= (struct entry
**)
593 (base
+ tblsize
- (entrytblsize
* sizeof(struct entry
*)));
594 baseep
= (struct entry
*)(base
+ hdr
.stringsize
- sizeof(struct entry
));
595 lep
= (struct entry
*)entry
;
596 for (i
= 0; i
< entrytblsize
; i
++) {
597 if (entry
[i
] == NULL
)
599 entry
[i
] = &baseep
[(long)entry
[i
]];
601 for (ep
= &baseep
[1]; ep
< lep
; ep
++) {
602 ep
->e_name
= base
+ (long)ep
->e_name
;
603 ep
->e_parent
= &baseep
[(long)ep
->e_parent
];
604 if (ep
->e_sibling
!= NULL
)
605 ep
->e_sibling
= &baseep
[(long)ep
->e_sibling
];
606 if (ep
->e_links
!= NULL
)
607 ep
->e_links
= &baseep
[(long)ep
->e_links
];
608 if (ep
->e_entries
!= NULL
)
609 ep
->e_entries
= &baseep
[(long)ep
->e_entries
];
610 if (ep
->e_next
!= NULL
)
611 ep
->e_next
= &baseep
[(long)ep
->e_next
];