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