nfsd: Add locking to protect the state owner lists
authorJeff Layton <jlayton@primarydata.com>
Wed, 30 Jul 2014 01:34:12 +0000 (21:34 -0400)
committerJ. Bruce Fields <bfields@redhat.com>
Thu, 31 Jul 2014 18:20:07 +0000 (14:20 -0400)
Change to using the clp->cl_lock for this. For now, there's a lot of
cl_lock thrashing, but in later patches we'll eliminate that and close
the potential races that can occur when releasing the cl_lock while
walking the lists. For now, the client_mutex prevents those races.

Signed-off-by: Trond Myklebust <trond.myklebust@primarydata.com>
Signed-off-by: Jeff Layton <jlayton@primarydata.com>
Signed-off-by: J. Bruce Fields <bfields@redhat.com>
fs/nfsd/nfs4state.c

index 8ce5894..3ac6e2f 100644 (file)
@@ -893,6 +893,8 @@ static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
 {
        struct nfs4_file *fp = stp->st_stid.sc_file;
 
+       lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
+
        spin_lock(&fp->fi_lock);
        list_del(&stp->st_perfile);
        spin_unlock(&fp->fi_lock);
@@ -921,9 +923,13 @@ static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
 
 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
 {
+       struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
+
+       spin_lock(&oo->oo_owner.so_client->cl_lock);
        list_del(&stp->st_locks);
        unhash_generic_stateid(stp);
        unhash_stid(&stp->st_stid);
+       spin_unlock(&oo->oo_owner.so_client->cl_lock);
        nfs4_put_stid(&stp->st_stid);
 }
 
@@ -967,20 +973,26 @@ static void release_lock_stateid(struct nfs4_ol_stateid *stp)
 }
 
 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
+       __releases(&open_stp->st_stateowner->so_client->cl_lock)
+       __acquires(&open_stp->st_stateowner->so_client->cl_lock)
 {
        struct nfs4_ol_stateid *stp;
 
        while (!list_empty(&open_stp->st_locks)) {
                stp = list_entry(open_stp->st_locks.next,
                                struct nfs4_ol_stateid, st_locks);
+               spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
                release_lock_stateid(stp);
+               spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
        }
 }
 
 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
 {
+       spin_lock(&stp->st_stateowner->so_client->cl_lock);
        unhash_generic_stateid(stp);
        release_open_stateid_locks(stp);
+       spin_unlock(&stp->st_stateowner->so_client->cl_lock);
 }
 
 static void release_open_stateid(struct nfs4_ol_stateid *stp)
@@ -2996,16 +3008,18 @@ static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp,
 
        stp->st_stid.sc_type = NFS4_OPEN_STID;
        INIT_LIST_HEAD(&stp->st_locks);
-       list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
        stp->st_stateowner = &oo->oo_owner;
        get_nfs4_file(fp);
        stp->st_stid.sc_file = fp;
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = 0;
        stp->st_openstp = NULL;
+       spin_lock(&oo->oo_owner.so_client->cl_lock);
+       list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
        spin_lock(&fp->fi_lock);
        list_add(&stp->st_perfile, &fp->fi_stateids);
        spin_unlock(&fp->fi_lock);
+       spin_unlock(&oo->oo_owner.so_client->cl_lock);
 }
 
 static void
@@ -4711,6 +4725,7 @@ alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
                struct nfs4_ol_stateid *open_stp)
 {
        struct nfs4_stid *s;
+       struct nfs4_openowner *oo = openowner(open_stp->st_stateowner);
        struct nfs4_ol_stateid *stp;
        struct nfs4_client *clp = lo->lo_owner.so_client;
 
@@ -4719,7 +4734,6 @@ alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
                return NULL;
        stp = openlockstateid(s);
        stp->st_stid.sc_type = NFS4_LOCK_STID;
-       list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
        stp->st_stateowner = &lo->lo_owner;
        get_nfs4_file(fp);
        stp->st_stid.sc_file = fp;
@@ -4727,10 +4741,13 @@ alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
        stp->st_access_bmap = 0;
        stp->st_deny_bmap = open_stp->st_deny_bmap;
        stp->st_openstp = open_stp;
+       spin_lock(&oo->oo_owner.so_client->cl_lock);
        list_add(&stp->st_locks, &open_stp->st_locks);
+       list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
        spin_lock(&fp->fi_lock);
        list_add(&stp->st_perfile, &fp->fi_stateids);
        spin_unlock(&fp->fi_lock);
+       spin_unlock(&oo->oo_owner.so_client->cl_lock);
        return stp;
 }