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