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