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 *, int);
398 int attr_set_path(pathname_t *, const char *, const char *, const int, 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, int flags)
894 char buf[NAME_MAX + 1];
898 rval = attr_remove(name->path, attrname, flags);
899 if (rval >= 0 || errno != ENAMETOOLONG)
901 separate_pathname(name, buf, &newname);
902 if (chdir(buf) == 0) {
903 rval = attr_remove_path(&newname, attrname, flags);
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, int flags)
914 char buf[NAME_MAX + 1];
918 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
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, valuelength,
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 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2368 printf("%d/%d: attr_remove %s %s %d\n",
2369 procid, opno, f.path, aname, e);
2374 attr_set_f(int opno, long r)
2381 static int lengths[] = { 10, 100, 1000, 10000 };
2386 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2387 append_pathname(&f, ".");
2388 sprintf(aname, "a%x", nameseq++);
2389 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2390 len = (int)(random() % lengths[li]);
2394 memset(aval, nameseq & 0xff, len);
2395 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2400 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2406 awrite_f(int opno, long r)
2409 do_aio_rw(opno, r, O_WRONLY);
2414 bulkstat_f(int opno, long r)
2420 struct xfs_bstat *t;
2422 struct xfs_fsop_bulkreq bsr;
2425 nent = (r % 999) + 2;
2426 t = malloc(nent * sizeof(*t));
2427 fd = open(".", O_RDONLY);
2435 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2439 printf("%d/%d: bulkstat nent %d total %lld\n",
2440 procid, opno, nent, (long long)total);
2445 bulkstat1_f(int opno, long r)
2455 struct xfs_fsop_bulkreq bsr;
2458 good = random() & 1;
2460 /* use an inode we know exists */
2462 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2463 append_pathname(&f, ".");
2464 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2469 * pick a random inode
2471 * note this can generate kernel warning messages
2472 * since bulkstat_one will read the disk block that
2473 * would contain a given inode even if that disk
2474 * block doesn't contain inodes.
2476 * this is detected later, but not until after the
2477 * warning is displayed.
2479 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2485 fd = open(".", O_RDONLY);
2491 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2493 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2494 procid, opno, good?"real":"random",
2495 verifiable_log ? -1LL : (long long)ino, e);
2500 chown_f(int opno, long r)
2510 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2511 append_pathname(&f, ".");
2512 u = (uid_t)random();
2513 g = (gid_t)random();
2514 nbits = (int)(random() % idmodulo);
2515 u &= (1 << nbits) - 1;
2516 g &= (1 << nbits) - 1;
2517 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2520 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2524 /* reflink some arbitrary range of f1 to f2. */
2531 struct file_clone_range fcr;
2532 struct pathname fpath1;
2533 struct pathname fpath2;
2534 struct stat64 stat1;
2535 struct stat64 stat2;
2536 char inoinfo1[1024];
2537 char inoinfo2[1024];
2551 init_pathname(&fpath1);
2552 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2554 printf("%d/%d: clonerange read - no filename\n",
2559 init_pathname(&fpath2);
2560 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2562 printf("%d/%d: clonerange write - no filename\n",
2568 fd1 = open_path(&fpath1, O_RDONLY);
2569 e = fd1 < 0 ? errno : 0;
2573 printf("%d/%d: clonerange read - open %s failed %d\n",
2574 procid, opno, fpath1.path, e);
2578 fd2 = open_path(&fpath2, O_WRONLY);
2579 e = fd2 < 0 ? errno : 0;
2583 printf("%d/%d: clonerange write - open %s failed %d\n",
2584 procid, opno, fpath2.path, e);
2588 /* Get file stats */
2589 if (fstat64(fd1, &stat1) < 0) {
2591 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2592 procid, opno, fpath1.path, errno);
2595 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2597 if (fstat64(fd2, &stat2) < 0) {
2599 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2600 procid, opno, fpath2.path, errno);
2603 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2605 /* Calculate offsets */
2606 len = (random() % FILELEN_MAX) + 1;
2607 len &= ~(stat1.st_blksize - 1);
2609 len = stat1.st_blksize;
2610 if (len > stat1.st_size)
2611 len = stat1.st_size;
2613 lr = ((int64_t)random() << 32) + random();
2614 if (stat1.st_size == len)
2617 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2619 off1 &= ~(stat1.st_blksize - 1);
2622 * If srcfile == destfile, randomly generate destination ranges
2623 * until we find one that doesn't overlap the source range.
2625 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2627 lr = ((int64_t)random() << 32) + random();
2628 off2 = (off64_t)(lr % max_off2);
2630 off2 &= ~(stat2.st_blksize - 1);
2631 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2633 /* Clone data blocks */
2635 fcr.src_offset = off1;
2636 fcr.src_length = len;
2637 fcr.dest_offset = off2;
2639 ret = ioctl(fd2, FICLONERANGE, &fcr);
2640 e = ret < 0 ? errno : 0;
2642 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2644 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2645 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2648 printf(" error %d", e);
2657 free_pathname(&fpath2);
2659 free_pathname(&fpath1);
2663 /* copy some arbitrary range of f1 to f2. */
2669 #ifdef HAVE_COPY_FILE_RANGE
2670 struct pathname fpath1;
2671 struct pathname fpath2;
2672 struct stat64 stat1;
2673 struct stat64 stat2;
2674 char inoinfo1[1024];
2675 char inoinfo2[1024];
2693 init_pathname(&fpath1);
2694 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2696 printf("%d/%d: copyrange read - no filename\n",
2701 init_pathname(&fpath2);
2702 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2704 printf("%d/%d: copyrange write - no filename\n",
2710 fd1 = open_path(&fpath1, O_RDONLY);
2711 e = fd1 < 0 ? errno : 0;
2715 printf("%d/%d: copyrange read - open %s failed %d\n",
2716 procid, opno, fpath1.path, e);
2720 fd2 = open_path(&fpath2, O_WRONLY);
2721 e = fd2 < 0 ? errno : 0;
2725 printf("%d/%d: copyrange write - open %s failed %d\n",
2726 procid, opno, fpath2.path, e);
2730 /* Get file stats */
2731 if (fstat64(fd1, &stat1) < 0) {
2733 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2734 procid, opno, fpath1.path, errno);
2737 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2739 if (fstat64(fd2, &stat2) < 0) {
2741 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2742 procid, opno, fpath2.path, errno);
2745 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2747 /* Calculate offsets */
2748 len = (random() % FILELEN_MAX) + 1;
2750 len = stat1.st_blksize;
2751 if (len > stat1.st_size)
2752 len = stat1.st_size;
2754 lr = ((int64_t)random() << 32) + random();
2755 if (stat1.st_size == len)
2758 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2762 * If srcfile == destfile, randomly generate destination ranges
2763 * until we find one that doesn't overlap the source range.
2765 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2767 lr = ((int64_t)random() << 32) + random();
2768 off2 = (off64_t)(lr % max_off2);
2770 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2773 * Since len, off1 and off2 will be changed later, preserve their
2781 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2784 if (errno != EAGAIN || tries++ >= 300)
2786 } else if (ret > len || ret == 0)
2791 e = ret < 0 ? errno : 0;
2793 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2795 fpath1.path, inoinfo1,
2796 (long long)offset1, (long long)length,
2797 fpath2.path, inoinfo2,
2798 (long long)offset2, (long long)length);
2801 printf(" error %d", e);
2802 else if (len && ret > len)
2803 printf(" asked for %lld, copied %lld??\n",
2804 (long long)len, (long long)ret);
2813 free_pathname(&fpath2);
2815 free_pathname(&fpath1);
2819 /* dedupe some arbitrary range of f1 to f2...fn. */
2825 #ifdef FIDEDUPERANGE
2826 #define INFO_SZ 1024
2827 struct file_dedupe_range *fdr;
2828 struct pathname *fpath;
2829 struct stat64 *stat;
2841 if (flist[FT_REG].nfiles < 2)
2844 /* Pick somewhere between 2 and 128 files. */
2846 nr = random() % (flist[FT_REG].nfiles + 1);
2847 } while (nr < 2 || nr > 128);
2850 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2851 sizeof(struct file_dedupe_range));
2853 printf("%d/%d: line %d error %d\n",
2854 procid, opno, __LINE__, errno);
2857 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2858 sizeof(struct file_dedupe_range)));
2860 fpath = calloc(nr, sizeof(struct pathname));
2862 printf("%d/%d: line %d error %d\n",
2863 procid, opno, __LINE__, errno);
2867 stat = calloc(nr, sizeof(struct stat64));
2869 printf("%d/%d: line %d error %d\n",
2870 procid, opno, __LINE__, errno);
2874 info = calloc(nr, INFO_SZ);
2876 printf("%d/%d: line %d error %d\n",
2877 procid, opno, __LINE__, errno);
2881 off = calloc(nr, sizeof(off64_t));
2883 printf("%d/%d: line %d error %d\n",
2884 procid, opno, __LINE__, errno);
2888 v = calloc(nr, sizeof(int));
2890 printf("%d/%d: line %d error %d\n",
2891 procid, opno, __LINE__, errno);
2894 fd = calloc(nr, sizeof(int));
2896 printf("%d/%d: line %d error %d\n",
2897 procid, opno, __LINE__, errno);
2900 memset(fd, 0xFF, nr * sizeof(int));
2902 /* Get paths for all files */
2903 for (i = 0; i < nr; i++)
2904 init_pathname(&fpath[i]);
2906 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2908 printf("%d/%d: deduperange read - no filename\n",
2913 for (i = 1; i < nr; i++) {
2914 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2916 printf("%d/%d: deduperange write - no filename\n",
2923 fd[0] = open_path(&fpath[0], O_RDONLY);
2924 e = fd[0] < 0 ? errno : 0;
2928 printf("%d/%d: deduperange read - open %s failed %d\n",
2929 procid, opno, fpath[0].path, e);
2933 for (i = 1; i < nr; i++) {
2934 fd[i] = open_path(&fpath[i], O_WRONLY);
2935 e = fd[i] < 0 ? errno : 0;
2939 printf("%d/%d: deduperange write - open %s failed %d\n",
2940 procid, opno, fpath[i].path, e);
2945 /* Get file stats */
2946 if (fstat64(fd[0], &stat[0]) < 0) {
2948 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2949 procid, opno, fpath[0].path, errno);
2953 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2955 for (i = 1; i < nr; i++) {
2956 if (fstat64(fd[i], &stat[i]) < 0) {
2958 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2959 procid, opno, fpath[i].path, errno);
2962 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2965 /* Never try to dedupe more than half of the src file. */
2966 len = (random() % FILELEN_MAX) + 1;
2967 len &= ~(stat[0].st_blksize - 1);
2969 len = stat[0].st_blksize / 2;
2970 if (len > stat[0].st_size / 2)
2971 len = stat[0].st_size / 2;
2973 /* Calculate offsets */
2974 lr = ((int64_t)random() << 32) + random();
2975 if (stat[0].st_size == len)
2978 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2980 off[0] &= ~(stat[0].st_blksize - 1);
2983 * If srcfile == destfile[i], randomly generate destination ranges
2984 * until we find one that doesn't overlap the source range.
2986 for (i = 1; i < nr; i++) {
2990 lr = ((int64_t)random() << 32) + random();
2991 if (stat[i].st_size <= len)
2994 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2996 off[i] &= ~(stat[i].st_blksize - 1);
2997 } while (stat[0].st_ino == stat[i].st_ino &&
2998 llabs(off[i] - off[0]) < len &&
3002 /* Clone data blocks */
3003 fdr->src_offset = off[0];
3004 fdr->src_length = len;
3005 fdr->dest_count = nr - 1;
3006 for (i = 1; i < nr; i++) {
3007 fdr->info[i - 1].dest_fd = fd[i];
3008 fdr->info[i - 1].dest_offset = off[i];
3011 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
3012 e = ret < 0 ? errno : 0;
3014 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
3016 fpath[0].path, &info[0], (long long)off[0],
3019 printf(" error %d", e);
3025 for (i = 1; i < nr; i++) {
3026 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
3028 printf("%d/%d: ...to %s%s [%lld,%lld]",
3030 fpath[i].path, &info[i * INFO_SZ],
3031 (long long)off[i], (long long)len);
3032 if (fdr->info[i - 1].status < 0)
3033 printf(" error %d", e);
3034 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
3035 printf(" %llu bytes deduplicated",
3036 fdr->info[i - 1].bytes_deduped);
3037 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
3038 printf(" differed");
3044 for (i = 0; i < nr; i++)
3048 for (i = 0; i < nr; i++)
3049 free_pathname(&fpath[i]);
3068 setxattr_f(int opno, long r)
3070 #ifdef XFS_XFLAG_EXTSIZE
3080 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3081 append_pathname(&f, ".");
3082 fd = open_path(&f, O_RDWR);
3083 e = fd < 0 ? errno : 0;
3088 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
3089 nbits = (int)(random() % e);
3090 p &= (1 << nbits) - 1;
3092 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
3094 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
3097 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
3104 splice_f(int opno, long r)
3106 struct pathname fpath1;
3107 struct pathname fpath2;
3108 struct stat64 stat1;
3109 struct stat64 stat2;
3110 char inoinfo1[1024];
3111 char inoinfo2[1024];
3115 loff_t offset1, offset2;
3122 ssize_t ret1 = 0, ret2 = 0;
3128 init_pathname(&fpath1);
3129 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3131 printf("%d/%d: splice read - no filename\n",
3136 init_pathname(&fpath2);
3137 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3139 printf("%d/%d: splice write - no filename\n",
3145 fd1 = open_path(&fpath1, O_RDONLY);
3146 e = fd1 < 0 ? errno : 0;
3150 printf("%d/%d: splice read - open %s failed %d\n",
3151 procid, opno, fpath1.path, e);
3155 fd2 = open_path(&fpath2, O_WRONLY);
3156 e = fd2 < 0 ? errno : 0;
3160 printf("%d/%d: splice write - open %s failed %d\n",
3161 procid, opno, fpath2.path, e);
3165 /* Get file stats */
3166 if (fstat64(fd1, &stat1) < 0) {
3168 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3169 procid, opno, fpath1.path, errno);
3172 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3174 if (fstat64(fd2, &stat2) < 0) {
3176 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3177 procid, opno, fpath2.path, errno);
3180 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3182 /* Calculate offsets */
3183 len = (random() % FILELEN_MAX) + 1;
3185 len = stat1.st_blksize;
3186 if (len > stat1.st_size)
3187 len = stat1.st_size;
3189 lr = ((int64_t)random() << 32) + random();
3190 if (stat1.st_size == len)
3193 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3197 * splice can overlap write, so the offset of the target file can be
3198 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3199 * past the current dest file EOF
3201 lr = ((int64_t)random() << 32) + random();
3202 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3205 * Since len, off1 and off2 will be changed later, preserve their
3212 /* Pipe initialize */
3213 if (pipe(filedes) < 0) {
3215 printf("%d/%d: splice - pipe failed %d\n",
3216 procid, opno, errno);
3224 /* move to pipe buffer */
3225 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3231 /* move from pipe buffer to dst file */
3233 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3246 if (ret1 < 0 || ret2 < 0)
3251 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3253 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3254 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3256 if (length && length > total)
3257 printf(" asked for %lld, spliced %lld??\n",
3258 (long long)length, (long long)total);
3269 free_pathname(&fpath2);
3271 free_pathname(&fpath1);
3275 creat_f(int opno, long r)
3290 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3295 e1 = (random() % 100);
3296 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3298 if (type == FT_RTF) /* rt always gets an extsize */
3299 extsize = (random() % 10) + 1;
3300 else if (e1 < 10) /* one-in-ten get an extsize */
3301 extsize = random() % 1024;
3305 e = generate_fname(fep, type, &f, &id, &v);
3309 (void)fent_to_name(&f, fep);
3310 printf("%d/%d: creat - no filename from %s\n",
3311 procid, opno, f.path);
3316 fd = creat_path(&f, 0666);
3317 e = fd < 0 ? errno : 0;
3322 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3323 if (type == FT_RTF) {
3324 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3325 a.fsx_extsize = extsize *
3326 geom.rtextsize * geom.blocksize;
3328 } else if (extsize) {
3329 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3330 a.fsx_extsize = extsize * geom.blocksize;
3333 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3336 add_to_flist(type, id, parid, 0);
3340 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3341 extsize ? a.fsx_extsize : 0, e, e1);
3342 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3348 dread_f(int opno, long r)
3352 struct dioattr diob;
3365 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3367 printf("%d/%d: dread - no filename\n", procid, opno);
3371 fd = open_path(&f, O_RDONLY|O_DIRECT);
3372 e = fd < 0 ? errno : 0;
3376 printf("%d/%d: dread - open %s failed %d\n",
3377 procid, opno, f.path, e);
3381 if (fstat64(fd, &stb) < 0) {
3383 printf("%d/%d: dread - fstat64 %s failed %d\n",
3384 procid, opno, f.path, errno);
3389 inode_info(st, sizeof(st), &stb, v);
3390 if (stb.st_size == 0) {
3392 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3398 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3401 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3402 " fallback to stat()\n",
3403 procid, opno, f.path, st, errno);
3404 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3405 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3408 dio_env = getenv("XFS_DIO_MIN");
3410 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3412 align = (int64_t)diob.d_miniosz;
3413 lr = ((int64_t)random() << 32) + random();
3414 off = (off64_t)(lr % stb.st_size);
3415 off -= (off % align);
3416 lseek64(fd, off, SEEK_SET);
3417 len = (random() % FILELEN_MAX) + 1;
3418 len -= (len % align);
3421 else if (len > diob.d_maxiosz)
3422 len = diob.d_maxiosz;
3423 buf = memalign(diob.d_mem, len);
3424 e = read(fd, buf, len) < 0 ? errno : 0;
3427 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3428 procid, opno, f.path, st, (long long)off, (int)len, e);
3434 dwrite_f(int opno, long r)
3438 struct dioattr diob;
3451 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3453 printf("%d/%d: dwrite - no filename\n", procid, opno);
3457 fd = open_path(&f, O_WRONLY|O_DIRECT);
3458 e = fd < 0 ? errno : 0;
3462 printf("%d/%d: dwrite - open %s failed %d\n",
3463 procid, opno, f.path, e);
3467 if (fstat64(fd, &stb) < 0) {
3469 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3470 procid, opno, f.path, errno);
3475 inode_info(st, sizeof(st), &stb, v);
3476 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3478 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3479 " %s%s return %d, fallback to stat()\n",
3480 procid, opno, f.path, st, errno);
3481 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3482 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3485 dio_env = getenv("XFS_DIO_MIN");
3487 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3489 align = (int64_t)diob.d_miniosz;
3490 lr = ((int64_t)random() << 32) + random();
3491 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3492 off -= (off % align);
3493 lseek64(fd, off, SEEK_SET);
3494 len = (random() % FILELEN_MAX) + 1;
3495 len -= (len % align);
3498 else if (len > diob.d_maxiosz)
3499 len = diob.d_maxiosz;
3500 buf = memalign(diob.d_mem, len);
3502 lseek64(fd, off, SEEK_SET);
3503 memset(buf, nameseq & 0xff, len);
3504 e = write(fd, buf, len) < 0 ? errno : 0;
3507 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3508 procid, opno, f.path, st, (long long)off, (int)len, e);
3514 #ifdef HAVE_LINUX_FALLOC_H
3515 struct print_flags falloc_flags [] = {
3516 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3517 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3518 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3519 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3520 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3521 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3525 #define translate_falloc_flags(mode) \
3526 ({translate_flags(mode, "|", falloc_flags);})
3530 do_fallocate(int opno, long r, int mode)
3532 #ifdef HAVE_LINUX_FALLOC_H
3544 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3546 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3550 fd = open_path(&f, O_RDWR);
3553 printf("%d/%d: do_fallocate - open %s failed %d\n",
3554 procid, opno, f.path, errno);
3559 if (fstat64(fd, &stb) < 0) {
3561 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3562 procid, opno, f.path, errno);
3567 inode_info(st, sizeof(st), &stb, v);
3568 lr = ((int64_t)random() << 32) + random();
3569 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3571 len = (off64_t)(random() % (1024 * 1024));
3573 * Collapse/insert range requires off and len to be block aligned,
3574 * make it more likely to be the case.
3576 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3578 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3579 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3581 mode |= FALLOC_FL_KEEP_SIZE & random();
3582 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3584 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3585 procid, opno, translate_falloc_flags(mode),
3586 f.path, st, (long long)off, (long long)len, e);
3593 fallocate_f(int opno, long r)
3595 #ifdef HAVE_LINUX_FALLOC_H
3596 do_fallocate(opno, r, 0);
3601 fdatasync_f(int opno, long r)
3609 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3611 printf("%d/%d: fdatasync - no filename\n",
3616 fd = open_path(&f, O_WRONLY);
3617 e = fd < 0 ? errno : 0;
3621 printf("%d/%d: fdatasync - open %s failed %d\n",
3622 procid, opno, f.path, e);
3626 e = fdatasync(fd) < 0 ? errno : 0;
3628 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3633 #ifdef HAVE_LINUX_FIEMAP_H
3634 struct print_flags fiemap_flags[] = {
3635 { FIEMAP_FLAG_SYNC, "SYNC"},
3636 { FIEMAP_FLAG_XATTR, "XATTR"},
3640 #define translate_fiemap_flags(mode) \
3641 ({translate_flags(mode, "|", fiemap_flags);})
3645 fiemap_f(int opno, long r)
3647 #ifdef HAVE_LINUX_FIEMAP_H
3657 struct fiemap *fiemap;
3660 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3662 printf("%d/%d: fiemap - no filename\n", procid, opno);
3666 fd = open_path(&f, O_RDWR);
3667 e = fd < 0 ? errno : 0;
3671 printf("%d/%d: fiemap - open %s failed %d\n",
3672 procid, opno, f.path, e);
3676 if (fstat64(fd, &stb) < 0) {
3678 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3679 procid, opno, f.path, errno);
3684 inode_info(st, sizeof(st), &stb, v);
3685 blocks_to_map = random() & 0xffff;
3686 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3687 (blocks_to_map * sizeof(struct fiemap_extent)));
3690 printf("%d/%d: malloc failed \n", procid, opno);
3695 lr = ((int64_t)random() << 32) + random();
3696 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3698 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3699 fiemap->fm_extent_count = blocks_to_map;
3700 fiemap->fm_mapped_extents = random() & 0xffff;
3701 fiemap->fm_start = off;
3702 fiemap->fm_length = ((int64_t)random() << 32) + random();
3704 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3706 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3707 procid, opno, f.path, st, (long long)fiemap->fm_start,
3708 (long long) fiemap->fm_length,
3709 translate_fiemap_flags(fiemap->fm_flags), e);
3717 freesp_f(int opno, long r)
3722 struct xfs_flock64 fl;
3730 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3732 printf("%d/%d: freesp - no filename\n", procid, opno);
3736 fd = open_path(&f, O_RDWR);
3737 e = fd < 0 ? errno : 0;
3741 printf("%d/%d: freesp - open %s failed %d\n",
3742 procid, opno, f.path, e);
3746 if (fstat64(fd, &stb) < 0) {
3748 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3749 procid, opno, f.path, errno);
3754 inode_info(st, sizeof(st), &stb, v);
3755 lr = ((int64_t)random() << 32) + random();
3756 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3758 fl.l_whence = SEEK_SET;
3761 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3763 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3764 procid, opno, f.path, st, (long long)off, e);
3770 fsync_f(int opno, long r)
3778 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3780 printf("%d/%d: fsync - no filename\n", procid, opno);
3784 fd = open_file_or_dir(&f, O_WRONLY);
3785 e = fd < 0 ? errno : 0;
3789 printf("%d/%d: fsync - open %s failed %d\n",
3790 procid, opno, f.path, e);
3794 e = fsync(fd) < 0 ? errno : 0;
3796 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3802 gen_random_string(int len)
3804 static const char charset[] = "0123456789"
3805 "abcdefghijklmnopqrstuvwxyz"
3806 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3817 for (i = 0; i < len; i++)
3818 s[i] = charset[random() % sizeof(charset)];
3824 getattr_f(int opno, long r)
3833 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3834 append_pathname(&f, ".");
3835 fd = open_path(&f, O_RDWR);
3836 e = fd < 0 ? errno : 0;
3839 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3841 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3847 getdents_f(int opno, long r)
3854 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3855 append_pathname(&f, ".");
3856 dir = opendir_path(&f);
3860 printf("%d/%d: getdents - can't open %s\n",
3861 procid, opno, f.path);
3865 while (readdir64(dir) != NULL)
3868 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3874 getfattr_f(int opno, long r)
3880 char name[XATTR_NAME_BUF_SIZE];
3886 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3888 printf("%d/%d: getfattr - no filename\n", procid, opno);
3894 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3895 * to read a xattr that doesn't exist (fgetxattr should fail with
3896 * errno set to ENOATTR (61) in this case).
3898 if (fep->xattr_counter > 0)
3899 xattr_num = (random() % fep->xattr_counter) + 1;
3903 e = generate_xattr_name(xattr_num, name, sizeof(name));
3905 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3906 procid, opno, f.path, e);
3910 value_len = getxattr(f.path, name, NULL, 0);
3911 if (value_len < 0) {
3913 printf("%d/%d: getfattr file %s name %s failed %d\n",
3914 procid, opno, f.path, name, errno);
3918 /* A xattr without value.*/
3919 if (value_len == 0) {
3924 value = malloc(value_len);
3927 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3928 procid, opno, f.path, value_len);
3932 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3935 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3936 procid, opno, f.path, name, value_len, e);
3943 link_f(int opno, long r)
3957 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3959 printf("%d/%d: link - no file\n", procid, opno);
3963 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3969 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3973 (void)fent_to_name(&l, fep);
3974 printf("%d/%d: link - no filename from %s\n",
3975 procid, opno, l.path);
3981 e = link_path(&f, &l) < 0 ? errno : 0;
3984 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3986 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3988 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3995 listfattr_f(int opno, long r)
4001 char *buffer = NULL;
4005 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4007 printf("%d/%d: listfattr - no filename\n", procid, opno);
4012 e = listxattr(f.path, NULL, 0);
4015 printf("%d/%d: listfattr %s failed %d\n",
4016 procid, opno, f.path, errno);
4020 if (buffer_len == 0) {
4022 printf("%d/%d: listfattr %s - has no extended attributes\n",
4023 procid, opno, f.path);
4027 buffer = malloc(buffer_len);
4030 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
4031 procid, opno, f.path, buffer_len);
4035 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
4037 printf("%d/%d: listfattr %s buffer length %d %d\n",
4038 procid, opno, f.path, buffer_len, e);
4045 mkdir_f(int opno, long r)
4055 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4060 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
4064 (void)fent_to_name(&f, fep);
4065 printf("%d/%d: mkdir - no filename from %s\n",
4066 procid, opno, f.path);
4071 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
4074 add_to_flist(FT_DIR, id, parid, 0);
4076 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
4077 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
4083 mknod_f(int opno, long r)
4093 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4098 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
4102 (void)fent_to_name(&f, fep);
4103 printf("%d/%d: mknod - no filename from %s\n",
4104 procid, opno, f.path);
4109 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
4112 add_to_flist(FT_DEV, id, parid, 0);
4114 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
4115 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
4120 #ifdef HAVE_SYS_MMAN_H
4121 struct print_flags mmap_flags[] = {
4122 { MAP_SHARED, "SHARED"},
4123 { MAP_PRIVATE, "PRIVATE"},
4127 #define translate_mmap_flags(flags) \
4128 ({translate_flags(flags, "|", mmap_flags);})
4132 do_mmap(int opno, long r, int prot)
4134 #ifdef HAVE_SYS_MMAN_H
4146 sigjmp_buf sigbus_jmpbuf;
4149 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4151 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4155 fd = open_path(&f, O_RDWR);
4156 e = fd < 0 ? errno : 0;
4160 printf("%d/%d: do_mmap - open %s failed %d\n",
4161 procid, opno, f.path, e);
4165 if (fstat64(fd, &stb) < 0) {
4167 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4168 procid, opno, f.path, errno);
4173 inode_info(st, sizeof(st), &stb, v);
4174 if (stb.st_size == 0) {
4176 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4183 lr = ((int64_t)random() << 32) + random();
4184 off = (off64_t)(lr % stb.st_size);
4185 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4186 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4188 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4189 addr = mmap(NULL, len, prot, flags, fd, off);
4190 e = (addr == MAP_FAILED) ? errno : 0;
4193 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4194 procid, opno, f.path, st, (long long)off,
4195 (int)len, translate_mmap_flags(flags), e);
4201 if (prot & PROT_WRITE) {
4202 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4203 sigbus_jmp = &sigbus_jmpbuf;
4204 memset(addr, nameseq & 0xff, len);
4208 if ((buf = malloc(len)) != NULL) {
4209 memcpy(buf, addr, len);
4214 /* set NULL to stop other functions from doing siglongjmp */
4218 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4219 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4220 f.path, st, (long long)off, (int)len,
4221 translate_mmap_flags(flags),
4222 (e == 0) ? "0" : "Bus error");
4230 mread_f(int opno, long r)
4232 #ifdef HAVE_SYS_MMAN_H
4233 do_mmap(opno, r, PROT_READ);
4238 mwrite_f(int opno, long r)
4240 #ifdef HAVE_SYS_MMAN_H
4241 do_mmap(opno, r, PROT_WRITE);
4246 punch_f(int opno, long r)
4248 #ifdef HAVE_LINUX_FALLOC_H
4249 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4254 zero_f(int opno, long r)
4256 #ifdef HAVE_LINUX_FALLOC_H
4257 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4262 collapse_f(int opno, long r)
4264 #ifdef HAVE_LINUX_FALLOC_H
4265 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4270 insert_f(int opno, long r)
4272 #ifdef HAVE_LINUX_FALLOC_H
4273 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4278 read_f(int opno, long r)
4292 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4294 printf("%d/%d: read - no filename\n", procid, opno);
4298 fd = open_path(&f, O_RDONLY);
4299 e = fd < 0 ? errno : 0;
4303 printf("%d/%d: read - open %s failed %d\n",
4304 procid, opno, f.path, e);
4308 if (fstat64(fd, &stb) < 0) {
4310 printf("%d/%d: read - fstat64 %s failed %d\n",
4311 procid, opno, f.path, errno);
4316 inode_info(st, sizeof(st), &stb, v);
4317 if (stb.st_size == 0) {
4319 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4325 lr = ((int64_t)random() << 32) + random();
4326 off = (off64_t)(lr % stb.st_size);
4327 lseek64(fd, off, SEEK_SET);
4328 len = (random() % FILELEN_MAX) + 1;
4330 e = read(fd, buf, len) < 0 ? errno : 0;
4333 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4334 procid, opno, f.path, st, (long long)off, (int)len, e);
4340 readlink_f(int opno, long r)
4348 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4350 printf("%d/%d: readlink - no filename\n", procid, opno);
4354 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4357 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4362 readv_f(int opno, long r)
4374 struct iovec *iov = NULL;
4381 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4383 printf("%d/%d: readv - no filename\n", procid, opno);
4387 fd = open_path(&f, O_RDONLY);
4388 e = fd < 0 ? errno : 0;
4392 printf("%d/%d: readv - open %s failed %d\n",
4393 procid, opno, f.path, e);
4397 if (fstat64(fd, &stb) < 0) {
4399 printf("%d/%d: readv - fstat64 %s failed %d\n",
4400 procid, opno, f.path, errno);
4405 inode_info(st, sizeof(st), &stb, v);
4406 if (stb.st_size == 0) {
4408 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4414 lr = ((int64_t)random() << 32) + random();
4415 off = (off64_t)(lr % stb.st_size);
4416 lseek64(fd, off, SEEK_SET);
4417 len = (random() % FILELEN_MAX) + 1;
4420 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4421 iov = calloc(iovcnt, sizeof(struct iovec));
4422 iovl = len / iovcnt;
4424 for (i=0; i<iovcnt; i++) {
4425 (iov + i)->iov_base = (buf + iovb);
4426 (iov + i)->iov_len = iovl;
4430 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4433 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4434 procid, opno, f.path, st, (long long)off, (int)iovl,
4441 removefattr_f(int opno, long r)
4447 char name[XATTR_NAME_BUF_SIZE];
4451 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4453 printf("%d/%d: removefattr - no filename\n", procid, opno);
4459 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4460 * remove a xattr that doesn't exist (fremovexattr should fail with
4461 * errno set to ENOATTR (61) in this case).
4463 if (fep->xattr_counter > 0)
4464 xattr_num = (random() % fep->xattr_counter) + 1;
4468 e = generate_xattr_name(xattr_num, name, sizeof(name));
4470 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4471 procid, opno, f.path, e);
4475 e = removexattr(f.path, name) < 0 ? errno : 0;
4477 printf("%d/%d: removefattr file %s name %s %d\n",
4478 procid, opno, f.path, name, e);
4483 struct print_flags renameat2_flags [] = {
4484 { RENAME_NOREPLACE, "NOREPLACE"},
4485 { RENAME_EXCHANGE, "EXCHANGE"},
4486 { RENAME_WHITEOUT, "WHITEOUT"},
4490 #define translate_renameat2_flags(mode) \
4491 ({translate_flags(mode, "|", renameat2_flags);})
4494 do_renameat2(int opno, long r, int mode)
4510 /* get an existing path for the source of the rename */
4512 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4513 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4515 printf("%d/%d: rename - no source filename\n",
4522 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4523 * order to maintain filelist/filename integrity, we should
4524 * restrict exchange operation to files of the same type.
4526 if (mode == RENAME_EXCHANGE) {
4527 which = 1 << (flp - flist);
4528 init_pathname(&newf);
4529 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4531 printf("%d/%d: rename - no target filename\n",
4533 free_pathname(&newf);
4537 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4538 fents_ancestor_check(dfep, fep))) {
4540 printf("%d/%d: rename(REXCHANGE) %s and %s "
4541 "have ancestor-descendant relationship\n",
4542 procid, opno, f.path, newf.path);
4543 free_pathname(&newf);
4549 parid = dfep->parent;
4552 * Get an existing directory for the destination parent
4555 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4562 * Generate a new path using an existing parent directory
4565 init_pathname(&newf);
4566 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4570 (void)fent_to_name(&f, dfep);
4571 printf("%d/%d: rename - no filename from %s\n",
4572 procid, opno, f.path);
4574 free_pathname(&newf);
4579 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4582 int xattr_counter = fep->xattr_counter;
4583 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4584 int ft = flp - flist;
4587 oldparid = fep->parent;
4590 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4591 * old parent id for the others.
4593 if (ft == FT_DIR || ft == FT_SUBVOL)
4594 fix_parent(oldid, id, swap);
4596 if (mode == RENAME_WHITEOUT) {
4597 fep->xattr_counter = 0;
4598 add_to_flist(flp - flist, id, parid, xattr_counter);
4599 } else if (mode == RENAME_EXCHANGE) {
4600 fep->xattr_counter = dfep->xattr_counter;
4601 dfep->xattr_counter = xattr_counter;
4603 del_from_flist(flp - flist, fep - flp->fents);
4604 add_to_flist(flp - flist, id, parid, xattr_counter);
4608 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4609 opno, translate_renameat2_flags(mode), f.path,
4612 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4613 procid, opno, oldid, oldparid);
4614 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4615 procid, opno, id, parid);
4618 free_pathname(&newf);
4623 rename_f(int opno, long r)
4625 do_renameat2(opno, r, 0);
4629 rnoreplace_f(int opno, long r)
4631 do_renameat2(opno, r, RENAME_NOREPLACE);
4635 rexchange_f(int opno, long r)
4637 do_renameat2(opno, r, RENAME_EXCHANGE);
4641 rwhiteout_f(int opno, long r)
4643 do_renameat2(opno, r, RENAME_WHITEOUT);
4647 resvsp_f(int opno, long r)
4652 struct xfs_flock64 fl;
4660 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4662 printf("%d/%d: resvsp - no filename\n", procid, opno);
4666 fd = open_path(&f, O_RDWR);
4667 e = fd < 0 ? errno : 0;
4671 printf("%d/%d: resvsp - open %s failed %d\n",
4672 procid, opno, f.path, e);
4676 if (fstat64(fd, &stb) < 0) {
4678 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4679 procid, opno, f.path, errno);
4684 inode_info(st, sizeof(st), &stb, v);
4685 lr = ((int64_t)random() << 32) + random();
4686 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4688 fl.l_whence = SEEK_SET;
4690 fl.l_len = (off64_t)(random() % (1024 * 1024));
4691 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4693 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4694 procid, opno, f.path, st,
4695 (long long)off, (long long)fl.l_len, e);
4701 rmdir_f(int opno, long r)
4711 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4713 printf("%d/%d: rmdir - no directory\n", procid, opno);
4717 e = rmdir_path(&f) < 0 ? errno : 0;
4721 oldparid = fep->parent;
4722 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4725 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4727 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4728 procid, opno, oldid, oldparid);
4734 setattr_f(int opno, long r)
4743 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4744 append_pathname(&f, ".");
4745 fd = open_path(&f, O_RDWR);
4746 e = fd < 0 ? errno : 0;
4749 fl = attr_mask & (uint)random();
4750 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4752 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4758 setfattr_f(int opno, long r)
4765 char name[XATTR_NAME_BUF_SIZE];
4771 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4773 printf("%d/%d: setfattr - no filename\n", procid, opno);
4778 if ((fep->xattr_counter > 0) && (random() % 2)) {
4780 * Use an existing xattr name for replacing its value or
4781 * create again a xattr that was previously deleted.
4783 xattr_num = (random() % fep->xattr_counter) + 1;
4785 flag = XATTR_REPLACE;
4787 /* Use a new xattr name. */
4788 xattr_num = fep->xattr_counter + 1;
4790 * Don't always use the create flag because even if our xattr
4791 * counter is 0, we may still have xattrs associated to this
4792 * file (this happens when xattrs are added to a file through
4793 * one of its other hard links), so we can end up updating an
4794 * existing xattr too.
4797 flag = XATTR_CREATE;
4801 * The maximum supported value size depends on the filesystem
4802 * implementation, but 100 bytes is a safe value for most filesystems
4805 value_len = random() % 101;
4806 value = gen_random_string(value_len);
4807 if (!value && value_len > 0) {
4809 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4810 procid, opno, f.path, value_len);
4813 e = generate_xattr_name(xattr_num, name, sizeof(name));
4815 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4816 procid, opno, f.path, e);
4820 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4822 fep->xattr_counter++;
4824 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4825 procid, opno, f.path, name, xattr_flag_to_string(flag),
4833 snapshot_f(int opno, long r)
4835 #ifdef HAVE_BTRFSUTIL_H
4836 enum btrfs_util_error e;
4847 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4849 printf("%d/%d: snapshot - no subvolume\n", procid,
4854 init_pathname(&newf);
4856 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4860 (void)fent_to_name(&f, fep);
4861 printf("%d/%d: snapshot - no filename from %s\n",
4862 procid, opno, f.path);
4867 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4868 if (e == BTRFS_UTIL_OK)
4869 add_to_flist(FT_SUBVOL, id, parid, 0);
4871 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4872 f.path, newf.path, e, btrfs_util_strerror(e));
4873 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4876 free_pathname(&newf);
4882 stat_f(int opno, long r)
4890 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4892 printf("%d/%d: stat - no entries\n", procid, opno);
4896 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4899 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4904 subvol_create_f(int opno, long r)
4906 #ifdef HAVE_BTRFSUTIL_H
4907 enum btrfs_util_error e;
4917 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4921 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4925 (void)fent_to_name(&f, fep);
4926 printf("%d/%d: subvol_create - no filename from %s\n",
4927 procid, opno, f.path);
4932 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4933 if (e == BTRFS_UTIL_OK)
4934 add_to_flist(FT_SUBVOL, id, parid, 0);
4936 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4937 f.path, e, btrfs_util_strerror(e));
4938 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4946 subvol_delete_f(int opno, long r)
4948 #ifdef HAVE_BTRFSUTIL_H
4949 enum btrfs_util_error e;
4957 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4959 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4964 e = btrfs_util_delete_subvolume(f.path, 0);
4966 if (e == BTRFS_UTIL_OK) {
4968 oldparid = fep->parent;
4969 delete_subvol_children(oldid);
4970 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4973 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4974 e, btrfs_util_strerror(e));
4975 if (e == BTRFS_UTIL_OK)
4976 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4977 procid, opno, oldid, oldparid);
4984 symlink_f(int opno, long r)
4997 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
5002 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
5006 (void)fent_to_name(&f, fep);
5007 printf("%d/%d: symlink - no filename from %s\n",
5008 procid, opno, f.path);
5013 len = (int)(random() % PATH_MAX);
5014 val = malloc(len + 1);
5016 memset(val, 'x', len);
5018 for (i = 10; i < len - 1; i += 10)
5020 e = symlink_path(val, &f) < 0 ? errno : 0;
5023 add_to_flist(FT_SYM, id, parid, 0);
5026 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
5027 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
5034 sync_f(int opno, long r)
5038 printf("%d/%d: sync\n", procid, opno);
5042 truncate_f(int opno, long r)
5053 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5055 printf("%d/%d: truncate - no filename\n", procid, opno);
5059 e = stat64_path(&f, &stb) < 0 ? errno : 0;
5063 printf("%d/%d: truncate - stat64 %s failed %d\n",
5064 procid, opno, f.path, e);
5068 inode_info(st, sizeof(st), &stb, v);
5069 lr = ((int64_t)random() << 32) + random();
5070 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5072 e = truncate64_path(&f, off) < 0 ? errno : 0;
5075 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
5076 st, (long long)off, e);
5081 unlink_f(int opno, long r)
5092 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
5094 printf("%d/%d: unlink - no file\n", procid, opno);
5098 e = unlink_path(&f) < 0 ? errno : 0;
5102 oldparid = fep->parent;
5103 del_from_flist(flp - flist, fep - flp->fents);
5106 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
5108 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
5109 procid, opno, oldid, oldparid);
5115 unresvsp_f(int opno, long r)
5120 struct xfs_flock64 fl;
5128 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5130 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5134 fd = open_path(&f, O_RDWR);
5135 e = fd < 0 ? errno : 0;
5139 printf("%d/%d: unresvsp - open %s failed %d\n",
5140 procid, opno, f.path, e);
5144 if (fstat64(fd, &stb) < 0) {
5146 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5147 procid, opno, f.path, errno);
5152 inode_info(st, sizeof(st), &stb, v);
5153 lr = ((int64_t)random() << 32) + random();
5154 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5156 fl.l_whence = SEEK_SET;
5158 fl.l_len = (off64_t)(random() % (1 << 20));
5159 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5161 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5162 procid, opno, f.path, st,
5163 (long long)off, (long long)fl.l_len, e);
5169 uring_read_f(int opno, long r)
5172 do_uring_rw(opno, r, O_RDONLY);
5177 uring_write_f(int opno, long r)
5180 do_uring_rw(opno, r, O_WRONLY);
5185 write_f(int opno, long r)
5199 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5201 printf("%d/%d: write - no filename\n", procid, opno);
5205 fd = open_path(&f, O_WRONLY);
5206 e = fd < 0 ? errno : 0;
5210 printf("%d/%d: write - open %s failed %d\n",
5211 procid, opno, f.path, e);
5215 if (fstat64(fd, &stb) < 0) {
5217 printf("%d/%d: write - fstat64 %s failed %d\n",
5218 procid, opno, f.path, errno);
5223 inode_info(st, sizeof(st), &stb, v);
5224 lr = ((int64_t)random() << 32) + random();
5225 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5227 lseek64(fd, off, SEEK_SET);
5228 len = (random() % FILELEN_MAX) + 1;
5230 memset(buf, nameseq & 0xff, len);
5231 e = write(fd, buf, len) < 0 ? errno : 0;
5234 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5235 procid, opno, f.path, st, (long long)off, (int)len, e);
5241 writev_f(int opno, long r)
5253 struct iovec *iov = NULL;
5260 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5262 printf("%d/%d: writev - no filename\n", procid, opno);
5266 fd = open_path(&f, O_WRONLY);
5267 e = fd < 0 ? errno : 0;
5271 printf("%d/%d: writev - open %s failed %d\n",
5272 procid, opno, f.path, e);
5276 if (fstat64(fd, &stb) < 0) {
5278 printf("%d/%d: writev - fstat64 %s failed %d\n",
5279 procid, opno, f.path, errno);
5284 inode_info(st, sizeof(st), &stb, v);
5285 lr = ((int64_t)random() << 32) + random();
5286 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5288 lseek64(fd, off, SEEK_SET);
5289 len = (random() % FILELEN_MAX) + 1;
5291 memset(buf, nameseq & 0xff, len);
5293 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5294 iov = calloc(iovcnt, sizeof(struct iovec));
5295 iovl = len / iovcnt;
5297 for (i=0; i<iovcnt; i++) {
5298 (iov + i)->iov_base = (buf + iovb);
5299 (iov + i)->iov_len = iovl;
5303 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5307 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5308 procid, opno, f.path, st, (long long)off, (int)iovl,
5315 xattr_flag_to_string(int flag)
5317 if (flag == XATTR_CREATE)
5319 if (flag == XATTR_REPLACE)