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 /*
226212Saw148015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
246613Sek110237  *
256613Sek110237  * Portions Copyright 2008 Denis Cheng
265184Sek110237  */
275184Sek110237 
285184Sek110237 #pragma ident	"%Z%%M%	%I%	%E% SMI"
295184Sek110237 
305184Sek110237 
315184Sek110237 #include <fcntl.h>
325184Sek110237 #include <pthread.h>
335184Sek110237 #include <errno.h>
345184Sek110237 #include <math.h>
355184Sek110237 #include <libgen.h>
365184Sek110237 #include <sys/mman.h>
376613Sek110237 
386613Sek110237 #include "filebench.h"
395184Sek110237 #include "fileset.h"
405184Sek110237 #include "gamma_dist.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.
50*6701Saw148015  *
51*6701Saw148015  * This routine is called from fileset_createset(), which is in turn
52*6701Saw148015  * called from parser_gram.y: parser_create_fileset() when a
53*6701Saw148015  * "create fileset" or "run" command is encountered.
54*6701Saw148015  * When the "create fileset" command is used, it is generally paired with
55*6701Saw148015  * a "create processes" command, and must appear first, in order to
56*6701Saw148015  * instantiate all the files in the fileset before trying to use them.
575184Sek110237  */
585184Sek110237 
596305Saw148015 static int fileset_checkraw(fileset_t *fileset);
606305Saw148015 
615673Saw148015 /* parallel allocation control */
625673Saw148015 #define	MAX_PARALLOC_THREADS 32
635673Saw148015 static pthread_mutex_t	paralloc_lock = PTHREAD_MUTEX_INITIALIZER;
645673Saw148015 static pthread_cond_t	paralloc_cv = PTHREAD_COND_INITIALIZER;
655673Saw148015 static int		paralloc_count;
665673Saw148015 
675673Saw148015 /*
685673Saw148015  * returns pointer to file or fileset
695673Saw148015  * string, as appropriate
705673Saw148015  */
715673Saw148015 static char *
725673Saw148015 fileset_entity_name(fileset_t *fileset)
735673Saw148015 {
745673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE)
755673Saw148015 		return ("file");
765673Saw148015 	else
775673Saw148015 		return ("fileset");
785673Saw148015 }
795673Saw148015 
805184Sek110237 /*
815184Sek110237  * Removes the last file or directory name from a pathname.
825184Sek110237  * Basically removes characters from the end of the path by
835184Sek110237  * setting them to \0 until a forward slash '/' is
845184Sek110237  * encountered. It also removes the forward slash.
855184Sek110237  */
865184Sek110237 static char *
875184Sek110237 trunc_dirname(char *dir)
885184Sek110237 {
895184Sek110237 	char *s = dir + strlen(dir);
905184Sek110237 
915184Sek110237 	while (s != dir) {
925184Sek110237 		int c = *s;
935184Sek110237 
945184Sek110237 		*s = 0;
955184Sek110237 		if (c == '/')
965184Sek110237 			break;
975184Sek110237 		s--;
985184Sek110237 	}
995184Sek110237 	return (dir);
1005184Sek110237 }
1015184Sek110237 
1025184Sek110237 /*
1035184Sek110237  * Prints a list of allowed options and how to specify them.
1045184Sek110237  */
1055184Sek110237 void
1065184Sek110237 fileset_usage(void)
1075184Sek110237 {
1085673Saw148015 	(void) fprintf(stderr,
1095673Saw148015 	    "define [file name=<name> | fileset name=<name>],path=<pathname>,"
1105673Saw148015 	    ",entries=<number>\n");
1115673Saw148015 	(void) fprintf(stderr,
1126212Saw148015 	    "		        [,filesize=[size]]\n");
1136212Saw148015 	(void) fprintf(stderr,
1145673Saw148015 	    "		        [,dirwidth=[width]]\n");
1155673Saw148015 	(void) fprintf(stderr,
1166212Saw148015 	    "		        [,dirdepthrv=$random_variable_name]\n");
1176212Saw148015 	(void) fprintf(stderr,
1185673Saw148015 	    "		        [,dirgamma=[100-10000]] "
1195184Sek110237 	    "(Gamma * 1000)\n");
1205184Sek110237 	(void) fprintf(stderr,
1215673Saw148015 	    "		        [,sizegamma=[100-10000]] (Gamma * 1000)\n");
1225184Sek110237 	(void) fprintf(stderr,
1235184Sek110237 	    "		        [,prealloc=[percent]]\n");
1245673Saw148015 	(void) fprintf(stderr, "		        [,paralloc]\n");
1255184Sek110237 	(void) fprintf(stderr, "		        [,reuse]\n");
1265184Sek110237 	(void) fprintf(stderr, "\n");
1275184Sek110237 }
1285184Sek110237 
1295184Sek110237 /*
1305184Sek110237  * Frees up memory mapped file region of supplied size. The
1315184Sek110237  * file descriptor "fd" indicates which memory mapped file.
1325184Sek110237  * If successful, returns 0. Otherwise returns -1 if "size"
1335184Sek110237  * is zero, or -1 times the number of times msync() failed.
1345184Sek110237  */
1355184Sek110237 static int
1365184Sek110237 fileset_freemem(int fd, off64_t size)
1375184Sek110237 {
1385184Sek110237 	off64_t left;
1395184Sek110237 	int ret = 0;
1405184Sek110237 
1415184Sek110237 	for (left = size; left > 0; left -= MMAP_SIZE) {
1425184Sek110237 		off64_t thismapsize;
1435184Sek110237 		caddr_t addr;
1445184Sek110237 
1455184Sek110237 		thismapsize = MIN(MMAP_SIZE, left);
1465184Sek110237 		addr = mmap64(0, thismapsize, PROT_READ|PROT_WRITE,
1475184Sek110237 		    MAP_SHARED, fd, size - left);
1485184Sek110237 		ret += msync(addr, thismapsize, MS_INVALIDATE);
1495184Sek110237 		(void) munmap(addr, thismapsize);
1505184Sek110237 	}
1515184Sek110237 	return (ret);
1525184Sek110237 }
1535184Sek110237 
1545184Sek110237 /*
1555184Sek110237  * Creates a path string from the filesetentry_t "*entry"
1565184Sek110237  * and all of its parent's path names. The resulting path
1575184Sek110237  * is a concatination of all the individual parent paths.
1585184Sek110237  * Allocates memory for the path string and returns a
1595184Sek110237  * pointer to it.
1605184Sek110237  */
1615184Sek110237 char *
1625184Sek110237 fileset_resolvepath(filesetentry_t *entry)
1635184Sek110237 {
1645184Sek110237 	filesetentry_t *fsep = entry;
1655184Sek110237 	char path[MAXPATHLEN];
1665184Sek110237 	char pathtmp[MAXPATHLEN];
1675184Sek110237 	char *s;
1685184Sek110237 
1695184Sek110237 	*path = 0;
1705184Sek110237 	while (fsep->fse_parent) {
1715184Sek110237 		(void) strcpy(pathtmp, "/");
1725184Sek110237 		(void) strcat(pathtmp, fsep->fse_path);
1735184Sek110237 		(void) strcat(pathtmp, path);
1745184Sek110237 		(void) strcpy(path, pathtmp);
1755184Sek110237 		fsep = fsep->fse_parent;
1765184Sek110237 	}
1775184Sek110237 
1785184Sek110237 	s = malloc(strlen(path) + 1);
1795184Sek110237 	(void) strcpy(s, path);
1805184Sek110237 	return (s);
1815184Sek110237 }
1825184Sek110237 
1835184Sek110237 /*
1845184Sek110237  * Creates multiple nested directories as required by the
1855184Sek110237  * supplied path. Starts at the end of the path, creating
1865184Sek110237  * a list of directories to mkdir, up to the root of the
1875184Sek110237  * path, then mkdirs them one at a time from the root on down.
1885184Sek110237  */
1895184Sek110237 static int
1905184Sek110237 fileset_mkdir(char *path, int mode)
1915184Sek110237 {
1925184Sek110237 	char *p;
1935184Sek110237 	char *dirs[65536];
1945184Sek110237 	int i = 0;
1955184Sek110237 
1965184Sek110237 	if ((p = strdup(path)) == NULL)
1975184Sek110237 		goto null_str;
1985184Sek110237 
1995184Sek110237 	/*
2005184Sek110237 	 * Fill an array of subdirectory path names until either we
2015184Sek110237 	 * reach the root or encounter an already existing subdirectory
2025184Sek110237 	 */
2035184Sek110237 	/* CONSTCOND */
2045184Sek110237 	while (1) {
2055184Sek110237 		struct stat64 sb;
2065184Sek110237 
2075184Sek110237 		if (stat64(p, &sb) == 0)
2085184Sek110237 			break;
2095184Sek110237 		if (strlen(p) < 3)
2105184Sek110237 			break;
2115184Sek110237 		if ((dirs[i] = strdup(p)) == NULL) {
2125184Sek110237 			free(p);
2135184Sek110237 			goto null_str;
2145184Sek110237 		}
2155184Sek110237 
2165184Sek110237 		(void) trunc_dirname(p);
2175184Sek110237 		i++;
2185184Sek110237 	}
2195184Sek110237 
2205184Sek110237 	/* Make the directories, from closest to root downwards. */
2215184Sek110237 	for (--i; i >= 0; i--) {
2225184Sek110237 		(void) mkdir(dirs[i], mode);
2235184Sek110237 		free(dirs[i]);
2245184Sek110237 	}
2255184Sek110237 
2265184Sek110237 	free(p);
2275184Sek110237 	return (0);
2285184Sek110237 
2295184Sek110237 null_str:
2305184Sek110237 	/* clean up */
2315184Sek110237 	for (--i; i >= 0; i--)
2325184Sek110237 		free(dirs[i]);
2335184Sek110237 
2345184Sek110237 	filebench_log(LOG_ERROR,
2355184Sek110237 	    "Failed to create directory path %s: Out of memory", path);
2365184Sek110237 
2375184Sek110237 	return (-1);
2385184Sek110237 }
2395184Sek110237 
2405673Saw148015 /*
2415673Saw148015  * creates the subdirectory tree for a fileset.
2425673Saw148015  */
2435673Saw148015 static int
2445673Saw148015 fileset_create_subdirs(fileset_t *fileset, char *filesetpath)
2455673Saw148015 {
2465673Saw148015 	filesetentry_t *direntry;
2475673Saw148015 	char full_path[MAXPATHLEN];
2485673Saw148015 	char *part_path;
2495673Saw148015 
2505673Saw148015 	/* walk the subdirectory list, enstanciating subdirs */
2515673Saw148015 	direntry = fileset->fs_dirlist;
2525673Saw148015 	while (direntry) {
2535673Saw148015 		(void) strcpy(full_path, filesetpath);
2545673Saw148015 		part_path = fileset_resolvepath(direntry);
2555673Saw148015 		(void) strcat(full_path, part_path);
2565673Saw148015 		free(part_path);
2575673Saw148015 
2585673Saw148015 		/* now create this portion of the subdirectory tree */
2595673Saw148015 		if (fileset_mkdir(full_path, 0755) == -1)
2605673Saw148015 			return (-1);
2615673Saw148015 
2625673Saw148015 		direntry = direntry->fse_dirnext;
2635673Saw148015 	}
2645673Saw148015 	return (0);
2655673Saw148015 }
2665673Saw148015 
2675673Saw148015 /*
2685673Saw148015  * given a fileset entry, determines if the associated file
2695673Saw148015  * needs to be allocated or not, and if so does the allocation.
2705673Saw148015  */
2715673Saw148015 static int
2725673Saw148015 fileset_alloc_file(filesetentry_t *entry)
2735673Saw148015 {
2745673Saw148015 	char path[MAXPATHLEN];
2755673Saw148015 	char *buf;
2765673Saw148015 	struct stat64 sb;
2775673Saw148015 	char *pathtmp;
2785673Saw148015 	off64_t seek;
2795673Saw148015 	int fd;
2805673Saw148015 
2815673Saw148015 	*path = 0;
2826212Saw148015 	(void) strcpy(path, avd_get_str(entry->fse_fileset->fs_path));
2835673Saw148015 	(void) strcat(path, "/");
2846212Saw148015 	(void) strcat(path, avd_get_str(entry->fse_fileset->fs_name));
2855673Saw148015 	pathtmp = fileset_resolvepath(entry);
2865673Saw148015 	(void) strcat(path, pathtmp);
2875673Saw148015 
2885673Saw148015 	filebench_log(LOG_DEBUG_IMPL, "Populated %s", entry->fse_path);
2895673Saw148015 
2905673Saw148015 	/* see if reusing and this file exists */
2915673Saw148015 	if ((entry->fse_flags & FSE_REUSING) && (stat64(path, &sb) == 0)) {
2925673Saw148015 		if ((fd = open64(path, O_RDWR)) < 0) {
2935673Saw148015 			filebench_log(LOG_INFO,
2945673Saw148015 			    "Attempted but failed to Re-use file %s",
2955673Saw148015 			    path);
2965673Saw148015 			return (-1);
2975673Saw148015 		}
2985673Saw148015 
2995673Saw148015 		if (sb.st_size == (off64_t)entry->fse_size) {
3005673Saw148015 			filebench_log(LOG_INFO,
3015673Saw148015 			    "Re-using file %s", path);
3025673Saw148015 
3036212Saw148015 			if (!avd_get_bool(entry->fse_fileset->fs_cached))
3045673Saw148015 				(void) fileset_freemem(fd,
3055673Saw148015 				    entry->fse_size);
3065673Saw148015 
3075673Saw148015 			entry->fse_flags |= FSE_EXISTS;
308*6701Saw148015 			(void) ipc_mutex_lock(
309*6701Saw148015 			    &entry->fse_fileset->fs_num_files_lock);
310*6701Saw148015 			entry->fse_fileset->fs_num_act_files++;
311*6701Saw148015 			(void) ipc_mutex_unlock(
312*6701Saw148015 			    &entry->fse_fileset->fs_num_files_lock);
313*6701Saw148015 
3145673Saw148015 			(void) close(fd);
3155673Saw148015 			return (0);
3165673Saw148015 
3175673Saw148015 		} else if (sb.st_size > (off64_t)entry->fse_size) {
3185673Saw148015 			/* reuse, but too large */
3195673Saw148015 			filebench_log(LOG_INFO,
3205673Saw148015 			    "Truncating & re-using file %s", path);
3215673Saw148015 
3226613Sek110237 #ifdef HAVE_FTRUNCATE64
3236613Sek110237 			(void) ftruncate64(fd, (off64_t)entry->fse_size);
3246613Sek110237 #else
3256613Sek110237 			(void) ftruncate(fd, (off_t)entry->fse_size);
3266613Sek110237 #endif
3275673Saw148015 
3286212Saw148015 			if (!avd_get_bool(entry->fse_fileset->fs_cached))
3295673Saw148015 				(void) fileset_freemem(fd,
3305673Saw148015 				    entry->fse_size);
3315673Saw148015 
3325673Saw148015 			entry->fse_flags |= FSE_EXISTS;
333*6701Saw148015 
334*6701Saw148015 			(void) ipc_mutex_lock(
335*6701Saw148015 			    &entry->fse_fileset->fs_num_files_lock);
336*6701Saw148015 			entry->fse_fileset->fs_num_act_files++;
337*6701Saw148015 			(void) ipc_mutex_unlock(
338*6701Saw148015 			    &entry->fse_fileset->fs_num_files_lock);
339*6701Saw148015 
3405673Saw148015 			(void) close(fd);
3415673Saw148015 			return (0);
3425673Saw148015 		}
3435673Saw148015 	} else {
3445673Saw148015 
3455673Saw148015 		/* No file or not reusing, so create */
3465673Saw148015 		if ((fd = open64(path, O_RDWR | O_CREAT, 0644)) < 0) {
3475673Saw148015 			filebench_log(LOG_ERROR,
3485673Saw148015 			    "Failed to pre-allocate file %s: %s",
3495673Saw148015 			    path, strerror(errno));
3505673Saw148015 
3515673Saw148015 			return (-1);
3525673Saw148015 		}
3535673Saw148015 	}
3545673Saw148015 
3555673Saw148015 	if ((buf = (char *)malloc(FILE_ALLOC_BLOCK)) == NULL)
3565673Saw148015 		return (-1);
3575673Saw148015 
3585673Saw148015 	entry->fse_flags |= FSE_EXISTS;
3595673Saw148015 
360*6701Saw148015 	(void) ipc_mutex_lock(&entry->fse_fileset->fs_num_files_lock);
361*6701Saw148015 	entry->fse_fileset->fs_num_act_files++;
362*6701Saw148015 	(void) ipc_mutex_unlock(&entry->fse_fileset->fs_num_files_lock);
363*6701Saw148015 
3645673Saw148015 	for (seek = 0; seek < entry->fse_size; ) {
3655673Saw148015 		off64_t wsize;
3665673Saw148015 		int ret = 0;
3675673Saw148015 
3685673Saw148015 		/*
3695673Saw148015 		 * Write FILE_ALLOC_BLOCK's worth,
3705673Saw148015 		 * except on last write
3715673Saw148015 		 */
3725673Saw148015 		wsize = MIN(entry->fse_size - seek, FILE_ALLOC_BLOCK);
3735673Saw148015 
3745673Saw148015 		ret = write(fd, buf, wsize);
3755673Saw148015 		if (ret != wsize) {
3765673Saw148015 			filebench_log(LOG_ERROR,
3775673Saw148015 			    "Failed to pre-allocate file %s: %s",
3785673Saw148015 			    path, strerror(errno));
3795673Saw148015 			(void) close(fd);
3805673Saw148015 			free(buf);
3815673Saw148015 			return (-1);
3825673Saw148015 		}
3835673Saw148015 		seek += wsize;
3845673Saw148015 	}
3855673Saw148015 
3866212Saw148015 	if (!avd_get_bool(entry->fse_fileset->fs_cached))
3875673Saw148015 		(void) fileset_freemem(fd, entry->fse_size);
3885673Saw148015 
3895673Saw148015 	(void) close(fd);
3905673Saw148015 
3915673Saw148015 	free(buf);
3925673Saw148015 
3935673Saw148015 	filebench_log(LOG_DEBUG_IMPL,
3946286Saw148015 	    "Pre-allocated file %s size %llu",
3956286Saw148015 	    path, (u_longlong_t)entry->fse_size);
3965673Saw148015 
3975673Saw148015 	return (0);
3985673Saw148015 }
3995673Saw148015 
4005673Saw148015 /*
4015673Saw148015  * given a fileset entry, determines if the associated file
4025673Saw148015  * needs to be allocated or not, and if so does the allocation.
4035673Saw148015  */
4045673Saw148015 static void *
4055673Saw148015 fileset_alloc_thread(filesetentry_t *entry)
4065673Saw148015 {
4075673Saw148015 	if (fileset_alloc_file(entry) == -1) {
4085673Saw148015 		(void) pthread_mutex_lock(&paralloc_lock);
4095673Saw148015 		paralloc_count = -1;
4105673Saw148015 	} else {
4115673Saw148015 		(void) pthread_mutex_lock(&paralloc_lock);
4125673Saw148015 		paralloc_count--;
4135673Saw148015 	}
4145673Saw148015 
4155673Saw148015 	(void) pthread_cond_signal(&paralloc_cv);
4165673Saw148015 	(void) pthread_mutex_unlock(&paralloc_lock);
4175673Saw148015 
4185673Saw148015 	pthread_exit(NULL);
4195673Saw148015 	return (NULL);
4205673Saw148015 }
4215673Saw148015 
4225184Sek110237 
4235184Sek110237 /*
4245184Sek110237  * First creates the parent directories of the file using
4255184Sek110237  * fileset_mkdir(). Then Optionally sets the O_DSYNC flag
4265184Sek110237  * and opens the file with open64(). It unlocks the fileset
4275184Sek110237  * entry lock, sets the DIRECTIO_ON or DIRECTIO_OFF flags
4285184Sek110237  * as requested, and returns the file descriptor integer
4295184Sek110237  * for the opened file.
4305184Sek110237  */
4315184Sek110237 int
4325184Sek110237 fileset_openfile(fileset_t *fileset,
4335184Sek110237     filesetentry_t *entry, int flag, int mode, int attrs)
4345184Sek110237 {
4355184Sek110237 	char path[MAXPATHLEN];
4365184Sek110237 	char dir[MAXPATHLEN];
4375184Sek110237 	char *pathtmp;
4385184Sek110237 	struct stat64 sb;
4395184Sek110237 	int fd;
4405184Sek110237 	int open_attrs = 0;
4415184Sek110237 
4425184Sek110237 	*path = 0;
4436212Saw148015 	(void) strcpy(path, avd_get_str(fileset->fs_path));
4445184Sek110237 	(void) strcat(path, "/");
4456212Saw148015 	(void) strcat(path, avd_get_str(fileset->fs_name));
4465184Sek110237 	pathtmp = fileset_resolvepath(entry);
4475184Sek110237 	(void) strcat(path, pathtmp);
4485184Sek110237 	(void) strcpy(dir, path);
4495184Sek110237 	free(pathtmp);
4505184Sek110237 	(void) trunc_dirname(dir);
4515184Sek110237 
4525184Sek110237 	/* If we are going to create a file, create the parent dirs */
4535184Sek110237 	if ((flag & O_CREAT) && (stat64(dir, &sb) != 0)) {
4545184Sek110237 		if (fileset_mkdir(dir, 0755) == -1)
4555184Sek110237 			return (-1);
4565184Sek110237 	}
4575184Sek110237 
458*6701Saw148015 	if (flag & O_CREAT) {
4595184Sek110237 		entry->fse_flags |= FSE_EXISTS;
4605184Sek110237 
461*6701Saw148015 		(void) ipc_mutex_lock(&fileset->fs_num_files_lock);
462*6701Saw148015 		fileset->fs_num_act_files++;
463*6701Saw148015 		(void) ipc_mutex_unlock(&fileset->fs_num_files_lock);
464*6701Saw148015 	}
465*6701Saw148015 
4665184Sek110237 	if (attrs & FLOW_ATTR_DSYNC) {
4675184Sek110237 #ifdef sun
4685184Sek110237 		open_attrs |= O_DSYNC;
4695184Sek110237 #else
4705184Sek110237 		open_attrs |= O_FSYNC;
4715184Sek110237 #endif
4725184Sek110237 	}
4735184Sek110237 
4745184Sek110237 	if ((fd = open64(path, flag | open_attrs, mode)) < 0) {
4755184Sek110237 		filebench_log(LOG_ERROR,
4765184Sek110237 		    "Failed to open file %s: %s",
4775184Sek110237 		    path, strerror(errno));
4785184Sek110237 		(void) ipc_mutex_unlock(&entry->fse_lock);
4795184Sek110237 		return (-1);
4805184Sek110237 	}
4815184Sek110237 	(void) ipc_mutex_unlock(&entry->fse_lock);
4825184Sek110237 
4835184Sek110237 #ifdef sun
4845184Sek110237 	if (attrs & FLOW_ATTR_DIRECTIO)
4855184Sek110237 		(void) directio(fd, DIRECTIO_ON);
4865184Sek110237 	else
4875184Sek110237 		(void) directio(fd, DIRECTIO_OFF);
4885184Sek110237 #endif
4895184Sek110237 
4905184Sek110237 	return (fd);
4915184Sek110237 }
4925184Sek110237 
4935184Sek110237 
4945184Sek110237 /*
4955184Sek110237  * Selects a fileset entry from a fileset. If the
4965184Sek110237  * FILESET_PICKDIR flag is set it will pick a directory
4975184Sek110237  * entry, otherwise a file entry. The FILESET_PICKRESET
4985184Sek110237  * flag will cause it to reset the free list to the
4995184Sek110237  * overall list (file or directory). The FILESET_PICKUNIQUE
5005184Sek110237  * flag will take an entry off of one of the free (unused)
5015184Sek110237  * lists (file or directory), otherwise the entry will be
5025184Sek110237  * picked off of one of the rotor lists (file or directory).
5035184Sek110237  * The FILESET_PICKEXISTS will insure that only extant
5045184Sek110237  * (FSE_EXISTS) state files are selected, while
5055184Sek110237  * FILESET_PICKNOEXIST insures that only non extant
5065184Sek110237  * (not FSE_EXISTS) state files are selected.
5076391Saw148015  * Note that the selected fileset entry (file) is returned
5086391Saw148015  * with its fse_lock field locked.
5095184Sek110237  */
5105184Sek110237 filesetentry_t *
5115184Sek110237 fileset_pick(fileset_t *fileset, int flags, int tid)
5125184Sek110237 {
5135184Sek110237 	filesetentry_t *entry = NULL;
5145184Sek110237 	filesetentry_t *first = NULL;
5155184Sek110237 
5166391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
5175184Sek110237 
518*6701Saw148015 	/* see if asking for impossible */
519*6701Saw148015 	(void) ipc_mutex_lock(&fileset->fs_num_files_lock);
520*6701Saw148015 	if (flags & FILESET_PICKEXISTS) {
521*6701Saw148015 		if (fileset->fs_num_act_files == 0) {
522*6701Saw148015 			(void) ipc_mutex_unlock(&fileset->fs_num_files_lock);
523*6701Saw148015 			(void) ipc_mutex_unlock(
524*6701Saw148015 			    &filebench_shm->shm_fileset_lock);
525*6701Saw148015 			return (NULL);
526*6701Saw148015 		}
527*6701Saw148015 	} else if (flags & FILESET_PICKNOEXIST) {
528*6701Saw148015 		if (fileset->fs_num_act_files == fileset->fs_realfiles) {
529*6701Saw148015 			(void) ipc_mutex_unlock(&fileset->fs_num_files_lock);
530*6701Saw148015 			(void) ipc_mutex_unlock(
531*6701Saw148015 			    &filebench_shm->shm_fileset_lock);
532*6701Saw148015 			return (NULL);
533*6701Saw148015 		}
534*6701Saw148015 	}
535*6701Saw148015 	(void) ipc_mutex_unlock(&fileset->fs_num_files_lock);
536*6701Saw148015 
5375184Sek110237 	while (entry == NULL) {
5385184Sek110237 
5395184Sek110237 		if ((flags & FILESET_PICKDIR) && (flags & FILESET_PICKRESET)) {
5405184Sek110237 			entry = fileset->fs_dirlist;
5415184Sek110237 			while (entry) {
5425184Sek110237 				entry->fse_flags |= FSE_FREE;
5435184Sek110237 				entry = entry->fse_dirnext;
5445184Sek110237 			}
5455184Sek110237 			fileset->fs_dirfree = fileset->fs_dirlist;
5465184Sek110237 		}
5475184Sek110237 
5485184Sek110237 		if (!(flags & FILESET_PICKDIR) && (flags & FILESET_PICKRESET)) {
5495184Sek110237 			entry = fileset->fs_filelist;
5505184Sek110237 			while (entry) {
5515184Sek110237 				entry->fse_flags |= FSE_FREE;
5525184Sek110237 				entry = entry->fse_filenext;
5535184Sek110237 			}
5545184Sek110237 			fileset->fs_filefree = fileset->fs_filelist;
5555184Sek110237 		}
5565184Sek110237 
5575184Sek110237 		if (flags & FILESET_PICKUNIQUE) {
5585184Sek110237 			if (flags & FILESET_PICKDIR) {
5595184Sek110237 				entry = fileset->fs_dirfree;
5605184Sek110237 				if (entry == NULL)
5615184Sek110237 					goto empty;
5625184Sek110237 				fileset->fs_dirfree = entry->fse_dirnext;
5635184Sek110237 			} else {
5645184Sek110237 				entry = fileset->fs_filefree;
5655184Sek110237 				if (entry == NULL)
5665184Sek110237 					goto empty;
5675184Sek110237 				fileset->fs_filefree = entry->fse_filenext;
5685184Sek110237 			}
5695184Sek110237 			entry->fse_flags &= ~FSE_FREE;
5705184Sek110237 		} else {
5715184Sek110237 			if (flags & FILESET_PICKDIR) {
5725184Sek110237 				entry = fileset->fs_dirrotor;
5735184Sek110237 				if (entry == NULL)
5745184Sek110237 				fileset->fs_dirrotor =
5755184Sek110237 				    entry = fileset->fs_dirlist;
5765184Sek110237 				fileset->fs_dirrotor = entry->fse_dirnext;
5775184Sek110237 			} else {
5785184Sek110237 				entry = fileset->fs_filerotor[tid];
5795184Sek110237 				if (entry == NULL)
5805184Sek110237 					fileset->fs_filerotor[tid] =
5815184Sek110237 					    entry = fileset->fs_filelist;
5825184Sek110237 				fileset->fs_filerotor[tid] =
5835184Sek110237 				    entry->fse_filenext;
5845184Sek110237 			}
5855184Sek110237 		}
5865184Sek110237 
5875184Sek110237 		if (first == entry)
5885184Sek110237 			goto empty;
5895184Sek110237 
5905184Sek110237 		if (first == NULL)
5915184Sek110237 			first = entry;
5925184Sek110237 
5935184Sek110237 		/* Return locked entry */
5945184Sek110237 		(void) ipc_mutex_lock(&entry->fse_lock);
5955184Sek110237 
5965184Sek110237 		/* If we ask for an existing file, go round again */
5975184Sek110237 		if ((flags & FILESET_PICKEXISTS) &&
5985184Sek110237 		    !(entry->fse_flags & FSE_EXISTS)) {
5995184Sek110237 			(void) ipc_mutex_unlock(&entry->fse_lock);
6005184Sek110237 			entry = NULL;
6015184Sek110237 		}
6025184Sek110237 
6035184Sek110237 		/* If we ask for not an existing file, go round again */
6045184Sek110237 		if ((flags & FILESET_PICKNOEXIST) &&
6055184Sek110237 		    (entry->fse_flags & FSE_EXISTS)) {
6065184Sek110237 			(void) ipc_mutex_unlock(&entry->fse_lock);
6075184Sek110237 			entry = NULL;
6085184Sek110237 		}
6095184Sek110237 	}
6105184Sek110237 
6116391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
6125184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "Picked file %s", entry->fse_path);
6135184Sek110237 	return (entry);
6145184Sek110237 
6155184Sek110237 empty:
6166391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
6175184Sek110237 	return (NULL);
6185184Sek110237 }
6195184Sek110237 
6205184Sek110237 /*
6215184Sek110237  * Given a fileset "fileset", create the associated files as
6225184Sek110237  * specified in the attributes of the fileset. The fileset is
6236212Saw148015  * rooted in a directory whose pathname is in fileset_path. If the
6245184Sek110237  * directory exists, meaning that there is already a fileset,
6256212Saw148015  * and the fileset_reuse attribute is false, then remove it and all
6265184Sek110237  * its contained files and subdirectories. Next, the routine
6275184Sek110237  * creates a root directory for the fileset. All the file type
6285184Sek110237  * filesetentries are cycled through creating as needed
6295184Sek110237  * their containing subdirectory trees in the filesystem and
6306212Saw148015  * creating actual files for fileset_preallocpercent of them. The
6315184Sek110237  * created files are filled with fse_size bytes of unitialized
6325184Sek110237  * data. The routine returns -1 on errors, 0 on success.
6335184Sek110237  */
6345184Sek110237 static int
6355184Sek110237 fileset_create(fileset_t *fileset)
6365184Sek110237 {
6375184Sek110237 	filesetentry_t *entry;
6385184Sek110237 	char path[MAXPATHLEN];
6395184Sek110237 	struct stat64 sb;
6405184Sek110237 	int pickflags = FILESET_PICKUNIQUE | FILESET_PICKRESET;
6415184Sek110237 	hrtime_t start = gethrtime();
6426212Saw148015 	char *fileset_path;
6436212Saw148015 	char *fileset_name;
6446212Saw148015 	int randno;
6455184Sek110237 	int preallocated = 0;
6465184Sek110237 	int reusing = 0;
6475184Sek110237 
6486212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
6495673Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
6505673Saw148015 		    fileset_entity_name(fileset));
6515184Sek110237 		return (-1);
6525184Sek110237 	}
6535184Sek110237 
6546212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
6556212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
6566212Saw148015 		    fileset_entity_name(fileset));
6576212Saw148015 		return (-1);
6586212Saw148015 	}
6596212Saw148015 
660*6701Saw148015 	/* declare all files currently non existant (single threaded code) */
661*6701Saw148015 	fileset->fs_num_act_files = 0;
662*6701Saw148015 
6635673Saw148015 #ifdef HAVE_RAW_SUPPORT
6645673Saw148015 	/* treat raw device as special case */
6655673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
6665673Saw148015 		return (0);
6675673Saw148015 #endif /* HAVE_RAW_SUPPORT */
6685673Saw148015 
6695184Sek110237 	/* XXX Add check to see if there is enough space */
6705184Sek110237 
6715184Sek110237 	/* Remove existing */
6726212Saw148015 	(void) strcpy(path, fileset_path);
6735184Sek110237 	(void) strcat(path, "/");
6746212Saw148015 	(void) strcat(path, fileset_name);
6755184Sek110237 	if ((stat64(path, &sb) == 0) && (strlen(path) > 3) &&
6766212Saw148015 	    (strlen(avd_get_str(fileset->fs_path)) > 2)) {
6776212Saw148015 		if (!avd_get_bool(fileset->fs_reuse)) {
6785184Sek110237 			char cmd[MAXPATHLEN];
6795184Sek110237 
6805184Sek110237 			(void) snprintf(cmd, sizeof (cmd), "rm -rf %s", path);
6815184Sek110237 			(void) system(cmd);
6825184Sek110237 			filebench_log(LOG_VERBOSE,
6836286Saw148015 			    "Removed any existing %s %s in %llu seconds",
6846212Saw148015 			    fileset_entity_name(fileset), fileset_name,
6856286Saw148015 			    (u_longlong_t)(((gethrtime() - start) /
6866286Saw148015 			    1000000000) + 1));
6875184Sek110237 		} else {
6885184Sek110237 			/* we are re-using */
6895184Sek110237 			reusing = 1;
6906613Sek110237 			filebench_log(LOG_VERBOSE, "Re-using %s %s.",
6916613Sek110237 			    fileset_entity_name(fileset), fileset_name);
6925184Sek110237 		}
6935184Sek110237 	}
6945184Sek110237 	(void) mkdir(path, 0755);
6955184Sek110237 
6965673Saw148015 	/* make the filesets directory tree */
6975673Saw148015 	if (fileset_create_subdirs(fileset, path) == -1)
6985673Saw148015 		return (-1);
6995673Saw148015 
7005184Sek110237 	start = gethrtime();
7015184Sek110237 
7025673Saw148015 	filebench_log(LOG_VERBOSE, "Creating %s %s...",
7036212Saw148015 	    fileset_entity_name(fileset), fileset_name);
7045673Saw148015 
7056212Saw148015 	if (!avd_get_bool(fileset->fs_prealloc))
7065673Saw148015 		goto exit;
7075184Sek110237 
7086212Saw148015 	randno = ((RAND_MAX * (100
7096212Saw148015 	    - avd_get_int(fileset->fs_preallocpercent))) / 100);
7106212Saw148015 
7115184Sek110237 	while (entry = fileset_pick(fileset, pickflags, 0)) {
7125673Saw148015 		pthread_t tid;
7135184Sek110237 
7145184Sek110237 		pickflags = FILESET_PICKUNIQUE;
7155184Sek110237 
7165184Sek110237 		entry->fse_flags &= ~FSE_EXISTS;
7175184Sek110237 
7185673Saw148015 		/* entry doesn't need to be locked during initialization */
7195673Saw148015 		(void) ipc_mutex_unlock(&entry->fse_lock);
7205673Saw148015 
7215673Saw148015 		if (rand() < randno)
7225184Sek110237 			continue;
7235184Sek110237 
7245184Sek110237 		preallocated++;
7255184Sek110237 
7265673Saw148015 		if (reusing)
7275673Saw148015 			entry->fse_flags |= FSE_REUSING;
7285673Saw148015 		else
7295673Saw148015 			entry->fse_flags &= (~FSE_REUSING);
7305673Saw148015 
7316212Saw148015 		if (avd_get_bool(fileset->fs_paralloc)) {
7325184Sek110237 
7335673Saw148015 			/* fire off a separate allocation thread */
7345673Saw148015 			(void) pthread_mutex_lock(&paralloc_lock);
7355673Saw148015 			while (paralloc_count >= MAX_PARALLOC_THREADS) {
7365673Saw148015 				(void) pthread_cond_wait(
7375673Saw148015 				    &paralloc_cv, &paralloc_lock);
7385673Saw148015 			}
7395673Saw148015 
7405673Saw148015 			if (paralloc_count < 0) {
7415673Saw148015 				(void) pthread_mutex_unlock(&paralloc_lock);
7425184Sek110237 				return (-1);
7435184Sek110237 			}
7445184Sek110237 
7455673Saw148015 			paralloc_count++;
7465673Saw148015 			(void) pthread_mutex_unlock(&paralloc_lock);
7475184Sek110237 
7485673Saw148015 			if (pthread_create(&tid, NULL,
7495673Saw148015 			    (void *(*)(void*))fileset_alloc_thread,
7505673Saw148015 			    entry) != 0) {
7515184Sek110237 				filebench_log(LOG_ERROR,
7525673Saw148015 				    "File prealloc thread create failed");
7535673Saw148015 				filebench_shutdown(1);
7545184Sek110237 			}
7555184Sek110237 
7565673Saw148015 		} else {
7575673Saw148015 			if (fileset_alloc_file(entry) == -1)
7585673Saw148015 				return (-1);
7595673Saw148015 		}
7605673Saw148015 	}
7615184Sek110237 
7625673Saw148015 exit:
7635184Sek110237 	filebench_log(LOG_VERBOSE,
7646286Saw148015 	    "Preallocated %d of %llu of %s %s in %llu seconds",
7655184Sek110237 	    preallocated,
7666286Saw148015 	    (u_longlong_t)fileset->fs_constentries,
7676212Saw148015 	    fileset_entity_name(fileset), fileset_name,
7686286Saw148015 	    (u_longlong_t)(((gethrtime() - start) / 1000000000) + 1));
7695184Sek110237 
7705184Sek110237 	return (0);
7715184Sek110237 }
7725184Sek110237 
7735184Sek110237 /*
7745184Sek110237  * Adds an entry to the fileset's file list. Single threaded so
7755184Sek110237  * no locking needed.
7765184Sek110237  */
7775184Sek110237 static void
7785184Sek110237 fileset_insfilelist(fileset_t *fileset, filesetentry_t *entry)
7795184Sek110237 {
7805184Sek110237 	if (fileset->fs_filelist == NULL) {
7815184Sek110237 		fileset->fs_filelist = entry;
7825184Sek110237 		entry->fse_filenext = NULL;
7835184Sek110237 	} else {
7845184Sek110237 		entry->fse_filenext = fileset->fs_filelist;
7855184Sek110237 		fileset->fs_filelist = entry;
7865184Sek110237 	}
7875184Sek110237 }
7885184Sek110237 
7895184Sek110237 /*
7905184Sek110237  * Adds an entry to the fileset's directory list. Single
7915184Sek110237  * threaded so no locking needed.
7925184Sek110237  */
7935184Sek110237 static void
7945184Sek110237 fileset_insdirlist(fileset_t *fileset, filesetentry_t *entry)
7955184Sek110237 {
7965184Sek110237 	if (fileset->fs_dirlist == NULL) {
7975184Sek110237 		fileset->fs_dirlist = entry;
7985184Sek110237 		entry->fse_dirnext = NULL;
7995184Sek110237 	} else {
8005184Sek110237 		entry->fse_dirnext = fileset->fs_dirlist;
8015184Sek110237 		fileset->fs_dirlist = entry;
8025184Sek110237 	}
8035184Sek110237 }
8045184Sek110237 
8055184Sek110237 /*
8065184Sek110237  * Obtaines a filesetentry entity for a file to be placed in a
8075184Sek110237  * (sub)directory of a fileset. The size of the file may be
8086212Saw148015  * specified by fileset_meansize, or calculated from a gamma
8096212Saw148015  * distribution of parameter fileset_sizegamma and of mean size
8106212Saw148015  * fileset_meansize. The filesetentry entity is placed on the file
8115184Sek110237  * list in the specified parent filesetentry entity, which may
8125184Sek110237  * be a directory filesetentry, or the root filesetentry in the
8135184Sek110237  * fileset. It is also placed on the fileset's list of all
8145184Sek110237  * contained files. Returns 0 if successful or -1 if ipc memory
8155184Sek110237  * for the path string cannot be allocated.
8165184Sek110237  */
8175184Sek110237 static int
8185184Sek110237 fileset_populate_file(fileset_t *fileset, filesetentry_t *parent, int serial)
8195184Sek110237 {
8205184Sek110237 	char tmpname[16];
8215184Sek110237 	filesetentry_t *entry;
8225184Sek110237 	double drand;
8235184Sek110237 
8245184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
8255184Sek110237 	    == NULL) {
8265184Sek110237 		filebench_log(LOG_ERROR,
8275184Sek110237 		    "fileset_populate_file: Can't malloc filesetentry");
8285184Sek110237 		return (-1);
8295184Sek110237 	}
8305184Sek110237 
8315184Sek110237 	(void) pthread_mutex_init(&entry->fse_lock, ipc_mutexattr());
8325184Sek110237 	entry->fse_parent = parent;
8335184Sek110237 	entry->fse_fileset = fileset;
8345184Sek110237 	entry->fse_flags |= FSE_FREE;
8355184Sek110237 	fileset_insfilelist(fileset, entry);
8365184Sek110237 
8375184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
8385184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
8395184Sek110237 		filebench_log(LOG_ERROR,
8405184Sek110237 		    "fileset_populate_file: Can't alloc path string");
8415184Sek110237 		return (-1);
8425184Sek110237 	}
8435184Sek110237 
8446212Saw148015 	/* see if random variable was supplied for file size */
8456212Saw148015 	if (fileset->fs_meansize == -1) {
8466212Saw148015 		entry->fse_size = (off64_t)avd_get_int(fileset->fs_size);
8476212Saw148015 	} else {
8486212Saw148015 		double gamma;
8495184Sek110237 
8506212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
8516212Saw148015 		if (gamma > 0) {
8526212Saw148015 			drand = gamma_dist_knuth(gamma,
8536212Saw148015 			    fileset->fs_meansize / gamma);
8546212Saw148015 			entry->fse_size = (off64_t)drand;
8556212Saw148015 		} else {
8566212Saw148015 			entry->fse_size = (off64_t)fileset->fs_meansize;
8576212Saw148015 		}
8585184Sek110237 	}
8595184Sek110237 
8605184Sek110237 	fileset->fs_bytes += entry->fse_size;
8615184Sek110237 
8625184Sek110237 	fileset->fs_realfiles++;
8635184Sek110237 	return (0);
8645184Sek110237 }
8655184Sek110237 
8665184Sek110237 /*
8675184Sek110237  * Creates a directory node in a fileset, by obtaining a
8685184Sek110237  * filesetentry entity for the node and initializing it
8695184Sek110237  * according to parameters of the fileset. It determines a
8705184Sek110237  * directory tree depth and directory width, optionally using
8715184Sek110237  * a gamma distribution. If its calculated depth is less then
8725184Sek110237  * its actual depth in the directory tree, it becomes a leaf
8735184Sek110237  * node and files itself with "width" number of file type
8745184Sek110237  * filesetentries, otherwise it files itself with "width"
8755184Sek110237  * number of directory type filesetentries, using recursive
8765184Sek110237  * calls to fileset_populate_subdir. The end result of the
8775184Sek110237  * initial call to this routine is a tree of directories of
8785184Sek110237  * random width and varying depth with sufficient leaf
8795184Sek110237  * directories to contain all required files.
8805184Sek110237  * Returns 0 on success. Returns -1 if ipc path string memory
8815184Sek110237  * cannot be allocated and returns an error code (currently
8825184Sek110237  * also -1) from calls to fileset_populate_file or recursive
8835184Sek110237  * calls to fileset_populate_subdir.
8845184Sek110237  */
8855184Sek110237 static int
8865184Sek110237 fileset_populate_subdir(fileset_t *fileset, filesetentry_t *parent,
8875184Sek110237     int serial, double depth)
8885184Sek110237 {
8896212Saw148015 	double randepth, drand, ranwidth;
8905184Sek110237 	int isleaf = 0;
8915184Sek110237 	char tmpname[16];
8925184Sek110237 	filesetentry_t *entry;
8935184Sek110237 	int i;
8945184Sek110237 
8955184Sek110237 	depth += 1;
8965184Sek110237 
8975184Sek110237 	/* Create dir node */
8985184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
8995184Sek110237 	    == NULL) {
9005184Sek110237 		filebench_log(LOG_ERROR,
9015184Sek110237 		    "fileset_populate_subdir: Can't malloc filesetentry");
9025184Sek110237 		return (-1);
9035184Sek110237 	}
9045184Sek110237 
9055184Sek110237 	(void) pthread_mutex_init(&entry->fse_lock, ipc_mutexattr());
9065184Sek110237 
9075184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
9085184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
9095184Sek110237 		filebench_log(LOG_ERROR,
9105184Sek110237 		    "fileset_populate_subdir: Can't alloc path string");
9115184Sek110237 		return (-1);
9125184Sek110237 	}
9135184Sek110237 
9145184Sek110237 	entry->fse_parent = parent;
9155184Sek110237 	entry->fse_flags |= FSE_DIR | FSE_FREE;
9165184Sek110237 	fileset_insdirlist(fileset, entry);
9175184Sek110237 
9186212Saw148015 	if (fileset->fs_dirdepthrv) {
9196212Saw148015 		randepth = (int)avd_get_int(fileset->fs_dirdepthrv);
9205184Sek110237 	} else {
9216212Saw148015 		double gamma;
9226212Saw148015 
9236212Saw148015 		gamma = avd_get_int(fileset->fs_dirgamma) / 1000.0;
9246212Saw148015 		if (gamma > 0) {
9256212Saw148015 			drand = gamma_dist_knuth(gamma,
9266212Saw148015 			    fileset->fs_meandepth / gamma);
9276212Saw148015 			randepth = (int)drand;
9286212Saw148015 		} else {
9296212Saw148015 			randepth = (int)fileset->fs_meandepth;
9306212Saw148015 		}
9315184Sek110237 	}
9325184Sek110237 
9336212Saw148015 	if (fileset->fs_meanwidth == -1) {
9346212Saw148015 		ranwidth = avd_get_dbl(fileset->fs_dirwidth);
9356212Saw148015 	} else {
9366212Saw148015 		double gamma;
9375184Sek110237 
9386212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
9396212Saw148015 		if (gamma > 0) {
9406212Saw148015 			drand = gamma_dist_knuth(gamma,
9416212Saw148015 			    fileset->fs_meanwidth / gamma);
9426212Saw148015 			ranwidth = drand;
9436212Saw148015 		} else {
9446212Saw148015 			ranwidth = fileset->fs_meanwidth;
9456212Saw148015 		}
9465184Sek110237 	}
9475184Sek110237 
9485184Sek110237 	if (randepth == 0)
9495184Sek110237 		randepth = 1;
9505184Sek110237 	if (ranwidth == 0)
9515184Sek110237 		ranwidth = 1;
9525184Sek110237 	if (depth >= randepth)
9535184Sek110237 		isleaf = 1;
9545184Sek110237 
9555184Sek110237 	/*
9565184Sek110237 	 * Create directory of random width according to distribution, or
9575184Sek110237 	 * if root directory, continue until #files required
9585184Sek110237 	 */
9596212Saw148015 	for (i = 1; ((parent == NULL) || (i < ranwidth + 1)) &&
9606212Saw148015 	    (fileset->fs_realfiles < fileset->fs_constentries);
9616212Saw148015 	    i++) {
9625184Sek110237 		int ret = 0;
9635184Sek110237 
9645184Sek110237 		if (parent && isleaf)
9655184Sek110237 			ret = fileset_populate_file(fileset, entry, i);
9665184Sek110237 		else
9675184Sek110237 			ret = fileset_populate_subdir(fileset, entry, i, depth);
9685184Sek110237 
9695184Sek110237 		if (ret != 0)
9705184Sek110237 			return (ret);
9715184Sek110237 	}
9725184Sek110237 	return (0);
9735184Sek110237 }
9745184Sek110237 
9755184Sek110237 /*
9765184Sek110237  * Populates a fileset with files and subdirectory entries. Uses
9776212Saw148015  * the supplied fileset_dirwidth and fileset_entries (number of files) to
9786212Saw148015  * calculate the required fileset_meandepth (of subdirectories) and
9796212Saw148015  * initialize the fileset_meanwidth and fileset_meansize variables. Then
9805184Sek110237  * calls fileset_populate_subdir() to do the recursive
9815184Sek110237  * subdirectory entry creation and leaf file entry creation. All
9825184Sek110237  * of the above is skipped if the fileset has already been
9835184Sek110237  * populated. Returns 0 on success, or an error code from the
9845184Sek110237  * call to fileset_populate_subdir if that call fails.
9855184Sek110237  */
9865184Sek110237 static int
9875184Sek110237 fileset_populate(fileset_t *fileset)
9885184Sek110237 {
9896212Saw148015 	int entries = (int)avd_get_int(fileset->fs_entries);
9906212Saw148015 	int meandirwidth;
9915184Sek110237 	int ret;
9925184Sek110237 
9935184Sek110237 	/* Skip if already populated */
9945184Sek110237 	if (fileset->fs_bytes > 0)
9955184Sek110237 		goto exists;
9965184Sek110237 
9975673Saw148015 #ifdef HAVE_RAW_SUPPORT
9985673Saw148015 	/* check for raw device */
9995673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
10005673Saw148015 		return (0);
10015673Saw148015 #endif /* HAVE_RAW_SUPPORT */
10025673Saw148015 
10036212Saw148015 	/* save value of entries obtained for later, in case it was random */
10046212Saw148015 	fileset->fs_constentries = entries;
10056212Saw148015 
10066212Saw148015 	/* is dirwidth a random variable? */
10076212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_dirwidth)) {
10086212Saw148015 		meandirwidth =
10096212Saw148015 		    (int)fileset->fs_dirwidth->avd_val.randptr->rnd_dbl_mean;
10106212Saw148015 		fileset->fs_meanwidth = -1;
10116212Saw148015 	} else {
10126212Saw148015 		meandirwidth = (int)avd_get_int(fileset->fs_dirwidth);
10136212Saw148015 		fileset->fs_meanwidth = (double)meandirwidth;
10146212Saw148015 	}
10156212Saw148015 
10165184Sek110237 	/*
10175184Sek110237 	 * Input params are:
10185184Sek110237 	 *	# of files
10195184Sek110237 	 *	ave # of files per dir
10205184Sek110237 	 *	max size of dir
10215184Sek110237 	 *	# ave size of file
10225184Sek110237 	 *	max size of file
10235184Sek110237 	 */
10246212Saw148015 	fileset->fs_meandepth = log(entries) / log(meandirwidth);
10256212Saw148015 
10266212Saw148015 	/* Has a random variable been supplied for dirdepth? */
10276212Saw148015 	if (fileset->fs_dirdepthrv) {
10286212Saw148015 		/* yes, so set the random variable's mean value to meandepth */
10296212Saw148015 		fileset->fs_dirdepthrv->avd_val.randptr->rnd_dbl_mean =
10306212Saw148015 		    fileset->fs_meandepth;
10316212Saw148015 	}
10326212Saw148015 
10336212Saw148015 	/* test for random size variable */
10346212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_size))
10356212Saw148015 		fileset->fs_meansize = -1;
10366212Saw148015 	else
10376212Saw148015 		fileset->fs_meansize = avd_get_int(fileset->fs_size);
10385184Sek110237 
10395184Sek110237 	if ((ret = fileset_populate_subdir(fileset, NULL, 1, 0)) != 0)
10405184Sek110237 		return (ret);
10415184Sek110237 
10425184Sek110237 
10435184Sek110237 exists:
10445673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
10456286Saw148015 		filebench_log(LOG_VERBOSE, "File %s: mbytes=%llu",
10466212Saw148015 		    avd_get_str(fileset->fs_name),
10476286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
10485673Saw148015 	} else {
10496286Saw148015 		filebench_log(LOG_VERBOSE, "Fileset %s: %d files, "
10506286Saw148015 		    "avg dir = %d, avg depth = %.1lf, mbytes=%llu",
10516212Saw148015 		    avd_get_str(fileset->fs_name), entries,
10526212Saw148015 		    meandirwidth,
10535673Saw148015 		    fileset->fs_meandepth,
10546286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
10555673Saw148015 	}
1056*6701Saw148015 
10575184Sek110237 	return (0);
10585184Sek110237 }
10595184Sek110237 
10605184Sek110237 /*
10616212Saw148015  * Allocates a fileset instance, initializes fileset_dirgamma and
10626212Saw148015  * fileset_sizegamma default values, and sets the fileset name to the
10635184Sek110237  * supplied name string. Puts the allocated fileset on the
10645184Sek110237  * master fileset list and returns a pointer to it.
1065*6701Saw148015  *
1066*6701Saw148015  * This routine implements the 'define fileset' calls found in a .f
1067*6701Saw148015  * workload, such as in the following example:
1068*6701Saw148015  * define fileset name=drew4ever, entries=$nfiles
10695184Sek110237  */
10705184Sek110237 fileset_t *
10716212Saw148015 fileset_define(avd_t name)
10725184Sek110237 {
10735184Sek110237 	fileset_t *fileset;
10745184Sek110237 
10755184Sek110237 	if (name == NULL)
10765184Sek110237 		return (NULL);
10775184Sek110237 
10785184Sek110237 	if ((fileset = (fileset_t *)ipc_malloc(FILEBENCH_FILESET)) == NULL) {
10795184Sek110237 		filebench_log(LOG_ERROR,
10805184Sek110237 		    "fileset_define: Can't malloc fileset");
10815184Sek110237 		return (NULL);
10825184Sek110237 	}
10835184Sek110237 
10846212Saw148015 	filebench_log(LOG_DEBUG_IMPL,
10856212Saw148015 	    "Defining file %s", avd_get_str(name));
10865184Sek110237 
1087*6701Saw148015 	/* initialize fs_num_act_files lock */
1088*6701Saw148015 	(void) pthread_mutex_init(&fileset->fs_num_files_lock,
1089*6701Saw148015 	    ipc_mutexattr());
1090*6701Saw148015 
10916391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
10925184Sek110237 
10936212Saw148015 	fileset->fs_dirgamma = avd_int_alloc(1500);
10946212Saw148015 	fileset->fs_sizegamma = avd_int_alloc(1500);
10955184Sek110237 
10965184Sek110237 	/* Add fileset to global list */
10976391Saw148015 	if (filebench_shm->shm_filesetlist == NULL) {
10986391Saw148015 		filebench_shm->shm_filesetlist = fileset;
10995184Sek110237 		fileset->fs_next = NULL;
11005184Sek110237 	} else {
11016391Saw148015 		fileset->fs_next = filebench_shm->shm_filesetlist;
11026391Saw148015 		filebench_shm->shm_filesetlist = fileset;
11035184Sek110237 	}
11045184Sek110237 
11056391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
11065184Sek110237 
11076212Saw148015 	fileset->fs_name = name;
11085184Sek110237 
11095184Sek110237 	return (fileset);
11105184Sek110237 }
11115184Sek110237 
11125184Sek110237 /*
11135184Sek110237  * If supplied with a pointer to a fileset and the fileset's
11146212Saw148015  * fileset_prealloc flag is set, calls fileset_populate() to populate
11155184Sek110237  * the fileset with filesetentries, then calls fileset_create()
11165184Sek110237  * to make actual directories and files for the filesetentries.
11175184Sek110237  * Otherwise, it applies fileset_populate() and fileset_create()
11185184Sek110237  * to all the filesets on the master fileset list. It always
11195184Sek110237  * returns zero (0) if one fileset is populated / created,
11205184Sek110237  * otherwise it returns the sum of returned values from
11215184Sek110237  * fileset_create() and fileset_populate(), which
11225184Sek110237  * will be a negative one (-1) times the number of
11235184Sek110237  * fileset_create() calls which failed.
11245184Sek110237  */
11255184Sek110237 int
11265184Sek110237 fileset_createset(fileset_t *fileset)
11275184Sek110237 {
11285184Sek110237 	fileset_t *list;
11295184Sek110237 	int ret = 0;
11305184Sek110237 
11315673Saw148015 	/* set up for possible parallel allocate */
11325673Saw148015 	paralloc_count = 0;
11335673Saw148015 
11346212Saw148015 	if (fileset && avd_get_bool(fileset->fs_prealloc)) {
11355673Saw148015 
11366305Saw148015 		/* check for raw files */
11376305Saw148015 		if (fileset_checkraw(fileset)) {
11386305Saw148015 			filebench_log(LOG_INFO,
11396305Saw148015 			    "file %s/%s is a RAW device",
11406305Saw148015 			    avd_get_str(fileset->fs_path),
11416305Saw148015 			    avd_get_str(fileset->fs_name));
11426305Saw148015 			return (0);
11436305Saw148015 		}
11446305Saw148015 
11455673Saw148015 		filebench_log(LOG_INFO,
11465673Saw148015 		    "creating/pre-allocating %s %s",
11476212Saw148015 		    fileset_entity_name(fileset),
11486212Saw148015 		    avd_get_str(fileset->fs_name));
11495673Saw148015 
11505184Sek110237 		if ((ret = fileset_populate(fileset)) != 0)
11515184Sek110237 			return (ret);
11525673Saw148015 
11535673Saw148015 		if ((ret = fileset_create(fileset)) != 0)
11545673Saw148015 			return (ret);
11555673Saw148015 	} else {
11565673Saw148015 
11575673Saw148015 		filebench_log(LOG_INFO,
11585673Saw148015 		    "Creating/pre-allocating files and filesets");
11595673Saw148015 
11606391Saw148015 		list = filebench_shm->shm_filesetlist;
11615673Saw148015 		while (list) {
11626305Saw148015 			/* check for raw files */
11636305Saw148015 			if (fileset_checkraw(list)) {
11646305Saw148015 				filebench_log(LOG_INFO,
11656305Saw148015 				    "file %s/%s is a RAW device",
11666305Saw148015 				    avd_get_str(list->fs_path),
11676305Saw148015 				    avd_get_str(list->fs_name));
11686305Saw148015 				list = list->fs_next;
11696305Saw148015 				continue;
11706305Saw148015 			}
11716305Saw148015 
11725673Saw148015 			if ((ret = fileset_populate(list)) != 0)
11735673Saw148015 				return (ret);
11745673Saw148015 			if ((ret = fileset_create(list)) != 0)
11755673Saw148015 				return (ret);
11765673Saw148015 			list = list->fs_next;
11775673Saw148015 		}
11785184Sek110237 	}
11795184Sek110237 
11805673Saw148015 	/* wait for allocation threads to finish */
11815673Saw148015 	filebench_log(LOG_INFO,
11825673Saw148015 	    "waiting for fileset pre-allocation to finish");
11835184Sek110237 
11845673Saw148015 	(void) pthread_mutex_lock(&paralloc_lock);
11855673Saw148015 	while (paralloc_count > 0)
11865673Saw148015 		(void) pthread_cond_wait(&paralloc_cv, &paralloc_lock);
11875673Saw148015 	(void) pthread_mutex_unlock(&paralloc_lock);
11885673Saw148015 
11895673Saw148015 	if (paralloc_count < 0)
11905673Saw148015 		return (-1);
11915673Saw148015 
11925673Saw148015 	return (0);
11935184Sek110237 }
11945184Sek110237 
11955184Sek110237 /*
11965184Sek110237  * Searches through the master fileset list for the named fileset.
11975184Sek110237  * If found, returns pointer to same, otherwise returns NULL.
11985184Sek110237  */
11995184Sek110237 fileset_t *
12005184Sek110237 fileset_find(char *name)
12015184Sek110237 {
12026391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
12035184Sek110237 
12046391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
12055184Sek110237 
12065184Sek110237 	while (fileset) {
12076212Saw148015 		if (strcmp(name, avd_get_str(fileset->fs_name)) == 0) {
12086391Saw148015 			(void) ipc_mutex_unlock(
12096391Saw148015 			    &filebench_shm->shm_fileset_lock);
12105184Sek110237 			return (fileset);
12115184Sek110237 		}
12125184Sek110237 		fileset = fileset->fs_next;
12135184Sek110237 	}
12146391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
12155184Sek110237 
12165184Sek110237 	return (NULL);
12175184Sek110237 }
12185673Saw148015 
12195673Saw148015 /*
12205673Saw148015  * Iterates over all the file sets in the filesetlist,
12215673Saw148015  * executing the supplied command "*cmd()" on them. Also
12225673Saw148015  * indicates to the executed command if it is the first
12235673Saw148015  * time the command has been executed since the current
12245673Saw148015  * call to fileset_iter.
12255673Saw148015  */
12265673Saw148015 void
12275673Saw148015 fileset_iter(int (*cmd)(fileset_t *fileset, int first))
12285673Saw148015 {
12296391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
12305673Saw148015 	int count = 0;
12315673Saw148015 
12326391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
12335673Saw148015 
12345673Saw148015 	while (fileset) {
12355673Saw148015 		cmd(fileset, count == 0);
12365673Saw148015 		fileset = fileset->fs_next;
12375673Saw148015 		count++;
12385673Saw148015 	}
12395673Saw148015 
12406391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
12415673Saw148015 }
12425673Saw148015 
12435673Saw148015 /*
12445673Saw148015  * Prints information to the filebench log about the file
12455673Saw148015  * object. Also prints a header on the first call.
12465673Saw148015  */
12475673Saw148015 int
12485673Saw148015 fileset_print(fileset_t *fileset, int first)
12495673Saw148015 {
12506212Saw148015 	int pathlength;
12516212Saw148015 	char *fileset_path;
12526212Saw148015 	char *fileset_name;
12536212Saw148015 	static char pad[] = "                              "; /* 30 spaces */
12546212Saw148015 
12556212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
12566212Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
12576212Saw148015 		    fileset_entity_name(fileset));
12586212Saw148015 		return (-1);
12596212Saw148015 	}
12606212Saw148015 
12616212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
12626212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
12636212Saw148015 		    fileset_entity_name(fileset));
12646212Saw148015 		return (-1);
12656212Saw148015 	}
12666212Saw148015 
12676212Saw148015 	pathlength = strlen(fileset_path) + strlen(fileset_name);
12685673Saw148015 
12695673Saw148015 	if (pathlength > 29)
12705673Saw148015 		pathlength = 29;
12715673Saw148015 
12725673Saw148015 	if (first) {
12735673Saw148015 		filebench_log(LOG_INFO, "File or Fileset name%20s%12s%10s",
12745673Saw148015 		    "file size",
12755673Saw148015 		    "dir width",
12765673Saw148015 		    "entries");
12775673Saw148015 	}
12785673Saw148015 
12795673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
12805673Saw148015 		if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
12815673Saw148015 			filebench_log(LOG_INFO,
12825673Saw148015 			    "%s/%s%s         (Raw Device)",
12836212Saw148015 			    fileset_path, fileset_name, &pad[pathlength]);
12845673Saw148015 		} else {
12855673Saw148015 			filebench_log(LOG_INFO,
12866286Saw148015 			    "%s/%s%s%9llu     (Single File)",
12876212Saw148015 			    fileset_path, fileset_name, &pad[pathlength],
12886286Saw148015 			    (u_longlong_t)avd_get_int(fileset->fs_size));
12895673Saw148015 		}
12905673Saw148015 	} else {
12916286Saw148015 		filebench_log(LOG_INFO, "%s/%s%s%9llu%12llu%10llu",
12926212Saw148015 		    fileset_path, fileset_name,
12935673Saw148015 		    &pad[pathlength],
12946286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_size),
12956286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_dirwidth),
12966286Saw148015 		    (u_longlong_t)fileset->fs_constentries);
12975673Saw148015 	}
12985673Saw148015 	return (0);
12995673Saw148015 }
13005673Saw148015 /*
13015673Saw148015  * checks to see if the path/name pair points to a raw device. If
13025673Saw148015  * so it sets the raw device flag (FILESET_IS_RAW_DEV) and returns 1.
13035673Saw148015  * If RAW is not defined, or it is not a raw device, it clears the
13045673Saw148015  * raw device flag and returns 0.
13055673Saw148015  */
13065673Saw148015 int
13075673Saw148015 fileset_checkraw(fileset_t *fileset)
13085673Saw148015 {
13095673Saw148015 	char path[MAXPATHLEN];
13105673Saw148015 	struct stat64 sb;
13116305Saw148015 	char *pathname;
13126305Saw148015 	char *setname;
13135673Saw148015 
13145673Saw148015 	fileset->fs_attrs &= (~FILESET_IS_RAW_DEV);
13155673Saw148015 
13165673Saw148015 #ifdef HAVE_RAW_SUPPORT
13175673Saw148015 	/* check for raw device */
13186305Saw148015 	if ((pathname = avd_get_str(fileset->fs_path)) == NULL)
13196305Saw148015 		return (0);
13206305Saw148015 
13216305Saw148015 	if ((setname = avd_get_str(fileset->fs_name)) == NULL)
13226305Saw148015 		return (0);
13236305Saw148015 
13246305Saw148015 	(void) strcpy(path, pathname);
13255673Saw148015 	(void) strcat(path, "/");
13266305Saw148015 	(void) strcat(path, setname);
13275673Saw148015 	if ((stat64(path, &sb) == 0) &&
13285673Saw148015 	    ((sb.st_mode & S_IFMT) == S_IFBLK) && sb.st_rdev) {
13295673Saw148015 		fileset->fs_attrs |= FILESET_IS_RAW_DEV;
13306305Saw148015 		if (!(fileset->fs_attrs & FILESET_IS_FILE)) {
13316305Saw148015 			filebench_log(LOG_ERROR,
13326305Saw148015 			    "WARNING Fileset %s/%s Cannot be RAW device",
13336305Saw148015 			    avd_get_str(fileset->fs_path),
13346305Saw148015 			    avd_get_str(fileset->fs_name));
13356305Saw148015 			filebench_shutdown(1);
13366305Saw148015 		}
13376305Saw148015 
13385673Saw148015 		return (1);
13395673Saw148015 	}
13405673Saw148015 #endif /* HAVE_RAW_SUPPORT */
13415673Saw148015 
13425673Saw148015 	return (0);
13435673Saw148015 }
1344