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))
203 #define FLIST_SLOT_INCR 16
206 #define MAXFSIZE ((1ULL << 63) - 1ULL)
207 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
209 #define XATTR_NAME_BUF_SIZE 18
211 void afsync_f(int, long);
212 void allocsp_f(int, long);
213 void aread_f(int, long);
214 void attr_remove_f(int, long);
215 void attr_set_f(int, long);
216 void awrite_f(int, long);
217 void bulkstat_f(int, long);
218 void bulkstat1_f(int, long);
219 void chown_f(int, long);
220 void clonerange_f(int, long);
221 void copyrange_f(int, long);
222 void creat_f(int, long);
223 void deduperange_f(int, long);
224 void dread_f(int, long);
225 void dwrite_f(int, long);
226 void fallocate_f(int, long);
227 void fdatasync_f(int, long);
228 void fiemap_f(int, long);
229 void freesp_f(int, long);
230 void fsync_f(int, long);
231 char *gen_random_string(int);
232 void getattr_f(int, long);
233 void getdents_f(int, long);
234 void getfattr_f(int, long);
235 void link_f(int, long);
236 void listfattr_f(int, long);
237 void mkdir_f(int, long);
238 void mknod_f(int, long);
239 void mread_f(int, long);
240 void mwrite_f(int, long);
241 void punch_f(int, long);
242 void zero_f(int, long);
243 void collapse_f(int, long);
244 void insert_f(int, long);
245 void read_f(int, long);
246 void readlink_f(int, long);
247 void readv_f(int, long);
248 void removefattr_f(int, long);
249 void rename_f(int, long);
250 void rnoreplace_f(int, long);
251 void rexchange_f(int, long);
252 void rwhiteout_f(int, long);
253 void resvsp_f(int, long);
254 void rmdir_f(int, long);
255 void setattr_f(int, long);
256 void setfattr_f(int, long);
257 void setxattr_f(int, long);
258 void snapshot_f(int, long);
259 void splice_f(int, long);
260 void stat_f(int, long);
261 void subvol_create_f(int, long);
262 void subvol_delete_f(int, long);
263 void symlink_f(int, long);
264 void sync_f(int, long);
265 void truncate_f(int, long);
266 void unlink_f(int, long);
267 void unresvsp_f(int, long);
268 void write_f(int, long);
269 void writev_f(int, long);
270 char *xattr_flag_to_string(int);
273 /* { OP_ENUM, "name", function, freq, iswrite }, */
274 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
275 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
276 { OP_AREAD, "aread", aread_f, 1, 0 },
277 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
278 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
279 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
280 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
281 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
282 { OP_CHOWN, "chown", chown_f, 3, 1 },
283 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
284 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
285 { OP_CREAT, "creat", creat_f, 4, 1 },
286 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
287 { OP_DREAD, "dread", dread_f, 4, 0 },
288 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
289 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
290 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
291 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
292 { OP_FREESP, "freesp", freesp_f, 1, 1 },
293 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
294 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
295 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
296 /* get extended attribute */
297 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
298 { OP_LINK, "link", link_f, 1, 1 },
299 /* list extent attributes */
300 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
301 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
302 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
303 { OP_MREAD, "mread", mread_f, 2, 0 },
304 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
305 { OP_PUNCH, "punch", punch_f, 1, 1 },
306 { OP_ZERO, "zero", zero_f, 1, 1 },
307 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
308 { OP_INSERT, "insert", insert_f, 1, 1 },
309 { OP_READ, "read", read_f, 1, 0 },
310 { OP_READLINK, "readlink", readlink_f, 1, 0 },
311 { OP_READV, "readv", readv_f, 1, 0 },
312 /* remove (delete) extended attribute */
313 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
314 { OP_RENAME, "rename", rename_f, 2, 1 },
315 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
316 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
317 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
318 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
319 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
320 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
321 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
322 /* set extended attribute */
323 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
324 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
325 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
326 { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
327 { OP_SPLICE, "splice", splice_f, 1, 1 },
328 { OP_STAT, "stat", stat_f, 1, 0 },
329 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
330 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
331 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
332 { OP_SYNC, "sync", sync_f, 1, 1 },
333 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
334 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
335 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
336 { OP_WRITE, "write", write_f, 4, 1 },
337 { OP_WRITEV, "writev", writev_f, 4, 1 },
340 flist_t flist[FT_nft] = {
354 struct xfs_fsop_geom geom;
365 unsigned int idmodulo = XFS_IDMODULO_MAX;
366 unsigned int attr_mask = ~0;
369 unsigned long seed = 0;
373 int verifiable_log = 0;
374 sig_atomic_t should_stop = 0;
375 sigjmp_buf *sigbus_jmp = NULL;
376 char *execute_cmd = NULL;
377 int execute_freq = 1;
378 struct print_string flag_str = {0};
380 void add_to_flist(int, int, int, int);
381 void append_pathname(pathname_t *, char *);
382 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
383 int attr_remove_path(pathname_t *, const char *, int);
384 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
385 void check_cwd(void);
386 void cleanup_flist(void);
387 int creat_path(pathname_t *, mode_t);
388 void dcache_enter(int, int);
389 void dcache_init(void);
390 fent_t *dcache_lookup(int);
391 void dcache_purge(int, int);
392 void del_from_flist(int, int);
393 int dirid_to_name(char *, int);
395 int fent_to_name(pathname_t *, fent_t *);
396 bool fents_ancestor_check(fent_t *, fent_t *);
397 void fix_parent(int, int, bool);
398 void free_pathname(pathname_t *);
399 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
400 int generate_xattr_name(int, char *, int);
401 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
402 void init_pathname(pathname_t *);
403 int lchown_path(pathname_t *, uid_t, gid_t);
404 int link_path(pathname_t *, pathname_t *);
405 int lstat64_path(pathname_t *, struct stat64 *);
406 void make_freq_table(void);
407 int mkdir_path(pathname_t *, mode_t);
408 int mknod_path(pathname_t *, mode_t, dev_t);
409 void namerandpad(int, char *, int);
410 int open_file_or_dir(pathname_t *, int);
411 int open_path(pathname_t *, int);
412 DIR *opendir_path(pathname_t *);
413 void process_freq(char *);
414 int readlink_path(pathname_t *, char *, size_t);
415 int rename_path(pathname_t *, pathname_t *, int);
416 int rmdir_path(pathname_t *);
417 void separate_pathname(pathname_t *, char *, pathname_t *);
418 void show_ops(int, char *);
419 int stat64_path(pathname_t *, struct stat64 *);
420 int symlink_path(const char *, pathname_t *);
421 int truncate64_path(pathname_t *, off64_t);
422 int unlink_path(pathname_t *);
424 void write_freq(void);
425 void zero_freq(void);
426 void non_btrfs_freq(const char *);
428 void sg_handler(int signum)
436 * Only handle SIGBUS when mmap write to a hole and no
437 * block can be allocated due to ENOSPC, abort otherwise.
440 siglongjmp(*sigbus_jmp, -1);
442 printf("Unknown SIGBUS is caught, Abort!\n");
445 /* should not reach here */
452 int main(int argc, char **argv)
456 char *dirname = NULL;
457 char *logname = NULL;
458 char rpath[PATH_MAX];
467 xfs_error_injection_t err_inj;
468 struct sigaction action;
470 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
472 errrange = errtag = 0;
474 nops = sizeof(ops) / sizeof(ops[0]);
475 ops_end = &ops[nops];
477 while ((c = getopt(argc, argv, allopts)) != -1) {
486 sscanf(optarg, "%d", &errtag);
490 } else if (errtag == 0)
492 if (errtag >= XFS_ERRTAG_MAX) {
494 "error tag %d too large (max %d)\n",
495 errtag, XFS_ERRTAG_MAX - 1);
500 process_freq(optarg);
503 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
504 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
507 idmodulo = strtoul(optarg, NULL, 0);
508 if (idmodulo > XFS_IDMODULO_MAX) {
510 "chown modulo %d too big (max %d)\n",
511 idmodulo, XFS_IDMODULO_MAX);
516 loops = atoi(optarg);
519 operations = atoi(optarg);
526 nproc = atoi(optarg);
532 seed = strtoul(optarg, NULL, 0);
541 execute_cmd = optarg;
547 attr_mask = strtoul(optarg, NULL, 0);
551 if (optarg[0] == 'c')
562 execute_freq = strtoul(optarg, NULL, 0);
565 fprintf(stderr, "%s - invalid parameters\n",
575 /* no directory specified */
576 if (!nousage) usage();
580 non_btrfs_freq(dirname);
581 (void)mkdir(dirname, 0777);
582 if (logname && logname[0] != '/') {
583 if (!getcwd(rpath, sizeof(rpath))){
584 perror("getcwd failed");
590 if (chdir(dirname) < 0) {
595 char path[PATH_MAX + NAME_MAX + 1];
596 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
597 if (freopen(path, "a", stdout) == NULL) {
598 perror("freopen logfile failed");
602 sprintf(buf, "fss%x", (unsigned int)getpid());
603 fd = creat(buf, 0666);
604 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
605 maxfsize = (off64_t)MAXFSIZE32;
607 maxfsize = (off64_t)MAXFSIZE;
612 gettimeofday(&t, (void *)NULL);
613 seed = (int)t.tv_sec ^ (int)t.tv_usec;
614 printf("seed = %ld\n", seed);
616 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
617 if (i >= 0 && geom.rtblocks)
618 rtpct = MIN(MAX(geom.rtblocks * 100 /
619 (geom.rtblocks + geom.datablocks), 1), 99);
628 for (i = 0; i < j; i++)
631 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
637 for (i = 0; i < j; i++)
640 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
642 printf("Injecting failure on tag #%d\n", errtag);
643 err_inj.errtag = errtag;
645 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
647 perror("fsstress - XFS_SYSSGI error injection call");
656 action.sa_handler = sg_handler;
657 sigemptyset(&action.sa_mask);
659 if (sigaction(SIGTERM, &action, 0)) {
660 perror("sigaction failed");
664 for (i = 0; i < nproc; i++) {
666 sigemptyset(&action.sa_mask);
667 action.sa_handler = SIG_DFL;
668 if (sigaction(SIGTERM, &action, 0))
670 action.sa_handler = sg_handler;
671 if (sigaction(SIGBUS, &action, 0))
673 #ifdef HAVE_SYS_PRCTL_H
674 prctl(PR_SET_PDEATHSIG, SIGKILL);
675 if (getppid() == 1) /* parent died already? */
679 char path[PATH_MAX + NAME_MAX + 2 + 11];
680 snprintf(path, sizeof(path), "%s/%s.%d",
682 if (freopen(path, "a", stdout) == NULL) {
683 perror("freopen logfile failed");
689 if (io_setup(128, &io_ctx) != 0) {
690 fprintf(stderr, "io_setup failed");
694 for (i = 0; !loops || (i < loops); i++)
697 if(io_destroy(io_ctx) != 0) {
698 fprintf(stderr, "io_destroy failed");
708 while (wait(&stat) > 0 && !should_stop) {
711 action.sa_flags = SA_RESTART;
712 sigaction(SIGTERM, &action, 0);
713 kill(-getpid(), SIGTERM);
714 while (wait(&stat) > 0)
720 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
722 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
724 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
737 add_string(struct print_string *str, const char *add)
739 int len = strlen(add);
744 if (len > (str->max - 1) - str->len) {
745 str->len = str->max - 1;
749 memcpy(str->buffer + str->len, add, len);
751 str->buffer[str->len] = '\0';
757 translate_flags(int flags, const char *delim,
758 const struct print_flags *flag_array)
760 int i, mask, first = 1;
763 if (!flag_str.buffer) {
764 flag_str.buffer = malloc(4096);
768 if (!flag_str.buffer)
771 flag_str.buffer[0] = '\0';
773 for (i = 0; flag_array[i].name && flags; i++) {
774 mask = flag_array[i].mask;
775 if ((flags & mask) != mask)
778 add = flag_array[i].name;
781 add_string(&flag_str, delim);
784 add_string(&flag_str, add);
787 /* Check whether there are any leftover flags. */
793 add_string(&flag_str, delim);
795 ret = snprintf(number, 11, "0x%x", flags) > 0;
796 if (ret > 0 && ret <= 11)
797 add_string(&flag_str, number);
800 return flag_str.buffer;
804 add_to_flist(int ft, int id, int parent, int xattr_counter)
810 if (ftp->nfiles == ftp->nslots) {
811 ftp->nslots += FLIST_SLOT_INCR;
812 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
814 fep = &ftp->fents[ftp->nfiles++];
817 fep->parent = parent;
818 fep->xattr_counter = xattr_counter;
822 append_pathname(pathname_t *name, char *str)
828 /* attempting to append to a dir a zero length path */
829 if (len && *str == '/' && name->len == 0) {
830 fprintf(stderr, "fsstress: append_pathname failure\n");
831 assert(chdir(homedir) == 0);
836 name->path = realloc(name->path, name->len + 1 + len);
837 strcpy(&name->path[name->len], str);
842 attr_list_path(pathname_t *name,
844 const int buffersize,
846 attrlist_cursor_t *cursor)
848 char buf[NAME_MAX + 1];
852 if (flags != ATTR_DONTFOLLOW) {
857 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
858 if (rval >= 0 || errno != ENAMETOOLONG)
860 separate_pathname(name, buf, &newname);
861 if (chdir(buf) == 0) {
862 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
863 assert(chdir("..") == 0);
865 free_pathname(&newname);
870 attr_remove_path(pathname_t *name, const char *attrname, int flags)
872 char buf[NAME_MAX + 1];
876 rval = attr_remove(name->path, attrname, flags);
877 if (rval >= 0 || errno != ENAMETOOLONG)
879 separate_pathname(name, buf, &newname);
880 if (chdir(buf) == 0) {
881 rval = attr_remove_path(&newname, attrname, flags);
882 assert(chdir("..") == 0);
884 free_pathname(&newname);
889 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
890 const int valuelength, int flags)
892 char buf[NAME_MAX + 1];
896 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
897 if (rval >= 0 || errno != ENAMETOOLONG)
899 separate_pathname(name, buf, &newname);
900 if (chdir(buf) == 0) {
901 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
903 assert(chdir("..") == 0);
905 free_pathname(&newname);
913 struct stat64 statbuf;
915 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
917 assert(chdir(homedir) == 0);
918 fprintf(stderr, "fsstress: check_cwd failure\n");
925 * go thru flist and release all entries
927 * NOTE: this function shouldn't be called until the end of a process
935 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
944 creat_path(pathname_t *name, mode_t mode)
946 char buf[NAME_MAX + 1];
950 rval = creat(name->path, mode);
951 if (rval >= 0 || errno != ENAMETOOLONG)
953 separate_pathname(name, buf, &newname);
954 if (chdir(buf) == 0) {
955 rval = creat_path(&newname, mode);
956 assert(chdir("..") == 0);
958 free_pathname(&newname);
963 dcache_enter(int dirid, int slot)
965 dcache[dirid % NDCACHE] = slot;
973 for (i = 0; i < NDCACHE; i++)
978 dcache_lookup(int dirid)
983 i = dcache[dirid % NDCACHE];
984 if (i >= 0 && i < flist[FT_DIR].nfiles &&
985 (fep = &flist[FT_DIR].fents[i])->id == dirid)
987 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
988 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
994 dcache_purge(int dirid, int ft)
997 dcp = &dcache[dirid % NDCACHE];
998 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
999 flist[ft].fents[*dcp].id == dirid)
1004 * Delete the item from the list by
1005 * moving last entry over the deleted one;
1006 * unless deleted entry is the last one.
1007 * Input: which file list array and which slot in array
1010 del_from_flist(int ft, int slot)
1015 if (ft == FT_DIR || ft == FT_SUBVOL)
1016 dcache_purge(ftp->fents[slot].id, ft);
1017 if (slot != ftp->nfiles - 1) {
1018 if (ft == FT_DIR || ft == FT_SUBVOL)
1019 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1020 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1026 delete_subvol_children(int parid)
1031 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1033 for (c = 0; c < flp->nfiles; c++) {
1034 if (flp->fents[c].parent == parid) {
1035 int id = flp->fents[c].id;
1036 del_from_flist(i, c);
1037 if (i == FT_DIR || i == FT_SUBVOL)
1038 delete_subvol_children(id);
1046 dirid_to_fent(int dirid)
1053 if ((fep = dcache_lookup(dirid)))
1057 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1058 if (fep->id == dirid) {
1059 dcache_enter(dirid, fep - flp->fents);
1073 struct stat64 statbuf;
1081 dividend = (operations + execute_freq) / (execute_freq + 1);
1082 sprintf(buf, "p%x", procid);
1083 (void)mkdir(buf, 0777);
1084 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1088 top_ino = statbuf.st_ino;
1089 homedir = getcwd(NULL, 0);
1091 perror("getcwd failed");
1097 namerand = random();
1098 for (opno = 0; opno < operations; opno++) {
1099 if (execute_cmd && opno && opno % dividend == 0) {
1101 printf("%d: execute command %s\n", opno,
1103 rval = system(execute_cmd);
1105 fprintf(stderr, "execute command failed with "
1108 p = &ops[freq_table[random() % freq_table_size]];
1109 p->func(opno, random());
1111 * test for forced shutdown by stat'ing the test
1112 * directory. If this stat returns EIO, assume
1113 * the forced shutdown happened.
1115 if (errtag != 0 && opno % 100 == 0) {
1116 rval = stat64(".", &statbuf);
1118 fprintf(stderr, "Detected EIO\n");
1124 assert(chdir("..") == 0);
1129 sprintf(cmd, "rm -rf %s", buf);
1132 perror("cleaning up");
1138 * build up a pathname going thru the file entry and all
1139 * its parent entries
1140 * Return 0 on error, 1 on success;
1143 fent_to_name(pathname_t *name, fent_t *fep)
1145 flist_t *flp = &flist[fep->ft];
1146 char buf[NAME_MAX + 1];
1154 /* build up parent directory name */
1155 if (fep->parent != -1) {
1156 pfep = dirid_to_fent(fep->parent);
1159 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1160 procid, fep->id, fep->parent);
1165 e = fent_to_name(name, pfep);
1168 append_pathname(name, "/");
1171 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1172 namerandpad(fep->id, buf, i);
1173 append_pathname(name, buf);
1178 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1182 for (tmpfep = fep; tmpfep->parent != -1;
1183 tmpfep = dirid_to_fent(tmpfep->parent)) {
1184 if (tmpfep->parent == dfep->id)
1192 fix_parent(int oldid, int newid, bool swap)
1199 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1200 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1201 if (fep->parent == oldid)
1202 fep->parent = newid;
1203 else if (swap && fep->parent == newid)
1204 fep->parent = oldid;
1210 free_pathname(pathname_t *name)
1220 * Generate a filename of type ft.
1221 * If we have a fep which should be a directory then use it
1222 * as the parent path for this new filename i.e. prepend with it.
1225 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1227 char buf[NAME_MAX + 1];
1236 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1237 namerandpad(id, buf, len);
1239 /* prepend fep parent dir-name to it */
1241 e = fent_to_name(name, fep);
1244 append_pathname(name, "/");
1246 append_pathname(name, buf);
1250 for (j = 0; !*v && j < ilistlen; j++) {
1251 if (ilist[j] == id) {
1259 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1263 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1273 * Input: "which" to choose the file-types eg. non-directory
1274 * Input: "r" to choose which file
1275 * Output: file-list, file-entry, name for the chosen file.
1276 * Output: verbose if chosen file is on the ilist.
1279 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1282 int totalsum = 0; /* total number of matching files */
1283 int partialsum = 0; /* partial sum of matching files */
1289 int e = 1; /* success */
1292 * go thru flist and add up number of files for each
1293 * category that matches with <which>.
1295 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1296 if (which & (1 << i))
1297 totalsum += flp->nfiles;
1299 if (totalsum == 0) {
1309 * Now we have possible matches between 0..totalsum-1.
1310 * And we use r to help us choose which one we want,
1311 * which when bounded by totalsum becomes x.
1313 x = (int)(r % totalsum);
1314 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1315 if (which & (1 << i)) {
1316 if (x < partialsum + flp->nfiles) {
1318 /* found the matching file entry */
1319 fep = &flp->fents[x - partialsum];
1321 /* fill-in what we were asked for */
1323 e = fent_to_name(name, fep);
1326 fprintf(stderr, "%d: failed to get path for entry:"
1327 " id=%d,parent=%d\n",
1328 procid, fep->id, fep->parent);
1337 /* turn on verbose if its an ilisted file */
1339 for (j = 0; !*v && j < ilistlen; j++) {
1340 if (ilist[j] == fep->id) {
1347 partialsum += flp->nfiles;
1351 fprintf(stderr, "fsstress: get_fname failure\n");
1358 init_pathname(pathname_t *name)
1365 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1367 char buf[NAME_MAX + 1];
1371 rval = lchown(name->path, owner, group);
1372 if (rval >= 0 || errno != ENAMETOOLONG)
1374 separate_pathname(name, buf, &newname);
1375 if (chdir(buf) == 0) {
1376 rval = lchown_path(&newname, owner, group);
1377 assert(chdir("..") == 0);
1379 free_pathname(&newname);
1384 link_path(pathname_t *name1, pathname_t *name2)
1386 char buf1[NAME_MAX + 1];
1387 char buf2[NAME_MAX + 1];
1389 pathname_t newname1;
1390 pathname_t newname2;
1393 rval = link(name1->path, name2->path);
1394 if (rval >= 0 || errno != ENAMETOOLONG)
1396 separate_pathname(name1, buf1, &newname1);
1397 separate_pathname(name2, buf2, &newname2);
1398 if (strcmp(buf1, buf2) == 0) {
1399 if (chdir(buf1) == 0) {
1400 rval = link_path(&newname1, &newname2);
1401 assert(chdir("..") == 0);
1404 if (strcmp(buf1, "..") == 0)
1406 else if (strcmp(buf2, "..") == 0)
1408 else if (strlen(buf1) == 0)
1410 else if (strlen(buf2) == 0)
1413 down1 = MAX(newname1.len, 3 + name2->len) <=
1414 MAX(3 + name1->len, newname2.len);
1416 free_pathname(&newname2);
1417 append_pathname(&newname2, "../");
1418 append_pathname(&newname2, name2->path);
1419 if (chdir(buf1) == 0) {
1420 rval = link_path(&newname1, &newname2);
1421 assert(chdir("..") == 0);
1424 free_pathname(&newname1);
1425 append_pathname(&newname1, "../");
1426 append_pathname(&newname1, name1->path);
1427 if (chdir(buf2) == 0) {
1428 rval = link_path(&newname1, &newname2);
1429 assert(chdir("..") == 0);
1433 free_pathname(&newname1);
1434 free_pathname(&newname2);
1439 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1441 char buf[NAME_MAX + 1];
1445 rval = lstat64(name->path, sbuf);
1446 if (rval >= 0 || errno != ENAMETOOLONG)
1448 separate_pathname(name, buf, &newname);
1449 if (chdir(buf) == 0) {
1450 rval = lstat64_path(&newname, sbuf);
1451 assert(chdir("..") == 0);
1453 free_pathname(&newname);
1458 make_freq_table(void)
1464 for (p = ops, f = 0; p < ops_end; p++)
1466 freq_table = malloc(f * sizeof(*freq_table));
1467 freq_table_size = f;
1468 for (p = ops, i = 0; p < ops_end; p++) {
1469 for (f = 0; f < p->freq; f++, i++)
1470 freq_table[i] = p->op;
1475 mkdir_path(pathname_t *name, mode_t mode)
1477 char buf[NAME_MAX + 1];
1481 rval = mkdir(name->path, mode);
1482 if (rval >= 0 || errno != ENAMETOOLONG)
1484 separate_pathname(name, buf, &newname);
1485 if (chdir(buf) == 0) {
1486 rval = mkdir_path(&newname, mode);
1487 assert(chdir("..") == 0);
1489 free_pathname(&newname);
1494 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1496 char buf[NAME_MAX + 1];
1500 rval = mknod(name->path, mode, dev);
1501 if (rval >= 0 || errno != ENAMETOOLONG)
1503 separate_pathname(name, buf, &newname);
1504 if (chdir(buf) == 0) {
1505 rval = mknod_path(&newname, mode, dev);
1506 assert(chdir("..") == 0);
1508 free_pathname(&newname);
1513 namerandpad(int id, char *buf, int i)
1516 static int buckets[] =
1517 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1523 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1524 padmod = buckets[bucket] + 1 - i;
1527 padlen = (id ^ namerand) % padmod;
1529 memset(&buf[i], 'X', padlen);
1530 buf[i + padlen] = '\0';
1535 open_file_or_dir(pathname_t *name, int flags)
1539 fd = open_path(name, flags);
1542 if (fd == -1 && errno != EISDIR)
1544 /* Directories can not be opened in write mode nor direct mode. */
1545 flags &= ~(O_WRONLY | O_DIRECT);
1546 flags |= O_RDONLY | O_DIRECTORY;
1547 return open_path(name, flags);
1551 open_path(pathname_t *name, int oflag)
1553 char buf[NAME_MAX + 1];
1557 rval = open(name->path, oflag);
1558 if (rval >= 0 || errno != ENAMETOOLONG)
1560 separate_pathname(name, buf, &newname);
1561 if (chdir(buf) == 0) {
1562 rval = open_path(&newname, oflag);
1563 assert(chdir("..") == 0);
1565 free_pathname(&newname);
1570 opendir_path(pathname_t *name)
1572 char buf[NAME_MAX + 1];
1576 rval = opendir(name->path);
1577 if (rval || errno != ENAMETOOLONG)
1579 separate_pathname(name, buf, &newname);
1580 if (chdir(buf) == 0) {
1581 rval = opendir_path(&newname);
1582 assert(chdir("..") == 0);
1584 free_pathname(&newname);
1589 process_freq(char *arg)
1594 s = strchr(arg, '=');
1596 fprintf(stderr, "bad argument '%s'\n", arg);
1600 for (p = ops; p < ops_end; p++) {
1601 if (strcmp(arg, p->name) == 0) {
1606 fprintf(stderr, "can't find op type %s for -f\n", arg);
1611 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1613 char buf[NAME_MAX + 1];
1617 rval = readlink(name->path, lbuf, lbufsiz);
1618 if (rval >= 0 || errno != ENAMETOOLONG)
1620 separate_pathname(name, buf, &newname);
1621 if (chdir(buf) == 0) {
1622 rval = readlink_path(&newname, lbuf, lbufsiz);
1623 assert(chdir("..") == 0);
1625 free_pathname(&newname);
1630 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1632 char buf1[NAME_MAX + 1];
1633 char buf2[NAME_MAX + 1];
1635 pathname_t newname1;
1636 pathname_t newname2;
1640 rval = rename(name1->path, name2->path);
1642 rval = renameat2(AT_FDCWD, name1->path,
1643 AT_FDCWD, name2->path, mode);
1644 if (rval >= 0 || errno != ENAMETOOLONG)
1646 separate_pathname(name1, buf1, &newname1);
1647 separate_pathname(name2, buf2, &newname2);
1648 if (strcmp(buf1, buf2) == 0) {
1649 if (chdir(buf1) == 0) {
1650 rval = rename_path(&newname1, &newname2, mode);
1651 assert(chdir("..") == 0);
1654 if (strcmp(buf1, "..") == 0)
1656 else if (strcmp(buf2, "..") == 0)
1658 else if (strlen(buf1) == 0)
1660 else if (strlen(buf2) == 0)
1663 down1 = MAX(newname1.len, 3 + name2->len) <=
1664 MAX(3 + name1->len, newname2.len);
1666 free_pathname(&newname2);
1667 append_pathname(&newname2, "../");
1668 append_pathname(&newname2, name2->path);
1669 if (chdir(buf1) == 0) {
1670 rval = rename_path(&newname1, &newname2, mode);
1671 assert(chdir("..") == 0);
1674 free_pathname(&newname1);
1675 append_pathname(&newname1, "../");
1676 append_pathname(&newname1, name1->path);
1677 if (chdir(buf2) == 0) {
1678 rval = rename_path(&newname1, &newname2, mode);
1679 assert(chdir("..") == 0);
1683 free_pathname(&newname1);
1684 free_pathname(&newname2);
1689 rmdir_path(pathname_t *name)
1691 char buf[NAME_MAX + 1];
1695 rval = rmdir(name->path);
1696 if (rval >= 0 || errno != ENAMETOOLONG)
1698 separate_pathname(name, buf, &newname);
1699 if (chdir(buf) == 0) {
1700 rval = rmdir_path(&newname);
1701 assert(chdir("..") == 0);
1703 free_pathname(&newname);
1708 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1712 init_pathname(newname);
1713 slash = strchr(name->path, '/');
1714 if (slash == NULL) {
1719 strcpy(buf, name->path);
1721 append_pathname(newname, slash + 1);
1727 show_ops(int flag, char *lead_str)
1732 /* print in list form */
1735 for (p = ops; p < ops_end; p++) {
1736 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1737 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1738 x+=printf("%s ", p->name);
1741 } else if (flag == 0) {
1742 /* Table view style */
1744 for (f = 0, p = ops; p < ops_end; p++)
1750 for (p = ops; p < ops_end; p++) {
1751 if (flag != 0 || p->freq > 0) {
1752 if (lead_str != NULL)
1753 printf("%s", lead_str);
1754 printf("%20s %d/%d %s\n",
1755 p->name, p->freq, f,
1756 (p->iswrite == 0) ? " " : "write op");
1760 /* Command line style */
1761 if (lead_str != NULL)
1762 printf("%s", lead_str);
1763 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1765 for (p = ops; p < ops_end; p++)
1767 printf("-f %s=%d \\\n",p->name, p->freq);
1772 stat64_path(pathname_t *name, struct stat64 *sbuf)
1774 char buf[NAME_MAX + 1];
1778 rval = stat64(name->path, sbuf);
1779 if (rval >= 0 || errno != ENAMETOOLONG)
1781 separate_pathname(name, buf, &newname);
1782 if (chdir(buf) == 0) {
1783 rval = stat64_path(&newname, sbuf);
1784 assert(chdir("..") == 0);
1786 free_pathname(&newname);
1791 symlink_path(const char *name1, pathname_t *name)
1793 char buf[NAME_MAX + 1];
1797 if (!strcmp(name1, name->path)) {
1798 printf("yikes! %s %s\n", name1, name->path);
1802 rval = symlink(name1, name->path);
1803 if (rval >= 0 || errno != ENAMETOOLONG)
1805 separate_pathname(name, buf, &newname);
1806 if (chdir(buf) == 0) {
1807 rval = symlink_path(name1, &newname);
1808 assert(chdir("..") == 0);
1810 free_pathname(&newname);
1815 truncate64_path(pathname_t *name, off64_t length)
1817 char buf[NAME_MAX + 1];
1821 rval = truncate64(name->path, length);
1822 if (rval >= 0 || errno != ENAMETOOLONG)
1824 separate_pathname(name, buf, &newname);
1825 if (chdir(buf) == 0) {
1826 rval = truncate64_path(&newname, length);
1827 assert(chdir("..") == 0);
1829 free_pathname(&newname);
1834 unlink_path(pathname_t *name)
1836 char buf[NAME_MAX + 1];
1840 rval = unlink(name->path);
1841 if (rval >= 0 || errno != ENAMETOOLONG)
1843 separate_pathname(name, buf, &newname);
1844 if (chdir(buf) == 0) {
1845 rval = unlink_path(&newname);
1846 assert(chdir("..") == 0);
1848 free_pathname(&newname);
1855 printf("Usage: %s -H or\n", myprog);
1856 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1858 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1860 printf(" -c clean up the test directory after each run\n");
1861 printf(" -d dir specifies the base directory for operations\n");
1862 printf(" -e errtg specifies error injection stuff\n");
1863 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1864 printf(" the valid operation names are:\n");
1866 printf(" -i filenum get verbose output for this nth file object\n");
1867 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1868 printf(" *use 0 for infinite (default 1)\n");
1869 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1870 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1871 printf(" -o logfile specifies logfile name\n");
1872 printf(" -p nproc specifies the no. of processes (default 1)\n");
1873 printf(" -r specifies random name padding\n");
1874 printf(" -s seed specifies the seed for the random generator (default random)\n");
1875 printf(" -v specifies verbose mode\n");
1876 printf(" -w zeros frequencies of non-write operations\n");
1877 printf(" -x cmd execute command in the middle of operations\n");
1878 printf(" -z zeros frequencies of all operations\n");
1879 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1880 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1881 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1882 printf(" -H prints usage and exits\n");
1890 for (p = ops; p < ops_end; p++) {
1901 for (p = ops; p < ops_end; p++)
1905 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1907 opty_t btrfs_ops[] = {
1914 non_btrfs_freq(const char *path)
1917 #ifdef HAVE_BTRFSUTIL_H
1918 enum btrfs_util_error e;
1920 e = btrfs_util_is_subvolume(path);
1921 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1924 for (p = ops; p < ops_end; p++) {
1926 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1927 if (p->op == btrfs_ops[i]) {
1935 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1938 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1939 verifiable_log ? -1: (long)s->st_ino,
1940 (long)s->st_nlink, s->st_uid, s->st_gid,
1941 (long long) s->st_blocks, (long long) s->st_size);
1945 afsync_f(int opno, long r)
1953 struct iocb *iocbs[] = { &iocb };
1954 struct io_event event;
1957 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1959 printf("%d/%d: afsync - no filename\n", procid, opno);
1963 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1964 e = fd < 0 ? errno : 0;
1968 printf("%d/%d: afsync - open %s failed %d\n",
1969 procid, opno, f.path, e);
1974 io_prep_fsync(&iocb, fd);
1975 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1977 printf("%d/%d: afsync - io_submit %s %d\n",
1978 procid, opno, f.path, e);
1983 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1985 printf("%d/%d: afsync - io_getevents failed %d\n",
1994 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
2001 allocsp_f(int opno, long r)
2006 struct xfs_flock64 fl;
2014 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2016 printf("%d/%d: allocsp - no filename\n", procid, opno);
2020 fd = open_path(&f, O_RDWR);
2021 e = fd < 0 ? errno : 0;
2025 printf("%d/%d: allocsp - open %s failed %d\n",
2026 procid, opno, f.path, e);
2030 if (fstat64(fd, &stb) < 0) {
2032 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2033 procid, opno, f.path, errno);
2038 inode_info(st, sizeof(st), &stb, v);
2039 lr = ((int64_t)random() << 32) + random();
2040 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2042 fl.l_whence = SEEK_SET;
2045 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2047 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2048 procid, opno, f.path, st, (long long)off, e);
2056 do_aio_rw(int opno, long r, int flags)
2060 struct dioattr diob;
2072 struct io_event event;
2073 struct iocb *iocbs[] = { &iocb };
2074 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2077 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2079 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2083 fd = open_path(&f, flags|O_DIRECT);
2084 e = fd < 0 ? errno : 0;
2088 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2089 procid, opno, f.path, e);
2093 if (fstat64(fd, &stb) < 0) {
2095 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2096 procid, opno, f.path, errno);
2101 inode_info(st, sizeof(st), &stb, v);
2102 if (!iswrite && stb.st_size == 0) {
2104 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2110 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2113 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2114 " fallback to stat()\n",
2115 procid, opno, f.path, st, errno);
2116 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2117 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2119 dio_env = getenv("XFS_DIO_MIN");
2121 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2122 align = (int64_t)diob.d_miniosz;
2123 lr = ((int64_t)random() << 32) + random();
2124 len = (random() % FILELEN_MAX) + 1;
2125 len -= (len % align);
2128 else if (len > diob.d_maxiosz)
2129 len = diob.d_maxiosz;
2130 buf = memalign(diob.d_mem, len);
2133 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2134 off -= (off % align);
2136 memset(buf, nameseq & 0xff, len);
2137 io_prep_pwrite(&iocb, fd, buf, len, off);
2139 off = (off64_t)(lr % stb.st_size);
2140 off -= (off % align);
2141 io_prep_pread(&iocb, fd, buf, len, off);
2143 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2145 printf("%d/%d: %s - io_submit failed %d\n",
2146 procid, opno, iswrite ? "awrite" : "aread", e);
2151 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2153 printf("%d/%d: %s - io_getevents failed %d\n",
2154 procid, opno, iswrite ? "awrite" : "aread", e);
2160 e = event.res != len ? event.res2 : 0;
2163 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2164 procid, opno, iswrite ? "awrite" : "aread",
2165 f.path, st, (long long)off, (int)len, e);
2172 aread_f(int opno, long r)
2175 do_aio_rw(opno, r, O_RDONLY);
2180 attr_remove_f(int opno, long r)
2182 attrlist_ent_t *aep;
2186 attrlist_cursor_t cursor;
2195 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2196 append_pathname(&f, ".");
2198 bzero(&cursor, sizeof(cursor));
2200 bzero(buf, sizeof(buf));
2201 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2205 alist = (attrlist_t *)buf;
2206 total += alist->al_count;
2207 } while (alist->al_more);
2210 printf("%d/%d: attr_remove - no attrs for %s\n",
2211 procid, opno, f.path);
2215 which = (int)(random() % total);
2216 bzero(&cursor, sizeof(cursor));
2220 bzero(buf, sizeof(buf));
2221 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2225 alist = (attrlist_t *)buf;
2226 if (which < ent + alist->al_count) {
2227 aep = (attrlist_ent_t *)
2228 &buf[alist->al_offset[which - ent]];
2229 aname = aep->a_name;
2232 ent += alist->al_count;
2233 } while (alist->al_more);
2234 if (aname == NULL) {
2237 "%d/%d: attr_remove - name %d not found at %s\n",
2238 procid, opno, which, f.path);
2242 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2245 printf("%d/%d: attr_remove %s %s %d\n",
2246 procid, opno, f.path, aname, e);
2251 attr_set_f(int opno, long r)
2258 static int lengths[] = { 10, 100, 1000, 10000 };
2263 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2264 append_pathname(&f, ".");
2265 sprintf(aname, "a%x", nameseq++);
2266 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2267 len = (int)(random() % lengths[li]);
2271 memset(aval, nameseq & 0xff, len);
2272 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2277 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2283 awrite_f(int opno, long r)
2286 do_aio_rw(opno, r, O_WRONLY);
2291 bulkstat_f(int opno, long r)
2297 struct xfs_bstat *t;
2299 struct xfs_fsop_bulkreq bsr;
2302 nent = (r % 999) + 2;
2303 t = malloc(nent * sizeof(*t));
2304 fd = open(".", O_RDONLY);
2312 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2316 printf("%d/%d: bulkstat nent %d total %lld\n",
2317 procid, opno, nent, (long long)total);
2322 bulkstat1_f(int opno, long r)
2332 struct xfs_fsop_bulkreq bsr;
2335 good = random() & 1;
2337 /* use an inode we know exists */
2339 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2340 append_pathname(&f, ".");
2341 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2346 * pick a random inode
2348 * note this can generate kernel warning messages
2349 * since bulkstat_one will read the disk block that
2350 * would contain a given inode even if that disk
2351 * block doesn't contain inodes.
2353 * this is detected later, but not until after the
2354 * warning is displayed.
2356 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2362 fd = open(".", O_RDONLY);
2368 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2370 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2371 procid, opno, good?"real":"random",
2372 verifiable_log ? -1LL : (long long)ino, e);
2377 chown_f(int opno, long r)
2387 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2388 append_pathname(&f, ".");
2389 u = (uid_t)random();
2390 g = (gid_t)random();
2391 nbits = (int)(random() % idmodulo);
2392 u &= (1 << nbits) - 1;
2393 g &= (1 << nbits) - 1;
2394 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2397 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2401 /* reflink some arbitrary range of f1 to f2. */
2408 struct file_clone_range fcr;
2409 struct pathname fpath1;
2410 struct pathname fpath2;
2411 struct stat64 stat1;
2412 struct stat64 stat2;
2413 char inoinfo1[1024];
2414 char inoinfo2[1024];
2428 init_pathname(&fpath1);
2429 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2431 printf("%d/%d: clonerange read - no filename\n",
2436 init_pathname(&fpath2);
2437 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2439 printf("%d/%d: clonerange write - no filename\n",
2445 fd1 = open_path(&fpath1, O_RDONLY);
2446 e = fd1 < 0 ? errno : 0;
2450 printf("%d/%d: clonerange read - open %s failed %d\n",
2451 procid, opno, fpath1.path, e);
2455 fd2 = open_path(&fpath2, O_WRONLY);
2456 e = fd2 < 0 ? errno : 0;
2460 printf("%d/%d: clonerange write - open %s failed %d\n",
2461 procid, opno, fpath2.path, e);
2465 /* Get file stats */
2466 if (fstat64(fd1, &stat1) < 0) {
2468 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2469 procid, opno, fpath1.path, errno);
2472 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2474 if (fstat64(fd2, &stat2) < 0) {
2476 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2477 procid, opno, fpath2.path, errno);
2480 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2482 /* Calculate offsets */
2483 len = (random() % FILELEN_MAX) + 1;
2484 len &= ~(stat1.st_blksize - 1);
2486 len = stat1.st_blksize;
2487 if (len > stat1.st_size)
2488 len = stat1.st_size;
2490 lr = ((int64_t)random() << 32) + random();
2491 if (stat1.st_size == len)
2494 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2496 off1 &= ~(stat1.st_blksize - 1);
2499 * If srcfile == destfile, randomly generate destination ranges
2500 * until we find one that doesn't overlap the source range.
2502 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2504 lr = ((int64_t)random() << 32) + random();
2505 off2 = (off64_t)(lr % max_off2);
2507 off2 &= ~(stat2.st_blksize - 1);
2508 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2510 /* Clone data blocks */
2512 fcr.src_offset = off1;
2513 fcr.src_length = len;
2514 fcr.dest_offset = off2;
2516 ret = ioctl(fd2, FICLONERANGE, &fcr);
2517 e = ret < 0 ? errno : 0;
2519 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2521 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2522 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2525 printf(" error %d", e);
2534 free_pathname(&fpath2);
2536 free_pathname(&fpath1);
2540 /* copy some arbitrary range of f1 to f2. */
2546 #ifdef HAVE_COPY_FILE_RANGE
2547 struct pathname fpath1;
2548 struct pathname fpath2;
2549 struct stat64 stat1;
2550 struct stat64 stat2;
2551 char inoinfo1[1024];
2552 char inoinfo2[1024];
2570 init_pathname(&fpath1);
2571 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2573 printf("%d/%d: copyrange read - no filename\n",
2578 init_pathname(&fpath2);
2579 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2581 printf("%d/%d: copyrange write - no filename\n",
2587 fd1 = open_path(&fpath1, O_RDONLY);
2588 e = fd1 < 0 ? errno : 0;
2592 printf("%d/%d: copyrange read - open %s failed %d\n",
2593 procid, opno, fpath1.path, e);
2597 fd2 = open_path(&fpath2, O_WRONLY);
2598 e = fd2 < 0 ? errno : 0;
2602 printf("%d/%d: copyrange write - open %s failed %d\n",
2603 procid, opno, fpath2.path, e);
2607 /* Get file stats */
2608 if (fstat64(fd1, &stat1) < 0) {
2610 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2611 procid, opno, fpath1.path, errno);
2614 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2616 if (fstat64(fd2, &stat2) < 0) {
2618 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2619 procid, opno, fpath2.path, errno);
2622 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2624 /* Calculate offsets */
2625 len = (random() % FILELEN_MAX) + 1;
2627 len = stat1.st_blksize;
2628 if (len > stat1.st_size)
2629 len = stat1.st_size;
2631 lr = ((int64_t)random() << 32) + random();
2632 if (stat1.st_size == len)
2635 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2639 * If srcfile == destfile, randomly generate destination ranges
2640 * until we find one that doesn't overlap the source range.
2642 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2644 lr = ((int64_t)random() << 32) + random();
2645 off2 = (off64_t)(lr % max_off2);
2647 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2650 * Since len, off1 and off2 will be changed later, preserve their
2658 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2661 if (errno != EAGAIN || tries++ >= 300)
2663 } else if (ret > len || ret == 0)
2668 e = ret < 0 ? errno : 0;
2670 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2672 fpath1.path, inoinfo1,
2673 (long long)offset1, (long long)length,
2674 fpath2.path, inoinfo2,
2675 (long long)offset2, (long long)length);
2678 printf(" error %d", e);
2679 else if (len && ret > len)
2680 printf(" asked for %lld, copied %lld??\n",
2681 (long long)len, (long long)ret);
2690 free_pathname(&fpath2);
2692 free_pathname(&fpath1);
2696 /* dedupe some arbitrary range of f1 to f2...fn. */
2702 #ifdef FIDEDUPERANGE
2703 #define INFO_SZ 1024
2704 struct file_dedupe_range *fdr;
2705 struct pathname *fpath;
2706 struct stat64 *stat;
2718 if (flist[FT_REG].nfiles < 2)
2721 /* Pick somewhere between 2 and 128 files. */
2723 nr = random() % (flist[FT_REG].nfiles + 1);
2724 } while (nr < 2 || nr > 128);
2727 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2728 sizeof(struct file_dedupe_range));
2730 printf("%d/%d: line %d error %d\n",
2731 procid, opno, __LINE__, errno);
2734 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2735 sizeof(struct file_dedupe_range)));
2737 fpath = calloc(nr, sizeof(struct pathname));
2739 printf("%d/%d: line %d error %d\n",
2740 procid, opno, __LINE__, errno);
2744 stat = calloc(nr, sizeof(struct stat64));
2746 printf("%d/%d: line %d error %d\n",
2747 procid, opno, __LINE__, errno);
2751 info = calloc(nr, INFO_SZ);
2753 printf("%d/%d: line %d error %d\n",
2754 procid, opno, __LINE__, errno);
2758 off = calloc(nr, sizeof(off64_t));
2760 printf("%d/%d: line %d error %d\n",
2761 procid, opno, __LINE__, errno);
2765 v = calloc(nr, sizeof(int));
2767 printf("%d/%d: line %d error %d\n",
2768 procid, opno, __LINE__, errno);
2771 fd = calloc(nr, sizeof(int));
2773 printf("%d/%d: line %d error %d\n",
2774 procid, opno, __LINE__, errno);
2777 memset(fd, 0xFF, nr * sizeof(int));
2779 /* Get paths for all files */
2780 for (i = 0; i < nr; i++)
2781 init_pathname(&fpath[i]);
2783 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2785 printf("%d/%d: deduperange read - no filename\n",
2790 for (i = 1; i < nr; i++) {
2791 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2793 printf("%d/%d: deduperange write - no filename\n",
2800 fd[0] = open_path(&fpath[0], O_RDONLY);
2801 e = fd[0] < 0 ? errno : 0;
2805 printf("%d/%d: deduperange read - open %s failed %d\n",
2806 procid, opno, fpath[0].path, e);
2810 for (i = 1; i < nr; i++) {
2811 fd[i] = open_path(&fpath[i], O_WRONLY);
2812 e = fd[i] < 0 ? errno : 0;
2816 printf("%d/%d: deduperange write - open %s failed %d\n",
2817 procid, opno, fpath[i].path, e);
2822 /* Get file stats */
2823 if (fstat64(fd[0], &stat[0]) < 0) {
2825 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2826 procid, opno, fpath[0].path, errno);
2830 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2832 for (i = 1; i < nr; i++) {
2833 if (fstat64(fd[i], &stat[i]) < 0) {
2835 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2836 procid, opno, fpath[i].path, errno);
2839 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2842 /* Never try to dedupe more than half of the src file. */
2843 len = (random() % FILELEN_MAX) + 1;
2844 len &= ~(stat[0].st_blksize - 1);
2846 len = stat[0].st_blksize / 2;
2847 if (len > stat[0].st_size / 2)
2848 len = stat[0].st_size / 2;
2850 /* Calculate offsets */
2851 lr = ((int64_t)random() << 32) + random();
2852 if (stat[0].st_size == len)
2855 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2857 off[0] &= ~(stat[0].st_blksize - 1);
2860 * If srcfile == destfile[i], randomly generate destination ranges
2861 * until we find one that doesn't overlap the source range.
2863 for (i = 1; i < nr; i++) {
2867 lr = ((int64_t)random() << 32) + random();
2868 if (stat[i].st_size <= len)
2871 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2873 off[i] &= ~(stat[i].st_blksize - 1);
2874 } while (stat[0].st_ino == stat[i].st_ino &&
2875 llabs(off[i] - off[0]) < len &&
2879 /* Clone data blocks */
2880 fdr->src_offset = off[0];
2881 fdr->src_length = len;
2882 fdr->dest_count = nr - 1;
2883 for (i = 1; i < nr; i++) {
2884 fdr->info[i - 1].dest_fd = fd[i];
2885 fdr->info[i - 1].dest_offset = off[i];
2888 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2889 e = ret < 0 ? errno : 0;
2891 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2893 fpath[0].path, &info[0], (long long)off[0],
2896 printf(" error %d", e);
2902 for (i = 1; i < nr; i++) {
2903 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2905 printf("%d/%d: ...to %s%s [%lld,%lld]",
2907 fpath[i].path, &info[i * INFO_SZ],
2908 (long long)off[i], (long long)len);
2909 if (fdr->info[i - 1].status < 0)
2910 printf(" error %d", e);
2911 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2912 printf(" %llu bytes deduplicated",
2913 fdr->info[i - 1].bytes_deduped);
2914 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2915 printf(" differed");
2921 for (i = 0; i < nr; i++)
2925 for (i = 0; i < nr; i++)
2926 free_pathname(&fpath[i]);
2945 setxattr_f(int opno, long r)
2947 #ifdef XFS_XFLAG_EXTSIZE
2957 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2958 append_pathname(&f, ".");
2959 fd = open_path(&f, O_RDWR);
2960 e = fd < 0 ? errno : 0;
2965 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2966 nbits = (int)(random() % e);
2967 p &= (1 << nbits) - 1;
2969 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2971 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2974 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2981 splice_f(int opno, long r)
2983 struct pathname fpath1;
2984 struct pathname fpath2;
2985 struct stat64 stat1;
2986 struct stat64 stat2;
2987 char inoinfo1[1024];
2988 char inoinfo2[1024];
2992 loff_t offset1, offset2;
2999 ssize_t ret1 = 0, ret2 = 0;
3005 init_pathname(&fpath1);
3006 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3008 printf("%d/%d: splice read - no filename\n",
3013 init_pathname(&fpath2);
3014 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3016 printf("%d/%d: splice write - no filename\n",
3022 fd1 = open_path(&fpath1, O_RDONLY);
3023 e = fd1 < 0 ? errno : 0;
3027 printf("%d/%d: splice read - open %s failed %d\n",
3028 procid, opno, fpath1.path, e);
3032 fd2 = open_path(&fpath2, O_WRONLY);
3033 e = fd2 < 0 ? errno : 0;
3037 printf("%d/%d: splice write - open %s failed %d\n",
3038 procid, opno, fpath2.path, e);
3042 /* Get file stats */
3043 if (fstat64(fd1, &stat1) < 0) {
3045 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3046 procid, opno, fpath1.path, errno);
3049 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3051 if (fstat64(fd2, &stat2) < 0) {
3053 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3054 procid, opno, fpath2.path, errno);
3057 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3059 /* Calculate offsets */
3060 len = (random() % FILELEN_MAX) + 1;
3062 len = stat1.st_blksize;
3063 if (len > stat1.st_size)
3064 len = stat1.st_size;
3066 lr = ((int64_t)random() << 32) + random();
3067 if (stat1.st_size == len)
3070 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3074 * splice can overlap write, so the offset of the target file can be
3075 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3076 * past the current dest file EOF
3078 lr = ((int64_t)random() << 32) + random();
3079 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3082 * Since len, off1 and off2 will be changed later, preserve their
3089 /* Pipe initialize */
3090 if (pipe(filedes) < 0) {
3092 printf("%d/%d: splice - pipe failed %d\n",
3093 procid, opno, errno);
3101 /* move to pipe buffer */
3102 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3108 /* move from pipe buffer to dst file */
3110 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3123 if (ret1 < 0 || ret2 < 0)
3128 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3130 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3131 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3133 if (length && length > total)
3134 printf(" asked for %lld, spliced %lld??\n",
3135 (long long)length, (long long)total);
3146 free_pathname(&fpath2);
3148 free_pathname(&fpath1);
3152 creat_f(int opno, long r)
3167 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
3172 e1 = (random() % 100);
3173 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3175 if (type == FT_RTF) /* rt always gets an extsize */
3176 extsize = (random() % 10) + 1;
3177 else if (e1 < 10) /* one-in-ten get an extsize */
3178 extsize = random() % 1024;
3182 e = generate_fname(fep, type, &f, &id, &v);
3186 (void)fent_to_name(&f, fep);
3187 printf("%d/%d: creat - no filename from %s\n",
3188 procid, opno, f.path);
3193 fd = creat_path(&f, 0666);
3194 e = fd < 0 ? errno : 0;
3199 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3200 if (type == FT_RTF) {
3201 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3202 a.fsx_extsize = extsize *
3203 geom.rtextsize * geom.blocksize;
3205 } else if (extsize) {
3206 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3207 a.fsx_extsize = extsize * geom.blocksize;
3210 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3213 add_to_flist(type, id, parid, 0);
3217 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3218 extsize ? a.fsx_extsize : 0, e, e1);
3219 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3225 dread_f(int opno, long r)
3229 struct dioattr diob;
3242 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3244 printf("%d/%d: dread - no filename\n", procid, opno);
3248 fd = open_path(&f, O_RDONLY|O_DIRECT);
3249 e = fd < 0 ? errno : 0;
3253 printf("%d/%d: dread - open %s failed %d\n",
3254 procid, opno, f.path, e);
3258 if (fstat64(fd, &stb) < 0) {
3260 printf("%d/%d: dread - fstat64 %s failed %d\n",
3261 procid, opno, f.path, errno);
3266 inode_info(st, sizeof(st), &stb, v);
3267 if (stb.st_size == 0) {
3269 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3275 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3278 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3279 " fallback to stat()\n",
3280 procid, opno, f.path, st, errno);
3281 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3282 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3285 dio_env = getenv("XFS_DIO_MIN");
3287 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3289 align = (int64_t)diob.d_miniosz;
3290 lr = ((int64_t)random() << 32) + random();
3291 off = (off64_t)(lr % stb.st_size);
3292 off -= (off % align);
3293 lseek64(fd, off, SEEK_SET);
3294 len = (random() % FILELEN_MAX) + 1;
3295 len -= (len % align);
3298 else if (len > diob.d_maxiosz)
3299 len = diob.d_maxiosz;
3300 buf = memalign(diob.d_mem, len);
3301 e = read(fd, buf, len) < 0 ? errno : 0;
3304 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3305 procid, opno, f.path, st, (long long)off, (int)len, e);
3311 dwrite_f(int opno, long r)
3315 struct dioattr diob;
3328 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3330 printf("%d/%d: dwrite - no filename\n", procid, opno);
3334 fd = open_path(&f, O_WRONLY|O_DIRECT);
3335 e = fd < 0 ? errno : 0;
3339 printf("%d/%d: dwrite - open %s failed %d\n",
3340 procid, opno, f.path, e);
3344 if (fstat64(fd, &stb) < 0) {
3346 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3347 procid, opno, f.path, errno);
3352 inode_info(st, sizeof(st), &stb, v);
3353 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3355 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3356 " %s%s return %d, fallback to stat()\n",
3357 procid, opno, f.path, st, errno);
3358 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3359 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3362 dio_env = getenv("XFS_DIO_MIN");
3364 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3366 align = (int64_t)diob.d_miniosz;
3367 lr = ((int64_t)random() << 32) + random();
3368 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3369 off -= (off % align);
3370 lseek64(fd, off, SEEK_SET);
3371 len = (random() % FILELEN_MAX) + 1;
3372 len -= (len % align);
3375 else if (len > diob.d_maxiosz)
3376 len = diob.d_maxiosz;
3377 buf = memalign(diob.d_mem, len);
3379 lseek64(fd, off, SEEK_SET);
3380 memset(buf, nameseq & 0xff, len);
3381 e = write(fd, buf, len) < 0 ? errno : 0;
3384 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3385 procid, opno, f.path, st, (long long)off, (int)len, e);
3391 #ifdef HAVE_LINUX_FALLOC_H
3392 struct print_flags falloc_flags [] = {
3393 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3394 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3395 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3396 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3397 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3398 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3402 #define translate_falloc_flags(mode) \
3403 ({translate_flags(mode, "|", falloc_flags);})
3407 do_fallocate(int opno, long r, int mode)
3409 #ifdef HAVE_LINUX_FALLOC_H
3421 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3423 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3427 fd = open_path(&f, O_RDWR);
3430 printf("%d/%d: do_fallocate - open %s failed %d\n",
3431 procid, opno, f.path, errno);
3436 if (fstat64(fd, &stb) < 0) {
3438 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3439 procid, opno, f.path, errno);
3444 inode_info(st, sizeof(st), &stb, v);
3445 lr = ((int64_t)random() << 32) + random();
3446 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3448 len = (off64_t)(random() % (1024 * 1024));
3450 * Collapse/insert range requires off and len to be block aligned,
3451 * make it more likely to be the case.
3453 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3455 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3456 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3458 mode |= FALLOC_FL_KEEP_SIZE & random();
3459 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3461 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3462 procid, opno, translate_falloc_flags(mode),
3463 f.path, st, (long long)off, (long long)len, e);
3470 fallocate_f(int opno, long r)
3472 #ifdef HAVE_LINUX_FALLOC_H
3473 do_fallocate(opno, r, 0);
3478 fdatasync_f(int opno, long r)
3486 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3488 printf("%d/%d: fdatasync - no filename\n",
3493 fd = open_path(&f, O_WRONLY);
3494 e = fd < 0 ? errno : 0;
3498 printf("%d/%d: fdatasync - open %s failed %d\n",
3499 procid, opno, f.path, e);
3503 e = fdatasync(fd) < 0 ? errno : 0;
3505 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3510 #ifdef HAVE_LINUX_FIEMAP_H
3511 struct print_flags fiemap_flags[] = {
3512 { FIEMAP_FLAG_SYNC, "SYNC"},
3513 { FIEMAP_FLAG_XATTR, "XATTR"},
3517 #define translate_fiemap_flags(mode) \
3518 ({translate_flags(mode, "|", fiemap_flags);})
3522 fiemap_f(int opno, long r)
3524 #ifdef HAVE_LINUX_FIEMAP_H
3534 struct fiemap *fiemap;
3537 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3539 printf("%d/%d: fiemap - no filename\n", procid, opno);
3543 fd = open_path(&f, O_RDWR);
3544 e = fd < 0 ? errno : 0;
3548 printf("%d/%d: fiemap - open %s failed %d\n",
3549 procid, opno, f.path, e);
3553 if (fstat64(fd, &stb) < 0) {
3555 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3556 procid, opno, f.path, errno);
3561 inode_info(st, sizeof(st), &stb, v);
3562 blocks_to_map = random() & 0xffff;
3563 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3564 (blocks_to_map * sizeof(struct fiemap_extent)));
3567 printf("%d/%d: malloc failed \n", procid, opno);
3572 lr = ((int64_t)random() << 32) + random();
3573 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3575 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3576 fiemap->fm_extent_count = blocks_to_map;
3577 fiemap->fm_mapped_extents = random() & 0xffff;
3578 fiemap->fm_start = off;
3579 fiemap->fm_length = ((int64_t)random() << 32) + random();
3581 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3583 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3584 procid, opno, f.path, st, (long long)fiemap->fm_start,
3585 (long long) fiemap->fm_length,
3586 translate_fiemap_flags(fiemap->fm_flags), e);
3594 freesp_f(int opno, long r)
3599 struct xfs_flock64 fl;
3607 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3609 printf("%d/%d: freesp - no filename\n", procid, opno);
3613 fd = open_path(&f, O_RDWR);
3614 e = fd < 0 ? errno : 0;
3618 printf("%d/%d: freesp - open %s failed %d\n",
3619 procid, opno, f.path, e);
3623 if (fstat64(fd, &stb) < 0) {
3625 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3626 procid, opno, f.path, errno);
3631 inode_info(st, sizeof(st), &stb, v);
3632 lr = ((int64_t)random() << 32) + random();
3633 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3635 fl.l_whence = SEEK_SET;
3638 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3640 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3641 procid, opno, f.path, st, (long long)off, e);
3647 fsync_f(int opno, long r)
3655 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3657 printf("%d/%d: fsync - no filename\n", procid, opno);
3661 fd = open_file_or_dir(&f, O_WRONLY);
3662 e = fd < 0 ? errno : 0;
3666 printf("%d/%d: fsync - open %s failed %d\n",
3667 procid, opno, f.path, e);
3671 e = fsync(fd) < 0 ? errno : 0;
3673 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3679 gen_random_string(int len)
3681 static const char charset[] = "0123456789"
3682 "abcdefghijklmnopqrstuvwxyz"
3683 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3694 for (i = 0; i < len; i++)
3695 s[i] = charset[random() % sizeof(charset)];
3701 getattr_f(int opno, long r)
3710 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3711 append_pathname(&f, ".");
3712 fd = open_path(&f, O_RDWR);
3713 e = fd < 0 ? errno : 0;
3716 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3718 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3724 getdents_f(int opno, long r)
3731 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3732 append_pathname(&f, ".");
3733 dir = opendir_path(&f);
3737 printf("%d/%d: getdents - can't open %s\n",
3738 procid, opno, f.path);
3742 while (readdir64(dir) != NULL)
3745 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3751 getfattr_f(int opno, long r)
3757 char name[XATTR_NAME_BUF_SIZE];
3763 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3765 printf("%d/%d: getfattr - no filename\n", procid, opno);
3771 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3772 * to read a xattr that doesn't exist (fgetxattr should fail with
3773 * errno set to ENOATTR (61) in this case).
3775 if (fep->xattr_counter > 0)
3776 xattr_num = (random() % fep->xattr_counter) + 1;
3780 e = generate_xattr_name(xattr_num, name, sizeof(name));
3782 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3783 procid, opno, f.path, e);
3787 value_len = getxattr(f.path, name, NULL, 0);
3788 if (value_len < 0) {
3790 printf("%d/%d: getfattr file %s name %s failed %d\n",
3791 procid, opno, f.path, name, errno);
3795 /* A xattr without value.*/
3796 if (value_len == 0) {
3801 value = malloc(value_len);
3804 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3805 procid, opno, f.path, value_len);
3809 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3812 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3813 procid, opno, f.path, name, value_len, e);
3820 link_f(int opno, long r)
3834 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3836 printf("%d/%d: link - no file\n", procid, opno);
3840 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3846 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3850 (void)fent_to_name(&l, fep);
3851 printf("%d/%d: link - no filename from %s\n",
3852 procid, opno, l.path);
3858 e = link_path(&f, &l) < 0 ? errno : 0;
3861 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3863 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3865 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3872 listfattr_f(int opno, long r)
3878 char *buffer = NULL;
3882 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3884 printf("%d/%d: listfattr - no filename\n", procid, opno);
3889 e = listxattr(f.path, NULL, 0);
3892 printf("%d/%d: listfattr %s failed %d\n",
3893 procid, opno, f.path, errno);
3897 if (buffer_len == 0) {
3899 printf("%d/%d: listfattr %s - has no extended attributes\n",
3900 procid, opno, f.path);
3904 buffer = malloc(buffer_len);
3907 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3908 procid, opno, f.path, buffer_len);
3912 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3914 printf("%d/%d: listfattr %s buffer length %d %d\n",
3915 procid, opno, f.path, buffer_len, e);
3922 mkdir_f(int opno, long r)
3932 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3937 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3941 (void)fent_to_name(&f, fep);
3942 printf("%d/%d: mkdir - no filename from %s\n",
3943 procid, opno, f.path);
3948 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3951 add_to_flist(FT_DIR, id, parid, 0);
3953 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3954 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3960 mknod_f(int opno, long r)
3970 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3975 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3979 (void)fent_to_name(&f, fep);
3980 printf("%d/%d: mknod - no filename from %s\n",
3981 procid, opno, f.path);
3986 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3989 add_to_flist(FT_DEV, id, parid, 0);
3991 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3992 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3997 #ifdef HAVE_SYS_MMAN_H
3998 struct print_flags mmap_flags[] = {
3999 { MAP_SHARED, "SHARED"},
4000 { MAP_PRIVATE, "PRIVATE"},
4004 #define translate_mmap_flags(flags) \
4005 ({translate_flags(flags, "|", mmap_flags);})
4009 do_mmap(int opno, long r, int prot)
4011 #ifdef HAVE_SYS_MMAN_H
4023 sigjmp_buf sigbus_jmpbuf;
4026 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4028 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4032 fd = open_path(&f, O_RDWR);
4033 e = fd < 0 ? errno : 0;
4037 printf("%d/%d: do_mmap - open %s failed %d\n",
4038 procid, opno, f.path, e);
4042 if (fstat64(fd, &stb) < 0) {
4044 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4045 procid, opno, f.path, errno);
4050 inode_info(st, sizeof(st), &stb, v);
4051 if (stb.st_size == 0) {
4053 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4060 lr = ((int64_t)random() << 32) + random();
4061 off = (off64_t)(lr % stb.st_size);
4062 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4063 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4065 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4066 addr = mmap(NULL, len, prot, flags, fd, off);
4067 e = (addr == MAP_FAILED) ? errno : 0;
4070 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4071 procid, opno, f.path, st, (long long)off,
4072 (int)len, translate_mmap_flags(flags), e);
4078 if (prot & PROT_WRITE) {
4079 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4080 sigbus_jmp = &sigbus_jmpbuf;
4081 memset(addr, nameseq & 0xff, len);
4085 if ((buf = malloc(len)) != NULL) {
4086 memcpy(buf, addr, len);
4091 /* set NULL to stop other functions from doing siglongjmp */
4095 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4096 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4097 f.path, st, (long long)off, (int)len,
4098 translate_mmap_flags(flags),
4099 (e == 0) ? "0" : "Bus error");
4107 mread_f(int opno, long r)
4109 #ifdef HAVE_SYS_MMAN_H
4110 do_mmap(opno, r, PROT_READ);
4115 mwrite_f(int opno, long r)
4117 #ifdef HAVE_SYS_MMAN_H
4118 do_mmap(opno, r, PROT_WRITE);
4123 punch_f(int opno, long r)
4125 #ifdef HAVE_LINUX_FALLOC_H
4126 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4131 zero_f(int opno, long r)
4133 #ifdef HAVE_LINUX_FALLOC_H
4134 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4139 collapse_f(int opno, long r)
4141 #ifdef HAVE_LINUX_FALLOC_H
4142 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4147 insert_f(int opno, long r)
4149 #ifdef HAVE_LINUX_FALLOC_H
4150 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4155 read_f(int opno, long r)
4169 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4171 printf("%d/%d: read - no filename\n", procid, opno);
4175 fd = open_path(&f, O_RDONLY);
4176 e = fd < 0 ? errno : 0;
4180 printf("%d/%d: read - open %s failed %d\n",
4181 procid, opno, f.path, e);
4185 if (fstat64(fd, &stb) < 0) {
4187 printf("%d/%d: read - fstat64 %s failed %d\n",
4188 procid, opno, f.path, errno);
4193 inode_info(st, sizeof(st), &stb, v);
4194 if (stb.st_size == 0) {
4196 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4202 lr = ((int64_t)random() << 32) + random();
4203 off = (off64_t)(lr % stb.st_size);
4204 lseek64(fd, off, SEEK_SET);
4205 len = (random() % FILELEN_MAX) + 1;
4207 e = read(fd, buf, len) < 0 ? errno : 0;
4210 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4211 procid, opno, f.path, st, (long long)off, (int)len, e);
4217 readlink_f(int opno, long r)
4225 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4227 printf("%d/%d: readlink - no filename\n", procid, opno);
4231 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4234 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4239 readv_f(int opno, long r)
4251 struct iovec *iov = NULL;
4258 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4260 printf("%d/%d: readv - no filename\n", procid, opno);
4264 fd = open_path(&f, O_RDONLY);
4265 e = fd < 0 ? errno : 0;
4269 printf("%d/%d: readv - open %s failed %d\n",
4270 procid, opno, f.path, e);
4274 if (fstat64(fd, &stb) < 0) {
4276 printf("%d/%d: readv - fstat64 %s failed %d\n",
4277 procid, opno, f.path, errno);
4282 inode_info(st, sizeof(st), &stb, v);
4283 if (stb.st_size == 0) {
4285 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4291 lr = ((int64_t)random() << 32) + random();
4292 off = (off64_t)(lr % stb.st_size);
4293 lseek64(fd, off, SEEK_SET);
4294 len = (random() % FILELEN_MAX) + 1;
4297 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4298 iov = calloc(iovcnt, sizeof(struct iovec));
4299 iovl = len / iovcnt;
4301 for (i=0; i<iovcnt; i++) {
4302 (iov + i)->iov_base = (buf + iovb);
4303 (iov + i)->iov_len = iovl;
4307 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4310 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4311 procid, opno, f.path, st, (long long)off, (int)iovl,
4318 removefattr_f(int opno, long r)
4324 char name[XATTR_NAME_BUF_SIZE];
4328 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4330 printf("%d/%d: removefattr - no filename\n", procid, opno);
4336 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4337 * remove a xattr that doesn't exist (fremovexattr should fail with
4338 * errno set to ENOATTR (61) in this case).
4340 if (fep->xattr_counter > 0)
4341 xattr_num = (random() % fep->xattr_counter) + 1;
4345 e = generate_xattr_name(xattr_num, name, sizeof(name));
4347 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4348 procid, opno, f.path, e);
4352 e = removexattr(f.path, name) < 0 ? errno : 0;
4354 printf("%d/%d: removefattr file %s name %s %d\n",
4355 procid, opno, f.path, name, e);
4360 struct print_flags renameat2_flags [] = {
4361 { RENAME_NOREPLACE, "NOREPLACE"},
4362 { RENAME_EXCHANGE, "EXCHANGE"},
4363 { RENAME_WHITEOUT, "WHITEOUT"},
4367 #define translate_renameat2_flags(mode) \
4368 ({translate_flags(mode, "|", renameat2_flags);})
4371 do_renameat2(int opno, long r, int mode)
4387 /* get an existing path for the source of the rename */
4389 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4390 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4392 printf("%d/%d: rename - no source filename\n",
4399 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4400 * order to maintain filelist/filename integrity, we should
4401 * restrict exchange operation to files of the same type.
4403 if (mode == RENAME_EXCHANGE) {
4404 which = 1 << (flp - flist);
4405 init_pathname(&newf);
4406 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4408 printf("%d/%d: rename - no target filename\n",
4410 free_pathname(&newf);
4414 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4415 fents_ancestor_check(dfep, fep))) {
4417 printf("%d/%d: rename(REXCHANGE) %s and %s "
4418 "have ancestor-descendant relationship\n",
4419 procid, opno, f.path, newf.path);
4420 free_pathname(&newf);
4426 parid = dfep->parent;
4429 * Get an existing directory for the destination parent
4432 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4439 * Generate a new path using an existing parent directory
4442 init_pathname(&newf);
4443 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4447 (void)fent_to_name(&f, dfep);
4448 printf("%d/%d: rename - no filename from %s\n",
4449 procid, opno, f.path);
4451 free_pathname(&newf);
4456 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4459 int xattr_counter = fep->xattr_counter;
4460 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4461 int ft = flp - flist;
4464 oldparid = fep->parent;
4467 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4468 * old parent id for the others.
4470 if (ft == FT_DIR || ft == FT_SUBVOL)
4471 fix_parent(oldid, id, swap);
4473 if (mode == RENAME_WHITEOUT) {
4474 fep->xattr_counter = 0;
4475 add_to_flist(flp - flist, id, parid, xattr_counter);
4476 } else if (mode == RENAME_EXCHANGE) {
4477 fep->xattr_counter = dfep->xattr_counter;
4478 dfep->xattr_counter = xattr_counter;
4480 del_from_flist(flp - flist, fep - flp->fents);
4481 add_to_flist(flp - flist, id, parid, xattr_counter);
4485 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4486 opno, translate_renameat2_flags(mode), f.path,
4489 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4490 procid, opno, oldid, oldparid);
4491 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4492 procid, opno, id, parid);
4495 free_pathname(&newf);
4500 rename_f(int opno, long r)
4502 do_renameat2(opno, r, 0);
4506 rnoreplace_f(int opno, long r)
4508 do_renameat2(opno, r, RENAME_NOREPLACE);
4512 rexchange_f(int opno, long r)
4514 do_renameat2(opno, r, RENAME_EXCHANGE);
4518 rwhiteout_f(int opno, long r)
4520 do_renameat2(opno, r, RENAME_WHITEOUT);
4524 resvsp_f(int opno, long r)
4529 struct xfs_flock64 fl;
4537 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4539 printf("%d/%d: resvsp - no filename\n", procid, opno);
4543 fd = open_path(&f, O_RDWR);
4544 e = fd < 0 ? errno : 0;
4548 printf("%d/%d: resvsp - open %s failed %d\n",
4549 procid, opno, f.path, e);
4553 if (fstat64(fd, &stb) < 0) {
4555 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4556 procid, opno, f.path, errno);
4561 inode_info(st, sizeof(st), &stb, v);
4562 lr = ((int64_t)random() << 32) + random();
4563 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4565 fl.l_whence = SEEK_SET;
4567 fl.l_len = (off64_t)(random() % (1024 * 1024));
4568 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4570 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4571 procid, opno, f.path, st,
4572 (long long)off, (long long)fl.l_len, e);
4578 rmdir_f(int opno, long r)
4588 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4590 printf("%d/%d: rmdir - no directory\n", procid, opno);
4594 e = rmdir_path(&f) < 0 ? errno : 0;
4598 oldparid = fep->parent;
4599 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4602 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4604 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4605 procid, opno, oldid, oldparid);
4611 setattr_f(int opno, long r)
4620 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4621 append_pathname(&f, ".");
4622 fd = open_path(&f, O_RDWR);
4623 e = fd < 0 ? errno : 0;
4626 fl = attr_mask & (uint)random();
4627 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4629 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4635 setfattr_f(int opno, long r)
4642 char name[XATTR_NAME_BUF_SIZE];
4648 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4650 printf("%d/%d: setfattr - no filename\n", procid, opno);
4655 if ((fep->xattr_counter > 0) && (random() % 2)) {
4657 * Use an existing xattr name for replacing its value or
4658 * create again a xattr that was previously deleted.
4660 xattr_num = (random() % fep->xattr_counter) + 1;
4662 flag = XATTR_REPLACE;
4664 /* Use a new xattr name. */
4665 xattr_num = fep->xattr_counter + 1;
4667 * Don't always use the create flag because even if our xattr
4668 * counter is 0, we may still have xattrs associated to this
4669 * file (this happens when xattrs are added to a file through
4670 * one of its other hard links), so we can end up updating an
4671 * existing xattr too.
4674 flag = XATTR_CREATE;
4678 * The maximum supported value size depends on the filesystem
4679 * implementation, but 100 bytes is a safe value for most filesystems
4682 value_len = random() % 101;
4683 value = gen_random_string(value_len);
4684 if (!value && value_len > 0) {
4686 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4687 procid, opno, f.path, value_len);
4690 e = generate_xattr_name(xattr_num, name, sizeof(name));
4692 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4693 procid, opno, f.path, e);
4697 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4699 fep->xattr_counter++;
4701 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4702 procid, opno, f.path, name, xattr_flag_to_string(flag),
4710 snapshot_f(int opno, long r)
4712 #ifdef HAVE_BTRFSUTIL_H
4713 enum btrfs_util_error e;
4724 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4726 printf("%d/%d: snapshot - no subvolume\n", procid,
4731 init_pathname(&newf);
4733 err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
4737 (void)fent_to_name(&f, fep);
4738 printf("%d/%d: snapshot - no filename from %s\n",
4739 procid, opno, f.path);
4744 e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
4745 if (e == BTRFS_UTIL_OK)
4746 add_to_flist(FT_SUBVOL, id, parid, 0);
4748 printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
4749 f.path, newf.path, e, btrfs_util_strerror(e));
4750 printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
4753 free_pathname(&newf);
4759 stat_f(int opno, long r)
4767 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4769 printf("%d/%d: stat - no entries\n", procid, opno);
4773 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4776 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4781 subvol_create_f(int opno, long r)
4783 #ifdef HAVE_BTRFSUTIL_H
4784 enum btrfs_util_error e;
4794 if (!get_fname(FT_DIRm | FT_SUBVOLm, r, NULL, NULL, &fep, &v))
4798 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4802 (void)fent_to_name(&f, fep);
4803 printf("%d/%d: subvol_create - no filename from %s\n",
4804 procid, opno, f.path);
4809 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4810 if (e == BTRFS_UTIL_OK)
4811 add_to_flist(FT_SUBVOL, id, parid, 0);
4813 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4814 f.path, e, btrfs_util_strerror(e));
4815 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4823 subvol_delete_f(int opno, long r)
4825 #ifdef HAVE_BTRFSUTIL_H
4826 enum btrfs_util_error e;
4834 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4836 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4841 e = btrfs_util_delete_subvolume(f.path, 0);
4843 if (e == BTRFS_UTIL_OK) {
4845 oldparid = fep->parent;
4846 delete_subvol_children(oldid);
4847 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4850 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4851 e, btrfs_util_strerror(e));
4852 if (e == BTRFS_UTIL_OK)
4853 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4854 procid, opno, oldid, oldparid);
4861 symlink_f(int opno, long r)
4874 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
4879 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4883 (void)fent_to_name(&f, fep);
4884 printf("%d/%d: symlink - no filename from %s\n",
4885 procid, opno, f.path);
4890 len = (int)(random() % PATH_MAX);
4891 val = malloc(len + 1);
4893 memset(val, 'x', len);
4895 for (i = 10; i < len - 1; i += 10)
4897 e = symlink_path(val, &f) < 0 ? errno : 0;
4900 add_to_flist(FT_SYM, id, parid, 0);
4903 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4904 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4911 sync_f(int opno, long r)
4915 printf("%d/%d: sync\n", procid, opno);
4919 truncate_f(int opno, long r)
4930 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4932 printf("%d/%d: truncate - no filename\n", procid, opno);
4936 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4940 printf("%d/%d: truncate - stat64 %s failed %d\n",
4941 procid, opno, f.path, e);
4945 inode_info(st, sizeof(st), &stb, v);
4946 lr = ((int64_t)random() << 32) + random();
4947 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4949 e = truncate64_path(&f, off) < 0 ? errno : 0;
4952 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4953 st, (long long)off, e);
4958 unlink_f(int opno, long r)
4969 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4971 printf("%d/%d: unlink - no file\n", procid, opno);
4975 e = unlink_path(&f) < 0 ? errno : 0;
4979 oldparid = fep->parent;
4980 del_from_flist(flp - flist, fep - flp->fents);
4983 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4985 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4986 procid, opno, oldid, oldparid);
4992 unresvsp_f(int opno, long r)
4997 struct xfs_flock64 fl;
5005 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
5007 printf("%d/%d: unresvsp - no filename\n", procid, opno);
5011 fd = open_path(&f, O_RDWR);
5012 e = fd < 0 ? errno : 0;
5016 printf("%d/%d: unresvsp - open %s failed %d\n",
5017 procid, opno, f.path, e);
5021 if (fstat64(fd, &stb) < 0) {
5023 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
5024 procid, opno, f.path, errno);
5029 inode_info(st, sizeof(st), &stb, v);
5030 lr = ((int64_t)random() << 32) + random();
5031 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5033 fl.l_whence = SEEK_SET;
5035 fl.l_len = (off64_t)(random() % (1 << 20));
5036 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
5038 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
5039 procid, opno, f.path, st,
5040 (long long)off, (long long)fl.l_len, e);
5046 write_f(int opno, long r)
5060 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5062 printf("%d/%d: write - no filename\n", procid, opno);
5066 fd = open_path(&f, O_WRONLY);
5067 e = fd < 0 ? errno : 0;
5071 printf("%d/%d: write - open %s failed %d\n",
5072 procid, opno, f.path, e);
5076 if (fstat64(fd, &stb) < 0) {
5078 printf("%d/%d: write - fstat64 %s failed %d\n",
5079 procid, opno, f.path, errno);
5084 inode_info(st, sizeof(st), &stb, v);
5085 lr = ((int64_t)random() << 32) + random();
5086 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5088 lseek64(fd, off, SEEK_SET);
5089 len = (random() % FILELEN_MAX) + 1;
5091 memset(buf, nameseq & 0xff, len);
5092 e = write(fd, buf, len) < 0 ? errno : 0;
5095 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5096 procid, opno, f.path, st, (long long)off, (int)len, e);
5102 writev_f(int opno, long r)
5114 struct iovec *iov = NULL;
5121 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5123 printf("%d/%d: writev - no filename\n", procid, opno);
5127 fd = open_path(&f, O_WRONLY);
5128 e = fd < 0 ? errno : 0;
5132 printf("%d/%d: writev - open %s failed %d\n",
5133 procid, opno, f.path, e);
5137 if (fstat64(fd, &stb) < 0) {
5139 printf("%d/%d: writev - fstat64 %s failed %d\n",
5140 procid, opno, f.path, errno);
5145 inode_info(st, sizeof(st), &stb, v);
5146 lr = ((int64_t)random() << 32) + random();
5147 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5149 lseek64(fd, off, SEEK_SET);
5150 len = (random() % FILELEN_MAX) + 1;
5152 memset(buf, nameseq & 0xff, len);
5154 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5155 iov = calloc(iovcnt, sizeof(struct iovec));
5156 iovl = len / iovcnt;
5158 for (i=0; i<iovcnt; i++) {
5159 (iov + i)->iov_base = (buf + iovb);
5160 (iov + i)->iov_len = iovl;
5164 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5168 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5169 procid, opno, f.path, st, (long long)off, (int)iovl,
5176 xattr_flag_to_string(int flag)
5178 if (flag == XATTR_CREATE)
5180 if (flag == XATTR_REPLACE)