xref: /freebsd/usr.sbin/makefs/walk.c (revision e3514747256465c52c3b2aedc9795f52c0d3efe9)
1 /*	$NetBSD: walk.c,v 1.24 2008/12/28 21:51:46 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Luke Mewburn for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 
46 #include <assert.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <dirent.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "makefs.h"
56 #include "mtree.h"
57 #include "extern.h"
58 
59 static	void	 apply_specdir(const char *, NODE *, fsnode *, int);
60 static	void	 apply_specentry(const char *, NODE *, fsnode *);
61 static	fsnode	*create_fsnode(const char *, const char *, const char *,
62 			       struct stat *);
63 
64 
65 /*
66  * walk_dir --
67  *	build a tree of fsnodes from `root' and `dir', with a parent
68  *	fsnode of `parent' (which may be NULL for the root of the tree).
69  *	append the tree to a fsnode of `join' if it is not NULL.
70  *	each "level" is a directory, with the "." entry guaranteed to be
71  *	at the start of the list, and without ".." entries.
72  */
73 fsnode *
74 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
75 {
76 	fsnode		*first, *cur, *prev, *last;
77 	DIR		*dirp;
78 	struct dirent	*dent;
79 	char		path[MAXPATHLEN + 1];
80 	struct stat	stbuf;
81 	char		*name, *rp;
82 	int		dot, len;
83 
84 	assert(root != NULL);
85 	assert(dir != NULL);
86 
87 	len = snprintf(path, sizeof(path), "%s/%s", root, dir);
88 	if (len >= (int)sizeof(path))
89 		errx(1, "Pathname too long.");
90 	if (debug & DEBUG_WALK_DIR)
91 		printf("walk_dir: %s %p\n", path, parent);
92 	if ((dirp = opendir(path)) == NULL)
93 		err(1, "Can't opendir `%s'", path);
94 	rp = path + strlen(root) + 1;
95 	if (join != NULL) {
96 		first = cur = join;
97 		while (cur->next != NULL)
98 			cur = cur->next;
99 		prev = cur;
100 	} else
101 		first = prev = NULL;
102 	last = prev;
103 	while ((dent = readdir(dirp)) != NULL) {
104 		name = dent->d_name;
105 		dot = 0;
106 		if (name[0] == '.')
107 			switch (name[1]) {
108 			case '\0':	/* "." */
109 				if (join != NULL)
110 					continue;
111 				dot = 1;
112 				break;
113 			case '.':	/* ".." */
114 				if (name[2] == '\0')
115 					continue;
116 				/* FALLTHROUGH */
117 			default:
118 				dot = 0;
119 			}
120 		if (debug & DEBUG_WALK_DIR_NODE)
121 			printf("scanning %s/%s/%s\n", root, dir, name);
122 		if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
123 		    (int)sizeof(path) - len)
124 			errx(1, "Pathname too long.");
125 		if (lstat(path, &stbuf) == -1)
126 			err(1, "Can't lstat `%s'", path);
127 #ifdef S_ISSOCK
128 		if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
129 			if (debug & DEBUG_WALK_DIR_NODE)
130 				printf("  skipping socket %s\n", path);
131 			continue;
132 		}
133 #endif
134 
135 		if (join != NULL) {
136 			cur = join->next;
137 			for (;;) {
138 				if (cur == NULL || strcmp(cur->name, name) == 0)
139 					break;
140 				if (cur == last) {
141 					cur = NULL;
142 					break;
143 				}
144 				cur = cur->next;
145 			}
146 			if (cur != NULL) {
147 				if (S_ISDIR(cur->type) &&
148 				    S_ISDIR(stbuf.st_mode)) {
149 					if (debug & DEBUG_WALK_DIR_NODE)
150 						printf("merging %s with %p\n",
151 						    path, cur->child);
152 					cur->child = walk_dir(root, rp, cur,
153 					    cur->child);
154 					continue;
155 				}
156 				errx(1, "Can't merge %s `%s' with existing %s",
157 				    inode_type(stbuf.st_mode), path,
158 				    inode_type(cur->type));
159 			}
160 		}
161 
162 		cur = create_fsnode(root, dir, name, &stbuf);
163 		cur->parent = parent;
164 		if (dot) {
165 				/* ensure "." is at the start of the list */
166 			cur->next = first;
167 			first = cur;
168 			if (! prev)
169 				prev = cur;
170 			cur->first = first;
171 		} else {			/* not "." */
172 			if (prev)
173 				prev->next = cur;
174 			prev = cur;
175 			if (!first)
176 				first = cur;
177 			cur->first = first;
178 			if (S_ISDIR(cur->type)) {
179 				cur->child = walk_dir(root, rp, cur, NULL);
180 				continue;
181 			}
182 		}
183 		if (stbuf.st_nlink > 1) {
184 			fsinode	*curino;
185 
186 			curino = link_check(cur->inode);
187 			if (curino != NULL) {
188 				free(cur->inode);
189 				cur->inode = curino;
190 				cur->inode->nlink++;
191 				if (debug & DEBUG_WALK_DIR_LINKCHECK)
192 					printf("link_check: found [%llu, %llu]\n",
193 					    (unsigned long long)curino->st.st_dev,
194 					    (unsigned long long)curino->st.st_ino);
195 			}
196 		}
197 		if (S_ISLNK(cur->type)) {
198 			char	slink[PATH_MAX+1];
199 			int	llen;
200 
201 			llen = readlink(path, slink, sizeof(slink) - 1);
202 			if (llen == -1)
203 				err(1, "Readlink `%s'", path);
204 			slink[llen] = '\0';
205 			cur->symlink = estrdup(slink);
206 		}
207 	}
208 	assert(first != NULL);
209 	if (join == NULL)
210 		for (cur = first->next; cur != NULL; cur = cur->next)
211 			cur->first = first;
212 	if (closedir(dirp) == -1)
213 		err(1, "Can't closedir `%s/%s'", root, dir);
214 	return (first);
215 }
216 
217 static fsnode *
218 create_fsnode(const char *root, const char *path, const char *name,
219     struct stat *stbuf)
220 {
221 	fsnode *cur;
222 
223 	cur = ecalloc(1, sizeof(*cur));
224 	cur->path = estrdup(path);
225 	cur->name = estrdup(name);
226 	cur->inode = ecalloc(1, sizeof(*cur->inode));
227 	cur->root = root;
228 	cur->type = stbuf->st_mode & S_IFMT;
229 	cur->inode->nlink = 1;
230 	cur->inode->st = *stbuf;
231 	if (stampst.st_ino) {
232 		cur->inode->st.st_atime = stampst.st_atime;
233 		cur->inode->st.st_mtime = stampst.st_mtime;
234 		cur->inode->st.st_ctime = stampst.st_ctime;
235 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
236 		cur->inode->st.st_atimensec = stampst.st_atimensec;
237 		cur->inode->st.st_mtimensec = stampst.st_mtimensec;
238 		cur->inode->st.st_ctimensec = stampst.st_ctimensec;
239 #endif
240 #if HAVE_STRUCT_STAT_BIRTHTIME
241 		cur->inode->st.st_birthtime = stampst.st_birthtime;
242 		cur->inode->st.st_birthtimensec = stampst.st_birthtimensec;
243 #endif
244 	}
245 	return (cur);
246 }
247 
248 /*
249  * free_fsnodes --
250  *	Removes node from tree and frees it and all of
251  *   its descendants.
252  */
253 void
254 free_fsnodes(fsnode *node)
255 {
256 	fsnode	*cur, *next;
257 
258 	assert(node != NULL);
259 
260 	/* for ".", start with actual parent node */
261 	if (node->first == node) {
262 		assert(node->name[0] == '.' && node->name[1] == '\0');
263 		if (node->parent) {
264 			assert(node->parent->child == node);
265 			node = node->parent;
266 		}
267 	}
268 
269 	/* Find ourselves in our sibling list and unlink */
270 	if (node->first != node) {
271 		for (cur = node->first; cur->next; cur = cur->next) {
272 			if (cur->next == node) {
273 				cur->next = node->next;
274 				node->next = NULL;
275 				break;
276 			}
277 		}
278 	}
279 
280 	for (cur = node; cur != NULL; cur = next) {
281 		next = cur->next;
282 		if (cur->child) {
283 			cur->child->parent = NULL;
284 			free_fsnodes(cur->child);
285 		}
286 		if (cur->inode->nlink-- == 1)
287 			free(cur->inode);
288 		if (cur->symlink)
289 			free(cur->symlink);
290 		free(cur->path);
291 		free(cur->name);
292 		free(cur);
293 	}
294 }
295 
296 /*
297  * apply_specfile --
298  *	read in the mtree(8) specfile, and apply it to the tree
299  *	at dir,parent. parameters in parent on equivalent types
300  *	will be changed to those found in specfile, and missing
301  *	entries will be added.
302  */
303 void
304 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
305 {
306 	struct timeval	 start;
307 	FILE	*fp;
308 	NODE	*root;
309 
310 	assert(specfile != NULL);
311 	assert(parent != NULL);
312 
313 	if (debug & DEBUG_APPLY_SPECFILE)
314 		printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
315 
316 				/* read in the specfile */
317 	if ((fp = fopen(specfile, "r")) == NULL)
318 		err(1, "Can't open `%s'", specfile);
319 	TIMER_START(start);
320 	root = spec(fp);
321 	TIMER_RESULTS(start, "spec");
322 	if (fclose(fp) == EOF)
323 		err(1, "Can't close `%s'", specfile);
324 
325 				/* perform some sanity checks */
326 	if (root == NULL)
327 		errx(1, "Specfile `%s' did not contain a tree", specfile);
328 	assert(strcmp(root->name, ".") == 0);
329 	assert(root->type == F_DIR);
330 
331 				/* merge in the changes */
332 	apply_specdir(dir, root, parent, speconly);
333 
334 	free_nodes(root);
335 }
336 
337 static void
338 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
339 {
340 	char	 path[MAXPATHLEN + 1];
341 	NODE	*curnode;
342 	fsnode	*curfsnode;
343 
344 	assert(specnode != NULL);
345 	assert(dirnode != NULL);
346 
347 	if (debug & DEBUG_APPLY_SPECFILE)
348 		printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
349 
350 	if (specnode->type != F_DIR)
351 		errx(1, "Specfile node `%s/%s' is not a directory",
352 		    dir, specnode->name);
353 	if (dirnode->type != S_IFDIR)
354 		errx(1, "Directory node `%s/%s' is not a directory",
355 		    dir, dirnode->name);
356 
357 	apply_specentry(dir, specnode, dirnode);
358 
359 	/* Remove any filesystem nodes not found in specfile */
360 	/* XXX inefficient.  This is O^2 in each dir and it would
361 	 * have been better never to have walked this part of the tree
362 	 * to begin with
363 	 */
364 	if (speconly) {
365 		fsnode *next;
366 		assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
367 		for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
368 			next = curfsnode->next;
369 			for (curnode = specnode->child; curnode != NULL;
370 			     curnode = curnode->next) {
371 				if (strcmp(curnode->name, curfsnode->name) == 0)
372 					break;
373 			}
374 			if (curnode == NULL) {
375 				if (debug & DEBUG_APPLY_SPECONLY) {
376 					printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
377 				}
378 				free_fsnodes(curfsnode);
379 			}
380 		}
381 	}
382 
383 			/* now walk specnode->child matching up with dirnode */
384 	for (curnode = specnode->child; curnode != NULL;
385 	    curnode = curnode->next) {
386 		if (debug & DEBUG_APPLY_SPECENTRY)
387 			printf("apply_specdir:  spec %s\n",
388 			    curnode->name);
389 		for (curfsnode = dirnode->next; curfsnode != NULL;
390 		    curfsnode = curfsnode->next) {
391 #if 0	/* too verbose for now */
392 			if (debug & DEBUG_APPLY_SPECENTRY)
393 				printf("apply_specdir:  dirent %s\n",
394 				    curfsnode->name);
395 #endif
396 			if (strcmp(curnode->name, curfsnode->name) == 0)
397 				break;
398 		}
399 		if (snprintf(path, sizeof(path), "%s/%s",
400 		    dir, curnode->name) >= sizeof(path))
401 			errx(1, "Pathname too long.");
402 		if (curfsnode == NULL) {	/* need new entry */
403 			struct stat	stbuf;
404 
405 					    /*
406 					     * don't add optional spec entries
407 					     * that lack an existing fs entry
408 					     */
409 			if ((curnode->flags & F_OPT) &&
410 			    lstat(path, &stbuf) == -1)
411 					continue;
412 
413 					/* check that enough info is provided */
414 #define NODETEST(t, m)							\
415 			if (!(t))					\
416 				errx(1, "`%s': %s not provided", path, m)
417 			NODETEST(curnode->flags & F_TYPE, "type");
418 			NODETEST(curnode->flags & F_MODE, "mode");
419 				/* XXX: require F_TIME ? */
420 			NODETEST(curnode->flags & F_GID ||
421 			    curnode->flags & F_GNAME, "group");
422 			NODETEST(curnode->flags & F_UID ||
423 			    curnode->flags & F_UNAME, "user");
424 /*			if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
425 				NODETEST(curnode->flags & F_DEV,
426 				    "device number");*/
427 #undef NODETEST
428 
429 			if (debug & DEBUG_APPLY_SPECFILE)
430 				printf("apply_specdir: adding %s\n",
431 				    curnode->name);
432 					/* build minimal fsnode */
433 			memset(&stbuf, 0, sizeof(stbuf));
434 			stbuf.st_mode = nodetoino(curnode->type);
435 			stbuf.st_nlink = 1;
436 			stbuf.st_mtime = stbuf.st_atime =
437 			    stbuf.st_ctime = start_time.tv_sec;
438 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
439 			stbuf.st_mtimensec = stbuf.st_atimensec =
440 			    stbuf.st_ctimensec = start_time.tv_nsec;
441 #endif
442 			curfsnode = create_fsnode(".", ".", curnode->name,
443 			    &stbuf);
444 			curfsnode->parent = dirnode->parent;
445 			curfsnode->first = dirnode;
446 			curfsnode->next = dirnode->next;
447 			dirnode->next = curfsnode;
448 			if (curfsnode->type == S_IFDIR) {
449 					/* for dirs, make "." entry as well */
450 				curfsnode->child = create_fsnode(".", ".", ".",
451 				    &stbuf);
452 				curfsnode->child->parent = curfsnode;
453 				curfsnode->child->first = curfsnode->child;
454 			}
455 			if (curfsnode->type == S_IFLNK) {
456 				assert(curnode->slink != NULL);
457 					/* for symlinks, copy the target */
458 				curfsnode->symlink = estrdup(curnode->slink);
459 			}
460 		}
461 		apply_specentry(dir, curnode, curfsnode);
462 		if (curnode->type == F_DIR) {
463 			if (curfsnode->type != S_IFDIR)
464 				errx(1, "`%s' is not a directory", path);
465 			assert (curfsnode->child != NULL);
466 			apply_specdir(path, curnode, curfsnode->child, speconly);
467 		}
468 	}
469 }
470 
471 static void
472 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
473 {
474 
475 	assert(specnode != NULL);
476 	assert(dirnode != NULL);
477 
478 	if (nodetoino(specnode->type) != dirnode->type)
479 		errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
480 		    dir, specnode->name, inode_type(nodetoino(specnode->type)),
481 		    inode_type(dirnode->type));
482 
483 	if (debug & DEBUG_APPLY_SPECENTRY)
484 		printf("apply_specentry: %s/%s\n", dir, dirnode->name);
485 
486 #define ASEPRINT(t, b, o, n) \
487 		if (debug & DEBUG_APPLY_SPECENTRY) \
488 			printf("\t\t\tchanging %s from " b " to " b "\n", \
489 			    t, o, n)
490 
491 	if (specnode->flags & (F_GID | F_GNAME)) {
492 		ASEPRINT("gid", "%d",
493 		    dirnode->inode->st.st_gid, specnode->st_gid);
494 		dirnode->inode->st.st_gid = specnode->st_gid;
495 	}
496 	if (specnode->flags & F_MODE) {
497 		ASEPRINT("mode", "%#o",
498 		    dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
499 		dirnode->inode->st.st_mode &= ~ALLPERMS;
500 		dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
501 	}
502 		/* XXX: ignoring F_NLINK for now */
503 	if (specnode->flags & F_SIZE) {
504 		ASEPRINT("size", "%lld",
505 		    (long long)dirnode->inode->st.st_size,
506 		    (long long)specnode->st_size);
507 		dirnode->inode->st.st_size = specnode->st_size;
508 	}
509 	if (specnode->flags & F_SLINK) {
510 		assert(dirnode->symlink != NULL);
511 		assert(specnode->slink != NULL);
512 		ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
513 		free(dirnode->symlink);
514 		dirnode->symlink = estrdup(specnode->slink);
515 	}
516 	if (specnode->flags & F_TIME) {
517 		ASEPRINT("time", "%ld",
518 		    (long)dirnode->inode->st.st_mtime,
519 		    (long)specnode->st_mtimespec.tv_sec);
520 		dirnode->inode->st.st_mtime =		specnode->st_mtimespec.tv_sec;
521 		dirnode->inode->st.st_atime =		specnode->st_mtimespec.tv_sec;
522 		dirnode->inode->st.st_ctime =		start_time.tv_sec;
523 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
524 		dirnode->inode->st.st_mtimensec =	specnode->st_mtimespec.tv_nsec;
525 		dirnode->inode->st.st_atimensec =	specnode->st_mtimespec.tv_nsec;
526 		dirnode->inode->st.st_ctimensec =	start_time.tv_nsec;
527 #endif
528 	}
529 	if (specnode->flags & (F_UID | F_UNAME)) {
530 		ASEPRINT("uid", "%d",
531 		    dirnode->inode->st.st_uid, specnode->st_uid);
532 		dirnode->inode->st.st_uid = specnode->st_uid;
533 	}
534 #if HAVE_STRUCT_STAT_ST_FLAGS
535 	if (specnode->flags & F_FLAGS) {
536 		ASEPRINT("flags", "%#lX",
537 		    (unsigned long)dirnode->inode->st.st_flags,
538 		    (unsigned long)specnode->st_flags);
539 		dirnode->inode->st.st_flags = specnode->st_flags;
540 	}
541 #endif
542 /*	if (specnode->flags & F_DEV) {
543 		ASEPRINT("rdev", "%#llx",
544 		    (unsigned long long)dirnode->inode->st.st_rdev,
545 		    (unsigned long long)specnode->st_rdev);
546 		dirnode->inode->st.st_rdev = specnode->st_rdev;
547 	}*/
548 #undef ASEPRINT
549 
550 	dirnode->flags |= FSNODE_F_HASSPEC;
551 }
552 
553 
554 /*
555  * dump_fsnodes --
556  *	dump the fsnodes from `cur'
557  */
558 void
559 dump_fsnodes(fsnode *root)
560 {
561 	fsnode	*cur;
562 	char	path[MAXPATHLEN + 1];
563 
564 	printf("dump_fsnodes: %s %p\n", root->path, root);
565 	for (cur = root; cur != NULL; cur = cur->next) {
566 		if (snprintf(path, sizeof(path), "%s/%s", cur->path,
567 		    cur->name) >= (int)sizeof(path))
568 			errx(1, "Pathname too long.");
569 
570 		if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
571 			printf("cur=%8p parent=%8p first=%8p ",
572 			    cur, cur->parent, cur->first);
573 		printf("%7s: %s", inode_type(cur->type), path);
574 		if (S_ISLNK(cur->type)) {
575 			assert(cur->symlink != NULL);
576 			printf(" -> %s", cur->symlink);
577 		} else {
578 			assert (cur->symlink == NULL);
579 		}
580 		if (cur->inode->nlink > 1)
581 			printf(", nlinks=%d", cur->inode->nlink);
582 		putchar('\n');
583 
584 		if (cur->child) {
585 			assert (cur->type == S_IFDIR);
586 			dump_fsnodes(cur->child);
587 		}
588 	}
589 	printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
590 }
591 
592 
593 /*
594  * inode_type --
595  *	for a given inode type `mode', return a descriptive string.
596  *	for most cases, uses inotype() from mtree/misc.c
597  */
598 const char *
599 inode_type(mode_t mode)
600 {
601 	if (S_ISREG(mode))
602 		return ("file");
603 	if (S_ISLNK(mode))
604 		return ("symlink");
605 	if (S_ISDIR(mode))
606 		return ("dir");
607 	if (S_ISLNK(mode))
608 		return ("link");
609 	if (S_ISFIFO(mode))
610 		return ("fifo");
611 	if (S_ISSOCK(mode))
612 		return ("socket");
613 	/* XXX should not happen but handle them */
614 	if (S_ISCHR(mode))
615 		return ("char");
616 	if (S_ISBLK(mode))
617 		return ("block");
618 	return ("unknown");
619 }
620 
621 
622 /*
623  * link_check --
624  *	return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
625  *	otherwise add `entry' to table and return NULL
626  */
627 /* This was borrowed from du.c and tweaked to keep an fsnode
628  * pointer instead. -- dbj@netbsd.org
629  */
630 fsinode *
631 link_check(fsinode *entry)
632 {
633 	static struct entry {
634 		fsinode *data;
635 	} *htable;
636 	static int htshift;  /* log(allocated size) */
637 	static int htmask;   /* allocated size - 1 */
638 	static int htused;   /* 2*number of insertions */
639 	int h, h2;
640 	uint64_t tmp;
641 	/* this constant is (1<<64)/((1+sqrt(5))/2)
642 	 * aka (word size)/(golden ratio)
643 	 */
644 	const uint64_t HTCONST = 11400714819323198485ULL;
645 	const int HTBITS = 64;
646 
647 	/* Never store zero in hashtable */
648 	assert(entry);
649 
650 	/* Extend hash table if necessary, keep load under 0.5 */
651 	if (htused<<1 >= htmask) {
652 		struct entry *ohtable;
653 
654 		if (!htable)
655 			htshift = 10;   /* starting hashtable size */
656 		else
657 			htshift++;   /* exponential hashtable growth */
658 
659 		htmask  = (1 << htshift) - 1;
660 		htused = 0;
661 
662 		ohtable = htable;
663 		htable = ecalloc(htmask+1, sizeof(*htable));
664 		/* populate newly allocated hashtable */
665 		if (ohtable) {
666 			int i;
667 			for (i = 0; i <= htmask>>1; i++)
668 				if (ohtable[i].data)
669 					link_check(ohtable[i].data);
670 			free(ohtable);
671 		}
672 	}
673 
674 	/* multiplicative hashing */
675 	tmp = entry->st.st_dev;
676 	tmp <<= HTBITS>>1;
677 	tmp |=  entry->st.st_ino;
678 	tmp *= HTCONST;
679 	h  = tmp >> (HTBITS - htshift);
680 	h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
681 
682 	/* open address hashtable search with double hash probing */
683 	while (htable[h].data) {
684 		if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
685 		    (htable[h].data->st.st_dev == entry->st.st_dev)) {
686 			return htable[h].data;
687 		}
688 		h = (h + h2) & htmask;
689 	}
690 
691 	/* Insert the current entry into hashtable */
692 	htable[h].data = entry;
693 	htused++;
694 	return NULL;
695 }
696