1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
14 #ifdef HAVE_BTRFSUTIL_H
15 #include <btrfsutil.h>
17 #ifdef HAVE_ATTR_ATTRIBUTES_H
18 #include <attr/attributes.h>
20 #ifdef HAVE_LINUX_FIEMAP_H
21 #include <linux/fiemap.h>
23 #ifndef HAVE_ATTR_LIST
24 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
26 #ifdef HAVE_SYS_PRCTL_H
27 #include <sys/prctl.h>
36 #define URING_ENTRIES 1
39 #include <sys/syscall.h>
40 #include <sys/xattr.h>
42 #ifndef FS_IOC_GETFLAGS
43 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
45 #ifndef FS_IOC_SETFLAGS
46 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
50 #define XFS_ERRTAG_MAX 17
51 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
52 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
57 #ifndef HAVE_RENAMEAT2
58 #if !defined(SYS_renameat2) && defined(__x86_64__)
59 #define SYS_renameat2 316
62 #if !defined(SYS_renameat2) && defined(__i386__)
63 #define SYS_renameat2 353
66 static int renameat2(int dfd1, const char *path1,
67 int dfd2, const char *path2,
71 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
79 #ifndef RENAME_NOREPLACE
80 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
82 #ifndef RENAME_EXCHANGE
83 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
85 #ifndef RENAME_WHITEOUT
86 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
89 #define FILELEN_MAX (32*4096)
155 typedef void (*opfnc_t)(int, long);
157 typedef struct opdesc {
165 typedef struct fent {
172 typedef struct flist {
179 typedef struct pathname {
189 struct print_string {
196 #define FT_DIRm (1 << FT_DIR)
198 #define FT_REGm (1 << FT_REG)
200 #define FT_SYMm (1 << FT_SYM)
202 #define FT_DEVm (1 << FT_DEV)
204 #define FT_RTFm (1 << FT_RTF)
206 #define FT_SUBVOLm (1 << FT_SUBVOL)
208 #define FT_ANYm ((1 << FT_nft) - 1)
209 #define FT_REGFILE (FT_REGm | FT_RTFm)
210 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
211 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
213 #define FLIST_SLOT_INCR 16
216 #define MAXFSIZE ((1ULL << 63) - 1ULL)
217 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
219 #define XATTR_NAME_BUF_SIZE 18
221 void afsync_f(int, long);
222 void allocsp_f(int, long);
223 void aread_f(int, long);
224 void attr_remove_f(int, long);
225 void attr_set_f(int, long);
226 void awrite_f(int, long);
227 void bulkstat_f(int, long);
228 void bulkstat1_f(int, long);
229 void chown_f(int, long);
230 void clonerange_f(int, long);
231 void copyrange_f(int, long);
232 void creat_f(int, long);
233 void deduperange_f(int, long);
234 void dread_f(int, long);
235 void dwrite_f(int, long);
236 void fallocate_f(int, long);
237 void fdatasync_f(int, long);
238 void fiemap_f(int, long);
239 void freesp_f(int, long);
240 void fsync_f(int, long);
241 char *gen_random_string(int);
242 void getattr_f(int, long);
243 void getdents_f(int, long);
244 void getfattr_f(int, long);
245 void link_f(int, long);
246 void listfattr_f(int, long);
247 void mkdir_f(int, long);
248 void mknod_f(int, long);
249 void mread_f(int, long);
250 void mwrite_f(int, long);
251 void punch_f(int, long);
252 void zero_f(int, long);
253 void collapse_f(int, long);
254 void insert_f(int, long);
255 void read_f(int, long);
256 void readlink_f(int, long);
257 void readv_f(int, long);
258 void removefattr_f(int, long);
259 void rename_f(int, long);
260 void rnoreplace_f(int, long);
261 void rexchange_f(int, long);
262 void rwhiteout_f(int, long);
263 void resvsp_f(int, long);
264 void rmdir_f(int, long);
265 void setattr_f(int, long);
266 void setfattr_f(int, long);
267 void setxattr_f(int, long);
268 void snapshot_f(int, long);
269 void splice_f(int, long);
270 void stat_f(int, long);
271 void subvol_create_f(int, long);
272 void subvol_delete_f(int, long);
273 void symlink_f(int, long);
274 void sync_f(int, long);
275 void truncate_f(int, long);
276 void unlink_f(int, long);
277 void unresvsp_f(int, long);
278 void uring_read_f(int, long);
279 void uring_write_f(int, long);
280 void write_f(int, long);
281 void writev_f(int, long);
282 char *xattr_flag_to_string(int);
285 /* { OP_ENUM, "name", function, freq, iswrite }, */
286 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
287 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
288 { OP_AREAD, "aread", aread_f, 1, 0 },
289 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
290 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
291 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
292 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
293 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
294 { OP_CHOWN, "chown", chown_f, 3, 1 },
295 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
296 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
297 { OP_CREAT, "creat", creat_f, 4, 1 },
298 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
299 { OP_DREAD, "dread", dread_f, 4, 0 },
300 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
301 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
302 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
303 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
304 { OP_FREESP, "freesp", freesp_f, 1, 1 },
305 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
306 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
307 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
308 /* get extended attribute */
309 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
310 { OP_LINK, "link", link_f, 1, 1 },
311 /* list extent attributes */
312 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
313 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
314 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
315 { OP_MREAD, "mread", mread_f, 2, 0 },
316 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
317 { OP_PUNCH, "punch", punch_f, 1, 1 },
318 { OP_ZERO, "zero", zero_f, 1, 1 },
319 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
320 { OP_INSERT, "insert", insert_f, 1, 1 },
321 { OP_READ, "read", read_f, 1, 0 },
322 { OP_READLINK, "readlink", readlink_f, 1, 0 },
323 { OP_READV, "readv", readv_f, 1, 0 },
324 /* remove (delete) extended attribute */
325 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
326 { OP_RENAME, "rename", rename_f, 2, 1 },
327 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
328 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
329 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
330 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
331 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
332 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
333 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
334 /* set extended attribute */
335 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
336 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
337 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
338 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
339 { OP_SPLICE, "splice", splice_f, 1, 1 },
340 { OP_STAT, "stat", stat_f, 1, 0 },
341 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
342 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
343 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
344 { OP_SYNC, "sync", sync_f, 1, 1 },
345 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
346 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
347 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
348 { OP_URING_READ, "uring_read", uring_read_f, 1, 0 },
349 { OP_URING_WRITE, "uring_write", uring_write_f, 1, 1 },
350 { OP_WRITE, "write", write_f, 4, 1 },
351 { OP_WRITEV, "writev", writev_f, 4, 1 },
354 flist_t flist[FT_nft] = {
368 struct xfs_fsop_geom geom;
379 unsigned int idmodulo = XFS_IDMODULO_MAX;
380 unsigned int attr_mask = ~0;
383 unsigned long seed = 0;
387 int verifiable_log = 0;
388 sig_atomic_t should_stop = 0;
389 sigjmp_buf *sigbus_jmp = NULL;
390 char *execute_cmd = NULL;
391 int execute_freq = 1;
392 struct print_string flag_str = {0};
394 void add_to_flist(int, int, int, int);
395 void append_pathname(pathname_t *, char *);
396 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
397 int attr_remove_path(pathname_t *, const char *);
398 int attr_set_path(pathname_t *, const char *, const char *, const int);
399 void check_cwd(void);
400 void cleanup_flist(void);
401 int creat_path(pathname_t *, mode_t);
402 void dcache_enter(int, int);
403 void dcache_init(void);
404 fent_t *dcache_lookup(int);
405 void dcache_purge(int, int);
406 void del_from_flist(int, int);
407 int dirid_to_name(char *, int);
409 int fent_to_name(pathname_t *, fent_t *);
410 bool fents_ancestor_check(fent_t *, fent_t *);
411 void fix_parent(int, int, bool);
412 void free_pathname(pathname_t *);
413 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
414 int generate_xattr_name(int, char *, int);
415 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
416 void init_pathname(pathname_t *);
417 int lchown_path(pathname_t *, uid_t, gid_t);
418 int link_path(pathname_t *, pathname_t *);
419 int lstat64_path(pathname_t *, struct stat64 *);
420 void make_freq_table(void);
421 int mkdir_path(pathname_t *, mode_t);
422 int mknod_path(pathname_t *, mode_t, dev_t);
423 void namerandpad(int, char *, int);
424 int open_file_or_dir(pathname_t *, int);
425 int open_path(pathname_t *, int);
426 DIR *opendir_path(pathname_t *);
427 void process_freq(char *);
428 int readlink_path(pathname_t *, char *, size_t);
429 int rename_path(pathname_t *, pathname_t *, int);
430 int rmdir_path(pathname_t *);
431 void separate_pathname(pathname_t *, char *, pathname_t *);
432 void show_ops(int, char *);
433 int stat64_path(pathname_t *, struct stat64 *);
434 int symlink_path(const char *, pathname_t *);
435 int truncate64_path(pathname_t *, off64_t);
436 int unlink_path(pathname_t *);
438 void write_freq(void);
439 void zero_freq(void);
440 void non_btrfs_freq(const char *);
442 void sg_handler(int signum)
450 * Only handle SIGBUS when mmap write to a hole and no
451 * block can be allocated due to ENOSPC, abort otherwise.
454 siglongjmp(*sigbus_jmp, -1);
456 printf("Unknown SIGBUS is caught, Abort!\n");
459 /* should not reach here */
466 int main(int argc, char **argv)
470 char *dirname = NULL;
471 char *logname = NULL;
472 char rpath[PATH_MAX];
481 xfs_error_injection_t err_inj;
482 struct sigaction action;
484 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
486 errrange = errtag = 0;
488 nops = sizeof(ops) / sizeof(ops[0]);
489 ops_end = &ops[nops];
491 while ((c = getopt(argc, argv, allopts)) != -1) {
500 sscanf(optarg, "%d", &errtag);
504 } else if (errtag == 0)
506 if (errtag >= XFS_ERRTAG_MAX) {
508 "error tag %d too large (max %d)\n",
509 errtag, XFS_ERRTAG_MAX - 1);
514 process_freq(optarg);
517 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
518 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
521 idmodulo = strtoul(optarg, NULL, 0);
522 if (idmodulo > XFS_IDMODULO_MAX) {
524 "chown modulo %d too big (max %d)\n",
525 idmodulo, XFS_IDMODULO_MAX);
530 loops = atoi(optarg);
533 operations = atoi(optarg);
540 nproc = atoi(optarg);
546 seed = strtoul(optarg, NULL, 0);
555 execute_cmd = optarg;
561 attr_mask = strtoul(optarg, NULL, 0);
565 if (optarg[0] == 'c')
576 execute_freq = strtoul(optarg, NULL, 0);
579 fprintf(stderr, "%s - invalid parameters\n",
589 /* no directory specified */
590 if (!nousage) usage();
594 non_btrfs_freq(dirname);
595 (void)mkdir(dirname, 0777);
596 if (logname && logname[0] != '/') {
597 if (!getcwd(rpath, sizeof(rpath))){
598 perror("getcwd failed");
604 if (chdir(dirname) < 0) {
609 char path[PATH_MAX + NAME_MAX + 1];
610 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
611 if (freopen(path, "a", stdout) == NULL) {
612 perror("freopen logfile failed");
616 sprintf(buf, "fss%x", (unsigned int)getpid());
617 fd = creat(buf, 0666);
618 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
619 maxfsize = (off64_t)MAXFSIZE32;
621 maxfsize = (off64_t)MAXFSIZE;
626 gettimeofday(&t, (void *)NULL);
627 seed = (int)t.tv_sec ^ (int)t.tv_usec;
628 printf("seed = %ld\n", seed);
630 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
631 if (i >= 0 && geom.rtblocks)
632 rtpct = MIN(MAX(geom.rtblocks * 100 /
633 (geom.rtblocks + geom.datablocks), 1), 99);
642 for (i = 0; i < j; i++)
645 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
651 for (i = 0; i < j; i++)
654 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
656 printf("Injecting failure on tag #%d\n", errtag);
657 err_inj.errtag = errtag;
659 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
661 perror("fsstress - XFS_SYSSGI error injection call");
670 action.sa_handler = sg_handler;
671 sigemptyset(&action.sa_mask);
673 if (sigaction(SIGTERM, &action, 0)) {
674 perror("sigaction failed");
678 for (i = 0; i < nproc; i++) {
680 sigemptyset(&action.sa_mask);
681 action.sa_handler = SIG_DFL;
682 if (sigaction(SIGTERM, &action, 0))
684 action.sa_handler = sg_handler;
685 if (sigaction(SIGBUS, &action, 0))
687 #ifdef HAVE_SYS_PRCTL_H
688 prctl(PR_SET_PDEATHSIG, SIGKILL);
689 if (getppid() == 1) /* parent died already? */
693 char path[PATH_MAX + NAME_MAX + 2 + 11];
694 snprintf(path, sizeof(path), "%s/%s.%d",
696 if (freopen(path, "a", stdout) == NULL) {
697 perror("freopen logfile failed");
703 if (io_setup(AIO_ENTRIES, &io_ctx) != 0) {
704 fprintf(stderr, "io_setup failed\n");
709 if (io_uring_queue_init(URING_ENTRIES, &ring, 0)) {
710 fprintf(stderr, "io_uring_queue_init failed\n");
714 for (i = 0; !loops || (i < loops); i++)
717 if(io_destroy(io_ctx) != 0) {
718 fprintf(stderr, "io_destroy failed");
723 io_uring_queue_exit(&ring);
730 while (wait(&stat) > 0 && !should_stop) {
733 action.sa_flags = SA_RESTART;
734 sigaction(SIGTERM, &action, 0);
735 kill(-getpid(), SIGTERM);
736 while (wait(&stat) > 0)
742 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
744 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
746 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
759 add_string(struct print_string *str, const char *add)
761 int len = strlen(add);
766 if (len > (str->max - 1) - str->len) {
767 str->len = str->max - 1;
771 memcpy(str->buffer + str->len, add, len);
773 str->buffer[str->len] = '\0';
779 translate_flags(int flags, const char *delim,
780 const struct print_flags *flag_array)
782 int i, mask, first = 1;
785 if (!flag_str.buffer) {
786 flag_str.buffer = malloc(4096);
790 if (!flag_str.buffer)
793 flag_str.buffer[0] = '\0';
795 for (i = 0; flag_array[i].name && flags; i++) {
796 mask = flag_array[i].mask;
797 if ((flags & mask) != mask)
800 add = flag_array[i].name;
803 add_string(&flag_str, delim);
806 add_string(&flag_str, add);
809 /* Check whether there are any leftover flags. */
815 add_string(&flag_str, delim);
817 ret = snprintf(number, 11, "0x%x", flags) > 0;
818 if (ret > 0 && ret <= 11)
819 add_string(&flag_str, number);
822 return flag_str.buffer;
826 add_to_flist(int ft, int id, int parent, int xattr_counter)
832 if (ftp->nfiles == ftp->nslots) {
833 ftp->nslots += FLIST_SLOT_INCR;
834 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
836 fep = &ftp->fents[ftp->nfiles++];
839 fep->parent = parent;
840 fep->xattr_counter = xattr_counter;
844 append_pathname(pathname_t *name, char *str)
850 /* attempting to append to a dir a zero length path */
851 if (len && *str == '/' && name->len == 0) {
852 fprintf(stderr, "fsstress: append_pathname failure\n");
853 assert(chdir(homedir) == 0);
858 name->path = realloc(name->path, name->len + 1 + len);
859 strcpy(&name->path[name->len], str);
864 attr_list_path(pathname_t *name,
866 const int buffersize,
868 attrlist_cursor_t *cursor)
870 char buf[NAME_MAX + 1];
874 if (flags != ATTR_DONTFOLLOW) {
879 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
880 if (rval >= 0 || errno != ENAMETOOLONG)
882 separate_pathname(name, buf, &newname);
883 if (chdir(buf) == 0) {
884 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
885 assert(chdir("..") == 0);
887 free_pathname(&newname);
892 attr_remove_path(pathname_t *name, const char *attrname)
894 char buf[NAME_MAX + 1];
898 rval = lremovexattr(name->path, attrname);
899 if (rval >= 0 || errno != ENAMETOOLONG)
901 separate_pathname(name, buf, &newname);
902 if (chdir(buf) == 0) {
903 rval = attr_remove_path(&newname, attrname);
904 assert(chdir("..") == 0);
906 free_pathname(&newname);
911 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
912 const int valuelength)
914 char buf[NAME_MAX + 1];
918 rval = lsetxattr(name->path, attrname, attrvalue, valuelength, 0);
919 if (rval >= 0 || errno != ENAMETOOLONG)
921 separate_pathname(name, buf, &newname);
922 if (chdir(buf) == 0) {
923 rval = attr_set_path(&newname, attrname, attrvalue,
925 assert(chdir("..") == 0);
927 free_pathname(&newname);
935 struct stat64 statbuf;
937 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
939 assert(chdir(homedir) == 0);
940 fprintf(stderr, "fsstress: check_cwd failure\n");
947 * go thru flist and release all entries
949 * NOTE: this function shouldn't be called until the end of a process
957 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
966 creat_path(pathname_t *name, mode_t mode)
968 char buf[NAME_MAX + 1];
972 rval = creat(name->path, mode);
973 if (rval >= 0 || errno != ENAMETOOLONG)
975 separate_pathname(name, buf, &newname);
976 if (chdir(buf) == 0) {
977 rval = creat_path(&newname, mode);
978 assert(chdir("..") == 0);
980 free_pathname(&newname);
985 dcache_enter(int dirid, int slot)
987 dcache[dirid % NDCACHE] = slot;
995 for (i = 0; i < NDCACHE; i++)
1000 dcache_lookup(int dirid)
1005 i = dcache[dirid % NDCACHE];
1006 if (i >= 0 && i < flist[FT_DIR].nfiles &&
1007 (fep = &flist[FT_DIR].fents[i])->id == dirid)
1009 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
1010 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
1016 dcache_purge(int dirid, int ft)
1019 dcp = &dcache[dirid % NDCACHE];
1020 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1021 flist[ft].fents[*dcp].id == dirid)
1026 * Delete the item from the list by
1027 * moving last entry over the deleted one;
1028 * unless deleted entry is the last one.
1029 * Input: which file list array and which slot in array
1032 del_from_flist(int ft, int slot)
1037 if (ft == FT_DIR || ft == FT_SUBVOL)
1038 dcache_purge(ftp->fents[slot].id, ft);
1039 if (slot != ftp->nfiles - 1) {
1040 if (ft == FT_DIR || ft == FT_SUBVOL)
1041 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1042 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1048 delete_subvol_children(int parid)
1053 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1055 for (c = 0; c < flp->nfiles; c++) {
1056 if (flp->fents[c].parent == parid) {
1057 int id = flp->fents[c].id;
1058 del_from_flist(i, c);
1059 if (i == FT_DIR || i == FT_SUBVOL)
1060 delete_subvol_children(id);
1068 dirid_to_fent(int dirid)
1075 if ((fep = dcache_lookup(dirid)))
1079 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1080 if (fep->id == dirid) {
1081 dcache_enter(dirid, fep - flp->fents);
1095 struct stat64 statbuf;
1103 dividend = (operations + execute_freq) / (execute_freq + 1);
1104 sprintf(buf, "p%x", procid);
1105 (void)mkdir(buf, 0777);
1106 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1110 top_ino = statbuf.st_ino;
1111 homedir = getcwd(NULL, 0);
1113 perror("getcwd failed");
1119 namerand = random();
1120 for (opno = 0; opno < operations; opno++) {
1121 if (execute_cmd && opno && opno % dividend == 0) {
1123 printf("%d: execute command %s\n", opno,
1125 rval = system(execute_cmd);
1127 fprintf(stderr, "execute command failed with "
1130 p = &ops[freq_table[random() % freq_table_size]];
1131 p->func(opno, random());
1133 * test for forced shutdown by stat'ing the test
1134 * directory. If this stat returns EIO, assume
1135 * the forced shutdown happened.
1137 if (errtag != 0 && opno % 100 == 0) {
1138 rval = stat64(".", &statbuf);
1140 fprintf(stderr, "Detected EIO\n");
1146 assert(chdir("..") == 0);
1151 sprintf(cmd, "rm -rf %s", buf);
1154 perror("cleaning up");
1160 * build up a pathname going thru the file entry and all
1161 * its parent entries
1162 * Return 0 on error, 1 on success;
1165 fent_to_name(pathname_t *name, fent_t *fep)
1167 flist_t *flp = &flist[fep->ft];
1168 char buf[NAME_MAX + 1];
1176 /* build up parent directory name */
1177 if (fep->parent != -1) {
1178 pfep = dirid_to_fent(fep->parent);
1181 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1182 procid, fep->id, fep->parent);
1187 e = fent_to_name(name, pfep);
1190 append_pathname(name, "/");
1193 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1194 namerandpad(fep->id, buf, i);
1195 append_pathname(name, buf);
1200 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1204 for (tmpfep = fep; tmpfep->parent != -1;
1205 tmpfep = dirid_to_fent(tmpfep->parent)) {
1206 if (tmpfep->parent == dfep->id)
1214 fix_parent(int oldid, int newid, bool swap)
1221 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1222 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1223 if (fep->parent == oldid)
1224 fep->parent = newid;
1225 else if (swap && fep->parent == newid)
1226 fep->parent = oldid;
1232 free_pathname(pathname_t *name)
1242 * Generate a filename of type ft.
1243 * If we have a fep which should be a directory then use it
1244 * as the parent path for this new filename i.e. prepend with it.
1247 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1249 char buf[NAME_MAX + 1];
1258 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1259 namerandpad(id, buf, len);
1261 /* prepend fep parent dir-name to it */
1263 e = fent_to_name(name, fep);
1266 append_pathname(name, "/");
1268 append_pathname(name, buf);
1272 for (j = 0; !*v && j < ilistlen; j++) {
1273 if (ilist[j] == id) {
1281 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1285 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1295 * Input: "which" to choose the file-types eg. non-directory
1296 * Input: "r" to choose which file
1297 * Output: file-list, file-entry, name for the chosen file.
1298 * Output: verbose if chosen file is on the ilist.
1301 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1304 int totalsum = 0; /* total number of matching files */
1305 int partialsum = 0; /* partial sum of matching files */
1311 int e = 1; /* success */
1314 * go thru flist and add up number of files for each
1315 * category that matches with <which>.
1317 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1318 if (which & (1 << i))
1319 totalsum += flp->nfiles;
1321 if (totalsum == 0) {
1331 * Now we have possible matches between 0..totalsum-1.
1332 * And we use r to help us choose which one we want,
1333 * which when bounded by totalsum becomes x.
1335 x = (int)(r % totalsum);
1336 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1337 if (which & (1 << i)) {
1338 if (x < partialsum + flp->nfiles) {
1340 /* found the matching file entry */
1341 fep = &flp->fents[x - partialsum];
1343 /* fill-in what we were asked for */
1345 e = fent_to_name(name, fep);
1348 fprintf(stderr, "%d: failed to get path for entry:"
1349 " id=%d,parent=%d\n",
1350 procid, fep->id, fep->parent);
1359 /* turn on verbose if its an ilisted file */
1361 for (j = 0; !*v && j < ilistlen; j++) {
1362 if (ilist[j] == fep->id) {
1369 partialsum += flp->nfiles;
1373 fprintf(stderr, "fsstress: get_fname failure\n");
1380 init_pathname(pathname_t *name)
1387 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1389 char buf[NAME_MAX + 1];
1393 rval = lchown(name->path, owner, group);
1394 if (rval >= 0 || errno != ENAMETOOLONG)
1396 separate_pathname(name, buf, &newname);
1397 if (chdir(buf) == 0) {
1398 rval = lchown_path(&newname, owner, group);
1399 assert(chdir("..") == 0);
1401 free_pathname(&newname);
1406 link_path(pathname_t *name1, pathname_t *name2)
1408 char buf1[NAME_MAX + 1];
1409 char buf2[NAME_MAX + 1];
1411 pathname_t newname1;
1412 pathname_t newname2;
1415 rval = link(name1->path, name2->path);
1416 if (rval >= 0 || errno != ENAMETOOLONG)
1418 separate_pathname(name1, buf1, &newname1);
1419 separate_pathname(name2, buf2, &newname2);
1420 if (strcmp(buf1, buf2) == 0) {
1421 if (chdir(buf1) == 0) {
1422 rval = link_path(&newname1, &newname2);
1423 assert(chdir("..") == 0);
1426 if (strcmp(buf1, "..") == 0)
1428 else if (strcmp(buf2, "..") == 0)
1430 else if (strlen(buf1) == 0)
1432 else if (strlen(buf2) == 0)
1435 down1 = MAX(newname1.len, 3 + name2->len) <=
1436 MAX(3 + name1->len, newname2.len);
1438 free_pathname(&newname2);
1439 append_pathname(&newname2, "../");
1440 append_pathname(&newname2, name2->path);
1441 if (chdir(buf1) == 0) {
1442 rval = link_path(&newname1, &newname2);
1443 assert(chdir("..") == 0);
1446 free_pathname(&newname1);
1447 append_pathname(&newname1, "../");
1448 append_pathname(&newname1, name1->path);
1449 if (chdir(buf2) == 0) {
1450 rval = link_path(&newname1, &newname2);
1451 assert(chdir("..") == 0);
1455 free_pathname(&newname1);
1456 free_pathname(&newname2);
1461 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1463 char buf[NAME_MAX + 1];
1467 rval = lstat64(name->path, sbuf);
1468 if (rval >= 0 || errno != ENAMETOOLONG)
1470 separate_pathname(name, buf, &newname);
1471 if (chdir(buf) == 0) {
1472 rval = lstat64_path(&newname, sbuf);
1473 assert(chdir("..") == 0);
1475 free_pathname(&newname);
1480 make_freq_table(void)
1486 for (p = ops, f = 0; p < ops_end; p++)
1488 freq_table = malloc(f * sizeof(*freq_table));
1489 freq_table_size = f;
1490 for (p = ops, i = 0; p < ops_end; p++) {
1491 for (f = 0; f < p->freq; f++, i++)
1492 freq_table[i] = p->op;
1497 mkdir_path(pathname_t *name, mode_t mode)
1499 char buf[NAME_MAX + 1];
1503 rval = mkdir(name->path, mode);
1504 if (rval >= 0 || errno != ENAMETOOLONG)
1506 separate_pathname(name, buf, &newname);
1507 if (chdir(buf) == 0) {
1508 rval = mkdir_path(&newname, mode);
1509 assert(chdir("..") == 0);
1511 free_pathname(&newname);
1516 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1518 char buf[NAME_MAX + 1];
1522 rval = mknod(name->path, mode, dev);
1523 if (rval >= 0 || errno != ENAMETOOLONG)
1525 separate_pathname(name, buf, &newname);
1526 if (chdir(buf) == 0) {
1527 rval = mknod_path(&newname, mode, dev);
1528 assert(chdir("..") == 0);
1530 free_pathname(&newname);
1535 namerandpad(int id, char *buf, int i)
1538 static int buckets[] =
1539 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1545 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1546 padmod = buckets[bucket] + 1 - i;
1549 padlen = (id ^ namerand) % padmod;
1551 memset(&buf[i], 'X', padlen);
1552 buf[i + padlen] = '\0';
1557 open_file_or_dir(pathname_t *name, int flags)
1561 fd = open_path(name, flags);
1564 if (fd == -1 && errno != EISDIR)
1566 /* Directories can not be opened in write mode nor direct mode. */
1567 flags &= ~(O_WRONLY | O_DIRECT);
1568 flags |= O_RDONLY | O_DIRECTORY;
1569 return open_path(name, flags);
1573 open_path(pathname_t *name, int oflag)
1575 char buf[NAME_MAX + 1];
1579 rval = open(name->path, oflag);
1580 if (rval >= 0 || errno != ENAMETOOLONG)
1582 separate_pathname(name, buf, &newname);
1583 if (chdir(buf) == 0) {
1584 rval = open_path(&newname, oflag);
1585 assert(chdir("..") == 0);
1587 free_pathname(&newname);
1592 opendir_path(pathname_t *name)
1594 char buf[NAME_MAX + 1];
1598 rval = opendir(name->path);
1599 if (rval || errno != ENAMETOOLONG)
1601 separate_pathname(name, buf, &newname);
1602 if (chdir(buf) == 0) {
1603 rval = opendir_path(&newname);
1604 assert(chdir("..") == 0);
1606 free_pathname(&newname);
1611 process_freq(char *arg)
1616 s = strchr(arg, '=');
1618 fprintf(stderr, "bad argument '%s'\n", arg);
1622 for (p = ops; p < ops_end; p++) {
1623 if (strcmp(arg, p->name) == 0) {
1628 fprintf(stderr, "can't find op type %s for -f\n", arg);
1633 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1635 char buf[NAME_MAX + 1];
1639 rval = readlink(name->path, lbuf, lbufsiz);
1640 if (rval >= 0 || errno != ENAMETOOLONG)
1642 separate_pathname(name, buf, &newname);
1643 if (chdir(buf) == 0) {
1644 rval = readlink_path(&newname, lbuf, lbufsiz);
1645 assert(chdir("..") == 0);
1647 free_pathname(&newname);
1652 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1654 char buf1[NAME_MAX + 1];
1655 char buf2[NAME_MAX + 1];
1657 pathname_t newname1;
1658 pathname_t newname2;
1662 rval = rename(name1->path, name2->path);
1664 rval = renameat2(AT_FDCWD, name1->path,
1665 AT_FDCWD, name2->path, mode);
1666 if (rval >= 0 || errno != ENAMETOOLONG)
1668 separate_pathname(name1, buf1, &newname1);
1669 separate_pathname(name2, buf2, &newname2);
1670 if (strcmp(buf1, buf2) == 0) {
1671 if (chdir(buf1) == 0) {
1672 rval = rename_path(&newname1, &newname2, mode);
1673 assert(chdir("..") == 0);
1676 if (strcmp(buf1, "..") == 0)
1678 else if (strcmp(buf2, "..") == 0)
1680 else if (strlen(buf1) == 0)
1682 else if (strlen(buf2) == 0)
1685 down1 = MAX(newname1.len, 3 + name2->len) <=
1686 MAX(3 + name1->len, newname2.len);
1688 free_pathname(&newname2);
1689 append_pathname(&newname2, "../");
1690 append_pathname(&newname2, name2->path);
1691 if (chdir(buf1) == 0) {
1692 rval = rename_path(&newname1, &newname2, mode);
1693 assert(chdir("..") == 0);
1696 free_pathname(&newname1);
1697 append_pathname(&newname1, "../");
1698 append_pathname(&newname1, name1->path);
1699 if (chdir(buf2) == 0) {
1700 rval = rename_path(&newname1, &newname2, mode);
1701 assert(chdir("..") == 0);
1705 free_pathname(&newname1);
1706 free_pathname(&newname2);
1711 rmdir_path(pathname_t *name)
1713 char buf[NAME_MAX + 1];
1717 rval = rmdir(name->path);
1718 if (rval >= 0 || errno != ENAMETOOLONG)
1720 separate_pathname(name, buf, &newname);
1721 if (chdir(buf) == 0) {
1722 rval = rmdir_path(&newname);
1723 assert(chdir("..") == 0);
1725 free_pathname(&newname);
1730 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1734 init_pathname(newname);
1735 slash = strchr(name->path, '/');
1736 if (slash == NULL) {
1741 strcpy(buf, name->path);
1743 append_pathname(newname, slash + 1);
1749 show_ops(int flag, char *lead_str)
1754 /* print in list form */
1757 for (p = ops; p < ops_end; p++) {
1758 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1759 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1760 x+=printf("%s ", p->name);
1763 } else if (flag == 0) {
1764 /* Table view style */
1766 for (f = 0, p = ops; p < ops_end; p++)
1772 for (p = ops; p < ops_end; p++) {
1773 if (flag != 0 || p->freq > 0) {
1774 if (lead_str != NULL)
1775 printf("%s", lead_str);
1776 printf("%20s %d/%d %s\n",
1777 p->name, p->freq, f,
1778 (p->iswrite == 0) ? " " : "write op");
1782 /* Command line style */
1783 if (lead_str != NULL)
1784 printf("%s", lead_str);
1785 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1787 for (p = ops; p < ops_end; p++)
1789 printf("-f %s=%d \\\n",p->name, p->freq);
1794 stat64_path(pathname_t *name, struct stat64 *sbuf)
1796 char buf[NAME_MAX + 1];
1800 rval = stat64(name->path, sbuf);
1801 if (rval >= 0 || errno != ENAMETOOLONG)
1803 separate_pathname(name, buf, &newname);
1804 if (chdir(buf) == 0) {
1805 rval = stat64_path(&newname, sbuf);
1806 assert(chdir("..") == 0);
1808 free_pathname(&newname);
1813 symlink_path(const char *name1, pathname_t *name)
1815 char buf[NAME_MAX + 1];
1819 if (!strcmp(name1, name->path)) {
1820 printf("yikes! %s %s\n", name1, name->path);
1824 rval = symlink(name1, name->path);
1825 if (rval >= 0 || errno != ENAMETOOLONG)
1827 separate_pathname(name, buf, &newname);
1828 if (chdir(buf) == 0) {
1829 rval = symlink_path(name1, &newname);
1830 assert(chdir("..") == 0);
1832 free_pathname(&newname);
1837 truncate64_path(pathname_t *name, off64_t length)
1839 char buf[NAME_MAX + 1];
1843 rval = truncate64(name->path, length);
1844 if (rval >= 0 || errno != ENAMETOOLONG)
1846 separate_pathname(name, buf, &newname);
1847 if (chdir(buf) == 0) {
1848 rval = truncate64_path(&newname, length);
1849 assert(chdir("..") == 0);
1851 free_pathname(&newname);
1856 unlink_path(pathname_t *name)
1858 char buf[NAME_MAX + 1];
1862 rval = unlink(name->path);
1863 if (rval >= 0 || errno != ENAMETOOLONG)
1865 separate_pathname(name, buf, &newname);
1866 if (chdir(buf) == 0) {
1867 rval = unlink_path(&newname);
1868 assert(chdir("..") == 0);
1870 free_pathname(&newname);
1877 printf("Usage: %s -H or\n", myprog);
1878 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1880 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1882 printf(" -c clean up the test directory after each run\n");
1883 printf(" -d dir specifies the base directory for operations\n");
1884 printf(" -e errtg specifies error injection stuff\n");
1885 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1886 printf(" the valid operation names are:\n");
1888 printf(" -i filenum get verbose output for this nth file object\n");
1889 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1890 printf(" *use 0 for infinite (default 1)\n");
1891 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1892 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1893 printf(" -o logfile specifies logfile name\n");
1894 printf(" -p nproc specifies the no. of processes (default 1)\n");
1895 printf(" -r specifies random name padding\n");
1896 printf(" -s seed specifies the seed for the random generator (default random)\n");
1897 printf(" -v specifies verbose mode\n");
1898 printf(" -w zeros frequencies of non-write operations\n");
1899 printf(" -x cmd execute command in the middle of operations\n");
1900 printf(" -z zeros frequencies of all operations\n");
1901 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1902 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1903 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1904 printf(" -H prints usage and exits\n");
1912 for (p = ops; p < ops_end; p++) {
1923 for (p = ops; p < ops_end; p++)
1927 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1929 opty_t btrfs_ops[] = {
1936 non_btrfs_freq(const char *path)
1939 #ifdef HAVE_BTRFSUTIL_H
1940 enum btrfs_util_error e;
1942 e = btrfs_util_is_subvolume(path);
1943 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1946 for (p = ops; p < ops_end; p++) {
1948 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1949 if (p->op == btrfs_ops[i]) {
1957 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1960 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1961 verifiable_log ? -1: (long)s->st_ino,
1962 (long)s->st_nlink, s->st_uid, s->st_gid,
1963 (long long) s->st_blocks, (long long) s->st_size);
1967 afsync_f(int opno, long r)
1975 struct iocb *iocbs[] = { &iocb };
1976 struct io_event event;
1979 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1981 printf("%d/%d: afsync - no filename\n", procid, opno);
1985 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1986 e = fd < 0 ? errno : 0;
1990 printf("%d/%d: afsync - open %s failed %d\n",
1991 procid, opno, f.path, e);
1996 io_prep_fsync(&iocb, fd);
1997 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1999 printf("%d/%d: afsync - io_submit %s %d\n",
2000 procid, opno, f.path, e);
2005 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2007 printf("%d/%d: afsync - io_getevents failed %d\n",
2016 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2023 allocsp_f(int opno, long r)
2028 struct xfs_flock64 fl;
2036 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2038 printf("%d/%d: allocsp - no filename\n", procid, opno);
2042 fd = open_path(&f, O_RDWR);
2043 e = fd < 0 ? errno : 0;
2047 printf("%d/%d: allocsp - open %s failed %d\n",
2048 procid, opno, f.path, e);
2052 if (fstat64(fd, &stb) < 0) {
2054 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2055 procid, opno, f.path, errno);
2060 inode_info(st, sizeof(st), &stb, v);
2061 lr = ((int64_t)random() << 32) + random();
2062 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2064 fl.l_whence = SEEK_SET;
2067 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2069 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2070 procid, opno, f.path, st, (long long)off, e);
2078 do_aio_rw(int opno, long r, int flags)
2082 struct dioattr diob;
2094 struct io_event event;
2095 struct iocb *iocbs[] = { &iocb };
2096 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2099 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2101 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2104 fd = open_path(&f, flags|O_DIRECT);
2105 e = fd < 0 ? errno : 0;
2109 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2110 procid, opno, f.path, e);
2113 if (fstat64(fd, &stb) < 0) {
2115 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2116 procid, opno, f.path, errno);
2119 inode_info(st, sizeof(st), &stb, v);
2120 if (!iswrite && stb.st_size == 0) {
2122 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2126 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2129 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2130 " fallback to stat()\n",
2131 procid, opno, f.path, st, errno);
2132 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2133 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2135 dio_env = getenv("XFS_DIO_MIN");
2137 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2138 align = (int64_t)diob.d_miniosz;
2139 lr = ((int64_t)random() << 32) + random();
2140 len = (random() % FILELEN_MAX) + 1;
2141 len -= (len % align);
2144 else if (len > diob.d_maxiosz)
2145 len = diob.d_maxiosz;
2146 buf = memalign(diob.d_mem, len);
2149 printf("%d/%d: do_aio_rw - memalign failed\n",
2155 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2156 off -= (off % align);
2158 memset(buf, nameseq & 0xff, len);
2159 io_prep_pwrite(&iocb, fd, buf, len, off);
2161 off = (off64_t)(lr % stb.st_size);
2162 off -= (off % align);
2163 io_prep_pread(&iocb, fd, buf, len, off);
2165 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2167 printf("%d/%d: %s - io_submit failed %d\n",
2168 procid, opno, iswrite ? "awrite" : "aread", e);
2171 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2173 printf("%d/%d: %s - io_getevents failed %d\n",
2174 procid, opno, iswrite ? "awrite" : "aread", e);
2178 e = event.res != len ? event.res2 : 0;
2180 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2181 procid, opno, iswrite ? "awrite" : "aread",
2182 f.path, st, (long long)off, (int)len, e);
2194 do_uring_rw(int opno, long r, int flags)
2206 struct io_uring_sqe *sqe;
2207 struct io_uring_cqe *cqe;
2209 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2212 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2214 printf("%d/%d: do_uring_rw - no filename\n", procid, opno);
2217 fd = open_path(&f, flags);
2218 e = fd < 0 ? errno : 0;
2222 printf("%d/%d: do_uring_rw - open %s failed %d\n",
2223 procid, opno, f.path, e);
2226 if (fstat64(fd, &stb) < 0) {
2228 printf("%d/%d: do_uring_rw - fstat64 %s failed %d\n",
2229 procid, opno, f.path, errno);
2232 inode_info(st, sizeof(st), &stb, v);
2233 if (!iswrite && stb.st_size == 0) {
2235 printf("%d/%d: do_uring_rw - %s%s zero size\n", procid, opno,
2239 sqe = io_uring_get_sqe(&ring);
2242 printf("%d/%d: do_uring_rw - io_uring_get_sqe failed\n",
2246 lr = ((int64_t)random() << 32) + random();
2247 len = (random() % FILELEN_MAX) + 1;
2251 printf("%d/%d: do_uring_rw - malloc failed\n",
2255 iovec.iov_base = buf;
2256 iovec.iov_len = len;
2258 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2260 memset(buf, nameseq & 0xff, len);
2261 io_uring_prep_writev(sqe, fd, &iovec, 1, off);
2263 off = (off64_t)(lr % stb.st_size);
2264 io_uring_prep_readv(sqe, fd, &iovec, 1, off);
2267 if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
2269 printf("%d/%d: %s - io_uring_submit failed %d\n", procid, opno,
2270 iswrite ? "uring_write" : "uring_read", e);
2273 if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
2275 printf("%d/%d: %s - io_uring_wait_cqe failed %d\n", procid, opno,
2276 iswrite ? "uring_write" : "uring_read", e);
2280 printf("%d/%d: %s %s%s [%lld, %d(res=%d)] %d\n",
2281 procid, opno, iswrite ? "uring_write" : "uring_read",
2282 f.path, st, (long long)off, (int)len, cqe->res, e);
2283 io_uring_cqe_seen(&ring, cqe);
2295 aread_f(int opno, long r)
2298 do_aio_rw(opno, r, O_RDONLY);
2303 attr_remove_f(int opno, long r)
2305 attrlist_ent_t *aep;
2309 attrlist_cursor_t cursor;
2318 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2319 append_pathname(&f, ".");
2321 bzero(&cursor, sizeof(cursor));
2323 bzero(buf, sizeof(buf));
2324 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2328 alist = (attrlist_t *)buf;
2329 total += alist->al_count;
2330 } while (alist->al_more);
2333 printf("%d/%d: attr_remove - no attrs for %s\n",
2334 procid, opno, f.path);
2338 which = (int)(random() % total);
2339 bzero(&cursor, sizeof(cursor));
2343 bzero(buf, sizeof(buf));
2344 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2348 alist = (attrlist_t *)buf;
2349 if (which < ent + alist->al_count) {
2350 aep = (attrlist_ent_t *)
2351 &buf[alist->al_offset[which - ent]];
2352 aname = aep->a_name;
2355 ent += alist->al_count;
2356 } while (alist->al_more);
2357 if (aname == NULL) {
2360 "%d/%d: attr_remove - name %d not found at %s\n",
2361 procid, opno, which, f.path);
2365 if (attr_remove_path(&f, aname) < 0)
2371 printf("%d/%d: attr_remove %s %s %d\n",
2372 procid, opno, f.path, aname, e);
2377 attr_set_f(int opno, long r)
2384 static int lengths[] = { 10, 100, 1000, 10000 };
2389 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2390 append_pathname(&f, ".");
2391 sprintf(aname, "a%x", nameseq++);
2392 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2393 len = (int)(random() % lengths[li]);
2397 memset(aval, nameseq & 0xff, len);
2398 if (attr_set_path(&f, aname, aval, len) < 0)
2405 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2411 awrite_f(int opno, long r)
2414 do_aio_rw(opno, r, O_WRONLY);
2419 bulkstat_f(int opno, long r)
2425 struct xfs_bstat *t;
2427 struct xfs_fsop_bulkreq bsr;
2430 nent = (r % 999) + 2;
2431 t = malloc(nent * sizeof(*t));
2432 fd = open(".", O_RDONLY);
2440 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2444 printf("%d/%d: bulkstat nent %d total %lld\n",
2445 procid, opno, nent, (long long)total);
2450 bulkstat1_f(int opno, long r)
2460 struct xfs_fsop_bulkreq bsr;
2463 good = random() & 1;
2465 /* use an inode we know exists */
2467 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2468 append_pathname(&f, ".");
2469 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2474 * pick a random inode
2476 * note this can generate kernel warning messages
2477 * since bulkstat_one will read the disk block that
2478 * would contain a given inode even if that disk
2479 * block doesn't contain inodes.
2481 * this is detected later, but not until after the
2482 * warning is displayed.
2484 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2490 fd = open(".", O_RDONLY);
2496 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2498 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2499 procid, opno, good?"real":"random",
2500 verifiable_log ? -1LL : (long long)ino, e);
2505 chown_f(int opno, long r)
2515 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2516 append_pathname(&f, ".");
2517 u = (uid_t)random();
2518 g = (gid_t)random();
2519 nbits = (int)(random() % idmodulo);
2520 u &= (1 << nbits) - 1;
2521 g &= (1 << nbits) - 1;
2522 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2525 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2529 /* reflink some arbitrary range of f1 to f2. */
2536 struct file_clone_range fcr;
2537 struct pathname fpath1;
2538 struct pathname fpath2;
2539 struct stat64 stat1;
2540 struct stat64 stat2;
2541 char inoinfo1[1024];
2542 char inoinfo2[1024];
2556 init_pathname(&fpath1);
2557 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2559 printf("%d/%d: clonerange read - no filename\n",
2564 init_pathname(&fpath2);
2565 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2567 printf("%d/%d: clonerange write - no filename\n",
2573 fd1 = open_path(&fpath1, O_RDONLY);
2574 e = fd1 < 0 ? errno : 0;
2578 printf("%d/%d: clonerange read - open %s failed %d\n",
2579 procid, opno, fpath1.path, e);
2583 fd2 = open_path(&fpath2, O_WRONLY);
2584 e = fd2 < 0 ? errno : 0;
2588 printf("%d/%d: clonerange write - open %s failed %d\n",
2589 procid, opno, fpath2.path, e);
2593 /* Get file stats */
2594 if (fstat64(fd1, &stat1) < 0) {
2596 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2597 procid, opno, fpath1.path, errno);
2600 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2602 if (fstat64(fd2, &stat2) < 0) {
2604 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2605 procid, opno, fpath2.path, errno);
2608 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2610 /* Calculate offsets */
2611 len = (random() % FILELEN_MAX) + 1;
2612 len &= ~(stat1.st_blksize - 1);
2614 len = stat1.st_blksize;
2615 if (len > stat1.st_size)
2616 len = stat1.st_size;
2618 lr = ((int64_t)random() << 32) + random();
2619 if (stat1.st_size == len)
2622 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2624 off1 &= ~(stat1.st_blksize - 1);
2627 * If srcfile == destfile, randomly generate destination ranges
2628 * until we find one that doesn't overlap the source range.
2630 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2632 lr = ((int64_t)random() << 32) + random();
2633 off2 = (off64_t)(lr % max_off2);
2635 off2 &= ~(stat2.st_blksize - 1);
2636 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2638 /* Clone data blocks */
2640 fcr.src_offset = off1;
2641 fcr.src_length = len;
2642 fcr.dest_offset = off2;
2644 ret = ioctl(fd2, FICLONERANGE, &fcr);
2645 e = ret < 0 ? errno : 0;
2647 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2649 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2650 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2653 printf(" error %d", e);
2662 free_pathname(&fpath2);
2664 free_pathname(&fpath1);
2668 /* copy some arbitrary range of f1 to f2. */
2674 #ifdef HAVE_COPY_FILE_RANGE
2675 struct pathname fpath1;
2676 struct pathname fpath2;
2677 struct stat64 stat1;
2678 struct stat64 stat2;
2679 char inoinfo1[1024];
2680 char inoinfo2[1024];
2698 init_pathname(&fpath1);
2699 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2701 printf("%d/%d: copyrange read - no filename\n",
2706 init_pathname(&fpath2);
2707 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2709 printf("%d/%d: copyrange write - no filename\n",
2715 fd1 = open_path(&fpath1, O_RDONLY);
2716 e = fd1 < 0 ? errno : 0;
2720 printf("%d/%d: copyrange read - open %s failed %d\n",
2721 procid, opno, fpath1.path, e);
2725 fd2 = open_path(&fpath2, O_WRONLY);
2726 e = fd2 < 0 ? errno : 0;
2730 printf("%d/%d: copyrange write - open %s failed %d\n",
2731 procid, opno, fpath2.path, e);
2735 /* Get file stats */
2736 if (fstat64(fd1, &stat1) < 0) {
2738 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2739 procid, opno, fpath1.path, errno);
2742 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2744 if (fstat64(fd2, &stat2) < 0) {
2746 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2747 procid, opno, fpath2.path, errno);
2750 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2752 /* Calculate offsets */
2753 len = (random() % FILELEN_MAX) + 1;
2755 len = stat1.st_blksize;
2756 if (len > stat1.st_size)
2757 len = stat1.st_size;
2759 lr = ((int64_t)random() << 32) + random();
2760 if (stat1.st_size == len)
2763 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2767 * If srcfile == destfile, randomly generate destination ranges
2768 * until we find one that doesn't overlap the source range.
2770 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2772 lr = ((int64_t)random() << 32) + random();
2773 off2 = (off64_t)(lr % max_off2);
2775 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2778 * Since len, off1 and off2 will be changed later, preserve their
2786 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2789 if (errno != EAGAIN || tries++ >= 300)
2791 } else if (ret > len || ret == 0)
2796 e = ret < 0 ? errno : 0;
2798 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2800 fpath1.path, inoinfo1,
2801 (long long)offset1, (long long)length,
2802 fpath2.path, inoinfo2,
2803 (long long)offset2, (long long)length);
2806 printf(" error %d", e);
2807 else if (len && ret > len)
2808 printf(" asked for %lld, copied %lld??\n",
2809 (long long)len, (long long)ret);
2818 free_pathname(&fpath2);
2820 free_pathname(&fpath1);
2824 /* dedupe some arbitrary range of f1 to f2...fn. */
2830 #ifdef FIDEDUPERANGE
2831 #define INFO_SZ 1024
2832 struct file_dedupe_range *fdr;
2833 struct pathname *fpath;
2834 struct stat64 *stat;
2846 if (flist[FT_REG].nfiles < 2)
2849 /* Pick somewhere between 2 and 128 files. */
2851 nr = random() % (flist[FT_REG].nfiles + 1);
2852 } while (nr < 2 || nr > 128);
2855 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2856 sizeof(struct file_dedupe_range));
2858 printf("%d/%d: line %d error %d\n",
2859 procid, opno, __LINE__, errno);
2862 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2863 sizeof(struct file_dedupe_range)));
2865 fpath = calloc(nr, sizeof(struct pathname));
2867 printf("%d/%d: line %d error %d\n",
2868 procid, opno, __LINE__, errno);
2872 stat = calloc(nr, sizeof(struct stat64));
2874 printf("%d/%d: line %d error %d\n",
2875 procid, opno, __LINE__, errno);
2879 info = calloc(nr, INFO_SZ);
2881 printf("%d/%d: line %d error %d\n",
2882 procid, opno, __LINE__, errno);
2886 off = calloc(nr, sizeof(off64_t));
2888 printf("%d/%d: line %d error %d\n",
2889 procid, opno, __LINE__, errno);
2893 v = calloc(nr, sizeof(int));
2895 printf("%d/%d: line %d error %d\n",
2896 procid, opno, __LINE__, errno);
2899 fd = calloc(nr, sizeof(int));
2901 printf("%d/%d: line %d error %d\n",
2902 procid, opno, __LINE__, errno);
2905 memset(fd, 0xFF, nr * sizeof(int));
2907 /* Get paths for all files */
2908 for (i = 0; i < nr; i++)
2909 init_pathname(&fpath[i]);
2911 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2913 printf("%d/%d: deduperange read - no filename\n",
2918 for (i = 1; i < nr; i++) {
2919 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2921 printf("%d/%d: deduperange write - no filename\n",
2928 fd[0] = open_path(&fpath[0], O_RDONLY);
2929 e = fd[0] < 0 ? errno : 0;
2933 printf("%d/%d: deduperange read - open %s failed %d\n",
2934 procid, opno, fpath[0].path, e);
2938 for (i = 1; i < nr; i++) {
2939 fd[i] = open_path(&fpath[i], O_WRONLY);
2940 e = fd[i] < 0 ? errno : 0;
2944 printf("%d/%d: deduperange write - open %s failed %d\n",
2945 procid, opno, fpath[i].path, e);
2950 /* Get file stats */
2951 if (fstat64(fd[0], &stat[0]) < 0) {
2953 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2954 procid, opno, fpath[0].path, errno);
2958 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2960 for (i = 1; i < nr; i++) {
2961 if (fstat64(fd[i], &stat[i]) < 0) {
2963 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2964 procid, opno, fpath[i].path, errno);
2967 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2970 /* Never try to dedupe more than half of the src file. */
2971 len = (random() % FILELEN_MAX) + 1;
2972 len &= ~(stat[0].st_blksize - 1);
2974 len = stat[0].st_blksize / 2;
2975 if (len > stat[0].st_size / 2)
2976 len = stat[0].st_size / 2;
2978 /* Calculate offsets */
2979 lr = ((int64_t)random() << 32) + random();
2980 if (stat[0].st_size == len)
2983 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2985 off[0] &= ~(stat[0].st_blksize - 1);
2988 * If srcfile == destfile[i], randomly generate destination ranges
2989 * until we find one that doesn't overlap the source range.
2991 for (i = 1; i < nr; i++) {
2995 lr = ((int64_t)random() << 32) + random();
2996 if (stat[i].st_size <= len)
2999 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
3001 off[i] &= ~(stat[i].st_blksize - 1);
3002 } while (stat[0].st_ino == stat[i].st_ino &&
3003 llabs(off[i] - off[0]) < len &&
3007 /* Clone data blocks */
3008 fdr->src_offset = off[0];
3009 fdr->src_length = len;
3010 fdr->dest_count = nr - 1;
3011 for (i = 1; i < nr; i++) {
3012 fdr->info[i - 1].dest_fd = fd[i];
3013 fdr->info[i - 1].dest_offset = off[i];
3016 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3017 e = ret < 0 ? errno : 0;
3019 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
3021 fpath[0].path, &info[0], (long long)off[0],
3024 printf(" error %d", e);
3030 for (i = 1; i < nr; i++) {
3031 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3033 printf("%d/%d: ...to %s%s [%lld,%lld]",
3035 fpath[i].path, &info[i * INFO_SZ],
3036 (long long)off[i], (long long)len);
3037 if (fdr->info[i - 1].status < 0)
3038 printf(" error %d", e);
3039 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3040 printf(" %llu bytes deduplicated",
3041 fdr->info[i - 1].bytes_deduped);
3042 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3043 printf(" differed");
3049 for (i = 0; i < nr; i++)
3053 for (i = 0; i < nr; i++)
3054 free_pathname(&fpath[i]);
3073 setxattr_f(int opno, long r)
3075 #ifdef XFS_XFLAG_EXTSIZE
3085 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3086 append_pathname(&f, ".");
3087 fd = open_path(&f, O_RDWR);
3088 e = fd < 0 ? errno : 0;
3093 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3094 nbits = (int)(random() % e);
3095 p &= (1 << nbits) - 1;
3097 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3099 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3102 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3109 splice_f(int opno, long r)
3111 struct pathname fpath1;
3112 struct pathname fpath2;
3113 struct stat64 stat1;
3114 struct stat64 stat2;
3115 char inoinfo1[1024];
3116 char inoinfo2[1024];
3120 loff_t offset1, offset2;
3127 ssize_t ret1 = 0, ret2 = 0;
3133 init_pathname(&fpath1);
3134 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3136 printf("%d/%d: splice read - no filename\n",
3141 init_pathname(&fpath2);
3142 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3144 printf("%d/%d: splice write - no filename\n",
3150 fd1 = open_path(&fpath1, O_RDONLY);
3151 e = fd1 < 0 ? errno : 0;
3155 printf("%d/%d: splice read - open %s failed %d\n",
3156 procid, opno, fpath1.path, e);
3160 fd2 = open_path(&fpath2, O_WRONLY);
3161 e = fd2 < 0 ? errno : 0;
3165 printf("%d/%d: splice write - open %s failed %d\n",
3166 procid, opno, fpath2.path, e);
3170 /* Get file stats */
3171 if (fstat64(fd1, &stat1) < 0) {
3173 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3174 procid, opno, fpath1.path, errno);
3177 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3179 if (fstat64(fd2, &stat2) < 0) {
3181 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3182 procid, opno, fpath2.path, errno);
3185 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3187 /* Calculate offsets */
3188 len = (random() % FILELEN_MAX) + 1;
3190 len = stat1.st_blksize;
3191 if (len > stat1.st_size)
3192 len = stat1.st_size;
3194 lr = ((int64_t)random() << 32) + random();
3195 if (stat1.st_size == len)
3198 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3202 * splice can overlap write, so the offset of the target file can be
3203 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3204 * past the current dest file EOF
3206 lr = ((int64_t)random() << 32) + random();
3207 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3210 * Since len, off1 and off2 will be changed later, preserve their
3217 /* Pipe initialize */
3218 if (pipe(filedes) < 0) {
3220 printf("%d/%d: splice - pipe failed %d\n",
3221 procid, opno, errno);
3229 /* move to pipe buffer */
3230 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3236 /* move from pipe buffer to dst file */
3238 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3251 if (ret1 < 0 || ret2 < 0)
3256 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3258 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3259 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3261 if (length && length > total)
3262 printf(" asked for %lld, spliced %lld??\n",
3263 (long long)length, (long long)total);
3274 free_pathname(&fpath2);
3276 free_pathname(&fpath1);
3280 creat_f(int opno, long r)
3295 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3300 e1 = (random() % 100);
3301 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3303 if (type == FT_RTF) /* rt always gets an extsize */
3304 extsize = (random() % 10) + 1;
3305 else if (e1 < 10) /* one-in-ten get an extsize */
3306 extsize = random() % 1024;
3310 e = generate_fname(fep, type, &f, &id, &v);
3314 (void)fent_to_name(&f, fep);
3315 printf("%d/%d: creat - no filename from %s\n",
3316 procid, opno, f.path);
3321 fd = creat_path(&f, 0666);
3322 e = fd < 0 ? errno : 0;
3327 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3328 if (type == FT_RTF) {
3329 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3330 a.fsx_extsize = extsize *
3331 geom.rtextsize * geom.blocksize;
3333 } else if (extsize) {
3334 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3335 a.fsx_extsize = extsize * geom.blocksize;
3338 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3341 add_to_flist(type, id, parid, 0);
3345 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3346 extsize ? a.fsx_extsize : 0, e, e1);
3347 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3353 dread_f(int opno, long r)
3357 struct dioattr diob;
3370 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3372 printf("%d/%d: dread - no filename\n", procid, opno);
3376 fd = open_path(&f, O_RDONLY|O_DIRECT);
3377 e = fd < 0 ? errno : 0;
3381 printf("%d/%d: dread - open %s failed %d\n",
3382 procid, opno, f.path, e);
3386 if (fstat64(fd, &stb) < 0) {
3388 printf("%d/%d: dread - fstat64 %s failed %d\n",
3389 procid, opno, f.path, errno);
3394 inode_info(st, sizeof(st), &stb, v);
3395 if (stb.st_size == 0) {
3397 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3403 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3406 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3407 " fallback to stat()\n",
3408 procid, opno, f.path, st, errno);
3409 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3410 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3413 dio_env = getenv("XFS_DIO_MIN");
3415 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3417 align = (int64_t)diob.d_miniosz;
3418 lr = ((int64_t)random() << 32) + random();
3419 off = (off64_t)(lr % stb.st_size);
3420 off -= (off % align);
3421 lseek64(fd, off, SEEK_SET);
3422 len = (random() % FILELEN_MAX) + 1;
3423 len -= (len % align);
3426 else if (len > diob.d_maxiosz)
3427 len = diob.d_maxiosz;
3428 buf = memalign(diob.d_mem, len);
3429 e = read(fd, buf, len) < 0 ? errno : 0;
3432 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3433 procid, opno, f.path, st, (long long)off, (int)len, e);
3439 dwrite_f(int opno, long r)
3443 struct dioattr diob;
3456 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3458 printf("%d/%d: dwrite - no filename\n", procid, opno);
3462 fd = open_path(&f, O_WRONLY|O_DIRECT);
3463 e = fd < 0 ? errno : 0;
3467 printf("%d/%d: dwrite - open %s failed %d\n",
3468 procid, opno, f.path, e);
3472 if (fstat64(fd, &stb) < 0) {
3474 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3475 procid, opno, f.path, errno);
3480 inode_info(st, sizeof(st), &stb, v);
3481 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3483 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3484 " %s%s return %d, fallback to stat()\n",
3485 procid, opno, f.path, st, errno);
3486 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3487 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3490 dio_env = getenv("XFS_DIO_MIN");
3492 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3494 align = (int64_t)diob.d_miniosz;
3495 lr = ((int64_t)random() << 32) + random();
3496 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3497 off -= (off % align);
3498 lseek64(fd, off, SEEK_SET);
3499 len = (random() % FILELEN_MAX) + 1;
3500 len -= (len % align);
3503 else if (len > diob.d_maxiosz)
3504 len = diob.d_maxiosz;
3505 buf = memalign(diob.d_mem, len);
3507 lseek64(fd, off, SEEK_SET);
3508 memset(buf, nameseq & 0xff, len);
3509 e = write(fd, buf, len) < 0 ? errno : 0;
3512 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3513 procid, opno, f.path, st, (long long)off, (int)len, e);
3519 #ifdef HAVE_LINUX_FALLOC_H
3520 struct print_flags falloc_flags [] = {
3521 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3522 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3523 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3524 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3525 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3526 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3530 #define translate_falloc_flags(mode) \
3531 ({translate_flags(mode, "|", falloc_flags);})
3535 do_fallocate(int opno, long r, int mode)
3537 #ifdef HAVE_LINUX_FALLOC_H
3549 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3551 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3555 fd = open_path(&f, O_RDWR);
3558 printf("%d/%d: do_fallocate - open %s failed %d\n",
3559 procid, opno, f.path, errno);
3564 if (fstat64(fd, &stb) < 0) {
3566 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3567 procid, opno, f.path, errno);
3572 inode_info(st, sizeof(st), &stb, v);
3573 lr = ((int64_t)random() << 32) + random();
3574 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3576 len = (off64_t)(random() % (1024 * 1024));
3578 * Collapse/insert range requires off and len to be block aligned,
3579 * make it more likely to be the case.
3581 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3583 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3584 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3586 mode |= FALLOC_FL_KEEP_SIZE & random();
3587 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3589 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3590 procid, opno, translate_falloc_flags(mode),
3591 f.path, st, (long long)off, (long long)len, e);
3598 fallocate_f(int opno, long r)
3600 #ifdef HAVE_LINUX_FALLOC_H
3601 do_fallocate(opno, r, 0);
3606 fdatasync_f(int opno, long r)
3614 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3616 printf("%d/%d: fdatasync - no filename\n",
3621 fd = open_path(&f, O_WRONLY);
3622 e = fd < 0 ? errno : 0;
3626 printf("%d/%d: fdatasync - open %s failed %d\n",
3627 procid, opno, f.path, e);
3631 e = fdatasync(fd) < 0 ? errno : 0;
3633 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3638 #ifdef HAVE_LINUX_FIEMAP_H
3639 struct print_flags fiemap_flags[] = {
3640 { FIEMAP_FLAG_SYNC, "SYNC"},
3641 { FIEMAP_FLAG_XATTR, "XATTR"},
3645 #define translate_fiemap_flags(mode) \
3646 ({translate_flags(mode, "|", fiemap_flags);})
3650 fiemap_f(int opno, long r)
3652 #ifdef HAVE_LINUX_FIEMAP_H
3662 struct fiemap *fiemap;
3665 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3667 printf("%d/%d: fiemap - no filename\n", procid, opno);
3671 fd = open_path(&f, O_RDWR);
3672 e = fd < 0 ? errno : 0;
3676 printf("%d/%d: fiemap - open %s failed %d\n",
3677 procid, opno, f.path, e);
3681 if (fstat64(fd, &stb) < 0) {
3683 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3684 procid, opno, f.path, errno);
3689 inode_info(st, sizeof(st), &stb, v);
3690 blocks_to_map = random() & 0xffff;
3691 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3692 (blocks_to_map * sizeof(struct fiemap_extent)));
3695 printf("%d/%d: malloc failed \n", procid, opno);
3700 lr = ((int64_t)random() << 32) + random();
3701 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3703 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3704 fiemap->fm_extent_count = blocks_to_map;
3705 fiemap->fm_mapped_extents = random() & 0xffff;
3706 fiemap->fm_start = off;
3707 fiemap->fm_length = ((int64_t)random() << 32) + random();
3709 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3711 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3712 procid, opno, f.path, st, (long long)fiemap->fm_start,
3713 (long long) fiemap->fm_length,
3714 translate_fiemap_flags(fiemap->fm_flags), e);
3722 freesp_f(int opno, long r)
3727 struct xfs_flock64 fl;
3735 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3737 printf("%d/%d: freesp - no filename\n", procid, opno);
3741 fd = open_path(&f, O_RDWR);
3742 e = fd < 0 ? errno : 0;
3746 printf("%d/%d: freesp - open %s failed %d\n",
3747 procid, opno, f.path, e);
3751 if (fstat64(fd, &stb) < 0) {
3753 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3754 procid, opno, f.path, errno);
3759 inode_info(st, sizeof(st), &stb, v);
3760 lr = ((int64_t)random() << 32) + random();
3761 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3763 fl.l_whence = SEEK_SET;
3766 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3768 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3769 procid, opno, f.path, st, (long long)off, e);
3775 fsync_f(int opno, long r)
3783 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3785 printf("%d/%d: fsync - no filename\n", procid, opno);
3789 fd = open_file_or_dir(&f, O_WRONLY);
3790 e = fd < 0 ? errno : 0;
3794 printf("%d/%d: fsync - open %s failed %d\n",
3795 procid, opno, f.path, e);
3799 e = fsync(fd) < 0 ? errno : 0;
3801 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3807 gen_random_string(int len)
3809 static const char charset[] = "0123456789"
3810 "abcdefghijklmnopqrstuvwxyz"
3811 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3822 for (i = 0; i < len; i++)
3823 s[i] = charset[random() % sizeof(charset)];
3829 getattr_f(int opno, long r)
3838 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3839 append_pathname(&f, ".");
3840 fd = open_path(&f, O_RDWR);
3841 e = fd < 0 ? errno : 0;
3844 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3846 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3852 getdents_f(int opno, long r)
3859 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3860 append_pathname(&f, ".");
3861 dir = opendir_path(&f);
3865 printf("%d/%d: getdents - can't open %s\n",
3866 procid, opno, f.path);
3870 while (readdir64(dir) != NULL)
3873 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3879 getfattr_f(int opno, long r)
3885 char name[XATTR_NAME_BUF_SIZE];
3891 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3893 printf("%d/%d: getfattr - no filename\n", procid, opno);
3899 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3900 * to read a xattr that doesn't exist (fgetxattr should fail with
3901 * errno set to ENOATTR (61) in this case).
3903 if (fep->xattr_counter > 0)
3904 xattr_num = (random() % fep->xattr_counter) + 1;
3908 e = generate_xattr_name(xattr_num, name, sizeof(name));
3910 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3911 procid, opno, f.path, e);
3915 value_len = getxattr(f.path, name, NULL, 0);
3916 if (value_len < 0) {
3918 printf("%d/%d: getfattr file %s name %s failed %d\n",
3919 procid, opno, f.path, name, errno);
3923 /* A xattr without value.*/
3924 if (value_len == 0) {
3929 value = malloc(value_len);
3932 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3933 procid, opno, f.path, value_len);
3937 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3940 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3941 procid, opno, f.path, name, value_len, e);
3948 link_f(int opno, long r)
3962 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3964 printf("%d/%d: link - no file\n", procid, opno);
3968 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3974 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3978 (void)fent_to_name(&l, fep);
3979 printf("%d/%d: link - no filename from %s\n",
3980 procid, opno, l.path);
3986 e = link_path(&f, &l) < 0 ? errno : 0;
3989 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3991 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3993 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
4000 listfattr_f(int opno, long r)
4006 char *buffer = NULL;
4010 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4012 printf("%d/%d: listfattr - no filename\n", procid, opno);
4017 e = listxattr(f.path, NULL, 0);
4020 printf("%d/%d: listfattr %s failed %d\n",
4021 procid, opno, f.path, errno);
4025 if (buffer_len == 0) {
4027 printf("%d/%d: listfattr %s - has no extended attributes\n",
4028 procid, opno, f.path);
4032 buffer = malloc(buffer_len);
4035 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
4036 procid, opno, f.path, buffer_len);
4040 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4042 printf("%d/%d: listfattr %s buffer length %d %d\n",
4043 procid, opno, f.path, buffer_len, e);
4050 mkdir_f(int opno, long r)
4060 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4065 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4069 (void)fent_to_name(&f, fep);
4070 printf("%d/%d: mkdir - no filename from %s\n",
4071 procid, opno, f.path);
4076 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4079 add_to_flist(FT_DIR, id, parid, 0);
4081 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
4082 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4088 mknod_f(int opno, long r)
4098 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4103 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4107 (void)fent_to_name(&f, fep);
4108 printf("%d/%d: mknod - no filename from %s\n",
4109 procid, opno, f.path);
4114 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4117 add_to_flist(FT_DEV, id, parid, 0);
4119 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
4120 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4125 #ifdef HAVE_SYS_MMAN_H
4126 struct print_flags mmap_flags[] = {
4127 { MAP_SHARED, "SHARED"},
4128 { MAP_PRIVATE, "PRIVATE"},
4132 #define translate_mmap_flags(flags) \
4133 ({translate_flags(flags, "|", mmap_flags);})
4137 do_mmap(int opno, long r, int prot)
4139 #ifdef HAVE_SYS_MMAN_H
4151 sigjmp_buf sigbus_jmpbuf;
4154 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4156 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4160 fd = open_path(&f, O_RDWR);
4161 e = fd < 0 ? errno : 0;
4165 printf("%d/%d: do_mmap - open %s failed %d\n",
4166 procid, opno, f.path, e);
4170 if (fstat64(fd, &stb) < 0) {
4172 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4173 procid, opno, f.path, errno);
4178 inode_info(st, sizeof(st), &stb, v);
4179 if (stb.st_size == 0) {
4181 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4188 lr = ((int64_t)random() << 32) + random();
4189 off = (off64_t)(lr % stb.st_size);
4190 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4191 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4193 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4194 addr = mmap(NULL, len, prot, flags, fd, off);
4195 e = (addr == MAP_FAILED) ? errno : 0;
4198 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4199 procid, opno, f.path, st, (long long)off,
4200 (int)len, translate_mmap_flags(flags), e);
4206 if (prot & PROT_WRITE) {
4207 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4208 sigbus_jmp = &sigbus_jmpbuf;
4209 memset(addr, nameseq & 0xff, len);
4213 if ((buf = malloc(len)) != NULL) {
4214 memcpy(buf, addr, len);
4219 /* set NULL to stop other functions from doing siglongjmp */
4223 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4224 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4225 f.path, st, (long long)off, (int)len,
4226 translate_mmap_flags(flags),
4227 (e == 0) ? "0" : "Bus error");
4235 mread_f(int opno, long r)
4237 #ifdef HAVE_SYS_MMAN_H
4238 do_mmap(opno, r, PROT_READ);
4243 mwrite_f(int opno, long r)
4245 #ifdef HAVE_SYS_MMAN_H
4246 do_mmap(opno, r, PROT_WRITE);
4251 punch_f(int opno, long r)
4253 #ifdef HAVE_LINUX_FALLOC_H
4254 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4259 zero_f(int opno, long r)
4261 #ifdef HAVE_LINUX_FALLOC_H
4262 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4267 collapse_f(int opno, long r)
4269 #ifdef HAVE_LINUX_FALLOC_H
4270 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4275 insert_f(int opno, long r)
4277 #ifdef HAVE_LINUX_FALLOC_H
4278 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4283 read_f(int opno, long r)
4297 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4299 printf("%d/%d: read - no filename\n", procid, opno);
4303 fd = open_path(&f, O_RDONLY);
4304 e = fd < 0 ? errno : 0;
4308 printf("%d/%d: read - open %s failed %d\n",
4309 procid, opno, f.path, e);
4313 if (fstat64(fd, &stb) < 0) {
4315 printf("%d/%d: read - fstat64 %s failed %d\n",
4316 procid, opno, f.path, errno);
4321 inode_info(st, sizeof(st), &stb, v);
4322 if (stb.st_size == 0) {
4324 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4330 lr = ((int64_t)random() << 32) + random();
4331 off = (off64_t)(lr % stb.st_size);
4332 lseek64(fd, off, SEEK_SET);
4333 len = (random() % FILELEN_MAX) + 1;
4335 e = read(fd, buf, len) < 0 ? errno : 0;
4338 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4339 procid, opno, f.path, st, (long long)off, (int)len, e);
4345 readlink_f(int opno, long r)
4353 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4355 printf("%d/%d: readlink - no filename\n", procid, opno);
4359 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4362 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4367 readv_f(int opno, long r)
4379 struct iovec *iov = NULL;
4386 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4388 printf("%d/%d: readv - no filename\n", procid, opno);
4392 fd = open_path(&f, O_RDONLY);
4393 e = fd < 0 ? errno : 0;
4397 printf("%d/%d: readv - open %s failed %d\n",
4398 procid, opno, f.path, e);
4402 if (fstat64(fd, &stb) < 0) {
4404 printf("%d/%d: readv - fstat64 %s failed %d\n",
4405 procid, opno, f.path, errno);
4410 inode_info(st, sizeof(st), &stb, v);
4411 if (stb.st_size == 0) {
4413 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4419 lr = ((int64_t)random() << 32) + random();
4420 off = (off64_t)(lr % stb.st_size);
4421 lseek64(fd, off, SEEK_SET);
4422 len = (random() % FILELEN_MAX) + 1;
4425 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4426 iov = calloc(iovcnt, sizeof(struct iovec));
4427 iovl = len / iovcnt;
4429 for (i=0; i<iovcnt; i++) {
4430 (iov + i)->iov_base = (buf + iovb);
4431 (iov + i)->iov_len = iovl;
4435 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4438 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4439 procid, opno, f.path, st, (long long)off, (int)iovl,
4446 removefattr_f(int opno, long r)
4452 char name[XATTR_NAME_BUF_SIZE];
4456 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4458 printf("%d/%d: removefattr - no filename\n", procid, opno);
4464 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4465 * remove a xattr that doesn't exist (fremovexattr should fail with
4466 * errno set to ENOATTR (61) in this case).
4468 if (fep->xattr_counter > 0)
4469 xattr_num = (random() % fep->xattr_counter) + 1;
4473 e = generate_xattr_name(xattr_num, name, sizeof(name));
4475 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4476 procid, opno, f.path, e);
4480 e = removexattr(f.path, name) < 0 ? errno : 0;
4482 printf("%d/%d: removefattr file %s name %s %d\n",
4483 procid, opno, f.path, name, e);
4488 struct print_flags renameat2_flags [] = {
4489 { RENAME_NOREPLACE, "NOREPLACE"},
4490 { RENAME_EXCHANGE, "EXCHANGE"},
4491 { RENAME_WHITEOUT, "WHITEOUT"},
4495 #define translate_renameat2_flags(mode) \
4496 ({translate_flags(mode, "|", renameat2_flags);})
4499 do_renameat2(int opno, long r, int mode)
4515 /* get an existing path for the source of the rename */
4517 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4518 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4520 printf("%d/%d: rename - no source filename\n",
4527 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4528 * order to maintain filelist/filename integrity, we should
4529 * restrict exchange operation to files of the same type.
4531 if (mode == RENAME_EXCHANGE) {
4532 which = 1 << (flp - flist);
4533 init_pathname(&newf);
4534 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4536 printf("%d/%d: rename - no target filename\n",
4538 free_pathname(&newf);
4542 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4543 fents_ancestor_check(dfep, fep))) {
4545 printf("%d/%d: rename(REXCHANGE) %s and %s "
4546 "have ancestor-descendant relationship\n",
4547 procid, opno, f.path, newf.path);
4548 free_pathname(&newf);
4554 parid = dfep->parent;
4557 * Get an existing directory for the destination parent
4560 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4567 * Generate a new path using an existing parent directory
4570 init_pathname(&newf);
4571 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4575 (void)fent_to_name(&f, dfep);
4576 printf("%d/%d: rename - no filename from %s\n",
4577 procid, opno, f.path);
4579 free_pathname(&newf);
4584 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4587 int xattr_counter = fep->xattr_counter;
4588 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4589 int ft = flp - flist;
4592 oldparid = fep->parent;
4595 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4596 * old parent id for the others.
4598 if (ft == FT_DIR || ft == FT_SUBVOL)
4599 fix_parent(oldid, id, swap);
4601 if (mode == RENAME_WHITEOUT) {
4602 fep->xattr_counter = 0;
4603 add_to_flist(flp - flist, id, parid, xattr_counter);
4604 } else if (mode == RENAME_EXCHANGE) {
4605 fep->xattr_counter = dfep->xattr_counter;
4606 dfep->xattr_counter = xattr_counter;
4608 del_from_flist(flp - flist, fep - flp->fents);
4609 add_to_flist(flp - flist, id, parid, xattr_counter);
4613 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4614 opno, translate_renameat2_flags(mode), f.path,
4617 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4618 procid, opno, oldid, oldparid);
4619 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4620 procid, opno, id, parid);
4623 free_pathname(&newf);
4628 rename_f(int opno, long r)
4630 do_renameat2(opno, r, 0);
4634 rnoreplace_f(int opno, long r)
4636 do_renameat2(opno, r, RENAME_NOREPLACE);
4640 rexchange_f(int opno, long r)
4642 do_renameat2(opno, r, RENAME_EXCHANGE);
4646 rwhiteout_f(int opno, long r)
4648 do_renameat2(opno, r, RENAME_WHITEOUT);
4652 resvsp_f(int opno, long r)
4657 struct xfs_flock64 fl;
4665 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4667 printf("%d/%d: resvsp - no filename\n", procid, opno);
4671 fd = open_path(&f, O_RDWR);
4672 e = fd < 0 ? errno : 0;
4676 printf("%d/%d: resvsp - open %s failed %d\n",
4677 procid, opno, f.path, e);
4681 if (fstat64(fd, &stb) < 0) {
4683 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4684 procid, opno, f.path, errno);
4689 inode_info(st, sizeof(st), &stb, v);
4690 lr = ((int64_t)random() << 32) + random();
4691 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4693 fl.l_whence = SEEK_SET;
4695 fl.l_len = (off64_t)(random() % (1024 * 1024));
4696 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4698 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4699 procid, opno, f.path, st,
4700 (long long)off, (long long)fl.l_len, e);
4706 rmdir_f(int opno, long r)
4716 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4718 printf("%d/%d: rmdir - no directory\n", procid, opno);
4722 e = rmdir_path(&f) < 0 ? errno : 0;
4726 oldparid = fep->parent;
4727 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4730 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4732 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4733 procid, opno, oldid, oldparid);
4739 setattr_f(int opno, long r)
4748 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4749 append_pathname(&f, ".");
4750 fd = open_path(&f, O_RDWR);
4751 e = fd < 0 ? errno : 0;
4754 fl = attr_mask & (uint)random();
4755 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4757 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4763 setfattr_f(int opno, long r)
4770 char name[XATTR_NAME_BUF_SIZE];
4776 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4778 printf("%d/%d: setfattr - no filename\n", procid, opno);
4783 if ((fep->xattr_counter > 0) && (random() % 2)) {
4785 * Use an existing xattr name for replacing its value or
4786 * create again a xattr that was previously deleted.
4788 xattr_num = (random() % fep->xattr_counter) + 1;
4790 flag = XATTR_REPLACE;
4792 /* Use a new xattr name. */
4793 xattr_num = fep->xattr_counter + 1;
4795 * Don't always use the create flag because even if our xattr
4796 * counter is 0, we may still have xattrs associated to this
4797 * file (this happens when xattrs are added to a file through
4798 * one of its other hard links), so we can end up updating an
4799 * existing xattr too.
4802 flag = XATTR_CREATE;
4806 * The maximum supported value size depends on the filesystem
4807 * implementation, but 100 bytes is a safe value for most filesystems
4810 value_len = random() % 101;
4811 value = gen_random_string(value_len);
4812 if (!value && value_len > 0) {
4814 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4815 procid, opno, f.path, value_len);
4818 e = generate_xattr_name(xattr_num, name, sizeof(name));
4820 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4821 procid, opno, f.path, e);
4825 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4827 fep->xattr_counter++;
4829 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4830 procid, opno, f.path, name, xattr_flag_to_string(flag),
4838 snapshot_f(int opno, long r)
4840 #ifdef HAVE_BTRFSUTIL_H
4841 enum btrfs_util_error e;
4852 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4854 printf("%d/%d: snapshot - no subvolume\n", procid,
4859 init_pathname(&newf);
4861 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4865 (void)fent_to_name(&f, fep);
4866 printf("%d/%d: snapshot - no filename from %s\n",
4867 procid, opno, f.path);
4872 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4873 if (e == BTRFS_UTIL_OK)
4874 add_to_flist(FT_SUBVOL, id, parid, 0);
4876 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4877 f.path, newf.path, e, btrfs_util_strerror(e));
4878 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4881 free_pathname(&newf);
4887 stat_f(int opno, long r)
4895 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4897 printf("%d/%d: stat - no entries\n", procid, opno);
4901 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4904 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4909 subvol_create_f(int opno, long r)
4911 #ifdef HAVE_BTRFSUTIL_H
4912 enum btrfs_util_error e;
4922 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4926 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4930 (void)fent_to_name(&f, fep);
4931 printf("%d/%d: subvol_create - no filename from %s\n",
4932 procid, opno, f.path);
4937 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4938 if (e == BTRFS_UTIL_OK)
4939 add_to_flist(FT_SUBVOL, id, parid, 0);
4941 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4942 f.path, e, btrfs_util_strerror(e));
4943 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4951 subvol_delete_f(int opno, long r)
4953 #ifdef HAVE_BTRFSUTIL_H
4954 enum btrfs_util_error e;
4962 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4964 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4969 e = btrfs_util_delete_subvolume(f.path, 0);
4971 if (e == BTRFS_UTIL_OK) {
4973 oldparid = fep->parent;
4974 delete_subvol_children(oldid);
4975 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4978 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4979 e, btrfs_util_strerror(e));
4980 if (e == BTRFS_UTIL_OK)
4981 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4982 procid, opno, oldid, oldparid);
4989 symlink_f(int opno, long r)
5002 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5007 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5011 (void)fent_to_name(&f, fep);
5012 printf("%d/%d: symlink - no filename from %s\n",
5013 procid, opno, f.path);
5018 len = (int)(random() % PATH_MAX);
5019 val = malloc(len + 1);
5021 memset(val, 'x', len);
5023 for (i = 10; i < len - 1; i += 10)
5025 e = symlink_path(val, &f) < 0 ? errno : 0;
5028 add_to_flist(FT_SYM, id, parid, 0);
5031 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
5032 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5039 sync_f(int opno, long r)
5043 printf("%d/%d: sync\n", procid, opno);
5047 truncate_f(int opno, long r)
5058 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5060 printf("%d/%d: truncate - no filename\n", procid, opno);
5064 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5068 printf("%d/%d: truncate - stat64 %s failed %d\n",
5069 procid, opno, f.path, e);
5073 inode_info(st, sizeof(st), &stb, v);
5074 lr = ((int64_t)random() << 32) + random();
5075 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5077 e = truncate64_path(&f, off) < 0 ? errno : 0;
5080 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
5081 st, (long long)off, e);
5086 unlink_f(int opno, long r)
5097 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5099 printf("%d/%d: unlink - no file\n", procid, opno);
5103 e = unlink_path(&f) < 0 ? errno : 0;
5107 oldparid = fep->parent;
5108 del_from_flist(flp - flist, fep - flp->fents);
5111 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
5113 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
5114 procid, opno, oldid, oldparid);
5120 unresvsp_f(int opno, long r)
5125 struct xfs_flock64 fl;
5133 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5135 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5139 fd = open_path(&f, O_RDWR);
5140 e = fd < 0 ? errno : 0;
5144 printf("%d/%d: unresvsp - open %s failed %d\n",
5145 procid, opno, f.path, e);
5149 if (fstat64(fd, &stb) < 0) {
5151 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5152 procid, opno, f.path, errno);
5157 inode_info(st, sizeof(st), &stb, v);
5158 lr = ((int64_t)random() << 32) + random();
5159 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5161 fl.l_whence = SEEK_SET;
5163 fl.l_len = (off64_t)(random() % (1 << 20));
5164 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5166 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5167 procid, opno, f.path, st,
5168 (long long)off, (long long)fl.l_len, e);
5174 uring_read_f(int opno, long r)
5177 do_uring_rw(opno, r, O_RDONLY);
5182 uring_write_f(int opno, long r)
5185 do_uring_rw(opno, r, O_WRONLY);
5190 write_f(int opno, long r)
5204 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5206 printf("%d/%d: write - no filename\n", procid, opno);
5210 fd = open_path(&f, O_WRONLY);
5211 e = fd < 0 ? errno : 0;
5215 printf("%d/%d: write - open %s failed %d\n",
5216 procid, opno, f.path, e);
5220 if (fstat64(fd, &stb) < 0) {
5222 printf("%d/%d: write - fstat64 %s failed %d\n",
5223 procid, opno, f.path, errno);
5228 inode_info(st, sizeof(st), &stb, v);
5229 lr = ((int64_t)random() << 32) + random();
5230 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5232 lseek64(fd, off, SEEK_SET);
5233 len = (random() % FILELEN_MAX) + 1;
5235 memset(buf, nameseq & 0xff, len);
5236 e = write(fd, buf, len) < 0 ? errno : 0;
5239 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5240 procid, opno, f.path, st, (long long)off, (int)len, e);
5246 writev_f(int opno, long r)
5258 struct iovec *iov = NULL;
5265 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5267 printf("%d/%d: writev - no filename\n", procid, opno);
5271 fd = open_path(&f, O_WRONLY);
5272 e = fd < 0 ? errno : 0;
5276 printf("%d/%d: writev - open %s failed %d\n",
5277 procid, opno, f.path, e);
5281 if (fstat64(fd, &stb) < 0) {
5283 printf("%d/%d: writev - fstat64 %s failed %d\n",
5284 procid, opno, f.path, errno);
5289 inode_info(st, sizeof(st), &stb, v);
5290 lr = ((int64_t)random() << 32) + random();
5291 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5293 lseek64(fd, off, SEEK_SET);
5294 len = (random() % FILELEN_MAX) + 1;
5296 memset(buf, nameseq & 0xff, len);
5298 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5299 iov = calloc(iovcnt, sizeof(struct iovec));
5300 iovl = len / iovcnt;
5302 for (i=0; i<iovcnt; i++) {
5303 (iov + i)->iov_base = (buf + iovb);
5304 (iov + i)->iov_len = iovl;
5308 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5312 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5313 procid, opno, f.path, st, (long long)off, (int)iovl,
5320 xattr_flag_to_string(int flag)
5322 if (flag == XATTR_CREATE)
5324 if (flag == XATTR_REPLACE)