1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
13 #ifdef HAVE_ATTR_ATTRIBUTES_H
14 #include <attr/attributes.h>
16 #ifdef HAVE_LINUX_FIEMAP_H
17 #include <linux/fiemap.h>
19 #ifndef HAVE_ATTR_LIST
20 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
22 #ifdef HAVE_SYS_PRCTL_H
23 #include <sys/prctl.h>
29 #include <sys/syscall.h>
30 #include <sys/xattr.h>
32 #ifndef FS_IOC_GETFLAGS
33 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
35 #ifndef FS_IOC_SETFLAGS
36 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
40 #define XFS_ERRTAG_MAX 17
41 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
42 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
47 #ifndef HAVE_RENAMEAT2
48 #if !defined(SYS_renameat2) && defined(__x86_64__)
49 #define SYS_renameat2 316
52 #if !defined(SYS_renameat2) && defined(__i386__)
53 #define SYS_renameat2 353
56 static int renameat2(int dfd1, const char *path1,
57 int dfd2, const char *path2,
61 return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
69 #ifndef RENAME_NOREPLACE
70 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
72 #ifndef RENAME_EXCHANGE
73 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
75 #ifndef RENAME_WHITEOUT
76 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
79 #define FILELEN_MAX (32*4096)
140 typedef void (*opfnc_t)(int, long);
142 typedef struct opdesc {
150 typedef struct fent {
156 typedef struct flist {
163 typedef struct pathname {
173 struct print_string {
180 #define FT_DIRm (1 << FT_DIR)
182 #define FT_REGm (1 << FT_REG)
184 #define FT_SYMm (1 << FT_SYM)
186 #define FT_DEVm (1 << FT_DEV)
188 #define FT_RTFm (1 << FT_RTF)
190 #define FT_ANYm ((1 << FT_nft) - 1)
191 #define FT_REGFILE (FT_REGm | FT_RTFm)
192 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
194 #define FLIST_SLOT_INCR 16
197 #define MAXFSIZE ((1ULL << 63) - 1ULL)
198 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
200 #define XATTR_NAME_BUF_SIZE 18
202 void afsync_f(int, long);
203 void allocsp_f(int, long);
204 void aread_f(int, long);
205 void attr_remove_f(int, long);
206 void attr_set_f(int, long);
207 void awrite_f(int, long);
208 void bulkstat_f(int, long);
209 void bulkstat1_f(int, long);
210 void chown_f(int, long);
211 void clonerange_f(int, long);
212 void copyrange_f(int, long);
213 void creat_f(int, long);
214 void deduperange_f(int, long);
215 void dread_f(int, long);
216 void dwrite_f(int, long);
217 void fallocate_f(int, long);
218 void fdatasync_f(int, long);
219 void fiemap_f(int, long);
220 void freesp_f(int, long);
221 void fsync_f(int, long);
222 char *gen_random_string(int);
223 void getattr_f(int, long);
224 void getdents_f(int, long);
225 void getfattr_f(int, long);
226 void link_f(int, long);
227 void listfattr_f(int, long);
228 void mkdir_f(int, long);
229 void mknod_f(int, long);
230 void mread_f(int, long);
231 void mwrite_f(int, long);
232 void punch_f(int, long);
233 void zero_f(int, long);
234 void collapse_f(int, long);
235 void insert_f(int, long);
236 void read_f(int, long);
237 void readlink_f(int, long);
238 void readv_f(int, long);
239 void removefattr_f(int, long);
240 void rename_f(int, long);
241 void rnoreplace_f(int, long);
242 void rexchange_f(int, long);
243 void rwhiteout_f(int, long);
244 void resvsp_f(int, long);
245 void rmdir_f(int, long);
246 void setattr_f(int, long);
247 void setfattr_f(int, long);
248 void setxattr_f(int, long);
249 void splice_f(int, long);
250 void stat_f(int, long);
251 void symlink_f(int, long);
252 void sync_f(int, long);
253 void truncate_f(int, long);
254 void unlink_f(int, long);
255 void unresvsp_f(int, long);
256 void write_f(int, long);
257 void writev_f(int, long);
258 char *xattr_flag_to_string(int);
261 /* { OP_ENUM, "name", function, freq, iswrite }, */
262 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
263 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
264 { OP_AREAD, "aread", aread_f, 1, 0 },
265 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
266 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
267 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
268 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
269 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
270 { OP_CHOWN, "chown", chown_f, 3, 1 },
271 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
272 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
273 { OP_CREAT, "creat", creat_f, 4, 1 },
274 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
275 { OP_DREAD, "dread", dread_f, 4, 0 },
276 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
277 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
278 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
279 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
280 { OP_FREESP, "freesp", freesp_f, 1, 1 },
281 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
282 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
283 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
284 /* get extended attribute */
285 { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
286 { OP_LINK, "link", link_f, 1, 1 },
287 /* list extent attributes */
288 { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
289 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
290 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
291 { OP_MREAD, "mread", mread_f, 2, 0 },
292 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
293 { OP_PUNCH, "punch", punch_f, 1, 1 },
294 { OP_ZERO, "zero", zero_f, 1, 1 },
295 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
296 { OP_INSERT, "insert", insert_f, 1, 1 },
297 { OP_READ, "read", read_f, 1, 0 },
298 { OP_READLINK, "readlink", readlink_f, 1, 0 },
299 { OP_READV, "readv", readv_f, 1, 0 },
300 /* remove (delete) extended attribute */
301 { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
302 { OP_RENAME, "rename", rename_f, 2, 1 },
303 { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
304 { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
305 { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
306 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
307 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
308 /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
309 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
310 /* set extended attribute */
311 { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
312 /* set project id (XFS_IOC_FSSETXATTR ioctl) */
313 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
314 { OP_SPLICE, "splice", splice_f, 1, 1 },
315 { OP_STAT, "stat", stat_f, 1, 0 },
316 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
317 { OP_SYNC, "sync", sync_f, 1, 1 },
318 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
319 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
320 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
321 { OP_WRITE, "write", write_f, 4, 1 },
322 { OP_WRITEV, "writev", writev_f, 4, 1 },
325 flist_t flist[FT_nft] = {
338 struct xfs_fsop_geom geom;
349 unsigned int idmodulo = XFS_IDMODULO_MAX;
350 unsigned int attr_mask = ~0;
353 unsigned long seed = 0;
357 int verifiable_log = 0;
358 sig_atomic_t should_stop = 0;
359 sigjmp_buf *sigbus_jmp = NULL;
360 char *execute_cmd = NULL;
361 int execute_freq = 1;
362 struct print_string flag_str = {0};
364 void add_to_flist(int, int, int, int);
365 void append_pathname(pathname_t *, char *);
366 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
367 int attr_remove_path(pathname_t *, const char *, int);
368 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
369 void check_cwd(void);
370 void cleanup_flist(void);
371 int creat_path(pathname_t *, mode_t);
372 void dcache_enter(int, int);
373 void dcache_init(void);
374 fent_t *dcache_lookup(int);
375 void dcache_purge(int);
376 void del_from_flist(int, int);
377 int dirid_to_name(char *, int);
379 int fent_to_name(pathname_t *, flist_t *, fent_t *);
380 bool fents_ancestor_check(fent_t *, fent_t *);
381 void fix_parent(int, int, bool);
382 void free_pathname(pathname_t *);
383 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
384 int generate_xattr_name(int, char *, int);
385 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
386 void init_pathname(pathname_t *);
387 int lchown_path(pathname_t *, uid_t, gid_t);
388 int link_path(pathname_t *, pathname_t *);
389 int lstat64_path(pathname_t *, struct stat64 *);
390 void make_freq_table(void);
391 int mkdir_path(pathname_t *, mode_t);
392 int mknod_path(pathname_t *, mode_t, dev_t);
393 void namerandpad(int, char *, int);
394 int open_file_or_dir(pathname_t *, int);
395 int open_path(pathname_t *, int);
396 DIR *opendir_path(pathname_t *);
397 void process_freq(char *);
398 int readlink_path(pathname_t *, char *, size_t);
399 int rename_path(pathname_t *, pathname_t *, int);
400 int rmdir_path(pathname_t *);
401 void separate_pathname(pathname_t *, char *, pathname_t *);
402 void show_ops(int, char *);
403 int stat64_path(pathname_t *, struct stat64 *);
404 int symlink_path(const char *, pathname_t *);
405 int truncate64_path(pathname_t *, off64_t);
406 int unlink_path(pathname_t *);
408 void write_freq(void);
409 void zero_freq(void);
411 void sg_handler(int signum)
419 * Only handle SIGBUS when mmap write to a hole and no
420 * block can be allocated due to ENOSPC, abort otherwise.
423 siglongjmp(*sigbus_jmp, -1);
425 printf("Unknown SIGBUS is caught, Abort!\n");
428 /* should not reach here */
435 int main(int argc, char **argv)
439 char *dirname = NULL;
440 char *logname = NULL;
441 char rpath[PATH_MAX];
450 xfs_error_injection_t err_inj;
451 struct sigaction action;
453 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
455 errrange = errtag = 0;
457 nops = sizeof(ops) / sizeof(ops[0]);
458 ops_end = &ops[nops];
460 while ((c = getopt(argc, argv, allopts)) != -1) {
469 sscanf(optarg, "%d", &errtag);
473 } else if (errtag == 0)
475 if (errtag >= XFS_ERRTAG_MAX) {
477 "error tag %d too large (max %d)\n",
478 errtag, XFS_ERRTAG_MAX - 1);
483 process_freq(optarg);
486 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
487 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
490 idmodulo = strtoul(optarg, NULL, 0);
491 if (idmodulo > XFS_IDMODULO_MAX) {
493 "chown modulo %d too big (max %d)\n",
494 idmodulo, XFS_IDMODULO_MAX);
499 loops = atoi(optarg);
502 operations = atoi(optarg);
509 nproc = atoi(optarg);
515 seed = strtoul(optarg, NULL, 0);
524 execute_cmd = optarg;
530 attr_mask = strtoul(optarg, NULL, 0);
534 if (optarg[0] == 'c')
545 execute_freq = strtoul(optarg, NULL, 0);
548 fprintf(stderr, "%s - invalid parameters\n",
558 /* no directory specified */
559 if (!nousage) usage();
563 (void)mkdir(dirname, 0777);
564 if (logname && logname[0] != '/') {
565 if (!getcwd(rpath, sizeof(rpath))){
566 perror("getcwd failed");
572 if (chdir(dirname) < 0) {
577 char path[PATH_MAX + NAME_MAX + 1];
578 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
579 if (freopen(path, "a", stdout) == NULL) {
580 perror("freopen logfile failed");
584 sprintf(buf, "fss%x", (unsigned int)getpid());
585 fd = creat(buf, 0666);
586 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
587 maxfsize = (off64_t)MAXFSIZE32;
589 maxfsize = (off64_t)MAXFSIZE;
594 gettimeofday(&t, (void *)NULL);
595 seed = (int)t.tv_sec ^ (int)t.tv_usec;
596 printf("seed = %ld\n", seed);
598 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
599 if (i >= 0 && geom.rtblocks)
600 rtpct = MIN(MAX(geom.rtblocks * 100 /
601 (geom.rtblocks + geom.datablocks), 1), 99);
610 for (i = 0; i < j; i++)
613 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
619 for (i = 0; i < j; i++)
622 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
624 printf("Injecting failure on tag #%d\n", errtag);
625 err_inj.errtag = errtag;
627 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
629 perror("fsstress - XFS_SYSSGI error injection call");
638 action.sa_handler = sg_handler;
639 sigemptyset(&action.sa_mask);
641 if (sigaction(SIGTERM, &action, 0)) {
642 perror("sigaction failed");
646 for (i = 0; i < nproc; i++) {
648 sigemptyset(&action.sa_mask);
649 action.sa_handler = SIG_DFL;
650 if (sigaction(SIGTERM, &action, 0))
652 action.sa_handler = sg_handler;
653 if (sigaction(SIGBUS, &action, 0))
655 #ifdef HAVE_SYS_PRCTL_H
656 prctl(PR_SET_PDEATHSIG, SIGKILL);
657 if (getppid() == 1) /* parent died already? */
661 char path[PATH_MAX + NAME_MAX + 2 + 11];
662 snprintf(path, sizeof(path), "%s/%s.%d",
664 if (freopen(path, "a", stdout) == NULL) {
665 perror("freopen logfile failed");
671 if (io_setup(128, &io_ctx) != 0) {
672 fprintf(stderr, "io_setup failed");
676 for (i = 0; !loops || (i < loops); i++)
679 if(io_destroy(io_ctx) != 0) {
680 fprintf(stderr, "io_destroy failed");
690 while (wait(&stat) > 0 && !should_stop) {
693 action.sa_flags = SA_RESTART;
694 sigaction(SIGTERM, &action, 0);
695 kill(-getpid(), SIGTERM);
696 while (wait(&stat) > 0)
702 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
704 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
706 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
719 add_string(struct print_string *str, const char *add)
721 int len = strlen(add);
726 if (len > (str->max - 1) - str->len) {
727 str->len = str->max - 1;
731 memcpy(str->buffer + str->len, add, len);
733 str->buffer[str->len] = '\0';
739 translate_flags(int flags, const char *delim,
740 const struct print_flags *flag_array)
742 int i, mask, first = 1;
745 if (!flag_str.buffer) {
746 flag_str.buffer = malloc(4096);
750 if (!flag_str.buffer)
753 flag_str.buffer[0] = '\0';
755 for (i = 0; flag_array[i].name && flags; i++) {
756 mask = flag_array[i].mask;
757 if ((flags & mask) != mask)
760 add = flag_array[i].name;
763 add_string(&flag_str, delim);
766 add_string(&flag_str, add);
769 /* Check whether there are any leftover flags. */
775 add_string(&flag_str, delim);
777 ret = snprintf(number, 11, "0x%x", flags) > 0;
778 if (ret > 0 && ret <= 11)
779 add_string(&flag_str, number);
782 return flag_str.buffer;
786 add_to_flist(int ft, int id, int parent, int xattr_counter)
792 if (ftp->nfiles == ftp->nslots) {
793 ftp->nslots += FLIST_SLOT_INCR;
794 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
796 fep = &ftp->fents[ftp->nfiles++];
798 fep->parent = parent;
799 fep->xattr_counter = xattr_counter;
803 append_pathname(pathname_t *name, char *str)
809 /* attempting to append to a dir a zero length path */
810 if (len && *str == '/' && name->len == 0) {
811 fprintf(stderr, "fsstress: append_pathname failure\n");
812 assert(chdir(homedir) == 0);
817 name->path = realloc(name->path, name->len + 1 + len);
818 strcpy(&name->path[name->len], str);
823 attr_list_path(pathname_t *name,
825 const int buffersize,
827 attrlist_cursor_t *cursor)
829 char buf[NAME_MAX + 1];
833 if (flags != ATTR_DONTFOLLOW) {
838 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
839 if (rval >= 0 || errno != ENAMETOOLONG)
841 separate_pathname(name, buf, &newname);
842 if (chdir(buf) == 0) {
843 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
844 assert(chdir("..") == 0);
846 free_pathname(&newname);
851 attr_remove_path(pathname_t *name, const char *attrname, int flags)
853 char buf[NAME_MAX + 1];
857 rval = attr_remove(name->path, attrname, flags);
858 if (rval >= 0 || errno != ENAMETOOLONG)
860 separate_pathname(name, buf, &newname);
861 if (chdir(buf) == 0) {
862 rval = attr_remove_path(&newname, attrname, flags);
863 assert(chdir("..") == 0);
865 free_pathname(&newname);
870 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
871 const int valuelength, int flags)
873 char buf[NAME_MAX + 1];
877 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
878 if (rval >= 0 || errno != ENAMETOOLONG)
880 separate_pathname(name, buf, &newname);
881 if (chdir(buf) == 0) {
882 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
884 assert(chdir("..") == 0);
886 free_pathname(&newname);
894 struct stat64 statbuf;
896 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
898 assert(chdir(homedir) == 0);
899 fprintf(stderr, "fsstress: check_cwd failure\n");
906 * go thru flist and release all entries
908 * NOTE: this function shouldn't be called until the end of a process
916 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
925 creat_path(pathname_t *name, mode_t mode)
927 char buf[NAME_MAX + 1];
931 rval = creat(name->path, mode);
932 if (rval >= 0 || errno != ENAMETOOLONG)
934 separate_pathname(name, buf, &newname);
935 if (chdir(buf) == 0) {
936 rval = creat_path(&newname, mode);
937 assert(chdir("..") == 0);
939 free_pathname(&newname);
944 dcache_enter(int dirid, int slot)
946 dcache[dirid % NDCACHE] = slot;
954 for (i = 0; i < NDCACHE; i++)
959 dcache_lookup(int dirid)
964 i = dcache[dirid % NDCACHE];
965 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
971 dcache_purge(int dirid)
975 dcp = &dcache[dirid % NDCACHE];
976 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
981 * Delete the item from the list by
982 * moving last entry over the deleted one;
983 * unless deleted entry is the last one.
984 * Input: which file list array and which slot in array
987 del_from_flist(int ft, int slot)
993 dcache_purge(ftp->fents[slot].id);
994 if (slot != ftp->nfiles - 1) {
996 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
997 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
1003 dirid_to_fent(int dirid)
1009 if ((fep = dcache_lookup(dirid)))
1011 flp = &flist[FT_DIR];
1012 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
1013 if (fep->id == dirid) {
1014 dcache_enter(dirid, fep - flp->fents);
1024 struct stat64 statbuf;
1032 dividend = (operations + execute_freq) / (execute_freq + 1);
1033 sprintf(buf, "p%x", procid);
1034 (void)mkdir(buf, 0777);
1035 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
1039 top_ino = statbuf.st_ino;
1040 homedir = getcwd(NULL, 0);
1042 perror("getcwd failed");
1048 namerand = random();
1049 for (opno = 0; opno < operations; opno++) {
1050 if (execute_cmd && opno && opno % dividend == 0) {
1052 printf("%d: execute command %s\n", opno,
1054 rval = system(execute_cmd);
1056 fprintf(stderr, "execute command failed with "
1059 p = &ops[freq_table[random() % freq_table_size]];
1060 p->func(opno, random());
1062 * test for forced shutdown by stat'ing the test
1063 * directory. If this stat returns EIO, assume
1064 * the forced shutdown happened.
1066 if (errtag != 0 && opno % 100 == 0) {
1067 rval = stat64(".", &statbuf);
1069 fprintf(stderr, "Detected EIO\n");
1075 assert(chdir("..") == 0);
1080 sprintf(cmd, "rm -rf %s", buf);
1083 perror("cleaning up");
1089 * build up a pathname going thru the file entry and all
1090 * its parent entries
1091 * Return 0 on error, 1 on success;
1094 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1096 char buf[NAME_MAX + 1];
1104 /* build up parent directory name */
1105 if (fep->parent != -1) {
1106 pfep = dirid_to_fent(fep->parent);
1109 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1110 procid, fep->id, fep->parent);
1115 e = fent_to_name(name, &flist[FT_DIR], pfep);
1118 append_pathname(name, "/");
1121 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1122 namerandpad(fep->id, buf, i);
1123 append_pathname(name, buf);
1128 fents_ancestor_check(fent_t *fep, fent_t *dfep)
1132 for (tmpfep = fep; tmpfep->parent != -1;
1133 tmpfep = dirid_to_fent(tmpfep->parent)) {
1134 if (tmpfep->parent == dfep->id)
1142 fix_parent(int oldid, int newid, bool swap)
1149 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1150 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1151 if (fep->parent == oldid)
1152 fep->parent = newid;
1153 else if (swap && fep->parent == newid)
1154 fep->parent = oldid;
1160 free_pathname(pathname_t *name)
1170 * Generate a filename of type ft.
1171 * If we have a fep which should be a directory then use it
1172 * as the parent path for this new filename i.e. prepend with it.
1175 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1177 char buf[NAME_MAX + 1];
1186 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1187 namerandpad(id, buf, len);
1189 /* prepend fep parent dir-name to it */
1191 e = fent_to_name(name, &flist[FT_DIR], fep);
1194 append_pathname(name, "/");
1196 append_pathname(name, buf);
1200 for (j = 0; !*v && j < ilistlen; j++) {
1201 if (ilist[j] == id) {
1209 int generate_xattr_name(int xattr_num, char *buffer, int buflen)
1213 ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
1223 * Input: "which" to choose the file-types eg. non-directory
1224 * Input: "r" to choose which file
1225 * Output: file-list, file-entry, name for the chosen file.
1226 * Output: verbose if chosen file is on the ilist.
1229 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1232 int totalsum = 0; /* total number of matching files */
1233 int partialsum = 0; /* partial sum of matching files */
1239 int e = 1; /* success */
1242 * go thru flist and add up number of files for each
1243 * category that matches with <which>.
1245 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1246 if (which & (1 << i))
1247 totalsum += flp->nfiles;
1249 if (totalsum == 0) {
1259 * Now we have possible matches between 0..totalsum-1.
1260 * And we use r to help us choose which one we want,
1261 * which when bounded by totalsum becomes x.
1263 x = (int)(r % totalsum);
1264 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1265 if (which & (1 << i)) {
1266 if (x < partialsum + flp->nfiles) {
1268 /* found the matching file entry */
1269 fep = &flp->fents[x - partialsum];
1271 /* fill-in what we were asked for */
1273 e = fent_to_name(name, flp, fep);
1276 fprintf(stderr, "%d: failed to get path for entry:"
1277 " id=%d,parent=%d\n",
1278 procid, fep->id, fep->parent);
1287 /* turn on verbose if its an ilisted file */
1289 for (j = 0; !*v && j < ilistlen; j++) {
1290 if (ilist[j] == fep->id) {
1297 partialsum += flp->nfiles;
1301 fprintf(stderr, "fsstress: get_fname failure\n");
1308 init_pathname(pathname_t *name)
1315 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1317 char buf[NAME_MAX + 1];
1321 rval = lchown(name->path, owner, group);
1322 if (rval >= 0 || errno != ENAMETOOLONG)
1324 separate_pathname(name, buf, &newname);
1325 if (chdir(buf) == 0) {
1326 rval = lchown_path(&newname, owner, group);
1327 assert(chdir("..") == 0);
1329 free_pathname(&newname);
1334 link_path(pathname_t *name1, pathname_t *name2)
1336 char buf1[NAME_MAX + 1];
1337 char buf2[NAME_MAX + 1];
1339 pathname_t newname1;
1340 pathname_t newname2;
1343 rval = link(name1->path, name2->path);
1344 if (rval >= 0 || errno != ENAMETOOLONG)
1346 separate_pathname(name1, buf1, &newname1);
1347 separate_pathname(name2, buf2, &newname2);
1348 if (strcmp(buf1, buf2) == 0) {
1349 if (chdir(buf1) == 0) {
1350 rval = link_path(&newname1, &newname2);
1351 assert(chdir("..") == 0);
1354 if (strcmp(buf1, "..") == 0)
1356 else if (strcmp(buf2, "..") == 0)
1358 else if (strlen(buf1) == 0)
1360 else if (strlen(buf2) == 0)
1363 down1 = MAX(newname1.len, 3 + name2->len) <=
1364 MAX(3 + name1->len, newname2.len);
1366 free_pathname(&newname2);
1367 append_pathname(&newname2, "../");
1368 append_pathname(&newname2, name2->path);
1369 if (chdir(buf1) == 0) {
1370 rval = link_path(&newname1, &newname2);
1371 assert(chdir("..") == 0);
1374 free_pathname(&newname1);
1375 append_pathname(&newname1, "../");
1376 append_pathname(&newname1, name1->path);
1377 if (chdir(buf2) == 0) {
1378 rval = link_path(&newname1, &newname2);
1379 assert(chdir("..") == 0);
1383 free_pathname(&newname1);
1384 free_pathname(&newname2);
1389 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1391 char buf[NAME_MAX + 1];
1395 rval = lstat64(name->path, sbuf);
1396 if (rval >= 0 || errno != ENAMETOOLONG)
1398 separate_pathname(name, buf, &newname);
1399 if (chdir(buf) == 0) {
1400 rval = lstat64_path(&newname, sbuf);
1401 assert(chdir("..") == 0);
1403 free_pathname(&newname);
1408 make_freq_table(void)
1414 for (p = ops, f = 0; p < ops_end; p++)
1416 freq_table = malloc(f * sizeof(*freq_table));
1417 freq_table_size = f;
1418 for (p = ops, i = 0; p < ops_end; p++) {
1419 for (f = 0; f < p->freq; f++, i++)
1420 freq_table[i] = p->op;
1425 mkdir_path(pathname_t *name, mode_t mode)
1427 char buf[NAME_MAX + 1];
1431 rval = mkdir(name->path, mode);
1432 if (rval >= 0 || errno != ENAMETOOLONG)
1434 separate_pathname(name, buf, &newname);
1435 if (chdir(buf) == 0) {
1436 rval = mkdir_path(&newname, mode);
1437 assert(chdir("..") == 0);
1439 free_pathname(&newname);
1444 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1446 char buf[NAME_MAX + 1];
1450 rval = mknod(name->path, mode, dev);
1451 if (rval >= 0 || errno != ENAMETOOLONG)
1453 separate_pathname(name, buf, &newname);
1454 if (chdir(buf) == 0) {
1455 rval = mknod_path(&newname, mode, dev);
1456 assert(chdir("..") == 0);
1458 free_pathname(&newname);
1463 namerandpad(int id, char *buf, int i)
1466 static int buckets[] =
1467 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1473 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1474 padmod = buckets[bucket] + 1 - i;
1477 padlen = (id ^ namerand) % padmod;
1479 memset(&buf[i], 'X', padlen);
1480 buf[i + padlen] = '\0';
1485 open_file_or_dir(pathname_t *name, int flags)
1489 fd = open_path(name, flags);
1492 if (fd == -1 && errno != EISDIR)
1494 /* Directories can not be opened in write mode nor direct mode. */
1495 flags &= ~(O_WRONLY | O_DIRECT);
1496 flags |= O_RDONLY | O_DIRECTORY;
1497 return open_path(name, flags);
1501 open_path(pathname_t *name, int oflag)
1503 char buf[NAME_MAX + 1];
1507 rval = open(name->path, oflag);
1508 if (rval >= 0 || errno != ENAMETOOLONG)
1510 separate_pathname(name, buf, &newname);
1511 if (chdir(buf) == 0) {
1512 rval = open_path(&newname, oflag);
1513 assert(chdir("..") == 0);
1515 free_pathname(&newname);
1520 opendir_path(pathname_t *name)
1522 char buf[NAME_MAX + 1];
1526 rval = opendir(name->path);
1527 if (rval || errno != ENAMETOOLONG)
1529 separate_pathname(name, buf, &newname);
1530 if (chdir(buf) == 0) {
1531 rval = opendir_path(&newname);
1532 assert(chdir("..") == 0);
1534 free_pathname(&newname);
1539 process_freq(char *arg)
1544 s = strchr(arg, '=');
1546 fprintf(stderr, "bad argument '%s'\n", arg);
1550 for (p = ops; p < ops_end; p++) {
1551 if (strcmp(arg, p->name) == 0) {
1556 fprintf(stderr, "can't find op type %s for -f\n", arg);
1561 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1563 char buf[NAME_MAX + 1];
1567 rval = readlink(name->path, lbuf, lbufsiz);
1568 if (rval >= 0 || errno != ENAMETOOLONG)
1570 separate_pathname(name, buf, &newname);
1571 if (chdir(buf) == 0) {
1572 rval = readlink_path(&newname, lbuf, lbufsiz);
1573 assert(chdir("..") == 0);
1575 free_pathname(&newname);
1580 rename_path(pathname_t *name1, pathname_t *name2, int mode)
1582 char buf1[NAME_MAX + 1];
1583 char buf2[NAME_MAX + 1];
1585 pathname_t newname1;
1586 pathname_t newname2;
1590 rval = rename(name1->path, name2->path);
1592 rval = renameat2(AT_FDCWD, name1->path,
1593 AT_FDCWD, name2->path, mode);
1594 if (rval >= 0 || errno != ENAMETOOLONG)
1596 separate_pathname(name1, buf1, &newname1);
1597 separate_pathname(name2, buf2, &newname2);
1598 if (strcmp(buf1, buf2) == 0) {
1599 if (chdir(buf1) == 0) {
1600 rval = rename_path(&newname1, &newname2, mode);
1601 assert(chdir("..") == 0);
1604 if (strcmp(buf1, "..") == 0)
1606 else if (strcmp(buf2, "..") == 0)
1608 else if (strlen(buf1) == 0)
1610 else if (strlen(buf2) == 0)
1613 down1 = MAX(newname1.len, 3 + name2->len) <=
1614 MAX(3 + name1->len, newname2.len);
1616 free_pathname(&newname2);
1617 append_pathname(&newname2, "../");
1618 append_pathname(&newname2, name2->path);
1619 if (chdir(buf1) == 0) {
1620 rval = rename_path(&newname1, &newname2, mode);
1621 assert(chdir("..") == 0);
1624 free_pathname(&newname1);
1625 append_pathname(&newname1, "../");
1626 append_pathname(&newname1, name1->path);
1627 if (chdir(buf2) == 0) {
1628 rval = rename_path(&newname1, &newname2, mode);
1629 assert(chdir("..") == 0);
1633 free_pathname(&newname1);
1634 free_pathname(&newname2);
1639 rmdir_path(pathname_t *name)
1641 char buf[NAME_MAX + 1];
1645 rval = rmdir(name->path);
1646 if (rval >= 0 || errno != ENAMETOOLONG)
1648 separate_pathname(name, buf, &newname);
1649 if (chdir(buf) == 0) {
1650 rval = rmdir_path(&newname);
1651 assert(chdir("..") == 0);
1653 free_pathname(&newname);
1658 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1662 init_pathname(newname);
1663 slash = strchr(name->path, '/');
1664 if (slash == NULL) {
1669 strcpy(buf, name->path);
1671 append_pathname(newname, slash + 1);
1677 show_ops(int flag, char *lead_str)
1682 /* print in list form */
1685 for (p = ops; p < ops_end; p++) {
1686 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1687 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1688 x+=printf("%s ", p->name);
1691 } else if (flag == 0) {
1692 /* Table view style */
1694 for (f = 0, p = ops; p < ops_end; p++)
1700 for (p = ops; p < ops_end; p++) {
1701 if (flag != 0 || p->freq > 0) {
1702 if (lead_str != NULL)
1703 printf("%s", lead_str);
1704 printf("%20s %d/%d %s\n",
1705 p->name, p->freq, f,
1706 (p->iswrite == 0) ? " " : "write op");
1710 /* Command line style */
1711 if (lead_str != NULL)
1712 printf("%s", lead_str);
1713 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1715 for (p = ops; p < ops_end; p++)
1717 printf("-f %s=%d \\\n",p->name, p->freq);
1722 stat64_path(pathname_t *name, struct stat64 *sbuf)
1724 char buf[NAME_MAX + 1];
1728 rval = stat64(name->path, sbuf);
1729 if (rval >= 0 || errno != ENAMETOOLONG)
1731 separate_pathname(name, buf, &newname);
1732 if (chdir(buf) == 0) {
1733 rval = stat64_path(&newname, sbuf);
1734 assert(chdir("..") == 0);
1736 free_pathname(&newname);
1741 symlink_path(const char *name1, pathname_t *name)
1743 char buf[NAME_MAX + 1];
1747 if (!strcmp(name1, name->path)) {
1748 printf("yikes! %s %s\n", name1, name->path);
1752 rval = symlink(name1, name->path);
1753 if (rval >= 0 || errno != ENAMETOOLONG)
1755 separate_pathname(name, buf, &newname);
1756 if (chdir(buf) == 0) {
1757 rval = symlink_path(name1, &newname);
1758 assert(chdir("..") == 0);
1760 free_pathname(&newname);
1765 truncate64_path(pathname_t *name, off64_t length)
1767 char buf[NAME_MAX + 1];
1771 rval = truncate64(name->path, length);
1772 if (rval >= 0 || errno != ENAMETOOLONG)
1774 separate_pathname(name, buf, &newname);
1775 if (chdir(buf) == 0) {
1776 rval = truncate64_path(&newname, length);
1777 assert(chdir("..") == 0);
1779 free_pathname(&newname);
1784 unlink_path(pathname_t *name)
1786 char buf[NAME_MAX + 1];
1790 rval = unlink(name->path);
1791 if (rval >= 0 || errno != ENAMETOOLONG)
1793 separate_pathname(name, buf, &newname);
1794 if (chdir(buf) == 0) {
1795 rval = unlink_path(&newname);
1796 assert(chdir("..") == 0);
1798 free_pathname(&newname);
1805 printf("Usage: %s -H or\n", myprog);
1806 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1808 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1810 printf(" -c clean up the test directory after each run\n");
1811 printf(" -d dir specifies the base directory for operations\n");
1812 printf(" -e errtg specifies error injection stuff\n");
1813 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1814 printf(" the valid operation names are:\n");
1816 printf(" -i filenum get verbose output for this nth file object\n");
1817 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1818 printf(" *use 0 for infinite (default 1)\n");
1819 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1820 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1821 printf(" -o logfile specifies logfile name\n");
1822 printf(" -p nproc specifies the no. of processes (default 1)\n");
1823 printf(" -r specifies random name padding\n");
1824 printf(" -s seed specifies the seed for the random generator (default random)\n");
1825 printf(" -v specifies verbose mode\n");
1826 printf(" -w zeros frequencies of non-write operations\n");
1827 printf(" -x cmd execute command in the middle of operations\n");
1828 printf(" -z zeros frequencies of all operations\n");
1829 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1830 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1831 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1832 printf(" -H prints usage and exits\n");
1840 for (p = ops; p < ops_end; p++) {
1851 for (p = ops; p < ops_end; p++)
1855 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1858 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1859 verifiable_log ? -1: (long)s->st_ino,
1860 (long)s->st_nlink, s->st_uid, s->st_gid,
1861 (long long) s->st_blocks, (long long) s->st_size);
1865 afsync_f(int opno, long r)
1873 struct iocb *iocbs[] = { &iocb };
1874 struct io_event event;
1877 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1879 printf("%d/%d: afsync - no filename\n", procid, opno);
1883 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1884 e = fd < 0 ? errno : 0;
1888 printf("%d/%d: afsync - open %s failed %d\n",
1889 procid, opno, f.path, e);
1894 io_prep_fsync(&iocb, fd);
1895 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1897 printf("%d/%d: afsync - io_submit %s %d\n",
1898 procid, opno, f.path, e);
1903 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1905 printf("%d/%d: afsync - io_getevents failed %d\n",
1914 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1921 allocsp_f(int opno, long r)
1926 struct xfs_flock64 fl;
1934 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1936 printf("%d/%d: allocsp - no filename\n", procid, opno);
1940 fd = open_path(&f, O_RDWR);
1941 e = fd < 0 ? errno : 0;
1945 printf("%d/%d: allocsp - open %s failed %d\n",
1946 procid, opno, f.path, e);
1950 if (fstat64(fd, &stb) < 0) {
1952 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1953 procid, opno, f.path, errno);
1958 inode_info(st, sizeof(st), &stb, v);
1959 lr = ((int64_t)random() << 32) + random();
1960 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1962 fl.l_whence = SEEK_SET;
1965 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1967 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1968 procid, opno, f.path, st, (long long)off, e);
1976 do_aio_rw(int opno, long r, int flags)
1980 struct dioattr diob;
1992 struct io_event event;
1993 struct iocb *iocbs[] = { &iocb };
1994 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1997 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1999 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
2003 fd = open_path(&f, flags|O_DIRECT);
2004 e = fd < 0 ? errno : 0;
2008 printf("%d/%d: do_aio_rw - open %s failed %d\n",
2009 procid, opno, f.path, e);
2013 if (fstat64(fd, &stb) < 0) {
2015 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
2016 procid, opno, f.path, errno);
2021 inode_info(st, sizeof(st), &stb, v);
2022 if (!iswrite && stb.st_size == 0) {
2024 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
2030 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2033 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2034 " fallback to stat()\n",
2035 procid, opno, f.path, st, errno);
2036 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2037 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2039 dio_env = getenv("XFS_DIO_MIN");
2041 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2042 align = (int64_t)diob.d_miniosz;
2043 lr = ((int64_t)random() << 32) + random();
2044 len = (random() % FILELEN_MAX) + 1;
2045 len -= (len % align);
2048 else if (len > diob.d_maxiosz)
2049 len = diob.d_maxiosz;
2050 buf = memalign(diob.d_mem, len);
2053 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2054 off -= (off % align);
2056 memset(buf, nameseq & 0xff, len);
2057 io_prep_pwrite(&iocb, fd, buf, len, off);
2059 off = (off64_t)(lr % stb.st_size);
2060 off -= (off % align);
2061 io_prep_pread(&iocb, fd, buf, len, off);
2063 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
2065 printf("%d/%d: %s - io_submit failed %d\n",
2066 procid, opno, iswrite ? "awrite" : "aread", e);
2071 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
2073 printf("%d/%d: %s - io_getevents failed %d\n",
2074 procid, opno, iswrite ? "awrite" : "aread", e);
2080 e = event.res != len ? event.res2 : 0;
2083 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
2084 procid, opno, iswrite ? "awrite" : "aread",
2085 f.path, st, (long long)off, (int)len, e);
2092 aread_f(int opno, long r)
2095 do_aio_rw(opno, r, O_RDONLY);
2100 attr_remove_f(int opno, long r)
2102 attrlist_ent_t *aep;
2106 attrlist_cursor_t cursor;
2115 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2116 append_pathname(&f, ".");
2118 bzero(&cursor, sizeof(cursor));
2120 bzero(buf, sizeof(buf));
2121 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2125 alist = (attrlist_t *)buf;
2126 total += alist->al_count;
2127 } while (alist->al_more);
2130 printf("%d/%d: attr_remove - no attrs for %s\n",
2131 procid, opno, f.path);
2135 which = (int)(random() % total);
2136 bzero(&cursor, sizeof(cursor));
2140 bzero(buf, sizeof(buf));
2141 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2145 alist = (attrlist_t *)buf;
2146 if (which < ent + alist->al_count) {
2147 aep = (attrlist_ent_t *)
2148 &buf[alist->al_offset[which - ent]];
2149 aname = aep->a_name;
2152 ent += alist->al_count;
2153 } while (alist->al_more);
2154 if (aname == NULL) {
2157 "%d/%d: attr_remove - name %d not found at %s\n",
2158 procid, opno, which, f.path);
2162 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2165 printf("%d/%d: attr_remove %s %s %d\n",
2166 procid, opno, f.path, aname, e);
2171 attr_set_f(int opno, long r)
2178 static int lengths[] = { 10, 100, 1000, 10000 };
2183 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2184 append_pathname(&f, ".");
2185 sprintf(aname, "a%x", nameseq++);
2186 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2187 len = (int)(random() % lengths[li]);
2191 memset(aval, nameseq & 0xff, len);
2192 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2197 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2203 awrite_f(int opno, long r)
2206 do_aio_rw(opno, r, O_WRONLY);
2211 bulkstat_f(int opno, long r)
2217 struct xfs_bstat *t;
2219 struct xfs_fsop_bulkreq bsr;
2222 nent = (r % 999) + 2;
2223 t = malloc(nent * sizeof(*t));
2224 fd = open(".", O_RDONLY);
2232 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2236 printf("%d/%d: bulkstat nent %d total %lld\n",
2237 procid, opno, nent, (long long)total);
2242 bulkstat1_f(int opno, long r)
2252 struct xfs_fsop_bulkreq bsr;
2255 good = random() & 1;
2257 /* use an inode we know exists */
2259 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2260 append_pathname(&f, ".");
2261 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2266 * pick a random inode
2268 * note this can generate kernel warning messages
2269 * since bulkstat_one will read the disk block that
2270 * would contain a given inode even if that disk
2271 * block doesn't contain inodes.
2273 * this is detected later, but not until after the
2274 * warning is displayed.
2276 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2282 fd = open(".", O_RDONLY);
2288 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2290 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2291 procid, opno, good?"real":"random",
2292 verifiable_log ? -1LL : (long long)ino, e);
2297 chown_f(int opno, long r)
2307 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2308 append_pathname(&f, ".");
2309 u = (uid_t)random();
2310 g = (gid_t)random();
2311 nbits = (int)(random() % idmodulo);
2312 u &= (1 << nbits) - 1;
2313 g &= (1 << nbits) - 1;
2314 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2317 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2321 /* reflink some arbitrary range of f1 to f2. */
2328 struct file_clone_range fcr;
2329 struct pathname fpath1;
2330 struct pathname fpath2;
2331 struct stat64 stat1;
2332 struct stat64 stat2;
2333 char inoinfo1[1024];
2334 char inoinfo2[1024];
2348 init_pathname(&fpath1);
2349 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2351 printf("%d/%d: clonerange read - no filename\n",
2356 init_pathname(&fpath2);
2357 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2359 printf("%d/%d: clonerange write - no filename\n",
2365 fd1 = open_path(&fpath1, O_RDONLY);
2366 e = fd1 < 0 ? errno : 0;
2370 printf("%d/%d: clonerange read - open %s failed %d\n",
2371 procid, opno, fpath1.path, e);
2375 fd2 = open_path(&fpath2, O_WRONLY);
2376 e = fd2 < 0 ? errno : 0;
2380 printf("%d/%d: clonerange write - open %s failed %d\n",
2381 procid, opno, fpath2.path, e);
2385 /* Get file stats */
2386 if (fstat64(fd1, &stat1) < 0) {
2388 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2389 procid, opno, fpath1.path, errno);
2392 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2394 if (fstat64(fd2, &stat2) < 0) {
2396 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2397 procid, opno, fpath2.path, errno);
2400 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2402 /* Calculate offsets */
2403 len = (random() % FILELEN_MAX) + 1;
2404 len &= ~(stat1.st_blksize - 1);
2406 len = stat1.st_blksize;
2407 if (len > stat1.st_size)
2408 len = stat1.st_size;
2410 lr = ((int64_t)random() << 32) + random();
2411 if (stat1.st_size == len)
2414 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2416 off1 &= ~(stat1.st_blksize - 1);
2419 * If srcfile == destfile, randomly generate destination ranges
2420 * until we find one that doesn't overlap the source range.
2422 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2424 lr = ((int64_t)random() << 32) + random();
2425 off2 = (off64_t)(lr % max_off2);
2427 off2 &= ~(stat2.st_blksize - 1);
2428 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2430 /* Clone data blocks */
2432 fcr.src_offset = off1;
2433 fcr.src_length = len;
2434 fcr.dest_offset = off2;
2436 ret = ioctl(fd2, FICLONERANGE, &fcr);
2437 e = ret < 0 ? errno : 0;
2439 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2441 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2442 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2445 printf(" error %d", e);
2454 free_pathname(&fpath2);
2456 free_pathname(&fpath1);
2460 /* copy some arbitrary range of f1 to f2. */
2466 #ifdef HAVE_COPY_FILE_RANGE
2467 struct pathname fpath1;
2468 struct pathname fpath2;
2469 struct stat64 stat1;
2470 struct stat64 stat2;
2471 char inoinfo1[1024];
2472 char inoinfo2[1024];
2490 init_pathname(&fpath1);
2491 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2493 printf("%d/%d: copyrange read - no filename\n",
2498 init_pathname(&fpath2);
2499 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2501 printf("%d/%d: copyrange write - no filename\n",
2507 fd1 = open_path(&fpath1, O_RDONLY);
2508 e = fd1 < 0 ? errno : 0;
2512 printf("%d/%d: copyrange read - open %s failed %d\n",
2513 procid, opno, fpath1.path, e);
2517 fd2 = open_path(&fpath2, O_WRONLY);
2518 e = fd2 < 0 ? errno : 0;
2522 printf("%d/%d: copyrange write - open %s failed %d\n",
2523 procid, opno, fpath2.path, e);
2527 /* Get file stats */
2528 if (fstat64(fd1, &stat1) < 0) {
2530 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2531 procid, opno, fpath1.path, errno);
2534 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2536 if (fstat64(fd2, &stat2) < 0) {
2538 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2539 procid, opno, fpath2.path, errno);
2542 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2544 /* Calculate offsets */
2545 len = (random() % FILELEN_MAX) + 1;
2547 len = stat1.st_blksize;
2548 if (len > stat1.st_size)
2549 len = stat1.st_size;
2551 lr = ((int64_t)random() << 32) + random();
2552 if (stat1.st_size == len)
2555 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2559 * If srcfile == destfile, randomly generate destination ranges
2560 * until we find one that doesn't overlap the source range.
2562 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2564 lr = ((int64_t)random() << 32) + random();
2565 off2 = (off64_t)(lr % max_off2);
2567 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2570 * Since len, off1 and off2 will be changed later, preserve their
2578 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2581 if (errno != EAGAIN || tries++ >= 300)
2583 } else if (ret > len || ret == 0)
2588 e = ret < 0 ? errno : 0;
2590 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2592 fpath1.path, inoinfo1,
2593 (long long)offset1, (long long)length,
2594 fpath2.path, inoinfo2,
2595 (long long)offset2, (long long)length);
2598 printf(" error %d", e);
2599 else if (len && ret > len)
2600 printf(" asked for %lld, copied %lld??\n",
2601 (long long)len, (long long)ret);
2610 free_pathname(&fpath2);
2612 free_pathname(&fpath1);
2616 /* dedupe some arbitrary range of f1 to f2...fn. */
2622 #ifdef FIDEDUPERANGE
2623 #define INFO_SZ 1024
2624 struct file_dedupe_range *fdr;
2625 struct pathname *fpath;
2626 struct stat64 *stat;
2638 if (flist[FT_REG].nfiles < 2)
2641 /* Pick somewhere between 2 and 128 files. */
2643 nr = random() % (flist[FT_REG].nfiles + 1);
2644 } while (nr < 2 || nr > 128);
2647 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2648 sizeof(struct file_dedupe_range));
2650 printf("%d/%d: line %d error %d\n",
2651 procid, opno, __LINE__, errno);
2654 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2655 sizeof(struct file_dedupe_range)));
2657 fpath = calloc(nr, sizeof(struct pathname));
2659 printf("%d/%d: line %d error %d\n",
2660 procid, opno, __LINE__, errno);
2664 stat = calloc(nr, sizeof(struct stat64));
2666 printf("%d/%d: line %d error %d\n",
2667 procid, opno, __LINE__, errno);
2671 info = calloc(nr, INFO_SZ);
2673 printf("%d/%d: line %d error %d\n",
2674 procid, opno, __LINE__, errno);
2678 off = calloc(nr, sizeof(off64_t));
2680 printf("%d/%d: line %d error %d\n",
2681 procid, opno, __LINE__, errno);
2685 v = calloc(nr, sizeof(int));
2687 printf("%d/%d: line %d error %d\n",
2688 procid, opno, __LINE__, errno);
2691 fd = calloc(nr, sizeof(int));
2693 printf("%d/%d: line %d error %d\n",
2694 procid, opno, __LINE__, errno);
2697 memset(fd, 0xFF, nr * sizeof(int));
2699 /* Get paths for all files */
2700 for (i = 0; i < nr; i++)
2701 init_pathname(&fpath[i]);
2703 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2705 printf("%d/%d: deduperange read - no filename\n",
2710 for (i = 1; i < nr; i++) {
2711 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2713 printf("%d/%d: deduperange write - no filename\n",
2720 fd[0] = open_path(&fpath[0], O_RDONLY);
2721 e = fd[0] < 0 ? errno : 0;
2725 printf("%d/%d: deduperange read - open %s failed %d\n",
2726 procid, opno, fpath[0].path, e);
2730 for (i = 1; i < nr; i++) {
2731 fd[i] = open_path(&fpath[i], O_WRONLY);
2732 e = fd[i] < 0 ? errno : 0;
2736 printf("%d/%d: deduperange write - open %s failed %d\n",
2737 procid, opno, fpath[i].path, e);
2742 /* Get file stats */
2743 if (fstat64(fd[0], &stat[0]) < 0) {
2745 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2746 procid, opno, fpath[0].path, errno);
2750 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2752 for (i = 1; i < nr; i++) {
2753 if (fstat64(fd[i], &stat[i]) < 0) {
2755 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2756 procid, opno, fpath[i].path, errno);
2759 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2762 /* Never try to dedupe more than half of the src file. */
2763 len = (random() % FILELEN_MAX) + 1;
2764 len &= ~(stat[0].st_blksize - 1);
2766 len = stat[0].st_blksize / 2;
2767 if (len > stat[0].st_size / 2)
2768 len = stat[0].st_size / 2;
2770 /* Calculate offsets */
2771 lr = ((int64_t)random() << 32) + random();
2772 if (stat[0].st_size == len)
2775 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2777 off[0] &= ~(stat[0].st_blksize - 1);
2780 * If srcfile == destfile[i], randomly generate destination ranges
2781 * until we find one that doesn't overlap the source range.
2783 for (i = 1; i < nr; i++) {
2787 lr = ((int64_t)random() << 32) + random();
2788 if (stat[i].st_size <= len)
2791 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2793 off[i] &= ~(stat[i].st_blksize - 1);
2794 } while (stat[0].st_ino == stat[i].st_ino &&
2795 llabs(off[i] - off[0]) < len &&
2799 /* Clone data blocks */
2800 fdr->src_offset = off[0];
2801 fdr->src_length = len;
2802 fdr->dest_count = nr - 1;
2803 for (i = 1; i < nr; i++) {
2804 fdr->info[i - 1].dest_fd = fd[i];
2805 fdr->info[i - 1].dest_offset = off[i];
2808 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2809 e = ret < 0 ? errno : 0;
2811 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2813 fpath[0].path, &info[0], (long long)off[0],
2816 printf(" error %d", e);
2822 for (i = 1; i < nr; i++) {
2823 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2825 printf("%d/%d: ...to %s%s [%lld,%lld]",
2827 fpath[i].path, &info[i * INFO_SZ],
2828 (long long)off[i], (long long)len);
2829 if (fdr->info[i - 1].status < 0)
2830 printf(" error %d", e);
2831 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2832 printf(" %llu bytes deduplicated",
2833 fdr->info[i - 1].bytes_deduped);
2834 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2835 printf(" differed");
2841 for (i = 0; i < nr; i++)
2845 for (i = 0; i < nr; i++)
2846 free_pathname(&fpath[i]);
2865 setxattr_f(int opno, long r)
2867 #ifdef XFS_XFLAG_EXTSIZE
2877 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2878 append_pathname(&f, ".");
2879 fd = open_path(&f, O_RDWR);
2880 e = fd < 0 ? errno : 0;
2885 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2886 nbits = (int)(random() % e);
2887 p &= (1 << nbits) - 1;
2889 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2891 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2894 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2901 splice_f(int opno, long r)
2903 struct pathname fpath1;
2904 struct pathname fpath2;
2905 struct stat64 stat1;
2906 struct stat64 stat2;
2907 char inoinfo1[1024];
2908 char inoinfo2[1024];
2912 loff_t offset1, offset2;
2919 ssize_t ret1 = 0, ret2 = 0;
2925 init_pathname(&fpath1);
2926 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2928 printf("%d/%d: splice read - no filename\n",
2933 init_pathname(&fpath2);
2934 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2936 printf("%d/%d: splice write - no filename\n",
2942 fd1 = open_path(&fpath1, O_RDONLY);
2943 e = fd1 < 0 ? errno : 0;
2947 printf("%d/%d: splice read - open %s failed %d\n",
2948 procid, opno, fpath1.path, e);
2952 fd2 = open_path(&fpath2, O_WRONLY);
2953 e = fd2 < 0 ? errno : 0;
2957 printf("%d/%d: splice write - open %s failed %d\n",
2958 procid, opno, fpath2.path, e);
2962 /* Get file stats */
2963 if (fstat64(fd1, &stat1) < 0) {
2965 printf("%d/%d: splice read - fstat64 %s failed %d\n",
2966 procid, opno, fpath1.path, errno);
2969 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2971 if (fstat64(fd2, &stat2) < 0) {
2973 printf("%d/%d: splice write - fstat64 %s failed %d\n",
2974 procid, opno, fpath2.path, errno);
2977 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2979 /* Calculate offsets */
2980 len = (random() % FILELEN_MAX) + 1;
2982 len = stat1.st_blksize;
2983 if (len > stat1.st_size)
2984 len = stat1.st_size;
2986 lr = ((int64_t)random() << 32) + random();
2987 if (stat1.st_size == len)
2990 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2994 * splice can overlap write, so the offset of the target file can be
2995 * any number. But to avoid too large offset, add a clamp of 1024 blocks
2996 * past the current dest file EOF
2998 lr = ((int64_t)random() << 32) + random();
2999 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
3002 * Since len, off1 and off2 will be changed later, preserve their
3009 /* Pipe initialize */
3010 if (pipe(filedes) < 0) {
3012 printf("%d/%d: splice - pipe failed %d\n",
3013 procid, opno, errno);
3021 /* move to pipe buffer */
3022 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
3028 /* move from pipe buffer to dst file */
3030 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
3043 if (ret1 < 0 || ret2 < 0)
3048 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
3050 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
3051 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
3053 if (length && length > total)
3054 printf(" asked for %lld, spliced %lld??\n",
3055 (long long)length, (long long)total);
3066 free_pathname(&fpath2);
3068 free_pathname(&fpath1);
3072 creat_f(int opno, long r)
3087 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
3092 e1 = (random() % 100);
3093 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
3095 if (type == FT_RTF) /* rt always gets an extsize */
3096 extsize = (random() % 10) + 1;
3097 else if (e1 < 10) /* one-in-ten get an extsize */
3098 extsize = random() % 1024;
3102 e = generate_fname(fep, type, &f, &id, &v);
3106 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3107 printf("%d/%d: creat - no filename from %s\n",
3108 procid, opno, f.path);
3113 fd = creat_path(&f, 0666);
3114 e = fd < 0 ? errno : 0;
3119 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3120 if (type == FT_RTF) {
3121 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3122 a.fsx_extsize = extsize *
3123 geom.rtextsize * geom.blocksize;
3125 } else if (extsize) {
3126 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3127 a.fsx_extsize = extsize * geom.blocksize;
3130 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3133 add_to_flist(type, id, parid, 0);
3137 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3138 extsize ? a.fsx_extsize : 0, e, e1);
3139 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3145 dread_f(int opno, long r)
3149 struct dioattr diob;
3162 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3164 printf("%d/%d: dread - no filename\n", procid, opno);
3168 fd = open_path(&f, O_RDONLY|O_DIRECT);
3169 e = fd < 0 ? errno : 0;
3173 printf("%d/%d: dread - open %s failed %d\n",
3174 procid, opno, f.path, e);
3178 if (fstat64(fd, &stb) < 0) {
3180 printf("%d/%d: dread - fstat64 %s failed %d\n",
3181 procid, opno, f.path, errno);
3186 inode_info(st, sizeof(st), &stb, v);
3187 if (stb.st_size == 0) {
3189 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3195 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3198 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3199 " fallback to stat()\n",
3200 procid, opno, f.path, st, errno);
3201 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3202 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3205 dio_env = getenv("XFS_DIO_MIN");
3207 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3209 align = (int64_t)diob.d_miniosz;
3210 lr = ((int64_t)random() << 32) + random();
3211 off = (off64_t)(lr % stb.st_size);
3212 off -= (off % align);
3213 lseek64(fd, off, SEEK_SET);
3214 len = (random() % FILELEN_MAX) + 1;
3215 len -= (len % align);
3218 else if (len > diob.d_maxiosz)
3219 len = diob.d_maxiosz;
3220 buf = memalign(diob.d_mem, len);
3221 e = read(fd, buf, len) < 0 ? errno : 0;
3224 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3225 procid, opno, f.path, st, (long long)off, (int)len, e);
3231 dwrite_f(int opno, long r)
3235 struct dioattr diob;
3248 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3250 printf("%d/%d: dwrite - no filename\n", procid, opno);
3254 fd = open_path(&f, O_WRONLY|O_DIRECT);
3255 e = fd < 0 ? errno : 0;
3259 printf("%d/%d: dwrite - open %s failed %d\n",
3260 procid, opno, f.path, e);
3264 if (fstat64(fd, &stb) < 0) {
3266 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3267 procid, opno, f.path, errno);
3272 inode_info(st, sizeof(st), &stb, v);
3273 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3275 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3276 " %s%s return %d, fallback to stat()\n",
3277 procid, opno, f.path, st, errno);
3278 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3279 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3282 dio_env = getenv("XFS_DIO_MIN");
3284 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3286 align = (int64_t)diob.d_miniosz;
3287 lr = ((int64_t)random() << 32) + random();
3288 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3289 off -= (off % align);
3290 lseek64(fd, off, SEEK_SET);
3291 len = (random() % FILELEN_MAX) + 1;
3292 len -= (len % align);
3295 else if (len > diob.d_maxiosz)
3296 len = diob.d_maxiosz;
3297 buf = memalign(diob.d_mem, len);
3299 lseek64(fd, off, SEEK_SET);
3300 memset(buf, nameseq & 0xff, len);
3301 e = write(fd, buf, len) < 0 ? errno : 0;
3304 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3305 procid, opno, f.path, st, (long long)off, (int)len, e);
3311 #ifdef HAVE_LINUX_FALLOC_H
3312 struct print_flags falloc_flags [] = {
3313 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3314 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3315 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3316 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3317 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3318 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3322 #define translate_falloc_flags(mode) \
3323 ({translate_flags(mode, "|", falloc_flags);})
3327 do_fallocate(int opno, long r, int mode)
3329 #ifdef HAVE_LINUX_FALLOC_H
3341 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3343 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3347 fd = open_path(&f, O_RDWR);
3350 printf("%d/%d: do_fallocate - open %s failed %d\n",
3351 procid, opno, f.path, errno);
3356 if (fstat64(fd, &stb) < 0) {
3358 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3359 procid, opno, f.path, errno);
3364 inode_info(st, sizeof(st), &stb, v);
3365 lr = ((int64_t)random() << 32) + random();
3366 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3368 len = (off64_t)(random() % (1024 * 1024));
3370 * Collapse/insert range requires off and len to be block aligned,
3371 * make it more likely to be the case.
3373 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3375 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3376 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3378 mode |= FALLOC_FL_KEEP_SIZE & random();
3379 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3381 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3382 procid, opno, translate_falloc_flags(mode),
3383 f.path, st, (long long)off, (long long)len, e);
3390 fallocate_f(int opno, long r)
3392 #ifdef HAVE_LINUX_FALLOC_H
3393 do_fallocate(opno, r, 0);
3398 fdatasync_f(int opno, long r)
3406 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3408 printf("%d/%d: fdatasync - no filename\n",
3413 fd = open_path(&f, O_WRONLY);
3414 e = fd < 0 ? errno : 0;
3418 printf("%d/%d: fdatasync - open %s failed %d\n",
3419 procid, opno, f.path, e);
3423 e = fdatasync(fd) < 0 ? errno : 0;
3425 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3430 #ifdef HAVE_LINUX_FIEMAP_H
3431 struct print_flags fiemap_flags[] = {
3432 { FIEMAP_FLAG_SYNC, "SYNC"},
3433 { FIEMAP_FLAG_XATTR, "XATTR"},
3437 #define translate_fiemap_flags(mode) \
3438 ({translate_flags(mode, "|", fiemap_flags);})
3442 fiemap_f(int opno, long r)
3444 #ifdef HAVE_LINUX_FIEMAP_H
3454 struct fiemap *fiemap;
3457 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3459 printf("%d/%d: fiemap - no filename\n", procid, opno);
3463 fd = open_path(&f, O_RDWR);
3464 e = fd < 0 ? errno : 0;
3468 printf("%d/%d: fiemap - open %s failed %d\n",
3469 procid, opno, f.path, e);
3473 if (fstat64(fd, &stb) < 0) {
3475 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3476 procid, opno, f.path, errno);
3481 inode_info(st, sizeof(st), &stb, v);
3482 blocks_to_map = random() & 0xffff;
3483 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3484 (blocks_to_map * sizeof(struct fiemap_extent)));
3487 printf("%d/%d: malloc failed \n", procid, opno);
3492 lr = ((int64_t)random() << 32) + random();
3493 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3495 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3496 fiemap->fm_extent_count = blocks_to_map;
3497 fiemap->fm_mapped_extents = random() & 0xffff;
3498 fiemap->fm_start = off;
3499 fiemap->fm_length = ((int64_t)random() << 32) + random();
3501 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3503 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3504 procid, opno, f.path, st, (long long)fiemap->fm_start,
3505 (long long) fiemap->fm_length,
3506 translate_fiemap_flags(fiemap->fm_flags), e);
3514 freesp_f(int opno, long r)
3519 struct xfs_flock64 fl;
3527 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3529 printf("%d/%d: freesp - no filename\n", procid, opno);
3533 fd = open_path(&f, O_RDWR);
3534 e = fd < 0 ? errno : 0;
3538 printf("%d/%d: freesp - open %s failed %d\n",
3539 procid, opno, f.path, e);
3543 if (fstat64(fd, &stb) < 0) {
3545 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3546 procid, opno, f.path, errno);
3551 inode_info(st, sizeof(st), &stb, v);
3552 lr = ((int64_t)random() << 32) + random();
3553 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3555 fl.l_whence = SEEK_SET;
3558 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3560 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3561 procid, opno, f.path, st, (long long)off, e);
3567 fsync_f(int opno, long r)
3575 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3577 printf("%d/%d: fsync - no filename\n", procid, opno);
3581 fd = open_file_or_dir(&f, O_WRONLY);
3582 e = fd < 0 ? errno : 0;
3586 printf("%d/%d: fsync - open %s failed %d\n",
3587 procid, opno, f.path, e);
3591 e = fsync(fd) < 0 ? errno : 0;
3593 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3599 gen_random_string(int len)
3601 static const char charset[] = "0123456789"
3602 "abcdefghijklmnopqrstuvwxyz"
3603 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3614 for (i = 0; i < len; i++)
3615 s[i] = charset[random() % sizeof(charset)];
3621 getattr_f(int opno, long r)
3630 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3631 append_pathname(&f, ".");
3632 fd = open_path(&f, O_RDWR);
3633 e = fd < 0 ? errno : 0;
3636 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3638 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3644 getdents_f(int opno, long r)
3651 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3652 append_pathname(&f, ".");
3653 dir = opendir_path(&f);
3657 printf("%d/%d: getdents - can't open %s\n",
3658 procid, opno, f.path);
3662 while (readdir64(dir) != NULL)
3665 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3671 getfattr_f(int opno, long r)
3677 char name[XATTR_NAME_BUF_SIZE];
3683 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3685 printf("%d/%d: getfattr - no filename\n", procid, opno);
3691 * If the file/dir has xattrs, pick one randomly, otherwise attempt
3692 * to read a xattr that doesn't exist (fgetxattr should fail with
3693 * errno set to ENOATTR (61) in this case).
3695 if (fep->xattr_counter > 0)
3696 xattr_num = (random() % fep->xattr_counter) + 1;
3700 e = generate_xattr_name(xattr_num, name, sizeof(name));
3702 printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
3703 procid, opno, f.path, e);
3707 value_len = getxattr(f.path, name, NULL, 0);
3708 if (value_len < 0) {
3710 printf("%d/%d: getfattr file %s name %s failed %d\n",
3711 procid, opno, f.path, name, errno);
3715 /* A xattr without value.*/
3716 if (value_len == 0) {
3721 value = malloc(value_len);
3724 printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
3725 procid, opno, f.path, value_len);
3729 e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
3732 printf("%d/%d: getfattr file %s name %s value length %d %d\n",
3733 procid, opno, f.path, name, value_len, e);
3740 link_f(int opno, long r)
3754 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
3756 printf("%d/%d: link - no file\n", procid, opno);
3760 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3766 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3770 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3771 printf("%d/%d: link - no filename from %s\n",
3772 procid, opno, l.path);
3778 e = link_path(&f, &l) < 0 ? errno : 0;
3781 add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
3783 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3785 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3792 listfattr_f(int opno, long r)
3798 char *buffer = NULL;
3802 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
3804 printf("%d/%d: listfattr - no filename\n", procid, opno);
3809 e = listxattr(f.path, NULL, 0);
3812 printf("%d/%d: listfattr %s failed %d\n",
3813 procid, opno, f.path, errno);
3817 if (buffer_len == 0) {
3819 printf("%d/%d: listfattr %s - has no extended attributes\n",
3820 procid, opno, f.path);
3824 buffer = malloc(buffer_len);
3827 printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
3828 procid, opno, f.path, buffer_len);
3832 e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
3834 printf("%d/%d: listfattr %s buffer length %d %d\n",
3835 procid, opno, f.path, buffer_len, e);
3842 mkdir_f(int opno, long r)
3852 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3857 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3861 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3862 printf("%d/%d: mkdir - no filename from %s\n",
3863 procid, opno, f.path);
3868 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3871 add_to_flist(FT_DIR, id, parid, 0);
3873 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3874 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3880 mknod_f(int opno, long r)
3890 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3895 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3899 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3900 printf("%d/%d: mknod - no filename from %s\n",
3901 procid, opno, f.path);
3906 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3909 add_to_flist(FT_DEV, id, parid, 0);
3911 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3912 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3917 #ifdef HAVE_SYS_MMAN_H
3918 struct print_flags mmap_flags[] = {
3919 { MAP_SHARED, "SHARED"},
3920 { MAP_PRIVATE, "PRIVATE"},
3924 #define translate_mmap_flags(flags) \
3925 ({translate_flags(flags, "|", mmap_flags);})
3929 do_mmap(int opno, long r, int prot)
3931 #ifdef HAVE_SYS_MMAN_H
3943 sigjmp_buf sigbus_jmpbuf;
3946 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3948 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3952 fd = open_path(&f, O_RDWR);
3953 e = fd < 0 ? errno : 0;
3957 printf("%d/%d: do_mmap - open %s failed %d\n",
3958 procid, opno, f.path, e);
3962 if (fstat64(fd, &stb) < 0) {
3964 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3965 procid, opno, f.path, errno);
3970 inode_info(st, sizeof(st), &stb, v);
3971 if (stb.st_size == 0) {
3973 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3980 lr = ((int64_t)random() << 32) + random();
3981 off = (off64_t)(lr % stb.st_size);
3982 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3983 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3985 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3986 addr = mmap(NULL, len, prot, flags, fd, off);
3987 e = (addr == MAP_FAILED) ? errno : 0;
3990 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3991 procid, opno, f.path, st, (long long)off,
3992 (int)len, translate_mmap_flags(flags), e);
3998 if (prot & PROT_WRITE) {
3999 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
4000 sigbus_jmp = &sigbus_jmpbuf;
4001 memset(addr, nameseq & 0xff, len);
4005 if ((buf = malloc(len)) != NULL) {
4006 memcpy(buf, addr, len);
4011 /* set NULL to stop other functions from doing siglongjmp */
4015 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
4016 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
4017 f.path, st, (long long)off, (int)len,
4018 translate_mmap_flags(flags),
4019 (e == 0) ? "0" : "Bus error");
4027 mread_f(int opno, long r)
4029 #ifdef HAVE_SYS_MMAN_H
4030 do_mmap(opno, r, PROT_READ);
4035 mwrite_f(int opno, long r)
4037 #ifdef HAVE_SYS_MMAN_H
4038 do_mmap(opno, r, PROT_WRITE);
4043 punch_f(int opno, long r)
4045 #ifdef HAVE_LINUX_FALLOC_H
4046 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
4051 zero_f(int opno, long r)
4053 #ifdef HAVE_LINUX_FALLOC_H
4054 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
4059 collapse_f(int opno, long r)
4061 #ifdef HAVE_LINUX_FALLOC_H
4062 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
4067 insert_f(int opno, long r)
4069 #ifdef HAVE_LINUX_FALLOC_H
4070 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
4075 read_f(int opno, long r)
4089 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4091 printf("%d/%d: read - no filename\n", procid, opno);
4095 fd = open_path(&f, O_RDONLY);
4096 e = fd < 0 ? errno : 0;
4100 printf("%d/%d: read - open %s failed %d\n",
4101 procid, opno, f.path, e);
4105 if (fstat64(fd, &stb) < 0) {
4107 printf("%d/%d: read - fstat64 %s failed %d\n",
4108 procid, opno, f.path, errno);
4113 inode_info(st, sizeof(st), &stb, v);
4114 if (stb.st_size == 0) {
4116 printf("%d/%d: read - %s%s zero size\n", procid, opno,
4122 lr = ((int64_t)random() << 32) + random();
4123 off = (off64_t)(lr % stb.st_size);
4124 lseek64(fd, off, SEEK_SET);
4125 len = (random() % FILELEN_MAX) + 1;
4127 e = read(fd, buf, len) < 0 ? errno : 0;
4130 printf("%d/%d: read %s%s [%lld,%d] %d\n",
4131 procid, opno, f.path, st, (long long)off, (int)len, e);
4137 readlink_f(int opno, long r)
4145 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
4147 printf("%d/%d: readlink - no filename\n", procid, opno);
4151 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
4154 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
4159 readv_f(int opno, long r)
4171 struct iovec *iov = NULL;
4178 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4180 printf("%d/%d: readv - no filename\n", procid, opno);
4184 fd = open_path(&f, O_RDONLY);
4185 e = fd < 0 ? errno : 0;
4189 printf("%d/%d: readv - open %s failed %d\n",
4190 procid, opno, f.path, e);
4194 if (fstat64(fd, &stb) < 0) {
4196 printf("%d/%d: readv - fstat64 %s failed %d\n",
4197 procid, opno, f.path, errno);
4202 inode_info(st, sizeof(st), &stb, v);
4203 if (stb.st_size == 0) {
4205 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
4211 lr = ((int64_t)random() << 32) + random();
4212 off = (off64_t)(lr % stb.st_size);
4213 lseek64(fd, off, SEEK_SET);
4214 len = (random() % FILELEN_MAX) + 1;
4217 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4218 iov = calloc(iovcnt, sizeof(struct iovec));
4219 iovl = len / iovcnt;
4221 for (i=0; i<iovcnt; i++) {
4222 (iov + i)->iov_base = (buf + iovb);
4223 (iov + i)->iov_len = iovl;
4227 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
4230 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
4231 procid, opno, f.path, st, (long long)off, (int)iovl,
4238 removefattr_f(int opno, long r)
4244 char name[XATTR_NAME_BUF_SIZE];
4248 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4250 printf("%d/%d: removefattr - no filename\n", procid, opno);
4256 * If the file/dir has xattrs, pick one randomly, otherwise attempt to
4257 * remove a xattr that doesn't exist (fremovexattr should fail with
4258 * errno set to ENOATTR (61) in this case).
4260 if (fep->xattr_counter > 0)
4261 xattr_num = (random() % fep->xattr_counter) + 1;
4265 e = generate_xattr_name(xattr_num, name, sizeof(name));
4267 printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
4268 procid, opno, f.path, e);
4272 e = removexattr(f.path, name) < 0 ? errno : 0;
4274 printf("%d/%d: removefattr file %s name %s %d\n",
4275 procid, opno, f.path, name, e);
4280 struct print_flags renameat2_flags [] = {
4281 { RENAME_NOREPLACE, "NOREPLACE"},
4282 { RENAME_EXCHANGE, "EXCHANGE"},
4283 { RENAME_WHITEOUT, "WHITEOUT"},
4287 #define translate_renameat2_flags(mode) \
4288 ({translate_flags(mode, "|", renameat2_flags);})
4291 do_renameat2(int opno, long r, int mode)
4307 /* get an existing path for the source of the rename */
4309 which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
4310 if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
4312 printf("%d/%d: rename - no source filename\n",
4319 * Both pathnames must exist for the RENAME_EXCHANGE, and in
4320 * order to maintain filelist/filename integrity, we should
4321 * restrict exchange operation to files of the same type.
4323 if (mode == RENAME_EXCHANGE) {
4324 which = 1 << (flp - flist);
4325 init_pathname(&newf);
4326 if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
4328 printf("%d/%d: rename - no target filename\n",
4330 free_pathname(&newf);
4334 if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
4335 fents_ancestor_check(dfep, fep))) {
4337 printf("%d/%d: rename(REXCHANGE) %s and %s "
4338 "have ancestor-descendant relationship\n",
4339 procid, opno, f.path, newf.path);
4340 free_pathname(&newf);
4346 parid = dfep->parent;
4349 * Get an existing directory for the destination parent
4352 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
4359 * Generate a new path using an existing parent directory
4362 init_pathname(&newf);
4363 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
4367 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
4368 printf("%d/%d: rename - no filename from %s\n",
4369 procid, opno, f.path);
4371 free_pathname(&newf);
4376 e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
4379 int xattr_counter = fep->xattr_counter;
4380 bool swap = (mode == RENAME_EXCHANGE) ? true : false;
4383 oldparid = fep->parent;
4386 * Swap the parent ids for RENAME_EXCHANGE, and replace the
4387 * old parent id for the others.
4389 if (flp - flist == FT_DIR)
4390 fix_parent(oldid, id, swap);
4392 if (mode == RENAME_WHITEOUT) {
4393 fep->xattr_counter = 0;
4394 add_to_flist(flp - flist, id, parid, xattr_counter);
4395 } else if (mode == RENAME_EXCHANGE) {
4396 fep->xattr_counter = dfep->xattr_counter;
4397 dfep->xattr_counter = xattr_counter;
4399 del_from_flist(flp - flist, fep - flp->fents);
4400 add_to_flist(flp - flist, id, parid, xattr_counter);
4404 printf("%d/%d: rename(%s) %s to %s %d\n", procid,
4405 opno, translate_renameat2_flags(mode), f.path,
4408 printf("%d/%d: rename source entry: id=%d,parent=%d\n",
4409 procid, opno, oldid, oldparid);
4410 printf("%d/%d: rename target entry: id=%d,parent=%d\n",
4411 procid, opno, id, parid);
4414 free_pathname(&newf);
4419 rename_f(int opno, long r)
4421 do_renameat2(opno, r, 0);
4425 rnoreplace_f(int opno, long r)
4427 do_renameat2(opno, r, RENAME_NOREPLACE);
4431 rexchange_f(int opno, long r)
4433 do_renameat2(opno, r, RENAME_EXCHANGE);
4437 rwhiteout_f(int opno, long r)
4439 do_renameat2(opno, r, RENAME_WHITEOUT);
4443 resvsp_f(int opno, long r)
4448 struct xfs_flock64 fl;
4456 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4458 printf("%d/%d: resvsp - no filename\n", procid, opno);
4462 fd = open_path(&f, O_RDWR);
4463 e = fd < 0 ? errno : 0;
4467 printf("%d/%d: resvsp - open %s failed %d\n",
4468 procid, opno, f.path, e);
4472 if (fstat64(fd, &stb) < 0) {
4474 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
4475 procid, opno, f.path, errno);
4480 inode_info(st, sizeof(st), &stb, v);
4481 lr = ((int64_t)random() << 32) + random();
4482 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4484 fl.l_whence = SEEK_SET;
4486 fl.l_len = (off64_t)(random() % (1024 * 1024));
4487 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
4489 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
4490 procid, opno, f.path, st,
4491 (long long)off, (long long)fl.l_len, e);
4497 rmdir_f(int opno, long r)
4505 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
4507 printf("%d/%d: rmdir - no directory\n", procid, opno);
4511 e = rmdir_path(&f) < 0 ? errno : 0;
4514 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
4516 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
4518 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
4519 procid, opno, fep->id, fep->parent);
4525 setattr_f(int opno, long r)
4534 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
4535 append_pathname(&f, ".");
4536 fd = open_path(&f, O_RDWR);
4537 e = fd < 0 ? errno : 0;
4540 fl = attr_mask & (uint)random();
4541 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
4543 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
4549 setfattr_f(int opno, long r)
4556 char name[XATTR_NAME_BUF_SIZE];
4562 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
4564 printf("%d/%d: setfattr - no filename\n", procid, opno);
4569 if ((fep->xattr_counter > 0) && (random() % 2)) {
4571 * Use an existing xattr name for replacing its value or
4572 * create again a xattr that was previously deleted.
4574 xattr_num = (random() % fep->xattr_counter) + 1;
4576 flag = XATTR_REPLACE;
4578 /* Use a new xattr name. */
4579 xattr_num = fep->xattr_counter + 1;
4581 * Don't always use the create flag because even if our xattr
4582 * counter is 0, we may still have xattrs associated to this
4583 * file (this happens when xattrs are added to a file through
4584 * one of its other hard links), so we can end up updating an
4585 * existing xattr too.
4588 flag = XATTR_CREATE;
4592 * The maximum supported value size depends on the filesystem
4593 * implementation, but 100 bytes is a safe value for most filesystems
4596 value_len = random() % 101;
4597 value = gen_random_string(value_len);
4598 if (!value && value_len > 0) {
4600 printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
4601 procid, opno, f.path, value_len);
4604 e = generate_xattr_name(xattr_num, name, sizeof(name));
4606 printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
4607 procid, opno, f.path, e);
4611 e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
4613 fep->xattr_counter++;
4615 printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
4616 procid, opno, f.path, name, xattr_flag_to_string(flag),
4624 stat_f(int opno, long r)
4632 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
4634 printf("%d/%d: stat - no entries\n", procid, opno);
4638 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
4641 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
4646 symlink_f(int opno, long r)
4659 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
4664 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
4668 (void)fent_to_name(&f, &flist[FT_DIR], fep);
4669 printf("%d/%d: symlink - no filename from %s\n",
4670 procid, opno, f.path);
4675 len = (int)(random() % PATH_MAX);
4676 val = malloc(len + 1);
4678 memset(val, 'x', len);
4680 for (i = 10; i < len - 1; i += 10)
4682 e = symlink_path(val, &f) < 0 ? errno : 0;
4685 add_to_flist(FT_SYM, id, parid, 0);
4688 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
4689 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
4696 sync_f(int opno, long r)
4700 printf("%d/%d: sync\n", procid, opno);
4704 truncate_f(int opno, long r)
4715 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4717 printf("%d/%d: truncate - no filename\n", procid, opno);
4721 e = stat64_path(&f, &stb) < 0 ? errno : 0;
4725 printf("%d/%d: truncate - stat64 %s failed %d\n",
4726 procid, opno, f.path, e);
4730 inode_info(st, sizeof(st), &stb, v);
4731 lr = ((int64_t)random() << 32) + random();
4732 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4734 e = truncate64_path(&f, off) < 0 ? errno : 0;
4737 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
4738 st, (long long)off, e);
4743 unlink_f(int opno, long r)
4752 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
4754 printf("%d/%d: unlink - no file\n", procid, opno);
4758 e = unlink_path(&f) < 0 ? errno : 0;
4761 del_from_flist(flp - flist, fep - flp->fents);
4763 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
4765 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
4766 procid, opno, fep->id, fep->parent);
4772 unresvsp_f(int opno, long r)
4777 struct xfs_flock64 fl;
4785 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
4787 printf("%d/%d: unresvsp - no filename\n", procid, opno);
4791 fd = open_path(&f, O_RDWR);
4792 e = fd < 0 ? errno : 0;
4796 printf("%d/%d: unresvsp - open %s failed %d\n",
4797 procid, opno, f.path, e);
4801 if (fstat64(fd, &stb) < 0) {
4803 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4804 procid, opno, f.path, errno);
4809 inode_info(st, sizeof(st), &stb, v);
4810 lr = ((int64_t)random() << 32) + random();
4811 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4813 fl.l_whence = SEEK_SET;
4815 fl.l_len = (off64_t)(random() % (1 << 20));
4816 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4818 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4819 procid, opno, f.path, st,
4820 (long long)off, (long long)fl.l_len, e);
4826 write_f(int opno, long r)
4840 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4842 printf("%d/%d: write - no filename\n", procid, opno);
4846 fd = open_path(&f, O_WRONLY);
4847 e = fd < 0 ? errno : 0;
4851 printf("%d/%d: write - open %s failed %d\n",
4852 procid, opno, f.path, e);
4856 if (fstat64(fd, &stb) < 0) {
4858 printf("%d/%d: write - fstat64 %s failed %d\n",
4859 procid, opno, f.path, errno);
4864 inode_info(st, sizeof(st), &stb, v);
4865 lr = ((int64_t)random() << 32) + random();
4866 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4868 lseek64(fd, off, SEEK_SET);
4869 len = (random() % FILELEN_MAX) + 1;
4871 memset(buf, nameseq & 0xff, len);
4872 e = write(fd, buf, len) < 0 ? errno : 0;
4875 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4876 procid, opno, f.path, st, (long long)off, (int)len, e);
4882 writev_f(int opno, long r)
4894 struct iovec *iov = NULL;
4901 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4903 printf("%d/%d: writev - no filename\n", procid, opno);
4907 fd = open_path(&f, O_WRONLY);
4908 e = fd < 0 ? errno : 0;
4912 printf("%d/%d: writev - open %s failed %d\n",
4913 procid, opno, f.path, e);
4917 if (fstat64(fd, &stb) < 0) {
4919 printf("%d/%d: writev - fstat64 %s failed %d\n",
4920 procid, opno, f.path, errno);
4925 inode_info(st, sizeof(st), &stb, v);
4926 lr = ((int64_t)random() << 32) + random();
4927 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4929 lseek64(fd, off, SEEK_SET);
4930 len = (random() % FILELEN_MAX) + 1;
4932 memset(buf, nameseq & 0xff, len);
4934 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4935 iov = calloc(iovcnt, sizeof(struct iovec));
4936 iovl = len / iovcnt;
4938 for (i=0; i<iovcnt; i++) {
4939 (iov + i)->iov_base = (buf + iovb);
4940 (iov + i)->iov_len = iovl;
4944 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4948 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4949 procid, opno, f.path, st, (long long)off, (int)iovl,
4956 xattr_flag_to_string(int flag)
4958 if (flag == XATTR_CREATE)
4960 if (flag == XATTR_REPLACE)