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