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