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>
31 #ifndef FS_IOC_GETFLAGS
32 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
34 #ifndef FS_IOC_SETFLAGS
35 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
39 #define XFS_ERRTAG_MAX 17
40 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
41 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
46 #define FILELEN_MAX (32*4096)
100 typedef void (*opfnc_t)(int, long);
102 typedef struct opdesc {
110 typedef struct fent {
115 typedef struct flist {
122 typedef struct pathname {
132 struct print_string {
139 #define FT_DIRm (1 << FT_DIR)
141 #define FT_REGm (1 << FT_REG)
143 #define FT_SYMm (1 << FT_SYM)
145 #define FT_DEVm (1 << FT_DEV)
147 #define FT_RTFm (1 << FT_RTF)
149 #define FT_ANYm ((1 << FT_nft) - 1)
150 #define FT_REGFILE (FT_REGm | FT_RTFm)
151 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
153 #define FLIST_SLOT_INCR 16
156 #define MAXFSIZE ((1ULL << 63) - 1ULL)
157 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
159 void afsync_f(int, long);
160 void allocsp_f(int, long);
161 void aread_f(int, long);
162 void attr_remove_f(int, long);
163 void attr_set_f(int, long);
164 void awrite_f(int, long);
165 void bulkstat_f(int, long);
166 void bulkstat1_f(int, long);
167 void chown_f(int, long);
168 void clonerange_f(int, long);
169 void copyrange_f(int, long);
170 void creat_f(int, long);
171 void deduperange_f(int, long);
172 void dread_f(int, long);
173 void dwrite_f(int, long);
174 void fallocate_f(int, long);
175 void fdatasync_f(int, long);
176 void fiemap_f(int, long);
177 void freesp_f(int, long);
178 void fsync_f(int, long);
179 void getattr_f(int, long);
180 void getdents_f(int, long);
181 void link_f(int, long);
182 void mkdir_f(int, long);
183 void mknod_f(int, long);
184 void mread_f(int, long);
185 void mwrite_f(int, long);
186 void punch_f(int, long);
187 void zero_f(int, long);
188 void collapse_f(int, long);
189 void insert_f(int, long);
190 void read_f(int, long);
191 void readlink_f(int, long);
192 void readv_f(int, long);
193 void rename_f(int, long);
194 void resvsp_f(int, long);
195 void rmdir_f(int, long);
196 void setattr_f(int, long);
197 void setxattr_f(int, long);
198 void splice_f(int, long);
199 void stat_f(int, long);
200 void symlink_f(int, long);
201 void sync_f(int, long);
202 void truncate_f(int, long);
203 void unlink_f(int, long);
204 void unresvsp_f(int, long);
205 void write_f(int, long);
206 void writev_f(int, long);
209 /* { OP_ENUM, "name", function, freq, iswrite }, */
210 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
211 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
212 { OP_AREAD, "aread", aread_f, 1, 0 },
213 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
214 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
215 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
216 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
217 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
218 { OP_CHOWN, "chown", chown_f, 3, 1 },
219 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
220 { OP_COPYRANGE, "copyrange", copyrange_f, 4, 1 },
221 { OP_CREAT, "creat", creat_f, 4, 1 },
222 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
223 { OP_DREAD, "dread", dread_f, 4, 0 },
224 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
225 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
226 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
227 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
228 { OP_FREESP, "freesp", freesp_f, 1, 1 },
229 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
230 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
231 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
232 { OP_LINK, "link", link_f, 1, 1 },
233 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
234 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
235 { OP_MREAD, "mread", mread_f, 2, 0 },
236 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
237 { OP_PUNCH, "punch", punch_f, 1, 1 },
238 { OP_ZERO, "zero", zero_f, 1, 1 },
239 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
240 { OP_INSERT, "insert", insert_f, 1, 1 },
241 { OP_READ, "read", read_f, 1, 0 },
242 { OP_READLINK, "readlink", readlink_f, 1, 0 },
243 { OP_READV, "readv", readv_f, 1, 0 },
244 { OP_RENAME, "rename", rename_f, 2, 1 },
245 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
246 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
247 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
248 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
249 { OP_SPLICE, "splice", splice_f, 1, 1 },
250 { OP_STAT, "stat", stat_f, 1, 0 },
251 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
252 { OP_SYNC, "sync", sync_f, 1, 1 },
253 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
254 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
255 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
256 { OP_WRITE, "write", write_f, 4, 1 },
257 { OP_WRITEV, "writev", writev_f, 4, 1 },
260 flist_t flist[FT_nft] = {
273 xfs_fsop_geom_t geom;
284 unsigned int idmodulo = XFS_IDMODULO_MAX;
285 unsigned int attr_mask = ~0;
288 unsigned long seed = 0;
292 int verifiable_log = 0;
293 sig_atomic_t should_stop = 0;
294 sigjmp_buf *sigbus_jmp = NULL;
295 char *execute_cmd = NULL;
296 int execute_freq = 1;
297 struct print_string flag_str = {0};
299 void add_to_flist(int, int, int);
300 void append_pathname(pathname_t *, char *);
301 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
302 int attr_remove_path(pathname_t *, const char *, int);
303 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
304 void check_cwd(void);
305 void cleanup_flist(void);
306 int creat_path(pathname_t *, mode_t);
307 void dcache_enter(int, int);
308 void dcache_init(void);
309 fent_t *dcache_lookup(int);
310 void dcache_purge(int);
311 void del_from_flist(int, int);
312 int dirid_to_name(char *, int);
314 int fent_to_name(pathname_t *, flist_t *, fent_t *);
315 void fix_parent(int, int);
316 void free_pathname(pathname_t *);
317 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
318 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
319 void init_pathname(pathname_t *);
320 int lchown_path(pathname_t *, uid_t, gid_t);
321 int link_path(pathname_t *, pathname_t *);
322 int lstat64_path(pathname_t *, struct stat64 *);
323 void make_freq_table(void);
324 int mkdir_path(pathname_t *, mode_t);
325 int mknod_path(pathname_t *, mode_t, dev_t);
326 void namerandpad(int, char *, int);
327 int open_file_or_dir(pathname_t *, int);
328 int open_path(pathname_t *, int);
329 DIR *opendir_path(pathname_t *);
330 void process_freq(char *);
331 int readlink_path(pathname_t *, char *, size_t);
332 int rename_path(pathname_t *, pathname_t *);
333 int rmdir_path(pathname_t *);
334 void separate_pathname(pathname_t *, char *, pathname_t *);
335 void show_ops(int, char *);
336 int stat64_path(pathname_t *, struct stat64 *);
337 int symlink_path(const char *, pathname_t *);
338 int truncate64_path(pathname_t *, off64_t);
339 int unlink_path(pathname_t *);
341 void write_freq(void);
342 void zero_freq(void);
344 void sg_handler(int signum)
352 * Only handle SIGBUS when mmap write to a hole and no
353 * block can be allocated due to ENOSPC, abort otherwise.
356 siglongjmp(*sigbus_jmp, -1);
358 printf("Unknown SIGBUS is caught, Abort!\n");
361 /* should not reach here */
368 int main(int argc, char **argv)
372 char *dirname = NULL;
373 char *logname = NULL;
374 char rpath[PATH_MAX];
383 xfs_error_injection_t err_inj;
384 struct sigaction action;
386 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
388 errrange = errtag = 0;
390 nops = sizeof(ops) / sizeof(ops[0]);
391 ops_end = &ops[nops];
393 while ((c = getopt(argc, argv, allopts)) != -1) {
402 sscanf(optarg, "%d", &errtag);
406 } else if (errtag == 0)
408 if (errtag >= XFS_ERRTAG_MAX) {
410 "error tag %d too large (max %d)\n",
411 errtag, XFS_ERRTAG_MAX - 1);
416 process_freq(optarg);
419 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
420 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
423 idmodulo = strtoul(optarg, NULL, 0);
424 if (idmodulo > XFS_IDMODULO_MAX) {
426 "chown modulo %d too big (max %d)\n",
427 idmodulo, XFS_IDMODULO_MAX);
432 loops = atoi(optarg);
435 operations = atoi(optarg);
442 nproc = atoi(optarg);
448 seed = strtoul(optarg, NULL, 0);
457 execute_cmd = optarg;
463 attr_mask = strtoul(optarg, NULL, 0);
467 if (optarg[0] == 'c')
478 execute_freq = strtoul(optarg, NULL, 0);
481 fprintf(stderr, "%s - invalid parameters\n",
491 /* no directory specified */
492 if (!nousage) usage();
496 (void)mkdir(dirname, 0777);
497 if (logname && logname[0] != '/') {
498 if (!getcwd(rpath, sizeof(rpath))){
499 perror("getcwd failed");
505 if (chdir(dirname) < 0) {
510 char path[PATH_MAX + NAME_MAX + 1];
511 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
512 if (freopen(path, "a", stdout) == NULL) {
513 perror("freopen logfile failed");
517 sprintf(buf, "fss%x", (unsigned int)getpid());
518 fd = creat(buf, 0666);
519 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
520 maxfsize = (off64_t)MAXFSIZE32;
522 maxfsize = (off64_t)MAXFSIZE;
527 gettimeofday(&t, (void *)NULL);
528 seed = (int)t.tv_sec ^ (int)t.tv_usec;
529 printf("seed = %ld\n", seed);
531 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
532 if (i >= 0 && geom.rtblocks)
533 rtpct = MIN(MAX(geom.rtblocks * 100 /
534 (geom.rtblocks + geom.datablocks), 1), 99);
543 for (i = 0; i < j; i++)
546 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
552 for (i = 0; i < j; i++)
555 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
557 printf("Injecting failure on tag #%d\n", errtag);
558 err_inj.errtag = errtag;
560 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
562 perror("fsstress - XFS_SYSSGI error injection call");
571 action.sa_handler = sg_handler;
572 sigemptyset(&action.sa_mask);
574 if (sigaction(SIGTERM, &action, 0)) {
575 perror("sigaction failed");
579 for (i = 0; i < nproc; i++) {
581 sigemptyset(&action.sa_mask);
582 action.sa_handler = SIG_DFL;
583 if (sigaction(SIGTERM, &action, 0))
585 action.sa_handler = sg_handler;
586 if (sigaction(SIGBUS, &action, 0))
588 #ifdef HAVE_SYS_PRCTL_H
589 prctl(PR_SET_PDEATHSIG, SIGKILL);
590 if (getppid() == 1) /* parent died already? */
594 char path[PATH_MAX + NAME_MAX + 2 + 11];
595 snprintf(path, sizeof(path), "%s/%s.%d",
597 if (freopen(path, "a", stdout) == NULL) {
598 perror("freopen logfile failed");
604 if (io_setup(128, &io_ctx) != 0) {
605 fprintf(stderr, "io_setup failed");
609 for (i = 0; !loops || (i < loops); i++)
612 if(io_destroy(io_ctx) != 0) {
613 fprintf(stderr, "io_destroy failed");
623 while (wait(&stat) > 0 && !should_stop) {
626 action.sa_flags = SA_RESTART;
627 sigaction(SIGTERM, &action, 0);
628 kill(-getpid(), SIGTERM);
629 while (wait(&stat) > 0)
635 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
637 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
639 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
652 add_string(struct print_string *str, const char *add)
654 int len = strlen(add);
659 if (len > (str->max - 1) - str->len) {
660 str->len = str->max - 1;
664 memcpy(str->buffer + str->len, add, len);
666 str->buffer[str->len] = '\0';
672 translate_flags(int flags, const char *delim,
673 const struct print_flags *flag_array)
675 int i, mask, first = 1;
678 if (!flag_str.buffer) {
679 flag_str.buffer = malloc(4096);
683 if (!flag_str.buffer)
686 flag_str.buffer[0] = '\0';
688 for (i = 0; flag_array[i].name && flags; i++) {
689 mask = flag_array[i].mask;
690 if ((flags & mask) != mask)
693 add = flag_array[i].name;
696 add_string(&flag_str, delim);
699 add_string(&flag_str, add);
702 /* Check whether there are any leftover flags. */
708 add_string(&flag_str, delim);
710 ret = snprintf(number, 11, "0x%x", flags) > 0;
711 if (ret > 0 && ret <= 11)
712 add_string(&flag_str, number);
715 return flag_str.buffer;
719 add_to_flist(int ft, int id, int parent)
725 if (ftp->nfiles == ftp->nslots) {
726 ftp->nslots += FLIST_SLOT_INCR;
727 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
729 fep = &ftp->fents[ftp->nfiles++];
731 fep->parent = parent;
735 append_pathname(pathname_t *name, char *str)
741 /* attempting to append to a dir a zero length path */
742 if (len && *str == '/' && name->len == 0) {
743 fprintf(stderr, "fsstress: append_pathname failure\n");
744 assert(chdir(homedir) == 0);
749 name->path = realloc(name->path, name->len + 1 + len);
750 strcpy(&name->path[name->len], str);
755 attr_list_path(pathname_t *name,
757 const int buffersize,
759 attrlist_cursor_t *cursor)
761 char buf[NAME_MAX + 1];
765 if (flags != ATTR_DONTFOLLOW) {
770 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
771 if (rval >= 0 || errno != ENAMETOOLONG)
773 separate_pathname(name, buf, &newname);
774 if (chdir(buf) == 0) {
775 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
776 assert(chdir("..") == 0);
778 free_pathname(&newname);
783 attr_remove_path(pathname_t *name, const char *attrname, int flags)
785 char buf[NAME_MAX + 1];
789 rval = attr_remove(name->path, attrname, flags);
790 if (rval >= 0 || errno != ENAMETOOLONG)
792 separate_pathname(name, buf, &newname);
793 if (chdir(buf) == 0) {
794 rval = attr_remove_path(&newname, attrname, flags);
795 assert(chdir("..") == 0);
797 free_pathname(&newname);
802 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
803 const int valuelength, int flags)
805 char buf[NAME_MAX + 1];
809 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
810 if (rval >= 0 || errno != ENAMETOOLONG)
812 separate_pathname(name, buf, &newname);
813 if (chdir(buf) == 0) {
814 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
816 assert(chdir("..") == 0);
818 free_pathname(&newname);
826 struct stat64 statbuf;
828 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
830 assert(chdir(homedir) == 0);
831 fprintf(stderr, "fsstress: check_cwd failure\n");
838 * go thru flist and release all entries
840 * NOTE: this function shouldn't be called until the end of a process
848 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
857 creat_path(pathname_t *name, mode_t mode)
859 char buf[NAME_MAX + 1];
863 rval = creat(name->path, mode);
864 if (rval >= 0 || errno != ENAMETOOLONG)
866 separate_pathname(name, buf, &newname);
867 if (chdir(buf) == 0) {
868 rval = creat_path(&newname, mode);
869 assert(chdir("..") == 0);
871 free_pathname(&newname);
876 dcache_enter(int dirid, int slot)
878 dcache[dirid % NDCACHE] = slot;
886 for (i = 0; i < NDCACHE; i++)
891 dcache_lookup(int dirid)
896 i = dcache[dirid % NDCACHE];
897 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
903 dcache_purge(int dirid)
907 dcp = &dcache[dirid % NDCACHE];
908 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
913 * Delete the item from the list by
914 * moving last entry over the deleted one;
915 * unless deleted entry is the last one.
916 * Input: which file list array and which slot in array
919 del_from_flist(int ft, int slot)
925 dcache_purge(ftp->fents[slot].id);
926 if (slot != ftp->nfiles - 1) {
928 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
929 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
935 dirid_to_fent(int dirid)
941 if ((fep = dcache_lookup(dirid)))
943 flp = &flist[FT_DIR];
944 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
945 if (fep->id == dirid) {
946 dcache_enter(dirid, fep - flp->fents);
956 struct stat64 statbuf;
964 dividend = (operations + execute_freq) / (execute_freq + 1);
965 sprintf(buf, "p%x", procid);
966 (void)mkdir(buf, 0777);
967 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
971 top_ino = statbuf.st_ino;
972 homedir = getcwd(NULL, 0);
974 perror("getcwd failed");
981 for (opno = 0; opno < operations; opno++) {
982 if (execute_cmd && opno && opno % dividend == 0) {
984 printf("%d: execute command %s\n", opno,
986 rval = system(execute_cmd);
988 fprintf(stderr, "execute command failed with "
991 p = &ops[freq_table[random() % freq_table_size]];
992 p->func(opno, random());
994 * test for forced shutdown by stat'ing the test
995 * directory. If this stat returns EIO, assume
996 * the forced shutdown happened.
998 if (errtag != 0 && opno % 100 == 0) {
999 rval = stat64(".", &statbuf);
1001 fprintf(stderr, "Detected EIO\n");
1007 assert(chdir("..") == 0);
1012 sprintf(cmd, "rm -rf %s", buf);
1015 perror("cleaning up");
1021 * build up a pathname going thru the file entry and all
1022 * its parent entries
1023 * Return 0 on error, 1 on success;
1026 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1028 char buf[NAME_MAX + 1];
1036 /* build up parent directory name */
1037 if (fep->parent != -1) {
1038 pfep = dirid_to_fent(fep->parent);
1041 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1042 procid, fep->id, fep->parent);
1047 e = fent_to_name(name, &flist[FT_DIR], pfep);
1050 append_pathname(name, "/");
1053 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1054 namerandpad(fep->id, buf, i);
1055 append_pathname(name, buf);
1060 fix_parent(int oldid, int newid)
1067 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1068 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1069 if (fep->parent == oldid)
1070 fep->parent = newid;
1076 free_pathname(pathname_t *name)
1086 * Generate a filename of type ft.
1087 * If we have a fep which should be a directory then use it
1088 * as the parent path for this new filename i.e. prepend with it.
1091 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1093 char buf[NAME_MAX + 1];
1102 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1103 namerandpad(id, buf, len);
1105 /* prepend fep parent dir-name to it */
1107 e = fent_to_name(name, &flist[FT_DIR], fep);
1110 append_pathname(name, "/");
1112 append_pathname(name, buf);
1116 for (j = 0; !*v && j < ilistlen; j++) {
1117 if (ilist[j] == id) {
1127 * Input: "which" to choose the file-types eg. non-directory
1128 * Input: "r" to choose which file
1129 * Output: file-list, file-entry, name for the chosen file.
1130 * Output: verbose if chosen file is on the ilist.
1133 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1136 int totalsum = 0; /* total number of matching files */
1137 int partialsum = 0; /* partial sum of matching files */
1143 int e = 1; /* success */
1146 * go thru flist and add up number of files for each
1147 * category that matches with <which>.
1149 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1150 if (which & (1 << i))
1151 totalsum += flp->nfiles;
1153 if (totalsum == 0) {
1163 * Now we have possible matches between 0..totalsum-1.
1164 * And we use r to help us choose which one we want,
1165 * which when bounded by totalsum becomes x.
1167 x = (int)(r % totalsum);
1168 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1169 if (which & (1 << i)) {
1170 if (x < partialsum + flp->nfiles) {
1172 /* found the matching file entry */
1173 fep = &flp->fents[x - partialsum];
1175 /* fill-in what we were asked for */
1177 e = fent_to_name(name, flp, fep);
1180 fprintf(stderr, "%d: failed to get path for entry:"
1181 " id=%d,parent=%d\n",
1182 procid, fep->id, fep->parent);
1191 /* turn on verbose if its an ilisted file */
1193 for (j = 0; !*v && j < ilistlen; j++) {
1194 if (ilist[j] == fep->id) {
1201 partialsum += flp->nfiles;
1205 fprintf(stderr, "fsstress: get_fname failure\n");
1212 init_pathname(pathname_t *name)
1219 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1221 char buf[NAME_MAX + 1];
1225 rval = lchown(name->path, owner, group);
1226 if (rval >= 0 || errno != ENAMETOOLONG)
1228 separate_pathname(name, buf, &newname);
1229 if (chdir(buf) == 0) {
1230 rval = lchown_path(&newname, owner, group);
1231 assert(chdir("..") == 0);
1233 free_pathname(&newname);
1238 link_path(pathname_t *name1, pathname_t *name2)
1240 char buf1[NAME_MAX + 1];
1241 char buf2[NAME_MAX + 1];
1243 pathname_t newname1;
1244 pathname_t newname2;
1247 rval = link(name1->path, name2->path);
1248 if (rval >= 0 || errno != ENAMETOOLONG)
1250 separate_pathname(name1, buf1, &newname1);
1251 separate_pathname(name2, buf2, &newname2);
1252 if (strcmp(buf1, buf2) == 0) {
1253 if (chdir(buf1) == 0) {
1254 rval = link_path(&newname1, &newname2);
1255 assert(chdir("..") == 0);
1258 if (strcmp(buf1, "..") == 0)
1260 else if (strcmp(buf2, "..") == 0)
1262 else if (strlen(buf1) == 0)
1264 else if (strlen(buf2) == 0)
1267 down1 = MAX(newname1.len, 3 + name2->len) <=
1268 MAX(3 + name1->len, newname2.len);
1270 free_pathname(&newname2);
1271 append_pathname(&newname2, "../");
1272 append_pathname(&newname2, name2->path);
1273 if (chdir(buf1) == 0) {
1274 rval = link_path(&newname1, &newname2);
1275 assert(chdir("..") == 0);
1278 free_pathname(&newname1);
1279 append_pathname(&newname1, "../");
1280 append_pathname(&newname1, name1->path);
1281 if (chdir(buf2) == 0) {
1282 rval = link_path(&newname1, &newname2);
1283 assert(chdir("..") == 0);
1287 free_pathname(&newname1);
1288 free_pathname(&newname2);
1293 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1295 char buf[NAME_MAX + 1];
1299 rval = lstat64(name->path, sbuf);
1300 if (rval >= 0 || errno != ENAMETOOLONG)
1302 separate_pathname(name, buf, &newname);
1303 if (chdir(buf) == 0) {
1304 rval = lstat64_path(&newname, sbuf);
1305 assert(chdir("..") == 0);
1307 free_pathname(&newname);
1312 make_freq_table(void)
1318 for (p = ops, f = 0; p < ops_end; p++)
1320 freq_table = malloc(f * sizeof(*freq_table));
1321 freq_table_size = f;
1322 for (p = ops, i = 0; p < ops_end; p++) {
1323 for (f = 0; f < p->freq; f++, i++)
1324 freq_table[i] = p->op;
1329 mkdir_path(pathname_t *name, mode_t mode)
1331 char buf[NAME_MAX + 1];
1335 rval = mkdir(name->path, mode);
1336 if (rval >= 0 || errno != ENAMETOOLONG)
1338 separate_pathname(name, buf, &newname);
1339 if (chdir(buf) == 0) {
1340 rval = mkdir_path(&newname, mode);
1341 assert(chdir("..") == 0);
1343 free_pathname(&newname);
1348 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1350 char buf[NAME_MAX + 1];
1354 rval = mknod(name->path, mode, dev);
1355 if (rval >= 0 || errno != ENAMETOOLONG)
1357 separate_pathname(name, buf, &newname);
1358 if (chdir(buf) == 0) {
1359 rval = mknod_path(&newname, mode, dev);
1360 assert(chdir("..") == 0);
1362 free_pathname(&newname);
1367 namerandpad(int id, char *buf, int i)
1370 static int buckets[] =
1371 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1377 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1378 padmod = buckets[bucket] + 1 - i;
1381 padlen = (id ^ namerand) % padmod;
1383 memset(&buf[i], 'X', padlen);
1384 buf[i + padlen] = '\0';
1389 open_file_or_dir(pathname_t *name, int flags)
1393 fd = open_path(name, flags);
1396 if (fd == -1 && errno != EISDIR)
1398 /* Directories can not be opened in write mode nor direct mode. */
1399 flags &= ~(O_WRONLY | O_DIRECT);
1400 flags |= O_RDONLY | O_DIRECTORY;
1401 return open_path(name, flags);
1405 open_path(pathname_t *name, int oflag)
1407 char buf[NAME_MAX + 1];
1411 rval = open(name->path, oflag);
1412 if (rval >= 0 || errno != ENAMETOOLONG)
1414 separate_pathname(name, buf, &newname);
1415 if (chdir(buf) == 0) {
1416 rval = open_path(&newname, oflag);
1417 assert(chdir("..") == 0);
1419 free_pathname(&newname);
1424 opendir_path(pathname_t *name)
1426 char buf[NAME_MAX + 1];
1430 rval = opendir(name->path);
1431 if (rval || errno != ENAMETOOLONG)
1433 separate_pathname(name, buf, &newname);
1434 if (chdir(buf) == 0) {
1435 rval = opendir_path(&newname);
1436 assert(chdir("..") == 0);
1438 free_pathname(&newname);
1443 process_freq(char *arg)
1448 s = strchr(arg, '=');
1450 fprintf(stderr, "bad argument '%s'\n", arg);
1454 for (p = ops; p < ops_end; p++) {
1455 if (strcmp(arg, p->name) == 0) {
1460 fprintf(stderr, "can't find op type %s for -f\n", arg);
1465 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1467 char buf[NAME_MAX + 1];
1471 rval = readlink(name->path, lbuf, lbufsiz);
1472 if (rval >= 0 || errno != ENAMETOOLONG)
1474 separate_pathname(name, buf, &newname);
1475 if (chdir(buf) == 0) {
1476 rval = readlink_path(&newname, lbuf, lbufsiz);
1477 assert(chdir("..") == 0);
1479 free_pathname(&newname);
1484 rename_path(pathname_t *name1, pathname_t *name2)
1486 char buf1[NAME_MAX + 1];
1487 char buf2[NAME_MAX + 1];
1489 pathname_t newname1;
1490 pathname_t newname2;
1493 rval = rename(name1->path, name2->path);
1494 if (rval >= 0 || errno != ENAMETOOLONG)
1496 separate_pathname(name1, buf1, &newname1);
1497 separate_pathname(name2, buf2, &newname2);
1498 if (strcmp(buf1, buf2) == 0) {
1499 if (chdir(buf1) == 0) {
1500 rval = rename_path(&newname1, &newname2);
1501 assert(chdir("..") == 0);
1504 if (strcmp(buf1, "..") == 0)
1506 else if (strcmp(buf2, "..") == 0)
1508 else if (strlen(buf1) == 0)
1510 else if (strlen(buf2) == 0)
1513 down1 = MAX(newname1.len, 3 + name2->len) <=
1514 MAX(3 + name1->len, newname2.len);
1516 free_pathname(&newname2);
1517 append_pathname(&newname2, "../");
1518 append_pathname(&newname2, name2->path);
1519 if (chdir(buf1) == 0) {
1520 rval = rename_path(&newname1, &newname2);
1521 assert(chdir("..") == 0);
1524 free_pathname(&newname1);
1525 append_pathname(&newname1, "../");
1526 append_pathname(&newname1, name1->path);
1527 if (chdir(buf2) == 0) {
1528 rval = rename_path(&newname1, &newname2);
1529 assert(chdir("..") == 0);
1533 free_pathname(&newname1);
1534 free_pathname(&newname2);
1539 rmdir_path(pathname_t *name)
1541 char buf[NAME_MAX + 1];
1545 rval = rmdir(name->path);
1546 if (rval >= 0 || errno != ENAMETOOLONG)
1548 separate_pathname(name, buf, &newname);
1549 if (chdir(buf) == 0) {
1550 rval = rmdir_path(&newname);
1551 assert(chdir("..") == 0);
1553 free_pathname(&newname);
1558 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1562 init_pathname(newname);
1563 slash = strchr(name->path, '/');
1564 if (slash == NULL) {
1569 strcpy(buf, name->path);
1571 append_pathname(newname, slash + 1);
1577 show_ops(int flag, char *lead_str)
1582 /* print in list form */
1585 for (p = ops; p < ops_end; p++) {
1586 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1587 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1588 x+=printf("%s ", p->name);
1591 } else if (flag == 0) {
1592 /* Table view style */
1594 for (f = 0, p = ops; p < ops_end; p++)
1600 for (p = ops; p < ops_end; p++) {
1601 if (flag != 0 || p->freq > 0) {
1602 if (lead_str != NULL)
1603 printf("%s", lead_str);
1604 printf("%20s %d/%d %s\n",
1605 p->name, p->freq, f,
1606 (p->iswrite == 0) ? " " : "write op");
1610 /* Command line style */
1611 if (lead_str != NULL)
1612 printf("%s", lead_str);
1613 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1615 for (p = ops; p < ops_end; p++)
1617 printf("-f %s=%d \\\n",p->name, p->freq);
1622 stat64_path(pathname_t *name, struct stat64 *sbuf)
1624 char buf[NAME_MAX + 1];
1628 rval = stat64(name->path, sbuf);
1629 if (rval >= 0 || errno != ENAMETOOLONG)
1631 separate_pathname(name, buf, &newname);
1632 if (chdir(buf) == 0) {
1633 rval = stat64_path(&newname, sbuf);
1634 assert(chdir("..") == 0);
1636 free_pathname(&newname);
1641 symlink_path(const char *name1, pathname_t *name)
1643 char buf[NAME_MAX + 1];
1647 if (!strcmp(name1, name->path)) {
1648 printf("yikes! %s %s\n", name1, name->path);
1652 rval = symlink(name1, name->path);
1653 if (rval >= 0 || errno != ENAMETOOLONG)
1655 separate_pathname(name, buf, &newname);
1656 if (chdir(buf) == 0) {
1657 rval = symlink_path(name1, &newname);
1658 assert(chdir("..") == 0);
1660 free_pathname(&newname);
1665 truncate64_path(pathname_t *name, off64_t length)
1667 char buf[NAME_MAX + 1];
1671 rval = truncate64(name->path, length);
1672 if (rval >= 0 || errno != ENAMETOOLONG)
1674 separate_pathname(name, buf, &newname);
1675 if (chdir(buf) == 0) {
1676 rval = truncate64_path(&newname, length);
1677 assert(chdir("..") == 0);
1679 free_pathname(&newname);
1684 unlink_path(pathname_t *name)
1686 char buf[NAME_MAX + 1];
1690 rval = unlink(name->path);
1691 if (rval >= 0 || errno != ENAMETOOLONG)
1693 separate_pathname(name, buf, &newname);
1694 if (chdir(buf) == 0) {
1695 rval = unlink_path(&newname);
1696 assert(chdir("..") == 0);
1698 free_pathname(&newname);
1705 printf("Usage: %s -H or\n", myprog);
1706 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1708 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1710 printf(" -c clean up the test directory after each run\n");
1711 printf(" -d dir specifies the base directory for operations\n");
1712 printf(" -e errtg specifies error injection stuff\n");
1713 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1714 printf(" the valid operation names are:\n");
1716 printf(" -i filenum get verbose output for this nth file object\n");
1717 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1718 printf(" *use 0 for infinite (default 1)\n");
1719 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1720 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1721 printf(" -o logfile specifies logfile name\n");
1722 printf(" -p nproc specifies the no. of processes (default 1)\n");
1723 printf(" -r specifies random name padding\n");
1724 printf(" -s seed specifies the seed for the random generator (default random)\n");
1725 printf(" -v specifies verbose mode\n");
1726 printf(" -w zeros frequencies of non-write operations\n");
1727 printf(" -x cmd execute command in the middle of operations\n");
1728 printf(" -z zeros frequencies of all operations\n");
1729 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1730 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1731 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1732 printf(" -H prints usage and exits\n");
1740 for (p = ops; p < ops_end; p++) {
1751 for (p = ops; p < ops_end; p++)
1755 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1758 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1759 verifiable_log ? -1: (long)s->st_ino,
1760 (long)s->st_nlink, s->st_uid, s->st_gid,
1761 (long long) s->st_blocks, (long long) s->st_size);
1765 afsync_f(int opno, long r)
1773 struct iocb *iocbs[] = { &iocb };
1774 struct io_event event;
1777 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
1779 printf("%d/%d: afsync - no filename\n", procid, opno);
1783 fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
1784 e = fd < 0 ? errno : 0;
1788 printf("%d/%d: afsync - open %s failed %d\n",
1789 procid, opno, f.path, e);
1794 io_prep_fsync(&iocb, fd);
1795 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1797 printf("%d/%d: afsync - io_submit %s %d\n",
1798 procid, opno, f.path, e);
1803 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1805 printf("%d/%d: afsync - io_getevents failed %d\n",
1814 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1821 allocsp_f(int opno, long r)
1826 struct xfs_flock64 fl;
1834 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1836 printf("%d/%d: allocsp - no filename\n", procid, opno);
1840 fd = open_path(&f, O_RDWR);
1841 e = fd < 0 ? errno : 0;
1845 printf("%d/%d: allocsp - open %s failed %d\n",
1846 procid, opno, f.path, e);
1850 if (fstat64(fd, &stb) < 0) {
1852 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1853 procid, opno, f.path, errno);
1858 inode_info(st, sizeof(st), &stb, v);
1859 lr = ((int64_t)random() << 32) + random();
1860 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1862 fl.l_whence = SEEK_SET;
1865 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1867 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1868 procid, opno, f.path, st, (long long)off, e);
1876 do_aio_rw(int opno, long r, int flags)
1880 struct dioattr diob;
1892 struct io_event event;
1893 struct iocb *iocbs[] = { &iocb };
1894 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1897 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1899 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1903 fd = open_path(&f, flags|O_DIRECT);
1904 e = fd < 0 ? errno : 0;
1908 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1909 procid, opno, f.path, e);
1913 if (fstat64(fd, &stb) < 0) {
1915 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1916 procid, opno, f.path, errno);
1921 inode_info(st, sizeof(st), &stb, v);
1922 if (!iswrite && stb.st_size == 0) {
1924 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1930 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1933 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1934 " fallback to stat()\n",
1935 procid, opno, f.path, st, errno);
1936 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1937 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1939 dio_env = getenv("XFS_DIO_MIN");
1941 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1942 align = (int64_t)diob.d_miniosz;
1943 lr = ((int64_t)random() << 32) + random();
1944 len = (random() % FILELEN_MAX) + 1;
1945 len -= (len % align);
1948 else if (len > diob.d_maxiosz)
1949 len = diob.d_maxiosz;
1950 buf = memalign(diob.d_mem, len);
1953 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1954 off -= (off % align);
1956 memset(buf, nameseq & 0xff, len);
1957 io_prep_pwrite(&iocb, fd, buf, len, off);
1959 off = (off64_t)(lr % stb.st_size);
1960 off -= (off % align);
1961 io_prep_pread(&iocb, fd, buf, len, off);
1963 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1965 printf("%d/%d: %s - io_submit failed %d\n",
1966 procid, opno, iswrite ? "awrite" : "aread", e);
1971 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1973 printf("%d/%d: %s - io_getevents failed %d\n",
1974 procid, opno, iswrite ? "awrite" : "aread", e);
1980 e = event.res != len ? event.res2 : 0;
1983 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1984 procid, opno, iswrite ? "awrite" : "aread",
1985 f.path, st, (long long)off, (int)len, e);
1992 aread_f(int opno, long r)
1995 do_aio_rw(opno, r, O_RDONLY);
2000 attr_remove_f(int opno, long r)
2002 attrlist_ent_t *aep;
2006 attrlist_cursor_t cursor;
2015 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2016 append_pathname(&f, ".");
2018 bzero(&cursor, sizeof(cursor));
2020 bzero(buf, sizeof(buf));
2021 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2025 alist = (attrlist_t *)buf;
2026 total += alist->al_count;
2027 } while (alist->al_more);
2030 printf("%d/%d: attr_remove - no attrs for %s\n",
2031 procid, opno, f.path);
2035 which = (int)(random() % total);
2036 bzero(&cursor, sizeof(cursor));
2040 bzero(buf, sizeof(buf));
2041 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2045 alist = (attrlist_t *)buf;
2046 if (which < ent + alist->al_count) {
2047 aep = (attrlist_ent_t *)
2048 &buf[alist->al_offset[which - ent]];
2049 aname = aep->a_name;
2052 ent += alist->al_count;
2053 } while (alist->al_more);
2054 if (aname == NULL) {
2057 "%d/%d: attr_remove - name %d not found at %s\n",
2058 procid, opno, which, f.path);
2062 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2065 printf("%d/%d: attr_remove %s %s %d\n",
2066 procid, opno, f.path, aname, e);
2071 attr_set_f(int opno, long r)
2078 static int lengths[] = { 10, 100, 1000, 10000 };
2083 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2084 append_pathname(&f, ".");
2085 sprintf(aname, "a%x", nameseq++);
2086 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2087 len = (int)(random() % lengths[li]);
2091 memset(aval, nameseq & 0xff, len);
2092 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2097 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2103 awrite_f(int opno, long r)
2106 do_aio_rw(opno, r, O_WRONLY);
2111 bulkstat_f(int opno, long r)
2119 xfs_fsop_bulkreq_t bsr;
2122 nent = (r % 999) + 2;
2123 t = malloc(nent * sizeof(*t));
2124 fd = open(".", O_RDONLY);
2132 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2136 printf("%d/%d: bulkstat nent %d total %lld\n",
2137 procid, opno, nent, (long long)total);
2142 bulkstat1_f(int opno, long r)
2152 xfs_fsop_bulkreq_t bsr;
2155 good = random() & 1;
2157 /* use an inode we know exists */
2159 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2160 append_pathname(&f, ".");
2161 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2166 * pick a random inode
2168 * note this can generate kernel warning messages
2169 * since bulkstat_one will read the disk block that
2170 * would contain a given inode even if that disk
2171 * block doesn't contain inodes.
2173 * this is detected later, but not until after the
2174 * warning is displayed.
2176 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2182 fd = open(".", O_RDONLY);
2188 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2190 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2191 procid, opno, good?"real":"random",
2192 verifiable_log ? -1LL : (long long)ino, e);
2197 chown_f(int opno, long r)
2207 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2208 append_pathname(&f, ".");
2209 u = (uid_t)random();
2210 g = (gid_t)random();
2211 nbits = (int)(random() % idmodulo);
2212 u &= (1 << nbits) - 1;
2213 g &= (1 << nbits) - 1;
2214 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2217 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2221 /* reflink some arbitrary range of f1 to f2. */
2228 struct file_clone_range fcr;
2229 struct pathname fpath1;
2230 struct pathname fpath2;
2231 struct stat64 stat1;
2232 struct stat64 stat2;
2233 char inoinfo1[1024];
2234 char inoinfo2[1024];
2248 init_pathname(&fpath1);
2249 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2251 printf("%d/%d: clonerange read - no filename\n",
2256 init_pathname(&fpath2);
2257 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2259 printf("%d/%d: clonerange write - no filename\n",
2265 fd1 = open_path(&fpath1, O_RDONLY);
2266 e = fd1 < 0 ? errno : 0;
2270 printf("%d/%d: clonerange read - open %s failed %d\n",
2271 procid, opno, fpath1.path, e);
2275 fd2 = open_path(&fpath2, O_WRONLY);
2276 e = fd2 < 0 ? errno : 0;
2280 printf("%d/%d: clonerange write - open %s failed %d\n",
2281 procid, opno, fpath2.path, e);
2285 /* Get file stats */
2286 if (fstat64(fd1, &stat1) < 0) {
2288 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2289 procid, opno, fpath1.path, errno);
2292 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2294 if (fstat64(fd2, &stat2) < 0) {
2296 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2297 procid, opno, fpath2.path, errno);
2300 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2302 /* Calculate offsets */
2303 len = (random() % FILELEN_MAX) + 1;
2304 len &= ~(stat1.st_blksize - 1);
2306 len = stat1.st_blksize;
2307 if (len > stat1.st_size)
2308 len = stat1.st_size;
2310 lr = ((int64_t)random() << 32) + random();
2311 if (stat1.st_size == len)
2314 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2316 off1 &= ~(stat1.st_blksize - 1);
2319 * If srcfile == destfile, randomly generate destination ranges
2320 * until we find one that doesn't overlap the source range.
2322 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2324 lr = ((int64_t)random() << 32) + random();
2325 off2 = (off64_t)(lr % max_off2);
2327 off2 &= ~(stat2.st_blksize - 1);
2328 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2330 /* Clone data blocks */
2332 fcr.src_offset = off1;
2333 fcr.src_length = len;
2334 fcr.dest_offset = off2;
2336 ret = ioctl(fd2, FICLONERANGE, &fcr);
2337 e = ret < 0 ? errno : 0;
2339 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2341 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2342 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2345 printf(" error %d", e);
2354 free_pathname(&fpath2);
2356 free_pathname(&fpath1);
2360 /* copy some arbitrary range of f1 to f2. */
2366 #ifdef HAVE_COPY_FILE_RANGE
2367 struct pathname fpath1;
2368 struct pathname fpath2;
2369 struct stat64 stat1;
2370 struct stat64 stat2;
2371 char inoinfo1[1024];
2372 char inoinfo2[1024];
2390 init_pathname(&fpath1);
2391 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2393 printf("%d/%d: copyrange read - no filename\n",
2398 init_pathname(&fpath2);
2399 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2401 printf("%d/%d: copyrange write - no filename\n",
2407 fd1 = open_path(&fpath1, O_RDONLY);
2408 e = fd1 < 0 ? errno : 0;
2412 printf("%d/%d: copyrange read - open %s failed %d\n",
2413 procid, opno, fpath1.path, e);
2417 fd2 = open_path(&fpath2, O_WRONLY);
2418 e = fd2 < 0 ? errno : 0;
2422 printf("%d/%d: copyrange write - open %s failed %d\n",
2423 procid, opno, fpath2.path, e);
2427 /* Get file stats */
2428 if (fstat64(fd1, &stat1) < 0) {
2430 printf("%d/%d: copyrange read - fstat64 %s failed %d\n",
2431 procid, opno, fpath1.path, errno);
2434 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2436 if (fstat64(fd2, &stat2) < 0) {
2438 printf("%d/%d: copyrange write - fstat64 %s failed %d\n",
2439 procid, opno, fpath2.path, errno);
2442 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2444 /* Calculate offsets */
2445 len = (random() % FILELEN_MAX) + 1;
2447 len = stat1.st_blksize;
2448 if (len > stat1.st_size)
2449 len = stat1.st_size;
2451 lr = ((int64_t)random() << 32) + random();
2452 if (stat1.st_size == len)
2455 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2459 * If srcfile == destfile, randomly generate destination ranges
2460 * until we find one that doesn't overlap the source range.
2462 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2464 lr = ((int64_t)random() << 32) + random();
2465 off2 = (off64_t)(lr % max_off2);
2467 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2470 * Since len, off1 and off2 will be changed later, preserve their
2478 ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
2481 if (errno != EAGAIN || tries++ >= 300)
2483 } else if (ret > len || ret == 0)
2488 e = ret < 0 ? errno : 0;
2490 printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2492 fpath1.path, inoinfo1,
2493 (long long)offset1, (long long)length,
2494 fpath2.path, inoinfo2,
2495 (long long)offset2, (long long)length);
2498 printf(" error %d", e);
2499 else if (len && ret > len)
2500 printf(" asked for %lld, copied %lld??\n",
2501 (long long)len, (long long)ret);
2510 free_pathname(&fpath2);
2512 free_pathname(&fpath1);
2516 /* dedupe some arbitrary range of f1 to f2...fn. */
2522 #ifdef FIDEDUPERANGE
2523 #define INFO_SZ 1024
2524 struct file_dedupe_range *fdr;
2525 struct pathname *fpath;
2526 struct stat64 *stat;
2538 if (flist[FT_REG].nfiles < 2)
2541 /* Pick somewhere between 2 and 128 files. */
2543 nr = random() % (flist[FT_REG].nfiles + 1);
2544 } while (nr < 2 || nr > 128);
2547 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2548 sizeof(struct file_dedupe_range));
2550 printf("%d/%d: line %d error %d\n",
2551 procid, opno, __LINE__, errno);
2554 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2555 sizeof(struct file_dedupe_range)));
2557 fpath = calloc(nr, sizeof(struct pathname));
2559 printf("%d/%d: line %d error %d\n",
2560 procid, opno, __LINE__, errno);
2564 stat = calloc(nr, sizeof(struct stat64));
2566 printf("%d/%d: line %d error %d\n",
2567 procid, opno, __LINE__, errno);
2571 info = calloc(nr, INFO_SZ);
2573 printf("%d/%d: line %d error %d\n",
2574 procid, opno, __LINE__, errno);
2578 off = calloc(nr, sizeof(off64_t));
2580 printf("%d/%d: line %d error %d\n",
2581 procid, opno, __LINE__, errno);
2585 v = calloc(nr, sizeof(int));
2587 printf("%d/%d: line %d error %d\n",
2588 procid, opno, __LINE__, errno);
2591 fd = calloc(nr, sizeof(int));
2593 printf("%d/%d: line %d error %d\n",
2594 procid, opno, __LINE__, errno);
2597 memset(fd, 0xFF, nr * sizeof(int));
2599 /* Get paths for all files */
2600 for (i = 0; i < nr; i++)
2601 init_pathname(&fpath[i]);
2603 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2605 printf("%d/%d: deduperange read - no filename\n",
2610 for (i = 1; i < nr; i++) {
2611 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2613 printf("%d/%d: deduperange write - no filename\n",
2620 fd[0] = open_path(&fpath[0], O_RDONLY);
2621 e = fd[0] < 0 ? errno : 0;
2625 printf("%d/%d: deduperange read - open %s failed %d\n",
2626 procid, opno, fpath[0].path, e);
2630 for (i = 1; i < nr; i++) {
2631 fd[i] = open_path(&fpath[i], O_WRONLY);
2632 e = fd[i] < 0 ? errno : 0;
2636 printf("%d/%d: deduperange write - open %s failed %d\n",
2637 procid, opno, fpath[i].path, e);
2642 /* Get file stats */
2643 if (fstat64(fd[0], &stat[0]) < 0) {
2645 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2646 procid, opno, fpath[0].path, errno);
2650 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2652 for (i = 1; i < nr; i++) {
2653 if (fstat64(fd[i], &stat[i]) < 0) {
2655 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2656 procid, opno, fpath[i].path, errno);
2659 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2662 /* Never try to dedupe more than half of the src file. */
2663 len = (random() % FILELEN_MAX) + 1;
2664 len &= ~(stat[0].st_blksize - 1);
2666 len = stat[0].st_blksize / 2;
2667 if (len > stat[0].st_size / 2)
2668 len = stat[0].st_size / 2;
2670 /* Calculate offsets */
2671 lr = ((int64_t)random() << 32) + random();
2672 if (stat[0].st_size == len)
2675 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2677 off[0] &= ~(stat[0].st_blksize - 1);
2680 * If srcfile == destfile[i], randomly generate destination ranges
2681 * until we find one that doesn't overlap the source range.
2683 for (i = 1; i < nr; i++) {
2687 lr = ((int64_t)random() << 32) + random();
2688 if (stat[i].st_size <= len)
2691 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2693 off[i] &= ~(stat[i].st_blksize - 1);
2694 } while (stat[0].st_ino == stat[i].st_ino &&
2695 llabs(off[i] - off[0]) < len &&
2699 /* Clone data blocks */
2700 fdr->src_offset = off[0];
2701 fdr->src_length = len;
2702 fdr->dest_count = nr - 1;
2703 for (i = 1; i < nr; i++) {
2704 fdr->info[i - 1].dest_fd = fd[i];
2705 fdr->info[i - 1].dest_offset = off[i];
2708 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2709 e = ret < 0 ? errno : 0;
2711 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2713 fpath[0].path, &info[0], (long long)off[0],
2716 printf(" error %d", e);
2722 for (i = 1; i < nr; i++) {
2723 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2725 printf("%d/%d: ...to %s%s [%lld,%lld]",
2727 fpath[i].path, &info[i * INFO_SZ],
2728 (long long)off[i], (long long)len);
2729 if (fdr->info[i - 1].status < 0)
2730 printf(" error %d", e);
2731 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2732 printf(" %llu bytes deduplicated",
2733 fdr->info[i - 1].bytes_deduped);
2734 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2735 printf(" differed");
2741 for (i = 0; i < nr; i++)
2745 for (i = 0; i < nr; i++)
2746 free_pathname(&fpath[i]);
2765 setxattr_f(int opno, long r)
2767 #ifdef XFS_XFLAG_EXTSIZE
2777 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2778 append_pathname(&f, ".");
2779 fd = open_path(&f, O_RDWR);
2780 e = fd < 0 ? errno : 0;
2785 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2786 nbits = (int)(random() % e);
2787 p &= (1 << nbits) - 1;
2789 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2791 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2794 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2801 splice_f(int opno, long r)
2803 struct pathname fpath1;
2804 struct pathname fpath2;
2805 struct stat64 stat1;
2806 struct stat64 stat2;
2807 char inoinfo1[1024];
2808 char inoinfo2[1024];
2812 loff_t offset1, offset2;
2819 ssize_t ret1 = 0, ret2 = 0;
2825 init_pathname(&fpath1);
2826 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2828 printf("%d/%d: splice read - no filename\n",
2833 init_pathname(&fpath2);
2834 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2836 printf("%d/%d: splice write - no filename\n",
2842 fd1 = open_path(&fpath1, O_RDONLY);
2843 e = fd1 < 0 ? errno : 0;
2847 printf("%d/%d: splice read - open %s failed %d\n",
2848 procid, opno, fpath1.path, e);
2852 fd2 = open_path(&fpath2, O_WRONLY);
2853 e = fd2 < 0 ? errno : 0;
2857 printf("%d/%d: splice write - open %s failed %d\n",
2858 procid, opno, fpath2.path, e);
2862 /* Get file stats */
2863 if (fstat64(fd1, &stat1) < 0) {
2865 printf("%d/%d: splice read - fstat64 %s failed %d\n",
2866 procid, opno, fpath1.path, errno);
2869 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2871 if (fstat64(fd2, &stat2) < 0) {
2873 printf("%d/%d: splice write - fstat64 %s failed %d\n",
2874 procid, opno, fpath2.path, errno);
2877 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2879 /* Calculate offsets */
2880 len = (random() % FILELEN_MAX) + 1;
2882 len = stat1.st_blksize;
2883 if (len > stat1.st_size)
2884 len = stat1.st_size;
2886 lr = ((int64_t)random() << 32) + random();
2887 if (stat1.st_size == len)
2890 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2894 * splice can overlap write, so the offset of the target file can be
2895 * any number. But to avoid too large offset, add a clamp of 1024 blocks
2896 * past the current dest file EOF
2898 lr = ((int64_t)random() << 32) + random();
2899 off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
2902 * Since len, off1 and off2 will be changed later, preserve their
2909 /* Pipe initialize */
2910 if (pipe(filedes) < 0) {
2912 printf("%d/%d: splice - pipe failed %d\n",
2913 procid, opno, errno);
2921 /* move to pipe buffer */
2922 ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
2928 /* move from pipe buffer to dst file */
2930 ret2 = splice(filedes[0], NULL, fd2, &off2, bytes, 0);
2943 if (ret1 < 0 || ret2 < 0)
2948 printf("%d/%d: splice %s%s [%lld,%lld] -> %s%s [%lld,%lld] %d",
2950 fpath1.path, inoinfo1, (long long)offset1, (long long)length,
2951 fpath2.path, inoinfo2, (long long)offset2, (long long)length, e);
2953 if (length && length > total)
2954 printf(" asked for %lld, spliced %lld??\n",
2955 (long long)length, (long long)total);
2966 free_pathname(&fpath2);
2968 free_pathname(&fpath1);
2972 creat_f(int opno, long r)
2987 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2992 e1 = (random() % 100);
2993 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2995 if (type == FT_RTF) /* rt always gets an extsize */
2996 extsize = (random() % 10) + 1;
2997 else if (e1 < 10) /* one-in-ten get an extsize */
2998 extsize = random() % 1024;
3002 e = generate_fname(fep, type, &f, &id, &v);
3006 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3007 printf("%d/%d: creat - no filename from %s\n",
3008 procid, opno, f.path);
3013 fd = creat_path(&f, 0666);
3014 e = fd < 0 ? errno : 0;
3019 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
3020 if (type == FT_RTF) {
3021 a.fsx_xflags |= XFS_XFLAG_REALTIME;
3022 a.fsx_extsize = extsize *
3023 geom.rtextsize * geom.blocksize;
3025 } else if (extsize) {
3026 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
3027 a.fsx_extsize = extsize * geom.blocksize;
3030 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
3033 add_to_flist(type, id, parid);
3037 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
3038 extsize ? a.fsx_extsize : 0, e, e1);
3039 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
3045 dread_f(int opno, long r)
3049 struct dioattr diob;
3062 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3064 printf("%d/%d: dread - no filename\n", procid, opno);
3068 fd = open_path(&f, O_RDONLY|O_DIRECT);
3069 e = fd < 0 ? errno : 0;
3073 printf("%d/%d: dread - open %s failed %d\n",
3074 procid, opno, f.path, e);
3078 if (fstat64(fd, &stb) < 0) {
3080 printf("%d/%d: dread - fstat64 %s failed %d\n",
3081 procid, opno, f.path, errno);
3086 inode_info(st, sizeof(st), &stb, v);
3087 if (stb.st_size == 0) {
3089 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
3095 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3098 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
3099 " fallback to stat()\n",
3100 procid, opno, f.path, st, errno);
3101 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3102 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3105 dio_env = getenv("XFS_DIO_MIN");
3107 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3109 align = (int64_t)diob.d_miniosz;
3110 lr = ((int64_t)random() << 32) + random();
3111 off = (off64_t)(lr % stb.st_size);
3112 off -= (off % align);
3113 lseek64(fd, off, SEEK_SET);
3114 len = (random() % FILELEN_MAX) + 1;
3115 len -= (len % align);
3118 else if (len > diob.d_maxiosz)
3119 len = diob.d_maxiosz;
3120 buf = memalign(diob.d_mem, len);
3121 e = read(fd, buf, len) < 0 ? errno : 0;
3124 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
3125 procid, opno, f.path, st, (long long)off, (int)len, e);
3131 dwrite_f(int opno, long r)
3135 struct dioattr diob;
3148 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3150 printf("%d/%d: dwrite - no filename\n", procid, opno);
3154 fd = open_path(&f, O_WRONLY|O_DIRECT);
3155 e = fd < 0 ? errno : 0;
3159 printf("%d/%d: dwrite - open %s failed %d\n",
3160 procid, opno, f.path, e);
3164 if (fstat64(fd, &stb) < 0) {
3166 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
3167 procid, opno, f.path, errno);
3172 inode_info(st, sizeof(st), &stb, v);
3173 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
3175 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
3176 " %s%s return %d, fallback to stat()\n",
3177 procid, opno, f.path, st, errno);
3178 diob.d_mem = diob.d_miniosz = stb.st_blksize;
3179 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
3182 dio_env = getenv("XFS_DIO_MIN");
3184 diob.d_mem = diob.d_miniosz = atoi(dio_env);
3186 align = (int64_t)diob.d_miniosz;
3187 lr = ((int64_t)random() << 32) + random();
3188 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3189 off -= (off % align);
3190 lseek64(fd, off, SEEK_SET);
3191 len = (random() % FILELEN_MAX) + 1;
3192 len -= (len % align);
3195 else if (len > diob.d_maxiosz)
3196 len = diob.d_maxiosz;
3197 buf = memalign(diob.d_mem, len);
3199 lseek64(fd, off, SEEK_SET);
3200 memset(buf, nameseq & 0xff, len);
3201 e = write(fd, buf, len) < 0 ? errno : 0;
3204 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
3205 procid, opno, f.path, st, (long long)off, (int)len, e);
3211 #ifdef HAVE_LINUX_FALLOC_H
3212 struct print_flags falloc_flags [] = {
3213 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
3214 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
3215 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
3216 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
3217 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
3218 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
3222 #define translate_falloc_flags(mode) \
3223 ({translate_flags(mode, "|", falloc_flags);})
3227 do_fallocate(int opno, long r, int mode)
3229 #ifdef HAVE_LINUX_FALLOC_H
3241 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3243 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
3247 fd = open_path(&f, O_RDWR);
3250 printf("%d/%d: do_fallocate - open %s failed %d\n",
3251 procid, opno, f.path, errno);
3256 if (fstat64(fd, &stb) < 0) {
3258 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
3259 procid, opno, f.path, errno);
3264 inode_info(st, sizeof(st), &stb, v);
3265 lr = ((int64_t)random() << 32) + random();
3266 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3268 len = (off64_t)(random() % (1024 * 1024));
3270 * Collapse/insert range requires off and len to be block aligned,
3271 * make it more likely to be the case.
3273 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
3275 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3276 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
3278 mode |= FALLOC_FL_KEEP_SIZE & random();
3279 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
3281 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
3282 procid, opno, translate_falloc_flags(mode),
3283 f.path, st, (long long)off, (long long)len, e);
3290 fallocate_f(int opno, long r)
3292 #ifdef HAVE_LINUX_FALLOC_H
3293 do_fallocate(opno, r, 0);
3298 fdatasync_f(int opno, long r)
3306 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3308 printf("%d/%d: fdatasync - no filename\n",
3313 fd = open_path(&f, O_WRONLY);
3314 e = fd < 0 ? errno : 0;
3318 printf("%d/%d: fdatasync - open %s failed %d\n",
3319 procid, opno, f.path, e);
3323 e = fdatasync(fd) < 0 ? errno : 0;
3325 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
3330 #ifdef HAVE_LINUX_FIEMAP_H
3331 struct print_flags fiemap_flags[] = {
3332 { FIEMAP_FLAG_SYNC, "SYNC"},
3333 { FIEMAP_FLAG_XATTR, "XATTR"},
3337 #define translate_fiemap_flags(mode) \
3338 ({translate_flags(mode, "|", fiemap_flags);})
3342 fiemap_f(int opno, long r)
3344 #ifdef HAVE_LINUX_FIEMAP_H
3354 struct fiemap *fiemap;
3357 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3359 printf("%d/%d: fiemap - no filename\n", procid, opno);
3363 fd = open_path(&f, O_RDWR);
3364 e = fd < 0 ? errno : 0;
3368 printf("%d/%d: fiemap - open %s failed %d\n",
3369 procid, opno, f.path, e);
3373 if (fstat64(fd, &stb) < 0) {
3375 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3376 procid, opno, f.path, errno);
3381 inode_info(st, sizeof(st), &stb, v);
3382 blocks_to_map = random() & 0xffff;
3383 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3384 (blocks_to_map * sizeof(struct fiemap_extent)));
3387 printf("%d/%d: malloc failed \n", procid, opno);
3392 lr = ((int64_t)random() << 32) + random();
3393 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3395 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3396 fiemap->fm_extent_count = blocks_to_map;
3397 fiemap->fm_mapped_extents = random() & 0xffff;
3398 fiemap->fm_start = off;
3399 fiemap->fm_length = ((int64_t)random() << 32) + random();
3401 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3403 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3404 procid, opno, f.path, st, (long long)fiemap->fm_start,
3405 (long long) fiemap->fm_length,
3406 translate_fiemap_flags(fiemap->fm_flags), e);
3414 freesp_f(int opno, long r)
3419 struct xfs_flock64 fl;
3427 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3429 printf("%d/%d: freesp - no filename\n", procid, opno);
3433 fd = open_path(&f, O_RDWR);
3434 e = fd < 0 ? errno : 0;
3438 printf("%d/%d: freesp - open %s failed %d\n",
3439 procid, opno, f.path, e);
3443 if (fstat64(fd, &stb) < 0) {
3445 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3446 procid, opno, f.path, errno);
3451 inode_info(st, sizeof(st), &stb, v);
3452 lr = ((int64_t)random() << 32) + random();
3453 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3455 fl.l_whence = SEEK_SET;
3458 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3460 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3461 procid, opno, f.path, st, (long long)off, e);
3467 fsync_f(int opno, long r)
3475 if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
3477 printf("%d/%d: fsync - no filename\n", procid, opno);
3481 fd = open_file_or_dir(&f, O_WRONLY);
3482 e = fd < 0 ? errno : 0;
3486 printf("%d/%d: fsync - open %s failed %d\n",
3487 procid, opno, f.path, e);
3491 e = fsync(fd) < 0 ? errno : 0;
3493 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3499 getattr_f(int opno, long r)
3508 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3509 append_pathname(&f, ".");
3510 fd = open_path(&f, O_RDWR);
3511 e = fd < 0 ? errno : 0;
3514 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3516 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3522 getdents_f(int opno, long r)
3529 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3530 append_pathname(&f, ".");
3531 dir = opendir_path(&f);
3535 printf("%d/%d: getdents - can't open %s\n",
3536 procid, opno, f.path);
3540 while (readdir64(dir) != NULL)
3543 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3549 link_f(int opno, long r)
3562 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3564 printf("%d/%d: link - no file\n", procid, opno);
3568 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3574 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3578 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3579 printf("%d/%d: link - no filename from %s\n",
3580 procid, opno, l.path);
3586 e = link_path(&f, &l) < 0 ? errno : 0;
3589 add_to_flist(flp - flist, id, parid);
3591 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3593 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3600 mkdir_f(int opno, long r)
3610 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3615 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3619 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3620 printf("%d/%d: mkdir - no filename from %s\n",
3621 procid, opno, f.path);
3626 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3629 add_to_flist(FT_DIR, id, parid);
3631 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3632 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3638 mknod_f(int opno, long r)
3648 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3653 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3657 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3658 printf("%d/%d: mknod - no filename from %s\n",
3659 procid, opno, f.path);
3664 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3667 add_to_flist(FT_DEV, id, parid);
3669 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3670 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3675 #ifdef HAVE_SYS_MMAN_H
3676 struct print_flags mmap_flags[] = {
3677 { MAP_SHARED, "SHARED"},
3678 { MAP_PRIVATE, "PRIVATE"},
3682 #define translate_mmap_flags(flags) \
3683 ({translate_flags(flags, "|", mmap_flags);})
3687 do_mmap(int opno, long r, int prot)
3689 #ifdef HAVE_SYS_MMAN_H
3701 sigjmp_buf sigbus_jmpbuf;
3704 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3706 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3710 fd = open_path(&f, O_RDWR);
3711 e = fd < 0 ? errno : 0;
3715 printf("%d/%d: do_mmap - open %s failed %d\n",
3716 procid, opno, f.path, e);
3720 if (fstat64(fd, &stb) < 0) {
3722 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3723 procid, opno, f.path, errno);
3728 inode_info(st, sizeof(st), &stb, v);
3729 if (stb.st_size == 0) {
3731 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3738 lr = ((int64_t)random() << 32) + random();
3739 off = (off64_t)(lr % stb.st_size);
3740 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3741 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3743 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3744 addr = mmap(NULL, len, prot, flags, fd, off);
3745 e = (addr == MAP_FAILED) ? errno : 0;
3748 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3749 procid, opno, f.path, st, (long long)off,
3750 (int)len, translate_mmap_flags(flags), e);
3756 if (prot & PROT_WRITE) {
3757 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3758 sigbus_jmp = &sigbus_jmpbuf;
3759 memset(addr, nameseq & 0xff, len);
3763 if ((buf = malloc(len)) != NULL) {
3764 memcpy(buf, addr, len);
3769 /* set NULL to stop other functions from doing siglongjmp */
3773 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3774 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3775 f.path, st, (long long)off, (int)len,
3776 translate_mmap_flags(flags),
3777 (e == 0) ? "0" : "Bus error");
3785 mread_f(int opno, long r)
3787 #ifdef HAVE_SYS_MMAN_H
3788 do_mmap(opno, r, PROT_READ);
3793 mwrite_f(int opno, long r)
3795 #ifdef HAVE_SYS_MMAN_H
3796 do_mmap(opno, r, PROT_WRITE);
3801 punch_f(int opno, long r)
3803 #ifdef HAVE_LINUX_FALLOC_H
3804 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3809 zero_f(int opno, long r)
3811 #ifdef HAVE_LINUX_FALLOC_H
3812 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3817 collapse_f(int opno, long r)
3819 #ifdef HAVE_LINUX_FALLOC_H
3820 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3825 insert_f(int opno, long r)
3827 #ifdef HAVE_LINUX_FALLOC_H
3828 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3833 read_f(int opno, long r)
3847 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3849 printf("%d/%d: read - no filename\n", procid, opno);
3853 fd = open_path(&f, O_RDONLY);
3854 e = fd < 0 ? errno : 0;
3858 printf("%d/%d: read - open %s failed %d\n",
3859 procid, opno, f.path, e);
3863 if (fstat64(fd, &stb) < 0) {
3865 printf("%d/%d: read - fstat64 %s failed %d\n",
3866 procid, opno, f.path, errno);
3871 inode_info(st, sizeof(st), &stb, v);
3872 if (stb.st_size == 0) {
3874 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3880 lr = ((int64_t)random() << 32) + random();
3881 off = (off64_t)(lr % stb.st_size);
3882 lseek64(fd, off, SEEK_SET);
3883 len = (random() % FILELEN_MAX) + 1;
3885 e = read(fd, buf, len) < 0 ? errno : 0;
3888 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3889 procid, opno, f.path, st, (long long)off, (int)len, e);
3895 readlink_f(int opno, long r)
3903 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3905 printf("%d/%d: readlink - no filename\n", procid, opno);
3909 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3912 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3917 readv_f(int opno, long r)
3929 struct iovec *iov = NULL;
3936 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3938 printf("%d/%d: readv - no filename\n", procid, opno);
3942 fd = open_path(&f, O_RDONLY);
3943 e = fd < 0 ? errno : 0;
3947 printf("%d/%d: readv - open %s failed %d\n",
3948 procid, opno, f.path, e);
3952 if (fstat64(fd, &stb) < 0) {
3954 printf("%d/%d: readv - fstat64 %s failed %d\n",
3955 procid, opno, f.path, errno);
3960 inode_info(st, sizeof(st), &stb, v);
3961 if (stb.st_size == 0) {
3963 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3969 lr = ((int64_t)random() << 32) + random();
3970 off = (off64_t)(lr % stb.st_size);
3971 lseek64(fd, off, SEEK_SET);
3972 len = (random() % FILELEN_MAX) + 1;