Turn back on direct I/O in the testing mix
[xfstests-dev.git] / src / fsstress.c
1 /*
2  * Copyright (c) 2000 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 "global.h"
34
35 #define XFS_ERRTAG_MAX          17
36
37 typedef enum {
38         OP_ALLOCSP,
39         OP_ATTR_REMOVE,
40         OP_ATTR_SET,
41         OP_BULKSTAT,
42         OP_BULKSTAT1,
43         OP_CHOWN,
44         OP_CREAT,
45         OP_DREAD,
46         OP_DWRITE,
47         OP_FDATASYNC,
48         OP_FREESP,
49         OP_FSYNC,
50         OP_GETDENTS,
51         OP_LINK,
52         OP_MKDIR,
53         OP_MKNOD,
54         OP_READ,
55         OP_READLINK,
56         OP_RENAME,
57         OP_RESVSP,
58         OP_RMDIR,
59         OP_STAT,
60         OP_SYMLINK,
61         OP_SYNC,
62         OP_TRUNCATE,
63         OP_UNLINK,
64         OP_UNRESVSP,
65         OP_WRITE,
66         OP_LAST
67 } opty_t;
68
69 typedef void (*opfnc_t)(int, long);
70
71 typedef struct opdesc {
72         opty_t  op;
73         char    *name;
74         opfnc_t func;
75         int     freq;
76         int     iswrite;
77 } opdesc_t;
78
79 typedef struct fent {
80         int     id;
81         int     parent;
82 } fent_t;
83
84 typedef struct flist {
85         int     nfiles;
86         int     nslots;
87         int     tag;
88         fent_t  *fents;
89 } flist_t;
90
91 typedef struct pathname {
92         int     len;
93         char    *path;
94 } pathname_t;
95
96 #define FT_DIR  0
97 #define FT_DIRm (1 << FT_DIR)
98 #define FT_REG  1
99 #define FT_REGm (1 << FT_REG)
100 #define FT_SYM  2
101 #define FT_SYMm (1 << FT_SYM)
102 #define FT_DEV  3
103 #define FT_DEVm (1 << FT_DEV)
104 #define FT_RTF  4
105 #define FT_RTFm (1 << FT_RTF)
106 #define FT_nft  5
107 #define FT_ANYm ((1 << FT_nft) - 1)
108 #define FT_REGFILE      (FT_REGm | FT_RTFm)
109 #define FT_NOTDIR       (FT_ANYm & ~FT_DIRm)
110
111 #define FLIST_SLOT_INCR 16
112 #define NDCACHE 64
113
114 #define MAXFSIZE        ((1ULL << 63) - 1ULL)
115 #define MAXFSIZE32      ((1ULL << 40) - 1ULL)
116
117 void    allocsp_f(int, long);
118 void    attr_remove_f(int, long);
119 void    attr_set_f(int, long);
120 void    bulkstat_f(int, long);
121 void    bulkstat1_f(int, long);
122 void    chown_f(int, long);
123 void    creat_f(int, long);
124 void    dread_f(int, long);
125 void    dwrite_f(int, long);
126 void    fdatasync_f(int, long);
127 void    freesp_f(int, long);
128 void    fsync_f(int, long);
129 void    getdents_f(int, long);
130 void    link_f(int, long);
131 void    mkdir_f(int, long);
132 void    mknod_f(int, long);
133 void    read_f(int, long);
134 void    readlink_f(int, long);
135 void    rename_f(int, long);
136 void    resvsp_f(int, long);
137 void    rmdir_f(int, long);
138 void    stat_f(int, long);
139 void    symlink_f(int, long);
140 void    sync_f(int, long);
141 void    truncate_f(int, long);
142 void    unlink_f(int, long);
143 void    unresvsp_f(int, long);
144 void    write_f(int, long);
145
146 opdesc_t        ops[] = {
147         { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
148         { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
149         { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
150         { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
151         { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
152         { OP_CHOWN, "chown", chown_f, 3, 1 },
153         { OP_CREAT, "creat", creat_f, 4, 1 },
154         { OP_DREAD, "dread", dread_f, 4, 0 },
155         { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
156         { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
157         { OP_FREESP, "freesp", freesp_f, 1, 1 },
158         { OP_FSYNC, "fsync", fsync_f, 1, 1 },
159         { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
160         { OP_LINK, "link", link_f, 1, 1 },
161         { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
162         { OP_MKNOD, "mknod", mknod_f, 2, 1 },
163         { OP_READ, "read", read_f, 1, 0 },
164         { OP_READLINK, "readlink", readlink_f, 1, 0 },
165         { OP_RENAME, "rename", rename_f, 2, 1 },
166         { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
167         { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
168         { OP_STAT, "stat", stat_f, 1, 0 },
169         { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
170         { OP_SYNC, "sync", sync_f, 1, 0 },
171         { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
172         { OP_UNLINK, "unlink", unlink_f, 1, 1 },
173         { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
174         { OP_WRITE, "write", write_f, 4, 1 },
175 }, *ops_end;
176
177 flist_t flist[FT_nft] = {
178         { 0, 0, 'd', NULL },
179         { 0, 0, 'f', NULL },
180         { 0, 0, 'l', NULL },
181         { 0, 0, 'c', NULL },
182         { 0, 0, 'r', NULL },
183 };
184
185 int             dcache[NDCACHE];
186 int             errrange;
187 int             errtag;
188 opty_t          *freq_table;
189 int             freq_table_size;
190 xfs_fsop_geom_t geom;
191 char            *homedir;
192 int             *ilist;
193 int             ilistlen;
194 off64_t         maxfsize;
195 char            *myprog;
196 int             namerand;
197 int             nameseq;
198 int             nops;
199 int             nproc = 1;
200 int             operations = 1;
201 int             procid;
202 int             rtpct;
203 unsigned long   seed = 0;
204 ino_t           top_ino;
205 int             verbose = 0;
206
207 void    add_to_flist(int, int, int);
208 void    append_pathname(pathname_t *, char *);
209 int     attr_list_path(pathname_t *, char *, const int, int,
210                        attrlist_cursor_t *);
211 int     attr_remove_path(pathname_t *, const char *, int);
212 int     attr_set_path(pathname_t *, const char *, const char *, const int, int);
213 void    check_cwd(void);
214 int     creat_path(pathname_t *, mode_t);
215 void    dcache_enter(int, int);
216 void    dcache_init(void);
217 fent_t  *dcache_lookup(int);
218 void    dcache_purge(int);
219 void    del_from_flist(int, int);
220 int     dirid_to_name(char *, int);
221 void    doproc(void);
222 void    fent_to_name(pathname_t *, flist_t *, fent_t *);
223 void    fix_parent(int, int);
224 void    free_pathname(pathname_t *);
225 int     generate_fname(fent_t *, int, pathname_t *, int *, int *);
226 int     get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
227 void    init_pathname(pathname_t *);
228 int     lchown_path(pathname_t *, uid_t, gid_t);
229 int     link_path(pathname_t *, pathname_t *);
230 int     lstat64_path(pathname_t *, struct stat64 *);
231 void    make_freq_table(void);
232 int     mkdir_path(pathname_t *, mode_t);
233 int     mknod_path(pathname_t *, mode_t, dev_t);
234 void    namerandpad(int, char *, int);
235 int     open_path(pathname_t *, int);
236 DIR     *opendir_path(pathname_t *);
237 void    process_freq(char *);
238 int     readlink_path(pathname_t *, char *, size_t);
239 int     rename_path(pathname_t *, pathname_t *);
240 int     rmdir_path(pathname_t *);
241 void    separate_pathname(pathname_t *, char *, pathname_t *);
242 void    show_ops(int, char *);
243 int     stat64_path(pathname_t *, struct stat64 *);
244 int     symlink_path(const char *, pathname_t *);
245 int     truncate64_path(pathname_t *, off64_t);
246 int     unlink_path(pathname_t *);
247 void    usage(void);
248 void    write_freq(void);
249 void    zero_freq(void);
250
251 int main(int argc, char **argv)
252 {
253         char            buf[10];
254         int             c;
255         char            *dirname = NULL;
256         int             fd;
257         int             i;
258         int             j;
259         char            *p;
260         int             stat;
261         struct timeval  t;
262         ptrdiff_t       srval;
263         int             nousage=0;
264         xfs_error_injection_t   err_inj;
265
266         errrange = errtag = 0;
267         umask(0);
268         nops = sizeof(ops) / sizeof(ops[0]);
269         ops_end = &ops[nops];
270         myprog = argv[0];
271         while ((c = getopt(argc, argv, "d:e:f:i:n:p:rs:vwzHS")) != -1) {
272                 switch (c) {
273                 case 'd':
274                         dirname = optarg;
275                         break;
276                 case 'e':
277                         sscanf(optarg, "%d", &errtag);
278                         if (errtag < 0) {
279                                 errtag = -errtag;
280                                 errrange = 1;
281                         } else if (errtag == 0)
282                                 errtag = -1;
283                         if (errtag >= XFS_ERRTAG_MAX) {
284                                 fprintf(stderr,
285                                         "error tag %d too large (max %d)\n",
286                                         errtag, XFS_ERRTAG_MAX - 1);
287                                 exit(1);
288                         }
289                         break;
290                 case 'f':
291                         process_freq(optarg);
292                         break;
293                 case 'i':
294                         ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
295                         ilist[ilistlen - 1] = strtol(optarg, &p, 16);
296                         break;
297                 case 'n':
298                         operations = atoi(optarg);
299                         break;
300                 case 'p':
301                         nproc = atoi(optarg);
302                         break;
303                 case 'r':
304                         namerand = 1;
305                         break;
306                 case 's':
307                         seed = strtoul(optarg, NULL, 0);
308                         break;
309                 case 'v':
310                         verbose = 1;
311                         break;
312                 case 'w':
313                         write_freq();
314                         break;
315                 case 'z':
316                         zero_freq();
317                         break;
318                 case 'S':
319                         show_ops(0, NULL);
320                         printf("\n");
321                         nousage=1;
322                         break;
323                 case '?':
324                         fprintf(stderr, "%s - invalid parameters\n",
325                                 myprog);
326                         /* fall through */
327                 case 'H':
328                         usage();
329                         exit(1);
330                 }
331         }
332         
333         if (!dirname) {
334             /* no directory specified */
335             if (!nousage) usage();
336             exit(1);
337         }
338         
339         (void)mkdir(dirname, 0777);
340         if (chdir(dirname) < 0) {
341                 perror(dirname);
342                 exit(1);
343         }
344         sprintf(buf, "fss%x", getpid());
345         fd = creat(buf, 0666);
346         if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
347                 maxfsize = (off64_t)MAXFSIZE32;
348         else
349                 maxfsize = (off64_t)MAXFSIZE;
350         make_freq_table();
351         dcache_init();
352         setlinebuf(stdout);
353         if (!seed) {
354                 gettimeofday(&t, (void *)NULL);
355                 seed = (int)t.tv_sec ^ (int)t.tv_usec;
356                 printf("seed = %ld\n", seed);
357         }
358         i = ioctl(fd, XFS_IOC_FSGEOMETRY, &geom);
359         if (i >= 0 && geom.rtblocks)
360                 rtpct = MIN(MAX(geom.rtblocks * 100 /
361                                 (geom.rtblocks + geom.datablocks), 1), 99);
362         else
363                 rtpct = 0;
364         if (errtag != 0) {
365                 if (errrange == 0) {
366                         if (errtag <= 0) {
367                                 srandom(seed);
368                                 j = random() % 100;
369
370                                 for (i = 0; i < j; i++)
371                                         (void) random();
372
373                                 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
374                         }
375                 } else {
376                         srandom(seed);
377                         j = random() % 100;
378
379                         for (i = 0; i < j; i++)
380                                 (void) random();
381
382                         errtag += (random() % (XFS_ERRTAG_MAX - errtag));
383                 }
384                 printf("Injecting failure on tag #%d\n", errtag);
385                 err_inj.errtag = errtag;
386                 err_inj.fd = fd;
387                 srval = ioctl(fd, XFS_IOC_ERROR_INJECTION, &err_inj);
388                 if (srval < -1) {
389                         perror("fsstress - XFS_SYSSGI error injection call");
390                         close(fd);
391                         unlink(buf);
392                         exit(1);
393                 }
394         } else
395                 close(fd);
396         unlink(buf);
397         for (i = 0; i < nproc; i++) {
398                 if (fork() == 0) {
399                         procid = i;
400                         doproc();
401                         return 0;
402                 }
403         }
404         while (wait(&stat) > 0)
405                 continue;
406         if (errtag != 0) {
407                 err_inj.errtag = 0;
408                 err_inj.fd = fd;
409                 if((srval = ioctl(fd, XFS_IOC_ERROR_CLEARALL, &err_inj)) != 0) {
410                         fprintf(stderr, "Bad ej clear on %d (%d).\n", fd, errno);
411                         perror("fsstress - XFS_SYSSGI clear error injection call");
412                         close(fd);
413                         exit(1);
414                 }
415                 close(fd);
416         }
417
418         return 0;
419 }
420
421 void
422 add_to_flist(int ft, int id, int parent)
423 {
424         fent_t  *fep;
425         flist_t *ftp;
426
427         ftp = &flist[ft];
428         if (ftp->nfiles == ftp->nslots) {
429                 ftp->nslots += FLIST_SLOT_INCR;
430                 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
431         }
432         fep = &ftp->fents[ftp->nfiles++];
433         fep->id = id;
434         fep->parent = parent;
435 }
436
437 void
438 append_pathname(pathname_t *name, char *str)
439 {
440         int     len;
441
442         len = strlen(str);
443 #ifdef DEBUG
444         if (len && *str == '/' && name->len == 0) {
445                 fprintf(stderr, "fsstress: append_pathname failure\n");
446                 chdir(homedir);
447                 abort();
448                 /* NOTREACHED */
449         }
450 #endif
451         name->path = realloc(name->path, name->len + 1 + len);
452         strcpy(&name->path[name->len], str);
453         name->len += len;
454 }
455
456 int
457 attr_list_path(pathname_t *name, char *buffer, const int buffersize, int flags,
458                attrlist_cursor_t *cursor)
459 {
460         char            buf[MAXNAMELEN];
461         pathname_t      newname;
462         int             rval;
463
464         rval = attr_list(name->path, buffer, buffersize, flags, cursor);
465         if (rval >= 0 || errno != ENAMETOOLONG)
466                 return rval;
467         separate_pathname(name, buf, &newname);
468         if (chdir(buf) == 0) {
469                 rval = attr_list_path(&newname, buffer, buffersize, flags,
470                         cursor);
471                 chdir("..");
472         }
473         free_pathname(&newname);
474         return rval;
475 }
476
477 int
478 attr_remove_path(pathname_t *name, const char *attrname, int flags)
479 {
480         char            buf[MAXNAMELEN];
481         pathname_t      newname;
482         int             rval;
483
484         rval = attr_remove(name->path, attrname, flags);
485         if (rval >= 0 || errno != ENAMETOOLONG)
486                 return rval;
487         separate_pathname(name, buf, &newname);
488         if (chdir(buf) == 0) {
489                 rval = attr_remove_path(&newname, attrname, flags);
490                 chdir("..");
491         }
492         free_pathname(&newname);
493         return rval;
494 }
495
496 int
497 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
498               const int valuelength, int flags)
499 {
500         char            buf[MAXNAMELEN];
501         pathname_t      newname;
502         int             rval;
503
504         rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
505         if (rval >= 0 || errno != ENAMETOOLONG)
506                 return rval;
507         separate_pathname(name, buf, &newname);
508         if (chdir(buf) == 0) {
509                 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
510                         flags);
511                 chdir("..");
512         }
513         free_pathname(&newname);
514         return rval;
515 }
516
517 void
518 check_cwd(void)
519 {
520 #ifdef DEBUG
521         struct stat64   statbuf;
522
523         if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
524                 return;
525         chdir(homedir);
526         fprintf(stderr, "fsstress: check_cwd failure\n");
527         abort();
528         /* NOTREACHED */
529 #endif
530 }
531
532 int
533 creat_path(pathname_t *name, mode_t mode)
534 {
535         char            buf[MAXNAMELEN];
536         pathname_t      newname;
537         int             rval;
538
539         rval = creat(name->path, mode);
540         if (rval >= 0 || errno != ENAMETOOLONG)
541                 return rval;
542         separate_pathname(name, buf, &newname);
543         if (chdir(buf) == 0) {
544                 rval = creat_path(&newname, mode);
545                 chdir("..");
546         }
547         free_pathname(&newname);
548         return rval;
549 }
550
551 void
552 dcache_enter(int dirid, int slot)
553 {
554         dcache[dirid % NDCACHE] = slot;
555 }
556
557 void
558 dcache_init(void)
559 {
560         int     i;
561
562         for (i = 0; i < NDCACHE; i++)
563                 dcache[i] = -1;
564 }
565
566 fent_t *
567 dcache_lookup(int dirid)
568 {
569         fent_t  *fep;
570         int     i;
571
572         i = dcache[dirid % NDCACHE];
573         if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
574                 return fep;
575         return NULL;
576 }
577
578 void
579 dcache_purge(int dirid)
580 {
581         int     *dcp;
582
583         dcp = &dcache[dirid % NDCACHE];
584         if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
585                 *dcp = -1;
586 }
587
588 void
589 del_from_flist(int ft, int slot)
590 {
591         flist_t *ftp;
592
593         ftp = &flist[ft];
594         if (ft == FT_DIR)
595                 dcache_purge(ftp->fents[slot].id);
596         if (slot != ftp->nfiles - 1) {
597                 if (ft == FT_DIR)
598                         dcache_purge(ftp->fents[ftp->nfiles - 1].id);
599                 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
600         } else
601                 ftp->nfiles--;
602 }
603
604 fent_t *
605 dirid_to_fent(int dirid)
606 {
607         fent_t  *efep;
608         fent_t  *fep;
609         flist_t *flp;
610
611         if ((fep = dcache_lookup(dirid)))
612                 return fep;
613         flp = &flist[FT_DIR];
614         for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
615                 if (fep->id == dirid) {
616                         dcache_enter(dirid, fep - flp->fents);
617                         return fep;
618                 }
619         }
620         return NULL;
621 }
622
623 void
624 doproc(void)
625 {
626         struct stat64   statbuf;
627         char            buf[10];
628         int             opno;
629         int             rval;
630         opdesc_t        *p;
631
632         sprintf(buf, "p%x", procid);
633         (void)mkdir(buf, 0777);
634         if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
635                 perror(buf);
636                 _exit(1);
637         }
638         top_ino = statbuf.st_ino;
639         homedir = getcwd(NULL, -1);
640         seed += procid;
641         srandom(seed);
642         if (namerand)
643                 namerand = random();
644         for (opno = 0; opno < operations; opno++) {
645                 p = &ops[freq_table[random() % freq_table_size]];
646                 p->func(opno, random());
647                 /*
648                  * test for forced shutdown by stat'ing the test
649                  * directory.  If this stat returns EIO, assume
650                  * the forced shutdown happened.
651                  */
652                 if (errtag != 0 && opno % 100 == 0)  {
653                         rval = stat64(".", &statbuf);
654                         if (rval == EIO)  {
655                                 fprintf(stderr, "Detected EIO\n");
656                                 return;
657                         }
658                 }
659         }
660 }
661
662 void
663 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
664 {
665         char    buf[MAXNAMELEN];
666         int     i;
667         fent_t  *pfep;
668
669         if (fep == NULL)
670                 return;
671         if (fep->parent != -1) {
672                 pfep = dirid_to_fent(fep->parent);
673                 fent_to_name(name, &flist[FT_DIR], pfep);
674                 append_pathname(name, "/");
675         }
676         i = sprintf(buf, "%c%x", flp->tag, fep->id);
677         namerandpad(fep->id, buf, i);
678         append_pathname(name, buf);
679 }
680
681 void
682 fix_parent(int oldid, int newid)
683 {
684         fent_t  *fep;
685         flist_t *flp;
686         int     i;
687         int     j;
688
689         for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
690                 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
691                         if (fep->parent == oldid)
692                                 fep->parent = newid;
693                 }
694         }
695 }
696
697 void
698 free_pathname(pathname_t *name)
699 {
700         if (name->path) {
701                 free(name->path);
702                 name->path = NULL;
703                 name->len = 0;
704         }
705 }
706
707 int
708 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
709 {
710         char    buf[MAXNAMELEN];
711         flist_t *flp;
712         int     id;
713         int     j;
714         int     len;
715
716         flp = &flist[ft];
717         len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
718         namerandpad(id, buf, len);
719         if (fep) {
720                 fent_to_name(name, &flist[FT_DIR], fep);
721                 append_pathname(name, "/");
722         }
723         append_pathname(name, buf);
724         *idp = id;
725         *v = verbose;
726         for (j = 0; !*v && j < ilistlen; j++) {
727                 if (ilist[j] == id) {
728                         *v = 1;
729                         break;
730                 }
731         }
732         return 1;
733 }
734
735 int
736 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
737           int *v)
738 {
739         int     c;
740         fent_t  *fep;
741         flist_t *flp;
742         int     i;
743         int     j;
744         int     x;
745
746         for (i = 0, c = 0, flp = flist; i < FT_nft; i++, flp++) {
747                 if (which & (1 << i))
748                         c += flp->nfiles;
749         }
750         if (c == 0) {
751                 if (flpp)
752                         *flpp = NULL;
753                 if (fepp)
754                         *fepp = NULL;
755                 *v = verbose;
756                 return 0;
757         }
758         x = (int)(r % c);
759         for (i = 0, c = 0, flp = flist; i < FT_nft; i++, flp++) {
760                 if (which & (1 << i)) {
761                         if (x < c + flp->nfiles) {
762                                 fep = &flp->fents[x - c];
763                                 if (name)
764                                         fent_to_name(name, flp, fep);
765                                 if (flpp)
766                                         *flpp = flp;
767                                 if (fepp)
768                                         *fepp = fep;
769                                 *v = verbose;
770                                 for (j = 0; !*v && j < ilistlen; j++) {
771                                         if (ilist[j] == fep->id) {
772                                                 *v = 1;
773                                                 break;
774                                         }
775                                 }
776                                 return 1;
777                         }
778                         c += flp->nfiles;
779                 }
780         }
781 #ifdef DEBUG
782         fprintf(stderr, "fsstress: get_fname failure\n");
783         abort();
784 #endif
785         return -1;
786         /* NOTREACHED */
787 }
788
789 void
790 init_pathname(pathname_t *name)
791 {
792         name->len = 0;
793         name->path = NULL;
794 }
795
796 int
797 lchown_path(pathname_t *name, uid_t owner, gid_t group)
798 {
799         char            buf[MAXNAMELEN];
800         pathname_t      newname;
801         int             rval;
802
803         rval = lchown(name->path, owner, group);
804         if (rval >= 0 || errno != ENAMETOOLONG)
805                 return rval;
806         separate_pathname(name, buf, &newname);
807         if (chdir(buf) == 0) {
808                 rval = lchown_path(&newname, owner, group);
809                 chdir("..");
810         }
811         free_pathname(&newname);
812         return rval;
813 }
814
815 int
816 link_path(pathname_t *name1, pathname_t *name2)
817 {
818         char            buf1[MAXNAMELEN];
819         char            buf2[MAXNAMELEN];
820         int             down1;
821         pathname_t      newname1;
822         pathname_t      newname2;
823         int             rval;
824
825         rval = link(name1->path, name2->path);
826         if (rval >= 0 || errno != ENAMETOOLONG)
827                 return rval;
828         separate_pathname(name1, buf1, &newname1);
829         separate_pathname(name2, buf2, &newname2);
830         if (strcmp(buf1, buf2) == 0) {
831                 if (chdir(buf1) == 0) {
832                         rval = link_path(&newname1, &newname2);
833                         chdir("..");
834                 }
835         } else {
836                 if (strcmp(buf1, "..") == 0)
837                         down1 = 0;
838                 else if (strcmp(buf2, "..") == 0)
839                         down1 = 1;
840                 else if (strlen(buf1) == 0)
841                         down1 = 0;
842                 else if (strlen(buf2) == 0)
843                         down1 = 1;
844                 else
845                         down1 = MAX(newname1.len, 3 + name2->len) <=
846                                 MAX(3 + name1->len, newname2.len);
847                 if (down1) {
848                         free_pathname(&newname2);
849                         append_pathname(&newname2, "../");
850                         append_pathname(&newname2, name2->path);
851                         if (chdir(buf1) == 0) {
852                                 rval = link_path(&newname1, &newname2);
853                                 chdir("..");
854                         }
855                 } else {
856                         free_pathname(&newname1);
857                         append_pathname(&newname1, "../");
858                         append_pathname(&newname1, name1->path);
859                         if (chdir(buf2) == 0) {
860                                 rval = link_path(&newname1, &newname2);
861                                 chdir("..");
862                         }
863                 }
864         }
865         free_pathname(&newname1);
866         free_pathname(&newname2);
867         return rval;
868 }
869
870 int
871 lstat64_path(pathname_t *name, struct stat64 *sbuf)
872 {
873         char            buf[MAXNAMELEN];
874         pathname_t      newname;
875         int             rval;
876
877         rval = lstat64(name->path, sbuf);
878         if (rval >= 0 || errno != ENAMETOOLONG)
879                 return rval;
880         separate_pathname(name, buf, &newname);
881         if (chdir(buf) == 0) {
882                 rval = lstat64_path(&newname, sbuf);
883                 chdir("..");
884         }
885         free_pathname(&newname);
886         return rval;
887 }
888
889 void
890 make_freq_table(void)
891 {
892         int             f;
893         int             i;
894         opdesc_t        *p;
895
896         for (p = ops, f = 0; p < ops_end; p++)
897                 f += p->freq;
898         freq_table = malloc(f * sizeof(*freq_table));
899         freq_table_size = f;
900         for (p = ops, i = 0; p < ops_end; p++) {
901                 for (f = 0; f < p->freq; f++, i++)
902                         freq_table[i] = p->op;
903         }
904 }
905
906 int
907 mkdir_path(pathname_t *name, mode_t mode)
908 {
909         char            buf[MAXNAMELEN];
910         pathname_t      newname;
911         int             rval;
912
913         rval = mkdir(name->path, mode);
914         if (rval >= 0 || errno != ENAMETOOLONG)
915                 return rval;
916         separate_pathname(name, buf, &newname);
917         if (chdir(buf) == 0) {
918                 rval = mkdir_path(&newname, mode);
919                 chdir("..");
920         }
921         free_pathname(&newname);
922         return rval;
923 }
924
925 int
926 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
927 {
928         char            buf[MAXNAMELEN];
929         pathname_t      newname;
930         int             rval;
931
932         rval = mknod(name->path, mode, dev);
933         if (rval >= 0 || errno != ENAMETOOLONG)
934                 return rval;
935         separate_pathname(name, buf, &newname);
936         if (chdir(buf) == 0) {
937                 rval = mknod_path(&newname, mode, dev);
938                 chdir("..");
939         }
940         free_pathname(&newname);
941         return rval;
942 }
943
944 void
945 namerandpad(int id, char *buf, int i)
946 {
947         int             bucket;
948         static int      buckets[] =
949                                 { 2, 4, 8, 16, 32, 64, 128, MAXNAMELEN - 1 };
950         int             padlen;
951         int             padmod;
952
953         if (namerand == 0)
954                 return;
955         bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
956         padmod = buckets[bucket] + 1 - i;
957         if (padmod <= 0)
958                 return;
959         padlen = (id ^ namerand) % padmod;
960         if (padlen) {
961                 memset(&buf[i], 'X', padlen);
962                 buf[i + padlen] = '\0';
963         }
964 }
965
966 int
967 open_path(pathname_t *name, int oflag)
968 {
969         char            buf[MAXNAMELEN];
970         pathname_t      newname;
971         int             rval;
972
973         rval = open(name->path, oflag);
974         if (rval >= 0 || errno != ENAMETOOLONG)
975                 return rval;
976         separate_pathname(name, buf, &newname);
977         if (chdir(buf) == 0) {
978                 rval = open_path(&newname, oflag);
979                 chdir("..");
980         }
981         free_pathname(&newname);
982         return rval;
983 }
984
985 DIR *
986 opendir_path(pathname_t *name)
987 {
988         char            buf[MAXNAMELEN];
989         pathname_t      newname;
990         DIR             *rval;
991
992         rval = opendir(name->path);
993         if (rval || errno != ENAMETOOLONG)
994                 return rval;
995         separate_pathname(name, buf, &newname);
996         if (chdir(buf) == 0) {
997                 rval = opendir_path(&newname);
998                 chdir("..");
999         }
1000         free_pathname(&newname);
1001         return rval;
1002 }
1003
1004 void
1005 process_freq(char *arg)
1006 {
1007         opdesc_t        *p;
1008         char            *s;
1009
1010         s = strchr(arg, '=');
1011         if (s == NULL) {
1012                 fprintf(stderr, "bad argument '%s'\n", arg);
1013                 exit(1);
1014         }
1015         *s++ = '\0';
1016         for (p = ops; p < ops_end; p++) {
1017                 if (strcmp(arg, p->name) == 0) {
1018                         p->freq = atoi(s);
1019                         return;
1020                 }
1021         }
1022         fprintf(stderr, "can't find op type %s for -f\n", arg);
1023         exit(1);
1024 }
1025
1026 int
1027 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1028 {
1029         char            buf[MAXNAMELEN];
1030         pathname_t      newname;
1031         int             rval;
1032
1033         rval = readlink(name->path, lbuf, lbufsiz);
1034         if (rval >= 0 || errno != ENAMETOOLONG)
1035                 return rval;
1036         separate_pathname(name, buf, &newname);
1037         if (chdir(buf) == 0) {
1038                 rval = readlink_path(&newname, lbuf, lbufsiz);
1039                 chdir("..");
1040         }
1041         free_pathname(&newname);
1042         return rval;
1043 }
1044
1045 int
1046 rename_path(pathname_t *name1, pathname_t *name2)
1047 {
1048         char            buf1[MAXNAMELEN];
1049         char            buf2[MAXNAMELEN];
1050         int             down1;
1051         pathname_t      newname1;
1052         pathname_t      newname2;
1053         int             rval;
1054
1055         rval = rename(name1->path, name2->path);
1056         if (rval >= 0 || errno != ENAMETOOLONG)
1057                 return rval;
1058         separate_pathname(name1, buf1, &newname1);
1059         separate_pathname(name2, buf2, &newname2);
1060         if (strcmp(buf1, buf2) == 0) {
1061                 if (chdir(buf1) == 0) {
1062                         rval = rename_path(&newname1, &newname2);
1063                         chdir("..");
1064                 }
1065         } else {
1066                 if (strcmp(buf1, "..") == 0)
1067                         down1 = 0;
1068                 else if (strcmp(buf2, "..") == 0)
1069                         down1 = 1;
1070                 else if (strlen(buf1) == 0)
1071                         down1 = 0;
1072                 else if (strlen(buf2) == 0)
1073                         down1 = 1;
1074                 else
1075                         down1 = MAX(newname1.len, 3 + name2->len) <=
1076                                 MAX(3 + name1->len, newname2.len);
1077                 if (down1) {
1078                         free_pathname(&newname2);
1079                         append_pathname(&newname2, "../");
1080                         append_pathname(&newname2, name2->path);
1081                         if (chdir(buf1) == 0) {
1082                                 rval = rename_path(&newname1, &newname2);
1083                                 chdir("..");
1084                         }
1085                 } else {
1086                         free_pathname(&newname1);
1087                         append_pathname(&newname1, "../");
1088                         append_pathname(&newname1, name1->path);
1089                         if (chdir(buf2) == 0) {
1090                                 rval = rename_path(&newname1, &newname2);
1091                                 chdir("..");
1092                         }
1093                 }
1094         }
1095         free_pathname(&newname1);
1096         free_pathname(&newname2);
1097         return rval;
1098 }
1099
1100 int
1101 rmdir_path(pathname_t *name)
1102 {
1103         char            buf[MAXNAMELEN];
1104         pathname_t      newname;
1105         int             rval;
1106
1107         rval = rmdir(name->path);
1108         if (rval >= 0 || errno != ENAMETOOLONG)
1109                 return rval;
1110         separate_pathname(name, buf, &newname);
1111         if (chdir(buf) == 0) {
1112                 rval = rmdir_path(&newname);
1113                 chdir("..");
1114         }
1115         free_pathname(&newname);
1116         return rval;
1117 }
1118
1119 void
1120 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1121 {
1122         char    *slash;
1123
1124         init_pathname(newname);
1125         slash = strchr(name->path, '/');
1126         if (slash == NULL) {
1127                 buf[0] = '\0';
1128                 return;
1129         }
1130         *slash = '\0';
1131         strcpy(buf, name->path);
1132         *slash = '/';
1133         append_pathname(newname, slash + 1);
1134 }
1135
1136 #define WIDTH 80
1137
1138 void
1139 show_ops(int flag, char *lead_str)
1140 {
1141         opdesc_t        *p;
1142
1143         if (flag<0) {
1144                 /* print in list form */
1145                 int             x = WIDTH;
1146                 
1147                 for (p = ops; p < ops_end; p++) {
1148                         if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1149                                 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1150                         x+=printf("%s ", p->name);
1151                 }
1152                 printf("\n");
1153         } else {
1154                 int             f;
1155                 for (f = 0, p = ops; p < ops_end; p++)
1156                         f += p->freq;
1157
1158                 if (f == 0)
1159                         flag = 1;
1160
1161                 for (p = ops; p < ops_end; p++) {
1162                         if (flag != 0 || p->freq > 0) {
1163                                 if (lead_str != NULL)
1164                                         printf("%s", lead_str);
1165                                 printf("%20s %d/%d %s\n",
1166                                 p->name, p->freq, f,
1167                                 (p->iswrite == 0) ? " " : "write op");
1168                         }
1169                 }
1170         }
1171 }
1172
1173 int
1174 stat64_path(pathname_t *name, struct stat64 *sbuf)
1175 {
1176         char            buf[MAXNAMELEN];
1177         pathname_t      newname;
1178         int             rval;
1179
1180         rval = stat64(name->path, sbuf);
1181         if (rval >= 0 || errno != ENAMETOOLONG)
1182                 return rval;
1183         separate_pathname(name, buf, &newname);
1184         if (chdir(buf) == 0) {
1185                 rval = stat64_path(&newname, sbuf);
1186                 chdir("..");
1187         }
1188         free_pathname(&newname);
1189         return rval;
1190 }
1191
1192 int
1193 symlink_path(const char *name1, pathname_t *name)
1194 {
1195         char            buf[MAXNAMELEN];
1196         pathname_t      newname;
1197         int             rval;
1198         
1199         if (!strcmp(name1, name->path)) {
1200             printf("yikes! %s %s\n", name1, name->path);
1201             return 0;
1202         }
1203
1204         rval = symlink(name1, name->path);
1205         if (rval >= 0 || errno != ENAMETOOLONG)
1206                 return rval;
1207         separate_pathname(name, buf, &newname);
1208         if (chdir(buf) == 0) {
1209                 rval = symlink_path(name1, &newname);
1210                 chdir("..");
1211         }
1212         free_pathname(&newname);
1213         return rval;
1214 }
1215
1216 int
1217 truncate64_path(pathname_t *name, off64_t length)
1218 {
1219         char            buf[MAXNAMELEN];
1220         pathname_t      newname;
1221         int             rval;
1222
1223         rval = truncate64(name->path, length);
1224         if (rval >= 0 || errno != ENAMETOOLONG)
1225                 return rval;
1226         separate_pathname(name, buf, &newname);
1227         if (chdir(buf) == 0) {
1228                 rval = truncate64_path(&newname, length);
1229                 chdir("..");
1230         }
1231         free_pathname(&newname);
1232         return rval;
1233 }
1234
1235 int
1236 unlink_path(pathname_t *name)
1237 {
1238         char            buf[MAXNAMELEN];
1239         pathname_t      newname;
1240         int             rval;
1241
1242         rval = unlink(name->path);
1243         if (rval >= 0 || errno != ENAMETOOLONG)
1244                 return rval;
1245         separate_pathname(name, buf, &newname);
1246         if (chdir(buf) == 0) {
1247                 rval = unlink_path(&newname);
1248                 chdir("..");
1249         }
1250         free_pathname(&newname);
1251         return rval;
1252 }
1253
1254 void
1255 usage(void)
1256 {
1257         printf("Usage: %s -H   or\n", myprog);
1258         printf("       %s [-d dir][-e errtg][-f op_name=freq][-n nops]\n",
1259                 myprog);
1260         printf("          [-p nproc][-r len][-s seed][-v][-w][-z][-S]\n");
1261         printf("where\n");
1262         printf("   -d dir           specifies the base directory for operations\n");
1263         printf("   -e errtg         specifies error injection stuff\n");
1264         printf("   -f op_name=freq  changes the frequency of option name to freq\n");
1265         printf("                    the valid operation names are:\n");
1266         show_ops(-1, "                        ");
1267         printf("   -n nops          specifies the no. of operations per process (default 1)\n");
1268         printf("   -p nproc         specifies the no. of processes (default 1)\n");
1269         printf("   -r               specifies random name padding\n");
1270         printf("   -s seed          specifies the seed for the random generator (default random)\n");
1271         printf("   -v               specifies verbose mode\n");
1272         printf("   -w               zeros frequencies of non-write operations\n");
1273         printf("   -z               zeros frequencies of all operations\n");
1274         printf("   -S               prints the table of operations (omitting zero frequency)\n");
1275         printf("   -H               prints usage and exits\n");
1276 }
1277
1278 void
1279 write_freq(void)
1280 {
1281         opdesc_t        *p;
1282
1283         for (p = ops; p < ops_end; p++) {
1284                 if (!p->iswrite)
1285                         p->freq = 0;
1286         }
1287 }
1288
1289 void
1290 zero_freq(void)
1291 {
1292         opdesc_t        *p;
1293
1294         for (p = ops; p < ops_end; p++)
1295                 p->freq = 0;
1296 }
1297
1298 void
1299 allocsp_f(int opno, long r)
1300 {
1301         int             e;
1302         pathname_t      f;
1303         int             fd;
1304         struct flock64  fl;
1305         __int64_t       lr;
1306         off64_t         off;
1307         struct stat64   stb;
1308         int             v;
1309
1310         init_pathname(&f);
1311         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1312                 if (v)
1313                         printf("%d/%d: allocsp - no filename\n", procid, opno);
1314                 free_pathname(&f);
1315                 return;
1316         }
1317         fd = open_path(&f, O_RDWR);
1318         e = fd < 0 ? errno : 0;
1319         check_cwd();
1320         if (fd < 0) {
1321                 if (v)
1322                         printf("%d/%d: allocsp - open %s failed %d\n",
1323                                 procid, opno, f.path, e);
1324                 free_pathname(&f);
1325                 return;
1326         }
1327         if (fstat64(fd, &stb) < 0) {
1328                 if (v)
1329                         printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1330                                 procid, opno, f.path, errno);
1331                 free_pathname(&f);
1332                 close(fd);
1333                 return;
1334         }
1335         lr = ((__int64_t)random() << 32) + random();
1336         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1337         off %= maxfsize;
1338         fl.l_whence = SEEK_SET;
1339         fl.l_start = off;
1340         fl.l_len = 0;
1341         e = ioctl(fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1342         if (v)
1343                 printf("%d/%d: ioctl(XFS_IOC_ALLOCSP64) %s %lld 0 %d\n",
1344                         procid, opno, f.path, off, e);
1345         free_pathname(&f);
1346         close(fd);
1347 }
1348
1349 void
1350 attr_remove_f(int opno, long r)
1351 {
1352         attrlist_ent_t          *aep;
1353         attrlist_t              *alist;
1354         char                    *aname;
1355         char                    buf[4096];
1356         attrlist_cursor_t       cursor;
1357         int                     e;
1358         int                     ent;
1359         pathname_t              f;
1360         int                     total;
1361         int                     v;
1362         int                     which;
1363
1364         init_pathname(&f);
1365         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1366                 append_pathname(&f, ".");
1367         total = 0;
1368         bzero(&cursor, sizeof(cursor));
1369         do {
1370                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW,
1371                         &cursor);
1372                 check_cwd();
1373                 if (e)
1374                         break;
1375                 alist = (attrlist_t *)buf;
1376                 total += alist->al_count;
1377         } while (alist->al_more);
1378         if (total == 0) {
1379                 if (v)
1380                         printf("%d/%d: attr_remove - no attrs for %s\n",
1381                                 procid, opno, f.path);
1382                 free_pathname(&f);
1383                 return;
1384         }
1385         which = (int)(random() % total);
1386         bzero(&cursor, sizeof(cursor));
1387         ent = 0;
1388         aname = NULL;
1389         do {
1390                 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW,
1391                         &cursor);
1392                 check_cwd();
1393                 if (e)
1394                         break;
1395                 alist = (attrlist_t *)buf;
1396                 if (which < ent + alist->al_count) {
1397                         aep = (attrlist_ent_t *)
1398                                 &buf[alist->al_offset[which - ent]];
1399                         aname = aep->a_name;
1400                         break;
1401                 }
1402                 ent += alist->al_count;
1403         } while (alist->al_more);
1404         if (aname == NULL) {
1405                 if (v)
1406                         printf(
1407                         "%d/%d: attr_remove - name %d not found at %s\n",       
1408                                 procid, opno, which, f.path);
1409                 free_pathname(&f);
1410                 return;
1411         }
1412         e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1413         check_cwd();
1414         if (v)
1415                 printf("%d/%d: attr_remove %s %s %d\n",
1416                         procid, opno, f.path, aname, e);
1417         free_pathname(&f);
1418 }
1419
1420 void
1421 attr_set_f(int opno, long r)
1422 {
1423         char            aname[10];
1424         char            *aval;
1425         int             e;
1426         pathname_t      f;
1427         int             len;
1428         static int      lengths[] = { 10, 100, 1000, 10000 };
1429         int             li;
1430         int             v;
1431
1432         init_pathname(&f);
1433         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1434                 append_pathname(&f, ".");
1435         sprintf(aname, "a%x", nameseq++);
1436         li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1437         len = (int)(random() % lengths[li]);
1438         if (len == 0)
1439                 len = 1;
1440         aval = malloc(len);
1441         memset(aval, nameseq & 0xff, len);
1442         e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1443                 errno : 0;
1444         check_cwd();
1445         free(aval);
1446         if (v)
1447                 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1448                         aname, e);
1449         free_pathname(&f);
1450 }
1451
1452 void
1453 bulkstat_f(int opno, long r)
1454 {
1455         int             count;
1456         int             fd;
1457         __uint64_t      last;
1458         int             nent;
1459         xfs_bstat_t     *t;
1460         __int64_t       total;
1461         xfs_fsop_bulkreq_t bsr;
1462
1463         last = 0;
1464         nent = (r % 999) + 2;
1465         t = malloc(nent * sizeof(*t));
1466         fd = open(".", O_RDONLY);
1467         total = 0;
1468     
1469         bsr.lastip=&last;
1470         bsr.icount=nent;
1471         bsr.ubuffer=t;
1472         bsr.ocount=&count;
1473             
1474         while (ioctl(fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1475                 total += count;
1476         free(t);
1477         if (verbose)
1478                 printf("%d/%d: bulkstat nent %d total %lld\n",
1479                         procid, opno, nent, total);
1480         close(fd);
1481 }
1482
1483 void
1484 bulkstat1_f(int opno, long r)
1485 {
1486         int             e;
1487         pathname_t      f;
1488         int             fd;
1489         int             good;
1490         __uint64_t      ino;
1491         struct stat64   s;
1492         xfs_bstat_t     t;
1493         int             v;
1494         xfs_fsop_bulkreq_t bsr;
1495         
1496
1497         good = random() & 1;
1498         if (good) {
1499                /* use an inode we know exists */
1500                 init_pathname(&f);
1501                 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1502                         append_pathname(&f, ".");
1503                 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1504                 check_cwd();
1505                 free_pathname(&f);
1506         } else {
1507                 /* 
1508                  * pick a random inode 
1509                  *
1510                  * note this can generate kernel warning messages
1511                  * since bulkstat_one will read the disk block that
1512                  * would contain a given inode even if that disk
1513                  * block doesn't contain inodes.
1514                  *
1515                  * this is detected later, but not until after the
1516                  * warning is displayed.
1517                  *
1518                  * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1519                  *
1520                  */
1521                 ino = (ino64_t)r;
1522                 v = verbose;
1523         }
1524         fd = open(".", O_RDONLY);
1525         
1526         bsr.lastip=&ino;
1527         bsr.icount=1;
1528         bsr.ubuffer=&t;
1529         bsr.ocount=NULL;
1530         
1531         e = ioctl(fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1532         if (v)
1533                 printf("%d/%d: bulkstat1 %s ino %lld %d\n", 
1534                     procid, opno, good?"real":"random", (int64_t)ino, e);
1535         close(fd);
1536 }
1537
1538 void
1539 chown_f(int opno, long r)
1540 {
1541         int             e;
1542         pathname_t      f;
1543         int             nbits;
1544         uid_t           u;
1545         int             v;
1546
1547         init_pathname(&f);
1548         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1549                 append_pathname(&f, ".");
1550         u = (uid_t)random();
1551         nbits = (int)(random() % 32);
1552         u &= (1 << nbits) - 1;
1553         e = lchown_path(&f, u, -1) < 0 ? errno : 0;
1554         check_cwd();
1555         if (v)
1556                 printf("%d/%d: chown %s %d %d\n", procid, opno, f.path, u, e);
1557         free_pathname(&f);
1558 }
1559
1560 void
1561 creat_f(int opno, long r)
1562 {
1563         struct fsxattr  a;
1564         int             e;
1565         int             e1;
1566         int             extsize;
1567         pathname_t      f;
1568         int             fd;
1569         fent_t          *fep;
1570         int             id;
1571         int             parid;
1572         int             type;
1573         int             v;
1574         int             v1;
1575
1576         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1577                 parid = -1;
1578         else
1579                 parid = fep->id;
1580         init_pathname(&f);
1581         type = rtpct ? ((random() % 100) > rtpct ? FT_REG : FT_RTF) : FT_REG;
1582         if (type == FT_RTF)
1583                 extsize = (random() % 10) + 1;
1584         else
1585                 extsize = 0;
1586         e = generate_fname(fep, type, &f, &id, &v);
1587         v |= v1;
1588         if (!e) {
1589                 if (v) {
1590                         fent_to_name(&f, &flist[FT_DIR], fep);
1591                         printf("%d/%d: creat - no filename from %s\n",
1592                                 procid, opno, f.path);
1593                 }
1594                 free_pathname(&f);
1595                 return;
1596         }
1597         fd = creat_path(&f, 0666);
1598         e = fd < 0 ? errno : 0;
1599         e1 = 0;
1600         check_cwd();
1601         if (fd >= 0) {
1602                 if (extsize && ioctl(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 (ioctl(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 (ioctl(fd, XFS_IOC_DIOINFO, &diob) < 0) {
1667                 if (v)
1668                         printf(
1669                         "%d/%d: dread - ioctl(fd, 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, off, 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 (ioctl(fd, XFS_IOC_DIOINFO, &diob) < 0) {
1737                 if (v)
1738                         printf(
1739                         "%d/%d: dwrite - ioctl(fd, XFS_IOC_DIOINFO) %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, off, 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 = ioctl(fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
1846         if (v)
1847                 printf("%d/%d: ioctl(XFS_IOC_FREESP64) %s %lld 0 %d\n",
1848                         procid, opno, f.path, 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, off, 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 = ioctl(fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2216         if (v)
2217                 printf("%d/%d: ioctl(XFS_IOC_RESVSP64) %s %lld %lld %d\n",
2218                         procid, opno, f.path, off, fl.l_len, e);
2219         free_pathname(&f);
2220         close(fd);
2221 }
2222
2223 void
2224 rmdir_f(int opno, long r)
2225 {
2226         int             e;
2227         pathname_t      f;
2228         fent_t          *fep;
2229         int             v;
2230
2231         init_pathname(&f);
2232         if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2233                 if (v)
2234                         printf("%d/%d: rmdir - no directory\n", procid, opno);
2235                 free_pathname(&f);
2236                 return;
2237         }
2238         e = rmdir_path(&f) < 0 ? errno : 0;
2239         check_cwd();
2240         if (e == 0)
2241                 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2242         if (v)
2243                 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2244         free_pathname(&f);
2245 }
2246
2247 void
2248 stat_f(int opno, long r)
2249 {
2250         int             e;
2251         pathname_t      f;
2252         struct stat64   stb;
2253         int             v;
2254
2255         init_pathname(&f);
2256         if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2257                 if (v)
2258                         printf("%d/%d: stat - no entries\n", procid, opno);
2259                 free_pathname(&f);
2260                 return;
2261         }
2262         e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2263         check_cwd();
2264         if (v)
2265                 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2266         free_pathname(&f);
2267 }
2268
2269 void
2270 symlink_f(int opno, long r)
2271 {
2272         int             e;
2273         pathname_t      f;
2274         fent_t          *fep;
2275         int             i;
2276         int             id;
2277         int             len;
2278         int             parid;
2279         int             v;
2280         int             v1;
2281         char            *val;
2282
2283         if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2284                 parid = -1;
2285         else
2286                 parid = fep->id;
2287         init_pathname(&f);
2288         e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2289         v |= v1;
2290         if (!e) {
2291                 if (v) {
2292                         fent_to_name(&f, &flist[FT_DIR], fep);
2293                         printf("%d/%d: symlink - no filename from %s\n",
2294                                 procid, opno, f.path);
2295                 }
2296                 free_pathname(&f);
2297                 return;
2298         }
2299         len = (int)(random() % PATH_MAX);
2300         val = malloc(len + 1);
2301         if (len)
2302                 memset(val, 'x', len);
2303         val[len] = '\0';
2304         for (i = 10; i < len - 1; i += 10)
2305                 val[i] = '/';
2306         e = symlink_path(val, &f) < 0 ? errno : 0;
2307         check_cwd();
2308         if (e == 0)
2309                 add_to_flist(FT_SYM, id, parid);
2310         free(val);
2311         if (v)
2312                 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
2313         free_pathname(&f);
2314 }
2315
2316 /* ARGSUSED */
2317 void
2318 sync_f(int opno, long r)
2319 {
2320         sync();
2321         if (verbose)
2322                 printf("%d/%d: sync\n", procid, opno);
2323 }
2324
2325 void
2326 truncate_f(int opno, long r)
2327 {
2328         int             e;
2329         pathname_t      f;
2330         __int64_t       lr;
2331         off64_t         off;
2332         struct stat64   stb;
2333         int             v;
2334
2335         init_pathname(&f);
2336         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2337                 if (v)
2338                         printf("%d/%d: truncate - no filename\n", procid, opno);
2339                 free_pathname(&f);
2340                 return;
2341         }
2342         e = stat64_path(&f, &stb) < 0 ? errno : 0;
2343         check_cwd();
2344         if (e > 0) {
2345                 if (v)
2346                         printf("%d/%d: truncate - stat64 %s failed %d\n",
2347                                 procid, opno, f.path, e);
2348                 free_pathname(&f);
2349                 return;
2350         }
2351         lr = ((__int64_t)random() << 32) + random();
2352         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2353         off %= maxfsize;
2354         e = truncate64_path(&f, off) < 0 ? errno : 0;
2355         check_cwd();
2356         if (v)
2357                 printf("%d/%d: truncate %s %lld %d\n", procid, opno, f.path,
2358                         off, e);
2359         free_pathname(&f);
2360 }
2361
2362 void
2363 unlink_f(int opno, long r)
2364 {
2365         int             e;
2366         pathname_t      f;
2367         fent_t          *fep;
2368         flist_t         *flp;
2369         int             v;
2370
2371         init_pathname(&f);
2372         if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
2373                 if (v)
2374                         printf("%d/%d: unlink - no file\n", procid, opno);
2375                 free_pathname(&f);
2376                 return;
2377         }
2378         e = unlink_path(&f) < 0 ? errno : 0;
2379         check_cwd();
2380         if (e == 0)
2381                 del_from_flist(flp - flist, fep - flp->fents);
2382         if (v)
2383                 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
2384         free_pathname(&f);
2385 }
2386
2387 void
2388 unresvsp_f(int opno, long r)
2389 {
2390         int             e;
2391         pathname_t      f;
2392         int             fd;
2393         struct flock64  fl;
2394         __int64_t       lr;
2395         off64_t         off;
2396         struct stat64   stb;
2397         int             v;
2398
2399         init_pathname(&f);
2400         if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2401                 if (v)
2402                         printf("%d/%d: unresvsp - 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: unresvsp - 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: unresvsp - fstat64 %s failed %d\n",
2419                                 procid, opno, f.path, errno);
2420                 free_pathname(&f);
2421                 close(fd);
2422                 return;
2423         }
2424         lr = ((__int64_t)random() << 32) + random();
2425         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2426         off %= maxfsize;
2427         fl.l_whence = SEEK_SET;
2428         fl.l_start = off;
2429         fl.l_len = (off64_t)(random() % (1 << 20));
2430         e = ioctl(fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
2431         if (v)
2432                 printf("%d/%d: ioctl(XFS_IOC_UNRESVSP64) %s %lld %lld %d\n",
2433                         procid, opno, f.path, off, fl.l_len, e);
2434         free_pathname(&f);
2435         close(fd);
2436 }
2437
2438 void
2439 write_f(int opno, long r)
2440 {
2441         char            *buf;
2442         int             e;
2443         pathname_t      f;
2444         int             fd;
2445         size_t          len;
2446         __int64_t       lr;
2447         off64_t         off;
2448         struct stat64   stb;
2449         int             v;
2450
2451         init_pathname(&f);
2452         if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
2453                 if (v)
2454                         printf("%d/%d: write - no filename\n", procid, opno);
2455                 free_pathname(&f);
2456                 return;
2457         }
2458         fd = open_path(&f, O_WRONLY);
2459         e = fd < 0 ? errno : 0;
2460         check_cwd();
2461         if (fd < 0) {
2462                 if (v)
2463                         printf("%d/%d: write - open %s failed %d\n",
2464                                 procid, opno, f.path, e);
2465                 free_pathname(&f);
2466                 return;
2467         }
2468         if (fstat64(fd, &stb) < 0) {
2469                 if (v)
2470                         printf("%d/%d: write - fstat64 %s failed %d\n",
2471                                 procid, opno, f.path, errno);
2472                 free_pathname(&f);
2473                 close(fd);
2474                 return;
2475         }
2476         lr = ((__int64_t)random() << 32) + random();
2477         off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2478         off %= maxfsize;
2479         lseek64(fd, off, SEEK_SET);
2480         len = (random() % (getpagesize() * 32)) + 1;
2481         buf = malloc(len);
2482         memset(buf, nameseq & 0xff, len);
2483         e = write(fd, buf, len) < 0 ? errno : 0;
2484         free(buf);
2485         if (v)
2486                 printf("%d/%d: write %s [%lld,%d] %d\n",
2487                         procid, opno, f.path, off, len, e);
2488         free_pathname(&f);
2489         close(fd);
2490 }