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