1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
14 #ifdef HAVE_BTRFSUTIL_H
15 #include <btrfsutil.h>
17 #ifdef HAVE_LINUX_FIEMAP_H
18 #include <linux/fiemap.h>
20 #ifdef HAVE_SYS_PRCTL_H
21 #include <sys/prctl.h>
30 #define URING_ENTRIES 1
32 bool have_io_uring; /* to indicate runtime availability */
34 #include <sys/syscall.h>
35 #include <sys/xattr.h>
37 #ifndef FS_IOC_GETFLAGS
38 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
40 #ifndef FS_IOC_SETFLAGS
41 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
45 #define XFS_ERRTAG_MAX 17
46 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
47 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
52 #ifndef HAVE_RENAMEAT2
53 #if !defined(SYS_renameat2) && defined(__x86_64__)
54 #define SYS_renameat2 316
57 #if !defined(SYS_renameat2) && defined(__i386__)
58 #define SYS_renameat2 353
61 static int renameat2(int dfd1, const char *path1,
62 int dfd2, const char *path2,
66 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
74 #ifndef RENAME_NOREPLACE
75 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
77 #ifndef RENAME_EXCHANGE
78 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
80 #ifndef RENAME_WHITEOUT
81 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
84 #define FILELEN_MAX (32*4096)
150 typedef void (*opfnc_t)(int, long);
152 typedef struct opdesc {
160 typedef struct fent {
167 typedef struct flist {
174 typedef struct pathname {
184 struct print_string {
191 #define FT_DIRm (1 << FT_DIR)
193 #define FT_REGm (1 << FT_REG)
195 #define FT_SYMm (1 << FT_SYM)
197 #define FT_DEVm (1 << FT_DEV)
199 #define FT_RTFm (1 << FT_RTF)
201 #define FT_SUBVOLm (1 << FT_SUBVOL)
203 #define FT_ANYm ((1 << FT_nft) - 1)
204 #define FT_REGFILE (FT_REGm | FT_RTFm)
205 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
206 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
208 #define FLIST_SLOT_INCR 16
211 #define MAXFSIZE ((1ULL << 63) - 1ULL)
212 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
214 #define XATTR_NAME_BUF_SIZE 18
216 void afsync_f(int, long);
217 void allocsp_f(int, long);
218 void aread_f(int, long);
219 void attr_remove_f(int, long);
220 void attr_set_f(int, long);
221 void awrite_f(int, long);
222 void bulkstat_f(int, long);
223 void bulkstat1_f(int, long);
224 void chown_f(int, long);
225 void clonerange_f(int, long);
226 void copyrange_f(int, long);
227 void creat_f(int, long);
228 void deduperange_f(int, long);
229 void dread_f(int, long);
230 void dwrite_f(int, long);
231 void fallocate_f(int, long);
232 void fdatasync_f(int, long);
233 void fiemap_f(int, long);
234 void freesp_f(int, long);
235 void fsync_f(int, long);
236 char *gen_random_string(int);
237 void getattr_f(int, long);
238 void getdents_f(int, long);
239 void getfattr_f(int, long);
240 void link_f(int, long);
241 void listfattr_f(int, long);
242 void mkdir_f(int, long);
243 void mknod_f(int, long);
244 void mread_f(int, long);
245 void mwrite_f(int, long);
246 void punch_f(int, long);
247 void zero_f(int, long);
248 void collapse_f(int, long);
249 void insert_f(int, long);
250 void read_f(int, long);
251 void readlink_f(int, long);
252 void readv_f(int, long);
253 void removefattr_f(int, long);
254 void rename_f(int, long);
255 void rnoreplace_f(int, long);
256 void rexchange_f(int, long);
257 void rwhiteout_f(int, long);
258 void resvsp_f(int, long);
259 void rmdir_f(int, long);
260 void setattr_f(int, long);
261 void setfattr_f(int, long);
262 void setxattr_f(int, long);
263 void snapshot_f(int, long);
264 void splice_f(int, long);
265 void stat_f(int, long);
266 void subvol_create_f(int, long);
267 void subvol_delete_f(int, long);
268 void symlink_f(int, long);
269 void sync_f(int, long);
270 void truncate_f(int, long);
271 void unlink_f(int, long);
272 void unresvsp_f(int, long);
273 void uring_read_f(int, long);
274 void uring_write_f(int, long);
275 void write_f(int, long);
276 void writev_f(int, long);
277 char *xattr_flag_to_string(int);
280 /* { OP_ENUM, "name", function, freq, iswrite }, */
281 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
282 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
283 { OP_AREAD, "aread", aread_f, 1, 0 },
284 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
285 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
286 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
287 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
288 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
289 { OP_CHOWN, "chown", chown_f, 3, 1 },
290 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
291 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
292 { OP_CREAT, "creat", creat_f, 4, 1 },
293 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
294 { OP_DREAD, "dread", dread_f, 4, 0 },
295 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
296 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
297 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
298 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
299 { OP_FREESP, "freesp", freesp_f, 1, 1 },
300 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
301 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
302 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
303 /* get extended attribute */
304 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
305 { OP_LINK, "link", link_f, 1, 1 },
306 /* list extent attributes */
307 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
308 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
309 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
310 { OP_MREAD, "mread", mread_f, 2, 0 },
311 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
312 { OP_PUNCH, "punch", punch_f, 1, 1 },
313 { OP_ZERO, "zero", zero_f, 1, 1 },
314 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
315 { OP_INSERT, "insert", insert_f, 1, 1 },
316 { OP_READ, "read", read_f, 1, 0 },
317 { OP_READLINK, "readlink", readlink_f, 1, 0 },
318 { OP_READV, "readv", readv_f, 1, 0 },
319 /* remove (delete) extended attribute */
320 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
321 { OP_RENAME, "rename", rename_f, 2, 1 },
322 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
323 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
324 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
325 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
326 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
327 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
328 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
329 /* set extended attribute */
330 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
331 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
332 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
333 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
334 { OP_SPLICE, "splice", splice_f, 1, 1 },
335 { OP_STAT, "stat", stat_f, 1, 0 },
336 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
337 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
338 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
339 { OP_SYNC, "sync", sync_f, 1, 1 },
340 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
341 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
342 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
343 { OP_URING_READ, "uring_read", uring_read_f, 1, 0 },
344 { OP_URING_WRITE, "uring_write", uring_write_f, 1, 1 },
345 { OP_WRITE, "write", write_f, 4, 1 },
346 { OP_WRITEV, "writev", writev_f, 4, 1 },
349 flist_t flist[FT_nft] = {
363 struct xfs_fsop_geom geom;
374 unsigned int idmodulo = XFS_IDMODULO_MAX;
375 unsigned int attr_mask = ~0;
378 unsigned long seed = 0;
382 int verifiable_log = 0;
383 sig_atomic_t should_stop = 0;
384 sigjmp_buf *sigbus_jmp = NULL;
385 char *execute_cmd = NULL;
386 int execute_freq = 1;
387 struct print_string flag_str = {0};
389 void add_to_flist(int, int, int, int);
390 void append_pathname(pathname_t *, char *);
391 int attr_list_path(pathname_t *, char *, const int);
392 int attr_remove_path(pathname_t *, const char *);
393 int attr_set_path(pathname_t *, const char *, const char *, const int);
394 void check_cwd(void);
395 void cleanup_flist(void);
396 int creat_path(pathname_t *, mode_t);
397 void dcache_enter(int, int);
398 void dcache_init(void);
399 fent_t *dcache_lookup(int);
400 void dcache_purge(int, int);
401 void del_from_flist(int, int);
402 int dirid_to_name(char *, int);
404 int fent_to_name(pathname_t *, fent_t *);
405 bool fents_ancestor_check(fent_t *, fent_t *);
406 void fix_parent(int, int, bool);
407 void free_pathname(pathname_t *);
408 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
409 int generate_xattr_name(int, char *, int);
410 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
411 void init_pathname(pathname_t *);
412 int lchown_path(pathname_t *, uid_t, gid_t);
413 int link_path(pathname_t *, pathname_t *);
414 int lstat64_path(pathname_t *, struct stat64 *);
415 void make_freq_table(void);
416 int mkdir_path(pathname_t *, mode_t);
417 int mknod_path(pathname_t *, mode_t, dev_t);
418 void namerandpad(int, char *, int);
419 int open_file_or_dir(pathname_t *, int);
420 int open_path(pathname_t *, int);
421 DIR *opendir_path(pathname_t *);
422 void process_freq(char *);
423 int readlink_path(pathname_t *, char *, size_t);
424 int rename_path(pathname_t *, pathname_t *, int);
425 int rmdir_path(pathname_t *);
426 void separate_pathname(pathname_t *, char *, pathname_t *);
427 void show_ops(int, char *);
428 int stat64_path(pathname_t *, struct stat64 *);
429 int symlink_path(const char *, pathname_t *);
430 int truncate64_path(pathname_t *, off64_t);
431 int unlink_path(pathname_t *);
433 void write_freq(void);
434 void zero_freq(void);
435 void non_btrfs_freq(const char *);
437 void sg_handler(int signum)
445 * Only handle SIGBUS when mmap write to a hole and no
446 * block can be allocated due to ENOSPC, abort otherwise.
449 siglongjmp(*sigbus_jmp, -1);
451 printf("Unknown SIGBUS is caught, Abort!\n");
454 /* should not reach here */
461 int main(int argc, char **argv)
465 char *dirname = NULL;
466 char *logname = NULL;
467 char rpath[PATH_MAX];
476 xfs_error_injection_t err_inj;
477 struct sigaction action;
479 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
481 errrange = errtag = 0;
483 nops = sizeof(ops) / sizeof(ops[0]);
484 ops_end = &ops[nops];
486 while ((c = getopt(argc, argv, allopts)) != -1) {
495 sscanf(optarg, "%d", &errtag);
499 } else if (errtag == 0)
501 if (errtag >= XFS_ERRTAG_MAX) {
503 "error tag %d too large (max %d)\n",
504 errtag, XFS_ERRTAG_MAX - 1);
509 process_freq(optarg);
512 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
513 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
516 idmodulo = strtoul(optarg, NULL, 0);
517 if (idmodulo > XFS_IDMODULO_MAX) {
519 "chown modulo %d too big (max %d)\n",
520 idmodulo, XFS_IDMODULO_MAX);
525 loops = atoi(optarg);
528 operations = atoi(optarg);
535 nproc = atoi(optarg);
541 seed = strtoul(optarg, NULL, 0);
550 execute_cmd = optarg;
556 attr_mask = strtoul(optarg, NULL, 0);
560 if (optarg[0] == 'c')
571 execute_freq = strtoul(optarg, NULL, 0);
574 fprintf(stderr, "%s - invalid parameters\n",
584 /* no directory specified */
585 if (!nousage) usage();
589 non_btrfs_freq(dirname);
590 (void)mkdir(dirname, 0777);
591 if (logname && logname[0] != '/') {
592 if (!getcwd(rpath, sizeof(rpath))){
593 perror("getcwd failed");
599 if (chdir(dirname) < 0) {
604 char path[PATH_MAX + NAME_MAX + 1];
605 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
606 if (freopen(path, "a", stdout) == NULL) {
607 perror("freopen logfile failed");
611 sprintf(buf, "fss%x", (unsigned int)getpid());
612 fd = creat(buf, 0666);
613 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
614 maxfsize = (off64_t)MAXFSIZE32;
616 maxfsize = (off64_t)MAXFSIZE;
621 gettimeofday(&t, (void *)NULL);
622 seed = (int)t.tv_sec ^ (int)t.tv_usec;
623 printf("seed = %ld\n", seed);
625 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
626 if (i >= 0 && geom.rtblocks)
627 rtpct = MIN(MAX(geom.rtblocks * 100 /
628 (geom.rtblocks + geom.datablocks), 1), 99);
637 for (i = 0; i < j; i++)
640 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
646 for (i = 0; i < j; i++)
649 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
651 printf("Injecting failure on tag #%d\n", errtag);
652 err_inj.errtag = errtag;
654 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
656 perror("fsstress - XFS_SYSSGI error injection call");
665 action.sa_handler = sg_handler;
666 sigemptyset(&action.sa_mask);
668 if (sigaction(SIGTERM, &action, 0)) {
669 perror("sigaction failed");
673 for (i = 0; i < nproc; i++) {
675 sigemptyset(&action.sa_mask);
676 action.sa_handler = SIG_DFL;
677 if (sigaction(SIGTERM, &action, 0))
679 action.sa_handler = sg_handler;
680 if (sigaction(SIGBUS, &action, 0))
682 #ifdef HAVE_SYS_PRCTL_H
683 prctl(PR_SET_PDEATHSIG, SIGKILL);
684 if (getppid() == 1) /* parent died already? */
688 char path[PATH_MAX + NAME_MAX + 2 + 11];
689 snprintf(path, sizeof(path), "%s/%s.%d",
691 if (freopen(path, "a", stdout) == NULL) {
692 perror("freopen logfile failed");
698 if (io_setup(AIO_ENTRIES, &io_ctx) != 0) {
699 fprintf(stderr, "io_setup failed\n");
704 have_io_uring = true;
705 /* If ENOSYS, just ignore uring, other errors are fatal. */
706 if (io_uring_queue_init(URING_ENTRIES, &ring, 0)) {
707 if (errno == ENOSYS) {
708 have_io_uring = false;
710 fprintf(stderr, "io_uring_queue_init failed\n");
715 for (i = 0; !loops || (i < loops); i++)
718 if(io_destroy(io_ctx) != 0) {
719 fprintf(stderr, "io_destroy failed");
725 io_uring_queue_exit(&ring);
732 while (wait(&stat) > 0 && !should_stop) {
735 action.sa_flags = SA_RESTART;
736 sigaction(SIGTERM, &action, 0);
737 kill(-getpid(), SIGTERM);
738 while (wait(&stat) > 0)
744 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
746 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
748 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
761 add_string(struct print_string *str, const char *add)
763 int len = strlen(add);
768 if (len > (str->max - 1) - str->len) {
769 str->len = str->max - 1;
773 memcpy(str->buffer + str->len, add, len);
775 str->buffer[str->len] = '\0';
781 translate_flags(int flags, const char *delim,
782 const struct print_flags *flag_array)
784 int i, mask, first = 1;
787 if (!flag_str.buffer) {
788 flag_str.buffer = malloc(4096);
792 if (!flag_str.buffer)
795 flag_str.buffer[0] = '\0';
797 for (i = 0; flag_array[i].name && flags; i++) {
798 mask = flag_array[i].mask;
799 if ((flags & mask) != mask)
802 add = flag_array[i].name;
805 add_string(&flag_str, delim);
808 add_string(&flag_str, add);
811 /* Check whether there are any leftover flags. */
817 add_string(&flag_str, delim);
819 ret = snprintf(number, 11, "0x%x", flags) > 0;
820 if (ret > 0 && ret <= 11)
821 add_string(&flag_str, number);
824 return flag_str.buffer;
828 add_to_flist(int ft, int id, int parent, int xattr_counter)
834 if (ftp->nfiles == ftp->nslots) {
835 ftp->nslots += FLIST_SLOT_INCR;
836 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
838 fep = &ftp->fents[ftp->nfiles++];
841 fep->parent = parent;
842 fep->xattr_counter = xattr_counter;
846 append_pathname(pathname_t *name, char *str)
852 /* attempting to append to a dir a zero length path */
853 if (len && *str == '/' && name->len == 0) {
854 fprintf(stderr, "fsstress: append_pathname failure\n");
855 assert(chdir(homedir) == 0);
860 name->path = realloc(name->path, name->len + 1 + len);
861 strcpy(&name->path[name->len], str);
866 attr_list_count(char *buffer, int buffersize)
869 char *end = buffer + buffersize;
872 while (p < end && *p != 0) {
881 attr_list_path(pathname_t *name,
883 const int buffersize)
885 char buf[NAME_MAX + 1];
889 rval = llistxattr(name->path, buffer, buffersize);
890 if (rval >= 0 || errno != ENAMETOOLONG)
892 separate_pathname(name, buf, &newname);
893 if (chdir(buf) == 0) {
894 rval = attr_list_path(&newname, buffer, buffersize);
895 assert(chdir("..") == 0);
897 free_pathname(&newname);
902 attr_remove_path(pathname_t *name, const char *attrname)
904 char buf[NAME_MAX + 1];
908 rval = lremovexattr(name->path, attrname);
909 if (rval >= 0 || errno != ENAMETOOLONG)
911 separate_pathname(name, buf, &newname);
912 if (chdir(buf) == 0) {
913 rval = attr_remove_path(&newname, attrname);
914 assert(chdir("..") == 0);
916 free_pathname(&newname);
921 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
922 const int valuelength)
924 char buf[NAME_MAX + 1];
928 rval = lsetxattr(name->path, attrname, attrvalue, valuelength, 0);
929 if (rval >= 0 || errno != ENAMETOOLONG)
931 separate_pathname(name, buf, &newname);
932 if (chdir(buf) == 0) {
933 rval = attr_set_path(&newname, attrname, attrvalue,
935 assert(chdir("..") == 0);
937 free_pathname(&newname);
945 struct stat64 statbuf;
947 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
949 assert(chdir(homedir) == 0);
950 fprintf(stderr, "fsstress: check_cwd failure\n");
957 * go thru flist and release all entries
959 * NOTE: this function shouldn't be called until the end of a process
967 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
976 creat_path(pathname_t *name, mode_t mode)
978 char buf[NAME_MAX + 1];
982 rval = creat(name->path, mode);
983 if (rval >= 0 || errno != ENAMETOOLONG)
985 separate_pathname(name, buf, &newname);
986 if (chdir(buf) == 0) {
987 rval = creat_path(&newname, mode);
988 assert(chdir("..") == 0);
990 free_pathname(&newname);
995 dcache_enter(int dirid, int slot)
997 dcache[dirid % NDCACHE] = slot;
1005 for (i = 0; i < NDCACHE; i++)
1010 dcache_lookup(int dirid)
1015 i = dcache[dirid % NDCACHE];
1016 if (i >= 0 && i < flist[FT_DIR].nfiles &&
1017 (fep = &flist[FT_DIR].fents[i])->id == dirid)
1019 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
1020 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
1026 dcache_purge(int dirid, int ft)
1029 dcp = &dcache[dirid % NDCACHE];
1030 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1031 flist[ft].fents[*dcp].id == dirid)
1036 * Delete the item from the list by
1037 * moving last entry over the deleted one;
1038 * unless deleted entry is the last one.
1039 * Input: which file list array and which slot in array
1042 del_from_flist(int ft, int slot)
1047 if (ft == FT_DIR || ft == FT_SUBVOL)
1048 dcache_purge(ftp->fents[slot].id, ft);
1049 if (slot != ftp->nfiles - 1) {
1050 if (ft == FT_DIR || ft == FT_SUBVOL)
1051 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1052 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1058 delete_subvol_children(int parid)
1063 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1065 for (c = 0; c < flp->nfiles; c++) {
1066 if (flp->fents[c].parent == parid) {
1067 int id = flp->fents[c].id;
1068 del_from_flist(i, c);
1069 if (i == FT_DIR || i == FT_SUBVOL)
1070 delete_subvol_children(id);
1078 dirid_to_fent(int dirid)
1085 if ((fep = dcache_lookup(dirid)))
1089 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1090 if (fep->id == dirid) {
1091 dcache_enter(dirid, fep - flp->fents);
1105 struct stat64 statbuf;
1113 dividend = (operations + execute_freq) / (execute_freq + 1);
1114 sprintf(buf, "p%x", procid);
1115 (void)mkdir(buf, 0777);
1116 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1120 top_ino = statbuf.st_ino;
1121 homedir = getcwd(NULL, 0);
1123 perror("getcwd failed");
1129 namerand = random();
1130 for (opno = 0; opno < operations; opno++) {
1131 if (execute_cmd && opno && opno % dividend == 0) {
1133 printf("%d: execute command %s\n", opno,
1135 rval = system(execute_cmd);
1137 fprintf(stderr, "execute command failed with "
1140 p = &ops[freq_table[random() % freq_table_size]];
1141 p->func(opno, random());
1143 * test for forced shutdown by stat'ing the test
1144 * directory. If this stat returns EIO, assume
1145 * the forced shutdown happened.
1147 if (errtag != 0 && opno % 100 == 0) {
1148 rval = stat64(".", &statbuf);
1150 fprintf(stderr, "Detected EIO\n");
1156 assert(chdir("..") == 0);
1161 sprintf(cmd, "rm -rf %s", buf);
1164 perror("cleaning up");
1170 * build up a pathname going thru the file entry and all
1171 * its parent entries
1172 * Return 0 on error, 1 on success;
1175 fent_to_name(pathname_t *name, fent_t *fep)
1177 flist_t *flp = &flist[fep->ft];
1178 char buf[NAME_MAX + 1];
1186 /* build up parent directory name */
1187 if (fep->parent != -1) {
1188 pfep = dirid_to_fent(fep->parent);
1191 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1192 procid, fep->id, fep->parent);
1197 e = fent_to_name(name, pfep);
1200 append_pathname(name, "/");
1203 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1204 namerandpad(fep->id, buf, i);
1205 append_pathname(name, buf);
1210 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1214 for (tmpfep = fep; tmpfep->parent != -1;
1215 tmpfep = dirid_to_fent(tmpfep->parent)) {
1216 if (tmpfep->parent == dfep->id)
1224 fix_parent(int oldid, int newid, bool swap)
1231 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1232 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1233 if (fep->parent == oldid)
1234 fep->parent = newid;
1235 else if (swap && fep->parent == newid)
1236 fep->parent = oldid;
1242 free_pathname(pathname_t *name)
1252 * Generate a filename of type ft.
1253 * If we have a fep which should be a directory then use it
1254 * as the parent path for this new filename i.e. prepend with it.
1257 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1259 char buf[NAME_MAX + 1];
1268 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1269 namerandpad(id, buf, len);
1271 /* prepend fep parent dir-name to it */
1273 e = fent_to_name(name, fep);
1276 append_pathname(name, "/");
1278 append_pathname(name, buf);
1282 for (j = 0; !*v && j < ilistlen; j++) {
1283 if (ilist[j] == id) {
1291 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1295 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1305 * Input: "which" to choose the file-types eg. non-directory
1306 * Input: "r" to choose which file
1307 * Output: file-list, file-entry, name for the chosen file.
1308 * Output: verbose if chosen file is on the ilist.
1311 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1314 int totalsum = 0; /* total number of matching files */
1315 int partialsum = 0; /* partial sum of matching files */
1321 int e = 1; /* success */
1324 * go thru flist and add up number of files for each
1325 * category that matches with <which>.
1327 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1328 if (which & (1 << i))
1329 totalsum += flp->nfiles;
1331 if (totalsum == 0) {
1341 * Now we have possible matches between 0..totalsum-1.
1342 * And we use r to help us choose which one we want,
1343 * which when bounded by totalsum becomes x.
1345 x = (int)(r % totalsum);
1346 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1347 if (which & (1 << i)) {
1348 if (x < partialsum + flp->nfiles) {
1350 /* found the matching file entry */
1351 fep = &flp->fents[x - partialsum];
1353 /* fill-in what we were asked for */
1355 e = fent_to_name(name, fep);
1358 fprintf(stderr, "%d: failed to get path for entry:"
1359 " id=%d,parent=%d\n",
1360 procid, fep->id, fep->parent);
1369 /* turn on verbose if its an ilisted file */
1371 for (j = 0; !*v && j < ilistlen; j++) {
1372 if (ilist[j] == fep->id) {
1379 partialsum += flp->nfiles;
1383 fprintf(stderr, "fsstress: get_fname failure\n");
1390 init_pathname(pathname_t *name)
1397 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1399 char buf[NAME_MAX + 1];
1403 rval = lchown(name->path, owner, group);
1404 if (rval >= 0 || errno != ENAMETOOLONG)
1406 separate_pathname(name, buf, &newname);
1407 if (chdir(buf) == 0) {
1408 rval = lchown_path(&newname, owner, group);
1409 assert(chdir("..") == 0);
1411 free_pathname(&newname);
1416 link_path(pathname_t *name1, pathname_t *name2)
1418 char buf1[NAME_MAX + 1];
1419 char buf2[NAME_MAX + 1];
1421 pathname_t newname1;
1422 pathname_t newname2;
1425 rval = link(name1->path, name2->path);
1426 if (rval >= 0 || errno != ENAMETOOLONG)
1428 separate_pathname(name1, buf1, &newname1);
1429 separate_pathname(name2, buf2, &newname2);
1430 if (strcmp(buf1, buf2) == 0) {
1431 if (chdir(buf1) == 0) {
1432 rval = link_path(&newname1, &newname2);
1433 assert(chdir("..") == 0);
1436 if (strcmp(buf1, "..") == 0)
1438 else if (strcmp(buf2, "..") == 0)
1440 else if (strlen(buf1) == 0)
1442 else if (strlen(buf2) == 0)
1445 down1 = MAX(newname1.len, 3 + name2->len) <=
1446 MAX(3 + name1->len, newname2.len);
1448 free_pathname(&newname2);
1449 append_pathname(&newname2, "../");
1450 append_pathname(&newname2, name2->path);
1451 if (chdir(buf1) == 0) {
1452 rval = link_path(&newname1, &newname2);
1453 assert(chdir("..") == 0);
1456 free_pathname(&newname1);
1457 append_pathname(&newname1, "../");
1458 append_pathname(&newname1, name1->path);
1459 if (chdir(buf2) == 0) {
1460 rval = link_path(&newname1, &newname2);
1461 assert(chdir("..") == 0);
1465 free_pathname(&newname1);
1466 free_pathname(&newname2);
1471 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1473 char buf[NAME_MAX + 1];
1477 rval = lstat64(name->path, sbuf);
1478 if (rval >= 0 || errno != ENAMETOOLONG)
1480 separate_pathname(name, buf, &newname);
1481 if (chdir(buf) == 0) {
1482 rval = lstat64_path(&newname, sbuf);
1483 assert(chdir("..") == 0);
1485 free_pathname(&newname);
1490 make_freq_table(void)
1496 for (p = ops, f = 0; p < ops_end; p++)
1498 freq_table = malloc(f * sizeof(*freq_table));
1499 freq_table_size = f;
1500 for (p = ops, i = 0; p < ops_end; p++) {
1501 for (f = 0; f < p->freq; f++, i++)
1502 freq_table[i] = p->op;
1507 mkdir_path(pathname_t *name, mode_t mode)
1509 char buf[NAME_MAX + 1];
1513 rval = mkdir(name->path, mode);
1514 if (rval >= 0 || errno != ENAMETOOLONG)
1516 separate_pathname(name, buf, &newname);
1517 if (chdir(buf) == 0) {
1518 rval = mkdir_path(&newname, mode);
1519 assert(chdir("..") == 0);
1521 free_pathname(&newname);
1526 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1528 char buf[NAME_MAX + 1];
1532 rval = mknod(name->path, mode, dev);
1533 if (rval >= 0 || errno != ENAMETOOLONG)
1535 separate_pathname(name, buf, &newname);
1536 if (chdir(buf) == 0) {
1537 rval = mknod_path(&newname, mode, dev);
1538 assert(chdir("..") == 0);
1540 free_pathname(&newname);
1545 namerandpad(int id, char *buf, int i)
1548 static int buckets[] =
1549 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1555 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1556 padmod = buckets[bucket] + 1 - i;
1559 padlen = (id ^ namerand) % padmod;
1561 memset(&buf[i], 'X', padlen);
1562 buf[i + padlen] = '\0';
1567 open_file_or_dir(pathname_t *name, int flags)
1571 fd = open_path(name, flags);
1574 if (fd == -1 && errno != EISDIR)
1576 /* Directories can not be opened in write mode nor direct mode. */
1577 flags &= ~(O_WRONLY | O_DIRECT);
1578 flags |= O_RDONLY | O_DIRECTORY;
1579 return open_path(name, flags);
1583 open_path(pathname_t *name, int oflag)
1585 char buf[NAME_MAX + 1];
1589 rval = open(name->path, oflag);
1590 if (rval >= 0 || errno != ENAMETOOLONG)
1592 separate_pathname(name, buf, &newname);
1593 if (chdir(buf) == 0) {
1594 rval = open_path(&newname, oflag);
1595 assert(chdir("..") == 0);
1597 free_pathname(&newname);
1602 opendir_path(pathname_t *name)
1604 char buf[NAME_MAX + 1];
1608 rval = opendir(name->path);
1609 if (rval || errno != ENAMETOOLONG)
1611 separate_pathname(name, buf, &newname);
1612 if (chdir(buf) == 0) {
1613 rval = opendir_path(&newname);
1614 assert(chdir("..") == 0);
1616 free_pathname(&newname);
1621 process_freq(char *arg)
1626 s = strchr(arg, '=');
1628 fprintf(stderr, "bad argument '%s'\n", arg);
1632 for (p = ops; p < ops_end; p++) {
1633 if (strcmp(arg, p->name) == 0) {
1638 fprintf(stderr, "can't find op type %s for -f\n", arg);
1643 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1645 char buf[NAME_MAX + 1];
1649 rval = readlink(name->path, lbuf, lbufsiz);
1650 if (rval >= 0 || errno != ENAMETOOLONG)
1652 separate_pathname(name, buf, &newname);
1653 if (chdir(buf) == 0) {
1654 rval = readlink_path(&newname, lbuf, lbufsiz);
1655 assert(chdir("..") == 0);
1657 free_pathname(&newname);
1662 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1664 char buf1[NAME_MAX + 1];
1665 char buf2[NAME_MAX + 1];
1667 pathname_t newname1;
1668 pathname_t newname2;
1672 rval = rename(name1->path, name2->path);
1674 rval = renameat2(AT_FDCWD, name1->path,
1675 AT_FDCWD, name2->path, mode);
1676 if (rval >= 0 || errno != ENAMETOOLONG)
1678 separate_pathname(name1, buf1, &newname1);
1679 separate_pathname(name2, buf2, &newname2);
1680 if (strcmp(buf1, buf2) == 0) {
1681 if (chdir(buf1) == 0) {
1682 rval = rename_path(&newname1, &newname2, mode);
1683 assert(chdir("..") == 0);
1686 if (strcmp(buf1, "..") == 0)
1688 else if (strcmp(buf2, "..") == 0)
1690 else if (strlen(buf1) == 0)
1692 else if (strlen(buf2) == 0)
1695 down1 = MAX(newname1.len, 3 + name2->len) <=
1696 MAX(3 + name1->len, newname2.len);
1698 free_pathname(&newname2);
1699 append_pathname(&newname2, "../");
1700 append_pathname(&newname2, name2->path);
1701 if (chdir(buf1) == 0) {
1702 rval = rename_path(&newname1, &newname2, mode);
1703 assert(chdir("..") == 0);
1706 free_pathname(&newname1);
1707 append_pathname(&newname1, "../");
1708 append_pathname(&newname1, name1->path);
1709 if (chdir(buf2) == 0) {
1710 rval = rename_path(&newname1, &newname2, mode);
1711 assert(chdir("..") == 0);
1715 free_pathname(&newname1);
1716 free_pathname(&newname2);
1721 rmdir_path(pathname_t *name)
1723 char buf[NAME_MAX + 1];
1727 rval = rmdir(name->path);
1728 if (rval >= 0 || errno != ENAMETOOLONG)
1730 separate_pathname(name, buf, &newname);
1731 if (chdir(buf) == 0) {
1732 rval = rmdir_path(&newname);
1733 assert(chdir("..") == 0);
1735 free_pathname(&newname);
1740 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1744 init_pathname(newname);
1745 slash = strchr(name->path, '/');
1746 if (slash == NULL) {
1751 strcpy(buf, name->path);
1753 append_pathname(newname, slash + 1);
1759 show_ops(int flag, char *lead_str)
1764 /* print in list form */
1767 for (p = ops; p < ops_end; p++) {
1768 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1769 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1770 x+=printf("%s ", p->name);
1773 } else if (flag == 0) {
1774 /* Table view style */
1776 for (f = 0, p = ops; p < ops_end; p++)
1782 for (p = ops; p < ops_end; p++) {
1783 if (flag != 0 || p->freq > 0) {
1784 if (lead_str != NULL)
1785 printf("%s", lead_str);
1786 printf("%20s %d/%d %s\n",
1787 p->name, p->freq, f,
1788 (p->iswrite == 0) ? " " : "write op");
1792 /* Command line style */
1793 if (lead_str != NULL)
1794 printf("%s", lead_str);
1795 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1797 for (p = ops; p < ops_end; p++)
1799 printf("-f %s=%d \\\n",p->name, p->freq);
1804 stat64_path(pathname_t *name, struct stat64 *sbuf)
1806 char buf[NAME_MAX + 1];
1810 rval = stat64(name->path, sbuf);
1811 if (rval >= 0 || errno != ENAMETOOLONG)
1813 separate_pathname(name, buf, &newname);
1814 if (chdir(buf) == 0) {
1815 rval = stat64_path(&newname, sbuf);
1816 assert(chdir("..") == 0);
1818 free_pathname(&newname);
1823 symlink_path(const char *name1, pathname_t *name)
1825 char buf[NAME_MAX + 1];
1829 if (!strcmp(name1, name->path)) {
1830 printf("yikes! %s %s\n", name1, name->path);
1834 rval = symlink(name1, name->path);
1835 if (rval >= 0 || errno != ENAMETOOLONG)
1837 separate_pathname(name, buf, &newname);
1838 if (chdir(buf) == 0) {
1839 rval = symlink_path(name1, &newname);
1840 assert(chdir("..") == 0);
1842 free_pathname(&newname);
1847 truncate64_path(pathname_t *name, off64_t length)
1849 char buf[NAME_MAX + 1];
1853 rval = truncate64(name->path, length);
1854 if (rval >= 0 || errno != ENAMETOOLONG)
1856 separate_pathname(name, buf, &newname);
1857 if (chdir(buf) == 0) {
1858 rval = truncate64_path(&newname, length);
1859 assert(chdir("..") == 0);
1861 free_pathname(&newname);
1866 unlink_path(pathname_t *name)
1868 char buf[NAME_MAX + 1];
1872 rval = unlink(name->path);
1873 if (rval >= 0 || errno != ENAMETOOLONG)
1875 separate_pathname(name, buf, &newname);
1876 if (chdir(buf) == 0) {
1877 rval = unlink_path(&newname);
1878 assert(chdir("..") == 0);
1880 free_pathname(&newname);
1887 printf("Usage: %s -H or\n", myprog);
1888 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1890 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1892 printf(" -c clean up the test directory after each run\n");
1893 printf(" -d dir specifies the base directory for operations\n");
1894 printf(" -e errtg specifies error injection stuff\n");
1895 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1896 printf(" the valid operation names are:\n");
1898 printf(" -i filenum get verbose output for this nth file object\n");
1899 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1900 printf(" *use 0 for infinite (default 1)\n");
1901 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1902 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1903 printf(" -o logfile specifies logfile name\n");
1904 printf(" -p nproc specifies the no. of processes (default 1)\n");
1905 printf(" -r specifies random name padding\n");
1906 printf(" -s seed specifies the seed for the random generator (default random)\n");
1907 printf(" -v specifies verbose mode\n");
1908 printf(" -w zeros frequencies of non-write operations\n");
1909 printf(" -x cmd execute command in the middle of operations\n");
1910 printf(" -z zeros frequencies of all operations\n");
1911 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1912 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1913 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1914 printf(" -H prints usage and exits\n");
1922 for (p = ops; p < ops_end; p++) {
1933 for (p = ops; p < ops_end; p++)
1937 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1939 opty_t btrfs_ops[] = {
1946 non_btrfs_freq(const char *path)
1949 #ifdef HAVE_BTRFSUTIL_H
1950 enum btrfs_util_error e;
1952 e = btrfs_util_is_subvolume(path);
1953 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1956 for (p = ops; p < ops_end; p++) {
1958 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1959 if (p->op == btrfs_ops[i]) {
1967 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1970 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1971 verifiable_log ? -1: (long)s->st_ino,
1972 (long)s->st_nlink, s->st_uid, s->st_gid,
1973 (long long) s->st_blocks, (long long) s->st_size);
1977 afsync_f(int opno, long r)
1985 struct iocb *iocbs[] = { &iocb };
1986 struct io_event event;
1989 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1991 printf("%d/%d: afsync - no filename\n", procid, opno);
1995 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1996 e = fd < 0 ? errno : 0;
2000 printf("%d/%d: afsync - open %s failed %d\n",
2001 procid, opno, f.path, e);
2006 io_prep_fsync(&iocb, fd);
2007 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2009 printf("%d/%d: afsync - io_submit %s %d\n",
2010 procid, opno, f.path, e);
2015 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2017 printf("%d/%d: afsync - io_getevents failed %d\n",
2026 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2033 allocsp_f(int opno, long r)
2038 struct xfs_flock64 fl;
2046 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2048 printf("%d/%d: allocsp - no filename\n", procid, opno);
2052 fd = open_path(&f, O_RDWR);
2053 e = fd < 0 ? errno : 0;
2057 printf("%d/%d: allocsp - open %s failed %d\n",
2058 procid, opno, f.path, e);
2062 if (fstat64(fd, &stb) < 0) {
2064 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2065 procid, opno, f.path, errno);
2070 inode_info(st, sizeof(st), &stb, v);
2071 lr = ((int64_t)random() << 32) + random();
2072 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2074 fl.l_whence = SEEK_SET;
2077 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2079 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2080 procid, opno, f.path, st, (long long)off, e);
2088 do_aio_rw(int opno, long r, int flags)
2092 struct dioattr diob;
2104 struct io_event event;
2105 struct iocb *iocbs[] = { &iocb };
2106 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2109 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2111 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2114 fd = open_path(&f, flags|O_DIRECT);
2115 e = fd < 0 ? errno : 0;
2119 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2120 procid, opno, f.path, e);
2123 if (fstat64(fd, &stb) < 0) {
2125 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2126 procid, opno, f.path, errno);
2129 inode_info(st, sizeof(st), &stb, v);
2130 if (!iswrite && stb.st_size == 0) {
2132 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2136 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2139 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2140 " fallback to stat()\n",
2141 procid, opno, f.path, st, errno);
2142 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2143 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2145 dio_env = getenv("XFS_DIO_MIN");
2147 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2148 align = (int64_t)diob.d_miniosz;
2149 lr = ((int64_t)random() << 32) + random();
2150 len = (random() % FILELEN_MAX) + 1;
2151 len -= (len % align);
2154 else if (len > diob.d_maxiosz)
2155 len = diob.d_maxiosz;
2156 buf = memalign(diob.d_mem, len);
2159 printf("%d/%d: do_aio_rw - memalign failed\n",
2165 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2166 off -= (off % align);
2168 memset(buf, nameseq & 0xff, len);
2169 io_prep_pwrite(&iocb, fd, buf, len, off);
2171 off = (off64_t)(lr % stb.st_size);
2172 off -= (off % align);
2173 io_prep_pread(&iocb, fd, buf, len, off);
2175 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2177 printf("%d/%d: %s - io_submit failed %d\n",
2178 procid, opno, iswrite ? "awrite" : "aread", e);
2181 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2183 printf("%d/%d: %s - io_getevents failed %d\n",
2184 procid, opno, iswrite ? "awrite" : "aread", e);
2188 e = event.res != len ? event.res2 : 0;
2190 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2191 procid, opno, iswrite ? "awrite" : "aread",
2192 f.path, st, (long long)off, (int)len, e);
2204 do_uring_rw(int opno, long r, int flags)
2216 struct io_uring_sqe *sqe;
2217 struct io_uring_cqe *cqe;
2219 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2225 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2227 printf("%d/%d: do_uring_rw - no filename\n", procid, opno);
2230 fd = open_path(&f, flags);
2231 e = fd < 0 ? errno : 0;
2235 printf("%d/%d: do_uring_rw - open %s failed %d\n",
2236 procid, opno, f.path, e);
2239 if (fstat64(fd, &stb) < 0) {
2241 printf("%d/%d: do_uring_rw - fstat64 %s failed %d\n",
2242 procid, opno, f.path, errno);
2245 inode_info(st, sizeof(st), &stb, v);
2246 if (!iswrite && stb.st_size == 0) {
2248 printf("%d/%d: do_uring_rw - %s%s zero size\n", procid, opno,
2252 sqe = io_uring_get_sqe(&ring);
2255 printf("%d/%d: do_uring_rw - io_uring_get_sqe failed\n",
2259 lr = ((int64_t)random() << 32) + random();
2260 len = (random() % FILELEN_MAX) + 1;
2264 printf("%d/%d: do_uring_rw - malloc failed\n",
2268 iovec.iov_base = buf;
2269 iovec.iov_len = len;
2271 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2273 memset(buf, nameseq & 0xff, len);
2274 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2276 off = (off64_t)(lr % stb.st_size);
2277 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2280 if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2282 printf("%d/%d: %s - io_uring_submit failed %d\n", procid, opno,
2283 iswrite ? "uring_write" : "uring_read", e);
2286 if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2288 printf("%d/%d: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2289 iswrite ? "uring_write" : "uring_read", e);
2293 printf("%d/%d: %s %s%s [%lld, %d(res=%d)] %d\n",
2294 procid, opno, iswrite ? "uring_write" : "uring_read",
2295 f.path, st, (long long)off, (int)len, cqe->res, e);
2296 io_uring_cqe_seen(&ring, cqe);
2308 aread_f(int opno, long r)
2311 do_aio_rw(opno, r, O_RDONLY);
2316 attr_remove_f(int opno, long r)
2321 char buf[XATTR_LIST_MAX];
2330 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2331 append_pathname(&f, ".");
2332 e = attr_list_path(&f, buf, sizeof(buf));
2335 total = attr_list_count(buf, e);
2338 printf("%d/%d: attr_remove - no attrs for %s\n",
2339 procid, opno, f.path);
2344 which = (int)(random() % total);
2348 while (bufname < bufend) {
2354 bufname += strlen(bufname) + 1;
2356 if (aname == NULL) {
2359 "%d/%d: attr_remove - name %d not found at %s\n",
2360 procid, opno, which, f.path);
2364 if (attr_remove_path(&f, aname) < 0)
2370 printf("%d/%d: attr_remove %s %s %d\n",
2371 procid, opno, f.path, aname, e);
2376 attr_set_f(int opno, long r)
2383 static int lengths[] = { 10, 100, 1000, 10000 };
2388 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2389 append_pathname(&f, ".");
2390 sprintf(aname, "a%x", nameseq++);
2391 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2392 len = (int)(random() % lengths[li]);
2396 memset(aval, nameseq & 0xff, len);
2397 if (attr_set_path(&f, aname, aval, len) < 0)
2404 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2410 awrite_f(int opno, long r)
2413 do_aio_rw(opno, r, O_WRONLY);
2418 bulkstat_f(int opno, long r)
2424 struct xfs_bstat *t;
2426 struct xfs_fsop_bulkreq bsr;
2429 nent = (r % 999) + 2;
2430 t = malloc(nent * sizeof(*t));
2431 fd = open(".", O_RDONLY);
2439 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2443 printf("%d/%d: bulkstat nent %d total %lld\n",
2444 procid, opno, nent, (long long)total);
2449 bulkstat1_f(int opno, long r)
2459 struct xfs_fsop_bulkreq bsr;
2462 good = random() & 1;
2464 /* use an inode we know exists */
2466 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2467 append_pathname(&f, ".");
2468 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2473 * pick a random inode
2475 * note this can generate kernel warning messages
2476 * since bulkstat_one will read the disk block that
2477 * would contain a given inode even if that disk
2478 * block doesn't contain inodes.
2480 * this is detected later, but not until after the
2481 * warning is displayed.
2483 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2489 fd = open(".", O_RDONLY);
2495 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2497 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2498 procid, opno, good?"real":"random",
2499 verifiable_log ? -1LL : (long long)ino, e);
2504 chown_f(int opno, long r)
2514 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2515 append_pathname(&f, ".");
2516 u = (uid_t)random();
2517 g = (gid_t)random();
2518 nbits = (int)(random() % idmodulo);
2519 u &= (1 << nbits) - 1;
2520 g &= (1 << nbits) - 1;
2521 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2524 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2528 /* reflink some arbitrary range of f1 to f2. */
2535 struct file_clone_range fcr;
2536 struct pathname fpath1;
2537 struct pathname fpath2;
2538 struct stat64 stat1;
2539 struct stat64 stat2;
2540 char inoinfo1[1024];
2541 char inoinfo2[1024];
2555 init_pathname(&fpath1);
2556 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2558 printf("%d/%d: clonerange read - no filename\n",
2563 init_pathname(&fpath2);
2564 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2566 printf("%d/%d: clonerange write - no filename\n",
2572 fd1 = open_path(&fpath1, O_RDONLY);
2573 e = fd1 < 0 ? errno : 0;
2577 printf("%d/%d: clonerange read - open %s failed %d\n",
2578 procid, opno, fpath1.path, e);
2582 fd2 = open_path(&fpath2, O_WRONLY);
2583 e = fd2 < 0 ? errno : 0;
2587 printf("%d/%d: clonerange write - open %s failed %d\n",
2588 procid, opno, fpath2.path, e);
2592 /* Get file stats */
2593 if (fstat64(fd1, &stat1) < 0) {
2595 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2596 procid, opno, fpath1.path, errno);
2599 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2601 if (fstat64(fd2, &stat2) < 0) {
2603 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2604 procid, opno, fpath2.path, errno);
2607 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2609 /* Calculate offsets */
2610 len = (random() % FILELEN_MAX) + 1;
2611 len &= ~(stat1.st_blksize - 1);
2613 len = stat1.st_blksize;
2614 if (len > stat1.st_size)
2615 len = stat1.st_size;
2617 lr = ((int64_t)random() << 32) + random();
2618 if (stat1.st_size == len)
2621 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2623 off1 &= ~(stat1.st_blksize - 1);
2626 * If srcfile == destfile, randomly generate destination ranges
2627 * until we find one that doesn't overlap the source range.
2629 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2631 lr = ((int64_t)random() << 32) + random();
2632 off2 = (off64_t)(lr % max_off2);
2634 off2 &= ~(stat2.st_blksize - 1);
2635 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2637 /* Clone data blocks */
2639 fcr.src_offset = off1;
2640 fcr.src_length = len;
2641 fcr.dest_offset = off2;
2643 ret = ioctl(fd2, FICLONERANGE, &fcr);
2644 e = ret < 0 ? errno : 0;
2646 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2648 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2649 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2652 printf(" error %d", e);
2661 free_pathname(&fpath2);
2663 free_pathname(&fpath1);
2667 /* copy some arbitrary range of f1 to f2. */
2673 #ifdef HAVE_COPY_FILE_RANGE
2674 struct pathname fpath1;
2675 struct pathname fpath2;
2676 struct stat64 stat1;
2677 struct stat64 stat2;
2678 char inoinfo1[1024];
2679 char inoinfo2[1024];
2697 init_pathname(&fpath1);
2698 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2700 printf("%d/%d: copyrange read - no filename\n",
2705 init_pathname(&fpath2);
2706 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2708 printf("%d/%d: copyrange write - no filename\n",
2714 fd1 = open_path(&fpath1, O_RDONLY);
2715 e = fd1 < 0 ? errno : 0;
2719 printf("%d/%d: copyrange read - open %s failed %d\n",
2720 procid, opno, fpath1.path, e);
2724 fd2 = open_path(&fpath2, O_WRONLY);
2725 e = fd2 < 0 ? errno : 0;
2729 printf("%d/%d: copyrange write - open %s failed %d\n",
2730 procid, opno, fpath2.path, e);
2734 /* Get file stats */
2735 if (fstat64(fd1, &stat1) < 0) {
2737 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2738 procid, opno, fpath1.path, errno);
2741 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2743 if (fstat64(fd2, &stat2) < 0) {
2745 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2746 procid, opno, fpath2.path, errno);
2749 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2751 /* Calculate offsets */
2752 len = (random() % FILELEN_MAX) + 1;
2754 len = stat1.st_blksize;
2755 if (len > stat1.st_size)
2756 len = stat1.st_size;
2758 lr = ((int64_t)random() << 32) + random();
2759 if (stat1.st_size == len)
2762 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2766 * If srcfile == destfile, randomly generate destination ranges
2767 * until we find one that doesn't overlap the source range.
2769 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2771 lr = ((int64_t)random() << 32) + random();
2772 off2 = (off64_t)(lr % max_off2);
2774 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2777 * Since len, off1 and off2 will be changed later, preserve their
2785 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2788 if (errno != EAGAIN || tries++ >= 300)
2790 } else if (ret > len || ret == 0)
2795 e = ret < 0 ? errno : 0;
2797 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2799 fpath1.path, inoinfo1,
2800 (long long)offset1, (long long)length,
2801 fpath2.path, inoinfo2,
2802 (long long)offset2, (long long)length);
2805 printf(" error %d", e);
2806 else if (len && ret > len)
2807 printf(" asked for %lld, copied %lld??\n",
2808 (long long)len, (long long)ret);
2817 free_pathname(&fpath2);
2819 free_pathname(&fpath1);
2823 /* dedupe some arbitrary range of f1 to f2...fn. */
2829 #ifdef FIDEDUPERANGE
2830 #define INFO_SZ 1024
2831 struct file_dedupe_range *fdr;
2832 struct pathname *fpath;
2833 struct stat64 *stat;
2845 if (flist[FT_REG].nfiles < 2)
2848 /* Pick somewhere between 2 and 128 files. */
2850 nr = random() % (flist[FT_REG].nfiles + 1);
2851 } while (nr < 2 || nr > 128);
2854 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2855 sizeof(struct file_dedupe_range));
2857 printf("%d/%d: line %d error %d\n",
2858 procid, opno, __LINE__, errno);
2861 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2862 sizeof(struct file_dedupe_range)));
2864 fpath = calloc(nr, sizeof(struct pathname));
2866 printf("%d/%d: line %d error %d\n",
2867 procid, opno, __LINE__, errno);
2871 stat = calloc(nr, sizeof(struct stat64));
2873 printf("%d/%d: line %d error %d\n",
2874 procid, opno, __LINE__, errno);
2878 info = calloc(nr, INFO_SZ);
2880 printf("%d/%d: line %d error %d\n",
2881 procid, opno, __LINE__, errno);
2885 off = calloc(nr, sizeof(off64_t));
2887 printf("%d/%d: line %d error %d\n",
2888 procid, opno, __LINE__, errno);
2892 v = calloc(nr, sizeof(int));
2894 printf("%d/%d: line %d error %d\n",
2895 procid, opno, __LINE__, errno);
2898 fd = calloc(nr, sizeof(int));
2900 printf("%d/%d: line %d error %d\n",
2901 procid, opno, __LINE__, errno);
2904 memset(fd, 0xFF, nr * sizeof(int));
2906 /* Get paths for all files */
2907 for (i = 0; i < nr; i++)
2908 init_pathname(&fpath[i]);
2910 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2912 printf("%d/%d: deduperange read - no filename\n",
2917 for (i = 1; i < nr; i++) {
2918 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2920 printf("%d/%d: deduperange write - no filename\n",
2927 fd[0] = open_path(&fpath[0], O_RDONLY);
2928 e = fd[0] < 0 ? errno : 0;
2932 printf("%d/%d: deduperange read - open %s failed %d\n",
2933 procid, opno, fpath[0].path, e);
2937 for (i = 1; i < nr; i++) {
2938 fd[i] = open_path(&fpath[i], O_WRONLY);
2939 e = fd[i] < 0 ? errno : 0;
2943 printf("%d/%d: deduperange write - open %s failed %d\n",
2944 procid, opno, fpath[i].path, e);
2949 /* Get file stats */
2950 if (fstat64(fd[0], &stat[0]) < 0) {
2952 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2953 procid, opno, fpath[0].path, errno);
2957 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2959 for (i = 1; i < nr; i++) {
2960 if (fstat64(fd[i], &stat[i]) < 0) {
2962 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2963 procid, opno, fpath[i].path, errno);
2966 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2969 /* Never try to dedupe more than half of the src file. */
2970 len = (random() % FILELEN_MAX) + 1;
2971 len &= ~(stat[0].st_blksize - 1);
2973 len = stat[0].st_blksize / 2;
2974 if (len > stat[0].st_size / 2)
2975 len = stat[0].st_size / 2;
2977 /* Calculate offsets */
2978 lr = ((int64_t)random() << 32) + random();
2979 if (stat[0].st_size == len)
2982 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2984 off[0] &= ~(stat[0].st_blksize - 1);
2987 * If srcfile == destfile[i], randomly generate destination ranges
2988 * until we find one that doesn't overlap the source range.
2990 for (i = 1; i < nr; i++) {
2994 lr = ((int64_t)random() << 32) + random();
2995 if (stat[i].st_size <= len)
2998 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
3000 off[i] &= ~(stat[i].st_blksize - 1);
3001 } while (stat[0].st_ino == stat[i].st_ino &&
3002 llabs(off[i] - off[0]) < len &&
3006 /* Clone data blocks */
3007 fdr->src_offset = off[0];
3008 fdr->src_length = len;
3009 fdr->dest_count = nr - 1;
3010 for (i = 1; i < nr; i++) {
3011 fdr->info[i - 1].dest_fd = fd[i];
3012 fdr->info[i - 1].dest_offset = off[i];
3015 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3016 e = ret < 0 ? errno : 0;
3018 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
3020 fpath[0].path, &info[0], (long long)off[0],
3023 printf(" error %d", e);
3029 for (i = 1; i < nr; i++) {
3030 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3032 printf("%d/%d: ...to %s%s [%lld,%lld]",
3034 fpath[i].path, &info[i * INFO_SZ],
3035 (long long)off[i], (long long)len);
3036 if (fdr->info[i - 1].status < 0)
3037 printf(" error %d", e);
3038 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3039 printf(" %llu bytes deduplicated",
3040 fdr->info[i - 1].bytes_deduped);
3041 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3042 printf(" differed");
3048 for (i = 0; i < nr; i++)
3052 for (i = 0; i < nr; i++)
3053 free_pathname(&fpath[i]);
3072 setxattr_f(int opno, long r)
3074 #ifdef XFS_XFLAG_EXTSIZE
3084 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3085 append_pathname(&f, ".");
3086 fd = open_path(&f, O_RDWR);
3087 e = fd < 0 ? errno : 0;
3092 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3093 nbits = (int)(random() % e);
3094 p &= (1 << nbits) - 1;
3096 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3098 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3101 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3108 splice_f(int opno, long r)
3110 struct pathname fpath1;
3111 struct pathname fpath2;
3112 struct stat64 stat1;
3113 struct stat64 stat2;
3114 char inoinfo1[1024];
3115 char inoinfo2[1024];
3119 loff_t offset1, offset2;
3126 ssize_t ret1 = 0, ret2 = 0;
3132 init_pathname(&fpath1);
3133 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3135 printf("%d/%d: splice read - no filename\n",
3140 init_pathname(&fpath2);
3141 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3143 printf("%d/%d: splice write - no filename\n",
3149 fd1 = open_path(&fpath1, O_RDONLY);
3150 e = fd1 < 0 ? errno : 0;
3154 printf("%d/%d: splice read - open %s failed %d\n",
3155 procid, opno, fpath1.path, e);
3159 fd2 = open_path(&fpath2, O_WRONLY);
3160 e = fd2 < 0 ? errno : 0;
3164 printf("%d/%d: splice write - open %s failed %d\n",
3165 procid, opno, fpath2.path, e);
3169 /* Get file stats */
3170 if (fstat64(fd1, &stat1) < 0) {
3172 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3173 procid, opno, fpath1.path, errno);
3176 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3178 if (fstat64(fd2, &stat2) < 0) {
3180 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3181 procid, opno, fpath2.path, errno);
3184 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3186 /* Calculate offsets */
3187 len = (random() % FILELEN_MAX) + 1;
3189 len = stat1.st_blksize;
3190 if (len > stat1.st_size)
3191 len = stat1.st_size;
3193 lr = ((int64_t)random() << 32) + random();
3194 if (stat1.st_size == len)
3197 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3201 * splice can overlap write, so the offset of the target file can be
3202 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3203 * past the current dest file EOF
3205 lr = ((int64_t)random() << 32) + random();
3206 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3209 * Since len, off1 and off2 will be changed later, preserve their
3216 /* Pipe initialize */
3217 if (pipe(filedes) < 0) {
3219 printf("%d/%d: splice - pipe failed %d\n",
3220 procid, opno, errno);
3228 /* move to pipe buffer */
3229 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3235 /* move from pipe buffer to dst file */
3237 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3250 if (ret1 < 0 || ret2 < 0)
3255 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3257 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3258 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3260 if (length && length > total)
3261 printf(" asked for %lld, spliced %lld??\n",
3262 (long long)length, (long long)total);
3273 free_pathname(&fpath2);
3275 free_pathname(&fpath1);
3279 creat_f(int opno, long r)
3294 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3299 e1 = (random() % 100);
3300 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3302 if (type == FT_RTF) /* rt always gets an extsize */
3303 extsize = (random() % 10) + 1;
3304 else if (e1 < 10) /* one-in-ten get an extsize */
3305 extsize = random() % 1024;
3309 e = generate_fname(fep, type, &f, &id, &v);
3313 (void)fent_to_name(&f, fep);
3314 printf("%d/%d: creat - no filename from %s\n",
3315 procid, opno, f.path);
3320 fd = creat_path(&f, 0666);
3321 e = fd < 0 ? errno : 0;
3326 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3327 if (type == FT_RTF) {
3328 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3329 a.fsx_extsize = extsize *
3330 geom.rtextsize * geom.blocksize;
3332 } else if (extsize) {
3333 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3334 a.fsx_extsize = extsize * geom.blocksize;
3337 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3340 add_to_flist(type, id, parid, 0);
3344 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3345 extsize ? a.fsx_extsize : 0, e, e1);
3346 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3352 dread_f(int opno, long r)
3356 struct dioattr diob;
3369 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3371 printf("%d/%d: dread - no filename\n", procid, opno);
3375 fd = open_path(&f, O_RDONLY|O_DIRECT);
3376 e = fd < 0 ? errno : 0;
3380 printf("%d/%d: dread - open %s failed %d\n",
3381 procid, opno, f.path, e);
3385 if (fstat64(fd, &stb) < 0) {
3387 printf("%d/%d: dread - fstat64 %s failed %d\n",
3388 procid, opno, f.path, errno);
3393 inode_info(st, sizeof(st), &stb, v);
3394 if (stb.st_size == 0) {
3396 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3402 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3405 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3406 " fallback to stat()\n",
3407 procid, opno, f.path, st, errno);
3408 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3409 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3412 dio_env = getenv("XFS_DIO_MIN");
3414 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3416 align = (int64_t)diob.d_miniosz;
3417 lr = ((int64_t)random() << 32) + random();
3418 off = (off64_t)(lr % stb.st_size);
3419 off -= (off % align);
3420 lseek64(fd, off, SEEK_SET);
3421 len = (random() % FILELEN_MAX) + 1;
3422 len -= (len % align);
3425 else if (len > diob.d_maxiosz)
3426 len = diob.d_maxiosz;
3427 buf = memalign(diob.d_mem, len);
3428 e = read(fd, buf, len) < 0 ? errno : 0;
3431 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3432 procid, opno, f.path, st, (long long)off, (int)len, e);
3438 dwrite_f(int opno, long r)
3442 struct dioattr diob;
3455 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3457 printf("%d/%d: dwrite - no filename\n", procid, opno);
3461 fd = open_path(&f, O_WRONLY|O_DIRECT);
3462 e = fd < 0 ? errno : 0;
3466 printf("%d/%d: dwrite - open %s failed %d\n",
3467 procid, opno, f.path, e);
3471 if (fstat64(fd, &stb) < 0) {
3473 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3474 procid, opno, f.path, errno);
3479 inode_info(st, sizeof(st), &stb, v);
3480 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3482 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3483 " %s%s return %d, fallback to stat()\n",
3484 procid, opno, f.path, st, errno);
3485 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3486 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3489 dio_env = getenv("XFS_DIO_MIN");
3491 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3493 align = (int64_t)diob.d_miniosz;
3494 lr = ((int64_t)random() << 32) + random();
3495 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3496 off -= (off % align);
3497 lseek64(fd, off, SEEK_SET);
3498 len = (random() % FILELEN_MAX) + 1;
3499 len -= (len % align);
3502 else if (len > diob.d_maxiosz)
3503 len = diob.d_maxiosz;
3504 buf = memalign(diob.d_mem, len);
3506 lseek64(fd, off, SEEK_SET);
3507 memset(buf, nameseq & 0xff, len);
3508 e = write(fd, buf, len) < 0 ? errno : 0;
3511 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3512 procid, opno, f.path, st, (long long)off, (int)len, e);
3518 #ifdef HAVE_LINUX_FALLOC_H
3519 struct print_flags falloc_flags [] = {
3520 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3521 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3522 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3523 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3524 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3525 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3529 #define translate_falloc_flags(mode) \
3530 ({translate_flags(mode, "|", falloc_flags);})
3534 do_fallocate(int opno, long r, int mode)
3536 #ifdef HAVE_LINUX_FALLOC_H
3548 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3550 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3554 fd = open_path(&f, O_RDWR);
3557 printf("%d/%d: do_fallocate - open %s failed %d\n",
3558 procid, opno, f.path, errno);
3563 if (fstat64(fd, &stb) < 0) {
3565 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3566 procid, opno, f.path, errno);
3571 inode_info(st, sizeof(st), &stb, v);
3572 lr = ((int64_t)random() << 32) + random();
3573 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3575 len = (off64_t)(random() % (1024 * 1024));
3577 * Collapse/insert range requires off and len to be block aligned,
3578 * make it more likely to be the case.
3580 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3582 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3583 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3585 mode |= FALLOC_FL_KEEP_SIZE & random();
3586 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3588 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3589 procid, opno, translate_falloc_flags(mode),
3590 f.path, st, (long long)off, (long long)len, e);
3597 fallocate_f(int opno, long r)
3599 #ifdef HAVE_LINUX_FALLOC_H
3600 do_fallocate(opno, r, 0);
3605 fdatasync_f(int opno, long r)
3613 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3615 printf("%d/%d: fdatasync - no filename\n",
3620 fd = open_path(&f, O_WRONLY);
3621 e = fd < 0 ? errno : 0;
3625 printf("%d/%d: fdatasync - open %s failed %d\n",
3626 procid, opno, f.path, e);
3630 e = fdatasync(fd) < 0 ? errno : 0;
3632 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3637 #ifdef HAVE_LINUX_FIEMAP_H
3638 struct print_flags fiemap_flags[] = {
3639 { FIEMAP_FLAG_SYNC, "SYNC"},
3640 { FIEMAP_FLAG_XATTR, "XATTR"},
3644 #define translate_fiemap_flags(mode) \
3645 ({translate_flags(mode, "|", fiemap_flags);})
3649 fiemap_f(int opno, long r)
3651 #ifdef HAVE_LINUX_FIEMAP_H
3661 struct fiemap *fiemap;
3664 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3666 printf("%d/%d: fiemap - no filename\n", procid, opno);
3670 fd = open_path(&f, O_RDWR);
3671 e = fd < 0 ? errno : 0;
3675 printf("%d/%d: fiemap - open %s failed %d\n",
3676 procid, opno, f.path, e);
3680 if (fstat64(fd, &stb) < 0) {
3682 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3683 procid, opno, f.path, errno);
3688 inode_info(st, sizeof(st), &stb, v);
3689 blocks_to_map = random() & 0xffff;
3690 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3691 (blocks_to_map * sizeof(struct fiemap_extent)));
3694 printf("%d/%d: malloc failed \n", procid, opno);
3699 lr = ((int64_t)random() << 32) + random();
3700 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3702 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3703 fiemap->fm_extent_count = blocks_to_map;
3704 fiemap->fm_mapped_extents = random() & 0xffff;
3705 fiemap->fm_start = off;
3706 fiemap->fm_length = ((int64_t)random() << 32) + random();
3708 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3710 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3711 procid, opno, f.path, st, (long long)fiemap->fm_start,
3712 (long long) fiemap->fm_length,
3713 translate_fiemap_flags(fiemap->fm_flags), e);
3721 freesp_f(int opno, long r)
3726 struct xfs_flock64 fl;
3734 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3736 printf("%d/%d: freesp - no filename\n", procid, opno);
3740 fd = open_path(&f, O_RDWR);
3741 e = fd < 0 ? errno : 0;
3745 printf("%d/%d: freesp - open %s failed %d\n",
3746 procid, opno, f.path, e);
3750 if (fstat64(fd, &stb) < 0) {
3752 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3753 procid, opno, f.path, errno);
3758 inode_info(st, sizeof(st), &stb, v);
3759 lr = ((int64_t)random() << 32) + random();
3760 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3762 fl.l_whence = SEEK_SET;
3765 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3767 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3768 procid, opno, f.path, st, (long long)off, e);
3774 fsync_f(int opno, long r)
3782 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3784 printf("%d/%d: fsync - no filename\n", procid, opno);
3788 fd = open_file_or_dir(&f, O_WRONLY);
3789 e = fd < 0 ? errno : 0;
3793 printf("%d/%d: fsync - open %s failed %d\n",
3794 procid, opno, f.path, e);
3798 e = fsync(fd) < 0 ? errno : 0;
3800 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3806 gen_random_string(int len)
3808 static const char charset[] = "0123456789"
3809 "abcdefghijklmnopqrstuvwxyz"
3810 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3821 for (i = 0; i < len; i++)
3822 s[i] = charset[random() % sizeof(charset)];
3828 getattr_f(int opno, long r)
3837 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3838 append_pathname(&f, ".");
3839 fd = open_path(&f, O_RDWR);
3840 e = fd < 0 ? errno : 0;
3843 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3845 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3851 getdents_f(int opno, long r)
3858 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3859 append_pathname(&f, ".");
3860 dir = opendir_path(&f);
3864 printf("%d/%d: getdents - can't open %s\n",
3865 procid, opno, f.path);
3869 while (readdir64(dir) != NULL)
3872 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3878 getfattr_f(int opno, long r)
3884 char name[XATTR_NAME_BUF_SIZE];
3890 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3892 printf("%d/%d: getfattr - no filename\n", procid, opno);
3898 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3899 * to read a xattr that doesn't exist (fgetxattr should fail with
3900 * errno set to ENOATTR (61) in this case).
3902 if (fep->xattr_counter > 0)
3903 xattr_num = (random() % fep->xattr_counter) + 1;
3907 e = generate_xattr_name(xattr_num, name, sizeof(name));
3909 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3910 procid, opno, f.path, e);
3914 value_len = getxattr(f.path, name, NULL, 0);
3915 if (value_len < 0) {
3917 printf("%d/%d: getfattr file %s name %s failed %d\n",
3918 procid, opno, f.path, name, errno);
3922 /* A xattr without value.*/
3923 if (value_len == 0) {
3928 value = malloc(value_len);
3931 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3932 procid, opno, f.path, value_len);
3936 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3939 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3940 procid, opno, f.path, name, value_len, e);
3947 link_f(int opno, long r)
3961 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3963 printf("%d/%d: link - no file\n", procid, opno);
3967 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3973 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3977 (void)fent_to_name(&l, fep);
3978 printf("%d/%d: link - no filename from %s\n",
3979 procid, opno, l.path);
3985 e = link_path(&f, &l) < 0 ? errno : 0;
3988 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3990 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3992 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3999 listfattr_f(int opno, long r)
4005 char *buffer = NULL;
4009 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4011 printf("%d/%d: listfattr - no filename\n", procid, opno);
4016 e = listxattr(f.path, NULL, 0);
4019 printf("%d/%d: listfattr %s failed %d\n",
4020 procid, opno, f.path, errno);
4024 if (buffer_len == 0) {
4026 printf("%d/%d: listfattr %s - has no extended attributes\n",
4027 procid, opno, f.path);
4031 buffer = malloc(buffer_len);
4034 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
4035 procid, opno, f.path, buffer_len);
4039 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4041 printf("%d/%d: listfattr %s buffer length %d %d\n",
4042 procid, opno, f.path, buffer_len, e);
4049 mkdir_f(int opno, long r)
4059 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4064 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4068 (void)fent_to_name(&f, fep);
4069 printf("%d/%d: mkdir - no filename from %s\n",
4070 procid, opno, f.path);
4075 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4078 add_to_flist(FT_DIR, id, parid, 0);
4080 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
4081 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4087 mknod_f(int opno, long r)
4097 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4102 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4106 (void)fent_to_name(&f, fep);
4107 printf("%d/%d: mknod - no filename from %s\n",
4108 procid, opno, f.path);
4113 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4116 add_to_flist(FT_DEV, id, parid, 0);
4118 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
4119 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4124 #ifdef HAVE_SYS_MMAN_H
4125 struct print_flags mmap_flags[] = {
4126 { MAP_SHARED, "SHARED"},
4127 { MAP_PRIVATE, "PRIVATE"},
4131 #define translate_mmap_flags(flags) \
4132 ({translate_flags(flags, "|", mmap_flags);})
4136 do_mmap(int opno, long r, int prot)
4138 #ifdef HAVE_SYS_MMAN_H
4150 sigjmp_buf sigbus_jmpbuf;
4153 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4155 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4159 fd = open_path(&f, O_RDWR);
4160 e = fd < 0 ? errno : 0;
4164 printf("%d/%d: do_mmap - open %s failed %d\n",
4165 procid, opno, f.path, e);
4169 if (fstat64(fd, &stb) < 0) {
4171 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4172 procid, opno, f.path, errno);
4177 inode_info(st, sizeof(st), &stb, v);
4178 if (stb.st_size == 0) {
4180 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4187 lr = ((int64_t)random() << 32) + random();
4188 off = (off64_t)(lr % stb.st_size);
4189 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4190 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4192 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4193 addr = mmap(NULL, len, prot, flags, fd, off);
4194 e = (addr == MAP_FAILED) ? errno : 0;
4197 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4198 procid, opno, f.path, st, (long long)off,
4199 (int)len, translate_mmap_flags(flags), e);
4205 if (prot & PROT_WRITE) {
4206 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4207 sigbus_jmp = &sigbus_jmpbuf;
4208 memset(addr, nameseq & 0xff, len);
4212 if ((buf = malloc(len)) != NULL) {
4213 memcpy(buf, addr, len);
4218 /* set NULL to stop other functions from doing siglongjmp */
4222 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4223 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4224 f.path, st, (long long)off, (int)len,
4225 translate_mmap_flags(flags),
4226 (e == 0) ? "0" : "Bus error");
4234 mread_f(int opno, long r)
4236 #ifdef HAVE_SYS_MMAN_H
4237 do_mmap(opno, r, PROT_READ);
4242 mwrite_f(int opno, long r)
4244 #ifdef HAVE_SYS_MMAN_H
4245 do_mmap(opno, r, PROT_WRITE);
4250 punch_f(int opno, long r)
4252 #ifdef HAVE_LINUX_FALLOC_H
4253 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4258 zero_f(int opno, long r)
4260 #ifdef HAVE_LINUX_FALLOC_H
4261 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4266 collapse_f(int opno, long r)
4268 #ifdef HAVE_LINUX_FALLOC_H
4269 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4274 insert_f(int opno, long r)
4276 #ifdef HAVE_LINUX_FALLOC_H
4277 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4282 read_f(int opno, long r)
4296 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4298 printf("%d/%d: read - no filename\n", procid, opno);
4302 fd = open_path(&f, O_RDONLY);
4303 e = fd < 0 ? errno : 0;
4307 printf("%d/%d: read - open %s failed %d\n",
4308 procid, opno, f.path, e);
4312 if (fstat64(fd, &stb) < 0) {
4314 printf("%d/%d: read - fstat64 %s failed %d\n",
4315 procid, opno, f.path, errno);
4320 inode_info(st, sizeof(st), &stb, v);
4321 if (stb.st_size == 0) {
4323 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4329 lr = ((int64_t)random() << 32) + random();
4330 off = (off64_t)(lr % stb.st_size);
4331 lseek64(fd, off, SEEK_SET);
4332 len = (random() % FILELEN_MAX) + 1;
4334 e = read(fd, buf, len) < 0 ? errno : 0;
4337 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4338 procid, opno, f.path, st, (long long)off, (int)len, e);
4344 readlink_f(int opno, long r)
4352 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4354 printf("%d/%d: readlink - no filename\n", procid, opno);
4358 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4361 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4366 readv_f(int opno, long r)
4378 struct iovec *iov = NULL;
4385 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4387 printf("%d/%d: readv - no filename\n", procid, opno);
4391 fd = open_path(&f, O_RDONLY);
4392 e = fd < 0 ? errno : 0;
4396 printf("%d/%d: readv - open %s failed %d\n",
4397 procid, opno, f.path, e);
4401 if (fstat64(fd, &stb) < 0) {
4403 printf("%d/%d: readv - fstat64 %s failed %d\n",
4404 procid, opno, f.path, errno);
4409 inode_info(st, sizeof(st), &stb, v);
4410 if (stb.st_size == 0) {
4412 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4418 lr = ((int64_t)random() << 32) + random();
4419 off = (off64_t)(lr % stb.st_size);
4420 lseek64(fd, off, SEEK_SET);
4421 len = (random() % FILELEN_MAX) + 1;
4424 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4425 iov = calloc(iovcnt, sizeof(struct iovec));
4426 iovl = len / iovcnt;
4428 for (i=0; i<iovcnt; i++) {
4429 (iov + i)->iov_base = (buf + iovb);
4430 (iov + i)->iov_len = iovl;
4434 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4437 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4438 procid, opno, f.path, st, (long long)off, (int)iovl,
4445 removefattr_f(int opno, long r)
4451 char name[XATTR_NAME_BUF_SIZE];
4455 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4457 printf("%d/%d: removefattr - no filename\n", procid, opno);
4463 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4464 * remove a xattr that doesn't exist (fremovexattr should fail with
4465 * errno set to ENOATTR (61) in this case).
4467 if (fep->xattr_counter > 0)
4468 xattr_num = (random() % fep->xattr_counter) + 1;
4472 e = generate_xattr_name(xattr_num, name, sizeof(name));
4474 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4475 procid, opno, f.path, e);
4479 e = removexattr(f.path, name) < 0 ? errno : 0;
4481 printf("%d/%d: removefattr file %s name %s %d\n",
4482 procid, opno, f.path, name, e);
4487 struct print_flags renameat2_flags [] = {
4488 { RENAME_NOREPLACE, "NOREPLACE"},
4489 { RENAME_EXCHANGE, "EXCHANGE"},
4490 { RENAME_WHITEOUT, "WHITEOUT"},
4494 #define translate_renameat2_flags(mode) \
4495 ({translate_flags(mode, "|", renameat2_flags);})
4498 do_renameat2(int opno, long r, int mode)
4514 /* get an existing path for the source of the rename */
4516 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4517 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4519 printf("%d/%d: rename - no source filename\n",
4526 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4527 * order to maintain filelist/filename integrity, we should
4528 * restrict exchange operation to files of the same type.
4530 if (mode == RENAME_EXCHANGE) {
4531 which = 1 << (flp - flist);
4532 init_pathname(&newf);
4533 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4535 printf("%d/%d: rename - no target filename\n",
4537 free_pathname(&newf);
4541 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4542 fents_ancestor_check(dfep, fep))) {
4544 printf("%d/%d: rename(REXCHANGE) %s and %s "
4545 "have ancestor-descendant relationship\n",
4546 procid, opno, f.path, newf.path);
4547 free_pathname(&newf);
4553 parid = dfep->parent;
4556 * Get an existing directory for the destination parent
4559 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4566 * Generate a new path using an existing parent directory
4569 init_pathname(&newf);
4570 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4574 (void)fent_to_name(&f, dfep);
4575 printf("%d/%d: rename - no filename from %s\n",
4576 procid, opno, f.path);
4578 free_pathname(&newf);
4583 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4586 int xattr_counter = fep->xattr_counter;
4587 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4588 int ft = flp - flist;
4591 oldparid = fep->parent;
4594 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4595 * old parent id for the others.
4597 if (ft == FT_DIR || ft == FT_SUBVOL)
4598 fix_parent(oldid, id, swap);
4600 if (mode == RENAME_WHITEOUT) {
4601 fep->xattr_counter = 0;
4602 add_to_flist(flp - flist, id, parid, xattr_counter);
4603 } else if (mode == RENAME_EXCHANGE) {
4604 fep->xattr_counter = dfep->xattr_counter;
4605 dfep->xattr_counter = xattr_counter;
4607 del_from_flist(flp - flist, fep - flp->fents);
4608 add_to_flist(flp - flist, id, parid, xattr_counter);
4612 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4613 opno, translate_renameat2_flags(mode), f.path,
4616 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4617 procid, opno, oldid, oldparid);
4618 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4619 procid, opno, id, parid);
4622 free_pathname(&newf);
4627 rename_f(int opno, long r)
4629 do_renameat2(opno, r, 0);
4633 rnoreplace_f(int opno, long r)
4635 do_renameat2(opno, r, RENAME_NOREPLACE);
4639 rexchange_f(int opno, long r)
4641 do_renameat2(opno, r, RENAME_EXCHANGE);
4645 rwhiteout_f(int opno, long r)
4647 do_renameat2(opno, r, RENAME_WHITEOUT);
4651 resvsp_f(int opno, long r)
4656 struct xfs_flock64 fl;
4664 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4666 printf("%d/%d: resvsp - no filename\n", procid, opno);
4670 fd = open_path(&f, O_RDWR);
4671 e = fd < 0 ? errno : 0;
4675 printf("%d/%d: resvsp - open %s failed %d\n",
4676 procid, opno, f.path, e);
4680 if (fstat64(fd, &stb) < 0) {
4682 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4683 procid, opno, f.path, errno);
4688 inode_info(st, sizeof(st), &stb, v);
4689 lr = ((int64_t)random() << 32) + random();
4690 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4692 fl.l_whence = SEEK_SET;
4694 fl.l_len = (off64_t)(random() % (1024 * 1024));
4695 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4697 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4698 procid, opno, f.path, st,
4699 (long long)off, (long long)fl.l_len, e);
4705 rmdir_f(int opno, long r)
4715 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4717 printf("%d/%d: rmdir - no directory\n", procid, opno);
4721 e = rmdir_path(&f) < 0 ? errno : 0;
4725 oldparid = fep->parent;
4726 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4729 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4731 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4732 procid, opno, oldid, oldparid);
4738 setattr_f(int opno, long r)
4747 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4748 append_pathname(&f, ".");
4749 fd = open_path(&f, O_RDWR);
4750 e = fd < 0 ? errno : 0;
4753 fl = attr_mask & (uint)random();
4754 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4756 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4762 setfattr_f(int opno, long r)
4769 char name[XATTR_NAME_BUF_SIZE];
4775 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4777 printf("%d/%d: setfattr - no filename\n", procid, opno);
4782 if ((fep->xattr_counter > 0) && (random() % 2)) {
4784 * Use an existing xattr name for replacing its value or
4785 * create again a xattr that was previously deleted.
4787 xattr_num = (random() % fep->xattr_counter) + 1;
4789 flag = XATTR_REPLACE;
4791 /* Use a new xattr name. */
4792 xattr_num = fep->xattr_counter + 1;
4794 * Don't always use the create flag because even if our xattr
4795 * counter is 0, we may still have xattrs associated to this
4796 * file (this happens when xattrs are added to a file through
4797 * one of its other hard links), so we can end up updating an
4798 * existing xattr too.
4801 flag = XATTR_CREATE;
4805 * The maximum supported value size depends on the filesystem
4806 * implementation, but 100 bytes is a safe value for most filesystems
4809 value_len = random() % 101;
4810 value = gen_random_string(value_len);
4811 if (!value && value_len > 0) {
4813 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4814 procid, opno, f.path, value_len);
4817 e = generate_xattr_name(xattr_num, name, sizeof(name));
4819 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4820 procid, opno, f.path, e);
4824 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4826 fep->xattr_counter++;
4828 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4829 procid, opno, f.path, name, xattr_flag_to_string(flag),
4837 snapshot_f(int opno, long r)
4839 #ifdef HAVE_BTRFSUTIL_H
4840 enum btrfs_util_error e;
4851 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4853 printf("%d/%d: snapshot - no subvolume\n", procid,
4858 init_pathname(&newf);
4860 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4864 (void)fent_to_name(&f, fep);
4865 printf("%d/%d: snapshot - no filename from %s\n",
4866 procid, opno, f.path);
4871 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4872 if (e == BTRFS_UTIL_OK)
4873 add_to_flist(FT_SUBVOL, id, parid, 0);
4875 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4876 f.path, newf.path, e, btrfs_util_strerror(e));
4877 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4880 free_pathname(&newf);
4886 stat_f(int opno, long r)
4894 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4896 printf("%d/%d: stat - no entries\n", procid, opno);
4900 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4903 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4908 subvol_create_f(int opno, long r)
4910 #ifdef HAVE_BTRFSUTIL_H
4911 enum btrfs_util_error e;
4921 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4925 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4929 (void)fent_to_name(&f, fep);
4930 printf("%d/%d: subvol_create - no filename from %s\n",
4931 procid, opno, f.path);
4936 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4937 if (e == BTRFS_UTIL_OK)
4938 add_to_flist(FT_SUBVOL, id, parid, 0);
4940 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4941 f.path, e, btrfs_util_strerror(e));
4942 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4950 subvol_delete_f(int opno, long r)
4952 #ifdef HAVE_BTRFSUTIL_H
4953 enum btrfs_util_error e;
4961 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4963 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4968 e = btrfs_util_delete_subvolume(f.path, 0);
4970 if (e == BTRFS_UTIL_OK) {
4972 oldparid = fep->parent;
4973 delete_subvol_children(oldid);
4974 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4977 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4978 e, btrfs_util_strerror(e));
4979 if (e == BTRFS_UTIL_OK)
4980 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4981 procid, opno, oldid, oldparid);
4988 symlink_f(int opno, long r)
5001 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5006 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5010 (void)fent_to_name(&f, fep);
5011 printf("%d/%d: symlink - no filename from %s\n",
5012 procid, opno, f.path);
5017 len = (int)(random() % PATH_MAX);
5018 val = malloc(len + 1);
5020 memset(val, 'x', len);
5022 for (i = 10; i < len - 1; i += 10)
5024 e = symlink_path(val, &f) < 0 ? errno : 0;
5027 add_to_flist(FT_SYM, id, parid, 0);
5030 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
5031 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5038 sync_f(int opno, long r)
5042 printf("%d/%d: sync\n", procid, opno);
5046 truncate_f(int opno, long r)
5057 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5059 printf("%d/%d: truncate - no filename\n", procid, opno);
5063 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5067 printf("%d/%d: truncate - stat64 %s failed %d\n",
5068 procid, opno, f.path, e);
5072 inode_info(st, sizeof(st), &stb, v);
5073 lr = ((int64_t)random() << 32) + random();
5074 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5076 e = truncate64_path(&f, off) < 0 ? errno : 0;
5079 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
5080 st, (long long)off, e);
5085 unlink_f(int opno, long r)
5096 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5098 printf("%d/%d: unlink - no file\n", procid, opno);
5102 e = unlink_path(&f) < 0 ? errno : 0;
5106 oldparid = fep->parent;
5107 del_from_flist(flp - flist, fep - flp->fents);
5110 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
5112 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
5113 procid, opno, oldid, oldparid);
5119 unresvsp_f(int opno, long r)
5124 struct xfs_flock64 fl;
5132 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5134 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5138 fd = open_path(&f, O_RDWR);
5139 e = fd < 0 ? errno : 0;
5143 printf("%d/%d: unresvsp - open %s failed %d\n",
5144 procid, opno, f.path, e);
5148 if (fstat64(fd, &stb) < 0) {
5150 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5151 procid, opno, f.path, errno);
5156 inode_info(st, sizeof(st), &stb, v);
5157 lr = ((int64_t)random() << 32) + random();
5158 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5160 fl.l_whence = SEEK_SET;
5162 fl.l_len = (off64_t)(random() % (1 << 20));
5163 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5165 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5166 procid, opno, f.path, st,
5167 (long long)off, (long long)fl.l_len, e);
5173 uring_read_f(int opno, long r)
5176 do_uring_rw(opno, r, O_RDONLY);
5181 uring_write_f(int opno, long r)
5184 do_uring_rw(opno, r, O_WRONLY);
5189 write_f(int opno, long r)
5203 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5205 printf("%d/%d: write - no filename\n", procid, opno);
5209 fd = open_path(&f, O_WRONLY);
5210 e = fd < 0 ? errno : 0;
5214 printf("%d/%d: write - open %s failed %d\n",
5215 procid, opno, f.path, e);
5219 if (fstat64(fd, &stb) < 0) {
5221 printf("%d/%d: write - fstat64 %s failed %d\n",
5222 procid, opno, f.path, errno);
5227 inode_info(st, sizeof(st), &stb, v);
5228 lr = ((int64_t)random() << 32) + random();
5229 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5231 lseek64(fd, off, SEEK_SET);
5232 len = (random() % FILELEN_MAX) + 1;
5234 memset(buf, nameseq & 0xff, len);
5235 e = write(fd, buf, len) < 0 ? errno : 0;
5238 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5239 procid, opno, f.path, st, (long long)off, (int)len, e);
5245 writev_f(int opno, long r)
5257 struct iovec *iov = NULL;
5264 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5266 printf("%d/%d: writev - no filename\n", procid, opno);
5270 fd = open_path(&f, O_WRONLY);
5271 e = fd < 0 ? errno : 0;
5275 printf("%d/%d: writev - open %s failed %d\n",
5276 procid, opno, f.path, e);
5280 if (fstat64(fd, &stb) < 0) {
5282 printf("%d/%d: writev - fstat64 %s failed %d\n",
5283 procid, opno, f.path, errno);
5288 inode_info(st, sizeof(st), &stb, v);
5289 lr = ((int64_t)random() << 32) + random();
5290 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5292 lseek64(fd, off, SEEK_SET);
5293 len = (random() % FILELEN_MAX) + 1;
5295 memset(buf, nameseq & 0xff, len);
5297 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5298 iov = calloc(iovcnt, sizeof(struct iovec));
5299 iovl = len / iovcnt;
5301 for (i=0; i<iovcnt; i++) {
5302 (iov + i)->iov_base = (buf + iovb);
5303 (iov + i)->iov_len = iovl;
5307 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5311 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5312 procid, opno, f.path, st, (long long)off, (int)iovl,
5319 xattr_flag_to_string(int flag)
5321 if (flag == XATTR_CREATE)
5323 if (flag == XATTR_REPLACE)