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