nfsd4: allow nfs4 state startup to fail
[safe/jmp/linux-2.6] / fs / nfsd / nfssvc.c
index 221eeaa..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))
-/* if the last thread dies with SIGHUP, then the exports table is
- * left unchanged ( like 2.4-{0-9} ).  Any other signal will clear
- * the exports table (like 2.2).
- */
-#define        SIG_NOCLEAN     SIGHUP
-
 extern struct svc_program      nfsd_program;
-static void                    nfsd(struct svc_rqst *rqstp);
+static int                     nfsd(void *vrqstp);
 struct timeval                 nfssvc_boot;
-       struct svc_serv                 *nfsd_serv;
-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
+ * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
+ * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
+ *
+ * If (out side the lock) nfsd_serv is non-NULL, then it must point to a
+ * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
+ * of nfsd threads must exist and each must listed in ->sp_all_threads in each
+ * entry of ->sv_pools[].
+ *
+ * Transitions of the thread count between zero and non-zero are of particular
+ * interest since the svc_serv needs to be created and initialized at that
+ * point, or freed.
+ *
+ * Finally, the nfsd_mutex also protects some of the global variables that are
+ * accessed when nfsd starts and that are settable via the write_* routines in
+ * nfsctl.c. In particular:
+ *
+ *     user_recovery_dirname
+ *     user_lease_time
+ *     nfsd_versions
+ */
+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;
@@ -112,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)
@@ -138,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
  */
@@ -145,28 +188,27 @@ 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 int killsig;    /* signal that was used to kill last nfsd */
 static void nfsd_last_thread(struct svc_serv *serv)
 {
        /* When last nfsd thread exits we need to do some clean-up */
-       struct svc_sock *svsk;
-       list_for_each_entry(svsk, &serv->sv_permsocks, sk_list)
+       struct svc_xprt *xprt;
+       list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list)
                lockd_down();
        nfsd_serv = NULL;
        nfsd_racache_shutdown();
        nfs4_state_shutdown();
 
-       printk(KERN_WARNING "nfsd: last server has exited\n");
-       if (killsig != SIG_NOCLEAN) {
-               printk(KERN_WARNING "nfsd: unexporting all filesystems\n");
-               nfsd_export_flush();
-       }
+       printk(KERN_WARNING "nfsd: last server has exited, flushing export "
+                           "cache\n");
+       nfsd_export_flush();
 }
 
 void nfsd_reset_versions(void)
@@ -190,13 +232,35 @@ 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)
 {
        int err = 0;
-       lock_kernel();
+
+       WARN_ON(!mutex_is_locked(&nfsd_mutex));
        if (nfsd_serv) {
                svc_get(nfsd_serv);
-               unlock_kernel();
                return 0;
        }
        if (nfsd_max_blksize == 0) {
@@ -216,14 +280,13 @@ 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, SIG_NOCLEAN, THIS_MODULE);
+       nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
+                                     nfsd_last_thread, nfsd, THIS_MODULE);
        if (nfsd_serv == NULL)
                err = -ENOMEM;
-       unlock_kernel();
+       else
+               set_max_drc();
+
        do_gettimeofday(&nfssvc_boot);          /* record boot time */
        return err;
 }
@@ -234,27 +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_makesock(nfsd_serv, IPPROTO_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;
 
-#ifdef CONFIG_NFSD_TCP
-       error = lockd_up(IPPROTO_TCP);
-       if (error >= 0) {
-               error = svc_makesock(nfsd_serv, IPPROTO_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;
-#endif
+
+       error = lockd_up();
+       if (error < 0)
+               return error;
+
        return 0;
 }
 
@@ -284,6 +344,8 @@ int nfsd_set_nrthreads(int n, int *nthreads)
        int tot = 0;
        int err = 0;
 
+       WARN_ON(!mutex_is_locked(&nfsd_mutex));
+
        if (nfsd_serv == NULL || n <= 0)
                return 0;
 
@@ -318,7 +380,6 @@ int nfsd_set_nrthreads(int n, int *nthreads)
                nthreads[0] = 1;
 
        /* apply the new numbers */
-       lock_kernel();
        svc_get(nfsd_serv);
        for (i = 0; i < n; i++) {
                err = svc_set_num_threads(nfsd_serv, &nfsd_serv->sv_pools[i],
@@ -327,7 +388,6 @@ int nfsd_set_nrthreads(int n, int *nthreads)
                        break;
        }
        svc_destroy(nfsd_serv);
-       unlock_kernel();
 
        return err;
 }
@@ -336,21 +396,23 @@ int
 nfsd_svc(unsigned short port, int nrservs)
 {
        int     error;
-       
-       lock_kernel();
+
+       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;
        error = nfs4_state_start();
-       if (error<0)
+       if (error)
                goto out;
 
        nfsd_reset_versions();
@@ -364,68 +426,53 @@ 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:
-       unlock_kernel();
+       mutex_unlock(&nfsd_mutex);
        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
  */
-static void
-nfsd(struct svc_rqst *rqstp)
+static int
+nfsd(void *vrqstp)
 {
-       struct fs_struct *fsp;
-       int             err;
-       sigset_t shutdown_mask, allowed_mask;
+       struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
+       int err, preverr = 0;
 
        /* Lock module and set up kernel thread */
-       lock_kernel();
-       daemonize("nfsd");
+       mutex_lock(&nfsd_mutex);
 
-       /* After daemonize() this kernel thread shares current->fs
+       /* 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);
+       /*
+        * thread is spawned with all signals set to SIG_IGN, re-enable
+        * the ones that will bring down the thread
+        */
+       allow_signal(SIGKILL);
+       allow_signal(SIGHUP);
+       allow_signal(SIGINT);
+       allow_signal(SIGQUIT);
 
        nfsdstats.th_cnt++;
-
-       rqstp->rq_task = current;
-
-       unlock_kernel();
+       mutex_unlock(&nfsd_mutex);
 
        /*
         * We want less throttling in balance_dirty_pages() so that nfs to
@@ -439,50 +486,38 @@ nfsd(struct svc_rqst *rqstp)
         * 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.
                 */
                while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
                        ;
-               if (err < 0)
+               if (err == -EINTR)
                        break;
-               update_thread_usage(atomic_read(&nfsd_busy));
-               atomic_inc(&nfsd_busy);
+               else if (err < 0) {
+                       if (err != preverr) {
+                               printk(KERN_WARNING "%s: unexpected error "
+                                       "from svc_recv (%d)\n", __func__, -err);
+                               preverr = err;
+                       }
+                       schedule_timeout_uninterruptible(HZ);
+                       continue;
+               }
+
 
                /* 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);
        }
 
-       if (err != -EINTR) {
-               printk(KERN_WARNING "nfsd: terminating on error %d\n", -err);
-       } else {
-               unsigned int    signo;
-
-               for (signo = 1; signo <= _NSIG; signo++)
-                       if (sigismember(&current->pending.signal, signo) &&
-                           !sigismember(&current->blocked, signo))
-                               break;
-               killsig = signo;
-       }
        /* Clear signals before calling svc_exit_thread() */
        flush_signals(current);
 
-       lock_kernel();
-
+       mutex_lock(&nfsd_mutex);
        nfsdstats.th_cnt --;
 
 out:
@@ -490,8 +525,9 @@ out:
        svc_exit_thread(rqstp);
 
        /* Release module */
-       unlock_kernel();
+       mutex_unlock(&nfsd_mutex);
        module_put_and_exit(0);
+       return 0;
 }
 
 static __be32 map_new_errors(u32 vers, __be32 nfserr)
@@ -546,8 +582,6 @@ nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
        /* Now call the procedure handler, and encode NFS status. */
        nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
        nfserr = map_new_errors(rqstp->rq_vers, nfserr);
-       if (nfserr == nfserr_jukebox && rqstp->rq_vers == 2)
-               nfserr = nfserr_dropit;
        if (nfserr == nfserr_dropit) {
                dprintk("nfsd: Dropping request; may be revisited later\n");
                nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
@@ -576,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;
+}