smbd: remove dead code
[samba.git] / source3 / smbd / conn_idle.c
index 4dfa4097c31ff3dbc4261db1c91fd82a3b728d61..ca697383877135db5b51543e6e64b278b3bf9842 100644 (file)
@@ -23,6 +23,7 @@
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "rpc_server/rpc_pipes.h"
+#include "lib/util/tevent_ntstatus.h"
 
 /****************************************************************************
  Update last used timestamps.
@@ -53,17 +54,12 @@ bool conn_idle_all(struct smbd_server_connection *sconn, time_t t)
        conn_lastused_update(sconn, t);
 
        if (deadtime <= 0) {
-               deadtime = DEFAULT_SMBD_TIMEOUT;
+               return false;
        }
 
        for (conn=sconn->connections;conn;conn=conn->next) {
                time_t age = t - conn->lastused;
 
-               /* close dirptrs on connections that are idle */
-               if (age > DPTR_IDLE_TIMEOUT) {
-                       dptr_idlecnum(conn);
-               }
-
                if (conn->num_files_open > 0 || age < deadtime) {
                        return false;
                }
@@ -81,81 +77,201 @@ bool conn_idle_all(struct smbd_server_connection *sconn, time_t t)
 }
 
 /****************************************************************************
- Close all conn structures.
- Return true if any were closed.
+ Forcibly unmount a share - async
+ All instances of the parameter 'sharename' share are unmounted.
+ The special sharename '*' forces unmount of all shares.
 ****************************************************************************/
 
-void conn_close_all(struct smbd_server_connection *sconn)
+static struct tevent_req *conn_force_tdis_send(connection_struct *conn);
+static void conn_force_tdis_done(struct tevent_req *req);
+
+void conn_force_tdis(
+       struct smbd_server_connection *sconn,
+       bool (*check_fn)(struct connection_struct *conn,
+                        void *private_data),
+       void *private_data)
 {
-       if (sconn->using_smb2) {
-               /* SMB2 */
-               struct smbd_smb2_session *sess;
+       connection_struct *conn;
 
-               for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
-                       struct smbd_smb2_tcon *tcon, *tc_next;
+       /* SMB1 and SMB 2*/
+       for (conn = sconn->connections; conn; conn = conn->next) {
+               struct smbXsrv_tcon *tcon;
+               bool do_close = false;
+               struct tevent_req *req;
 
-                       file_close_user(sconn, sess->vuid);
+               if (conn->tcon == NULL) {
+                       continue;
+               }
+               tcon = conn->tcon;
 
-                       for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
-                               tc_next = tcon->next;
-                               TALLOC_FREE(tcon);
-                       }
+               if (!NT_STATUS_IS_OK(tcon->status)) {
+                       /* In the process of already being disconnected. */
+                       continue;
+               }
+
+               do_close = check_fn(conn, private_data);
+               if (!do_close) {
+                       continue;
                }
-       } else {
-               /* SMB1 */
-               connection_struct *conn, *next;
-
-               for (conn=sconn->connections;conn;conn=next) {
-                       next=conn->next;
-                       set_current_service(conn, 0, True);
-                       close_cnum(conn, conn->vuid);
+
+               req = conn_force_tdis_send(conn);
+               if (req == NULL) {
+                       DBG_WARNING("talloc_fail forcing async close of "
+                               "share '%s'\n",
+                               tcon->global->share_name);
+                       continue;
                }
+
+               DBG_WARNING("Forcing close of "
+                           "share '%s' (wire_id=0x%08x)\n",
+                           tcon->global->share_name,
+                           tcon->global->tcon_wire_id);
+
+               tevent_req_set_callback(req, conn_force_tdis_done, conn);
        }
 }
 
+struct conn_force_tdis_state {
+       struct tevent_queue *wait_queue;
+};
 
-/****************************************************************************
- Forcibly unmount a share.
- All instances of the parameter 'sharename' share are unmounted.
- The special sharename '*' forces unmount of all shares.
-****************************************************************************/
+static void conn_force_tdis_wait_done(struct tevent_req *subreq);
 
-void conn_force_tdis(struct smbd_server_connection *sconn, const char *sharename)
+static struct tevent_req *conn_force_tdis_send(connection_struct *conn)
 {
-       connection_struct *conn, *next;
+       struct tevent_req *req;
+       struct conn_force_tdis_state *state;
+       struct tevent_req *subreq;
+       files_struct *fsp;
 
-       if (strcmp(sharename, "*") == 0) {
-               DEBUG(1,("Forcing close of all shares\n"));
-               conn_close_all(sconn);
-               return;
+       /* Create this off the NULL context. We must clean up on return. */
+       req = tevent_req_create(NULL, &state,
+                       struct conn_force_tdis_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->wait_queue = tevent_queue_create(state,
+                       "conn_force_tdis_wait_queue");
+       if (tevent_req_nomem(state->wait_queue, req)) {
+               TALLOC_FREE(req);
+               return NULL;
        }
 
-       if (sconn->using_smb2) {
-               /* SMB2 */
-               struct smbd_smb2_session *sess;
-               for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
-                       struct smbd_smb2_tcon *tcon, *tc_next;
-
-                       for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
-                               tc_next = tcon->next;
-                               if (tcon->compat_conn &&
-                                               strequal(lp_servicename(SNUM(tcon->compat_conn)),
-                                                               sharename)) {
-                                       DEBUG(1,("Forcing close of share %s cnum=%d\n",
-                                               sharename, tcon->compat_conn->cnum));
-                                       TALLOC_FREE(tcon);
-                               }
-                       }
+       /*
+        * Make sure that no new request will be able to use this tcon.
+        * This ensures that once all outstanding fsp->aio_requests
+        * on this tcon are done, we are safe to close it.
+        */
+       conn->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
+
+       for (fsp = conn->sconn->files; fsp; fsp = fsp->next) {
+               if (fsp->conn != conn) {
+                       continue;
                }
-       } else {
-               /* SMB1 */
-               for (conn=sconn->connections;conn;conn=next) {
-                       next=conn->next;
-                       if (strequal(lp_servicename(SNUM(conn)), sharename)) {
-                               DEBUG(1,("Forcing close of share %s cnum=%d\n",
-                                       sharename, conn->cnum));
-                               close_cnum(conn, UID_FIELD_INVALID);
+               /*
+                * Flag the file as close in progress.
+                * This will prevent any more IO being
+                * done on it. Not strictly needed, but
+                * doesn't hurt to flag it as closing.
+                */
+               fsp->fsp_flags.closing = true;
+
+               if (fsp->num_aio_requests > 0) {
+                       /*
+                        * Now wait until all aio requests on this fsp are
+                        * finished.
+                        *
+                        * We don't set a callback, as we just want to block the
+                        * wait queue and the talloc_free() of fsp->aio_request
+                        * will remove the item from the wait queue.
+                        */
+                       subreq = tevent_queue_wait_send(fsp->aio_requests,
+                                               conn->sconn->ev_ctx,
+                                               state->wait_queue);
+                       if (tevent_req_nomem(subreq, req)) {
+                               TALLOC_FREE(req);
+                               return NULL;
                        }
                }
        }
+       /*
+        * Now we add our own waiter to the end of the queue,
+        * this way we get notified when all pending requests are finished
+        * and reply to the outstanding SMB1 request.
+        */
+       subreq = tevent_queue_wait_send(state,
+                               conn->sconn->ev_ctx,
+                               state->wait_queue);
+       if (tevent_req_nomem(subreq, req)) {
+               TALLOC_FREE(req);
+               return NULL;
+       }
+
+       tevent_req_set_callback(subreq, conn_force_tdis_wait_done, req);
+       return req;
+}
+
+static void conn_force_tdis_wait_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+
+       tevent_queue_wait_recv(subreq);
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
+
+static NTSTATUS conn_force_tdis_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+static void conn_force_tdis_done(struct tevent_req *req)
+{
+       connection_struct *conn = tevent_req_callback_data(
+               req, connection_struct);
+       NTSTATUS status;
+       uint64_t vuid = UID_FIELD_INVALID;
+       struct smbXsrv_tcon *tcon = conn->tcon;
+       struct smbd_server_connection *sconn = conn->sconn;
+
+       status = conn_force_tdis_recv(req);
+       TALLOC_FREE(req);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_ERR("conn_force_tdis_recv of share '%s' "
+                       "(wire_id=0x%08x) failed: %s\n",
+                       tcon->global->share_name,
+                       tcon->global->tcon_wire_id,
+                       nt_errstr(status));
+               return;
+       }
+
+       if (conn->sconn->using_smb2) {
+               vuid = conn->vuid;
+       }
+
+       DBG_WARNING("Closing "
+                   "share '%s' (wire_id=0x%08x)\n",
+                   tcon->global->share_name,
+                   tcon->global->tcon_wire_id);
+
+       conn = NULL;
+       status = smbXsrv_tcon_disconnect(tcon, vuid);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_ERR("smbXsrv_tcon_disconnect() of share '%s' "
+                       "(wire_id=0x%08x) failed: %s\n",
+                       tcon->global->share_name,
+                       tcon->global->tcon_wire_id,
+                       nt_errstr(status));
+               return;
+       }
+
+       TALLOC_FREE(tcon);
+
+       /*
+       * As we've been awoken, we may have changed
+       * uid in the meantime. Ensure we're still root.
+       */
+       change_to_root_user();
+       reload_services(sconn, conn_snum_used, true);
 }