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