r21187: - Convert LdbBrowse to use TreeVirtual. This adds the following capabilities
[Samba/ekacnet.git] / source4 / smb_server / handle.c
blob87575896bdef4bc9da6ac200ef8de508bc97c28a
1 /*
2 Unix SMB/CIFS implementation.
3 Manage smbsrv_handle structures
4 Copyright (C) Stefan Metzmacher 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "lib/util/dlinklist.h"
23 #include "smb_server/smb_server.h"
24 #include "ntvfs/ntvfs.h"
27 /****************************************************************************
28 init the handle structures
29 ****************************************************************************/
30 NTSTATUS smbsrv_init_handles(struct smbsrv_tcon *tcon, uint64_t limit)
32 /*
33 * the idr_* functions take 'int' as limit,
34 * and only work with a max limit 0x00FFFFFF
36 limit &= 0x00FFFFFF;
38 tcon->handles.idtree_hid = idr_init(tcon);
39 NT_STATUS_HAVE_NO_MEMORY(tcon->handles.idtree_hid);
40 tcon->handles.idtree_limit = limit;
41 tcon->handles.list = NULL;
43 return NT_STATUS_OK;
46 /****************************************************************************
47 find a handle given a handle id
48 ****************************************************************************/
49 static struct smbsrv_handle *smbsrv_handle_find(struct smbsrv_handles_context *handles_ctx,
50 uint64_t hid, struct timeval request_time)
52 void *p;
53 struct smbsrv_handle *handle;
55 if (hid == 0) return NULL;
57 if (hid > handles_ctx->idtree_limit) return NULL;
59 p = idr_find(handles_ctx->idtree_hid, hid);
60 if (!p) return NULL;
62 handle = talloc_get_type(p, struct smbsrv_handle);
63 if (!handle) return NULL;
65 /* only give it away when the ntvfs subsystem has made the handle valid */
66 if (!handle->ntvfs) return NULL;
68 handle->statistics.last_use_time = request_time;
70 return handle;
73 struct smbsrv_handle *smbsrv_smb_handle_find(struct smbsrv_tcon *smb_tcon,
74 uint16_t fnum, struct timeval request_time)
76 return smbsrv_handle_find(&smb_tcon->handles, fnum, request_time);
79 struct smbsrv_handle *smbsrv_smb2_handle_find(struct smbsrv_tcon *smb_tcon,
80 uint64_t hid, struct timeval request_time)
82 return smbsrv_handle_find(&smb_tcon->handles, hid, request_time);
86 destroy a connection structure
88 static int smbsrv_handle_destructor(struct smbsrv_handle *handle)
90 struct smbsrv_handles_context *handles_ctx;
92 handles_ctx = &handle->tcon->handles;
94 idr_remove(handles_ctx->idtree_hid, handle->hid);
95 DLIST_REMOVE(handles_ctx->list, handle);
96 DLIST_REMOVE(handle->session->handles, &handle->session_item);
98 /* tell the ntvfs backend that we are disconnecting */
99 if (handle->ntvfs) {
100 talloc_free(handle->ntvfs);
101 handle->ntvfs = NULL;
104 return 0;
108 find first available handle slot
110 struct smbsrv_handle *smbsrv_handle_new(struct smbsrv_session *session,
111 struct smbsrv_tcon *tcon,
112 TALLOC_CTX *mem_ctx,
113 struct timeval request_time)
115 struct smbsrv_handles_context *handles_ctx = &tcon->handles;
116 struct smbsrv_handle *handle;
117 int i;
119 handle = talloc_zero(mem_ctx, struct smbsrv_handle);
120 if (!handle) return NULL;
121 handle->tcon = tcon;
122 handle->session = session;
124 i = idr_get_new_above(handles_ctx->idtree_hid, handle, 1, handles_ctx->idtree_limit);
125 if (i == -1) {
126 DEBUG(1,("ERROR! Out of handle structures\n"));
127 goto failed;
129 handle->hid = i;
130 handle->session_item.handle = handle;
132 DLIST_ADD(handles_ctx->list, handle);
133 DLIST_ADD(session->handles, &handle->session_item);
134 talloc_set_destructor(handle, smbsrv_handle_destructor);
136 /* now fill in some statistics */
137 handle->statistics.open_time = request_time;
138 handle->statistics.last_use_time = request_time;
140 return handle;
142 failed:
143 talloc_free(handle);
144 return NULL;