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
23 #ifdef HAVE_ATTR_XATTR_H
24 #include <attr/xattr.h>
26 #ifdef HAVE_ATTR_ATTRIBUTES_H
27 #include <attr/attributes.h>
29 #ifdef HAVE_LINUX_FIEMAP_H
30 #include <linux/fiemap.h>
32 #ifndef HAVE_ATTR_LIST
33 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
35 #ifdef HAVE_SYS_PRCTL_H
36 #include <sys/prctl.h>
39 #ifndef FS_IOC_GETFLAGS
40 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
42 #ifndef FS_IOC_SETFLAGS
43 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
47 #define XFS_ERRTAG_MAX 17
48 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
49 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
51 #define FILELEN_MAX (32*4096)
96 typedef void (*opfnc_t)(int, long);
98 typedef struct opdesc {
106 typedef struct fent {
111 typedef struct flist {
118 typedef struct pathname {
128 struct print_string {
135 #define FT_DIRm (1 << FT_DIR)
137 #define FT_REGm (1 << FT_REG)
139 #define FT_SYMm (1 << FT_SYM)
141 #define FT_DEVm (1 << FT_DEV)
143 #define FT_RTFm (1 << FT_RTF)
145 #define FT_ANYm ((1 << FT_nft) - 1)
146 #define FT_REGFILE (FT_REGm | FT_RTFm)
147 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
149 #define FLIST_SLOT_INCR 16
152 #define MAXFSIZE ((1ULL << 63) - 1ULL)
153 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
155 void allocsp_f(int, long);
156 void attr_remove_f(int, long);
157 void attr_set_f(int, long);
158 void bulkstat_f(int, long);
159 void bulkstat1_f(int, long);
160 void chown_f(int, long);
161 void creat_f(int, long);
162 void dread_f(int, long);
163 void dwrite_f(int, long);
164 void fallocate_f(int, long);
165 void fdatasync_f(int, long);
166 void fiemap_f(int, long);
167 void freesp_f(int, long);
168 void fsync_f(int, long);
169 void getattr_f(int, long);
170 void getdents_f(int, long);
171 void link_f(int, long);
172 void mkdir_f(int, long);
173 void mknod_f(int, long);
174 void mread_f(int, long);
175 void mwrite_f(int, long);
176 void punch_f(int, long);
177 void zero_f(int, long);
178 void collapse_f(int, long);
179 void insert_f(int, long);
180 void read_f(int, long);
181 void readlink_f(int, long);
182 void rename_f(int, long);
183 void resvsp_f(int, long);
184 void rmdir_f(int, long);
185 void setattr_f(int, long);
186 void setxattr_f(int, long);
187 void stat_f(int, long);
188 void symlink_f(int, long);
189 void sync_f(int, long);
190 void truncate_f(int, long);
191 void unlink_f(int, long);
192 void unresvsp_f(int, long);
193 void write_f(int, long);
196 /* { OP_ENUM, "name", function, freq, iswrite }, */
197 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
198 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
199 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
200 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
201 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
202 { OP_CHOWN, "chown", chown_f, 3, 1 },
203 { OP_CREAT, "creat", creat_f, 4, 1 },
204 { OP_DREAD, "dread", dread_f, 4, 0 },
205 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
206 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
207 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
208 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
209 { OP_FREESP, "freesp", freesp_f, 1, 1 },
210 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
211 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
212 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
213 { OP_LINK, "link", link_f, 1, 1 },
214 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
215 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
216 { OP_MREAD, "mread", mread_f, 2, 0 },
217 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
218 { OP_PUNCH, "punch", punch_f, 1, 1 },
219 { OP_ZERO, "zero", zero_f, 1, 1 },
220 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
221 { OP_INSERT, "insert", insert_f, 1, 1 },
222 { OP_READ, "read", read_f, 1, 0 },
223 { OP_READLINK, "readlink", readlink_f, 1, 0 },
224 { OP_RENAME, "rename", rename_f, 2, 1 },
225 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
226 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
227 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
228 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
229 { OP_STAT, "stat", stat_f, 1, 0 },
230 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
231 { OP_SYNC, "sync", sync_f, 1, 1 },
232 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
233 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
234 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
235 { OP_WRITE, "write", write_f, 4, 1 },
238 flist_t flist[FT_nft] = {
251 xfs_fsop_geom_t geom;
262 unsigned int idmodulo = XFS_IDMODULO_MAX;
263 unsigned int attr_mask = ~0;
266 unsigned long seed = 0;
270 int verifiable_log = 0;
271 sig_atomic_t should_stop = 0;
272 sigjmp_buf *sigbus_jmp = NULL;
273 char *execute_cmd = NULL;
274 int execute_freq = 1;
275 struct print_string flag_str = {0};
277 void add_to_flist(int, int, int);
278 void append_pathname(pathname_t *, char *);
279 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
280 int attr_remove_path(pathname_t *, const char *, int);
281 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
282 void check_cwd(void);
283 void cleanup_flist(void);
284 int creat_path(pathname_t *, mode_t);
285 void dcache_enter(int, int);
286 void dcache_init(void);
287 fent_t *dcache_lookup(int);
288 void dcache_purge(int);
289 void del_from_flist(int, int);
290 int dirid_to_name(char *, int);
292 int fent_to_name(pathname_t *, flist_t *, fent_t *);
293 void fix_parent(int, int);
294 void free_pathname(pathname_t *);
295 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
296 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
297 void init_pathname(pathname_t *);
298 int lchown_path(pathname_t *, uid_t, gid_t);
299 int link_path(pathname_t *, pathname_t *);
300 int lstat64_path(pathname_t *, struct stat64 *);
301 void make_freq_table(void);
302 int mkdir_path(pathname_t *, mode_t);
303 int mknod_path(pathname_t *, mode_t, dev_t);
304 void namerandpad(int, char *, int);
305 int open_path(pathname_t *, int);
306 DIR *opendir_path(pathname_t *);
307 void process_freq(char *);
308 int readlink_path(pathname_t *, char *, size_t);
309 int rename_path(pathname_t *, pathname_t *);
310 int rmdir_path(pathname_t *);
311 void separate_pathname(pathname_t *, char *, pathname_t *);
312 void show_ops(int, char *);
313 int stat64_path(pathname_t *, struct stat64 *);
314 int symlink_path(const char *, pathname_t *);
315 int truncate64_path(pathname_t *, off64_t);
316 int unlink_path(pathname_t *);
318 void write_freq(void);
319 void zero_freq(void);
321 void sg_handler(int signum)
329 * Only handle SIGBUS when mmap write to a hole and no
330 * block can be allocated due to ENOSPC, abort otherwise.
333 siglongjmp(*sigbus_jmp, -1);
335 printf("Unknown SIGBUS is caught, Abort!\n");
338 /* should not reach here */
345 int main(int argc, char **argv)
349 char *dirname = NULL;
350 char *logname = NULL;
351 char rpath[PATH_MAX];
360 xfs_error_injection_t err_inj;
361 struct sigaction action;
363 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
365 errrange = errtag = 0;
367 nops = sizeof(ops) / sizeof(ops[0]);
368 ops_end = &ops[nops];
370 while ((c = getopt(argc, argv, allopts)) != -1) {
379 sscanf(optarg, "%d", &errtag);
383 } else if (errtag == 0)
385 if (errtag >= XFS_ERRTAG_MAX) {
387 "error tag %d too large (max %d)\n",
388 errtag, XFS_ERRTAG_MAX - 1);
393 process_freq(optarg);
396 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
397 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
400 idmodulo = strtoul(optarg, NULL, 0);
401 if (idmodulo > XFS_IDMODULO_MAX) {
403 "chown modulo %d too big (max %d)\n",
404 idmodulo, XFS_IDMODULO_MAX);
409 loops = atoi(optarg);
412 operations = atoi(optarg);
419 nproc = atoi(optarg);
425 seed = strtoul(optarg, NULL, 0);
434 execute_cmd = optarg;
440 attr_mask = strtoul(optarg, NULL, 0);
444 if (optarg[0] == 'c')
455 execute_freq = strtoul(optarg, NULL, 0);
458 fprintf(stderr, "%s - invalid parameters\n",
468 /* no directory specified */
469 if (!nousage) usage();
473 (void)mkdir(dirname, 0777);
474 if (logname && logname[0] != '/') {
475 if (getcwd(rpath, sizeof(rpath)) < 0){
476 perror("getcwd failed");
482 if (chdir(dirname) < 0) {
488 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
489 if (freopen(path, "a", stdout) == NULL) {
490 perror("freopen logfile failed");
494 sprintf(buf, "fss%x", (unsigned int)getpid());
495 fd = creat(buf, 0666);
496 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
497 maxfsize = (off64_t)MAXFSIZE32;
499 maxfsize = (off64_t)MAXFSIZE;
504 gettimeofday(&t, (void *)NULL);
505 seed = (int)t.tv_sec ^ (int)t.tv_usec;
506 printf("seed = %ld\n", seed);
508 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
509 if (i >= 0 && geom.rtblocks)
510 rtpct = MIN(MAX(geom.rtblocks * 100 /
511 (geom.rtblocks + geom.datablocks), 1), 99);
520 for (i = 0; i < j; i++)
523 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
529 for (i = 0; i < j; i++)
532 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
534 printf("Injecting failure on tag #%d\n", errtag);
535 err_inj.errtag = errtag;
537 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
539 perror("fsstress - XFS_SYSSGI error injection call");
548 action.sa_handler = sg_handler;
549 sigemptyset(&action.sa_mask);
551 if (sigaction(SIGTERM, &action, 0)) {
552 perror("sigaction failed");
556 for (i = 0; i < nproc; i++) {
558 sigemptyset(&action.sa_mask);
559 action.sa_handler = SIG_DFL;
560 if (sigaction(SIGTERM, &action, 0))
562 action.sa_handler = sg_handler;
563 if (sigaction(SIGBUS, &action, 0))
565 #ifdef HAVE_SYS_PRCTL_H
566 prctl(PR_SET_PDEATHSIG, SIGKILL);
567 if (getppid() == 1) /* parent died already? */
572 snprintf(path, sizeof(path), "%s/%s.%d",
574 if (freopen(path, "a", stdout) == NULL) {
575 perror("freopen logfile failed");
580 for (i = 0; !loops || (i < loops); i++)
585 while (wait(&stat) > 0 && !should_stop) {
588 action.sa_flags = SA_RESTART;
589 sigaction(SIGTERM, &action, 0);
590 kill(-getpid(), SIGTERM);
591 while (wait(&stat) > 0)
597 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
599 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
601 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
613 add_string(struct print_string *str, const char *add)
615 int len = strlen(add);
620 if (len > (str->max - 1) - str->len) {
621 str->len = str->max - 1;
625 memcpy(str->buffer + str->len, add, len);
627 str->buffer[str->len] = '\0';
633 translate_flags(int flags, const char *delim,
634 const struct print_flags *flag_array)
636 int i, mask, first = 1;
639 if (!flag_str.buffer) {
640 flag_str.buffer = malloc(4096);
644 if (!flag_str.buffer)
647 flag_str.buffer[0] = '\0';
649 for (i = 0; flag_array[i].name && flags; i++) {
650 mask = flag_array[i].mask;
651 if ((flags & mask) != mask)
654 add = flag_array[i].name;
657 add_string(&flag_str, delim);
660 add_string(&flag_str, add);
663 /* Check whether there are any leftover flags. */
669 add_string(&flag_str, delim);
671 ret = snprintf(number, 11, "0x%x", flags) > 0;
672 if (ret > 0 && ret <= 11)
673 add_string(&flag_str, number);
676 return flag_str.buffer;
680 add_to_flist(int ft, int id, int parent)
686 if (ftp->nfiles == ftp->nslots) {
687 ftp->nslots += FLIST_SLOT_INCR;
688 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
690 fep = &ftp->fents[ftp->nfiles++];
692 fep->parent = parent;
696 append_pathname(pathname_t *name, char *str)
702 /* attempting to append to a dir a zero length path */
703 if (len && *str == '/' && name->len == 0) {
704 fprintf(stderr, "fsstress: append_pathname failure\n");
710 name->path = realloc(name->path, name->len + 1 + len);
711 strcpy(&name->path[name->len], str);
716 attr_list_path(pathname_t *name,
718 const int buffersize,
720 attrlist_cursor_t *cursor)
722 char buf[NAME_MAX + 1];
726 if (flags != ATTR_DONTFOLLOW) {
731 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
732 if (rval >= 0 || errno != ENAMETOOLONG)
734 separate_pathname(name, buf, &newname);
735 if (chdir(buf) == 0) {
736 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
739 free_pathname(&newname);
744 attr_remove_path(pathname_t *name, const char *attrname, int flags)
746 char buf[NAME_MAX + 1];
750 rval = attr_remove(name->path, attrname, flags);
751 if (rval >= 0 || errno != ENAMETOOLONG)
753 separate_pathname(name, buf, &newname);
754 if (chdir(buf) == 0) {
755 rval = attr_remove_path(&newname, attrname, flags);
758 free_pathname(&newname);
763 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
764 const int valuelength, int flags)
766 char buf[NAME_MAX + 1];
770 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
771 if (rval >= 0 || errno != ENAMETOOLONG)
773 separate_pathname(name, buf, &newname);
774 if (chdir(buf) == 0) {
775 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
779 free_pathname(&newname);
787 struct stat64 statbuf;
789 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
792 fprintf(stderr, "fsstress: check_cwd failure\n");
799 * go thru flist and release all entries
801 * NOTE: this function shouldn't be called until the end of a process
809 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
818 creat_path(pathname_t *name, mode_t mode)
820 char buf[NAME_MAX + 1];
824 rval = creat(name->path, mode);
825 if (rval >= 0 || errno != ENAMETOOLONG)
827 separate_pathname(name, buf, &newname);
828 if (chdir(buf) == 0) {
829 rval = creat_path(&newname, mode);
832 free_pathname(&newname);
837 dcache_enter(int dirid, int slot)
839 dcache[dirid % NDCACHE] = slot;
847 for (i = 0; i < NDCACHE; i++)
852 dcache_lookup(int dirid)
857 i = dcache[dirid % NDCACHE];
858 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
864 dcache_purge(int dirid)
868 dcp = &dcache[dirid % NDCACHE];
869 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
874 * Delete the item from the list by
875 * moving last entry over the deleted one;
876 * unless deleted entry is the last one.
877 * Input: which file list array and which slot in array
880 del_from_flist(int ft, int slot)
886 dcache_purge(ftp->fents[slot].id);
887 if (slot != ftp->nfiles - 1) {
889 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
890 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
896 dirid_to_fent(int dirid)
902 if ((fep = dcache_lookup(dirid)))
904 flp = &flist[FT_DIR];
905 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
906 if (fep->id == dirid) {
907 dcache_enter(dirid, fep - flp->fents);
917 struct stat64 statbuf;
925 dividend = (operations + execute_freq) / (execute_freq + 1);
926 sprintf(buf, "p%x", procid);
927 (void)mkdir(buf, 0777);
928 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
932 top_ino = statbuf.st_ino;
933 homedir = getcwd(NULL, -1);
938 for (opno = 0; opno < operations; opno++) {
939 if (execute_cmd && opno && opno % dividend == 0) {
941 printf("%d: execute command %s\n", opno,
943 rval = system(execute_cmd);
945 fprintf(stderr, "execute command failed with "
948 p = &ops[freq_table[random() % freq_table_size]];
949 p->func(opno, random());
951 * test for forced shutdown by stat'ing the test
952 * directory. If this stat returns EIO, assume
953 * the forced shutdown happened.
955 if (errtag != 0 && opno % 100 == 0) {
956 rval = stat64(".", &statbuf);
958 fprintf(stderr, "Detected EIO\n");
966 sprintf(cmd, "rm -rf %s", buf);
973 * build up a pathname going thru the file entry and all
975 * Return 0 on error, 1 on success;
978 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
980 char buf[NAME_MAX + 1];
988 /* build up parent directory name */
989 if (fep->parent != -1) {
990 pfep = dirid_to_fent(fep->parent);
993 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
994 procid, fep->id, fep->parent);
999 e = fent_to_name(name, &flist[FT_DIR], pfep);
1002 append_pathname(name, "/");
1005 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1006 namerandpad(fep->id, buf, i);
1007 append_pathname(name, buf);
1012 fix_parent(int oldid, int newid)
1019 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1020 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1021 if (fep->parent == oldid)
1022 fep->parent = newid;
1028 free_pathname(pathname_t *name)
1038 * Generate a filename of type ft.
1039 * If we have a fep which should be a directory then use it
1040 * as the parent path for this new filename i.e. prepend with it.
1043 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1045 char buf[NAME_MAX + 1];
1054 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1055 namerandpad(id, buf, len);
1057 /* prepend fep parent dir-name to it */
1059 e = fent_to_name(name, &flist[FT_DIR], fep);
1062 append_pathname(name, "/");
1064 append_pathname(name, buf);
1068 for (j = 0; !*v && j < ilistlen; j++) {
1069 if (ilist[j] == id) {
1079 * Input: "which" to choose the file-types eg. non-directory
1080 * Input: "r" to choose which file
1081 * Output: file-list, file-entry, name for the chosen file.
1082 * Output: verbose if chosen file is on the ilist.
1085 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1088 int totalsum = 0; /* total number of matching files */
1089 int partialsum = 0; /* partial sum of matching files */
1095 int e = 1; /* success */
1098 * go thru flist and add up number of files for each
1099 * category that matches with <which>.
1101 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1102 if (which & (1 << i))
1103 totalsum += flp->nfiles;
1105 if (totalsum == 0) {
1115 * Now we have possible matches between 0..totalsum-1.
1116 * And we use r to help us choose which one we want,
1117 * which when bounded by totalsum becomes x.
1119 x = (int)(r % totalsum);
1120 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1121 if (which & (1 << i)) {
1122 if (x < partialsum + flp->nfiles) {
1124 /* found the matching file entry */
1125 fep = &flp->fents[x - partialsum];
1127 /* fill-in what we were asked for */
1129 e = fent_to_name(name, flp, fep);
1132 fprintf(stderr, "%d: failed to get path for entry:"
1133 " id=%d,parent=%d\n",
1134 procid, fep->id, fep->parent);
1143 /* turn on verbose if its an ilisted file */
1145 for (j = 0; !*v && j < ilistlen; j++) {
1146 if (ilist[j] == fep->id) {
1153 partialsum += flp->nfiles;
1157 fprintf(stderr, "fsstress: get_fname failure\n");
1164 init_pathname(pathname_t *name)
1171 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1173 char buf[NAME_MAX + 1];
1177 rval = lchown(name->path, owner, group);
1178 if (rval >= 0 || errno != ENAMETOOLONG)
1180 separate_pathname(name, buf, &newname);
1181 if (chdir(buf) == 0) {
1182 rval = lchown_path(&newname, owner, group);
1185 free_pathname(&newname);
1190 link_path(pathname_t *name1, pathname_t *name2)
1192 char buf1[NAME_MAX + 1];
1193 char buf2[NAME_MAX + 1];
1195 pathname_t newname1;
1196 pathname_t newname2;
1199 rval = link(name1->path, name2->path);
1200 if (rval >= 0 || errno != ENAMETOOLONG)
1202 separate_pathname(name1, buf1, &newname1);
1203 separate_pathname(name2, buf2, &newname2);
1204 if (strcmp(buf1, buf2) == 0) {
1205 if (chdir(buf1) == 0) {
1206 rval = link_path(&newname1, &newname2);
1210 if (strcmp(buf1, "..") == 0)
1212 else if (strcmp(buf2, "..") == 0)
1214 else if (strlen(buf1) == 0)
1216 else if (strlen(buf2) == 0)
1219 down1 = MAX(newname1.len, 3 + name2->len) <=
1220 MAX(3 + name1->len, newname2.len);
1222 free_pathname(&newname2);
1223 append_pathname(&newname2, "../");
1224 append_pathname(&newname2, name2->path);
1225 if (chdir(buf1) == 0) {
1226 rval = link_path(&newname1, &newname2);
1230 free_pathname(&newname1);
1231 append_pathname(&newname1, "../");
1232 append_pathname(&newname1, name1->path);
1233 if (chdir(buf2) == 0) {
1234 rval = link_path(&newname1, &newname2);
1239 free_pathname(&newname1);
1240 free_pathname(&newname2);
1245 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1247 char buf[NAME_MAX + 1];
1251 rval = lstat64(name->path, sbuf);
1252 if (rval >= 0 || errno != ENAMETOOLONG)
1254 separate_pathname(name, buf, &newname);
1255 if (chdir(buf) == 0) {
1256 rval = lstat64_path(&newname, sbuf);
1259 free_pathname(&newname);
1264 make_freq_table(void)
1270 for (p = ops, f = 0; p < ops_end; p++)
1272 freq_table = malloc(f * sizeof(*freq_table));
1273 freq_table_size = f;
1274 for (p = ops, i = 0; p < ops_end; p++) {
1275 for (f = 0; f < p->freq; f++, i++)
1276 freq_table[i] = p->op;
1281 mkdir_path(pathname_t *name, mode_t mode)
1283 char buf[NAME_MAX + 1];
1287 rval = mkdir(name->path, mode);
1288 if (rval >= 0 || errno != ENAMETOOLONG)
1290 separate_pathname(name, buf, &newname);
1291 if (chdir(buf) == 0) {
1292 rval = mkdir_path(&newname, mode);
1295 free_pathname(&newname);
1300 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1302 char buf[NAME_MAX + 1];
1306 rval = mknod(name->path, mode, dev);
1307 if (rval >= 0 || errno != ENAMETOOLONG)
1309 separate_pathname(name, buf, &newname);
1310 if (chdir(buf) == 0) {
1311 rval = mknod_path(&newname, mode, dev);
1314 free_pathname(&newname);
1319 namerandpad(int id, char *buf, int i)
1322 static int buckets[] =
1323 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1329 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1330 padmod = buckets[bucket] + 1 - i;
1333 padlen = (id ^ namerand) % padmod;
1335 memset(&buf[i], 'X', padlen);
1336 buf[i + padlen] = '\0';
1341 open_path(pathname_t *name, int oflag)
1343 char buf[NAME_MAX + 1];
1347 rval = open(name->path, oflag);
1348 if (rval >= 0 || errno != ENAMETOOLONG)
1350 separate_pathname(name, buf, &newname);
1351 if (chdir(buf) == 0) {
1352 rval = open_path(&newname, oflag);
1355 free_pathname(&newname);
1360 opendir_path(pathname_t *name)
1362 char buf[NAME_MAX + 1];
1366 rval = opendir(name->path);
1367 if (rval || errno != ENAMETOOLONG)
1369 separate_pathname(name, buf, &newname);
1370 if (chdir(buf) == 0) {
1371 rval = opendir_path(&newname);
1374 free_pathname(&newname);
1379 process_freq(char *arg)
1384 s = strchr(arg, '=');
1386 fprintf(stderr, "bad argument '%s'\n", arg);
1390 for (p = ops; p < ops_end; p++) {
1391 if (strcmp(arg, p->name) == 0) {
1396 fprintf(stderr, "can't find op type %s for -f\n", arg);
1401 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1403 char buf[NAME_MAX + 1];
1407 rval = readlink(name->path, lbuf, lbufsiz);
1408 if (rval >= 0 || errno != ENAMETOOLONG)
1410 separate_pathname(name, buf, &newname);
1411 if (chdir(buf) == 0) {
1412 rval = readlink_path(&newname, lbuf, lbufsiz);
1415 free_pathname(&newname);
1420 rename_path(pathname_t *name1, pathname_t *name2)
1422 char buf1[NAME_MAX + 1];
1423 char buf2[NAME_MAX + 1];
1425 pathname_t newname1;
1426 pathname_t newname2;
1429 rval = rename(name1->path, name2->path);
1430 if (rval >= 0 || errno != ENAMETOOLONG)
1432 separate_pathname(name1, buf1, &newname1);
1433 separate_pathname(name2, buf2, &newname2);
1434 if (strcmp(buf1, buf2) == 0) {
1435 if (chdir(buf1) == 0) {
1436 rval = rename_path(&newname1, &newname2);
1440 if (strcmp(buf1, "..") == 0)
1442 else if (strcmp(buf2, "..") == 0)
1444 else if (strlen(buf1) == 0)
1446 else if (strlen(buf2) == 0)
1449 down1 = MAX(newname1.len, 3 + name2->len) <=
1450 MAX(3 + name1->len, newname2.len);
1452 free_pathname(&newname2);
1453 append_pathname(&newname2, "../");
1454 append_pathname(&newname2, name2->path);
1455 if (chdir(buf1) == 0) {
1456 rval = rename_path(&newname1, &newname2);
1460 free_pathname(&newname1);
1461 append_pathname(&newname1, "../");
1462 append_pathname(&newname1, name1->path);
1463 if (chdir(buf2) == 0) {
1464 rval = rename_path(&newname1, &newname2);
1469 free_pathname(&newname1);
1470 free_pathname(&newname2);
1475 rmdir_path(pathname_t *name)
1477 char buf[NAME_MAX + 1];
1481 rval = rmdir(name->path);
1482 if (rval >= 0 || errno != ENAMETOOLONG)
1484 separate_pathname(name, buf, &newname);
1485 if (chdir(buf) == 0) {
1486 rval = rmdir_path(&newname);
1489 free_pathname(&newname);
1494 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1498 init_pathname(newname);
1499 slash = strchr(name->path, '/');
1500 if (slash == NULL) {
1505 strcpy(buf, name->path);
1507 append_pathname(newname, slash + 1);
1513 show_ops(int flag, char *lead_str)
1518 /* print in list form */
1521 for (p = ops; p < ops_end; p++) {
1522 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1523 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1524 x+=printf("%s ", p->name);
1527 } else if (flag == 0) {
1528 /* Table view style */
1530 for (f = 0, p = ops; p < ops_end; p++)
1536 for (p = ops; p < ops_end; p++) {
1537 if (flag != 0 || p->freq > 0) {
1538 if (lead_str != NULL)
1539 printf("%s", lead_str);
1540 printf("%20s %d/%d %s\n",
1541 p->name, p->freq, f,
1542 (p->iswrite == 0) ? " " : "write op");
1546 /* Command line style */
1547 if (lead_str != NULL)
1548 printf("%s", lead_str);
1549 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1551 for (p = ops; p < ops_end; p++)
1553 printf("-f %s=%d \\\n",p->name, p->freq);
1558 stat64_path(pathname_t *name, struct stat64 *sbuf)
1560 char buf[NAME_MAX + 1];
1564 rval = stat64(name->path, sbuf);
1565 if (rval >= 0 || errno != ENAMETOOLONG)
1567 separate_pathname(name, buf, &newname);
1568 if (chdir(buf) == 0) {
1569 rval = stat64_path(&newname, sbuf);
1572 free_pathname(&newname);
1577 symlink_path(const char *name1, pathname_t *name)
1579 char buf[NAME_MAX + 1];
1583 if (!strcmp(name1, name->path)) {
1584 printf("yikes! %s %s\n", name1, name->path);
1588 rval = symlink(name1, name->path);
1589 if (rval >= 0 || errno != ENAMETOOLONG)
1591 separate_pathname(name, buf, &newname);
1592 if (chdir(buf) == 0) {
1593 rval = symlink_path(name1, &newname);
1596 free_pathname(&newname);
1601 truncate64_path(pathname_t *name, off64_t length)
1603 char buf[NAME_MAX + 1];
1607 rval = truncate64(name->path, length);
1608 if (rval >= 0 || errno != ENAMETOOLONG)
1610 separate_pathname(name, buf, &newname);
1611 if (chdir(buf) == 0) {
1612 rval = truncate64_path(&newname, length);
1615 free_pathname(&newname);
1620 unlink_path(pathname_t *name)
1622 char buf[NAME_MAX + 1];
1626 rval = unlink(name->path);
1627 if (rval >= 0 || errno != ENAMETOOLONG)
1629 separate_pathname(name, buf, &newname);
1630 if (chdir(buf) == 0) {
1631 rval = unlink_path(&newname);
1634 free_pathname(&newname);
1641 printf("Usage: %s -H or\n", myprog);
1642 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1644 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1646 printf(" -c clean up the test directory after each run\n");
1647 printf(" -d dir specifies the base directory for operations\n");
1648 printf(" -e errtg specifies error injection stuff\n");
1649 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1650 printf(" the valid operation names are:\n");
1652 printf(" -i filenum get verbose output for this nth file object\n");
1653 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1654 printf(" *use 0 for infinite (default 1)\n");
1655 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1656 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1657 printf(" -o logfile specifies logfile name\n");
1658 printf(" -p nproc specifies the no. of processes (default 1)\n");
1659 printf(" -r specifies random name padding\n");
1660 printf(" -s seed specifies the seed for the random generator (default random)\n");
1661 printf(" -v specifies verbose mode\n");
1662 printf(" -w zeros frequencies of non-write operations\n");
1663 printf(" -x cmd execute command in the middle of operations\n");
1664 printf(" -z zeros frequencies of all operations\n");
1665 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1666 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1667 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1668 printf(" -H prints usage and exits\n");
1676 for (p = ops; p < ops_end; p++) {
1687 for (p = ops; p < ops_end; p++)
1691 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1694 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1695 verifiable_log ? -1: (long)s->st_ino,
1696 (long)s->st_nlink, s->st_uid, s->st_gid,
1697 (long long) s->st_blocks, (long long) s->st_size);
1701 allocsp_f(int opno, long r)
1706 struct xfs_flock64 fl;
1714 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1716 printf("%d/%d: allocsp - no filename\n", procid, opno);
1720 fd = open_path(&f, O_RDWR);
1721 e = fd < 0 ? errno : 0;
1725 printf("%d/%d: allocsp - open %s failed %d\n",
1726 procid, opno, f.path, e);
1730 if (fstat64(fd, &stb) < 0) {
1732 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1733 procid, opno, f.path, errno);
1738 inode_info(st, sizeof(st), &stb, v);
1739 lr = ((__int64_t)random() << 32) + random();
1740 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1742 fl.l_whence = SEEK_SET;
1745 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1747 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1748 procid, opno, f.path, st, (long long)off, e);
1755 attr_remove_f(int opno, long r)
1757 attrlist_ent_t *aep;
1761 attrlist_cursor_t cursor;
1770 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1771 append_pathname(&f, ".");
1773 bzero(&cursor, sizeof(cursor));
1775 bzero(buf, sizeof(buf));
1776 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1780 alist = (attrlist_t *)buf;
1781 total += alist->al_count;
1782 } while (alist->al_more);
1785 printf("%d/%d: attr_remove - no attrs for %s\n",
1786 procid, opno, f.path);
1790 which = (int)(random() % total);
1791 bzero(&cursor, sizeof(cursor));
1795 bzero(buf, sizeof(buf));
1796 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1800 alist = (attrlist_t *)buf;
1801 if (which < ent + alist->al_count) {
1802 aep = (attrlist_ent_t *)
1803 &buf[alist->al_offset[which - ent]];
1804 aname = aep->a_name;
1807 ent += alist->al_count;
1808 } while (alist->al_more);
1809 if (aname == NULL) {
1812 "%d/%d: attr_remove - name %d not found at %s\n",
1813 procid, opno, which, f.path);
1817 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1820 printf("%d/%d: attr_remove %s %s %d\n",
1821 procid, opno, f.path, aname, e);
1826 attr_set_f(int opno, long r)
1833 static int lengths[] = { 10, 100, 1000, 10000 };
1838 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1839 append_pathname(&f, ".");
1840 sprintf(aname, "a%x", nameseq++);
1841 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1842 len = (int)(random() % lengths[li]);
1846 memset(aval, nameseq & 0xff, len);
1847 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1852 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1858 bulkstat_f(int opno, long r)
1866 xfs_fsop_bulkreq_t bsr;
1869 nent = (r % 999) + 2;
1870 t = malloc(nent * sizeof(*t));
1871 fd = open(".", O_RDONLY);
1879 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1883 printf("%d/%d: bulkstat nent %d total %lld\n",
1884 procid, opno, nent, (long long)total);
1889 bulkstat1_f(int opno, long r)
1899 xfs_fsop_bulkreq_t bsr;
1902 good = random() & 1;
1904 /* use an inode we know exists */
1906 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1907 append_pathname(&f, ".");
1908 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1913 * pick a random inode
1915 * note this can generate kernel warning messages
1916 * since bulkstat_one will read the disk block that
1917 * would contain a given inode even if that disk
1918 * block doesn't contain inodes.
1920 * this is detected later, but not until after the
1921 * warning is displayed.
1923 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1929 fd = open(".", O_RDONLY);
1935 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1937 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
1938 procid, opno, good?"real":"random",
1939 verifiable_log ? -1LL : (long long)ino, e);
1944 chown_f(int opno, long r)
1954 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1955 append_pathname(&f, ".");
1956 u = (uid_t)random();
1957 g = (gid_t)random();
1958 nbits = (int)(random() % idmodulo);
1959 u &= (1 << nbits) - 1;
1960 g &= (1 << nbits) - 1;
1961 e = lchown_path(&f, u, g) < 0 ? errno : 0;
1964 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
1969 setxattr_f(int opno, long r)
1971 #ifdef XFS_XFLAG_EXTSIZE
1981 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1982 append_pathname(&f, ".");
1983 fd = open_path(&f, O_RDWR);
1984 e = fd < 0 ? errno : 0;
1989 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
1990 nbits = (int)(random() % e);
1991 p &= (1 << nbits) - 1;
1993 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
1995 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
1998 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2005 creat_f(int opno, long r)
2020 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2025 e1 = (random() % 100);
2026 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2028 if (type == FT_RTF) /* rt always gets an extsize */
2029 extsize = (random() % 10) + 1;
2030 else if (e1 < 10) /* one-in-ten get an extsize */
2031 extsize = random() % 1024;
2035 e = generate_fname(fep, type, &f, &id, &v);
2039 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2040 printf("%d/%d: creat - no filename from %s\n",
2041 procid, opno, f.path);
2046 fd = creat_path(&f, 0666);
2047 e = fd < 0 ? errno : 0;
2052 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2053 if (type == FT_RTF) {
2054 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2055 a.fsx_extsize = extsize *
2056 geom.rtextsize * geom.blocksize;
2058 } else if (extsize) {
2059 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2060 a.fsx_extsize = extsize * geom.blocksize;
2063 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2066 add_to_flist(type, id, parid);
2070 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2071 extsize ? a.fsx_extsize : 0, e, e1);
2072 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2078 dread_f(int opno, long r)
2082 struct dioattr diob;
2095 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2097 printf("%d/%d: dread - no filename\n", procid, opno);
2101 fd = open_path(&f, O_RDONLY|O_DIRECT);
2102 e = fd < 0 ? errno : 0;
2106 printf("%d/%d: dread - open %s failed %d\n",
2107 procid, opno, f.path, e);
2111 if (fstat64(fd, &stb) < 0) {
2113 printf("%d/%d: dread - fstat64 %s failed %d\n",
2114 procid, opno, f.path, errno);
2119 inode_info(st, sizeof(st), &stb, v);
2120 if (stb.st_size == 0) {
2122 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2128 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2131 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s failed %d\n",
2132 procid, opno, f.path, st, errno);
2138 dio_env = getenv("XFS_DIO_MIN");
2140 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2142 align = (__int64_t)diob.d_miniosz;
2143 lr = ((__int64_t)random() << 32) + random();
2144 off = (off64_t)(lr % stb.st_size);
2145 off -= (off % align);
2146 lseek64(fd, off, SEEK_SET);
2147 len = (random() % FILELEN_MAX) + 1;
2148 len -= (len % align);
2151 else if (len > diob.d_maxiosz)
2152 len = diob.d_maxiosz;
2153 buf = memalign(diob.d_mem, len);
2154 e = read(fd, buf, len) < 0 ? errno : 0;
2157 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2158 procid, opno, f.path, st, (long long)off, (int)len, e);
2164 dwrite_f(int opno, long r)
2168 struct dioattr diob;
2181 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2183 printf("%d/%d: dwrite - no filename\n", procid, opno);
2187 fd = open_path(&f, O_WRONLY|O_DIRECT);
2188 e = fd < 0 ? errno : 0;
2192 printf("%d/%d: dwrite - open %s failed %d\n",
2193 procid, opno, f.path, e);
2197 if (fstat64(fd, &stb) < 0) {
2199 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2200 procid, opno, f.path, errno);
2205 inode_info(st, sizeof(st), &stb, v);
2206 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2208 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2209 " %s%s failed %d\n",
2210 procid, opno, f.path, st, errno);
2216 dio_env = getenv("XFS_DIO_MIN");
2218 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2220 align = (__int64_t)diob.d_miniosz;
2221 lr = ((__int64_t)random() << 32) + random();
2222 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2223 off -= (off % align);
2224 lseek64(fd, off, SEEK_SET);
2225 len = (random() % FILELEN_MAX) + 1;
2226 len -= (len % align);
2229 else if (len > diob.d_maxiosz)
2230 len = diob.d_maxiosz;
2231 buf = memalign(diob.d_mem, len);
2233 lseek64(fd, off, SEEK_SET);
2234 memset(buf, nameseq & 0xff, len);
2235 e = write(fd, buf, len) < 0 ? errno : 0;
2238 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2239 procid, opno, f.path, st, (long long)off, (int)len, e);
2245 #ifdef HAVE_LINUX_FALLOC_H
2246 struct print_flags falloc_flags [] = {
2247 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2248 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2249 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2250 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2251 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2252 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2256 #define translate_falloc_flags(mode) \
2257 ({translate_flags(mode, "|", falloc_flags);})
2261 do_fallocate(int opno, long r, int mode)
2263 #ifdef HAVE_LINUX_FALLOC_H
2275 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2277 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2281 fd = open_path(&f, O_RDWR);
2284 printf("%d/%d: do_fallocate - open %s failed %d\n",
2285 procid, opno, f.path, errno);
2290 if (fstat64(fd, &stb) < 0) {
2292 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2293 procid, opno, f.path, errno);
2298 inode_info(st, sizeof(st), &stb, v);
2299 lr = ((__int64_t)random() << 32) + random();
2300 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2302 len = (off64_t)(random() % (1024 * 1024));
2304 * Collapse/insert range requires off and len to be block aligned,
2305 * make it more likely to be the case.
2307 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2309 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2310 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2312 mode |= FALLOC_FL_KEEP_SIZE & random();
2313 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2315 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2316 procid, opno, translate_falloc_flags(mode),
2317 f.path, st, (long long)off, (long long)len, e);
2324 fallocate_f(int opno, long r)
2326 #ifdef HAVE_LINUX_FALLOC_H
2327 do_fallocate(opno, r, 0);
2332 fdatasync_f(int opno, long r)
2340 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2342 printf("%d/%d: fdatasync - no filename\n",
2347 fd = open_path(&f, O_WRONLY);
2348 e = fd < 0 ? errno : 0;
2352 printf("%d/%d: fdatasync - open %s failed %d\n",
2353 procid, opno, f.path, e);
2357 e = fdatasync(fd) < 0 ? errno : 0;
2359 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2364 #ifdef HAVE_LINUX_FIEMAP_H
2365 struct print_flags fiemap_flags[] = {
2366 { FIEMAP_FLAG_SYNC, "SYNC"},
2367 { FIEMAP_FLAG_XATTR, "XATTR"},
2371 #define translate_fiemap_flags(mode) \
2372 ({translate_flags(mode, "|", fiemap_flags);})
2376 fiemap_f(int opno, long r)
2378 #ifdef HAVE_LINUX_FIEMAP_H
2388 struct fiemap *fiemap;
2391 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2393 printf("%d/%d: fiemap - no filename\n", procid, opno);
2397 fd = open_path(&f, O_RDWR);
2398 e = fd < 0 ? errno : 0;
2402 printf("%d/%d: fiemap - open %s failed %d\n",
2403 procid, opno, f.path, e);
2407 if (fstat64(fd, &stb) < 0) {
2409 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
2410 procid, opno, f.path, errno);
2415 inode_info(st, sizeof(st), &stb, v);
2416 blocks_to_map = random() & 0xffff;
2417 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
2418 (blocks_to_map * sizeof(struct fiemap_extent)));
2421 printf("%d/%d: malloc failed \n", procid, opno);
2426 lr = ((__int64_t)random() << 32) + random();
2427 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2429 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
2430 fiemap->fm_extent_count = blocks_to_map;
2431 fiemap->fm_mapped_extents = random() & 0xffff;
2432 fiemap->fm_start = off;
2433 fiemap->fm_length = ((__int64_t)random() << 32) + random();
2435 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
2437 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
2438 procid, opno, f.path, st, (long long)fiemap->fm_start,
2439 (long long) fiemap->fm_length,
2440 translate_fiemap_flags(fiemap->fm_flags), e);
2448 freesp_f(int opno, long r)
2453 struct xfs_flock64 fl;
2461 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2463 printf("%d/%d: freesp - no filename\n", procid, opno);
2467 fd = open_path(&f, O_RDWR);
2468 e = fd < 0 ? errno : 0;
2472 printf("%d/%d: freesp - open %s failed %d\n",
2473 procid, opno, f.path, e);
2477 if (fstat64(fd, &stb) < 0) {
2479 printf("%d/%d: freesp - fstat64 %s failed %d\n",
2480 procid, opno, f.path, errno);
2485 inode_info(st, sizeof(st), &stb, v);
2486 lr = ((__int64_t)random() << 32) + random();
2487 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2489 fl.l_whence = SEEK_SET;
2492 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
2494 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
2495 procid, opno, f.path, st, (long long)off, e);
2501 fsync_f(int opno, long r)
2509 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2511 printf("%d/%d: fsync - no filename\n", procid, opno);
2515 fd = open_path(&f, O_WRONLY);
2516 e = fd < 0 ? errno : 0;
2520 printf("%d/%d: fsync - open %s failed %d\n",
2521 procid, opno, f.path, e);
2525 e = fsync(fd) < 0 ? errno : 0;
2527 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
2533 getattr_f(int opno, long r)
2542 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2543 append_pathname(&f, ".");
2544 fd = open_path(&f, O_RDWR);
2545 e = fd < 0 ? errno : 0;
2548 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
2550 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
2556 getdents_f(int opno, long r)
2563 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
2564 append_pathname(&f, ".");
2565 dir = opendir_path(&f);
2569 printf("%d/%d: getdents - can't open %s\n",
2570 procid, opno, f.path);
2574 while (readdir64(dir) != NULL)
2577 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
2583 link_f(int opno, long r)
2596 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
2598 printf("%d/%d: link - no file\n", procid, opno);
2602 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
2608 e = generate_fname(fep, flp - flist, &l, &id, &v1);
2612 (void)fent_to_name(&l, &flist[FT_DIR], fep);
2613 printf("%d/%d: link - no filename from %s\n",
2614 procid, opno, l.path);
2620 e = link_path(&f, &l) < 0 ? errno : 0;
2623 add_to_flist(flp - flist, id, parid);
2625 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
2627 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
2634 mkdir_f(int opno, long r)
2644 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2649 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
2653 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2654 printf("%d/%d: mkdir - no filename from %s\n",
2655 procid, opno, f.path);
2660 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
2663 add_to_flist(FT_DIR, id, parid);
2665 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
2666 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
2672 mknod_f(int opno, long r)
2682 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2687 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2691 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2692 printf("%d/%d: mknod - no filename from %s\n",
2693 procid, opno, f.path);
2698 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2701 add_to_flist(FT_DEV, id, parid);
2703 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2704 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
2709 #ifdef HAVE_SYS_MMAN_H
2710 struct print_flags mmap_flags[] = {
2711 { MAP_SHARED, "SHARED"},
2712 { MAP_PRIVATE, "PRIVATE"},
2716 #define translate_mmap_flags(flags) \
2717 ({translate_flags(flags, "|", mmap_flags);})
2721 do_mmap(int opno, long r, int prot)
2723 #ifdef HAVE_SYS_MMAN_H
2735 sigjmp_buf sigbus_jmpbuf;
2738 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2740 printf("%d/%d: do_mmap - no filename\n", procid, opno);
2744 fd = open_path(&f, O_RDWR);
2745 e = fd < 0 ? errno : 0;
2749 printf("%d/%d: do_mmap - open %s failed %d\n",
2750 procid, opno, f.path, e);
2754 if (fstat64(fd, &stb) < 0) {
2756 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
2757 procid, opno, f.path, errno);
2762 inode_info(st, sizeof(st), &stb, v);
2763 if (stb.st_size == 0) {
2765 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
2772 lr = ((__int64_t)random() << 32) + random();
2773 off = (off64_t)(lr % stb.st_size);
2774 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
2775 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
2777 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
2778 addr = mmap(NULL, len, prot, flags, fd, off);
2779 e = (addr == MAP_FAILED) ? errno : 0;
2782 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
2783 procid, opno, f.path, st, (long long)off,
2784 (int)len, translate_mmap_flags(flags), e);
2790 if (prot & PROT_WRITE) {
2791 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
2792 sigbus_jmp = &sigbus_jmpbuf;
2793 memset(addr, nameseq & 0xff, len);
2797 if ((buf = malloc(len)) != NULL) {
2798 memcpy(buf, addr, len);
2803 /* set NULL to stop other functions from doing siglongjmp */
2807 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
2808 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
2809 f.path, st, (long long)off, (int)len,
2810 translate_mmap_flags(flags),
2811 (e == 0) ? "0" : "Bus error");
2819 mread_f(int opno, long r)
2821 #ifdef HAVE_SYS_MMAN_H
2822 do_mmap(opno, r, PROT_READ);
2827 mwrite_f(int opno, long r)
2829 #ifdef HAVE_SYS_MMAN_H
2830 do_mmap(opno, r, PROT_WRITE);
2835 punch_f(int opno, long r)
2837 #ifdef HAVE_LINUX_FALLOC_H
2838 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
2843 zero_f(int opno, long r)
2845 #ifdef HAVE_LINUX_FALLOC_H
2846 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
2851 collapse_f(int opno, long r)
2853 #ifdef HAVE_LINUX_FALLOC_H
2854 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
2859 insert_f(int opno, long r)
2861 #ifdef HAVE_LINUX_FALLOC_H
2862 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
2867 read_f(int opno, long r)
2881 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2883 printf("%d/%d: read - no filename\n", procid, opno);
2887 fd = open_path(&f, O_RDONLY);
2888 e = fd < 0 ? errno : 0;
2892 printf("%d/%d: read - open %s failed %d\n",
2893 procid, opno, f.path, e);
2897 if (fstat64(fd, &stb) < 0) {
2899 printf("%d/%d: read - fstat64 %s failed %d\n",
2900 procid, opno, f.path, errno);
2905 inode_info(st, sizeof(st), &stb, v);
2906 if (stb.st_size == 0) {
2908 printf("%d/%d: read - %s%s zero size\n", procid, opno,
2914 lr = ((__int64_t)random() << 32) + random();
2915 off = (off64_t)(lr % stb.st_size);
2916 lseek64(fd, off, SEEK_SET);
2917 len = (random() % FILELEN_MAX) + 1;
2919 e = read(fd, buf, len) < 0 ? errno : 0;
2922 printf("%d/%d: read %s%s [%lld,%d] %d\n",
2923 procid, opno, f.path, st, (long long)off, (int)len, e);
2929 readlink_f(int opno, long r)
2937 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2939 printf("%d/%d: readlink - no filename\n", procid, opno);
2943 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2946 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2951 rename_f(int opno, long r)
2965 /* get an existing path for the source of the rename */
2967 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2969 printf("%d/%d: rename - no filename\n", procid, opno);
2974 /* get an existing directory for the destination parent directory name */
2975 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2981 /* generate a new path using an existing parent directory in name */
2982 init_pathname(&newf);
2983 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2987 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
2988 printf("%d/%d: rename - no filename from %s\n",
2989 procid, opno, f.path);
2991 free_pathname(&newf);
2995 e = rename_path(&f, &newf) < 0 ? errno : 0;
2998 if (flp - flist == FT_DIR) {
3000 fix_parent(oldid, id);
3002 del_from_flist(flp - flist, fep - flp->fents);
3003 add_to_flist(flp - flist, id, parid);
3006 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3009 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3010 procid, opno, fep->id, fep->parent);
3011 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3012 procid, opno, id, parid);
3015 free_pathname(&newf);
3020 resvsp_f(int opno, long r)
3025 struct xfs_flock64 fl;
3033 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3035 printf("%d/%d: resvsp - no filename\n", procid, opno);
3039 fd = open_path(&f, O_RDWR);
3040 e = fd < 0 ? errno : 0;
3044 printf("%d/%d: resvsp - open %s failed %d\n",
3045 procid, opno, f.path, e);
3049 if (fstat64(fd, &stb) < 0) {
3051 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3052 procid, opno, f.path, errno);
3057 inode_info(st, sizeof(st), &stb, v);
3058 lr = ((__int64_t)random() << 32) + random();
3059 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3061 fl.l_whence = SEEK_SET;
3063 fl.l_len = (off64_t)(random() % (1024 * 1024));
3064 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3066 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3067 procid, opno, f.path, st,
3068 (long long)off, (long long)fl.l_len, e);
3074 rmdir_f(int opno, long r)
3082 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3084 printf("%d/%d: rmdir - no directory\n", procid, opno);
3088 e = rmdir_path(&f) < 0 ? errno : 0;
3091 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3093 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3095 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3096 procid, opno, fep->id, fep->parent);
3102 setattr_f(int opno, long r)
3111 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3112 append_pathname(&f, ".");
3113 fd = open_path(&f, O_RDWR);
3114 e = fd < 0 ? errno : 0;
3117 fl = attr_mask & (uint)random();
3118 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3120 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3126 stat_f(int opno, long r)
3134 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3136 printf("%d/%d: stat - no entries\n", procid, opno);
3140 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3143 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3148 symlink_f(int opno, long r)
3161 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3166 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3170 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3171 printf("%d/%d: symlink - no filename from %s\n",
3172 procid, opno, f.path);
3177 len = (int)(random() % PATH_MAX);
3178 val = malloc(len + 1);
3180 memset(val, 'x', len);
3182 for (i = 10; i < len - 1; i += 10)
3184 e = symlink_path(val, &f) < 0 ? errno : 0;
3187 add_to_flist(FT_SYM, id, parid);
3190 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3191 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3198 sync_f(int opno, long r)
3202 printf("%d/%d: sync\n", procid, opno);
3206 truncate_f(int opno, long r)
3217 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3219 printf("%d/%d: truncate - no filename\n", procid, opno);
3223 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3227 printf("%d/%d: truncate - stat64 %s failed %d\n",
3228 procid, opno, f.path, e);
3232 inode_info(st, sizeof(st), &stb, v);
3233 lr = ((__int64_t)random() << 32) + random();
3234 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3236 e = truncate64_path(&f, off) < 0 ? errno : 0;
3239 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3240 st, (long long)off, e);
3245 unlink_f(int opno, long r)
3254 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3256 printf("%d/%d: unlink - no file\n", procid, opno);
3260 e = unlink_path(&f) < 0 ? errno : 0;
3263 del_from_flist(flp - flist, fep - flp->fents);
3265 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3267 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3268 procid, opno, fep->id, fep->parent);
3274 unresvsp_f(int opno, long r)
3279 struct xfs_flock64 fl;
3287 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3289 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3293 fd = open_path(&f, O_RDWR);
3294 e = fd < 0 ? errno : 0;
3298 printf("%d/%d: unresvsp - open %s failed %d\n",
3299 procid, opno, f.path, e);
3303 if (fstat64(fd, &stb) < 0) {
3305 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3306 procid, opno, f.path, errno);
3311 inode_info(st, sizeof(st), &stb, v);
3312 lr = ((__int64_t)random() << 32) + random();
3313 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3315 fl.l_whence = SEEK_SET;
3317 fl.l_len = (off64_t)(random() % (1 << 20));
3318 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
3320 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
3321 procid, opno, f.path, st,
3322 (long long)off, (long long)fl.l_len, e);
3328 write_f(int opno, long r)
3342 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
3344 printf("%d/%d: write - no filename\n", procid, opno);
3348 fd = open_path(&f, O_WRONLY);
3349 e = fd < 0 ? errno : 0;
3353 printf("%d/%d: write - open %s failed %d\n",
3354 procid, opno, f.path, e);
3358 if (fstat64(fd, &stb) < 0) {
3360 printf("%d/%d: write - fstat64 %s failed %d\n",
3361 procid, opno, f.path, errno);
3366 inode_info(st, sizeof(st), &stb, v);
3367 lr = ((__int64_t)random() << 32) + random();
3368 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3370 lseek64(fd, off, SEEK_SET);
3371 len = (random() % FILELEN_MAX) + 1;
3373 memset(buf, nameseq & 0xff, len);
3374 e = write(fd, buf, len) < 0 ? errno : 0;
3377 printf("%d/%d: write %s%s [%lld,%d] %d\n",
3378 procid, opno, f.path, st, (long long)off, (int)len, e);