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
22 #ifdef HAVE_ATTR_XATTR_H
23 #include <attr/xattr.h>
25 #ifdef HAVE_ATTR_ATTRIBUTES_H
26 #include <attr/attributes.h>
28 #ifdef HAVE_LINUX_FIEMAP_H
29 #include <linux/fiemap.h>
31 #ifndef HAVE_ATTR_LIST
32 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
34 #ifdef HAVE_SYS_PRCTL_H
35 #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;
263 int verifiable_log = 0;
264 sig_atomic_t should_stop = 0;
265 char *execute_cmd = NULL;
266 int execute_freq = 1;
267 struct print_string flag_str = {0};
269 void add_to_flist(int, int, int);
270 void append_pathname(pathname_t *, char *);
271 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
272 int attr_remove_path(pathname_t *, const char *, int);
273 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
274 void check_cwd(void);
275 int creat_path(pathname_t *, mode_t);
276 void dcache_enter(int, int);
277 void dcache_init(void);
278 fent_t *dcache_lookup(int);
279 void dcache_purge(int);
280 void del_from_flist(int, int);
281 int dirid_to_name(char *, int);
283 int fent_to_name(pathname_t *, flist_t *, fent_t *);
284 void fix_parent(int, int);
285 void free_pathname(pathname_t *);
286 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
287 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
288 void init_pathname(pathname_t *);
289 int lchown_path(pathname_t *, uid_t, gid_t);
290 int link_path(pathname_t *, pathname_t *);
291 int lstat64_path(pathname_t *, struct stat64 *);
292 void make_freq_table(void);
293 int mkdir_path(pathname_t *, mode_t);
294 int mknod_path(pathname_t *, mode_t, dev_t);
295 void namerandpad(int, char *, int);
296 int open_path(pathname_t *, int);
297 DIR *opendir_path(pathname_t *);
298 void process_freq(char *);
299 int readlink_path(pathname_t *, char *, size_t);
300 int rename_path(pathname_t *, pathname_t *);
301 int rmdir_path(pathname_t *);
302 void separate_pathname(pathname_t *, char *, pathname_t *);
303 void show_ops(int, char *);
304 int stat64_path(pathname_t *, struct stat64 *);
305 int symlink_path(const char *, pathname_t *);
306 int truncate64_path(pathname_t *, off64_t);
307 int unlink_path(pathname_t *);
309 void write_freq(void);
310 void zero_freq(void);
312 void sg_handler(int signum)
317 int main(int argc, char **argv)
321 char *dirname = NULL;
322 char *logname = NULL;
323 char rpath[PATH_MAX];
332 xfs_error_injection_t err_inj;
333 struct sigaction action;
335 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
337 errrange = errtag = 0;
339 nops = sizeof(ops) / sizeof(ops[0]);
340 ops_end = &ops[nops];
342 while ((c = getopt(argc, argv, allopts)) != -1) {
351 sscanf(optarg, "%d", &errtag);
355 } else if (errtag == 0)
357 if (errtag >= XFS_ERRTAG_MAX) {
359 "error tag %d too large (max %d)\n",
360 errtag, XFS_ERRTAG_MAX - 1);
365 process_freq(optarg);
368 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
369 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
372 idmodulo = strtoul(optarg, NULL, 0);
373 if (idmodulo > XFS_IDMODULO_MAX) {
375 "chown modulo %d too big (max %d)\n",
376 idmodulo, XFS_IDMODULO_MAX);
381 loops = atoi(optarg);
384 operations = atoi(optarg);
391 nproc = atoi(optarg);
397 seed = strtoul(optarg, NULL, 0);
406 execute_cmd = optarg;
412 attr_mask = strtoul(optarg, NULL, 0);
416 if (optarg[0] == 'c')
427 execute_freq = strtoul(optarg, NULL, 0);
430 fprintf(stderr, "%s - invalid parameters\n",
440 /* no directory specified */
441 if (!nousage) usage();
445 (void)mkdir(dirname, 0777);
446 if (logname && logname[0] != '/') {
447 if (getcwd(rpath, sizeof(rpath)) < 0){
448 perror("getcwd failed");
454 if (chdir(dirname) < 0) {
460 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
461 if (freopen(path, "a", stdout) == NULL) {
462 perror("freopen logfile failed");
466 sprintf(buf, "fss%x", (unsigned int)getpid());
467 fd = creat(buf, 0666);
468 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
469 maxfsize = (off64_t)MAXFSIZE32;
471 maxfsize = (off64_t)MAXFSIZE;
476 gettimeofday(&t, (void *)NULL);
477 seed = (int)t.tv_sec ^ (int)t.tv_usec;
478 printf("seed = %ld\n", seed);
480 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
481 if (i >= 0 && geom.rtblocks)
482 rtpct = MIN(MAX(geom.rtblocks * 100 /
483 (geom.rtblocks + geom.datablocks), 1), 99);
492 for (i = 0; i < j; i++)
495 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
501 for (i = 0; i < j; i++)
504 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
506 printf("Injecting failure on tag #%d\n", errtag);
507 err_inj.errtag = errtag;
509 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
511 perror("fsstress - XFS_SYSSGI error injection call");
520 action.sa_handler = sg_handler;
521 sigemptyset(&action.sa_mask);
523 if (sigaction(SIGTERM, &action, 0)) {
524 perror("sigaction failed");
528 for (i = 0; i < nproc; i++) {
530 action.sa_handler = SIG_DFL;
531 sigemptyset(&action.sa_mask);
532 if (sigaction(SIGTERM, &action, 0))
534 #ifdef HAVE_SYS_PRCTL_H
535 prctl(PR_SET_PDEATHSIG, SIGKILL);
536 if (getppid() == 1) /* parent died already? */
541 snprintf(path, sizeof(path), "%s/%s.%d",
543 if (freopen(path, "a", stdout) == NULL) {
544 perror("freopen logfile failed");
549 for (i = 0; !loops || (i < loops); i++)
554 while (wait(&stat) > 0 && !should_stop) {
557 action.sa_flags = SA_RESTART;
558 sigaction(SIGTERM, &action, 0);
559 kill(-getpid(), SIGTERM);
560 while (wait(&stat) > 0)
566 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
568 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
570 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
582 add_string(struct print_string *str, const char *add)
584 int len = strlen(add);
589 if (len > (str->max - 1) - str->len) {
590 str->len = str->max - 1;
594 memcpy(str->buffer + str->len, add, len);
596 str->buffer[str->len] = '\0';
602 translate_flags(int flags, const char *delim,
603 const struct print_flags *flag_array)
605 int i, mask, first = 1;
608 if (!flag_str.buffer) {
609 flag_str.buffer = malloc(4096);
613 if (!flag_str.buffer)
616 flag_str.buffer[0] = '\0';
618 for (i = 0; flag_array[i].name && flags; i++) {
619 mask = flag_array[i].mask;
620 if ((flags & mask) != mask)
623 add = flag_array[i].name;
626 add_string(&flag_str, delim);
629 add_string(&flag_str, add);
632 /* Check whether there are any leftover flags. */
638 add_string(&flag_str, delim);
640 ret = snprintf(number, 11, "0x%x", flags) > 0;
641 if (ret > 0 && ret <= 11)
642 add_string(&flag_str, number);
645 return flag_str.buffer;
649 add_to_flist(int ft, int id, int parent)
655 if (ftp->nfiles == ftp->nslots) {
656 ftp->nslots += FLIST_SLOT_INCR;
657 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
659 fep = &ftp->fents[ftp->nfiles++];
661 fep->parent = parent;
665 append_pathname(pathname_t *name, char *str)
671 /* attempting to append to a dir a zero length path */
672 if (len && *str == '/' && name->len == 0) {
673 fprintf(stderr, "fsstress: append_pathname failure\n");
679 name->path = realloc(name->path, name->len + 1 + len);
680 strcpy(&name->path[name->len], str);
685 attr_list_path(pathname_t *name,
687 const int buffersize,
689 attrlist_cursor_t *cursor)
691 char buf[NAME_MAX + 1];
695 if (flags != ATTR_DONTFOLLOW) {
700 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
701 if (rval >= 0 || errno != ENAMETOOLONG)
703 separate_pathname(name, buf, &newname);
704 if (chdir(buf) == 0) {
705 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
708 free_pathname(&newname);
713 attr_remove_path(pathname_t *name, const char *attrname, int flags)
715 char buf[NAME_MAX + 1];
719 rval = attr_remove(name->path, attrname, flags);
720 if (rval >= 0 || errno != ENAMETOOLONG)
722 separate_pathname(name, buf, &newname);
723 if (chdir(buf) == 0) {
724 rval = attr_remove_path(&newname, attrname, flags);
727 free_pathname(&newname);
732 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
733 const int valuelength, int flags)
735 char buf[NAME_MAX + 1];
739 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
740 if (rval >= 0 || errno != ENAMETOOLONG)
742 separate_pathname(name, buf, &newname);
743 if (chdir(buf) == 0) {
744 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
748 free_pathname(&newname);
756 struct stat64 statbuf;
758 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
761 fprintf(stderr, "fsstress: check_cwd failure\n");
768 creat_path(pathname_t *name, mode_t mode)
770 char buf[NAME_MAX + 1];
774 rval = creat(name->path, mode);
775 if (rval >= 0 || errno != ENAMETOOLONG)
777 separate_pathname(name, buf, &newname);
778 if (chdir(buf) == 0) {
779 rval = creat_path(&newname, mode);
782 free_pathname(&newname);
787 dcache_enter(int dirid, int slot)
789 dcache[dirid % NDCACHE] = slot;
797 for (i = 0; i < NDCACHE; i++)
802 dcache_lookup(int dirid)
807 i = dcache[dirid % NDCACHE];
808 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
814 dcache_purge(int dirid)
818 dcp = &dcache[dirid % NDCACHE];
819 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
824 * Delete the item from the list by
825 * moving last entry over the deleted one;
826 * unless deleted entry is the last one.
827 * Input: which file list array and which slot in array
830 del_from_flist(int ft, int slot)
836 dcache_purge(ftp->fents[slot].id);
837 if (slot != ftp->nfiles - 1) {
839 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
840 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
846 dirid_to_fent(int dirid)
852 if ((fep = dcache_lookup(dirid)))
854 flp = &flist[FT_DIR];
855 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
856 if (fep->id == dirid) {
857 dcache_enter(dirid, fep - flp->fents);
867 struct stat64 statbuf;
875 dividend = (operations + execute_freq) / (execute_freq + 1);
876 sprintf(buf, "p%x", procid);
877 (void)mkdir(buf, 0777);
878 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
882 top_ino = statbuf.st_ino;
883 homedir = getcwd(NULL, -1);
888 for (opno = 0; opno < operations; opno++) {
889 if (execute_cmd && opno && opno % dividend == 0) {
891 printf("%d: execute command %s\n", opno,
893 rval = system(execute_cmd);
895 fprintf(stderr, "execute command failed with "
898 p = &ops[freq_table[random() % freq_table_size]];
899 p->func(opno, random());
901 * test for forced shutdown by stat'ing the test
902 * directory. If this stat returns EIO, assume
903 * the forced shutdown happened.
905 if (errtag != 0 && opno % 100 == 0) {
906 rval = stat64(".", &statbuf);
908 fprintf(stderr, "Detected EIO\n");
916 sprintf(cmd, "rm -rf %s", buf);
922 * build up a pathname going thru the file entry and all
924 * Return 0 on error, 1 on success;
927 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
929 char buf[NAME_MAX + 1];
937 /* build up parent directory name */
938 if (fep->parent != -1) {
939 pfep = dirid_to_fent(fep->parent);
942 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
943 procid, fep->id, fep->parent);
948 e = fent_to_name(name, &flist[FT_DIR], pfep);
951 append_pathname(name, "/");
954 i = sprintf(buf, "%c%x", flp->tag, fep->id);
955 namerandpad(fep->id, buf, i);
956 append_pathname(name, buf);
961 fix_parent(int oldid, int newid)
968 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
969 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
970 if (fep->parent == oldid)
977 free_pathname(pathname_t *name)
987 * Generate a filename of type ft.
988 * If we have a fep which should be a directory then use it
989 * as the parent path for this new filename i.e. prepend with it.
992 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
994 char buf[NAME_MAX + 1];
1003 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1004 namerandpad(id, buf, len);
1006 /* prepend fep parent dir-name to it */
1008 e = fent_to_name(name, &flist[FT_DIR], fep);
1011 append_pathname(name, "/");
1013 append_pathname(name, buf);
1017 for (j = 0; !*v && j < ilistlen; j++) {
1018 if (ilist[j] == id) {
1028 * Input: "which" to choose the file-types eg. non-directory
1029 * Input: "r" to choose which file
1030 * Output: file-list, file-entry, name for the chosen file.
1031 * Output: verbose if chosen file is on the ilist.
1034 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1037 int totalsum = 0; /* total number of matching files */
1038 int partialsum = 0; /* partial sum of matching files */
1044 int e = 1; /* success */
1047 * go thru flist and add up number of files for each
1048 * category that matches with <which>.
1050 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1051 if (which & (1 << i))
1052 totalsum += flp->nfiles;
1054 if (totalsum == 0) {
1064 * Now we have possible matches between 0..totalsum-1.
1065 * And we use r to help us choose which one we want,
1066 * which when bounded by totalsum becomes x.
1068 x = (int)(r % totalsum);
1069 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1070 if (which & (1 << i)) {
1071 if (x < partialsum + flp->nfiles) {
1073 /* found the matching file entry */
1074 fep = &flp->fents[x - partialsum];
1076 /* fill-in what we were asked for */
1078 e = fent_to_name(name, flp, fep);
1081 fprintf(stderr, "%d: failed to get path for entry:"
1082 " id=%d,parent=%d\n",
1083 procid, fep->id, fep->parent);
1092 /* turn on verbose if its an ilisted file */
1094 for (j = 0; !*v && j < ilistlen; j++) {
1095 if (ilist[j] == fep->id) {
1102 partialsum += flp->nfiles;
1106 fprintf(stderr, "fsstress: get_fname failure\n");
1113 init_pathname(pathname_t *name)
1120 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1122 char buf[NAME_MAX + 1];
1126 rval = lchown(name->path, owner, group);
1127 if (rval >= 0 || errno != ENAMETOOLONG)
1129 separate_pathname(name, buf, &newname);
1130 if (chdir(buf) == 0) {
1131 rval = lchown_path(&newname, owner, group);
1134 free_pathname(&newname);
1139 link_path(pathname_t *name1, pathname_t *name2)
1141 char buf1[NAME_MAX + 1];
1142 char buf2[NAME_MAX + 1];
1144 pathname_t newname1;
1145 pathname_t newname2;
1148 rval = link(name1->path, name2->path);
1149 if (rval >= 0 || errno != ENAMETOOLONG)
1151 separate_pathname(name1, buf1, &newname1);
1152 separate_pathname(name2, buf2, &newname2);
1153 if (strcmp(buf1, buf2) == 0) {
1154 if (chdir(buf1) == 0) {
1155 rval = link_path(&newname1, &newname2);
1159 if (strcmp(buf1, "..") == 0)
1161 else if (strcmp(buf2, "..") == 0)
1163 else if (strlen(buf1) == 0)
1165 else if (strlen(buf2) == 0)
1168 down1 = MAX(newname1.len, 3 + name2->len) <=
1169 MAX(3 + name1->len, newname2.len);
1171 free_pathname(&newname2);
1172 append_pathname(&newname2, "../");
1173 append_pathname(&newname2, name2->path);
1174 if (chdir(buf1) == 0) {
1175 rval = link_path(&newname1, &newname2);
1179 free_pathname(&newname1);
1180 append_pathname(&newname1, "../");
1181 append_pathname(&newname1, name1->path);
1182 if (chdir(buf2) == 0) {
1183 rval = link_path(&newname1, &newname2);
1188 free_pathname(&newname1);
1189 free_pathname(&newname2);
1194 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1196 char buf[NAME_MAX + 1];
1200 rval = lstat64(name->path, sbuf);
1201 if (rval >= 0 || errno != ENAMETOOLONG)
1203 separate_pathname(name, buf, &newname);
1204 if (chdir(buf) == 0) {
1205 rval = lstat64_path(&newname, sbuf);
1208 free_pathname(&newname);
1213 make_freq_table(void)
1219 for (p = ops, f = 0; p < ops_end; p++)
1221 freq_table = malloc(f * sizeof(*freq_table));
1222 freq_table_size = f;
1223 for (p = ops, i = 0; p < ops_end; p++) {
1224 for (f = 0; f < p->freq; f++, i++)
1225 freq_table[i] = p->op;
1230 mkdir_path(pathname_t *name, mode_t mode)
1232 char buf[NAME_MAX + 1];
1236 rval = mkdir(name->path, mode);
1237 if (rval >= 0 || errno != ENAMETOOLONG)
1239 separate_pathname(name, buf, &newname);
1240 if (chdir(buf) == 0) {
1241 rval = mkdir_path(&newname, mode);
1244 free_pathname(&newname);
1249 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1251 char buf[NAME_MAX + 1];
1255 rval = mknod(name->path, mode, dev);
1256 if (rval >= 0 || errno != ENAMETOOLONG)
1258 separate_pathname(name, buf, &newname);
1259 if (chdir(buf) == 0) {
1260 rval = mknod_path(&newname, mode, dev);
1263 free_pathname(&newname);
1268 namerandpad(int id, char *buf, int i)
1271 static int buckets[] =
1272 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1278 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1279 padmod = buckets[bucket] + 1 - i;
1282 padlen = (id ^ namerand) % padmod;
1284 memset(&buf[i], 'X', padlen);
1285 buf[i + padlen] = '\0';
1290 open_path(pathname_t *name, int oflag)
1292 char buf[NAME_MAX + 1];
1296 rval = open(name->path, oflag);
1297 if (rval >= 0 || errno != ENAMETOOLONG)
1299 separate_pathname(name, buf, &newname);
1300 if (chdir(buf) == 0) {
1301 rval = open_path(&newname, oflag);
1304 free_pathname(&newname);
1309 opendir_path(pathname_t *name)
1311 char buf[NAME_MAX + 1];
1315 rval = opendir(name->path);
1316 if (rval || errno != ENAMETOOLONG)
1318 separate_pathname(name, buf, &newname);
1319 if (chdir(buf) == 0) {
1320 rval = opendir_path(&newname);
1323 free_pathname(&newname);
1328 process_freq(char *arg)
1333 s = strchr(arg, '=');
1335 fprintf(stderr, "bad argument '%s'\n", arg);
1339 for (p = ops; p < ops_end; p++) {
1340 if (strcmp(arg, p->name) == 0) {
1345 fprintf(stderr, "can't find op type %s for -f\n", arg);
1350 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1352 char buf[NAME_MAX + 1];
1356 rval = readlink(name->path, lbuf, lbufsiz);
1357 if (rval >= 0 || errno != ENAMETOOLONG)
1359 separate_pathname(name, buf, &newname);
1360 if (chdir(buf) == 0) {
1361 rval = readlink_path(&newname, lbuf, lbufsiz);
1364 free_pathname(&newname);
1369 rename_path(pathname_t *name1, pathname_t *name2)
1371 char buf1[NAME_MAX + 1];
1372 char buf2[NAME_MAX + 1];
1374 pathname_t newname1;
1375 pathname_t newname2;
1378 rval = rename(name1->path, name2->path);
1379 if (rval >= 0 || errno != ENAMETOOLONG)
1381 separate_pathname(name1, buf1, &newname1);
1382 separate_pathname(name2, buf2, &newname2);
1383 if (strcmp(buf1, buf2) == 0) {
1384 if (chdir(buf1) == 0) {
1385 rval = rename_path(&newname1, &newname2);
1389 if (strcmp(buf1, "..") == 0)
1391 else if (strcmp(buf2, "..") == 0)
1393 else if (strlen(buf1) == 0)
1395 else if (strlen(buf2) == 0)
1398 down1 = MAX(newname1.len, 3 + name2->len) <=
1399 MAX(3 + name1->len, newname2.len);
1401 free_pathname(&newname2);
1402 append_pathname(&newname2, "../");
1403 append_pathname(&newname2, name2->path);
1404 if (chdir(buf1) == 0) {
1405 rval = rename_path(&newname1, &newname2);
1409 free_pathname(&newname1);
1410 append_pathname(&newname1, "../");
1411 append_pathname(&newname1, name1->path);
1412 if (chdir(buf2) == 0) {
1413 rval = rename_path(&newname1, &newname2);
1418 free_pathname(&newname1);
1419 free_pathname(&newname2);
1424 rmdir_path(pathname_t *name)
1426 char buf[NAME_MAX + 1];
1430 rval = rmdir(name->path);
1431 if (rval >= 0 || errno != ENAMETOOLONG)
1433 separate_pathname(name, buf, &newname);
1434 if (chdir(buf) == 0) {
1435 rval = rmdir_path(&newname);
1438 free_pathname(&newname);
1443 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1447 init_pathname(newname);
1448 slash = strchr(name->path, '/');
1449 if (slash == NULL) {
1454 strcpy(buf, name->path);
1456 append_pathname(newname, slash + 1);
1462 show_ops(int flag, char *lead_str)
1467 /* print in list form */
1470 for (p = ops; p < ops_end; p++) {
1471 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1472 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1473 x+=printf("%s ", p->name);
1476 } else if (flag == 0) {
1477 /* Table view style */
1479 for (f = 0, p = ops; p < ops_end; p++)
1485 for (p = ops; p < ops_end; p++) {
1486 if (flag != 0 || p->freq > 0) {
1487 if (lead_str != NULL)
1488 printf("%s", lead_str);
1489 printf("%20s %d/%d %s\n",
1490 p->name, p->freq, f,
1491 (p->iswrite == 0) ? " " : "write op");
1495 /* Command line style */
1496 if (lead_str != NULL)
1497 printf("%s", lead_str);
1498 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1500 for (p = ops; p < ops_end; p++)
1502 printf("-f %s=%d \\\n",p->name, p->freq);
1507 stat64_path(pathname_t *name, struct stat64 *sbuf)
1509 char buf[NAME_MAX + 1];
1513 rval = stat64(name->path, sbuf);
1514 if (rval >= 0 || errno != ENAMETOOLONG)
1516 separate_pathname(name, buf, &newname);
1517 if (chdir(buf) == 0) {
1518 rval = stat64_path(&newname, sbuf);
1521 free_pathname(&newname);
1526 symlink_path(const char *name1, pathname_t *name)
1528 char buf[NAME_MAX + 1];
1532 if (!strcmp(name1, name->path)) {
1533 printf("yikes! %s %s\n", name1, name->path);
1537 rval = symlink(name1, name->path);
1538 if (rval >= 0 || errno != ENAMETOOLONG)
1540 separate_pathname(name, buf, &newname);
1541 if (chdir(buf) == 0) {
1542 rval = symlink_path(name1, &newname);
1545 free_pathname(&newname);
1550 truncate64_path(pathname_t *name, off64_t length)
1552 char buf[NAME_MAX + 1];
1556 rval = truncate64(name->path, length);
1557 if (rval >= 0 || errno != ENAMETOOLONG)
1559 separate_pathname(name, buf, &newname);
1560 if (chdir(buf) == 0) {
1561 rval = truncate64_path(&newname, length);
1564 free_pathname(&newname);
1569 unlink_path(pathname_t *name)
1571 char buf[NAME_MAX + 1];
1575 rval = unlink(name->path);
1576 if (rval >= 0 || errno != ENAMETOOLONG)
1578 separate_pathname(name, buf, &newname);
1579 if (chdir(buf) == 0) {
1580 rval = unlink_path(&newname);
1583 free_pathname(&newname);
1590 printf("Usage: %s -H or\n", myprog);
1591 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1593 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1595 printf(" -c clean up the test directory after each run\n");
1596 printf(" -d dir specifies the base directory for operations\n");
1597 printf(" -e errtg specifies error injection stuff\n");
1598 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1599 printf(" the valid operation names are:\n");
1601 printf(" -i filenum get verbose output for this nth file object\n");
1602 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1603 printf(" *use 0 for infinite (default 1)\n");
1604 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1605 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1606 printf(" -o logfile specifies logfile name\n");
1607 printf(" -p nproc specifies the no. of processes (default 1)\n");
1608 printf(" -r specifies random name padding\n");
1609 printf(" -s seed specifies the seed for the random generator (default random)\n");
1610 printf(" -v specifies verbose mode\n");
1611 printf(" -w zeros frequencies of non-write operations\n");
1612 printf(" -x cmd execute command in the middle of operations\n");
1613 printf(" -z zeros frequencies of all operations\n");
1614 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1615 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1616 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1617 printf(" -H prints usage and exits\n");
1625 for (p = ops; p < ops_end; p++) {
1636 for (p = ops; p < ops_end; p++)
1640 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1643 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1644 verifiable_log ? -1: (long)s->st_ino,
1645 (long)s->st_nlink, s->st_uid, s->st_gid,
1646 (long long) s->st_blocks, (long long) s->st_size);
1650 allocsp_f(int opno, long r)
1655 struct xfs_flock64 fl;
1663 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1665 printf("%d/%d: allocsp - no filename\n", procid, opno);
1669 fd = open_path(&f, O_RDWR);
1670 e = fd < 0 ? errno : 0;
1674 printf("%d/%d: allocsp - open %s failed %d\n",
1675 procid, opno, f.path, e);
1679 if (fstat64(fd, &stb) < 0) {
1681 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1682 procid, opno, f.path, errno);
1687 inode_info(st, sizeof(st), &stb, v);
1688 lr = ((__int64_t)random() << 32) + random();
1689 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1691 fl.l_whence = SEEK_SET;
1694 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1696 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1697 procid, opno, f.path, st, (long long)off, e);
1704 attr_remove_f(int opno, long r)
1706 attrlist_ent_t *aep;
1710 attrlist_cursor_t cursor;
1719 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1720 append_pathname(&f, ".");
1722 bzero(&cursor, sizeof(cursor));
1724 bzero(buf, sizeof(buf));
1725 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1729 alist = (attrlist_t *)buf;
1730 total += alist->al_count;
1731 } while (alist->al_more);
1734 printf("%d/%d: attr_remove - no attrs for %s\n",
1735 procid, opno, f.path);
1739 which = (int)(random() % total);
1740 bzero(&cursor, sizeof(cursor));
1744 bzero(buf, sizeof(buf));
1745 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
1749 alist = (attrlist_t *)buf;
1750 if (which < ent + alist->al_count) {
1751 aep = (attrlist_ent_t *)
1752 &buf[alist->al_offset[which - ent]];
1753 aname = aep->a_name;
1756 ent += alist->al_count;
1757 } while (alist->al_more);
1758 if (aname == NULL) {
1761 "%d/%d: attr_remove - name %d not found at %s\n",
1762 procid, opno, which, f.path);
1766 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
1769 printf("%d/%d: attr_remove %s %s %d\n",
1770 procid, opno, f.path, aname, e);
1775 attr_set_f(int opno, long r)
1782 static int lengths[] = { 10, 100, 1000, 10000 };
1787 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1788 append_pathname(&f, ".");
1789 sprintf(aname, "a%x", nameseq++);
1790 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
1791 len = (int)(random() % lengths[li]);
1795 memset(aval, nameseq & 0xff, len);
1796 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
1801 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
1807 bulkstat_f(int opno, long r)
1815 xfs_fsop_bulkreq_t bsr;
1818 nent = (r % 999) + 2;
1819 t = malloc(nent * sizeof(*t));
1820 fd = open(".", O_RDONLY);
1828 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
1832 printf("%d/%d: bulkstat nent %d total %lld\n",
1833 procid, opno, nent, (long long)total);
1838 bulkstat1_f(int opno, long r)
1848 xfs_fsop_bulkreq_t bsr;
1851 good = random() & 1;
1853 /* use an inode we know exists */
1855 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1856 append_pathname(&f, ".");
1857 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
1862 * pick a random inode
1864 * note this can generate kernel warning messages
1865 * since bulkstat_one will read the disk block that
1866 * would contain a given inode even if that disk
1867 * block doesn't contain inodes.
1869 * this is detected later, but not until after the
1870 * warning is displayed.
1872 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
1878 fd = open(".", O_RDONLY);
1884 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
1886 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
1887 procid, opno, good?"real":"random",
1888 verifiable_log ? -1LL : (long long)ino, e);
1893 chown_f(int opno, long r)
1903 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1904 append_pathname(&f, ".");
1905 u = (uid_t)random();
1906 g = (gid_t)random();
1907 nbits = (int)(random() % idmodulo);
1908 u &= (1 << nbits) - 1;
1909 g &= (1 << nbits) - 1;
1910 e = lchown_path(&f, u, g) < 0 ? errno : 0;
1913 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
1918 setxattr_f(int opno, long r)
1920 #ifdef XFS_XFLAG_EXTSIZE
1930 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
1931 append_pathname(&f, ".");
1932 fd = open_path(&f, O_RDWR);
1933 e = fd < 0 ? errno : 0;
1938 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
1939 nbits = (int)(random() % e);
1940 p &= (1 << nbits) - 1;
1942 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
1944 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
1947 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
1954 creat_f(int opno, long r)
1969 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
1974 e1 = (random() % 100);
1975 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
1977 if (type == FT_RTF) /* rt always gets an extsize */
1978 extsize = (random() % 10) + 1;
1979 else if (e1 < 10) /* one-in-ten get an extsize */
1980 extsize = random() % 1024;
1984 e = generate_fname(fep, type, &f, &id, &v);
1988 (void)fent_to_name(&f, &flist[FT_DIR], fep);
1989 printf("%d/%d: creat - no filename from %s\n",
1990 procid, opno, f.path);
1995 fd = creat_path(&f, 0666);
1996 e = fd < 0 ? errno : 0;
2001 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2002 if (type == FT_RTF) {
2003 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2004 a.fsx_extsize = extsize *
2005 geom.rtextsize * geom.blocksize;
2007 } else if (extsize) {
2008 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2009 a.fsx_extsize = extsize * geom.blocksize;
2012 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2015 add_to_flist(type, id, parid);
2019 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2020 extsize ? a.fsx_extsize : 0, e, e1);
2021 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2027 dread_f(int opno, long r)
2031 struct dioattr diob;
2044 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2046 printf("%d/%d: dread - no filename\n", procid, opno);
2050 fd = open_path(&f, O_RDONLY|O_DIRECT);
2051 e = fd < 0 ? errno : 0;
2055 printf("%d/%d: dread - open %s failed %d\n",
2056 procid, opno, f.path, e);
2060 if (fstat64(fd, &stb) < 0) {
2062 printf("%d/%d: dread - fstat64 %s failed %d\n",
2063 procid, opno, f.path, errno);
2068 inode_info(st, sizeof(st), &stb, v);
2069 if (stb.st_size == 0) {
2071 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2077 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2080 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s failed %d\n",
2081 procid, opno, f.path, st, errno);
2087 dio_env = getenv("XFS_DIO_MIN");
2089 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2091 align = (__int64_t)diob.d_miniosz;
2092 lr = ((__int64_t)random() << 32) + random();
2093 off = (off64_t)(lr % stb.st_size);
2094 off -= (off % align);
2095 lseek64(fd, off, SEEK_SET);
2096 len = (random() % FILELEN_MAX) + 1;
2097 len -= (len % align);
2100 else if (len > diob.d_maxiosz)
2101 len = diob.d_maxiosz;
2102 buf = memalign(diob.d_mem, len);
2103 e = read(fd, buf, len) < 0 ? errno : 0;
2106 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2107 procid, opno, f.path, st, (long long)off, (int)len, e);
2113 dwrite_f(int opno, long r)
2117 struct dioattr diob;
2130 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2132 printf("%d/%d: dwrite - no filename\n", procid, opno);
2136 fd = open_path(&f, O_WRONLY|O_DIRECT);
2137 e = fd < 0 ? errno : 0;
2141 printf("%d/%d: dwrite - open %s failed %d\n",
2142 procid, opno, f.path, e);
2146 if (fstat64(fd, &stb) < 0) {
2148 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2149 procid, opno, f.path, errno);
2154 inode_info(st, sizeof(st), &stb, v);
2155 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2157 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2158 " %s%s failed %d\n",
2159 procid, opno, f.path, st, errno);
2165 dio_env = getenv("XFS_DIO_MIN");
2167 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2169 align = (__int64_t)diob.d_miniosz;
2170 lr = ((__int64_t)random() << 32) + random();
2171 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2172 off -= (off % align);
2173 lseek64(fd, off, SEEK_SET);
2174 len = (random() % FILELEN_MAX) + 1;
2175 len -= (len % align);
2178 else if (len > diob.d_maxiosz)
2179 len = diob.d_maxiosz;
2180 buf = memalign(diob.d_mem, len);
2182 lseek64(fd, off, SEEK_SET);
2183 memset(buf, nameseq & 0xff, len);
2184 e = write(fd, buf, len) < 0 ? errno : 0;
2187 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2188 procid, opno, f.path, st, (long long)off, (int)len, e);
2194 #ifdef HAVE_LINUX_FALLOC_H
2195 struct print_flags falloc_flags [] = {
2196 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2197 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2198 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2199 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2200 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2201 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2205 #define translate_falloc_flags(mode) \
2206 ({translate_flags(mode, "|", falloc_flags);})
2210 do_fallocate(int opno, long r, int mode)
2212 #ifdef HAVE_LINUX_FALLOC_H
2224 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2226 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2230 fd = open_path(&f, O_RDWR);
2233 printf("%d/%d: do_fallocate - open %s failed %d\n",
2234 procid, opno, f.path, errno);
2239 if (fstat64(fd, &stb) < 0) {
2241 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2242 procid, opno, f.path, errno);
2247 inode_info(st, sizeof(st), &stb, v);
2248 lr = ((__int64_t)random() << 32) + random();
2249 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2251 len = (off64_t)(random() % (1024 * 1024));
2253 * Collapse/insert range requires off and len to be block aligned,
2254 * make it more likely to be the case.
2256 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2258 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2259 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2261 mode |= FALLOC_FL_KEEP_SIZE & random();
2262 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2264 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2265 procid, opno, translate_falloc_flags(mode),
2266 f.path, st, (long long)off, (long long)len, e);
2273 fallocate_f(int opno, long r)
2275 #ifdef HAVE_LINUX_FALLOC_H
2276 do_fallocate(opno, r, 0);
2281 fdatasync_f(int opno, long r)
2289 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2291 printf("%d/%d: fdatasync - no filename\n",
2296 fd = open_path(&f, O_WRONLY);
2297 e = fd < 0 ? errno : 0;
2301 printf("%d/%d: fdatasync - open %s failed %d\n",
2302 procid, opno, f.path, e);
2306 e = fdatasync(fd) < 0 ? errno : 0;
2308 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2313 #ifdef HAVE_LINUX_FIEMAP_H
2314 struct print_flags fiemap_flags[] = {
2315 { FIEMAP_FLAG_SYNC, "SYNC"},
2316 { FIEMAP_FLAG_XATTR, "XATTR"},
2320 #define translate_fiemap_flags(mode) \
2321 ({translate_flags(mode, "|", fiemap_flags);})
2325 fiemap_f(int opno, long r)
2327 #ifdef HAVE_LINUX_FIEMAP_H
2337 struct fiemap *fiemap;
2340 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2342 printf("%d/%d: fiemap - no filename\n", procid, opno);
2346 fd = open_path(&f, O_RDWR);
2347 e = fd < 0 ? errno : 0;
2351 printf("%d/%d: fiemap - open %s failed %d\n",
2352 procid, opno, f.path, e);
2356 if (fstat64(fd, &stb) < 0) {
2358 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
2359 procid, opno, f.path, errno);
2364 inode_info(st, sizeof(st), &stb, v);
2365 blocks_to_map = random() & 0xffff;
2366 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
2367 (blocks_to_map * sizeof(struct fiemap_extent)));
2370 printf("%d/%d: malloc failed \n", procid, opno);
2375 lr = ((__int64_t)random() << 32) + random();
2376 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2378 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
2379 fiemap->fm_extent_count = blocks_to_map;
2380 fiemap->fm_mapped_extents = random() & 0xffff;
2381 fiemap->fm_start = off;
2382 fiemap->fm_length = ((__int64_t)random() << 32) + random();
2384 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
2386 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
2387 procid, opno, f.path, st, (long long)fiemap->fm_start,
2388 (long long) fiemap->fm_length,
2389 translate_fiemap_flags(fiemap->fm_flags), e);
2397 freesp_f(int opno, long r)
2402 struct xfs_flock64 fl;
2410 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2412 printf("%d/%d: freesp - no filename\n", procid, opno);
2416 fd = open_path(&f, O_RDWR);
2417 e = fd < 0 ? errno : 0;
2421 printf("%d/%d: freesp - open %s failed %d\n",
2422 procid, opno, f.path, e);
2426 if (fstat64(fd, &stb) < 0) {
2428 printf("%d/%d: freesp - fstat64 %s failed %d\n",
2429 procid, opno, f.path, errno);
2434 inode_info(st, sizeof(st), &stb, v);
2435 lr = ((__int64_t)random() << 32) + random();
2436 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2438 fl.l_whence = SEEK_SET;
2441 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
2443 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
2444 procid, opno, f.path, st, (long long)off, e);
2450 fsync_f(int opno, long r)
2458 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2460 printf("%d/%d: fsync - no filename\n", procid, opno);
2464 fd = open_path(&f, O_WRONLY);
2465 e = fd < 0 ? errno : 0;
2469 printf("%d/%d: fsync - open %s failed %d\n",
2470 procid, opno, f.path, e);
2474 e = fsync(fd) < 0 ? errno : 0;
2476 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
2482 getattr_f(int opno, long r)
2491 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2492 append_pathname(&f, ".");
2493 fd = open_path(&f, O_RDWR);
2494 e = fd < 0 ? errno : 0;
2497 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
2499 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
2505 getdents_f(int opno, long r)
2512 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
2513 append_pathname(&f, ".");
2514 dir = opendir_path(&f);
2518 printf("%d/%d: getdents - can't open %s\n",
2519 procid, opno, f.path);
2523 while (readdir64(dir) != NULL)
2526 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
2532 link_f(int opno, long r)
2545 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
2547 printf("%d/%d: link - no file\n", procid, opno);
2551 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
2557 e = generate_fname(fep, flp - flist, &l, &id, &v1);
2561 (void)fent_to_name(&l, &flist[FT_DIR], fep);
2562 printf("%d/%d: link - no filename from %s\n",
2563 procid, opno, l.path);
2569 e = link_path(&f, &l) < 0 ? errno : 0;
2572 add_to_flist(flp - flist, id, parid);
2574 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
2576 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
2583 mkdir_f(int opno, long r)
2593 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2598 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
2602 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2603 printf("%d/%d: mkdir - no filename from %s\n",
2604 procid, opno, f.path);
2609 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
2612 add_to_flist(FT_DIR, id, parid);
2614 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
2615 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
2621 mknod_f(int opno, long r)
2631 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2636 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
2640 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2641 printf("%d/%d: mknod - no filename from %s\n",
2642 procid, opno, f.path);
2647 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
2650 add_to_flist(FT_DEV, id, parid);
2652 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
2653 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
2659 punch_f(int opno, long r)
2661 #ifdef HAVE_LINUX_FALLOC_H
2662 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
2667 zero_f(int opno, long r)
2669 #ifdef HAVE_LINUX_FALLOC_H
2670 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
2675 collapse_f(int opno, long r)
2677 #ifdef HAVE_LINUX_FALLOC_H
2678 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
2683 insert_f(int opno, long r)
2685 #ifdef HAVE_LINUX_FALLOC_H
2686 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
2691 read_f(int opno, long r)
2705 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2707 printf("%d/%d: read - no filename\n", procid, opno);
2711 fd = open_path(&f, O_RDONLY);
2712 e = fd < 0 ? errno : 0;
2716 printf("%d/%d: read - open %s failed %d\n",
2717 procid, opno, f.path, e);
2721 if (fstat64(fd, &stb) < 0) {
2723 printf("%d/%d: read - fstat64 %s failed %d\n",
2724 procid, opno, f.path, errno);
2729 inode_info(st, sizeof(st), &stb, v);
2730 if (stb.st_size == 0) {
2732 printf("%d/%d: read - %s%s zero size\n", procid, opno,
2738 lr = ((__int64_t)random() << 32) + random();
2739 off = (off64_t)(lr % stb.st_size);
2740 lseek64(fd, off, SEEK_SET);
2741 len = (random() % FILELEN_MAX) + 1;
2743 e = read(fd, buf, len) < 0 ? errno : 0;
2746 printf("%d/%d: read %s%s [%lld,%d] %d\n",
2747 procid, opno, f.path, st, (long long)off, (int)len, e);
2753 readlink_f(int opno, long r)
2761 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
2763 printf("%d/%d: readlink - no filename\n", procid, opno);
2767 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
2770 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
2775 rename_f(int opno, long r)
2789 /* get an existing path for the source of the rename */
2791 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
2793 printf("%d/%d: rename - no filename\n", procid, opno);
2798 /* get an existing directory for the destination parent directory name */
2799 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
2805 /* generate a new path using an existing parent directory in name */
2806 init_pathname(&newf);
2807 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
2811 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
2812 printf("%d/%d: rename - no filename from %s\n",
2813 procid, opno, f.path);
2815 free_pathname(&newf);
2819 e = rename_path(&f, &newf) < 0 ? errno : 0;
2822 if (flp - flist == FT_DIR) {
2824 fix_parent(oldid, id);
2826 del_from_flist(flp - flist, fep - flp->fents);
2827 add_to_flist(flp - flist, id, parid);
2830 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
2833 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
2834 procid, opno, fep->id, fep->parent);
2835 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
2836 procid, opno, id, parid);
2839 free_pathname(&newf);
2844 resvsp_f(int opno, long r)
2849 struct xfs_flock64 fl;
2857 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2859 printf("%d/%d: resvsp - no filename\n", procid, opno);
2863 fd = open_path(&f, O_RDWR);
2864 e = fd < 0 ? errno : 0;
2868 printf("%d/%d: resvsp - open %s failed %d\n",
2869 procid, opno, f.path, e);
2873 if (fstat64(fd, &stb) < 0) {
2875 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
2876 procid, opno, f.path, errno);
2881 inode_info(st, sizeof(st), &stb, v);
2882 lr = ((__int64_t)random() << 32) + random();
2883 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2885 fl.l_whence = SEEK_SET;
2887 fl.l_len = (off64_t)(random() % (1024 * 1024));
2888 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
2890 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
2891 procid, opno, f.path, st,
2892 (long long)off, (long long)fl.l_len, e);
2898 rmdir_f(int opno, long r)
2906 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
2908 printf("%d/%d: rmdir - no directory\n", procid, opno);
2912 e = rmdir_path(&f) < 0 ? errno : 0;
2915 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
2917 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
2919 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
2920 procid, opno, fep->id, fep->parent);
2926 setattr_f(int opno, long r)
2935 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2936 append_pathname(&f, ".");
2937 fd = open_path(&f, O_RDWR);
2938 e = fd < 0 ? errno : 0;
2941 fl = attr_mask & (uint)random();
2942 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
2944 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
2950 stat_f(int opno, long r)
2958 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
2960 printf("%d/%d: stat - no entries\n", procid, opno);
2964 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
2967 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
2972 symlink_f(int opno, long r)
2985 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
2990 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
2994 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2995 printf("%d/%d: symlink - no filename from %s\n",
2996 procid, opno, f.path);
3001 len = (int)(random() % PATH_MAX);
3002 val = malloc(len + 1);
3004 memset(val, 'x', len);
3006 for (i = 10; i < len - 1; i += 10)
3008 e = symlink_path(val, &f) < 0 ? errno : 0;
3011 add_to_flist(FT_SYM, id, parid);
3014 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3015 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3022 sync_f(int opno, long r)
3026 printf("%d/%d: sync\n", procid, opno);
3030 truncate_f(int opno, long r)
3041 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3043 printf("%d/%d: truncate - no filename\n", procid, opno);
3047 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3051 printf("%d/%d: truncate - stat64 %s failed %d\n",
3052 procid, opno, f.path, e);
3056 inode_info(st, sizeof(st), &stb, v);
3057 lr = ((__int64_t)random() << 32) + random();
3058 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3060 e = truncate64_path(&f, off) < 0 ? errno : 0;
3063 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3064 st, (long long)off, e);
3069 unlink_f(int opno, long r)
3078 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3080 printf("%d/%d: unlink - no file\n", procid, opno);
3084 e = unlink_path(&f) < 0 ? errno : 0;
3087 del_from_flist(flp - flist, fep - flp->fents);
3089 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3091 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3092 procid, opno, fep->id, fep->parent);
3098 unresvsp_f(int opno, long r)
3103 struct xfs_flock64 fl;
3111 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3113 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3117 fd = open_path(&f, O_RDWR);
3118 e = fd < 0 ? errno : 0;
3122 printf("%d/%d: unresvsp - open %s failed %d\n",
3123 procid, opno, f.path, e);
3127 if (fstat64(fd, &stb) < 0) {
3129 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
3130 procid, opno, f.path, errno);
3135 inode_info(st, sizeof(st), &stb, v);
3136 lr = ((__int64_t)random() << 32) + random();
3137 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3139 fl.l_whence = SEEK_SET;
3141 fl.l_len = (off64_t)(random() % (1 << 20));
3142 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
3144 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
3145 procid, opno, f.path, st,
3146 (long long)off, (long long)fl.l_len, e);
3152 write_f(int opno, long r)
3166 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
3168 printf("%d/%d: write - no filename\n", procid, opno);
3172 fd = open_path(&f, O_WRONLY);
3173 e = fd < 0 ? errno : 0;
3177 printf("%d/%d: write - open %s failed %d\n",
3178 procid, opno, f.path, e);
3182 if (fstat64(fd, &stb) < 0) {
3184 printf("%d/%d: write - fstat64 %s failed %d\n",
3185 procid, opno, f.path, errno);
3190 inode_info(st, sizeof(st), &stb, v);
3191 lr = ((__int64_t)random() << 32) + random();
3192 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3194 lseek64(fd, off, SEEK_SET);
3195 len = (random() % FILELEN_MAX) + 1;
3197 memset(buf, nameseq & 0xff, len);
3198 e = write(fd, buf, len) < 0 ? errno : 0;
3201 printf("%d/%d: write %s%s [%lld,%d] %d\n",
3202 procid, opno, f.path, st, (long long)off, (int)len, e);