These modules are no longer experimental but production-ready (especially
[amitay/samba.git] / source3 / modules / vfs_streams_xattr.c
index eccc2379c9b1eacd5d64956c677b4a78a8a16f6b..2772c96c4d8f1f938cd4151d0922985a86e76561 100644 (file)
@@ -22,6 +22,9 @@
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "system/filesys.h"
+#include "../lib/crypto/md5.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
@@ -49,9 +52,9 @@ static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
        SMB_ASSERT(upper_sname != NULL);
 
         MD5Init(&ctx);
-        MD5Update(&ctx, (unsigned char *)&(sbuf->st_ex_dev),
+        MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
                  sizeof(sbuf->st_ex_dev));
-        MD5Update(&ctx, (unsigned char *)&(sbuf->st_ex_ino),
+        MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
                  sizeof(sbuf->st_ex_ino));
         MD5Update(&ctx, (unsigned char *)upper_sname,
                  talloc_get_size(upper_sname)-1);
@@ -128,27 +131,20 @@ static NTSTATUS streams_xattr_get_name(TALLOC_CTX *ctx,
 static bool streams_xattr_recheck(struct stream_io *sio)
 {
        NTSTATUS status;
-       struct smb_filename *smb_fname = NULL;
        char *xattr_name = NULL;
 
        if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
                return true;
        }
 
-       status = create_synthetic_smb_fname_split(talloc_tos(),
-                                                 sio->fsp->fsp_name, NULL,
-                                                 &smb_fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return false;
-       }
-
-       if (smb_fname->stream_name == NULL) {
+       if (sio->fsp->fsp_name->stream_name == NULL) {
                /* how can this happen */
                errno = EINVAL;
                return false;
        }
 
-       status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
+       status = streams_xattr_get_name(talloc_tos(),
+                                       sio->fsp->fsp_name->stream_name,
                                        &xattr_name);
        if (!NT_STATUS_IS_OK(status)) {
                return false;
@@ -159,10 +155,9 @@ static bool streams_xattr_recheck(struct stream_io *sio)
        sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
                                        xattr_name);
        sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
-                                 smb_fname->base_name);
+                                 sio->fsp->fsp_name->base_name);
        sio->fsp_name_ptr = sio->fsp->fsp_name;
 
-       TALLOC_FREE(smb_fname);
        TALLOC_FREE(xattr_name);
 
        if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
@@ -245,7 +240,7 @@ static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
        sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
        sbuf->st_ex_mode &= ~S_IFMT;
         sbuf->st_ex_mode |= S_IFREG;
-        sbuf->st_ex_blocks = sbuf->st_ex_size % STAT_ST_BLOCKSIZE + 1;
+        sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
 
        return 0;
 }
@@ -261,6 +256,11 @@ static int streams_xattr_stat(vfs_handle_struct *handle,
                return SMB_VFS_NEXT_STAT(handle, smb_fname);
        }
 
+       /* Note if lp_posix_paths() is true, we can never
+        * get here as is_ntfs_stream_smb_fname() is
+        * always false. So we never need worry about
+        * not following links here. */
+
        /* If the default stream is requested, just stat the base file. */
        if (is_ntfs_default_stream_smb_fname(smb_fname)) {
                return streams_xattr_stat_base(handle, smb_fname, true);
@@ -293,7 +293,7 @@ static int streams_xattr_stat(vfs_handle_struct *handle,
        smb_fname->st.st_ex_mode &= ~S_IFMT;
         smb_fname->st.st_ex_mode |= S_IFREG;
         smb_fname->st.st_ex_blocks =
-           smb_fname->st.st_ex_size % STAT_ST_BLOCKSIZE + 1;
+           smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
 
        result = 0;
  fail:
@@ -312,7 +312,7 @@ static int streams_xattr_lstat(vfs_handle_struct *handle,
                return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
        }
 
-               /* If the default stream is requested, just stat the base file. */
+       /* If the default stream is requested, just stat the base file. */
        if (is_ntfs_default_stream_smb_fname(smb_fname)) {
                return streams_xattr_stat_base(handle, smb_fname, false);
        }
@@ -344,7 +344,7 @@ static int streams_xattr_lstat(vfs_handle_struct *handle,
        smb_fname->st.st_ex_mode &= ~S_IFMT;
         smb_fname->st.st_ex_mode |= S_IFREG;
         smb_fname->st.st_ex_blocks =
-           smb_fname->st.st_ex_size % STAT_ST_BLOCKSIZE + 1;
+           smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
 
        result = 0;
 
@@ -614,7 +614,7 @@ static int streams_xattr_rename(vfs_handle_struct *handle,
        }
 
        /* Don't rename if the streams are identical. */
-       if (StrCaseCmp(smb_fname_src->stream_name,
+       if (strcasecmp_m(smb_fname_src->stream_name,
                       smb_fname_dst->stream_name) == 0) {
                goto done;
        }
@@ -731,7 +731,7 @@ static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
 {
        struct stream_struct *tmp;
 
-       tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
+       tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
                                   (*num_streams)+1);
        if (tmp == NULL) {
                return false;
@@ -845,9 +845,10 @@ static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
        return NT_STATUS_OK;
 }
 
-static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
+static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
+                       enum timestamp_set_resolution *p_ts_res)
 {
-       return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
+       return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
 }
 
 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
@@ -879,7 +880,7 @@ static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
         if ((offset + n) > ea.value.length-1) {
                uint8 *tmp;
 
-               tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
+               tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
                                           offset + n + 1);
 
                if (tmp == NULL) {
@@ -899,7 +900,8 @@ static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
                                sio->xattr_name,
                                ea.value.data, ea.value.length, 0);
        } else {
-               ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
+               ret = SMB_VFS_SETXATTR(fsp->conn,
+                                      fsp->base_fsp->fsp_name->base_name,
                                sio->xattr_name,
                                ea.value.data, ea.value.length, 0);
        }
@@ -922,6 +924,9 @@ static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
        NTSTATUS status;
        size_t length, overlap;
 
+       DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
+                  (int)offset, (int)n));
+
        if (sio == NULL) {
                return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
        }
@@ -938,10 +943,12 @@ static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
 
        length = ea.value.length-1;
 
+       DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
+                  (int)length));
+
         /* Attempt to read past EOF. */
         if (length <= offset) {
-                errno = EINVAL;
-                return -1;
+                return 0;
         }
 
         overlap = (offset + n) > length ? (length - offset) : n;
@@ -963,8 +970,7 @@ static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
                (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
 
        DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
-               fsp->fsp_name,
-               (double)offset ));
+                  fsp_str_dbg(fsp), (double)offset));
 
        if (sio == NULL) {
                return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
@@ -980,7 +986,7 @@ static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
                return -1;
        }
 
-       tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
+       tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
                                   offset + 1);
 
        if (tmp == NULL) {
@@ -1004,7 +1010,8 @@ static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
                                sio->xattr_name,
                                ea.value.data, ea.value.length, 0);
        } else {
-               ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
+               ret = SMB_VFS_SETXATTR(fsp->conn,
+                                      fsp->base_fsp->fsp_name->base_name,
                                sio->xattr_name,
                                ea.value.data, ea.value.length, 0);
        }
@@ -1018,37 +1025,50 @@ static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
        return 0;
 }
 
-/* VFS operations structure */
-
-static vfs_op_tuple streams_xattr_ops[] = {
-       {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_rename), SMB_VFS_OP_RENAME,
-        SMB_VFS_LAYER_TRANSPARENT},
-        {SMB_VFS_OP(streams_xattr_ftruncate),  SMB_VFS_OP_FTRUNCATE,
-         SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
-        SMB_VFS_LAYER_OPAQUE},
-       {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
+                                       struct files_struct *fsp,
+                                       enum vfs_fallocate_mode mode,
+                                       SMB_OFF_T offset,
+                                       SMB_OFF_T len)
+{
+        struct stream_io *sio =
+               (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+
+       DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
+               "len = %.0f\n",
+               fsp_str_dbg(fsp), (double)offset, (double)len));
+
+       if (sio == NULL) {
+               return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
+       }
+
+       if (!streams_xattr_recheck(sio)) {
+               return errno;
+       }
+
+       /* Let the pwrite code path handle it. */
+       return ENOSYS;
+}
+
+
+static struct vfs_fn_pointers vfs_streams_xattr_fns = {
+       .fs_capabilities = streams_xattr_fs_capabilities,
+       .open_fn = streams_xattr_open,
+       .stat = streams_xattr_stat,
+       .fstat = streams_xattr_fstat,
+       .lstat = streams_xattr_lstat,
+       .pread = streams_xattr_pread,
+       .pwrite = streams_xattr_pwrite,
+       .unlink = streams_xattr_unlink,
+       .rename = streams_xattr_rename,
+        .ftruncate = streams_xattr_ftruncate,
+        .fallocate = streams_xattr_fallocate,
+       .streaminfo = streams_xattr_streaminfo,
 };
 
 NTSTATUS vfs_streams_xattr_init(void);
 NTSTATUS vfs_streams_xattr_init(void)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
-                               streams_xattr_ops);
+                               &vfs_streams_xattr_fns);
 }