1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
13 #ifdef HAVE_ATTR_ATTRIBUTES_H
14 #include <attr/attributes.h>
16 #ifdef HAVE_LINUX_FIEMAP_H
17 #include <linux/fiemap.h>
19 #ifndef HAVE_ATTR_LIST
20 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
22 #ifdef HAVE_SYS_PRCTL_H
23 #include <sys/prctl.h>
29 #include <sys/syscall.h>
30 #include <sys/xattr.h>
32 #ifndef FS_IOC_GETFLAGS
33 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
35 #ifndef FS_IOC_SETFLAGS
36 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
40 #define XFS_ERRTAG_MAX 17
41 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
42 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
47 #ifndef HAVE_RENAMEAT2
48 #if !defined(SYS_renameat2) && defined(__x86_64__)
49 #define SYS_renameat2 316
52 #if !defined(SYS_renameat2) && defined(__i386__)
53 #define SYS_renameat2 353
56 static int renameat2(int dfd1, const char *path1,
57 int dfd2, const char *path2,
61 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
69 #ifndef RENAME_NOREPLACE
70 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
72 #ifndef RENAME_WHITEOUT
73 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
76 #define FILELEN_MAX (32*4096)
136 typedef void (*opfnc_t)(int, long);
138 typedef struct opdesc {
146 typedef struct fent {
152 typedef struct flist {
159 typedef struct pathname {
169 struct print_string {
176 #define FT_DIRm (1 << FT_DIR)
178 #define FT_REGm (1 << FT_REG)
180 #define FT_SYMm (1 << FT_SYM)
182 #define FT_DEVm (1 << FT_DEV)
184 #define FT_RTFm (1 << FT_RTF)
186 #define FT_ANYm ((1 << FT_nft) - 1)
187 #define FT_REGFILE (FT_REGm | FT_RTFm)
188 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
190 #define FLIST_SLOT_INCR 16
193 #define MAXFSIZE ((1ULL << 63) - 1ULL)
194 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
196 #define XATTR_NAME_BUF_SIZE 18
198 void afsync_f(int, long);
199 void allocsp_f(int, long);
200 void aread_f(int, long);
201 void attr_remove_f(int, long);
202 void attr_set_f(int, long);
203 void awrite_f(int, long);
204 void bulkstat_f(int, long);
205 void bulkstat1_f(int, long);
206 void chown_f(int, long);
207 void clonerange_f(int, long);
208 void copyrange_f(int, long);
209 void creat_f(int, long);
210 void deduperange_f(int, long);
211 void dread_f(int, long);
212 void dwrite_f(int, long);
213 void fallocate_f(int, long);
214 void fdatasync_f(int, long);
215 void fiemap_f(int, long);
216 void freesp_f(int, long);
217 void fsync_f(int, long);
218 char *gen_random_string(int);
219 void getattr_f(int, long);
220 void getdents_f(int, long);
221 void getfattr_f(int, long);
222 void link_f(int, long);
223 void listfattr_f(int, long);
224 void mkdir_f(int, long);
225 void mknod_f(int, long);
226 void mread_f(int, long);
227 void mwrite_f(int, long);
228 void punch_f(int, long);
229 void zero_f(int, long);
230 void collapse_f(int, long);
231 void insert_f(int, long);
232 void read_f(int, long);
233 void readlink_f(int, long);
234 void readv_f(int, long);
235 void removefattr_f(int, long);
236 void rename_f(int, long);
237 void rnoreplace_f(int, long);
238 void rwhiteout_f(int, long);
239 void resvsp_f(int, long);
240 void rmdir_f(int, long);
241 void setattr_f(int, long);
242 void setfattr_f(int, long);
243 void setxattr_f(int, long);
244 void splice_f(int, long);
245 void stat_f(int, long);
246 void symlink_f(int, long);
247 void sync_f(int, long);
248 void truncate_f(int, long);
249 void unlink_f(int, long);
250 void unresvsp_f(int, long);
251 void write_f(int, long);
252 void writev_f(int, long);
253 char *xattr_flag_to_string(int);
256 /* { OP_ENUM, "name", function, freq, iswrite }, */
257 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
258 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
259 { OP_AREAD, "aread", aread_f, 1, 0 },
260 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
261 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
262 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
263 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
264 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
265 { OP_CHOWN, "chown", chown_f, 3, 1 },
266 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
267 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
268 { OP_CREAT, "creat", creat_f, 4, 1 },
269 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
270 { OP_DREAD, "dread", dread_f, 4, 0 },
271 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
272 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
273 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
274 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
275 { OP_FREESP, "freesp", freesp_f, 1, 1 },
276 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
277 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
278 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
279 /* get extended attribute */
280 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
281 { OP_LINK, "link", link_f, 1, 1 },
282 /* list extent attributes */
283 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
284 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
285 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
286 { OP_MREAD, "mread", mread_f, 2, 0 },
287 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
288 { OP_PUNCH, "punch", punch_f, 1, 1 },
289 { OP_ZERO, "zero", zero_f, 1, 1 },
290 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
291 { OP_INSERT, "insert", insert_f, 1, 1 },
292 { OP_READ, "read", read_f, 1, 0 },
293 { OP_READLINK, "readlink", readlink_f, 1, 0 },
294 { OP_READV, "readv", readv_f, 1, 0 },
295 /* remove (delete) extended attribute */
296 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
297 { OP_RENAME, "rename", rename_f, 2, 1 },
298 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
299 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
300 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
301 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
302 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
303 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
304 /* set extended attribute */
305 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
306 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
307 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
308 { OP_SPLICE, "splice", splice_f, 1, 1 },
309 { OP_STAT, "stat", stat_f, 1, 0 },
310 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
311 { OP_SYNC, "sync", sync_f, 1, 1 },
312 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
313 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
314 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
315 { OP_WRITE, "write", write_f, 4, 1 },
316 { OP_WRITEV, "writev", writev_f, 4, 1 },
319 flist_t flist[FT_nft] = {
332 struct xfs_fsop_geom geom;
343 unsigned int idmodulo = XFS_IDMODULO_MAX;
344 unsigned int attr_mask = ~0;
347 unsigned long seed = 0;
351 int verifiable_log = 0;
352 sig_atomic_t should_stop = 0;
353 sigjmp_buf *sigbus_jmp = NULL;
354 char *execute_cmd = NULL;
355 int execute_freq = 1;
356 struct print_string flag_str = {0};
358 void add_to_flist(int, int, int, int);
359 void append_pathname(pathname_t *, char *);
360 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
361 int attr_remove_path(pathname_t *, const char *, int);
362 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
363 void check_cwd(void);
364 void cleanup_flist(void);
365 int creat_path(pathname_t *, mode_t);
366 void dcache_enter(int, int);
367 void dcache_init(void);
368 fent_t *dcache_lookup(int);
369 void dcache_purge(int);
370 void del_from_flist(int, int);
371 int dirid_to_name(char *, int);
373 int fent_to_name(pathname_t *, flist_t *, fent_t *);
374 void fix_parent(int, int);
375 void free_pathname(pathname_t *);
376 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
377 int generate_xattr_name(int, char *, int);
378 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
379 void init_pathname(pathname_t *);
380 int lchown_path(pathname_t *, uid_t, gid_t);
381 int link_path(pathname_t *, pathname_t *);
382 int lstat64_path(pathname_t *, struct stat64 *);
383 void make_freq_table(void);
384 int mkdir_path(pathname_t *, mode_t);
385 int mknod_path(pathname_t *, mode_t, dev_t);
386 void namerandpad(int, char *, int);
387 int open_file_or_dir(pathname_t *, int);
388 int open_path(pathname_t *, int);
389 DIR *opendir_path(pathname_t *);
390 void process_freq(char *);
391 int readlink_path(pathname_t *, char *, size_t);
392 int rename_path(pathname_t *, pathname_t *, int);
393 int rmdir_path(pathname_t *);
394 void separate_pathname(pathname_t *, char *, pathname_t *);
395 void show_ops(int, char *);
396 int stat64_path(pathname_t *, struct stat64 *);
397 int symlink_path(const char *, pathname_t *);
398 int truncate64_path(pathname_t *, off64_t);
399 int unlink_path(pathname_t *);
401 void write_freq(void);
402 void zero_freq(void);
404 void sg_handler(int signum)
412 * Only handle SIGBUS when mmap write to a hole and no
413 * block can be allocated due to ENOSPC, abort otherwise.
416 siglongjmp(*sigbus_jmp, -1);
418 printf("Unknown SIGBUS is caught, Abort!\n");
421 /* should not reach here */
428 int main(int argc, char **argv)
432 char *dirname = NULL;
433 char *logname = NULL;
434 char rpath[PATH_MAX];
443 xfs_error_injection_t err_inj;
444 struct sigaction action;
446 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
448 errrange = errtag = 0;
450 nops = sizeof(ops) / sizeof(ops[0]);
451 ops_end = &ops[nops];
453 while ((c = getopt(argc, argv, allopts)) != -1) {
462 sscanf(optarg, "%d", &errtag);
466 } else if (errtag == 0)
468 if (errtag >= XFS_ERRTAG_MAX) {
470 "error tag %d too large (max %d)\n",
471 errtag, XFS_ERRTAG_MAX - 1);
476 process_freq(optarg);
479 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
480 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
483 idmodulo = strtoul(optarg, NULL, 0);
484 if (idmodulo > XFS_IDMODULO_MAX) {
486 "chown modulo %d too big (max %d)\n",
487 idmodulo, XFS_IDMODULO_MAX);
492 loops = atoi(optarg);
495 operations = atoi(optarg);
502 nproc = atoi(optarg);
508 seed = strtoul(optarg, NULL, 0);
517 execute_cmd = optarg;
523 attr_mask = strtoul(optarg, NULL, 0);
527 if (optarg[0] == 'c')
538 execute_freq = strtoul(optarg, NULL, 0);
541 fprintf(stderr, "%s - invalid parameters\n",
551 /* no directory specified */
552 if (!nousage) usage();
556 (void)mkdir(dirname, 0777);
557 if (logname && logname[0] != '/') {
558 if (!getcwd(rpath, sizeof(rpath))){
559 perror("getcwd failed");
565 if (chdir(dirname) < 0) {
570 char path[PATH_MAX + NAME_MAX + 1];
571 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
572 if (freopen(path, "a", stdout) == NULL) {
573 perror("freopen logfile failed");
577 sprintf(buf, "fss%x", (unsigned int)getpid());
578 fd = creat(buf, 0666);
579 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
580 maxfsize = (off64_t)MAXFSIZE32;
582 maxfsize = (off64_t)MAXFSIZE;
587 gettimeofday(&t, (void *)NULL);
588 seed = (int)t.tv_sec ^ (int)t.tv_usec;
589 printf("seed = %ld\n", seed);
591 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
592 if (i >= 0 && geom.rtblocks)
593 rtpct = MIN(MAX(geom.rtblocks * 100 /
594 (geom.rtblocks + geom.datablocks), 1), 99);
603 for (i = 0; i < j; i++)
606 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
612 for (i = 0; i < j; i++)
615 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
617 printf("Injecting failure on tag #%d\n", errtag);
618 err_inj.errtag = errtag;
620 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
622 perror("fsstress - XFS_SYSSGI error injection call");
631 action.sa_handler = sg_handler;
632 sigemptyset(&action.sa_mask);
634 if (sigaction(SIGTERM, &action, 0)) {
635 perror("sigaction failed");
639 for (i = 0; i < nproc; i++) {
641 sigemptyset(&action.sa_mask);
642 action.sa_handler = SIG_DFL;
643 if (sigaction(SIGTERM, &action, 0))
645 action.sa_handler = sg_handler;
646 if (sigaction(SIGBUS, &action, 0))
648 #ifdef HAVE_SYS_PRCTL_H
649 prctl(PR_SET_PDEATHSIG, SIGKILL);
650 if (getppid() == 1) /* parent died already? */
654 char path[PATH_MAX + NAME_MAX + 2 + 11];
655 snprintf(path, sizeof(path), "%s/%s.%d",
657 if (freopen(path, "a", stdout) == NULL) {
658 perror("freopen logfile failed");
664 if (io_setup(128, &io_ctx) != 0) {
665 fprintf(stderr, "io_setup failed");
669 for (i = 0; !loops || (i < loops); i++)
672 if(io_destroy(io_ctx) != 0) {
673 fprintf(stderr, "io_destroy failed");
683 while (wait(&stat) > 0 && !should_stop) {
686 action.sa_flags = SA_RESTART;
687 sigaction(SIGTERM, &action, 0);
688 kill(-getpid(), SIGTERM);
689 while (wait(&stat) > 0)
695 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
697 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
699 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
712 add_string(struct print_string *str, const char *add)
714 int len = strlen(add);
719 if (len > (str->max - 1) - str->len) {
720 str->len = str->max - 1;
724 memcpy(str->buffer + str->len, add, len);
726 str->buffer[str->len] = '\0';
732 translate_flags(int flags, const char *delim,
733 const struct print_flags *flag_array)
735 int i, mask, first = 1;
738 if (!flag_str.buffer) {
739 flag_str.buffer = malloc(4096);
743 if (!flag_str.buffer)
746 flag_str.buffer[0] = '\0';
748 for (i = 0; flag_array[i].name && flags; i++) {
749 mask = flag_array[i].mask;
750 if ((flags & mask) != mask)
753 add = flag_array[i].name;
756 add_string(&flag_str, delim);
759 add_string(&flag_str, add);
762 /* Check whether there are any leftover flags. */
768 add_string(&flag_str, delim);
770 ret = snprintf(number, 11, "0x%x", flags) > 0;
771 if (ret > 0 && ret <= 11)
772 add_string(&flag_str, number);
775 return flag_str.buffer;
779 add_to_flist(int ft, int id, int parent, int xattr_counter)
785 if (ftp->nfiles == ftp->nslots) {
786 ftp->nslots += FLIST_SLOT_INCR;
787 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
789 fep = &ftp->fents[ftp->nfiles++];
791 fep->parent = parent;
792 fep->xattr_counter = xattr_counter;
796 append_pathname(pathname_t *name, char *str)
802 /* attempting to append to a dir a zero length path */
803 if (len && *str == '/' && name->len == 0) {
804 fprintf(stderr, "fsstress: append_pathname failure\n");
805 assert(chdir(homedir) == 0);
810 name->path = realloc(name->path, name->len + 1 + len);
811 strcpy(&name->path[name->len], str);
816 attr_list_path(pathname_t *name,
818 const int buffersize,
820 attrlist_cursor_t *cursor)
822 char buf[NAME_MAX + 1];
826 if (flags != ATTR_DONTFOLLOW) {
831 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
832 if (rval >= 0 || errno != ENAMETOOLONG)
834 separate_pathname(name, buf, &newname);
835 if (chdir(buf) == 0) {
836 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
837 assert(chdir("..") == 0);
839 free_pathname(&newname);
844 attr_remove_path(pathname_t *name, const char *attrname, int flags)
846 char buf[NAME_MAX + 1];
850 rval = attr_remove(name->path, attrname, flags);
851 if (rval >= 0 || errno != ENAMETOOLONG)
853 separate_pathname(name, buf, &newname);
854 if (chdir(buf) == 0) {
855 rval = attr_remove_path(&newname, attrname, flags);
856 assert(chdir("..") == 0);
858 free_pathname(&newname);
863 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
864 const int valuelength, int flags)
866 char buf[NAME_MAX + 1];
870 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
871 if (rval >= 0 || errno != ENAMETOOLONG)
873 separate_pathname(name, buf, &newname);
874 if (chdir(buf) == 0) {
875 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
877 assert(chdir("..") == 0);
879 free_pathname(&newname);
887 struct stat64 statbuf;
889 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
891 assert(chdir(homedir) == 0);
892 fprintf(stderr, "fsstress: check_cwd failure\n");
899 * go thru flist and release all entries
901 * NOTE: this function shouldn't be called until the end of a process
909 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
918 creat_path(pathname_t *name, mode_t mode)
920 char buf[NAME_MAX + 1];
924 rval = creat(name->path, mode);
925 if (rval >= 0 || errno != ENAMETOOLONG)
927 separate_pathname(name, buf, &newname);
928 if (chdir(buf) == 0) {
929 rval = creat_path(&newname, mode);
930 assert(chdir("..") == 0);
932 free_pathname(&newname);
937 dcache_enter(int dirid, int slot)
939 dcache[dirid % NDCACHE] = slot;
947 for (i = 0; i < NDCACHE; i++)
952 dcache_lookup(int dirid)
957 i = dcache[dirid % NDCACHE];
958 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
964 dcache_purge(int dirid)
968 dcp = &dcache[dirid % NDCACHE];
969 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
974 * Delete the item from the list by
975 * moving last entry over the deleted one;
976 * unless deleted entry is the last one.
977 * Input: which file list array and which slot in array
980 del_from_flist(int ft, int slot)
986 dcache_purge(ftp->fents[slot].id);
987 if (slot != ftp->nfiles - 1) {
989 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
990 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
996 dirid_to_fent(int dirid)
1002 if ((fep = dcache_lookup(dirid)))
1004 flp = &flist[FT_DIR];
1005 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1006 if (fep->id == dirid) {
1007 dcache_enter(dirid, fep - flp->fents);
1017 struct stat64 statbuf;
1025 dividend = (operations + execute_freq) / (execute_freq + 1);
1026 sprintf(buf, "p%x", procid);
1027 (void)mkdir(buf, 0777);
1028 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1032 top_ino = statbuf.st_ino;
1033 homedir = getcwd(NULL, 0);
1035 perror("getcwd failed");
1041 namerand = random();
1042 for (opno = 0; opno < operations; opno++) {
1043 if (execute_cmd && opno && opno % dividend == 0) {
1045 printf("%d: execute command %s\n", opno,
1047 rval = system(execute_cmd);
1049 fprintf(stderr, "execute command failed with "
1052 p = &ops[freq_table[random() % freq_table_size]];
1053 p->func(opno, random());
1055 * test for forced shutdown by stat'ing the test
1056 * directory. If this stat returns EIO, assume
1057 * the forced shutdown happened.
1059 if (errtag != 0 && opno % 100 == 0) {
1060 rval = stat64(".", &statbuf);
1062 fprintf(stderr, "Detected EIO\n");
1068 assert(chdir("..") == 0);
1073 sprintf(cmd, "rm -rf %s", buf);
1076 perror("cleaning up");
1082 * build up a pathname going thru the file entry and all
1083 * its parent entries
1084 * Return 0 on error, 1 on success;
1087 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1089 char buf[NAME_MAX + 1];
1097 /* build up parent directory name */
1098 if (fep->parent != -1) {
1099 pfep = dirid_to_fent(fep->parent);
1102 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1103 procid, fep->id, fep->parent);
1108 e = fent_to_name(name, &flist[FT_DIR], pfep);
1111 append_pathname(name, "/");
1114 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1115 namerandpad(fep->id, buf, i);
1116 append_pathname(name, buf);
1121 fix_parent(int oldid, int newid)
1128 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1129 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1130 if (fep->parent == oldid)
1131 fep->parent = newid;
1137 free_pathname(pathname_t *name)
1147 * Generate a filename of type ft.
1148 * If we have a fep which should be a directory then use it
1149 * as the parent path for this new filename i.e. prepend with it.
1152 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1154 char buf[NAME_MAX + 1];
1163 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1164 namerandpad(id, buf, len);
1166 /* prepend fep parent dir-name to it */
1168 e = fent_to_name(name, &flist[FT_DIR], fep);
1171 append_pathname(name, "/");
1173 append_pathname(name, buf);
1177 for (j = 0; !*v && j < ilistlen; j++) {
1178 if (ilist[j] == id) {
1186 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1190 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1200 * Input: "which" to choose the file-types eg. non-directory
1201 * Input: "r" to choose which file
1202 * Output: file-list, file-entry, name for the chosen file.
1203 * Output: verbose if chosen file is on the ilist.
1206 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1209 int totalsum = 0; /* total number of matching files */
1210 int partialsum = 0; /* partial sum of matching files */
1216 int e = 1; /* success */
1219 * go thru flist and add up number of files for each
1220 * category that matches with <which>.
1222 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1223 if (which & (1 << i))
1224 totalsum += flp->nfiles;
1226 if (totalsum == 0) {
1236 * Now we have possible matches between 0..totalsum-1.
1237 * And we use r to help us choose which one we want,
1238 * which when bounded by totalsum becomes x.
1240 x = (int)(r % totalsum);
1241 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1242 if (which & (1 << i)) {
1243 if (x < partialsum + flp->nfiles) {
1245 /* found the matching file entry */
1246 fep = &flp->fents[x - partialsum];
1248 /* fill-in what we were asked for */
1250 e = fent_to_name(name, flp, fep);
1253 fprintf(stderr, "%d: failed to get path for entry:"
1254 " id=%d,parent=%d\n",
1255 procid, fep->id, fep->parent);
1264 /* turn on verbose if its an ilisted file */
1266 for (j = 0; !*v && j < ilistlen; j++) {
1267 if (ilist[j] == fep->id) {
1274 partialsum += flp->nfiles;
1278 fprintf(stderr, "fsstress: get_fname failure\n");
1285 init_pathname(pathname_t *name)
1292 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1294 char buf[NAME_MAX + 1];
1298 rval = lchown(name->path, owner, group);
1299 if (rval >= 0 || errno != ENAMETOOLONG)
1301 separate_pathname(name, buf, &newname);
1302 if (chdir(buf) == 0) {
1303 rval = lchown_path(&newname, owner, group);
1304 assert(chdir("..") == 0);
1306 free_pathname(&newname);
1311 link_path(pathname_t *name1, pathname_t *name2)
1313 char buf1[NAME_MAX + 1];
1314 char buf2[NAME_MAX + 1];
1316 pathname_t newname1;
1317 pathname_t newname2;
1320 rval = link(name1->path, name2->path);
1321 if (rval >= 0 || errno != ENAMETOOLONG)
1323 separate_pathname(name1, buf1, &newname1);
1324 separate_pathname(name2, buf2, &newname2);
1325 if (strcmp(buf1, buf2) == 0) {
1326 if (chdir(buf1) == 0) {
1327 rval = link_path(&newname1, &newname2);
1328 assert(chdir("..") == 0);
1331 if (strcmp(buf1, "..") == 0)
1333 else if (strcmp(buf2, "..") == 0)
1335 else if (strlen(buf1) == 0)
1337 else if (strlen(buf2) == 0)
1340 down1 = MAX(newname1.len, 3 + name2->len) <=
1341 MAX(3 + name1->len, newname2.len);
1343 free_pathname(&newname2);
1344 append_pathname(&newname2, "../");
1345 append_pathname(&newname2, name2->path);
1346 if (chdir(buf1) == 0) {
1347 rval = link_path(&newname1, &newname2);
1348 assert(chdir("..") == 0);
1351 free_pathname(&newname1);
1352 append_pathname(&newname1, "../");
1353 append_pathname(&newname1, name1->path);
1354 if (chdir(buf2) == 0) {
1355 rval = link_path(&newname1, &newname2);
1356 assert(chdir("..") == 0);
1360 free_pathname(&newname1);
1361 free_pathname(&newname2);
1366 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1368 char buf[NAME_MAX + 1];
1372 rval = lstat64(name->path, sbuf);
1373 if (rval >= 0 || errno != ENAMETOOLONG)
1375 separate_pathname(name, buf, &newname);
1376 if (chdir(buf) == 0) {
1377 rval = lstat64_path(&newname, sbuf);
1378 assert(chdir("..") == 0);
1380 free_pathname(&newname);
1385 make_freq_table(void)
1391 for (p = ops, f = 0; p < ops_end; p++)
1393 freq_table = malloc(f * sizeof(*freq_table));
1394 freq_table_size = f;
1395 for (p = ops, i = 0; p < ops_end; p++) {
1396 for (f = 0; f < p->freq; f++, i++)
1397 freq_table[i] = p->op;
1402 mkdir_path(pathname_t *name, mode_t mode)
1404 char buf[NAME_MAX + 1];
1408 rval = mkdir(name->path, mode);
1409 if (rval >= 0 || errno != ENAMETOOLONG)
1411 separate_pathname(name, buf, &newname);
1412 if (chdir(buf) == 0) {
1413 rval = mkdir_path(&newname, mode);
1414 assert(chdir("..") == 0);
1416 free_pathname(&newname);
1421 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1423 char buf[NAME_MAX + 1];
1427 rval = mknod(name->path, mode, dev);
1428 if (rval >= 0 || errno != ENAMETOOLONG)
1430 separate_pathname(name, buf, &newname);
1431 if (chdir(buf) == 0) {
1432 rval = mknod_path(&newname, mode, dev);
1433 assert(chdir("..") == 0);
1435 free_pathname(&newname);
1440 namerandpad(int id, char *buf, int i)
1443 static int buckets[] =
1444 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1450 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1451 padmod = buckets[bucket] + 1 - i;
1454 padlen = (id ^ namerand) % padmod;
1456 memset(&buf[i], 'X', padlen);
1457 buf[i + padlen] = '\0';
1462 open_file_or_dir(pathname_t *name, int flags)
1466 fd = open_path(name, flags);
1469 if (fd == -1 && errno != EISDIR)
1471 /* Directories can not be opened in write mode nor direct mode. */
1472 flags &= ~(O_WRONLY | O_DIRECT);
1473 flags |= O_RDONLY | O_DIRECTORY;
1474 return open_path(name, flags);
1478 open_path(pathname_t *name, int oflag)
1480 char buf[NAME_MAX + 1];
1484 rval = open(name->path, oflag);
1485 if (rval >= 0 || errno != ENAMETOOLONG)
1487 separate_pathname(name, buf, &newname);
1488 if (chdir(buf) == 0) {
1489 rval = open_path(&newname, oflag);
1490 assert(chdir("..") == 0);
1492 free_pathname(&newname);
1497 opendir_path(pathname_t *name)
1499 char buf[NAME_MAX + 1];
1503 rval = opendir(name->path);
1504 if (rval || errno != ENAMETOOLONG)
1506 separate_pathname(name, buf, &newname);
1507 if (chdir(buf) == 0) {
1508 rval = opendir_path(&newname);
1509 assert(chdir("..") == 0);
1511 free_pathname(&newname);
1516 process_freq(char *arg)
1521 s = strchr(arg, '=');
1523 fprintf(stderr, "bad argument '%s'\n", arg);
1527 for (p = ops; p < ops_end; p++) {
1528 if (strcmp(arg, p->name) == 0) {
1533 fprintf(stderr, "can't find op type %s for -f\n", arg);
1538 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1540 char buf[NAME_MAX + 1];
1544 rval = readlink(name->path, lbuf, lbufsiz);
1545 if (rval >= 0 || errno != ENAMETOOLONG)
1547 separate_pathname(name, buf, &newname);
1548 if (chdir(buf) == 0) {
1549 rval = readlink_path(&newname, lbuf, lbufsiz);
1550 assert(chdir("..") == 0);
1552 free_pathname(&newname);
1557 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1559 char buf1[NAME_MAX + 1];
1560 char buf2[NAME_MAX + 1];
1562 pathname_t newname1;
1563 pathname_t newname2;
1567 rval = rename(name1->path, name2->path);
1569 rval = renameat2(AT_FDCWD, name1->path,
1570 AT_FDCWD, name2->path, mode);
1571 if (rval >= 0 || errno != ENAMETOOLONG)
1573 separate_pathname(name1, buf1, &newname1);
1574 separate_pathname(name2, buf2, &newname2);
1575 if (strcmp(buf1, buf2) == 0) {
1576 if (chdir(buf1) == 0) {
1577 rval = rename_path(&newname1, &newname2, mode);
1578 assert(chdir("..") == 0);
1581 if (strcmp(buf1, "..") == 0)
1583 else if (strcmp(buf2, "..") == 0)
1585 else if (strlen(buf1) == 0)
1587 else if (strlen(buf2) == 0)
1590 down1 = MAX(newname1.len, 3 + name2->len) <=
1591 MAX(3 + name1->len, newname2.len);
1593 free_pathname(&newname2);
1594 append_pathname(&newname2, "../");
1595 append_pathname(&newname2, name2->path);
1596 if (chdir(buf1) == 0) {
1597 rval = rename_path(&newname1, &newname2, mode);
1598 assert(chdir("..") == 0);
1601 free_pathname(&newname1);
1602 append_pathname(&newname1, "../");
1603 append_pathname(&newname1, name1->path);
1604 if (chdir(buf2) == 0) {
1605 rval = rename_path(&newname1, &newname2, mode);
1606 assert(chdir("..") == 0);
1610 free_pathname(&newname1);
1611 free_pathname(&newname2);
1616 rmdir_path(pathname_t *name)
1618 char buf[NAME_MAX + 1];
1622 rval = rmdir(name->path);
1623 if (rval >= 0 || errno != ENAMETOOLONG)
1625 separate_pathname(name, buf, &newname);
1626 if (chdir(buf) == 0) {
1627 rval = rmdir_path(&newname);
1628 assert(chdir("..") == 0);
1630 free_pathname(&newname);
1635 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1639 init_pathname(newname);
1640 slash = strchr(name->path, '/');
1641 if (slash == NULL) {
1646 strcpy(buf, name->path);
1648 append_pathname(newname, slash + 1);
1654 show_ops(int flag, char *lead_str)
1659 /* print in list form */
1662 for (p = ops; p < ops_end; p++) {
1663 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1664 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1665 x+=printf("%s ", p->name);
1668 } else if (flag == 0) {
1669 /* Table view style */
1671 for (f = 0, p = ops; p < ops_end; p++)
1677 for (p = ops; p < ops_end; p++) {
1678 if (flag != 0 || p->freq > 0) {
1679 if (lead_str != NULL)
1680 printf("%s", lead_str);
1681 printf("%20s %d/%d %s\n",
1682 p->name, p->freq, f,
1683 (p->iswrite == 0) ? " " : "write op");
1687 /* Command line style */
1688 if (lead_str != NULL)
1689 printf("%s", lead_str);
1690 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1692 for (p = ops; p < ops_end; p++)
1694 printf("-f %s=%d \\\n",p->name, p->freq);
1699 stat64_path(pathname_t *name, struct stat64 *sbuf)
1701 char buf[NAME_MAX + 1];
1705 rval = stat64(name->path, sbuf);
1706 if (rval >= 0 || errno != ENAMETOOLONG)
1708 separate_pathname(name, buf, &newname);
1709 if (chdir(buf) == 0) {
1710 rval = stat64_path(&newname, sbuf);
1711 assert(chdir("..") == 0);
1713 free_pathname(&newname);
1718 symlink_path(const char *name1, pathname_t *name)
1720 char buf[NAME_MAX + 1];
1724 if (!strcmp(name1, name->path)) {
1725 printf("yikes! %s %s\n", name1, name->path);
1729 rval = symlink(name1, name->path);
1730 if (rval >= 0 || errno != ENAMETOOLONG)
1732 separate_pathname(name, buf, &newname);
1733 if (chdir(buf) == 0) {
1734 rval = symlink_path(name1, &newname);
1735 assert(chdir("..") == 0);
1737 free_pathname(&newname);
1742 truncate64_path(pathname_t *name, off64_t length)
1744 char buf[NAME_MAX + 1];
1748 rval = truncate64(name->path, length);
1749 if (rval >= 0 || errno != ENAMETOOLONG)
1751 separate_pathname(name, buf, &newname);
1752 if (chdir(buf) == 0) {
1753 rval = truncate64_path(&newname, length);
1754 assert(chdir("..") == 0);
1756 free_pathname(&newname);
1761 unlink_path(pathname_t *name)
1763 char buf[NAME_MAX + 1];
1767 rval = unlink(name->path);
1768 if (rval >= 0 || errno != ENAMETOOLONG)
1770 separate_pathname(name, buf, &newname);
1771 if (chdir(buf) == 0) {
1772 rval = unlink_path(&newname);
1773 assert(chdir("..") == 0);
1775 free_pathname(&newname);
1782 printf("Usage: %s -H or\n", myprog);
1783 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1785 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1787 printf(" -c clean up the test directory after each run\n");
1788 printf(" -d dir specifies the base directory for operations\n");
1789 printf(" -e errtg specifies error injection stuff\n");
1790 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1791 printf(" the valid operation names are:\n");
1793 printf(" -i filenum get verbose output for this nth file object\n");
1794 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1795 printf(" *use 0 for infinite (default 1)\n");
1796 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1797 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1798 printf(" -o logfile specifies logfile name\n");
1799 printf(" -p nproc specifies the no. of processes (default 1)\n");
1800 printf(" -r specifies random name padding\n");
1801 printf(" -s seed specifies the seed for the random generator (default random)\n");
1802 printf(" -v specifies verbose mode\n");
1803 printf(" -w zeros frequencies of non-write operations\n");
1804 printf(" -x cmd execute command in the middle of operations\n");
1805 printf(" -z zeros frequencies of all operations\n");
1806 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1807 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1808 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1809 printf(" -H prints usage and exits\n");
1817 for (p = ops; p < ops_end; p++) {
1828 for (p = ops; p < ops_end; p++)
1832 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1835 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1836 verifiable_log ? -1: (long)s->st_ino,
1837 (long)s->st_nlink, s->st_uid, s->st_gid,
1838 (long long) s->st_blocks, (long long) s->st_size);
1842 afsync_f(int opno, long r)
1850 struct iocb *iocbs[] = { &iocb };
1851 struct io_event event;
1854 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1856 printf("%d/%d: afsync - no filename\n", procid, opno);
1860 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1861 e = fd < 0 ? errno : 0;
1865 printf("%d/%d: afsync - open %s failed %d\n",
1866 procid, opno, f.path, e);
1871 io_prep_fsync(&iocb, fd);
1872 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1874 printf("%d/%d: afsync - io_submit %s %d\n",
1875 procid, opno, f.path, e);
1880 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1882 printf("%d/%d: afsync - io_getevents failed %d\n",
1891 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1898 allocsp_f(int opno, long r)
1903 struct xfs_flock64 fl;
1911 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1913 printf("%d/%d: allocsp - no filename\n", procid, opno);
1917 fd = open_path(&f, O_RDWR);
1918 e = fd < 0 ? errno : 0;
1922 printf("%d/%d: allocsp - open %s failed %d\n",
1923 procid, opno, f.path, e);
1927 if (fstat64(fd, &stb) < 0) {
1929 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1930 procid, opno, f.path, errno);
1935 inode_info(st, sizeof(st), &stb, v);
1936 lr = ((int64_t)random() << 32) + random();
1937 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1939 fl.l_whence = SEEK_SET;
1942 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1944 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1945 procid, opno, f.path, st, (long long)off, e);
1953 do_aio_rw(int opno, long r, int flags)
1957 struct dioattr diob;
1969 struct io_event event;
1970 struct iocb *iocbs[] = { &iocb };
1971 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1974 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1976 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1980 fd = open_path(&f, flags|O_DIRECT);
1981 e = fd < 0 ? errno : 0;
1985 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1986 procid, opno, f.path, e);
1990 if (fstat64(fd, &stb) < 0) {
1992 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1993 procid, opno, f.path, errno);
1998 inode_info(st, sizeof(st), &stb, v);
1999 if (!iswrite && stb.st_size == 0) {
2001 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2007 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2010 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2011 " fallback to stat()\n",
2012 procid, opno, f.path, st, errno);
2013 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2014 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2016 dio_env = getenv("XFS_DIO_MIN");
2018 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2019 align = (int64_t)diob.d_miniosz;
2020 lr = ((int64_t)random() << 32) + random();
2021 len = (random() % FILELEN_MAX) + 1;
2022 len -= (len % align);
2025 else if (len > diob.d_maxiosz)
2026 len = diob.d_maxiosz;
2027 buf = memalign(diob.d_mem, len);
2030 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2031 off -= (off % align);
2033 memset(buf, nameseq & 0xff, len);
2034 io_prep_pwrite(&iocb, fd, buf, len, off);
2036 off = (off64_t)(lr % stb.st_size);
2037 off -= (off % align);
2038 io_prep_pread(&iocb, fd, buf, len, off);
2040 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2042 printf("%d/%d: %s - io_submit failed %d\n",
2043 procid, opno, iswrite ? "awrite" : "aread", e);
2048 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2050 printf("%d/%d: %s - io_getevents failed %d\n",
2051 procid, opno, iswrite ? "awrite" : "aread", e);
2057 e = event.res != len ? event.res2 : 0;
2060 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2061 procid, opno, iswrite ? "awrite" : "aread",
2062 f.path, st, (long long)off, (int)len, e);
2069 aread_f(int opno, long r)
2072 do_aio_rw(opno, r, O_RDONLY);
2077 attr_remove_f(int opno, long r)
2079 attrlist_ent_t *aep;
2083 attrlist_cursor_t cursor;
2092 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2093 append_pathname(&f, ".");
2095 bzero(&cursor, sizeof(cursor));
2097 bzero(buf, sizeof(buf));
2098 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2102 alist = (attrlist_t *)buf;
2103 total += alist->al_count;
2104 } while (alist->al_more);
2107 printf("%d/%d: attr_remove - no attrs for %s\n",
2108 procid, opno, f.path);
2112 which = (int)(random() % total);
2113 bzero(&cursor, sizeof(cursor));
2117 bzero(buf, sizeof(buf));
2118 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2122 alist = (attrlist_t *)buf;
2123 if (which < ent + alist->al_count) {
2124 aep = (attrlist_ent_t *)
2125 &buf[alist->al_offset[which - ent]];
2126 aname = aep->a_name;
2129 ent += alist->al_count;
2130 } while (alist->al_more);
2131 if (aname == NULL) {
2134 "%d/%d: attr_remove - name %d not found at %s\n",
2135 procid, opno, which, f.path);
2139 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2142 printf("%d/%d: attr_remove %s %s %d\n",
2143 procid, opno, f.path, aname, e);
2148 attr_set_f(int opno, long r)
2155 static int lengths[] = { 10, 100, 1000, 10000 };
2160 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2161 append_pathname(&f, ".");
2162 sprintf(aname, "a%x", nameseq++);
2163 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2164 len = (int)(random() % lengths[li]);
2168 memset(aval, nameseq & 0xff, len);
2169 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2174 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2180 awrite_f(int opno, long r)
2183 do_aio_rw(opno, r, O_WRONLY);
2188 bulkstat_f(int opno, long r)
2194 struct xfs_bstat *t;
2196 struct xfs_fsop_bulkreq bsr;
2199 nent = (r % 999) + 2;
2200 t = malloc(nent * sizeof(*t));
2201 fd = open(".", O_RDONLY);
2209 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2213 printf("%d/%d: bulkstat nent %d total %lld\n",
2214 procid, opno, nent, (long long)total);
2219 bulkstat1_f(int opno, long r)
2229 struct xfs_fsop_bulkreq bsr;
2232 good = random() & 1;
2234 /* use an inode we know exists */
2236 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2237 append_pathname(&f, ".");
2238 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2243 * pick a random inode
2245 * note this can generate kernel warning messages
2246 * since bulkstat_one will read the disk block that
2247 * would contain a given inode even if that disk
2248 * block doesn't contain inodes.
2250 * this is detected later, but not until after the
2251 * warning is displayed.
2253 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2259 fd = open(".", O_RDONLY);
2265 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2267 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2268 procid, opno, good?"real":"random",
2269 verifiable_log ? -1LL : (long long)ino, e);
2274 chown_f(int opno, long r)
2284 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2285 append_pathname(&f, ".");
2286 u = (uid_t)random();
2287 g = (gid_t)random();
2288 nbits = (int)(random() % idmodulo);
2289 u &= (1 << nbits) - 1;
2290 g &= (1 << nbits) - 1;
2291 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2294 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2298 /* reflink some arbitrary range of f1 to f2. */
2305 struct file_clone_range fcr;
2306 struct pathname fpath1;
2307 struct pathname fpath2;
2308 struct stat64 stat1;
2309 struct stat64 stat2;
2310 char inoinfo1[1024];
2311 char inoinfo2[1024];
2325 init_pathname(&fpath1);
2326 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2328 printf("%d/%d: clonerange read - no filename\n",
2333 init_pathname(&fpath2);
2334 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2336 printf("%d/%d: clonerange write - no filename\n",
2342 fd1 = open_path(&fpath1, O_RDONLY);
2343 e = fd1 < 0 ? errno : 0;
2347 printf("%d/%d: clonerange read - open %s failed %d\n",
2348 procid, opno, fpath1.path, e);
2352 fd2 = open_path(&fpath2, O_WRONLY);
2353 e = fd2 < 0 ? errno : 0;
2357 printf("%d/%d: clonerange write - open %s failed %d\n",
2358 procid, opno, fpath2.path, e);
2362 /* Get file stats */
2363 if (fstat64(fd1, &stat1) < 0) {
2365 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2366 procid, opno, fpath1.path, errno);
2369 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2371 if (fstat64(fd2, &stat2) < 0) {
2373 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2374 procid, opno, fpath2.path, errno);
2377 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2379 /* Calculate offsets */
2380 len = (random() % FILELEN_MAX) + 1;
2381 len &= ~(stat1.st_blksize - 1);
2383 len = stat1.st_blksize;
2384 if (len > stat1.st_size)
2385 len = stat1.st_size;
2387 lr = ((int64_t)random() << 32) + random();
2388 if (stat1.st_size == len)
2391 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2393 off1 &= ~(stat1.st_blksize - 1);
2396 * If srcfile == destfile, randomly generate destination ranges
2397 * until we find one that doesn't overlap the source range.
2399 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2401 lr = ((int64_t)random() << 32) + random();
2402 off2 = (off64_t)(lr % max_off2);
2404 off2 &= ~(stat2.st_blksize - 1);
2405 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2407 /* Clone data blocks */
2409 fcr.src_offset = off1;
2410 fcr.src_length = len;
2411 fcr.dest_offset = off2;
2413 ret = ioctl(fd2, FICLONERANGE, &fcr);
2414 e = ret < 0 ? errno : 0;
2416 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2418 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2419 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2422 printf(" error %d", e);
2431 free_pathname(&fpath2);
2433 free_pathname(&fpath1);
2437 /* copy some arbitrary range of f1 to f2. */
2443 #ifdef HAVE_COPY_FILE_RANGE
2444 struct pathname fpath1;
2445 struct pathname fpath2;
2446 struct stat64 stat1;
2447 struct stat64 stat2;
2448 char inoinfo1[1024];
2449 char inoinfo2[1024];
2467 init_pathname(&fpath1);
2468 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2470 printf("%d/%d: copyrange read - no filename\n",
2475 init_pathname(&fpath2);
2476 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2478 printf("%d/%d: copyrange write - no filename\n",
2484 fd1 = open_path(&fpath1, O_RDONLY);
2485 e = fd1 < 0 ? errno : 0;
2489 printf("%d/%d: copyrange read - open %s failed %d\n",
2490 procid, opno, fpath1.path, e);
2494 fd2 = open_path(&fpath2, O_WRONLY);
2495 e = fd2 < 0 ? errno : 0;
2499 printf("%d/%d: copyrange write - open %s failed %d\n",
2500 procid, opno, fpath2.path, e);
2504 /* Get file stats */
2505 if (fstat64(fd1, &stat1) < 0) {
2507 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2508 procid, opno, fpath1.path, errno);
2511 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2513 if (fstat64(fd2, &stat2) < 0) {
2515 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2516 procid, opno, fpath2.path, errno);
2519 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2521 /* Calculate offsets */
2522 len = (random() % FILELEN_MAX) + 1;
2524 len = stat1.st_blksize;
2525 if (len > stat1.st_size)
2526 len = stat1.st_size;
2528 lr = ((int64_t)random() << 32) + random();
2529 if (stat1.st_size == len)
2532 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2536 * If srcfile == destfile, randomly generate destination ranges
2537 * until we find one that doesn't overlap the source range.
2539 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2541 lr = ((int64_t)random() << 32) + random();
2542 off2 = (off64_t)(lr % max_off2);
2544 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2547 * Since len, off1 and off2 will be changed later, preserve their
2555 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2558 if (errno != EAGAIN || tries++ >= 300)
2560 } else if (ret > len || ret == 0)
2565 e = ret < 0 ? errno : 0;
2567 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2569 fpath1.path, inoinfo1,
2570 (long long)offset1, (long long)length,
2571 fpath2.path, inoinfo2,
2572 (long long)offset2, (long long)length);
2575 printf(" error %d", e);
2576 else if (len && ret > len)
2577 printf(" asked for %lld, copied %lld??\n",
2578 (long long)len, (long long)ret);
2587 free_pathname(&fpath2);
2589 free_pathname(&fpath1);
2593 /* dedupe some arbitrary range of f1 to f2...fn. */
2599 #ifdef FIDEDUPERANGE
2600 #define INFO_SZ 1024
2601 struct file_dedupe_range *fdr;
2602 struct pathname *fpath;
2603 struct stat64 *stat;
2615 if (flist[FT_REG].nfiles < 2)
2618 /* Pick somewhere between 2 and 128 files. */
2620 nr = random() % (flist[FT_REG].nfiles + 1);
2621 } while (nr < 2 || nr > 128);
2624 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2625 sizeof(struct file_dedupe_range));
2627 printf("%d/%d: line %d error %d\n",
2628 procid, opno, __LINE__, errno);
2631 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2632 sizeof(struct file_dedupe_range)));
2634 fpath = calloc(nr, sizeof(struct pathname));
2636 printf("%d/%d: line %d error %d\n",
2637 procid, opno, __LINE__, errno);
2641 stat = calloc(nr, sizeof(struct stat64));
2643 printf("%d/%d: line %d error %d\n",
2644 procid, opno, __LINE__, errno);
2648 info = calloc(nr, INFO_SZ);
2650 printf("%d/%d: line %d error %d\n",
2651 procid, opno, __LINE__, errno);
2655 off = calloc(nr, sizeof(off64_t));
2657 printf("%d/%d: line %d error %d\n",
2658 procid, opno, __LINE__, errno);
2662 v = calloc(nr, sizeof(int));
2664 printf("%d/%d: line %d error %d\n",
2665 procid, opno, __LINE__, errno);
2668 fd = calloc(nr, sizeof(int));
2670 printf("%d/%d: line %d error %d\n",
2671 procid, opno, __LINE__, errno);
2674 memset(fd, 0xFF, nr * sizeof(int));
2676 /* Get paths for all files */
2677 for (i = 0; i < nr; i++)
2678 init_pathname(&fpath[i]);
2680 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2682 printf("%d/%d: deduperange read - no filename\n",
2687 for (i = 1; i < nr; i++) {
2688 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2690 printf("%d/%d: deduperange write - no filename\n",
2697 fd[0] = open_path(&fpath[0], O_RDONLY);
2698 e = fd[0] < 0 ? errno : 0;
2702 printf("%d/%d: deduperange read - open %s failed %d\n",
2703 procid, opno, fpath[0].path, e);
2707 for (i = 1; i < nr; i++) {
2708 fd[i] = open_path(&fpath[i], O_WRONLY);
2709 e = fd[i] < 0 ? errno : 0;
2713 printf("%d/%d: deduperange write - open %s failed %d\n",
2714 procid, opno, fpath[i].path, e);
2719 /* Get file stats */
2720 if (fstat64(fd[0], &stat[0]) < 0) {
2722 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2723 procid, opno, fpath[0].path, errno);
2727 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2729 for (i = 1; i < nr; i++) {
2730 if (fstat64(fd[i], &stat[i]) < 0) {
2732 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2733 procid, opno, fpath[i].path, errno);
2736 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2739 /* Never try to dedupe more than half of the src file. */
2740 len = (random() % FILELEN_MAX) + 1;
2741 len &= ~(stat[0].st_blksize - 1);
2743 len = stat[0].st_blksize / 2;
2744 if (len > stat[0].st_size / 2)
2745 len = stat[0].st_size / 2;
2747 /* Calculate offsets */
2748 lr = ((int64_t)random() << 32) + random();
2749 if (stat[0].st_size == len)
2752 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2754 off[0] &= ~(stat[0].st_blksize - 1);
2757 * If srcfile == destfile[i], randomly generate destination ranges
2758 * until we find one that doesn't overlap the source range.
2760 for (i = 1; i < nr; i++) {
2764 lr = ((int64_t)random() << 32) + random();
2765 if (stat[i].st_size <= len)
2768 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2770 off[i] &= ~(stat[i].st_blksize - 1);
2771 } while (stat[0].st_ino == stat[i].st_ino &&
2772 llabs(off[i] - off[0]) < len &&
2776 /* Clone data blocks */
2777 fdr->src_offset = off[0];
2778 fdr->src_length = len;
2779 fdr->dest_count = nr - 1;
2780 for (i = 1; i < nr; i++) {
2781 fdr->info[i - 1].dest_fd = fd[i];
2782 fdr->info[i - 1].dest_offset = off[i];
2785 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2786 e = ret < 0 ? errno : 0;
2788 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2790 fpath[0].path, &info[0], (long long)off[0],
2793 printf(" error %d", e);
2799 for (i = 1; i < nr; i++) {
2800 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2802 printf("%d/%d: ...to %s%s [%lld,%lld]",
2804 fpath[i].path, &info[i * INFO_SZ],
2805 (long long)off[i], (long long)len);
2806 if (fdr->info[i - 1].status < 0)
2807 printf(" error %d", e);
2808 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2809 printf(" %llu bytes deduplicated",
2810 fdr->info[i - 1].bytes_deduped);
2811 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2812 printf(" differed");
2818 for (i = 0; i < nr; i++)
2822 for (i = 0; i < nr; i++)
2823 free_pathname(&fpath[i]);
2842 setxattr_f(int opno, long r)
2844 #ifdef XFS_XFLAG_EXTSIZE
2854 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2855 append_pathname(&f, ".");
2856 fd = open_path(&f, O_RDWR);
2857 e = fd < 0 ? errno : 0;
2862 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2863 nbits = (int)(random() % e);
2864 p &= (1 << nbits) - 1;
2866 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2868 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2871 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2878 splice_f(int opno, long r)
2880 struct pathname fpath1;
2881 struct pathname fpath2;
2882 struct stat64 stat1;
2883 struct stat64 stat2;
2884 char inoinfo1[1024];
2885 char inoinfo2[1024];
2889 loff_t offset1, offset2;
2896 ssize_t ret1 = 0, ret2 = 0;
2902 init_pathname(&fpath1);
2903 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2905 printf("%d/%d: splice read - no filename\n",
2910 init_pathname(&fpath2);
2911 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2913 printf("%d/%d: splice write - no filename\n",
2919 fd1 = open_path(&fpath1, O_RDONLY);
2920 e = fd1 < 0 ? errno : 0;
2924 printf("%d/%d: splice read - open %s failed %d\n",
2925 procid, opno, fpath1.path, e);
2929 fd2 = open_path(&fpath2, O_WRONLY);
2930 e = fd2 < 0 ? errno : 0;
2934 printf("%d/%d: splice write - open %s failed %d\n",
2935 procid, opno, fpath2.path, e);
2939 /* Get file stats */
2940 if (fstat64(fd1, &stat1) < 0) {
2942 printf("%d/%d: splice read - fstat64 %s failed %d\n",
2943 procid, opno, fpath1.path, errno);
2946 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2948 if (fstat64(fd2, &stat2) < 0) {
2950 printf("%d/%d: splice write - fstat64 %s failed %d\n",
2951 procid, opno, fpath2.path, errno);
2954 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2956 /* Calculate offsets */
2957 len = (random() % FILELEN_MAX) + 1;
2959 len = stat1.st_blksize;
2960 if (len > stat1.st_size)
2961 len = stat1.st_size;
2963 lr = ((int64_t)random() << 32) + random();
2964 if (stat1.st_size == len)
2967 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2971 * splice can overlap write, so the offset of the target file can be
2972 * any number. But to avoid too large offset, add a clamp of 1024 blocks
2973 * past the current dest file EOF
2975 lr = ((int64_t)random() << 32) + random();
2976 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
2979 * Since len, off1 and off2 will be changed later, preserve their
2986 /* Pipe initialize */
2987 if (pipe(filedes) < 0) {
2989 printf("%d/%d: splice - pipe failed %d\n",
2990 procid, opno, errno);
2998 /* move to pipe buffer */
2999 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3005 /* move from pipe buffer to dst file */
3007 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3020 if (ret1 < 0 || ret2 < 0)
3025 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3027 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3028 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3030 if (length && length > total)
3031 printf(" asked for %lld, spliced %lld??\n",
3032 (long long)length, (long long)total);
3043 free_pathname(&fpath2);
3045 free_pathname(&fpath1);
3049 creat_f(int opno, long r)
3064 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
3069 e1 = (random() % 100);
3070 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3072 if (type == FT_RTF) /* rt always gets an extsize */
3073 extsize = (random() % 10) + 1;
3074 else if (e1 < 10) /* one-in-ten get an extsize */
3075 extsize = random() % 1024;
3079 e = generate_fname(fep, type, &f, &id, &v);
3083 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3084 printf("%d/%d: creat - no filename from %s\n",
3085 procid, opno, f.path);
3090 fd = creat_path(&f, 0666);
3091 e = fd < 0 ? errno : 0;
3096 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3097 if (type == FT_RTF) {
3098 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3099 a.fsx_extsize = extsize *
3100 geom.rtextsize * geom.blocksize;
3102 } else if (extsize) {
3103 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3104 a.fsx_extsize = extsize * geom.blocksize;
3107 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3110 add_to_flist(type, id, parid, 0);
3114 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3115 extsize ? a.fsx_extsize : 0, e, e1);
3116 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3122 dread_f(int opno, long r)
3126 struct dioattr diob;
3139 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3141 printf("%d/%d: dread - no filename\n", procid, opno);
3145 fd = open_path(&f, O_RDONLY|O_DIRECT);
3146 e = fd < 0 ? errno : 0;
3150 printf("%d/%d: dread - open %s failed %d\n",
3151 procid, opno, f.path, e);
3155 if (fstat64(fd, &stb) < 0) {
3157 printf("%d/%d: dread - fstat64 %s failed %d\n",
3158 procid, opno, f.path, errno);
3163 inode_info(st, sizeof(st), &stb, v);
3164 if (stb.st_size == 0) {
3166 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3172 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3175 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3176 " fallback to stat()\n",
3177 procid, opno, f.path, st, errno);
3178 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3179 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3182 dio_env = getenv("XFS_DIO_MIN");
3184 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3186 align = (int64_t)diob.d_miniosz;
3187 lr = ((int64_t)random() << 32) + random();
3188 off = (off64_t)(lr % stb.st_size);
3189 off -= (off % align);
3190 lseek64(fd, off, SEEK_SET);
3191 len = (random() % FILELEN_MAX) + 1;
3192 len -= (len % align);
3195 else if (len > diob.d_maxiosz)
3196 len = diob.d_maxiosz;
3197 buf = memalign(diob.d_mem, len);
3198 e = read(fd, buf, len) < 0 ? errno : 0;
3201 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3202 procid, opno, f.path, st, (long long)off, (int)len, e);
3208 dwrite_f(int opno, long r)
3212 struct dioattr diob;
3225 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3227 printf("%d/%d: dwrite - no filename\n", procid, opno);
3231 fd = open_path(&f, O_WRONLY|O_DIRECT);
3232 e = fd < 0 ? errno : 0;
3236 printf("%d/%d: dwrite - open %s failed %d\n",
3237 procid, opno, f.path, e);
3241 if (fstat64(fd, &stb) < 0) {
3243 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3244 procid, opno, f.path, errno);
3249 inode_info(st, sizeof(st), &stb, v);
3250 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3252 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3253 " %s%s return %d, fallback to stat()\n",
3254 procid, opno, f.path, st, errno);
3255 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3256 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3259 dio_env = getenv("XFS_DIO_MIN");
3261 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3263 align = (int64_t)diob.d_miniosz;
3264 lr = ((int64_t)random() << 32) + random();
3265 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3266 off -= (off % align);
3267 lseek64(fd, off, SEEK_SET);
3268 len = (random() % FILELEN_MAX) + 1;
3269 len -= (len % align);
3272 else if (len > diob.d_maxiosz)
3273 len = diob.d_maxiosz;
3274 buf = memalign(diob.d_mem, len);
3276 lseek64(fd, off, SEEK_SET);
3277 memset(buf, nameseq & 0xff, len);
3278 e = write(fd, buf, len) < 0 ? errno : 0;
3281 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3282 procid, opno, f.path, st, (long long)off, (int)len, e);
3288 #ifdef HAVE_LINUX_FALLOC_H
3289 struct print_flags falloc_flags [] = {
3290 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3291 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3292 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3293 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3294 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3295 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3299 #define translate_falloc_flags(mode) \
3300 ({translate_flags(mode, "|", falloc_flags);})
3304 do_fallocate(int opno, long r, int mode)
3306 #ifdef HAVE_LINUX_FALLOC_H
3318 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3320 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3324 fd = open_path(&f, O_RDWR);
3327 printf("%d/%d: do_fallocate - open %s failed %d\n",
3328 procid, opno, f.path, errno);
3333 if (fstat64(fd, &stb) < 0) {
3335 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3336 procid, opno, f.path, errno);
3341 inode_info(st, sizeof(st), &stb, v);
3342 lr = ((int64_t)random() << 32) + random();
3343 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3345 len = (off64_t)(random() % (1024 * 1024));
3347 * Collapse/insert range requires off and len to be block aligned,
3348 * make it more likely to be the case.
3350 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3352 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3353 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3355 mode |= FALLOC_FL_KEEP_SIZE & random();
3356 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3358 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3359 procid, opno, translate_falloc_flags(mode),
3360 f.path, st, (long long)off, (long long)len, e);
3367 fallocate_f(int opno, long r)
3369 #ifdef HAVE_LINUX_FALLOC_H
3370 do_fallocate(opno, r, 0);
3375 fdatasync_f(int opno, long r)
3383 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3385 printf("%d/%d: fdatasync - no filename\n",
3390 fd = open_path(&f, O_WRONLY);
3391 e = fd < 0 ? errno : 0;
3395 printf("%d/%d: fdatasync - open %s failed %d\n",
3396 procid, opno, f.path, e);
3400 e = fdatasync(fd) < 0 ? errno : 0;
3402 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3407 #ifdef HAVE_LINUX_FIEMAP_H
3408 struct print_flags fiemap_flags[] = {
3409 { FIEMAP_FLAG_SYNC, "SYNC"},
3410 { FIEMAP_FLAG_XATTR, "XATTR"},
3414 #define translate_fiemap_flags(mode) \
3415 ({translate_flags(mode, "|", fiemap_flags);})
3419 fiemap_f(int opno, long r)
3421 #ifdef HAVE_LINUX_FIEMAP_H
3431 struct fiemap *fiemap;
3434 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3436 printf("%d/%d: fiemap - no filename\n", procid, opno);
3440 fd = open_path(&f, O_RDWR);
3441 e = fd < 0 ? errno : 0;
3445 printf("%d/%d: fiemap - open %s failed %d\n",
3446 procid, opno, f.path, e);
3450 if (fstat64(fd, &stb) < 0) {
3452 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3453 procid, opno, f.path, errno);
3458 inode_info(st, sizeof(st), &stb, v);
3459 blocks_to_map = random() & 0xffff;
3460 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3461 (blocks_to_map * sizeof(struct fiemap_extent)));
3464 printf("%d/%d: malloc failed \n", procid, opno);
3469 lr = ((int64_t)random() << 32) + random();
3470 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3472 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3473 fiemap->fm_extent_count = blocks_to_map;
3474 fiemap->fm_mapped_extents = random() & 0xffff;
3475 fiemap->fm_start = off;
3476 fiemap->fm_length = ((int64_t)random() << 32) + random();
3478 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3480 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3481 procid, opno, f.path, st, (long long)fiemap->fm_start,
3482 (long long) fiemap->fm_length,
3483 translate_fiemap_flags(fiemap->fm_flags), e);
3491 freesp_f(int opno, long r)
3496 struct xfs_flock64 fl;
3504 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3506 printf("%d/%d: freesp - no filename\n", procid, opno);
3510 fd = open_path(&f, O_RDWR);
3511 e = fd < 0 ? errno : 0;
3515 printf("%d/%d: freesp - open %s failed %d\n",
3516 procid, opno, f.path, e);
3520 if (fstat64(fd, &stb) < 0) {
3522 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3523 procid, opno, f.path, errno);
3528 inode_info(st, sizeof(st), &stb, v);
3529 lr = ((int64_t)random() << 32) + random();
3530 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3532 fl.l_whence = SEEK_SET;
3535 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3537 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3538 procid, opno, f.path, st, (long long)off, e);
3544 fsync_f(int opno, long r)
3552 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3554 printf("%d/%d: fsync - no filename\n", procid, opno);
3558 fd = open_file_or_dir(&f, O_WRONLY);
3559 e = fd < 0 ? errno : 0;
3563 printf("%d/%d: fsync - open %s failed %d\n",
3564 procid, opno, f.path, e);
3568 e = fsync(fd) < 0 ? errno : 0;
3570 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3576 gen_random_string(int len)
3578 static const char charset[] = "0123456789"
3579 "abcdefghijklmnopqrstuvwxyz"
3580 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3591 for (i = 0; i < len; i++)
3592 s[i] = charset[random() % sizeof(charset)];
3598 getattr_f(int opno, long r)
3607 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3608 append_pathname(&f, ".");
3609 fd = open_path(&f, O_RDWR);
3610 e = fd < 0 ? errno : 0;
3613 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3615 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3621 getdents_f(int opno, long r)
3628 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3629 append_pathname(&f, ".");
3630 dir = opendir_path(&f);
3634 printf("%d/%d: getdents - can't open %s\n",
3635 procid, opno, f.path);
3639 while (readdir64(dir) != NULL)
3642 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3648 getfattr_f(int opno, long r)
3654 char name[XATTR_NAME_BUF_SIZE];
3660 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3662 printf("%d/%d: getfattr - no filename\n", procid, opno);
3668 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3669 * to read a xattr that doesn't exist (fgetxattr should fail with
3670 * errno set to ENOATTR (61) in this case).
3672 if (fep->xattr_counter > 0)
3673 xattr_num = (random() % fep->xattr_counter) + 1;
3677 e = generate_xattr_name(xattr_num, name, sizeof(name));
3679 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3680 procid, opno, f.path, e);
3684 value_len = getxattr(f.path, name, NULL, 0);
3685 if (value_len < 0) {
3687 printf("%d/%d: getfattr file %s name %s failed %d\n",
3688 procid, opno, f.path, name, errno);
3692 /* A xattr without value.*/
3693 if (value_len == 0) {
3698 value = malloc(value_len);
3701 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3702 procid, opno, f.path, value_len);
3706 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3709 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3710 procid, opno, f.path, name, value_len, e);
3717 link_f(int opno, long r)
3731 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3733 printf("%d/%d: link - no file\n", procid, opno);
3737 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3743 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3747 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3748 printf("%d/%d: link - no filename from %s\n",
3749 procid, opno, l.path);
3755 e = link_path(&f, &l) < 0 ? errno : 0;
3758 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3760 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3762 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3769 listfattr_f(int opno, long r)
3775 char *buffer = NULL;
3779 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3781 printf("%d/%d: listfattr - no filename\n", procid, opno);
3786 e = listxattr(f.path, NULL, 0);
3789 printf("%d/%d: listfattr %s failed %d\n",
3790 procid, opno, f.path, errno);
3794 if (buffer_len == 0) {
3796 printf("%d/%d: listfattr %s - has no extended attributes\n",
3797 procid, opno, f.path);
3801 buffer = malloc(buffer_len);
3804 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3805 procid, opno, f.path, buffer_len);
3809 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3811 printf("%d/%d: listfattr %s buffer length %d %d\n",
3812 procid, opno, f.path, buffer_len, e);
3819 mkdir_f(int opno, long r)
3829 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3834 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3838 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3839 printf("%d/%d: mkdir - no filename from %s\n",
3840 procid, opno, f.path);
3845 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3848 add_to_flist(FT_DIR, id, parid, 0);
3850 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3851 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3857 mknod_f(int opno, long r)
3867 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3872 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3876 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3877 printf("%d/%d: mknod - no filename from %s\n",
3878 procid, opno, f.path);
3883 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3886 add_to_flist(FT_DEV, id, parid, 0);
3888 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3889 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3894 #ifdef HAVE_SYS_MMAN_H
3895 struct print_flags mmap_flags[] = {
3896 { MAP_SHARED, "SHARED"},
3897 { MAP_PRIVATE, "PRIVATE"},
3901 #define translate_mmap_flags(flags) \
3902 ({translate_flags(flags, "|", mmap_flags);})
3906 do_mmap(int opno, long r, int prot)
3908 #ifdef HAVE_SYS_MMAN_H
3920 sigjmp_buf sigbus_jmpbuf;
3923 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3925 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3929 fd = open_path(&f, O_RDWR);
3930 e = fd < 0 ? errno : 0;
3934 printf("%d/%d: do_mmap - open %s failed %d\n",
3935 procid, opno, f.path, e);
3939 if (fstat64(fd, &stb) < 0) {
3941 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3942 procid, opno, f.path, errno);
3947 inode_info(st, sizeof(st), &stb, v);
3948 if (stb.st_size == 0) {
3950 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3957 lr = ((int64_t)random() << 32) + random();
3958 off = (off64_t)(lr % stb.st_size);
3959 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3960 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3962 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3963 addr = mmap(NULL, len, prot, flags, fd, off);
3964 e = (addr == MAP_FAILED) ? errno : 0;
3967 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3968 procid, opno, f.path, st, (long long)off,
3969 (int)len, translate_mmap_flags(flags), e);
3975 if (prot & PROT_WRITE) {
3976 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3977 sigbus_jmp = &sigbus_jmpbuf;
3978 memset(addr, nameseq & 0xff, len);
3982 if ((buf = malloc(len)) != NULL) {
3983 memcpy(buf, addr, len);
3988 /* set NULL to stop other functions from doing siglongjmp */
3992 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3993 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3994 f.path, st, (long long)off, (int)len,
3995 translate_mmap_flags(flags),
3996 (e == 0) ? "0" : "Bus error");
4004 mread_f(int opno, long r)
4006 #ifdef HAVE_SYS_MMAN_H
4007 do_mmap(opno, r, PROT_READ);
4012 mwrite_f(int opno, long r)
4014 #ifdef HAVE_SYS_MMAN_H
4015 do_mmap(opno, r, PROT_WRITE);
4020 punch_f(int opno, long r)
4022 #ifdef HAVE_LINUX_FALLOC_H
4023 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4028 zero_f(int opno, long r)
4030 #ifdef HAVE_LINUX_FALLOC_H
4031 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4036 collapse_f(int opno, long r)
4038 #ifdef HAVE_LINUX_FALLOC_H
4039 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4044 insert_f(int opno, long r)
4046 #ifdef HAVE_LINUX_FALLOC_H
4047 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4052 read_f(int opno, long r)
4066 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4068 printf("%d/%d: read - no filename\n", procid, opno);
4072 fd = open_path(&f, O_RDONLY);
4073 e = fd < 0 ? errno : 0;
4077 printf("%d/%d: read - open %s failed %d\n",
4078 procid, opno, f.path, e);
4082 if (fstat64(fd, &stb) < 0) {
4084 printf("%d/%d: read - fstat64 %s failed %d\n",
4085 procid, opno, f.path, errno);
4090 inode_info(st, sizeof(st), &stb, v);
4091 if (stb.st_size == 0) {
4093 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4099 lr = ((int64_t)random() << 32) + random();
4100 off = (off64_t)(lr % stb.st_size);
4101 lseek64(fd, off, SEEK_SET);
4102 len = (random() % FILELEN_MAX) + 1;
4104 e = read(fd, buf, len) < 0 ? errno : 0;
4107 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4108 procid, opno, f.path, st, (long long)off, (int)len, e);
4114 readlink_f(int opno, long r)
4122 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4124 printf("%d/%d: readlink - no filename\n", procid, opno);
4128 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4131 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4136 readv_f(int opno, long r)
4148 struct iovec *iov = NULL;
4155 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4157 printf("%d/%d: readv - no filename\n", procid, opno);
4161 fd = open_path(&f, O_RDONLY);
4162 e = fd < 0 ? errno : 0;
4166 printf("%d/%d: readv - open %s failed %d\n",
4167 procid, opno, f.path, e);
4171 if (fstat64(fd, &stb) < 0) {
4173 printf("%d/%d: readv - fstat64 %s failed %d\n",
4174 procid, opno, f.path, errno);
4179 inode_info(st, sizeof(st), &stb, v);
4180 if (stb.st_size == 0) {
4182 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4188 lr = ((int64_t)random() << 32) + random();
4189 off = (off64_t)(lr % stb.st_size);
4190 lseek64(fd, off, SEEK_SET);
4191 len = (random() % FILELEN_MAX) + 1;
4194 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4195 iov = calloc(iovcnt, sizeof(struct iovec));
4196 iovl = len / iovcnt;
4198 for (i=0; i<iovcnt; i++) {
4199 (iov + i)->iov_base = (buf + iovb);
4200 (iov + i)->iov_len = iovl;
4204 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4207 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4208 procid, opno, f.path, st, (long long)off, (int)iovl,
4215 removefattr_f(int opno, long r)
4221 char name[XATTR_NAME_BUF_SIZE];
4225 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4227 printf("%d/%d: removefattr - no filename\n", procid, opno);
4233 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4234 * remove a xattr that doesn't exist (fremovexattr should fail with
4235 * errno set to ENOATTR (61) in this case).
4237 if (fep->xattr_counter > 0)
4238 xattr_num = (random() % fep->xattr_counter) + 1;
4242 e = generate_xattr_name(xattr_num, name, sizeof(name));
4244 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4245 procid, opno, f.path, e);
4249 e = removexattr(f.path, name) < 0 ? errno : 0;
4251 printf("%d/%d: removefattr file %s name %s %d\n",
4252 procid, opno, f.path, name, e);
4257 struct print_flags renameat2_flags [] = {
4258 { RENAME_NOREPLACE, "NOREPLACE"},
4259 { RENAME_WHITEOUT, "WHITEOUT"},
4263 #define translate_renameat2_flags(mode) \
4264 ({translate_flags(mode, "|", renameat2_flags);})
4267 do_renameat2(int opno, long r, int mode)
4283 /* get an existing path for the source of the rename */
4285 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4286 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4288 printf("%d/%d: rename - no source filename\n",
4294 /* get an existing directory for the destination parent directory name */
4295 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4301 /* generate a new path using an existing parent directory in name */
4302 init_pathname(&newf);
4303 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4307 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
4308 printf("%d/%d: rename - no filename from %s\n",
4309 procid, opno, f.path);
4311 free_pathname(&newf);
4315 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4318 int xattr_counter = fep->xattr_counter;
4321 oldparid = fep->parent;
4323 if (flp - flist == FT_DIR)
4324 fix_parent(oldid, id);
4326 if (mode == RENAME_WHITEOUT) {
4327 fep->xattr_counter = 0;
4328 add_to_flist(flp - flist, id, parid, xattr_counter);
4330 del_from_flist(flp - flist, fep - flp->fents);
4331 add_to_flist(flp - flist, id, parid, xattr_counter);
4335 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4336 opno, translate_renameat2_flags(mode), f.path,
4339 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4340 procid, opno, oldid, oldparid);
4341 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4342 procid, opno, id, parid);
4345 free_pathname(&newf);
4350 rename_f(int opno, long r)
4352 do_renameat2(opno, r, 0);
4356 rnoreplace_f(int opno, long r)
4358 do_renameat2(opno, r, RENAME_NOREPLACE);
4362 rwhiteout_f(int opno, long r)
4364 do_renameat2(opno, r, RENAME_WHITEOUT);
4368 resvsp_f(int opno, long r)
4373 struct xfs_flock64 fl;
4381 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4383 printf("%d/%d: resvsp - no filename\n", procid, opno);
4387 fd = open_path(&f, O_RDWR);
4388 e = fd < 0 ? errno : 0;
4392 printf("%d/%d: resvsp - open %s failed %d\n",
4393 procid, opno, f.path, e);
4397 if (fstat64(fd, &stb) < 0) {
4399 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4400 procid, opno, f.path, errno);
4405 inode_info(st, sizeof(st), &stb, v);
4406 lr = ((int64_t)random() << 32) + random();
4407 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4409 fl.l_whence = SEEK_SET;
4411 fl.l_len = (off64_t)(random() % (1024 * 1024));
4412 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4414 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4415 procid, opno, f.path, st,
4416 (long long)off, (long long)fl.l_len, e);
4422 rmdir_f(int opno, long r)
4430 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4432 printf("%d/%d: rmdir - no directory\n", procid, opno);
4436 e = rmdir_path(&f) < 0 ? errno : 0;
4439 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4441 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4443 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4444 procid, opno, fep->id, fep->parent);
4450 setattr_f(int opno, long r)
4459 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4460 append_pathname(&f, ".");
4461 fd = open_path(&f, O_RDWR);
4462 e = fd < 0 ? errno : 0;
4465 fl = attr_mask & (uint)random();
4466 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4468 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4474 setfattr_f(int opno, long r)
4481 char name[XATTR_NAME_BUF_SIZE];
4487 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4489 printf("%d/%d: setfattr - no filename\n", procid, opno);
4494 if ((fep->xattr_counter > 0) && (random() % 2)) {
4496 * Use an existing xattr name for replacing its value or
4497 * create again a xattr that was previously deleted.
4499 xattr_num = (random() % fep->xattr_counter) + 1;
4501 flag = XATTR_REPLACE;
4503 /* Use a new xattr name. */
4504 xattr_num = fep->xattr_counter + 1;
4506 * Don't always use the create flag because even if our xattr
4507 * counter is 0, we may still have xattrs associated to this
4508 * file (this happens when xattrs are added to a file through
4509 * one of its other hard links), so we can end up updating an
4510 * existing xattr too.
4513 flag = XATTR_CREATE;
4517 * The maximum supported value size depends on the filesystem
4518 * implementation, but 100 bytes is a safe value for most filesystems
4521 value_len = random() % 101;
4522 value = gen_random_string(value_len);
4523 if (!value && value_len > 0) {
4525 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4526 procid, opno, f.path, value_len);
4529 e = generate_xattr_name(xattr_num, name, sizeof(name));
4531 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4532 procid, opno, f.path, e);
4536 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4538 fep->xattr_counter++;
4540 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4541 procid, opno, f.path, name, xattr_flag_to_string(flag),
4549 stat_f(int opno, long r)
4557 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4559 printf("%d/%d: stat - no entries\n", procid, opno);
4563 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4566 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4571 symlink_f(int opno, long r)
4584 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
4589 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4593 (void)fent_to_name(&f, &flist[FT_DIR], fep);
4594 printf("%d/%d: symlink - no filename from %s\n",
4595 procid, opno, f.path);
4600 len = (int)(random() % PATH_MAX);
4601 val = malloc(len + 1);
4603 memset(val, 'x', len);
4605 for (i = 10; i < len - 1; i += 10)
4607 e = symlink_path(val, &f) < 0 ? errno : 0;
4610 add_to_flist(FT_SYM, id, parid, 0);
4613 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4614 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4621 sync_f(int opno, long r)
4625 printf("%d/%d: sync\n", procid, opno);
4629 truncate_f(int opno, long r)
4640 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4642 printf("%d/%d: truncate - no filename\n", procid, opno);
4646 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4650 printf("%d/%d: truncate - stat64 %s failed %d\n",
4651 procid, opno, f.path, e);
4655 inode_info(st, sizeof(st), &stb, v);
4656 lr = ((int64_t)random() << 32) + random();
4657 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4659 e = truncate64_path(&f, off) < 0 ? errno : 0;
4662 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4663 st, (long long)off, e);
4668 unlink_f(int opno, long r)
4677 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4679 printf("%d/%d: unlink - no file\n", procid, opno);
4683 e = unlink_path(&f) < 0 ? errno : 0;
4686 del_from_flist(flp - flist, fep - flp->fents);
4688 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4690 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4691 procid, opno, fep->id, fep->parent);
4697 unresvsp_f(int opno, long r)
4702 struct xfs_flock64 fl;
4710 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4712 printf("%d/%d: unresvsp - no filename\n", procid, opno);
4716 fd = open_path(&f, O_RDWR);
4717 e = fd < 0 ? errno : 0;
4721 printf("%d/%d: unresvsp - open %s failed %d\n",
4722 procid, opno, f.path, e);
4726 if (fstat64(fd, &stb) < 0) {
4728 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4729 procid, opno, f.path, errno);
4734 inode_info(st, sizeof(st), &stb, v);
4735 lr = ((int64_t)random() << 32) + random();
4736 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4738 fl.l_whence = SEEK_SET;
4740 fl.l_len = (off64_t)(random() % (1 << 20));
4741 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4743 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4744 procid, opno, f.path, st,
4745 (long long)off, (long long)fl.l_len, e);
4751 write_f(int opno, long r)
4765 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4767 printf("%d/%d: write - no filename\n", procid, opno);
4771 fd = open_path(&f, O_WRONLY);
4772 e = fd < 0 ? errno : 0;
4776 printf("%d/%d: write - open %s failed %d\n",
4777 procid, opno, f.path, e);
4781 if (fstat64(fd, &stb) < 0) {
4783 printf("%d/%d: write - fstat64 %s failed %d\n",
4784 procid, opno, f.path, errno);
4789 inode_info(st, sizeof(st), &stb, v);
4790 lr = ((int64_t)random() << 32) + random();
4791 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4793 lseek64(fd, off, SEEK_SET);
4794 len = (random() % FILELEN_MAX) + 1;
4796 memset(buf, nameseq & 0xff, len);
4797 e = write(fd, buf, len) < 0 ? errno : 0;
4800 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4801 procid, opno, f.path, st, (long long)off, (int)len, e);
4807 writev_f(int opno, long r)
4819 struct iovec *iov = NULL;
4826 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4828 printf("%d/%d: writev - no filename\n", procid, opno);
4832 fd = open_path(&f, O_WRONLY);
4833 e = fd < 0 ? errno : 0;
4837 printf("%d/%d: writev - open %s failed %d\n",
4838 procid, opno, f.path, e);
4842 if (fstat64(fd, &stb) < 0) {
4844 printf("%d/%d: writev - fstat64 %s failed %d\n",
4845 procid, opno, f.path, errno);
4850 inode_info(st, sizeof(st), &stb, v);
4851 lr = ((int64_t)random() << 32) + random();
4852 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4854 lseek64(fd, off, SEEK_SET);
4855 len = (random() % FILELEN_MAX) + 1;
4857 memset(buf, nameseq & 0xff, len);
4859 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4860 iov = calloc(iovcnt, sizeof(struct iovec));
4861 iovl = len / iovcnt;
4863 for (i=0; i<iovcnt; i++) {
4864 (iov + i)->iov_base = (buf + iovb);
4865 (iov + i)->iov_len = iovl;
4869 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4873 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4874 procid, opno, f.path, st, (long long)off, (int)iovl,
4881 xattr_flag_to_string(int flag)
4883 if (flag == XATTR_CREATE)
4885 if (flag == XATTR_REPLACE)