s3/vfs: add SMB_VFS_OFFLOAD_READ_SEND/RECV
[samba.git] / source3 / modules / vfs_fruit.c
index cbd1477059fab657bdf4c090451f492094f2d252..20d15b39af7eacea4f65de60f566d0af5f1107b4 100644 (file)
@@ -31,6 +31,8 @@
 #include "../libcli/smb/smb2_create_ctx.h"
 #include "lib/util/sys_rw.h"
 #include "lib/util/tevent_ntstatus.h"
+#include "lib/util/tevent_unix.h"
+#include "offload_token.h"
 
 /*
  * Enhanced OS X and Netatalk compatibility
 
 static int vfs_fruit_debug_level = DBGC_VFS;
 
+static struct global_fruit_config {
+       bool nego_aapl; /* client negotiated AAPL */
+
+} global_fruit_config;
+
 #undef DBGC_CLASS
 #define DBGC_CLASS vfs_fruit_debug_level
 
@@ -124,13 +131,13 @@ struct fruit_config_data {
        enum fruit_locking locking;
        enum fruit_encoding encoding;
        bool use_aapl;          /* config from smb.conf */
-       bool nego_aapl;         /* client negotiated AAPL */
        bool use_copyfile;
        bool readdir_attr_enabled;
        bool unix_info_enabled;
        bool copyfile_enabled;
        bool veto_appledouble;
        bool posix_rename;
+       bool aapl_zero_file_id;
 
        /*
         * Additional options, all enabled by default,
@@ -349,7 +356,8 @@ struct ad_entry {
 
 struct adouble {
        vfs_handle_struct        *ad_handle;
-       files_struct             *ad_fsp;
+       int                       ad_fd;
+       bool                      ad_opened;
        adouble_type_t            ad_type;
        uint32_t                  ad_magic;
        uint32_t                  ad_version;
@@ -402,13 +410,24 @@ static const uint32_t set_eid[] = {
        AD_DEV, AD_INO, AD_SYN, AD_ID
 };
 
+struct fio {
+       /* tcon config handle */
+       struct fruit_config_data *config;
+
+       /* Denote stream type, meta or rsrc */
+       adouble_type_t type;
+};
+
 /*
  * Forward declarations
  */
 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
-                              adouble_type_t type, files_struct *fsp);
-static int ad_write(struct adouble *ad, const char *path);
-static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out);
+                              adouble_type_t type);
+static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
+static int ad_fset(struct adouble *ad, files_struct *fsp);
+static int adouble_path(TALLOC_CTX *ctx,
+                       const struct smb_filename *smb_fname__in,
+                       struct smb_filename **ppsmb_fname_out);
 
 /**
  * Return a pointer to an AppleDouble entry
@@ -769,15 +788,16 @@ exit:
 /**
  * Read and parse Netatalk AppleDouble metadata xattr
  **/
-static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
+static ssize_t ad_read_meta(struct adouble *ad,
+                               const struct smb_filename *smb_fname)
 {
        int      rc = 0;
        ssize_t  ealen;
        bool     ok;
 
-       DEBUG(10, ("reading meta xattr for %s\n", path));
+       DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
 
-       ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, path,
+       ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
                                 AFPINFO_EA_NETATALK, ad->ad_data,
                                 AD_DATASZ_XATTR);
        if (ealen == -1) {
@@ -827,13 +847,14 @@ static ssize_t ad_header_read_meta(struct adouble *ad, const char *path)
        }
 
 exit:
-       DEBUG(10, ("reading meta xattr for %s, rc: %d\n", path, rc));
+       DEBUG(10, ("reading meta xattr for %s, rc: %d\n",
+               smb_fname->base_name, rc));
 
        if (rc != 0) {
                ealen = -1;
                if (errno == EINVAL) {
                        become_root();
-                       removexattr(path, AFPINFO_EA_NETATALK);
+                       removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
                        unbecome_root();
                        errno = ENOENT;
                }
@@ -841,214 +862,256 @@ exit:
        return ealen;
 }
 
-/**
- * Read and parse resource fork, either ._ AppleDouble file or xattr
- **/
-static ssize_t ad_header_read_rsrc(struct adouble *ad, const char *path)
+static int ad_open_meta(const struct smb_filename *smb_fname,
+                       int flags,
+                       mode_t mode)
+{
+       return open(smb_fname->base_name, flags, mode);
+}
+
+static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
+                               int flags,
+                               mode_t mode)
+{
+#ifdef HAVE_ATTROPEN
+       /* FIXME: direct Solaris xattr syscall */
+       return attropen(smb_fname->base_name,
+                       AFPRESOURCE_EA_NETATALK, flags, mode);
+#else
+       errno = ENOSYS;
+       return -1;
+#endif
+}
+
+static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
+                               int flags,
+                               mode_t mode)
+{
+       int ret;
+       int fd;
+       struct smb_filename *adp_smb_fname = NULL;
+
+       ret = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
+       if (ret != 0) {
+               return -1;
+       }
+
+       fd = open(adp_smb_fname->base_name, flags, mode);
+       TALLOC_FREE(adp_smb_fname);
+
+       return fd;
+}
+
+static int ad_open_rsrc(vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       int flags,
+                       mode_t mode)
 {
        struct fruit_config_data *config = NULL;
-       int fd = -1;
-       int rc = 0;
-       ssize_t len;
-       char *adpath = NULL;
-       bool opened = false;
-       int mode;
-       struct adouble *meta_ad = NULL;
-       SMB_STRUCT_STAT sbuf;
-       bool ok;
-       int saved_errno = 0;
+       int fd;
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return -1);
 
-       /* Try rw first so we can use the fd in ad_convert() */
-       mode = O_RDWR;
-
-       if (ad->ad_fsp && ad->ad_fsp->fh && (ad->ad_fsp->fh->fd != -1)) {
-               fd = ad->ad_fsp->fh->fd;
+       if (config->rsrc == FRUIT_RSRC_XATTR) {
+               fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
        } else {
-               if (config->rsrc == FRUIT_RSRC_XATTR) {
-                       adpath = talloc_strdup(talloc_tos(), path);
-               } else {
-                       rc = adouble_path(talloc_tos(), path, &adpath);
-                       if (rc != 0) {
-                               goto exit;
-                       }
-               }
+               fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
+       }
 
-       retry:
-               if (config->rsrc == FRUIT_RSRC_XATTR) {
-#ifndef HAVE_ATTROPEN
-                       errno = ENOSYS;
-                       rc = -1;
-                       goto exit;
-#else
-                       /* FIXME: direct Solaris xattr syscall */
-                       fd = attropen(adpath, AFPRESOURCE_EA_NETATALK,
-                                     mode, 0);
-#endif
-               } else {
-                       /* FIXME: direct open(), don't have an fsp */
-                       fd = open(adpath, mode);
-               }
+       return fd;
+}
 
-               if (fd == -1) {
-                       switch (errno) {
-                       case EROFS:
-                       case EACCES:
-                               if (mode == O_RDWR) {
-                                       mode = O_RDONLY;
-                                       goto retry;
-                               }
-                               /* fall through ... */
-                       default:
-                               DEBUG(2, ("open AppleDouble: %s, %s\n",
-                                         adpath, strerror(errno)));
-                               rc = -1;
-                               goto exit;
-                       }
-               }
-               opened = true;
+static int ad_open(vfs_handle_struct *handle,
+                  struct adouble *ad,
+                  const struct smb_filename *smb_fname,
+                  adouble_type_t t,
+                  int flags,
+                  mode_t mode)
+{
+       int fd;
+
+       DBG_DEBUG("Path [%s] type [%s]\n",
+                 smb_fname->base_name, t == ADOUBLE_META ? "meta" : "rsrc");
+
+       if (t == ADOUBLE_META) {
+               fd = ad_open_meta(smb_fname, flags, mode);
+       } else {
+               fd = ad_open_rsrc(handle, smb_fname, flags, mode);
        }
 
-       if (config->rsrc == FRUIT_RSRC_XATTR) {
-               /* FIXME: direct sys_fstat(), don't have an fsp */
-               rc = sys_fstat(
-                       fd, &sbuf,
+       if (fd != -1) {
+               ad->ad_opened = true;
+               ad->ad_fd = fd;
+       }
+
+       DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
+                 smb_fname->base_name,
+                 t == ADOUBLE_META ? "meta" : "rsrc", fd);
+
+       return fd;
+}
+
+static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
+{
+       int ret;
+       SMB_STRUCT_STAT st;
+
+       /* FIXME: direct sys_fstat(), don't have an fsp */
+       ret = sys_fstat(ad->ad_fd, &st,
                        lp_fake_directory_create_times(
                                SNUM(ad->ad_handle->conn)));
-               if (rc != 0) {
-                       goto exit;
-               }
-               len = sbuf.st_ex_size;
-               ad_setentrylen(ad, ADEID_RFORK, len);
-       } else {
-               /* FIXME: direct sys_pread(), don't have an fsp */
-               len = sys_pread(fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
-               if (len != AD_DATASZ_DOT_UND) {
-                       DEBUG(2, ("%s: bad size: %zd\n",
-                                 strerror(errno), len));
-                       rc = -1;
-                       goto exit;
-               }
+       if (ret != 0) {
+               return -1;
+       }
 
-               /* FIXME: direct sys_fstat(), we don't have an fsp */
-               rc = sys_fstat(fd, &sbuf,
-                              lp_fake_directory_create_times(
-                                      SNUM(ad->ad_handle->conn)));
-               if (rc != 0) {
-                       goto exit;
-               }
+       ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
+       return st.st_ex_size;
+}
 
-               /* Now parse entries */
-               ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
-               if (!ok) {
-                       DEBUG(1, ("invalid AppleDouble resource %s\n", path));
-                       errno = EINVAL;
-                       rc = -1;
-                       goto exit;
-               }
+static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
+                               const struct smb_filename *smb_fname)
+{
+       struct adouble *meta_ad = NULL;
+       SMB_STRUCT_STAT sbuf;
+       char *p_ad = NULL;
+       char *p_meta_ad = NULL;
+       ssize_t len;
+       int ret;
+       bool ok;
 
-               if ((ad_getentryoff(ad, ADEID_FINDERI)
-                    != ADEDOFF_FINDERI_DOT_UND)
-                   || (ad_getentrylen(ad, ADEID_FINDERI)
-                       < ADEDLEN_FINDERI)
-                   || (ad_getentryoff(ad, ADEID_RFORK)
-                       < ADEDOFF_RFORK_DOT_UND)) {
-                       DEBUG(2, ("invalid AppleDouble resource %s\n", path));
-                       errno = EINVAL;
-                       rc = -1;
-                       goto exit;
-               }
+       len = sys_pread(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
+       if (len != AD_DATASZ_DOT_UND) {
+               DBG_NOTICE("%s %s: bad size: %zd\n",
+                          smb_fname->base_name, strerror(errno), len);
+               return -1;
+       }
 
-               if ((mode == O_RDWR)
-                   && (ad_getentrylen(ad, ADEID_FINDERI) > ADEDLEN_FINDERI)) {
-                       char *p_ad = NULL;
-                       char *p_meta_ad = NULL;
+       ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
+                               SNUM(ad->ad_handle->conn)));
+       if (ret != 0) {
+               return -1;
+       }
 
-                       rc = ad_convert(ad, fd);
-                       if (rc != 0) {
-                               rc = -1;
-                               goto exit;
-                       }
-                       /*
-                        * Can't use ad_write() because we might not have a fsp
-                        */
-                       ok = ad_pack(ad);
-                       if (!ok) {
-                               rc = -1;
-                               goto exit;
-                       }
-                       /* FIXME: direct sys_pwrite(), don't have an fsp */
-                       len = sys_pwrite(fd, ad->ad_data,
-                                        AD_DATASZ_DOT_UND, 0);
-                       if (len != AD_DATASZ_DOT_UND) {
-                               DEBUG(2, ("%s: bad size: %zd\n", adpath, len));
-                               rc = -1;
-                               goto exit;
-                       }
+       /* Now parse entries */
+       ok = ad_unpack(ad, ADEID_NUM_DOT_UND, sbuf.st_ex_size);
+       if (!ok) {
+               DBG_ERR("invalid AppleDouble resource %s\n",
+                       smb_fname->base_name);
+               errno = EINVAL;
+               return -1;
+       }
 
-                       meta_ad = ad_init(talloc_tos(), ad->ad_handle,
-                                         ADOUBLE_META, NULL);
-                       if (meta_ad == NULL) {
-                               rc = -1;
-                               goto exit;
-                       }
+       if ((ad_getentryoff(ad, ADEID_FINDERI) != ADEDOFF_FINDERI_DOT_UND)
+           || (ad_getentrylen(ad, ADEID_FINDERI) < ADEDLEN_FINDERI)
+           || (ad_getentryoff(ad, ADEID_RFORK) < ADEDOFF_RFORK_DOT_UND)) {
+               DBG_ERR("invalid AppleDouble resource %s\n",
+                       smb_fname->base_name);
+               errno = EINVAL;
+               return -1;
+       }
 
-                       p_ad = ad_get_entry(ad, ADEID_FINDERI);
-                       if (p_ad == NULL) {
-                               rc = -1;
-                               goto exit;
-                       }
-                       p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
-                       if (p_meta_ad == NULL) {
-                               rc = -1;
-                               goto exit;
-                       }
+       if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
+               return len;
+       }
+
+       /*
+        * Try to fixup AppleDouble files created by OS X with xattrs
+        * appended to the ADEID_FINDERI entry. We simply remove the
+        * xattrs blob, this means any fancy xattr that was stored
+        * there is lost.
+        */
 
-                       memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
+       ret = ad_convert(ad, ad->ad_fd);
+       if (ret != 0) {
+               DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
+               return len;
+       }
 
-                       rc = ad_write(meta_ad, path);
-                       if (rc != 0) {
-                               rc = -1;
-                               goto exit;
-                       }
-               }
+       ok = ad_pack(ad);
+       if (!ok) {
+               DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
+               return -1;
        }
 
-       DEBUG(10, ("opened AppleDouble: %s\n", path));
+       len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
+       if (len != AD_DATASZ_DOT_UND) {
+               DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
+               return -1;
+       }
 
-exit:
-       if (rc != 0) {
-               saved_errno = errno;
-               len = -1;
+       meta_ad = ad_init(talloc_tos(), ad->ad_handle, ADOUBLE_META);
+       if (meta_ad == NULL) {
+               return -1;
        }
-       if (opened && fd != -1) {
-               close(fd);
+
+       p_ad = ad_get_entry(ad, ADEID_FINDERI);
+       if (p_ad == NULL) {
+               TALLOC_FREE(meta_ad);
+               return -1;
+       }
+       p_meta_ad = ad_get_entry(meta_ad, ADEID_FINDERI);
+       if (p_meta_ad == NULL) {
+               TALLOC_FREE(meta_ad);
+               return -1;
        }
-       TALLOC_FREE(adpath);
+
+       memcpy(p_meta_ad, p_ad, ADEDLEN_FINDERI);
+
+       ret = ad_set(meta_ad, smb_fname);
        TALLOC_FREE(meta_ad);
-       if (rc != 0) {
-               errno = saved_errno;
+       if (ret != 0) {
+               return -1;
+       }
+
+       return len;
+}
+
+/**
+ * Read and parse resource fork, either ._ AppleDouble file or xattr
+ **/
+static ssize_t ad_read_rsrc(struct adouble *ad,
+                       const struct smb_filename *smb_fname)
+{
+       struct fruit_config_data *config = NULL;
+       ssize_t len;
+
+       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+                               struct fruit_config_data, return -1);
+
+       if (config->rsrc == FRUIT_RSRC_XATTR) {
+               len = ad_read_rsrc_xattr(ad);
+       } else {
+               len = ad_read_rsrc_adouble(ad, smb_fname);
        }
+
        return len;
 }
 
 /**
  * Read and unpack an AppleDouble metadata xattr or resource
  **/
-static ssize_t ad_read(struct adouble *ad, const char *path)
+static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
 {
        switch (ad->ad_type) {
        case ADOUBLE_META:
-               return ad_header_read_meta(ad, path);
+               return ad_read_meta(ad, smb_fname);
        case ADOUBLE_RSRC:
-               return ad_header_read_rsrc(ad, path);
+               return ad_read_rsrc(ad, smb_fname);
        default:
                return -1;
        }
 }
 
+static int adouble_destructor(struct adouble *ad)
+{
+       if ((ad->ad_fd != -1) && ad->ad_opened) {
+               close(ad->ad_fd);
+               ad->ad_fd = -1;
+       }
+       return 0;
+}
+
 /**
  * Allocate a struct adouble without initialiing it
  *
@@ -1058,14 +1121,11 @@ static ssize_t ad_read(struct adouble *ad, const char *path)
  * @param[in] ctx        talloc context
  * @param[in] handle     vfs handle
  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
-
- * @param[in] fsp        if not NULL (for stream IO), the adouble handle is
- *                       added as an fsp extension
  *
  * @return               adouble handle
  **/
 static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
-                               adouble_type_t type, files_struct *fsp)
+                               adouble_type_t type)
 {
        int rc = 0;
        size_t adsize = 0;
@@ -1088,39 +1148,27 @@ static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                return NULL;
        }
 
-       if (!fsp) {
-               ad = talloc_zero(ctx, struct adouble);
-               if (ad == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (adsize) {
-                       ad->ad_data = talloc_zero_array(ad, char, adsize);
-               }
-       } else {
-               ad = (struct adouble *)VFS_ADD_FSP_EXTENSION(handle, fsp,
-                                                            struct adouble,
-                                                            NULL);
-               if (ad == NULL) {
+       ad = talloc_zero(ctx, struct adouble);
+       if (ad == NULL) {
+               rc = -1;
+               goto exit;
+       }
+
+       if (adsize) {
+               ad->ad_data = talloc_zero_array(ad, char, adsize);
+               if (ad->ad_data == NULL) {
                        rc = -1;
                        goto exit;
                }
-               if (adsize) {
-                       ad->ad_data = talloc_zero_array(
-                               VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                               char, adsize);
-               }
-               ad->ad_fsp = fsp;
        }
 
-       if (adsize && ad->ad_data == NULL) {
-               rc = -1;
-               goto exit;
-       }
        ad->ad_handle = handle;
        ad->ad_type = type;
        ad->ad_magic = AD_MAGIC;
        ad->ad_version = AD_VERSION;
+       ad->ad_fd = -1;
+
+       talloc_set_destructor(ad, adouble_destructor);
 
 exit:
        if (rc != 0) {
@@ -1135,12 +1183,11 @@ exit:
  * @param[in] ctx        talloc context
  * @param[in] handle     vfs handle
  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
- * @param[in] fsp        file handle, may be NULL for a type of e_ad_meta
  *
  * @return               adouble handle, initialized
  **/
 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
-                              adouble_type_t type, files_struct *fsp)
+                              adouble_type_t type)
 {
        int rc = 0;
        const struct ad_entry_order  *eid;
@@ -1166,7 +1213,7 @@ static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                return NULL;
        }
 
-       ad = ad_alloc(ctx, handle, type, fsp);
+       ad = ad_alloc(ctx, handle, type);
        if (ad == NULL) {
                return NULL;
        }
@@ -1194,37 +1241,68 @@ static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
  *
  * @param[in] ctx      talloc context
  * @param[in] handle   vfs handle
- * @param[in] path     pathname to file or directory
+ * @param[in] smb_fname     pathname to file or directory
  * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
  *
  * @return             talloced struct adouble or NULL on error
  **/
 static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
-                             const char *path, adouble_type_t type)
+                       const struct smb_filename *smb_fname,
+                       adouble_type_t type)
 {
        int rc = 0;
        ssize_t len;
        struct adouble *ad = NULL;
+       int fd;
+       int mode;
 
        DEBUG(10, ("ad_get(%s) called for %s\n",
-                  type == ADOUBLE_META ? "meta" : "rsrc", path));
+                  type == ADOUBLE_META ? "meta" : "rsrc",
+                  smb_fname->base_name));
 
-       ad = ad_alloc(ctx, handle, type, NULL);
+       ad = ad_alloc(ctx, handle, type);
        if (ad == NULL) {
                rc = -1;
                goto exit;
        }
 
-       len = ad_read(ad, path);
+       /*
+        * Here's the deal: for ADOUBLE_META we can do without an fd
+        * as we can issue path based xattr calls. For ADOUBLE_RSRC
+        * however we need a full-fledged fd for file IO on the ._
+        * file.
+        */
+       if (type == ADOUBLE_RSRC) {
+               /* Try rw first so we can use the fd in ad_convert() */
+               mode = O_RDWR;
+
+               fd = ad_open(handle, ad, smb_fname, ADOUBLE_RSRC, mode, 0);
+               if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
+                       mode = O_RDONLY;
+                       fd = ad_open(handle, ad, smb_fname,
+                                       ADOUBLE_RSRC, mode, 0);
+               }
+
+               if (fd == -1) {
+                       DBG_DEBUG("ad_open [%s] error [%s]\n",
+                                 smb_fname->base_name, strerror(errno));
+                       rc = -1;
+                       goto exit;
+               }
+       }
+
+       len = ad_read(ad, smb_fname);
        if (len == -1) {
-               DEBUG(10, ("error reading AppleDouble for %s\n", path));
+               DEBUG(10, ("error reading AppleDouble for %s\n",
+                       smb_fname->base_name));
                rc = -1;
                goto exit;
        }
 
 exit:
        DEBUG(10, ("ad_get(%s) for %s returning %d\n",
-                 type == ADOUBLE_META ? "meta" : "rsrc", path, rc));
+                 type == ADOUBLE_META ? "meta" : "rsrc",
+                 smb_fname->base_name, rc));
 
        if (rc != 0) {
                TALLOC_FREE(ad);
@@ -1233,53 +1311,181 @@ exit:
 }
 
 /**
- * Set AppleDouble metadata on a file or directory
+ * Return AppleDouble data for a file
  *
- * @param[in] ad      adouble handle
-
- * @param[in] path    pathname to file or directory, may be NULL for a
- *                    resource fork
+ * @param[in] ctx      talloc context
+ * @param[in] handle   vfs handle
+ * @param[in] fsp      fsp to use for IO
+ * @param[in] type     type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
  *
- * @return            status code, 0 means success
+ * @return             talloced struct adouble or NULL on error
  **/
-static int ad_write(struct adouble *ad, const char *path)
+static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
+                              files_struct *fsp, adouble_type_t type)
 {
        int rc = 0;
        ssize_t len;
-       bool ok;
+       struct adouble *ad = NULL;
 
-       ok = ad_pack(ad);
-       if (!ok) {
-               return -1;
-       }
+       DBG_DEBUG("ad_get(%s) path [%s]\n",
+                 type == ADOUBLE_META ? "meta" : "rsrc",
+                 fsp_str_dbg(fsp));
 
-       switch (ad->ad_type) {
-       case ADOUBLE_META:
-               rc = SMB_VFS_SETXATTR(ad->ad_handle->conn, path,
-                                     AFPINFO_EA_NETATALK, ad->ad_data,
-                                     AD_DATASZ_XATTR, 0);
-               break;
-       case ADOUBLE_RSRC:
-               if ((ad->ad_fsp == NULL)
-                   || (ad->ad_fsp->fh == NULL)
-                   || (ad->ad_fsp->fh->fd == -1)) {
-                       rc = -1;
-                       goto exit;
-               }
-               /* FIXME: direct sys_pwrite(), don't have an fsp */
-               len = sys_pwrite(ad->ad_fsp->fh->fd, ad->ad_data,
-                                talloc_get_size(ad->ad_data), 0);
-               if (len != talloc_get_size(ad->ad_data)) {
-                       DEBUG(1, ("short write on %s: %zd",
-                                 fsp_str_dbg(ad->ad_fsp), len));
-                       rc = -1;
-                       goto exit;
-               }
-               break;
-       default:
-               return -1;
+       ad = ad_alloc(ctx, handle, type);
+       if (ad == NULL) {
+               rc = -1;
+               goto exit;
        }
+
+       if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
+               ad->ad_fd = fsp->fh->fd;
+       } else {
+               /*
+                * Here's the deal: for ADOUBLE_META we can do without an fd
+                * as we can issue path based xattr calls. For ADOUBLE_RSRC
+                * however we need a full-fledged fd for file IO on the ._
+                * file.
+                */
+               int fd;
+               int mode;
+
+               if (type == ADOUBLE_RSRC) {
+                       /* Try rw first so we can use the fd in ad_convert() */
+                       mode = O_RDWR;
+
+                       fd = ad_open(handle, ad, fsp->base_fsp->fsp_name,
+                                       ADOUBLE_RSRC, mode, 0);
+                       if (fd == -1 &&
+                           ((errno == EROFS) || (errno == EACCES)))
+                       {
+                               mode = O_RDONLY;
+                               fd = ad_open(handle, ad,
+                                       fsp->base_fsp->fsp_name, ADOUBLE_RSRC,
+                                       mode, 0);
+                       }
+
+                       if (fd == -1) {
+                               DBG_DEBUG("error opening AppleDouble for %s\n",
+                                       fsp_str_dbg(fsp));
+                               rc = -1;
+                               goto exit;
+                       }
+               }
+       }
+
+       len = ad_read(ad, fsp->base_fsp->fsp_name);
+       if (len == -1) {
+               DBG_DEBUG("error reading AppleDouble for %s\n",
+                       fsp_str_dbg(fsp));
+               rc = -1;
+               goto exit;
+       }
+
 exit:
+       DBG_DEBUG("ad_get(%s) path [%s] rc [%d]\n",
+                 type == ADOUBLE_META ? "meta" : "rsrc",
+                 fsp_str_dbg(fsp), rc);
+
+       if (rc != 0) {
+               TALLOC_FREE(ad);
+       }
+       return ad;
+}
+
+/**
+ * Set AppleDouble metadata on a file or directory
+ *
+ * @param[in] ad      adouble handle
+ *
+ * @param[in] smb_fname    pathname to file or directory
+ *
+ * @return            status code, 0 means success
+ **/
+static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
+{
+       bool ok;
+       int ret;
+
+       DBG_DEBUG("Path [%s]\n", smb_fname->base_name);
+
+       if (ad->ad_type != ADOUBLE_META) {
+               DBG_ERR("ad_set on [%s] used with ADOUBLE_RSRC\n",
+                       smb_fname->base_name);
+               return -1;
+       }
+
+       ok = ad_pack(ad);
+       if (!ok) {
+               return -1;
+       }
+
+       ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
+                              smb_fname,
+                              AFPINFO_EA_NETATALK,
+                              ad->ad_data,
+                              AD_DATASZ_XATTR, 0);
+
+       DBG_DEBUG("Path [%s] ret [%d]\n", smb_fname->base_name, ret);
+
+       return ret;
+}
+
+/**
+ * Set AppleDouble metadata on a file or directory
+ *
+ * @param[in] ad      adouble handle
+ * @param[in] fsp     file handle
+ *
+ * @return            status code, 0 means success
+ **/
+static int ad_fset(struct adouble *ad, files_struct *fsp)
+{
+       int rc = -1;
+       ssize_t len;
+       bool ok;
+
+       DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
+
+       if ((fsp == NULL)
+           || (fsp->fh == NULL)
+           || (fsp->fh->fd == -1))
+       {
+               smb_panic("bad fsp");
+       }
+
+       ok = ad_pack(ad);
+       if (!ok) {
+               return -1;
+       }
+
+       switch (ad->ad_type) {
+       case ADOUBLE_META:
+               rc = SMB_VFS_NEXT_FSETXATTR(ad->ad_handle,
+                                           fsp,
+                                           AFPINFO_EA_NETATALK,
+                                           ad->ad_data,
+                                           AD_DATASZ_XATTR, 0);
+               break;
+
+       case ADOUBLE_RSRC:
+               len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
+                                         fsp,
+                                         ad->ad_data,
+                                         talloc_get_size(ad->ad_data),
+                                         0);
+               if (len != (ssize_t)talloc_get_size(ad->ad_data)) {
+                       DBG_ERR("short write on %s: %zd", fsp_str_dbg(fsp), len);
+                       return -1;
+               }
+               rc = 0;
+               break;
+
+       default:
+               return -1;
+       }
+
+       DBG_DEBUG("Path [%s] rc [%d]\n", fsp_str_dbg(fsp), rc);
+
        return rc;
 }
 
@@ -1391,9 +1597,12 @@ static int init_fruit_config(vfs_handle_struct *handle)
        }
        config->encoding = (enum fruit_encoding)enumval;
 
-       config->veto_appledouble = lp_parm_bool(
-               SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
-               "veto_appledouble", true);
+       if (config->rsrc == FRUIT_RSRC_ADFILE) {
+               config->veto_appledouble = lp_parm_bool(SNUM(handle->conn),
+                                                       FRUIT_PARAM_TYPE_NAME,
+                                                       "veto_appledouble",
+                                                       true);
+       }
 
        config->use_aapl = lp_parm_bool(
                -1, FRUIT_PARAM_TYPE_NAME, "aapl", true);
@@ -1407,6 +1616,9 @@ static int init_fruit_config(vfs_handle_struct *handle)
        config->posix_rename = lp_parm_bool(
                SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME, "posix_rename", true);
 
+       config->aapl_zero_file_id =
+           lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME, "zero_file_id", true);
+
        config->readdir_attr_rsize = lp_parm_bool(
                SNUM(handle->conn), "readdir_attr", "aapl_rsize", true);
 
@@ -1425,21 +1637,42 @@ static int init_fruit_config(vfs_handle_struct *handle)
 
 /**
  * Prepend "._" to a basename
+ * Return a new struct smb_filename with stream_name == NULL.
  **/
-static int adouble_path(TALLOC_CTX *ctx, const char *path_in, char **path_out)
+static int adouble_path(TALLOC_CTX *ctx,
+                       const struct smb_filename *smb_fname_in,
+                       struct smb_filename **pp_smb_fname_out)
 {
        char *parent;
        const char *base;
+       struct smb_filename *smb_fname = cp_smb_filename(ctx,
+                                               smb_fname_in);
 
-       if (!parent_dirname(ctx, path_in, &parent, &base)) {
+       if (smb_fname == NULL) {
                return -1;
        }
 
-       *path_out = talloc_asprintf(ctx, "%s/._%s", parent, base);
-       if (*path_out == NULL) {
+       /* We need streamname to be NULL */
+       TALLOC_FREE(smb_fname->stream_name);
+
+       /* And we're replacing base_name. */
+       TALLOC_FREE(smb_fname->base_name);
+
+       if (!parent_dirname(smb_fname, smb_fname_in->base_name,
+                               &parent, &base)) {
+               TALLOC_FREE(smb_fname);
+               return -1;
+       }
+
+       smb_fname->base_name = talloc_asprintf(smb_fname,
+                                       "%s/._%s", parent, base);
+       if (smb_fname->base_name == NULL) {
+               TALLOC_FREE(smb_fname);
                return -1;
        }
 
+       *pp_smb_fname_out = smb_fname;
+
        return 0;
 }
 
@@ -1537,19 +1770,6 @@ static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
        return result;
 }
 
-/**
- * Ensure ad_fsp is still valid
- **/
-static bool fruit_fsp_recheck(struct adouble *ad, files_struct *fsp)
-{
-       if (ad->ad_fsp == fsp) {
-               return true;
-       }
-       ad->ad_fsp = fsp;
-
-       return true;
-}
-
 static bool add_fruit_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
                             struct stream_struct **streams,
                             const char *name, off_t size,
@@ -1691,7 +1911,7 @@ static void update_btime(vfs_handle_struct *handle,
                return;
        }
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
        if (ad == NULL) {
                return;
        }
@@ -1805,6 +2025,8 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
        struct byte_range_lock *br_lck = NULL;
        bool open_for_reading, open_for_writing, deny_read, deny_write;
        off_t off;
+       bool have_read = false;
+       int flags;
 
        /* FIXME: hardcoded data fork, add resource fork */
        enum apple_fork fork_type = APPLE_FORK_DATA;
@@ -1816,6 +2038,26 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                  deny_mode & DENY_READ ? "DENY_READ" : "-",
                  deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
 
+       if (fsp->fh->fd == -1) {
+               return NT_STATUS_OK;
+       }
+
+       flags = fcntl(fsp->fh->fd, F_GETFL);
+       if (flags == -1) {
+               DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
+                       fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
+               return map_nt_error_from_unix(errno);
+       }
+
+       if (flags & (O_RDONLY|O_RDWR)) {
+               /*
+                * Applying fcntl read locks requires an fd opened for
+                * reading. This means we won't be applying locks for
+                * files openend write-only, but what can we do...
+                */
+               have_read = true;
+       }
+
        /*
         * Check read access and deny read mode
         */
@@ -1837,7 +2079,7 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                }
 
                /* Set locks */
-               if (access_mask & FILE_READ_DATA) {
+               if ((access_mask & FILE_READ_DATA) && have_read) {
                        off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
                        br_lck = do_lock(
                                handle->conn->sconn->msg_ctx, fsp,
@@ -1851,7 +2093,7 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                        TALLOC_FREE(br_lck);
                }
 
-               if (deny_mode & DENY_READ) {
+               if ((deny_mode & DENY_READ) && have_read) {
                        off = denymode_to_netatalk_brl(fork_type, DENY_READ);
                        br_lck = do_lock(
                                handle->conn->sconn->msg_ctx, fsp,
@@ -1887,7 +2129,7 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                }
 
                /* Set locks */
-               if (access_mask & FILE_WRITE_DATA) {
+               if ((access_mask & FILE_WRITE_DATA) && have_read) {
                        off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
                        br_lck = do_lock(
                                handle->conn->sconn->msg_ctx, fsp,
@@ -1901,7 +2143,7 @@ static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                        TALLOC_FREE(br_lck);
 
                }
-               if (deny_mode & DENY_WRITE) {
+               if ((deny_mode & DENY_WRITE) && have_read) {
                        off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
                        br_lck = do_lock(
                                handle->conn->sconn->msg_ctx, fsp,
@@ -2004,9 +2246,23 @@ static NTSTATUS check_aapl(vfs_handle_struct *handle,
        }
 
        if (req_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) {
-               SBVAL(p, 0,
-                     lp_case_sensitive(SNUM(handle->conn->tcon->compat)) ?
-                     SMB2_CRTCTX_AAPL_CASE_SENSITIVE : 0);
+               int val = lp_case_sensitive(SNUM(handle->conn->tcon->compat));
+               uint64_t caps = 0;
+
+               switch (val) {
+               case Auto:
+                       break;
+
+               case True:
+                       caps |= SMB2_CRTCTX_AAPL_CASE_SENSITIVE;
+                       break;
+
+               default:
+                       break;
+               }
+
+               SBVAL(p, 0, caps);
+
                ok = data_blob_append(req, &blob, p, 8);
                if (!ok) {
                        return NT_STATUS_UNSUCCESSFUL;
@@ -2042,7 +2298,10 @@ static NTSTATUS check_aapl(vfs_handle_struct *handle,
                                      SMB2_CREATE_TAG_AAPL,
                                      blob);
        if (NT_STATUS_IS_OK(status)) {
-               config->nego_aapl = true;
+               global_fruit_config.nego_aapl = true;
+               if (config->aapl_zero_file_id) {
+                       aapl_force_zero_file_id(handle->conn->sconn);
+               }
        }
 
        return status;
@@ -2130,7 +2389,7 @@ static bool readdir_attr_meta_finderi_netatalk(
        struct adouble *ad = NULL;
        char *p = NULL;
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
        if (ad == NULL) {
                return false;
        }
@@ -2215,7 +2474,7 @@ static uint64_t readdir_attr_rfork_size_adouble(
        struct adouble *ad = NULL;
        uint64_t rfork_size;
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
+       ad = ad_get(talloc_tos(), handle, smb_fname,
                    ADOUBLE_RSRC);
        if (ad == NULL) {
                return 0;
@@ -2507,6 +2766,8 @@ static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
        int hostfd = -1;
        struct adouble *ad = NULL;
 
+       DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
+
        /* Create an smb_filename with stream_name == NULL. */
        smb_fname_base = synthetic_smb_fname(talloc_tos(),
                                        smb_fname->base_name,
@@ -2529,8 +2790,8 @@ static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
        baseflags &= ~O_EXCL;
        baseflags &= ~O_CREAT;
 
-       hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
-                             baseflags, mode);
+       hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
+                                  baseflags, mode);
 
        /*
         * It is legit to open a stream on a directory, but the base
@@ -2539,8 +2800,8 @@ static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
        if ((hostfd == -1) && (errno == EISDIR)) {
                baseflags &= ~O_ACCMODE;
                baseflags |= O_RDONLY;
-               hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
-                                     baseflags, mode);
+               hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
+                                          baseflags, mode);
        }
 
        TALLOC_FREE(smb_fname_base);
@@ -2555,29 +2816,22 @@ static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
                 * The attribute does not exist or needs to be truncated,
                 * create an AppleDouble EA
                 */
-               ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                            handle, ADOUBLE_META, fsp);
+               ad = ad_init(fsp, handle, ADOUBLE_META);
                if (ad == NULL) {
                        rc = -1;
                        goto exit;
                }
 
-               rc = ad_write(ad, smb_fname->base_name);
+               fsp->fh->fd = hostfd;
+
+               rc = ad_fset(ad, fsp);
+               fsp->fh->fd = -1;
                if (rc != 0) {
                        rc = -1;
                        goto exit;
                }
-       } else {
-               ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                             handle, ADOUBLE_META, fsp);
-               if (ad == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (ad_read(ad, smb_fname->base_name) == -1) {
-                       rc = -1;
-                       goto exit;
-               }
+
+               TALLOC_FREE(ad);
        }
 
 exit:
@@ -2591,7 +2845,7 @@ exit:
                         * full fsp yet
                         */
                        fsp->fh->fd = hostfd;
-                       SMB_VFS_CLOSE(fsp);
+                       SMB_VFS_NEXT_CLOSE(handle, fsp);
                }
                hostfd = -1;
                errno = saved_errno;
@@ -2603,8 +2857,9 @@ static int fruit_open_meta(vfs_handle_struct *handle,
                           struct smb_filename *smb_fname,
                           files_struct *fsp, int flags, mode_t mode)
 {
-       int rc;
+       int fd;
        struct fruit_config_data *config = NULL;
+       struct fio *fio = NULL;
 
        DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
 
@@ -2613,12 +2868,12 @@ static int fruit_open_meta(vfs_handle_struct *handle,
 
        switch (config->meta) {
        case FRUIT_META_STREAM:
-               rc = fruit_open_meta_stream(handle, smb_fname,
+               fd = fruit_open_meta_stream(handle, smb_fname,
                                            fsp, flags, mode);
                break;
 
        case FRUIT_META_NETATALK:
-               rc = fruit_open_meta_netatalk(handle, smb_fname,
+               fd = fruit_open_meta_netatalk(handle, smb_fname,
                                              fsp, flags, mode);
                break;
 
@@ -2627,8 +2882,17 @@ static int fruit_open_meta(vfs_handle_struct *handle,
                return -1;
        }
 
-       DBG_DEBUG("path [%s] rc [%d]\n", smb_fname_str_dbg(smb_fname), rc);
-       return rc;
+       DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
+
+       if (fd == -1) {
+               return -1;
+       }
+
+       fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
+       fio->type = ADOUBLE_META;
+       fio->config = config;
+
+       return fd;
 }
 
 static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
@@ -2640,40 +2904,25 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
        int rc = 0;
        struct adouble *ad = NULL;
        struct smb_filename *smb_fname_base = NULL;
-       char *adpath = NULL;
+       struct fruit_config_data *config = NULL;
        int hostfd = -1;
 
-       if (!(flags & O_CREAT) && !VALID_STAT(smb_fname->st)) {
-               rc = SMB_VFS_NEXT_STAT(handle, smb_fname);
-               if (rc != 0) {
-                       rc = -1;
-                       goto exit;
-               }
-       }
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
+                               struct fruit_config_data, return -1);
 
-       if (VALID_STAT(smb_fname->st) && S_ISDIR(smb_fname->st.st_ex_mode)) {
+       if ((!(flags & O_CREAT)) &&
+           S_ISDIR(fsp->base_fsp->fsp_name->st.st_ex_mode))
+       {
                /* sorry, but directories don't habe a resource fork */
                rc = -1;
                goto exit;
        }
 
-       rc = adouble_path(talloc_tos(), smb_fname->base_name, &adpath);
+       rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_base);
        if (rc != 0) {
                goto exit;
        }
 
-       /* Create an smb_filename with stream_name == NULL. */
-       smb_fname_base = synthetic_smb_fname(talloc_tos(),
-                                       adpath,
-                                       NULL,
-                                       NULL,
-                                       smb_fname->flags);
-       if (smb_fname_base == NULL) {
-               errno = ENOMEM;
-               rc = -1;
-               goto exit;
-       }
-
        /* Sanitize flags */
        if (flags & O_WRONLY) {
                /* We always need read access for the metadata header too */
@@ -2681,44 +2930,33 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
                flags |= O_RDWR;
        }
 
-       hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
-                             flags, mode);
+       hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
+                                  flags, mode);
        if (hostfd == -1) {
                rc = -1;
                goto exit;
        }
 
-       /* REVIEW: we need this in ad_write() */
-       fsp->fh->fd = hostfd;
-
        if (flags & (O_CREAT | O_TRUNC)) {
-               ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                            handle, ADOUBLE_RSRC, fsp);
+               ad = ad_init(fsp, handle, ADOUBLE_RSRC);
                if (ad == NULL) {
                        rc = -1;
                        goto exit;
                }
-               rc = ad_write(ad, smb_fname->base_name);
+
+               fsp->fh->fd = hostfd;
+
+               rc = ad_fset(ad, fsp);
+               fsp->fh->fd = -1;
                if (rc != 0) {
                        rc = -1;
                        goto exit;
                }
-       } else {
-               ad = ad_alloc(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                             handle, ADOUBLE_RSRC, fsp);
-               if (ad == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (ad_read(ad, smb_fname->base_name) == -1) {
-                       rc = -1;
-                       goto exit;
-               }
+               TALLOC_FREE(ad);
        }
 
 exit:
 
-       TALLOC_FREE(adpath);
        TALLOC_FREE(smb_fname_base);
 
        DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc, hostfd));
@@ -2747,16 +2985,11 @@ static int fruit_open_rsrc_xattr(vfs_handle_struct *handle,
 {
 #ifdef HAVE_ATTROPEN
        int fd = -1;
-       struct adouble *ad = NULL;
-
-       ad = ad_init(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
-                    handle, ADOUBLE_RSRC, fsp);
-       if (ad == NULL) {
-               return -1;
-       }
 
        fd = attropen(smb_fname->base_name,
-                         AFPRESOURCE_EA_NETATALK, flags, mode);
+                     AFPRESOURCE_EA_NETATALK,
+                     flags,
+                     mode);
        if (fd == -1) {
                return -1;
        }
@@ -2775,12 +3008,27 @@ static int fruit_open_rsrc(vfs_handle_struct *handle,
 {
        int fd;
        struct fruit_config_data *config = NULL;
+       struct fio *fio = NULL;
 
        DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
 
        SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return -1);
 
+       if (((flags & O_ACCMODE) == O_RDONLY)
+           && (flags & O_CREAT)
+           && !VALID_STAT(fsp->fsp_name->st))
+       {
+               /*
+                * This means the stream doesn't exist. macOS SMB server fails
+                * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
+                * 12565 and the test for this combination in
+                * test_rfork_create().
+                */
+               errno = ENOENT;
+               return -1;
+       }
+
        switch (config->rsrc) {
        case FRUIT_RSRC_STREAM:
                fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
@@ -2802,6 +3050,15 @@ static int fruit_open_rsrc(vfs_handle_struct *handle,
        }
 
        DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
+
+       if (fd == -1) {
+               return -1;
+       }
+
+       fio = (struct fio *)VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
+       fio->type = ADOUBLE_RSRC;
+       fio->config = config;
+
        return fd;
 }
 
@@ -2809,20 +3066,25 @@ static int fruit_open(vfs_handle_struct *handle,
                       struct smb_filename *smb_fname,
                       files_struct *fsp, int flags, mode_t mode)
 {
-       DEBUG(10, ("fruit_open called for %s\n",
-                  smb_fname_str_dbg(smb_fname)));
+       int fd;
+
+       DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
 
        if (!is_ntfs_stream_smb_fname(smb_fname)) {
                return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
        }
 
        if (is_afpinfo_stream(smb_fname)) {
-               return fruit_open_meta(handle, smb_fname, fsp, flags, mode);
+               fd = fruit_open_meta(handle, smb_fname, fsp, flags, mode);
        } else if (is_afpresource_stream(smb_fname)) {
-               return fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
+               fd = fruit_open_rsrc(handle, smb_fname, fsp, flags, mode);
+       } else {
+               fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
        }
 
-       return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
+       DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
+
+       return fd;
 }
 
 static int fruit_rename(struct vfs_handle_struct *handle,
@@ -2830,8 +3092,6 @@ static int fruit_rename(struct vfs_handle_struct *handle,
                        const struct smb_filename *smb_fname_dst)
 {
        int rc = -1;
-       char *src_adouble_path = NULL;
-       char *dst_adouble_path = NULL;
        struct fruit_config_data *config = NULL;
        struct smb_filename *src_adp_smb_fname = NULL;
        struct smb_filename *dst_adp_smb_fname = NULL;
@@ -2856,35 +3116,15 @@ static int fruit_rename(struct vfs_handle_struct *handle,
                return 0;
        }
 
-       rc = adouble_path(talloc_tos(), smb_fname_src->base_name,
-                         &src_adouble_path);
+       rc = adouble_path(talloc_tos(), smb_fname_src, &src_adp_smb_fname);
        if (rc != 0) {
                goto done;
        }
-       src_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
-                                               src_adouble_path,
-                                               NULL, NULL,
-                                               smb_fname_src->flags);
-       TALLOC_FREE(src_adouble_path);
-       if (src_adp_smb_fname == NULL) {
-               rc = -1;
-               goto done;
-       }
 
-       rc = adouble_path(talloc_tos(), smb_fname_dst->base_name,
-                         &dst_adouble_path);
+       rc = adouble_path(talloc_tos(), smb_fname_dst, &dst_adp_smb_fname);
        if (rc != 0) {
                goto done;
        }
-       dst_adp_smb_fname = synthetic_smb_fname(talloc_tos(),
-                                               dst_adouble_path,
-                                               NULL, NULL,
-                                               smb_fname_dst->flags);
-       TALLOC_FREE(dst_adouble_path);
-       if (dst_adp_smb_fname == NULL) {
-               rc = -1;
-               goto done;
-       }
 
        DBG_DEBUG("%s -> %s\n",
                  smb_fname_str_dbg(src_adp_smb_fname),
@@ -2911,7 +3151,7 @@ static int fruit_unlink_meta_netatalk(vfs_handle_struct *handle,
                                      const struct smb_filename *smb_fname)
 {
        return SMB_VFS_REMOVEXATTR(handle->conn,
-                                  smb_fname->base_name,
+                                  smb_fname,
                                   AFPINFO_EA_NETATALK);
 }
 
@@ -2992,12 +3232,11 @@ static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
                                     bool force_unlink)
 {
        int rc;
-       char *adp = NULL;
        struct adouble *ad = NULL;
        struct smb_filename *adp_smb_fname = NULL;
 
        if (!force_unlink) {
-               ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
+               ad = ad_get(talloc_tos(), handle, smb_fname,
                            ADOUBLE_RSRC);
                if (ad == NULL) {
                        errno = ENOENT;
@@ -3020,19 +3259,11 @@ static int fruit_unlink_rsrc_adouble(vfs_handle_struct *handle,
                TALLOC_FREE(ad);
        }
 
-       rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
+       rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
        if (rc != 0) {
                return -1;
        }
 
-       adp_smb_fname = synthetic_smb_fname(talloc_tos(), adp,
-                                           NULL, NULL,
-                                           smb_fname->flags);
-       TALLOC_FREE(adp);
-       if (adp_smb_fname == NULL) {
-               return -1;
-       }
-
        rc = SMB_VFS_NEXT_UNLINK(handle, adp_smb_fname);
        TALLOC_FREE(adp_smb_fname);
        if ((rc != 0) && (errno == ENOENT) && force_unlink) {
@@ -3136,9 +3367,7 @@ static int fruit_chmod(vfs_handle_struct *handle,
                       mode_t mode)
 {
        int rc = -1;
-       char *adp = NULL;
        struct fruit_config_data *config = NULL;
-       const char *path = smb_fname->base_name;
        struct smb_filename *smb_fname_adp = NULL;
 
        rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
@@ -3161,23 +3390,12 @@ static int fruit_chmod(vfs_handle_struct *handle,
                return 0;
        }
 
-       rc = adouble_path(talloc_tos(), path, &adp);
+       rc = adouble_path(talloc_tos(), smb_fname, &smb_fname_adp);
        if (rc != 0) {
                return -1;
        }
 
-       DEBUG(10, ("fruit_chmod: %s\n", adp));
-
-       smb_fname_adp = synthetic_smb_fname(talloc_tos(),
-                                       adp,
-                                       NULL,
-                                       NULL,
-                                       smb_fname->flags);
-       if (smb_fname_adp == NULL) {
-               TALLOC_FREE(adp);
-               errno = ENOMEM;
-               return -1;
-       }
+       DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp->base_name));
 
        rc = SMB_VFS_NEXT_CHMOD(handle, smb_fname_adp, mode);
        if (errno == ENOENT) {
@@ -3185,7 +3403,6 @@ static int fruit_chmod(vfs_handle_struct *handle,
        }
 
        TALLOC_FREE(smb_fname_adp);
-       TALLOC_FREE(adp);
        return rc;
 }
 
@@ -3195,7 +3412,6 @@ static int fruit_chown(vfs_handle_struct *handle,
                       gid_t gid)
 {
        int rc = -1;
-       char *adp = NULL;
        struct fruit_config_data *config = NULL;
        struct smb_filename *adp_smb_fname = NULL;
 
@@ -3219,23 +3435,12 @@ static int fruit_chown(vfs_handle_struct *handle,
                return 0;
        }
 
-       rc = adouble_path(talloc_tos(), smb_fname->base_name, &adp);
+       rc = adouble_path(talloc_tos(), smb_fname, &adp_smb_fname);
        if (rc != 0) {
                goto done;
        }
 
-       DEBUG(10, ("fruit_chown: %s\n", adp));
-
-       adp_smb_fname = synthetic_smb_fname(talloc_tos(),
-                                       adp,
-                                       NULL,
-                                       NULL,
-                                       smb_fname->flags);
-       if (adp_smb_fname == NULL) {
-               errno = ENOMEM;
-               rc = -1;
-               goto done;
-       }
+       DEBUG(10, ("fruit_chown: %s\n", adp_smb_fname->base_name));
 
        rc = SMB_VFS_NEXT_CHOWN(handle, adp_smb_fname, uid, gid);
        if (errno == ENOENT) {
@@ -3243,7 +3448,6 @@ static int fruit_chown(vfs_handle_struct *handle,
        }
 
  done:
-       TALLOC_FREE(adp);
        TALLOC_FREE(adp_smb_fname);
        return rc;
 }
@@ -3293,26 +3497,27 @@ static int fruit_rmdir(struct vfs_handle_struct *handle,
                        return -1;
                }
 
+               ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
+                                                   NULL, NULL,
+                                                   smb_fname->flags);
+               TALLOC_FREE(p);
+               if (ad_smb_fname == NULL) {
+                       DBG_ERR("synthetic_smb_fname failed\n");
+                       return -1;
+               }
+
                /*
                 * Check whether it's a valid AppleDouble file, if
                 * yes, delete it, ignore it otherwise.
                 */
-               ad = ad_get(talloc_tos(), handle, p, ADOUBLE_RSRC);
+               ad = ad_get(talloc_tos(), handle, ad_smb_fname, ADOUBLE_RSRC);
                if (ad == NULL) {
+                       TALLOC_FREE(ad_smb_fname);
                        TALLOC_FREE(p);
                        continue;
                }
                TALLOC_FREE(ad);
 
-               ad_smb_fname = synthetic_smb_fname(talloc_tos(), p,
-                                                   NULL, NULL,
-                                                   smb_fname->flags);
-               TALLOC_FREE(p);
-               if (ad_smb_fname == NULL) {
-                       DBG_ERR("synthetic_smb_fname failed\n");
-                       return -1;
-               }
-
                ret = SMB_VFS_NEXT_UNLINK(handle, ad_smb_fname);
                TALLOC_FREE(ad_smb_fname);
                if (ret != 0) {
@@ -3328,288 +3533,601 @@ exit_rmdir:
        return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
 }
 
-static ssize_t fruit_pread(vfs_handle_struct *handle,
-                          files_struct *fsp, void *data,
-                          size_t n, off_t offset)
+static ssize_t fruit_pread_meta_stream(vfs_handle_struct *handle,
+                                      files_struct *fsp, void *data,
+                                      size_t n, off_t offset)
 {
-       int rc = 0;
-        struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
-               handle, fsp);
-       struct fruit_config_data *config = NULL;
-       AfpInfo *ai = NULL;
-       ssize_t len = -1;
-       char *name = NULL;
-       char *tmp_base_name = NULL;
-       NTSTATUS status;
-       size_t to_return = n;
+       ssize_t nread;
+       int ret;
 
-       DEBUG(10, ("fruit_pread: offset=%d, size=%d\n", (int)offset, (int)n));
+       nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
 
-       if (!fsp->base_fsp) {
-               return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
+       if (nread == n) {
+               return nread;
        }
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return -1);
+       DBG_ERR("Removing [%s] after short read [%zd]\n",
+               fsp_str_dbg(fsp), nread);
 
-       /* fsp_name is not converted with vfs_catia */
-       tmp_base_name = fsp->base_fsp->fsp_name->base_name;
-       status = SMB_VFS_TRANSLATE_NAME(handle->conn,
-                                       fsp->base_fsp->fsp_name->base_name,
-                                       vfs_translate_to_unix,
-                                       talloc_tos(), &name);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
-               name = talloc_strdup(talloc_tos(), tmp_base_name);
-               if (name == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
-       } else if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
-               rc = -1;
-               goto exit;
+       ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
+       if (ret != 0) {
+               DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp));
+               return -1;
        }
-       fsp->base_fsp->fsp_name->base_name = name;
 
-       if (is_afpinfo_stream(fsp->fsp_name)) {
-               /*
-                * OS X has a off-by-1 error in the offset calculation, so we're
-                * bug compatible here. It won't hurt, as any relevant real
-                * world read requests from the AFP_AfpInfo stream will be
-                * offset=0 n=60. offset is ignored anyway, see below.
-                */
-               if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
-                       len = 0;
-                       rc = 0;
-                       goto exit;
-               }
+       errno = EINVAL;
+       return -1;
+}
 
-               to_return = MIN(n, AFP_INFO_SIZE);
+static ssize_t fruit_pread_meta_adouble(vfs_handle_struct *handle,
+                                       files_struct *fsp, void *data,
+                                       size_t n, off_t offset)
+{
+       AfpInfo *ai = NULL;
+       struct adouble *ad = NULL;
+       char afpinfo_buf[AFP_INFO_SIZE];
+       char *p = NULL;
+       ssize_t nread;
 
-               /* Yes, macOS always reads from offset 0 */
-               offset = 0;
+       ai = afpinfo_new(talloc_tos());
+       if (ai == NULL) {
+               return -1;
        }
 
+       ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
        if (ad == NULL) {
-               len = SMB_VFS_NEXT_PREAD(handle, fsp, data, to_return, offset);
-               if (len == -1) {
-                       rc = -1;
-                       goto exit;
-               }
-               goto exit;
+               nread = -1;
+               goto fail;
        }
 
-       if (!fruit_fsp_recheck(ad, fsp)) {
-               rc = -1;
-               goto exit;
+       p = ad_get_entry(ad, ADEID_FINDERI);
+       if (p == NULL) {
+               DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
+               nread = -1;
+               goto fail;
        }
 
-       if (ad->ad_type == ADOUBLE_META) {
-               char afpinfo_buf[AFP_INFO_SIZE];
-               char *p = NULL;
+       memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
 
-               ai = afpinfo_new(talloc_tos());
-               if (ai == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
+       nread = afpinfo_pack(ai, afpinfo_buf);
+       if (nread != AFP_INFO_SIZE) {
+               nread = -1;
+               goto fail;
+       }
 
-               len = ad_read(ad, fsp->base_fsp->fsp_name->base_name);
-               if (len == -1) {
-                       rc = -1;
-                       goto exit;
-               }
+       memcpy(data, afpinfo_buf, n);
+       nread = n;
 
-               p = ad_get_entry(ad, ADEID_FINDERI);
-               if (p == NULL) {
-                       DBG_ERR("No ADEID_FINDERI for [%s]\n",
-                               fsp->fsp_name->base_name);
-                       rc = -1;
-                       goto exit;
-               }
+fail:
+       TALLOC_FREE(ai);
+       return nread;
+}
 
-               memcpy(&ai->afpi_FinderInfo[0], p, ADEDLEN_FINDERI);
+static ssize_t fruit_pread_meta(vfs_handle_struct *handle,
+                               files_struct *fsp, void *data,
+                               size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nread;
+       ssize_t to_return;
 
-               len = afpinfo_pack(ai, afpinfo_buf);
-               if (len != AFP_INFO_SIZE) {
-                       rc = -1;
-                       goto exit;
-               }
+       /*
+        * OS X has a off-by-1 error in the offset calculation, so we're
+        * bug compatible here. It won't hurt, as any relevant real
+        * world read requests from the AFP_AfpInfo stream will be
+        * offset=0 n=60. offset is ignored anyway, see below.
+        */
+       if ((offset < 0) || (offset >= AFP_INFO_SIZE + 1)) {
+               return 0;
+       }
 
-               /*
-                * OS X ignores offset when reading from AFP_AfpInfo stream!
-                */
-               memcpy(data, afpinfo_buf, to_return);
-               len = to_return;
+       /* Yes, macOS always reads from offset 0 */
+       offset = 0;
+       to_return = MIN(n, AFP_INFO_SIZE);
+
+       switch (fio->config->meta) {
+       case FRUIT_META_STREAM:
+               nread = fruit_pread_meta_stream(handle, fsp, data,
+                                               to_return, offset);
+               break;
+
+       case FRUIT_META_NETATALK:
+               nread = fruit_pread_meta_adouble(handle, fsp, data,
+                                                to_return, offset);
+               break;
+
+       default:
+               DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
+               return -1;
+       }
+
+       return nread;
+}
+
+static ssize_t fruit_pread_rsrc_stream(vfs_handle_struct *handle,
+                                      files_struct *fsp, void *data,
+                                      size_t n, off_t offset)
+{
+       return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
+}
+
+static ssize_t fruit_pread_rsrc_xattr(vfs_handle_struct *handle,
+                                     files_struct *fsp, void *data,
+                                     size_t n, off_t offset)
+{
+       return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
+}
+
+static ssize_t fruit_pread_rsrc_adouble(vfs_handle_struct *handle,
+                                       files_struct *fsp, void *data,
+                                       size_t n, off_t offset)
+{
+       struct adouble *ad = NULL;
+       ssize_t nread;
+
+       ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
+       if (ad == NULL) {
+               return -1;
+       }
+
+       nread = SMB_VFS_NEXT_PREAD(handle, fsp, data, n,
+                                  offset + ad_getentryoff(ad, ADEID_RFORK));
+
+       TALLOC_FREE(ad);
+       return nread;
+}
+
+static ssize_t fruit_pread_rsrc(vfs_handle_struct *handle,
+                               files_struct *fsp, void *data,
+                               size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nread;
+
+       switch (fio->config->rsrc) {
+       case FRUIT_RSRC_STREAM:
+               nread = fruit_pread_rsrc_stream(handle, fsp, data, n, offset);
+               break;
+
+       case FRUIT_RSRC_ADFILE:
+               nread = fruit_pread_rsrc_adouble(handle, fsp, data, n, offset);
+               break;
+
+       case FRUIT_RSRC_XATTR:
+               nread = fruit_pread_rsrc_xattr(handle, fsp, data, n, offset);
+               break;
+
+       default:
+               DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
+               return -1;
+       }
+
+       return nread;
+}
+
+static ssize_t fruit_pread(vfs_handle_struct *handle,
+                          files_struct *fsp, void *data,
+                          size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nread;
+
+       DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
+                 fsp_str_dbg(fsp), (intmax_t)offset, n);
+
+       if (fio == NULL) {
+               return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
+       }
+
+       if (fio->type == ADOUBLE_META) {
+               nread = fruit_pread_meta(handle, fsp, data, n, offset);
        } else {
-               len = SMB_VFS_NEXT_PREAD(
-                       handle, fsp, data, n,
-                       offset + ad_getentryoff(ad, ADEID_RFORK));
-               if (len == -1) {
-                       rc = -1;
-                       goto exit;
+               nread = fruit_pread_rsrc(handle, fsp, data, n, offset);
+       }
+
+       DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp), nread);
+       return nread;
+}
+
+static bool fruit_must_handle_aio_stream(struct fio *fio)
+{
+       if (fio == NULL) {
+               return false;
+       };
+
+       if ((fio->type == ADOUBLE_META) &&
+           (fio->config->meta == FRUIT_META_NETATALK))
+       {
+               return true;
+       }
+
+       if ((fio->type == ADOUBLE_RSRC) &&
+           (fio->config->rsrc == FRUIT_RSRC_ADFILE))
+       {
+               return true;
+       }
+
+       return false;
+}
+
+struct fruit_pread_state {
+       ssize_t nread;
+       struct vfs_aio_state vfs_aio_state;
+};
+
+static void fruit_pread_done(struct tevent_req *subreq);
+
+static struct tevent_req *fruit_pread_send(
+       struct vfs_handle_struct *handle,
+       TALLOC_CTX *mem_ctx,
+       struct tevent_context *ev,
+       struct files_struct *fsp,
+       void *data,
+       size_t n, off_t offset)
+{
+       struct tevent_req *req = NULL;
+       struct tevent_req *subreq = NULL;
+       struct fruit_pread_state *state = NULL;
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct fruit_pread_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (fruit_must_handle_aio_stream(fio)) {
+               state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
+               if (state->nread != n) {
+                       if (state->nread != -1) {
+                               errno = EIO;
+                       }
+                       tevent_req_error(req, errno);
+                       return tevent_req_post(req, ev);
                }
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
        }
-exit:
-       fsp->base_fsp->fsp_name->base_name = tmp_base_name;
-       TALLOC_FREE(name);
-       TALLOC_FREE(ai);
-       if (rc != 0) {
-               len = -1;
+
+       subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
+                                        data, n, offset);
+       if (tevent_req_nomem(req, subreq)) {
+               return tevent_req_post(req, ev);
        }
-       DEBUG(10, ("fruit_pread: rc=%d, len=%zd\n", rc, len));
-       return len;
+       tevent_req_set_callback(subreq, fruit_pread_done, req);
+       return req;
 }
 
-static ssize_t fruit_pwrite(vfs_handle_struct *handle,
-                           files_struct *fsp, const void *data,
-                           size_t n, off_t offset)
+static void fruit_pread_done(struct tevent_req *subreq)
 {
-       int rc = 0;
-       struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
-               handle, fsp);
-       struct fruit_config_data *config = NULL;
-       AfpInfo *ai = NULL;
-       ssize_t len;
-       char *name = NULL;
-       char *tmp_base_name = NULL;
-       NTSTATUS status;
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fruit_pread_state *state = tevent_req_data(
+               req, struct fruit_pread_state);
 
-       DEBUG(10, ("fruit_pwrite: offset=%d, size=%d\n", (int)offset, (int)n));
+       state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
+       TALLOC_FREE(subreq);
 
-       if (!fsp->base_fsp) {
-               return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+       if (tevent_req_error(req, state->vfs_aio_state.error)) {
+               return;
        }
+       tevent_req_done(req);
+}
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return -1);
+static ssize_t fruit_pread_recv(struct tevent_req *req,
+                                       struct vfs_aio_state *vfs_aio_state)
+{
+       struct fruit_pread_state *state = tevent_req_data(
+               req, struct fruit_pread_state);
 
-       tmp_base_name = fsp->base_fsp->fsp_name->base_name;
-       status = SMB_VFS_TRANSLATE_NAME(handle->conn,
-                                       fsp->base_fsp->fsp_name->base_name,
-                                       vfs_translate_to_unix,
-                                       talloc_tos(), &name);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
-               name = talloc_strdup(talloc_tos(), tmp_base_name);
-               if (name == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
-       } else if (!NT_STATUS_IS_OK(status)) {
-               errno = map_errno_from_nt_status(status);
-               rc = -1;
-               goto exit;
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               return -1;
        }
-       fsp->base_fsp->fsp_name->base_name = name;
 
-       if (is_afpinfo_stream(fsp->fsp_name)) {
-               /*
-                * Writing an all 0 blob to the metadata stream
-                * results in the stream being removed on a macOS
-                * server. This ensures we behave the same and it
-                * verified by the "delete AFP_AfpInfo by writing all
-                * 0" test.
-                */
-               if (n != AFP_INFO_SIZE || offset != 0) {
-                       DEBUG(1, ("unexpected offset=%jd or size=%jd\n",
-                                 (intmax_t)offset, (intmax_t)n));
-                       rc = -1;
-                       goto exit;
-               }
-               ai = afpinfo_unpack(talloc_tos(), data);
-               if (ai == NULL) {
-                       rc = -1;
-                       goto exit;
+       *vfs_aio_state = state->vfs_aio_state;
+       return state->nread;
+}
+
+static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
+                                       files_struct *fsp, const void *data,
+                                       size_t n, off_t offset)
+{
+       AfpInfo *ai = NULL;
+       int ret;
+
+       ai = afpinfo_unpack(talloc_tos(), data);
+       if (ai == NULL) {
+               return -1;
+       }
+
+       if (ai_empty_finderinfo(ai)) {
+               ret = SMB_VFS_NEXT_UNLINK(handle, fsp->fsp_name);
+               if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
+                       DBG_ERR("Can't delete metadata for %s: %s\n",
+                               fsp_str_dbg(fsp), strerror(errno));
+                       TALLOC_FREE(ai);
+                       return -1;
                }
 
-               if (ai_empty_finderinfo(ai)) {
-                       switch (config->meta) {
-                       case FRUIT_META_STREAM:
-                               rc = SMB_VFS_UNLINK(handle->conn, fsp->fsp_name);
-                               break;
-
-                       case FRUIT_META_NETATALK:
-                               rc = SMB_VFS_REMOVEXATTR(
-                                       handle->conn,
-                                       fsp->fsp_name->base_name,
-                                       AFPINFO_EA_NETATALK);
-                               break;
-
-                       default:
-                               DBG_ERR("Unexpected meta config [%d]\n",
-                                       config->meta);
-                               rc = -1;
-                               goto exit;
-                       }
+               return n;
+       }
 
-                       if (rc != 0 && errno != ENOENT && errno != ENOATTR) {
-                               DBG_WARNING("Can't delete metadata for %s: %s\n",
-                                           fsp->fsp_name->base_name, strerror(errno));
-                               goto exit;
-                       }
+       return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+}
 
-                       rc = 0;
-                       goto exit;
+static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
+                                         files_struct *fsp, const void *data,
+                                         size_t n, off_t offset)
+{
+       struct adouble *ad = NULL;
+       AfpInfo *ai = NULL;
+       char *p = NULL;
+       int ret;
+
+       ai = afpinfo_unpack(talloc_tos(), data);
+       if (ai == NULL) {
+               return -1;
+       }
+
+       if (ai_empty_finderinfo(ai)) {
+               ret = SMB_VFS_REMOVEXATTR(handle->conn,
+                                         fsp->fsp_name,
+                                         AFPINFO_EA_NETATALK);
+
+               if (ret != 0 && errno != ENOENT && errno != ENOATTR) {
+                       DBG_ERR("Can't delete metadata for %s: %s\n",
+                               fsp_str_dbg(fsp), strerror(errno));
+                       return -1;
                }
+
+               return n;
        }
 
+       ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
        if (ad == NULL) {
-               len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
-               if (len != n) {
-                       rc = -1;
-                       goto exit;
+               ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
+               if (ad == NULL) {
+                       return -1;
                }
-               goto exit;
+       }
+       p = ad_get_entry(ad, ADEID_FINDERI);
+       if (p == NULL) {
+               DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp));
+               TALLOC_FREE(ad);
+               return -1;
        }
 
-       if (!fruit_fsp_recheck(ad, fsp)) {
-               rc = -1;
-               goto exit;
+       memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
+
+       ret = ad_fset(ad, fsp);
+       if (ret != 0) {
+               DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
+               TALLOC_FREE(ad);
+               return -1;
        }
 
-       if (ad->ad_type == ADOUBLE_META) {
-               char *p = NULL;
+       TALLOC_FREE(ad);
+       return n;
+}
 
-               p = ad_get_entry(ad, ADEID_FINDERI);
-               if (p == NULL) {
-                       DBG_ERR("No ADEID_FINDERI for [%s]\n",
-                               fsp->fsp_name->base_name);
-                       rc = -1;
-                       goto exit;
+static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
+                                files_struct *fsp, const void *data,
+                                size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nwritten;
+
+       /*
+        * Writing an all 0 blob to the metadata stream
+        * results in the stream being removed on a macOS
+        * server. This ensures we behave the same and it
+        * verified by the "delete AFP_AfpInfo by writing all
+        * 0" test.
+        */
+       if (n != AFP_INFO_SIZE || offset != 0) {
+               DBG_ERR("unexpected offset=%jd or size=%jd\n",
+                       (intmax_t)offset, (intmax_t)n);
+               return -1;
+       }
+
+       switch (fio->config->meta) {
+       case FRUIT_META_STREAM:
+               nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
+                                                   n, offset);
+               break;
+
+       case FRUIT_META_NETATALK:
+               nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
+                                                     n, offset);
+               break;
+
+       default:
+               DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
+               return -1;
+       }
+
+       return nwritten;
+}
+
+static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
+                                       files_struct *fsp, const void *data,
+                                       size_t n, off_t offset)
+{
+       return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+}
+
+static ssize_t fruit_pwrite_rsrc_xattr(vfs_handle_struct *handle,
+                                      files_struct *fsp, const void *data,
+                                      size_t n, off_t offset)
+{
+       return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+}
+
+static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
+                                        files_struct *fsp, const void *data,
+                                        size_t n, off_t offset)
+{
+       struct adouble *ad = NULL;
+       ssize_t nwritten;
+       int ret;
+
+       ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
+       if (ad == NULL) {
+               DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp));
+               return -1;
+       }
+
+       nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
+                                      offset + ad_getentryoff(ad, ADEID_RFORK));
+       if (nwritten != n) {
+               DBG_ERR("Short write on [%s] [%zd/%zd]\n",
+                       fsp_str_dbg(fsp), nwritten, n);
+               TALLOC_FREE(ad);
+               return -1;
+       }
+
+       if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
+               ad_setentrylen(ad, ADEID_RFORK, n + offset);
+               ret = ad_fset(ad, fsp);
+               if (ret != 0) {
+                       DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
+                       TALLOC_FREE(ad);
+                       return -1;
                }
+       }
+
+       TALLOC_FREE(ad);
+       return n;
+}
+
+static ssize_t fruit_pwrite_rsrc(vfs_handle_struct *handle,
+                                files_struct *fsp, const void *data,
+                                size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nwritten;
 
-               memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
-               rc = ad_write(ad, name);
+       switch (fio->config->rsrc) {
+       case FRUIT_RSRC_STREAM:
+               nwritten = fruit_pwrite_rsrc_stream(handle, fsp, data, n, offset);
+               break;
+
+       case FRUIT_RSRC_ADFILE:
+               nwritten = fruit_pwrite_rsrc_adouble(handle, fsp, data, n, offset);
+               break;
+
+       case FRUIT_RSRC_XATTR:
+               nwritten = fruit_pwrite_rsrc_xattr(handle, fsp, data, n, offset);
+               break;
+
+       default:
+               DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
+               return -1;
+       }
+
+       return nwritten;
+}
+
+static ssize_t fruit_pwrite(vfs_handle_struct *handle,
+                           files_struct *fsp, const void *data,
+                           size_t n, off_t offset)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       ssize_t nwritten;
+
+       DBG_DEBUG("Path [%s] offset=%"PRIdMAX", size=%zd\n",
+                 fsp_str_dbg(fsp), (intmax_t)offset, n);
+
+       if (fio == NULL) {
+               return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+       }
+
+       if (fio->type == ADOUBLE_META) {
+               nwritten = fruit_pwrite_meta(handle, fsp, data, n, offset);
        } else {
-               len = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n,
-                                   offset + ad_getentryoff(ad, ADEID_RFORK));
-               if (len != n) {
-                       rc = -1;
-                       goto exit;
+               nwritten = fruit_pwrite_rsrc(handle, fsp, data, n, offset);
+       }
+
+       DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp), nwritten);
+       return nwritten;
+}
+
+struct fruit_pwrite_state {
+       ssize_t nwritten;
+       struct vfs_aio_state vfs_aio_state;
+};
+
+static void fruit_pwrite_done(struct tevent_req *subreq);
+
+static struct tevent_req *fruit_pwrite_send(
+       struct vfs_handle_struct *handle,
+       TALLOC_CTX *mem_ctx,
+       struct tevent_context *ev,
+       struct files_struct *fsp,
+       const void *data,
+       size_t n, off_t offset)
+{
+       struct tevent_req *req = NULL;
+       struct tevent_req *subreq = NULL;
+       struct fruit_pwrite_state *state = NULL;
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct fruit_pwrite_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (fruit_must_handle_aio_stream(fio)) {
+               state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
+               if (state->nwritten != n) {
+                       if (state->nwritten != -1) {
+                               errno = EIO;
+                       }
+                       tevent_req_error(req, errno);
+                       return tevent_req_post(req, ev);
                }
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
+       }
+
+       subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
+                                         data, n, offset);
+       if (tevent_req_nomem(req, subreq)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, fruit_pwrite_done, req);
+       return req;
+}
+
+static void fruit_pwrite_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fruit_pwrite_state *state = tevent_req_data(
+               req, struct fruit_pwrite_state);
 
-               if (config->rsrc == FRUIT_RSRC_ADFILE) {
-                       rc = ad_read(ad, name);
-                       if (rc == -1) {
-                               goto exit;
-                       }
-                       rc = 0;
+       state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
+       TALLOC_FREE(subreq);
 
-                       if ((len + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
-                               ad_setentrylen(ad, ADEID_RFORK, len + offset);
-                               rc = ad_write(ad, name);
-                       }
-               }
+       if (tevent_req_error(req, state->vfs_aio_state.error)) {
+               return;
        }
+       tevent_req_done(req);
+}
 
-exit:
-       fsp->base_fsp->fsp_name->base_name = tmp_base_name;
-       TALLOC_FREE(name);
-       TALLOC_FREE(ai);
-       if (rc != 0) {
+static ssize_t fruit_pwrite_recv(struct tevent_req *req,
+                                        struct vfs_aio_state *vfs_aio_state)
+{
+       struct fruit_pwrite_state *state = tevent_req_data(
+               req, struct fruit_pwrite_state);
+
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
                return -1;
        }
-       return n;
+
+       *vfs_aio_state = state->vfs_aio_state;
+       return state->nwritten;
 }
 
 /**
@@ -3654,7 +4172,7 @@ static int fruit_stat_meta_netatalk(vfs_handle_struct *handle,
 {
        struct adouble *ad = NULL;
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
        if (ad == NULL) {
                DBG_INFO("fruit_stat_meta %s: %s\n",
                         smb_fname_str_dbg(smb_fname), strerror(errno));
@@ -3707,7 +4225,7 @@ static int fruit_stat_rsrc_netatalk(vfs_handle_struct *handle,
        struct adouble *ad = NULL;
        int ret;
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_RSRC);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
        if (ad == NULL) {
                errno = ENOENT;
                return -1;
@@ -3894,17 +4412,24 @@ static int fruit_lstat(vfs_handle_struct *handle,
        return rc;
 }
 
-static int fruit_fstat_meta(vfs_handle_struct *handle,
-                           files_struct *fsp,
-                           SMB_STRUCT_STAT *sbuf)
+static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
+                                  files_struct *fsp,
+                                  SMB_STRUCT_STAT *sbuf)
 {
-       DEBUG(10, ("fruit_fstat_meta called for %s\n",
-                  smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
+       return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+}
 
-       /* Populate the stat struct with info from the base file. */
-       if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
+static int fruit_fstat_meta_netatalk(vfs_handle_struct *handle,
+                                    files_struct *fsp,
+                                    SMB_STRUCT_STAT *sbuf)
+{
+       int ret;
+
+       ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
+       if (ret != 0) {
                return -1;
        }
+
        *sbuf = fsp->base_fsp->fsp_name->st;
        sbuf->st_ex_size = AFP_INFO_SIZE;
        sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
@@ -3912,96 +4437,119 @@ static int fruit_fstat_meta(vfs_handle_struct *handle,
        return 0;
 }
 
-static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
-                           SMB_STRUCT_STAT *sbuf)
+static int fruit_fstat_meta(vfs_handle_struct *handle,
+                           files_struct *fsp,
+                           SMB_STRUCT_STAT *sbuf,
+                           struct fio *fio)
 {
-       struct fruit_config_data *config;
-       struct adouble *ad = (struct adouble *)VFS_FETCH_FSP_EXTENSION(
-               handle, fsp);
+       int ret;
 
-       DEBUG(10, ("fruit_fstat_rsrc called for %s\n",
-                  smb_fname_str_dbg(fsp->base_fsp->fsp_name)));
+       DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return -1);
+       switch (fio->config->meta) {
+       case FRUIT_META_STREAM:
+               ret = fruit_fstat_meta_stream(handle, fsp, sbuf);
+               break;
 
-       if (config->rsrc == FRUIT_RSRC_STREAM) {
-               return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+       case FRUIT_META_NETATALK:
+               ret = fruit_fstat_meta_netatalk(handle, fsp, sbuf);
+               break;
+
+       default:
+               DBG_ERR("Unexpected meta config [%d]\n", fio->config->meta);
+               return -1;
        }
 
+       DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp), ret);
+       return ret;
+}
+
+static int fruit_fstat_rsrc_xattr(vfs_handle_struct *handle,
+                                 files_struct *fsp,
+                                 SMB_STRUCT_STAT *sbuf)
+{
+       return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+}
+
+static int fruit_fstat_rsrc_stream(vfs_handle_struct *handle,
+                                  files_struct *fsp,
+                                  SMB_STRUCT_STAT *sbuf)
+{
+       return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+}
+
+static int fruit_fstat_rsrc_adouble(vfs_handle_struct *handle,
+                                   files_struct *fsp,
+                                   SMB_STRUCT_STAT *sbuf)
+{
+       struct adouble *ad = NULL;
+       int ret;
+
        /* Populate the stat struct with info from the base file. */
-       if (fruit_stat_base(handle, fsp->base_fsp->fsp_name, false) == -1) {
+       ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
+       if (ret == -1) {
+               return -1;
+       }
+
+       ad = ad_get(talloc_tos(), handle,
+                   fsp->base_fsp->fsp_name,
+                   ADOUBLE_RSRC);
+       if (ad == NULL) {
+               DBG_ERR("ad_get [%s] failed [%s]\n",
+                       fsp_str_dbg(fsp), strerror(errno));
                return -1;
        }
+
        *sbuf = fsp->base_fsp->fsp_name->st;
        sbuf->st_ex_size = ad_getentrylen(ad, ADEID_RFORK);
        sbuf->st_ex_ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
 
-       DEBUG(10, ("fruit_fstat_rsrc %s, size: %zd\n",
-                  smb_fname_str_dbg(fsp->fsp_name),
-                  (ssize_t)sbuf->st_ex_size));
-
+       TALLOC_FREE(ad);
        return 0;
 }
 
-static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
-                      SMB_STRUCT_STAT *sbuf)
+static int fruit_fstat_rsrc(vfs_handle_struct *handle, files_struct *fsp,
+                           SMB_STRUCT_STAT *sbuf, struct fio *fio)
 {
-       int rc;
-       char *name = NULL;
-       char *tmp_base_name = NULL;
-       NTSTATUS status;
-       struct adouble *ad = (struct adouble *)
-               VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       int ret;
 
-       DEBUG(10, ("fruit_fstat called for %s\n",
-                  smb_fname_str_dbg(fsp->fsp_name)));
+       switch (fio->config->rsrc) {
+       case FRUIT_RSRC_STREAM:
+               ret = fruit_fstat_rsrc_stream(handle, fsp, sbuf);
+               break;
 
-       if (fsp->base_fsp) {
-               tmp_base_name = fsp->base_fsp->fsp_name->base_name;
-               /* fsp_name is not converted with vfs_catia */
-               status = SMB_VFS_TRANSLATE_NAME(
-                       handle->conn,
-                       fsp->base_fsp->fsp_name->base_name,
-                       vfs_translate_to_unix,
-                       talloc_tos(), &name);
+       case FRUIT_RSRC_ADFILE:
+               ret = fruit_fstat_rsrc_adouble(handle, fsp, sbuf);
+               break;
 
-               if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
-                       name = talloc_strdup(talloc_tos(), tmp_base_name);
-                       if (name == NULL) {
-                               rc = -1;
-                               goto exit;
-                       }
-               } else if (!NT_STATUS_IS_OK(status)) {
-                       errno = map_errno_from_nt_status(status);
-                       rc = -1;
-                       goto exit;
-               }
-               fsp->base_fsp->fsp_name->base_name = name;
-       }
+       case FRUIT_RSRC_XATTR:
+               ret = fruit_fstat_rsrc_xattr(handle, fsp, sbuf);
+               break;
 
-       if (ad == NULL || fsp->base_fsp == NULL) {
-               rc = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
-               goto exit;
+       default:
+               DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
+               return -1;
        }
 
-       if (!fruit_fsp_recheck(ad, fsp)) {
-               rc = -1;
-               goto exit;
+       return ret;
+}
+
+static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
+                      SMB_STRUCT_STAT *sbuf)
+{
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       int rc;
+
+       if (fio == NULL) {
+               return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
        }
 
-       switch (ad->ad_type) {
-       case ADOUBLE_META:
-               rc = fruit_fstat_meta(handle, fsp, sbuf);
-               break;
-       case ADOUBLE_RSRC:
-               rc = fruit_fstat_rsrc(handle, fsp, sbuf);
-               break;
-       default:
-               DEBUG(10, ("fruit_fstat %s: bad type\n",
-                          smb_fname_str_dbg(fsp->fsp_name)));
-               rc = -1;
-               goto exit;
+       DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp));
+
+       if (fio->type == ADOUBLE_META) {
+               rc = fruit_fstat_meta(handle, fsp, sbuf, fio);
+       } else {
+               rc = fruit_fstat_rsrc(handle, fsp, sbuf, fio);
        }
 
        if (rc == 0) {
@@ -4010,14 +4558,8 @@ static int fruit_fstat(vfs_handle_struct *handle, files_struct *fsp,
                sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
        }
 
-exit:
-       DEBUG(10, ("fruit_fstat %s, size: %zd\n",
-                  smb_fname_str_dbg(fsp->fsp_name),
-                  (ssize_t)sbuf->st_ex_size));
-       if (tmp_base_name) {
-               fsp->base_fsp->fsp_name->base_name = tmp_base_name;
-       }
-       TALLOC_FREE(name);
+       DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX"]\n",
+                 fsp_str_dbg(fsp), rc, (intmax_t)sbuf->st_ex_size);
        return rc;
 }
 
@@ -4029,6 +4571,51 @@ static NTSTATUS fruit_streaminfo_meta_stream(
        unsigned int *pnum_streams,
        struct stream_struct **pstreams)
 {
+       struct stream_struct *stream = *pstreams;
+       unsigned int num_streams = *pnum_streams;
+       struct smb_filename *sname = NULL;
+       int i;
+       int ret;
+       bool ok;
+
+       for (i = 0; i < num_streams; i++) {
+               if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
+                       break;
+               }
+       }
+
+       if (i == num_streams) {
+               return NT_STATUS_OK;
+       }
+
+       if (stream[i].size == AFP_INFO_SIZE) {
+               return NT_STATUS_OK;
+       }
+
+       DBG_ERR("Removing invalid AFPINFO_STREAM size [%"PRIdMAX"] "
+               "from [%s]\n", (intmax_t)stream[i].size,
+               smb_fname_str_dbg(smb_fname));
+
+       ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams, AFPINFO_STREAM);
+       if (!ok) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       sname = synthetic_smb_fname(talloc_tos(),
+                                   smb_fname->base_name,
+                                   AFPINFO_STREAM_NAME,
+                                   NULL, 0);
+       if (sname == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ret = SMB_VFS_NEXT_UNLINK(handle, sname);
+       TALLOC_FREE(sname);
+       if (ret != 0) {
+               DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname));
+               return map_nt_error_from_unix(errno);
+       }
+
        return NT_STATUS_OK;
 }
 
@@ -4040,8 +4627,11 @@ static NTSTATUS fruit_streaminfo_meta_netatalk(
        unsigned int *pnum_streams,
        struct stream_struct **pstreams)
 {
+       struct stream_struct *stream = *pstreams;
+       unsigned int num_streams = *pnum_streams;
        struct adouble *ad = NULL;
        bool is_fi_empty;
+       int i;
        bool ok;
 
        /* Remove the Netatalk xattr from the list */
@@ -4051,8 +4641,28 @@ static NTSTATUS fruit_streaminfo_meta_netatalk(
                return NT_STATUS_NO_MEMORY;
        }
 
-       ad = ad_get(talloc_tos(), handle,
-                   smb_fname->base_name, ADOUBLE_META);
+       /*
+        * Check if there's a AFPINFO_STREAM from the VFS streams
+        * backend and if yes, remove it from the list
+        */
+       for (i = 0; i < num_streams; i++) {
+               if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
+                       break;
+               }
+       }
+
+       if (i < num_streams) {
+               DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
+                           smb_fname_str_dbg(smb_fname));
+
+               ok = del_fruit_stream(mem_ctx, pnum_streams, pstreams,
+                                     AFPINFO_STREAM);
+               if (!ok) {
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+       }
+
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
        if (ad == NULL) {
                return NT_STATUS_OK;
        }
@@ -4179,8 +4789,7 @@ static NTSTATUS fruit_streaminfo_rsrc_adouble(
                }
        }
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name,
-                   ADOUBLE_RSRC);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
        if (ad == NULL) {
                return NT_STATUS_OK;
        }
@@ -4297,7 +4906,7 @@ static int fruit_ntimes(vfs_handle_struct *handle,
        DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname),
                 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
 
-       ad = ad_get(talloc_tos(), handle, smb_fname->base_name, ADOUBLE_META);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
        if (ad == NULL) {
                goto exit;
        }
@@ -4305,7 +4914,7 @@ static int fruit_ntimes(vfs_handle_struct *handle,
        ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
                   convert_time_t_to_uint32_t(ft->create_time.tv_sec));
 
-       rc = ad_write(ad, smb_fname->base_name);
+       rc = ad_set(ad, smb_fname);
 
 exit:
 
@@ -4323,17 +4932,12 @@ static int fruit_fallocate(struct vfs_handle_struct *handle,
                           off_t offset,
                           off_t len)
 {
-        struct adouble *ad =
-               (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
 
-       if (ad == NULL) {
+       if (fio == NULL) {
                return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
        }
 
-       if (!fruit_fsp_recheck(ad, fsp)) {
-               return -1;
-       }
-
        /* Let the pwrite code path handle it. */
        errno = ENOSYS;
        return -1;
@@ -4358,31 +4962,35 @@ static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
                                        off_t offset)
 {
        int rc;
-        struct adouble *ad =
-               (struct adouble *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
-       off_t ad_off = ad_getentryoff(ad, ADEID_RFORK);
+       struct adouble *ad = NULL;
+       off_t ad_off;
 
-       if (!fruit_fsp_recheck(ad, fsp)) {
+       ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_RSRC);
+       if (ad == NULL) {
+               DBG_DEBUG("ad_get [%s] failed [%s]\n",
+                         fsp_str_dbg(fsp), strerror(errno));
                return -1;
        }
 
+       ad_off = ad_getentryoff(ad, ADEID_RFORK);
+
        rc = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset + ad_off);
        if (rc != 0) {
+               TALLOC_FREE(ad);
                return -1;
        }
 
        ad_setentrylen(ad, ADEID_RFORK, offset);
 
-       rc = ad_write(ad, NULL);
+       rc = ad_fset(ad, fsp);
        if (rc != 0) {
-               DBG_ERR("ad_write [%s] failed [%s]\n",
+               DBG_ERR("ad_fset [%s] failed [%s]\n",
                        fsp_str_dbg(fsp), strerror(errno));
+               TALLOC_FREE(ad);
                return -1;
        }
 
-       DBG_DEBUG("Path [%s] offset [%jd]\n",
-                 fsp_str_dbg(fsp), (intmax_t)offset);
-
+       TALLOC_FREE(ad);
        return 0;
 }
 
@@ -4401,13 +5009,10 @@ static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
                                struct files_struct *fsp,
                                off_t offset)
 {
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
        int ret;
-       struct fruit_config_data *config;
-
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return -1);
 
-       switch (config->rsrc) {
+       switch (fio->config->rsrc) {
        case FRUIT_RSRC_XATTR:
                ret = fruit_ftruncate_rsrc_xattr(handle, fsp, offset);
                break;
@@ -4421,7 +5026,7 @@ static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
                break;
 
        default:
-               DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
+               DBG_ERR("Unexpected rsrc config [%d]\n", fio->config->rsrc);
                return -1;
        }
 
@@ -4429,33 +5034,46 @@ static int fruit_ftruncate_rsrc(struct vfs_handle_struct *handle,
        return ret;
 }
 
+static int fruit_ftruncate_meta(struct vfs_handle_struct *handle,
+                               struct files_struct *fsp,
+                               off_t offset)
+{
+       if (offset > 60) {
+               DBG_WARNING("ftruncate %s to %jd",
+                           fsp_str_dbg(fsp), (intmax_t)offset);
+               /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED  */
+               errno = EOVERFLOW;
+               return -1;
+       }
+
+       /* OS X returns success but does nothing  */
+       DBG_INFO("ignoring ftruncate %s to %jd\n",
+                fsp_str_dbg(fsp), (intmax_t)offset);
+       return 0;
+}
+
 static int fruit_ftruncate(struct vfs_handle_struct *handle,
                           struct files_struct *fsp,
                           off_t offset)
 {
-       DBG_DEBUG("fruit_ftruncate called for file %s offset %.0f\n",
-                  fsp_str_dbg(fsp), (double)offset);
+       struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       int ret;
 
-       if (is_afpinfo_stream(fsp->fsp_name)) {
-               if (offset > 60) {
-                       DBG_WARNING("ftruncate %s to %jd",
-                                   fsp_str_dbg(fsp), (intmax_t)offset);
-                       /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED  */
-                       errno = EOVERFLOW;
-                       return -1;
-               }
+       DBG_DEBUG("Path [%s] offset [%"PRIdMAX"]\n", fsp_str_dbg(fsp),
+                 (intmax_t)offset);
 
-               DBG_WARNING("ignoring ftruncate %s to %jd",
-                           fsp_str_dbg(fsp), (intmax_t)offset);
-               /* OS X returns success but does nothing  */
-               return 0;
+       if (fio == NULL) {
+               return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
        }
 
-       if (is_afpresource_stream(fsp->fsp_name)) {
-               return fruit_ftruncate_rsrc(handle, fsp, offset);
+       if (fio->type == ADOUBLE_META) {
+               ret = fruit_ftruncate_meta(handle, fsp, offset);
+       } else {
+               ret = fruit_ftruncate_rsrc(handle, fsp, offset);
        }
 
-       return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
+       DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp), ret);
+       return ret;
 }
 
 static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
@@ -4505,7 +5123,7 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
 
        fsp = *result;
 
-       if (config->nego_aapl) {
+       if (global_fruit_config.nego_aapl) {
                if (config->copyfile_enabled) {
                        /*
                         * Set a flag in the fsp. Gets used in
@@ -4582,7 +5200,7 @@ static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
                                struct fruit_config_data,
                                return NT_STATUS_UNSUCCESSFUL);
 
-       if (!config->nego_aapl) {
+       if (!global_fruit_config.nego_aapl) {
                return SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
        }
 
@@ -4748,6 +5366,113 @@ static NTSTATUS fruit_fset_nt_acl(vfs_handle_struct *handle,
        return NT_STATUS_OK;
 }
 
+static struct vfs_offload_ctx *fruit_offload_ctx;
+
+struct fruit_offload_read_state {
+       struct vfs_handle_struct *handle;
+       struct tevent_context *ev;
+       files_struct *fsp;
+       uint32_t fsctl;
+       DATA_BLOB token;
+};
+
+static void fruit_offload_read_done(struct tevent_req *subreq);
+
+static struct tevent_req *fruit_offload_read_send(
+       TALLOC_CTX *mem_ctx,
+       struct tevent_context *ev,
+       struct vfs_handle_struct *handle,
+       files_struct *fsp,
+       uint32_t fsctl,
+       uint32_t ttl,
+       off_t offset,
+       size_t to_copy)
+{
+       struct tevent_req *req = NULL;
+       struct tevent_req *subreq = NULL;
+       struct fruit_offload_read_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct fruit_offload_read_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       *state = (struct fruit_offload_read_state) {
+               .handle = handle,
+               .ev = ev,
+               .fsp = fsp,
+               .fsctl = fsctl,
+       };
+
+       subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
+                                               fsctl, ttl, offset, to_copy);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, fruit_offload_read_done, req);
+       return req;
+}
+
+static void fruit_offload_read_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fruit_offload_read_state *state = tevent_req_data(
+               req, struct fruit_offload_read_state);
+       NTSTATUS status;
+
+       status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
+                                               state->handle,
+                                               state,
+                                               &state->token);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       if (state->fsctl != FSCTL_SRV_REQUEST_RESUME_KEY) {
+               tevent_req_done(req);
+               return;
+       }
+
+       status = vfs_offload_token_ctx_init(state->fsp->conn->sconn->client,
+                                           &fruit_offload_ctx);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       status = vfs_offload_token_db_store_fsp(fruit_offload_ctx,
+                                               state->fsp,
+                                               &state->token);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       tevent_req_done(req);
+       return;
+}
+
+static NTSTATUS fruit_offload_read_recv(struct tevent_req *req,
+                                       struct vfs_handle_struct *handle,
+                                       TALLOC_CTX *mem_ctx,
+                                       DATA_BLOB *token)
+{
+       struct fruit_offload_read_state *state = tevent_req_data(
+               req, struct fruit_offload_read_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
+               return status;
+       }
+
+       token->length = state->token.length;
+       token->data = talloc_move(mem_ctx, &state->token.data);
+
+       tevent_req_received(req);
+       return NT_STATUS_OK;
+}
+
 struct fruit_copy_chunk_state {
        struct vfs_handle_struct *handle;
        off_t copied;
@@ -4764,7 +5489,8 @@ static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle
                                                off_t src_off,
                                                struct files_struct *dest_fsp,
                                                off_t dest_off,
-                                               off_t num)
+                                               off_t num,
+                                               uint32_t flags)
 {
        struct tevent_req *req, *subreq;
        struct fruit_copy_chunk_state *fruit_copy_chunk_state;
@@ -4814,7 +5540,8 @@ static struct tevent_req *fruit_copy_chunk_send(struct vfs_handle_struct *handle
                                              src_off,
                                              dest_fsp,
                                              dest_off,
-                                             to_copy);
+                                             to_copy,
+                                             flags);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4958,6 +5685,10 @@ static struct vfs_fn_pointers vfs_fruit_fns = {
        .open_fn = fruit_open,
        .pread_fn = fruit_pread,
        .pwrite_fn = fruit_pwrite,
+       .pread_send_fn = fruit_pread_send,
+       .pread_recv_fn = fruit_pread_recv,
+       .pwrite_send_fn = fruit_pwrite_send,
+       .pwrite_recv_fn = fruit_pwrite_recv,
        .stat_fn = fruit_stat,
        .lstat_fn = fruit_lstat,
        .fstat_fn = fruit_fstat,
@@ -4967,6 +5698,8 @@ static struct vfs_fn_pointers vfs_fruit_fns = {
        .fallocate_fn = fruit_fallocate,
        .create_file_fn = fruit_create_file,
        .readdir_attr_fn = fruit_readdir_attr,
+       .offload_read_send_fn = fruit_offload_read_send,
+       .offload_read_recv_fn = fruit_offload_read_recv,
        .copy_chunk_send_fn = fruit_copy_chunk_send,
        .copy_chunk_recv_fn = fruit_copy_chunk_recv,
 
@@ -4975,8 +5708,8 @@ static struct vfs_fn_pointers vfs_fruit_fns = {
        .fset_nt_acl_fn = fruit_fset_nt_acl,
 };
 
-NTSTATUS vfs_fruit_init(void);
-NTSTATUS vfs_fruit_init(void)
+NTSTATUS vfs_fruit_init(TALLOC_CTX *);
+NTSTATUS vfs_fruit_init(TALLOC_CTX *ctx)
 {
        NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fruit",
                                        &vfs_fruit_fns);