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