knfsd: cleanup of nfsd4 cmp_* functions
[linux-2.6-block.git] / fs / nfsd / nfs4state.c
index 3cc8ce422ab1a245a746cc15401be505031f17c2..578d809e85ec921437e5d3262accfb0eca153406 100644 (file)
 #include <linux/nfsd/state.h>
 #include <linux/nfsd/xdr4.h>
 #include <linux/namei.h>
+#include <linux/swap.h>
 #include <linux/mutex.h>
 #include <linux/lockd/bind.h>
+#include <linux/module.h>
 
 #define NFSDDBG_FACILITY                NFSDDBG_PROC
 
@@ -149,6 +151,7 @@ get_nfs4_file(struct nfs4_file *fi)
 }
 
 static int num_delegations;
+unsigned int max_delegations;
 
 /*
  * Open owner state (share locks)
@@ -192,7 +195,9 @@ 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 (num_delegations > STATEID_HASH_SIZE * 4)
+       if (fp->fi_had_conflict)
+               return NULL;
+       if (num_delegations > max_delegations)
                return NULL;
        dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
        if (dp == NULL)
@@ -251,7 +256,7 @@ nfs4_close_delegation(struct nfs4_delegation *dp)
        /* The following nfsd_close may not actually close the file,
         * but we want to remove the lease in any case. */
        if (dp->dl_flock)
-               setlease(filp, F_UNLCK, &dp->dl_flock);
+               vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
        nfsd_close(filp);
 }
 
@@ -378,7 +383,6 @@ shutdown_callback_client(struct nfs4_client *clp)
        if (clnt) {
                clp->cl_callback.cb_client = NULL;
                rpc_shutdown_client(clnt);
-               rpciod_down();
        }
 }
 
@@ -458,26 +462,28 @@ copy_cred(struct svc_cred *target, struct svc_cred *source) {
 }
 
 static inline int
-same_name(const char *n1, const char *n2) {
+same_name(const char *n1, const char *n2)
+{
        return 0 == memcmp(n1, n2, HEXDIR_LEN);
 }
 
 static int
-cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
-       return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
+same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
+{
+       return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
 }
 
 static int
-cmp_clid(clientid_t * cl1, clientid_t * cl2) {
-       return((cl1->cl_boot == cl2->cl_boot) &&
-               (cl1->cl_id == cl2->cl_id));
+same_clid(clientid_t *cl1, clientid_t *cl2)
+{
+       return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
 }
 
 /* XXX what about NGROUP */
 static int
-cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
-       return(cr1->cr_uid == cr2->cr_uid);
-
+same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
+{
+       return cr1->cr_uid == cr2->cr_uid;
 }
 
 static void
@@ -542,7 +548,7 @@ find_confirmed_client(clientid_t *clid)
        unsigned int idhashval = clientid_hashval(clid->cl_id);
 
        list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
-               if (cmp_clid(&clp->cl_clientid, clid))
+               if (same_clid(&clp->cl_clientid, clid))
                        return clp;
        }
        return NULL;
@@ -555,7 +561,7 @@ find_unconfirmed_client(clientid_t *clid)
        unsigned int idhashval = clientid_hashval(clid->cl_id);
 
        list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
-               if (cmp_clid(&clp->cl_clientid, clid))
+               if (same_clid(&clp->cl_clientid, clid))
                        return clp;
        }
        return NULL;
@@ -749,7 +755,7 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                 * or different ip_address
                 */
                status = nfserr_clid_inuse;
-               if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
+               if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)
                                || conf->cl_addr != sin->sin_addr.s_addr) {
                        dprintk("NFSD: setclientid: string in use by client"
                                "at %u.%u.%u.%u\n", NIPQUAD(conf->cl_addr));
@@ -775,7 +781,7 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                gen_confirm(new);
                gen_callback(new, setclid);
                add_to_unconfirmed(new, strhashval);
-       } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
+       } else if (same_verf(&conf->cl_verifier, &clverifier)) {
                /*
                 * CASE 1:
                 * cl_name match, confirmed, principal match
@@ -826,7 +832,7 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                gen_confirm(new);
                gen_callback(new, setclid);
                add_to_unconfirmed(new, strhashval);
-       } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
+       } else if (!same_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
                /*      
                 * CASE3:
                 * confirmed found (name, principal match)
@@ -906,16 +912,16 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                goto out;
 
        if ((conf && unconf) && 
-           (cmp_verf(&unconf->cl_confirm, &confirm)) &&
-           (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
+           (same_verf(&unconf->cl_confirm, &confirm)) &&
+           (same_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
            (same_name(conf->cl_recdir,unconf->cl_recdir))  &&
-           (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
+           (!same_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
                /* CASE 1:
                * unconf record that matches input clientid and input confirm.
                * conf record that matches input clientid.
                * conf and unconf records match names, verifiers
                */
-               if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred)) 
+               if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
                        status = nfserr_clid_inuse;
                else {
                        /* XXX: We just turn off callbacks until we can handle
@@ -929,7 +935,7 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                }
        } else if ((conf && !unconf) ||
            ((conf && unconf) && 
-            (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
+            (!same_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
              !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
                /* CASE 2:
                 * conf record that matches input clientid.
@@ -937,18 +943,18 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                 * unconf->cl_name or unconf->cl_verifier don't match the
                 * conf record.
                 */
-               if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
+               if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
                        status = nfserr_clid_inuse;
                else
                        status = nfs_ok;
        } else if (!conf && unconf
-                       && cmp_verf(&unconf->cl_confirm, &confirm)) {
+                       && same_verf(&unconf->cl_confirm, &confirm)) {
                /* CASE 3:
                 * conf record not found.
                 * unconf record found.
                 * unconf->cl_confirm matches input confirm
                 */
-               if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
+               if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
                        status = nfserr_clid_inuse;
                } else {
                        unsigned int hash =
@@ -963,8 +969,8 @@ nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
                        conf = unconf;
                        status = nfs_ok;
                }
-       } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
-           && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
+       } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
+           && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
                                                                &confirm)))) {
                /* CASE 4:
                 * conf record not found, or if conf, conf->cl_confirm does not
@@ -1000,6 +1006,7 @@ alloc_init_file(struct inode *ino)
                list_add(&fp->fi_hash, &file_hashtbl[hashval]);
                fp->fi_inode = igrab(ino);
                fp->fi_id = current_fileid++;
+               fp->fi_had_conflict = false;
                return fp;
        }
        return NULL;
@@ -1027,19 +1034,19 @@ static int
 nfsd4_init_slabs(void)
 {
        stateowner_slab = kmem_cache_create("nfsd4_stateowners",
-                       sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
+                       sizeof(struct nfs4_stateowner), 0, 0, NULL);
        if (stateowner_slab == NULL)
                goto out_nomem;
        file_slab = kmem_cache_create("nfsd4_files",
-                       sizeof(struct nfs4_file), 0, 0, NULL, NULL);
+                       sizeof(struct nfs4_file), 0, 0, NULL);
        if (file_slab == NULL)
                goto out_nomem;
        stateid_slab = kmem_cache_create("nfsd4_stateids",
-                       sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
+                       sizeof(struct nfs4_stateid), 0, 0, NULL);
        if (stateid_slab == NULL)
                goto out_nomem;
        deleg_slab = kmem_cache_create("nfsd4_delegations",
-                       sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
+                       sizeof(struct nfs4_delegation), 0, 0, NULL);
        if (deleg_slab == NULL)
                goto out_nomem;
        return 0;
@@ -1202,10 +1209,12 @@ move_to_close_lru(struct nfs4_stateowner *sop)
 }
 
 static int
-cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
-       return ((sop->so_owner.len == owner->len) && 
-        !memcmp(sop->so_owner.data, owner->data, owner->len) && 
-         (sop->so_client->cl_clientid.cl_id == clid->cl_id));
+same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
+                                                       clientid_t *clid)
+{
+       return (sop->so_owner.len == owner->len) &&
+               0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
+               (sop->so_client->cl_clientid.cl_id == clid->cl_id);
 }
 
 static struct nfs4_stateowner *
@@ -1214,7 +1223,7 @@ find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
        struct nfs4_stateowner *so = NULL;
 
        list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
-               if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
+               if (same_owner_str(so, &open->op_owner, &open->op_clientid))
                        return so;
        }
        return NULL;
@@ -1326,6 +1335,7 @@ do_recall(void *__dp)
 {
        struct nfs4_delegation *dp = __dp;
 
+       dp->dl_file->fi_had_conflict = true;
        nfsd4_cb_recall(dp);
        return 0;
 }
@@ -1396,7 +1406,7 @@ void nfsd_release_deleg_cb(struct file_lock *fl)
 /*
  * Set the delegation file_lock back pointer.
  *
- * Called from __setlease() with lock_kernel() held.
+ * Called from setlease() with lock_kernel() held.
  */
 static
 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
@@ -1410,7 +1420,7 @@ void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
 }
 
 /*
- * Called from __setlease() with lock_kernel() held
+ * Called from setlease() with lock_kernel() held
  */
 static
 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
@@ -1710,10 +1720,10 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
        fl.fl_file = stp->st_vfs_file;
        fl.fl_pid = current->tgid;
 
-       /* setlease checks to see if delegation should be handed out.
+       /* vfs_setlease checks to see if delegation should be handed out.
         * the lock_manager callbacks fl_mylease and fl_change are used
         */
-       if ((status = setlease(stp->st_vfs_file,
+       if ((status = vfs_setlease(stp->st_vfs_file,
                flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
                dprintk("NFSD: setlease failed [%d], no delegation\n", status);
                unhash_delegation(dp);
@@ -2175,21 +2185,20 @@ nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *statei
                lkflg = setlkflg(lock->lk_type);
 
                if (lock->lk_is_new) {
-                       if (!sop->so_is_open_owner)
-                              return nfserr_bad_stateid;
-                       if (!cmp_clid(&clp->cl_clientid, lockclid))
+                       if (!sop->so_is_open_owner)
+                               return nfserr_bad_stateid;
+                       if (!same_clid(&clp->cl_clientid, lockclid))
                               return nfserr_bad_stateid;
-                       /* stp is the open stateid */
-                       status = nfs4_check_openmode(stp, lkflg);
-                       if (status)
-                              return status;
-               } else {
-                       /* stp is the lock stateid */
-                       status = nfs4_check_openmode(stp->st_openstp, lkflg);
-                       if (status)
-                              return status;
+                       /* stp is the open stateid */
+                       status = nfs4_check_openmode(stp, lkflg);
+                       if (status)
+                               return status;
+               } else {
+                       /* stp is the lock stateid */
+                       status = nfs4_check_openmode(stp->st_openstp, lkflg);
+                       if (status)
+                               return status;
                }
-
        }
 
        if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
@@ -2555,7 +2564,7 @@ find_lockstateowner_str(struct inode *inode, clientid_t *clid,
        struct nfs4_stateowner *op;
 
        list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
-               if (cmp_owner_str(op, owner, clid))
+               if (same_owner_str(op, owner, clid))
                        return op;
        }
        return NULL;
@@ -3019,7 +3028,7 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp,
        INIT_LIST_HEAD(&matches);
        for (i = 0; i < LOCK_HASH_SIZE; i++) {
                list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
-                       if (!cmp_owner_str(sop, owner, clid))
+                       if (!same_owner_str(sop, owner, clid))
                                continue;
                        list_for_each_entry(stp, &sop->so_stateids,
                                        st_perstateowner) {
@@ -3191,20 +3200,49 @@ nfsd4_load_reboot_recovery_data(void)
                printk("NFSD: Failure reading reboot recovery data\n");
 }
 
+unsigned long
+get_nfs4_grace_period(void)
+{
+       return max(user_lease_time, lease_time) * HZ;
+}
+
+/*
+ * Since the lifetime of a delegation isn't limited to that of an open, a
+ * client may quite reasonably hang on to a delegation as long as it has
+ * the inode cached.  This becomes an obvious problem the first time a
+ * client's inode cache approaches the size of the server's total memory.
+ *
+ * For now we avoid this problem by imposing a hard limit on the number
+ * of delegations, which varies according to the server's memory size.
+ */
+static void
+set_max_delegations(void)
+{
+       /*
+        * Allow at most 4 delegations per megabyte of RAM.  Quick
+        * estimates suggest that in the worst case (where every delegation
+        * is for a different inode), a delegation could take about 1.5K,
+        * giving a worst case usage of about 6% of memory.
+        */
+       max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
+}
+
 /* initialization to perform when the nfsd service is started: */
 
 static void
 __nfs4_state_start(void)
 {
-       time_t grace_time;
+       unsigned long grace_time;
 
        boot_time = get_seconds();
-       grace_time = max(user_lease_time, lease_time);
+       grace_time = get_nfs_grace_period();
        lease_time = user_lease_time;
        in_grace = 1;
-       printk("NFSD: starting %ld-second grace period\n", grace_time);
+       printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
+              grace_time/HZ);
        laundry_wq = create_singlethread_workqueue("nfsd4");
-       queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
+       queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
+       set_max_delegations();
 }
 
 int