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>
31 #include <linux/falloc.h>
32 #ifndef FALLOC_FL_PUNCH_HOLE
33 /* Copy-paste from linux/falloc.h */
34 #define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */
37 #ifndef HAVE_ATTR_LIST
38 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
40 #ifdef HAVE_SYS_PRCTL_H
41 #include <sys/prctl.h>
45 #ifndef FS_IOC_GETFLAGS
46 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
48 #ifndef FS_IOC_SETFLAGS
49 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
53 #define XFS_ERRTAG_MAX 17
54 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
55 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
57 #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 {
125 #define FT_DIRm (1 << FT_DIR)
127 #define FT_REGm (1 << FT_REG)
129 #define FT_SYMm (1 << FT_SYM)
131 #define FT_DEVm (1 << FT_DEV)
133 #define FT_RTFm (1 << FT_RTF)
135 #define FT_ANYm ((1 << FT_nft) - 1)
136 #define FT_REGFILE (FT_REGm | FT_RTFm)
137 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
139 #define FLIST_SLOT_INCR 16
142 #define MAXFSIZE ((1ULL << 63) - 1ULL)
143 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
145 void allocsp_f(int, long);
146 void attr_remove_f(int, long);
147 void attr_set_f(int, long);
148 void bulkstat_f(int, long);
149 void bulkstat1_f(int, long);
150 void chown_f(int, long);
151 void creat_f(int, long);
152 void dread_f(int, long);
153 void dwrite_f(int, long);
154 void fallocate_f(int, long);
155 void fdatasync_f(int, long);
156 void fiemap_f(int, long);
157 void freesp_f(int, long);
158 void fsync_f(int, long);
159 void getattr_f(int, long);
160 void getdents_f(int, long);
161 void link_f(int, long);
162 void mkdir_f(int, long);
163 void mknod_f(int, long);
164 void punch_f(int, long);
165 void read_f(int, long);
166 void readlink_f(int, long);
167 void rename_f(int, long);
168 void resvsp_f(int, long);
169 void rmdir_f(int, long);
170 void setattr_f(int, long);
171 void setxattr_f(int, long);
172 void stat_f(int, long);
173 void symlink_f(int, long);
174 void sync_f(int, long);
175 void truncate_f(int, long);
176 void unlink_f(int, long);
177 void unresvsp_f(int, long);
178 void write_f(int, long);
181 /* { OP_ENUM, "name", function, freq, iswrite }, */
182 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
183 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
184 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
185 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
186 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
187 { OP_CHOWN, "chown", chown_f, 3, 1 },
188 { OP_CREAT, "creat", creat_f, 4, 1 },
189 { OP_DREAD, "dread", dread_f, 4, 0 },
190 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
191 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
192 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
193 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
194 { OP_FREESP, "freesp", freesp_f, 1, 1 },
195 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
196 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
197 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
198 { OP_LINK, "link", link_f, 1, 1 },
199 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
200 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
201 { OP_PUNCH, "punch", punch_f, 1, 1 },
202 { OP_READ, "read", read_f, 1, 0 },
203 { OP_READLINK, "readlink", readlink_f, 1, 0 },
204 { OP_RENAME, "rename", rename_f, 2, 1 },
205 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
206 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
207 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
208 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
209 { OP_STAT, "stat", stat_f, 1, 0 },
210 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
211 { OP_SYNC, "sync", sync_f, 1, 1 },
212 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
213 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
214 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
215 { OP_WRITE, "write", write_f, 4, 1 },
218 flist_t flist[FT_nft] = {
231 xfs_fsop_geom_t geom;
242 unsigned int idmodulo = XFS_IDMODULO_MAX;
243 unsigned int attr_mask = ~0;
246 unsigned long seed = 0;
249 int verifiable_log = 0;
250 sig_atomic_t should_stop = 0;
251 char *execute_cmd = NULL;
252 int execute_freq = 1;
254 void add_to_flist(int, int, int);
255 void append_pathname(pathname_t *, char *);
256 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
257 int attr_remove_path(pathname_t *, const char *, int);
258 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
259 void check_cwd(void);
260 int creat_path(pathname_t *, mode_t);
261 void dcache_enter(int, int);
262 void dcache_init(void);
263 fent_t *dcache_lookup(int);
264 void dcache_purge(int);
265 void del_from_flist(int, int);
266 int dirid_to_name(char *, int);
268 int fent_to_name(pathname_t *, flist_t *, fent_t *);
269 void fix_parent(int, int);
270 void free_pathname(pathname_t *);
271 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
272 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
273 void init_pathname(pathname_t *);
274 int lchown_path(pathname_t *, uid_t, gid_t);
275 int link_path(pathname_t *, pathname_t *);
276 int lstat64_path(pathname_t *, struct stat64 *);
277 void make_freq_table(void);
278 int mkdir_path(pathname_t *, mode_t);
279 int mknod_path(pathname_t *, mode_t, dev_t);
280 void namerandpad(int, char *, int);
281 int open_path(pathname_t *, int);
282 DIR *opendir_path(pathname_t *);
283 void process_freq(char *);
284 int readlink_path(pathname_t *, char *, size_t);
285 int rename_path(pathname_t *, pathname_t *);
286 int rmdir_path(pathname_t *);
287 void separate_pathname(pathname_t *, char *, pathname_t *);
288 void show_ops(int, char *);
289 int stat64_path(pathname_t *, struct stat64 *);
290 int symlink_path(const char *, pathname_t *);
291 int truncate64_path(pathname_t *, off64_t);
292 int unlink_path(pathname_t *);
294 void write_freq(void);
295 void zero_freq(void);
297 void sg_handler(int signum)
302 int main(int argc, char **argv)
306 char *dirname = NULL;
307 char *logname = NULL;
308 char rpath[PATH_MAX];
317 xfs_error_injection_t err_inj;
318 struct sigaction action;
319 const char *allopts = "d:e:f:i:m:M:n:o:p:rs:S:vVwx:X:zH";
321 errrange = errtag = 0;
323 nops = sizeof(ops) / sizeof(ops[0]);
324 ops_end = &ops[nops];
326 while ((c = getopt(argc, argv, allopts)) != -1) {
332 sscanf(optarg, "%d", &errtag);
336 } else if (errtag == 0)
338 if (errtag >= XFS_ERRTAG_MAX) {
340 "error tag %d too large (max %d)\n",
341 errtag, XFS_ERRTAG_MAX - 1);
346 process_freq(optarg);
349 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
350 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
353 idmodulo = strtoul(optarg, NULL, 0);
354 if (idmodulo > XFS_IDMODULO_MAX) {
356 "chown modulo %d too big (max %d)\n",
357 idmodulo, XFS_IDMODULO_MAX);
362 operations = atoi(optarg);
369 nproc = atoi(optarg);
375 seed = strtoul(optarg, NULL, 0);
384 execute_cmd = optarg;
390 attr_mask = strtoul(optarg, NULL, 0);
394 if (optarg[0] == 'c')
405 execute_freq = strtoul(optarg, NULL, 0);
408 fprintf(stderr, "%s - invalid parameters\n",
418 /* no directory specified */
419 if (!nousage) usage();
423 (void)mkdir(dirname, 0777);
424 if (logname && logname[0] != '/') {
425 if (getcwd(rpath, sizeof(rpath)) < 0){
426 perror("getcwd failed");
432 if (chdir(dirname) < 0) {
438 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
439 if (freopen(path, "a", stdout) == NULL) {
440 perror("freopen logfile failed");
444 sprintf(buf, "fss%x", (unsigned int)getpid());
445 fd = creat(buf, 0666);
446 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
447 maxfsize = (off64_t)MAXFSIZE32;
449 maxfsize = (off64_t)MAXFSIZE;
454 gettimeofday(&t, (void *)NULL);
455 seed = (int)t.tv_sec ^ (int)t.tv_usec;
456 printf("seed = %ld\n", seed);
458 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
459 if (i >= 0 && geom.rtblocks)
460 rtpct = MIN(MAX(geom.rtblocks * 100 /
461 (geom.rtblocks + geom.datablocks), 1), 99);
470 for (i = 0; i < j; i++)
473 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
479 for (i = 0; i < j; i++)
482 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
484 printf("Injecting failure on tag #%d\n", errtag);
485 err_inj.errtag = errtag;
487 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
489 perror("fsstress - XFS_SYSSGI error injection call");
498 action.sa_handler = sg_handler;
499 sigemptyset(&action.sa_mask);
501 if (sigaction(SIGTERM, &action, 0)) {
502 perror("sigaction failed");
506 for (i = 0; i < nproc; i++) {
508 action.sa_handler = SIG_DFL;
509 sigemptyset(&action.sa_mask);
510 if (sigaction(SIGTERM, &action, 0))
512 #ifdef HAVE_SYS_PRCTL_H
513 prctl(PR_SET_PDEATHSIG, SIGKILL);
514 if (getppid() == 1) /* parent died already? */
519 snprintf(path, sizeof(path), "%s/%s.%d",
521 if (freopen(path, "a", stdout) == NULL) {
522 perror("freopen logfile failed");
531 while (wait(&stat) > 0 && !should_stop) {
534 action.sa_flags = SA_RESTART;
535 sigaction(SIGTERM, &action, 0);
536 kill(-getpid(), SIGTERM);
537 while (wait(&stat) > 0)
543 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
545 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
547 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
559 add_to_flist(int ft, int id, int parent)
565 if (ftp->nfiles == ftp->nslots) {
566 ftp->nslots += FLIST_SLOT_INCR;
567 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
569 fep = &ftp->fents[ftp->nfiles++];
571 fep->parent = parent;
575 append_pathname(pathname_t *name, char *str)
581 /* attempting to append to a dir a zero length path */
582 if (len && *str == '/' && name->len == 0) {
583 fprintf(stderr, "fsstress: append_pathname failure\n");
589 name->path = realloc(name->path, name->len + 1 + len);
590 strcpy(&name->path[name->len], str);
595 attr_list_path(pathname_t *name,
597 const int buffersize,
599 attrlist_cursor_t *cursor)
601 char buf[NAME_MAX + 1];
605 if (flags != ATTR_DONTFOLLOW) {
610 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
611 if (rval >= 0 || errno != ENAMETOOLONG)
613 separate_pathname(name, buf, &newname);
614 if (chdir(buf) == 0) {
615 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
618 free_pathname(&newname);
623 attr_remove_path(pathname_t *name, const char *attrname, int flags)
625 char buf[NAME_MAX + 1];
629 rval = attr_remove(name->path, attrname, flags);
630 if (rval >= 0 || errno != ENAMETOOLONG)
632 separate_pathname(name, buf, &newname);
633 if (chdir(buf) == 0) {
634 rval = attr_remove_path(&newname, attrname, flags);
637 free_pathname(&newname);
642 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
643 const int valuelength, int flags)
645 char buf[NAME_MAX + 1];
649 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
650 if (rval >= 0 || errno != ENAMETOOLONG)
652 separate_pathname(name, buf, &newname);
653 if (chdir(buf) == 0) {
654 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
658 free_pathname(&newname);
666 struct stat64 statbuf;
668 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
671 fprintf(stderr, "fsstress: check_cwd failure\n");
678 creat_path(pathname_t *name, mode_t mode)
680 char buf[NAME_MAX + 1];
684 rval = creat(name->path, mode);
685 if (rval >= 0 || errno != ENAMETOOLONG)
687 separate_pathname(name, buf, &newname);
688 if (chdir(buf) == 0) {
689 rval = creat_path(&newname, mode);
692 free_pathname(&newname);
697 dcache_enter(int dirid, int slot)
699 dcache[dirid % NDCACHE] = slot;
707 for (i = 0; i < NDCACHE; i++)
712 dcache_lookup(int dirid)
717 i = dcache[dirid % NDCACHE];
718 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
724 dcache_purge(int dirid)
728 dcp = &dcache[dirid % NDCACHE];
729 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
734 * Delete the item from the list by
735 * moving last entry over the deleted one;
736 * unless deleted entry is the last one.
737 * Input: which file list array and which slot in array
740 del_from_flist(int ft, int slot)
746 dcache_purge(ftp->fents[slot].id);
747 if (slot != ftp->nfiles - 1) {
749 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
750 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
756 dirid_to_fent(int dirid)
762 if ((fep = dcache_lookup(dirid)))
764 flp = &flist[FT_DIR];
765 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
766 if (fep->id == dirid) {
767 dcache_enter(dirid, fep - flp->fents);
777 struct stat64 statbuf;
784 dividend = (operations + execute_freq) / (execute_freq + 1);
785 sprintf(buf, "p%x", procid);
786 (void)mkdir(buf, 0777);
787 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
791 top_ino = statbuf.st_ino;
792 homedir = getcwd(NULL, -1);
797 for (opno = 0; opno < operations; opno++) {
798 if (execute_cmd && opno && opno % dividend == 0) {
800 printf("%d: execute command %s\n", opno,
802 rval = system(execute_cmd);
804 fprintf(stderr, "execute command failed with "
807 p = &ops[freq_table[random() % freq_table_size]];
808 p->func(opno, random());
810 * test for forced shutdown by stat'ing the test
811 * directory. If this stat returns EIO, assume
812 * the forced shutdown happened.
814 if (errtag != 0 && opno % 100 == 0) {
815 rval = stat64(".", &statbuf);
817 fprintf(stderr, "Detected EIO\n");
825 * build up a pathname going thru the file entry and all
827 * Return 0 on error, 1 on success;
830 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
832 char buf[NAME_MAX + 1];
840 /* build up parent directory name */
841 if (fep->parent != -1) {
842 pfep = dirid_to_fent(fep->parent);
845 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
846 procid, fep->id, fep->parent);
851 e = fent_to_name(name, &flist[FT_DIR], pfep);
854 append_pathname(name, "/");
857 i = sprintf(buf, "%c%x", flp->tag, fep->id);
858 namerandpad(fep->id, buf, i);
859 append_pathname(name, buf);
864 fix_parent(int oldid, int newid)
871 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
872 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
873 if (fep->parent == oldid)
880 free_pathname(pathname_t *name)
890 * Generate a filename of type ft.
891 * If we have a fep which should be a directory then use it
892 * as the parent path for this new filename i.e. prepend with it.
895 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
897 char buf[NAME_MAX + 1];
906 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
907 namerandpad(id, buf, len);
909 /* prepend fep parent dir-name to it */
911 e = fent_to_name(name, &flist[FT_DIR], fep);
914 append_pathname(name, "/");
916 append_pathname(name, buf);
920 for (j = 0; !*v && j < ilistlen; j++) {
921 if (ilist[j] == id) {
931 * Input: "which" to choose the file-types eg. non-directory
932 * Input: "r" to choose which file
933 * Output: file-list, file-entry, name for the chosen file.
934 * Output: verbose if chosen file is on the ilist.
937 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
940 int totalsum = 0; /* total number of matching files */
941 int partialsum = 0; /* partial sum of matching files */
947 int e = 1; /* success */
950 * go thru flist and add up number of files for each
951 * category that matches with <which>.
953 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
954 if (which & (1 << i))
955 totalsum += flp->nfiles;
967 * Now we have possible matches between 0..totalsum-1.
968 * And we use r to help us choose which one we want,
969 * which when bounded by totalsum becomes x.
971 x = (int)(r % totalsum);
972 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
973 if (which & (1 << i)) {
974 if (x < partialsum + flp->nfiles) {
976 /* found the matching file entry */
977 fep = &flp->fents[x - partialsum];
979 /* fill-in what we were asked for */
981 e = fent_to_name(name, flp, fep);
984 fprintf(stderr, "%d: failed to get path for entry:"
985 " id=%d,parent=%d\n",
986 procid, fep->id, fep->parent);
995 /* turn on verbose if its an ilisted file */
997 for (j = 0; !*v && j < ilistlen; j++) {
998 if (ilist[j] == fep->id) {
1005 partialsum += flp->nfiles;
1009 fprintf(stderr, "fsstress: get_fname failure\n");
1016 init_pathname(pathname_t *name)
1023 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1025 char buf[NAME_MAX + 1];
1029 rval = lchown(name->path, owner, group);
1030 if (rval >= 0 || errno != ENAMETOOLONG)
1032 separate_pathname(name, buf, &newname);
1033 if (chdir(buf) == 0) {
1034 rval = lchown_path(&newname, owner, group);
1037 free_pathname(&newname);
1042 link_path(pathname_t *name1, pathname_t *name2)
1044 char buf1[NAME_MAX + 1];
1045 char buf2[NAME_MAX + 1];
1047 pathname_t newname1;
1048 pathname_t newname2;
1051 rval = link(name1->path, name2->path);
1052 if (rval >= 0 || errno != ENAMETOOLONG)
1054 separate_pathname(name1, buf1, &newname1);
1055 separate_pathname(name2, buf2, &newname2);
1056 if (strcmp(buf1, buf2) == 0) {
1057 if (chdir(buf1) == 0) {
1058 rval = link_path(&newname1, &newname2);
1062 if (strcmp(buf1, "..") == 0)
1064 else if (strcmp(buf2, "..") == 0)
1066 else if (strlen(buf1) == 0)
1068 else if (strlen(buf2) == 0)
1071 down1 = MAX(newname1.len, 3 + name2->len) <=
1072 MAX(3 + name1->len, newname2.len);
1074 free_pathname(&newname2);
1075 append_pathname(&newname2, "../");
1076 append_pathname(&newname2, name2->path);
1077 if (chdir(buf1) == 0) {
1078 rval = link_path(&newname1, &newname2);
1082 free_pathname(&newname1);
1083 append_pathname(&newname1, "../");
1084 append_pathname(&newname1, name1->path);
1085 if (chdir(buf2) == 0) {
1086 rval = link_path(&newname1, &newname2);
1091 free_pathname(&newname1);
1092 free_pathname(&newname2);
1097 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1099 char buf[NAME_MAX + 1];
1103 rval = lstat64(name->path, sbuf);
1104 if (rval >= 0 || errno != ENAMETOOLONG)
1106 separate_pathname(name, buf, &newname);
1107 if (chdir(buf) == 0) {
1108 rval = lstat64_path(&newname, sbuf);
1111 free_pathname(&newname);
1116 make_freq_table(void)
1122 for (p = ops, f = 0; p < ops_end; p++)
1124 freq_table = malloc(f * sizeof(*freq_table));
1125 freq_table_size = f;
1126 for (p = ops, i = 0; p < ops_end; p++) {
1127 for (f = 0; f < p->freq; f++, i++)
1128 freq_table[i] = p->op;
1133 mkdir_path(pathname_t *name, mode_t mode)
1135 char buf[NAME_MAX + 1];
1139 rval = mkdir(name->path, mode);
1140 if (rval >= 0 || errno != ENAMETOOLONG)
1142 separate_pathname(name, buf, &newname);
1143 if (chdir(buf) == 0) {
1144 rval = mkdir_path(&newname, mode);
1147 free_pathname(&newname);
1152 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1154 char buf[NAME_MAX + 1];
1158 rval = mknod(name->path, mode, dev);
1159 if (rval >= 0 || errno != ENAMETOOLONG)
1161 separate_pathname(name, buf, &newname);
1162 if (chdir(buf) == 0) {
1163 rval = mknod_path(&newname, mode, dev);
1166 free_pathname(&newname);
1171 namerandpad(int id, char *buf, int i)
1174 static int buckets[] =
1175 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1181 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1182 padmod = buckets[bucket] + 1 - i;
1185 padlen = (id ^ namerand) % padmod;
1187 memset(&buf[i], 'X', padlen);
1188 buf[i + padlen] = '\0';
1193 open_path(pathname_t *name, int oflag)
1195 char buf[NAME_MAX + 1];
1199 rval = open(name->path, oflag);
1200 if (rval >= 0 || errno != ENAMETOOLONG)
1202 separate_pathname(name, buf, &newname);
1203 if (chdir(buf) == 0) {
1204 rval = open_path(&newname, oflag);
1207 free_pathname(&newname);
1212 opendir_path(pathname_t *name)
1214 char buf[NAME_MAX + 1];
1218 rval = opendir(name->path);
1219 if (rval || errno != ENAMETOOLONG)
1221 separate_pathname(name, buf, &newname);
1222 if (chdir(buf) == 0) {
1223 rval = opendir_path(&newname);
1226 free_pathname(&newname);
1231 process_freq(char *arg)
1236 s = strchr(arg, '=');
1238 fprintf(stderr, "bad argument '%s'\n", arg);
1242 for (p = ops; p < ops_end; p++) {
1243 if (strcmp(arg, p->name) == 0) {
1248 fprintf(stderr, "can't find op type %s for -f\n", arg);
1253 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1255 char buf[NAME_MAX + 1];
1259 rval = readlink(name->path, lbuf, lbufsiz);
1260 if (rval >= 0 || errno != ENAMETOOLONG)
1262 separate_pathname(name, buf, &newname);
1263 if (chdir(buf) == 0) {
1264 rval = readlink_path(&newname, lbuf, lbufsiz);
1267 free_pathname(&newname);
1272 rename_path(pathname_t *name1, pathname_t *name2)
1274 char buf1[NAME_MAX + 1];
1275 char buf2[NAME_MAX + 1];
1277 pathname_t newname1;
1278 pathname_t newname2;
1281 rval = rename(name1->path, name2->path);
1282 if (rval >= 0 || errno != ENAMETOOLONG)
1284 separate_pathname(name1, buf1, &newname1);
1285 separate_pathname(name2, buf2, &newname2);
1286 if (strcmp(buf1, buf2) == 0) {
1287 if (chdir(buf1) == 0) {
1288 rval = rename_path(&newname1, &newname2);
1292 if (strcmp(buf1, "..") == 0)
1294 else if (strcmp(buf2, "..") == 0)
1296 else if (strlen(buf1) == 0)
1298 else if (strlen(buf2) == 0)
1301 down1 = MAX(newname1.len, 3 + name2->len) <=
1302 MAX(3 + name1->len, newname2.len);
1304 free_pathname(&newname2);
1305 append_pathname(&newname2, "../");
1306 append_pathname(&newname2, name2->path);
1307 if (chdir(buf1) == 0) {
1308 rval = rename_path(&newname1, &newname2);
1312 free_pathname(&newname1);
1313 append_pathname(&newname1, "../");
1314 append_pathname(&newname1, name1->path);
1315 if (chdir(buf2) == 0) {
1316 rval = rename_path(&newname1, &newname2);
1321 free_pathname(&newname1);
1322 free_pathname(&newname2);
1327 rmdir_path(pathname_t *name)
1329 char buf[NAME_MAX + 1];
1333 rval = rmdir(name->path);
1334 if (rval >= 0 || errno != ENAMETOOLONG)
1336 separate_pathname(name, buf, &newname);
1337 if (chdir(buf) == 0) {
1338 rval = rmdir_path(&newname);
1341 free_pathname(&newname);
1346 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1350 init_pathname(newname);
1351 slash = strchr(name->path, '/');
1352 if (slash == NULL) {
1357 strcpy(buf, name->path);
1359 append_pathname(newname, slash + 1);
1365 show_ops(int flag, char *lead_str)
1370 /* print in list form */
1373 for (p = ops; p < ops_end; p++) {
1374 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1375 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1376 x+=printf("%s ", p->name);
1379 } else if (flag == 0) {
1380 /* Table view style */
1382 for (f = 0, p = ops; p < ops_end; p++)
1388 for (p = ops; p < ops_end; p++) {
1389 if (flag != 0 || p->freq > 0) {
1390 if (lead_str != NULL)
1391 printf("%s", lead_str);
1392 printf("%20s %d/%d %s\n",
1393 p->name, p->freq, f,
1394 (p->iswrite == 0) ? " " : "write op");
1398 /* Command line style */
1399 if (lead_str != NULL)
1400 printf("%s", lead_str);
1401 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1403 for (p = ops; p < ops_end; p++)
1405 printf("-f %s=%d \\\n",p->name, p->freq);
1410 stat64_path(pathname_t *name, struct stat64 *sbuf)
1412 char buf[NAME_MAX + 1];
1416 rval = stat64(name->path, sbuf);
1417 if (rval >= 0 || errno != ENAMETOOLONG)
1419 separate_pathname(name, buf, &newname);
1420 if (chdir(buf) == 0) {
1421 rval = stat64_path(&newname, sbuf);
1424 free_pathname(&newname);
1429 symlink_path(const char *name1, pathname_t *name)
1431 char buf[NAME_MAX + 1];
1435 if (!strcmp(name1, name->path)) {
1436 printf("yikes! %s %s\n", name1, name->path);
1440 rval = symlink(name1, name->path);
1441 if (rval >= 0 || errno != ENAMETOOLONG)
1443 separate_pathname(name, buf, &newname);
1444 if (chdir(buf) == 0) {
1445 rval = symlink_path(name1, &newname);
1448 free_pathname(&newname);
1453 truncate64_path(pathname_t *name, off64_t length)
1455 char buf[NAME_MAX + 1];
1459 rval = truncate64(name->path, length);
1460 if (rval >= 0 || errno != ENAMETOOLONG)
1462 separate_pathname(name, buf, &newname);
1463 if (chdir(buf) == 0) {
1464 rval = truncate64_path(&newname, length);
1467 free_pathname(&newname);
1472 unlink_path(pathname_t *name)
1474 char buf[NAME_MAX + 1];
1478 rval = unlink(name->path);
1479 if (rval >= 0 || errno != ENAMETOOLONG)
1481 separate_pathname(name, buf, &newname);
1482 if (chdir(buf) == 0) {
1483 rval = unlink_path(&newname);
1486 free_pathname(&newname);
1493 printf("Usage: %s -H or\n", myprog);
1494 printf(" %s [-d dir][-e errtg][-f op_name=freq][-n nops]\n",
1496 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1498 printf(" -d dir specifies the base directory for operations\n");
1499 printf(" -e errtg specifies error injection stuff\n");
1500 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1501 printf(" the valid operation names are:\n");
1503 printf(" -i filenum get verbose output for this nth file object\n");
1504 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1505 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1506 printf(" -o logfile specifies logfile name\n");
1507 printf(" -p nproc specifies the no. of processes (default 1)\n");
1508 printf(" -r specifies random name padding\n");
1509 printf(" -s seed specifies the seed for the random generator (default random)\n");
1510 printf(" -v specifies verbose mode\n");
1511 printf(" -w zeros frequencies of non-write operations\n");
1512 printf(" -x cmd execute command in the middle of operations\n");
1513 printf(" -z zeros frequencies of all operations\n");
1514 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1515 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1516 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1517 printf(" -H prints usage and exits\n");
1525 for (p = ops; p < ops_end; p++) {
1536 for (p = ops; p < ops_end; p++)
1540 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1543 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1544 verifiable_log ? -1: (long)s->st_ino,
1545 (long)s->st_nlink, s->st_uid, s->st_gid,
1546 (long long) s->st_blocks, (long long) s->st_size);
1550 allocsp_f(int opno, long r)
1555 struct xfs_flock64 fl;
1563 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1565 printf("%d/%d: allocsp - no filename\n", procid, opno);
1569 fd = open_path(&f, O_RDWR);
1570 e = fd < 0 ? errno : 0;
1574 printf("%d/%d: allocsp - open %s failed %d\n",
1575 procid, opno, f.path, e);
1579 if (fstat64(fd, &stb) < 0) {
1581 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1582 procid, opno, f.path, errno);
1587 inode_info(st, sizeof(st), &stb, v);
1588 lr = ((__int64_t)random() << 32) + random();
1589 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1591 fl.l_whence = SEEK_SET;
1594 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1596 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1597 procid, opno, f.path, st, (long long)off, e);
1604 attr_remove_f(int opno, long r)
1606 attrlist_ent_t *aep;
1610 attrlist_cursor_t cursor;
1619 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1620 append_pathname(&f, ".");
1622 bzero(&cursor, sizeof(cursor));
1624 bzero(buf, sizeof(buf));
1625 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1629 alist = (attrlist_t *)buf;
1630 total += alist->al_count;
1631 } while (alist->al_more);
1634 printf("%d/%d: attr_remove - no attrs for %s\n",
1635 procid, opno, f.path);
1639 which = (int)(random() % total);
1640 bzero(&cursor, sizeof(cursor));
1644 bzero(buf, sizeof(buf));
1645 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1649 alist = (attrlist_t *)buf;
1650 if (which < ent + alist->al_count) {
1651 aep = (attrlist_ent_t *)
1652 &buf[alist->al_offset[which - ent]];
1653 aname = aep->a_name;
1656 ent += alist->al_count;
1657 } while (alist->al_more);
1658 if (aname == NULL) {
1661 "%d/%d: attr_remove - name %d not found at %s\n",
1662 procid, opno, which, f.path);
1666 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1669 printf("%d/%d: attr_remove %s %s %d\n",
1670 procid, opno, f.path, aname, e);
1675 attr_set_f(int opno, long r)
1682 static int lengths[] = { 10, 100, 1000, 10000 };
1687 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1688 append_pathname(&f, ".");
1689 sprintf(aname, "a%x", nameseq++);
1690 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1691 len = (int)(random() % lengths[li]);
1695 memset(aval, nameseq & 0xff, len);
1696 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1701 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1707 bulkstat_f(int opno, long r)
1715 xfs_fsop_bulkreq_t bsr;
1718 nent = (r % 999) + 2;
1719 t = malloc(nent * sizeof(*t));
1720 fd = open(".", O_RDONLY);
1728 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1732 printf("%d/%d: bulkstat nent %d total %lld\n",
1733 procid, opno, nent, (long long)total);
1738 bulkstat1_f(int opno, long r)
1748 xfs_fsop_bulkreq_t bsr;
1751 good = random() & 1;
1753 /* use an inode we know exists */
1755 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1756 append_pathname(&f, ".");
1757 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1762 * pick a random inode
1764 * note this can generate kernel warning messages
1765 * since bulkstat_one will read the disk block that
1766 * would contain a given inode even if that disk
1767 * block doesn't contain inodes.
1769 * this is detected later, but not until after the
1770 * warning is displayed.
1772 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1778 fd = open(".", O_RDONLY);
1784 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1786 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
1787 procid, opno, good?"real":"random",
1788 verifiable_log ? -1LL : (long long)ino, e);
1793 chown_f(int opno, long r)
1803 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1804 append_pathname(&f, ".");
1805 u = (uid_t)random();
1806 g = (gid_t)random();
1807 nbits = (int)(random() % idmodulo);
1808 u &= (1 << nbits) - 1;
1809 g &= (1 << nbits) - 1;
1810 e = lchown_path(&f, u, g) < 0 ? errno : 0;
1813 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
1818 setxattr_f(int opno, long r)
1820 #ifdef XFS_XFLAG_EXTSIZE
1830 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1831 append_pathname(&f, ".");
1832 fd = open_path(&f, O_RDWR);
1833 e = fd < 0 ? errno : 0;
1838 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
1839 nbits = (int)(random() % e);
1840 p &= (1 << nbits) - 1;
1842 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
1844 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
1847 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
1854 creat_f(int opno, long r)
1869 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1874 e1 = (random() % 100);
1875 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
1877 if (type == FT_RTF) /* rt always gets an extsize */
1878 extsize = (random() % 10) + 1;
1879 else if (e1 < 10) /* one-in-ten get an extsize */
1880 extsize = random() % 1024;
1884 e = generate_fname(fep, type, &f, &id, &v);
1888 (void)fent_to_name(&f, &flist[FT_DIR], fep);
1889 printf("%d/%d: creat - no filename from %s\n",
1890 procid, opno, f.path);
1895 fd = creat_path(&f, 0666);
1896 e = fd < 0 ? errno : 0;
1901 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
1902 if (type == FT_RTF) {
1903 a.fsx_xflags |= XFS_XFLAG_REALTIME;
1904 a.fsx_extsize = extsize *
1905 geom.rtextsize * geom.blocksize;
1907 } else if (extsize) {
1908 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
1909 a.fsx_extsize = extsize * geom.blocksize;
1912 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
1915 add_to_flist(type, id, parid);
1919 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
1920 extsize ? a.fsx_extsize : 0, e, e1);
1921 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
1927 dread_f(int opno, long r)
1931 struct dioattr diob;
1944 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1946 printf("%d/%d: dread - no filename\n", procid, opno);
1950 fd = open_path(&f, O_RDONLY|O_DIRECT);
1951 e = fd < 0 ? errno : 0;
1955 printf("%d/%d: dread - open %s failed %d\n",
1956 procid, opno, f.path, e);
1960 if (fstat64(fd, &stb) < 0) {
1962 printf("%d/%d: dread - fstat64 %s failed %d\n",
1963 procid, opno, f.path, errno);
1968 inode_info(st, sizeof(st), &stb, v);
1969 if (stb.st_size == 0) {
1971 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
1977 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1980 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s failed %d\n",
1981 procid, opno, f.path, st, errno);
1987 dio_env = getenv("XFS_DIO_MIN");
1989 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1991 align = (__int64_t)diob.d_miniosz;
1992 lr = ((__int64_t)random() << 32) + random();
1993 off = (off64_t)(lr % stb.st_size);
1994 off -= (off % align);
1995 lseek64(fd, off, SEEK_SET);
1996 len = (random() % FILELEN_MAX) + 1;
1997 len -= (len % align);
2000 else if (len > diob.d_maxiosz)
2001 len = diob.d_maxiosz;
2002 buf = memalign(diob.d_mem, len);
2003 e = read(fd, buf, len) < 0 ? errno : 0;
2006 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2007 procid, opno, f.path, st, (long long)off, (int)len, e);
2013 dwrite_f(int opno, long r)
2017 struct dioattr diob;
2030 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2032 printf("%d/%d: dwrite - no filename\n", procid, opno);
2036 fd = open_path(&f, O_WRONLY|O_DIRECT);
2037 e = fd < 0 ? errno : 0;
2041 printf("%d/%d: dwrite - open %s failed %d\n",
2042 procid, opno, f.path, e);
2046 if (fstat64(fd, &stb) < 0) {
2048 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2049 procid, opno, f.path, errno);
2054 inode_info(st, sizeof(st), &stb, v);
2055 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2057 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2058 " %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 % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
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);
2082 lseek64(fd, off, SEEK_SET);
2083 memset(buf, nameseq & 0xff, len);
2084 e = write(fd, buf, len) < 0 ? errno : 0;
2087 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2088 procid, opno, f.path, st, (long long)off, (int)len, e);
2094 fallocate_f(int opno, long r)
2109 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2111 printf("%d/%d: fallocate - no filename\n", procid, opno);
2115 fd = open_path(&f, O_RDWR);
2116 e = fd < 0 ? errno : 0;
2120 printf("%d/%d: fallocate - open %s failed %d\n",
2121 procid, opno, f.path, e);
2125 if (fstat64(fd, &stb) < 0) {
2127 printf("%d/%d: fallocate - fstat64 %s failed %d\n",
2128 procid, opno, f.path, errno);
2133 inode_info(st, sizeof(st), &stb, v);
2134 lr = ((__int64_t)random() << 32) + random();
2135 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2137 len = (off64_t)(random() % (1024 * 1024));
2138 mode |= FALLOC_FL_KEEP_SIZE & random();
2139 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2141 printf("%d/%d: fallocate(%d) %s %st %lld %lld %d\n",
2143 f.path, st, (long long)off, (long long)len, e);
2151 fdatasync_f(int opno, long r)
2159 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2161 printf("%d/%d: fdatasync - no filename\n",
2166 fd = open_path(&f, O_WRONLY);
2167 e = fd < 0 ? errno : 0;
2171 printf("%d/%d: fdatasync - open %s failed %d\n",
2172 procid, opno, f.path, e);
2176 e = fdatasync(fd) < 0 ? errno : 0;
2178 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2183 fiemap_f(int opno, long r)
2185 #ifdef HAVE_LINUX_FIEMAP_H
2195 struct fiemap *fiemap;
2198 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2200 printf("%d/%d: fiemap - no filename\n", procid, opno);
2204 fd = open_path(&f, O_RDWR);
2205 e = fd < 0 ? errno : 0;
2209 printf("%d/%d: fiemap - open %s failed %d\n",
2210 procid, opno, f.path, e);
2214 if (fstat64(fd, &stb) < 0) {
2216 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
2217 procid, opno, f.path, errno);
2222 inode_info(st, sizeof(st), &stb, v);
2223 blocks_to_map = random() & 0xffff;
2224 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
2225 (blocks_to_map * sizeof(struct fiemap_extent)));
2228 printf("%d/%d: malloc failed \n", procid, opno);
2233 lr = ((__int64_t)random() << 32) + random();
2234 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2236 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
2237 fiemap->fm_extent_count = blocks_to_map;
2238 fiemap->fm_mapped_extents = random() & 0xffff;
2239 fiemap->fm_start = off;
2240 fiemap->fm_length = ((__int64_t)random() << 32) + random();
2242 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
2244 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld %x %d\n",
2245 procid, opno, f.path, st, (long long)fiemap->fm_start,
2246 (long long) fiemap->fm_length, fiemap->fm_flags, e);
2254 freesp_f(int opno, long r)
2259 struct xfs_flock64 fl;
2267 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2269 printf("%d/%d: freesp - no filename\n", procid, opno);
2273 fd = open_path(&f, O_RDWR);
2274 e = fd < 0 ? errno : 0;
2278 printf("%d/%d: freesp - open %s failed %d\n",
2279 procid, opno, f.path, e);
2283 if (fstat64(fd, &stb) < 0) {
2285 printf("%d/%d: freesp - fstat64 %s failed %d\n",
2286 procid, opno, f.path, errno);
2291 inode_info(st, sizeof(st), &stb, v);
2292 lr = ((__int64_t)random() << 32) + random();
2293 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2295 fl.l_whence = SEEK_SET;
2298 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
2300 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
2301 procid, opno, f.path, st, (long long)off, e);
2307 fsync_f(int opno, long r)
2315 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2317 printf("%d/%d: fsync - no filename\n", procid, opno);
2321 fd = open_path(&f, O_WRONLY);
2322 e = fd < 0 ? errno : 0;
2326 printf("%d/%d: fsync - open %s failed %d\n",
2327 procid, opno, f.path, e);
2331 e = fsync(fd) < 0 ? errno : 0;
2333 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
2339 getattr_f(int opno, long r)
2348 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2349 append_pathname(&f, ".");
2350 fd = open_path(&f, O_RDWR);
2351 e = fd < 0 ? errno : 0;
2354 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
2356 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
2362 getdents_f(int opno, long r)
2369 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
2370 append_pathname(&f, ".");
2371 dir = opendir_path(&f);
2375 printf("%d/%d: getdents - can't open %s\n",
2376 procid, opno, f.path);
2380 while (readdir64(dir) != NULL)
2383 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
2389 link_f(int opno, long r)
2402 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
2404 printf("%d/%d: link - no file\n", procid, opno);
2408 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
2414 e = generate_fname(fep, flp - flist, &l, &id, &v1);
2418 (void)fent_to_name(&l, &flist[FT_DIR], fep);
2419 printf("%d/%d: link - no filename from %s\n",
2420 procid, opno, l.path);
2426 e = link_path(&f, &l) < 0 ? errno : 0;
2429 add_to_flist(flp - flist, id, parid);
2431 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
2433 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
2440 mkdir_f(int opno, long r)
2450 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2455 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
2459 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2460 printf("%d/%d: mkdir - no filename from %s\n",
2461 procid, opno, f.path);
2466 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
2469 add_to_flist(FT_DIR, id, parid);
2471 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
2472 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
2478 mknod_f(int opno, long r)
2488 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2493 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2497 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2498 printf("%d/%d: mknod - no filename from %s\n",
2499 procid, opno, f.path);
2504 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2507 add_to_flist(FT_DEV, id, parid);
2509 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2510 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
2516 punch_f(int opno, long r)
2528 int mode = FALLOC_FL_PUNCH_HOLE;
2531 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2533 printf("%d/%d: punch hole - no filename\n", procid, opno);
2537 fd = open_path(&f, O_RDWR);
2538 e = fd < 0 ? errno : 0;
2542 printf("%d/%d: punch hole - open %s failed %d\n",
2543 procid, opno, f.path, e);
2547 if (fstat64(fd, &stb) < 0) {
2549 printf("%d/%d: punch hole - fstat64 %s failed %d\n",
2550 procid, opno, f.path, errno);
2555 inode_info(st, sizeof(st), &stb, v);
2556 lr = ((__int64_t)random() << 32) + random();
2557 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2559 len = (off64_t)(random() % (1024 * 1024));
2560 mode |= FALLOC_FL_KEEP_SIZE & random();
2561 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2563 printf("%d/%d: punch hole(%d) %s %s %lld %lld %d\n",
2565 f.path, st, (long long)off, (long long)len, e);
2572 read_f(int opno, long r)
2586 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2588 printf("%d/%d: read - no filename\n", procid, opno);
2592 fd = open_path(&f, O_RDONLY);
2593 e = fd < 0 ? errno : 0;
2597 printf("%d/%d: read - open %s failed %d\n",
2598 procid, opno, f.path, e);
2602 if (fstat64(fd, &stb) < 0) {
2604 printf("%d/%d: read - fstat64 %s failed %d\n",
2605 procid, opno, f.path, errno);
2610 inode_info(st, sizeof(st), &stb, v);
2611 if (stb.st_size == 0) {
2613 printf("%d/%d: read - %s%s zero size\n", procid, opno,
2619 lr = ((__int64_t)random() << 32) + random();
2620 off = (off64_t)(lr % stb.st_size);
2621 lseek64(fd, off, SEEK_SET);
2622 len = (random() % FILELEN_MAX) + 1;
2624 e = read(fd, buf, len) < 0 ? errno : 0;
2627 printf("%d/%d: read %s%s [%lld,%d] %d\n",
2628 procid, opno, f.path, st, (long long)off, (int)len, e);
2634 readlink_f(int opno, long r)
2642 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2644 printf("%d/%d: readlink - no filename\n", procid, opno);
2648 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2651 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2656 rename_f(int opno, long r)
2670 /* get an existing path for the source of the rename */
2672 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2674 printf("%d/%d: rename - no filename\n", procid, opno);
2679 /* get an existing directory for the destination parent directory name */
2680 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2686 /* generate a new path using an existing parent directory in name */
2687 init_pathname(&newf);
2688 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2692 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
2693 printf("%d/%d: rename - no filename from %s\n",
2694 procid, opno, f.path);
2696 free_pathname(&newf);
2700 e = rename_path(&f, &newf) < 0 ? errno : 0;
2703 if (flp - flist == FT_DIR) {
2705 fix_parent(oldid, id);
2707 del_from_flist(flp - flist, fep - flp->fents);
2708 add_to_flist(flp - flist, id, parid);
2711 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
2714 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
2715 procid, opno, fep->id, fep->parent);
2716 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
2717 procid, opno, id, parid);
2720 free_pathname(&newf);
2725 resvsp_f(int opno, long r)
2730 struct xfs_flock64 fl;
2738 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2740 printf("%d/%d: resvsp - no filename\n", procid, opno);
2744 fd = open_path(&f, O_RDWR);
2745 e = fd < 0 ? errno : 0;
2749 printf("%d/%d: resvsp - open %s failed %d\n",
2750 procid, opno, f.path, e);
2754 if (fstat64(fd, &stb) < 0) {
2756 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
2757 procid, opno, f.path, errno);
2762 inode_info(st, sizeof(st), &stb, v);
2763 lr = ((__int64_t)random() << 32) + random();
2764 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2766 fl.l_whence = SEEK_SET;
2768 fl.l_len = (off64_t)(random() % (1024 * 1024));
2769 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2771 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
2772 procid, opno, f.path, st,
2773 (long long)off, (long long)fl.l_len, e);
2779 rmdir_f(int opno, long r)
2787 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2789 printf("%d/%d: rmdir - no directory\n", procid, opno);
2793 e = rmdir_path(&f) < 0 ? errno : 0;
2796 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2798 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2800 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
2801 procid, opno, fep->id, fep->parent);
2807 setattr_f(int opno, long r)
2816 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2817 append_pathname(&f, ".");
2818 fd = open_path(&f, O_RDWR);
2819 e = fd < 0 ? errno : 0;
2822 fl = attr_mask & (uint)random();
2823 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
2825 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
2831 stat_f(int opno, long r)
2839 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2841 printf("%d/%d: stat - no entries\n", procid, opno);
2845 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2848 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2853 symlink_f(int opno, long r)
2866 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2871 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2875 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2876 printf("%d/%d: symlink - no filename from %s\n",
2877 procid, opno, f.path);
2882 len = (int)(random() % PATH_MAX);
2883 val = malloc(len + 1);
2885 memset(val, 'x', len);
2887 for (i = 10; i < len - 1; i += 10)
2889 e = symlink_path(val, &f) < 0 ? errno : 0;
2892 add_to_flist(FT_SYM, id, parid);
2895 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
2896 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
2903 sync_f(int opno, long r)
2907 printf("%d/%d: sync\n", procid, opno);
2911 truncate_f(int opno, long r)
2922 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2924 printf("%d/%d: truncate - no filename\n", procid, opno);
2928 e = stat64_path(&f, &stb) < 0 ? errno : 0;
2932 printf("%d/%d: truncate - stat64 %s failed %d\n",
2933 procid, opno, f.path, e);
2937 inode_info(st, sizeof(st), &stb, v);
2938 lr = ((__int64_t)random() << 32) + random();
2939 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2941 e = truncate64_path(&f, off) < 0 ? errno : 0;
2944 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
2945 st, (long long)off, e);
2950 unlink_f(int opno, long r)
2959 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
2961 printf("%d/%d: unlink - no file\n", procid, opno);
2965 e = unlink_path(&f) < 0 ? errno : 0;
2968 del_from_flist(flp - flist, fep - flp->fents);
2970 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
2972 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
2973 procid, opno, fep->id, fep->parent);
2979 unresvsp_f(int opno, long r)
2984 struct xfs_flock64 fl;
2992 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2994 printf("%d/%d: unresvsp - no filename\n", procid, opno);
2998 fd = open_path(&f, O_RDWR);
2999 e = fd < 0 ? errno : 0;
3003 printf("%d/%d: unresvsp - open %s failed %d\n",
3004 procid, opno, f.path, e);
3008 if (fstat64(fd, &stb) < 0) {
3010 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3011 procid, opno, f.path, errno);
3016 inode_info(st, sizeof(st), &stb, v);
3017 lr = ((__int64_t)random() << 32) + random();
3018 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3020 fl.l_whence = SEEK_SET;
3022 fl.l_len = (off64_t)(random() % (1 << 20));
3023 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
3025 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
3026 procid, opno, f.path, st,
3027 (long long)off, (long long)fl.l_len, e);
3033 write_f(int opno, long r)
3047 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
3049 printf("%d/%d: write - no filename\n", procid, opno);
3053 fd = open_path(&f, O_WRONLY);
3054 e = fd < 0 ? errno : 0;
3058 printf("%d/%d: write - open %s failed %d\n",
3059 procid, opno, f.path, e);
3063 if (fstat64(fd, &stb) < 0) {
3065 printf("%d/%d: write - fstat64 %s failed %d\n",
3066 procid, opno, f.path, errno);
3071 inode_info(st, sizeof(st), &stb, v);
3072 lr = ((__int64_t)random() << 32) + random();
3073 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3075 lseek64(fd, off, SEEK_SET);
3076 len = (random() % FILELEN_MAX) + 1;
3078 memset(buf, nameseq & 0xff, len);
3079 e = write(fd, buf, len) < 0 ? errno : 0;
3082 printf("%d/%d: write %s%s [%lld,%d] %d\n",
3083 procid, opno, f.path, st, (long long)off, (int)len, e);