1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
13 #ifdef HAVE_BTRFSUTIL_H
14 #include <btrfsutil.h>
16 #ifdef HAVE_ATTR_ATTRIBUTES_H
17 #include <attr/attributes.h>
19 #ifdef HAVE_LINUX_FIEMAP_H
20 #include <linux/fiemap.h>
22 #ifndef HAVE_ATTR_LIST
23 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
32 #include <sys/syscall.h>
33 #include <sys/xattr.h>
35 #ifndef FS_IOC_GETFLAGS
36 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
38 #ifndef FS_IOC_SETFLAGS
39 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
43 #define XFS_ERRTAG_MAX 17
44 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
45 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
50 #ifndef HAVE_RENAMEAT2
51 #if !defined(SYS_renameat2) && defined(__x86_64__)
52 #define SYS_renameat2 316
55 #if !defined(SYS_renameat2) && defined(__i386__)
56 #define SYS_renameat2 353
59 static int renameat2(int dfd1, const char *path1,
60 int dfd2, const char *path2,
64 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
72 #ifndef RENAME_NOREPLACE
73 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
75 #ifndef RENAME_EXCHANGE
76 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
78 #ifndef RENAME_WHITEOUT
79 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
82 #define FILELEN_MAX (32*4096)
146 typedef void (*opfnc_t)(int, long);
148 typedef struct opdesc {
156 typedef struct fent {
163 typedef struct flist {
170 typedef struct pathname {
180 struct print_string {
187 #define FT_DIRm (1 << FT_DIR)
189 #define FT_REGm (1 << FT_REG)
191 #define FT_SYMm (1 << FT_SYM)
193 #define FT_DEVm (1 << FT_DEV)
195 #define FT_RTFm (1 << FT_RTF)
197 #define FT_SUBVOLm (1 << FT_SUBVOL)
199 #define FT_ANYm ((1 << FT_nft) - 1)
200 #define FT_REGFILE (FT_REGm | FT_RTFm)
201 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
202 #define FT_ANYDIR (FT_DIRm | FT_SUBVOLm)
204 #define FLIST_SLOT_INCR 16
207 #define MAXFSIZE ((1ULL << 63) - 1ULL)
208 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
210 #define XATTR_NAME_BUF_SIZE 18
212 void afsync_f(int, long);
213 void allocsp_f(int, long);
214 void aread_f(int, long);
215 void attr_remove_f(int, long);
216 void attr_set_f(int, long);
217 void awrite_f(int, long);
218 void bulkstat_f(int, long);
219 void bulkstat1_f(int, long);
220 void chown_f(int, long);
221 void clonerange_f(int, long);
222 void copyrange_f(int, long);
223 void creat_f(int, long);
224 void deduperange_f(int, long);
225 void dread_f(int, long);
226 void dwrite_f(int, long);
227 void fallocate_f(int, long);
228 void fdatasync_f(int, long);
229 void fiemap_f(int, long);
230 void freesp_f(int, long);
231 void fsync_f(int, long);
232 char *gen_random_string(int);
233 void getattr_f(int, long);
234 void getdents_f(int, long);
235 void getfattr_f(int, long);
236 void link_f(int, long);
237 void listfattr_f(int, long);
238 void mkdir_f(int, long);
239 void mknod_f(int, long);
240 void mread_f(int, long);
241 void mwrite_f(int, long);
242 void punch_f(int, long);
243 void zero_f(int, long);
244 void collapse_f(int, long);
245 void insert_f(int, long);
246 void read_f(int, long);
247 void readlink_f(int, long);
248 void readv_f(int, long);
249 void removefattr_f(int, long);
250 void rename_f(int, long);
251 void rnoreplace_f(int, long);
252 void rexchange_f(int, long);
253 void rwhiteout_f(int, long);
254 void resvsp_f(int, long);
255 void rmdir_f(int, long);
256 void setattr_f(int, long);
257 void setfattr_f(int, long);
258 void setxattr_f(int, long);
259 void snapshot_f(int, long);
260 void splice_f(int, long);
261 void stat_f(int, long);
262 void subvol_create_f(int, long);
263 void subvol_delete_f(int, long);
264 void symlink_f(int, long);
265 void sync_f(int, long);
266 void truncate_f(int, long);
267 void unlink_f(int, long);
268 void unresvsp_f(int, long);
269 void write_f(int, long);
270 void writev_f(int, long);
271 char *xattr_flag_to_string(int);
274 /* { OP_ENUM, "name", function, freq, iswrite }, */
275 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
276 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
277 { OP_AREAD, "aread", aread_f, 1, 0 },
278 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
279 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
280 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
281 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
282 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
283 { OP_CHOWN, "chown", chown_f, 3, 1 },
284 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
285 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
286 { OP_CREAT, "creat", creat_f, 4, 1 },
287 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
288 { OP_DREAD, "dread", dread_f, 4, 0 },
289 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
290 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
291 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
292 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
293 { OP_FREESP, "freesp", freesp_f, 1, 1 },
294 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
295 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
296 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
297 /* get extended attribute */
298 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
299 { OP_LINK, "link", link_f, 1, 1 },
300 /* list extent attributes */
301 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
302 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
303 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
304 { OP_MREAD, "mread", mread_f, 2, 0 },
305 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
306 { OP_PUNCH, "punch", punch_f, 1, 1 },
307 { OP_ZERO, "zero", zero_f, 1, 1 },
308 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
309 { OP_INSERT, "insert", insert_f, 1, 1 },
310 { OP_READ, "read", read_f, 1, 0 },
311 { OP_READLINK, "readlink", readlink_f, 1, 0 },
312 { OP_READV, "readv", readv_f, 1, 0 },
313 /* remove (delete) extended attribute */
314 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
315 { OP_RENAME, "rename", rename_f, 2, 1 },
316 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
317 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
318 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
319 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
320 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
321 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
322 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
323 /* set extended attribute */
324 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
325 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
326 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
327 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
328 { OP_SPLICE, "splice", splice_f, 1, 1 },
329 { OP_STAT, "stat", stat_f, 1, 0 },
330 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
331 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
332 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
333 { OP_SYNC, "sync", sync_f, 1, 1 },
334 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
335 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
336 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
337 { OP_WRITE, "write", write_f, 4, 1 },
338 { OP_WRITEV, "writev", writev_f, 4, 1 },
341 flist_t flist[FT_nft] = {
355 struct xfs_fsop_geom geom;
366 unsigned int idmodulo = XFS_IDMODULO_MAX;
367 unsigned int attr_mask = ~0;
370 unsigned long seed = 0;
374 int verifiable_log = 0;
375 sig_atomic_t should_stop = 0;
376 sigjmp_buf *sigbus_jmp = NULL;
377 char *execute_cmd = NULL;
378 int execute_freq = 1;
379 struct print_string flag_str = {0};
381 void add_to_flist(int, int, int, int);
382 void append_pathname(pathname_t *, char *);
383 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
384 int attr_remove_path(pathname_t *, const char *, int);
385 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
386 void check_cwd(void);
387 void cleanup_flist(void);
388 int creat_path(pathname_t *, mode_t);
389 void dcache_enter(int, int);
390 void dcache_init(void);
391 fent_t *dcache_lookup(int);
392 void dcache_purge(int, int);
393 void del_from_flist(int, int);
394 int dirid_to_name(char *, int);
396 int fent_to_name(pathname_t *, fent_t *);
397 bool fents_ancestor_check(fent_t *, fent_t *);
398 void fix_parent(int, int, bool);
399 void free_pathname(pathname_t *);
400 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
401 int generate_xattr_name(int, char *, int);
402 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
403 void init_pathname(pathname_t *);
404 int lchown_path(pathname_t *, uid_t, gid_t);
405 int link_path(pathname_t *, pathname_t *);
406 int lstat64_path(pathname_t *, struct stat64 *);
407 void make_freq_table(void);
408 int mkdir_path(pathname_t *, mode_t);
409 int mknod_path(pathname_t *, mode_t, dev_t);
410 void namerandpad(int, char *, int);
411 int open_file_or_dir(pathname_t *, int);
412 int open_path(pathname_t *, int);
413 DIR *opendir_path(pathname_t *);
414 void process_freq(char *);
415 int readlink_path(pathname_t *, char *, size_t);
416 int rename_path(pathname_t *, pathname_t *, int);
417 int rmdir_path(pathname_t *);
418 void separate_pathname(pathname_t *, char *, pathname_t *);
419 void show_ops(int, char *);
420 int stat64_path(pathname_t *, struct stat64 *);
421 int symlink_path(const char *, pathname_t *);
422 int truncate64_path(pathname_t *, off64_t);
423 int unlink_path(pathname_t *);
425 void write_freq(void);
426 void zero_freq(void);
427 void non_btrfs_freq(const char *);
429 void sg_handler(int signum)
437 * Only handle SIGBUS when mmap write to a hole and no
438 * block can be allocated due to ENOSPC, abort otherwise.
441 siglongjmp(*sigbus_jmp, -1);
443 printf("Unknown SIGBUS is caught, Abort!\n");
446 /* should not reach here */
453 int main(int argc, char **argv)
457 char *dirname = NULL;
458 char *logname = NULL;
459 char rpath[PATH_MAX];
468 xfs_error_injection_t err_inj;
469 struct sigaction action;
471 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
473 errrange = errtag = 0;
475 nops = sizeof(ops) / sizeof(ops[0]);
476 ops_end = &ops[nops];
478 while ((c = getopt(argc, argv, allopts)) != -1) {
487 sscanf(optarg, "%d", &errtag);
491 } else if (errtag == 0)
493 if (errtag >= XFS_ERRTAG_MAX) {
495 "error tag %d too large (max %d)\n",
496 errtag, XFS_ERRTAG_MAX - 1);
501 process_freq(optarg);
504 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
505 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
508 idmodulo = strtoul(optarg, NULL, 0);
509 if (idmodulo > XFS_IDMODULO_MAX) {
511 "chown modulo %d too big (max %d)\n",
512 idmodulo, XFS_IDMODULO_MAX);
517 loops = atoi(optarg);
520 operations = atoi(optarg);
527 nproc = atoi(optarg);
533 seed = strtoul(optarg, NULL, 0);
542 execute_cmd = optarg;
548 attr_mask = strtoul(optarg, NULL, 0);
552 if (optarg[0] == 'c')
563 execute_freq = strtoul(optarg, NULL, 0);
566 fprintf(stderr, "%s - invalid parameters\n",
576 /* no directory specified */
577 if (!nousage) usage();
581 non_btrfs_freq(dirname);
582 (void)mkdir(dirname, 0777);
583 if (logname && logname[0] != '/') {
584 if (!getcwd(rpath, sizeof(rpath))){
585 perror("getcwd failed");
591 if (chdir(dirname) < 0) {
596 char path[PATH_MAX + NAME_MAX + 1];
597 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
598 if (freopen(path, "a", stdout) == NULL) {
599 perror("freopen logfile failed");
603 sprintf(buf, "fss%x", (unsigned int)getpid());
604 fd = creat(buf, 0666);
605 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
606 maxfsize = (off64_t)MAXFSIZE32;
608 maxfsize = (off64_t)MAXFSIZE;
613 gettimeofday(&t, (void *)NULL);
614 seed = (int)t.tv_sec ^ (int)t.tv_usec;
615 printf("seed = %ld\n", seed);
617 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
618 if (i >= 0 && geom.rtblocks)
619 rtpct = MIN(MAX(geom.rtblocks * 100 /
620 (geom.rtblocks + geom.datablocks), 1), 99);
629 for (i = 0; i < j; i++)
632 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
638 for (i = 0; i < j; i++)
641 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
643 printf("Injecting failure on tag #%d\n", errtag);
644 err_inj.errtag = errtag;
646 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
648 perror("fsstress - XFS_SYSSGI error injection call");
657 action.sa_handler = sg_handler;
658 sigemptyset(&action.sa_mask);
660 if (sigaction(SIGTERM, &action, 0)) {
661 perror("sigaction failed");
665 for (i = 0; i < nproc; i++) {
667 sigemptyset(&action.sa_mask);
668 action.sa_handler = SIG_DFL;
669 if (sigaction(SIGTERM, &action, 0))
671 action.sa_handler = sg_handler;
672 if (sigaction(SIGBUS, &action, 0))
674 #ifdef HAVE_SYS_PRCTL_H
675 prctl(PR_SET_PDEATHSIG, SIGKILL);
676 if (getppid() == 1) /* parent died already? */
680 char path[PATH_MAX + NAME_MAX + 2 + 11];
681 snprintf(path, sizeof(path), "%s/%s.%d",
683 if (freopen(path, "a", stdout) == NULL) {
684 perror("freopen logfile failed");
690 if (io_setup(128, &io_ctx) != 0) {
691 fprintf(stderr, "io_setup failed");
695 for (i = 0; !loops || (i < loops); i++)
698 if(io_destroy(io_ctx) != 0) {
699 fprintf(stderr, "io_destroy failed");
709 while (wait(&stat) > 0 && !should_stop) {
712 action.sa_flags = SA_RESTART;
713 sigaction(SIGTERM, &action, 0);
714 kill(-getpid(), SIGTERM);
715 while (wait(&stat) > 0)
721 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
723 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
725 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
738 add_string(struct print_string *str, const char *add)
740 int len = strlen(add);
745 if (len > (str->max - 1) - str->len) {
746 str->len = str->max - 1;
750 memcpy(str->buffer + str->len, add, len);
752 str->buffer[str->len] = '\0';
758 translate_flags(int flags, const char *delim,
759 const struct print_flags *flag_array)
761 int i, mask, first = 1;
764 if (!flag_str.buffer) {
765 flag_str.buffer = malloc(4096);
769 if (!flag_str.buffer)
772 flag_str.buffer[0] = '\0';
774 for (i = 0; flag_array[i].name && flags; i++) {
775 mask = flag_array[i].mask;
776 if ((flags & mask) != mask)
779 add = flag_array[i].name;
782 add_string(&flag_str, delim);
785 add_string(&flag_str, add);
788 /* Check whether there are any leftover flags. */
794 add_string(&flag_str, delim);
796 ret = snprintf(number, 11, "0x%x", flags) > 0;
797 if (ret > 0 && ret <= 11)
798 add_string(&flag_str, number);
801 return flag_str.buffer;
805 add_to_flist(int ft, int id, int parent, int xattr_counter)
811 if (ftp->nfiles == ftp->nslots) {
812 ftp->nslots += FLIST_SLOT_INCR;
813 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
815 fep = &ftp->fents[ftp->nfiles++];
818 fep->parent = parent;
819 fep->xattr_counter = xattr_counter;
823 append_pathname(pathname_t *name, char *str)
829 /* attempting to append to a dir a zero length path */
830 if (len && *str == '/' && name->len == 0) {
831 fprintf(stderr, "fsstress: append_pathname failure\n");
832 assert(chdir(homedir) == 0);
837 name->path = realloc(name->path, name->len + 1 + len);
838 strcpy(&name->path[name->len], str);
843 attr_list_path(pathname_t *name,
845 const int buffersize,
847 attrlist_cursor_t *cursor)
849 char buf[NAME_MAX + 1];
853 if (flags != ATTR_DONTFOLLOW) {
858 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
859 if (rval >= 0 || errno != ENAMETOOLONG)
861 separate_pathname(name, buf, &newname);
862 if (chdir(buf) == 0) {
863 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
864 assert(chdir("..") == 0);
866 free_pathname(&newname);
871 attr_remove_path(pathname_t *name, const char *attrname, int flags)
873 char buf[NAME_MAX + 1];
877 rval = attr_remove(name->path, attrname, flags);
878 if (rval >= 0 || errno != ENAMETOOLONG)
880 separate_pathname(name, buf, &newname);
881 if (chdir(buf) == 0) {
882 rval = attr_remove_path(&newname, attrname, flags);
883 assert(chdir("..") == 0);
885 free_pathname(&newname);
890 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
891 const int valuelength, int flags)
893 char buf[NAME_MAX + 1];
897 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
898 if (rval >= 0 || errno != ENAMETOOLONG)
900 separate_pathname(name, buf, &newname);
901 if (chdir(buf) == 0) {
902 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
904 assert(chdir("..") == 0);
906 free_pathname(&newname);
914 struct stat64 statbuf;
916 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
918 assert(chdir(homedir) == 0);
919 fprintf(stderr, "fsstress: check_cwd failure\n");
926 * go thru flist and release all entries
928 * NOTE: this function shouldn't be called until the end of a process
936 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
945 creat_path(pathname_t *name, mode_t mode)
947 char buf[NAME_MAX + 1];
951 rval = creat(name->path, mode);
952 if (rval >= 0 || errno != ENAMETOOLONG)
954 separate_pathname(name, buf, &newname);
955 if (chdir(buf) == 0) {
956 rval = creat_path(&newname, mode);
957 assert(chdir("..") == 0);
959 free_pathname(&newname);
964 dcache_enter(int dirid, int slot)
966 dcache[dirid % NDCACHE] = slot;
974 for (i = 0; i < NDCACHE; i++)
979 dcache_lookup(int dirid)
984 i = dcache[dirid % NDCACHE];
985 if (i >= 0 && i < flist[FT_DIR].nfiles &&
986 (fep = &flist[FT_DIR].fents[i])->id == dirid)
988 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
989 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
995 dcache_purge(int dirid, int ft)
998 dcp = &dcache[dirid % NDCACHE];
999 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
1000 flist[ft].fents[*dcp].id == dirid)
1005 * Delete the item from the list by
1006 * moving last entry over the deleted one;
1007 * unless deleted entry is the last one.
1008 * Input: which file list array and which slot in array
1011 del_from_flist(int ft, int slot)
1016 if (ft == FT_DIR || ft == FT_SUBVOL)
1017 dcache_purge(ftp->fents[slot].id, ft);
1018 if (slot != ftp->nfiles - 1) {
1019 if (ft == FT_DIR || ft == FT_SUBVOL)
1020 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1021 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1027 delete_subvol_children(int parid)
1032 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1034 for (c = 0; c < flp->nfiles; c++) {
1035 if (flp->fents[c].parent == parid) {
1036 int id = flp->fents[c].id;
1037 del_from_flist(i, c);
1038 if (i == FT_DIR || i == FT_SUBVOL)
1039 delete_subvol_children(id);
1047 dirid_to_fent(int dirid)
1054 if ((fep = dcache_lookup(dirid)))
1058 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1059 if (fep->id == dirid) {
1060 dcache_enter(dirid, fep - flp->fents);
1074 struct stat64 statbuf;
1082 dividend = (operations + execute_freq) / (execute_freq + 1);
1083 sprintf(buf, "p%x", procid);
1084 (void)mkdir(buf, 0777);
1085 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1089 top_ino = statbuf.st_ino;
1090 homedir = getcwd(NULL, 0);
1092 perror("getcwd failed");
1098 namerand = random();
1099 for (opno = 0; opno < operations; opno++) {
1100 if (execute_cmd && opno && opno % dividend == 0) {
1102 printf("%d: execute command %s\n", opno,
1104 rval = system(execute_cmd);
1106 fprintf(stderr, "execute command failed with "
1109 p = &ops[freq_table[random() % freq_table_size]];
1110 p->func(opno, random());
1112 * test for forced shutdown by stat'ing the test
1113 * directory. If this stat returns EIO, assume
1114 * the forced shutdown happened.
1116 if (errtag != 0 && opno % 100 == 0) {
1117 rval = stat64(".", &statbuf);
1119 fprintf(stderr, "Detected EIO\n");
1125 assert(chdir("..") == 0);
1130 sprintf(cmd, "rm -rf %s", buf);
1133 perror("cleaning up");
1139 * build up a pathname going thru the file entry and all
1140 * its parent entries
1141 * Return 0 on error, 1 on success;
1144 fent_to_name(pathname_t *name, fent_t *fep)
1146 flist_t *flp = &flist[fep->ft];
1147 char buf[NAME_MAX + 1];
1155 /* build up parent directory name */
1156 if (fep->parent != -1) {
1157 pfep = dirid_to_fent(fep->parent);
1160 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1161 procid, fep->id, fep->parent);
1166 e = fent_to_name(name, pfep);
1169 append_pathname(name, "/");
1172 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1173 namerandpad(fep->id, buf, i);
1174 append_pathname(name, buf);
1179 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1183 for (tmpfep = fep; tmpfep->parent != -1;
1184 tmpfep = dirid_to_fent(tmpfep->parent)) {
1185 if (tmpfep->parent == dfep->id)
1193 fix_parent(int oldid, int newid, bool swap)
1200 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1201 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1202 if (fep->parent == oldid)
1203 fep->parent = newid;
1204 else if (swap && fep->parent == newid)
1205 fep->parent = oldid;
1211 free_pathname(pathname_t *name)
1221 * Generate a filename of type ft.
1222 * If we have a fep which should be a directory then use it
1223 * as the parent path for this new filename i.e. prepend with it.
1226 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1228 char buf[NAME_MAX + 1];
1237 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1238 namerandpad(id, buf, len);
1240 /* prepend fep parent dir-name to it */
1242 e = fent_to_name(name, fep);
1245 append_pathname(name, "/");
1247 append_pathname(name, buf);
1251 for (j = 0; !*v && j < ilistlen; j++) {
1252 if (ilist[j] == id) {
1260 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1264 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1274 * Input: "which" to choose the file-types eg. non-directory
1275 * Input: "r" to choose which file
1276 * Output: file-list, file-entry, name for the chosen file.
1277 * Output: verbose if chosen file is on the ilist.
1280 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1283 int totalsum = 0; /* total number of matching files */
1284 int partialsum = 0; /* partial sum of matching files */
1290 int e = 1; /* success */
1293 * go thru flist and add up number of files for each
1294 * category that matches with <which>.
1296 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1297 if (which & (1 << i))
1298 totalsum += flp->nfiles;
1300 if (totalsum == 0) {
1310 * Now we have possible matches between 0..totalsum-1.
1311 * And we use r to help us choose which one we want,
1312 * which when bounded by totalsum becomes x.
1314 x = (int)(r % totalsum);
1315 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1316 if (which & (1 << i)) {
1317 if (x < partialsum + flp->nfiles) {
1319 /* found the matching file entry */
1320 fep = &flp->fents[x - partialsum];
1322 /* fill-in what we were asked for */
1324 e = fent_to_name(name, fep);
1327 fprintf(stderr, "%d: failed to get path for entry:"
1328 " id=%d,parent=%d\n",
1329 procid, fep->id, fep->parent);
1338 /* turn on verbose if its an ilisted file */
1340 for (j = 0; !*v && j < ilistlen; j++) {
1341 if (ilist[j] == fep->id) {
1348 partialsum += flp->nfiles;
1352 fprintf(stderr, "fsstress: get_fname failure\n");
1359 init_pathname(pathname_t *name)
1366 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1368 char buf[NAME_MAX + 1];
1372 rval = lchown(name->path, owner, group);
1373 if (rval >= 0 || errno != ENAMETOOLONG)
1375 separate_pathname(name, buf, &newname);
1376 if (chdir(buf) == 0) {
1377 rval = lchown_path(&newname, owner, group);
1378 assert(chdir("..") == 0);
1380 free_pathname(&newname);
1385 link_path(pathname_t *name1, pathname_t *name2)
1387 char buf1[NAME_MAX + 1];
1388 char buf2[NAME_MAX + 1];
1390 pathname_t newname1;
1391 pathname_t newname2;
1394 rval = link(name1->path, name2->path);
1395 if (rval >= 0 || errno != ENAMETOOLONG)
1397 separate_pathname(name1, buf1, &newname1);
1398 separate_pathname(name2, buf2, &newname2);
1399 if (strcmp(buf1, buf2) == 0) {
1400 if (chdir(buf1) == 0) {
1401 rval = link_path(&newname1, &newname2);
1402 assert(chdir("..") == 0);
1405 if (strcmp(buf1, "..") == 0)
1407 else if (strcmp(buf2, "..") == 0)
1409 else if (strlen(buf1) == 0)
1411 else if (strlen(buf2) == 0)
1414 down1 = MAX(newname1.len, 3 + name2->len) <=
1415 MAX(3 + name1->len, newname2.len);
1417 free_pathname(&newname2);
1418 append_pathname(&newname2, "../");
1419 append_pathname(&newname2, name2->path);
1420 if (chdir(buf1) == 0) {
1421 rval = link_path(&newname1, &newname2);
1422 assert(chdir("..") == 0);
1425 free_pathname(&newname1);
1426 append_pathname(&newname1, "../");
1427 append_pathname(&newname1, name1->path);
1428 if (chdir(buf2) == 0) {
1429 rval = link_path(&newname1, &newname2);
1430 assert(chdir("..") == 0);
1434 free_pathname(&newname1);
1435 free_pathname(&newname2);
1440 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1442 char buf[NAME_MAX + 1];
1446 rval = lstat64(name->path, sbuf);
1447 if (rval >= 0 || errno != ENAMETOOLONG)
1449 separate_pathname(name, buf, &newname);
1450 if (chdir(buf) == 0) {
1451 rval = lstat64_path(&newname, sbuf);
1452 assert(chdir("..") == 0);
1454 free_pathname(&newname);
1459 make_freq_table(void)
1465 for (p = ops, f = 0; p < ops_end; p++)
1467 freq_table = malloc(f * sizeof(*freq_table));
1468 freq_table_size = f;
1469 for (p = ops, i = 0; p < ops_end; p++) {
1470 for (f = 0; f < p->freq; f++, i++)
1471 freq_table[i] = p->op;
1476 mkdir_path(pathname_t *name, mode_t mode)
1478 char buf[NAME_MAX + 1];
1482 rval = mkdir(name->path, mode);
1483 if (rval >= 0 || errno != ENAMETOOLONG)
1485 separate_pathname(name, buf, &newname);
1486 if (chdir(buf) == 0) {
1487 rval = mkdir_path(&newname, mode);
1488 assert(chdir("..") == 0);
1490 free_pathname(&newname);
1495 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1497 char buf[NAME_MAX + 1];
1501 rval = mknod(name->path, mode, dev);
1502 if (rval >= 0 || errno != ENAMETOOLONG)
1504 separate_pathname(name, buf, &newname);
1505 if (chdir(buf) == 0) {
1506 rval = mknod_path(&newname, mode, dev);
1507 assert(chdir("..") == 0);
1509 free_pathname(&newname);
1514 namerandpad(int id, char *buf, int i)
1517 static int buckets[] =
1518 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1524 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1525 padmod = buckets[bucket] + 1 - i;
1528 padlen = (id ^ namerand) % padmod;
1530 memset(&buf[i], 'X', padlen);
1531 buf[i + padlen] = '\0';
1536 open_file_or_dir(pathname_t *name, int flags)
1540 fd = open_path(name, flags);
1543 if (fd == -1 && errno != EISDIR)
1545 /* Directories can not be opened in write mode nor direct mode. */
1546 flags &= ~(O_WRONLY | O_DIRECT);
1547 flags |= O_RDONLY | O_DIRECTORY;
1548 return open_path(name, flags);
1552 open_path(pathname_t *name, int oflag)
1554 char buf[NAME_MAX + 1];
1558 rval = open(name->path, oflag);
1559 if (rval >= 0 || errno != ENAMETOOLONG)
1561 separate_pathname(name, buf, &newname);
1562 if (chdir(buf) == 0) {
1563 rval = open_path(&newname, oflag);
1564 assert(chdir("..") == 0);
1566 free_pathname(&newname);
1571 opendir_path(pathname_t *name)
1573 char buf[NAME_MAX + 1];
1577 rval = opendir(name->path);
1578 if (rval || errno != ENAMETOOLONG)
1580 separate_pathname(name, buf, &newname);
1581 if (chdir(buf) == 0) {
1582 rval = opendir_path(&newname);
1583 assert(chdir("..") == 0);
1585 free_pathname(&newname);
1590 process_freq(char *arg)
1595 s = strchr(arg, '=');
1597 fprintf(stderr, "bad argument '%s'\n", arg);
1601 for (p = ops; p < ops_end; p++) {
1602 if (strcmp(arg, p->name) == 0) {
1607 fprintf(stderr, "can't find op type %s for -f\n", arg);
1612 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1614 char buf[NAME_MAX + 1];
1618 rval = readlink(name->path, lbuf, lbufsiz);
1619 if (rval >= 0 || errno != ENAMETOOLONG)
1621 separate_pathname(name, buf, &newname);
1622 if (chdir(buf) == 0) {
1623 rval = readlink_path(&newname, lbuf, lbufsiz);
1624 assert(chdir("..") == 0);
1626 free_pathname(&newname);
1631 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1633 char buf1[NAME_MAX + 1];
1634 char buf2[NAME_MAX + 1];
1636 pathname_t newname1;
1637 pathname_t newname2;
1641 rval = rename(name1->path, name2->path);
1643 rval = renameat2(AT_FDCWD, name1->path,
1644 AT_FDCWD, name2->path, mode);
1645 if (rval >= 0 || errno != ENAMETOOLONG)
1647 separate_pathname(name1, buf1, &newname1);
1648 separate_pathname(name2, buf2, &newname2);
1649 if (strcmp(buf1, buf2) == 0) {
1650 if (chdir(buf1) == 0) {
1651 rval = rename_path(&newname1, &newname2, mode);
1652 assert(chdir("..") == 0);
1655 if (strcmp(buf1, "..") == 0)
1657 else if (strcmp(buf2, "..") == 0)
1659 else if (strlen(buf1) == 0)
1661 else if (strlen(buf2) == 0)
1664 down1 = MAX(newname1.len, 3 + name2->len) <=
1665 MAX(3 + name1->len, newname2.len);
1667 free_pathname(&newname2);
1668 append_pathname(&newname2, "../");
1669 append_pathname(&newname2, name2->path);
1670 if (chdir(buf1) == 0) {
1671 rval = rename_path(&newname1, &newname2, mode);
1672 assert(chdir("..") == 0);
1675 free_pathname(&newname1);
1676 append_pathname(&newname1, "../");
1677 append_pathname(&newname1, name1->path);
1678 if (chdir(buf2) == 0) {
1679 rval = rename_path(&newname1, &newname2, mode);
1680 assert(chdir("..") == 0);
1684 free_pathname(&newname1);
1685 free_pathname(&newname2);
1690 rmdir_path(pathname_t *name)
1692 char buf[NAME_MAX + 1];
1696 rval = rmdir(name->path);
1697 if (rval >= 0 || errno != ENAMETOOLONG)
1699 separate_pathname(name, buf, &newname);
1700 if (chdir(buf) == 0) {
1701 rval = rmdir_path(&newname);
1702 assert(chdir("..") == 0);
1704 free_pathname(&newname);
1709 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1713 init_pathname(newname);
1714 slash = strchr(name->path, '/');
1715 if (slash == NULL) {
1720 strcpy(buf, name->path);
1722 append_pathname(newname, slash + 1);
1728 show_ops(int flag, char *lead_str)
1733 /* print in list form */
1736 for (p = ops; p < ops_end; p++) {
1737 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1738 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1739 x+=printf("%s ", p->name);
1742 } else if (flag == 0) {
1743 /* Table view style */
1745 for (f = 0, p = ops; p < ops_end; p++)
1751 for (p = ops; p < ops_end; p++) {
1752 if (flag != 0 || p->freq > 0) {
1753 if (lead_str != NULL)
1754 printf("%s", lead_str);
1755 printf("%20s %d/%d %s\n",
1756 p->name, p->freq, f,
1757 (p->iswrite == 0) ? " " : "write op");
1761 /* Command line style */
1762 if (lead_str != NULL)
1763 printf("%s", lead_str);
1764 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1766 for (p = ops; p < ops_end; p++)
1768 printf("-f %s=%d \\\n",p->name, p->freq);
1773 stat64_path(pathname_t *name, struct stat64 *sbuf)
1775 char buf[NAME_MAX + 1];
1779 rval = stat64(name->path, sbuf);
1780 if (rval >= 0 || errno != ENAMETOOLONG)
1782 separate_pathname(name, buf, &newname);
1783 if (chdir(buf) == 0) {
1784 rval = stat64_path(&newname, sbuf);
1785 assert(chdir("..") == 0);
1787 free_pathname(&newname);
1792 symlink_path(const char *name1, pathname_t *name)
1794 char buf[NAME_MAX + 1];
1798 if (!strcmp(name1, name->path)) {
1799 printf("yikes! %s %s\n", name1, name->path);
1803 rval = symlink(name1, name->path);
1804 if (rval >= 0 || errno != ENAMETOOLONG)
1806 separate_pathname(name, buf, &newname);
1807 if (chdir(buf) == 0) {
1808 rval = symlink_path(name1, &newname);
1809 assert(chdir("..") == 0);
1811 free_pathname(&newname);
1816 truncate64_path(pathname_t *name, off64_t length)
1818 char buf[NAME_MAX + 1];
1822 rval = truncate64(name->path, length);
1823 if (rval >= 0 || errno != ENAMETOOLONG)
1825 separate_pathname(name, buf, &newname);
1826 if (chdir(buf) == 0) {
1827 rval = truncate64_path(&newname, length);
1828 assert(chdir("..") == 0);
1830 free_pathname(&newname);
1835 unlink_path(pathname_t *name)
1837 char buf[NAME_MAX + 1];
1841 rval = unlink(name->path);
1842 if (rval >= 0 || errno != ENAMETOOLONG)
1844 separate_pathname(name, buf, &newname);
1845 if (chdir(buf) == 0) {
1846 rval = unlink_path(&newname);
1847 assert(chdir("..") == 0);
1849 free_pathname(&newname);
1856 printf("Usage: %s -H or\n", myprog);
1857 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1859 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1861 printf(" -c clean up the test directory after each run\n");
1862 printf(" -d dir specifies the base directory for operations\n");
1863 printf(" -e errtg specifies error injection stuff\n");
1864 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1865 printf(" the valid operation names are:\n");
1867 printf(" -i filenum get verbose output for this nth file object\n");
1868 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1869 printf(" *use 0 for infinite (default 1)\n");
1870 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1871 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1872 printf(" -o logfile specifies logfile name\n");
1873 printf(" -p nproc specifies the no. of processes (default 1)\n");
1874 printf(" -r specifies random name padding\n");
1875 printf(" -s seed specifies the seed for the random generator (default random)\n");
1876 printf(" -v specifies verbose mode\n");
1877 printf(" -w zeros frequencies of non-write operations\n");
1878 printf(" -x cmd execute command in the middle of operations\n");
1879 printf(" -z zeros frequencies of all operations\n");
1880 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1881 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1882 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1883 printf(" -H prints usage and exits\n");
1891 for (p = ops; p < ops_end; p++) {
1902 for (p = ops; p < ops_end; p++)
1906 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1908 opty_t btrfs_ops[] = {
1915 non_btrfs_freq(const char *path)
1918 #ifdef HAVE_BTRFSUTIL_H
1919 enum btrfs_util_error e;
1921 e = btrfs_util_is_subvolume(path);
1922 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1925 for (p = ops; p < ops_end; p++) {
1927 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1928 if (p->op == btrfs_ops[i]) {
1936 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1939 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1940 verifiable_log ? -1: (long)s->st_ino,
1941 (long)s->st_nlink, s->st_uid, s->st_gid,
1942 (long long) s->st_blocks, (long long) s->st_size);
1946 afsync_f(int opno, long r)
1954 struct iocb *iocbs[] = { &iocb };
1955 struct io_event event;
1958 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1960 printf("%d/%d: afsync - no filename\n", procid, opno);
1964 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1965 e = fd < 0 ? errno : 0;
1969 printf("%d/%d: afsync - open %s failed %d\n",
1970 procid, opno, f.path, e);
1975 io_prep_fsync(&iocb, fd);
1976 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1978 printf("%d/%d: afsync - io_submit %s %d\n",
1979 procid, opno, f.path, e);
1984 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1986 printf("%d/%d: afsync - io_getevents failed %d\n",
1995 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2002 allocsp_f(int opno, long r)
2007 struct xfs_flock64 fl;
2015 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2017 printf("%d/%d: allocsp - no filename\n", procid, opno);
2021 fd = open_path(&f, O_RDWR);
2022 e = fd < 0 ? errno : 0;
2026 printf("%d/%d: allocsp - open %s failed %d\n",
2027 procid, opno, f.path, e);
2031 if (fstat64(fd, &stb) < 0) {
2033 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2034 procid, opno, f.path, errno);
2039 inode_info(st, sizeof(st), &stb, v);
2040 lr = ((int64_t)random() << 32) + random();
2041 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2043 fl.l_whence = SEEK_SET;
2046 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2048 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2049 procid, opno, f.path, st, (long long)off, e);
2057 do_aio_rw(int opno, long r, int flags)
2061 struct dioattr diob;
2073 struct io_event event;
2074 struct iocb *iocbs[] = { &iocb };
2075 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2078 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2080 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2084 fd = open_path(&f, flags|O_DIRECT);
2085 e = fd < 0 ? errno : 0;
2089 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2090 procid, opno, f.path, e);
2094 if (fstat64(fd, &stb) < 0) {
2096 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2097 procid, opno, f.path, errno);
2102 inode_info(st, sizeof(st), &stb, v);
2103 if (!iswrite && stb.st_size == 0) {
2105 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2111 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2114 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2115 " fallback to stat()\n",
2116 procid, opno, f.path, st, errno);
2117 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2118 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2120 dio_env = getenv("XFS_DIO_MIN");
2122 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2123 align = (int64_t)diob.d_miniosz;
2124 lr = ((int64_t)random() << 32) + random();
2125 len = (random() % FILELEN_MAX) + 1;
2126 len -= (len % align);
2129 else if (len > diob.d_maxiosz)
2130 len = diob.d_maxiosz;
2131 buf = memalign(diob.d_mem, len);
2134 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2135 off -= (off % align);
2137 memset(buf, nameseq & 0xff, len);
2138 io_prep_pwrite(&iocb, fd, buf, len, off);
2140 off = (off64_t)(lr % stb.st_size);
2141 off -= (off % align);
2142 io_prep_pread(&iocb, fd, buf, len, off);
2144 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2146 printf("%d/%d: %s - io_submit failed %d\n",
2147 procid, opno, iswrite ? "awrite" : "aread", e);
2152 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2154 printf("%d/%d: %s - io_getevents failed %d\n",
2155 procid, opno, iswrite ? "awrite" : "aread", e);
2161 e = event.res != len ? event.res2 : 0;
2164 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2165 procid, opno, iswrite ? "awrite" : "aread",
2166 f.path, st, (long long)off, (int)len, e);
2173 aread_f(int opno, long r)
2176 do_aio_rw(opno, r, O_RDONLY);
2181 attr_remove_f(int opno, long r)
2183 attrlist_ent_t *aep;
2187 attrlist_cursor_t cursor;
2196 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2197 append_pathname(&f, ".");
2199 bzero(&cursor, sizeof(cursor));
2201 bzero(buf, sizeof(buf));
2202 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2206 alist = (attrlist_t *)buf;
2207 total += alist->al_count;
2208 } while (alist->al_more);
2211 printf("%d/%d: attr_remove - no attrs for %s\n",
2212 procid, opno, f.path);
2216 which = (int)(random() % total);
2217 bzero(&cursor, sizeof(cursor));
2221 bzero(buf, sizeof(buf));
2222 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2226 alist = (attrlist_t *)buf;
2227 if (which < ent + alist->al_count) {
2228 aep = (attrlist_ent_t *)
2229 &buf[alist->al_offset[which - ent]];
2230 aname = aep->a_name;
2233 ent += alist->al_count;
2234 } while (alist->al_more);
2235 if (aname == NULL) {
2238 "%d/%d: attr_remove - name %d not found at %s\n",
2239 procid, opno, which, f.path);
2243 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2246 printf("%d/%d: attr_remove %s %s %d\n",
2247 procid, opno, f.path, aname, e);
2252 attr_set_f(int opno, long r)
2259 static int lengths[] = { 10, 100, 1000, 10000 };
2264 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2265 append_pathname(&f, ".");
2266 sprintf(aname, "a%x", nameseq++);
2267 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2268 len = (int)(random() % lengths[li]);
2272 memset(aval, nameseq & 0xff, len);
2273 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2278 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2284 awrite_f(int opno, long r)
2287 do_aio_rw(opno, r, O_WRONLY);
2292 bulkstat_f(int opno, long r)
2298 struct xfs_bstat *t;
2300 struct xfs_fsop_bulkreq bsr;
2303 nent = (r % 999) + 2;
2304 t = malloc(nent * sizeof(*t));
2305 fd = open(".", O_RDONLY);
2313 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2317 printf("%d/%d: bulkstat nent %d total %lld\n",
2318 procid, opno, nent, (long long)total);
2323 bulkstat1_f(int opno, long r)
2333 struct xfs_fsop_bulkreq bsr;
2336 good = random() & 1;
2338 /* use an inode we know exists */
2340 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2341 append_pathname(&f, ".");
2342 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2347 * pick a random inode
2349 * note this can generate kernel warning messages
2350 * since bulkstat_one will read the disk block that
2351 * would contain a given inode even if that disk
2352 * block doesn't contain inodes.
2354 * this is detected later, but not until after the
2355 * warning is displayed.
2357 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2363 fd = open(".", O_RDONLY);
2369 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2371 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2372 procid, opno, good?"real":"random",
2373 verifiable_log ? -1LL : (long long)ino, e);
2378 chown_f(int opno, long r)
2388 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2389 append_pathname(&f, ".");
2390 u = (uid_t)random();
2391 g = (gid_t)random();
2392 nbits = (int)(random() % idmodulo);
2393 u &= (1 << nbits) - 1;
2394 g &= (1 << nbits) - 1;
2395 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2398 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2402 /* reflink some arbitrary range of f1 to f2. */
2409 struct file_clone_range fcr;
2410 struct pathname fpath1;
2411 struct pathname fpath2;
2412 struct stat64 stat1;
2413 struct stat64 stat2;
2414 char inoinfo1[1024];
2415 char inoinfo2[1024];
2429 init_pathname(&fpath1);
2430 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2432 printf("%d/%d: clonerange read - no filename\n",
2437 init_pathname(&fpath2);
2438 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2440 printf("%d/%d: clonerange write - no filename\n",
2446 fd1 = open_path(&fpath1, O_RDONLY);
2447 e = fd1 < 0 ? errno : 0;
2451 printf("%d/%d: clonerange read - open %s failed %d\n",
2452 procid, opno, fpath1.path, e);
2456 fd2 = open_path(&fpath2, O_WRONLY);
2457 e = fd2 < 0 ? errno : 0;
2461 printf("%d/%d: clonerange write - open %s failed %d\n",
2462 procid, opno, fpath2.path, e);
2466 /* Get file stats */
2467 if (fstat64(fd1, &stat1) < 0) {
2469 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2470 procid, opno, fpath1.path, errno);
2473 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2475 if (fstat64(fd2, &stat2) < 0) {
2477 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2478 procid, opno, fpath2.path, errno);
2481 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2483 /* Calculate offsets */
2484 len = (random() % FILELEN_MAX) + 1;
2485 len &= ~(stat1.st_blksize - 1);
2487 len = stat1.st_blksize;
2488 if (len > stat1.st_size)
2489 len = stat1.st_size;
2491 lr = ((int64_t)random() << 32) + random();
2492 if (stat1.st_size == len)
2495 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2497 off1 &= ~(stat1.st_blksize - 1);
2500 * If srcfile == destfile, randomly generate destination ranges
2501 * until we find one that doesn't overlap the source range.
2503 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2505 lr = ((int64_t)random() << 32) + random();
2506 off2 = (off64_t)(lr % max_off2);
2508 off2 &= ~(stat2.st_blksize - 1);
2509 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2511 /* Clone data blocks */
2513 fcr.src_offset = off1;
2514 fcr.src_length = len;
2515 fcr.dest_offset = off2;
2517 ret = ioctl(fd2, FICLONERANGE, &fcr);
2518 e = ret < 0 ? errno : 0;
2520 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2522 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2523 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2526 printf(" error %d", e);
2535 free_pathname(&fpath2);
2537 free_pathname(&fpath1);
2541 /* copy some arbitrary range of f1 to f2. */
2547 #ifdef HAVE_COPY_FILE_RANGE
2548 struct pathname fpath1;
2549 struct pathname fpath2;
2550 struct stat64 stat1;
2551 struct stat64 stat2;
2552 char inoinfo1[1024];
2553 char inoinfo2[1024];
2571 init_pathname(&fpath1);
2572 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2574 printf("%d/%d: copyrange read - no filename\n",
2579 init_pathname(&fpath2);
2580 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2582 printf("%d/%d: copyrange write - no filename\n",
2588 fd1 = open_path(&fpath1, O_RDONLY);
2589 e = fd1 < 0 ? errno : 0;
2593 printf("%d/%d: copyrange read - open %s failed %d\n",
2594 procid, opno, fpath1.path, e);
2598 fd2 = open_path(&fpath2, O_WRONLY);
2599 e = fd2 < 0 ? errno : 0;
2603 printf("%d/%d: copyrange write - open %s failed %d\n",
2604 procid, opno, fpath2.path, e);
2608 /* Get file stats */
2609 if (fstat64(fd1, &stat1) < 0) {
2611 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2612 procid, opno, fpath1.path, errno);
2615 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2617 if (fstat64(fd2, &stat2) < 0) {
2619 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2620 procid, opno, fpath2.path, errno);
2623 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2625 /* Calculate offsets */
2626 len = (random() % FILELEN_MAX) + 1;
2628 len = stat1.st_blksize;
2629 if (len > stat1.st_size)
2630 len = stat1.st_size;
2632 lr = ((int64_t)random() << 32) + random();
2633 if (stat1.st_size == len)
2636 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2640 * If srcfile == destfile, randomly generate destination ranges
2641 * until we find one that doesn't overlap the source range.
2643 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2645 lr = ((int64_t)random() << 32) + random();
2646 off2 = (off64_t)(lr % max_off2);
2648 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2651 * Since len, off1 and off2 will be changed later, preserve their
2659 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2662 if (errno != EAGAIN || tries++ >= 300)
2664 } else if (ret > len || ret == 0)
2669 e = ret < 0 ? errno : 0;
2671 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2673 fpath1.path, inoinfo1,
2674 (long long)offset1, (long long)length,
2675 fpath2.path, inoinfo2,
2676 (long long)offset2, (long long)length);
2679 printf(" error %d", e);
2680 else if (len && ret > len)
2681 printf(" asked for %lld, copied %lld??\n",
2682 (long long)len, (long long)ret);
2691 free_pathname(&fpath2);
2693 free_pathname(&fpath1);
2697 /* dedupe some arbitrary range of f1 to f2...fn. */
2703 #ifdef FIDEDUPERANGE
2704 #define INFO_SZ 1024
2705 struct file_dedupe_range *fdr;
2706 struct pathname *fpath;
2707 struct stat64 *stat;
2719 if (flist[FT_REG].nfiles < 2)
2722 /* Pick somewhere between 2 and 128 files. */
2724 nr = random() % (flist[FT_REG].nfiles + 1);
2725 } while (nr < 2 || nr > 128);
2728 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2729 sizeof(struct file_dedupe_range));
2731 printf("%d/%d: line %d error %d\n",
2732 procid, opno, __LINE__, errno);
2735 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2736 sizeof(struct file_dedupe_range)));
2738 fpath = calloc(nr, sizeof(struct pathname));
2740 printf("%d/%d: line %d error %d\n",
2741 procid, opno, __LINE__, errno);
2745 stat = calloc(nr, sizeof(struct stat64));
2747 printf("%d/%d: line %d error %d\n",
2748 procid, opno, __LINE__, errno);
2752 info = calloc(nr, INFO_SZ);
2754 printf("%d/%d: line %d error %d\n",
2755 procid, opno, __LINE__, errno);
2759 off = calloc(nr, sizeof(off64_t));
2761 printf("%d/%d: line %d error %d\n",
2762 procid, opno, __LINE__, errno);
2766 v = calloc(nr, sizeof(int));
2768 printf("%d/%d: line %d error %d\n",
2769 procid, opno, __LINE__, errno);
2772 fd = calloc(nr, sizeof(int));
2774 printf("%d/%d: line %d error %d\n",
2775 procid, opno, __LINE__, errno);
2778 memset(fd, 0xFF, nr * sizeof(int));
2780 /* Get paths for all files */
2781 for (i = 0; i < nr; i++)
2782 init_pathname(&fpath[i]);
2784 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2786 printf("%d/%d: deduperange read - no filename\n",
2791 for (i = 1; i < nr; i++) {
2792 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2794 printf("%d/%d: deduperange write - no filename\n",
2801 fd[0] = open_path(&fpath[0], O_RDONLY);
2802 e = fd[0] < 0 ? errno : 0;
2806 printf("%d/%d: deduperange read - open %s failed %d\n",
2807 procid, opno, fpath[0].path, e);
2811 for (i = 1; i < nr; i++) {
2812 fd[i] = open_path(&fpath[i], O_WRONLY);
2813 e = fd[i] < 0 ? errno : 0;
2817 printf("%d/%d: deduperange write - open %s failed %d\n",
2818 procid, opno, fpath[i].path, e);
2823 /* Get file stats */
2824 if (fstat64(fd[0], &stat[0]) < 0) {
2826 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2827 procid, opno, fpath[0].path, errno);
2831 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2833 for (i = 1; i < nr; i++) {
2834 if (fstat64(fd[i], &stat[i]) < 0) {
2836 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2837 procid, opno, fpath[i].path, errno);
2840 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2843 /* Never try to dedupe more than half of the src file. */
2844 len = (random() % FILELEN_MAX) + 1;
2845 len &= ~(stat[0].st_blksize - 1);
2847 len = stat[0].st_blksize / 2;
2848 if (len > stat[0].st_size / 2)
2849 len = stat[0].st_size / 2;
2851 /* Calculate offsets */
2852 lr = ((int64_t)random() << 32) + random();
2853 if (stat[0].st_size == len)
2856 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2858 off[0] &= ~(stat[0].st_blksize - 1);
2861 * If srcfile == destfile[i], randomly generate destination ranges
2862 * until we find one that doesn't overlap the source range.
2864 for (i = 1; i < nr; i++) {
2868 lr = ((int64_t)random() << 32) + random();
2869 if (stat[i].st_size <= len)
2872 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2874 off[i] &= ~(stat[i].st_blksize - 1);
2875 } while (stat[0].st_ino == stat[i].st_ino &&
2876 llabs(off[i] - off[0]) < len &&
2880 /* Clone data blocks */
2881 fdr->src_offset = off[0];
2882 fdr->src_length = len;
2883 fdr->dest_count = nr - 1;
2884 for (i = 1; i < nr; i++) {
2885 fdr->info[i - 1].dest_fd = fd[i];
2886 fdr->info[i - 1].dest_offset = off[i];
2889 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2890 e = ret < 0 ? errno : 0;
2892 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2894 fpath[0].path, &info[0], (long long)off[0],
2897 printf(" error %d", e);
2903 for (i = 1; i < nr; i++) {
2904 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2906 printf("%d/%d: ...to %s%s [%lld,%lld]",
2908 fpath[i].path, &info[i * INFO_SZ],
2909 (long long)off[i], (long long)len);
2910 if (fdr->info[i - 1].status < 0)
2911 printf(" error %d", e);
2912 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2913 printf(" %llu bytes deduplicated",
2914 fdr->info[i - 1].bytes_deduped);
2915 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2916 printf(" differed");
2922 for (i = 0; i < nr; i++)
2926 for (i = 0; i < nr; i++)
2927 free_pathname(&fpath[i]);
2946 setxattr_f(int opno, long r)
2948 #ifdef XFS_XFLAG_EXTSIZE
2958 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2959 append_pathname(&f, ".");
2960 fd = open_path(&f, O_RDWR);
2961 e = fd < 0 ? errno : 0;
2966 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2967 nbits = (int)(random() % e);
2968 p &= (1 << nbits) - 1;
2970 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2972 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2975 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2982 splice_f(int opno, long r)
2984 struct pathname fpath1;
2985 struct pathname fpath2;
2986 struct stat64 stat1;
2987 struct stat64 stat2;
2988 char inoinfo1[1024];
2989 char inoinfo2[1024];
2993 loff_t offset1, offset2;
3000 ssize_t ret1 = 0, ret2 = 0;
3006 init_pathname(&fpath1);
3007 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3009 printf("%d/%d: splice read - no filename\n",
3014 init_pathname(&fpath2);
3015 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3017 printf("%d/%d: splice write - no filename\n",
3023 fd1 = open_path(&fpath1, O_RDONLY);
3024 e = fd1 < 0 ? errno : 0;
3028 printf("%d/%d: splice read - open %s failed %d\n",
3029 procid, opno, fpath1.path, e);
3033 fd2 = open_path(&fpath2, O_WRONLY);
3034 e = fd2 < 0 ? errno : 0;
3038 printf("%d/%d: splice write - open %s failed %d\n",
3039 procid, opno, fpath2.path, e);
3043 /* Get file stats */
3044 if (fstat64(fd1, &stat1) < 0) {
3046 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3047 procid, opno, fpath1.path, errno);
3050 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3052 if (fstat64(fd2, &stat2) < 0) {
3054 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3055 procid, opno, fpath2.path, errno);
3058 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3060 /* Calculate offsets */
3061 len = (random() % FILELEN_MAX) + 1;
3063 len = stat1.st_blksize;
3064 if (len > stat1.st_size)
3065 len = stat1.st_size;
3067 lr = ((int64_t)random() << 32) + random();
3068 if (stat1.st_size == len)
3071 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3075 * splice can overlap write, so the offset of the target file can be
3076 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3077 * past the current dest file EOF
3079 lr = ((int64_t)random() << 32) + random();
3080 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3083 * Since len, off1 and off2 will be changed later, preserve their
3090 /* Pipe initialize */
3091 if (pipe(filedes) < 0) {
3093 printf("%d/%d: splice - pipe failed %d\n",
3094 procid, opno, errno);
3102 /* move to pipe buffer */
3103 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3109 /* move from pipe buffer to dst file */
3111 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3124 if (ret1 < 0 || ret2 < 0)
3129 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3131 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3132 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3134 if (length && length > total)
3135 printf(" asked for %lld, spliced %lld??\n",
3136 (long long)length, (long long)total);
3147 free_pathname(&fpath2);
3149 free_pathname(&fpath1);
3153 creat_f(int opno, long r)
3168 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
3173 e1 = (random() % 100);
3174 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3176 if (type == FT_RTF) /* rt always gets an extsize */
3177 extsize = (random() % 10) + 1;
3178 else if (e1 < 10) /* one-in-ten get an extsize */
3179 extsize = random() % 1024;
3183 e = generate_fname(fep, type, &f, &id, &v);
3187 (void)fent_to_name(&f, fep);
3188 printf("%d/%d: creat - no filename from %s\n",
3189 procid, opno, f.path);
3194 fd = creat_path(&f, 0666);
3195 e = fd < 0 ? errno : 0;
3200 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3201 if (type == FT_RTF) {
3202 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3203 a.fsx_extsize = extsize *
3204 geom.rtextsize * geom.blocksize;
3206 } else if (extsize) {
3207 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3208 a.fsx_extsize = extsize * geom.blocksize;
3211 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3214 add_to_flist(type, id, parid, 0);
3218 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3219 extsize ? a.fsx_extsize : 0, e, e1);
3220 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3226 dread_f(int opno, long r)
3230 struct dioattr diob;
3243 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3245 printf("%d/%d: dread - no filename\n", procid, opno);
3249 fd = open_path(&f, O_RDONLY|O_DIRECT);
3250 e = fd < 0 ? errno : 0;
3254 printf("%d/%d: dread - open %s failed %d\n",
3255 procid, opno, f.path, e);
3259 if (fstat64(fd, &stb) < 0) {
3261 printf("%d/%d: dread - fstat64 %s failed %d\n",
3262 procid, opno, f.path, errno);
3267 inode_info(st, sizeof(st), &stb, v);
3268 if (stb.st_size == 0) {
3270 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3276 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3279 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3280 " fallback to stat()\n",
3281 procid, opno, f.path, st, errno);
3282 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3283 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3286 dio_env = getenv("XFS_DIO_MIN");
3288 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3290 align = (int64_t)diob.d_miniosz;
3291 lr = ((int64_t)random() << 32) + random();
3292 off = (off64_t)(lr % stb.st_size);
3293 off -= (off % align);
3294 lseek64(fd, off, SEEK_SET);
3295 len = (random() % FILELEN_MAX) + 1;
3296 len -= (len % align);
3299 else if (len > diob.d_maxiosz)
3300 len = diob.d_maxiosz;
3301 buf = memalign(diob.d_mem, len);
3302 e = read(fd, buf, len) < 0 ? errno : 0;
3305 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3306 procid, opno, f.path, st, (long long)off, (int)len, e);
3312 dwrite_f(int opno, long r)
3316 struct dioattr diob;
3329 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3331 printf("%d/%d: dwrite - no filename\n", procid, opno);
3335 fd = open_path(&f, O_WRONLY|O_DIRECT);
3336 e = fd < 0 ? errno : 0;
3340 printf("%d/%d: dwrite - open %s failed %d\n",
3341 procid, opno, f.path, e);
3345 if (fstat64(fd, &stb) < 0) {
3347 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3348 procid, opno, f.path, errno);
3353 inode_info(st, sizeof(st), &stb, v);
3354 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3356 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3357 " %s%s return %d, fallback to stat()\n",
3358 procid, opno, f.path, st, errno);
3359 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3360 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3363 dio_env = getenv("XFS_DIO_MIN");
3365 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3367 align = (int64_t)diob.d_miniosz;
3368 lr = ((int64_t)random() << 32) + random();
3369 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3370 off -= (off % align);
3371 lseek64(fd, off, SEEK_SET);
3372 len = (random() % FILELEN_MAX) + 1;
3373 len -= (len % align);
3376 else if (len > diob.d_maxiosz)
3377 len = diob.d_maxiosz;
3378 buf = memalign(diob.d_mem, len);
3380 lseek64(fd, off, SEEK_SET);
3381 memset(buf, nameseq & 0xff, len);
3382 e = write(fd, buf, len) < 0 ? errno : 0;
3385 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3386 procid, opno, f.path, st, (long long)off, (int)len, e);
3392 #ifdef HAVE_LINUX_FALLOC_H
3393 struct print_flags falloc_flags [] = {
3394 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3395 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3396 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3397 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3398 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3399 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3403 #define translate_falloc_flags(mode) \
3404 ({translate_flags(mode, "|", falloc_flags);})
3408 do_fallocate(int opno, long r, int mode)
3410 #ifdef HAVE_LINUX_FALLOC_H
3422 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3424 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3428 fd = open_path(&f, O_RDWR);
3431 printf("%d/%d: do_fallocate - open %s failed %d\n",
3432 procid, opno, f.path, errno);
3437 if (fstat64(fd, &stb) < 0) {
3439 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3440 procid, opno, f.path, errno);
3445 inode_info(st, sizeof(st), &stb, v);
3446 lr = ((int64_t)random() << 32) + random();
3447 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3449 len = (off64_t)(random() % (1024 * 1024));
3451 * Collapse/insert range requires off and len to be block aligned,
3452 * make it more likely to be the case.
3454 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3456 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3457 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3459 mode |= FALLOC_FL_KEEP_SIZE & random();
3460 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3462 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3463 procid, opno, translate_falloc_flags(mode),
3464 f.path, st, (long long)off, (long long)len, e);
3471 fallocate_f(int opno, long r)
3473 #ifdef HAVE_LINUX_FALLOC_H
3474 do_fallocate(opno, r, 0);
3479 fdatasync_f(int opno, long r)
3487 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3489 printf("%d/%d: fdatasync - no filename\n",
3494 fd = open_path(&f, O_WRONLY);
3495 e = fd < 0 ? errno : 0;
3499 printf("%d/%d: fdatasync - open %s failed %d\n",
3500 procid, opno, f.path, e);
3504 e = fdatasync(fd) < 0 ? errno : 0;
3506 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3511 #ifdef HAVE_LINUX_FIEMAP_H
3512 struct print_flags fiemap_flags[] = {
3513 { FIEMAP_FLAG_SYNC, "SYNC"},
3514 { FIEMAP_FLAG_XATTR, "XATTR"},
3518 #define translate_fiemap_flags(mode) \
3519 ({translate_flags(mode, "|", fiemap_flags);})
3523 fiemap_f(int opno, long r)
3525 #ifdef HAVE_LINUX_FIEMAP_H
3535 struct fiemap *fiemap;
3538 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3540 printf("%d/%d: fiemap - no filename\n", procid, opno);
3544 fd = open_path(&f, O_RDWR);
3545 e = fd < 0 ? errno : 0;
3549 printf("%d/%d: fiemap - open %s failed %d\n",
3550 procid, opno, f.path, e);
3554 if (fstat64(fd, &stb) < 0) {
3556 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3557 procid, opno, f.path, errno);
3562 inode_info(st, sizeof(st), &stb, v);
3563 blocks_to_map = random() & 0xffff;
3564 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3565 (blocks_to_map * sizeof(struct fiemap_extent)));
3568 printf("%d/%d: malloc failed \n", procid, opno);
3573 lr = ((int64_t)random() << 32) + random();
3574 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3576 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3577 fiemap->fm_extent_count = blocks_to_map;
3578 fiemap->fm_mapped_extents = random() & 0xffff;
3579 fiemap->fm_start = off;
3580 fiemap->fm_length = ((int64_t)random() << 32) + random();
3582 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3584 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3585 procid, opno, f.path, st, (long long)fiemap->fm_start,
3586 (long long) fiemap->fm_length,
3587 translate_fiemap_flags(fiemap->fm_flags), e);
3595 freesp_f(int opno, long r)
3600 struct xfs_flock64 fl;
3608 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3610 printf("%d/%d: freesp - no filename\n", procid, opno);
3614 fd = open_path(&f, O_RDWR);
3615 e = fd < 0 ? errno : 0;
3619 printf("%d/%d: freesp - open %s failed %d\n",
3620 procid, opno, f.path, e);
3624 if (fstat64(fd, &stb) < 0) {
3626 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3627 procid, opno, f.path, errno);
3632 inode_info(st, sizeof(st), &stb, v);
3633 lr = ((int64_t)random() << 32) + random();
3634 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3636 fl.l_whence = SEEK_SET;
3639 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3641 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3642 procid, opno, f.path, st, (long long)off, e);
3648 fsync_f(int opno, long r)
3656 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3658 printf("%d/%d: fsync - no filename\n", procid, opno);
3662 fd = open_file_or_dir(&f, O_WRONLY);
3663 e = fd < 0 ? errno : 0;
3667 printf("%d/%d: fsync - open %s failed %d\n",
3668 procid, opno, f.path, e);
3672 e = fsync(fd) < 0 ? errno : 0;
3674 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3680 gen_random_string(int len)
3682 static const char charset[] = "0123456789"
3683 "abcdefghijklmnopqrstuvwxyz"
3684 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3695 for (i = 0; i < len; i++)
3696 s[i] = charset[random() % sizeof(charset)];
3702 getattr_f(int opno, long r)
3711 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3712 append_pathname(&f, ".");
3713 fd = open_path(&f, O_RDWR);
3714 e = fd < 0 ? errno : 0;
3717 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3719 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3725 getdents_f(int opno, long r)
3732 if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
3733 append_pathname(&f, ".");
3734 dir = opendir_path(&f);
3738 printf("%d/%d: getdents - can't open %s\n",
3739 procid, opno, f.path);
3743 while (readdir64(dir) != NULL)
3746 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3752 getfattr_f(int opno, long r)
3758 char name[XATTR_NAME_BUF_SIZE];
3764 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3766 printf("%d/%d: getfattr - no filename\n", procid, opno);
3772 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3773 * to read a xattr that doesn't exist (fgetxattr should fail with
3774 * errno set to ENOATTR (61) in this case).
3776 if (fep->xattr_counter > 0)
3777 xattr_num = (random() % fep->xattr_counter) + 1;
3781 e = generate_xattr_name(xattr_num, name, sizeof(name));
3783 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3784 procid, opno, f.path, e);
3788 value_len = getxattr(f.path, name, NULL, 0);
3789 if (value_len < 0) {
3791 printf("%d/%d: getfattr file %s name %s failed %d\n",
3792 procid, opno, f.path, name, errno);
3796 /* A xattr without value.*/
3797 if (value_len == 0) {
3802 value = malloc(value_len);
3805 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3806 procid, opno, f.path, value_len);
3810 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3813 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3814 procid, opno, f.path, name, value_len, e);
3821 link_f(int opno, long r)
3835 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3837 printf("%d/%d: link - no file\n", procid, opno);
3841 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3847 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3851 (void)fent_to_name(&l, fep);
3852 printf("%d/%d: link - no filename from %s\n",
3853 procid, opno, l.path);
3859 e = link_path(&f, &l) < 0 ? errno : 0;
3862 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3864 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3866 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3873 listfattr_f(int opno, long r)
3879 char *buffer = NULL;
3883 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
3885 printf("%d/%d: listfattr - no filename\n", procid, opno);
3890 e = listxattr(f.path, NULL, 0);
3893 printf("%d/%d: listfattr %s failed %d\n",
3894 procid, opno, f.path, errno);
3898 if (buffer_len == 0) {
3900 printf("%d/%d: listfattr %s - has no extended attributes\n",
3901 procid, opno, f.path);
3905 buffer = malloc(buffer_len);
3908 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3909 procid, opno, f.path, buffer_len);
3913 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3915 printf("%d/%d: listfattr %s buffer length %d %d\n",
3916 procid, opno, f.path, buffer_len, e);
3923 mkdir_f(int opno, long r)
3933 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
3938 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3942 (void)fent_to_name(&f, fep);
3943 printf("%d/%d: mkdir - no filename from %s\n",
3944 procid, opno, f.path);
3949 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3952 add_to_flist(FT_DIR, id, parid, 0);
3954 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3955 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3961 mknod_f(int opno, long r)
3971 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
3976 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3980 (void)fent_to_name(&f, fep);
3981 printf("%d/%d: mknod - no filename from %s\n",
3982 procid, opno, f.path);
3987 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3990 add_to_flist(FT_DEV, id, parid, 0);
3992 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3993 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3998 #ifdef HAVE_SYS_MMAN_H
3999 struct print_flags mmap_flags[] = {
4000 { MAP_SHARED, "SHARED"},
4001 { MAP_PRIVATE, "PRIVATE"},
4005 #define translate_mmap_flags(flags) \
4006 ({translate_flags(flags, "|", mmap_flags);})
4010 do_mmap(int opno, long r, int prot)
4012 #ifdef HAVE_SYS_MMAN_H
4024 sigjmp_buf sigbus_jmpbuf;
4027 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4029 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4033 fd = open_path(&f, O_RDWR);
4034 e = fd < 0 ? errno : 0;
4038 printf("%d/%d: do_mmap - open %s failed %d\n",
4039 procid, opno, f.path, e);
4043 if (fstat64(fd, &stb) < 0) {
4045 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4046 procid, opno, f.path, errno);
4051 inode_info(st, sizeof(st), &stb, v);
4052 if (stb.st_size == 0) {
4054 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4061 lr = ((int64_t)random() << 32) + random();
4062 off = (off64_t)(lr % stb.st_size);
4063 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4064 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4066 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4067 addr = mmap(NULL, len, prot, flags, fd, off);
4068 e = (addr == MAP_FAILED) ? errno : 0;
4071 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4072 procid, opno, f.path, st, (long long)off,
4073 (int)len, translate_mmap_flags(flags), e);
4079 if (prot & PROT_WRITE) {
4080 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4081 sigbus_jmp = &sigbus_jmpbuf;
4082 memset(addr, nameseq & 0xff, len);
4086 if ((buf = malloc(len)) != NULL) {
4087 memcpy(buf, addr, len);
4092 /* set NULL to stop other functions from doing siglongjmp */
4096 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4097 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4098 f.path, st, (long long)off, (int)len,
4099 translate_mmap_flags(flags),
4100 (e == 0) ? "0" : "Bus error");
4108 mread_f(int opno, long r)
4110 #ifdef HAVE_SYS_MMAN_H
4111 do_mmap(opno, r, PROT_READ);
4116 mwrite_f(int opno, long r)
4118 #ifdef HAVE_SYS_MMAN_H
4119 do_mmap(opno, r, PROT_WRITE);
4124 punch_f(int opno, long r)
4126 #ifdef HAVE_LINUX_FALLOC_H
4127 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4132 zero_f(int opno, long r)
4134 #ifdef HAVE_LINUX_FALLOC_H
4135 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4140 collapse_f(int opno, long r)
4142 #ifdef HAVE_LINUX_FALLOC_H
4143 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4148 insert_f(int opno, long r)
4150 #ifdef HAVE_LINUX_FALLOC_H
4151 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4156 read_f(int opno, long r)
4170 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4172 printf("%d/%d: read - no filename\n", procid, opno);
4176 fd = open_path(&f, O_RDONLY);
4177 e = fd < 0 ? errno : 0;
4181 printf("%d/%d: read - open %s failed %d\n",
4182 procid, opno, f.path, e);
4186 if (fstat64(fd, &stb) < 0) {
4188 printf("%d/%d: read - fstat64 %s failed %d\n",
4189 procid, opno, f.path, errno);
4194 inode_info(st, sizeof(st), &stb, v);
4195 if (stb.st_size == 0) {
4197 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4203 lr = ((int64_t)random() << 32) + random();
4204 off = (off64_t)(lr % stb.st_size);
4205 lseek64(fd, off, SEEK_SET);
4206 len = (random() % FILELEN_MAX) + 1;
4208 e = read(fd, buf, len) < 0 ? errno : 0;
4211 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4212 procid, opno, f.path, st, (long long)off, (int)len, e);
4218 readlink_f(int opno, long r)
4226 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4228 printf("%d/%d: readlink - no filename\n", procid, opno);
4232 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4235 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4240 readv_f(int opno, long r)
4252 struct iovec *iov = NULL;
4259 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4261 printf("%d/%d: readv - no filename\n", procid, opno);
4265 fd = open_path(&f, O_RDONLY);
4266 e = fd < 0 ? errno : 0;
4270 printf("%d/%d: readv - open %s failed %d\n",
4271 procid, opno, f.path, e);
4275 if (fstat64(fd, &stb) < 0) {
4277 printf("%d/%d: readv - fstat64 %s failed %d\n",
4278 procid, opno, f.path, errno);
4283 inode_info(st, sizeof(st), &stb, v);
4284 if (stb.st_size == 0) {
4286 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4292 lr = ((int64_t)random() << 32) + random();
4293 off = (off64_t)(lr % stb.st_size);
4294 lseek64(fd, off, SEEK_SET);
4295 len = (random() % FILELEN_MAX) + 1;
4298 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4299 iov = calloc(iovcnt, sizeof(struct iovec));
4300 iovl = len / iovcnt;
4302 for (i=0; i<iovcnt; i++) {
4303 (iov + i)->iov_base = (buf + iovb);
4304 (iov + i)->iov_len = iovl;
4308 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4311 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4312 procid, opno, f.path, st, (long long)off, (int)iovl,
4319 removefattr_f(int opno, long r)
4325 char name[XATTR_NAME_BUF_SIZE];
4329 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4331 printf("%d/%d: removefattr - no filename\n", procid, opno);
4337 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4338 * remove a xattr that doesn't exist (fremovexattr should fail with
4339 * errno set to ENOATTR (61) in this case).
4341 if (fep->xattr_counter > 0)
4342 xattr_num = (random() % fep->xattr_counter) + 1;
4346 e = generate_xattr_name(xattr_num, name, sizeof(name));
4348 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4349 procid, opno, f.path, e);
4353 e = removexattr(f.path, name) < 0 ? errno : 0;
4355 printf("%d/%d: removefattr file %s name %s %d\n",
4356 procid, opno, f.path, name, e);
4361 struct print_flags renameat2_flags [] = {
4362 { RENAME_NOREPLACE, "NOREPLACE"},
4363 { RENAME_EXCHANGE, "EXCHANGE"},
4364 { RENAME_WHITEOUT, "WHITEOUT"},
4368 #define translate_renameat2_flags(mode) \
4369 ({translate_flags(mode, "|", renameat2_flags);})
4372 do_renameat2(int opno, long r, int mode)
4388 /* get an existing path for the source of the rename */
4390 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4391 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4393 printf("%d/%d: rename - no source filename\n",
4400 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4401 * order to maintain filelist/filename integrity, we should
4402 * restrict exchange operation to files of the same type.
4404 if (mode == RENAME_EXCHANGE) {
4405 which = 1 << (flp - flist);
4406 init_pathname(&newf);
4407 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4409 printf("%d/%d: rename - no target filename\n",
4411 free_pathname(&newf);
4415 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4416 fents_ancestor_check(dfep, fep))) {
4418 printf("%d/%d: rename(REXCHANGE) %s and %s "
4419 "have ancestor-descendant relationship\n",
4420 procid, opno, f.path, newf.path);
4421 free_pathname(&newf);
4427 parid = dfep->parent;
4430 * Get an existing directory for the destination parent
4433 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4440 * Generate a new path using an existing parent directory
4443 init_pathname(&newf);
4444 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4448 (void)fent_to_name(&f, dfep);
4449 printf("%d/%d: rename - no filename from %s\n",
4450 procid, opno, f.path);
4452 free_pathname(&newf);
4457 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4460 int xattr_counter = fep->xattr_counter;
4461 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4462 int ft = flp - flist;
4465 oldparid = fep->parent;
4468 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4469 * old parent id for the others.
4471 if (ft == FT_DIR || ft == FT_SUBVOL)
4472 fix_parent(oldid, id, swap);
4474 if (mode == RENAME_WHITEOUT) {
4475 fep->xattr_counter = 0;
4476 add_to_flist(flp - flist, id, parid, xattr_counter);
4477 } else if (mode == RENAME_EXCHANGE) {
4478 fep->xattr_counter = dfep->xattr_counter;
4479 dfep->xattr_counter = xattr_counter;
4481 del_from_flist(flp - flist, fep - flp->fents);
4482 add_to_flist(flp - flist, id, parid, xattr_counter);
4486 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4487 opno, translate_renameat2_flags(mode), f.path,
4490 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4491 procid, opno, oldid, oldparid);
4492 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4493 procid, opno, id, parid);
4496 free_pathname(&newf);
4501 rename_f(int opno, long r)
4503 do_renameat2(opno, r, 0);
4507 rnoreplace_f(int opno, long r)
4509 do_renameat2(opno, r, RENAME_NOREPLACE);
4513 rexchange_f(int opno, long r)
4515 do_renameat2(opno, r, RENAME_EXCHANGE);
4519 rwhiteout_f(int opno, long r)
4521 do_renameat2(opno, r, RENAME_WHITEOUT);
4525 resvsp_f(int opno, long r)
4530 struct xfs_flock64 fl;
4538 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4540 printf("%d/%d: resvsp - no filename\n", procid, opno);
4544 fd = open_path(&f, O_RDWR);
4545 e = fd < 0 ? errno : 0;
4549 printf("%d/%d: resvsp - open %s failed %d\n",
4550 procid, opno, f.path, e);
4554 if (fstat64(fd, &stb) < 0) {
4556 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4557 procid, opno, f.path, errno);
4562 inode_info(st, sizeof(st), &stb, v);
4563 lr = ((int64_t)random() << 32) + random();
4564 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4566 fl.l_whence = SEEK_SET;
4568 fl.l_len = (off64_t)(random() % (1024 * 1024));
4569 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4571 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4572 procid, opno, f.path, st,
4573 (long long)off, (long long)fl.l_len, e);
4579 rmdir_f(int opno, long r)
4589 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4591 printf("%d/%d: rmdir - no directory\n", procid, opno);
4595 e = rmdir_path(&f) < 0 ? errno : 0;
4599 oldparid = fep->parent;
4600 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4603 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4605 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4606 procid, opno, oldid, oldparid);
4612 setattr_f(int opno, long r)
4621 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4622 append_pathname(&f, ".");
4623 fd = open_path(&f, O_RDWR);
4624 e = fd < 0 ? errno : 0;
4627 fl = attr_mask & (uint)random();
4628 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4630 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4636 setfattr_f(int opno, long r)
4643 char name[XATTR_NAME_BUF_SIZE];
4649 if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
4651 printf("%d/%d: setfattr - no filename\n", procid, opno);
4656 if ((fep->xattr_counter > 0) && (random() % 2)) {
4658 * Use an existing xattr name for replacing its value or
4659 * create again a xattr that was previously deleted.
4661 xattr_num = (random() % fep->xattr_counter) + 1;
4663 flag = XATTR_REPLACE;
4665 /* Use a new xattr name. */
4666 xattr_num = fep->xattr_counter + 1;
4668 * Don't always use the create flag because even if our xattr
4669 * counter is 0, we may still have xattrs associated to this
4670 * file (this happens when xattrs are added to a file through
4671 * one of its other hard links), so we can end up updating an
4672 * existing xattr too.
4675 flag = XATTR_CREATE;
4679 * The maximum supported value size depends on the filesystem
4680 * implementation, but 100 bytes is a safe value for most filesystems
4683 value_len = random() % 101;
4684 value = gen_random_string(value_len);
4685 if (!value && value_len > 0) {
4687 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4688 procid, opno, f.path, value_len);
4691 e = generate_xattr_name(xattr_num, name, sizeof(name));
4693 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4694 procid, opno, f.path, e);
4698 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4700 fep->xattr_counter++;
4702 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4703 procid, opno, f.path, name, xattr_flag_to_string(flag),
4711 snapshot_f(int opno, long r)
4713 #ifdef HAVE_BTRFSUTIL_H
4714 enum btrfs_util_error e;
4725 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4727 printf("%d/%d: snapshot - no subvolume\n", procid,
4732 init_pathname(&newf);
4734 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4738 (void)fent_to_name(&f, fep);
4739 printf("%d/%d: snapshot - no filename from %s\n",
4740 procid, opno, f.path);
4745 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4746 if (e == BTRFS_UTIL_OK)
4747 add_to_flist(FT_SUBVOL, id, parid, 0);
4749 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4750 f.path, newf.path, e, btrfs_util_strerror(e));
4751 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4754 free_pathname(&newf);
4760 stat_f(int opno, long r)
4768 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4770 printf("%d/%d: stat - no entries\n", procid, opno);
4774 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4777 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4782 subvol_create_f(int opno, long r)
4784 #ifdef HAVE_BTRFSUTIL_H
4785 enum btrfs_util_error e;
4795 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4799 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4803 (void)fent_to_name(&f, fep);
4804 printf("%d/%d: subvol_create - no filename from %s\n",
4805 procid, opno, f.path);
4810 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4811 if (e == BTRFS_UTIL_OK)
4812 add_to_flist(FT_SUBVOL, id, parid, 0);
4814 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4815 f.path, e, btrfs_util_strerror(e));
4816 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4824 subvol_delete_f(int opno, long r)
4826 #ifdef HAVE_BTRFSUTIL_H
4827 enum btrfs_util_error e;
4835 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4837 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4842 e = btrfs_util_delete_subvolume(f.path, 0);
4844 if (e == BTRFS_UTIL_OK) {
4846 oldparid = fep->parent;
4847 delete_subvol_children(oldid);
4848 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4851 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4852 e, btrfs_util_strerror(e));
4853 if (e == BTRFS_UTIL_OK)
4854 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4855 procid, opno, oldid, oldparid);
4862 symlink_f(int opno, long r)
4875 if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
4880 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4884 (void)fent_to_name(&f, fep);
4885 printf("%d/%d: symlink - no filename from %s\n",
4886 procid, opno, f.path);
4891 len = (int)(random() % PATH_MAX);
4892 val = malloc(len + 1);
4894 memset(val, 'x', len);
4896 for (i = 10; i < len - 1; i += 10)
4898 e = symlink_path(val, &f) < 0 ? errno : 0;
4901 add_to_flist(FT_SYM, id, parid, 0);
4904 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4905 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4912 sync_f(int opno, long r)
4916 printf("%d/%d: sync\n", procid, opno);
4920 truncate_f(int opno, long r)
4931 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4933 printf("%d/%d: truncate - no filename\n", procid, opno);
4937 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4941 printf("%d/%d: truncate - stat64 %s failed %d\n",
4942 procid, opno, f.path, e);
4946 inode_info(st, sizeof(st), &stb, v);
4947 lr = ((int64_t)random() << 32) + random();
4948 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4950 e = truncate64_path(&f, off) < 0 ? errno : 0;
4953 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4954 st, (long long)off, e);
4959 unlink_f(int opno, long r)
4970 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4972 printf("%d/%d: unlink - no file\n", procid, opno);
4976 e = unlink_path(&f) < 0 ? errno : 0;
4980 oldparid = fep->parent;
4981 del_from_flist(flp - flist, fep - flp->fents);
4984 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4986 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4987 procid, opno, oldid, oldparid);
4993 unresvsp_f(int opno, long r)
4998 struct xfs_flock64 fl;
5006 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5008 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5012 fd = open_path(&f, O_RDWR);
5013 e = fd < 0 ? errno : 0;
5017 printf("%d/%d: unresvsp - open %s failed %d\n",
5018 procid, opno, f.path, e);
5022 if (fstat64(fd, &stb) < 0) {
5024 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5025 procid, opno, f.path, errno);
5030 inode_info(st, sizeof(st), &stb, v);
5031 lr = ((int64_t)random() << 32) + random();
5032 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5034 fl.l_whence = SEEK_SET;
5036 fl.l_len = (off64_t)(random() % (1 << 20));
5037 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5039 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5040 procid, opno, f.path, st,
5041 (long long)off, (long long)fl.l_len, e);
5047 write_f(int opno, long r)
5061 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5063 printf("%d/%d: write - no filename\n", procid, opno);
5067 fd = open_path(&f, O_WRONLY);
5068 e = fd < 0 ? errno : 0;
5072 printf("%d/%d: write - open %s failed %d\n",
5073 procid, opno, f.path, e);
5077 if (fstat64(fd, &stb) < 0) {
5079 printf("%d/%d: write - fstat64 %s failed %d\n",
5080 procid, opno, f.path, errno);
5085 inode_info(st, sizeof(st), &stb, v);
5086 lr = ((int64_t)random() << 32) + random();
5087 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5089 lseek64(fd, off, SEEK_SET);
5090 len = (random() % FILELEN_MAX) + 1;
5092 memset(buf, nameseq & 0xff, len);
5093 e = write(fd, buf, len) < 0 ? errno : 0;
5096 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5097 procid, opno, f.path, st, (long long)off, (int)len, e);
5103 writev_f(int opno, long r)
5115 struct iovec *iov = NULL;
5122 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5124 printf("%d/%d: writev - no filename\n", procid, opno);
5128 fd = open_path(&f, O_WRONLY);
5129 e = fd < 0 ? errno : 0;
5133 printf("%d/%d: writev - open %s failed %d\n",
5134 procid, opno, f.path, e);
5138 if (fstat64(fd, &stb) < 0) {
5140 printf("%d/%d: writev - fstat64 %s failed %d\n",
5141 procid, opno, f.path, errno);
5146 inode_info(st, sizeof(st), &stb, v);
5147 lr = ((int64_t)random() << 32) + random();
5148 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5150 lseek64(fd, off, SEEK_SET);
5151 len = (random() % FILELEN_MAX) + 1;
5153 memset(buf, nameseq & 0xff, len);
5155 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5156 iov = calloc(iovcnt, sizeof(struct iovec));
5157 iovl = len / iovcnt;
5159 for (i=0; i<iovcnt; i++) {
5160 (iov + i)->iov_base = (buf + iovb);
5161 (iov + i)->iov_len = iovl;
5165 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5169 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5170 procid, opno, f.path, st, (long long)off, (int)iovl,
5177 xattr_flag_to_string(int flag)
5179 if (flag == XATTR_CREATE)
5181 if (flag == XATTR_REPLACE)