r4067: no matches in findnext is not an error
[samba.git] / source / ntvfs / posix / pvfs_search.c
index 3004e92d51ecd1c504e48e8bebd104cac945e2b3..4ee81503c0a991c7412b1544792b8a5558450bb2 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#include "include/includes.h"
+#include "includes.h"
 #include "vfs_posix.h"
+#include "system/time.h"
+#include "system/filesys.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;
+};
+
+
+/* place a reasonable limit on old-style searches as clients tend to
+   not send search close requests */
+#define MAX_OLD_SEARCHES 2000
+
+/*
+  destroy an open search
+*/
+static int pvfs_search_destructor(void *ptr)
+{
+       struct pvfs_search_state *search = ptr;
+       idr_remove(search->pvfs->idtree_search, search->handle);
+       return 0;
+}
 
 /*
   fill in a single search result for a given info level
@@ -30,19 +59,94 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                                 enum smb_search_level level,
                                 const char *unix_path,
                                 const char *fname, 
-                                uint16_t search_attrib,
+                                struct pvfs_search_state *search,
                                 uint32_t dir_index,
                                 union smb_search_data *file)
 {
        struct pvfs_filename *name;
        NTSTATUS status;
+       const char *shortname;
 
        status = pvfs_resolve_partial(pvfs, file, unix_path, fname, &name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
+       status = pvfs_match_attrib(pvfs, name, search->search_attrib, search->must_attrib);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
        switch (level) {
+       case RAW_SEARCH_SEARCH:
+       case RAW_SEARCH_FFIRST:
+       case RAW_SEARCH_FUNIQUE:
+               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);
+               file->search.size             = name->st.st_size;
+               file->search.name             = shortname;
+               file->search.id.reserved      = search->handle >> 8;
+               memset(file->search.id.name, ' ', sizeof(file->search.id.name));
+               memcpy(file->search.id.name, shortname, 
+                      MIN(strlen(shortname)+1, sizeof(file->search.id.name)));
+               file->search.id.handle        = search->handle & 0xFF;
+               file->search.id.server_cookie = dir_index;
+               file->search.id.client_cookie = 0;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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);
+               file->standard.write_time   = nt_time_to_unix(name->dos.write_time);
+               file->standard.size         = name->st.st_size;
+               file->standard.alloc_size   = name->dos.alloc_size;
+               file->standard.attrib       = name->dos.attrib;
+               file->standard.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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);
+               file->ea_size.write_time   = nt_time_to_unix(name->dos.write_time);
+               file->ea_size.size         = name->st.st_size;
+               file->ea_size.alloc_size   = name->dos.alloc_size;
+               file->ea_size.attrib       = name->dos.attrib;
+               file->ea_size.ea_size      = name->dos.ea_size;
+               file->ea_size.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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;
+               file->directory_info.write_time   = name->dos.write_time;
+               file->directory_info.change_time  = name->dos.change_time;
+               file->directory_info.size         = name->st.st_size;
+               file->directory_info.alloc_size   = name->dos.alloc_size;
+               file->directory_info.attrib       = name->dos.attrib;
+               file->directory_info.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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;
+               file->full_directory_info.write_time   = name->dos.write_time;
+               file->full_directory_info.change_time  = name->dos.change_time;
+               file->full_directory_info.size         = name->st.st_size;
+               file->full_directory_info.alloc_size   = name->dos.alloc_size;
+               file->full_directory_info.attrib       = name->dos.attrib;
+               file->full_directory_info.ea_size      = name->dos.ea_size;
+               file->full_directory_info.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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:
                file->both_directory_info.file_index   = dir_index;
@@ -54,33 +158,253 @@ static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
                file->both_directory_info.alloc_size   = name->dos.alloc_size;
                file->both_directory_info.attrib       = name->dos.attrib;
                file->both_directory_info.ea_size      = name->dos.ea_size;
-               file->both_directory_info.short_name.s = pvfs_short_name(pvfs, name);
+               file->both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
                file->both_directory_info.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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;
+               file->id_full_directory_info.write_time   = name->dos.write_time;
+               file->id_full_directory_info.change_time  = name->dos.change_time;
+               file->id_full_directory_info.size         = name->st.st_size;
+               file->id_full_directory_info.alloc_size   = name->dos.alloc_size;
+               file->id_full_directory_info.attrib       = name->dos.attrib;
+               file->id_full_directory_info.ea_size      = name->dos.ea_size;
+               file->id_full_directory_info.file_id      = name->dos.file_id;
+               file->id_full_directory_info.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_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;
+               file->id_both_directory_info.write_time   = name->dos.write_time;
+               file->id_both_directory_info.change_time  = name->dos.change_time;
+               file->id_both_directory_info.size         = name->st.st_size;
+               file->id_both_directory_info.alloc_size   = name->dos.alloc_size;
+               file->id_both_directory_info.attrib       = name->dos.attrib;
+               file->id_both_directory_info.ea_size      = name->dos.ea_size;
+               file->id_both_directory_info.file_id      = name->dos.file_id;
+               file->id_both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
+               file->id_both_directory_info.name.s       = fname;
+               return NT_STATUS_OK;
+
+       case RAW_SEARCH_GENERIC:
                break;
        }
 
+       return NT_STATUS_INVALID_LEVEL;
+}
+
+
+/*
+  the search fill loop
+*/
+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,
+                                uint_t *reply_count,
+                                void *search_private, 
+                                BOOL (*callback)(void *, union smb_search_data *))
+{
+       struct pvfs_dir *dir = search->dir;
+       NTSTATUS status;
+
+       *reply_count = 0;
+
+       if (max_count == 0) {
+               max_count = 1;
+       }
+
+       while ((*reply_count) < max_count) {
+               union smb_search_data *file;
+               const char *name;
+               uint_t ofs = search->current_index;
+
+               name = pvfs_list_next(dir, &search->current_index);
+               if (name == NULL) break;
+
+               file = talloc_p(mem_ctx, union smb_search_data);
+               if (!file) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               status = fill_search_info(pvfs, level, 
+                                         pvfs_list_unix_path(dir), name, 
+                                         search, search->current_index, file);
+               if (!NT_STATUS_IS_OK(status)) {
+                       talloc_free(file);
+                       continue;
+               }
+
+               if (!callback(search_private, file)) {
+                       talloc_free(file);
+                       search->current_index = ofs;
+                       break;
+               }
+
+               (*reply_count)++;
+               talloc_free(file);
+       }
+
+       pvfs_list_hibernate(dir);
+
        return NT_STATUS_OK;
 }
 
 /*
-  return the next available search handle
+  we've run out of search handles - cleanup those that the client forgot
+  to close
 */
-static NTSTATUS pvfs_next_search_handle(struct pvfs_state *pvfs, uint16_t *handle)
+static void pvfs_search_cleanup(struct pvfs_state *pvfs)
 {
+       int i;
+       time_t t = time(NULL);
+
+       for (i=0;i<MAX_OLD_SEARCHES;i++) {
+               struct pvfs_search_state *search = idr_find(pvfs->idtree_search, i);
+               if (search == NULL) return;
+               if (pvfs_list_eos(search->dir, search->current_index) &&
+                   search->last_used != 0 &&
+                   t > search->last_used + 30) {
+                       /* its almost certainly been forgotten
+                        about */
+                       talloc_free(search);
+               }
+       }
+}
+
+
+/* 
+   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, 
+                                     void *search_private, 
+                                     BOOL (*callback)(void *, 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 search_attrib;
+       const char *pattern;
+       NTSTATUS status;
+       struct pvfs_filename *name;
+       int id;
+
+       search_attrib = io->search_first.in.search_attrib;
+       pattern       = io->search_first.in.pattern;
+
+       /* resolve the cifs name to a posix name */
+       status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (!name->has_wildcard && !name->exists) {
+               return STATUS_NO_MORE_FILES;
+       }
+
+       /* 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_p(req, struct pvfs_search_state);
+       if (!search) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* do the actual directory listing */
+       status = pvfs_list_start(pvfs, name, search, &dir);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if (pvfs->search.num_active_searches >= 0x10000) {
+       /* 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);
+       if (id == -1) {
+               pvfs_search_cleanup(pvfs);
+               id = idr_get_new(pvfs->idtree_search, search, MAX_OLD_SEARCHES);
+       }
+       if (id == -1) {
                return NT_STATUS_INSUFFICIENT_RESOURCES;
        }
 
-       (*handle) = pvfs->search.next_search_handle;
-       for (search=pvfs->search.open_searches;search;search=search->next) {
-               if (*handle == search->handle) {
-                       *handle = ((*handle)+1) & 0xFFFF;
-                       continue;
-               } 
+       search->pvfs = pvfs;
+       search->handle = id;
+       search->dir = dir;
+       search->current_index = 0;
+       search->search_attrib = search_attrib & 0xFF;
+       search->must_attrib = (search_attrib>>8) & 0xFF;
+       search->last_used = time(NULL);
+
+       talloc_set_destructor(search, pvfs_search_destructor);
+
+       status = pvfs_search_fill(pvfs, req, io->search_first.in.max_count, search, io->generic.level,
+                                 &reply_count, search_private, callback);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       io->search_first.out.count = reply_count;
+
+       /* not matching any entries is an error */
+       if (reply_count == 0) {
+               return STATUS_NO_MORE_FILES;
+       }
+
+       talloc_steal(pvfs, search);
+
+       return NT_STATUS_OK;
+}
+
+/* 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, 
+                                    void *search_private, 
+                                    BOOL (*callback)(void *, union smb_search_data *))
+{
+       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_search_state *search;
+       struct pvfs_dir *dir;
+       uint_t reply_count, max_count;
+       uint16_t handle;
+       NTSTATUS status;
+
+       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);
+       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);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
+                                 &reply_count, search_private, callback);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       io->search_next.out.count = reply_count;
+
+       /* not matching any entries means end of search */
+       if (reply_count == 0) {
+               talloc_free(search);
        }
-       pvfs->search.next_search_handle = ((*handle)+1) & 0xFFFF;
 
        return NT_STATUS_OK;
 }
@@ -88,38 +412,37 @@ static NTSTATUS pvfs_next_search_handle(struct pvfs_state *pvfs, uint16_t *handl
 /* 
    list files in a directory matching a wildcard pattern
 */
-NTSTATUS pvfs_search_first(struct smbsrv_request *req, union smb_search_first *io, 
+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 *))
 {
        struct pvfs_dir *dir;
-       struct pvfs_state *pvfs = req->tcon->ntvfs_private;
+       struct pvfs_state *pvfs = ntvfs->private_data;
        struct pvfs_search_state *search;
-       uint16_t max_count, reply_count;
-       uint16_t search_attrib;
+       uint_t reply_count;
+       uint16_t search_attrib, max_count;
        const char *pattern;
-       int i;
        NTSTATUS status;
        struct pvfs_filename *name;
+       int id;
 
-       if (io->generic.level == RAW_SEARCH_SEARCH) {
-               max_count     = io->search_first.in.max_count;
-               search_attrib = io->search_first.in.search_attrib;
-               pattern       = io->search_first.in.pattern;
-       } else {
-               max_count     = io->t2ffirst.in.max_count;
-               search_attrib = io->t2ffirst.in.search_attrib;
-               pattern       = io->t2ffirst.in.pattern;
+       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;
+
        /* resolve the cifs name to a posix name */
-       status = pvfs_resolve_name(pvfs, req, pattern, 0, &name);
+       status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        if (!name->has_wildcard && !name->exists) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               return NT_STATUS_NO_SUCH_FILE;
        }
 
        /* we initially make search a child of the request, then if we
@@ -130,184 +453,140 @@ NTSTATUS pvfs_search_first(struct smbsrv_request *req, union smb_search_first *i
                return NT_STATUS_NO_MEMORY;
        }
 
-       dir = talloc_p(search, struct pvfs_dir);
-       if (!dir) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        /* do the actual directory listing */
-       status = pvfs_list(pvfs, name, dir);
+       status = pvfs_list_start(pvfs, name, search, &dir);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       /* we need to give a handle back to the client so it
-          can continue a search */
-       status = pvfs_next_search_handle(pvfs, &search->handle);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       id = idr_get_new(pvfs->idtree_search, search, UINT16_MAX);
+       if (id == -1) {
+               return NT_STATUS_INSUFFICIENT_RESOURCES;
        }
-       
+
+       search->pvfs = pvfs;
+       search->handle = id;
        search->dir = dir;
        search->current_index = 0;
        search->search_attrib = search_attrib;
+       search->must_attrib = 0;
+       search->last_used = 0;
 
-       if (dir->count < max_count) {
-               max_count = dir->count;
-       }
-
-       /* note that fill_search_info() can fail, if for example a
-          file disappears during a search or we don't have sufficient
-          permissions to stat() it, or the search_attrib does not
-          match the files attribute. In that case the name is ignored
-          and the search continues. */
-       for (i=reply_count=0; i < dir->count && reply_count < max_count;i++) {
-               union smb_search_data *file;
-
-               file = talloc_p(req, union smb_search_data);
-               if (!file) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+       talloc_set_destructor(search, pvfs_search_destructor);
 
-               status = fill_search_info(pvfs, io->generic.level, dir->unix_path, dir->names[i], 
-                                         search_attrib, i, file);
-               if (NT_STATUS_IS_OK(status)) {
-                       if (!callback(search_private, file)) {
-                               break;
-                       }
-                       reply_count++;
-               }
-               talloc_free(file);
+       status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
+                                 &reply_count, search_private, callback);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        /* not matching any entries is an error */
        if (reply_count == 0) {
-               return NT_STATUS_NO_MORE_ENTRIES;
+               return NT_STATUS_NO_SUCH_FILE;
        }
 
-       search->current_index = i;
+       io->t2ffirst.out.count = reply_count;
+       io->t2ffirst.out.handle = search->handle;
+       io->t2ffirst.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
 
-       if (io->generic.level == RAW_SEARCH_SEARCH) {
-               io->search_first.out.count = reply_count;
-               DEBUG(0,("TODO: handle RAW_SEARCH_SEARCH continue\n"));
+       /* work out if we are going to keep the search state
+          and allow for a search continue */
+       if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
+           ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && 
+            io->t2ffirst.out.end_of_search)) {
+               talloc_free(search);
        } else {
-               io->t2ffirst.out.count = reply_count;
-               io->t2ffirst.out.handle = search->handle;
-               io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
-               /* work out if we are going to keep the search state
-                  and allow for a search continue */
-               if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
-                   ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
-                       talloc_free(search);
-               } else {
-                       pvfs->search.num_active_searches++;
-                       pvfs->search.next_search_handle++;
-                       talloc_steal(pvfs, search);
-                       DLIST_ADD(pvfs->search.open_searches, search);
-               }
+               talloc_steal(pvfs, search);
        }
 
        return NT_STATUS_OK;
 }
 
 /* continue a search */
-NTSTATUS pvfs_search_next(struct smbsrv_request *req, union smb_search_next *io, 
+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 *))
 {
-#if 0
-       struct pvfs_state *pvfs = req->tcon->ntvfs_private;
-       struct search_state *search;
-       union smb_search_data file;
-       uint_t max_count;
+       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_search_state *search;
+       struct pvfs_dir *dir;
+       uint_t reply_count;
        uint16_t handle;
-       int i;
+       NTSTATUS status;
 
-       if (io->generic.level == RAW_SEARCH_SEARCH) {
-               max_count     = io->search_next.in.max_count;
-               search_attrib = io->search_next.in.search_attrib;
-       } else {
-               handle = io->t2fnext.in.handle;
+       if (io->generic.level >= RAW_SEARCH_SEARCH) {
+               return pvfs_search_next_old(ntvfs, req, io, search_private, callback);
        }
 
-       if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
-               return NT_STATUS_NOT_SUPPORTED;
-       }
+       handle = io->t2fnext.in.handle;
 
-       for (search=private->search; search; search = search->next) {
-               if (search->handle == io->t2fnext.in.handle) break;
-       }
-       
-       if (!search) {
+       search = idr_find(pvfs->idtree_search, handle);
+       if (search == NULL) {
                /* we didn't find the search handle */
-               return NT_STATUS_FOOBAR;
+               return NT_STATUS_INVALID_HANDLE;
        }
 
        dir = search->dir;
 
-       /* the client might be asking for something other than just continuing
-          with the search */
-       if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
-           (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
-           io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
-               /* look backwards first */
-               for (i=search->current_index; i > 0; i--) {
-                       if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
-                               search->current_index = i;
-                               goto found;
-                       }
-               }
-
-               /* then look forwards */
-               for (i=search->current_index+1; i <= dir->count; i++) {
-                       if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
-                               search->current_index = i;
-                               goto found;
-                       }
+       /* 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)) {
+                       return status;
                }
+       } else if (io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) {
+               /* plain continue - nothing to do */
+       } else {
+               search->current_index = io->t2fnext.in.resume_key;
        }
 
-found: 
-       max_count = search->current_index + io->t2fnext.in.max_count;
-
-       if (max_count > dir->count) {
-               max_count = dir->count;
+       status = pvfs_list_wakeup(dir, &search->current_index);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       for (i = search->current_index; i < max_count;i++) {
-               ZERO_STRUCT(file);
-               unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
-               unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
-               unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
-               unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
-               file.both_directory_info.name.s = dir->files[i].name;
-               file.both_directory_info.short_name.s = dir->files[i].name;
-               file.both_directory_info.size = dir->files[i].st.st_size;
-               file.both_directory_info.attrib = pvfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
-
-               if (!callback(search_private, &file)) {
-                       break;
-               }
+       status = pvfs_search_fill(pvfs, req, io->t2fnext.in.max_count, search, io->generic.level,
+                                 &reply_count, search_private, callback);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       io->t2fnext.out.count = i - search->current_index;
-       io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
-
-       search->current_index = i;
+       io->t2fnext.out.count = reply_count;
+       io->t2fnext.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
 
        /* work out if we are going to keep the search state */
        if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
-           ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
-               DLIST_REMOVE(private->search, search);
+           ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && 
+            io->t2fnext.out.end_of_search)) {
                talloc_free(search);
        }
-#endif
+
        return NT_STATUS_OK;
 }
 
 /* close a search */
-NTSTATUS pvfs_search_close(struct smbsrv_request *req, union smb_search_close *io)
+NTSTATUS pvfs_search_close(struct ntvfs_module_context *ntvfs,
+                          struct smbsrv_request *req, union smb_search_close *io)
 {
-       return NT_STATUS_NOT_IMPLEMENTED;
+       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_search_state *search;
+       uint16_t handle;
+
+       if (io->generic.level == RAW_FINDCLOSE_FCLOSE) {
+               handle = io->fclose.in.id.handle;
+       } else {
+               handle = io->findclose.in.handle;
+       }
+
+       search = idr_find(pvfs->idtree_search, handle);
+       if (search == NULL) {
+               /* we didn't find the search handle */
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       talloc_free(search);
+
+       return NT_STATUS_OK;
 }