2 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_ATTR_XATTR_H
22 #include <attr/xattr.h>
24 #ifdef HAVE_ATTR_ATTRIBUTES_H
25 #include <attr/attributes.h>
27 #ifdef HAVE_LINUX_FIEMAP_H
28 #include <linux/fiemap.h>
30 #ifndef HAVE_ATTR_LIST
31 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
33 #ifdef HAVE_SYS_PRCTL_H
34 #include <sys/prctl.h>
38 #ifndef FS_IOC_GETFLAGS
39 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
41 #ifndef FS_IOC_SETFLAGS
42 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
46 #define XFS_ERRTAG_MAX 17
47 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
48 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
50 #define FILELEN_MAX (32*4096)
93 typedef void (*opfnc_t)(int, long);
95 typedef struct opdesc {
103 typedef struct fent {
108 typedef struct flist {
115 typedef struct pathname {
125 struct print_string {
132 #define FT_DIRm (1 << FT_DIR)
134 #define FT_REGm (1 << FT_REG)
136 #define FT_SYMm (1 << FT_SYM)
138 #define FT_DEVm (1 << FT_DEV)
140 #define FT_RTFm (1 << FT_RTF)
142 #define FT_ANYm ((1 << FT_nft) - 1)
143 #define FT_REGFILE (FT_REGm | FT_RTFm)
144 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
146 #define FLIST_SLOT_INCR 16
149 #define MAXFSIZE ((1ULL << 63) - 1ULL)
150 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
152 void allocsp_f(int, long);
153 void attr_remove_f(int, long);
154 void attr_set_f(int, long);
155 void bulkstat_f(int, long);
156 void bulkstat1_f(int, long);
157 void chown_f(int, long);
158 void creat_f(int, long);
159 void dread_f(int, long);
160 void dwrite_f(int, long);
161 void fallocate_f(int, long);
162 void fdatasync_f(int, long);
163 void fiemap_f(int, long);
164 void freesp_f(int, long);
165 void fsync_f(int, long);
166 void getattr_f(int, long);
167 void getdents_f(int, long);
168 void link_f(int, long);
169 void mkdir_f(int, long);
170 void mknod_f(int, long);
171 void punch_f(int, long);
172 void zero_f(int, long);
173 void collapse_f(int, long);
174 void insert_f(int, long);
175 void read_f(int, long);
176 void readlink_f(int, long);
177 void rename_f(int, long);
178 void resvsp_f(int, long);
179 void rmdir_f(int, long);
180 void setattr_f(int, long);
181 void setxattr_f(int, long);
182 void stat_f(int, long);
183 void symlink_f(int, long);
184 void sync_f(int, long);
185 void truncate_f(int, long);
186 void unlink_f(int, long);
187 void unresvsp_f(int, long);
188 void write_f(int, long);
191 /* { OP_ENUM, "name", function, freq, iswrite }, */
192 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
193 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
194 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
195 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
196 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
197 { OP_CHOWN, "chown", chown_f, 3, 1 },
198 { OP_CREAT, "creat", creat_f, 4, 1 },
199 { OP_DREAD, "dread", dread_f, 4, 0 },
200 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
201 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
202 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
203 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
204 { OP_FREESP, "freesp", freesp_f, 1, 1 },
205 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
206 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
207 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
208 { OP_LINK, "link", link_f, 1, 1 },
209 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
210 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
211 { OP_PUNCH, "punch", punch_f, 1, 1 },
212 { OP_ZERO, "zero", zero_f, 1, 1 },
213 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
214 { OP_INSERT, "insert", insert_f, 1, 1 },
215 { OP_READ, "read", read_f, 1, 0 },
216 { OP_READLINK, "readlink", readlink_f, 1, 0 },
217 { OP_RENAME, "rename", rename_f, 2, 1 },
218 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
219 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
220 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
221 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
222 { OP_STAT, "stat", stat_f, 1, 0 },
223 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
224 { OP_SYNC, "sync", sync_f, 1, 1 },
225 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
226 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
227 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
228 { OP_WRITE, "write", write_f, 4, 1 },
231 flist_t flist[FT_nft] = {
244 xfs_fsop_geom_t geom;
255 unsigned int idmodulo = XFS_IDMODULO_MAX;
256 unsigned int attr_mask = ~0;
259 unsigned long seed = 0;
262 int verifiable_log = 0;
263 sig_atomic_t should_stop = 0;
264 char *execute_cmd = NULL;
265 int execute_freq = 1;
266 struct print_string flag_str = {0};
268 void add_to_flist(int, int, int);
269 void append_pathname(pathname_t *, char *);
270 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
271 int attr_remove_path(pathname_t *, const char *, int);
272 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
273 void check_cwd(void);
274 int creat_path(pathname_t *, mode_t);
275 void dcache_enter(int, int);
276 void dcache_init(void);
277 fent_t *dcache_lookup(int);
278 void dcache_purge(int);
279 void del_from_flist(int, int);
280 int dirid_to_name(char *, int);
282 int fent_to_name(pathname_t *, flist_t *, fent_t *);
283 void fix_parent(int, int);
284 void free_pathname(pathname_t *);
285 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
286 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
287 void init_pathname(pathname_t *);
288 int lchown_path(pathname_t *, uid_t, gid_t);
289 int link_path(pathname_t *, pathname_t *);
290 int lstat64_path(pathname_t *, struct stat64 *);
291 void make_freq_table(void);
292 int mkdir_path(pathname_t *, mode_t);
293 int mknod_path(pathname_t *, mode_t, dev_t);
294 void namerandpad(int, char *, int);
295 int open_path(pathname_t *, int);
296 DIR *opendir_path(pathname_t *);
297 void process_freq(char *);
298 int readlink_path(pathname_t *, char *, size_t);
299 int rename_path(pathname_t *, pathname_t *);
300 int rmdir_path(pathname_t *);
301 void separate_pathname(pathname_t *, char *, pathname_t *);
302 void show_ops(int, char *);
303 int stat64_path(pathname_t *, struct stat64 *);
304 int symlink_path(const char *, pathname_t *);
305 int truncate64_path(pathname_t *, off64_t);
306 int unlink_path(pathname_t *);
308 void write_freq(void);
309 void zero_freq(void);
311 void sg_handler(int signum)
316 int main(int argc, char **argv)
320 char *dirname = NULL;
321 char *logname = NULL;
322 char rpath[PATH_MAX];
331 xfs_error_injection_t err_inj;
332 struct sigaction action;
333 const char *allopts = "d:e:f:i:m:M:n:o:p:rs:S:vVwx:X:zH";
335 errrange = errtag = 0;
337 nops = sizeof(ops) / sizeof(ops[0]);
338 ops_end = &ops[nops];
340 while ((c = getopt(argc, argv, allopts)) != -1) {
346 sscanf(optarg, "%d", &errtag);
350 } else if (errtag == 0)
352 if (errtag >= XFS_ERRTAG_MAX) {
354 "error tag %d too large (max %d)\n",
355 errtag, XFS_ERRTAG_MAX - 1);
360 process_freq(optarg);
363 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
364 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
367 idmodulo = strtoul(optarg, NULL, 0);
368 if (idmodulo > XFS_IDMODULO_MAX) {
370 "chown modulo %d too big (max %d)\n",
371 idmodulo, XFS_IDMODULO_MAX);
376 operations = atoi(optarg);
383 nproc = atoi(optarg);
389 seed = strtoul(optarg, NULL, 0);
398 execute_cmd = optarg;
404 attr_mask = strtoul(optarg, NULL, 0);
408 if (optarg[0] == 'c')
419 execute_freq = strtoul(optarg, NULL, 0);
422 fprintf(stderr, "%s - invalid parameters\n",
432 /* no directory specified */
433 if (!nousage) usage();
437 (void)mkdir(dirname, 0777);
438 if (logname && logname[0] != '/') {
439 if (getcwd(rpath, sizeof(rpath)) < 0){
440 perror("getcwd failed");
446 if (chdir(dirname) < 0) {
452 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
453 if (freopen(path, "a", stdout) == NULL) {
454 perror("freopen logfile failed");
458 sprintf(buf, "fss%x", (unsigned int)getpid());
459 fd = creat(buf, 0666);
460 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
461 maxfsize = (off64_t)MAXFSIZE32;
463 maxfsize = (off64_t)MAXFSIZE;
468 gettimeofday(&t, (void *)NULL);
469 seed = (int)t.tv_sec ^ (int)t.tv_usec;
470 printf("seed = %ld\n", seed);
472 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
473 if (i >= 0 && geom.rtblocks)
474 rtpct = MIN(MAX(geom.rtblocks * 100 /
475 (geom.rtblocks + geom.datablocks), 1), 99);
484 for (i = 0; i < j; i++)
487 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
493 for (i = 0; i < j; i++)
496 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
498 printf("Injecting failure on tag #%d\n", errtag);
499 err_inj.errtag = errtag;
501 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
503 perror("fsstress - XFS_SYSSGI error injection call");
512 action.sa_handler = sg_handler;
513 sigemptyset(&action.sa_mask);
515 if (sigaction(SIGTERM, &action, 0)) {
516 perror("sigaction failed");
520 for (i = 0; i < nproc; i++) {
522 action.sa_handler = SIG_DFL;
523 sigemptyset(&action.sa_mask);
524 if (sigaction(SIGTERM, &action, 0))
526 #ifdef HAVE_SYS_PRCTL_H
527 prctl(PR_SET_PDEATHSIG, SIGKILL);
528 if (getppid() == 1) /* parent died already? */
533 snprintf(path, sizeof(path), "%s/%s.%d",
535 if (freopen(path, "a", stdout) == NULL) {
536 perror("freopen logfile failed");
545 while (wait(&stat) > 0 && !should_stop) {
548 action.sa_flags = SA_RESTART;
549 sigaction(SIGTERM, &action, 0);
550 kill(-getpid(), SIGTERM);
551 while (wait(&stat) > 0)
557 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
559 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
561 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
573 add_string(struct print_string *str, const char *add)
575 int len = strlen(add);
580 if (len > (str->max - 1) - str->len) {
581 str->len = str->max - 1;
585 memcpy(str->buffer + str->len, add, len);
587 str->buffer[str->len] = '\0';
593 translate_flags(int flags, const char *delim,
594 const struct print_flags *flag_array)
596 int i, mask, first = 1;
599 if (!flag_str.buffer) {
600 flag_str.buffer = malloc(4096);
604 if (!flag_str.buffer)
607 flag_str.buffer[0] = '\0';
609 for (i = 0; flag_array[i].name && flags; i++) {
610 mask = flag_array[i].mask;
611 if ((flags & mask) != mask)
614 add = flag_array[i].name;
617 add_string(&flag_str, delim);
620 add_string(&flag_str, add);
623 /* Check whether there are any leftover flags. */
629 add_string(&flag_str, delim);
631 ret = snprintf(number, 11, "0x%x", flags) > 0;
632 if (ret > 0 && ret <= 11)
633 add_string(&flag_str, number);
636 return flag_str.buffer;
640 add_to_flist(int ft, int id, int parent)
646 if (ftp->nfiles == ftp->nslots) {
647 ftp->nslots += FLIST_SLOT_INCR;
648 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
650 fep = &ftp->fents[ftp->nfiles++];
652 fep->parent = parent;
656 append_pathname(pathname_t *name, char *str)
662 /* attempting to append to a dir a zero length path */
663 if (len && *str == '/' && name->len == 0) {
664 fprintf(stderr, "fsstress: append_pathname failure\n");
670 name->path = realloc(name->path, name->len + 1 + len);
671 strcpy(&name->path[name->len], str);
676 attr_list_path(pathname_t *name,
678 const int buffersize,
680 attrlist_cursor_t *cursor)
682 char buf[NAME_MAX + 1];
686 if (flags != ATTR_DONTFOLLOW) {
691 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
692 if (rval >= 0 || errno != ENAMETOOLONG)
694 separate_pathname(name, buf, &newname);
695 if (chdir(buf) == 0) {
696 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
699 free_pathname(&newname);
704 attr_remove_path(pathname_t *name, const char *attrname, int flags)
706 char buf[NAME_MAX + 1];
710 rval = attr_remove(name->path, attrname, flags);
711 if (rval >= 0 || errno != ENAMETOOLONG)
713 separate_pathname(name, buf, &newname);
714 if (chdir(buf) == 0) {
715 rval = attr_remove_path(&newname, attrname, flags);
718 free_pathname(&newname);
723 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
724 const int valuelength, int flags)
726 char buf[NAME_MAX + 1];
730 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
731 if (rval >= 0 || errno != ENAMETOOLONG)
733 separate_pathname(name, buf, &newname);
734 if (chdir(buf) == 0) {
735 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
739 free_pathname(&newname);
747 struct stat64 statbuf;
749 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
752 fprintf(stderr, "fsstress: check_cwd failure\n");
759 creat_path(pathname_t *name, mode_t mode)
761 char buf[NAME_MAX + 1];
765 rval = creat(name->path, mode);
766 if (rval >= 0 || errno != ENAMETOOLONG)
768 separate_pathname(name, buf, &newname);
769 if (chdir(buf) == 0) {
770 rval = creat_path(&newname, mode);
773 free_pathname(&newname);
778 dcache_enter(int dirid, int slot)
780 dcache[dirid % NDCACHE] = slot;
788 for (i = 0; i < NDCACHE; i++)
793 dcache_lookup(int dirid)
798 i = dcache[dirid % NDCACHE];
799 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
805 dcache_purge(int dirid)
809 dcp = &dcache[dirid % NDCACHE];
810 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
815 * Delete the item from the list by
816 * moving last entry over the deleted one;
817 * unless deleted entry is the last one.
818 * Input: which file list array and which slot in array
821 del_from_flist(int ft, int slot)
827 dcache_purge(ftp->fents[slot].id);
828 if (slot != ftp->nfiles - 1) {
830 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
831 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
837 dirid_to_fent(int dirid)
843 if ((fep = dcache_lookup(dirid)))
845 flp = &flist[FT_DIR];
846 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
847 if (fep->id == dirid) {
848 dcache_enter(dirid, fep - flp->fents);
858 struct stat64 statbuf;
865 dividend = (operations + execute_freq) / (execute_freq + 1);
866 sprintf(buf, "p%x", procid);
867 (void)mkdir(buf, 0777);
868 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
872 top_ino = statbuf.st_ino;
873 homedir = getcwd(NULL, -1);
878 for (opno = 0; opno < operations; opno++) {
879 if (execute_cmd && opno && opno % dividend == 0) {
881 printf("%d: execute command %s\n", opno,
883 rval = system(execute_cmd);
885 fprintf(stderr, "execute command failed with "
888 p = &ops[freq_table[random() % freq_table_size]];
889 p->func(opno, random());
891 * test for forced shutdown by stat'ing the test
892 * directory. If this stat returns EIO, assume
893 * the forced shutdown happened.
895 if (errtag != 0 && opno % 100 == 0) {
896 rval = stat64(".", &statbuf);
898 fprintf(stderr, "Detected EIO\n");
906 * build up a pathname going thru the file entry and all
908 * Return 0 on error, 1 on success;
911 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
913 char buf[NAME_MAX + 1];
921 /* build up parent directory name */
922 if (fep->parent != -1) {
923 pfep = dirid_to_fent(fep->parent);
926 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
927 procid, fep->id, fep->parent);
932 e = fent_to_name(name, &flist[FT_DIR], pfep);
935 append_pathname(name, "/");
938 i = sprintf(buf, "%c%x", flp->tag, fep->id);
939 namerandpad(fep->id, buf, i);
940 append_pathname(name, buf);
945 fix_parent(int oldid, int newid)
952 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
953 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
954 if (fep->parent == oldid)
961 free_pathname(pathname_t *name)
971 * Generate a filename of type ft.
972 * If we have a fep which should be a directory then use it
973 * as the parent path for this new filename i.e. prepend with it.
976 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
978 char buf[NAME_MAX + 1];
987 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
988 namerandpad(id, buf, len);
990 /* prepend fep parent dir-name to it */
992 e = fent_to_name(name, &flist[FT_DIR], fep);
995 append_pathname(name, "/");
997 append_pathname(name, buf);
1001 for (j = 0; !*v && j < ilistlen; j++) {
1002 if (ilist[j] == id) {
1012 * Input: "which" to choose the file-types eg. non-directory
1013 * Input: "r" to choose which file
1014 * Output: file-list, file-entry, name for the chosen file.
1015 * Output: verbose if chosen file is on the ilist.
1018 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1021 int totalsum = 0; /* total number of matching files */
1022 int partialsum = 0; /* partial sum of matching files */
1028 int e = 1; /* success */
1031 * go thru flist and add up number of files for each
1032 * category that matches with <which>.
1034 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1035 if (which & (1 << i))
1036 totalsum += flp->nfiles;
1038 if (totalsum == 0) {
1048 * Now we have possible matches between 0..totalsum-1.
1049 * And we use r to help us choose which one we want,
1050 * which when bounded by totalsum becomes x.
1052 x = (int)(r % totalsum);
1053 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1054 if (which & (1 << i)) {
1055 if (x < partialsum + flp->nfiles) {
1057 /* found the matching file entry */
1058 fep = &flp->fents[x - partialsum];
1060 /* fill-in what we were asked for */
1062 e = fent_to_name(name, flp, fep);
1065 fprintf(stderr, "%d: failed to get path for entry:"
1066 " id=%d,parent=%d\n",
1067 procid, fep->id, fep->parent);
1076 /* turn on verbose if its an ilisted file */
1078 for (j = 0; !*v && j < ilistlen; j++) {
1079 if (ilist[j] == fep->id) {
1086 partialsum += flp->nfiles;
1090 fprintf(stderr, "fsstress: get_fname failure\n");
1097 init_pathname(pathname_t *name)
1104 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1106 char buf[NAME_MAX + 1];
1110 rval = lchown(name->path, owner, group);
1111 if (rval >= 0 || errno != ENAMETOOLONG)
1113 separate_pathname(name, buf, &newname);
1114 if (chdir(buf) == 0) {
1115 rval = lchown_path(&newname, owner, group);
1118 free_pathname(&newname);
1123 link_path(pathname_t *name1, pathname_t *name2)
1125 char buf1[NAME_MAX + 1];
1126 char buf2[NAME_MAX + 1];
1128 pathname_t newname1;
1129 pathname_t newname2;
1132 rval = link(name1->path, name2->path);
1133 if (rval >= 0 || errno != ENAMETOOLONG)
1135 separate_pathname(name1, buf1, &newname1);
1136 separate_pathname(name2, buf2, &newname2);
1137 if (strcmp(buf1, buf2) == 0) {
1138 if (chdir(buf1) == 0) {
1139 rval = link_path(&newname1, &newname2);
1143 if (strcmp(buf1, "..") == 0)
1145 else if (strcmp(buf2, "..") == 0)
1147 else if (strlen(buf1) == 0)
1149 else if (strlen(buf2) == 0)
1152 down1 = MAX(newname1.len, 3 + name2->len) <=
1153 MAX(3 + name1->len, newname2.len);
1155 free_pathname(&newname2);
1156 append_pathname(&newname2, "../");
1157 append_pathname(&newname2, name2->path);
1158 if (chdir(buf1) == 0) {
1159 rval = link_path(&newname1, &newname2);
1163 free_pathname(&newname1);
1164 append_pathname(&newname1, "../");
1165 append_pathname(&newname1, name1->path);
1166 if (chdir(buf2) == 0) {
1167 rval = link_path(&newname1, &newname2);
1172 free_pathname(&newname1);
1173 free_pathname(&newname2);
1178 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1180 char buf[NAME_MAX + 1];
1184 rval = lstat64(name->path, sbuf);
1185 if (rval >= 0 || errno != ENAMETOOLONG)
1187 separate_pathname(name, buf, &newname);
1188 if (chdir(buf) == 0) {
1189 rval = lstat64_path(&newname, sbuf);
1192 free_pathname(&newname);
1197 make_freq_table(void)
1203 for (p = ops, f = 0; p < ops_end; p++)
1205 freq_table = malloc(f * sizeof(*freq_table));
1206 freq_table_size = f;
1207 for (p = ops, i = 0; p < ops_end; p++) {
1208 for (f = 0; f < p->freq; f++, i++)
1209 freq_table[i] = p->op;
1214 mkdir_path(pathname_t *name, mode_t mode)
1216 char buf[NAME_MAX + 1];
1220 rval = mkdir(name->path, mode);
1221 if (rval >= 0 || errno != ENAMETOOLONG)
1223 separate_pathname(name, buf, &newname);
1224 if (chdir(buf) == 0) {
1225 rval = mkdir_path(&newname, mode);
1228 free_pathname(&newname);
1233 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1235 char buf[NAME_MAX + 1];
1239 rval = mknod(name->path, mode, dev);
1240 if (rval >= 0 || errno != ENAMETOOLONG)
1242 separate_pathname(name, buf, &newname);
1243 if (chdir(buf) == 0) {
1244 rval = mknod_path(&newname, mode, dev);
1247 free_pathname(&newname);
1252 namerandpad(int id, char *buf, int i)
1255 static int buckets[] =
1256 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1262 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1263 padmod = buckets[bucket] + 1 - i;
1266 padlen = (id ^ namerand) % padmod;
1268 memset(&buf[i], 'X', padlen);
1269 buf[i + padlen] = '\0';
1274 open_path(pathname_t *name, int oflag)
1276 char buf[NAME_MAX + 1];
1280 rval = open(name->path, oflag);
1281 if (rval >= 0 || errno != ENAMETOOLONG)
1283 separate_pathname(name, buf, &newname);
1284 if (chdir(buf) == 0) {
1285 rval = open_path(&newname, oflag);
1288 free_pathname(&newname);
1293 opendir_path(pathname_t *name)
1295 char buf[NAME_MAX + 1];
1299 rval = opendir(name->path);
1300 if (rval || errno != ENAMETOOLONG)
1302 separate_pathname(name, buf, &newname);
1303 if (chdir(buf) == 0) {
1304 rval = opendir_path(&newname);
1307 free_pathname(&newname);
1312 process_freq(char *arg)
1317 s = strchr(arg, '=');
1319 fprintf(stderr, "bad argument '%s'\n", arg);
1323 for (p = ops; p < ops_end; p++) {
1324 if (strcmp(arg, p->name) == 0) {
1329 fprintf(stderr, "can't find op type %s for -f\n", arg);
1334 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1336 char buf[NAME_MAX + 1];
1340 rval = readlink(name->path, lbuf, lbufsiz);
1341 if (rval >= 0 || errno != ENAMETOOLONG)
1343 separate_pathname(name, buf, &newname);
1344 if (chdir(buf) == 0) {
1345 rval = readlink_path(&newname, lbuf, lbufsiz);
1348 free_pathname(&newname);
1353 rename_path(pathname_t *name1, pathname_t *name2)
1355 char buf1[NAME_MAX + 1];
1356 char buf2[NAME_MAX + 1];
1358 pathname_t newname1;
1359 pathname_t newname2;
1362 rval = rename(name1->path, name2->path);
1363 if (rval >= 0 || errno != ENAMETOOLONG)
1365 separate_pathname(name1, buf1, &newname1);
1366 separate_pathname(name2, buf2, &newname2);
1367 if (strcmp(buf1, buf2) == 0) {
1368 if (chdir(buf1) == 0) {
1369 rval = rename_path(&newname1, &newname2);
1373 if (strcmp(buf1, "..") == 0)
1375 else if (strcmp(buf2, "..") == 0)
1377 else if (strlen(buf1) == 0)
1379 else if (strlen(buf2) == 0)
1382 down1 = MAX(newname1.len, 3 + name2->len) <=
1383 MAX(3 + name1->len, newname2.len);
1385 free_pathname(&newname2);
1386 append_pathname(&newname2, "../");
1387 append_pathname(&newname2, name2->path);
1388 if (chdir(buf1) == 0) {
1389 rval = rename_path(&newname1, &newname2);
1393 free_pathname(&newname1);
1394 append_pathname(&newname1, "../");
1395 append_pathname(&newname1, name1->path);
1396 if (chdir(buf2) == 0) {
1397 rval = rename_path(&newname1, &newname2);
1402 free_pathname(&newname1);
1403 free_pathname(&newname2);
1408 rmdir_path(pathname_t *name)
1410 char buf[NAME_MAX + 1];
1414 rval = rmdir(name->path);
1415 if (rval >= 0 || errno != ENAMETOOLONG)
1417 separate_pathname(name, buf, &newname);
1418 if (chdir(buf) == 0) {
1419 rval = rmdir_path(&newname);
1422 free_pathname(&newname);
1427 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1431 init_pathname(newname);
1432 slash = strchr(name->path, '/');
1433 if (slash == NULL) {
1438 strcpy(buf, name->path);
1440 append_pathname(newname, slash + 1);
1446 show_ops(int flag, char *lead_str)
1451 /* print in list form */
1454 for (p = ops; p < ops_end; p++) {
1455 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1456 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1457 x+=printf("%s ", p->name);
1460 } else if (flag == 0) {
1461 /* Table view style */
1463 for (f = 0, p = ops; p < ops_end; p++)
1469 for (p = ops; p < ops_end; p++) {
1470 if (flag != 0 || p->freq > 0) {
1471 if (lead_str != NULL)
1472 printf("%s", lead_str);
1473 printf("%20s %d/%d %s\n",
1474 p->name, p->freq, f,
1475 (p->iswrite == 0) ? " " : "write op");
1479 /* Command line style */
1480 if (lead_str != NULL)
1481 printf("%s", lead_str);
1482 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1484 for (p = ops; p < ops_end; p++)
1486 printf("-f %s=%d \\\n",p->name, p->freq);
1491 stat64_path(pathname_t *name, struct stat64 *sbuf)
1493 char buf[NAME_MAX + 1];
1497 rval = stat64(name->path, sbuf);
1498 if (rval >= 0 || errno != ENAMETOOLONG)
1500 separate_pathname(name, buf, &newname);
1501 if (chdir(buf) == 0) {
1502 rval = stat64_path(&newname, sbuf);
1505 free_pathname(&newname);
1510 symlink_path(const char *name1, pathname_t *name)
1512 char buf[NAME_MAX + 1];
1516 if (!strcmp(name1, name->path)) {
1517 printf("yikes! %s %s\n", name1, name->path);
1521 rval = symlink(name1, name->path);
1522 if (rval >= 0 || errno != ENAMETOOLONG)
1524 separate_pathname(name, buf, &newname);
1525 if (chdir(buf) == 0) {
1526 rval = symlink_path(name1, &newname);
1529 free_pathname(&newname);
1534 truncate64_path(pathname_t *name, off64_t length)
1536 char buf[NAME_MAX + 1];
1540 rval = truncate64(name->path, length);
1541 if (rval >= 0 || errno != ENAMETOOLONG)
1543 separate_pathname(name, buf, &newname);
1544 if (chdir(buf) == 0) {
1545 rval = truncate64_path(&newname, length);
1548 free_pathname(&newname);
1553 unlink_path(pathname_t *name)
1555 char buf[NAME_MAX + 1];
1559 rval = unlink(name->path);
1560 if (rval >= 0 || errno != ENAMETOOLONG)
1562 separate_pathname(name, buf, &newname);
1563 if (chdir(buf) == 0) {
1564 rval = unlink_path(&newname);
1567 free_pathname(&newname);
1574 printf("Usage: %s -H or\n", myprog);
1575 printf(" %s [-d dir][-e errtg][-f op_name=freq][-n nops]\n",
1577 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1579 printf(" -d dir specifies the base directory for operations\n");
1580 printf(" -e errtg specifies error injection stuff\n");
1581 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1582 printf(" the valid operation names are:\n");
1584 printf(" -i filenum get verbose output for this nth file object\n");
1585 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1586 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1587 printf(" -o logfile specifies logfile name\n");
1588 printf(" -p nproc specifies the no. of processes (default 1)\n");
1589 printf(" -r specifies random name padding\n");
1590 printf(" -s seed specifies the seed for the random generator (default random)\n");
1591 printf(" -v specifies verbose mode\n");
1592 printf(" -w zeros frequencies of non-write operations\n");
1593 printf(" -x cmd execute command in the middle of operations\n");
1594 printf(" -z zeros frequencies of all operations\n");
1595 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1596 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1597 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1598 printf(" -H prints usage and exits\n");
1606 for (p = ops; p < ops_end; p++) {
1617 for (p = ops; p < ops_end; p++)
1621 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1624 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1625 verifiable_log ? -1: (long)s->st_ino,
1626 (long)s->st_nlink, s->st_uid, s->st_gid,
1627 (long long) s->st_blocks, (long long) s->st_size);
1631 allocsp_f(int opno, long r)
1636 struct xfs_flock64 fl;
1644 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1646 printf("%d/%d: allocsp - no filename\n", procid, opno);
1650 fd = open_path(&f, O_RDWR);
1651 e = fd < 0 ? errno : 0;
1655 printf("%d/%d: allocsp - open %s failed %d\n",
1656 procid, opno, f.path, e);
1660 if (fstat64(fd, &stb) < 0) {
1662 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1663 procid, opno, f.path, errno);
1668 inode_info(st, sizeof(st), &stb, v);
1669 lr = ((__int64_t)random() << 32) + random();
1670 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1672 fl.l_whence = SEEK_SET;
1675 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1677 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1678 procid, opno, f.path, st, (long long)off, e);
1685 attr_remove_f(int opno, long r)
1687 attrlist_ent_t *aep;
1691 attrlist_cursor_t cursor;
1700 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1701 append_pathname(&f, ".");
1703 bzero(&cursor, sizeof(cursor));
1705 bzero(buf, sizeof(buf));
1706 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1710 alist = (attrlist_t *)buf;
1711 total += alist->al_count;
1712 } while (alist->al_more);
1715 printf("%d/%d: attr_remove - no attrs for %s\n",
1716 procid, opno, f.path);
1720 which = (int)(random() % total);
1721 bzero(&cursor, sizeof(cursor));
1725 bzero(buf, sizeof(buf));
1726 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1730 alist = (attrlist_t *)buf;
1731 if (which < ent + alist->al_count) {
1732 aep = (attrlist_ent_t *)
1733 &buf[alist->al_offset[which - ent]];
1734 aname = aep->a_name;
1737 ent += alist->al_count;
1738 } while (alist->al_more);
1739 if (aname == NULL) {
1742 "%d/%d: attr_remove - name %d not found at %s\n",
1743 procid, opno, which, f.path);
1747 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1750 printf("%d/%d: attr_remove %s %s %d\n",
1751 procid, opno, f.path, aname, e);
1756 attr_set_f(int opno, long r)
1763 static int lengths[] = { 10, 100, 1000, 10000 };
1768 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1769 append_pathname(&f, ".");
1770 sprintf(aname, "a%x", nameseq++);
1771 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1772 len = (int)(random() % lengths[li]);
1776 memset(aval, nameseq & 0xff, len);
1777 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1782 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1788 bulkstat_f(int opno, long r)
1796 xfs_fsop_bulkreq_t bsr;
1799 nent = (r % 999) + 2;
1800 t = malloc(nent * sizeof(*t));
1801 fd = open(".", O_RDONLY);
1809 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1813 printf("%d/%d: bulkstat nent %d total %lld\n",
1814 procid, opno, nent, (long long)total);
1819 bulkstat1_f(int opno, long r)
1829 xfs_fsop_bulkreq_t bsr;
1832 good = random() & 1;
1834 /* use an inode we know exists */
1836 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1837 append_pathname(&f, ".");
1838 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1843 * pick a random inode
1845 * note this can generate kernel warning messages
1846 * since bulkstat_one will read the disk block that
1847 * would contain a given inode even if that disk
1848 * block doesn't contain inodes.
1850 * this is detected later, but not until after the
1851 * warning is displayed.
1853 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1859 fd = open(".", O_RDONLY);
1865 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1867 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
1868 procid, opno, good?"real":"random",
1869 verifiable_log ? -1LL : (long long)ino, e);
1874 chown_f(int opno, long r)
1884 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1885 append_pathname(&f, ".");
1886 u = (uid_t)random();
1887 g = (gid_t)random();
1888 nbits = (int)(random() % idmodulo);
1889 u &= (1 << nbits) - 1;
1890 g &= (1 << nbits) - 1;
1891 e = lchown_path(&f, u, g) < 0 ? errno : 0;
1894 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
1899 setxattr_f(int opno, long r)
1901 #ifdef XFS_XFLAG_EXTSIZE
1911 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1912 append_pathname(&f, ".");
1913 fd = open_path(&f, O_RDWR);
1914 e = fd < 0 ? errno : 0;
1919 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
1920 nbits = (int)(random() % e);
1921 p &= (1 << nbits) - 1;
1923 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
1925 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
1928 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
1935 creat_f(int opno, long r)
1950 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1955 e1 = (random() % 100);
1956 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
1958 if (type == FT_RTF) /* rt always gets an extsize */
1959 extsize = (random() % 10) + 1;
1960 else if (e1 < 10) /* one-in-ten get an extsize */
1961 extsize = random() % 1024;
1965 e = generate_fname(fep, type, &f, &id, &v);
1969 (void)fent_to_name(&f, &flist[FT_DIR], fep);
1970 printf("%d/%d: creat - no filename from %s\n",
1971 procid, opno, f.path);
1976 fd = creat_path(&f, 0666);
1977 e = fd < 0 ? errno : 0;
1982 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
1983 if (type == FT_RTF) {
1984 a.fsx_xflags |= XFS_XFLAG_REALTIME;
1985 a.fsx_extsize = extsize *
1986 geom.rtextsize * geom.blocksize;
1988 } else if (extsize) {
1989 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
1990 a.fsx_extsize = extsize * geom.blocksize;
1993 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
1996 add_to_flist(type, id, parid);
2000 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2001 extsize ? a.fsx_extsize : 0, e, e1);
2002 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2008 dread_f(int opno, long r)
2012 struct dioattr diob;
2025 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2027 printf("%d/%d: dread - no filename\n", procid, opno);
2031 fd = open_path(&f, O_RDONLY|O_DIRECT);
2032 e = fd < 0 ? errno : 0;
2036 printf("%d/%d: dread - open %s failed %d\n",
2037 procid, opno, f.path, e);
2041 if (fstat64(fd, &stb) < 0) {
2043 printf("%d/%d: dread - fstat64 %s failed %d\n",
2044 procid, opno, f.path, errno);
2049 inode_info(st, sizeof(st), &stb, v);
2050 if (stb.st_size == 0) {
2052 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2058 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2061 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s failed %d\n",
2062 procid, opno, f.path, st, errno);
2068 dio_env = getenv("XFS_DIO_MIN");
2070 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2072 align = (__int64_t)diob.d_miniosz;
2073 lr = ((__int64_t)random() << 32) + random();
2074 off = (off64_t)(lr % stb.st_size);
2075 off -= (off % align);
2076 lseek64(fd, off, SEEK_SET);
2077 len = (random() % FILELEN_MAX) + 1;
2078 len -= (len % align);
2081 else if (len > diob.d_maxiosz)
2082 len = diob.d_maxiosz;
2083 buf = memalign(diob.d_mem, len);
2084 e = read(fd, buf, len) < 0 ? errno : 0;
2087 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2088 procid, opno, f.path, st, (long long)off, (int)len, e);
2094 dwrite_f(int opno, long r)
2098 struct dioattr diob;
2111 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2113 printf("%d/%d: dwrite - no filename\n", procid, opno);
2117 fd = open_path(&f, O_WRONLY|O_DIRECT);
2118 e = fd < 0 ? errno : 0;
2122 printf("%d/%d: dwrite - open %s failed %d\n",
2123 procid, opno, f.path, e);
2127 if (fstat64(fd, &stb) < 0) {
2129 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2130 procid, opno, f.path, errno);
2135 inode_info(st, sizeof(st), &stb, v);
2136 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2138 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2139 " %s%s failed %d\n",
2140 procid, opno, f.path, st, errno);
2146 dio_env = getenv("XFS_DIO_MIN");
2148 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2150 align = (__int64_t)diob.d_miniosz;
2151 lr = ((__int64_t)random() << 32) + random();
2152 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2153 off -= (off % align);
2154 lseek64(fd, off, SEEK_SET);
2155 len = (random() % FILELEN_MAX) + 1;
2156 len -= (len % align);
2159 else if (len > diob.d_maxiosz)
2160 len = diob.d_maxiosz;
2161 buf = memalign(diob.d_mem, len);
2163 lseek64(fd, off, SEEK_SET);
2164 memset(buf, nameseq & 0xff, len);
2165 e = write(fd, buf, len) < 0 ? errno : 0;
2168 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2169 procid, opno, f.path, st, (long long)off, (int)len, e);
2175 #ifdef HAVE_LINUX_FALLOC_H
2176 struct print_flags falloc_flags [] = {
2177 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2178 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2179 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2180 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2181 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2182 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2186 #define translate_falloc_flags(mode) \
2187 ({translate_flags(mode, "|", falloc_flags);})
2191 do_fallocate(int opno, long r, int mode)
2193 #ifdef HAVE_LINUX_FALLOC_H
2205 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2207 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2211 fd = open_path(&f, O_RDWR);
2214 printf("%d/%d: do_fallocate - open %s failed %d\n",
2215 procid, opno, f.path, errno);
2220 if (fstat64(fd, &stb) < 0) {
2222 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2223 procid, opno, f.path, errno);
2228 inode_info(st, sizeof(st), &stb, v);
2229 lr = ((__int64_t)random() << 32) + random();
2230 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2232 len = (off64_t)(random() % (1024 * 1024));
2234 * Collapse/insert range requires off and len to be block aligned,
2235 * make it more likely to be the case.
2237 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2239 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2240 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2242 mode |= FALLOC_FL_KEEP_SIZE & random();
2243 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2245 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2246 procid, opno, translate_falloc_flags(mode),
2247 f.path, st, (long long)off, (long long)len, e);
2254 fallocate_f(int opno, long r)
2256 #ifdef HAVE_LINUX_FALLOC_H
2257 do_fallocate(opno, r, 0);
2262 fdatasync_f(int opno, long r)
2270 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2272 printf("%d/%d: fdatasync - no filename\n",
2277 fd = open_path(&f, O_WRONLY);
2278 e = fd < 0 ? errno : 0;
2282 printf("%d/%d: fdatasync - open %s failed %d\n",
2283 procid, opno, f.path, e);
2287 e = fdatasync(fd) < 0 ? errno : 0;
2289 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2294 #ifdef HAVE_LINUX_FIEMAP_H
2295 struct print_flags fiemap_flags[] = {
2296 { FIEMAP_FLAG_SYNC, "SYNC"},
2297 { FIEMAP_FLAG_XATTR, "XATTR"},
2301 #define translate_fiemap_flags(mode) \
2302 ({translate_flags(mode, "|", fiemap_flags);})
2306 fiemap_f(int opno, long r)
2308 #ifdef HAVE_LINUX_FIEMAP_H
2318 struct fiemap *fiemap;
2321 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2323 printf("%d/%d: fiemap - no filename\n", procid, opno);
2327 fd = open_path(&f, O_RDWR);
2328 e = fd < 0 ? errno : 0;
2332 printf("%d/%d: fiemap - open %s failed %d\n",
2333 procid, opno, f.path, e);
2337 if (fstat64(fd, &stb) < 0) {
2339 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
2340 procid, opno, f.path, errno);
2345 inode_info(st, sizeof(st), &stb, v);
2346 blocks_to_map = random() & 0xffff;
2347 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
2348 (blocks_to_map * sizeof(struct fiemap_extent)));
2351 printf("%d/%d: malloc failed \n", procid, opno);
2356 lr = ((__int64_t)random() << 32) + random();
2357 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2359 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
2360 fiemap->fm_extent_count = blocks_to_map;
2361 fiemap->fm_mapped_extents = random() & 0xffff;
2362 fiemap->fm_start = off;
2363 fiemap->fm_length = ((__int64_t)random() << 32) + random();
2365 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
2367 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
2368 procid, opno, f.path, st, (long long)fiemap->fm_start,
2369 (long long) fiemap->fm_length,
2370 translate_fiemap_flags(fiemap->fm_flags), e);
2378 freesp_f(int opno, long r)
2383 struct xfs_flock64 fl;
2391 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2393 printf("%d/%d: freesp - no filename\n", procid, opno);
2397 fd = open_path(&f, O_RDWR);
2398 e = fd < 0 ? errno : 0;
2402 printf("%d/%d: freesp - open %s failed %d\n",
2403 procid, opno, f.path, e);
2407 if (fstat64(fd, &stb) < 0) {
2409 printf("%d/%d: freesp - fstat64 %s failed %d\n",
2410 procid, opno, f.path, errno);
2415 inode_info(st, sizeof(st), &stb, v);
2416 lr = ((__int64_t)random() << 32) + random();
2417 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2419 fl.l_whence = SEEK_SET;
2422 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
2424 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
2425 procid, opno, f.path, st, (long long)off, e);
2431 fsync_f(int opno, long r)
2439 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2441 printf("%d/%d: fsync - no filename\n", procid, opno);
2445 fd = open_path(&f, O_WRONLY);
2446 e = fd < 0 ? errno : 0;
2450 printf("%d/%d: fsync - open %s failed %d\n",
2451 procid, opno, f.path, e);
2455 e = fsync(fd) < 0 ? errno : 0;
2457 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
2463 getattr_f(int opno, long r)
2472 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2473 append_pathname(&f, ".");
2474 fd = open_path(&f, O_RDWR);
2475 e = fd < 0 ? errno : 0;
2478 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
2480 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
2486 getdents_f(int opno, long r)
2493 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
2494 append_pathname(&f, ".");
2495 dir = opendir_path(&f);
2499 printf("%d/%d: getdents - can't open %s\n",
2500 procid, opno, f.path);
2504 while (readdir64(dir) != NULL)
2507 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
2513 link_f(int opno, long r)
2526 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
2528 printf("%d/%d: link - no file\n", procid, opno);
2532 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
2538 e = generate_fname(fep, flp - flist, &l, &id, &v1);
2542 (void)fent_to_name(&l, &flist[FT_DIR], fep);
2543 printf("%d/%d: link - no filename from %s\n",
2544 procid, opno, l.path);
2550 e = link_path(&f, &l) < 0 ? errno : 0;
2553 add_to_flist(flp - flist, id, parid);
2555 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
2557 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
2564 mkdir_f(int opno, long r)
2574 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2579 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
2583 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2584 printf("%d/%d: mkdir - no filename from %s\n",
2585 procid, opno, f.path);
2590 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
2593 add_to_flist(FT_DIR, id, parid);
2595 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
2596 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
2602 mknod_f(int opno, long r)
2612 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2617 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2621 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2622 printf("%d/%d: mknod - no filename from %s\n",
2623 procid, opno, f.path);
2628 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2631 add_to_flist(FT_DEV, id, parid);
2633 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2634 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
2640 punch_f(int opno, long r)
2642 #ifdef HAVE_LINUX_FALLOC_H
2643 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
2648 zero_f(int opno, long r)
2650 #ifdef HAVE_LINUX_FALLOC_H
2651 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
2656 collapse_f(int opno, long r)
2658 #ifdef HAVE_LINUX_FALLOC_H
2659 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
2664 insert_f(int opno, long r)
2666 #ifdef HAVE_LINUX_FALLOC_H
2667 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
2672 read_f(int opno, long r)
2686 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2688 printf("%d/%d: read - no filename\n", procid, opno);
2692 fd = open_path(&f, O_RDONLY);
2693 e = fd < 0 ? errno : 0;
2697 printf("%d/%d: read - open %s failed %d\n",
2698 procid, opno, f.path, e);
2702 if (fstat64(fd, &stb) < 0) {
2704 printf("%d/%d: read - fstat64 %s failed %d\n",
2705 procid, opno, f.path, errno);
2710 inode_info(st, sizeof(st), &stb, v);
2711 if (stb.st_size == 0) {
2713 printf("%d/%d: read - %s%s zero size\n", procid, opno,
2719 lr = ((__int64_t)random() << 32) + random();
2720 off = (off64_t)(lr % stb.st_size);
2721 lseek64(fd, off, SEEK_SET);
2722 len = (random() % FILELEN_MAX) + 1;
2724 e = read(fd, buf, len) < 0 ? errno : 0;
2727 printf("%d/%d: read %s%s [%lld,%d] %d\n",
2728 procid, opno, f.path, st, (long long)off, (int)len, e);
2734 readlink_f(int opno, long r)
2742 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2744 printf("%d/%d: readlink - no filename\n", procid, opno);
2748 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2751 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2756 rename_f(int opno, long r)
2770 /* get an existing path for the source of the rename */
2772 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2774 printf("%d/%d: rename - no filename\n", procid, opno);
2779 /* get an existing directory for the destination parent directory name */
2780 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2786 /* generate a new path using an existing parent directory in name */
2787 init_pathname(&newf);
2788 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2792 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
2793 printf("%d/%d: rename - no filename from %s\n",
2794 procid, opno, f.path);
2796 free_pathname(&newf);
2800 e = rename_path(&f, &newf) < 0 ? errno : 0;
2803 if (flp - flist == FT_DIR) {
2805 fix_parent(oldid, id);
2807 del_from_flist(flp - flist, fep - flp->fents);
2808 add_to_flist(flp - flist, id, parid);
2811 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
2814 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
2815 procid, opno, fep->id, fep->parent);
2816 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
2817 procid, opno, id, parid);
2820 free_pathname(&newf);
2825 resvsp_f(int opno, long r)
2830 struct xfs_flock64 fl;
2838 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2840 printf("%d/%d: resvsp - no filename\n", procid, opno);
2844 fd = open_path(&f, O_RDWR);
2845 e = fd < 0 ? errno : 0;
2849 printf("%d/%d: resvsp - open %s failed %d\n",
2850 procid, opno, f.path, e);
2854 if (fstat64(fd, &stb) < 0) {
2856 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
2857 procid, opno, f.path, errno);
2862 inode_info(st, sizeof(st), &stb, v);
2863 lr = ((__int64_t)random() << 32) + random();
2864 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2866 fl.l_whence = SEEK_SET;
2868 fl.l_len = (off64_t)(random() % (1024 * 1024));
2869 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2871 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
2872 procid, opno, f.path, st,
2873 (long long)off, (long long)fl.l_len, e);
2879 rmdir_f(int opno, long r)
2887 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2889 printf("%d/%d: rmdir - no directory\n", procid, opno);
2893 e = rmdir_path(&f) < 0 ? errno : 0;
2896 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2898 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2900 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
2901 procid, opno, fep->id, fep->parent);
2907 setattr_f(int opno, long r)
2916 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2917 append_pathname(&f, ".");
2918 fd = open_path(&f, O_RDWR);
2919 e = fd < 0 ? errno : 0;
2922 fl = attr_mask & (uint)random();
2923 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
2925 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
2931 stat_f(int opno, long r)
2939 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2941 printf("%d/%d: stat - no entries\n", procid, opno);
2945 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2948 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2953 symlink_f(int opno, long r)
2966 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2971 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2975 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2976 printf("%d/%d: symlink - no filename from %s\n",
2977 procid, opno, f.path);
2982 len = (int)(random() % PATH_MAX);
2983 val = malloc(len + 1);
2985 memset(val, 'x', len);
2987 for (i = 10; i < len - 1; i += 10)
2989 e = symlink_path(val, &f) < 0 ? errno : 0;
2992 add_to_flist(FT_SYM, id, parid);
2995 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
2996 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3003 sync_f(int opno, long r)
3007 printf("%d/%d: sync\n", procid, opno);
3011 truncate_f(int opno, long r)
3022 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3024 printf("%d/%d: truncate - no filename\n", procid, opno);
3028 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3032 printf("%d/%d: truncate - stat64 %s failed %d\n",
3033 procid, opno, f.path, e);
3037 inode_info(st, sizeof(st), &stb, v);
3038 lr = ((__int64_t)random() << 32) + random();
3039 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3041 e = truncate64_path(&f, off) < 0 ? errno : 0;
3044 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3045 st, (long long)off, e);
3050 unlink_f(int opno, long r)
3059 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3061 printf("%d/%d: unlink - no file\n", procid, opno);
3065 e = unlink_path(&f) < 0 ? errno : 0;
3068 del_from_flist(flp - flist, fep - flp->fents);
3070 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3072 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3073 procid, opno, fep->id, fep->parent);
3079 unresvsp_f(int opno, long r)
3084 struct xfs_flock64 fl;
3092 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3094 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3098 fd = open_path(&f, O_RDWR);
3099 e = fd < 0 ? errno : 0;
3103 printf("%d/%d: unresvsp - open %s failed %d\n",
3104 procid, opno, f.path, e);
3108 if (fstat64(fd, &stb) < 0) {
3110 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3111 procid, opno, f.path, errno);
3116 inode_info(st, sizeof(st), &stb, v);
3117 lr = ((__int64_t)random() << 32) + random();
3118 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3120 fl.l_whence = SEEK_SET;
3122 fl.l_len = (off64_t)(random() % (1 << 20));
3123 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
3125 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
3126 procid, opno, f.path, st,
3127 (long long)off, (long long)fl.l_len, e);
3133 write_f(int opno, long r)
3147 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
3149 printf("%d/%d: write - no filename\n", procid, opno);
3153 fd = open_path(&f, O_WRONLY);
3154 e = fd < 0 ? errno : 0;
3158 printf("%d/%d: write - open %s failed %d\n",
3159 procid, opno, f.path, e);
3163 if (fstat64(fd, &stb) < 0) {
3165 printf("%d/%d: write - fstat64 %s failed %d\n",
3166 procid, opno, f.path, errno);
3171 inode_info(st, sizeof(st), &stb, v);
3172 lr = ((__int64_t)random() << 32) + random();
3173 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3175 lseek64(fd, off, SEEK_SET);
3176 len = (random() % FILELEN_MAX) + 1;
3178 memset(buf, nameseq & 0xff, len);
3179 e = write(fd, buf, len) < 0 ? errno : 0;
3182 printf("%d/%d: write %s%s [%lld,%d] %d\n",
3183 procid, opno, f.path, st, (long long)off, (int)len, e);