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