nfsd4: allow nfs4 state startup to fail
[safe/jmp/linux-2.6] / fs / nfsd / nfssvc.c
index 144d699..fcc0010 100644 (file)
 #include <linux/unistd.h>
 #include <linux/slab.h>
 #include <linux/smp.h>
-#include <linux/smp_lock.h>
 #include <linux/freezer.h>
 #include <linux/fs_struct.h>
 #include <linux/kthread.h>
+#include <linux/swap.h>
 
 #include <linux/sunrpc/types.h>
 #include <linux/sunrpc/stats.h>
 #include <linux/nfsd/syscall.h>
 #include <linux/lockd/bind.h>
 #include <linux/nfsacl.h>
+#include <linux/seq_file.h>
 
 #define NFSDDBG_FACILITY       NFSDDBG_SVC
 
 extern struct svc_program      nfsd_program;
 static int                     nfsd(void *vrqstp);
 struct timeval                 nfssvc_boot;
-static atomic_t                        nfsd_busy;
-static unsigned long           nfsd_last_call;
-static DEFINE_SPINLOCK(nfsd_call_lock);
 
 /*
  * nfsd_mutex protects nfsd_serv -- both the pointer itself and the members
@@ -69,6 +67,16 @@ static DEFINE_SPINLOCK(nfsd_call_lock);
 DEFINE_MUTEX(nfsd_mutex);
 struct svc_serv                *nfsd_serv;
 
+/*
+ * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
+ * nfsd_drc_max_pages limits the total amount of memory available for
+ * version 4.1 DRC caches.
+ * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
+ */
+spinlock_t     nfsd_drc_lock;
+unsigned int   nfsd_drc_max_mem;
+unsigned int   nfsd_drc_mem_used;
+
 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
 static struct svc_stat nfsd_acl_svcstats;
 static struct svc_version *    nfsd_acl_version[] = {
@@ -123,6 +131,8 @@ struct svc_program          nfsd_program = {
 
 };
 
+u32 nfsd_supported_minorversion;
+
 int nfsd_vers(int vers, enum vers_op change)
 {
        if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
@@ -149,6 +159,28 @@ int nfsd_vers(int vers, enum vers_op change)
        }
        return 0;
 }
+
+int nfsd_minorversion(u32 minorversion, enum vers_op change)
+{
+       if (minorversion > NFSD_SUPPORTED_MINOR_VERSION)
+               return -1;
+       switch(change) {
+       case NFSD_SET:
+               nfsd_supported_minorversion = minorversion;
+               break;
+       case NFSD_CLEAR:
+               if (minorversion == 0)
+                       return -1;
+               nfsd_supported_minorversion = minorversion - 1;
+               break;
+       case NFSD_TEST:
+               return minorversion <= nfsd_supported_minorversion;
+       case NFSD_AVAIL:
+               return minorversion <= NFSD_SUPPORTED_MINOR_VERSION;
+       }
+       return 0;
+}
+
 /*
  * Maximum number of nfsd processes
  */
@@ -200,6 +232,27 @@ void nfsd_reset_versions(void)
        }
 }
 
+/*
+ * Each session guarantees a negotiated per slot memory cache for replies
+ * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
+ * NFSv4.1 server might want to use more memory for a DRC than a machine
+ * with mutiple services.
+ *
+ * Impose a hard limit on the number of pages for the DRC which varies
+ * according to the machines free pages. This is of course only a default.
+ *
+ * For now this is a #defined shift which could be under admin control
+ * in the future.
+ */
+static void set_max_drc(void)
+{
+       #define NFSD_DRC_SIZE_SHIFT     10
+       nfsd_drc_max_mem = (nr_free_buffer_pages()
+                                       >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
+       nfsd_drc_mem_used = 0;
+       spin_lock_init(&nfsd_drc_lock);
+       dprintk("%s nfsd_drc_max_mem %u \n", __func__, nfsd_drc_max_mem);
+}
 
 int nfsd_create_serv(void)
 {
@@ -227,12 +280,12 @@ int nfsd_create_serv(void)
                        nfsd_max_blksize /= 2;
        }
 
-       atomic_set(&nfsd_busy, 0);
        nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
-                                     AF_INET,
                                      nfsd_last_thread, nfsd, THIS_MODULE);
        if (nfsd_serv == NULL)
                err = -ENOMEM;
+       else
+               set_max_drc();
 
        do_gettimeofday(&nfssvc_boot);          /* record boot time */
        return err;
@@ -244,7 +297,7 @@ static int nfsd_init_socks(int port)
        if (!list_empty(&nfsd_serv->sv_permsocks))
                return 0;
 
-       error = svc_create_xprt(nfsd_serv, "udp", port,
+       error = svc_create_xprt(nfsd_serv, "udp", PF_INET, port,
                                        SVC_SOCK_DEFAULTS);
        if (error < 0)
                return error;
@@ -253,7 +306,7 @@ static int nfsd_init_socks(int port)
        if (error < 0)
                return error;
 
-       error = svc_create_xprt(nfsd_serv, "tcp", port,
+       error = svc_create_xprt(nfsd_serv, "tcp", PF_INET, port,
                                        SVC_SOCK_DEFAULTS);
        if (error < 0)
                return error;
@@ -346,17 +399,21 @@ nfsd_svc(unsigned short port, int nrservs)
 
        mutex_lock(&nfsd_mutex);
        dprintk("nfsd: creating service\n");
-       error = -EINVAL;
        if (nrservs <= 0)
                nrservs = 0;
        if (nrservs > NFSD_MAXSERVS)
                nrservs = NFSD_MAXSERVS;
-       
+       error = 0;
+       if (nrservs == 0 && nfsd_serv == NULL)
+               goto out;
+
        /* Readahead param cache - will no-op if it already exists */
        error = nfsd_racache_init(2*nrservs);
        if (error<0)
                goto out;
-       nfs4_state_start();
+       error = nfs4_state_start();
+       if (error)
+               goto out;
 
        nfsd_reset_versions();
 
@@ -369,6 +426,12 @@ nfsd_svc(unsigned short port, int nrservs)
                goto failure;
 
        error = svc_set_num_threads(nfsd_serv, NULL, nrservs);
+       if (error == 0)
+               /* We are holding a reference to nfsd_serv which
+                * we don't want to count in the return value,
+                * so subtract 1
+                */
+               error = nfsd_serv->sv_nrthreads - 1;
  failure:
        svc_destroy(nfsd_serv);         /* Release server */
  out:
@@ -376,26 +439,6 @@ nfsd_svc(unsigned short port, int nrservs)
        return error;
 }
 
-static inline void
-update_thread_usage(int busy_threads)
-{
-       unsigned long prev_call;
-       unsigned long diff;
-       int decile;
-
-       spin_lock(&nfsd_call_lock);
-       prev_call = nfsd_last_call;
-       nfsd_last_call = jiffies;
-       decile = busy_threads*10/nfsdstats.th_cnt;
-       if (decile>0 && decile <= 10) {
-               diff = nfsd_last_call - prev_call;
-               if ( (nfsdstats.th_usage[decile-1] += diff) >= NFSD_USAGE_WRAP)
-                       nfsdstats.th_usage[decile-1] -= NFSD_USAGE_WRAP;
-               if (decile == 10)
-                       nfsdstats.th_fullcnt++;
-       }
-       spin_unlock(&nfsd_call_lock);
-}
 
 /*
  * This is the NFS server kernel thread
@@ -461,8 +504,6 @@ nfsd(void *vrqstp)
                        continue;
                }
 
-               update_thread_usage(atomic_read(&nfsd_busy));
-               atomic_inc(&nfsd_busy);
 
                /* Lock the export hash tables for reading. */
                exp_readlock();
@@ -471,8 +512,6 @@ nfsd(void *vrqstp)
 
                /* Unlock export hash tables */
                exp_readunlock();
-               update_thread_usage(atomic_read(&nfsd_busy));
-               atomic_dec(&nfsd_busy);
        }
 
        /* Clear signals before calling svc_exit_thread() */
@@ -571,3 +610,28 @@ nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
        nfsd_cache_update(rqstp, proc->pc_cachetype, statp + 1);
        return 1;
 }
+
+int nfsd_pool_stats_open(struct inode *inode, struct file *file)
+{
+       int ret;
+       mutex_lock(&nfsd_mutex);
+       if (nfsd_serv == NULL) {
+               mutex_unlock(&nfsd_mutex);
+               return -ENODEV;
+       }
+       /* bump up the psudo refcount while traversing */
+       svc_get(nfsd_serv);
+       ret = svc_pool_stats_open(nfsd_serv, file);
+       mutex_unlock(&nfsd_mutex);
+       return ret;
+}
+
+int nfsd_pool_stats_release(struct inode *inode, struct file *file)
+{
+       int ret = seq_release(inode, file);
+       mutex_lock(&nfsd_mutex);
+       /* this function really, really should have been called svc_put() */
+       svc_destroy(nfsd_serv);
+       mutex_unlock(&nfsd_mutex);
+       return ret;
+}