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
25 #ifdef HAVE_ATTR_XATTR_H
26 #include <attr/xattr.h>
28 #ifdef HAVE_ATTR_ATTRIBUTES_H
29 #include <attr/attributes.h>
31 #ifdef HAVE_LINUX_FIEMAP_H
32 #include <linux/fiemap.h>
34 #ifndef HAVE_ATTR_LIST
35 #define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
37 #ifdef HAVE_SYS_PRCTL_H
38 #include <sys/prctl.h>
45 #ifndef FS_IOC_GETFLAGS
46 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
48 #ifndef FS_IOC_SETFLAGS
49 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
53 #define XFS_ERRTAG_MAX 17
54 #define XFS_IDMODULO_MAX 31 /* user/group IDs (1 << x) */
55 #define XFS_PROJIDMODULO_MAX 16 /* project IDs (1 << x) */
60 #define FILELEN_MAX (32*4096)
112 typedef void (*opfnc_t)(int, long);
114 typedef struct opdesc {
122 typedef struct fent {
127 typedef struct flist {
134 typedef struct pathname {
144 struct print_string {
151 #define FT_DIRm (1 << FT_DIR)
153 #define FT_REGm (1 << FT_REG)
155 #define FT_SYMm (1 << FT_SYM)
157 #define FT_DEVm (1 << FT_DEV)
159 #define FT_RTFm (1 << FT_RTF)
161 #define FT_ANYm ((1 << FT_nft) - 1)
162 #define FT_REGFILE (FT_REGm | FT_RTFm)
163 #define FT_NOTDIR (FT_ANYm & ~FT_DIRm)
165 #define FLIST_SLOT_INCR 16
168 #define MAXFSIZE ((1ULL << 63) - 1ULL)
169 #define MAXFSIZE32 ((1ULL << 40) - 1ULL)
171 void afsync_f(int, long);
172 void allocsp_f(int, long);
173 void aread_f(int, long);
174 void attr_remove_f(int, long);
175 void attr_set_f(int, long);
176 void awrite_f(int, long);
177 void bulkstat_f(int, long);
178 void bulkstat1_f(int, long);
179 void chown_f(int, long);
180 void clonerange_f(int, long);
181 void creat_f(int, long);
182 void deduperange_f(int, long);
183 void dread_f(int, long);
184 void dwrite_f(int, long);
185 void fallocate_f(int, long);
186 void fdatasync_f(int, long);
187 void fiemap_f(int, long);
188 void freesp_f(int, long);
189 void fsync_f(int, long);
190 void getattr_f(int, long);
191 void getdents_f(int, long);
192 void link_f(int, long);
193 void mkdir_f(int, long);
194 void mknod_f(int, long);
195 void mread_f(int, long);
196 void mwrite_f(int, long);
197 void punch_f(int, long);
198 void zero_f(int, long);
199 void collapse_f(int, long);
200 void insert_f(int, long);
201 void read_f(int, long);
202 void readlink_f(int, long);
203 void readv_f(int, long);
204 void rename_f(int, long);
205 void resvsp_f(int, long);
206 void rmdir_f(int, long);
207 void setattr_f(int, long);
208 void setxattr_f(int, long);
209 void stat_f(int, long);
210 void symlink_f(int, long);
211 void sync_f(int, long);
212 void truncate_f(int, long);
213 void unlink_f(int, long);
214 void unresvsp_f(int, long);
215 void write_f(int, long);
216 void writev_f(int, long);
219 /* { OP_ENUM, "name", function, freq, iswrite }, */
220 { OP_AFSYNC, "afsync", afsync_f, 0, 1 },
221 { OP_ALLOCSP, "allocsp", allocsp_f, 1, 1 },
222 { OP_AREAD, "aread", aread_f, 1, 0 },
223 { OP_ATTR_REMOVE, "attr_remove", attr_remove_f, /* 1 */ 0, 1 },
224 { OP_ATTR_SET, "attr_set", attr_set_f, /* 2 */ 0, 1 },
225 { OP_AWRITE, "awrite", awrite_f, 1, 1 },
226 { OP_BULKSTAT, "bulkstat", bulkstat_f, 1, 0 },
227 { OP_BULKSTAT1, "bulkstat1", bulkstat1_f, 1, 0 },
228 { OP_CHOWN, "chown", chown_f, 3, 1 },
229 { OP_CLONERANGE, "clonerange", clonerange_f, 4, 1 },
230 { OP_CREAT, "creat", creat_f, 4, 1 },
231 { OP_DEDUPERANGE, "deduperange", deduperange_f, 4, 1},
232 { OP_DREAD, "dread", dread_f, 4, 0 },
233 { OP_DWRITE, "dwrite", dwrite_f, 4, 1 },
234 { OP_FALLOCATE, "fallocate", fallocate_f, 1, 1 },
235 { OP_FDATASYNC, "fdatasync", fdatasync_f, 1, 1 },
236 { OP_FIEMAP, "fiemap", fiemap_f, 1, 1 },
237 { OP_FREESP, "freesp", freesp_f, 1, 1 },
238 { OP_FSYNC, "fsync", fsync_f, 1, 1 },
239 { OP_GETATTR, "getattr", getattr_f, 1, 0 },
240 { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
241 { OP_LINK, "link", link_f, 1, 1 },
242 { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
243 { OP_MKNOD, "mknod", mknod_f, 2, 1 },
244 { OP_MREAD, "mread", mread_f, 2, 0 },
245 { OP_MWRITE, "mwrite", mwrite_f, 2, 1 },
246 { OP_PUNCH, "punch", punch_f, 1, 1 },
247 { OP_ZERO, "zero", zero_f, 1, 1 },
248 { OP_COLLAPSE, "collapse", collapse_f, 1, 1 },
249 { OP_INSERT, "insert", insert_f, 1, 1 },
250 { OP_READ, "read", read_f, 1, 0 },
251 { OP_READLINK, "readlink", readlink_f, 1, 0 },
252 { OP_READV, "readv", readv_f, 1, 0 },
253 { OP_RENAME, "rename", rename_f, 2, 1 },
254 { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
255 { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
256 { OP_SETATTR, "setattr", setattr_f, 0, 1 },
257 { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
258 { OP_STAT, "stat", stat_f, 1, 0 },
259 { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
260 { OP_SYNC, "sync", sync_f, 1, 1 },
261 { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
262 { OP_UNLINK, "unlink", unlink_f, 1, 1 },
263 { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
264 { OP_WRITE, "write", write_f, 4, 1 },
265 { OP_WRITEV, "writev", writev_f, 4, 1 },
268 flist_t flist[FT_nft] = {
281 xfs_fsop_geom_t geom;
292 unsigned int idmodulo = XFS_IDMODULO_MAX;
293 unsigned int attr_mask = ~0;
296 unsigned long seed = 0;
300 int verifiable_log = 0;
301 sig_atomic_t should_stop = 0;
302 sigjmp_buf *sigbus_jmp = NULL;
303 char *execute_cmd = NULL;
304 int execute_freq = 1;
305 struct print_string flag_str = {0};
307 void add_to_flist(int, int, int);
308 void append_pathname(pathname_t *, char *);
309 int attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
310 int attr_remove_path(pathname_t *, const char *, int);
311 int attr_set_path(pathname_t *, const char *, const char *, const int, int);
312 void check_cwd(void);
313 void cleanup_flist(void);
314 int creat_path(pathname_t *, mode_t);
315 void dcache_enter(int, int);
316 void dcache_init(void);
317 fent_t *dcache_lookup(int);
318 void dcache_purge(int);
319 void del_from_flist(int, int);
320 int dirid_to_name(char *, int);
322 int fent_to_name(pathname_t *, flist_t *, fent_t *);
323 void fix_parent(int, int);
324 void free_pathname(pathname_t *);
325 int generate_fname(fent_t *, int, pathname_t *, int *, int *);
326 int get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
327 void init_pathname(pathname_t *);
328 int lchown_path(pathname_t *, uid_t, gid_t);
329 int link_path(pathname_t *, pathname_t *);
330 int lstat64_path(pathname_t *, struct stat64 *);
331 void make_freq_table(void);
332 int mkdir_path(pathname_t *, mode_t);
333 int mknod_path(pathname_t *, mode_t, dev_t);
334 void namerandpad(int, char *, int);
335 int open_path(pathname_t *, int);
336 DIR *opendir_path(pathname_t *);
337 void process_freq(char *);
338 int readlink_path(pathname_t *, char *, size_t);
339 int rename_path(pathname_t *, pathname_t *);
340 int rmdir_path(pathname_t *);
341 void separate_pathname(pathname_t *, char *, pathname_t *);
342 void show_ops(int, char *);
343 int stat64_path(pathname_t *, struct stat64 *);
344 int symlink_path(const char *, pathname_t *);
345 int truncate64_path(pathname_t *, off64_t);
346 int unlink_path(pathname_t *);
348 void write_freq(void);
349 void zero_freq(void);
351 void sg_handler(int signum)
359 * Only handle SIGBUS when mmap write to a hole and no
360 * block can be allocated due to ENOSPC, abort otherwise.
363 siglongjmp(*sigbus_jmp, -1);
365 printf("Unknown SIGBUS is caught, Abort!\n");
368 /* should not reach here */
375 int main(int argc, char **argv)
379 char *dirname = NULL;
380 char *logname = NULL;
381 char rpath[PATH_MAX];
390 xfs_error_injection_t err_inj;
391 struct sigaction action;
393 const char *allopts = "cd:e:f:i:l:m:M:n:o:p:rs:S:vVwx:X:zH";
395 errrange = errtag = 0;
397 nops = sizeof(ops) / sizeof(ops[0]);
398 ops_end = &ops[nops];
400 while ((c = getopt(argc, argv, allopts)) != -1) {
409 sscanf(optarg, "%d", &errtag);
413 } else if (errtag == 0)
415 if (errtag >= XFS_ERRTAG_MAX) {
417 "error tag %d too large (max %d)\n",
418 errtag, XFS_ERRTAG_MAX - 1);
423 process_freq(optarg);
426 ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
427 ilist[ilistlen - 1] = strtol(optarg, &p, 16);
430 idmodulo = strtoul(optarg, NULL, 0);
431 if (idmodulo > XFS_IDMODULO_MAX) {
433 "chown modulo %d too big (max %d)\n",
434 idmodulo, XFS_IDMODULO_MAX);
439 loops = atoi(optarg);
442 operations = atoi(optarg);
449 nproc = atoi(optarg);
455 seed = strtoul(optarg, NULL, 0);
464 execute_cmd = optarg;
470 attr_mask = strtoul(optarg, NULL, 0);
474 if (optarg[0] == 'c')
485 execute_freq = strtoul(optarg, NULL, 0);
488 fprintf(stderr, "%s - invalid parameters\n",
498 /* no directory specified */
499 if (!nousage) usage();
503 (void)mkdir(dirname, 0777);
504 if (logname && logname[0] != '/') {
505 if (!getcwd(rpath, sizeof(rpath))){
506 perror("getcwd failed");
512 if (chdir(dirname) < 0) {
518 snprintf(path, sizeof(path), "%s/%s", rpath, logname);
519 if (freopen(path, "a", stdout) == NULL) {
520 perror("freopen logfile failed");
524 sprintf(buf, "fss%x", (unsigned int)getpid());
525 fd = creat(buf, 0666);
526 if (lseek64(fd, (off64_t)(MAXFSIZE32 + 1ULL), SEEK_SET) < 0)
527 maxfsize = (off64_t)MAXFSIZE32;
529 maxfsize = (off64_t)MAXFSIZE;
534 gettimeofday(&t, (void *)NULL);
535 seed = (int)t.tv_sec ^ (int)t.tv_usec;
536 printf("seed = %ld\n", seed);
538 i = xfsctl(buf, fd, XFS_IOC_FSGEOMETRY, &geom);
539 if (i >= 0 && geom.rtblocks)
540 rtpct = MIN(MAX(geom.rtblocks * 100 /
541 (geom.rtblocks + geom.datablocks), 1), 99);
550 for (i = 0; i < j; i++)
553 errtag = (random() % (XFS_ERRTAG_MAX-1)) + 1;
559 for (i = 0; i < j; i++)
562 errtag += (random() % (XFS_ERRTAG_MAX - errtag));
564 printf("Injecting failure on tag #%d\n", errtag);
565 err_inj.errtag = errtag;
567 srval = xfsctl(buf, fd, XFS_IOC_ERROR_INJECTION, &err_inj);
569 perror("fsstress - XFS_SYSSGI error injection call");
578 action.sa_handler = sg_handler;
579 sigemptyset(&action.sa_mask);
581 if (sigaction(SIGTERM, &action, 0)) {
582 perror("sigaction failed");
586 for (i = 0; i < nproc; i++) {
588 sigemptyset(&action.sa_mask);
589 action.sa_handler = SIG_DFL;
590 if (sigaction(SIGTERM, &action, 0))
592 action.sa_handler = sg_handler;
593 if (sigaction(SIGBUS, &action, 0))
595 #ifdef HAVE_SYS_PRCTL_H
596 prctl(PR_SET_PDEATHSIG, SIGKILL);
597 if (getppid() == 1) /* parent died already? */
602 snprintf(path, sizeof(path), "%s/%s.%d",
604 if (freopen(path, "a", stdout) == NULL) {
605 perror("freopen logfile failed");
611 if (io_setup(128, &io_ctx) != 0) {
612 fprintf(stderr, "io_setup failed");
616 for (i = 0; !loops || (i < loops); i++)
619 if(io_destroy(io_ctx) != 0) {
620 fprintf(stderr, "io_destroy failed");
630 while (wait(&stat) > 0 && !should_stop) {
633 action.sa_flags = SA_RESTART;
634 sigaction(SIGTERM, &action, 0);
635 kill(-getpid(), SIGTERM);
636 while (wait(&stat) > 0)
642 srval = xfsctl(buf, fd, XFS_IOC_ERROR_CLEARALL, &err_inj);
644 fprintf(stderr, "Bad ej clear on %s fd=%d (%d).\n",
646 perror("xfsctl(XFS_IOC_ERROR_CLEARALL)");
659 add_string(struct print_string *str, const char *add)
661 int len = strlen(add);
666 if (len > (str->max - 1) - str->len) {
667 str->len = str->max - 1;
671 memcpy(str->buffer + str->len, add, len);
673 str->buffer[str->len] = '\0';
679 translate_flags(int flags, const char *delim,
680 const struct print_flags *flag_array)
682 int i, mask, first = 1;
685 if (!flag_str.buffer) {
686 flag_str.buffer = malloc(4096);
690 if (!flag_str.buffer)
693 flag_str.buffer[0] = '\0';
695 for (i = 0; flag_array[i].name && flags; i++) {
696 mask = flag_array[i].mask;
697 if ((flags & mask) != mask)
700 add = flag_array[i].name;
703 add_string(&flag_str, delim);
706 add_string(&flag_str, add);
709 /* Check whether there are any leftover flags. */
715 add_string(&flag_str, delim);
717 ret = snprintf(number, 11, "0x%x", flags) > 0;
718 if (ret > 0 && ret <= 11)
719 add_string(&flag_str, number);
722 return flag_str.buffer;
726 add_to_flist(int ft, int id, int parent)
732 if (ftp->nfiles == ftp->nslots) {
733 ftp->nslots += FLIST_SLOT_INCR;
734 ftp->fents = realloc(ftp->fents, ftp->nslots * sizeof(fent_t));
736 fep = &ftp->fents[ftp->nfiles++];
738 fep->parent = parent;
742 append_pathname(pathname_t *name, char *str)
748 /* attempting to append to a dir a zero length path */
749 if (len && *str == '/' && name->len == 0) {
750 fprintf(stderr, "fsstress: append_pathname failure\n");
756 name->path = realloc(name->path, name->len + 1 + len);
757 strcpy(&name->path[name->len], str);
762 attr_list_path(pathname_t *name,
764 const int buffersize,
766 attrlist_cursor_t *cursor)
768 char buf[NAME_MAX + 1];
772 if (flags != ATTR_DONTFOLLOW) {
777 rval = attr_list(name->path, buffer, buffersize, flags, cursor);
778 if (rval >= 0 || errno != ENAMETOOLONG)
780 separate_pathname(name, buf, &newname);
781 if (chdir(buf) == 0) {
782 rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
785 free_pathname(&newname);
790 attr_remove_path(pathname_t *name, const char *attrname, int flags)
792 char buf[NAME_MAX + 1];
796 rval = attr_remove(name->path, attrname, flags);
797 if (rval >= 0 || errno != ENAMETOOLONG)
799 separate_pathname(name, buf, &newname);
800 if (chdir(buf) == 0) {
801 rval = attr_remove_path(&newname, attrname, flags);
804 free_pathname(&newname);
809 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
810 const int valuelength, int flags)
812 char buf[NAME_MAX + 1];
816 rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
817 if (rval >= 0 || errno != ENAMETOOLONG)
819 separate_pathname(name, buf, &newname);
820 if (chdir(buf) == 0) {
821 rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
825 free_pathname(&newname);
833 struct stat64 statbuf;
835 if (stat64(".", &statbuf) == 0 && statbuf.st_ino == top_ino)
838 fprintf(stderr, "fsstress: check_cwd failure\n");
845 * go thru flist and release all entries
847 * NOTE: this function shouldn't be called until the end of a process
855 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
864 creat_path(pathname_t *name, mode_t mode)
866 char buf[NAME_MAX + 1];
870 rval = creat(name->path, mode);
871 if (rval >= 0 || errno != ENAMETOOLONG)
873 separate_pathname(name, buf, &newname);
874 if (chdir(buf) == 0) {
875 rval = creat_path(&newname, mode);
878 free_pathname(&newname);
883 dcache_enter(int dirid, int slot)
885 dcache[dirid % NDCACHE] = slot;
893 for (i = 0; i < NDCACHE; i++)
898 dcache_lookup(int dirid)
903 i = dcache[dirid % NDCACHE];
904 if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
910 dcache_purge(int dirid)
914 dcp = &dcache[dirid % NDCACHE];
915 if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
920 * Delete the item from the list by
921 * moving last entry over the deleted one;
922 * unless deleted entry is the last one.
923 * Input: which file list array and which slot in array
926 del_from_flist(int ft, int slot)
932 dcache_purge(ftp->fents[slot].id);
933 if (slot != ftp->nfiles - 1) {
935 dcache_purge(ftp->fents[ftp->nfiles - 1].id);
936 ftp->fents[slot] = ftp->fents[--ftp->nfiles];
942 dirid_to_fent(int dirid)
948 if ((fep = dcache_lookup(dirid)))
950 flp = &flist[FT_DIR];
951 for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
952 if (fep->id == dirid) {
953 dcache_enter(dirid, fep - flp->fents);
963 struct stat64 statbuf;
971 dividend = (operations + execute_freq) / (execute_freq + 1);
972 sprintf(buf, "p%x", procid);
973 (void)mkdir(buf, 0777);
974 if (chdir(buf) < 0 || stat64(".", &statbuf) < 0) {
978 top_ino = statbuf.st_ino;
979 homedir = getcwd(NULL, 0);
981 perror("getcwd failed");
988 for (opno = 0; opno < operations; opno++) {
989 if (execute_cmd && opno && opno % dividend == 0) {
991 printf("%d: execute command %s\n", opno,
993 rval = system(execute_cmd);
995 fprintf(stderr, "execute command failed with "
998 p = &ops[freq_table[random() % freq_table_size]];
999 p->func(opno, random());
1001 * test for forced shutdown by stat'ing the test
1002 * directory. If this stat returns EIO, assume
1003 * the forced shutdown happened.
1005 if (errtag != 0 && opno % 100 == 0) {
1006 rval = stat64(".", &statbuf);
1008 fprintf(stderr, "Detected EIO\n");
1017 sprintf(cmd, "rm -rf %s", buf);
1024 * build up a pathname going thru the file entry and all
1025 * its parent entries
1026 * Return 0 on error, 1 on success;
1029 fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
1031 char buf[NAME_MAX + 1];
1039 /* build up parent directory name */
1040 if (fep->parent != -1) {
1041 pfep = dirid_to_fent(fep->parent);
1044 fprintf(stderr, "%d: fent-id = %d: can't find parent id: %d\n",
1045 procid, fep->id, fep->parent);
1050 e = fent_to_name(name, &flist[FT_DIR], pfep);
1053 append_pathname(name, "/");
1056 i = sprintf(buf, "%c%x", flp->tag, fep->id);
1057 namerandpad(fep->id, buf, i);
1058 append_pathname(name, buf);
1063 fix_parent(int oldid, int newid)
1070 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1071 for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
1072 if (fep->parent == oldid)
1073 fep->parent = newid;
1079 free_pathname(pathname_t *name)
1089 * Generate a filename of type ft.
1090 * If we have a fep which should be a directory then use it
1091 * as the parent path for this new filename i.e. prepend with it.
1094 generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
1096 char buf[NAME_MAX + 1];
1105 len = sprintf(buf, "%c%x", flp->tag, id = nameseq++);
1106 namerandpad(id, buf, len);
1108 /* prepend fep parent dir-name to it */
1110 e = fent_to_name(name, &flist[FT_DIR], fep);
1113 append_pathname(name, "/");
1115 append_pathname(name, buf);
1119 for (j = 0; !*v && j < ilistlen; j++) {
1120 if (ilist[j] == id) {
1130 * Input: "which" to choose the file-types eg. non-directory
1131 * Input: "r" to choose which file
1132 * Output: file-list, file-entry, name for the chosen file.
1133 * Output: verbose if chosen file is on the ilist.
1136 get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
1139 int totalsum = 0; /* total number of matching files */
1140 int partialsum = 0; /* partial sum of matching files */
1146 int e = 1; /* success */
1149 * go thru flist and add up number of files for each
1150 * category that matches with <which>.
1152 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1153 if (which & (1 << i))
1154 totalsum += flp->nfiles;
1156 if (totalsum == 0) {
1166 * Now we have possible matches between 0..totalsum-1.
1167 * And we use r to help us choose which one we want,
1168 * which when bounded by totalsum becomes x.
1170 x = (int)(r % totalsum);
1171 for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
1172 if (which & (1 << i)) {
1173 if (x < partialsum + flp->nfiles) {
1175 /* found the matching file entry */
1176 fep = &flp->fents[x - partialsum];
1178 /* fill-in what we were asked for */
1180 e = fent_to_name(name, flp, fep);
1183 fprintf(stderr, "%d: failed to get path for entry:"
1184 " id=%d,parent=%d\n",
1185 procid, fep->id, fep->parent);
1194 /* turn on verbose if its an ilisted file */
1196 for (j = 0; !*v && j < ilistlen; j++) {
1197 if (ilist[j] == fep->id) {
1204 partialsum += flp->nfiles;
1208 fprintf(stderr, "fsstress: get_fname failure\n");
1215 init_pathname(pathname_t *name)
1222 lchown_path(pathname_t *name, uid_t owner, gid_t group)
1224 char buf[NAME_MAX + 1];
1228 rval = lchown(name->path, owner, group);
1229 if (rval >= 0 || errno != ENAMETOOLONG)
1231 separate_pathname(name, buf, &newname);
1232 if (chdir(buf) == 0) {
1233 rval = lchown_path(&newname, owner, group);
1236 free_pathname(&newname);
1241 link_path(pathname_t *name1, pathname_t *name2)
1243 char buf1[NAME_MAX + 1];
1244 char buf2[NAME_MAX + 1];
1246 pathname_t newname1;
1247 pathname_t newname2;
1250 rval = link(name1->path, name2->path);
1251 if (rval >= 0 || errno != ENAMETOOLONG)
1253 separate_pathname(name1, buf1, &newname1);
1254 separate_pathname(name2, buf2, &newname2);
1255 if (strcmp(buf1, buf2) == 0) {
1256 if (chdir(buf1) == 0) {
1257 rval = link_path(&newname1, &newname2);
1261 if (strcmp(buf1, "..") == 0)
1263 else if (strcmp(buf2, "..") == 0)
1265 else if (strlen(buf1) == 0)
1267 else if (strlen(buf2) == 0)
1270 down1 = MAX(newname1.len, 3 + name2->len) <=
1271 MAX(3 + name1->len, newname2.len);
1273 free_pathname(&newname2);
1274 append_pathname(&newname2, "../");
1275 append_pathname(&newname2, name2->path);
1276 if (chdir(buf1) == 0) {
1277 rval = link_path(&newname1, &newname2);
1281 free_pathname(&newname1);
1282 append_pathname(&newname1, "../");
1283 append_pathname(&newname1, name1->path);
1284 if (chdir(buf2) == 0) {
1285 rval = link_path(&newname1, &newname2);
1290 free_pathname(&newname1);
1291 free_pathname(&newname2);
1296 lstat64_path(pathname_t *name, struct stat64 *sbuf)
1298 char buf[NAME_MAX + 1];
1302 rval = lstat64(name->path, sbuf);
1303 if (rval >= 0 || errno != ENAMETOOLONG)
1305 separate_pathname(name, buf, &newname);
1306 if (chdir(buf) == 0) {
1307 rval = lstat64_path(&newname, sbuf);
1310 free_pathname(&newname);
1315 make_freq_table(void)
1321 for (p = ops, f = 0; p < ops_end; p++)
1323 freq_table = malloc(f * sizeof(*freq_table));
1324 freq_table_size = f;
1325 for (p = ops, i = 0; p < ops_end; p++) {
1326 for (f = 0; f < p->freq; f++, i++)
1327 freq_table[i] = p->op;
1332 mkdir_path(pathname_t *name, mode_t mode)
1334 char buf[NAME_MAX + 1];
1338 rval = mkdir(name->path, mode);
1339 if (rval >= 0 || errno != ENAMETOOLONG)
1341 separate_pathname(name, buf, &newname);
1342 if (chdir(buf) == 0) {
1343 rval = mkdir_path(&newname, mode);
1346 free_pathname(&newname);
1351 mknod_path(pathname_t *name, mode_t mode, dev_t dev)
1353 char buf[NAME_MAX + 1];
1357 rval = mknod(name->path, mode, dev);
1358 if (rval >= 0 || errno != ENAMETOOLONG)
1360 separate_pathname(name, buf, &newname);
1361 if (chdir(buf) == 0) {
1362 rval = mknod_path(&newname, mode, dev);
1365 free_pathname(&newname);
1370 namerandpad(int id, char *buf, int i)
1373 static int buckets[] =
1374 { 2, 4, 8, 16, 32, 64, 128, NAME_MAX };
1380 bucket = (id ^ namerand) % (sizeof(buckets) / sizeof(buckets[0]));
1381 padmod = buckets[bucket] + 1 - i;
1384 padlen = (id ^ namerand) % padmod;
1386 memset(&buf[i], 'X', padlen);
1387 buf[i + padlen] = '\0';
1392 open_path(pathname_t *name, int oflag)
1394 char buf[NAME_MAX + 1];
1398 rval = open(name->path, oflag);
1399 if (rval >= 0 || errno != ENAMETOOLONG)
1401 separate_pathname(name, buf, &newname);
1402 if (chdir(buf) == 0) {
1403 rval = open_path(&newname, oflag);
1406 free_pathname(&newname);
1411 opendir_path(pathname_t *name)
1413 char buf[NAME_MAX + 1];
1417 rval = opendir(name->path);
1418 if (rval || errno != ENAMETOOLONG)
1420 separate_pathname(name, buf, &newname);
1421 if (chdir(buf) == 0) {
1422 rval = opendir_path(&newname);
1425 free_pathname(&newname);
1430 process_freq(char *arg)
1435 s = strchr(arg, '=');
1437 fprintf(stderr, "bad argument '%s'\n", arg);
1441 for (p = ops; p < ops_end; p++) {
1442 if (strcmp(arg, p->name) == 0) {
1447 fprintf(stderr, "can't find op type %s for -f\n", arg);
1452 readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
1454 char buf[NAME_MAX + 1];
1458 rval = readlink(name->path, lbuf, lbufsiz);
1459 if (rval >= 0 || errno != ENAMETOOLONG)
1461 separate_pathname(name, buf, &newname);
1462 if (chdir(buf) == 0) {
1463 rval = readlink_path(&newname, lbuf, lbufsiz);
1466 free_pathname(&newname);
1471 rename_path(pathname_t *name1, pathname_t *name2)
1473 char buf1[NAME_MAX + 1];
1474 char buf2[NAME_MAX + 1];
1476 pathname_t newname1;
1477 pathname_t newname2;
1480 rval = rename(name1->path, name2->path);
1481 if (rval >= 0 || errno != ENAMETOOLONG)
1483 separate_pathname(name1, buf1, &newname1);
1484 separate_pathname(name2, buf2, &newname2);
1485 if (strcmp(buf1, buf2) == 0) {
1486 if (chdir(buf1) == 0) {
1487 rval = rename_path(&newname1, &newname2);
1491 if (strcmp(buf1, "..") == 0)
1493 else if (strcmp(buf2, "..") == 0)
1495 else if (strlen(buf1) == 0)
1497 else if (strlen(buf2) == 0)
1500 down1 = MAX(newname1.len, 3 + name2->len) <=
1501 MAX(3 + name1->len, newname2.len);
1503 free_pathname(&newname2);
1504 append_pathname(&newname2, "../");
1505 append_pathname(&newname2, name2->path);
1506 if (chdir(buf1) == 0) {
1507 rval = rename_path(&newname1, &newname2);
1511 free_pathname(&newname1);
1512 append_pathname(&newname1, "../");
1513 append_pathname(&newname1, name1->path);
1514 if (chdir(buf2) == 0) {
1515 rval = rename_path(&newname1, &newname2);
1520 free_pathname(&newname1);
1521 free_pathname(&newname2);
1526 rmdir_path(pathname_t *name)
1528 char buf[NAME_MAX + 1];
1532 rval = rmdir(name->path);
1533 if (rval >= 0 || errno != ENAMETOOLONG)
1535 separate_pathname(name, buf, &newname);
1536 if (chdir(buf) == 0) {
1537 rval = rmdir_path(&newname);
1540 free_pathname(&newname);
1545 separate_pathname(pathname_t *name, char *buf, pathname_t *newname)
1549 init_pathname(newname);
1550 slash = strchr(name->path, '/');
1551 if (slash == NULL) {
1556 strcpy(buf, name->path);
1558 append_pathname(newname, slash + 1);
1564 show_ops(int flag, char *lead_str)
1569 /* print in list form */
1572 for (p = ops; p < ops_end; p++) {
1573 if (lead_str != NULL && x+strlen(p->name)>=WIDTH-5)
1574 x=printf("%s%s", (p==ops)?"":"\n", lead_str);
1575 x+=printf("%s ", p->name);
1578 } else if (flag == 0) {
1579 /* Table view style */
1581 for (f = 0, p = ops; p < ops_end; p++)
1587 for (p = ops; p < ops_end; p++) {
1588 if (flag != 0 || p->freq > 0) {
1589 if (lead_str != NULL)
1590 printf("%s", lead_str);
1591 printf("%20s %d/%d %s\n",
1592 p->name, p->freq, f,
1593 (p->iswrite == 0) ? " " : "write op");
1597 /* Command line style */
1598 if (lead_str != NULL)
1599 printf("%s", lead_str);
1600 printf ("-z -s %ld -m %d -n %d -p %d \\\n", seed, idmodulo,
1602 for (p = ops; p < ops_end; p++)
1604 printf("-f %s=%d \\\n",p->name, p->freq);
1609 stat64_path(pathname_t *name, struct stat64 *sbuf)
1611 char buf[NAME_MAX + 1];
1615 rval = stat64(name->path, sbuf);
1616 if (rval >= 0 || errno != ENAMETOOLONG)
1618 separate_pathname(name, buf, &newname);
1619 if (chdir(buf) == 0) {
1620 rval = stat64_path(&newname, sbuf);
1623 free_pathname(&newname);
1628 symlink_path(const char *name1, pathname_t *name)
1630 char buf[NAME_MAX + 1];
1634 if (!strcmp(name1, name->path)) {
1635 printf("yikes! %s %s\n", name1, name->path);
1639 rval = symlink(name1, name->path);
1640 if (rval >= 0 || errno != ENAMETOOLONG)
1642 separate_pathname(name, buf, &newname);
1643 if (chdir(buf) == 0) {
1644 rval = symlink_path(name1, &newname);
1647 free_pathname(&newname);
1652 truncate64_path(pathname_t *name, off64_t length)
1654 char buf[NAME_MAX + 1];
1658 rval = truncate64(name->path, length);
1659 if (rval >= 0 || errno != ENAMETOOLONG)
1661 separate_pathname(name, buf, &newname);
1662 if (chdir(buf) == 0) {
1663 rval = truncate64_path(&newname, length);
1666 free_pathname(&newname);
1671 unlink_path(pathname_t *name)
1673 char buf[NAME_MAX + 1];
1677 rval = unlink(name->path);
1678 if (rval >= 0 || errno != ENAMETOOLONG)
1680 separate_pathname(name, buf, &newname);
1681 if (chdir(buf) == 0) {
1682 rval = unlink_path(&newname);
1685 free_pathname(&newname);
1692 printf("Usage: %s -H or\n", myprog);
1693 printf(" %s [-c][-d dir][-e errtg][-f op_name=freq][-l loops][-n nops]\n",
1695 printf(" [-p nproc][-r len][-s seed][-v][-w][-x cmd][-z][-S][-X ncmd]\n");
1697 printf(" -c clean up the test directory after each run\n");
1698 printf(" -d dir specifies the base directory for operations\n");
1699 printf(" -e errtg specifies error injection stuff\n");
1700 printf(" -f op_name=freq changes the frequency of option name to freq\n");
1701 printf(" the valid operation names are:\n");
1703 printf(" -i filenum get verbose output for this nth file object\n");
1704 printf(" -l loops specifies the no. of times the testrun should loop.\n");
1705 printf(" *use 0 for infinite (default 1)\n");
1706 printf(" -m modulo uid/gid modulo for chown/chgrp (default 32)\n");
1707 printf(" -n nops specifies the no. of operations per process (default 1)\n");
1708 printf(" -o logfile specifies logfile name\n");
1709 printf(" -p nproc specifies the no. of processes (default 1)\n");
1710 printf(" -r specifies random name padding\n");
1711 printf(" -s seed specifies the seed for the random generator (default random)\n");
1712 printf(" -v specifies verbose mode\n");
1713 printf(" -w zeros frequencies of non-write operations\n");
1714 printf(" -x cmd execute command in the middle of operations\n");
1715 printf(" -z zeros frequencies of all operations\n");
1716 printf(" -S [c,t] prints the list of operations (omitting zero frequency) in command line or table style\n");
1717 printf(" -V specifies verifiable logging mode (omitting inode numbers)\n");
1718 printf(" -X ncmd number of calls to the -x command (default 1)\n");
1719 printf(" -H prints usage and exits\n");
1727 for (p = ops; p < ops_end; p++) {
1738 for (p = ops; p < ops_end; p++)
1742 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
1745 snprintf(str, sz, "[%ld %ld %d %d %lld %lld]",
1746 verifiable_log ? -1: (long)s->st_ino,
1747 (long)s->st_nlink, s->st_uid, s->st_gid,
1748 (long long) s->st_blocks, (long long) s->st_size);
1752 afsync_f(int opno, long r)
1760 struct iocb *iocbs[] = { &iocb };
1761 struct io_event event;
1764 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1766 printf("%d/%d: afsync - no filename\n", procid, opno);
1770 fd = open_path(&f, O_WRONLY | O_DIRECT);
1771 e = fd < 0 ? errno : 0;
1775 printf("%d/%d: afsync - open %s failed %d\n",
1776 procid, opno, f.path, e);
1781 io_prep_fsync(&iocb, fd);
1782 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1784 printf("%d/%d: afsync - io_submit %s %d\n",
1785 procid, opno, f.path, e);
1790 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1792 printf("%d/%d: afsync - io_getevents failed %d\n",
1801 printf("%d/%d: afsync %s %d\n", procid, opno, f.path, e);
1808 allocsp_f(int opno, long r)
1813 struct xfs_flock64 fl;
1821 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1823 printf("%d/%d: allocsp - no filename\n", procid, opno);
1827 fd = open_path(&f, O_RDWR);
1828 e = fd < 0 ? errno : 0;
1832 printf("%d/%d: allocsp - open %s failed %d\n",
1833 procid, opno, f.path, e);
1837 if (fstat64(fd, &stb) < 0) {
1839 printf("%d/%d: allocsp - fstat64 %s failed %d\n",
1840 procid, opno, f.path, errno);
1845 inode_info(st, sizeof(st), &stb, v);
1846 lr = ((int64_t)random() << 32) + random();
1847 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1849 fl.l_whence = SEEK_SET;
1852 e = xfsctl(f.path, fd, XFS_IOC_ALLOCSP64, &fl) < 0 ? errno : 0;
1854 printf("%d/%d: xfsctl(XFS_IOC_ALLOCSP64) %s%s %lld 0 %d\n",
1855 procid, opno, f.path, st, (long long)off, e);
1863 do_aio_rw(int opno, long r, int flags)
1867 struct dioattr diob;
1879 struct io_event event;
1880 struct iocb *iocbs[] = { &iocb };
1881 int iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
1884 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
1886 printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
1890 fd = open_path(&f, flags|O_DIRECT);
1891 e = fd < 0 ? errno : 0;
1895 printf("%d/%d: do_aio_rw - open %s failed %d\n",
1896 procid, opno, f.path, e);
1900 if (fstat64(fd, &stb) < 0) {
1902 printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
1903 procid, opno, f.path, errno);
1908 inode_info(st, sizeof(st), &stb, v);
1909 if (!iswrite && stb.st_size == 0) {
1911 printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
1917 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
1920 "%d/%d: do_aio_rw - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
1921 " fallback to stat()\n",
1922 procid, opno, f.path, st, errno);
1923 diob.d_mem = diob.d_miniosz = stb.st_blksize;
1924 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
1926 dio_env = getenv("XFS_DIO_MIN");
1928 diob.d_mem = diob.d_miniosz = atoi(dio_env);
1929 align = (int64_t)diob.d_miniosz;
1930 lr = ((int64_t)random() << 32) + random();
1931 len = (random() % FILELEN_MAX) + 1;
1932 len -= (len % align);
1935 else if (len > diob.d_maxiosz)
1936 len = diob.d_maxiosz;
1937 buf = memalign(diob.d_mem, len);
1940 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
1941 off -= (off % align);
1943 memset(buf, nameseq & 0xff, len);
1944 io_prep_pwrite(&iocb, fd, buf, len, off);
1946 off = (off64_t)(lr % stb.st_size);
1947 off -= (off % align);
1948 io_prep_pread(&iocb, fd, buf, len, off);
1950 if ((e = io_submit(io_ctx, 1, iocbs)) != 1) {
1952 printf("%d/%d: %s - io_submit failed %d\n",
1953 procid, opno, iswrite ? "awrite" : "aread", e);
1958 if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
1960 printf("%d/%d: %s - io_getevents failed %d\n",
1961 procid, opno, iswrite ? "awrite" : "aread", e);
1967 e = event.res != len ? event.res2 : 0;
1970 printf("%d/%d: %s %s%s [%lld,%d] %d\n",
1971 procid, opno, iswrite ? "awrite" : "aread",
1972 f.path, st, (long long)off, (int)len, e);
1979 aread_f(int opno, long r)
1982 do_aio_rw(opno, r, O_RDONLY);
1987 attr_remove_f(int opno, long r)
1989 attrlist_ent_t *aep;
1993 attrlist_cursor_t cursor;
2002 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2003 append_pathname(&f, ".");
2005 bzero(&cursor, sizeof(cursor));
2007 bzero(buf, sizeof(buf));
2008 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2012 alist = (attrlist_t *)buf;
2013 total += alist->al_count;
2014 } while (alist->al_more);
2017 printf("%d/%d: attr_remove - no attrs for %s\n",
2018 procid, opno, f.path);
2022 which = (int)(random() % total);
2023 bzero(&cursor, sizeof(cursor));
2027 bzero(buf, sizeof(buf));
2028 e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
2032 alist = (attrlist_t *)buf;
2033 if (which < ent + alist->al_count) {
2034 aep = (attrlist_ent_t *)
2035 &buf[alist->al_offset[which - ent]];
2036 aname = aep->a_name;
2039 ent += alist->al_count;
2040 } while (alist->al_more);
2041 if (aname == NULL) {
2044 "%d/%d: attr_remove - name %d not found at %s\n",
2045 procid, opno, which, f.path);
2049 e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
2052 printf("%d/%d: attr_remove %s %s %d\n",
2053 procid, opno, f.path, aname, e);
2058 attr_set_f(int opno, long r)
2065 static int lengths[] = { 10, 100, 1000, 10000 };
2070 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2071 append_pathname(&f, ".");
2072 sprintf(aname, "a%x", nameseq++);
2073 li = (int)(random() % (sizeof(lengths) / sizeof(lengths[0])));
2074 len = (int)(random() % lengths[li]);
2078 memset(aval, nameseq & 0xff, len);
2079 e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
2084 printf("%d/%d: attr_set %s %s %d\n", procid, opno, f.path,
2090 awrite_f(int opno, long r)
2093 do_aio_rw(opno, r, O_WRONLY);
2098 bulkstat_f(int opno, long r)
2106 xfs_fsop_bulkreq_t bsr;
2109 nent = (r % 999) + 2;
2110 t = malloc(nent * sizeof(*t));
2111 fd = open(".", O_RDONLY);
2119 while (xfsctl(".", fd, XFS_IOC_FSBULKSTAT, &bsr) == 0 && count > 0)
2123 printf("%d/%d: bulkstat nent %d total %lld\n",
2124 procid, opno, nent, (long long)total);
2129 bulkstat1_f(int opno, long r)
2139 xfs_fsop_bulkreq_t bsr;
2142 good = random() & 1;
2144 /* use an inode we know exists */
2146 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2147 append_pathname(&f, ".");
2148 ino = stat64_path(&f, &s) < 0 ? (ino64_t)r : s.st_ino;
2153 * pick a random inode
2155 * note this can generate kernel warning messages
2156 * since bulkstat_one will read the disk block that
2157 * would contain a given inode even if that disk
2158 * block doesn't contain inodes.
2160 * this is detected later, but not until after the
2161 * warning is displayed.
2163 * "XFS: device 0x825- bad inode magic/vsn daddr 0x0 #0"
2169 fd = open(".", O_RDONLY);
2175 e = xfsctl(".", fd, XFS_IOC_FSBULKSTAT_SINGLE, &bsr) < 0 ? errno : 0;
2177 printf("%d/%d: bulkstat1 %s ino %lld %d\n",
2178 procid, opno, good?"real":"random",
2179 verifiable_log ? -1LL : (long long)ino, e);
2184 chown_f(int opno, long r)
2194 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2195 append_pathname(&f, ".");
2196 u = (uid_t)random();
2197 g = (gid_t)random();
2198 nbits = (int)(random() % idmodulo);
2199 u &= (1 << nbits) - 1;
2200 g &= (1 << nbits) - 1;
2201 e = lchown_path(&f, u, g) < 0 ? errno : 0;
2204 printf("%d/%d: chown %s %d/%d %d\n", procid, opno, f.path, (int)u, (int)g, e);
2208 /* reflink some arbitrary range of f1 to f2. */
2215 struct file_clone_range fcr;
2216 struct pathname fpath1;
2217 struct pathname fpath2;
2218 struct stat64 stat1;
2219 struct stat64 stat2;
2220 char inoinfo1[1024];
2221 char inoinfo2[1024];
2235 init_pathname(&fpath1);
2236 if (!get_fname(FT_REGm, r, &fpath1, NULL, NULL, &v1)) {
2238 printf("%d/%d: clonerange read - no filename\n",
2243 init_pathname(&fpath2);
2244 if (!get_fname(FT_REGm, random(), &fpath2, NULL, NULL, &v2)) {
2246 printf("%d/%d: clonerange write - no filename\n",
2252 fd1 = open_path(&fpath1, O_RDONLY);
2253 e = fd1 < 0 ? errno : 0;
2257 printf("%d/%d: clonerange read - open %s failed %d\n",
2258 procid, opno, fpath1.path, e);
2262 fd2 = open_path(&fpath2, O_WRONLY);
2263 e = fd2 < 0 ? errno : 0;
2267 printf("%d/%d: clonerange write - open %s failed %d\n",
2268 procid, opno, fpath2.path, e);
2272 /* Get file stats */
2273 if (fstat64(fd1, &stat1) < 0) {
2275 printf("%d/%d: clonerange read - fstat64 %s failed %d\n",
2276 procid, opno, fpath1.path, errno);
2279 inode_info(inoinfo1, sizeof(inoinfo1), &stat1, v1);
2281 if (fstat64(fd2, &stat2) < 0) {
2283 printf("%d/%d: clonerange write - fstat64 %s failed %d\n",
2284 procid, opno, fpath2.path, errno);
2287 inode_info(inoinfo2, sizeof(inoinfo2), &stat2, v2);
2289 /* Calculate offsets */
2290 len = (random() % FILELEN_MAX) + 1;
2291 len &= ~(stat1.st_blksize - 1);
2293 len = stat1.st_blksize;
2294 if (len > stat1.st_size)
2295 len = stat1.st_size;
2297 lr = ((int64_t)random() << 32) + random();
2298 if (stat1.st_size == len)
2301 off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
2303 off1 &= ~(stat1.st_blksize - 1);
2306 * If srcfile == destfile, randomly generate destination ranges
2307 * until we find one that doesn't overlap the source range.
2309 max_off2 = MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE);
2311 lr = ((int64_t)random() << 32) + random();
2312 off2 = (off64_t)(lr % max_off2);
2314 off2 &= ~(stat2.st_blksize - 1);
2315 } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
2317 /* Clone data blocks */
2319 fcr.src_offset = off1;
2320 fcr.src_length = len;
2321 fcr.dest_offset = off2;
2323 ret = ioctl(fd2, FICLONERANGE, &fcr);
2324 e = ret < 0 ? errno : 0;
2326 printf("%d/%d: clonerange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
2328 fpath1.path, inoinfo1, (long long)off1, (long long)len,
2329 fpath2.path, inoinfo2, (long long)off2, (long long)len);
2332 printf(" error %d", e);
2341 free_pathname(&fpath2);
2343 free_pathname(&fpath1);
2347 /* dedupe some arbitrary range of f1 to f2...fn. */
2353 #ifdef FIDEDUPERANGE
2354 #define INFO_SZ 1024
2355 struct file_dedupe_range *fdr;
2356 struct pathname *fpath;
2357 struct stat64 *stat;
2369 if (flist[FT_REG].nfiles < 2)
2372 /* Pick somewhere between 2 and 128 files. */
2374 nr = random() % (flist[FT_REG].nfiles + 1);
2375 } while (nr < 2 || nr > 128);
2378 fdr = malloc(nr * sizeof(struct file_dedupe_range_info) +
2379 sizeof(struct file_dedupe_range));
2381 printf("%d/%d: line %d error %d\n",
2382 procid, opno, __LINE__, errno);
2385 memset(fdr, 0, (nr * sizeof(struct file_dedupe_range_info) +
2386 sizeof(struct file_dedupe_range)));
2388 fpath = calloc(nr, sizeof(struct pathname));
2390 printf("%d/%d: line %d error %d\n",
2391 procid, opno, __LINE__, errno);
2395 stat = calloc(nr, sizeof(struct stat64));
2397 printf("%d/%d: line %d error %d\n",
2398 procid, opno, __LINE__, errno);
2402 info = calloc(nr, INFO_SZ);
2404 printf("%d/%d: line %d error %d\n",
2405 procid, opno, __LINE__, errno);
2409 off = calloc(nr, sizeof(off64_t));
2411 printf("%d/%d: line %d error %d\n",
2412 procid, opno, __LINE__, errno);
2416 v = calloc(nr, sizeof(int));
2418 printf("%d/%d: line %d error %d\n",
2419 procid, opno, __LINE__, errno);
2422 fd = calloc(nr, sizeof(int));
2424 printf("%d/%d: line %d error %d\n",
2425 procid, opno, __LINE__, errno);
2428 memset(fd, 0xFF, nr * sizeof(int));
2430 /* Get paths for all files */
2431 for (i = 0; i < nr; i++)
2432 init_pathname(&fpath[i]);
2434 if (!get_fname(FT_REGm, r, &fpath[0], NULL, NULL, &v[0])) {
2436 printf("%d/%d: deduperange read - no filename\n",
2441 for (i = 1; i < nr; i++) {
2442 if (!get_fname(FT_REGm, random(), &fpath[i], NULL, NULL, &v[i])) {
2444 printf("%d/%d: deduperange write - no filename\n",
2451 fd[0] = open_path(&fpath[0], O_RDONLY);
2452 e = fd[0] < 0 ? errno : 0;
2456 printf("%d/%d: deduperange read - open %s failed %d\n",
2457 procid, opno, fpath[0].path, e);
2461 for (i = 1; i < nr; i++) {
2462 fd[i] = open_path(&fpath[i], O_WRONLY);
2463 e = fd[i] < 0 ? errno : 0;
2467 printf("%d/%d: deduperange write - open %s failed %d\n",
2468 procid, opno, fpath[i].path, e);
2473 /* Get file stats */
2474 if (fstat64(fd[0], &stat[0]) < 0) {
2476 printf("%d/%d: deduperange read - fstat64 %s failed %d\n",
2477 procid, opno, fpath[0].path, errno);
2481 inode_info(&info[0], INFO_SZ, &stat[0], v[0]);
2483 for (i = 1; i < nr; i++) {
2484 if (fstat64(fd[i], &stat[i]) < 0) {
2486 printf("%d/%d: deduperange write - fstat64 %s failed %d\n",
2487 procid, opno, fpath[i].path, errno);
2490 inode_info(&info[i * INFO_SZ], INFO_SZ, &stat[i], v[i]);
2493 /* Never try to dedupe more than half of the src file. */
2494 len = (random() % FILELEN_MAX) + 1;
2495 len &= ~(stat[0].st_blksize - 1);
2497 len = stat[0].st_blksize / 2;
2498 if (len > stat[0].st_size / 2)
2499 len = stat[0].st_size / 2;
2501 /* Calculate offsets */
2502 lr = ((int64_t)random() << 32) + random();
2503 if (stat[0].st_size == len)
2506 off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
2508 off[0] &= ~(stat[0].st_blksize - 1);
2511 * If srcfile == destfile[i], randomly generate destination ranges
2512 * until we find one that doesn't overlap the source range.
2514 for (i = 1; i < nr; i++) {
2518 lr = ((int64_t)random() << 32) + random();
2519 if (stat[i].st_size <= len)
2522 off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
2524 off[i] &= ~(stat[i].st_blksize - 1);
2525 } while (stat[0].st_ino == stat[i].st_ino &&
2526 llabs(off[i] - off[0]) < len &&
2530 /* Clone data blocks */
2531 fdr->src_offset = off[0];
2532 fdr->src_length = len;
2533 fdr->dest_count = nr - 1;
2534 for (i = 1; i < nr; i++) {
2535 fdr->info[i - 1].dest_fd = fd[i];
2536 fdr->info[i - 1].dest_offset = off[i];
2539 ret = ioctl(fd[0], FIDEDUPERANGE, fdr);
2540 e = ret < 0 ? errno : 0;
2542 printf("%d/%d: deduperange from %s%s [%lld,%lld]",
2544 fpath[0].path, &info[0], (long long)off[0],
2547 printf(" error %d", e);
2553 for (i = 1; i < nr; i++) {
2554 e = fdr->info[i - 1].status < 0 ? fdr->info[i - 1].status : 0;
2556 printf("%d/%d: ...to %s%s [%lld,%lld]",
2558 fpath[i].path, &info[i * INFO_SZ],
2559 (long long)off[i], (long long)len);
2560 if (fdr->info[i - 1].status < 0)
2561 printf(" error %d", e);
2562 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_SAME)
2563 printf(" %llu bytes deduplicated",
2564 fdr->info[i - 1].bytes_deduped);
2565 if (fdr->info[i - 1].status == FILE_DEDUPE_RANGE_DIFFERS)
2566 printf(" differed");
2572 for (i = 0; i < nr; i++)
2576 for (i = 0; i < nr; i++)
2577 free_pathname(&fpath[i]);
2596 setxattr_f(int opno, long r)
2598 #ifdef XFS_XFLAG_EXTSIZE
2608 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
2609 append_pathname(&f, ".");
2610 fd = open_path(&f, O_RDWR);
2611 e = fd < 0 ? errno : 0;
2616 e = MIN(idmodulo, XFS_PROJIDMODULO_MAX);
2617 nbits = (int)(random() % e);
2618 p &= (1 << nbits) - 1;
2620 if ((e = xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &fsx)) == 0) {
2622 e = xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &fsx);
2625 printf("%d/%d: setxattr %s %u %d\n", procid, opno, f.path, p, e);
2632 creat_f(int opno, long r)
2647 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
2652 e1 = (random() % 100);
2653 type = rtpct ? ((e1 > rtpct) ? FT_REG : FT_RTF) : FT_REG;
2655 if (type == FT_RTF) /* rt always gets an extsize */
2656 extsize = (random() % 10) + 1;
2657 else if (e1 < 10) /* one-in-ten get an extsize */
2658 extsize = random() % 1024;
2662 e = generate_fname(fep, type, &f, &id, &v);
2666 (void)fent_to_name(&f, &flist[FT_DIR], fep);
2667 printf("%d/%d: creat - no filename from %s\n",
2668 procid, opno, f.path);
2673 fd = creat_path(&f, 0666);
2674 e = fd < 0 ? errno : 0;
2679 xfsctl(f.path, fd, XFS_IOC_FSGETXATTR, &a) >= 0) {
2680 if (type == FT_RTF) {
2681 a.fsx_xflags |= XFS_XFLAG_REALTIME;
2682 a.fsx_extsize = extsize *
2683 geom.rtextsize * geom.blocksize;
2685 } else if (extsize) {
2686 a.fsx_xflags |= XFS_XFLAG_EXTSIZE;
2687 a.fsx_extsize = extsize * geom.blocksize;
2690 if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
2693 add_to_flist(type, id, parid);
2697 printf("%d/%d: creat %s x:%d %d %d\n", procid, opno, f.path,
2698 extsize ? a.fsx_extsize : 0, e, e1);
2699 printf("%d/%d: creat add id=%d,parent=%d\n", procid, opno, id, parid);
2705 dread_f(int opno, long r)
2709 struct dioattr diob;
2722 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2724 printf("%d/%d: dread - no filename\n", procid, opno);
2728 fd = open_path(&f, O_RDONLY|O_DIRECT);
2729 e = fd < 0 ? errno : 0;
2733 printf("%d/%d: dread - open %s failed %d\n",
2734 procid, opno, f.path, e);
2738 if (fstat64(fd, &stb) < 0) {
2740 printf("%d/%d: dread - fstat64 %s failed %d\n",
2741 procid, opno, f.path, errno);
2746 inode_info(st, sizeof(st), &stb, v);
2747 if (stb.st_size == 0) {
2749 printf("%d/%d: dread - %s%s zero size\n", procid, opno,
2755 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2758 "%d/%d: dread - xfsctl(XFS_IOC_DIOINFO) %s%s return %d,"
2759 " fallback to stat()\n",
2760 procid, opno, f.path, st, errno);
2761 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2762 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2765 dio_env = getenv("XFS_DIO_MIN");
2767 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2769 align = (int64_t)diob.d_miniosz;
2770 lr = ((int64_t)random() << 32) + random();
2771 off = (off64_t)(lr % stb.st_size);
2772 off -= (off % align);
2773 lseek64(fd, off, SEEK_SET);
2774 len = (random() % FILELEN_MAX) + 1;
2775 len -= (len % align);
2778 else if (len > diob.d_maxiosz)
2779 len = diob.d_maxiosz;
2780 buf = memalign(diob.d_mem, len);
2781 e = read(fd, buf, len) < 0 ? errno : 0;
2784 printf("%d/%d: dread %s%s [%lld,%d] %d\n",
2785 procid, opno, f.path, st, (long long)off, (int)len, e);
2791 dwrite_f(int opno, long r)
2795 struct dioattr diob;
2808 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2810 printf("%d/%d: dwrite - no filename\n", procid, opno);
2814 fd = open_path(&f, O_WRONLY|O_DIRECT);
2815 e = fd < 0 ? errno : 0;
2819 printf("%d/%d: dwrite - open %s failed %d\n",
2820 procid, opno, f.path, e);
2824 if (fstat64(fd, &stb) < 0) {
2826 printf("%d/%d: dwrite - fstat64 %s failed %d\n",
2827 procid, opno, f.path, errno);
2832 inode_info(st, sizeof(st), &stb, v);
2833 if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
2835 printf("%d/%d: dwrite - xfsctl(XFS_IOC_DIOINFO)"
2836 " %s%s return %d, fallback to stat()\n",
2837 procid, opno, f.path, st, errno);
2838 diob.d_mem = diob.d_miniosz = stb.st_blksize;
2839 diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
2842 dio_env = getenv("XFS_DIO_MIN");
2844 diob.d_mem = diob.d_miniosz = atoi(dio_env);
2846 align = (int64_t)diob.d_miniosz;
2847 lr = ((int64_t)random() << 32) + random();
2848 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2849 off -= (off % align);
2850 lseek64(fd, off, SEEK_SET);
2851 len = (random() % FILELEN_MAX) + 1;
2852 len -= (len % align);
2855 else if (len > diob.d_maxiosz)
2856 len = diob.d_maxiosz;
2857 buf = memalign(diob.d_mem, len);
2859 lseek64(fd, off, SEEK_SET);
2860 memset(buf, nameseq & 0xff, len);
2861 e = write(fd, buf, len) < 0 ? errno : 0;
2864 printf("%d/%d: dwrite %s%s [%lld,%d] %d\n",
2865 procid, opno, f.path, st, (long long)off, (int)len, e);
2871 #ifdef HAVE_LINUX_FALLOC_H
2872 struct print_flags falloc_flags [] = {
2873 { FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"},
2874 { FALLOC_FL_PUNCH_HOLE, "PUNCH_HOLE"},
2875 { FALLOC_FL_NO_HIDE_STALE, "NO_HIDE_STALE"},
2876 { FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"},
2877 { FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"},
2878 { FALLOC_FL_INSERT_RANGE, "INSERT_RANGE"},
2882 #define translate_falloc_flags(mode) \
2883 ({translate_flags(mode, "|", falloc_flags);})
2887 do_fallocate(int opno, long r, int mode)
2889 #ifdef HAVE_LINUX_FALLOC_H
2901 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2903 printf("%d/%d: do_fallocate - no filename\n", procid, opno);
2907 fd = open_path(&f, O_RDWR);
2910 printf("%d/%d: do_fallocate - open %s failed %d\n",
2911 procid, opno, f.path, errno);
2916 if (fstat64(fd, &stb) < 0) {
2918 printf("%d/%d: do_fallocate - fstat64 %s failed %d\n",
2919 procid, opno, f.path, errno);
2924 inode_info(st, sizeof(st), &stb, v);
2925 lr = ((int64_t)random() << 32) + random();
2926 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
2928 len = (off64_t)(random() % (1024 * 1024));
2930 * Collapse/insert range requires off and len to be block aligned,
2931 * make it more likely to be the case.
2933 if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
2935 off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2936 len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
2938 mode |= FALLOC_FL_KEEP_SIZE & random();
2939 e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
2941 printf("%d/%d: fallocate(%s) %s %st %lld %lld %d\n",
2942 procid, opno, translate_falloc_flags(mode),
2943 f.path, st, (long long)off, (long long)len, e);
2950 fallocate_f(int opno, long r)
2952 #ifdef HAVE_LINUX_FALLOC_H
2953 do_fallocate(opno, r, 0);
2958 fdatasync_f(int opno, long r)
2966 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
2968 printf("%d/%d: fdatasync - no filename\n",
2973 fd = open_path(&f, O_WRONLY);
2974 e = fd < 0 ? errno : 0;
2978 printf("%d/%d: fdatasync - open %s failed %d\n",
2979 procid, opno, f.path, e);
2983 e = fdatasync(fd) < 0 ? errno : 0;
2985 printf("%d/%d: fdatasync %s %d\n", procid, opno, f.path, e);
2990 #ifdef HAVE_LINUX_FIEMAP_H
2991 struct print_flags fiemap_flags[] = {
2992 { FIEMAP_FLAG_SYNC, "SYNC"},
2993 { FIEMAP_FLAG_XATTR, "XATTR"},
2997 #define translate_fiemap_flags(mode) \
2998 ({translate_flags(mode, "|", fiemap_flags);})
3002 fiemap_f(int opno, long r)
3004 #ifdef HAVE_LINUX_FIEMAP_H
3014 struct fiemap *fiemap;
3017 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3019 printf("%d/%d: fiemap - no filename\n", procid, opno);
3023 fd = open_path(&f, O_RDWR);
3024 e = fd < 0 ? errno : 0;
3028 printf("%d/%d: fiemap - open %s failed %d\n",
3029 procid, opno, f.path, e);
3033 if (fstat64(fd, &stb) < 0) {
3035 printf("%d/%d: fiemap - fstat64 %s failed %d\n",
3036 procid, opno, f.path, errno);
3041 inode_info(st, sizeof(st), &stb, v);
3042 blocks_to_map = random() & 0xffff;
3043 fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) +
3044 (blocks_to_map * sizeof(struct fiemap_extent)));
3047 printf("%d/%d: malloc failed \n", procid, opno);
3052 lr = ((int64_t)random() << 32) + random();
3053 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3055 fiemap->fm_flags = random() & (FIEMAP_FLAGS_COMPAT | 0x10000);
3056 fiemap->fm_extent_count = blocks_to_map;
3057 fiemap->fm_mapped_extents = random() & 0xffff;
3058 fiemap->fm_start = off;
3059 fiemap->fm_length = ((int64_t)random() << 32) + random();
3061 e = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
3063 printf("%d/%d: ioctl(FIEMAP) %s%s %lld %lld (%s) %d\n",
3064 procid, opno, f.path, st, (long long)fiemap->fm_start,
3065 (long long) fiemap->fm_length,
3066 translate_fiemap_flags(fiemap->fm_flags), e);
3074 freesp_f(int opno, long r)
3079 struct xfs_flock64 fl;
3087 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3089 printf("%d/%d: freesp - no filename\n", procid, opno);
3093 fd = open_path(&f, O_RDWR);
3094 e = fd < 0 ? errno : 0;
3098 printf("%d/%d: freesp - open %s failed %d\n",
3099 procid, opno, f.path, e);
3103 if (fstat64(fd, &stb) < 0) {
3105 printf("%d/%d: freesp - fstat64 %s failed %d\n",
3106 procid, opno, f.path, errno);
3111 inode_info(st, sizeof(st), &stb, v);
3112 lr = ((int64_t)random() << 32) + random();
3113 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3115 fl.l_whence = SEEK_SET;
3118 e = xfsctl(f.path, fd, XFS_IOC_FREESP64, &fl) < 0 ? errno : 0;
3120 printf("%d/%d: xfsctl(XFS_IOC_FREESP64) %s%s %lld 0 %d\n",
3121 procid, opno, f.path, st, (long long)off, e);
3127 fsync_f(int opno, long r)
3135 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3137 printf("%d/%d: fsync - no filename\n", procid, opno);
3141 fd = open_path(&f, O_WRONLY);
3142 e = fd < 0 ? errno : 0;
3146 printf("%d/%d: fsync - open %s failed %d\n",
3147 procid, opno, f.path, e);
3151 e = fsync(fd) < 0 ? errno : 0;
3153 printf("%d/%d: fsync %s %d\n", procid, opno, f.path, e);
3159 getattr_f(int opno, long r)
3168 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3169 append_pathname(&f, ".");
3170 fd = open_path(&f, O_RDWR);
3171 e = fd < 0 ? errno : 0;
3174 e = ioctl(fd, FS_IOC_GETFLAGS, &fl);
3176 printf("%d/%d: getattr %s %u %d\n", procid, opno, f.path, fl, e);
3182 getdents_f(int opno, long r)
3189 if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
3190 append_pathname(&f, ".");
3191 dir = opendir_path(&f);
3195 printf("%d/%d: getdents - can't open %s\n",
3196 procid, opno, f.path);
3200 while (readdir64(dir) != NULL)
3203 printf("%d/%d: getdents %s 0\n", procid, opno, f.path);
3209 link_f(int opno, long r)
3222 if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
3224 printf("%d/%d: link - no file\n", procid, opno);
3228 if (!get_fname(FT_DIRm, random(), NULL, NULL, &fep, &v))
3234 e = generate_fname(fep, flp - flist, &l, &id, &v1);
3238 (void)fent_to_name(&l, &flist[FT_DIR], fep);
3239 printf("%d/%d: link - no filename from %s\n",
3240 procid, opno, l.path);
3246 e = link_path(&f, &l) < 0 ? errno : 0;
3249 add_to_flist(flp - flist, id, parid);
3251 printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
3253 printf("%d/%d: link add id=%d,parent=%d\n", procid, opno, id, parid);
3260 mkdir_f(int opno, long r)
3270 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3275 e = generate_fname(fep, FT_DIR, &f, &id, &v1);
3279 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3280 printf("%d/%d: mkdir - no filename from %s\n",
3281 procid, opno, f.path);
3286 e = mkdir_path(&f, 0777) < 0 ? errno : 0;
3289 add_to_flist(FT_DIR, id, parid);
3291 printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
3292 printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
3298 mknod_f(int opno, long r)
3308 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3313 e = generate_fname(fep, FT_DEV, &f, &id, &v1);
3317 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3318 printf("%d/%d: mknod - no filename from %s\n",
3319 procid, opno, f.path);
3324 e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
3327 add_to_flist(FT_DEV, id, parid);
3329 printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
3330 printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
3335 #ifdef HAVE_SYS_MMAN_H
3336 struct print_flags mmap_flags[] = {
3337 { MAP_SHARED, "SHARED"},
3338 { MAP_PRIVATE, "PRIVATE"},
3342 #define translate_mmap_flags(flags) \
3343 ({translate_flags(flags, "|", mmap_flags);})
3347 do_mmap(int opno, long r, int prot)
3349 #ifdef HAVE_SYS_MMAN_H
3361 sigjmp_buf sigbus_jmpbuf;
3364 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3366 printf("%d/%d: do_mmap - no filename\n", procid, opno);
3370 fd = open_path(&f, O_RDWR);
3371 e = fd < 0 ? errno : 0;
3375 printf("%d/%d: do_mmap - open %s failed %d\n",
3376 procid, opno, f.path, e);
3380 if (fstat64(fd, &stb) < 0) {
3382 printf("%d/%d: do_mmap - fstat64 %s failed %d\n",
3383 procid, opno, f.path, errno);
3388 inode_info(st, sizeof(st), &stb, v);
3389 if (stb.st_size == 0) {
3391 printf("%d/%d: do_mmap - %s%s zero size\n", procid, opno,
3398 lr = ((int64_t)random() << 32) + random();
3399 off = (off64_t)(lr % stb.st_size);
3400 off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
3401 len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
3403 flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;
3404 addr = mmap(NULL, len, prot, flags, fd, off);
3405 e = (addr == MAP_FAILED) ? errno : 0;
3408 printf("%d/%d: do_mmap - mmap failed %s%s [%lld,%d,%s] %d\n",
3409 procid, opno, f.path, st, (long long)off,
3410 (int)len, translate_mmap_flags(flags), e);
3416 if (prot & PROT_WRITE) {
3417 if ((e = sigsetjmp(sigbus_jmpbuf, 1)) == 0) {
3418 sigbus_jmp = &sigbus_jmpbuf;
3419 memset(addr, nameseq & 0xff, len);
3423 if ((buf = malloc(len)) != NULL) {
3424 memcpy(buf, addr, len);
3429 /* set NULL to stop other functions from doing siglongjmp */
3433 printf("%d/%d: %s %s%s [%lld,%d,%s] %s\n",
3434 procid, opno, (prot & PROT_WRITE) ? "mwrite" : "mread",
3435 f.path, st, (long long)off, (int)len,
3436 translate_mmap_flags(flags),
3437 (e == 0) ? "0" : "Bus error");
3445 mread_f(int opno, long r)
3447 #ifdef HAVE_SYS_MMAN_H
3448 do_mmap(opno, r, PROT_READ);
3453 mwrite_f(int opno, long r)
3455 #ifdef HAVE_SYS_MMAN_H
3456 do_mmap(opno, r, PROT_WRITE);
3461 punch_f(int opno, long r)
3463 #ifdef HAVE_LINUX_FALLOC_H
3464 do_fallocate(opno, r, FALLOC_FL_PUNCH_HOLE);
3469 zero_f(int opno, long r)
3471 #ifdef HAVE_LINUX_FALLOC_H
3472 do_fallocate(opno, r, FALLOC_FL_ZERO_RANGE);
3477 collapse_f(int opno, long r)
3479 #ifdef HAVE_LINUX_FALLOC_H
3480 do_fallocate(opno, r, FALLOC_FL_COLLAPSE_RANGE);
3485 insert_f(int opno, long r)
3487 #ifdef HAVE_LINUX_FALLOC_H
3488 do_fallocate(opno, r, FALLOC_FL_INSERT_RANGE);
3493 read_f(int opno, long r)
3507 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3509 printf("%d/%d: read - no filename\n", procid, opno);
3513 fd = open_path(&f, O_RDONLY);
3514 e = fd < 0 ? errno : 0;
3518 printf("%d/%d: read - open %s failed %d\n",
3519 procid, opno, f.path, e);
3523 if (fstat64(fd, &stb) < 0) {
3525 printf("%d/%d: read - fstat64 %s failed %d\n",
3526 procid, opno, f.path, errno);
3531 inode_info(st, sizeof(st), &stb, v);
3532 if (stb.st_size == 0) {
3534 printf("%d/%d: read - %s%s zero size\n", procid, opno,
3540 lr = ((int64_t)random() << 32) + random();
3541 off = (off64_t)(lr % stb.st_size);
3542 lseek64(fd, off, SEEK_SET);
3543 len = (random() % FILELEN_MAX) + 1;
3545 e = read(fd, buf, len) < 0 ? errno : 0;
3548 printf("%d/%d: read %s%s [%lld,%d] %d\n",
3549 procid, opno, f.path, st, (long long)off, (int)len, e);
3555 readlink_f(int opno, long r)
3563 if (!get_fname(FT_SYMm, r, &f, NULL, NULL, &v)) {
3565 printf("%d/%d: readlink - no filename\n", procid, opno);
3569 e = readlink_path(&f, buf, PATH_MAX) < 0 ? errno : 0;
3572 printf("%d/%d: readlink %s %d\n", procid, opno, f.path, e);
3577 readv_f(int opno, long r)
3589 struct iovec *iov = NULL;
3596 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3598 printf("%d/%d: readv - no filename\n", procid, opno);
3602 fd = open_path(&f, O_RDONLY);
3603 e = fd < 0 ? errno : 0;
3607 printf("%d/%d: readv - open %s failed %d\n",
3608 procid, opno, f.path, e);
3612 if (fstat64(fd, &stb) < 0) {
3614 printf("%d/%d: readv - fstat64 %s failed %d\n",
3615 procid, opno, f.path, errno);
3620 inode_info(st, sizeof(st), &stb, v);
3621 if (stb.st_size == 0) {
3623 printf("%d/%d: readv - %s%s zero size\n", procid, opno,
3629 lr = ((int64_t)random() << 32) + random();
3630 off = (off64_t)(lr % stb.st_size);
3631 lseek64(fd, off, SEEK_SET);
3632 len = (random() % FILELEN_MAX) + 1;
3635 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
3636 iov = calloc(iovcnt, sizeof(struct iovec));
3637 iovl = len / iovcnt;
3639 for (i=0; i<iovcnt; i++) {
3640 (iov + i)->iov_base = (buf + iovb);
3641 (iov + i)->iov_len = iovl;
3645 e = readv(fd, iov, iovcnt) < 0 ? errno : 0;
3648 printf("%d/%d: readv %s%s [%lld,%d,%d] %d\n",
3649 procid, opno, f.path, st, (long long)off, (int)iovl,
3656 rename_f(int opno, long r)
3670 /* get an existing path for the source of the rename */
3672 if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
3674 printf("%d/%d: rename - no filename\n", procid, opno);
3679 /* get an existing directory for the destination parent directory name */
3680 if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
3686 /* generate a new path using an existing parent directory in name */
3687 init_pathname(&newf);
3688 e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
3692 (void)fent_to_name(&f, &flist[FT_DIR], dfep);
3693 printf("%d/%d: rename - no filename from %s\n",
3694 procid, opno, f.path);
3696 free_pathname(&newf);
3700 e = rename_path(&f, &newf) < 0 ? errno : 0;
3703 if (flp - flist == FT_DIR) {
3705 fix_parent(oldid, id);
3707 del_from_flist(flp - flist, fep - flp->fents);
3708 add_to_flist(flp - flist, id, parid);
3711 printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
3714 printf("%d/%d: rename del entry: id=%d,parent=%d\n",
3715 procid, opno, fep->id, fep->parent);
3716 printf("%d/%d: rename add entry: id=%d,parent=%d\n",
3717 procid, opno, id, parid);
3720 free_pathname(&newf);
3725 resvsp_f(int opno, long r)
3730 struct xfs_flock64 fl;
3738 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3740 printf("%d/%d: resvsp - no filename\n", procid, opno);
3744 fd = open_path(&f, O_RDWR);
3745 e = fd < 0 ? errno : 0;
3749 printf("%d/%d: resvsp - open %s failed %d\n",
3750 procid, opno, f.path, e);
3754 if (fstat64(fd, &stb) < 0) {
3756 printf("%d/%d: resvsp - fstat64 %s failed %d\n",
3757 procid, opno, f.path, errno);
3762 inode_info(st, sizeof(st), &stb, v);
3763 lr = ((int64_t)random() << 32) + random();
3764 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3766 fl.l_whence = SEEK_SET;
3768 fl.l_len = (off64_t)(random() % (1024 * 1024));
3769 e = xfsctl(f.path, fd, XFS_IOC_RESVSP64, &fl) < 0 ? errno : 0;
3771 printf("%d/%d: xfsctl(XFS_IOC_RESVSP64) %s%s %lld %lld %d\n",
3772 procid, opno, f.path, st,
3773 (long long)off, (long long)fl.l_len, e);
3779 rmdir_f(int opno, long r)
3787 if (!get_fname(FT_DIRm, r, &f, NULL, &fep, &v)) {
3789 printf("%d/%d: rmdir - no directory\n", procid, opno);
3793 e = rmdir_path(&f) < 0 ? errno : 0;
3796 del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
3798 printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
3800 printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
3801 procid, opno, fep->id, fep->parent);
3807 setattr_f(int opno, long r)
3816 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
3817 append_pathname(&f, ".");
3818 fd = open_path(&f, O_RDWR);
3819 e = fd < 0 ? errno : 0;
3822 fl = attr_mask & (uint)random();
3823 e = ioctl(fd, FS_IOC_SETFLAGS, &fl);
3825 printf("%d/%d: setattr %s %x %d\n", procid, opno, f.path, fl, e);
3831 stat_f(int opno, long r)
3839 if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v)) {
3841 printf("%d/%d: stat - no entries\n", procid, opno);
3845 e = lstat64_path(&f, &stb) < 0 ? errno : 0;
3848 printf("%d/%d: stat %s %d\n", procid, opno, f.path, e);
3853 symlink_f(int opno, long r)
3866 if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
3871 e = generate_fname(fep, FT_SYM, &f, &id, &v1);
3875 (void)fent_to_name(&f, &flist[FT_DIR], fep);
3876 printf("%d/%d: symlink - no filename from %s\n",
3877 procid, opno, f.path);
3882 len = (int)(random() % PATH_MAX);
3883 val = malloc(len + 1);
3885 memset(val, 'x', len);
3887 for (i = 10; i < len - 1; i += 10)
3889 e = symlink_path(val, &f) < 0 ? errno : 0;
3892 add_to_flist(FT_SYM, id, parid);
3895 printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
3896 printf("%d/%d: symlink add id=%d,parent=%d\n", procid, opno, id, parid);
3903 sync_f(int opno, long r)
3907 printf("%d/%d: sync\n", procid, opno);
3911 truncate_f(int opno, long r)
3922 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3924 printf("%d/%d: truncate - no filename\n", procid, opno);
3928 e = stat64_path(&f, &stb) < 0 ? errno : 0;
3932 printf("%d/%d: truncate - stat64 %s failed %d\n",
3933 procid, opno, f.path, e);
3937 inode_info(st, sizeof(st), &stb, v);
3938 lr = ((int64_t)random() << 32) + random();
3939 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
3941 e = truncate64_path(&f, off) < 0 ? errno : 0;
3944 printf("%d/%d: truncate %s%s %lld %d\n", procid, opno, f.path,
3945 st, (long long)off, e);
3950 unlink_f(int opno, long r)
3959 if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep, &v)) {
3961 printf("%d/%d: unlink - no file\n", procid, opno);
3965 e = unlink_path(&f) < 0 ? errno : 0;
3968 del_from_flist(flp - flist, fep - flp->fents);
3970 printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
3972 printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
3973 procid, opno, fep->id, fep->parent);
3979 unresvsp_f(int opno, long r)
3984 struct xfs_flock64 fl;
3992 if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
3994 printf("%d/%d: unresvsp - no filename\n", procid, opno);
3998 fd = open_path(&f, O_RDWR);
3999 e = fd < 0 ? errno : 0;
4003 printf("%d/%d: unresvsp - open %s failed %d\n",
4004 procid, opno, f.path, e);
4008 if (fstat64(fd, &stb) < 0) {
4010 printf("%d/%d: unresvsp - fstat64 %s failed %d\n",
4011 procid, opno, f.path, errno);
4016 inode_info(st, sizeof(st), &stb, v);
4017 lr = ((int64_t)random() << 32) + random();
4018 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4020 fl.l_whence = SEEK_SET;
4022 fl.l_len = (off64_t)(random() % (1 << 20));
4023 e = xfsctl(f.path, fd, XFS_IOC_UNRESVSP64, &fl) < 0 ? errno : 0;
4025 printf("%d/%d: xfsctl(XFS_IOC_UNRESVSP64) %s%s %lld %lld %d\n",
4026 procid, opno, f.path, st,
4027 (long long)off, (long long)fl.l_len, e);
4033 write_f(int opno, long r)
4047 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4049 printf("%d/%d: write - no filename\n", procid, opno);
4053 fd = open_path(&f, O_WRONLY);
4054 e = fd < 0 ? errno : 0;
4058 printf("%d/%d: write - open %s failed %d\n",
4059 procid, opno, f.path, e);
4063 if (fstat64(fd, &stb) < 0) {
4065 printf("%d/%d: write - fstat64 %s failed %d\n",
4066 procid, opno, f.path, errno);
4071 inode_info(st, sizeof(st), &stb, v);
4072 lr = ((int64_t)random() << 32) + random();
4073 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4075 lseek64(fd, off, SEEK_SET);
4076 len = (random() % FILELEN_MAX) + 1;
4078 memset(buf, nameseq & 0xff, len);
4079 e = write(fd, buf, len) < 0 ? errno : 0;
4082 printf("%d/%d: write %s%s [%lld,%d] %d\n",
4083 procid, opno, f.path, st, (long long)off, (int)len, e);
4089 writev_f(int opno, long r)
4101 struct iovec *iov = NULL;
4108 if (!get_fname(FT_REGm, r, &f, NULL, NULL, &v)) {
4110 printf("%d/%d: writev - no filename\n", procid, opno);
4114 fd = open_path(&f, O_WRONLY);
4115 e = fd < 0 ? errno : 0;
4119 printf("%d/%d: writev - open %s failed %d\n",
4120 procid, opno, f.path, e);
4124 if (fstat64(fd, &stb) < 0) {
4126 printf("%d/%d: writev - fstat64 %s failed %d\n",
4127 procid, opno, f.path, errno);
4132 inode_info(st, sizeof(st), &stb, v);
4133 lr = ((int64_t)random() << 32) + random();
4134 off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
4136 lseek64(fd, off, SEEK_SET);
4137 len = (random() % FILELEN_MAX) + 1;
4139 memset(buf, nameseq & 0xff, len);
4141 iovcnt = (random() % MIN(len, IOV_MAX)) + 1;
4142 iov = calloc(iovcnt, sizeof(struct iovec));
4143 iovl = len / iovcnt;
4145 for (i=0; i<iovcnt; i++) {
4146 (iov + i)->iov_base = (buf + iovb);
4147 (iov + i)->iov_len = iovl;
4151 e = writev(fd, iov, iovcnt) < 0 ? errno : 0;
4155 printf("%d/%d: writev %s%s [%lld,%d,%d] %d\n",
4156 procid, opno, f.path, st, (long long)off, (int)iovl,