15184Sek110237 /*
25184Sek110237  * CDDL HEADER START
35184Sek110237  *
45184Sek110237  * The contents of this file are subject to the terms of the
55184Sek110237  * Common Development and Distribution License (the "License").
65184Sek110237  * You may not use this file except in compliance with the License.
75184Sek110237  *
85184Sek110237  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95184Sek110237  * or http://www.opensolaris.org/os/licensing.
105184Sek110237  * See the License for the specific language governing permissions
115184Sek110237  * and limitations under the License.
125184Sek110237  *
135184Sek110237  * When distributing Covered Code, include this CDDL HEADER in each
145184Sek110237  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155184Sek110237  * If applicable, add the following below this CDDL HEADER, with the
165184Sek110237  * fields enclosed by brackets "[]" replaced with your own identifying
175184Sek110237  * information: Portions Copyright [yyyy] [name of copyright owner]
185184Sek110237  *
195184Sek110237  * CDDL HEADER END
205184Sek110237  */
215184Sek110237 /*
228615SAndrew.W.Wilson@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
246613Sek110237  *
256613Sek110237  * Portions Copyright 2008 Denis Cheng
265184Sek110237  */
275184Sek110237 
285184Sek110237 #include <fcntl.h>
295184Sek110237 #include <pthread.h>
305184Sek110237 #include <errno.h>
315184Sek110237 #include <math.h>
325184Sek110237 #include <libgen.h>
335184Sek110237 #include <sys/mman.h>
348404SAndrew.W.Wilson@sun.com #include <sys/shm.h>
356613Sek110237 
366613Sek110237 #include "filebench.h"
375184Sek110237 #include "fileset.h"
385184Sek110237 #include "gamma_dist.h"
397946SAndrew.W.Wilson@sun.com #include "utils.h"
408615SAndrew.W.Wilson@sun.com #include "fsplug.h"
415184Sek110237 
425184Sek110237 /*
435184Sek110237  * File sets, of type fileset_t, are entities which contain
445184Sek110237  * information about collections of files and subdirectories in Filebench.
455184Sek110237  * The fileset, once populated, consists of a tree of fileset entries of
465184Sek110237  * type filesetentry_t which specify files and directories.  The fileset
476212Saw148015  * is rooted in a directory specified by fileset_path, and once the populated
485184Sek110237  * fileset has been created, has a tree of directories and files
495184Sek110237  * corresponding to the fileset's filesetentry tree.
506701Saw148015  *
517556SAndrew.W.Wilson@sun.com  * Fileset entities are allocated by fileset_define() which is called from
527556SAndrew.W.Wilson@sun.com  * parser_gram.y: parser_fileset_define(). The filesetentry tree corrseponding
537556SAndrew.W.Wilson@sun.com  * to the eventual directory and file tree to be instantiated on the storage
547946SAndrew.W.Wilson@sun.com  * medium is built by fileset_populate(), which is This routine is called
557946SAndrew.W.Wilson@sun.com  * from fileset_createset(), which is in turn called by fileset_createset().
567946SAndrew.W.Wilson@sun.com  * After calling fileset_populate(), fileset_createset() will call
577946SAndrew.W.Wilson@sun.com  * fileset_create() to pre-allocate designated files and directories.
587556SAndrew.W.Wilson@sun.com  *
597556SAndrew.W.Wilson@sun.com  * Fileset_createset() is called from parser_gram.y: parser_create_fileset()
607556SAndrew.W.Wilson@sun.com  * when a "create fileset" or "run" command is encountered. When the
617556SAndrew.W.Wilson@sun.com  * "create fileset" command is used, it is generally paired with
626701Saw148015  * a "create processes" command, and must appear first, in order to
636701Saw148015  * instantiate all the files in the fileset before trying to use them.
645184Sek110237  */
655184Sek110237 
666305Saw148015 static int fileset_checkraw(fileset_t *fileset);
676305Saw148015 
687556SAndrew.W.Wilson@sun.com /* maximum parallel allocation control */
695673Saw148015 #define	MAX_PARALLOC_THREADS 32
705673Saw148015 
715673Saw148015 /*
725673Saw148015  * returns pointer to file or fileset
735673Saw148015  * string, as appropriate
745673Saw148015  */
755673Saw148015 static char *
765673Saw148015 fileset_entity_name(fileset_t *fileset)
775673Saw148015 {
785673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE)
795673Saw148015 		return ("file");
805673Saw148015 	else
815673Saw148015 		return ("fileset");
825673Saw148015 }
835673Saw148015 
845184Sek110237 /*
855184Sek110237  * Removes the last file or directory name from a pathname.
865184Sek110237  * Basically removes characters from the end of the path by
875184Sek110237  * setting them to \0 until a forward slash '/' is
885184Sek110237  * encountered. It also removes the forward slash.
895184Sek110237  */
905184Sek110237 static char *
915184Sek110237 trunc_dirname(char *dir)
925184Sek110237 {
935184Sek110237 	char *s = dir + strlen(dir);
945184Sek110237 
955184Sek110237 	while (s != dir) {
965184Sek110237 		int c = *s;
975184Sek110237 
985184Sek110237 		*s = 0;
995184Sek110237 		if (c == '/')
1005184Sek110237 			break;
1015184Sek110237 		s--;
1025184Sek110237 	}
1035184Sek110237 	return (dir);
1045184Sek110237 }
1055184Sek110237 
1065184Sek110237 /*
1075184Sek110237  * Prints a list of allowed options and how to specify them.
1085184Sek110237  */
1095184Sek110237 void
1105184Sek110237 fileset_usage(void)
1115184Sek110237 {
1125673Saw148015 	(void) fprintf(stderr,
1135673Saw148015 	    "define [file name=<name> | fileset name=<name>],path=<pathname>,"
1145673Saw148015 	    ",entries=<number>\n");
1155673Saw148015 	(void) fprintf(stderr,
1166212Saw148015 	    "		        [,filesize=[size]]\n");
1176212Saw148015 	(void) fprintf(stderr,
1185673Saw148015 	    "		        [,dirwidth=[width]]\n");
1195673Saw148015 	(void) fprintf(stderr,
1206212Saw148015 	    "		        [,dirdepthrv=$random_variable_name]\n");
1216212Saw148015 	(void) fprintf(stderr,
1225673Saw148015 	    "		        [,dirgamma=[100-10000]] "
1235184Sek110237 	    "(Gamma * 1000)\n");
1245184Sek110237 	(void) fprintf(stderr,
1255673Saw148015 	    "		        [,sizegamma=[100-10000]] (Gamma * 1000)\n");
1265184Sek110237 	(void) fprintf(stderr,
1275184Sek110237 	    "		        [,prealloc=[percent]]\n");
1285673Saw148015 	(void) fprintf(stderr, "		        [,paralloc]\n");
1295184Sek110237 	(void) fprintf(stderr, "		        [,reuse]\n");
1305184Sek110237 	(void) fprintf(stderr, "\n");
1315184Sek110237 }
1325184Sek110237 
1335184Sek110237 /*
1345184Sek110237  * Creates a path string from the filesetentry_t "*entry"
1355184Sek110237  * and all of its parent's path names. The resulting path
1365184Sek110237  * is a concatination of all the individual parent paths.
1375184Sek110237  * Allocates memory for the path string and returns a
1385184Sek110237  * pointer to it.
1395184Sek110237  */
1405184Sek110237 char *
1415184Sek110237 fileset_resolvepath(filesetentry_t *entry)
1425184Sek110237 {
1435184Sek110237 	filesetentry_t *fsep = entry;
1445184Sek110237 	char path[MAXPATHLEN];
1455184Sek110237 	char pathtmp[MAXPATHLEN];
1465184Sek110237 	char *s;
1475184Sek110237 
1487946SAndrew.W.Wilson@sun.com 	path[0] = '\0';
1495184Sek110237 	while (fsep->fse_parent) {
1505184Sek110237 		(void) strcpy(pathtmp, "/");
1517946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(pathtmp, fsep->fse_path, MAXPATHLEN);
1527946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(pathtmp, path, MAXPATHLEN);
1537946SAndrew.W.Wilson@sun.com 		(void) fb_strlcpy(path, pathtmp, MAXPATHLEN);
1545184Sek110237 		fsep = fsep->fse_parent;
1555184Sek110237 	}
1565184Sek110237 
1575184Sek110237 	s = malloc(strlen(path) + 1);
1587946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(s, path, MAXPATHLEN);
1595184Sek110237 	return (s);
1605184Sek110237 }
1615184Sek110237 
1625184Sek110237 /*
1635184Sek110237  * Creates multiple nested directories as required by the
1645184Sek110237  * supplied path. Starts at the end of the path, creating
1655184Sek110237  * a list of directories to mkdir, up to the root of the
1665184Sek110237  * path, then mkdirs them one at a time from the root on down.
1675184Sek110237  */
1685184Sek110237 static int
1695184Sek110237 fileset_mkdir(char *path, int mode)
1705184Sek110237 {
1715184Sek110237 	char *p;
1725184Sek110237 	char *dirs[65536];
1735184Sek110237 	int i = 0;
1745184Sek110237 
1755184Sek110237 	if ((p = strdup(path)) == NULL)
1765184Sek110237 		goto null_str;
1775184Sek110237 
1785184Sek110237 	/*
1795184Sek110237 	 * Fill an array of subdirectory path names until either we
1805184Sek110237 	 * reach the root or encounter an already existing subdirectory
1815184Sek110237 	 */
1825184Sek110237 	/* CONSTCOND */
1835184Sek110237 	while (1) {
1845184Sek110237 		struct stat64 sb;
1855184Sek110237 
1865184Sek110237 		if (stat64(p, &sb) == 0)
1875184Sek110237 			break;
1885184Sek110237 		if (strlen(p) < 3)
1895184Sek110237 			break;
1905184Sek110237 		if ((dirs[i] = strdup(p)) == NULL) {
1915184Sek110237 			free(p);
1925184Sek110237 			goto null_str;
1935184Sek110237 		}
1945184Sek110237 
1955184Sek110237 		(void) trunc_dirname(p);
1965184Sek110237 		i++;
1975184Sek110237 	}
1985184Sek110237 
1995184Sek110237 	/* Make the directories, from closest to root downwards. */
2005184Sek110237 	for (--i; i >= 0; i--) {
2018615SAndrew.W.Wilson@sun.com 		(void) FB_MKDIR(dirs[i], mode);
2025184Sek110237 		free(dirs[i]);
2035184Sek110237 	}
2045184Sek110237 
2055184Sek110237 	free(p);
2067556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2075184Sek110237 
2085184Sek110237 null_str:
2095184Sek110237 	/* clean up */
2105184Sek110237 	for (--i; i >= 0; i--)
2115184Sek110237 		free(dirs[i]);
2125184Sek110237 
2135184Sek110237 	filebench_log(LOG_ERROR,
2145184Sek110237 	    "Failed to create directory path %s: Out of memory", path);
2157556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_ERROR);
2165184Sek110237 }
2175184Sek110237 
2185673Saw148015 /*
2195673Saw148015  * creates the subdirectory tree for a fileset.
2205673Saw148015  */
2215673Saw148015 static int
2225673Saw148015 fileset_create_subdirs(fileset_t *fileset, char *filesetpath)
2235673Saw148015 {
2245673Saw148015 	filesetentry_t *direntry;
2255673Saw148015 	char full_path[MAXPATHLEN];
2265673Saw148015 	char *part_path;
2275673Saw148015 
2285673Saw148015 	/* walk the subdirectory list, enstanciating subdirs */
2295673Saw148015 	direntry = fileset->fs_dirlist;
2305673Saw148015 	while (direntry) {
2317946SAndrew.W.Wilson@sun.com 		(void) fb_strlcpy(full_path, filesetpath, MAXPATHLEN);
2325673Saw148015 		part_path = fileset_resolvepath(direntry);
2337946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(full_path, part_path, MAXPATHLEN);
2345673Saw148015 		free(part_path);
2355673Saw148015 
2365673Saw148015 		/* now create this portion of the subdirectory tree */
2377556SAndrew.W.Wilson@sun.com 		if (fileset_mkdir(full_path, 0755) == FILEBENCH_ERROR)
2387556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
2395673Saw148015 
2408404SAndrew.W.Wilson@sun.com 		direntry = direntry->fse_nextoftype;
2415673Saw148015 	}
2427556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2435673Saw148015 }
2445673Saw148015 
2455673Saw148015 /*
2468404SAndrew.W.Wilson@sun.com  * move filesetentry between exist tree and non-exist tree, source_tree
2478404SAndrew.W.Wilson@sun.com  * to destination tree.
2488404SAndrew.W.Wilson@sun.com  */
2498404SAndrew.W.Wilson@sun.com static void
2508404SAndrew.W.Wilson@sun.com fileset_move_entry(avl_tree_t *src_tree, avl_tree_t *dst_tree,
2518404SAndrew.W.Wilson@sun.com     filesetentry_t *entry)
2528404SAndrew.W.Wilson@sun.com {
2538404SAndrew.W.Wilson@sun.com 	avl_remove(src_tree, entry);
2548404SAndrew.W.Wilson@sun.com 	avl_add(dst_tree, entry);
2558404SAndrew.W.Wilson@sun.com }
2568404SAndrew.W.Wilson@sun.com 
2578404SAndrew.W.Wilson@sun.com /*
2587946SAndrew.W.Wilson@sun.com  * given a fileset entry, determines if the associated leaf directory
2597946SAndrew.W.Wilson@sun.com  * needs to be made or not, and if so does the mkdir.
2607946SAndrew.W.Wilson@sun.com  */
2617946SAndrew.W.Wilson@sun.com static int
2627946SAndrew.W.Wilson@sun.com fileset_alloc_leafdir(filesetentry_t *entry)
2637946SAndrew.W.Wilson@sun.com {
2647946SAndrew.W.Wilson@sun.com 	fileset_t *fileset;
2657946SAndrew.W.Wilson@sun.com 	char path[MAXPATHLEN];
2667946SAndrew.W.Wilson@sun.com 	struct stat64 sb;
2677946SAndrew.W.Wilson@sun.com 	char *pathtmp;
2687946SAndrew.W.Wilson@sun.com 
2697946SAndrew.W.Wilson@sun.com 	fileset = entry->fse_fileset;
2707946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
2717946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
2727946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
2737946SAndrew.W.Wilson@sun.com 	pathtmp = fileset_resolvepath(entry);
2747946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
2757946SAndrew.W.Wilson@sun.com 	free(pathtmp);
2767946SAndrew.W.Wilson@sun.com 
2777946SAndrew.W.Wilson@sun.com 	filebench_log(LOG_DEBUG_IMPL, "Populated %s", entry->fse_path);
2787946SAndrew.W.Wilson@sun.com 
2797946SAndrew.W.Wilson@sun.com 	/* see if not reusing and this directory does not exist */
2807946SAndrew.W.Wilson@sun.com 	if (!((entry->fse_flags & FSE_REUSING) && (stat64(path, &sb) == 0))) {
2817946SAndrew.W.Wilson@sun.com 
2827946SAndrew.W.Wilson@sun.com 		/* No file or not reusing, so create */
2838615SAndrew.W.Wilson@sun.com 		if (FB_MKDIR(path, 0755) < 0) {
2847946SAndrew.W.Wilson@sun.com 			filebench_log(LOG_ERROR,
2857946SAndrew.W.Wilson@sun.com 			    "Failed to pre-allocate leaf directory %s: %s",
2867946SAndrew.W.Wilson@sun.com 			    path, strerror(errno));
2878404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
2887946SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
2897946SAndrew.W.Wilson@sun.com 		}
2907946SAndrew.W.Wilson@sun.com 	}
2917946SAndrew.W.Wilson@sun.com 
2928404SAndrew.W.Wilson@sun.com 	/* unbusy the allocated entry */
2938404SAndrew.W.Wilson@sun.com 	fileset_unbusy(entry, TRUE, TRUE, 0);
2947946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2957946SAndrew.W.Wilson@sun.com }
2967946SAndrew.W.Wilson@sun.com 
2977946SAndrew.W.Wilson@sun.com /*
2985673Saw148015  * given a fileset entry, determines if the associated file
2995673Saw148015  * needs to be allocated or not, and if so does the allocation.
3005673Saw148015  */
3015673Saw148015 static int
3025673Saw148015 fileset_alloc_file(filesetentry_t *entry)
3035673Saw148015 {
3047946SAndrew.W.Wilson@sun.com 	fileset_t *fileset;
3055673Saw148015 	char path[MAXPATHLEN];
3065673Saw148015 	char *buf;
3075673Saw148015 	struct stat64 sb;
3085673Saw148015 	char *pathtmp;
3095673Saw148015 	off64_t seek;
3108615SAndrew.W.Wilson@sun.com 	fb_fdesc_t fdesc;
311*9326SAndrew.W.Wilson@sun.com 	int trust_tree;
3125673Saw148015 
3137946SAndrew.W.Wilson@sun.com 	fileset = entry->fse_fileset;
3147946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
3157946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
3167946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
3175673Saw148015 	pathtmp = fileset_resolvepath(entry);
3187946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
3197946SAndrew.W.Wilson@sun.com 	free(pathtmp);
3205673Saw148015 
3215673Saw148015 	filebench_log(LOG_DEBUG_IMPL, "Populated %s", entry->fse_path);
3225673Saw148015 
3235673Saw148015 	/* see if reusing and this file exists */
324*9326SAndrew.W.Wilson@sun.com 	trust_tree = avd_get_bool(fileset->fs_trust_tree);
325*9326SAndrew.W.Wilson@sun.com 	if ((entry->fse_flags & FSE_REUSING) && (trust_tree ||
326*9326SAndrew.W.Wilson@sun.com 	    (FB_STAT(path, &sb) == 0))) {
3278615SAndrew.W.Wilson@sun.com 		if (FB_OPEN(&fdesc, path, O_RDWR, 0) == FILEBENCH_ERROR) {
3285673Saw148015 			filebench_log(LOG_INFO,
3295673Saw148015 			    "Attempted but failed to Re-use file %s",
3305673Saw148015 			    path);
3318404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
3327556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
3335673Saw148015 		}
3345673Saw148015 
335*9326SAndrew.W.Wilson@sun.com 		if (trust_tree || (sb.st_size == (off64_t)entry->fse_size)) {
3367556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_IMPL,
3375673Saw148015 			    "Re-using file %s", path);
3385673Saw148015 
3397946SAndrew.W.Wilson@sun.com 			if (!avd_get_bool(fileset->fs_cached))
3408615SAndrew.W.Wilson@sun.com 				(void) FB_FREEMEM(&fdesc, entry->fse_size);
3415673Saw148015 
3428615SAndrew.W.Wilson@sun.com 			(void) FB_CLOSE(&fdesc);
3436701Saw148015 
3448404SAndrew.W.Wilson@sun.com 			/* unbusy the allocated entry */
3458404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, TRUE, 0);
3467556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
3475673Saw148015 
3485673Saw148015 		} else if (sb.st_size > (off64_t)entry->fse_size) {
3495673Saw148015 			/* reuse, but too large */
3508404SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_IMPL,
3515673Saw148015 			    "Truncating & re-using file %s", path);
3525673Saw148015 
3538615SAndrew.W.Wilson@sun.com 			(void) FB_FTRUNC(&fdesc, (off64_t)entry->fse_size);
3545673Saw148015 
3557946SAndrew.W.Wilson@sun.com 			if (!avd_get_bool(fileset->fs_cached))
3568615SAndrew.W.Wilson@sun.com 				(void) FB_FREEMEM(&fdesc, entry->fse_size);
3575673Saw148015 
3588615SAndrew.W.Wilson@sun.com 			(void) FB_CLOSE(&fdesc);
3596701Saw148015 
3608404SAndrew.W.Wilson@sun.com 			/* unbusy the allocated entry */
3618404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, TRUE, 0);
3627556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
3635673Saw148015 		}
3645673Saw148015 	} else {
3655673Saw148015 
3665673Saw148015 		/* No file or not reusing, so create */
3678615SAndrew.W.Wilson@sun.com 		if (FB_OPEN(&fdesc, path, O_RDWR | O_CREAT, 0644) ==
3688615SAndrew.W.Wilson@sun.com 		    FILEBENCH_ERROR) {
3695673Saw148015 			filebench_log(LOG_ERROR,
3705673Saw148015 			    "Failed to pre-allocate file %s: %s",
3715673Saw148015 			    path, strerror(errno));
3725673Saw148015 
3738404SAndrew.W.Wilson@sun.com 			/* unbusy the unallocated entry */
3748404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
3757556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
3765673Saw148015 		}
3775673Saw148015 	}
3785673Saw148015 
3798404SAndrew.W.Wilson@sun.com 	if ((buf = (char *)malloc(FILE_ALLOC_BLOCK)) == NULL) {
3808404SAndrew.W.Wilson@sun.com 		/* unbusy the unallocated entry */
3818404SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, TRUE, FALSE, 0);
3827556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
3838404SAndrew.W.Wilson@sun.com 	}
3846701Saw148015 
3855673Saw148015 	for (seek = 0; seek < entry->fse_size; ) {
3865673Saw148015 		off64_t wsize;
3875673Saw148015 		int ret = 0;
3885673Saw148015 
3895673Saw148015 		/*
3905673Saw148015 		 * Write FILE_ALLOC_BLOCK's worth,
3915673Saw148015 		 * except on last write
3925673Saw148015 		 */
3935673Saw148015 		wsize = MIN(entry->fse_size - seek, FILE_ALLOC_BLOCK);
3945673Saw148015 
3958615SAndrew.W.Wilson@sun.com 		ret = FB_WRITE(&fdesc, buf, wsize);
3965673Saw148015 		if (ret != wsize) {
3975673Saw148015 			filebench_log(LOG_ERROR,
3985673Saw148015 			    "Failed to pre-allocate file %s: %s",
3995673Saw148015 			    path, strerror(errno));
4008615SAndrew.W.Wilson@sun.com 			(void) FB_CLOSE(&fdesc);
4015673Saw148015 			free(buf);
4028404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
4037556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
4045673Saw148015 		}
4055673Saw148015 		seek += wsize;
4065673Saw148015 	}
4075673Saw148015 
4087946SAndrew.W.Wilson@sun.com 	if (!avd_get_bool(fileset->fs_cached))
4098615SAndrew.W.Wilson@sun.com 		(void) FB_FREEMEM(&fdesc, entry->fse_size);
4105673Saw148015 
4118615SAndrew.W.Wilson@sun.com 	(void) FB_CLOSE(&fdesc);
4125673Saw148015 
4135673Saw148015 	free(buf);
4145673Saw148015 
4158404SAndrew.W.Wilson@sun.com 	/* unbusy the allocated entry */
4168404SAndrew.W.Wilson@sun.com 	fileset_unbusy(entry, TRUE, TRUE, 0);
4178404SAndrew.W.Wilson@sun.com 
4185673Saw148015 	filebench_log(LOG_DEBUG_IMPL,
4196286Saw148015 	    "Pre-allocated file %s size %llu",
4206286Saw148015 	    path, (u_longlong_t)entry->fse_size);
4215673Saw148015 
4227556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
4235673Saw148015 }
4245673Saw148015 
4255673Saw148015 /*
4265673Saw148015  * given a fileset entry, determines if the associated file
4275673Saw148015  * needs to be allocated or not, and if so does the allocation.
4287556SAndrew.W.Wilson@sun.com  * Sets shm_fsparalloc_count to -1 on error.
4295673Saw148015  */
4305673Saw148015 static void *
4315673Saw148015 fileset_alloc_thread(filesetentry_t *entry)
4325673Saw148015 {
4337556SAndrew.W.Wilson@sun.com 	if (fileset_alloc_file(entry) == FILEBENCH_ERROR) {
4347556SAndrew.W.Wilson@sun.com 		(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
4357556SAndrew.W.Wilson@sun.com 		filebench_shm->shm_fsparalloc_count = -1;
4365673Saw148015 	} else {
4377556SAndrew.W.Wilson@sun.com 		(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
4387556SAndrew.W.Wilson@sun.com 		filebench_shm->shm_fsparalloc_count--;
4395673Saw148015 	}
4405673Saw148015 
4417556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_signal(&filebench_shm->shm_fsparalloc_cv);
4427556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_unlock(&filebench_shm->shm_fsparalloc_lock);
4435673Saw148015 
4445673Saw148015 	pthread_exit(NULL);
4455673Saw148015 	return (NULL);
4465673Saw148015 }
4475673Saw148015 
4485184Sek110237 
4495184Sek110237 /*
4505184Sek110237  * First creates the parent directories of the file using
4515184Sek110237  * fileset_mkdir(). Then Optionally sets the O_DSYNC flag
4525184Sek110237  * and opens the file with open64(). It unlocks the fileset
4535184Sek110237  * entry lock, sets the DIRECTIO_ON or DIRECTIO_OFF flags
4545184Sek110237  * as requested, and returns the file descriptor integer
4558615SAndrew.W.Wilson@sun.com  * for the opened file in the supplied filebench file descriptor.
4568615SAndrew.W.Wilson@sun.com  * Returns FILEBENCH_ERROR on error, and FILEBENCH_OK on success.
4575184Sek110237  */
4585184Sek110237 int
4598615SAndrew.W.Wilson@sun.com fileset_openfile(fb_fdesc_t *fdesc, fileset_t *fileset,
4607736SAndrew.W.Wilson@sun.com     filesetentry_t *entry, int flag, int filemode, int attrs)
4615184Sek110237 {
4625184Sek110237 	char path[MAXPATHLEN];
4635184Sek110237 	char dir[MAXPATHLEN];
4645184Sek110237 	char *pathtmp;
4655184Sek110237 	struct stat64 sb;
4665184Sek110237 	int open_attrs = 0;
4675184Sek110237 
4687946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
4697946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
4707946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
4715184Sek110237 	pathtmp = fileset_resolvepath(entry);
4727946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
4737946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(dir, path, MAXPATHLEN);
4745184Sek110237 	free(pathtmp);
4755184Sek110237 	(void) trunc_dirname(dir);
4765184Sek110237 
4775184Sek110237 	/* If we are going to create a file, create the parent dirs */
4785184Sek110237 	if ((flag & O_CREAT) && (stat64(dir, &sb) != 0)) {
4797556SAndrew.W.Wilson@sun.com 		if (fileset_mkdir(dir, 0755) == FILEBENCH_ERROR)
4807556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
4816701Saw148015 	}
4826701Saw148015 
4835184Sek110237 	if (attrs & FLOW_ATTR_DSYNC) {
4845184Sek110237 #ifdef sun
4855184Sek110237 		open_attrs |= O_DSYNC;
4865184Sek110237 #else
4875184Sek110237 		open_attrs |= O_FSYNC;
4885184Sek110237 #endif
4895184Sek110237 	}
4905184Sek110237 
4918615SAndrew.W.Wilson@sun.com 	if (FB_OPEN(fdesc, path, flag | open_attrs, filemode)
4928615SAndrew.W.Wilson@sun.com 	    == FILEBENCH_ERROR) {
4935184Sek110237 		filebench_log(LOG_ERROR,
4948404SAndrew.W.Wilson@sun.com 		    "Failed to open file %d, %s, with status %x: %s",
4958404SAndrew.W.Wilson@sun.com 		    entry->fse_index, path, entry->fse_flags, strerror(errno));
4967556SAndrew.W.Wilson@sun.com 
4978404SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, FALSE, FALSE, 0);
4987556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
4995184Sek110237 	}
5007556SAndrew.W.Wilson@sun.com 
5017556SAndrew.W.Wilson@sun.com 	if (flag & O_CREAT)
5028404SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, TRUE, TRUE, 1);
5037556SAndrew.W.Wilson@sun.com 	else
5048404SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, FALSE, FALSE, 1);
5055184Sek110237 
5065184Sek110237 #ifdef sun
5075184Sek110237 	if (attrs & FLOW_ATTR_DIRECTIO)
5088615SAndrew.W.Wilson@sun.com 		(void) directio(fdesc->fd_num, DIRECTIO_ON);
5095184Sek110237 	else
5108615SAndrew.W.Wilson@sun.com 		(void) directio(fdesc->fd_num, DIRECTIO_OFF);
5115184Sek110237 #endif
5125184Sek110237 
5138615SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
5145184Sek110237 }
5155184Sek110237 
5168404SAndrew.W.Wilson@sun.com /*
5178404SAndrew.W.Wilson@sun.com  * removes all filesetentries from their respective btrees, and puts them
5188404SAndrew.W.Wilson@sun.com  * on the free list. The supplied argument indicates which free list to
5198404SAndrew.W.Wilson@sun.com  * use.
5208404SAndrew.W.Wilson@sun.com  */
5218404SAndrew.W.Wilson@sun.com static void
5228404SAndrew.W.Wilson@sun.com fileset_pickreset(fileset_t *fileset, int entry_type)
5238404SAndrew.W.Wilson@sun.com {
5248404SAndrew.W.Wilson@sun.com 	filesetentry_t	*entry;
5258404SAndrew.W.Wilson@sun.com 
5268404SAndrew.W.Wilson@sun.com 	switch (entry_type & FILESET_PICKMASK) {
5278404SAndrew.W.Wilson@sun.com 	case FILESET_PICKFILE:
5288404SAndrew.W.Wilson@sun.com 		entry = (filesetentry_t *)avl_first(&fileset->fs_noex_files);
5298404SAndrew.W.Wilson@sun.com 
5308404SAndrew.W.Wilson@sun.com 		/* make sure non-existing files are marked free */
5318404SAndrew.W.Wilson@sun.com 		while (entry) {
5328404SAndrew.W.Wilson@sun.com 			entry->fse_flags |= FSE_FREE;
5338404SAndrew.W.Wilson@sun.com 			entry->fse_open_cnt = 0;
5348404SAndrew.W.Wilson@sun.com 			fileset_move_entry(&fileset->fs_noex_files,
5358404SAndrew.W.Wilson@sun.com 			    &fileset->fs_free_files, entry);
5368404SAndrew.W.Wilson@sun.com 			entry =  AVL_NEXT(&fileset->fs_noex_files, entry);
5378404SAndrew.W.Wilson@sun.com 		}
5388404SAndrew.W.Wilson@sun.com 
5398404SAndrew.W.Wilson@sun.com 		/* free up any existing files */
5408404SAndrew.W.Wilson@sun.com 		entry = (filesetentry_t *)avl_first(&fileset->fs_exist_files);
5418404SAndrew.W.Wilson@sun.com 
5428404SAndrew.W.Wilson@sun.com 		while (entry) {
5438404SAndrew.W.Wilson@sun.com 			entry->fse_flags |= FSE_FREE;
5448404SAndrew.W.Wilson@sun.com 			entry->fse_open_cnt = 0;
5458404SAndrew.W.Wilson@sun.com 			fileset_move_entry(&fileset->fs_exist_files,
5468404SAndrew.W.Wilson@sun.com 			    &fileset->fs_free_files, entry);
5478404SAndrew.W.Wilson@sun.com 
5488404SAndrew.W.Wilson@sun.com 			entry =  AVL_NEXT(&fileset->fs_exist_files, entry);
5498404SAndrew.W.Wilson@sun.com 		}
5508404SAndrew.W.Wilson@sun.com 
5518404SAndrew.W.Wilson@sun.com 		break;
5528404SAndrew.W.Wilson@sun.com 
5538404SAndrew.W.Wilson@sun.com 	case FILESET_PICKDIR:
5548404SAndrew.W.Wilson@sun.com 		/* nothing to reset, as all (sub)dirs always exist */
5558404SAndrew.W.Wilson@sun.com 		break;
5568404SAndrew.W.Wilson@sun.com 
5578404SAndrew.W.Wilson@sun.com 	case FILESET_PICKLEAFDIR:
5588404SAndrew.W.Wilson@sun.com 		entry = (filesetentry_t *)
5598404SAndrew.W.Wilson@sun.com 		    avl_first(&fileset->fs_noex_leaf_dirs);
5608404SAndrew.W.Wilson@sun.com 
5618404SAndrew.W.Wilson@sun.com 		/* make sure non-existing leaf dirs are marked free */
5628404SAndrew.W.Wilson@sun.com 		while (entry) {
5638404SAndrew.W.Wilson@sun.com 			entry->fse_flags |= FSE_FREE;
5648404SAndrew.W.Wilson@sun.com 			entry->fse_open_cnt = 0;
5658404SAndrew.W.Wilson@sun.com 			fileset_move_entry(&fileset->fs_noex_leaf_dirs,
5668404SAndrew.W.Wilson@sun.com 			    &fileset->fs_free_leaf_dirs, entry);
5678404SAndrew.W.Wilson@sun.com 			entry =  AVL_NEXT(&fileset->fs_noex_leaf_dirs, entry);
5688404SAndrew.W.Wilson@sun.com 		}
5698404SAndrew.W.Wilson@sun.com 
5708404SAndrew.W.Wilson@sun.com 		/* free up any existing leaf dirs */
5718404SAndrew.W.Wilson@sun.com 		entry = (filesetentry_t *)
5728404SAndrew.W.Wilson@sun.com 		    avl_first(&fileset->fs_exist_leaf_dirs);
5738404SAndrew.W.Wilson@sun.com 
5748404SAndrew.W.Wilson@sun.com 		while (entry) {
5758404SAndrew.W.Wilson@sun.com 			entry->fse_flags |= FSE_FREE;
5768404SAndrew.W.Wilson@sun.com 			entry->fse_open_cnt = 0;
5778404SAndrew.W.Wilson@sun.com 			fileset_move_entry(&fileset->fs_exist_leaf_dirs,
5788404SAndrew.W.Wilson@sun.com 			    &fileset->fs_free_leaf_dirs, entry);
5798404SAndrew.W.Wilson@sun.com 
5808404SAndrew.W.Wilson@sun.com 			entry =  AVL_NEXT(&fileset->fs_exist_leaf_dirs, entry);
5818404SAndrew.W.Wilson@sun.com 		}
5828404SAndrew.W.Wilson@sun.com 
5838404SAndrew.W.Wilson@sun.com 		break;
5848404SAndrew.W.Wilson@sun.com 	}
5858404SAndrew.W.Wilson@sun.com }
5868404SAndrew.W.Wilson@sun.com 
5878404SAndrew.W.Wilson@sun.com /*
5888404SAndrew.W.Wilson@sun.com  * find a filesetentry from the fileset using the supplied index
5898404SAndrew.W.Wilson@sun.com  */
5908404SAndrew.W.Wilson@sun.com static filesetentry_t *
5918404SAndrew.W.Wilson@sun.com fileset_find_entry(avl_tree_t *atp, uint_t index)
5928404SAndrew.W.Wilson@sun.com {
5938404SAndrew.W.Wilson@sun.com 	avl_index_t	found_loc;
5948404SAndrew.W.Wilson@sun.com 	filesetentry_t	desired_fse, *found_fse;
5958404SAndrew.W.Wilson@sun.com 
5968404SAndrew.W.Wilson@sun.com 	/* find the file with the desired index, if it is in the tree */
5978404SAndrew.W.Wilson@sun.com 	desired_fse.fse_index = index;
5988404SAndrew.W.Wilson@sun.com 	found_fse = avl_find(atp, (void *)(&desired_fse), &found_loc);
5998404SAndrew.W.Wilson@sun.com 	if (found_fse != NULL)
6008404SAndrew.W.Wilson@sun.com 		return (found_fse);
6018404SAndrew.W.Wilson@sun.com 
6028404SAndrew.W.Wilson@sun.com 	/* if requested node not found, find next higher node */
6038404SAndrew.W.Wilson@sun.com 	found_fse = avl_nearest(atp, found_loc, AVL_AFTER);
6048404SAndrew.W.Wilson@sun.com 	if (found_fse != NULL)
6058404SAndrew.W.Wilson@sun.com 		return (found_fse);
6068404SAndrew.W.Wilson@sun.com 
6078404SAndrew.W.Wilson@sun.com 	/* might have hit the end, return lowest available index node */
6088404SAndrew.W.Wilson@sun.com 	found_fse = avl_first(atp);
6098404SAndrew.W.Wilson@sun.com 	return (found_fse);
6108404SAndrew.W.Wilson@sun.com }
6115184Sek110237 
6125184Sek110237 /*
6135184Sek110237  * Selects a fileset entry from a fileset. If the
6147946SAndrew.W.Wilson@sun.com  * FILESET_PICKLEAFDIR flag is set it will pick a leaf directory entry,
6157946SAndrew.W.Wilson@sun.com  * if the FILESET_PICKDIR flag is set it will pick a non leaf directory
6168404SAndrew.W.Wilson@sun.com  * entry, otherwise a file entry. The FILESET_PICKUNIQUE
6175184Sek110237  * flag will take an entry off of one of the free (unused)
6185184Sek110237  * lists (file or directory), otherwise the entry will be
6195184Sek110237  * picked off of one of the rotor lists (file or directory).
6205184Sek110237  * The FILESET_PICKEXISTS will insure that only extant
6215184Sek110237  * (FSE_EXISTS) state files are selected, while
6225184Sek110237  * FILESET_PICKNOEXIST insures that only non extant
6235184Sek110237  * (not FSE_EXISTS) state files are selected.
6246391Saw148015  * Note that the selected fileset entry (file) is returned
6257556SAndrew.W.Wilson@sun.com  * with its FSE_BUSY flag (in fse_flags) set.
6265184Sek110237  */
6275184Sek110237 filesetentry_t *
6288404SAndrew.W.Wilson@sun.com fileset_pick(fileset_t *fileset, int flags, int tid, int index)
6295184Sek110237 {
6305184Sek110237 	filesetentry_t *entry = NULL;
6318404SAndrew.W.Wilson@sun.com 	filesetentry_t *start_point;
6328404SAndrew.W.Wilson@sun.com 	avl_tree_t *atp;
6338404SAndrew.W.Wilson@sun.com 	fbint_t max_entries;
6345184Sek110237 
6357556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
6367556SAndrew.W.Wilson@sun.com 
6377556SAndrew.W.Wilson@sun.com 	/* see if we have to wait for available files or directories */
6387946SAndrew.W.Wilson@sun.com 	switch (flags & FILESET_PICKMASK) {
6397946SAndrew.W.Wilson@sun.com 	case FILESET_PICKFILE:
6407946SAndrew.W.Wilson@sun.com 		if (fileset->fs_filelist == NULL)
6417946SAndrew.W.Wilson@sun.com 			goto empty;
6428404SAndrew.W.Wilson@sun.com 
6437556SAndrew.W.Wilson@sun.com 		while (fileset->fs_idle_files == 0) {
6447556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_idle_files_cv,
6457556SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
6467556SAndrew.W.Wilson@sun.com 		}
6478404SAndrew.W.Wilson@sun.com 
6488404SAndrew.W.Wilson@sun.com 		max_entries = fileset->fs_constentries;
6498404SAndrew.W.Wilson@sun.com 		if (flags & FILESET_PICKUNIQUE) {
6508404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_free_files;
6518404SAndrew.W.Wilson@sun.com 		} else if (flags & FILESET_PICKNOEXIST) {
6528404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_noex_files;
6538404SAndrew.W.Wilson@sun.com 		} else {
6548404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_exist_files;
6558404SAndrew.W.Wilson@sun.com 		}
6567946SAndrew.W.Wilson@sun.com 		break;
6578404SAndrew.W.Wilson@sun.com 
6587946SAndrew.W.Wilson@sun.com 	case FILESET_PICKDIR:
6597946SAndrew.W.Wilson@sun.com 		if (fileset->fs_dirlist == NULL)
6607946SAndrew.W.Wilson@sun.com 			goto empty;
6618404SAndrew.W.Wilson@sun.com 
6627946SAndrew.W.Wilson@sun.com 		while (fileset->fs_idle_dirs == 0) {
6637946SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_idle_dirs_cv,
6647946SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
6657946SAndrew.W.Wilson@sun.com 		}
6668404SAndrew.W.Wilson@sun.com 
6678404SAndrew.W.Wilson@sun.com 		max_entries = 1;
6688404SAndrew.W.Wilson@sun.com 		atp = &fileset->fs_dirs;
6697946SAndrew.W.Wilson@sun.com 		break;
6708404SAndrew.W.Wilson@sun.com 
6717946SAndrew.W.Wilson@sun.com 	case FILESET_PICKLEAFDIR:
6727946SAndrew.W.Wilson@sun.com 		if (fileset->fs_leafdirlist == NULL)
6737946SAndrew.W.Wilson@sun.com 			goto empty;
6748404SAndrew.W.Wilson@sun.com 
6757946SAndrew.W.Wilson@sun.com 		while (fileset->fs_idle_leafdirs == 0) {
6767946SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_idle_leafdirs_cv,
6777946SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
6787946SAndrew.W.Wilson@sun.com 		}
6798404SAndrew.W.Wilson@sun.com 
6808404SAndrew.W.Wilson@sun.com 		max_entries = fileset->fs_constleafdirs;
6818404SAndrew.W.Wilson@sun.com 		if (flags & FILESET_PICKUNIQUE) {
6828404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_free_leaf_dirs;
6838404SAndrew.W.Wilson@sun.com 		} else if (flags & FILESET_PICKNOEXIST) {
6848404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_noex_leaf_dirs;
6858404SAndrew.W.Wilson@sun.com 		} else {
6868404SAndrew.W.Wilson@sun.com 			atp = &fileset->fs_exist_leaf_dirs;
6878404SAndrew.W.Wilson@sun.com 		}
6887946SAndrew.W.Wilson@sun.com 		break;
6897556SAndrew.W.Wilson@sun.com 	}
6905184Sek110237 
6916701Saw148015 	/* see if asking for impossible */
6928404SAndrew.W.Wilson@sun.com 	if (avl_is_empty(atp))
6938404SAndrew.W.Wilson@sun.com 		goto empty;
6948404SAndrew.W.Wilson@sun.com 
6958404SAndrew.W.Wilson@sun.com 	if (flags & FILESET_PICKUNIQUE) {
6968404SAndrew.W.Wilson@sun.com 		uint64_t  index64;
6978404SAndrew.W.Wilson@sun.com 
6988404SAndrew.W.Wilson@sun.com 		/*
6998404SAndrew.W.Wilson@sun.com 		 * pick at random from free list in order to
7008404SAndrew.W.Wilson@sun.com 		 * distribute initially allocated files more
7018404SAndrew.W.Wilson@sun.com 		 * randomly on storage media. Use uniform
7028404SAndrew.W.Wilson@sun.com 		 * random number generator to select index
7038404SAndrew.W.Wilson@sun.com 		 * if it is not supplied with pick call.
7048404SAndrew.W.Wilson@sun.com 		 */
7058404SAndrew.W.Wilson@sun.com 		if (index) {
7068404SAndrew.W.Wilson@sun.com 			index64 = index;
7078404SAndrew.W.Wilson@sun.com 		} else {
7088404SAndrew.W.Wilson@sun.com 			if (filebench_randomno64(&index64, max_entries, 1,
7098404SAndrew.W.Wilson@sun.com 			    NULL) == FILEBENCH_ERROR)
7107946SAndrew.W.Wilson@sun.com 				return (NULL);
7115184Sek110237 		}
7125184Sek110237 
7138404SAndrew.W.Wilson@sun.com 		entry = fileset_find_entry(atp, (int)index64);
7148404SAndrew.W.Wilson@sun.com 
7158404SAndrew.W.Wilson@sun.com 		if (entry == NULL)
7168404SAndrew.W.Wilson@sun.com 			goto empty;
7178404SAndrew.W.Wilson@sun.com 
7188404SAndrew.W.Wilson@sun.com 	} else if (flags & FILESET_PICKBYINDEX) {
7198404SAndrew.W.Wilson@sun.com 		/* pick by supplied index */
7208404SAndrew.W.Wilson@sun.com 		entry = fileset_find_entry(atp, index);
7218404SAndrew.W.Wilson@sun.com 
7228404SAndrew.W.Wilson@sun.com 	} else {
7238404SAndrew.W.Wilson@sun.com 		/* pick in rotation */
7248404SAndrew.W.Wilson@sun.com 		switch (flags & FILESET_PICKMASK) {
7258404SAndrew.W.Wilson@sun.com 		case FILESET_PICKFILE:
7268404SAndrew.W.Wilson@sun.com 			if (flags & FILESET_PICKNOEXIST) {
7278404SAndrew.W.Wilson@sun.com 				entry = fileset_find_entry(atp,
7288404SAndrew.W.Wilson@sun.com 				    fileset->fs_file_nerotor);
7298404SAndrew.W.Wilson@sun.com 				fileset->fs_file_nerotor =
7308404SAndrew.W.Wilson@sun.com 				    entry->fse_index + 1;
7318404SAndrew.W.Wilson@sun.com 			} else {
7328404SAndrew.W.Wilson@sun.com 				entry = fileset_find_entry(atp,
7338404SAndrew.W.Wilson@sun.com 				    fileset->fs_file_exrotor[tid]);
7348404SAndrew.W.Wilson@sun.com 				fileset->fs_file_exrotor[tid] =
7358404SAndrew.W.Wilson@sun.com 				    entry->fse_index + 1;
7365184Sek110237 			}
7378404SAndrew.W.Wilson@sun.com 			break;
7388404SAndrew.W.Wilson@sun.com 
7398404SAndrew.W.Wilson@sun.com 		case FILESET_PICKDIR:
7408404SAndrew.W.Wilson@sun.com 			entry = fileset_find_entry(atp, fileset->fs_dirrotor);
7418404SAndrew.W.Wilson@sun.com 			fileset->fs_dirrotor = entry->fse_index + 1;
7428404SAndrew.W.Wilson@sun.com 			break;
7438404SAndrew.W.Wilson@sun.com 
7448404SAndrew.W.Wilson@sun.com 		case FILESET_PICKLEAFDIR:
7458404SAndrew.W.Wilson@sun.com 			if (flags & FILESET_PICKNOEXIST) {
7468404SAndrew.W.Wilson@sun.com 				entry = fileset_find_entry(atp,
7478404SAndrew.W.Wilson@sun.com 				    fileset->fs_leafdir_nerotor);
7488404SAndrew.W.Wilson@sun.com 				fileset->fs_leafdir_nerotor =
7498404SAndrew.W.Wilson@sun.com 				    entry->fse_index + 1;
7508404SAndrew.W.Wilson@sun.com 			} else {
7518404SAndrew.W.Wilson@sun.com 				entry = fileset_find_entry(atp,
7528404SAndrew.W.Wilson@sun.com 				    fileset->fs_leafdir_exrotor);
7538404SAndrew.W.Wilson@sun.com 				fileset->fs_leafdir_exrotor =
7548404SAndrew.W.Wilson@sun.com 				    entry->fse_index + 1;
7555184Sek110237 			}
7568404SAndrew.W.Wilson@sun.com 			break;
7578404SAndrew.W.Wilson@sun.com 		}
7588404SAndrew.W.Wilson@sun.com 	}
7598404SAndrew.W.Wilson@sun.com 
7608404SAndrew.W.Wilson@sun.com 	if (entry == NULL)
7618404SAndrew.W.Wilson@sun.com 		goto empty;
7628404SAndrew.W.Wilson@sun.com 
7638404SAndrew.W.Wilson@sun.com 	/* see if entry in use */
7648404SAndrew.W.Wilson@sun.com 	start_point = entry;
7658404SAndrew.W.Wilson@sun.com 	while (entry->fse_flags & FSE_BUSY) {
7668404SAndrew.W.Wilson@sun.com 
7678404SAndrew.W.Wilson@sun.com 		/* it is, so try next */
7688404SAndrew.W.Wilson@sun.com 		entry = AVL_NEXT(atp, entry);
7698404SAndrew.W.Wilson@sun.com 		if (entry == NULL)
7708404SAndrew.W.Wilson@sun.com 			entry = avl_first(atp);
7718404SAndrew.W.Wilson@sun.com 
7728404SAndrew.W.Wilson@sun.com 		/* see if we have wrapped around */
7738404SAndrew.W.Wilson@sun.com 		if ((entry == NULL) || (entry == start_point)) {
7748404SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_SCRIPT,
7758404SAndrew.W.Wilson@sun.com 			    "All %d files are busy", avl_numnodes(atp));
7768404SAndrew.W.Wilson@sun.com 			goto empty;
7775184Sek110237 		}
7785184Sek110237 
7795184Sek110237 	}
7805184Sek110237 
7817556SAndrew.W.Wilson@sun.com 	/* update file or directory idle counts */
7827946SAndrew.W.Wilson@sun.com 	switch (flags & FILESET_PICKMASK) {
7837946SAndrew.W.Wilson@sun.com 	case FILESET_PICKFILE:
7847946SAndrew.W.Wilson@sun.com 		fileset->fs_idle_files--;
7857946SAndrew.W.Wilson@sun.com 		break;
7867946SAndrew.W.Wilson@sun.com 	case FILESET_PICKDIR:
7877556SAndrew.W.Wilson@sun.com 		fileset->fs_idle_dirs--;
7887946SAndrew.W.Wilson@sun.com 		break;
7897946SAndrew.W.Wilson@sun.com 	case FILESET_PICKLEAFDIR:
7907946SAndrew.W.Wilson@sun.com 		fileset->fs_idle_leafdirs--;
7917946SAndrew.W.Wilson@sun.com 		break;
7927946SAndrew.W.Wilson@sun.com 	}
7937556SAndrew.W.Wilson@sun.com 
7947556SAndrew.W.Wilson@sun.com 	/* Indicate that file or directory is now busy */
7957556SAndrew.W.Wilson@sun.com 	entry->fse_flags |= FSE_BUSY;
7967556SAndrew.W.Wilson@sun.com 
7977556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
7985184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "Picked file %s", entry->fse_path);
7995184Sek110237 	return (entry);
8005184Sek110237 
8015184Sek110237 empty:
8028404SAndrew.W.Wilson@sun.com 	filebench_log(LOG_DEBUG_SCRIPT, "No file found");
8037556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
8045184Sek110237 	return (NULL);
8055184Sek110237 }
8065184Sek110237 
8075184Sek110237 /*
8087556SAndrew.W.Wilson@sun.com  * Removes a filesetentry from the "FSE_BUSY" state, signaling any threads
8097556SAndrew.W.Wilson@sun.com  * that are waiting for a NOT BUSY filesetentry. Also sets whether it is
8107556SAndrew.W.Wilson@sun.com  * existant or not, or leaves that designation alone.
8117556SAndrew.W.Wilson@sun.com  */
8127556SAndrew.W.Wilson@sun.com void
8138404SAndrew.W.Wilson@sun.com fileset_unbusy(filesetentry_t *entry, int update_exist,
8148404SAndrew.W.Wilson@sun.com     int new_exist_val, int open_cnt_incr)
8157556SAndrew.W.Wilson@sun.com {
8167556SAndrew.W.Wilson@sun.com 	fileset_t *fileset = NULL;
8177556SAndrew.W.Wilson@sun.com 
8187556SAndrew.W.Wilson@sun.com 	if (entry)
8197556SAndrew.W.Wilson@sun.com 		fileset = entry->fse_fileset;
8207556SAndrew.W.Wilson@sun.com 
8217556SAndrew.W.Wilson@sun.com 	if (fileset == NULL) {
8227556SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "fileset_unbusy: NO FILESET!");
8237556SAndrew.W.Wilson@sun.com 		return;
8247556SAndrew.W.Wilson@sun.com 	}
8257556SAndrew.W.Wilson@sun.com 
8267556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
8277556SAndrew.W.Wilson@sun.com 
8288404SAndrew.W.Wilson@sun.com 	/* modify FSE_EXIST flag and actual dirs/files count, if requested */
8298404SAndrew.W.Wilson@sun.com 	if (update_exist) {
8308404SAndrew.W.Wilson@sun.com 		if (new_exist_val == TRUE) {
8318404SAndrew.W.Wilson@sun.com 			if (entry->fse_flags & FSE_FREE) {
8328404SAndrew.W.Wilson@sun.com 
8338404SAndrew.W.Wilson@sun.com 				/* asked to set and it was free */
8348404SAndrew.W.Wilson@sun.com 				entry->fse_flags |= FSE_EXISTS;
8358404SAndrew.W.Wilson@sun.com 				entry->fse_flags &= (~FSE_FREE);
8368404SAndrew.W.Wilson@sun.com 				switch (entry->fse_flags & FSE_TYPE_MASK) {
8378404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_FILE:
8388404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8398404SAndrew.W.Wilson@sun.com 					    &fileset->fs_free_files,
8408404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_files, entry);
8418404SAndrew.W.Wilson@sun.com 					break;
8428404SAndrew.W.Wilson@sun.com 
8438404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_DIR:
8448404SAndrew.W.Wilson@sun.com 					break;
8458404SAndrew.W.Wilson@sun.com 
8468404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_LEAFDIR:
8478404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8488404SAndrew.W.Wilson@sun.com 					    &fileset->fs_free_leaf_dirs,
8498404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_leaf_dirs,
8508404SAndrew.W.Wilson@sun.com 					    entry);
8518404SAndrew.W.Wilson@sun.com 					break;
8528404SAndrew.W.Wilson@sun.com 				}
8538404SAndrew.W.Wilson@sun.com 
8548404SAndrew.W.Wilson@sun.com 			} else if (!(entry->fse_flags & FSE_EXISTS)) {
8558404SAndrew.W.Wilson@sun.com 
8568404SAndrew.W.Wilson@sun.com 				/* asked to set, and it was clear */
8578404SAndrew.W.Wilson@sun.com 				entry->fse_flags |= FSE_EXISTS;
8588404SAndrew.W.Wilson@sun.com 				switch (entry->fse_flags & FSE_TYPE_MASK) {
8598404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_FILE:
8608404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8618404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_files,
8628404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_files, entry);
8638404SAndrew.W.Wilson@sun.com 					break;
8648404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_DIR:
8658404SAndrew.W.Wilson@sun.com 					break;
8668404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_LEAFDIR:
8678404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8688404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_leaf_dirs,
8698404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_leaf_dirs,
8708404SAndrew.W.Wilson@sun.com 					    entry);
8718404SAndrew.W.Wilson@sun.com 					break;
8728404SAndrew.W.Wilson@sun.com 				}
8738404SAndrew.W.Wilson@sun.com 			}
8748404SAndrew.W.Wilson@sun.com 		} else {
8758404SAndrew.W.Wilson@sun.com 			if (entry->fse_flags & FSE_FREE) {
8768404SAndrew.W.Wilson@sun.com 				/* asked to clear, and it was free */
8778404SAndrew.W.Wilson@sun.com 				entry->fse_flags &= (~(FSE_FREE | FSE_EXISTS));
8788404SAndrew.W.Wilson@sun.com 				switch (entry->fse_flags & FSE_TYPE_MASK) {
8798404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_FILE:
8808404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8818404SAndrew.W.Wilson@sun.com 					    &fileset->fs_free_files,
8828404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_files, entry);
8838404SAndrew.W.Wilson@sun.com 					break;
8848404SAndrew.W.Wilson@sun.com 
8858404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_DIR:
8868404SAndrew.W.Wilson@sun.com 					break;
8878404SAndrew.W.Wilson@sun.com 
8888404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_LEAFDIR:
8898404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
8908404SAndrew.W.Wilson@sun.com 					    &fileset->fs_free_leaf_dirs,
8918404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_leaf_dirs,
8928404SAndrew.W.Wilson@sun.com 					    entry);
8938404SAndrew.W.Wilson@sun.com 					break;
8948404SAndrew.W.Wilson@sun.com 				}
8958404SAndrew.W.Wilson@sun.com 			} else if (entry->fse_flags & FSE_EXISTS) {
8968404SAndrew.W.Wilson@sun.com 
8978404SAndrew.W.Wilson@sun.com 				/* asked to clear, and it was set */
8988404SAndrew.W.Wilson@sun.com 				entry->fse_flags &= (~FSE_EXISTS);
8998404SAndrew.W.Wilson@sun.com 				switch (entry->fse_flags & FSE_TYPE_MASK) {
9008404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_FILE:
9018404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
9028404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_files,
9038404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_files, entry);
9048404SAndrew.W.Wilson@sun.com 					break;
9058404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_DIR:
9068404SAndrew.W.Wilson@sun.com 					break;
9078404SAndrew.W.Wilson@sun.com 				case FSE_TYPE_LEAFDIR:
9088404SAndrew.W.Wilson@sun.com 					fileset_move_entry(
9098404SAndrew.W.Wilson@sun.com 					    &fileset->fs_exist_leaf_dirs,
9108404SAndrew.W.Wilson@sun.com 					    &fileset->fs_noex_leaf_dirs,
9118404SAndrew.W.Wilson@sun.com 					    entry);
9128404SAndrew.W.Wilson@sun.com 					break;
9138404SAndrew.W.Wilson@sun.com 				}
9148404SAndrew.W.Wilson@sun.com 			}
9158404SAndrew.W.Wilson@sun.com 		}
9168404SAndrew.W.Wilson@sun.com 	}
9178404SAndrew.W.Wilson@sun.com 
9188404SAndrew.W.Wilson@sun.com 	/* update open count */
9198404SAndrew.W.Wilson@sun.com 	entry->fse_open_cnt += open_cnt_incr;
9208404SAndrew.W.Wilson@sun.com 
9217556SAndrew.W.Wilson@sun.com 	/* increment idle count, clear FSE_BUSY and signal IF it was busy */
9227556SAndrew.W.Wilson@sun.com 	if (entry->fse_flags & FSE_BUSY) {
9237556SAndrew.W.Wilson@sun.com 
9247556SAndrew.W.Wilson@sun.com 		/* unbusy it */
9257556SAndrew.W.Wilson@sun.com 		entry->fse_flags &= (~FSE_BUSY);
9267556SAndrew.W.Wilson@sun.com 
9277556SAndrew.W.Wilson@sun.com 		/* release any threads waiting for unbusy */
9287556SAndrew.W.Wilson@sun.com 		if (entry->fse_flags & FSE_THRD_WAITNG) {
9297556SAndrew.W.Wilson@sun.com 			entry->fse_flags &= (~FSE_THRD_WAITNG);
9307556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_broadcast(
9317556SAndrew.W.Wilson@sun.com 			    &fileset->fs_thrd_wait_cv);
9327556SAndrew.W.Wilson@sun.com 		}
9337556SAndrew.W.Wilson@sun.com 
9347556SAndrew.W.Wilson@sun.com 		/* increment idle count and signal waiting threads */
9357946SAndrew.W.Wilson@sun.com 		switch (entry->fse_flags & FSE_TYPE_MASK) {
9367946SAndrew.W.Wilson@sun.com 		case FSE_TYPE_FILE:
9377946SAndrew.W.Wilson@sun.com 			fileset->fs_idle_files++;
9387946SAndrew.W.Wilson@sun.com 			if (fileset->fs_idle_files == 1) {
9397946SAndrew.W.Wilson@sun.com 				(void) pthread_cond_signal(
9407946SAndrew.W.Wilson@sun.com 				    &fileset->fs_idle_files_cv);
9417946SAndrew.W.Wilson@sun.com 			}
9427946SAndrew.W.Wilson@sun.com 			break;
9438404SAndrew.W.Wilson@sun.com 
9447946SAndrew.W.Wilson@sun.com 		case FSE_TYPE_DIR:
9457556SAndrew.W.Wilson@sun.com 			fileset->fs_idle_dirs++;
9467556SAndrew.W.Wilson@sun.com 			if (fileset->fs_idle_dirs == 1) {
9477556SAndrew.W.Wilson@sun.com 				(void) pthread_cond_signal(
9487556SAndrew.W.Wilson@sun.com 				    &fileset->fs_idle_dirs_cv);
9497556SAndrew.W.Wilson@sun.com 			}
9507946SAndrew.W.Wilson@sun.com 			break;
9518404SAndrew.W.Wilson@sun.com 
9527946SAndrew.W.Wilson@sun.com 		case FSE_TYPE_LEAFDIR:
9537946SAndrew.W.Wilson@sun.com 			fileset->fs_idle_leafdirs++;
9547946SAndrew.W.Wilson@sun.com 			if (fileset->fs_idle_leafdirs == 1) {
9557556SAndrew.W.Wilson@sun.com 				(void) pthread_cond_signal(
9567946SAndrew.W.Wilson@sun.com 				    &fileset->fs_idle_leafdirs_cv);
9577556SAndrew.W.Wilson@sun.com 			}
9587946SAndrew.W.Wilson@sun.com 			break;
9597556SAndrew.W.Wilson@sun.com 		}
9607556SAndrew.W.Wilson@sun.com 	}
9617556SAndrew.W.Wilson@sun.com 
9627556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
9637556SAndrew.W.Wilson@sun.com }
9647556SAndrew.W.Wilson@sun.com 
9657556SAndrew.W.Wilson@sun.com /*
9665184Sek110237  * Given a fileset "fileset", create the associated files as
9675184Sek110237  * specified in the attributes of the fileset. The fileset is
9686212Saw148015  * rooted in a directory whose pathname is in fileset_path. If the
9695184Sek110237  * directory exists, meaning that there is already a fileset,
9706212Saw148015  * and the fileset_reuse attribute is false, then remove it and all
9715184Sek110237  * its contained files and subdirectories. Next, the routine
9725184Sek110237  * creates a root directory for the fileset. All the file type
9735184Sek110237  * filesetentries are cycled through creating as needed
9745184Sek110237  * their containing subdirectory trees in the filesystem and
9756212Saw148015  * creating actual files for fileset_preallocpercent of them. The
9765184Sek110237  * created files are filled with fse_size bytes of unitialized
9777556SAndrew.W.Wilson@sun.com  * data. The routine returns FILEBENCH_ERROR on errors,
9787556SAndrew.W.Wilson@sun.com  * FILEBENCH_OK on success.
9795184Sek110237  */
9805184Sek110237 static int
9815184Sek110237 fileset_create(fileset_t *fileset)
9825184Sek110237 {
9835184Sek110237 	filesetentry_t *entry;
9845184Sek110237 	char path[MAXPATHLEN];
9855184Sek110237 	struct stat64 sb;
9865184Sek110237 	hrtime_t start = gethrtime();
9876212Saw148015 	char *fileset_path;
9886212Saw148015 	char *fileset_name;
9896212Saw148015 	int randno;
9905184Sek110237 	int preallocated = 0;
9917736SAndrew.W.Wilson@sun.com 	int reusing;
9925184Sek110237 
9936212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
9945673Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
9955673Saw148015 		    fileset_entity_name(fileset));
9967556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
9975184Sek110237 	}
9985184Sek110237 
9996212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
10006212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
10016212Saw148015 		    fileset_entity_name(fileset));
10027556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
10036212Saw148015 	}
10046212Saw148015 
10055673Saw148015 #ifdef HAVE_RAW_SUPPORT
10065673Saw148015 	/* treat raw device as special case */
10075673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
10087556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
10095673Saw148015 #endif /* HAVE_RAW_SUPPORT */
10105673Saw148015 
10115184Sek110237 	/* XXX Add check to see if there is enough space */
10125184Sek110237 
10137736SAndrew.W.Wilson@sun.com 	/* set up path to fileset */
10147946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, fileset_path, MAXPATHLEN);
10157946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
10167946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, fileset_name, MAXPATHLEN);
10177736SAndrew.W.Wilson@sun.com 
1018*9326SAndrew.W.Wilson@sun.com 	/* if reusing and trusting to exist, just blindly reuse */
1019*9326SAndrew.W.Wilson@sun.com 	if (avd_get_bool(fileset->fs_trust_tree)) {
1020*9326SAndrew.W.Wilson@sun.com 		reusing = 1;
1021*9326SAndrew.W.Wilson@sun.com 
10227736SAndrew.W.Wilson@sun.com 	/* if exists and resusing, then don't create new */
1023*9326SAndrew.W.Wilson@sun.com 	} else if (((stat64(path, &sb) == 0)&& (strlen(path) > 3) &&
10247736SAndrew.W.Wilson@sun.com 	    (strlen(avd_get_str(fileset->fs_path)) > 2)) &&
10257736SAndrew.W.Wilson@sun.com 	    avd_get_bool(fileset->fs_reuse)) {
10267736SAndrew.W.Wilson@sun.com 		reusing = 1;
10277736SAndrew.W.Wilson@sun.com 	} else {
10287736SAndrew.W.Wilson@sun.com 		reusing = 0;
10297736SAndrew.W.Wilson@sun.com 	}
10307736SAndrew.W.Wilson@sun.com 
10317736SAndrew.W.Wilson@sun.com 	if (!reusing) {
10327736SAndrew.W.Wilson@sun.com 		char cmd[MAXPATHLEN];
10335184Sek110237 
10347736SAndrew.W.Wilson@sun.com 		/* Remove existing */
10357736SAndrew.W.Wilson@sun.com 		(void) snprintf(cmd, sizeof (cmd), "rm -rf %s", path);
10367736SAndrew.W.Wilson@sun.com 		(void) system(cmd);
10377736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE,
10387736SAndrew.W.Wilson@sun.com 		    "Removed any existing %s %s in %llu seconds",
10397736SAndrew.W.Wilson@sun.com 		    fileset_entity_name(fileset), fileset_name,
10407736SAndrew.W.Wilson@sun.com 		    (u_longlong_t)(((gethrtime() - start) /
10417736SAndrew.W.Wilson@sun.com 		    1000000000) + 1));
10427736SAndrew.W.Wilson@sun.com 	} else {
10437736SAndrew.W.Wilson@sun.com 		/* we are re-using */
10447736SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE, "Re-using %s %s.",
10457736SAndrew.W.Wilson@sun.com 		    fileset_entity_name(fileset), fileset_name);
10465184Sek110237 	}
10477736SAndrew.W.Wilson@sun.com 
10487736SAndrew.W.Wilson@sun.com 	/* make the filesets directory tree unless in reuse mode */
10497736SAndrew.W.Wilson@sun.com 	if (!reusing && (avd_get_bool(fileset->fs_prealloc))) {
10507946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE,
10517736SAndrew.W.Wilson@sun.com 		    "making tree for filset %s", path);
10525184Sek110237 
10538615SAndrew.W.Wilson@sun.com 		(void) FB_MKDIR(path, 0755);
10547736SAndrew.W.Wilson@sun.com 
10557736SAndrew.W.Wilson@sun.com 		if (fileset_create_subdirs(fileset, path) == FILEBENCH_ERROR)
10567736SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
10577736SAndrew.W.Wilson@sun.com 	}
10585673Saw148015 
10595184Sek110237 	start = gethrtime();
10605184Sek110237 
10615673Saw148015 	filebench_log(LOG_VERBOSE, "Creating %s %s...",
10626212Saw148015 	    fileset_entity_name(fileset), fileset_name);
10635673Saw148015 
10646212Saw148015 	randno = ((RAND_MAX * (100
10656212Saw148015 	    - avd_get_int(fileset->fs_preallocpercent))) / 100);
10666212Saw148015 
10677946SAndrew.W.Wilson@sun.com 	/* alloc any files, as required */
10688404SAndrew.W.Wilson@sun.com 	fileset_pickreset(fileset, FILESET_PICKFILE);
10698404SAndrew.W.Wilson@sun.com 	while (entry = fileset_pick(fileset,
10708404SAndrew.W.Wilson@sun.com 	    FILESET_PICKFREE | FILESET_PICKFILE, 0, 0)) {
10715673Saw148015 		pthread_t tid;
10727736SAndrew.W.Wilson@sun.com 		int newrand;
10735184Sek110237 
10747736SAndrew.W.Wilson@sun.com 		newrand = rand();
10757736SAndrew.W.Wilson@sun.com 
10768404SAndrew.W.Wilson@sun.com 		if (newrand < randno) {
10778404SAndrew.W.Wilson@sun.com 			/* unbusy the unallocated entry */
10788404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
10795184Sek110237 			continue;
10808404SAndrew.W.Wilson@sun.com 		}
10815184Sek110237 
10825184Sek110237 		preallocated++;
10835184Sek110237 
10845673Saw148015 		if (reusing)
10855673Saw148015 			entry->fse_flags |= FSE_REUSING;
10865673Saw148015 		else
10875673Saw148015 			entry->fse_flags &= (~FSE_REUSING);
10885673Saw148015 
10897556SAndrew.W.Wilson@sun.com 		/* fire off allocation threads for each file if paralloc set */
10906212Saw148015 		if (avd_get_bool(fileset->fs_paralloc)) {
10915184Sek110237 
10927556SAndrew.W.Wilson@sun.com 			/* limit total number of simultaneous allocations */
10937556SAndrew.W.Wilson@sun.com 			(void) pthread_mutex_lock(
10947556SAndrew.W.Wilson@sun.com 			    &filebench_shm->shm_fsparalloc_lock);
10957556SAndrew.W.Wilson@sun.com 			while (filebench_shm->shm_fsparalloc_count
10967556SAndrew.W.Wilson@sun.com 			    >= MAX_PARALLOC_THREADS) {
10975673Saw148015 				(void) pthread_cond_wait(
10987556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_cv,
10997556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_lock);
11005673Saw148015 			}
11015673Saw148015 
11028615SAndrew.W.Wilson@sun.com 			/* quit if any allocation thread reports an error */
11037556SAndrew.W.Wilson@sun.com 			if (filebench_shm->shm_fsparalloc_count < 0) {
11047556SAndrew.W.Wilson@sun.com 				(void) pthread_mutex_unlock(
11057556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_lock);
11067556SAndrew.W.Wilson@sun.com 				return (FILEBENCH_ERROR);
11075184Sek110237 			}
11085184Sek110237 
11097556SAndrew.W.Wilson@sun.com 			filebench_shm->shm_fsparalloc_count++;
11107556SAndrew.W.Wilson@sun.com 			(void) pthread_mutex_unlock(
11117556SAndrew.W.Wilson@sun.com 			    &filebench_shm->shm_fsparalloc_lock);
11125184Sek110237 
11137556SAndrew.W.Wilson@sun.com 			/*
11147556SAndrew.W.Wilson@sun.com 			 * Fire off a detached allocation thread per file.
11157556SAndrew.W.Wilson@sun.com 			 * The thread will self destruct when it finishes
11167556SAndrew.W.Wilson@sun.com 			 * writing pre-allocation data to the file.
11177556SAndrew.W.Wilson@sun.com 			 */
11185673Saw148015 			if (pthread_create(&tid, NULL,
11195673Saw148015 			    (void *(*)(void*))fileset_alloc_thread,
11207556SAndrew.W.Wilson@sun.com 			    entry) == 0) {
11217556SAndrew.W.Wilson@sun.com 				/*
11227556SAndrew.W.Wilson@sun.com 				 * A thread was created; detach it so it can
11237556SAndrew.W.Wilson@sun.com 				 * fully quit when finished.
11247556SAndrew.W.Wilson@sun.com 				 */
11257556SAndrew.W.Wilson@sun.com 				(void) pthread_detach(tid);
11267556SAndrew.W.Wilson@sun.com 			} else {
11275184Sek110237 				filebench_log(LOG_ERROR,
11285673Saw148015 				    "File prealloc thread create failed");
11295673Saw148015 				filebench_shutdown(1);
11305184Sek110237 			}
11315184Sek110237 
11325673Saw148015 		} else {
11337556SAndrew.W.Wilson@sun.com 			if (fileset_alloc_file(entry) == FILEBENCH_ERROR)
11347556SAndrew.W.Wilson@sun.com 				return (FILEBENCH_ERROR);
11355673Saw148015 		}
11365673Saw148015 	}
11375184Sek110237 
11387946SAndrew.W.Wilson@sun.com 	/* alloc any leaf directories, as required */
11398404SAndrew.W.Wilson@sun.com 	fileset_pickreset(fileset, FILESET_PICKLEAFDIR);
11408404SAndrew.W.Wilson@sun.com 	while (entry = fileset_pick(fileset,
11418404SAndrew.W.Wilson@sun.com 	    FILESET_PICKFREE | FILESET_PICKLEAFDIR, 0, 0)) {
11427946SAndrew.W.Wilson@sun.com 
11438404SAndrew.W.Wilson@sun.com 		if (rand() < randno) {
11448404SAndrew.W.Wilson@sun.com 			/* unbusy the unallocated entry */
11458404SAndrew.W.Wilson@sun.com 			fileset_unbusy(entry, TRUE, FALSE, 0);
11467946SAndrew.W.Wilson@sun.com 			continue;
11478404SAndrew.W.Wilson@sun.com 		}
11487946SAndrew.W.Wilson@sun.com 
11497946SAndrew.W.Wilson@sun.com 		preallocated++;
11507946SAndrew.W.Wilson@sun.com 
11517946SAndrew.W.Wilson@sun.com 		if (reusing)
11527946SAndrew.W.Wilson@sun.com 			entry->fse_flags |= FSE_REUSING;
11537946SAndrew.W.Wilson@sun.com 		else
11547946SAndrew.W.Wilson@sun.com 			entry->fse_flags &= (~FSE_REUSING);
11557946SAndrew.W.Wilson@sun.com 
11567946SAndrew.W.Wilson@sun.com 		if (fileset_alloc_leafdir(entry) == FILEBENCH_ERROR)
11577946SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
11587946SAndrew.W.Wilson@sun.com 	}
11597946SAndrew.W.Wilson@sun.com 
11605673Saw148015 exit:
11615184Sek110237 	filebench_log(LOG_VERBOSE,
11626286Saw148015 	    "Preallocated %d of %llu of %s %s in %llu seconds",
11635184Sek110237 	    preallocated,
11646286Saw148015 	    (u_longlong_t)fileset->fs_constentries,
11656212Saw148015 	    fileset_entity_name(fileset), fileset_name,
11666286Saw148015 	    (u_longlong_t)(((gethrtime() - start) / 1000000000) + 1));
11675184Sek110237 
11687556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
11695184Sek110237 }
11705184Sek110237 
11715184Sek110237 /*
11725184Sek110237  * Adds an entry to the fileset's file list. Single threaded so
11735184Sek110237  * no locking needed.
11745184Sek110237  */
11755184Sek110237 static void
11765184Sek110237 fileset_insfilelist(fileset_t *fileset, filesetentry_t *entry)
11775184Sek110237 {
11788404SAndrew.W.Wilson@sun.com 	entry->fse_flags = FSE_TYPE_FILE | FSE_FREE;
11798404SAndrew.W.Wilson@sun.com 	avl_add(&fileset->fs_free_files, entry);
11808404SAndrew.W.Wilson@sun.com 
11815184Sek110237 	if (fileset->fs_filelist == NULL) {
11825184Sek110237 		fileset->fs_filelist = entry;
11838404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = NULL;
11845184Sek110237 	} else {
11858404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = fileset->fs_filelist;
11865184Sek110237 		fileset->fs_filelist = entry;
11875184Sek110237 	}
11885184Sek110237 }
11895184Sek110237 
11905184Sek110237 /*
11915184Sek110237  * Adds an entry to the fileset's directory list. Single
11925184Sek110237  * threaded so no locking needed.
11935184Sek110237  */
11945184Sek110237 static void
11955184Sek110237 fileset_insdirlist(fileset_t *fileset, filesetentry_t *entry)
11965184Sek110237 {
11978404SAndrew.W.Wilson@sun.com 	entry->fse_flags = FSE_TYPE_DIR | FSE_EXISTS;
11988404SAndrew.W.Wilson@sun.com 	avl_add(&fileset->fs_dirs, entry);
11998404SAndrew.W.Wilson@sun.com 
12005184Sek110237 	if (fileset->fs_dirlist == NULL) {
12015184Sek110237 		fileset->fs_dirlist = entry;
12028404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = NULL;
12035184Sek110237 	} else {
12048404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = fileset->fs_dirlist;
12055184Sek110237 		fileset->fs_dirlist = entry;
12065184Sek110237 	}
12075184Sek110237 }
12085184Sek110237 
12095184Sek110237 /*
12107946SAndrew.W.Wilson@sun.com  * Adds an entry to the fileset's leaf directory list. Single
12117946SAndrew.W.Wilson@sun.com  * threaded so no locking needed.
12127946SAndrew.W.Wilson@sun.com  */
12137946SAndrew.W.Wilson@sun.com static void
12147946SAndrew.W.Wilson@sun.com fileset_insleafdirlist(fileset_t *fileset, filesetentry_t *entry)
12157946SAndrew.W.Wilson@sun.com {
12168404SAndrew.W.Wilson@sun.com 	entry->fse_flags = FSE_TYPE_LEAFDIR | FSE_FREE;
12178404SAndrew.W.Wilson@sun.com 	avl_add(&fileset->fs_free_leaf_dirs, entry);
12188404SAndrew.W.Wilson@sun.com 
12197946SAndrew.W.Wilson@sun.com 	if (fileset->fs_leafdirlist == NULL) {
12207946SAndrew.W.Wilson@sun.com 		fileset->fs_leafdirlist = entry;
12218404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = NULL;
12227946SAndrew.W.Wilson@sun.com 	} else {
12238404SAndrew.W.Wilson@sun.com 		entry->fse_nextoftype = fileset->fs_leafdirlist;
12247946SAndrew.W.Wilson@sun.com 		fileset->fs_leafdirlist = entry;
12257946SAndrew.W.Wilson@sun.com 	}
12267946SAndrew.W.Wilson@sun.com }
12277946SAndrew.W.Wilson@sun.com 
12287946SAndrew.W.Wilson@sun.com /*
12298404SAndrew.W.Wilson@sun.com  * Compares two fileset entries to determine their relative order
12308404SAndrew.W.Wilson@sun.com  */
12318404SAndrew.W.Wilson@sun.com static int
12328404SAndrew.W.Wilson@sun.com fileset_entry_compare(const void *node_1, const void *node_2)
12338404SAndrew.W.Wilson@sun.com {
12348404SAndrew.W.Wilson@sun.com 	if (((filesetentry_t *)node_1)->fse_index <
12358404SAndrew.W.Wilson@sun.com 	    ((filesetentry_t *)node_2)->fse_index)
12368404SAndrew.W.Wilson@sun.com 		return (-1);
12378404SAndrew.W.Wilson@sun.com 
12388404SAndrew.W.Wilson@sun.com 	if (((filesetentry_t *)node_1)->fse_index ==
12398404SAndrew.W.Wilson@sun.com 	    ((filesetentry_t *)node_2)->fse_index)
12408404SAndrew.W.Wilson@sun.com 		return (0);
12418404SAndrew.W.Wilson@sun.com 
12428404SAndrew.W.Wilson@sun.com 	return (1);
12438404SAndrew.W.Wilson@sun.com }
12448404SAndrew.W.Wilson@sun.com 
12458404SAndrew.W.Wilson@sun.com /*
12467946SAndrew.W.Wilson@sun.com  * Obtains a filesetentry entity for a file to be placed in a
12475184Sek110237  * (sub)directory of a fileset. The size of the file may be
12486212Saw148015  * specified by fileset_meansize, or calculated from a gamma
12496212Saw148015  * distribution of parameter fileset_sizegamma and of mean size
12506212Saw148015  * fileset_meansize. The filesetentry entity is placed on the file
12515184Sek110237  * list in the specified parent filesetentry entity, which may
12525184Sek110237  * be a directory filesetentry, or the root filesetentry in the
12535184Sek110237  * fileset. It is also placed on the fileset's list of all
12547556SAndrew.W.Wilson@sun.com  * contained files. Returns FILEBENCH_OK if successful or FILEBENCH_ERROR
12557556SAndrew.W.Wilson@sun.com  * if ipc memory for the path string cannot be allocated.
12565184Sek110237  */
12575184Sek110237 static int
12585184Sek110237 fileset_populate_file(fileset_t *fileset, filesetentry_t *parent, int serial)
12595184Sek110237 {
12605184Sek110237 	char tmpname[16];
12615184Sek110237 	filesetentry_t *entry;
12625184Sek110237 	double drand;
12638404SAndrew.W.Wilson@sun.com 	uint_t index;
12645184Sek110237 
12655184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
12665184Sek110237 	    == NULL) {
12675184Sek110237 		filebench_log(LOG_ERROR,
12685184Sek110237 		    "fileset_populate_file: Can't malloc filesetentry");
12697556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
12705184Sek110237 	}
12715184Sek110237 
12727556SAndrew.W.Wilson@sun.com 	/* Another currently idle file */
12737556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
12748404SAndrew.W.Wilson@sun.com 	index = fileset->fs_idle_files++;
12757556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
12767556SAndrew.W.Wilson@sun.com 
12778404SAndrew.W.Wilson@sun.com 	entry->fse_index = index;
12785184Sek110237 	entry->fse_parent = parent;
12795184Sek110237 	entry->fse_fileset = fileset;
12805184Sek110237 	fileset_insfilelist(fileset, entry);
12815184Sek110237 
12825184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
12835184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
12845184Sek110237 		filebench_log(LOG_ERROR,
12855184Sek110237 		    "fileset_populate_file: Can't alloc path string");
12867556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
12875184Sek110237 	}
12885184Sek110237 
12896212Saw148015 	/* see if random variable was supplied for file size */
12906212Saw148015 	if (fileset->fs_meansize == -1) {
12916212Saw148015 		entry->fse_size = (off64_t)avd_get_int(fileset->fs_size);
12926212Saw148015 	} else {
12936212Saw148015 		double gamma;
12945184Sek110237 
12956212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
12966212Saw148015 		if (gamma > 0) {
12976212Saw148015 			drand = gamma_dist_knuth(gamma,
12986212Saw148015 			    fileset->fs_meansize / gamma);
12996212Saw148015 			entry->fse_size = (off64_t)drand;
13006212Saw148015 		} else {
13016212Saw148015 			entry->fse_size = (off64_t)fileset->fs_meansize;
13026212Saw148015 		}
13035184Sek110237 	}
13045184Sek110237 
13055184Sek110237 	fileset->fs_bytes += entry->fse_size;
13065184Sek110237 
13075184Sek110237 	fileset->fs_realfiles++;
13087556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
13095184Sek110237 }
13105184Sek110237 
13115184Sek110237 /*
13127946SAndrew.W.Wilson@sun.com  * Obtaines a filesetentry entity for a leaf directory to be placed in a
13137946SAndrew.W.Wilson@sun.com  * (sub)directory of a fileset. The leaf directory will always be empty so
13147946SAndrew.W.Wilson@sun.com  * it can be created and deleted (mkdir, rmdir) at will. The filesetentry
13157946SAndrew.W.Wilson@sun.com  * entity is placed on the leaf directory list in the specified parent
13167946SAndrew.W.Wilson@sun.com  * filesetentry entity, which may be a (sub) directory filesetentry, or
13177946SAndrew.W.Wilson@sun.com  * the root filesetentry in the fileset. It is also placed on the fileset's
13187946SAndrew.W.Wilson@sun.com  * list of all contained leaf directories. Returns FILEBENCH_OK if successful
13197946SAndrew.W.Wilson@sun.com  * or FILEBENCH_ERROR if ipc memory cannot be allocated.
13207946SAndrew.W.Wilson@sun.com  */
13217946SAndrew.W.Wilson@sun.com static int
13227946SAndrew.W.Wilson@sun.com fileset_populate_leafdir(fileset_t *fileset, filesetentry_t *parent, int serial)
13237946SAndrew.W.Wilson@sun.com {
13247946SAndrew.W.Wilson@sun.com 	char tmpname[16];
13257946SAndrew.W.Wilson@sun.com 	filesetentry_t *entry;
13268404SAndrew.W.Wilson@sun.com 	uint_t index;
13277946SAndrew.W.Wilson@sun.com 
13287946SAndrew.W.Wilson@sun.com 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
13297946SAndrew.W.Wilson@sun.com 	    == NULL) {
13307946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
13317946SAndrew.W.Wilson@sun.com 		    "fileset_populate_file: Can't malloc filesetentry");
13327946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
13337946SAndrew.W.Wilson@sun.com 	}
13347946SAndrew.W.Wilson@sun.com 
13357946SAndrew.W.Wilson@sun.com 	/* Another currently idle leaf directory */
13367946SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
13378404SAndrew.W.Wilson@sun.com 	index = fileset->fs_idle_leafdirs++;
13387946SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
13397946SAndrew.W.Wilson@sun.com 
13408404SAndrew.W.Wilson@sun.com 	entry->fse_index = index;
13417946SAndrew.W.Wilson@sun.com 	entry->fse_parent = parent;
13427946SAndrew.W.Wilson@sun.com 	entry->fse_fileset = fileset;
13437946SAndrew.W.Wilson@sun.com 	fileset_insleafdirlist(fileset, entry);
13447946SAndrew.W.Wilson@sun.com 
13457946SAndrew.W.Wilson@sun.com 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
13467946SAndrew.W.Wilson@sun.com 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
13477946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
13487946SAndrew.W.Wilson@sun.com 		    "fileset_populate_file: Can't alloc path string");
13497946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
13507946SAndrew.W.Wilson@sun.com 	}
13517946SAndrew.W.Wilson@sun.com 
13527946SAndrew.W.Wilson@sun.com 	fileset->fs_realleafdirs++;
13537946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
13547946SAndrew.W.Wilson@sun.com }
13557946SAndrew.W.Wilson@sun.com 
13567946SAndrew.W.Wilson@sun.com /*
13575184Sek110237  * Creates a directory node in a fileset, by obtaining a
13585184Sek110237  * filesetentry entity for the node and initializing it
13595184Sek110237  * according to parameters of the fileset. It determines a
13605184Sek110237  * directory tree depth and directory width, optionally using
13615184Sek110237  * a gamma distribution. If its calculated depth is less then
13625184Sek110237  * its actual depth in the directory tree, it becomes a leaf
13635184Sek110237  * node and files itself with "width" number of file type
13645184Sek110237  * filesetentries, otherwise it files itself with "width"
13655184Sek110237  * number of directory type filesetentries, using recursive
13665184Sek110237  * calls to fileset_populate_subdir. The end result of the
13675184Sek110237  * initial call to this routine is a tree of directories of
13685184Sek110237  * random width and varying depth with sufficient leaf
13695184Sek110237  * directories to contain all required files.
13707556SAndrew.W.Wilson@sun.com  * Returns FILEBENCH_OK on success. Returns FILEBENCH_ERROR if ipc path
13717556SAndrew.W.Wilson@sun.com  * string memory cannot be allocated and returns the error code (currently
13727556SAndrew.W.Wilson@sun.com  * also FILEBENCH_ERROR) from calls to fileset_populate_file or recursive
13735184Sek110237  * calls to fileset_populate_subdir.
13745184Sek110237  */
13755184Sek110237 static int
13765184Sek110237 fileset_populate_subdir(fileset_t *fileset, filesetentry_t *parent,
13775184Sek110237     int serial, double depth)
13785184Sek110237 {
13796212Saw148015 	double randepth, drand, ranwidth;
13805184Sek110237 	int isleaf = 0;
13815184Sek110237 	char tmpname[16];
13825184Sek110237 	filesetentry_t *entry;
13835184Sek110237 	int i;
13848404SAndrew.W.Wilson@sun.com 	uint_t index;
13855184Sek110237 
13865184Sek110237 	depth += 1;
13875184Sek110237 
13885184Sek110237 	/* Create dir node */
13895184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
13905184Sek110237 	    == NULL) {
13915184Sek110237 		filebench_log(LOG_ERROR,
13925184Sek110237 		    "fileset_populate_subdir: Can't malloc filesetentry");
13937556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
13945184Sek110237 	}
13955184Sek110237 
13967556SAndrew.W.Wilson@sun.com 	/* another idle directory */
13977556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
13988404SAndrew.W.Wilson@sun.com 	index = fileset->fs_idle_dirs++;
13997556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
14005184Sek110237 
14015184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
14025184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
14035184Sek110237 		filebench_log(LOG_ERROR,
14045184Sek110237 		    "fileset_populate_subdir: Can't alloc path string");
14057556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
14065184Sek110237 	}
14075184Sek110237 
14088404SAndrew.W.Wilson@sun.com 	entry->fse_index = index;
14095184Sek110237 	entry->fse_parent = parent;
14107946SAndrew.W.Wilson@sun.com 	entry->fse_fileset = fileset;
14115184Sek110237 	fileset_insdirlist(fileset, entry);
14125184Sek110237 
14136212Saw148015 	if (fileset->fs_dirdepthrv) {
14146212Saw148015 		randepth = (int)avd_get_int(fileset->fs_dirdepthrv);
14155184Sek110237 	} else {
14166212Saw148015 		double gamma;
14176212Saw148015 
14186212Saw148015 		gamma = avd_get_int(fileset->fs_dirgamma) / 1000.0;
14196212Saw148015 		if (gamma > 0) {
14206212Saw148015 			drand = gamma_dist_knuth(gamma,
14216212Saw148015 			    fileset->fs_meandepth / gamma);
14226212Saw148015 			randepth = (int)drand;
14236212Saw148015 		} else {
14246212Saw148015 			randepth = (int)fileset->fs_meandepth;
14256212Saw148015 		}
14265184Sek110237 	}
14275184Sek110237 
14286212Saw148015 	if (fileset->fs_meanwidth == -1) {
14296212Saw148015 		ranwidth = avd_get_dbl(fileset->fs_dirwidth);
14306212Saw148015 	} else {
14316212Saw148015 		double gamma;
14325184Sek110237 
14336212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
14346212Saw148015 		if (gamma > 0) {
14356212Saw148015 			drand = gamma_dist_knuth(gamma,
14366212Saw148015 			    fileset->fs_meanwidth / gamma);
14376212Saw148015 			ranwidth = drand;
14386212Saw148015 		} else {
14396212Saw148015 			ranwidth = fileset->fs_meanwidth;
14406212Saw148015 		}
14415184Sek110237 	}
14425184Sek110237 
14435184Sek110237 	if (randepth == 0)
14445184Sek110237 		randepth = 1;
14455184Sek110237 	if (ranwidth == 0)
14465184Sek110237 		ranwidth = 1;
14475184Sek110237 	if (depth >= randepth)
14485184Sek110237 		isleaf = 1;
14495184Sek110237 
14505184Sek110237 	/*
14517946SAndrew.W.Wilson@sun.com 	 * Create directory of random width filled with files according
14527946SAndrew.W.Wilson@sun.com 	 * to distribution, or if root directory, continue until #files required
14535184Sek110237 	 */
14546212Saw148015 	for (i = 1; ((parent == NULL) || (i < ranwidth + 1)) &&
14556212Saw148015 	    (fileset->fs_realfiles < fileset->fs_constentries);
14566212Saw148015 	    i++) {
14575184Sek110237 		int ret = 0;
14585184Sek110237 
14595184Sek110237 		if (parent && isleaf)
14605184Sek110237 			ret = fileset_populate_file(fileset, entry, i);
14615184Sek110237 		else
14625184Sek110237 			ret = fileset_populate_subdir(fileset, entry, i, depth);
14635184Sek110237 
14645184Sek110237 		if (ret != 0)
14655184Sek110237 			return (ret);
14665184Sek110237 	}
14677946SAndrew.W.Wilson@sun.com 
14687946SAndrew.W.Wilson@sun.com 	/*
14697946SAndrew.W.Wilson@sun.com 	 * Create directory of random width filled with leaf directories
14707946SAndrew.W.Wilson@sun.com 	 * according to distribution, or if root directory, continue until
14717946SAndrew.W.Wilson@sun.com 	 * the number of leaf directories required has been generated.
14727946SAndrew.W.Wilson@sun.com 	 */
14737946SAndrew.W.Wilson@sun.com 	for (i = 1; ((parent == NULL) || (i < ranwidth + 1)) &&
14747946SAndrew.W.Wilson@sun.com 	    (fileset->fs_realleafdirs < fileset->fs_constleafdirs);
14757946SAndrew.W.Wilson@sun.com 	    i++) {
14767946SAndrew.W.Wilson@sun.com 		int ret = 0;
14777946SAndrew.W.Wilson@sun.com 
14787946SAndrew.W.Wilson@sun.com 		if (parent && isleaf)
14797946SAndrew.W.Wilson@sun.com 			ret = fileset_populate_leafdir(fileset, entry, i);
14807946SAndrew.W.Wilson@sun.com 		else
14817946SAndrew.W.Wilson@sun.com 			ret = fileset_populate_subdir(fileset, entry, i, depth);
14827946SAndrew.W.Wilson@sun.com 
14837946SAndrew.W.Wilson@sun.com 		if (ret != 0)
14847946SAndrew.W.Wilson@sun.com 			return (ret);
14857946SAndrew.W.Wilson@sun.com 	}
14867946SAndrew.W.Wilson@sun.com 
14877556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
14885184Sek110237 }
14895184Sek110237 
14905184Sek110237 /*
14915184Sek110237  * Populates a fileset with files and subdirectory entries. Uses
14926212Saw148015  * the supplied fileset_dirwidth and fileset_entries (number of files) to
14936212Saw148015  * calculate the required fileset_meandepth (of subdirectories) and
14946212Saw148015  * initialize the fileset_meanwidth and fileset_meansize variables. Then
14955184Sek110237  * calls fileset_populate_subdir() to do the recursive
14965184Sek110237  * subdirectory entry creation and leaf file entry creation. All
14975184Sek110237  * of the above is skipped if the fileset has already been
14985184Sek110237  * populated. Returns 0 on success, or an error code from the
14995184Sek110237  * call to fileset_populate_subdir if that call fails.
15005184Sek110237  */
15015184Sek110237 static int
15025184Sek110237 fileset_populate(fileset_t *fileset)
15035184Sek110237 {
15048404SAndrew.W.Wilson@sun.com 	fbint_t entries = avd_get_int(fileset->fs_entries);
15058404SAndrew.W.Wilson@sun.com 	fbint_t leafdirs = avd_get_int(fileset->fs_leafdirs);
15066212Saw148015 	int meandirwidth;
15075184Sek110237 	int ret;
15085184Sek110237 
15095184Sek110237 	/* Skip if already populated */
15105184Sek110237 	if (fileset->fs_bytes > 0)
15115184Sek110237 		goto exists;
15125184Sek110237 
15135673Saw148015 #ifdef HAVE_RAW_SUPPORT
15145673Saw148015 	/* check for raw device */
15155673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
15167556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
15175673Saw148015 #endif /* HAVE_RAW_SUPPORT */
15185673Saw148015 
15197946SAndrew.W.Wilson@sun.com 	/*
15207946SAndrew.W.Wilson@sun.com 	 * save value of entries and leaf dirs obtained for later
15217946SAndrew.W.Wilson@sun.com 	 * in case it was random
15227946SAndrew.W.Wilson@sun.com 	 */
15236212Saw148015 	fileset->fs_constentries = entries;
15247946SAndrew.W.Wilson@sun.com 	fileset->fs_constleafdirs = leafdirs;
15256212Saw148015 
15267556SAndrew.W.Wilson@sun.com 	/* initialize idle files and directories condition variables */
15277946SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_idle_files_cv, ipc_condattr());
15287556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_idle_dirs_cv, ipc_condattr());
15297946SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_idle_leafdirs_cv, ipc_condattr());
15307556SAndrew.W.Wilson@sun.com 
15317556SAndrew.W.Wilson@sun.com 	/* no files or dirs idle (or busy) yet */
15327556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_files = 0;
15337556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_dirs = 0;
15347946SAndrew.W.Wilson@sun.com 	fileset->fs_idle_leafdirs = 0;
15357556SAndrew.W.Wilson@sun.com 
15367556SAndrew.W.Wilson@sun.com 	/* initialize locks and other condition variables */
15377556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_init(&fileset->fs_pick_lock,
15387556SAndrew.W.Wilson@sun.com 	    ipc_mutexattr(IPC_MUTEX_NORMAL));
15398404SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_init(&fileset->fs_histo_lock,
15408404SAndrew.W.Wilson@sun.com 	    ipc_mutexattr(IPC_MUTEX_NORMAL));
15417556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_thrd_wait_cv, ipc_condattr());
15427556SAndrew.W.Wilson@sun.com 
15438404SAndrew.W.Wilson@sun.com 	/* Initialize avl btrees */
15448404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_free_files), fileset_entry_compare,
15458404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15468404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_noex_files), fileset_entry_compare,
15478404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15488404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_exist_files), fileset_entry_compare,
15498404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15508404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_free_leaf_dirs), fileset_entry_compare,
15518404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15528404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_noex_leaf_dirs), fileset_entry_compare,
15538404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15548404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_exist_leaf_dirs), fileset_entry_compare,
15558404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15568404SAndrew.W.Wilson@sun.com 	avl_create(&(fileset->fs_dirs), fileset_entry_compare,
15578404SAndrew.W.Wilson@sun.com 	    sizeof (filesetentry_t), FSE_OFFSETOF(fse_link));
15588404SAndrew.W.Wilson@sun.com 
15596212Saw148015 	/* is dirwidth a random variable? */
15606212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_dirwidth)) {
15616212Saw148015 		meandirwidth =
15626212Saw148015 		    (int)fileset->fs_dirwidth->avd_val.randptr->rnd_dbl_mean;
15636212Saw148015 		fileset->fs_meanwidth = -1;
15646212Saw148015 	} else {
15656212Saw148015 		meandirwidth = (int)avd_get_int(fileset->fs_dirwidth);
15666212Saw148015 		fileset->fs_meanwidth = (double)meandirwidth;
15676212Saw148015 	}
15686212Saw148015 
15695184Sek110237 	/*
15705184Sek110237 	 * Input params are:
15715184Sek110237 	 *	# of files
15725184Sek110237 	 *	ave # of files per dir
15735184Sek110237 	 *	max size of dir
15745184Sek110237 	 *	# ave size of file
15755184Sek110237 	 *	max size of file
15765184Sek110237 	 */
15777946SAndrew.W.Wilson@sun.com 	fileset->fs_meandepth = log(entries+leafdirs) / log(meandirwidth);
15786212Saw148015 
15796212Saw148015 	/* Has a random variable been supplied for dirdepth? */
15806212Saw148015 	if (fileset->fs_dirdepthrv) {
15816212Saw148015 		/* yes, so set the random variable's mean value to meandepth */
15826212Saw148015 		fileset->fs_dirdepthrv->avd_val.randptr->rnd_dbl_mean =
15836212Saw148015 		    fileset->fs_meandepth;
15846212Saw148015 	}
15856212Saw148015 
15866212Saw148015 	/* test for random size variable */
15876212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_size))
15886212Saw148015 		fileset->fs_meansize = -1;
15896212Saw148015 	else
15906212Saw148015 		fileset->fs_meansize = avd_get_int(fileset->fs_size);
15915184Sek110237 
15925184Sek110237 	if ((ret = fileset_populate_subdir(fileset, NULL, 1, 0)) != 0)
15935184Sek110237 		return (ret);
15945184Sek110237 
15955184Sek110237 
15965184Sek110237 exists:
15975673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
15986286Saw148015 		filebench_log(LOG_VERBOSE, "File %s: mbytes=%llu",
15996212Saw148015 		    avd_get_str(fileset->fs_name),
16006286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
16015673Saw148015 	} else {
16027946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE, "Fileset %s: %d files, %d leafdirs "
16036286Saw148015 		    "avg dir = %d, avg depth = %.1lf, mbytes=%llu",
16047946SAndrew.W.Wilson@sun.com 		    avd_get_str(fileset->fs_name), entries, leafdirs,
16056212Saw148015 		    meandirwidth,
16065673Saw148015 		    fileset->fs_meandepth,
16076286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
16085673Saw148015 	}
16096701Saw148015 
16107556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
16115184Sek110237 }
16125184Sek110237 
16135184Sek110237 /*
16146212Saw148015  * Allocates a fileset instance, initializes fileset_dirgamma and
16156212Saw148015  * fileset_sizegamma default values, and sets the fileset name to the
16165184Sek110237  * supplied name string. Puts the allocated fileset on the
16175184Sek110237  * master fileset list and returns a pointer to it.
16186701Saw148015  *
16196701Saw148015  * This routine implements the 'define fileset' calls found in a .f
16206701Saw148015  * workload, such as in the following example:
16216701Saw148015  * define fileset name=drew4ever, entries=$nfiles
16225184Sek110237  */
16235184Sek110237 fileset_t *
16246212Saw148015 fileset_define(avd_t name)
16255184Sek110237 {
16265184Sek110237 	fileset_t *fileset;
16275184Sek110237 
16285184Sek110237 	if (name == NULL)
16295184Sek110237 		return (NULL);
16305184Sek110237 
16315184Sek110237 	if ((fileset = (fileset_t *)ipc_malloc(FILEBENCH_FILESET)) == NULL) {
16325184Sek110237 		filebench_log(LOG_ERROR,
16335184Sek110237 		    "fileset_define: Can't malloc fileset");
16345184Sek110237 		return (NULL);
16355184Sek110237 	}
16365184Sek110237 
16376212Saw148015 	filebench_log(LOG_DEBUG_IMPL,
16386212Saw148015 	    "Defining file %s", avd_get_str(name));
16395184Sek110237 
16406391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
16415184Sek110237 
16426212Saw148015 	fileset->fs_dirgamma = avd_int_alloc(1500);
16436212Saw148015 	fileset->fs_sizegamma = avd_int_alloc(1500);
16448404SAndrew.W.Wilson@sun.com 	fileset->fs_histo_id = -1;
16455184Sek110237 
16465184Sek110237 	/* Add fileset to global list */
16476391Saw148015 	if (filebench_shm->shm_filesetlist == NULL) {
16486391Saw148015 		filebench_shm->shm_filesetlist = fileset;
16495184Sek110237 		fileset->fs_next = NULL;
16505184Sek110237 	} else {
16516391Saw148015 		fileset->fs_next = filebench_shm->shm_filesetlist;
16526391Saw148015 		filebench_shm->shm_filesetlist = fileset;
16535184Sek110237 	}
16545184Sek110237 
16556391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
16565184Sek110237 
16576212Saw148015 	fileset->fs_name = name;
16585184Sek110237 
16595184Sek110237 	return (fileset);
16605184Sek110237 }
16615184Sek110237 
16625184Sek110237 /*
16635184Sek110237  * If supplied with a pointer to a fileset and the fileset's
16646212Saw148015  * fileset_prealloc flag is set, calls fileset_populate() to populate
16655184Sek110237  * the fileset with filesetentries, then calls fileset_create()
16665184Sek110237  * to make actual directories and files for the filesetentries.
16675184Sek110237  * Otherwise, it applies fileset_populate() and fileset_create()
16685184Sek110237  * to all the filesets on the master fileset list. It always
16695184Sek110237  * returns zero (0) if one fileset is populated / created,
16705184Sek110237  * otherwise it returns the sum of returned values from
16715184Sek110237  * fileset_create() and fileset_populate(), which
16725184Sek110237  * will be a negative one (-1) times the number of
16735184Sek110237  * fileset_create() calls which failed.
16745184Sek110237  */
16755184Sek110237 int
16765184Sek110237 fileset_createset(fileset_t *fileset)
16775184Sek110237 {
16785184Sek110237 	fileset_t *list;
16795184Sek110237 	int ret = 0;
16805184Sek110237 
16815673Saw148015 	/* set up for possible parallel allocate */
16827556SAndrew.W.Wilson@sun.com 	filebench_shm->shm_fsparalloc_count = 0;
16837556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(
16847556SAndrew.W.Wilson@sun.com 	    &filebench_shm->shm_fsparalloc_cv,
16857556SAndrew.W.Wilson@sun.com 	    ipc_condattr());
16865673Saw148015 
16876212Saw148015 	if (fileset && avd_get_bool(fileset->fs_prealloc)) {
16885673Saw148015 
16896305Saw148015 		/* check for raw files */
16906305Saw148015 		if (fileset_checkraw(fileset)) {
16916305Saw148015 			filebench_log(LOG_INFO,
16926305Saw148015 			    "file %s/%s is a RAW device",
16936305Saw148015 			    avd_get_str(fileset->fs_path),
16946305Saw148015 			    avd_get_str(fileset->fs_name));
16957556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
16966305Saw148015 		}
16976305Saw148015 
16985673Saw148015 		filebench_log(LOG_INFO,
16995673Saw148015 		    "creating/pre-allocating %s %s",
17006212Saw148015 		    fileset_entity_name(fileset),
17016212Saw148015 		    avd_get_str(fileset->fs_name));
17025673Saw148015 
17037556SAndrew.W.Wilson@sun.com 		if ((ret = fileset_populate(fileset)) != FILEBENCH_OK)
17045184Sek110237 			return (ret);
17055673Saw148015 
17067556SAndrew.W.Wilson@sun.com 		if ((ret = fileset_create(fileset)) != FILEBENCH_OK)
17075673Saw148015 			return (ret);
17085673Saw148015 	} else {
17095673Saw148015 
17105673Saw148015 		filebench_log(LOG_INFO,
17115673Saw148015 		    "Creating/pre-allocating files and filesets");
17125673Saw148015 
17136391Saw148015 		list = filebench_shm->shm_filesetlist;
17145673Saw148015 		while (list) {
17156305Saw148015 			/* check for raw files */
17166305Saw148015 			if (fileset_checkraw(list)) {
17176305Saw148015 				filebench_log(LOG_INFO,
17186305Saw148015 				    "file %s/%s is a RAW device",
17196305Saw148015 				    avd_get_str(list->fs_path),
17206305Saw148015 				    avd_get_str(list->fs_name));
17216305Saw148015 				list = list->fs_next;
17226305Saw148015 				continue;
17236305Saw148015 			}
17246305Saw148015 
17257556SAndrew.W.Wilson@sun.com 			if ((ret = fileset_populate(list)) != FILEBENCH_OK)
17265673Saw148015 				return (ret);
17277556SAndrew.W.Wilson@sun.com 
17287556SAndrew.W.Wilson@sun.com 			if ((ret = fileset_create(list)) != FILEBENCH_OK)
17295673Saw148015 				return (ret);
17307556SAndrew.W.Wilson@sun.com 
17315673Saw148015 			list = list->fs_next;
17325673Saw148015 		}
17335184Sek110237 	}
17345184Sek110237 
17355673Saw148015 	/* wait for allocation threads to finish */
17365673Saw148015 	filebench_log(LOG_INFO,
17375673Saw148015 	    "waiting for fileset pre-allocation to finish");
17385184Sek110237 
17397556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
17407556SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_fsparalloc_count > 0)
17417556SAndrew.W.Wilson@sun.com 		(void) pthread_cond_wait(
17427556SAndrew.W.Wilson@sun.com 		    &filebench_shm->shm_fsparalloc_cv,
17437556SAndrew.W.Wilson@sun.com 		    &filebench_shm->shm_fsparalloc_lock);
17447556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_unlock(&filebench_shm->shm_fsparalloc_lock);
17455673Saw148015 
17467556SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_fsparalloc_count < 0)
17477556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
17485673Saw148015 
17497556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
17505184Sek110237 }
17515184Sek110237 
17525184Sek110237 /*
17535184Sek110237  * Searches through the master fileset list for the named fileset.
17545184Sek110237  * If found, returns pointer to same, otherwise returns NULL.
17555184Sek110237  */
17565184Sek110237 fileset_t *
17575184Sek110237 fileset_find(char *name)
17585184Sek110237 {
17596391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
17605184Sek110237 
17616391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
17625184Sek110237 
17635184Sek110237 	while (fileset) {
17646212Saw148015 		if (strcmp(name, avd_get_str(fileset->fs_name)) == 0) {
17656391Saw148015 			(void) ipc_mutex_unlock(
17666391Saw148015 			    &filebench_shm->shm_fileset_lock);
17675184Sek110237 			return (fileset);
17685184Sek110237 		}
17695184Sek110237 		fileset = fileset->fs_next;
17705184Sek110237 	}
17716391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
17725184Sek110237 
17735184Sek110237 	return (NULL);
17745184Sek110237 }
17755673Saw148015 
17765673Saw148015 /*
17775673Saw148015  * Iterates over all the file sets in the filesetlist,
17785673Saw148015  * executing the supplied command "*cmd()" on them. Also
17795673Saw148015  * indicates to the executed command if it is the first
17805673Saw148015  * time the command has been executed since the current
17815673Saw148015  * call to fileset_iter.
17825673Saw148015  */
17838404SAndrew.W.Wilson@sun.com int
17845673Saw148015 fileset_iter(int (*cmd)(fileset_t *fileset, int first))
17855673Saw148015 {
17866391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
17875673Saw148015 	int count = 0;
17885673Saw148015 
17896391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
17905673Saw148015 
17915673Saw148015 	while (fileset) {
17928404SAndrew.W.Wilson@sun.com 		if (cmd(fileset, count == 0) == FILEBENCH_ERROR) {
17938404SAndrew.W.Wilson@sun.com 			(void) ipc_mutex_unlock(
17948404SAndrew.W.Wilson@sun.com 			    &filebench_shm->shm_fileset_lock);
17958404SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
17968404SAndrew.W.Wilson@sun.com 		}
17975673Saw148015 		fileset = fileset->fs_next;
17985673Saw148015 		count++;
17995673Saw148015 	}
18005673Saw148015 
18016391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
18028404SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
18035673Saw148015 }
18045673Saw148015 
18055673Saw148015 /*
18065673Saw148015  * Prints information to the filebench log about the file
18075673Saw148015  * object. Also prints a header on the first call.
18085673Saw148015  */
18095673Saw148015 int
18105673Saw148015 fileset_print(fileset_t *fileset, int first)
18115673Saw148015 {
18126212Saw148015 	int pathlength;
18136212Saw148015 	char *fileset_path;
18146212Saw148015 	char *fileset_name;
18156212Saw148015 	static char pad[] = "                              "; /* 30 spaces */
18166212Saw148015 
18176212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
18186212Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
18196212Saw148015 		    fileset_entity_name(fileset));
18207556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
18216212Saw148015 	}
18226212Saw148015 
18236212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
18246212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
18256212Saw148015 		    fileset_entity_name(fileset));
18267556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
18276212Saw148015 	}
18286212Saw148015 
18296212Saw148015 	pathlength = strlen(fileset_path) + strlen(fileset_name);
18305673Saw148015 
18315673Saw148015 	if (pathlength > 29)
18325673Saw148015 		pathlength = 29;
18335673Saw148015 
18345673Saw148015 	if (first) {
18355673Saw148015 		filebench_log(LOG_INFO, "File or Fileset name%20s%12s%10s",
18365673Saw148015 		    "file size",
18375673Saw148015 		    "dir width",
18385673Saw148015 		    "entries");
18395673Saw148015 	}
18405673Saw148015 
18415673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
18425673Saw148015 		if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
18435673Saw148015 			filebench_log(LOG_INFO,
18445673Saw148015 			    "%s/%s%s         (Raw Device)",
18456212Saw148015 			    fileset_path, fileset_name, &pad[pathlength]);
18465673Saw148015 		} else {
18475673Saw148015 			filebench_log(LOG_INFO,
18486286Saw148015 			    "%s/%s%s%9llu     (Single File)",
18496212Saw148015 			    fileset_path, fileset_name, &pad[pathlength],
18506286Saw148015 			    (u_longlong_t)avd_get_int(fileset->fs_size));
18515673Saw148015 		}
18525673Saw148015 	} else {
18536286Saw148015 		filebench_log(LOG_INFO, "%s/%s%s%9llu%12llu%10llu",
18546212Saw148015 		    fileset_path, fileset_name,
18555673Saw148015 		    &pad[pathlength],
18566286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_size),
18576286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_dirwidth),
18586286Saw148015 		    (u_longlong_t)fileset->fs_constentries);
18595673Saw148015 	}
18607556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
18615673Saw148015 }
18627946SAndrew.W.Wilson@sun.com 
18635673Saw148015 /*
18645673Saw148015  * checks to see if the path/name pair points to a raw device. If
18655673Saw148015  * so it sets the raw device flag (FILESET_IS_RAW_DEV) and returns 1.
18665673Saw148015  * If RAW is not defined, or it is not a raw device, it clears the
18675673Saw148015  * raw device flag and returns 0.
18685673Saw148015  */
18695673Saw148015 int
18705673Saw148015 fileset_checkraw(fileset_t *fileset)
18715673Saw148015 {
18725673Saw148015 	char path[MAXPATHLEN];
18735673Saw148015 	struct stat64 sb;
18746305Saw148015 	char *pathname;
18756305Saw148015 	char *setname;
18765673Saw148015 
18775673Saw148015 	fileset->fs_attrs &= (~FILESET_IS_RAW_DEV);
18785673Saw148015 
18795673Saw148015 #ifdef HAVE_RAW_SUPPORT
18805673Saw148015 	/* check for raw device */
18816305Saw148015 	if ((pathname = avd_get_str(fileset->fs_path)) == NULL)
18827556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
18836305Saw148015 
18846305Saw148015 	if ((setname = avd_get_str(fileset->fs_name)) == NULL)
18857556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
18866305Saw148015 
18877946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, pathname, MAXPATHLEN);
18887946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
18897946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, setname, MAXPATHLEN);
18905673Saw148015 	if ((stat64(path, &sb) == 0) &&
18915673Saw148015 	    ((sb.st_mode & S_IFMT) == S_IFBLK) && sb.st_rdev) {
18925673Saw148015 		fileset->fs_attrs |= FILESET_IS_RAW_DEV;
18936305Saw148015 		if (!(fileset->fs_attrs & FILESET_IS_FILE)) {
18946305Saw148015 			filebench_log(LOG_ERROR,
18956305Saw148015 			    "WARNING Fileset %s/%s Cannot be RAW device",
18966305Saw148015 			    avd_get_str(fileset->fs_path),
18976305Saw148015 			    avd_get_str(fileset->fs_name));
18986305Saw148015 			filebench_shutdown(1);
18996305Saw148015 		}
19006305Saw148015 
19015673Saw148015 		return (1);
19025673Saw148015 	}
19035673Saw148015 #endif /* HAVE_RAW_SUPPORT */
19045673Saw148015 
19057556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
19065673Saw148015 }
1907