1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
13 #ifdef HAVE_ATTR_XATTR_H
14 #include <attr/xattr.h>
16 #ifdef HAVE_ATTR_ATTRIBUTES_H
17 #include <attr/attributes.h>
19 #ifdef HAVE_LINUX_FIEMAP_H
20 #include <linux/fiemap.h>
22 #ifndef HAVE_ATTR_LIST
23 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
33 #ifndef FS_IOC_GETFLAGS
34 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
36 #ifndef FS_IOC_SETFLAGS
37 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
41 #define XFS_ERRTAG_MAX 17
42 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
43 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
48 #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 creat_f(int, long);
170 void deduperange_f(int, long);
171 void dread_f(int, long);
172 void dwrite_f(int, long);
173 void fallocate_f(int, long);
174 void fdatasync_f(int, long);
175 void fiemap_f(int, long);
176 void freesp_f(int, long);
177 void fsync_f(int, long);
178 void getattr_f(int, long);
179 void getdents_f(int, long);
180 void link_f(int, long);
181 void mkdir_f(int, long);
182 void mknod_f(int, long);
183 void mread_f(int, long);
184 void mwrite_f(int, long);
185 void punch_f(int, long);
186 void zero_f(int, long);
187 void collapse_f(int, long);
188 void insert_f(int, long);
189 void read_f(int, long);
190 void readlink_f(int, long);
191 void readv_f(int, long);
192 void rename_f(int, long);
193 void resvsp_f(int, long);
194 void rmdir_f(int, long);
195 void setattr_f(int, long);
196 void setxattr_f(int, long);
197 void stat_f(int, long);
198 void symlink_f(int, long);
199 void sync_f(int, long);
200 void truncate_f(int, long);
201 void unlink_f(int, long);
202 void unresvsp_f(int, long);
203 void write_f(int, long);
204 void writev_f(int, long);
207 /* { OP_ENUM, "name", function, freq, iswrite }, */
208 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
209 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
210 { OP_AREAD, "aread", aread_f, 1, 0 },
211 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
212 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
213 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
214 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
215 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
216 { OP_CHOWN, "chown", chown_f, 3, 1 },
217 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
218 { OP_CREAT, "creat", creat_f, 4, 1 },
219 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
220 { OP_DREAD, "dread", dread_f, 4, 0 },
221 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
222 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
223 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
224 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
225 { OP_FREESP, "freesp", freesp_f, 1, 1 },
226 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
227 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
228 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
229 { OP_LINK, "link", link_f, 1, 1 },
230 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
231 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
232 { OP_MREAD, "mread", mread_f, 2, 0 },
233 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
234 { OP_PUNCH, "punch", punch_f, 1, 1 },
235 { OP_ZERO, "zero", zero_f, 1, 1 },
236 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
237 { OP_INSERT, "insert", insert_f, 1, 1 },
238 { OP_READ, "read", read_f, 1, 0 },
239 { OP_READLINK, "readlink", readlink_f, 1, 0 },
240 { OP_READV, "readv", readv_f, 1, 0 },
241 { OP_RENAME, "rename", rename_f, 2, 1 },
242 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
243 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
244 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
245 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
246 { OP_STAT, "stat", stat_f, 1, 0 },
247 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
248 { OP_SYNC, "sync", sync_f, 1, 1 },
249 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
250 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
251 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
252 { OP_WRITE, "write", write_f, 4, 1 },
253 { OP_WRITEV, "writev", writev_f, 4, 1 },
256 flist_t flist[FT_nft] = {
269 xfs_fsop_geom_t geom;
280 unsigned int idmodulo = XFS_IDMODULO_MAX;
281 unsigned int attr_mask = ~0;
284 unsigned long seed = 0;
288 int verifiable_log = 0;
289 sig_atomic_t should_stop = 0;
290 sigjmp_buf *sigbus_jmp = NULL;
291 char *execute_cmd = NULL;
292 int execute_freq = 1;
293 struct print_string flag_str = {0};
295 void add_to_flist(int, int, int);
296 void append_pathname(pathname_t *, char *);
297 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
298 int attr_remove_path(pathname_t *, const char *, int);
299 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
300 void check_cwd(void);
301 void cleanup_flist(void);
302 int creat_path(pathname_t *, mode_t);
303 void dcache_enter(int, int);
304 void dcache_init(void);
305 fent_t *dcache_lookup(int);
306 void dcache_purge(int);
307 void del_from_flist(int, int);
308 int dirid_to_name(char *, int);
310 int fent_to_name(pathname_t *, flist_t *, fent_t *);
311 void fix_parent(int, int);
312 void free_pathname(pathname_t *);
313 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
314 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
315 void init_pathname(pathname_t *);
316 int lchown_path(pathname_t *, uid_t, gid_t);
317 int link_path(pathname_t *, pathname_t *);
318 int lstat64_path(pathname_t *, struct stat64 *);
319 void make_freq_table(void);
320 int mkdir_path(pathname_t *, mode_t);
321 int mknod_path(pathname_t *, mode_t, dev_t);
322 void namerandpad(int, char *, int);
323 int open_path(pathname_t *, int);
324 DIR *opendir_path(pathname_t *);
325 void process_freq(char *);
326 int readlink_path(pathname_t *, char *, size_t);
327 int rename_path(pathname_t *, pathname_t *);
328 int rmdir_path(pathname_t *);
329 void separate_pathname(pathname_t *, char *, pathname_t *);
330 void show_ops(int, char *);
331 int stat64_path(pathname_t *, struct stat64 *);
332 int symlink_path(const char *, pathname_t *);
333 int truncate64_path(pathname_t *, off64_t);
334 int unlink_path(pathname_t *);
336 void write_freq(void);
337 void zero_freq(void);
339 void sg_handler(int signum)
347 * Only handle SIGBUS when mmap write to a hole and no
348 * block can be allocated due to ENOSPC, abort otherwise.
351 siglongjmp(*sigbus_jmp, -1);
353 printf("Unknown SIGBUS is caught, Abort!\n");
356 /* should not reach here */
363 int main(int argc, char **argv)
367 char *dirname = NULL;
368 char *logname = NULL;
369 char rpath[PATH_MAX];
378 xfs_error_injection_t err_inj;
379 struct sigaction action;
381 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
383 errrange = errtag = 0;
385 nops = sizeof(ops) / sizeof(ops[0]);
386 ops_end = &ops[nops];
388 while ((c = getopt(argc, argv, allopts)) != -1) {
397 sscanf(optarg, "%d", &errtag);
401 } else if (errtag == 0)
403 if (errtag >= XFS_ERRTAG_MAX) {
405 "error tag %d too large (max %d)\n",
406 errtag, XFS_ERRTAG_MAX - 1);
411 process_freq(optarg);
414 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
415 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
418 idmodulo = strtoul(optarg, NULL, 0);
419 if (idmodulo > XFS_IDMODULO_MAX) {
421 "chown modulo %d too big (max %d)\n",
422 idmodulo, XFS_IDMODULO_MAX);
427 loops = atoi(optarg);
430 operations = atoi(optarg);
437 nproc = atoi(optarg);
443 seed = strtoul(optarg, NULL, 0);
452 execute_cmd = optarg;
458 attr_mask = strtoul(optarg, NULL, 0);
462 if (optarg[0] == 'c')
473 execute_freq = strtoul(optarg, NULL, 0);
476 fprintf(stderr, "%s - invalid parameters\n",
486 /* no directory specified */
487 if (!nousage) usage();
491 (void)mkdir(dirname, 0777);
492 if (logname && logname[0] != '/') {
493 if (!getcwd(rpath, sizeof(rpath))){
494 perror("getcwd failed");
500 if (chdir(dirname) < 0) {
506 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
507 if (freopen(path, "a", stdout) == NULL) {
508 perror("freopen logfile failed");
512 sprintf(buf, "fss%x", (unsigned int)getpid());
513 fd = creat(buf, 0666);
514 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
515 maxfsize = (off64_t)MAXFSIZE32;
517 maxfsize = (off64_t)MAXFSIZE;
522 gettimeofday(&t, (void *)NULL);
523 seed = (int)t.tv_sec ^ (int)t.tv_usec;
524 printf("seed = %ld\n", seed);
526 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
527 if (i >= 0 && geom.rtblocks)
528 rtpct = MIN(MAX(geom.rtblocks * 100 /
529 (geom.rtblocks + geom.datablocks), 1), 99);
538 for (i = 0; i < j; i++)
541 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
547 for (i = 0; i < j; i++)
550 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
552 printf("Injecting failure on tag #%d\n", errtag);
553 err_inj.errtag = errtag;
555 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
557 perror("fsstress - XFS_SYSSGI error injection call");
566 action.sa_handler = sg_handler;
567 sigemptyset(&action.sa_mask);
569 if (sigaction(SIGTERM, &action, 0)) {
570 perror("sigaction failed");
574 for (i = 0; i < nproc; i++) {
576 sigemptyset(&action.sa_mask);
577 action.sa_handler = SIG_DFL;
578 if (sigaction(SIGTERM, &action, 0))
580 action.sa_handler = sg_handler;
581 if (sigaction(SIGBUS, &action, 0))
583 #ifdef HAVE_SYS_PRCTL_H
584 prctl(PR_SET_PDEATHSIG, SIGKILL);
585 if (getppid() == 1) /* parent died already? */
590 snprintf(path, sizeof(path), "%s/%s.%d",
592 if (freopen(path, "a", stdout) == NULL) {
593 perror("freopen logfile failed");
599 if (io_setup(128, &io_ctx) != 0) {
600 fprintf(stderr, "io_setup failed");
604 for (i = 0; !loops || (i < loops); i++)
607 if(io_destroy(io_ctx) != 0) {
608 fprintf(stderr, "io_destroy failed");
618 while (wait(&stat) > 0 && !should_stop) {
621 action.sa_flags = SA_RESTART;
622 sigaction(SIGTERM, &action, 0);
623 kill(-getpid(), SIGTERM);
624 while (wait(&stat) > 0)
630 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
632 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
634 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
647 add_string(struct print_string *str, const char *add)
649 int len = strlen(add);
654 if (len > (str->max - 1) - str->len) {
655 str->len = str->max - 1;
659 memcpy(str->buffer + str->len, add, len);
661 str->buffer[str->len] = '\0';
667 translate_flags(int flags, const char *delim,
668 const struct print_flags *flag_array)
670 int i, mask, first = 1;
673 if (!flag_str.buffer) {
674 flag_str.buffer = malloc(4096);
678 if (!flag_str.buffer)
681 flag_str.buffer[0] = '\0';
683 for (i = 0; flag_array[i].name && flags; i++) {
684 mask = flag_array[i].mask;
685 if ((flags & mask) != mask)
688 add = flag_array[i].name;
691 add_string(&flag_str, delim);
694 add_string(&flag_str, add);
697 /* Check whether there are any leftover flags. */
703 add_string(&flag_str, delim);
705 ret = snprintf(number, 11, "0x%x", flags) > 0;
706 if (ret > 0 && ret <= 11)
707 add_string(&flag_str, number);
710 return flag_str.buffer;
714 add_to_flist(int ft, int id, int parent)
720 if (ftp->nfiles == ftp->nslots) {
721 ftp->nslots += FLIST_SLOT_INCR;
722 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
724 fep = &ftp->fents[ftp->nfiles++];
726 fep->parent = parent;
730 append_pathname(pathname_t *name, char *str)
736 /* attempting to append to a dir a zero length path */
737 if (len && *str == '/' && name->len == 0) {
738 fprintf(stderr, "fsstress: append_pathname failure\n");
744 name->path = realloc(name->path, name->len + 1 + len);
745 strcpy(&name->path[name->len], str);
750 attr_list_path(pathname_t *name,
752 const int buffersize,
754 attrlist_cursor_t *cursor)
756 char buf[NAME_MAX + 1];
760 if (flags != ATTR_DONTFOLLOW) {
765 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
766 if (rval >= 0 || errno != ENAMETOOLONG)
768 separate_pathname(name, buf, &newname);
769 if (chdir(buf) == 0) {
770 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
773 free_pathname(&newname);
778 attr_remove_path(pathname_t *name, const char *attrname, int flags)
780 char buf[NAME_MAX + 1];
784 rval = attr_remove(name->path, attrname, flags);
785 if (rval >= 0 || errno != ENAMETOOLONG)
787 separate_pathname(name, buf, &newname);
788 if (chdir(buf) == 0) {
789 rval = attr_remove_path(&newname, attrname, flags);
792 free_pathname(&newname);
797 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
798 const int valuelength, int flags)
800 char buf[NAME_MAX + 1];
804 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
805 if (rval >= 0 || errno != ENAMETOOLONG)
807 separate_pathname(name, buf, &newname);
808 if (chdir(buf) == 0) {
809 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
813 free_pathname(&newname);
821 struct stat64 statbuf;
823 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
826 fprintf(stderr, "fsstress: check_cwd failure\n");
833 * go thru flist and release all entries
835 * NOTE: this function shouldn't be called until the end of a process
843 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
852 creat_path(pathname_t *name, mode_t mode)
854 char buf[NAME_MAX + 1];
858 rval = creat(name->path, mode);
859 if (rval >= 0 || errno != ENAMETOOLONG)
861 separate_pathname(name, buf, &newname);
862 if (chdir(buf) == 0) {
863 rval = creat_path(&newname, mode);
866 free_pathname(&newname);
871 dcache_enter(int dirid, int slot)
873 dcache[dirid % NDCACHE] = slot;
881 for (i = 0; i < NDCACHE; i++)
886 dcache_lookup(int dirid)
891 i = dcache[dirid % NDCACHE];
892 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
898 dcache_purge(int dirid)
902 dcp = &dcache[dirid % NDCACHE];
903 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
908 * Delete the item from the list by
909 * moving last entry over the deleted one;
910 * unless deleted entry is the last one.
911 * Input: which file list array and which slot in array
914 del_from_flist(int ft, int slot)
920 dcache_purge(ftp->fents[slot].id);
921 if (slot != ftp->nfiles - 1) {
923 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
924 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
930 dirid_to_fent(int dirid)
936 if ((fep = dcache_lookup(dirid)))
938 flp = &flist[FT_DIR];
939 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
940 if (fep->id == dirid) {
941 dcache_enter(dirid, fep - flp->fents);
951 struct stat64 statbuf;
959 dividend = (operations + execute_freq) / (execute_freq + 1);
960 sprintf(buf, "p%x", procid);
961 (void)mkdir(buf, 0777);
962 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
966 top_ino = statbuf.st_ino;
967 homedir = getcwd(NULL, 0);
969 perror("getcwd failed");
976 for (opno = 0; opno < operations; opno++) {
977 if (execute_cmd && opno && opno % dividend == 0) {
979 printf("%d: execute command %s\n", opno,
981 rval = system(execute_cmd);
983 fprintf(stderr, "execute command failed with "
986 p = &ops[freq_table[random() % freq_table_size]];
987 p->func(opno, random());
989 * test for forced shutdown by stat'ing the test
990 * directory. If this stat returns EIO, assume
991 * the forced shutdown happened.
993 if (errtag != 0 && opno % 100 == 0) {
994 rval = stat64(".", &statbuf);
996 fprintf(stderr, "Detected EIO\n");
1005 sprintf(cmd, "rm -rf %s", buf);
1012 * build up a pathname going thru the file entry and all
1013 * its parent entries
1014 * Return 0 on error, 1 on success;
1017 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1019 char buf[NAME_MAX + 1];
1027 /* build up parent directory name */
1028 if (fep->parent != -1) {
1029 pfep = dirid_to_fent(fep->parent);
1032 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1033 procid, fep->id, fep->parent);
1038 e = fent_to_name(name, &flist[FT_DIR], pfep);
1041 append_pathname(name, "/");
1044 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1045 namerandpad(fep->id, buf, i);
1046 append_pathname(name, buf);
1051 fix_parent(int oldid, int newid)
1058 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1059 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1060 if (fep->parent == oldid)
1061 fep->parent = newid;
1067 free_pathname(pathname_t *name)
1077 * Generate a filename of type ft.
1078 * If we have a fep which should be a directory then use it
1079 * as the parent path for this new filename i.e. prepend with it.
1082 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1084 char buf[NAME_MAX + 1];
1093 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1094 namerandpad(id, buf, len);
1096 /* prepend fep parent dir-name to it */
1098 e = fent_to_name(name, &flist[FT_DIR], fep);
1101 append_pathname(name, "/");
1103 append_pathname(name, buf);
1107 for (j = 0; !*v && j < ilistlen; j++) {
1108 if (ilist[j] == id) {
1118 * Input: "which" to choose the file-types eg. non-directory
1119 * Input: "r" to choose which file
1120 * Output: file-list, file-entry, name for the chosen file.
1121 * Output: verbose if chosen file is on the ilist.
1124 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1127 int totalsum = 0; /* total number of matching files */
1128 int partialsum = 0; /* partial sum of matching files */
1134 int e = 1; /* success */
1137 * go thru flist and add up number of files for each
1138 * category that matches with <which>.
1140 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1141 if (which & (1 << i))
1142 totalsum += flp->nfiles;
1144 if (totalsum == 0) {
1154 * Now we have possible matches between 0..totalsum-1.
1155 * And we use r to help us choose which one we want,
1156 * which when bounded by totalsum becomes x.
1158 x = (int)(r % totalsum);
1159 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1160 if (which & (1 << i)) {
1161 if (x < partialsum + flp->nfiles) {
1163 /* found the matching file entry */
1164 fep = &flp->fents[x - partialsum];
1166 /* fill-in what we were asked for */
1168 e = fent_to_name(name, flp, fep);
1171 fprintf(stderr, "%d: failed to get path for entry:"
1172 " id=%d,parent=%d\n",
1173 procid, fep->id, fep->parent);
1182 /* turn on verbose if its an ilisted file */
1184 for (j = 0; !*v && j < ilistlen; j++) {
1185 if (ilist[j] == fep->id) {
1192 partialsum += flp->nfiles;
1196 fprintf(stderr, "fsstress: get_fname failure\n");
1203 init_pathname(pathname_t *name)
1210 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1212 char buf[NAME_MAX + 1];
1216 rval = lchown(name->path, owner, group);
1217 if (rval >= 0 || errno != ENAMETOOLONG)
1219 separate_pathname(name, buf, &newname);
1220 if (chdir(buf) == 0) {
1221 rval = lchown_path(&newname, owner, group);
1224 free_pathname(&newname);
1229 link_path(pathname_t *name1, pathname_t *name2)
1231 char buf1[NAME_MAX + 1];
1232 char buf2[NAME_MAX + 1];
1234 pathname_t newname1;
1235 pathname_t newname2;
1238 rval = link(name1->path, name2->path);
1239 if (rval >= 0 || errno != ENAMETOOLONG)
1241 separate_pathname(name1, buf1, &newname1);
1242 separate_pathname(name2, buf2, &newname2);
1243 if (strcmp(buf1, buf2) == 0) {
1244 if (chdir(buf1) == 0) {
1245 rval = link_path(&newname1, &newname2);
1249 if (strcmp(buf1, "..") == 0)
1251 else if (strcmp(buf2, "..") == 0)
1253 else if (strlen(buf1) == 0)
1255 else if (strlen(buf2) == 0)
1258 down1 = MAX(newname1.len, 3 + name2->len) <=
1259 MAX(3 + name1->len, newname2.len);
1261 free_pathname(&newname2);
1262 append_pathname(&newname2, "../");
1263 append_pathname(&newname2, name2->path);
1264 if (chdir(buf1) == 0) {
1265 rval = link_path(&newname1, &newname2);
1269 free_pathname(&newname1);
1270 append_pathname(&newname1, "../");
1271 append_pathname(&newname1, name1->path);
1272 if (chdir(buf2) == 0) {
1273 rval = link_path(&newname1, &newname2);
1278 free_pathname(&newname1);
1279 free_pathname(&newname2);
1284 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1286 char buf[NAME_MAX + 1];
1290 rval = lstat64(name->path, sbuf);
1291 if (rval >= 0 || errno != ENAMETOOLONG)
1293 separate_pathname(name, buf, &newname);
1294 if (chdir(buf) == 0) {
1295 rval = lstat64_path(&newname, sbuf);
1298 free_pathname(&newname);
1303 make_freq_table(void)
1309 for (p = ops, f = 0; p < ops_end; p++)
1311 freq_table = malloc(f * sizeof(*freq_table));
1312 freq_table_size = f;
1313 for (p = ops, i = 0; p < ops_end; p++) {
1314 for (f = 0; f < p->freq; f++, i++)
1315 freq_table[i] = p->op;
1320 mkdir_path(pathname_t *name, mode_t mode)
1322 char buf[NAME_MAX + 1];
1326 rval = mkdir(name->path, mode);
1327 if (rval >= 0 || errno != ENAMETOOLONG)
1329 separate_pathname(name, buf, &newname);
1330 if (chdir(buf) == 0) {
1331 rval = mkdir_path(&newname, mode);
1334 free_pathname(&newname);
1339 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1341 char buf[NAME_MAX + 1];
1345 rval = mknod(name->path, mode, dev);
1346 if (rval >= 0 || errno != ENAMETOOLONG)
1348 separate_pathname(name, buf, &newname);
1349 if (chdir(buf) == 0) {
1350 rval = mknod_path(&newname, mode, dev);
1353 free_pathname(&newname);
1358 namerandpad(int id, char *buf, int i)
1361 static int buckets[] =
1362 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1368 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1369 padmod = buckets[bucket] + 1 - i;
1372 padlen = (id ^ namerand) % padmod;
1374 memset(&buf[i], 'X', padlen);
1375 buf[i + padlen] = '\0';
1380 open_path(pathname_t *name, int oflag)
1382 char buf[NAME_MAX + 1];
1386 rval = open(name->path, oflag);
1387 if (rval >= 0 || errno != ENAMETOOLONG)
1389 separate_pathname(name, buf, &newname);
1390 if (chdir(buf) == 0) {
1391 rval = open_path(&newname, oflag);
1394 free_pathname(&newname);
1399 opendir_path(pathname_t *name)
1401 char buf[NAME_MAX + 1];
1405 rval = opendir(name->path);
1406 if (rval || errno != ENAMETOOLONG)
1408 separate_pathname(name, buf, &newname);
1409 if (chdir(buf) == 0) {
1410 rval = opendir_path(&newname);
1413 free_pathname(&newname);
1418 process_freq(char *arg)
1423 s = strchr(arg, '=');
1425 fprintf(stderr, "bad argument '%s'\n", arg);
1429 for (p = ops; p < ops_end; p++) {
1430 if (strcmp(arg, p->name) == 0) {
1435 fprintf(stderr, "can't find op type %s for -f\n", arg);
1440 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1442 char buf[NAME_MAX + 1];
1446 rval = readlink(name->path, lbuf, lbufsiz);
1447 if (rval >= 0 || errno != ENAMETOOLONG)
1449 separate_pathname(name, buf, &newname);
1450 if (chdir(buf) == 0) {
1451 rval = readlink_path(&newname, lbuf, lbufsiz);
1454 free_pathname(&newname);
1459 rename_path(pathname_t *name1, pathname_t *name2)
1461 char buf1[NAME_MAX + 1];
1462 char buf2[NAME_MAX + 1];
1464 pathname_t newname1;
1465 pathname_t newname2;
1468 rval = rename(name1->path, name2->path);
1469 if (rval >= 0 || errno != ENAMETOOLONG)
1471 separate_pathname(name1, buf1, &newname1);
1472 separate_pathname(name2, buf2, &newname2);
1473 if (strcmp(buf1, buf2) == 0) {
1474 if (chdir(buf1) == 0) {
1475 rval = rename_path(&newname1, &newname2);
1479 if (strcmp(buf1, "..") == 0)
1481 else if (strcmp(buf2, "..") == 0)
1483 else if (strlen(buf1) == 0)
1485 else if (strlen(buf2) == 0)
1488 down1 = MAX(newname1.len, 3 + name2->len) <=
1489 MAX(3 + name1->len, newname2.len);
1491 free_pathname(&newname2);
1492 append_pathname(&newname2, "../");
1493 append_pathname(&newname2, name2->path);
1494 if (chdir(buf1) == 0) {
1495 rval = rename_path(&newname1, &newname2);
1499 free_pathname(&newname1);
1500 append_pathname(&newname1, "../");
1501 append_pathname(&newname1, name1->path);
1502 if (chdir(buf2) == 0) {
1503 rval = rename_path(&newname1, &newname2);
1508 free_pathname(&newname1);
1509 free_pathname(&newname2);
1514 rmdir_path(pathname_t *name)
1516 char buf[NAME_MAX + 1];
1520 rval = rmdir(name->path);
1521 if (rval >= 0 || errno != ENAMETOOLONG)
1523 separate_pathname(name, buf, &newname);
1524 if (chdir(buf) == 0) {
1525 rval = rmdir_path(&newname);
1528 free_pathname(&newname);
1533 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1537 init_pathname(newname);
1538 slash = strchr(name->path, '/');
1539 if (slash == NULL) {
1544 strcpy(buf, name->path);
1546 append_pathname(newname, slash + 1);
1552 show_ops(int flag, char *lead_str)
1557 /* print in list form */
1560 for (p = ops; p < ops_end; p++) {
1561 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1562 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1563 x+=printf("%s ", p->name);
1566 } else if (flag == 0) {
1567 /* Table view style */
1569 for (f = 0, p = ops; p < ops_end; p++)
1575 for (p = ops; p < ops_end; p++) {
1576 if (flag != 0 || p->freq > 0) {
1577 if (lead_str != NULL)
1578 printf("%s", lead_str);
1579 printf("%20s %d/%d %s\n",
1580 p->name, p->freq, f,
1581 (p->iswrite == 0) ? " " : "write op");
1585 /* Command line style */
1586 if (lead_str != NULL)
1587 printf("%s", lead_str);
1588 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1590 for (p = ops; p < ops_end; p++)
1592 printf("-f %s=%d \\\n",p->name, p->freq);
1597 stat64_path(pathname_t *name, struct stat64 *sbuf)
1599 char buf[NAME_MAX + 1];
1603 rval = stat64(name->path, sbuf);
1604 if (rval >= 0 || errno != ENAMETOOLONG)
1606 separate_pathname(name, buf, &newname);
1607 if (chdir(buf) == 0) {
1608 rval = stat64_path(&newname, sbuf);
1611 free_pathname(&newname);
1616 symlink_path(const char *name1, pathname_t *name)
1618 char buf[NAME_MAX + 1];
1622 if (!strcmp(name1, name->path)) {
1623 printf("yikes! %s %s\n", name1, name->path);
1627 rval = symlink(name1, name->path);
1628 if (rval >= 0 || errno != ENAMETOOLONG)
1630 separate_pathname(name, buf, &newname);
1631 if (chdir(buf) == 0) {
1632 rval = symlink_path(name1, &newname);
1635 free_pathname(&newname);
1640 truncate64_path(pathname_t *name, off64_t length)
1642 char buf[NAME_MAX + 1];
1646 rval = truncate64(name->path, length);
1647 if (rval >= 0 || errno != ENAMETOOLONG)
1649 separate_pathname(name, buf, &newname);
1650 if (chdir(buf) == 0) {
1651 rval = truncate64_path(&newname, length);
1654 free_pathname(&newname);
1659 unlink_path(pathname_t *name)
1661 char buf[NAME_MAX + 1];
1665 rval = unlink(name->path);
1666 if (rval >= 0 || errno != ENAMETOOLONG)
1668 separate_pathname(name, buf, &newname);
1669 if (chdir(buf) == 0) {
1670 rval = unlink_path(&newname);
1673 free_pathname(&newname);
1680 printf("Usage: %s -H or\n", myprog);
1681 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1683 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1685 printf(" -c clean up the test directory after each run\n");
1686 printf(" -d dir specifies the base directory for operations\n");
1687 printf(" -e errtg specifies error injection stuff\n");
1688 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1689 printf(" the valid operation names are:\n");
1691 printf(" -i filenum get verbose output for this nth file object\n");
1692 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1693 printf(" *use 0 for infinite (default 1)\n");
1694 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1695 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1696 printf(" -o logfile specifies logfile name\n");
1697 printf(" -p nproc specifies the no. of processes (default 1)\n");
1698 printf(" -r specifies random name padding\n");
1699 printf(" -s seed specifies the seed for the random generator (default random)\n");
1700 printf(" -v specifies verbose mode\n");
1701 printf(" -w zeros frequencies of non-write operations\n");
1702 printf(" -x cmd execute command in the middle of operations\n");
1703 printf(" -z zeros frequencies of all operations\n");
1704 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1705 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1706 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1707 printf(" -H prints usage and exits\n");
1715 for (p = ops; p < ops_end; p++) {
1726 for (p = ops; p < ops_end; p++)
1730 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1733 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1734 verifiable_log ? -1: (long)s->st_ino,
1735 (long)s->st_nlink, s->st_uid, s->st_gid,
1736 (long long) s->st_blocks, (long long) s->st_size);
1740 afsync_f(int opno, long r)
1748 struct iocb *iocbs[] = { &iocb };
1749 struct io_event event;
1752 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1754 printf("%d/%d: afsync - no filename\n", procid, opno);
1758 fd = open_path(&f, O_WRONLY | O_DIRECT);
1759 e = fd < 0 ? errno : 0;
1763 printf("%d/%d: afsync - open %s failed %d\n",
1764 procid, opno, f.path, e);
1769 io_prep_fsync(&iocb, fd);
1770 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1772 printf("%d/%d: afsync - io_submit %s %d\n",
1773 procid, opno, f.path, e);
1778 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1780 printf("%d/%d: afsync - io_getevents failed %d\n",
1789 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1796 allocsp_f(int opno, long r)
1801 struct xfs_flock64 fl;
1809 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1811 printf("%d/%d: allocsp - no filename\n", procid, opno);
1815 fd = open_path(&f, O_RDWR);
1816 e = fd < 0 ? errno : 0;
1820 printf("%d/%d: allocsp - open %s failed %d\n",
1821 procid, opno, f.path, e);
1825 if (fstat64(fd, &stb) < 0) {
1827 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1828 procid, opno, f.path, errno);
1833 inode_info(st, sizeof(st), &stb, v);
1834 lr = ((int64_t)random() << 32) + random();
1835 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1837 fl.l_whence = SEEK_SET;
1840 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1842 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1843 procid, opno, f.path, st, (long long)off, e);
1851 do_aio_rw(int opno, long r, int flags)
1855 struct dioattr diob;
1867 struct io_event event;
1868 struct iocb *iocbs[] = { &iocb };
1869 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1872 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1874 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1878 fd = open_path(&f, flags|O_DIRECT);
1879 e = fd < 0 ? errno : 0;
1883 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1884 procid, opno, f.path, e);
1888 if (fstat64(fd, &stb) < 0) {
1890 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1891 procid, opno, f.path, errno);
1896 inode_info(st, sizeof(st), &stb, v);
1897 if (!iswrite && stb.st_size == 0) {
1899 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1905 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1908 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1909 " fallback to stat()\n",
1910 procid, opno, f.path, st, errno);
1911 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1912 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1914 dio_env = getenv("XFS_DIO_MIN");
1916 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1917 align = (int64_t)diob.d_miniosz;
1918 lr = ((int64_t)random() << 32) + random();
1919 len = (random() % FILELEN_MAX) + 1;
1920 len -= (len % align);
1923 else if (len > diob.d_maxiosz)
1924 len = diob.d_maxiosz;
1925 buf = memalign(diob.d_mem, len);
1928 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1929 off -= (off % align);
1931 memset(buf, nameseq & 0xff, len);
1932 io_prep_pwrite(&iocb, fd, buf, len, off);
1934 off = (off64_t)(lr % stb.st_size);
1935 off -= (off % align);
1936 io_prep_pread(&iocb, fd, buf, len, off);
1938 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1940 printf("%d/%d: %s - io_submit failed %d\n",
1941 procid, opno, iswrite ? "awrite" : "aread", e);
1946 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1948 printf("%d/%d: %s - io_getevents failed %d\n",
1949 procid, opno, iswrite ? "awrite" : "aread", e);
1955 e = event.res != len ? event.res2 : 0;
1958 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1959 procid, opno, iswrite ? "awrite" : "aread",
1960 f.path, st, (long long)off, (int)len, e);
1967 aread_f(int opno, long r)
1970 do_aio_rw(opno, r, O_RDONLY);
1975 attr_remove_f(int opno, long r)
1977 attrlist_ent_t *aep;
1981 attrlist_cursor_t cursor;
1990 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1991 append_pathname(&f, ".");
1993 bzero(&cursor, sizeof(cursor));
1995 bzero(buf, sizeof(buf));
1996 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2000 alist = (attrlist_t *)buf;
2001 total += alist->al_count;
2002 } while (alist->al_more);
2005 printf("%d/%d: attr_remove - no attrs for %s\n",
2006 procid, opno, f.path);
2010 which = (int)(random() % total);
2011 bzero(&cursor, sizeof(cursor));
2015 bzero(buf, sizeof(buf));
2016 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2020 alist = (attrlist_t *)buf;
2021 if (which < ent + alist->al_count) {
2022 aep = (attrlist_ent_t *)
2023 &buf[alist->al_offset[which - ent]];
2024 aname = aep->a_name;
2027 ent += alist->al_count;
2028 } while (alist->al_more);
2029 if (aname == NULL) {
2032 "%d/%d: attr_remove - name %d not found at %s\n",
2033 procid, opno, which, f.path);
2037 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2040 printf("%d/%d: attr_remove %s %s %d\n",
2041 procid, opno, f.path, aname, e);
2046 attr_set_f(int opno, long r)
2053 static int lengths[] = { 10, 100, 1000, 10000 };
2058 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2059 append_pathname(&f, ".");
2060 sprintf(aname, "a%x", nameseq++);
2061 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2062 len = (int)(random() % lengths[li]);
2066 memset(aval, nameseq & 0xff, len);
2067 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2072 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2078 awrite_f(int opno, long r)
2081 do_aio_rw(opno, r, O_WRONLY);
2086 bulkstat_f(int opno, long r)
2094 xfs_fsop_bulkreq_t bsr;
2097 nent = (r % 999) + 2;
2098 t = malloc(nent * sizeof(*t));
2099 fd = open(".", O_RDONLY);
2107 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2111 printf("%d/%d: bulkstat nent %d total %lld\n",
2112 procid, opno, nent, (long long)total);
2117 bulkstat1_f(int opno, long r)
2127 xfs_fsop_bulkreq_t bsr;
2130 good = random() & 1;
2132 /* use an inode we know exists */
2134 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2135 append_pathname(&f, ".");
2136 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2141 * pick a random inode
2143 * note this can generate kernel warning messages
2144 * since bulkstat_one will read the disk block that
2145 * would contain a given inode even if that disk
2146 * block doesn't contain inodes.
2148 * this is detected later, but not until after the
2149 * warning is displayed.
2151 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2157 fd = open(".", O_RDONLY);
2163 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2165 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2166 procid, opno, good?"real":"random",
2167 verifiable_log ? -1LL : (long long)ino, e);
2172 chown_f(int opno, long r)
2182 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2183 append_pathname(&f, ".");
2184 u = (uid_t)random();
2185 g = (gid_t)random();
2186 nbits = (int)(random() % idmodulo);
2187 u &= (1 << nbits) - 1;
2188 g &= (1 << nbits) - 1;
2189 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2192 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2196 /* reflink some arbitrary range of f1 to f2. */
2203 struct file_clone_range fcr;
2204 struct pathname fpath1;
2205 struct pathname fpath2;
2206 struct stat64 stat1;
2207 struct stat64 stat2;
2208 char inoinfo1[1024];
2209 char inoinfo2[1024];
2223 init_pathname(&fpath1);
2224 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2226 printf("%d/%d: clonerange read - no filename\n",
2231 init_pathname(&fpath2);
2232 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2234 printf("%d/%d: clonerange write - no filename\n",
2240 fd1 = open_path(&fpath1, O_RDONLY);
2241 e = fd1 < 0 ? errno : 0;
2245 printf("%d/%d: clonerange read - open %s failed %d\n",
2246 procid, opno, fpath1.path, e);
2250 fd2 = open_path(&fpath2, O_WRONLY);
2251 e = fd2 < 0 ? errno : 0;
2255 printf("%d/%d: clonerange write - open %s failed %d\n",
2256 procid, opno, fpath2.path, e);
2260 /* Get file stats */
2261 if (fstat64(fd1, &stat1) < 0) {
2263 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2264 procid, opno, fpath1.path, errno);
2267 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2269 if (fstat64(fd2, &stat2) < 0) {
2271 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2272 procid, opno, fpath2.path, errno);
2275 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2277 /* Calculate offsets */
2278 len = (random() % FILELEN_MAX) + 1;
2279 len &= ~(stat1.st_blksize - 1);
2281 len = stat1.st_blksize;
2282 if (len > stat1.st_size)
2283 len = stat1.st_size;
2285 lr = ((int64_t)random() << 32) + random();
2286 if (stat1.st_size == len)
2289 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2291 off1 &= ~(stat1.st_blksize - 1);
2294 * If srcfile == destfile, randomly generate destination ranges
2295 * until we find one that doesn't overlap the source range.
2297 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2299 lr = ((int64_t)random() << 32) + random();
2300 off2 = (off64_t)(lr % max_off2);
2302 off2 &= ~(stat2.st_blksize - 1);
2303 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2305 /* Clone data blocks */
2307 fcr.src_offset = off1;
2308 fcr.src_length = len;
2309 fcr.dest_offset = off2;
2311 ret = ioctl(fd2, FICLONERANGE, &fcr);
2312 e = ret < 0 ? errno : 0;
2314 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2316 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2317 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2320 printf(" error %d", e);
2329 free_pathname(&fpath2);
2331 free_pathname(&fpath1);
2335 /* dedupe some arbitrary range of f1 to f2...fn. */
2341 #ifdef FIDEDUPERANGE
2342 #define INFO_SZ 1024
2343 struct file_dedupe_range *fdr;
2344 struct pathname *fpath;
2345 struct stat64 *stat;
2357 if (flist[FT_REG].nfiles < 2)
2360 /* Pick somewhere between 2 and 128 files. */
2362 nr = random() % (flist[FT_REG].nfiles + 1);
2363 } while (nr < 2 || nr > 128);
2366 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2367 sizeof(struct file_dedupe_range));
2369 printf("%d/%d: line %d error %d\n",
2370 procid, opno, __LINE__, errno);
2373 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2374 sizeof(struct file_dedupe_range)));
2376 fpath = calloc(nr, sizeof(struct pathname));
2378 printf("%d/%d: line %d error %d\n",
2379 procid, opno, __LINE__, errno);
2383 stat = calloc(nr, sizeof(struct stat64));
2385 printf("%d/%d: line %d error %d\n",
2386 procid, opno, __LINE__, errno);
2390 info = calloc(nr, INFO_SZ);
2392 printf("%d/%d: line %d error %d\n",
2393 procid, opno, __LINE__, errno);
2397 off = calloc(nr, sizeof(off64_t));
2399 printf("%d/%d: line %d error %d\n",
2400 procid, opno, __LINE__, errno);
2404 v = calloc(nr, sizeof(int));
2406 printf("%d/%d: line %d error %d\n",
2407 procid, opno, __LINE__, errno);
2410 fd = calloc(nr, sizeof(int));
2412 printf("%d/%d: line %d error %d\n",
2413 procid, opno, __LINE__, errno);
2416 memset(fd, 0xFF, nr * sizeof(int));
2418 /* Get paths for all files */
2419 for (i = 0; i < nr; i++)
2420 init_pathname(&fpath[i]);
2422 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2424 printf("%d/%d: deduperange read - no filename\n",
2429 for (i = 1; i < nr; i++) {
2430 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2432 printf("%d/%d: deduperange write - no filename\n",
2439 fd[0] = open_path(&fpath[0], O_RDONLY);
2440 e = fd[0] < 0 ? errno : 0;
2444 printf("%d/%d: deduperange read - open %s failed %d\n",
2445 procid, opno, fpath[0].path, e);
2449 for (i = 1; i < nr; i++) {
2450 fd[i] = open_path(&fpath[i], O_WRONLY);
2451 e = fd[i] < 0 ? errno : 0;
2455 printf("%d/%d: deduperange write - open %s failed %d\n",
2456 procid, opno, fpath[i].path, e);
2461 /* Get file stats */
2462 if (fstat64(fd[0], &stat[0]) < 0) {
2464 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2465 procid, opno, fpath[0].path, errno);
2469 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2471 for (i = 1; i < nr; i++) {
2472 if (fstat64(fd[i], &stat[i]) < 0) {
2474 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2475 procid, opno, fpath[i].path, errno);
2478 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2481 /* Never try to dedupe more than half of the src file. */
2482 len = (random() % FILELEN_MAX) + 1;
2483 len &= ~(stat[0].st_blksize - 1);
2485 len = stat[0].st_blksize / 2;
2486 if (len > stat[0].st_size / 2)
2487 len = stat[0].st_size / 2;
2489 /* Calculate offsets */
2490 lr = ((int64_t)random() << 32) + random();
2491 if (stat[0].st_size == len)
2494 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2496 off[0] &= ~(stat[0].st_blksize - 1);
2499 * If srcfile == destfile[i], randomly generate destination ranges
2500 * until we find one that doesn't overlap the source range.
2502 for (i = 1; i < nr; i++) {
2506 lr = ((int64_t)random() << 32) + random();
2507 if (stat[i].st_size <= len)
2510 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2512 off[i] &= ~(stat[i].st_blksize - 1);
2513 } while (stat[0].st_ino == stat[i].st_ino &&
2514 llabs(off[i] - off[0]) < len &&
2518 /* Clone data blocks */
2519 fdr->src_offset = off[0];
2520 fdr->src_length = len;
2521 fdr->dest_count = nr - 1;
2522 for (i = 1; i < nr; i++) {
2523 fdr->info[i - 1].dest_fd = fd[i];
2524 fdr->info[i - 1].dest_offset = off[i];
2527 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2528 e = ret < 0 ? errno : 0;
2530 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2532 fpath[0].path, &info[0], (long long)off[0],
2535 printf(" error %d", e);
2541 for (i = 1; i < nr; i++) {
2542 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2544 printf("%d/%d: ...to %s%s [%lld,%lld]",
2546 fpath[i].path, &info[i * INFO_SZ],
2547 (long long)off[i], (long long)len);
2548 if (fdr->info[i - 1].status < 0)
2549 printf(" error %d", e);
2550 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2551 printf(" %llu bytes deduplicated",
2552 fdr->info[i - 1].bytes_deduped);
2553 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2554 printf(" differed");
2560 for (i = 0; i < nr; i++)
2564 for (i = 0; i < nr; i++)
2565 free_pathname(&fpath[i]);
2584 setxattr_f(int opno, long r)
2586 #ifdef XFS_XFLAG_EXTSIZE
2596 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2597 append_pathname(&f, ".");
2598 fd = open_path(&f, O_RDWR);
2599 e = fd < 0 ? errno : 0;
2604 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2605 nbits = (int)(random() % e);
2606 p &= (1 << nbits) - 1;
2608 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2610 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2613 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2620 creat_f(int opno, long r)
2635 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2640 e1 = (random() % 100);
2641 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2643 if (type == FT_RTF) /* rt always gets an extsize */
2644 extsize = (random() % 10) + 1;
2645 else if (e1 < 10) /* one-in-ten get an extsize */
2646 extsize = random() % 1024;
2650 e = generate_fname(fep, type, &f, &id, &v);
2654 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2655 printf("%d/%d: creat - no filename from %s\n",
2656 procid, opno, f.path);
2661 fd = creat_path(&f, 0666);
2662 e = fd < 0 ? errno : 0;
2667 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2668 if (type == FT_RTF) {
2669 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2670 a.fsx_extsize = extsize *
2671 geom.rtextsize * geom.blocksize;
2673 } else if (extsize) {
2674 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2675 a.fsx_extsize = extsize * geom.blocksize;
2678 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2681 add_to_flist(type, id, parid);
2685 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2686 extsize ? a.fsx_extsize : 0, e, e1);
2687 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2693 dread_f(int opno, long r)
2697 struct dioattr diob;
2710 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2712 printf("%d/%d: dread - no filename\n", procid, opno);
2716 fd = open_path(&f, O_RDONLY|O_DIRECT);
2717 e = fd < 0 ? errno : 0;
2721 printf("%d/%d: dread - open %s failed %d\n",
2722 procid, opno, f.path, e);
2726 if (fstat64(fd, &stb) < 0) {
2728 printf("%d/%d: dread - fstat64 %s failed %d\n",
2729 procid, opno, f.path, errno);
2734 inode_info(st, sizeof(st), &stb, v);
2735 if (stb.st_size == 0) {
2737 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2743 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2746 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2747 " fallback to stat()\n",
2748 procid, opno, f.path, st, errno);
2749 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2750 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2753 dio_env = getenv("XFS_DIO_MIN");
2755 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2757 align = (int64_t)diob.d_miniosz;
2758 lr = ((int64_t)random() << 32) + random();
2759 off = (off64_t)(lr % stb.st_size);
2760 off -= (off % align);
2761 lseek64(fd, off, SEEK_SET);
2762 len = (random() % FILELEN_MAX) + 1;
2763 len -= (len % align);
2766 else if (len > diob.d_maxiosz)
2767 len = diob.d_maxiosz;
2768 buf = memalign(diob.d_mem, len);
2769 e = read(fd, buf, len) < 0 ? errno : 0;
2772 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2773 procid, opno, f.path, st, (long long)off, (int)len, e);
2779 dwrite_f(int opno, long r)
2783 struct dioattr diob;
2796 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2798 printf("%d/%d: dwrite - no filename\n", procid, opno);
2802 fd = open_path(&f, O_WRONLY|O_DIRECT);
2803 e = fd < 0 ? errno : 0;
2807 printf("%d/%d: dwrite - open %s failed %d\n",
2808 procid, opno, f.path, e);
2812 if (fstat64(fd, &stb) < 0) {
2814 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2815 procid, opno, f.path, errno);
2820 inode_info(st, sizeof(st), &stb, v);
2821 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2823 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2824 " %s%s return %d, fallback to stat()\n",
2825 procid, opno, f.path, st, errno);
2826 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2827 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2830 dio_env = getenv("XFS_DIO_MIN");
2832 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2834 align = (int64_t)diob.d_miniosz;
2835 lr = ((int64_t)random() << 32) + random();
2836 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2837 off -= (off % align);
2838 lseek64(fd, off, SEEK_SET);
2839 len = (random() % FILELEN_MAX) + 1;
2840 len -= (len % align);
2843 else if (len > diob.d_maxiosz)
2844 len = diob.d_maxiosz;
2845 buf = memalign(diob.d_mem, len);
2847 lseek64(fd, off, SEEK_SET);
2848 memset(buf, nameseq & 0xff, len);
2849 e = write(fd, buf, len) < 0 ? errno : 0;
2852 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2853 procid, opno, f.path, st, (long long)off, (int)len, e);
2859 #ifdef HAVE_LINUX_FALLOC_H
2860 struct print_flags falloc_flags [] = {
2861 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2862 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2863 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2864 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2865 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2866 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2870 #define translate_falloc_flags(mode) \
2871 ({translate_flags(mode, "|", falloc_flags);})
2875 do_fallocate(int opno, long r, int mode)
2877 #ifdef HAVE_LINUX_FALLOC_H
2889 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2891 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2895 fd = open_path(&f, O_RDWR);
2898 printf("%d/%d: do_fallocate - open %s failed %d\n",
2899 procid, opno, f.path, errno);
2904 if (fstat64(fd, &stb) < 0) {
2906 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2907 procid, opno, f.path, errno);
2912 inode_info(st, sizeof(st), &stb, v);
2913 lr = ((int64_t)random() << 32) + random();
2914 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2916 len = (off64_t)(random() % (1024 * 1024));
2918 * Collapse/insert range requires off and len to be block aligned,
2919 * make it more likely to be the case.
2921 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2923 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2924 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2926 mode |= FALLOC_FL_KEEP_SIZE & random();
2927 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2929 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2930 procid, opno, translate_falloc_flags(mode),
2931 f.path, st, (long long)off, (long long)len, e);
2938 fallocate_f(int opno, long r)
2940 #ifdef HAVE_LINUX_FALLOC_H
2941 do_fallocate(opno, r, 0);
2946 fdatasync_f(int opno, long r)
2954 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2956 printf("%d/%d: fdatasync - no filename\n",
2961 fd = open_path(&f, O_WRONLY);
2962 e = fd < 0 ? errno : 0;
2966 printf("%d/%d: fdatasync - open %s failed %d\n",
2967 procid, opno, f.path, e);
2971 e = fdatasync(fd) < 0 ? errno : 0;
2973 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2978 #ifdef HAVE_LINUX_FIEMAP_H
2979 struct print_flags fiemap_flags[] = {
2980 { FIEMAP_FLAG_SYNC, "SYNC"},
2981 { FIEMAP_FLAG_XATTR, "XATTR"},
2985 #define translate_fiemap_flags(mode) \
2986 ({translate_flags(mode, "|", fiemap_flags);})
2990 fiemap_f(int opno, long r)
2992 #ifdef HAVE_LINUX_FIEMAP_H
3002 struct fiemap *fiemap;
3005 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3007 printf("%d/%d: fiemap - no filename\n", procid, opno);
3011 fd = open_path(&f, O_RDWR);
3012 e = fd < 0 ? errno : 0;
3016 printf("%d/%d: fiemap - open %s failed %d\n",
3017 procid, opno, f.path, e);
3021 if (fstat64(fd, &stb) < 0) {
3023 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3024 procid, opno, f.path, errno);
3029 inode_info(st, sizeof(st), &stb, v);
3030 blocks_to_map = random() & 0xffff;
3031 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3032 (blocks_to_map * sizeof(struct fiemap_extent)));
3035 printf("%d/%d: malloc failed \n", procid, opno);
3040 lr = ((int64_t)random() << 32) + random();
3041 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3043 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3044 fiemap->fm_extent_count = blocks_to_map;
3045 fiemap->fm_mapped_extents = random() & 0xffff;
3046 fiemap->fm_start = off;
3047 fiemap->fm_length = ((int64_t)random() << 32) + random();
3049 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3051 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3052 procid, opno, f.path, st, (long long)fiemap->fm_start,
3053 (long long) fiemap->fm_length,
3054 translate_fiemap_flags(fiemap->fm_flags), e);
3062 freesp_f(int opno, long r)
3067 struct xfs_flock64 fl;
3075 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3077 printf("%d/%d: freesp - no filename\n", procid, opno);
3081 fd = open_path(&f, O_RDWR);
3082 e = fd < 0 ? errno : 0;
3086 printf("%d/%d: freesp - open %s failed %d\n",
3087 procid, opno, f.path, e);
3091 if (fstat64(fd, &stb) < 0) {
3093 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3094 procid, opno, f.path, errno);
3099 inode_info(st, sizeof(st), &stb, v);
3100 lr = ((int64_t)random() << 32) + random();
3101 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3103 fl.l_whence = SEEK_SET;
3106 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3108 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3109 procid, opno, f.path, st, (long long)off, e);
3115 fsync_f(int opno, long r)
3123 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3125 printf("%d/%d: fsync - no filename\n", procid, opno);
3129 fd = open_path(&f, O_WRONLY);
3130 e = fd < 0 ? errno : 0;
3134 printf("%d/%d: fsync - open %s failed %d\n",
3135 procid, opno, f.path, e);
3139 e = fsync(fd) < 0 ? errno : 0;
3141 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3147 getattr_f(int opno, long r)
3156 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3157 append_pathname(&f, ".");
3158 fd = open_path(&f, O_RDWR);
3159 e = fd < 0 ? errno : 0;
3162 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3164 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3170 getdents_f(int opno, long r)
3177 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3178 append_pathname(&f, ".");
3179 dir = opendir_path(&f);
3183 printf("%d/%d: getdents - can't open %s\n",
3184 procid, opno, f.path);
3188 while (readdir64(dir) != NULL)
3191 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3197 link_f(int opno, long r)
3210 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3212 printf("%d/%d: link - no file\n", procid, opno);
3216 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3222 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3226 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3227 printf("%d/%d: link - no filename from %s\n",
3228 procid, opno, l.path);
3234 e = link_path(&f, &l) < 0 ? errno : 0;
3237 add_to_flist(flp - flist, id, parid);
3239 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3241 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3248 mkdir_f(int opno, long r)
3258 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3263 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3267 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3268 printf("%d/%d: mkdir - no filename from %s\n",
3269 procid, opno, f.path);
3274 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3277 add_to_flist(FT_DIR, id, parid);
3279 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3280 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3286 mknod_f(int opno, long r)
3296 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3301 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3305 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3306 printf("%d/%d: mknod - no filename from %s\n",
3307 procid, opno, f.path);
3312 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3315 add_to_flist(FT_DEV, id, parid);
3317 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3318 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3323 #ifdef HAVE_SYS_MMAN_H
3324 struct print_flags mmap_flags[] = {
3325 { MAP_SHARED, "SHARED"},
3326 { MAP_PRIVATE, "PRIVATE"},
3330 #define translate_mmap_flags(flags) \
3331 ({translate_flags(flags, "|", mmap_flags);})
3335 do_mmap(int opno, long r, int prot)
3337 #ifdef HAVE_SYS_MMAN_H
3349 sigjmp_buf sigbus_jmpbuf;
3352 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3354 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3358 fd = open_path(&f, O_RDWR);
3359 e = fd < 0 ? errno : 0;
3363 printf("%d/%d: do_mmap - open %s failed %d\n",
3364 procid, opno, f.path, e);
3368 if (fstat64(fd, &stb) < 0) {
3370 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3371 procid, opno, f.path, errno);
3376 inode_info(st, sizeof(st), &stb, v);
3377 if (stb.st_size == 0) {
3379 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3386 lr = ((int64_t)random() << 32) + random();
3387 off = (off64_t)(lr % stb.st_size);
3388 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3389 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3391 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3392 addr = mmap(NULL, len, prot, flags, fd, off);
3393 e = (addr == MAP_FAILED) ? errno : 0;
3396 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3397 procid, opno, f.path, st, (long long)off,
3398 (int)len, translate_mmap_flags(flags), e);
3404 if (prot & PROT_WRITE) {
3405 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3406 sigbus_jmp = &sigbus_jmpbuf;
3407 memset(addr, nameseq & 0xff, len);
3411 if ((buf = malloc(len)) != NULL) {
3412 memcpy(buf, addr, len);
3417 /* set NULL to stop other functions from doing siglongjmp */
3421 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3422 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3423 f.path, st, (long long)off, (int)len,
3424 translate_mmap_flags(flags),
3425 (e == 0) ? "0" : "Bus error");
3433 mread_f(int opno, long r)
3435 #ifdef HAVE_SYS_MMAN_H
3436 do_mmap(opno, r, PROT_READ);
3441 mwrite_f(int opno, long r)
3443 #ifdef HAVE_SYS_MMAN_H
3444 do_mmap(opno, r, PROT_WRITE);
3449 punch_f(int opno, long r)
3451 #ifdef HAVE_LINUX_FALLOC_H
3452 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3457 zero_f(int opno, long r)
3459 #ifdef HAVE_LINUX_FALLOC_H
3460 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3465 collapse_f(int opno, long r)
3467 #ifdef HAVE_LINUX_FALLOC_H
3468 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3473 insert_f(int opno, long r)
3475 #ifdef HAVE_LINUX_FALLOC_H
3476 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3481 read_f(int opno, long r)
3495 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3497 printf("%d/%d: read - no filename\n", procid, opno);
3501 fd = open_path(&f, O_RDONLY);
3502 e = fd < 0 ? errno : 0;
3506 printf("%d/%d: read - open %s failed %d\n",
3507 procid, opno, f.path, e);
3511 if (fstat64(fd, &stb) < 0) {
3513 printf("%d/%d: read - fstat64 %s failed %d\n",
3514 procid, opno, f.path, errno);
3519 inode_info(st, sizeof(st), &stb, v);
3520 if (stb.st_size == 0) {
3522 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3528 lr = ((int64_t)random() << 32) + random();
3529 off = (off64_t)(lr % stb.st_size);
3530 lseek64(fd, off, SEEK_SET);
3531 len = (random() % FILELEN_MAX) + 1;
3533 e = read(fd, buf, len) < 0 ? errno : 0;
3536 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3537 procid, opno, f.path, st, (long long)off, (int)len, e);
3543 readlink_f(int opno, long r)
3551 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3553 printf("%d/%d: readlink - no filename\n", procid, opno);
3557 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3560 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3565 readv_f(int opno, long r)
3577 struct iovec *iov = NULL;
3584 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3586 printf("%d/%d: readv - no filename\n", procid, opno);
3590 fd = open_path(&f, O_RDONLY);
3591 e = fd < 0 ? errno : 0;
3595 printf("%d/%d: readv - open %s failed %d\n",
3596 procid, opno, f.path, e);
3600 if (fstat64(fd, &stb) < 0) {
3602 printf("%d/%d: readv - fstat64 %s failed %d\n",
3603 procid, opno, f.path, errno);
3608 inode_info(st, sizeof(st), &stb, v);
3609 if (stb.st_size == 0) {
3611 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3617 lr = ((int64_t)random() << 32) + random();
3618 off = (off64_t)(lr % stb.st_size);
3619 lseek64(fd, off, SEEK_SET);
3620 len = (random() % FILELEN_MAX) + 1;
3623 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
3624 iov = calloc(iovcnt, sizeof(struct iovec));
3625 iovl = len / iovcnt;
3627 for (i=0; i<iovcnt; i++) {
3628 (iov + i)->iov_base = (buf + iovb);
3629 (iov + i)->iov_len = iovl;
3633 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
3636 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
3637 procid, opno, f.path, st, (long long)off, (int)iovl,
3644 rename_f(int opno, long r)
3658 /* get an existing path for the source of the rename */
3660 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
3662 printf("%d/%d: rename - no filename\n", procid, opno);
3667 /* get an existing directory for the destination parent directory name */
3668 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
3674 /* generate a new path using an existing parent directory in name */
3675 init_pathname(&newf);
3676 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
3680 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
3681 printf("%d/%d: rename - no filename from %s\n",
3682 procid, opno, f.path);
3684 free_pathname(&newf);
3688 e = rename_path(&f, &newf) < 0 ? errno : 0;
3691 if (flp - flist == FT_DIR) {
3693 fix_parent(oldid, id);
3695 del_from_flist(flp - flist, fep - flp->fents);
3696 add_to_flist(flp - flist, id, parid);
3699 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3702 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3703 procid, opno, fep->id, fep->parent);
3704 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3705 procid, opno, id, parid);
3708 free_pathname(&newf);
3713 resvsp_f(int opno, long r)
3718 struct xfs_flock64 fl;
3726 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3728 printf("%d/%d: resvsp - no filename\n", procid, opno);
3732 fd = open_path(&f, O_RDWR);
3733 e = fd < 0 ? errno : 0;
3737 printf("%d/%d: resvsp - open %s failed %d\n",
3738 procid, opno, f.path, e);
3742 if (fstat64(fd, &stb) < 0) {
3744 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3745 procid, opno, f.path, errno);
3750 inode_info(st, sizeof(st), &stb, v);
3751 lr = ((int64_t)random() << 32) + random();
3752 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3754 fl.l_whence = SEEK_SET;
3756 fl.l_len = (off64_t)(random() % (1024 * 1024));
3757 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3759 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3760 procid, opno, f.path, st,
3761 (long long)off, (long long)fl.l_len, e);
3767 rmdir_f(int opno, long r)
3775 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3777 printf("%d/%d: rmdir - no directory\n", procid, opno);
3781 e = rmdir_path(&f) < 0 ? errno : 0;
3784 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3786 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3788 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3789 procid, opno, fep->id, fep->parent);
3795 setattr_f(int opno, long r)
3804 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3805 append_pathname(&f, ".");
3806 fd = open_path(&f, O_RDWR);
3807 e = fd < 0 ? errno : 0;
3810 fl = attr_mask & (uint)random();
3811 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3813 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3819 stat_f(int opno, long r)
3827 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3829 printf("%d/%d: stat - no entries\n", procid, opno);
3833 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3836 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3841 symlink_f(int opno, long r)
3854 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3859 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3863 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3864 printf("%d/%d: symlink - no filename from %s\n",
3865 procid, opno, f.path);
3870 len = (int)(random() % PATH_MAX);
3871 val = malloc(len + 1);
3873 memset(val, 'x', len);
3875 for (i = 10; i < len - 1; i += 10)
3877 e = symlink_path(val, &f) < 0 ? errno : 0;
3880 add_to_flist(FT_SYM, id, parid);
3883 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3884 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3891 sync_f(int opno, long r)
3895 printf("%d/%d: sync\n", procid, opno);
3899 truncate_f(int opno, long r)
3910 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3912 printf("%d/%d: truncate - no filename\n", procid, opno);
3916 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3920 printf("%d/%d: truncate - stat64 %s failed %d\n",
3921 procid, opno, f.path, e);
3925 inode_info(st, sizeof(st), &stb, v);
3926 lr = ((int64_t)random() << 32) + random();
3927 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3929 e = truncate64_path(&f, off) < 0 ? errno : 0;
3932 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3933 st, (long long)off, e);
3938 unlink_f(int opno, long r)
3947 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3949 printf("%d/%d: unlink - no file\n", procid, opno);
3953 e = unlink_path(&f) < 0 ? errno : 0;
3956 del_from_flist(flp - flist, fep - flp->fents);
3958 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3960 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3961 procid, opno, fep->id, fep->parent);
3967 unresvsp_f(int opno, long r)
3972 struct xfs_flock64 fl;
3980 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3982 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3986 fd = open_path(&f, O_RDWR);
3987 e = fd < 0 ? errno : 0;
3991 printf("%d/%d: unresvsp - open %s failed %d\n",
3992 procid, opno, f.path, e);
3996 if (fstat64(fd, &stb) < 0) {
3998 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3999 procid, opno, f.path, errno);
4004 inode_info(st, sizeof(st), &stb, v);
4005 lr = ((int64_t)random() << 32) + random();
4006 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4008 fl.l_whence = SEEK_SET;
4010 fl.l_len = (off64_t)(random() % (1 << 20));
4011 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4013 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4014 procid, opno, f.path, st,
4015 (long long)off, (long long)fl.l_len, e);
4021 write_f(int opno, long r)
4035 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4037 printf("%d/%d: write - no filename\n", procid, opno);
4041 fd = open_path(&f, O_WRONLY);
4042 e = fd < 0 ? errno : 0;
4046 printf("%d/%d: write - open %s failed %d\n",
4047 procid, opno, f.path, e);
4051 if (fstat64(fd, &stb) < 0) {
4053 printf("%d/%d: write - fstat64 %s failed %d\n",
4054 procid, opno, f.path, errno);
4059 inode_info(st, sizeof(st), &stb, v);
4060 lr = ((int64_t)random() << 32) + random();
4061 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4063 lseek64(fd, off, SEEK_SET);
4064 len = (random() % FILELEN_MAX) + 1;
4066 memset(buf, nameseq & 0xff, len);
4067 e = write(fd, buf, len) < 0 ? errno : 0;
4070 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4071 procid, opno, f.path, st, (long long)off, (int)len, e);
4077 writev_f(int opno, long r)
4089 struct iovec *iov = NULL;
4096 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4098 printf("%d/%d: writev - no filename\n", procid, opno);
4102 fd = open_path(&f, O_WRONLY);
4103 e = fd < 0 ? errno : 0;
4107 printf("%d/%d: writev - open %s failed %d\n",
4108 procid, opno, f.path, e);
4112 if (fstat64(fd, &stb) < 0) {
4114 printf("%d/%d: writev - fstat64 %s failed %d\n",
4115 procid, opno, f.path, errno);
4120 inode_info(st, sizeof(st), &stb, v);
4121 lr = ((int64_t)random() << 32) + random();
4122 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4124 lseek64(fd, off, SEEK_SET);
4125 len = (random() % FILELEN_MAX) + 1;
4127 memset(buf, nameseq & 0xff, len);
4129 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4130 iov = calloc(iovcnt, sizeof(struct iovec));
4131 iovl = len / iovcnt;
4133 for (i=0; i<iovcnt; i++) {
4134 (iov + i)->iov_base = (buf + iovb);
4135 (iov + i)->iov_len = iovl;
4139 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4143 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4144 procid, opno, f.path, st, (long long)off, (int)iovl,