nfsd: fix horrible indentation in nfsd_setattr
[safe/jmp/linux-2.6] / fs / nfsd / nfs4state.c
index 03b0578..3f55970 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
 
@@ -148,6 +151,7 @@ get_nfs4_file(struct nfs4_file *fi)
 }
 
 static int num_delegations;
+unsigned int max_delegations;
 
 /*
  * Open owner state (share locks)
@@ -191,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)
@@ -250,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);
 }
 
@@ -377,7 +383,6 @@ shutdown_callback_client(struct nfs4_client *clp)
        if (clnt) {
                clp->cl_callback.cb_client = NULL;
                rpc_shutdown_client(clnt);
-               rpciod_down();
        }
 }
 
@@ -999,6 +1004,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;
@@ -1026,19 +1032,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;
@@ -1325,8 +1331,7 @@ do_recall(void *__dp)
 {
        struct nfs4_delegation *dp = __dp;
 
-       daemonize("nfsv4-recall");
-
+       dp->dl_file->fi_had_conflict = true;
        nfsd4_cb_recall(dp);
        return 0;
 }
@@ -1397,7 +1402,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)
@@ -1411,7 +1416,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)
@@ -1711,10 +1716,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);
@@ -2773,7 +2778,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        /* XXX?: Just to divert the locks_release_private at the start of
         * locks_copy_lock: */
        locks_init_lock(&conflock);
-       err = posix_lock_file(filp, &file_lock, &conflock);
+       err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
        switch (-err) {
        case 0: /* success! */
                update_stateid(&lock_stp->st_stateid);
@@ -2790,7 +2795,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                status = nfserr_deadlock;
                break;
        default:        
-               dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",err);
+               dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
                status = nfserr_resource;
                break;
        }
@@ -2815,6 +2820,7 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        struct inode *inode;
        struct file file;
        struct file_lock file_lock;
+       int error;
        __be32 status;
 
        if (nfs4_in_grace())
@@ -2870,16 +2876,20 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
        nfs4_transform_lock_offset(&file_lock);
 
-       /* posix_test_lock uses the struct file _only_ to resolve the inode.
+       /* vfs_test_lock uses the struct file _only_ to resolve the inode.
         * since LOCKT doesn't require an OPEN, and therefore a struct
-        * file may not exist, pass posix_test_lock a struct file with
+        * file may not exist, pass vfs_test_lock a struct file with
         * only the dentry:inode set.
         */
        memset(&file, 0, sizeof (struct file));
        file.f_path.dentry = cstate->current_fh.fh_dentry;
 
        status = nfs_ok;
-       posix_test_lock(&file, &file_lock);
+       error = vfs_test_lock(&file, &file_lock);
+       if (error) {
+               status = nfserrno(error);
+               goto out;
+       }
        if (file_lock.fl_type != F_UNLCK) {
                status = nfserr_denied;
                nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
@@ -2935,9 +2945,9 @@ nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        /*
        *  Try to unlock the file in the VFS.
        */
-       err = posix_lock_file(filp, &file_lock, NULL);
+       err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
        if (err) {
-               dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
+               dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
                goto out_nfserr;
        }
        /*
@@ -3187,20 +3197,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