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