s3: Add extid to the dev/inode pair
[samba.git] / source3 / modules / vfs_default.c
index de801a20414263e1f01b66a6a49ba70552153cb2..7384268dae405587c9faa21e535760c57be3db82 100644 (file)
@@ -40,10 +40,10 @@ static void vfswrap_disconnect(vfs_handle_struct *handle)
 
 /* Disk operations */
 
-static SMB_BIG_UINT vfswrap_disk_free(vfs_handle_struct *handle,  const char *path, bool small_query, SMB_BIG_UINT *bsize,
-                              SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
+static uint64_t vfswrap_disk_free(vfs_handle_struct *handle,  const char *path, bool small_query, uint64_t *bsize,
+                              uint64_t *dfree, uint64_t *dsize)
 {
-       SMB_BIG_UINT result;
+       uint64_t result;
 
        result = sys_disk_free(handle->conn, path, small_query, bsize, dfree, dsize);
        return result;
@@ -113,12 +113,18 @@ static SMB_STRUCT_DIR *vfswrap_opendir(vfs_handle_struct *handle,  const char *f
        return result;
 }
 
-static SMB_STRUCT_DIRENT *vfswrap_readdir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
+static SMB_STRUCT_DIRENT *vfswrap_readdir(vfs_handle_struct *handle,
+                                         SMB_STRUCT_DIR *dirp,
+                                         SMB_STRUCT_STAT *sbuf)
 {
        SMB_STRUCT_DIRENT *result;
 
        START_PROFILE(syscall_readdir);
        result = sys_readdir(dirp);
+       /* Default Posix readdir() does not give us stat info.
+        * Set to invalid to indicate we didn't return this info. */
+       if (sbuf)
+               SET_STAT_INVALID(*sbuf);
        END_PROFILE(syscall_readdir);
        return result;
 }
@@ -150,12 +156,17 @@ static int vfswrap_mkdir(vfs_handle_struct *handle,  const char *path, mode_t mo
 {
        int result;
        bool has_dacl = False;
+       char *parent = NULL;
 
        START_PROFILE(syscall_mkdir);
 
-       if (lp_inherit_acls(SNUM(handle->conn)) && (has_dacl = directory_has_default_acl(handle->conn, parent_dirname(path))))
+       if (lp_inherit_acls(SNUM(handle->conn))
+           && parent_dirname(talloc_tos(), path, &parent, NULL)
+           && (has_dacl = directory_has_default_acl(handle->conn, parent)))
                mode = 0777;
 
+       TALLOC_FREE(parent);
+
        result = mkdir(path, mode);
 
        if (result == 0 && !has_dacl) {
@@ -195,6 +206,12 @@ static int vfswrap_closedir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
        return result;
 }
 
+static void vfswrap_init_search_op(vfs_handle_struct *handle,
+                                  SMB_STRUCT_DIR *dirp)
+{
+       /* Default behavior is a NOOP */
+}
+
 /* File operations */
 
 static int vfswrap_open(vfs_handle_struct *handle,  const char *fname,
@@ -208,6 +225,32 @@ static int vfswrap_open(vfs_handle_struct *handle,  const char *fname,
        return result;
 }
 
+static NTSTATUS vfswrap_create_file(vfs_handle_struct *handle,
+                                   struct smb_request *req,
+                                   uint16_t root_dir_fid,
+                                   const char *fname,
+                                   uint32_t create_file_flags,
+                                   uint32_t access_mask,
+                                   uint32_t share_access,
+                                   uint32_t create_disposition,
+                                   uint32_t create_options,
+                                   uint32_t file_attributes,
+                                   uint32_t oplock_request,
+                                   uint64_t allocation_size,
+                                   struct security_descriptor *sd,
+                                   struct ea_list *ea_list,
+                                   files_struct **result,
+                                   int *pinfo,
+                                   SMB_STRUCT_STAT *psbuf)
+{
+       return create_file_default(handle->conn, req, root_dir_fid, fname,
+                                  create_file_flags, access_mask, share_access,
+                                  create_disposition, create_options,
+                                  file_attributes, oplock_request,
+                                  allocation_size, sd, ea_list, result, pinfo,
+                                  psbuf);
+}
+
 static int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp)
 {
        int result;
@@ -520,6 +563,39 @@ int vfswrap_lstat(vfs_handle_struct *handle,  const char *path, SMB_STRUCT_STAT
        return result;
 }
 
+/********************************************************************
+ Given a stat buffer return the allocated size on disk, taking into
+ account sparse files.
+********************************************************************/
+static uint64_t vfswrap_get_alloc_size(vfs_handle_struct *handle,
+                                      struct files_struct *fsp,
+                                      const SMB_STRUCT_STAT *sbuf)
+{
+       uint64_t result;
+
+       START_PROFILE(syscall_get_alloc_size);
+
+       if(S_ISDIR(sbuf->st_mode)) {
+               result = 0;
+               goto out;
+       }
+
+#if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
+       result = (uint64_t)STAT_ST_BLOCKSIZE * (uint64_t)sbuf->st_blocks;
+#else
+       result = get_file_size_stat(sbuf);
+#endif
+
+       if (fsp && fsp->initial_allocation_size)
+               result = MAX(result,fsp->initial_allocation_size);
+
+       result = smb_roundup(handle->conn, result);
+
+ out:
+       END_PROFILE(syscall_get_alloc_size);
+       return result;
+}
+
 static int vfswrap_unlink(vfs_handle_struct *handle,  const char *path)
 {
        int result;
@@ -596,7 +672,7 @@ static int vfswrap_chown(vfs_handle_struct *handle, const char *path, uid_t uid,
        int result;
 
        START_PROFILE(syscall_chown);
-       result = sys_chown(path, uid, gid);
+       result = chown(path, uid, gid);
        END_PROFILE(syscall_chown);
        return result;
 }
@@ -621,7 +697,7 @@ static int vfswrap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid
        int result;
 
        START_PROFILE(syscall_lchown);
-       result = sys_lchown(path, uid, gid);
+       result = lchown(path, uid, gid);
        END_PROFILE(syscall_lchown);
        return result;
 }
@@ -651,24 +727,29 @@ static char *vfswrap_getwd(vfs_handle_struct *handle,  char *path)
  system will support.
 **********************************************************************/
 
-static int vfswrap_ntimes(vfs_handle_struct *handle, const char *path, const struct timespec ts[2])
+static int vfswrap_ntimes(vfs_handle_struct *handle, const char *path,
+                         struct smb_file_time *ft)
 {
        int result;
 
        START_PROFILE(syscall_ntimes);
 #if defined(HAVE_UTIMES)
-       {
+       if (ft != NULL) {
                struct timeval tv[2];
-               tv[0] = convert_timespec_to_timeval(ts[0]);
-               tv[1] = convert_timespec_to_timeval(ts[1]);
+               tv[0] = convert_timespec_to_timeval(ft->atime);
+               tv[1] = convert_timespec_to_timeval(ft->mtime);
                result = utimes(path, tv);
+       } else {
+               result = utimes(path, NULL);
        }
 #elif defined(HAVE_UTIME)
-       {
+       if (ft != NULL) {
                struct utimbuf times;
-               times.actime = convert_timespec_to_time_t(ts[0]);
-               times.modtime = convert_timespec_to_time_t(ts[1]);
-               result = utime(path, times);
+               times.actime = convert_timespec_to_time_t(ft->atime);
+               times.modtime = convert_timespec_to_time_t(ft->mtime);
+               result = utime(path, &times);
+       } else {
+               result = utime(path, NULL);
        }
 #else
        errno = ENOSYS;
@@ -710,6 +791,20 @@ static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fs
        if (st.st_size > len)
                return sys_ftruncate(fsp->fh->fd, len);
 
+       /* available disk space is enough or not? */
+       if (lp_strict_allocate(SNUM(fsp->conn))){
+               uint64_t space_avail;
+               uint64_t bsize,dfree,dsize;
+
+               space_avail = get_dfree_info(fsp->conn,fsp->fsp_name,false,&bsize,&dfree,&dsize);
+               /* space_avail is 1k blocks */
+               if (space_avail == (uint64_t)-1 ||
+                               ((uint64_t)space_to_write/1024 > space_avail) ) {
+                       errno = ENOSPC;
+                       return -1;
+               }
+       }
+
        /* Write out the real space on disk. */
        if (SMB_VFS_LSEEK(fsp, st.st_size, SEEK_SET) != st.st_size)
                return -1;
@@ -865,7 +960,7 @@ static int vfswrap_symlink(vfs_handle_struct *handle,  const char *oldpath, cons
        int result;
 
        START_PROFILE(syscall_symlink);
-       result = sys_symlink(oldpath, newpath);
+       result = symlink(oldpath, newpath);
        END_PROFILE(syscall_symlink);
        return result;
 }
@@ -875,7 +970,7 @@ static int vfswrap_readlink(vfs_handle_struct *handle,  const char *path, char *
        int result;
 
        START_PROFILE(syscall_readlink);
-       result = sys_readlink(path, buf, bufsiz);
+       result = readlink(path, buf, bufsiz);
        END_PROFILE(syscall_readlink);
        return result;
 }
@@ -885,7 +980,7 @@ static int vfswrap_link(vfs_handle_struct *handle,  const char *oldpath, const c
        int result;
 
        START_PROFILE(syscall_link);
-       result = sys_link(oldpath, newpath);
+       result = link(oldpath, newpath);
        END_PROFILE(syscall_link);
        return result;
 }
@@ -905,7 +1000,7 @@ static char *vfswrap_realpath(vfs_handle_struct *handle,  const char *path, char
        char *result;
 
        START_PROFILE(syscall_realpath);
-       result = sys_realpath(path, resolved_path);
+       result = realpath(path, resolved_path);
        END_PROFILE(syscall_realpath);
        return result;
 }
@@ -947,9 +1042,20 @@ static int vfswrap_chflags(vfs_handle_struct *handle, const char *path, int flag
 #endif
 }
 
-static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle, SMB_DEV_T dev, SMB_INO_T inode)
+static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
+                                            SMB_STRUCT_STAT *sbuf)
 {
-       return file_id_create_dev(dev, inode);
+       struct file_id key;
+
+       /* the ZERO_STRUCT ensures padding doesn't break using the key as a
+        * blob */
+       ZERO_STRUCT(key);
+
+       key.devid = sbuf->st_dev;
+       key.inode = sbuf->st_ino;
+       /* key.extid is unused by default. */
+
+       return key;
 }
 
 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
@@ -993,7 +1099,7 @@ static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
        }
 
        streams->size = sbuf.st_size;
-       streams->alloc_size = get_allocation_size(handle->conn, fsp, &sbuf);
+       streams->alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
 
        streams->name = talloc_strdup(streams, "::$DATA");
        if (streams->name == NULL) {
@@ -1008,6 +1114,51 @@ static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
        return NT_STATUS_OK;
 }
 
+static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
+                                    const char *path,
+                                    const char *name,
+                                    TALLOC_CTX *mem_ctx,
+                                    char **found_name)
+{
+       return get_real_filename(handle->conn, path, name, mem_ctx,
+                                found_name);
+}
+
+static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
+                                        struct byte_range_lock *br_lck,
+                                        struct lock_struct *plock,
+                                        bool blocking_lock,
+                                        struct blocking_lock_record *blr)
+{
+       SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
+
+       /* Note: blr is not used in the default implementation. */
+       return brl_lock_windows_default(br_lck, plock, blocking_lock);
+}
+
+static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
+                                      struct messaging_context *msg_ctx,
+                                      struct byte_range_lock *br_lck,
+                                      const struct lock_struct *plock)
+{
+       SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
+
+       return brl_unlock_windows_default(msg_ctx, br_lck, plock);
+}
+
+static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
+                                      struct byte_range_lock *br_lck,
+                                      struct lock_struct *plock,
+                                      struct blocking_lock_record *blr)
+{
+       SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
+
+       /* Note: blr is not used in the default implementation. */
+       return brl_lock_cancel_default(br_lck, plock);
+}
+
+/* NT ACL operations. */
+
 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
                                    files_struct *fsp,
                                    uint32 security_info, SEC_DESC **ppdesc)
@@ -1032,7 +1183,7 @@ static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
        return result;
 }
 
-static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
+static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
 {
        NTSTATUS result;
 
@@ -1042,16 +1193,6 @@ static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp
        return result;
 }
 
-static NTSTATUS vfswrap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, SEC_DESC *psd)
-{
-       NTSTATUS result;
-
-       START_PROFILE(set_nt_acl);
-       result = set_nt_acl(fsp, security_info_sent, psd);
-       END_PROFILE(set_nt_acl);
-       return result;
-}
-
 static int vfswrap_chmod_acl(vfs_handle_struct *handle,  const char *name, mode_t mode)
 {
 #ifdef HAVE_NO_ACL
@@ -1360,11 +1501,15 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_closedir),  SMB_VFS_OP_CLOSEDIR,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_init_search_op), SMB_VFS_OP_INIT_SEARCH_OP,
+        SMB_VFS_LAYER_OPAQUE},
 
        /* File operations */
 
        {SMB_VFS_OP(vfswrap_open),      SMB_VFS_OP_OPEN,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_create_file),       SMB_VFS_OP_CREATE_FILE,
+        SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_close),     SMB_VFS_OP_CLOSE,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_read),      SMB_VFS_OP_READ,
@@ -1391,6 +1536,8 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_lstat),     SMB_VFS_OP_LSTAT,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_get_alloc_size),    SMB_VFS_OP_GET_ALLOC_SIZE,
+        SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_unlink),    SMB_VFS_OP_UNLINK,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_chmod),     SMB_VFS_OP_CHMOD,
@@ -1437,6 +1584,14 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_streaminfo),        SMB_VFS_OP_STREAMINFO,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_get_real_filename), SMB_VFS_OP_GET_REAL_FILENAME,
+        SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_brl_lock_windows),  SMB_VFS_OP_BRL_LOCK_WINDOWS,
+        SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_brl_unlock_windows),SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
+        SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_brl_cancel_windows),SMB_VFS_OP_BRL_CANCEL_WINDOWS,
+        SMB_VFS_LAYER_OPAQUE},
 
        /* NT ACL operations. */
 
@@ -1446,8 +1601,6 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_fset_nt_acl),       SMB_VFS_OP_FSET_NT_ACL,
         SMB_VFS_LAYER_OPAQUE},
-       {SMB_VFS_OP(vfswrap_set_nt_acl),        SMB_VFS_OP_SET_NT_ACL,
-        SMB_VFS_LAYER_OPAQUE},
 
        /* POSIX ACL operations. */