Add fdopendir to the VFS. We will use this to reuse a directory fd already open by...
authorJeremy Allison <jra@samba.org>
Tue, 8 Feb 2011 23:07:48 +0000 (15:07 -0800)
committerJeremy Allison <jra@samba.org>
Tue, 8 Feb 2011 23:55:22 +0000 (00:55 +0100)
Autobuild-User: Jeremy Allison <jra@samba.org>
Autobuild-Date: Wed Feb  9 00:55:22 CET 2011 on sn-devel-104

14 files changed:
examples/VFS/skel_opaque.c
examples/VFS/skel_transparent.c
source3/include/smbprofile.h
source3/include/vfs.h
source3/include/vfs_macros.h
source3/modules/vfs_default.c
source3/modules/vfs_dirsort.c
source3/modules/vfs_full_audit.c
source3/modules/vfs_netatalk.c
source3/modules/vfs_scannedonly.c
source3/modules/vfs_shadow_copy.c
source3/modules/vfs_time_audit.c
source3/smbd/vfs.c
source3/utils/status_profile.c

index e506a2060d0faee76d9b3eadfa13f3beb8d45b96..da126e3c0aca2c87c6061f25f42cb5ebe1f64d26 100644 (file)
@@ -87,6 +87,11 @@ static SMB_STRUCT_DIR *skel_opendir(vfs_handle_struct *handle,  const char *fnam
        return NULL;
 }
 
+static SMB_STRUCT_DIR *skel_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
+{
+       return NULL;
+}
+
 static SMB_STRUCT_DIRENT *skel_readdir(vfs_handle_struct *handle,
                                       SMB_STRUCT_DIR *dirp,
                                       SMB_STRUCT_STAT *sbuf)
@@ -784,6 +789,7 @@ struct vfs_fn_pointers skel_transparent_fns = {
        /* Directory operations */
 
        .opendir = skel_opendir,
+       .fdopendir = skel_fdopendir,
        .readdir = skel_readdir,
        .seekdir = skel_seekdir,
        .telldir = skel_telldir,
index 3ab3b63eafb052d633362a7e52656d65e85d9533..0ffdc11b2114ac88c97249c989521ca76454afcc 100644 (file)
@@ -84,6 +84,11 @@ static SMB_STRUCT_DIR *skel_opendir(vfs_handle_struct *handle,  const char *fnam
        return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
 }
 
+static SMB_STRUCT_DIR *skel_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
+{
+       return SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+}
+
 static SMB_STRUCT_DIRENT *skel_readdir(vfs_handle_struct *handle,
                                       SMB_STRUCT_DIR *dirp,
                                       SMB_STRUCT_STAT *sbuf)
@@ -728,6 +733,7 @@ struct vfs_fn_pointers skel_transparent_fns = {
        /* Directory operations */
 
        .opendir = skel_opendir,
+       .fdopendir = skel_fdopendir,
        .readdir = skel_readdir,
        .seekdir = skel_seekdir,
        .telldir = skel_telldir,
index ff8ab90ce1a8b0f3a0ce6c604efdd0415ef11bb8..3041f6c87ec03146abfa6d0dd00262957c5c315a 100644 (file)
@@ -43,6 +43,10 @@ enum profile_stats_values
 #define syscall_opendir_count __profile_stats_value(PR_VALUE_SYSCALL_OPENDIR, count)
 #define syscall_opendir_time __profile_stats_value(PR_VALUE_SYSCALL_OPENDIR, time)
 
+       PR_VALUE_SYSCALL_FDOPENDIR,
+#define syscall_fdopendir_count __profile_stats_value(PR_VALUE_SYSCALL_FDOPENDIR, count)
+#define syscall_fdopendir_time __profile_stats_value(PR_VALUE_SYSCALL_FDOPENDIR, time)
+
        PR_VALUE_SYSCALL_READDIR,
 #define syscall_readdir_count __profile_stats_value(PR_VALUE_SYSCALL_READDIR, count)
 #define syscall_readdir_time __profile_stats_value(PR_VALUE_SYSCALL_READDIR, time)
index 72d61e330f6a410e0a1276346bc1bc8baabc2a14..45193d2b5492cbd5a1ead13f8dfb4f858cbfa304 100644 (file)
                where it belongs. JRA. */
 /* Leave at 28 - not yet released. Rename posix_fallocate to fallocate
                to split out the two possible uses. JRA. */
+/* Leave at 28 - not yet released. Add fdopendir. JRA. */
 #define SMB_VFS_INTERFACE_VERSION 28
 
 
@@ -198,6 +199,7 @@ struct vfs_fn_pointers {
        /* Directory operations */
 
        SMB_STRUCT_DIR *(*opendir)(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attributes);
+       SMB_STRUCT_DIR *(*fdopendir)(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attributes);
        SMB_STRUCT_DIRENT *(*readdir)(struct vfs_handle_struct *handle,
                                      SMB_STRUCT_DIR *dirp,
                                      SMB_STRUCT_STAT *sbuf);
@@ -526,6 +528,10 @@ uint32_t smb_vfs_call_fs_capabilities(struct vfs_handle_struct *handle,
 SMB_STRUCT_DIR *smb_vfs_call_opendir(struct vfs_handle_struct *handle,
                                     const char *fname, const char *mask,
                                     uint32 attributes);
+SMB_STRUCT_DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
+                                       struct files_struct *fsp,
+                                       const char *mask,
+                                       uint32 attributes);
 SMB_STRUCT_DIRENT *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
                                        SMB_STRUCT_DIR *dirp,
                                        SMB_STRUCT_STAT *sbuf);
index bb6428ead5aa05ed05243fe0c33b69179f44b8e0..c0fbe3860d056cd248b5ed0285e262b4a832f174 100644 (file)
 #define SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr) \
        smb_vfs_call_opendir((handle)->next, (fname), (mask), (attr))
 
+#define SMB_VFS_FDOPENDIR(conn, fsp, mask, attr) \
+       smb_vfs_call_fdopendir((conn)->vfs_handles, (fsp), (mask), (attr))
+#define SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr) \
+       smb_vfs_call_fdopendir((handle)->next, (fsp), (mask), (attr))
+
 #define SMB_VFS_READDIR(conn, dirp, sbuf) \
        smb_vfs_call_readdir((conn)->vfs_handles, (dirp), (sbuf))
 #define SMB_VFS_NEXT_READDIR(handle, dirp, sbuf) \
index 698e745d5904dc989748fd829914324825acedd3..eb5a2a4b0d749147713db1fc6475fdf210e3535a 100644 (file)
@@ -167,6 +167,20 @@ static SMB_STRUCT_DIR *vfswrap_opendir(vfs_handle_struct *handle,  const char *f
        return result;
 }
 
+static SMB_STRUCT_DIR *vfswrap_fdopendir(vfs_handle_struct *handle,
+                       files_struct *fsp,
+                       const char *mask,
+                       uint32 attr)
+{
+       SMB_STRUCT_DIR *result;
+
+       START_PROFILE(syscall_fdopendir);
+       result = sys_fdopendir(fsp->fh->fd);
+       END_PROFILE(syscall_fdopendir);
+       return result;
+}
+
+
 static SMB_STRUCT_DIRENT *vfswrap_readdir(vfs_handle_struct *handle,
                                          SMB_STRUCT_DIR *dirp,
                                          SMB_STRUCT_STAT *sbuf)
index 4869bc05a09a1491538d13780e9115b87c10225f..82a3a4bb98f0e1c43360aef5f50e6c218377b374 100644 (file)
@@ -122,6 +122,48 @@ static SMB_STRUCT_DIR *dirsort_opendir(vfs_handle_struct *handle,
        return data->source_directory;
 }
 
+static SMB_STRUCT_DIR *dirsort_fdopendir(vfs_handle_struct *handle,
+                                       files_struct *fsp,
+                                       const char *mask,
+                                       uint32 attr)
+{
+       struct dirsort_privates *data = NULL;
+
+       /* set up our private data about this directory */
+       data = (struct dirsort_privates *)SMB_MALLOC(
+               sizeof(struct dirsort_privates));
+
+       if (!data) {
+               return NULL;
+       }
+
+       data->directory_list = NULL;
+       data->pos = 0;
+
+       /* Open the underlying directory and count the number of entries */
+       data->source_directory = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask,
+                                                     attr);
+
+       if (data->source_directory == NULL) {
+               SAFE_FREE(data);
+               return NULL;
+       }
+
+       data->fd = dirfd(data->source_directory);
+
+       SMB_VFS_HANDLE_SET_DATA(handle, data, free_dirsort_privates,
+                               struct dirsort_privates, return NULL);
+
+       if (!open_and_sort_dir(handle)) {
+               SMB_VFS_NEXT_CLOSEDIR(handle,data->source_directory);
+               /* fd is now closed. */
+               fsp->fh->fd = -1;
+               return NULL;
+       }
+
+       return data->source_directory;
+}
+
 static SMB_STRUCT_DIRENT *dirsort_readdir(vfs_handle_struct *handle,
                                          SMB_STRUCT_DIR *dirp,
                                          SMB_STRUCT_STAT *sbuf)
@@ -179,6 +221,7 @@ static void dirsort_rewinddir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
 
 static struct vfs_fn_pointers vfs_dirsort_fns = {
        .opendir = dirsort_opendir,
+       .fdopendir = dirsort_fdopendir,
        .readdir = dirsort_readdir,
        .seekdir = dirsort_seekdir,
        .telldir = dirsort_telldir,
index 10422fca791b934177be76ed458d040f597d7d3f..a81f3b58701bf1cac30a6469c1333f8c4438bc0c 100644 (file)
@@ -87,6 +87,7 @@ typedef enum _vfs_op_type {
        /* Directory operations */
 
        SMB_VFS_OP_OPENDIR,
+       SMB_VFS_OP_FDOPENDIR,
        SMB_VFS_OP_READDIR,
        SMB_VFS_OP_SEEKDIR,
        SMB_VFS_OP_TELLDIR,
@@ -229,6 +230,7 @@ static struct {
        { SMB_VFS_OP_STATVFS,   "statvfs" },
        { SMB_VFS_OP_FS_CAPABILITIES,   "fs_capabilities" },
        { SMB_VFS_OP_OPENDIR,   "opendir" },
+       { SMB_VFS_OP_FDOPENDIR, "fdopendir" },
        { SMB_VFS_OP_READDIR,   "readdir" },
        { SMB_VFS_OP_SEEKDIR,   "seekdir" },
        { SMB_VFS_OP_TELLDIR,   "telldir" },
@@ -735,6 +737,19 @@ static SMB_STRUCT_DIR *smb_full_audit_opendir(vfs_handle_struct *handle,
        return result;
 }
 
+static SMB_STRUCT_DIR *smb_full_audit_fdopendir(vfs_handle_struct *handle,
+                         files_struct *fsp, const char *mask, uint32 attr)
+{
+       SMB_STRUCT_DIR *result;
+
+       result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+
+       do_log(SMB_VFS_OP_FDOPENDIR, (result != NULL), handle, "%s",
+                       fsp_str_do_log(fsp));
+
+       return result;
+}
+
 static SMB_STRUCT_DIRENT *smb_full_audit_readdir(vfs_handle_struct *handle,
                                    SMB_STRUCT_DIR *dirp, SMB_STRUCT_STAT *sbuf)
 {
@@ -2201,6 +2216,7 @@ static struct vfs_fn_pointers vfs_full_audit_fns = {
        .statvfs = smb_full_audit_statvfs,
        .fs_capabilities = smb_full_audit_fs_capabilities,
        .opendir = smb_full_audit_opendir,
+       .fdopendir = smb_full_audit_fdopendir,
        .readdir = smb_full_audit_readdir,
        .seekdir = smb_full_audit_seekdir,
        .telldir = smb_full_audit_telldir,
index d5a85b33903c1b7705a92bef8bb722775e917830..734c603618f40a58bf72b3ed384a5e4508da5b2a 100644 (file)
@@ -198,6 +198,31 @@ static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const cha
        return ret;
 }
 
+static SMB_STRUCT_DIR *atalk_fdopendir(struct vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
+{
+       SMB_STRUCT_DIR *ret = 0;
+
+       ret = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+
+       if (ret == NULL) {
+               return ret;
+       }
+
+       /*
+        * when we try to perform delete operation upon file which has fork
+        * in ./.AppleDouble and this directory wasn't hidden by Samba,
+        * MS Windows explorer causes the error: "Cannot find the specified file"
+        * There is some workaround to avoid this situation, i.e. if
+        * connection has not .AppleDouble entry in either veto or hide 
+        * list then it would be nice to add one.
+        */
+
+       atalk_add_to_list(&handle->conn->hide_list);
+       atalk_add_to_list(&handle->conn->veto_list);
+
+       return ret;
+}
+
 static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
 {
        bool add = False;
@@ -432,6 +457,7 @@ exit_lchown:
 
 static struct vfs_fn_pointers vfs_netatalk_fns = {
        .opendir = atalk_opendir,
+       .fdopendir = atalk_fdopendir,
        .rmdir = atalk_rmdir,
        .rename = atalk_rename,
        .unlink = atalk_unlink,
index 123bf65fd4b14e2115117a3196853b417ec4ae43..fd6c3e2077bd239feb54ed11f05246e105ba1fec 100644 (file)
@@ -526,6 +526,35 @@ static SMB_STRUCT_DIR *scannedonly_opendir(vfs_handle_struct * handle,
        return (SMB_STRUCT_DIR *) sDIR;
 }
 
+static SMB_STRUCT_DIR *scannedonly_fdopendir(vfs_handle_struct * handle,
+                                          files_struct *fsp,
+                                          const char *mask, uint32 attr)
+{
+       SMB_STRUCT_DIR *DIRp;
+       struct scannedonly_DIR *sDIR;
+       const char *fname;
+
+       DIRp = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+       if (!DIRp) {
+               return NULL;
+       }
+
+       fname = (const char *)fsp->fsp_name->base_name;
+
+       sDIR = TALLOC_P(NULL, struct scannedonly_DIR);
+       if (fname[0] != '/') {
+               sDIR->base = construct_full_path(sDIR,handle, fname, true);
+       } else {
+               sDIR->base = name_w_ending_slash(sDIR, fname);
+       }
+       DEBUG(SCANNEDONLY_DEBUG,
+                       ("scannedonly_fdopendir, fname=%s, base=%s\n",fname,sDIR->base));
+       sDIR->DIR = DIRp;
+       sDIR->notify_loop_done = 0;
+       return (SMB_STRUCT_DIR *) sDIR;
+}
+
+
 static SMB_STRUCT_DIRENT *scannedonly_readdir(vfs_handle_struct *handle,
                                              SMB_STRUCT_DIR * dirp,
                                              SMB_STRUCT_STAT *sbuf)
@@ -986,6 +1015,7 @@ static int scannedonly_connect(struct vfs_handle_struct *handle,
 /* VFS operations structure */
 static struct vfs_fn_pointers vfs_scannedonly_fns = {
        .opendir = scannedonly_opendir,
+       .fdopendir = scannedonly_fdopendir,
        .readdir = scannedonly_readdir,
        .seekdir = scannedonly_seekdir,
        .telldir = scannedonly_telldir,
index 6bad1686d9a03cb53fadd933948767663a08edfd..b597644869b70d4547e679354e69f350f193ecc4 100644 (file)
@@ -118,6 +118,58 @@ static SMB_STRUCT_DIR *shadow_copy_opendir(vfs_handle_struct *handle, const char
        return((SMB_STRUCT_DIR *)dirp);
 }
 
+static SMB_STRUCT_DIR *shadow_copy_fdopendir(vfs_handle_struct *handle, files_struct *fsp, const char *mask, uint32 attr)
+{
+       shadow_copy_Dir *dirp;
+       SMB_STRUCT_DIR *p = SMB_VFS_NEXT_FDOPENDIR(handle,fsp,mask,attr);
+
+       if (!p) {
+               DEBUG(10,("shadow_copy_opendir: SMB_VFS_NEXT_FDOPENDIR() failed for [%s]\n",
+                       smb_fname_str_dbg(fsp->fsp_name)));
+               return NULL;
+       }
+
+       dirp = SMB_MALLOC_P(shadow_copy_Dir);
+       if (!dirp) {
+               DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
+               SMB_VFS_NEXT_CLOSEDIR(handle,p);
+               /* We have now closed the fd in fsp. */
+               fsp->fh->fd = -1;
+               return NULL;
+       }
+
+       ZERO_STRUCTP(dirp);
+
+       while (True) {
+               SMB_STRUCT_DIRENT *d;
+
+               d = SMB_VFS_NEXT_READDIR(handle, p, NULL);
+               if (d == NULL) {
+                       break;
+               }
+
+               if (shadow_copy_match_name(d->d_name)) {
+                       DEBUG(8,("shadow_copy_fdopendir: hide [%s]\n",d->d_name));
+                       continue;
+               }
+
+               DEBUG(10,("shadow_copy_fdopendir: not hide [%s]\n",d->d_name));
+
+               dirp->dirs = SMB_REALLOC_ARRAY(dirp->dirs,SMB_STRUCT_DIRENT, dirp->num+1);
+               if (!dirp->dirs) {
+                       DEBUG(0,("shadow_copy_fdopendir: Out of memory\n"));
+                       break;
+               }
+
+               dirp->dirs[dirp->num++] = *d;
+       }
+
+       SMB_VFS_NEXT_CLOSEDIR(handle,p);
+       /* We have now closed the fd in fsp. */
+       fsp->fh->fd = -1;
+       return((SMB_STRUCT_DIR *)dirp);
+}
+
 static SMB_STRUCT_DIRENT *shadow_copy_readdir(vfs_handle_struct *handle,
                                              SMB_STRUCT_DIR *_dirp,
                                              SMB_STRUCT_STAT *sbuf)
@@ -216,6 +268,7 @@ static int shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle, files_str
 
 static struct vfs_fn_pointers vfs_shadow_copy_fns = {
        .opendir = shadow_copy_opendir,
+       .fdopendir = shadow_copy_fdopendir,
        .readdir = shadow_copy_readdir,
        .seekdir = shadow_copy_seekdir,
        .telldir = shadow_copy_telldir,
index 136807f067cc6919a0e1131c2ddad11378509eb9..d692529a345554ebdd38ced5fd386bf61a9fa95e 100644 (file)
@@ -221,6 +221,26 @@ static SMB_STRUCT_DIR *smb_time_audit_opendir(vfs_handle_struct *handle,
        return result;
 }
 
+static SMB_STRUCT_DIR *smb_time_audit_fdopendir(vfs_handle_struct *handle,
+                                             files_struct *fsp,
+                                             const char *mask, uint32 attr)
+{
+       SMB_STRUCT_DIR *result;
+       struct timespec ts1,ts2;
+       double timediff;
+
+       clock_gettime_mono(&ts1);
+       result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
+       clock_gettime_mono(&ts2);
+       timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
+
+       if (timediff > audit_timeout) {
+               smb_time_audit_log("fdopendir", timediff);
+       }
+
+       return result;
+}
+
 static SMB_STRUCT_DIRENT *smb_time_audit_readdir(vfs_handle_struct *handle,
                                                 SMB_STRUCT_DIR *dirp,
                                                 SMB_STRUCT_STAT *sbuf)
@@ -2324,6 +2344,7 @@ static struct vfs_fn_pointers vfs_time_audit_fns = {
        .statvfs = smb_time_audit_statvfs,
        .fs_capabilities = smb_time_audit_fs_capabilities,
        .opendir = smb_time_audit_opendir,
+       .fdopendir = smb_time_audit_fdopendir,
        .readdir = smb_time_audit_readdir,
        .seekdir = smb_time_audit_seekdir,
        .telldir = smb_time_audit_telldir,
index 32be699ef86e83cf95671ad80faab76e02903cdd..40aaf6bb742d55215001691310b9b902e39cea82 100644 (file)
@@ -1200,6 +1200,15 @@ SMB_STRUCT_DIR *smb_vfs_call_opendir(struct vfs_handle_struct *handle,
        return handle->fns->opendir(handle, fname, mask, attributes);
 }
 
+SMB_STRUCT_DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
+                                       struct files_struct *fsp,
+                                       const char *mask,
+                                       uint32 attributes)
+{
+       VFS_FIND(fdopendir);
+       return handle->fns->fdopendir(handle, fsp, mask, attributes);
+}
+
 SMB_STRUCT_DIRENT *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
                                              SMB_STRUCT_DIR *dirp,
                                              SMB_STRUCT_STAT *sbuf)
index ec0ea03806668cf817bdc4a7d062c39278eaa873..198dac69a19c0fa76726f412f48703e2d3e4a28a 100644 (file)
@@ -57,6 +57,8 @@ bool status_profile_dump(bool verbose)
        profile_separator("System Calls");
        d_printf("opendir_count:                  %u\n", profile_p->syscall_opendir_count);
        d_printf("opendir_time:                   %u\n", profile_p->syscall_opendir_time);
+       d_printf("fdopendir_count:                %u\n", profile_p->syscall_fdopendir_count);
+       d_printf("fdopendir_time:                 %u\n", profile_p->syscall_fdopendir_time);
        d_printf("readdir_count:                  %u\n", profile_p->syscall_readdir_count);
        d_printf("readdir_time:                   %u\n", profile_p->syscall_readdir_time);
        d_printf("mkdir_count:                    %u\n", profile_p->syscall_mkdir_count);