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