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 #include <fcntl.h>
295184Sek110237 #include <pthread.h>
305184Sek110237 #include <errno.h>
315184Sek110237 #include <math.h>
325184Sek110237 #include <libgen.h>
335184Sek110237 #include <sys/mman.h>
346613Sek110237 
356613Sek110237 #include "filebench.h"
365184Sek110237 #include "fileset.h"
375184Sek110237 #include "gamma_dist.h"
385184Sek110237 
395184Sek110237 /*
405184Sek110237  * File sets, of type fileset_t, are entities which contain
415184Sek110237  * information about collections of files and subdirectories in Filebench.
425184Sek110237  * The fileset, once populated, consists of a tree of fileset entries of
435184Sek110237  * type filesetentry_t which specify files and directories.  The fileset
446212Saw148015  * is rooted in a directory specified by fileset_path, and once the populated
455184Sek110237  * fileset has been created, has a tree of directories and files
465184Sek110237  * corresponding to the fileset's filesetentry tree.
476701Saw148015  *
48*7556SAndrew.W.Wilson@sun.com  * Fileset entities are allocated by fileset_define() which is called from
49*7556SAndrew.W.Wilson@sun.com  * parser_gram.y: parser_fileset_define(). The filesetentry tree corrseponding
50*7556SAndrew.W.Wilson@sun.com  * to the eventual directory and file tree to be instantiated on the storage
51*7556SAndrew.W.Wilson@sun.com  * medium is built by fileset_populate(), which is called from
52*7556SAndrew.W.Wilson@sun.com  * fileset_createset(). After calling fileset_populate(), fileset_createset()
53*7556SAndrew.W.Wilson@sun.com  * will call fileset_create() to pre-allocate designated files and directories.
54*7556SAndrew.W.Wilson@sun.com  *
55*7556SAndrew.W.Wilson@sun.com  * Fileset_createset() is called from parser_gram.y: parser_create_fileset()
56*7556SAndrew.W.Wilson@sun.com  * when a "create fileset" or "run" command is encountered. When the
57*7556SAndrew.W.Wilson@sun.com  * "create fileset" command is used, it is generally paired with
586701Saw148015  * a "create processes" command, and must appear first, in order to
596701Saw148015  * instantiate all the files in the fileset before trying to use them.
605184Sek110237  */
615184Sek110237 
626305Saw148015 static int fileset_checkraw(fileset_t *fileset);
636305Saw148015 
64*7556SAndrew.W.Wilson@sun.com /* maximum parallel allocation control */
655673Saw148015 #define	MAX_PARALLOC_THREADS 32
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.
132*7556SAndrew.W.Wilson@sun.com  * If successful, returns 0. Otherwise returns -1 times the number of
133*7556SAndrew.W.Wilson@sun.com  * 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);
227*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
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 
237*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_ERROR);
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 */
259*7556SAndrew.W.Wilson@sun.com 		if (fileset_mkdir(full_path, 0755) == FILEBENCH_ERROR)
260*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
2615673Saw148015 
2625673Saw148015 		direntry = direntry->fse_dirnext;
2635673Saw148015 	}
264*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
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);
296*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
2975673Saw148015 		}
2985673Saw148015 
2995673Saw148015 		if (sb.st_size == (off64_t)entry->fse_size) {
300*7556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_IMPL,
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 
307*7556SAndrew.W.Wilson@sun.com 			(void) ipc_mutex_lock(
308*7556SAndrew.W.Wilson@sun.com 			    &entry->fse_fileset->fs_pick_lock);
3095673Saw148015 			entry->fse_flags |= FSE_EXISTS;
3106701Saw148015 			entry->fse_fileset->fs_num_act_files++;
3116701Saw148015 			(void) ipc_mutex_unlock(
312*7556SAndrew.W.Wilson@sun.com 			    &entry->fse_fileset->fs_pick_lock);
3136701Saw148015 
3145673Saw148015 			(void) close(fd);
315*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
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 
332*7556SAndrew.W.Wilson@sun.com 			(void) ipc_mutex_lock(
333*7556SAndrew.W.Wilson@sun.com 			    &entry->fse_fileset->fs_pick_lock);
3345673Saw148015 			entry->fse_flags |= FSE_EXISTS;
3356701Saw148015 			entry->fse_fileset->fs_num_act_files++;
3366701Saw148015 			(void) ipc_mutex_unlock(
337*7556SAndrew.W.Wilson@sun.com 			    &entry->fse_fileset->fs_pick_lock);
3386701Saw148015 
3395673Saw148015 			(void) close(fd);
340*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
3415673Saw148015 		}
3425673Saw148015 	} else {
3435673Saw148015 
3445673Saw148015 		/* No file or not reusing, so create */
3455673Saw148015 		if ((fd = open64(path, O_RDWR | O_CREAT, 0644)) < 0) {
3465673Saw148015 			filebench_log(LOG_ERROR,
3475673Saw148015 			    "Failed to pre-allocate file %s: %s",
3485673Saw148015 			    path, strerror(errno));
3495673Saw148015 
350*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
3515673Saw148015 		}
3525673Saw148015 	}
3535673Saw148015 
3545673Saw148015 	if ((buf = (char *)malloc(FILE_ALLOC_BLOCK)) == NULL)
355*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
3565673Saw148015 
357*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&entry->fse_fileset->fs_pick_lock);
358*7556SAndrew.W.Wilson@sun.com 	entry->fse_flags |= FSE_EXISTS;
3596701Saw148015 	entry->fse_fileset->fs_num_act_files++;
360*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&entry->fse_fileset->fs_pick_lock);
3616701Saw148015 
3625673Saw148015 	for (seek = 0; seek < entry->fse_size; ) {
3635673Saw148015 		off64_t wsize;
3645673Saw148015 		int ret = 0;
3655673Saw148015 
3665673Saw148015 		/*
3675673Saw148015 		 * Write FILE_ALLOC_BLOCK's worth,
3685673Saw148015 		 * except on last write
3695673Saw148015 		 */
3705673Saw148015 		wsize = MIN(entry->fse_size - seek, FILE_ALLOC_BLOCK);
3715673Saw148015 
3725673Saw148015 		ret = write(fd, buf, wsize);
3735673Saw148015 		if (ret != wsize) {
3745673Saw148015 			filebench_log(LOG_ERROR,
3755673Saw148015 			    "Failed to pre-allocate file %s: %s",
3765673Saw148015 			    path, strerror(errno));
3775673Saw148015 			(void) close(fd);
3785673Saw148015 			free(buf);
379*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
3805673Saw148015 		}
3815673Saw148015 		seek += wsize;
3825673Saw148015 	}
3835673Saw148015 
3846212Saw148015 	if (!avd_get_bool(entry->fse_fileset->fs_cached))
3855673Saw148015 		(void) fileset_freemem(fd, entry->fse_size);
3865673Saw148015 
3875673Saw148015 	(void) close(fd);
3885673Saw148015 
3895673Saw148015 	free(buf);
3905673Saw148015 
3915673Saw148015 	filebench_log(LOG_DEBUG_IMPL,
3926286Saw148015 	    "Pre-allocated file %s size %llu",
3936286Saw148015 	    path, (u_longlong_t)entry->fse_size);
3945673Saw148015 
395*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
3965673Saw148015 }
3975673Saw148015 
3985673Saw148015 /*
3995673Saw148015  * given a fileset entry, determines if the associated file
4005673Saw148015  * needs to be allocated or not, and if so does the allocation.
401*7556SAndrew.W.Wilson@sun.com  * Sets shm_fsparalloc_count to -1 on error.
4025673Saw148015  */
4035673Saw148015 static void *
4045673Saw148015 fileset_alloc_thread(filesetentry_t *entry)
4055673Saw148015 {
406*7556SAndrew.W.Wilson@sun.com 	if (fileset_alloc_file(entry) == FILEBENCH_ERROR) {
407*7556SAndrew.W.Wilson@sun.com 		(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
408*7556SAndrew.W.Wilson@sun.com 		filebench_shm->shm_fsparalloc_count = -1;
4095673Saw148015 	} else {
410*7556SAndrew.W.Wilson@sun.com 		(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
411*7556SAndrew.W.Wilson@sun.com 		filebench_shm->shm_fsparalloc_count--;
4125673Saw148015 	}
4135673Saw148015 
414*7556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_signal(&filebench_shm->shm_fsparalloc_cv);
415*7556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_unlock(&filebench_shm->shm_fsparalloc_lock);
4165673Saw148015 
4175673Saw148015 	pthread_exit(NULL);
4185673Saw148015 	return (NULL);
4195673Saw148015 }
4205673Saw148015 
4215184Sek110237 
4225184Sek110237 /*
4235184Sek110237  * First creates the parent directories of the file using
4245184Sek110237  * fileset_mkdir(). Then Optionally sets the O_DSYNC flag
4255184Sek110237  * and opens the file with open64(). It unlocks the fileset
4265184Sek110237  * entry lock, sets the DIRECTIO_ON or DIRECTIO_OFF flags
4275184Sek110237  * as requested, and returns the file descriptor integer
4285184Sek110237  * for the opened file.
4295184Sek110237  */
4305184Sek110237 int
4315184Sek110237 fileset_openfile(fileset_t *fileset,
4325184Sek110237     filesetentry_t *entry, int flag, int mode, int attrs)
4335184Sek110237 {
4345184Sek110237 	char path[MAXPATHLEN];
4355184Sek110237 	char dir[MAXPATHLEN];
4365184Sek110237 	char *pathtmp;
4375184Sek110237 	struct stat64 sb;
4385184Sek110237 	int fd;
4395184Sek110237 	int open_attrs = 0;
4405184Sek110237 
4415184Sek110237 	*path = 0;
4426212Saw148015 	(void) strcpy(path, avd_get_str(fileset->fs_path));
4435184Sek110237 	(void) strcat(path, "/");
4446212Saw148015 	(void) strcat(path, avd_get_str(fileset->fs_name));
4455184Sek110237 	pathtmp = fileset_resolvepath(entry);
4465184Sek110237 	(void) strcat(path, pathtmp);
4475184Sek110237 	(void) strcpy(dir, path);
4485184Sek110237 	free(pathtmp);
4495184Sek110237 	(void) trunc_dirname(dir);
4505184Sek110237 
4515184Sek110237 	/* If we are going to create a file, create the parent dirs */
4525184Sek110237 	if ((flag & O_CREAT) && (stat64(dir, &sb) != 0)) {
453*7556SAndrew.W.Wilson@sun.com 		if (fileset_mkdir(dir, 0755) == FILEBENCH_ERROR)
454*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
4556701Saw148015 	}
4566701Saw148015 
4575184Sek110237 	if (attrs & FLOW_ATTR_DSYNC) {
4585184Sek110237 #ifdef sun
4595184Sek110237 		open_attrs |= O_DSYNC;
4605184Sek110237 #else
4615184Sek110237 		open_attrs |= O_FSYNC;
4625184Sek110237 #endif
4635184Sek110237 	}
4645184Sek110237 
4655184Sek110237 	if ((fd = open64(path, flag | open_attrs, mode)) < 0) {
4665184Sek110237 		filebench_log(LOG_ERROR,
4675184Sek110237 		    "Failed to open file %s: %s",
4685184Sek110237 		    path, strerror(errno));
469*7556SAndrew.W.Wilson@sun.com 
470*7556SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, FALSE, FALSE);
471*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
4725184Sek110237 	}
473*7556SAndrew.W.Wilson@sun.com 
474*7556SAndrew.W.Wilson@sun.com 	if (flag & O_CREAT)
475*7556SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, TRUE, TRUE);
476*7556SAndrew.W.Wilson@sun.com 	else
477*7556SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, FALSE, FALSE);
4785184Sek110237 
4795184Sek110237 #ifdef sun
4805184Sek110237 	if (attrs & FLOW_ATTR_DIRECTIO)
4815184Sek110237 		(void) directio(fd, DIRECTIO_ON);
4825184Sek110237 	else
4835184Sek110237 		(void) directio(fd, DIRECTIO_OFF);
4845184Sek110237 #endif
4855184Sek110237 
4865184Sek110237 	return (fd);
4875184Sek110237 }
4885184Sek110237 
4895184Sek110237 
4905184Sek110237 /*
4915184Sek110237  * Selects a fileset entry from a fileset. If the
4925184Sek110237  * FILESET_PICKDIR flag is set it will pick a directory
4935184Sek110237  * entry, otherwise a file entry. The FILESET_PICKRESET
4945184Sek110237  * flag will cause it to reset the free list to the
4955184Sek110237  * overall list (file or directory). The FILESET_PICKUNIQUE
4965184Sek110237  * flag will take an entry off of one of the free (unused)
4975184Sek110237  * lists (file or directory), otherwise the entry will be
4985184Sek110237  * picked off of one of the rotor lists (file or directory).
4995184Sek110237  * The FILESET_PICKEXISTS will insure that only extant
5005184Sek110237  * (FSE_EXISTS) state files are selected, while
5015184Sek110237  * FILESET_PICKNOEXIST insures that only non extant
5025184Sek110237  * (not FSE_EXISTS) state files are selected.
5036391Saw148015  * Note that the selected fileset entry (file) is returned
504*7556SAndrew.W.Wilson@sun.com  * with its FSE_BUSY flag (in fse_flags) set.
5055184Sek110237  */
5065184Sek110237 filesetentry_t *
5075184Sek110237 fileset_pick(fileset_t *fileset, int flags, int tid)
5085184Sek110237 {
5095184Sek110237 	filesetentry_t *entry = NULL;
5105184Sek110237 	filesetentry_t *first = NULL;
5115184Sek110237 
512*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
513*7556SAndrew.W.Wilson@sun.com 
514*7556SAndrew.W.Wilson@sun.com 	/* see if we have to wait for available files or directories */
515*7556SAndrew.W.Wilson@sun.com 	if (flags & FILESET_PICKDIR) {
516*7556SAndrew.W.Wilson@sun.com 		while (fileset->fs_idle_dirs == 0) {
517*7556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_idle_dirs_cv,
518*7556SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
519*7556SAndrew.W.Wilson@sun.com 		}
520*7556SAndrew.W.Wilson@sun.com 	} else {
521*7556SAndrew.W.Wilson@sun.com 		while (fileset->fs_idle_files == 0) {
522*7556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_idle_files_cv,
523*7556SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
524*7556SAndrew.W.Wilson@sun.com 		}
525*7556SAndrew.W.Wilson@sun.com 	}
5265184Sek110237 
5276701Saw148015 	/* see if asking for impossible */
5286701Saw148015 	if (flags & FILESET_PICKEXISTS) {
5296701Saw148015 		if (fileset->fs_num_act_files == 0) {
530*7556SAndrew.W.Wilson@sun.com 			(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
5316701Saw148015 			return (NULL);
5326701Saw148015 		}
5336701Saw148015 	} else if (flags & FILESET_PICKNOEXIST) {
5346701Saw148015 		if (fileset->fs_num_act_files == fileset->fs_realfiles) {
535*7556SAndrew.W.Wilson@sun.com 			(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
5366701Saw148015 			return (NULL);
5376701Saw148015 		}
5386701Saw148015 	}
5396701Saw148015 
5405184Sek110237 	while (entry == NULL) {
5415184Sek110237 
5425184Sek110237 		if ((flags & FILESET_PICKDIR) && (flags & FILESET_PICKRESET)) {
5435184Sek110237 			entry = fileset->fs_dirlist;
5445184Sek110237 			while (entry) {
5455184Sek110237 				entry->fse_flags |= FSE_FREE;
5465184Sek110237 				entry = entry->fse_dirnext;
5475184Sek110237 			}
5485184Sek110237 			fileset->fs_dirfree = fileset->fs_dirlist;
5495184Sek110237 		}
5505184Sek110237 
5515184Sek110237 		if (!(flags & FILESET_PICKDIR) && (flags & FILESET_PICKRESET)) {
5525184Sek110237 			entry = fileset->fs_filelist;
5535184Sek110237 			while (entry) {
5545184Sek110237 				entry->fse_flags |= FSE_FREE;
5555184Sek110237 				entry = entry->fse_filenext;
5565184Sek110237 			}
5575184Sek110237 			fileset->fs_filefree = fileset->fs_filelist;
5585184Sek110237 		}
5595184Sek110237 
5605184Sek110237 		if (flags & FILESET_PICKUNIQUE) {
5615184Sek110237 			if (flags & FILESET_PICKDIR) {
5625184Sek110237 				entry = fileset->fs_dirfree;
5635184Sek110237 				if (entry == NULL)
5645184Sek110237 					goto empty;
5655184Sek110237 				fileset->fs_dirfree = entry->fse_dirnext;
5665184Sek110237 			} else {
5675184Sek110237 				entry = fileset->fs_filefree;
5685184Sek110237 				if (entry == NULL)
5695184Sek110237 					goto empty;
5705184Sek110237 				fileset->fs_filefree = entry->fse_filenext;
5715184Sek110237 			}
5725184Sek110237 			entry->fse_flags &= ~FSE_FREE;
5735184Sek110237 		} else {
5745184Sek110237 			if (flags & FILESET_PICKDIR) {
5755184Sek110237 				entry = fileset->fs_dirrotor;
5765184Sek110237 				if (entry == NULL)
5775184Sek110237 				fileset->fs_dirrotor =
5785184Sek110237 				    entry = fileset->fs_dirlist;
5795184Sek110237 				fileset->fs_dirrotor = entry->fse_dirnext;
5805184Sek110237 			} else {
581*7556SAndrew.W.Wilson@sun.com 				if (flags & FILESET_PICKNOEXIST) {
582*7556SAndrew.W.Wilson@sun.com 					entry = fileset->fs_file_ne_rotor;
583*7556SAndrew.W.Wilson@sun.com 					if (entry == NULL)
584*7556SAndrew.W.Wilson@sun.com 						fileset->fs_file_ne_rotor =
585*7556SAndrew.W.Wilson@sun.com 						    entry =
586*7556SAndrew.W.Wilson@sun.com 						    fileset->fs_filelist;
587*7556SAndrew.W.Wilson@sun.com 					fileset->fs_file_ne_rotor =
588*7556SAndrew.W.Wilson@sun.com 					    entry->fse_filenext;
589*7556SAndrew.W.Wilson@sun.com 				} else {
590*7556SAndrew.W.Wilson@sun.com 					entry = fileset->fs_filerotor[tid];
591*7556SAndrew.W.Wilson@sun.com 					if (entry == NULL)
592*7556SAndrew.W.Wilson@sun.com 						fileset->fs_filerotor[tid] =
593*7556SAndrew.W.Wilson@sun.com 						    entry =
594*7556SAndrew.W.Wilson@sun.com 						    fileset->fs_filelist;
5955184Sek110237 					fileset->fs_filerotor[tid] =
596*7556SAndrew.W.Wilson@sun.com 					    entry->fse_filenext;
597*7556SAndrew.W.Wilson@sun.com 				}
5985184Sek110237 			}
5995184Sek110237 		}
6005184Sek110237 
6015184Sek110237 		if (first == entry)
6025184Sek110237 			goto empty;
6035184Sek110237 
6045184Sek110237 		if (first == NULL)
6055184Sek110237 			first = entry;
6065184Sek110237 
607*7556SAndrew.W.Wilson@sun.com 		/* see if entry in use */
608*7556SAndrew.W.Wilson@sun.com 		if (entry->fse_flags & FSE_BUSY) {
609*7556SAndrew.W.Wilson@sun.com 
610*7556SAndrew.W.Wilson@sun.com 			/* it is, so try next */
611*7556SAndrew.W.Wilson@sun.com 			entry = NULL;
612*7556SAndrew.W.Wilson@sun.com 			continue;
613*7556SAndrew.W.Wilson@sun.com 		}
6145184Sek110237 
6155184Sek110237 		/* If we ask for an existing file, go round again */
6165184Sek110237 		if ((flags & FILESET_PICKEXISTS) &&
617*7556SAndrew.W.Wilson@sun.com 		    !(entry->fse_flags & FSE_EXISTS))
6185184Sek110237 			entry = NULL;
6195184Sek110237 
6205184Sek110237 		/* If we ask for not an existing file, go round again */
6215184Sek110237 		if ((flags & FILESET_PICKNOEXIST) &&
622*7556SAndrew.W.Wilson@sun.com 		    (entry->fse_flags & FSE_EXISTS))
6235184Sek110237 			entry = NULL;
6245184Sek110237 	}
6255184Sek110237 
626*7556SAndrew.W.Wilson@sun.com 	/* update file or directory idle counts */
627*7556SAndrew.W.Wilson@sun.com 	if (flags & FILESET_PICKDIR)
628*7556SAndrew.W.Wilson@sun.com 		fileset->fs_idle_dirs--;
629*7556SAndrew.W.Wilson@sun.com 	else
630*7556SAndrew.W.Wilson@sun.com 		fileset->fs_idle_files--;
631*7556SAndrew.W.Wilson@sun.com 
632*7556SAndrew.W.Wilson@sun.com 	/* Indicate that file or directory is now busy */
633*7556SAndrew.W.Wilson@sun.com 	entry->fse_flags |= FSE_BUSY;
634*7556SAndrew.W.Wilson@sun.com 
635*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
6365184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "Picked file %s", entry->fse_path);
6375184Sek110237 	return (entry);
6385184Sek110237 
6395184Sek110237 empty:
640*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
6415184Sek110237 	return (NULL);
6425184Sek110237 }
6435184Sek110237 
6445184Sek110237 /*
645*7556SAndrew.W.Wilson@sun.com  * Removes a filesetentry from the "FSE_BUSY" state, signaling any threads
646*7556SAndrew.W.Wilson@sun.com  * that are waiting for a NOT BUSY filesetentry. Also sets whether it is
647*7556SAndrew.W.Wilson@sun.com  * existant or not, or leaves that designation alone.
648*7556SAndrew.W.Wilson@sun.com  */
649*7556SAndrew.W.Wilson@sun.com void
650*7556SAndrew.W.Wilson@sun.com fileset_unbusy(filesetentry_t *entry, int update_exist, int new_exist_val)
651*7556SAndrew.W.Wilson@sun.com {
652*7556SAndrew.W.Wilson@sun.com 	fileset_t *fileset = NULL;
653*7556SAndrew.W.Wilson@sun.com 	int fse_is_dir;
654*7556SAndrew.W.Wilson@sun.com 
655*7556SAndrew.W.Wilson@sun.com 	if (entry)
656*7556SAndrew.W.Wilson@sun.com 		fileset = entry->fse_fileset;
657*7556SAndrew.W.Wilson@sun.com 
658*7556SAndrew.W.Wilson@sun.com 	if (fileset == NULL) {
659*7556SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "fileset_unbusy: NO FILESET!");
660*7556SAndrew.W.Wilson@sun.com 		return;
661*7556SAndrew.W.Wilson@sun.com 	}
662*7556SAndrew.W.Wilson@sun.com 
663*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
664*7556SAndrew.W.Wilson@sun.com 	fse_is_dir = entry->fse_flags & FSE_DIR;
665*7556SAndrew.W.Wilson@sun.com 
666*7556SAndrew.W.Wilson@sun.com 	/* increment idle count, clear FSE_BUSY and signal IF it was busy */
667*7556SAndrew.W.Wilson@sun.com 	if (entry->fse_flags & FSE_BUSY) {
668*7556SAndrew.W.Wilson@sun.com 
669*7556SAndrew.W.Wilson@sun.com 		/* unbusy it */
670*7556SAndrew.W.Wilson@sun.com 		entry->fse_flags &= (~FSE_BUSY);
671*7556SAndrew.W.Wilson@sun.com 
672*7556SAndrew.W.Wilson@sun.com 		/* release any threads waiting for unbusy */
673*7556SAndrew.W.Wilson@sun.com 		if (entry->fse_flags & FSE_THRD_WAITNG) {
674*7556SAndrew.W.Wilson@sun.com 			entry->fse_flags &= (~FSE_THRD_WAITNG);
675*7556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_broadcast(
676*7556SAndrew.W.Wilson@sun.com 			    &fileset->fs_thrd_wait_cv);
677*7556SAndrew.W.Wilson@sun.com 		}
678*7556SAndrew.W.Wilson@sun.com 
679*7556SAndrew.W.Wilson@sun.com 		/* increment idle count and signal waiting threads */
680*7556SAndrew.W.Wilson@sun.com 		if (fse_is_dir) {
681*7556SAndrew.W.Wilson@sun.com 			fileset->fs_idle_dirs++;
682*7556SAndrew.W.Wilson@sun.com 			if (fileset->fs_idle_dirs == 1) {
683*7556SAndrew.W.Wilson@sun.com 				(void) pthread_cond_signal(
684*7556SAndrew.W.Wilson@sun.com 				    &fileset->fs_idle_dirs_cv);
685*7556SAndrew.W.Wilson@sun.com 			}
686*7556SAndrew.W.Wilson@sun.com 		} else {
687*7556SAndrew.W.Wilson@sun.com 			fileset->fs_idle_files++;
688*7556SAndrew.W.Wilson@sun.com 			if (fileset->fs_idle_files == 1) {
689*7556SAndrew.W.Wilson@sun.com 				(void) pthread_cond_signal(
690*7556SAndrew.W.Wilson@sun.com 				    &fileset->fs_idle_files_cv);
691*7556SAndrew.W.Wilson@sun.com 			}
692*7556SAndrew.W.Wilson@sun.com 		}
693*7556SAndrew.W.Wilson@sun.com 	}
694*7556SAndrew.W.Wilson@sun.com 
695*7556SAndrew.W.Wilson@sun.com 	/* modify FSE_EXIST flag and actual dirs/files count, if requested */
696*7556SAndrew.W.Wilson@sun.com 	if (update_exist) {
697*7556SAndrew.W.Wilson@sun.com 		if (new_exist_val == TRUE) {
698*7556SAndrew.W.Wilson@sun.com 			if (!(entry->fse_flags & FSE_EXISTS)) {
699*7556SAndrew.W.Wilson@sun.com 
700*7556SAndrew.W.Wilson@sun.com 				/* asked to set, and it was clear */
701*7556SAndrew.W.Wilson@sun.com 				entry->fse_flags |= FSE_EXISTS;
702*7556SAndrew.W.Wilson@sun.com 				if (fse_is_dir)
703*7556SAndrew.W.Wilson@sun.com 					fileset->fs_num_act_dirs++;
704*7556SAndrew.W.Wilson@sun.com 				else
705*7556SAndrew.W.Wilson@sun.com 					fileset->fs_num_act_files++;
706*7556SAndrew.W.Wilson@sun.com 			}
707*7556SAndrew.W.Wilson@sun.com 		} else {
708*7556SAndrew.W.Wilson@sun.com 			if (entry->fse_flags & FSE_EXISTS) {
709*7556SAndrew.W.Wilson@sun.com 
710*7556SAndrew.W.Wilson@sun.com 				/* asked to clear, and it was set */
711*7556SAndrew.W.Wilson@sun.com 				entry->fse_flags &= (~FSE_EXISTS);
712*7556SAndrew.W.Wilson@sun.com 				if (fse_is_dir)
713*7556SAndrew.W.Wilson@sun.com 					fileset->fs_num_act_dirs--;
714*7556SAndrew.W.Wilson@sun.com 				else
715*7556SAndrew.W.Wilson@sun.com 					fileset->fs_num_act_files--;
716*7556SAndrew.W.Wilson@sun.com 			}
717*7556SAndrew.W.Wilson@sun.com 		}
718*7556SAndrew.W.Wilson@sun.com 	}
719*7556SAndrew.W.Wilson@sun.com 
720*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
721*7556SAndrew.W.Wilson@sun.com }
722*7556SAndrew.W.Wilson@sun.com 
723*7556SAndrew.W.Wilson@sun.com /*
7245184Sek110237  * Given a fileset "fileset", create the associated files as
7255184Sek110237  * specified in the attributes of the fileset. The fileset is
7266212Saw148015  * rooted in a directory whose pathname is in fileset_path. If the
7275184Sek110237  * directory exists, meaning that there is already a fileset,
7286212Saw148015  * and the fileset_reuse attribute is false, then remove it and all
7295184Sek110237  * its contained files and subdirectories. Next, the routine
7305184Sek110237  * creates a root directory for the fileset. All the file type
7315184Sek110237  * filesetentries are cycled through creating as needed
7325184Sek110237  * their containing subdirectory trees in the filesystem and
7336212Saw148015  * creating actual files for fileset_preallocpercent of them. The
7345184Sek110237  * created files are filled with fse_size bytes of unitialized
735*7556SAndrew.W.Wilson@sun.com  * data. The routine returns FILEBENCH_ERROR on errors,
736*7556SAndrew.W.Wilson@sun.com  * FILEBENCH_OK on success.
7375184Sek110237  */
7385184Sek110237 static int
7395184Sek110237 fileset_create(fileset_t *fileset)
7405184Sek110237 {
7415184Sek110237 	filesetentry_t *entry;
7425184Sek110237 	char path[MAXPATHLEN];
7435184Sek110237 	struct stat64 sb;
7445184Sek110237 	int pickflags = FILESET_PICKUNIQUE | FILESET_PICKRESET;
7455184Sek110237 	hrtime_t start = gethrtime();
7466212Saw148015 	char *fileset_path;
7476212Saw148015 	char *fileset_name;
7486212Saw148015 	int randno;
7495184Sek110237 	int preallocated = 0;
7505184Sek110237 	int reusing = 0;
7515184Sek110237 
7526212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
7535673Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
7545673Saw148015 		    fileset_entity_name(fileset));
755*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
7565184Sek110237 	}
7575184Sek110237 
7586212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
7596212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
7606212Saw148015 		    fileset_entity_name(fileset));
761*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
7626212Saw148015 	}
7636212Saw148015 
7645673Saw148015 #ifdef HAVE_RAW_SUPPORT
7655673Saw148015 	/* treat raw device as special case */
7665673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
767*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
7685673Saw148015 #endif /* HAVE_RAW_SUPPORT */
7695673Saw148015 
7705184Sek110237 	/* XXX Add check to see if there is enough space */
7715184Sek110237 
7725184Sek110237 	/* Remove existing */
7736212Saw148015 	(void) strcpy(path, fileset_path);
7745184Sek110237 	(void) strcat(path, "/");
7756212Saw148015 	(void) strcat(path, fileset_name);
7765184Sek110237 	if ((stat64(path, &sb) == 0) && (strlen(path) > 3) &&
7776212Saw148015 	    (strlen(avd_get_str(fileset->fs_path)) > 2)) {
7786212Saw148015 		if (!avd_get_bool(fileset->fs_reuse)) {
7795184Sek110237 			char cmd[MAXPATHLEN];
7805184Sek110237 
7815184Sek110237 			(void) snprintf(cmd, sizeof (cmd), "rm -rf %s", path);
7825184Sek110237 			(void) system(cmd);
7835184Sek110237 			filebench_log(LOG_VERBOSE,
7846286Saw148015 			    "Removed any existing %s %s in %llu seconds",
7856212Saw148015 			    fileset_entity_name(fileset), fileset_name,
7866286Saw148015 			    (u_longlong_t)(((gethrtime() - start) /
7876286Saw148015 			    1000000000) + 1));
7885184Sek110237 		} else {
7895184Sek110237 			/* we are re-using */
7905184Sek110237 			reusing = 1;
7916613Sek110237 			filebench_log(LOG_VERBOSE, "Re-using %s %s.",
7926613Sek110237 			    fileset_entity_name(fileset), fileset_name);
7935184Sek110237 		}
7945184Sek110237 	}
7955184Sek110237 	(void) mkdir(path, 0755);
7965184Sek110237 
7975673Saw148015 	/* make the filesets directory tree */
798*7556SAndrew.W.Wilson@sun.com 	if (fileset_create_subdirs(fileset, path) == FILEBENCH_ERROR)
799*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
8005673Saw148015 
8015184Sek110237 	start = gethrtime();
8025184Sek110237 
8035673Saw148015 	filebench_log(LOG_VERBOSE, "Creating %s %s...",
8046212Saw148015 	    fileset_entity_name(fileset), fileset_name);
8055673Saw148015 
8066212Saw148015 	if (!avd_get_bool(fileset->fs_prealloc))
8075673Saw148015 		goto exit;
8085184Sek110237 
8096212Saw148015 	randno = ((RAND_MAX * (100
8106212Saw148015 	    - avd_get_int(fileset->fs_preallocpercent))) / 100);
8116212Saw148015 
8125184Sek110237 	while (entry = fileset_pick(fileset, pickflags, 0)) {
8135673Saw148015 		pthread_t tid;
8145184Sek110237 
8155184Sek110237 		pickflags = FILESET_PICKUNIQUE;
8165184Sek110237 
8175673Saw148015 		/* entry doesn't need to be locked during initialization */
818*7556SAndrew.W.Wilson@sun.com 		fileset_unbusy(entry, FALSE, FALSE);
8195673Saw148015 
8205673Saw148015 		if (rand() < randno)
8215184Sek110237 			continue;
8225184Sek110237 
8235184Sek110237 		preallocated++;
8245184Sek110237 
8255673Saw148015 		if (reusing)
8265673Saw148015 			entry->fse_flags |= FSE_REUSING;
8275673Saw148015 		else
8285673Saw148015 			entry->fse_flags &= (~FSE_REUSING);
8295673Saw148015 
830*7556SAndrew.W.Wilson@sun.com 		/* fire off allocation threads for each file if paralloc set */
8316212Saw148015 		if (avd_get_bool(fileset->fs_paralloc)) {
8325184Sek110237 
833*7556SAndrew.W.Wilson@sun.com 			/* limit total number of simultaneous allocations */
834*7556SAndrew.W.Wilson@sun.com 			(void) pthread_mutex_lock(
835*7556SAndrew.W.Wilson@sun.com 			    &filebench_shm->shm_fsparalloc_lock);
836*7556SAndrew.W.Wilson@sun.com 			while (filebench_shm->shm_fsparalloc_count
837*7556SAndrew.W.Wilson@sun.com 			    >= MAX_PARALLOC_THREADS) {
8385673Saw148015 				(void) pthread_cond_wait(
839*7556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_cv,
840*7556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_lock);
8415673Saw148015 			}
8425673Saw148015 
843*7556SAndrew.W.Wilson@sun.com 			/* quit if any allocation thread reports and error */
844*7556SAndrew.W.Wilson@sun.com 			if (filebench_shm->shm_fsparalloc_count < 0) {
845*7556SAndrew.W.Wilson@sun.com 				(void) pthread_mutex_unlock(
846*7556SAndrew.W.Wilson@sun.com 				    &filebench_shm->shm_fsparalloc_lock);
847*7556SAndrew.W.Wilson@sun.com 				return (FILEBENCH_ERROR);
8485184Sek110237 			}
8495184Sek110237 
850*7556SAndrew.W.Wilson@sun.com 			filebench_shm->shm_fsparalloc_count++;
851*7556SAndrew.W.Wilson@sun.com 			(void) pthread_mutex_unlock(
852*7556SAndrew.W.Wilson@sun.com 			    &filebench_shm->shm_fsparalloc_lock);
8535184Sek110237 
854*7556SAndrew.W.Wilson@sun.com 			/*
855*7556SAndrew.W.Wilson@sun.com 			 * Fire off a detached allocation thread per file.
856*7556SAndrew.W.Wilson@sun.com 			 * The thread will self destruct when it finishes
857*7556SAndrew.W.Wilson@sun.com 			 * writing pre-allocation data to the file.
858*7556SAndrew.W.Wilson@sun.com 			 */
8595673Saw148015 			if (pthread_create(&tid, NULL,
8605673Saw148015 			    (void *(*)(void*))fileset_alloc_thread,
861*7556SAndrew.W.Wilson@sun.com 			    entry) == 0) {
862*7556SAndrew.W.Wilson@sun.com 				/*
863*7556SAndrew.W.Wilson@sun.com 				 * A thread was created; detach it so it can
864*7556SAndrew.W.Wilson@sun.com 				 * fully quit when finished.
865*7556SAndrew.W.Wilson@sun.com 				 */
866*7556SAndrew.W.Wilson@sun.com 				(void) pthread_detach(tid);
867*7556SAndrew.W.Wilson@sun.com 			} else {
8685184Sek110237 				filebench_log(LOG_ERROR,
8695673Saw148015 				    "File prealloc thread create failed");
8705673Saw148015 				filebench_shutdown(1);
8715184Sek110237 			}
8725184Sek110237 
8735673Saw148015 		} else {
874*7556SAndrew.W.Wilson@sun.com 			if (fileset_alloc_file(entry) == FILEBENCH_ERROR)
875*7556SAndrew.W.Wilson@sun.com 				return (FILEBENCH_ERROR);
8765673Saw148015 		}
8775673Saw148015 	}
8785184Sek110237 
8795673Saw148015 exit:
8805184Sek110237 	filebench_log(LOG_VERBOSE,
8816286Saw148015 	    "Preallocated %d of %llu of %s %s in %llu seconds",
8825184Sek110237 	    preallocated,
8836286Saw148015 	    (u_longlong_t)fileset->fs_constentries,
8846212Saw148015 	    fileset_entity_name(fileset), fileset_name,
8856286Saw148015 	    (u_longlong_t)(((gethrtime() - start) / 1000000000) + 1));
8865184Sek110237 
887*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
8885184Sek110237 }
8895184Sek110237 
8905184Sek110237 /*
8915184Sek110237  * Adds an entry to the fileset's file list. Single threaded so
8925184Sek110237  * no locking needed.
8935184Sek110237  */
8945184Sek110237 static void
8955184Sek110237 fileset_insfilelist(fileset_t *fileset, filesetentry_t *entry)
8965184Sek110237 {
8975184Sek110237 	if (fileset->fs_filelist == NULL) {
8985184Sek110237 		fileset->fs_filelist = entry;
8995184Sek110237 		entry->fse_filenext = NULL;
9005184Sek110237 	} else {
9015184Sek110237 		entry->fse_filenext = fileset->fs_filelist;
9025184Sek110237 		fileset->fs_filelist = entry;
9035184Sek110237 	}
9045184Sek110237 }
9055184Sek110237 
9065184Sek110237 /*
9075184Sek110237  * Adds an entry to the fileset's directory list. Single
9085184Sek110237  * threaded so no locking needed.
9095184Sek110237  */
9105184Sek110237 static void
9115184Sek110237 fileset_insdirlist(fileset_t *fileset, filesetentry_t *entry)
9125184Sek110237 {
9135184Sek110237 	if (fileset->fs_dirlist == NULL) {
9145184Sek110237 		fileset->fs_dirlist = entry;
9155184Sek110237 		entry->fse_dirnext = NULL;
9165184Sek110237 	} else {
9175184Sek110237 		entry->fse_dirnext = fileset->fs_dirlist;
9185184Sek110237 		fileset->fs_dirlist = entry;
9195184Sek110237 	}
9205184Sek110237 }
9215184Sek110237 
9225184Sek110237 /*
9235184Sek110237  * Obtaines a filesetentry entity for a file to be placed in a
9245184Sek110237  * (sub)directory of a fileset. The size of the file may be
9256212Saw148015  * specified by fileset_meansize, or calculated from a gamma
9266212Saw148015  * distribution of parameter fileset_sizegamma and of mean size
9276212Saw148015  * fileset_meansize. The filesetentry entity is placed on the file
9285184Sek110237  * list in the specified parent filesetentry entity, which may
9295184Sek110237  * be a directory filesetentry, or the root filesetentry in the
9305184Sek110237  * fileset. It is also placed on the fileset's list of all
931*7556SAndrew.W.Wilson@sun.com  * contained files. Returns FILEBENCH_OK if successful or FILEBENCH_ERROR
932*7556SAndrew.W.Wilson@sun.com  * if ipc memory for the path string cannot be allocated.
9335184Sek110237  */
9345184Sek110237 static int
9355184Sek110237 fileset_populate_file(fileset_t *fileset, filesetentry_t *parent, int serial)
9365184Sek110237 {
9375184Sek110237 	char tmpname[16];
9385184Sek110237 	filesetentry_t *entry;
9395184Sek110237 	double drand;
9405184Sek110237 
9415184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
9425184Sek110237 	    == NULL) {
9435184Sek110237 		filebench_log(LOG_ERROR,
9445184Sek110237 		    "fileset_populate_file: Can't malloc filesetentry");
945*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
9465184Sek110237 	}
9475184Sek110237 
948*7556SAndrew.W.Wilson@sun.com 	/* Another currently idle file */
949*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
950*7556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_files++;
951*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
952*7556SAndrew.W.Wilson@sun.com 
9535184Sek110237 	entry->fse_parent = parent;
9545184Sek110237 	entry->fse_fileset = fileset;
955*7556SAndrew.W.Wilson@sun.com 	entry->fse_flags = FSE_FREE;
9565184Sek110237 	fileset_insfilelist(fileset, entry);
9575184Sek110237 
9585184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
9595184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
9605184Sek110237 		filebench_log(LOG_ERROR,
9615184Sek110237 		    "fileset_populate_file: Can't alloc path string");
962*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
9635184Sek110237 	}
9645184Sek110237 
9656212Saw148015 	/* see if random variable was supplied for file size */
9666212Saw148015 	if (fileset->fs_meansize == -1) {
9676212Saw148015 		entry->fse_size = (off64_t)avd_get_int(fileset->fs_size);
9686212Saw148015 	} else {
9696212Saw148015 		double gamma;
9705184Sek110237 
9716212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
9726212Saw148015 		if (gamma > 0) {
9736212Saw148015 			drand = gamma_dist_knuth(gamma,
9746212Saw148015 			    fileset->fs_meansize / gamma);
9756212Saw148015 			entry->fse_size = (off64_t)drand;
9766212Saw148015 		} else {
9776212Saw148015 			entry->fse_size = (off64_t)fileset->fs_meansize;
9786212Saw148015 		}
9795184Sek110237 	}
9805184Sek110237 
9815184Sek110237 	fileset->fs_bytes += entry->fse_size;
9825184Sek110237 
9835184Sek110237 	fileset->fs_realfiles++;
984*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
9855184Sek110237 }
9865184Sek110237 
9875184Sek110237 /*
9885184Sek110237  * Creates a directory node in a fileset, by obtaining a
9895184Sek110237  * filesetentry entity for the node and initializing it
9905184Sek110237  * according to parameters of the fileset. It determines a
9915184Sek110237  * directory tree depth and directory width, optionally using
9925184Sek110237  * a gamma distribution. If its calculated depth is less then
9935184Sek110237  * its actual depth in the directory tree, it becomes a leaf
9945184Sek110237  * node and files itself with "width" number of file type
9955184Sek110237  * filesetentries, otherwise it files itself with "width"
9965184Sek110237  * number of directory type filesetentries, using recursive
9975184Sek110237  * calls to fileset_populate_subdir. The end result of the
9985184Sek110237  * initial call to this routine is a tree of directories of
9995184Sek110237  * random width and varying depth with sufficient leaf
10005184Sek110237  * directories to contain all required files.
1001*7556SAndrew.W.Wilson@sun.com  * Returns FILEBENCH_OK on success. Returns FILEBENCH_ERROR if ipc path
1002*7556SAndrew.W.Wilson@sun.com  * string memory cannot be allocated and returns the error code (currently
1003*7556SAndrew.W.Wilson@sun.com  * also FILEBENCH_ERROR) from calls to fileset_populate_file or recursive
10045184Sek110237  * calls to fileset_populate_subdir.
10055184Sek110237  */
10065184Sek110237 static int
10075184Sek110237 fileset_populate_subdir(fileset_t *fileset, filesetentry_t *parent,
10085184Sek110237     int serial, double depth)
10095184Sek110237 {
10106212Saw148015 	double randepth, drand, ranwidth;
10115184Sek110237 	int isleaf = 0;
10125184Sek110237 	char tmpname[16];
10135184Sek110237 	filesetentry_t *entry;
10145184Sek110237 	int i;
10155184Sek110237 
10165184Sek110237 	depth += 1;
10175184Sek110237 
10185184Sek110237 	/* Create dir node */
10195184Sek110237 	if ((entry = (filesetentry_t *)ipc_malloc(FILEBENCH_FILESETENTRY))
10205184Sek110237 	    == NULL) {
10215184Sek110237 		filebench_log(LOG_ERROR,
10225184Sek110237 		    "fileset_populate_subdir: Can't malloc filesetentry");
1023*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
10245184Sek110237 	}
10255184Sek110237 
1026*7556SAndrew.W.Wilson@sun.com 	/* another idle directory */
1027*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_lock(&fileset->fs_pick_lock);
1028*7556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_dirs++;
1029*7556SAndrew.W.Wilson@sun.com 	(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
10305184Sek110237 
10315184Sek110237 	(void) snprintf(tmpname, sizeof (tmpname), "%08d", serial);
10325184Sek110237 	if ((entry->fse_path = (char *)ipc_pathalloc(tmpname)) == NULL) {
10335184Sek110237 		filebench_log(LOG_ERROR,
10345184Sek110237 		    "fileset_populate_subdir: Can't alloc path string");
1035*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
10365184Sek110237 	}
10375184Sek110237 
10385184Sek110237 	entry->fse_parent = parent;
1039*7556SAndrew.W.Wilson@sun.com 	entry->fse_flags = FSE_DIR | FSE_FREE;
10405184Sek110237 	fileset_insdirlist(fileset, entry);
10415184Sek110237 
10426212Saw148015 	if (fileset->fs_dirdepthrv) {
10436212Saw148015 		randepth = (int)avd_get_int(fileset->fs_dirdepthrv);
10445184Sek110237 	} else {
10456212Saw148015 		double gamma;
10466212Saw148015 
10476212Saw148015 		gamma = avd_get_int(fileset->fs_dirgamma) / 1000.0;
10486212Saw148015 		if (gamma > 0) {
10496212Saw148015 			drand = gamma_dist_knuth(gamma,
10506212Saw148015 			    fileset->fs_meandepth / gamma);
10516212Saw148015 			randepth = (int)drand;
10526212Saw148015 		} else {
10536212Saw148015 			randepth = (int)fileset->fs_meandepth;
10546212Saw148015 		}
10555184Sek110237 	}
10565184Sek110237 
10576212Saw148015 	if (fileset->fs_meanwidth == -1) {
10586212Saw148015 		ranwidth = avd_get_dbl(fileset->fs_dirwidth);
10596212Saw148015 	} else {
10606212Saw148015 		double gamma;
10615184Sek110237 
10626212Saw148015 		gamma = avd_get_int(fileset->fs_sizegamma) / 1000.0;
10636212Saw148015 		if (gamma > 0) {
10646212Saw148015 			drand = gamma_dist_knuth(gamma,
10656212Saw148015 			    fileset->fs_meanwidth / gamma);
10666212Saw148015 			ranwidth = drand;
10676212Saw148015 		} else {
10686212Saw148015 			ranwidth = fileset->fs_meanwidth;
10696212Saw148015 		}
10705184Sek110237 	}
10715184Sek110237 
10725184Sek110237 	if (randepth == 0)
10735184Sek110237 		randepth = 1;
10745184Sek110237 	if (ranwidth == 0)
10755184Sek110237 		ranwidth = 1;
10765184Sek110237 	if (depth >= randepth)
10775184Sek110237 		isleaf = 1;
10785184Sek110237 
10795184Sek110237 	/*
10805184Sek110237 	 * Create directory of random width according to distribution, or
10815184Sek110237 	 * if root directory, continue until #files required
10825184Sek110237 	 */
10836212Saw148015 	for (i = 1; ((parent == NULL) || (i < ranwidth + 1)) &&
10846212Saw148015 	    (fileset->fs_realfiles < fileset->fs_constentries);
10856212Saw148015 	    i++) {
10865184Sek110237 		int ret = 0;
10875184Sek110237 
10885184Sek110237 		if (parent && isleaf)
10895184Sek110237 			ret = fileset_populate_file(fileset, entry, i);
10905184Sek110237 		else
10915184Sek110237 			ret = fileset_populate_subdir(fileset, entry, i, depth);
10925184Sek110237 
10935184Sek110237 		if (ret != 0)
10945184Sek110237 			return (ret);
10955184Sek110237 	}
1096*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
10975184Sek110237 }
10985184Sek110237 
10995184Sek110237 /*
11005184Sek110237  * Populates a fileset with files and subdirectory entries. Uses
11016212Saw148015  * the supplied fileset_dirwidth and fileset_entries (number of files) to
11026212Saw148015  * calculate the required fileset_meandepth (of subdirectories) and
11036212Saw148015  * initialize the fileset_meanwidth and fileset_meansize variables. Then
11045184Sek110237  * calls fileset_populate_subdir() to do the recursive
11055184Sek110237  * subdirectory entry creation and leaf file entry creation. All
11065184Sek110237  * of the above is skipped if the fileset has already been
11075184Sek110237  * populated. Returns 0 on success, or an error code from the
11085184Sek110237  * call to fileset_populate_subdir if that call fails.
11095184Sek110237  */
11105184Sek110237 static int
11115184Sek110237 fileset_populate(fileset_t *fileset)
11125184Sek110237 {
11136212Saw148015 	int entries = (int)avd_get_int(fileset->fs_entries);
11146212Saw148015 	int meandirwidth;
11155184Sek110237 	int ret;
11165184Sek110237 
11175184Sek110237 	/* Skip if already populated */
11185184Sek110237 	if (fileset->fs_bytes > 0)
11195184Sek110237 		goto exists;
11205184Sek110237 
11215673Saw148015 #ifdef HAVE_RAW_SUPPORT
11225673Saw148015 	/* check for raw device */
11235673Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV)
1124*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
11255673Saw148015 #endif /* HAVE_RAW_SUPPORT */
11265673Saw148015 
11276212Saw148015 	/* save value of entries obtained for later, in case it was random */
11286212Saw148015 	fileset->fs_constentries = entries;
11296212Saw148015 
1130*7556SAndrew.W.Wilson@sun.com 	/* declare all files currently non existant */
1131*7556SAndrew.W.Wilson@sun.com 	fileset->fs_num_act_files = 0;
1132*7556SAndrew.W.Wilson@sun.com 
1133*7556SAndrew.W.Wilson@sun.com 	/* initialize idle files and directories condition variables */
1134*7556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_idle_dirs_cv, ipc_condattr());
1135*7556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_idle_files_cv, ipc_condattr());
1136*7556SAndrew.W.Wilson@sun.com 
1137*7556SAndrew.W.Wilson@sun.com 	/* no files or dirs idle (or busy) yet */
1138*7556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_files = 0;
1139*7556SAndrew.W.Wilson@sun.com 	fileset->fs_idle_dirs = 0;
1140*7556SAndrew.W.Wilson@sun.com 
1141*7556SAndrew.W.Wilson@sun.com 	/* initialize locks and other condition variables */
1142*7556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_init(&fileset->fs_pick_lock,
1143*7556SAndrew.W.Wilson@sun.com 	    ipc_mutexattr(IPC_MUTEX_NORMAL));
1144*7556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(&fileset->fs_thrd_wait_cv, ipc_condattr());
1145*7556SAndrew.W.Wilson@sun.com 
11466212Saw148015 	/* is dirwidth a random variable? */
11476212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_dirwidth)) {
11486212Saw148015 		meandirwidth =
11496212Saw148015 		    (int)fileset->fs_dirwidth->avd_val.randptr->rnd_dbl_mean;
11506212Saw148015 		fileset->fs_meanwidth = -1;
11516212Saw148015 	} else {
11526212Saw148015 		meandirwidth = (int)avd_get_int(fileset->fs_dirwidth);
11536212Saw148015 		fileset->fs_meanwidth = (double)meandirwidth;
11546212Saw148015 	}
11556212Saw148015 
11565184Sek110237 	/*
11575184Sek110237 	 * Input params are:
11585184Sek110237 	 *	# of files
11595184Sek110237 	 *	ave # of files per dir
11605184Sek110237 	 *	max size of dir
11615184Sek110237 	 *	# ave size of file
11625184Sek110237 	 *	max size of file
11635184Sek110237 	 */
11646212Saw148015 	fileset->fs_meandepth = log(entries) / log(meandirwidth);
11656212Saw148015 
11666212Saw148015 	/* Has a random variable been supplied for dirdepth? */
11676212Saw148015 	if (fileset->fs_dirdepthrv) {
11686212Saw148015 		/* yes, so set the random variable's mean value to meandepth */
11696212Saw148015 		fileset->fs_dirdepthrv->avd_val.randptr->rnd_dbl_mean =
11706212Saw148015 		    fileset->fs_meandepth;
11716212Saw148015 	}
11726212Saw148015 
11736212Saw148015 	/* test for random size variable */
11746212Saw148015 	if (AVD_IS_RANDOM(fileset->fs_size))
11756212Saw148015 		fileset->fs_meansize = -1;
11766212Saw148015 	else
11776212Saw148015 		fileset->fs_meansize = avd_get_int(fileset->fs_size);
11785184Sek110237 
11795184Sek110237 	if ((ret = fileset_populate_subdir(fileset, NULL, 1, 0)) != 0)
11805184Sek110237 		return (ret);
11815184Sek110237 
11825184Sek110237 
11835184Sek110237 exists:
11845673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
11856286Saw148015 		filebench_log(LOG_VERBOSE, "File %s: mbytes=%llu",
11866212Saw148015 		    avd_get_str(fileset->fs_name),
11876286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
11885673Saw148015 	} else {
11896286Saw148015 		filebench_log(LOG_VERBOSE, "Fileset %s: %d files, "
11906286Saw148015 		    "avg dir = %d, avg depth = %.1lf, mbytes=%llu",
11916212Saw148015 		    avd_get_str(fileset->fs_name), entries,
11926212Saw148015 		    meandirwidth,
11935673Saw148015 		    fileset->fs_meandepth,
11946286Saw148015 		    (u_longlong_t)(fileset->fs_bytes / 1024UL / 1024UL));
11955673Saw148015 	}
11966701Saw148015 
1197*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
11985184Sek110237 }
11995184Sek110237 
12005184Sek110237 /*
12016212Saw148015  * Allocates a fileset instance, initializes fileset_dirgamma and
12026212Saw148015  * fileset_sizegamma default values, and sets the fileset name to the
12035184Sek110237  * supplied name string. Puts the allocated fileset on the
12045184Sek110237  * master fileset list and returns a pointer to it.
12056701Saw148015  *
12066701Saw148015  * This routine implements the 'define fileset' calls found in a .f
12076701Saw148015  * workload, such as in the following example:
12086701Saw148015  * define fileset name=drew4ever, entries=$nfiles
12095184Sek110237  */
12105184Sek110237 fileset_t *
12116212Saw148015 fileset_define(avd_t name)
12125184Sek110237 {
12135184Sek110237 	fileset_t *fileset;
12145184Sek110237 
12155184Sek110237 	if (name == NULL)
12165184Sek110237 		return (NULL);
12175184Sek110237 
12185184Sek110237 	if ((fileset = (fileset_t *)ipc_malloc(FILEBENCH_FILESET)) == NULL) {
12195184Sek110237 		filebench_log(LOG_ERROR,
12205184Sek110237 		    "fileset_define: Can't malloc fileset");
12215184Sek110237 		return (NULL);
12225184Sek110237 	}
12235184Sek110237 
12246212Saw148015 	filebench_log(LOG_DEBUG_IMPL,
12256212Saw148015 	    "Defining file %s", avd_get_str(name));
12265184Sek110237 
12276391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
12285184Sek110237 
12296212Saw148015 	fileset->fs_dirgamma = avd_int_alloc(1500);
12306212Saw148015 	fileset->fs_sizegamma = avd_int_alloc(1500);
12315184Sek110237 
12325184Sek110237 	/* Add fileset to global list */
12336391Saw148015 	if (filebench_shm->shm_filesetlist == NULL) {
12346391Saw148015 		filebench_shm->shm_filesetlist = fileset;
12355184Sek110237 		fileset->fs_next = NULL;
12365184Sek110237 	} else {
12376391Saw148015 		fileset->fs_next = filebench_shm->shm_filesetlist;
12386391Saw148015 		filebench_shm->shm_filesetlist = fileset;
12395184Sek110237 	}
12405184Sek110237 
12416391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
12425184Sek110237 
12436212Saw148015 	fileset->fs_name = name;
12445184Sek110237 
12455184Sek110237 	return (fileset);
12465184Sek110237 }
12475184Sek110237 
12485184Sek110237 /*
12495184Sek110237  * If supplied with a pointer to a fileset and the fileset's
12506212Saw148015  * fileset_prealloc flag is set, calls fileset_populate() to populate
12515184Sek110237  * the fileset with filesetentries, then calls fileset_create()
12525184Sek110237  * to make actual directories and files for the filesetentries.
12535184Sek110237  * Otherwise, it applies fileset_populate() and fileset_create()
12545184Sek110237  * to all the filesets on the master fileset list. It always
12555184Sek110237  * returns zero (0) if one fileset is populated / created,
12565184Sek110237  * otherwise it returns the sum of returned values from
12575184Sek110237  * fileset_create() and fileset_populate(), which
12585184Sek110237  * will be a negative one (-1) times the number of
12595184Sek110237  * fileset_create() calls which failed.
12605184Sek110237  */
12615184Sek110237 int
12625184Sek110237 fileset_createset(fileset_t *fileset)
12635184Sek110237 {
12645184Sek110237 	fileset_t *list;
12655184Sek110237 	int ret = 0;
12665184Sek110237 
12675673Saw148015 	/* set up for possible parallel allocate */
1268*7556SAndrew.W.Wilson@sun.com 	filebench_shm->shm_fsparalloc_count = 0;
1269*7556SAndrew.W.Wilson@sun.com 	(void) pthread_cond_init(
1270*7556SAndrew.W.Wilson@sun.com 	    &filebench_shm->shm_fsparalloc_cv,
1271*7556SAndrew.W.Wilson@sun.com 	    ipc_condattr());
12725673Saw148015 
12736212Saw148015 	if (fileset && avd_get_bool(fileset->fs_prealloc)) {
12745673Saw148015 
12756305Saw148015 		/* check for raw files */
12766305Saw148015 		if (fileset_checkraw(fileset)) {
12776305Saw148015 			filebench_log(LOG_INFO,
12786305Saw148015 			    "file %s/%s is a RAW device",
12796305Saw148015 			    avd_get_str(fileset->fs_path),
12806305Saw148015 			    avd_get_str(fileset->fs_name));
1281*7556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_OK);
12826305Saw148015 		}
12836305Saw148015 
12845673Saw148015 		filebench_log(LOG_INFO,
12855673Saw148015 		    "creating/pre-allocating %s %s",
12866212Saw148015 		    fileset_entity_name(fileset),
12876212Saw148015 		    avd_get_str(fileset->fs_name));
12885673Saw148015 
1289*7556SAndrew.W.Wilson@sun.com 		if ((ret = fileset_populate(fileset)) != FILEBENCH_OK)
12905184Sek110237 			return (ret);
12915673Saw148015 
1292*7556SAndrew.W.Wilson@sun.com 		if ((ret = fileset_create(fileset)) != FILEBENCH_OK)
12935673Saw148015 			return (ret);
12945673Saw148015 	} else {
12955673Saw148015 
12965673Saw148015 		filebench_log(LOG_INFO,
12975673Saw148015 		    "Creating/pre-allocating files and filesets");
12985673Saw148015 
12996391Saw148015 		list = filebench_shm->shm_filesetlist;
13005673Saw148015 		while (list) {
13016305Saw148015 			/* check for raw files */
13026305Saw148015 			if (fileset_checkraw(list)) {
13036305Saw148015 				filebench_log(LOG_INFO,
13046305Saw148015 				    "file %s/%s is a RAW device",
13056305Saw148015 				    avd_get_str(list->fs_path),
13066305Saw148015 				    avd_get_str(list->fs_name));
13076305Saw148015 				list = list->fs_next;
13086305Saw148015 				continue;
13096305Saw148015 			}
13106305Saw148015 
1311*7556SAndrew.W.Wilson@sun.com 			if ((ret = fileset_populate(list)) != FILEBENCH_OK)
13125673Saw148015 				return (ret);
1313*7556SAndrew.W.Wilson@sun.com 
1314*7556SAndrew.W.Wilson@sun.com 			if ((ret = fileset_create(list)) != FILEBENCH_OK)
13155673Saw148015 				return (ret);
1316*7556SAndrew.W.Wilson@sun.com 
13175673Saw148015 			list = list->fs_next;
13185673Saw148015 		}
13195184Sek110237 	}
13205184Sek110237 
13215673Saw148015 	/* wait for allocation threads to finish */
13225673Saw148015 	filebench_log(LOG_INFO,
13235673Saw148015 	    "waiting for fileset pre-allocation to finish");
13245184Sek110237 
1325*7556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
1326*7556SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_fsparalloc_count > 0)
1327*7556SAndrew.W.Wilson@sun.com 		(void) pthread_cond_wait(
1328*7556SAndrew.W.Wilson@sun.com 		    &filebench_shm->shm_fsparalloc_cv,
1329*7556SAndrew.W.Wilson@sun.com 		    &filebench_shm->shm_fsparalloc_lock);
1330*7556SAndrew.W.Wilson@sun.com 	(void) pthread_mutex_unlock(&filebench_shm->shm_fsparalloc_lock);
13315673Saw148015 
1332*7556SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_fsparalloc_count < 0)
1333*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
13345673Saw148015 
1335*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
13365184Sek110237 }
13375184Sek110237 
13385184Sek110237 /*
13395184Sek110237  * Searches through the master fileset list for the named fileset.
13405184Sek110237  * If found, returns pointer to same, otherwise returns NULL.
13415184Sek110237  */
13425184Sek110237 fileset_t *
13435184Sek110237 fileset_find(char *name)
13445184Sek110237 {
13456391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
13465184Sek110237 
13476391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
13485184Sek110237 
13495184Sek110237 	while (fileset) {
13506212Saw148015 		if (strcmp(name, avd_get_str(fileset->fs_name)) == 0) {
13516391Saw148015 			(void) ipc_mutex_unlock(
13526391Saw148015 			    &filebench_shm->shm_fileset_lock);
13535184Sek110237 			return (fileset);
13545184Sek110237 		}
13555184Sek110237 		fileset = fileset->fs_next;
13565184Sek110237 	}
13576391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
13585184Sek110237 
13595184Sek110237 	return (NULL);
13605184Sek110237 }
13615673Saw148015 
13625673Saw148015 /*
13635673Saw148015  * Iterates over all the file sets in the filesetlist,
13645673Saw148015  * executing the supplied command "*cmd()" on them. Also
13655673Saw148015  * indicates to the executed command if it is the first
13665673Saw148015  * time the command has been executed since the current
13675673Saw148015  * call to fileset_iter.
13685673Saw148015  */
13695673Saw148015 void
13705673Saw148015 fileset_iter(int (*cmd)(fileset_t *fileset, int first))
13715673Saw148015 {
13726391Saw148015 	fileset_t *fileset = filebench_shm->shm_filesetlist;
13735673Saw148015 	int count = 0;
13745673Saw148015 
13756391Saw148015 	(void) ipc_mutex_lock(&filebench_shm->shm_fileset_lock);
13765673Saw148015 
13775673Saw148015 	while (fileset) {
13785673Saw148015 		cmd(fileset, count == 0);
13795673Saw148015 		fileset = fileset->fs_next;
13805673Saw148015 		count++;
13815673Saw148015 	}
13825673Saw148015 
13836391Saw148015 	(void) ipc_mutex_unlock(&filebench_shm->shm_fileset_lock);
13845673Saw148015 }
13855673Saw148015 
13865673Saw148015 /*
13875673Saw148015  * Prints information to the filebench log about the file
13885673Saw148015  * object. Also prints a header on the first call.
13895673Saw148015  */
13905673Saw148015 int
13915673Saw148015 fileset_print(fileset_t *fileset, int first)
13925673Saw148015 {
13936212Saw148015 	int pathlength;
13946212Saw148015 	char *fileset_path;
13956212Saw148015 	char *fileset_name;
13966212Saw148015 	static char pad[] = "                              "; /* 30 spaces */
13976212Saw148015 
13986212Saw148015 	if ((fileset_path = avd_get_str(fileset->fs_path)) == NULL) {
13996212Saw148015 		filebench_log(LOG_ERROR, "%s path not set",
14006212Saw148015 		    fileset_entity_name(fileset));
1401*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
14026212Saw148015 	}
14036212Saw148015 
14046212Saw148015 	if ((fileset_name = avd_get_str(fileset->fs_name)) == NULL) {
14056212Saw148015 		filebench_log(LOG_ERROR, "%s name not set",
14066212Saw148015 		    fileset_entity_name(fileset));
1407*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
14086212Saw148015 	}
14096212Saw148015 
14106212Saw148015 	pathlength = strlen(fileset_path) + strlen(fileset_name);
14115673Saw148015 
14125673Saw148015 	if (pathlength > 29)
14135673Saw148015 		pathlength = 29;
14145673Saw148015 
14155673Saw148015 	if (first) {
14165673Saw148015 		filebench_log(LOG_INFO, "File or Fileset name%20s%12s%10s",
14175673Saw148015 		    "file size",
14185673Saw148015 		    "dir width",
14195673Saw148015 		    "entries");
14205673Saw148015 	}
14215673Saw148015 
14225673Saw148015 	if (fileset->fs_attrs & FILESET_IS_FILE) {
14235673Saw148015 		if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
14245673Saw148015 			filebench_log(LOG_INFO,
14255673Saw148015 			    "%s/%s%s         (Raw Device)",
14266212Saw148015 			    fileset_path, fileset_name, &pad[pathlength]);
14275673Saw148015 		} else {
14285673Saw148015 			filebench_log(LOG_INFO,
14296286Saw148015 			    "%s/%s%s%9llu     (Single File)",
14306212Saw148015 			    fileset_path, fileset_name, &pad[pathlength],
14316286Saw148015 			    (u_longlong_t)avd_get_int(fileset->fs_size));
14325673Saw148015 		}
14335673Saw148015 	} else {
14346286Saw148015 		filebench_log(LOG_INFO, "%s/%s%s%9llu%12llu%10llu",
14356212Saw148015 		    fileset_path, fileset_name,
14365673Saw148015 		    &pad[pathlength],
14376286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_size),
14386286Saw148015 		    (u_longlong_t)avd_get_int(fileset->fs_dirwidth),
14396286Saw148015 		    (u_longlong_t)fileset->fs_constentries);
14405673Saw148015 	}
1441*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
14425673Saw148015 }
14435673Saw148015 /*
14445673Saw148015  * checks to see if the path/name pair points to a raw device. If
14455673Saw148015  * so it sets the raw device flag (FILESET_IS_RAW_DEV) and returns 1.
14465673Saw148015  * If RAW is not defined, or it is not a raw device, it clears the
14475673Saw148015  * raw device flag and returns 0.
14485673Saw148015  */
14495673Saw148015 int
14505673Saw148015 fileset_checkraw(fileset_t *fileset)
14515673Saw148015 {
14525673Saw148015 	char path[MAXPATHLEN];
14535673Saw148015 	struct stat64 sb;
14546305Saw148015 	char *pathname;
14556305Saw148015 	char *setname;
14565673Saw148015 
14575673Saw148015 	fileset->fs_attrs &= (~FILESET_IS_RAW_DEV);
14585673Saw148015 
14595673Saw148015 #ifdef HAVE_RAW_SUPPORT
14605673Saw148015 	/* check for raw device */
14616305Saw148015 	if ((pathname = avd_get_str(fileset->fs_path)) == NULL)
1462*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
14636305Saw148015 
14646305Saw148015 	if ((setname = avd_get_str(fileset->fs_name)) == NULL)
1465*7556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_OK);
14666305Saw148015 
14676305Saw148015 	(void) strcpy(path, pathname);
14685673Saw148015 	(void) strcat(path, "/");
14696305Saw148015 	(void) strcat(path, setname);
14705673Saw148015 	if ((stat64(path, &sb) == 0) &&
14715673Saw148015 	    ((sb.st_mode & S_IFMT) == S_IFBLK) && sb.st_rdev) {
14725673Saw148015 		fileset->fs_attrs |= FILESET_IS_RAW_DEV;
14736305Saw148015 		if (!(fileset->fs_attrs & FILESET_IS_FILE)) {
14746305Saw148015 			filebench_log(LOG_ERROR,
14756305Saw148015 			    "WARNING Fileset %s/%s Cannot be RAW device",
14766305Saw148015 			    avd_get_str(fileset->fs_path),
14776305Saw148015 			    avd_get_str(fileset->fs_name));
14786305Saw148015 			filebench_shutdown(1);
14796305Saw148015 		}
14806305Saw148015 
14815673Saw148015 		return (1);
14825673Saw148015 	}
14835673Saw148015 #endif /* HAVE_RAW_SUPPORT */
14845673Saw148015 
1485*7556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
14865673Saw148015 }
1487