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");
736 assert(chdir(homedir) == 0);
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);
768 assert(chdir("..") == 0);
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);
787 assert(chdir("..") == 0);
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,
808 assert(chdir("..") == 0);
810 free_pathname(&newname);
818 struct stat64 statbuf;
820 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
822 assert(chdir(homedir) == 0);
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);
861 assert(chdir("..") == 0);
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");
999 assert(chdir("..") == 0);
1004 sprintf(cmd, "rm -rf %s", buf);
1007 perror("cleaning up");
1013 * build up a pathname going thru the file entry and all
1014 * its parent entries
1015 * Return 0 on error, 1 on success;
1018 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1020 char buf[NAME_MAX + 1];
1028 /* build up parent directory name */
1029 if (fep->parent != -1) {
1030 pfep = dirid_to_fent(fep->parent);
1033 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1034 procid, fep->id, fep->parent);
1039 e = fent_to_name(name, &flist[FT_DIR], pfep);
1042 append_pathname(name, "/");
1045 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1046 namerandpad(fep->id, buf, i);
1047 append_pathname(name, buf);
1052 fix_parent(int oldid, int newid)
1059 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1060 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1061 if (fep->parent == oldid)
1062 fep->parent = newid;
1068 free_pathname(pathname_t *name)
1078 * Generate a filename of type ft.
1079 * If we have a fep which should be a directory then use it
1080 * as the parent path for this new filename i.e. prepend with it.
1083 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1085 char buf[NAME_MAX + 1];
1094 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1095 namerandpad(id, buf, len);
1097 /* prepend fep parent dir-name to it */
1099 e = fent_to_name(name, &flist[FT_DIR], fep);
1102 append_pathname(name, "/");
1104 append_pathname(name, buf);
1108 for (j = 0; !*v && j < ilistlen; j++) {
1109 if (ilist[j] == id) {
1119 * Input: "which" to choose the file-types eg. non-directory
1120 * Input: "r" to choose which file
1121 * Output: file-list, file-entry, name for the chosen file.
1122 * Output: verbose if chosen file is on the ilist.
1125 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1128 int totalsum = 0; /* total number of matching files */
1129 int partialsum = 0; /* partial sum of matching files */
1135 int e = 1; /* success */
1138 * go thru flist and add up number of files for each
1139 * category that matches with <which>.
1141 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1142 if (which & (1 << i))
1143 totalsum += flp->nfiles;
1145 if (totalsum == 0) {
1155 * Now we have possible matches between 0..totalsum-1.
1156 * And we use r to help us choose which one we want,
1157 * which when bounded by totalsum becomes x.
1159 x = (int)(r % totalsum);
1160 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1161 if (which & (1 << i)) {
1162 if (x < partialsum + flp->nfiles) {
1164 /* found the matching file entry */
1165 fep = &flp->fents[x - partialsum];
1167 /* fill-in what we were asked for */
1169 e = fent_to_name(name, flp, fep);
1172 fprintf(stderr, "%d: failed to get path for entry:"
1173 " id=%d,parent=%d\n",
1174 procid, fep->id, fep->parent);
1183 /* turn on verbose if its an ilisted file */
1185 for (j = 0; !*v && j < ilistlen; j++) {
1186 if (ilist[j] == fep->id) {
1193 partialsum += flp->nfiles;
1197 fprintf(stderr, "fsstress: get_fname failure\n");
1204 init_pathname(pathname_t *name)
1211 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1213 char buf[NAME_MAX + 1];
1217 rval = lchown(name->path, owner, group);
1218 if (rval >= 0 || errno != ENAMETOOLONG)
1220 separate_pathname(name, buf, &newname);
1221 if (chdir(buf) == 0) {
1222 rval = lchown_path(&newname, owner, group);
1223 assert(chdir("..") == 0);
1225 free_pathname(&newname);
1230 link_path(pathname_t *name1, pathname_t *name2)
1232 char buf1[NAME_MAX + 1];
1233 char buf2[NAME_MAX + 1];
1235 pathname_t newname1;
1236 pathname_t newname2;
1239 rval = link(name1->path, name2->path);
1240 if (rval >= 0 || errno != ENAMETOOLONG)
1242 separate_pathname(name1, buf1, &newname1);
1243 separate_pathname(name2, buf2, &newname2);
1244 if (strcmp(buf1, buf2) == 0) {
1245 if (chdir(buf1) == 0) {
1246 rval = link_path(&newname1, &newname2);
1247 assert(chdir("..") == 0);
1250 if (strcmp(buf1, "..") == 0)
1252 else if (strcmp(buf2, "..") == 0)
1254 else if (strlen(buf1) == 0)
1256 else if (strlen(buf2) == 0)
1259 down1 = MAX(newname1.len, 3 + name2->len) <=
1260 MAX(3 + name1->len, newname2.len);
1262 free_pathname(&newname2);
1263 append_pathname(&newname2, "../");
1264 append_pathname(&newname2, name2->path);
1265 if (chdir(buf1) == 0) {
1266 rval = link_path(&newname1, &newname2);
1267 assert(chdir("..") == 0);
1270 free_pathname(&newname1);
1271 append_pathname(&newname1, "../");
1272 append_pathname(&newname1, name1->path);
1273 if (chdir(buf2) == 0) {
1274 rval = link_path(&newname1, &newname2);
1275 assert(chdir("..") == 0);
1279 free_pathname(&newname1);
1280 free_pathname(&newname2);
1285 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1287 char buf[NAME_MAX + 1];
1291 rval = lstat64(name->path, sbuf);
1292 if (rval >= 0 || errno != ENAMETOOLONG)
1294 separate_pathname(name, buf, &newname);
1295 if (chdir(buf) == 0) {
1296 rval = lstat64_path(&newname, sbuf);
1297 assert(chdir("..") == 0);
1299 free_pathname(&newname);
1304 make_freq_table(void)
1310 for (p = ops, f = 0; p < ops_end; p++)
1312 freq_table = malloc(f * sizeof(*freq_table));
1313 freq_table_size = f;
1314 for (p = ops, i = 0; p < ops_end; p++) {
1315 for (f = 0; f < p->freq; f++, i++)
1316 freq_table[i] = p->op;
1321 mkdir_path(pathname_t *name, mode_t mode)
1323 char buf[NAME_MAX + 1];
1327 rval = mkdir(name->path, mode);
1328 if (rval >= 0 || errno != ENAMETOOLONG)
1330 separate_pathname(name, buf, &newname);
1331 if (chdir(buf) == 0) {
1332 rval = mkdir_path(&newname, mode);
1333 assert(chdir("..") == 0);
1335 free_pathname(&newname);
1340 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1342 char buf[NAME_MAX + 1];
1346 rval = mknod(name->path, mode, dev);
1347 if (rval >= 0 || errno != ENAMETOOLONG)
1349 separate_pathname(name, buf, &newname);
1350 if (chdir(buf) == 0) {
1351 rval = mknod_path(&newname, mode, dev);
1352 assert(chdir("..") == 0);
1354 free_pathname(&newname);
1359 namerandpad(int id, char *buf, int i)
1362 static int buckets[] =
1363 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1369 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1370 padmod = buckets[bucket] + 1 - i;
1373 padlen = (id ^ namerand) % padmod;
1375 memset(&buf[i], 'X', padlen);
1376 buf[i + padlen] = '\0';
1381 open_path(pathname_t *name, int oflag)
1383 char buf[NAME_MAX + 1];
1387 rval = open(name->path, oflag);
1388 if (rval >= 0 || errno != ENAMETOOLONG)
1390 separate_pathname(name, buf, &newname);
1391 if (chdir(buf) == 0) {
1392 rval = open_path(&newname, oflag);
1393 assert(chdir("..") == 0);
1395 free_pathname(&newname);
1400 opendir_path(pathname_t *name)
1402 char buf[NAME_MAX + 1];
1406 rval = opendir(name->path);
1407 if (rval || errno != ENAMETOOLONG)
1409 separate_pathname(name, buf, &newname);
1410 if (chdir(buf) == 0) {
1411 rval = opendir_path(&newname);
1412 assert(chdir("..") == 0);
1414 free_pathname(&newname);
1419 process_freq(char *arg)
1424 s = strchr(arg, '=');
1426 fprintf(stderr, "bad argument '%s'\n", arg);
1430 for (p = ops; p < ops_end; p++) {
1431 if (strcmp(arg, p->name) == 0) {
1436 fprintf(stderr, "can't find op type %s for -f\n", arg);
1441 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1443 char buf[NAME_MAX + 1];
1447 rval = readlink(name->path, lbuf, lbufsiz);
1448 if (rval >= 0 || errno != ENAMETOOLONG)
1450 separate_pathname(name, buf, &newname);
1451 if (chdir(buf) == 0) {
1452 rval = readlink_path(&newname, lbuf, lbufsiz);
1453 assert(chdir("..") == 0);
1455 free_pathname(&newname);
1460 rename_path(pathname_t *name1, pathname_t *name2)
1462 char buf1[NAME_MAX + 1];
1463 char buf2[NAME_MAX + 1];
1465 pathname_t newname1;
1466 pathname_t newname2;
1469 rval = rename(name1->path, name2->path);
1470 if (rval >= 0 || errno != ENAMETOOLONG)
1472 separate_pathname(name1, buf1, &newname1);
1473 separate_pathname(name2, buf2, &newname2);
1474 if (strcmp(buf1, buf2) == 0) {
1475 if (chdir(buf1) == 0) {
1476 rval = rename_path(&newname1, &newname2);
1477 assert(chdir("..") == 0);
1480 if (strcmp(buf1, "..") == 0)
1482 else if (strcmp(buf2, "..") == 0)
1484 else if (strlen(buf1) == 0)
1486 else if (strlen(buf2) == 0)
1489 down1 = MAX(newname1.len, 3 + name2->len) <=
1490 MAX(3 + name1->len, newname2.len);
1492 free_pathname(&newname2);
1493 append_pathname(&newname2, "../");
1494 append_pathname(&newname2, name2->path);
1495 if (chdir(buf1) == 0) {
1496 rval = rename_path(&newname1, &newname2);
1497 assert(chdir("..") == 0);
1500 free_pathname(&newname1);
1501 append_pathname(&newname1, "../");
1502 append_pathname(&newname1, name1->path);
1503 if (chdir(buf2) == 0) {
1504 rval = rename_path(&newname1, &newname2);
1505 assert(chdir("..") == 0);
1509 free_pathname(&newname1);
1510 free_pathname(&newname2);
1515 rmdir_path(pathname_t *name)
1517 char buf[NAME_MAX + 1];
1521 rval = rmdir(name->path);
1522 if (rval >= 0 || errno != ENAMETOOLONG)
1524 separate_pathname(name, buf, &newname);
1525 if (chdir(buf) == 0) {
1526 rval = rmdir_path(&newname);
1527 assert(chdir("..") == 0);
1529 free_pathname(&newname);
1534 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1538 init_pathname(newname);
1539 slash = strchr(name->path, '/');
1540 if (slash == NULL) {
1545 strcpy(buf, name->path);
1547 append_pathname(newname, slash + 1);
1553 show_ops(int flag, char *lead_str)
1558 /* print in list form */
1561 for (p = ops; p < ops_end; p++) {
1562 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1563 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1564 x+=printf("%s ", p->name);
1567 } else if (flag == 0) {
1568 /* Table view style */
1570 for (f = 0, p = ops; p < ops_end; p++)
1576 for (p = ops; p < ops_end; p++) {
1577 if (flag != 0 || p->freq > 0) {
1578 if (lead_str != NULL)
1579 printf("%s", lead_str);
1580 printf("%20s %d/%d %s\n",
1581 p->name, p->freq, f,
1582 (p->iswrite == 0) ? " " : "write op");
1586 /* Command line style */
1587 if (lead_str != NULL)
1588 printf("%s", lead_str);
1589 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1591 for (p = ops; p < ops_end; p++)
1593 printf("-f %s=%d \\\n",p->name, p->freq);
1598 stat64_path(pathname_t *name, struct stat64 *sbuf)
1600 char buf[NAME_MAX + 1];
1604 rval = stat64(name->path, sbuf);
1605 if (rval >= 0 || errno != ENAMETOOLONG)
1607 separate_pathname(name, buf, &newname);
1608 if (chdir(buf) == 0) {
1609 rval = stat64_path(&newname, sbuf);
1610 assert(chdir("..") == 0);
1612 free_pathname(&newname);
1617 symlink_path(const char *name1, pathname_t *name)
1619 char buf[NAME_MAX + 1];
1623 if (!strcmp(name1, name->path)) {
1624 printf("yikes! %s %s\n", name1, name->path);
1628 rval = symlink(name1, name->path);
1629 if (rval >= 0 || errno != ENAMETOOLONG)
1631 separate_pathname(name, buf, &newname);
1632 if (chdir(buf) == 0) {
1633 rval = symlink_path(name1, &newname);
1634 assert(chdir("..") == 0);
1636 free_pathname(&newname);
1641 truncate64_path(pathname_t *name, off64_t length)
1643 char buf[NAME_MAX + 1];
1647 rval = truncate64(name->path, length);
1648 if (rval >= 0 || errno != ENAMETOOLONG)
1650 separate_pathname(name, buf, &newname);
1651 if (chdir(buf) == 0) {
1652 rval = truncate64_path(&newname, length);
1653 assert(chdir("..") == 0);
1655 free_pathname(&newname);
1660 unlink_path(pathname_t *name)
1662 char buf[NAME_MAX + 1];
1666 rval = unlink(name->path);
1667 if (rval >= 0 || errno != ENAMETOOLONG)
1669 separate_pathname(name, buf, &newname);
1670 if (chdir(buf) == 0) {
1671 rval = unlink_path(&newname);
1672 assert(chdir("..") == 0);
1674 free_pathname(&newname);
1681 printf("Usage: %s -H or\n", myprog);
1682 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1684 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1686 printf(" -c clean up the test directory after each run\n");
1687 printf(" -d dir specifies the base directory for operations\n");
1688 printf(" -e errtg specifies error injection stuff\n");
1689 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1690 printf(" the valid operation names are:\n");
1692 printf(" -i filenum get verbose output for this nth file object\n");
1693 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1694 printf(" *use 0 for infinite (default 1)\n");
1695 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1696 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1697 printf(" -o logfile specifies logfile name\n");
1698 printf(" -p nproc specifies the no. of processes (default 1)\n");
1699 printf(" -r specifies random name padding\n");
1700 printf(" -s seed specifies the seed for the random generator (default random)\n");
1701 printf(" -v specifies verbose mode\n");
1702 printf(" -w zeros frequencies of non-write operations\n");
1703 printf(" -x cmd execute command in the middle of operations\n");
1704 printf(" -z zeros frequencies of all operations\n");
1705 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1706 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1707 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1708 printf(" -H prints usage and exits\n");
1716 for (p = ops; p < ops_end; p++) {
1727 for (p = ops; p < ops_end; p++)
1731 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1734 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1735 verifiable_log ? -1: (long)s->st_ino,
1736 (long)s->st_nlink, s->st_uid, s->st_gid,
1737 (long long) s->st_blocks, (long long) s->st_size);
1741 afsync_f(int opno, long r)
1749 struct iocb *iocbs[] = { &iocb };
1750 struct io_event event;
1753 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1755 printf("%d/%d: afsync - no filename\n", procid, opno);
1759 fd = open_path(&f, O_WRONLY | O_DIRECT);
1760 e = fd < 0 ? errno : 0;
1764 printf("%d/%d: afsync - open %s failed %d\n",
1765 procid, opno, f.path, e);
1770 io_prep_fsync(&iocb, fd);
1771 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1773 printf("%d/%d: afsync - io_submit %s %d\n",
1774 procid, opno, f.path, e);
1779 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1781 printf("%d/%d: afsync - io_getevents failed %d\n",
1790 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1797 allocsp_f(int opno, long r)
1802 struct xfs_flock64 fl;
1810 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1812 printf("%d/%d: allocsp - no filename\n", procid, opno);
1816 fd = open_path(&f, O_RDWR);
1817 e = fd < 0 ? errno : 0;
1821 printf("%d/%d: allocsp - open %s failed %d\n",
1822 procid, opno, f.path, e);
1826 if (fstat64(fd, &stb) < 0) {
1828 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1829 procid, opno, f.path, errno);
1834 inode_info(st, sizeof(st), &stb, v);
1835 lr = ((int64_t)random() << 32) + random();
1836 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1838 fl.l_whence = SEEK_SET;
1841 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1843 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1844 procid, opno, f.path, st, (long long)off, e);
1852 do_aio_rw(int opno, long r, int flags)
1856 struct dioattr diob;
1868 struct io_event event;
1869 struct iocb *iocbs[] = { &iocb };
1870 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1873 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1875 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1879 fd = open_path(&f, flags|O_DIRECT);
1880 e = fd < 0 ? errno : 0;
1884 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1885 procid, opno, f.path, e);
1889 if (fstat64(fd, &stb) < 0) {
1891 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1892 procid, opno, f.path, errno);
1897 inode_info(st, sizeof(st), &stb, v);
1898 if (!iswrite && stb.st_size == 0) {
1900 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1906 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1909 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1910 " fallback to stat()\n",
1911 procid, opno, f.path, st, errno);
1912 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1913 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1915 dio_env = getenv("XFS_DIO_MIN");
1917 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1918 align = (int64_t)diob.d_miniosz;
1919 lr = ((int64_t)random() << 32) + random();
1920 len = (random() % FILELEN_MAX) + 1;
1921 len -= (len % align);
1924 else if (len > diob.d_maxiosz)
1925 len = diob.d_maxiosz;
1926 buf = memalign(diob.d_mem, len);
1929 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1930 off -= (off % align);
1932 memset(buf, nameseq & 0xff, len);
1933 io_prep_pwrite(&iocb, fd, buf, len, off);
1935 off = (off64_t)(lr % stb.st_size);
1936 off -= (off % align);
1937 io_prep_pread(&iocb, fd, buf, len, off);
1939 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1941 printf("%d/%d: %s - io_submit failed %d\n",
1942 procid, opno, iswrite ? "awrite" : "aread", e);
1947 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1949 printf("%d/%d: %s - io_getevents failed %d\n",
1950 procid, opno, iswrite ? "awrite" : "aread", e);
1956 e = event.res != len ? event.res2 : 0;
1959 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1960 procid, opno, iswrite ? "awrite" : "aread",
1961 f.path, st, (long long)off, (int)len, e);
1968 aread_f(int opno, long r)
1971 do_aio_rw(opno, r, O_RDONLY);
1976 attr_remove_f(int opno, long r)
1978 attrlist_ent_t *aep;
1982 attrlist_cursor_t cursor;
1991 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1992 append_pathname(&f, ".");
1994 bzero(&cursor, sizeof(cursor));
1996 bzero(buf, sizeof(buf));
1997 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2001 alist = (attrlist_t *)buf;
2002 total += alist->al_count;
2003 } while (alist->al_more);
2006 printf("%d/%d: attr_remove - no attrs for %s\n",
2007 procid, opno, f.path);
2011 which = (int)(random() % total);
2012 bzero(&cursor, sizeof(cursor));
2016 bzero(buf, sizeof(buf));
2017 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2021 alist = (attrlist_t *)buf;
2022 if (which < ent + alist->al_count) {
2023 aep = (attrlist_ent_t *)
2024 &buf[alist->al_offset[which - ent]];
2025 aname = aep->a_name;
2028 ent += alist->al_count;
2029 } while (alist->al_more);
2030 if (aname == NULL) {
2033 "%d/%d: attr_remove - name %d not found at %s\n",
2034 procid, opno, which, f.path);
2038 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2041 printf("%d/%d: attr_remove %s %s %d\n",
2042 procid, opno, f.path, aname, e);
2047 attr_set_f(int opno, long r)
2054 static int lengths[] = { 10, 100, 1000, 10000 };
2059 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2060 append_pathname(&f, ".");
2061 sprintf(aname, "a%x", nameseq++);
2062 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2063 len = (int)(random() % lengths[li]);
2067 memset(aval, nameseq & 0xff, len);
2068 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2073 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2079 awrite_f(int opno, long r)
2082 do_aio_rw(opno, r, O_WRONLY);
2087 bulkstat_f(int opno, long r)
2095 xfs_fsop_bulkreq_t bsr;
2098 nent = (r % 999) + 2;
2099 t = malloc(nent * sizeof(*t));
2100 fd = open(".", O_RDONLY);
2108 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2112 printf("%d/%d: bulkstat nent %d total %lld\n",
2113 procid, opno, nent, (long long)total);
2118 bulkstat1_f(int opno, long r)
2128 xfs_fsop_bulkreq_t bsr;
2131 good = random() & 1;
2133 /* use an inode we know exists */
2135 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2136 append_pathname(&f, ".");
2137 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2142 * pick a random inode
2144 * note this can generate kernel warning messages
2145 * since bulkstat_one will read the disk block that
2146 * would contain a given inode even if that disk
2147 * block doesn't contain inodes.
2149 * this is detected later, but not until after the
2150 * warning is displayed.
2152 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2158 fd = open(".", O_RDONLY);
2164 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2166 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2167 procid, opno, good?"real":"random",
2168 verifiable_log ? -1LL : (long long)ino, e);
2173 chown_f(int opno, long r)
2183 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2184 append_pathname(&f, ".");
2185 u = (uid_t)random();
2186 g = (gid_t)random();
2187 nbits = (int)(random() % idmodulo);
2188 u &= (1 << nbits) - 1;
2189 g &= (1 << nbits) - 1;
2190 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2193 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2197 /* reflink some arbitrary range of f1 to f2. */
2204 struct file_clone_range fcr;
2205 struct pathname fpath1;
2206 struct pathname fpath2;
2207 struct stat64 stat1;
2208 struct stat64 stat2;
2209 char inoinfo1[1024];
2210 char inoinfo2[1024];
2224 init_pathname(&fpath1);
2225 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2227 printf("%d/%d: clonerange read - no filename\n",
2232 init_pathname(&fpath2);
2233 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2235 printf("%d/%d: clonerange write - no filename\n",
2241 fd1 = open_path(&fpath1, O_RDONLY);
2242 e = fd1 < 0 ? errno : 0;
2246 printf("%d/%d: clonerange read - open %s failed %d\n",
2247 procid, opno, fpath1.path, e);
2251 fd2 = open_path(&fpath2, O_WRONLY);
2252 e = fd2 < 0 ? errno : 0;
2256 printf("%d/%d: clonerange write - open %s failed %d\n",
2257 procid, opno, fpath2.path, e);
2261 /* Get file stats */
2262 if (fstat64(fd1, &stat1) < 0) {
2264 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2265 procid, opno, fpath1.path, errno);
2268 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2270 if (fstat64(fd2, &stat2) < 0) {
2272 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2273 procid, opno, fpath2.path, errno);
2276 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2278 /* Calculate offsets */
2279 len = (random() % FILELEN_MAX) + 1;
2280 len &= ~(stat1.st_blksize - 1);
2282 len = stat1.st_blksize;
2283 if (len > stat1.st_size)
2284 len = stat1.st_size;
2286 lr = ((int64_t)random() << 32) + random();
2287 if (stat1.st_size == len)
2290 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2292 off1 &= ~(stat1.st_blksize - 1);
2295 * If srcfile == destfile, randomly generate destination ranges
2296 * until we find one that doesn't overlap the source range.
2298 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2300 lr = ((int64_t)random() << 32) + random();
2301 off2 = (off64_t)(lr % max_off2);
2303 off2 &= ~(stat2.st_blksize - 1);
2304 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2306 /* Clone data blocks */
2308 fcr.src_offset = off1;
2309 fcr.src_length = len;
2310 fcr.dest_offset = off2;
2312 ret = ioctl(fd2, FICLONERANGE, &fcr);
2313 e = ret < 0 ? errno : 0;
2315 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2317 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2318 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2321 printf(" error %d", e);
2330 free_pathname(&fpath2);
2332 free_pathname(&fpath1);
2336 /* dedupe some arbitrary range of f1 to f2...fn. */
2342 #ifdef FIDEDUPERANGE
2343 #define INFO_SZ 1024
2344 struct file_dedupe_range *fdr;
2345 struct pathname *fpath;
2346 struct stat64 *stat;
2358 if (flist[FT_REG].nfiles < 2)
2361 /* Pick somewhere between 2 and 128 files. */
2363 nr = random() % (flist[FT_REG].nfiles + 1);
2364 } while (nr < 2 || nr > 128);
2367 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2368 sizeof(struct file_dedupe_range));
2370 printf("%d/%d: line %d error %d\n",
2371 procid, opno, __LINE__, errno);
2374 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2375 sizeof(struct file_dedupe_range)));
2377 fpath = calloc(nr, sizeof(struct pathname));
2379 printf("%d/%d: line %d error %d\n",
2380 procid, opno, __LINE__, errno);
2384 stat = calloc(nr, sizeof(struct stat64));
2386 printf("%d/%d: line %d error %d\n",
2387 procid, opno, __LINE__, errno);
2391 info = calloc(nr, INFO_SZ);
2393 printf("%d/%d: line %d error %d\n",
2394 procid, opno, __LINE__, errno);
2398 off = calloc(nr, sizeof(off64_t));
2400 printf("%d/%d: line %d error %d\n",
2401 procid, opno, __LINE__, errno);
2405 v = calloc(nr, sizeof(int));
2407 printf("%d/%d: line %d error %d\n",
2408 procid, opno, __LINE__, errno);
2411 fd = calloc(nr, sizeof(int));
2413 printf("%d/%d: line %d error %d\n",
2414 procid, opno, __LINE__, errno);
2417 memset(fd, 0xFF, nr * sizeof(int));
2419 /* Get paths for all files */
2420 for (i = 0; i < nr; i++)
2421 init_pathname(&fpath[i]);
2423 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2425 printf("%d/%d: deduperange read - no filename\n",
2430 for (i = 1; i < nr; i++) {
2431 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2433 printf("%d/%d: deduperange write - no filename\n",
2440 fd[0] = open_path(&fpath[0], O_RDONLY);
2441 e = fd[0] < 0 ? errno : 0;
2445 printf("%d/%d: deduperange read - open %s failed %d\n",
2446 procid, opno, fpath[0].path, e);
2450 for (i = 1; i < nr; i++) {
2451 fd[i] = open_path(&fpath[i], O_WRONLY);
2452 e = fd[i] < 0 ? errno : 0;
2456 printf("%d/%d: deduperange write - open %s failed %d\n",
2457 procid, opno, fpath[i].path, e);
2462 /* Get file stats */
2463 if (fstat64(fd[0], &stat[0]) < 0) {
2465 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2466 procid, opno, fpath[0].path, errno);
2470 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2472 for (i = 1; i < nr; i++) {
2473 if (fstat64(fd[i], &stat[i]) < 0) {
2475 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2476 procid, opno, fpath[i].path, errno);
2479 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2482 /* Never try to dedupe more than half of the src file. */
2483 len = (random() % FILELEN_MAX) + 1;
2484 len &= ~(stat[0].st_blksize - 1);
2486 len = stat[0].st_blksize / 2;
2487 if (len > stat[0].st_size / 2)
2488 len = stat[0].st_size / 2;
2490 /* Calculate offsets */
2491 lr = ((int64_t)random() << 32) + random();
2492 if (stat[0].st_size == len)
2495 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2497 off[0] &= ~(stat[0].st_blksize - 1);
2500 * If srcfile == destfile[i], randomly generate destination ranges
2501 * until we find one that doesn't overlap the source range.
2503 for (i = 1; i < nr; i++) {
2507 lr = ((int64_t)random() << 32) + random();
2508 if (stat[i].st_size <= len)
2511 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2513 off[i] &= ~(stat[i].st_blksize - 1);
2514 } while (stat[0].st_ino == stat[i].st_ino &&
2515 llabs(off[i] - off[0]) < len &&
2519 /* Clone data blocks */
2520 fdr->src_offset = off[0];
2521 fdr->src_length = len;
2522 fdr->dest_count = nr - 1;
2523 for (i = 1; i < nr; i++) {
2524 fdr->info[i - 1].dest_fd = fd[i];
2525 fdr->info[i - 1].dest_offset = off[i];
2528 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2529 e = ret < 0 ? errno : 0;
2531 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2533 fpath[0].path, &info[0], (long long)off[0],
2536 printf(" error %d", e);
2542 for (i = 1; i < nr; i++) {
2543 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2545 printf("%d/%d: ...to %s%s [%lld,%lld]",
2547 fpath[i].path, &info[i * INFO_SZ],
2548 (long long)off[i], (long long)len);
2549 if (fdr->info[i - 1].status < 0)
2550 printf(" error %d", e);
2551 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2552 printf(" %llu bytes deduplicated",
2553 fdr->info[i - 1].bytes_deduped);
2554 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2555 printf(" differed");
2561 for (i = 0; i < nr; i++)
2565 for (i = 0; i < nr; i++)
2566 free_pathname(&fpath[i]);
2585 setxattr_f(int opno, long r)
2587 #ifdef XFS_XFLAG_EXTSIZE
2597 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2598 append_pathname(&f, ".");
2599 fd = open_path(&f, O_RDWR);
2600 e = fd < 0 ? errno : 0;
2605 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2606 nbits = (int)(random() % e);
2607 p &= (1 << nbits) - 1;
2609 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2611 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2614 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2621 creat_f(int opno, long r)
2636 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2641 e1 = (random() % 100);
2642 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2644 if (type == FT_RTF) /* rt always gets an extsize */
2645 extsize = (random() % 10) + 1;
2646 else if (e1 < 10) /* one-in-ten get an extsize */
2647 extsize = random() % 1024;
2651 e = generate_fname(fep, type, &f, &id, &v);
2655 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2656 printf("%d/%d: creat - no filename from %s\n",
2657 procid, opno, f.path);
2662 fd = creat_path(&f, 0666);
2663 e = fd < 0 ? errno : 0;
2668 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2669 if (type == FT_RTF) {
2670 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2671 a.fsx_extsize = extsize *
2672 geom.rtextsize * geom.blocksize;
2674 } else if (extsize) {
2675 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2676 a.fsx_extsize = extsize * geom.blocksize;
2679 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2682 add_to_flist(type, id, parid);
2686 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2687 extsize ? a.fsx_extsize : 0, e, e1);
2688 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2694 dread_f(int opno, long r)
2698 struct dioattr diob;
2711 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2713 printf("%d/%d: dread - no filename\n", procid, opno);
2717 fd = open_path(&f, O_RDONLY|O_DIRECT);
2718 e = fd < 0 ? errno : 0;
2722 printf("%d/%d: dread - open %s failed %d\n",
2723 procid, opno, f.path, e);
2727 if (fstat64(fd, &stb) < 0) {
2729 printf("%d/%d: dread - fstat64 %s failed %d\n",
2730 procid, opno, f.path, errno);
2735 inode_info(st, sizeof(st), &stb, v);
2736 if (stb.st_size == 0) {
2738 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2744 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2747 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2748 " fallback to stat()\n",
2749 procid, opno, f.path, st, errno);
2750 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2751 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2754 dio_env = getenv("XFS_DIO_MIN");
2756 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2758 align = (int64_t)diob.d_miniosz;
2759 lr = ((int64_t)random() << 32) + random();
2760 off = (off64_t)(lr % stb.st_size);
2761 off -= (off % align);
2762 lseek64(fd, off, SEEK_SET);
2763 len = (random() % FILELEN_MAX) + 1;
2764 len -= (len % align);
2767 else if (len > diob.d_maxiosz)
2768 len = diob.d_maxiosz;
2769 buf = memalign(diob.d_mem, len);
2770 e = read(fd, buf, len) < 0 ? errno : 0;
2773 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2774 procid, opno, f.path, st, (long long)off, (int)len, e);
2780 dwrite_f(int opno, long r)
2784 struct dioattr diob;
2797 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2799 printf("%d/%d: dwrite - no filename\n", procid, opno);
2803 fd = open_path(&f, O_WRONLY|O_DIRECT);
2804 e = fd < 0 ? errno : 0;
2808 printf("%d/%d: dwrite - open %s failed %d\n",
2809 procid, opno, f.path, e);
2813 if (fstat64(fd, &stb) < 0) {
2815 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2816 procid, opno, f.path, errno);
2821 inode_info(st, sizeof(st), &stb, v);
2822 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2824 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2825 " %s%s return %d, fallback to stat()\n",
2826 procid, opno, f.path, st, errno);
2827 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2828 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2831 dio_env = getenv("XFS_DIO_MIN");
2833 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2835 align = (int64_t)diob.d_miniosz;
2836 lr = ((int64_t)random() << 32) + random();
2837 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2838 off -= (off % align);
2839 lseek64(fd, off, SEEK_SET);
2840 len = (random() % FILELEN_MAX) + 1;
2841 len -= (len % align);
2844 else if (len > diob.d_maxiosz)
2845 len = diob.d_maxiosz;
2846 buf = memalign(diob.d_mem, len);
2848 lseek64(fd, off, SEEK_SET);
2849 memset(buf, nameseq & 0xff, len);
2850 e = write(fd, buf, len) < 0 ? errno : 0;
2853 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2854 procid, opno, f.path, st, (long long)off, (int)len, e);
2860 #ifdef HAVE_LINUX_FALLOC_H
2861 struct print_flags falloc_flags [] = {
2862 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2863 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2864 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2865 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2866 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2867 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2871 #define translate_falloc_flags(mode) \
2872 ({translate_flags(mode, "|", falloc_flags);})
2876 do_fallocate(int opno, long r, int mode)
2878 #ifdef HAVE_LINUX_FALLOC_H
2890 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2892 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2896 fd = open_path(&f, O_RDWR);
2899 printf("%d/%d: do_fallocate - open %s failed %d\n",
2900 procid, opno, f.path, errno);
2905 if (fstat64(fd, &stb) < 0) {
2907 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2908 procid, opno, f.path, errno);
2913 inode_info(st, sizeof(st), &stb, v);
2914 lr = ((int64_t)random() << 32) + random();
2915 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2917 len = (off64_t)(random() % (1024 * 1024));
2919 * Collapse/insert range requires off and len to be block aligned,
2920 * make it more likely to be the case.
2922 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2924 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2925 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2927 mode |= FALLOC_FL_KEEP_SIZE & random();
2928 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2930 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2931 procid, opno, translate_falloc_flags(mode),
2932 f.path, st, (long long)off, (long long)len, e);
2939 fallocate_f(int opno, long r)
2941 #ifdef HAVE_LINUX_FALLOC_H
2942 do_fallocate(opno, r, 0);
2947 fdatasync_f(int opno, long r)
2955 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2957 printf("%d/%d: fdatasync - no filename\n",
2962 fd = open_path(&f, O_WRONLY);
2963 e = fd < 0 ? errno : 0;
2967 printf("%d/%d: fdatasync - open %s failed %d\n",
2968 procid, opno, f.path, e);
2972 e = fdatasync(fd) < 0 ? errno : 0;
2974 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2979 #ifdef HAVE_LINUX_FIEMAP_H
2980 struct print_flags fiemap_flags[] = {
2981 { FIEMAP_FLAG_SYNC, "SYNC"},
2982 { FIEMAP_FLAG_XATTR, "XATTR"},
2986 #define translate_fiemap_flags(mode) \
2987 ({translate_flags(mode, "|", fiemap_flags);})
2991 fiemap_f(int opno, long r)
2993 #ifdef HAVE_LINUX_FIEMAP_H
3003 struct fiemap *fiemap;
3006 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3008 printf("%d/%d: fiemap - no filename\n", procid, opno);
3012 fd = open_path(&f, O_RDWR);
3013 e = fd < 0 ? errno : 0;
3017 printf("%d/%d: fiemap - open %s failed %d\n",
3018 procid, opno, f.path, e);
3022 if (fstat64(fd, &stb) < 0) {
3024 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3025 procid, opno, f.path, errno);
3030 inode_info(st, sizeof(st), &stb, v);
3031 blocks_to_map = random() & 0xffff;
3032 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3033 (blocks_to_map * sizeof(struct fiemap_extent)));
3036 printf("%d/%d: malloc failed \n", procid, opno);
3041 lr = ((int64_t)random() << 32) + random();
3042 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3044 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3045 fiemap->fm_extent_count = blocks_to_map;
3046 fiemap->fm_mapped_extents = random() & 0xffff;
3047 fiemap->fm_start = off;
3048 fiemap->fm_length = ((int64_t)random() << 32) + random();
3050 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3052 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3053 procid, opno, f.path, st, (long long)fiemap->fm_start,
3054 (long long) fiemap->fm_length,
3055 translate_fiemap_flags(fiemap->fm_flags), e);
3063 freesp_f(int opno, long r)
3068 struct xfs_flock64 fl;
3076 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3078 printf("%d/%d: freesp - no filename\n", procid, opno);
3082 fd = open_path(&f, O_RDWR);
3083 e = fd < 0 ? errno : 0;
3087 printf("%d/%d: freesp - open %s failed %d\n",
3088 procid, opno, f.path, e);
3092 if (fstat64(fd, &stb) < 0) {
3094 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3095 procid, opno, f.path, errno);
3100 inode_info(st, sizeof(st), &stb, v);
3101 lr = ((int64_t)random() << 32) + random();
3102 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3104 fl.l_whence = SEEK_SET;
3107 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3109 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3110 procid, opno, f.path, st, (long long)off, e);
3116 fsync_f(int opno, long r)
3124 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3126 printf("%d/%d: fsync - no filename\n", procid, opno);
3130 fd = open_path(&f, O_WRONLY);
3131 e = fd < 0 ? errno : 0;
3135 printf("%d/%d: fsync - open %s failed %d\n",
3136 procid, opno, f.path, e);
3140 e = fsync(fd) < 0 ? errno : 0;
3142 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3148 getattr_f(int opno, long r)
3157 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3158 append_pathname(&f, ".");
3159 fd = open_path(&f, O_RDWR);
3160 e = fd < 0 ? errno : 0;
3163 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3165 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3171 getdents_f(int opno, long r)
3178 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3179 append_pathname(&f, ".");
3180 dir = opendir_path(&f);
3184 printf("%d/%d: getdents - can't open %s\n",
3185 procid, opno, f.path);
3189 while (readdir64(dir) != NULL)
3192 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3198 link_f(int opno, long r)
3211 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3213 printf("%d/%d: link - no file\n", procid, opno);
3217 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3223 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3227 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3228 printf("%d/%d: link - no filename from %s\n",
3229 procid, opno, l.path);
3235 e = link_path(&f, &l) < 0 ? errno : 0;
3238 add_to_flist(flp - flist, id, parid);
3240 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3242 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3249 mkdir_f(int opno, long r)
3259 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3264 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3268 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3269 printf("%d/%d: mkdir - no filename from %s\n",
3270 procid, opno, f.path);
3275 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3278 add_to_flist(FT_DIR, id, parid);
3280 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3281 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3287 mknod_f(int opno, long r)
3297 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3302 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3306 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3307 printf("%d/%d: mknod - no filename from %s\n",
3308 procid, opno, f.path);
3313 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3316 add_to_flist(FT_DEV, id, parid);
3318 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3319 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3324 #ifdef HAVE_SYS_MMAN_H
3325 struct print_flags mmap_flags[] = {
3326 { MAP_SHARED, "SHARED"},
3327 { MAP_PRIVATE, "PRIVATE"},
3331 #define translate_mmap_flags(flags) \
3332 ({translate_flags(flags, "|", mmap_flags);})
3336 do_mmap(int opno, long r, int prot)
3338 #ifdef HAVE_SYS_MMAN_H
3350 sigjmp_buf sigbus_jmpbuf;
3353 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3355 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3359 fd = open_path(&f, O_RDWR);
3360 e = fd < 0 ? errno : 0;
3364 printf("%d/%d: do_mmap - open %s failed %d\n",
3365 procid, opno, f.path, e);
3369 if (fstat64(fd, &stb) < 0) {
3371 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3372 procid, opno, f.path, errno);
3377 inode_info(st, sizeof(st), &stb, v);
3378 if (stb.st_size == 0) {
3380 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3387 lr = ((int64_t)random() << 32) + random();
3388 off = (off64_t)(lr % stb.st_size);
3389 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3390 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3392 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3393 addr = mmap(NULL, len, prot, flags, fd, off);
3394 e = (addr == MAP_FAILED) ? errno : 0;
3397 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3398 procid, opno, f.path, st, (long long)off,
3399 (int)len, translate_mmap_flags(flags), e);
3405 if (prot & PROT_WRITE) {
3406 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3407 sigbus_jmp = &sigbus_jmpbuf;
3408 memset(addr, nameseq & 0xff, len);
3412 if ((buf = malloc(len)) != NULL) {
3413 memcpy(buf, addr, len);
3418 /* set NULL to stop other functions from doing siglongjmp */
3422 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3423 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3424 f.path, st, (long long)off, (int)len,
3425 translate_mmap_flags(flags),
3426 (e == 0) ? "0" : "Bus error");
3434 mread_f(int opno, long r)
3436 #ifdef HAVE_SYS_MMAN_H
3437 do_mmap(opno, r, PROT_READ);
3442 mwrite_f(int opno, long r)
3444 #ifdef HAVE_SYS_MMAN_H
3445 do_mmap(opno, r, PROT_WRITE);
3450 punch_f(int opno, long r)
3452 #ifdef HAVE_LINUX_FALLOC_H
3453 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3458 zero_f(int opno, long r)
3460 #ifdef HAVE_LINUX_FALLOC_H
3461 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3466 collapse_f(int opno, long r)
3468 #ifdef HAVE_LINUX_FALLOC_H
3469 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3474 insert_f(int opno, long r)
3476 #ifdef HAVE_LINUX_FALLOC_H
3477 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3482 read_f(int opno, long r)
3496 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3498 printf("%d/%d: read - no filename\n", procid, opno);
3502 fd = open_path(&f, O_RDONLY);
3503 e = fd < 0 ? errno : 0;
3507 printf("%d/%d: read - open %s failed %d\n",
3508 procid, opno, f.path, e);
3512 if (fstat64(fd, &stb) < 0) {
3514 printf("%d/%d: read - fstat64 %s failed %d\n",
3515 procid, opno, f.path, errno);
3520 inode_info(st, sizeof(st), &stb, v);
3521 if (stb.st_size == 0) {
3523 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3529 lr = ((int64_t)random() << 32) + random();
3530 off = (off64_t)(lr % stb.st_size);
3531 lseek64(fd, off, SEEK_SET);
3532 len = (random() % FILELEN_MAX) + 1;
3534 e = read(fd, buf, len) < 0 ? errno : 0;
3537 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3538 procid, opno, f.path, st, (long long)off, (int)len, e);
3544 readlink_f(int opno, long r)
3552 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3554 printf("%d/%d: readlink - no filename\n", procid, opno);
3558 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3561 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3566 readv_f(int opno, long r)
3578 struct iovec *iov = NULL;
3585 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3587 printf("%d/%d: readv - no filename\n", procid, opno);
3591 fd = open_path(&f, O_RDONLY);
3592 e = fd < 0 ? errno : 0;
3596 printf("%d/%d: readv - open %s failed %d\n",
3597 procid, opno, f.path, e);
3601 if (fstat64(fd, &stb) < 0) {
3603 printf("%d/%d: readv - fstat64 %s failed %d\n",
3604 procid, opno, f.path, errno);
3609 inode_info(st, sizeof(st), &stb, v);
3610 if (stb.st_size == 0) {
3612 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3618 lr = ((int64_t)random() << 32) + random();
3619 off = (off64_t)(lr % stb.st_size);
3620 lseek64(fd, off, SEEK_SET);
3621 len = (random() % FILELEN_MAX) + 1;
3624 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
3625 iov = calloc(iovcnt, sizeof(struct iovec));
3626 iovl = len / iovcnt;
3628 for (i=0; i<iovcnt; i++) {
3629 (iov + i)->iov_base = (buf + iovb);
3630 (iov + i)->iov_len = iovl;
3634 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
3637 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
3638 procid, opno, f.path, st, (long long)off, (int)iovl,
3645 rename_f(int opno, long r)
3659 /* get an existing path for the source of the rename */
3661 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
3663 printf("%d/%d: rename - no filename\n", procid, opno);
3668 /* get an existing directory for the destination parent directory name */
3669 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
3675 /* generate a new path using an existing parent directory in name */
3676 init_pathname(&newf);
3677 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
3681 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
3682 printf("%d/%d: rename - no filename from %s\n",
3683 procid, opno, f.path);
3685 free_pathname(&newf);
3689 e = rename_path(&f, &newf) < 0 ? errno : 0;
3692 if (flp - flist == FT_DIR) {
3694 fix_parent(oldid, id);
3696 del_from_flist(flp - flist, fep - flp->fents);
3697 add_to_flist(flp - flist, id, parid);
3700 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3703 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3704 procid, opno, fep->id, fep->parent);
3705 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3706 procid, opno, id, parid);
3709 free_pathname(&newf);
3714 resvsp_f(int opno, long r)
3719 struct xfs_flock64 fl;
3727 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3729 printf("%d/%d: resvsp - no filename\n", procid, opno);
3733 fd = open_path(&f, O_RDWR);
3734 e = fd < 0 ? errno : 0;
3738 printf("%d/%d: resvsp - open %s failed %d\n",
3739 procid, opno, f.path, e);
3743 if (fstat64(fd, &stb) < 0) {
3745 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3746 procid, opno, f.path, errno);
3751 inode_info(st, sizeof(st), &stb, v);
3752 lr = ((int64_t)random() << 32) + random();
3753 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3755 fl.l_whence = SEEK_SET;
3757 fl.l_len = (off64_t)(random() % (1024 * 1024));
3758 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3760 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3761 procid, opno, f.path, st,
3762 (long long)off, (long long)fl.l_len, e);
3768 rmdir_f(int opno, long r)
3776 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3778 printf("%d/%d: rmdir - no directory\n", procid, opno);
3782 e = rmdir_path(&f) < 0 ? errno : 0;
3785 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3787 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3789 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3790 procid, opno, fep->id, fep->parent);
3796 setattr_f(int opno, long r)
3805 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3806 append_pathname(&f, ".");
3807 fd = open_path(&f, O_RDWR);
3808 e = fd < 0 ? errno : 0;
3811 fl = attr_mask & (uint)random();
3812 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3814 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3820 stat_f(int opno, long r)
3828 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3830 printf("%d/%d: stat - no entries\n", procid, opno);
3834 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3837 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3842 symlink_f(int opno, long r)
3855 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3860 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3864 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3865 printf("%d/%d: symlink - no filename from %s\n",
3866 procid, opno, f.path);
3871 len = (int)(random() % PATH_MAX);
3872 val = malloc(len + 1);
3874 memset(val, 'x', len);
3876 for (i = 10; i < len - 1; i += 10)
3878 e = symlink_path(val, &f) < 0 ? errno : 0;
3881 add_to_flist(FT_SYM, id, parid);
3884 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3885 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3892 sync_f(int opno, long r)
3896 printf("%d/%d: sync\n", procid, opno);
3900 truncate_f(int opno, long r)
3911 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3913 printf("%d/%d: truncate - no filename\n", procid, opno);
3917 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3921 printf("%d/%d: truncate - stat64 %s failed %d\n",
3922 procid, opno, f.path, e);
3926 inode_info(st, sizeof(st), &stb, v);
3927 lr = ((int64_t)random() << 32) + random();
3928 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3930 e = truncate64_path(&f, off) < 0 ? errno : 0;
3933 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3934 st, (long long)off, e);
3939 unlink_f(int opno, long r)
3948 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3950 printf("%d/%d: unlink - no file\n", procid, opno);
3954 e = unlink_path(&f) < 0 ? errno : 0;
3957 del_from_flist(flp - flist, fep - flp->fents);
3959 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3961 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3962 procid, opno, fep->id, fep->parent);
3968 unresvsp_f(int opno, long r)
3973 struct xfs_flock64 fl;
3981 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3983 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3987 fd = open_path(&f, O_RDWR);
3988 e = fd < 0 ? errno : 0;
3992 printf("%d/%d: unresvsp - open %s failed %d\n",
3993 procid, opno, f.path, e);
3997 if (fstat64(fd, &stb) < 0) {
3999 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4000 procid, opno, f.path, errno);
4005 inode_info(st, sizeof(st), &stb, v);
4006 lr = ((int64_t)random() << 32) + random();
4007 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4009 fl.l_whence = SEEK_SET;
4011 fl.l_len = (off64_t)(random() % (1 << 20));
4012 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4014 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4015 procid, opno, f.path, st,
4016 (long long)off, (long long)fl.l_len, e);
4022 write_f(int opno, long r)
4036 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4038 printf("%d/%d: write - no filename\n", procid, opno);
4042 fd = open_path(&f, O_WRONLY);
4043 e = fd < 0 ? errno : 0;
4047 printf("%d/%d: write - open %s failed %d\n",
4048 procid, opno, f.path, e);
4052 if (fstat64(fd, &stb) < 0) {
4054 printf("%d/%d: write - fstat64 %s failed %d\n",
4055 procid, opno, f.path, errno);
4060 inode_info(st, sizeof(st), &stb, v);
4061 lr = ((int64_t)random() << 32) + random();
4062 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4064 lseek64(fd, off, SEEK_SET);
4065 len = (random() % FILELEN_MAX) + 1;
4067 memset(buf, nameseq & 0xff, len);
4068 e = write(fd, buf, len) < 0 ? errno : 0;
4071 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4072 procid, opno, f.path, st, (long long)off, (int)len, e);
4078 writev_f(int opno, long r)
4090 struct iovec *iov = NULL;
4097 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4099 printf("%d/%d: writev - no filename\n", procid, opno);
4103 fd = open_path(&f, O_WRONLY);
4104 e = fd < 0 ? errno : 0;
4108 printf("%d/%d: writev - open %s failed %d\n",
4109 procid, opno, f.path, e);
4113 if (fstat64(fd, &stb) < 0) {
4115 printf("%d/%d: writev - fstat64 %s failed %d\n",
4116 procid, opno, f.path, errno);
4121 inode_info(st, sizeof(st), &stb, v);
4122 lr = ((int64_t)random() << 32) + random();
4123 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4125 lseek64(fd, off, SEEK_SET);
4126 len = (random() % FILELEN_MAX) + 1;
4128 memset(buf, nameseq & 0xff, len);
4130 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4131 iov = calloc(iovcnt, sizeof(struct iovec));
4132 iovl = len / iovcnt;
4134 for (i=0; i<iovcnt; i++) {
4135 (iov + i)->iov_base = (buf + iovb);
4136 (iov + i)->iov_len = iovl;
4140 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4144 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4145 procid, opno, f.path, st, (long long)off, (int)iovl,