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