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