ltp/fsx: drop caches if we're doing closeopen
[xfstests-dev.git] / ltp / fsstress.c
index 25e0c3e2a1d732f5654f2eb43b616ecc69d25c9f..709fdeec7f0d19acbd6b863c8a04c0988afe593c 100644 (file)
@@ -8,8 +8,12 @@
 #include <setjmp.h>
 #include <sys/uio.h>
 #include <stddef.h>
+#include <stdbool.h>
 #include "global.h"
 
+#ifdef HAVE_BTRFSUTIL_H
+#include <btrfsutil.h>
+#endif
 #ifdef HAVE_ATTR_ATTRIBUTES_H
 #include <attr/attributes.h>
 #endif
@@ -27,6 +31,7 @@
 io_context_t   io_ctx;
 #endif
 #include <sys/syscall.h>
+#include <sys/xattr.h>
 
 #ifndef FS_IOC_GETFLAGS
 #define FS_IOC_GETFLAGS                 _IOR('f', 1, long)
@@ -43,6 +48,38 @@ io_context_t io_ctx;
 #define IOV_MAX 1024
 #endif
 
+#ifndef HAVE_RENAMEAT2
+#if !defined(SYS_renameat2) && defined(__x86_64__)
+#define SYS_renameat2 316
+#endif
+
+#if !defined(SYS_renameat2) && defined(__i386__)
+#define SYS_renameat2 353
+#endif
+
+static int renameat2(int dfd1, const char *path1,
+                    int dfd2, const char *path2,
+                    unsigned int flags)
+{
+#ifdef SYS_renameat2
+       return syscall(SYS_renameat2, dfd1, path1, dfd2, path2, flags);
+#else
+       errno = ENOSYS;
+       return -1;
+#endif
+}
+#endif
+
+#ifndef RENAME_NOREPLACE
+#define RENAME_NOREPLACE       (1 << 0)        /* Don't overwrite target */
+#endif
+#ifndef RENAME_EXCHANGE
+#define RENAME_EXCHANGE                (1 << 1)        /* Exchange source and dest */
+#endif
+#ifndef RENAME_WHITEOUT
+#define RENAME_WHITEOUT                (1 << 2)        /* Whiteout source */
+#endif
+
 #define FILELEN_MAX            (32*4096)
 
 typedef enum {
@@ -68,7 +105,9 @@ typedef enum {
        OP_FSYNC,
        OP_GETATTR,
        OP_GETDENTS,
+       OP_GETFATTR,
        OP_LINK,
+       OP_LISTFATTR,
        OP_MKDIR,
        OP_MKNOD,
        OP_MREAD,
@@ -80,13 +119,21 @@ typedef enum {
        OP_READ,
        OP_READLINK,
        OP_READV,
+       OP_REMOVEFATTR,
        OP_RENAME,
+       OP_RNOREPLACE,
+       OP_REXCHANGE,
+       OP_RWHITEOUT,
        OP_RESVSP,
        OP_RMDIR,
        OP_SETATTR,
+       OP_SETFATTR,
        OP_SETXATTR,
+       OP_SNAPSHOT,
        OP_SPLICE,
        OP_STAT,
+       OP_SUBVOL_CREATE,
+       OP_SUBVOL_DELETE,
        OP_SYMLINK,
        OP_SYNC,
        OP_TRUNCATE,
@@ -109,7 +156,9 @@ typedef struct opdesc {
 
 typedef struct fent {
        int     id;
+       int     ft;
        int     parent;
+       int     xattr_counter;
 } fent_t;
 
 typedef struct flist {
@@ -135,20 +184,23 @@ struct print_string {
        int max;
 };
 
-#define        FT_DIR  0
-#define        FT_DIRm (1 << FT_DIR)
-#define        FT_REG  1
-#define        FT_REGm (1 << FT_REG)
-#define        FT_SYM  2
-#define        FT_SYMm (1 << FT_SYM)
-#define        FT_DEV  3
-#define        FT_DEVm (1 << FT_DEV)
-#define        FT_RTF  4
-#define        FT_RTFm (1 << FT_RTF)
-#define        FT_nft  5
-#define        FT_ANYm ((1 << FT_nft) - 1)
+#define        FT_DIR          0
+#define        FT_DIRm         (1 << FT_DIR)
+#define        FT_REG          1
+#define        FT_REGm         (1 << FT_REG)
+#define        FT_SYM          2
+#define        FT_SYMm         (1 << FT_SYM)
+#define        FT_DEV          3
+#define        FT_DEVm         (1 << FT_DEV)
+#define        FT_RTF          4
+#define        FT_RTFm         (1 << FT_RTF)
+#define        FT_SUBVOL       5
+#define        FT_SUBVOLm      (1 << FT_SUBVOL)
+#define        FT_nft          6
+#define        FT_ANYm         ((1 << FT_nft) - 1)
 #define        FT_REGFILE      (FT_REGm | FT_RTFm)
-#define        FT_NOTDIR       (FT_ANYm & ~FT_DIRm)
+#define        FT_NOTDIR       (FT_ANYm & (~FT_DIRm & ~FT_SUBVOLm))
+#define        FT_ANYDIR       (FT_DIRm | FT_SUBVOLm)
 
 #define        FLIST_SLOT_INCR 16
 #define        NDCACHE 64
@@ -156,6 +208,8 @@ struct print_string {
 #define        MAXFSIZE        ((1ULL << 63) - 1ULL)
 #define        MAXFSIZE32      ((1ULL << 40) - 1ULL)
 
+#define XATTR_NAME_BUF_SIZE 18
+
 void   afsync_f(int, long);
 void   allocsp_f(int, long);
 void   aread_f(int, long);
@@ -176,9 +230,12 @@ void       fdatasync_f(int, long);
 void   fiemap_f(int, long);
 void   freesp_f(int, long);
 void   fsync_f(int, long);
+char   *gen_random_string(int);
 void   getattr_f(int, long);
 void   getdents_f(int, long);
+void   getfattr_f(int, long);
 void   link_f(int, long);
+void   listfattr_f(int, long);
 void   mkdir_f(int, long);
 void   mknod_f(int, long);
 void   mread_f(int, long);
@@ -190,13 +247,21 @@ void      insert_f(int, long);
 void   read_f(int, long);
 void   readlink_f(int, long);
 void   readv_f(int, long);
+void   removefattr_f(int, long);
 void   rename_f(int, long);
+void   rnoreplace_f(int, long);
+void   rexchange_f(int, long);
+void   rwhiteout_f(int, long);
 void   resvsp_f(int, long);
 void   rmdir_f(int, long);
 void   setattr_f(int, long);
+void   setfattr_f(int, long);
 void   setxattr_f(int, long);
+void   snapshot_f(int, long);
 void   splice_f(int, long);
 void   stat_f(int, long);
+void   subvol_create_f(int, long);
+void   subvol_delete_f(int, long);
 void   symlink_f(int, long);
 void   sync_f(int, long);
 void   truncate_f(int, long);
@@ -204,6 +269,7 @@ void        unlink_f(int, long);
 void   unresvsp_f(int, long);
 void   write_f(int, long);
 void   writev_f(int, long);
+char   *xattr_flag_to_string(int);
 
 opdesc_t       ops[] = {
      /* { OP_ENUM, "name", function, freq, iswrite }, */
@@ -229,7 +295,11 @@ opdesc_t   ops[] = {
        { OP_FSYNC, "fsync", fsync_f, 1, 1 },
        { OP_GETATTR, "getattr", getattr_f, 1, 0 },
        { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
+       /* get extended attribute */
+       { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
        { OP_LINK, "link", link_f, 1, 1 },
+       /* list extent attributes */
+       { OP_LISTFATTR, "listfattr", listfattr_f, 1, 0 },
        { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
        { OP_MKNOD, "mknod", mknod_f, 2, 1 },
        { OP_MREAD, "mread", mread_f, 2, 0 },
@@ -241,13 +311,25 @@ opdesc_t  ops[] = {
        { OP_READ, "read", read_f, 1, 0 },
        { OP_READLINK, "readlink", readlink_f, 1, 0 },
        { OP_READV, "readv", readv_f, 1, 0 },
+       /* remove (delete) extended attribute */
+       { OP_REMOVEFATTR, "removefattr", removefattr_f, 1, 1 },
        { OP_RENAME, "rename", rename_f, 2, 1 },
+       { OP_RNOREPLACE, "rnoreplace", rnoreplace_f, 2, 1 },
+       { OP_REXCHANGE, "rexchange", rexchange_f, 2, 1 },
+       { OP_RWHITEOUT, "rwhiteout", rwhiteout_f, 2, 1 },
        { OP_RESVSP, "resvsp", resvsp_f, 1, 1 },
        { OP_RMDIR, "rmdir", rmdir_f, 1, 1 },
+       /* set attribute flag (FS_IOC_SETFLAGS ioctl) */
        { OP_SETATTR, "setattr", setattr_f, 0, 1 },
+       /* set extended attribute */
+       { OP_SETFATTR, "setfattr", setfattr_f, 2, 1 },
+       /* set project id (XFS_IOC_FSSETXATTR ioctl) */
        { OP_SETXATTR, "setxattr", setxattr_f, 1, 1 },
+       { OP_SNAPSHOT, "snapshot", snapshot_f, 1, 1 },
        { OP_SPLICE, "splice", splice_f, 1, 1 },
        { OP_STAT, "stat", stat_f, 1, 0 },
+       { OP_SUBVOL_CREATE, "subvol_create", subvol_create_f, 1, 1},
+       { OP_SUBVOL_DELETE, "subvol_delete", subvol_delete_f, 1, 1},
        { OP_SYMLINK, "symlink", symlink_f, 2, 1 },
        { OP_SYNC, "sync", sync_f, 1, 1 },
        { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
@@ -263,6 +345,7 @@ flist_t     flist[FT_nft] = {
        { 0, 0, 'l', NULL },
        { 0, 0, 'c', NULL },
        { 0, 0, 'r', NULL },
+       { 0, 0, 's', NULL },
 };
 
 int            dcache[NDCACHE];
@@ -270,7 +353,7 @@ int         errrange;
 int            errtag;
 opty_t         *freq_table;
 int            freq_table_size;
-xfs_fsop_geom_t        geom;
+struct xfs_fsop_geom   geom;
 char           *homedir;
 int            *ilist;
 int            ilistlen;
@@ -296,7 +379,7 @@ char                *execute_cmd = NULL;
 int            execute_freq = 1;
 struct print_string    flag_str = {0};
 
-void   add_to_flist(int, int, int);
+void   add_to_flist(int, int, int, int);
 void   append_pathname(pathname_t *, char *);
 int    attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
 int    attr_remove_path(pathname_t *, const char *, int);
@@ -307,14 +390,16 @@ int       creat_path(pathname_t *, mode_t);
 void   dcache_enter(int, int);
 void   dcache_init(void);
 fent_t *dcache_lookup(int);
-void   dcache_purge(int);
+void   dcache_purge(int, int);
 void   del_from_flist(int, int);
 int    dirid_to_name(char *, int);
 void   doproc(void);
-int    fent_to_name(pathname_t *, flist_t *, fent_t *);
-void   fix_parent(int, int);
+int    fent_to_name(pathname_t *, fent_t *);
+bool   fents_ancestor_check(fent_t *, fent_t *);
+void   fix_parent(int, int, bool);
 void   free_pathname(pathname_t *);
 int    generate_fname(fent_t *, int, pathname_t *, int *, int *);
+int    generate_xattr_name(int, char *, int);
 int    get_fname(int, long, pathname_t *, flist_t **, fent_t **, int *);
 void   init_pathname(pathname_t *);
 int    lchown_path(pathname_t *, uid_t, gid_t);
@@ -324,11 +409,12 @@ void      make_freq_table(void);
 int    mkdir_path(pathname_t *, mode_t);
 int    mknod_path(pathname_t *, mode_t, dev_t);
 void   namerandpad(int, char *, int);
+int    open_file_or_dir(pathname_t *, int);
 int    open_path(pathname_t *, int);
 DIR    *opendir_path(pathname_t *);
 void   process_freq(char *);
 int    readlink_path(pathname_t *, char *, size_t);
-int    rename_path(pathname_t *, pathname_t *);
+int    rename_path(pathname_t *, pathname_t *, int);
 int    rmdir_path(pathname_t *);
 void   separate_pathname(pathname_t *, char *, pathname_t *);
 void   show_ops(int, char *);
@@ -339,6 +425,7 @@ int unlink_path(pathname_t *);
 void   usage(void);
 void   write_freq(void);
 void   zero_freq(void);
+void   non_btrfs_freq(const char *);
 
 void sg_handler(int signum)
 {
@@ -492,6 +579,7 @@ int main(int argc, char **argv)
             exit(1);
         }
 
+       non_btrfs_freq(dirname);
        (void)mkdir(dirname, 0777);
        if (logname && logname[0] != '/') {
                if (!getcwd(rpath, sizeof(rpath))){
@@ -715,7 +803,7 @@ translate_flags(int flags, const char *delim,
 }
 
 void
-add_to_flist(int ft, int id, int parent)
+add_to_flist(int ft, int id, int parent, int xattr_counter)
 {
        fent_t  *fep;
        flist_t *ftp;
@@ -727,7 +815,9 @@ add_to_flist(int ft, int id, int parent)
        }
        fep = &ftp->fents[ftp->nfiles++];
        fep->id = id;
+       fep->ft = ft;
        fep->parent = parent;
+       fep->xattr_counter = xattr_counter;
 }
 
 void
@@ -893,18 +983,22 @@ dcache_lookup(int dirid)
        int     i;
 
        i = dcache[dirid % NDCACHE];
-       if (i >= 0 && (fep = &flist[FT_DIR].fents[i])->id == dirid)
+       if (i >= 0 && i < flist[FT_DIR].nfiles &&
+           (fep = &flist[FT_DIR].fents[i])->id == dirid)
+               return fep;
+       if (i >= 0 && i < flist[FT_SUBVOL].nfiles &&
+           (fep = &flist[FT_SUBVOL].fents[i])->id == dirid)
                return fep;
        return NULL;
 }
 
 void
-dcache_purge(int dirid)
+dcache_purge(int dirid, int ft)
 {
        int     *dcp;
-
        dcp = &dcache[dirid % NDCACHE];
-       if (*dcp >= 0 && flist[FT_DIR].fents[*dcp].id == dirid)
+       if (*dcp >= 0 && *dcp < flist[ft].nfiles &&
+           flist[ft].fents[*dcp].id == dirid)
                *dcp = -1;
 }
 
@@ -920,32 +1014,58 @@ del_from_flist(int ft, int slot)
        flist_t *ftp;
 
        ftp = &flist[ft];
-       if (ft == FT_DIR)
-               dcache_purge(ftp->fents[slot].id);
+       if (ft == FT_DIR || ft == FT_SUBVOL)
+               dcache_purge(ftp->fents[slot].id, ft);
        if (slot != ftp->nfiles - 1) {
-               if (ft == FT_DIR)
-                       dcache_purge(ftp->fents[ftp->nfiles - 1].id);
+               if (ft == FT_DIR || ft == FT_SUBVOL)
+                       dcache_purge(ftp->fents[ftp->nfiles - 1].id, ft);
                ftp->fents[slot] = ftp->fents[--ftp->nfiles];
        } else
                ftp->nfiles--;
 }
 
+void
+delete_subvol_children(int parid)
+{
+       flist_t *flp;
+       int     i;
+again:
+       for (i = 0, flp = flist; i < FT_nft; i++, flp++) {
+               int c;
+               for (c = 0; c < flp->nfiles; c++) {
+                       if (flp->fents[c].parent == parid) {
+                               int id = flp->fents[c].id;
+                               del_from_flist(i, c);
+                               if (i == FT_DIR || i == FT_SUBVOL)
+                                       delete_subvol_children(id);
+                               goto again;
+                       }
+               }
+       }
+}
+
 fent_t *
 dirid_to_fent(int dirid)
 {
        fent_t  *efep;
        fent_t  *fep;
        flist_t *flp;
+       int     ft = FT_DIR;
 
        if ((fep = dcache_lookup(dirid)))
                return fep;
-       flp = &flist[FT_DIR];
+again:
+       flp = &flist[ft];
        for (fep = flp->fents, efep = &fep[flp->nfiles]; fep < efep; fep++) {
                if (fep->id == dirid) {
                        dcache_enter(dirid, fep - flp->fents);
                        return fep;
                }
        }
+       if (ft == FT_DIR) {
+               ft = FT_SUBVOL;
+               goto again;
+       }
        return NULL;
 }
 
@@ -1022,8 +1142,9 @@ errout:
  * Return 0 on error, 1 on success;
  */
 int
-fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
+fent_to_name(pathname_t *name, fent_t *fep)
 {
+       flist_t *flp = &flist[fep->ft];
        char    buf[NAME_MAX + 1];
        int     i;
        fent_t  *pfep;
@@ -1043,7 +1164,7 @@ fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
 #endif
                if (pfep == NULL)
                        return 0;
-               e = fent_to_name(name, &flist[FT_DIR], pfep);
+               e = fent_to_name(name, pfep);
                if (!e)
                        return 0;
                append_pathname(name, "/");
@@ -1055,8 +1176,22 @@ fent_to_name(pathname_t *name, flist_t *flp, fent_t *fep)
        return 1;
 }
 
+bool
+fents_ancestor_check(fent_t *fep, fent_t *dfep)
+{
+       fent_t  *tmpfep;
+
+       for (tmpfep = fep; tmpfep->parent != -1;
+            tmpfep = dirid_to_fent(tmpfep->parent)) {
+               if (tmpfep->parent == dfep->id)
+                       return true;
+       }
+
+       return false;
+}
+
 void
-fix_parent(int oldid, int newid)
+fix_parent(int oldid, int newid, bool swap)
 {
        fent_t  *fep;
        flist_t *flp;
@@ -1067,6 +1202,8 @@ fix_parent(int oldid, int newid)
                for (j = 0, fep = flp->fents; j < flp->nfiles; j++, fep++) {
                        if (fep->parent == oldid)
                                fep->parent = newid;
+                       else if (swap && fep->parent == newid)
+                               fep->parent = oldid;
                }
        }
 }
@@ -1103,7 +1240,7 @@ generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
 
        /* prepend fep parent dir-name to it */
        if (fep) {
-               e = fent_to_name(name, &flist[FT_DIR], fep);
+               e = fent_to_name(name, fep);
                if (!e)
                        return 0;
                append_pathname(name, "/");
@@ -1121,6 +1258,18 @@ generate_fname(fent_t *fep, int ft, pathname_t *name, int *idp, int *v)
        return 1;
 }
 
+int generate_xattr_name(int xattr_num, char *buffer, int buflen)
+{
+       int ret;
+
+       ret = snprintf(buffer, buflen, "user.x%d", xattr_num);
+       if (ret < 0)
+               return ret;
+       if (ret < buflen)
+               return 0;
+       return -EOVERFLOW;
+}
+
 /*
  * Get file 
  * Input: "which" to choose the file-types eg. non-directory
@@ -1173,7 +1322,7 @@ get_fname(int which, long r, pathname_t *name, flist_t **flpp, fent_t **fepp,
 
                                /* fill-in what we were asked for */
                                if (name) {
-                                       e = fent_to_name(name, flp, fep);
+                                       e = fent_to_name(name, fep);
 #ifdef DEBUG
                                        if (!e) {
                                                fprintf(stderr, "%d: failed to get path for entry:"
@@ -1384,6 +1533,22 @@ namerandpad(int id, char *buf, int i)
        }
 }
 
+int
+open_file_or_dir(pathname_t *name, int flags)
+{
+       int fd;
+
+       fd = open_path(name, flags);
+       if (fd != -1)
+               return fd;
+       if (fd == -1 && errno != EISDIR)
+               return fd;
+       /* Directories can not be opened in write mode nor direct mode. */
+       flags &= ~(O_WRONLY | O_DIRECT);
+       flags |= O_RDONLY | O_DIRECTORY;
+       return open_path(name, flags);
+}
+
 int
 open_path(pathname_t *name, int oflag)
 {
@@ -1464,7 +1629,7 @@ readlink_path(pathname_t *name, char *lbuf, size_t lbufsiz)
 }
 
 int
-rename_path(pathname_t *name1, pathname_t *name2)
+rename_path(pathname_t *name1, pathname_t *name2, int mode)
 {
        char            buf1[NAME_MAX + 1];
        char            buf2[NAME_MAX + 1];
@@ -1473,14 +1638,18 @@ rename_path(pathname_t *name1, pathname_t *name2)
        pathname_t      newname2;
        int             rval;
 
-       rval = rename(name1->path, name2->path);
+       if (mode == 0)
+               rval = rename(name1->path, name2->path);
+       else
+               rval = renameat2(AT_FDCWD, name1->path,
+                                AT_FDCWD, name2->path, mode);
        if (rval >= 0 || errno != ENAMETOOLONG)
                return rval;
        separate_pathname(name1, buf1, &newname1);
        separate_pathname(name2, buf2, &newname2);
        if (strcmp(buf1, buf2) == 0) {
                if (chdir(buf1) == 0) {
-                       rval = rename_path(&newname1, &newname2);
+                       rval = rename_path(&newname1, &newname2, mode);
                        assert(chdir("..") == 0);
                }
        } else {
@@ -1500,7 +1669,7 @@ rename_path(pathname_t *name1, pathname_t *name2)
                        append_pathname(&newname2, "../");
                        append_pathname(&newname2, name2->path);
                        if (chdir(buf1) == 0) {
-                               rval = rename_path(&newname1, &newname2);
+                               rval = rename_path(&newname1, &newname2, mode);
                                assert(chdir("..") == 0);
                        }
                } else {
@@ -1508,7 +1677,7 @@ rename_path(pathname_t *name1, pathname_t *name2)
                        append_pathname(&newname1, "../");
                        append_pathname(&newname1, name1->path);
                        if (chdir(buf2) == 0) {
-                               rval = rename_path(&newname1, &newname2);
+                               rval = rename_path(&newname1, &newname2, mode);
                                assert(chdir("..") == 0);
                        }
                }
@@ -1735,6 +1904,36 @@ zero_freq(void)
                p->freq = 0;
 }
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+opty_t btrfs_ops[] = {
+       OP_SNAPSHOT,
+       OP_SUBVOL_CREATE,
+       OP_SUBVOL_DELETE,
+};
+
+void
+non_btrfs_freq(const char *path)
+{
+       opdesc_t                *p;
+#ifdef HAVE_BTRFSUTIL_H
+       enum btrfs_util_error   e;
+
+       e = btrfs_util_is_subvolume(path);
+       if (e != BTRFS_UTIL_ERROR_NOT_BTRFS)
+               return;
+#endif
+       for (p = ops; p < ops_end; p++) {
+               int i;
+               for (i = 0; i < ARRAY_SIZE(btrfs_ops); i++) {
+                       if (p->op == btrfs_ops[i]) {
+                               p->freq = 0;
+                               break;
+                       }
+               }
+       }
+}
+
 void inode_info(char *str, size_t sz, struct stat64 *s, int verbose)
 {
        if (verbose)
@@ -1757,13 +1956,13 @@ afsync_f(int opno, long r)
        struct io_event event;
 
        init_pathname(&f);
-       if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
+       if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
                if (v)
                        printf("%d/%d: afsync - no filename\n", procid, opno);
                free_pathname(&f);
                return;
        }
-       fd = open_path(&f, O_WRONLY | O_DIRECT);
+       fd = open_file_or_dir(&f, O_WRONLY | O_DIRECT);
        e = fd < 0 ? errno : 0;
        check_cwd();
        if (fd < 0) {
@@ -2097,9 +2296,9 @@ bulkstat_f(int opno, long r)
        int             fd;
        __u64           last;
        int             nent;
-       xfs_bstat_t     *t;
+       struct xfs_bstat        *t;
        int64_t         total;
-        xfs_fsop_bulkreq_t bsr;
+       struct xfs_fsop_bulkreq bsr;
 
        last = 0;
        nent = (r % 999) + 2;
@@ -2130,9 +2329,9 @@ bulkstat1_f(int opno, long r)
        int             good;
        __u64           ino;
        struct stat64   s;
-       xfs_bstat_t     t;
+       struct xfs_bstat        t;
        int             v;
-        xfs_fsop_bulkreq_t bsr;
+       struct xfs_fsop_bulkreq bsr;
         
 
        good = random() & 1;
@@ -2356,14 +2555,17 @@ copyrange_f(
        loff_t                  lr;
        loff_t                  off1;
        loff_t                  off2;
+       loff_t                  offset1;
+       loff_t                  offset2;
        loff_t                  max_off2;
        size_t                  len;
+       size_t                  length;
        int                     tries = 0;
        int                     v1;
        int                     v2;
        int                     fd1;
        int                     fd2;
-       size_t                  ret;
+       size_t                  ret = 0;
        int                     e;
 
        /* Load paths */
@@ -2446,13 +2648,21 @@ copyrange_f(
                off2 %= maxfsize;
        } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
 
+       /*
+        * Since len, off1 and off2 will be changed later, preserve their
+        * original values.
+        */
+       length = len;
+       offset1 = off1;
+       offset2 = off2;
+
        while (len > 0) {
                ret = syscall(__NR_copy_file_range, fd1, &off1, fd2, &off2,
                              len, 0);
                if (ret < 0) {
                        if (errno != EAGAIN || tries++ >= 300)
                                break;
-               } else if (ret > len)
+               } else if (ret > len || ret == 0)
                        break;
                else if (ret > 0)
                        len -= ret;
@@ -2461,8 +2671,10 @@ copyrange_f(
        if (v1 || v2) {
                printf("%d/%d: copyrange %s%s [%lld,%lld] -> %s%s [%lld,%lld]",
                        procid, opno,
-                       fpath1.path, inoinfo1, (long long)off1, (long long)len,
-                       fpath2.path, inoinfo2, (long long)off2, (long long)len);
+                       fpath1.path, inoinfo1,
+                       (long long)offset1, (long long)length,
+                       fpath2.path, inoinfo2,
+                       (long long)offset2, (long long)length);
 
                if (ret < 0)
                        printf(" error %d", e);
@@ -2869,8 +3081,8 @@ splice_f(int opno, long r)
        off2 = (off64_t)(lr % MIN(stat2.st_size + (1024ULL * stat2.st_blksize), MAXFSIZE));
 
        /*
-        * Due to len, off1 and off2 will be changed later, so record the
-        * original number at here
+        * Since len, off1 and off2 will be changed later, preserve their
+        * original values.
         */
        length = len;
        offset1 = off1;
@@ -2890,7 +3102,7 @@ splice_f(int opno, long r)
        while (len > 0) {
                /* move to pipe buffer */
                ret1 = splice(fd1, &off1, filedes[1], NULL, len, 0);
-               if (ret1 < 0) {
+               if (ret1 <= 0) {
                        break;
                }
                bytes = ret1;
@@ -2954,7 +3166,7 @@ creat_f(int opno, long r)
        int             v;
        int             v1;
 
-       if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v1))
+       if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v1))
                parid = -1;
        else
                parid = fep->id;
@@ -2973,7 +3185,7 @@ creat_f(int opno, long r)
        v |= v1;
        if (!e) {
                if (v) {
-                       (void)fent_to_name(&f, &flist[FT_DIR], fep);
+                       (void)fent_to_name(&f, fep);
                        printf("%d/%d: creat - no filename from %s\n",
                                procid, opno, f.path);
                }
@@ -3000,7 +3212,7 @@ creat_f(int opno, long r)
                        if (xfsctl(f.path, fd, XFS_IOC_FSSETXATTR, &a) < 0)
                                e1 = errno;
                }
-               add_to_flist(type, id, parid);
+               add_to_flist(type, id, parid, 0);
                close(fd);
        }
        if (v) {
@@ -3442,13 +3654,13 @@ fsync_f(int opno, long r)
        int             v;
 
        init_pathname(&f);
-       if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
+       if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, NULL, &v)) {
                if (v)
                        printf("%d/%d: fsync - no filename\n", procid, opno);
                free_pathname(&f);
                return;
        }
-       fd = open_path(&f, O_WRONLY);
+       fd = open_file_or_dir(&f, O_WRONLY);
        e = fd < 0 ? errno : 0;
        check_cwd();
        if (fd < 0) {
@@ -3465,6 +3677,28 @@ fsync_f(int opno, long r)
        close(fd);
 }
 
+char *
+gen_random_string(int len)
+{
+       static const char charset[] = "0123456789"
+               "abcdefghijklmnopqrstuvwxyz"
+               "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+       int i;
+       char *s;
+
+       if (len == 0)
+               return NULL;
+
+       s = malloc(len);
+       if (!s)
+               return NULL;
+
+       for (i = 0; i < len; i++)
+               s[i] = charset[random() % sizeof(charset)];
+
+       return s;
+}
+
 void
 getattr_f(int opno, long r)
 {
@@ -3496,7 +3730,7 @@ getdents_f(int opno, long r)
        int             v;
 
        init_pathname(&f);
-       if (!get_fname(FT_DIRm, r, &f, NULL, NULL, &v))
+       if (!get_fname(FT_ANYDIR, r, &f, NULL, NULL, &v))
                append_pathname(&f, ".");
        dir = opendir_path(&f);
        check_cwd();
@@ -3515,12 +3749,82 @@ getdents_f(int opno, long r)
        closedir(dir);
 }
 
+void
+getfattr_f(int opno, long r)
+{
+       fent_t          *fep;
+       int             e;
+       pathname_t      f;
+       int             v;
+       char            name[XATTR_NAME_BUF_SIZE];
+       char            *value = NULL;
+       int             value_len;
+       int             xattr_num;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: getfattr - no filename\n", procid, opno);
+               goto out;
+       }
+       check_cwd();
+
+       /*
+        * If the file/dir has xattrs, pick one randomly, otherwise attempt
+        * to read a xattr that doesn't exist (fgetxattr should fail with
+        * errno set to ENOATTR (61) in this case).
+        */
+       if (fep->xattr_counter > 0)
+               xattr_num = (random() % fep->xattr_counter) + 1;
+       else
+               xattr_num = 0;
+
+       e = generate_xattr_name(xattr_num, name, sizeof(name));
+       if (e < 0) {
+               printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
+                      procid, opno, f.path, e);
+               goto out;
+       }
+
+       value_len = getxattr(f.path, name, NULL, 0);
+       if (value_len < 0) {
+               if (v)
+                       printf("%d/%d: getfattr file %s name %s failed %d\n",
+                              procid, opno, f.path, name, errno);
+               goto out;
+       }
+
+       /* A xattr without value.*/
+       if (value_len == 0) {
+               e = 0;
+               goto out_log;
+       }
+
+       value = malloc(value_len);
+       if (!value) {
+               if (v)
+                       printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
+                              procid, opno, f.path, value_len);
+               goto out;
+       }
+
+       e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
+out_log:
+       if (v)
+               printf("%d/%d: getfattr file %s name %s value length %d %d\n",
+                      procid, opno, f.path, name, value_len, e);
+out:
+       free(value);
+       free_pathname(&f);
+}
+
 void
 link_f(int opno, long r)
 {
        int             e;
        pathname_t      f;
        fent_t          *fep;
+       fent_t          *fep_src;
        flist_t         *flp;
        int             id;
        pathname_t      l;
@@ -3529,7 +3833,7 @@ link_f(int opno, long r)
        int             v1;
 
        init_pathname(&f);
-       if (!get_fname(FT_NOTDIR, r, &f, &flp, NULL, &v1)) {
+       if (!get_fname(FT_NOTDIR, r, &f, &flp, &fep_src, &v1)) {
                if (v1)
                        printf("%d/%d: link - no file\n", procid, opno);
                free_pathname(&f);
@@ -3545,7 +3849,7 @@ link_f(int opno, long r)
        v |= v1;
        if (!e) {
                if (v) {
-                       (void)fent_to_name(&l, &flist[FT_DIR], fep);
+                       (void)fent_to_name(&l, fep);
                        printf("%d/%d: link - no filename from %s\n",
                                procid, opno, l.path);
                }
@@ -3556,7 +3860,7 @@ link_f(int opno, long r)
        e = link_path(&f, &l) < 0 ? errno : 0;
        check_cwd();
        if (e == 0)
-               add_to_flist(flp - flist, id, parid);
+               add_to_flist(flp - flist, id, parid, fep_src->xattr_counter);
        if (v) {
                printf("%d/%d: link %s %s %d\n", procid, opno, f.path, l.path,
                        e);
@@ -3566,6 +3870,56 @@ link_f(int opno, long r)
        free_pathname(&f);
 }
 
+void
+listfattr_f(int opno, long r)
+{
+       fent_t          *fep;
+       int             e;
+       pathname_t      f;
+       int             v;
+       char            *buffer = NULL;
+       int             buffer_len;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: listfattr - no filename\n", procid, opno);
+               goto out;
+       }
+       check_cwd();
+
+       e = listxattr(f.path, NULL, 0);
+       if (e < 0) {
+               if (v)
+                       printf("%d/%d: listfattr %s failed %d\n",
+                              procid, opno, f.path, errno);
+               goto out;
+       }
+       buffer_len = e;
+       if (buffer_len == 0) {
+               if (v)
+                       printf("%d/%d: listfattr %s - has no extended attributes\n",
+                              procid, opno, f.path);
+               goto out;
+       }
+
+       buffer = malloc(buffer_len);
+       if (!buffer) {
+               if (v)
+                       printf("%d/%d: listfattr %s failed to allocate buffer with %d bytes\n",
+                              procid, opno, f.path, buffer_len);
+               goto out;
+       }
+
+       e = listxattr(f.path, buffer, buffer_len) < 0 ? errno : 0;
+       if (v)
+               printf("%d/%d: listfattr %s buffer length %d %d\n",
+                      procid, opno, f.path, buffer_len, e);
+out:
+       free(buffer);
+       free_pathname(&f);
+}
+
 void
 mkdir_f(int opno, long r)
 {
@@ -3577,7 +3931,7 @@ mkdir_f(int opno, long r)
        int             v;
        int             v1;
 
-       if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
+       if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
                parid = -1;
        else
                parid = fep->id;
@@ -3586,7 +3940,7 @@ mkdir_f(int opno, long r)
        v |= v1;
        if (!e) {
                if (v) {
-                       (void)fent_to_name(&f, &flist[FT_DIR], fep);
+                       (void)fent_to_name(&f, fep);
                        printf("%d/%d: mkdir - no filename from %s\n",
                                procid, opno, f.path);
                }
@@ -3596,7 +3950,7 @@ mkdir_f(int opno, long r)
        e = mkdir_path(&f, 0777) < 0 ? errno : 0;
        check_cwd();
        if (e == 0)
-               add_to_flist(FT_DIR, id, parid);
+               add_to_flist(FT_DIR, id, parid, 0);
        if (v) {
                printf("%d/%d: mkdir %s %d\n", procid, opno, f.path, e);
                printf("%d/%d: mkdir add id=%d,parent=%d\n", procid, opno, id, parid);
@@ -3615,7 +3969,7 @@ mknod_f(int opno, long r)
        int             v;
        int             v1;
 
-       if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
+       if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
                parid = -1;
        else
                parid = fep->id;
@@ -3624,7 +3978,7 @@ mknod_f(int opno, long r)
        v |= v1;
        if (!e) {
                if (v) {
-                       (void)fent_to_name(&f, &flist[FT_DIR], fep);
+                       (void)fent_to_name(&f, fep);
                        printf("%d/%d: mknod - no filename from %s\n",
                                procid, opno, f.path);
                }
@@ -3634,7 +3988,7 @@ mknod_f(int opno, long r)
        e = mknod_path(&f, S_IFCHR|0444, 0) < 0 ? errno : 0;
        check_cwd();
        if (e == 0)
-               add_to_flist(FT_DEV, id, parid);
+               add_to_flist(FT_DEV, id, parid, 0);
        if (v) {
                printf("%d/%d: mknod %s %d\n", procid, opno, f.path, e);
                printf("%d/%d: mknod add id=%d,parent=%d\n", procid, opno, id, parid);
@@ -3963,7 +4317,60 @@ readv_f(int opno, long r)
 }
 
 void
-rename_f(int opno, long r)
+removefattr_f(int opno, long r)
+{
+       fent_t          *fep;
+       int             e;
+       pathname_t      f;
+       int             v;
+       char            name[XATTR_NAME_BUF_SIZE];
+       int             xattr_num;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: removefattr - no filename\n", procid, opno);
+               goto out;
+       }
+       check_cwd();
+
+       /*
+        * If the file/dir has xattrs, pick one randomly, otherwise attempt to
+        * remove a xattr that doesn't exist (fremovexattr should fail with
+        * errno set to ENOATTR (61) in this case).
+        */
+       if (fep->xattr_counter > 0)
+               xattr_num = (random() % fep->xattr_counter) + 1;
+       else
+               xattr_num = 0;
+
+       e = generate_xattr_name(xattr_num, name, sizeof(name));
+       if (e < 0) {
+               printf("%d/%d: removefattr - file %s failed to generate xattr name: %d\n",
+                      procid, opno, f.path, e);
+               goto out;
+       }
+
+       e = removexattr(f.path, name) < 0 ? errno : 0;
+       if (v)
+               printf("%d/%d: removefattr file %s name %s %d\n",
+                      procid, opno, f.path, name, e);
+out:
+       free_pathname(&f);
+}
+
+struct print_flags renameat2_flags [] = {
+       { RENAME_NOREPLACE, "NOREPLACE"},
+       { RENAME_EXCHANGE, "EXCHANGE"},
+       { RENAME_WHITEOUT, "WHITEOUT"},
+       { -1, NULL}
+};
+
+#define translate_renameat2_flags(mode)        \
+       ({translate_flags(mode, "|", renameat2_flags);})
+
+void
+do_renameat2(int opno, long r, int mode)
 {
        fent_t          *dfep;
        int             e;
@@ -3974,56 +4381,116 @@ rename_f(int opno, long r)
        pathname_t      newf;
        int             oldid;
        int             parid;
+       int             oldparid;
+       int             which;
        int             v;
        int             v1;
 
        /* get an existing path for the source of the rename */
        init_pathname(&f);
-       if (!get_fname(FT_ANYm, r, &f, &flp, &fep, &v1)) {
+       which = (mode == RENAME_WHITEOUT) ? FT_DEVm : FT_ANYm;
+       if (!get_fname(which, r, &f, &flp, &fep, &v1)) {
                if (v1)
-                       printf("%d/%d: rename - no filename\n", procid, opno);
+                       printf("%d/%d: rename - no source filename\n",
+                               procid, opno);
                free_pathname(&f);
                return;
        }
 
-       /* get an existing directory for the destination parent directory name */
-       if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
-               parid = -1;
-       else
-               parid = dfep->id;
-       v |= v1;
+       /*
+        * Both pathnames must exist for the RENAME_EXCHANGE, and in
+        * order to maintain filelist/filename integrity, we should
+        * restrict exchange operation to files of the same type.
+        */
+       if (mode == RENAME_EXCHANGE) {
+               which = 1 << (flp - flist);
+               init_pathname(&newf);
+               if (!get_fname(which, random(), &newf, NULL, &dfep, &v)) {
+                       if (v)
+                               printf("%d/%d: rename - no target filename\n",
+                                       procid, opno);
+                       free_pathname(&newf);
+                       free_pathname(&f);
+                       return;
+               }
+               if (which == FT_DIRm && (fents_ancestor_check(fep, dfep) ||
+                   fents_ancestor_check(dfep, fep))) {
+                       if (v)
+                               printf("%d/%d: rename(REXCHANGE) %s and %s "
+                                       "have ancestor-descendant relationship\n",
+                                       procid, opno, f.path, newf.path);
+                       free_pathname(&newf);
+                       free_pathname(&f);
+                       return;
+               }
+               v |= v1;
+               id = dfep->id;
+               parid = dfep->parent;
+       } else {
+               /*
+                * Get an existing directory for the destination parent
+                * directory name.
+                */
+               if (!get_fname(FT_DIRm, random(), NULL, NULL, &dfep, &v))
+                       parid = -1;
+               else
+                       parid = dfep->id;
+               v |= v1;
 
-       /* generate a new path using an existing parent directory in name */
-       init_pathname(&newf);
-       e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
-       v |= v1;
-       if (!e) {
-               if (v) {
-                       (void)fent_to_name(&f, &flist[FT_DIR], dfep);
-                       printf("%d/%d: rename - no filename from %s\n",
-                               procid, opno, f.path);
+               /*
+                * Generate a new path using an existing parent directory
+                * in name.
+                */
+               init_pathname(&newf);
+               e = generate_fname(dfep, flp - flist, &newf, &id, &v1);
+               v |= v1;
+               if (!e) {
+                       if (v) {
+                               (void)fent_to_name(&f, dfep);
+                               printf("%d/%d: rename - no filename from %s\n",
+                                       procid, opno, f.path);
+                       }
+                       free_pathname(&newf);
+                       free_pathname(&f);
+                       return;
                }
-               free_pathname(&newf);
-               free_pathname(&f);
-               return;
        }
-       e = rename_path(&f, &newf) < 0 ? errno : 0;
+       e = rename_path(&f, &newf, mode) < 0 ? errno : 0;
        check_cwd();
        if (e == 0) {
-               if (flp - flist == FT_DIR) {
-                       oldid = fep->id;
-                       fix_parent(oldid, id);
+               int xattr_counter = fep->xattr_counter;
+               bool swap = (mode == RENAME_EXCHANGE) ? true : false;
+               int ft = flp - flist;
+
+               oldid = fep->id;
+               oldparid = fep->parent;
+
+               /*
+                * Swap the parent ids for RENAME_EXCHANGE, and replace the
+                * old parent id for the others.
+                */
+               if (ft == FT_DIR || ft == FT_SUBVOL)
+                       fix_parent(oldid, id, swap);
+
+               if (mode == RENAME_WHITEOUT) {
+                       fep->xattr_counter = 0;
+                       add_to_flist(flp - flist, id, parid, xattr_counter);
+               } else if (mode == RENAME_EXCHANGE) {
+                       fep->xattr_counter = dfep->xattr_counter;
+                       dfep->xattr_counter = xattr_counter;
+               } else {
+                       del_from_flist(flp - flist, fep - flp->fents);
+                       add_to_flist(flp - flist, id, parid, xattr_counter);
                }
-               del_from_flist(flp - flist, fep - flp->fents);
-               add_to_flist(flp - flist, id, parid);
        }
        if (v) {
-               printf("%d/%d: rename %s to %s %d\n", procid, opno, f.path,
+               printf("%d/%d: rename(%s) %s to %s %d\n", procid,
+                       opno, translate_renameat2_flags(mode), f.path,
                        newf.path, e);
                if (e == 0) {
-                       printf("%d/%d: rename del entry: id=%d,parent=%d\n",
-                               procid, opno, fep->id, fep->parent);
-                       printf("%d/%d: rename add entry: id=%d,parent=%d\n",
+                       printf("%d/%d: rename source entry: id=%d,parent=%d\n",
+                               procid, opno, oldid, oldparid);
+                       printf("%d/%d: rename target entry: id=%d,parent=%d\n",
                                procid, opno, id, parid);
                }
        }
@@ -4031,6 +4498,30 @@ rename_f(int opno, long r)
        free_pathname(&f);
 }
 
+void
+rename_f(int opno, long r)
+{
+       do_renameat2(opno, r, 0);
+}
+
+void
+rnoreplace_f(int opno, long r)
+{
+       do_renameat2(opno, r, RENAME_NOREPLACE);
+}
+
+void
+rexchange_f(int opno, long r)
+{
+       do_renameat2(opno, r, RENAME_EXCHANGE);
+}
+
+void
+rwhiteout_f(int opno, long r)
+{
+       do_renameat2(opno, r, RENAME_WHITEOUT);
+}
+
 void
 resvsp_f(int opno, long r)
 {
@@ -4091,6 +4582,8 @@ rmdir_f(int opno, long r)
        int             e;
        pathname_t      f;
        fent_t          *fep;
+       int             oldid;
+       int             oldparid;
        int             v;
 
        init_pathname(&f);
@@ -4102,13 +4595,16 @@ rmdir_f(int opno, long r)
        }
        e = rmdir_path(&f) < 0 ? errno : 0;
        check_cwd();
-       if (e == 0)
+       if (e == 0) {
+               oldid = fep->id;
+               oldparid = fep->parent;
                del_from_flist(FT_DIR, fep - flist[FT_DIR].fents);
+       }
        if (v) {
                printf("%d/%d: rmdir %s %d\n", procid, opno, f.path, e);
                if (e == 0)
                        printf("%d/%d: rmdir del entry: id=%d,parent=%d\n",
-                               procid, opno, fep->id, fep->parent);
+                               procid, opno, oldid, oldparid);
        }
        free_pathname(&f);
 }
@@ -4137,6 +4633,130 @@ setattr_f(int opno, long r)
        close(fd);
 }
 
+void
+setfattr_f(int opno, long r)
+{
+       int             e;
+       pathname_t      f;
+       fent_t          *fep;
+       int             v;
+       int             value_len;
+       char            name[XATTR_NAME_BUF_SIZE];
+       char            *value = NULL;
+       int             flag = 0;
+       int             xattr_num;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE | FT_ANYDIR, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: setfattr - no filename\n", procid, opno);
+               goto out;
+       }
+       check_cwd();
+
+       if ((fep->xattr_counter > 0) && (random() % 2)) {
+               /*
+                * Use an existing xattr name for replacing its value or
+                * create again a xattr that was previously deleted.
+                */
+               xattr_num = (random() % fep->xattr_counter) + 1;
+               if (random() % 2)
+                       flag = XATTR_REPLACE;
+       } else {
+               /* Use a new xattr name. */
+               xattr_num = fep->xattr_counter + 1;
+               /*
+                * Don't always use the create flag because even if our xattr
+                * counter is 0, we may still have xattrs associated to this
+                * file (this happens when xattrs are added to a file through
+                * one of its other hard links), so we can end up updating an
+                * existing xattr too.
+                */
+               if (random() % 2)
+                       flag = XATTR_CREATE;
+       }
+
+       /*
+        * The maximum supported value size depends on the filesystem
+        * implementation, but 100 bytes is a safe value for most filesystems
+        * at least.
+        */
+       value_len = random() % 101;
+       value = gen_random_string(value_len);
+       if (!value && value_len > 0) {
+               if (v)
+                       printf("%d/%d: setfattr - file %s failed to allocate value with %d bytes\n",
+                              procid, opno, f.path, value_len);
+               goto out;
+       }
+       e = generate_xattr_name(xattr_num, name, sizeof(name));
+       if (e < 0) {
+               printf("%d/%d: setfattr - file %s failed to generate xattr name: %d\n",
+                      procid, opno, f.path, e);
+               goto out;
+       }
+
+       e = setxattr(f.path, name, value, value_len, flag) < 0 ? errno : 0;
+       if (e == 0)
+               fep->xattr_counter++;
+       if (v)
+               printf("%d/%d: setfattr file %s name %s flag %s value length %d: %d\n",
+                      procid, opno, f.path, name, xattr_flag_to_string(flag),
+                      value_len, e);
+out:
+       free(value);
+       free_pathname(&f);
+}
+
+void
+snapshot_f(int opno, long r)
+{
+#ifdef HAVE_BTRFSUTIL_H
+       enum btrfs_util_error   e;
+       pathname_t              f;
+       pathname_t              newf;
+       fent_t                  *fep;
+       int                     id;
+       int                     parid;
+       int                     v;
+       int                     v1;
+       int                     err;
+
+       init_pathname(&f);
+       if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: snapshot - no subvolume\n", procid,
+                              opno);
+               free_pathname(&f);
+               return;
+       }
+       init_pathname(&newf);
+       parid = fep->id;
+       err = generate_fname(fep, FT_SUBVOL, &newf, &id, &v1);
+       v |= v1;
+       if (!err) {
+               if (v) {
+                       (void)fent_to_name(&f, fep);
+                       printf("%d/%d: snapshot - no filename from %s\n",
+                              procid, opno, f.path);
+               }
+               free_pathname(&f);
+               return;
+       }
+       e = btrfs_util_create_snapshot(f.path, newf.path, 0, NULL, NULL);
+       if (e == BTRFS_UTIL_OK)
+               add_to_flist(FT_SUBVOL, id, parid, 0);
+       if (v) {
+               printf("%d/%d: snapshot %s->%s %d(%s)\n", procid, opno,
+                      f.path, newf.path, e, btrfs_util_strerror(e));
+               printf("%d/%d: snapshot add id=%d,parent=%d\n", procid, opno,
+                      id, parid);
+       }
+       free_pathname(&newf);
+       free_pathname(&f);
+#endif
+}
+
 void
 stat_f(int opno, long r)
 {
@@ -4159,6 +4779,86 @@ stat_f(int opno, long r)
        free_pathname(&f);
 }
 
+void
+subvol_create_f(int opno, long r)
+{
+#ifdef HAVE_BTRFSUTIL_H
+       enum btrfs_util_error   e;
+       pathname_t              f;
+       fent_t                  *fep;
+       int                     id;
+       int                     parid;
+       int                     v;
+       int                     v1;
+       int                     err;
+
+       init_pathname(&f);
+       if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
+               parid = -1;
+       else
+               parid = fep->id;
+       err = generate_fname(fep, FT_SUBVOL, &f, &id, &v1);
+       v |= v1;
+       if (!err) {
+               if (v) {
+                       (void)fent_to_name(&f, fep);
+                       printf("%d/%d: subvol_create - no filename from %s\n",
+                              procid, opno, f.path);
+               }
+               free_pathname(&f);
+               return;
+       }
+       e = btrfs_util_create_subvolume(f.path, 0, NULL, NULL);
+       if (e == BTRFS_UTIL_OK)
+               add_to_flist(FT_SUBVOL, id, parid, 0);
+       if (v) {
+               printf("%d/%d: subvol_create %s %d(%s)\n", procid, opno,
+                      f.path, e, btrfs_util_strerror(e));
+               printf("%d/%d: subvol_create add id=%d,parent=%d\n", procid,
+                      opno, id, parid);
+       }
+       free_pathname(&f);
+#endif
+}
+
+void
+subvol_delete_f(int opno, long r)
+{
+#ifdef HAVE_BTRFSUTIL_H
+       enum btrfs_util_error   e;
+       pathname_t              f;
+       fent_t                  *fep;
+       int                     v;
+       int                     oldid;
+       int                     oldparid;
+
+       init_pathname(&f);
+       if (!get_fname(FT_SUBVOLm, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d:%d: subvol_delete - no subvolume\n", procid,
+                              opno);
+               free_pathname(&f);
+               return;
+       }
+       e = btrfs_util_delete_subvolume(f.path, 0);
+       check_cwd();
+       if (e == BTRFS_UTIL_OK) {
+               oldid = fep->id;
+               oldparid = fep->parent;
+               delete_subvol_children(oldid);
+               del_from_flist(FT_SUBVOL, fep - flist[FT_SUBVOL].fents);
+       }
+       if (v) {
+               printf("%d/%d: subvol_delete %s %d(%s)\n", procid, opno, f.path,
+                      e, btrfs_util_strerror(e));
+               if (e == BTRFS_UTIL_OK)
+                       printf("%d/%d: subvol_delete del entry: id=%d,parent=%d\n",
+                              procid, opno, oldid, oldparid);
+       }
+       free_pathname(&f);
+#endif
+}
+
 void
 symlink_f(int opno, long r)
 {
@@ -4173,7 +4873,7 @@ symlink_f(int opno, long r)
        int             v1;
        char            *val;
 
-       if (!get_fname(FT_DIRm, r, NULL, NULL, &fep, &v))
+       if (!get_fname(FT_ANYDIR, r, NULL, NULL, &fep, &v))
                parid = -1;
        else
                parid = fep->id;
@@ -4182,7 +4882,7 @@ symlink_f(int opno, long r)
        v |= v1;
        if (!e) {
                if (v) {
-                       (void)fent_to_name(&f, &flist[FT_DIR], fep);
+                       (void)fent_to_name(&f, fep);
                        printf("%d/%d: symlink - no filename from %s\n",
                                procid, opno, f.path);
                }
@@ -4199,7 +4899,7 @@ symlink_f(int opno, long r)
        e = symlink_path(val, &f) < 0 ? errno : 0;
        check_cwd();
        if (e == 0)
-               add_to_flist(FT_SYM, id, parid);
+               add_to_flist(FT_SYM, id, parid, 0);
        free(val);
        if (v) {
                printf("%d/%d: symlink %s %d\n", procid, opno, f.path, e);
@@ -4263,6 +4963,8 @@ unlink_f(int opno, long r)
        pathname_t      f;
        fent_t          *fep;
        flist_t         *flp;
+       int             oldid;
+       int             oldparid;
        int             v;
 
        init_pathname(&f);
@@ -4274,13 +4976,16 @@ unlink_f(int opno, long r)
        }
        e = unlink_path(&f) < 0 ? errno : 0;
        check_cwd();
-       if (e == 0)
+       if (e == 0) {
+               oldid = fep->id;
+               oldparid = fep->parent;
                del_from_flist(flp - flist, fep - flp->fents);
+       }
        if (v) {
                printf("%d/%d: unlink %s %d\n", procid, opno, f.path, e);
                if (e == 0)
                        printf("%d/%d: unlink del entry: id=%d,parent=%d\n",
-                               procid, opno, fep->id, fep->parent);
+                               procid, opno, oldid, oldparid);
        }
        free_pathname(&f);
 }
@@ -4468,3 +5173,13 @@ writev_f(int opno, long r)
        free_pathname(&f);
        close(fd);
 }
+
+char *
+xattr_flag_to_string(int flag)
+{
+       if (flag == XATTR_CREATE)
+               return "create";
+       if (flag == XATTR_REPLACE)
+               return "replace";
+       return "none";
+}