nfsd4: allow nfs4 state startup to fail
[safe/jmp/linux-2.6] / fs / nfsd / nfssvc.c
index 26c8114..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
 
-/* these signals will be delivered to an nfsd thread 
- * when handling a request
- */
-#define ALLOWED_SIGS   (sigmask(SIGKILL))
-/* these signals will be delivered to an nfsd thread
- * when not handling a request. i.e. when waiting
- */
-#define SHUTDOWN_SIGS  (sigmask(SIGKILL) | sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT))
-
 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
@@ -78,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[] = {
@@ -132,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)
@@ -158,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
  */
@@ -165,10 +188,12 @@ int nfsd_vers(int vers, enum vers_op change)
 
 int nfsd_nrthreads(void)
 {
-       if (nfsd_serv == NULL)
-               return 0;
-       else
-               return nfsd_serv->sv_nrthreads;
+       int rv = 0;
+       mutex_lock(&nfsd_mutex);
+       if (nfsd_serv)
+               rv = nfsd_serv->sv_nrthreads;
+       mutex_unlock(&nfsd_mutex);
+       return rv;
 }
 
 static void nfsd_last_thread(struct svc_serv *serv)
@@ -207,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)
 {
@@ -234,11 +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,
                                      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;
@@ -250,25 +297,24 @@ static int nfsd_init_socks(int port)
        if (!list_empty(&nfsd_serv->sv_permsocks))
                return 0;
 
-       error = lockd_up(IPPROTO_UDP);
-       if (error >= 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)
-                       lockd_down();
-       }
        if (error < 0)
                return error;
 
-       error = lockd_up(IPPROTO_TCP);
-       if (error >= 0) {
-               error = svc_create_xprt(nfsd_serv, "tcp", port,
+       error = lockd_up();
+       if (error < 0)
+               return error;
+
+       error = svc_create_xprt(nfsd_serv, "tcp", PF_INET, port,
                                        SVC_SOCK_DEFAULTS);
-               if (error < 0)
-                       lockd_down();
-       }
        if (error < 0)
                return error;
+
+       error = lockd_up();
+       if (error < 0)
+               return error;
+
        return 0;
 }
 
@@ -353,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();
 
@@ -376,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:
@@ -383,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
@@ -411,10 +447,7 @@ static int
 nfsd(void *vrqstp)
 {
        struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
-       struct fs_struct *fsp;
-       sigset_t shutdown_mask, allowed_mask;
        int err, preverr = 0;
-       unsigned int signo;
 
        /* Lock module and set up kernel thread */
        mutex_lock(&nfsd_mutex);
@@ -422,26 +455,21 @@ nfsd(void *vrqstp)
        /* At this point, the thread shares current->fs
         * with the init process. We need to create files with a
         * umask of 0 instead of init's umask. */
-       fsp = copy_fs_struct(current->fs);
-       if (!fsp) {
+       if (unshare_fs_struct() < 0) {
                printk("Unable to start nfsd thread: out of memory\n");
                goto out;
        }
-       exit_fs(current);
-       current->fs = fsp;
-       current->fs->umask = 0;
 
-       siginitsetinv(&shutdown_mask, SHUTDOWN_SIGS);
-       siginitsetinv(&allowed_mask, ALLOWED_SIGS);
+       current->fs->umask = 0;
 
        /*
         * thread is spawned with all signals set to SIG_IGN, re-enable
-        * the ones that matter
+        * the ones that will bring down the thread
         */
-       for (signo = 1; signo <= _NSIG; signo++) {
-               if (!sigismember(&shutdown_mask, signo))
-                       allow_signal(signo);
-       }
+       allow_signal(SIGKILL);
+       allow_signal(SIGHUP);
+       allow_signal(SIGINT);
+       allow_signal(SIGQUIT);
 
        nfsdstats.th_cnt++;
        mutex_unlock(&nfsd_mutex);
@@ -458,9 +486,6 @@ nfsd(void *vrqstp)
         * The main request loop
         */
        for (;;) {
-               /* Block all but the shutdown signals */
-               sigprocmask(SIG_SETMASK, &shutdown_mask, NULL);
-
                /*
                 * Find a socket with data available and call its
                 * recvfrom routine.
@@ -479,21 +504,14 @@ nfsd(void *vrqstp)
                        continue;
                }
 
-               update_thread_usage(atomic_read(&nfsd_busy));
-               atomic_inc(&nfsd_busy);
 
                /* Lock the export hash tables for reading. */
                exp_readlock();
 
-               /* Process request with signals blocked. */
-               sigprocmask(SIG_SETMASK, &allowed_mask, NULL);
-
                svc_process(rqstp);
 
                /* Unlock export hash tables */
                exp_readunlock();
-               update_thread_usage(atomic_read(&nfsd_busy));
-               atomic_dec(&nfsd_busy);
        }
 
        /* Clear signals before calling svc_exit_thread() */
@@ -592,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;
+}