Merge branch 'fix/misc' into for-linus
[cascardo/linux.git] / fs / nfs / client.c
index bc3a862..192f2f8 100644 (file)
@@ -172,7 +172,6 @@ static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_
        if (err)
                goto error_cleanup;
 
-       INIT_LIST_HEAD(&clp->cl_delegations);
        spin_lock_init(&clp->cl_lock);
        INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
        rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
@@ -244,6 +243,11 @@ static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
                idr_remove(&cb_ident_idr, clp->cl_cb_ident);
 }
 
+static void pnfs_init_server(struct nfs_server *server)
+{
+       rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
+}
+
 #else
 static void nfs4_shutdown_client(struct nfs_client *clp)
 {
@@ -256,6 +260,11 @@ void nfs_cleanup_cb_ident_idr(void)
 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
 {
 }
+
+static void pnfs_init_server(struct nfs_server *server)
+{
+}
+
 #endif /* CONFIG_NFS_V4 */
 
 /*
@@ -410,70 +419,28 @@ static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
        return 0;
 }
 
-/*
- * Find a client by IP address and protocol version
- * - returns NULL if no such client
- */
-struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
+/* Common match routine for v4.0 and v4.1 callback services */
+bool
+nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
+                    u32 minorversion)
 {
-       struct nfs_client *clp;
-
-       spin_lock(&nfs_client_lock);
-       list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
-               struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
-
-               /* Don't match clients that failed to initialise properly */
-               if (!(clp->cl_cons_state == NFS_CS_READY ||
-                     clp->cl_cons_state == NFS_CS_SESSION_INITING))
-                       continue;
-
-               /* Different NFS versions cannot share the same nfs_client */
-               if (clp->rpc_ops->version != nfsversion)
-                       continue;
-
-               /* Match only the IP address, not the port number */
-               if (!nfs_sockaddr_match_ipaddr(addr, clap))
-                       continue;
-
-               atomic_inc(&clp->cl_count);
-               spin_unlock(&nfs_client_lock);
-               return clp;
-       }
-       spin_unlock(&nfs_client_lock);
-       return NULL;
-}
-
-/*
- * Find a client by IP address and protocol version
- * - returns NULL if no such client
- */
-struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
-{
-       struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
-       u32 nfsvers = clp->rpc_ops->version;
-
-       spin_lock(&nfs_client_lock);
-       list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
-               struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
+       struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
 
-               /* Don't match clients that failed to initialise properly */
-               if (clp->cl_cons_state != NFS_CS_READY)
-                       continue;
+       /* Don't match clients that failed to initialise */
+       if (!(clp->cl_cons_state == NFS_CS_READY ||
+           clp->cl_cons_state == NFS_CS_SESSION_INITING))
+               return false;
 
-               /* Different NFS versions cannot share the same nfs_client */
-               if (clp->rpc_ops->version != nfsvers)
-                       continue;
+       /* Match the version and minorversion */
+       if (clp->rpc_ops->version != 4 ||
+           clp->cl_minorversion != minorversion)
+               return false;
 
-               /* Match only the IP address, not the port number */
-               if (!nfs_sockaddr_match_ipaddr(sap, clap))
-                       continue;
+       /* Match only the IP address, not the port number */
+       if (!nfs_sockaddr_match_ipaddr(addr, clap))
+               return false;
 
-               atomic_inc(&clp->cl_count);
-               spin_unlock(&nfs_client_lock);
-               return clp;
-       }
-       spin_unlock(&nfs_client_lock);
-       return NULL;
+       return true;
 }
 
 /*
@@ -1035,6 +1002,27 @@ static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_serve
        target->options = source->options;
 }
 
+static void nfs_server_insert_lists(struct nfs_server *server)
+{
+       struct nfs_client *clp = server->nfs_client;
+
+       spin_lock(&nfs_client_lock);
+       list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
+       list_add_tail(&server->master_link, &nfs_volume_list);
+       spin_unlock(&nfs_client_lock);
+
+}
+
+static void nfs_server_remove_lists(struct nfs_server *server)
+{
+       spin_lock(&nfs_client_lock);
+       list_del_rcu(&server->client_link);
+       list_del(&server->master_link);
+       spin_unlock(&nfs_client_lock);
+
+       synchronize_rcu();
+}
+
 /*
  * Allocate and initialise a server record
  */
@@ -1051,6 +1039,7 @@ static struct nfs_server *nfs_alloc_server(void)
        /* Zero out the NFS state stuff */
        INIT_LIST_HEAD(&server->client_link);
        INIT_LIST_HEAD(&server->master_link);
+       INIT_LIST_HEAD(&server->delegations);
 
        atomic_set(&server->active, 0);
 
@@ -1066,6 +1055,8 @@ static struct nfs_server *nfs_alloc_server(void)
                return NULL;
        }
 
+       pnfs_init_server(server);
+
        return server;
 }
 
@@ -1076,11 +1067,8 @@ void nfs_free_server(struct nfs_server *server)
 {
        dprintk("--> nfs_free_server()\n");
 
+       nfs_server_remove_lists(server);
        unset_pnfs_layoutdriver(server);
-       spin_lock(&nfs_client_lock);
-       list_del(&server->client_link);
-       list_del(&server->master_link);
-       spin_unlock(&nfs_client_lock);
 
        if (server->destroy != NULL)
                server->destroy(server);
@@ -1155,11 +1143,7 @@ struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
                (unsigned long long) server->fsid.major,
                (unsigned long long) server->fsid.minor);
 
-       spin_lock(&nfs_client_lock);
-       list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
-       list_add_tail(&server->master_link, &nfs_volume_list);
-       spin_unlock(&nfs_client_lock);
-
+       nfs_server_insert_lists(server);
        server->mount_time = jiffies;
        nfs_free_fattr(fattr);
        return server;
@@ -1171,6 +1155,101 @@ error:
 }
 
 #ifdef CONFIG_NFS_V4
+/*
+ * NFSv4.0 callback thread helper
+ *
+ * Find a client by IP address, protocol version, and minorversion
+ *
+ * Called from the pg_authenticate method. The callback identifier
+ * is not used as it has not been decoded.
+ *
+ * Returns NULL if no such client
+ */
+struct nfs_client *
+nfs4_find_client_no_ident(const struct sockaddr *addr)
+{
+       struct nfs_client *clp;
+
+       spin_lock(&nfs_client_lock);
+       list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
+               if (nfs4_cb_match_client(addr, clp, 0) == false)
+                       continue;
+               atomic_inc(&clp->cl_count);
+               spin_unlock(&nfs_client_lock);
+               return clp;
+       }
+       spin_unlock(&nfs_client_lock);
+       return NULL;
+}
+
+/*
+ * NFSv4.0 callback thread helper
+ *
+ * Find a client by callback identifier
+ */
+struct nfs_client *
+nfs4_find_client_ident(int cb_ident)
+{
+       struct nfs_client *clp;
+
+       spin_lock(&nfs_client_lock);
+       clp = idr_find(&cb_ident_idr, cb_ident);
+       if (clp)
+               atomic_inc(&clp->cl_count);
+       spin_unlock(&nfs_client_lock);
+       return clp;
+}
+
+#if defined(CONFIG_NFS_V4_1)
+/*
+ * NFSv4.1 callback thread helper
+ * For CB_COMPOUND calls, find a client by IP address, protocol version,
+ * minorversion, and sessionID
+ *
+ * CREATE_SESSION triggers a CB_NULL ping from servers. The callback service
+ * sessionid can only be set after the CREATE_SESSION return, so a CB_NULL
+ * can arrive before the callback sessionid is set. For CB_NULL calls,
+ * find a client by IP address protocol version, and minorversion.
+ *
+ * Returns NULL if no such client
+ */
+struct nfs_client *
+nfs4_find_client_sessionid(const struct sockaddr *addr,
+                          struct nfs4_sessionid *sid, int is_cb_compound)
+{
+       struct nfs_client *clp;
+
+       spin_lock(&nfs_client_lock);
+       list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
+               if (nfs4_cb_match_client(addr, clp, 1) == false)
+                       continue;
+
+               if (!nfs4_has_session(clp))
+                       continue;
+
+               /* Match sessionid unless cb_null call*/
+               if (is_cb_compound && (memcmp(clp->cl_session->sess_id.data,
+                   sid->data, NFS4_MAX_SESSIONID_LEN) != 0))
+                       continue;
+
+               atomic_inc(&clp->cl_count);
+               spin_unlock(&nfs_client_lock);
+               return clp;
+       }
+       spin_unlock(&nfs_client_lock);
+       return NULL;
+}
+
+#else /* CONFIG_NFS_V4_1 */
+
+struct nfs_client *
+nfs4_find_client_sessionid(const struct sockaddr *addr,
+                          struct nfs4_sessionid *sid, int is_cb_compound)
+{
+       return NULL;
+}
+#endif /* CONFIG_NFS_V4_1 */
+
 /*
  * Initialize the NFS4 callback service
  */
@@ -1389,11 +1468,7 @@ static int nfs4_server_common_setup(struct nfs_server *server,
        if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
                server->namelen = NFS4_MAXNAMLEN;
 
-       spin_lock(&nfs_client_lock);
-       list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
-       list_add_tail(&server->master_link, &nfs_volume_list);
-       spin_unlock(&nfs_client_lock);
-
+       nfs_server_insert_lists(server);
        server->mount_time = jiffies;
 out:
        nfs_free_fattr(fattr);
@@ -1598,11 +1673,7 @@ struct nfs_server *nfs_clone_server(struct nfs_server *source,
        if (error < 0)
                goto out_free_server;
 
-       spin_lock(&nfs_client_lock);
-       list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
-       list_add_tail(&server->master_link, &nfs_volume_list);
-       spin_unlock(&nfs_client_lock);
-
+       nfs_server_insert_lists(server);
        server->mount_time = jiffies;
 
        nfs_free_fattr(fattr_fsinfo);