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