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