]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_srvcache.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_srvcache.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfs_srvcache.c 8.3 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfs_srvcache.c,v 1.15 1997/10/12 20:25:46 phk Exp $
66 */
67
ea3f0419
A
68#include <nfs/nfs_conf.h>
69#if CONFIG_NFS_SERVER
70
1c79356b
A
71/*
72 * Reference: Chet Juszczak, "Improving the Performance and Correctness
73 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
74 * pages 53-63. San Diego, February 1989.
75 */
76#include <sys/param.h>
77#include <sys/vnode.h>
91447636 78#include <sys/mount_internal.h>
1c79356b
A
79#include <sys/kernel.h>
80#include <sys/systm.h>
81#include <sys/proc.h>
91447636 82#include <sys/kpi_mbuf.h>
1c79356b
A
83#include <sys/malloc.h>
84#include <sys/socket.h>
91447636 85#include <libkern/OSAtomic.h>
1c79356b
A
86
87#include <netinet/in.h>
1c79356b
A
88#include <nfs/rpcv2.h>
89#include <nfs/nfsproto.h>
90#include <nfs/nfs.h>
91#include <nfs/nfsrvcache.h>
92
1c79356b 93extern int nfsv2_procid[NFS_NPROCS];
2d21ac55
A
94static int nfsrv_reqcache_count;
95int nfsrv_reqcache_size = NFSRVCACHESIZ;
1c79356b 96
0a7de745 97#define NFSRCHASH(xid) \
2d21ac55 98 (&nfsrv_reqcache_hashtbl[((xid) + ((xid) >> 24)) & nfsrv_reqcache_hash])
0a7de745 99LIST_HEAD(nfsrv_reqcache_hash, nfsrvcache) * nfsrv_reqcache_hashtbl;
2d21ac55
A
100TAILQ_HEAD(nfsrv_reqcache_lru, nfsrvcache) nfsrv_reqcache_lruhead;
101u_long nfsrv_reqcache_hash;
1c79356b 102
c3c9b80d
A
103static LCK_GRP_DECLARE(nfsrv_reqcache_lck_grp, "nfsrv_reqcache");
104LCK_MTX_DECLARE(nfsrv_reqcache_mutex, &nfsrv_reqcache_lck_grp);
1c79356b 105
1c79356b
A
106/*
107 * Static array that defines which nfs rpc's are nonidempotent
108 */
109static int nonidempotent[NFS_NPROCS] = {
110 FALSE,
111 FALSE,
112 TRUE,
113 FALSE,
114 FALSE,
115 FALSE,
116 FALSE,
117 TRUE,
118 TRUE,
119 TRUE,
120 TRUE,
121 TRUE,
122 TRUE,
123 TRUE,
124 TRUE,
125 TRUE,
126 FALSE,
127 FALSE,
128 FALSE,
129 FALSE,
130 FALSE,
131 FALSE,
132 FALSE,
1c79356b
A
133};
134
135/* True iff the rpc reply is an nfs status ONLY! */
136static int nfsv2_repstat[NFS_NPROCS] = {
137 FALSE,
138 FALSE,
139 FALSE,
140 FALSE,
141 FALSE,
142 FALSE,
143 FALSE,
144 FALSE,
145 FALSE,
146 FALSE,
147 TRUE,
148 TRUE,
149 TRUE,
150 TRUE,
151 FALSE,
152 TRUE,
153 FALSE,
154 FALSE,
155};
156
157/*
158 * Initialize the server request cache list
159 */
160void
2d21ac55
A
161nfsrv_initcache(void)
162{
0a7de745 163 if (nfsrv_reqcache_size <= 0) {
2d21ac55 164 return;
0a7de745 165 }
2d21ac55 166
c3c9b80d 167 lck_mtx_lock(&nfsrv_reqcache_mutex);
2d21ac55
A
168 /* init nfs server request cache hash table */
169 nfsrv_reqcache_hashtbl = hashinit(nfsrv_reqcache_size, M_NFSD, &nfsrv_reqcache_hash);
170 TAILQ_INIT(&nfsrv_reqcache_lruhead);
c3c9b80d 171 lck_mtx_unlock(&nfsrv_reqcache_mutex);
2d21ac55
A
172}
173
174/*
175 * This function compares two net addresses by family and returns TRUE
176 * if they are the same host.
177 * If there is any doubt, return FALSE.
178 * The AF_INET family is handled as a special case so that address mbufs
179 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
6d2010ae 180 * Ditto for AF_INET6 which is only 16 bytes.
2d21ac55
A
181 */
182static int
183netaddr_match(
184 int family,
185 union nethostaddr *haddr,
186 mbuf_t nam)
1c79356b 187{
2d21ac55 188 struct sockaddr_in *inetaddr;
6d2010ae 189 struct sockaddr_in6 *inet6addr;
1c79356b 190
2d21ac55
A
191 switch (family) {
192 case AF_INET:
193 inetaddr = mbuf_data(nam);
6d2010ae 194 if ((inetaddr->sin_family == AF_INET) &&
0a7de745
A
195 (inetaddr->sin_addr.s_addr == haddr->had_inetaddr)) {
196 return 1;
197 }
2d21ac55 198 break;
6d2010ae
A
199 case AF_INET6:
200 inet6addr = mbuf_data(nam);
201 if ((inet6addr->sin6_family == AF_INET6) &&
0a7de745
A
202 !bcmp(&inet6addr->sin6_addr, &haddr->had_inet6addr, sizeof(inet6addr->sin6_addr))) {
203 return 1;
204 }
2d21ac55 205 break;
6d2010ae 206 }
0a7de745 207 return 0;
1c79356b
A
208}
209
210/*
211 * Look for the request in the cache
212 * If found then
213 * return action and optionally reply
214 * else
215 * insert it in the cache
216 *
217 * The rules are as follows:
218 * - if in progress, return DROP request
219 * - if completed within DELAY of the current time, return DROP it
220 * - if completed a longer time ago return REPLY if the reply was cached or
221 * return DOIT
222 * Update/add new request at end of lru list
223 */
224int
2d21ac55
A
225nfsrv_getcache(
226 struct nfsrv_descript *nd,
227 struct nfsrv_sock *slp,
228 mbuf_t *mrepp)
1c79356b 229{
91447636 230 struct nfsrvcache *rp;
2d21ac55 231 struct nfsm_chain nmrep;
6d2010ae 232 struct sockaddr *saddr;
91447636 233 int ret, error;
1c79356b
A
234
235 /*
236 * Don't cache recent requests for reliable transport protocols.
237 * (Maybe we should for the case of a reconnect, but..)
238 */
0a7de745
A
239 if (!nd->nd_nam2) {
240 return RC_DOIT;
241 }
c3c9b80d 242 lck_mtx_lock(&nfsrv_reqcache_mutex);
1c79356b
A
243loop:
244 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
245 rp = rp->rc_hash.le_next) {
0a7de745
A
246 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
247 netaddr_match(rp->rc_family, &rp->rc_haddr, nd->nd_nam)) {
1c79356b
A
248 if ((rp->rc_flag & RC_LOCKED) != 0) {
249 rp->rc_flag |= RC_WANTED;
c3c9b80d 250 msleep(rp, &nfsrv_reqcache_mutex, PZERO - 1, "nfsrc", NULL);
1c79356b
A
251 goto loop;
252 }
253 rp->rc_flag |= RC_LOCKED;
254 /* If not at end of LRU chain, move it there */
255 if (rp->rc_lru.tqe_next) {
2d21ac55
A
256 TAILQ_REMOVE(&nfsrv_reqcache_lruhead, rp, rc_lru);
257 TAILQ_INSERT_TAIL(&nfsrv_reqcache_lruhead, rp, rc_lru);
1c79356b 258 }
0a7de745 259 if (rp->rc_state == RC_UNUSED) {
1c79356b 260 panic("nfsrv cache");
0a7de745 261 }
1c79356b 262 if (rp->rc_state == RC_INPROG) {
316670eb 263 OSAddAtomic64(1, &nfsstats.srvcache_inproghits);
1c79356b
A
264 ret = RC_DROPIT;
265 } else if (rp->rc_flag & RC_REPSTATUS) {
316670eb 266 OSAddAtomic64(1, &nfsstats.srvcache_nonidemdonehits);
2d21ac55
A
267 nd->nd_repstat = rp->rc_status;
268 error = nfsrv_rephead(nd, slp, &nmrep, 0);
269 if (error) {
270 printf("nfsrv cache: reply alloc failed for nonidem request hit\n");
271 ret = RC_DROPIT;
272 *mrepp = NULL;
273 } else {
274 ret = RC_REPLY;
275 *mrepp = nmrep.nmc_mhead;
276 }
1c79356b 277 } else if (rp->rc_flag & RC_REPMBUF) {
316670eb 278 OSAddAtomic64(1, &nfsstats.srvcache_nonidemdonehits);
2d21ac55 279 error = mbuf_copym(rp->rc_reply, 0, MBUF_COPYALL, MBUF_WAITOK, mrepp);
91447636
A
280 if (error) {
281 printf("nfsrv cache: reply copym failed for nonidem request hit\n");
282 ret = RC_DROPIT;
283 } else {
284 ret = RC_REPLY;
285 }
1c79356b 286 } else {
316670eb 287 OSAddAtomic64(1, &nfsstats.srvcache_idemdonehits);
1c79356b
A
288 rp->rc_state = RC_INPROG;
289 ret = RC_DOIT;
290 }
291 rp->rc_flag &= ~RC_LOCKED;
292 if (rp->rc_flag & RC_WANTED) {
293 rp->rc_flag &= ~RC_WANTED;
2d21ac55 294 wakeup(rp);
1c79356b 295 }
c3c9b80d 296 lck_mtx_unlock(&nfsrv_reqcache_mutex);
0a7de745 297 return ret;
1c79356b
A
298 }
299 }
316670eb 300 OSAddAtomic64(1, &nfsstats.srvcache_misses);
2d21ac55 301 if (nfsrv_reqcache_count < nfsrv_reqcache_size) {
91447636 302 /* try to allocate a new entry */
1c79356b 303 MALLOC(rp, struct nfsrvcache *, sizeof *rp, M_NFSD, M_WAITOK);
91447636
A
304 if (rp) {
305 bzero((char *)rp, sizeof *rp);
2d21ac55 306 nfsrv_reqcache_count++;
91447636
A
307 rp->rc_flag = RC_LOCKED;
308 }
1c79356b 309 } else {
91447636
A
310 rp = NULL;
311 }
312 if (!rp) {
313 /* try to reuse the least recently used entry */
2d21ac55 314 rp = nfsrv_reqcache_lruhead.tqh_first;
91447636
A
315 if (!rp) {
316 /* no entry to reuse? */
317 /* OK, we just won't be able to cache this request */
c3c9b80d 318 lck_mtx_unlock(&nfsrv_reqcache_mutex);
0a7de745 319 return RC_DOIT;
91447636 320 }
1c79356b
A
321 while ((rp->rc_flag & RC_LOCKED) != 0) {
322 rp->rc_flag |= RC_WANTED;
c3c9b80d 323 msleep(rp, &nfsrv_reqcache_mutex, PZERO - 1, "nfsrc", NULL);
2d21ac55 324 rp = nfsrv_reqcache_lruhead.tqh_first;
1c79356b
A
325 }
326 rp->rc_flag |= RC_LOCKED;
327 LIST_REMOVE(rp, rc_hash);
2d21ac55 328 TAILQ_REMOVE(&nfsrv_reqcache_lruhead, rp, rc_lru);
0a7de745 329 if (rp->rc_flag & RC_REPMBUF) {
91447636 330 mbuf_freem(rp->rc_reply);
0a7de745
A
331 }
332 if (rp->rc_flag & RC_NAM) {
91447636 333 mbuf_freem(rp->rc_nam);
0a7de745 334 }
1c79356b
A
335 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
336 }
2d21ac55 337 TAILQ_INSERT_TAIL(&nfsrv_reqcache_lruhead, rp, rc_lru);
1c79356b
A
338 rp->rc_state = RC_INPROG;
339 rp->rc_xid = nd->nd_retxid;
91447636 340 saddr = mbuf_data(nd->nd_nam);
6d2010ae
A
341 rp->rc_family = saddr->sa_family;
342 switch (saddr->sa_family) {
1c79356b
A
343 case AF_INET:
344 rp->rc_flag |= RC_INETADDR;
6d2010ae
A
345 rp->rc_inetaddr = ((struct sockaddr_in*)saddr)->sin_addr.s_addr;
346 break;
347 case AF_INET6:
348 rp->rc_flag |= RC_INETADDR;
349 rp->rc_inet6addr = ((struct sockaddr_in6*)saddr)->sin6_addr;
1c79356b 350 break;
1c79356b 351 default:
91447636 352 error = mbuf_copym(nd->nd_nam, 0, MBUF_COPYALL, MBUF_WAITOK, &rp->rc_nam);
0a7de745 353 if (error) {
91447636 354 printf("nfsrv cache: nam copym failed\n");
0a7de745 355 } else {
91447636 356 rp->rc_flag |= RC_NAM;
0a7de745 357 }
1c79356b 358 break;
0a7de745
A
359 }
360 ;
1c79356b
A
361 rp->rc_proc = nd->nd_procnum;
362 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
363 rp->rc_flag &= ~RC_LOCKED;
364 if (rp->rc_flag & RC_WANTED) {
365 rp->rc_flag &= ~RC_WANTED;
2d21ac55 366 wakeup(rp);
1c79356b 367 }
c3c9b80d 368 lck_mtx_unlock(&nfsrv_reqcache_mutex);
0a7de745 369 return RC_DOIT;
1c79356b
A
370}
371
372/*
373 * Update a request cache entry after the rpc has been done
374 */
375void
2d21ac55
A
376nfsrv_updatecache(
377 struct nfsrv_descript *nd,
378 int repvalid,
379 mbuf_t repmbuf)
1c79356b 380{
91447636
A
381 struct nfsrvcache *rp;
382 int error;
1c79356b 383
0a7de745 384 if (!nd->nd_nam2) {
1c79356b 385 return;
0a7de745 386 }
c3c9b80d 387 lck_mtx_lock(&nfsrv_reqcache_mutex);
1c79356b
A
388loop:
389 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
390 rp = rp->rc_hash.le_next) {
0a7de745
A
391 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
392 netaddr_match(rp->rc_family, &rp->rc_haddr, nd->nd_nam)) {
1c79356b
A
393 if ((rp->rc_flag & RC_LOCKED) != 0) {
394 rp->rc_flag |= RC_WANTED;
c3c9b80d 395 msleep(rp, &nfsrv_reqcache_mutex, PZERO - 1, "nfsrc", NULL);
1c79356b
A
396 goto loop;
397 }
398 rp->rc_flag |= RC_LOCKED;
0a7de745
A
399 if (rp->rc_state == RC_DONE) {
400 /*
401 * This can occur if the cache is too small.
402 * Retransmits of the same request aren't
403 * dropped so we may see the operation
404 * complete more then once.
405 */
406 if (rp->rc_flag & RC_REPMBUF) {
407 mbuf_freem(rp->rc_reply);
408 rp->rc_flag &= ~RC_REPMBUF;
409 }
2d21ac55 410 }
1c79356b
A
411 rp->rc_state = RC_DONE;
412 /*
413 * If we have a valid reply update status and save
414 * the reply for non-idempotent rpc's.
415 */
416 if (repvalid && nonidempotent[nd->nd_procnum]) {
2d21ac55 417 if ((nd->nd_vers == NFS_VER2) &&
0a7de745 418 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
1c79356b
A
419 rp->rc_status = nd->nd_repstat;
420 rp->rc_flag |= RC_REPSTATUS;
421 } else {
91447636 422 error = mbuf_copym(repmbuf, 0, MBUF_COPYALL, MBUF_WAITOK, &rp->rc_reply);
0a7de745 423 if (!error) {
91447636 424 rp->rc_flag |= RC_REPMBUF;
0a7de745 425 }
1c79356b
A
426 }
427 }
428 rp->rc_flag &= ~RC_LOCKED;
429 if (rp->rc_flag & RC_WANTED) {
430 rp->rc_flag &= ~RC_WANTED;
2d21ac55 431 wakeup(rp);
1c79356b 432 }
c3c9b80d 433 lck_mtx_unlock(&nfsrv_reqcache_mutex);
1c79356b
A
434 return;
435 }
436 }
c3c9b80d 437 lck_mtx_unlock(&nfsrv_reqcache_mutex);
1c79356b
A
438}
439
440/*
441 * Clean out the cache. Called when the last nfsd terminates.
442 */
443void
2d21ac55 444nfsrv_cleancache(void)
1c79356b 445{
91447636 446 struct nfsrvcache *rp, *nextrp;
1c79356b 447
c3c9b80d 448 lck_mtx_lock(&nfsrv_reqcache_mutex);
2d21ac55 449 for (rp = nfsrv_reqcache_lruhead.tqh_first; rp != 0; rp = nextrp) {
1c79356b
A
450 nextrp = rp->rc_lru.tqe_next;
451 LIST_REMOVE(rp, rc_hash);
2d21ac55 452 TAILQ_REMOVE(&nfsrv_reqcache_lruhead, rp, rc_lru);
1c79356b
A
453 _FREE(rp, M_NFSD);
454 }
2d21ac55
A
455 nfsrv_reqcache_count = 0;
456 FREE(nfsrv_reqcache_hashtbl, M_TEMP);
c3c9b80d 457 lck_mtx_unlock(&nfsrv_reqcache_mutex);
1c79356b
A
458}
459
ea3f0419 460#endif /* CONFIG_NFS_SERVER */