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