2 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_ATTR_XATTR_H
22 #include <attr/xattr.h>
24 #ifdef HAVE_ATTR_ATTRIBUTES_H
25 #include <attr/attributes.h>
27 #ifdef HAVE_LINUX_FIEMAP_H
28 #include <linux/fiemap.h>
30 #ifndef HAVE_ATTR_LIST
31 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
33 #ifdef HAVE_SYS_PRCTL_H
34 #include <sys/prctl.h>
38 #ifndef FS_IOC_GETFLAGS
39 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
41 #ifndef FS_IOC_SETFLAGS
42 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
46 #define XFS_ERRTAG_MAX 17
47 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
48 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
50 #define FILELEN_MAX (32*4096)
92 typedef void (*opfnc_t)(int, long);
94 typedef struct opdesc {
102 typedef struct fent {
107 typedef struct flist {
114 typedef struct pathname {
124 struct print_string {
131 #define FT_DIRm (1 << FT_DIR)
133 #define FT_REGm (1 << FT_REG)
135 #define FT_SYMm (1 << FT_SYM)
137 #define FT_DEVm (1 << FT_DEV)
139 #define FT_RTFm (1 << FT_RTF)
141 #define FT_ANYm ((1 << FT_nft) - 1)
142 #define FT_REGFILE (FT_REGm | FT_RTFm)
143 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
145 #define FLIST_SLOT_INCR 16
148 #define MAXFSIZE ((1ULL << 63) - 1ULL)
149 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
151 void allocsp_f(int, long);
152 void attr_remove_f(int, long);
153 void attr_set_f(int, long);
154 void bulkstat_f(int, long);
155 void bulkstat1_f(int, long);
156 void chown_f(int, long);
157 void creat_f(int, long);
158 void dread_f(int, long);
159 void dwrite_f(int, long);
160 void fallocate_f(int, long);
161 void fdatasync_f(int, long);
162 void fiemap_f(int, long);
163 void freesp_f(int, long);
164 void fsync_f(int, long);
165 void getattr_f(int, long);
166 void getdents_f(int, long);
167 void link_f(int, long);
168 void mkdir_f(int, long);
169 void mknod_f(int, long);
170 void punch_f(int, long);
171 void zero_f(int, long);
172 void collapse_f(int, long);
173 void read_f(int, long);
174 void readlink_f(int, long);
175 void rename_f(int, long);
176 void resvsp_f(int, long);
177 void rmdir_f(int, long);
178 void setattr_f(int, long);
179 void setxattr_f(int, long);
180 void stat_f(int, long);
181 void symlink_f(int, long);
182 void sync_f(int, long);
183 void truncate_f(int, long);
184 void unlink_f(int, long);
185 void unresvsp_f(int, long);
186 void write_f(int, long);
189 /* { OP_ENUM, "name", function, freq, iswrite }, */
190 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
191 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
192 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
193 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
194 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
195 { OP_CHOWN, "chown", chown_f, 3, 1 },
196 { OP_CREAT, "creat", creat_f, 4, 1 },
197 { OP_DREAD, "dread", dread_f, 4, 0 },
198 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
199 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
200 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
201 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
202 { OP_FREESP, "freesp", freesp_f, 1, 1 },
203 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
204 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
205 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
206 { OP_LINK, "link", link_f, 1, 1 },
207 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
208 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
209 { OP_PUNCH, "punch", punch_f, 1, 1 },
210 { OP_ZERO, "zero", zero_f, 1, 1 },
211 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
212 { OP_READ, "read", read_f, 1, 0 },
213 { OP_READLINK, "readlink", readlink_f, 1, 0 },
214 { OP_RENAME, "rename", rename_f, 2, 1 },
215 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
216 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
217 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
218 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
219 { OP_STAT, "stat", stat_f, 1, 0 },
220 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
221 { OP_SYNC, "sync", sync_f, 1, 1 },
222 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
223 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
224 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
225 { OP_WRITE, "write", write_f, 4, 1 },
228 flist_t flist[FT_nft] = {
241 xfs_fsop_geom_t geom;
252 unsigned int idmodulo = XFS_IDMODULO_MAX;
253 unsigned int attr_mask = ~0;
256 unsigned long seed = 0;
259 int verifiable_log = 0;
260 sig_atomic_t should_stop = 0;
261 char *execute_cmd = NULL;
262 int execute_freq = 1;
263 struct print_string flag_str = {0};
265 void add_to_flist(int, int, int);
266 void append_pathname(pathname_t *, char *);
267 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
268 int attr_remove_path(pathname_t *, const char *, int);
269 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
270 void check_cwd(void);
271 int creat_path(pathname_t *, mode_t);
272 void dcache_enter(int, int);
273 void dcache_init(void);
274 fent_t *dcache_lookup(int);
275 void dcache_purge(int);
276 void del_from_flist(int, int);
277 int dirid_to_name(char *, int);
279 int fent_to_name(pathname_t *, flist_t *, fent_t *);
280 void fix_parent(int, int);
281 void free_pathname(pathname_t *);
282 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
283 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
284 void init_pathname(pathname_t *);
285 int lchown_path(pathname_t *, uid_t, gid_t);
286 int link_path(pathname_t *, pathname_t *);
287 int lstat64_path(pathname_t *, struct stat64 *);
288 void make_freq_table(void);
289 int mkdir_path(pathname_t *, mode_t);
290 int mknod_path(pathname_t *, mode_t, dev_t);
291 void namerandpad(int, char *, int);
292 int open_path(pathname_t *, int);
293 DIR *opendir_path(pathname_t *);
294 void process_freq(char *);
295 int readlink_path(pathname_t *, char *, size_t);
296 int rename_path(pathname_t *, pathname_t *);
297 int rmdir_path(pathname_t *);
298 void separate_pathname(pathname_t *, char *, pathname_t *);
299 void show_ops(int, char *);
300 int stat64_path(pathname_t *, struct stat64 *);
301 int symlink_path(const char *, pathname_t *);
302 int truncate64_path(pathname_t *, off64_t);
303 int unlink_path(pathname_t *);
305 void write_freq(void);
306 void zero_freq(void);
308 void sg_handler(int signum)
313 int main(int argc, char **argv)
317 char *dirname = NULL;
318 char *logname = NULL;
319 char rpath[PATH_MAX];
328 xfs_error_injection_t err_inj;
329 struct sigaction action;
330 const char *allopts = "d:e:f:i:m:M:n:o:p:rs:S:vVwx:X:zH";
332 errrange = errtag = 0;
334 nops = sizeof(ops) / sizeof(ops[0]);
335 ops_end = &ops[nops];
337 while ((c = getopt(argc, argv, allopts)) != -1) {
343 sscanf(optarg, "%d", &errtag);
347 } else if (errtag == 0)
349 if (errtag >= XFS_ERRTAG_MAX) {
351 "error tag %d too large (max %d)\n",
352 errtag, XFS_ERRTAG_MAX - 1);
357 process_freq(optarg);
360 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
361 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
364 idmodulo = strtoul(optarg, NULL, 0);
365 if (idmodulo > XFS_IDMODULO_MAX) {
367 "chown modulo %d too big (max %d)\n",
368 idmodulo, XFS_IDMODULO_MAX);
373 operations = atoi(optarg);
380 nproc = atoi(optarg);
386 seed = strtoul(optarg, NULL, 0);
395 execute_cmd = optarg;
401 attr_mask = strtoul(optarg, NULL, 0);
405 if (optarg[0] == 'c')
416 execute_freq = strtoul(optarg, NULL, 0);
419 fprintf(stderr, "%s - invalid parameters\n",
429 /* no directory specified */
430 if (!nousage) usage();
434 (void)mkdir(dirname, 0777);
435 if (logname && logname[0] != '/') {
436 if (getcwd(rpath, sizeof(rpath)) < 0){
437 perror("getcwd failed");
443 if (chdir(dirname) < 0) {
449 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
450 if (freopen(path, "a", stdout) == NULL) {
451 perror("freopen logfile failed");
455 sprintf(buf, "fss%x", (unsigned int)getpid());
456 fd = creat(buf, 0666);
457 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
458 maxfsize = (off64_t)MAXFSIZE32;
460 maxfsize = (off64_t)MAXFSIZE;
465 gettimeofday(&t, (void *)NULL);
466 seed = (int)t.tv_sec ^ (int)t.tv_usec;
467 printf("seed = %ld\n", seed);
469 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
470 if (i >= 0 && geom.rtblocks)
471 rtpct = MIN(MAX(geom.rtblocks * 100 /
472 (geom.rtblocks + geom.datablocks), 1), 99);
481 for (i = 0; i < j; i++)
484 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
490 for (i = 0; i < j; i++)
493 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
495 printf("Injecting failure on tag #%d\n", errtag);
496 err_inj.errtag = errtag;
498 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
500 perror("fsstress - XFS_SYSSGI error injection call");
509 action.sa_handler = sg_handler;
510 sigemptyset(&action.sa_mask);
512 if (sigaction(SIGTERM, &action, 0)) {
513 perror("sigaction failed");
517 for (i = 0; i < nproc; i++) {
519 action.sa_handler = SIG_DFL;
520 sigemptyset(&action.sa_mask);
521 if (sigaction(SIGTERM, &action, 0))
523 #ifdef HAVE_SYS_PRCTL_H
524 prctl(PR_SET_PDEATHSIG, SIGKILL);
525 if (getppid() == 1) /* parent died already? */
530 snprintf(path, sizeof(path), "%s/%s.%d",
532 if (freopen(path, "a", stdout) == NULL) {
533 perror("freopen logfile failed");
542 while (wait(&stat) > 0 && !should_stop) {
545 action.sa_flags = SA_RESTART;
546 sigaction(SIGTERM, &action, 0);
547 kill(-getpid(), SIGTERM);
548 while (wait(&stat) > 0)
554 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
556 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
558 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
570 add_string(struct print_string *str, const char *add)
572 int len = strlen(add);
577 if (len > (str->max - 1) - str->len) {
578 str->len = str->max - 1;
582 memcpy(str->buffer + str->len, add, len);
584 str->buffer[str->len] = '\0';
590 translate_flags(int flags, const char *delim,
591 const struct print_flags *flag_array)
593 int i, mask, first = 1;
596 if (!flag_str.buffer) {
597 flag_str.buffer = malloc(4096);
601 if (!flag_str.buffer)
604 flag_str.buffer[0] = '\0';
606 for (i = 0; flag_array[i].name && flags; i++) {
607 mask = flag_array[i].mask;
608 if ((flags & mask) != mask)
611 add = flag_array[i].name;
614 add_string(&flag_str, delim);
617 add_string(&flag_str, add);
620 /* Check whether there are any leftover flags. */
626 add_string(&flag_str, delim);
628 ret = snprintf(number, 11, "0x%x", flags) > 0;
629 if (ret > 0 && ret <= 11)
630 add_string(&flag_str, number);
633 return flag_str.buffer;
637 add_to_flist(int ft, int id, int parent)
643 if (ftp->nfiles == ftp->nslots) {
644 ftp->nslots += FLIST_SLOT_INCR;
645 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
647 fep = &ftp->fents[ftp->nfiles++];
649 fep->parent = parent;
653 append_pathname(pathname_t *name, char *str)
659 /* attempting to append to a dir a zero length path */
660 if (len && *str == '/' && name->len == 0) {
661 fprintf(stderr, "fsstress: append_pathname failure\n");
667 name->path = realloc(name->path, name->len + 1 + len);
668 strcpy(&name->path[name->len], str);
673 attr_list_path(pathname_t *name,
675 const int buffersize,
677 attrlist_cursor_t *cursor)
679 char buf[NAME_MAX + 1];
683 if (flags != ATTR_DONTFOLLOW) {
688 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
689 if (rval >= 0 || errno != ENAMETOOLONG)
691 separate_pathname(name, buf, &newname);
692 if (chdir(buf) == 0) {
693 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
696 free_pathname(&newname);
701 attr_remove_path(pathname_t *name, const char *attrname, int flags)
703 char buf[NAME_MAX + 1];
707 rval = attr_remove(name->path, attrname, flags);
708 if (rval >= 0 || errno != ENAMETOOLONG)
710 separate_pathname(name, buf, &newname);
711 if (chdir(buf) == 0) {
712 rval = attr_remove_path(&newname, attrname, flags);
715 free_pathname(&newname);
720 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
721 const int valuelength, int flags)
723 char buf[NAME_MAX + 1];
727 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
728 if (rval >= 0 || errno != ENAMETOOLONG)
730 separate_pathname(name, buf, &newname);
731 if (chdir(buf) == 0) {
732 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
736 free_pathname(&newname);
744 struct stat64 statbuf;
746 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
749 fprintf(stderr, "fsstress: check_cwd failure\n");
756 creat_path(pathname_t *name, mode_t mode)
758 char buf[NAME_MAX + 1];
762 rval = creat(name->path, mode);
763 if (rval >= 0 || errno != ENAMETOOLONG)
765 separate_pathname(name, buf, &newname);
766 if (chdir(buf) == 0) {
767 rval = creat_path(&newname, mode);
770 free_pathname(&newname);
775 dcache_enter(int dirid, int slot)
777 dcache[dirid % NDCACHE] = slot;
785 for (i = 0; i < NDCACHE; i++)
790 dcache_lookup(int dirid)
795 i = dcache[dirid % NDCACHE];
796 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
802 dcache_purge(int dirid)
806 dcp = &dcache[dirid % NDCACHE];
807 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
812 * Delete the item from the list by
813 * moving last entry over the deleted one;
814 * unless deleted entry is the last one.
815 * Input: which file list array and which slot in array
818 del_from_flist(int ft, int slot)
824 dcache_purge(ftp->fents[slot].id);
825 if (slot != ftp->nfiles - 1) {
827 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
828 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
834 dirid_to_fent(int dirid)
840 if ((fep = dcache_lookup(dirid)))
842 flp = &flist[FT_DIR];
843 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
844 if (fep->id == dirid) {
845 dcache_enter(dirid, fep - flp->fents);
855 struct stat64 statbuf;
862 dividend = (operations + execute_freq) / (execute_freq + 1);
863 sprintf(buf, "p%x", procid);
864 (void)mkdir(buf, 0777);
865 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
869 top_ino = statbuf.st_ino;
870 homedir = getcwd(NULL, -1);
875 for (opno = 0; opno < operations; opno++) {
876 if (execute_cmd && opno && opno % dividend == 0) {
878 printf("%d: execute command %s\n", opno,
880 rval = system(execute_cmd);
882 fprintf(stderr, "execute command failed with "
885 p = &ops[freq_table[random() % freq_table_size]];
886 p->func(opno, random());
888 * test for forced shutdown by stat'ing the test
889 * directory. If this stat returns EIO, assume
890 * the forced shutdown happened.
892 if (errtag != 0 && opno % 100 == 0) {
893 rval = stat64(".", &statbuf);
895 fprintf(stderr, "Detected EIO\n");
903 * build up a pathname going thru the file entry and all
905 * Return 0 on error, 1 on success;
908 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
910 char buf[NAME_MAX + 1];
918 /* build up parent directory name */
919 if (fep->parent != -1) {
920 pfep = dirid_to_fent(fep->parent);
923 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
924 procid, fep->id, fep->parent);
929 e = fent_to_name(name, &flist[FT_DIR], pfep);
932 append_pathname(name, "/");
935 i = sprintf(buf, "%c%x", flp->tag, fep->id);
936 namerandpad(fep->id, buf, i);
937 append_pathname(name, buf);
942 fix_parent(int oldid, int newid)
949 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
950 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
951 if (fep->parent == oldid)
958 free_pathname(pathname_t *name)
968 * Generate a filename of type ft.
969 * If we have a fep which should be a directory then use it
970 * as the parent path for this new filename i.e. prepend with it.
973 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
975 char buf[NAME_MAX + 1];
984 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
985 namerandpad(id, buf, len);
987 /* prepend fep parent dir-name to it */
989 e = fent_to_name(name, &flist[FT_DIR], fep);
992 append_pathname(name, "/");
994 append_pathname(name, buf);
998 for (j = 0; !*v && j < ilistlen; j++) {
999 if (ilist[j] == id) {
1009 * Input: "which" to choose the file-types eg. non-directory
1010 * Input: "r" to choose which file
1011 * Output: file-list, file-entry, name for the chosen file.
1012 * Output: verbose if chosen file is on the ilist.
1015 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1018 int totalsum = 0; /* total number of matching files */
1019 int partialsum = 0; /* partial sum of matching files */
1025 int e = 1; /* success */
1028 * go thru flist and add up number of files for each
1029 * category that matches with <which>.
1031 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1032 if (which & (1 << i))
1033 totalsum += flp->nfiles;
1035 if (totalsum == 0) {
1045 * Now we have possible matches between 0..totalsum-1.
1046 * And we use r to help us choose which one we want,
1047 * which when bounded by totalsum becomes x.
1049 x = (int)(r % totalsum);
1050 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1051 if (which & (1 << i)) {
1052 if (x < partialsum + flp->nfiles) {
1054 /* found the matching file entry */
1055 fep = &flp->fents[x - partialsum];
1057 /* fill-in what we were asked for */
1059 e = fent_to_name(name, flp, fep);
1062 fprintf(stderr, "%d: failed to get path for entry:"
1063 " id=%d,parent=%d\n",
1064 procid, fep->id, fep->parent);
1073 /* turn on verbose if its an ilisted file */
1075 for (j = 0; !*v && j < ilistlen; j++) {
1076 if (ilist[j] == fep->id) {
1083 partialsum += flp->nfiles;
1087 fprintf(stderr, "fsstress: get_fname failure\n");
1094 init_pathname(pathname_t *name)
1101 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1103 char buf[NAME_MAX + 1];
1107 rval = lchown(name->path, owner, group);
1108 if (rval >= 0 || errno != ENAMETOOLONG)
1110 separate_pathname(name, buf, &newname);
1111 if (chdir(buf) == 0) {
1112 rval = lchown_path(&newname, owner, group);
1115 free_pathname(&newname);
1120 link_path(pathname_t *name1, pathname_t *name2)
1122 char buf1[NAME_MAX + 1];
1123 char buf2[NAME_MAX + 1];
1125 pathname_t newname1;
1126 pathname_t newname2;
1129 rval = link(name1->path, name2->path);
1130 if (rval >= 0 || errno != ENAMETOOLONG)
1132 separate_pathname(name1, buf1, &newname1);
1133 separate_pathname(name2, buf2, &newname2);
1134 if (strcmp(buf1, buf2) == 0) {
1135 if (chdir(buf1) == 0) {
1136 rval = link_path(&newname1, &newname2);
1140 if (strcmp(buf1, "..") == 0)
1142 else if (strcmp(buf2, "..") == 0)
1144 else if (strlen(buf1) == 0)
1146 else if (strlen(buf2) == 0)
1149 down1 = MAX(newname1.len, 3 + name2->len) <=
1150 MAX(3 + name1->len, newname2.len);
1152 free_pathname(&newname2);
1153 append_pathname(&newname2, "../");
1154 append_pathname(&newname2, name2->path);
1155 if (chdir(buf1) == 0) {
1156 rval = link_path(&newname1, &newname2);
1160 free_pathname(&newname1);
1161 append_pathname(&newname1, "../");
1162 append_pathname(&newname1, name1->path);
1163 if (chdir(buf2) == 0) {
1164 rval = link_path(&newname1, &newname2);
1169 free_pathname(&newname1);
1170 free_pathname(&newname2);
1175 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1177 char buf[NAME_MAX + 1];
1181 rval = lstat64(name->path, sbuf);
1182 if (rval >= 0 || errno != ENAMETOOLONG)
1184 separate_pathname(name, buf, &newname);
1185 if (chdir(buf) == 0) {
1186 rval = lstat64_path(&newname, sbuf);
1189 free_pathname(&newname);
1194 make_freq_table(void)
1200 for (p = ops, f = 0; p < ops_end; p++)
1202 freq_table = malloc(f * sizeof(*freq_table));
1203 freq_table_size = f;
1204 for (p = ops, i = 0; p < ops_end; p++) {
1205 for (f = 0; f < p->freq; f++, i++)
1206 freq_table[i] = p->op;
1211 mkdir_path(pathname_t *name, mode_t mode)
1213 char buf[NAME_MAX + 1];
1217 rval = mkdir(name->path, mode);
1218 if (rval >= 0 || errno != ENAMETOOLONG)
1220 separate_pathname(name, buf, &newname);
1221 if (chdir(buf) == 0) {
1222 rval = mkdir_path(&newname, mode);
1225 free_pathname(&newname);
1230 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1232 char buf[NAME_MAX + 1];
1236 rval = mknod(name->path, mode, dev);
1237 if (rval >= 0 || errno != ENAMETOOLONG)
1239 separate_pathname(name, buf, &newname);
1240 if (chdir(buf) == 0) {
1241 rval = mknod_path(&newname, mode, dev);
1244 free_pathname(&newname);
1249 namerandpad(int id, char *buf, int i)
1252 static int buckets[] =
1253 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1259 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1260 padmod = buckets[bucket] + 1 - i;
1263 padlen = (id ^ namerand) % padmod;
1265 memset(&buf[i], 'X', padlen);
1266 buf[i + padlen] = '\0';
1271 open_path(pathname_t *name, int oflag)
1273 char buf[NAME_MAX + 1];
1277 rval = open(name->path, oflag);
1278 if (rval >= 0 || errno != ENAMETOOLONG)
1280 separate_pathname(name, buf, &newname);
1281 if (chdir(buf) == 0) {
1282 rval = open_path(&newname, oflag);
1285 free_pathname(&newname);
1290 opendir_path(pathname_t *name)
1292 char buf[NAME_MAX + 1];
1296 rval = opendir(name->path);
1297 if (rval || errno != ENAMETOOLONG)
1299 separate_pathname(name, buf, &newname);
1300 if (chdir(buf) == 0) {
1301 rval = opendir_path(&newname);
1304 free_pathname(&newname);
1309 process_freq(char *arg)
1314 s = strchr(arg, '=');
1316 fprintf(stderr, "bad argument '%s'\n", arg);
1320 for (p = ops; p < ops_end; p++) {
1321 if (strcmp(arg, p->name) == 0) {
1326 fprintf(stderr, "can't find op type %s for -f\n", arg);
1331 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1333 char buf[NAME_MAX + 1];
1337 rval = readlink(name->path, lbuf, lbufsiz);
1338 if (rval >= 0 || errno != ENAMETOOLONG)
1340 separate_pathname(name, buf, &newname);
1341 if (chdir(buf) == 0) {
1342 rval = readlink_path(&newname, lbuf, lbufsiz);
1345 free_pathname(&newname);
1350 rename_path(pathname_t *name1, pathname_t *name2)
1352 char buf1[NAME_MAX + 1];
1353 char buf2[NAME_MAX + 1];
1355 pathname_t newname1;
1356 pathname_t newname2;
1359 rval = rename(name1->path, name2->path);
1360 if (rval >= 0 || errno != ENAMETOOLONG)
1362 separate_pathname(name1, buf1, &newname1);
1363 separate_pathname(name2, buf2, &newname2);
1364 if (strcmp(buf1, buf2) == 0) {
1365 if (chdir(buf1) == 0) {
1366 rval = rename_path(&newname1, &newname2);
1370 if (strcmp(buf1, "..") == 0)
1372 else if (strcmp(buf2, "..") == 0)
1374 else if (strlen(buf1) == 0)
1376 else if (strlen(buf2) == 0)
1379 down1 = MAX(newname1.len, 3 + name2->len) <=
1380 MAX(3 + name1->len, newname2.len);
1382 free_pathname(&newname2);
1383 append_pathname(&newname2, "../");
1384 append_pathname(&newname2, name2->path);
1385 if (chdir(buf1) == 0) {
1386 rval = rename_path(&newname1, &newname2);
1390 free_pathname(&newname1);
1391 append_pathname(&newname1, "../");
1392 append_pathname(&newname1, name1->path);
1393 if (chdir(buf2) == 0) {
1394 rval = rename_path(&newname1, &newname2);
1399 free_pathname(&newname1);
1400 free_pathname(&newname2);
1405 rmdir_path(pathname_t *name)
1407 char buf[NAME_MAX + 1];
1411 rval = rmdir(name->path);
1412 if (rval >= 0 || errno != ENAMETOOLONG)
1414 separate_pathname(name, buf, &newname);
1415 if (chdir(buf) == 0) {
1416 rval = rmdir_path(&newname);
1419 free_pathname(&newname);
1424 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1428 init_pathname(newname);
1429 slash = strchr(name->path, '/');
1430 if (slash == NULL) {
1435 strcpy(buf, name->path);
1437 append_pathname(newname, slash + 1);
1443 show_ops(int flag, char *lead_str)
1448 /* print in list form */
1451 for (p = ops; p < ops_end; p++) {
1452 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1453 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1454 x+=printf("%s ", p->name);
1457 } else if (flag == 0) {
1458 /* Table view style */
1460 for (f = 0, p = ops; p < ops_end; p++)
1466 for (p = ops; p < ops_end; p++) {
1467 if (flag != 0 || p->freq > 0) {
1468 if (lead_str != NULL)
1469 printf("%s", lead_str);
1470 printf("%20s %d/%d %s\n",
1471 p->name, p->freq, f,
1472 (p->iswrite == 0) ? " " : "write op");
1476 /* Command line style */
1477 if (lead_str != NULL)
1478 printf("%s", lead_str);
1479 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1481 for (p = ops; p < ops_end; p++)
1483 printf("-f %s=%d \\\n",p->name, p->freq);
1488 stat64_path(pathname_t *name, struct stat64 *sbuf)
1490 char buf[NAME_MAX + 1];
1494 rval = stat64(name->path, sbuf);
1495 if (rval >= 0 || errno != ENAMETOOLONG)
1497 separate_pathname(name, buf, &newname);
1498 if (chdir(buf) == 0) {
1499 rval = stat64_path(&newname, sbuf);
1502 free_pathname(&newname);
1507 symlink_path(const char *name1, pathname_t *name)
1509 char buf[NAME_MAX + 1];
1513 if (!strcmp(name1, name->path)) {
1514 printf("yikes! %s %s\n", name1, name->path);
1518 rval = symlink(name1, name->path);
1519 if (rval >= 0 || errno != ENAMETOOLONG)
1521 separate_pathname(name, buf, &newname);
1522 if (chdir(buf) == 0) {
1523 rval = symlink_path(name1, &newname);
1526 free_pathname(&newname);
1531 truncate64_path(pathname_t *name, off64_t length)
1533 char buf[NAME_MAX + 1];
1537 rval = truncate64(name->path, length);
1538 if (rval >= 0 || errno != ENAMETOOLONG)
1540 separate_pathname(name, buf, &newname);
1541 if (chdir(buf) == 0) {
1542 rval = truncate64_path(&newname, length);
1545 free_pathname(&newname);
1550 unlink_path(pathname_t *name)
1552 char buf[NAME_MAX + 1];
1556 rval = unlink(name->path);
1557 if (rval >= 0 || errno != ENAMETOOLONG)
1559 separate_pathname(name, buf, &newname);
1560 if (chdir(buf) == 0) {
1561 rval = unlink_path(&newname);
1564 free_pathname(&newname);
1571 printf("Usage: %s -H or\n", myprog);
1572 printf(" %s [-d dir][-e errtg][-f op_name=freq][-n nops]\n",
1574 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1576 printf(" -d dir specifies the base directory for operations\n");
1577 printf(" -e errtg specifies error injection stuff\n");
1578 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1579 printf(" the valid operation names are:\n");
1581 printf(" -i filenum get verbose output for this nth file object\n");
1582 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1583 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1584 printf(" -o logfile specifies logfile name\n");
1585 printf(" -p nproc specifies the no. of processes (default 1)\n");
1586 printf(" -r specifies random name padding\n");
1587 printf(" -s seed specifies the seed for the random generator (default random)\n");
1588 printf(" -v specifies verbose mode\n");
1589 printf(" -w zeros frequencies of non-write operations\n");
1590 printf(" -x cmd execute command in the middle of operations\n");
1591 printf(" -z zeros frequencies of all operations\n");
1592 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1593 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1594 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1595 printf(" -H prints usage and exits\n");
1603 for (p = ops; p < ops_end; p++) {
1614 for (p = ops; p < ops_end; p++)
1618 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1621 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1622 verifiable_log ? -1: (long)s->st_ino,
1623 (long)s->st_nlink, s->st_uid, s->st_gid,
1624 (long long) s->st_blocks, (long long) s->st_size);
1628 allocsp_f(int opno, long r)
1633 struct xfs_flock64 fl;
1641 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1643 printf("%d/%d: allocsp - no filename\n", procid, opno);
1647 fd = open_path(&f, O_RDWR);
1648 e = fd < 0 ? errno : 0;
1652 printf("%d/%d: allocsp - open %s failed %d\n",
1653 procid, opno, f.path, e);
1657 if (fstat64(fd, &stb) < 0) {
1659 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1660 procid, opno, f.path, errno);
1665 inode_info(st, sizeof(st), &stb, v);
1666 lr = ((__int64_t)random() << 32) + random();
1667 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1669 fl.l_whence = SEEK_SET;
1672 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1674 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1675 procid, opno, f.path, st, (long long)off, e);
1682 attr_remove_f(int opno, long r)
1684 attrlist_ent_t *aep;
1688 attrlist_cursor_t cursor;
1697 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1698 append_pathname(&f, ".");
1700 bzero(&cursor, sizeof(cursor));
1702 bzero(buf, sizeof(buf));
1703 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1707 alist = (attrlist_t *)buf;
1708 total += alist->al_count;
1709 } while (alist->al_more);
1712 printf("%d/%d: attr_remove - no attrs for %s\n",
1713 procid, opno, f.path);
1717 which = (int)(random() % total);
1718 bzero(&cursor, sizeof(cursor));
1722 bzero(buf, sizeof(buf));
1723 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1727 alist = (attrlist_t *)buf;
1728 if (which < ent + alist->al_count) {
1729 aep = (attrlist_ent_t *)
1730 &buf[alist->al_offset[which - ent]];
1731 aname = aep->a_name;
1734 ent += alist->al_count;
1735 } while (alist->al_more);
1736 if (aname == NULL) {
1739 "%d/%d: attr_remove - name %d not found at %s\n",
1740 procid, opno, which, f.path);
1744 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1747 printf("%d/%d: attr_remove %s %s %d\n",
1748 procid, opno, f.path, aname, e);
1753 attr_set_f(int opno, long r)
1760 static int lengths[] = { 10, 100, 1000, 10000 };
1765 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1766 append_pathname(&f, ".");
1767 sprintf(aname, "a%x", nameseq++);
1768 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1769 len = (int)(random() % lengths[li]);
1773 memset(aval, nameseq & 0xff, len);
1774 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1779 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1785 bulkstat_f(int opno, long r)
1793 xfs_fsop_bulkreq_t bsr;
1796 nent = (r % 999) + 2;
1797 t = malloc(nent * sizeof(*t));
1798 fd = open(".", O_RDONLY);
1806 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1810 printf("%d/%d: bulkstat nent %d total %lld\n",
1811 procid, opno, nent, (long long)total);
1816 bulkstat1_f(int opno, long r)
1826 xfs_fsop_bulkreq_t bsr;
1829 good = random() & 1;
1831 /* use an inode we know exists */
1833 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1834 append_pathname(&f, ".");
1835 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1840 * pick a random inode
1842 * note this can generate kernel warning messages
1843 * since bulkstat_one will read the disk block that
1844 * would contain a given inode even if that disk
1845 * block doesn't contain inodes.
1847 * this is detected later, but not until after the
1848 * warning is displayed.
1850 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1856 fd = open(".", O_RDONLY);
1862 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1864 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
1865 procid, opno, good?"real":"random",
1866 verifiable_log ? -1LL : (long long)ino, e);
1871 chown_f(int opno, long r)
1881 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1882 append_pathname(&f, ".");
1883 u = (uid_t)random();
1884 g = (gid_t)random();
1885 nbits = (int)(random() % idmodulo);
1886 u &= (1 << nbits) - 1;
1887 g &= (1 << nbits) - 1;
1888 e = lchown_path(&f, u, g) < 0 ? errno : 0;
1891 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
1896 setxattr_f(int opno, long r)
1898 #ifdef XFS_XFLAG_EXTSIZE
1908 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1909 append_pathname(&f, ".");
1910 fd = open_path(&f, O_RDWR);
1911 e = fd < 0 ? errno : 0;
1916 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
1917 nbits = (int)(random() % e);
1918 p &= (1 << nbits) - 1;
1920 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
1922 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
1925 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
1932 creat_f(int opno, long r)
1947 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1952 e1 = (random() % 100);
1953 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
1955 if (type == FT_RTF) /* rt always gets an extsize */
1956 extsize = (random() % 10) + 1;
1957 else if (e1 < 10) /* one-in-ten get an extsize */
1958 extsize = random() % 1024;
1962 e = generate_fname(fep, type, &f, &id, &v);
1966 (void)fent_to_name(&f, &flist[FT_DIR], fep);
1967 printf("%d/%d: creat - no filename from %s\n",
1968 procid, opno, f.path);
1973 fd = creat_path(&f, 0666);
1974 e = fd < 0 ? errno : 0;
1979 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
1980 if (type == FT_RTF) {
1981 a.fsx_xflags |= XFS_XFLAG_REALTIME;
1982 a.fsx_extsize = extsize *
1983 geom.rtextsize * geom.blocksize;
1985 } else if (extsize) {
1986 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
1987 a.fsx_extsize = extsize * geom.blocksize;
1990 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
1993 add_to_flist(type, id, parid);
1997 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
1998 extsize ? a.fsx_extsize : 0, e, e1);
1999 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2005 dread_f(int opno, long r)
2009 struct dioattr diob;
2022 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2024 printf("%d/%d: dread - no filename\n", procid, opno);
2028 fd = open_path(&f, O_RDONLY|O_DIRECT);
2029 e = fd < 0 ? errno : 0;
2033 printf("%d/%d: dread - open %s failed %d\n",
2034 procid, opno, f.path, e);
2038 if (fstat64(fd, &stb) < 0) {
2040 printf("%d/%d: dread - fstat64 %s failed %d\n",
2041 procid, opno, f.path, errno);
2046 inode_info(st, sizeof(st), &stb, v);
2047 if (stb.st_size == 0) {
2049 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2055 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2058 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s failed %d\n",
2059 procid, opno, f.path, st, errno);
2065 dio_env = getenv("XFS_DIO_MIN");
2067 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2069 align = (__int64_t)diob.d_miniosz;
2070 lr = ((__int64_t)random() << 32) + random();
2071 off = (off64_t)(lr % stb.st_size);
2072 off -= (off % align);
2073 lseek64(fd, off, SEEK_SET);
2074 len = (random() % FILELEN_MAX) + 1;
2075 len -= (len % align);
2078 else if (len > diob.d_maxiosz)
2079 len = diob.d_maxiosz;
2080 buf = memalign(diob.d_mem, len);
2081 e = read(fd, buf, len) < 0 ? errno : 0;
2084 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2085 procid, opno, f.path, st, (long long)off, (int)len, e);
2091 dwrite_f(int opno, long r)
2095 struct dioattr diob;
2108 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2110 printf("%d/%d: dwrite - no filename\n", procid, opno);
2114 fd = open_path(&f, O_WRONLY|O_DIRECT);
2115 e = fd < 0 ? errno : 0;
2119 printf("%d/%d: dwrite - open %s failed %d\n",
2120 procid, opno, f.path, e);
2124 if (fstat64(fd, &stb) < 0) {
2126 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2127 procid, opno, f.path, errno);
2132 inode_info(st, sizeof(st), &stb, v);
2133 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2135 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2136 " %s%s failed %d\n",
2137 procid, opno, f.path, st, errno);
2143 dio_env = getenv("XFS_DIO_MIN");
2145 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2147 align = (__int64_t)diob.d_miniosz;
2148 lr = ((__int64_t)random() << 32) + random();
2149 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2150 off -= (off % align);
2151 lseek64(fd, off, SEEK_SET);
2152 len = (random() % FILELEN_MAX) + 1;
2153 len -= (len % align);
2156 else if (len > diob.d_maxiosz)
2157 len = diob.d_maxiosz;
2158 buf = memalign(diob.d_mem, len);
2160 lseek64(fd, off, SEEK_SET);
2161 memset(buf, nameseq & 0xff, len);
2162 e = write(fd, buf, len) < 0 ? errno : 0;
2165 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2166 procid, opno, f.path, st, (long long)off, (int)len, e);
2172 #ifdef HAVE_LINUX_FALLOC_H
2173 struct print_flags falloc_flags [] = {
2174 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2175 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2176 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2177 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2178 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2179 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2183 #define translate_falloc_flags(mode) \
2184 ({translate_flags(mode, "|", falloc_flags);})
2188 do_fallocate(int opno, long r, int mode)
2190 #ifdef HAVE_LINUX_FALLOC_H
2202 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2204 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2208 fd = open_path(&f, O_RDWR);
2211 printf("%d/%d: do_fallocate - open %s failed %d\n",
2212 procid, opno, f.path, errno);
2217 if (fstat64(fd, &stb) < 0) {
2219 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2220 procid, opno, f.path, errno);
2225 inode_info(st, sizeof(st), &stb, v);
2226 lr = ((__int64_t)random() << 32) + random();
2227 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2229 len = (off64_t)(random() % (1024 * 1024));
2231 * Collapse range requires off and len to be block aligned, make it
2232 * more likely to be the case.
2234 if (FALLOC_FL_COLLAPSE_RANGE && (opno % 2)) {
2235 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2236 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2238 mode |= FALLOC_FL_KEEP_SIZE & random();
2239 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2241 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2242 procid, opno, translate_falloc_flags(mode),
2243 f.path, st, (long long)off, (long long)len, e);
2250 fallocate_f(int opno, long r)
2252 #ifdef HAVE_LINUX_FALLOC_H
2253 do_fallocate(opno, r, 0);
2258 fdatasync_f(int opno, long r)
2266 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2268 printf("%d/%d: fdatasync - no filename\n",
2273 fd = open_path(&f, O_WRONLY);
2274 e = fd < 0 ? errno : 0;
2278 printf("%d/%d: fdatasync - open %s failed %d\n",
2279 procid, opno, f.path, e);
2283 e = fdatasync(fd) < 0 ? errno : 0;
2285 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2290 #ifdef HAVE_LINUX_FIEMAP_H
2291 struct print_flags fiemap_flags[] = {
2292 { FIEMAP_FLAG_SYNC, "SYNC"},
2293 { FIEMAP_FLAG_XATTR, "XATTR"},
2297 #define translate_fiemap_flags(mode) \
2298 ({translate_flags(mode, "|", fiemap_flags);})
2302 fiemap_f(int opno, long r)
2304 #ifdef HAVE_LINUX_FIEMAP_H
2314 struct fiemap *fiemap;
2317 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2319 printf("%d/%d: fiemap - no filename\n", procid, opno);
2323 fd = open_path(&f, O_RDWR);
2324 e = fd < 0 ? errno : 0;
2328 printf("%d/%d: fiemap - open %s failed %d\n",
2329 procid, opno, f.path, e);
2333 if (fstat64(fd, &stb) < 0) {
2335 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
2336 procid, opno, f.path, errno);
2341 inode_info(st, sizeof(st), &stb, v);
2342 blocks_to_map = random() & 0xffff;
2343 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
2344 (blocks_to_map * sizeof(struct fiemap_extent)));
2347 printf("%d/%d: malloc failed \n", procid, opno);
2352 lr = ((__int64_t)random() << 32) + random();
2353 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2355 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
2356 fiemap->fm_extent_count = blocks_to_map;
2357 fiemap->fm_mapped_extents = random() & 0xffff;
2358 fiemap->fm_start = off;
2359 fiemap->fm_length = ((__int64_t)random() << 32) + random();
2361 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
2363 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
2364 procid, opno, f.path, st, (long long)fiemap->fm_start,
2365 (long long) fiemap->fm_length,
2366 translate_fiemap_flags(fiemap->fm_flags), e);
2374 freesp_f(int opno, long r)
2379 struct xfs_flock64 fl;
2387 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2389 printf("%d/%d: freesp - no filename\n", procid, opno);
2393 fd = open_path(&f, O_RDWR);
2394 e = fd < 0 ? errno : 0;
2398 printf("%d/%d: freesp - open %s failed %d\n",
2399 procid, opno, f.path, e);
2403 if (fstat64(fd, &stb) < 0) {
2405 printf("%d/%d: freesp - fstat64 %s failed %d\n",
2406 procid, opno, f.path, errno);
2411 inode_info(st, sizeof(st), &stb, v);
2412 lr = ((__int64_t)random() << 32) + random();
2413 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2415 fl.l_whence = SEEK_SET;
2418 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
2420 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
2421 procid, opno, f.path, st, (long long)off, e);
2427 fsync_f(int opno, long r)
2435 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2437 printf("%d/%d: fsync - no filename\n", procid, opno);
2441 fd = open_path(&f, O_WRONLY);
2442 e = fd < 0 ? errno : 0;
2446 printf("%d/%d: fsync - open %s failed %d\n",
2447 procid, opno, f.path, e);
2451 e = fsync(fd) < 0 ? errno : 0;
2453 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
2459 getattr_f(int opno, long r)
2468 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2469 append_pathname(&f, ".");
2470 fd = open_path(&f, O_RDWR);
2471 e = fd < 0 ? errno : 0;
2474 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
2476 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
2482 getdents_f(int opno, long r)
2489 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
2490 append_pathname(&f, ".");
2491 dir = opendir_path(&f);
2495 printf("%d/%d: getdents - can't open %s\n",
2496 procid, opno, f.path);
2500 while (readdir64(dir) != NULL)
2503 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
2509 link_f(int opno, long r)
2522 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
2524 printf("%d/%d: link - no file\n", procid, opno);
2528 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
2534 e = generate_fname(fep, flp - flist, &l, &id, &v1);
2538 (void)fent_to_name(&l, &flist[FT_DIR], fep);
2539 printf("%d/%d: link - no filename from %s\n",
2540 procid, opno, l.path);
2546 e = link_path(&f, &l) < 0 ? errno : 0;
2549 add_to_flist(flp - flist, id, parid);
2551 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
2553 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
2560 mkdir_f(int opno, long r)
2570 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2575 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
2579 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2580 printf("%d/%d: mkdir - no filename from %s\n",
2581 procid, opno, f.path);
2586 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
2589 add_to_flist(FT_DIR, id, parid);
2591 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
2592 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
2598 mknod_f(int opno, long r)
2608 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2613 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2617 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2618 printf("%d/%d: mknod - no filename from %s\n",
2619 procid, opno, f.path);
2624 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2627 add_to_flist(FT_DEV, id, parid);
2629 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2630 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
2636 punch_f(int opno, long r)
2638 #ifdef HAVE_LINUX_FALLOC_H
2639 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
2644 zero_f(int opno, long r)
2646 #ifdef HAVE_LINUX_FALLOC_H
2647 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
2652 collapse_f(int opno, long r)
2654 #ifdef HAVE_LINUX_FALLOC_H
2655 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
2660 read_f(int opno, long r)
2674 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2676 printf("%d/%d: read - no filename\n", procid, opno);
2680 fd = open_path(&f, O_RDONLY);
2681 e = fd < 0 ? errno : 0;
2685 printf("%d/%d: read - open %s failed %d\n",
2686 procid, opno, f.path, e);
2690 if (fstat64(fd, &stb) < 0) {
2692 printf("%d/%d: read - fstat64 %s failed %d\n",
2693 procid, opno, f.path, errno);
2698 inode_info(st, sizeof(st), &stb, v);
2699 if (stb.st_size == 0) {
2701 printf("%d/%d: read - %s%s zero size\n", procid, opno,
2707 lr = ((__int64_t)random() << 32) + random();
2708 off = (off64_t)(lr % stb.st_size);
2709 lseek64(fd, off, SEEK_SET);
2710 len = (random() % FILELEN_MAX) + 1;
2712 e = read(fd, buf, len) < 0 ? errno : 0;
2715 printf("%d/%d: read %s%s [%lld,%d] %d\n",
2716 procid, opno, f.path, st, (long long)off, (int)len, e);
2722 readlink_f(int opno, long r)
2730 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2732 printf("%d/%d: readlink - no filename\n", procid, opno);
2736 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2739 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2744 rename_f(int opno, long r)
2758 /* get an existing path for the source of the rename */
2760 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2762 printf("%d/%d: rename - no filename\n", procid, opno);
2767 /* get an existing directory for the destination parent directory name */
2768 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2774 /* generate a new path using an existing parent directory in name */
2775 init_pathname(&newf);
2776 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2780 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
2781 printf("%d/%d: rename - no filename from %s\n",
2782 procid, opno, f.path);
2784 free_pathname(&newf);
2788 e = rename_path(&f, &newf) < 0 ? errno : 0;
2791 if (flp - flist == FT_DIR) {
2793 fix_parent(oldid, id);
2795 del_from_flist(flp - flist, fep - flp->fents);
2796 add_to_flist(flp - flist, id, parid);
2799 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
2802 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
2803 procid, opno, fep->id, fep->parent);
2804 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
2805 procid, opno, id, parid);
2808 free_pathname(&newf);
2813 resvsp_f(int opno, long r)
2818 struct xfs_flock64 fl;
2826 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2828 printf("%d/%d: resvsp - no filename\n", procid, opno);
2832 fd = open_path(&f, O_RDWR);
2833 e = fd < 0 ? errno : 0;
2837 printf("%d/%d: resvsp - open %s failed %d\n",
2838 procid, opno, f.path, e);
2842 if (fstat64(fd, &stb) < 0) {
2844 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
2845 procid, opno, f.path, errno);
2850 inode_info(st, sizeof(st), &stb, v);
2851 lr = ((__int64_t)random() << 32) + random();
2852 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2854 fl.l_whence = SEEK_SET;
2856 fl.l_len = (off64_t)(random() % (1024 * 1024));
2857 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2859 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
2860 procid, opno, f.path, st,
2861 (long long)off, (long long)fl.l_len, e);
2867 rmdir_f(int opno, long r)
2875 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2877 printf("%d/%d: rmdir - no directory\n", procid, opno);
2881 e = rmdir_path(&f) < 0 ? errno : 0;
2884 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2886 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2888 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
2889 procid, opno, fep->id, fep->parent);
2895 setattr_f(int opno, long r)
2904 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2905 append_pathname(&f, ".");
2906 fd = open_path(&f, O_RDWR);
2907 e = fd < 0 ? errno : 0;
2910 fl = attr_mask & (uint)random();
2911 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
2913 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
2919 stat_f(int opno, long r)
2927 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2929 printf("%d/%d: stat - no entries\n", procid, opno);
2933 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2936 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2941 symlink_f(int opno, long r)
2954 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2959 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2963 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2964 printf("%d/%d: symlink - no filename from %s\n",
2965 procid, opno, f.path);
2970 len = (int)(random() % PATH_MAX);
2971 val = malloc(len + 1);
2973 memset(val, 'x', len);
2975 for (i = 10; i < len - 1; i += 10)
2977 e = symlink_path(val, &f) < 0 ? errno : 0;
2980 add_to_flist(FT_SYM, id, parid);
2983 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
2984 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
2991 sync_f(int opno, long r)
2995 printf("%d/%d: sync\n", procid, opno);
2999 truncate_f(int opno, long r)
3010 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3012 printf("%d/%d: truncate - no filename\n", procid, opno);
3016 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3020 printf("%d/%d: truncate - stat64 %s failed %d\n",
3021 procid, opno, f.path, e);
3025 inode_info(st, sizeof(st), &stb, v);
3026 lr = ((__int64_t)random() << 32) + random();
3027 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3029 e = truncate64_path(&f, off) < 0 ? errno : 0;
3032 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3033 st, (long long)off, e);
3038 unlink_f(int opno, long r)
3047 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3049 printf("%d/%d: unlink - no file\n", procid, opno);
3053 e = unlink_path(&f) < 0 ? errno : 0;
3056 del_from_flist(flp - flist, fep - flp->fents);
3058 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3060 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3061 procid, opno, fep->id, fep->parent);
3067 unresvsp_f(int opno, long r)
3072 struct xfs_flock64 fl;
3080 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3082 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3086 fd = open_path(&f, O_RDWR);
3087 e = fd < 0 ? errno : 0;
3091 printf("%d/%d: unresvsp - open %s failed %d\n",
3092 procid, opno, f.path, e);
3096 if (fstat64(fd, &stb) < 0) {
3098 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3099 procid, opno, f.path, errno);
3104 inode_info(st, sizeof(st), &stb, v);
3105 lr = ((__int64_t)random() << 32) + random();
3106 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3108 fl.l_whence = SEEK_SET;
3110 fl.l_len = (off64_t)(random() % (1 << 20));
3111 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
3113 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
3114 procid, opno, f.path, st,
3115 (long long)off, (long long)fl.l_len, e);
3121 write_f(int opno, long r)
3135 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
3137 printf("%d/%d: write - no filename\n", procid, opno);
3141 fd = open_path(&f, O_WRONLY);
3142 e = fd < 0 ? errno : 0;
3146 printf("%d/%d: write - open %s failed %d\n",
3147 procid, opno, f.path, e);
3151 if (fstat64(fd, &stb) < 0) {
3153 printf("%d/%d: write - fstat64 %s failed %d\n",
3154 procid, opno, f.path, errno);
3159 inode_info(st, sizeof(st), &stb, v);
3160 lr = ((__int64_t)random() << 32) + random();
3161 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3163 lseek64(fd, off, SEEK_SET);
3164 len = (random() % FILELEN_MAX) + 1;
3166 memset(buf, nameseq & 0xff, len);
3167 e = write(fd, buf, len) < 0 ? errno : 0;
3170 printf("%d/%d: write %s%s [%lld,%d] %d\n",
3171 procid, opno, f.path, st, (long long)off, (int)len, e);