r23792: convert Samba4 to GPLv3
[kai/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_search.c
index 69ca6ef997018e6095dc1fe3b148be57e65f1690..cefcee6155bb051e9284a0eee9f17aafb83b6590 100644 (file)
@@ -7,7 +7,7 @@
 
    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,
    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"
 #include "vfs_posix.h"
 #include "system/time.h"
-#include "librpc/gen_ndr/ndr_security.h"
-
-
-/* the state of a search started with pvfs_search_first() */
-struct pvfs_search_state {
-       struct pvfs_state *pvfs;
-       uint16_t handle;
-       uint_t current_index;
-       uint16_t search_attrib;
-       uint16_t must_attrib;
-       struct pvfs_dir *dir;
-       time_t last_used;
-       uint_t num_ea_names;
-       struct ea_name *ea_names;
-};
-
+#include "librpc/gen_ndr/security.h"
+#include "smbd/service_stream.h"
+#include "lib/events/events.h"
+#include "lib/util/dlinklist.h"
 
 /* place a reasonable limit on old-style searches as clients tend to
    not send search close requests */
 #define MAX_OLD_SEARCHES 2000
+#define MAX_SEARCH_HANDLES (UINT16_MAX - 1)
+#define INVALID_SEARCH_HANDLE UINT16_MAX
 
 /*
   destroy an open search
 */
-static int pvfs_search_destructor(void *ptr)
+static int pvfs_search_destructor(struct pvfs_search_state *search)
 {
-       struct pvfs_search_state *search = ptr;
-       idr_remove(search->pvfs->idtree_search, search->handle);
+       DLIST_REMOVE(search->pvfs->search.list, search);
+       idr_remove(search->pvfs->search.idtree, search->handle);
        return 0;
 }
 
+/*
+  called when a search timer goes off
+*/
+static void pvfs_search_timer(struct event_context *ev, struct timed_event *te, 
+                                     struct timeval t, void *ptr)
+{
+       struct pvfs_search_state *search = talloc_get_type(ptr, struct pvfs_search_state);
+       talloc_free(search);
+}
+
+/*
+  setup a timer to destroy a open search after a inactivity period
+*/
+static void pvfs_search_setup_timer(struct pvfs_search_state *search)
+{
+       struct event_context *ev = search->pvfs->ntvfs->ctx->event_ctx;
+       if (search->handle == INVALID_SEARCH_HANDLE) return;
+       talloc_free(search->te);
+       search->te = event_add_timed(ev, search, 
+                                    timeval_current_ofs(search->pvfs->search.inactivity_time, 0), 
+                                    pvfs_search_timer, search);
+}
+
 /*
   fill in a single search result for a given info level
 */
 static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
-                                enum smb_search_level level,
+                                enum smb_search_data_level level,
                                 const char *unix_path,
                                 const char *fname, 
                                 struct pvfs_search_state *search,
-                                uint32_t dir_index,
+                                off_t dir_offset,
                                 union smb_search_data *file)
 {
        struct pvfs_filename *name;
        NTSTATUS status;
        const char *shortname;
+       uint32_t dir_index = (uint32_t)dir_offset; /* truncated - see the code 
+                                                     in pvfs_list_seek_ofs() for 
+                                                     how we cope with this */
 
        status = pvfs_resolve_partial(pvfs, file, unix_path, fname, &name);
        if (!NT_STATUS_IS_OK(status)) {
@@ -80,9 +95,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
        }
 
        switch (level) {
-       case RAW_SEARCH_SEARCH:
-       case RAW_SEARCH_FFIRST:
-       case RAW_SEARCH_FUNIQUE:
+       case RAW_SEARCH_DATA_SEARCH:
                shortname = pvfs_short_name(pvfs, name, name);
                file->search.attrib           = name->dos.attrib;
                file->search.write_time       = nt_time_to_unix(name->dos.write_time);
@@ -97,7 +110,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->search.id.client_cookie = 0;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_STANDARD:
+       case RAW_SEARCH_DATA_STANDARD:
                file->standard.resume_key   = dir_index;
                file->standard.create_time  = nt_time_to_unix(name->dos.create_time);
                file->standard.access_time  = nt_time_to_unix(name->dos.access_time);
@@ -108,7 +121,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->standard.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_EA_SIZE:
+       case RAW_SEARCH_DATA_EA_SIZE:
                file->ea_size.resume_key   = dir_index;
                file->ea_size.create_time  = nt_time_to_unix(name->dos.create_time);
                file->ea_size.access_time  = nt_time_to_unix(name->dos.access_time);
@@ -120,7 +133,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->ea_size.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_EA_LIST:
+       case RAW_SEARCH_DATA_EA_LIST:
                file->ea_list.resume_key   = dir_index;
                file->ea_list.create_time  = nt_time_to_unix(name->dos.create_time);
                file->ea_list.access_time  = nt_time_to_unix(name->dos.access_time);
@@ -134,7 +147,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                                          search->ea_names,
                                          &file->ea_list.eas);
 
-       case RAW_SEARCH_DIRECTORY_INFO:
+       case RAW_SEARCH_DATA_DIRECTORY_INFO:
                file->directory_info.file_index   = dir_index;
                file->directory_info.create_time  = name->dos.create_time;
                file->directory_info.access_time  = name->dos.access_time;
@@ -146,7 +159,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->directory_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_FULL_DIRECTORY_INFO:
+       case RAW_SEARCH_DATA_FULL_DIRECTORY_INFO:
                file->full_directory_info.file_index   = dir_index;
                file->full_directory_info.create_time  = name->dos.create_time;
                file->full_directory_info.access_time  = name->dos.access_time;
@@ -159,12 +172,12 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->full_directory_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_NAME_INFO:
+       case RAW_SEARCH_DATA_NAME_INFO:
                file->name_info.file_index   = dir_index;
                file->name_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_BOTH_DIRECTORY_INFO:
+       case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
                file->both_directory_info.file_index   = dir_index;
                file->both_directory_info.create_time  = name->dos.create_time;
                file->both_directory_info.access_time  = name->dos.access_time;
@@ -178,7 +191,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->both_directory_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_ID_FULL_DIRECTORY_INFO:
+       case RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO:
                file->id_full_directory_info.file_index   = dir_index;
                file->id_full_directory_info.create_time  = name->dos.create_time;
                file->id_full_directory_info.access_time  = name->dos.access_time;
@@ -192,7 +205,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->id_full_directory_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_ID_BOTH_DIRECTORY_INFO:
+       case RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO:
                file->id_both_directory_info.file_index   = dir_index;
                file->id_both_directory_info.create_time  = name->dos.create_time;
                file->id_both_directory_info.access_time  = name->dos.access_time;
@@ -207,7 +220,7 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->id_both_directory_info.name.s       = fname;
                return NT_STATUS_OK;
 
-       case RAW_SEARCH_GENERIC:
+       case RAW_SEARCH_DATA_GENERIC:
                break;
        }
 
@@ -221,10 +234,10 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
 static NTSTATUS pvfs_search_fill(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
                                 uint_t max_count, 
                                 struct pvfs_search_state *search,
-                                enum smb_search_level level,
+                                enum smb_search_data_level level,
                                 uint_t *reply_count,
                                 void *search_private, 
-                                BOOL (*callback)(void *, union smb_search_data *))
+                                BOOL (*callback)(void *, const union smb_search_data *))
 {
        struct pvfs_dir *dir = search->dir;
        NTSTATUS status;
@@ -238,7 +251,7 @@ static NTSTATUS pvfs_search_fill(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
        while ((*reply_count) < max_count) {
                union smb_search_data *file;
                const char *name;
-               uint_t ofs = search->current_index;
+               off_t ofs = search->current_index;
 
                name = pvfs_list_next(dir, &search->current_index);
                if (name == NULL) break;
@@ -266,7 +279,7 @@ static NTSTATUS pvfs_search_fill(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
                talloc_free(file);
        }
 
-       pvfs_list_hibernate(dir);
+       pvfs_search_setup_timer(search);
 
        return NT_STATUS_OK;
 }
@@ -281,7 +294,7 @@ static void pvfs_search_cleanup(struct pvfs_state *pvfs)
        time_t t = time(NULL);
 
        for (i=0;i<MAX_OLD_SEARCHES;i++) {
-               struct pvfs_search_state *search = idr_find(pvfs->idtree_search, i);
+               struct pvfs_search_state *search = idr_find(pvfs->search.idtree, i);
                if (search == NULL) return;
                if (pvfs_list_eos(search->dir, search->current_index) &&
                    search->last_used != 0 &&
@@ -298,9 +311,9 @@ static void pvfs_search_cleanup(struct pvfs_state *pvfs)
    list files in a directory matching a wildcard pattern - old SMBsearch interface
 */
 static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
-                                     struct smbsrv_request *req, union smb_search_first *io, 
+                                     struct ntvfs_request *req, union smb_search_first *io, 
                                      void *search_private, 
-                                     BOOL (*callback)(void *, union smb_search_data *))
+                                     BOOL (*callback)(void *, const union smb_search_data *))
 {
        struct pvfs_dir *dir;
        struct pvfs_state *pvfs = ntvfs->private_data;
@@ -346,10 +359,10 @@ static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
 
        /* we need to give a handle back to the client so it
           can continue a search */
-       id = idr_get_new(pvfs->idtree_search, search, MAX_OLD_SEARCHES);
+       id = idr_get_new(pvfs->search.idtree, search, MAX_OLD_SEARCHES);
        if (id == -1) {
                pvfs_search_cleanup(pvfs);
-               id = idr_get_new(pvfs->idtree_search, search, MAX_OLD_SEARCHES);
+               id = idr_get_new(pvfs->search.idtree, search, MAX_OLD_SEARCHES);
        }
        if (id == -1) {
                return NT_STATUS_INSUFFICIENT_RESOURCES;
@@ -362,10 +375,13 @@ static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
        search->search_attrib = search_attrib & 0xFF;
        search->must_attrib = (search_attrib>>8) & 0xFF;
        search->last_used = time(NULL);
+       search->te = NULL;
+
+       DLIST_ADD(pvfs->search.list, search);
 
        talloc_set_destructor(search, pvfs_search_destructor);
 
-       status = pvfs_search_fill(pvfs, req, io->search_first.in.max_count, search, io->generic.level,
+       status = pvfs_search_fill(pvfs, req, io->search_first.in.max_count, search, io->generic.data_level,
                                  &reply_count, search_private, callback);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -385,9 +401,9 @@ static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
 
 /* continue a old style search */
 static NTSTATUS pvfs_search_next_old(struct ntvfs_module_context *ntvfs,
-                                    struct smbsrv_request *req, union smb_search_next *io, 
+                                    struct ntvfs_request *req, union smb_search_next *io, 
                                     void *search_private, 
-                                    BOOL (*callback)(void *, union smb_search_data *))
+                                    BOOL (*callback)(void *, const union smb_search_data *))
 {
        struct pvfs_state *pvfs = ntvfs->private_data;
        struct pvfs_search_state *search;
@@ -399,22 +415,22 @@ static NTSTATUS pvfs_search_next_old(struct ntvfs_module_context *ntvfs,
        handle    = io->search_next.in.id.handle | (io->search_next.in.id.reserved<<8);
        max_count = io->search_next.in.max_count;
 
-       search = idr_find(pvfs->idtree_search, handle);
+       search = idr_find(pvfs->search.idtree, handle);
        if (search == NULL) {
                /* we didn't find the search handle */
                return NT_STATUS_INVALID_HANDLE;
        }
 
-       search->current_index = io->search_next.in.id.server_cookie;
-       search->last_used = time(NULL);
        dir = search->dir;
 
-       status = pvfs_list_wakeup(dir, &search->current_index);
+       status = pvfs_list_seek_ofs(dir, io->search_next.in.id.server_cookie, 
+                                   &search->current_index);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
+       search->last_used = time(NULL);
 
-       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.data_level,
                                  &reply_count, search_private, callback);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -433,10 +449,10 @@ static NTSTATUS pvfs_search_next_old(struct ntvfs_module_context *ntvfs,
 /* 
    list files in a directory matching a wildcard pattern
 */
-NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
-                          struct smbsrv_request *req, union smb_search_first *io, 
-                          void *search_private, 
-                          BOOL (*callback)(void *, union smb_search_data *))
+static NTSTATUS pvfs_search_first_trans2(struct ntvfs_module_context *ntvfs,
+                                        struct ntvfs_request *req, union smb_search_first *io, 
+                                        void *search_private, 
+                                        BOOL (*callback)(void *, const union smb_search_data *))
 {
        struct pvfs_dir *dir;
        struct pvfs_state *pvfs = ntvfs->private_data;
@@ -448,10 +464,6 @@ NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
        struct pvfs_filename *name;
        int id;
 
-       if (io->generic.level >= RAW_SEARCH_SEARCH) {
-               return pvfs_search_first_old(ntvfs, req, io, search_private, callback);
-       }
-
        search_attrib = io->t2ffirst.in.search_attrib;
        pattern       = io->t2ffirst.in.pattern;
        max_count     = io->t2ffirst.in.max_count;
@@ -485,7 +497,7 @@ NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
                return status;
        }
 
-       id = idr_get_new(pvfs->idtree_search, search, UINT16_MAX);
+       id = idr_get_new(pvfs->search.idtree, search, MAX_SEARCH_HANDLES);
        if (id == -1) {
                return NT_STATUS_INSUFFICIENT_RESOURCES;
        }
@@ -499,10 +511,12 @@ NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
        search->last_used = 0;
        search->num_ea_names = io->t2ffirst.in.num_names;
        search->ea_names = io->t2ffirst.in.ea_names;
+       search->te = NULL;
 
+       DLIST_ADD(pvfs->search.list, search);
        talloc_set_destructor(search, pvfs_search_destructor);
 
-       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.data_level,
                                  &reply_count, search_private, callback);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -531,10 +545,10 @@ NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
 }
 
 /* continue a search */
-NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
-                         struct smbsrv_request *req, union smb_search_next *io, 
-                         void *search_private, 
-                         BOOL (*callback)(void *, union smb_search_data *))
+static NTSTATUS pvfs_search_next_trans2(struct ntvfs_module_context *ntvfs,
+                                       struct ntvfs_request *req, union smb_search_next *io, 
+                                       void *search_private, 
+                                       BOOL (*callback)(void *, const union smb_search_data *))
 {
        struct pvfs_state *pvfs = ntvfs->private_data;
        struct pvfs_search_state *search;
@@ -543,37 +557,29 @@ NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
        uint16_t handle;
        NTSTATUS status;
 
-       if (io->generic.level >= RAW_SEARCH_SEARCH) {
-               return pvfs_search_next_old(ntvfs, req, io, search_private, callback);
-       }
-
        handle = io->t2fnext.in.handle;
 
-       search = idr_find(pvfs->idtree_search, handle);
+       search = idr_find(pvfs->search.idtree, handle);
        if (search == NULL) {
                /* we didn't find the search handle */
                return NT_STATUS_INVALID_HANDLE;
        }
-
+       
        dir = search->dir;
+       
+       status = NT_STATUS_OK;
 
        /* work out what type of continuation is being used */
        if (io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
                status = pvfs_list_seek(dir, io->t2fnext.in.last_name, &search->current_index);
-               if (!NT_STATUS_IS_OK(status)) {
-                       if (io->t2fnext.in.resume_key) {
-                               search->current_index = io->t2fnext.in.resume_key;
-                       } else {
-                               return status;
-                       }
+               if (!NT_STATUS_IS_OK(status) && io->t2fnext.in.resume_key) {
+                       status = pvfs_list_seek_ofs(dir, io->t2fnext.in.resume_key, 
+                                                   &search->current_index);
                }
-       } else if (io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) {
-               /* plain continue - nothing to do */
-       } else {
-               search->current_index = io->t2fnext.in.resume_key;
+       } else if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE)) {
+               status = pvfs_list_seek_ofs(dir, io->t2fnext.in.resume_key, 
+                                           &search->current_index);
        }
-
-       status = pvfs_list_wakeup(dir, &search->current_index);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -581,7 +587,7 @@ NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
        search->num_ea_names = io->t2fnext.in.num_names;
        search->ea_names = io->t2fnext.in.ea_names;
 
-       status = pvfs_search_fill(pvfs, req, io->t2fnext.in.max_count, search, io->generic.level,
+       status = pvfs_search_fill(pvfs, req, io->t2fnext.in.max_count, search, io->generic.data_level,
                                  &reply_count, search_private, callback);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -600,21 +606,230 @@ NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
        return NT_STATUS_OK;
 }
 
+static NTSTATUS pvfs_search_first_smb2(struct ntvfs_module_context *ntvfs,
+                                      struct ntvfs_request *req, const struct smb2_find *io, 
+                                      void *search_private, 
+                                      BOOL (*callback)(void *, const union smb_search_data *))
+{
+       struct pvfs_dir *dir;
+       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_search_state *search;
+       uint_t reply_count;
+       uint16_t max_count;
+       const char *pattern;
+       NTSTATUS status;
+       struct pvfs_filename *name;
+       struct pvfs_file *f;
+
+       f = pvfs_find_fd(pvfs, req, io->in.file.ntvfs);
+       if (!f) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       /* its only valid for directories */
+       if (f->handle->fd != -1) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!(f->access_mask & SEC_DIR_LIST)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (f->search) {
+               talloc_free(f->search);
+               f->search = NULL;
+       }
+
+       if (strequal(io->in.pattern, "")) {
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
+       if (strchr_m(io->in.pattern, '\\')) {
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
+       if (strchr_m(io->in.pattern, '/')) {
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
+
+       if (strequal("", f->handle->name->original_name)) {
+               pattern = talloc_asprintf(req, "\\%s", io->in.pattern);
+               NT_STATUS_HAVE_NO_MEMORY(pattern);
+       } else {
+               pattern = talloc_asprintf(req, "\\%s\\%s",
+                                         f->handle->name->original_name,
+                                         io->in.pattern);
+               NT_STATUS_HAVE_NO_MEMORY(pattern);
+       }
+
+       /* resolve the cifs name to a posix name */
+       status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       if (!name->has_wildcard && !name->exists) {
+               return NT_STATUS_NO_SUCH_FILE;
+       }
+
+       /* we initially make search a child of the request, then if we
+          need to keep it long term we steal it for the private
+          structure */
+       search = talloc(req, struct pvfs_search_state);
+       NT_STATUS_HAVE_NO_MEMORY(search);
+
+       /* do the actual directory listing */
+       status = pvfs_list_start(pvfs, name, search, &dir);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       search->pvfs            = pvfs;
+       search->handle          = INVALID_SEARCH_HANDLE;
+       search->dir             = dir;
+       search->current_index   = 0;
+       search->search_attrib   = 0x0000FFFF;
+       search->must_attrib     = 0;
+       search->last_used       = 0;
+       search->num_ea_names    = 0;
+       search->ea_names        = NULL;
+       search->te              = NULL;
+
+       if (io->in.continue_flags & SMB2_CONTINUE_FLAG_SINGLE) {
+               max_count = 1;
+       } else {
+               max_count = UINT16_MAX;
+       }
+
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->data_level,
+                                 &reply_count, search_private, callback);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       /* not matching any entries is an error */
+       if (reply_count == 0) {
+               return NT_STATUS_NO_SUCH_FILE;
+       }
+
+       f->search = talloc_steal(f, search);
+
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS pvfs_search_next_smb2(struct ntvfs_module_context *ntvfs,
+                                     struct ntvfs_request *req, const struct smb2_find *io, 
+                                     void *search_private, 
+                                     BOOL (*callback)(void *, const union smb_search_data *))
+{
+       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_search_state *search;
+       uint_t reply_count;
+       uint16_t max_count;
+       NTSTATUS status;
+       struct pvfs_file *f;
+
+       f = pvfs_find_fd(pvfs, req, io->in.file.ntvfs);
+       if (!f) {
+               return NT_STATUS_FILE_CLOSED;
+       }
+
+       /* its only valid for directories */
+       if (f->handle->fd != -1) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /* if there's no search started on the dir handle, it's like a search_first */
+       search = f->search;
+       if (!search) {
+               return pvfs_search_first_smb2(ntvfs, req, io, search_private, callback);
+       }
+
+       if (io->in.continue_flags & SMB2_CONTINUE_FLAG_RESTART) {
+               search->current_index = 0;
+       }
+
+       if (io->in.continue_flags & SMB2_CONTINUE_FLAG_SINGLE) {
+               max_count = 1;
+       } else {
+               max_count = UINT16_MAX;
+       }
+
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->data_level,
+                                 &reply_count, search_private, callback);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       /* not matching any entries is an error */
+       if (reply_count == 0) {
+               return STATUS_NO_MORE_FILES;
+       }
+
+       return NT_STATUS_OK;
+}
+
+/* 
+   list files in a directory matching a wildcard pattern
+*/
+NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
+                          struct ntvfs_request *req, union smb_search_first *io, 
+                          void *search_private, 
+                          BOOL (*callback)(void *, const union smb_search_data *))
+{
+       switch (io->generic.level) {
+       case RAW_SEARCH_SEARCH:
+       case RAW_SEARCH_FFIRST:
+       case RAW_SEARCH_FUNIQUE:
+               return pvfs_search_first_old(ntvfs, req, io, search_private, callback);
+
+       case RAW_SEARCH_TRANS2:
+               return pvfs_search_first_trans2(ntvfs, req, io, search_private, callback);
+
+       case RAW_SEARCH_SMB2:
+               return pvfs_search_first_smb2(ntvfs, req, &io->smb2, search_private, callback);
+       }
+
+       return NT_STATUS_INVALID_LEVEL;
+}
+
+/* continue a search */
+NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
+                         struct ntvfs_request *req, union smb_search_next *io, 
+                         void *search_private, 
+                         BOOL (*callback)(void *, const union smb_search_data *))
+{
+       switch (io->generic.level) {
+       case RAW_SEARCH_SEARCH:
+       case RAW_SEARCH_FFIRST:
+               return pvfs_search_next_old(ntvfs, req, io, search_private, callback);
+
+       case RAW_SEARCH_FUNIQUE:
+               return NT_STATUS_INVALID_LEVEL;
+
+       case RAW_SEARCH_TRANS2:
+               return pvfs_search_next_trans2(ntvfs, req, io, search_private, callback);
+
+       case RAW_SEARCH_SMB2:
+               return pvfs_search_next_smb2(ntvfs, req, &io->smb2, search_private, callback);
+       }
+
+       return NT_STATUS_INVALID_LEVEL;
+}
+
+
 /* close a search */
 NTSTATUS pvfs_search_close(struct ntvfs_module_context *ntvfs,
-                          struct smbsrv_request *req, union smb_search_close *io)
+                          struct ntvfs_request *req, union smb_search_close *io)
 {
        struct pvfs_state *pvfs = ntvfs->private_data;
        struct pvfs_search_state *search;
-       uint16_t handle;
+       uint16_t handle = INVALID_SEARCH_HANDLE;
 
-       if (io->generic.level == RAW_FINDCLOSE_FCLOSE) {
+       switch (io->generic.level) {
+       case RAW_FINDCLOSE_GENERIC:
+               return NT_STATUS_INVALID_LEVEL;
+
+       case RAW_FINDCLOSE_FCLOSE:
                handle = io->fclose.in.id.handle;
-       } else {
+               break;
+
+       case RAW_FINDCLOSE_FINDCLOSE:
                handle = io->findclose.in.handle;
+               break;
        }
 
-       search = idr_find(pvfs->idtree_search, handle);
+       search = idr_find(pvfs->search.idtree, handle);
        if (search == NULL) {
                /* we didn't find the search handle */
                return NT_STATUS_INVALID_HANDLE;