Remove redundant parameter fd from SMB_VFS_LSEEK().
[ira/wip.git] / source3 / modules / vfs_default.c
index 458618ee0b3e98cab8634e707df3da4e7d1be18e..41162e67ca9a1f80fc3728b67d78ec411324768d 100644 (file)
@@ -2,10 +2,11 @@
    Unix SMB/CIFS implementation.
    Wrap disk only vfs functions to sidestep dodgy compilers.
    Copyright (C) Tim Potter 1998
+   Copyright (C) Jeremy Allison 2007
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -14,8 +15,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -40,7 +40,7 @@ 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,
+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)
 {
        SMB_BIG_UINT result;
@@ -79,7 +79,7 @@ static int vfswrap_set_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_T
 #endif
 }
 
-static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle, struct files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, BOOL labels)
+static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle, struct files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, bool labels)
 {
        errno = ENOSYS;
        return -1;  /* Not implemented. */
@@ -138,7 +138,7 @@ static void vfswrap_rewinddir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
 static int vfswrap_mkdir(vfs_handle_struct *handle,  const char *path, mode_t mode)
 {
        int result;
-       BOOL has_dacl = False;
+       bool has_dacl = False;
 
        START_PROFILE(syscall_mkdir);
 
@@ -218,19 +218,19 @@ static ssize_t vfswrap_read(vfs_handle_struct *handle, files_struct *fsp, int fd
        return result;
 }
 
-static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, int fd, void *data,
+static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, void *data,
                        size_t n, SMB_OFF_T offset)
 {
        ssize_t result;
 
 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
        START_PROFILE_BYTES(syscall_pread, n);
-       result = sys_pread(fd, data, n, offset);
+       result = sys_pread(fsp->fh->fd, data, n, offset);
        END_PROFILE(syscall_pread);
 
        if (result == -1 && errno == ESPIPE) {
                /* Maintain the fiction that pipes can be seeked (sought?) on. */
-               result = SMB_VFS_READ(fsp, fd, data, n);
+               result = SMB_VFS_READ(fsp, fsp->fh->fd, data, n);
                fsp->fh->pos = 0;
        }
 
@@ -238,23 +238,23 @@ static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, int f
        SMB_OFF_T   curr;
        int lerrno;
 
-       curr = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
+       curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
        if (curr == -1 && errno == ESPIPE) {
                /* Maintain the fiction that pipes can be seeked (sought?) on. */
-               result = SMB_VFS_READ(fsp, fd, data, n);
+               result = SMB_VFS_READ(fsp, fsp->fh->fd, data, n);
                fsp->fh->pos = 0;
                return result;
        }
 
-       if (SMB_VFS_LSEEK(fsp, fd, offset, SEEK_SET) == -1) {
+       if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
                return -1;
        }
 
        errno = 0;
-       result = SMB_VFS_READ(fsp, fd, data, n);
+       result = SMB_VFS_READ(fsp, fsp->fh->fd, data, n);
        lerrno = errno;
 
-       SMB_VFS_LSEEK(fsp, fd, curr, SEEK_SET);
+       SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
        errno = lerrno;
 
 #endif /* HAVE_PREAD */
@@ -272,38 +272,38 @@ static ssize_t vfswrap_write(vfs_handle_struct *handle, files_struct *fsp, int f
        return result;
 }
 
-static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, int fd, const void *data,
+static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data,
                        size_t n, SMB_OFF_T offset)
 {
        ssize_t result;
 
 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
        START_PROFILE_BYTES(syscall_pwrite, n);
-       result = sys_pwrite(fd, data, n, offset);
+       result = sys_pwrite(fsp->fh->fd, data, n, offset);
        END_PROFILE(syscall_pwrite);
 
        if (result == -1 && errno == ESPIPE) {
                /* Maintain the fiction that pipes can be sought on. */
-               result = SMB_VFS_WRITE(fsp, fd, data, n);
+               result = SMB_VFS_WRITE(fsp, fsp->fh->fd, data, n);
        }
 
 #else /* HAVE_PWRITE */
        SMB_OFF_T   curr;
        int         lerrno;
 
-       curr = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
+       curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
        if (curr == -1) {
                return -1;
        }
 
-       if (SMB_VFS_LSEEK(fsp, fd, offset, SEEK_SET) == -1) {
+       if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
                return -1;
        }
 
-       result = SMB_VFS_WRITE(fsp, fd, data, n);
+       result = SMB_VFS_WRITE(fsp, fsp->fh->fd, data, n);
        lerrno = errno;
 
-       SMB_VFS_LSEEK(fsp, fd, curr, SEEK_SET);
+       SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
        errno = lerrno;
 
 #endif /* HAVE_PWRITE */
@@ -311,15 +311,15 @@ static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, int
        return result;
 }
 
-static SMB_OFF_T vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, int filedes, SMB_OFF_T offset, int whence)
+static SMB_OFF_T vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T offset, int whence)
 {
        SMB_OFF_T result = 0;
 
        START_PROFILE(syscall_lseek);
 
        /* Cope with 'stat' file opens. */
-       if (filedes != -1)
-               result = sys_lseek(filedes, offset, whence);
+       if (fsp->fh->fd != -1)
+               result = sys_lseek(fsp->fh->fd, offset, whence);
 
        /*
         * We want to maintain the fiction that we can seek
@@ -348,6 +348,21 @@ static ssize_t vfswrap_sendfile(vfs_handle_struct *handle, int tofd, files_struc
        return result;
 }
 
+static ssize_t vfswrap_recvfile(vfs_handle_struct *handle,
+                       int fromfd,
+                       files_struct *fsp,
+                       int tofd,
+                       SMB_OFF_T offset,
+                       size_t n)
+{
+       ssize_t result;
+
+       START_PROFILE_BYTES(syscall_recvfile, n);
+       result = sys_recvfile(fromfd, tofd, offset, n);
+       END_PROFILE(syscall_recvfile);
+       return result;
+}
+
 /*********************************************************
  For rename across filesystems Patch from Warren Birnbaum
  <warrenb@hpcvscdp.cv.hp.com>
@@ -567,7 +582,7 @@ static int vfswrap_fchmod(vfs_handle_struct *handle, files_struct *fsp, int fd,
        return result;
 }
 
-static int vfswrap_chown(vfs_handle_struct *handle,  const char *path, uid_t uid, gid_t gid)
+static int vfswrap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
 {
        int result;
 
@@ -592,6 +607,16 @@ static int vfswrap_fchown(vfs_handle_struct *handle, files_struct *fsp, int fd,
 #endif
 }
 
+static int vfswrap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
+{
+       int result;
+
+       START_PROFILE(syscall_lchown);
+       result = sys_lchown(path, uid, gid);
+       END_PROFILE(syscall_lchown);
+       return result;
+}
+
 static int vfswrap_chdir(vfs_handle_struct *handle,  const char *path)
 {
        int result;
@@ -612,13 +637,35 @@ static char *vfswrap_getwd(vfs_handle_struct *handle,  char *path)
        return result;
 }
 
-static int vfswrap_utime(vfs_handle_struct *handle,  const char *path, struct utimbuf *times)
+/*********************************************************************
+ nsec timestamp resolution call. Convert down to whatever the underlying
+ system will support.
+**********************************************************************/
+
+static int vfswrap_ntimes(vfs_handle_struct *handle, const char *path, const struct timespec ts[2])
 {
        int result;
 
-       START_PROFILE(syscall_utime);
-       result = utime(path, times);
-       END_PROFILE(syscall_utime);
+       START_PROFILE(syscall_ntimes);
+#if defined(HAVE_UTIMES)
+       {
+               struct timeval tv[2];
+               tv[0] = convert_timespec_to_timeval(ts[0]);
+               tv[1] = convert_timespec_to_timeval(ts[1]);
+               result = utimes(path, tv);
+       }
+#elif defined(HAVE_UTIME)
+       {
+               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);
+       }
+#else
+       errno = ENOSYS;
+       result = -1;
+#endif
+       END_PROFILE(syscall_ntimes);
        return result;
 }
 
@@ -630,7 +677,7 @@ static int vfswrap_utime(vfs_handle_struct *handle,  const char *path, struct ut
 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, int fd, SMB_OFF_T len)
 {
        SMB_STRUCT_STAT st;
-       SMB_OFF_T currpos = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
+       SMB_OFF_T currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
        unsigned char zero_space[4096];
        SMB_OFF_T space_to_write;
 
@@ -655,7 +702,7 @@ static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fs
                return sys_ftruncate(fd, len);
 
        /* Write out the real space on disk. */
-       if (SMB_VFS_LSEEK(fsp, fd, st.st_size, SEEK_SET) != st.st_size)
+       if (SMB_VFS_LSEEK(fsp, st.st_size, SEEK_SET) != st.st_size)
                return -1;
 
        space_to_write = len - st.st_size;
@@ -673,7 +720,7 @@ static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fs
        }
 
        /* Seek to where we were */
-       if (SMB_VFS_LSEEK(fsp, fd, currpos, SEEK_SET) != currpos)
+       if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
                return -1;
 
        return 0;
@@ -707,7 +754,7 @@ static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, int f
        /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
           extend a file with ftruncate. Provide alternate implementation
           for this */
-       currpos = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
+       currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
        if (currpos == -1) {
                goto done;
        }
@@ -737,14 +784,14 @@ static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, int f
                goto done;
        }
 
-       if (SMB_VFS_LSEEK(fsp, fd, len-1, SEEK_SET) != len -1)
+       if (SMB_VFS_LSEEK(fsp, len-1, SEEK_SET) != len -1)
                goto done;
 
        if (SMB_VFS_WRITE(fsp, fd, &c, 1)!=1)
                goto done;
 
        /* Seek to where we were */
-       if (SMB_VFS_LSEEK(fsp, fd, currpos, SEEK_SET) != currpos)
+       if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
                goto done;
        result = 0;
 
@@ -754,9 +801,9 @@ static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, int f
        return result;
 }
 
-static BOOL vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
+static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
 {
-       BOOL result;
+       bool result;
 
        START_PROFILE(syscall_fcntl_lock);
        result =  fcntl_lock(fd, op, offset, count, type);
@@ -773,9 +820,9 @@ static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp, in
        return 0;
 }
 
-static BOOL vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
+static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
 {
-       BOOL result;
+       bool result;
 
        START_PROFILE(syscall_fcntl_getlock);
        result =  fcntl_getlock(fd, poffset, pcount, ptype, ppid);
@@ -783,6 +830,27 @@ static BOOL vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, int fd
        return result;
 }
 
+static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp, int fd,
+                               int leasetype)
+{
+       int result = -1;
+
+       START_PROFILE(syscall_linux_setlease);
+
+#ifdef HAVE_KERNEL_OPLOCKS_LINUX
+       /* first set the signal handler */
+       if(linux_set_lease_sighandler(fd) == -1) {
+               return -1;
+       }
+
+       result = linux_setlease(fd, leasetype);
+#else
+       errno = ENOSYS;
+#endif
+       END_PROFILE(syscall_linux_setlease);
+       return result;
+}
+
 static int vfswrap_symlink(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
 {
        int result;
@@ -841,6 +909,14 @@ static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
                                                      struct notify_event *ev),
                                     void *private_data, void *handle)
 {
+       /*
+        * So far inotify is the only supported default notify mechanism. If
+        * another platform like the the BSD's or a proprietary Unix comes
+        * along and wants another default, we can play the same trick we
+        * played with Posix ACLs.
+        *
+        * Until that is the case, hard-code inotify here.
+        */
 #ifdef HAVE_INOTIFY
        if (lp_kernel_change_notify(ctx->conn->params)) {
                return inotify_watch(ctx, e, callback, private_data, handle);
@@ -852,29 +928,48 @@ static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
        return NT_STATUS_OK;
 }
 
-static size_t vfswrap_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, uint32 security_info, SEC_DESC **ppdesc)
+static int vfswrap_chflags(vfs_handle_struct *handle, const char *path, int flags)
+{
+#ifdef HAVE_CHFLAGS
+       return chflags(path, flags);
+#else
+       errno = ENOSYS;
+       return -1;
+#endif
+}
+
+static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle, SMB_DEV_T dev, SMB_INO_T inode)
 {
-       size_t result;
+       return file_id_create_dev(dev, inode);
+}
+
+static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
+                                   files_struct *fsp,
+                                   uint32 security_info, SEC_DESC **ppdesc)
+{
+       NTSTATUS result;
 
        START_PROFILE(fget_nt_acl);
-       result = get_nt_acl(fsp, security_info, ppdesc);
+       result = posix_fget_nt_acl(fsp, security_info, ppdesc);
        END_PROFILE(fget_nt_acl);
        return result;
 }
 
-static size_t vfswrap_get_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info, SEC_DESC **ppdesc)
+static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
+                                  const char *name,
+                                  uint32 security_info, SEC_DESC **ppdesc)
 {
-       size_t result;
+       NTSTATUS result;
 
        START_PROFILE(get_nt_acl);
-       result = get_nt_acl(fsp, security_info, ppdesc);
+       result = posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
        END_PROFILE(get_nt_acl);
        return result;
 }
 
-static BOOL vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, int fd, uint32 security_info_sent, SEC_DESC *psd)
+static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, SEC_DESC *psd)
 {
-       BOOL result;
+       NTSTATUS result;
 
        START_PROFILE(fset_nt_acl);
        result = set_nt_acl(fsp, security_info_sent, psd);
@@ -882,9 +977,9 @@ static BOOL vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, in
        return result;
 }
 
-static BOOL vfswrap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, SEC_DESC *psd)
+static NTSTATUS vfswrap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, SEC_DESC *psd)
 {
-       BOOL result;
+       NTSTATUS result;
 
        START_PROFILE(set_nt_acl);
        result = set_nt_acl(fsp, security_info_sent, psd);
@@ -1187,6 +1282,8 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_sendfile),  SMB_VFS_OP_SENDFILE,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_recvfile),  SMB_VFS_OP_RECVFILE,
+        SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_rename),    SMB_VFS_OP_RENAME,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_fsync),     SMB_VFS_OP_FSYNC,
@@ -1207,11 +1304,13 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_fchown),    SMB_VFS_OP_FCHOWN,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_lchown),    SMB_VFS_OP_LCHOWN,
+        SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_chdir),     SMB_VFS_OP_CHDIR,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_getwd),     SMB_VFS_OP_GETWD,
         SMB_VFS_LAYER_OPAQUE},
-       {SMB_VFS_OP(vfswrap_utime),     SMB_VFS_OP_UTIME,
+       {SMB_VFS_OP(vfswrap_ntimes),    SMB_VFS_OP_NTIMES,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_ftruncate), SMB_VFS_OP_FTRUNCATE,
         SMB_VFS_LAYER_OPAQUE},
@@ -1219,6 +1318,8 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_kernel_flock),      SMB_VFS_OP_KERNEL_FLOCK,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_linux_setlease),    SMB_VFS_OP_LINUX_SETLEASE,
+        SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_getlock),   SMB_VFS_OP_GETLOCK,
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_symlink),   SMB_VFS_OP_SYMLINK,
@@ -1233,6 +1334,10 @@ static vfs_op_tuple vfs_default_ops[] = {
         SMB_VFS_LAYER_OPAQUE},
        {SMB_VFS_OP(vfswrap_notify_watch),      SMB_VFS_OP_NOTIFY_WATCH,
         SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_chflags),   SMB_VFS_OP_CHFLAGS,
+        SMB_VFS_LAYER_OPAQUE},
+       {SMB_VFS_OP(vfswrap_file_id_create),    SMB_VFS_OP_FILE_ID_CREATE,
+        SMB_VFS_LAYER_OPAQUE},
 
        /* NT ACL operations. */