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