2 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifdef HAVE_ATTR_XATTR_H
25 #include <attr/xattr.h>
27 #ifdef HAVE_ATTR_ATTRIBUTES_H
28 #include <attr/attributes.h>
30 #ifdef HAVE_LINUX_FIEMAP_H
31 #include <linux/fiemap.h>
33 #ifndef HAVE_ATTR_LIST
34 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
36 #ifdef HAVE_SYS_PRCTL_H
37 #include <sys/prctl.h>
44 #ifndef FS_IOC_GETFLAGS
45 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
47 #ifndef FS_IOC_SETFLAGS
48 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
52 #define XFS_ERRTAG_MAX 17
53 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
54 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
59 #define FILELEN_MAX (32*4096)
111 typedef void (*opfnc_t)(int, long);
113 typedef struct opdesc {
121 typedef struct fent {
126 typedef struct flist {
133 typedef struct pathname {
143 struct print_string {
150 #define FT_DIRm (1 << FT_DIR)
152 #define FT_REGm (1 << FT_REG)
154 #define FT_SYMm (1 << FT_SYM)
156 #define FT_DEVm (1 << FT_DEV)
158 #define FT_RTFm (1 << FT_RTF)
160 #define FT_ANYm ((1 << FT_nft) - 1)
161 #define FT_REGFILE (FT_REGm | FT_RTFm)
162 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
164 #define FLIST_SLOT_INCR 16
167 #define MAXFSIZE ((1ULL << 63) - 1ULL)
168 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
170 void afsync_f(int, long);
171 void allocsp_f(int, long);
172 void aread_f(int, long);
173 void attr_remove_f(int, long);
174 void attr_set_f(int, long);
175 void awrite_f(int, long);
176 void bulkstat_f(int, long);
177 void bulkstat1_f(int, long);
178 void chown_f(int, long);
179 void clonerange_f(int, long);
180 void creat_f(int, long);
181 void deduperange_f(int, long);
182 void dread_f(int, long);
183 void dwrite_f(int, long);
184 void fallocate_f(int, long);
185 void fdatasync_f(int, long);
186 void fiemap_f(int, long);
187 void freesp_f(int, long);
188 void fsync_f(int, long);
189 void getattr_f(int, long);
190 void getdents_f(int, long);
191 void link_f(int, long);
192 void mkdir_f(int, long);
193 void mknod_f(int, long);
194 void mread_f(int, long);
195 void mwrite_f(int, long);
196 void punch_f(int, long);
197 void zero_f(int, long);
198 void collapse_f(int, long);
199 void insert_f(int, long);
200 void read_f(int, long);
201 void readlink_f(int, long);
202 void readv_f(int, long);
203 void rename_f(int, long);
204 void resvsp_f(int, long);
205 void rmdir_f(int, long);
206 void setattr_f(int, long);
207 void setxattr_f(int, long);
208 void stat_f(int, long);
209 void symlink_f(int, long);
210 void sync_f(int, long);
211 void truncate_f(int, long);
212 void unlink_f(int, long);
213 void unresvsp_f(int, long);
214 void write_f(int, long);
215 void writev_f(int, long);
218 /* { OP_ENUM, "name", function, freq, iswrite }, */
219 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
220 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
221 { OP_AREAD, "aread", aread_f, 1, 0 },
222 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
223 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
224 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
225 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
226 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
227 { OP_CHOWN, "chown", chown_f, 3, 1 },
228 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
229 { OP_CREAT, "creat", creat_f, 4, 1 },
230 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
231 { OP_DREAD, "dread", dread_f, 4, 0 },
232 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
233 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
234 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
235 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
236 { OP_FREESP, "freesp", freesp_f, 1, 1 },
237 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
238 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
239 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
240 { OP_LINK, "link", link_f, 1, 1 },
241 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
242 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
243 { OP_MREAD, "mread", mread_f, 2, 0 },
244 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
245 { OP_PUNCH, "punch", punch_f, 1, 1 },
246 { OP_ZERO, "zero", zero_f, 1, 1 },
247 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
248 { OP_INSERT, "insert", insert_f, 1, 1 },
249 { OP_READ, "read", read_f, 1, 0 },
250 { OP_READLINK, "readlink", readlink_f, 1, 0 },
251 { OP_READV, "readv", readv_f, 1, 0 },
252 { OP_RENAME, "rename", rename_f, 2, 1 },
253 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
254 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
255 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
256 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
257 { OP_STAT, "stat", stat_f, 1, 0 },
258 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
259 { OP_SYNC, "sync", sync_f, 1, 1 },
260 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
261 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
262 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
263 { OP_WRITE, "write", write_f, 4, 1 },
264 { OP_WRITEV, "writev", writev_f, 4, 1 },
267 flist_t flist[FT_nft] = {
280 xfs_fsop_geom_t geom;
291 unsigned int idmodulo = XFS_IDMODULO_MAX;
292 unsigned int attr_mask = ~0;
295 unsigned long seed = 0;
299 int verifiable_log = 0;
300 sig_atomic_t should_stop = 0;
301 sigjmp_buf *sigbus_jmp = NULL;
302 char *execute_cmd = NULL;
303 int execute_freq = 1;
304 struct print_string flag_str = {0};
306 void add_to_flist(int, int, int);
307 void append_pathname(pathname_t *, char *);
308 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
309 int attr_remove_path(pathname_t *, const char *, int);
310 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
311 void check_cwd(void);
312 void cleanup_flist(void);
313 int creat_path(pathname_t *, mode_t);
314 void dcache_enter(int, int);
315 void dcache_init(void);
316 fent_t *dcache_lookup(int);
317 void dcache_purge(int);
318 void del_from_flist(int, int);
319 int dirid_to_name(char *, int);
321 int fent_to_name(pathname_t *, flist_t *, fent_t *);
322 void fix_parent(int, int);
323 void free_pathname(pathname_t *);
324 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
325 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
326 void init_pathname(pathname_t *);
327 int lchown_path(pathname_t *, uid_t, gid_t);
328 int link_path(pathname_t *, pathname_t *);
329 int lstat64_path(pathname_t *, struct stat64 *);
330 void make_freq_table(void);
331 int mkdir_path(pathname_t *, mode_t);
332 int mknod_path(pathname_t *, mode_t, dev_t);
333 void namerandpad(int, char *, int);
334 int open_path(pathname_t *, int);
335 DIR *opendir_path(pathname_t *);
336 void process_freq(char *);
337 int readlink_path(pathname_t *, char *, size_t);
338 int rename_path(pathname_t *, pathname_t *);
339 int rmdir_path(pathname_t *);
340 void separate_pathname(pathname_t *, char *, pathname_t *);
341 void show_ops(int, char *);
342 int stat64_path(pathname_t *, struct stat64 *);
343 int symlink_path(const char *, pathname_t *);
344 int truncate64_path(pathname_t *, off64_t);
345 int unlink_path(pathname_t *);
347 void write_freq(void);
348 void zero_freq(void);
350 void sg_handler(int signum)
358 * Only handle SIGBUS when mmap write to a hole and no
359 * block can be allocated due to ENOSPC, abort otherwise.
362 siglongjmp(*sigbus_jmp, -1);
364 printf("Unknown SIGBUS is caught, Abort!\n");
367 /* should not reach here */
374 int main(int argc, char **argv)
378 char *dirname = NULL;
379 char *logname = NULL;
380 char rpath[PATH_MAX];
389 xfs_error_injection_t err_inj;
390 struct sigaction action;
392 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
394 errrange = errtag = 0;
396 nops = sizeof(ops) / sizeof(ops[0]);
397 ops_end = &ops[nops];
399 while ((c = getopt(argc, argv, allopts)) != -1) {
408 sscanf(optarg, "%d", &errtag);
412 } else if (errtag == 0)
414 if (errtag >= XFS_ERRTAG_MAX) {
416 "error tag %d too large (max %d)\n",
417 errtag, XFS_ERRTAG_MAX - 1);
422 process_freq(optarg);
425 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
426 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
429 idmodulo = strtoul(optarg, NULL, 0);
430 if (idmodulo > XFS_IDMODULO_MAX) {
432 "chown modulo %d too big (max %d)\n",
433 idmodulo, XFS_IDMODULO_MAX);
438 loops = atoi(optarg);
441 operations = atoi(optarg);
448 nproc = atoi(optarg);
454 seed = strtoul(optarg, NULL, 0);
463 execute_cmd = optarg;
469 attr_mask = strtoul(optarg, NULL, 0);
473 if (optarg[0] == 'c')
484 execute_freq = strtoul(optarg, NULL, 0);
487 fprintf(stderr, "%s - invalid parameters\n",
497 /* no directory specified */
498 if (!nousage) usage();
502 (void)mkdir(dirname, 0777);
503 if (logname && logname[0] != '/') {
504 if (!getcwd(rpath, sizeof(rpath))){
505 perror("getcwd failed");
511 if (chdir(dirname) < 0) {
517 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
518 if (freopen(path, "a", stdout) == NULL) {
519 perror("freopen logfile failed");
523 sprintf(buf, "fss%x", (unsigned int)getpid());
524 fd = creat(buf, 0666);
525 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
526 maxfsize = (off64_t)MAXFSIZE32;
528 maxfsize = (off64_t)MAXFSIZE;
533 gettimeofday(&t, (void *)NULL);
534 seed = (int)t.tv_sec ^ (int)t.tv_usec;
535 printf("seed = %ld\n", seed);
537 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
538 if (i >= 0 && geom.rtblocks)
539 rtpct = MIN(MAX(geom.rtblocks * 100 /
540 (geom.rtblocks + geom.datablocks), 1), 99);
549 for (i = 0; i < j; i++)
552 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
558 for (i = 0; i < j; i++)
561 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
563 printf("Injecting failure on tag #%d\n", errtag);
564 err_inj.errtag = errtag;
566 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
568 perror("fsstress - XFS_SYSSGI error injection call");
577 action.sa_handler = sg_handler;
578 sigemptyset(&action.sa_mask);
580 if (sigaction(SIGTERM, &action, 0)) {
581 perror("sigaction failed");
585 for (i = 0; i < nproc; i++) {
587 sigemptyset(&action.sa_mask);
588 action.sa_handler = SIG_DFL;
589 if (sigaction(SIGTERM, &action, 0))
591 action.sa_handler = sg_handler;
592 if (sigaction(SIGBUS, &action, 0))
594 #ifdef HAVE_SYS_PRCTL_H
595 prctl(PR_SET_PDEATHSIG, SIGKILL);
596 if (getppid() == 1) /* parent died already? */
601 snprintf(path, sizeof(path), "%s/%s.%d",
603 if (freopen(path, "a", stdout) == NULL) {
604 perror("freopen logfile failed");
610 if (io_setup(128, &io_ctx) != 0) {
611 fprintf(stderr, "io_setup failed");
615 for (i = 0; !loops || (i < loops); i++)
618 if(io_destroy(io_ctx) != 0) {
619 fprintf(stderr, "io_destroy failed");
629 while (wait(&stat) > 0 && !should_stop) {
632 action.sa_flags = SA_RESTART;
633 sigaction(SIGTERM, &action, 0);
634 kill(-getpid(), SIGTERM);
635 while (wait(&stat) > 0)
641 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
643 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
645 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
658 add_string(struct print_string *str, const char *add)
660 int len = strlen(add);
665 if (len > (str->max - 1) - str->len) {
666 str->len = str->max - 1;
670 memcpy(str->buffer + str->len, add, len);
672 str->buffer[str->len] = '\0';
678 translate_flags(int flags, const char *delim,
679 const struct print_flags *flag_array)
681 int i, mask, first = 1;
684 if (!flag_str.buffer) {
685 flag_str.buffer = malloc(4096);
689 if (!flag_str.buffer)
692 flag_str.buffer[0] = '\0';
694 for (i = 0; flag_array[i].name && flags; i++) {
695 mask = flag_array[i].mask;
696 if ((flags & mask) != mask)
699 add = flag_array[i].name;
702 add_string(&flag_str, delim);
705 add_string(&flag_str, add);
708 /* Check whether there are any leftover flags. */
714 add_string(&flag_str, delim);
716 ret = snprintf(number, 11, "0x%x", flags) > 0;
717 if (ret > 0 && ret <= 11)
718 add_string(&flag_str, number);
721 return flag_str.buffer;
725 add_to_flist(int ft, int id, int parent)
731 if (ftp->nfiles == ftp->nslots) {
732 ftp->nslots += FLIST_SLOT_INCR;
733 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
735 fep = &ftp->fents[ftp->nfiles++];
737 fep->parent = parent;
741 append_pathname(pathname_t *name, char *str)
747 /* attempting to append to a dir a zero length path */
748 if (len && *str == '/' && name->len == 0) {
749 fprintf(stderr, "fsstress: append_pathname failure\n");
755 name->path = realloc(name->path, name->len + 1 + len);
756 strcpy(&name->path[name->len], str);
761 attr_list_path(pathname_t *name,
763 const int buffersize,
765 attrlist_cursor_t *cursor)
767 char buf[NAME_MAX + 1];
771 if (flags != ATTR_DONTFOLLOW) {
776 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
777 if (rval >= 0 || errno != ENAMETOOLONG)
779 separate_pathname(name, buf, &newname);
780 if (chdir(buf) == 0) {
781 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
784 free_pathname(&newname);
789 attr_remove_path(pathname_t *name, const char *attrname, int flags)
791 char buf[NAME_MAX + 1];
795 rval = attr_remove(name->path, attrname, flags);
796 if (rval >= 0 || errno != ENAMETOOLONG)
798 separate_pathname(name, buf, &newname);
799 if (chdir(buf) == 0) {
800 rval = attr_remove_path(&newname, attrname, flags);
803 free_pathname(&newname);
808 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
809 const int valuelength, int flags)
811 char buf[NAME_MAX + 1];
815 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
816 if (rval >= 0 || errno != ENAMETOOLONG)
818 separate_pathname(name, buf, &newname);
819 if (chdir(buf) == 0) {
820 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
824 free_pathname(&newname);
832 struct stat64 statbuf;
834 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
837 fprintf(stderr, "fsstress: check_cwd failure\n");
844 * go thru flist and release all entries
846 * NOTE: this function shouldn't be called until the end of a process
854 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
863 creat_path(pathname_t *name, mode_t mode)
865 char buf[NAME_MAX + 1];
869 rval = creat(name->path, mode);
870 if (rval >= 0 || errno != ENAMETOOLONG)
872 separate_pathname(name, buf, &newname);
873 if (chdir(buf) == 0) {
874 rval = creat_path(&newname, mode);
877 free_pathname(&newname);
882 dcache_enter(int dirid, int slot)
884 dcache[dirid % NDCACHE] = slot;
892 for (i = 0; i < NDCACHE; i++)
897 dcache_lookup(int dirid)
902 i = dcache[dirid % NDCACHE];
903 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
909 dcache_purge(int dirid)
913 dcp = &dcache[dirid % NDCACHE];
914 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
919 * Delete the item from the list by
920 * moving last entry over the deleted one;
921 * unless deleted entry is the last one.
922 * Input: which file list array and which slot in array
925 del_from_flist(int ft, int slot)
931 dcache_purge(ftp->fents[slot].id);
932 if (slot != ftp->nfiles - 1) {
934 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
935 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
941 dirid_to_fent(int dirid)
947 if ((fep = dcache_lookup(dirid)))
949 flp = &flist[FT_DIR];
950 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
951 if (fep->id == dirid) {
952 dcache_enter(dirid, fep - flp->fents);
962 struct stat64 statbuf;
970 dividend = (operations + execute_freq) / (execute_freq + 1);
971 sprintf(buf, "p%x", procid);
972 (void)mkdir(buf, 0777);
973 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
977 top_ino = statbuf.st_ino;
978 homedir = getcwd(NULL, 0);
980 perror("getcwd failed");
987 for (opno = 0; opno < operations; opno++) {
988 if (execute_cmd && opno && opno % dividend == 0) {
990 printf("%d: execute command %s\n", opno,
992 rval = system(execute_cmd);
994 fprintf(stderr, "execute command failed with "
997 p = &ops[freq_table[random() % freq_table_size]];
998 p->func(opno, random());
1000 * test for forced shutdown by stat'ing the test
1001 * directory. If this stat returns EIO, assume
1002 * the forced shutdown happened.
1004 if (errtag != 0 && opno % 100 == 0) {
1005 rval = stat64(".", &statbuf);
1007 fprintf(stderr, "Detected EIO\n");
1016 sprintf(cmd, "rm -rf %s", buf);
1023 * build up a pathname going thru the file entry and all
1024 * its parent entries
1025 * Return 0 on error, 1 on success;
1028 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1030 char buf[NAME_MAX + 1];
1038 /* build up parent directory name */
1039 if (fep->parent != -1) {
1040 pfep = dirid_to_fent(fep->parent);
1043 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1044 procid, fep->id, fep->parent);
1049 e = fent_to_name(name, &flist[FT_DIR], pfep);
1052 append_pathname(name, "/");
1055 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1056 namerandpad(fep->id, buf, i);
1057 append_pathname(name, buf);
1062 fix_parent(int oldid, int newid)
1069 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1070 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1071 if (fep->parent == oldid)
1072 fep->parent = newid;
1078 free_pathname(pathname_t *name)
1088 * Generate a filename of type ft.
1089 * If we have a fep which should be a directory then use it
1090 * as the parent path for this new filename i.e. prepend with it.
1093 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1095 char buf[NAME_MAX + 1];
1104 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1105 namerandpad(id, buf, len);
1107 /* prepend fep parent dir-name to it */
1109 e = fent_to_name(name, &flist[FT_DIR], fep);
1112 append_pathname(name, "/");
1114 append_pathname(name, buf);
1118 for (j = 0; !*v && j < ilistlen; j++) {
1119 if (ilist[j] == id) {
1129 * Input: "which" to choose the file-types eg. non-directory
1130 * Input: "r" to choose which file
1131 * Output: file-list, file-entry, name for the chosen file.
1132 * Output: verbose if chosen file is on the ilist.
1135 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1138 int totalsum = 0; /* total number of matching files */
1139 int partialsum = 0; /* partial sum of matching files */
1145 int e = 1; /* success */
1148 * go thru flist and add up number of files for each
1149 * category that matches with <which>.
1151 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1152 if (which & (1 << i))
1153 totalsum += flp->nfiles;
1155 if (totalsum == 0) {
1165 * Now we have possible matches between 0..totalsum-1.
1166 * And we use r to help us choose which one we want,
1167 * which when bounded by totalsum becomes x.
1169 x = (int)(r % totalsum);
1170 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1171 if (which & (1 << i)) {
1172 if (x < partialsum + flp->nfiles) {
1174 /* found the matching file entry */
1175 fep = &flp->fents[x - partialsum];
1177 /* fill-in what we were asked for */
1179 e = fent_to_name(name, flp, fep);
1182 fprintf(stderr, "%d: failed to get path for entry:"
1183 " id=%d,parent=%d\n",
1184 procid, fep->id, fep->parent);
1193 /* turn on verbose if its an ilisted file */
1195 for (j = 0; !*v && j < ilistlen; j++) {
1196 if (ilist[j] == fep->id) {
1203 partialsum += flp->nfiles;
1207 fprintf(stderr, "fsstress: get_fname failure\n");
1214 init_pathname(pathname_t *name)
1221 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1223 char buf[NAME_MAX + 1];
1227 rval = lchown(name->path, owner, group);
1228 if (rval >= 0 || errno != ENAMETOOLONG)
1230 separate_pathname(name, buf, &newname);
1231 if (chdir(buf) == 0) {
1232 rval = lchown_path(&newname, owner, group);
1235 free_pathname(&newname);
1240 link_path(pathname_t *name1, pathname_t *name2)
1242 char buf1[NAME_MAX + 1];
1243 char buf2[NAME_MAX + 1];
1245 pathname_t newname1;
1246 pathname_t newname2;
1249 rval = link(name1->path, name2->path);
1250 if (rval >= 0 || errno != ENAMETOOLONG)
1252 separate_pathname(name1, buf1, &newname1);
1253 separate_pathname(name2, buf2, &newname2);
1254 if (strcmp(buf1, buf2) == 0) {
1255 if (chdir(buf1) == 0) {
1256 rval = link_path(&newname1, &newname2);
1260 if (strcmp(buf1, "..") == 0)
1262 else if (strcmp(buf2, "..") == 0)
1264 else if (strlen(buf1) == 0)
1266 else if (strlen(buf2) == 0)
1269 down1 = MAX(newname1.len, 3 + name2->len) <=
1270 MAX(3 + name1->len, newname2.len);
1272 free_pathname(&newname2);
1273 append_pathname(&newname2, "../");
1274 append_pathname(&newname2, name2->path);
1275 if (chdir(buf1) == 0) {
1276 rval = link_path(&newname1, &newname2);
1280 free_pathname(&newname1);
1281 append_pathname(&newname1, "../");
1282 append_pathname(&newname1, name1->path);
1283 if (chdir(buf2) == 0) {
1284 rval = link_path(&newname1, &newname2);
1289 free_pathname(&newname1);
1290 free_pathname(&newname2);
1295 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1297 char buf[NAME_MAX + 1];
1301 rval = lstat64(name->path, sbuf);
1302 if (rval >= 0 || errno != ENAMETOOLONG)
1304 separate_pathname(name, buf, &newname);
1305 if (chdir(buf) == 0) {
1306 rval = lstat64_path(&newname, sbuf);
1309 free_pathname(&newname);
1314 make_freq_table(void)
1320 for (p = ops, f = 0; p < ops_end; p++)
1322 freq_table = malloc(f * sizeof(*freq_table));
1323 freq_table_size = f;
1324 for (p = ops, i = 0; p < ops_end; p++) {
1325 for (f = 0; f < p->freq; f++, i++)
1326 freq_table[i] = p->op;
1331 mkdir_path(pathname_t *name, mode_t mode)
1333 char buf[NAME_MAX + 1];
1337 rval = mkdir(name->path, mode);
1338 if (rval >= 0 || errno != ENAMETOOLONG)
1340 separate_pathname(name, buf, &newname);
1341 if (chdir(buf) == 0) {
1342 rval = mkdir_path(&newname, mode);
1345 free_pathname(&newname);
1350 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1352 char buf[NAME_MAX + 1];
1356 rval = mknod(name->path, mode, dev);
1357 if (rval >= 0 || errno != ENAMETOOLONG)
1359 separate_pathname(name, buf, &newname);
1360 if (chdir(buf) == 0) {
1361 rval = mknod_path(&newname, mode, dev);
1364 free_pathname(&newname);
1369 namerandpad(int id, char *buf, int i)
1372 static int buckets[] =
1373 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1379 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1380 padmod = buckets[bucket] + 1 - i;
1383 padlen = (id ^ namerand) % padmod;
1385 memset(&buf[i], 'X', padlen);
1386 buf[i + padlen] = '\0';
1391 open_path(pathname_t *name, int oflag)
1393 char buf[NAME_MAX + 1];
1397 rval = open(name->path, oflag);
1398 if (rval >= 0 || errno != ENAMETOOLONG)
1400 separate_pathname(name, buf, &newname);
1401 if (chdir(buf) == 0) {
1402 rval = open_path(&newname, oflag);
1405 free_pathname(&newname);
1410 opendir_path(pathname_t *name)
1412 char buf[NAME_MAX + 1];
1416 rval = opendir(name->path);
1417 if (rval || errno != ENAMETOOLONG)
1419 separate_pathname(name, buf, &newname);
1420 if (chdir(buf) == 0) {
1421 rval = opendir_path(&newname);
1424 free_pathname(&newname);
1429 process_freq(char *arg)
1434 s = strchr(arg, '=');
1436 fprintf(stderr, "bad argument '%s'\n", arg);
1440 for (p = ops; p < ops_end; p++) {
1441 if (strcmp(arg, p->name) == 0) {
1446 fprintf(stderr, "can't find op type %s for -f\n", arg);
1451 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1453 char buf[NAME_MAX + 1];
1457 rval = readlink(name->path, lbuf, lbufsiz);
1458 if (rval >= 0 || errno != ENAMETOOLONG)
1460 separate_pathname(name, buf, &newname);
1461 if (chdir(buf) == 0) {
1462 rval = readlink_path(&newname, lbuf, lbufsiz);
1465 free_pathname(&newname);
1470 rename_path(pathname_t *name1, pathname_t *name2)
1472 char buf1[NAME_MAX + 1];
1473 char buf2[NAME_MAX + 1];
1475 pathname_t newname1;
1476 pathname_t newname2;
1479 rval = rename(name1->path, name2->path);
1480 if (rval >= 0 || errno != ENAMETOOLONG)
1482 separate_pathname(name1, buf1, &newname1);
1483 separate_pathname(name2, buf2, &newname2);
1484 if (strcmp(buf1, buf2) == 0) {
1485 if (chdir(buf1) == 0) {
1486 rval = rename_path(&newname1, &newname2);
1490 if (strcmp(buf1, "..") == 0)
1492 else if (strcmp(buf2, "..") == 0)
1494 else if (strlen(buf1) == 0)
1496 else if (strlen(buf2) == 0)
1499 down1 = MAX(newname1.len, 3 + name2->len) <=
1500 MAX(3 + name1->len, newname2.len);
1502 free_pathname(&newname2);
1503 append_pathname(&newname2, "../");
1504 append_pathname(&newname2, name2->path);
1505 if (chdir(buf1) == 0) {
1506 rval = rename_path(&newname1, &newname2);
1510 free_pathname(&newname1);
1511 append_pathname(&newname1, "../");
1512 append_pathname(&newname1, name1->path);
1513 if (chdir(buf2) == 0) {
1514 rval = rename_path(&newname1, &newname2);
1519 free_pathname(&newname1);
1520 free_pathname(&newname2);
1525 rmdir_path(pathname_t *name)
1527 char buf[NAME_MAX + 1];
1531 rval = rmdir(name->path);
1532 if (rval >= 0 || errno != ENAMETOOLONG)
1534 separate_pathname(name, buf, &newname);
1535 if (chdir(buf) == 0) {
1536 rval = rmdir_path(&newname);
1539 free_pathname(&newname);
1544 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1548 init_pathname(newname);
1549 slash = strchr(name->path, '/');
1550 if (slash == NULL) {
1555 strcpy(buf, name->path);
1557 append_pathname(newname, slash + 1);
1563 show_ops(int flag, char *lead_str)
1568 /* print in list form */
1571 for (p = ops; p < ops_end; p++) {
1572 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1573 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1574 x+=printf("%s ", p->name);
1577 } else if (flag == 0) {
1578 /* Table view style */
1580 for (f = 0, p = ops; p < ops_end; p++)
1586 for (p = ops; p < ops_end; p++) {
1587 if (flag != 0 || p->freq > 0) {
1588 if (lead_str != NULL)
1589 printf("%s", lead_str);
1590 printf("%20s %d/%d %s\n",
1591 p->name, p->freq, f,
1592 (p->iswrite == 0) ? " " : "write op");
1596 /* Command line style */
1597 if (lead_str != NULL)
1598 printf("%s", lead_str);
1599 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1601 for (p = ops; p < ops_end; p++)
1603 printf("-f %s=%d \\\n",p->name, p->freq);
1608 stat64_path(pathname_t *name, struct stat64 *sbuf)
1610 char buf[NAME_MAX + 1];
1614 rval = stat64(name->path, sbuf);
1615 if (rval >= 0 || errno != ENAMETOOLONG)
1617 separate_pathname(name, buf, &newname);
1618 if (chdir(buf) == 0) {
1619 rval = stat64_path(&newname, sbuf);
1622 free_pathname(&newname);
1627 symlink_path(const char *name1, pathname_t *name)
1629 char buf[NAME_MAX + 1];
1633 if (!strcmp(name1, name->path)) {
1634 printf("yikes! %s %s\n", name1, name->path);
1638 rval = symlink(name1, name->path);
1639 if (rval >= 0 || errno != ENAMETOOLONG)
1641 separate_pathname(name, buf, &newname);
1642 if (chdir(buf) == 0) {
1643 rval = symlink_path(name1, &newname);
1646 free_pathname(&newname);
1651 truncate64_path(pathname_t *name, off64_t length)
1653 char buf[NAME_MAX + 1];
1657 rval = truncate64(name->path, length);
1658 if (rval >= 0 || errno != ENAMETOOLONG)
1660 separate_pathname(name, buf, &newname);
1661 if (chdir(buf) == 0) {
1662 rval = truncate64_path(&newname, length);
1665 free_pathname(&newname);
1670 unlink_path(pathname_t *name)
1672 char buf[NAME_MAX + 1];
1676 rval = unlink(name->path);
1677 if (rval >= 0 || errno != ENAMETOOLONG)
1679 separate_pathname(name, buf, &newname);
1680 if (chdir(buf) == 0) {
1681 rval = unlink_path(&newname);
1684 free_pathname(&newname);
1691 printf("Usage: %s -H or\n", myprog);
1692 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1694 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1696 printf(" -c clean up the test directory after each run\n");
1697 printf(" -d dir specifies the base directory for operations\n");
1698 printf(" -e errtg specifies error injection stuff\n");
1699 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1700 printf(" the valid operation names are:\n");
1702 printf(" -i filenum get verbose output for this nth file object\n");
1703 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1704 printf(" *use 0 for infinite (default 1)\n");
1705 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1706 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1707 printf(" -o logfile specifies logfile name\n");
1708 printf(" -p nproc specifies the no. of processes (default 1)\n");
1709 printf(" -r specifies random name padding\n");
1710 printf(" -s seed specifies the seed for the random generator (default random)\n");
1711 printf(" -v specifies verbose mode\n");
1712 printf(" -w zeros frequencies of non-write operations\n");
1713 printf(" -x cmd execute command in the middle of operations\n");
1714 printf(" -z zeros frequencies of all operations\n");
1715 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1716 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1717 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1718 printf(" -H prints usage and exits\n");
1726 for (p = ops; p < ops_end; p++) {
1737 for (p = ops; p < ops_end; p++)
1741 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1744 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1745 verifiable_log ? -1: (long)s->st_ino,
1746 (long)s->st_nlink, s->st_uid, s->st_gid,
1747 (long long) s->st_blocks, (long long) s->st_size);
1751 afsync_f(int opno, long r)
1759 struct iocb *iocbs[] = { &iocb };
1760 struct io_event event;
1763 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1765 printf("%d/%d: afsync - no filename\n", procid, opno);
1769 fd = open_path(&f, O_WRONLY | O_DIRECT);
1770 e = fd < 0 ? errno : 0;
1774 printf("%d/%d: afsync - open %s failed %d\n",
1775 procid, opno, f.path, e);
1780 io_prep_fsync(&iocb, fd);
1781 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1783 printf("%d/%d: afsync - io_submit %s %d\n",
1784 procid, opno, f.path, e);
1789 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1791 printf("%d/%d: afsync - io_getevents failed %d\n",
1800 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1807 allocsp_f(int opno, long r)
1812 struct xfs_flock64 fl;
1820 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1822 printf("%d/%d: allocsp - no filename\n", procid, opno);
1826 fd = open_path(&f, O_RDWR);
1827 e = fd < 0 ? errno : 0;
1831 printf("%d/%d: allocsp - open %s failed %d\n",
1832 procid, opno, f.path, e);
1836 if (fstat64(fd, &stb) < 0) {
1838 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1839 procid, opno, f.path, errno);
1844 inode_info(st, sizeof(st), &stb, v);
1845 lr = ((__int64_t)random() << 32) + random();
1846 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1848 fl.l_whence = SEEK_SET;
1851 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1853 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1854 procid, opno, f.path, st, (long long)off, e);
1862 do_aio_rw(int opno, long r, int flags)
1866 struct dioattr diob;
1878 struct io_event event;
1879 struct iocb *iocbs[] = { &iocb };
1880 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1883 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1885 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1889 fd = open_path(&f, flags|O_DIRECT);
1890 e = fd < 0 ? errno : 0;
1894 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1895 procid, opno, f.path, e);
1899 if (fstat64(fd, &stb) < 0) {
1901 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1902 procid, opno, f.path, errno);
1907 inode_info(st, sizeof(st), &stb, v);
1908 if (!iswrite && stb.st_size == 0) {
1910 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1916 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1919 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1920 " fallback to stat()\n",
1921 procid, opno, f.path, st, errno);
1922 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1923 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1925 dio_env = getenv("XFS_DIO_MIN");
1927 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1928 align = (__int64_t)diob.d_miniosz;
1929 lr = ((__int64_t)random() << 32) + random();
1930 len = (random() % FILELEN_MAX) + 1;
1931 len -= (len % align);
1934 else if (len > diob.d_maxiosz)
1935 len = diob.d_maxiosz;
1936 buf = memalign(diob.d_mem, len);
1939 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1940 off -= (off % align);
1942 memset(buf, nameseq & 0xff, len);
1943 io_prep_pwrite(&iocb, fd, buf, len, off);
1945 off = (off64_t)(lr % stb.st_size);
1946 off -= (off % align);
1947 io_prep_pread(&iocb, fd, buf, len, off);
1949 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1951 printf("%d/%d: %s - io_submit failed %d\n",
1952 procid, opno, iswrite ? "awrite" : "aread", e);
1957 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1959 printf("%d/%d: %s - io_getevents failed %d\n",
1960 procid, opno, iswrite ? "awrite" : "aread", e);
1966 e = event.res != len ? event.res2 : 0;
1969 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1970 procid, opno, iswrite ? "awrite" : "aread",
1971 f.path, st, (long long)off, (int)len, e);
1978 aread_f(int opno, long r)
1981 do_aio_rw(opno, r, O_RDONLY);
1986 attr_remove_f(int opno, long r)
1988 attrlist_ent_t *aep;
1992 attrlist_cursor_t cursor;
2001 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2002 append_pathname(&f, ".");
2004 bzero(&cursor, sizeof(cursor));
2006 bzero(buf, sizeof(buf));
2007 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2011 alist = (attrlist_t *)buf;
2012 total += alist->al_count;
2013 } while (alist->al_more);
2016 printf("%d/%d: attr_remove - no attrs for %s\n",
2017 procid, opno, f.path);
2021 which = (int)(random() % total);
2022 bzero(&cursor, sizeof(cursor));
2026 bzero(buf, sizeof(buf));
2027 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2031 alist = (attrlist_t *)buf;
2032 if (which < ent + alist->al_count) {
2033 aep = (attrlist_ent_t *)
2034 &buf[alist->al_offset[which - ent]];
2035 aname = aep->a_name;
2038 ent += alist->al_count;
2039 } while (alist->al_more);
2040 if (aname == NULL) {
2043 "%d/%d: attr_remove - name %d not found at %s\n",
2044 procid, opno, which, f.path);
2048 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2051 printf("%d/%d: attr_remove %s %s %d\n",
2052 procid, opno, f.path, aname, e);
2057 attr_set_f(int opno, long r)
2064 static int lengths[] = { 10, 100, 1000, 10000 };
2069 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2070 append_pathname(&f, ".");
2071 sprintf(aname, "a%x", nameseq++);
2072 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2073 len = (int)(random() % lengths[li]);
2077 memset(aval, nameseq & 0xff, len);
2078 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2083 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2089 awrite_f(int opno, long r)
2092 do_aio_rw(opno, r, O_WRONLY);
2097 bulkstat_f(int opno, long r)
2105 xfs_fsop_bulkreq_t bsr;
2108 nent = (r % 999) + 2;
2109 t = malloc(nent * sizeof(*t));
2110 fd = open(".", O_RDONLY);
2118 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2122 printf("%d/%d: bulkstat nent %d total %lld\n",
2123 procid, opno, nent, (long long)total);
2128 bulkstat1_f(int opno, long r)
2138 xfs_fsop_bulkreq_t bsr;
2141 good = random() & 1;
2143 /* use an inode we know exists */
2145 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2146 append_pathname(&f, ".");
2147 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2152 * pick a random inode
2154 * note this can generate kernel warning messages
2155 * since bulkstat_one will read the disk block that
2156 * would contain a given inode even if that disk
2157 * block doesn't contain inodes.
2159 * this is detected later, but not until after the
2160 * warning is displayed.
2162 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2168 fd = open(".", O_RDONLY);
2174 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2176 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2177 procid, opno, good?"real":"random",
2178 verifiable_log ? -1LL : (long long)ino, e);
2183 chown_f(int opno, long r)
2193 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2194 append_pathname(&f, ".");
2195 u = (uid_t)random();
2196 g = (gid_t)random();
2197 nbits = (int)(random() % idmodulo);
2198 u &= (1 << nbits) - 1;
2199 g &= (1 << nbits) - 1;
2200 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2203 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2207 /* reflink some arbitrary range of f1 to f2. */
2214 struct file_clone_range fcr;
2215 struct pathname fpath1;
2216 struct pathname fpath2;
2217 struct stat64 stat1;
2218 struct stat64 stat2;
2219 char inoinfo1[1024];
2220 char inoinfo2[1024];
2233 init_pathname(&fpath1);
2234 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2236 printf("%d/%d: clonerange read - no filename\n",
2241 init_pathname(&fpath2);
2242 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2244 printf("%d/%d: clonerange write - no filename\n",
2250 fd1 = open_path(&fpath1, O_RDONLY);
2251 e = fd1 < 0 ? errno : 0;
2255 printf("%d/%d: clonerange read - open %s failed %d\n",
2256 procid, opno, fpath1.path, e);
2260 fd2 = open_path(&fpath2, O_WRONLY);
2261 e = fd2 < 0 ? errno : 0;
2265 printf("%d/%d: clonerange write - open %s failed %d\n",
2266 procid, opno, fpath2.path, e);
2270 /* Get file stats */
2271 if (fstat64(fd1, &stat1) < 0) {
2273 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2274 procid, opno, fpath1.path, errno);
2277 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2279 if (fstat64(fd2, &stat2) < 0) {
2281 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2282 procid, opno, fpath2.path, errno);
2285 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2287 /* Calculate offsets */
2288 len = (random() % FILELEN_MAX) + 1;
2289 len &= ~(stat1.st_blksize - 1);
2291 len = stat1.st_blksize;
2292 if (len > stat1.st_size)
2293 len = stat1.st_size;
2295 lr = ((__int64_t)random() << 32) + random();
2296 if (stat1.st_size == len)
2299 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2301 off1 &= ~(stat1.st_blksize - 1);
2304 * If srcfile == destfile, randomly generate destination ranges
2305 * until we find one that doesn't overlap the source range.
2308 lr = ((__int64_t)random() << 32) + random();
2309 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024 * 1024), MAXFSIZE));
2311 off2 &= ~(stat2.st_blksize - 1);
2312 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2314 /* Clone data blocks */
2316 fcr.src_offset = off1;
2317 fcr.src_length = len;
2318 fcr.dest_offset = off2;
2320 ret = ioctl(fd2, FICLONERANGE, &fcr);
2321 e = ret < 0 ? errno : 0;
2323 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2325 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2326 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2329 printf(" error %d", e);
2338 free_pathname(&fpath2);
2340 free_pathname(&fpath1);
2344 /* dedupe some arbitrary range of f1 to f2...fn. */
2350 #ifdef FIDEDUPERANGE
2351 #define INFO_SZ 1024
2352 struct file_dedupe_range *fdr;
2353 struct pathname *fpath;
2354 struct stat64 *stat;
2366 if (flist[FT_REG].nfiles < 2)
2369 /* Pick somewhere between 2 and 128 files. */
2371 nr = random() % (flist[FT_REG].nfiles + 1);
2372 } while (nr < 2 || nr > 128);
2375 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2376 sizeof(struct file_dedupe_range));
2378 printf("%d/%d: line %d error %d\n",
2379 procid, opno, __LINE__, errno);
2382 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2383 sizeof(struct file_dedupe_range)));
2385 fpath = calloc(nr, sizeof(struct pathname));
2387 printf("%d/%d: line %d error %d\n",
2388 procid, opno, __LINE__, errno);
2392 stat = calloc(nr, sizeof(struct stat64));
2394 printf("%d/%d: line %d error %d\n",
2395 procid, opno, __LINE__, errno);
2399 info = calloc(nr, INFO_SZ);
2401 printf("%d/%d: line %d error %d\n",
2402 procid, opno, __LINE__, errno);
2406 off = calloc(nr, sizeof(off64_t));
2408 printf("%d/%d: line %d error %d\n",
2409 procid, opno, __LINE__, errno);
2413 v = calloc(nr, sizeof(int));
2415 printf("%d/%d: line %d error %d\n",
2416 procid, opno, __LINE__, errno);
2419 fd = calloc(nr, sizeof(int));
2421 printf("%d/%d: line %d error %d\n",
2422 procid, opno, __LINE__, errno);
2425 memset(fd, 0xFF, nr * sizeof(int));
2427 /* Get paths for all files */
2428 for (i = 0; i < nr; i++)
2429 init_pathname(&fpath[i]);
2431 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2433 printf("%d/%d: deduperange read - no filename\n",
2438 for (i = 1; i < nr; i++) {
2439 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2441 printf("%d/%d: deduperange write - no filename\n",
2448 fd[0] = open_path(&fpath[0], O_RDONLY);
2449 e = fd[0] < 0 ? errno : 0;
2453 printf("%d/%d: deduperange read - open %s failed %d\n",
2454 procid, opno, fpath[0].path, e);
2458 for (i = 1; i < nr; i++) {
2459 fd[i] = open_path(&fpath[i], O_WRONLY);
2460 e = fd[i] < 0 ? errno : 0;
2464 printf("%d/%d: deduperange write - open %s failed %d\n",
2465 procid, opno, fpath[i].path, e);
2470 /* Get file stats */
2471 if (fstat64(fd[0], &stat[0]) < 0) {
2473 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2474 procid, opno, fpath[0].path, errno);
2478 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2480 for (i = 1; i < nr; i++) {
2481 if (fstat64(fd[i], &stat[i]) < 0) {
2483 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2484 procid, opno, fpath[i].path, errno);
2487 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2490 /* Never try to dedupe more than half of the src file. */
2491 len = (random() % FILELEN_MAX) + 1;
2492 len &= ~(stat[0].st_blksize - 1);
2494 len = stat[0].st_blksize / 2;
2495 if (len > stat[0].st_size / 2)
2496 len = stat[0].st_size / 2;
2498 /* Calculate offsets */
2499 lr = ((__int64_t)random() << 32) + random();
2500 if (stat[0].st_size == len)
2503 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2505 off[0] &= ~(stat[0].st_blksize - 1);
2508 * If srcfile == destfile[i], randomly generate destination ranges
2509 * until we find one that doesn't overlap the source range.
2511 for (i = 1; i < nr; i++) {
2515 lr = ((__int64_t)random() << 32) + random();
2516 if (stat[i].st_size <= len)
2519 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2521 off[i] &= ~(stat[i].st_blksize - 1);
2522 } while (stat[0].st_ino == stat[i].st_ino &&
2523 llabs(off[i] - off[0]) < len &&
2527 /* Clone data blocks */
2528 fdr->src_offset = off[0];
2529 fdr->src_length = len;
2530 fdr->dest_count = nr - 1;
2531 for (i = 1; i < nr; i++) {
2532 fdr->info[i - 1].dest_fd = fd[i];
2533 fdr->info[i - 1].dest_offset = off[i];
2536 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2537 e = ret < 0 ? errno : 0;
2539 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2541 fpath[0].path, &info[0], (long long)off[0],
2544 printf(" error %d", e);
2550 for (i = 1; i < nr; i++) {
2551 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2553 printf("%d/%d: ...to %s%s [%lld,%lld]",
2555 fpath[i].path, &info[i * INFO_SZ],
2556 (long long)off[i], (long long)len);
2557 if (fdr->info[i - 1].status < 0)
2558 printf(" error %d", e);
2559 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2560 printf(" %llu bytes deduplicated",
2561 fdr->info[i - 1].bytes_deduped);
2562 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2563 printf(" differed");
2569 for (i = 0; i < nr; i++)
2573 for (i = 0; i < nr; i++)
2574 free_pathname(&fpath[i]);
2593 setxattr_f(int opno, long r)
2595 #ifdef XFS_XFLAG_EXTSIZE
2605 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2606 append_pathname(&f, ".");
2607 fd = open_path(&f, O_RDWR);
2608 e = fd < 0 ? errno : 0;
2613 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2614 nbits = (int)(random() % e);
2615 p &= (1 << nbits) - 1;
2617 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2619 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2622 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2629 creat_f(int opno, long r)
2644 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2649 e1 = (random() % 100);
2650 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2652 if (type == FT_RTF) /* rt always gets an extsize */
2653 extsize = (random() % 10) + 1;
2654 else if (e1 < 10) /* one-in-ten get an extsize */
2655 extsize = random() % 1024;
2659 e = generate_fname(fep, type, &f, &id, &v);
2663 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2664 printf("%d/%d: creat - no filename from %s\n",
2665 procid, opno, f.path);
2670 fd = creat_path(&f, 0666);
2671 e = fd < 0 ? errno : 0;
2676 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2677 if (type == FT_RTF) {
2678 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2679 a.fsx_extsize = extsize *
2680 geom.rtextsize * geom.blocksize;
2682 } else if (extsize) {
2683 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2684 a.fsx_extsize = extsize * geom.blocksize;
2687 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2690 add_to_flist(type, id, parid);
2694 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2695 extsize ? a.fsx_extsize : 0, e, e1);
2696 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2702 dread_f(int opno, long r)
2706 struct dioattr diob;
2719 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2721 printf("%d/%d: dread - no filename\n", procid, opno);
2725 fd = open_path(&f, O_RDONLY|O_DIRECT);
2726 e = fd < 0 ? errno : 0;
2730 printf("%d/%d: dread - open %s failed %d\n",
2731 procid, opno, f.path, e);
2735 if (fstat64(fd, &stb) < 0) {
2737 printf("%d/%d: dread - fstat64 %s failed %d\n",
2738 procid, opno, f.path, errno);
2743 inode_info(st, sizeof(st), &stb, v);
2744 if (stb.st_size == 0) {
2746 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2752 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2755 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2756 " fallback to stat()\n",
2757 procid, opno, f.path, st, errno);
2758 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2759 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2762 dio_env = getenv("XFS_DIO_MIN");
2764 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2766 align = (__int64_t)diob.d_miniosz;
2767 lr = ((__int64_t)random() << 32) + random();
2768 off = (off64_t)(lr % stb.st_size);
2769 off -= (off % align);
2770 lseek64(fd, off, SEEK_SET);
2771 len = (random() % FILELEN_MAX) + 1;
2772 len -= (len % align);
2775 else if (len > diob.d_maxiosz)
2776 len = diob.d_maxiosz;
2777 buf = memalign(diob.d_mem, len);
2778 e = read(fd, buf, len) < 0 ? errno : 0;
2781 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2782 procid, opno, f.path, st, (long long)off, (int)len, e);
2788 dwrite_f(int opno, long r)
2792 struct dioattr diob;
2805 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2807 printf("%d/%d: dwrite - no filename\n", procid, opno);
2811 fd = open_path(&f, O_WRONLY|O_DIRECT);
2812 e = fd < 0 ? errno : 0;
2816 printf("%d/%d: dwrite - open %s failed %d\n",
2817 procid, opno, f.path, e);
2821 if (fstat64(fd, &stb) < 0) {
2823 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2824 procid, opno, f.path, errno);
2829 inode_info(st, sizeof(st), &stb, v);
2830 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2832 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2833 " %s%s return %d, fallback to stat()\n",
2834 procid, opno, f.path, st, errno);
2835 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2836 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2839 dio_env = getenv("XFS_DIO_MIN");
2841 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2843 align = (__int64_t)diob.d_miniosz;
2844 lr = ((__int64_t)random() << 32) + random();
2845 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2846 off -= (off % align);
2847 lseek64(fd, off, SEEK_SET);
2848 len = (random() % FILELEN_MAX) + 1;
2849 len -= (len % align);
2852 else if (len > diob.d_maxiosz)
2853 len = diob.d_maxiosz;
2854 buf = memalign(diob.d_mem, len);
2856 lseek64(fd, off, SEEK_SET);
2857 memset(buf, nameseq & 0xff, len);
2858 e = write(fd, buf, len) < 0 ? errno : 0;
2861 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2862 procid, opno, f.path, st, (long long)off, (int)len, e);
2868 #ifdef HAVE_LINUX_FALLOC_H
2869 struct print_flags falloc_flags [] = {
2870 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2871 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2872 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2873 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2874 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2875 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2879 #define translate_falloc_flags(mode) \
2880 ({translate_flags(mode, "|", falloc_flags);})
2884 do_fallocate(int opno, long r, int mode)
2886 #ifdef HAVE_LINUX_FALLOC_H
2898 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2900 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2904 fd = open_path(&f, O_RDWR);
2907 printf("%d/%d: do_fallocate - open %s failed %d\n",
2908 procid, opno, f.path, errno);
2913 if (fstat64(fd, &stb) < 0) {
2915 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2916 procid, opno, f.path, errno);
2921 inode_info(st, sizeof(st), &stb, v);
2922 lr = ((__int64_t)random() << 32) + random();
2923 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2925 len = (off64_t)(random() % (1024 * 1024));
2927 * Collapse/insert range requires off and len to be block aligned,
2928 * make it more likely to be the case.
2930 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2932 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2933 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2935 mode |= FALLOC_FL_KEEP_SIZE & random();
2936 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2938 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2939 procid, opno, translate_falloc_flags(mode),
2940 f.path, st, (long long)off, (long long)len, e);
2947 fallocate_f(int opno, long r)
2949 #ifdef HAVE_LINUX_FALLOC_H
2950 do_fallocate(opno, r, 0);
2955 fdatasync_f(int opno, long r)
2963 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2965 printf("%d/%d: fdatasync - no filename\n",
2970 fd = open_path(&f, O_WRONLY);
2971 e = fd < 0 ? errno : 0;
2975 printf("%d/%d: fdatasync - open %s failed %d\n",
2976 procid, opno, f.path, e);
2980 e = fdatasync(fd) < 0 ? errno : 0;
2982 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2987 #ifdef HAVE_LINUX_FIEMAP_H
2988 struct print_flags fiemap_flags[] = {
2989 { FIEMAP_FLAG_SYNC, "SYNC"},
2990 { FIEMAP_FLAG_XATTR, "XATTR"},
2994 #define translate_fiemap_flags(mode) \
2995 ({translate_flags(mode, "|", fiemap_flags);})
2999 fiemap_f(int opno, long r)
3001 #ifdef HAVE_LINUX_FIEMAP_H
3011 struct fiemap *fiemap;
3014 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3016 printf("%d/%d: fiemap - no filename\n", procid, opno);
3020 fd = open_path(&f, O_RDWR);
3021 e = fd < 0 ? errno : 0;
3025 printf("%d/%d: fiemap - open %s failed %d\n",
3026 procid, opno, f.path, e);
3030 if (fstat64(fd, &stb) < 0) {
3032 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3033 procid, opno, f.path, errno);
3038 inode_info(st, sizeof(st), &stb, v);
3039 blocks_to_map = random() & 0xffff;
3040 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3041 (blocks_to_map * sizeof(struct fiemap_extent)));
3044 printf("%d/%d: malloc failed \n", procid, opno);
3049 lr = ((__int64_t)random() << 32) + random();
3050 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3052 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3053 fiemap->fm_extent_count = blocks_to_map;
3054 fiemap->fm_mapped_extents = random() & 0xffff;
3055 fiemap->fm_start = off;
3056 fiemap->fm_length = ((__int64_t)random() << 32) + random();
3058 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3060 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3061 procid, opno, f.path, st, (long long)fiemap->fm_start,
3062 (long long) fiemap->fm_length,
3063 translate_fiemap_flags(fiemap->fm_flags), e);
3071 freesp_f(int opno, long r)
3076 struct xfs_flock64 fl;
3084 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3086 printf("%d/%d: freesp - no filename\n", procid, opno);
3090 fd = open_path(&f, O_RDWR);
3091 e = fd < 0 ? errno : 0;
3095 printf("%d/%d: freesp - open %s failed %d\n",
3096 procid, opno, f.path, e);
3100 if (fstat64(fd, &stb) < 0) {
3102 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3103 procid, opno, f.path, errno);
3108 inode_info(st, sizeof(st), &stb, v);
3109 lr = ((__int64_t)random() << 32) + random();
3110 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3112 fl.l_whence = SEEK_SET;
3115 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3117 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3118 procid, opno, f.path, st, (long long)off, e);
3124 fsync_f(int opno, long r)
3132 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3134 printf("%d/%d: fsync - no filename\n", procid, opno);
3138 fd = open_path(&f, O_WRONLY);
3139 e = fd < 0 ? errno : 0;
3143 printf("%d/%d: fsync - open %s failed %d\n",
3144 procid, opno, f.path, e);
3148 e = fsync(fd) < 0 ? errno : 0;
3150 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3156 getattr_f(int opno, long r)
3165 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3166 append_pathname(&f, ".");
3167 fd = open_path(&f, O_RDWR);
3168 e = fd < 0 ? errno : 0;
3171 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3173 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3179 getdents_f(int opno, long r)
3186 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3187 append_pathname(&f, ".");
3188 dir = opendir_path(&f);
3192 printf("%d/%d: getdents - can't open %s\n",
3193 procid, opno, f.path);
3197 while (readdir64(dir) != NULL)
3200 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3206 link_f(int opno, long r)
3219 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3221 printf("%d/%d: link - no file\n", procid, opno);
3225 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3231 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3235 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3236 printf("%d/%d: link - no filename from %s\n",
3237 procid, opno, l.path);
3243 e = link_path(&f, &l) < 0 ? errno : 0;
3246 add_to_flist(flp - flist, id, parid);
3248 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3250 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3257 mkdir_f(int opno, long r)
3267 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3272 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3276 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3277 printf("%d/%d: mkdir - no filename from %s\n",
3278 procid, opno, f.path);
3283 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3286 add_to_flist(FT_DIR, id, parid);
3288 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3289 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3295 mknod_f(int opno, long r)
3305 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3310 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3314 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3315 printf("%d/%d: mknod - no filename from %s\n",
3316 procid, opno, f.path);
3321 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3324 add_to_flist(FT_DEV, id, parid);
3326 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3327 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3332 #ifdef HAVE_SYS_MMAN_H
3333 struct print_flags mmap_flags[] = {
3334 { MAP_SHARED, "SHARED"},
3335 { MAP_PRIVATE, "PRIVATE"},
3339 #define translate_mmap_flags(flags) \
3340 ({translate_flags(flags, "|", mmap_flags);})
3344 do_mmap(int opno, long r, int prot)
3346 #ifdef HAVE_SYS_MMAN_H
3358 sigjmp_buf sigbus_jmpbuf;
3361 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3363 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3367 fd = open_path(&f, O_RDWR);
3368 e = fd < 0 ? errno : 0;
3372 printf("%d/%d: do_mmap - open %s failed %d\n",
3373 procid, opno, f.path, e);
3377 if (fstat64(fd, &stb) < 0) {
3379 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3380 procid, opno, f.path, errno);
3385 inode_info(st, sizeof(st), &stb, v);
3386 if (stb.st_size == 0) {
3388 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3395 lr = ((__int64_t)random() << 32) + random();
3396 off = (off64_t)(lr % stb.st_size);
3397 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3398 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3400 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3401 addr = mmap(NULL, len, prot, flags, fd, off);
3402 e = (addr == MAP_FAILED) ? errno : 0;
3405 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3406 procid, opno, f.path, st, (long long)off,
3407 (int)len, translate_mmap_flags(flags), e);
3413 if (prot & PROT_WRITE) {
3414 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3415 sigbus_jmp = &sigbus_jmpbuf;
3416 memset(addr, nameseq & 0xff, len);
3420 if ((buf = malloc(len)) != NULL) {
3421 memcpy(buf, addr, len);
3426 /* set NULL to stop other functions from doing siglongjmp */
3430 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3431 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3432 f.path, st, (long long)off, (int)len,
3433 translate_mmap_flags(flags),
3434 (e == 0) ? "0" : "Bus error");
3442 mread_f(int opno, long r)
3444 #ifdef HAVE_SYS_MMAN_H
3445 do_mmap(opno, r, PROT_READ);
3450 mwrite_f(int opno, long r)
3452 #ifdef HAVE_SYS_MMAN_H
3453 do_mmap(opno, r, PROT_WRITE);
3458 punch_f(int opno, long r)
3460 #ifdef HAVE_LINUX_FALLOC_H
3461 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3466 zero_f(int opno, long r)
3468 #ifdef HAVE_LINUX_FALLOC_H
3469 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3474 collapse_f(int opno, long r)
3476 #ifdef HAVE_LINUX_FALLOC_H
3477 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3482 insert_f(int opno, long r)
3484 #ifdef HAVE_LINUX_FALLOC_H
3485 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3490 read_f(int opno, long r)
3504 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3506 printf("%d/%d: read - no filename\n", procid, opno);
3510 fd = open_path(&f, O_RDONLY);
3511 e = fd < 0 ? errno : 0;
3515 printf("%d/%d: read - open %s failed %d\n",
3516 procid, opno, f.path, e);
3520 if (fstat64(fd, &stb) < 0) {
3522 printf("%d/%d: read - fstat64 %s failed %d\n",
3523 procid, opno, f.path, errno);
3528 inode_info(st, sizeof(st), &stb, v);
3529 if (stb.st_size == 0) {
3531 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3537 lr = ((__int64_t)random() << 32) + random();
3538 off = (off64_t)(lr % stb.st_size);
3539 lseek64(fd, off, SEEK_SET);
3540 len = (random() % FILELEN_MAX) + 1;
3542 e = read(fd, buf, len) < 0 ? errno : 0;
3545 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3546 procid, opno, f.path, st, (long long)off, (int)len, e);
3552 readlink_f(int opno, long r)
3560 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3562 printf("%d/%d: readlink - no filename\n", procid, opno);
3566 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3569 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3574 readv_f(int opno, long r)
3586 struct iovec *iov = NULL;
3593 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3595 printf("%d/%d: readv - no filename\n", procid, opno);
3599 fd = open_path(&f, O_RDONLY);
3600 e = fd < 0 ? errno : 0;
3604 printf("%d/%d: readv - open %s failed %d\n",
3605 procid, opno, f.path, e);
3609 if (fstat64(fd, &stb) < 0) {
3611 printf("%d/%d: readv - fstat64 %s failed %d\n",
3612 procid, opno, f.path, errno);
3617 inode_info(st, sizeof(st), &stb, v);
3618 if (stb.st_size == 0) {
3620 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3626 lr = ((__int64_t)random() << 32) + random();
3627 off = (off64_t)(lr % stb.st_size);
3628 lseek64(fd, off, SEEK_SET);
3629 len = (random() % FILELEN_MAX) + 1;
3632 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
3633 iov = calloc(iovcnt, sizeof(struct iovec));
3634 iovl = len / iovcnt;
3636 for (i=0; i<iovcnt; i++) {
3637 (iov + i)->iov_base = (buf + iovb);
3638 (iov + i)->iov_len = iovl;
3642 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
3645 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
3646 procid, opno, f.path, st, (long long)off, (int)iovl,
3653 rename_f(int opno, long r)
3667 /* get an existing path for the source of the rename */
3669 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
3671 printf("%d/%d: rename - no filename\n", procid, opno);
3676 /* get an existing directory for the destination parent directory name */
3677 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
3683 /* generate a new path using an existing parent directory in name */
3684 init_pathname(&newf);
3685 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
3689 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
3690 printf("%d/%d: rename - no filename from %s\n",
3691 procid, opno, f.path);
3693 free_pathname(&newf);
3697 e = rename_path(&f, &newf) < 0 ? errno : 0;
3700 if (flp - flist == FT_DIR) {
3702 fix_parent(oldid, id);
3704 del_from_flist(flp - flist, fep - flp->fents);
3705 add_to_flist(flp - flist, id, parid);
3708 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3711 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3712 procid, opno, fep->id, fep->parent);
3713 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3714 procid, opno, id, parid);
3717 free_pathname(&newf);
3722 resvsp_f(int opno, long r)
3727 struct xfs_flock64 fl;
3735 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3737 printf("%d/%d: resvsp - no filename\n", procid, opno);
3741 fd = open_path(&f, O_RDWR);
3742 e = fd < 0 ? errno : 0;
3746 printf("%d/%d: resvsp - open %s failed %d\n",
3747 procid, opno, f.path, e);
3751 if (fstat64(fd, &stb) < 0) {
3753 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3754 procid, opno, f.path, errno);
3759 inode_info(st, sizeof(st), &stb, v);
3760 lr = ((__int64_t)random() << 32) + random();
3761 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3763 fl.l_whence = SEEK_SET;
3765 fl.l_len = (off64_t)(random() % (1024 * 1024));
3766 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3768 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3769 procid, opno, f.path, st,
3770 (long long)off, (long long)fl.l_len, e);
3776 rmdir_f(int opno, long r)
3784 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3786 printf("%d/%d: rmdir - no directory\n", procid, opno);
3790 e = rmdir_path(&f) < 0 ? errno : 0;
3793 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3795 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3797 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3798 procid, opno, fep->id, fep->parent);
3804 setattr_f(int opno, long r)
3813 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3814 append_pathname(&f, ".");
3815 fd = open_path(&f, O_RDWR);
3816 e = fd < 0 ? errno : 0;
3819 fl = attr_mask & (uint)random();
3820 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3822 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3828 stat_f(int opno, long r)
3836 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3838 printf("%d/%d: stat - no entries\n", procid, opno);
3842 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3845 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3850 symlink_f(int opno, long r)
3863 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3868 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3872 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3873 printf("%d/%d: symlink - no filename from %s\n",
3874 procid, opno, f.path);
3879 len = (int)(random() % PATH_MAX);
3880 val = malloc(len + 1);
3882 memset(val, 'x', len);
3884 for (i = 10; i < len - 1; i += 10)
3886 e = symlink_path(val, &f) < 0 ? errno : 0;
3889 add_to_flist(FT_SYM, id, parid);
3892 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3893 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3900 sync_f(int opno, long r)
3904 printf("%d/%d: sync\n", procid, opno);
3908 truncate_f(int opno, long r)
3919 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3921 printf("%d/%d: truncate - no filename\n", procid, opno);
3925 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3929 printf("%d/%d: truncate - stat64 %s failed %d\n",
3930 procid, opno, f.path, e);
3934 inode_info(st, sizeof(st), &stb, v);
3935 lr = ((__int64_t)random() << 32) + random();
3936 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3938 e = truncate64_path(&f, off) < 0 ? errno : 0;
3941 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3942 st, (long long)off, e);
3947 unlink_f(int opno, long r)
3956 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3958 printf("%d/%d: unlink - no file\n", procid, opno);
3962 e = unlink_path(&f) < 0 ? errno : 0;
3965 del_from_flist(flp - flist, fep - flp->fents);
3967 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3969 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3970 procid, opno, fep->id, fep->parent);
3976 unresvsp_f(int opno, long r)
3981 struct xfs_flock64 fl;
3989 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3991 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3995 fd = open_path(&f, O_RDWR);
3996 e = fd < 0 ? errno : 0;
4000 printf("%d/%d: unresvsp - open %s failed %d\n",
4001 procid, opno, f.path, e);
4005 if (fstat64(fd, &stb) < 0) {
4007 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4008 procid, opno, f.path, errno);
4013 inode_info(st, sizeof(st), &stb, v);
4014 lr = ((__int64_t)random() << 32) + random();
4015 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4017 fl.l_whence = SEEK_SET;
4019 fl.l_len = (off64_t)(random() % (1 << 20));
4020 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4022 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4023 procid, opno, f.path, st,
4024 (long long)off, (long long)fl.l_len, e);
4030 write_f(int opno, long r)
4044 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4046 printf("%d/%d: write - no filename\n", procid, opno);
4050 fd = open_path(&f, O_WRONLY);
4051 e = fd < 0 ? errno : 0;
4055 printf("%d/%d: write - open %s failed %d\n",
4056 procid, opno, f.path, e);
4060 if (fstat64(fd, &stb) < 0) {
4062 printf("%d/%d: write - fstat64 %s failed %d\n",
4063 procid, opno, f.path, errno);
4068 inode_info(st, sizeof(st), &stb, v);
4069 lr = ((__int64_t)random() << 32) + random();
4070 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4072 lseek64(fd, off, SEEK_SET);
4073 len = (random() % FILELEN_MAX) + 1;
4075 memset(buf, nameseq & 0xff, len);
4076 e = write(fd, buf, len) < 0 ? errno : 0;
4079 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4080 procid, opno, f.path, st, (long long)off, (int)len, e);
4086 writev_f(int opno, long r)
4098 struct iovec *iov = NULL;
4105 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4107 printf("%d/%d: writev - no filename\n", procid, opno);
4111 fd = open_path(&f, O_WRONLY);
4112 e = fd < 0 ? errno : 0;
4116 printf("%d/%d: writev - open %s failed %d\n",
4117 procid, opno, f.path, e);
4121 if (fstat64(fd, &stb) < 0) {
4123 printf("%d/%d: writev - fstat64 %s failed %d\n",
4124 procid, opno, f.path, errno);
4129 inode_info(st, sizeof(st), &stb, v);
4130 lr = ((__int64_t)random() << 32) + random();
4131 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4133 lseek64(fd, off, SEEK_SET);
4134 len = (random() % FILELEN_MAX) + 1;
4136 memset(buf, nameseq & 0xff, len);
4138 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4139 iov = calloc(iovcnt, sizeof(struct iovec));
4140 iovl = len / iovcnt;
4142 for (i=0; i<iovcnt; i++) {
4143 (iov + i)->iov_base = (buf + iovb);
4144 (iov + i)->iov_len = iovl;
4148 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4152 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4153 procid, opno, f.path, st, (long long)off, (int)iovl,