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