The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/nfsd/nfscache.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * linux/fs/nfsd/nfscache.c
    3  *
    4  * Request reply cache. This is currently a global cache, but this may
    5  * change in the future and be a per-client cache.
    6  *
    7  * This code is heavily inspired by the 44BSD implementation, although
    8  * it does things a bit differently.
    9  *
   10  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
   11  */
   12 
   13 #include <linux/kernel.h>
   14 #include <linux/sched.h>
   15 #include <linux/slab.h>
   16 #include <linux/string.h>
   17 
   18 #include <linux/sunrpc/svc.h>
   19 #include <linux/nfsd/nfsd.h>
   20 #include <linux/nfsd/cache.h>
   21 
   22 /* Size of reply cache. Common values are:
   23  * 4.3BSD:      128
   24  * 4.4BSD:      256
   25  * Solaris2:    1024
   26  * DEC Unix:    512-4096
   27  */
   28 #define CACHESIZE               1024
   29 #define HASHSIZE                64
   30 #define REQHASH(xid)            ((((xid) >> 24) ^ (xid)) & (HASHSIZE-1))
   31 
   32 struct nfscache_head {
   33         struct svc_cacherep *   next;
   34         struct svc_cacherep *   prev;
   35 };
   36 
   37 static struct nfscache_head *   hash_list;
   38 static struct svc_cacherep *    lru_head;
   39 static struct svc_cacherep *    lru_tail;
   40 static struct svc_cacherep *    nfscache;
   41 static int                      cache_disabled = 1;
   42 
   43 static int      nfsd_cache_append(struct svc_rqst *rqstp, struct svc_buf *data);
   44 
   45 void
   46 nfsd_cache_init(void)
   47 {
   48         struct svc_cacherep     *rp;
   49         struct nfscache_head    *rh;
   50         size_t                  i;
   51         unsigned long           order;
   52 
   53 
   54         i = CACHESIZE * sizeof (struct svc_cacherep);
   55         for (order = 0; (PAGE_SIZE << order) < i; order++)
   56                 ;
   57         nfscache = (struct svc_cacherep *)
   58                 __get_free_pages(GFP_KERNEL, order);
   59         if (!nfscache) {
   60                 printk (KERN_ERR "nfsd: cannot allocate %Zd bytes for reply cache\n", i);
   61                 return;
   62         }
   63         memset(nfscache, 0, i);
   64 
   65         i = HASHSIZE * sizeof (struct nfscache_head);
   66         hash_list = kmalloc (i, GFP_KERNEL);
   67         if (!hash_list) {
   68                 free_pages ((unsigned long)nfscache, order);
   69                 nfscache = NULL;
   70                 printk (KERN_ERR "nfsd: cannot allocate %Zd bytes for hash list\n", i);
   71                 return;
   72         }
   73 
   74         for (i = 0, rh = hash_list; i < HASHSIZE; i++, rh++)
   75                 rh->next = rh->prev = (struct svc_cacherep *) rh;
   76 
   77         for (i = 0, rp = nfscache; i < CACHESIZE; i++, rp++) {
   78                 rp->c_state = RC_UNUSED;
   79                 rp->c_type = RC_NOCACHE;
   80                 rp->c_hash_next =
   81                 rp->c_hash_prev = rp;
   82                 rp->c_lru_next = rp + 1;
   83                 rp->c_lru_prev = rp - 1;
   84         }
   85         lru_head = nfscache;
   86         lru_tail = nfscache + CACHESIZE - 1;
   87         lru_head->c_lru_prev = NULL;
   88         lru_tail->c_lru_next = NULL;
   89 
   90         cache_disabled = 0;
   91 }
   92 
   93 void
   94 nfsd_cache_shutdown(void)
   95 {
   96         struct svc_cacherep     *rp;
   97         size_t                  i;
   98         unsigned long           order;
   99 
  100         for (rp = lru_head; rp; rp = rp->c_lru_next) {
  101                 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
  102                         kfree(rp->c_replbuf.buf);
  103         }
  104 
  105         cache_disabled = 1;
  106 
  107         i = CACHESIZE * sizeof (struct svc_cacherep);
  108         for (order = 0; (PAGE_SIZE << order) < i; order++)
  109                 ;
  110         free_pages ((unsigned long)nfscache, order);
  111         nfscache = NULL;
  112         kfree (hash_list);
  113         hash_list = NULL;
  114 }
  115 
  116 /*
  117  * Move cache entry to front of LRU list
  118  */
  119 static void
  120 lru_put_front(struct svc_cacherep *rp)
  121 {
  122         struct svc_cacherep     *prev = rp->c_lru_prev,
  123                                 *next = rp->c_lru_next;
  124 
  125         if (prev)
  126                 prev->c_lru_next = next;
  127         else
  128                 lru_head = next;
  129         if (next)
  130                 next->c_lru_prev = prev;
  131         else
  132                 lru_tail = prev;
  133 
  134         rp->c_lru_next = lru_head;
  135         rp->c_lru_prev = NULL;
  136         if (lru_head)
  137                 lru_head->c_lru_prev = rp;
  138         lru_head = rp;
  139 }
  140 
  141 /*
  142  * Move a cache entry from one hash list to another
  143  */
  144 static void
  145 hash_refile(struct svc_cacherep *rp)
  146 {
  147         struct svc_cacherep     *prev = rp->c_hash_prev,
  148                                 *next = rp->c_hash_next;
  149         struct nfscache_head    *head = hash_list + REQHASH(rp->c_xid);
  150 
  151         prev->c_hash_next = next;
  152         next->c_hash_prev = prev;
  153 
  154         rp->c_hash_next = head->next;
  155         rp->c_hash_prev = (struct svc_cacherep *) head;
  156         head->next->c_hash_prev = rp;
  157         head->next = rp;
  158 }
  159 
  160 /*
  161  * Try to find an entry matching the current call in the cache. When none
  162  * is found, we grab the oldest unlocked entry off the LRU list.
  163  * Note that no operation within the loop may sleep.
  164  */
  165 int
  166 nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
  167 {
  168         struct svc_cacherep     *rh, *rp;
  169         u32                     xid = rqstp->rq_xid,
  170                                 proto =  rqstp->rq_prot,
  171                                 vers = rqstp->rq_vers,
  172                                 proc = rqstp->rq_proc;
  173         unsigned long           age;
  174 
  175         rqstp->rq_cacherep = NULL;
  176         if (cache_disabled || type == RC_NOCACHE) {
  177                 nfsdstats.rcnocache++;
  178                 return RC_DOIT;
  179         }
  180 
  181         rp = rh = (struct svc_cacherep *) &hash_list[REQHASH(xid)];
  182         while ((rp = rp->c_hash_next) != rh) {
  183                 if (rp->c_state != RC_UNUSED &&
  184                     xid == rp->c_xid && proc == rp->c_proc &&
  185                     proto == rp->c_prot && vers == rp->c_vers &&
  186                     time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  187                     memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
  188                         nfsdstats.rchits++;
  189                         goto found_entry;
  190                 }
  191         }
  192         nfsdstats.rcmisses++;
  193 
  194         /* This loop shouldn't take more than a few iterations normally */
  195         {
  196         int     safe = 0;
  197         for (rp = lru_tail; rp; rp = rp->c_lru_prev) {
  198                 if (rp->c_state != RC_INPROG)
  199                         break;
  200                 if (safe++ > CACHESIZE) {
  201                         printk("nfsd: loop in repcache LRU list\n");
  202                         cache_disabled = 1;
  203                         return RC_DOIT;
  204                 }
  205         }
  206         }
  207 
  208         /* This should not happen */
  209         if (rp == NULL) {
  210                 static int      complaints;
  211 
  212                 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  213                 if (++complaints > 5) {
  214                         printk(KERN_WARNING "nfsd: disabling repcache.\n");
  215                         cache_disabled = 1;
  216                 }
  217                 return RC_DOIT;
  218         }
  219 
  220         rqstp->rq_cacherep = rp;
  221         rp->c_state = RC_INPROG;
  222         rp->c_xid = xid;
  223         rp->c_proc = proc;
  224         rp->c_addr = rqstp->rq_addr;
  225         rp->c_prot = proto;
  226         rp->c_vers = vers;
  227         rp->c_timestamp = jiffies;
  228 
  229         hash_refile(rp);
  230 
  231         /* release any buffer */
  232         if (rp->c_type == RC_REPLBUFF) {
  233                 kfree(rp->c_replbuf.buf);
  234                 rp->c_replbuf.buf = NULL;
  235         }
  236         rp->c_type = RC_NOCACHE;
  237 
  238         return RC_DOIT;
  239 
  240 found_entry:
  241         /* We found a matching entry which is either in progress or done. */
  242         age = jiffies - rp->c_timestamp;
  243         rp->c_timestamp = jiffies;
  244         lru_put_front(rp);
  245 
  246         /* Request being processed or excessive rexmits */
  247         if (rp->c_state == RC_INPROG || age < RC_DELAY)
  248                 return RC_DROPIT;
  249 
  250         /* From the hall of fame of impractical attacks:
  251          * Is this a user who tries to snoop on the cache? */
  252         if (!rqstp->rq_secure && rp->c_secure)
  253                 return RC_DOIT;
  254 
  255         /* Compose RPC reply header */
  256         switch (rp->c_type) {
  257         case RC_NOCACHE:
  258                 return RC_DOIT;
  259         case RC_REPLSTAT:
  260                 svc_putlong(&rqstp->rq_resbuf, rp->c_replstat);
  261                 break;
  262         case RC_REPLBUFF:
  263                 if (!nfsd_cache_append(rqstp, &rp->c_replbuf))
  264                         return RC_DOIT; /* should not happen */
  265                 break;
  266         default:
  267                 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  268                 rp->c_state = RC_UNUSED;
  269                 return RC_DOIT;
  270         }
  271 
  272         return RC_REPLY;
  273 }
  274 
  275 /*
  276  * Update a cache entry. This is called from nfsd_dispatch when
  277  * the procedure has been executed and the complete reply is in
  278  * rqstp->rq_res.
  279  *
  280  * We're copying around data here rather than swapping buffers because
  281  * the toplevel loop requires max-sized buffers, which would be a waste
  282  * of memory for a cache with a max reply size of 100 bytes (diropokres).
  283  *
  284  * If we should start to use different types of cache entries tailored
  285  * specifically for attrstat and fh's, we may save even more space.
  286  *
  287  * Also note that a cachetype of RC_NOCACHE can legally be passed when
  288  * nfsd failed to encode a reply that otherwise would have been cached.
  289  * In this case, nfsd_cache_update is called with statp == NULL.
  290  */
  291 void
  292 nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
  293 {
  294         struct svc_cacherep *rp;
  295         struct svc_buf  *resp = &rqstp->rq_resbuf, *cachp;
  296         int             len;
  297 
  298         if (!(rp = rqstp->rq_cacherep) || cache_disabled)
  299                 return;
  300 
  301         len = resp->len - (statp - resp->base);
  302         
  303         /* Don't cache excessive amounts of data and XDR failures */
  304         if (!statp || len > (256 >> 2)) {
  305                 rp->c_state = RC_UNUSED;
  306                 return;
  307         }
  308 
  309         switch (cachetype) {
  310         case RC_REPLSTAT:
  311                 if (len != 1)
  312                         printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  313                 rp->c_replstat = *statp;
  314                 break;
  315         case RC_REPLBUFF:
  316                 cachp = &rp->c_replbuf;
  317                 cachp->buf = (u32 *) kmalloc(len << 2, GFP_KERNEL);
  318                 if (!cachp->buf) {
  319                         rp->c_state = RC_UNUSED;
  320                         return;
  321                 }
  322                 cachp->len = len;
  323                 memcpy(cachp->buf, statp, len << 2);
  324                 break;
  325         }
  326 
  327         lru_put_front(rp);
  328         rp->c_secure = rqstp->rq_secure;
  329         rp->c_type = cachetype;
  330         rp->c_state = RC_DONE;
  331         rp->c_timestamp = jiffies;
  332 
  333         return;
  334 }
  335 
  336 /*
  337  * Copy cached reply to current reply buffer. Should always fit.
  338  */
  339 static int
  340 nfsd_cache_append(struct svc_rqst *rqstp, struct svc_buf *data)
  341 {
  342         struct svc_buf  *resp = &rqstp->rq_resbuf;
  343 
  344         if (resp->len + data->len > resp->buflen) {
  345                 printk(KERN_WARNING "nfsd: cached reply too large (%d).\n",
  346                                 data->len);
  347                 return 0;
  348         }
  349         memcpy(resp->buf, data->buf, data->len << 2);
  350         resp->buf += data->len;
  351         resp->len += data->len;
  352         return 1;
  353 }

Cache object: 93708875b6a7d2c0250264f912ccccf6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.