1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
15 #ifdef HAVE_BTRFSUTIL_H
16 #include <btrfsutil.h>
18 #ifdef HAVE_LINUX_FIEMAP_H
19 #include <linux/fiemap.h>
21 #ifdef HAVE_SYS_PRCTL_H
22 #include <sys/prctl.h>
31 #define URING_ENTRIES 1
33 bool have_io_uring; /* to indicate runtime availability */
35 #include <sys/syscall.h>
36 #include <sys/xattr.h>
38 #ifndef FS_IOC_GETFLAGS
39 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
41 #ifndef FS_IOC_SETFLAGS
42 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
46 #define XFS_ERRTAG_MAX 17
47 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
48 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
53 #ifndef HAVE_RENAMEAT2
54 #if !defined(SYS_renameat2) && defined(__x86_64__)
55 #define SYS_renameat2 316
58 #if !defined(SYS_renameat2) && defined(__i386__)
59 #define SYS_renameat2 353
62 static int renameat2(int dfd1, const char *path1,
63 int dfd2, const char *path2,
67 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
75 #ifndef RENAME_NOREPLACE
76 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
78 #ifndef RENAME_EXCHANGE
79 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
81 #ifndef RENAME_WHITEOUT
82 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
85 #define FILELEN_MAX (32*4096)
151 typedef long long opnum_t;
153 typedef void (*opfnc_t)(opnum_t, long);
155 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);
281 struct opdesc ops[OP_LAST] = {
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, 0, 1 },
287 [OP_ATTR_SET] = {"attr_set", attr_set_f, 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;
955 ret = stat64(".", &statbuf);
957 fprintf(stderr, "fsstress: check_cwd stat64() returned %d with errno: %d (%s)\n",
958 ret, errno, strerror(errno));
962 if (statbuf.st_ino == top_ino)
965 fprintf(stderr, "fsstress: check_cwd statbuf.st_ino (%llu) != top_ino (%llu)\n",
966 (unsigned long long) statbuf.st_ino,
967 (unsigned long long) top_ino);
969 assert(chdir(homedir) == 0);
970 fprintf(stderr, "fsstress: check_cwd failure\n");
977 * go thru flist and release all entries
979 * NOTE: this function shouldn't be called until the end of a process
987 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
996 creat_path(pathname_t *name, mode_t mode)
998 char buf[NAME_MAX + 1];
1002 rval = creat(name->path, mode);
1003 if (rval >= 0 || errno != ENAMETOOLONG)
1005 separate_pathname(name, buf, &newname);
1006 if (chdir(buf) == 0) {
1007 rval = creat_path(&newname, mode);
1008 assert(chdir("..") == 0);
1010 free_pathname(&newname);
1015 dcache_enter(int dirid, int slot)
1017 dcache[dirid % NDCACHE] = slot;
1025 for (i = 0; i < NDCACHE; i++)
1030 dcache_lookup(int dirid)
1035 i = dcache[dirid % NDCACHE];
1036 if (i >= 0 && i < flist[FT_DIR].nfiles &&
1037 (fep = &flist[FT_DIR].fents[i])->id == dirid)
1039 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
1040 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
1046 dcache_purge(int dirid, int ft)
1049 dcp = &dcache[dirid % NDCACHE];
1050 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1051 flist[ft].fents[*dcp].id == dirid)
1056 * Delete the item from the list by
1057 * moving last entry over the deleted one;
1058 * unless deleted entry is the last one.
1059 * Input: which file list array and which slot in array
1062 del_from_flist(int ft, int slot)
1067 if (ft == FT_DIR || ft == FT_SUBVOL)
1068 dcache_purge(ftp->fents[slot].id, ft);
1069 if (slot != ftp->nfiles - 1) {
1070 if (ft == FT_DIR || ft == FT_SUBVOL)
1071 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1072 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1078 delete_subvol_children(int parid)
1083 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1085 for (c = 0; c < flp->nfiles; c++) {
1086 if (flp->fents[c].parent == parid) {
1087 int id = flp->fents[c].id;
1088 del_from_flist(i, c);
1089 if (i == FT_DIR || i == FT_SUBVOL)
1090 delete_subvol_children(id);
1098 dirid_to_fent(int dirid)
1105 if ((fep = dcache_lookup(dirid)))
1109 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1110 if (fep->id == dirid) {
1111 dcache_enter(dirid, fep - flp->fents);
1125 struct stat64 statbuf;
1133 dividend = (operations + execute_freq) / (execute_freq + 1);
1134 sprintf(buf, "p%x", procid);
1135 (void)mkdir(buf, 0777);
1136 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1140 top_ino = statbuf.st_ino;
1141 homedir = getcwd(NULL, 0);
1143 perror("getcwd failed");
1149 namerand = random();
1150 for (opno = 0; opno < operations; opno++) {
1151 if (execute_cmd && opno && opno % dividend == 0) {
1153 printf("%lld: execute command %s\n", opno,
1155 rval = system(execute_cmd);
1157 fprintf(stderr, "execute command failed with "
1160 p = &ops[freq_table[random() % freq_table_size]];
1161 p->func(opno, random());
1163 * test for forced shutdown by stat'ing the test
1164 * directory. If this stat returns EIO, assume
1165 * the forced shutdown happened.
1167 if (errtag != 0 && opno % 100 == 0) {
1168 rval = stat64(".", &statbuf);
1170 fprintf(stderr, "Detected EIO\n");
1176 assert(chdir("..") == 0);
1181 sprintf(cmd, "rm -rf %s", buf);
1184 perror("cleaning up");
1190 * build up a pathname going thru the file entry and all
1191 * its parent entries
1192 * Return 0 on error, 1 on success;
1195 fent_to_name(pathname_t *name, fent_t *fep)
1197 flist_t *flp = &flist[fep->ft];
1198 char buf[NAME_MAX + 1];
1206 /* build up parent directory name */
1207 if (fep->parent != -1) {
1208 pfep = dirid_to_fent(fep->parent);
1211 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1212 procid, fep->id, fep->parent);
1217 e = fent_to_name(name, pfep);
1220 append_pathname(name, "/");
1223 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1224 namerandpad(fep->id, buf, i);
1225 append_pathname(name, buf);
1230 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1234 for (tmpfep = fep; tmpfep->parent != -1;
1235 tmpfep = dirid_to_fent(tmpfep->parent)) {
1236 if (tmpfep->parent == dfep->id)
1244 fix_parent(int oldid, int newid, bool swap)
1251 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1252 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1253 if (fep->parent == oldid)
1254 fep->parent = newid;
1255 else if (swap && fep->parent == newid)
1256 fep->parent = oldid;
1262 free_pathname(pathname_t *name)
1272 * Generate a filename of type ft.
1273 * If we have a fep which should be a directory then use it
1274 * as the parent path for this new filename i.e. prepend with it.
1277 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1279 char buf[NAME_MAX + 1];
1288 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1289 namerandpad(id, buf, len);
1291 /* prepend fep parent dir-name to it */
1293 e = fent_to_name(name, fep);
1296 append_pathname(name, "/");
1298 append_pathname(name, buf);
1302 for (j = 0; !*v && j < ilistlen; j++) {
1303 if (ilist[j] == id) {
1311 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1315 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1325 * Input: "which" to choose the file-types eg. non-directory
1326 * Input: "r" to choose which file
1327 * Output: file-list, file-entry, name for the chosen file.
1328 * Output: verbose if chosen file is on the ilist.
1331 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1334 int totalsum = 0; /* total number of matching files */
1335 int partialsum = 0; /* partial sum of matching files */
1341 int e = 1; /* success */
1344 * go thru flist and add up number of files for each
1345 * category that matches with <which>.
1347 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1348 if (which & (1 << i))
1349 totalsum += flp->nfiles;
1351 if (totalsum == 0) {
1361 * Now we have possible matches between 0..totalsum-1.
1362 * And we use r to help us choose which one we want,
1363 * which when bounded by totalsum becomes x.
1365 x = (int)(r % totalsum);
1366 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1367 if (which & (1 << i)) {
1368 if (x < partialsum + flp->nfiles) {
1370 /* found the matching file entry */
1371 fep = &flp->fents[x - partialsum];
1373 /* fill-in what we were asked for */
1375 e = fent_to_name(name, fep);
1378 fprintf(stderr, "%d: failed to get path for entry:"
1379 " id=%d,parent=%d\n",
1380 procid, fep->id, fep->parent);
1389 /* turn on verbose if its an ilisted file */
1391 for (j = 0; !*v && j < ilistlen; j++) {
1392 if (ilist[j] == fep->id) {
1399 partialsum += flp->nfiles;
1403 fprintf(stderr, "fsstress: get_fname failure\n");
1410 init_pathname(pathname_t *name)
1417 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1419 char buf[NAME_MAX + 1];
1423 rval = lchown(name->path, owner, group);
1424 if (rval >= 0 || errno != ENAMETOOLONG)
1426 separate_pathname(name, buf, &newname);
1427 if (chdir(buf) == 0) {
1428 rval = lchown_path(&newname, owner, group);
1429 assert(chdir("..") == 0);
1431 free_pathname(&newname);
1436 link_path(pathname_t *name1, pathname_t *name2)
1438 char buf1[NAME_MAX + 1];
1439 char buf2[NAME_MAX + 1];
1441 pathname_t newname1;
1442 pathname_t newname2;
1445 rval = link(name1->path, name2->path);
1446 if (rval >= 0 || errno != ENAMETOOLONG)
1448 separate_pathname(name1, buf1, &newname1);
1449 separate_pathname(name2, buf2, &newname2);
1450 if (strcmp(buf1, buf2) == 0) {
1451 if (chdir(buf1) == 0) {
1452 rval = link_path(&newname1, &newname2);
1453 assert(chdir("..") == 0);
1456 if (strcmp(buf1, "..") == 0)
1458 else if (strcmp(buf2, "..") == 0)
1460 else if (strlen(buf1) == 0)
1462 else if (strlen(buf2) == 0)
1465 down1 = MAX(newname1.len, 3 + name2->len) <=
1466 MAX(3 + name1->len, newname2.len);
1468 free_pathname(&newname2);
1469 append_pathname(&newname2, "../");
1470 append_pathname(&newname2, name2->path);
1471 if (chdir(buf1) == 0) {
1472 rval = link_path(&newname1, &newname2);
1473 assert(chdir("..") == 0);
1476 free_pathname(&newname1);
1477 append_pathname(&newname1, "../");
1478 append_pathname(&newname1, name1->path);
1479 if (chdir(buf2) == 0) {
1480 rval = link_path(&newname1, &newname2);
1481 assert(chdir("..") == 0);
1485 free_pathname(&newname1);
1486 free_pathname(&newname2);
1491 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1493 char buf[NAME_MAX + 1];
1497 rval = lstat64(name->path, sbuf);
1498 if (rval >= 0 || errno != ENAMETOOLONG)
1500 separate_pathname(name, buf, &newname);
1501 if (chdir(buf) == 0) {
1502 rval = lstat64_path(&newname, sbuf);
1503 assert(chdir("..") == 0);
1505 free_pathname(&newname);
1510 make_freq_table(void)
1516 for (p = ops, f = 0; p < ops_end; p++)
1518 freq_table = malloc(f * sizeof(*freq_table));
1519 freq_table_size = f;
1520 for (p = ops, i = 0; p < ops_end; p++) {
1521 for (f = 0; f < p->freq; f++, i++)
1522 freq_table[i] = p - ops;
1527 mkdir_path(pathname_t *name, mode_t mode)
1529 char buf[NAME_MAX + 1];
1533 rval = mkdir(name->path, mode);
1534 if (rval >= 0 || errno != ENAMETOOLONG)
1536 separate_pathname(name, buf, &newname);
1537 if (chdir(buf) == 0) {
1538 rval = mkdir_path(&newname, mode);
1539 assert(chdir("..") == 0);
1541 free_pathname(&newname);
1546 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1548 char buf[NAME_MAX + 1];
1552 rval = mknod(name->path, mode, dev);
1553 if (rval >= 0 || errno != ENAMETOOLONG)
1555 separate_pathname(name, buf, &newname);
1556 if (chdir(buf) == 0) {
1557 rval = mknod_path(&newname, mode, dev);
1558 assert(chdir("..") == 0);
1560 free_pathname(&newname);
1565 namerandpad(int id, char *buf, int i)
1568 static int buckets[] =
1569 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1575 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1576 padmod = buckets[bucket] + 1 - i;
1579 padlen = (id ^ namerand) % padmod;
1581 memset(&buf[i], 'X', padlen);
1582 buf[i + padlen] = '\0';
1587 open_file_or_dir(pathname_t *name, int flags)
1591 fd = open_path(name, flags);
1594 if (fd == -1 && errno != EISDIR)
1596 /* Directories can not be opened in write mode nor direct mode. */
1597 flags &= ~(O_WRONLY | O_DIRECT);
1598 flags |= O_RDONLY | O_DIRECTORY;
1599 return open_path(name, flags);
1603 open_path(pathname_t *name, int oflag)
1605 char buf[NAME_MAX + 1];
1609 rval = open(name->path, oflag);
1610 if (rval >= 0 || errno != ENAMETOOLONG)
1612 separate_pathname(name, buf, &newname);
1613 if (chdir(buf) == 0) {
1614 rval = open_path(&newname, oflag);
1615 assert(chdir("..") == 0);
1617 free_pathname(&newname);
1622 opendir_path(pathname_t *name)
1624 char buf[NAME_MAX + 1];
1628 rval = opendir(name->path);
1629 if (rval || errno != ENAMETOOLONG)
1631 separate_pathname(name, buf, &newname);
1632 if (chdir(buf) == 0) {
1633 rval = opendir_path(&newname);
1634 assert(chdir("..") == 0);
1636 free_pathname(&newname);
1641 process_freq(char *arg)
1646 s = strchr(arg, '=');
1648 fprintf(stderr, "bad argument '%s'\n", arg);
1652 for (p = ops; p < ops_end; p++) {
1653 if (strcmp(arg, p->name) == 0) {
1658 fprintf(stderr, "can't find op type %s for -f\n", arg);
1663 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1665 char buf[NAME_MAX + 1];
1669 rval = readlink(name->path, lbuf, lbufsiz);
1670 if (rval >= 0 || errno != ENAMETOOLONG)
1672 separate_pathname(name, buf, &newname);
1673 if (chdir(buf) == 0) {
1674 rval = readlink_path(&newname, lbuf, lbufsiz);
1675 assert(chdir("..") == 0);
1677 free_pathname(&newname);
1682 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1684 char buf1[NAME_MAX + 1];
1685 char buf2[NAME_MAX + 1];
1687 pathname_t newname1;
1688 pathname_t newname2;
1692 rval = rename(name1->path, name2->path);
1694 rval = renameat2(AT_FDCWD, name1->path,
1695 AT_FDCWD, name2->path, mode);
1696 if (rval >= 0 || errno != ENAMETOOLONG)
1698 separate_pathname(name1, buf1, &newname1);
1699 separate_pathname(name2, buf2, &newname2);
1700 if (strcmp(buf1, buf2) == 0) {
1701 if (chdir(buf1) == 0) {
1702 rval = rename_path(&newname1, &newname2, mode);
1703 assert(chdir("..") == 0);
1706 if (strcmp(buf1, "..") == 0)
1708 else if (strcmp(buf2, "..") == 0)
1710 else if (strlen(buf1) == 0)
1712 else if (strlen(buf2) == 0)
1715 down1 = MAX(newname1.len, 3 + name2->len) <=
1716 MAX(3 + name1->len, newname2.len);
1718 free_pathname(&newname2);
1719 append_pathname(&newname2, "../");
1720 append_pathname(&newname2, name2->path);
1721 if (chdir(buf1) == 0) {
1722 rval = rename_path(&newname1, &newname2, mode);
1723 assert(chdir("..") == 0);
1726 free_pathname(&newname1);
1727 append_pathname(&newname1, "../");
1728 append_pathname(&newname1, name1->path);
1729 if (chdir(buf2) == 0) {
1730 rval = rename_path(&newname1, &newname2, mode);
1731 assert(chdir("..") == 0);
1735 free_pathname(&newname1);
1736 free_pathname(&newname2);
1741 rmdir_path(pathname_t *name)
1743 char buf[NAME_MAX + 1];
1747 rval = rmdir(name->path);
1748 if (rval >= 0 || errno != ENAMETOOLONG)
1750 separate_pathname(name, buf, &newname);
1751 if (chdir(buf) == 0) {
1752 rval = rmdir_path(&newname);
1753 assert(chdir("..") == 0);
1755 free_pathname(&newname);
1760 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1764 init_pathname(newname);
1765 slash = strchr(name->path, '/');
1766 if (slash == NULL) {
1771 strcpy(buf, name->path);
1773 append_pathname(newname, slash + 1);
1779 show_ops(int flag, char *lead_str)
1784 /* print in list form */
1787 for (p = ops; p < ops_end; p++) {
1788 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1789 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1790 x+=printf("%s ", p->name);
1793 } else if (flag == 0) {
1794 /* Table view style */
1796 for (f = 0, p = ops; p < ops_end; p++)
1802 for (p = ops; p < ops_end; p++) {
1803 if (flag != 0 || p->freq > 0) {
1804 if (lead_str != NULL)
1805 printf("%s", lead_str);
1806 printf("%20s %d/%d %s\n",
1807 p->name, p->freq, f,
1808 (p->iswrite == 0) ? " " : "write op");
1812 /* Command line style */
1813 if (lead_str != NULL)
1814 printf("%s", lead_str);
1815 printf ("-z -s %ld -m %d -n %lld -p %d \\\n", seed, idmodulo,
1817 for (p = ops; p < ops_end; p++)
1819 printf("-f %s=%d \\\n",p->name, p->freq);
1824 stat64_path(pathname_t *name, struct stat64 *sbuf)
1826 char buf[NAME_MAX + 1];
1830 rval = stat64(name->path, sbuf);
1831 if (rval >= 0 || errno != ENAMETOOLONG)
1833 separate_pathname(name, buf, &newname);
1834 if (chdir(buf) == 0) {
1835 rval = stat64_path(&newname, sbuf);
1836 assert(chdir("..") == 0);
1838 free_pathname(&newname);
1843 symlink_path(const char *name1, pathname_t *name)
1845 char buf[NAME_MAX + 1];
1849 if (!strcmp(name1, name->path)) {
1850 printf("yikes! %s %s\n", name1, name->path);
1854 rval = symlink(name1, name->path);
1855 if (rval >= 0 || errno != ENAMETOOLONG)
1857 separate_pathname(name, buf, &newname);
1858 if (chdir(buf) == 0) {
1859 rval = symlink_path(name1, &newname);
1860 assert(chdir("..") == 0);
1862 free_pathname(&newname);
1867 truncate64_path(pathname_t *name, off64_t length)
1869 char buf[NAME_MAX + 1];
1873 rval = truncate64(name->path, length);
1874 if (rval >= 0 || errno != ENAMETOOLONG)
1876 separate_pathname(name, buf, &newname);
1877 if (chdir(buf) == 0) {
1878 rval = truncate64_path(&newname, length);
1879 assert(chdir("..") == 0);
1881 free_pathname(&newname);
1886 unlink_path(pathname_t *name)
1888 char buf[NAME_MAX + 1];
1892 rval = unlink(name->path);
1893 if (rval >= 0 || errno != ENAMETOOLONG)
1895 separate_pathname(name, buf, &newname);
1896 if (chdir(buf) == 0) {
1897 rval = unlink_path(&newname);
1898 assert(chdir("..") == 0);
1900 free_pathname(&newname);
1907 printf("Usage: %s -H or\n", myprog);
1908 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1910 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1912 printf(" -c clean up the test directory after each run\n");
1913 printf(" -d dir specifies the base directory for operations\n");
1914 printf(" -e errtg specifies error injection stuff\n");
1915 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1916 printf(" the valid operation names are:\n");
1918 printf(" -i filenum get verbose output for this nth file object\n");
1919 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1920 printf(" *use 0 for infinite (default 1)\n");
1921 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1922 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1923 printf(" -o logfile specifies logfile name\n");
1924 printf(" -p nproc specifies the no. of processes (default 1)\n");
1925 printf(" -r specifies random name padding\n");
1926 printf(" -s seed specifies the seed for the random generator (default random)\n");
1927 printf(" -v specifies verbose mode\n");
1928 printf(" -w zeros frequencies of non-write operations\n");
1929 printf(" -x cmd execute command in the middle of operations\n");
1930 printf(" -z zeros frequencies of all operations\n");
1931 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1932 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1933 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1934 printf(" -H prints usage and exits\n");
1942 for (p = ops; p < ops_end; p++) {
1953 for (p = ops; p < ops_end; p++)
1957 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1959 opty_t btrfs_ops[] = {
1966 non_btrfs_freq(const char *path)
1969 #ifdef HAVE_BTRFSUTIL_H
1970 enum btrfs_util_error e;
1972 e = btrfs_util_is_subvolume(path);
1973 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1976 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++)
1977 ops[btrfs_ops[i]].freq = 0;
1980 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1983 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1984 verifiable_log ? -1: (long)s->st_ino,
1985 (long)s->st_nlink, s->st_uid, s->st_gid,
1986 (long long) s->st_blocks, (long long) s->st_size);
1990 afsync_f(opnum_t opno, long r)
1998 struct iocb *iocbs[] = { &iocb };
1999 struct io_event event;
2002 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
2004 printf("%d/%lld: afsync - no filename\n", procid, opno);
2008 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
2009 e = fd < 0 ? errno : 0;
2013 printf("%d/%lld: afsync - open %s failed %d\n",
2014 procid, opno, f.path, e);
2019 io_prep_fsync(&iocb, fd);
2020 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2022 printf("%d/%lld: afsync - io_submit %s %d\n",
2023 procid, opno, f.path, e);
2028 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2030 printf("%d/%lld: afsync - io_getevents failed %d\n",
2039 printf("%d/%lld: afsync %s %d\n", procid, opno, f.path, e);
2046 allocsp_f(opnum_t opno, long r)
2051 struct xfs_flock64 fl;
2059 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2061 printf("%d/%lld: allocsp - no filename\n", procid, opno);
2065 fd = open_path(&f, O_RDWR);
2066 e = fd < 0 ? errno : 0;
2070 printf("%d/%lld: allocsp - open %s failed %d\n",
2071 procid, opno, f.path, e);
2075 if (fstat64(fd, &stb) < 0) {
2077 printf("%d/%lld: allocsp - fstat64 %s failed %d\n",
2078 procid, opno, f.path, errno);
2083 inode_info(st, sizeof(st), &stb, v);
2084 lr = ((int64_t)random() << 32) + random();
2085 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2087 fl.l_whence = SEEK_SET;
2090 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2092 printf("%d/%lld: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2093 procid, opno, f.path, st, (long long)off, e);
2101 do_aio_rw(opnum_t opno, long r, int flags)
2105 struct dioattr diob;
2117 struct io_event event;
2118 struct iocb *iocbs[] = { &iocb };
2119 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2122 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2124 printf("%d/%lld: do_aio_rw - no filename\n", procid, opno);
2127 fd = open_path(&f, flags|O_DIRECT);
2128 e = fd < 0 ? errno : 0;
2132 printf("%d/%lld: do_aio_rw - open %s failed %d\n",
2133 procid, opno, f.path, e);
2136 if (fstat64(fd, &stb) < 0) {
2138 printf("%d/%lld: do_aio_rw - fstat64 %s failed %d\n",
2139 procid, opno, f.path, errno);
2142 inode_info(st, sizeof(st), &stb, v);
2143 if (!iswrite && stb.st_size == 0) {
2145 printf("%d/%lld: do_aio_rw - %s%s zero size\n", procid, opno,
2149 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2152 "%d/%lld: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2153 " fallback to stat()\n",
2154 procid, opno, f.path, st, errno);
2155 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2156 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
2158 dio_env = getenv("XFS_DIO_MIN");
2160 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2161 align = (int64_t)diob.d_miniosz;
2162 lr = ((int64_t)random() << 32) + random();
2163 len = (random() % FILELEN_MAX) + 1;
2164 len -= (len % align);
2167 else if (len > diob.d_maxiosz)
2168 len = diob.d_maxiosz;
2169 buf = memalign(diob.d_mem, len);
2172 printf("%d/%lld: do_aio_rw - memalign failed\n",
2178 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2179 off -= (off % align);
2181 memset(buf, nameseq & 0xff, len);
2182 io_prep_pwrite(&iocb, fd, buf, len, off);
2184 off = (off64_t)(lr % stb.st_size);
2185 off -= (off % align);
2186 io_prep_pread(&iocb, fd, buf, len, off);
2188 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2190 printf("%d/%lld: %s - io_submit failed %d\n",
2191 procid, opno, iswrite ? "awrite" : "aread", e);
2194 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2196 printf("%d/%lld: %s - io_getevents failed %d\n",
2197 procid, opno, iswrite ? "awrite" : "aread", e);
2201 e = event.res != len ? event.res2 : 0;
2203 printf("%d/%lld: %s %s%s [%lld,%d] %d\n",
2204 procid, opno, iswrite ? "awrite" : "aread",
2205 f.path, st, (long long)off, (int)len, e);
2217 do_uring_rw(opnum_t opno, long r, int flags)
2229 struct io_uring_sqe *sqe;
2230 struct io_uring_cqe *cqe;
2232 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2238 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2240 printf("%d/%lld: do_uring_rw - no filename\n", procid, opno);
2243 fd = open_path(&f, flags);
2244 e = fd < 0 ? errno : 0;
2248 printf("%d/%lld: do_uring_rw - open %s failed %d\n",
2249 procid, opno, f.path, e);
2252 if (fstat64(fd, &stb) < 0) {
2254 printf("%d/%lld: do_uring_rw - fstat64 %s failed %d\n",
2255 procid, opno, f.path, errno);
2258 inode_info(st, sizeof(st), &stb, v);
2259 if (!iswrite && stb.st_size == 0) {
2261 printf("%d/%lld: do_uring_rw - %s%s zero size\n", procid, opno,
2265 sqe = io_uring_get_sqe(&ring);
2268 printf("%d/%lld: do_uring_rw - io_uring_get_sqe failed\n",
2272 lr = ((int64_t)random() << 32) + random();
2273 len = (random() % FILELEN_MAX) + 1;
2277 printf("%d/%lld: do_uring_rw - malloc failed\n",
2281 iovec.iov_base = buf;
2282 iovec.iov_len = len;
2284 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2286 memset(buf, nameseq & 0xff, len);
2287 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2289 off = (off64_t)(lr % stb.st_size);
2290 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2293 if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2295 printf("%d/%lld: %s - io_uring_submit failed %d\n", procid, opno,
2296 iswrite ? "uring_write" : "uring_read", e);
2299 if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2301 printf("%d/%lld: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2302 iswrite ? "uring_write" : "uring_read", e);
2306 printf("%d/%lld: %s %s%s [%lld, %d(res=%d)] %d\n",
2307 procid, opno, iswrite ? "uring_write" : "uring_read",
2308 f.path, st, (long long)off, (int)len, cqe->res, e);
2309 io_uring_cqe_seen(&ring, cqe);
2321 aread_f(opnum_t opno, long r)
2324 do_aio_rw(opno, r, O_RDONLY);
2329 attr_remove_f(opnum_t opno, long r)
2334 char buf[XATTR_LIST_MAX];
2343 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2344 append_pathname(&f, ".");
2345 e = attr_list_path(&f, buf, sizeof(buf));
2348 total = attr_list_count(buf, e);
2351 printf("%d/%lld: attr_remove - no attrs for %s\n",
2352 procid, opno, f.path);
2357 which = (int)(random() % total);
2361 while (bufname < bufend) {
2367 bufname += strlen(bufname) + 1;
2369 if (aname == NULL) {
2372 "%d/%lld: attr_remove - name %d not found at %s\n",
2373 procid, opno, which, f.path);
2377 if (attr_remove_path(&f, aname) < 0)
2383 printf("%d/%lld: attr_remove %s %s %d\n",
2384 procid, opno, f.path, aname, e);
2389 attr_set_f(opnum_t opno, long r)
2396 static int lengths[] = { 10, 100, 1000, 10000 };
2401 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2402 append_pathname(&f, ".");
2403 sprintf(aname, "a%x", nameseq++);
2404 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2405 len = (int)(random() % lengths[li]);
2409 memset(aval, nameseq & 0xff, len);
2410 if (attr_set_path(&f, aname, aval, len) < 0)
2417 printf("%d/%lld: attr_set %s %s %d\n", procid, opno, f.path,
2423 awrite_f(opnum_t opno, long r)
2426 do_aio_rw(opno, r, O_WRONLY);
2431 bulkstat_f(opnum_t opno, long r)
2437 struct xfs_bstat *t;
2439 struct xfs_fsop_bulkreq bsr;
2442 nent = (r % 999) + 2;
2443 t = malloc(nent * sizeof(*t));
2444 fd = open(".", O_RDONLY);
2452 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2456 printf("%d/%lld: bulkstat nent %d total %lld\n",
2457 procid, opno, nent, (long long)total);
2462 bulkstat1_f(opnum_t opno, long r)
2472 struct xfs_fsop_bulkreq bsr;
2475 good = random() & 1;
2477 /* use an inode we know exists */
2479 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2480 append_pathname(&f, ".");
2481 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2486 * pick a random inode
2488 * note this can generate kernel warning messages
2489 * since bulkstat_one will read the disk block that
2490 * would contain a given inode even if that disk
2491 * block doesn't contain inodes.
2493 * this is detected later, but not until after the
2494 * warning is displayed.
2496 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2502 fd = open(".", O_RDONLY);
2508 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2510 printf("%d/%lld: bulkstat1 %s ino %lld %d\n",
2511 procid, opno, good?"real":"random",
2512 verifiable_log ? -1LL : (long long)ino, e);
2517 chown_f(opnum_t opno, long r)
2527 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2528 append_pathname(&f, ".");
2529 u = (uid_t)random();
2530 g = (gid_t)random();
2531 nbits = (int)(random() % idmodulo);
2532 u &= (1 << nbits) - 1;
2533 g &= (1 << nbits) - 1;
2534 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2537 printf("%d/%lld: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2541 /* reflink some arbitrary range of f1 to f2. */
2548 struct file_clone_range fcr;
2549 struct pathname fpath1;
2550 struct pathname fpath2;
2551 struct stat64 stat1;
2552 struct stat64 stat2;
2553 char inoinfo1[1024];
2554 char inoinfo2[1024];
2568 init_pathname(&fpath1);
2569 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2571 printf("%d/%lld: clonerange read - no filename\n",
2576 init_pathname(&fpath2);
2577 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2579 printf("%d/%lld: clonerange write - no filename\n",
2585 fd1 = open_path(&fpath1, O_RDONLY);
2586 e = fd1 < 0 ? errno : 0;
2590 printf("%d/%lld: clonerange read - open %s failed %d\n",
2591 procid, opno, fpath1.path, e);
2595 fd2 = open_path(&fpath2, O_WRONLY);
2596 e = fd2 < 0 ? errno : 0;
2600 printf("%d/%lld: clonerange write - open %s failed %d\n",
2601 procid, opno, fpath2.path, e);
2605 /* Get file stats */
2606 if (fstat64(fd1, &stat1) < 0) {
2608 printf("%d/%lld: clonerange read - fstat64 %s failed %d\n",
2609 procid, opno, fpath1.path, errno);
2612 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2614 if (fstat64(fd2, &stat2) < 0) {
2616 printf("%d/%lld: clonerange write - fstat64 %s failed %d\n",
2617 procid, opno, fpath2.path, errno);
2620 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2622 /* Calculate offsets */
2623 len = (random() % FILELEN_MAX) + 1;
2624 len = rounddown_64(len, stat1.st_blksize);
2626 len = stat1.st_blksize;
2627 if (len > stat1.st_size)
2628 len = stat1.st_size;
2630 lr = ((int64_t)random() << 32) + random();
2631 if (stat1.st_size == len)
2634 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2636 off1 = rounddown_64(off1, stat1.st_blksize);
2639 * If srcfile == destfile, randomly generate destination ranges
2640 * until we find one that doesn't overlap the source range.
2642 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2644 lr = ((int64_t)random() << 32) + random();
2645 off2 = (off64_t)(lr % max_off2);
2647 off2 = rounddown_64(off2, stat2.st_blksize);
2648 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2650 /* Clone data blocks */
2652 fcr.src_offset = off1;
2653 fcr.src_length = len;
2654 fcr.dest_offset = off2;
2656 ret = ioctl(fd2, FICLONERANGE, &fcr);
2657 e = ret < 0 ? errno : 0;
2659 printf("%d/%lld: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2661 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2662 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2665 printf(" error %d", e);
2674 free_pathname(&fpath2);
2676 free_pathname(&fpath1);
2680 /* copy some arbitrary range of f1 to f2. */
2686 #ifdef HAVE_COPY_FILE_RANGE
2687 struct pathname fpath1;
2688 struct pathname fpath2;
2689 struct stat64 stat1;
2690 struct stat64 stat2;
2691 char inoinfo1[1024];
2692 char inoinfo2[1024];
2710 init_pathname(&fpath1);
2711 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2713 printf("%d/%lld: copyrange read - no filename\n",
2718 init_pathname(&fpath2);
2719 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2721 printf("%d/%lld: copyrange write - no filename\n",
2727 fd1 = open_path(&fpath1, O_RDONLY);
2728 e = fd1 < 0 ? errno : 0;
2732 printf("%d/%lld: copyrange read - open %s failed %d\n",
2733 procid, opno, fpath1.path, e);
2737 fd2 = open_path(&fpath2, O_WRONLY);
2738 e = fd2 < 0 ? errno : 0;
2742 printf("%d/%lld: copyrange write - open %s failed %d\n",
2743 procid, opno, fpath2.path, e);
2747 /* Get file stats */
2748 if (fstat64(fd1, &stat1) < 0) {
2750 printf("%d/%lld: copyrange read - fstat64 %s failed %d\n",
2751 procid, opno, fpath1.path, errno);
2754 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2756 if (fstat64(fd2, &stat2) < 0) {
2758 printf("%d/%lld: copyrange write - fstat64 %s failed %d\n",
2759 procid, opno, fpath2.path, errno);
2762 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2764 /* Calculate offsets */
2765 len = (random() % FILELEN_MAX) + 1;
2767 len = stat1.st_blksize;
2768 if (len > stat1.st_size)
2769 len = stat1.st_size;
2771 lr = ((int64_t)random() << 32) + random();
2772 if (stat1.st_size == len)
2775 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2779 * If srcfile == destfile, randomly generate destination ranges
2780 * until we find one that doesn't overlap the source range.
2782 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2784 lr = ((int64_t)random() << 32) + random();
2785 off2 = (off64_t)(lr % max_off2);
2787 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2790 * Since len, off1 and off2 will be changed later, preserve their
2798 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2801 if (errno != EAGAIN || tries++ >= 300)
2803 } else if (ret > len || ret == 0)
2808 e = ret < 0 ? errno : 0;
2810 printf("%d/%lld: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2812 fpath1.path, inoinfo1,
2813 (long long)offset1, (long long)length,
2814 fpath2.path, inoinfo2,
2815 (long long)offset2, (long long)length);
2818 printf(" error %d", e);
2819 else if (len && ret > len)
2820 printf(" asked for %lld, copied %lld??\n",
2821 (long long)len, (long long)ret);
2830 free_pathname(&fpath2);
2832 free_pathname(&fpath1);
2836 /* dedupe some arbitrary range of f1 to f2...fn. */
2842 #ifdef FIDEDUPERANGE
2843 #define INFO_SZ 1024
2844 struct file_dedupe_range *fdr;
2845 struct pathname *fpath;
2846 struct stat64 *stat;
2858 if (flist[FT_REG].nfiles < 2)
2861 /* Pick somewhere between 2 and 128 files. */
2863 nr = random() % (flist[FT_REG].nfiles + 1);
2864 } while (nr < 2 || nr > 128);
2867 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2868 sizeof(struct file_dedupe_range));
2870 printf("%d/%lld: line %d error %d\n",
2871 procid, opno, __LINE__, errno);
2874 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2875 sizeof(struct file_dedupe_range)));
2877 fpath = calloc(nr, sizeof(struct pathname));
2879 printf("%d/%lld: line %d error %d\n",
2880 procid, opno, __LINE__, errno);
2884 stat = calloc(nr, sizeof(struct stat64));
2886 printf("%d/%lld: line %d error %d\n",
2887 procid, opno, __LINE__, errno);
2891 info = calloc(nr, INFO_SZ);
2893 printf("%d/%lld: line %d error %d\n",
2894 procid, opno, __LINE__, errno);
2898 off = calloc(nr, sizeof(off64_t));
2900 printf("%d/%lld: line %d error %d\n",
2901 procid, opno, __LINE__, errno);
2905 v = calloc(nr, sizeof(int));
2907 printf("%d/%lld: line %d error %d\n",
2908 procid, opno, __LINE__, errno);
2911 fd = calloc(nr, sizeof(int));
2913 printf("%d/%lld: line %d error %d\n",
2914 procid, opno, __LINE__, errno);
2917 memset(fd, 0xFF, nr * sizeof(int));
2919 /* Get paths for all files */
2920 for (i = 0; i < nr; i++)
2921 init_pathname(&fpath[i]);
2923 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2925 printf("%d/%lld: deduperange read - no filename\n",
2930 for (i = 1; i < nr; i++) {
2931 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2933 printf("%d/%lld: deduperange write - no filename\n",
2940 fd[0] = open_path(&fpath[0], O_RDONLY);
2941 e = fd[0] < 0 ? errno : 0;
2945 printf("%d/%lld: deduperange read - open %s failed %d\n",
2946 procid, opno, fpath[0].path, e);
2950 for (i = 1; i < nr; i++) {
2951 fd[i] = open_path(&fpath[i], O_WRONLY);
2952 e = fd[i] < 0 ? errno : 0;
2956 printf("%d/%lld: deduperange write - open %s failed %d\n",
2957 procid, opno, fpath[i].path, e);
2962 /* Get file stats */
2963 if (fstat64(fd[0], &stat[0]) < 0) {
2965 printf("%d/%lld: deduperange read - fstat64 %s failed %d\n",
2966 procid, opno, fpath[0].path, errno);
2970 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2972 for (i = 1; i < nr; i++) {
2973 if (fstat64(fd[i], &stat[i]) < 0) {
2975 printf("%d/%lld: deduperange write - fstat64 %s failed %d\n",
2976 procid, opno, fpath[i].path, errno);
2979 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2982 /* Never try to dedupe more than half of the src file. */
2983 len = (random() % FILELEN_MAX) + 1;
2984 len = rounddown_64(len, stat[0].st_blksize);
2986 len = stat[0].st_blksize / 2;
2987 if (len > stat[0].st_size / 2)
2988 len = stat[0].st_size / 2;
2990 /* Calculate offsets */
2991 lr = ((int64_t)random() << 32) + random();
2992 if (stat[0].st_size == len)
2995 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2997 off[0] = rounddown_64(off[0], stat[0].st_blksize);
3000 * If srcfile == destfile[i], randomly generate destination ranges
3001 * until we find one that doesn't overlap the source range.
3003 for (i = 1; i < nr; i++) {
3007 lr = ((int64_t)random() << 32) + random();
3008 if (stat[i].st_size <= len)
3011 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
3013 off[i] = rounddown_64(off[i], stat[i].st_blksize);
3014 } while (stat[0].st_ino == stat[i].st_ino &&
3015 llabs(off[i] - off[0]) < len &&
3019 /* Clone data blocks */
3020 fdr->src_offset = off[0];
3021 fdr->src_length = len;
3022 fdr->dest_count = nr - 1;
3023 for (i = 1; i < nr; i++) {
3024 fdr->info[i - 1].dest_fd = fd[i];
3025 fdr->info[i - 1].dest_offset = off[i];
3028 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3029 e = ret < 0 ? errno : 0;
3031 printf("%d/%lld: deduperange from %s%s [%lld,%lld]",
3033 fpath[0].path, &info[0], (long long)off[0],
3036 printf(" error %d", e);
3042 for (i = 1; i < nr; i++) {
3043 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3045 printf("%d/%lld: ...to %s%s [%lld,%lld]",
3047 fpath[i].path, &info[i * INFO_SZ],
3048 (long long)off[i], (long long)len);
3049 if (fdr->info[i - 1].status < 0)
3050 printf(" error %d", e);
3051 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3052 printf(" %llu bytes deduplicated",
3053 fdr->info[i - 1].bytes_deduped);
3054 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3055 printf(" differed");
3061 for (i = 0; i < nr; i++)
3065 for (i = 0; i < nr; i++)
3066 free_pathname(&fpath[i]);
3085 setxattr_f(opnum_t opno, long r)
3087 #ifdef XFS_XFLAG_EXTSIZE
3097 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3098 append_pathname(&f, ".");
3099 fd = open_path(&f, O_RDWR);
3100 e = fd < 0 ? errno : 0;
3105 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3106 nbits = (int)(random() % e);
3107 p &= (1 << nbits) - 1;
3109 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3111 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3114 printf("%d/%lld: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3121 splice_f(opnum_t opno, long r)
3123 struct pathname fpath1;
3124 struct pathname fpath2;
3125 struct stat64 stat1;
3126 struct stat64 stat2;
3127 char inoinfo1[1024];
3128 char inoinfo2[1024];
3132 loff_t offset1, offset2;
3139 ssize_t ret1 = 0, ret2 = 0;
3145 init_pathname(&fpath1);
3146 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3148 printf("%d/%lld: splice read - no filename\n",
3153 init_pathname(&fpath2);
3154 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3156 printf("%d/%lld: splice write - no filename\n",
3162 fd1 = open_path(&fpath1, O_RDONLY);
3163 e = fd1 < 0 ? errno : 0;
3167 printf("%d/%lld: splice read - open %s failed %d\n",
3168 procid, opno, fpath1.path, e);
3172 fd2 = open_path(&fpath2, O_WRONLY);
3173 e = fd2 < 0 ? errno : 0;
3177 printf("%d/%lld: splice write - open %s failed %d\n",
3178 procid, opno, fpath2.path, e);
3182 /* Get file stats */
3183 if (fstat64(fd1, &stat1) < 0) {
3185 printf("%d/%lld: splice read - fstat64 %s failed %d\n",
3186 procid, opno, fpath1.path, errno);
3189 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3191 if (fstat64(fd2, &stat2) < 0) {
3193 printf("%d/%lld: splice write - fstat64 %s failed %d\n",
3194 procid, opno, fpath2.path, errno);
3197 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3199 /* Calculate offsets */
3200 len = (random() % FILELEN_MAX) + 1;
3202 len = stat1.st_blksize;
3203 if (len > stat1.st_size)
3204 len = stat1.st_size;
3206 lr = ((int64_t)random() << 32) + random();
3207 if (stat1.st_size == len)
3210 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3214 * splice can overlap write, so the offset of the target file can be
3215 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3216 * past the current dest file EOF
3218 lr = ((int64_t)random() << 32) + random();
3219 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3222 * Since len, off1 and off2 will be changed later, preserve their
3229 /* Pipe initialize */
3230 if (pipe(filedes) < 0) {
3232 printf("%d/%lld: splice - pipe failed %d\n",
3233 procid, opno, errno);
3241 /* move to pipe buffer */
3242 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3248 /* move from pipe buffer to dst file */
3250 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3263 if (ret1 < 0 || ret2 < 0)
3268 printf("%d/%lld: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3270 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3271 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3273 if (length && length > total)
3274 printf(" asked for %lld, spliced %lld??\n",
3275 (long long)length, (long long)total);
3286 free_pathname(&fpath2);
3288 free_pathname(&fpath1);
3292 creat_f(opnum_t opno, long r)
3307 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3312 e1 = (random() % 100);
3313 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3315 if (type == FT_RTF) /* rt always gets an extsize */
3316 extsize = (random() % 10) + 1;
3317 else if (e1 < 10) /* one-in-ten get an extsize */
3318 extsize = random() % 1024;
3322 e = generate_fname(fep, type, &f, &id, &v);
3326 (void)fent_to_name(&f, fep);
3327 printf("%d/%lld: creat - no filename from %s\n",
3328 procid, opno, f.path);
3333 fd = creat_path(&f, 0666);
3334 e = fd < 0 ? errno : 0;
3339 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3340 if (type == FT_RTF) {
3341 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3342 a.fsx_extsize = extsize *
3343 geom.rtextsize * geom.blocksize;
3345 } else if (extsize) {
3346 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3347 a.fsx_extsize = extsize * geom.blocksize;
3350 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3353 add_to_flist(type, id, parid, 0);
3357 printf("%d/%lld: creat %s x:%d %d %d\n", procid, opno, f.path,
3358 extsize ? a.fsx_extsize : 0, e, e1);
3359 printf("%d/%lld: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3365 dread_f(opnum_t opno, long r)
3369 struct dioattr diob;
3382 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3384 printf("%d/%lld: dread - no filename\n", procid, opno);
3388 fd = open_path(&f, O_RDONLY|O_DIRECT);
3389 e = fd < 0 ? errno : 0;
3393 printf("%d/%lld: dread - open %s failed %d\n",
3394 procid, opno, f.path, e);
3398 if (fstat64(fd, &stb) < 0) {
3400 printf("%d/%lld: dread - fstat64 %s failed %d\n",
3401 procid, opno, f.path, errno);
3406 inode_info(st, sizeof(st), &stb, v);
3407 if (stb.st_size == 0) {
3409 printf("%d/%lld: dread - %s%s zero size\n", procid, opno,
3415 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3418 "%d/%lld: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3419 " fallback to stat()\n",
3420 procid, opno, f.path, st, errno);
3421 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3422 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
3425 dio_env = getenv("XFS_DIO_MIN");
3427 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3429 align = (int64_t)diob.d_miniosz;
3430 lr = ((int64_t)random() << 32) + random();
3431 off = (off64_t)(lr % stb.st_size);
3432 off -= (off % align);
3433 lseek64(fd, off, SEEK_SET);
3434 len = (random() % FILELEN_MAX) + 1;
3435 len -= (len % align);
3438 else if (len > diob.d_maxiosz)
3439 len = diob.d_maxiosz;
3440 buf = memalign(diob.d_mem, len);
3441 e = read(fd, buf, len) < 0 ? errno : 0;
3444 printf("%d/%lld: dread %s%s [%lld,%d] %d\n",
3445 procid, opno, f.path, st, (long long)off, (int)len, e);
3451 dwrite_f(opnum_t opno, long r)
3455 struct dioattr diob;
3468 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3470 printf("%d/%lld: dwrite - no filename\n", procid, opno);
3474 fd = open_path(&f, O_WRONLY|O_DIRECT);
3475 e = fd < 0 ? errno : 0;
3479 printf("%d/%lld: dwrite - open %s failed %d\n",
3480 procid, opno, f.path, e);
3484 if (fstat64(fd, &stb) < 0) {
3486 printf("%d/%lld: dwrite - fstat64 %s failed %d\n",
3487 procid, opno, f.path, errno);
3492 inode_info(st, sizeof(st), &stb, v);
3493 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3495 printf("%d/%lld: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3496 " %s%s return %d, fallback to stat()\n",
3497 procid, opno, f.path, st, errno);
3498 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3499 diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
3502 dio_env = getenv("XFS_DIO_MIN");
3504 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3506 align = (int64_t)diob.d_miniosz;
3507 lr = ((int64_t)random() << 32) + random();
3508 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3509 off -= (off % align);
3510 lseek64(fd, off, SEEK_SET);
3511 len = (random() % FILELEN_MAX) + 1;
3512 len -= (len % align);
3515 else if (len > diob.d_maxiosz)
3516 len = diob.d_maxiosz;
3517 buf = memalign(diob.d_mem, len);
3519 lseek64(fd, off, SEEK_SET);
3520 memset(buf, nameseq & 0xff, len);
3521 e = write(fd, buf, len) < 0 ? errno : 0;
3524 printf("%d/%lld: dwrite %s%s [%lld,%d] %d\n",
3525 procid, opno, f.path, st, (long long)off, (int)len, e);
3531 #ifdef HAVE_LINUX_FALLOC_H
3532 struct print_flags falloc_flags [] = {
3533 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3534 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3535 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3536 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3537 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3538 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3542 #define translate_falloc_flags(mode) \
3543 ({translate_flags(mode, "|", falloc_flags);})
3547 do_fallocate(opnum_t opno, long r, int mode)
3549 #ifdef HAVE_LINUX_FALLOC_H
3561 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3563 printf("%d/%lld: do_fallocate - no filename\n", procid, opno);
3567 fd = open_path(&f, O_RDWR);
3570 printf("%d/%lld: do_fallocate - open %s failed %d\n",
3571 procid, opno, f.path, errno);
3576 if (fstat64(fd, &stb) < 0) {
3578 printf("%d/%lld: do_fallocate - fstat64 %s failed %d\n",
3579 procid, opno, f.path, errno);
3584 inode_info(st, sizeof(st), &stb, v);
3585 lr = ((int64_t)random() << 32) + random();
3586 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3588 len = (off64_t)(random() % (1024 * 1024));
3590 * Collapse/insert range requires off and len to be block aligned,
3591 * make it more likely to be the case.
3593 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3595 off = roundup_64(off, stb.st_blksize);
3596 len = roundup_64(len, stb.st_blksize);
3598 mode |= FALLOC_FL_KEEP_SIZE & random();
3599 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3601 printf("%d/%lld: fallocate(%s) %s %st %lld %lld %d\n",
3602 procid, opno, translate_falloc_flags(mode),
3603 f.path, st, (long long)off, (long long)len, e);
3610 fallocate_f(opnum_t opno, long r)
3612 #ifdef HAVE_LINUX_FALLOC_H
3613 do_fallocate(opno, r, 0);
3618 fdatasync_f(opnum_t opno, long r)
3626 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3628 printf("%d/%lld: fdatasync - no filename\n",
3633 fd = open_path(&f, O_WRONLY);
3634 e = fd < 0 ? errno : 0;
3638 printf("%d/%lld: fdatasync - open %s failed %d\n",
3639 procid, opno, f.path, e);
3643 e = fdatasync(fd) < 0 ? errno : 0;
3645 printf("%d/%lld: fdatasync %s %d\n", procid, opno, f.path, e);
3650 #ifdef HAVE_LINUX_FIEMAP_H
3651 struct print_flags fiemap_flags[] = {
3652 { FIEMAP_FLAG_SYNC, "SYNC"},
3653 { FIEMAP_FLAG_XATTR, "XATTR"},
3657 #define translate_fiemap_flags(mode) \
3658 ({translate_flags(mode, "|", fiemap_flags);})
3662 fiemap_f(opnum_t opno, long r)
3664 #ifdef HAVE_LINUX_FIEMAP_H
3674 struct fiemap *fiemap;
3677 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3679 printf("%d/%lld: fiemap - no filename\n", procid, opno);
3683 fd = open_path(&f, O_RDWR);
3684 e = fd < 0 ? errno : 0;
3688 printf("%d/%lld: fiemap - open %s failed %d\n",
3689 procid, opno, f.path, e);
3693 if (fstat64(fd, &stb) < 0) {
3695 printf("%d/%lld: fiemap - fstat64 %s failed %d\n",
3696 procid, opno, f.path, errno);
3701 inode_info(st, sizeof(st), &stb, v);
3702 blocks_to_map = random() & 0xffff;
3703 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3704 (blocks_to_map * sizeof(struct fiemap_extent)));
3707 printf("%d/%lld: malloc failed \n", procid, opno);
3712 lr = ((int64_t)random() << 32) + random();
3713 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3715 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3716 fiemap->fm_extent_count = blocks_to_map;
3717 fiemap->fm_mapped_extents = random() & 0xffff;
3718 fiemap->fm_start = off;
3719 fiemap->fm_length = ((int64_t)random() << 32) + random();
3721 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3723 printf("%d/%lld: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3724 procid, opno, f.path, st, (long long)fiemap->fm_start,
3725 (long long) fiemap->fm_length,
3726 translate_fiemap_flags(fiemap->fm_flags), e);
3734 freesp_f(opnum_t opno, long r)
3739 struct xfs_flock64 fl;
3747 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3749 printf("%d/%lld: freesp - no filename\n", procid, opno);
3753 fd = open_path(&f, O_RDWR);
3754 e = fd < 0 ? errno : 0;
3758 printf("%d/%lld: freesp - open %s failed %d\n",
3759 procid, opno, f.path, e);
3763 if (fstat64(fd, &stb) < 0) {
3765 printf("%d/%lld: freesp - fstat64 %s failed %d\n",
3766 procid, opno, f.path, errno);
3771 inode_info(st, sizeof(st), &stb, v);
3772 lr = ((int64_t)random() << 32) + random();
3773 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3775 fl.l_whence = SEEK_SET;
3778 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3780 printf("%d/%lld: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3781 procid, opno, f.path, st, (long long)off, e);
3787 fsync_f(opnum_t opno, long r)
3795 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3797 printf("%d/%lld: fsync - no filename\n", procid, opno);
3801 fd = open_file_or_dir(&f, O_WRONLY);
3802 e = fd < 0 ? errno : 0;
3806 printf("%d/%lld: fsync - open %s failed %d\n",
3807 procid, opno, f.path, e);
3811 e = fsync(fd) < 0 ? errno : 0;
3813 printf("%d/%lld: fsync %s %d\n", procid, opno, f.path, e);
3819 gen_random_string(int len)
3821 static const char charset[] = "0123456789"
3822 "abcdefghijklmnopqrstuvwxyz"
3823 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3834 for (i = 0; i < len; i++)
3835 s[i] = charset[random() % sizeof(charset)];
3841 getattr_f(opnum_t opno, long r)
3850 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3851 append_pathname(&f, ".");
3852 fd = open_path(&f, O_RDWR);
3853 e = fd < 0 ? errno : 0;
3856 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3858 printf("%d/%lld: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3864 getdents_f(opnum_t opno, long r)
3871 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3872 append_pathname(&f, ".");
3873 dir = opendir_path(&f);
3877 printf("%d/%lld: getdents - can't open %s\n",
3878 procid, opno, f.path);
3882 while (readdir64(dir) != NULL)
3885 printf("%d/%lld: getdents %s 0\n", procid, opno, f.path);
3891 getfattr_f(opnum_t opno, long r)
3897 char name[XATTR_NAME_BUF_SIZE];
3903 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3905 printf("%d/%lld: getfattr - no filename\n", procid, opno);
3911 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3912 * to read a xattr that doesn't exist (fgetxattr should fail with
3913 * errno set to ENOATTR (61) in this case).
3915 if (fep->xattr_counter > 0)
3916 xattr_num = (random() % fep->xattr_counter) + 1;
3920 e = generate_xattr_name(xattr_num, name, sizeof(name));
3922 printf("%d/%lld: getfattr - file %s failed to generate xattr name: %d\n",
3923 procid, opno, f.path, e);
3927 value_len = getxattr(f.path, name, NULL, 0);
3928 if (value_len < 0) {
3930 printf("%d/%lld: getfattr file %s name %s failed %d\n",
3931 procid, opno, f.path, name, errno);
3935 /* A xattr without value.*/
3936 if (value_len == 0) {
3941 value = malloc(value_len);
3944 printf("%d/%lld: getfattr file %s failed to allocate buffer with %d bytes\n",
3945 procid, opno, f.path, value_len);
3949 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3952 printf("%d/%lld: getfattr file %s name %s value length %d %d\n",
3953 procid, opno, f.path, name, value_len, e);
3960 link_f(opnum_t opno, long r)
3974 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3976 printf("%d/%lld: link - no file\n", procid, opno);
3980 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3986 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3990 (void)fent_to_name(&l, fep);
3991 printf("%d/%lld: link - no filename from %s\n",
3992 procid, opno, l.path);
3998 e = link_path(&f, &l) < 0 ? errno : 0;
4001 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
4003 printf("%d/%lld: link %s %s %d\n", procid, opno, f.path, l.path,
4005 printf("%d/%lld: link add id=%d,parent=%d\n", procid, opno, id, parid);
4012 listfattr_f(opnum_t opno, long r)
4018 char *buffer = NULL;
4022 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4024 printf("%d/%lld: listfattr - no filename\n", procid, opno);
4029 e = listxattr(f.path, NULL, 0);
4032 printf("%d/%lld: listfattr %s failed %d\n",
4033 procid, opno, f.path, errno);
4037 if (buffer_len == 0) {
4039 printf("%d/%lld: listfattr %s - has no extended attributes\n",
4040 procid, opno, f.path);
4044 buffer = malloc(buffer_len);
4047 printf("%d/%lld: listfattr %s failed to allocate buffer with %d bytes\n",
4048 procid, opno, f.path, buffer_len);
4052 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4054 printf("%d/%lld: listfattr %s buffer length %d %d\n",
4055 procid, opno, f.path, buffer_len, e);
4062 mkdir_f(opnum_t opno, long r)
4072 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4077 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4081 (void)fent_to_name(&f, fep);
4082 printf("%d/%lld: mkdir - no filename from %s\n",
4083 procid, opno, f.path);
4088 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4091 add_to_flist(FT_DIR, id, parid, 0);
4093 printf("%d/%lld: mkdir %s %d\n", procid, opno, f.path, e);
4094 printf("%d/%lld: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4100 mknod_f(opnum_t opno, long r)
4110 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4115 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4119 (void)fent_to_name(&f, fep);
4120 printf("%d/%lld: mknod - no filename from %s\n",
4121 procid, opno, f.path);
4126 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4129 add_to_flist(FT_DEV, id, parid, 0);
4131 printf("%d/%lld: mknod %s %d\n", procid, opno, f.path, e);
4132 printf("%d/%lld: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4137 #ifdef HAVE_SYS_MMAN_H
4138 struct print_flags mmap_flags[] = {
4139 { MAP_SHARED, "SHARED"},
4140 { MAP_PRIVATE, "PRIVATE"},
4144 #define translate_mmap_flags(flags) \
4145 ({translate_flags(flags, "|", mmap_flags);})
4149 do_mmap(opnum_t opno, long r, int prot)
4151 #ifdef HAVE_SYS_MMAN_H
4163 sigjmp_buf sigbus_jmpbuf;
4166 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4168 printf("%d/%lld: do_mmap - no filename\n", procid, opno);
4172 fd = open_path(&f, O_RDWR);
4173 e = fd < 0 ? errno : 0;
4177 printf("%d/%lld: do_mmap - open %s failed %d\n",
4178 procid, opno, f.path, e);
4182 if (fstat64(fd, &stb) < 0) {
4184 printf("%d/%lld: do_mmap - fstat64 %s failed %d\n",
4185 procid, opno, f.path, errno);
4190 inode_info(st, sizeof(st), &stb, v);
4191 if (stb.st_size == 0) {
4193 printf("%d/%lld: do_mmap - %s%s zero size\n", procid, opno,
4200 lr = ((int64_t)random() << 32) + random();
4201 off = (off64_t)(lr % stb.st_size);
4202 off = rounddown_64(off, sysconf(_SC_PAGE_SIZE));
4203 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4205 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4206 addr = mmap(NULL, len, prot, flags, fd, off);
4207 e = (addr == MAP_FAILED) ? errno : 0;
4210 printf("%d/%lld: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4211 procid, opno, f.path, st, (long long)off,
4212 (int)len, translate_mmap_flags(flags), e);
4218 if (prot & PROT_WRITE) {
4219 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4220 sigbus_jmp = &sigbus_jmpbuf;
4221 memset(addr, nameseq & 0xff, len);
4225 if ((buf = malloc(len)) != NULL) {
4226 memcpy(buf, addr, len);
4231 /* set NULL to stop other functions from doing siglongjmp */
4235 printf("%d/%lld: %s %s%s [%lld,%d,%s] %s\n",
4236 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4237 f.path, st, (long long)off, (int)len,
4238 translate_mmap_flags(flags),
4239 (e == 0) ? "0" : "Bus error");
4247 mread_f(opnum_t opno, long r)
4249 #ifdef HAVE_SYS_MMAN_H
4250 do_mmap(opno, r, PROT_READ);
4255 mwrite_f(opnum_t opno, long r)
4257 #ifdef HAVE_SYS_MMAN_H
4258 do_mmap(opno, r, PROT_WRITE);
4263 punch_f(opnum_t opno, long r)
4265 #ifdef HAVE_LINUX_FALLOC_H
4266 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4271 zero_f(opnum_t opno, long r)
4273 #ifdef HAVE_LINUX_FALLOC_H
4274 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4279 collapse_f(opnum_t opno, long r)
4281 #ifdef HAVE_LINUX_FALLOC_H
4282 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4287 insert_f(opnum_t opno, long r)
4289 #ifdef HAVE_LINUX_FALLOC_H
4290 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4295 read_f(opnum_t opno, long r)
4309 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4311 printf("%d/%lld: read - no filename\n", procid, opno);
4315 fd = open_path(&f, O_RDONLY);
4316 e = fd < 0 ? errno : 0;
4320 printf("%d/%lld: read - open %s failed %d\n",
4321 procid, opno, f.path, e);
4325 if (fstat64(fd, &stb) < 0) {
4327 printf("%d/%lld: read - fstat64 %s failed %d\n",
4328 procid, opno, f.path, errno);
4333 inode_info(st, sizeof(st), &stb, v);
4334 if (stb.st_size == 0) {
4336 printf("%d/%lld: read - %s%s zero size\n", procid, opno,
4342 lr = ((int64_t)random() << 32) + random();
4343 off = (off64_t)(lr % stb.st_size);
4344 lseek64(fd, off, SEEK_SET);
4345 len = (random() % FILELEN_MAX) + 1;
4347 e = read(fd, buf, len) < 0 ? errno : 0;
4350 printf("%d/%lld: read %s%s [%lld,%d] %d\n",
4351 procid, opno, f.path, st, (long long)off, (int)len, e);
4357 readlink_f(opnum_t opno, long r)
4365 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4367 printf("%d/%lld: readlink - no filename\n", procid, opno);
4371 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4374 printf("%d/%lld: readlink %s %d\n", procid, opno, f.path, e);
4379 readv_f(opnum_t opno, long r)
4391 struct iovec *iov = NULL;
4398 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4400 printf("%d/%lld: readv - no filename\n", procid, opno);
4404 fd = open_path(&f, O_RDONLY);
4405 e = fd < 0 ? errno : 0;
4409 printf("%d/%lld: readv - open %s failed %d\n",
4410 procid, opno, f.path, e);
4414 if (fstat64(fd, &stb) < 0) {
4416 printf("%d/%lld: readv - fstat64 %s failed %d\n",
4417 procid, opno, f.path, errno);
4422 inode_info(st, sizeof(st), &stb, v);
4423 if (stb.st_size == 0) {
4425 printf("%d/%lld: readv - %s%s zero size\n", procid, opno,
4431 lr = ((int64_t)random() << 32) + random();
4432 off = (off64_t)(lr % stb.st_size);
4433 lseek64(fd, off, SEEK_SET);
4434 len = (random() % FILELEN_MAX) + 1;
4437 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4438 iov = calloc(iovcnt, sizeof(struct iovec));
4439 iovl = len / iovcnt;
4441 for (i=0; i<iovcnt; i++) {
4442 (iov + i)->iov_base = (buf + iovb);
4443 (iov + i)->iov_len = iovl;
4447 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4450 printf("%d/%lld: readv %s%s [%lld,%d,%d] %d\n",
4451 procid, opno, f.path, st, (long long)off, (int)iovl,
4458 removefattr_f(opnum_t opno, long r)
4464 char name[XATTR_NAME_BUF_SIZE];
4468 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4470 printf("%d/%lld: removefattr - no filename\n", procid, opno);
4476 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4477 * remove a xattr that doesn't exist (fremovexattr should fail with
4478 * errno set to ENOATTR (61) in this case).
4480 if (fep->xattr_counter > 0)
4481 xattr_num = (random() % fep->xattr_counter) + 1;
4485 e = generate_xattr_name(xattr_num, name, sizeof(name));
4487 printf("%d/%lld: removefattr - file %s failed to generate xattr name: %d\n",
4488 procid, opno, f.path, e);
4492 e = removexattr(f.path, name) < 0 ? errno : 0;
4494 printf("%d/%lld: removefattr file %s name %s %d\n",
4495 procid, opno, f.path, name, e);
4500 struct print_flags renameat2_flags [] = {
4501 { RENAME_NOREPLACE, "NOREPLACE"},
4502 { RENAME_EXCHANGE, "EXCHANGE"},
4503 { RENAME_WHITEOUT, "WHITEOUT"},
4507 #define translate_renameat2_flags(mode) \
4508 ({translate_flags(mode, "|", renameat2_flags);})
4511 do_renameat2(opnum_t opno, long r, int mode)
4527 /* get an existing path for the source of the rename */
4529 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4530 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4532 printf("%d/%lld: rename - no source filename\n",
4539 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4540 * order to maintain filelist/filename integrity, we should
4541 * restrict exchange operation to files of the same type.
4543 if (mode == RENAME_EXCHANGE) {
4544 which = 1 << (flp - flist);
4545 init_pathname(&newf);
4546 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4548 printf("%d/%lld: rename - no target filename\n",
4550 free_pathname(&newf);
4554 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4555 fents_ancestor_check(dfep, fep))) {
4557 printf("%d/%lld: rename(REXCHANGE) %s and %s "
4558 "have ancestor-descendant relationship\n",
4559 procid, opno, f.path, newf.path);
4560 free_pathname(&newf);
4566 parid = dfep->parent;
4569 * Get an existing directory for the destination parent
4572 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4579 * Generate a new path using an existing parent directory
4582 init_pathname(&newf);
4583 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4587 (void)fent_to_name(&f, dfep);
4588 printf("%d/%lld: rename - no filename from %s\n",
4589 procid, opno, f.path);
4591 free_pathname(&newf);
4596 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4599 int xattr_counter = fep->xattr_counter;
4600 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4601 int ft = flp - flist;
4604 oldparid = fep->parent;
4607 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4608 * old parent id for the others.
4610 if (ft == FT_DIR || ft == FT_SUBVOL)
4611 fix_parent(oldid, id, swap);
4613 if (mode == RENAME_WHITEOUT) {
4614 fep->xattr_counter = 0;
4615 add_to_flist(flp - flist, id, parid, xattr_counter);
4616 } else if (mode == RENAME_EXCHANGE) {
4617 fep->xattr_counter = dfep->xattr_counter;
4618 dfep->xattr_counter = xattr_counter;
4620 del_from_flist(flp - flist, fep - flp->fents);
4621 add_to_flist(flp - flist, id, parid, xattr_counter);
4625 printf("%d/%lld: rename(%s) %s to %s %d\n", procid,
4626 opno, translate_renameat2_flags(mode), f.path,
4629 printf("%d/%lld: rename source entry: id=%d,parent=%d\n",
4630 procid, opno, oldid, oldparid);
4631 printf("%d/%lld: rename target entry: id=%d,parent=%d\n",
4632 procid, opno, id, parid);
4635 free_pathname(&newf);
4640 rename_f(opnum_t opno, long r)
4642 do_renameat2(opno, r, 0);
4646 rnoreplace_f(opnum_t opno, long r)
4648 do_renameat2(opno, r, RENAME_NOREPLACE);
4652 rexchange_f(opnum_t opno, long r)
4654 do_renameat2(opno, r, RENAME_EXCHANGE);
4658 rwhiteout_f(opnum_t opno, long r)
4660 do_renameat2(opno, r, RENAME_WHITEOUT);
4664 resvsp_f(opnum_t opno, long r)
4669 struct xfs_flock64 fl;
4677 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4679 printf("%d/%lld: resvsp - no filename\n", procid, opno);
4683 fd = open_path(&f, O_RDWR);
4684 e = fd < 0 ? errno : 0;
4688 printf("%d/%lld: resvsp - open %s failed %d\n",
4689 procid, opno, f.path, e);
4693 if (fstat64(fd, &stb) < 0) {
4695 printf("%d/%lld: resvsp - fstat64 %s failed %d\n",
4696 procid, opno, f.path, errno);
4701 inode_info(st, sizeof(st), &stb, v);
4702 lr = ((int64_t)random() << 32) + random();
4703 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4705 fl.l_whence = SEEK_SET;
4707 fl.l_len = (off64_t)(random() % (1024 * 1024));
4708 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4710 printf("%d/%lld: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4711 procid, opno, f.path, st,
4712 (long long)off, (long long)fl.l_len, e);
4718 rmdir_f(opnum_t opno, long r)
4728 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4730 printf("%d/%lld: rmdir - no directory\n", procid, opno);
4734 e = rmdir_path(&f) < 0 ? errno : 0;
4738 oldparid = fep->parent;
4739 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4742 printf("%d/%lld: rmdir %s %d\n", procid, opno, f.path, e);
4744 printf("%d/%lld: rmdir del entry: id=%d,parent=%d\n",
4745 procid, opno, oldid, oldparid);
4751 setattr_f(opnum_t opno, long r)
4760 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4761 append_pathname(&f, ".");
4762 fd = open_path(&f, O_RDWR);
4763 e = fd < 0 ? errno : 0;
4766 fl = attr_mask & (uint)random();
4767 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4769 printf("%d/%lld: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4775 setfattr_f(opnum_t opno, long r)
4782 char name[XATTR_NAME_BUF_SIZE];
4788 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4790 printf("%d/%lld: setfattr - no filename\n", procid, opno);
4795 if ((fep->xattr_counter > 0) && (random() % 2)) {
4797 * Use an existing xattr name for replacing its value or
4798 * create again a xattr that was previously deleted.
4800 xattr_num = (random() % fep->xattr_counter) + 1;
4802 flag = XATTR_REPLACE;
4804 /* Use a new xattr name. */
4805 xattr_num = fep->xattr_counter + 1;
4807 * Don't always use the create flag because even if our xattr
4808 * counter is 0, we may still have xattrs associated to this
4809 * file (this happens when xattrs are added to a file through
4810 * one of its other hard links), so we can end up updating an
4811 * existing xattr too.
4814 flag = XATTR_CREATE;
4818 * The maximum supported value size depends on the filesystem
4819 * implementation, but 100 bytes is a safe value for most filesystems
4822 value_len = random() % 101;
4823 value = gen_random_string(value_len);
4824 if (!value && value_len > 0) {
4826 printf("%d/%lld: setfattr - file %s failed to allocate value with %d bytes\n",
4827 procid, opno, f.path, value_len);
4830 e = generate_xattr_name(xattr_num, name, sizeof(name));
4832 printf("%d/%lld: setfattr - file %s failed to generate xattr name: %d\n",
4833 procid, opno, f.path, e);
4837 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4839 fep->xattr_counter++;
4841 printf("%d/%lld: setfattr file %s name %s flag %s value length %d: %d\n",
4842 procid, opno, f.path, name, xattr_flag_to_string(flag),
4850 snapshot_f(opnum_t opno, long r)
4852 #ifdef HAVE_BTRFSUTIL_H
4853 enum btrfs_util_error e;
4864 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4866 printf("%d/%lld: snapshot - no subvolume\n", procid,
4871 init_pathname(&newf);
4873 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4877 (void)fent_to_name(&f, fep);
4878 printf("%d/%lld: snapshot - no filename from %s\n",
4879 procid, opno, f.path);
4884 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4885 if (e == BTRFS_UTIL_OK)
4886 add_to_flist(FT_SUBVOL, id, parid, 0);
4888 printf("%d/%lld: snapshot %s->%s %d(%s)\n", procid, opno,
4889 f.path, newf.path, e, btrfs_util_strerror(e));
4890 printf("%d/%lld: snapshot add id=%d,parent=%d\n", procid, opno,
4893 free_pathname(&newf);
4899 stat_f(opnum_t opno, long r)
4907 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4909 printf("%d/%lld: stat - no entries\n", procid, opno);
4913 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4916 printf("%d/%lld: stat %s %d\n", procid, opno, f.path, e);
4921 subvol_create_f(opnum_t opno, long r)
4923 #ifdef HAVE_BTRFSUTIL_H
4924 enum btrfs_util_error e;
4934 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4938 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4942 (void)fent_to_name(&f, fep);
4943 printf("%d/%lld: subvol_create - no filename from %s\n",
4944 procid, opno, f.path);
4949 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4950 if (e == BTRFS_UTIL_OK)
4951 add_to_flist(FT_SUBVOL, id, parid, 0);
4953 printf("%d/%lld: subvol_create %s %d(%s)\n", procid, opno,
4954 f.path, e, btrfs_util_strerror(e));
4955 printf("%d/%lld: subvol_create add id=%d,parent=%d\n", procid,
4963 subvol_delete_f(opnum_t opno, long r)
4965 #ifdef HAVE_BTRFSUTIL_H
4966 enum btrfs_util_error e;
4974 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4976 printf("%d:%lld: subvol_delete - no subvolume\n", procid,
4981 e = btrfs_util_delete_subvolume(f.path, 0);
4983 if (e == BTRFS_UTIL_OK) {
4985 oldparid = fep->parent;
4986 delete_subvol_children(oldid);
4987 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4990 printf("%d/%lld: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4991 e, btrfs_util_strerror(e));
4992 if (e == BTRFS_UTIL_OK)
4993 printf("%d/%lld: subvol_delete del entry: id=%d,parent=%d\n",
4994 procid, opno, oldid, oldparid);
5001 symlink_f(opnum_t opno, long r)
5014 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5019 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5023 (void)fent_to_name(&f, fep);
5024 printf("%d/%lld: symlink - no filename from %s\n",
5025 procid, opno, f.path);
5030 len = (int)(random() % PATH_MAX);
5031 val = malloc(len + 1);
5033 memset(val, 'x', len);
5035 for (i = 10; i < len - 1; i += 10)
5037 e = symlink_path(val, &f) < 0 ? errno : 0;
5040 add_to_flist(FT_SYM, id, parid, 0);
5043 printf("%d/%lld: symlink %s %d\n", procid, opno, f.path, e);
5044 printf("%d/%lld: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5051 sync_f(opnum_t opno, long r)
5055 printf("%d/%lld: sync\n", procid, opno);
5059 truncate_f(opnum_t opno, long r)
5070 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5072 printf("%d/%lld: truncate - no filename\n", procid, opno);
5076 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5080 printf("%d/%lld: truncate - stat64 %s failed %d\n",
5081 procid, opno, f.path, e);
5085 inode_info(st, sizeof(st), &stb, v);
5086 lr = ((int64_t)random() << 32) + random();
5087 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5089 e = truncate64_path(&f, off) < 0 ? errno : 0;
5092 printf("%d/%lld: truncate %s%s %lld %d\n", procid, opno, f.path,
5093 st, (long long)off, e);
5098 unlink_f(opnum_t opno, long r)
5109 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5111 printf("%d/%lld: unlink - no file\n", procid, opno);
5115 e = unlink_path(&f) < 0 ? errno : 0;
5119 oldparid = fep->parent;
5120 del_from_flist(flp - flist, fep - flp->fents);
5123 printf("%d/%lld: unlink %s %d\n", procid, opno, f.path, e);
5125 printf("%d/%lld: unlink del entry: id=%d,parent=%d\n",
5126 procid, opno, oldid, oldparid);
5132 unresvsp_f(opnum_t opno, long r)
5137 struct xfs_flock64 fl;
5145 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5147 printf("%d/%lld: unresvsp - no filename\n", procid, opno);
5151 fd = open_path(&f, O_RDWR);
5152 e = fd < 0 ? errno : 0;
5156 printf("%d/%lld: unresvsp - open %s failed %d\n",
5157 procid, opno, f.path, e);
5161 if (fstat64(fd, &stb) < 0) {
5163 printf("%d/%lld: unresvsp - fstat64 %s failed %d\n",
5164 procid, opno, f.path, errno);
5169 inode_info(st, sizeof(st), &stb, v);
5170 lr = ((int64_t)random() << 32) + random();
5171 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5173 fl.l_whence = SEEK_SET;
5175 fl.l_len = (off64_t)(random() % (1 << 20));
5176 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5178 printf("%d/%lld: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5179 procid, opno, f.path, st,
5180 (long long)off, (long long)fl.l_len, e);
5186 uring_read_f(opnum_t opno, long r)
5189 do_uring_rw(opno, r, O_RDONLY);
5194 uring_write_f(opnum_t opno, long r)
5197 do_uring_rw(opno, r, O_WRONLY);
5202 write_f(opnum_t opno, long r)
5216 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5218 printf("%d/%lld: write - no filename\n", procid, opno);
5222 fd = open_path(&f, O_WRONLY);
5223 e = fd < 0 ? errno : 0;
5227 printf("%d/%lld: write - open %s failed %d\n",
5228 procid, opno, f.path, e);
5232 if (fstat64(fd, &stb) < 0) {
5234 printf("%d/%lld: write - fstat64 %s failed %d\n",
5235 procid, opno, f.path, errno);
5240 inode_info(st, sizeof(st), &stb, v);
5241 lr = ((int64_t)random() << 32) + random();
5242 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5244 lseek64(fd, off, SEEK_SET);
5245 len = (random() % FILELEN_MAX) + 1;
5247 memset(buf, nameseq & 0xff, len);
5248 e = write(fd, buf, len) < 0 ? errno : 0;
5251 printf("%d/%lld: write %s%s [%lld,%d] %d\n",
5252 procid, opno, f.path, st, (long long)off, (int)len, e);
5258 writev_f(opnum_t opno, long r)
5270 struct iovec *iov = NULL;
5277 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5279 printf("%d/%lld: writev - no filename\n", procid, opno);
5283 fd = open_path(&f, O_WRONLY);
5284 e = fd < 0 ? errno : 0;
5288 printf("%d/%lld: writev - open %s failed %d\n",
5289 procid, opno, f.path, e);
5293 if (fstat64(fd, &stb) < 0) {
5295 printf("%d/%lld: writev - fstat64 %s failed %d\n",
5296 procid, opno, f.path, errno);
5301 inode_info(st, sizeof(st), &stb, v);
5302 lr = ((int64_t)random() << 32) + random();
5303 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5305 lseek64(fd, off, SEEK_SET);
5306 len = (random() % FILELEN_MAX) + 1;
5308 memset(buf, nameseq & 0xff, len);
5310 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5311 iov = calloc(iovcnt, sizeof(struct iovec));
5312 iovl = len / iovcnt;
5314 for (i=0; i<iovcnt; i++) {
5315 (iov + i)->iov_base = (buf + iovb);
5316 (iov + i)->iov_len = iovl;
5320 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5324 printf("%d/%lld: writev %s%s [%lld,%d,%d] %d\n",
5325 procid, opno, f.path, st, (long long)off, (int)iovl,
5332 xattr_flag_to_string(int flag)
5334 if (flag == XATTR_CREATE)
5336 if (flag == XATTR_REPLACE)