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_ATTR_ATTRIBUTES_H
18 #include <attr/attributes.h>
20 #ifdef HAVE_LINUX_FIEMAP_H
21 #include <linux/fiemap.h>
23 #ifndef HAVE_ATTR_LIST
24 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
26 #ifdef HAVE_SYS_PRCTL_H
27 #include <sys/prctl.h>
36 #define URING_ENTRIES 1
38 bool have_io_uring; /* to indicate runtime availability */
40 #include <sys/syscall.h>
41 #include <sys/xattr.h>
43 #ifndef FS_IOC_GETFLAGS
44 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
46 #ifndef FS_IOC_SETFLAGS
47 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
51 #define XFS_ERRTAG_MAX 17
52 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
53 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
58 #ifndef HAVE_RENAMEAT2
59 #if !defined(SYS_renameat2) && defined(__x86_64__)
60 #define SYS_renameat2 316
63 #if !defined(SYS_renameat2) && defined(__i386__)
64 #define SYS_renameat2 353
67 static int renameat2(int dfd1, const char *path1,
68 int dfd2, const char *path2,
72 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
80 #ifndef RENAME_NOREPLACE
81 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
83 #ifndef RENAME_EXCHANGE
84 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
86 #ifndef RENAME_WHITEOUT
87 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
90 #define FILELEN_MAX (32*4096)
156 typedef void (*opfnc_t)(int, long);
158 typedef struct opdesc {
166 typedef struct fent {
173 typedef struct flist {
180 typedef struct pathname {
190 struct print_string {
197 #define FT_DIRm (1 << FT_DIR)
199 #define FT_REGm (1 << FT_REG)
201 #define FT_SYMm (1 << FT_SYM)
203 #define FT_DEVm (1 << FT_DEV)
205 #define FT_RTFm (1 << FT_RTF)
207 #define FT_SUBVOLm (1 << FT_SUBVOL)
209 #define FT_ANYm ((1 << FT_nft) - 1)
210 #define FT_REGFILE (FT_REGm | FT_RTFm)
211 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
212 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
214 #define FLIST_SLOT_INCR 16
217 #define MAXFSIZE ((1ULL << 63) - 1ULL)
218 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
220 #define XATTR_NAME_BUF_SIZE 18
222 void afsync_f(int, long);
223 void allocsp_f(int, long);
224 void aread_f(int, long);
225 void attr_remove_f(int, long);
226 void attr_set_f(int, long);
227 void awrite_f(int, long);
228 void bulkstat_f(int, long);
229 void bulkstat1_f(int, long);
230 void chown_f(int, long);
231 void clonerange_f(int, long);
232 void copyrange_f(int, long);
233 void creat_f(int, long);
234 void deduperange_f(int, long);
235 void dread_f(int, long);
236 void dwrite_f(int, long);
237 void fallocate_f(int, long);
238 void fdatasync_f(int, long);
239 void fiemap_f(int, long);
240 void freesp_f(int, long);
241 void fsync_f(int, long);
242 char *gen_random_string(int);
243 void getattr_f(int, long);
244 void getdents_f(int, long);
245 void getfattr_f(int, long);
246 void link_f(int, long);
247 void listfattr_f(int, long);
248 void mkdir_f(int, long);
249 void mknod_f(int, long);
250 void mread_f(int, long);
251 void mwrite_f(int, long);
252 void punch_f(int, long);
253 void zero_f(int, long);
254 void collapse_f(int, long);
255 void insert_f(int, long);
256 void read_f(int, long);
257 void readlink_f(int, long);
258 void readv_f(int, long);
259 void removefattr_f(int, long);
260 void rename_f(int, long);
261 void rnoreplace_f(int, long);
262 void rexchange_f(int, long);
263 void rwhiteout_f(int, long);
264 void resvsp_f(int, long);
265 void rmdir_f(int, long);
266 void setattr_f(int, long);
267 void setfattr_f(int, long);
268 void setxattr_f(int, long);
269 void snapshot_f(int, long);
270 void splice_f(int, long);
271 void stat_f(int, long);
272 void subvol_create_f(int, long);
273 void subvol_delete_f(int, long);
274 void symlink_f(int, long);
275 void sync_f(int, long);
276 void truncate_f(int, long);
277 void unlink_f(int, long);
278 void unresvsp_f(int, long);
279 void uring_read_f(int, long);
280 void uring_write_f(int, long);
281 void write_f(int, long);
282 void writev_f(int, long);
283 char *xattr_flag_to_string(int);
286 /* { OP_ENUM, "name", function, freq, iswrite }, */
287 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
288 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
289 { OP_AREAD, "aread", aread_f, 1, 0 },
290 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
291 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
292 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
293 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
294 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
295 { OP_CHOWN, "chown", chown_f, 3, 1 },
296 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
297 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
298 { OP_CREAT, "creat", creat_f, 4, 1 },
299 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
300 { OP_DREAD, "dread", dread_f, 4, 0 },
301 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
302 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
303 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
304 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
305 { OP_FREESP, "freesp", freesp_f, 1, 1 },
306 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
307 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
308 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
309 /* get extended attribute */
310 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
311 { OP_LINK, "link", link_f, 1, 1 },
312 /* list extent attributes */
313 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
314 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
315 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
316 { OP_MREAD, "mread", mread_f, 2, 0 },
317 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
318 { OP_PUNCH, "punch", punch_f, 1, 1 },
319 { OP_ZERO, "zero", zero_f, 1, 1 },
320 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
321 { OP_INSERT, "insert", insert_f, 1, 1 },
322 { OP_READ, "read", read_f, 1, 0 },
323 { OP_READLINK, "readlink", readlink_f, 1, 0 },
324 { OP_READV, "readv", readv_f, 1, 0 },
325 /* remove (delete) extended attribute */
326 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
327 { OP_RENAME, "rename", rename_f, 2, 1 },
328 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
329 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
330 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
331 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
332 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
333 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
334 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
335 /* set extended attribute */
336 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
337 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
338 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
339 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
340 { OP_SPLICE, "splice", splice_f, 1, 1 },
341 { OP_STAT, "stat", stat_f, 1, 0 },
342 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
343 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
344 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
345 { OP_SYNC, "sync", sync_f, 1, 1 },
346 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
347 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
348 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
349 { OP_URING_READ, "uring_read", uring_read_f, 1, 0 },
350 { OP_URING_WRITE, "uring_write", uring_write_f, 1, 1 },
351 { OP_WRITE, "write", write_f, 4, 1 },
352 { OP_WRITEV, "writev", writev_f, 4, 1 },
355 flist_t flist[FT_nft] = {
369 struct xfs_fsop_geom geom;
380 unsigned int idmodulo = XFS_IDMODULO_MAX;
381 unsigned int attr_mask = ~0;
384 unsigned long seed = 0;
388 int verifiable_log = 0;
389 sig_atomic_t should_stop = 0;
390 sigjmp_buf *sigbus_jmp = NULL;
391 char *execute_cmd = NULL;
392 int execute_freq = 1;
393 struct print_string flag_str = {0};
395 void add_to_flist(int, int, int, int);
396 void append_pathname(pathname_t *, char *);
397 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
398 int attr_remove_path(pathname_t *, const char *);
399 int attr_set_path(pathname_t *, const char *, const char *, const int);
400 void check_cwd(void);
401 void cleanup_flist(void);
402 int creat_path(pathname_t *, mode_t);
403 void dcache_enter(int, int);
404 void dcache_init(void);
405 fent_t *dcache_lookup(int);
406 void dcache_purge(int, int);
407 void del_from_flist(int, int);
408 int dirid_to_name(char *, int);
410 int fent_to_name(pathname_t *, fent_t *);
411 bool fents_ancestor_check(fent_t *, fent_t *);
412 void fix_parent(int, int, bool);
413 void free_pathname(pathname_t *);
414 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
415 int generate_xattr_name(int, char *, int);
416 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
417 void init_pathname(pathname_t *);
418 int lchown_path(pathname_t *, uid_t, gid_t);
419 int link_path(pathname_t *, pathname_t *);
420 int lstat64_path(pathname_t *, struct stat64 *);
421 void make_freq_table(void);
422 int mkdir_path(pathname_t *, mode_t);
423 int mknod_path(pathname_t *, mode_t, dev_t);
424 void namerandpad(int, char *, int);
425 int open_file_or_dir(pathname_t *, int);
426 int open_path(pathname_t *, int);
427 DIR *opendir_path(pathname_t *);
428 void process_freq(char *);
429 int readlink_path(pathname_t *, char *, size_t);
430 int rename_path(pathname_t *, pathname_t *, int);
431 int rmdir_path(pathname_t *);
432 void separate_pathname(pathname_t *, char *, pathname_t *);
433 void show_ops(int, char *);
434 int stat64_path(pathname_t *, struct stat64 *);
435 int symlink_path(const char *, pathname_t *);
436 int truncate64_path(pathname_t *, off64_t);
437 int unlink_path(pathname_t *);
439 void write_freq(void);
440 void zero_freq(void);
441 void non_btrfs_freq(const char *);
443 void sg_handler(int signum)
451 * Only handle SIGBUS when mmap write to a hole and no
452 * block can be allocated due to ENOSPC, abort otherwise.
455 siglongjmp(*sigbus_jmp, -1);
457 printf("Unknown SIGBUS is caught, Abort!\n");
460 /* should not reach here */
467 int main(int argc, char **argv)
471 char *dirname = NULL;
472 char *logname = NULL;
473 char rpath[PATH_MAX];
482 xfs_error_injection_t err_inj;
483 struct sigaction action;
485 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
487 errrange = errtag = 0;
489 nops = sizeof(ops) / sizeof(ops[0]);
490 ops_end = &ops[nops];
492 while ((c = getopt(argc, argv, allopts)) != -1) {
501 sscanf(optarg, "%d", &errtag);
505 } else if (errtag == 0)
507 if (errtag >= XFS_ERRTAG_MAX) {
509 "error tag %d too large (max %d)\n",
510 errtag, XFS_ERRTAG_MAX - 1);
515 process_freq(optarg);
518 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
519 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
522 idmodulo = strtoul(optarg, NULL, 0);
523 if (idmodulo > XFS_IDMODULO_MAX) {
525 "chown modulo %d too big (max %d)\n",
526 idmodulo, XFS_IDMODULO_MAX);
531 loops = atoi(optarg);
534 operations = atoi(optarg);
541 nproc = atoi(optarg);
547 seed = strtoul(optarg, NULL, 0);
556 execute_cmd = optarg;
562 attr_mask = strtoul(optarg, NULL, 0);
566 if (optarg[0] == 'c')
577 execute_freq = strtoul(optarg, NULL, 0);
580 fprintf(stderr, "%s - invalid parameters\n",
590 /* no directory specified */
591 if (!nousage) usage();
595 non_btrfs_freq(dirname);
596 (void)mkdir(dirname, 0777);
597 if (logname && logname[0] != '/') {
598 if (!getcwd(rpath, sizeof(rpath))){
599 perror("getcwd failed");
605 if (chdir(dirname) < 0) {
610 char path[PATH_MAX + NAME_MAX + 1];
611 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
612 if (freopen(path, "a", stdout) == NULL) {
613 perror("freopen logfile failed");
617 sprintf(buf, "fss%x", (unsigned int)getpid());
618 fd = creat(buf, 0666);
619 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
620 maxfsize = (off64_t)MAXFSIZE32;
622 maxfsize = (off64_t)MAXFSIZE;
627 gettimeofday(&t, (void *)NULL);
628 seed = (int)t.tv_sec ^ (int)t.tv_usec;
629 printf("seed = %ld\n", seed);
631 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
632 if (i >= 0 && geom.rtblocks)
633 rtpct = MIN(MAX(geom.rtblocks * 100 /
634 (geom.rtblocks + geom.datablocks), 1), 99);
643 for (i = 0; i < j; i++)
646 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
652 for (i = 0; i < j; i++)
655 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
657 printf("Injecting failure on tag #%d\n", errtag);
658 err_inj.errtag = errtag;
660 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
662 perror("fsstress - XFS_SYSSGI error injection call");
671 action.sa_handler = sg_handler;
672 sigemptyset(&action.sa_mask);
674 if (sigaction(SIGTERM, &action, 0)) {
675 perror("sigaction failed");
679 for (i = 0; i < nproc; i++) {
681 sigemptyset(&action.sa_mask);
682 action.sa_handler = SIG_DFL;
683 if (sigaction(SIGTERM, &action, 0))
685 action.sa_handler = sg_handler;
686 if (sigaction(SIGBUS, &action, 0))
688 #ifdef HAVE_SYS_PRCTL_H
689 prctl(PR_SET_PDEATHSIG, SIGKILL);
690 if (getppid() == 1) /* parent died already? */
694 char path[PATH_MAX + NAME_MAX + 2 + 11];
695 snprintf(path, sizeof(path), "%s/%s.%d",
697 if (freopen(path, "a", stdout) == NULL) {
698 perror("freopen logfile failed");
704 if (io_setup(AIO_ENTRIES, &io_ctx) != 0) {
705 fprintf(stderr, "io_setup failed\n");
710 have_io_uring = true;
711 /* If ENOSYS, just ignore uring, other errors are fatal. */
712 if (io_uring_queue_init(URING_ENTRIES, &ring, 0)) {
713 if (errno == ENOSYS) {
714 have_io_uring = false;
716 fprintf(stderr, "io_uring_queue_init failed\n");
721 for (i = 0; !loops || (i < loops); i++)
724 if(io_destroy(io_ctx) != 0) {
725 fprintf(stderr, "io_destroy failed");
731 io_uring_queue_exit(&ring);
738 while (wait(&stat) > 0 && !should_stop) {
741 action.sa_flags = SA_RESTART;
742 sigaction(SIGTERM, &action, 0);
743 kill(-getpid(), SIGTERM);
744 while (wait(&stat) > 0)
750 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
752 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
754 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
767 add_string(struct print_string *str, const char *add)
769 int len = strlen(add);
774 if (len > (str->max - 1) - str->len) {
775 str->len = str->max - 1;
779 memcpy(str->buffer + str->len, add, len);
781 str->buffer[str->len] = '\0';
787 translate_flags(int flags, const char *delim,
788 const struct print_flags *flag_array)
790 int i, mask, first = 1;
793 if (!flag_str.buffer) {
794 flag_str.buffer = malloc(4096);
798 if (!flag_str.buffer)
801 flag_str.buffer[0] = '\0';
803 for (i = 0; flag_array[i].name && flags; i++) {
804 mask = flag_array[i].mask;
805 if ((flags & mask) != mask)
808 add = flag_array[i].name;
811 add_string(&flag_str, delim);
814 add_string(&flag_str, add);
817 /* Check whether there are any leftover flags. */
823 add_string(&flag_str, delim);
825 ret = snprintf(number, 11, "0x%x", flags) > 0;
826 if (ret > 0 && ret <= 11)
827 add_string(&flag_str, number);
830 return flag_str.buffer;
834 add_to_flist(int ft, int id, int parent, int xattr_counter)
840 if (ftp->nfiles == ftp->nslots) {
841 ftp->nslots += FLIST_SLOT_INCR;
842 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
844 fep = &ftp->fents[ftp->nfiles++];
847 fep->parent = parent;
848 fep->xattr_counter = xattr_counter;
852 append_pathname(pathname_t *name, char *str)
858 /* attempting to append to a dir a zero length path */
859 if (len && *str == '/' && name->len == 0) {
860 fprintf(stderr, "fsstress: append_pathname failure\n");
861 assert(chdir(homedir) == 0);
866 name->path = realloc(name->path, name->len + 1 + len);
867 strcpy(&name->path[name->len], str);
872 attr_list_path(pathname_t *name,
874 const int buffersize,
876 attrlist_cursor_t *cursor)
878 char buf[NAME_MAX + 1];
882 if (flags != ATTR_DONTFOLLOW) {
887 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
888 if (rval >= 0 || errno != ENAMETOOLONG)
890 separate_pathname(name, buf, &newname);
891 if (chdir(buf) == 0) {
892 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
893 assert(chdir("..") == 0);
895 free_pathname(&newname);
900 attr_remove_path(pathname_t *name, const char *attrname)
902 char buf[NAME_MAX + 1];
906 rval = lremovexattr(name->path, attrname);
907 if (rval >= 0 || errno != ENAMETOOLONG)
909 separate_pathname(name, buf, &newname);
910 if (chdir(buf) == 0) {
911 rval = attr_remove_path(&newname, attrname);
912 assert(chdir("..") == 0);
914 free_pathname(&newname);
919 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
920 const int valuelength)
922 char buf[NAME_MAX + 1];
926 rval = lsetxattr(name->path, attrname, attrvalue, valuelength, 0);
927 if (rval >= 0 || errno != ENAMETOOLONG)
929 separate_pathname(name, buf, &newname);
930 if (chdir(buf) == 0) {
931 rval = attr_set_path(&newname, attrname, attrvalue,
933 assert(chdir("..") == 0);
935 free_pathname(&newname);
943 struct stat64 statbuf;
945 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
947 assert(chdir(homedir) == 0);
948 fprintf(stderr, "fsstress: check_cwd failure\n");
955 * go thru flist and release all entries
957 * NOTE: this function shouldn't be called until the end of a process
965 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
974 creat_path(pathname_t *name, mode_t mode)
976 char buf[NAME_MAX + 1];
980 rval = creat(name->path, mode);
981 if (rval >= 0 || errno != ENAMETOOLONG)
983 separate_pathname(name, buf, &newname);
984 if (chdir(buf) == 0) {
985 rval = creat_path(&newname, mode);
986 assert(chdir("..") == 0);
988 free_pathname(&newname);
993 dcache_enter(int dirid, int slot)
995 dcache[dirid % NDCACHE] = slot;
1003 for (i = 0; i < NDCACHE; i++)
1008 dcache_lookup(int dirid)
1013 i = dcache[dirid % NDCACHE];
1014 if (i >= 0 && i < flist[FT_DIR].nfiles &&
1015 (fep = &flist[FT_DIR].fents[i])->id == dirid)
1017 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
1018 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
1024 dcache_purge(int dirid, int ft)
1027 dcp = &dcache[dirid % NDCACHE];
1028 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1029 flist[ft].fents[*dcp].id == dirid)
1034 * Delete the item from the list by
1035 * moving last entry over the deleted one;
1036 * unless deleted entry is the last one.
1037 * Input: which file list array and which slot in array
1040 del_from_flist(int ft, int slot)
1045 if (ft == FT_DIR || ft == FT_SUBVOL)
1046 dcache_purge(ftp->fents[slot].id, ft);
1047 if (slot != ftp->nfiles - 1) {
1048 if (ft == FT_DIR || ft == FT_SUBVOL)
1049 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1050 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1056 delete_subvol_children(int parid)
1061 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1063 for (c = 0; c < flp->nfiles; c++) {
1064 if (flp->fents[c].parent == parid) {
1065 int id = flp->fents[c].id;
1066 del_from_flist(i, c);
1067 if (i == FT_DIR || i == FT_SUBVOL)
1068 delete_subvol_children(id);
1076 dirid_to_fent(int dirid)
1083 if ((fep = dcache_lookup(dirid)))
1087 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1088 if (fep->id == dirid) {
1089 dcache_enter(dirid, fep - flp->fents);
1103 struct stat64 statbuf;
1111 dividend = (operations + execute_freq) / (execute_freq + 1);
1112 sprintf(buf, "p%x", procid);
1113 (void)mkdir(buf, 0777);
1114 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1118 top_ino = statbuf.st_ino;
1119 homedir = getcwd(NULL, 0);
1121 perror("getcwd failed");
1127 namerand = random();
1128 for (opno = 0; opno < operations; opno++) {
1129 if (execute_cmd && opno && opno % dividend == 0) {
1131 printf("%d: execute command %s\n", opno,
1133 rval = system(execute_cmd);
1135 fprintf(stderr, "execute command failed with "
1138 p = &ops[freq_table[random() % freq_table_size]];
1139 p->func(opno, random());
1141 * test for forced shutdown by stat'ing the test
1142 * directory. If this stat returns EIO, assume
1143 * the forced shutdown happened.
1145 if (errtag != 0 && opno % 100 == 0) {
1146 rval = stat64(".", &statbuf);
1148 fprintf(stderr, "Detected EIO\n");
1154 assert(chdir("..") == 0);
1159 sprintf(cmd, "rm -rf %s", buf);
1162 perror("cleaning up");
1168 * build up a pathname going thru the file entry and all
1169 * its parent entries
1170 * Return 0 on error, 1 on success;
1173 fent_to_name(pathname_t *name, fent_t *fep)
1175 flist_t *flp = &flist[fep->ft];
1176 char buf[NAME_MAX + 1];
1184 /* build up parent directory name */
1185 if (fep->parent != -1) {
1186 pfep = dirid_to_fent(fep->parent);
1189 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1190 procid, fep->id, fep->parent);
1195 e = fent_to_name(name, pfep);
1198 append_pathname(name, "/");
1201 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1202 namerandpad(fep->id, buf, i);
1203 append_pathname(name, buf);
1208 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1212 for (tmpfep = fep; tmpfep->parent != -1;
1213 tmpfep = dirid_to_fent(tmpfep->parent)) {
1214 if (tmpfep->parent == dfep->id)
1222 fix_parent(int oldid, int newid, bool swap)
1229 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1230 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1231 if (fep->parent == oldid)
1232 fep->parent = newid;
1233 else if (swap && fep->parent == newid)
1234 fep->parent = oldid;
1240 free_pathname(pathname_t *name)
1250 * Generate a filename of type ft.
1251 * If we have a fep which should be a directory then use it
1252 * as the parent path for this new filename i.e. prepend with it.
1255 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1257 char buf[NAME_MAX + 1];
1266 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1267 namerandpad(id, buf, len);
1269 /* prepend fep parent dir-name to it */
1271 e = fent_to_name(name, fep);
1274 append_pathname(name, "/");
1276 append_pathname(name, buf);
1280 for (j = 0; !*v && j < ilistlen; j++) {
1281 if (ilist[j] == id) {
1289 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1293 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1303 * Input: "which" to choose the file-types eg. non-directory
1304 * Input: "r" to choose which file
1305 * Output: file-list, file-entry, name for the chosen file.
1306 * Output: verbose if chosen file is on the ilist.
1309 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1312 int totalsum = 0; /* total number of matching files */
1313 int partialsum = 0; /* partial sum of matching files */
1319 int e = 1; /* success */
1322 * go thru flist and add up number of files for each
1323 * category that matches with <which>.
1325 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1326 if (which & (1 << i))
1327 totalsum += flp->nfiles;
1329 if (totalsum == 0) {
1339 * Now we have possible matches between 0..totalsum-1.
1340 * And we use r to help us choose which one we want,
1341 * which when bounded by totalsum becomes x.
1343 x = (int)(r % totalsum);
1344 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1345 if (which & (1 << i)) {
1346 if (x < partialsum + flp->nfiles) {
1348 /* found the matching file entry */
1349 fep = &flp->fents[x - partialsum];
1351 /* fill-in what we were asked for */
1353 e = fent_to_name(name, fep);
1356 fprintf(stderr, "%d: failed to get path for entry:"
1357 " id=%d,parent=%d\n",
1358 procid, fep->id, fep->parent);
1367 /* turn on verbose if its an ilisted file */
1369 for (j = 0; !*v && j < ilistlen; j++) {
1370 if (ilist[j] == fep->id) {
1377 partialsum += flp->nfiles;
1381 fprintf(stderr, "fsstress: get_fname failure\n");
1388 init_pathname(pathname_t *name)
1395 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1397 char buf[NAME_MAX + 1];
1401 rval = lchown(name->path, owner, group);
1402 if (rval >= 0 || errno != ENAMETOOLONG)
1404 separate_pathname(name, buf, &newname);
1405 if (chdir(buf) == 0) {
1406 rval = lchown_path(&newname, owner, group);
1407 assert(chdir("..") == 0);
1409 free_pathname(&newname);
1414 link_path(pathname_t *name1, pathname_t *name2)
1416 char buf1[NAME_MAX + 1];
1417 char buf2[NAME_MAX + 1];
1419 pathname_t newname1;
1420 pathname_t newname2;
1423 rval = link(name1->path, name2->path);
1424 if (rval >= 0 || errno != ENAMETOOLONG)
1426 separate_pathname(name1, buf1, &newname1);
1427 separate_pathname(name2, buf2, &newname2);
1428 if (strcmp(buf1, buf2) == 0) {
1429 if (chdir(buf1) == 0) {
1430 rval = link_path(&newname1, &newname2);
1431 assert(chdir("..") == 0);
1434 if (strcmp(buf1, "..") == 0)
1436 else if (strcmp(buf2, "..") == 0)
1438 else if (strlen(buf1) == 0)
1440 else if (strlen(buf2) == 0)
1443 down1 = MAX(newname1.len, 3 + name2->len) <=
1444 MAX(3 + name1->len, newname2.len);
1446 free_pathname(&newname2);
1447 append_pathname(&newname2, "../");
1448 append_pathname(&newname2, name2->path);
1449 if (chdir(buf1) == 0) {
1450 rval = link_path(&newname1, &newname2);
1451 assert(chdir("..") == 0);
1454 free_pathname(&newname1);
1455 append_pathname(&newname1, "../");
1456 append_pathname(&newname1, name1->path);
1457 if (chdir(buf2) == 0) {
1458 rval = link_path(&newname1, &newname2);
1459 assert(chdir("..") == 0);
1463 free_pathname(&newname1);
1464 free_pathname(&newname2);
1469 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1471 char buf[NAME_MAX + 1];
1475 rval = lstat64(name->path, sbuf);
1476 if (rval >= 0 || errno != ENAMETOOLONG)
1478 separate_pathname(name, buf, &newname);
1479 if (chdir(buf) == 0) {
1480 rval = lstat64_path(&newname, sbuf);
1481 assert(chdir("..") == 0);
1483 free_pathname(&newname);
1488 make_freq_table(void)
1494 for (p = ops, f = 0; p < ops_end; p++)
1496 freq_table = malloc(f * sizeof(*freq_table));
1497 freq_table_size = f;
1498 for (p = ops, i = 0; p < ops_end; p++) {
1499 for (f = 0; f < p->freq; f++, i++)
1500 freq_table[i] = p->op;
1505 mkdir_path(pathname_t *name, mode_t mode)
1507 char buf[NAME_MAX + 1];
1511 rval = mkdir(name->path, mode);
1512 if (rval >= 0 || errno != ENAMETOOLONG)
1514 separate_pathname(name, buf, &newname);
1515 if (chdir(buf) == 0) {
1516 rval = mkdir_path(&newname, mode);
1517 assert(chdir("..") == 0);
1519 free_pathname(&newname);
1524 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1526 char buf[NAME_MAX + 1];
1530 rval = mknod(name->path, mode, dev);
1531 if (rval >= 0 || errno != ENAMETOOLONG)
1533 separate_pathname(name, buf, &newname);
1534 if (chdir(buf) == 0) {
1535 rval = mknod_path(&newname, mode, dev);
1536 assert(chdir("..") == 0);
1538 free_pathname(&newname);
1543 namerandpad(int id, char *buf, int i)
1546 static int buckets[] =
1547 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1553 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1554 padmod = buckets[bucket] + 1 - i;
1557 padlen = (id ^ namerand) % padmod;
1559 memset(&buf[i], 'X', padlen);
1560 buf[i + padlen] = '\0';
1565 open_file_or_dir(pathname_t *name, int flags)
1569 fd = open_path(name, flags);
1572 if (fd == -1 && errno != EISDIR)
1574 /* Directories can not be opened in write mode nor direct mode. */
1575 flags &= ~(O_WRONLY | O_DIRECT);
1576 flags |= O_RDONLY | O_DIRECTORY;
1577 return open_path(name, flags);
1581 open_path(pathname_t *name, int oflag)
1583 char buf[NAME_MAX + 1];
1587 rval = open(name->path, oflag);
1588 if (rval >= 0 || errno != ENAMETOOLONG)
1590 separate_pathname(name, buf, &newname);
1591 if (chdir(buf) == 0) {
1592 rval = open_path(&newname, oflag);
1593 assert(chdir("..") == 0);
1595 free_pathname(&newname);
1600 opendir_path(pathname_t *name)
1602 char buf[NAME_MAX + 1];
1606 rval = opendir(name->path);
1607 if (rval || errno != ENAMETOOLONG)
1609 separate_pathname(name, buf, &newname);
1610 if (chdir(buf) == 0) {
1611 rval = opendir_path(&newname);
1612 assert(chdir("..") == 0);
1614 free_pathname(&newname);
1619 process_freq(char *arg)
1624 s = strchr(arg, '=');
1626 fprintf(stderr, "bad argument '%s'\n", arg);
1630 for (p = ops; p < ops_end; p++) {
1631 if (strcmp(arg, p->name) == 0) {
1636 fprintf(stderr, "can't find op type %s for -f\n", arg);
1641 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1643 char buf[NAME_MAX + 1];
1647 rval = readlink(name->path, lbuf, lbufsiz);
1648 if (rval >= 0 || errno != ENAMETOOLONG)
1650 separate_pathname(name, buf, &newname);
1651 if (chdir(buf) == 0) {
1652 rval = readlink_path(&newname, lbuf, lbufsiz);
1653 assert(chdir("..") == 0);
1655 free_pathname(&newname);
1660 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1662 char buf1[NAME_MAX + 1];
1663 char buf2[NAME_MAX + 1];
1665 pathname_t newname1;
1666 pathname_t newname2;
1670 rval = rename(name1->path, name2->path);
1672 rval = renameat2(AT_FDCWD, name1->path,
1673 AT_FDCWD, name2->path, mode);
1674 if (rval >= 0 || errno != ENAMETOOLONG)
1676 separate_pathname(name1, buf1, &newname1);
1677 separate_pathname(name2, buf2, &newname2);
1678 if (strcmp(buf1, buf2) == 0) {
1679 if (chdir(buf1) == 0) {
1680 rval = rename_path(&newname1, &newname2, mode);
1681 assert(chdir("..") == 0);
1684 if (strcmp(buf1, "..") == 0)
1686 else if (strcmp(buf2, "..") == 0)
1688 else if (strlen(buf1) == 0)
1690 else if (strlen(buf2) == 0)
1693 down1 = MAX(newname1.len, 3 + name2->len) <=
1694 MAX(3 + name1->len, newname2.len);
1696 free_pathname(&newname2);
1697 append_pathname(&newname2, "../");
1698 append_pathname(&newname2, name2->path);
1699 if (chdir(buf1) == 0) {
1700 rval = rename_path(&newname1, &newname2, mode);
1701 assert(chdir("..") == 0);
1704 free_pathname(&newname1);
1705 append_pathname(&newname1, "../");
1706 append_pathname(&newname1, name1->path);
1707 if (chdir(buf2) == 0) {
1708 rval = rename_path(&newname1, &newname2, mode);
1709 assert(chdir("..") == 0);
1713 free_pathname(&newname1);
1714 free_pathname(&newname2);
1719 rmdir_path(pathname_t *name)
1721 char buf[NAME_MAX + 1];
1725 rval = rmdir(name->path);
1726 if (rval >= 0 || errno != ENAMETOOLONG)
1728 separate_pathname(name, buf, &newname);
1729 if (chdir(buf) == 0) {
1730 rval = rmdir_path(&newname);
1731 assert(chdir("..") == 0);
1733 free_pathname(&newname);
1738 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1742 init_pathname(newname);
1743 slash = strchr(name->path, '/');
1744 if (slash == NULL) {
1749 strcpy(buf, name->path);
1751 append_pathname(newname, slash + 1);
1757 show_ops(int flag, char *lead_str)
1762 /* print in list form */
1765 for (p = ops; p < ops_end; p++) {
1766 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1767 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1768 x+=printf("%s ", p->name);
1771 } else if (flag == 0) {
1772 /* Table view style */
1774 for (f = 0, p = ops; p < ops_end; p++)
1780 for (p = ops; p < ops_end; p++) {
1781 if (flag != 0 || p->freq > 0) {
1782 if (lead_str != NULL)
1783 printf("%s", lead_str);
1784 printf("%20s %d/%d %s\n",
1785 p->name, p->freq, f,
1786 (p->iswrite == 0) ? " " : "write op");
1790 /* Command line style */
1791 if (lead_str != NULL)
1792 printf("%s", lead_str);
1793 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1795 for (p = ops; p < ops_end; p++)
1797 printf("-f %s=%d \\\n",p->name, p->freq);
1802 stat64_path(pathname_t *name, struct stat64 *sbuf)
1804 char buf[NAME_MAX + 1];
1808 rval = stat64(name->path, sbuf);
1809 if (rval >= 0 || errno != ENAMETOOLONG)
1811 separate_pathname(name, buf, &newname);
1812 if (chdir(buf) == 0) {
1813 rval = stat64_path(&newname, sbuf);
1814 assert(chdir("..") == 0);
1816 free_pathname(&newname);
1821 symlink_path(const char *name1, pathname_t *name)
1823 char buf[NAME_MAX + 1];
1827 if (!strcmp(name1, name->path)) {
1828 printf("yikes! %s %s\n", name1, name->path);
1832 rval = symlink(name1, name->path);
1833 if (rval >= 0 || errno != ENAMETOOLONG)
1835 separate_pathname(name, buf, &newname);
1836 if (chdir(buf) == 0) {
1837 rval = symlink_path(name1, &newname);
1838 assert(chdir("..") == 0);
1840 free_pathname(&newname);
1845 truncate64_path(pathname_t *name, off64_t length)
1847 char buf[NAME_MAX + 1];
1851 rval = truncate64(name->path, length);
1852 if (rval >= 0 || errno != ENAMETOOLONG)
1854 separate_pathname(name, buf, &newname);
1855 if (chdir(buf) == 0) {
1856 rval = truncate64_path(&newname, length);
1857 assert(chdir("..") == 0);
1859 free_pathname(&newname);
1864 unlink_path(pathname_t *name)
1866 char buf[NAME_MAX + 1];
1870 rval = unlink(name->path);
1871 if (rval >= 0 || errno != ENAMETOOLONG)
1873 separate_pathname(name, buf, &newname);
1874 if (chdir(buf) == 0) {
1875 rval = unlink_path(&newname);
1876 assert(chdir("..") == 0);
1878 free_pathname(&newname);
1885 printf("Usage: %s -H or\n", myprog);
1886 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1888 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1890 printf(" -c clean up the test directory after each run\n");
1891 printf(" -d dir specifies the base directory for operations\n");
1892 printf(" -e errtg specifies error injection stuff\n");
1893 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1894 printf(" the valid operation names are:\n");
1896 printf(" -i filenum get verbose output for this nth file object\n");
1897 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1898 printf(" *use 0 for infinite (default 1)\n");
1899 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1900 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1901 printf(" -o logfile specifies logfile name\n");
1902 printf(" -p nproc specifies the no. of processes (default 1)\n");
1903 printf(" -r specifies random name padding\n");
1904 printf(" -s seed specifies the seed for the random generator (default random)\n");
1905 printf(" -v specifies verbose mode\n");
1906 printf(" -w zeros frequencies of non-write operations\n");
1907 printf(" -x cmd execute command in the middle of operations\n");
1908 printf(" -z zeros frequencies of all operations\n");
1909 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1910 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1911 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1912 printf(" -H prints usage and exits\n");
1920 for (p = ops; p < ops_end; p++) {
1931 for (p = ops; p < ops_end; p++)
1935 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1937 opty_t btrfs_ops[] = {
1944 non_btrfs_freq(const char *path)
1947 #ifdef HAVE_BTRFSUTIL_H
1948 enum btrfs_util_error e;
1950 e = btrfs_util_is_subvolume(path);
1951 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1954 for (p = ops; p < ops_end; p++) {
1956 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1957 if (p->op == btrfs_ops[i]) {
1965 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1968 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1969 verifiable_log ? -1: (long)s->st_ino,
1970 (long)s->st_nlink, s->st_uid, s->st_gid,
1971 (long long) s->st_blocks, (long long) s->st_size);
1975 afsync_f(int opno, long r)
1983 struct iocb *iocbs[] = { &iocb };
1984 struct io_event event;
1987 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1989 printf("%d/%d: afsync - no filename\n", procid, opno);
1993 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1994 e = fd < 0 ? errno : 0;
1998 printf("%d/%d: afsync - open %s failed %d\n",
1999 procid, opno, f.path, e);
2004 io_prep_fsync(&iocb, fd);
2005 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2007 printf("%d/%d: afsync - io_submit %s %d\n",
2008 procid, opno, f.path, e);
2013 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2015 printf("%d/%d: afsync - io_getevents failed %d\n",
2024 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2031 allocsp_f(int opno, long r)
2036 struct xfs_flock64 fl;
2044 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2046 printf("%d/%d: allocsp - no filename\n", procid, opno);
2050 fd = open_path(&f, O_RDWR);
2051 e = fd < 0 ? errno : 0;
2055 printf("%d/%d: allocsp - open %s failed %d\n",
2056 procid, opno, f.path, e);
2060 if (fstat64(fd, &stb) < 0) {
2062 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2063 procid, opno, f.path, errno);
2068 inode_info(st, sizeof(st), &stb, v);
2069 lr = ((int64_t)random() << 32) + random();
2070 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2072 fl.l_whence = SEEK_SET;
2075 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2077 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2078 procid, opno, f.path, st, (long long)off, e);
2086 do_aio_rw(int opno, long r, int flags)
2090 struct dioattr diob;
2102 struct io_event event;
2103 struct iocb *iocbs[] = { &iocb };
2104 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2107 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2109 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2112 fd = open_path(&f, flags|O_DIRECT);
2113 e = fd < 0 ? errno : 0;
2117 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2118 procid, opno, f.path, e);
2121 if (fstat64(fd, &stb) < 0) {
2123 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2124 procid, opno, f.path, errno);
2127 inode_info(st, sizeof(st), &stb, v);
2128 if (!iswrite && stb.st_size == 0) {
2130 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2134 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2137 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2138 " fallback to stat()\n",
2139 procid, opno, f.path, st, errno);
2140 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2141 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2143 dio_env = getenv("XFS_DIO_MIN");
2145 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2146 align = (int64_t)diob.d_miniosz;
2147 lr = ((int64_t)random() << 32) + random();
2148 len = (random() % FILELEN_MAX) + 1;
2149 len -= (len % align);
2152 else if (len > diob.d_maxiosz)
2153 len = diob.d_maxiosz;
2154 buf = memalign(diob.d_mem, len);
2157 printf("%d/%d: do_aio_rw - memalign failed\n",
2163 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2164 off -= (off % align);
2166 memset(buf, nameseq & 0xff, len);
2167 io_prep_pwrite(&iocb, fd, buf, len, off);
2169 off = (off64_t)(lr % stb.st_size);
2170 off -= (off % align);
2171 io_prep_pread(&iocb, fd, buf, len, off);
2173 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2175 printf("%d/%d: %s - io_submit failed %d\n",
2176 procid, opno, iswrite ? "awrite" : "aread", e);
2179 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2181 printf("%d/%d: %s - io_getevents failed %d\n",
2182 procid, opno, iswrite ? "awrite" : "aread", e);
2186 e = event.res != len ? event.res2 : 0;
2188 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2189 procid, opno, iswrite ? "awrite" : "aread",
2190 f.path, st, (long long)off, (int)len, e);
2202 do_uring_rw(int opno, long r, int flags)
2214 struct io_uring_sqe *sqe;
2215 struct io_uring_cqe *cqe;
2217 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2223 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2225 printf("%d/%d: do_uring_rw - no filename\n", procid, opno);
2228 fd = open_path(&f, flags);
2229 e = fd < 0 ? errno : 0;
2233 printf("%d/%d: do_uring_rw - open %s failed %d\n",
2234 procid, opno, f.path, e);
2237 if (fstat64(fd, &stb) < 0) {
2239 printf("%d/%d: do_uring_rw - fstat64 %s failed %d\n",
2240 procid, opno, f.path, errno);
2243 inode_info(st, sizeof(st), &stb, v);
2244 if (!iswrite && stb.st_size == 0) {
2246 printf("%d/%d: do_uring_rw - %s%s zero size\n", procid, opno,
2250 sqe = io_uring_get_sqe(&ring);
2253 printf("%d/%d: do_uring_rw - io_uring_get_sqe failed\n",
2257 lr = ((int64_t)random() << 32) + random();
2258 len = (random() % FILELEN_MAX) + 1;
2262 printf("%d/%d: do_uring_rw - malloc failed\n",
2266 iovec.iov_base = buf;
2267 iovec.iov_len = len;
2269 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2271 memset(buf, nameseq & 0xff, len);
2272 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2274 off = (off64_t)(lr % stb.st_size);
2275 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2278 if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2280 printf("%d/%d: %s - io_uring_submit failed %d\n", procid, opno,
2281 iswrite ? "uring_write" : "uring_read", e);
2284 if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2286 printf("%d/%d: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2287 iswrite ? "uring_write" : "uring_read", e);
2291 printf("%d/%d: %s %s%s [%lld, %d(res=%d)] %d\n",
2292 procid, opno, iswrite ? "uring_write" : "uring_read",
2293 f.path, st, (long long)off, (int)len, cqe->res, e);
2294 io_uring_cqe_seen(&ring, cqe);
2306 aread_f(int opno, long r)
2309 do_aio_rw(opno, r, O_RDONLY);
2314 attr_remove_f(int opno, long r)
2316 attrlist_ent_t *aep;
2320 attrlist_cursor_t cursor;
2329 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2330 append_pathname(&f, ".");
2332 bzero(&cursor, sizeof(cursor));
2334 bzero(buf, sizeof(buf));
2335 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2339 alist = (attrlist_t *)buf;
2340 total += alist->al_count;
2341 } while (alist->al_more);
2344 printf("%d/%d: attr_remove - no attrs for %s\n",
2345 procid, opno, f.path);
2349 which = (int)(random() % total);
2350 bzero(&cursor, sizeof(cursor));
2354 bzero(buf, sizeof(buf));
2355 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2359 alist = (attrlist_t *)buf;
2360 if (which < ent + alist->al_count) {
2361 aep = (attrlist_ent_t *)
2362 &buf[alist->al_offset[which - ent]];
2363 aname = aep->a_name;
2366 ent += alist->al_count;
2367 } while (alist->al_more);
2368 if (aname == NULL) {
2371 "%d/%d: attr_remove - name %d not found at %s\n",
2372 procid, opno, which, f.path);
2376 if (attr_remove_path(&f, aname) < 0)
2382 printf("%d/%d: attr_remove %s %s %d\n",
2383 procid, opno, f.path, aname, e);
2388 attr_set_f(int opno, long r)
2395 static int lengths[] = { 10, 100, 1000, 10000 };
2400 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2401 append_pathname(&f, ".");
2402 sprintf(aname, "a%x", nameseq++);
2403 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2404 len = (int)(random() % lengths[li]);
2408 memset(aval, nameseq & 0xff, len);
2409 if (attr_set_path(&f, aname, aval, len) < 0)
2416 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2422 awrite_f(int opno, long r)
2425 do_aio_rw(opno, r, O_WRONLY);
2430 bulkstat_f(int opno, long r)
2436 struct xfs_bstat *t;
2438 struct xfs_fsop_bulkreq bsr;
2441 nent = (r % 999) + 2;
2442 t = malloc(nent * sizeof(*t));
2443 fd = open(".", O_RDONLY);
2451 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2455 printf("%d/%d: bulkstat nent %d total %lld\n",
2456 procid, opno, nent, (long long)total);
2461 bulkstat1_f(int opno, long r)
2471 struct xfs_fsop_bulkreq bsr;
2474 good = random() & 1;
2476 /* use an inode we know exists */
2478 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2479 append_pathname(&f, ".");
2480 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2485 * pick a random inode
2487 * note this can generate kernel warning messages
2488 * since bulkstat_one will read the disk block that
2489 * would contain a given inode even if that disk
2490 * block doesn't contain inodes.
2492 * this is detected later, but not until after the
2493 * warning is displayed.
2495 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2501 fd = open(".", O_RDONLY);
2507 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2509 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2510 procid, opno, good?"real":"random",
2511 verifiable_log ? -1LL : (long long)ino, e);
2516 chown_f(int opno, long r)
2526 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2527 append_pathname(&f, ".");
2528 u = (uid_t)random();
2529 g = (gid_t)random();
2530 nbits = (int)(random() % idmodulo);
2531 u &= (1 << nbits) - 1;
2532 g &= (1 << nbits) - 1;
2533 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2536 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2540 /* reflink some arbitrary range of f1 to f2. */
2547 struct file_clone_range fcr;
2548 struct pathname fpath1;
2549 struct pathname fpath2;
2550 struct stat64 stat1;
2551 struct stat64 stat2;
2552 char inoinfo1[1024];
2553 char inoinfo2[1024];
2567 init_pathname(&fpath1);
2568 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2570 printf("%d/%d: clonerange read - no filename\n",
2575 init_pathname(&fpath2);
2576 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2578 printf("%d/%d: clonerange write - no filename\n",
2584 fd1 = open_path(&fpath1, O_RDONLY);
2585 e = fd1 < 0 ? errno : 0;
2589 printf("%d/%d: clonerange read - open %s failed %d\n",
2590 procid, opno, fpath1.path, e);
2594 fd2 = open_path(&fpath2, O_WRONLY);
2595 e = fd2 < 0 ? errno : 0;
2599 printf("%d/%d: clonerange write - open %s failed %d\n",
2600 procid, opno, fpath2.path, e);
2604 /* Get file stats */
2605 if (fstat64(fd1, &stat1) < 0) {
2607 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2608 procid, opno, fpath1.path, errno);
2611 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2613 if (fstat64(fd2, &stat2) < 0) {
2615 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2616 procid, opno, fpath2.path, errno);
2619 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2621 /* Calculate offsets */
2622 len = (random() % FILELEN_MAX) + 1;
2623 len &= ~(stat1.st_blksize - 1);
2625 len = stat1.st_blksize;
2626 if (len > stat1.st_size)
2627 len = stat1.st_size;
2629 lr = ((int64_t)random() << 32) + random();
2630 if (stat1.st_size == len)
2633 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2635 off1 &= ~(stat1.st_blksize - 1);
2638 * If srcfile == destfile, randomly generate destination ranges
2639 * until we find one that doesn't overlap the source range.
2641 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2643 lr = ((int64_t)random() << 32) + random();
2644 off2 = (off64_t)(lr % max_off2);
2646 off2 &= ~(stat2.st_blksize - 1);
2647 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2649 /* Clone data blocks */
2651 fcr.src_offset = off1;
2652 fcr.src_length = len;
2653 fcr.dest_offset = off2;
2655 ret = ioctl(fd2, FICLONERANGE, &fcr);
2656 e = ret < 0 ? errno : 0;
2658 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2660 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2661 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2664 printf(" error %d", e);
2673 free_pathname(&fpath2);
2675 free_pathname(&fpath1);
2679 /* copy some arbitrary range of f1 to f2. */
2685 #ifdef HAVE_COPY_FILE_RANGE
2686 struct pathname fpath1;
2687 struct pathname fpath2;
2688 struct stat64 stat1;
2689 struct stat64 stat2;
2690 char inoinfo1[1024];
2691 char inoinfo2[1024];
2709 init_pathname(&fpath1);
2710 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2712 printf("%d/%d: copyrange read - no filename\n",
2717 init_pathname(&fpath2);
2718 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2720 printf("%d/%d: copyrange write - no filename\n",
2726 fd1 = open_path(&fpath1, O_RDONLY);
2727 e = fd1 < 0 ? errno : 0;
2731 printf("%d/%d: copyrange read - open %s failed %d\n",
2732 procid, opno, fpath1.path, e);
2736 fd2 = open_path(&fpath2, O_WRONLY);
2737 e = fd2 < 0 ? errno : 0;
2741 printf("%d/%d: copyrange write - open %s failed %d\n",
2742 procid, opno, fpath2.path, e);
2746 /* Get file stats */
2747 if (fstat64(fd1, &stat1) < 0) {
2749 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2750 procid, opno, fpath1.path, errno);
2753 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2755 if (fstat64(fd2, &stat2) < 0) {
2757 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2758 procid, opno, fpath2.path, errno);
2761 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2763 /* Calculate offsets */
2764 len = (random() % FILELEN_MAX) + 1;
2766 len = stat1.st_blksize;
2767 if (len > stat1.st_size)
2768 len = stat1.st_size;
2770 lr = ((int64_t)random() << 32) + random();
2771 if (stat1.st_size == len)
2774 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2778 * If srcfile == destfile, randomly generate destination ranges
2779 * until we find one that doesn't overlap the source range.
2781 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2783 lr = ((int64_t)random() << 32) + random();
2784 off2 = (off64_t)(lr % max_off2);
2786 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2789 * Since len, off1 and off2 will be changed later, preserve their
2797 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2800 if (errno != EAGAIN || tries++ >= 300)
2802 } else if (ret > len || ret == 0)
2807 e = ret < 0 ? errno : 0;
2809 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2811 fpath1.path, inoinfo1,
2812 (long long)offset1, (long long)length,
2813 fpath2.path, inoinfo2,
2814 (long long)offset2, (long long)length);
2817 printf(" error %d", e);
2818 else if (len && ret > len)
2819 printf(" asked for %lld, copied %lld??\n",
2820 (long long)len, (long long)ret);
2829 free_pathname(&fpath2);
2831 free_pathname(&fpath1);
2835 /* dedupe some arbitrary range of f1 to f2...fn. */
2841 #ifdef FIDEDUPERANGE
2842 #define INFO_SZ 1024
2843 struct file_dedupe_range *fdr;
2844 struct pathname *fpath;
2845 struct stat64 *stat;
2857 if (flist[FT_REG].nfiles < 2)
2860 /* Pick somewhere between 2 and 128 files. */
2862 nr = random() % (flist[FT_REG].nfiles + 1);
2863 } while (nr < 2 || nr > 128);
2866 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2867 sizeof(struct file_dedupe_range));
2869 printf("%d/%d: line %d error %d\n",
2870 procid, opno, __LINE__, errno);
2873 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2874 sizeof(struct file_dedupe_range)));
2876 fpath = calloc(nr, sizeof(struct pathname));
2878 printf("%d/%d: line %d error %d\n",
2879 procid, opno, __LINE__, errno);
2883 stat = calloc(nr, sizeof(struct stat64));
2885 printf("%d/%d: line %d error %d\n",
2886 procid, opno, __LINE__, errno);
2890 info = calloc(nr, INFO_SZ);
2892 printf("%d/%d: line %d error %d\n",
2893 procid, opno, __LINE__, errno);
2897 off = calloc(nr, sizeof(off64_t));
2899 printf("%d/%d: line %d error %d\n",
2900 procid, opno, __LINE__, errno);
2904 v = calloc(nr, sizeof(int));
2906 printf("%d/%d: line %d error %d\n",
2907 procid, opno, __LINE__, errno);
2910 fd = calloc(nr, sizeof(int));
2912 printf("%d/%d: line %d error %d\n",
2913 procid, opno, __LINE__, errno);
2916 memset(fd, 0xFF, nr * sizeof(int));
2918 /* Get paths for all files */
2919 for (i = 0; i < nr; i++)
2920 init_pathname(&fpath[i]);
2922 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2924 printf("%d/%d: deduperange read - no filename\n",
2929 for (i = 1; i < nr; i++) {
2930 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2932 printf("%d/%d: deduperange write - no filename\n",
2939 fd[0] = open_path(&fpath[0], O_RDONLY);
2940 e = fd[0] < 0 ? errno : 0;
2944 printf("%d/%d: deduperange read - open %s failed %d\n",
2945 procid, opno, fpath[0].path, e);
2949 for (i = 1; i < nr; i++) {
2950 fd[i] = open_path(&fpath[i], O_WRONLY);
2951 e = fd[i] < 0 ? errno : 0;
2955 printf("%d/%d: deduperange write - open %s failed %d\n",
2956 procid, opno, fpath[i].path, e);
2961 /* Get file stats */
2962 if (fstat64(fd[0], &stat[0]) < 0) {
2964 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2965 procid, opno, fpath[0].path, errno);
2969 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2971 for (i = 1; i < nr; i++) {
2972 if (fstat64(fd[i], &stat[i]) < 0) {
2974 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2975 procid, opno, fpath[i].path, errno);
2978 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2981 /* Never try to dedupe more than half of the src file. */
2982 len = (random() % FILELEN_MAX) + 1;
2983 len &= ~(stat[0].st_blksize - 1);
2985 len = stat[0].st_blksize / 2;
2986 if (len > stat[0].st_size / 2)
2987 len = stat[0].st_size / 2;
2989 /* Calculate offsets */
2990 lr = ((int64_t)random() << 32) + random();
2991 if (stat[0].st_size == len)
2994 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2996 off[0] &= ~(stat[0].st_blksize - 1);
2999 * If srcfile == destfile[i], randomly generate destination ranges
3000 * until we find one that doesn't overlap the source range.
3002 for (i = 1; i < nr; i++) {
3006 lr = ((int64_t)random() << 32) + random();
3007 if (stat[i].st_size <= len)
3010 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
3012 off[i] &= ~(stat[i].st_blksize - 1);
3013 } while (stat[0].st_ino == stat[i].st_ino &&
3014 llabs(off[i] - off[0]) < len &&
3018 /* Clone data blocks */
3019 fdr->src_offset = off[0];
3020 fdr->src_length = len;
3021 fdr->dest_count = nr - 1;
3022 for (i = 1; i < nr; i++) {
3023 fdr->info[i - 1].dest_fd = fd[i];
3024 fdr->info[i - 1].dest_offset = off[i];
3027 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3028 e = ret < 0 ? errno : 0;
3030 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
3032 fpath[0].path, &info[0], (long long)off[0],
3035 printf(" error %d", e);
3041 for (i = 1; i < nr; i++) {
3042 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3044 printf("%d/%d: ...to %s%s [%lld,%lld]",
3046 fpath[i].path, &info[i * INFO_SZ],
3047 (long long)off[i], (long long)len);
3048 if (fdr->info[i - 1].status < 0)
3049 printf(" error %d", e);
3050 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3051 printf(" %llu bytes deduplicated",
3052 fdr->info[i - 1].bytes_deduped);
3053 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3054 printf(" differed");
3060 for (i = 0; i < nr; i++)
3064 for (i = 0; i < nr; i++)
3065 free_pathname(&fpath[i]);
3084 setxattr_f(int opno, long r)
3086 #ifdef XFS_XFLAG_EXTSIZE
3096 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3097 append_pathname(&f, ".");
3098 fd = open_path(&f, O_RDWR);
3099 e = fd < 0 ? errno : 0;
3104 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3105 nbits = (int)(random() % e);
3106 p &= (1 << nbits) - 1;
3108 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3110 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3113 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3120 splice_f(int opno, long r)
3122 struct pathname fpath1;
3123 struct pathname fpath2;
3124 struct stat64 stat1;
3125 struct stat64 stat2;
3126 char inoinfo1[1024];
3127 char inoinfo2[1024];
3131 loff_t offset1, offset2;
3138 ssize_t ret1 = 0, ret2 = 0;
3144 init_pathname(&fpath1);
3145 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3147 printf("%d/%d: splice read - no filename\n",
3152 init_pathname(&fpath2);
3153 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3155 printf("%d/%d: splice write - no filename\n",
3161 fd1 = open_path(&fpath1, O_RDONLY);
3162 e = fd1 < 0 ? errno : 0;
3166 printf("%d/%d: splice read - open %s failed %d\n",
3167 procid, opno, fpath1.path, e);
3171 fd2 = open_path(&fpath2, O_WRONLY);
3172 e = fd2 < 0 ? errno : 0;
3176 printf("%d/%d: splice write - open %s failed %d\n",
3177 procid, opno, fpath2.path, e);
3181 /* Get file stats */
3182 if (fstat64(fd1, &stat1) < 0) {
3184 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3185 procid, opno, fpath1.path, errno);
3188 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3190 if (fstat64(fd2, &stat2) < 0) {
3192 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3193 procid, opno, fpath2.path, errno);
3196 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3198 /* Calculate offsets */
3199 len = (random() % FILELEN_MAX) + 1;
3201 len = stat1.st_blksize;
3202 if (len > stat1.st_size)
3203 len = stat1.st_size;
3205 lr = ((int64_t)random() << 32) + random();
3206 if (stat1.st_size == len)
3209 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3213 * splice can overlap write, so the offset of the target file can be
3214 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3215 * past the current dest file EOF
3217 lr = ((int64_t)random() << 32) + random();
3218 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3221 * Since len, off1 and off2 will be changed later, preserve their
3228 /* Pipe initialize */
3229 if (pipe(filedes) < 0) {
3231 printf("%d/%d: splice - pipe failed %d\n",
3232 procid, opno, errno);
3240 /* move to pipe buffer */
3241 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3247 /* move from pipe buffer to dst file */
3249 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3262 if (ret1 < 0 || ret2 < 0)
3267 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3269 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3270 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3272 if (length && length > total)
3273 printf(" asked for %lld, spliced %lld??\n",
3274 (long long)length, (long long)total);
3285 free_pathname(&fpath2);
3287 free_pathname(&fpath1);
3291 creat_f(int opno, long r)
3306 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3311 e1 = (random() % 100);
3312 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3314 if (type == FT_RTF) /* rt always gets an extsize */
3315 extsize = (random() % 10) + 1;
3316 else if (e1 < 10) /* one-in-ten get an extsize */
3317 extsize = random() % 1024;
3321 e = generate_fname(fep, type, &f, &id, &v);
3325 (void)fent_to_name(&f, fep);
3326 printf("%d/%d: creat - no filename from %s\n",
3327 procid, opno, f.path);
3332 fd = creat_path(&f, 0666);
3333 e = fd < 0 ? errno : 0;
3338 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3339 if (type == FT_RTF) {
3340 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3341 a.fsx_extsize = extsize *
3342 geom.rtextsize * geom.blocksize;
3344 } else if (extsize) {
3345 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3346 a.fsx_extsize = extsize * geom.blocksize;
3349 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3352 add_to_flist(type, id, parid, 0);
3356 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3357 extsize ? a.fsx_extsize : 0, e, e1);
3358 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3364 dread_f(int opno, long r)
3368 struct dioattr diob;
3381 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3383 printf("%d/%d: dread - no filename\n", procid, opno);
3387 fd = open_path(&f, O_RDONLY|O_DIRECT);
3388 e = fd < 0 ? errno : 0;
3392 printf("%d/%d: dread - open %s failed %d\n",
3393 procid, opno, f.path, e);
3397 if (fstat64(fd, &stb) < 0) {
3399 printf("%d/%d: dread - fstat64 %s failed %d\n",
3400 procid, opno, f.path, errno);
3405 inode_info(st, sizeof(st), &stb, v);
3406 if (stb.st_size == 0) {
3408 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3414 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3417 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3418 " fallback to stat()\n",
3419 procid, opno, f.path, st, errno);
3420 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3421 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3424 dio_env = getenv("XFS_DIO_MIN");
3426 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3428 align = (int64_t)diob.d_miniosz;
3429 lr = ((int64_t)random() << 32) + random();
3430 off = (off64_t)(lr % stb.st_size);
3431 off -= (off % align);
3432 lseek64(fd, off, SEEK_SET);
3433 len = (random() % FILELEN_MAX) + 1;
3434 len -= (len % align);
3437 else if (len > diob.d_maxiosz)
3438 len = diob.d_maxiosz;
3439 buf = memalign(diob.d_mem, len);
3440 e = read(fd, buf, len) < 0 ? errno : 0;
3443 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3444 procid, opno, f.path, st, (long long)off, (int)len, e);
3450 dwrite_f(int opno, long r)
3454 struct dioattr diob;
3467 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3469 printf("%d/%d: dwrite - no filename\n", procid, opno);
3473 fd = open_path(&f, O_WRONLY|O_DIRECT);
3474 e = fd < 0 ? errno : 0;
3478 printf("%d/%d: dwrite - open %s failed %d\n",
3479 procid, opno, f.path, e);
3483 if (fstat64(fd, &stb) < 0) {
3485 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3486 procid, opno, f.path, errno);
3491 inode_info(st, sizeof(st), &stb, v);
3492 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3494 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3495 " %s%s return %d, fallback to stat()\n",
3496 procid, opno, f.path, st, errno);
3497 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3498 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3501 dio_env = getenv("XFS_DIO_MIN");
3503 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3505 align = (int64_t)diob.d_miniosz;
3506 lr = ((int64_t)random() << 32) + random();
3507 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3508 off -= (off % align);
3509 lseek64(fd, off, SEEK_SET);
3510 len = (random() % FILELEN_MAX) + 1;
3511 len -= (len % align);
3514 else if (len > diob.d_maxiosz)
3515 len = diob.d_maxiosz;
3516 buf = memalign(diob.d_mem, len);
3518 lseek64(fd, off, SEEK_SET);
3519 memset(buf, nameseq & 0xff, len);
3520 e = write(fd, buf, len) < 0 ? errno : 0;
3523 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3524 procid, opno, f.path, st, (long long)off, (int)len, e);
3530 #ifdef HAVE_LINUX_FALLOC_H
3531 struct print_flags falloc_flags [] = {
3532 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3533 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3534 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3535 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3536 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3537 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3541 #define translate_falloc_flags(mode) \
3542 ({translate_flags(mode, "|", falloc_flags);})
3546 do_fallocate(int opno, long r, int mode)
3548 #ifdef HAVE_LINUX_FALLOC_H
3560 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3562 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3566 fd = open_path(&f, O_RDWR);
3569 printf("%d/%d: do_fallocate - open %s failed %d\n",
3570 procid, opno, f.path, errno);
3575 if (fstat64(fd, &stb) < 0) {
3577 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3578 procid, opno, f.path, errno);
3583 inode_info(st, sizeof(st), &stb, v);
3584 lr = ((int64_t)random() << 32) + random();
3585 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3587 len = (off64_t)(random() % (1024 * 1024));
3589 * Collapse/insert range requires off and len to be block aligned,
3590 * make it more likely to be the case.
3592 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3594 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3595 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3597 mode |= FALLOC_FL_KEEP_SIZE & random();
3598 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3600 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3601 procid, opno, translate_falloc_flags(mode),
3602 f.path, st, (long long)off, (long long)len, e);
3609 fallocate_f(int opno, long r)
3611 #ifdef HAVE_LINUX_FALLOC_H
3612 do_fallocate(opno, r, 0);
3617 fdatasync_f(int opno, long r)
3625 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3627 printf("%d/%d: fdatasync - no filename\n",
3632 fd = open_path(&f, O_WRONLY);
3633 e = fd < 0 ? errno : 0;
3637 printf("%d/%d: fdatasync - open %s failed %d\n",
3638 procid, opno, f.path, e);
3642 e = fdatasync(fd) < 0 ? errno : 0;
3644 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3649 #ifdef HAVE_LINUX_FIEMAP_H
3650 struct print_flags fiemap_flags[] = {
3651 { FIEMAP_FLAG_SYNC, "SYNC"},
3652 { FIEMAP_FLAG_XATTR, "XATTR"},
3656 #define translate_fiemap_flags(mode) \
3657 ({translate_flags(mode, "|", fiemap_flags);})
3661 fiemap_f(int opno, long r)
3663 #ifdef HAVE_LINUX_FIEMAP_H
3673 struct fiemap *fiemap;
3676 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3678 printf("%d/%d: fiemap - no filename\n", procid, opno);
3682 fd = open_path(&f, O_RDWR);
3683 e = fd < 0 ? errno : 0;
3687 printf("%d/%d: fiemap - open %s failed %d\n",
3688 procid, opno, f.path, e);
3692 if (fstat64(fd, &stb) < 0) {
3694 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3695 procid, opno, f.path, errno);
3700 inode_info(st, sizeof(st), &stb, v);
3701 blocks_to_map = random() & 0xffff;
3702 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3703 (blocks_to_map * sizeof(struct fiemap_extent)));
3706 printf("%d/%d: malloc failed \n", procid, opno);
3711 lr = ((int64_t)random() << 32) + random();
3712 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3714 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3715 fiemap->fm_extent_count = blocks_to_map;
3716 fiemap->fm_mapped_extents = random() & 0xffff;
3717 fiemap->fm_start = off;
3718 fiemap->fm_length = ((int64_t)random() << 32) + random();
3720 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3722 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3723 procid, opno, f.path, st, (long long)fiemap->fm_start,
3724 (long long) fiemap->fm_length,
3725 translate_fiemap_flags(fiemap->fm_flags), e);
3733 freesp_f(int opno, long r)
3738 struct xfs_flock64 fl;
3746 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3748 printf("%d/%d: freesp - no filename\n", procid, opno);
3752 fd = open_path(&f, O_RDWR);
3753 e = fd < 0 ? errno : 0;
3757 printf("%d/%d: freesp - open %s failed %d\n",
3758 procid, opno, f.path, e);
3762 if (fstat64(fd, &stb) < 0) {
3764 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3765 procid, opno, f.path, errno);
3770 inode_info(st, sizeof(st), &stb, v);
3771 lr = ((int64_t)random() << 32) + random();
3772 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3774 fl.l_whence = SEEK_SET;
3777 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3779 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3780 procid, opno, f.path, st, (long long)off, e);
3786 fsync_f(int opno, long r)
3794 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3796 printf("%d/%d: fsync - no filename\n", procid, opno);
3800 fd = open_file_or_dir(&f, O_WRONLY);
3801 e = fd < 0 ? errno : 0;
3805 printf("%d/%d: fsync - open %s failed %d\n",
3806 procid, opno, f.path, e);
3810 e = fsync(fd) < 0 ? errno : 0;
3812 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3818 gen_random_string(int len)
3820 static const char charset[] = "0123456789"
3821 "abcdefghijklmnopqrstuvwxyz"
3822 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3833 for (i = 0; i < len; i++)
3834 s[i] = charset[random() % sizeof(charset)];
3840 getattr_f(int opno, long r)
3849 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3850 append_pathname(&f, ".");
3851 fd = open_path(&f, O_RDWR);
3852 e = fd < 0 ? errno : 0;
3855 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3857 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3863 getdents_f(int opno, long r)
3870 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3871 append_pathname(&f, ".");
3872 dir = opendir_path(&f);
3876 printf("%d/%d: getdents - can't open %s\n",
3877 procid, opno, f.path);
3881 while (readdir64(dir) != NULL)
3884 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3890 getfattr_f(int opno, long r)
3896 char name[XATTR_NAME_BUF_SIZE];
3902 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3904 printf("%d/%d: getfattr - no filename\n", procid, opno);
3910 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3911 * to read a xattr that doesn't exist (fgetxattr should fail with
3912 * errno set to ENOATTR (61) in this case).
3914 if (fep->xattr_counter > 0)
3915 xattr_num = (random() % fep->xattr_counter) + 1;
3919 e = generate_xattr_name(xattr_num, name, sizeof(name));
3921 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3922 procid, opno, f.path, e);
3926 value_len = getxattr(f.path, name, NULL, 0);
3927 if (value_len < 0) {
3929 printf("%d/%d: getfattr file %s name %s failed %d\n",
3930 procid, opno, f.path, name, errno);
3934 /* A xattr without value.*/
3935 if (value_len == 0) {
3940 value = malloc(value_len);
3943 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3944 procid, opno, f.path, value_len);
3948 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3951 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3952 procid, opno, f.path, name, value_len, e);
3959 link_f(int opno, long r)
3973 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3975 printf("%d/%d: link - no file\n", procid, opno);
3979 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3985 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3989 (void)fent_to_name(&l, fep);
3990 printf("%d/%d: link - no filename from %s\n",
3991 procid, opno, l.path);
3997 e = link_path(&f, &l) < 0 ? errno : 0;
4000 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
4002 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
4004 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
4011 listfattr_f(int opno, long r)
4017 char *buffer = NULL;
4021 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4023 printf("%d/%d: listfattr - no filename\n", procid, opno);
4028 e = listxattr(f.path, NULL, 0);
4031 printf("%d/%d: listfattr %s failed %d\n",
4032 procid, opno, f.path, errno);
4036 if (buffer_len == 0) {
4038 printf("%d/%d: listfattr %s - has no extended attributes\n",
4039 procid, opno, f.path);
4043 buffer = malloc(buffer_len);
4046 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
4047 procid, opno, f.path, buffer_len);
4051 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4053 printf("%d/%d: listfattr %s buffer length %d %d\n",
4054 procid, opno, f.path, buffer_len, e);
4061 mkdir_f(int opno, long r)
4071 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4076 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4080 (void)fent_to_name(&f, fep);
4081 printf("%d/%d: mkdir - no filename from %s\n",
4082 procid, opno, f.path);
4087 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4090 add_to_flist(FT_DIR, id, parid, 0);
4092 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
4093 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4099 mknod_f(int opno, long r)
4109 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4114 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4118 (void)fent_to_name(&f, fep);
4119 printf("%d/%d: mknod - no filename from %s\n",
4120 procid, opno, f.path);
4125 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4128 add_to_flist(FT_DEV, id, parid, 0);
4130 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
4131 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4136 #ifdef HAVE_SYS_MMAN_H
4137 struct print_flags mmap_flags[] = {
4138 { MAP_SHARED, "SHARED"},
4139 { MAP_PRIVATE, "PRIVATE"},
4143 #define translate_mmap_flags(flags) \
4144 ({translate_flags(flags, "|", mmap_flags);})
4148 do_mmap(int opno, long r, int prot)
4150 #ifdef HAVE_SYS_MMAN_H
4162 sigjmp_buf sigbus_jmpbuf;
4165 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4167 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4171 fd = open_path(&f, O_RDWR);
4172 e = fd < 0 ? errno : 0;
4176 printf("%d/%d: do_mmap - open %s failed %d\n",
4177 procid, opno, f.path, e);
4181 if (fstat64(fd, &stb) < 0) {
4183 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4184 procid, opno, f.path, errno);
4189 inode_info(st, sizeof(st), &stb, v);
4190 if (stb.st_size == 0) {
4192 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4199 lr = ((int64_t)random() << 32) + random();
4200 off = (off64_t)(lr % stb.st_size);
4201 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4202 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4204 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4205 addr = mmap(NULL, len, prot, flags, fd, off);
4206 e = (addr == MAP_FAILED) ? errno : 0;
4209 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4210 procid, opno, f.path, st, (long long)off,
4211 (int)len, translate_mmap_flags(flags), e);
4217 if (prot & PROT_WRITE) {
4218 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4219 sigbus_jmp = &sigbus_jmpbuf;
4220 memset(addr, nameseq & 0xff, len);
4224 if ((buf = malloc(len)) != NULL) {
4225 memcpy(buf, addr, len);
4230 /* set NULL to stop other functions from doing siglongjmp */
4234 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4235 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4236 f.path, st, (long long)off, (int)len,
4237 translate_mmap_flags(flags),
4238 (e == 0) ? "0" : "Bus error");
4246 mread_f(int opno, long r)
4248 #ifdef HAVE_SYS_MMAN_H
4249 do_mmap(opno, r, PROT_READ);
4254 mwrite_f(int opno, long r)
4256 #ifdef HAVE_SYS_MMAN_H
4257 do_mmap(opno, r, PROT_WRITE);
4262 punch_f(int opno, long r)
4264 #ifdef HAVE_LINUX_FALLOC_H
4265 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4270 zero_f(int opno, long r)
4272 #ifdef HAVE_LINUX_FALLOC_H
4273 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4278 collapse_f(int opno, long r)
4280 #ifdef HAVE_LINUX_FALLOC_H
4281 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4286 insert_f(int opno, long r)
4288 #ifdef HAVE_LINUX_FALLOC_H
4289 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4294 read_f(int opno, long r)
4308 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4310 printf("%d/%d: read - no filename\n", procid, opno);
4314 fd = open_path(&f, O_RDONLY);
4315 e = fd < 0 ? errno : 0;
4319 printf("%d/%d: read - open %s failed %d\n",
4320 procid, opno, f.path, e);
4324 if (fstat64(fd, &stb) < 0) {
4326 printf("%d/%d: read - fstat64 %s failed %d\n",
4327 procid, opno, f.path, errno);
4332 inode_info(st, sizeof(st), &stb, v);
4333 if (stb.st_size == 0) {
4335 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4341 lr = ((int64_t)random() << 32) + random();
4342 off = (off64_t)(lr % stb.st_size);
4343 lseek64(fd, off, SEEK_SET);
4344 len = (random() % FILELEN_MAX) + 1;
4346 e = read(fd, buf, len) < 0 ? errno : 0;
4349 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4350 procid, opno, f.path, st, (long long)off, (int)len, e);
4356 readlink_f(int opno, long r)
4364 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4366 printf("%d/%d: readlink - no filename\n", procid, opno);
4370 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4373 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4378 readv_f(int opno, long r)
4390 struct iovec *iov = NULL;
4397 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4399 printf("%d/%d: readv - no filename\n", procid, opno);
4403 fd = open_path(&f, O_RDONLY);
4404 e = fd < 0 ? errno : 0;
4408 printf("%d/%d: readv - open %s failed %d\n",
4409 procid, opno, f.path, e);
4413 if (fstat64(fd, &stb) < 0) {
4415 printf("%d/%d: readv - fstat64 %s failed %d\n",
4416 procid, opno, f.path, errno);
4421 inode_info(st, sizeof(st), &stb, v);
4422 if (stb.st_size == 0) {
4424 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4430 lr = ((int64_t)random() << 32) + random();
4431 off = (off64_t)(lr % stb.st_size);
4432 lseek64(fd, off, SEEK_SET);
4433 len = (random() % FILELEN_MAX) + 1;
4436 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4437 iov = calloc(iovcnt, sizeof(struct iovec));
4438 iovl = len / iovcnt;
4440 for (i=0; i<iovcnt; i++) {
4441 (iov + i)->iov_base = (buf + iovb);
4442 (iov + i)->iov_len = iovl;
4446 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4449 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4450 procid, opno, f.path, st, (long long)off, (int)iovl,
4457 removefattr_f(int opno, long r)
4463 char name[XATTR_NAME_BUF_SIZE];
4467 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4469 printf("%d/%d: removefattr - no filename\n", procid, opno);
4475 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4476 * remove a xattr that doesn't exist (fremovexattr should fail with
4477 * errno set to ENOATTR (61) in this case).
4479 if (fep->xattr_counter > 0)
4480 xattr_num = (random() % fep->xattr_counter) + 1;
4484 e = generate_xattr_name(xattr_num, name, sizeof(name));
4486 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4487 procid, opno, f.path, e);
4491 e = removexattr(f.path, name) < 0 ? errno : 0;
4493 printf("%d/%d: removefattr file %s name %s %d\n",
4494 procid, opno, f.path, name, e);
4499 struct print_flags renameat2_flags [] = {
4500 { RENAME_NOREPLACE, "NOREPLACE"},
4501 { RENAME_EXCHANGE, "EXCHANGE"},
4502 { RENAME_WHITEOUT, "WHITEOUT"},
4506 #define translate_renameat2_flags(mode) \
4507 ({translate_flags(mode, "|", renameat2_flags);})
4510 do_renameat2(int opno, long r, int mode)
4526 /* get an existing path for the source of the rename */
4528 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4529 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4531 printf("%d/%d: rename - no source filename\n",
4538 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4539 * order to maintain filelist/filename integrity, we should
4540 * restrict exchange operation to files of the same type.
4542 if (mode == RENAME_EXCHANGE) {
4543 which = 1 << (flp - flist);
4544 init_pathname(&newf);
4545 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4547 printf("%d/%d: rename - no target filename\n",
4549 free_pathname(&newf);
4553 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4554 fents_ancestor_check(dfep, fep))) {
4556 printf("%d/%d: rename(REXCHANGE) %s and %s "
4557 "have ancestor-descendant relationship\n",
4558 procid, opno, f.path, newf.path);
4559 free_pathname(&newf);
4565 parid = dfep->parent;
4568 * Get an existing directory for the destination parent
4571 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4578 * Generate a new path using an existing parent directory
4581 init_pathname(&newf);
4582 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4586 (void)fent_to_name(&f, dfep);
4587 printf("%d/%d: rename - no filename from %s\n",
4588 procid, opno, f.path);
4590 free_pathname(&newf);
4595 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4598 int xattr_counter = fep->xattr_counter;
4599 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4600 int ft = flp - flist;
4603 oldparid = fep->parent;
4606 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4607 * old parent id for the others.
4609 if (ft == FT_DIR || ft == FT_SUBVOL)
4610 fix_parent(oldid, id, swap);
4612 if (mode == RENAME_WHITEOUT) {
4613 fep->xattr_counter = 0;
4614 add_to_flist(flp - flist, id, parid, xattr_counter);
4615 } else if (mode == RENAME_EXCHANGE) {
4616 fep->xattr_counter = dfep->xattr_counter;
4617 dfep->xattr_counter = xattr_counter;
4619 del_from_flist(flp - flist, fep - flp->fents);
4620 add_to_flist(flp - flist, id, parid, xattr_counter);
4624 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4625 opno, translate_renameat2_flags(mode), f.path,
4628 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4629 procid, opno, oldid, oldparid);
4630 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4631 procid, opno, id, parid);
4634 free_pathname(&newf);
4639 rename_f(int opno, long r)
4641 do_renameat2(opno, r, 0);
4645 rnoreplace_f(int opno, long r)
4647 do_renameat2(opno, r, RENAME_NOREPLACE);
4651 rexchange_f(int opno, long r)
4653 do_renameat2(opno, r, RENAME_EXCHANGE);
4657 rwhiteout_f(int opno, long r)
4659 do_renameat2(opno, r, RENAME_WHITEOUT);
4663 resvsp_f(int opno, long r)
4668 struct xfs_flock64 fl;
4676 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4678 printf("%d/%d: resvsp - no filename\n", procid, opno);
4682 fd = open_path(&f, O_RDWR);
4683 e = fd < 0 ? errno : 0;
4687 printf("%d/%d: resvsp - open %s failed %d\n",
4688 procid, opno, f.path, e);
4692 if (fstat64(fd, &stb) < 0) {
4694 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4695 procid, opno, f.path, errno);
4700 inode_info(st, sizeof(st), &stb, v);
4701 lr = ((int64_t)random() << 32) + random();
4702 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4704 fl.l_whence = SEEK_SET;
4706 fl.l_len = (off64_t)(random() % (1024 * 1024));
4707 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4709 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4710 procid, opno, f.path, st,
4711 (long long)off, (long long)fl.l_len, e);
4717 rmdir_f(int opno, long r)
4727 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4729 printf("%d/%d: rmdir - no directory\n", procid, opno);
4733 e = rmdir_path(&f) < 0 ? errno : 0;
4737 oldparid = fep->parent;
4738 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4741 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4743 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4744 procid, opno, oldid, oldparid);
4750 setattr_f(int opno, long r)
4759 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4760 append_pathname(&f, ".");
4761 fd = open_path(&f, O_RDWR);
4762 e = fd < 0 ? errno : 0;
4765 fl = attr_mask & (uint)random();
4766 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4768 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4774 setfattr_f(int opno, long r)
4781 char name[XATTR_NAME_BUF_SIZE];
4787 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4789 printf("%d/%d: setfattr - no filename\n", procid, opno);
4794 if ((fep->xattr_counter > 0) && (random() % 2)) {
4796 * Use an existing xattr name for replacing its value or
4797 * create again a xattr that was previously deleted.
4799 xattr_num = (random() % fep->xattr_counter) + 1;
4801 flag = XATTR_REPLACE;
4803 /* Use a new xattr name. */
4804 xattr_num = fep->xattr_counter + 1;
4806 * Don't always use the create flag because even if our xattr
4807 * counter is 0, we may still have xattrs associated to this
4808 * file (this happens when xattrs are added to a file through
4809 * one of its other hard links), so we can end up updating an
4810 * existing xattr too.
4813 flag = XATTR_CREATE;
4817 * The maximum supported value size depends on the filesystem
4818 * implementation, but 100 bytes is a safe value for most filesystems
4821 value_len = random() % 101;
4822 value = gen_random_string(value_len);
4823 if (!value && value_len > 0) {
4825 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4826 procid, opno, f.path, value_len);
4829 e = generate_xattr_name(xattr_num, name, sizeof(name));
4831 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4832 procid, opno, f.path, e);
4836 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4838 fep->xattr_counter++;
4840 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4841 procid, opno, f.path, name, xattr_flag_to_string(flag),
4849 snapshot_f(int opno, long r)
4851 #ifdef HAVE_BTRFSUTIL_H
4852 enum btrfs_util_error e;
4863 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4865 printf("%d/%d: snapshot - no subvolume\n", procid,
4870 init_pathname(&newf);
4872 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4876 (void)fent_to_name(&f, fep);
4877 printf("%d/%d: snapshot - no filename from %s\n",
4878 procid, opno, f.path);
4883 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4884 if (e == BTRFS_UTIL_OK)
4885 add_to_flist(FT_SUBVOL, id, parid, 0);
4887 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4888 f.path, newf.path, e, btrfs_util_strerror(e));
4889 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4892 free_pathname(&newf);
4898 stat_f(int opno, long r)
4906 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4908 printf("%d/%d: stat - no entries\n", procid, opno);
4912 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4915 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4920 subvol_create_f(int opno, long r)
4922 #ifdef HAVE_BTRFSUTIL_H
4923 enum btrfs_util_error e;
4933 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4937 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4941 (void)fent_to_name(&f, fep);
4942 printf("%d/%d: subvol_create - no filename from %s\n",
4943 procid, opno, f.path);
4948 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4949 if (e == BTRFS_UTIL_OK)
4950 add_to_flist(FT_SUBVOL, id, parid, 0);
4952 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4953 f.path, e, btrfs_util_strerror(e));
4954 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4962 subvol_delete_f(int opno, long r)
4964 #ifdef HAVE_BTRFSUTIL_H
4965 enum btrfs_util_error e;
4973 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4975 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4980 e = btrfs_util_delete_subvolume(f.path, 0);
4982 if (e == BTRFS_UTIL_OK) {
4984 oldparid = fep->parent;
4985 delete_subvol_children(oldid);
4986 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4989 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4990 e, btrfs_util_strerror(e));
4991 if (e == BTRFS_UTIL_OK)
4992 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4993 procid, opno, oldid, oldparid);
5000 symlink_f(int opno, long r)
5013 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5018 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5022 (void)fent_to_name(&f, fep);
5023 printf("%d/%d: symlink - no filename from %s\n",
5024 procid, opno, f.path);
5029 len = (int)(random() % PATH_MAX);
5030 val = malloc(len + 1);
5032 memset(val, 'x', len);
5034 for (i = 10; i < len - 1; i += 10)
5036 e = symlink_path(val, &f) < 0 ? errno : 0;
5039 add_to_flist(FT_SYM, id, parid, 0);
5042 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
5043 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5050 sync_f(int opno, long r)
5054 printf("%d/%d: sync\n", procid, opno);
5058 truncate_f(int opno, long r)
5069 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5071 printf("%d/%d: truncate - no filename\n", procid, opno);
5075 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5079 printf("%d/%d: truncate - stat64 %s failed %d\n",
5080 procid, opno, f.path, e);
5084 inode_info(st, sizeof(st), &stb, v);
5085 lr = ((int64_t)random() << 32) + random();
5086 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5088 e = truncate64_path(&f, off) < 0 ? errno : 0;
5091 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
5092 st, (long long)off, e);
5097 unlink_f(int opno, long r)
5108 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5110 printf("%d/%d: unlink - no file\n", procid, opno);
5114 e = unlink_path(&f) < 0 ? errno : 0;
5118 oldparid = fep->parent;
5119 del_from_flist(flp - flist, fep - flp->fents);
5122 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
5124 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
5125 procid, opno, oldid, oldparid);
5131 unresvsp_f(int opno, long r)
5136 struct xfs_flock64 fl;
5144 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5146 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5150 fd = open_path(&f, O_RDWR);
5151 e = fd < 0 ? errno : 0;
5155 printf("%d/%d: unresvsp - open %s failed %d\n",
5156 procid, opno, f.path, e);
5160 if (fstat64(fd, &stb) < 0) {
5162 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5163 procid, opno, f.path, errno);
5168 inode_info(st, sizeof(st), &stb, v);
5169 lr = ((int64_t)random() << 32) + random();
5170 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5172 fl.l_whence = SEEK_SET;
5174 fl.l_len = (off64_t)(random() % (1 << 20));
5175 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5177 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5178 procid, opno, f.path, st,
5179 (long long)off, (long long)fl.l_len, e);
5185 uring_read_f(int opno, long r)
5188 do_uring_rw(opno, r, O_RDONLY);
5193 uring_write_f(int opno, long r)
5196 do_uring_rw(opno, r, O_WRONLY);
5201 write_f(int opno, long r)
5215 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5217 printf("%d/%d: write - no filename\n", procid, opno);
5221 fd = open_path(&f, O_WRONLY);
5222 e = fd < 0 ? errno : 0;
5226 printf("%d/%d: write - open %s failed %d\n",
5227 procid, opno, f.path, e);
5231 if (fstat64(fd, &stb) < 0) {
5233 printf("%d/%d: write - fstat64 %s failed %d\n",
5234 procid, opno, f.path, errno);
5239 inode_info(st, sizeof(st), &stb, v);
5240 lr = ((int64_t)random() << 32) + random();
5241 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5243 lseek64(fd, off, SEEK_SET);
5244 len = (random() % FILELEN_MAX) + 1;
5246 memset(buf, nameseq & 0xff, len);
5247 e = write(fd, buf, len) < 0 ? errno : 0;
5250 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5251 procid, opno, f.path, st, (long long)off, (int)len, e);
5257 writev_f(int opno, long r)
5269 struct iovec *iov = NULL;
5276 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5278 printf("%d/%d: writev - no filename\n", procid, opno);
5282 fd = open_path(&f, O_WRONLY);
5283 e = fd < 0 ? errno : 0;
5287 printf("%d/%d: writev - open %s failed %d\n",
5288 procid, opno, f.path, e);
5292 if (fstat64(fd, &stb) < 0) {
5294 printf("%d/%d: writev - fstat64 %s failed %d\n",
5295 procid, opno, f.path, errno);
5300 inode_info(st, sizeof(st), &stb, v);
5301 lr = ((int64_t)random() << 32) + random();
5302 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5304 lseek64(fd, off, SEEK_SET);
5305 len = (random() % FILELEN_MAX) + 1;
5307 memset(buf, nameseq & 0xff, len);
5309 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5310 iov = calloc(iovcnt, sizeof(struct iovec));
5311 iovl = len / iovcnt;
5313 for (i=0; i<iovcnt; i++) {
5314 (iov + i)->iov_base = (buf + iovb);
5315 (iov + i)->iov_len = iovl;
5319 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5323 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5324 procid, opno, f.path, st, (long long)off, (int)iovl,
5331 xattr_flag_to_string(int flag)
5333 if (flag == XATTR_CREATE)
5335 if (flag == XATTR_REPLACE)