[PATCH] nfsd4: remove debugging counters
[powerpc.git] / fs / nfsd / nfs4state.c
index 75e8b13..f03a418 100644 (file)
@@ -65,18 +65,6 @@ static u32 nfs4_init;
 stateid_t zerostateid;             /* bits all 0 */
 stateid_t onestateid;              /* bits all 1 */
 
-/* debug counters */
-u32 list_add_perfile = 0; 
-u32 list_del_perfile = 0;
-u32 add_perclient = 0;
-u32 del_perclient = 0;
-u32 alloc_file = 0;
-u32 free_file = 0;
-u32 vfsopen = 0;
-u32 vfsclose = 0;
-u32 alloc_delegation= 0;
-u32 free_delegation= 0;
-
 /* forward declarations */
 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
@@ -90,6 +78,11 @@ static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
  */
 static DECLARE_MUTEX(client_sema);
 
+kmem_cache_t *stateowner_slab = NULL;
+kmem_cache_t *file_slab = NULL;
+kmem_cache_t *stateid_slab = NULL;
+kmem_cache_t *deleg_slab = NULL;
+
 void
 nfs4_lock_state(void)
 {
@@ -136,8 +129,8 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_f
        struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
 
        dprintk("NFSD alloc_init_deleg\n");
-       if ((dp = kmalloc(sizeof(struct nfs4_delegation),
-               GFP_KERNEL)) == NULL)
+       dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
+       if (dp == NULL)
                return dp;
        INIT_LIST_HEAD(&dp->dl_del_perfile);
        INIT_LIST_HEAD(&dp->dl_del_perclnt);
@@ -162,7 +155,6 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_f
        atomic_set(&dp->dl_count, 1);
        list_add(&dp->dl_del_perfile, &fp->fi_del_perfile);
        list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
-       alloc_delegation++;
        return dp;
 }
 
@@ -171,8 +163,7 @@ nfs4_put_delegation(struct nfs4_delegation *dp)
 {
        if (atomic_dec_and_test(&dp->dl_count)) {
                dprintk("NFSD: freeing dp %p\n",dp);
-               kfree(dp);
-               free_delegation++;
+               kmem_cache_free(deleg_slab, dp);
        }
 }
 
@@ -193,7 +184,6 @@ nfs4_close_delegation(struct nfs4_delegation *dp)
        if (dp->dl_flock)
                setlease(filp, F_UNLCK, &dp->dl_flock);
        nfsd_close(filp);
-       vfsclose++;
 }
 
 /* Called under the state lock. */
@@ -961,14 +951,14 @@ alloc_init_file(struct inode *ino)
        struct nfs4_file *fp;
        unsigned int hashval = file_hashval(ino);
 
-       if ((fp = kmalloc(sizeof(struct nfs4_file),GFP_KERNEL))) {
+       fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
+       if (fp) {
                INIT_LIST_HEAD(&fp->fi_hash);
                INIT_LIST_HEAD(&fp->fi_perfile);
                INIT_LIST_HEAD(&fp->fi_del_perfile);
                list_add(&fp->fi_hash, &file_hashtbl[hashval]);
                fp->fi_inode = igrab(ino);
                fp->fi_id = current_fileid++;
-               alloc_file++;
                return fp;
        }
        return NULL;
@@ -992,29 +982,51 @@ release_all_files(void)
        }
 }
 
-kmem_cache_t *stateowner_slab = NULL;
-
-static int
-nfsd4_init_slabs(void)
+static void
+nfsd4_free_slab(kmem_cache_t **slab)
 {
-       stateowner_slab = kmem_cache_create("nfsd4_stateowners",
-                       sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
-       if (stateowner_slab == NULL) {
-               dprintk("nfsd4: out of memory while initializing nfsv4\n");
-               return -ENOMEM;
-       }
-       return 0;
+       int status;
+
+       if (*slab == NULL)
+               return;
+       status = kmem_cache_destroy(*slab);
+       *slab = NULL;
+       WARN_ON(status);
 }
 
 static void
 nfsd4_free_slabs(void)
 {
-       int status = 0;
+       nfsd4_free_slab(&stateowner_slab);
+       nfsd4_free_slab(&file_slab);
+       nfsd4_free_slab(&stateid_slab);
+       nfsd4_free_slab(&deleg_slab);
+}
 
-       if (stateowner_slab)
-               status = kmem_cache_destroy(stateowner_slab);
-       stateowner_slab = NULL;
-       BUG_ON(status);
+static int
+nfsd4_init_slabs(void)
+{
+       stateowner_slab = kmem_cache_create("nfsd4_stateowners",
+                       sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
+       if (stateowner_slab == NULL)
+               goto out_nomem;
+       file_slab = kmem_cache_create("nfsd4_files",
+                       sizeof(struct nfs4_file), 0, 0, NULL, NULL);
+       if (file_slab == NULL)
+               goto out_nomem;
+       stateid_slab = kmem_cache_create("nfsd4_stateids",
+                       sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
+       if (stateid_slab == NULL)
+               goto out_nomem;
+       deleg_slab = kmem_cache_create("nfsd4_delegations",
+                       sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
+       if (deleg_slab == NULL)
+               goto out_nomem;
+       return 0;
+out_nomem:
+       nfsd4_free_slabs();
+       dprintk("nfsd4: out of memory while initializing nfsv4\n");
+       return -ENOMEM;
 }
 
 void
@@ -1062,7 +1074,6 @@ alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, str
        list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
        list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
        list_add(&sop->so_perclient, &clp->cl_perclient);
-       add_perclient++;
        sop->so_is_open_owner = 1;
        sop->so_id = current_ownerid++;
        sop->so_client = clp;
@@ -1096,10 +1107,8 @@ unhash_stateowner(struct nfs4_stateowner *sop)
 
        list_del(&sop->so_idhash);
        list_del(&sop->so_strhash);
-       if (sop->so_is_open_owner) {
+       if (sop->so_is_open_owner)
                list_del(&sop->so_perclient);
-               del_perclient++;
-       }
        list_del(&sop->so_perlockowner);
        while (!list_empty(&sop->so_perfilestate)) {
                stp = list_entry(sop->so_perfilestate.next, 
@@ -1130,7 +1139,6 @@ init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *
        INIT_LIST_HEAD(&stp->st_perfile);
        list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
        list_add(&stp->st_perfilestate, &sop->so_perfilestate);
-       list_add_perfile++;
        list_add(&stp->st_perfile, &fp->fi_perfile);
        stp->st_stateowner = sop;
        stp->st_file = fp;
@@ -1150,27 +1158,24 @@ release_stateid(struct nfs4_stateid *stp, int flags)
        struct file *filp = stp->st_vfs_file;
 
        list_del(&stp->st_hash);
-       list_del_perfile++;
        list_del(&stp->st_perfile);
        list_del(&stp->st_perfilestate);
        if (flags & OPEN_STATE) {
                release_stateid_lockowners(stp);
                stp->st_vfs_file = NULL;
                nfsd_close(filp);
-               vfsclose++;
        } else if (flags & LOCK_STATE)
                locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
-       kfree(stp);
+       kmem_cache_free(stateid_slab, stp);
        stp = NULL;
 }
 
 static void
 release_file(struct nfs4_file *fp)
 {
-       free_file++;
        list_del(&fp->fi_hash);
        iput(fp->fi_inode);
-       kfree(fp);
+       kmem_cache_free(file_slab, fp);
 }      
 
 void
@@ -1526,6 +1531,51 @@ out:
        return status;
 }
 
+static inline int
+nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
+{
+       if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
+               return nfserr_openmode;
+       else
+               return nfs_ok;
+}
+
+static struct nfs4_delegation *
+find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
+{
+       struct nfs4_delegation *dp;
+
+       list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
+               if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
+                       return dp;
+       }
+       return NULL;
+}
+
+static int
+nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
+               struct nfs4_delegation **dp)
+{
+       int flags;
+       int status = nfserr_bad_stateid;
+
+       *dp = find_delegation_file(fp, &open->op_delegate_stateid);
+       if (*dp == NULL)
+               goto out;
+       flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
+                                               RD_STATE : WR_STATE;
+       status = nfs4_check_delegmode(*dp, flags);
+       if (status)
+               *dp = NULL;
+out:
+       if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
+               return nfs_ok;
+       if (status)
+               return status;
+       open->op_stateowner->so_confirmed = 1;
+       return nfs_ok;
+}
+
 static int
 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
 {
@@ -1549,25 +1599,37 @@ out:
        return status;
 }
 
+static inline struct nfs4_stateid *
+nfs4_alloc_stateid(void)
+{
+       return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
+}
+
 static int
 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
+               struct nfs4_delegation *dp,
                struct svc_fh *cur_fh, int flags)
 {
        struct nfs4_stateid *stp;
-       int status;
 
-       stp = kmalloc(sizeof(struct nfs4_stateid), GFP_KERNEL);
+       stp = nfs4_alloc_stateid();
        if (stp == NULL)
                return nfserr_resource;
 
-       status = nfsd_open(rqstp, cur_fh, S_IFREG, flags, &stp->st_vfs_file);
-       if (status) {
-               if (status == nfserr_dropit)
-                       status = nfserr_jukebox;
-               kfree(stp);
-               return status;
+       if (dp) {
+               get_file(dp->dl_vfs_file);
+               stp->st_vfs_file = dp->dl_vfs_file;
+       } else {
+               int status;
+               status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
+                               &stp->st_vfs_file);
+               if (status) {
+                       if (status == nfserr_dropit)
+                               status = nfserr_jukebox;
+                       kmem_cache_free(stateid_slab, stp);
+                       return status;
+               }
        }
-       vfsopen++;
        *stpp = stp;
        return 0;
 }
@@ -1699,6 +1761,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        struct nfs4_file *fp = NULL;
        struct inode *ino = current_fh->fh_dentry->d_inode;
        struct nfs4_stateid *stp = NULL;
+       struct nfs4_delegation *dp = NULL;
        int status;
 
        status = nfserr_inval;
@@ -1713,7 +1776,13 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
        if (fp) {
                if ((status = nfs4_check_open(fp, open, &stp)))
                        goto out;
+               status = nfs4_check_deleg(fp, open, &dp);
+               if (status)
+                       goto out;
        } else {
+               status = nfserr_bad_stateid;
+               if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
+                       goto out;
                status = nfserr_resource;
                fp = alloc_init_file(ino);
                if (fp == NULL)
@@ -1736,7 +1805,8 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
                        flags = MAY_WRITE;
                else
                        flags = MAY_READ;
-               if ((status = nfs4_new_open(rqstp, &stp, current_fh, flags)))
+               status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
+               if (status)
                        goto out;
                init_stateid(stp, fp, open);
                status = nfsd4_truncate(rqstp, current_fh, open);
@@ -1948,15 +2018,6 @@ out:
        return status;
 }
 
-static inline int
-nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
-{
-       if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
-               return nfserr_openmode;
-       else
-               return nfs_ok;
-}
-
 static inline int
 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
 {
@@ -2419,24 +2480,15 @@ find_stateid(stateid_t *stid, int flags)
 static struct nfs4_delegation *
 find_delegation_stateid(struct inode *ino, stateid_t *stid)
 {
-       struct nfs4_delegation *dp = NULL;
        struct nfs4_file *fp = NULL;
-       u32 st_id;
 
        dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
                     stid->si_boot, stid->si_stateownerid,
                     stid->si_fileid, stid->si_generation);
 
-       st_id = stid->si_stateownerid;
        fp = find_file(ino);
-       if (fp) {
-               list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
-                       if(dp->dl_stateid.si_stateownerid == st_id) {
-                               dprintk("NFSD: find_delegation dp %p\n",dp);
-                               return dp;
-                       }
-               }
-       }
+       if (fp)
+               return find_delegation_file(fp, stid);
        return NULL;
 }
 
@@ -2573,8 +2625,8 @@ alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struc
        struct nfs4_stateid *stp;
        unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
 
-       if ((stp = kmalloc(sizeof(struct nfs4_stateid), 
-                                       GFP_KERNEL)) == NULL)
+       stp = nfs4_alloc_stateid();
+       if (stp == NULL)
                goto out;
        INIT_LIST_HEAD(&stp->st_hash);
        INIT_LIST_HEAD(&stp->st_perfile);
@@ -2582,7 +2634,6 @@ alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struc
        INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
        list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
        list_add(&stp->st_perfile, &fp->fi_perfile);
-       list_add_perfile++;
        list_add(&stp->st_perfilestate, &sop->so_perfilestate);
        stp->st_stateowner = sop;
        stp->st_file = fp;
@@ -3240,17 +3291,6 @@ __nfs4_state_shutdown(void)
        cancel_delayed_work(&laundromat_work);
        flush_scheduled_work();
        nfs4_init = 0;
-       dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
-                       list_add_perfile, list_del_perfile);
-       dprintk("NFSD: add_perclient %d del_perclient %d\n",
-                       add_perclient, del_perclient);
-       dprintk("NFSD: alloc_file %d free_file %d\n",
-                       alloc_file, free_file);
-       dprintk("NFSD: vfsopen %d vfsclose %d\n",
-                       vfsopen, vfsclose);
-       dprintk("NFSD: alloc_delegation %d free_delegation %d\n",
-                       alloc_delegation, free_delegation);
-
 }
 
 void