vfs_fruit: don't open basefile in ad_open() and simplify API
authorRalph Boehme <slow@samba.org>
Tue, 23 May 2017 15:31:47 +0000 (17:31 +0200)
committerKarolin Seeger <kseeger@samba.org>
Mon, 14 Aug 2017 08:50:10 +0000 (10:50 +0200)
We never need an fd on the basefile when operating on the metadata, as
we can always use path based syscalls. Opening the basefile conflicts
with "kernel oplocks" so just don't do it.

Additional changes:

- remove the adouble_type_t argument to ad_open(), the type is passed
  and set when allocating a struct adouble with ad_alloc()

- additionally pass an optional fsp to ad_open() (so the caller can pass
  NULL). With this change we can move the fd inheritance from fsp to ad
  into ad_open() itself where it belongs and remove it from the caller
  ad_fget()

Bug: https://bugzilla.samba.org/show_bug.cgi?id=12791

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Richard Sharpe <realrichardsharpe@gmail.com>
(backported from commit e92a39255e66f655e2758f0a71a01eaf258cf711)

source3/modules/vfs_fruit.c

index 1046e6a1e66e5d4d0cfeb794d2297e1fb915fca7..8ed848ce481e8968698dce6111b0127dc9ca6fad 100644 (file)
@@ -859,11 +859,6 @@ exit:
        return ealen;
 }
 
-static int ad_open_meta(const char *path, int flags, mode_t mode)
-{
-       return open(path, flags, mode);
-}
-
 static int ad_open_rsrc_xattr(const char *path, int flags, mode_t mode)
 {
 #ifdef HAVE_ATTROPEN
@@ -912,33 +907,44 @@ static int ad_open_rsrc(vfs_handle_struct *handle,
        return fd;
 }
 
+/*
+ * 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.
+ */
 static int ad_open(vfs_handle_struct *handle,
                   struct adouble *ad,
+                  files_struct *fsp,
                   const char *path,
-                  adouble_type_t t,
                   int flags,
                   mode_t mode)
 {
        int fd;
 
        DBG_DEBUG("Path [%s] type [%s]\n",
-                 path, t == ADOUBLE_META ? "meta" : "rsrc");
+                 path, ad->ad_type == ADOUBLE_META ? "meta" : "rsrc");
 
-       if (t == ADOUBLE_META) {
-               fd = ad_open_meta(path, flags, mode);
-       } else {
-               fd = ad_open_rsrc(handle, path, flags, mode);
+       if (ad->ad_type == ADOUBLE_META) {
+               return 0;
+       }
+
+       if ((fsp != NULL) && (fsp->fh != NULL) && (fsp->fh->fd != -1)) {
+               ad->ad_fd = fsp->fh->fd;
+               ad->ad_opened = false;
+               return 0;
        }
 
-       if (fd != -1) {
-               ad->ad_opened = true;
-               ad->ad_fd = fd;
+       fd = ad_open_rsrc(handle, path, flags, mode);
+       if (fd == -1) {
+               return -1;
        }
+       ad->ad_opened = true;
+       ad->ad_fd = fd;
 
        DBG_DEBUG("Path [%s] type [%s] fd [%d]\n",
-                 path, t == ADOUBLE_META ? "meta" : "rsrc", fd);
+                 path, ad->ad_type == ADOUBLE_META ? "meta" : "rsrc", fd);
 
-       return fd;
+       return 0;
 }
 
 static ssize_t ad_read_rsrc_xattr(struct adouble *ad,
@@ -1240,7 +1246,6 @@ static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
        int rc = 0;
        ssize_t len;
        struct adouble *ad = NULL;
-       int fd;
        int mode;
 
        DEBUG(10, ("ad_get(%s) called for %s\n",
@@ -1252,28 +1257,19 @@ static struct adouble *ad_get(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                goto exit;
        }
 
-       /*
-        * 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, path, ADOUBLE_RSRC, mode, 0);
-               if (fd == -1 && ((errno == EROFS) || (errno == EACCES))) {
-                       mode = O_RDONLY;
-                       fd = ad_open(handle, ad, path, ADOUBLE_RSRC, mode, 0);
-               }
+       /* Try rw first so we can use the fd in ad_convert() */
+       mode = O_RDWR;
 
-               if (fd == -1) {
-                       DBG_DEBUG("ad_open [%s] error [%s]\n",
-                                 path, strerror(errno));
-                       rc = -1;
-                       goto exit;
-               }
+       rc = ad_open(handle, ad, NULL, path, mode, 0);
+       if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
+               mode = O_RDONLY;
+               rc = ad_open(handle, ad, NULL, path, mode, 0);
+       }
+
+       if (rc == -1) {
+               DBG_DEBUG("ad_open [%s] error [%s]\n",
+                         path, strerror(errno));
+               goto exit;
        }
 
        len = ad_read(ad, path);
@@ -1310,6 +1306,7 @@ static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
        ssize_t len;
        struct adouble *ad = NULL;
        char *path = fsp->base_fsp->fsp_name->base_name;
+       int mode;
 
        DBG_DEBUG("ad_get(%s) path [%s]\n",
                  type == ADOUBLE_META ? "meta" : "rsrc",
@@ -1321,37 +1318,17 @@ static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                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, path, ADOUBLE_RSRC, mode, 0);
-                       if (fd == -1 &&
-                           ((errno == EROFS) || (errno == EACCES)))
-                       {
-                               mode = O_RDONLY;
-                               fd = ad_open(handle, ad, path, ADOUBLE_RSRC,
-                                            mode, 0);
-                       }
+       /* Try rw first so we can use the fd in ad_convert() */
+       mode = O_RDWR;
 
-                       if (fd == -1) {
-                               DBG_DEBUG("error opening AppleDouble for %s\n", path);
-                               rc = -1;
-                               goto exit;
-                       }
-               }
+       rc = ad_open(handle, ad, fsp, path, mode, 0);
+       if (rc == -1 && ((errno == EROFS) || (errno == EACCES))) {
+               mode = O_RDONLY;
+               rc = ad_open(handle, ad, fsp, path, mode, 0);
+       }
+       if (rc == -1) {
+               DBG_DEBUG("error opening AppleDouble [%s]\n", fsp_str_dbg(fsp));
+               goto exit;
        }
 
        len = ad_read(ad, path);