]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - fs/nfs/client.c
nfs: fix a deadlock in nfs client initialization
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / client.c
index efebe6cf4378e32a13d109eebd0b694dd629c2a5..b9129e2befeaa4186138bbd84e78d7ca2d128370 100644 (file)
@@ -163,7 +163,7 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
 
        clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
 
-       atomic_set(&clp->cl_count, 1);
+       refcount_set(&clp->cl_count, 1);
        clp->cl_cons_state = NFS_CS_INITING;
 
        memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
@@ -218,7 +218,6 @@ static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
 static void pnfs_init_server(struct nfs_server *server)
 {
        rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
-       rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
 }
 
 #else
@@ -270,7 +269,7 @@ void nfs_put_client(struct nfs_client *clp)
 
        nn = net_generic(clp->cl_net, nfs_net_id);
 
-       if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
+       if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
                list_del(&clp->cl_share_link);
                nfs_cb_idr_remove_locked(clp);
                spin_unlock(&nn->nfs_client_lock);
@@ -292,12 +291,23 @@ static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *dat
        const struct sockaddr *sap = data->addr;
        struct nfs_net *nn = net_generic(data->net, nfs_net_id);
 
+again:
        list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
                const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
                /* Don't match clients that failed to initialise properly */
                if (clp->cl_cons_state < 0)
                        continue;
 
+               /* If a client is still initializing then we need to wait */
+               if (clp->cl_cons_state > NFS_CS_READY) {
+                       refcount_inc(&clp->cl_count);
+                       spin_unlock(&nn->nfs_client_lock);
+                       nfs_wait_client_init_complete(clp);
+                       nfs_put_client(clp);
+                       spin_lock(&nn->nfs_client_lock);
+                       goto again;
+               }
+
                /* Different NFS versions cannot share the same nfs_client */
                if (clp->rpc_ops != data->nfs_mod->rpc_ops)
                        continue;
@@ -315,7 +325,7 @@ static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *dat
                                                           sap))
                                continue;
 
-               atomic_inc(&clp->cl_count);
+               refcount_inc(&clp->cl_count);
                return clp;
        }
        return NULL;
@@ -888,6 +898,7 @@ struct nfs_server *nfs_alloc_server(void)
        ida_init(&server->openowner_id);
        ida_init(&server->lockowner_id);
        pnfs_init_server(server);
+       rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
 
        return server;
 }
@@ -1006,7 +1017,7 @@ struct nfs_server *nfs_clone_server(struct nfs_server *source,
        /* Copy data from the source */
        server->nfs_client = source->nfs_client;
        server->destroy = source->destroy;
-       atomic_inc(&server->nfs_client->cl_count);
+       refcount_inc(&server->nfs_client->cl_count);
        nfs_server_copy_userdata(server, source);
 
        server->fsid = fattr->fsid;
@@ -1166,7 +1177,7 @@ static int nfs_server_list_show(struct seq_file *m, void *v)
                   clp->rpc_ops->version,
                   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
                   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
-                  atomic_read(&clp->cl_count),
+                  refcount_read(&clp->cl_count),
                   clp->cl_hostname);
        rcu_read_unlock();