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 long long opnum_t;
152 typedef void (*opfnc_t)(opnum_t, long);
154 typedef struct opdesc {
162 typedef struct fent {
169 typedef struct flist {
176 typedef struct pathname {
186 struct print_string {
193 #define FT_DIRm (1 << FT_DIR)
195 #define FT_REGm (1 << FT_REG)
197 #define FT_SYMm (1 << FT_SYM)
199 #define FT_DEVm (1 << FT_DEV)
201 #define FT_RTFm (1 << FT_RTF)
203 #define FT_SUBVOLm (1 << FT_SUBVOL)
205 #define FT_ANYm ((1 << FT_nft) - 1)
206 #define FT_REGFILE (FT_REGm | FT_RTFm)
207 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
208 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
210 #define FLIST_SLOT_INCR 16
213 #define MAXFSIZE ((1ULL << 63) - 1ULL)
214 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
216 #define XATTR_NAME_BUF_SIZE 18
218 void afsync_f(opnum_t, long);
219 void allocsp_f(opnum_t, long);
220 void aread_f(opnum_t, long);
221 void attr_remove_f(opnum_t, long);
222 void attr_set_f(opnum_t, long);
223 void awrite_f(opnum_t, long);
224 void bulkstat_f(opnum_t, long);
225 void bulkstat1_f(opnum_t, long);
226 void chown_f(opnum_t, long);
227 void clonerange_f(opnum_t, long);
228 void copyrange_f(opnum_t, long);
229 void creat_f(opnum_t, long);
230 void deduperange_f(opnum_t, long);
231 void dread_f(opnum_t, long);
232 void dwrite_f(opnum_t, long);
233 void fallocate_f(opnum_t, long);
234 void fdatasync_f(opnum_t, long);
235 void fiemap_f(opnum_t, long);
236 void freesp_f(opnum_t, long);
237 void fsync_f(opnum_t, long);
238 char *gen_random_string(int);
239 void getattr_f(opnum_t, long);
240 void getdents_f(opnum_t, long);
241 void getfattr_f(opnum_t, long);
242 void link_f(opnum_t, long);
243 void listfattr_f(opnum_t, long);
244 void mkdir_f(opnum_t, long);
245 void mknod_f(opnum_t, long);
246 void mread_f(opnum_t, long);
247 void mwrite_f(opnum_t, long);
248 void punch_f(opnum_t, long);
249 void zero_f(opnum_t, long);
250 void collapse_f(opnum_t, long);
251 void insert_f(opnum_t, long);
252 void read_f(opnum_t, long);
253 void readlink_f(opnum_t, long);
254 void readv_f(opnum_t, long);
255 void removefattr_f(opnum_t, long);
256 void rename_f(opnum_t, long);
257 void rnoreplace_f(opnum_t, long);
258 void rexchange_f(opnum_t, long);
259 void rwhiteout_f(opnum_t, long);
260 void resvsp_f(opnum_t, long);
261 void rmdir_f(opnum_t, long);
262 void setattr_f(opnum_t, long);
263 void setfattr_f(opnum_t, long);
264 void setxattr_f(opnum_t, long);
265 void snapshot_f(opnum_t, long);
266 void splice_f(opnum_t, long);
267 void stat_f(opnum_t, long);
268 void subvol_create_f(opnum_t, long);
269 void subvol_delete_f(opnum_t, long);
270 void symlink_f(opnum_t, long);
271 void sync_f(opnum_t, long);
272 void truncate_f(opnum_t, long);
273 void unlink_f(opnum_t, long);
274 void unresvsp_f(opnum_t, long);
275 void uring_read_f(opnum_t, long);
276 void uring_write_f(opnum_t, long);
277 void write_f(opnum_t, long);
278 void writev_f(opnum_t, long);
279 char *xattr_flag_to_string(int);
282 /* { OP_ENUM, "name", function, freq, iswrite }, */
283 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
284 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
285 { OP_AREAD, "aread", aread_f, 1, 0 },
286 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
287 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
288 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
289 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
290 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
291 { OP_CHOWN, "chown", chown_f, 3, 1 },
292 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
293 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
294 { OP_CREAT, "creat", creat_f, 4, 1 },
295 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
296 { OP_DREAD, "dread", dread_f, 4, 0 },
297 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
298 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
299 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
300 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
301 { OP_FREESP, "freesp", freesp_f, 1, 1 },
302 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
303 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
304 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
305 /* get extended attribute */
306 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
307 { OP_LINK, "link", link_f, 1, 1 },
308 /* list extent attributes */
309 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
310 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
311 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
312 { OP_MREAD, "mread", mread_f, 2, 0 },
313 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
314 { OP_PUNCH, "punch", punch_f, 1, 1 },
315 { OP_ZERO, "zero", zero_f, 1, 1 },
316 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
317 { OP_INSERT, "insert", insert_f, 1, 1 },
318 { OP_READ, "read", read_f, 1, 0 },
319 { OP_READLINK, "readlink", readlink_f, 1, 0 },
320 { OP_READV, "readv", readv_f, 1, 0 },
321 /* remove (delete) extended attribute */
322 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
323 { OP_RENAME, "rename", rename_f, 2, 1 },
324 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
325 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
326 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
327 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
328 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
329 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
330 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
331 /* set extended attribute */
332 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
333 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
334 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
335 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
336 { OP_SPLICE, "splice", splice_f, 1, 1 },
337 { OP_STAT, "stat", stat_f, 1, 0 },
338 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
339 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
340 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
341 { OP_SYNC, "sync", sync_f, 1, 1 },
342 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
343 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
344 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
345 { OP_URING_READ, "uring_read", uring_read_f, 1, 0 },
346 { OP_URING_WRITE, "uring_write", uring_write_f, 1, 1 },
347 { OP_WRITE, "write", write_f, 4, 1 },
348 { OP_WRITEV, "writev", writev_f, 4, 1 },
351 flist_t flist[FT_nft] = {
365 struct xfs_fsop_geom geom;
375 opnum_t operations = 1;
376 unsigned int idmodulo = XFS_IDMODULO_MAX;
377 unsigned int attr_mask = ~0;
380 unsigned long seed = 0;
384 int verifiable_log = 0;
385 sig_atomic_t should_stop = 0;
386 sigjmp_buf *sigbus_jmp = NULL;
387 char *execute_cmd = NULL;
388 int execute_freq = 1;
389 struct print_string flag_str = {0};
391 void add_to_flist(int, int, int, int);
392 void append_pathname(pathname_t *, char *);
393 int attr_list_path(pathname_t *, char *, const int);
394 int attr_remove_path(pathname_t *, const char *);
395 int attr_set_path(pathname_t *, const char *, const char *, const int);
396 void check_cwd(void);
397 void cleanup_flist(void);
398 int creat_path(pathname_t *, mode_t);
399 void dcache_enter(int, int);
400 void dcache_init(void);
401 fent_t *dcache_lookup(int);
402 void dcache_purge(int, int);
403 void del_from_flist(int, int);
404 int dirid_to_name(char *, int);
406 int fent_to_name(pathname_t *, fent_t *);
407 bool fents_ancestor_check(fent_t *, fent_t *);
408 void fix_parent(int, int, bool);
409 void free_pathname(pathname_t *);
410 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
411 int generate_xattr_name(int, char *, int);
412 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
413 void init_pathname(pathname_t *);
414 int lchown_path(pathname_t *, uid_t, gid_t);
415 int link_path(pathname_t *, pathname_t *);
416 int lstat64_path(pathname_t *, struct stat64 *);
417 void make_freq_table(void);
418 int mkdir_path(pathname_t *, mode_t);
419 int mknod_path(pathname_t *, mode_t, dev_t);
420 void namerandpad(int, char *, int);
421 int open_file_or_dir(pathname_t *, int);
422 int open_path(pathname_t *, int);
423 DIR *opendir_path(pathname_t *);
424 void process_freq(char *);
425 int readlink_path(pathname_t *, char *, size_t);
426 int rename_path(pathname_t *, pathname_t *, int);
427 int rmdir_path(pathname_t *);
428 void separate_pathname(pathname_t *, char *, pathname_t *);
429 void show_ops(int, char *);
430 int stat64_path(pathname_t *, struct stat64 *);
431 int symlink_path(const char *, pathname_t *);
432 int truncate64_path(pathname_t *, off64_t);
433 int unlink_path(pathname_t *);
435 void write_freq(void);
436 void zero_freq(void);
437 void non_btrfs_freq(const char *);
439 void sg_handler(int signum)
447 * Only handle SIGBUS when mmap write to a hole and no
448 * block can be allocated due to ENOSPC, abort otherwise.
451 siglongjmp(*sigbus_jmp, -1);
453 printf("Unknown SIGBUS is caught, Abort!\n");
456 /* should not reach here */
463 int main(int argc, char **argv)
467 char *dirname = NULL;
468 char *logname = NULL;
469 char rpath[PATH_MAX];
478 xfs_error_injection_t err_inj;
479 struct sigaction action;
481 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
483 errrange = errtag = 0;
485 nops = sizeof(ops) / sizeof(ops[0]);
486 ops_end = &ops[nops];
488 while ((c = getopt(argc, argv, allopts)) != -1) {
497 sscanf(optarg, "%d", &errtag);
501 } else if (errtag == 0)
503 if (errtag >= XFS_ERRTAG_MAX) {
505 "error tag %d too large (max %d)\n",
506 errtag, XFS_ERRTAG_MAX - 1);
511 process_freq(optarg);
514 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
515 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
518 idmodulo = strtoul(optarg, NULL, 0);
519 if (idmodulo > XFS_IDMODULO_MAX) {
521 "chown modulo %d too big (max %d)\n",
522 idmodulo, XFS_IDMODULO_MAX);
527 loops = atoi(optarg);
531 operations = strtoll(optarg, NULL, 0);
542 nproc = atoi(optarg);
548 seed = strtoul(optarg, NULL, 0);
557 execute_cmd = optarg;
563 attr_mask = strtoul(optarg, NULL, 0);
567 if (optarg[0] == 'c')
578 execute_freq = strtoul(optarg, NULL, 0);
581 fprintf(stderr, "%s - invalid parameters\n",
591 /* no directory specified */
592 if (!nousage) usage();
596 non_btrfs_freq(dirname);
597 (void)mkdir(dirname, 0777);
598 if (logname && logname[0] != '/') {
599 if (!getcwd(rpath, sizeof(rpath))){
600 perror("getcwd failed");
606 if (chdir(dirname) < 0) {
611 char path[PATH_MAX + NAME_MAX + 1];
612 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
613 if (freopen(path, "a", stdout) == NULL) {
614 perror("freopen logfile failed");
618 sprintf(buf, "fss%x", (unsigned int)getpid());
619 fd = creat(buf, 0666);
620 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
621 maxfsize = (off64_t)MAXFSIZE32;
623 maxfsize = (off64_t)MAXFSIZE;
628 gettimeofday(&t, (void *)NULL);
629 seed = (int)t.tv_sec ^ (int)t.tv_usec;
630 printf("seed = %ld\n", seed);
632 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
633 if (i >= 0 && geom.rtblocks)
634 rtpct = MIN(MAX(geom.rtblocks * 100 /
635 (geom.rtblocks + geom.datablocks), 1), 99);
644 for (i = 0; i < j; i++)
647 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
653 for (i = 0; i < j; i++)
656 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
658 printf("Injecting failure on tag #%d\n", errtag);
659 err_inj.errtag = errtag;
661 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
663 perror("fsstress - XFS_SYSSGI error injection call");
672 action.sa_handler = sg_handler;
673 sigemptyset(&action.sa_mask);
675 if (sigaction(SIGTERM, &action, 0)) {
676 perror("sigaction failed");
680 for (i = 0; i < nproc; i++) {
682 sigemptyset(&action.sa_mask);
683 action.sa_handler = SIG_DFL;
684 if (sigaction(SIGTERM, &action, 0))
686 action.sa_handler = sg_handler;
687 if (sigaction(SIGBUS, &action, 0))
689 #ifdef HAVE_SYS_PRCTL_H
690 prctl(PR_SET_PDEATHSIG, SIGKILL);
691 if (getppid() == 1) /* parent died already? */
695 char path[PATH_MAX + NAME_MAX + 2 + 11];
696 snprintf(path, sizeof(path), "%s/%s.%d",
698 if (freopen(path, "a", stdout) == NULL) {
699 perror("freopen logfile failed");
705 if (io_setup(AIO_ENTRIES, &io_ctx) != 0) {
706 fprintf(stderr, "io_setup failed\n");
711 have_io_uring = true;
712 /* If ENOSYS, just ignore uring, other errors are fatal. */
713 if (io_uring_queue_init(URING_ENTRIES, &ring, 0)) {
714 if (errno == ENOSYS) {
715 have_io_uring = false;
717 fprintf(stderr, "io_uring_queue_init failed\n");
722 for (i = 0; !loops || (i < loops); i++)
725 if(io_destroy(io_ctx) != 0) {
726 fprintf(stderr, "io_destroy failed");
732 io_uring_queue_exit(&ring);
739 while (wait(&stat) > 0 && !should_stop) {
742 action.sa_flags = SA_RESTART;
743 sigaction(SIGTERM, &action, 0);
744 kill(-getpid(), SIGTERM);
745 while (wait(&stat) > 0)
751 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
753 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
755 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
768 add_string(struct print_string *str, const char *add)
770 int len = strlen(add);
775 if (len > (str->max - 1) - str->len) {
776 str->len = str->max - 1;
780 memcpy(str->buffer + str->len, add, len);
782 str->buffer[str->len] = '\0';
788 translate_flags(int flags, const char *delim,
789 const struct print_flags *flag_array)
791 int i, mask, first = 1;
794 if (!flag_str.buffer) {
795 flag_str.buffer = malloc(4096);
799 if (!flag_str.buffer)
802 flag_str.buffer[0] = '\0';
804 for (i = 0; flag_array[i].name && flags; i++) {
805 mask = flag_array[i].mask;
806 if ((flags & mask) != mask)
809 add = flag_array[i].name;
812 add_string(&flag_str, delim);
815 add_string(&flag_str, add);
818 /* Check whether there are any leftover flags. */
824 add_string(&flag_str, delim);
826 ret = snprintf(number, 11, "0x%x", flags) > 0;
827 if (ret > 0 && ret <= 11)
828 add_string(&flag_str, number);
831 return flag_str.buffer;
835 add_to_flist(int ft, int id, int parent, int xattr_counter)
841 if (ftp->nfiles == ftp->nslots) {
842 ftp->nslots += FLIST_SLOT_INCR;
843 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
845 fep = &ftp->fents[ftp->nfiles++];
848 fep->parent = parent;
849 fep->xattr_counter = xattr_counter;
853 append_pathname(pathname_t *name, char *str)
859 /* attempting to append to a dir a zero length path */
860 if (len && *str == '/' && name->len == 0) {
861 fprintf(stderr, "fsstress: append_pathname failure\n");
862 assert(chdir(homedir) == 0);
867 name->path = realloc(name->path, name->len + 1 + len);
868 strcpy(&name->path[name->len], str);
873 attr_list_count(char *buffer, int buffersize)
876 char *end = buffer + buffersize;
879 while (p < end && *p != 0) {
888 attr_list_path(pathname_t *name,
890 const int buffersize)
892 char buf[NAME_MAX + 1];
896 rval = llistxattr(name->path, buffer, buffersize);
897 if (rval >= 0 || errno != ENAMETOOLONG)
899 separate_pathname(name, buf, &newname);
900 if (chdir(buf) == 0) {
901 rval = attr_list_path(&newname, buffer, buffersize);
902 assert(chdir("..") == 0);
904 free_pathname(&newname);
909 attr_remove_path(pathname_t *name, const char *attrname)
911 char buf[NAME_MAX + 1];
915 rval = lremovexattr(name->path, attrname);
916 if (rval >= 0 || errno != ENAMETOOLONG)
918 separate_pathname(name, buf, &newname);
919 if (chdir(buf) == 0) {
920 rval = attr_remove_path(&newname, attrname);
921 assert(chdir("..") == 0);
923 free_pathname(&newname);
928 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
929 const int valuelength)
931 char buf[NAME_MAX + 1];
935 rval = lsetxattr(name->path, attrname, attrvalue, valuelength, 0);
936 if (rval >= 0 || errno != ENAMETOOLONG)
938 separate_pathname(name, buf, &newname);
939 if (chdir(buf) == 0) {
940 rval = attr_set_path(&newname, attrname, attrvalue,
942 assert(chdir("..") == 0);
944 free_pathname(&newname);
952 struct stat64 statbuf;
954 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
956 assert(chdir(homedir) == 0);
957 fprintf(stderr, "fsstress: check_cwd failure\n");
964 * go thru flist and release all entries
966 * NOTE: this function shouldn't be called until the end of a process
974 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
983 creat_path(pathname_t *name, mode_t mode)
985 char buf[NAME_MAX + 1];
989 rval = creat(name->path, mode);
990 if (rval >= 0 || errno != ENAMETOOLONG)
992 separate_pathname(name, buf, &newname);
993 if (chdir(buf) == 0) {
994 rval = creat_path(&newname, mode);
995 assert(chdir("..") == 0);
997 free_pathname(&newname);
1002 dcache_enter(int dirid, int slot)
1004 dcache[dirid % NDCACHE] = slot;
1012 for (i = 0; i < NDCACHE; i++)
1017 dcache_lookup(int dirid)
1022 i = dcache[dirid % NDCACHE];
1023 if (i >= 0 && i < flist[FT_DIR].nfiles &&
1024 (fep = &flist[FT_DIR].fents[i])->id == dirid)
1026 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
1027 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
1033 dcache_purge(int dirid, int ft)
1036 dcp = &dcache[dirid % NDCACHE];
1037 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1038 flist[ft].fents[*dcp].id == dirid)
1043 * Delete the item from the list by
1044 * moving last entry over the deleted one;
1045 * unless deleted entry is the last one.
1046 * Input: which file list array and which slot in array
1049 del_from_flist(int ft, int slot)
1054 if (ft == FT_DIR || ft == FT_SUBVOL)
1055 dcache_purge(ftp->fents[slot].id, ft);
1056 if (slot != ftp->nfiles - 1) {
1057 if (ft == FT_DIR || ft == FT_SUBVOL)
1058 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1059 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1065 delete_subvol_children(int parid)
1070 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1072 for (c = 0; c < flp->nfiles; c++) {
1073 if (flp->fents[c].parent == parid) {
1074 int id = flp->fents[c].id;
1075 del_from_flist(i, c);
1076 if (i == FT_DIR || i == FT_SUBVOL)
1077 delete_subvol_children(id);
1085 dirid_to_fent(int dirid)
1092 if ((fep = dcache_lookup(dirid)))
1096 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1097 if (fep->id == dirid) {
1098 dcache_enter(dirid, fep - flp->fents);
1112 struct stat64 statbuf;
1120 dividend = (operations + execute_freq) / (execute_freq + 1);
1121 sprintf(buf, "p%x", procid);
1122 (void)mkdir(buf, 0777);
1123 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1127 top_ino = statbuf.st_ino;
1128 homedir = getcwd(NULL, 0);
1130 perror("getcwd failed");
1136 namerand = random();
1137 for (opno = 0; opno < operations; opno++) {
1138 if (execute_cmd && opno && opno % dividend == 0) {
1140 printf("%lld: execute command %s\n", opno,
1142 rval = system(execute_cmd);
1144 fprintf(stderr, "execute command failed with "
1147 p = &ops[freq_table[random() % freq_table_size]];
1148 p->func(opno, random());
1150 * test for forced shutdown by stat'ing the test
1151 * directory. If this stat returns EIO, assume
1152 * the forced shutdown happened.
1154 if (errtag != 0 && opno % 100 == 0) {
1155 rval = stat64(".", &statbuf);
1157 fprintf(stderr, "Detected EIO\n");
1163 assert(chdir("..") == 0);
1168 sprintf(cmd, "rm -rf %s", buf);
1171 perror("cleaning up");
1177 * build up a pathname going thru the file entry and all
1178 * its parent entries
1179 * Return 0 on error, 1 on success;
1182 fent_to_name(pathname_t *name, fent_t *fep)
1184 flist_t *flp = &flist[fep->ft];
1185 char buf[NAME_MAX + 1];
1193 /* build up parent directory name */
1194 if (fep->parent != -1) {
1195 pfep = dirid_to_fent(fep->parent);
1198 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1199 procid, fep->id, fep->parent);
1204 e = fent_to_name(name, pfep);
1207 append_pathname(name, "/");
1210 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1211 namerandpad(fep->id, buf, i);
1212 append_pathname(name, buf);
1217 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1221 for (tmpfep = fep; tmpfep->parent != -1;
1222 tmpfep = dirid_to_fent(tmpfep->parent)) {
1223 if (tmpfep->parent == dfep->id)
1231 fix_parent(int oldid, int newid, bool swap)
1238 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1239 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1240 if (fep->parent == oldid)
1241 fep->parent = newid;
1242 else if (swap && fep->parent == newid)
1243 fep->parent = oldid;
1249 free_pathname(pathname_t *name)
1259 * Generate a filename of type ft.
1260 * If we have a fep which should be a directory then use it
1261 * as the parent path for this new filename i.e. prepend with it.
1264 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1266 char buf[NAME_MAX + 1];
1275 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1276 namerandpad(id, buf, len);
1278 /* prepend fep parent dir-name to it */
1280 e = fent_to_name(name, fep);
1283 append_pathname(name, "/");
1285 append_pathname(name, buf);
1289 for (j = 0; !*v && j < ilistlen; j++) {
1290 if (ilist[j] == id) {
1298 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1302 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1312 * Input: "which" to choose the file-types eg. non-directory
1313 * Input: "r" to choose which file
1314 * Output: file-list, file-entry, name for the chosen file.
1315 * Output: verbose if chosen file is on the ilist.
1318 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1321 int totalsum = 0; /* total number of matching files */
1322 int partialsum = 0; /* partial sum of matching files */
1328 int e = 1; /* success */
1331 * go thru flist and add up number of files for each
1332 * category that matches with <which>.
1334 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1335 if (which & (1 << i))
1336 totalsum += flp->nfiles;
1338 if (totalsum == 0) {
1348 * Now we have possible matches between 0..totalsum-1.
1349 * And we use r to help us choose which one we want,
1350 * which when bounded by totalsum becomes x.
1352 x = (int)(r % totalsum);
1353 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1354 if (which & (1 << i)) {
1355 if (x < partialsum + flp->nfiles) {
1357 /* found the matching file entry */
1358 fep = &flp->fents[x - partialsum];
1360 /* fill-in what we were asked for */
1362 e = fent_to_name(name, fep);
1365 fprintf(stderr, "%d: failed to get path for entry:"
1366 " id=%d,parent=%d\n",
1367 procid, fep->id, fep->parent);
1376 /* turn on verbose if its an ilisted file */
1378 for (j = 0; !*v && j < ilistlen; j++) {
1379 if (ilist[j] == fep->id) {
1386 partialsum += flp->nfiles;
1390 fprintf(stderr, "fsstress: get_fname failure\n");
1397 init_pathname(pathname_t *name)
1404 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1406 char buf[NAME_MAX + 1];
1410 rval = lchown(name->path, owner, group);
1411 if (rval >= 0 || errno != ENAMETOOLONG)
1413 separate_pathname(name, buf, &newname);
1414 if (chdir(buf) == 0) {
1415 rval = lchown_path(&newname, owner, group);
1416 assert(chdir("..") == 0);
1418 free_pathname(&newname);
1423 link_path(pathname_t *name1, pathname_t *name2)
1425 char buf1[NAME_MAX + 1];
1426 char buf2[NAME_MAX + 1];
1428 pathname_t newname1;
1429 pathname_t newname2;
1432 rval = link(name1->path, name2->path);
1433 if (rval >= 0 || errno != ENAMETOOLONG)
1435 separate_pathname(name1, buf1, &newname1);
1436 separate_pathname(name2, buf2, &newname2);
1437 if (strcmp(buf1, buf2) == 0) {
1438 if (chdir(buf1) == 0) {
1439 rval = link_path(&newname1, &newname2);
1440 assert(chdir("..") == 0);
1443 if (strcmp(buf1, "..") == 0)
1445 else if (strcmp(buf2, "..") == 0)
1447 else if (strlen(buf1) == 0)
1449 else if (strlen(buf2) == 0)
1452 down1 = MAX(newname1.len, 3 + name2->len) <=
1453 MAX(3 + name1->len, newname2.len);
1455 free_pathname(&newname2);
1456 append_pathname(&newname2, "../");
1457 append_pathname(&newname2, name2->path);
1458 if (chdir(buf1) == 0) {
1459 rval = link_path(&newname1, &newname2);
1460 assert(chdir("..") == 0);
1463 free_pathname(&newname1);
1464 append_pathname(&newname1, "../");
1465 append_pathname(&newname1, name1->path);
1466 if (chdir(buf2) == 0) {
1467 rval = link_path(&newname1, &newname2);
1468 assert(chdir("..") == 0);
1472 free_pathname(&newname1);
1473 free_pathname(&newname2);
1478 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1480 char buf[NAME_MAX + 1];
1484 rval = lstat64(name->path, sbuf);
1485 if (rval >= 0 || errno != ENAMETOOLONG)
1487 separate_pathname(name, buf, &newname);
1488 if (chdir(buf) == 0) {
1489 rval = lstat64_path(&newname, sbuf);
1490 assert(chdir("..") == 0);
1492 free_pathname(&newname);
1497 make_freq_table(void)
1503 for (p = ops, f = 0; p < ops_end; p++)
1505 freq_table = malloc(f * sizeof(*freq_table));
1506 freq_table_size = f;
1507 for (p = ops, i = 0; p < ops_end; p++) {
1508 for (f = 0; f < p->freq; f++, i++)
1509 freq_table[i] = p->op;
1514 mkdir_path(pathname_t *name, mode_t mode)
1516 char buf[NAME_MAX + 1];
1520 rval = mkdir(name->path, mode);
1521 if (rval >= 0 || errno != ENAMETOOLONG)
1523 separate_pathname(name, buf, &newname);
1524 if (chdir(buf) == 0) {
1525 rval = mkdir_path(&newname, mode);
1526 assert(chdir("..") == 0);
1528 free_pathname(&newname);
1533 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1535 char buf[NAME_MAX + 1];
1539 rval = mknod(name->path, mode, dev);
1540 if (rval >= 0 || errno != ENAMETOOLONG)
1542 separate_pathname(name, buf, &newname);
1543 if (chdir(buf) == 0) {
1544 rval = mknod_path(&newname, mode, dev);
1545 assert(chdir("..") == 0);
1547 free_pathname(&newname);
1552 namerandpad(int id, char *buf, int i)
1555 static int buckets[] =
1556 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1562 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1563 padmod = buckets[bucket] + 1 - i;
1566 padlen = (id ^ namerand) % padmod;
1568 memset(&buf[i], 'X', padlen);
1569 buf[i + padlen] = '\0';
1574 open_file_or_dir(pathname_t *name, int flags)
1578 fd = open_path(name, flags);
1581 if (fd == -1 && errno != EISDIR)
1583 /* Directories can not be opened in write mode nor direct mode. */
1584 flags &= ~(O_WRONLY | O_DIRECT);
1585 flags |= O_RDONLY | O_DIRECTORY;
1586 return open_path(name, flags);
1590 open_path(pathname_t *name, int oflag)
1592 char buf[NAME_MAX + 1];
1596 rval = open(name->path, oflag);
1597 if (rval >= 0 || errno != ENAMETOOLONG)
1599 separate_pathname(name, buf, &newname);
1600 if (chdir(buf) == 0) {
1601 rval = open_path(&newname, oflag);
1602 assert(chdir("..") == 0);
1604 free_pathname(&newname);
1609 opendir_path(pathname_t *name)
1611 char buf[NAME_MAX + 1];
1615 rval = opendir(name->path);
1616 if (rval || errno != ENAMETOOLONG)
1618 separate_pathname(name, buf, &newname);
1619 if (chdir(buf) == 0) {
1620 rval = opendir_path(&newname);
1621 assert(chdir("..") == 0);
1623 free_pathname(&newname);
1628 process_freq(char *arg)
1633 s = strchr(arg, '=');
1635 fprintf(stderr, "bad argument '%s'\n", arg);
1639 for (p = ops; p < ops_end; p++) {
1640 if (strcmp(arg, p->name) == 0) {
1645 fprintf(stderr, "can't find op type %s for -f\n", arg);
1650 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1652 char buf[NAME_MAX + 1];
1656 rval = readlink(name->path, lbuf, lbufsiz);
1657 if (rval >= 0 || errno != ENAMETOOLONG)
1659 separate_pathname(name, buf, &newname);
1660 if (chdir(buf) == 0) {
1661 rval = readlink_path(&newname, lbuf, lbufsiz);
1662 assert(chdir("..") == 0);
1664 free_pathname(&newname);
1669 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1671 char buf1[NAME_MAX + 1];
1672 char buf2[NAME_MAX + 1];
1674 pathname_t newname1;
1675 pathname_t newname2;
1679 rval = rename(name1->path, name2->path);
1681 rval = renameat2(AT_FDCWD, name1->path,
1682 AT_FDCWD, name2->path, mode);
1683 if (rval >= 0 || errno != ENAMETOOLONG)
1685 separate_pathname(name1, buf1, &newname1);
1686 separate_pathname(name2, buf2, &newname2);
1687 if (strcmp(buf1, buf2) == 0) {
1688 if (chdir(buf1) == 0) {
1689 rval = rename_path(&newname1, &newname2, mode);
1690 assert(chdir("..") == 0);
1693 if (strcmp(buf1, "..") == 0)
1695 else if (strcmp(buf2, "..") == 0)
1697 else if (strlen(buf1) == 0)
1699 else if (strlen(buf2) == 0)
1702 down1 = MAX(newname1.len, 3 + name2->len) <=
1703 MAX(3 + name1->len, newname2.len);
1705 free_pathname(&newname2);
1706 append_pathname(&newname2, "../");
1707 append_pathname(&newname2, name2->path);
1708 if (chdir(buf1) == 0) {
1709 rval = rename_path(&newname1, &newname2, mode);
1710 assert(chdir("..") == 0);
1713 free_pathname(&newname1);
1714 append_pathname(&newname1, "../");
1715 append_pathname(&newname1, name1->path);
1716 if (chdir(buf2) == 0) {
1717 rval = rename_path(&newname1, &newname2, mode);
1718 assert(chdir("..") == 0);
1722 free_pathname(&newname1);
1723 free_pathname(&newname2);
1728 rmdir_path(pathname_t *name)
1730 char buf[NAME_MAX + 1];
1734 rval = rmdir(name->path);
1735 if (rval >= 0 || errno != ENAMETOOLONG)
1737 separate_pathname(name, buf, &newname);
1738 if (chdir(buf) == 0) {
1739 rval = rmdir_path(&newname);
1740 assert(chdir("..") == 0);
1742 free_pathname(&newname);
1747 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1751 init_pathname(newname);
1752 slash = strchr(name->path, '/');
1753 if (slash == NULL) {
1758 strcpy(buf, name->path);
1760 append_pathname(newname, slash + 1);
1766 show_ops(int flag, char *lead_str)
1771 /* print in list form */
1774 for (p = ops; p < ops_end; p++) {
1775 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1776 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1777 x+=printf("%s ", p->name);
1780 } else if (flag == 0) {
1781 /* Table view style */
1783 for (f = 0, p = ops; p < ops_end; p++)
1789 for (p = ops; p < ops_end; p++) {
1790 if (flag != 0 || p->freq > 0) {
1791 if (lead_str != NULL)
1792 printf("%s", lead_str);
1793 printf("%20s %d/%d %s\n",
1794 p->name, p->freq, f,
1795 (p->iswrite == 0) ? " " : "write op");
1799 /* Command line style */
1800 if (lead_str != NULL)
1801 printf("%s", lead_str);
1802 printf ("-z -s %ld -m %d -n %lld -p %d \\\n", seed, idmodulo,
1804 for (p = ops; p < ops_end; p++)
1806 printf("-f %s=%d \\\n",p->name, p->freq);
1811 stat64_path(pathname_t *name, struct stat64 *sbuf)
1813 char buf[NAME_MAX + 1];
1817 rval = stat64(name->path, sbuf);
1818 if (rval >= 0 || errno != ENAMETOOLONG)
1820 separate_pathname(name, buf, &newname);
1821 if (chdir(buf) == 0) {
1822 rval = stat64_path(&newname, sbuf);
1823 assert(chdir("..") == 0);
1825 free_pathname(&newname);
1830 symlink_path(const char *name1, pathname_t *name)
1832 char buf[NAME_MAX + 1];
1836 if (!strcmp(name1, name->path)) {
1837 printf("yikes! %s %s\n", name1, name->path);
1841 rval = symlink(name1, name->path);
1842 if (rval >= 0 || errno != ENAMETOOLONG)
1844 separate_pathname(name, buf, &newname);
1845 if (chdir(buf) == 0) {
1846 rval = symlink_path(name1, &newname);
1847 assert(chdir("..") == 0);
1849 free_pathname(&newname);
1854 truncate64_path(pathname_t *name, off64_t length)
1856 char buf[NAME_MAX + 1];
1860 rval = truncate64(name->path, length);
1861 if (rval >= 0 || errno != ENAMETOOLONG)
1863 separate_pathname(name, buf, &newname);
1864 if (chdir(buf) == 0) {
1865 rval = truncate64_path(&newname, length);
1866 assert(chdir("..") == 0);
1868 free_pathname(&newname);
1873 unlink_path(pathname_t *name)
1875 char buf[NAME_MAX + 1];
1879 rval = unlink(name->path);
1880 if (rval >= 0 || errno != ENAMETOOLONG)
1882 separate_pathname(name, buf, &newname);
1883 if (chdir(buf) == 0) {
1884 rval = unlink_path(&newname);
1885 assert(chdir("..") == 0);
1887 free_pathname(&newname);
1894 printf("Usage: %s -H or\n", myprog);
1895 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1897 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1899 printf(" -c clean up the test directory after each run\n");
1900 printf(" -d dir specifies the base directory for operations\n");
1901 printf(" -e errtg specifies error injection stuff\n");
1902 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1903 printf(" the valid operation names are:\n");
1905 printf(" -i filenum get verbose output for this nth file object\n");
1906 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1907 printf(" *use 0 for infinite (default 1)\n");
1908 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1909 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1910 printf(" -o logfile specifies logfile name\n");
1911 printf(" -p nproc specifies the no. of processes (default 1)\n");
1912 printf(" -r specifies random name padding\n");
1913 printf(" -s seed specifies the seed for the random generator (default random)\n");
1914 printf(" -v specifies verbose mode\n");
1915 printf(" -w zeros frequencies of non-write operations\n");
1916 printf(" -x cmd execute command in the middle of operations\n");
1917 printf(" -z zeros frequencies of all operations\n");
1918 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1919 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1920 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1921 printf(" -H prints usage and exits\n");
1929 for (p = ops; p < ops_end; p++) {
1940 for (p = ops; p < ops_end; p++)
1944 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1946 opty_t btrfs_ops[] = {
1953 non_btrfs_freq(const char *path)
1956 #ifdef HAVE_BTRFSUTIL_H
1957 enum btrfs_util_error e;
1959 e = btrfs_util_is_subvolume(path);
1960 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1963 for (p = ops; p < ops_end; p++) {
1965 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1966 if (p->op == btrfs_ops[i]) {
1974 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1977 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1978 verifiable_log ? -1: (long)s->st_ino,
1979 (long)s->st_nlink, s->st_uid, s->st_gid,
1980 (long long) s->st_blocks, (long long) s->st_size);
1984 afsync_f(opnum_t opno, long r)
1992 struct iocb *iocbs[] = { &iocb };
1993 struct io_event event;
1996 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1998 printf("%d/%lld: afsync - no filename\n", procid, opno);
2002 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
2003 e = fd < 0 ? errno : 0;
2007 printf("%d/%lld: afsync - open %s failed %d\n",
2008 procid, opno, f.path, e);
2013 io_prep_fsync(&iocb, fd);
2014 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2016 printf("%d/%lld: afsync - io_submit %s %d\n",
2017 procid, opno, f.path, e);
2022 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2024 printf("%d/%lld: afsync - io_getevents failed %d\n",
2033 printf("%d/%lld: afsync %s %d\n", procid, opno, f.path, e);
2040 allocsp_f(opnum_t opno, long r)
2045 struct xfs_flock64 fl;
2053 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2055 printf("%d/%lld: allocsp - no filename\n", procid, opno);
2059 fd = open_path(&f, O_RDWR);
2060 e = fd < 0 ? errno : 0;
2064 printf("%d/%lld: allocsp - open %s failed %d\n",
2065 procid, opno, f.path, e);
2069 if (fstat64(fd, &stb) < 0) {
2071 printf("%d/%lld: allocsp - fstat64 %s failed %d\n",
2072 procid, opno, f.path, errno);
2077 inode_info(st, sizeof(st), &stb, v);
2078 lr = ((int64_t)random() << 32) + random();
2079 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2081 fl.l_whence = SEEK_SET;
2084 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2086 printf("%d/%lld: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2087 procid, opno, f.path, st, (long long)off, e);
2095 do_aio_rw(opnum_t opno, long r, int flags)
2099 struct dioattr diob;
2111 struct io_event event;
2112 struct iocb *iocbs[] = { &iocb };
2113 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2116 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2118 printf("%d/%lld: do_aio_rw - no filename\n", procid, opno);
2121 fd = open_path(&f, flags|O_DIRECT);
2122 e = fd < 0 ? errno : 0;
2126 printf("%d/%lld: do_aio_rw - open %s failed %d\n",
2127 procid, opno, f.path, e);
2130 if (fstat64(fd, &stb) < 0) {
2132 printf("%d/%lld: do_aio_rw - fstat64 %s failed %d\n",
2133 procid, opno, f.path, errno);
2136 inode_info(st, sizeof(st), &stb, v);
2137 if (!iswrite && stb.st_size == 0) {
2139 printf("%d/%lld: do_aio_rw - %s%s zero size\n", procid, opno,
2143 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2146 "%d/%lld: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2147 " fallback to stat()\n",
2148 procid, opno, f.path, st, errno);
2149 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2150 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
2152 dio_env = getenv("XFS_DIO_MIN");
2154 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2155 align = (int64_t)diob.d_miniosz;
2156 lr = ((int64_t)random() << 32) + random();
2157 len = (random() % FILELEN_MAX) + 1;
2158 len -= (len % align);
2161 else if (len > diob.d_maxiosz)
2162 len = diob.d_maxiosz;
2163 buf = memalign(diob.d_mem, len);
2166 printf("%d/%lld: do_aio_rw - memalign failed\n",
2172 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2173 off -= (off % align);
2175 memset(buf, nameseq & 0xff, len);
2176 io_prep_pwrite(&iocb, fd, buf, len, off);
2178 off = (off64_t)(lr % stb.st_size);
2179 off -= (off % align);
2180 io_prep_pread(&iocb, fd, buf, len, off);
2182 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2184 printf("%d/%lld: %s - io_submit failed %d\n",
2185 procid, opno, iswrite ? "awrite" : "aread", e);
2188 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2190 printf("%d/%lld: %s - io_getevents failed %d\n",
2191 procid, opno, iswrite ? "awrite" : "aread", e);
2195 e = event.res != len ? event.res2 : 0;
2197 printf("%d/%lld: %s %s%s [%lld,%d] %d\n",
2198 procid, opno, iswrite ? "awrite" : "aread",
2199 f.path, st, (long long)off, (int)len, e);
2211 do_uring_rw(opnum_t opno, long r, int flags)
2223 struct io_uring_sqe *sqe;
2224 struct io_uring_cqe *cqe;
2226 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2232 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2234 printf("%d/%lld: do_uring_rw - no filename\n", procid, opno);
2237 fd = open_path(&f, flags);
2238 e = fd < 0 ? errno : 0;
2242 printf("%d/%lld: do_uring_rw - open %s failed %d\n",
2243 procid, opno, f.path, e);
2246 if (fstat64(fd, &stb) < 0) {
2248 printf("%d/%lld: do_uring_rw - fstat64 %s failed %d\n",
2249 procid, opno, f.path, errno);
2252 inode_info(st, sizeof(st), &stb, v);
2253 if (!iswrite && stb.st_size == 0) {
2255 printf("%d/%lld: do_uring_rw - %s%s zero size\n", procid, opno,
2259 sqe = io_uring_get_sqe(&ring);
2262 printf("%d/%lld: do_uring_rw - io_uring_get_sqe failed\n",
2266 lr = ((int64_t)random() << 32) + random();
2267 len = (random() % FILELEN_MAX) + 1;
2271 printf("%d/%lld: do_uring_rw - malloc failed\n",
2275 iovec.iov_base = buf;
2276 iovec.iov_len = len;
2278 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2280 memset(buf, nameseq & 0xff, len);
2281 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2283 off = (off64_t)(lr % stb.st_size);
2284 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2287 if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2289 printf("%d/%lld: %s - io_uring_submit failed %d\n", procid, opno,
2290 iswrite ? "uring_write" : "uring_read", e);
2293 if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2295 printf("%d/%lld: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2296 iswrite ? "uring_write" : "uring_read", e);
2300 printf("%d/%lld: %s %s%s [%lld, %d(res=%d)] %d\n",
2301 procid, opno, iswrite ? "uring_write" : "uring_read",
2302 f.path, st, (long long)off, (int)len, cqe->res, e);
2303 io_uring_cqe_seen(&ring, cqe);
2315 aread_f(opnum_t opno, long r)
2318 do_aio_rw(opno, r, O_RDONLY);
2323 attr_remove_f(opnum_t opno, long r)
2328 char buf[XATTR_LIST_MAX];
2337 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2338 append_pathname(&f, ".");
2339 e = attr_list_path(&f, buf, sizeof(buf));
2342 total = attr_list_count(buf, e);
2345 printf("%d/%lld: attr_remove - no attrs for %s\n",
2346 procid, opno, f.path);
2351 which = (int)(random() % total);
2355 while (bufname < bufend) {
2361 bufname += strlen(bufname) + 1;
2363 if (aname == NULL) {
2366 "%d/%lld: attr_remove - name %d not found at %s\n",
2367 procid, opno, which, f.path);
2371 if (attr_remove_path(&f, aname) < 0)
2377 printf("%d/%lld: attr_remove %s %s %d\n",
2378 procid, opno, f.path, aname, e);
2383 attr_set_f(opnum_t opno, long r)
2390 static int lengths[] = { 10, 100, 1000, 10000 };
2395 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2396 append_pathname(&f, ".");
2397 sprintf(aname, "a%x", nameseq++);
2398 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2399 len = (int)(random() % lengths[li]);
2403 memset(aval, nameseq & 0xff, len);
2404 if (attr_set_path(&f, aname, aval, len) < 0)
2411 printf("%d/%lld: attr_set %s %s %d\n", procid, opno, f.path,
2417 awrite_f(opnum_t opno, long r)
2420 do_aio_rw(opno, r, O_WRONLY);
2425 bulkstat_f(opnum_t opno, long r)
2431 struct xfs_bstat *t;
2433 struct xfs_fsop_bulkreq bsr;
2436 nent = (r % 999) + 2;
2437 t = malloc(nent * sizeof(*t));
2438 fd = open(".", O_RDONLY);
2446 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2450 printf("%d/%lld: bulkstat nent %d total %lld\n",
2451 procid, opno, nent, (long long)total);
2456 bulkstat1_f(opnum_t opno, long r)
2466 struct xfs_fsop_bulkreq bsr;
2469 good = random() & 1;
2471 /* use an inode we know exists */
2473 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2474 append_pathname(&f, ".");
2475 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2480 * pick a random inode
2482 * note this can generate kernel warning messages
2483 * since bulkstat_one will read the disk block that
2484 * would contain a given inode even if that disk
2485 * block doesn't contain inodes.
2487 * this is detected later, but not until after the
2488 * warning is displayed.
2490 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2496 fd = open(".", O_RDONLY);
2502 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2504 printf("%d/%lld: bulkstat1 %s ino %lld %d\n",
2505 procid, opno, good?"real":"random",
2506 verifiable_log ? -1LL : (long long)ino, e);
2511 chown_f(opnum_t opno, long r)
2521 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2522 append_pathname(&f, ".");
2523 u = (uid_t)random();
2524 g = (gid_t)random();
2525 nbits = (int)(random() % idmodulo);
2526 u &= (1 << nbits) - 1;
2527 g &= (1 << nbits) - 1;
2528 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2531 printf("%d/%lld: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2535 /* reflink some arbitrary range of f1 to f2. */
2542 struct file_clone_range fcr;
2543 struct pathname fpath1;
2544 struct pathname fpath2;
2545 struct stat64 stat1;
2546 struct stat64 stat2;
2547 char inoinfo1[1024];
2548 char inoinfo2[1024];
2562 init_pathname(&fpath1);
2563 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2565 printf("%d/%lld: clonerange read - no filename\n",
2570 init_pathname(&fpath2);
2571 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2573 printf("%d/%lld: clonerange write - no filename\n",
2579 fd1 = open_path(&fpath1, O_RDONLY);
2580 e = fd1 < 0 ? errno : 0;
2584 printf("%d/%lld: clonerange read - open %s failed %d\n",
2585 procid, opno, fpath1.path, e);
2589 fd2 = open_path(&fpath2, O_WRONLY);
2590 e = fd2 < 0 ? errno : 0;
2594 printf("%d/%lld: clonerange write - open %s failed %d\n",
2595 procid, opno, fpath2.path, e);
2599 /* Get file stats */
2600 if (fstat64(fd1, &stat1) < 0) {
2602 printf("%d/%lld: clonerange read - fstat64 %s failed %d\n",
2603 procid, opno, fpath1.path, errno);
2606 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2608 if (fstat64(fd2, &stat2) < 0) {
2610 printf("%d/%lld: clonerange write - fstat64 %s failed %d\n",
2611 procid, opno, fpath2.path, errno);
2614 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2616 /* Calculate offsets */
2617 len = (random() % FILELEN_MAX) + 1;
2618 len = rounddown_64(len, stat1.st_blksize);
2620 len = stat1.st_blksize;
2621 if (len > stat1.st_size)
2622 len = stat1.st_size;
2624 lr = ((int64_t)random() << 32) + random();
2625 if (stat1.st_size == len)
2628 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2630 off1 = rounddown_64(off1, stat1.st_blksize);
2633 * If srcfile == destfile, randomly generate destination ranges
2634 * until we find one that doesn't overlap the source range.
2636 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2638 lr = ((int64_t)random() << 32) + random();
2639 off2 = (off64_t)(lr % max_off2);
2641 off2 = rounddown_64(off2, stat2.st_blksize);
2642 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2644 /* Clone data blocks */
2646 fcr.src_offset = off1;
2647 fcr.src_length = len;
2648 fcr.dest_offset = off2;
2650 ret = ioctl(fd2, FICLONERANGE, &fcr);
2651 e = ret < 0 ? errno : 0;
2653 printf("%d/%lld: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2655 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2656 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2659 printf(" error %d", e);
2668 free_pathname(&fpath2);
2670 free_pathname(&fpath1);
2674 /* copy some arbitrary range of f1 to f2. */
2680 #ifdef HAVE_COPY_FILE_RANGE
2681 struct pathname fpath1;
2682 struct pathname fpath2;
2683 struct stat64 stat1;
2684 struct stat64 stat2;
2685 char inoinfo1[1024];
2686 char inoinfo2[1024];
2704 init_pathname(&fpath1);
2705 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2707 printf("%d/%lld: copyrange read - no filename\n",
2712 init_pathname(&fpath2);
2713 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2715 printf("%d/%lld: copyrange write - no filename\n",
2721 fd1 = open_path(&fpath1, O_RDONLY);
2722 e = fd1 < 0 ? errno : 0;
2726 printf("%d/%lld: copyrange read - open %s failed %d\n",
2727 procid, opno, fpath1.path, e);
2731 fd2 = open_path(&fpath2, O_WRONLY);
2732 e = fd2 < 0 ? errno : 0;
2736 printf("%d/%lld: copyrange write - open %s failed %d\n",
2737 procid, opno, fpath2.path, e);
2741 /* Get file stats */
2742 if (fstat64(fd1, &stat1) < 0) {
2744 printf("%d/%lld: copyrange read - fstat64 %s failed %d\n",
2745 procid, opno, fpath1.path, errno);
2748 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2750 if (fstat64(fd2, &stat2) < 0) {
2752 printf("%d/%lld: copyrange write - fstat64 %s failed %d\n",
2753 procid, opno, fpath2.path, errno);
2756 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2758 /* Calculate offsets */
2759 len = (random() % FILELEN_MAX) + 1;
2761 len = stat1.st_blksize;
2762 if (len > stat1.st_size)
2763 len = stat1.st_size;
2765 lr = ((int64_t)random() << 32) + random();
2766 if (stat1.st_size == len)
2769 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2773 * If srcfile == destfile, randomly generate destination ranges
2774 * until we find one that doesn't overlap the source range.
2776 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2778 lr = ((int64_t)random() << 32) + random();
2779 off2 = (off64_t)(lr % max_off2);
2781 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2784 * Since len, off1 and off2 will be changed later, preserve their
2792 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2795 if (errno != EAGAIN || tries++ >= 300)
2797 } else if (ret > len || ret == 0)
2802 e = ret < 0 ? errno : 0;
2804 printf("%d/%lld: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2806 fpath1.path, inoinfo1,
2807 (long long)offset1, (long long)length,
2808 fpath2.path, inoinfo2,
2809 (long long)offset2, (long long)length);
2812 printf(" error %d", e);
2813 else if (len && ret > len)
2814 printf(" asked for %lld, copied %lld??\n",
2815 (long long)len, (long long)ret);
2824 free_pathname(&fpath2);
2826 free_pathname(&fpath1);
2830 /* dedupe some arbitrary range of f1 to f2...fn. */
2836 #ifdef FIDEDUPERANGE
2837 #define INFO_SZ 1024
2838 struct file_dedupe_range *fdr;
2839 struct pathname *fpath;
2840 struct stat64 *stat;
2852 if (flist[FT_REG].nfiles < 2)
2855 /* Pick somewhere between 2 and 128 files. */
2857 nr = random() % (flist[FT_REG].nfiles + 1);
2858 } while (nr < 2 || nr > 128);
2861 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2862 sizeof(struct file_dedupe_range));
2864 printf("%d/%lld: line %d error %d\n",
2865 procid, opno, __LINE__, errno);
2868 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2869 sizeof(struct file_dedupe_range)));
2871 fpath = calloc(nr, sizeof(struct pathname));
2873 printf("%d/%lld: line %d error %d\n",
2874 procid, opno, __LINE__, errno);
2878 stat = calloc(nr, sizeof(struct stat64));
2880 printf("%d/%lld: line %d error %d\n",
2881 procid, opno, __LINE__, errno);
2885 info = calloc(nr, INFO_SZ);
2887 printf("%d/%lld: line %d error %d\n",
2888 procid, opno, __LINE__, errno);
2892 off = calloc(nr, sizeof(off64_t));
2894 printf("%d/%lld: line %d error %d\n",
2895 procid, opno, __LINE__, errno);
2899 v = calloc(nr, sizeof(int));
2901 printf("%d/%lld: line %d error %d\n",
2902 procid, opno, __LINE__, errno);
2905 fd = calloc(nr, sizeof(int));
2907 printf("%d/%lld: line %d error %d\n",
2908 procid, opno, __LINE__, errno);
2911 memset(fd, 0xFF, nr * sizeof(int));
2913 /* Get paths for all files */
2914 for (i = 0; i < nr; i++)
2915 init_pathname(&fpath[i]);
2917 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2919 printf("%d/%lld: deduperange read - no filename\n",
2924 for (i = 1; i < nr; i++) {
2925 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2927 printf("%d/%lld: deduperange write - no filename\n",
2934 fd[0] = open_path(&fpath[0], O_RDONLY);
2935 e = fd[0] < 0 ? errno : 0;
2939 printf("%d/%lld: deduperange read - open %s failed %d\n",
2940 procid, opno, fpath[0].path, e);
2944 for (i = 1; i < nr; i++) {
2945 fd[i] = open_path(&fpath[i], O_WRONLY);
2946 e = fd[i] < 0 ? errno : 0;
2950 printf("%d/%lld: deduperange write - open %s failed %d\n",
2951 procid, opno, fpath[i].path, e);
2956 /* Get file stats */
2957 if (fstat64(fd[0], &stat[0]) < 0) {
2959 printf("%d/%lld: deduperange read - fstat64 %s failed %d\n",
2960 procid, opno, fpath[0].path, errno);
2964 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2966 for (i = 1; i < nr; i++) {
2967 if (fstat64(fd[i], &stat[i]) < 0) {
2969 printf("%d/%lld: deduperange write - fstat64 %s failed %d\n",
2970 procid, opno, fpath[i].path, errno);
2973 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2976 /* Never try to dedupe more than half of the src file. */
2977 len = (random() % FILELEN_MAX) + 1;
2978 len = rounddown_64(len, stat[0].st_blksize);
2980 len = stat[0].st_blksize / 2;
2981 if (len > stat[0].st_size / 2)
2982 len = stat[0].st_size / 2;
2984 /* Calculate offsets */
2985 lr = ((int64_t)random() << 32) + random();
2986 if (stat[0].st_size == len)
2989 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2991 off[0] = rounddown_64(off[0], stat[0].st_blksize);
2994 * If srcfile == destfile[i], randomly generate destination ranges
2995 * until we find one that doesn't overlap the source range.
2997 for (i = 1; i < nr; i++) {
3001 lr = ((int64_t)random() << 32) + random();
3002 if (stat[i].st_size <= len)
3005 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
3007 off[i] = rounddown_64(off[i], stat[i].st_blksize);
3008 } while (stat[0].st_ino == stat[i].st_ino &&
3009 llabs(off[i] - off[0]) < len &&
3013 /* Clone data blocks */
3014 fdr->src_offset = off[0];
3015 fdr->src_length = len;
3016 fdr->dest_count = nr - 1;
3017 for (i = 1; i < nr; i++) {
3018 fdr->info[i - 1].dest_fd = fd[i];
3019 fdr->info[i - 1].dest_offset = off[i];
3022 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3023 e = ret < 0 ? errno : 0;
3025 printf("%d/%lld: deduperange from %s%s [%lld,%lld]",
3027 fpath[0].path, &info[0], (long long)off[0],
3030 printf(" error %d", e);
3036 for (i = 1; i < nr; i++) {
3037 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3039 printf("%d/%lld: ...to %s%s [%lld,%lld]",
3041 fpath[i].path, &info[i * INFO_SZ],
3042 (long long)off[i], (long long)len);
3043 if (fdr->info[i - 1].status < 0)
3044 printf(" error %d", e);
3045 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3046 printf(" %llu bytes deduplicated",
3047 fdr->info[i - 1].bytes_deduped);
3048 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3049 printf(" differed");
3055 for (i = 0; i < nr; i++)
3059 for (i = 0; i < nr; i++)
3060 free_pathname(&fpath[i]);
3079 setxattr_f(opnum_t opno, long r)
3081 #ifdef XFS_XFLAG_EXTSIZE
3091 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3092 append_pathname(&f, ".");
3093 fd = open_path(&f, O_RDWR);
3094 e = fd < 0 ? errno : 0;
3099 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3100 nbits = (int)(random() % e);
3101 p &= (1 << nbits) - 1;
3103 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3105 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3108 printf("%d/%lld: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3115 splice_f(opnum_t opno, long r)
3117 struct pathname fpath1;
3118 struct pathname fpath2;
3119 struct stat64 stat1;
3120 struct stat64 stat2;
3121 char inoinfo1[1024];
3122 char inoinfo2[1024];
3126 loff_t offset1, offset2;
3133 ssize_t ret1 = 0, ret2 = 0;
3139 init_pathname(&fpath1);
3140 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3142 printf("%d/%lld: splice read - no filename\n",
3147 init_pathname(&fpath2);
3148 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3150 printf("%d/%lld: splice write - no filename\n",
3156 fd1 = open_path(&fpath1, O_RDONLY);
3157 e = fd1 < 0 ? errno : 0;
3161 printf("%d/%lld: splice read - open %s failed %d\n",
3162 procid, opno, fpath1.path, e);
3166 fd2 = open_path(&fpath2, O_WRONLY);
3167 e = fd2 < 0 ? errno : 0;
3171 printf("%d/%lld: splice write - open %s failed %d\n",
3172 procid, opno, fpath2.path, e);
3176 /* Get file stats */
3177 if (fstat64(fd1, &stat1) < 0) {
3179 printf("%d/%lld: splice read - fstat64 %s failed %d\n",
3180 procid, opno, fpath1.path, errno);
3183 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3185 if (fstat64(fd2, &stat2) < 0) {
3187 printf("%d/%lld: splice write - fstat64 %s failed %d\n",
3188 procid, opno, fpath2.path, errno);
3191 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3193 /* Calculate offsets */
3194 len = (random() % FILELEN_MAX) + 1;
3196 len = stat1.st_blksize;
3197 if (len > stat1.st_size)
3198 len = stat1.st_size;
3200 lr = ((int64_t)random() << 32) + random();
3201 if (stat1.st_size == len)
3204 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3208 * splice can overlap write, so the offset of the target file can be
3209 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3210 * past the current dest file EOF
3212 lr = ((int64_t)random() << 32) + random();
3213 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3216 * Since len, off1 and off2 will be changed later, preserve their
3223 /* Pipe initialize */
3224 if (pipe(filedes) < 0) {
3226 printf("%d/%lld: splice - pipe failed %d\n",
3227 procid, opno, errno);
3235 /* move to pipe buffer */
3236 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3242 /* move from pipe buffer to dst file */
3244 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3257 if (ret1 < 0 || ret2 < 0)
3262 printf("%d/%lld: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3264 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3265 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3267 if (length && length > total)
3268 printf(" asked for %lld, spliced %lld??\n",
3269 (long long)length, (long long)total);
3280 free_pathname(&fpath2);
3282 free_pathname(&fpath1);
3286 creat_f(opnum_t opno, long r)
3301 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3306 e1 = (random() % 100);
3307 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3309 if (type == FT_RTF) /* rt always gets an extsize */
3310 extsize = (random() % 10) + 1;
3311 else if (e1 < 10) /* one-in-ten get an extsize */
3312 extsize = random() % 1024;
3316 e = generate_fname(fep, type, &f, &id, &v);
3320 (void)fent_to_name(&f, fep);
3321 printf("%d/%lld: creat - no filename from %s\n",
3322 procid, opno, f.path);
3327 fd = creat_path(&f, 0666);
3328 e = fd < 0 ? errno : 0;
3333 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3334 if (type == FT_RTF) {
3335 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3336 a.fsx_extsize = extsize *
3337 geom.rtextsize * geom.blocksize;
3339 } else if (extsize) {
3340 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3341 a.fsx_extsize = extsize * geom.blocksize;
3344 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3347 add_to_flist(type, id, parid, 0);
3351 printf("%d/%lld: creat %s x:%d %d %d\n", procid, opno, f.path,
3352 extsize ? a.fsx_extsize : 0, e, e1);
3353 printf("%d/%lld: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3359 dread_f(opnum_t opno, long r)
3363 struct dioattr diob;
3376 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3378 printf("%d/%lld: dread - no filename\n", procid, opno);
3382 fd = open_path(&f, O_RDONLY|O_DIRECT);
3383 e = fd < 0 ? errno : 0;
3387 printf("%d/%lld: dread - open %s failed %d\n",
3388 procid, opno, f.path, e);
3392 if (fstat64(fd, &stb) < 0) {
3394 printf("%d/%lld: dread - fstat64 %s failed %d\n",
3395 procid, opno, f.path, errno);
3400 inode_info(st, sizeof(st), &stb, v);
3401 if (stb.st_size == 0) {
3403 printf("%d/%lld: dread - %s%s zero size\n", procid, opno,
3409 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3412 "%d/%lld: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3413 " fallback to stat()\n",
3414 procid, opno, f.path, st, errno);
3415 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3416 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
3419 dio_env = getenv("XFS_DIO_MIN");
3421 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3423 align = (int64_t)diob.d_miniosz;
3424 lr = ((int64_t)random() << 32) + random();
3425 off = (off64_t)(lr % stb.st_size);
3426 off -= (off % align);
3427 lseek64(fd, off, SEEK_SET);
3428 len = (random() % FILELEN_MAX) + 1;
3429 len -= (len % align);
3432 else if (len > diob.d_maxiosz)
3433 len = diob.d_maxiosz;
3434 buf = memalign(diob.d_mem, len);
3435 e = read(fd, buf, len) < 0 ? errno : 0;
3438 printf("%d/%lld: dread %s%s [%lld,%d] %d\n",
3439 procid, opno, f.path, st, (long long)off, (int)len, e);
3445 dwrite_f(opnum_t opno, long r)
3449 struct dioattr diob;
3462 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3464 printf("%d/%lld: dwrite - no filename\n", procid, opno);
3468 fd = open_path(&f, O_WRONLY|O_DIRECT);
3469 e = fd < 0 ? errno : 0;
3473 printf("%d/%lld: dwrite - open %s failed %d\n",
3474 procid, opno, f.path, e);
3478 if (fstat64(fd, &stb) < 0) {
3480 printf("%d/%lld: dwrite - fstat64 %s failed %d\n",
3481 procid, opno, f.path, errno);
3486 inode_info(st, sizeof(st), &stb, v);
3487 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3489 printf("%d/%lld: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3490 " %s%s return %d, fallback to stat()\n",
3491 procid, opno, f.path, st, errno);
3492 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3493 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
3496 dio_env = getenv("XFS_DIO_MIN");
3498 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3500 align = (int64_t)diob.d_miniosz;
3501 lr = ((int64_t)random() << 32) + random();
3502 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3503 off -= (off % align);
3504 lseek64(fd, off, SEEK_SET);
3505 len = (random() % FILELEN_MAX) + 1;
3506 len -= (len % align);
3509 else if (len > diob.d_maxiosz)
3510 len = diob.d_maxiosz;
3511 buf = memalign(diob.d_mem, len);
3513 lseek64(fd, off, SEEK_SET);
3514 memset(buf, nameseq & 0xff, len);
3515 e = write(fd, buf, len) < 0 ? errno : 0;
3518 printf("%d/%lld: dwrite %s%s [%lld,%d] %d\n",
3519 procid, opno, f.path, st, (long long)off, (int)len, e);
3525 #ifdef HAVE_LINUX_FALLOC_H
3526 struct print_flags falloc_flags [] = {
3527 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3528 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3529 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3530 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3531 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3532 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3536 #define translate_falloc_flags(mode) \
3537 ({translate_flags(mode, "|", falloc_flags);})
3541 do_fallocate(opnum_t opno, long r, int mode)
3543 #ifdef HAVE_LINUX_FALLOC_H
3555 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3557 printf("%d/%lld: do_fallocate - no filename\n", procid, opno);
3561 fd = open_path(&f, O_RDWR);
3564 printf("%d/%lld: do_fallocate - open %s failed %d\n",
3565 procid, opno, f.path, errno);
3570 if (fstat64(fd, &stb) < 0) {
3572 printf("%d/%lld: do_fallocate - fstat64 %s failed %d\n",
3573 procid, opno, f.path, errno);
3578 inode_info(st, sizeof(st), &stb, v);
3579 lr = ((int64_t)random() << 32) + random();
3580 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3582 len = (off64_t)(random() % (1024 * 1024));
3584 * Collapse/insert range requires off and len to be block aligned,
3585 * make it more likely to be the case.
3587 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3589 off = roundup_64(off, stb.st_blksize);
3590 len = roundup_64(len, stb.st_blksize);
3592 mode |= FALLOC_FL_KEEP_SIZE & random();
3593 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3595 printf("%d/%lld: fallocate(%s) %s %st %lld %lld %d\n",
3596 procid, opno, translate_falloc_flags(mode),
3597 f.path, st, (long long)off, (long long)len, e);
3604 fallocate_f(opnum_t opno, long r)
3606 #ifdef HAVE_LINUX_FALLOC_H
3607 do_fallocate(opno, r, 0);
3612 fdatasync_f(opnum_t opno, long r)
3620 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3622 printf("%d/%lld: fdatasync - no filename\n",
3627 fd = open_path(&f, O_WRONLY);
3628 e = fd < 0 ? errno : 0;
3632 printf("%d/%lld: fdatasync - open %s failed %d\n",
3633 procid, opno, f.path, e);
3637 e = fdatasync(fd) < 0 ? errno : 0;
3639 printf("%d/%lld: fdatasync %s %d\n", procid, opno, f.path, e);
3644 #ifdef HAVE_LINUX_FIEMAP_H
3645 struct print_flags fiemap_flags[] = {
3646 { FIEMAP_FLAG_SYNC, "SYNC"},
3647 { FIEMAP_FLAG_XATTR, "XATTR"},
3651 #define translate_fiemap_flags(mode) \
3652 ({translate_flags(mode, "|", fiemap_flags);})
3656 fiemap_f(opnum_t opno, long r)
3658 #ifdef HAVE_LINUX_FIEMAP_H
3668 struct fiemap *fiemap;
3671 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3673 printf("%d/%lld: fiemap - no filename\n", procid, opno);
3677 fd = open_path(&f, O_RDWR);
3678 e = fd < 0 ? errno : 0;
3682 printf("%d/%lld: fiemap - open %s failed %d\n",
3683 procid, opno, f.path, e);
3687 if (fstat64(fd, &stb) < 0) {
3689 printf("%d/%lld: fiemap - fstat64 %s failed %d\n",
3690 procid, opno, f.path, errno);
3695 inode_info(st, sizeof(st), &stb, v);
3696 blocks_to_map = random() & 0xffff;
3697 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3698 (blocks_to_map * sizeof(struct fiemap_extent)));
3701 printf("%d/%lld: malloc failed \n", procid, opno);
3706 lr = ((int64_t)random() << 32) + random();
3707 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3709 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3710 fiemap->fm_extent_count = blocks_to_map;
3711 fiemap->fm_mapped_extents = random() & 0xffff;
3712 fiemap->fm_start = off;
3713 fiemap->fm_length = ((int64_t)random() << 32) + random();
3715 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3717 printf("%d/%lld: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3718 procid, opno, f.path, st, (long long)fiemap->fm_start,
3719 (long long) fiemap->fm_length,
3720 translate_fiemap_flags(fiemap->fm_flags), e);
3728 freesp_f(opnum_t opno, long r)
3733 struct xfs_flock64 fl;
3741 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3743 printf("%d/%lld: freesp - no filename\n", procid, opno);
3747 fd = open_path(&f, O_RDWR);
3748 e = fd < 0 ? errno : 0;
3752 printf("%d/%lld: freesp - open %s failed %d\n",
3753 procid, opno, f.path, e);
3757 if (fstat64(fd, &stb) < 0) {
3759 printf("%d/%lld: freesp - fstat64 %s failed %d\n",
3760 procid, opno, f.path, errno);
3765 inode_info(st, sizeof(st), &stb, v);
3766 lr = ((int64_t)random() << 32) + random();
3767 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3769 fl.l_whence = SEEK_SET;
3772 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3774 printf("%d/%lld: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3775 procid, opno, f.path, st, (long long)off, e);
3781 fsync_f(opnum_t opno, long r)
3789 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3791 printf("%d/%lld: fsync - no filename\n", procid, opno);
3795 fd = open_file_or_dir(&f, O_WRONLY);
3796 e = fd < 0 ? errno : 0;
3800 printf("%d/%lld: fsync - open %s failed %d\n",
3801 procid, opno, f.path, e);
3805 e = fsync(fd) < 0 ? errno : 0;
3807 printf("%d/%lld: fsync %s %d\n", procid, opno, f.path, e);
3813 gen_random_string(int len)
3815 static const char charset[] = "0123456789"
3816 "abcdefghijklmnopqrstuvwxyz"
3817 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3828 for (i = 0; i < len; i++)
3829 s[i] = charset[random() % sizeof(charset)];
3835 getattr_f(opnum_t opno, long r)
3844 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3845 append_pathname(&f, ".");
3846 fd = open_path(&f, O_RDWR);
3847 e = fd < 0 ? errno : 0;
3850 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3852 printf("%d/%lld: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3858 getdents_f(opnum_t opno, long r)
3865 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3866 append_pathname(&f, ".");
3867 dir = opendir_path(&f);
3871 printf("%d/%lld: getdents - can't open %s\n",
3872 procid, opno, f.path);
3876 while (readdir64(dir) != NULL)
3879 printf("%d/%lld: getdents %s 0\n", procid, opno, f.path);
3885 getfattr_f(opnum_t opno, long r)
3891 char name[XATTR_NAME_BUF_SIZE];
3897 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3899 printf("%d/%lld: getfattr - no filename\n", procid, opno);
3905 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3906 * to read a xattr that doesn't exist (fgetxattr should fail with
3907 * errno set to ENOATTR (61) in this case).
3909 if (fep->xattr_counter > 0)
3910 xattr_num = (random() % fep->xattr_counter) + 1;
3914 e = generate_xattr_name(xattr_num, name, sizeof(name));
3916 printf("%d/%lld: getfattr - file %s failed to generate xattr name: %d\n",
3917 procid, opno, f.path, e);
3921 value_len = getxattr(f.path, name, NULL, 0);
3922 if (value_len < 0) {
3924 printf("%d/%lld: getfattr file %s name %s failed %d\n",
3925 procid, opno, f.path, name, errno);
3929 /* A xattr without value.*/
3930 if (value_len == 0) {
3935 value = malloc(value_len);
3938 printf("%d/%lld: getfattr file %s failed to allocate buffer with %d bytes\n",
3939 procid, opno, f.path, value_len);
3943 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3946 printf("%d/%lld: getfattr file %s name %s value length %d %d\n",
3947 procid, opno, f.path, name, value_len, e);
3954 link_f(opnum_t opno, long r)
3968 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3970 printf("%d/%lld: link - no file\n", procid, opno);
3974 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3980 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3984 (void)fent_to_name(&l, fep);
3985 printf("%d/%lld: link - no filename from %s\n",
3986 procid, opno, l.path);
3992 e = link_path(&f, &l) < 0 ? errno : 0;
3995 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3997 printf("%d/%lld: link %s %s %d\n", procid, opno, f.path, l.path,
3999 printf("%d/%lld: link add id=%d,parent=%d\n", procid, opno, id, parid);
4006 listfattr_f(opnum_t opno, long r)
4012 char *buffer = NULL;
4016 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4018 printf("%d/%lld: listfattr - no filename\n", procid, opno);
4023 e = listxattr(f.path, NULL, 0);
4026 printf("%d/%lld: listfattr %s failed %d\n",
4027 procid, opno, f.path, errno);
4031 if (buffer_len == 0) {
4033 printf("%d/%lld: listfattr %s - has no extended attributes\n",
4034 procid, opno, f.path);
4038 buffer = malloc(buffer_len);
4041 printf("%d/%lld: listfattr %s failed to allocate buffer with %d bytes\n",
4042 procid, opno, f.path, buffer_len);
4046 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4048 printf("%d/%lld: listfattr %s buffer length %d %d\n",
4049 procid, opno, f.path, buffer_len, e);
4056 mkdir_f(opnum_t opno, long r)
4066 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4071 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4075 (void)fent_to_name(&f, fep);
4076 printf("%d/%lld: mkdir - no filename from %s\n",
4077 procid, opno, f.path);
4082 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4085 add_to_flist(FT_DIR, id, parid, 0);
4087 printf("%d/%lld: mkdir %s %d\n", procid, opno, f.path, e);
4088 printf("%d/%lld: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4094 mknod_f(opnum_t opno, long r)
4104 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4109 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4113 (void)fent_to_name(&f, fep);
4114 printf("%d/%lld: mknod - no filename from %s\n",
4115 procid, opno, f.path);
4120 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4123 add_to_flist(FT_DEV, id, parid, 0);
4125 printf("%d/%lld: mknod %s %d\n", procid, opno, f.path, e);
4126 printf("%d/%lld: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4131 #ifdef HAVE_SYS_MMAN_H
4132 struct print_flags mmap_flags[] = {
4133 { MAP_SHARED, "SHARED"},
4134 { MAP_PRIVATE, "PRIVATE"},
4138 #define translate_mmap_flags(flags) \
4139 ({translate_flags(flags, "|", mmap_flags);})
4143 do_mmap(opnum_t opno, long r, int prot)
4145 #ifdef HAVE_SYS_MMAN_H
4157 sigjmp_buf sigbus_jmpbuf;
4160 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4162 printf("%d/%lld: do_mmap - no filename\n", procid, opno);
4166 fd = open_path(&f, O_RDWR);
4167 e = fd < 0 ? errno : 0;
4171 printf("%d/%lld: do_mmap - open %s failed %d\n",
4172 procid, opno, f.path, e);
4176 if (fstat64(fd, &stb) < 0) {
4178 printf("%d/%lld: do_mmap - fstat64 %s failed %d\n",
4179 procid, opno, f.path, errno);
4184 inode_info(st, sizeof(st), &stb, v);
4185 if (stb.st_size == 0) {
4187 printf("%d/%lld: do_mmap - %s%s zero size\n", procid, opno,
4194 lr = ((int64_t)random() << 32) + random();
4195 off = (off64_t)(lr % stb.st_size);
4196 off = rounddown_64(off, sysconf(_SC_PAGE_SIZE));
4197 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4199 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4200 addr = mmap(NULL, len, prot, flags, fd, off);
4201 e = (addr == MAP_FAILED) ? errno : 0;
4204 printf("%d/%lld: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4205 procid, opno, f.path, st, (long long)off,
4206 (int)len, translate_mmap_flags(flags), e);
4212 if (prot & PROT_WRITE) {
4213 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4214 sigbus_jmp = &sigbus_jmpbuf;
4215 memset(addr, nameseq & 0xff, len);
4219 if ((buf = malloc(len)) != NULL) {
4220 memcpy(buf, addr, len);
4225 /* set NULL to stop other functions from doing siglongjmp */
4229 printf("%d/%lld: %s %s%s [%lld,%d,%s] %s\n",
4230 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4231 f.path, st, (long long)off, (int)len,
4232 translate_mmap_flags(flags),
4233 (e == 0) ? "0" : "Bus error");
4241 mread_f(opnum_t opno, long r)
4243 #ifdef HAVE_SYS_MMAN_H
4244 do_mmap(opno, r, PROT_READ);
4249 mwrite_f(opnum_t opno, long r)
4251 #ifdef HAVE_SYS_MMAN_H
4252 do_mmap(opno, r, PROT_WRITE);
4257 punch_f(opnum_t opno, long r)
4259 #ifdef HAVE_LINUX_FALLOC_H
4260 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4265 zero_f(opnum_t opno, long r)
4267 #ifdef HAVE_LINUX_FALLOC_H
4268 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4273 collapse_f(opnum_t opno, long r)
4275 #ifdef HAVE_LINUX_FALLOC_H
4276 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4281 insert_f(opnum_t opno, long r)
4283 #ifdef HAVE_LINUX_FALLOC_H
4284 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4289 read_f(opnum_t opno, long r)
4303 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4305 printf("%d/%lld: read - no filename\n", procid, opno);
4309 fd = open_path(&f, O_RDONLY);
4310 e = fd < 0 ? errno : 0;
4314 printf("%d/%lld: read - open %s failed %d\n",
4315 procid, opno, f.path, e);
4319 if (fstat64(fd, &stb) < 0) {
4321 printf("%d/%lld: read - fstat64 %s failed %d\n",
4322 procid, opno, f.path, errno);
4327 inode_info(st, sizeof(st), &stb, v);
4328 if (stb.st_size == 0) {
4330 printf("%d/%lld: read - %s%s zero size\n", procid, opno,
4336 lr = ((int64_t)random() << 32) + random();
4337 off = (off64_t)(lr % stb.st_size);
4338 lseek64(fd, off, SEEK_SET);
4339 len = (random() % FILELEN_MAX) + 1;
4341 e = read(fd, buf, len) < 0 ? errno : 0;
4344 printf("%d/%lld: read %s%s [%lld,%d] %d\n",
4345 procid, opno, f.path, st, (long long)off, (int)len, e);
4351 readlink_f(opnum_t opno, long r)
4359 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4361 printf("%d/%lld: readlink - no filename\n", procid, opno);
4365 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4368 printf("%d/%lld: readlink %s %d\n", procid, opno, f.path, e);
4373 readv_f(opnum_t opno, long r)
4385 struct iovec *iov = NULL;
4392 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4394 printf("%d/%lld: readv - no filename\n", procid, opno);
4398 fd = open_path(&f, O_RDONLY);
4399 e = fd < 0 ? errno : 0;
4403 printf("%d/%lld: readv - open %s failed %d\n",
4404 procid, opno, f.path, e);
4408 if (fstat64(fd, &stb) < 0) {
4410 printf("%d/%lld: readv - fstat64 %s failed %d\n",
4411 procid, opno, f.path, errno);
4416 inode_info(st, sizeof(st), &stb, v);
4417 if (stb.st_size == 0) {
4419 printf("%d/%lld: readv - %s%s zero size\n", procid, opno,
4425 lr = ((int64_t)random() << 32) + random();
4426 off = (off64_t)(lr % stb.st_size);
4427 lseek64(fd, off, SEEK_SET);
4428 len = (random() % FILELEN_MAX) + 1;
4431 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4432 iov = calloc(iovcnt, sizeof(struct iovec));
4433 iovl = len / iovcnt;
4435 for (i=0; i<iovcnt; i++) {
4436 (iov + i)->iov_base = (buf + iovb);
4437 (iov + i)->iov_len = iovl;
4441 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4444 printf("%d/%lld: readv %s%s [%lld,%d,%d] %d\n",
4445 procid, opno, f.path, st, (long long)off, (int)iovl,
4452 removefattr_f(opnum_t opno, long r)
4458 char name[XATTR_NAME_BUF_SIZE];
4462 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4464 printf("%d/%lld: removefattr - no filename\n", procid, opno);
4470 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4471 * remove a xattr that doesn't exist (fremovexattr should fail with
4472 * errno set to ENOATTR (61) in this case).
4474 if (fep->xattr_counter > 0)
4475 xattr_num = (random() % fep->xattr_counter) + 1;
4479 e = generate_xattr_name(xattr_num, name, sizeof(name));
4481 printf("%d/%lld: removefattr - file %s failed to generate xattr name: %d\n",
4482 procid, opno, f.path, e);
4486 e = removexattr(f.path, name) < 0 ? errno : 0;
4488 printf("%d/%lld: removefattr file %s name %s %d\n",
4489 procid, opno, f.path, name, e);
4494 struct print_flags renameat2_flags [] = {
4495 { RENAME_NOREPLACE, "NOREPLACE"},
4496 { RENAME_EXCHANGE, "EXCHANGE"},
4497 { RENAME_WHITEOUT, "WHITEOUT"},
4501 #define translate_renameat2_flags(mode) \
4502 ({translate_flags(mode, "|", renameat2_flags);})
4505 do_renameat2(opnum_t opno, long r, int mode)
4521 /* get an existing path for the source of the rename */
4523 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4524 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4526 printf("%d/%lld: rename - no source filename\n",
4533 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4534 * order to maintain filelist/filename integrity, we should
4535 * restrict exchange operation to files of the same type.
4537 if (mode == RENAME_EXCHANGE) {
4538 which = 1 << (flp - flist);
4539 init_pathname(&newf);
4540 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4542 printf("%d/%lld: rename - no target filename\n",
4544 free_pathname(&newf);
4548 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4549 fents_ancestor_check(dfep, fep))) {
4551 printf("%d/%lld: rename(REXCHANGE) %s and %s "
4552 "have ancestor-descendant relationship\n",
4553 procid, opno, f.path, newf.path);
4554 free_pathname(&newf);
4560 parid = dfep->parent;
4563 * Get an existing directory for the destination parent
4566 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4573 * Generate a new path using an existing parent directory
4576 init_pathname(&newf);
4577 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4581 (void)fent_to_name(&f, dfep);
4582 printf("%d/%lld: rename - no filename from %s\n",
4583 procid, opno, f.path);
4585 free_pathname(&newf);
4590 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4593 int xattr_counter = fep->xattr_counter;
4594 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4595 int ft = flp - flist;
4598 oldparid = fep->parent;
4601 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4602 * old parent id for the others.
4604 if (ft == FT_DIR || ft == FT_SUBVOL)
4605 fix_parent(oldid, id, swap);
4607 if (mode == RENAME_WHITEOUT) {
4608 fep->xattr_counter = 0;
4609 add_to_flist(flp - flist, id, parid, xattr_counter);
4610 } else if (mode == RENAME_EXCHANGE) {
4611 fep->xattr_counter = dfep->xattr_counter;
4612 dfep->xattr_counter = xattr_counter;
4614 del_from_flist(flp - flist, fep - flp->fents);
4615 add_to_flist(flp - flist, id, parid, xattr_counter);
4619 printf("%d/%lld: rename(%s) %s to %s %d\n", procid,
4620 opno, translate_renameat2_flags(mode), f.path,
4623 printf("%d/%lld: rename source entry: id=%d,parent=%d\n",
4624 procid, opno, oldid, oldparid);
4625 printf("%d/%lld: rename target entry: id=%d,parent=%d\n",
4626 procid, opno, id, parid);
4629 free_pathname(&newf);
4634 rename_f(opnum_t opno, long r)
4636 do_renameat2(opno, r, 0);
4640 rnoreplace_f(opnum_t opno, long r)
4642 do_renameat2(opno, r, RENAME_NOREPLACE);
4646 rexchange_f(opnum_t opno, long r)
4648 do_renameat2(opno, r, RENAME_EXCHANGE);
4652 rwhiteout_f(opnum_t opno, long r)
4654 do_renameat2(opno, r, RENAME_WHITEOUT);
4658 resvsp_f(opnum_t opno, long r)
4663 struct xfs_flock64 fl;
4671 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4673 printf("%d/%lld: resvsp - no filename\n", procid, opno);
4677 fd = open_path(&f, O_RDWR);
4678 e = fd < 0 ? errno : 0;
4682 printf("%d/%lld: resvsp - open %s failed %d\n",
4683 procid, opno, f.path, e);
4687 if (fstat64(fd, &stb) < 0) {
4689 printf("%d/%lld: resvsp - fstat64 %s failed %d\n",
4690 procid, opno, f.path, errno);
4695 inode_info(st, sizeof(st), &stb, v);
4696 lr = ((int64_t)random() << 32) + random();
4697 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4699 fl.l_whence = SEEK_SET;
4701 fl.l_len = (off64_t)(random() % (1024 * 1024));
4702 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4704 printf("%d/%lld: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4705 procid, opno, f.path, st,
4706 (long long)off, (long long)fl.l_len, e);
4712 rmdir_f(opnum_t opno, long r)
4722 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4724 printf("%d/%lld: rmdir - no directory\n", procid, opno);
4728 e = rmdir_path(&f) < 0 ? errno : 0;
4732 oldparid = fep->parent;
4733 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4736 printf("%d/%lld: rmdir %s %d\n", procid, opno, f.path, e);
4738 printf("%d/%lld: rmdir del entry: id=%d,parent=%d\n",
4739 procid, opno, oldid, oldparid);
4745 setattr_f(opnum_t opno, long r)
4754 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4755 append_pathname(&f, ".");
4756 fd = open_path(&f, O_RDWR);
4757 e = fd < 0 ? errno : 0;
4760 fl = attr_mask & (uint)random();
4761 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4763 printf("%d/%lld: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4769 setfattr_f(opnum_t opno, long r)
4776 char name[XATTR_NAME_BUF_SIZE];
4782 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4784 printf("%d/%lld: setfattr - no filename\n", procid, opno);
4789 if ((fep->xattr_counter > 0) && (random() % 2)) {
4791 * Use an existing xattr name for replacing its value or
4792 * create again a xattr that was previously deleted.
4794 xattr_num = (random() % fep->xattr_counter) + 1;
4796 flag = XATTR_REPLACE;
4798 /* Use a new xattr name. */
4799 xattr_num = fep->xattr_counter + 1;
4801 * Don't always use the create flag because even if our xattr
4802 * counter is 0, we may still have xattrs associated to this
4803 * file (this happens when xattrs are added to a file through
4804 * one of its other hard links), so we can end up updating an
4805 * existing xattr too.
4808 flag = XATTR_CREATE;
4812 * The maximum supported value size depends on the filesystem
4813 * implementation, but 100 bytes is a safe value for most filesystems
4816 value_len = random() % 101;
4817 value = gen_random_string(value_len);
4818 if (!value && value_len > 0) {
4820 printf("%d/%lld: setfattr - file %s failed to allocate value with %d bytes\n",
4821 procid, opno, f.path, value_len);
4824 e = generate_xattr_name(xattr_num, name, sizeof(name));
4826 printf("%d/%lld: setfattr - file %s failed to generate xattr name: %d\n",
4827 procid, opno, f.path, e);
4831 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4833 fep->xattr_counter++;
4835 printf("%d/%lld: setfattr file %s name %s flag %s value length %d: %d\n",
4836 procid, opno, f.path, name, xattr_flag_to_string(flag),
4844 snapshot_f(opnum_t opno, long r)
4846 #ifdef HAVE_BTRFSUTIL_H
4847 enum btrfs_util_error e;
4858 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4860 printf("%d/%lld: snapshot - no subvolume\n", procid,
4865 init_pathname(&newf);
4867 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4871 (void)fent_to_name(&f, fep);
4872 printf("%d/%lld: snapshot - no filename from %s\n",
4873 procid, opno, f.path);
4878 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4879 if (e == BTRFS_UTIL_OK)
4880 add_to_flist(FT_SUBVOL, id, parid, 0);
4882 printf("%d/%lld: snapshot %s->%s %d(%s)\n", procid, opno,
4883 f.path, newf.path, e, btrfs_util_strerror(e));
4884 printf("%d/%lld: snapshot add id=%d,parent=%d\n", procid, opno,
4887 free_pathname(&newf);
4893 stat_f(opnum_t opno, long r)
4901 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4903 printf("%d/%lld: stat - no entries\n", procid, opno);
4907 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4910 printf("%d/%lld: stat %s %d\n", procid, opno, f.path, e);
4915 subvol_create_f(opnum_t opno, long r)
4917 #ifdef HAVE_BTRFSUTIL_H
4918 enum btrfs_util_error e;
4928 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4932 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4936 (void)fent_to_name(&f, fep);
4937 printf("%d/%lld: subvol_create - no filename from %s\n",
4938 procid, opno, f.path);
4943 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4944 if (e == BTRFS_UTIL_OK)
4945 add_to_flist(FT_SUBVOL, id, parid, 0);
4947 printf("%d/%lld: subvol_create %s %d(%s)\n", procid, opno,
4948 f.path, e, btrfs_util_strerror(e));
4949 printf("%d/%lld: subvol_create add id=%d,parent=%d\n", procid,
4957 subvol_delete_f(opnum_t opno, long r)
4959 #ifdef HAVE_BTRFSUTIL_H
4960 enum btrfs_util_error e;
4968 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4970 printf("%d:%lld: subvol_delete - no subvolume\n", procid,
4975 e = btrfs_util_delete_subvolume(f.path, 0);
4977 if (e == BTRFS_UTIL_OK) {
4979 oldparid = fep->parent;
4980 delete_subvol_children(oldid);
4981 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4984 printf("%d/%lld: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4985 e, btrfs_util_strerror(e));
4986 if (e == BTRFS_UTIL_OK)
4987 printf("%d/%lld: subvol_delete del entry: id=%d,parent=%d\n",
4988 procid, opno, oldid, oldparid);
4995 symlink_f(opnum_t opno, long r)
5008 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5013 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5017 (void)fent_to_name(&f, fep);
5018 printf("%d/%lld: symlink - no filename from %s\n",
5019 procid, opno, f.path);
5024 len = (int)(random() % PATH_MAX);
5025 val = malloc(len + 1);
5027 memset(val, 'x', len);
5029 for (i = 10; i < len - 1; i += 10)
5031 e = symlink_path(val, &f) < 0 ? errno : 0;
5034 add_to_flist(FT_SYM, id, parid, 0);
5037 printf("%d/%lld: symlink %s %d\n", procid, opno, f.path, e);
5038 printf("%d/%lld: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5045 sync_f(opnum_t opno, long r)
5049 printf("%d/%lld: sync\n", procid, opno);
5053 truncate_f(opnum_t opno, long r)
5064 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5066 printf("%d/%lld: truncate - no filename\n", procid, opno);
5070 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5074 printf("%d/%lld: truncate - stat64 %s failed %d\n",
5075 procid, opno, f.path, e);
5079 inode_info(st, sizeof(st), &stb, v);
5080 lr = ((int64_t)random() << 32) + random();
5081 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5083 e = truncate64_path(&f, off) < 0 ? errno : 0;
5086 printf("%d/%lld: truncate %s%s %lld %d\n", procid, opno, f.path,
5087 st, (long long)off, e);
5092 unlink_f(opnum_t opno, long r)
5103 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5105 printf("%d/%lld: unlink - no file\n", procid, opno);
5109 e = unlink_path(&f) < 0 ? errno : 0;
5113 oldparid = fep->parent;
5114 del_from_flist(flp - flist, fep - flp->fents);
5117 printf("%d/%lld: unlink %s %d\n", procid, opno, f.path, e);
5119 printf("%d/%lld: unlink del entry: id=%d,parent=%d\n",
5120 procid, opno, oldid, oldparid);
5126 unresvsp_f(opnum_t opno, long r)
5131 struct xfs_flock64 fl;
5139 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5141 printf("%d/%lld: unresvsp - no filename\n", procid, opno);
5145 fd = open_path(&f, O_RDWR);
5146 e = fd < 0 ? errno : 0;
5150 printf("%d/%lld: unresvsp - open %s failed %d\n",
5151 procid, opno, f.path, e);
5155 if (fstat64(fd, &stb) < 0) {
5157 printf("%d/%lld: unresvsp - fstat64 %s failed %d\n",
5158 procid, opno, f.path, errno);
5163 inode_info(st, sizeof(st), &stb, v);
5164 lr = ((int64_t)random() << 32) + random();
5165 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5167 fl.l_whence = SEEK_SET;
5169 fl.l_len = (off64_t)(random() % (1 << 20));
5170 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5172 printf("%d/%lld: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5173 procid, opno, f.path, st,
5174 (long long)off, (long long)fl.l_len, e);
5180 uring_read_f(opnum_t opno, long r)
5183 do_uring_rw(opno, r, O_RDONLY);
5188 uring_write_f(opnum_t opno, long r)
5191 do_uring_rw(opno, r, O_WRONLY);
5196 write_f(opnum_t opno, long r)
5210 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5212 printf("%d/%lld: write - no filename\n", procid, opno);
5216 fd = open_path(&f, O_WRONLY);
5217 e = fd < 0 ? errno : 0;
5221 printf("%d/%lld: write - open %s failed %d\n",
5222 procid, opno, f.path, e);
5226 if (fstat64(fd, &stb) < 0) {
5228 printf("%d/%lld: write - fstat64 %s failed %d\n",
5229 procid, opno, f.path, errno);
5234 inode_info(st, sizeof(st), &stb, v);
5235 lr = ((int64_t)random() << 32) + random();
5236 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5238 lseek64(fd, off, SEEK_SET);
5239 len = (random() % FILELEN_MAX) + 1;
5241 memset(buf, nameseq & 0xff, len);
5242 e = write(fd, buf, len) < 0 ? errno : 0;
5245 printf("%d/%lld: write %s%s [%lld,%d] %d\n",
5246 procid, opno, f.path, st, (long long)off, (int)len, e);
5252 writev_f(opnum_t opno, long r)
5264 struct iovec *iov = NULL;
5271 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5273 printf("%d/%lld: writev - no filename\n", procid, opno);
5277 fd = open_path(&f, O_WRONLY);
5278 e = fd < 0 ? errno : 0;
5282 printf("%d/%lld: writev - open %s failed %d\n",
5283 procid, opno, f.path, e);
5287 if (fstat64(fd, &stb) < 0) {
5289 printf("%d/%lld: writev - fstat64 %s failed %d\n",
5290 procid, opno, f.path, errno);
5295 inode_info(st, sizeof(st), &stb, v);
5296 lr = ((int64_t)random() << 32) + random();
5297 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5299 lseek64(fd, off, SEEK_SET);
5300 len = (random() % FILELEN_MAX) + 1;
5302 memset(buf, nameseq & 0xff, len);
5304 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5305 iov = calloc(iovcnt, sizeof(struct iovec));
5306 iovl = len / iovcnt;
5308 for (i=0; i<iovcnt; i++) {
5309 (iov + i)->iov_base = (buf + iovb);
5310 (iov + i)->iov_len = iovl;
5314 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5318 printf("%d/%lld: writev %s%s [%lld,%d,%d] %d\n",
5319 procid, opno, f.path, st, (long long)off, (int)iovl,
5326 xattr_flag_to_string(int flag)
5328 if (flag == XATTR_CREATE)
5330 if (flag == XATTR_REPLACE)