xfstests updates - rework build to be like other xfs packages, revive some old fs...
[xfstests-dev.git] / ltp / fsstress.c
1 /*
2  * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <xfs/libxfs.h>
34 #include <attr/xattr.h>
35 #include <attr/attributes.h>
36 #include <sys/statvfs.h>
37 #include <sys/time.h>
38 #include <sys/wait.h>
39 #include <dirent.h>
40
41 #define XFS_ERRTAG_MAX          17
42
43 typedef enum {
44         OP_ALLOCSP,
45         OP_ATTR_REMOVE,
46         OP_ATTR_SET,
47         OP_BULKSTAT,
48         OP_BULKSTAT1,
49         OP_CHOWN,
50         OP_CREAT,
51         OP_DREAD,
52         OP_DWRITE,
53         OP_FDATASYNC,
54         OP_FREESP,
55         OP_FSYNC,
56         OP_GETDENTS,
57         OP_LINK,
58         OP_MKDIR,
59         OP_MKNOD,
60         OP_READ,
61         OP_READLINK,
62         OP_RENAME,
63         OP_RESVSP,
64         OP_RMDIR,
65         OP_STAT,
66         OP_SYMLINK,
67         OP_SYNC,
68         OP_TRUNCATE,
69         OP_UNLINK,
70         OP_UNRESVSP,
71         OP_WRITE,
72         OP_LAST
73 } opty_t;
74
75 typedef void (*opfnc_t)(int, long);
76
77 typedef struct opdesc {
78         opty_t  op;
79         char    *name;
80         opfnc_t func;
81         int     freq;
82         int     iswrite;
83 } opdesc_t;
84
85 typedef struct fent {
86         int     id;
87         int     parent;
88 } fent_t;
89
90 typedef struct flist {
91         int     nfiles;
92         int     nslots;
93         int     tag;
94         fent_t  *fents;
95 } flist_t;
96
97 typedef struct pathname {
98         int     len;
99         char    *path;
100 } pathname_t;
101
102 #define FT_DIR  0
103 #define FT_DIRm (1 << FT_DIR)
104 #define FT_REG  1
105 #define FT_REGm (1 << FT_REG)
106 #define FT_SYM  2
107 #define FT_SYMm (1 << FT_SYM)
108 #define FT_DEV  3
109 #define FT_DEVm (1 << FT_DEV)
110 #define FT_RTF  4
111 #define FT_RTFm (1 << FT_RTF)
112 #define FT_nft  5
113 #define FT_ANYm ((1 << FT_nft) - 1)
114 #define FT_REGFILE      (FT_REGm | FT_RTFm)
115 #define FT_NOTDIR       (FT_ANYm & ~FT_DIRm)
116
117 #define FLIST_SLOT_INCR 16
118 #define NDCACHE 64
119
120 #define MAXFSIZE        ((1ULL << 63) - 1ULL)
121 #define MAXFSIZE32      ((1ULL << 40) - 1ULL)
122
123 void    allocsp_f(int, long);
124 void    attr_remove_f(int, long);
125 void    attr_set_f(int, long);
126 void    bulkstat_f(int, long);
127 void    bulkstat1_f(int, long);
128 void    chown_f(int, long);
129 void    creat_f(int, long);
130 void    dread_f(int, long);
131 void    dwrite_f(int, long);
132 void    fdatasync_f(int, long);
133 void    freesp_f(int, long);
134 void    fsync_f(int, long);
135 void    getdents_f(int, long);
136 void    link_f(int, long);
137 void    mkdir_f(int, long);
138 void    mknod_f(int, long);
139 void    read_f(int, long);
140 void    readlink_f(int, long);
141 void    rename_f(int, long);
142 void    resvsp_f(int, long);
143 void    rmdir_f(int, long);
144 void    stat_f(int, long);
145 void    symlink_f(int, long);
146 void    sync_f(int, long);
147 void    truncate_f(int, long);
148 void    unlink_f(int, long);
149 void    unresvsp_f(int, long);
150 void    write_f(int, long);
151
152 opdesc_t        ops[] = {
153         { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
154         { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
155         { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
156         { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
157         { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
158         { OP_CHOWN, "chown", chown_f, 3, 1 },
159         { OP_CREAT, "creat", creat_f, 4, 1 },
160         { OP_DREAD, "dread", dread_f, 4, 0 },
161         { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
162         { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
163         { OP_FREESP, "freesp", freesp_f, 1, 1 },
164         { OP_FSYNC, "fsync", fsync_f, 1, 1 },
165         { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
166         { OP_LINK, "link", link_f, 1, 1 },
167         { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
168         { OP_MKNOD, "mknod", mknod_f, 2, 1 },
169         { OP_READ, "read", read_f, 1, 0 },
170         { OP_READLINK, "readlink", readlink_f, 1, 0 },
171         { OP_RENAME, "rename", rename_f, 2, 1 },
172         { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
173         { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
174         { OP_STAT, "stat", stat_f, 1, 0 },
175         { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
176         { OP_SYNC, "sync", sync_f, 1, 0 },
177         { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
178         { OP_UNLINK, "unlink", unlink_f, 1, 1 },
179         { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
180         { OP_WRITE, "write", write_f, 4, 1 },
181 }, *ops_end;
182
183 flist_t flist[FT_nft] = {
184         { 0, 0, 'd', NULL },
185         { 0, 0, 'f', NULL },
186         { 0, 0, 'l', NULL },
187         { 0, 0, 'c', NULL },
188         { 0, 0, 'r', NULL },
189 };
190
191 int             dcache[NDCACHE];
192 int             errrange;
193 int             errtag;
194 opty_t          *freq_table;
195 int             freq_table_size;
196 xfs_fsop_geom_t geom;
197 char            *homedir;
198 int             *ilist;
199 int             ilistlen;
200 off64_t         maxfsize;
201 char            *myprog;
202 int             namerand;
203 int             nameseq;
204 int             nops;
205 int             nproc = 1;
206 int             operations = 1;
207 int             procid;
208 int             rtpct;
209 unsigned long   seed = 0;
210 ino_t           top_ino;
211 int             verbose = 0;
212
213 void    add_to_flist(int, int, int);
214 void    append_pathname(pathname_t *, char *);
215 int     attr_list_path(pathname_t *, char *, const int, int);
216 int     attr_remove_path(pathname_t *, const char *, int);
217 int     attr_set_path(pathname_t *, const char *, const char *, const int, int);
218 void    check_cwd(void);
219 int     creat_path(pathname_t *, mode_t);
220 void    dcache_enter(int, int);
221 void    dcache_init(void);
222 fent_t  *dcache_lookup(int);
223 void    dcache_purge(int);
224 void    del_from_flist(int, int);
225 int     dirid_to_name(char *, int);
226 void    doproc(void);
227 void    fent_to_name(pathname_t *, flist_t *, fent_t *);
228 void    fix_parent(int, int);
229 void    free_pathname(pathname_t *);
230 int     generate_fname(fent_t *, int, pathname_t *, int *, int *);
231 int     get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
232 void    init_pathname(pathname_t *);
233 int     lchown_path(pathname_t *, uid_t, gid_t);
234 int     link_path(pathname_t *, pathname_t *);
235 int     lstat64_path(pathname_t *, struct stat64 *);
236 void    make_freq_table(void);
237 int     mkdir_path(pathname_t *, mode_t);
238 int     mknod_path(pathname_t *, mode_t, dev_t);
239 void    namerandpad(int, char *, int);
240 int     open_path(pathname_t *, int);
241 DIR     *opendir_path(pathname_t *);
242 void    process_freq(char *);
243 int     readlink_path(pathname_t *, char *, size_t);
244 int     rename_path(pathname_t *, pathname_t *);
245 int     rmdir_path(pathname_t *);
246 void    separate_pathname(pathname_t *, char *, pathname_t *);
247 void    show_ops(int, char *);
248 int     stat64_path(pathname_t *, struct stat64 *);
249 int     symlink_path(const char *, pathname_t *);
250 int     truncate64_path(pathname_t *, off64_t);
251 int     unlink_path(pathname_t *);
252 void    usage(void);
253 void    write_freq(void);
254 void    zero_freq(void);
255
256 int main(int argc, char **argv)
257 {
258         char            buf[10];
259         int             c;
260         char            *dirname = NULL;
261         int             fd;
262         int             i;
263         int             j;
264         char            *p;
265         int             stat;
266         struct timeval  t;
267         ptrdiff_t       srval;
268         int             nousage=0;
269         xfs_error_injection_t   err_inj;
270
271         errrange = errtag = 0;
272         umask(0);
273         nops = sizeof(ops) / sizeof(ops[0]);
274         ops_end = &ops[nops];
275         myprog = argv[0];
276         while ((c = getopt(argc, argv, "d:e:f:i:n:p:rs:vwzHS")) != -1) {
277                 switch (c) {
278                 case 'd':
279                         dirname = optarg;
280                         break;
281                 case 'e':
282                         sscanf(optarg, "%d", &errtag);
283                         if (errtag < 0) {
284                                 errtag = -errtag;
285                                 errrange = 1;
286                         } else if (errtag == 0)
287                                 errtag = -1;
288                         if (errtag >= XFS_ERRTAG_MAX) {
289                                 fprintf(stderr,
290                                         "error tag %d too large (max %d)\n",
291                                         errtag, XFS_ERRTAG_MAX - 1);
292                                 exit(1);
293                         }
294                         break;
295                 case 'f':
296                         process_freq(optarg);
297                         break;
298                 case 'i':
299                         ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
300                         ilist[ilistlen - 1] = strtol(optarg, &p, 16);
301                         break;
302                 case 'n':
303                         operations = atoi(optarg);
304                         break;
305                 case 'p':
306                         nproc = atoi(optarg);
307                         break;
308                 case 'r':
309                         namerand = 1;
310                         break;
311                 case 's':
312                         seed = strtoul(optarg, NULL, 0);
313                         break;
314                 case 'v':
315                         verbose = 1;
316                         break;
317                 case 'w':
318                         write_freq();
319                         break;
320                 case 'z':
321                         zero_freq();
322                         break;
323                 case 'S':
324                         show_ops(0, NULL);
325                         printf("\n");
326                         nousage=1;
327                         break;
328                 case '?':
329                         fprintf(stderr, "%s - invalid parameters\n",
330                                 myprog);
331                         /* fall through */
332                 case 'H':
333                         usage();
334                         exit(1);
335                 }
336         }
337         
338         if (!dirname) {
339             /* no directory specified */
340             if (!nousage) usage();
341             exit(1);
342         }
343         
344         (void)mkdir(dirname, 0777);
345         if (chdir(dirname) < 0) {
346                 perror(dirname);
347                 exit(1);
348         }
349         sprintf(buf, "fss%x", getpid());
350         fd = creat(buf, 0666);
351         if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
352                 maxfsize = (off64_t)MAXFSIZE32;
353         else
354                 maxfsize = (off64_t)MAXFSIZE;
355         make_freq_table();
356         dcache_init();
357         setlinebuf(stdout);
358         if (!seed) {
359                 gettimeofday(&t, (void *)NULL);
360                 seed = (int)t.tv_sec ^ (int)t.tv_usec;
361                 printf("seed = %ld\n", seed);
362         }
363         i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
364         if (i >= 0 && geom.rtblocks)
365                 rtpct = MIN(MAX(geom.rtblocks * 100 /
366                                 (geom.rtblocks + geom.datablocks), 1), 99);
367         else
368                 rtpct = 0;
369         if (errtag != 0) {
370                 if (errrange == 0) {
371                         if (errtag <= 0) {
372                                 srandom(seed);
373                                 j = random() % 100;
374
375                                 for (i = 0; i < j; i++)
376                                         (void) random();
377
378                                 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
379                         }
380                 } else {
381                         srandom(seed);
382                         j = random() % 100;
383
384                         for (i = 0; i < j; i++)
385                                 (void) random();
386
387                         errtag += (random() % (XFS_ERRTAG_MAX - errtag));
388                 }
389                 printf("Injecting failure on tag #%d\n", errtag);
390                 err_inj.errtag = errtag;
391                 err_inj.fd = fd;
392                 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
393                 if (srval < -1) {
394                         perror("fsstress - XFS_SYSSGI error injection call");
395                         close(fd);
396                         unlink(buf);
397                         exit(1);
398                 }
399         } else
400                 close(fd);
401         for (i = 0; i < nproc; i++) {
402                 if (fork() == 0) {
403                         procid = i;
404                         doproc();
405                         return 0;
406                 }
407         }
408         while (wait(&stat) > 0)
409                 continue;
410         if (errtag != 0) {
411                 err_inj.errtag = 0;
412                 err_inj.fd = fd;
413                 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
414                 if (srval != 0) {
415                         fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
416                                 buf, fd, errno);
417                         perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
418                         close(fd);
419                         exit(1);
420                 }
421                 close(fd);
422         }
423
424         unlink(buf);
425         return 0;
426 }
427
428 void
429 add_to_flist(int ft, int id, int parent)
430 {
431         fent_t  *fep;
432         flist_t *ftp;
433
434         ftp = &flist[ft];
435         if (ftp->nfiles == ftp->nslots) {
436                 ftp->nslots += FLIST_SLOT_INCR;
437                 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
438         }
439         fep = &ftp->fents[ftp->nfiles++];
440         fep->id = id;
441         fep->parent = parent;
442 }
443
444 void
445 append_pathname(pathname_t *name, char *str)
446 {
447         int     len;
448
449         len = strlen(str);
450 #ifdef DEBUG
451         if (len && *str == '/' && name->len == 0) {
452                 fprintf(stderr, "fsstress: append_pathname failure\n");
453                 chdir(homedir);
454                 abort();
455                 /* NOTREACHED */
456         }
457 #endif
458         name->path = realloc(name->path, name->len + 1 + len);
459         strcpy(&name->path[name->len], str);
460         name->len += len;
461 }
462
463 int
464 attr_list_path(pathname_t *name, char *buffer, const int buffersize, int flags)
465 {
466         char            buf[MAXNAMELEN];
467         pathname_t      newname;
468         int             rval;
469
470         if (flags != ATTR_DONTFOLLOW) {
471                 errno = EINVAL;
472                 return -1;
473         }
474         rval = llistxattr(name->path, buffer, buffersize);
475         if (rval >= 0 || errno != ENAMETOOLONG)
476                 return rval;
477         separate_pathname(name, buf, &newname);
478         if (chdir(buf) == 0) {
479                 rval = attr_list_path(&newname, buffer, buffersize, flags);
480                 chdir("..");
481         }
482         free_pathname(&newname);
483         return rval;
484 }
485
486 int
487 attr_remove_path(pathname_t *name, const char *attrname, int flags)
488 {
489         char            buf[MAXNAMELEN];
490         pathname_t      newname;
491         int             rval;
492
493         rval = attr_remove(name->path, attrname, flags);
494         if (rval >= 0 || errno != ENAMETOOLONG)
495                 return rval;
496         separate_pathname(name, buf, &newname);
497         if (chdir(buf) == 0) {
498                 rval = attr_remove_path(&newname, attrname, flags);
499                 chdir("..");
500         }
501         free_pathname(&newname);
502         return rval;
503 }
504
505 int
506 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
507               const int valuelength, int flags)
508 {
509         char            buf[MAXNAMELEN];
510         pathname_t      newname;
511         int             rval;
512
513         rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
514         if (rval >= 0 || errno != ENAMETOOLONG)
515                 return rval;
516         separate_pathname(name, buf, &newname);
517         if (chdir(buf) == 0) {
518                 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
519                         flags);
520                 chdir("..");
521         }
522         free_pathname(&newname);
523         return rval;
524 }
525
526 void
527 check_cwd(void)
528 {
529 #ifdef DEBUG
530         struct stat64   statbuf;
531
532         if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
533                 return;
534         chdir(homedir);
535         fprintf(stderr, "fsstress: check_cwd failure\n");
536         abort();
537         /* NOTREACHED */
538 #endif
539 }
540
541 int
542 creat_path(pathname_t *name, mode_t mode)
543 {
544         char            buf[MAXNAMELEN];
545         pathname_t      newname;
546         int             rval;
547
548         rval = creat(name->path, mode);
549         if (rval >= 0 || errno != ENAMETOOLONG)
550                 return rval;
551         separate_pathname(name, buf, &newname);
552         if (chdir(buf) == 0) {
553                 rval = creat_path(&newname, mode);
554                 chdir("..");
555         }
556         free_pathname(&newname);
557         return rval;
558 }
559
560 void
561 dcache_enter(int dirid, int slot)
562 {
563         dcache[dirid % NDCACHE] = slot;
564 }
565
566 void
567 dcache_init(void)
568 {
569         int     i;
570
571         for (i = 0; i < NDCACHE; i++)
572                 dcache[i] = -1;
573 }
574
575 fent_t *
576 dcache_lookup(int dirid)
577 {
578         fent_t  *fep;
579         int     i;
580
581         i = dcache[dirid % NDCACHE];
582         if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
583                 return fep;
584         return NULL;
585 }
586
587 void
588 dcache_purge(int dirid)
589 {
590         int     *dcp;
591
592         dcp = &dcache[dirid % NDCACHE];
593         if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
594                 *dcp = -1;
595 }
596
597 void
598 del_from_flist(int ft, int slot)
599 {
600         flist_t *ftp;
601
602         ftp = &flist[ft];
603         if (ft == FT_DIR)
604                 dcache_purge(ftp->fents[slot].id);
605         if (slot != ftp->nfiles - 1) {
606                 if (ft == FT_DIR)
607                         dcache_purge(ftp->fents[ftp->nfiles - 1].id);
608                 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
609         } else
610                 ftp->nfiles--;
611 }
612
613 fent_t *
614 dirid_to_fent(int dirid)
615 {
616         fent_t  *efep;
617         fent_t  *fep;
618         flist_t *flp;
619
620         if ((fep = dcache_lookup(dirid)))
621                 return fep;
622         flp = &flist[FT_DIR];
623         for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
624                 if (fep->id == dirid) {
625                         dcache_enter(dirid, fep - flp->fents);
626                         return fep;
627                 }
628         }
629         return NULL;
630 }
631
632 void
633 doproc(void)
634 {
635         struct stat64   statbuf;
636         char            buf[10];
637         int             opno;
638         int             rval;
639         opdesc_t        *p;
640
641         sprintf(buf, "p%x", procid);
642         (void)mkdir(buf, 0777);
643         if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
644                 perror(buf);
645                 _exit(1);
646         }
647         top_ino = statbuf.st_ino;
648         homedir = getcwd(NULL, -1);
649         seed += procid;
650         srandom(seed);
651         if (namerand)
652                 namerand = random();
653         for (opno = 0; opno < operations; opno++) {
654                 p = &ops[freq_table[random() % freq_table_size]];
655                 p->func(opno, random());
656                 /*
657                  * test for forced shutdown by stat'ing the test
658                  * directory.  If this stat returns EIO, assume
659                  * the forced shutdown happened.
660                  */
661                 if (errtag != 0 && opno % 100 == 0)  {
662                         rval = stat64(".", &statbuf);
663                         if (rval == EIO)  {
664                                 fprintf(stderr, "Detected EIO\n");
665                                 return;
666                         }
667                 }
668         }
669 }
670
671 void
672 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
673 {
674         char    buf[MAXNAMELEN];
675         int     i;
676         fent_t  *pfep;
677
678         if (fep == NULL)
679                 return;
680         if (fep->parent != -1) {
681                 pfep = dirid_to_fent(fep->parent);
682                 fent_to_name(name, &flist[FT_DIR], pfep);
683                 append_pathname(name, "/");
684         }
685         i = sprintf(buf, "%c%x", flp->tag, fep->id);
686         namerandpad(fep->id, buf, i);
687         append_pathname(name, buf);
688 }
689
690 void
691 fix_parent(int oldid, int newid)
692 {
693         fent_t  *fep;
694         flist_t *flp;
695         int     i;
696         int     j;
697
698         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
699                 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
700                         if (fep->parent == oldid)
701                                 fep->parent = newid;
702                 }
703         }
704 }
705
706 void
707 free_pathname(pathname_t *name)
708 {
709         if (name->path) {
710                 free(name->path);
711                 name->path = NULL;
712                 name->len = 0;
713         }
714 }
715
716 int
717 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
718 {
719         char    buf[MAXNAMELEN];
720         flist_t *flp;
721         int     id;
722         int     j;
723         int     len;
724
725         flp = &flist[ft];
726         len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
727         namerandpad(id, buf, len);
728         if (fep) {
729                 fent_to_name(name, &flist[FT_DIR], fep);
730                 append_pathname(name, "/");
731         }
732         append_pathname(name, buf);
733         *idp = id;
734         *v = verbose;
735         for (j = 0; !*v && j < ilistlen; j++) {
736                 if (ilist[j] == id) {
737                         *v = 1;
738                         break;
739                 }
740         }
741         return 1;
742 }
743
744 int
745 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
746           int *v)
747 {
748         int     c;
749         fent_t  *fep;
750         flist_t *flp;
751         int     i;
752         int     j;
753         int     x;
754
755         for (i = 0, c = 0, flp = flist; i < FT_nft; i++, flp++) {
756                 if (which & (1 << i))
757                         c += flp->nfiles;
758         }
759         if (c == 0) {
760                 if (flpp)
761                         *flpp = NULL;
762                 if (fepp)
763                         *fepp = NULL;
764                 *v = verbose;
765                 return 0;
766         }
767         x = (int)(r % c);
768         for (i = 0, c = 0, flp = flist; i < FT_nft; i++, flp++) {
769                 if (which & (1 << i)) {
770                         if (x < c + flp->nfiles) {
771                                 fep = &flp->fents[x - c];
772                                 if (name)
773                                         fent_to_name(name, flp, fep);
774                                 if (flpp)
775                                         *flpp = flp;
776                                 if (fepp)
777                                         *fepp = fep;
778                                 *v = verbose;
779                                 for (j = 0; !*v && j < ilistlen; j++) {
780                                         if (ilist[j] == fep->id) {
781                                                 *v = 1;
782                                                 break;
783                                         }
784                                 }
785                                 return 1;
786                         }
787                         c += flp->nfiles;
788                 }
789         }
790 #ifdef DEBUG
791         fprintf(stderr, "fsstress: get_fname failure\n");
792         abort();
793 #endif
794         return -1;
795         /* NOTREACHED */
796 }
797
798 void
799 init_pathname(pathname_t *name)
800 {
801         name->len = 0;
802         name->path = NULL;
803 }
804
805 int
806 lchown_path(pathname_t *name, uid_t owner, gid_t group)
807 {
808         char            buf[MAXNAMELEN];
809         pathname_t      newname;
810         int             rval;
811
812         rval = lchown(name->path, owner, group);
813         if (rval >= 0 || errno != ENAMETOOLONG)
814                 return rval;
815         separate_pathname(name, buf, &newname);
816         if (chdir(buf) == 0) {
817                 rval = lchown_path(&newname, owner, group);
818                 chdir("..");
819         }
820         free_pathname(&newname);
821         return rval;
822 }
823
824 int
825 link_path(pathname_t *name1, pathname_t *name2)
826 {
827         char            buf1[MAXNAMELEN];
828         char            buf2[MAXNAMELEN];
829         int             down1;
830         pathname_t      newname1;
831         pathname_t      newname2;
832         int             rval;
833
834         rval = link(name1->path, name2->path);
835         if (rval >= 0 || errno != ENAMETOOLONG)
836                 return rval;
837         separate_pathname(name1, buf1, &newname1);
838         separate_pathname(name2, buf2, &newname2);
839         if (strcmp(buf1, buf2) == 0) {
840                 if (chdir(buf1) == 0) {
841                         rval = link_path(&newname1, &newname2);
842                         chdir("..");
843                 }
844         } else {
845                 if (strcmp(buf1, "..") == 0)
846                         down1 = 0;
847                 else if (strcmp(buf2, "..") == 0)
848                         down1 = 1;
849                 else if (strlen(buf1) == 0)
850                         down1 = 0;
851                 else if (strlen(buf2) == 0)
852                         down1 = 1;
853                 else
854                         down1 = MAX(newname1.len, 3 + name2->len) <=
855                                 MAX(3 + name1->len, newname2.len);
856                 if (down1) {
857                         free_pathname(&newname2);
858                         append_pathname(&newname2, "../");
859                         append_pathname(&newname2, name2->path);
860                         if (chdir(buf1) == 0) {
861                                 rval = link_path(&newname1, &newname2);
862                                 chdir("..");
863                         }
864                 } else {
865                         free_pathname(&newname1);
866                         append_pathname(&newname1, "../");
867                         append_pathname(&newname1, name1->path);
868                         if (chdir(buf2) == 0) {
869                                 rval = link_path(&newname1, &newname2);
870                                 chdir("..");
871                         }
872                 }
873         }
874         free_pathname(&newname1);
875         free_pathname(&newname2);
876         return rval;
877 }
878
879 int
880 lstat64_path(pathname_t *name, struct stat64 *sbuf)
881 {
882         char            buf[MAXNAMELEN];
883         pathname_t      newname;
884         int             rval;
885
886         rval = lstat64(name->path, sbuf);
887         if (rval >= 0 || errno != ENAMETOOLONG)
888                 return rval;
889         separate_pathname(name, buf, &newname);
890         if (chdir(buf) == 0) {
891                 rval = lstat64_path(&newname, sbuf);
892                 chdir("..");
893         }
894         free_pathname(&newname);
895         return rval;
896 }
897
898 void
899 make_freq_table(void)
900 {
901         int             f;
902         int             i;
903         opdesc_t        *p;
904
905         for (p = ops, f = 0; p < ops_end; p++)
906                 f += p->freq;
907         freq_table = malloc(f * sizeof(*freq_table));
908         freq_table_size = f;
909         for (p = ops, i = 0; p < ops_end; p++) {
910                 for (f = 0; f < p->freq; f++, i++)
911                         freq_table[i] = p->op;
912         }
913 }
914
915 int
916 mkdir_path(pathname_t *name, mode_t mode)
917 {
918         char            buf[MAXNAMELEN];
919         pathname_t      newname;
920         int             rval;
921
922         rval = mkdir(name->path, mode);
923         if (rval >= 0 || errno != ENAMETOOLONG)
924                 return rval;
925         separate_pathname(name, buf, &newname);
926         if (chdir(buf) == 0) {
927                 rval = mkdir_path(&newname, mode);
928                 chdir("..");
929         }
930         free_pathname(&newname);
931         return rval;
932 }
933
934 int
935 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
936 {
937         char            buf[MAXNAMELEN];
938         pathname_t      newname;
939         int             rval;
940
941         rval = mknod(name->path, mode, dev);
942         if (rval >= 0 || errno != ENAMETOOLONG)
943                 return rval;
944         separate_pathname(name, buf, &newname);
945         if (chdir(buf) == 0) {
946                 rval = mknod_path(&newname, mode, dev);
947                 chdir("..");
948         }
949         free_pathname(&newname);
950         return rval;
951 }
952
953 void
954 namerandpad(int id, char *buf, int i)
955 {
956         int             bucket;
957         static int      buckets[] =
958                                 { 2, 4, 8, 16, 32, 64, 128, MAXNAMELEN - 1 };
959         int             padlen;
960         int             padmod;
961
962         if (namerand == 0)
963                 return;
964         bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
965         padmod = buckets[bucket] + 1 - i;
966         if (padmod <= 0)
967                 return;
968         padlen = (id ^ namerand) % padmod;
969         if (padlen) {
970                 memset(&buf[i], 'X', padlen);
971                 buf[i + padlen] = '\0';
972         }
973 }
974
975 int
976 open_path(pathname_t *name, int oflag)
977 {
978         char            buf[MAXNAMELEN];
979         pathname_t      newname;
980         int             rval;
981
982         rval = open(name->path, oflag);
983         if (rval >= 0 || errno != ENAMETOOLONG)
984                 return rval;
985         separate_pathname(name, buf, &newname);
986         if (chdir(buf) == 0) {
987                 rval = open_path(&newname, oflag);
988                 chdir("..");
989         }
990         free_pathname(&newname);
991         return rval;
992 }
993
994 DIR *
995 opendir_path(pathname_t *name)
996 {
997         char            buf[MAXNAMELEN];
998         pathname_t      newname;
999         DIR             *rval;
1000
1001         rval = opendir(name->path);
1002         if (rval || errno != ENAMETOOLONG)
1003                 return rval;
1004         separate_pathname(name, buf, &newname);
1005         if (chdir(buf) == 0) {
1006                 rval = opendir_path(&newname);
1007                 chdir("..");
1008         }
1009         free_pathname(&newname);
1010         return rval;
1011 }
1012
1013 void
1014 process_freq(char *arg)
1015 {
1016         opdesc_t        *p;
1017         char            *s;
1018
1019         s = strchr(arg, '=');
1020         if (s == NULL) {
1021                 fprintf(stderr, "bad argument '%s'\n", arg);
1022                 exit(1);
1023         }
1024         *s++ = '\0';
1025         for (p = ops; p < ops_end; p++) {
1026                 if (strcmp(arg, p->name) == 0) {
1027                         p->freq = atoi(s);
1028                         return;
1029                 }
1030         }
1031         fprintf(stderr, "can't find op type %s for -f\n", arg);
1032         exit(1);
1033 }
1034
1035 int
1036 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1037 {
1038         char            buf[MAXNAMELEN];
1039         pathname_t      newname;
1040         int             rval;
1041
1042         rval = readlink(name->path, lbuf, lbufsiz);
1043         if (rval >= 0 || errno != ENAMETOOLONG)
1044                 return rval;
1045         separate_pathname(name, buf, &newname);
1046         if (chdir(buf) == 0) {
1047                 rval = readlink_path(&newname, lbuf, lbufsiz);
1048                 chdir("..");
1049         }
1050         free_pathname(&newname);
1051         return rval;
1052 }
1053
1054 int
1055 rename_path(pathname_t *name1, pathname_t *name2)
1056 {
1057         char            buf1[MAXNAMELEN];
1058         char            buf2[MAXNAMELEN];
1059         int             down1;
1060         pathname_t      newname1;
1061         pathname_t      newname2;
1062         int             rval;
1063
1064         rval = rename(name1->path, name2->path);
1065         if (rval >= 0 || errno != ENAMETOOLONG)
1066                 return rval;
1067         separate_pathname(name1, buf1, &newname1);
1068         separate_pathname(name2, buf2, &newname2);
1069         if (strcmp(buf1, buf2) == 0) {
1070                 if (chdir(buf1) == 0) {
1071                         rval = rename_path(&newname1, &newname2);
1072                         chdir("..");
1073                 }
1074         } else {
1075                 if (strcmp(buf1, "..") == 0)
1076                         down1 = 0;
1077                 else if (strcmp(buf2, "..") == 0)
1078                         down1 = 1;
1079                 else if (strlen(buf1) == 0)
1080                         down1 = 0;
1081                 else if (strlen(buf2) == 0)
1082                         down1 = 1;
1083                 else
1084                         down1 = MAX(newname1.len, 3 + name2->len) <=
1085                                 MAX(3 + name1->len, newname2.len);
1086                 if (down1) {
1087                         free_pathname(&newname2);
1088                         append_pathname(&newname2, "../");
1089                         append_pathname(&newname2, name2->path);
1090                         if (chdir(buf1) == 0) {
1091                                 rval = rename_path(&newname1, &newname2);
1092                                 chdir("..");
1093                         }
1094                 } else {
1095                         free_pathname(&newname1);
1096                         append_pathname(&newname1, "../");
1097                         append_pathname(&newname1, name1->path);
1098                         if (chdir(buf2) == 0) {
1099                                 rval = rename_path(&newname1, &newname2);
1100                                 chdir("..");
1101                         }
1102                 }
1103         }
1104         free_pathname(&newname1);
1105         free_pathname(&newname2);
1106         return rval;
1107 }
1108
1109 int
1110 rmdir_path(pathname_t *name)
1111 {
1112         char            buf[MAXNAMELEN];
1113         pathname_t      newname;
1114         int             rval;
1115
1116         rval = rmdir(name->path);
1117         if (rval >= 0 || errno != ENAMETOOLONG)
1118                 return rval;
1119         separate_pathname(name, buf, &newname);
1120         if (chdir(buf) == 0) {
1121                 rval = rmdir_path(&newname);
1122                 chdir("..");
1123         }
1124         free_pathname(&newname);
1125         return rval;
1126 }
1127
1128 void
1129 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1130 {
1131         char    *slash;
1132
1133         init_pathname(newname);
1134         slash = strchr(name->path, '/');
1135         if (slash == NULL) {
1136                 buf[0] = '\0';
1137                 return;
1138         }
1139         *slash = '\0';
1140         strcpy(buf, name->path);
1141         *slash = '/';
1142         append_pathname(newname, slash + 1);
1143 }
1144
1145 #define WIDTH 80
1146
1147 void
1148 show_ops(int flag, char *lead_str)
1149 {
1150         opdesc_t        *p;
1151
1152         if (flag<0) {
1153                 /* print in list form */
1154                 int             x = WIDTH;
1155                 
1156                 for (p = ops; p < ops_end; p++) {
1157                         if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1158                                 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1159                         x+=printf("%s ", p->name);
1160                 }
1161                 printf("\n");
1162         } else {
1163                 int             f;
1164                 for (f = 0, p = ops; p < ops_end; p++)
1165                         f += p->freq;
1166
1167                 if (f == 0)
1168                         flag = 1;
1169
1170                 for (p = ops; p < ops_end; p++) {
1171                         if (flag != 0 || p->freq > 0) {
1172                                 if (lead_str != NULL)
1173                                         printf("%s", lead_str);
1174                                 printf("%20s %d/%d %s\n",
1175                                 p->name, p->freq, f,
1176                                 (p->iswrite == 0) ? " " : "write op");
1177                         }
1178                 }
1179         }
1180 }
1181
1182 int
1183 stat64_path(pathname_t *name, struct stat64 *sbuf)
1184 {
1185         char            buf[MAXNAMELEN];
1186         pathname_t      newname;
1187         int             rval;
1188
1189         rval = stat64(name->path, sbuf);
1190         if (rval >= 0 || errno != ENAMETOOLONG)
1191                 return rval;
1192         separate_pathname(name, buf, &newname);
1193         if (chdir(buf) == 0) {
1194                 rval = stat64_path(&newname, sbuf);
1195                 chdir("..");
1196         }
1197         free_pathname(&newname);
1198         return rval;
1199 }
1200
1201 int
1202 symlink_path(const char *name1, pathname_t *name)
1203 {
1204         char            buf[MAXNAMELEN];
1205         pathname_t      newname;
1206         int             rval;
1207         
1208         if (!strcmp(name1, name->path)) {
1209             printf("yikes! %s %s\n", name1, name->path);
1210             return 0;
1211         }
1212
1213         rval = symlink(name1, name->path);
1214         if (rval >= 0 || errno != ENAMETOOLONG)
1215                 return rval;
1216         separate_pathname(name, buf, &newname);
1217         if (chdir(buf) == 0) {
1218                 rval = symlink_path(name1, &newname);
1219                 chdir("..");
1220         }
1221         free_pathname(&newname);
1222         return rval;
1223 }
1224
1225 int
1226 truncate64_path(pathname_t *name, off64_t length)
1227 {
1228         char            buf[MAXNAMELEN];
1229         pathname_t      newname;
1230         int             rval;
1231
1232         rval = truncate64(name->path, length);
1233         if (rval >= 0 || errno != ENAMETOOLONG)
1234                 return rval;
1235         separate_pathname(name, buf, &newname);
1236         if (chdir(buf) == 0) {
1237                 rval = truncate64_path(&newname, length);
1238                 chdir("..");
1239         }
1240         free_pathname(&newname);
1241         return rval;
1242 }
1243
1244 int
1245 unlink_path(pathname_t *name)
1246 {
1247         char            buf[MAXNAMELEN];
1248         pathname_t      newname;
1249         int             rval;
1250
1251         rval = unlink(name->path);
1252         if (rval >= 0 || errno != ENAMETOOLONG)
1253                 return rval;
1254         separate_pathname(name, buf, &newname);
1255         if (chdir(buf) == 0) {
1256                 rval = unlink_path(&newname);
1257                 chdir("..");
1258         }
1259         free_pathname(&newname);
1260         return rval;
1261 }
1262
1263 void
1264 usage(void)
1265 {
1266         printf("Usage: %s -H   or\n", myprog);
1267         printf("       %s [-d dir][-e errtg][-f op_name=freq][-n nops]\n",
1268                 myprog);
1269         printf("          [-p nproc][-r len][-s seed][-v][-w][-z][-S]\n");
1270         printf("where\n");
1271         printf("   -d dir           specifies the base directory for operations\n");
1272         printf("   -e errtg         specifies error injection stuff\n");
1273         printf("   -f op_name=freq  changes the frequency of option name to freq\n");
1274         printf("                    the valid operation names are:\n");
1275         show_ops(-1, "                        ");
1276         printf("   -n nops          specifies the no. of operations per process (default 1)\n");
1277         printf("   -p nproc         specifies the no. of processes (default 1)\n");
1278         printf("   -r               specifies random name padding\n");
1279         printf("   -s seed          specifies the seed for the random generator (default random)\n");
1280         printf("   -v               specifies verbose mode\n");
1281         printf("   -w               zeros frequencies of non-write operations\n");
1282         printf("   -z               zeros frequencies of all operations\n");
1283         printf("   -S               prints the table of operations (omitting zero frequency)\n");
1284         printf("   -H               prints usage and exits\n");
1285 }
1286
1287 void
1288 write_freq(void)
1289 {
1290         opdesc_t        *p;
1291
1292         for (p = ops; p < ops_end; p++) {
1293                 if (!p->iswrite)
1294                         p->freq = 0;
1295         }
1296 }
1297
1298 void
1299 zero_freq(void)
1300 {
1301         opdesc_t        *p;
1302
1303         for (p = ops; p < ops_end; p++)
1304                 p->freq = 0;
1305 }
1306
1307 void
1308 allocsp_f(int opno, long r)
1309 {
1310         int             e;
1311         pathname_t      f;
1312         int             fd;
1313         struct flock64  fl;
1314         __int64_t       lr;
1315         off64_t         off;
1316         struct stat64   stb;
1317         int             v;
1318
1319         init_pathname(&f);
1320         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1321                 if (v)
1322                         printf("%d/%d: allocsp - no filename\n", procid, opno);
1323                 free_pathname(&f);
1324                 return;
1325         }
1326         fd = open_path(&f, O_RDWR);
1327         e = fd < 0 ? errno : 0;
1328         check_cwd();
1329         if (fd < 0) {
1330                 if (v)
1331                         printf("%d/%d: allocsp - open %s failed %d\n",
1332                                 procid, opno, f.path, e);
1333                 free_pathname(&f);
1334                 return;
1335         }
1336         if (fstat64(fd, &stb) < 0) {
1337                 if (v)
1338                         printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1339                                 procid, opno, f.path, errno);
1340                 free_pathname(&f);
1341                 close(fd);
1342                 return;
1343         }
1344         lr = ((__int64_t)random() << 32) + random();
1345         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1346         off %= maxfsize;
1347         fl.l_whence = SEEK_SET;
1348         fl.l_start = off;
1349         fl.l_len = 0;
1350         e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1351         if (v)
1352                 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s %lld 0 %d\n",
1353                         procid, opno, f.path, (long long)off, e);
1354         free_pathname(&f);
1355         close(fd);
1356 }
1357
1358 void
1359 attr_remove_f(int opno, long r)
1360 {
1361         char                    *aname, *l;
1362         char                    buf[4096];
1363         int                     e;
1364         int                     ent;
1365         pathname_t              f;
1366         int                     total;
1367         int                     v;
1368         int                     which;
1369
1370         init_pathname(&f);
1371         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1372                 append_pathname(&f, ".");
1373         total = 0;
1374         e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW);
1375         check_cwd();
1376         if (e > 0) {
1377                 for (l = buf; l - buf <= e; l += strlen(l)+1)
1378                         if (strncmp(l, "user.",5) == 0)
1379                                 total++;
1380         }
1381         if (total == 0) {
1382                 if (v)
1383                         printf("%d/%d: attr_remove - no attrs for %s\n",
1384                                 procid, opno, f.path);
1385                 free_pathname(&f);
1386                 return;
1387         }
1388         which = (int)(random() % total);
1389         ent = 0;
1390         aname = NULL;
1391         e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW);
1392         check_cwd();
1393         if (e <= 0)
1394                 return;
1395         for (l = buf; l - buf <= e; l += strlen(l)+1) {
1396                 if (strncmp(l, "user.",5) == 0) {
1397                         if (++ent == which) {
1398                                 aname = l;
1399                                 break;
1400                         }
1401                 }
1402         }
1403         if (aname == NULL) {
1404                 if (v)
1405                         printf(
1406                         "%d/%d: attr_remove - name %d not found at %s\n",       
1407                                 procid, opno, which, f.path);
1408                 free_pathname(&f);
1409                 return;
1410         }
1411         e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1412         check_cwd();
1413         if (v)
1414                 printf("%d/%d: attr_remove %s %s %d\n",
1415                         procid, opno, f.path, aname, e);
1416         free_pathname(&f);
1417 }
1418
1419 void
1420 attr_set_f(int opno, long r)
1421 {
1422         char            aname[10];
1423         char            *aval;
1424         int             e;
1425         pathname_t      f;
1426         int             len;
1427         static int      lengths[] = { 10, 100, 1000, 10000 };
1428         int             li;
1429         int             v;
1430
1431         init_pathname(&f);
1432         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1433                 append_pathname(&f, ".");
1434         sprintf(aname, "a%x", nameseq++);
1435         li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1436         len = (int)(random() % lengths[li]);
1437         if (len == 0)
1438                 len = 1;
1439         aval = malloc(len);
1440         memset(aval, nameseq & 0xff, len);
1441         e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1442                 errno : 0;
1443         check_cwd();
1444         free(aval);
1445         if (v)
1446                 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1447                         aname, e);
1448         free_pathname(&f);
1449 }
1450
1451 void
1452 bulkstat_f(int opno, long r)
1453 {
1454         int             count;
1455         int             fd;
1456         __uint64_t      last;
1457         int             nent;
1458         xfs_bstat_t     *t;
1459         __int64_t       total;
1460         xfs_fsop_bulkreq_t bsr;
1461
1462         last = 0;
1463         nent = (r % 999) + 2;
1464         t = malloc(nent * sizeof(*t));
1465         fd = open(".", O_RDONLY);
1466         total = 0;
1467     
1468         bsr.lastip=&last;
1469         bsr.icount=nent;
1470         bsr.ubuffer=t;
1471         bsr.ocount=&count;
1472             
1473         while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1474                 total += count;
1475         free(t);
1476         if (verbose)
1477                 printf("%d/%d: bulkstat nent %d total %llu\n",
1478                         procid, opno, nent, (long long)total);
1479         close(fd);
1480 }
1481
1482 void
1483 bulkstat1_f(int opno, long r)
1484 {
1485         int             e;
1486         pathname_t      f;
1487         int             fd;
1488         int             good;
1489         __uint64_t      ino;
1490         struct stat64   s;
1491         xfs_bstat_t     t;
1492         int             v;
1493         xfs_fsop_bulkreq_t bsr;
1494         
1495
1496         good = random() & 1;
1497         if (good) {
1498                /* use an inode we know exists */
1499                 init_pathname(&f);
1500                 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1501                         append_pathname(&f, ".");
1502                 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1503                 check_cwd();
1504                 free_pathname(&f);
1505         } else {
1506                 /* 
1507                  * pick a random inode 
1508                  *
1509                  * note this can generate kernel warning messages
1510                  * since bulkstat_one will read the disk block that
1511                  * would contain a given inode even if that disk
1512                  * block doesn't contain inodes.
1513                  *
1514                  * this is detected later, but not until after the
1515                  * warning is displayed.
1516                  *
1517                  * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1518                  *
1519                  */
1520                 ino = (ino64_t)r;
1521                 v = verbose;
1522         }
1523         fd = open(".", O_RDONLY);
1524         
1525         bsr.lastip=&ino;
1526         bsr.icount=1;
1527         bsr.ubuffer=&t;
1528         bsr.ocount=NULL;
1529         
1530         e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1531         if (v)
1532                 printf("%d/%d: bulkstat1 %s ino %lld %d\n", 
1533                     procid, opno, good?"real":"random", (long long)ino, e);
1534         close(fd);
1535 }
1536
1537 void
1538 chown_f(int opno, long r)
1539 {
1540         int             e;
1541         pathname_t      f;
1542         int             nbits;
1543         uid_t           u;
1544         int             v;
1545
1546         init_pathname(&f);
1547         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1548                 append_pathname(&f, ".");
1549         u = (uid_t)random();
1550         nbits = (int)(random() % 32);
1551         u &= (1 << nbits) - 1;
1552         e = lchown_path(&f, u, -1) < 0 ? errno : 0;
1553         check_cwd();
1554         if (v)
1555                 printf("%d/%d: chown %s %d %d\n", procid, opno, f.path, u, e);
1556         free_pathname(&f);
1557 }
1558
1559 void
1560 creat_f(int opno, long r)
1561 {
1562         struct fsxattr  a;
1563         int             e;
1564         int             e1;
1565         int             extsize;
1566         pathname_t      f;
1567         int             fd;
1568         fent_t          *fep;
1569         int             id;
1570         int             parid;
1571         int             type;
1572         int             v;
1573         int             v1;
1574
1575         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1576                 parid = -1;
1577         else
1578                 parid = fep->id;
1579         init_pathname(&f);
1580         type = rtpct ? ((random() % 100) > rtpct ? FT_REG : FT_RTF) : FT_REG;
1581         if (type == FT_RTF)
1582                 extsize = (random() % 10) + 1;
1583         else
1584                 extsize = 0;
1585         e = generate_fname(fep, type, &f, &id, &v);
1586         v |= v1;
1587         if (!e) {
1588                 if (v) {
1589                         fent_to_name(&f, &flist[FT_DIR], fep);
1590                         printf("%d/%d: creat - no filename from %s\n",
1591                                 procid, opno, f.path);
1592                 }
1593                 free_pathname(&f);
1594                 return;
1595         }
1596         fd = creat_path(&f, 0666);
1597         e = fd < 0 ? errno : 0;
1598         e1 = 0;
1599         check_cwd();
1600         if (fd >= 0) {
1601                 if (extsize &&
1602                     xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
1603                         a.fsx_xflags |= XFS_XFLAG_REALTIME;
1604                         a.fsx_extsize =
1605                                 geom.rtextsize * geom.blocksize * extsize;
1606                         if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
1607                                 e1 = errno;
1608                 }
1609                 add_to_flist(type, id, parid);
1610                 close(fd);
1611         }
1612         if (v)
1613                 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
1614                         extsize ? a.fsx_extsize : 0, e, e1);
1615         free_pathname(&f);
1616 }
1617
1618 void
1619 dread_f(int opno, long r)
1620 {
1621         __int64_t       align;
1622         char            *buf;
1623         struct dioattr  diob;
1624         int             e;
1625         pathname_t      f;
1626         int             fd;
1627         size_t          len;
1628         __int64_t       lr;
1629         off64_t         off;
1630         struct stat64   stb;
1631         int             v;
1632
1633         init_pathname(&f);
1634         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1635                 if (v)
1636                         printf("%d/%d: dread - no filename\n", procid, opno);
1637                 free_pathname(&f);
1638                 return;
1639         }
1640         fd = open_path(&f, O_RDONLY|O_DIRECT);
1641         e = fd < 0 ? errno : 0;
1642         check_cwd();
1643         if (fd < 0) {
1644                 if (v)
1645                         printf("%d/%d: dread - open %s failed %d\n",
1646                                 procid, opno, f.path, e);
1647                 free_pathname(&f);
1648                 return;
1649         }
1650         if (fstat64(fd, &stb) < 0) {
1651                 if (v)
1652                         printf("%d/%d: dread - fstat64 %s failed %d\n",
1653                                 procid, opno, f.path, errno);
1654                 free_pathname(&f);
1655                 close(fd);
1656                 return;
1657         }
1658         if (stb.st_size == 0) {
1659                 if (v)
1660                         printf("%d/%d: dread - %s zero size\n", procid, opno,
1661                                 f.path);
1662                 free_pathname(&f);
1663                 close(fd);
1664                 return;
1665         }
1666         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1667                 if (v)
1668                         printf(
1669                         "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s failed %d\n",
1670                                 procid, opno, f.path, errno);
1671                 free_pathname(&f);
1672                 close(fd);
1673                 return;
1674         }
1675         align = (__int64_t)diob.d_miniosz;
1676         lr = ((__int64_t)random() << 32) + random();
1677         off = (off64_t)(lr % stb.st_size);
1678         off -= (off % align);
1679         lseek64(fd, off, SEEK_SET);
1680         len = (random() % (getpagesize() * 32)) + 1;
1681         len -= (len % align);
1682         if (len <= 0)
1683                 len = align;
1684         else if (len > diob.d_maxiosz) 
1685                 len = diob.d_maxiosz;
1686         buf = memalign(diob.d_mem, len);
1687         e = read(fd, buf, len) < 0 ? errno : 0;
1688         free(buf);
1689         if (v)
1690                 printf("%d/%d: dread %s [%lld,%d] %d\n",
1691                         procid, opno, f.path, (long long)off, (int)len, e);
1692         free_pathname(&f);
1693         close(fd);
1694 }
1695
1696 void
1697 dwrite_f(int opno, long r)
1698 {
1699         __int64_t       align;
1700         char            *buf;
1701         struct dioattr  diob;
1702         int             e;
1703         pathname_t      f;
1704         int             fd;
1705         size_t          len;
1706         __int64_t       lr;
1707         off64_t         off;
1708         struct stat64   stb;
1709         int             v;
1710
1711         init_pathname(&f);
1712         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1713                 if (v)
1714                         printf("%d/%d: dwrite - no filename\n", procid, opno);
1715                 free_pathname(&f);
1716                 return;
1717         }
1718         fd = open_path(&f, O_WRONLY|O_DIRECT);
1719         e = fd < 0 ? errno : 0;
1720         check_cwd();
1721         if (fd < 0) {
1722                 if (v)
1723                         printf("%d/%d: dwrite - open %s failed %d\n",
1724                                 procid, opno, f.path, e);
1725                 free_pathname(&f);
1726                 return;
1727         }
1728         if (fstat64(fd, &stb) < 0) {
1729                 if (v)
1730                         printf("%d/%d: dwrite - fstat64 %s failed %d\n",
1731                                 procid, opno, f.path, errno);
1732                 free_pathname(&f);
1733                 close(fd);
1734                 return;
1735         }
1736         if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1737                 if (v)
1738                         printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
1739                                 " %s failed %d\n",
1740                                 procid, opno, f.path, errno);
1741                 free_pathname(&f);
1742                 close(fd);
1743                 return;
1744         }
1745         align = (__int64_t)diob.d_miniosz;
1746         lr = ((__int64_t)random() << 32) + random();
1747         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1748         off -= (off % align);
1749         lseek64(fd, off, SEEK_SET);
1750         len = (random() % (getpagesize() * 32)) + 1;
1751         len -= (len % align);
1752         if (len <= 0)
1753                 len = align;
1754         else if (len > diob.d_maxiosz) 
1755                 len = diob.d_maxiosz;
1756         buf = memalign(diob.d_mem, len);
1757         off %= maxfsize;
1758         lseek64(fd, off, SEEK_SET);
1759         memset(buf, nameseq & 0xff, len);
1760         e = write(fd, buf, len) < 0 ? errno : 0;
1761         free(buf);
1762         if (v)
1763                 printf("%d/%d: dwrite %s [%lld,%d] %d\n",
1764                         procid, opno, f.path, (long long)off, (int)len, e);
1765         free_pathname(&f);
1766         close(fd);
1767 }
1768
1769 void
1770 fdatasync_f(int opno, long r)
1771 {
1772         int             e;
1773         pathname_t      f;
1774         int             fd;
1775         int             v;
1776
1777         init_pathname(&f);
1778         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1779                 if (v)
1780                         printf("%d/%d: fdatasync - no filename\n",
1781                                 procid, opno);
1782                 free_pathname(&f);
1783                 return;
1784         }
1785         fd = open_path(&f, O_WRONLY);
1786         e = fd < 0 ? errno : 0;
1787         check_cwd();
1788         if (fd < 0) {
1789                 if (v)
1790                         printf("%d/%d: fdatasync - open %s failed %d\n",
1791                                 procid, opno, f.path, e);
1792                 free_pathname(&f);
1793                 return;
1794         }
1795         e = fdatasync(fd) < 0 ? errno : 0;
1796         if (v)
1797                 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
1798         free_pathname(&f);
1799         close(fd);
1800 }
1801
1802 void
1803 freesp_f(int opno, long r)
1804 {
1805         int             e;
1806         pathname_t      f;
1807         int             fd;
1808         struct flock64  fl;
1809         __int64_t       lr;
1810         off64_t         off;
1811         struct stat64   stb;
1812         int             v;
1813
1814         init_pathname(&f);
1815         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1816                 if (v)
1817                         printf("%d/%d: freesp - no filename\n", procid, opno);
1818                 free_pathname(&f);
1819                 return;
1820         }
1821         fd = open_path(&f, O_RDWR);
1822         e = fd < 0 ? errno : 0;
1823         check_cwd();
1824         if (fd < 0) {
1825                 if (v)
1826                         printf("%d/%d: freesp - open %s failed %d\n",
1827                                 procid, opno, f.path, e);
1828                 free_pathname(&f);
1829                 return;
1830         }
1831         if (fstat64(fd, &stb) < 0) {
1832                 if (v)
1833                         printf("%d/%d: freesp - fstat64 %s failed %d\n",
1834                                 procid, opno, f.path, errno);
1835                 free_pathname(&f);
1836                 close(fd);
1837                 return;
1838         }
1839         lr = ((__int64_t)random() << 32) + random();
1840         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1841         off %= maxfsize;
1842         fl.l_whence = SEEK_SET;
1843         fl.l_start = off;
1844         fl.l_len = 0;
1845         e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
1846         if (v)
1847                 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s %lld 0 %d\n",
1848                         procid, opno, f.path, (long long)off, e);
1849         free_pathname(&f);
1850         close(fd);
1851 }
1852
1853 void
1854 fsync_f(int opno, long r)
1855 {
1856         int             e;
1857         pathname_t      f;
1858         int             fd;
1859         int             v;
1860
1861         init_pathname(&f);
1862         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1863                 if (v)
1864                         printf("%d/%d: fsync - no filename\n", procid, opno);
1865                 free_pathname(&f);
1866                 return;
1867         }
1868         fd = open_path(&f, O_WRONLY);
1869         e = fd < 0 ? errno : 0;
1870         check_cwd();
1871         if (fd < 0) {
1872                 if (v)
1873                         printf("%d/%d: fsync - open %s failed %d\n",
1874                                 procid, opno, f.path, e);
1875                 free_pathname(&f);
1876                 return;
1877         }
1878         e = fsync(fd) < 0 ? errno : 0;
1879         if (v)
1880                 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
1881         free_pathname(&f);
1882         close(fd);
1883 }
1884
1885 void
1886 getdents_f(int opno, long r)
1887 {
1888         DIR             *dir;
1889         pathname_t      f;
1890         int             v;
1891
1892         init_pathname(&f);
1893         if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
1894                 append_pathname(&f, ".");
1895         dir = opendir_path(&f);
1896         check_cwd();
1897         if (dir == NULL) {
1898                 if (v)
1899                         printf("%d/%d: getdents - can't open %s\n",
1900                                 procid, opno, f.path);
1901                 free_pathname(&f);
1902                 return;
1903         }
1904         while (readdir64(dir) != NULL)
1905                 continue;
1906         if (v)
1907                 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
1908         free_pathname(&f);
1909         closedir(dir);
1910 }
1911
1912 void
1913 link_f(int opno, long r)
1914 {
1915         int             e;
1916         pathname_t      f;
1917         fent_t          *fep;
1918         flist_t         *flp;
1919         int             id;
1920         pathname_t      l;
1921         int             parid;
1922         int             v;
1923         int             v1;
1924
1925         init_pathname(&f);
1926         if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
1927                 if (v1)
1928                         printf("%d/%d: link - no file\n", procid, opno);
1929                 free_pathname(&f);
1930                 return;
1931         }
1932         if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
1933                 parid = -1;
1934         else
1935                 parid = fep->id;
1936         v |= v1;
1937         init_pathname(&l);
1938         e = generate_fname(fep, flp - flist, &l, &id, &v1);
1939         v |= v1;
1940         if (!e) {
1941                 if (v) {
1942                         fent_to_name(&l, &flist[FT_DIR], fep);
1943                         printf("%d/%d: link - no filename from %s\n",
1944                                 procid, opno, l.path);
1945                 }
1946                 free_pathname(&l);
1947                 free_pathname(&f);
1948                 return;
1949         }
1950         e = link_path(&f, &l) < 0 ? errno : 0;
1951         check_cwd();
1952         if (e == 0)
1953                 add_to_flist(flp - flist, id, parid);
1954         if (v)
1955                 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
1956                         e);
1957         free_pathname(&l);
1958         free_pathname(&f);
1959 }
1960
1961 void
1962 mkdir_f(int opno, long r)
1963 {
1964         int             e;
1965         pathname_t      f;
1966         fent_t          *fep;
1967         int             id;
1968         int             parid;
1969         int             v;
1970         int             v1;
1971
1972         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
1973                 parid = -1;
1974         else
1975                 parid = fep->id;
1976         init_pathname(&f);
1977         e = generate_fname(fep, FT_DIR, &f, &id, &v1);
1978         v |= v1;
1979         if (!e) {
1980                 if (v) {
1981                         fent_to_name(&f, &flist[FT_DIR], fep);
1982                         printf("%d/%d: mkdir - no filename from %s\n",
1983                                 procid, opno, f.path);
1984                 }
1985                 free_pathname(&f);
1986                 return;
1987         }
1988         e = mkdir_path(&f, 0777) < 0 ? errno : 0;
1989         check_cwd();
1990         if (e == 0)
1991                 add_to_flist(FT_DIR, id, parid);
1992         if (v)
1993                 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
1994         free_pathname(&f);
1995 }
1996
1997 void
1998 mknod_f(int opno, long r)
1999 {
2000         int             e;
2001         pathname_t      f;
2002         fent_t          *fep;
2003         int             id;
2004         int             parid;
2005         int             v;
2006         int             v1;
2007
2008         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2009                 parid = -1;
2010         else
2011                 parid = fep->id;
2012         init_pathname(&f);
2013         e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2014         v |= v1;
2015         if (!e) {
2016                 if (v) {
2017                         fent_to_name(&f, &flist[FT_DIR], fep);
2018                         printf("%d/%d: mknod - no filename from %s\n",
2019                                 procid, opno, f.path);
2020                 }
2021                 free_pathname(&f);
2022                 return;
2023         }
2024         e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2025         check_cwd();
2026         if (e == 0)
2027                 add_to_flist(FT_DEV, id, parid);
2028         if (v)
2029                 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2030         free_pathname(&f);
2031 }
2032
2033 void
2034 read_f(int opno, long r)
2035 {
2036         char            *buf;
2037         int             e;
2038         pathname_t      f;
2039         int             fd;
2040         size_t          len;
2041         __int64_t       lr;
2042         off64_t         off;
2043         struct stat64   stb;
2044         int             v;
2045
2046         init_pathname(&f);
2047         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2048                 if (v)
2049                         printf("%d/%d: read - no filename\n", procid, opno);
2050                 free_pathname(&f);
2051                 return;
2052         }
2053         fd = open_path(&f, O_RDONLY);
2054         e = fd < 0 ? errno : 0;
2055         check_cwd();
2056         if (fd < 0) {
2057                 if (v)
2058                         printf("%d/%d: read - open %s failed %d\n",
2059                                 procid, opno, f.path, e);
2060                 free_pathname(&f);
2061                 return;
2062         }
2063         if (fstat64(fd, &stb) < 0) {
2064                 if (v)
2065                         printf("%d/%d: read - fstat64 %s failed %d\n",
2066                                 procid, opno, f.path, errno);
2067                 free_pathname(&f);
2068                 close(fd);
2069                 return;
2070         }
2071         if (stb.st_size == 0) {
2072                 if (v)
2073                         printf("%d/%d: read - %s zero size\n", procid, opno,
2074                                 f.path);
2075                 free_pathname(&f);
2076                 close(fd);
2077                 return;
2078         }
2079         lr = ((__int64_t)random() << 32) + random();
2080         off = (off64_t)(lr % stb.st_size);
2081         lseek64(fd, off, SEEK_SET);
2082         len = (random() % (getpagesize() * 32)) + 1;
2083         buf = malloc(len);
2084         e = read(fd, buf, len) < 0 ? errno : 0;
2085         free(buf);
2086         if (v)
2087                 printf("%d/%d: read %s [%lld,%d] %d\n",
2088                         procid, opno, f.path, (long long)off, (int)len, e);
2089         free_pathname(&f);
2090         close(fd);
2091 }
2092
2093 void
2094 readlink_f(int opno, long r)
2095 {
2096         char            buf[PATH_MAX];
2097         int             e;
2098         pathname_t      f;
2099         int             v;
2100
2101         init_pathname(&f);
2102         if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2103                 if (v)
2104                         printf("%d/%d: readlink - no filename\n", procid, opno);
2105                 free_pathname(&f);
2106                 return;
2107         }
2108         e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2109         check_cwd();
2110         if (v)
2111                 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2112         free_pathname(&f);
2113 }
2114
2115 void
2116 rename_f(int opno, long r)
2117 {
2118         fent_t          *dfep;
2119         int             e;
2120         pathname_t      f;
2121         fent_t          *fep;
2122         flist_t         *flp;
2123         int             id;
2124         pathname_t      newf;
2125         int             oldid;
2126         int             parid;
2127         int             v;
2128         int             v1;
2129
2130         init_pathname(&f);
2131         if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2132                 if (v1)
2133                         printf("%d/%d: rename - no filename\n", procid, opno);
2134                 free_pathname(&f);
2135                 return;
2136         }
2137         if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2138                 parid = -1;
2139         else
2140                 parid = dfep->id;
2141         v |= v1;
2142         init_pathname(&newf);
2143         e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2144         v |= v1;
2145         if (!e) {
2146                 if (v) {
2147                         fent_to_name(&f, &flist[FT_DIR], dfep);
2148                         printf("%d/%d: rename - no filename from %s\n",
2149                                 procid, opno, f.path);
2150                 }
2151                 free_pathname(&newf);
2152                 free_pathname(&f);
2153                 return;
2154         }
2155         e = rename_path(&f, &newf) < 0 ? errno : 0;
2156         check_cwd();
2157         if (e == 0) {
2158                 if (flp - flist == FT_DIR) {
2159                         oldid = fep->id;
2160                         fix_parent(oldid, id);
2161                 }
2162                 del_from_flist(flp - flist, fep - flp->fents);
2163                 add_to_flist(flp - flist, id, parid);
2164         }
2165         if (v)
2166                 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
2167                         newf.path, e);
2168         free_pathname(&newf);
2169         free_pathname(&f);
2170 }
2171
2172 void
2173 resvsp_f(int opno, long r)
2174 {
2175         int             e;
2176         pathname_t      f;
2177         int             fd;
2178         struct flock64  fl;
2179         __int64_t       lr;
2180         off64_t         off;
2181         struct stat64   stb;
2182         int             v;
2183
2184         init_pathname(&f);
2185         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2186                 if (v)
2187                         printf("%d/%d: resvsp - no filename\n", procid, opno);
2188                 free_pathname(&f);
2189                 return;
2190         }
2191         fd = open_path(&f, O_RDWR);
2192         e = fd < 0 ? errno : 0;
2193         check_cwd();
2194         if (fd < 0) {
2195                 if (v)
2196                         printf("%d/%d: resvsp - open %s failed %d\n",
2197                                 procid, opno, f.path, e);
2198                 free_pathname(&f);
2199                 return;
2200         }
2201         if (fstat64(fd, &stb) < 0) {
2202                 if (v)
2203                         printf("%d/%d: resvsp - fstat64 %s failed %d\n",
2204                                 procid, opno, f.path, errno);
2205                 free_pathname(&f);
2206                 close(fd);
2207                 return;
2208         }
2209         lr = ((__int64_t)random() << 32) + random();
2210         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2211         off %= maxfsize;
2212         fl.l_whence = SEEK_SET;
2213         fl.l_start = off;
2214         fl.l_len = (off64_t)(random() % (1024 * 1024));
2215         e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2216         if (v)
2217                 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s %lld %lld %d\n",
2218                         procid, opno, f.path,
2219                         (long long)off, (long long)fl.l_len, e);
2220         free_pathname(&f);
2221         close(fd);
2222 }
2223
2224 void
2225 rmdir_f(int opno, long r)
2226 {
2227         int             e;
2228         pathname_t      f;
2229         fent_t          *fep;
2230         int             v;
2231
2232         init_pathname(&f);
2233         if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2234                 if (v)
2235                         printf("%d/%d: rmdir - no directory\n", procid, opno);
2236                 free_pathname(&f);
2237                 return;
2238         }
2239         e = rmdir_path(&f) < 0 ? errno : 0;
2240         check_cwd();
2241         if (e == 0)
2242                 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2243         if (v)
2244                 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2245         free_pathname(&f);
2246 }
2247
2248 void
2249 stat_f(int opno, long r)
2250 {
2251         int             e;
2252         pathname_t      f;
2253         struct stat64   stb;
2254         int             v;
2255
2256         init_pathname(&f);
2257         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2258                 if (v)
2259                         printf("%d/%d: stat - no entries\n", procid, opno);
2260                 free_pathname(&f);
2261                 return;
2262         }
2263         e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2264         check_cwd();
2265         if (v)
2266                 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2267         free_pathname(&f);
2268 }
2269
2270 void
2271 symlink_f(int opno, long r)
2272 {
2273         int             e;
2274         pathname_t      f;
2275         fent_t          *fep;
2276         int             i;
2277         int             id;
2278         int             len;
2279         int             parid;
2280         int             v;
2281         int             v1;
2282         char            *val;
2283
2284         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2285                 parid = -1;
2286         else
2287                 parid = fep->id;
2288         init_pathname(&f);
2289         e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2290         v |= v1;
2291         if (!e) {
2292                 if (v) {
2293                         fent_to_name(&f, &flist[FT_DIR], fep);
2294                         printf("%d/%d: symlink - no filename from %s\n",
2295                                 procid, opno, f.path);
2296                 }
2297                 free_pathname(&f);
2298                 return;
2299         }
2300         len = (int)(random() % PATH_MAX);
2301         val = malloc(len + 1);
2302         if (len)
2303                 memset(val, 'x', len);
2304         val[len] = '\0';
2305         for (i = 10; i < len - 1; i += 10)
2306                 val[i] = '/';
2307         e = symlink_path(val, &f) < 0 ? errno : 0;
2308         check_cwd();
2309         if (e == 0)
2310                 add_to_flist(FT_SYM, id, parid);
2311         free(val);
2312         if (v)
2313                 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
2314         free_pathname(&f);
2315 }
2316
2317 /* ARGSUSED */
2318 void
2319 sync_f(int opno, long r)
2320 {
2321         sync();
2322         if (verbose)
2323                 printf("%d/%d: sync\n", procid, opno);
2324 }
2325
2326 void
2327 truncate_f(int opno, long r)
2328 {
2329         int             e;
2330         pathname_t      f;
2331         __int64_t       lr;
2332         off64_t         off;
2333         struct stat64   stb;
2334         int             v;
2335
2336         init_pathname(&f);
2337         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2338                 if (v)
2339                         printf("%d/%d: truncate - no filename\n", procid, opno);
2340                 free_pathname(&f);
2341                 return;
2342         }
2343         e = stat64_path(&f, &stb) < 0 ? errno : 0;
2344         check_cwd();
2345         if (e > 0) {
2346                 if (v)
2347                         printf("%d/%d: truncate - stat64 %s failed %d\n",
2348                                 procid, opno, f.path, e);
2349                 free_pathname(&f);
2350                 return;
2351         }
2352         lr = ((__int64_t)random() << 32) + random();
2353         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2354         off %= maxfsize;
2355         e = truncate64_path(&f, off) < 0 ? errno : 0;
2356         check_cwd();
2357         if (v)
2358                 printf("%d/%d: truncate %s %lld %d\n", procid, opno, f.path,
2359                         (long long)off, e);
2360         free_pathname(&f);
2361 }
2362
2363 void
2364 unlink_f(int opno, long r)
2365 {
2366         int             e;
2367         pathname_t      f;
2368         fent_t          *fep;
2369         flist_t         *flp;
2370         int             v;
2371
2372         init_pathname(&f);
2373         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
2374                 if (v)
2375                         printf("%d/%d: unlink - no file\n", procid, opno);
2376                 free_pathname(&f);
2377                 return;
2378         }
2379         e = unlink_path(&f) < 0 ? errno : 0;
2380         check_cwd();
2381         if (e == 0)
2382                 del_from_flist(flp - flist, fep - flp->fents);
2383         if (v)
2384                 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
2385         free_pathname(&f);
2386 }
2387
2388 void
2389 unresvsp_f(int opno, long r)
2390 {
2391         int             e;
2392         pathname_t      f;
2393         int             fd;
2394         struct flock64  fl;
2395         __int64_t       lr;
2396         off64_t         off;
2397         struct stat64   stb;
2398         int             v;
2399
2400         init_pathname(&f);
2401         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2402                 if (v)
2403                         printf("%d/%d: unresvsp - no filename\n", procid, opno);
2404                 free_pathname(&f);
2405                 return;
2406         }
2407         fd = open_path(&f, O_RDWR);
2408         e = fd < 0 ? errno : 0;
2409         check_cwd();
2410         if (fd < 0) {
2411                 if (v)
2412                         printf("%d/%d: unresvsp - open %s failed %d\n",
2413                                 procid, opno, f.path, e);
2414                 free_pathname(&f);
2415                 return;
2416         }
2417         if (fstat64(fd, &stb) < 0) {
2418                 if (v)
2419                         printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
2420                                 procid, opno, f.path, errno);
2421                 free_pathname(&f);
2422                 close(fd);
2423                 return;
2424         }
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 = (off64_t)(random() % (1 << 20));
2431         e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
2432         if (v)
2433                 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s %lld %lld %d\n",
2434                         procid, opno, f.path,
2435                         (long long)off, (long long)fl.l_len, e);
2436         free_pathname(&f);
2437         close(fd);
2438 }
2439
2440 void
2441 write_f(int opno, long r)
2442 {
2443         char            *buf;
2444         int             e;
2445         pathname_t      f;
2446         int             fd;
2447         size_t          len;
2448         __int64_t       lr;
2449         off64_t         off;
2450         struct stat64   stb;
2451         int             v;
2452
2453         init_pathname(&f);
2454         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
2455                 if (v)
2456                         printf("%d/%d: write - no filename\n", procid, opno);
2457                 free_pathname(&f);
2458                 return;
2459         }
2460         fd = open_path(&f, O_WRONLY);
2461         e = fd < 0 ? errno : 0;
2462         check_cwd();
2463         if (fd < 0) {
2464                 if (v)
2465                         printf("%d/%d: write - open %s failed %d\n",
2466                                 procid, opno, f.path, e);
2467                 free_pathname(&f);
2468                 return;
2469         }
2470         if (fstat64(fd, &stb) < 0) {
2471                 if (v)
2472                         printf("%d/%d: write - fstat64 %s failed %d\n",
2473                                 procid, opno, f.path, errno);
2474                 free_pathname(&f);
2475                 close(fd);
2476                 return;
2477         }
2478         lr = ((__int64_t)random() << 32) + random();
2479         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2480         off %= maxfsize;
2481         lseek64(fd, off, SEEK_SET);
2482         len = (random() % (getpagesize() * 32)) + 1;
2483         buf = malloc(len);
2484         memset(buf, nameseq & 0xff, len);
2485         e = write(fd, buf, len) < 0 ? errno : 0;
2486         free(buf);
2487         if (v)
2488                 printf("%d/%d: write %s [%lld,%d] %d\n",
2489                         procid, opno, f.path, (long long)off, (int)len, e);
2490         free_pathname(&f);
2491         close(fd);
2492 }