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)
145 typedef void (*opfnc_t)(int, long);
147 typedef struct opdesc {
155 typedef struct fent {
162 typedef struct flist {
169 typedef struct pathname {
179 struct print_string {
186 #define FT_DIRm (1 << FT_DIR)
188 #define FT_REGm (1 << FT_REG)
190 #define FT_SYMm (1 << FT_SYM)
192 #define FT_DEVm (1 << FT_DEV)
194 #define FT_RTFm (1 << FT_RTF)
196 #define FT_SUBVOLm (1 << FT_SUBVOL)
198 #define FT_ANYm ((1 << FT_nft) - 1)
199 #define FT_REGFILE (FT_REGm | FT_RTFm)
200 #define FT_NOTDIR (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
202 #define FLIST_SLOT_INCR 16
205 #define MAXFSIZE ((1ULL << 63) - 1ULL)
206 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
208 #define XATTR_NAME_BUF_SIZE 18
210 void afsync_f(int, long);
211 void allocsp_f(int, long);
212 void aread_f(int, long);
213 void attr_remove_f(int, long);
214 void attr_set_f(int, long);
215 void awrite_f(int, long);
216 void bulkstat_f(int, long);
217 void bulkstat1_f(int, long);
218 void chown_f(int, long);
219 void clonerange_f(int, long);
220 void copyrange_f(int, long);
221 void creat_f(int, long);
222 void deduperange_f(int, long);
223 void dread_f(int, long);
224 void dwrite_f(int, long);
225 void fallocate_f(int, long);
226 void fdatasync_f(int, long);
227 void fiemap_f(int, long);
228 void freesp_f(int, long);
229 void fsync_f(int, long);
230 char *gen_random_string(int);
231 void getattr_f(int, long);
232 void getdents_f(int, long);
233 void getfattr_f(int, long);
234 void link_f(int, long);
235 void listfattr_f(int, long);
236 void mkdir_f(int, long);
237 void mknod_f(int, long);
238 void mread_f(int, long);
239 void mwrite_f(int, long);
240 void punch_f(int, long);
241 void zero_f(int, long);
242 void collapse_f(int, long);
243 void insert_f(int, long);
244 void read_f(int, long);
245 void readlink_f(int, long);
246 void readv_f(int, long);
247 void removefattr_f(int, long);
248 void rename_f(int, long);
249 void rnoreplace_f(int, long);
250 void rexchange_f(int, long);
251 void rwhiteout_f(int, long);
252 void resvsp_f(int, long);
253 void rmdir_f(int, long);
254 void setattr_f(int, long);
255 void setfattr_f(int, long);
256 void setxattr_f(int, long);
257 void splice_f(int, long);
258 void stat_f(int, long);
259 void subvol_create_f(int, long);
260 void subvol_delete_f(int, long);
261 void symlink_f(int, long);
262 void sync_f(int, long);
263 void truncate_f(int, long);
264 void unlink_f(int, long);
265 void unresvsp_f(int, long);
266 void write_f(int, long);
267 void writev_f(int, long);
268 char *xattr_flag_to_string(int);
271 /* { OP_ENUM, "name", function, freq, iswrite }, */
272 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
273 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
274 { OP_AREAD, "aread", aread_f, 1, 0 },
275 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
276 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
277 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
278 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
279 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
280 { OP_CHOWN, "chown", chown_f, 3, 1 },
281 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
282 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
283 { OP_CREAT, "creat", creat_f, 4, 1 },
284 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
285 { OP_DREAD, "dread", dread_f, 4, 0 },
286 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
287 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
288 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
289 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
290 { OP_FREESP, "freesp", freesp_f, 1, 1 },
291 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
292 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
293 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
294 /* get extended attribute */
295 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
296 { OP_LINK, "link", link_f, 1, 1 },
297 /* list extent attributes */
298 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
299 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
300 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
301 { OP_MREAD, "mread", mread_f, 2, 0 },
302 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
303 { OP_PUNCH, "punch", punch_f, 1, 1 },
304 { OP_ZERO, "zero", zero_f, 1, 1 },
305 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
306 { OP_INSERT, "insert", insert_f, 1, 1 },
307 { OP_READ, "read", read_f, 1, 0 },
308 { OP_READLINK, "readlink", readlink_f, 1, 0 },
309 { OP_READV, "readv", readv_f, 1, 0 },
310 /* remove (delete) extended attribute */
311 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
312 { OP_RENAME, "rename", rename_f, 2, 1 },
313 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
314 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
315 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
316 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
317 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
318 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
319 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
320 /* set extended attribute */
321 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
322 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
323 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
324 { OP_SPLICE, "splice", splice_f, 1, 1 },
325 { OP_STAT, "stat", stat_f, 1, 0 },
326 { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
327 { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
328 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
329 { OP_SYNC, "sync", sync_f, 1, 1 },
330 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
331 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
332 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
333 { OP_WRITE, "write", write_f, 4, 1 },
334 { OP_WRITEV, "writev", writev_f, 4, 1 },
337 flist_t flist[FT_nft] = {
351 struct xfs_fsop_geom geom;
362 unsigned int idmodulo = XFS_IDMODULO_MAX;
363 unsigned int attr_mask = ~0;
366 unsigned long seed = 0;
370 int verifiable_log = 0;
371 sig_atomic_t should_stop = 0;
372 sigjmp_buf *sigbus_jmp = NULL;
373 char *execute_cmd = NULL;
374 int execute_freq = 1;
375 struct print_string flag_str = {0};
377 void add_to_flist(int, int, int, int);
378 void append_pathname(pathname_t *, char *);
379 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
380 int attr_remove_path(pathname_t *, const char *, int);
381 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
382 void check_cwd(void);
383 void cleanup_flist(void);
384 int creat_path(pathname_t *, mode_t);
385 void dcache_enter(int, int);
386 void dcache_init(void);
387 fent_t *dcache_lookup(int);
388 void dcache_purge(int, int);
389 void del_from_flist(int, int);
390 int dirid_to_name(char *, int);
392 int fent_to_name(pathname_t *, fent_t *);
393 bool fents_ancestor_check(fent_t *, fent_t *);
394 void fix_parent(int, int, bool);
395 void free_pathname(pathname_t *);
396 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
397 int generate_xattr_name(int, char *, int);
398 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
399 void init_pathname(pathname_t *);
400 int lchown_path(pathname_t *, uid_t, gid_t);
401 int link_path(pathname_t *, pathname_t *);
402 int lstat64_path(pathname_t *, struct stat64 *);
403 void make_freq_table(void);
404 int mkdir_path(pathname_t *, mode_t);
405 int mknod_path(pathname_t *, mode_t, dev_t);
406 void namerandpad(int, char *, int);
407 int open_file_or_dir(pathname_t *, int);
408 int open_path(pathname_t *, int);
409 DIR *opendir_path(pathname_t *);
410 void process_freq(char *);
411 int readlink_path(pathname_t *, char *, size_t);
412 int rename_path(pathname_t *, pathname_t *, int);
413 int rmdir_path(pathname_t *);
414 void separate_pathname(pathname_t *, char *, pathname_t *);
415 void show_ops(int, char *);
416 int stat64_path(pathname_t *, struct stat64 *);
417 int symlink_path(const char *, pathname_t *);
418 int truncate64_path(pathname_t *, off64_t);
419 int unlink_path(pathname_t *);
421 void write_freq(void);
422 void zero_freq(void);
423 void non_btrfs_freq(const char *);
425 void sg_handler(int signum)
433 * Only handle SIGBUS when mmap write to a hole and no
434 * block can be allocated due to ENOSPC, abort otherwise.
437 siglongjmp(*sigbus_jmp, -1);
439 printf("Unknown SIGBUS is caught, Abort!\n");
442 /* should not reach here */
449 int main(int argc, char **argv)
453 char *dirname = NULL;
454 char *logname = NULL;
455 char rpath[PATH_MAX];
464 xfs_error_injection_t err_inj;
465 struct sigaction action;
467 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
469 errrange = errtag = 0;
471 nops = sizeof(ops) / sizeof(ops[0]);
472 ops_end = &ops[nops];
474 while ((c = getopt(argc, argv, allopts)) != -1) {
483 sscanf(optarg, "%d", &errtag);
487 } else if (errtag == 0)
489 if (errtag >= XFS_ERRTAG_MAX) {
491 "error tag %d too large (max %d)\n",
492 errtag, XFS_ERRTAG_MAX - 1);
497 process_freq(optarg);
500 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
501 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
504 idmodulo = strtoul(optarg, NULL, 0);
505 if (idmodulo > XFS_IDMODULO_MAX) {
507 "chown modulo %d too big (max %d)\n",
508 idmodulo, XFS_IDMODULO_MAX);
513 loops = atoi(optarg);
516 operations = atoi(optarg);
523 nproc = atoi(optarg);
529 seed = strtoul(optarg, NULL, 0);
538 execute_cmd = optarg;
544 attr_mask = strtoul(optarg, NULL, 0);
548 if (optarg[0] == 'c')
559 execute_freq = strtoul(optarg, NULL, 0);
562 fprintf(stderr, "%s - invalid parameters\n",
572 /* no directory specified */
573 if (!nousage) usage();
577 non_btrfs_freq(dirname);
578 (void)mkdir(dirname, 0777);
579 if (logname && logname[0] != '/') {
580 if (!getcwd(rpath, sizeof(rpath))){
581 perror("getcwd failed");
587 if (chdir(dirname) < 0) {
592 char path[PATH_MAX + NAME_MAX + 1];
593 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
594 if (freopen(path, "a", stdout) == NULL) {
595 perror("freopen logfile failed");
599 sprintf(buf, "fss%x", (unsigned int)getpid());
600 fd = creat(buf, 0666);
601 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
602 maxfsize = (off64_t)MAXFSIZE32;
604 maxfsize = (off64_t)MAXFSIZE;
609 gettimeofday(&t, (void *)NULL);
610 seed = (int)t.tv_sec ^ (int)t.tv_usec;
611 printf("seed = %ld\n", seed);
613 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
614 if (i >= 0 && geom.rtblocks)
615 rtpct = MIN(MAX(geom.rtblocks * 100 /
616 (geom.rtblocks + geom.datablocks), 1), 99);
625 for (i = 0; i < j; i++)
628 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
634 for (i = 0; i < j; i++)
637 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
639 printf("Injecting failure on tag #%d\n", errtag);
640 err_inj.errtag = errtag;
642 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
644 perror("fsstress - XFS_SYSSGI error injection call");
653 action.sa_handler = sg_handler;
654 sigemptyset(&action.sa_mask);
656 if (sigaction(SIGTERM, &action, 0)) {
657 perror("sigaction failed");
661 for (i = 0; i < nproc; i++) {
663 sigemptyset(&action.sa_mask);
664 action.sa_handler = SIG_DFL;
665 if (sigaction(SIGTERM, &action, 0))
667 action.sa_handler = sg_handler;
668 if (sigaction(SIGBUS, &action, 0))
670 #ifdef HAVE_SYS_PRCTL_H
671 prctl(PR_SET_PDEATHSIG, SIGKILL);
672 if (getppid() == 1) /* parent died already? */
676 char path[PATH_MAX + NAME_MAX + 2 + 11];
677 snprintf(path, sizeof(path), "%s/%s.%d",
679 if (freopen(path, "a", stdout) == NULL) {
680 perror("freopen logfile failed");
686 if (io_setup(128, &io_ctx) != 0) {
687 fprintf(stderr, "io_setup failed");
691 for (i = 0; !loops || (i < loops); i++)
694 if(io_destroy(io_ctx) != 0) {
695 fprintf(stderr, "io_destroy failed");
705 while (wait(&stat) > 0 && !should_stop) {
708 action.sa_flags = SA_RESTART;
709 sigaction(SIGTERM, &action, 0);
710 kill(-getpid(), SIGTERM);
711 while (wait(&stat) > 0)
717 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
719 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
721 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
734 add_string(struct print_string *str, const char *add)
736 int len = strlen(add);
741 if (len > (str->max - 1) - str->len) {
742 str->len = str->max - 1;
746 memcpy(str->buffer + str->len, add, len);
748 str->buffer[str->len] = '\0';
754 translate_flags(int flags, const char *delim,
755 const struct print_flags *flag_array)
757 int i, mask, first = 1;
760 if (!flag_str.buffer) {
761 flag_str.buffer = malloc(4096);
765 if (!flag_str.buffer)
768 flag_str.buffer[0] = '\0';
770 for (i = 0; flag_array[i].name && flags; i++) {
771 mask = flag_array[i].mask;
772 if ((flags & mask) != mask)
775 add = flag_array[i].name;
778 add_string(&flag_str, delim);
781 add_string(&flag_str, add);
784 /* Check whether there are any leftover flags. */
790 add_string(&flag_str, delim);
792 ret = snprintf(number, 11, "0x%x", flags) > 0;
793 if (ret > 0 && ret <= 11)
794 add_string(&flag_str, number);
797 return flag_str.buffer;
801 add_to_flist(int ft, int id, int parent, int xattr_counter)
807 if (ftp->nfiles == ftp->nslots) {
808 ftp->nslots += FLIST_SLOT_INCR;
809 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
811 fep = &ftp->fents[ftp->nfiles++];
814 fep->parent = parent;
815 fep->xattr_counter = xattr_counter;
819 append_pathname(pathname_t *name, char *str)
825 /* attempting to append to a dir a zero length path */
826 if (len && *str == '/' && name->len == 0) {
827 fprintf(stderr, "fsstress: append_pathname failure\n");
828 assert(chdir(homedir) == 0);
833 name->path = realloc(name->path, name->len + 1 + len);
834 strcpy(&name->path[name->len], str);
839 attr_list_path(pathname_t *name,
841 const int buffersize,
843 attrlist_cursor_t *cursor)
845 char buf[NAME_MAX + 1];
849 if (flags != ATTR_DONTFOLLOW) {
854 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
855 if (rval >= 0 || errno != ENAMETOOLONG)
857 separate_pathname(name, buf, &newname);
858 if (chdir(buf) == 0) {
859 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
860 assert(chdir("..") == 0);
862 free_pathname(&newname);
867 attr_remove_path(pathname_t *name, const char *attrname, int flags)
869 char buf[NAME_MAX + 1];
873 rval = attr_remove(name->path, attrname, flags);
874 if (rval >= 0 || errno != ENAMETOOLONG)
876 separate_pathname(name, buf, &newname);
877 if (chdir(buf) == 0) {
878 rval = attr_remove_path(&newname, attrname, flags);
879 assert(chdir("..") == 0);
881 free_pathname(&newname);
886 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
887 const int valuelength, int flags)
889 char buf[NAME_MAX + 1];
893 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
894 if (rval >= 0 || errno != ENAMETOOLONG)
896 separate_pathname(name, buf, &newname);
897 if (chdir(buf) == 0) {
898 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
900 assert(chdir("..") == 0);
902 free_pathname(&newname);
910 struct stat64 statbuf;
912 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
914 assert(chdir(homedir) == 0);
915 fprintf(stderr, "fsstress: check_cwd failure\n");
922 * go thru flist and release all entries
924 * NOTE: this function shouldn't be called until the end of a process
932 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
941 creat_path(pathname_t *name, mode_t mode)
943 char buf[NAME_MAX + 1];
947 rval = creat(name->path, mode);
948 if (rval >= 0 || errno != ENAMETOOLONG)
950 separate_pathname(name, buf, &newname);
951 if (chdir(buf) == 0) {
952 rval = creat_path(&newname, mode);
953 assert(chdir("..") == 0);
955 free_pathname(&newname);
960 dcache_enter(int dirid, int slot)
962 dcache[dirid % NDCACHE] = slot;
970 for (i = 0; i < NDCACHE; i++)
975 dcache_lookup(int dirid)
980 i = dcache[dirid % NDCACHE];
981 if (i >= 0 && i < flist[FT_DIR].nfiles &&
982 (fep = &flist[FT_DIR].fents[i])->id == dirid)
984 if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
985 (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
991 dcache_purge(int dirid, int ft)
994 dcp = &dcache[dirid % NDCACHE];
995 if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
996 flist[ft].fents[*dcp].id == dirid)
1001 * Delete the item from the list by
1002 * moving last entry over the deleted one;
1003 * unless deleted entry is the last one.
1004 * Input: which file list array and which slot in array
1007 del_from_flist(int ft, int slot)
1012 if (ft == FT_DIR || ft == FT_SUBVOL)
1013 dcache_purge(ftp->fents[slot].id, ft);
1014 if (slot != ftp->nfiles - 1) {
1015 if (ft == FT_DIR || ft == FT_SUBVOL)
1016 dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
1017 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1023 delete_subvol_children(int parid)
1028 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1030 for (c = 0; c < flp->nfiles; c++) {
1031 if (flp->fents[c].parent == parid) {
1032 int id = flp->fents[c].id;
1033 del_from_flist(i, c);
1034 if (i == FT_DIR || i == FT_SUBVOL)
1035 delete_subvol_children(id);
1043 dirid_to_fent(int dirid)
1050 if ((fep = dcache_lookup(dirid)))
1054 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1055 if (fep->id == dirid) {
1056 dcache_enter(dirid, fep - flp->fents);
1070 struct stat64 statbuf;
1078 dividend = (operations + execute_freq) / (execute_freq + 1);
1079 sprintf(buf, "p%x", procid);
1080 (void)mkdir(buf, 0777);
1081 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1085 top_ino = statbuf.st_ino;
1086 homedir = getcwd(NULL, 0);
1088 perror("getcwd failed");
1094 namerand = random();
1095 for (opno = 0; opno < operations; opno++) {
1096 if (execute_cmd && opno && opno % dividend == 0) {
1098 printf("%d: execute command %s\n", opno,
1100 rval = system(execute_cmd);
1102 fprintf(stderr, "execute command failed with "
1105 p = &ops[freq_table[random() % freq_table_size]];
1106 p->func(opno, random());
1108 * test for forced shutdown by stat'ing the test
1109 * directory. If this stat returns EIO, assume
1110 * the forced shutdown happened.
1112 if (errtag != 0 && opno % 100 == 0) {
1113 rval = stat64(".", &statbuf);
1115 fprintf(stderr, "Detected EIO\n");
1121 assert(chdir("..") == 0);
1126 sprintf(cmd, "rm -rf %s", buf);
1129 perror("cleaning up");
1135 * build up a pathname going thru the file entry and all
1136 * its parent entries
1137 * Return 0 on error, 1 on success;
1140 fent_to_name(pathname_t *name, fent_t *fep)
1142 flist_t *flp = &flist[fep->ft];
1143 char buf[NAME_MAX + 1];
1151 /* build up parent directory name */
1152 if (fep->parent != -1) {
1153 pfep = dirid_to_fent(fep->parent);
1156 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1157 procid, fep->id, fep->parent);
1162 e = fent_to_name(name, pfep);
1165 append_pathname(name, "/");
1168 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1169 namerandpad(fep->id, buf, i);
1170 append_pathname(name, buf);
1175 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1179 for (tmpfep = fep; tmpfep->parent != -1;
1180 tmpfep = dirid_to_fent(tmpfep->parent)) {
1181 if (tmpfep->parent == dfep->id)
1189 fix_parent(int oldid, int newid, bool swap)
1196 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1197 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1198 if (fep->parent == oldid)
1199 fep->parent = newid;
1200 else if (swap && fep->parent == newid)
1201 fep->parent = oldid;
1207 free_pathname(pathname_t *name)
1217 * Generate a filename of type ft.
1218 * If we have a fep which should be a directory then use it
1219 * as the parent path for this new filename i.e. prepend with it.
1222 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1224 char buf[NAME_MAX + 1];
1233 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1234 namerandpad(id, buf, len);
1236 /* prepend fep parent dir-name to it */
1238 e = fent_to_name(name, fep);
1241 append_pathname(name, "/");
1243 append_pathname(name, buf);
1247 for (j = 0; !*v && j < ilistlen; j++) {
1248 if (ilist[j] == id) {
1256 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1260 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1270 * Input: "which" to choose the file-types eg. non-directory
1271 * Input: "r" to choose which file
1272 * Output: file-list, file-entry, name for the chosen file.
1273 * Output: verbose if chosen file is on the ilist.
1276 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1279 int totalsum = 0; /* total number of matching files */
1280 int partialsum = 0; /* partial sum of matching files */
1286 int e = 1; /* success */
1289 * go thru flist and add up number of files for each
1290 * category that matches with <which>.
1292 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1293 if (which & (1 << i))
1294 totalsum += flp->nfiles;
1296 if (totalsum == 0) {
1306 * Now we have possible matches between 0..totalsum-1.
1307 * And we use r to help us choose which one we want,
1308 * which when bounded by totalsum becomes x.
1310 x = (int)(r % totalsum);
1311 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1312 if (which & (1 << i)) {
1313 if (x < partialsum + flp->nfiles) {
1315 /* found the matching file entry */
1316 fep = &flp->fents[x - partialsum];
1318 /* fill-in what we were asked for */
1320 e = fent_to_name(name, fep);
1323 fprintf(stderr, "%d: failed to get path for entry:"
1324 " id=%d,parent=%d\n",
1325 procid, fep->id, fep->parent);
1334 /* turn on verbose if its an ilisted file */
1336 for (j = 0; !*v && j < ilistlen; j++) {
1337 if (ilist[j] == fep->id) {
1344 partialsum += flp->nfiles;
1348 fprintf(stderr, "fsstress: get_fname failure\n");
1355 init_pathname(pathname_t *name)
1362 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1364 char buf[NAME_MAX + 1];
1368 rval = lchown(name->path, owner, group);
1369 if (rval >= 0 || errno != ENAMETOOLONG)
1371 separate_pathname(name, buf, &newname);
1372 if (chdir(buf) == 0) {
1373 rval = lchown_path(&newname, owner, group);
1374 assert(chdir("..") == 0);
1376 free_pathname(&newname);
1381 link_path(pathname_t *name1, pathname_t *name2)
1383 char buf1[NAME_MAX + 1];
1384 char buf2[NAME_MAX + 1];
1386 pathname_t newname1;
1387 pathname_t newname2;
1390 rval = link(name1->path, name2->path);
1391 if (rval >= 0 || errno != ENAMETOOLONG)
1393 separate_pathname(name1, buf1, &newname1);
1394 separate_pathname(name2, buf2, &newname2);
1395 if (strcmp(buf1, buf2) == 0) {
1396 if (chdir(buf1) == 0) {
1397 rval = link_path(&newname1, &newname2);
1398 assert(chdir("..") == 0);
1401 if (strcmp(buf1, "..") == 0)
1403 else if (strcmp(buf2, "..") == 0)
1405 else if (strlen(buf1) == 0)
1407 else if (strlen(buf2) == 0)
1410 down1 = MAX(newname1.len, 3 + name2->len) <=
1411 MAX(3 + name1->len, newname2.len);
1413 free_pathname(&newname2);
1414 append_pathname(&newname2, "../");
1415 append_pathname(&newname2, name2->path);
1416 if (chdir(buf1) == 0) {
1417 rval = link_path(&newname1, &newname2);
1418 assert(chdir("..") == 0);
1421 free_pathname(&newname1);
1422 append_pathname(&newname1, "../");
1423 append_pathname(&newname1, name1->path);
1424 if (chdir(buf2) == 0) {
1425 rval = link_path(&newname1, &newname2);
1426 assert(chdir("..") == 0);
1430 free_pathname(&newname1);
1431 free_pathname(&newname2);
1436 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1438 char buf[NAME_MAX + 1];
1442 rval = lstat64(name->path, sbuf);
1443 if (rval >= 0 || errno != ENAMETOOLONG)
1445 separate_pathname(name, buf, &newname);
1446 if (chdir(buf) == 0) {
1447 rval = lstat64_path(&newname, sbuf);
1448 assert(chdir("..") == 0);
1450 free_pathname(&newname);
1455 make_freq_table(void)
1461 for (p = ops, f = 0; p < ops_end; p++)
1463 freq_table = malloc(f * sizeof(*freq_table));
1464 freq_table_size = f;
1465 for (p = ops, i = 0; p < ops_end; p++) {
1466 for (f = 0; f < p->freq; f++, i++)
1467 freq_table[i] = p->op;
1472 mkdir_path(pathname_t *name, mode_t mode)
1474 char buf[NAME_MAX + 1];
1478 rval = mkdir(name->path, mode);
1479 if (rval >= 0 || errno != ENAMETOOLONG)
1481 separate_pathname(name, buf, &newname);
1482 if (chdir(buf) == 0) {
1483 rval = mkdir_path(&newname, mode);
1484 assert(chdir("..") == 0);
1486 free_pathname(&newname);
1491 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1493 char buf[NAME_MAX + 1];
1497 rval = mknod(name->path, mode, dev);
1498 if (rval >= 0 || errno != ENAMETOOLONG)
1500 separate_pathname(name, buf, &newname);
1501 if (chdir(buf) == 0) {
1502 rval = mknod_path(&newname, mode, dev);
1503 assert(chdir("..") == 0);
1505 free_pathname(&newname);
1510 namerandpad(int id, char *buf, int i)
1513 static int buckets[] =
1514 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1520 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1521 padmod = buckets[bucket] + 1 - i;
1524 padlen = (id ^ namerand) % padmod;
1526 memset(&buf[i], 'X', padlen);
1527 buf[i + padlen] = '\0';
1532 open_file_or_dir(pathname_t *name, int flags)
1536 fd = open_path(name, flags);
1539 if (fd == -1 && errno != EISDIR)
1541 /* Directories can not be opened in write mode nor direct mode. */
1542 flags &= ~(O_WRONLY | O_DIRECT);
1543 flags |= O_RDONLY | O_DIRECTORY;
1544 return open_path(name, flags);
1548 open_path(pathname_t *name, int oflag)
1550 char buf[NAME_MAX + 1];
1554 rval = open(name->path, oflag);
1555 if (rval >= 0 || errno != ENAMETOOLONG)
1557 separate_pathname(name, buf, &newname);
1558 if (chdir(buf) == 0) {
1559 rval = open_path(&newname, oflag);
1560 assert(chdir("..") == 0);
1562 free_pathname(&newname);
1567 opendir_path(pathname_t *name)
1569 char buf[NAME_MAX + 1];
1573 rval = opendir(name->path);
1574 if (rval || errno != ENAMETOOLONG)
1576 separate_pathname(name, buf, &newname);
1577 if (chdir(buf) == 0) {
1578 rval = opendir_path(&newname);
1579 assert(chdir("..") == 0);
1581 free_pathname(&newname);
1586 process_freq(char *arg)
1591 s = strchr(arg, '=');
1593 fprintf(stderr, "bad argument '%s'\n", arg);
1597 for (p = ops; p < ops_end; p++) {
1598 if (strcmp(arg, p->name) == 0) {
1603 fprintf(stderr, "can't find op type %s for -f\n", arg);
1608 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1610 char buf[NAME_MAX + 1];
1614 rval = readlink(name->path, lbuf, lbufsiz);
1615 if (rval >= 0 || errno != ENAMETOOLONG)
1617 separate_pathname(name, buf, &newname);
1618 if (chdir(buf) == 0) {
1619 rval = readlink_path(&newname, lbuf, lbufsiz);
1620 assert(chdir("..") == 0);
1622 free_pathname(&newname);
1627 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1629 char buf1[NAME_MAX + 1];
1630 char buf2[NAME_MAX + 1];
1632 pathname_t newname1;
1633 pathname_t newname2;
1637 rval = rename(name1->path, name2->path);
1639 rval = renameat2(AT_FDCWD, name1->path,
1640 AT_FDCWD, name2->path, mode);
1641 if (rval >= 0 || errno != ENAMETOOLONG)
1643 separate_pathname(name1, buf1, &newname1);
1644 separate_pathname(name2, buf2, &newname2);
1645 if (strcmp(buf1, buf2) == 0) {
1646 if (chdir(buf1) == 0) {
1647 rval = rename_path(&newname1, &newname2, mode);
1648 assert(chdir("..") == 0);
1651 if (strcmp(buf1, "..") == 0)
1653 else if (strcmp(buf2, "..") == 0)
1655 else if (strlen(buf1) == 0)
1657 else if (strlen(buf2) == 0)
1660 down1 = MAX(newname1.len, 3 + name2->len) <=
1661 MAX(3 + name1->len, newname2.len);
1663 free_pathname(&newname2);
1664 append_pathname(&newname2, "../");
1665 append_pathname(&newname2, name2->path);
1666 if (chdir(buf1) == 0) {
1667 rval = rename_path(&newname1, &newname2, mode);
1668 assert(chdir("..") == 0);
1671 free_pathname(&newname1);
1672 append_pathname(&newname1, "../");
1673 append_pathname(&newname1, name1->path);
1674 if (chdir(buf2) == 0) {
1675 rval = rename_path(&newname1, &newname2, mode);
1676 assert(chdir("..") == 0);
1680 free_pathname(&newname1);
1681 free_pathname(&newname2);
1686 rmdir_path(pathname_t *name)
1688 char buf[NAME_MAX + 1];
1692 rval = rmdir(name->path);
1693 if (rval >= 0 || errno != ENAMETOOLONG)
1695 separate_pathname(name, buf, &newname);
1696 if (chdir(buf) == 0) {
1697 rval = rmdir_path(&newname);
1698 assert(chdir("..") == 0);
1700 free_pathname(&newname);
1705 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1709 init_pathname(newname);
1710 slash = strchr(name->path, '/');
1711 if (slash == NULL) {
1716 strcpy(buf, name->path);
1718 append_pathname(newname, slash + 1);
1724 show_ops(int flag, char *lead_str)
1729 /* print in list form */
1732 for (p = ops; p < ops_end; p++) {
1733 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1734 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1735 x+=printf("%s ", p->name);
1738 } else if (flag == 0) {
1739 /* Table view style */
1741 for (f = 0, p = ops; p < ops_end; p++)
1747 for (p = ops; p < ops_end; p++) {
1748 if (flag != 0 || p->freq > 0) {
1749 if (lead_str != NULL)
1750 printf("%s", lead_str);
1751 printf("%20s %d/%d %s\n",
1752 p->name, p->freq, f,
1753 (p->iswrite == 0) ? " " : "write op");
1757 /* Command line style */
1758 if (lead_str != NULL)
1759 printf("%s", lead_str);
1760 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1762 for (p = ops; p < ops_end; p++)
1764 printf("-f %s=%d \\\n",p->name, p->freq);
1769 stat64_path(pathname_t *name, struct stat64 *sbuf)
1771 char buf[NAME_MAX + 1];
1775 rval = stat64(name->path, sbuf);
1776 if (rval >= 0 || errno != ENAMETOOLONG)
1778 separate_pathname(name, buf, &newname);
1779 if (chdir(buf) == 0) {
1780 rval = stat64_path(&newname, sbuf);
1781 assert(chdir("..") == 0);
1783 free_pathname(&newname);
1788 symlink_path(const char *name1, pathname_t *name)
1790 char buf[NAME_MAX + 1];
1794 if (!strcmp(name1, name->path)) {
1795 printf("yikes! %s %s\n", name1, name->path);
1799 rval = symlink(name1, name->path);
1800 if (rval >= 0 || errno != ENAMETOOLONG)
1802 separate_pathname(name, buf, &newname);
1803 if (chdir(buf) == 0) {
1804 rval = symlink_path(name1, &newname);
1805 assert(chdir("..") == 0);
1807 free_pathname(&newname);
1812 truncate64_path(pathname_t *name, off64_t length)
1814 char buf[NAME_MAX + 1];
1818 rval = truncate64(name->path, length);
1819 if (rval >= 0 || errno != ENAMETOOLONG)
1821 separate_pathname(name, buf, &newname);
1822 if (chdir(buf) == 0) {
1823 rval = truncate64_path(&newname, length);
1824 assert(chdir("..") == 0);
1826 free_pathname(&newname);
1831 unlink_path(pathname_t *name)
1833 char buf[NAME_MAX + 1];
1837 rval = unlink(name->path);
1838 if (rval >= 0 || errno != ENAMETOOLONG)
1840 separate_pathname(name, buf, &newname);
1841 if (chdir(buf) == 0) {
1842 rval = unlink_path(&newname);
1843 assert(chdir("..") == 0);
1845 free_pathname(&newname);
1852 printf("Usage: %s -H or\n", myprog);
1853 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1855 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1857 printf(" -c clean up the test directory after each run\n");
1858 printf(" -d dir specifies the base directory for operations\n");
1859 printf(" -e errtg specifies error injection stuff\n");
1860 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1861 printf(" the valid operation names are:\n");
1863 printf(" -i filenum get verbose output for this nth file object\n");
1864 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1865 printf(" *use 0 for infinite (default 1)\n");
1866 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1867 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1868 printf(" -o logfile specifies logfile name\n");
1869 printf(" -p nproc specifies the no. of processes (default 1)\n");
1870 printf(" -r specifies random name padding\n");
1871 printf(" -s seed specifies the seed for the random generator (default random)\n");
1872 printf(" -v specifies verbose mode\n");
1873 printf(" -w zeros frequencies of non-write operations\n");
1874 printf(" -x cmd execute command in the middle of operations\n");
1875 printf(" -z zeros frequencies of all operations\n");
1876 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1877 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1878 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1879 printf(" -H prints usage and exits\n");
1887 for (p = ops; p < ops_end; p++) {
1898 for (p = ops; p < ops_end; p++)
1902 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1904 opty_t btrfs_ops[] = {
1910 non_btrfs_freq(const char *path)
1913 #ifdef HAVE_BTRFSUTIL_H
1914 enum btrfs_util_error e;
1916 e = btrfs_util_is_subvolume(path);
1917 if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
1920 for (p = ops; p < ops_end; p++) {
1922 for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
1923 if (p->op == btrfs_ops[i]) {
1931 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1934 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1935 verifiable_log ? -1: (long)s->st_ino,
1936 (long)s->st_nlink, s->st_uid, s->st_gid,
1937 (long long) s->st_blocks, (long long) s->st_size);
1941 afsync_f(int opno, long r)
1949 struct iocb *iocbs[] = { &iocb };
1950 struct io_event event;
1953 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1955 printf("%d/%d: afsync - no filename\n", procid, opno);
1959 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1960 e = fd < 0 ? errno : 0;
1964 printf("%d/%d: afsync - open %s failed %d\n",
1965 procid, opno, f.path, e);
1970 io_prep_fsync(&iocb, fd);
1971 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1973 printf("%d/%d: afsync - io_submit %s %d\n",
1974 procid, opno, f.path, e);
1979 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1981 printf("%d/%d: afsync - io_getevents failed %d\n",
1990 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1997 allocsp_f(int opno, long r)
2002 struct xfs_flock64 fl;
2010 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2012 printf("%d/%d: allocsp - no filename\n", procid, opno);
2016 fd = open_path(&f, O_RDWR);
2017 e = fd < 0 ? errno : 0;
2021 printf("%d/%d: allocsp - open %s failed %d\n",
2022 procid, opno, f.path, e);
2026 if (fstat64(fd, &stb) < 0) {
2028 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
2029 procid, opno, f.path, errno);
2034 inode_info(st, sizeof(st), &stb, v);
2035 lr = ((int64_t)random() << 32) + random();
2036 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2038 fl.l_whence = SEEK_SET;
2041 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
2043 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
2044 procid, opno, f.path, st, (long long)off, e);
2052 do_aio_rw(int opno, long r, int flags)
2056 struct dioattr diob;
2068 struct io_event event;
2069 struct iocb *iocbs[] = { &iocb };
2070 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
2073 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2075 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2079 fd = open_path(&f, flags|O_DIRECT);
2080 e = fd < 0 ? errno : 0;
2084 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2085 procid, opno, f.path, e);
2089 if (fstat64(fd, &stb) < 0) {
2091 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2092 procid, opno, f.path, errno);
2097 inode_info(st, sizeof(st), &stb, v);
2098 if (!iswrite && stb.st_size == 0) {
2100 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2106 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2109 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2110 " fallback to stat()\n",
2111 procid, opno, f.path, st, errno);
2112 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2113 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2115 dio_env = getenv("XFS_DIO_MIN");
2117 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2118 align = (int64_t)diob.d_miniosz;
2119 lr = ((int64_t)random() << 32) + random();
2120 len = (random() % FILELEN_MAX) + 1;
2121 len -= (len % align);
2124 else if (len > diob.d_maxiosz)
2125 len = diob.d_maxiosz;
2126 buf = memalign(diob.d_mem, len);
2129 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2130 off -= (off % align);
2132 memset(buf, nameseq & 0xff, len);
2133 io_prep_pwrite(&iocb, fd, buf, len, off);
2135 off = (off64_t)(lr % stb.st_size);
2136 off -= (off % align);
2137 io_prep_pread(&iocb, fd, buf, len, off);
2139 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2141 printf("%d/%d: %s - io_submit failed %d\n",
2142 procid, opno, iswrite ? "awrite" : "aread", e);
2147 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2149 printf("%d/%d: %s - io_getevents failed %d\n",
2150 procid, opno, iswrite ? "awrite" : "aread", e);
2156 e = event.res != len ? event.res2 : 0;
2159 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2160 procid, opno, iswrite ? "awrite" : "aread",
2161 f.path, st, (long long)off, (int)len, e);
2168 aread_f(int opno, long r)
2171 do_aio_rw(opno, r, O_RDONLY);
2176 attr_remove_f(int opno, long r)
2178 attrlist_ent_t *aep;
2182 attrlist_cursor_t cursor;
2191 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2192 append_pathname(&f, ".");
2194 bzero(&cursor, sizeof(cursor));
2196 bzero(buf, sizeof(buf));
2197 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2201 alist = (attrlist_t *)buf;
2202 total += alist->al_count;
2203 } while (alist->al_more);
2206 printf("%d/%d: attr_remove - no attrs for %s\n",
2207 procid, opno, f.path);
2211 which = (int)(random() % total);
2212 bzero(&cursor, sizeof(cursor));
2216 bzero(buf, sizeof(buf));
2217 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2221 alist = (attrlist_t *)buf;
2222 if (which < ent + alist->al_count) {
2223 aep = (attrlist_ent_t *)
2224 &buf[alist->al_offset[which - ent]];
2225 aname = aep->a_name;
2228 ent += alist->al_count;
2229 } while (alist->al_more);
2230 if (aname == NULL) {
2233 "%d/%d: attr_remove - name %d not found at %s\n",
2234 procid, opno, which, f.path);
2238 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2241 printf("%d/%d: attr_remove %s %s %d\n",
2242 procid, opno, f.path, aname, e);
2247 attr_set_f(int opno, long r)
2254 static int lengths[] = { 10, 100, 1000, 10000 };
2259 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2260 append_pathname(&f, ".");
2261 sprintf(aname, "a%x", nameseq++);
2262 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2263 len = (int)(random() % lengths[li]);
2267 memset(aval, nameseq & 0xff, len);
2268 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2273 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2279 awrite_f(int opno, long r)
2282 do_aio_rw(opno, r, O_WRONLY);
2287 bulkstat_f(int opno, long r)
2293 struct xfs_bstat *t;
2295 struct xfs_fsop_bulkreq bsr;
2298 nent = (r % 999) + 2;
2299 t = malloc(nent * sizeof(*t));
2300 fd = open(".", O_RDONLY);
2308 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2312 printf("%d/%d: bulkstat nent %d total %lld\n",
2313 procid, opno, nent, (long long)total);
2318 bulkstat1_f(int opno, long r)
2328 struct xfs_fsop_bulkreq bsr;
2331 good = random() & 1;
2333 /* use an inode we know exists */
2335 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2336 append_pathname(&f, ".");
2337 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2342 * pick a random inode
2344 * note this can generate kernel warning messages
2345 * since bulkstat_one will read the disk block that
2346 * would contain a given inode even if that disk
2347 * block doesn't contain inodes.
2349 * this is detected later, but not until after the
2350 * warning is displayed.
2352 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2358 fd = open(".", O_RDONLY);
2364 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2366 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2367 procid, opno, good?"real":"random",
2368 verifiable_log ? -1LL : (long long)ino, e);
2373 chown_f(int opno, long r)
2383 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2384 append_pathname(&f, ".");
2385 u = (uid_t)random();
2386 g = (gid_t)random();
2387 nbits = (int)(random() % idmodulo);
2388 u &= (1 << nbits) - 1;
2389 g &= (1 << nbits) - 1;
2390 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2393 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2397 /* reflink some arbitrary range of f1 to f2. */
2404 struct file_clone_range fcr;
2405 struct pathname fpath1;
2406 struct pathname fpath2;
2407 struct stat64 stat1;
2408 struct stat64 stat2;
2409 char inoinfo1[1024];
2410 char inoinfo2[1024];
2424 init_pathname(&fpath1);
2425 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2427 printf("%d/%d: clonerange read - no filename\n",
2432 init_pathname(&fpath2);
2433 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2435 printf("%d/%d: clonerange write - no filename\n",
2441 fd1 = open_path(&fpath1, O_RDONLY);
2442 e = fd1 < 0 ? errno : 0;
2446 printf("%d/%d: clonerange read - open %s failed %d\n",
2447 procid, opno, fpath1.path, e);
2451 fd2 = open_path(&fpath2, O_WRONLY);
2452 e = fd2 < 0 ? errno : 0;
2456 printf("%d/%d: clonerange write - open %s failed %d\n",
2457 procid, opno, fpath2.path, e);
2461 /* Get file stats */
2462 if (fstat64(fd1, &stat1) < 0) {
2464 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2465 procid, opno, fpath1.path, errno);
2468 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2470 if (fstat64(fd2, &stat2) < 0) {
2472 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2473 procid, opno, fpath2.path, errno);
2476 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2478 /* Calculate offsets */
2479 len = (random() % FILELEN_MAX) + 1;
2480 len &= ~(stat1.st_blksize - 1);
2482 len = stat1.st_blksize;
2483 if (len > stat1.st_size)
2484 len = stat1.st_size;
2486 lr = ((int64_t)random() << 32) + random();
2487 if (stat1.st_size == len)
2490 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2492 off1 &= ~(stat1.st_blksize - 1);
2495 * If srcfile == destfile, randomly generate destination ranges
2496 * until we find one that doesn't overlap the source range.
2498 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2500 lr = ((int64_t)random() << 32) + random();
2501 off2 = (off64_t)(lr % max_off2);
2503 off2 &= ~(stat2.st_blksize - 1);
2504 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2506 /* Clone data blocks */
2508 fcr.src_offset = off1;
2509 fcr.src_length = len;
2510 fcr.dest_offset = off2;
2512 ret = ioctl(fd2, FICLONERANGE, &fcr);
2513 e = ret < 0 ? errno : 0;
2515 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2517 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2518 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2521 printf(" error %d", e);
2530 free_pathname(&fpath2);
2532 free_pathname(&fpath1);
2536 /* copy some arbitrary range of f1 to f2. */
2542 #ifdef HAVE_COPY_FILE_RANGE
2543 struct pathname fpath1;
2544 struct pathname fpath2;
2545 struct stat64 stat1;
2546 struct stat64 stat2;
2547 char inoinfo1[1024];
2548 char inoinfo2[1024];
2566 init_pathname(&fpath1);
2567 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2569 printf("%d/%d: copyrange read - no filename\n",
2574 init_pathname(&fpath2);
2575 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2577 printf("%d/%d: copyrange write - no filename\n",
2583 fd1 = open_path(&fpath1, O_RDONLY);
2584 e = fd1 < 0 ? errno : 0;
2588 printf("%d/%d: copyrange read - open %s failed %d\n",
2589 procid, opno, fpath1.path, e);
2593 fd2 = open_path(&fpath2, O_WRONLY);
2594 e = fd2 < 0 ? errno : 0;
2598 printf("%d/%d: copyrange write - open %s failed %d\n",
2599 procid, opno, fpath2.path, e);
2603 /* Get file stats */
2604 if (fstat64(fd1, &stat1) < 0) {
2606 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2607 procid, opno, fpath1.path, errno);
2610 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2612 if (fstat64(fd2, &stat2) < 0) {
2614 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2615 procid, opno, fpath2.path, errno);
2618 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2620 /* Calculate offsets */
2621 len = (random() % FILELEN_MAX) + 1;
2623 len = stat1.st_blksize;
2624 if (len > stat1.st_size)
2625 len = stat1.st_size;
2627 lr = ((int64_t)random() << 32) + random();
2628 if (stat1.st_size == len)
2631 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2635 * If srcfile == destfile, randomly generate destination ranges
2636 * until we find one that doesn't overlap the source range.
2638 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2640 lr = ((int64_t)random() << 32) + random();
2641 off2 = (off64_t)(lr % max_off2);
2643 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2646 * Since len, off1 and off2 will be changed later, preserve their
2654 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2657 if (errno != EAGAIN || tries++ >= 300)
2659 } else if (ret > len || ret == 0)
2664 e = ret < 0 ? errno : 0;
2666 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2668 fpath1.path, inoinfo1,
2669 (long long)offset1, (long long)length,
2670 fpath2.path, inoinfo2,
2671 (long long)offset2, (long long)length);
2674 printf(" error %d", e);
2675 else if (len && ret > len)
2676 printf(" asked for %lld, copied %lld??\n",
2677 (long long)len, (long long)ret);
2686 free_pathname(&fpath2);
2688 free_pathname(&fpath1);
2692 /* dedupe some arbitrary range of f1 to f2...fn. */
2698 #ifdef FIDEDUPERANGE
2699 #define INFO_SZ 1024
2700 struct file_dedupe_range *fdr;
2701 struct pathname *fpath;
2702 struct stat64 *stat;
2714 if (flist[FT_REG].nfiles < 2)
2717 /* Pick somewhere between 2 and 128 files. */
2719 nr = random() % (flist[FT_REG].nfiles + 1);
2720 } while (nr < 2 || nr > 128);
2723 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2724 sizeof(struct file_dedupe_range));
2726 printf("%d/%d: line %d error %d\n",
2727 procid, opno, __LINE__, errno);
2730 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2731 sizeof(struct file_dedupe_range)));
2733 fpath = calloc(nr, sizeof(struct pathname));
2735 printf("%d/%d: line %d error %d\n",
2736 procid, opno, __LINE__, errno);
2740 stat = calloc(nr, sizeof(struct stat64));
2742 printf("%d/%d: line %d error %d\n",
2743 procid, opno, __LINE__, errno);
2747 info = calloc(nr, INFO_SZ);
2749 printf("%d/%d: line %d error %d\n",
2750 procid, opno, __LINE__, errno);
2754 off = calloc(nr, sizeof(off64_t));
2756 printf("%d/%d: line %d error %d\n",
2757 procid, opno, __LINE__, errno);
2761 v = calloc(nr, sizeof(int));
2763 printf("%d/%d: line %d error %d\n",
2764 procid, opno, __LINE__, errno);
2767 fd = calloc(nr, sizeof(int));
2769 printf("%d/%d: line %d error %d\n",
2770 procid, opno, __LINE__, errno);
2773 memset(fd, 0xFF, nr * sizeof(int));
2775 /* Get paths for all files */
2776 for (i = 0; i < nr; i++)
2777 init_pathname(&fpath[i]);
2779 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2781 printf("%d/%d: deduperange read - no filename\n",
2786 for (i = 1; i < nr; i++) {
2787 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2789 printf("%d/%d: deduperange write - no filename\n",
2796 fd[0] = open_path(&fpath[0], O_RDONLY);
2797 e = fd[0] < 0 ? errno : 0;
2801 printf("%d/%d: deduperange read - open %s failed %d\n",
2802 procid, opno, fpath[0].path, e);
2806 for (i = 1; i < nr; i++) {
2807 fd[i] = open_path(&fpath[i], O_WRONLY);
2808 e = fd[i] < 0 ? errno : 0;
2812 printf("%d/%d: deduperange write - open %s failed %d\n",
2813 procid, opno, fpath[i].path, e);
2818 /* Get file stats */
2819 if (fstat64(fd[0], &stat[0]) < 0) {
2821 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2822 procid, opno, fpath[0].path, errno);
2826 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2828 for (i = 1; i < nr; i++) {
2829 if (fstat64(fd[i], &stat[i]) < 0) {
2831 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2832 procid, opno, fpath[i].path, errno);
2835 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2838 /* Never try to dedupe more than half of the src file. */
2839 len = (random() % FILELEN_MAX) + 1;
2840 len &= ~(stat[0].st_blksize - 1);
2842 len = stat[0].st_blksize / 2;
2843 if (len > stat[0].st_size / 2)
2844 len = stat[0].st_size / 2;
2846 /* Calculate offsets */
2847 lr = ((int64_t)random() << 32) + random();
2848 if (stat[0].st_size == len)
2851 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2853 off[0] &= ~(stat[0].st_blksize - 1);
2856 * If srcfile == destfile[i], randomly generate destination ranges
2857 * until we find one that doesn't overlap the source range.
2859 for (i = 1; i < nr; i++) {
2863 lr = ((int64_t)random() << 32) + random();
2864 if (stat[i].st_size <= len)
2867 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2869 off[i] &= ~(stat[i].st_blksize - 1);
2870 } while (stat[0].st_ino == stat[i].st_ino &&
2871 llabs(off[i] - off[0]) < len &&
2875 /* Clone data blocks */
2876 fdr->src_offset = off[0];
2877 fdr->src_length = len;
2878 fdr->dest_count = nr - 1;
2879 for (i = 1; i < nr; i++) {
2880 fdr->info[i - 1].dest_fd = fd[i];
2881 fdr->info[i - 1].dest_offset = off[i];
2884 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2885 e = ret < 0 ? errno : 0;
2887 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2889 fpath[0].path, &info[0], (long long)off[0],
2892 printf(" error %d", e);
2898 for (i = 1; i < nr; i++) {
2899 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2901 printf("%d/%d: ...to %s%s [%lld,%lld]",
2903 fpath[i].path, &info[i * INFO_SZ],
2904 (long long)off[i], (long long)len);
2905 if (fdr->info[i - 1].status < 0)
2906 printf(" error %d", e);
2907 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2908 printf(" %llu bytes deduplicated",
2909 fdr->info[i - 1].bytes_deduped);
2910 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2911 printf(" differed");
2917 for (i = 0; i < nr; i++)
2921 for (i = 0; i < nr; i++)
2922 free_pathname(&fpath[i]);
2941 setxattr_f(int opno, long r)
2943 #ifdef XFS_XFLAG_EXTSIZE
2953 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2954 append_pathname(&f, ".");
2955 fd = open_path(&f, O_RDWR);
2956 e = fd < 0 ? errno : 0;
2961 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2962 nbits = (int)(random() % e);
2963 p &= (1 << nbits) - 1;
2965 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2967 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2970 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2977 splice_f(int opno, long r)
2979 struct pathname fpath1;
2980 struct pathname fpath2;
2981 struct stat64 stat1;
2982 struct stat64 stat2;
2983 char inoinfo1[1024];
2984 char inoinfo2[1024];
2988 loff_t offset1, offset2;
2995 ssize_t ret1 = 0, ret2 = 0;
3001 init_pathname(&fpath1);
3002 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
3004 printf("%d/%d: splice read - no filename\n",
3009 init_pathname(&fpath2);
3010 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
3012 printf("%d/%d: splice write - no filename\n",
3018 fd1 = open_path(&fpath1, O_RDONLY);
3019 e = fd1 < 0 ? errno : 0;
3023 printf("%d/%d: splice read - open %s failed %d\n",
3024 procid, opno, fpath1.path, e);
3028 fd2 = open_path(&fpath2, O_WRONLY);
3029 e = fd2 < 0 ? errno : 0;
3033 printf("%d/%d: splice write - open %s failed %d\n",
3034 procid, opno, fpath2.path, e);
3038 /* Get file stats */
3039 if (fstat64(fd1, &stat1) < 0) {
3041 printf("%d/%d: splice read - fstat64 %s failed %d\n",
3042 procid, opno, fpath1.path, errno);
3045 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
3047 if (fstat64(fd2, &stat2) < 0) {
3049 printf("%d/%d: splice write - fstat64 %s failed %d\n",
3050 procid, opno, fpath2.path, errno);
3053 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
3055 /* Calculate offsets */
3056 len = (random() % FILELEN_MAX) + 1;
3058 len = stat1.st_blksize;
3059 if (len > stat1.st_size)
3060 len = stat1.st_size;
3062 lr = ((int64_t)random() << 32) + random();
3063 if (stat1.st_size == len)
3066 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
3070 * splice can overlap write, so the offset of the target file can be
3071 * any number. But to avoid too large offset, add a clamp of 1024 blocks
3072 * past the current dest file EOF
3074 lr = ((int64_t)random() << 32) + random();
3075 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3078 * Since len, off1 and off2 will be changed later, preserve their
3085 /* Pipe initialize */
3086 if (pipe(filedes) < 0) {
3088 printf("%d/%d: splice - pipe failed %d\n",
3089 procid, opno, errno);
3097 /* move to pipe buffer */
3098 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3104 /* move from pipe buffer to dst file */
3106 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3119 if (ret1 < 0 || ret2 < 0)
3124 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3126 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3127 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3129 if (length && length > total)
3130 printf(" asked for %lld, spliced %lld??\n",
3131 (long long)length, (long long)total);
3142 free_pathname(&fpath2);
3144 free_pathname(&fpath1);
3148 creat_f(int opno, long r)
3163 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
3168 e1 = (random() % 100);
3169 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3171 if (type == FT_RTF) /* rt always gets an extsize */
3172 extsize = (random() % 10) + 1;
3173 else if (e1 < 10) /* one-in-ten get an extsize */
3174 extsize = random() % 1024;
3178 e = generate_fname(fep, type, &f, &id, &v);
3182 (void)fent_to_name(&f, fep);
3183 printf("%d/%d: creat - no filename from %s\n",
3184 procid, opno, f.path);
3189 fd = creat_path(&f, 0666);
3190 e = fd < 0 ? errno : 0;
3195 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3196 if (type == FT_RTF) {
3197 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3198 a.fsx_extsize = extsize *
3199 geom.rtextsize * geom.blocksize;
3201 } else if (extsize) {
3202 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3203 a.fsx_extsize = extsize * geom.blocksize;
3206 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3209 add_to_flist(type, id, parid, 0);
3213 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3214 extsize ? a.fsx_extsize : 0, e, e1);
3215 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3221 dread_f(int opno, long r)
3225 struct dioattr diob;
3238 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3240 printf("%d/%d: dread - no filename\n", procid, opno);
3244 fd = open_path(&f, O_RDONLY|O_DIRECT);
3245 e = fd < 0 ? errno : 0;
3249 printf("%d/%d: dread - open %s failed %d\n",
3250 procid, opno, f.path, e);
3254 if (fstat64(fd, &stb) < 0) {
3256 printf("%d/%d: dread - fstat64 %s failed %d\n",
3257 procid, opno, f.path, errno);
3262 inode_info(st, sizeof(st), &stb, v);
3263 if (stb.st_size == 0) {
3265 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3271 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3274 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3275 " fallback to stat()\n",
3276 procid, opno, f.path, st, errno);
3277 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3278 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3281 dio_env = getenv("XFS_DIO_MIN");
3283 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3285 align = (int64_t)diob.d_miniosz;
3286 lr = ((int64_t)random() << 32) + random();
3287 off = (off64_t)(lr % stb.st_size);
3288 off -= (off % align);
3289 lseek64(fd, off, SEEK_SET);
3290 len = (random() % FILELEN_MAX) + 1;
3291 len -= (len % align);
3294 else if (len > diob.d_maxiosz)
3295 len = diob.d_maxiosz;
3296 buf = memalign(diob.d_mem, len);
3297 e = read(fd, buf, len) < 0 ? errno : 0;
3300 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3301 procid, opno, f.path, st, (long long)off, (int)len, e);
3307 dwrite_f(int opno, long r)
3311 struct dioattr diob;
3324 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3326 printf("%d/%d: dwrite - no filename\n", procid, opno);
3330 fd = open_path(&f, O_WRONLY|O_DIRECT);
3331 e = fd < 0 ? errno : 0;
3335 printf("%d/%d: dwrite - open %s failed %d\n",
3336 procid, opno, f.path, e);
3340 if (fstat64(fd, &stb) < 0) {
3342 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3343 procid, opno, f.path, errno);
3348 inode_info(st, sizeof(st), &stb, v);
3349 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3351 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3352 " %s%s return %d, fallback to stat()\n",
3353 procid, opno, f.path, st, errno);
3354 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3355 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3358 dio_env = getenv("XFS_DIO_MIN");
3360 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3362 align = (int64_t)diob.d_miniosz;
3363 lr = ((int64_t)random() << 32) + random();
3364 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3365 off -= (off % align);
3366 lseek64(fd, off, SEEK_SET);
3367 len = (random() % FILELEN_MAX) + 1;
3368 len -= (len % align);
3371 else if (len > diob.d_maxiosz)
3372 len = diob.d_maxiosz;
3373 buf = memalign(diob.d_mem, len);
3375 lseek64(fd, off, SEEK_SET);
3376 memset(buf, nameseq & 0xff, len);
3377 e = write(fd, buf, len) < 0 ? errno : 0;
3380 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3381 procid, opno, f.path, st, (long long)off, (int)len, e);
3387 #ifdef HAVE_LINUX_FALLOC_H
3388 struct print_flags falloc_flags [] = {
3389 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3390 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3391 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3392 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3393 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3394 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3398 #define translate_falloc_flags(mode) \
3399 ({translate_flags(mode, "|", falloc_flags);})
3403 do_fallocate(int opno, long r, int mode)
3405 #ifdef HAVE_LINUX_FALLOC_H
3417 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3419 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3423 fd = open_path(&f, O_RDWR);
3426 printf("%d/%d: do_fallocate - open %s failed %d\n",
3427 procid, opno, f.path, errno);
3432 if (fstat64(fd, &stb) < 0) {
3434 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3435 procid, opno, f.path, errno);
3440 inode_info(st, sizeof(st), &stb, v);
3441 lr = ((int64_t)random() << 32) + random();
3442 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3444 len = (off64_t)(random() % (1024 * 1024));
3446 * Collapse/insert range requires off and len to be block aligned,
3447 * make it more likely to be the case.
3449 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3451 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3452 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3454 mode |= FALLOC_FL_KEEP_SIZE & random();
3455 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3457 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3458 procid, opno, translate_falloc_flags(mode),
3459 f.path, st, (long long)off, (long long)len, e);
3466 fallocate_f(int opno, long r)
3468 #ifdef HAVE_LINUX_FALLOC_H
3469 do_fallocate(opno, r, 0);
3474 fdatasync_f(int opno, long r)
3482 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3484 printf("%d/%d: fdatasync - no filename\n",
3489 fd = open_path(&f, O_WRONLY);
3490 e = fd < 0 ? errno : 0;
3494 printf("%d/%d: fdatasync - open %s failed %d\n",
3495 procid, opno, f.path, e);
3499 e = fdatasync(fd) < 0 ? errno : 0;
3501 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3506 #ifdef HAVE_LINUX_FIEMAP_H
3507 struct print_flags fiemap_flags[] = {
3508 { FIEMAP_FLAG_SYNC, "SYNC"},
3509 { FIEMAP_FLAG_XATTR, "XATTR"},
3513 #define translate_fiemap_flags(mode) \
3514 ({translate_flags(mode, "|", fiemap_flags);})
3518 fiemap_f(int opno, long r)
3520 #ifdef HAVE_LINUX_FIEMAP_H
3530 struct fiemap *fiemap;
3533 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3535 printf("%d/%d: fiemap - no filename\n", procid, opno);
3539 fd = open_path(&f, O_RDWR);
3540 e = fd < 0 ? errno : 0;
3544 printf("%d/%d: fiemap - open %s failed %d\n",
3545 procid, opno, f.path, e);
3549 if (fstat64(fd, &stb) < 0) {
3551 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3552 procid, opno, f.path, errno);
3557 inode_info(st, sizeof(st), &stb, v);
3558 blocks_to_map = random() & 0xffff;
3559 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3560 (blocks_to_map * sizeof(struct fiemap_extent)));
3563 printf("%d/%d: malloc failed \n", procid, opno);
3568 lr = ((int64_t)random() << 32) + random();
3569 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3571 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3572 fiemap->fm_extent_count = blocks_to_map;
3573 fiemap->fm_mapped_extents = random() & 0xffff;
3574 fiemap->fm_start = off;
3575 fiemap->fm_length = ((int64_t)random() << 32) + random();
3577 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3579 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3580 procid, opno, f.path, st, (long long)fiemap->fm_start,
3581 (long long) fiemap->fm_length,
3582 translate_fiemap_flags(fiemap->fm_flags), e);
3590 freesp_f(int opno, long r)
3595 struct xfs_flock64 fl;
3603 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3605 printf("%d/%d: freesp - no filename\n", procid, opno);
3609 fd = open_path(&f, O_RDWR);
3610 e = fd < 0 ? errno : 0;
3614 printf("%d/%d: freesp - open %s failed %d\n",
3615 procid, opno, f.path, e);
3619 if (fstat64(fd, &stb) < 0) {
3621 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3622 procid, opno, f.path, errno);
3627 inode_info(st, sizeof(st), &stb, v);
3628 lr = ((int64_t)random() << 32) + random();
3629 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3631 fl.l_whence = SEEK_SET;
3634 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3636 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3637 procid, opno, f.path, st, (long long)off, e);
3643 fsync_f(int opno, long r)
3651 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3653 printf("%d/%d: fsync - no filename\n", procid, opno);
3657 fd = open_file_or_dir(&f, O_WRONLY);
3658 e = fd < 0 ? errno : 0;
3662 printf("%d/%d: fsync - open %s failed %d\n",
3663 procid, opno, f.path, e);
3667 e = fsync(fd) < 0 ? errno : 0;
3669 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3675 gen_random_string(int len)
3677 static const char charset[] = "0123456789"
3678 "abcdefghijklmnopqrstuvwxyz"
3679 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3690 for (i = 0; i < len; i++)
3691 s[i] = charset[random() % sizeof(charset)];
3697 getattr_f(int opno, long r)
3706 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3707 append_pathname(&f, ".");
3708 fd = open_path(&f, O_RDWR);
3709 e = fd < 0 ? errno : 0;
3712 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3714 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3720 getdents_f(int opno, long r)
3727 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3728 append_pathname(&f, ".");
3729 dir = opendir_path(&f);
3733 printf("%d/%d: getdents - can't open %s\n",
3734 procid, opno, f.path);
3738 while (readdir64(dir) != NULL)
3741 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3747 getfattr_f(int opno, long r)
3753 char name[XATTR_NAME_BUF_SIZE];
3759 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3761 printf("%d/%d: getfattr - no filename\n", procid, opno);
3767 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3768 * to read a xattr that doesn't exist (fgetxattr should fail with
3769 * errno set to ENOATTR (61) in this case).
3771 if (fep->xattr_counter > 0)
3772 xattr_num = (random() % fep->xattr_counter) + 1;
3776 e = generate_xattr_name(xattr_num, name, sizeof(name));
3778 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3779 procid, opno, f.path, e);
3783 value_len = getxattr(f.path, name, NULL, 0);
3784 if (value_len < 0) {
3786 printf("%d/%d: getfattr file %s name %s failed %d\n",
3787 procid, opno, f.path, name, errno);
3791 /* A xattr without value.*/
3792 if (value_len == 0) {
3797 value = malloc(value_len);
3800 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3801 procid, opno, f.path, value_len);
3805 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3808 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3809 procid, opno, f.path, name, value_len, e);
3816 link_f(int opno, long r)
3830 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3832 printf("%d/%d: link - no file\n", procid, opno);
3836 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3842 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3846 (void)fent_to_name(&l, fep);
3847 printf("%d/%d: link - no filename from %s\n",
3848 procid, opno, l.path);
3854 e = link_path(&f, &l) < 0 ? errno : 0;
3857 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3859 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3861 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3868 listfattr_f(int opno, long r)
3874 char *buffer = NULL;
3878 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3880 printf("%d/%d: listfattr - no filename\n", procid, opno);
3885 e = listxattr(f.path, NULL, 0);
3888 printf("%d/%d: listfattr %s failed %d\n",
3889 procid, opno, f.path, errno);
3893 if (buffer_len == 0) {
3895 printf("%d/%d: listfattr %s - has no extended attributes\n",
3896 procid, opno, f.path);
3900 buffer = malloc(buffer_len);
3903 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3904 procid, opno, f.path, buffer_len);
3908 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3910 printf("%d/%d: listfattr %s buffer length %d %d\n",
3911 procid, opno, f.path, buffer_len, e);
3918 mkdir_f(int opno, long r)
3928 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3933 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3937 (void)fent_to_name(&f, fep);
3938 printf("%d/%d: mkdir - no filename from %s\n",
3939 procid, opno, f.path);
3944 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3947 add_to_flist(FT_DIR, id, parid, 0);
3949 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3950 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3956 mknod_f(int opno, long r)
3966 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3971 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3975 (void)fent_to_name(&f, fep);
3976 printf("%d/%d: mknod - no filename from %s\n",
3977 procid, opno, f.path);
3982 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3985 add_to_flist(FT_DEV, id, parid, 0);
3987 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3988 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3993 #ifdef HAVE_SYS_MMAN_H
3994 struct print_flags mmap_flags[] = {
3995 { MAP_SHARED, "SHARED"},
3996 { MAP_PRIVATE, "PRIVATE"},
4000 #define translate_mmap_flags(flags) \
4001 ({translate_flags(flags, "|", mmap_flags);})
4005 do_mmap(int opno, long r, int prot)
4007 #ifdef HAVE_SYS_MMAN_H
4019 sigjmp_buf sigbus_jmpbuf;
4022 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4024 printf("%d/%d: do_mmap - no filename\n", procid, opno);
4028 fd = open_path(&f, O_RDWR);
4029 e = fd < 0 ? errno : 0;
4033 printf("%d/%d: do_mmap - open %s failed %d\n",
4034 procid, opno, f.path, e);
4038 if (fstat64(fd, &stb) < 0) {
4040 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
4041 procid, opno, f.path, errno);
4046 inode_info(st, sizeof(st), &stb, v);
4047 if (stb.st_size == 0) {
4049 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
4056 lr = ((int64_t)random() << 32) + random();
4057 off = (off64_t)(lr % stb.st_size);
4058 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
4059 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
4061 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
4062 addr = mmap(NULL, len, prot, flags, fd, off);
4063 e = (addr == MAP_FAILED) ? errno : 0;
4066 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
4067 procid, opno, f.path, st, (long long)off,
4068 (int)len, translate_mmap_flags(flags), e);
4074 if (prot & PROT_WRITE) {
4075 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4076 sigbus_jmp = &sigbus_jmpbuf;
4077 memset(addr, nameseq & 0xff, len);
4081 if ((buf = malloc(len)) != NULL) {
4082 memcpy(buf, addr, len);
4087 /* set NULL to stop other functions from doing siglongjmp */
4091 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4092 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4093 f.path, st, (long long)off, (int)len,
4094 translate_mmap_flags(flags),
4095 (e == 0) ? "0" : "Bus error");
4103 mread_f(int opno, long r)
4105 #ifdef HAVE_SYS_MMAN_H
4106 do_mmap(opno, r, PROT_READ);
4111 mwrite_f(int opno, long r)
4113 #ifdef HAVE_SYS_MMAN_H
4114 do_mmap(opno, r, PROT_WRITE);
4119 punch_f(int opno, long r)
4121 #ifdef HAVE_LINUX_FALLOC_H
4122 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4127 zero_f(int opno, long r)
4129 #ifdef HAVE_LINUX_FALLOC_H
4130 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4135 collapse_f(int opno, long r)
4137 #ifdef HAVE_LINUX_FALLOC_H
4138 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4143 insert_f(int opno, long r)
4145 #ifdef HAVE_LINUX_FALLOC_H
4146 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4151 read_f(int opno, long r)
4165 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4167 printf("%d/%d: read - no filename\n", procid, opno);
4171 fd = open_path(&f, O_RDONLY);
4172 e = fd < 0 ? errno : 0;
4176 printf("%d/%d: read - open %s failed %d\n",
4177 procid, opno, f.path, e);
4181 if (fstat64(fd, &stb) < 0) {
4183 printf("%d/%d: read - fstat64 %s failed %d\n",
4184 procid, opno, f.path, errno);
4189 inode_info(st, sizeof(st), &stb, v);
4190 if (stb.st_size == 0) {
4192 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4198 lr = ((int64_t)random() << 32) + random();
4199 off = (off64_t)(lr % stb.st_size);
4200 lseek64(fd, off, SEEK_SET);
4201 len = (random() % FILELEN_MAX) + 1;
4203 e = read(fd, buf, len) < 0 ? errno : 0;
4206 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4207 procid, opno, f.path, st, (long long)off, (int)len, e);
4213 readlink_f(int opno, long r)
4221 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4223 printf("%d/%d: readlink - no filename\n", procid, opno);
4227 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4230 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4235 readv_f(int opno, long r)
4247 struct iovec *iov = NULL;
4254 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4256 printf("%d/%d: readv - no filename\n", procid, opno);
4260 fd = open_path(&f, O_RDONLY);
4261 e = fd < 0 ? errno : 0;
4265 printf("%d/%d: readv - open %s failed %d\n",
4266 procid, opno, f.path, e);
4270 if (fstat64(fd, &stb) < 0) {
4272 printf("%d/%d: readv - fstat64 %s failed %d\n",
4273 procid, opno, f.path, errno);
4278 inode_info(st, sizeof(st), &stb, v);
4279 if (stb.st_size == 0) {
4281 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4287 lr = ((int64_t)random() << 32) + random();
4288 off = (off64_t)(lr % stb.st_size);
4289 lseek64(fd, off, SEEK_SET);
4290 len = (random() % FILELEN_MAX) + 1;
4293 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4294 iov = calloc(iovcnt, sizeof(struct iovec));
4295 iovl = len / iovcnt;
4297 for (i=0; i<iovcnt; i++) {
4298 (iov + i)->iov_base = (buf + iovb);
4299 (iov + i)->iov_len = iovl;
4303 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4306 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4307 procid, opno, f.path, st, (long long)off, (int)iovl,
4314 removefattr_f(int opno, long r)
4320 char name[XATTR_NAME_BUF_SIZE];
4324 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4326 printf("%d/%d: removefattr - no filename\n", procid, opno);
4332 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4333 * remove a xattr that doesn't exist (fremovexattr should fail with
4334 * errno set to ENOATTR (61) in this case).
4336 if (fep->xattr_counter > 0)
4337 xattr_num = (random() % fep->xattr_counter) + 1;
4341 e = generate_xattr_name(xattr_num, name, sizeof(name));
4343 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4344 procid, opno, f.path, e);
4348 e = removexattr(f.path, name) < 0 ? errno : 0;
4350 printf("%d/%d: removefattr file %s name %s %d\n",
4351 procid, opno, f.path, name, e);
4356 struct print_flags renameat2_flags [] = {
4357 { RENAME_NOREPLACE, "NOREPLACE"},
4358 { RENAME_EXCHANGE, "EXCHANGE"},
4359 { RENAME_WHITEOUT, "WHITEOUT"},
4363 #define translate_renameat2_flags(mode) \
4364 ({translate_flags(mode, "|", renameat2_flags);})
4367 do_renameat2(int opno, long r, int mode)
4383 /* get an existing path for the source of the rename */
4385 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4386 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4388 printf("%d/%d: rename - no source filename\n",
4395 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4396 * order to maintain filelist/filename integrity, we should
4397 * restrict exchange operation to files of the same type.
4399 if (mode == RENAME_EXCHANGE) {
4400 which = 1 << (flp - flist);
4401 init_pathname(&newf);
4402 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4404 printf("%d/%d: rename - no target filename\n",
4406 free_pathname(&newf);
4410 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4411 fents_ancestor_check(dfep, fep))) {
4413 printf("%d/%d: rename(REXCHANGE) %s and %s "
4414 "have ancestor-descendant relationship\n",
4415 procid, opno, f.path, newf.path);
4416 free_pathname(&newf);
4422 parid = dfep->parent;
4425 * Get an existing directory for the destination parent
4428 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4435 * Generate a new path using an existing parent directory
4438 init_pathname(&newf);
4439 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4443 (void)fent_to_name(&f, dfep);
4444 printf("%d/%d: rename - no filename from %s\n",
4445 procid, opno, f.path);
4447 free_pathname(&newf);
4452 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4455 int xattr_counter = fep->xattr_counter;
4456 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4457 int ft = flp - flist;
4460 oldparid = fep->parent;
4463 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4464 * old parent id for the others.
4466 if (ft == FT_DIR || ft == FT_SUBVOL)
4467 fix_parent(oldid, id, swap);
4469 if (mode == RENAME_WHITEOUT) {
4470 fep->xattr_counter = 0;
4471 add_to_flist(flp - flist, id, parid, xattr_counter);
4472 } else if (mode == RENAME_EXCHANGE) {
4473 fep->xattr_counter = dfep->xattr_counter;
4474 dfep->xattr_counter = xattr_counter;
4476 del_from_flist(flp - flist, fep - flp->fents);
4477 add_to_flist(flp - flist, id, parid, xattr_counter);
4481 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4482 opno, translate_renameat2_flags(mode), f.path,
4485 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4486 procid, opno, oldid, oldparid);
4487 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4488 procid, opno, id, parid);
4491 free_pathname(&newf);
4496 rename_f(int opno, long r)
4498 do_renameat2(opno, r, 0);
4502 rnoreplace_f(int opno, long r)
4504 do_renameat2(opno, r, RENAME_NOREPLACE);
4508 rexchange_f(int opno, long r)
4510 do_renameat2(opno, r, RENAME_EXCHANGE);
4514 rwhiteout_f(int opno, long r)
4516 do_renameat2(opno, r, RENAME_WHITEOUT);
4520 resvsp_f(int opno, long r)
4525 struct xfs_flock64 fl;
4533 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4535 printf("%d/%d: resvsp - no filename\n", procid, opno);
4539 fd = open_path(&f, O_RDWR);
4540 e = fd < 0 ? errno : 0;
4544 printf("%d/%d: resvsp - open %s failed %d\n",
4545 procid, opno, f.path, e);
4549 if (fstat64(fd, &stb) < 0) {
4551 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4552 procid, opno, f.path, errno);
4557 inode_info(st, sizeof(st), &stb, v);
4558 lr = ((int64_t)random() << 32) + random();
4559 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4561 fl.l_whence = SEEK_SET;
4563 fl.l_len = (off64_t)(random() % (1024 * 1024));
4564 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4566 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4567 procid, opno, f.path, st,
4568 (long long)off, (long long)fl.l_len, e);
4574 rmdir_f(int opno, long r)
4584 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4586 printf("%d/%d: rmdir - no directory\n", procid, opno);
4590 e = rmdir_path(&f) < 0 ? errno : 0;
4594 oldparid = fep->parent;
4595 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4598 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4600 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4601 procid, opno, oldid, oldparid);
4607 setattr_f(int opno, long r)
4616 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4617 append_pathname(&f, ".");
4618 fd = open_path(&f, O_RDWR);
4619 e = fd < 0 ? errno : 0;
4622 fl = attr_mask & (uint)random();
4623 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4625 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4631 setfattr_f(int opno, long r)
4638 char name[XATTR_NAME_BUF_SIZE];
4644 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4646 printf("%d/%d: setfattr - no filename\n", procid, opno);
4651 if ((fep->xattr_counter > 0) && (random() % 2)) {
4653 * Use an existing xattr name for replacing its value or
4654 * create again a xattr that was previously deleted.
4656 xattr_num = (random() % fep->xattr_counter) + 1;
4658 flag = XATTR_REPLACE;
4660 /* Use a new xattr name. */
4661 xattr_num = fep->xattr_counter + 1;
4663 * Don't always use the create flag because even if our xattr
4664 * counter is 0, we may still have xattrs associated to this
4665 * file (this happens when xattrs are added to a file through
4666 * one of its other hard links), so we can end up updating an
4667 * existing xattr too.
4670 flag = XATTR_CREATE;
4674 * The maximum supported value size depends on the filesystem
4675 * implementation, but 100 bytes is a safe value for most filesystems
4678 value_len = random() % 101;
4679 value = gen_random_string(value_len);
4680 if (!value && value_len > 0) {
4682 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4683 procid, opno, f.path, value_len);
4686 e = generate_xattr_name(xattr_num, name, sizeof(name));
4688 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4689 procid, opno, f.path, e);
4693 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4695 fep->xattr_counter++;
4697 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4698 procid, opno, f.path, name, xattr_flag_to_string(flag),
4706 stat_f(int opno, long r)
4714 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4716 printf("%d/%d: stat - no entries\n", procid, opno);
4720 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4723 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4728 subvol_create_f(int opno, long r)
4730 #ifdef HAVE_BTRFSUTIL_H
4731 enum btrfs_util_error e;
4741 if (!get_fname(FT_DIRm | FT_SUBVOLm, r, NULL, NULL, &fep, &v))
4745 err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
4749 (void)fent_to_name(&f, fep);
4750 printf("%d/%d: subvol_create - no filename from %s\n",
4751 procid, opno, f.path);
4756 e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
4757 if (e == BTRFS_UTIL_OK)
4758 add_to_flist(FT_SUBVOL, id, parid, 0);
4760 printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
4761 f.path, e, btrfs_util_strerror(e));
4762 printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
4770 subvol_delete_f(int opno, long r)
4772 #ifdef HAVE_BTRFSUTIL_H
4773 enum btrfs_util_error e;
4781 if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
4783 printf("%d:%d: subvol_delete - no subvolume\n", procid,
4788 e = btrfs_util_delete_subvolume(f.path, 0);
4790 if (e == BTRFS_UTIL_OK) {
4792 oldparid = fep->parent;
4793 delete_subvol_children(oldid);
4794 del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
4797 printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
4798 e, btrfs_util_strerror(e));
4799 if (e == BTRFS_UTIL_OK)
4800 printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
4801 procid, opno, oldid, oldparid);
4808 symlink_f(int opno, long r)
4821 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
4826 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4830 (void)fent_to_name(&f, fep);
4831 printf("%d/%d: symlink - no filename from %s\n",
4832 procid, opno, f.path);
4837 len = (int)(random() % PATH_MAX);
4838 val = malloc(len + 1);
4840 memset(val, 'x', len);
4842 for (i = 10; i < len - 1; i += 10)
4844 e = symlink_path(val, &f) < 0 ? errno : 0;
4847 add_to_flist(FT_SYM, id, parid, 0);
4850 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4851 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4858 sync_f(int opno, long r)
4862 printf("%d/%d: sync\n", procid, opno);
4866 truncate_f(int opno, long r)
4877 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4879 printf("%d/%d: truncate - no filename\n", procid, opno);
4883 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4887 printf("%d/%d: truncate - stat64 %s failed %d\n",
4888 procid, opno, f.path, e);
4892 inode_info(st, sizeof(st), &stb, v);
4893 lr = ((int64_t)random() << 32) + random();
4894 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4896 e = truncate64_path(&f, off) < 0 ? errno : 0;
4899 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4900 st, (long long)off, e);
4905 unlink_f(int opno, long r)
4916 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4918 printf("%d/%d: unlink - no file\n", procid, opno);
4922 e = unlink_path(&f) < 0 ? errno : 0;
4926 oldparid = fep->parent;
4927 del_from_flist(flp - flist, fep - flp->fents);
4930 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4932 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4933 procid, opno, oldid, oldparid);
4939 unresvsp_f(int opno, long r)
4944 struct xfs_flock64 fl;
4952 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4954 printf("%d/%d: unresvsp - no filename\n", procid, opno);
4958 fd = open_path(&f, O_RDWR);
4959 e = fd < 0 ? errno : 0;
4963 printf("%d/%d: unresvsp - open %s failed %d\n",
4964 procid, opno, f.path, e);
4968 if (fstat64(fd, &stb) < 0) {
4970 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4971 procid, opno, f.path, errno);
4976 inode_info(st, sizeof(st), &stb, v);
4977 lr = ((int64_t)random() << 32) + random();
4978 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4980 fl.l_whence = SEEK_SET;
4982 fl.l_len = (off64_t)(random() % (1 << 20));
4983 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4985 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4986 procid, opno, f.path, st,
4987 (long long)off, (long long)fl.l_len, e);
4993 write_f(int opno, long r)
5007 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5009 printf("%d/%d: write - no filename\n", procid, opno);
5013 fd = open_path(&f, O_WRONLY);
5014 e = fd < 0 ? errno : 0;
5018 printf("%d/%d: write - open %s failed %d\n",
5019 procid, opno, f.path, e);
5023 if (fstat64(fd, &stb) < 0) {
5025 printf("%d/%d: write - fstat64 %s failed %d\n",
5026 procid, opno, f.path, errno);
5031 inode_info(st, sizeof(st), &stb, v);
5032 lr = ((int64_t)random() << 32) + random();
5033 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5035 lseek64(fd, off, SEEK_SET);
5036 len = (random() % FILELEN_MAX) + 1;
5038 memset(buf, nameseq & 0xff, len);
5039 e = write(fd, buf, len) < 0 ? errno : 0;
5042 printf("%d/%d: write %s%s [%lld,%d] %d\n",
5043 procid, opno, f.path, st, (long long)off, (int)len, e);
5049 writev_f(int opno, long r)
5061 struct iovec *iov = NULL;
5068 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
5070 printf("%d/%d: writev - no filename\n", procid, opno);
5074 fd = open_path(&f, O_WRONLY);
5075 e = fd < 0 ? errno : 0;
5079 printf("%d/%d: writev - open %s failed %d\n",
5080 procid, opno, f.path, e);
5084 if (fstat64(fd, &stb) < 0) {
5086 printf("%d/%d: writev - fstat64 %s failed %d\n",
5087 procid, opno, f.path, errno);
5092 inode_info(st, sizeof(st), &stb, v);
5093 lr = ((int64_t)random() << 32) + random();
5094 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
5096 lseek64(fd, off, SEEK_SET);
5097 len = (random() % FILELEN_MAX) + 1;
5099 memset(buf, nameseq & 0xff, len);
5101 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
5102 iov = calloc(iovcnt, sizeof(struct iovec));
5103 iovl = len / iovcnt;
5105 for (i=0; i<iovcnt; i++) {
5106 (iov + i)->iov_base = (buf + iovb);
5107 (iov + i)->iov_len = iovl;
5111 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
5115 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
5116 procid, opno, f.path, st, (long long)off, (int)iovl,
5123 xattr_flag_to_string(int flag)
5125 if (flag == XATTR_CREATE)
5127 if (flag == XATTR_REPLACE)