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>
30 #ifndef FS_IOC_GETFLAGS
31 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
33 #ifndef FS_IOC_SETFLAGS
34 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
38 #define XFS_ERRTAG_MAX 17
39 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
40 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
45 #define FILELEN_MAX (32*4096)
97 typedef void (*opfnc_t)(int, long);
99 typedef struct opdesc {
107 typedef struct fent {
112 typedef struct flist {
119 typedef struct pathname {
129 struct print_string {
136 #define FT_DIRm (1 << FT_DIR)
138 #define FT_REGm (1 << FT_REG)
140 #define FT_SYMm (1 << FT_SYM)
142 #define FT_DEVm (1 << FT_DEV)
144 #define FT_RTFm (1 << FT_RTF)
146 #define FT_ANYm ((1 << FT_nft) - 1)
147 #define FT_REGFILE (FT_REGm | FT_RTFm)
148 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
150 #define FLIST_SLOT_INCR 16
153 #define MAXFSIZE ((1ULL << 63) - 1ULL)
154 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
156 void afsync_f(int, long);
157 void allocsp_f(int, long);
158 void aread_f(int, long);
159 void attr_remove_f(int, long);
160 void attr_set_f(int, long);
161 void awrite_f(int, long);
162 void bulkstat_f(int, long);
163 void bulkstat1_f(int, long);
164 void chown_f(int, long);
165 void clonerange_f(int, long);
166 void creat_f(int, long);
167 void deduperange_f(int, long);
168 void dread_f(int, long);
169 void dwrite_f(int, long);
170 void fallocate_f(int, long);
171 void fdatasync_f(int, long);
172 void fiemap_f(int, long);
173 void freesp_f(int, long);
174 void fsync_f(int, long);
175 void getattr_f(int, long);
176 void getdents_f(int, long);
177 void link_f(int, long);
178 void mkdir_f(int, long);
179 void mknod_f(int, long);
180 void mread_f(int, long);
181 void mwrite_f(int, long);
182 void punch_f(int, long);
183 void zero_f(int, long);
184 void collapse_f(int, long);
185 void insert_f(int, long);
186 void read_f(int, long);
187 void readlink_f(int, long);
188 void readv_f(int, long);
189 void rename_f(int, long);
190 void resvsp_f(int, long);
191 void rmdir_f(int, long);
192 void setattr_f(int, long);
193 void setxattr_f(int, long);
194 void stat_f(int, long);
195 void symlink_f(int, long);
196 void sync_f(int, long);
197 void truncate_f(int, long);
198 void unlink_f(int, long);
199 void unresvsp_f(int, long);
200 void write_f(int, long);
201 void writev_f(int, long);
204 /* { OP_ENUM, "name", function, freq, iswrite }, */
205 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
206 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
207 { OP_AREAD, "aread", aread_f, 1, 0 },
208 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
209 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
210 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
211 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
212 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
213 { OP_CHOWN, "chown", chown_f, 3, 1 },
214 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
215 { OP_CREAT, "creat", creat_f, 4, 1 },
216 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
217 { OP_DREAD, "dread", dread_f, 4, 0 },
218 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
219 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
220 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
221 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
222 { OP_FREESP, "freesp", freesp_f, 1, 1 },
223 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
224 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
225 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
226 { OP_LINK, "link", link_f, 1, 1 },
227 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
228 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
229 { OP_MREAD, "mread", mread_f, 2, 0 },
230 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
231 { OP_PUNCH, "punch", punch_f, 1, 1 },
232 { OP_ZERO, "zero", zero_f, 1, 1 },
233 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
234 { OP_INSERT, "insert", insert_f, 1, 1 },
235 { OP_READ, "read", read_f, 1, 0 },
236 { OP_READLINK, "readlink", readlink_f, 1, 0 },
237 { OP_READV, "readv", readv_f, 1, 0 },
238 { OP_RENAME, "rename", rename_f, 2, 1 },
239 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
240 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
241 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
242 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
243 { OP_STAT, "stat", stat_f, 1, 0 },
244 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
245 { OP_SYNC, "sync", sync_f, 1, 1 },
246 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
247 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
248 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
249 { OP_WRITE, "write", write_f, 4, 1 },
250 { OP_WRITEV, "writev", writev_f, 4, 1 },
253 flist_t flist[FT_nft] = {
266 xfs_fsop_geom_t geom;
277 unsigned int idmodulo = XFS_IDMODULO_MAX;
278 unsigned int attr_mask = ~0;
281 unsigned long seed = 0;
285 int verifiable_log = 0;
286 sig_atomic_t should_stop = 0;
287 sigjmp_buf *sigbus_jmp = NULL;
288 char *execute_cmd = NULL;
289 int execute_freq = 1;
290 struct print_string flag_str = {0};
292 void add_to_flist(int, int, int);
293 void append_pathname(pathname_t *, char *);
294 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
295 int attr_remove_path(pathname_t *, const char *, int);
296 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
297 void check_cwd(void);
298 void cleanup_flist(void);
299 int creat_path(pathname_t *, mode_t);
300 void dcache_enter(int, int);
301 void dcache_init(void);
302 fent_t *dcache_lookup(int);
303 void dcache_purge(int);
304 void del_from_flist(int, int);
305 int dirid_to_name(char *, int);
307 int fent_to_name(pathname_t *, flist_t *, fent_t *);
308 void fix_parent(int, int);
309 void free_pathname(pathname_t *);
310 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
311 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
312 void init_pathname(pathname_t *);
313 int lchown_path(pathname_t *, uid_t, gid_t);
314 int link_path(pathname_t *, pathname_t *);
315 int lstat64_path(pathname_t *, struct stat64 *);
316 void make_freq_table(void);
317 int mkdir_path(pathname_t *, mode_t);
318 int mknod_path(pathname_t *, mode_t, dev_t);
319 void namerandpad(int, char *, int);
320 int open_path(pathname_t *, int);
321 DIR *opendir_path(pathname_t *);
322 void process_freq(char *);
323 int readlink_path(pathname_t *, char *, size_t);
324 int rename_path(pathname_t *, pathname_t *);
325 int rmdir_path(pathname_t *);
326 void separate_pathname(pathname_t *, char *, pathname_t *);
327 void show_ops(int, char *);
328 int stat64_path(pathname_t *, struct stat64 *);
329 int symlink_path(const char *, pathname_t *);
330 int truncate64_path(pathname_t *, off64_t);
331 int unlink_path(pathname_t *);
333 void write_freq(void);
334 void zero_freq(void);
336 void sg_handler(int signum)
344 * Only handle SIGBUS when mmap write to a hole and no
345 * block can be allocated due to ENOSPC, abort otherwise.
348 siglongjmp(*sigbus_jmp, -1);
350 printf("Unknown SIGBUS is caught, Abort!\n");
353 /* should not reach here */
360 int main(int argc, char **argv)
364 char *dirname = NULL;
365 char *logname = NULL;
366 char rpath[PATH_MAX];
375 xfs_error_injection_t err_inj;
376 struct sigaction action;
378 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
380 errrange = errtag = 0;
382 nops = sizeof(ops) / sizeof(ops[0]);
383 ops_end = &ops[nops];
385 while ((c = getopt(argc, argv, allopts)) != -1) {
394 sscanf(optarg, "%d", &errtag);
398 } else if (errtag == 0)
400 if (errtag >= XFS_ERRTAG_MAX) {
402 "error tag %d too large (max %d)\n",
403 errtag, XFS_ERRTAG_MAX - 1);
408 process_freq(optarg);
411 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
412 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
415 idmodulo = strtoul(optarg, NULL, 0);
416 if (idmodulo > XFS_IDMODULO_MAX) {
418 "chown modulo %d too big (max %d)\n",
419 idmodulo, XFS_IDMODULO_MAX);
424 loops = atoi(optarg);
427 operations = atoi(optarg);
434 nproc = atoi(optarg);
440 seed = strtoul(optarg, NULL, 0);
449 execute_cmd = optarg;
455 attr_mask = strtoul(optarg, NULL, 0);
459 if (optarg[0] == 'c')
470 execute_freq = strtoul(optarg, NULL, 0);
473 fprintf(stderr, "%s - invalid parameters\n",
483 /* no directory specified */
484 if (!nousage) usage();
488 (void)mkdir(dirname, 0777);
489 if (logname && logname[0] != '/') {
490 if (!getcwd(rpath, sizeof(rpath))){
491 perror("getcwd failed");
497 if (chdir(dirname) < 0) {
502 char path[PATH_MAX + NAME_MAX + 1];
503 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
504 if (freopen(path, "a", stdout) == NULL) {
505 perror("freopen logfile failed");
509 sprintf(buf, "fss%x", (unsigned int)getpid());
510 fd = creat(buf, 0666);
511 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
512 maxfsize = (off64_t)MAXFSIZE32;
514 maxfsize = (off64_t)MAXFSIZE;
519 gettimeofday(&t, (void *)NULL);
520 seed = (int)t.tv_sec ^ (int)t.tv_usec;
521 printf("seed = %ld\n", seed);
523 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
524 if (i >= 0 && geom.rtblocks)
525 rtpct = MIN(MAX(geom.rtblocks * 100 /
526 (geom.rtblocks + geom.datablocks), 1), 99);
535 for (i = 0; i < j; i++)
538 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
544 for (i = 0; i < j; i++)
547 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
549 printf("Injecting failure on tag #%d\n", errtag);
550 err_inj.errtag = errtag;
552 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
554 perror("fsstress - XFS_SYSSGI error injection call");
563 action.sa_handler = sg_handler;
564 sigemptyset(&action.sa_mask);
566 if (sigaction(SIGTERM, &action, 0)) {
567 perror("sigaction failed");
571 for (i = 0; i < nproc; i++) {
573 sigemptyset(&action.sa_mask);
574 action.sa_handler = SIG_DFL;
575 if (sigaction(SIGTERM, &action, 0))
577 action.sa_handler = sg_handler;
578 if (sigaction(SIGBUS, &action, 0))
580 #ifdef HAVE_SYS_PRCTL_H
581 prctl(PR_SET_PDEATHSIG, SIGKILL);
582 if (getppid() == 1) /* parent died already? */
586 char path[PATH_MAX + NAME_MAX + 2 + 11];
587 snprintf(path, sizeof(path), "%s/%s.%d",
589 if (freopen(path, "a", stdout) == NULL) {
590 perror("freopen logfile failed");
596 if (io_setup(128, &io_ctx) != 0) {
597 fprintf(stderr, "io_setup failed");
601 for (i = 0; !loops || (i < loops); i++)
604 if(io_destroy(io_ctx) != 0) {
605 fprintf(stderr, "io_destroy failed");
615 while (wait(&stat) > 0 && !should_stop) {
618 action.sa_flags = SA_RESTART;
619 sigaction(SIGTERM, &action, 0);
620 kill(-getpid(), SIGTERM);
621 while (wait(&stat) > 0)
627 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
629 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
631 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
644 add_string(struct print_string *str, const char *add)
646 int len = strlen(add);
651 if (len > (str->max - 1) - str->len) {
652 str->len = str->max - 1;
656 memcpy(str->buffer + str->len, add, len);
658 str->buffer[str->len] = '\0';
664 translate_flags(int flags, const char *delim,
665 const struct print_flags *flag_array)
667 int i, mask, first = 1;
670 if (!flag_str.buffer) {
671 flag_str.buffer = malloc(4096);
675 if (!flag_str.buffer)
678 flag_str.buffer[0] = '\0';
680 for (i = 0; flag_array[i].name && flags; i++) {
681 mask = flag_array[i].mask;
682 if ((flags & mask) != mask)
685 add = flag_array[i].name;
688 add_string(&flag_str, delim);
691 add_string(&flag_str, add);
694 /* Check whether there are any leftover flags. */
700 add_string(&flag_str, delim);
702 ret = snprintf(number, 11, "0x%x", flags) > 0;
703 if (ret > 0 && ret <= 11)
704 add_string(&flag_str, number);
707 return flag_str.buffer;
711 add_to_flist(int ft, int id, int parent)
717 if (ftp->nfiles == ftp->nslots) {
718 ftp->nslots += FLIST_SLOT_INCR;
719 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
721 fep = &ftp->fents[ftp->nfiles++];
723 fep->parent = parent;
727 append_pathname(pathname_t *name, char *str)
733 /* attempting to append to a dir a zero length path */
734 if (len && *str == '/' && name->len == 0) {
735 fprintf(stderr, "fsstress: append_pathname failure\n");
741 name->path = realloc(name->path, name->len + 1 + len);
742 strcpy(&name->path[name->len], str);
747 attr_list_path(pathname_t *name,
749 const int buffersize,
751 attrlist_cursor_t *cursor)
753 char buf[NAME_MAX + 1];
757 if (flags != ATTR_DONTFOLLOW) {
762 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
763 if (rval >= 0 || errno != ENAMETOOLONG)
765 separate_pathname(name, buf, &newname);
766 if (chdir(buf) == 0) {
767 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
770 free_pathname(&newname);
775 attr_remove_path(pathname_t *name, const char *attrname, int flags)
777 char buf[NAME_MAX + 1];
781 rval = attr_remove(name->path, attrname, flags);
782 if (rval >= 0 || errno != ENAMETOOLONG)
784 separate_pathname(name, buf, &newname);
785 if (chdir(buf) == 0) {
786 rval = attr_remove_path(&newname, attrname, flags);
789 free_pathname(&newname);
794 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
795 const int valuelength, int flags)
797 char buf[NAME_MAX + 1];
801 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
802 if (rval >= 0 || errno != ENAMETOOLONG)
804 separate_pathname(name, buf, &newname);
805 if (chdir(buf) == 0) {
806 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
810 free_pathname(&newname);
818 struct stat64 statbuf;
820 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
823 fprintf(stderr, "fsstress: check_cwd failure\n");
830 * go thru flist and release all entries
832 * NOTE: this function shouldn't be called until the end of a process
840 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
849 creat_path(pathname_t *name, mode_t mode)
851 char buf[NAME_MAX + 1];
855 rval = creat(name->path, mode);
856 if (rval >= 0 || errno != ENAMETOOLONG)
858 separate_pathname(name, buf, &newname);
859 if (chdir(buf) == 0) {
860 rval = creat_path(&newname, mode);
863 free_pathname(&newname);
868 dcache_enter(int dirid, int slot)
870 dcache[dirid % NDCACHE] = slot;
878 for (i = 0; i < NDCACHE; i++)
883 dcache_lookup(int dirid)
888 i = dcache[dirid % NDCACHE];
889 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
895 dcache_purge(int dirid)
899 dcp = &dcache[dirid % NDCACHE];
900 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
905 * Delete the item from the list by
906 * moving last entry over the deleted one;
907 * unless deleted entry is the last one.
908 * Input: which file list array and which slot in array
911 del_from_flist(int ft, int slot)
917 dcache_purge(ftp->fents[slot].id);
918 if (slot != ftp->nfiles - 1) {
920 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
921 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
927 dirid_to_fent(int dirid)
933 if ((fep = dcache_lookup(dirid)))
935 flp = &flist[FT_DIR];
936 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
937 if (fep->id == dirid) {
938 dcache_enter(dirid, fep - flp->fents);
948 struct stat64 statbuf;
956 dividend = (operations + execute_freq) / (execute_freq + 1);
957 sprintf(buf, "p%x", procid);
958 (void)mkdir(buf, 0777);
959 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
963 top_ino = statbuf.st_ino;
964 homedir = getcwd(NULL, 0);
966 perror("getcwd failed");
973 for (opno = 0; opno < operations; opno++) {
974 if (execute_cmd && opno && opno % dividend == 0) {
976 printf("%d: execute command %s\n", opno,
978 rval = system(execute_cmd);
980 fprintf(stderr, "execute command failed with "
983 p = &ops[freq_table[random() % freq_table_size]];
984 p->func(opno, random());
986 * test for forced shutdown by stat'ing the test
987 * directory. If this stat returns EIO, assume
988 * the forced shutdown happened.
990 if (errtag != 0 && opno % 100 == 0) {
991 rval = stat64(".", &statbuf);
993 fprintf(stderr, "Detected EIO\n");
1002 sprintf(cmd, "rm -rf %s", buf);
1009 * build up a pathname going thru the file entry and all
1010 * its parent entries
1011 * Return 0 on error, 1 on success;
1014 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1016 char buf[NAME_MAX + 1];
1024 /* build up parent directory name */
1025 if (fep->parent != -1) {
1026 pfep = dirid_to_fent(fep->parent);
1029 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1030 procid, fep->id, fep->parent);
1035 e = fent_to_name(name, &flist[FT_DIR], pfep);
1038 append_pathname(name, "/");
1041 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1042 namerandpad(fep->id, buf, i);
1043 append_pathname(name, buf);
1048 fix_parent(int oldid, int newid)
1055 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1056 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1057 if (fep->parent == oldid)
1058 fep->parent = newid;
1064 free_pathname(pathname_t *name)
1074 * Generate a filename of type ft.
1075 * If we have a fep which should be a directory then use it
1076 * as the parent path for this new filename i.e. prepend with it.
1079 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1081 char buf[NAME_MAX + 1];
1090 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1091 namerandpad(id, buf, len);
1093 /* prepend fep parent dir-name to it */
1095 e = fent_to_name(name, &flist[FT_DIR], fep);
1098 append_pathname(name, "/");
1100 append_pathname(name, buf);
1104 for (j = 0; !*v && j < ilistlen; j++) {
1105 if (ilist[j] == id) {
1115 * Input: "which" to choose the file-types eg. non-directory
1116 * Input: "r" to choose which file
1117 * Output: file-list, file-entry, name for the chosen file.
1118 * Output: verbose if chosen file is on the ilist.
1121 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1124 int totalsum = 0; /* total number of matching files */
1125 int partialsum = 0; /* partial sum of matching files */
1131 int e = 1; /* success */
1134 * go thru flist and add up number of files for each
1135 * category that matches with <which>.
1137 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1138 if (which & (1 << i))
1139 totalsum += flp->nfiles;
1141 if (totalsum == 0) {
1151 * Now we have possible matches between 0..totalsum-1.
1152 * And we use r to help us choose which one we want,
1153 * which when bounded by totalsum becomes x.
1155 x = (int)(r % totalsum);
1156 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1157 if (which & (1 << i)) {
1158 if (x < partialsum + flp->nfiles) {
1160 /* found the matching file entry */
1161 fep = &flp->fents[x - partialsum];
1163 /* fill-in what we were asked for */
1165 e = fent_to_name(name, flp, fep);
1168 fprintf(stderr, "%d: failed to get path for entry:"
1169 " id=%d,parent=%d\n",
1170 procid, fep->id, fep->parent);
1179 /* turn on verbose if its an ilisted file */
1181 for (j = 0; !*v && j < ilistlen; j++) {
1182 if (ilist[j] == fep->id) {
1189 partialsum += flp->nfiles;
1193 fprintf(stderr, "fsstress: get_fname failure\n");
1200 init_pathname(pathname_t *name)
1207 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1209 char buf[NAME_MAX + 1];
1213 rval = lchown(name->path, owner, group);
1214 if (rval >= 0 || errno != ENAMETOOLONG)
1216 separate_pathname(name, buf, &newname);
1217 if (chdir(buf) == 0) {
1218 rval = lchown_path(&newname, owner, group);
1221 free_pathname(&newname);
1226 link_path(pathname_t *name1, pathname_t *name2)
1228 char buf1[NAME_MAX + 1];
1229 char buf2[NAME_MAX + 1];
1231 pathname_t newname1;
1232 pathname_t newname2;
1235 rval = link(name1->path, name2->path);
1236 if (rval >= 0 || errno != ENAMETOOLONG)
1238 separate_pathname(name1, buf1, &newname1);
1239 separate_pathname(name2, buf2, &newname2);
1240 if (strcmp(buf1, buf2) == 0) {
1241 if (chdir(buf1) == 0) {
1242 rval = link_path(&newname1, &newname2);
1246 if (strcmp(buf1, "..") == 0)
1248 else if (strcmp(buf2, "..") == 0)
1250 else if (strlen(buf1) == 0)
1252 else if (strlen(buf2) == 0)
1255 down1 = MAX(newname1.len, 3 + name2->len) <=
1256 MAX(3 + name1->len, newname2.len);
1258 free_pathname(&newname2);
1259 append_pathname(&newname2, "../");
1260 append_pathname(&newname2, name2->path);
1261 if (chdir(buf1) == 0) {
1262 rval = link_path(&newname1, &newname2);
1266 free_pathname(&newname1);
1267 append_pathname(&newname1, "../");
1268 append_pathname(&newname1, name1->path);
1269 if (chdir(buf2) == 0) {
1270 rval = link_path(&newname1, &newname2);
1275 free_pathname(&newname1);
1276 free_pathname(&newname2);
1281 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1283 char buf[NAME_MAX + 1];
1287 rval = lstat64(name->path, sbuf);
1288 if (rval >= 0 || errno != ENAMETOOLONG)
1290 separate_pathname(name, buf, &newname);
1291 if (chdir(buf) == 0) {
1292 rval = lstat64_path(&newname, sbuf);
1295 free_pathname(&newname);
1300 make_freq_table(void)
1306 for (p = ops, f = 0; p < ops_end; p++)
1308 freq_table = malloc(f * sizeof(*freq_table));
1309 freq_table_size = f;
1310 for (p = ops, i = 0; p < ops_end; p++) {
1311 for (f = 0; f < p->freq; f++, i++)
1312 freq_table[i] = p->op;
1317 mkdir_path(pathname_t *name, mode_t mode)
1319 char buf[NAME_MAX + 1];
1323 rval = mkdir(name->path, mode);
1324 if (rval >= 0 || errno != ENAMETOOLONG)
1326 separate_pathname(name, buf, &newname);
1327 if (chdir(buf) == 0) {
1328 rval = mkdir_path(&newname, mode);
1331 free_pathname(&newname);
1336 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1338 char buf[NAME_MAX + 1];
1342 rval = mknod(name->path, mode, dev);
1343 if (rval >= 0 || errno != ENAMETOOLONG)
1345 separate_pathname(name, buf, &newname);
1346 if (chdir(buf) == 0) {
1347 rval = mknod_path(&newname, mode, dev);
1350 free_pathname(&newname);
1355 namerandpad(int id, char *buf, int i)
1358 static int buckets[] =
1359 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1365 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1366 padmod = buckets[bucket] + 1 - i;
1369 padlen = (id ^ namerand) % padmod;
1371 memset(&buf[i], 'X', padlen);
1372 buf[i + padlen] = '\0';
1377 open_path(pathname_t *name, int oflag)
1379 char buf[NAME_MAX + 1];
1383 rval = open(name->path, oflag);
1384 if (rval >= 0 || errno != ENAMETOOLONG)
1386 separate_pathname(name, buf, &newname);
1387 if (chdir(buf) == 0) {
1388 rval = open_path(&newname, oflag);
1391 free_pathname(&newname);
1396 opendir_path(pathname_t *name)
1398 char buf[NAME_MAX + 1];
1402 rval = opendir(name->path);
1403 if (rval || errno != ENAMETOOLONG)
1405 separate_pathname(name, buf, &newname);
1406 if (chdir(buf) == 0) {
1407 rval = opendir_path(&newname);
1410 free_pathname(&newname);
1415 process_freq(char *arg)
1420 s = strchr(arg, '=');
1422 fprintf(stderr, "bad argument '%s'\n", arg);
1426 for (p = ops; p < ops_end; p++) {
1427 if (strcmp(arg, p->name) == 0) {
1432 fprintf(stderr, "can't find op type %s for -f\n", arg);
1437 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1439 char buf[NAME_MAX + 1];
1443 rval = readlink(name->path, lbuf, lbufsiz);
1444 if (rval >= 0 || errno != ENAMETOOLONG)
1446 separate_pathname(name, buf, &newname);
1447 if (chdir(buf) == 0) {
1448 rval = readlink_path(&newname, lbuf, lbufsiz);
1451 free_pathname(&newname);
1456 rename_path(pathname_t *name1, pathname_t *name2)
1458 char buf1[NAME_MAX + 1];
1459 char buf2[NAME_MAX + 1];
1461 pathname_t newname1;
1462 pathname_t newname2;
1465 rval = rename(name1->path, name2->path);
1466 if (rval >= 0 || errno != ENAMETOOLONG)
1468 separate_pathname(name1, buf1, &newname1);
1469 separate_pathname(name2, buf2, &newname2);
1470 if (strcmp(buf1, buf2) == 0) {
1471 if (chdir(buf1) == 0) {
1472 rval = rename_path(&newname1, &newname2);
1476 if (strcmp(buf1, "..") == 0)
1478 else if (strcmp(buf2, "..") == 0)
1480 else if (strlen(buf1) == 0)
1482 else if (strlen(buf2) == 0)
1485 down1 = MAX(newname1.len, 3 + name2->len) <=
1486 MAX(3 + name1->len, newname2.len);
1488 free_pathname(&newname2);
1489 append_pathname(&newname2, "../");
1490 append_pathname(&newname2, name2->path);
1491 if (chdir(buf1) == 0) {
1492 rval = rename_path(&newname1, &newname2);
1496 free_pathname(&newname1);
1497 append_pathname(&newname1, "../");
1498 append_pathname(&newname1, name1->path);
1499 if (chdir(buf2) == 0) {
1500 rval = rename_path(&newname1, &newname2);
1505 free_pathname(&newname1);
1506 free_pathname(&newname2);
1511 rmdir_path(pathname_t *name)
1513 char buf[NAME_MAX + 1];
1517 rval = rmdir(name->path);
1518 if (rval >= 0 || errno != ENAMETOOLONG)
1520 separate_pathname(name, buf, &newname);
1521 if (chdir(buf) == 0) {
1522 rval = rmdir_path(&newname);
1525 free_pathname(&newname);
1530 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1534 init_pathname(newname);
1535 slash = strchr(name->path, '/');
1536 if (slash == NULL) {
1541 strcpy(buf, name->path);
1543 append_pathname(newname, slash + 1);
1549 show_ops(int flag, char *lead_str)
1554 /* print in list form */
1557 for (p = ops; p < ops_end; p++) {
1558 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1559 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1560 x+=printf("%s ", p->name);
1563 } else if (flag == 0) {
1564 /* Table view style */
1566 for (f = 0, p = ops; p < ops_end; p++)
1572 for (p = ops; p < ops_end; p++) {
1573 if (flag != 0 || p->freq > 0) {
1574 if (lead_str != NULL)
1575 printf("%s", lead_str);
1576 printf("%20s %d/%d %s\n",
1577 p->name, p->freq, f,
1578 (p->iswrite == 0) ? " " : "write op");
1582 /* Command line style */
1583 if (lead_str != NULL)
1584 printf("%s", lead_str);
1585 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1587 for (p = ops; p < ops_end; p++)
1589 printf("-f %s=%d \\\n",p->name, p->freq);
1594 stat64_path(pathname_t *name, struct stat64 *sbuf)
1596 char buf[NAME_MAX + 1];
1600 rval = stat64(name->path, sbuf);
1601 if (rval >= 0 || errno != ENAMETOOLONG)
1603 separate_pathname(name, buf, &newname);
1604 if (chdir(buf) == 0) {
1605 rval = stat64_path(&newname, sbuf);
1608 free_pathname(&newname);
1613 symlink_path(const char *name1, pathname_t *name)
1615 char buf[NAME_MAX + 1];
1619 if (!strcmp(name1, name->path)) {
1620 printf("yikes! %s %s\n", name1, name->path);
1624 rval = symlink(name1, name->path);
1625 if (rval >= 0 || errno != ENAMETOOLONG)
1627 separate_pathname(name, buf, &newname);
1628 if (chdir(buf) == 0) {
1629 rval = symlink_path(name1, &newname);
1632 free_pathname(&newname);
1637 truncate64_path(pathname_t *name, off64_t length)
1639 char buf[NAME_MAX + 1];
1643 rval = truncate64(name->path, length);
1644 if (rval >= 0 || errno != ENAMETOOLONG)
1646 separate_pathname(name, buf, &newname);
1647 if (chdir(buf) == 0) {
1648 rval = truncate64_path(&newname, length);
1651 free_pathname(&newname);
1656 unlink_path(pathname_t *name)
1658 char buf[NAME_MAX + 1];
1662 rval = unlink(name->path);
1663 if (rval >= 0 || errno != ENAMETOOLONG)
1665 separate_pathname(name, buf, &newname);
1666 if (chdir(buf) == 0) {
1667 rval = unlink_path(&newname);
1670 free_pathname(&newname);
1677 printf("Usage: %s -H or\n", myprog);
1678 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1680 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1682 printf(" -c clean up the test directory after each run\n");
1683 printf(" -d dir specifies the base directory for operations\n");
1684 printf(" -e errtg specifies error injection stuff\n");
1685 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1686 printf(" the valid operation names are:\n");
1688 printf(" -i filenum get verbose output for this nth file object\n");
1689 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1690 printf(" *use 0 for infinite (default 1)\n");
1691 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1692 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1693 printf(" -o logfile specifies logfile name\n");
1694 printf(" -p nproc specifies the no. of processes (default 1)\n");
1695 printf(" -r specifies random name padding\n");
1696 printf(" -s seed specifies the seed for the random generator (default random)\n");
1697 printf(" -v specifies verbose mode\n");
1698 printf(" -w zeros frequencies of non-write operations\n");
1699 printf(" -x cmd execute command in the middle of operations\n");
1700 printf(" -z zeros frequencies of all operations\n");
1701 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1702 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1703 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1704 printf(" -H prints usage and exits\n");
1712 for (p = ops; p < ops_end; p++) {
1723 for (p = ops; p < ops_end; p++)
1727 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1730 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1731 verifiable_log ? -1: (long)s->st_ino,
1732 (long)s->st_nlink, s->st_uid, s->st_gid,
1733 (long long) s->st_blocks, (long long) s->st_size);
1737 afsync_f(int opno, long r)
1745 struct iocb *iocbs[] = { &iocb };
1746 struct io_event event;
1749 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1751 printf("%d/%d: afsync - no filename\n", procid, opno);
1755 fd = open_path(&f, O_WRONLY | O_DIRECT);
1756 e = fd < 0 ? errno : 0;
1760 printf("%d/%d: afsync - open %s failed %d\n",
1761 procid, opno, f.path, e);
1766 io_prep_fsync(&iocb, fd);
1767 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1769 printf("%d/%d: afsync - io_submit %s %d\n",
1770 procid, opno, f.path, e);
1775 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1777 printf("%d/%d: afsync - io_getevents failed %d\n",
1786 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1793 allocsp_f(int opno, long r)
1798 struct xfs_flock64 fl;
1806 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1808 printf("%d/%d: allocsp - no filename\n", procid, opno);
1812 fd = open_path(&f, O_RDWR);
1813 e = fd < 0 ? errno : 0;
1817 printf("%d/%d: allocsp - open %s failed %d\n",
1818 procid, opno, f.path, e);
1822 if (fstat64(fd, &stb) < 0) {
1824 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1825 procid, opno, f.path, errno);
1830 inode_info(st, sizeof(st), &stb, v);
1831 lr = ((int64_t)random() << 32) + random();
1832 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1834 fl.l_whence = SEEK_SET;
1837 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1839 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1840 procid, opno, f.path, st, (long long)off, e);
1848 do_aio_rw(int opno, long r, int flags)
1852 struct dioattr diob;
1864 struct io_event event;
1865 struct iocb *iocbs[] = { &iocb };
1866 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1869 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1871 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1875 fd = open_path(&f, flags|O_DIRECT);
1876 e = fd < 0 ? errno : 0;
1880 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1881 procid, opno, f.path, e);
1885 if (fstat64(fd, &stb) < 0) {
1887 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1888 procid, opno, f.path, errno);
1893 inode_info(st, sizeof(st), &stb, v);
1894 if (!iswrite && stb.st_size == 0) {
1896 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1902 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1905 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1906 " fallback to stat()\n",
1907 procid, opno, f.path, st, errno);
1908 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1909 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1911 dio_env = getenv("XFS_DIO_MIN");
1913 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1914 align = (int64_t)diob.d_miniosz;
1915 lr = ((int64_t)random() << 32) + random();
1916 len = (random() % FILELEN_MAX) + 1;
1917 len -= (len % align);
1920 else if (len > diob.d_maxiosz)
1921 len = diob.d_maxiosz;
1922 buf = memalign(diob.d_mem, len);
1925 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1926 off -= (off % align);
1928 memset(buf, nameseq & 0xff, len);
1929 io_prep_pwrite(&iocb, fd, buf, len, off);
1931 off = (off64_t)(lr % stb.st_size);
1932 off -= (off % align);
1933 io_prep_pread(&iocb, fd, buf, len, off);
1935 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1937 printf("%d/%d: %s - io_submit failed %d\n",
1938 procid, opno, iswrite ? "awrite" : "aread", e);
1943 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1945 printf("%d/%d: %s - io_getevents failed %d\n",
1946 procid, opno, iswrite ? "awrite" : "aread", e);
1952 e = event.res != len ? event.res2 : 0;
1955 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1956 procid, opno, iswrite ? "awrite" : "aread",
1957 f.path, st, (long long)off, (int)len, e);
1964 aread_f(int opno, long r)
1967 do_aio_rw(opno, r, O_RDONLY);
1972 attr_remove_f(int opno, long r)
1974 attrlist_ent_t *aep;
1978 attrlist_cursor_t cursor;
1987 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1988 append_pathname(&f, ".");
1990 bzero(&cursor, sizeof(cursor));
1992 bzero(buf, sizeof(buf));
1993 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1997 alist = (attrlist_t *)buf;
1998 total += alist->al_count;
1999 } while (alist->al_more);
2002 printf("%d/%d: attr_remove - no attrs for %s\n",
2003 procid, opno, f.path);
2007 which = (int)(random() % total);
2008 bzero(&cursor, sizeof(cursor));
2012 bzero(buf, sizeof(buf));
2013 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2017 alist = (attrlist_t *)buf;
2018 if (which < ent + alist->al_count) {
2019 aep = (attrlist_ent_t *)
2020 &buf[alist->al_offset[which - ent]];
2021 aname = aep->a_name;
2024 ent += alist->al_count;
2025 } while (alist->al_more);
2026 if (aname == NULL) {
2029 "%d/%d: attr_remove - name %d not found at %s\n",
2030 procid, opno, which, f.path);
2034 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2037 printf("%d/%d: attr_remove %s %s %d\n",
2038 procid, opno, f.path, aname, e);
2043 attr_set_f(int opno, long r)
2050 static int lengths[] = { 10, 100, 1000, 10000 };
2055 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2056 append_pathname(&f, ".");
2057 sprintf(aname, "a%x", nameseq++);
2058 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2059 len = (int)(random() % lengths[li]);
2063 memset(aval, nameseq & 0xff, len);
2064 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2069 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2075 awrite_f(int opno, long r)
2078 do_aio_rw(opno, r, O_WRONLY);
2083 bulkstat_f(int opno, long r)
2091 xfs_fsop_bulkreq_t bsr;
2094 nent = (r % 999) + 2;
2095 t = malloc(nent * sizeof(*t));
2096 fd = open(".", O_RDONLY);
2104 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2108 printf("%d/%d: bulkstat nent %d total %lld\n",
2109 procid, opno, nent, (long long)total);
2114 bulkstat1_f(int opno, long r)
2124 xfs_fsop_bulkreq_t bsr;
2127 good = random() & 1;
2129 /* use an inode we know exists */
2131 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2132 append_pathname(&f, ".");
2133 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2138 * pick a random inode
2140 * note this can generate kernel warning messages
2141 * since bulkstat_one will read the disk block that
2142 * would contain a given inode even if that disk
2143 * block doesn't contain inodes.
2145 * this is detected later, but not until after the
2146 * warning is displayed.
2148 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2154 fd = open(".", O_RDONLY);
2160 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2162 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2163 procid, opno, good?"real":"random",
2164 verifiable_log ? -1LL : (long long)ino, e);
2169 chown_f(int opno, long r)
2179 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2180 append_pathname(&f, ".");
2181 u = (uid_t)random();
2182 g = (gid_t)random();
2183 nbits = (int)(random() % idmodulo);
2184 u &= (1 << nbits) - 1;
2185 g &= (1 << nbits) - 1;
2186 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2189 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2193 /* reflink some arbitrary range of f1 to f2. */
2200 struct file_clone_range fcr;
2201 struct pathname fpath1;
2202 struct pathname fpath2;
2203 struct stat64 stat1;
2204 struct stat64 stat2;
2205 char inoinfo1[1024];
2206 char inoinfo2[1024];
2220 init_pathname(&fpath1);
2221 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2223 printf("%d/%d: clonerange read - no filename\n",
2228 init_pathname(&fpath2);
2229 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2231 printf("%d/%d: clonerange write - no filename\n",
2237 fd1 = open_path(&fpath1, O_RDONLY);
2238 e = fd1 < 0 ? errno : 0;
2242 printf("%d/%d: clonerange read - open %s failed %d\n",
2243 procid, opno, fpath1.path, e);
2247 fd2 = open_path(&fpath2, O_WRONLY);
2248 e = fd2 < 0 ? errno : 0;
2252 printf("%d/%d: clonerange write - open %s failed %d\n",
2253 procid, opno, fpath2.path, e);
2257 /* Get file stats */
2258 if (fstat64(fd1, &stat1) < 0) {
2260 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2261 procid, opno, fpath1.path, errno);
2264 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2266 if (fstat64(fd2, &stat2) < 0) {
2268 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2269 procid, opno, fpath2.path, errno);
2272 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2274 /* Calculate offsets */
2275 len = (random() % FILELEN_MAX) + 1;
2276 len &= ~(stat1.st_blksize - 1);
2278 len = stat1.st_blksize;
2279 if (len > stat1.st_size)
2280 len = stat1.st_size;
2282 lr = ((int64_t)random() << 32) + random();
2283 if (stat1.st_size == len)
2286 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2288 off1 &= ~(stat1.st_blksize - 1);
2291 * If srcfile == destfile, randomly generate destination ranges
2292 * until we find one that doesn't overlap the source range.
2294 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2296 lr = ((int64_t)random() << 32) + random();
2297 off2 = (off64_t)(lr % max_off2);
2299 off2 &= ~(stat2.st_blksize - 1);
2300 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2302 /* Clone data blocks */
2304 fcr.src_offset = off1;
2305 fcr.src_length = len;
2306 fcr.dest_offset = off2;
2308 ret = ioctl(fd2, FICLONERANGE, &fcr);
2309 e = ret < 0 ? errno : 0;
2311 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2313 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2314 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2317 printf(" error %d", e);
2326 free_pathname(&fpath2);
2328 free_pathname(&fpath1);
2332 /* dedupe some arbitrary range of f1 to f2...fn. */
2338 #ifdef FIDEDUPERANGE
2339 #define INFO_SZ 1024
2340 struct file_dedupe_range *fdr;
2341 struct pathname *fpath;
2342 struct stat64 *stat;
2354 if (flist[FT_REG].nfiles < 2)
2357 /* Pick somewhere between 2 and 128 files. */
2359 nr = random() % (flist[FT_REG].nfiles + 1);
2360 } while (nr < 2 || nr > 128);
2363 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2364 sizeof(struct file_dedupe_range));
2366 printf("%d/%d: line %d error %d\n",
2367 procid, opno, __LINE__, errno);
2370 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2371 sizeof(struct file_dedupe_range)));
2373 fpath = calloc(nr, sizeof(struct pathname));
2375 printf("%d/%d: line %d error %d\n",
2376 procid, opno, __LINE__, errno);
2380 stat = calloc(nr, sizeof(struct stat64));
2382 printf("%d/%d: line %d error %d\n",
2383 procid, opno, __LINE__, errno);
2387 info = calloc(nr, INFO_SZ);
2389 printf("%d/%d: line %d error %d\n",
2390 procid, opno, __LINE__, errno);
2394 off = calloc(nr, sizeof(off64_t));
2396 printf("%d/%d: line %d error %d\n",
2397 procid, opno, __LINE__, errno);
2401 v = calloc(nr, sizeof(int));
2403 printf("%d/%d: line %d error %d\n",
2404 procid, opno, __LINE__, errno);
2407 fd = calloc(nr, sizeof(int));
2409 printf("%d/%d: line %d error %d\n",
2410 procid, opno, __LINE__, errno);
2413 memset(fd, 0xFF, nr * sizeof(int));
2415 /* Get paths for all files */
2416 for (i = 0; i < nr; i++)
2417 init_pathname(&fpath[i]);
2419 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2421 printf("%d/%d: deduperange read - no filename\n",
2426 for (i = 1; i < nr; i++) {
2427 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2429 printf("%d/%d: deduperange write - no filename\n",
2436 fd[0] = open_path(&fpath[0], O_RDONLY);
2437 e = fd[0] < 0 ? errno : 0;
2441 printf("%d/%d: deduperange read - open %s failed %d\n",
2442 procid, opno, fpath[0].path, e);
2446 for (i = 1; i < nr; i++) {
2447 fd[i] = open_path(&fpath[i], O_WRONLY);
2448 e = fd[i] < 0 ? errno : 0;
2452 printf("%d/%d: deduperange write - open %s failed %d\n",
2453 procid, opno, fpath[i].path, e);
2458 /* Get file stats */
2459 if (fstat64(fd[0], &stat[0]) < 0) {
2461 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2462 procid, opno, fpath[0].path, errno);
2466 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2468 for (i = 1; i < nr; i++) {
2469 if (fstat64(fd[i], &stat[i]) < 0) {
2471 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2472 procid, opno, fpath[i].path, errno);
2475 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2478 /* Never try to dedupe more than half of the src file. */
2479 len = (random() % FILELEN_MAX) + 1;
2480 len &= ~(stat[0].st_blksize - 1);
2482 len = stat[0].st_blksize / 2;
2483 if (len > stat[0].st_size / 2)
2484 len = stat[0].st_size / 2;
2486 /* Calculate offsets */
2487 lr = ((int64_t)random() << 32) + random();
2488 if (stat[0].st_size == len)
2491 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2493 off[0] &= ~(stat[0].st_blksize - 1);
2496 * If srcfile == destfile[i], randomly generate destination ranges
2497 * until we find one that doesn't overlap the source range.
2499 for (i = 1; i < nr; i++) {
2503 lr = ((int64_t)random() << 32) + random();
2504 if (stat[i].st_size <= len)
2507 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2509 off[i] &= ~(stat[i].st_blksize - 1);
2510 } while (stat[0].st_ino == stat[i].st_ino &&
2511 llabs(off[i] - off[0]) < len &&
2515 /* Clone data blocks */
2516 fdr->src_offset = off[0];
2517 fdr->src_length = len;
2518 fdr->dest_count = nr - 1;
2519 for (i = 1; i < nr; i++) {
2520 fdr->info[i - 1].dest_fd = fd[i];
2521 fdr->info[i - 1].dest_offset = off[i];
2524 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2525 e = ret < 0 ? errno : 0;
2527 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2529 fpath[0].path, &info[0], (long long)off[0],
2532 printf(" error %d", e);
2538 for (i = 1; i < nr; i++) {
2539 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2541 printf("%d/%d: ...to %s%s [%lld,%lld]",
2543 fpath[i].path, &info[i * INFO_SZ],
2544 (long long)off[i], (long long)len);
2545 if (fdr->info[i - 1].status < 0)
2546 printf(" error %d", e);
2547 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2548 printf(" %llu bytes deduplicated",
2549 fdr->info[i - 1].bytes_deduped);
2550 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2551 printf(" differed");
2557 for (i = 0; i < nr; i++)
2561 for (i = 0; i < nr; i++)
2562 free_pathname(&fpath[i]);
2581 setxattr_f(int opno, long r)
2583 #ifdef XFS_XFLAG_EXTSIZE
2593 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2594 append_pathname(&f, ".");
2595 fd = open_path(&f, O_RDWR);
2596 e = fd < 0 ? errno : 0;
2601 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2602 nbits = (int)(random() % e);
2603 p &= (1 << nbits) - 1;
2605 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2607 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2610 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2617 creat_f(int opno, long r)
2632 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2637 e1 = (random() % 100);
2638 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2640 if (type == FT_RTF) /* rt always gets an extsize */
2641 extsize = (random() % 10) + 1;
2642 else if (e1 < 10) /* one-in-ten get an extsize */
2643 extsize = random() % 1024;
2647 e = generate_fname(fep, type, &f, &id, &v);
2651 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2652 printf("%d/%d: creat - no filename from %s\n",
2653 procid, opno, f.path);
2658 fd = creat_path(&f, 0666);
2659 e = fd < 0 ? errno : 0;
2664 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2665 if (type == FT_RTF) {
2666 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2667 a.fsx_extsize = extsize *
2668 geom.rtextsize * geom.blocksize;
2670 } else if (extsize) {
2671 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2672 a.fsx_extsize = extsize * geom.blocksize;
2675 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2678 add_to_flist(type, id, parid);
2682 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2683 extsize ? a.fsx_extsize : 0, e, e1);
2684 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2690 dread_f(int opno, long r)
2694 struct dioattr diob;
2707 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2709 printf("%d/%d: dread - no filename\n", procid, opno);
2713 fd = open_path(&f, O_RDONLY|O_DIRECT);
2714 e = fd < 0 ? errno : 0;
2718 printf("%d/%d: dread - open %s failed %d\n",
2719 procid, opno, f.path, e);
2723 if (fstat64(fd, &stb) < 0) {
2725 printf("%d/%d: dread - fstat64 %s failed %d\n",
2726 procid, opno, f.path, errno);
2731 inode_info(st, sizeof(st), &stb, v);
2732 if (stb.st_size == 0) {
2734 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2740 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2743 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2744 " fallback to stat()\n",
2745 procid, opno, f.path, st, errno);
2746 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2747 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2750 dio_env = getenv("XFS_DIO_MIN");
2752 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2754 align = (int64_t)diob.d_miniosz;
2755 lr = ((int64_t)random() << 32) + random();
2756 off = (off64_t)(lr % stb.st_size);
2757 off -= (off % align);
2758 lseek64(fd, off, SEEK_SET);
2759 len = (random() % FILELEN_MAX) + 1;
2760 len -= (len % align);
2763 else if (len > diob.d_maxiosz)
2764 len = diob.d_maxiosz;
2765 buf = memalign(diob.d_mem, len);
2766 e = read(fd, buf, len) < 0 ? errno : 0;
2769 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2770 procid, opno, f.path, st, (long long)off, (int)len, e);
2776 dwrite_f(int opno, long r)
2780 struct dioattr diob;
2793 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2795 printf("%d/%d: dwrite - no filename\n", procid, opno);
2799 fd = open_path(&f, O_WRONLY|O_DIRECT);
2800 e = fd < 0 ? errno : 0;
2804 printf("%d/%d: dwrite - open %s failed %d\n",
2805 procid, opno, f.path, e);
2809 if (fstat64(fd, &stb) < 0) {
2811 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2812 procid, opno, f.path, errno);
2817 inode_info(st, sizeof(st), &stb, v);
2818 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2820 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2821 " %s%s return %d, fallback to stat()\n",
2822 procid, opno, f.path, st, errno);
2823 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2824 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2827 dio_env = getenv("XFS_DIO_MIN");
2829 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2831 align = (int64_t)diob.d_miniosz;
2832 lr = ((int64_t)random() << 32) + random();
2833 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2834 off -= (off % align);
2835 lseek64(fd, off, SEEK_SET);
2836 len = (random() % FILELEN_MAX) + 1;
2837 len -= (len % align);
2840 else if (len > diob.d_maxiosz)
2841 len = diob.d_maxiosz;
2842 buf = memalign(diob.d_mem, len);
2844 lseek64(fd, off, SEEK_SET);
2845 memset(buf, nameseq & 0xff, len);
2846 e = write(fd, buf, len) < 0 ? errno : 0;
2849 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2850 procid, opno, f.path, st, (long long)off, (int)len, e);
2856 #ifdef HAVE_LINUX_FALLOC_H
2857 struct print_flags falloc_flags [] = {
2858 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2859 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2860 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2861 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2862 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2863 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2867 #define translate_falloc_flags(mode) \
2868 ({translate_flags(mode, "|", falloc_flags);})
2872 do_fallocate(int opno, long r, int mode)
2874 #ifdef HAVE_LINUX_FALLOC_H
2886 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2888 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2892 fd = open_path(&f, O_RDWR);
2895 printf("%d/%d: do_fallocate - open %s failed %d\n",
2896 procid, opno, f.path, errno);
2901 if (fstat64(fd, &stb) < 0) {
2903 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2904 procid, opno, f.path, errno);
2909 inode_info(st, sizeof(st), &stb, v);
2910 lr = ((int64_t)random() << 32) + random();
2911 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2913 len = (off64_t)(random() % (1024 * 1024));
2915 * Collapse/insert range requires off and len to be block aligned,
2916 * make it more likely to be the case.
2918 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2920 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2921 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2923 mode |= FALLOC_FL_KEEP_SIZE & random();
2924 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2926 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2927 procid, opno, translate_falloc_flags(mode),
2928 f.path, st, (long long)off, (long long)len, e);
2935 fallocate_f(int opno, long r)
2937 #ifdef HAVE_LINUX_FALLOC_H
2938 do_fallocate(opno, r, 0);
2943 fdatasync_f(int opno, long r)
2951 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2953 printf("%d/%d: fdatasync - no filename\n",
2958 fd = open_path(&f, O_WRONLY);
2959 e = fd < 0 ? errno : 0;
2963 printf("%d/%d: fdatasync - open %s failed %d\n",
2964 procid, opno, f.path, e);
2968 e = fdatasync(fd) < 0 ? errno : 0;
2970 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2975 #ifdef HAVE_LINUX_FIEMAP_H
2976 struct print_flags fiemap_flags[] = {
2977 { FIEMAP_FLAG_SYNC, "SYNC"},
2978 { FIEMAP_FLAG_XATTR, "XATTR"},
2982 #define translate_fiemap_flags(mode) \
2983 ({translate_flags(mode, "|", fiemap_flags);})
2987 fiemap_f(int opno, long r)
2989 #ifdef HAVE_LINUX_FIEMAP_H
2999 struct fiemap *fiemap;
3002 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3004 printf("%d/%d: fiemap - no filename\n", procid, opno);
3008 fd = open_path(&f, O_RDWR);
3009 e = fd < 0 ? errno : 0;
3013 printf("%d/%d: fiemap - open %s failed %d\n",
3014 procid, opno, f.path, e);
3018 if (fstat64(fd, &stb) < 0) {
3020 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3021 procid, opno, f.path, errno);
3026 inode_info(st, sizeof(st), &stb, v);
3027 blocks_to_map = random() & 0xffff;
3028 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3029 (blocks_to_map * sizeof(struct fiemap_extent)));
3032 printf("%d/%d: malloc failed \n", procid, opno);
3037 lr = ((int64_t)random() << 32) + random();
3038 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3040 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3041 fiemap->fm_extent_count = blocks_to_map;
3042 fiemap->fm_mapped_extents = random() & 0xffff;
3043 fiemap->fm_start = off;
3044 fiemap->fm_length = ((int64_t)random() << 32) + random();
3046 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3048 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3049 procid, opno, f.path, st, (long long)fiemap->fm_start,
3050 (long long) fiemap->fm_length,
3051 translate_fiemap_flags(fiemap->fm_flags), e);
3059 freesp_f(int opno, long r)
3064 struct xfs_flock64 fl;
3072 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3074 printf("%d/%d: freesp - no filename\n", procid, opno);
3078 fd = open_path(&f, O_RDWR);
3079 e = fd < 0 ? errno : 0;
3083 printf("%d/%d: freesp - open %s failed %d\n",
3084 procid, opno, f.path, e);
3088 if (fstat64(fd, &stb) < 0) {
3090 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3091 procid, opno, f.path, errno);
3096 inode_info(st, sizeof(st), &stb, v);
3097 lr = ((int64_t)random() << 32) + random();
3098 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3100 fl.l_whence = SEEK_SET;
3103 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3105 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3106 procid, opno, f.path, st, (long long)off, e);
3112 fsync_f(int opno, long r)
3120 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3122 printf("%d/%d: fsync - no filename\n", procid, opno);
3126 fd = open_path(&f, O_WRONLY);
3127 e = fd < 0 ? errno : 0;
3131 printf("%d/%d: fsync - open %s failed %d\n",
3132 procid, opno, f.path, e);
3136 e = fsync(fd) < 0 ? errno : 0;
3138 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3144 getattr_f(int opno, long r)
3153 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3154 append_pathname(&f, ".");
3155 fd = open_path(&f, O_RDWR);
3156 e = fd < 0 ? errno : 0;
3159 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3161 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3167 getdents_f(int opno, long r)
3174 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3175 append_pathname(&f, ".");
3176 dir = opendir_path(&f);
3180 printf("%d/%d: getdents - can't open %s\n",
3181 procid, opno, f.path);
3185 while (readdir64(dir) != NULL)
3188 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3194 link_f(int opno, long r)
3207 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3209 printf("%d/%d: link - no file\n", procid, opno);
3213 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3219 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3223 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3224 printf("%d/%d: link - no filename from %s\n",
3225 procid, opno, l.path);
3231 e = link_path(&f, &l) < 0 ? errno : 0;
3234 add_to_flist(flp - flist, id, parid);
3236 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3238 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3245 mkdir_f(int opno, long r)
3255 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3260 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3264 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3265 printf("%d/%d: mkdir - no filename from %s\n",
3266 procid, opno, f.path);
3271 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3274 add_to_flist(FT_DIR, id, parid);
3276 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3277 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3283 mknod_f(int opno, long r)
3293 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3298 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3302 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3303 printf("%d/%d: mknod - no filename from %s\n",
3304 procid, opno, f.path);
3309 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3312 add_to_flist(FT_DEV, id, parid);
3314 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3315 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3320 #ifdef HAVE_SYS_MMAN_H
3321 struct print_flags mmap_flags[] = {
3322 { MAP_SHARED, "SHARED"},
3323 { MAP_PRIVATE, "PRIVATE"},
3327 #define translate_mmap_flags(flags) \
3328 ({translate_flags(flags, "|", mmap_flags);})
3332 do_mmap(int opno, long r, int prot)
3334 #ifdef HAVE_SYS_MMAN_H
3346 sigjmp_buf sigbus_jmpbuf;
3349 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3351 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3355 fd = open_path(&f, O_RDWR);
3356 e = fd < 0 ? errno : 0;
3360 printf("%d/%d: do_mmap - open %s failed %d\n",
3361 procid, opno, f.path, e);
3365 if (fstat64(fd, &stb) < 0) {
3367 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3368 procid, opno, f.path, errno);
3373 inode_info(st, sizeof(st), &stb, v);
3374 if (stb.st_size == 0) {
3376 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3383 lr = ((int64_t)random() << 32) + random();
3384 off = (off64_t)(lr % stb.st_size);
3385 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3386 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3388 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3389 addr = mmap(NULL, len, prot, flags, fd, off);
3390 e = (addr == MAP_FAILED) ? errno : 0;
3393 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3394 procid, opno, f.path, st, (long long)off,
3395 (int)len, translate_mmap_flags(flags), e);
3401 if (prot & PROT_WRITE) {
3402 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3403 sigbus_jmp = &sigbus_jmpbuf;
3404 memset(addr, nameseq & 0xff, len);
3408 if ((buf = malloc(len)) != NULL) {
3409 memcpy(buf, addr, len);
3414 /* set NULL to stop other functions from doing siglongjmp */
3418 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3419 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3420 f.path, st, (long long)off, (int)len,
3421 translate_mmap_flags(flags),
3422 (e == 0) ? "0" : "Bus error");
3430 mread_f(int opno, long r)
3432 #ifdef HAVE_SYS_MMAN_H
3433 do_mmap(opno, r, PROT_READ);
3438 mwrite_f(int opno, long r)
3440 #ifdef HAVE_SYS_MMAN_H
3441 do_mmap(opno, r, PROT_WRITE);
3446 punch_f(int opno, long r)
3448 #ifdef HAVE_LINUX_FALLOC_H
3449 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3454 zero_f(int opno, long r)
3456 #ifdef HAVE_LINUX_FALLOC_H
3457 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3462 collapse_f(int opno, long r)
3464 #ifdef HAVE_LINUX_FALLOC_H
3465 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3470 insert_f(int opno, long r)
3472 #ifdef HAVE_LINUX_FALLOC_H
3473 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3478 read_f(int opno, long r)
3492 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3494 printf("%d/%d: read - no filename\n", procid, opno);
3498 fd = open_path(&f, O_RDONLY);
3499 e = fd < 0 ? errno : 0;
3503 printf("%d/%d: read - open %s failed %d\n",
3504 procid, opno, f.path, e);
3508 if (fstat64(fd, &stb) < 0) {
3510 printf("%d/%d: read - fstat64 %s failed %d\n",
3511 procid, opno, f.path, errno);
3516 inode_info(st, sizeof(st), &stb, v);
3517 if (stb.st_size == 0) {
3519 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3525 lr = ((int64_t)random() << 32) + random();
3526 off = (off64_t)(lr % stb.st_size);
3527 lseek64(fd, off, SEEK_SET);
3528 len = (random() % FILELEN_MAX) + 1;
3530 e = read(fd, buf, len) < 0 ? errno : 0;
3533 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3534 procid, opno, f.path, st, (long long)off, (int)len, e);
3540 readlink_f(int opno, long r)
3548 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3550 printf("%d/%d: readlink - no filename\n", procid, opno);
3554 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3557 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3562 readv_f(int opno, long r)
3574 struct iovec *iov = NULL;
3581 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3583 printf("%d/%d: readv - no filename\n", procid, opno);
3587 fd = open_path(&f, O_RDONLY);
3588 e = fd < 0 ? errno : 0;
3592 printf("%d/%d: readv - open %s failed %d\n",
3593 procid, opno, f.path, e);
3597 if (fstat64(fd, &stb) < 0) {
3599 printf("%d/%d: readv - fstat64 %s failed %d\n",
3600 procid, opno, f.path, errno);
3605 inode_info(st, sizeof(st), &stb, v);
3606 if (stb.st_size == 0) {
3608 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3614 lr = ((int64_t)random() << 32) + random();
3615 off = (off64_t)(lr % stb.st_size);
3616 lseek64(fd, off, SEEK_SET);
3617 len = (random() % FILELEN_MAX) + 1;
3620 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
3621 iov = calloc(iovcnt, sizeof(struct iovec));
3622 iovl = len / iovcnt;
3624 for (i=0; i<iovcnt; i++) {
3625 (iov + i)->iov_base = (buf + iovb);
3626 (iov + i)->iov_len = iovl;
3630 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
3633 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
3634 procid, opno, f.path, st, (long long)off, (int)iovl,
3641 rename_f(int opno, long r)
3655 /* get an existing path for the source of the rename */
3657 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
3659 printf("%d/%d: rename - no filename\n", procid, opno);
3664 /* get an existing directory for the destination parent directory name */
3665 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
3671 /* generate a new path using an existing parent directory in name */
3672 init_pathname(&newf);
3673 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
3677 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
3678 printf("%d/%d: rename - no filename from %s\n",
3679 procid, opno, f.path);
3681 free_pathname(&newf);
3685 e = rename_path(&f, &newf) < 0 ? errno : 0;
3688 if (flp - flist == FT_DIR) {
3690 fix_parent(oldid, id);
3692 del_from_flist(flp - flist, fep - flp->fents);
3693 add_to_flist(flp - flist, id, parid);
3696 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3699 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3700 procid, opno, fep->id, fep->parent);
3701 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3702 procid, opno, id, parid);
3705 free_pathname(&newf);
3710 resvsp_f(int opno, long r)
3715 struct xfs_flock64 fl;
3723 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3725 printf("%d/%d: resvsp - no filename\n", procid, opno);
3729 fd = open_path(&f, O_RDWR);
3730 e = fd < 0 ? errno : 0;
3734 printf("%d/%d: resvsp - open %s failed %d\n",
3735 procid, opno, f.path, e);
3739 if (fstat64(fd, &stb) < 0) {
3741 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3742 procid, opno, f.path, errno);
3747 inode_info(st, sizeof(st), &stb, v);
3748 lr = ((int64_t)random() << 32) + random();
3749 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3751 fl.l_whence = SEEK_SET;
3753 fl.l_len = (off64_t)(random() % (1024 * 1024));
3754 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3756 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3757 procid, opno, f.path, st,
3758 (long long)off, (long long)fl.l_len, e);
3764 rmdir_f(int opno, long r)
3772 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3774 printf("%d/%d: rmdir - no directory\n", procid, opno);
3778 e = rmdir_path(&f) < 0 ? errno : 0;
3781 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3783 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3785 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3786 procid, opno, fep->id, fep->parent);
3792 setattr_f(int opno, long r)
3801 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3802 append_pathname(&f, ".");
3803 fd = open_path(&f, O_RDWR);
3804 e = fd < 0 ? errno : 0;
3807 fl = attr_mask & (uint)random();
3808 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3810 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3816 stat_f(int opno, long r)
3824 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3826 printf("%d/%d: stat - no entries\n", procid, opno);
3830 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3833 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3838 symlink_f(int opno, long r)
3851 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3856 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3860 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3861 printf("%d/%d: symlink - no filename from %s\n",
3862 procid, opno, f.path);
3867 len = (int)(random() % PATH_MAX);
3868 val = malloc(len + 1);
3870 memset(val, 'x', len);
3872 for (i = 10; i < len - 1; i += 10)
3874 e = symlink_path(val, &f) < 0 ? errno : 0;
3877 add_to_flist(FT_SYM, id, parid);
3880 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3881 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3888 sync_f(int opno, long r)
3892 printf("%d/%d: sync\n", procid, opno);
3896 truncate_f(int opno, long r)
3907 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3909 printf("%d/%d: truncate - no filename\n", procid, opno);
3913 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3917 printf("%d/%d: truncate - stat64 %s failed %d\n",
3918 procid, opno, f.path, e);
3922 inode_info(st, sizeof(st), &stb, v);
3923 lr = ((int64_t)random() << 32) + random();
3924 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3926 e = truncate64_path(&f, off) < 0 ? errno : 0;
3929 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3930 st, (long long)off, e);
3935 unlink_f(int opno, long r)
3944 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3946 printf("%d/%d: unlink - no file\n", procid, opno);
3950 e = unlink_path(&f) < 0 ? errno : 0;
3953 del_from_flist(flp - flist, fep - flp->fents);
3955 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3957 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3958 procid, opno, fep->id, fep->parent);
3964 unresvsp_f(int opno, long r)
3969 struct xfs_flock64 fl;
3977 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3979 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3983 fd = open_path(&f, O_RDWR);
3984 e = fd < 0 ? errno : 0;
3988 printf("%d/%d: unresvsp - open %s failed %d\n",
3989 procid, opno, f.path, e);
3993 if (fstat64(fd, &stb) < 0) {
3995 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3996 procid, opno, f.path, errno);
4001 inode_info(st, sizeof(st), &stb, v);
4002 lr = ((int64_t)random() << 32) + random();
4003 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4005 fl.l_whence = SEEK_SET;
4007 fl.l_len = (off64_t)(random() % (1 << 20));
4008 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4010 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4011 procid, opno, f.path, st,
4012 (long long)off, (long long)fl.l_len, e);
4018 write_f(int opno, long r)
4032 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4034 printf("%d/%d: write - no filename\n", procid, opno);
4038 fd = open_path(&f, O_WRONLY);
4039 e = fd < 0 ? errno : 0;
4043 printf("%d/%d: write - open %s failed %d\n",
4044 procid, opno, f.path, e);
4048 if (fstat64(fd, &stb) < 0) {
4050 printf("%d/%d: write - fstat64 %s failed %d\n",
4051 procid, opno, f.path, errno);
4056 inode_info(st, sizeof(st), &stb, v);
4057 lr = ((int64_t)random() << 32) + random();
4058 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4060 lseek64(fd, off, SEEK_SET);
4061 len = (random() % FILELEN_MAX) + 1;
4063 memset(buf, nameseq & 0xff, len);
4064 e = write(fd, buf, len) < 0 ? errno : 0;
4067 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4068 procid, opno, f.path, st, (long long)off, (int)len, e);
4074 writev_f(int opno, long r)
4086 struct iovec *iov = NULL;
4093 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4095 printf("%d/%d: writev - no filename\n", procid, opno);
4099 fd = open_path(&f, O_WRONLY);
4100 e = fd < 0 ? errno : 0;
4104 printf("%d/%d: writev - open %s failed %d\n",
4105 procid, opno, f.path, e);
4109 if (fstat64(fd, &stb) < 0) {
4111 printf("%d/%d: writev - fstat64 %s failed %d\n",
4112 procid, opno, f.path, errno);
4117 inode_info(st, sizeof(st), &stb, v);
4118 lr = ((int64_t)random() << 32) + random();
4119 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4121 lseek64(fd, off, SEEK_SET);
4122 len = (random() % FILELEN_MAX) + 1;
4124 memset(buf, nameseq & 0xff, len);
4126 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4127 iov = calloc(iovcnt, sizeof(struct iovec));
4128 iovl = len / iovcnt;
4130 for (i=0; i<iovcnt; i++) {
4131 (iov + i)->iov_base = (buf + iovb);
4132 (iov + i)->iov_len = iovl;
4136 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4140 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4141 procid, opno, f.path, st, (long long)off, (int)iovl,