linux/fs/nfsd/nfssvc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Central processing for nfsd.
   4 *
   5 * Authors:     Olaf Kirch (okir@monad.swb.de)
   6 *
   7 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
   8 */
   9
  10#include <linux/sched/signal.h>
  11#include <linux/freezer.h>
  12#include <linux/module.h>
  13#include <linux/fs_struct.h>
  14#include <linux/swap.h>
  15
  16#include <linux/sunrpc/stats.h>
  17#include <linux/sunrpc/svcsock.h>
  18#include <linux/sunrpc/svc_xprt.h>
  19#include <linux/lockd/bind.h>
  20#include <linux/nfsacl.h>
  21#include <linux/seq_file.h>
  22#include <linux/inetdevice.h>
  23#include <net/addrconf.h>
  24#include <net/ipv6.h>
  25#include <net/net_namespace.h>
  26#include "nfsd.h"
  27#include "cache.h"
  28#include "vfs.h"
  29#include "netns.h"
  30
  31#define NFSDDBG_FACILITY        NFSDDBG_SVC
  32
  33extern struct svc_program       nfsd_program;
  34static int                      nfsd(void *vrqstp);
  35#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  36static int                      nfsd_acl_rpcbind_set(struct net *,
  37                                                     const struct svc_program *,
  38                                                     u32, int,
  39                                                     unsigned short,
  40                                                     unsigned short);
  41static __be32                   nfsd_acl_init_request(struct svc_rqst *,
  42                                                const struct svc_program *,
  43                                                struct svc_process_info *);
  44#endif
  45static int                      nfsd_rpcbind_set(struct net *,
  46                                                 const struct svc_program *,
  47                                                 u32, int,
  48                                                 unsigned short,
  49                                                 unsigned short);
  50static __be32                   nfsd_init_request(struct svc_rqst *,
  51                                                const struct svc_program *,
  52                                                struct svc_process_info *);
  53
  54/*
  55 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
  56 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
  57 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
  58 *
  59 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
  60 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
  61 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
  62 * entry of ->sv_pools[].
  63 *
  64 * Transitions of the thread count between zero and non-zero are of particular
  65 * interest since the svc_serv needs to be created and initialized at that
  66 * point, or freed.
  67 *
  68 * Finally, the nfsd_mutex also protects some of the global variables that are
  69 * accessed when nfsd starts and that are settable via the write_* routines in
  70 * nfsctl.c. In particular:
  71 *
  72 *      user_recovery_dirname
  73 *      user_lease_time
  74 *      nfsd_versions
  75 */
  76DEFINE_MUTEX(nfsd_mutex);
  77
  78/*
  79 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
  80 * nfsd_drc_max_pages limits the total amount of memory available for
  81 * version 4.1 DRC caches.
  82 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
  83 */
  84spinlock_t      nfsd_drc_lock;
  85unsigned long   nfsd_drc_max_mem;
  86unsigned long   nfsd_drc_mem_used;
  87
  88#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  89static struct svc_stat  nfsd_acl_svcstats;
  90static const struct svc_version *nfsd_acl_version[] = {
  91        [2] = &nfsd_acl_version2,
  92        [3] = &nfsd_acl_version3,
  93};
  94
  95#define NFSD_ACL_MINVERS            2
  96#define NFSD_ACL_NRVERS         ARRAY_SIZE(nfsd_acl_version)
  97
  98static struct svc_program       nfsd_acl_program = {
  99        .pg_prog                = NFS_ACL_PROGRAM,
 100        .pg_nvers               = NFSD_ACL_NRVERS,
 101        .pg_vers                = nfsd_acl_version,
 102        .pg_name                = "nfsacl",
 103        .pg_class               = "nfsd",
 104        .pg_stats               = &nfsd_acl_svcstats,
 105        .pg_authenticate        = &svc_set_client,
 106        .pg_init_request        = nfsd_acl_init_request,
 107        .pg_rpcbind_set         = nfsd_acl_rpcbind_set,
 108};
 109
 110static struct svc_stat  nfsd_acl_svcstats = {
 111        .program        = &nfsd_acl_program,
 112};
 113#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
 114
 115static const struct svc_version *nfsd_version[] = {
 116        [2] = &nfsd_version2,
 117#if defined(CONFIG_NFSD_V3)
 118        [3] = &nfsd_version3,
 119#endif
 120#if defined(CONFIG_NFSD_V4)
 121        [4] = &nfsd_version4,
 122#endif
 123};
 124
 125#define NFSD_MINVERS            2
 126#define NFSD_NRVERS             ARRAY_SIZE(nfsd_version)
 127
 128struct svc_program              nfsd_program = {
 129#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
 130        .pg_next                = &nfsd_acl_program,
 131#endif
 132        .pg_prog                = NFS_PROGRAM,          /* program number */
 133        .pg_nvers               = NFSD_NRVERS,          /* nr of entries in nfsd_version */
 134        .pg_vers                = nfsd_version,         /* version table */
 135        .pg_name                = "nfsd",               /* program name */
 136        .pg_class               = "nfsd",               /* authentication class */
 137        .pg_stats               = &nfsd_svcstats,       /* version table */
 138        .pg_authenticate        = &svc_set_client,      /* export authentication */
 139        .pg_init_request        = nfsd_init_request,
 140        .pg_rpcbind_set         = nfsd_rpcbind_set,
 141};
 142
 143static bool
 144nfsd_support_version(int vers)
 145{
 146        if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
 147                return nfsd_version[vers] != NULL;
 148        return false;
 149}
 150
 151static bool *
 152nfsd_alloc_versions(void)
 153{
 154        bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
 155        unsigned i;
 156
 157        if (vers) {
 158                /* All compiled versions are enabled by default */
 159                for (i = 0; i < NFSD_NRVERS; i++)
 160                        vers[i] = nfsd_support_version(i);
 161        }
 162        return vers;
 163}
 164
 165static bool *
 166nfsd_alloc_minorversions(void)
 167{
 168        bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
 169                        sizeof(bool), GFP_KERNEL);
 170        unsigned i;
 171
 172        if (vers) {
 173                /* All minor versions are enabled by default */
 174                for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
 175                        vers[i] = nfsd_support_version(4);
 176        }
 177        return vers;
 178}
 179
 180void
 181nfsd_netns_free_versions(struct nfsd_net *nn)
 182{
 183        kfree(nn->nfsd_versions);
 184        kfree(nn->nfsd4_minorversions);
 185        nn->nfsd_versions = NULL;
 186        nn->nfsd4_minorversions = NULL;
 187}
 188
 189static void
 190nfsd_netns_init_versions(struct nfsd_net *nn)
 191{
 192        if (!nn->nfsd_versions) {
 193                nn->nfsd_versions = nfsd_alloc_versions();
 194                nn->nfsd4_minorversions = nfsd_alloc_minorversions();
 195                if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
 196                        nfsd_netns_free_versions(nn);
 197        }
 198}
 199
 200int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
 201{
 202        if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
 203                return 0;
 204        switch(change) {
 205        case NFSD_SET:
 206                if (nn->nfsd_versions)
 207                        nn->nfsd_versions[vers] = nfsd_support_version(vers);
 208                break;
 209        case NFSD_CLEAR:
 210                nfsd_netns_init_versions(nn);
 211                if (nn->nfsd_versions)
 212                        nn->nfsd_versions[vers] = false;
 213                break;
 214        case NFSD_TEST:
 215                if (nn->nfsd_versions)
 216                        return nn->nfsd_versions[vers];
 217                /* Fallthrough */
 218        case NFSD_AVAIL:
 219                return nfsd_support_version(vers);
 220        }
 221        return 0;
 222}
 223
 224static void
 225nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
 226{
 227        unsigned i;
 228
 229        for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
 230                if (nn->nfsd4_minorversions[i])
 231                        return;
 232        }
 233        nfsd_vers(nn, 4, NFSD_CLEAR);
 234}
 235
 236int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
 237{
 238        if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
 239            change != NFSD_AVAIL)
 240                return -1;
 241
 242        switch(change) {
 243        case NFSD_SET:
 244                if (nn->nfsd4_minorversions) {
 245                        nfsd_vers(nn, 4, NFSD_SET);
 246                        nn->nfsd4_minorversions[minorversion] =
 247                                nfsd_vers(nn, 4, NFSD_TEST);
 248                }
 249                break;
 250        case NFSD_CLEAR:
 251                nfsd_netns_init_versions(nn);
 252                if (nn->nfsd4_minorversions) {
 253                        nn->nfsd4_minorversions[minorversion] = false;
 254                        nfsd_adjust_nfsd_versions4(nn);
 255                }
 256                break;
 257        case NFSD_TEST:
 258                if (nn->nfsd4_minorversions)
 259                        return nn->nfsd4_minorversions[minorversion];
 260                return nfsd_vers(nn, 4, NFSD_TEST);
 261        case NFSD_AVAIL:
 262                return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
 263                        nfsd_vers(nn, 4, NFSD_AVAIL);
 264        }
 265        return 0;
 266}
 267
 268/*
 269 * Maximum number of nfsd processes
 270 */
 271#define NFSD_MAXSERVS           8192
 272
 273int nfsd_nrthreads(struct net *net)
 274{
 275        int rv = 0;
 276        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 277
 278        mutex_lock(&nfsd_mutex);
 279        if (nn->nfsd_serv)
 280                rv = nn->nfsd_serv->sv_nrthreads;
 281        mutex_unlock(&nfsd_mutex);
 282        return rv;
 283}
 284
 285static int nfsd_init_socks(struct net *net, const struct cred *cred)
 286{
 287        int error;
 288        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 289
 290        if (!list_empty(&nn->nfsd_serv->sv_permsocks))
 291                return 0;
 292
 293        error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
 294                                        SVC_SOCK_DEFAULTS, cred);
 295        if (error < 0)
 296                return error;
 297
 298        error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
 299                                        SVC_SOCK_DEFAULTS, cred);
 300        if (error < 0)
 301                return error;
 302
 303        return 0;
 304}
 305
 306static int nfsd_users = 0;
 307
 308static int nfsd_startup_generic(int nrservs)
 309{
 310        int ret;
 311
 312        if (nfsd_users++)
 313                return 0;
 314
 315        /*
 316         * Readahead param cache - will no-op if it already exists.
 317         * (Note therefore results will be suboptimal if number of
 318         * threads is modified after nfsd start.)
 319         */
 320        ret = nfsd_racache_init(2*nrservs);
 321        if (ret)
 322                goto dec_users;
 323
 324        ret = nfs4_state_start();
 325        if (ret)
 326                goto out_racache;
 327        return 0;
 328
 329out_racache:
 330        nfsd_racache_shutdown();
 331dec_users:
 332        nfsd_users--;
 333        return ret;
 334}
 335
 336static void nfsd_shutdown_generic(void)
 337{
 338        if (--nfsd_users)
 339                return;
 340
 341        nfs4_state_shutdown();
 342        nfsd_racache_shutdown();
 343}
 344
 345static bool nfsd_needs_lockd(struct nfsd_net *nn)
 346{
 347        return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
 348}
 349
 350void nfsd_copy_boot_verifier(__be32 verf[2], struct nfsd_net *nn)
 351{
 352        int seq = 0;
 353
 354        do {
 355                read_seqbegin_or_lock(&nn->boot_lock, &seq);
 356                /*
 357                 * This is opaque to client, so no need to byte-swap. Use
 358                 * __force to keep sparse happy. y2038 time_t overflow is
 359                 * irrelevant in this usage
 360                 */
 361                verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
 362                verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
 363        } while (need_seqretry(&nn->boot_lock, seq));
 364        done_seqretry(&nn->boot_lock, seq);
 365}
 366
 367static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
 368{
 369        ktime_get_real_ts64(&nn->nfssvc_boot);
 370}
 371
 372void nfsd_reset_boot_verifier(struct nfsd_net *nn)
 373{
 374        write_seqlock(&nn->boot_lock);
 375        nfsd_reset_boot_verifier_locked(nn);
 376        write_sequnlock(&nn->boot_lock);
 377}
 378
 379static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
 380{
 381        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 382        int ret;
 383
 384        if (nn->nfsd_net_up)
 385                return 0;
 386
 387        ret = nfsd_startup_generic(nrservs);
 388        if (ret)
 389                return ret;
 390        ret = nfsd_init_socks(net, cred);
 391        if (ret)
 392                goto out_socks;
 393
 394        if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
 395                ret = lockd_up(net, cred);
 396                if (ret)
 397                        goto out_socks;
 398                nn->lockd_up = true;
 399        }
 400
 401        ret = nfs4_state_start_net(net);
 402        if (ret)
 403                goto out_lockd;
 404
 405        nn->nfsd_net_up = true;
 406        return 0;
 407
 408out_lockd:
 409        if (nn->lockd_up) {
 410                lockd_down(net);
 411                nn->lockd_up = false;
 412        }
 413out_socks:
 414        nfsd_shutdown_generic();
 415        return ret;
 416}
 417
 418static void nfsd_shutdown_net(struct net *net)
 419{
 420        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 421
 422        nfs4_state_shutdown_net(net);
 423        if (nn->lockd_up) {
 424                lockd_down(net);
 425                nn->lockd_up = false;
 426        }
 427        nn->nfsd_net_up = false;
 428        nfsd_shutdown_generic();
 429}
 430
 431static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
 432        void *ptr)
 433{
 434        struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
 435        struct net_device *dev = ifa->ifa_dev->dev;
 436        struct net *net = dev_net(dev);
 437        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 438        struct sockaddr_in sin;
 439
 440        if ((event != NETDEV_DOWN) ||
 441            !atomic_inc_not_zero(&nn->ntf_refcnt))
 442                goto out;
 443
 444        if (nn->nfsd_serv) {
 445                dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
 446                sin.sin_family = AF_INET;
 447                sin.sin_addr.s_addr = ifa->ifa_local;
 448                svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
 449        }
 450        atomic_dec(&nn->ntf_refcnt);
 451        wake_up(&nn->ntf_wq);
 452
 453out:
 454        return NOTIFY_DONE;
 455}
 456
 457static struct notifier_block nfsd_inetaddr_notifier = {
 458        .notifier_call = nfsd_inetaddr_event,
 459};
 460
 461#if IS_ENABLED(CONFIG_IPV6)
 462static int nfsd_inet6addr_event(struct notifier_block *this,
 463        unsigned long event, void *ptr)
 464{
 465        struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
 466        struct net_device *dev = ifa->idev->dev;
 467        struct net *net = dev_net(dev);
 468        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 469        struct sockaddr_in6 sin6;
 470
 471        if ((event != NETDEV_DOWN) ||
 472            !atomic_inc_not_zero(&nn->ntf_refcnt))
 473                goto out;
 474
 475        if (nn->nfsd_serv) {
 476                dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
 477                sin6.sin6_family = AF_INET6;
 478                sin6.sin6_addr = ifa->addr;
 479                if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
 480                        sin6.sin6_scope_id = ifa->idev->dev->ifindex;
 481                svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
 482        }
 483        atomic_dec(&nn->ntf_refcnt);
 484        wake_up(&nn->ntf_wq);
 485out:
 486        return NOTIFY_DONE;
 487}
 488
 489static struct notifier_block nfsd_inet6addr_notifier = {
 490        .notifier_call = nfsd_inet6addr_event,
 491};
 492#endif
 493
 494/* Only used under nfsd_mutex, so this atomic may be overkill: */
 495static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
 496
 497static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
 498{
 499        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 500
 501        atomic_dec(&nn->ntf_refcnt);
 502        /* check if the notifier still has clients */
 503        if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
 504                unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
 505#if IS_ENABLED(CONFIG_IPV6)
 506                unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
 507#endif
 508        }
 509        wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
 510
 511        /*
 512         * write_ports can create the server without actually starting
 513         * any threads--if we get shut down before any threads are
 514         * started, then nfsd_last_thread will be run before any of this
 515         * other initialization has been done except the rpcb information.
 516         */
 517        svc_rpcb_cleanup(serv, net);
 518        if (!nn->nfsd_net_up)
 519                return;
 520
 521        nfsd_shutdown_net(net);
 522        printk(KERN_WARNING "nfsd: last server has exited, flushing export "
 523                            "cache\n");
 524        nfsd_export_flush(net);
 525}
 526
 527void nfsd_reset_versions(struct nfsd_net *nn)
 528{
 529        int i;
 530
 531        for (i = 0; i < NFSD_NRVERS; i++)
 532                if (nfsd_vers(nn, i, NFSD_TEST))
 533                        return;
 534
 535        for (i = 0; i < NFSD_NRVERS; i++)
 536                if (i != 4)
 537                        nfsd_vers(nn, i, NFSD_SET);
 538                else {
 539                        int minor = 0;
 540                        while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
 541                                minor++;
 542                }
 543}
 544
 545/*
 546 * Each session guarantees a negotiated per slot memory cache for replies
 547 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
 548 * NFSv4.1 server might want to use more memory for a DRC than a machine
 549 * with mutiple services.
 550 *
 551 * Impose a hard limit on the number of pages for the DRC which varies
 552 * according to the machines free pages. This is of course only a default.
 553 *
 554 * For now this is a #defined shift which could be under admin control
 555 * in the future.
 556 */
 557static void set_max_drc(void)
 558{
 559        #define NFSD_DRC_SIZE_SHIFT     7
 560        nfsd_drc_max_mem = (nr_free_buffer_pages()
 561                                        >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
 562        nfsd_drc_mem_used = 0;
 563        spin_lock_init(&nfsd_drc_lock);
 564        dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
 565}
 566
 567static int nfsd_get_default_max_blksize(void)
 568{
 569        struct sysinfo i;
 570        unsigned long long target;
 571        unsigned long ret;
 572
 573        si_meminfo(&i);
 574        target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
 575        /*
 576         * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
 577         * machines, but only uses 32K on 128M machines.  Bottom out at
 578         * 8K on 32M and smaller.  Of course, this is only a default.
 579         */
 580        target >>= 12;
 581
 582        ret = NFSSVC_MAXBLKSIZE;
 583        while (ret > target && ret >= 8*1024*2)
 584                ret /= 2;
 585        return ret;
 586}
 587
 588static const struct svc_serv_ops nfsd_thread_sv_ops = {
 589        .svo_shutdown           = nfsd_last_thread,
 590        .svo_function           = nfsd,
 591        .svo_enqueue_xprt       = svc_xprt_do_enqueue,
 592        .svo_setup              = svc_set_num_threads,
 593        .svo_module             = THIS_MODULE,
 594};
 595
 596bool i_am_nfsd(void)
 597{
 598        return kthread_func(current) == nfsd;
 599}
 600
 601int nfsd_create_serv(struct net *net)
 602{
 603        int error;
 604        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 605
 606        WARN_ON(!mutex_is_locked(&nfsd_mutex));
 607        if (nn->nfsd_serv) {
 608                svc_get(nn->nfsd_serv);
 609                return 0;
 610        }
 611        if (nfsd_max_blksize == 0)
 612                nfsd_max_blksize = nfsd_get_default_max_blksize();
 613        nfsd_reset_versions(nn);
 614        nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
 615                                                &nfsd_thread_sv_ops);
 616        if (nn->nfsd_serv == NULL)
 617                return -ENOMEM;
 618
 619        nn->nfsd_serv->sv_maxconn = nn->max_connections;
 620        error = svc_bind(nn->nfsd_serv, net);
 621        if (error < 0) {
 622                svc_destroy(nn->nfsd_serv);
 623                return error;
 624        }
 625
 626        set_max_drc();
 627        /* check if the notifier is already set */
 628        if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
 629                register_inetaddr_notifier(&nfsd_inetaddr_notifier);
 630#if IS_ENABLED(CONFIG_IPV6)
 631                register_inet6addr_notifier(&nfsd_inet6addr_notifier);
 632#endif
 633        }
 634        atomic_inc(&nn->ntf_refcnt);
 635        nfsd_reset_boot_verifier(nn);
 636        return 0;
 637}
 638
 639int nfsd_nrpools(struct net *net)
 640{
 641        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 642
 643        if (nn->nfsd_serv == NULL)
 644                return 0;
 645        else
 646                return nn->nfsd_serv->sv_nrpools;
 647}
 648
 649int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
 650{
 651        int i = 0;
 652        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 653
 654        if (nn->nfsd_serv != NULL) {
 655                for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
 656                        nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
 657        }
 658
 659        return 0;
 660}
 661
 662void nfsd_destroy(struct net *net)
 663{
 664        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 665        int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
 666
 667        if (destroy)
 668                svc_shutdown_net(nn->nfsd_serv, net);
 669        svc_destroy(nn->nfsd_serv);
 670        if (destroy)
 671                nn->nfsd_serv = NULL;
 672}
 673
 674int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
 675{
 676        int i = 0;
 677        int tot = 0;
 678        int err = 0;
 679        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 680
 681        WARN_ON(!mutex_is_locked(&nfsd_mutex));
 682
 683        if (nn->nfsd_serv == NULL || n <= 0)
 684                return 0;
 685
 686        if (n > nn->nfsd_serv->sv_nrpools)
 687                n = nn->nfsd_serv->sv_nrpools;
 688
 689        /* enforce a global maximum number of threads */
 690        tot = 0;
 691        for (i = 0; i < n; i++) {
 692                nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
 693                tot += nthreads[i];
 694        }
 695        if (tot > NFSD_MAXSERVS) {
 696                /* total too large: scale down requested numbers */
 697                for (i = 0; i < n && tot > 0; i++) {
 698                        int new = nthreads[i] * NFSD_MAXSERVS / tot;
 699                        tot -= (nthreads[i] - new);
 700                        nthreads[i] = new;
 701                }
 702                for (i = 0; i < n && tot > 0; i++) {
 703                        nthreads[i]--;
 704                        tot--;
 705                }
 706        }
 707
 708        /*
 709         * There must always be a thread in pool 0; the admin
 710         * can't shut down NFS completely using pool_threads.
 711         */
 712        if (nthreads[0] == 0)
 713                nthreads[0] = 1;
 714
 715        /* apply the new numbers */
 716        svc_get(nn->nfsd_serv);
 717        for (i = 0; i < n; i++) {
 718                err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
 719                                &nn->nfsd_serv->sv_pools[i], nthreads[i]);
 720                if (err)
 721                        break;
 722        }
 723        nfsd_destroy(net);
 724        return err;
 725}
 726
 727/*
 728 * Adjust the number of threads and return the new number of threads.
 729 * This is also the function that starts the server if necessary, if
 730 * this is the first time nrservs is nonzero.
 731 */
 732int
 733nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
 734{
 735        int     error;
 736        bool    nfsd_up_before;
 737        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 738
 739        mutex_lock(&nfsd_mutex);
 740        dprintk("nfsd: creating service\n");
 741
 742        nrservs = max(nrservs, 0);
 743        nrservs = min(nrservs, NFSD_MAXSERVS);
 744        error = 0;
 745
 746        if (nrservs == 0 && nn->nfsd_serv == NULL)
 747                goto out;
 748
 749        strlcpy(nn->nfsd_name, utsname()->nodename,
 750                sizeof(nn->nfsd_name));
 751
 752        error = nfsd_create_serv(net);
 753        if (error)
 754                goto out;
 755
 756        nfsd_up_before = nn->nfsd_net_up;
 757
 758        error = nfsd_startup_net(nrservs, net, cred);
 759        if (error)
 760                goto out_destroy;
 761        error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
 762                        NULL, nrservs);
 763        if (error)
 764                goto out_shutdown;
 765        /* We are holding a reference to nn->nfsd_serv which
 766         * we don't want to count in the return value,
 767         * so subtract 1
 768         */
 769        error = nn->nfsd_serv->sv_nrthreads - 1;
 770out_shutdown:
 771        if (error < 0 && !nfsd_up_before)
 772                nfsd_shutdown_net(net);
 773out_destroy:
 774        nfsd_destroy(net);              /* Release server */
 775out:
 776        mutex_unlock(&nfsd_mutex);
 777        return error;
 778}
 779
 780#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
 781static bool
 782nfsd_support_acl_version(int vers)
 783{
 784        if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
 785                return nfsd_acl_version[vers] != NULL;
 786        return false;
 787}
 788
 789static int
 790nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
 791                     u32 version, int family, unsigned short proto,
 792                     unsigned short port)
 793{
 794        if (!nfsd_support_acl_version(version) ||
 795            !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
 796                return 0;
 797        return svc_generic_rpcbind_set(net, progp, version, family,
 798                        proto, port);
 799}
 800
 801static __be32
 802nfsd_acl_init_request(struct svc_rqst *rqstp,
 803                      const struct svc_program *progp,
 804                      struct svc_process_info *ret)
 805{
 806        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
 807        int i;
 808
 809        if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
 810            nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
 811                return svc_generic_init_request(rqstp, progp, ret);
 812
 813        ret->mismatch.lovers = NFSD_ACL_NRVERS;
 814        for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
 815                if (nfsd_support_acl_version(rqstp->rq_vers) &&
 816                    nfsd_vers(nn, i, NFSD_TEST)) {
 817                        ret->mismatch.lovers = i;
 818                        break;
 819                }
 820        }
 821        if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
 822                return rpc_prog_unavail;
 823        ret->mismatch.hivers = NFSD_ACL_MINVERS;
 824        for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
 825                if (nfsd_support_acl_version(rqstp->rq_vers) &&
 826                    nfsd_vers(nn, i, NFSD_TEST)) {
 827                        ret->mismatch.hivers = i;
 828                        break;
 829                }
 830        }
 831        return rpc_prog_mismatch;
 832}
 833#endif
 834
 835static int
 836nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
 837                 u32 version, int family, unsigned short proto,
 838                 unsigned short port)
 839{
 840        if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
 841                return 0;
 842        return svc_generic_rpcbind_set(net, progp, version, family,
 843                        proto, port);
 844}
 845
 846static __be32
 847nfsd_init_request(struct svc_rqst *rqstp,
 848                  const struct svc_program *progp,
 849                  struct svc_process_info *ret)
 850{
 851        struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
 852        int i;
 853
 854        if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
 855                return svc_generic_init_request(rqstp, progp, ret);
 856
 857        ret->mismatch.lovers = NFSD_NRVERS;
 858        for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
 859                if (nfsd_vers(nn, i, NFSD_TEST)) {
 860                        ret->mismatch.lovers = i;
 861                        break;
 862                }
 863        }
 864        if (ret->mismatch.lovers == NFSD_NRVERS)
 865                return rpc_prog_unavail;
 866        ret->mismatch.hivers = NFSD_MINVERS;
 867        for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
 868                if (nfsd_vers(nn, i, NFSD_TEST)) {
 869                        ret->mismatch.hivers = i;
 870                        break;
 871                }
 872        }
 873        return rpc_prog_mismatch;
 874}
 875
 876/*
 877 * This is the NFS server kernel thread
 878 */
 879static int
 880nfsd(void *vrqstp)
 881{
 882        struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
 883        struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
 884        struct net *net = perm_sock->xpt_net;
 885        struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 886        int err;
 887
 888        /* Lock module and set up kernel thread */
 889        mutex_lock(&nfsd_mutex);
 890
 891        /* At this point, the thread shares current->fs
 892         * with the init process. We need to create files with the
 893         * umask as defined by the client instead of init's umask. */
 894        if (unshare_fs_struct() < 0) {
 895                printk("Unable to start nfsd thread: out of memory\n");
 896                goto out;
 897        }
 898
 899        current->fs->umask = 0;
 900
 901        /*
 902         * thread is spawned with all signals set to SIG_IGN, re-enable
 903         * the ones that will bring down the thread
 904         */
 905        allow_signal(SIGKILL);
 906        allow_signal(SIGHUP);
 907        allow_signal(SIGINT);
 908        allow_signal(SIGQUIT);
 909
 910        nfsdstats.th_cnt++;
 911        mutex_unlock(&nfsd_mutex);
 912
 913        set_freezable();
 914
 915        /*
 916         * The main request loop
 917         */
 918        for (;;) {
 919                /* Update sv_maxconn if it has changed */
 920                rqstp->rq_server->sv_maxconn = nn->max_connections;
 921
 922                /*
 923                 * Find a socket with data available and call its
 924                 * recvfrom routine.
 925                 */
 926                while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
 927                        ;
 928                if (err == -EINTR)
 929                        break;
 930                validate_process_creds();
 931                svc_process(rqstp);
 932                validate_process_creds();
 933        }
 934
 935        /* Clear signals before calling svc_exit_thread() */
 936        flush_signals(current);
 937
 938        mutex_lock(&nfsd_mutex);
 939        nfsdstats.th_cnt --;
 940
 941out:
 942        rqstp->rq_server = NULL;
 943
 944        /* Release the thread */
 945        svc_exit_thread(rqstp);
 946
 947        nfsd_destroy(net);
 948
 949        /* Release module */
 950        mutex_unlock(&nfsd_mutex);
 951        module_put_and_exit(0);
 952        return 0;
 953}
 954
 955static __be32 map_new_errors(u32 vers, __be32 nfserr)
 956{
 957        if (nfserr == nfserr_jukebox && vers == 2)
 958                return nfserr_dropit;
 959        if (nfserr == nfserr_wrongsec && vers < 4)
 960                return nfserr_acces;
 961        return nfserr;
 962}
 963
 964/*
 965 * A write procedure can have a large argument, and a read procedure can
 966 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
 967 * reply that can both be larger than a page.  The xdr code has taken
 968 * advantage of this assumption to be a sloppy about bounds checking in
 969 * some cases.  Pending a rewrite of the NFSv2/v3 xdr code to fix that
 970 * problem, we enforce these assumptions here:
 971 */
 972static bool nfs_request_too_big(struct svc_rqst *rqstp,
 973                                const struct svc_procedure *proc)
 974{
 975        /*
 976         * The ACL code has more careful bounds-checking and is not
 977         * susceptible to this problem:
 978         */
 979        if (rqstp->rq_prog != NFS_PROGRAM)
 980                return false;
 981        /*
 982         * Ditto NFSv4 (which can in theory have argument and reply both
 983         * more than a page):
 984         */
 985        if (rqstp->rq_vers >= 4)
 986                return false;
 987        /* The reply will be small, we're OK: */
 988        if (proc->pc_xdrressize > 0 &&
 989            proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
 990                return false;
 991
 992        return rqstp->rq_arg.len > PAGE_SIZE;
 993}
 994
 995int
 996nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
 997{
 998        const struct svc_procedure *proc;
 999        __be32                  nfserr;
1000        __be32                  *nfserrp;
1001
1002        dprintk("nfsd_dispatch: vers %d proc %d\n",
1003                                rqstp->rq_vers, rqstp->rq_proc);
1004        proc = rqstp->rq_procinfo;
1005
1006        if (nfs_request_too_big(rqstp, proc)) {
1007                dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
1008                *statp = rpc_garbage_args;
1009                return 1;
1010        }
1011        rqstp->rq_lease_breaker = NULL;
1012        /*
1013         * Give the xdr decoder a chance to change this if it wants
1014         * (necessary in the NFSv4.0 compound case)
1015         */
1016        rqstp->rq_cachetype = proc->pc_cachetype;
1017        /* Decode arguments */
1018        if (proc->pc_decode &&
1019            !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
1020                dprintk("nfsd: failed to decode arguments!\n");
1021                *statp = rpc_garbage_args;
1022                return 1;
1023        }
1024
1025        /* Check whether we have this call in the cache. */
1026        switch (nfsd_cache_lookup(rqstp)) {
1027        case RC_DROPIT:
1028                return 0;
1029        case RC_REPLY:
1030                return 1;
1031        case RC_DOIT:;
1032                /* do it */
1033        }
1034
1035        /* need to grab the location to store the status, as
1036         * nfsv4 does some encoding while processing 
1037         */
1038        nfserrp = rqstp->rq_res.head[0].iov_base
1039                + rqstp->rq_res.head[0].iov_len;
1040        rqstp->rq_res.head[0].iov_len += sizeof(__be32);
1041
1042        /* Now call the procedure handler, and encode NFS status. */
1043        nfserr = proc->pc_func(rqstp);
1044        nfserr = map_new_errors(rqstp->rq_vers, nfserr);
1045        if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
1046                dprintk("nfsd: Dropping request; may be revisited later\n");
1047                nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1048                return 0;
1049        }
1050
1051        if (rqstp->rq_proc != 0)
1052                *nfserrp++ = nfserr;
1053
1054        /* Encode result.
1055         * For NFSv2, additional info is never returned in case of an error.
1056         */
1057        if (!(nfserr && rqstp->rq_vers == 2)) {
1058                if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
1059                        /* Failed to encode result. Release cache entry */
1060                        dprintk("nfsd: failed to encode result!\n");
1061                        nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1062                        *statp = rpc_system_err;
1063                        return 1;
1064                }
1065        }
1066
1067        /* Store reply in cache. */
1068        nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1069        return 1;
1070}
1071
1072int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1073{
1074        int ret;
1075        struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1076
1077        mutex_lock(&nfsd_mutex);
1078        if (nn->nfsd_serv == NULL) {
1079                mutex_unlock(&nfsd_mutex);
1080                return -ENODEV;
1081        }
1082        /* bump up the psudo refcount while traversing */
1083        svc_get(nn->nfsd_serv);
1084        ret = svc_pool_stats_open(nn->nfsd_serv, file);
1085        mutex_unlock(&nfsd_mutex);
1086        return ret;
1087}
1088
1089int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1090{
1091        int ret = seq_release(inode, file);
1092        struct net *net = inode->i_sb->s_fs_info;
1093
1094        mutex_lock(&nfsd_mutex);
1095        /* this function really, really should have been called svc_put() */
1096        nfsd_destroy(net);
1097        mutex_unlock(&nfsd_mutex);
1098        return ret;
1099}
1100