2 Unix SMB/CIFS implementation.
3 Manage connections_struct structures
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Alexander Bokovoy 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
24 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
25 * maximum size of the bitmap is the largest positive integer, but you will hit
26 * the "max connections" limit, looong before that.
28 #define BITMAP_BLOCK_SZ 128
30 /****************************************************************************
31 init the conn structures
32 ****************************************************************************/
33 void conn_init(struct smbd_server_connection
*sconn
)
35 sconn
->smb1
.tcons
.Connections
= NULL
;
36 sconn
->smb1
.tcons
.num_open
= 0;
37 sconn
->smb1
.tcons
.bmap
= bitmap_talloc(sconn
, BITMAP_BLOCK_SZ
);
40 /****************************************************************************
41 return the number of open connections
42 ****************************************************************************/
43 int conn_num_open(struct smbd_server_connection
*sconn
)
45 return sconn
->smb1
.tcons
.num_open
;
49 /****************************************************************************
50 check if a snum is in use
51 ****************************************************************************/
52 bool conn_snum_used(int snum
)
54 struct smbd_server_connection
*sconn
= smbd_server_conn
;
55 connection_struct
*conn
;
56 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=conn
->next
) {
57 if (conn
->params
->service
== snum
) {
64 /****************************************************************************
65 Find a conn given a cnum.
66 ****************************************************************************/
68 connection_struct
*conn_find(struct smbd_server_connection
*sconn
,unsigned cnum
)
71 connection_struct
*conn
;
73 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=conn
->next
,count
++) {
74 if (conn
->cnum
== cnum
) {
76 DLIST_PROMOTE(sconn
->smb1
.tcons
.Connections
,
86 /****************************************************************************
87 find first available connection slot, starting from a random position.
88 The randomisation stops problems with the server dieing and clients
89 thinking the server is still available.
90 ****************************************************************************/
91 connection_struct
*conn_new(struct smbd_server_connection
*sconn
)
93 connection_struct
*conn
;
97 if (sconn
->allow_smb2
) {
98 if (!(conn
=TALLOC_ZERO_P(NULL
, connection_struct
)) ||
99 !(conn
->params
= TALLOC_P(conn
, struct share_params
))) {
100 DEBUG(0,("TALLOC_ZERO() failed!\n"));
109 i
= bitmap_find(sconn
->smb1
.tcons
.bmap
, find_offset
);
112 /* Expand the connections bitmap. */
113 int oldsz
= sconn
->smb1
.tcons
.bmap
->n
;
114 int newsz
= sconn
->smb1
.tcons
.bmap
->n
+
116 struct bitmap
* nbmap
;
118 if (newsz
<= oldsz
) {
120 DEBUG(0,("ERROR! Out of connection structures\n"));
124 DEBUG(4,("resizing connections bitmap from %d to %d\n",
127 nbmap
= bitmap_talloc(sconn
, newsz
);
129 DEBUG(0,("ERROR! malloc fail.\n"));
133 bitmap_copy(nbmap
, sconn
->smb1
.tcons
.bmap
);
134 TALLOC_FREE(sconn
->smb1
.tcons
.bmap
);
136 sconn
->smb1
.tcons
.bmap
= nbmap
;
137 find_offset
= oldsz
; /* Start next search in the new portion. */
142 /* The bitmap position is used below as the connection number
143 * conn->cnum). This ends up as the TID field in the SMB header,
144 * which is limited to 16 bits (we skip 0xffff which is the
148 DEBUG(0, ("Maximum connection limit reached\n"));
152 if (!(conn
=TALLOC_ZERO_P(NULL
, connection_struct
)) ||
153 !(conn
->params
= TALLOC_P(conn
, struct share_params
))) {
154 DEBUG(0,("TALLOC_ZERO() failed!\n"));
160 conn
->force_group_gid
= (gid_t
)-1;
162 bitmap_set(sconn
->smb1
.tcons
.bmap
, i
);
164 sconn
->smb1
.tcons
.num_open
++;
166 string_set(&conn
->connectpath
,"");
167 string_set(&conn
->origpath
,"");
169 DLIST_ADD(sconn
->smb1
.tcons
.Connections
, conn
);
174 /****************************************************************************
175 Close all conn structures.
176 return true if any were closed
177 ****************************************************************************/
178 bool conn_close_all(struct smbd_server_connection
*sconn
)
180 if (sconn
->allow_smb2
) {
182 if (sconn
->smb2
.sessions
.list
&&
183 sconn
->smb2
.sessions
.list
->tcons
.list
) {
184 struct smbd_smb2_tcon
*tcon
, *tc_next
;
186 for (tcon
= sconn
->smb2
.sessions
.list
->tcons
.list
;
187 tcon
; tcon
= tc_next
) {
188 tc_next
= tcon
->next
;
196 connection_struct
*conn
, *next
;
199 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=next
) {
201 set_current_service(conn
, 0, True
);
202 close_cnum(conn
, conn
->vuid
);
209 /****************************************************************************
210 Idle inactive connections.
211 ****************************************************************************/
213 bool conn_idle_all(struct smbd_server_connection
*sconn
,time_t t
)
215 int deadtime
= lp_deadtime()*60;
216 pipes_struct
*plist
= NULL
;
217 connection_struct
*conn
;
220 deadtime
= DEFAULT_SMBD_TIMEOUT
;
222 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=conn
->next
) {
224 time_t age
= t
- conn
->lastused
;
226 /* Update if connection wasn't idle. */
227 if (conn
->lastused
!= conn
->lastused_count
) {
229 conn
->lastused_count
= t
;
232 /* close dirptrs on connections that are idle */
233 if (age
> DPTR_IDLE_TIMEOUT
) {
237 if (conn
->num_files_open
> 0 || age
< deadtime
) {
243 * Check all pipes for any open handles. We cannot
244 * idle with a handle open.
247 for (plist
= get_first_internal_pipe(); plist
;
248 plist
= get_next_internal_pipe(plist
)) {
249 if (num_pipe_handles(plist
->pipe_handles
) != 0) {
257 /****************************************************************************
258 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
259 ****************************************************************************/
261 void conn_clear_vuid_caches(struct smbd_server_connection
*sconn
,uint16_t vuid
)
263 connection_struct
*conn
;
265 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=conn
->next
) {
266 if (conn
->vuid
== vuid
) {
267 conn
->vuid
= UID_FIELD_INVALID
;
269 conn_clear_vuid_cache(conn
, vuid
);
273 /****************************************************************************
274 Free a conn structure - internal part.
275 ****************************************************************************/
277 static void conn_free_internal(connection_struct
*conn
)
279 vfs_handle_struct
*handle
= NULL
, *thandle
= NULL
;
280 struct trans_state
*state
= NULL
;
282 /* Free vfs_connection_struct */
283 handle
= conn
->vfs_handles
;
285 thandle
= handle
->next
;
286 DLIST_REMOVE(conn
->vfs_handles
, handle
);
287 if (handle
->free_data
)
288 handle
->free_data(&handle
->data
);
292 /* Free any pending transactions stored on this conn. */
293 for (state
= conn
->pending_trans
; state
; state
= state
->next
) {
294 /* state->setup is a talloc child of state. */
295 SAFE_FREE(state
->param
);
296 SAFE_FREE(state
->data
);
299 free_namearray(conn
->veto_list
);
300 free_namearray(conn
->hide_list
);
301 free_namearray(conn
->veto_oplock_list
);
302 free_namearray(conn
->aio_write_behind_list
);
304 string_free(&conn
->connectpath
);
305 string_free(&conn
->origpath
);
308 talloc_destroy(conn
);
311 /****************************************************************************
312 Free a conn structure.
313 ****************************************************************************/
315 void conn_free(connection_struct
*conn
)
317 if (conn
->sconn
== NULL
) {
318 conn_free_internal(conn
);
322 if (conn
->sconn
->allow_smb2
) {
323 conn_free_internal(conn
);
327 DLIST_REMOVE(conn
->sconn
->smb1
.tcons
.Connections
, conn
);
329 bitmap_clear(conn
->sconn
->smb1
.tcons
.bmap
, conn
->cnum
);
331 SMB_ASSERT(conn
->sconn
->smb1
.tcons
.num_open
> 0);
332 conn
->sconn
->smb1
.tcons
.num_open
--;
334 conn_free_internal(conn
);
337 /****************************************************************************
338 receive a smbcontrol message to forcibly unmount a share
339 the message contains just a share name and all instances of that
341 the special sharename '*' forces unmount of all shares
342 ****************************************************************************/
343 void msg_force_tdis(struct messaging_context
*msg
,
346 struct server_id server_id
,
349 struct smbd_server_connection
*sconn
= smbd_server_conn
;
350 connection_struct
*conn
, *next
;
353 fstrcpy(sharename
, (const char *)data
->data
);
355 if (strcmp(sharename
, "*") == 0) {
356 DEBUG(1,("Forcing close of all shares\n"));
357 conn_close_all(sconn
);
361 for (conn
=sconn
->smb1
.tcons
.Connections
;conn
;conn
=next
) {
363 if (strequal(lp_servicename(SNUM(conn
)), sharename
)) {
364 DEBUG(1,("Forcing close of share %s cnum=%d\n",
365 sharename
, conn
->cnum
));
366 close_cnum(conn
, (uint16
)-1);