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