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