r17930: Merge noinclude branch:
[kai/samba-autobuild/.git] / source4 / smb_server / handle.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Manage smbsrv_handle structures
4    Copyright (C) Stefan Metzmacher 2006
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "lib/util/dlinklist.h"
23 #include "smb_server/smb_server.h"
24 #include "ntvfs/ntvfs.h"
25
26
27 /****************************************************************************
28 init the handle structures
29 ****************************************************************************/
30 NTSTATUS smbsrv_init_handles(struct smbsrv_tcon *tcon, uint64_t limit)
31 {
32         /* 
33          * the idr_* functions take 'int' as limit,
34          * and only work with a max limit 0x00FFFFFF
35          */
36         limit &= 0x00FFFFFF;
37
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;
42
43         return NT_STATUS_OK;
44 }
45
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)
51 {
52         void *p;
53         struct smbsrv_handle *handle;
54
55         if (hid == 0) return NULL;
56
57         if (hid > handles_ctx->idtree_limit) return NULL;
58
59         p = idr_find(handles_ctx->idtree_hid, hid);
60         if (!p) return NULL;
61
62         handle = talloc_get_type(p, struct smbsrv_handle);
63         if (!handle) return NULL;
64
65         /* only give it away when the ntvfs subsystem has made the handle valid */
66         if (!handle->ntvfs) return NULL;
67
68         handle->statistics.last_use_time = request_time;
69
70         return handle;
71 }
72
73 struct smbsrv_handle *smbsrv_smb_handle_find(struct smbsrv_tcon *smb_tcon,
74                                              uint16_t fnum, struct timeval request_time)
75 {
76         return smbsrv_handle_find(&smb_tcon->handles, fnum, request_time);
77 }
78
79 struct smbsrv_handle *smbsrv_smb2_handle_find(struct smbsrv_tcon *smb_tcon,
80                                               uint64_t hid, struct timeval request_time)
81 {
82         return smbsrv_handle_find(&smb_tcon->handles, hid, request_time);
83 }
84
85 /*
86   destroy a connection structure
87 */
88 static int smbsrv_handle_destructor(struct smbsrv_handle *handle)
89 {
90         struct smbsrv_handles_context *handles_ctx;
91
92         handles_ctx = &handle->tcon->handles;
93
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);
97
98         /* tell the ntvfs backend that we are disconnecting */
99         if (handle->ntvfs) {
100                 talloc_free(handle->ntvfs);
101                 handle->ntvfs = NULL;
102         }
103
104         return 0;
105 }
106
107 /*
108   find first available handle slot
109 */
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)
114 {
115         struct smbsrv_handles_context *handles_ctx = &tcon->handles;
116         struct smbsrv_handle *handle;
117         int i;
118
119         handle = talloc_zero(mem_ctx, struct smbsrv_handle);
120         if (!handle) return NULL;
121         handle->tcon    = tcon;
122         handle->session = session;
123         
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;
128         }
129         handle->hid = i;
130         handle->session_item.handle = handle;
131
132         DLIST_ADD(handles_ctx->list, handle);
133         DLIST_ADD(session->handles, &handle->session_item);
134         talloc_set_destructor(handle, smbsrv_handle_destructor);
135
136         /* now fill in some statistics */
137         handle->statistics.open_time            = request_time;
138         handle->statistics.last_use_time        = request_time;
139
140         return handle;
141
142 failed:
143         talloc_free(handle);
144         return NULL;
145 }