fsstress: add the ability to create/delete subvolumes
[xfstests-dev.git] / ltp / fsstress.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <linux/fs.h>
8 #include <setjmp.h>
9 #include <sys/uio.h>
10 #include <stddef.h>
11 #include "global.h"
12
13 #ifdef HAVE_BTRFSUTIL_H
14 #include <btrfsutil.h>
15 #endif
16 #ifdef HAVE_ATTR_ATTRIBUTES_H
17 #include <attr/attributes.h>
18 #endif
19 #ifdef HAVE_LINUX_FIEMAP_H
20 #include <linux/fiemap.h>
21 #endif
22 #ifndef HAVE_ATTR_LIST
23 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
24 #endif
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
27 #endif
28 #ifdef AIO
29 #include <libaio.h>
30 io_context_t    io_ctx;
31 #endif
32 #include <sys/syscall.h>
33 #include <sys/xattr.h>
34
35 #ifndef FS_IOC_GETFLAGS
36 #define FS_IOC_GETFLAGS                 _IOR('f', 1, long)
37 #endif
38 #ifndef FS_IOC_SETFLAGS
39 #define FS_IOC_SETFLAGS                 _IOW('f', 2, long)
40 #endif
41
42 #include <math.h>
43 #define XFS_ERRTAG_MAX          17
44 #define XFS_IDMODULO_MAX        31      /* user/group IDs (1 << x)  */
45 #define XFS_PROJIDMODULO_MAX    16      /* project IDs (1 << x)     */
46 #ifndef IOV_MAX
47 #define IOV_MAX 1024
48 #endif
49
50 #ifndef HAVE_RENAMEAT2
51 #if !defined(SYS_renameat2) && defined(__x86_64__)
52 #define SYS_renameat2 316
53 #endif
54
55 #if !defined(SYS_renameat2) && defined(__i386__)
56 #define SYS_renameat2 353
57 #endif
58
59 static int renameat2(int dfd1, const char *path1,
60                      int dfd2, const char *path2,
61                      unsigned int flags)
62 {
63 #ifdef SYS_renameat2
64         return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
65 #else
66         errno = ENOSYS;
67         return -1;
68 #endif
69 }
70 #endif
71
72 #ifndef RENAME_NOREPLACE
73 #define RENAME_NOREPLACE        (1 << 0)        /* Don't overwrite target */
74 #endif
75 #ifndef RENAME_EXCHANGE
76 #define RENAME_EXCHANGE         (1 << 1)        /* Exchange source and dest */
77 #endif
78 #ifndef RENAME_WHITEOUT
79 #define RENAME_WHITEOUT         (1 << 2)        /* Whiteout source */
80 #endif
81
82 #define FILELEN_MAX             (32*4096)
83
84 typedef enum {
85         OP_AFSYNC,
86         OP_ALLOCSP,
87         OP_AREAD,
88         OP_ATTR_REMOVE,
89         OP_ATTR_SET,
90         OP_AWRITE,
91         OP_BULKSTAT,
92         OP_BULKSTAT1,
93         OP_CHOWN,
94         OP_CLONERANGE,
95         OP_COPYRANGE,
96         OP_CREAT,
97         OP_DEDUPERANGE,
98         OP_DREAD,
99         OP_DWRITE,
100         OP_FALLOCATE,
101         OP_FDATASYNC,
102         OP_FIEMAP,
103         OP_FREESP,
104         OP_FSYNC,
105         OP_GETATTR,
106         OP_GETDENTS,
107         OP_GETFATTR,
108         OP_LINK,
109         OP_LISTFATTR,
110         OP_MKDIR,
111         OP_MKNOD,
112         OP_MREAD,
113         OP_MWRITE,
114         OP_PUNCH,
115         OP_ZERO,
116         OP_COLLAPSE,
117         OP_INSERT,
118         OP_READ,
119         OP_READLINK,
120         OP_READV,
121         OP_REMOVEFATTR,
122         OP_RENAME,
123         OP_RNOREPLACE,
124         OP_REXCHANGE,
125         OP_RWHITEOUT,
126         OP_RESVSP,
127         OP_RMDIR,
128         OP_SETATTR,
129         OP_SETFATTR,
130         OP_SETXATTR,
131         OP_SPLICE,
132         OP_STAT,
133         OP_SUBVOL_CREATE,
134         OP_SUBVOL_DELETE,
135         OP_SYMLINK,
136         OP_SYNC,
137         OP_TRUNCATE,
138         OP_UNLINK,
139         OP_UNRESVSP,
140         OP_WRITE,
141         OP_WRITEV,
142         OP_LAST
143 } opty_t;
144
145 typedef void (*opfnc_t)(int, long);
146
147 typedef struct opdesc {
148         opty_t  op;
149         char    *name;
150         opfnc_t func;
151         int     freq;
152         int     iswrite;
153 } opdesc_t;
154
155 typedef struct fent {
156         int     id;
157         int     ft;
158         int     parent;
159         int     xattr_counter;
160 } fent_t;
161
162 typedef struct flist {
163         int     nfiles;
164         int     nslots;
165         int     tag;
166         fent_t  *fents;
167 } flist_t;
168
169 typedef struct pathname {
170         int     len;
171         char    *path;
172 } pathname_t;
173
174 struct print_flags {
175         unsigned long mask;
176         const char *name;
177 };
178
179 struct print_string {
180         char *buffer;
181         int len;
182         int max;
183 };
184
185 #define FT_DIR          0
186 #define FT_DIRm         (1 << FT_DIR)
187 #define FT_REG          1
188 #define FT_REGm         (1 << FT_REG)
189 #define FT_SYM          2
190 #define FT_SYMm         (1 << FT_SYM)
191 #define FT_DEV          3
192 #define FT_DEVm         (1 << FT_DEV)
193 #define FT_RTF          4
194 #define FT_RTFm         (1 << FT_RTF)
195 #define FT_SUBVOL       5
196 #define FT_SUBVOLm      (1 << FT_SUBVOL)
197 #define FT_nft          6
198 #define FT_ANYm         ((1 << FT_nft) - 1)
199 #define FT_REGFILE      (FT_REGm | FT_RTFm)
200 #define FT_NOTDIR       (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
201
202 #define FLIST_SLOT_INCR 16
203 #define NDCACHE 64
204
205 #define MAXFSIZE        ((1ULL << 63) - 1ULL)
206 #define MAXFSIZE32      ((1ULL << 40) - 1ULL)
207
208 #define XATTR_NAME_BUF_SIZE 18
209
210 void    afsync_f(int, long);
211 void    allocsp_f(int, long);
212 void    aread_f(int, long);
213 void    attr_remove_f(int, long);
214 void    attr_set_f(int, long);
215 void    awrite_f(int, long);
216 void    bulkstat_f(int, long);
217 void    bulkstat1_f(int, long);
218 void    chown_f(int, long);
219 void    clonerange_f(int, long);
220 void    copyrange_f(int, long);
221 void    creat_f(int, long);
222 void    deduperange_f(int, long);
223 void    dread_f(int, long);
224 void    dwrite_f(int, long);
225 void    fallocate_f(int, long);
226 void    fdatasync_f(int, long);
227 void    fiemap_f(int, long);
228 void    freesp_f(int, long);
229 void    fsync_f(int, long);
230 char    *gen_random_string(int);
231 void    getattr_f(int, long);
232 void    getdents_f(int, long);
233 void    getfattr_f(int, long);
234 void    link_f(int, long);
235 void    listfattr_f(int, long);
236 void    mkdir_f(int, long);
237 void    mknod_f(int, long);
238 void    mread_f(int, long);
239 void    mwrite_f(int, long);
240 void    punch_f(int, long);
241 void    zero_f(int, long);
242 void    collapse_f(int, long);
243 void    insert_f(int, long);
244 void    read_f(int, long);
245 void    readlink_f(int, long);
246 void    readv_f(int, long);
247 void    removefattr_f(int, long);
248 void    rename_f(int, long);
249 void    rnoreplace_f(int, long);
250 void    rexchange_f(int, long);
251 void    rwhiteout_f(int, long);
252 void    resvsp_f(int, long);
253 void    rmdir_f(int, long);
254 void    setattr_f(int, long);
255 void    setfattr_f(int, long);
256 void    setxattr_f(int, long);
257 void    splice_f(int, long);
258 void    stat_f(int, long);
259 void    subvol_create_f(int, long);
260 void    subvol_delete_f(int, long);
261 void    symlink_f(int, long);
262 void    sync_f(int, long);
263 void    truncate_f(int, long);
264 void    unlink_f(int, long);
265 void    unresvsp_f(int, long);
266 void    write_f(int, long);
267 void    writev_f(int, long);
268 char    *xattr_flag_to_string(int);
269
270 opdesc_t        ops[] = {
271      /* { OP_ENUM, "name", function, freq, iswrite }, */
272         { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
273         { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
274         { OP_AREAD, "aread", aread_f, 1, 0 },
275         { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
276         { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
277         { OP_AWRITE, "awrite", awrite_f, 1, 1 },
278         { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
279         { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
280         { OP_CHOWN, "chown", chown_f, 3, 1 },
281         { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
282         { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
283         { OP_CREAT, "creat", creat_f, 4, 1 },
284         { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
285         { OP_DREAD, "dread", dread_f, 4, 0 },
286         { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
287         { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
288         { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
289         { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
290         { OP_FREESP, "freesp", freesp_f, 1, 1 },
291         { OP_FSYNC, "fsync", fsync_f, 1, 1 },
292         { OP_GETATTR, "getattr", getattr_f, 1, 0 },
293         { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
294         /* get extended attribute */
295         { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
296         { OP_LINK, "link", link_f, 1, 1 },
297         /* list extent attributes */
298         { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
299         { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
300         { OP_MKNOD, "mknod", mknod_f, 2, 1 },
301         { OP_MREAD, "mread", mread_f, 2, 0 },
302         { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
303         { OP_PUNCH, "punch", punch_f, 1, 1 },
304         { OP_ZERO, "zero", zero_f, 1, 1 },
305         { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
306         { OP_INSERT, "insert", insert_f, 1, 1 },
307         { OP_READ, "read", read_f, 1, 0 },
308         { OP_READLINK, "readlink", readlink_f, 1, 0 },
309         { OP_READV, "readv", readv_f, 1, 0 },
310         /* remove (delete) extended attribute */
311         { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
312         { OP_RENAME, "rename", rename_f, 2, 1 },
313         { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
314         { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
315         { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
316         { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
317         { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
318         /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
319         { OP_SETATTR, "setattr", setattr_f, 0, 1 },
320         /* set extended attribute */
321         { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
322         /* set project id (XFS_IOC_FSSETXATTR ioctl) */
323         { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
324         { OP_SPLICE, "splice", splice_f, 1, 1 },
325         { OP_STAT, "stat", stat_f, 1, 0 },
326         { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
327         { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
328         { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
329         { OP_SYNC, "sync", sync_f, 1, 1 },
330         { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
331         { OP_UNLINK, "unlink", unlink_f, 1, 1 },
332         { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
333         { OP_WRITE, "write", write_f, 4, 1 },
334         { OP_WRITEV, "writev", writev_f, 4, 1 },
335 }, *ops_end;
336
337 flist_t flist[FT_nft] = {
338         { 0, 0, 'd', NULL },
339         { 0, 0, 'f', NULL },
340         { 0, 0, 'l', NULL },
341         { 0, 0, 'c', NULL },
342         { 0, 0, 'r', NULL },
343         { 0, 0, 's', NULL },
344 };
345
346 int             dcache[NDCACHE];
347 int             errrange;
348 int             errtag;
349 opty_t          *freq_table;
350 int             freq_table_size;
351 struct xfs_fsop_geom    geom;
352 char            *homedir;
353 int             *ilist;
354 int             ilistlen;
355 off64_t         maxfsize;
356 char            *myprog;
357 int             namerand;
358 int             nameseq;
359 int             nops;
360 int             nproc = 1;
361 int             operations = 1;
362 unsigned int    idmodulo = XFS_IDMODULO_MAX;
363 unsigned int    attr_mask = ~0;
364 int             procid;
365 int             rtpct;
366 unsigned long   seed = 0;
367 ino_t           top_ino;
368 int             cleanup = 0;
369 int             verbose = 0;
370 int             verifiable_log = 0;
371 sig_atomic_t    should_stop = 0;
372 sigjmp_buf      *sigbus_jmp = NULL;
373 char            *execute_cmd = NULL;
374 int             execute_freq = 1;
375 struct print_string     flag_str = {0};
376
377 void    add_to_flist(int, int, int, int);
378 void    append_pathname(pathname_t *, char *);
379 int     attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
380 int     attr_remove_path(pathname_t *, const char *, int);
381 int     attr_set_path(pathname_t *, const char *, const char *, const int, int);
382 void    check_cwd(void);
383 void    cleanup_flist(void);
384 int     creat_path(pathname_t *, mode_t);
385 void    dcache_enter(int, int);
386 void    dcache_init(void);
387 fent_t  *dcache_lookup(int);
388 void    dcache_purge(int, int);
389 void    del_from_flist(int, int);
390 int     dirid_to_name(char *, int);
391 void    doproc(void);
392 int     fent_to_name(pathname_t *, fent_t *);
393 bool    fents_ancestor_check(fent_t *, fent_t *);
394 void    fix_parent(int, int, bool);
395 void    free_pathname(pathname_t *);
396 int     generate_fname(fent_t *, int, pathname_t *, int *, int *);
397 int     generate_xattr_name(int, char *, int);
398 int     get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
399 void    init_pathname(pathname_t *);
400 int     lchown_path(pathname_t *, uid_t, gid_t);
401 int     link_path(pathname_t *, pathname_t *);
402 int     lstat64_path(pathname_t *, struct stat64 *);
403 void    make_freq_table(void);
404 int     mkdir_path(pathname_t *, mode_t);
405 int     mknod_path(pathname_t *, mode_t, dev_t);
406 void    namerandpad(int, char *, int);
407 int     open_file_or_dir(pathname_t *, int);
408 int     open_path(pathname_t *, int);
409 DIR     *opendir_path(pathname_t *);
410 void    process_freq(char *);
411 int     readlink_path(pathname_t *, char *, size_t);
412 int     rename_path(pathname_t *, pathname_t *, int);
413 int     rmdir_path(pathname_t *);
414 void    separate_pathname(pathname_t *, char *, pathname_t *);
415 void    show_ops(int, char *);
416 int     stat64_path(pathname_t *, struct stat64 *);
417 int     symlink_path(const char *, pathname_t *);
418 int     truncate64_path(pathname_t *, off64_t);
419 int     unlink_path(pathname_t *);
420 void    usage(void);
421 void    write_freq(void);
422 void    zero_freq(void);
423 void    non_btrfs_freq(const char *);
424
425 void sg_handler(int signum)
426 {
427         switch (signum) {
428         case SIGTERM:
429                 should_stop = 1;
430                 break;
431         case SIGBUS:
432                 /*
433                  * Only handle SIGBUS when mmap write to a hole and no
434                  * block can be allocated due to ENOSPC, abort otherwise.
435                  */
436                 if (sigbus_jmp) {
437                         siglongjmp(*sigbus_jmp, -1);
438                 } else {
439                         printf("Unknown SIGBUS is caught, Abort!\n");
440                         abort();
441                 }
442                 /* should not reach here */
443                 break;
444         default:
445                 break;
446         }
447 }
448
449 int main(int argc, char **argv)
450 {
451         char            buf[10];
452         int             c;
453         char            *dirname = NULL;
454         char            *logname = NULL;
455         char            rpath[PATH_MAX];
456         int             fd;
457         int             i;
458         int             j;
459         char            *p;
460         int             stat;
461         struct timeval  t;
462         ptrdiff_t       srval;
463         int             nousage = 0;
464         xfs_error_injection_t           err_inj;
465         struct sigaction action;
466         int             loops = 1;
467         const char      *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
468
469         errrange = errtag = 0;
470         umask(0);
471         nops = sizeof(ops) / sizeof(ops[0]);
472         ops_end = &ops[nops];
473         myprog = argv[0];
474         while ((c = getopt(argc, argv, allopts)) != -1) {
475                 switch (c) {
476                 case 'c':
477                         cleanup = 1;
478                         break;
479                 case 'd':
480                         dirname = optarg;
481                         break;
482                 case 'e':
483                         sscanf(optarg, "%d", &errtag);
484                         if (errtag < 0) {
485                                 errtag = -errtag;
486                                 errrange = 1;
487                         } else if (errtag == 0)
488                                 errtag = -1;
489                         if (errtag >= XFS_ERRTAG_MAX) {
490                                 fprintf(stderr,
491                                         "error tag %d too large (max %d)\n",
492                                         errtag, XFS_ERRTAG_MAX - 1);
493                                 exit(1);
494                         }
495                         break;
496                 case 'f':
497                         process_freq(optarg);
498                         break;
499                 case 'i':
500                         ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
501                         ilist[ilistlen - 1] = strtol(optarg, &p, 16);
502                         break;
503                 case 'm':
504                         idmodulo = strtoul(optarg, NULL, 0);
505                         if (idmodulo > XFS_IDMODULO_MAX) {
506                                 fprintf(stderr,
507                                         "chown modulo %d too big (max %d)\n",
508                                         idmodulo, XFS_IDMODULO_MAX);
509                                 exit(1);
510                         }
511                         break;
512                 case 'l':
513                         loops = atoi(optarg);
514                         break;
515                 case 'n':
516                         operations = atoi(optarg);
517                         break;
518                 case 'o':
519                         logname = optarg;
520                         break;
521
522                 case 'p':
523                         nproc = atoi(optarg);
524                         break;
525                 case 'r':
526                         namerand = 1;
527                         break;
528                 case 's':
529                         seed = strtoul(optarg, NULL, 0);
530                         break;
531                 case 'v':
532                         verbose = 1;
533                         break;
534                 case 'w':
535                         write_freq();
536                         break;
537                 case 'x':
538                         execute_cmd = optarg;
539                         break;
540                 case 'z':
541                         zero_freq();
542                         break;
543                 case 'M':
544                         attr_mask = strtoul(optarg, NULL, 0);
545                         break;
546                 case 'S':
547                         i = 0;
548                         if (optarg[0] == 'c')
549                                 i = 1;
550                         show_ops(i, NULL);
551                         printf("\n");
552                         nousage=1;
553                         break;
554                 case 'V':
555                         verifiable_log = 1;
556                         break;
557
558                 case 'X':
559                         execute_freq = strtoul(optarg, NULL, 0);
560                         break;
561                 case '?':
562                         fprintf(stderr, "%s - invalid parameters\n",
563                                 myprog);
564                         /* fall through */
565                 case 'H':
566                         usage();
567                         exit(1);
568                 }
569         }
570
571         if (!dirname) {
572             /* no directory specified */
573             if (!nousage) usage();
574             exit(1);
575         }
576
577         non_btrfs_freq(dirname);
578         (void)mkdir(dirname, 0777);
579         if (logname && logname[0] != '/') {
580                 if (!getcwd(rpath, sizeof(rpath))){
581                         perror("getcwd failed");
582                         exit(1);
583                 }
584         } else {
585                 rpath[0] = '\0';
586         }
587         if (chdir(dirname) < 0) {
588                 perror(dirname);
589                 exit(1);
590         }
591         if (logname) {
592                 char path[PATH_MAX + NAME_MAX + 1];
593                 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
594                 if (freopen(path, "a", stdout) == NULL) {
595                         perror("freopen logfile failed");
596                         exit(1);
597                 }
598         }
599         sprintf(buf, "fss%x", (unsigned int)getpid());
600         fd = creat(buf, 0666);
601         if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
602                 maxfsize = (off64_t)MAXFSIZE32;
603         else
604                 maxfsize = (off64_t)MAXFSIZE;
605         make_freq_table();
606         dcache_init();
607         setlinebuf(stdout);
608         if (!seed) {
609                 gettimeofday(&t, (void *)NULL);
610                 seed = (int)t.tv_sec ^ (int)t.tv_usec;
611                 printf("seed = %ld\n", seed);
612         }
613         i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
614         if (i >= 0 && geom.rtblocks)
615                 rtpct = MIN(MAX(geom.rtblocks * 100 /
616                                 (geom.rtblocks + geom.datablocks), 1), 99);
617         else
618                 rtpct = 0;
619         if (errtag != 0) {
620                 if (errrange == 0) {
621                         if (errtag <= 0) {
622                                 srandom(seed);
623                                 j = random() % 100;
624
625                                 for (i = 0; i < j; i++)
626                                         (void) random();
627
628                                 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
629                         }
630                 } else {
631                         srandom(seed);
632                         j = random() % 100;
633
634                         for (i = 0; i < j; i++)
635                                 (void) random();
636
637                         errtag += (random() % (XFS_ERRTAG_MAX - errtag));
638                 }
639                 printf("Injecting failure on tag #%d\n", errtag);
640                 err_inj.errtag = errtag;
641                 err_inj.fd = fd;
642                 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
643                 if (srval < -1) {
644                         perror("fsstress - XFS_SYSSGI error injection call");
645                         close(fd);
646                         unlink(buf);
647                         exit(1);
648                 }
649         } else
650                 close(fd);
651
652         setpgid(0, 0);
653         action.sa_handler = sg_handler;
654         sigemptyset(&action.sa_mask);
655         action.sa_flags = 0;
656         if (sigaction(SIGTERM, &action, 0)) {
657                 perror("sigaction failed");
658                 exit(1);
659         }
660
661         for (i = 0; i < nproc; i++) {
662                 if (fork() == 0) {
663                         sigemptyset(&action.sa_mask);
664                         action.sa_handler = SIG_DFL;
665                         if (sigaction(SIGTERM, &action, 0))
666                                 return 1;
667                         action.sa_handler = sg_handler;
668                         if (sigaction(SIGBUS, &action, 0))
669                                 return 1;
670 #ifdef HAVE_SYS_PRCTL_H
671                         prctl(PR_SET_PDEATHSIG, SIGKILL);
672                         if (getppid() == 1) /* parent died already? */
673                                 return 0;
674 #endif
675                         if (logname) {
676                                 char path[PATH_MAX + NAME_MAX + 2 + 11];
677                                 snprintf(path, sizeof(path), "%s/%s.%d",
678                                          rpath, logname, i);
679                                 if (freopen(path, "a", stdout) == NULL) {
680                                         perror("freopen logfile failed");
681                                         exit(1);
682                                 }
683                         }
684                         procid = i;
685 #ifdef AIO
686                         if (io_setup(128, &io_ctx) != 0) {
687                                 fprintf(stderr, "io_setup failed");
688                                 exit(1);
689                         }
690 #endif
691                         for (i = 0; !loops || (i < loops); i++)
692                                 doproc();
693 #ifdef AIO
694                         if(io_destroy(io_ctx) != 0) {
695                                 fprintf(stderr, "io_destroy failed");
696                                 return 1;
697                         }
698 #endif
699
700                         cleanup_flist();
701                         free(freq_table);
702                         return 0;
703                 }
704         }
705         while (wait(&stat) > 0 && !should_stop) {
706                 continue;
707         }
708         action.sa_flags = SA_RESTART;
709         sigaction(SIGTERM, &action, 0);
710         kill(-getpid(), SIGTERM);
711         while (wait(&stat) > 0)
712                 continue;
713
714         if (errtag != 0) {
715                 err_inj.errtag = 0;
716                 err_inj.fd = fd;
717                 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
718                 if (srval != 0) {
719                         fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
720                                 buf, fd, errno);
721                         perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
722                         close(fd);
723                         exit(1);
724                 }
725                 close(fd);
726         }
727
728         free(freq_table);
729         unlink(buf);
730         return 0;
731 }
732
733 int
734 add_string(struct print_string *str, const char *add)
735 {
736         int len = strlen(add);
737
738         if (len <= 0)
739                 return 0;
740
741         if (len > (str->max - 1) - str->len) {
742                 str->len = str->max - 1;
743                 return 0;
744         }
745
746         memcpy(str->buffer + str->len, add, len);
747         str->len += len;
748         str->buffer[str->len] = '\0';
749
750         return len;
751 }
752
753 char *
754 translate_flags(int flags, const char *delim,
755                 const struct print_flags *flag_array)
756 {
757         int i, mask, first = 1;
758         const char *add;
759
760         if (!flag_str.buffer) {
761                 flag_str.buffer = malloc(4096);
762                 flag_str.max = 4096;
763                 flag_str.len = 0;
764         }
765         if (!flag_str.buffer)
766                 return NULL;
767         flag_str.len = 0;
768         flag_str.buffer[0] = '\0';
769
770         for (i = 0;  flag_array[i].name && flags; i++) {
771                 mask = flag_array[i].mask;
772                 if ((flags & mask) != mask)
773                         continue;
774
775                 add = flag_array[i].name;
776                 flags &= ~mask;
777                 if (!first && delim)
778                         add_string(&flag_str, delim);
779                 else
780                         first = 0;
781                 add_string(&flag_str, add);
782         }
783
784         /* Check whether there are any leftover flags. */
785         if (flags) {
786                 int ret;
787                 char number[11];
788
789                 if (!first && delim)
790                         add_string(&flag_str, delim);
791
792                 ret = snprintf(number, 11, "0x%x", flags) > 0;
793                 if (ret > 0 && ret <= 11)
794                         add_string(&flag_str, number);
795         }
796
797         return flag_str.buffer;
798 }
799
800 void
801 add_to_flist(int ft, int id, int parent, int xattr_counter)
802 {
803         fent_t  *fep;
804         flist_t *ftp;
805
806         ftp = &flist[ft];
807         if (ftp->nfiles == ftp->nslots) {
808                 ftp->nslots += FLIST_SLOT_INCR;
809                 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
810         }
811         fep = &ftp->fents[ftp->nfiles++];
812         fep->id = id;
813         fep->ft = ft;
814         fep->parent = parent;
815         fep->xattr_counter = xattr_counter;
816 }
817
818 void
819 append_pathname(pathname_t *name, char *str)
820 {
821         int     len;
822
823         len = strlen(str);
824 #ifdef DEBUG
825         /* attempting to append to a dir a zero length path */
826         if (len && *str == '/' && name->len == 0) {
827                 fprintf(stderr, "fsstress: append_pathname failure\n");
828                 assert(chdir(homedir) == 0);
829                 abort();
830                 /* NOTREACHED */
831         }
832 #endif
833         name->path = realloc(name->path, name->len + 1 + len);
834         strcpy(&name->path[name->len], str);
835         name->len += len;
836 }
837
838 int
839 attr_list_path(pathname_t *name,
840                char *buffer,
841                const int buffersize,
842                int flags,
843                attrlist_cursor_t *cursor)
844 {
845         char            buf[NAME_MAX + 1];
846         pathname_t      newname;
847         int             rval;
848
849         if (flags != ATTR_DONTFOLLOW) {
850                 errno = EINVAL;
851                 return -1;
852         }
853
854         rval = attr_list(name->path, buffer, buffersize, flags, cursor);
855         if (rval >= 0 || errno != ENAMETOOLONG)
856                 return rval;
857         separate_pathname(name, buf, &newname);
858         if (chdir(buf) == 0) {
859                 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
860                 assert(chdir("..") == 0);
861         }
862         free_pathname(&newname);
863         return rval;
864 }
865
866 int
867 attr_remove_path(pathname_t *name, const char *attrname, int flags)
868 {
869         char            buf[NAME_MAX + 1];
870         pathname_t      newname;
871         int             rval;
872
873         rval = attr_remove(name->path, attrname, flags);
874         if (rval >= 0 || errno != ENAMETOOLONG)
875                 return rval;
876         separate_pathname(name, buf, &newname);
877         if (chdir(buf) == 0) {
878                 rval = attr_remove_path(&newname, attrname, flags);
879                 assert(chdir("..") == 0);
880         }
881         free_pathname(&newname);
882         return rval;
883 }
884
885 int
886 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
887               const int valuelength, int flags)
888 {
889         char            buf[NAME_MAX + 1];
890         pathname_t      newname;
891         int             rval;
892
893         rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
894         if (rval >= 0 || errno != ENAMETOOLONG)
895                 return rval;
896         separate_pathname(name, buf, &newname);
897         if (chdir(buf) == 0) {
898                 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
899                         flags);
900                 assert(chdir("..") == 0);
901         }
902         free_pathname(&newname);
903         return rval;
904 }
905
906 void
907 check_cwd(void)
908 {
909 #ifdef DEBUG
910         struct stat64   statbuf;
911
912         if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
913                 return;
914         assert(chdir(homedir) == 0);
915         fprintf(stderr, "fsstress: check_cwd failure\n");
916         abort();
917         /* NOTREACHED */
918 #endif
919 }
920
921 /*
922  * go thru flist and release all entries
923  *
924  * NOTE: this function shouldn't be called until the end of a process
925  */
926 void
927 cleanup_flist(void)
928 {
929         flist_t *flp;
930         int     i;
931
932         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
933                 flp->nslots = 0;
934                 flp->nfiles = 0;
935                 free(flp->fents);
936                 flp->fents = NULL;
937         }
938 }
939
940 int
941 creat_path(pathname_t *name, mode_t mode)
942 {
943         char            buf[NAME_MAX + 1];
944         pathname_t      newname;
945         int             rval;
946
947         rval = creat(name->path, mode);
948         if (rval >= 0 || errno != ENAMETOOLONG)
949                 return rval;
950         separate_pathname(name, buf, &newname);
951         if (chdir(buf) == 0) {
952                 rval = creat_path(&newname, mode);
953                 assert(chdir("..") == 0);
954         }
955         free_pathname(&newname);
956         return rval;
957 }
958
959 void
960 dcache_enter(int dirid, int slot)
961 {
962         dcache[dirid % NDCACHE] = slot;
963 }
964
965 void
966 dcache_init(void)
967 {
968         int     i;
969
970         for (i = 0; i < NDCACHE; i++)
971                 dcache[i] = -1;
972 }
973
974 fent_t *
975 dcache_lookup(int dirid)
976 {
977         fent_t  *fep;
978         int     i;
979
980         i = dcache[dirid % NDCACHE];
981         if (i >= 0 && i < flist[FT_DIR].nfiles &&
982             (fep = &flist[FT_DIR].fents[i])->id == dirid)
983                 return fep;
984         if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
985             (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
986                 return fep;
987         return NULL;
988 }
989
990 void
991 dcache_purge(int dirid, int ft)
992 {
993         int     *dcp;
994         dcp = &dcache[dirid % NDCACHE];
995         if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
996             flist[ft].fents[*dcp].id == dirid)
997                 *dcp = -1;
998 }
999
1000 /*
1001  * Delete the item from the list by
1002  * moving last entry over the deleted one;
1003  * unless deleted entry is the last one.
1004  * Input: which file list array and which slot in array
1005  */
1006 void
1007 del_from_flist(int ft, int slot)
1008 {
1009         flist_t *ftp;
1010
1011         ftp = &flist[ft];
1012         if (ft == FT_DIR || ft == FT_SUBVOL)
1013                 dcache_purge(ftp->fents[slot].id, ft);
1014         if (slot != ftp->nfiles - 1) {
1015                 if (ft == FT_DIR || ft == FT_SUBVOL)
1016                         dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1017                 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1018         } else
1019                 ftp->nfiles--;
1020 }
1021
1022 void
1023 delete_subvol_children(int parid)
1024 {
1025         flist_t *flp;
1026         int     i;
1027 again:
1028         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1029                 int c;
1030                 for (c = 0; c < flp->nfiles; c++) {
1031                         if (flp->fents[c].parent == parid) {
1032                                 int id = flp->fents[c].id;
1033                                 del_from_flist(i, c);
1034                                 if (i == FT_DIR || i == FT_SUBVOL)
1035                                         delete_subvol_children(id);
1036                                 goto again;
1037                         }
1038                 }
1039         }
1040 }
1041
1042 fent_t *
1043 dirid_to_fent(int dirid)
1044 {
1045         fent_t  *efep;
1046         fent_t  *fep;
1047         flist_t *flp;
1048         int     ft = FT_DIR;
1049
1050         if ((fep = dcache_lookup(dirid)))
1051                 return fep;
1052 again:
1053         flp = &flist[ft];
1054         for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1055                 if (fep->id == dirid) {
1056                         dcache_enter(dirid, fep - flp->fents);
1057                         return fep;
1058                 }
1059         }
1060         if (ft == FT_DIR) {
1061                 ft = FT_SUBVOL;
1062                 goto again;
1063         }
1064         return NULL;
1065 }
1066
1067 void
1068 doproc(void)
1069 {
1070         struct stat64   statbuf;
1071         char            buf[10];
1072         char            cmd[64];
1073         int             opno;
1074         int             rval;
1075         opdesc_t        *p;
1076         int             dividend;
1077
1078         dividend = (operations + execute_freq) / (execute_freq + 1);
1079         sprintf(buf, "p%x", procid);
1080         (void)mkdir(buf, 0777);
1081         if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1082                 perror(buf);
1083                 _exit(1);
1084         }
1085         top_ino = statbuf.st_ino;
1086         homedir = getcwd(NULL, 0);
1087         if (!homedir) {
1088                 perror("getcwd failed");
1089                 _exit(1);
1090         }
1091         seed += procid;
1092         srandom(seed);
1093         if (namerand)
1094                 namerand = random();
1095         for (opno = 0; opno < operations; opno++) {
1096                 if (execute_cmd && opno && opno % dividend == 0) {
1097                         if (verbose)
1098                                 printf("%d: execute command %s\n", opno,
1099                                         execute_cmd);
1100                         rval = system(execute_cmd);
1101                         if (rval)
1102                                 fprintf(stderr, "execute command failed with "
1103                                         "%d\n", rval);
1104                 }
1105                 p = &ops[freq_table[random() % freq_table_size]];
1106                 p->func(opno, random());
1107                 /*
1108                  * test for forced shutdown by stat'ing the test
1109                  * directory.  If this stat returns EIO, assume
1110                  * the forced shutdown happened.
1111                  */
1112                 if (errtag != 0 && opno % 100 == 0)  {
1113                         rval = stat64(".", &statbuf);
1114                         if (rval == EIO)  {
1115                                 fprintf(stderr, "Detected EIO\n");
1116                                 goto errout;
1117                         }
1118                 }
1119         }
1120 errout:
1121         assert(chdir("..") == 0);
1122         free(homedir);
1123         if (cleanup) {
1124                 int ret;
1125
1126                 sprintf(cmd, "rm -rf %s", buf);
1127                 ret = system(cmd);
1128                 if (ret != 0)
1129                         perror("cleaning up");
1130                 cleanup_flist();
1131         }
1132 }
1133
1134 /*
1135  * build up a pathname going thru the file entry and all
1136  * its parent entries
1137  * Return 0 on error, 1 on success;
1138  */
1139 int
1140 fent_to_name(pathname_t *name, fent_t *fep)
1141 {
1142         flist_t *flp = &flist[fep->ft];
1143         char    buf[NAME_MAX + 1];
1144         int     i;
1145         fent_t  *pfep;
1146         int     e;
1147
1148         if (fep == NULL)
1149                 return 0;
1150
1151         /* build up parent directory name */
1152         if (fep->parent != -1) {
1153                 pfep = dirid_to_fent(fep->parent);
1154 #ifdef DEBUG
1155                 if (pfep == NULL) {
1156                         fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1157                                 procid, fep->id, fep->parent);
1158                 } 
1159 #endif
1160                 if (pfep == NULL)
1161                         return 0;
1162                 e = fent_to_name(name, pfep);
1163                 if (!e)
1164                         return 0;
1165                 append_pathname(name, "/");
1166         }
1167
1168         i = sprintf(buf, "%c%x", flp->tag, fep->id);
1169         namerandpad(fep->id, buf, i);
1170         append_pathname(name, buf);
1171         return 1;
1172 }
1173
1174 bool
1175 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1176 {
1177         fent_t  *tmpfep;
1178
1179         for (tmpfep = fep; tmpfep->parent != -1;
1180              tmpfep = dirid_to_fent(tmpfep->parent)) {
1181                 if (tmpfep->parent == dfep->id)
1182                         return true;
1183         }
1184
1185         return false;
1186 }
1187
1188 void
1189 fix_parent(int oldid, int newid, bool swap)
1190 {
1191         fent_t  *fep;
1192         flist_t *flp;
1193         int     i;
1194         int     j;
1195
1196         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1197                 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1198                         if (fep->parent == oldid)
1199                                 fep->parent = newid;
1200                         else if (swap && fep->parent == newid)
1201                                 fep->parent = oldid;
1202                 }
1203         }
1204 }
1205
1206 void
1207 free_pathname(pathname_t *name)
1208 {
1209         if (name->path) {
1210                 free(name->path);
1211                 name->path = NULL;
1212                 name->len = 0;
1213         }
1214 }
1215
1216 /*
1217  * Generate a filename of type ft.
1218  * If we have a fep which should be a directory then use it
1219  * as the parent path for this new filename i.e. prepend with it.
1220  */
1221 int
1222 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1223 {
1224         char    buf[NAME_MAX + 1];
1225         flist_t *flp;
1226         int     id;
1227         int     j;
1228         int     len;
1229         int     e;
1230
1231         /* create name */
1232         flp = &flist[ft];
1233         len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1234         namerandpad(id, buf, len);
1235
1236         /* prepend fep parent dir-name to it */
1237         if (fep) {
1238                 e = fent_to_name(name, fep);
1239                 if (!e)
1240                         return 0;
1241                 append_pathname(name, "/");
1242         }
1243         append_pathname(name, buf);
1244
1245         *idp = id;
1246         *v = verbose;
1247         for (j = 0; !*v && j < ilistlen; j++) {
1248                 if (ilist[j] == id) {
1249                         *v = 1;
1250                         break;
1251                 }
1252         }
1253         return 1;
1254 }
1255
1256 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1257 {
1258         int ret;
1259
1260         ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1261         if (ret < 0)
1262                 return ret;
1263         if (ret < buflen)
1264                 return 0;
1265         return -EOVERFLOW;
1266 }
1267
1268 /*
1269  * Get file 
1270  * Input: "which" to choose the file-types eg. non-directory
1271  * Input: "r" to choose which file
1272  * Output: file-list, file-entry, name for the chosen file.
1273  * Output: verbose if chosen file is on the ilist.
1274  */
1275 int
1276 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1277           int *v)
1278 {
1279         int     totalsum = 0; /* total number of matching files */
1280         int     partialsum = 0; /* partial sum of matching files */
1281         fent_t  *fep;
1282         flist_t *flp;
1283         int     i;
1284         int     j;
1285         int     x;
1286         int     e = 1; /* success */
1287
1288         /*
1289          * go thru flist and add up number of files for each
1290          * category that matches with <which>.
1291          */
1292         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1293                 if (which & (1 << i))
1294                         totalsum += flp->nfiles;
1295         }
1296         if (totalsum == 0) {
1297                 if (flpp)
1298                         *flpp = NULL;
1299                 if (fepp)
1300                         *fepp = NULL;
1301                 *v = verbose;
1302                 return 0;
1303         }
1304
1305         /*
1306          * Now we have possible matches between 0..totalsum-1.
1307          * And we use r to help us choose which one we want,
1308          * which when bounded by totalsum becomes x.
1309          */ 
1310         x = (int)(r % totalsum);
1311         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1312                 if (which & (1 << i)) {
1313                         if (x < partialsum + flp->nfiles) {
1314
1315                                 /* found the matching file entry */
1316                                 fep = &flp->fents[x - partialsum];
1317
1318                                 /* fill-in what we were asked for */
1319                                 if (name) {
1320                                         e = fent_to_name(name, fep);
1321 #ifdef DEBUG
1322                                         if (!e) {
1323                                                 fprintf(stderr, "%d: failed to get path for entry:"
1324                                                                 " id=%d,parent=%d\n",   
1325                                                         procid, fep->id, fep->parent);
1326                                         }
1327 #endif
1328                                 }
1329                                 if (flpp)
1330                                         *flpp = flp;
1331                                 if (fepp)
1332                                         *fepp = fep;
1333
1334                                 /* turn on verbose if its an ilisted file */
1335                                 *v = verbose;
1336                                 for (j = 0; !*v && j < ilistlen; j++) {
1337                                         if (ilist[j] == fep->id) {
1338                                                 *v = 1;
1339                                                 break;
1340                                         }
1341                                 }
1342                                 return e;
1343                         }
1344                         partialsum += flp->nfiles;
1345                 }
1346         }
1347 #ifdef DEBUG
1348         fprintf(stderr, "fsstress: get_fname failure\n");
1349         abort();
1350 #endif
1351         return 0;
1352 }
1353
1354 void
1355 init_pathname(pathname_t *name)
1356 {
1357         name->len = 0;
1358         name->path = NULL;
1359 }
1360
1361 int
1362 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1363 {
1364         char            buf[NAME_MAX + 1];
1365         pathname_t      newname;
1366         int             rval;
1367
1368         rval = lchown(name->path, owner, group);
1369         if (rval >= 0 || errno != ENAMETOOLONG)
1370                 return rval;
1371         separate_pathname(name, buf, &newname);
1372         if (chdir(buf) == 0) {
1373                 rval = lchown_path(&newname, owner, group);
1374                 assert(chdir("..") == 0);
1375         }
1376         free_pathname(&newname);
1377         return rval;
1378 }
1379
1380 int
1381 link_path(pathname_t *name1, pathname_t *name2)
1382 {
1383         char            buf1[NAME_MAX + 1];
1384         char            buf2[NAME_MAX + 1];
1385         int             down1;
1386         pathname_t      newname1;
1387         pathname_t      newname2;
1388         int             rval;
1389
1390         rval = link(name1->path, name2->path);
1391         if (rval >= 0 || errno != ENAMETOOLONG)
1392                 return rval;
1393         separate_pathname(name1, buf1, &newname1);
1394         separate_pathname(name2, buf2, &newname2);
1395         if (strcmp(buf1, buf2) == 0) {
1396                 if (chdir(buf1) == 0) {
1397                         rval = link_path(&newname1, &newname2);
1398                         assert(chdir("..") == 0);
1399                 }
1400         } else {
1401                 if (strcmp(buf1, "..") == 0)
1402                         down1 = 0;
1403                 else if (strcmp(buf2, "..") == 0)
1404                         down1 = 1;
1405                 else if (strlen(buf1) == 0)
1406                         down1 = 0;
1407                 else if (strlen(buf2) == 0)
1408                         down1 = 1;
1409                 else
1410                         down1 = MAX(newname1.len, 3 + name2->len) <=
1411                                 MAX(3 + name1->len, newname2.len);
1412                 if (down1) {
1413                         free_pathname(&newname2);
1414                         append_pathname(&newname2, "../");
1415                         append_pathname(&newname2, name2->path);
1416                         if (chdir(buf1) == 0) {
1417                                 rval = link_path(&newname1, &newname2);
1418                                 assert(chdir("..") == 0);
1419                         }
1420                 } else {
1421                         free_pathname(&newname1);
1422                         append_pathname(&newname1, "../");
1423                         append_pathname(&newname1, name1->path);
1424                         if (chdir(buf2) == 0) {
1425                                 rval = link_path(&newname1, &newname2);
1426                                 assert(chdir("..") == 0);
1427                         }
1428                 }
1429         }
1430         free_pathname(&newname1);
1431         free_pathname(&newname2);
1432         return rval;
1433 }
1434
1435 int
1436 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1437 {
1438         char            buf[NAME_MAX + 1];
1439         pathname_t      newname;
1440         int             rval;
1441
1442         rval = lstat64(name->path, sbuf);
1443         if (rval >= 0 || errno != ENAMETOOLONG)
1444                 return rval;
1445         separate_pathname(name, buf, &newname);
1446         if (chdir(buf) == 0) {
1447                 rval = lstat64_path(&newname, sbuf);
1448                 assert(chdir("..") == 0);
1449         }
1450         free_pathname(&newname);
1451         return rval;
1452 }
1453
1454 void
1455 make_freq_table(void)
1456 {
1457         int             f;
1458         int             i;
1459         opdesc_t        *p;
1460
1461         for (p = ops, f = 0; p < ops_end; p++)
1462                 f += p->freq;
1463         freq_table = malloc(f * sizeof(*freq_table));
1464         freq_table_size = f;
1465         for (p = ops, i = 0; p < ops_end; p++) {
1466                 for (f = 0; f < p->freq; f++, i++)
1467                         freq_table[i] = p->op;
1468         }
1469 }
1470
1471 int
1472 mkdir_path(pathname_t *name, mode_t mode)
1473 {
1474         char            buf[NAME_MAX + 1];
1475         pathname_t      newname;
1476         int             rval;
1477
1478         rval = mkdir(name->path, mode);
1479         if (rval >= 0 || errno != ENAMETOOLONG)
1480                 return rval;
1481         separate_pathname(name, buf, &newname);
1482         if (chdir(buf) == 0) {
1483                 rval = mkdir_path(&newname, mode);
1484                 assert(chdir("..") == 0);
1485         }
1486         free_pathname(&newname);
1487         return rval;
1488 }
1489
1490 int
1491 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1492 {
1493         char            buf[NAME_MAX + 1];
1494         pathname_t      newname;
1495         int             rval;
1496
1497         rval = mknod(name->path, mode, dev);
1498         if (rval >= 0 || errno != ENAMETOOLONG)
1499                 return rval;
1500         separate_pathname(name, buf, &newname);
1501         if (chdir(buf) == 0) {
1502                 rval = mknod_path(&newname, mode, dev);
1503                 assert(chdir("..") == 0);
1504         }
1505         free_pathname(&newname);
1506         return rval;
1507 }
1508
1509 void
1510 namerandpad(int id, char *buf, int i)
1511 {
1512         int             bucket;
1513         static int      buckets[] =
1514                                 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1515         int             padlen;
1516         int             padmod;
1517
1518         if (namerand == 0)
1519                 return;
1520         bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1521         padmod = buckets[bucket] + 1 - i;
1522         if (padmod <= 0)
1523                 return;
1524         padlen = (id ^ namerand) % padmod;
1525         if (padlen) {
1526                 memset(&buf[i], 'X', padlen);
1527                 buf[i + padlen] = '\0';
1528         }
1529 }
1530
1531 int
1532 open_file_or_dir(pathname_t *name, int flags)
1533 {
1534         int fd;
1535
1536         fd = open_path(name, flags);
1537         if (fd != -1)
1538                 return fd;
1539         if (fd == -1 && errno != EISDIR)
1540                 return fd;
1541         /* Directories can not be opened in write mode nor direct mode. */
1542         flags &= ~(O_WRONLY | O_DIRECT);
1543         flags |= O_RDONLY | O_DIRECTORY;
1544         return open_path(name, flags);
1545 }
1546
1547 int
1548 open_path(pathname_t *name, int oflag)
1549 {
1550         char            buf[NAME_MAX + 1];
1551         pathname_t      newname;
1552         int             rval;
1553
1554         rval = open(name->path, oflag);
1555         if (rval >= 0 || errno != ENAMETOOLONG)
1556                 return rval;
1557         separate_pathname(name, buf, &newname);
1558         if (chdir(buf) == 0) {
1559                 rval = open_path(&newname, oflag);
1560                 assert(chdir("..") == 0);
1561         }
1562         free_pathname(&newname);
1563         return rval;
1564 }
1565
1566 DIR *
1567 opendir_path(pathname_t *name)
1568 {
1569         char            buf[NAME_MAX + 1];
1570         pathname_t      newname;
1571         DIR             *rval;
1572
1573         rval = opendir(name->path);
1574         if (rval || errno != ENAMETOOLONG)
1575                 return rval;
1576         separate_pathname(name, buf, &newname);
1577         if (chdir(buf) == 0) {
1578                 rval = opendir_path(&newname);
1579                 assert(chdir("..") == 0);
1580         }
1581         free_pathname(&newname);
1582         return rval;
1583 }
1584
1585 void
1586 process_freq(char *arg)
1587 {
1588         opdesc_t        *p;
1589         char            *s;
1590
1591         s = strchr(arg, '=');
1592         if (s == NULL) {
1593                 fprintf(stderr, "bad argument '%s'\n", arg);
1594                 exit(1);
1595         }
1596         *s++ = '\0';
1597         for (p = ops; p < ops_end; p++) {
1598                 if (strcmp(arg, p->name) == 0) {
1599                         p->freq = atoi(s);
1600                         return;
1601                 }
1602         }
1603         fprintf(stderr, "can't find op type %s for -f\n", arg);
1604         exit(1);
1605 }
1606
1607 int
1608 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1609 {
1610         char            buf[NAME_MAX + 1];
1611         pathname_t      newname;
1612         int             rval;
1613
1614         rval = readlink(name->path, lbuf, lbufsiz);
1615         if (rval >= 0 || errno != ENAMETOOLONG)
1616                 return rval;
1617         separate_pathname(name, buf, &newname);
1618         if (chdir(buf) == 0) {
1619                 rval = readlink_path(&newname, lbuf, lbufsiz);
1620                 assert(chdir("..") == 0);
1621         }
1622         free_pathname(&newname);
1623         return rval;
1624 }
1625
1626 int
1627 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1628 {
1629         char            buf1[NAME_MAX + 1];
1630         char            buf2[NAME_MAX + 1];
1631         int             down1;
1632         pathname_t      newname1;
1633         pathname_t      newname2;
1634         int             rval;
1635
1636         if (mode == 0)
1637                 rval = rename(name1->path, name2->path);
1638         else
1639                 rval = renameat2(AT_FDCWD, name1->path,
1640                                  AT_FDCWD, name2->path, mode);
1641         if (rval >= 0 || errno != ENAMETOOLONG)
1642                 return rval;
1643         separate_pathname(name1, buf1, &newname1);
1644         separate_pathname(name2, buf2, &newname2);
1645         if (strcmp(buf1, buf2) == 0) {
1646                 if (chdir(buf1) == 0) {
1647                         rval = rename_path(&newname1, &newname2, mode);
1648                         assert(chdir("..") == 0);
1649                 }
1650         } else {
1651                 if (strcmp(buf1, "..") == 0)
1652                         down1 = 0;
1653                 else if (strcmp(buf2, "..") == 0)
1654                         down1 = 1;
1655                 else if (strlen(buf1) == 0)
1656                         down1 = 0;
1657                 else if (strlen(buf2) == 0)
1658                         down1 = 1;
1659                 else
1660                         down1 = MAX(newname1.len, 3 + name2->len) <=
1661                                 MAX(3 + name1->len, newname2.len);
1662                 if (down1) {
1663                         free_pathname(&newname2);
1664                         append_pathname(&newname2, "../");
1665                         append_pathname(&newname2, name2->path);
1666                         if (chdir(buf1) == 0) {
1667                                 rval = rename_path(&newname1, &newname2, mode);
1668                                 assert(chdir("..") == 0);
1669                         }
1670                 } else {
1671                         free_pathname(&newname1);
1672                         append_pathname(&newname1, "../");
1673                         append_pathname(&newname1, name1->path);
1674                         if (chdir(buf2) == 0) {
1675                                 rval = rename_path(&newname1, &newname2, mode);
1676                                 assert(chdir("..") == 0);
1677                         }
1678                 }
1679         }
1680         free_pathname(&newname1);
1681         free_pathname(&newname2);
1682         return rval;
1683 }
1684
1685 int
1686 rmdir_path(pathname_t *name)
1687 {
1688         char            buf[NAME_MAX + 1];
1689         pathname_t      newname;
1690         int             rval;
1691
1692         rval = rmdir(name->path);
1693         if (rval >= 0 || errno != ENAMETOOLONG)
1694                 return rval;
1695         separate_pathname(name, buf, &newname);
1696         if (chdir(buf) == 0) {
1697                 rval = rmdir_path(&newname);
1698                 assert(chdir("..") == 0);
1699         }
1700         free_pathname(&newname);
1701         return rval;
1702 }
1703
1704 void
1705 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1706 {
1707         char    *slash;
1708
1709         init_pathname(newname);
1710         slash = strchr(name->path, '/');
1711         if (slash == NULL) {
1712                 buf[0] = '\0';
1713                 return;
1714         }
1715         *slash = '\0';
1716         strcpy(buf, name->path);
1717         *slash = '/';
1718         append_pathname(newname, slash + 1);
1719 }
1720
1721 #define WIDTH 80
1722
1723 void
1724 show_ops(int flag, char *lead_str)
1725 {
1726         opdesc_t        *p;
1727
1728         if (flag<0) {
1729                 /* print in list form */
1730                 int             x = WIDTH;
1731                 
1732                 for (p = ops; p < ops_end; p++) {
1733                         if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1734                                 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1735                         x+=printf("%s ", p->name);
1736                 }
1737                 printf("\n");
1738         } else if (flag == 0) {
1739                 /* Table view style */
1740                 int             f;
1741                 for (f = 0, p = ops; p < ops_end; p++)
1742                         f += p->freq;
1743
1744                 if (f == 0)
1745                         flag = 1;
1746
1747                 for (p = ops; p < ops_end; p++) {
1748                         if (flag != 0 || p->freq > 0) {
1749                                 if (lead_str != NULL)
1750                                         printf("%s", lead_str);
1751                                 printf("%20s %d/%d %s\n",
1752                                 p->name, p->freq, f,
1753                                 (p->iswrite == 0) ? " " : "write op");
1754                         }
1755                 }
1756         } else {
1757                 /* Command line style */
1758                 if (lead_str != NULL)
1759                         printf("%s", lead_str);
1760                 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1761                         operations, nproc);
1762                 for (p = ops; p < ops_end; p++)
1763                         if (p->freq > 0)
1764                                 printf("-f %s=%d \\\n",p->name, p->freq);
1765         }
1766 }
1767
1768 int
1769 stat64_path(pathname_t *name, struct stat64 *sbuf)
1770 {
1771         char            buf[NAME_MAX + 1];
1772         pathname_t      newname;
1773         int             rval;
1774
1775         rval = stat64(name->path, sbuf);
1776         if (rval >= 0 || errno != ENAMETOOLONG)
1777                 return rval;
1778         separate_pathname(name, buf, &newname);
1779         if (chdir(buf) == 0) {
1780                 rval = stat64_path(&newname, sbuf);
1781                 assert(chdir("..") == 0);
1782         }
1783         free_pathname(&newname);
1784         return rval;
1785 }
1786
1787 int
1788 symlink_path(const char *name1, pathname_t *name)
1789 {
1790         char            buf[NAME_MAX + 1];
1791         pathname_t      newname;
1792         int             rval;
1793         
1794         if (!strcmp(name1, name->path)) {
1795             printf("yikes! %s %s\n", name1, name->path);
1796             return 0;
1797         }
1798
1799         rval = symlink(name1, name->path);
1800         if (rval >= 0 || errno != ENAMETOOLONG)
1801                 return rval;
1802         separate_pathname(name, buf, &newname);
1803         if (chdir(buf) == 0) {
1804                 rval = symlink_path(name1, &newname);
1805                 assert(chdir("..") == 0);
1806         }
1807         free_pathname(&newname);
1808         return rval;
1809 }
1810
1811 int
1812 truncate64_path(pathname_t *name, off64_t length)
1813 {
1814         char            buf[NAME_MAX + 1];
1815         pathname_t      newname;
1816         int             rval;
1817
1818         rval = truncate64(name->path, length);
1819         if (rval >= 0 || errno != ENAMETOOLONG)
1820                 return rval;
1821         separate_pathname(name, buf, &newname);
1822         if (chdir(buf) == 0) {
1823                 rval = truncate64_path(&newname, length);
1824                 assert(chdir("..") == 0);
1825         }
1826         free_pathname(&newname);
1827         return rval;
1828 }
1829
1830 int
1831 unlink_path(pathname_t *name)
1832 {
1833         char            buf[NAME_MAX + 1];
1834         pathname_t      newname;
1835         int             rval;
1836
1837         rval = unlink(name->path);
1838         if (rval >= 0 || errno != ENAMETOOLONG)
1839                 return rval;
1840         separate_pathname(name, buf, &newname);
1841         if (chdir(buf) == 0) {
1842                 rval = unlink_path(&newname);
1843                 assert(chdir("..") == 0);
1844         }
1845         free_pathname(&newname);
1846         return rval;
1847 }
1848
1849 void
1850 usage(void)
1851 {
1852         printf("Usage: %s -H   or\n", myprog);
1853         printf("       %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1854                 myprog);
1855         printf("          [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1856         printf("where\n");
1857         printf("   -c               clean up the test directory after each run\n");
1858         printf("   -d dir           specifies the base directory for operations\n");
1859         printf("   -e errtg         specifies error injection stuff\n");
1860         printf("   -f op_name=freq  changes the frequency of option name to freq\n");
1861         printf("                    the valid operation names are:\n");
1862         show_ops(-1, "                        ");
1863         printf("   -i filenum       get verbose output for this nth file object\n");
1864         printf("   -l loops         specifies the no. of times the testrun should loop.\n");
1865         printf("                     *use 0 for infinite (default 1)\n");
1866         printf("   -m modulo        uid/gid modulo for chown/chgrp (default 32)\n");
1867         printf("   -n nops          specifies the no. of operations per process (default 1)\n");
1868         printf("   -o logfile       specifies logfile name\n");
1869         printf("   -p nproc         specifies the no. of processes (default 1)\n");
1870         printf("   -r               specifies random name padding\n");
1871         printf("   -s seed          specifies the seed for the random generator (default random)\n");
1872         printf("   -v               specifies verbose mode\n");
1873         printf("   -w               zeros frequencies of non-write operations\n");
1874         printf("   -x cmd           execute command in the middle of operations\n");
1875         printf("   -z               zeros frequencies of all operations\n");
1876         printf("   -S [c,t]         prints the list of operations (omitting zero frequency) in command line or table style\n");
1877         printf("   -V               specifies verifiable logging mode (omitting inode numbers)\n");
1878         printf("   -X ncmd          number of calls to the -x command (default 1)\n");
1879         printf("   -H               prints usage and exits\n");
1880 }
1881
1882 void
1883 write_freq(void)
1884 {
1885         opdesc_t        *p;
1886
1887         for (p = ops; p < ops_end; p++) {
1888                 if (!p->iswrite)
1889                         p->freq = 0;
1890         }
1891 }
1892
1893 void
1894 zero_freq(void)
1895 {
1896         opdesc_t        *p;
1897
1898         for (p = ops; p < ops_end; p++)
1899                 p->freq = 0;
1900 }
1901
1902 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1903
1904 opty_t btrfs_ops[] = {
1905         OP_SUBVOL_CREATE,
1906         OP_SUBVOL_DELETE,
1907 };
1908
1909 void
1910 non_btrfs_freq(const char *path)
1911 {
1912         opdesc_t                *p;
1913 #ifdef HAVE_BTRFSUTIL_H
1914         enum btrfs_util_error   e;
1915
1916         e = btrfs_util_is_subvolume(path);
1917         if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1918                 return;
1919 #endif
1920         for (p = ops; p < ops_end; p++) {
1921                 int i;
1922                 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1923                         if (p->op == btrfs_ops[i]) {
1924                                 p->freq = 0;
1925                                 break;
1926                         }
1927                 }
1928         }
1929 }
1930
1931 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1932 {
1933         if (verbose)
1934                 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1935                          verifiable_log ? -1: (long)s->st_ino,
1936                          (long)s->st_nlink,  s->st_uid, s->st_gid,
1937                          (long long) s->st_blocks, (long long) s->st_size);
1938 }
1939
1940 void
1941 afsync_f(int opno, long r)
1942 {
1943 #ifdef AIO
1944         int             e;
1945         pathname_t      f;
1946         int             fd;
1947         int             v;
1948         struct iocb     iocb;
1949         struct iocb     *iocbs[] = { &iocb };
1950         struct io_event event;
1951
1952         init_pathname(&f);
1953         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1954                 if (v)
1955                         printf("%d/%d: afsync - no filename\n", procid, opno);
1956                 free_pathname(&f);
1957                 return;
1958         }
1959         fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1960         e = fd < 0 ? errno : 0;
1961         check_cwd();
1962         if (fd < 0) {
1963                 if (v)
1964                         printf("%d/%d: afsync - open %s failed %d\n",
1965                                procid, opno, f.path, e);
1966                 free_pathname(&f);
1967                 return;
1968         }
1969
1970         io_prep_fsync(&iocb, fd);
1971         if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1972                 if (v)
1973                         printf("%d/%d: afsync - io_submit %s %d\n",
1974                                procid, opno, f.path, e);
1975                 free_pathname(&f);
1976                 close(fd);
1977                 return;
1978         }
1979         if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1980                 if (v)
1981                         printf("%d/%d: afsync - io_getevents failed %d\n",
1982                                procid, opno, e);
1983                 free_pathname(&f);
1984                 close(fd);
1985                 return;
1986         }
1987
1988         e = event.res2;
1989         if (v)
1990                 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1991         free_pathname(&f);
1992         close(fd);
1993 #endif
1994 }
1995
1996 void
1997 allocsp_f(int opno, long r)
1998 {
1999         int             e;
2000         pathname_t      f;
2001         int             fd;
2002         struct xfs_flock64      fl;
2003         int64_t         lr;
2004         off64_t         off;
2005         struct stat64   stb;
2006         int             v;
2007         char            st[1024];
2008
2009         init_pathname(&f);
2010         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2011                 if (v)
2012                         printf("%d/%d: allocsp - no filename\n", procid, opno);
2013                 free_pathname(&f);
2014                 return;
2015         }
2016         fd = open_path(&f, O_RDWR);
2017         e = fd < 0 ? errno : 0;
2018         check_cwd();
2019         if (fd < 0) {
2020                 if (v)
2021                         printf("%d/%d: allocsp - open %s failed %d\n",
2022                                 procid, opno, f.path, e);
2023                 free_pathname(&f);
2024                 return;
2025         }
2026         if (fstat64(fd, &stb) < 0) {
2027                 if (v)
2028                         printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2029                                 procid, opno, f.path, errno);
2030                 free_pathname(&f);
2031                 close(fd);
2032                 return;
2033         }
2034         inode_info(st, sizeof(st), &stb, v);
2035         lr = ((int64_t)random() << 32) + random();
2036         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2037         off %= maxfsize;
2038         fl.l_whence = SEEK_SET;
2039         fl.l_start = off;
2040         fl.l_len = 0;
2041         e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2042         if (v) {
2043                 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2044                        procid, opno, f.path, st, (long long)off, e);
2045         }
2046         free_pathname(&f);
2047         close(fd);
2048 }
2049
2050 #ifdef AIO
2051 void
2052 do_aio_rw(int opno, long r, int flags)
2053 {
2054         int64_t         align;
2055         char            *buf;
2056         struct dioattr  diob;
2057         int             e;
2058         pathname_t      f;
2059         int             fd;
2060         size_t          len;
2061         int64_t         lr;
2062         off64_t         off;
2063         struct stat64   stb;
2064         int             v;
2065         char            st[1024];
2066         char            *dio_env;
2067         struct iocb     iocb;
2068         struct io_event event;
2069         struct iocb     *iocbs[] = { &iocb };
2070         int             iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2071
2072         init_pathname(&f);
2073         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2074                 if (v)
2075                         printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2076                 free_pathname(&f);
2077                 return;
2078         }
2079         fd = open_path(&f, flags|O_DIRECT);
2080         e = fd < 0 ? errno : 0;
2081         check_cwd();
2082         if (fd < 0) {
2083                 if (v)
2084                         printf("%d/%d: do_aio_rw - open %s failed %d\n",
2085                                procid, opno, f.path, e);
2086                 free_pathname(&f);
2087                 return;
2088         }
2089         if (fstat64(fd, &stb) < 0) {
2090                 if (v)
2091                         printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2092                                procid, opno, f.path, errno);
2093                 free_pathname(&f);
2094                 close(fd);
2095                 return;
2096         }
2097         inode_info(st, sizeof(st), &stb, v);
2098         if (!iswrite && stb.st_size == 0) {
2099                 if (v)
2100                         printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2101                                f.path, st);
2102                 free_pathname(&f);
2103                 close(fd);
2104                 return;
2105         }
2106         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2107                 if (v)
2108                         printf(
2109                         "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2110                         " fallback to stat()\n",
2111                                 procid, opno, f.path, st, errno);
2112                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2113                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2114         }
2115         dio_env = getenv("XFS_DIO_MIN");
2116         if (dio_env)
2117                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2118         align = (int64_t)diob.d_miniosz;
2119         lr = ((int64_t)random() << 32) + random();
2120         len = (random() % FILELEN_MAX) + 1;
2121         len -= (len % align);
2122         if (len <= 0)
2123                 len = align;
2124         else if (len > diob.d_maxiosz)
2125                 len = diob.d_maxiosz;
2126         buf = memalign(diob.d_mem, len);
2127
2128         if (iswrite) {
2129                 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2130                 off -= (off % align);
2131                 off %= maxfsize;
2132                 memset(buf, nameseq & 0xff, len);
2133                 io_prep_pwrite(&iocb, fd, buf, len, off);
2134         } else {
2135                 off = (off64_t)(lr % stb.st_size);
2136                 off -= (off % align);
2137                 io_prep_pread(&iocb, fd, buf, len, off);
2138         }
2139         if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2140                 if (v)
2141                         printf("%d/%d: %s - io_submit failed %d\n",
2142                                procid, opno, iswrite ? "awrite" : "aread", e);
2143                 free_pathname(&f);
2144                 close(fd);
2145                 return;
2146         }
2147         if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2148                 if (v)
2149                         printf("%d/%d: %s - io_getevents failed %d\n",
2150                                procid, opno, iswrite ? "awrite" : "aread", e);
2151                 free_pathname(&f);
2152                 close(fd);
2153                 return;
2154         }
2155
2156         e = event.res != len ? event.res2 : 0;
2157         free(buf);
2158         if (v)
2159                 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2160                        procid, opno, iswrite ? "awrite" : "aread",
2161                        f.path, st, (long long)off, (int)len, e);
2162         free_pathname(&f);
2163         close(fd);
2164 }
2165 #endif
2166
2167 void
2168 aread_f(int opno, long r)
2169 {
2170 #ifdef AIO
2171         do_aio_rw(opno, r, O_RDONLY);
2172 #endif
2173 }
2174
2175 void
2176 attr_remove_f(int opno, long r)
2177 {
2178         attrlist_ent_t          *aep;
2179         attrlist_t              *alist;
2180         char                    *aname;
2181         char                    buf[4096];
2182         attrlist_cursor_t       cursor;
2183         int                     e;
2184         int                     ent;
2185         pathname_t              f;
2186         int                     total;
2187         int                     v;
2188         int                     which;
2189
2190         init_pathname(&f);
2191         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2192                 append_pathname(&f, ".");
2193         total = 0;
2194         bzero(&cursor, sizeof(cursor));
2195         do {
2196                 bzero(buf, sizeof(buf));
2197                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2198                 check_cwd();
2199                 if (e)
2200                         break;
2201                 alist = (attrlist_t *)buf;
2202                 total += alist->al_count;
2203         } while (alist->al_more);
2204         if (total == 0) {
2205                 if (v)
2206                         printf("%d/%d: attr_remove - no attrs for %s\n",
2207                                 procid, opno, f.path);
2208                 free_pathname(&f);
2209                 return;
2210         }
2211         which = (int)(random() % total);
2212         bzero(&cursor, sizeof(cursor));
2213         ent = 0;
2214         aname = NULL;
2215         do {
2216                 bzero(buf, sizeof(buf));
2217                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2218                 check_cwd();
2219                 if (e)
2220                         break;
2221                 alist = (attrlist_t *)buf;
2222                 if (which < ent + alist->al_count) {
2223                         aep = (attrlist_ent_t *)
2224                                 &buf[alist->al_offset[which - ent]];
2225                         aname = aep->a_name;
2226                         break;
2227                 }
2228                 ent += alist->al_count;
2229         } while (alist->al_more);
2230         if (aname == NULL) {
2231                 if (v)
2232                         printf(
2233                         "%d/%d: attr_remove - name %d not found at %s\n",
2234                                 procid, opno, which, f.path);
2235                 free_pathname(&f);
2236                 return;
2237         }
2238         e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2239         check_cwd();
2240         if (v)
2241                 printf("%d/%d: attr_remove %s %s %d\n",
2242                         procid, opno, f.path, aname, e);
2243         free_pathname(&f);
2244 }
2245
2246 void
2247 attr_set_f(int opno, long r)
2248 {
2249         char            aname[10];
2250         char            *aval;
2251         int             e;
2252         pathname_t      f;
2253         int             len;
2254         static int      lengths[] = { 10, 100, 1000, 10000 };
2255         int             li;
2256         int             v;
2257
2258         init_pathname(&f);
2259         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2260                 append_pathname(&f, ".");
2261         sprintf(aname, "a%x", nameseq++);
2262         li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2263         len = (int)(random() % lengths[li]);
2264         if (len == 0)
2265                 len = 1;
2266         aval = malloc(len);
2267         memset(aval, nameseq & 0xff, len);
2268         e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2269                 errno : 0;
2270         check_cwd();
2271         free(aval);
2272         if (v)
2273                 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2274                         aname, e);
2275         free_pathname(&f);
2276 }
2277
2278 void
2279 awrite_f(int opno, long r)
2280 {
2281 #ifdef AIO
2282         do_aio_rw(opno, r, O_WRONLY);
2283 #endif
2284 }
2285
2286 void
2287 bulkstat_f(int opno, long r)
2288 {
2289         int             count;
2290         int             fd;
2291         __u64           last;
2292         int             nent;
2293         struct xfs_bstat        *t;
2294         int64_t         total;
2295         struct xfs_fsop_bulkreq bsr;
2296
2297         last = 0;
2298         nent = (r % 999) + 2;
2299         t = malloc(nent * sizeof(*t));
2300         fd = open(".", O_RDONLY);
2301         total = 0;
2302
2303         bsr.lastip=&last;
2304         bsr.icount=nent;
2305         bsr.ubuffer=t;
2306         bsr.ocount=&count;
2307             
2308         while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2309                 total += count;
2310         free(t);
2311         if (verbose)
2312                 printf("%d/%d: bulkstat nent %d total %lld\n",
2313                         procid, opno, nent, (long long)total);
2314         close(fd);
2315 }
2316
2317 void
2318 bulkstat1_f(int opno, long r)
2319 {
2320         int             e;
2321         pathname_t      f;
2322         int             fd;
2323         int             good;
2324         __u64           ino;
2325         struct stat64   s;
2326         struct xfs_bstat        t;
2327         int             v;
2328         struct xfs_fsop_bulkreq bsr;
2329         
2330
2331         good = random() & 1;
2332         if (good) {
2333                /* use an inode we know exists */
2334                 init_pathname(&f);
2335                 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2336                         append_pathname(&f, ".");
2337                 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2338                 check_cwd();
2339                 free_pathname(&f);
2340         } else {
2341                 /* 
2342                  * pick a random inode 
2343                  *
2344                  * note this can generate kernel warning messages
2345                  * since bulkstat_one will read the disk block that
2346                  * would contain a given inode even if that disk
2347                  * block doesn't contain inodes.
2348                  *
2349                  * this is detected later, but not until after the
2350                  * warning is displayed.
2351                  *
2352                  * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2353                  *
2354                  */
2355                 ino = (ino64_t)r;
2356                 v = verbose;
2357         }
2358         fd = open(".", O_RDONLY);
2359         
2360         bsr.lastip=&ino;
2361         bsr.icount=1;
2362         bsr.ubuffer=&t;
2363         bsr.ocount=NULL;
2364         e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2365         if (v)
2366                 printf("%d/%d: bulkstat1 %s ino %lld %d\n", 
2367                        procid, opno, good?"real":"random",
2368                        verifiable_log ? -1LL : (long long)ino, e);
2369         close(fd);
2370 }
2371
2372 void
2373 chown_f(int opno, long r)
2374 {
2375         int             e;
2376         pathname_t      f;
2377         int             nbits;
2378         uid_t           u;
2379         gid_t           g;
2380         int             v;
2381
2382         init_pathname(&f);
2383         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2384                 append_pathname(&f, ".");
2385         u = (uid_t)random();
2386         g = (gid_t)random();
2387         nbits = (int)(random() % idmodulo);
2388         u &= (1 << nbits) - 1;
2389         g &= (1 << nbits) - 1;
2390         e = lchown_path(&f, u, g) < 0 ? errno : 0;
2391         check_cwd();
2392         if (v)
2393                 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2394         free_pathname(&f);
2395 }
2396
2397 /* reflink some arbitrary range of f1 to f2. */
2398 void
2399 clonerange_f(
2400         int                     opno,
2401         long                    r)
2402 {
2403 #ifdef FICLONERANGE
2404         struct file_clone_range fcr;
2405         struct pathname         fpath1;
2406         struct pathname         fpath2;
2407         struct stat64           stat1;
2408         struct stat64           stat2;
2409         char                    inoinfo1[1024];
2410         char                    inoinfo2[1024];
2411         off64_t                 lr;
2412         off64_t                 off1;
2413         off64_t                 off2;
2414         off64_t                 max_off2;
2415         size_t                  len;
2416         int                     v1;
2417         int                     v2;
2418         int                     fd1;
2419         int                     fd2;
2420         int                     ret;
2421         int                     e;
2422
2423         /* Load paths */
2424         init_pathname(&fpath1);
2425         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2426                 if (v1)
2427                         printf("%d/%d: clonerange read - no filename\n",
2428                                 procid, opno);
2429                 goto out_fpath1;
2430         }
2431
2432         init_pathname(&fpath2);
2433         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2434                 if (v2)
2435                         printf("%d/%d: clonerange write - no filename\n",
2436                                 procid, opno);
2437                 goto out_fpath2;
2438         }
2439
2440         /* Open files */
2441         fd1 = open_path(&fpath1, O_RDONLY);
2442         e = fd1 < 0 ? errno : 0;
2443         check_cwd();
2444         if (fd1 < 0) {
2445                 if (v1)
2446                         printf("%d/%d: clonerange read - open %s failed %d\n",
2447                                 procid, opno, fpath1.path, e);
2448                 goto out_fpath2;
2449         }
2450
2451         fd2 = open_path(&fpath2, O_WRONLY);
2452         e = fd2 < 0 ? errno : 0;
2453         check_cwd();
2454         if (fd2 < 0) {
2455                 if (v2)
2456                         printf("%d/%d: clonerange write - open %s failed %d\n",
2457                                 procid, opno, fpath2.path, e);
2458                 goto out_fd1;
2459         }
2460
2461         /* Get file stats */
2462         if (fstat64(fd1, &stat1) < 0) {
2463                 if (v1)
2464                         printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2465                                 procid, opno, fpath1.path, errno);
2466                 goto out_fd2;
2467         }
2468         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2469
2470         if (fstat64(fd2, &stat2) < 0) {
2471                 if (v2)
2472                         printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2473                                 procid, opno, fpath2.path, errno);
2474                 goto out_fd2;
2475         }
2476         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2477
2478         /* Calculate offsets */
2479         len = (random() % FILELEN_MAX) + 1;
2480         len &= ~(stat1.st_blksize - 1);
2481         if (len == 0)
2482                 len = stat1.st_blksize;
2483         if (len > stat1.st_size)
2484                 len = stat1.st_size;
2485
2486         lr = ((int64_t)random() << 32) + random();
2487         if (stat1.st_size == len)
2488                 off1 = 0;
2489         else
2490                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2491         off1 %= maxfsize;
2492         off1 &= ~(stat1.st_blksize - 1);
2493
2494         /*
2495          * If srcfile == destfile, randomly generate destination ranges
2496          * until we find one that doesn't overlap the source range.
2497          */
2498         max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2499         do {
2500                 lr = ((int64_t)random() << 32) + random();
2501                 off2 = (off64_t)(lr % max_off2);
2502                 off2 %= maxfsize;
2503                 off2 &= ~(stat2.st_blksize - 1);
2504         } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2505
2506         /* Clone data blocks */
2507         fcr.src_fd = fd1;
2508         fcr.src_offset = off1;
2509         fcr.src_length = len;
2510         fcr.dest_offset = off2;
2511
2512         ret = ioctl(fd2, FICLONERANGE, &fcr);
2513         e = ret < 0 ? errno : 0;
2514         if (v1 || v2) {
2515                 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2516                         procid, opno,
2517                         fpath1.path, inoinfo1, (long long)off1, (long long)len,
2518                         fpath2.path, inoinfo2, (long long)off2, (long long)len);
2519
2520                 if (ret < 0)
2521                         printf(" error %d", e);
2522                 printf("\n");
2523         }
2524
2525 out_fd2:
2526         close(fd2);
2527 out_fd1:
2528         close(fd1);
2529 out_fpath2:
2530         free_pathname(&fpath2);
2531 out_fpath1:
2532         free_pathname(&fpath1);
2533 #endif
2534 }
2535
2536 /* copy some arbitrary range of f1 to f2. */
2537 void
2538 copyrange_f(
2539         int                     opno,
2540         long                    r)
2541 {
2542 #ifdef HAVE_COPY_FILE_RANGE
2543         struct pathname         fpath1;
2544         struct pathname         fpath2;
2545         struct stat64           stat1;
2546         struct stat64           stat2;
2547         char                    inoinfo1[1024];
2548         char                    inoinfo2[1024];
2549         loff_t                  lr;
2550         loff_t                  off1;
2551         loff_t                  off2;
2552         loff_t                  offset1;
2553         loff_t                  offset2;
2554         loff_t                  max_off2;
2555         size_t                  len;
2556         size_t                  length;
2557         int                     tries = 0;
2558         int                     v1;
2559         int                     v2;
2560         int                     fd1;
2561         int                     fd2;
2562         size_t                  ret = 0;
2563         int                     e;
2564
2565         /* Load paths */
2566         init_pathname(&fpath1);
2567         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2568                 if (v1)
2569                         printf("%d/%d: copyrange read - no filename\n",
2570                                 procid, opno);
2571                 goto out_fpath1;
2572         }
2573
2574         init_pathname(&fpath2);
2575         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2576                 if (v2)
2577                         printf("%d/%d: copyrange write - no filename\n",
2578                                 procid, opno);
2579                 goto out_fpath2;
2580         }
2581
2582         /* Open files */
2583         fd1 = open_path(&fpath1, O_RDONLY);
2584         e = fd1 < 0 ? errno : 0;
2585         check_cwd();
2586         if (fd1 < 0) {
2587                 if (v1)
2588                         printf("%d/%d: copyrange read - open %s failed %d\n",
2589                                 procid, opno, fpath1.path, e);
2590                 goto out_fpath2;
2591         }
2592
2593         fd2 = open_path(&fpath2, O_WRONLY);
2594         e = fd2 < 0 ? errno : 0;
2595         check_cwd();
2596         if (fd2 < 0) {
2597                 if (v2)
2598                         printf("%d/%d: copyrange write - open %s failed %d\n",
2599                                 procid, opno, fpath2.path, e);
2600                 goto out_fd1;
2601         }
2602
2603         /* Get file stats */
2604         if (fstat64(fd1, &stat1) < 0) {
2605                 if (v1)
2606                         printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2607                                 procid, opno, fpath1.path, errno);
2608                 goto out_fd2;
2609         }
2610         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2611
2612         if (fstat64(fd2, &stat2) < 0) {
2613                 if (v2)
2614                         printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2615                                 procid, opno, fpath2.path, errno);
2616                 goto out_fd2;
2617         }
2618         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2619
2620         /* Calculate offsets */
2621         len = (random() % FILELEN_MAX) + 1;
2622         if (len == 0)
2623                 len = stat1.st_blksize;
2624         if (len > stat1.st_size)
2625                 len = stat1.st_size;
2626
2627         lr = ((int64_t)random() << 32) + random();
2628         if (stat1.st_size == len)
2629                 off1 = 0;
2630         else
2631                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2632         off1 %= maxfsize;
2633
2634         /*
2635          * If srcfile == destfile, randomly generate destination ranges
2636          * until we find one that doesn't overlap the source range.
2637          */
2638         max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2639         do {
2640                 lr = ((int64_t)random() << 32) + random();
2641                 off2 = (off64_t)(lr % max_off2);
2642                 off2 %= maxfsize;
2643         } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2644
2645         /*
2646          * Since len, off1 and off2 will be changed later, preserve their
2647          * original values.
2648          */
2649         length = len;
2650         offset1 = off1;
2651         offset2 = off2;
2652
2653         while (len > 0) {
2654                 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2655                               len, 0);
2656                 if (ret < 0) {
2657                         if (errno != EAGAIN || tries++ >= 300)
2658                                 break;
2659                 } else if (ret > len || ret == 0)
2660                         break;
2661                 else if (ret > 0)
2662                         len -= ret;
2663         }
2664         e = ret < 0 ? errno : 0;
2665         if (v1 || v2) {
2666                 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2667                         procid, opno,
2668                         fpath1.path, inoinfo1,
2669                         (long long)offset1, (long long)length,
2670                         fpath2.path, inoinfo2,
2671                         (long long)offset2, (long long)length);
2672
2673                 if (ret < 0)
2674                         printf(" error %d", e);
2675                 else if (len && ret > len)
2676                         printf(" asked for %lld, copied %lld??\n",
2677                                 (long long)len, (long long)ret);
2678                 printf("\n");
2679         }
2680
2681 out_fd2:
2682         close(fd2);
2683 out_fd1:
2684         close(fd1);
2685 out_fpath2:
2686         free_pathname(&fpath2);
2687 out_fpath1:
2688         free_pathname(&fpath1);
2689 #endif
2690 }
2691
2692 /* dedupe some arbitrary range of f1 to f2...fn. */
2693 void
2694 deduperange_f(
2695         int                     opno,
2696         long                    r)
2697 {
2698 #ifdef FIDEDUPERANGE
2699 #define INFO_SZ                 1024
2700         struct file_dedupe_range *fdr;
2701         struct pathname         *fpath;
2702         struct stat64           *stat;
2703         char                    *info;
2704         off64_t                 *off;
2705         int                     *v;
2706         int                     *fd;
2707         int                     nr;
2708         off64_t                 lr;
2709         size_t                  len;
2710         int                     ret;
2711         int                     i;
2712         int                     e;
2713
2714         if (flist[FT_REG].nfiles < 2)
2715                 return;
2716
2717         /* Pick somewhere between 2 and 128 files. */
2718         do {
2719                 nr = random() % (flist[FT_REG].nfiles + 1);
2720         } while (nr < 2 || nr > 128);
2721
2722         /* Alloc memory */
2723         fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2724                      sizeof(struct file_dedupe_range));
2725         if (!fdr) {
2726                 printf("%d/%d: line %d error %d\n",
2727                         procid, opno, __LINE__, errno);
2728                 return;
2729         }
2730         memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2731                         sizeof(struct file_dedupe_range)));
2732
2733         fpath = calloc(nr, sizeof(struct pathname));
2734         if (!fpath) {
2735                 printf("%d/%d: line %d error %d\n",
2736                         procid, opno, __LINE__, errno);
2737                 goto out_fdr;
2738         }
2739
2740         stat = calloc(nr, sizeof(struct stat64));
2741         if (!stat) {
2742                 printf("%d/%d: line %d error %d\n",
2743                         procid, opno, __LINE__, errno);
2744                 goto out_paths;
2745         }
2746
2747         info = calloc(nr, INFO_SZ);
2748         if (!info) {
2749                 printf("%d/%d: line %d error %d\n",
2750                         procid, opno, __LINE__, errno);
2751                 goto out_stats;
2752         }
2753
2754         off = calloc(nr, sizeof(off64_t));
2755         if (!off) {
2756                 printf("%d/%d: line %d error %d\n",
2757                         procid, opno, __LINE__, errno);
2758                 goto out_info;
2759         }
2760
2761         v = calloc(nr, sizeof(int));
2762         if (!v) {
2763                 printf("%d/%d: line %d error %d\n",
2764                         procid, opno, __LINE__, errno);
2765                 goto out_offsets;
2766         }
2767         fd = calloc(nr, sizeof(int));
2768         if (!fd) {
2769                 printf("%d/%d: line %d error %d\n",
2770                         procid, opno, __LINE__, errno);
2771                 goto out_v;
2772         }
2773         memset(fd, 0xFF, nr * sizeof(int));
2774
2775         /* Get paths for all files */
2776         for (i = 0; i < nr; i++)
2777                 init_pathname(&fpath[i]);
2778
2779         if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2780                 if (v[0])
2781                         printf("%d/%d: deduperange read - no filename\n",
2782                                 procid, opno);
2783                 goto out_pathnames;
2784         }
2785
2786         for (i = 1; i < nr; i++) {
2787                 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2788                         if (v[i])
2789                                 printf("%d/%d: deduperange write - no filename\n",
2790                                         procid, opno);
2791                         goto out_pathnames;
2792                 }
2793         }
2794
2795         /* Open files */
2796         fd[0] = open_path(&fpath[0], O_RDONLY);
2797         e = fd[0] < 0 ? errno : 0;
2798         check_cwd();
2799         if (fd[0] < 0) {
2800                 if (v[0])
2801                         printf("%d/%d: deduperange read - open %s failed %d\n",
2802                                 procid, opno, fpath[0].path, e);
2803                 goto out_pathnames;
2804         }
2805
2806         for (i = 1; i < nr; i++) {
2807                 fd[i] = open_path(&fpath[i], O_WRONLY);
2808                 e = fd[i] < 0 ? errno : 0;
2809                 check_cwd();
2810                 if (fd[i] < 0) {
2811                         if (v[i])
2812                                 printf("%d/%d: deduperange write - open %s failed %d\n",
2813                                         procid, opno, fpath[i].path, e);
2814                         goto out_fds;
2815                 }
2816         }
2817
2818         /* Get file stats */
2819         if (fstat64(fd[0], &stat[0]) < 0) {
2820                 if (v[0])
2821                         printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2822                                 procid, opno, fpath[0].path, errno);
2823                 goto out_fds;
2824         }
2825
2826         inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2827
2828         for (i = 1; i < nr; i++) {
2829                 if (fstat64(fd[i], &stat[i]) < 0) {
2830                         if (v[i])
2831                                 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2832                                         procid, opno, fpath[i].path, errno);
2833                         goto out_fds;
2834                 }
2835                 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2836         }
2837
2838         /* Never try to dedupe more than half of the src file. */
2839         len = (random() % FILELEN_MAX) + 1;
2840         len &= ~(stat[0].st_blksize - 1);
2841         if (len == 0)
2842                 len = stat[0].st_blksize / 2;
2843         if (len > stat[0].st_size / 2)
2844                 len = stat[0].st_size / 2;
2845
2846         /* Calculate offsets */
2847         lr = ((int64_t)random() << 32) + random();
2848         if (stat[0].st_size == len)
2849                 off[0] = 0;
2850         else
2851                 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2852         off[0] %= maxfsize;
2853         off[0] &= ~(stat[0].st_blksize - 1);
2854
2855         /*
2856          * If srcfile == destfile[i], randomly generate destination ranges
2857          * until we find one that doesn't overlap the source range.
2858          */
2859         for (i = 1; i < nr; i++) {
2860                 int     tries = 0;
2861
2862                 do {
2863                         lr = ((int64_t)random() << 32) + random();
2864                         if (stat[i].st_size <= len)
2865                                 off[i] = 0;
2866                         else
2867                                 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2868                         off[i] %= maxfsize;
2869                         off[i] &= ~(stat[i].st_blksize - 1);
2870                 } while (stat[0].st_ino == stat[i].st_ino &&
2871                          llabs(off[i] - off[0]) < len &&
2872                          tries++ < 10);
2873         }
2874
2875         /* Clone data blocks */
2876         fdr->src_offset = off[0];
2877         fdr->src_length = len;
2878         fdr->dest_count = nr - 1;
2879         for (i = 1; i < nr; i++) {
2880                 fdr->info[i - 1].dest_fd = fd[i];
2881                 fdr->info[i - 1].dest_offset = off[i];
2882         }
2883
2884         ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2885         e = ret < 0 ? errno : 0;
2886         if (v[0]) {
2887                 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2888                         procid, opno,
2889                         fpath[0].path, &info[0], (long long)off[0],
2890                         (long long)len);
2891                 if (ret < 0)
2892                         printf(" error %d", e);
2893                 printf("\n");
2894         }
2895         if (ret < 0)
2896                 goto out_fds;
2897
2898         for (i = 1; i < nr; i++) {
2899                 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2900                 if (v[i]) {
2901                         printf("%d/%d: ...to %s%s [%lld,%lld]",
2902                                 procid, opno,
2903                                 fpath[i].path, &info[i * INFO_SZ],
2904                                 (long long)off[i], (long long)len);
2905                         if (fdr->info[i - 1].status < 0)
2906                                 printf(" error %d", e);
2907                         if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2908                                 printf(" %llu bytes deduplicated",
2909                                         fdr->info[i - 1].bytes_deduped);
2910                         if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2911                                 printf(" differed");
2912                         printf("\n");
2913                 }
2914         }
2915
2916 out_fds:
2917         for (i = 0; i < nr; i++)
2918                 if (fd[i] >= 0)
2919                         close(fd[i]);
2920 out_pathnames:
2921         for (i = 0; i < nr; i++)
2922                 free_pathname(&fpath[i]);
2923
2924         free(fd);
2925 out_v:
2926         free(v);
2927 out_offsets:
2928         free(off);
2929 out_info:
2930         free(info);
2931 out_stats:
2932         free(stat);
2933 out_paths:
2934         free(fpath);
2935 out_fdr:
2936         free(fdr);
2937 #endif
2938 }
2939
2940 void
2941 setxattr_f(int opno, long r)
2942 {
2943 #ifdef XFS_XFLAG_EXTSIZE
2944         struct fsxattr  fsx;
2945         int             fd;
2946         int             e;
2947         pathname_t      f;
2948         int             nbits;
2949         uint            p;
2950         int             v;
2951
2952         init_pathname(&f);
2953         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2954                 append_pathname(&f, ".");
2955         fd = open_path(&f, O_RDWR);
2956         e = fd < 0 ? errno : 0;
2957         check_cwd();
2958
2959         /* project ID */
2960         p = (uint)random();
2961         e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2962         nbits = (int)(random() % e);
2963         p &= (1 << nbits) - 1;
2964
2965         if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2966                 fsx.fsx_projid = p;
2967                 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2968         }
2969         if (v)
2970                 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2971         free_pathname(&f);
2972         close(fd);
2973 #endif
2974 }
2975
2976 void
2977 splice_f(int opno, long r)
2978 {
2979         struct pathname         fpath1;
2980         struct pathname         fpath2;
2981         struct stat64           stat1;
2982         struct stat64           stat2;
2983         char                    inoinfo1[1024];
2984         char                    inoinfo2[1024];
2985         loff_t                  lr;
2986         loff_t                  off1, off2;
2987         size_t                  len;
2988         loff_t                  offset1, offset2;
2989         size_t                  length;
2990         size_t                  total;
2991         int                     v1;
2992         int                     v2;
2993         int                     fd1;
2994         int                     fd2;
2995         ssize_t                 ret1 = 0, ret2 = 0;
2996         size_t                  bytes;
2997         int                     e;
2998         int                     filedes[2];
2999
3000         /* Load paths */
3001         init_pathname(&fpath1);
3002         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3003                 if (v1)
3004                         printf("%d/%d: splice read - no filename\n",
3005                                 procid, opno);
3006                 goto out_fpath1;
3007         }
3008
3009         init_pathname(&fpath2);
3010         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3011                 if (v2)
3012                         printf("%d/%d: splice write - no filename\n",
3013                                 procid, opno);
3014                 goto out_fpath2;
3015         }
3016
3017         /* Open files */
3018         fd1 = open_path(&fpath1, O_RDONLY);
3019         e = fd1 < 0 ? errno : 0;
3020         check_cwd();
3021         if (fd1 < 0) {
3022                 if (v1)
3023                         printf("%d/%d: splice read - open %s failed %d\n",
3024                                 procid, opno, fpath1.path, e);
3025                 goto out_fpath2;
3026         }
3027
3028         fd2 = open_path(&fpath2, O_WRONLY);
3029         e = fd2 < 0 ? errno : 0;
3030         check_cwd();
3031         if (fd2 < 0) {
3032                 if (v2)
3033                         printf("%d/%d: splice write - open %s failed %d\n",
3034                                 procid, opno, fpath2.path, e);
3035                 goto out_fd1;
3036         }
3037
3038         /* Get file stats */
3039         if (fstat64(fd1, &stat1) < 0) {
3040                 if (v1)
3041                         printf("%d/%d: splice read - fstat64 %s failed %d\n",
3042                                 procid, opno, fpath1.path, errno);
3043                 goto out_fd2;
3044         }
3045         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3046
3047         if (fstat64(fd2, &stat2) < 0) {
3048                 if (v2)
3049                         printf("%d/%d: splice write - fstat64 %s failed %d\n",
3050                                 procid, opno, fpath2.path, errno);
3051                 goto out_fd2;
3052         }
3053         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3054
3055         /* Calculate offsets */
3056         len = (random() % FILELEN_MAX) + 1;
3057         if (len == 0)
3058                 len = stat1.st_blksize;
3059         if (len > stat1.st_size)
3060                 len = stat1.st_size;
3061
3062         lr = ((int64_t)random() << 32) + random();
3063         if (stat1.st_size == len)
3064                 off1 = 0;
3065         else
3066                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3067         off1 %= maxfsize;
3068
3069         /*
3070          * splice can overlap write, so the offset of the target file can be
3071          * any number. But to avoid too large offset, add a clamp of 1024 blocks
3072          * past the current dest file EOF
3073          */
3074         lr = ((int64_t)random() << 32) + random();
3075         off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3076
3077         /*
3078          * Since len, off1 and off2 will be changed later, preserve their
3079          * original values.
3080          */
3081         length = len;
3082         offset1 = off1;
3083         offset2 = off2;
3084
3085         /* Pipe initialize */
3086         if (pipe(filedes) < 0) {
3087                 if (v1 || v2) {
3088                         printf("%d/%d: splice - pipe failed %d\n",
3089                                 procid, opno, errno);
3090                         goto out_fd2;
3091                 }
3092         }
3093
3094         bytes = 0;
3095         total = 0;
3096         while (len > 0) {
3097                 /* move to pipe buffer */
3098                 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3099                 if (ret1 <= 0) {
3100                         break;
3101                 }
3102                 bytes = ret1;
3103
3104                 /* move from pipe buffer to dst file */
3105                 while (bytes > 0) {
3106                         ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3107                         if (ret2 < 0) {
3108                                 break;
3109                         }
3110                         bytes -= ret2;
3111                 }
3112                 if (ret2 < 0)
3113                         break;
3114
3115                 len -= ret1;
3116                 total += ret1;
3117         }
3118
3119         if (ret1 < 0 || ret2 < 0)
3120                 e = errno;
3121         else
3122                 e = 0;
3123         if (v1 || v2) {
3124                 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3125                         procid, opno,
3126                         fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3127                         fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3128
3129                 if (length && length > total)
3130                         printf(" asked for %lld, spliced %lld??\n",
3131                                 (long long)length, (long long)total);
3132                 printf("\n");
3133         }
3134
3135         close(filedes[0]);
3136         close(filedes[1]);
3137 out_fd2:
3138         close(fd2);
3139 out_fd1:
3140         close(fd1);
3141 out_fpath2:
3142         free_pathname(&fpath2);
3143 out_fpath1:
3144         free_pathname(&fpath1);
3145 }
3146
3147 void
3148 creat_f(int opno, long r)
3149 {
3150         struct fsxattr  a;
3151         int             e;
3152         int             e1;
3153         int             extsize;
3154         pathname_t      f;
3155         int             fd;
3156         fent_t          *fep;
3157         int             id;
3158         int             parid;
3159         int             type;
3160         int             v;
3161         int             v1;
3162
3163         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
3164                 parid = -1;
3165         else
3166                 parid = fep->id;
3167         init_pathname(&f);
3168         e1 = (random() % 100);
3169         type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3170 #ifdef NOTYET
3171         if (type == FT_RTF)     /* rt always gets an extsize */
3172                 extsize = (random() % 10) + 1;
3173         else if (e1 < 10)       /* one-in-ten get an extsize */
3174                 extsize = random() % 1024;
3175         else
3176 #endif
3177                 extsize = 0;
3178         e = generate_fname(fep, type, &f, &id, &v);
3179         v |= v1;
3180         if (!e) {
3181                 if (v) {
3182                         (void)fent_to_name(&f, fep);
3183                         printf("%d/%d: creat - no filename from %s\n",
3184                                 procid, opno, f.path);
3185                 }
3186                 free_pathname(&f);
3187                 return;
3188         }
3189         fd = creat_path(&f, 0666);
3190         e = fd < 0 ? errno : 0;
3191         e1 = 0;
3192         check_cwd();
3193         if (fd >= 0) {
3194                 if (extsize &&
3195                     xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3196                         if (type == FT_RTF) {
3197                                 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3198                                 a.fsx_extsize = extsize *
3199                                                 geom.rtextsize * geom.blocksize;
3200 #ifdef NOTYET
3201                         } else if (extsize) {
3202                                 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3203                                 a.fsx_extsize = extsize * geom.blocksize;
3204 #endif
3205                         }
3206                         if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3207                                 e1 = errno;
3208                 }
3209                 add_to_flist(type, id, parid, 0);
3210                 close(fd);
3211         }
3212         if (v) {
3213                 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3214                         extsize ? a.fsx_extsize : 0, e, e1);
3215                 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3216         }
3217         free_pathname(&f);
3218 }
3219
3220 void
3221 dread_f(int opno, long r)
3222 {
3223         int64_t         align;
3224         char            *buf;
3225         struct dioattr  diob;
3226         int             e;
3227         pathname_t      f;
3228         int             fd;
3229         size_t          len;
3230         int64_t         lr;
3231         off64_t         off;
3232         struct stat64   stb;
3233         int             v;
3234         char            st[1024];
3235         char            *dio_env;
3236
3237         init_pathname(&f);
3238         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3239                 if (v)
3240                         printf("%d/%d: dread - no filename\n", procid, opno);
3241                 free_pathname(&f);
3242                 return;
3243         }
3244         fd = open_path(&f, O_RDONLY|O_DIRECT);
3245         e = fd < 0 ? errno : 0;
3246         check_cwd();
3247         if (fd < 0) {
3248                 if (v)
3249                         printf("%d/%d: dread - open %s failed %d\n",
3250                                 procid, opno, f.path, e);
3251                 free_pathname(&f);
3252                 return;
3253         }
3254         if (fstat64(fd, &stb) < 0) {
3255                 if (v)
3256                         printf("%d/%d: dread - fstat64 %s failed %d\n",
3257                                procid, opno, f.path, errno);
3258                 free_pathname(&f);
3259                 close(fd);
3260                 return;
3261         }
3262         inode_info(st, sizeof(st), &stb, v);
3263         if (stb.st_size == 0) {
3264                 if (v)
3265                         printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3266                                f.path, st);
3267                 free_pathname(&f);
3268                 close(fd);
3269                 return;
3270         }
3271         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3272                 if (v)
3273                         printf(
3274                         "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3275                         " fallback to stat()\n",
3276                                 procid, opno, f.path, st, errno);
3277                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3278                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3279         }
3280
3281         dio_env = getenv("XFS_DIO_MIN");
3282         if (dio_env)
3283                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3284
3285         align = (int64_t)diob.d_miniosz;
3286         lr = ((int64_t)random() << 32) + random();
3287         off = (off64_t)(lr % stb.st_size);
3288         off -= (off % align);
3289         lseek64(fd, off, SEEK_SET);
3290         len = (random() % FILELEN_MAX) + 1;
3291         len -= (len % align);
3292         if (len <= 0)
3293                 len = align;
3294         else if (len > diob.d_maxiosz) 
3295                 len = diob.d_maxiosz;
3296         buf = memalign(diob.d_mem, len);
3297         e = read(fd, buf, len) < 0 ? errno : 0;
3298         free(buf);
3299         if (v)
3300                 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3301                        procid, opno, f.path, st, (long long)off, (int)len, e);
3302         free_pathname(&f);
3303         close(fd);
3304 }
3305
3306 void
3307 dwrite_f(int opno, long r)
3308 {
3309         int64_t         align;
3310         char            *buf;
3311         struct dioattr  diob;
3312         int             e;
3313         pathname_t      f;
3314         int             fd;
3315         size_t          len;
3316         int64_t         lr;
3317         off64_t         off;
3318         struct stat64   stb;
3319         int             v;
3320         char            st[1024];
3321         char            *dio_env;
3322
3323         init_pathname(&f);
3324         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3325                 if (v)
3326                         printf("%d/%d: dwrite - no filename\n", procid, opno);
3327                 free_pathname(&f);
3328                 return;
3329         }
3330         fd = open_path(&f, O_WRONLY|O_DIRECT);
3331         e = fd < 0 ? errno : 0;
3332         check_cwd();
3333         if (fd < 0) {
3334                 if (v)
3335                         printf("%d/%d: dwrite - open %s failed %d\n",
3336                                 procid, opno, f.path, e);
3337                 free_pathname(&f);
3338                 return;
3339         }
3340         if (fstat64(fd, &stb) < 0) {
3341                 if (v)
3342                         printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3343                                 procid, opno, f.path, errno);
3344                 free_pathname(&f);
3345                 close(fd);
3346                 return;
3347         }
3348         inode_info(st, sizeof(st), &stb, v);
3349         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3350                 if (v)
3351                         printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3352                                 " %s%s return %d, fallback to stat()\n",
3353                                procid, opno, f.path, st, errno);
3354                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3355                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3356         }
3357
3358         dio_env = getenv("XFS_DIO_MIN");
3359         if (dio_env)
3360                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3361
3362         align = (int64_t)diob.d_miniosz;
3363         lr = ((int64_t)random() << 32) + random();
3364         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3365         off -= (off % align);
3366         lseek64(fd, off, SEEK_SET);
3367         len = (random() % FILELEN_MAX) + 1;
3368         len -= (len % align);
3369         if (len <= 0)
3370                 len = align;
3371         else if (len > diob.d_maxiosz) 
3372                 len = diob.d_maxiosz;
3373         buf = memalign(diob.d_mem, len);
3374         off %= maxfsize;
3375         lseek64(fd, off, SEEK_SET);
3376         memset(buf, nameseq & 0xff, len);
3377         e = write(fd, buf, len) < 0 ? errno : 0;
3378         free(buf);
3379         if (v)
3380                 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3381                        procid, opno, f.path, st, (long long)off, (int)len, e);
3382         free_pathname(&f);
3383         close(fd);
3384 }
3385
3386
3387 #ifdef HAVE_LINUX_FALLOC_H
3388 struct print_flags falloc_flags [] = {
3389         { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3390         { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3391         { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3392         { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3393         { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3394         { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3395         { -1, NULL}
3396 };
3397
3398 #define translate_falloc_flags(mode)    \
3399         ({translate_flags(mode, "|", falloc_flags);})
3400 #endif
3401
3402 void
3403 do_fallocate(int opno, long r, int mode)
3404 {
3405 #ifdef HAVE_LINUX_FALLOC_H
3406         int             e;
3407         pathname_t      f;
3408         int             fd;
3409         int64_t         lr;
3410         off64_t         off;
3411         off64_t         len;
3412         struct stat64   stb;
3413         int             v;
3414         char            st[1024];
3415
3416         init_pathname(&f);
3417         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3418                 if (v)
3419                         printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3420                 free_pathname(&f);
3421                 return;
3422         }
3423         fd = open_path(&f, O_RDWR);
3424         if (fd < 0) {
3425                 if (v)
3426                         printf("%d/%d: do_fallocate - open %s failed %d\n",
3427                                 procid, opno, f.path, errno);
3428                 free_pathname(&f);
3429                 return;
3430         }
3431         check_cwd();
3432         if (fstat64(fd, &stb) < 0) {
3433                 if (v)
3434                         printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3435                                 procid, opno, f.path, errno);
3436                 free_pathname(&f);
3437                 close(fd);
3438                 return;
3439         }
3440         inode_info(st, sizeof(st), &stb, v);
3441         lr = ((int64_t)random() << 32) + random();
3442         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3443         off %= maxfsize;
3444         len = (off64_t)(random() % (1024 * 1024));
3445         /*
3446          * Collapse/insert range requires off and len to be block aligned,
3447          * make it more likely to be the case.
3448          */
3449         if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3450                 (opno % 2)) {
3451                 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3452                 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3453         }
3454         mode |= FALLOC_FL_KEEP_SIZE & random();
3455         e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3456         if (v)
3457                 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3458                        procid, opno, translate_falloc_flags(mode),
3459                        f.path, st, (long long)off, (long long)len, e);
3460         free_pathname(&f);
3461         close(fd);
3462 #endif
3463 }
3464
3465 void
3466 fallocate_f(int opno, long r)
3467 {
3468 #ifdef HAVE_LINUX_FALLOC_H
3469         do_fallocate(opno, r, 0);
3470 #endif
3471 }
3472
3473 void
3474 fdatasync_f(int opno, long r)
3475 {
3476         int             e;
3477         pathname_t      f;
3478         int             fd;
3479         int             v;
3480
3481         init_pathname(&f);
3482         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3483                 if (v)
3484                         printf("%d/%d: fdatasync - no filename\n",
3485                                 procid, opno);
3486                 free_pathname(&f);
3487                 return;
3488         }
3489         fd = open_path(&f, O_WRONLY);
3490         e = fd < 0 ? errno : 0;
3491         check_cwd();
3492         if (fd < 0) {
3493                 if (v)
3494                         printf("%d/%d: fdatasync - open %s failed %d\n",
3495                                 procid, opno, f.path, e);
3496                 free_pathname(&f);
3497                 return;
3498         }
3499         e = fdatasync(fd) < 0 ? errno : 0;
3500         if (v)
3501                 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3502         free_pathname(&f);
3503         close(fd);
3504 }
3505
3506 #ifdef HAVE_LINUX_FIEMAP_H
3507 struct print_flags fiemap_flags[] = {
3508         { FIEMAP_FLAG_SYNC, "SYNC"},
3509         { FIEMAP_FLAG_XATTR, "XATTR"},
3510         { -1, NULL}
3511 };
3512
3513 #define translate_fiemap_flags(mode)    \
3514         ({translate_flags(mode, "|", fiemap_flags);})
3515 #endif
3516
3517 void
3518 fiemap_f(int opno, long r)
3519 {
3520 #ifdef HAVE_LINUX_FIEMAP_H
3521         int             e;
3522         pathname_t      f;
3523         int             fd;
3524         int64_t         lr;
3525         off64_t         off;
3526         struct stat64   stb;
3527         int             v;
3528         char            st[1024];
3529         int blocks_to_map;
3530         struct fiemap *fiemap;
3531
3532         init_pathname(&f);
3533         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3534                 if (v)
3535                         printf("%d/%d: fiemap - no filename\n", procid, opno);
3536                 free_pathname(&f);
3537                 return;
3538         }
3539         fd = open_path(&f, O_RDWR);
3540         e = fd < 0 ? errno : 0;
3541         check_cwd();
3542         if (fd < 0) {
3543                 if (v)
3544                         printf("%d/%d: fiemap - open %s failed %d\n",
3545                                 procid, opno, f.path, e);
3546                 free_pathname(&f);
3547                 return;
3548         }
3549         if (fstat64(fd, &stb) < 0) {
3550                 if (v)
3551                         printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3552                                 procid, opno, f.path, errno);
3553                 free_pathname(&f);
3554                 close(fd);
3555                 return;
3556         }
3557         inode_info(st, sizeof(st), &stb, v);
3558         blocks_to_map = random() & 0xffff;
3559         fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3560                         (blocks_to_map * sizeof(struct fiemap_extent)));
3561         if (!fiemap) {
3562                 if (v)
3563                         printf("%d/%d: malloc failed \n", procid, opno);
3564                 free_pathname(&f);
3565                 close(fd);
3566                 return;
3567         }
3568         lr = ((int64_t)random() << 32) + random();
3569         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3570         off %= maxfsize;
3571         fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3572         fiemap->fm_extent_count = blocks_to_map;
3573         fiemap->fm_mapped_extents = random() & 0xffff;
3574         fiemap->fm_start = off;
3575         fiemap->fm_length = ((int64_t)random() << 32) + random();
3576
3577         e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3578         if (v)
3579                 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3580                        procid, opno, f.path, st, (long long)fiemap->fm_start,
3581                        (long long) fiemap->fm_length,
3582                        translate_fiemap_flags(fiemap->fm_flags), e);
3583         free(fiemap);
3584         free_pathname(&f);
3585         close(fd);
3586 #endif
3587 }
3588
3589 void
3590 freesp_f(int opno, long r)
3591 {
3592         int             e;
3593         pathname_t      f;
3594         int             fd;
3595         struct xfs_flock64      fl;
3596         int64_t         lr;
3597         off64_t         off;
3598         struct stat64   stb;
3599         int             v;
3600         char            st[1024];
3601
3602         init_pathname(&f);
3603         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3604                 if (v)
3605                         printf("%d/%d: freesp - no filename\n", procid, opno);
3606                 free_pathname(&f);
3607                 return;
3608         }
3609         fd = open_path(&f, O_RDWR);
3610         e = fd < 0 ? errno : 0;
3611         check_cwd();
3612         if (fd < 0) {
3613                 if (v)
3614                         printf("%d/%d: freesp - open %s failed %d\n",
3615                                 procid, opno, f.path, e);
3616                 free_pathname(&f);
3617                 return;
3618         }
3619         if (fstat64(fd, &stb) < 0) {
3620                 if (v)
3621                         printf("%d/%d: freesp - fstat64 %s failed %d\n",
3622                                 procid, opno, f.path, errno);
3623                 free_pathname(&f);
3624                 close(fd);
3625                 return;
3626         }
3627         inode_info(st, sizeof(st), &stb, v);
3628         lr = ((int64_t)random() << 32) + random();
3629         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3630         off %= maxfsize;
3631         fl.l_whence = SEEK_SET;
3632         fl.l_start = off;
3633         fl.l_len = 0;
3634         e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3635         if (v)
3636                 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3637                        procid, opno, f.path, st, (long long)off, e);
3638         free_pathname(&f);
3639         close(fd);
3640 }
3641
3642 void
3643 fsync_f(int opno, long r)
3644 {
3645         int             e;
3646         pathname_t      f;
3647         int             fd;
3648         int             v;
3649
3650         init_pathname(&f);
3651         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3652                 if (v)
3653                         printf("%d/%d: fsync - no filename\n", procid, opno);
3654                 free_pathname(&f);
3655                 return;
3656         }
3657         fd = open_file_or_dir(&f, O_WRONLY);
3658         e = fd < 0 ? errno : 0;
3659         check_cwd();
3660         if (fd < 0) {
3661                 if (v)
3662                         printf("%d/%d: fsync - open %s failed %d\n",
3663                                 procid, opno, f.path, e);
3664                 free_pathname(&f);
3665                 return;
3666         }
3667         e = fsync(fd) < 0 ? errno : 0;
3668         if (v)
3669                 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3670         free_pathname(&f);
3671         close(fd);
3672 }
3673
3674 char *
3675 gen_random_string(int len)
3676 {
3677         static const char charset[] = "0123456789"
3678                 "abcdefghijklmnopqrstuvwxyz"
3679                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3680         int i;
3681         char *s;
3682
3683         if (len == 0)
3684                 return NULL;
3685
3686         s = malloc(len);
3687         if (!s)
3688                 return NULL;
3689
3690         for (i = 0; i < len; i++)
3691                 s[i] = charset[random() % sizeof(charset)];
3692
3693         return s;
3694 }
3695
3696 void
3697 getattr_f(int opno, long r)
3698 {
3699         int             fd;
3700         int             e;
3701         pathname_t      f;
3702         uint            fl;
3703         int             v;
3704
3705         init_pathname(&f);
3706         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3707                 append_pathname(&f, ".");
3708         fd = open_path(&f, O_RDWR);
3709         e = fd < 0 ? errno : 0;
3710         check_cwd();
3711
3712         e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3713         if (v)
3714                 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3715         free_pathname(&f);
3716         close(fd);
3717 }
3718
3719 void
3720 getdents_f(int opno, long r)
3721 {
3722         DIR             *dir;
3723         pathname_t      f;
3724         int             v;
3725
3726         init_pathname(&f);
3727         if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3728                 append_pathname(&f, ".");
3729         dir = opendir_path(&f);
3730         check_cwd();
3731         if (dir == NULL) {
3732                 if (v)
3733                         printf("%d/%d: getdents - can't open %s\n",
3734                                 procid, opno, f.path);
3735                 free_pathname(&f);
3736                 return;
3737         }
3738         while (readdir64(dir) != NULL)
3739                 continue;
3740         if (v)
3741                 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3742         free_pathname(&f);
3743         closedir(dir);
3744 }
3745
3746 void
3747 getfattr_f(int opno, long r)
3748 {
3749         fent_t          *fep;
3750         int             e;
3751         pathname_t      f;
3752         int             v;
3753         char            name[XATTR_NAME_BUF_SIZE];
3754         char            *value = NULL;
3755         int             value_len;
3756         int             xattr_num;
3757
3758         init_pathname(&f);
3759         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3760                 if (v)
3761                         printf("%d/%d: getfattr - no filename\n", procid, opno);
3762                 goto out;
3763         }
3764         check_cwd();
3765
3766         /*
3767          * If the file/dir has xattrs, pick one randomly, otherwise attempt
3768          * to read a xattr that doesn't exist (fgetxattr should fail with
3769          * errno set to ENOATTR (61) in this case).
3770          */
3771         if (fep->xattr_counter > 0)
3772                 xattr_num = (random() % fep->xattr_counter) + 1;
3773         else
3774                 xattr_num = 0;
3775
3776         e = generate_xattr_name(xattr_num, name, sizeof(name));
3777         if (e < 0) {
3778                 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3779                        procid, opno, f.path, e);
3780                 goto out;
3781         }
3782
3783         value_len = getxattr(f.path, name, NULL, 0);
3784         if (value_len < 0) {
3785                 if (v)
3786                         printf("%d/%d: getfattr file %s name %s failed %d\n",
3787                                procid, opno, f.path, name, errno);
3788                 goto out;
3789         }
3790
3791         /* A xattr without value.*/
3792         if (value_len == 0) {
3793                 e = 0;
3794                 goto out_log;
3795         }
3796
3797         value = malloc(value_len);
3798         if (!value) {
3799                 if (v)
3800                         printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3801                                procid, opno, f.path, value_len);
3802                 goto out;
3803         }
3804
3805         e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3806 out_log:
3807         if (v)
3808                 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3809                        procid, opno, f.path, name, value_len, e);
3810 out:
3811         free(value);
3812         free_pathname(&f);
3813 }
3814
3815 void
3816 link_f(int opno, long r)
3817 {
3818         int             e;
3819         pathname_t      f;
3820         fent_t          *fep;
3821         fent_t          *fep_src;
3822         flist_t         *flp;
3823         int             id;
3824         pathname_t      l;
3825         int             parid;
3826         int             v;
3827         int             v1;
3828
3829         init_pathname(&f);
3830         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3831                 if (v1)
3832                         printf("%d/%d: link - no file\n", procid, opno);
3833                 free_pathname(&f);
3834                 return;
3835         }
3836         if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3837                 parid = -1;
3838         else
3839                 parid = fep->id;
3840         v |= v1;
3841         init_pathname(&l);
3842         e = generate_fname(fep, flp - flist, &l, &id, &v1);
3843         v |= v1;
3844         if (!e) {
3845                 if (v) {
3846                         (void)fent_to_name(&l, fep);
3847                         printf("%d/%d: link - no filename from %s\n",
3848                                 procid, opno, l.path);
3849                 }
3850                 free_pathname(&l);
3851                 free_pathname(&f);
3852                 return;
3853         }
3854         e = link_path(&f, &l) < 0 ? errno : 0;
3855         check_cwd();
3856         if (e == 0)
3857                 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3858         if (v) {
3859                 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3860                         e);
3861                 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3862         }
3863         free_pathname(&l);
3864         free_pathname(&f);
3865 }
3866
3867 void
3868 listfattr_f(int opno, long r)
3869 {
3870         fent_t          *fep;
3871         int             e;
3872         pathname_t      f;
3873         int             v;
3874         char            *buffer = NULL;
3875         int             buffer_len;
3876
3877         init_pathname(&f);
3878         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3879                 if (v)
3880                         printf("%d/%d: listfattr - no filename\n", procid, opno);
3881                 goto out;
3882         }
3883         check_cwd();
3884
3885         e = listxattr(f.path, NULL, 0);
3886         if (e < 0) {
3887                 if (v)
3888                         printf("%d/%d: listfattr %s failed %d\n",
3889                                procid, opno, f.path, errno);
3890                 goto out;
3891         }
3892         buffer_len = e;
3893         if (buffer_len == 0) {
3894                 if (v)
3895                         printf("%d/%d: listfattr %s - has no extended attributes\n",
3896                                procid, opno, f.path);
3897                 goto out;
3898         }
3899
3900         buffer = malloc(buffer_len);
3901         if (!buffer) {
3902                 if (v)
3903                         printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3904                                procid, opno, f.path, buffer_len);
3905                 goto out;
3906         }
3907
3908         e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3909         if (v)
3910                 printf("%d/%d: listfattr %s buffer length %d %d\n",
3911                        procid, opno, f.path, buffer_len, e);
3912 out:
3913         free(buffer);
3914         free_pathname(&f);
3915 }
3916
3917 void
3918 mkdir_f(int opno, long r)
3919 {
3920         int             e;
3921         pathname_t      f;
3922         fent_t          *fep;
3923         int             id;
3924         int             parid;
3925         int             v;
3926         int             v1;
3927
3928         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3929                 parid = -1;
3930         else
3931                 parid = fep->id;
3932         init_pathname(&f);
3933         e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3934         v |= v1;
3935         if (!e) {
3936                 if (v) {
3937                         (void)fent_to_name(&f, fep);
3938                         printf("%d/%d: mkdir - no filename from %s\n",
3939                                 procid, opno, f.path);
3940                 }
3941                 free_pathname(&f);
3942                 return;
3943         }
3944         e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3945         check_cwd();
3946         if (e == 0)
3947                 add_to_flist(FT_DIR, id, parid, 0);
3948         if (v) {
3949                 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3950                 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3951         }
3952         free_pathname(&f);
3953 }
3954
3955 void
3956 mknod_f(int opno, long r)
3957 {
3958         int             e;
3959         pathname_t      f;
3960         fent_t          *fep;
3961         int             id;
3962         int             parid;
3963         int             v;
3964         int             v1;
3965
3966         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3967                 parid = -1;
3968         else
3969                 parid = fep->id;
3970         init_pathname(&f);
3971         e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3972         v |= v1;
3973         if (!e) {
3974                 if (v) {
3975                         (void)fent_to_name(&f, fep);
3976                         printf("%d/%d: mknod - no filename from %s\n",
3977                                 procid, opno, f.path);
3978                 }
3979                 free_pathname(&f);
3980                 return;
3981         }
3982         e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3983         check_cwd();
3984         if (e == 0)
3985                 add_to_flist(FT_DEV, id, parid, 0);
3986         if (v) {
3987                 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3988                 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3989         }
3990         free_pathname(&f);
3991 }
3992
3993 #ifdef HAVE_SYS_MMAN_H
3994 struct print_flags mmap_flags[] = {
3995         { MAP_SHARED, "SHARED"},
3996         { MAP_PRIVATE, "PRIVATE"},
3997         { -1, NULL}
3998 };
3999
4000 #define translate_mmap_flags(flags)       \
4001         ({translate_flags(flags, "|", mmap_flags);})
4002 #endif
4003
4004 void
4005 do_mmap(int opno, long r, int prot)
4006 {
4007 #ifdef HAVE_SYS_MMAN_H
4008         char            *addr;
4009         int             e;
4010         pathname_t      f;
4011         int             fd;
4012         size_t          len;
4013         int64_t         lr;
4014         off64_t         off;
4015         int             flags;
4016         struct stat64   stb;
4017         int             v;
4018         char            st[1024];
4019         sigjmp_buf      sigbus_jmpbuf;
4020
4021         init_pathname(&f);
4022         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4023                 if (v)
4024                         printf("%d/%d: do_mmap - no filename\n", procid, opno);
4025                 free_pathname(&f);
4026                 return;
4027         }
4028         fd = open_path(&f, O_RDWR);
4029         e = fd < 0 ? errno : 0;
4030         check_cwd();
4031         if (fd < 0) {
4032                 if (v)
4033                         printf("%d/%d: do_mmap - open %s failed %d\n",
4034                                procid, opno, f.path, e);
4035                 free_pathname(&f);
4036                 return;
4037         }
4038         if (fstat64(fd, &stb) < 0) {
4039                 if (v)
4040                         printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4041                                procid, opno, f.path, errno);
4042                 free_pathname(&f);
4043                 close(fd);
4044                 return;
4045         }
4046         inode_info(st, sizeof(st), &stb, v);
4047         if (stb.st_size == 0) {
4048                 if (v)
4049                         printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4050                                f.path, st);
4051                 free_pathname(&f);
4052                 close(fd);
4053                 return;
4054         }
4055
4056         lr = ((int64_t)random() << 32) + random();
4057         off = (off64_t)(lr % stb.st_size);
4058         off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4059         len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4060
4061         flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4062         addr = mmap(NULL, len, prot, flags, fd, off);
4063         e = (addr == MAP_FAILED) ? errno : 0;
4064         if (e) {
4065                 if (v)
4066                         printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4067                                procid, opno, f.path, st, (long long)off,
4068                                (int)len, translate_mmap_flags(flags), e);
4069                 free_pathname(&f);
4070                 close(fd);
4071                 return;
4072         }
4073
4074         if (prot & PROT_WRITE) {
4075                 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4076                         sigbus_jmp = &sigbus_jmpbuf;
4077                         memset(addr, nameseq & 0xff, len);
4078                 }
4079         } else {
4080                 char *buf;
4081                 if ((buf = malloc(len)) != NULL) {
4082                         memcpy(buf, addr, len);
4083                         free(buf);
4084                 }
4085         }
4086         munmap(addr, len);
4087         /* set NULL to stop other functions from doing siglongjmp */
4088         sigbus_jmp = NULL;
4089
4090         if (v)
4091                 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4092                        procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4093                        f.path, st, (long long)off, (int)len,
4094                        translate_mmap_flags(flags),
4095                        (e == 0) ? "0" : "Bus error");
4096
4097         free_pathname(&f);
4098         close(fd);
4099 #endif
4100 }
4101
4102 void
4103 mread_f(int opno, long r)
4104 {
4105 #ifdef HAVE_SYS_MMAN_H
4106         do_mmap(opno, r, PROT_READ);
4107 #endif
4108 }
4109
4110 void
4111 mwrite_f(int opno, long r)
4112 {
4113 #ifdef HAVE_SYS_MMAN_H
4114         do_mmap(opno, r, PROT_WRITE);
4115 #endif
4116 }
4117
4118 void
4119 punch_f(int opno, long r)
4120 {
4121 #ifdef HAVE_LINUX_FALLOC_H
4122         do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4123 #endif
4124 }
4125
4126 void
4127 zero_f(int opno, long r)
4128 {
4129 #ifdef HAVE_LINUX_FALLOC_H
4130         do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4131 #endif
4132 }
4133
4134 void
4135 collapse_f(int opno, long r)
4136 {
4137 #ifdef HAVE_LINUX_FALLOC_H
4138         do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4139 #endif
4140 }
4141
4142 void
4143 insert_f(int opno, long r)
4144 {
4145 #ifdef HAVE_LINUX_FALLOC_H
4146         do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4147 #endif
4148 }
4149
4150 void
4151 read_f(int opno, long r)
4152 {
4153         char            *buf;
4154         int             e;
4155         pathname_t      f;
4156         int             fd;
4157         size_t          len;
4158         int64_t         lr;
4159         off64_t         off;
4160         struct stat64   stb;
4161         int             v;
4162         char            st[1024];
4163
4164         init_pathname(&f);
4165         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4166                 if (v)
4167                         printf("%d/%d: read - no filename\n", procid, opno);
4168                 free_pathname(&f);
4169                 return;
4170         }
4171         fd = open_path(&f, O_RDONLY);
4172         e = fd < 0 ? errno : 0;
4173         check_cwd();
4174         if (fd < 0) {
4175                 if (v)
4176                         printf("%d/%d: read - open %s failed %d\n",
4177                                 procid, opno, f.path, e);
4178                 free_pathname(&f);
4179                 return;
4180         }
4181         if (fstat64(fd, &stb) < 0) {
4182                 if (v)
4183                         printf("%d/%d: read - fstat64 %s failed %d\n",
4184                                 procid, opno, f.path, errno);
4185                 free_pathname(&f);
4186                 close(fd);
4187                 return;
4188         }
4189         inode_info(st, sizeof(st), &stb, v);
4190         if (stb.st_size == 0) {
4191                 if (v)
4192                         printf("%d/%d: read - %s%s zero size\n", procid, opno,
4193                                f.path, st);
4194                 free_pathname(&f);
4195                 close(fd);
4196                 return;
4197         }
4198         lr = ((int64_t)random() << 32) + random();
4199         off = (off64_t)(lr % stb.st_size);
4200         lseek64(fd, off, SEEK_SET);
4201         len = (random() % FILELEN_MAX) + 1;
4202         buf = malloc(len);
4203         e = read(fd, buf, len) < 0 ? errno : 0;
4204         free(buf);
4205         if (v)
4206                 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4207                        procid, opno, f.path, st, (long long)off, (int)len, e);
4208         free_pathname(&f);
4209         close(fd);
4210 }
4211
4212 void
4213 readlink_f(int opno, long r)
4214 {
4215         char            buf[PATH_MAX];
4216         int             e;
4217         pathname_t      f;
4218         int             v;
4219
4220         init_pathname(&f);
4221         if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4222                 if (v)
4223                         printf("%d/%d: readlink - no filename\n", procid, opno);
4224                 free_pathname(&f);
4225                 return;
4226         }
4227         e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4228         check_cwd();
4229         if (v)
4230                 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4231         free_pathname(&f);
4232 }
4233
4234 void
4235 readv_f(int opno, long r)
4236 {
4237         char            *buf;
4238         int             e;
4239         pathname_t      f;
4240         int             fd;
4241         size_t          len;
4242         int64_t         lr;
4243         off64_t         off;
4244         struct stat64   stb;
4245         int             v;
4246         char            st[1024];
4247         struct iovec    *iov = NULL;
4248         int             iovcnt;
4249         size_t          iovb;
4250         size_t          iovl;
4251         int             i;
4252
4253         init_pathname(&f);
4254         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4255                 if (v)
4256                         printf("%d/%d: readv - no filename\n", procid, opno);
4257                 free_pathname(&f);
4258                 return;
4259         }
4260         fd = open_path(&f, O_RDONLY);
4261         e = fd < 0 ? errno : 0;
4262         check_cwd();
4263         if (fd < 0) {
4264                 if (v)
4265                         printf("%d/%d: readv - open %s failed %d\n",
4266                                 procid, opno, f.path, e);
4267                 free_pathname(&f);
4268                 return;
4269         }
4270         if (fstat64(fd, &stb) < 0) {
4271                 if (v)
4272                         printf("%d/%d: readv - fstat64 %s failed %d\n",
4273                                 procid, opno, f.path, errno);
4274                 free_pathname(&f);
4275                 close(fd);
4276                 return;
4277         }
4278         inode_info(st, sizeof(st), &stb, v);
4279         if (stb.st_size == 0) {
4280                 if (v)
4281                         printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4282                                f.path, st);
4283                 free_pathname(&f);
4284                 close(fd);
4285                 return;
4286         }
4287         lr = ((int64_t)random() << 32) + random();
4288         off = (off64_t)(lr % stb.st_size);
4289         lseek64(fd, off, SEEK_SET);
4290         len = (random() % FILELEN_MAX) + 1;
4291         buf = malloc(len);
4292
4293         iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4294         iov = calloc(iovcnt, sizeof(struct iovec));
4295         iovl = len / iovcnt;
4296         iovb = 0;
4297         for (i=0; i<iovcnt; i++) {
4298                 (iov + i)->iov_base = (buf + iovb);
4299                 (iov + i)->iov_len  = iovl;
4300                 iovb += iovl;
4301         }
4302
4303         e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4304         free(buf);
4305         if (v)
4306                 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4307                        procid, opno, f.path, st, (long long)off, (int)iovl,
4308                        iovcnt, e);
4309         free_pathname(&f);
4310         close(fd);
4311 }
4312
4313 void
4314 removefattr_f(int opno, long r)
4315 {
4316         fent_t          *fep;
4317         int             e;
4318         pathname_t      f;
4319         int             v;
4320         char            name[XATTR_NAME_BUF_SIZE];
4321         int             xattr_num;
4322
4323         init_pathname(&f);
4324         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4325                 if (v)
4326                         printf("%d/%d: removefattr - no filename\n", procid, opno);
4327                 goto out;
4328         }
4329         check_cwd();
4330
4331         /*
4332          * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4333          * remove a xattr that doesn't exist (fremovexattr should fail with
4334          * errno set to ENOATTR (61) in this case).
4335          */
4336         if (fep->xattr_counter > 0)
4337                 xattr_num = (random() % fep->xattr_counter) + 1;
4338         else
4339                 xattr_num = 0;
4340
4341         e = generate_xattr_name(xattr_num, name, sizeof(name));
4342         if (e < 0) {
4343                 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4344                        procid, opno, f.path, e);
4345                 goto out;
4346         }
4347
4348         e = removexattr(f.path, name) < 0 ? errno : 0;
4349         if (v)
4350                 printf("%d/%d: removefattr file %s name %s %d\n",
4351                        procid, opno, f.path, name, e);
4352 out:
4353         free_pathname(&f);
4354 }
4355
4356 struct print_flags renameat2_flags [] = {
4357         { RENAME_NOREPLACE, "NOREPLACE"},
4358         { RENAME_EXCHANGE, "EXCHANGE"},
4359         { RENAME_WHITEOUT, "WHITEOUT"},
4360         { -1, NULL}
4361 };
4362
4363 #define translate_renameat2_flags(mode)        \
4364         ({translate_flags(mode, "|", renameat2_flags);})
4365
4366 void
4367 do_renameat2(int opno, long r, int mode)
4368 {
4369         fent_t          *dfep;
4370         int             e;
4371         pathname_t      f;
4372         fent_t          *fep;
4373         flist_t         *flp;
4374         int             id;
4375         pathname_t      newf;
4376         int             oldid;
4377         int             parid;
4378         int             oldparid;
4379         int             which;
4380         int             v;
4381         int             v1;
4382
4383         /* get an existing path for the source of the rename */
4384         init_pathname(&f);
4385         which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4386         if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4387                 if (v1)
4388                         printf("%d/%d: rename - no source filename\n",
4389                                 procid, opno);
4390                 free_pathname(&f);
4391                 return;
4392         }
4393
4394         /*
4395          * Both pathnames must exist for the RENAME_EXCHANGE, and in
4396          * order to maintain filelist/filename integrity, we should
4397          * restrict exchange operation to files of the same type.
4398          */
4399         if (mode == RENAME_EXCHANGE) {
4400                 which = 1 << (flp - flist);
4401                 init_pathname(&newf);
4402                 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4403                         if (v)
4404                                 printf("%d/%d: rename - no target filename\n",
4405                                         procid, opno);
4406                         free_pathname(&newf);
4407                         free_pathname(&f);
4408                         return;
4409                 }
4410                 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4411                     fents_ancestor_check(dfep, fep))) {
4412                         if (v)
4413                                 printf("%d/%d: rename(REXCHANGE) %s and %s "
4414                                         "have ancestor-descendant relationship\n",
4415                                         procid, opno, f.path, newf.path);
4416                         free_pathname(&newf);
4417                         free_pathname(&f);
4418                         return;
4419                 }
4420                 v |= v1;
4421                 id = dfep->id;
4422                 parid = dfep->parent;
4423         } else {
4424                 /*
4425                  * Get an existing directory for the destination parent
4426                  * directory name.
4427                  */
4428                 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4429                         parid = -1;
4430                 else
4431                         parid = dfep->id;
4432                 v |= v1;
4433
4434                 /*
4435                  * Generate a new path using an existing parent directory
4436                  * in name.
4437                  */
4438                 init_pathname(&newf);
4439                 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4440                 v |= v1;
4441                 if (!e) {
4442                         if (v) {
4443                                 (void)fent_to_name(&f, dfep);
4444                                 printf("%d/%d: rename - no filename from %s\n",
4445                                         procid, opno, f.path);
4446                         }
4447                         free_pathname(&newf);
4448                         free_pathname(&f);
4449                         return;
4450                 }
4451         }
4452         e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4453         check_cwd();
4454         if (e == 0) {
4455                 int xattr_counter = fep->xattr_counter;
4456                 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4457                 int ft = flp - flist;
4458
4459                 oldid = fep->id;
4460                 oldparid = fep->parent;
4461
4462                 /*
4463                  * Swap the parent ids for RENAME_EXCHANGE, and replace the
4464                  * old parent id for the others.
4465                  */
4466                 if (ft == FT_DIR || ft == FT_SUBVOL)
4467                         fix_parent(oldid, id, swap);
4468
4469                 if (mode == RENAME_WHITEOUT) {
4470                         fep->xattr_counter = 0;
4471                         add_to_flist(flp - flist, id, parid, xattr_counter);
4472                 } else if (mode == RENAME_EXCHANGE) {
4473                         fep->xattr_counter = dfep->xattr_counter;
4474                         dfep->xattr_counter = xattr_counter;
4475                 } else {
4476                         del_from_flist(flp - flist, fep - flp->fents);
4477                         add_to_flist(flp - flist, id, parid, xattr_counter);
4478                 }
4479         }
4480         if (v) {
4481                 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4482                         opno, translate_renameat2_flags(mode), f.path,
4483                         newf.path, e);
4484                 if (e == 0) {
4485                         printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4486                                 procid, opno, oldid, oldparid);
4487                         printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4488                                 procid, opno, id, parid);
4489                 }
4490         }
4491         free_pathname(&newf);
4492         free_pathname(&f);
4493 }
4494
4495 void
4496 rename_f(int opno, long r)
4497 {
4498         do_renameat2(opno, r, 0);
4499 }
4500
4501 void
4502 rnoreplace_f(int opno, long r)
4503 {
4504         do_renameat2(opno, r, RENAME_NOREPLACE);
4505 }
4506
4507 void
4508 rexchange_f(int opno, long r)
4509 {
4510         do_renameat2(opno, r, RENAME_EXCHANGE);
4511 }
4512
4513 void
4514 rwhiteout_f(int opno, long r)
4515 {
4516         do_renameat2(opno, r, RENAME_WHITEOUT);
4517 }
4518
4519 void
4520 resvsp_f(int opno, long r)
4521 {
4522         int             e;
4523         pathname_t      f;
4524         int             fd;
4525         struct xfs_flock64      fl;
4526         int64_t         lr;
4527         off64_t         off;
4528         struct stat64   stb;
4529         int             v;
4530         char            st[1024];
4531
4532         init_pathname(&f);
4533         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4534                 if (v)
4535                         printf("%d/%d: resvsp - no filename\n", procid, opno);
4536                 free_pathname(&f);
4537                 return;
4538         }
4539         fd = open_path(&f, O_RDWR);
4540         e = fd < 0 ? errno : 0;
4541         check_cwd();
4542         if (fd < 0) {
4543                 if (v)
4544                         printf("%d/%d: resvsp - open %s failed %d\n",
4545                                 procid, opno, f.path, e);
4546                 free_pathname(&f);
4547                 return;
4548         }
4549         if (fstat64(fd, &stb) < 0) {
4550                 if (v)
4551                         printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4552                                 procid, opno, f.path, errno);
4553                 free_pathname(&f);
4554                 close(fd);
4555                 return;
4556         }
4557         inode_info(st, sizeof(st), &stb, v);
4558         lr = ((int64_t)random() << 32) + random();
4559         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4560         off %= maxfsize;
4561         fl.l_whence = SEEK_SET;
4562         fl.l_start = off;
4563         fl.l_len = (off64_t)(random() % (1024 * 1024));
4564         e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4565         if (v)
4566                 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4567                        procid, opno, f.path, st,
4568                         (long long)off, (long long)fl.l_len, e);
4569         free_pathname(&f);
4570         close(fd);
4571 }
4572
4573 void
4574 rmdir_f(int opno, long r)
4575 {
4576         int             e;
4577         pathname_t      f;
4578         fent_t          *fep;
4579         int             oldid;
4580         int             oldparid;
4581         int             v;
4582
4583         init_pathname(&f);
4584         if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4585                 if (v)
4586                         printf("%d/%d: rmdir - no directory\n", procid, opno);
4587                 free_pathname(&f);
4588                 return;
4589         }
4590         e = rmdir_path(&f) < 0 ? errno : 0;
4591         check_cwd();
4592         if (e == 0) {
4593                 oldid = fep->id;
4594                 oldparid = fep->parent;
4595                 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4596         }
4597         if (v) {
4598                 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4599                 if (e == 0)
4600                         printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4601                                 procid, opno, oldid, oldparid);
4602         }
4603         free_pathname(&f);
4604 }
4605
4606 void
4607 setattr_f(int opno, long r)
4608 {
4609         int             fd;
4610         int             e;
4611         pathname_t      f;
4612         uint            fl;
4613         int             v;
4614
4615         init_pathname(&f);
4616         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4617                 append_pathname(&f, ".");
4618         fd = open_path(&f, O_RDWR);
4619         e = fd < 0 ? errno : 0;
4620         check_cwd();
4621
4622         fl = attr_mask & (uint)random();
4623         e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4624         if (v)
4625                 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4626         free_pathname(&f);
4627         close(fd);
4628 }
4629
4630 void
4631 setfattr_f(int opno, long r)
4632 {
4633         int             e;
4634         pathname_t      f;
4635         fent_t          *fep;
4636         int             v;
4637         int             value_len;
4638         char            name[XATTR_NAME_BUF_SIZE];
4639         char            *value = NULL;
4640         int             flag = 0;
4641         int             xattr_num;
4642
4643         init_pathname(&f);
4644         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4645                 if (v)
4646                         printf("%d/%d: setfattr - no filename\n", procid, opno);
4647                 goto out;
4648         }
4649         check_cwd();
4650
4651         if ((fep->xattr_counter > 0) && (random() % 2)) {
4652                 /*
4653                  * Use an existing xattr name for replacing its value or
4654                  * create again a xattr that was previously deleted.
4655                  */
4656                 xattr_num = (random() % fep->xattr_counter) + 1;
4657                 if (random() % 2)
4658                         flag = XATTR_REPLACE;
4659         } else {
4660                 /* Use a new xattr name. */
4661                 xattr_num = fep->xattr_counter + 1;
4662                 /*
4663                  * Don't always use the create flag because even if our xattr
4664                  * counter is 0, we may still have xattrs associated to this
4665                  * file (this happens when xattrs are added to a file through
4666                  * one of its other hard links), so we can end up updating an
4667                  * existing xattr too.
4668                  */
4669                 if (random() % 2)
4670                         flag = XATTR_CREATE;
4671         }
4672
4673         /*
4674          * The maximum supported value size depends on the filesystem
4675          * implementation, but 100 bytes is a safe value for most filesystems
4676          * at least.
4677          */
4678         value_len = random() % 101;
4679         value = gen_random_string(value_len);
4680         if (!value && value_len > 0) {
4681                 if (v)
4682                         printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4683                                procid, opno, f.path, value_len);
4684                 goto out;
4685         }
4686         e = generate_xattr_name(xattr_num, name, sizeof(name));
4687         if (e < 0) {
4688                 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4689                        procid, opno, f.path, e);
4690                 goto out;
4691         }
4692
4693         e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4694         if (e == 0)
4695                 fep->xattr_counter++;
4696         if (v)
4697                 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4698                        procid, opno, f.path, name, xattr_flag_to_string(flag),
4699                        value_len, e);
4700 out:
4701         free(value);
4702         free_pathname(&f);
4703 }
4704
4705 void
4706 stat_f(int opno, long r)
4707 {
4708         int             e;
4709         pathname_t      f;
4710         struct stat64   stb;
4711         int             v;
4712
4713         init_pathname(&f);
4714         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4715                 if (v)
4716                         printf("%d/%d: stat - no entries\n", procid, opno);
4717                 free_pathname(&f);
4718                 return;
4719         }
4720         e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4721         check_cwd();
4722         if (v)
4723                 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4724         free_pathname(&f);
4725 }
4726
4727 void
4728 subvol_create_f(int opno, long r)
4729 {
4730 #ifdef HAVE_BTRFSUTIL_H
4731         enum btrfs_util_error   e;
4732         pathname_t              f;
4733         fent_t                  *fep;
4734         int                     id;
4735         int                     parid;
4736         int                     v;
4737         int                     v1;
4738         int                     err;
4739
4740         init_pathname(&f);
4741         if (!get_fname(FT_DIRm | FT_SUBVOLm, r, NULL, NULL, &fep, &v))
4742                 parid = -1;
4743         else
4744                 parid = fep->id;
4745         err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4746         v |= v1;
4747         if (!err) {
4748                 if (v) {
4749                         (void)fent_to_name(&f, fep);
4750                         printf("%d/%d: subvol_create - no filename from %s\n",
4751                                procid, opno, f.path);
4752                 }
4753                 free_pathname(&f);
4754                 return;
4755         }
4756         e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4757         if (e == BTRFS_UTIL_OK)
4758                 add_to_flist(FT_SUBVOL, id, parid, 0);
4759         if (v) {
4760                 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4761                        f.path, e, btrfs_util_strerror(e));
4762                 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4763                        opno, id, parid);
4764         }
4765         free_pathname(&f);
4766 #endif
4767 }
4768
4769 void
4770 subvol_delete_f(int opno, long r)
4771 {
4772 #ifdef HAVE_BTRFSUTIL_H
4773         enum btrfs_util_error   e;
4774         pathname_t              f;
4775         fent_t                  *fep;
4776         int                     v;
4777         int                     oldid;
4778         int                     oldparid;
4779
4780         init_pathname(&f);
4781         if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4782                 if (v)
4783                         printf("%d:%d: subvol_delete - no subvolume\n", procid,
4784                                opno);
4785                 free_pathname(&f);
4786                 return;
4787         }
4788         e = btrfs_util_delete_subvolume(f.path, 0);
4789         check_cwd();
4790         if (e == BTRFS_UTIL_OK) {
4791                 oldid = fep->id;
4792                 oldparid = fep->parent;
4793                 delete_subvol_children(oldid);
4794                 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4795         }
4796         if (v) {
4797                 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4798                        e, btrfs_util_strerror(e));
4799                 if (e == BTRFS_UTIL_OK)
4800                         printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4801                                procid, opno, oldid, oldparid);
4802         }
4803         free_pathname(&f);
4804 #endif
4805 }
4806
4807 void
4808 symlink_f(int opno, long r)
4809 {
4810         int             e;
4811         pathname_t      f;
4812         fent_t          *fep;
4813         int             i;
4814         int             id;
4815         int             len;
4816         int             parid;
4817         int             v;
4818         int             v1;
4819         char            *val;
4820
4821         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
4822                 parid = -1;
4823         else
4824                 parid = fep->id;
4825         init_pathname(&f);
4826         e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4827         v |= v1;
4828         if (!e) {
4829                 if (v) {
4830                         (void)fent_to_name(&f, fep);
4831                         printf("%d/%d: symlink - no filename from %s\n",
4832                                 procid, opno, f.path);
4833                 }
4834                 free_pathname(&f);
4835                 return;
4836         }
4837         len = (int)(random() % PATH_MAX);
4838         val = malloc(len + 1);
4839         if (len)
4840                 memset(val, 'x', len);
4841         val[len] = '\0';
4842         for (i = 10; i < len - 1; i += 10)
4843                 val[i] = '/';
4844         e = symlink_path(val, &f) < 0 ? errno : 0;
4845         check_cwd();
4846         if (e == 0)
4847                 add_to_flist(FT_SYM, id, parid, 0);
4848         free(val);
4849         if (v) {
4850                 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4851                 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4852         }
4853         free_pathname(&f);
4854 }
4855
4856 /* ARGSUSED */
4857 void
4858 sync_f(int opno, long r)
4859 {
4860         sync();
4861         if (verbose)
4862                 printf("%d/%d: sync\n", procid, opno);
4863 }
4864
4865 void
4866 truncate_f(int opno, long r)
4867 {
4868         int             e;
4869         pathname_t      f;
4870         int64_t         lr;
4871         off64_t         off;
4872         struct stat64   stb;
4873         int             v;
4874         char            st[1024];
4875
4876         init_pathname(&f);
4877         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4878                 if (v)
4879                         printf("%d/%d: truncate - no filename\n", procid, opno);
4880                 free_pathname(&f);
4881                 return;
4882         }
4883         e = stat64_path(&f, &stb) < 0 ? errno : 0;
4884         check_cwd();
4885         if (e > 0) {
4886                 if (v)
4887                         printf("%d/%d: truncate - stat64 %s failed %d\n",
4888                                 procid, opno, f.path, e);
4889                 free_pathname(&f);
4890                 return;
4891         }
4892         inode_info(st, sizeof(st), &stb, v);
4893         lr = ((int64_t)random() << 32) + random();
4894         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4895         off %= maxfsize;
4896         e = truncate64_path(&f, off) < 0 ? errno : 0;
4897         check_cwd();
4898         if (v)
4899                 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4900                        st, (long long)off, e);
4901         free_pathname(&f);
4902 }
4903
4904 void
4905 unlink_f(int opno, long r)
4906 {
4907         int             e;
4908         pathname_t      f;
4909         fent_t          *fep;
4910         flist_t         *flp;
4911         int             oldid;
4912         int             oldparid;
4913         int             v;
4914
4915         init_pathname(&f);
4916         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4917                 if (v)
4918                         printf("%d/%d: unlink - no file\n", procid, opno);
4919                 free_pathname(&f);
4920                 return;
4921         }
4922         e = unlink_path(&f) < 0 ? errno : 0;
4923         check_cwd();
4924         if (e == 0) {
4925                 oldid = fep->id;
4926                 oldparid = fep->parent;
4927                 del_from_flist(flp - flist, fep - flp->fents);
4928         }
4929         if (v) {
4930                 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4931                 if (e == 0)
4932                         printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4933                                 procid, opno, oldid, oldparid);
4934         }
4935         free_pathname(&f);
4936 }
4937
4938 void
4939 unresvsp_f(int opno, long r)
4940 {
4941         int             e;
4942         pathname_t      f;
4943         int             fd;
4944         struct xfs_flock64      fl;
4945         int64_t         lr;
4946         off64_t         off;
4947         struct stat64   stb;
4948         int             v;
4949         char            st[1024];
4950
4951         init_pathname(&f);
4952         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4953                 if (v)
4954                         printf("%d/%d: unresvsp - no filename\n", procid, opno);
4955                 free_pathname(&f);
4956                 return;
4957         }
4958         fd = open_path(&f, O_RDWR);
4959         e = fd < 0 ? errno : 0;
4960         check_cwd();
4961         if (fd < 0) {
4962                 if (v)
4963                         printf("%d/%d: unresvsp - open %s failed %d\n",
4964                                 procid, opno, f.path, e);
4965                 free_pathname(&f);
4966                 return;
4967         }
4968         if (fstat64(fd, &stb) < 0) {
4969                 if (v)
4970                         printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4971                                 procid, opno, f.path, errno);
4972                 free_pathname(&f);
4973                 close(fd);
4974                 return;
4975         }
4976         inode_info(st, sizeof(st), &stb, v);
4977         lr = ((int64_t)random() << 32) + random();
4978         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4979         off %= maxfsize;
4980         fl.l_whence = SEEK_SET;
4981         fl.l_start = off;
4982         fl.l_len = (off64_t)(random() % (1 << 20));
4983         e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4984         if (v)
4985                 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4986                        procid, opno, f.path, st,
4987                         (long long)off, (long long)fl.l_len, e);
4988         free_pathname(&f);
4989         close(fd);
4990 }
4991
4992 void
4993 write_f(int opno, long r)
4994 {
4995         char            *buf;
4996         int             e;
4997         pathname_t      f;
4998         int             fd;
4999         size_t          len;
5000         int64_t         lr;
5001         off64_t         off;
5002         struct stat64   stb;
5003         int             v;
5004         char            st[1024];
5005
5006         init_pathname(&f);
5007         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5008                 if (v)
5009                         printf("%d/%d: write - no filename\n", procid, opno);
5010                 free_pathname(&f);
5011                 return;
5012         }
5013         fd = open_path(&f, O_WRONLY);
5014         e = fd < 0 ? errno : 0;
5015         check_cwd();
5016         if (fd < 0) {
5017                 if (v)
5018                         printf("%d/%d: write - open %s failed %d\n",
5019                                 procid, opno, f.path, e);
5020                 free_pathname(&f);
5021                 return;
5022         }
5023         if (fstat64(fd, &stb) < 0) {
5024                 if (v)
5025                         printf("%d/%d: write - fstat64 %s failed %d\n",
5026                                 procid, opno, f.path, errno);
5027                 free_pathname(&f);
5028                 close(fd);
5029                 return;
5030         }
5031         inode_info(st, sizeof(st), &stb, v);
5032         lr = ((int64_t)random() << 32) + random();
5033         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5034         off %= maxfsize;
5035         lseek64(fd, off, SEEK_SET);
5036         len = (random() % FILELEN_MAX) + 1;
5037         buf = malloc(len);
5038         memset(buf, nameseq & 0xff, len);
5039         e = write(fd, buf, len) < 0 ? errno : 0;
5040         free(buf);
5041         if (v)
5042                 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5043                        procid, opno, f.path, st, (long long)off, (int)len, e);
5044         free_pathname(&f);
5045         close(fd);
5046 }
5047
5048 void
5049 writev_f(int opno, long r)
5050 {
5051         char            *buf;
5052         int             e;
5053         pathname_t      f;
5054         int             fd;
5055         size_t          len;
5056         int64_t         lr;
5057         off64_t         off;
5058         struct stat64   stb;
5059         int             v;
5060         char            st[1024];
5061         struct iovec    *iov = NULL;
5062         int             iovcnt;
5063         size_t          iovb;
5064         size_t          iovl;
5065         int             i;
5066
5067         init_pathname(&f);
5068         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5069                 if (v)
5070                         printf("%d/%d: writev - no filename\n", procid, opno);
5071                 free_pathname(&f);
5072                 return;
5073         }
5074         fd = open_path(&f, O_WRONLY);
5075         e = fd < 0 ? errno : 0;
5076         check_cwd();
5077         if (fd < 0) {
5078                 if (v)
5079                         printf("%d/%d: writev - open %s failed %d\n",
5080                                 procid, opno, f.path, e);
5081                 free_pathname(&f);
5082                 return;
5083         }
5084         if (fstat64(fd, &stb) < 0) {
5085                 if (v)
5086                         printf("%d/%d: writev - fstat64 %s failed %d\n",
5087                                 procid, opno, f.path, errno);
5088                 free_pathname(&f);
5089                 close(fd);
5090                 return;
5091         }
5092         inode_info(st, sizeof(st), &stb, v);
5093         lr = ((int64_t)random() << 32) + random();
5094         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5095         off %= maxfsize;
5096         lseek64(fd, off, SEEK_SET);
5097         len = (random() % FILELEN_MAX) + 1;
5098         buf = malloc(len);
5099         memset(buf, nameseq & 0xff, len);
5100
5101         iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5102         iov = calloc(iovcnt, sizeof(struct iovec));
5103         iovl = len / iovcnt;
5104         iovb = 0;
5105         for (i=0; i<iovcnt; i++) {
5106                 (iov + i)->iov_base = (buf + iovb);
5107                 (iov + i)->iov_len  = iovl;
5108                 iovb += iovl;
5109         }
5110
5111         e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5112         free(buf);
5113         free(iov);
5114         if (v)
5115                 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5116                        procid, opno, f.path, st, (long long)off, (int)iovl,
5117                        iovcnt, e);
5118         free_pathname(&f);
5119         close(fd);
5120 }
5121
5122 char *
5123 xattr_flag_to_string(int flag)
5124 {
5125         if (flag == XATTR_CREATE)
5126                 return "create";
5127         if (flag == XATTR_REPLACE)
5128                 return "replace";
5129         return "none";
5130 }