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