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>
33 #include <sys/syscall.h>
34 #include <sys/xattr.h>
36 #ifndef FS_IOC_GETFLAGS
37 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
39 #ifndef FS_IOC_SETFLAGS
40 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
44 #define XFS_ERRTAG_MAX 17
45 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
46 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
51 #ifndef HAVE_RENAMEAT2
52 #if !defined(SYS_renameat2) && defined(__x86_64__)
53 #define SYS_renameat2 316
56 #if !defined(SYS_renameat2) && defined(__i386__)
57 #define SYS_renameat2 353
60 static int renameat2(int dfd1, const char *path1,
61 int dfd2, const char *path2,
65 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
73 #ifndef RENAME_NOREPLACE
74 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
76 #ifndef RENAME_EXCHANGE
77 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
79 #ifndef RENAME_WHITEOUT
80 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
83 #define FILELEN_MAX (32*4096)
147 typedef void (*opfnc_t)(int, long);
149 typedef struct opdesc {
157 typedef struct fent {
164 typedef struct flist {
171 typedef struct pathname {
181 struct print_string {
188 #define FT_DIRm (1 << FT_DIR)
190 #define FT_REGm (1 << FT_REG)
192 #define FT_SYMm (1 << FT_SYM)
194 #define FT_DEVm (1 << FT_DEV)
196 #define FT_RTFm (1 << FT_RTF)
198 #define FT_SUBVOLm (1 << FT_SUBVOL)
200 #define FT_ANYm ((1 << FT_nft) - 1)
201 #define FT_REGFILE (FT_REGm | FT_RTFm)
202 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
203 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
205 #define FLIST_SLOT_INCR 16
208 #define MAXFSIZE ((1ULL << 63) - 1ULL)
209 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
211 #define XATTR_NAME_BUF_SIZE 18
213 void afsync_f(int, long);
214 void allocsp_f(int, long);
215 void aread_f(int, long);
216 void attr_remove_f(int, long);
217 void attr_set_f(int, long);
218 void awrite_f(int, long);
219 void bulkstat_f(int, long);
220 void bulkstat1_f(int, long);
221 void chown_f(int, long);
222 void clonerange_f(int, long);
223 void copyrange_f(int, long);
224 void creat_f(int, long);
225 void deduperange_f(int, long);
226 void dread_f(int, long);
227 void dwrite_f(int, long);
228 void fallocate_f(int, long);
229 void fdatasync_f(int, long);
230 void fiemap_f(int, long);
231 void freesp_f(int, long);
232 void fsync_f(int, long);
233 char *gen_random_string(int);
234 void getattr_f(int, long);
235 void getdents_f(int, long);
236 void getfattr_f(int, long);
237 void link_f(int, long);
238 void listfattr_f(int, long);
239 void mkdir_f(int, long);
240 void mknod_f(int, long);
241 void mread_f(int, long);
242 void mwrite_f(int, long);
243 void punch_f(int, long);
244 void zero_f(int, long);
245 void collapse_f(int, long);
246 void insert_f(int, long);
247 void read_f(int, long);
248 void readlink_f(int, long);
249 void readv_f(int, long);
250 void removefattr_f(int, long);
251 void rename_f(int, long);
252 void rnoreplace_f(int, long);
253 void rexchange_f(int, long);
254 void rwhiteout_f(int, long);
255 void resvsp_f(int, long);
256 void rmdir_f(int, long);
257 void setattr_f(int, long);
258 void setfattr_f(int, long);
259 void setxattr_f(int, long);
260 void snapshot_f(int, long);
261 void splice_f(int, long);
262 void stat_f(int, long);
263 void subvol_create_f(int, long);
264 void subvol_delete_f(int, long);
265 void symlink_f(int, long);
266 void sync_f(int, long);
267 void truncate_f(int, long);
268 void unlink_f(int, long);
269 void unresvsp_f(int, long);
270 void write_f(int, long);
271 void writev_f(int, long);
272 char *xattr_flag_to_string(int);
275 /* { OP_ENUM, "name", function, freq, iswrite }, */
276 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
277 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
278 { OP_AREAD, "aread", aread_f, 1, 0 },
279 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
280 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
281 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
282 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
283 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
284 { OP_CHOWN, "chown", chown_f, 3, 1 },
285 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
286 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
287 { OP_CREAT, "creat", creat_f, 4, 1 },
288 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
289 { OP_DREAD, "dread", dread_f, 4, 0 },
290 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
291 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
292 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
293 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
294 { OP_FREESP, "freesp", freesp_f, 1, 1 },
295 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
296 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
297 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
298 /* get extended attribute */
299 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
300 { OP_LINK, "link", link_f, 1, 1 },
301 /* list extent attributes */
302 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
303 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
304 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
305 { OP_MREAD, "mread", mread_f, 2, 0 },
306 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
307 { OP_PUNCH, "punch", punch_f, 1, 1 },
308 { OP_ZERO, "zero", zero_f, 1, 1 },
309 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
310 { OP_INSERT, "insert", insert_f, 1, 1 },
311 { OP_READ, "read", read_f, 1, 0 },
312 { OP_READLINK, "readlink", readlink_f, 1, 0 },
313 { OP_READV, "readv", readv_f, 1, 0 },
314 /* remove (delete) extended attribute */
315 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
316 { OP_RENAME, "rename", rename_f, 2, 1 },
317 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
318 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
319 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
320 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
321 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
322 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
323 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
324 /* set extended attribute */
325 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
326 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
327 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
328 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
329 { OP_SPLICE, "splice", splice_f, 1, 1 },
330 { OP_STAT, "stat", stat_f, 1, 0 },
331 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
332 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
333 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
334 { OP_SYNC, "sync", sync_f, 1, 1 },
335 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
336 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
337 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
338 { OP_WRITE, "write", write_f, 4, 1 },
339 { OP_WRITEV, "writev", writev_f, 4, 1 },
342 flist_t flist[FT_nft] = {
356 struct xfs_fsop_geom geom;
367 unsigned int idmodulo = XFS_IDMODULO_MAX;
368 unsigned int attr_mask = ~0;
371 unsigned long seed = 0;
375 int verifiable_log = 0;
376 sig_atomic_t should_stop = 0;
377 sigjmp_buf *sigbus_jmp = NULL;
378 char *execute_cmd = NULL;
379 int execute_freq = 1;
380 struct print_string flag_str = {0};
382 void add_to_flist(int, int, int, int);
383 void append_pathname(pathname_t *, char *);
384 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
385 int attr_remove_path(pathname_t *, const char *, int);
386 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
387 void check_cwd(void);
388 void cleanup_flist(void);
389 int creat_path(pathname_t *, mode_t);
390 void dcache_enter(int, int);
391 void dcache_init(void);
392 fent_t *dcache_lookup(int);
393 void dcache_purge(int, int);
394 void del_from_flist(int, int);
395 int dirid_to_name(char *, int);
397 int fent_to_name(pathname_t *, fent_t *);
398 bool fents_ancestor_check(fent_t *, fent_t *);
399 void fix_parent(int, int, bool);
400 void free_pathname(pathname_t *);
401 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
402 int generate_xattr_name(int, char *, int);
403 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
404 void init_pathname(pathname_t *);
405 int lchown_path(pathname_t *, uid_t, gid_t);
406 int link_path(pathname_t *, pathname_t *);
407 int lstat64_path(pathname_t *, struct stat64 *);
408 void make_freq_table(void);
409 int mkdir_path(pathname_t *, mode_t);
410 int mknod_path(pathname_t *, mode_t, dev_t);
411 void namerandpad(int, char *, int);
412 int open_file_or_dir(pathname_t *, int);
413 int open_path(pathname_t *, int);
414 DIR *opendir_path(pathname_t *);
415 void process_freq(char *);
416 int readlink_path(pathname_t *, char *, size_t);
417 int rename_path(pathname_t *, pathname_t *, int);
418 int rmdir_path(pathname_t *);
419 void separate_pathname(pathname_t *, char *, pathname_t *);
420 void show_ops(int, char *);
421 int stat64_path(pathname_t *, struct stat64 *);
422 int symlink_path(const char *, pathname_t *);
423 int truncate64_path(pathname_t *, off64_t);
424 int unlink_path(pathname_t *);
426 void write_freq(void);
427 void zero_freq(void);
428 void non_btrfs_freq(const char *);
430 void sg_handler(int signum)
438 * Only handle SIGBUS when mmap write to a hole and no
439 * block can be allocated due to ENOSPC, abort otherwise.
442 siglongjmp(*sigbus_jmp, -1);
444 printf("Unknown SIGBUS is caught, Abort!\n");
447 /* should not reach here */
454 int main(int argc, char **argv)
458 char *dirname = NULL;
459 char *logname = NULL;
460 char rpath[PATH_MAX];
469 xfs_error_injection_t err_inj;
470 struct sigaction action;
472 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
474 errrange = errtag = 0;
476 nops = sizeof(ops) / sizeof(ops[0]);
477 ops_end = &ops[nops];
479 while ((c = getopt(argc, argv, allopts)) != -1) {
488 sscanf(optarg, "%d", &errtag);
492 } else if (errtag == 0)
494 if (errtag >= XFS_ERRTAG_MAX) {
496 "error tag %d too large (max %d)\n",
497 errtag, XFS_ERRTAG_MAX - 1);
502 process_freq(optarg);
505 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
506 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
509 idmodulo = strtoul(optarg, NULL, 0);
510 if (idmodulo > XFS_IDMODULO_MAX) {
512 "chown modulo %d too big (max %d)\n",
513 idmodulo, XFS_IDMODULO_MAX);
518 loops = atoi(optarg);
521 operations = atoi(optarg);
528 nproc = atoi(optarg);
534 seed = strtoul(optarg, NULL, 0);
543 execute_cmd = optarg;
549 attr_mask = strtoul(optarg, NULL, 0);
553 if (optarg[0] == 'c')
564 execute_freq = strtoul(optarg, NULL, 0);
567 fprintf(stderr, "%s - invalid parameters\n",
577 /* no directory specified */
578 if (!nousage) usage();
582 non_btrfs_freq(dirname);
583 (void)mkdir(dirname, 0777);
584 if (logname && logname[0] != '/') {
585 if (!getcwd(rpath, sizeof(rpath))){
586 perror("getcwd failed");
592 if (chdir(dirname) < 0) {
597 char path[PATH_MAX + NAME_MAX + 1];
598 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
599 if (freopen(path, "a", stdout) == NULL) {
600 perror("freopen logfile failed");
604 sprintf(buf, "fss%x", (unsigned int)getpid());
605 fd = creat(buf, 0666);
606 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
607 maxfsize = (off64_t)MAXFSIZE32;
609 maxfsize = (off64_t)MAXFSIZE;
614 gettimeofday(&t, (void *)NULL);
615 seed = (int)t.tv_sec ^ (int)t.tv_usec;
616 printf("seed = %ld\n", seed);
618 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
619 if (i >= 0 && geom.rtblocks)
620 rtpct = MIN(MAX(geom.rtblocks * 100 /
621 (geom.rtblocks + geom.datablocks), 1), 99);
630 for (i = 0; i < j; i++)
633 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
639 for (i = 0; i < j; i++)
642 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
644 printf("Injecting failure on tag #%d\n", errtag);
645 err_inj.errtag = errtag;
647 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
649 perror("fsstress - XFS_SYSSGI error injection call");
658 action.sa_handler = sg_handler;
659 sigemptyset(&action.sa_mask);
661 if (sigaction(SIGTERM, &action, 0)) {
662 perror("sigaction failed");
666 for (i = 0; i < nproc; i++) {
668 sigemptyset(&action.sa_mask);
669 action.sa_handler = SIG_DFL;
670 if (sigaction(SIGTERM, &action, 0))
672 action.sa_handler = sg_handler;
673 if (sigaction(SIGBUS, &action, 0))
675 #ifdef HAVE_SYS_PRCTL_H
676 prctl(PR_SET_PDEATHSIG, SIGKILL);
677 if (getppid() == 1) /* parent died already? */
681 char path[PATH_MAX + NAME_MAX + 2 + 11];
682 snprintf(path, sizeof(path), "%s/%s.%d",
684 if (freopen(path, "a", stdout) == NULL) {
685 perror("freopen logfile failed");
691 if (io_setup(128, &io_ctx) != 0) {
692 fprintf(stderr, "io_setup failed");
696 for (i = 0; !loops || (i < loops); i++)
699 if(io_destroy(io_ctx) != 0) {
700 fprintf(stderr, "io_destroy failed");
710 while (wait(&stat) > 0 && !should_stop) {
713 action.sa_flags = SA_RESTART;
714 sigaction(SIGTERM, &action, 0);
715 kill(-getpid(), SIGTERM);
716 while (wait(&stat) > 0)
722 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
724 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
726 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
739 add_string(struct print_string *str, const char *add)
741 int len = strlen(add);
746 if (len > (str->max - 1) - str->len) {
747 str->len = str->max - 1;
751 memcpy(str->buffer + str->len, add, len);
753 str->buffer[str->len] = '\0';
759 translate_flags(int flags, const char *delim,
760 const struct print_flags *flag_array)
762 int i, mask, first = 1;
765 if (!flag_str.buffer) {
766 flag_str.buffer = malloc(4096);
770 if (!flag_str.buffer)
773 flag_str.buffer[0] = '\0';
775 for (i = 0; flag_array[i].name && flags; i++) {
776 mask = flag_array[i].mask;
777 if ((flags & mask) != mask)
780 add = flag_array[i].name;
783 add_string(&flag_str, delim);
786 add_string(&flag_str, add);
789 /* Check whether there are any leftover flags. */
795 add_string(&flag_str, delim);
797 ret = snprintf(number, 11, "0x%x", flags) > 0;
798 if (ret > 0 && ret <= 11)
799 add_string(&flag_str, number);
802 return flag_str.buffer;
806 add_to_flist(int ft, int id, int parent, int xattr_counter)
812 if (ftp->nfiles == ftp->nslots) {
813 ftp->nslots += FLIST_SLOT_INCR;
814 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
816 fep = &ftp->fents[ftp->nfiles++];
819 fep->parent = parent;
820 fep->xattr_counter = xattr_counter;
824 append_pathname(pathname_t *name, char *str)
830 /* attempting to append to a dir a zero length path */
831 if (len && *str == '/' && name->len == 0) {
832 fprintf(stderr, "fsstress: append_pathname failure\n");
833 assert(chdir(homedir) == 0);
838 name->path = realloc(name->path, name->len + 1 + len);
839 strcpy(&name->path[name->len], str);
844 attr_list_path(pathname_t *name,
846 const int buffersize,
848 attrlist_cursor_t *cursor)
850 char buf[NAME_MAX + 1];
854 if (flags != ATTR_DONTFOLLOW) {
859 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
860 if (rval >= 0 || errno != ENAMETOOLONG)
862 separate_pathname(name, buf, &newname);
863 if (chdir(buf) == 0) {
864 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
865 assert(chdir("..") == 0);
867 free_pathname(&newname);
872 attr_remove_path(pathname_t *name, const char *attrname, int flags)
874 char buf[NAME_MAX + 1];
878 rval = attr_remove(name->path, attrname, flags);
879 if (rval >= 0 || errno != ENAMETOOLONG)
881 separate_pathname(name, buf, &newname);
882 if (chdir(buf) == 0) {
883 rval = attr_remove_path(&newname, attrname, flags);
884 assert(chdir("..") == 0);
886 free_pathname(&newname);
891 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
892 const int valuelength, int flags)
894 char buf[NAME_MAX + 1];
898 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
899 if (rval >= 0 || errno != ENAMETOOLONG)
901 separate_pathname(name, buf, &newname);
902 if (chdir(buf) == 0) {
903 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
905 assert(chdir("..") == 0);
907 free_pathname(&newname);
915 struct stat64 statbuf;
917 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
919 assert(chdir(homedir) == 0);
920 fprintf(stderr, "fsstress: check_cwd failure\n");
927 * go thru flist and release all entries
929 * NOTE: this function shouldn't be called until the end of a process
937 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
946 creat_path(pathname_t *name, mode_t mode)
948 char buf[NAME_MAX + 1];
952 rval = creat(name->path, mode);
953 if (rval >= 0 || errno != ENAMETOOLONG)
955 separate_pathname(name, buf, &newname);
956 if (chdir(buf) == 0) {
957 rval = creat_path(&newname, mode);
958 assert(chdir("..") == 0);
960 free_pathname(&newname);
965 dcache_enter(int dirid, int slot)
967 dcache[dirid % NDCACHE] = slot;
975 for (i = 0; i < NDCACHE; i++)
980 dcache_lookup(int dirid)
985 i = dcache[dirid % NDCACHE];
986 if (i >= 0 && i < flist[FT_DIR].nfiles &&
987 (fep = &flist[FT_DIR].fents[i])->id == dirid)
989 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
990 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
996 dcache_purge(int dirid, int ft)
999 dcp = &dcache[dirid % NDCACHE];
1000 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1001 flist[ft].fents[*dcp].id == dirid)
1006 * Delete the item from the list by
1007 * moving last entry over the deleted one;
1008 * unless deleted entry is the last one.
1009 * Input: which file list array and which slot in array
1012 del_from_flist(int ft, int slot)
1017 if (ft == FT_DIR || ft == FT_SUBVOL)
1018 dcache_purge(ftp->fents[slot].id, ft);
1019 if (slot != ftp->nfiles - 1) {
1020 if (ft == FT_DIR || ft == FT_SUBVOL)
1021 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1022 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1028 delete_subvol_children(int parid)
1033 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1035 for (c = 0; c < flp->nfiles; c++) {
1036 if (flp->fents[c].parent == parid) {
1037 int id = flp->fents[c].id;
1038 del_from_flist(i, c);
1039 if (i == FT_DIR || i == FT_SUBVOL)
1040 delete_subvol_children(id);
1048 dirid_to_fent(int dirid)
1055 if ((fep = dcache_lookup(dirid)))
1059 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1060 if (fep->id == dirid) {
1061 dcache_enter(dirid, fep - flp->fents);
1075 struct stat64 statbuf;
1083 dividend = (operations + execute_freq) / (execute_freq + 1);
1084 sprintf(buf, "p%x", procid);
1085 (void)mkdir(buf, 0777);
1086 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1090 top_ino = statbuf.st_ino;
1091 homedir = getcwd(NULL, 0);
1093 perror("getcwd failed");
1099 namerand = random();
1100 for (opno = 0; opno < operations; opno++) {
1101 if (execute_cmd && opno && opno % dividend == 0) {
1103 printf("%d: execute command %s\n", opno,
1105 rval = system(execute_cmd);
1107 fprintf(stderr, "execute command failed with "
1110 p = &ops[freq_table[random() % freq_table_size]];
1111 p->func(opno, random());
1113 * test for forced shutdown by stat'ing the test
1114 * directory. If this stat returns EIO, assume
1115 * the forced shutdown happened.
1117 if (errtag != 0 && opno % 100 == 0) {
1118 rval = stat64(".", &statbuf);
1120 fprintf(stderr, "Detected EIO\n");
1126 assert(chdir("..") == 0);
1131 sprintf(cmd, "rm -rf %s", buf);
1134 perror("cleaning up");
1140 * build up a pathname going thru the file entry and all
1141 * its parent entries
1142 * Return 0 on error, 1 on success;
1145 fent_to_name(pathname_t *name, fent_t *fep)
1147 flist_t *flp = &flist[fep->ft];
1148 char buf[NAME_MAX + 1];
1156 /* build up parent directory name */
1157 if (fep->parent != -1) {
1158 pfep = dirid_to_fent(fep->parent);
1161 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1162 procid, fep->id, fep->parent);
1167 e = fent_to_name(name, pfep);
1170 append_pathname(name, "/");
1173 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1174 namerandpad(fep->id, buf, i);
1175 append_pathname(name, buf);
1180 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1184 for (tmpfep = fep; tmpfep->parent != -1;
1185 tmpfep = dirid_to_fent(tmpfep->parent)) {
1186 if (tmpfep->parent == dfep->id)
1194 fix_parent(int oldid, int newid, bool swap)
1201 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1202 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1203 if (fep->parent == oldid)
1204 fep->parent = newid;
1205 else if (swap && fep->parent == newid)
1206 fep->parent = oldid;
1212 free_pathname(pathname_t *name)
1222 * Generate a filename of type ft.
1223 * If we have a fep which should be a directory then use it
1224 * as the parent path for this new filename i.e. prepend with it.
1227 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1229 char buf[NAME_MAX + 1];
1238 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1239 namerandpad(id, buf, len);
1241 /* prepend fep parent dir-name to it */
1243 e = fent_to_name(name, fep);
1246 append_pathname(name, "/");
1248 append_pathname(name, buf);
1252 for (j = 0; !*v && j < ilistlen; j++) {
1253 if (ilist[j] == id) {
1261 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1265 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1275 * Input: "which" to choose the file-types eg. non-directory
1276 * Input: "r" to choose which file
1277 * Output: file-list, file-entry, name for the chosen file.
1278 * Output: verbose if chosen file is on the ilist.
1281 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1284 int totalsum = 0; /* total number of matching files */
1285 int partialsum = 0; /* partial sum of matching files */
1291 int e = 1; /* success */
1294 * go thru flist and add up number of files for each
1295 * category that matches with <which>.
1297 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1298 if (which & (1 << i))
1299 totalsum += flp->nfiles;
1301 if (totalsum == 0) {
1311 * Now we have possible matches between 0..totalsum-1.
1312 * And we use r to help us choose which one we want,
1313 * which when bounded by totalsum becomes x.
1315 x = (int)(r % totalsum);
1316 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1317 if (which & (1 << i)) {
1318 if (x < partialsum + flp->nfiles) {
1320 /* found the matching file entry */
1321 fep = &flp->fents[x - partialsum];
1323 /* fill-in what we were asked for */
1325 e = fent_to_name(name, fep);
1328 fprintf(stderr, "%d: failed to get path for entry:"
1329 " id=%d,parent=%d\n",
1330 procid, fep->id, fep->parent);
1339 /* turn on verbose if its an ilisted file */
1341 for (j = 0; !*v && j < ilistlen; j++) {
1342 if (ilist[j] == fep->id) {
1349 partialsum += flp->nfiles;
1353 fprintf(stderr, "fsstress: get_fname failure\n");
1360 init_pathname(pathname_t *name)
1367 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1369 char buf[NAME_MAX + 1];
1373 rval = lchown(name->path, owner, group);
1374 if (rval >= 0 || errno != ENAMETOOLONG)
1376 separate_pathname(name, buf, &newname);
1377 if (chdir(buf) == 0) {
1378 rval = lchown_path(&newname, owner, group);
1379 assert(chdir("..") == 0);
1381 free_pathname(&newname);
1386 link_path(pathname_t *name1, pathname_t *name2)
1388 char buf1[NAME_MAX + 1];
1389 char buf2[NAME_MAX + 1];
1391 pathname_t newname1;
1392 pathname_t newname2;
1395 rval = link(name1->path, name2->path);
1396 if (rval >= 0 || errno != ENAMETOOLONG)
1398 separate_pathname(name1, buf1, &newname1);
1399 separate_pathname(name2, buf2, &newname2);
1400 if (strcmp(buf1, buf2) == 0) {
1401 if (chdir(buf1) == 0) {
1402 rval = link_path(&newname1, &newname2);
1403 assert(chdir("..") == 0);
1406 if (strcmp(buf1, "..") == 0)
1408 else if (strcmp(buf2, "..") == 0)
1410 else if (strlen(buf1) == 0)
1412 else if (strlen(buf2) == 0)
1415 down1 = MAX(newname1.len, 3 + name2->len) <=
1416 MAX(3 + name1->len, newname2.len);
1418 free_pathname(&newname2);
1419 append_pathname(&newname2, "../");
1420 append_pathname(&newname2, name2->path);
1421 if (chdir(buf1) == 0) {
1422 rval = link_path(&newname1, &newname2);
1423 assert(chdir("..") == 0);
1426 free_pathname(&newname1);
1427 append_pathname(&newname1, "../");
1428 append_pathname(&newname1, name1->path);
1429 if (chdir(buf2) == 0) {
1430 rval = link_path(&newname1, &newname2);
1431 assert(chdir("..") == 0);
1435 free_pathname(&newname1);
1436 free_pathname(&newname2);
1441 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1443 char buf[NAME_MAX + 1];
1447 rval = lstat64(name->path, sbuf);
1448 if (rval >= 0 || errno != ENAMETOOLONG)
1450 separate_pathname(name, buf, &newname);
1451 if (chdir(buf) == 0) {
1452 rval = lstat64_path(&newname, sbuf);
1453 assert(chdir("..") == 0);
1455 free_pathname(&newname);
1460 make_freq_table(void)
1466 for (p = ops, f = 0; p < ops_end; p++)
1468 freq_table = malloc(f * sizeof(*freq_table));
1469 freq_table_size = f;
1470 for (p = ops, i = 0; p < ops_end; p++) {
1471 for (f = 0; f < p->freq; f++, i++)
1472 freq_table[i] = p->op;
1477 mkdir_path(pathname_t *name, mode_t mode)
1479 char buf[NAME_MAX + 1];
1483 rval = mkdir(name->path, mode);
1484 if (rval >= 0 || errno != ENAMETOOLONG)
1486 separate_pathname(name, buf, &newname);
1487 if (chdir(buf) == 0) {
1488 rval = mkdir_path(&newname, mode);
1489 assert(chdir("..") == 0);
1491 free_pathname(&newname);
1496 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1498 char buf[NAME_MAX + 1];
1502 rval = mknod(name->path, mode, dev);
1503 if (rval >= 0 || errno != ENAMETOOLONG)
1505 separate_pathname(name, buf, &newname);
1506 if (chdir(buf) == 0) {
1507 rval = mknod_path(&newname, mode, dev);
1508 assert(chdir("..") == 0);
1510 free_pathname(&newname);
1515 namerandpad(int id, char *buf, int i)
1518 static int buckets[] =
1519 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1525 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1526 padmod = buckets[bucket] + 1 - i;
1529 padlen = (id ^ namerand) % padmod;
1531 memset(&buf[i], 'X', padlen);
1532 buf[i + padlen] = '\0';
1537 open_file_or_dir(pathname_t *name, int flags)
1541 fd = open_path(name, flags);
1544 if (fd == -1 && errno != EISDIR)
1546 /* Directories can not be opened in write mode nor direct mode. */
1547 flags &= ~(O_WRONLY | O_DIRECT);
1548 flags |= O_RDONLY | O_DIRECTORY;
1549 return open_path(name, flags);
1553 open_path(pathname_t *name, int oflag)
1555 char buf[NAME_MAX + 1];
1559 rval = open(name->path, oflag);
1560 if (rval >= 0 || errno != ENAMETOOLONG)
1562 separate_pathname(name, buf, &newname);
1563 if (chdir(buf) == 0) {
1564 rval = open_path(&newname, oflag);
1565 assert(chdir("..") == 0);
1567 free_pathname(&newname);
1572 opendir_path(pathname_t *name)
1574 char buf[NAME_MAX + 1];
1578 rval = opendir(name->path);
1579 if (rval || errno != ENAMETOOLONG)
1581 separate_pathname(name, buf, &newname);
1582 if (chdir(buf) == 0) {
1583 rval = opendir_path(&newname);
1584 assert(chdir("..") == 0);
1586 free_pathname(&newname);
1591 process_freq(char *arg)
1596 s = strchr(arg, '=');
1598 fprintf(stderr, "bad argument '%s'\n", arg);
1602 for (p = ops; p < ops_end; p++) {
1603 if (strcmp(arg, p->name) == 0) {
1608 fprintf(stderr, "can't find op type %s for -f\n", arg);
1613 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1615 char buf[NAME_MAX + 1];
1619 rval = readlink(name->path, lbuf, lbufsiz);
1620 if (rval >= 0 || errno != ENAMETOOLONG)
1622 separate_pathname(name, buf, &newname);
1623 if (chdir(buf) == 0) {
1624 rval = readlink_path(&newname, lbuf, lbufsiz);
1625 assert(chdir("..") == 0);
1627 free_pathname(&newname);
1632 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1634 char buf1[NAME_MAX + 1];
1635 char buf2[NAME_MAX + 1];
1637 pathname_t newname1;
1638 pathname_t newname2;
1642 rval = rename(name1->path, name2->path);
1644 rval = renameat2(AT_FDCWD, name1->path,
1645 AT_FDCWD, name2->path, mode);
1646 if (rval >= 0 || errno != ENAMETOOLONG)
1648 separate_pathname(name1, buf1, &newname1);
1649 separate_pathname(name2, buf2, &newname2);
1650 if (strcmp(buf1, buf2) == 0) {
1651 if (chdir(buf1) == 0) {
1652 rval = rename_path(&newname1, &newname2, mode);
1653 assert(chdir("..") == 0);
1656 if (strcmp(buf1, "..") == 0)
1658 else if (strcmp(buf2, "..") == 0)
1660 else if (strlen(buf1) == 0)
1662 else if (strlen(buf2) == 0)
1665 down1 = MAX(newname1.len, 3 + name2->len) <=
1666 MAX(3 + name1->len, newname2.len);
1668 free_pathname(&newname2);
1669 append_pathname(&newname2, "../");
1670 append_pathname(&newname2, name2->path);
1671 if (chdir(buf1) == 0) {
1672 rval = rename_path(&newname1, &newname2, mode);
1673 assert(chdir("..") == 0);
1676 free_pathname(&newname1);
1677 append_pathname(&newname1, "../");
1678 append_pathname(&newname1, name1->path);
1679 if (chdir(buf2) == 0) {
1680 rval = rename_path(&newname1, &newname2, mode);
1681 assert(chdir("..") == 0);
1685 free_pathname(&newname1);
1686 free_pathname(&newname2);
1691 rmdir_path(pathname_t *name)
1693 char buf[NAME_MAX + 1];
1697 rval = rmdir(name->path);
1698 if (rval >= 0 || errno != ENAMETOOLONG)
1700 separate_pathname(name, buf, &newname);
1701 if (chdir(buf) == 0) {
1702 rval = rmdir_path(&newname);
1703 assert(chdir("..") == 0);
1705 free_pathname(&newname);
1710 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1714 init_pathname(newname);
1715 slash = strchr(name->path, '/');
1716 if (slash == NULL) {
1721 strcpy(buf, name->path);
1723 append_pathname(newname, slash + 1);
1729 show_ops(int flag, char *lead_str)
1734 /* print in list form */
1737 for (p = ops; p < ops_end; p++) {
1738 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1739 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1740 x+=printf("%s ", p->name);
1743 } else if (flag == 0) {
1744 /* Table view style */
1746 for (f = 0, p = ops; p < ops_end; p++)
1752 for (p = ops; p < ops_end; p++) {
1753 if (flag != 0 || p->freq > 0) {
1754 if (lead_str != NULL)
1755 printf("%s", lead_str);
1756 printf("%20s %d/%d %s\n",
1757 p->name, p->freq, f,
1758 (p->iswrite == 0) ? " " : "write op");
1762 /* Command line style */
1763 if (lead_str != NULL)
1764 printf("%s", lead_str);
1765 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1767 for (p = ops; p < ops_end; p++)
1769 printf("-f %s=%d \\\n",p->name, p->freq);
1774 stat64_path(pathname_t *name, struct stat64 *sbuf)
1776 char buf[NAME_MAX + 1];
1780 rval = stat64(name->path, sbuf);
1781 if (rval >= 0 || errno != ENAMETOOLONG)
1783 separate_pathname(name, buf, &newname);
1784 if (chdir(buf) == 0) {
1785 rval = stat64_path(&newname, sbuf);
1786 assert(chdir("..") == 0);
1788 free_pathname(&newname);
1793 symlink_path(const char *name1, pathname_t *name)
1795 char buf[NAME_MAX + 1];
1799 if (!strcmp(name1, name->path)) {
1800 printf("yikes! %s %s\n", name1, name->path);
1804 rval = symlink(name1, name->path);
1805 if (rval >= 0 || errno != ENAMETOOLONG)
1807 separate_pathname(name, buf, &newname);
1808 if (chdir(buf) == 0) {
1809 rval = symlink_path(name1, &newname);
1810 assert(chdir("..") == 0);
1812 free_pathname(&newname);
1817 truncate64_path(pathname_t *name, off64_t length)
1819 char buf[NAME_MAX + 1];
1823 rval = truncate64(name->path, length);
1824 if (rval >= 0 || errno != ENAMETOOLONG)
1826 separate_pathname(name, buf, &newname);
1827 if (chdir(buf) == 0) {
1828 rval = truncate64_path(&newname, length);
1829 assert(chdir("..") == 0);
1831 free_pathname(&newname);
1836 unlink_path(pathname_t *name)
1838 char buf[NAME_MAX + 1];
1842 rval = unlink(name->path);
1843 if (rval >= 0 || errno != ENAMETOOLONG)
1845 separate_pathname(name, buf, &newname);
1846 if (chdir(buf) == 0) {
1847 rval = unlink_path(&newname);
1848 assert(chdir("..") == 0);
1850 free_pathname(&newname);
1857 printf("Usage: %s -H or\n", myprog);
1858 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1860 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1862 printf(" -c clean up the test directory after each run\n");
1863 printf(" -d dir specifies the base directory for operations\n");
1864 printf(" -e errtg specifies error injection stuff\n");
1865 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1866 printf(" the valid operation names are:\n");
1868 printf(" -i filenum get verbose output for this nth file object\n");
1869 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1870 printf(" *use 0 for infinite (default 1)\n");
1871 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1872 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1873 printf(" -o logfile specifies logfile name\n");
1874 printf(" -p nproc specifies the no. of processes (default 1)\n");
1875 printf(" -r specifies random name padding\n");
1876 printf(" -s seed specifies the seed for the random generator (default random)\n");
1877 printf(" -v specifies verbose mode\n");
1878 printf(" -w zeros frequencies of non-write operations\n");
1879 printf(" -x cmd execute command in the middle of operations\n");
1880 printf(" -z zeros frequencies of all operations\n");
1881 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1882 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1883 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1884 printf(" -H prints usage and exits\n");
1892 for (p = ops; p < ops_end; p++) {
1903 for (p = ops; p < ops_end; p++)
1907 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1909 opty_t btrfs_ops[] = {
1916 non_btrfs_freq(const char *path)
1919 #ifdef HAVE_BTRFSUTIL_H
1920 enum btrfs_util_error e;
1922 e = btrfs_util_is_subvolume(path);
1923 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1926 for (p = ops; p < ops_end; p++) {
1928 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1929 if (p->op == btrfs_ops[i]) {
1937 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1940 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1941 verifiable_log ? -1: (long)s->st_ino,
1942 (long)s->st_nlink, s->st_uid, s->st_gid,
1943 (long long) s->st_blocks, (long long) s->st_size);
1947 afsync_f(int opno, long r)
1955 struct iocb *iocbs[] = { &iocb };
1956 struct io_event event;
1959 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1961 printf("%d/%d: afsync - no filename\n", procid, opno);
1965 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1966 e = fd < 0 ? errno : 0;
1970 printf("%d/%d: afsync - open %s failed %d\n",
1971 procid, opno, f.path, e);
1976 io_prep_fsync(&iocb, fd);
1977 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1979 printf("%d/%d: afsync - io_submit %s %d\n",
1980 procid, opno, f.path, e);
1985 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1987 printf("%d/%d: afsync - io_getevents failed %d\n",
1996 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2003 allocsp_f(int opno, long r)
2008 struct xfs_flock64 fl;
2016 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2018 printf("%d/%d: allocsp - no filename\n", procid, opno);
2022 fd = open_path(&f, O_RDWR);
2023 e = fd < 0 ? errno : 0;
2027 printf("%d/%d: allocsp - open %s failed %d\n",
2028 procid, opno, f.path, e);
2032 if (fstat64(fd, &stb) < 0) {
2034 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2035 procid, opno, f.path, errno);
2040 inode_info(st, sizeof(st), &stb, v);
2041 lr = ((int64_t)random() << 32) + random();
2042 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2044 fl.l_whence = SEEK_SET;
2047 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2049 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2050 procid, opno, f.path, st, (long long)off, e);
2058 do_aio_rw(int opno, long r, int flags)
2062 struct dioattr diob;
2074 struct io_event event;
2075 struct iocb *iocbs[] = { &iocb };
2076 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2079 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2081 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2085 fd = open_path(&f, flags|O_DIRECT);
2086 e = fd < 0 ? errno : 0;
2090 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2091 procid, opno, f.path, e);
2095 if (fstat64(fd, &stb) < 0) {
2097 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2098 procid, opno, f.path, errno);
2103 inode_info(st, sizeof(st), &stb, v);
2104 if (!iswrite && stb.st_size == 0) {
2106 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2112 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2115 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2116 " fallback to stat()\n",
2117 procid, opno, f.path, st, errno);
2118 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2119 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2121 dio_env = getenv("XFS_DIO_MIN");
2123 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2124 align = (int64_t)diob.d_miniosz;
2125 lr = ((int64_t)random() << 32) + random();
2126 len = (random() % FILELEN_MAX) + 1;
2127 len -= (len % align);
2130 else if (len > diob.d_maxiosz)
2131 len = diob.d_maxiosz;
2132 buf = memalign(diob.d_mem, len);
2135 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2136 off -= (off % align);
2138 memset(buf, nameseq & 0xff, len);
2139 io_prep_pwrite(&iocb, fd, buf, len, off);
2141 off = (off64_t)(lr % stb.st_size);
2142 off -= (off % align);
2143 io_prep_pread(&iocb, fd, buf, len, off);
2145 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2147 printf("%d/%d: %s - io_submit failed %d\n",
2148 procid, opno, iswrite ? "awrite" : "aread", e);
2153 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2155 printf("%d/%d: %s - io_getevents failed %d\n",
2156 procid, opno, iswrite ? "awrite" : "aread", e);
2162 e = event.res != len ? event.res2 : 0;
2165 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2166 procid, opno, iswrite ? "awrite" : "aread",
2167 f.path, st, (long long)off, (int)len, e);
2174 aread_f(int opno, long r)
2177 do_aio_rw(opno, r, O_RDONLY);
2182 attr_remove_f(int opno, long r)
2184 attrlist_ent_t *aep;
2188 attrlist_cursor_t cursor;
2197 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2198 append_pathname(&f, ".");
2200 bzero(&cursor, sizeof(cursor));
2202 bzero(buf, sizeof(buf));
2203 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2207 alist = (attrlist_t *)buf;
2208 total += alist->al_count;
2209 } while (alist->al_more);
2212 printf("%d/%d: attr_remove - no attrs for %s\n",
2213 procid, opno, f.path);
2217 which = (int)(random() % total);
2218 bzero(&cursor, sizeof(cursor));
2222 bzero(buf, sizeof(buf));
2223 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2227 alist = (attrlist_t *)buf;
2228 if (which < ent + alist->al_count) {
2229 aep = (attrlist_ent_t *)
2230 &buf[alist->al_offset[which - ent]];
2231 aname = aep->a_name;
2234 ent += alist->al_count;
2235 } while (alist->al_more);
2236 if (aname == NULL) {
2239 "%d/%d: attr_remove - name %d not found at %s\n",
2240 procid, opno, which, f.path);
2244 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2247 printf("%d/%d: attr_remove %s %s %d\n",
2248 procid, opno, f.path, aname, e);
2253 attr_set_f(int opno, long r)
2260 static int lengths[] = { 10, 100, 1000, 10000 };
2265 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2266 append_pathname(&f, ".");
2267 sprintf(aname, "a%x", nameseq++);
2268 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2269 len = (int)(random() % lengths[li]);
2273 memset(aval, nameseq & 0xff, len);
2274 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2279 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2285 awrite_f(int opno, long r)
2288 do_aio_rw(opno, r, O_WRONLY);
2293 bulkstat_f(int opno, long r)
2299 struct xfs_bstat *t;
2301 struct xfs_fsop_bulkreq bsr;
2304 nent = (r % 999) + 2;
2305 t = malloc(nent * sizeof(*t));
2306 fd = open(".", O_RDONLY);
2314 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2318 printf("%d/%d: bulkstat nent %d total %lld\n",
2319 procid, opno, nent, (long long)total);
2324 bulkstat1_f(int opno, long r)
2334 struct xfs_fsop_bulkreq bsr;
2337 good = random() & 1;
2339 /* use an inode we know exists */
2341 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2342 append_pathname(&f, ".");
2343 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2348 * pick a random inode
2350 * note this can generate kernel warning messages
2351 * since bulkstat_one will read the disk block that
2352 * would contain a given inode even if that disk
2353 * block doesn't contain inodes.
2355 * this is detected later, but not until after the
2356 * warning is displayed.
2358 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2364 fd = open(".", O_RDONLY);
2370 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2372 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2373 procid, opno, good?"real":"random",
2374 verifiable_log ? -1LL : (long long)ino, e);
2379 chown_f(int opno, long r)
2389 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2390 append_pathname(&f, ".");
2391 u = (uid_t)random();
2392 g = (gid_t)random();
2393 nbits = (int)(random() % idmodulo);
2394 u &= (1 << nbits) - 1;
2395 g &= (1 << nbits) - 1;
2396 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2399 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2403 /* reflink some arbitrary range of f1 to f2. */
2410 struct file_clone_range fcr;
2411 struct pathname fpath1;
2412 struct pathname fpath2;
2413 struct stat64 stat1;
2414 struct stat64 stat2;
2415 char inoinfo1[1024];
2416 char inoinfo2[1024];
2430 init_pathname(&fpath1);
2431 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2433 printf("%d/%d: clonerange read - no filename\n",
2438 init_pathname(&fpath2);
2439 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2441 printf("%d/%d: clonerange write - no filename\n",
2447 fd1 = open_path(&fpath1, O_RDONLY);
2448 e = fd1 < 0 ? errno : 0;
2452 printf("%d/%d: clonerange read - open %s failed %d\n",
2453 procid, opno, fpath1.path, e);
2457 fd2 = open_path(&fpath2, O_WRONLY);
2458 e = fd2 < 0 ? errno : 0;
2462 printf("%d/%d: clonerange write - open %s failed %d\n",
2463 procid, opno, fpath2.path, e);
2467 /* Get file stats */
2468 if (fstat64(fd1, &stat1) < 0) {
2470 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2471 procid, opno, fpath1.path, errno);
2474 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2476 if (fstat64(fd2, &stat2) < 0) {
2478 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2479 procid, opno, fpath2.path, errno);
2482 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2484 /* Calculate offsets */
2485 len = (random() % FILELEN_MAX) + 1;
2486 len &= ~(stat1.st_blksize - 1);
2488 len = stat1.st_blksize;
2489 if (len > stat1.st_size)
2490 len = stat1.st_size;
2492 lr = ((int64_t)random() << 32) + random();
2493 if (stat1.st_size == len)
2496 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2498 off1 &= ~(stat1.st_blksize - 1);
2501 * If srcfile == destfile, randomly generate destination ranges
2502 * until we find one that doesn't overlap the source range.
2504 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2506 lr = ((int64_t)random() << 32) + random();
2507 off2 = (off64_t)(lr % max_off2);
2509 off2 &= ~(stat2.st_blksize - 1);
2510 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2512 /* Clone data blocks */
2514 fcr.src_offset = off1;
2515 fcr.src_length = len;
2516 fcr.dest_offset = off2;
2518 ret = ioctl(fd2, FICLONERANGE, &fcr);
2519 e = ret < 0 ? errno : 0;
2521 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2523 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2524 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2527 printf(" error %d", e);
2536 free_pathname(&fpath2);
2538 free_pathname(&fpath1);
2542 /* copy some arbitrary range of f1 to f2. */
2548 #ifdef HAVE_COPY_FILE_RANGE
2549 struct pathname fpath1;
2550 struct pathname fpath2;
2551 struct stat64 stat1;
2552 struct stat64 stat2;
2553 char inoinfo1[1024];
2554 char inoinfo2[1024];
2572 init_pathname(&fpath1);
2573 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2575 printf("%d/%d: copyrange read - no filename\n",
2580 init_pathname(&fpath2);
2581 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2583 printf("%d/%d: copyrange write - no filename\n",
2589 fd1 = open_path(&fpath1, O_RDONLY);
2590 e = fd1 < 0 ? errno : 0;
2594 printf("%d/%d: copyrange read - open %s failed %d\n",
2595 procid, opno, fpath1.path, e);
2599 fd2 = open_path(&fpath2, O_WRONLY);
2600 e = fd2 < 0 ? errno : 0;
2604 printf("%d/%d: copyrange write - open %s failed %d\n",
2605 procid, opno, fpath2.path, e);
2609 /* Get file stats */
2610 if (fstat64(fd1, &stat1) < 0) {
2612 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2613 procid, opno, fpath1.path, errno);
2616 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2618 if (fstat64(fd2, &stat2) < 0) {
2620 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2621 procid, opno, fpath2.path, errno);
2624 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2626 /* Calculate offsets */
2627 len = (random() % FILELEN_MAX) + 1;
2629 len = stat1.st_blksize;
2630 if (len > stat1.st_size)
2631 len = stat1.st_size;
2633 lr = ((int64_t)random() << 32) + random();
2634 if (stat1.st_size == len)
2637 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2641 * If srcfile == destfile, randomly generate destination ranges
2642 * until we find one that doesn't overlap the source range.
2644 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2646 lr = ((int64_t)random() << 32) + random();
2647 off2 = (off64_t)(lr % max_off2);
2649 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2652 * Since len, off1 and off2 will be changed later, preserve their
2660 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2663 if (errno != EAGAIN || tries++ >= 300)
2665 } else if (ret > len || ret == 0)
2670 e = ret < 0 ? errno : 0;
2672 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2674 fpath1.path, inoinfo1,
2675 (long long)offset1, (long long)length,
2676 fpath2.path, inoinfo2,
2677 (long long)offset2, (long long)length);
2680 printf(" error %d", e);
2681 else if (len && ret > len)
2682 printf(" asked for %lld, copied %lld??\n",
2683 (long long)len, (long long)ret);
2692 free_pathname(&fpath2);
2694 free_pathname(&fpath1);
2698 /* dedupe some arbitrary range of f1 to f2...fn. */
2704 #ifdef FIDEDUPERANGE
2705 #define INFO_SZ 1024
2706 struct file_dedupe_range *fdr;
2707 struct pathname *fpath;
2708 struct stat64 *stat;
2720 if (flist[FT_REG].nfiles < 2)
2723 /* Pick somewhere between 2 and 128 files. */
2725 nr = random() % (flist[FT_REG].nfiles + 1);
2726 } while (nr < 2 || nr > 128);
2729 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2730 sizeof(struct file_dedupe_range));
2732 printf("%d/%d: line %d error %d\n",
2733 procid, opno, __LINE__, errno);
2736 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2737 sizeof(struct file_dedupe_range)));
2739 fpath = calloc(nr, sizeof(struct pathname));
2741 printf("%d/%d: line %d error %d\n",
2742 procid, opno, __LINE__, errno);
2746 stat = calloc(nr, sizeof(struct stat64));
2748 printf("%d/%d: line %d error %d\n",
2749 procid, opno, __LINE__, errno);
2753 info = calloc(nr, INFO_SZ);
2755 printf("%d/%d: line %d error %d\n",
2756 procid, opno, __LINE__, errno);
2760 off = calloc(nr, sizeof(off64_t));
2762 printf("%d/%d: line %d error %d\n",
2763 procid, opno, __LINE__, errno);
2767 v = calloc(nr, sizeof(int));
2769 printf("%d/%d: line %d error %d\n",
2770 procid, opno, __LINE__, errno);
2773 fd = calloc(nr, sizeof(int));
2775 printf("%d/%d: line %d error %d\n",
2776 procid, opno, __LINE__, errno);
2779 memset(fd, 0xFF, nr * sizeof(int));
2781 /* Get paths for all files */
2782 for (i = 0; i < nr; i++)
2783 init_pathname(&fpath[i]);
2785 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2787 printf("%d/%d: deduperange read - no filename\n",
2792 for (i = 1; i < nr; i++) {
2793 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2795 printf("%d/%d: deduperange write - no filename\n",
2802 fd[0] = open_path(&fpath[0], O_RDONLY);
2803 e = fd[0] < 0 ? errno : 0;
2807 printf("%d/%d: deduperange read - open %s failed %d\n",
2808 procid, opno, fpath[0].path, e);
2812 for (i = 1; i < nr; i++) {
2813 fd[i] = open_path(&fpath[i], O_WRONLY);
2814 e = fd[i] < 0 ? errno : 0;
2818 printf("%d/%d: deduperange write - open %s failed %d\n",
2819 procid, opno, fpath[i].path, e);
2824 /* Get file stats */
2825 if (fstat64(fd[0], &stat[0]) < 0) {
2827 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2828 procid, opno, fpath[0].path, errno);
2832 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2834 for (i = 1; i < nr; i++) {
2835 if (fstat64(fd[i], &stat[i]) < 0) {
2837 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2838 procid, opno, fpath[i].path, errno);
2841 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2844 /* Never try to dedupe more than half of the src file. */
2845 len = (random() % FILELEN_MAX) + 1;
2846 len &= ~(stat[0].st_blksize - 1);
2848 len = stat[0].st_blksize / 2;
2849 if (len > stat[0].st_size / 2)
2850 len = stat[0].st_size / 2;
2852 /* Calculate offsets */
2853 lr = ((int64_t)random() << 32) + random();
2854 if (stat[0].st_size == len)
2857 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2859 off[0] &= ~(stat[0].st_blksize - 1);
2862 * If srcfile == destfile[i], randomly generate destination ranges
2863 * until we find one that doesn't overlap the source range.
2865 for (i = 1; i < nr; i++) {
2869 lr = ((int64_t)random() << 32) + random();
2870 if (stat[i].st_size <= len)
2873 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2875 off[i] &= ~(stat[i].st_blksize - 1);
2876 } while (stat[0].st_ino == stat[i].st_ino &&
2877 llabs(off[i] - off[0]) < len &&
2881 /* Clone data blocks */
2882 fdr->src_offset = off[0];
2883 fdr->src_length = len;
2884 fdr->dest_count = nr - 1;
2885 for (i = 1; i < nr; i++) {
2886 fdr->info[i - 1].dest_fd = fd[i];
2887 fdr->info[i - 1].dest_offset = off[i];
2890 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2891 e = ret < 0 ? errno : 0;
2893 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2895 fpath[0].path, &info[0], (long long)off[0],
2898 printf(" error %d", e);
2904 for (i = 1; i < nr; i++) {
2905 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2907 printf("%d/%d: ...to %s%s [%lld,%lld]",
2909 fpath[i].path, &info[i * INFO_SZ],
2910 (long long)off[i], (long long)len);
2911 if (fdr->info[i - 1].status < 0)
2912 printf(" error %d", e);
2913 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2914 printf(" %llu bytes deduplicated",
2915 fdr->info[i - 1].bytes_deduped);
2916 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2917 printf(" differed");
2923 for (i = 0; i < nr; i++)
2927 for (i = 0; i < nr; i++)
2928 free_pathname(&fpath[i]);
2947 setxattr_f(int opno, long r)
2949 #ifdef XFS_XFLAG_EXTSIZE
2959 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2960 append_pathname(&f, ".");
2961 fd = open_path(&f, O_RDWR);
2962 e = fd < 0 ? errno : 0;
2967 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2968 nbits = (int)(random() % e);
2969 p &= (1 << nbits) - 1;
2971 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2973 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2976 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2983 splice_f(int opno, long r)
2985 struct pathname fpath1;
2986 struct pathname fpath2;
2987 struct stat64 stat1;
2988 struct stat64 stat2;
2989 char inoinfo1[1024];
2990 char inoinfo2[1024];
2994 loff_t offset1, offset2;
3001 ssize_t ret1 = 0, ret2 = 0;
3007 init_pathname(&fpath1);
3008 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3010 printf("%d/%d: splice read - no filename\n",
3015 init_pathname(&fpath2);
3016 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3018 printf("%d/%d: splice write - no filename\n",
3024 fd1 = open_path(&fpath1, O_RDONLY);
3025 e = fd1 < 0 ? errno : 0;
3029 printf("%d/%d: splice read - open %s failed %d\n",
3030 procid, opno, fpath1.path, e);
3034 fd2 = open_path(&fpath2, O_WRONLY);
3035 e = fd2 < 0 ? errno : 0;
3039 printf("%d/%d: splice write - open %s failed %d\n",
3040 procid, opno, fpath2.path, e);
3044 /* Get file stats */
3045 if (fstat64(fd1, &stat1) < 0) {
3047 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3048 procid, opno, fpath1.path, errno);
3051 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3053 if (fstat64(fd2, &stat2) < 0) {
3055 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3056 procid, opno, fpath2.path, errno);
3059 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3061 /* Calculate offsets */
3062 len = (random() % FILELEN_MAX) + 1;
3064 len = stat1.st_blksize;
3065 if (len > stat1.st_size)
3066 len = stat1.st_size;
3068 lr = ((int64_t)random() << 32) + random();
3069 if (stat1.st_size == len)
3072 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3076 * splice can overlap write, so the offset of the target file can be
3077 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3078 * past the current dest file EOF
3080 lr = ((int64_t)random() << 32) + random();
3081 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3084 * Since len, off1 and off2 will be changed later, preserve their
3091 /* Pipe initialize */
3092 if (pipe(filedes) < 0) {
3094 printf("%d/%d: splice - pipe failed %d\n",
3095 procid, opno, errno);
3103 /* move to pipe buffer */
3104 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3110 /* move from pipe buffer to dst file */
3112 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3125 if (ret1 < 0 || ret2 < 0)
3130 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3132 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3133 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3135 if (length && length > total)
3136 printf(" asked for %lld, spliced %lld??\n",
3137 (long long)length, (long long)total);
3148 free_pathname(&fpath2);
3150 free_pathname(&fpath1);
3154 creat_f(int opno, long r)
3169 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3174 e1 = (random() % 100);
3175 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3177 if (type == FT_RTF) /* rt always gets an extsize */
3178 extsize = (random() % 10) + 1;
3179 else if (e1 < 10) /* one-in-ten get an extsize */
3180 extsize = random() % 1024;
3184 e = generate_fname(fep, type, &f, &id, &v);
3188 (void)fent_to_name(&f, fep);
3189 printf("%d/%d: creat - no filename from %s\n",
3190 procid, opno, f.path);
3195 fd = creat_path(&f, 0666);
3196 e = fd < 0 ? errno : 0;
3201 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3202 if (type == FT_RTF) {
3203 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3204 a.fsx_extsize = extsize *
3205 geom.rtextsize * geom.blocksize;
3207 } else if (extsize) {
3208 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3209 a.fsx_extsize = extsize * geom.blocksize;
3212 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3215 add_to_flist(type, id, parid, 0);
3219 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3220 extsize ? a.fsx_extsize : 0, e, e1);
3221 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3227 dread_f(int opno, long r)
3231 struct dioattr diob;
3244 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3246 printf("%d/%d: dread - no filename\n", procid, opno);
3250 fd = open_path(&f, O_RDONLY|O_DIRECT);
3251 e = fd < 0 ? errno : 0;
3255 printf("%d/%d: dread - open %s failed %d\n",
3256 procid, opno, f.path, e);
3260 if (fstat64(fd, &stb) < 0) {
3262 printf("%d/%d: dread - fstat64 %s failed %d\n",
3263 procid, opno, f.path, errno);
3268 inode_info(st, sizeof(st), &stb, v);
3269 if (stb.st_size == 0) {
3271 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3277 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3280 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3281 " fallback to stat()\n",
3282 procid, opno, f.path, st, errno);
3283 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3284 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3287 dio_env = getenv("XFS_DIO_MIN");
3289 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3291 align = (int64_t)diob.d_miniosz;
3292 lr = ((int64_t)random() << 32) + random();
3293 off = (off64_t)(lr % stb.st_size);
3294 off -= (off % align);
3295 lseek64(fd, off, SEEK_SET);
3296 len = (random() % FILELEN_MAX) + 1;
3297 len -= (len % align);
3300 else if (len > diob.d_maxiosz)
3301 len = diob.d_maxiosz;
3302 buf = memalign(diob.d_mem, len);
3303 e = read(fd, buf, len) < 0 ? errno : 0;
3306 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3307 procid, opno, f.path, st, (long long)off, (int)len, e);
3313 dwrite_f(int opno, long r)
3317 struct dioattr diob;
3330 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3332 printf("%d/%d: dwrite - no filename\n", procid, opno);
3336 fd = open_path(&f, O_WRONLY|O_DIRECT);
3337 e = fd < 0 ? errno : 0;
3341 printf("%d/%d: dwrite - open %s failed %d\n",
3342 procid, opno, f.path, e);
3346 if (fstat64(fd, &stb) < 0) {
3348 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3349 procid, opno, f.path, errno);
3354 inode_info(st, sizeof(st), &stb, v);
3355 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3357 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3358 " %s%s return %d, fallback to stat()\n",
3359 procid, opno, f.path, st, errno);
3360 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3361 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3364 dio_env = getenv("XFS_DIO_MIN");
3366 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3368 align = (int64_t)diob.d_miniosz;
3369 lr = ((int64_t)random() << 32) + random();
3370 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3371 off -= (off % align);
3372 lseek64(fd, off, SEEK_SET);
3373 len = (random() % FILELEN_MAX) + 1;
3374 len -= (len % align);
3377 else if (len > diob.d_maxiosz)
3378 len = diob.d_maxiosz;
3379 buf = memalign(diob.d_mem, len);
3381 lseek64(fd, off, SEEK_SET);
3382 memset(buf, nameseq & 0xff, len);
3383 e = write(fd, buf, len) < 0 ? errno : 0;
3386 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3387 procid, opno, f.path, st, (long long)off, (int)len, e);
3393 #ifdef HAVE_LINUX_FALLOC_H
3394 struct print_flags falloc_flags [] = {
3395 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3396 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3397 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3398 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3399 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3400 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3404 #define translate_falloc_flags(mode) \
3405 ({translate_flags(mode, "|", falloc_flags);})
3409 do_fallocate(int opno, long r, int mode)
3411 #ifdef HAVE_LINUX_FALLOC_H
3423 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3425 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3429 fd = open_path(&f, O_RDWR);
3432 printf("%d/%d: do_fallocate - open %s failed %d\n",
3433 procid, opno, f.path, errno);
3438 if (fstat64(fd, &stb) < 0) {
3440 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3441 procid, opno, f.path, errno);
3446 inode_info(st, sizeof(st), &stb, v);
3447 lr = ((int64_t)random() << 32) + random();
3448 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3450 len = (off64_t)(random() % (1024 * 1024));
3452 * Collapse/insert range requires off and len to be block aligned,
3453 * make it more likely to be the case.
3455 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3457 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3458 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3460 mode |= FALLOC_FL_KEEP_SIZE & random();
3461 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3463 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3464 procid, opno, translate_falloc_flags(mode),
3465 f.path, st, (long long)off, (long long)len, e);
3472 fallocate_f(int opno, long r)
3474 #ifdef HAVE_LINUX_FALLOC_H
3475 do_fallocate(opno, r, 0);
3480 fdatasync_f(int opno, long r)
3488 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3490 printf("%d/%d: fdatasync - no filename\n",
3495 fd = open_path(&f, O_WRONLY);
3496 e = fd < 0 ? errno : 0;
3500 printf("%d/%d: fdatasync - open %s failed %d\n",
3501 procid, opno, f.path, e);
3505 e = fdatasync(fd) < 0 ? errno : 0;
3507 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3512 #ifdef HAVE_LINUX_FIEMAP_H
3513 struct print_flags fiemap_flags[] = {
3514 { FIEMAP_FLAG_SYNC, "SYNC"},
3515 { FIEMAP_FLAG_XATTR, "XATTR"},
3519 #define translate_fiemap_flags(mode) \
3520 ({translate_flags(mode, "|", fiemap_flags);})
3524 fiemap_f(int opno, long r)
3526 #ifdef HAVE_LINUX_FIEMAP_H
3536 struct fiemap *fiemap;
3539 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3541 printf("%d/%d: fiemap - no filename\n", procid, opno);
3545 fd = open_path(&f, O_RDWR);
3546 e = fd < 0 ? errno : 0;
3550 printf("%d/%d: fiemap - open %s failed %d\n",
3551 procid, opno, f.path, e);
3555 if (fstat64(fd, &stb) < 0) {
3557 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3558 procid, opno, f.path, errno);
3563 inode_info(st, sizeof(st), &stb, v);
3564 blocks_to_map = random() & 0xffff;
3565 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3566 (blocks_to_map * sizeof(struct fiemap_extent)));
3569 printf("%d/%d: malloc failed \n", procid, opno);
3574 lr = ((int64_t)random() << 32) + random();
3575 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3577 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3578 fiemap->fm_extent_count = blocks_to_map;
3579 fiemap->fm_mapped_extents = random() & 0xffff;
3580 fiemap->fm_start = off;
3581 fiemap->fm_length = ((int64_t)random() << 32) + random();
3583 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3585 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3586 procid, opno, f.path, st, (long long)fiemap->fm_start,
3587 (long long) fiemap->fm_length,
3588 translate_fiemap_flags(fiemap->fm_flags), e);
3596 freesp_f(int opno, long r)
3601 struct xfs_flock64 fl;
3609 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3611 printf("%d/%d: freesp - no filename\n", procid, opno);
3615 fd = open_path(&f, O_RDWR);
3616 e = fd < 0 ? errno : 0;
3620 printf("%d/%d: freesp - open %s failed %d\n",
3621 procid, opno, f.path, e);
3625 if (fstat64(fd, &stb) < 0) {
3627 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3628 procid, opno, f.path, errno);
3633 inode_info(st, sizeof(st), &stb, v);
3634 lr = ((int64_t)random() << 32) + random();
3635 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3637 fl.l_whence = SEEK_SET;
3640 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3642 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3643 procid, opno, f.path, st, (long long)off, e);
3649 fsync_f(int opno, long r)
3657 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3659 printf("%d/%d: fsync - no filename\n", procid, opno);
3663 fd = open_file_or_dir(&f, O_WRONLY);
3664 e = fd < 0 ? errno : 0;
3668 printf("%d/%d: fsync - open %s failed %d\n",
3669 procid, opno, f.path, e);
3673 e = fsync(fd) < 0 ? errno : 0;
3675 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3681 gen_random_string(int len)
3683 static const char charset[] = "0123456789"
3684 "abcdefghijklmnopqrstuvwxyz"
3685 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3696 for (i = 0; i < len; i++)
3697 s[i] = charset[random() % sizeof(charset)];
3703 getattr_f(int opno, long r)
3712 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3713 append_pathname(&f, ".");
3714 fd = open_path(&f, O_RDWR);
3715 e = fd < 0 ? errno : 0;
3718 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3720 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3726 getdents_f(int opno, long r)
3733 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3734 append_pathname(&f, ".");
3735 dir = opendir_path(&f);
3739 printf("%d/%d: getdents - can't open %s\n",
3740 procid, opno, f.path);
3744 while (readdir64(dir) != NULL)
3747 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3753 getfattr_f(int opno, long r)
3759 char name[XATTR_NAME_BUF_SIZE];
3765 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3767 printf("%d/%d: getfattr - no filename\n", procid, opno);
3773 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3774 * to read a xattr that doesn't exist (fgetxattr should fail with
3775 * errno set to ENOATTR (61) in this case).
3777 if (fep->xattr_counter > 0)
3778 xattr_num = (random() % fep->xattr_counter) + 1;
3782 e = generate_xattr_name(xattr_num, name, sizeof(name));
3784 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3785 procid, opno, f.path, e);
3789 value_len = getxattr(f.path, name, NULL, 0);
3790 if (value_len < 0) {
3792 printf("%d/%d: getfattr file %s name %s failed %d\n",
3793 procid, opno, f.path, name, errno);
3797 /* A xattr without value.*/
3798 if (value_len == 0) {
3803 value = malloc(value_len);
3806 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3807 procid, opno, f.path, value_len);
3811 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3814 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3815 procid, opno, f.path, name, value_len, e);
3822 link_f(int opno, long r)
3836 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3838 printf("%d/%d: link - no file\n", procid, opno);
3842 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3848 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3852 (void)fent_to_name(&l, fep);
3853 printf("%d/%d: link - no filename from %s\n",
3854 procid, opno, l.path);
3860 e = link_path(&f, &l) < 0 ? errno : 0;
3863 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3865 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3867 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3874 listfattr_f(int opno, long r)
3880 char *buffer = NULL;
3884 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3886 printf("%d/%d: listfattr - no filename\n", procid, opno);
3891 e = listxattr(f.path, NULL, 0);
3894 printf("%d/%d: listfattr %s failed %d\n",
3895 procid, opno, f.path, errno);
3899 if (buffer_len == 0) {
3901 printf("%d/%d: listfattr %s - has no extended attributes\n",
3902 procid, opno, f.path);
3906 buffer = malloc(buffer_len);
3909 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3910 procid, opno, f.path, buffer_len);
3914 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3916 printf("%d/%d: listfattr %s buffer length %d %d\n",
3917 procid, opno, f.path, buffer_len, e);
3924 mkdir_f(int opno, long r)
3934 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
3939 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3943 (void)fent_to_name(&f, fep);
3944 printf("%d/%d: mkdir - no filename from %s\n",
3945 procid, opno, f.path);
3950 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3953 add_to_flist(FT_DIR, id, parid, 0);
3955 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3956 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3962 mknod_f(int opno, long r)
3972 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
3977 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3981 (void)fent_to_name(&f, fep);
3982 printf("%d/%d: mknod - no filename from %s\n",
3983 procid, opno, f.path);
3988 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3991 add_to_flist(FT_DEV, id, parid, 0);
3993 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3994 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3999 #ifdef HAVE_SYS_MMAN_H
4000 struct print_flags mmap_flags[] = {
4001 { MAP_SHARED, "SHARED"},
4002 { MAP_PRIVATE, "PRIVATE"},
4006 #define translate_mmap_flags(flags) \
4007 ({translate_flags(flags, "|", mmap_flags);})
4011 do_mmap(int opno, long r, int prot)
4013 #ifdef HAVE_SYS_MMAN_H
4025 sigjmp_buf sigbus_jmpbuf;
4028 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4030 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4034 fd = open_path(&f, O_RDWR);
4035 e = fd < 0 ? errno : 0;
4039 printf("%d/%d: do_mmap - open %s failed %d\n",
4040 procid, opno, f.path, e);
4044 if (fstat64(fd, &stb) < 0) {
4046 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4047 procid, opno, f.path, errno);
4052 inode_info(st, sizeof(st), &stb, v);
4053 if (stb.st_size == 0) {
4055 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4062 lr = ((int64_t)random() << 32) + random();
4063 off = (off64_t)(lr % stb.st_size);
4064 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4065 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4067 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4068 addr = mmap(NULL, len, prot, flags, fd, off);
4069 e = (addr == MAP_FAILED) ? errno : 0;
4072 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4073 procid, opno, f.path, st, (long long)off,
4074 (int)len, translate_mmap_flags(flags), e);
4080 if (prot & PROT_WRITE) {
4081 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4082 sigbus_jmp = &sigbus_jmpbuf;
4083 memset(addr, nameseq & 0xff, len);
4087 if ((buf = malloc(len)) != NULL) {
4088 memcpy(buf, addr, len);
4093 /* set NULL to stop other functions from doing siglongjmp */
4097 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4098 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4099 f.path, st, (long long)off, (int)len,
4100 translate_mmap_flags(flags),
4101 (e == 0) ? "0" : "Bus error");
4109 mread_f(int opno, long r)
4111 #ifdef HAVE_SYS_MMAN_H
4112 do_mmap(opno, r, PROT_READ);
4117 mwrite_f(int opno, long r)
4119 #ifdef HAVE_SYS_MMAN_H
4120 do_mmap(opno, r, PROT_WRITE);
4125 punch_f(int opno, long r)
4127 #ifdef HAVE_LINUX_FALLOC_H
4128 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4133 zero_f(int opno, long r)
4135 #ifdef HAVE_LINUX_FALLOC_H
4136 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4141 collapse_f(int opno, long r)
4143 #ifdef HAVE_LINUX_FALLOC_H
4144 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4149 insert_f(int opno, long r)
4151 #ifdef HAVE_LINUX_FALLOC_H
4152 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4157 read_f(int opno, long r)
4171 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4173 printf("%d/%d: read - no filename\n", procid, opno);
4177 fd = open_path(&f, O_RDONLY);
4178 e = fd < 0 ? errno : 0;
4182 printf("%d/%d: read - open %s failed %d\n",
4183 procid, opno, f.path, e);
4187 if (fstat64(fd, &stb) < 0) {
4189 printf("%d/%d: read - fstat64 %s failed %d\n",
4190 procid, opno, f.path, errno);
4195 inode_info(st, sizeof(st), &stb, v);
4196 if (stb.st_size == 0) {
4198 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4204 lr = ((int64_t)random() << 32) + random();
4205 off = (off64_t)(lr % stb.st_size);
4206 lseek64(fd, off, SEEK_SET);
4207 len = (random() % FILELEN_MAX) + 1;
4209 e = read(fd, buf, len) < 0 ? errno : 0;
4212 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4213 procid, opno, f.path, st, (long long)off, (int)len, e);
4219 readlink_f(int opno, long r)
4227 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4229 printf("%d/%d: readlink - no filename\n", procid, opno);
4233 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4236 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4241 readv_f(int opno, long r)
4253 struct iovec *iov = NULL;
4260 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4262 printf("%d/%d: readv - no filename\n", procid, opno);
4266 fd = open_path(&f, O_RDONLY);
4267 e = fd < 0 ? errno : 0;
4271 printf("%d/%d: readv - open %s failed %d\n",
4272 procid, opno, f.path, e);
4276 if (fstat64(fd, &stb) < 0) {
4278 printf("%d/%d: readv - fstat64 %s failed %d\n",
4279 procid, opno, f.path, errno);
4284 inode_info(st, sizeof(st), &stb, v);
4285 if (stb.st_size == 0) {
4287 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4293 lr = ((int64_t)random() << 32) + random();
4294 off = (off64_t)(lr % stb.st_size);
4295 lseek64(fd, off, SEEK_SET);
4296 len = (random() % FILELEN_MAX) + 1;
4299 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4300 iov = calloc(iovcnt, sizeof(struct iovec));
4301 iovl = len / iovcnt;
4303 for (i=0; i<iovcnt; i++) {
4304 (iov + i)->iov_base = (buf + iovb);
4305 (iov + i)->iov_len = iovl;
4309 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4312 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4313 procid, opno, f.path, st, (long long)off, (int)iovl,
4320 removefattr_f(int opno, long r)
4326 char name[XATTR_NAME_BUF_SIZE];
4330 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4332 printf("%d/%d: removefattr - no filename\n", procid, opno);
4338 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4339 * remove a xattr that doesn't exist (fremovexattr should fail with
4340 * errno set to ENOATTR (61) in this case).
4342 if (fep->xattr_counter > 0)
4343 xattr_num = (random() % fep->xattr_counter) + 1;
4347 e = generate_xattr_name(xattr_num, name, sizeof(name));
4349 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4350 procid, opno, f.path, e);
4354 e = removexattr(f.path, name) < 0 ? errno : 0;
4356 printf("%d/%d: removefattr file %s name %s %d\n",
4357 procid, opno, f.path, name, e);
4362 struct print_flags renameat2_flags [] = {
4363 { RENAME_NOREPLACE, "NOREPLACE"},
4364 { RENAME_EXCHANGE, "EXCHANGE"},
4365 { RENAME_WHITEOUT, "WHITEOUT"},
4369 #define translate_renameat2_flags(mode) \
4370 ({translate_flags(mode, "|", renameat2_flags);})
4373 do_renameat2(int opno, long r, int mode)
4389 /* get an existing path for the source of the rename */
4391 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4392 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4394 printf("%d/%d: rename - no source filename\n",
4401 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4402 * order to maintain filelist/filename integrity, we should
4403 * restrict exchange operation to files of the same type.
4405 if (mode == RENAME_EXCHANGE) {
4406 which = 1 << (flp - flist);
4407 init_pathname(&newf);
4408 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4410 printf("%d/%d: rename - no target filename\n",
4412 free_pathname(&newf);
4416 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4417 fents_ancestor_check(dfep, fep))) {
4419 printf("%d/%d: rename(REXCHANGE) %s and %s "
4420 "have ancestor-descendant relationship\n",
4421 procid, opno, f.path, newf.path);
4422 free_pathname(&newf);
4428 parid = dfep->parent;
4431 * Get an existing directory for the destination parent
4434 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4441 * Generate a new path using an existing parent directory
4444 init_pathname(&newf);
4445 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4449 (void)fent_to_name(&f, dfep);
4450 printf("%d/%d: rename - no filename from %s\n",
4451 procid, opno, f.path);
4453 free_pathname(&newf);
4458 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4461 int xattr_counter = fep->xattr_counter;
4462 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4463 int ft = flp - flist;
4466 oldparid = fep->parent;
4469 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4470 * old parent id for the others.
4472 if (ft == FT_DIR || ft == FT_SUBVOL)
4473 fix_parent(oldid, id, swap);
4475 if (mode == RENAME_WHITEOUT) {
4476 fep->xattr_counter = 0;
4477 add_to_flist(flp - flist, id, parid, xattr_counter);
4478 } else if (mode == RENAME_EXCHANGE) {
4479 fep->xattr_counter = dfep->xattr_counter;
4480 dfep->xattr_counter = xattr_counter;
4482 del_from_flist(flp - flist, fep - flp->fents);
4483 add_to_flist(flp - flist, id, parid, xattr_counter);
4487 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4488 opno, translate_renameat2_flags(mode), f.path,
4491 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4492 procid, opno, oldid, oldparid);
4493 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4494 procid, opno, id, parid);
4497 free_pathname(&newf);
4502 rename_f(int opno, long r)
4504 do_renameat2(opno, r, 0);
4508 rnoreplace_f(int opno, long r)
4510 do_renameat2(opno, r, RENAME_NOREPLACE);
4514 rexchange_f(int opno, long r)
4516 do_renameat2(opno, r, RENAME_EXCHANGE);
4520 rwhiteout_f(int opno, long r)
4522 do_renameat2(opno, r, RENAME_WHITEOUT);
4526 resvsp_f(int opno, long r)
4531 struct xfs_flock64 fl;
4539 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4541 printf("%d/%d: resvsp - no filename\n", procid, opno);
4545 fd = open_path(&f, O_RDWR);
4546 e = fd < 0 ? errno : 0;
4550 printf("%d/%d: resvsp - open %s failed %d\n",
4551 procid, opno, f.path, e);
4555 if (fstat64(fd, &stb) < 0) {
4557 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4558 procid, opno, f.path, errno);
4563 inode_info(st, sizeof(st), &stb, v);
4564 lr = ((int64_t)random() << 32) + random();
4565 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4567 fl.l_whence = SEEK_SET;
4569 fl.l_len = (off64_t)(random() % (1024 * 1024));
4570 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4572 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4573 procid, opno, f.path, st,
4574 (long long)off, (long long)fl.l_len, e);
4580 rmdir_f(int opno, long r)
4590 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4592 printf("%d/%d: rmdir - no directory\n", procid, opno);
4596 e = rmdir_path(&f) < 0 ? errno : 0;
4600 oldparid = fep->parent;
4601 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4604 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4606 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4607 procid, opno, oldid, oldparid);
4613 setattr_f(int opno, long r)
4622 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4623 append_pathname(&f, ".");
4624 fd = open_path(&f, O_RDWR);
4625 e = fd < 0 ? errno : 0;
4628 fl = attr_mask & (uint)random();
4629 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4631 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4637 setfattr_f(int opno, long r)
4644 char name[XATTR_NAME_BUF_SIZE];
4650 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4652 printf("%d/%d: setfattr - no filename\n", procid, opno);
4657 if ((fep->xattr_counter > 0) && (random() % 2)) {
4659 * Use an existing xattr name for replacing its value or
4660 * create again a xattr that was previously deleted.
4662 xattr_num = (random() % fep->xattr_counter) + 1;
4664 flag = XATTR_REPLACE;
4666 /* Use a new xattr name. */
4667 xattr_num = fep->xattr_counter + 1;
4669 * Don't always use the create flag because even if our xattr
4670 * counter is 0, we may still have xattrs associated to this
4671 * file (this happens when xattrs are added to a file through
4672 * one of its other hard links), so we can end up updating an
4673 * existing xattr too.
4676 flag = XATTR_CREATE;
4680 * The maximum supported value size depends on the filesystem
4681 * implementation, but 100 bytes is a safe value for most filesystems
4684 value_len = random() % 101;
4685 value = gen_random_string(value_len);
4686 if (!value && value_len > 0) {
4688 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4689 procid, opno, f.path, value_len);
4692 e = generate_xattr_name(xattr_num, name, sizeof(name));
4694 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4695 procid, opno, f.path, e);
4699 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4701 fep->xattr_counter++;
4703 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4704 procid, opno, f.path, name, xattr_flag_to_string(flag),
4712 snapshot_f(int opno, long r)
4714 #ifdef HAVE_BTRFSUTIL_H
4715 enum btrfs_util_error e;
4726 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4728 printf("%d/%d: snapshot - no subvolume\n", procid,
4733 init_pathname(&newf);
4735 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4739 (void)fent_to_name(&f, fep);
4740 printf("%d/%d: snapshot - no filename from %s\n",
4741 procid, opno, f.path);
4746 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4747 if (e == BTRFS_UTIL_OK)
4748 add_to_flist(FT_SUBVOL, id, parid, 0);
4750 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4751 f.path, newf.path, e, btrfs_util_strerror(e));
4752 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4755 free_pathname(&newf);
4761 stat_f(int opno, long r)
4769 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4771 printf("%d/%d: stat - no entries\n", procid, opno);
4775 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4778 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4783 subvol_create_f(int opno, long r)
4785 #ifdef HAVE_BTRFSUTIL_H
4786 enum btrfs_util_error e;
4796 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4800 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4804 (void)fent_to_name(&f, fep);
4805 printf("%d/%d: subvol_create - no filename from %s\n",
4806 procid, opno, f.path);
4811 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4812 if (e == BTRFS_UTIL_OK)
4813 add_to_flist(FT_SUBVOL, id, parid, 0);
4815 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4816 f.path, e, btrfs_util_strerror(e));
4817 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4825 subvol_delete_f(int opno, long r)
4827 #ifdef HAVE_BTRFSUTIL_H
4828 enum btrfs_util_error e;
4836 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4838 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4843 e = btrfs_util_delete_subvolume(f.path, 0);
4845 if (e == BTRFS_UTIL_OK) {
4847 oldparid = fep->parent;
4848 delete_subvol_children(oldid);
4849 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4852 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4853 e, btrfs_util_strerror(e));
4854 if (e == BTRFS_UTIL_OK)
4855 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4856 procid, opno, oldid, oldparid);
4863 symlink_f(int opno, long r)
4876 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4881 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4885 (void)fent_to_name(&f, fep);
4886 printf("%d/%d: symlink - no filename from %s\n",
4887 procid, opno, f.path);
4892 len = (int)(random() % PATH_MAX);
4893 val = malloc(len + 1);
4895 memset(val, 'x', len);
4897 for (i = 10; i < len - 1; i += 10)
4899 e = symlink_path(val, &f) < 0 ? errno : 0;
4902 add_to_flist(FT_SYM, id, parid, 0);
4905 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4906 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4913 sync_f(int opno, long r)
4917 printf("%d/%d: sync\n", procid, opno);
4921 truncate_f(int opno, long r)
4932 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4934 printf("%d/%d: truncate - no filename\n", procid, opno);
4938 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4942 printf("%d/%d: truncate - stat64 %s failed %d\n",
4943 procid, opno, f.path, e);
4947 inode_info(st, sizeof(st), &stb, v);
4948 lr = ((int64_t)random() << 32) + random();
4949 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4951 e = truncate64_path(&f, off) < 0 ? errno : 0;
4954 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4955 st, (long long)off, e);
4960 unlink_f(int opno, long r)
4971 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4973 printf("%d/%d: unlink - no file\n", procid, opno);
4977 e = unlink_path(&f) < 0 ? errno : 0;
4981 oldparid = fep->parent;
4982 del_from_flist(flp - flist, fep - flp->fents);
4985 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4987 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4988 procid, opno, oldid, oldparid);
4994 unresvsp_f(int opno, long r)
4999 struct xfs_flock64 fl;
5007 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5009 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5013 fd = open_path(&f, O_RDWR);
5014 e = fd < 0 ? errno : 0;
5018 printf("%d/%d: unresvsp - open %s failed %d\n",
5019 procid, opno, f.path, e);
5023 if (fstat64(fd, &stb) < 0) {
5025 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5026 procid, opno, f.path, errno);
5031 inode_info(st, sizeof(st), &stb, v);
5032 lr = ((int64_t)random() << 32) + random();
5033 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5035 fl.l_whence = SEEK_SET;
5037 fl.l_len = (off64_t)(random() % (1 << 20));
5038 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5040 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5041 procid, opno, f.path, st,
5042 (long long)off, (long long)fl.l_len, e);
5048 write_f(int opno, long r)
5062 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5064 printf("%d/%d: write - no filename\n", procid, opno);
5068 fd = open_path(&f, O_WRONLY);
5069 e = fd < 0 ? errno : 0;
5073 printf("%d/%d: write - open %s failed %d\n",
5074 procid, opno, f.path, e);
5078 if (fstat64(fd, &stb) < 0) {
5080 printf("%d/%d: write - fstat64 %s failed %d\n",
5081 procid, opno, f.path, errno);
5086 inode_info(st, sizeof(st), &stb, v);
5087 lr = ((int64_t)random() << 32) + random();
5088 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5090 lseek64(fd, off, SEEK_SET);
5091 len = (random() % FILELEN_MAX) + 1;
5093 memset(buf, nameseq & 0xff, len);
5094 e = write(fd, buf, len) < 0 ? errno : 0;
5097 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5098 procid, opno, f.path, st, (long long)off, (int)len, e);
5104 writev_f(int opno, long r)
5116 struct iovec *iov = NULL;
5123 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5125 printf("%d/%d: writev - no filename\n", procid, opno);
5129 fd = open_path(&f, O_WRONLY);
5130 e = fd < 0 ? errno : 0;
5134 printf("%d/%d: writev - open %s failed %d\n",
5135 procid, opno, f.path, e);
5139 if (fstat64(fd, &stb) < 0) {
5141 printf("%d/%d: writev - fstat64 %s failed %d\n",
5142 procid, opno, f.path, errno);
5147 inode_info(st, sizeof(st), &stb, v);
5148 lr = ((int64_t)random() << 32) + random();
5149 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5151 lseek64(fd, off, SEEK_SET);
5152 len = (random() % FILELEN_MAX) + 1;
5154 memset(buf, nameseq & 0xff, len);
5156 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5157 iov = calloc(iovcnt, sizeof(struct iovec));
5158 iovl = len / iovcnt;
5160 for (i=0; i<iovcnt; i++) {
5161 (iov + i)->iov_base = (buf + iovb);
5162 (iov + i)->iov_len = iovl;
5166 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5170 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5171 procid, opno, f.path, st, (long long)off, (int)iovl,
5178 xattr_flag_to_string(int flag)
5180 if (flag == XATTR_CREATE)
5182 if (flag == XATTR_REPLACE)