fsstress: reduce the number of events when io_setup
[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 *, int);
398 int     attr_set_path(pathname_t *, const char *, const char *, const int, 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, int flags)
893 {
894         char            buf[NAME_MAX + 1];
895         pathname_t      newname;
896         int             rval;
897
898         rval = attr_remove(name->path, attrname, flags);
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, flags);
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, int flags)
913 {
914         char            buf[NAME_MAX + 1];
915         pathname_t      newname;
916         int             rval;
917
918         rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
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, valuelength,
924                         flags);
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;
2082         struct dioattr  diob;
2083         int             e;
2084         pathname_t      f;
2085         int             fd;
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                 free_pathname(&f);
2103                 return;
2104         }
2105         fd = open_path(&f, flags|O_DIRECT);
2106         e = fd < 0 ? errno : 0;
2107         check_cwd();
2108         if (fd < 0) {
2109                 if (v)
2110                         printf("%d/%d: do_aio_rw - open %s failed %d\n",
2111                                procid, opno, f.path, e);
2112                 free_pathname(&f);
2113                 return;
2114         }
2115         if (fstat64(fd, &stb) < 0) {
2116                 if (v)
2117                         printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2118                                procid, opno, f.path, errno);
2119                 free_pathname(&f);
2120                 close(fd);
2121                 return;
2122         }
2123         inode_info(st, sizeof(st), &stb, v);
2124         if (!iswrite && stb.st_size == 0) {
2125                 if (v)
2126                         printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2127                                f.path, st);
2128                 free_pathname(&f);
2129                 close(fd);
2130                 return;
2131         }
2132         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2133                 if (v)
2134                         printf(
2135                         "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2136                         " fallback to stat()\n",
2137                                 procid, opno, f.path, st, errno);
2138                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2139                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2140         }
2141         dio_env = getenv("XFS_DIO_MIN");
2142         if (dio_env)
2143                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2144         align = (int64_t)diob.d_miniosz;
2145         lr = ((int64_t)random() << 32) + random();
2146         len = (random() % FILELEN_MAX) + 1;
2147         len -= (len % align);
2148         if (len <= 0)
2149                 len = align;
2150         else if (len > diob.d_maxiosz)
2151                 len = diob.d_maxiosz;
2152         buf = memalign(diob.d_mem, len);
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                 free_pathname(&f);
2170                 close(fd);
2171                 return;
2172         }
2173         if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2174                 if (v)
2175                         printf("%d/%d: %s - io_getevents failed %d\n",
2176                                procid, opno, iswrite ? "awrite" : "aread", e);
2177                 free_pathname(&f);
2178                 close(fd);
2179                 return;
2180         }
2181
2182         e = event.res != len ? event.res2 : 0;
2183         free(buf);
2184         if (v)
2185                 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2186                        procid, opno, iswrite ? "awrite" : "aread",
2187                        f.path, st, (long long)off, (int)len, e);
2188         free_pathname(&f);
2189         close(fd);
2190 }
2191 #endif
2192
2193 #ifdef URING
2194 void
2195 do_uring_rw(int opno, long r, int flags)
2196 {
2197         char            *buf = NULL;
2198         int             e;
2199         pathname_t      f;
2200         int             fd = -1;
2201         size_t          len;
2202         int64_t         lr;
2203         off64_t         off;
2204         struct stat64   stb;
2205         int             v;
2206         char            st[1024];
2207         struct io_uring_sqe     *sqe;
2208         struct io_uring_cqe     *cqe;
2209         struct iovec    iovec;
2210         int             iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2211
2212         init_pathname(&f);
2213         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2214                 if (v)
2215                         printf("%d/%d: do_uring_rw - no filename\n", procid, opno);
2216                 goto uring_out;
2217         }
2218         fd = open_path(&f, flags);
2219         e = fd < 0 ? errno : 0;
2220         check_cwd();
2221         if (fd < 0) {
2222                 if (v)
2223                         printf("%d/%d: do_uring_rw - open %s failed %d\n",
2224                                procid, opno, f.path, e);
2225                 goto uring_out;
2226         }
2227         if (fstat64(fd, &stb) < 0) {
2228                 if (v)
2229                         printf("%d/%d: do_uring_rw - fstat64 %s failed %d\n",
2230                                procid, opno, f.path, errno);
2231                 goto uring_out;
2232         }
2233         inode_info(st, sizeof(st), &stb, v);
2234         if (!iswrite && stb.st_size == 0) {
2235                 if (v)
2236                         printf("%d/%d: do_uring_rw - %s%s zero size\n", procid, opno,
2237                                f.path, st);
2238                 goto uring_out;
2239         }
2240         sqe = io_uring_get_sqe(&ring);
2241         if (!sqe) {
2242                 if (v)
2243                         printf("%d/%d: do_uring_rw - io_uring_get_sqe failed\n",
2244                                procid, opno);
2245                 goto uring_out;
2246         }
2247         lr = ((int64_t)random() << 32) + random();
2248         len = (random() % FILELEN_MAX) + 1;
2249         buf = malloc(len);
2250         if (!buf) {
2251                 if (v)
2252                         printf("%d/%d: do_uring_rw - malloc failed\n",
2253                                procid, opno);
2254                 goto uring_out;
2255         }
2256         iovec.iov_base = buf;
2257         iovec.iov_len = len;
2258         if (iswrite) {
2259                 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2260                 off %= maxfsize;
2261                 memset(buf, nameseq & 0xff, len);
2262                 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2263         } else {
2264                 off = (off64_t)(lr % stb.st_size);
2265                 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2266         }
2267
2268         if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2269                 if (v)
2270                         printf("%d/%d: %s - io_uring_submit failed %d\n", procid, opno,
2271                                iswrite ? "uring_write" : "uring_read", e);
2272                 goto uring_out;
2273         }
2274         if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2275                 if (v)
2276                         printf("%d/%d: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2277                                iswrite ? "uring_write" : "uring_read", e);
2278                 goto uring_out;
2279         }
2280         if (v)
2281                 printf("%d/%d: %s %s%s [%lld, %d(res=%d)] %d\n",
2282                        procid, opno, iswrite ? "uring_write" : "uring_read",
2283                        f.path, st, (long long)off, (int)len, cqe->res, e);
2284         io_uring_cqe_seen(&ring, cqe);
2285
2286  uring_out:
2287         if (buf)
2288                 free(buf);
2289         if (fd != -1)
2290                 close(fd);
2291         free_pathname(&f);
2292 }
2293 #endif
2294
2295 void
2296 aread_f(int opno, long r)
2297 {
2298 #ifdef AIO
2299         do_aio_rw(opno, r, O_RDONLY);
2300 #endif
2301 }
2302
2303 void
2304 attr_remove_f(int opno, long r)
2305 {
2306         attrlist_ent_t          *aep;
2307         attrlist_t              *alist;
2308         char                    *aname;
2309         char                    buf[4096];
2310         attrlist_cursor_t       cursor;
2311         int                     e;
2312         int                     ent;
2313         pathname_t              f;
2314         int                     total;
2315         int                     v;
2316         int                     which;
2317
2318         init_pathname(&f);
2319         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2320                 append_pathname(&f, ".");
2321         total = 0;
2322         bzero(&cursor, sizeof(cursor));
2323         do {
2324                 bzero(buf, sizeof(buf));
2325                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2326                 check_cwd();
2327                 if (e)
2328                         break;
2329                 alist = (attrlist_t *)buf;
2330                 total += alist->al_count;
2331         } while (alist->al_more);
2332         if (total == 0) {
2333                 if (v)
2334                         printf("%d/%d: attr_remove - no attrs for %s\n",
2335                                 procid, opno, f.path);
2336                 free_pathname(&f);
2337                 return;
2338         }
2339         which = (int)(random() % total);
2340         bzero(&cursor, sizeof(cursor));
2341         ent = 0;
2342         aname = NULL;
2343         do {
2344                 bzero(buf, sizeof(buf));
2345                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2346                 check_cwd();
2347                 if (e)
2348                         break;
2349                 alist = (attrlist_t *)buf;
2350                 if (which < ent + alist->al_count) {
2351                         aep = (attrlist_ent_t *)
2352                                 &buf[alist->al_offset[which - ent]];
2353                         aname = aep->a_name;
2354                         break;
2355                 }
2356                 ent += alist->al_count;
2357         } while (alist->al_more);
2358         if (aname == NULL) {
2359                 if (v)
2360                         printf(
2361                         "%d/%d: attr_remove - name %d not found at %s\n",
2362                                 procid, opno, which, f.path);
2363                 free_pathname(&f);
2364                 return;
2365         }
2366         e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2367         check_cwd();
2368         if (v)
2369                 printf("%d/%d: attr_remove %s %s %d\n",
2370                         procid, opno, f.path, aname, e);
2371         free_pathname(&f);
2372 }
2373
2374 void
2375 attr_set_f(int opno, long r)
2376 {
2377         char            aname[10];
2378         char            *aval;
2379         int             e;
2380         pathname_t      f;
2381         int             len;
2382         static int      lengths[] = { 10, 100, 1000, 10000 };
2383         int             li;
2384         int             v;
2385
2386         init_pathname(&f);
2387         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2388                 append_pathname(&f, ".");
2389         sprintf(aname, "a%x", nameseq++);
2390         li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2391         len = (int)(random() % lengths[li]);
2392         if (len == 0)
2393                 len = 1;
2394         aval = malloc(len);
2395         memset(aval, nameseq & 0xff, len);
2396         e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2397                 errno : 0;
2398         check_cwd();
2399         free(aval);
2400         if (v)
2401                 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2402                         aname, e);
2403         free_pathname(&f);
2404 }
2405
2406 void
2407 awrite_f(int opno, long r)
2408 {
2409 #ifdef AIO
2410         do_aio_rw(opno, r, O_WRONLY);
2411 #endif
2412 }
2413
2414 void
2415 bulkstat_f(int opno, long r)
2416 {
2417         int             count;
2418         int             fd;
2419         __u64           last;
2420         int             nent;
2421         struct xfs_bstat        *t;
2422         int64_t         total;
2423         struct xfs_fsop_bulkreq bsr;
2424
2425         last = 0;
2426         nent = (r % 999) + 2;
2427         t = malloc(nent * sizeof(*t));
2428         fd = open(".", O_RDONLY);
2429         total = 0;
2430
2431         bsr.lastip=&last;
2432         bsr.icount=nent;
2433         bsr.ubuffer=t;
2434         bsr.ocount=&count;
2435             
2436         while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2437                 total += count;
2438         free(t);
2439         if (verbose)
2440                 printf("%d/%d: bulkstat nent %d total %lld\n",
2441                         procid, opno, nent, (long long)total);
2442         close(fd);
2443 }
2444
2445 void
2446 bulkstat1_f(int opno, long r)
2447 {
2448         int             e;
2449         pathname_t      f;
2450         int             fd;
2451         int             good;
2452         __u64           ino;
2453         struct stat64   s;
2454         struct xfs_bstat        t;
2455         int             v;
2456         struct xfs_fsop_bulkreq bsr;
2457         
2458
2459         good = random() & 1;
2460         if (good) {
2461                /* use an inode we know exists */
2462                 init_pathname(&f);
2463                 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2464                         append_pathname(&f, ".");
2465                 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2466                 check_cwd();
2467                 free_pathname(&f);
2468         } else {
2469                 /* 
2470                  * pick a random inode 
2471                  *
2472                  * note this can generate kernel warning messages
2473                  * since bulkstat_one will read the disk block that
2474                  * would contain a given inode even if that disk
2475                  * block doesn't contain inodes.
2476                  *
2477                  * this is detected later, but not until after the
2478                  * warning is displayed.
2479                  *
2480                  * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2481                  *
2482                  */
2483                 ino = (ino64_t)r;
2484                 v = verbose;
2485         }
2486         fd = open(".", O_RDONLY);
2487         
2488         bsr.lastip=&ino;
2489         bsr.icount=1;
2490         bsr.ubuffer=&t;
2491         bsr.ocount=NULL;
2492         e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2493         if (v)
2494                 printf("%d/%d: bulkstat1 %s ino %lld %d\n", 
2495                        procid, opno, good?"real":"random",
2496                        verifiable_log ? -1LL : (long long)ino, e);
2497         close(fd);
2498 }
2499
2500 void
2501 chown_f(int opno, long r)
2502 {
2503         int             e;
2504         pathname_t      f;
2505         int             nbits;
2506         uid_t           u;
2507         gid_t           g;
2508         int             v;
2509
2510         init_pathname(&f);
2511         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2512                 append_pathname(&f, ".");
2513         u = (uid_t)random();
2514         g = (gid_t)random();
2515         nbits = (int)(random() % idmodulo);
2516         u &= (1 << nbits) - 1;
2517         g &= (1 << nbits) - 1;
2518         e = lchown_path(&f, u, g) < 0 ? errno : 0;
2519         check_cwd();
2520         if (v)
2521                 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2522         free_pathname(&f);
2523 }
2524
2525 /* reflink some arbitrary range of f1 to f2. */
2526 void
2527 clonerange_f(
2528         int                     opno,
2529         long                    r)
2530 {
2531 #ifdef FICLONERANGE
2532         struct file_clone_range fcr;
2533         struct pathname         fpath1;
2534         struct pathname         fpath2;
2535         struct stat64           stat1;
2536         struct stat64           stat2;
2537         char                    inoinfo1[1024];
2538         char                    inoinfo2[1024];
2539         off64_t                 lr;
2540         off64_t                 off1;
2541         off64_t                 off2;
2542         off64_t                 max_off2;
2543         size_t                  len;
2544         int                     v1;
2545         int                     v2;
2546         int                     fd1;
2547         int                     fd2;
2548         int                     ret;
2549         int                     e;
2550
2551         /* Load paths */
2552         init_pathname(&fpath1);
2553         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2554                 if (v1)
2555                         printf("%d/%d: clonerange read - no filename\n",
2556                                 procid, opno);
2557                 goto out_fpath1;
2558         }
2559
2560         init_pathname(&fpath2);
2561         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2562                 if (v2)
2563                         printf("%d/%d: clonerange write - no filename\n",
2564                                 procid, opno);
2565                 goto out_fpath2;
2566         }
2567
2568         /* Open files */
2569         fd1 = open_path(&fpath1, O_RDONLY);
2570         e = fd1 < 0 ? errno : 0;
2571         check_cwd();
2572         if (fd1 < 0) {
2573                 if (v1)
2574                         printf("%d/%d: clonerange read - open %s failed %d\n",
2575                                 procid, opno, fpath1.path, e);
2576                 goto out_fpath2;
2577         }
2578
2579         fd2 = open_path(&fpath2, O_WRONLY);
2580         e = fd2 < 0 ? errno : 0;
2581         check_cwd();
2582         if (fd2 < 0) {
2583                 if (v2)
2584                         printf("%d/%d: clonerange write - open %s failed %d\n",
2585                                 procid, opno, fpath2.path, e);
2586                 goto out_fd1;
2587         }
2588
2589         /* Get file stats */
2590         if (fstat64(fd1, &stat1) < 0) {
2591                 if (v1)
2592                         printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2593                                 procid, opno, fpath1.path, errno);
2594                 goto out_fd2;
2595         }
2596         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2597
2598         if (fstat64(fd2, &stat2) < 0) {
2599                 if (v2)
2600                         printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2601                                 procid, opno, fpath2.path, errno);
2602                 goto out_fd2;
2603         }
2604         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2605
2606         /* Calculate offsets */
2607         len = (random() % FILELEN_MAX) + 1;
2608         len &= ~(stat1.st_blksize - 1);
2609         if (len == 0)
2610                 len = stat1.st_blksize;
2611         if (len > stat1.st_size)
2612                 len = stat1.st_size;
2613
2614         lr = ((int64_t)random() << 32) + random();
2615         if (stat1.st_size == len)
2616                 off1 = 0;
2617         else
2618                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2619         off1 %= maxfsize;
2620         off1 &= ~(stat1.st_blksize - 1);
2621
2622         /*
2623          * If srcfile == destfile, randomly generate destination ranges
2624          * until we find one that doesn't overlap the source range.
2625          */
2626         max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2627         do {
2628                 lr = ((int64_t)random() << 32) + random();
2629                 off2 = (off64_t)(lr % max_off2);
2630                 off2 %= maxfsize;
2631                 off2 &= ~(stat2.st_blksize - 1);
2632         } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2633
2634         /* Clone data blocks */
2635         fcr.src_fd = fd1;
2636         fcr.src_offset = off1;
2637         fcr.src_length = len;
2638         fcr.dest_offset = off2;
2639
2640         ret = ioctl(fd2, FICLONERANGE, &fcr);
2641         e = ret < 0 ? errno : 0;
2642         if (v1 || v2) {
2643                 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2644                         procid, opno,
2645                         fpath1.path, inoinfo1, (long long)off1, (long long)len,
2646                         fpath2.path, inoinfo2, (long long)off2, (long long)len);
2647
2648                 if (ret < 0)
2649                         printf(" error %d", e);
2650                 printf("\n");
2651         }
2652
2653 out_fd2:
2654         close(fd2);
2655 out_fd1:
2656         close(fd1);
2657 out_fpath2:
2658         free_pathname(&fpath2);
2659 out_fpath1:
2660         free_pathname(&fpath1);
2661 #endif
2662 }
2663
2664 /* copy some arbitrary range of f1 to f2. */
2665 void
2666 copyrange_f(
2667         int                     opno,
2668         long                    r)
2669 {
2670 #ifdef HAVE_COPY_FILE_RANGE
2671         struct pathname         fpath1;
2672         struct pathname         fpath2;
2673         struct stat64           stat1;
2674         struct stat64           stat2;
2675         char                    inoinfo1[1024];
2676         char                    inoinfo2[1024];
2677         loff_t                  lr;
2678         loff_t                  off1;
2679         loff_t                  off2;
2680         loff_t                  offset1;
2681         loff_t                  offset2;
2682         loff_t                  max_off2;
2683         size_t                  len;
2684         size_t                  length;
2685         int                     tries = 0;
2686         int                     v1;
2687         int                     v2;
2688         int                     fd1;
2689         int                     fd2;
2690         size_t                  ret = 0;
2691         int                     e;
2692
2693         /* Load paths */
2694         init_pathname(&fpath1);
2695         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2696                 if (v1)
2697                         printf("%d/%d: copyrange read - no filename\n",
2698                                 procid, opno);
2699                 goto out_fpath1;
2700         }
2701
2702         init_pathname(&fpath2);
2703         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2704                 if (v2)
2705                         printf("%d/%d: copyrange write - no filename\n",
2706                                 procid, opno);
2707                 goto out_fpath2;
2708         }
2709
2710         /* Open files */
2711         fd1 = open_path(&fpath1, O_RDONLY);
2712         e = fd1 < 0 ? errno : 0;
2713         check_cwd();
2714         if (fd1 < 0) {
2715                 if (v1)
2716                         printf("%d/%d: copyrange read - open %s failed %d\n",
2717                                 procid, opno, fpath1.path, e);
2718                 goto out_fpath2;
2719         }
2720
2721         fd2 = open_path(&fpath2, O_WRONLY);
2722         e = fd2 < 0 ? errno : 0;
2723         check_cwd();
2724         if (fd2 < 0) {
2725                 if (v2)
2726                         printf("%d/%d: copyrange write - open %s failed %d\n",
2727                                 procid, opno, fpath2.path, e);
2728                 goto out_fd1;
2729         }
2730
2731         /* Get file stats */
2732         if (fstat64(fd1, &stat1) < 0) {
2733                 if (v1)
2734                         printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2735                                 procid, opno, fpath1.path, errno);
2736                 goto out_fd2;
2737         }
2738         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2739
2740         if (fstat64(fd2, &stat2) < 0) {
2741                 if (v2)
2742                         printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2743                                 procid, opno, fpath2.path, errno);
2744                 goto out_fd2;
2745         }
2746         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2747
2748         /* Calculate offsets */
2749         len = (random() % FILELEN_MAX) + 1;
2750         if (len == 0)
2751                 len = stat1.st_blksize;
2752         if (len > stat1.st_size)
2753                 len = stat1.st_size;
2754
2755         lr = ((int64_t)random() << 32) + random();
2756         if (stat1.st_size == len)
2757                 off1 = 0;
2758         else
2759                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2760         off1 %= maxfsize;
2761
2762         /*
2763          * If srcfile == destfile, randomly generate destination ranges
2764          * until we find one that doesn't overlap the source range.
2765          */
2766         max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2767         do {
2768                 lr = ((int64_t)random() << 32) + random();
2769                 off2 = (off64_t)(lr % max_off2);
2770                 off2 %= maxfsize;
2771         } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2772
2773         /*
2774          * Since len, off1 and off2 will be changed later, preserve their
2775          * original values.
2776          */
2777         length = len;
2778         offset1 = off1;
2779         offset2 = off2;
2780
2781         while (len > 0) {
2782                 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2783                               len, 0);
2784                 if (ret < 0) {
2785                         if (errno != EAGAIN || tries++ >= 300)
2786                                 break;
2787                 } else if (ret > len || ret == 0)
2788                         break;
2789                 else if (ret > 0)
2790                         len -= ret;
2791         }
2792         e = ret < 0 ? errno : 0;
2793         if (v1 || v2) {
2794                 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2795                         procid, opno,
2796                         fpath1.path, inoinfo1,
2797                         (long long)offset1, (long long)length,
2798                         fpath2.path, inoinfo2,
2799                         (long long)offset2, (long long)length);
2800
2801                 if (ret < 0)
2802                         printf(" error %d", e);
2803                 else if (len && ret > len)
2804                         printf(" asked for %lld, copied %lld??\n",
2805                                 (long long)len, (long long)ret);
2806                 printf("\n");
2807         }
2808
2809 out_fd2:
2810         close(fd2);
2811 out_fd1:
2812         close(fd1);
2813 out_fpath2:
2814         free_pathname(&fpath2);
2815 out_fpath1:
2816         free_pathname(&fpath1);
2817 #endif
2818 }
2819
2820 /* dedupe some arbitrary range of f1 to f2...fn. */
2821 void
2822 deduperange_f(
2823         int                     opno,
2824         long                    r)
2825 {
2826 #ifdef FIDEDUPERANGE
2827 #define INFO_SZ                 1024
2828         struct file_dedupe_range *fdr;
2829         struct pathname         *fpath;
2830         struct stat64           *stat;
2831         char                    *info;
2832         off64_t                 *off;
2833         int                     *v;
2834         int                     *fd;
2835         int                     nr;
2836         off64_t                 lr;
2837         size_t                  len;
2838         int                     ret;
2839         int                     i;
2840         int                     e;
2841
2842         if (flist[FT_REG].nfiles < 2)
2843                 return;
2844
2845         /* Pick somewhere between 2 and 128 files. */
2846         do {
2847                 nr = random() % (flist[FT_REG].nfiles + 1);
2848         } while (nr < 2 || nr > 128);
2849
2850         /* Alloc memory */
2851         fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2852                      sizeof(struct file_dedupe_range));
2853         if (!fdr) {
2854                 printf("%d/%d: line %d error %d\n",
2855                         procid, opno, __LINE__, errno);
2856                 return;
2857         }
2858         memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2859                         sizeof(struct file_dedupe_range)));
2860
2861         fpath = calloc(nr, sizeof(struct pathname));
2862         if (!fpath) {
2863                 printf("%d/%d: line %d error %d\n",
2864                         procid, opno, __LINE__, errno);
2865                 goto out_fdr;
2866         }
2867
2868         stat = calloc(nr, sizeof(struct stat64));
2869         if (!stat) {
2870                 printf("%d/%d: line %d error %d\n",
2871                         procid, opno, __LINE__, errno);
2872                 goto out_paths;
2873         }
2874
2875         info = calloc(nr, INFO_SZ);
2876         if (!info) {
2877                 printf("%d/%d: line %d error %d\n",
2878                         procid, opno, __LINE__, errno);
2879                 goto out_stats;
2880         }
2881
2882         off = calloc(nr, sizeof(off64_t));
2883         if (!off) {
2884                 printf("%d/%d: line %d error %d\n",
2885                         procid, opno, __LINE__, errno);
2886                 goto out_info;
2887         }
2888
2889         v = calloc(nr, sizeof(int));
2890         if (!v) {
2891                 printf("%d/%d: line %d error %d\n",
2892                         procid, opno, __LINE__, errno);
2893                 goto out_offsets;
2894         }
2895         fd = calloc(nr, sizeof(int));
2896         if (!fd) {
2897                 printf("%d/%d: line %d error %d\n",
2898                         procid, opno, __LINE__, errno);
2899                 goto out_v;
2900         }
2901         memset(fd, 0xFF, nr * sizeof(int));
2902
2903         /* Get paths for all files */
2904         for (i = 0; i < nr; i++)
2905                 init_pathname(&fpath[i]);
2906
2907         if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2908                 if (v[0])
2909                         printf("%d/%d: deduperange read - no filename\n",
2910                                 procid, opno);
2911                 goto out_pathnames;
2912         }
2913
2914         for (i = 1; i < nr; i++) {
2915                 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2916                         if (v[i])
2917                                 printf("%d/%d: deduperange write - no filename\n",
2918                                         procid, opno);
2919                         goto out_pathnames;
2920                 }
2921         }
2922
2923         /* Open files */
2924         fd[0] = open_path(&fpath[0], O_RDONLY);
2925         e = fd[0] < 0 ? errno : 0;
2926         check_cwd();
2927         if (fd[0] < 0) {
2928                 if (v[0])
2929                         printf("%d/%d: deduperange read - open %s failed %d\n",
2930                                 procid, opno, fpath[0].path, e);
2931                 goto out_pathnames;
2932         }
2933
2934         for (i = 1; i < nr; i++) {
2935                 fd[i] = open_path(&fpath[i], O_WRONLY);
2936                 e = fd[i] < 0 ? errno : 0;
2937                 check_cwd();
2938                 if (fd[i] < 0) {
2939                         if (v[i])
2940                                 printf("%d/%d: deduperange write - open %s failed %d\n",
2941                                         procid, opno, fpath[i].path, e);
2942                         goto out_fds;
2943                 }
2944         }
2945
2946         /* Get file stats */
2947         if (fstat64(fd[0], &stat[0]) < 0) {
2948                 if (v[0])
2949                         printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2950                                 procid, opno, fpath[0].path, errno);
2951                 goto out_fds;
2952         }
2953
2954         inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2955
2956         for (i = 1; i < nr; i++) {
2957                 if (fstat64(fd[i], &stat[i]) < 0) {
2958                         if (v[i])
2959                                 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2960                                         procid, opno, fpath[i].path, errno);
2961                         goto out_fds;
2962                 }
2963                 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2964         }
2965
2966         /* Never try to dedupe more than half of the src file. */
2967         len = (random() % FILELEN_MAX) + 1;
2968         len &= ~(stat[0].st_blksize - 1);
2969         if (len == 0)
2970                 len = stat[0].st_blksize / 2;
2971         if (len > stat[0].st_size / 2)
2972                 len = stat[0].st_size / 2;
2973
2974         /* Calculate offsets */
2975         lr = ((int64_t)random() << 32) + random();
2976         if (stat[0].st_size == len)
2977                 off[0] = 0;
2978         else
2979                 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2980         off[0] %= maxfsize;
2981         off[0] &= ~(stat[0].st_blksize - 1);
2982
2983         /*
2984          * If srcfile == destfile[i], randomly generate destination ranges
2985          * until we find one that doesn't overlap the source range.
2986          */
2987         for (i = 1; i < nr; i++) {
2988                 int     tries = 0;
2989
2990                 do {
2991                         lr = ((int64_t)random() << 32) + random();
2992                         if (stat[i].st_size <= len)
2993                                 off[i] = 0;
2994                         else
2995                                 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2996                         off[i] %= maxfsize;
2997                         off[i] &= ~(stat[i].st_blksize - 1);
2998                 } while (stat[0].st_ino == stat[i].st_ino &&
2999                          llabs(off[i] - off[0]) < len &&
3000                          tries++ < 10);
3001         }
3002
3003         /* Clone data blocks */
3004         fdr->src_offset = off[0];
3005         fdr->src_length = len;
3006         fdr->dest_count = nr - 1;
3007         for (i = 1; i < nr; i++) {
3008                 fdr->info[i - 1].dest_fd = fd[i];
3009                 fdr->info[i - 1].dest_offset = off[i];
3010         }
3011
3012         ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3013         e = ret < 0 ? errno : 0;
3014         if (v[0]) {
3015                 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
3016                         procid, opno,
3017                         fpath[0].path, &info[0], (long long)off[0],
3018                         (long long)len);
3019                 if (ret < 0)
3020                         printf(" error %d", e);
3021                 printf("\n");
3022         }
3023         if (ret < 0)
3024                 goto out_fds;
3025
3026         for (i = 1; i < nr; i++) {
3027                 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3028                 if (v[i]) {
3029                         printf("%d/%d: ...to %s%s [%lld,%lld]",
3030                                 procid, opno,
3031                                 fpath[i].path, &info[i * INFO_SZ],
3032                                 (long long)off[i], (long long)len);
3033                         if (fdr->info[i - 1].status < 0)
3034                                 printf(" error %d", e);
3035                         if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3036                                 printf(" %llu bytes deduplicated",
3037                                         fdr->info[i - 1].bytes_deduped);
3038                         if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3039                                 printf(" differed");
3040                         printf("\n");
3041                 }
3042         }
3043
3044 out_fds:
3045         for (i = 0; i < nr; i++)
3046                 if (fd[i] >= 0)
3047                         close(fd[i]);
3048 out_pathnames:
3049         for (i = 0; i < nr; i++)
3050                 free_pathname(&fpath[i]);
3051
3052         free(fd);
3053 out_v:
3054         free(v);
3055 out_offsets:
3056         free(off);
3057 out_info:
3058         free(info);
3059 out_stats:
3060         free(stat);
3061 out_paths:
3062         free(fpath);
3063 out_fdr:
3064         free(fdr);
3065 #endif
3066 }
3067
3068 void
3069 setxattr_f(int opno, long r)
3070 {
3071 #ifdef XFS_XFLAG_EXTSIZE
3072         struct fsxattr  fsx;
3073         int             fd;
3074         int             e;
3075         pathname_t      f;
3076         int             nbits;
3077         uint            p;
3078         int             v;
3079
3080         init_pathname(&f);
3081         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3082                 append_pathname(&f, ".");
3083         fd = open_path(&f, O_RDWR);
3084         e = fd < 0 ? errno : 0;
3085         check_cwd();
3086
3087         /* project ID */
3088         p = (uint)random();
3089         e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3090         nbits = (int)(random() % e);
3091         p &= (1 << nbits) - 1;
3092
3093         if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3094                 fsx.fsx_projid = p;
3095                 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3096         }
3097         if (v)
3098                 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3099         free_pathname(&f);
3100         close(fd);
3101 #endif
3102 }
3103
3104 void
3105 splice_f(int opno, long r)
3106 {
3107         struct pathname         fpath1;
3108         struct pathname         fpath2;
3109         struct stat64           stat1;
3110         struct stat64           stat2;
3111         char                    inoinfo1[1024];
3112         char                    inoinfo2[1024];
3113         loff_t                  lr;
3114         loff_t                  off1, off2;
3115         size_t                  len;
3116         loff_t                  offset1, offset2;
3117         size_t                  length;
3118         size_t                  total;
3119         int                     v1;
3120         int                     v2;
3121         int                     fd1;
3122         int                     fd2;
3123         ssize_t                 ret1 = 0, ret2 = 0;
3124         size_t                  bytes;
3125         int                     e;
3126         int                     filedes[2];
3127
3128         /* Load paths */
3129         init_pathname(&fpath1);
3130         if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3131                 if (v1)
3132                         printf("%d/%d: splice read - no filename\n",
3133                                 procid, opno);
3134                 goto out_fpath1;
3135         }
3136
3137         init_pathname(&fpath2);
3138         if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3139                 if (v2)
3140                         printf("%d/%d: splice write - no filename\n",
3141                                 procid, opno);
3142                 goto out_fpath2;
3143         }
3144
3145         /* Open files */
3146         fd1 = open_path(&fpath1, O_RDONLY);
3147         e = fd1 < 0 ? errno : 0;
3148         check_cwd();
3149         if (fd1 < 0) {
3150                 if (v1)
3151                         printf("%d/%d: splice read - open %s failed %d\n",
3152                                 procid, opno, fpath1.path, e);
3153                 goto out_fpath2;
3154         }
3155
3156         fd2 = open_path(&fpath2, O_WRONLY);
3157         e = fd2 < 0 ? errno : 0;
3158         check_cwd();
3159         if (fd2 < 0) {
3160                 if (v2)
3161                         printf("%d/%d: splice write - open %s failed %d\n",
3162                                 procid, opno, fpath2.path, e);
3163                 goto out_fd1;
3164         }
3165
3166         /* Get file stats */
3167         if (fstat64(fd1, &stat1) < 0) {
3168                 if (v1)
3169                         printf("%d/%d: splice read - fstat64 %s failed %d\n",
3170                                 procid, opno, fpath1.path, errno);
3171                 goto out_fd2;
3172         }
3173         inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3174
3175         if (fstat64(fd2, &stat2) < 0) {
3176                 if (v2)
3177                         printf("%d/%d: splice write - fstat64 %s failed %d\n",
3178                                 procid, opno, fpath2.path, errno);
3179                 goto out_fd2;
3180         }
3181         inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3182
3183         /* Calculate offsets */
3184         len = (random() % FILELEN_MAX) + 1;
3185         if (len == 0)
3186                 len = stat1.st_blksize;
3187         if (len > stat1.st_size)
3188                 len = stat1.st_size;
3189
3190         lr = ((int64_t)random() << 32) + random();
3191         if (stat1.st_size == len)
3192                 off1 = 0;
3193         else
3194                 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3195         off1 %= maxfsize;
3196
3197         /*
3198          * splice can overlap write, so the offset of the target file can be
3199          * any number. But to avoid too large offset, add a clamp of 1024 blocks
3200          * past the current dest file EOF
3201          */
3202         lr = ((int64_t)random() << 32) + random();
3203         off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3204
3205         /*
3206          * Since len, off1 and off2 will be changed later, preserve their
3207          * original values.
3208          */
3209         length = len;
3210         offset1 = off1;
3211         offset2 = off2;
3212
3213         /* Pipe initialize */
3214         if (pipe(filedes) < 0) {
3215                 if (v1 || v2) {
3216                         printf("%d/%d: splice - pipe failed %d\n",
3217                                 procid, opno, errno);
3218                         goto out_fd2;
3219                 }
3220         }
3221
3222         bytes = 0;
3223         total = 0;
3224         while (len > 0) {
3225                 /* move to pipe buffer */
3226                 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3227                 if (ret1 <= 0) {
3228                         break;
3229                 }
3230                 bytes = ret1;
3231
3232                 /* move from pipe buffer to dst file */
3233                 while (bytes > 0) {
3234                         ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3235                         if (ret2 < 0) {
3236                                 break;
3237                         }
3238                         bytes -= ret2;
3239                 }
3240                 if (ret2 < 0)
3241                         break;
3242
3243                 len -= ret1;
3244                 total += ret1;
3245         }
3246
3247         if (ret1 < 0 || ret2 < 0)
3248                 e = errno;
3249         else
3250                 e = 0;
3251         if (v1 || v2) {
3252                 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3253                         procid, opno,
3254                         fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3255                         fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3256
3257                 if (length && length > total)
3258                         printf(" asked for %lld, spliced %lld??\n",
3259                                 (long long)length, (long long)total);
3260                 printf("\n");
3261         }
3262
3263         close(filedes[0]);
3264         close(filedes[1]);
3265 out_fd2:
3266         close(fd2);
3267 out_fd1:
3268         close(fd1);
3269 out_fpath2:
3270         free_pathname(&fpath2);
3271 out_fpath1:
3272         free_pathname(&fpath1);
3273 }
3274
3275 void
3276 creat_f(int opno, long r)
3277 {
3278         struct fsxattr  a;
3279         int             e;
3280         int             e1;
3281         int             extsize;
3282         pathname_t      f;
3283         int             fd;
3284         fent_t          *fep;
3285         int             id;
3286         int             parid;
3287         int             type;
3288         int             v;
3289         int             v1;
3290
3291         if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3292                 parid = -1;
3293         else
3294                 parid = fep->id;
3295         init_pathname(&f);
3296         e1 = (random() % 100);
3297         type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3298 #ifdef NOTYET
3299         if (type == FT_RTF)     /* rt always gets an extsize */
3300                 extsize = (random() % 10) + 1;
3301         else if (e1 < 10)       /* one-in-ten get an extsize */
3302                 extsize = random() % 1024;
3303         else
3304 #endif
3305                 extsize = 0;
3306         e = generate_fname(fep, type, &f, &id, &v);
3307         v |= v1;
3308         if (!e) {
3309                 if (v) {
3310                         (void)fent_to_name(&f, fep);
3311                         printf("%d/%d: creat - no filename from %s\n",
3312                                 procid, opno, f.path);
3313                 }
3314                 free_pathname(&f);
3315                 return;
3316         }
3317         fd = creat_path(&f, 0666);
3318         e = fd < 0 ? errno : 0;
3319         e1 = 0;
3320         check_cwd();
3321         if (fd >= 0) {
3322                 if (extsize &&
3323                     xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3324                         if (type == FT_RTF) {
3325                                 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3326                                 a.fsx_extsize = extsize *
3327                                                 geom.rtextsize * geom.blocksize;
3328 #ifdef NOTYET
3329                         } else if (extsize) {
3330                                 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3331                                 a.fsx_extsize = extsize * geom.blocksize;
3332 #endif
3333                         }
3334                         if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3335                                 e1 = errno;
3336                 }
3337                 add_to_flist(type, id, parid, 0);
3338                 close(fd);
3339         }
3340         if (v) {
3341                 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3342                         extsize ? a.fsx_extsize : 0, e, e1);
3343                 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3344         }
3345         free_pathname(&f);
3346 }
3347
3348 void
3349 dread_f(int opno, long r)
3350 {
3351         int64_t         align;
3352         char            *buf;
3353         struct dioattr  diob;
3354         int             e;
3355         pathname_t      f;
3356         int             fd;
3357         size_t          len;
3358         int64_t         lr;
3359         off64_t         off;
3360         struct stat64   stb;
3361         int             v;
3362         char            st[1024];
3363         char            *dio_env;
3364
3365         init_pathname(&f);
3366         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3367                 if (v)
3368                         printf("%d/%d: dread - no filename\n", procid, opno);
3369                 free_pathname(&f);
3370                 return;
3371         }
3372         fd = open_path(&f, O_RDONLY|O_DIRECT);
3373         e = fd < 0 ? errno : 0;
3374         check_cwd();
3375         if (fd < 0) {
3376                 if (v)
3377                         printf("%d/%d: dread - open %s failed %d\n",
3378                                 procid, opno, f.path, e);
3379                 free_pathname(&f);
3380                 return;
3381         }
3382         if (fstat64(fd, &stb) < 0) {
3383                 if (v)
3384                         printf("%d/%d: dread - fstat64 %s failed %d\n",
3385                                procid, opno, f.path, errno);
3386                 free_pathname(&f);
3387                 close(fd);
3388                 return;
3389         }
3390         inode_info(st, sizeof(st), &stb, v);
3391         if (stb.st_size == 0) {
3392                 if (v)
3393                         printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3394                                f.path, st);
3395                 free_pathname(&f);
3396                 close(fd);
3397                 return;
3398         }
3399         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3400                 if (v)
3401                         printf(
3402                         "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3403                         " fallback to stat()\n",
3404                                 procid, opno, f.path, st, errno);
3405                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3406                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3407         }
3408
3409         dio_env = getenv("XFS_DIO_MIN");
3410         if (dio_env)
3411                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3412
3413         align = (int64_t)diob.d_miniosz;
3414         lr = ((int64_t)random() << 32) + random();
3415         off = (off64_t)(lr % stb.st_size);
3416         off -= (off % align);
3417         lseek64(fd, off, SEEK_SET);
3418         len = (random() % FILELEN_MAX) + 1;
3419         len -= (len % align);
3420         if (len <= 0)
3421                 len = align;
3422         else if (len > diob.d_maxiosz) 
3423                 len = diob.d_maxiosz;
3424         buf = memalign(diob.d_mem, len);
3425         e = read(fd, buf, len) < 0 ? errno : 0;
3426         free(buf);
3427         if (v)
3428                 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3429                        procid, opno, f.path, st, (long long)off, (int)len, e);
3430         free_pathname(&f);
3431         close(fd);
3432 }
3433
3434 void
3435 dwrite_f(int opno, long r)
3436 {
3437         int64_t         align;
3438         char            *buf;
3439         struct dioattr  diob;
3440         int             e;
3441         pathname_t      f;
3442         int             fd;
3443         size_t          len;
3444         int64_t         lr;
3445         off64_t         off;
3446         struct stat64   stb;
3447         int             v;
3448         char            st[1024];
3449         char            *dio_env;
3450
3451         init_pathname(&f);
3452         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3453                 if (v)
3454                         printf("%d/%d: dwrite - no filename\n", procid, opno);
3455                 free_pathname(&f);
3456                 return;
3457         }
3458         fd = open_path(&f, O_WRONLY|O_DIRECT);
3459         e = fd < 0 ? errno : 0;
3460         check_cwd();
3461         if (fd < 0) {
3462                 if (v)
3463                         printf("%d/%d: dwrite - open %s failed %d\n",
3464                                 procid, opno, f.path, e);
3465                 free_pathname(&f);
3466                 return;
3467         }
3468         if (fstat64(fd, &stb) < 0) {
3469                 if (v)
3470                         printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3471                                 procid, opno, f.path, errno);
3472                 free_pathname(&f);
3473                 close(fd);
3474                 return;
3475         }
3476         inode_info(st, sizeof(st), &stb, v);
3477         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3478                 if (v)
3479                         printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3480                                 " %s%s return %d, fallback to stat()\n",
3481                                procid, opno, f.path, st, errno);
3482                 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3483                 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3484         }
3485
3486         dio_env = getenv("XFS_DIO_MIN");
3487         if (dio_env)
3488                 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3489
3490         align = (int64_t)diob.d_miniosz;
3491         lr = ((int64_t)random() << 32) + random();
3492         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3493         off -= (off % align);
3494         lseek64(fd, off, SEEK_SET);
3495         len = (random() % FILELEN_MAX) + 1;
3496         len -= (len % align);
3497         if (len <= 0)
3498                 len = align;
3499         else if (len > diob.d_maxiosz) 
3500                 len = diob.d_maxiosz;
3501         buf = memalign(diob.d_mem, len);
3502         off %= maxfsize;
3503         lseek64(fd, off, SEEK_SET);
3504         memset(buf, nameseq & 0xff, len);
3505         e = write(fd, buf, len) < 0 ? errno : 0;
3506         free(buf);
3507         if (v)
3508                 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3509                        procid, opno, f.path, st, (long long)off, (int)len, e);
3510         free_pathname(&f);
3511         close(fd);
3512 }
3513
3514
3515 #ifdef HAVE_LINUX_FALLOC_H
3516 struct print_flags falloc_flags [] = {
3517         { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3518         { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3519         { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3520         { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3521         { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3522         { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3523         { -1, NULL}
3524 };
3525
3526 #define translate_falloc_flags(mode)    \
3527         ({translate_flags(mode, "|", falloc_flags);})
3528 #endif
3529
3530 void
3531 do_fallocate(int opno, long r, int mode)
3532 {
3533 #ifdef HAVE_LINUX_FALLOC_H
3534         int             e;
3535         pathname_t      f;
3536         int             fd;
3537         int64_t         lr;
3538         off64_t         off;
3539         off64_t         len;
3540         struct stat64   stb;
3541         int             v;
3542         char            st[1024];
3543
3544         init_pathname(&f);
3545         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3546                 if (v)
3547                         printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3548                 free_pathname(&f);
3549                 return;
3550         }
3551         fd = open_path(&f, O_RDWR);
3552         if (fd < 0) {
3553                 if (v)
3554                         printf("%d/%d: do_fallocate - open %s failed %d\n",
3555                                 procid, opno, f.path, errno);
3556                 free_pathname(&f);
3557                 return;
3558         }
3559         check_cwd();
3560         if (fstat64(fd, &stb) < 0) {
3561                 if (v)
3562                         printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3563                                 procid, opno, f.path, errno);
3564                 free_pathname(&f);
3565                 close(fd);
3566                 return;
3567         }
3568         inode_info(st, sizeof(st), &stb, v);
3569         lr = ((int64_t)random() << 32) + random();
3570         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3571         off %= maxfsize;
3572         len = (off64_t)(random() % (1024 * 1024));
3573         /*
3574          * Collapse/insert range requires off and len to be block aligned,
3575          * make it more likely to be the case.
3576          */
3577         if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3578                 (opno % 2)) {
3579                 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3580                 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3581         }
3582         mode |= FALLOC_FL_KEEP_SIZE & random();
3583         e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3584         if (v)
3585                 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3586                        procid, opno, translate_falloc_flags(mode),
3587                        f.path, st, (long long)off, (long long)len, e);
3588         free_pathname(&f);
3589         close(fd);
3590 #endif
3591 }
3592
3593 void
3594 fallocate_f(int opno, long r)
3595 {
3596 #ifdef HAVE_LINUX_FALLOC_H
3597         do_fallocate(opno, r, 0);
3598 #endif
3599 }
3600
3601 void
3602 fdatasync_f(int opno, long r)
3603 {
3604         int             e;
3605         pathname_t      f;
3606         int             fd;
3607         int             v;
3608
3609         init_pathname(&f);
3610         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3611                 if (v)
3612                         printf("%d/%d: fdatasync - no filename\n",
3613                                 procid, opno);
3614                 free_pathname(&f);
3615                 return;
3616         }
3617         fd = open_path(&f, O_WRONLY);
3618         e = fd < 0 ? errno : 0;
3619         check_cwd();
3620         if (fd < 0) {
3621                 if (v)
3622                         printf("%d/%d: fdatasync - open %s failed %d\n",
3623                                 procid, opno, f.path, e);
3624                 free_pathname(&f);
3625                 return;
3626         }
3627         e = fdatasync(fd) < 0 ? errno : 0;
3628         if (v)
3629                 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3630         free_pathname(&f);
3631         close(fd);
3632 }
3633
3634 #ifdef HAVE_LINUX_FIEMAP_H
3635 struct print_flags fiemap_flags[] = {
3636         { FIEMAP_FLAG_SYNC, "SYNC"},
3637         { FIEMAP_FLAG_XATTR, "XATTR"},
3638         { -1, NULL}
3639 };
3640
3641 #define translate_fiemap_flags(mode)    \
3642         ({translate_flags(mode, "|", fiemap_flags);})
3643 #endif
3644
3645 void
3646 fiemap_f(int opno, long r)
3647 {
3648 #ifdef HAVE_LINUX_FIEMAP_H
3649         int             e;
3650         pathname_t      f;
3651         int             fd;
3652         int64_t         lr;
3653         off64_t         off;
3654         struct stat64   stb;
3655         int             v;
3656         char            st[1024];
3657         int blocks_to_map;
3658         struct fiemap *fiemap;
3659
3660         init_pathname(&f);
3661         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3662                 if (v)
3663                         printf("%d/%d: fiemap - no filename\n", procid, opno);
3664                 free_pathname(&f);
3665                 return;
3666         }
3667         fd = open_path(&f, O_RDWR);
3668         e = fd < 0 ? errno : 0;
3669         check_cwd();
3670         if (fd < 0) {
3671                 if (v)
3672                         printf("%d/%d: fiemap - open %s failed %d\n",
3673                                 procid, opno, f.path, e);
3674                 free_pathname(&f);
3675                 return;
3676         }
3677         if (fstat64(fd, &stb) < 0) {
3678                 if (v)
3679                         printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3680                                 procid, opno, f.path, errno);
3681                 free_pathname(&f);
3682                 close(fd);
3683                 return;
3684         }
3685         inode_info(st, sizeof(st), &stb, v);
3686         blocks_to_map = random() & 0xffff;
3687         fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3688                         (blocks_to_map * sizeof(struct fiemap_extent)));
3689         if (!fiemap) {
3690                 if (v)
3691                         printf("%d/%d: malloc failed \n", procid, opno);
3692                 free_pathname(&f);
3693                 close(fd);
3694                 return;
3695         }
3696         lr = ((int64_t)random() << 32) + random();
3697         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3698         off %= maxfsize;
3699         fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3700         fiemap->fm_extent_count = blocks_to_map;
3701         fiemap->fm_mapped_extents = random() & 0xffff;
3702         fiemap->fm_start = off;
3703         fiemap->fm_length = ((int64_t)random() << 32) + random();
3704
3705         e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3706         if (v)
3707                 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3708                        procid, opno, f.path, st, (long long)fiemap->fm_start,
3709                        (long long) fiemap->fm_length,
3710                        translate_fiemap_flags(fiemap->fm_flags), e);
3711         free(fiemap);
3712         free_pathname(&f);
3713         close(fd);
3714 #endif
3715 }
3716
3717 void
3718 freesp_f(int opno, long r)
3719 {
3720         int             e;
3721         pathname_t      f;
3722         int             fd;
3723         struct xfs_flock64      fl;
3724         int64_t         lr;
3725         off64_t         off;
3726         struct stat64   stb;
3727         int             v;
3728         char            st[1024];
3729
3730         init_pathname(&f);
3731         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3732                 if (v)
3733                         printf("%d/%d: freesp - no filename\n", procid, opno);
3734                 free_pathname(&f);
3735                 return;
3736         }
3737         fd = open_path(&f, O_RDWR);
3738         e = fd < 0 ? errno : 0;
3739         check_cwd();
3740         if (fd < 0) {
3741                 if (v)
3742                         printf("%d/%d: freesp - open %s failed %d\n",
3743                                 procid, opno, f.path, e);
3744                 free_pathname(&f);
3745                 return;
3746         }
3747         if (fstat64(fd, &stb) < 0) {
3748                 if (v)
3749                         printf("%d/%d: freesp - fstat64 %s failed %d\n",
3750                                 procid, opno, f.path, errno);
3751                 free_pathname(&f);
3752                 close(fd);
3753                 return;
3754         }
3755         inode_info(st, sizeof(st), &stb, v);
3756         lr = ((int64_t)random() << 32) + random();
3757         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3758         off %= maxfsize;
3759         fl.l_whence = SEEK_SET;
3760         fl.l_start = off;
3761         fl.l_len = 0;
3762         e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3763         if (v)
3764                 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3765                        procid, opno, f.path, st, (long long)off, e);
3766         free_pathname(&f);
3767         close(fd);
3768 }
3769
3770 void
3771 fsync_f(int opno, long r)
3772 {
3773         int             e;
3774         pathname_t      f;
3775         int             fd;
3776         int             v;
3777
3778         init_pathname(&f);
3779         if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3780                 if (v)
3781                         printf("%d/%d: fsync - no filename\n", procid, opno);
3782                 free_pathname(&f);
3783                 return;
3784         }
3785         fd = open_file_or_dir(&f, O_WRONLY);
3786         e = fd < 0 ? errno : 0;
3787         check_cwd();
3788         if (fd < 0) {
3789                 if (v)
3790                         printf("%d/%d: fsync - open %s failed %d\n",
3791                                 procid, opno, f.path, e);
3792                 free_pathname(&f);
3793                 return;
3794         }
3795         e = fsync(fd) < 0 ? errno : 0;
3796         if (v)
3797                 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3798         free_pathname(&f);
3799         close(fd);
3800 }
3801
3802 char *
3803 gen_random_string(int len)
3804 {
3805         static const char charset[] = "0123456789"
3806                 "abcdefghijklmnopqrstuvwxyz"
3807                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3808         int i;
3809         char *s;
3810
3811         if (len == 0)
3812                 return NULL;
3813
3814         s = malloc(len);
3815         if (!s)
3816                 return NULL;
3817
3818         for (i = 0; i < len; i++)
3819                 s[i] = charset[random() % sizeof(charset)];
3820
3821         return s;
3822 }
3823
3824 void
3825 getattr_f(int opno, long r)
3826 {
3827         int             fd;
3828         int             e;
3829         pathname_t      f;
3830         uint            fl;
3831         int             v;
3832
3833         init_pathname(&f);
3834         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3835                 append_pathname(&f, ".");
3836         fd = open_path(&f, O_RDWR);
3837         e = fd < 0 ? errno : 0;
3838         check_cwd();
3839
3840         e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3841         if (v)
3842                 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3843         free_pathname(&f);
3844         close(fd);
3845 }
3846
3847 void
3848 getdents_f(int opno, long r)
3849 {
3850         DIR             *dir;
3851         pathname_t      f;
3852         int             v;
3853
3854         init_pathname(&f);
3855         if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3856                 append_pathname(&f, ".");
3857         dir = opendir_path(&f);
3858         check_cwd();
3859         if (dir == NULL) {
3860                 if (v)
3861                         printf("%d/%d: getdents - can't open %s\n",
3862                                 procid, opno, f.path);
3863                 free_pathname(&f);
3864                 return;
3865         }
3866         while (readdir64(dir) != NULL)
3867                 continue;
3868         if (v)
3869                 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3870         free_pathname(&f);
3871         closedir(dir);
3872 }
3873
3874 void
3875 getfattr_f(int opno, long r)
3876 {
3877         fent_t          *fep;
3878         int             e;
3879         pathname_t      f;
3880         int             v;
3881         char            name[XATTR_NAME_BUF_SIZE];
3882         char            *value = NULL;
3883         int             value_len;
3884         int             xattr_num;
3885
3886         init_pathname(&f);
3887         if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3888                 if (v)
3889                         printf("%d/%d: getfattr - no filename\n", procid, opno);
3890                 goto out;
3891         }
3892         check_cwd();
3893
3894         /*
3895          * If the file/dir has xattrs, pick one randomly, otherwise attempt
3896          * to read a xattr that doesn't exist (fgetxattr should fail with
3897          * errno set to ENOATTR (61) in this case).
3898          */
3899         if (fep->xattr_counter > 0)
3900                 xattr_num = (random() % fep->xattr_counter) + 1;
3901         else
3902                 xattr_num = 0;
3903
3904         e = generate_xattr_name(xattr_num, name, sizeof(name));
3905         if (e < 0) {
3906                 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3907                        procid, opno, f.path, e);
3908                 goto out;
3909         }
3910
3911         value_len = getxattr(f.path, name, NULL, 0);
3912         if (value_len < 0) {
3913                 if (v)
3914                         printf("%d/%d: getfattr file %s name %s failed %d\n",
3915                                procid, opno, f.path, name, errno);
3916                 goto out;
3917         }
3918
3919         /* A xattr without value.*/
3920         if (value_len == 0) {
3921                 e = 0;
3922                 goto out_log;
3923         }
3924
3925         value = malloc(value_len);
3926         if (!value) {
3927                 if (v)
3928                         printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3929                                procid, opno, f.path, value_len);
3930                 goto out;
3931         }
3932
3933         e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3934 out_log:
3935         if (v)
3936                 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3937                        procid, opno, f.path, name, value_len, e);
3938 out:
3939         free(value);
3940         free_pathname(&f);
3941 }
3942
3943 void
3944 link_f(int opno, long r)
3945 {
3946         int             e;
3947         pathname_t      f;
3948         fent_t          *fep;
3949         fent_t          *fep_src;
3950         flist_t         *flp;
3951         int             id;
3952         pathname_t      l;
3953         int             parid;
3954         int             v;
3955         int             v1;
3956
3957         init_pathname(&f);
3958         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3959                 if (v1)
3960                         printf("%d/%d: link - no file\n", procid, opno);
3961                 free_pathname(&f);
3962                 return;
3963         }
3964         if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3965                 parid = -1;
3966         else
3967                 parid = fep->id;
3968         v |= v1;
3969         init_pathname(&l);
3970         e = generate_fname(fep, flp - flist, &l, &id, &v1);
3971         v |= v1;
3972         if (!e) {
3973                 if (v) {
3974                         (void)fent_to_name(&l, fep);
3975                         printf("%d/%d: link - no filename from %s\n",
3976                                 procid, opno, l.path);
3977                 }
3978                 free_pathname(&l);
3979                 free_pathname(&f);
3980                 return;
3981         }
3982         e = link_path(&f, &l) < 0 ? errno : 0;
3983         check_cwd();
3984         if (e == 0)
3985                 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3986         if (v) {
3987                 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3988                         e);
3989                 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3990         }
3991         free_pathname(&l);
3992         free_pathname(&f);
3993 }
3994
3995 void
3996 listfattr_f(int opno, long r)
3997 {
3998         fent_t          *fep;
3999         int             e;
4000         pathname_t      f;
4001         int             v;
4002         char            *buffer = NULL;
4003         int             buffer_len;
4004
4005         init_pathname(&f);
4006         if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4007                 if (v)
4008                         printf("%d/%d: listfattr - no filename\n", procid, opno);
4009                 goto out;
4010         }
4011         check_cwd();
4012
4013         e = listxattr(f.path, NULL, 0);
4014         if (e < 0) {
4015                 if (v)
4016                         printf("%d/%d: listfattr %s failed %d\n",
4017                                procid, opno, f.path, errno);
4018                 goto out;
4019         }
4020         buffer_len = e;
4021         if (buffer_len == 0) {
4022                 if (v)
4023                         printf("%d/%d: listfattr %s - has no extended attributes\n",
4024                                procid, opno, f.path);
4025                 goto out;
4026         }
4027
4028         buffer = malloc(buffer_len);
4029         if (!buffer) {
4030                 if (v)
4031                         printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
4032                                procid, opno, f.path, buffer_len);
4033                 goto out;
4034         }
4035
4036         e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4037         if (v)
4038                 printf("%d/%d: listfattr %s buffer length %d %d\n",
4039                        procid, opno, f.path, buffer_len, e);
4040 out:
4041         free(buffer);
4042         free_pathname(&f);
4043 }
4044
4045 void
4046 mkdir_f(int opno, long r)
4047 {
4048         int             e;
4049         pathname_t      f;
4050         fent_t          *fep;
4051         int             id;
4052         int             parid;
4053         int             v;
4054         int             v1;
4055
4056         if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4057                 parid = -1;
4058         else
4059                 parid = fep->id;
4060         init_pathname(&f);
4061         e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4062         v |= v1;
4063         if (!e) {
4064                 if (v) {
4065                         (void)fent_to_name(&f, fep);
4066                         printf("%d/%d: mkdir - no filename from %s\n",
4067                                 procid, opno, f.path);
4068                 }
4069                 free_pathname(&f);
4070                 return;
4071         }
4072         e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4073         check_cwd();
4074         if (e == 0)
4075                 add_to_flist(FT_DIR, id, parid, 0);
4076         if (v) {
4077                 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
4078                 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4079         }
4080         free_pathname(&f);
4081 }
4082
4083 void
4084 mknod_f(int opno, long r)
4085 {
4086         int             e;
4087         pathname_t      f;
4088         fent_t          *fep;
4089         int             id;
4090         int             parid;
4091         int             v;
4092         int             v1;
4093
4094         if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4095                 parid = -1;
4096         else
4097                 parid = fep->id;
4098         init_pathname(&f);
4099         e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4100         v |= v1;
4101         if (!e) {
4102                 if (v) {
4103                         (void)fent_to_name(&f, fep);
4104                         printf("%d/%d: mknod - no filename from %s\n",
4105                                 procid, opno, f.path);
4106                 }
4107                 free_pathname(&f);
4108                 return;
4109         }
4110         e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4111         check_cwd();
4112         if (e == 0)
4113                 add_to_flist(FT_DEV, id, parid, 0);
4114         if (v) {
4115                 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
4116                 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4117         }
4118         free_pathname(&f);
4119 }
4120
4121 #ifdef HAVE_SYS_MMAN_H
4122 struct print_flags mmap_flags[] = {
4123         { MAP_SHARED, "SHARED"},
4124         { MAP_PRIVATE, "PRIVATE"},
4125         { -1, NULL}
4126 };
4127
4128 #define translate_mmap_flags(flags)       \
4129         ({translate_flags(flags, "|", mmap_flags);})
4130 #endif
4131
4132 void
4133 do_mmap(int opno, long r, int prot)
4134 {
4135 #ifdef HAVE_SYS_MMAN_H
4136         char            *addr;
4137         int             e;
4138         pathname_t      f;
4139         int             fd;
4140         size_t          len;
4141         int64_t         lr;
4142         off64_t         off;
4143         int             flags;
4144         struct stat64   stb;
4145         int             v;
4146         char            st[1024];
4147         sigjmp_buf      sigbus_jmpbuf;
4148
4149         init_pathname(&f);
4150         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4151                 if (v)
4152                         printf("%d/%d: do_mmap - no filename\n", procid, opno);
4153                 free_pathname(&f);
4154                 return;
4155         }
4156         fd = open_path(&f, O_RDWR);
4157         e = fd < 0 ? errno : 0;
4158         check_cwd();
4159         if (fd < 0) {
4160                 if (v)
4161                         printf("%d/%d: do_mmap - open %s failed %d\n",
4162                                procid, opno, f.path, e);
4163                 free_pathname(&f);
4164                 return;
4165         }
4166         if (fstat64(fd, &stb) < 0) {
4167                 if (v)
4168                         printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4169                                procid, opno, f.path, errno);
4170                 free_pathname(&f);
4171                 close(fd);
4172                 return;
4173         }
4174         inode_info(st, sizeof(st), &stb, v);
4175         if (stb.st_size == 0) {
4176                 if (v)
4177                         printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4178                                f.path, st);
4179                 free_pathname(&f);
4180                 close(fd);
4181                 return;
4182         }
4183
4184         lr = ((int64_t)random() << 32) + random();
4185         off = (off64_t)(lr % stb.st_size);
4186         off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4187         len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4188
4189         flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4190         addr = mmap(NULL, len, prot, flags, fd, off);
4191         e = (addr == MAP_FAILED) ? errno : 0;
4192         if (e) {
4193                 if (v)
4194                         printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4195                                procid, opno, f.path, st, (long long)off,
4196                                (int)len, translate_mmap_flags(flags), e);
4197                 free_pathname(&f);
4198                 close(fd);
4199                 return;
4200         }
4201
4202         if (prot & PROT_WRITE) {
4203                 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4204                         sigbus_jmp = &sigbus_jmpbuf;
4205                         memset(addr, nameseq & 0xff, len);
4206                 }
4207         } else {
4208                 char *buf;
4209                 if ((buf = malloc(len)) != NULL) {
4210                         memcpy(buf, addr, len);
4211                         free(buf);
4212                 }
4213         }
4214         munmap(addr, len);
4215         /* set NULL to stop other functions from doing siglongjmp */
4216         sigbus_jmp = NULL;
4217
4218         if (v)
4219                 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4220                        procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4221                        f.path, st, (long long)off, (int)len,
4222                        translate_mmap_flags(flags),
4223                        (e == 0) ? "0" : "Bus error");
4224
4225         free_pathname(&f);
4226         close(fd);
4227 #endif
4228 }
4229
4230 void
4231 mread_f(int opno, long r)
4232 {
4233 #ifdef HAVE_SYS_MMAN_H
4234         do_mmap(opno, r, PROT_READ);
4235 #endif
4236 }
4237
4238 void
4239 mwrite_f(int opno, long r)
4240 {
4241 #ifdef HAVE_SYS_MMAN_H
4242         do_mmap(opno, r, PROT_WRITE);
4243 #endif
4244 }
4245
4246 void
4247 punch_f(int opno, long r)
4248 {
4249 #ifdef HAVE_LINUX_FALLOC_H
4250         do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4251 #endif
4252 }
4253
4254 void
4255 zero_f(int opno, long r)
4256 {
4257 #ifdef HAVE_LINUX_FALLOC_H
4258         do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4259 #endif
4260 }
4261
4262 void
4263 collapse_f(int opno, long r)
4264 {
4265 #ifdef HAVE_LINUX_FALLOC_H
4266         do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4267 #endif
4268 }
4269
4270 void
4271 insert_f(int opno, long r)
4272 {
4273 #ifdef HAVE_LINUX_FALLOC_H
4274         do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4275 #endif
4276 }
4277
4278 void
4279 read_f(int opno, long r)
4280 {
4281         char            *buf;
4282         int             e;
4283         pathname_t      f;
4284         int             fd;
4285         size_t          len;
4286         int64_t         lr;
4287         off64_t         off;
4288         struct stat64   stb;
4289         int             v;
4290         char            st[1024];
4291
4292         init_pathname(&f);
4293         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4294                 if (v)
4295                         printf("%d/%d: read - no filename\n", procid, opno);
4296                 free_pathname(&f);
4297                 return;
4298         }
4299         fd = open_path(&f, O_RDONLY);
4300         e = fd < 0 ? errno : 0;
4301         check_cwd();
4302         if (fd < 0) {
4303                 if (v)
4304                         printf("%d/%d: read - open %s failed %d\n",
4305                                 procid, opno, f.path, e);
4306                 free_pathname(&f);
4307                 return;
4308         }
4309         if (fstat64(fd, &stb) < 0) {
4310                 if (v)
4311                         printf("%d/%d: read - fstat64 %s failed %d\n",
4312                                 procid, opno, f.path, errno);
4313                 free_pathname(&f);
4314                 close(fd);
4315                 return;
4316         }
4317         inode_info(st, sizeof(st), &stb, v);
4318         if (stb.st_size == 0) {
4319                 if (v)
4320                         printf("%d/%d: read - %s%s zero size\n", procid, opno,
4321                                f.path, st);
4322                 free_pathname(&f);
4323                 close(fd);
4324                 return;
4325         }
4326         lr = ((int64_t)random() << 32) + random();
4327         off = (off64_t)(lr % stb.st_size);
4328         lseek64(fd, off, SEEK_SET);
4329         len = (random() % FILELEN_MAX) + 1;
4330         buf = malloc(len);
4331         e = read(fd, buf, len) < 0 ? errno : 0;
4332         free(buf);
4333         if (v)
4334                 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4335                        procid, opno, f.path, st, (long long)off, (int)len, e);
4336         free_pathname(&f);
4337         close(fd);
4338 }
4339
4340 void
4341 readlink_f(int opno, long r)
4342 {
4343         char            buf[PATH_MAX];
4344         int             e;
4345         pathname_t      f;
4346         int             v;
4347
4348         init_pathname(&f);
4349         if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4350                 if (v)
4351                         printf("%d/%d: readlink - no filename\n", procid, opno);
4352                 free_pathname(&f);
4353                 return;
4354         }
4355         e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4356         check_cwd();
4357         if (v)
4358                 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4359         free_pathname(&f);
4360 }
4361
4362 void
4363 readv_f(int opno, long r)
4364 {
4365         char            *buf;
4366         int             e;
4367         pathname_t      f;
4368         int             fd;
4369         size_t          len;
4370         int64_t         lr;
4371         off64_t         off;
4372         struct stat64   stb;
4373         int             v;
4374         char            st[1024];
4375         struct iovec    *iov = NULL;
4376         int             iovcnt;
4377         size_t          iovb;
4378         size_t          iovl;
4379         int             i;
4380
4381         init_pathname(&f);
4382         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4383                 if (v)
4384                         printf("%d/%d: readv - no filename\n", procid, opno);
4385                 free_pathname(&f);
4386                 return;
4387         }
4388         fd = open_path(&f, O_RDONLY);
4389         e = fd < 0 ? errno : 0;
4390         check_cwd();
4391         if (fd < 0) {
4392                 if (v)
4393                         printf("%d/%d: readv - open %s failed %d\n",
4394                                 procid, opno, f.path, e);
4395                 free_pathname(&f);
4396                 return;
4397         }
4398         if (fstat64(fd, &stb) < 0) {
4399                 if (v)
4400                         printf("%d/%d: readv - fstat64 %s failed %d\n",
4401                                 procid, opno, f.path, errno);
4402                 free_pathname(&f);
4403                 close(fd);
4404                 return;
4405         }
4406         inode_info(st, sizeof(st), &stb, v);
4407         if (stb.st_size == 0) {
4408                 if (v)
4409                         printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4410                                f.path, st);
4411                 free_pathname(&f);
4412                 close(fd);
4413                 return;
4414         }
4415         lr = ((int64_t)random() << 32) + random();
4416         off = (off64_t)(lr % stb.st_size);
4417         lseek64(fd, off, SEEK_SET);
4418         len = (random() % FILELEN_MAX) + 1;
4419         buf = malloc(len);
4420
4421         iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4422         iov = calloc(iovcnt, sizeof(struct iovec));
4423         iovl = len / iovcnt;
4424         iovb = 0;
4425         for (i=0; i<iovcnt; i++) {
4426                 (iov + i)->iov_base = (buf + iovb);
4427                 (iov + i)->iov_len  = iovl;
4428                 iovb += iovl;
4429         }
4430
4431         e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4432         free(buf);
4433         if (v)
4434                 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4435                        procid, opno, f.path, st, (long long)off, (int)iovl,
4436                        iovcnt, e);
4437         free_pathname(&f);
4438         close(fd);
4439 }
4440
4441 void
4442 removefattr_f(int opno, long r)
4443 {
4444         fent_t          *fep;
4445         int             e;
4446         pathname_t      f;
4447         int             v;
4448         char            name[XATTR_NAME_BUF_SIZE];
4449         int             xattr_num;
4450
4451         init_pathname(&f);
4452         if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4453                 if (v)
4454                         printf("%d/%d: removefattr - no filename\n", procid, opno);
4455                 goto out;
4456         }
4457         check_cwd();
4458
4459         /*
4460          * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4461          * remove a xattr that doesn't exist (fremovexattr should fail with
4462          * errno set to ENOATTR (61) in this case).
4463          */
4464         if (fep->xattr_counter > 0)
4465                 xattr_num = (random() % fep->xattr_counter) + 1;
4466         else
4467                 xattr_num = 0;
4468
4469         e = generate_xattr_name(xattr_num, name, sizeof(name));
4470         if (e < 0) {
4471                 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4472                        procid, opno, f.path, e);
4473                 goto out;
4474         }
4475
4476         e = removexattr(f.path, name) < 0 ? errno : 0;
4477         if (v)
4478                 printf("%d/%d: removefattr file %s name %s %d\n",
4479                        procid, opno, f.path, name, e);
4480 out:
4481         free_pathname(&f);
4482 }
4483
4484 struct print_flags renameat2_flags [] = {
4485         { RENAME_NOREPLACE, "NOREPLACE"},
4486         { RENAME_EXCHANGE, "EXCHANGE"},
4487         { RENAME_WHITEOUT, "WHITEOUT"},
4488         { -1, NULL}
4489 };
4490
4491 #define translate_renameat2_flags(mode)        \
4492         ({translate_flags(mode, "|", renameat2_flags);})
4493
4494 void
4495 do_renameat2(int opno, long r, int mode)
4496 {
4497         fent_t          *dfep;
4498         int             e;
4499         pathname_t      f;
4500         fent_t          *fep;
4501         flist_t         *flp;
4502         int             id;
4503         pathname_t      newf;
4504         int             oldid;
4505         int             parid;
4506         int             oldparid;
4507         int             which;
4508         int             v;
4509         int             v1;
4510
4511         /* get an existing path for the source of the rename */
4512         init_pathname(&f);
4513         which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4514         if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4515                 if (v1)
4516                         printf("%d/%d: rename - no source filename\n",
4517                                 procid, opno);
4518                 free_pathname(&f);
4519                 return;
4520         }
4521
4522         /*
4523          * Both pathnames must exist for the RENAME_EXCHANGE, and in
4524          * order to maintain filelist/filename integrity, we should
4525          * restrict exchange operation to files of the same type.
4526          */
4527         if (mode == RENAME_EXCHANGE) {
4528                 which = 1 << (flp - flist);
4529                 init_pathname(&newf);
4530                 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4531                         if (v)
4532                                 printf("%d/%d: rename - no target filename\n",
4533                                         procid, opno);
4534                         free_pathname(&newf);
4535                         free_pathname(&f);
4536                         return;
4537                 }
4538                 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4539                     fents_ancestor_check(dfep, fep))) {
4540                         if (v)
4541                                 printf("%d/%d: rename(REXCHANGE) %s and %s "
4542                                         "have ancestor-descendant relationship\n",
4543                                         procid, opno, f.path, newf.path);
4544                         free_pathname(&newf);
4545                         free_pathname(&f);
4546                         return;
4547                 }
4548                 v |= v1;
4549                 id = dfep->id;
4550                 parid = dfep->parent;
4551         } else {
4552                 /*
4553                  * Get an existing directory for the destination parent
4554                  * directory name.
4555                  */
4556                 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4557                         parid = -1;
4558                 else
4559                         parid = dfep->id;
4560                 v |= v1;
4561
4562                 /*
4563                  * Generate a new path using an existing parent directory
4564                  * in name.
4565                  */
4566                 init_pathname(&newf);
4567                 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4568                 v |= v1;
4569                 if (!e) {
4570                         if (v) {
4571                                 (void)fent_to_name(&f, dfep);
4572                                 printf("%d/%d: rename - no filename from %s\n",
4573                                         procid, opno, f.path);
4574                         }
4575                         free_pathname(&newf);
4576                         free_pathname(&f);
4577                         return;
4578                 }
4579         }
4580         e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4581         check_cwd();
4582         if (e == 0) {
4583                 int xattr_counter = fep->xattr_counter;
4584                 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4585                 int ft = flp - flist;
4586
4587                 oldid = fep->id;
4588                 oldparid = fep->parent;
4589
4590                 /*
4591                  * Swap the parent ids for RENAME_EXCHANGE, and replace the
4592                  * old parent id for the others.
4593                  */
4594                 if (ft == FT_DIR || ft == FT_SUBVOL)
4595                         fix_parent(oldid, id, swap);
4596
4597                 if (mode == RENAME_WHITEOUT) {
4598                         fep->xattr_counter = 0;
4599                         add_to_flist(flp - flist, id, parid, xattr_counter);
4600                 } else if (mode == RENAME_EXCHANGE) {
4601                         fep->xattr_counter = dfep->xattr_counter;
4602                         dfep->xattr_counter = xattr_counter;
4603                 } else {
4604                         del_from_flist(flp - flist, fep - flp->fents);
4605                         add_to_flist(flp - flist, id, parid, xattr_counter);
4606                 }
4607         }
4608         if (v) {
4609                 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4610                         opno, translate_renameat2_flags(mode), f.path,
4611                         newf.path, e);
4612                 if (e == 0) {
4613                         printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4614                                 procid, opno, oldid, oldparid);
4615                         printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4616                                 procid, opno, id, parid);
4617                 }
4618         }
4619         free_pathname(&newf);
4620         free_pathname(&f);
4621 }
4622
4623 void
4624 rename_f(int opno, long r)
4625 {
4626         do_renameat2(opno, r, 0);
4627 }
4628
4629 void
4630 rnoreplace_f(int opno, long r)
4631 {
4632         do_renameat2(opno, r, RENAME_NOREPLACE);
4633 }
4634
4635 void
4636 rexchange_f(int opno, long r)
4637 {
4638         do_renameat2(opno, r, RENAME_EXCHANGE);
4639 }
4640
4641 void
4642 rwhiteout_f(int opno, long r)
4643 {
4644         do_renameat2(opno, r, RENAME_WHITEOUT);
4645 }
4646
4647 void
4648 resvsp_f(int opno, long r)
4649 {
4650         int             e;
4651         pathname_t      f;
4652         int             fd;
4653         struct xfs_flock64      fl;
4654         int64_t         lr;
4655         off64_t         off;
4656         struct stat64   stb;
4657         int             v;
4658         char            st[1024];
4659
4660         init_pathname(&f);
4661         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4662                 if (v)
4663                         printf("%d/%d: resvsp - no filename\n", procid, opno);
4664                 free_pathname(&f);
4665                 return;
4666         }
4667         fd = open_path(&f, O_RDWR);
4668         e = fd < 0 ? errno : 0;
4669         check_cwd();
4670         if (fd < 0) {
4671                 if (v)
4672                         printf("%d/%d: resvsp - open %s failed %d\n",
4673                                 procid, opno, f.path, e);
4674                 free_pathname(&f);
4675                 return;
4676         }
4677         if (fstat64(fd, &stb) < 0) {
4678                 if (v)
4679                         printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4680                                 procid, opno, f.path, errno);
4681                 free_pathname(&f);
4682                 close(fd);
4683                 return;
4684         }
4685         inode_info(st, sizeof(st), &stb, v);
4686         lr = ((int64_t)random() << 32) + random();
4687         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4688         off %= maxfsize;
4689         fl.l_whence = SEEK_SET;
4690         fl.l_start = off;
4691         fl.l_len = (off64_t)(random() % (1024 * 1024));
4692         e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4693         if (v)
4694                 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4695                        procid, opno, f.path, st,
4696                         (long long)off, (long long)fl.l_len, e);
4697         free_pathname(&f);
4698         close(fd);
4699 }
4700
4701 void
4702 rmdir_f(int opno, long r)
4703 {
4704         int             e;
4705         pathname_t      f;
4706         fent_t          *fep;
4707         int             oldid;
4708         int             oldparid;
4709         int             v;
4710
4711         init_pathname(&f);
4712         if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4713                 if (v)
4714                         printf("%d/%d: rmdir - no directory\n", procid, opno);
4715                 free_pathname(&f);
4716                 return;
4717         }
4718         e = rmdir_path(&f) < 0 ? errno : 0;
4719         check_cwd();
4720         if (e == 0) {
4721                 oldid = fep->id;
4722                 oldparid = fep->parent;
4723                 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4724         }
4725         if (v) {
4726                 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4727                 if (e == 0)
4728                         printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4729                                 procid, opno, oldid, oldparid);
4730         }
4731         free_pathname(&f);
4732 }
4733
4734 void
4735 setattr_f(int opno, long r)
4736 {
4737         int             fd;
4738         int             e;
4739         pathname_t      f;
4740         uint            fl;
4741         int             v;
4742
4743         init_pathname(&f);
4744         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4745                 append_pathname(&f, ".");
4746         fd = open_path(&f, O_RDWR);
4747         e = fd < 0 ? errno : 0;
4748         check_cwd();
4749
4750         fl = attr_mask & (uint)random();
4751         e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4752         if (v)
4753                 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4754         free_pathname(&f);
4755         close(fd);
4756 }
4757
4758 void
4759 setfattr_f(int opno, long r)
4760 {
4761         int             e;
4762         pathname_t      f;
4763         fent_t          *fep;
4764         int             v;
4765         int             value_len;
4766         char            name[XATTR_NAME_BUF_SIZE];
4767         char            *value = NULL;
4768         int             flag = 0;
4769         int             xattr_num;
4770
4771         init_pathname(&f);
4772         if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4773                 if (v)
4774                         printf("%d/%d: setfattr - no filename\n", procid, opno);
4775                 goto out;
4776         }
4777         check_cwd();
4778
4779         if ((fep->xattr_counter > 0) && (random() % 2)) {
4780                 /*
4781                  * Use an existing xattr name for replacing its value or
4782                  * create again a xattr that was previously deleted.
4783                  */
4784                 xattr_num = (random() % fep->xattr_counter) + 1;
4785                 if (random() % 2)
4786                         flag = XATTR_REPLACE;
4787         } else {
4788                 /* Use a new xattr name. */
4789                 xattr_num = fep->xattr_counter + 1;
4790                 /*
4791                  * Don't always use the create flag because even if our xattr
4792                  * counter is 0, we may still have xattrs associated to this
4793                  * file (this happens when xattrs are added to a file through
4794                  * one of its other hard links), so we can end up updating an
4795                  * existing xattr too.
4796                  */
4797                 if (random() % 2)
4798                         flag = XATTR_CREATE;
4799         }
4800
4801         /*
4802          * The maximum supported value size depends on the filesystem
4803          * implementation, but 100 bytes is a safe value for most filesystems
4804          * at least.
4805          */
4806         value_len = random() % 101;
4807         value = gen_random_string(value_len);
4808         if (!value && value_len > 0) {
4809                 if (v)
4810                         printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4811                                procid, opno, f.path, value_len);
4812                 goto out;
4813         }
4814         e = generate_xattr_name(xattr_num, name, sizeof(name));
4815         if (e < 0) {
4816                 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4817                        procid, opno, f.path, e);
4818                 goto out;
4819         }
4820
4821         e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4822         if (e == 0)
4823                 fep->xattr_counter++;
4824         if (v)
4825                 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4826                        procid, opno, f.path, name, xattr_flag_to_string(flag),
4827                        value_len, e);
4828 out:
4829         free(value);
4830         free_pathname(&f);
4831 }
4832
4833 void
4834 snapshot_f(int opno, long r)
4835 {
4836 #ifdef HAVE_BTRFSUTIL_H
4837         enum btrfs_util_error   e;
4838         pathname_t              f;
4839         pathname_t              newf;
4840         fent_t                  *fep;
4841         int                     id;
4842         int                     parid;
4843         int                     v;
4844         int                     v1;
4845         int                     err;
4846
4847         init_pathname(&f);
4848         if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4849                 if (v)
4850                         printf("%d/%d: snapshot - no subvolume\n", procid,
4851                                opno);
4852                 free_pathname(&f);
4853                 return;
4854         }
4855         init_pathname(&newf);
4856         parid = fep->id;
4857         err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4858         v |= v1;
4859         if (!err) {
4860                 if (v) {
4861                         (void)fent_to_name(&f, fep);
4862                         printf("%d/%d: snapshot - no filename from %s\n",
4863                                procid, opno, f.path);
4864                 }
4865                 free_pathname(&f);
4866                 return;
4867         }
4868         e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4869         if (e == BTRFS_UTIL_OK)
4870                 add_to_flist(FT_SUBVOL, id, parid, 0);
4871         if (v) {
4872                 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4873                        f.path, newf.path, e, btrfs_util_strerror(e));
4874                 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4875                        id, parid);
4876         }
4877         free_pathname(&newf);
4878         free_pathname(&f);
4879 #endif
4880 }
4881
4882 void
4883 stat_f(int opno, long r)
4884 {
4885         int             e;
4886         pathname_t      f;
4887         struct stat64   stb;
4888         int             v;
4889
4890         init_pathname(&f);
4891         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4892                 if (v)
4893                         printf("%d/%d: stat - no entries\n", procid, opno);
4894                 free_pathname(&f);
4895                 return;
4896         }
4897         e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4898         check_cwd();
4899         if (v)
4900                 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4901         free_pathname(&f);
4902 }
4903
4904 void
4905 subvol_create_f(int opno, long r)
4906 {
4907 #ifdef HAVE_BTRFSUTIL_H
4908         enum btrfs_util_error   e;
4909         pathname_t              f;
4910         fent_t                  *fep;
4911         int                     id;
4912         int                     parid;
4913         int                     v;
4914         int                     v1;
4915         int                     err;
4916
4917         init_pathname(&f);
4918         if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4919                 parid = -1;
4920         else
4921                 parid = fep->id;
4922         err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4923         v |= v1;
4924         if (!err) {
4925                 if (v) {
4926                         (void)fent_to_name(&f, fep);
4927                         printf("%d/%d: subvol_create - no filename from %s\n",
4928                                procid, opno, f.path);
4929                 }
4930                 free_pathname(&f);
4931                 return;
4932         }
4933         e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4934         if (e == BTRFS_UTIL_OK)
4935                 add_to_flist(FT_SUBVOL, id, parid, 0);
4936         if (v) {
4937                 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4938                        f.path, e, btrfs_util_strerror(e));
4939                 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4940                        opno, id, parid);
4941         }
4942         free_pathname(&f);
4943 #endif
4944 }
4945
4946 void
4947 subvol_delete_f(int opno, long r)
4948 {
4949 #ifdef HAVE_BTRFSUTIL_H
4950         enum btrfs_util_error   e;
4951         pathname_t              f;
4952         fent_t                  *fep;
4953         int                     v;
4954         int                     oldid;
4955         int                     oldparid;
4956
4957         init_pathname(&f);
4958         if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4959                 if (v)
4960                         printf("%d:%d: subvol_delete - no subvolume\n", procid,
4961                                opno);
4962                 free_pathname(&f);
4963                 return;
4964         }
4965         e = btrfs_util_delete_subvolume(f.path, 0);
4966         check_cwd();
4967         if (e == BTRFS_UTIL_OK) {
4968                 oldid = fep->id;
4969                 oldparid = fep->parent;
4970                 delete_subvol_children(oldid);
4971                 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4972         }
4973         if (v) {
4974                 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4975                        e, btrfs_util_strerror(e));
4976                 if (e == BTRFS_UTIL_OK)
4977                         printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4978                                procid, opno, oldid, oldparid);
4979         }
4980         free_pathname(&f);
4981 #endif
4982 }
4983
4984 void
4985 symlink_f(int opno, long r)
4986 {
4987         int             e;
4988         pathname_t      f;
4989         fent_t          *fep;
4990         int             i;
4991         int             id;
4992         int             len;
4993         int             parid;
4994         int             v;
4995         int             v1;
4996         char            *val;
4997
4998         if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4999                 parid = -1;
5000         else
5001                 parid = fep->id;
5002         init_pathname(&f);
5003         e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5004         v |= v1;
5005         if (!e) {
5006                 if (v) {
5007                         (void)fent_to_name(&f, fep);
5008                         printf("%d/%d: symlink - no filename from %s\n",
5009                                 procid, opno, f.path);
5010                 }
5011                 free_pathname(&f);
5012                 return;
5013         }
5014         len = (int)(random() % PATH_MAX);
5015         val = malloc(len + 1);
5016         if (len)
5017                 memset(val, 'x', len);
5018         val[len] = '\0';
5019         for (i = 10; i < len - 1; i += 10)
5020                 val[i] = '/';
5021         e = symlink_path(val, &f) < 0 ? errno : 0;
5022         check_cwd();
5023         if (e == 0)
5024                 add_to_flist(FT_SYM, id, parid, 0);
5025         free(val);
5026         if (v) {
5027                 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
5028                 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5029         }
5030         free_pathname(&f);
5031 }
5032
5033 /* ARGSUSED */
5034 void
5035 sync_f(int opno, long r)
5036 {
5037         sync();
5038         if (verbose)
5039                 printf("%d/%d: sync\n", procid, opno);
5040 }
5041
5042 void
5043 truncate_f(int opno, long r)
5044 {
5045         int             e;
5046         pathname_t      f;
5047         int64_t         lr;
5048         off64_t         off;
5049         struct stat64   stb;
5050         int             v;
5051         char            st[1024];
5052
5053         init_pathname(&f);
5054         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5055                 if (v)
5056                         printf("%d/%d: truncate - no filename\n", procid, opno);
5057                 free_pathname(&f);
5058                 return;
5059         }
5060         e = stat64_path(&f, &stb) < 0 ? errno : 0;
5061         check_cwd();
5062         if (e > 0) {
5063                 if (v)
5064                         printf("%d/%d: truncate - stat64 %s failed %d\n",
5065                                 procid, opno, f.path, e);
5066                 free_pathname(&f);
5067                 return;
5068         }
5069         inode_info(st, sizeof(st), &stb, v);
5070         lr = ((int64_t)random() << 32) + random();
5071         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5072         off %= maxfsize;
5073         e = truncate64_path(&f, off) < 0 ? errno : 0;
5074         check_cwd();
5075         if (v)
5076                 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
5077                        st, (long long)off, e);
5078         free_pathname(&f);
5079 }
5080
5081 void
5082 unlink_f(int opno, long r)
5083 {
5084         int             e;
5085         pathname_t      f;
5086         fent_t          *fep;
5087         flist_t         *flp;
5088         int             oldid;
5089         int             oldparid;
5090         int             v;
5091
5092         init_pathname(&f);
5093         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5094                 if (v)
5095                         printf("%d/%d: unlink - no file\n", procid, opno);
5096                 free_pathname(&f);
5097                 return;
5098         }
5099         e = unlink_path(&f) < 0 ? errno : 0;
5100         check_cwd();
5101         if (e == 0) {
5102                 oldid = fep->id;
5103                 oldparid = fep->parent;
5104                 del_from_flist(flp - flist, fep - flp->fents);
5105         }
5106         if (v) {
5107                 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
5108                 if (e == 0)
5109                         printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
5110                                 procid, opno, oldid, oldparid);
5111         }
5112         free_pathname(&f);
5113 }
5114
5115 void
5116 unresvsp_f(int opno, long r)
5117 {
5118         int             e;
5119         pathname_t      f;
5120         int             fd;
5121         struct xfs_flock64      fl;
5122         int64_t         lr;
5123         off64_t         off;
5124         struct stat64   stb;
5125         int             v;
5126         char            st[1024];
5127
5128         init_pathname(&f);
5129         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5130                 if (v)
5131                         printf("%d/%d: unresvsp - no filename\n", procid, opno);
5132                 free_pathname(&f);
5133                 return;
5134         }
5135         fd = open_path(&f, O_RDWR);
5136         e = fd < 0 ? errno : 0;
5137         check_cwd();
5138         if (fd < 0) {
5139                 if (v)
5140                         printf("%d/%d: unresvsp - open %s failed %d\n",
5141                                 procid, opno, f.path, e);
5142                 free_pathname(&f);
5143                 return;
5144         }
5145         if (fstat64(fd, &stb) < 0) {
5146                 if (v)
5147                         printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5148                                 procid, opno, f.path, errno);
5149                 free_pathname(&f);
5150                 close(fd);
5151                 return;
5152         }
5153         inode_info(st, sizeof(st), &stb, v);
5154         lr = ((int64_t)random() << 32) + random();
5155         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5156         off %= maxfsize;
5157         fl.l_whence = SEEK_SET;
5158         fl.l_start = off;
5159         fl.l_len = (off64_t)(random() % (1 << 20));
5160         e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5161         if (v)
5162                 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5163                        procid, opno, f.path, st,
5164                         (long long)off, (long long)fl.l_len, e);
5165         free_pathname(&f);
5166         close(fd);
5167 }
5168
5169 void
5170 uring_read_f(int opno, long r)
5171 {
5172 #ifdef URING
5173         do_uring_rw(opno, r, O_RDONLY);
5174 #endif
5175 }
5176
5177 void
5178 uring_write_f(int opno, long r)
5179 {
5180 #ifdef URING
5181         do_uring_rw(opno, r, O_WRONLY);
5182 #endif
5183 }
5184
5185 void
5186 write_f(int opno, long r)
5187 {
5188         char            *buf;
5189         int             e;
5190         pathname_t      f;
5191         int             fd;
5192         size_t          len;
5193         int64_t         lr;
5194         off64_t         off;
5195         struct stat64   stb;
5196         int             v;
5197         char            st[1024];
5198
5199         init_pathname(&f);
5200         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5201                 if (v)
5202                         printf("%d/%d: write - no filename\n", procid, opno);
5203                 free_pathname(&f);
5204                 return;
5205         }
5206         fd = open_path(&f, O_WRONLY);
5207         e = fd < 0 ? errno : 0;
5208         check_cwd();
5209         if (fd < 0) {
5210                 if (v)
5211                         printf("%d/%d: write - open %s failed %d\n",
5212                                 procid, opno, f.path, e);
5213                 free_pathname(&f);
5214                 return;
5215         }
5216         if (fstat64(fd, &stb) < 0) {
5217                 if (v)
5218                         printf("%d/%d: write - fstat64 %s failed %d\n",
5219                                 procid, opno, f.path, errno);
5220                 free_pathname(&f);
5221                 close(fd);
5222                 return;
5223         }
5224         inode_info(st, sizeof(st), &stb, v);
5225         lr = ((int64_t)random() << 32) + random();
5226         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5227         off %= maxfsize;
5228         lseek64(fd, off, SEEK_SET);
5229         len = (random() % FILELEN_MAX) + 1;
5230         buf = malloc(len);
5231         memset(buf, nameseq & 0xff, len);
5232         e = write(fd, buf, len) < 0 ? errno : 0;
5233         free(buf);
5234         if (v)
5235                 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5236                        procid, opno, f.path, st, (long long)off, (int)len, e);
5237         free_pathname(&f);
5238         close(fd);
5239 }
5240
5241 void
5242 writev_f(int opno, long r)
5243 {
5244         char            *buf;
5245         int             e;
5246         pathname_t      f;
5247         int             fd;
5248         size_t          len;
5249         int64_t         lr;
5250         off64_t         off;
5251         struct stat64   stb;
5252         int             v;
5253         char            st[1024];
5254         struct iovec    *iov = NULL;
5255         int             iovcnt;
5256         size_t          iovb;
5257         size_t          iovl;
5258         int             i;
5259
5260         init_pathname(&f);
5261         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5262                 if (v)
5263                         printf("%d/%d: writev - no filename\n", procid, opno);
5264                 free_pathname(&f);
5265                 return;
5266         }
5267         fd = open_path(&f, O_WRONLY);
5268         e = fd < 0 ? errno : 0;
5269         check_cwd();
5270         if (fd < 0) {
5271                 if (v)
5272                         printf("%d/%d: writev - open %s failed %d\n",
5273                                 procid, opno, f.path, e);
5274                 free_pathname(&f);
5275                 return;
5276         }
5277         if (fstat64(fd, &stb) < 0) {
5278                 if (v)
5279                         printf("%d/%d: writev - fstat64 %s failed %d\n",
5280                                 procid, opno, f.path, errno);
5281                 free_pathname(&f);
5282                 close(fd);
5283                 return;
5284         }
5285         inode_info(st, sizeof(st), &stb, v);
5286         lr = ((int64_t)random() << 32) + random();
5287         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5288         off %= maxfsize;
5289         lseek64(fd, off, SEEK_SET);
5290         len = (random() % FILELEN_MAX) + 1;
5291         buf = malloc(len);
5292         memset(buf, nameseq & 0xff, len);
5293
5294         iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5295         iov = calloc(iovcnt, sizeof(struct iovec));
5296         iovl = len / iovcnt;
5297         iovb = 0;
5298         for (i=0; i<iovcnt; i++) {
5299                 (iov + i)->iov_base = (buf + iovb);
5300                 (iov + i)->iov_len  = iovl;
5301                 iovb += iovl;
5302         }
5303
5304         e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5305         free(buf);
5306         free(iov);
5307         if (v)
5308                 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5309                        procid, opno, f.path, st, (long long)off, (int)iovl,
5310                        iovcnt, e);
5311         free_pathname(&f);
5312         close(fd);
5313 }
5314
5315 char *
5316 xattr_flag_to_string(int flag)
5317 {
5318         if (flag == XATTR_CREATE)
5319                 return "create";
5320         if (flag == XATTR_REPLACE)
5321                 return "replace";
5322         return "none";
5323 }