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