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