]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_subs.c
xnu-6153.101.6.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_subs.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2018 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_subs.c 8.8 (Berkeley) 5/22/95
65 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
66 */
67
ea3f0419
A
68#include <nfs/nfs_conf.h>
69#if CONFIG_NFS
70
1c79356b
A
71/*
72 * These functions support the macros and help fiddle mbuf chains for
73 * the nfs op functions. They do things like create the rpc header and
74 * copy data between mbuf chains and uio lists.
75 */
76#include <sys/param.h>
77#include <sys/proc.h>
91447636 78#include <sys/kauth.h>
1c79356b
A
79#include <sys/systm.h>
80#include <sys/kernel.h>
91447636
A
81#include <sys/mount_internal.h>
82#include <sys/vnode_internal.h>
83#include <sys/kpi_mbuf.h>
1c79356b 84#include <sys/socket.h>
cb323159 85#include <sys/un.h>
1c79356b
A
86#include <sys/stat.h>
87#include <sys/malloc.h>
88#include <sys/syscall.h>
91447636 89#include <sys/ubc_internal.h>
e5568f75 90#include <sys/fcntl.h>
b0d623f7 91#include <sys/uio.h>
91447636
A
92#include <sys/domain.h>
93#include <libkern/OSAtomic.h>
2d21ac55 94#include <kern/thread_call.h>
cb323159 95#include <kern/task.h>
1c79356b
A
96
97#include <sys/vm.h>
98#include <sys/vmparam.h>
1c79356b
A
99
100#include <sys/time.h>
101#include <kern/clock.h>
102
103#include <nfs/rpcv2.h>
104#include <nfs/nfsproto.h>
105#include <nfs/nfs.h>
106#include <nfs/nfsnode.h>
ea3f0419 107#if CONFIG_NFS_CLIENT
6d2010ae
A
108#define _NFS_XDR_SUBS_FUNCS_ /* define this to get xdrbuf function definitions */
109#endif
1c79356b
A
110#include <nfs/xdr_subs.h>
111#include <nfs/nfsm_subs.h>
2d21ac55 112#include <nfs/nfs_gss.h>
1c79356b 113#include <nfs/nfsmount.h>
e5568f75 114#include <nfs/nfs_lock.h>
1c79356b
A
115
116#include <miscfs/specfs/specdev.h>
117
118#include <netinet/in.h>
2d21ac55
A
119#include <net/kpi_interface.h>
120
6d2010ae
A
121#include <sys/utfconv.h>
122
1c79356b 123/*
2d21ac55 124 * NFS globals
1c79356b 125 */
0a7de745 126struct nfsstats __attribute__((aligned(8))) nfsstats;
2d21ac55
A
127size_t nfs_mbuf_mhlen = 0, nfs_mbuf_minclsize = 0;
128
129/*
130 * functions to convert between NFS and VFS types
131 */
132nfstype
133vtonfs_type(enum vtype vtype, int nfsvers)
134{
135 switch (vtype) {
136 case VNON:
137 return NFNON;
138 case VREG:
139 return NFREG;
140 case VDIR:
141 return NFDIR;
142 case VBLK:
143 return NFBLK;
144 case VCHR:
145 return NFCHR;
146 case VLNK:
147 return NFLNK;
148 case VSOCK:
0a7de745 149 if (nfsvers > NFS_VER2) {
2d21ac55 150 return NFSOCK;
0a7de745 151 }
2d21ac55 152 case VFIFO:
0a7de745 153 if (nfsvers > NFS_VER2) {
2d21ac55 154 return NFFIFO;
0a7de745 155 }
2d21ac55
A
156 case VBAD:
157 case VSTR:
158 case VCPLX:
159 default:
160 return NFNON;
161 }
162}
163
164enum vtype
165nfstov_type(nfstype nvtype, int nfsvers)
166{
167 switch (nvtype) {
168 case NFNON:
169 return VNON;
170 case NFREG:
171 return VREG;
172 case NFDIR:
173 return VDIR;
174 case NFBLK:
175 return VBLK;
176 case NFCHR:
177 return VCHR;
178 case NFLNK:
179 return VLNK;
180 case NFSOCK:
0a7de745 181 if (nfsvers > NFS_VER2) {
2d21ac55 182 return VSOCK;
0a7de745 183 }
2d21ac55 184 case NFFIFO:
0a7de745 185 if (nfsvers > NFS_VER2) {
2d21ac55 186 return VFIFO;
0a7de745 187 }
2d21ac55 188 case NFATTRDIR:
0a7de745 189 if (nfsvers > NFS_VER3) {
2d21ac55 190 return VDIR;
0a7de745 191 }
2d21ac55 192 case NFNAMEDATTR:
0a7de745 193 if (nfsvers > NFS_VER3) {
2d21ac55 194 return VREG;
0a7de745 195 }
2d21ac55
A
196 default:
197 return VNON;
198 }
199}
200
201int
202vtonfsv2_mode(enum vtype vtype, mode_t m)
203{
b0d623f7
A
204 switch (vtype) {
205 case VNON:
206 case VREG:
207 case VDIR:
208 case VBLK:
209 case VCHR:
210 case VLNK:
211 case VSOCK:
212 return vnode_makeimode(vtype, m);
213 case VFIFO:
2d21ac55 214 return vnode_makeimode(VCHR, m);
b0d623f7
A
215 case VBAD:
216 case VSTR:
217 case VCPLX:
218 default:
219 return vnode_makeimode(VNON, m);
220 }
2d21ac55
A
221}
222
ea3f0419 223#if CONFIG_NFS_SERVER
1c79356b 224
1c79356b
A
225/*
226 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
227 */
228int nfsv3_procid[NFS_NPROCS] = {
229 NFSPROC_NULL,
230 NFSPROC_GETATTR,
231 NFSPROC_SETATTR,
232 NFSPROC_NOOP,
233 NFSPROC_LOOKUP,
234 NFSPROC_READLINK,
235 NFSPROC_READ,
236 NFSPROC_NOOP,
237 NFSPROC_WRITE,
238 NFSPROC_CREATE,
239 NFSPROC_REMOVE,
240 NFSPROC_RENAME,
241 NFSPROC_LINK,
242 NFSPROC_SYMLINK,
243 NFSPROC_MKDIR,
244 NFSPROC_RMDIR,
245 NFSPROC_READDIR,
246 NFSPROC_FSSTAT,
247 NFSPROC_NOOP,
248 NFSPROC_NOOP,
249 NFSPROC_NOOP,
250 NFSPROC_NOOP,
1c79356b
A
251 NFSPROC_NOOP
252};
253
ea3f0419 254#endif /* CONFIG_NFS_SERVER */
2d21ac55 255
1c79356b
A
256/*
257 * and the reverse mapping from generic to Version 2 procedure numbers
258 */
259int nfsv2_procid[NFS_NPROCS] = {
260 NFSV2PROC_NULL,
261 NFSV2PROC_GETATTR,
262 NFSV2PROC_SETATTR,
263 NFSV2PROC_LOOKUP,
264 NFSV2PROC_NOOP,
265 NFSV2PROC_READLINK,
266 NFSV2PROC_READ,
267 NFSV2PROC_WRITE,
268 NFSV2PROC_CREATE,
269 NFSV2PROC_MKDIR,
270 NFSV2PROC_SYMLINK,
271 NFSV2PROC_CREATE,
272 NFSV2PROC_REMOVE,
273 NFSV2PROC_RMDIR,
274 NFSV2PROC_RENAME,
275 NFSV2PROC_LINK,
276 NFSV2PROC_READDIR,
277 NFSV2PROC_NOOP,
278 NFSV2PROC_STATFS,
279 NFSV2PROC_NOOP,
280 NFSV2PROC_NOOP,
281 NFSV2PROC_NOOP,
91447636 282 NFSV2PROC_NOOP
1c79356b
A
283};
284
2d21ac55 285
1c79356b 286/*
2d21ac55 287 * initialize NFS's cache of mbuf constants
1c79356b 288 */
2d21ac55
A
289void
290nfs_mbuf_init(void)
291{
292 struct mbuf_stat ms;
293
294 mbuf_stats(&ms);
295 nfs_mbuf_mhlen = ms.mhlen;
296 nfs_mbuf_minclsize = ms.minclsize;
297}
298
ea3f0419 299#if CONFIG_NFS_SERVER
1c79356b
A
300
301/*
2d21ac55 302 * allocate a list of mbufs to hold the given amount of data
1c79356b 303 */
2d21ac55
A
304int
305nfsm_mbuf_get_list(size_t size, mbuf_t *mp, int *mbcnt)
306{
307 int error, cnt;
308 mbuf_t mhead, mlast, m;
309 size_t len, mlen;
1c79356b 310
2d21ac55
A
311 error = cnt = 0;
312 mhead = mlast = NULL;
313 len = 0;
1c79356b 314
2d21ac55
A
315 while (len < size) {
316 nfsm_mbuf_get(error, &m, (size - len));
0a7de745 317 if (error) {
2d21ac55 318 break;
0a7de745
A
319 }
320 if (!mhead) {
2d21ac55 321 mhead = m;
0a7de745 322 }
2d21ac55
A
323 if (mlast && ((error = mbuf_setnext(mlast, m)))) {
324 mbuf_free(m);
325 break;
326 }
327 mlen = mbuf_maxlen(m);
0a7de745 328 if ((len + mlen) > size) {
2d21ac55 329 mlen = size - len;
0a7de745 330 }
2d21ac55
A
331 mbuf_setlen(m, mlen);
332 len += mlen;
333 cnt++;
334 mlast = m;
335 }
1c79356b 336
2d21ac55
A
337 if (!error) {
338 *mp = mhead;
339 *mbcnt = cnt;
340 }
0a7de745 341 return error;
2d21ac55 342}
1c79356b 343
ea3f0419 344#endif /* CONFIG_NFS_SERVER */
1c79356b 345
2d21ac55
A
346/*
347 * nfsm_chain_new_mbuf()
348 *
349 * Add a new mbuf to the given chain.
350 */
351int
352nfsm_chain_new_mbuf(struct nfsm_chain *nmc, size_t sizehint)
353{
354 mbuf_t mb;
355 int error = 0;
1c79356b 356
0a7de745 357 if (nmc->nmc_flags & NFSM_CHAIN_FLAG_ADD_CLUSTERS) {
2d21ac55 358 sizehint = nfs_mbuf_minclsize;
0a7de745 359 }
1c79356b 360
2d21ac55
A
361 /* allocate a new mbuf */
362 nfsm_mbuf_get(error, &mb, sizehint);
0a7de745
A
363 if (error) {
364 return error;
365 }
366 if (mb == NULL) {
2d21ac55 367 panic("got NULL mbuf?");
0a7de745 368 }
2d21ac55
A
369
370 /* do we have a current mbuf? */
371 if (nmc->nmc_mcur) {
372 /* first cap off current mbuf */
373 mbuf_setlen(nmc->nmc_mcur, nmc->nmc_ptr - (caddr_t)mbuf_data(nmc->nmc_mcur));
374 /* then append the new mbuf */
375 error = mbuf_setnext(nmc->nmc_mcur, mb);
376 if (error) {
377 mbuf_free(mb);
0a7de745 378 return error;
2d21ac55
A
379 }
380 }
1c79356b 381
2d21ac55
A
382 /* set up for using the new mbuf */
383 nmc->nmc_mcur = mb;
384 nmc->nmc_ptr = mbuf_data(mb);
385 nmc->nmc_left = mbuf_trailingspace(mb);
1c79356b 386
0a7de745 387 return 0;
2d21ac55 388}
1c79356b 389
2d21ac55
A
390/*
391 * nfsm_chain_add_opaque_f()
392 *
393 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
394 */
395int
396nfsm_chain_add_opaque_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
397{
398 uint32_t paddedlen, tlen;
399 int error;
1c79356b 400
2d21ac55 401 paddedlen = nfsm_rndup(len);
1c79356b 402
2d21ac55
A
403 while (paddedlen) {
404 if (!nmc->nmc_left) {
405 error = nfsm_chain_new_mbuf(nmc, paddedlen);
0a7de745
A
406 if (error) {
407 return error;
408 }
2d21ac55
A
409 }
410 tlen = MIN(nmc->nmc_left, paddedlen);
411 if (tlen) {
412 if (len) {
0a7de745 413 if (tlen > len) {
2d21ac55 414 tlen = len;
0a7de745 415 }
2d21ac55
A
416 bcopy(buf, nmc->nmc_ptr, tlen);
417 } else {
418 bzero(nmc->nmc_ptr, tlen);
419 }
420 nmc->nmc_ptr += tlen;
421 nmc->nmc_left -= tlen;
422 paddedlen -= tlen;
423 if (len) {
424 buf += tlen;
425 len -= tlen;
426 }
427 }
428 }
0a7de745 429 return 0;
2d21ac55 430}
1c79356b 431
1c79356b 432/*
2d21ac55
A
433 * nfsm_chain_add_opaque_nopad_f()
434 *
435 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
436 * Do not XDR pad.
1c79356b 437 */
91447636 438int
2d21ac55 439nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
1c79356b 440{
2d21ac55 441 uint32_t tlen;
91447636 442 int error;
1c79356b 443
2d21ac55
A
444 while (len > 0) {
445 if (nmc->nmc_left <= 0) {
446 error = nfsm_chain_new_mbuf(nmc, len);
0a7de745
A
447 if (error) {
448 return error;
449 }
2d21ac55
A
450 }
451 tlen = MIN(nmc->nmc_left, len);
452 bcopy(buf, nmc->nmc_ptr, tlen);
453 nmc->nmc_ptr += tlen;
454 nmc->nmc_left -= tlen;
455 len -= tlen;
456 buf += tlen;
457 }
0a7de745 458 return 0;
1c79356b
A
459}
460
461/*
2d21ac55
A
462 * nfsm_chain_add_uio()
463 *
464 * Add "len" bytes of data from "uio" to the given chain.
1c79356b 465 */
91447636 466int
b0d623f7 467nfsm_chain_add_uio(struct nfsm_chain *nmc, uio_t uio, uint32_t len)
1c79356b 468{
2d21ac55
A
469 uint32_t paddedlen, tlen;
470 int error;
1c79356b 471
2d21ac55 472 paddedlen = nfsm_rndup(len);
1c79356b 473
2d21ac55
A
474 while (paddedlen) {
475 if (!nmc->nmc_left) {
476 error = nfsm_chain_new_mbuf(nmc, paddedlen);
0a7de745
A
477 if (error) {
478 return error;
479 }
1c79356b 480 }
2d21ac55
A
481 tlen = MIN(nmc->nmc_left, paddedlen);
482 if (tlen) {
483 if (len) {
0a7de745 484 if (tlen > len) {
2d21ac55 485 tlen = len;
0a7de745 486 }
b0d623f7 487 uiomove(nmc->nmc_ptr, tlen, uio);
2d21ac55
A
488 } else {
489 bzero(nmc->nmc_ptr, tlen);
1c79356b 490 }
2d21ac55
A
491 nmc->nmc_ptr += tlen;
492 nmc->nmc_left -= tlen;
493 paddedlen -= tlen;
0a7de745 494 if (len) {
2d21ac55 495 len -= tlen;
0a7de745 496 }
1c79356b 497 }
91447636 498 }
0a7de745 499 return 0;
1c79356b
A
500}
501
502/*
2d21ac55
A
503 * Find the length of the NFS mbuf chain
504 * up to the current encoding/decoding offset.
1c79356b
A
505 */
506int
2d21ac55 507nfsm_chain_offset(struct nfsm_chain *nmc)
1c79356b 508{
2d21ac55
A
509 mbuf_t mb;
510 int len = 0;
1c79356b 511
2d21ac55 512 for (mb = nmc->nmc_mhead; mb; mb = mbuf_next(mb)) {
0a7de745
A
513 if (mb == nmc->nmc_mcur) {
514 return len + (nmc->nmc_ptr - (caddr_t) mbuf_data(mb));
515 }
2d21ac55 516 len += mbuf_len(mb);
1c79356b 517 }
2d21ac55 518
0a7de745 519 return len;
1c79356b
A
520}
521
522/*
2d21ac55
A
523 * nfsm_chain_advance()
524 *
525 * Advance an nfsm_chain by "len" bytes.
1c79356b
A
526 */
527int
2d21ac55 528nfsm_chain_advance(struct nfsm_chain *nmc, uint32_t len)
1c79356b 529{
2d21ac55 530 mbuf_t mb;
1c79356b 531
2d21ac55
A
532 while (len) {
533 if (nmc->nmc_left >= len) {
534 nmc->nmc_left -= len;
535 nmc->nmc_ptr += len;
0a7de745 536 return 0;
2d21ac55
A
537 }
538 len -= nmc->nmc_left;
539 nmc->nmc_mcur = mb = mbuf_next(nmc->nmc_mcur);
0a7de745
A
540 if (!mb) {
541 return EBADRPC;
542 }
2d21ac55
A
543 nmc->nmc_ptr = mbuf_data(mb);
544 nmc->nmc_left = mbuf_len(mb);
91447636 545 }
2d21ac55 546
0a7de745 547 return 0;
1c79356b
A
548}
549
550/*
2d21ac55
A
551 * nfsm_chain_reverse()
552 *
553 * Reverse decode offset in an nfsm_chain by "len" bytes.
1c79356b
A
554 */
555int
2d21ac55 556nfsm_chain_reverse(struct nfsm_chain *nmc, uint32_t len)
1c79356b 557{
2d21ac55
A
558 uint32_t mlen, new_offset;
559 int error = 0;
560
561 mlen = nmc->nmc_ptr - (caddr_t) mbuf_data(nmc->nmc_mcur);
562 if (len <= mlen) {
563 nmc->nmc_ptr -= len;
564 nmc->nmc_left += len;
0a7de745 565 return 0;
1c79356b 566 }
2d21ac55
A
567
568 new_offset = nfsm_chain_offset(nmc) - len;
569 nfsm_chain_dissect_init(error, nmc, nmc->nmc_mhead);
0a7de745
A
570 if (error) {
571 return error;
572 }
2d21ac55 573
0a7de745 574 return nfsm_chain_advance(nmc, new_offset);
2d21ac55
A
575}
576
577/*
578 * nfsm_chain_get_opaque_pointer_f()
579 *
580 * Return a pointer to the next "len" bytes of contiguous data in
581 * the mbuf chain. If the next "len" bytes are not contiguous, we
582 * try to manipulate the mbuf chain so that it is.
583 *
584 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
585 */
586int
587nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *nmc, uint32_t len, u_char **pptr)
588{
589 mbuf_t mbcur, mb;
590 uint32_t left, need, mblen, cplen, padlen;
591 u_char *ptr;
592 int error = 0;
593
594 /* move to next mbuf with data */
595 while (nmc->nmc_mcur && (nmc->nmc_left == 0)) {
596 mb = mbuf_next(nmc->nmc_mcur);
597 nmc->nmc_mcur = mb;
0a7de745 598 if (!mb) {
2d21ac55 599 break;
0a7de745 600 }
2d21ac55
A
601 nmc->nmc_ptr = mbuf_data(mb);
602 nmc->nmc_left = mbuf_len(mb);
603 }
604 /* check if we've run out of data */
0a7de745
A
605 if (!nmc->nmc_mcur) {
606 return EBADRPC;
607 }
2d21ac55
A
608
609 /* do we already have a contiguous buffer? */
610 if (nmc->nmc_left >= len) {
611 /* the returned pointer will be the current pointer */
612 *pptr = (u_char*)nmc->nmc_ptr;
613 error = nfsm_chain_advance(nmc, nfsm_rndup(len));
0a7de745 614 return error;
2d21ac55
A
615 }
616
617 padlen = nfsm_rndup(len) - len;
618
619 /* we need (len - left) more bytes */
620 mbcur = nmc->nmc_mcur;
621 left = nmc->nmc_left;
622 need = len - left;
623
624 if (need > mbuf_trailingspace(mbcur)) {
625 /*
626 * The needed bytes won't fit in the current mbuf so we'll
627 * allocate a new mbuf to hold the contiguous range of data.
628 */
629 nfsm_mbuf_get(error, &mb, len);
0a7de745
A
630 if (error) {
631 return error;
632 }
2d21ac55
A
633 /* double check that this mbuf can hold all the data */
634 if (mbuf_maxlen(mb) < len) {
635 mbuf_free(mb);
0a7de745 636 return EOVERFLOW;
2d21ac55
A
637 }
638
639 /* the returned pointer will be the new mbuf's data pointer */
640 *pptr = ptr = mbuf_data(mb);
641
642 /* copy "left" bytes to the new mbuf */
643 bcopy(nmc->nmc_ptr, ptr, left);
644 ptr += left;
645 mbuf_setlen(mb, left);
646
647 /* insert the new mbuf between the current and next mbufs */
648 error = mbuf_setnext(mb, mbuf_next(mbcur));
0a7de745 649 if (!error) {
2d21ac55 650 error = mbuf_setnext(mbcur, mb);
0a7de745 651 }
91447636 652 if (error) {
2d21ac55 653 mbuf_free(mb);
0a7de745 654 return error;
91447636 655 }
2d21ac55
A
656
657 /* reduce current mbuf's length by "left" */
658 mbuf_setlen(mbcur, mbuf_len(mbcur) - left);
659
660 /*
661 * update nmc's state to point at the end of the mbuf
662 * where the needed data will be copied to.
663 */
664 nmc->nmc_mcur = mbcur = mb;
665 nmc->nmc_left = 0;
666 nmc->nmc_ptr = (caddr_t)ptr;
667 } else {
668 /* The rest of the data will fit in this mbuf. */
669
670 /* the returned pointer will be the current pointer */
671 *pptr = (u_char*)nmc->nmc_ptr;
672
673 /*
674 * update nmc's state to point at the end of the mbuf
675 * where the needed data will be copied to.
676 */
677 nmc->nmc_ptr += left;
678 nmc->nmc_left = 0;
679 }
680
681 /*
682 * move the next "need" bytes into the current
683 * mbuf from the mbufs that follow
684 */
685
686 /* extend current mbuf length */
687 mbuf_setlen(mbcur, mbuf_len(mbcur) + need);
688
689 /* mb follows mbufs we're copying/compacting data from */
690 mb = mbuf_next(mbcur);
691
692 while (need && mb) {
693 /* copy as much as we need/can */
694 ptr = mbuf_data(mb);
695 mblen = mbuf_len(mb);
696 cplen = MIN(mblen, need);
697 if (cplen) {
698 bcopy(ptr, nmc->nmc_ptr, cplen);
699 /*
700 * update the mbuf's pointer and length to reflect that
701 * the data was shifted to an earlier mbuf in the chain
702 */
703 error = mbuf_setdata(mb, ptr + cplen, mblen - cplen);
704 if (error) {
705 mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
0a7de745 706 return error;
91447636 707 }
2d21ac55
A
708 /* update pointer/need */
709 nmc->nmc_ptr += cplen;
710 need -= cplen;
1c79356b 711 }
2d21ac55 712 /* if more needed, go to next mbuf */
0a7de745 713 if (need) {
2d21ac55 714 mb = mbuf_next(mb);
0a7de745 715 }
1c79356b 716 }
2d21ac55
A
717
718 /* did we run out of data in the mbuf chain? */
719 if (need) {
720 mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
0a7de745 721 return EBADRPC;
2d21ac55
A
722 }
723
724 /*
725 * update nmc's state to point after this contiguous data
726 *
727 * "mb" points to the last mbuf we copied data from so we
728 * just set nmc to point at whatever remains in that mbuf.
729 */
730 nmc->nmc_mcur = mb;
731 nmc->nmc_ptr = mbuf_data(mb);
732 nmc->nmc_left = mbuf_len(mb);
733
734 /* move past any padding */
0a7de745 735 if (padlen) {
2d21ac55 736 error = nfsm_chain_advance(nmc, padlen);
0a7de745 737 }
2d21ac55 738
0a7de745 739 return error;
1c79356b
A
740}
741
742/*
2d21ac55
A
743 * nfsm_chain_get_opaque_f()
744 *
745 * Read the next "len" bytes in the chain into "buf".
746 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
1c79356b
A
747 */
748int
2d21ac55 749nfsm_chain_get_opaque_f(struct nfsm_chain *nmc, uint32_t len, u_char *buf)
1c79356b 750{
2d21ac55
A
751 uint32_t cplen, padlen;
752 int error = 0;
753
754 padlen = nfsm_rndup(len) - len;
755
756 /* loop through mbufs copying all the data we need */
757 while (len && nmc->nmc_mcur) {
758 /* copy as much as we need/can */
759 cplen = MIN(nmc->nmc_left, len);
760 if (cplen) {
761 bcopy(nmc->nmc_ptr, buf, cplen);
762 nmc->nmc_ptr += cplen;
763 nmc->nmc_left -= cplen;
764 buf += cplen;
765 len -= cplen;
766 }
767 /* if more needed, go to next mbuf */
768 if (len) {
769 mbuf_t mb = mbuf_next(nmc->nmc_mcur);
770 nmc->nmc_mcur = mb;
771 nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
772 nmc->nmc_left = mb ? mbuf_len(mb) : 0;
773 }
1c79356b 774 }
2d21ac55
A
775
776 /* did we run out of data in the mbuf chain? */
0a7de745
A
777 if (len) {
778 return EBADRPC;
779 }
2d21ac55 780
0a7de745 781 if (padlen) {
2d21ac55 782 nfsm_chain_adv(error, nmc, padlen);
0a7de745 783 }
2d21ac55 784
0a7de745 785 return error;
1c79356b
A
786}
787
788/*
2d21ac55
A
789 * nfsm_chain_get_uio()
790 *
791 * Read the next "len" bytes in the chain into the given uio.
792 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
1c79356b
A
793 */
794int
b0d623f7 795nfsm_chain_get_uio(struct nfsm_chain *nmc, uint32_t len, uio_t uio)
1c79356b 796{
2d21ac55
A
797 uint32_t cplen, padlen;
798 int error = 0;
799
800 padlen = nfsm_rndup(len) - len;
801
802 /* loop through mbufs copying all the data we need */
803 while (len && nmc->nmc_mcur) {
804 /* copy as much as we need/can */
805 cplen = MIN(nmc->nmc_left, len);
806 if (cplen) {
b0d623f7 807 error = uiomove(nmc->nmc_ptr, cplen, uio);
0a7de745
A
808 if (error) {
809 return error;
810 }
2d21ac55
A
811 nmc->nmc_ptr += cplen;
812 nmc->nmc_left -= cplen;
813 len -= cplen;
814 }
815 /* if more needed, go to next mbuf */
816 if (len) {
817 mbuf_t mb = mbuf_next(nmc->nmc_mcur);
818 nmc->nmc_mcur = mb;
819 nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
820 nmc->nmc_left = mb ? mbuf_len(mb) : 0;
821 }
822 }
823
824 /* did we run out of data in the mbuf chain? */
0a7de745
A
825 if (len) {
826 return EBADRPC;
827 }
2d21ac55 828
0a7de745 829 if (padlen) {
2d21ac55 830 nfsm_chain_adv(error, nmc, padlen);
0a7de745 831 }
2d21ac55 832
0a7de745 833 return error;
2d21ac55
A
834}
835
ea3f0419 836#if CONFIG_NFS_CLIENT
2d21ac55 837
6d2010ae
A
838int
839nfsm_chain_add_string_nfc(struct nfsm_chain *nmc, const uint8_t *s, uint32_t slen)
840{
841 uint8_t smallbuf[64];
842 uint8_t *nfcname = smallbuf;
843 size_t buflen = sizeof(smallbuf), nfclen;
844 int error;
845
0a7de745 846 error = utf8_normalizestr(s, slen, nfcname, &nfclen, buflen, UTF_PRECOMPOSED | UTF_NO_NULL_TERM);
6d2010ae
A
847 if (error == ENAMETOOLONG) {
848 buflen = MAXPATHLEN;
849 MALLOC_ZONE(nfcname, uint8_t *, MAXPATHLEN, M_NAMEI, M_WAITOK);
0a7de745
A
850 if (nfcname) {
851 error = utf8_normalizestr(s, slen, nfcname, &nfclen, buflen, UTF_PRECOMPOSED | UTF_NO_NULL_TERM);
852 }
6d2010ae
A
853 }
854
855 /* if we got an error, just use the original string */
0a7de745 856 if (error) {
6d2010ae 857 nfsm_chain_add_string(error, nmc, s, slen);
0a7de745 858 } else {
6d2010ae 859 nfsm_chain_add_string(error, nmc, nfcname, nfclen);
0a7de745 860 }
6d2010ae 861
0a7de745 862 if (nfcname && (nfcname != smallbuf)) {
6d2010ae 863 FREE_ZONE(nfcname, MAXPATHLEN, M_NAMEI);
0a7de745
A
864 }
865 return error;
6d2010ae
A
866}
867
2d21ac55
A
868/*
869 * Add an NFSv2 "sattr" structure to an mbuf chain
870 */
871int
872nfsm_chain_add_v2sattr_f(struct nfsm_chain *nmc, struct vnode_attr *vap, uint32_t szrdev)
873{
874 int error = 0;
875
876 nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type,
0a7de745 877 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600)));
2d21ac55 878 nfsm_chain_add_32(error, nmc,
0a7de745 879 VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uint32_t)-1);
2d21ac55 880 nfsm_chain_add_32(error, nmc,
0a7de745 881 VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (uint32_t)-1);
2d21ac55
A
882 nfsm_chain_add_32(error, nmc, szrdev);
883 nfsm_chain_add_v2time(error, nmc,
0a7de745
A
884 VATTR_IS_ACTIVE(vap, va_access_time) ?
885 &vap->va_access_time : NULL);
2d21ac55 886 nfsm_chain_add_v2time(error, nmc,
0a7de745
A
887 VATTR_IS_ACTIVE(vap, va_modify_time) ?
888 &vap->va_modify_time : NULL);
2d21ac55 889
0a7de745 890 return error;
2d21ac55
A
891}
892
893/*
894 * Add an NFSv3 "sattr" structure to an mbuf chain
895 */
896int
cb323159 897nfsm_chain_add_v3sattr_f(
c6bf4f31 898 __unused struct nfsmount *nmp,
cb323159
A
899 struct nfsm_chain *nmc,
900 struct vnode_attr *vap)
2d21ac55
A
901{
902 int error = 0;
903
904 if (VATTR_IS_ACTIVE(vap, va_mode)) {
905 nfsm_chain_add_32(error, nmc, TRUE);
906 nfsm_chain_add_32(error, nmc, vap->va_mode);
907 } else {
908 nfsm_chain_add_32(error, nmc, FALSE);
909 }
910 if (VATTR_IS_ACTIVE(vap, va_uid)) {
911 nfsm_chain_add_32(error, nmc, TRUE);
912 nfsm_chain_add_32(error, nmc, vap->va_uid);
913 } else {
914 nfsm_chain_add_32(error, nmc, FALSE);
915 }
916 if (VATTR_IS_ACTIVE(vap, va_gid)) {
917 nfsm_chain_add_32(error, nmc, TRUE);
918 nfsm_chain_add_32(error, nmc, vap->va_gid);
919 } else {
920 nfsm_chain_add_32(error, nmc, FALSE);
921 }
922 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
923 nfsm_chain_add_32(error, nmc, TRUE);
924 nfsm_chain_add_64(error, nmc, vap->va_data_size);
925 } else {
926 nfsm_chain_add_32(error, nmc, FALSE);
927 }
928 if (vap->va_vaflags & VA_UTIMES_NULL) {
929 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
930 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
931 } else {
932 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
933 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
934 nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_sec);
935 nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_nsec);
936 } else {
937 nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
938 }
939 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
940 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
941 nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_sec);
942 nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_nsec);
1c79356b 943 } else {
2d21ac55 944 nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
1c79356b 945 }
1c79356b 946 }
2d21ac55 947
cb323159 948
0a7de745 949 return error;
1c79356b
A
950}
951
2d21ac55 952
1c79356b 953/*
2d21ac55
A
954 * nfsm_chain_get_fh_attr()
955 *
956 * Get the file handle and attributes from an mbuf chain. (NFSv2/v3)
1c79356b
A
957 */
958int
2d21ac55 959nfsm_chain_get_fh_attr(
cb323159 960 struct nfsmount *nmp,
2d21ac55
A
961 struct nfsm_chain *nmc,
962 nfsnode_t dnp,
963 vfs_context_t ctx,
964 int nfsvers,
965 uint64_t *xidp,
966 fhandle_t *fhp,
967 struct nfs_vattr *nvap)
1c79356b 968{
2d21ac55 969 int error = 0, gotfh, gotattr;
1c79356b 970
2d21ac55 971 gotfh = gotattr = 1;
1c79356b 972
0a7de745 973 if (nfsvers == NFS_VER3) { /* check for file handle */
2d21ac55 974 nfsm_chain_get_32(error, nmc, gotfh);
0a7de745
A
975 }
976 if (!error && gotfh) { /* get file handle */
2d21ac55 977 nfsm_chain_get_fh(error, nmc, nfsvers, fhp);
0a7de745 978 } else {
2d21ac55 979 fhp->fh_len = 0;
0a7de745
A
980 }
981 if (nfsvers == NFS_VER3) { /* check for file attributes */
2d21ac55 982 nfsm_chain_get_32(error, nmc, gotattr);
0a7de745 983 }
2d21ac55
A
984 nfsmout_if(error);
985 if (gotattr) {
0a7de745 986 if (!gotfh) { /* skip attributes */
2d21ac55 987 nfsm_chain_adv(error, nmc, NFSX_V3FATTR);
0a7de745 988 } else { /* get attributes */
cb323159 989 error = nfs_parsefattr(nmp, nmc, nfsvers, nvap);
0a7de745 990 }
2d21ac55
A
991 } else if (gotfh) {
992 /* we need valid attributes in order to call nfs_nget() */
6d2010ae 993 if (nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp)) {
2d21ac55
A
994 gotattr = 0;
995 fhp->fh_len = 0;
996 }
997 }
998nfsmout:
0a7de745 999 return error;
2d21ac55 1000}
1c79356b 1001
2d21ac55
A
1002/*
1003 * Get and process NFSv3 WCC data from an mbuf chain
1004 */
1005int
1006nfsm_chain_get_wcc_data_f(
1007 struct nfsm_chain *nmc,
1008 nfsnode_t np,
1009 struct timespec *premtime,
1010 int *newpostattr,
1011 u_int64_t *xidp)
1012{
1013 int error = 0;
1014 uint32_t flag = 0;
1015
1016 nfsm_chain_get_32(error, nmc, flag);
1017 if (!error && flag) {
1018 nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
1019 nfsm_chain_get_32(error, nmc, premtime->tv_sec);
1020 nfsm_chain_get_32(error, nmc, premtime->tv_nsec);
1021 nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
1022 } else {
1023 premtime->tv_sec = 0;
1024 premtime->tv_nsec = 0;
1025 }
1026 nfsm_chain_postop_attr_update_flag(error, nmc, np, *newpostattr, xidp);
1c79356b 1027
0a7de745 1028 return error;
1c79356b
A
1029}
1030
b0d623f7
A
1031/*
1032 * Get the next RPC transaction ID (XID)
1033 */
1034void
1035nfs_get_xid(uint64_t *xidp)
1036{
1037 struct timeval tv;
1038
1039 lck_mtx_lock(nfs_request_mutex);
1040 if (!nfs_xid) {
1041 /*
1042 * Derive initial xid from system time.
1043 *
1044 * Note: it's OK if this code inits nfs_xid to 0 (for example,
1045 * due to a broken clock) because we immediately increment it
1046 * and we guarantee to never use xid 0. So, nfs_xid should only
1047 * ever be 0 the first time this function is called.
1048 */
1049 microtime(&tv);
1050 nfs_xid = tv.tv_sec << 12;
1051 }
1052 if (++nfs_xid == 0) {
1053 /* Skip zero xid if it should ever happen. */
1054 nfs_xidwrap++;
1055 nfs_xid++;
1056 }
1057 *xidp = nfs_xid + ((uint64_t)nfs_xidwrap << 32);
1058 lck_mtx_unlock(nfs_request_mutex);
1059}
1060
1c79356b 1061/*
2d21ac55
A
1062 * Build the RPC header and fill in the authorization info.
1063 * Returns the head of the mbuf list and the xid.
1c79356b 1064 */
2d21ac55
A
1065
1066int
1067nfsm_rpchead(
1068 struct nfsreq *req,
2d21ac55
A
1069 mbuf_t mrest,
1070 u_int64_t *xidp,
1071 mbuf_t *mreqp)
91447636 1072{
2d21ac55
A
1073 struct nfsmount *nmp = req->r_nmp;
1074 int nfsvers = nmp->nm_vers;
1075 int proc = ((nfsvers == NFS_VER2) ? nfsv2_procid[req->r_procnum] : (int)req->r_procnum);
91447636 1076
6d2010ae 1077 return nfsm_rpchead2(nmp, nmp->nm_sotype, NFS_PROG, nfsvers, proc,
0a7de745 1078 req->r_auth, req->r_cred, req, mrest, xidp, mreqp);
2d21ac55
A
1079}
1080
39236c6e
A
1081/*
1082 * get_auiliary_groups: Gets the supplementary groups from a credential.
1083 *
1084 * IN: cred: credential to get the associated groups from.
1085 * OUT: groups: An array of gids of NGROUPS size.
1086 * IN: count: The number of groups to get; i.e.; the number of groups the server supports
1087 *
0a7de745 1088 * returns: The number of groups found.
39236c6e
A
1089 *
1090 * Just a wrapper around kauth_cred_getgroups to handle the case of a server supporting less
0a7de745 1091 * than NGROUPS.
39236c6e
A
1092 */
1093static int
1094get_auxiliary_groups(kauth_cred_t cred, gid_t groups[NGROUPS], int count)
1095{
1096 gid_t pgid;
1097 int maxcount = count < NGROUPS ? count + 1 : NGROUPS;
1098 int i;
39236c6e 1099
0a7de745
A
1100 for (i = 0; i < NGROUPS; i++) {
1101 groups[i] = -2; /* Initialize to the nobody group */
1102 }
39236c6e 1103 (void)kauth_cred_getgroups(cred, groups, &maxcount);
0a7de745
A
1104 if (maxcount < 1) {
1105 return maxcount;
1106 }
1107
39236c6e
A
1108 /*
1109 * kauth_get_groups returns the primary group followed by the
1110 * users auxiliary groups. If the number of groups the server supports
1111 * is less than NGROUPS, then we will drop the first group so that
1112 * we can send one more group over the wire.
1113 */
1114
1115
1116 if (count < NGROUPS) {
1117 pgid = kauth_cred_getgid(cred);
1118 if (pgid == groups[0]) {
1119 maxcount -= 1;
0a7de745
A
1120 for (i = 0; i < maxcount; i++) {
1121 groups[i] = groups[i + 1];
39236c6e
A
1122 }
1123 }
1124 }
0a7de745
A
1125
1126 return maxcount;
39236c6e
A
1127}
1128
2d21ac55 1129int
c6bf4f31 1130nfsm_rpchead2(__unused struct nfsmount *nmp, int sotype, int prog, int vers, int proc, int auth_type,
0a7de745 1131 kauth_cred_t cred, struct nfsreq *req, mbuf_t mrest, u_int64_t *xidp, mbuf_t *mreqp)
2d21ac55
A
1132{
1133 mbuf_t mreq, mb;
39236c6e 1134 int error, i, auth_len = 0, authsiz, reqlen;
2d21ac55 1135 size_t headlen;
2d21ac55 1136 struct nfsm_chain nmreq;
39236c6e
A
1137 gid_t grouplist[NGROUPS];
1138 int groupcount;
2d21ac55 1139
6d2010ae
A
1140 /* calculate expected auth length */
1141 switch (auth_type) {
0a7de745
A
1142 case RPCAUTH_NONE:
1143 auth_len = 0;
1144 break;
1145 case RPCAUTH_SYS:
1146 {
1147 int count = nmp->nm_numgrps < NGROUPS ? nmp->nm_numgrps : NGROUPS;
6d2010ae 1148
0a7de745
A
1149 if (!cred) {
1150 return EINVAL;
1151 }
1152 groupcount = get_auxiliary_groups(cred, grouplist, count);
1153 if (groupcount < 0) {
1154 return EINVAL;
1155 }
1156 auth_len = ((uint32_t)groupcount + 5) * NFSX_UNSIGNED;
1157 break;
1158 }
cb323159 1159#if CONFIG_NFS_GSS
0a7de745
A
1160 case RPCAUTH_KRB5:
1161 case RPCAUTH_KRB5I:
1162 case RPCAUTH_KRB5P:
1163 if (!req || !cred) {
1164 return EINVAL;
6d2010ae 1165 }
0a7de745
A
1166 auth_len = 5 * NFSX_UNSIGNED + 0; // zero context handle for now
1167 break;
cb323159 1168#endif /* CONFIG_NFS_GSS */
0a7de745
A
1169 default:
1170 return EINVAL;
1171 }
2d21ac55 1172 authsiz = nfsm_rndup(auth_len);
6d2010ae
A
1173
1174 /* allocate the packet */
2d21ac55 1175 headlen = authsiz + 10 * NFSX_UNSIGNED;
0a7de745 1176 if (sotype == SOCK_STREAM) { /* also include room for any RPC Record Mark */
2d21ac55 1177 headlen += NFSX_UNSIGNED;
0a7de745 1178 }
2d21ac55
A
1179 if (headlen >= nfs_mbuf_minclsize) {
1180 error = mbuf_getpacket(MBUF_WAITOK, &mreq);
1181 } else {
1182 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mreq);
1183 if (!error) {
0a7de745 1184 if (headlen < nfs_mbuf_mhlen) {
2d21ac55 1185 mbuf_align_32(mreq, headlen);
0a7de745 1186 } else {
2d21ac55 1187 mbuf_align_32(mreq, 8 * NFSX_UNSIGNED);
0a7de745 1188 }
2d21ac55
A
1189 }
1190 }
1191 if (error) {
1192 /* unable to allocate packet */
1193 /* XXX should we keep statistics for these errors? */
0a7de745 1194 return error;
2d21ac55
A
1195 }
1196
1197 /*
1198 * If the caller gave us a non-zero XID then use it because
1199 * it may be a higher-level resend with a GSSAPI credential.
1200 * Otherwise, allocate a new one.
1201 */
0a7de745 1202 if (*xidp == 0) {
b0d623f7 1203 nfs_get_xid(xidp);
0a7de745 1204 }
2d21ac55
A
1205
1206 /* build the header(s) */
b0d623f7 1207 nfsm_chain_init(&nmreq, mreq);
2d21ac55
A
1208
1209 /* First, if it's a TCP stream insert space for an RPC record mark */
0a7de745 1210 if (sotype == SOCK_STREAM) {
2d21ac55 1211 nfsm_chain_add_32(error, &nmreq, 0);
0a7de745 1212 }
2d21ac55
A
1213
1214 /* Then the RPC header. */
1215 nfsm_chain_add_32(error, &nmreq, (*xidp & 0xffffffff));
1216 nfsm_chain_add_32(error, &nmreq, RPC_CALL);
1217 nfsm_chain_add_32(error, &nmreq, RPC_VER2);
1218 nfsm_chain_add_32(error, &nmreq, prog);
1219 nfsm_chain_add_32(error, &nmreq, vers);
1220 nfsm_chain_add_32(error, &nmreq, proc);
1221
cb323159 1222#if CONFIG_NFS_GSS
2d21ac55 1223add_cred:
cb323159 1224#endif
2d21ac55 1225 switch (auth_type) {
6d2010ae
A
1226 case RPCAUTH_NONE:
1227 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* auth */
0a7de745
A
1228 nfsm_chain_add_32(error, &nmreq, 0); /* length */
1229 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* verf */
1230 nfsm_chain_add_32(error, &nmreq, 0); /* length */
2d21ac55 1231 nfsm_chain_build_done(error, &nmreq);
6d2010ae 1232 /* Append the args mbufs */
0a7de745 1233 if (!error) {
6d2010ae 1234 error = mbuf_setnext(nmreq.nmc_mcur, mrest);
0a7de745 1235 }
2d21ac55 1236 break;
6d2010ae 1237 case RPCAUTH_SYS: {
6d2010ae 1238 nfsm_chain_add_32(error, &nmreq, RPCAUTH_SYS);
2d21ac55 1239 nfsm_chain_add_32(error, &nmreq, authsiz);
cb323159
A
1240 {
1241 nfsm_chain_add_32(error, &nmreq, 0); /* stamp */
1242 }
0a7de745
A
1243 nfsm_chain_add_32(error, &nmreq, 0); /* zero-length hostname */
1244 nfsm_chain_add_32(error, &nmreq, kauth_cred_getuid(cred)); /* UID */
1245 nfsm_chain_add_32(error, &nmreq, kauth_cred_getgid(cred)); /* GID */
39236c6e 1246 nfsm_chain_add_32(error, &nmreq, groupcount);/* additional GIDs */
0a7de745 1247 for (i = 0; i < groupcount; i++) {
6d2010ae 1248 nfsm_chain_add_32(error, &nmreq, grouplist[i]);
0a7de745 1249 }
2d21ac55
A
1250
1251 /* And the verifier... */
0a7de745
A
1252 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* flavor */
1253 nfsm_chain_add_32(error, &nmreq, 0); /* length */
2d21ac55
A
1254 nfsm_chain_build_done(error, &nmreq);
1255
1256 /* Append the args mbufs */
0a7de745 1257 if (!error) {
2d21ac55 1258 error = mbuf_setnext(nmreq.nmc_mcur, mrest);
0a7de745 1259 }
2d21ac55 1260 break;
6d2010ae 1261 }
cb323159 1262#if CONFIG_NFS_GSS
2d21ac55
A
1263 case RPCAUTH_KRB5:
1264 case RPCAUTH_KRB5I:
1265 case RPCAUTH_KRB5P:
1266 error = nfs_gss_clnt_cred_put(req, &nmreq, mrest);
1267 if (error == ENEEDAUTH) {
39236c6e
A
1268 int count = nmp->nm_numgrps < NGROUPS ? nmp->nm_numgrps : NGROUPS;
1269
2d21ac55
A
1270 /*
1271 * Use sec=sys for this user
1272 */
1273 error = 0;
6d2010ae 1274 req->r_auth = auth_type = RPCAUTH_SYS;
0a7de745
A
1275 groupcount = get_auxiliary_groups(cred, grouplist, count);
1276 if (groupcount < 0) {
1277 return EINVAL;
1278 }
1279 auth_len = ((uint32_t)groupcount + 5) * NFSX_UNSIGNED;
6d2010ae 1280 authsiz = nfsm_rndup(auth_len);
2d21ac55
A
1281 goto add_cred;
1282 }
1283 break;
cb323159 1284#endif /* CONFIG_NFS_GSS */
0a7de745
A
1285 }
1286 ;
2d21ac55
A
1287
1288 /* finish setting up the packet */
0a7de745 1289 if (!error) {
2d21ac55 1290 error = mbuf_pkthdr_setrcvif(mreq, 0);
0a7de745 1291 }
2d21ac55
A
1292
1293 if (error) {
1294 mbuf_freem(mreq);
0a7de745 1295 return error;
2d21ac55
A
1296 }
1297
1298 /* Calculate the size of the request */
1299 reqlen = 0;
0a7de745 1300 for (mb = nmreq.nmc_mhead; mb; mb = mbuf_next(mb)) {
2d21ac55 1301 reqlen += mbuf_len(mb);
0a7de745 1302 }
2d21ac55
A
1303
1304 mbuf_pkthdr_setlen(mreq, reqlen);
1305
1306 /*
1307 * If the request goes on a TCP stream,
1308 * set its size in the RPC record mark.
1309 * The record mark count doesn't include itself
1310 * and the last fragment bit is set.
1311 */
0a7de745 1312 if (sotype == SOCK_STREAM) {
2d21ac55 1313 nfsm_chain_set_recmark(error, &nmreq,
0a7de745
A
1314 (reqlen - NFSX_UNSIGNED) | 0x80000000);
1315 }
2d21ac55
A
1316
1317 *mreqp = mreq;
0a7de745 1318 return 0;
91447636 1319}
1c79356b
A
1320
1321/*
2d21ac55 1322 * Parse an NFS file attribute structure out of an mbuf chain.
1c79356b
A
1323 */
1324int
cb323159 1325nfs_parsefattr(
c6bf4f31 1326 __unused struct nfsmount *nmp,
cb323159
A
1327 struct nfsm_chain *nmc,
1328 int nfsvers,
1329 struct nfs_vattr *nvap)
1c79356b 1330{
2d21ac55 1331 int error = 0;
91447636 1332 enum vtype vtype;
316670eb 1333 nfstype nvtype;
1c79356b 1334 u_short vmode;
2d21ac55
A
1335 uint32_t val, val2;
1336 dev_t rdev;
55e303ae 1337
2d21ac55 1338 val = val2 = 0;
6d2010ae
A
1339 NVATTR_INIT(nvap);
1340
1341 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TYPE);
1342 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_MODE);
1343 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_NUMLINKS);
1344 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_OWNER);
1345 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP);
1346 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_SIZE);
1347 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_SPACE_USED);
1348 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_RAWDEV);
1349 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_FSID);
1350 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_FILEID);
1351 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_ACCESS);
1352 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_MODIFY);
1353 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_METADATA);
2d21ac55 1354
316670eb 1355 nfsm_chain_get_32(error, nmc, nvtype);
2d21ac55
A
1356 nfsm_chain_get_32(error, nmc, vmode);
1357 nfsmout_if(error);
1358
1359 if (nfsvers == NFS_VER3) {
316670eb 1360 nvap->nva_type = vtype = nfstov_type(nvtype, nfsvers);
1c79356b 1361 } else {
1c79356b 1362 /*
1c79356b
A
1363 * The duplicate information returned in fa_type and fa_mode
1364 * is an ambiguity in the NFS version 2 protocol.
1365 *
1366 * VREG should be taken literally as a regular file. If a
2d21ac55 1367 * server intends to return some type information differently
1c79356b
A
1368 * in the upper bits of the mode field (e.g. for sockets, or
1369 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1370 * leave the examination of the mode bits even in the VREG
1371 * case to avoid breakage for bogus servers, but we make sure
1372 * that there are actually type bits set in the upper part of
1373 * fa_mode (and failing that, trust the va_type field).
1374 *
1375 * NFSv3 cleared the issue, and requires fa_mode to not
2d21ac55
A
1376 * contain any type information (while also introducing
1377 * sockets and FIFOs for fa_type).
1c79356b 1378 */
316670eb 1379 vtype = nfstov_type(nvtype, nfsvers);
0a7de745 1380 if ((vtype == VNON) || ((vtype == VREG) && ((vmode & S_IFMT) != 0))) {
91447636 1381 vtype = IFTOVT(vmode);
0a7de745 1382 }
2d21ac55 1383 nvap->nva_type = vtype;
91447636
A
1384 }
1385
91447636 1386 nvap->nva_mode = (vmode & 07777);
91447636 1387
2d21ac55
A
1388 nfsm_chain_get_32(error, nmc, nvap->nva_nlink);
1389 nfsm_chain_get_32(error, nmc, nvap->nva_uid);
1390 nfsm_chain_get_32(error, nmc, nvap->nva_gid);
1391
1392 if (nfsvers == NFS_VER3) {
1393 nfsm_chain_get_64(error, nmc, nvap->nva_size);
1394 nfsm_chain_get_64(error, nmc, nvap->nva_bytes);
1395 nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata1);
1396 nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata2);
1397 nfsmout_if(error);
1398 nfsm_chain_get_64(error, nmc, nvap->nva_fsid.major);
1399 nvap->nva_fsid.minor = 0;
1400 nfsm_chain_get_64(error, nmc, nvap->nva_fileid);
1401 } else {
1402 nfsm_chain_get_32(error, nmc, nvap->nva_size);
1403 nfsm_chain_adv(error, nmc, NFSX_UNSIGNED);
1404 nfsm_chain_get_32(error, nmc, rdev);
1405 nfsmout_if(error);
1406 nvap->nva_rawdev.specdata1 = major(rdev);
1407 nvap->nva_rawdev.specdata2 = minor(rdev);
1408 nfsm_chain_get_32(error, nmc, val); /* blocks */
1409 nfsmout_if(error);
1410 nvap->nva_bytes = val * NFS_FABLKSIZE;
1411 nfsm_chain_get_32(error, nmc, val);
1412 nfsmout_if(error);
1413 nvap->nva_fsid.major = (uint64_t)val;
1414 nvap->nva_fsid.minor = 0;
1415 nfsm_chain_get_32(error, nmc, val);
1416 nfsmout_if(error);
1417 nvap->nva_fileid = (uint64_t)val;
1418 /* Really ugly NFSv2 kludge. */
0a7de745 1419 if ((vtype == VCHR) && (rdev == (dev_t)0xffffffff)) {
2d21ac55 1420 nvap->nva_type = VFIFO;
0a7de745 1421 }
2d21ac55
A
1422 }
1423 nfsm_chain_get_time(error, nmc, nfsvers,
0a7de745
A
1424 nvap->nva_timesec[NFSTIME_ACCESS],
1425 nvap->nva_timensec[NFSTIME_ACCESS]);
2d21ac55 1426 nfsm_chain_get_time(error, nmc, nfsvers,
0a7de745
A
1427 nvap->nva_timesec[NFSTIME_MODIFY],
1428 nvap->nva_timensec[NFSTIME_MODIFY]);
2d21ac55 1429 nfsm_chain_get_time(error, nmc, nfsvers,
0a7de745
A
1430 nvap->nva_timesec[NFSTIME_CHANGE],
1431 nvap->nva_timensec[NFSTIME_CHANGE]);
cb323159 1432
2d21ac55 1433nfsmout:
0a7de745 1434 return error;
91447636
A
1435}
1436
cb323159 1437
91447636
A
1438/*
1439 * Load the attribute cache (that lives in the nfsnode entry) with
1440 * the value pointed to by nvap, unless the file type in the attribute
1441 * cache doesn't match the file type in the nvap, in which case log a
1442 * warning and return ESTALE.
1443 *
1444 * If the dontshrink flag is set, then it's not safe to call ubc_setsize()
1445 * to shrink the size of the file.
1446 */
1447int
1448nfs_loadattrcache(
2d21ac55 1449 nfsnode_t np,
91447636
A
1450 struct nfs_vattr *nvap,
1451 u_int64_t *xidp,
1452 int dontshrink)
1453{
1454 mount_t mp;
1455 vnode_t vp;
1456 struct timeval now;
1457 struct nfs_vattr *npnvap;
6d2010ae
A
1458 int xattr = np->n_vattr.nva_flags & NFS_FFLAG_IS_ATTR;
1459 int referral = np->n_vattr.nva_flags & NFS_FFLAG_TRIGGER_REFERRAL;
1460 int aclbit, monitored, error = 0;
1461 kauth_acl_t acl;
1462 struct nfsmount *nmp;
1463 uint32_t events = np->n_events;
91447636 1464
2d21ac55 1465 if (np->n_hflag & NHINIT) {
91447636
A
1466 vp = NULL;
1467 mp = np->n_mount;
1468 } else {
1469 vp = NFSTOV(np);
1470 mp = vnode_mount(vp);
1471 }
6d2010ae 1472 monitored = vp ? vnode_ismonitored(vp) : 0;
91447636 1473
2d21ac55 1474 FSDBG_TOP(527, np, vp, *xidp >> 32, *xidp);
91447636 1475
6d2010ae 1476 if (!((nmp = VFSTONFS(mp)))) {
91447636 1477 FSDBG_BOT(527, ENXIO, 1, 0, *xidp);
0a7de745 1478 return ENXIO;
1c79356b
A
1479 }
1480
55e303ae 1481 if (*xidp < np->n_xid) {
fa4905b1
A
1482 /*
1483 * We have already updated attributes with a response from
1484 * a later request. The attributes we have here are probably
2d21ac55 1485 * stale so we drop them (just return). However, our
fa4905b1
A
1486 * out-of-order receipt could be correct - if the requests were
1487 * processed out of order at the server. Given the uncertainty
1488 * we invalidate our cached attributes. *xidp is zeroed here
1489 * to indicate the attributes were dropped - only getattr
1490 * cares - it needs to retry the rpc.
1491 */
91447636 1492 NATTRINVALIDATE(np);
fa4905b1
A
1493 FSDBG_BOT(527, 0, np, np->n_xid, *xidp);
1494 *xidp = 0;
0a7de745 1495 return 0;
fa4905b1 1496 }
1c79356b 1497
91447636
A
1498 if (vp && (nvap->nva_type != vnode_vtype(vp))) {
1499 /*
1500 * The filehandle has changed type on us. This can be
1501 * caused by either the server not having unique filehandles
1502 * or because another client has removed the previous
1503 * filehandle and a new object (of a different type)
1504 * has been created with the same filehandle.
1505 *
1506 * We can't simply switch the type on the vnode because
1507 * there may be type-specific fields that need to be
1508 * cleaned up or set up.
1509 *
1510 * So, what should we do with this vnode?
1511 *
1512 * About the best we can do is log a warning and return
1513 * an error. ESTALE is about the closest error, but it
1514 * is a little strange that we come up with this error
1515 * internally instead of simply passing it through from
1516 * the server. Hopefully, the vnode will be reclaimed
1517 * soon so the filehandle can be reincarnated as the new
1518 * object type.
1519 */
1520 printf("nfs loadattrcache vnode changed type, was %d now %d\n",
0a7de745 1521 vnode_vtype(vp), nvap->nva_type);
6d2010ae 1522 error = ESTALE;
0a7de745 1523 if (monitored) {
6d2010ae 1524 events |= VNODE_EVENT_DELETE;
0a7de745 1525 }
6d2010ae 1526 goto out;
1c79356b
A
1527 }
1528
6d2010ae
A
1529 npnvap = &np->n_vattr;
1530
1531 /*
1532 * The ACL cache needs special handling because it is not
1533 * always updated. Save current ACL cache state so it can
1534 * be restored after copying the new attributes into place.
1535 */
1536 aclbit = NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL);
1537 acl = npnvap->nva_acl;
1538
1539 if (monitored) {
1540 /*
1541 * For monitored nodes, check for attribute changes that should generate events.
1542 */
1543 if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_NUMLINKS) &&
0a7de745 1544 (nvap->nva_nlink != npnvap->nva_nlink)) {
6d2010ae 1545 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_LINK;
0a7de745
A
1546 }
1547 if (events & VNODE_EVENT_PERMS) {
6d2010ae 1548 /* no need to do all the checking if it's already set */;
0a7de745
A
1549 } else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_MODE) &&
1550 (nvap->nva_mode != npnvap->nva_mode)) {
6d2010ae 1551 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
0a7de745
A
1552 } else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER) &&
1553 (nvap->nva_uid != npnvap->nva_uid)) {
6d2010ae 1554 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
0a7de745
A
1555 } else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP) &&
1556 (nvap->nva_gid != npnvap->nva_gid)) {
6d2010ae 1557 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
cb323159 1558#if CONFIG_NFS4
0a7de745 1559 } else if (nmp->nm_vers >= NFS_VER4) {
6d2010ae 1560 if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER) &&
0a7de745 1561 !kauth_guid_equal(&nvap->nva_uuuid, &npnvap->nva_uuuid)) {
6d2010ae 1562 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
0a7de745
A
1563 } else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP) &&
1564 !kauth_guid_equal(&nvap->nva_guuid, &npnvap->nva_guuid)) {
6d2010ae 1565 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
0a7de745
A
1566 } else if ((NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_ACL) &&
1567 nvap->nva_acl && npnvap->nva_acl &&
1568 ((nvap->nva_acl->acl_entrycount != npnvap->nva_acl->acl_entrycount) ||
1569 bcmp(nvap->nva_acl, npnvap->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl))))) {
6d2010ae 1570 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
0a7de745 1571 }
cb323159 1572#endif
6d2010ae 1573 }
cb323159
A
1574 if (/* Oh, C... */
1575#if CONFIG_NFS4
1576 ((nmp->nm_vers >= NFS_VER4) && (nvap->nva_change != npnvap->nva_change)) ||
1577#endif
1578 (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_MODIFY) &&
1579 ((nvap->nva_timesec[NFSTIME_MODIFY] != npnvap->nva_timesec[NFSTIME_MODIFY]) ||
1580 (nvap->nva_timensec[NFSTIME_MODIFY] != npnvap->nva_timensec[NFSTIME_MODIFY])))) {
6d2010ae 1581 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_WRITE;
0a7de745 1582 }
6d2010ae
A
1583 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_RAWDEV) &&
1584 ((nvap->nva_rawdev.specdata1 != npnvap->nva_rawdev.specdata1) ||
0a7de745 1585 (nvap->nva_rawdev.specdata2 != npnvap->nva_rawdev.specdata2))) {
6d2010ae 1586 events |= VNODE_EVENT_ATTRIB;
0a7de745 1587 }
6d2010ae 1588 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_FILEID) &&
0a7de745 1589 (nvap->nva_fileid != npnvap->nva_fileid)) {
6d2010ae 1590 events |= VNODE_EVENT_ATTRIB;
0a7de745 1591 }
6d2010ae 1592 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ARCHIVE) &&
0a7de745 1593 ((nvap->nva_flags & NFS_FFLAG_ARCHIVED) != (npnvap->nva_flags & NFS_FFLAG_ARCHIVED))) {
6d2010ae 1594 events |= VNODE_EVENT_ATTRIB;
0a7de745 1595 }
6d2010ae 1596 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_HIDDEN) &&
0a7de745 1597 ((nvap->nva_flags & NFS_FFLAG_HIDDEN) != (npnvap->nva_flags & NFS_FFLAG_HIDDEN))) {
6d2010ae 1598 events |= VNODE_EVENT_ATTRIB;
0a7de745 1599 }
6d2010ae
A
1600 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_CREATE) &&
1601 ((nvap->nva_timesec[NFSTIME_CREATE] != npnvap->nva_timesec[NFSTIME_CREATE]) ||
0a7de745 1602 (nvap->nva_timensec[NFSTIME_CREATE] != npnvap->nva_timensec[NFSTIME_CREATE]))) {
6d2010ae 1603 events |= VNODE_EVENT_ATTRIB;
0a7de745 1604 }
6d2010ae
A
1605 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_BACKUP) &&
1606 ((nvap->nva_timesec[NFSTIME_BACKUP] != npnvap->nva_timesec[NFSTIME_BACKUP]) ||
0a7de745 1607 (nvap->nva_timensec[NFSTIME_BACKUP] != npnvap->nva_timensec[NFSTIME_BACKUP]))) {
6d2010ae 1608 events |= VNODE_EVENT_ATTRIB;
0a7de745 1609 }
6d2010ae
A
1610 }
1611
1612 /* Copy the attributes to the attribute cache */
1613 bcopy((caddr_t)nvap, (caddr_t)npnvap, sizeof(*nvap));
1614
55e303ae
A
1615 microuptime(&now);
1616 np->n_attrstamp = now.tv_sec;
91447636 1617 np->n_xid = *xidp;
6d2010ae 1618 /* NFS_FFLAG_IS_ATTR and NFS_FFLAG_TRIGGER_REFERRAL need to be sticky... */
0a7de745 1619 if (vp && xattr) {
6d2010ae 1620 nvap->nva_flags |= xattr;
0a7de745
A
1621 }
1622 if (vp && referral) {
6d2010ae 1623 nvap->nva_flags |= referral;
0a7de745 1624 }
6d2010ae
A
1625
1626 if (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL)) {
1627 /* we're updating the ACL */
1628 if (nvap->nva_acl) {
1629 /* make a copy of the acl for the cache */
1630 npnvap->nva_acl = kauth_acl_alloc(nvap->nva_acl->acl_entrycount);
1631 if (npnvap->nva_acl) {
1632 bcopy(nvap->nva_acl, npnvap->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl));
1633 } else {
1634 /* can't make a copy to cache, invalidate ACL cache */
1635 NFS_BITMAP_CLR(npnvap->nva_bitmap, NFS_FATTR_ACL);
1636 NACLINVALIDATE(np);
1637 aclbit = 0;
1638 }
1639 }
1640 if (acl) {
1641 kauth_acl_free(acl);
1642 acl = NULL;
1643 }
1644 }
1645 if (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL)) {
1646 /* update the ACL timestamp */
1647 np->n_aclstamp = now.tv_sec;
1648 } else {
1649 /* we aren't updating the ACL, so restore original values */
0a7de745 1650 if (aclbit) {
6d2010ae 1651 NFS_BITMAP_SET(npnvap->nva_bitmap, NFS_FATTR_ACL);
0a7de745 1652 }
6d2010ae
A
1653 npnvap->nva_acl = acl;
1654 }
55e303ae 1655
6d2010ae 1656#if CONFIG_TRIGGERS
cb323159 1657#if CONFIG_NFS4
6d2010ae
A
1658 /*
1659 * For NFSv4, if the fsid doesn't match the fsid for the mount, then
1660 * this node is for a different file system on the server. So we mark
1661 * this node as a trigger node that will trigger the mirror mount.
1662 */
1663 if ((nmp->nm_vers >= NFS_VER4) && (nvap->nva_type == VDIR) &&
1664 ((np->n_vattr.nva_fsid.major != nmp->nm_fsid.major) ||
0a7de745 1665 (np->n_vattr.nva_fsid.minor != nmp->nm_fsid.minor))) {
6d2010ae 1666 np->n_vattr.nva_flags |= NFS_FFLAG_TRIGGER;
0a7de745 1667 }
cb323159
A
1668#endif /* CONFIG_NFS4 */
1669#endif /* CONFIG_TRIGGERS */
1c79356b 1670
b0d623f7
A
1671 if (!vp || (nvap->nva_type != VREG)) {
1672 np->n_size = nvap->nva_size;
1673 } else if (nvap->nva_size != np->n_size) {
2d21ac55 1674 FSDBG(527, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
b0d623f7
A
1675 if (!UBCINFOEXISTS(vp) || (dontshrink && (nvap->nva_size < np->n_size))) {
1676 /* asked not to shrink, so stick with current size */
1677 FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
1678 nvap->nva_size = np->n_size;
1679 NATTRINVALIDATE(np);
1680 } else if ((np->n_flag & NMODIFIED) && (nvap->nva_size < np->n_size)) {
1681 /* if we've modified, stick with larger size */
1682 FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002);
1683 nvap->nva_size = np->n_size;
1684 npnvap->nva_size = np->n_size;
1685 } else {
1686 /*
1687 * n_size is protected by the data lock, so we need to
1688 * defer updating it until it's safe. We save the new size
1689 * and set a flag and it'll get updated the next time we get/drop
1690 * the data lock or the next time we do a getattr.
1691 */
1692 np->n_newsize = nvap->nva_size;
1693 SET(np->n_flag, NUPDATESIZE);
0a7de745 1694 if (monitored) {
6d2010ae 1695 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_EXTEND;
0a7de745 1696 }
91447636 1697 }
1c79356b
A
1698 }
1699
91447636 1700 if (np->n_flag & NCHG) {
2d21ac55
A
1701 if (np->n_flag & NACC) {
1702 nvap->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
1703 nvap->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
1704 }
1705 if (np->n_flag & NUPD) {
1706 nvap->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
1707 nvap->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
1708 }
1c79356b 1709 }
91447636 1710
6d2010ae 1711out:
0a7de745 1712 if (monitored && events) {
6d2010ae 1713 nfs_vnode_notify(np, events);
0a7de745 1714 }
6d2010ae 1715 FSDBG_BOT(527, error, np, np->n_size, *xidp);
0a7de745 1716 return error;
1c79356b
A
1717}
1718
1719/*
91447636
A
1720 * Calculate the attribute timeout based on
1721 * how recently the file has been modified.
1c79356b
A
1722 */
1723int
2d21ac55 1724nfs_attrcachetimeout(nfsnode_t np)
1c79356b 1725{
91447636
A
1726 struct nfsmount *nmp;
1727 struct timeval now;
6d2010ae
A
1728 int isdir;
1729 uint32_t timeo;
55e303ae 1730
fe8ab488 1731 nmp = NFSTONMP(np);
0a7de745
A
1732 if (nfs_mount_gone(nmp)) {
1733 return 0;
1734 }
91447636 1735
2d21ac55 1736 isdir = vnode_isdir(NFSTOV(np));
cb323159 1737#if CONFIG_NFS4
6d2010ae
A
1738 if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK)) {
1739 /* If we have a delegation, we always use the max timeout. */
1740 timeo = isdir ? nmp->nm_acdirmax : nmp->nm_acregmax;
cb323159
A
1741 } else
1742#endif
1743 if ((np)->n_flag & NMODIFIED) {
6d2010ae 1744 /* If we have modifications, we always use the min timeout. */
91447636 1745 timeo = isdir ? nmp->nm_acdirmin : nmp->nm_acregmin;
6d2010ae
A
1746 } else {
1747 /* Otherwise, we base the timeout on how old the file seems. */
55e303ae
A
1748 /* Note that if the client and server clocks are way out of sync, */
1749 /* timeout will probably get clamped to a min or max value */
1750 microtime(&now);
b0d623f7 1751 timeo = (now.tv_sec - (np)->n_vattr.nva_timesec[NFSTIME_MODIFY]) / 10;
91447636 1752 if (isdir) {
0a7de745 1753 if (timeo < nmp->nm_acdirmin) {
91447636 1754 timeo = nmp->nm_acdirmin;
0a7de745 1755 } else if (timeo > nmp->nm_acdirmax) {
91447636 1756 timeo = nmp->nm_acdirmax;
0a7de745 1757 }
91447636 1758 } else {
0a7de745 1759 if (timeo < nmp->nm_acregmin) {
91447636 1760 timeo = nmp->nm_acregmin;
0a7de745 1761 } else if (timeo > nmp->nm_acregmax) {
91447636 1762 timeo = nmp->nm_acregmax;
0a7de745 1763 }
91447636 1764 }
55e303ae 1765 }
1c79356b 1766
0a7de745 1767 return timeo;
91447636
A
1768}
1769
1770/*
b0d623f7 1771 * Check the attribute cache time stamp.
91447636 1772 * If the cache is valid, copy contents to *nvaper and return 0
b0d623f7
A
1773 * otherwise return an error.
1774 * Must be called with the node locked.
91447636
A
1775 */
1776int
6d2010ae 1777nfs_getattrcache(nfsnode_t np, struct nfs_vattr *nvaper, int flags)
91447636 1778{
91447636
A
1779 struct nfs_vattr *nvap;
1780 struct timeval nowup;
1781 int32_t timeo;
fe8ab488 1782 struct nfsmount *nmp;
91447636 1783
6d2010ae
A
1784 /* Check if the attributes are valid. */
1785 if (!NATTRVALID(np) || ((flags & NGA_ACL) && !NACLVALID(np))) {
2d21ac55 1786 FSDBG(528, np, 0, 0xffffff01, ENOENT);
316670eb 1787 OSAddAtomic64(1, &nfsstats.attrcache_misses);
0a7de745 1788 return ENOENT;
91447636
A
1789 }
1790
fe8ab488 1791 nmp = NFSTONMP(np);
0a7de745
A
1792 if (nfs_mount_gone(nmp)) {
1793 return ENXIO;
1794 }
fe8ab488
A
1795 /*
1796 * Verify the cached attributes haven't timed out.
1797 * If the server isn't responding, skip the check
1798 * and return cached attributes.
1799 */
1800 if (!nfs_use_cache(nmp)) {
fe8ab488 1801 microuptime(&nowup);
d9a64523
A
1802 if (np->n_attrstamp > nowup.tv_sec) {
1803 printf("NFS: Attribute time stamp is in the future by %ld seconds. Invalidating cache\n",
0a7de745 1804 np->n_attrstamp - nowup.tv_sec);
d9a64523
A
1805 NATTRINVALIDATE(np);
1806 NACCESSINVALIDATE(np);
0a7de745 1807 return ENOENT;
d9a64523
A
1808 }
1809 timeo = nfs_attrcachetimeout(np);
fe8ab488
A
1810 if ((nowup.tv_sec - np->n_attrstamp) >= timeo) {
1811 FSDBG(528, np, 0, 0xffffff02, ENOENT);
1812 OSAddAtomic64(1, &nfsstats.attrcache_misses);
0a7de745 1813 return ENOENT;
fe8ab488
A
1814 }
1815 if ((flags & NGA_ACL) && ((nowup.tv_sec - np->n_aclstamp) >= timeo)) {
1816 FSDBG(528, np, 0, 0xffffff02, ENOENT);
1817 OSAddAtomic64(1, &nfsstats.attrcache_misses);
0a7de745 1818 return ENOENT;
fe8ab488 1819 }
6d2010ae 1820 }
2d21ac55 1821
91447636 1822 nvap = &np->n_vattr;
2d21ac55 1823 FSDBG(528, np, nvap->nva_size, np->n_size, 0xcace);
316670eb 1824 OSAddAtomic64(1, &nfsstats.attrcache_hits);
1c79356b 1825
b0d623f7
A
1826 if (nvap->nva_type != VREG) {
1827 np->n_size = nvap->nva_size;
1828 } else if (nvap->nva_size != np->n_size) {
2d21ac55 1829 FSDBG(528, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
b0d623f7
A
1830 if ((np->n_flag & NMODIFIED) && (nvap->nva_size < np->n_size)) {
1831 /* if we've modified, stick with larger size */
2d21ac55 1832 nvap->nva_size = np->n_size;
b0d623f7
A
1833 } else {
1834 /*
1835 * n_size is protected by the data lock, so we need to
1836 * defer updating it until it's safe. We save the new size
1837 * and set a flag and it'll get updated the next time we get/drop
1838 * the data lock or the next time we do a getattr.
1839 */
1840 np->n_newsize = nvap->nva_size;
1841 SET(np->n_flag, NUPDATESIZE);
2d21ac55 1842 }
1c79356b
A
1843 }
1844
91447636 1845 bcopy((caddr_t)nvap, (caddr_t)nvaper, sizeof(struct nfs_vattr));
1c79356b 1846 if (np->n_flag & NCHG) {
2d21ac55
A
1847 if (np->n_flag & NACC) {
1848 nvaper->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
1849 nvaper->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
1850 }
1851 if (np->n_flag & NUPD) {
1852 nvaper->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
1853 nvaper->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
1854 }
1c79356b 1855 }
6d2010ae
A
1856 if (nvap->nva_acl) {
1857 if (flags & NGA_ACL) {
1858 nvaper->nva_acl = kauth_acl_alloc(nvap->nva_acl->acl_entrycount);
0a7de745
A
1859 if (!nvaper->nva_acl) {
1860 return ENOMEM;
1861 }
6d2010ae
A
1862 bcopy(nvap->nva_acl, nvaper->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl));
1863 } else {
1864 nvaper->nva_acl = NULL;
1865 }
1866 }
0a7de745 1867 return 0;
1c79356b
A
1868}
1869
6d2010ae
A
1870/*
1871 * When creating file system objects:
1872 * Don't bother setting UID if it's the same as the credential performing the create.
1873 * Don't bother setting GID if it's the same as the directory or credential.
1874 */
1875void
1876nfs_avoid_needless_id_setting_on_create(nfsnode_t dnp, struct vnode_attr *vap, vfs_context_t ctx)
1877{
1878 if (VATTR_IS_ACTIVE(vap, va_uid)) {
1879 if (kauth_cred_getuid(vfs_context_ucred(ctx)) == vap->va_uid) {
1880 VATTR_CLEAR_ACTIVE(vap, va_uid);
1881 VATTR_CLEAR_ACTIVE(vap, va_uuuid);
1882 }
1883 }
1884 if (VATTR_IS_ACTIVE(vap, va_gid)) {
1885 if ((vap->va_gid == dnp->n_vattr.nva_gid) ||
1886 (kauth_cred_getgid(vfs_context_ucred(ctx)) == vap->va_gid)) {
1887 VATTR_CLEAR_ACTIVE(vap, va_gid);
1888 VATTR_CLEAR_ACTIVE(vap, va_guuid);
1889 }
1890 }
1891}
1892
1893/*
1894 * Convert a universal address string to a sockaddr structure.
1895 *
1896 * Universal addresses can be in the following formats:
1897 *
1898 * d = decimal (IPv4)
1899 * x = hexadecimal (IPv6)
1900 * p = port (decimal)
1901 *
1902 * d.d.d.d
1903 * d.d.d.d.p.p
1904 * x:x:x:x:x:x:x:x
1905 * x:x:x:x:x:x:x:x.p.p
1906 * x:x:x:x:x:x:d.d.d.d
1907 * x:x:x:x:x:x:d.d.d.d.p.p
1908 *
1909 * IPv6 strings can also have a series of zeroes elided
1910 * IPv6 strings can also have a %scope suffix at the end (after any port)
1911 *
1912 * rules & exceptions:
1913 * - value before : is hex
1914 * - value before . is dec
1915 * - once . hit, all values are dec
1916 * - hex+port case means value before first dot is actually hex
1917 * - . is always preceded by digits except if last hex was double-colon
1918 *
1919 * scan, converting #s to bytes
1920 * first time a . is encountered, scan the rest to count them.
1921 * 2 dots = just port
1922 * 3 dots = just IPv4 no port
1923 * 5 dots = IPv4 and port
1924 */
1925
1926#define IS_DIGIT(C) \
1927 (((C) >= '0') && ((C) <= '9'))
1928
1929#define IS_XDIGIT(C) \
1930 (IS_DIGIT(C) || \
1931 (((C) >= 'A') && ((C) <= 'F')) || \
1932 (((C) >= 'a') && ((C) <= 'f')))
1933
1934int
1935nfs_uaddr2sockaddr(const char *uaddr, struct sockaddr *addr)
1936{
0a7de745
A
1937 const char *p, *pd; /* pointers to current character in scan */
1938 const char *pnum; /* pointer to current number to decode */
1939 const char *pscope; /* pointer to IPv6 scope ID */
1940 uint8_t a[18]; /* octet array to store address bytes */
1941 int i; /* index of next octet to decode */
1942 int dci; /* index of octet to insert double-colon zeroes */
1943 int dcount, xdcount; /* count of digits in current number */
1944 int needmore; /* set when we know we need more input (e.g. after colon, period) */
1945 int dots; /* # of dots */
1946 int hex; /* contains hex values */
1947 unsigned long val; /* decoded value */
1948 int s; /* index used for sliding array to insert elided zeroes */
1949
cb323159
A
1950 /* AF_LOCAL address are paths that start with '/' or are empty */
1951 if (*uaddr == '/' || *uaddr == '\0') { /* AF_LOCAL address */
1952 struct sockaddr_un *sun = (struct sockaddr_un *)addr;
1953 sun->sun_family = AF_LOCAL;
1954 sun->sun_len = sizeof(struct sockaddr_un);
1955 strlcpy(sun->sun_path, uaddr, sizeof(sun->sun_path));
1956
1957 return 1;
1958 }
1959
0a7de745
A
1960#define HEXVALUE 0
1961#define DECIMALVALUE 1
cb323159 1962
6d2010ae
A
1963#define GET(TYPE) \
1964 do { \
0a7de745
A
1965 if ((dcount <= 0) || (dcount > (((TYPE) == DECIMALVALUE) ? 3 : 4))) \
1966 return (0); \
1967 if (((TYPE) == DECIMALVALUE) && xdcount) \
1968 return (0); \
1969 val = strtoul(pnum, NULL, ((TYPE) == DECIMALVALUE) ? 10 : 16); \
1970 if (((TYPE) == DECIMALVALUE) && (val >= 256)) \
1971 return (0); \
1972 /* check if there is room left in the array */ \
1973 if (i > (int)(sizeof(a) - (((TYPE) == HEXVALUE) ? 2 : 1) - ((dci != -1) ? 2 : 0))) \
1974 return (0); \
1975 if ((TYPE) == HEXVALUE) \
1976 a[i++] = ((val >> 8) & 0xff); \
1977 a[i++] = (val & 0xff); \
6d2010ae
A
1978 } while (0)
1979
1980 hex = 0;
1981 dots = 0;
1982 dci = -1;
1983 i = dcount = xdcount = 0;
1984 pnum = p = uaddr;
1985 pscope = NULL;
1986 needmore = 1;
0a7de745
A
1987 if ((*p == ':') && (*++p != ':')) { /* if it starts with colon, gotta be a double */
1988 return 0;
1989 }
6d2010ae
A
1990
1991 while (*p) {
1992 if (IS_XDIGIT(*p)) {
1993 dcount++;
0a7de745 1994 if (!IS_DIGIT(*p)) {
6d2010ae 1995 xdcount++;
0a7de745 1996 }
6d2010ae
A
1997 needmore = 0;
1998 p++;
1999 } else if (*p == '.') {
2000 /* rest is decimal IPv4 dotted quad and/or port */
2001 if (!dots) {
2002 /* this is the first, so count them */
2003 for (pd = p; *pd; pd++) {
2004 if (*pd == '.') {
0a7de745
A
2005 if (++dots > 5) {
2006 return 0;
2007 }
6d2010ae
A
2008 } else if (hex && (*pd == '%')) {
2009 break;
2010 } else if ((*pd < '0') || (*pd > '9')) {
0a7de745 2011 return 0;
6d2010ae
A
2012 }
2013 }
0a7de745
A
2014 if ((dots != 2) && (dots != 3) && (dots != 5)) {
2015 return 0;
2016 }
6d2010ae 2017 if (hex && (dots == 2)) { /* hex+port */
0a7de745
A
2018 if (!dcount && needmore) {
2019 return 0;
2020 }
2021 if (dcount) { /* last hex may be elided zero */
6d2010ae 2022 GET(HEXVALUE);
0a7de745 2023 }
6d2010ae
A
2024 } else {
2025 GET(DECIMALVALUE);
2026 }
2027 } else {
2028 GET(DECIMALVALUE);
2029 }
2030 dcount = xdcount = 0;
2031 needmore = 1;
2032 pnum = ++p;
2033 } else if (*p == ':') {
2034 hex = 1;
0a7de745
A
2035 if (dots) {
2036 return 0;
2037 }
6d2010ae 2038 if (!dcount) { /* missing number, probably double colon */
0a7de745
A
2039 if (dci >= 0) { /* can only have one double colon */
2040 return 0;
2041 }
6d2010ae
A
2042 dci = i;
2043 needmore = 0;
2044 } else {
2045 GET(HEXVALUE);
2046 dcount = xdcount = 0;
2047 needmore = 1;
2048 }
2049 pnum = ++p;
2050 } else if (*p == '%') { /* scope ID delimiter */
0a7de745
A
2051 if (!hex) {
2052 return 0;
2053 }
6d2010ae
A
2054 p++;
2055 pscope = p;
2056 break;
2057 } else { /* unexpected character */
0a7de745 2058 return 0;
6d2010ae
A
2059 }
2060 }
0a7de745
A
2061 if (needmore && !dcount) {
2062 return 0;
2063 }
2064 if (dcount) { /* decode trailing number */
6d2010ae 2065 GET(dots ? DECIMALVALUE : HEXVALUE);
0a7de745 2066 }
6d2010ae
A
2067 if (dci >= 0) { /* got a double-colon at i, need to insert a range of zeroes */
2068 /* if we got a port, slide to end of array */
2069 /* otherwise, slide to end of address (non-port) values */
2070 int end = ((dots == 2) || (dots == 5)) ? sizeof(a) : (sizeof(a) - 2);
0a7de745
A
2071 if (i % 2) { /* length of zero range must be multiple of 2 */
2072 return 0;
2073 }
2074 if (i >= end) { /* no room? */
2075 return 0;
2076 }
6d2010ae 2077 /* slide (i-dci) numbers up from index dci */
0a7de745
A
2078 for (s = 0; s < (i - dci); s++) {
2079 a[end - 1 - s] = a[i - 1 - s];
2080 }
6d2010ae 2081 /* zero (end-i) numbers at index dci */
0a7de745
A
2082 for (s = 0; s < (end - i); s++) {
2083 a[dci + s] = 0;
2084 }
6d2010ae
A
2085 i = end;
2086 }
2087
2088 /* copy out resulting socket address */
2089 if (hex) {
2090 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addr;
0a7de745
A
2091 if ((((dots == 0) || (dots == 3)) && (i != (sizeof(a) - 2)))) {
2092 return 0;
2093 }
2094 if ((((dots == 2) || (dots == 5)) && (i != sizeof(a)))) {
2095 return 0;
2096 }
6d2010ae
A
2097 bzero(sin6, sizeof(struct sockaddr_in6));
2098 sin6->sin6_len = sizeof(struct sockaddr_in6);
2099 sin6->sin6_family = AF_INET6;
2100 bcopy(a, &sin6->sin6_addr.s6_addr, sizeof(struct in6_addr));
0a7de745 2101 if ((dots == 5) || (dots == 2)) {
6d2010ae 2102 sin6->sin6_port = htons((a[16] << 8) | a[17]);
0a7de745 2103 }
6d2010ae 2104 if (pscope) {
0a7de745 2105 for (p = pscope; IS_DIGIT(*p); p++) {
6d2010ae 2106 ;
0a7de745 2107 }
6d2010ae
A
2108 if (*p && !IS_DIGIT(*p)) { /* name */
2109 ifnet_t interface = NULL;
0a7de745 2110 if (ifnet_find_by_name(pscope, &interface) == 0) {
6d2010ae 2111 sin6->sin6_scope_id = ifnet_index(interface);
0a7de745
A
2112 }
2113 if (interface) {
6d2010ae 2114 ifnet_release(interface);
0a7de745 2115 }
6d2010ae
A
2116 } else { /* decimal number */
2117 sin6->sin6_scope_id = strtoul(pscope, NULL, 10);
2118 }
2119 /* XXX should we also embed scope id for linklocal? */
2120 }
2121 } else {
2122 struct sockaddr_in *sin = (struct sockaddr_in*)addr;
0a7de745
A
2123 if ((dots != 3) && (dots != 5)) {
2124 return 0;
2125 }
2126 if ((dots == 3) && (i != 4)) {
2127 return 0;
2128 }
2129 if ((dots == 5) && (i != 6)) {
2130 return 0;
2131 }
6d2010ae
A
2132 bzero(sin, sizeof(struct sockaddr_in));
2133 sin->sin_len = sizeof(struct sockaddr_in);
2134 sin->sin_family = AF_INET;
2135 bcopy(a, &sin->sin_addr.s_addr, sizeof(struct in_addr));
0a7de745 2136 if (dots == 5) {
6d2010ae 2137 sin->sin_port = htons((a[4] << 8) | a[5]);
0a7de745 2138 }
6d2010ae 2139 }
0a7de745 2140 return 1;
6d2010ae
A
2141}
2142
2143
39236c6e
A
2144/* NFS Client debugging support */
2145uint32_t nfs_debug_ctl;
2146
2147#include <libkern/libkern.h>
2148#include <stdarg.h>
2149
2150void
cb323159 2151nfs_printf(unsigned int facility, unsigned int level, const char *fmt, ...)
39236c6e
A
2152{
2153 va_list ap;
0a7de745 2154
cb323159
A
2155 if (NFS_IS_DBG(facility, level)) {
2156 va_start(ap, fmt);
2157 vprintf(fmt, ap);
2158 va_end(ap);
0a7de745 2159 }
cb323159
A
2160}
2161
2162
2163#define DISPLAYLEN 16
2164
2165static bool
2166isprint(int ch)
2167{
2168 return ch >= 0x20 && ch <= 0x7e;
2169}
2170
2171static void
2172hexdump(void *data, size_t len)
2173{
2174 size_t i, j;
2175 unsigned char *d = data;
2176 char *p, disbuf[3 * DISPLAYLEN + 1];
2177
2178 for (i = 0; i < len; i += DISPLAYLEN) {
2179 for (p = disbuf, j = 0; (j + i) < len && j < DISPLAYLEN; j++, p += 3) {
2180 snprintf(p, 4, "%2.2x ", d[i + j]);
2181 }
2182 for (; j < DISPLAYLEN; j++, p += 3) {
2183 snprintf(p, 4, " ");
2184 }
2185 printf("%s ", disbuf);
2186 for (p = disbuf, j = 0; (j + i) < len && j < DISPLAYLEN; j++, p++) {
2187 snprintf(p, 2, "%c", isprint(d[i + j]) ? d[i + j] : '.');
2188 }
2189 printf("%s\n", disbuf);
0a7de745 2190 }
cb323159 2191}
0a7de745 2192
cb323159
A
2193void
2194nfs_dump_mbuf(const char *func, int lineno, const char *msg, mbuf_t mb)
2195{
2196 mbuf_t m;
2197
2198 printf("%s:%d %s\n", func, lineno, msg);
2199 for (m = mb; m; m = mbuf_next(m)) {
2200 hexdump(mbuf_data(m), mbuf_len(m));
2201 }
39236c6e
A
2202}
2203
fe8ab488
A
2204/* Is a mount gone away? */
2205int
2206nfs_mount_gone(struct nfsmount *nmp)
2207{
0a7de745 2208 return !nmp || vfs_isforce(nmp->nm_mountp) || (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD));
fe8ab488
A
2209}
2210
2211/*
2212 * Return some of the more significant mount options
2213 * as a string, e.g. "'ro,hard,intr,tcp,vers=3,sec=krb5,deadtimeout=0'
2214 */
2215int
2216nfs_mountopts(struct nfsmount *nmp, char *buf, int buflen)
2217{
2218 int c;
2219
2220 c = snprintf(buf, buflen, "%s,%s,%s,%s,vers=%d,sec=%s,%sdeadtimeout=%d",
0a7de745
A
2221 (vfs_flags(nmp->nm_mountp) & MNT_RDONLY) ? "ro" : "rw",
2222 NMFLAG(nmp, SOFT) ? "soft" : "hard",
2223 NMFLAG(nmp, INTR) ? "intr" : "nointr",
2224 nmp->nm_sotype == SOCK_STREAM ? "tcp" : "udp",
2225 nmp->nm_vers,
2226 nmp->nm_auth == RPCAUTH_KRB5 ? "krb5" :
2227 nmp->nm_auth == RPCAUTH_KRB5I ? "krb5i" :
2228 nmp->nm_auth == RPCAUTH_KRB5P ? "krb5p" :
2229 nmp->nm_auth == RPCAUTH_SYS ? "sys" : "none",
2230 nmp->nm_lockmode == NFS_LOCK_MODE_ENABLED ? "locks," :
2231 nmp->nm_lockmode == NFS_LOCK_MODE_DISABLED ? "nolocks," :
2232 nmp->nm_lockmode == NFS_LOCK_MODE_LOCAL ? "locallocks," : "",
2233 nmp->nm_deadtimeout);
2234
2235 return c > buflen ? ENOMEM : 0;
fe8ab488
A
2236}
2237
ea3f0419 2238#endif /* CONFIG_NFS_CLIENT */
2d21ac55
A
2239
2240/*
2241 * Schedule a callout thread to run an NFS timer function
2242 * interval milliseconds in the future.
2243 */
2244void
2245nfs_interval_timer_start(thread_call_t call, int interval)
2246{
2247 uint64_t deadline;
2248
2249 clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
2250 thread_call_enter_delayed(call, deadline);
2251}
2252
2253
ea3f0419 2254#if CONFIG_NFS_SERVER
2d21ac55 2255
b0d623f7
A
2256int nfsrv_cmp_secflavs(struct nfs_sec *, struct nfs_sec *);
2257int nfsrv_hang_addrlist(struct nfs_export *, struct user_nfs_export_args *);
2258int nfsrv_free_netopt(struct radix_node *, void *);
2259int nfsrv_free_addrlist(struct nfs_export *, struct user_nfs_export_args *);
2260struct nfs_export_options *nfsrv_export_lookup(struct nfs_export *, mbuf_t);
2261struct nfs_export *nfsrv_fhtoexport(struct nfs_filehandle *);
6d2010ae 2262struct nfs_user_stat_node *nfsrv_get_user_stat_node(struct nfs_active_user_list *, struct sockaddr *, uid_t);
b0d623f7
A
2263void nfsrv_init_user_list(struct nfs_active_user_list *);
2264void nfsrv_free_user_list(struct nfs_active_user_list *);
2d21ac55
A
2265
2266/*
2267 * add NFSv3 WCC data to an mbuf chain
2268 */
2269int
2270nfsm_chain_add_wcc_data_f(
2271 struct nfsrv_descript *nd,
2272 struct nfsm_chain *nmc,
2273 int preattrerr,
2274 struct vnode_attr *prevap,
2275 int postattrerr,
2276 struct vnode_attr *postvap)
2277{
2278 int error = 0;
2279
2280 if (preattrerr) {
2281 nfsm_chain_add_32(error, nmc, FALSE);
2282 } else {
2283 nfsm_chain_add_32(error, nmc, TRUE);
2284 nfsm_chain_add_64(error, nmc, prevap->va_data_size);
2285 nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_modify_time);
2286 nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_change_time);
2287 }
2288 nfsm_chain_add_postop_attr(error, nd, nmc, postattrerr, postvap);
2289
0a7de745 2290 return error;
2d21ac55
A
2291}
2292
1c79356b 2293/*
91447636
A
2294 * Extract a lookup path from the given mbufs and store it in
2295 * a newly allocated buffer saved in the given nameidata structure.
1c79356b
A
2296 */
2297int
2d21ac55
A
2298nfsm_chain_get_path_namei(
2299 struct nfsm_chain *nmc,
2300 uint32_t len,
2301 struct nameidata *nip)
1c79356b 2302{
2d21ac55
A
2303 struct componentname *cnp = &nip->ni_cnd;
2304 int error = 0;
2305 char *cp;
2306
0a7de745
A
2307 if (len > (MAXPATHLEN - 1)) {
2308 return ENAMETOOLONG;
2309 }
55e303ae 2310
91447636
A
2311 /*
2312 * Get a buffer for the name to be translated, and copy the
2313 * name into the buffer.
2314 */
2315 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
0a7de745
A
2316 if (!cnp->cn_pnbuf) {
2317 return ENOMEM;
2318 }
55e303ae 2319 cnp->cn_pnlen = MAXPATHLEN;
91447636 2320 cnp->cn_flags |= HASBUF;
1c79356b 2321
2d21ac55
A
2322 /* Copy the name from the mbuf list to the string */
2323 cp = cnp->cn_pnbuf;
2324 nfsm_chain_get_opaque(error, nmc, len, cp);
0a7de745 2325 if (error) {
2d21ac55 2326 goto out;
0a7de745 2327 }
2d21ac55 2328 cnp->cn_pnbuf[len] = '\0';
91447636 2329
2d21ac55 2330 /* sanity check the string */
0a7de745 2331 if ((strlen(cp) != len) || strchr(cp, '/')) {
2d21ac55 2332 error = EACCES;
0a7de745 2333 }
91447636
A
2334out:
2335 if (error) {
0a7de745 2336 if (cnp->cn_pnbuf) {
91447636 2337 FREE_ZONE(cnp->cn_pnbuf, MAXPATHLEN, M_NAMEI);
0a7de745 2338 }
91447636
A
2339 cnp->cn_flags &= ~HASBUF;
2340 } else {
2d21ac55 2341 nip->ni_pathlen = len;
91447636 2342 }
0a7de745 2343 return error;
91447636
A
2344}
2345
2346/*
2347 * Set up nameidata for a lookup() call and do it.
91447636
A
2348 */
2349int
2d21ac55
A
2350nfsrv_namei(
2351 struct nfsrv_descript *nd,
2352 vfs_context_t ctx,
2353 struct nameidata *nip,
91447636 2354 struct nfs_filehandle *nfhp,
91447636
A
2355 vnode_t *retdirp,
2356 struct nfs_export **nxp,
2357 struct nfs_export_options **nxop)
2358{
91447636
A
2359 vnode_t dp;
2360 int error;
2d21ac55 2361 struct componentname *cnp = &nip->ni_cnd;
b0d623f7 2362 uint32_t cnflags;
91447636
A
2363 char *tmppn;
2364
2365 *retdirp = NULL;
2366
1c79356b
A
2367 /*
2368 * Extract and set starting directory.
2369 */
2d21ac55 2370 error = nfsrv_fhtovp(nfhp, nd, &dp, nxp, nxop);
0a7de745 2371 if (error) {
1c79356b 2372 goto out;
0a7de745 2373 }
2d21ac55 2374 error = nfsrv_credcheck(nd, ctx, *nxp, *nxop);
91447636
A
2375 if (error || (vnode_vtype(dp) != VDIR)) {
2376 vnode_put(dp);
1c79356b
A
2377 error = ENOTDIR;
2378 goto out;
2379 }
2d21ac55 2380 *retdirp = dp;
1c79356b 2381
2d21ac55 2382 nip->ni_cnd.cn_context = ctx;
91447636 2383
0a7de745 2384 if (*nxop && ((*nxop)->nxo_flags & NX_READONLY)) {
1c79356b 2385 cnp->cn_flags |= RDONLY;
0a7de745 2386 }
1c79356b 2387
1c79356b 2388 cnp->cn_flags |= NOCROSSMOUNT;
1c79356b 2389 cnp->cn_nameptr = cnp->cn_pnbuf;
2d21ac55 2390 nip->ni_usedvp = nip->ni_startdir = dp;
813fb2f6 2391 nip->ni_rootdir = rootvnode;
2d21ac55 2392
1c79356b
A
2393 /*
2394 * And call lookup() to do the real work
2395 */
b0d623f7
A
2396 cnflags = nip->ni_cnd.cn_flags; /* store in case we have to restore */
2397 while ((error = lookup(nip)) == ERECYCLE) {
2398 nip->ni_cnd.cn_flags = cnflags;
2399 cnp->cn_nameptr = cnp->cn_pnbuf;
2400 nip->ni_usedvp = nip->ni_dvp = nip->ni_startdir = dp;
2401 }
0a7de745 2402 if (error) {
2d21ac55 2403 goto out;
0a7de745 2404 }
2d21ac55
A
2405
2406 /* Check for encountering a symbolic link */
2407 if (cnp->cn_flags & ISSYMLINK) {
0a7de745 2408 if (cnp->cn_flags & (LOCKPARENT | WANTPARENT)) {
2d21ac55 2409 vnode_put(nip->ni_dvp);
0a7de745 2410 }
2d21ac55
A
2411 if (nip->ni_vp) {
2412 vnode_put(nip->ni_vp);
2413 nip->ni_vp = NULL;
1c79356b 2414 }
2d21ac55 2415 error = EINVAL;
1c79356b 2416 }
1c79356b 2417out:
2d21ac55
A
2418 if (error) {
2419 tmppn = cnp->cn_pnbuf;
2420 cnp->cn_pnbuf = NULL;
2421 cnp->cn_flags &= ~HASBUF;
2422 FREE_ZONE(tmppn, cnp->cn_pnlen, M_NAMEI);
2423 }
0a7de745 2424 return error;
1c79356b
A
2425}
2426
2427/*
b0d623f7 2428 * A fiddled version of m_adj() that ensures null fill to a 4-byte
1c79356b
A
2429 * boundary and only trims off the back end
2430 */
2431void
2d21ac55 2432nfsm_adj(mbuf_t mp, int len, int nul)
1c79356b 2433{
91447636
A
2434 mbuf_t m, mnext;
2435 int count, i, mlen;
2436 char *cp;
1c79356b
A
2437
2438 /*
2439 * Trim from tail. Scan the mbuf chain,
2440 * calculating its length and finding the last mbuf.
2441 * If the adjustment only affects this mbuf, then just
2442 * adjust and return. Otherwise, rescan and truncate
2443 * after the remaining size.
2444 */
2445 count = 0;
2446 m = mp;
2447 for (;;) {
91447636
A
2448 mlen = mbuf_len(m);
2449 count += mlen;
2450 mnext = mbuf_next(m);
0a7de745 2451 if (mnext == NULL) {
1c79356b 2452 break;
0a7de745 2453 }
91447636 2454 m = mnext;
1c79356b 2455 }
91447636
A
2456 if (mlen > len) {
2457 mlen -= len;
2458 mbuf_setlen(m, mlen);
1c79356b 2459 if (nul > 0) {
91447636 2460 cp = (caddr_t)mbuf_data(m) + mlen - nul;
0a7de745 2461 for (i = 0; i < nul; i++) {
1c79356b 2462 *cp++ = '\0';
0a7de745 2463 }
1c79356b
A
2464 }
2465 return;
2466 }
2467 count -= len;
0a7de745 2468 if (count < 0) {
1c79356b 2469 count = 0;
0a7de745 2470 }
1c79356b
A
2471 /*
2472 * Correct length for chain is "count".
2473 * Find the mbuf with last data, adjust its length,
2474 * and toss data from remaining mbufs on chain.
2475 */
91447636
A
2476 for (m = mp; m; m = mbuf_next(m)) {
2477 mlen = mbuf_len(m);
2478 if (mlen >= count) {
2479 mlen = count;
2480 mbuf_setlen(m, count);
1c79356b 2481 if (nul > 0) {
91447636 2482 cp = (caddr_t)mbuf_data(m) + mlen - nul;
0a7de745 2483 for (i = 0; i < nul; i++) {
1c79356b 2484 *cp++ = '\0';
0a7de745 2485 }
1c79356b
A
2486 }
2487 break;
2488 }
91447636 2489 count -= mlen;
1c79356b 2490 }
0a7de745 2491 for (m = mbuf_next(m); m; m = mbuf_next(m)) {
91447636 2492 mbuf_setlen(m, 0);
0a7de745 2493 }
1c79356b
A
2494}
2495
2496/*
2d21ac55
A
2497 * Trim the header out of the mbuf list and trim off any trailing
2498 * junk so that the mbuf list has only the write data.
1c79356b 2499 */
2d21ac55
A
2500int
2501nfsm_chain_trim_data(struct nfsm_chain *nmc, int len, int *mlen)
1c79356b 2502{
2d21ac55
A
2503 int cnt = 0, dlen, adjust;
2504 caddr_t data;
2505 mbuf_t m;
1c79356b 2506
0a7de745 2507 if (mlen) {
2d21ac55 2508 *mlen = 0;
0a7de745 2509 }
1c79356b 2510
2d21ac55 2511 /* trim header */
0a7de745 2512 for (m = nmc->nmc_mhead; m && (m != nmc->nmc_mcur); m = mbuf_next(m)) {
2d21ac55 2513 mbuf_setlen(m, 0);
0a7de745
A
2514 }
2515 if (!m) {
2516 return EIO;
2517 }
2d21ac55
A
2518
2519 /* trim current mbuf */
2520 data = mbuf_data(m);
2521 dlen = mbuf_len(m);
2522 adjust = nmc->nmc_ptr - data;
2523 dlen -= adjust;
2524 if ((dlen > 0) && (adjust > 0)) {
0a7de745
A
2525 if (mbuf_setdata(m, nmc->nmc_ptr, dlen)) {
2526 return EIO;
2527 }
2528 } else {
2d21ac55 2529 mbuf_setlen(m, dlen);
0a7de745 2530 }
2d21ac55
A
2531
2532 /* skip next len bytes */
2533 for (; m && (cnt < len); m = mbuf_next(m)) {
2534 dlen = mbuf_len(m);
2535 cnt += dlen;
2536 if (cnt > len) {
2537 /* truncate to end of data */
2538 mbuf_setlen(m, dlen - (cnt - len));
0a7de745 2539 if (m == nmc->nmc_mcur) {
2d21ac55 2540 nmc->nmc_left -= (cnt - len);
0a7de745 2541 }
2d21ac55
A
2542 cnt = len;
2543 }
1c79356b 2544 }
0a7de745 2545 if (mlen) {
2d21ac55 2546 *mlen = cnt;
0a7de745 2547 }
2d21ac55
A
2548
2549 /* trim any trailing data */
0a7de745 2550 if (m == nmc->nmc_mcur) {
2d21ac55 2551 nmc->nmc_left = 0;
0a7de745
A
2552 }
2553 for (; m; m = mbuf_next(m)) {
2d21ac55 2554 mbuf_setlen(m, 0);
0a7de745 2555 }
2d21ac55 2556
0a7de745 2557 return 0;
1c79356b
A
2558}
2559
2d21ac55
A
2560int
2561nfsm_chain_add_fattr(
2562 struct nfsrv_descript *nd,
2563 struct nfsm_chain *nmc,
2564 struct vnode_attr *vap)
1c79356b 2565{
2d21ac55 2566 int error = 0;
1c79356b 2567
91447636
A
2568 // XXX Should we assert here that all fields are supported?
2569
2d21ac55
A
2570 nfsm_chain_add_32(error, nmc, vtonfs_type(vap->va_type, nd->nd_vers));
2571 if (nd->nd_vers == NFS_VER3) {
2572 nfsm_chain_add_32(error, nmc, vap->va_mode & 07777);
2573 } else {
2574 nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type, vap->va_mode));
2575 }
2576 nfsm_chain_add_32(error, nmc, vap->va_nlink);
2577 nfsm_chain_add_32(error, nmc, vap->va_uid);
2578 nfsm_chain_add_32(error, nmc, vap->va_gid);
2579 if (nd->nd_vers == NFS_VER3) {
2580 nfsm_chain_add_64(error, nmc, vap->va_data_size);
2581 nfsm_chain_add_64(error, nmc, vap->va_data_alloc);
2582 nfsm_chain_add_32(error, nmc, major(vap->va_rdev));
2583 nfsm_chain_add_32(error, nmc, minor(vap->va_rdev));
2584 nfsm_chain_add_64(error, nmc, vap->va_fsid);
2585 nfsm_chain_add_64(error, nmc, vap->va_fileid);
1c79356b 2586 } else {
2d21ac55
A
2587 nfsm_chain_add_32(error, nmc, vap->va_data_size);
2588 nfsm_chain_add_32(error, nmc, NFS_FABLKSIZE);
0a7de745 2589 if (vap->va_type == VFIFO) {
2d21ac55 2590 nfsm_chain_add_32(error, nmc, 0xffffffff);
0a7de745 2591 } else {
2d21ac55 2592 nfsm_chain_add_32(error, nmc, vap->va_rdev);
0a7de745 2593 }
2d21ac55
A
2594 nfsm_chain_add_32(error, nmc, vap->va_data_alloc / NFS_FABLKSIZE);
2595 nfsm_chain_add_32(error, nmc, vap->va_fsid);
2596 nfsm_chain_add_32(error, nmc, vap->va_fileid);
2597 }
2598 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_access_time);
2599 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_modify_time);
2600 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_change_time);
2601
0a7de745 2602 return error;
2d21ac55
A
2603}
2604
2605int
2606nfsm_chain_get_sattr(
2607 struct nfsrv_descript *nd,
2608 struct nfsm_chain *nmc,
2609 struct vnode_attr *vap)
2610{
b0d623f7 2611 int error = 0;
2d21ac55 2612 uint32_t val = 0;
39236c6e 2613 uint64_t val64 = 0;
2d21ac55
A
2614 struct timespec now;
2615
2616 if (nd->nd_vers == NFS_VER2) {
2617 /*
2618 * There is/was a bug in the Sun client that puts 0xffff in the mode
2619 * field of sattr when it should put in 0xffffffff. The u_short
2620 * doesn't sign extend. So check the low order 2 bytes for 0xffff.
2621 */
2622 nfsm_chain_get_32(error, nmc, val);
2623 if ((val & 0xffff) != 0xffff) {
2624 VATTR_SET(vap, va_mode, val & 07777);
2625 /* save the "type" bits for NFSv2 create */
2626 VATTR_SET(vap, va_type, IFTOVT(val));
2627 VATTR_CLEAR_ACTIVE(vap, va_type);
2628 }
2629 nfsm_chain_get_32(error, nmc, val);
0a7de745 2630 if (val != (uint32_t)-1) {
2d21ac55 2631 VATTR_SET(vap, va_uid, val);
0a7de745 2632 }
2d21ac55 2633 nfsm_chain_get_32(error, nmc, val);
0a7de745 2634 if (val != (uint32_t)-1) {
2d21ac55 2635 VATTR_SET(vap, va_gid, val);
0a7de745 2636 }
2d21ac55
A
2637 /* save the "size" bits for NFSv2 create (even if they appear unset) */
2638 nfsm_chain_get_32(error, nmc, val);
2639 VATTR_SET(vap, va_data_size, val);
0a7de745 2640 if (val == (uint32_t)-1) {
2d21ac55 2641 VATTR_CLEAR_ACTIVE(vap, va_data_size);
0a7de745 2642 }
2d21ac55 2643 nfsm_chain_get_time(error, nmc, NFS_VER2,
0a7de745
A
2644 vap->va_access_time.tv_sec,
2645 vap->va_access_time.tv_nsec);
2646 if (vap->va_access_time.tv_sec != -1) {
2d21ac55 2647 VATTR_SET_ACTIVE(vap, va_access_time);
0a7de745 2648 }
2d21ac55 2649 nfsm_chain_get_time(error, nmc, NFS_VER2,
0a7de745
A
2650 vap->va_modify_time.tv_sec,
2651 vap->va_modify_time.tv_nsec);
2652 if (vap->va_modify_time.tv_sec != -1) {
2d21ac55 2653 VATTR_SET_ACTIVE(vap, va_modify_time);
0a7de745
A
2654 }
2655 return error;
2d21ac55
A
2656 }
2657
2658 /* NFSv3 */
2659 nfsm_chain_get_32(error, nmc, val);
2660 if (val) {
2661 nfsm_chain_get_32(error, nmc, val);
2662 VATTR_SET(vap, va_mode, val & 07777);
2663 }
2664 nfsm_chain_get_32(error, nmc, val);
2665 if (val) {
2666 nfsm_chain_get_32(error, nmc, val);
2667 VATTR_SET(vap, va_uid, val);
2668 }
2669 nfsm_chain_get_32(error, nmc, val);
2670 if (val) {
2671 nfsm_chain_get_32(error, nmc, val);
2672 VATTR_SET(vap, va_gid, val);
2673 }
2674 nfsm_chain_get_32(error, nmc, val);
2675 if (val) {
2676 nfsm_chain_get_64(error, nmc, val64);
2677 VATTR_SET(vap, va_data_size, val64);
2678 }
2679 nanotime(&now);
2680 nfsm_chain_get_32(error, nmc, val);
2681 switch (val) {
2682 case NFS_TIME_SET_TO_CLIENT:
2683 nfsm_chain_get_time(error, nmc, nd->nd_vers,
0a7de745
A
2684 vap->va_access_time.tv_sec,
2685 vap->va_access_time.tv_nsec);
2d21ac55 2686 VATTR_SET_ACTIVE(vap, va_access_time);
b0d623f7 2687 vap->va_vaflags &= ~VA_UTIMES_NULL;
2d21ac55
A
2688 break;
2689 case NFS_TIME_SET_TO_SERVER:
2690 VATTR_SET(vap, va_access_time, now);
b0d623f7 2691 vap->va_vaflags |= VA_UTIMES_NULL;
2d21ac55
A
2692 break;
2693 }
2694 nfsm_chain_get_32(error, nmc, val);
2695 switch (val) {
2696 case NFS_TIME_SET_TO_CLIENT:
2697 nfsm_chain_get_time(error, nmc, nd->nd_vers,
0a7de745
A
2698 vap->va_modify_time.tv_sec,
2699 vap->va_modify_time.tv_nsec);
2d21ac55 2700 VATTR_SET_ACTIVE(vap, va_modify_time);
b0d623f7 2701 vap->va_vaflags &= ~VA_UTIMES_NULL;
2d21ac55
A
2702 break;
2703 case NFS_TIME_SET_TO_SERVER:
2704 VATTR_SET(vap, va_modify_time, now);
0a7de745 2705 if (!VATTR_IS_ACTIVE(vap, va_access_time)) {
b0d623f7 2706 vap->va_vaflags |= VA_UTIMES_NULL;
0a7de745 2707 }
2d21ac55 2708 break;
91447636 2709 }
2d21ac55 2710
0a7de745 2711 return error;
2d21ac55
A
2712}
2713
2714/*
2715 * Compare two security flavor structs
2716 */
b0d623f7 2717int
2d21ac55
A
2718nfsrv_cmp_secflavs(struct nfs_sec *sf1, struct nfs_sec *sf2)
2719{
2720 int i;
2721
0a7de745 2722 if (sf1->count != sf2->count) {
2d21ac55 2723 return 1;
0a7de745
A
2724 }
2725 for (i = 0; i < sf1->count; i++) {
2726 if (sf1->flavors[i] != sf2->flavors[i]) {
2d21ac55 2727 return 1;
0a7de745
A
2728 }
2729 }
2d21ac55 2730 return 0;
91447636
A
2731}
2732
2733/*
2734 * Build hash lists of net addresses and hang them off the NFS export.
2735 * Called by nfsrv_export() to set up the lists of export addresses.
2736 */
b0d623f7 2737int
91447636
A
2738nfsrv_hang_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
2739{
2740 struct nfs_export_net_args nxna;
2d21ac55 2741 struct nfs_netopt *no, *rn_no;
91447636
A
2742 struct radix_node_head *rnh;
2743 struct radix_node *rn;
2744 struct sockaddr *saddr, *smask;
2745 struct domain *dom;
2746 int i, error;
2747 unsigned int net;
2748 user_addr_t uaddr;
2749 kauth_cred_t cred;
91447636
A
2750
2751 uaddr = unxa->nxa_nets;
2752 for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
2753 error = copyin(uaddr, &nxna, sizeof(nxna));
0a7de745
A
2754 if (error) {
2755 return error;
2756 }
91447636 2757
3e170ce0
A
2758 if (nxna.nxna_addr.ss_len > sizeof(struct sockaddr_storage) ||
2759 nxna.nxna_mask.ss_len > sizeof(struct sockaddr_storage) ||
2760 nxna.nxna_addr.ss_family > AF_MAX ||
0a7de745
A
2761 nxna.nxna_mask.ss_family > AF_MAX) {
2762 return EINVAL;
2763 }
3e170ce0 2764
0a7de745 2765 if (nxna.nxna_flags & (NX_MAPROOT | NX_MAPALL)) {
6d2010ae 2766 struct posix_cred temp_pcred;
0a7de745 2767 bzero(&temp_pcred, sizeof(temp_pcred));
6d2010ae
A
2768 temp_pcred.cr_uid = nxna.nxna_cred.cr_uid;
2769 temp_pcred.cr_ngroups = nxna.nxna_cred.cr_ngroups;
0a7de745 2770 for (i = 0; i < nxna.nxna_cred.cr_ngroups && i < NGROUPS; i++) {
6d2010ae 2771 temp_pcred.cr_groups[i] = nxna.nxna_cred.cr_groups[i];
0a7de745 2772 }
6d2010ae 2773 cred = posix_cred_create(&temp_pcred);
0a7de745
A
2774 if (!IS_VALID_CRED(cred)) {
2775 return ENOMEM;
2776 }
91447636 2777 } else {
0c530ab8 2778 cred = NOCRED;
91447636
A
2779 }
2780
2781 if (nxna.nxna_addr.ss_len == 0) {
2782 /* No address means this is a default/world export */
2d21ac55 2783 if (nx->nx_flags & NX_DEFAULTEXPORT) {
0a7de745
A
2784 if (IS_VALID_CRED(cred)) {
2785 kauth_cred_unref(&cred);
2786 }
2787 return EEXIST;
2d21ac55 2788 }
91447636
A
2789 nx->nx_flags |= NX_DEFAULTEXPORT;
2790 nx->nx_defopt.nxo_flags = nxna.nxna_flags;
2791 nx->nx_defopt.nxo_cred = cred;
2d21ac55 2792 bcopy(&nxna.nxna_sec, &nx->nx_defopt.nxo_sec, sizeof(struct nfs_sec));
91447636
A
2793 nx->nx_expcnt++;
2794 continue;
2795 }
2796
2797 i = sizeof(struct nfs_netopt);
2798 i += nxna.nxna_addr.ss_len + nxna.nxna_mask.ss_len;
2799 MALLOC(no, struct nfs_netopt *, i, M_NETADDR, M_WAITOK);
2d21ac55 2800 if (!no) {
0a7de745 2801 if (IS_VALID_CRED(cred)) {
2d21ac55 2802 kauth_cred_unref(&cred);
0a7de745
A
2803 }
2804 return ENOMEM;
2d21ac55 2805 }
91447636
A
2806 bzero(no, sizeof(struct nfs_netopt));
2807 no->no_opt.nxo_flags = nxna.nxna_flags;
2808 no->no_opt.nxo_cred = cred;
2d21ac55 2809 bcopy(&nxna.nxna_sec, &no->no_opt.nxo_sec, sizeof(struct nfs_sec));
91447636
A
2810
2811 saddr = (struct sockaddr *)(no + 1);
2812 bcopy(&nxna.nxna_addr, saddr, nxna.nxna_addr.ss_len);
2813 if (nxna.nxna_mask.ss_len) {
2814 smask = (struct sockaddr *)((caddr_t)saddr + nxna.nxna_addr.ss_len);
2815 bcopy(&nxna.nxna_mask, smask, nxna.nxna_mask.ss_len);
2816 } else {
2817 smask = NULL;
2818 }
2819 i = saddr->sa_family;
2820 if ((rnh = nx->nx_rtable[i]) == 0) {
2821 /*
2822 * Seems silly to initialize every AF when most are not
2823 * used, do so on demand here
2824 */
39236c6e 2825 TAILQ_FOREACH(dom, &domains, dom_entry) {
91447636
A
2826 if (dom->dom_family == i && dom->dom_rtattach) {
2827 dom->dom_rtattach((void **)&nx->nx_rtable[i],
0a7de745 2828 dom->dom_rtoffset);
91447636
A
2829 break;
2830 }
39236c6e 2831 }
91447636 2832 if ((rnh = nx->nx_rtable[i]) == 0) {
0a7de745
A
2833 if (IS_VALID_CRED(cred)) {
2834 kauth_cred_unref(&cred);
2835 }
91447636 2836 _FREE(no, M_NETADDR);
0a7de745 2837 return ENOBUFS;
91447636
A
2838 }
2839 }
2840 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, no->no_rnodes);
2841 if (rn == 0) {
2842 /*
2843 * One of the reasons that rnh_addaddr may fail is that
2844 * the entry already exists. To check for this case, we
2845 * look up the entry to see if it is there. If so, we
2846 * do not need to make a new entry but do continue.
2d21ac55
A
2847 *
2848 * XXX should this be rnh_lookup() instead?
91447636
A
2849 */
2850 int matched = 0;
2851 rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
2d21ac55 2852 rn_no = (struct nfs_netopt *)rn;
91447636 2853 if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 &&
2d21ac55
A
2854 (rn_no->no_opt.nxo_flags == nxna.nxna_flags) &&
2855 (!nfsrv_cmp_secflavs(&rn_no->no_opt.nxo_sec, &nxna.nxna_sec))) {
2856 kauth_cred_t cred2 = rn_no->no_opt.nxo_cred;
2857 if (cred == cred2) {
2858 /* creds are same (or both NULL) */
2859 matched = 1;
6d2010ae 2860 } else if (cred && cred2 && (kauth_cred_getuid(cred) == kauth_cred_getuid(cred2))) {
0a7de745
A
2861 /*
2862 * Now compare the effective and
2863 * supplementary groups...
2864 *
2865 * Note: This comparison, as written,
2866 * does not correctly indicate that
2867 * the groups are equivalent, since
2868 * other than the first supplementary
2869 * group, which is also the effective
2870 * group, order on the remaining groups
2871 * doesn't matter, and this is an
2872 * ordered compare.
2873 */
2874 gid_t groups[NGROUPS];
2875 gid_t groups2[NGROUPS];
2876 int groupcount = NGROUPS;
2877 int group2count = NGROUPS;
2878
2879 if (!kauth_cred_getgroups(cred, groups, &groupcount) &&
2880 !kauth_cred_getgroups(cred2, groups2, &group2count) &&
2881 groupcount == group2count) {
2882 for (i = 0; i < group2count; i++) {
2883 if (groups[i] != groups2[i]) {
2884 break;
2885 }
2886 }
2887 if (i >= group2count || i >= NGROUPS) {
2888 matched = 1;
2889 }
2890 }
91447636
A
2891 }
2892 }
0a7de745
A
2893 if (IS_VALID_CRED(cred)) {
2894 kauth_cred_unref(&cred);
2895 }
91447636 2896 _FREE(no, M_NETADDR);
0a7de745 2897 if (matched) {
91447636 2898 continue;
0a7de745
A
2899 }
2900 return EPERM;
91447636
A
2901 }
2902 nx->nx_expcnt++;
2903 }
2904
0a7de745 2905 return 0;
91447636
A
2906}
2907
2908/*
2d21ac55 2909 * In order to properly track an export's netopt count, we need to pass
91447636
A
2910 * an additional argument to nfsrv_free_netopt() so that it can decrement
2911 * the export's netopt count.
2912 */
2913struct nfsrv_free_netopt_arg {
2914 uint32_t *cnt;
2915 struct radix_node_head *rnh;
2916};
2917
b0d623f7 2918int
91447636
A
2919nfsrv_free_netopt(struct radix_node *rn, void *w)
2920{
2921 struct nfsrv_free_netopt_arg *fna = (struct nfsrv_free_netopt_arg *)w;
2922 struct radix_node_head *rnh = fna->rnh;
2923 uint32_t *cnt = fna->cnt;
2924 struct nfs_netopt *nno = (struct nfs_netopt *)rn;
2925
2926 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
0a7de745 2927 if (IS_VALID_CRED(nno->no_opt.nxo_cred)) {
0c530ab8 2928 kauth_cred_unref(&nno->no_opt.nxo_cred);
0a7de745 2929 }
91447636
A
2930 _FREE((caddr_t)rn, M_NETADDR);
2931 *cnt -= 1;
0a7de745 2932 return 0;
91447636
A
2933}
2934
2935/*
2936 * Free the net address hash lists that are hanging off the mount points.
2937 */
b0d623f7 2938int
2d21ac55 2939nfsrv_free_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
91447636 2940{
2d21ac55 2941 struct nfs_export_net_args nxna;
91447636 2942 struct radix_node_head *rnh;
2d21ac55 2943 struct radix_node *rn;
91447636 2944 struct nfsrv_free_netopt_arg fna;
2d21ac55
A
2945 struct nfs_netopt *nno;
2946 user_addr_t uaddr;
2947 unsigned int net;
2948 int i, error;
2949
2950 if (!unxa || !unxa->nxa_netcount) {
2951 /* delete everything */
0a7de745
A
2952 for (i = 0; i <= AF_MAX; i++) {
2953 if ((rnh = nx->nx_rtable[i])) {
2d21ac55
A
2954 fna.rnh = rnh;
2955 fna.cnt = &nx->nx_expcnt;
2956 (*rnh->rnh_walktree)(rnh, nfsrv_free_netopt, (caddr_t)&fna);
2957 _FREE((caddr_t)rnh, M_RTABLE);
2958 nx->nx_rtable[i] = 0;
2959 }
0a7de745
A
2960 }
2961 return 0;
2d21ac55
A
2962 }
2963
2964 /* delete only the exports specified */
2965 uaddr = unxa->nxa_nets;
2966 for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
2967 error = copyin(uaddr, &nxna, sizeof(nxna));
0a7de745
A
2968 if (error) {
2969 return error;
2970 }
2d21ac55
A
2971
2972 if (nxna.nxna_addr.ss_len == 0) {
2973 /* No address means this is a default/world export */
2974 if (nx->nx_flags & NX_DEFAULTEXPORT) {
2975 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2976 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
2977 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
2978 }
2979 nx->nx_expcnt--;
2980 }
2981 continue;
2982 }
2983
2984 if ((rnh = nx->nx_rtable[nxna.nxna_addr.ss_family]) == 0) {
2985 /* AF not initialized? */
0a7de745 2986 if (!(unxa->nxa_flags & NXA_ADD)) {
2d21ac55 2987 printf("nfsrv_free_addrlist: address not found (0)\n");
0a7de745 2988 }
2d21ac55
A
2989 continue;
2990 }
91447636 2991
2d21ac55 2992 rn = (*rnh->rnh_lookup)(&nxna.nxna_addr,
0a7de745 2993 nxna.nxna_mask.ss_len ? &nxna.nxna_mask : NULL, rnh);
2d21ac55 2994 if (!rn || (rn->rn_flags & RNF_ROOT)) {
0a7de745 2995 if (!(unxa->nxa_flags & NXA_ADD)) {
2d21ac55 2996 printf("nfsrv_free_addrlist: address not found (1)\n");
0a7de745 2997 }
2d21ac55
A
2998 continue;
2999 }
3000
3001 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
3002 nno = (struct nfs_netopt *)rn;
0a7de745 3003 if (IS_VALID_CRED(nno->no_opt.nxo_cred)) {
2d21ac55 3004 kauth_cred_unref(&nno->no_opt.nxo_cred);
0a7de745 3005 }
2d21ac55
A
3006 _FREE((caddr_t)rn, M_NETADDR);
3007
3008 nx->nx_expcnt--;
3009 if (nx->nx_expcnt == ((nx->nx_flags & NX_DEFAULTEXPORT) ? 1 : 0)) {
3010 /* no more entries in rnh, so free it up */
91447636 3011 _FREE((caddr_t)rnh, M_RTABLE);
2d21ac55 3012 nx->nx_rtable[nxna.nxna_addr.ss_family] = 0;
91447636 3013 }
2d21ac55
A
3014 }
3015
0a7de745 3016 return 0;
91447636
A
3017}
3018
3019void enablequotas(struct mount *mp, vfs_context_t ctx); // XXX
3020
3021int
2d21ac55 3022nfsrv_export(struct user_nfs_export_args *unxa, vfs_context_t ctx)
91447636 3023{
6d2010ae
A
3024 int error = 0;
3025 size_t pathlen;
91447636
A
3026 struct nfs_exportfs *nxfs, *nxfs2, *nxfs3;
3027 struct nfs_export *nx, *nx2, *nx3;
3028 struct nfs_filehandle nfh;
3029 struct nameidata mnd, xnd;
3030 vnode_t mvp = NULL, xvp = NULL;
2d21ac55 3031 mount_t mp = NULL;
91447636 3032 char path[MAXPATHLEN];
ea3f0419
A
3033 char fl_pathbuff[MAXPATHLEN];
3034 int fl_pathbuff_len = MAXPATHLEN;
91447636
A
3035 int expisroot;
3036
b0d623f7
A
3037 if (unxa->nxa_flags == NXA_CHECK) {
3038 /* just check if the path is an NFS-exportable file system */
6d2010ae 3039 error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, &pathlen);
0a7de745
A
3040 if (error) {
3041 return error;
3042 }
6d2010ae 3043 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
0a7de745 3044 UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
b0d623f7 3045 error = namei(&mnd);
0a7de745
A
3046 if (error) {
3047 return error;
3048 }
b0d623f7
A
3049 mvp = mnd.ni_vp;
3050 mp = vnode_mount(mvp);
3051 /* make sure it's the root of a file system */
0a7de745 3052 if (!vnode_isvroot(mvp)) {
b0d623f7 3053 error = EINVAL;
0a7de745 3054 }
b0d623f7
A
3055 /* make sure the file system is NFS-exportable */
3056 if (!error) {
3057 nfh.nfh_len = NFSV3_MAX_FID_SIZE;
3058 error = VFS_VPTOFH(mvp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
3059 }
0a7de745 3060 if (!error && (nfh.nfh_len > (int)NFSV3_MAX_FID_SIZE)) {
b0d623f7 3061 error = EIO;
0a7de745
A
3062 }
3063 if (!error && !(mp->mnt_vtable->vfc_vfsflags & VFC_VFSREADDIR_EXTENDED)) {
b0d623f7 3064 error = EISDIR;
0a7de745 3065 }
b0d623f7
A
3066 vnode_put(mvp);
3067 nameidone(&mnd);
0a7de745 3068 return error;
b0d623f7
A
3069 }
3070
3071 /* all other operations: must be super user */
0a7de745
A
3072 if ((error = vfs_context_suser(ctx))) {
3073 return error;
3074 }
b0d623f7 3075
3a60a9f5
A
3076 if (unxa->nxa_flags & NXA_DELETE_ALL) {
3077 /* delete all exports on all file systems */
2d21ac55
A
3078 lck_rw_lock_exclusive(&nfsrv_export_rwlock);
3079 while ((nxfs = LIST_FIRST(&nfsrv_exports))) {
3a60a9f5 3080 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
6d2010ae 3081 if (mp) {
2d21ac55 3082 vfs_clearflags(mp, MNT_EXPORTED);
6d2010ae
A
3083 mount_iterdrop(mp);
3084 mp = NULL;
3085 }
3a60a9f5
A
3086 /* delete all exports on this file system */
3087 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
3088 LIST_REMOVE(nx, nx_next);
3089 LIST_REMOVE(nx, nx_hash);
3090 /* delete all netopts for this export */
2d21ac55 3091 nfsrv_free_addrlist(nx, NULL);
3a60a9f5 3092 nx->nx_flags &= ~NX_DEFAULTEXPORT;
0c530ab8
A
3093 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
3094 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
3a60a9f5 3095 }
2d21ac55
A
3096 /* free active user list for this export */
3097 nfsrv_free_user_list(&nx->nx_user_list);
3a60a9f5
A
3098 FREE(nx->nx_path, M_TEMP);
3099 FREE(nx, M_TEMP);
3100 }
3101 LIST_REMOVE(nxfs, nxfs_next);
3102 FREE(nxfs->nxfs_path, M_TEMP);
3103 FREE(nxfs, M_TEMP);
3104 }
b0d623f7
A
3105 if (nfsrv_export_hashtbl) {
3106 /* all exports deleted, clean up export hash table */
3107 FREE(nfsrv_export_hashtbl, M_TEMP);
3108 nfsrv_export_hashtbl = NULL;
3109 }
2d21ac55 3110 lck_rw_done(&nfsrv_export_rwlock);
0a7de745 3111 return 0;
3a60a9f5
A
3112 }
3113
6d2010ae 3114 error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, &pathlen);
0a7de745
A
3115 if (error) {
3116 return error;
3117 }
91447636 3118
2d21ac55 3119 lck_rw_lock_exclusive(&nfsrv_export_rwlock);
91447636 3120
b0d623f7
A
3121 /* init export hash table if not already */
3122 if (!nfsrv_export_hashtbl) {
0a7de745 3123 if (nfsrv_export_hash_size <= 0) {
b0d623f7 3124 nfsrv_export_hash_size = NFSRVEXPHASHSZ;
0a7de745 3125 }
b0d623f7
A
3126 nfsrv_export_hashtbl = hashinit(nfsrv_export_hash_size, M_TEMP, &nfsrv_export_hash);
3127 }
3128
91447636 3129 // first check if we've already got an exportfs with the given ID
2d21ac55 3130 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
0a7de745 3131 if (nxfs->nxfs_id == unxa->nxa_fsid) {
91447636 3132 break;
0a7de745 3133 }
91447636
A
3134 }
3135 if (nxfs) {
3136 /* verify exported FS path matches given path */
2d21ac55 3137 if (strncmp(path, nxfs->nxfs_path, MAXPATHLEN)) {
91447636
A
3138 error = EEXIST;
3139 goto unlock_out;
3140 }
0a7de745 3141 if ((unxa->nxa_flags & (NXA_ADD | NXA_OFFLINE)) == NXA_ADD) {
2d21ac55 3142 /* find exported FS root vnode */
6d2010ae 3143 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
0a7de745 3144 UIO_SYSSPACE, CAST_USER_ADDR_T(nxfs->nxfs_path), ctx);
2d21ac55 3145 error = namei(&mnd);
0a7de745 3146 if (error) {
2d21ac55 3147 goto unlock_out;
0a7de745 3148 }
2d21ac55
A
3149 mvp = mnd.ni_vp;
3150 /* make sure it's (still) the root of a file system */
3151 if (!vnode_isvroot(mvp)) {
3152 error = EINVAL;
3153 goto out;
3154 }
ea3f0419
A
3155 /* if adding, verify that the mount is still what we expect */
3156 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
3157 if (!mp) {
3158 /* check for firmlink-free path */
3159 if (vn_getpath_no_firmlink(mvp, fl_pathbuff, &fl_pathbuff_len) == 0 &&
3160 fl_pathbuff_len > 0 &&
3161 !strncmp(nxfs->nxfs_path, fl_pathbuff, MAXPATHLEN)) {
3162 mp = vfs_getvfs_by_mntonname(vnode_mount(mvp)->mnt_vfsstat.f_mntonname);
3163 }
3164 }
3165 if (mp) {
3166 mount_ref(mp, 0);
3167 mount_iterdrop(mp);
3168 }
2d21ac55
A
3169 /* sanity check: this should be same mount */
3170 if (mp != vnode_mount(mvp)) {
3171 error = EINVAL;
3172 goto out;
3173 }
91447636
A
3174 }
3175 } else {
3176 /* no current exported file system with that ID */
3177 if (!(unxa->nxa_flags & NXA_ADD)) {
3178 error = ENOENT;
3179 goto unlock_out;
3180 }
3181
3182 /* find exported FS root vnode */
6d2010ae 3183 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
0a7de745 3184 UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
91447636 3185 error = namei(&mnd);
2d21ac55 3186 if (error) {
0a7de745 3187 if (!(unxa->nxa_flags & NXA_OFFLINE)) {
2d21ac55 3188 goto unlock_out;
0a7de745 3189 }
2d21ac55
A
3190 } else {
3191 mvp = mnd.ni_vp;
3192 /* make sure it's the root of a file system */
3193 if (!vnode_isvroot(mvp)) {
3194 /* bail if not marked offline */
3195 if (!(unxa->nxa_flags & NXA_OFFLINE)) {
3196 error = EINVAL;
3197 goto out;
3198 }
3199 vnode_put(mvp);
3200 nameidone(&mnd);
3201 mvp = NULL;
3202 } else {
3203 mp = vnode_mount(mvp);
6d2010ae 3204 mount_ref(mp, 0);
91447636 3205
2d21ac55
A
3206 /* make sure the file system is NFS-exportable */
3207 nfh.nfh_len = NFSV3_MAX_FID_SIZE;
3208 error = VFS_VPTOFH(mvp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
0a7de745 3209 if (!error && (nfh.nfh_len > (int)NFSV3_MAX_FID_SIZE)) {
2d21ac55 3210 error = EIO;
0a7de745
A
3211 }
3212 if (!error && !(mp->mnt_vtable->vfc_vfsflags & VFC_VFSREADDIR_EXTENDED)) {
b0d623f7 3213 error = EISDIR;
0a7de745
A
3214 }
3215 if (error) {
2d21ac55 3216 goto out;
0a7de745 3217 }
2d21ac55
A
3218 }
3219 }
91447636
A
3220
3221 /* add an exportfs for it */
3222 MALLOC(nxfs, struct nfs_exportfs *, sizeof(struct nfs_exportfs), M_TEMP, M_WAITOK);
3223 if (!nxfs) {
3224 error = ENOMEM;
3225 goto out;
3226 }
3227 bzero(nxfs, sizeof(struct nfs_exportfs));
3228 nxfs->nxfs_id = unxa->nxa_fsid;
3229 MALLOC(nxfs->nxfs_path, char*, pathlen, M_TEMP, M_WAITOK);
3230 if (!nxfs->nxfs_path) {
3231 FREE(nxfs, M_TEMP);
3232 error = ENOMEM;
3233 goto out;
3234 }
3235 bcopy(path, nxfs->nxfs_path, pathlen);
3236 /* insert into list in reverse-sorted order */
3237 nxfs3 = NULL;
2d21ac55 3238 LIST_FOREACH(nxfs2, &nfsrv_exports, nxfs_next) {
0a7de745 3239 if (strncmp(nxfs->nxfs_path, nxfs2->nxfs_path, MAXPATHLEN) > 0) {
91447636 3240 break;
0a7de745 3241 }
91447636
A
3242 nxfs3 = nxfs2;
3243 }
0a7de745 3244 if (nxfs2) {
91447636 3245 LIST_INSERT_BEFORE(nxfs2, nxfs, nxfs_next);
0a7de745 3246 } else if (nxfs3) {
91447636 3247 LIST_INSERT_AFTER(nxfs3, nxfs, nxfs_next);
0a7de745 3248 } else {
2d21ac55 3249 LIST_INSERT_HEAD(&nfsrv_exports, nxfs, nxfs_next);
0a7de745 3250 }
91447636
A
3251
3252 /* make sure any quotas are enabled before we export the file system */
0a7de745 3253 if (mp) {
2d21ac55 3254 enablequotas(mp, ctx);
0a7de745 3255 }
1c79356b 3256 }
91447636
A
3257
3258 if (unxa->nxa_exppath) {
6d2010ae 3259 error = copyinstr(unxa->nxa_exppath, path, MAXPATHLEN, &pathlen);
0a7de745 3260 if (error) {
91447636 3261 goto out;
0a7de745 3262 }
91447636 3263 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
0a7de745 3264 if (nx->nx_id == unxa->nxa_expid) {
91447636 3265 break;
0a7de745 3266 }
91447636
A
3267 }
3268 if (nx) {
3269 /* verify exported FS path matches given path */
2d21ac55 3270 if (strncmp(path, nx->nx_path, MAXPATHLEN)) {
91447636
A
3271 error = EEXIST;
3272 goto out;
3273 }
3274 } else {
3275 /* no current export with that ID */
3276 if (!(unxa->nxa_flags & NXA_ADD)) {
3277 error = ENOENT;
3278 goto out;
3279 }
3280 /* add an export for it */
3281 MALLOC(nx, struct nfs_export *, sizeof(struct nfs_export), M_TEMP, M_WAITOK);
3282 if (!nx) {
3283 error = ENOMEM;
3284 goto out1;
3285 }
3286 bzero(nx, sizeof(struct nfs_export));
3287 nx->nx_id = unxa->nxa_expid;
3288 nx->nx_fs = nxfs;
2d21ac55 3289 microtime(&nx->nx_exptime);
91447636
A
3290 MALLOC(nx->nx_path, char*, pathlen, M_TEMP, M_WAITOK);
3291 if (!nx->nx_path) {
3292 error = ENOMEM;
3293 FREE(nx, M_TEMP);
3294 nx = NULL;
3295 goto out1;
3296 }
3297 bcopy(path, nx->nx_path, pathlen);
2d21ac55
A
3298 /* initialize the active user list */
3299 nfsrv_init_user_list(&nx->nx_user_list);
91447636
A
3300 /* insert into list in reverse-sorted order */
3301 nx3 = NULL;
3302 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
0a7de745 3303 if (strncmp(nx->nx_path, nx2->nx_path, MAXPATHLEN) > 0) {
91447636 3304 break;
0a7de745 3305 }
91447636
A
3306 nx3 = nx2;
3307 }
0a7de745 3308 if (nx2) {
91447636 3309 LIST_INSERT_BEFORE(nx2, nx, nx_next);
0a7de745 3310 } else if (nx3) {
91447636 3311 LIST_INSERT_AFTER(nx3, nx, nx_next);
0a7de745 3312 } else {
91447636 3313 LIST_INSERT_HEAD(&nxfs->nxfs_exports, nx, nx_next);
0a7de745 3314 }
91447636 3315 /* insert into hash */
2d21ac55 3316 LIST_INSERT_HEAD(NFSRVEXPHASH(nxfs->nxfs_id, nx->nx_id), nx, nx_hash);
91447636
A
3317
3318 /*
2d21ac55 3319 * We don't allow/support nested exports. Check if the new entry
91447636
A
3320 * nests with the entries before and after or if there's an
3321 * entry for the file system root and subdirs.
3322 */
3323 error = 0;
3324 if ((nx3 && !strncmp(nx3->nx_path, nx->nx_path, pathlen - 1) &&
0a7de745 3325 (nx3->nx_path[pathlen - 1] == '/')) ||
91447636 3326 (nx2 && !strncmp(nx2->nx_path, nx->nx_path, strlen(nx2->nx_path)) &&
0a7de745 3327 (nx->nx_path[strlen(nx2->nx_path)] == '/'))) {
91447636 3328 error = EINVAL;
0a7de745 3329 }
91447636
A
3330 if (!error) {
3331 /* check export conflict with fs root export and vice versa */
3332 expisroot = !nx->nx_path[0] ||
0a7de745 3333 ((nx->nx_path[0] == '.') && !nx->nx_path[1]);
91447636
A
3334 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
3335 if (expisroot) {
0a7de745 3336 if (nx2 != nx) {
91447636 3337 break;
0a7de745
A
3338 }
3339 } else if (!nx2->nx_path[0]) {
91447636 3340 break;
0a7de745 3341 } else if ((nx2->nx_path[0] == '.') && !nx2->nx_path[1]) {
91447636 3342 break;
0a7de745 3343 }
91447636 3344 }
0a7de745 3345 if (nx2) {
91447636 3346 error = EINVAL;
0a7de745 3347 }
91447636
A
3348 }
3349 if (error) {
2d21ac55
A
3350 /*
3351 * Don't actually return an error because mountd is
3352 * probably about to delete the conflicting export.
3353 * This can happen when a new export momentarily conflicts
3354 * with an old export while the transition is being made.
3355 * Theoretically, mountd could be written to avoid this
3356 * transient situation - but it would greatly increase the
3357 * complexity of mountd for very little overall benefit.
3358 */
3359 printf("nfsrv_export: warning: nested exports: %s/%s\n",
0a7de745 3360 nxfs->nxfs_path, nx->nx_path);
2d21ac55 3361 error = 0;
91447636 3362 }
2d21ac55
A
3363 nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
3364 }
3365 /* make sure file handle is set up */
3366 if ((nx->nx_fh.nfh_xh.nxh_version != htonl(NFS_FH_VERSION)) ||
3367 (nx->nx_fh.nfh_xh.nxh_flags & NXHF_INVALIDFH)) {
3368 /* try to set up export root file handle */
0c530ab8
A
3369 nx->nx_fh.nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
3370 nx->nx_fh.nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
3371 nx->nx_fh.nfh_xh.nxh_expid = htonl(nx->nx_id);
91447636
A
3372 nx->nx_fh.nfh_xh.nxh_flags = 0;
3373 nx->nx_fh.nfh_xh.nxh_reserved = 0;
2d21ac55
A
3374 nx->nx_fh.nfh_fhp = (u_char*)&nx->nx_fh.nfh_xh;
3375 bzero(&nx->nx_fh.nfh_fid[0], NFSV2_MAX_FID_SIZE);
3376 if (mvp) {
3377 /* find export root vnode */
3378 if (!nx->nx_path[0] || ((nx->nx_path[0] == '.') && !nx->nx_path[1])) {
3379 /* exporting file system's root directory */
3380 xvp = mvp;
3381 vnode_get(xvp);
3382 } else {
3383 xnd.ni_cnd.cn_nameiop = LOOKUP;
6d2010ae
A
3384#if CONFIG_TRIGGERS
3385 xnd.ni_op = OP_LOOKUP;
3386#endif
2d21ac55
A
3387 xnd.ni_cnd.cn_flags = LOCKLEAF;
3388 xnd.ni_pathlen = pathlen - 1;
3389 xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf = path;
3390 xnd.ni_startdir = mvp;
3391 xnd.ni_usedvp = mvp;
813fb2f6 3392 xnd.ni_rootdir = rootvnode;
2d21ac55 3393 xnd.ni_cnd.cn_context = ctx;
b0d623f7
A
3394 while ((error = lookup(&xnd)) == ERECYCLE) {
3395 xnd.ni_cnd.cn_flags = LOCKLEAF;
3396 xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf;
3397 xnd.ni_usedvp = xnd.ni_dvp = xnd.ni_startdir = mvp;
3398 }
0a7de745 3399 if (error) {
2d21ac55 3400 goto out1;
0a7de745 3401 }
2d21ac55
A
3402 xvp = xnd.ni_vp;
3403 }
3404
3405 if (vnode_vtype(xvp) != VDIR) {
3406 error = EINVAL;
3407 vnode_put(xvp);
3408 goto out1;
3409 }
3410
3411 /* grab file handle */
3412 nx->nx_fh.nfh_len = NFSV3_MAX_FID_SIZE;
3413 error = VFS_VPTOFH(xvp, (int*)&nx->nx_fh.nfh_len, &nx->nx_fh.nfh_fid[0], NULL);
3414 if (!error && (nx->nx_fh.nfh_len > (int)NFSV3_MAX_FID_SIZE)) {
3415 error = EIO;
3416 } else {
3417 nx->nx_fh.nfh_xh.nxh_fidlen = nx->nx_fh.nfh_len;
3418 nx->nx_fh.nfh_len += sizeof(nx->nx_fh.nfh_xh);
3419 }
3420
3421 vnode_put(xvp);
0a7de745 3422 if (error) {
2d21ac55 3423 goto out1;
0a7de745 3424 }
91447636 3425 } else {
2d21ac55
A
3426 nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
3427 nx->nx_fh.nfh_xh.nxh_fidlen = 0;
3428 nx->nx_fh.nfh_len = sizeof(nx->nx_fh.nfh_xh);
91447636 3429 }
91447636
A
3430 }
3431 } else {
3432 nx = NULL;
3433 }
3434
3435 /* perform the export changes */
3436 if (unxa->nxa_flags & NXA_DELETE) {
3437 if (!nx) {
3438 /* delete all exports on this file system */
3439 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
3440 LIST_REMOVE(nx, nx_next);
3441 LIST_REMOVE(nx, nx_hash);
3442 /* delete all netopts for this export */
2d21ac55 3443 nfsrv_free_addrlist(nx, NULL);
91447636 3444 nx->nx_flags &= ~NX_DEFAULTEXPORT;
0c530ab8
A
3445 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
3446 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
91447636 3447 }
2d21ac55
A
3448 /* delete active user list for this export */
3449 nfsrv_free_user_list(&nx->nx_user_list);
91447636
A
3450 FREE(nx->nx_path, M_TEMP);
3451 FREE(nx, M_TEMP);
3452 }
3453 goto out1;
2d21ac55 3454 } else if (!unxa->nxa_netcount) {
91447636 3455 /* delete all netopts for this export */
2d21ac55 3456 nfsrv_free_addrlist(nx, NULL);
91447636 3457 nx->nx_flags &= ~NX_DEFAULTEXPORT;
0c530ab8
A
3458 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
3459 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
91447636 3460 }
2d21ac55
A
3461 } else {
3462 /* delete only the netopts for the given addresses */
3463 error = nfsrv_free_addrlist(nx, unxa);
0a7de745 3464 if (error) {
2d21ac55 3465 goto out1;
0a7de745 3466 }
91447636
A
3467 }
3468 }
3469 if (unxa->nxa_flags & NXA_ADD) {
0a7de745 3470 /*
2d21ac55
A
3471 * If going offline set the export time so that when
3472 * coming back on line we will present a new write verifier
3473 * to the client.
3474 */
0a7de745 3475 if (unxa->nxa_flags & NXA_OFFLINE) {
2d21ac55 3476 microtime(&nx->nx_exptime);
0a7de745 3477 }
2d21ac55 3478
91447636 3479 error = nfsrv_hang_addrlist(nx, unxa);
0a7de745 3480 if (!error && mp) {
2d21ac55 3481 vfs_setflags(mp, MNT_EXPORTED);
0a7de745 3482 }
91447636
A
3483 }
3484
3485out1:
3486 if (nx && !nx->nx_expcnt) {
3487 /* export has no export options */
3488 LIST_REMOVE(nx, nx_next);
3489 LIST_REMOVE(nx, nx_hash);
2d21ac55
A
3490 /* delete active user list for this export */
3491 nfsrv_free_user_list(&nx->nx_user_list);
91447636
A
3492 FREE(nx->nx_path, M_TEMP);
3493 FREE(nx, M_TEMP);
3494 }
3495 if (LIST_EMPTY(&nxfs->nxfs_exports)) {
3496 /* exported file system has no more exports */
3497 LIST_REMOVE(nxfs, nxfs_next);
3498 FREE(nxfs->nxfs_path, M_TEMP);
3499 FREE(nxfs, M_TEMP);
0a7de745 3500 if (mp) {
2d21ac55 3501 vfs_clearflags(mp, MNT_EXPORTED);
0a7de745 3502 }
91447636
A
3503 }
3504
3505out:
3506 if (mvp) {
3507 vnode_put(mvp);
3508 nameidone(&mnd);
3509 }
3510unlock_out:
0a7de745 3511 if (mp) {
6d2010ae 3512 mount_drop(mp, 0);
0a7de745 3513 }
2d21ac55 3514 lck_rw_done(&nfsrv_export_rwlock);
0a7de745 3515 return error;
91447636
A
3516}
3517
3e170ce0
A
3518/*
3519 * Check if there is a least one export that will allow this address.
3520 *
3521 * Return 0, if there is an export that will allow this address,
3522 * else return EACCES
3523 */
3524int
3525nfsrv_check_exports_allow_address(mbuf_t nam)
3526{
0a7de745
A
3527 struct nfs_exportfs *nxfs;
3528 struct nfs_export *nx;
3529 struct nfs_export_options *nxo = NULL;
3e170ce0 3530
0a7de745
A
3531 if (nam == NULL) {
3532 return EACCES;
3533 }
3e170ce0
A
3534
3535 lck_rw_lock_shared(&nfsrv_export_rwlock);
3536 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
3537 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
3538 /* A little optimizing by checking for the default first */
0a7de745 3539 if (nx->nx_flags & NX_DEFAULTEXPORT) {
3e170ce0 3540 nxo = &nx->nx_defopt;
0a7de745
A
3541 }
3542 if (nxo || (nxo = nfsrv_export_lookup(nx, nam))) {
3e170ce0 3543 goto found;
0a7de745 3544 }
3e170ce0
A
3545 }
3546 }
3547found:
3548 lck_rw_done(&nfsrv_export_rwlock);
3549
0a7de745 3550 return nxo ? 0 : EACCES;
3e170ce0
A
3551}
3552
b0d623f7 3553struct nfs_export_options *
91447636
A
3554nfsrv_export_lookup(struct nfs_export *nx, mbuf_t nam)
3555{
3556 struct nfs_export_options *nxo = NULL;
3557 struct nfs_netopt *no = NULL;
3558 struct radix_node_head *rnh;
3559 struct sockaddr *saddr;
3560
3561 /* Lookup in the export list first. */
3562 if (nam != NULL) {
3563 saddr = mbuf_data(nam);
527f9951
A
3564 if (saddr->sa_family > AF_MAX) {
3565 /* Bogus sockaddr? Don't match anything. */
0a7de745 3566 return NULL;
527f9951 3567 }
91447636
A
3568 rnh = nx->nx_rtable[saddr->sa_family];
3569 if (rnh != NULL) {
3570 no = (struct nfs_netopt *)
0a7de745
A
3571 (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
3572 if (no && no->no_rnodes->rn_flags & RNF_ROOT) {
91447636 3573 no = NULL;
0a7de745
A
3574 }
3575 if (no) {
91447636 3576 nxo = &no->no_opt;
0a7de745 3577 }
91447636
A
3578 }
3579 }
3580 /* If no address match, use the default if it exists. */
0a7de745 3581 if ((nxo == NULL) && (nx->nx_flags & NX_DEFAULTEXPORT)) {
91447636 3582 nxo = &nx->nx_defopt;
0a7de745
A
3583 }
3584 return nxo;
91447636
A
3585}
3586
3587/* find an export for the given handle */
b0d623f7 3588struct nfs_export *
91447636
A
3589nfsrv_fhtoexport(struct nfs_filehandle *nfhp)
3590{
2d21ac55 3591 struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
91447636 3592 struct nfs_export *nx;
0c530ab8
A
3593 uint32_t fsid, expid;
3594
0a7de745
A
3595 if (!nfsrv_export_hashtbl) {
3596 return NULL;
3597 }
2d21ac55
A
3598 fsid = ntohl(nxh->nxh_fsid);
3599 expid = ntohl(nxh->nxh_expid);
3600 nx = NFSRVEXPHASH(fsid, expid)->lh_first;
91447636 3601 for (; nx; nx = LIST_NEXT(nx, nx_hash)) {
0a7de745 3602 if (nx->nx_fs->nxfs_id != fsid) {
91447636 3603 continue;
0a7de745
A
3604 }
3605 if (nx->nx_id != expid) {
91447636 3606 continue;
0a7de745 3607 }
91447636
A
3608 break;
3609 }
0a7de745 3610 return nx;
1c79356b
A
3611}
3612
94ff46dc
A
3613struct nfsrv_getvfs_by_mntonname_callback_args {
3614 const char *path; /* IN */
3615 mount_t mp; /* OUT */
3616};
3617
3618static int
3619nfsrv_getvfs_by_mntonname_callback(mount_t mp, void *v)
3620{
3621 struct nfsrv_getvfs_by_mntonname_callback_args * const args = v;
3622 char real_mntonname[MAXPATHLEN];
3623 int pathbuflen = MAXPATHLEN;
3624 vnode_t rvp;
3625 int error;
3626
3627 error = VFS_ROOT(mp, &rvp, vfs_context_current());
3628 if (error) {
3629 goto out;
3630 }
3631 error = vn_getpath_ext(rvp, NULLVP, real_mntonname, &pathbuflen,
3632 VN_GETPATH_FSENTER | VN_GETPATH_NO_FIRMLINK);
3633 vnode_put(rvp);
3634 if (error) {
3635 goto out;
3636 }
3637 if (strcmp(args->path, real_mntonname) == 0) {
3638 error = vfs_busy(mp, LK_NOWAIT);
3639 if (error == 0) {
3640 args->mp = mp;
3641 }
3642 return VFS_RETURNED_DONE;
3643 }
3644out:
3645 return VFS_RETURNED;
3646}
3647
3648static mount_t
3649nfsrv_getvfs_by_mntonname(char *path)
3650{
3651 struct nfsrv_getvfs_by_mntonname_callback_args args = {
3652 .path = path,
3653 .mp = NULL,
3654 };
3655 mount_t mp;
3656 int error;
3657
3658 mp = vfs_getvfs_by_mntonname(path);
3659 if (mp) {
3660 error = vfs_busy(mp, LK_NOWAIT);
3661 mount_iterdrop(mp);
3662 if (error) {
3663 mp = NULL;
3664 }
3665 } else if (vfs_iterate(0, nfsrv_getvfs_by_mntonname_callback,
3666 &args) == 0) {
3667 mp = args.mp;
3668 }
3669 return mp;
3670}
3671
1c79356b 3672/*
91447636 3673 * nfsrv_fhtovp() - convert FH to vnode and export info
1c79356b
A
3674 */
3675int
91447636
A
3676nfsrv_fhtovp(
3677 struct nfs_filehandle *nfhp,
2d21ac55 3678 struct nfsrv_descript *nd,
91447636
A
3679 vnode_t *vpp,
3680 struct nfs_export **nxp,
3681 struct nfs_export_options **nxop)
1c79356b 3682{
2d21ac55
A
3683 struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
3684 struct nfs_export_options *nxo;
3685 u_char *fidp;
91447636
A
3686 int error;
3687 struct mount *mp;
2d21ac55 3688 mbuf_t nam = NULL;
0c530ab8 3689 uint32_t v;
2d21ac55 3690 int i, valid;
1c79356b 3691
91447636
A
3692 *vpp = NULL;
3693 *nxp = NULL;
3694 *nxop = NULL;
1c79356b 3695
0a7de745 3696 if (nd != NULL) {
2d21ac55 3697 nam = nd->nd_nam;
0a7de745 3698 }
2d21ac55
A
3699
3700 v = ntohl(nxh->nxh_version);
0c530ab8 3701 if (v != NFS_FH_VERSION) {
91447636 3702 /* file handle format not supported */
0a7de745
A
3703 return ESTALE;
3704 }
3705 if (nfhp->nfh_len > NFSV3_MAX_FH_SIZE) {
3706 return EBADRPC;
3707 }
3708 if (nfhp->nfh_len < (int)sizeof(struct nfs_exphandle)) {
3709 return ESTALE;
91447636 3710 }
2d21ac55 3711 v = ntohs(nxh->nxh_flags);
0a7de745
A
3712 if (v & NXHF_INVALIDFH) {
3713 return ESTALE;
3714 }
91447636 3715
91447636 3716 *nxp = nfsrv_fhtoexport(nfhp);
0a7de745
A
3717 if (!*nxp) {
3718 return ESTALE;
3719 }
91447636
A
3720
3721 /* Get the export option structure for this <export, client> tuple. */
2d21ac55 3722 *nxop = nxo = nfsrv_export_lookup(*nxp, nam);
0a7de745
A
3723 if (nam && (*nxop == NULL)) {
3724 return EACCES;
3725 }
91447636 3726
2d21ac55
A
3727 if (nd != NULL) {
3728 /* Validate the security flavor of the request */
3729 for (i = 0, valid = 0; i < nxo->nxo_sec.count; i++) {
3730 if (nd->nd_sec == nxo->nxo_sec.flavors[i]) {
3731 valid = 1;
3732 break;
3733 }
3734 }
3735 if (!valid) {
3736 /*
3737 * RFC 2623 section 2.3.2 recommends no authentication
3738 * requirement for certain NFS procedures used for mounting.
3739 * This allows an unauthenticated superuser on the client
3740 * to do mounts for the benefit of authenticated users.
3741 */
0a7de745 3742 if (nd->nd_vers == NFS_VER2) {
2d21ac55 3743 if (nd->nd_procnum == NFSV2PROC_GETATTR ||
0a7de745 3744 nd->nd_procnum == NFSV2PROC_STATFS) {
2d21ac55 3745 valid = 1;
0a7de745
A
3746 }
3747 }
3748 if (nd->nd_vers == NFS_VER3) {
3749 if (nd->nd_procnum == NFSPROC_FSINFO) {
2d21ac55 3750 valid = 1;
0a7de745
A
3751 }
3752 }
2d21ac55 3753
0a7de745
A
3754 if (!valid) {
3755 return NFSERR_AUTHERR | AUTH_REJECTCRED;
3756 }
2d21ac55
A
3757 }
3758 }
3759
0a7de745
A
3760 if (nxo && (nxo->nxo_flags & NX_OFFLINE)) {
3761 return (nd == NULL || nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER;
3762 }
2d21ac55 3763
91447636 3764 /* find mount structure */
94ff46dc 3765 mp = nfsrv_getvfs_by_mntonname((*nxp)->nx_fs->nxfs_path);
2d21ac55
A
3766 if (!mp) {
3767 /*
3768 * We have an export, but no mount?
3769 * Perhaps the export just hasn't been marked offline yet.
3770 */
0a7de745 3771 return (nd == NULL || nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER;
2d21ac55 3772 }
91447636 3773
2d21ac55
A
3774 fidp = nfhp->nfh_fhp + sizeof(*nxh);
3775 error = VFS_FHTOVP(mp, nxh->nxh_fidlen, fidp, vpp, NULL);
6d2010ae 3776 vfs_unbusy(mp);
0a7de745
A
3777 if (error) {
3778 return error;
3779 }
1c79356b 3780 /* vnode pointer should be good at this point or ... */
0a7de745
A
3781 if (*vpp == NULL) {
3782 return ESTALE;
3783 }
3784 return 0;
91447636 3785}
1c79356b 3786
91447636 3787/*
2d21ac55
A
3788 * nfsrv_credcheck() - check/map credentials according
3789 * to given export options.
91447636
A
3790 */
3791int
3792nfsrv_credcheck(
2d21ac55
A
3793 struct nfsrv_descript *nd,
3794 vfs_context_t ctx,
91447636
A
3795 __unused struct nfs_export *nx,
3796 struct nfs_export_options *nxo)
3797{
3798 if (nxo && nxo->nxo_cred) {
3799 if ((nxo->nxo_flags & NX_MAPALL) ||
2d21ac55 3800 ((nxo->nxo_flags & NX_MAPROOT) && !suser(nd->nd_cr, NULL))) {
0c530ab8 3801 kauth_cred_ref(nxo->nxo_cred);
2d21ac55
A
3802 kauth_cred_unref(&nd->nd_cr);
3803 nd->nd_cr = nxo->nxo_cred;
91447636
A
3804 }
3805 }
2d21ac55 3806 ctx->vc_ucred = nd->nd_cr;
0a7de745 3807 return 0;
1c79356b
A
3808}
3809
91447636
A
3810/*
3811 * nfsrv_vptofh() - convert vnode to file handle for given export
3812 *
3813 * If the caller is passing in a vnode for a ".." directory entry,
3814 * they can pass a directory NFS file handle (dnfhp) which will be
3815 * checked against the root export file handle. If it matches, we
3816 * refuse to provide the file handle for the out-of-export directory.
3817 */
3818int
3819nfsrv_vptofh(
3820 struct nfs_export *nx,
2d21ac55 3821 int nfsvers,
91447636
A
3822 struct nfs_filehandle *dnfhp,
3823 vnode_t vp,
2d21ac55 3824 vfs_context_t ctx,
91447636
A
3825 struct nfs_filehandle *nfhp)
3826{
3827 int error;
2d21ac55 3828 uint32_t maxfidsize;
91447636 3829
2d21ac55 3830 nfhp->nfh_fhp = (u_char*)&nfhp->nfh_xh;
0c530ab8
A
3831 nfhp->nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
3832 nfhp->nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
3833 nfhp->nfh_xh.nxh_expid = htonl(nx->nx_id);
91447636
A
3834 nfhp->nfh_xh.nxh_flags = 0;
3835 nfhp->nfh_xh.nxh_reserved = 0;
3836
0a7de745 3837 if (nfsvers == NFS_VER2) {
91447636 3838 bzero(&nfhp->nfh_fid[0], NFSV2_MAX_FID_SIZE);
0a7de745 3839 }
91447636
A
3840
3841 /* if directory FH matches export root, return invalid FH */
3842 if (dnfhp && nfsrv_fhmatch(dnfhp, &nx->nx_fh)) {
0a7de745 3843 if (nfsvers == NFS_VER2) {
2d21ac55 3844 nfhp->nfh_len = NFSX_V2FH;
0a7de745 3845 } else {
2d21ac55 3846 nfhp->nfh_len = sizeof(nfhp->nfh_xh);
0a7de745 3847 }
91447636 3848 nfhp->nfh_xh.nxh_fidlen = 0;
0c530ab8 3849 nfhp->nfh_xh.nxh_flags = htons(NXHF_INVALIDFH);
0a7de745 3850 return 0;
91447636
A
3851 }
3852
0a7de745 3853 if (nfsvers == NFS_VER2) {
2d21ac55 3854 maxfidsize = NFSV2_MAX_FID_SIZE;
0a7de745 3855 } else {
2d21ac55 3856 maxfidsize = NFSV3_MAX_FID_SIZE;
0a7de745 3857 }
2d21ac55
A
3858 nfhp->nfh_len = maxfidsize;
3859
3860 error = VFS_VPTOFH(vp, (int*)&nfhp->nfh_len, &nfhp->nfh_fid[0], ctx);
0a7de745
A
3861 if (error) {
3862 return error;
3863 }
3864 if (nfhp->nfh_len > maxfidsize) {
3865 return EOVERFLOW;
3866 }
91447636
A
3867 nfhp->nfh_xh.nxh_fidlen = nfhp->nfh_len;
3868 nfhp->nfh_len += sizeof(nfhp->nfh_xh);
0a7de745 3869 if ((nfsvers == NFS_VER2) && (nfhp->nfh_len < NFSX_V2FH)) {
91447636 3870 nfhp->nfh_len = NFSX_V2FH;
0a7de745 3871 }
91447636 3872
0a7de745 3873 return 0;
91447636
A
3874}
3875
2d21ac55
A
3876/*
3877 * Compare two file handles to see it they're the same.
3878 * Note that we don't use nfh_len because that may include
3879 * padding in an NFSv2 file handle.
3880 */
91447636
A
3881int
3882nfsrv_fhmatch(struct nfs_filehandle *fh1, struct nfs_filehandle *fh2)
3883{
2d21ac55 3884 struct nfs_exphandle *nxh1, *nxh2;
91447636
A
3885 int len1, len2;
3886
2d21ac55
A
3887 nxh1 = (struct nfs_exphandle *)fh1->nfh_fhp;
3888 nxh2 = (struct nfs_exphandle *)fh2->nfh_fhp;
3889 len1 = sizeof(fh1->nfh_xh) + nxh1->nxh_fidlen;
3890 len2 = sizeof(fh2->nfh_xh) + nxh2->nxh_fidlen;
0a7de745
A
3891 if (len1 != len2) {
3892 return 0;
3893 }
3894 if (bcmp(nxh1, nxh2, len1)) {
3895 return 0;
3896 }
3897 return 1;
91447636 3898}
2d21ac55 3899
1c79356b 3900/*
2d21ac55 3901 * Functions for dealing with active user lists
1c79356b 3902 */
2d21ac55 3903
1c79356b 3904/*
2d21ac55
A
3905 * Search the hash table for a user node with a matching IP address and uid field.
3906 * If found, the node's tm_last timestamp is updated and the node is returned.
3907 *
3908 * If not found, a new node is allocated (or reclaimed via LRU), initialized, and returned.
3909 * Returns NULL if a new node could not be allcoated.
3910 *
3911 * The list's user_mutex lock MUST be held.
1c79356b 3912 */
b0d623f7 3913struct nfs_user_stat_node *
6d2010ae 3914nfsrv_get_user_stat_node(struct nfs_active_user_list *list, struct sockaddr *saddr, uid_t uid)
1c79356b 3915{
0a7de745
A
3916 struct nfs_user_stat_node *unode;
3917 struct timeval now;
3918 struct nfs_user_stat_hashtbl_head *head;
2d21ac55
A
3919
3920 /* seach the hash table */
3921 head = NFS_USER_STAT_HASH(list->user_hashtbl, uid);
3922 LIST_FOREACH(unode, head, hash_link) {
6d2010ae 3923 if ((uid == unode->uid) && (nfs_sockaddr_cmp(saddr, (struct sockaddr*)&unode->sock) == 0)) {
2d21ac55
A
3924 /* found matching node */
3925 break;
3926 }
3927 }
1c79356b 3928
2d21ac55
A
3929 if (unode) {
3930 /* found node in the hash table, now update lru position */
3931 TAILQ_REMOVE(&list->user_lru, unode, lru_link);
3932 TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
3933
3934 /* update time stamp */
3935 microtime(&now);
3936 unode->tm_last = (uint32_t)now.tv_sec;
3937 return unode;
1c79356b 3938 }
2d21ac55
A
3939
3940 if (list->node_count < nfsrv_user_stat_max_nodes) {
3941 /* Allocate a new node */
3942 MALLOC(unode, struct nfs_user_stat_node *, sizeof(struct nfs_user_stat_node),
0a7de745 3943 M_TEMP, M_WAITOK | M_ZERO);
2d21ac55 3944
0a7de745 3945 if (!unode) {
2d21ac55 3946 return NULL;
0a7de745 3947 }
2d21ac55
A
3948
3949 /* increment node count */
b0d623f7 3950 OSAddAtomic(1, &nfsrv_user_stat_node_count);
2d21ac55
A
3951 list->node_count++;
3952 } else {
3953 /* reuse the oldest node in the lru list */
3954 unode = TAILQ_FIRST(&list->user_lru);
3955
0a7de745 3956 if (!unode) {
2d21ac55 3957 return NULL;
0a7de745 3958 }
2d21ac55
A
3959
3960 /* Remove the node */
3961 TAILQ_REMOVE(&list->user_lru, unode, lru_link);
3962 LIST_REMOVE(unode, hash_link);
1c79356b 3963 }
2d21ac55
A
3964
3965 /* Initialize the node */
3966 unode->uid = uid;
6d2010ae 3967 bcopy(saddr, &unode->sock, saddr->sa_len);
2d21ac55
A
3968 microtime(&now);
3969 unode->ops = 0;
3970 unode->bytes_read = 0;
3971 unode->bytes_written = 0;
3972 unode->tm_start = (uint32_t)now.tv_sec;
3973 unode->tm_last = (uint32_t)now.tv_sec;
3974
3975 /* insert the node */
3976 TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
3977 LIST_INSERT_HEAD(head, unode, hash_link);
3978
3979 return unode;
3980}
3981
3982void
3983nfsrv_update_user_stat(struct nfs_export *nx, struct nfsrv_descript *nd, uid_t uid, u_int ops, u_int rd_bytes, u_int wr_bytes)
3984{
0a7de745
A
3985 struct nfs_user_stat_node *unode;
3986 struct nfs_active_user_list *ulist;
3987 struct sockaddr *saddr;
2d21ac55 3988
0a7de745 3989 if ((!nfsrv_user_stat_enabled) || (!nx) || (!nd) || (!nd->nd_nam)) {
2d21ac55 3990 return;
0a7de745 3991 }
2d21ac55 3992
6d2010ae 3993 saddr = (struct sockaddr *)mbuf_data(nd->nd_nam);
2d21ac55
A
3994
3995 /* check address family before going any further */
0a7de745 3996 if ((saddr->sa_family != AF_INET) && (saddr->sa_family != AF_INET6)) {
2d21ac55 3997 return;
0a7de745 3998 }
2d21ac55
A
3999
4000 ulist = &nx->nx_user_list;
4001
4002 /* lock the active user list */
4003 lck_mtx_lock(&ulist->user_mutex);
4004
4005 /* get the user node */
6d2010ae 4006 unode = nfsrv_get_user_stat_node(ulist, saddr, uid);
2d21ac55
A
4007
4008 if (!unode) {
4009 lck_mtx_unlock(&ulist->user_mutex);
4010 return;
1c79356b 4011 }
2d21ac55
A
4012
4013 /* update counters */
4014 unode->ops += ops;
4015 unode->bytes_read += rd_bytes;
4016 unode->bytes_written += wr_bytes;
4017
4018 /* done */
4019 lck_mtx_unlock(&ulist->user_mutex);
4020}
4021
4022/* initialize an active user list */
b0d623f7 4023void
2d21ac55
A
4024nfsrv_init_user_list(struct nfs_active_user_list *ulist)
4025{
4026 uint i;
4027
4028 /* initialize the lru */
4029 TAILQ_INIT(&ulist->user_lru);
4030
4031 /* initialize the hash table */
0a7de745 4032 for (i = 0; i < NFS_USER_STAT_HASH_SIZE; i++) {
2d21ac55 4033 LIST_INIT(&ulist->user_hashtbl[i]);
0a7de745 4034 }
2d21ac55
A
4035 ulist->node_count = 0;
4036
4037 lck_mtx_init(&ulist->user_mutex, nfsrv_active_user_mutex_group, LCK_ATTR_NULL);
4038}
4039
4040/* Free all nodes in an active user list */
b0d623f7 4041void
2d21ac55
A
4042nfsrv_free_user_list(struct nfs_active_user_list *ulist)
4043{
4044 struct nfs_user_stat_node *unode;
4045
0a7de745 4046 if (!ulist) {
2d21ac55 4047 return;
0a7de745 4048 }
2d21ac55
A
4049
4050 while ((unode = TAILQ_FIRST(&ulist->user_lru))) {
4051 /* Remove node and free */
4052 TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
4053 LIST_REMOVE(unode, hash_link);
4054 FREE(unode, M_TEMP);
4055
4056 /* decrement node count */
b0d623f7 4057 OSAddAtomic(-1, &nfsrv_user_stat_node_count);
1c79356b 4058 }
2d21ac55
A
4059 ulist->node_count = 0;
4060
4061 lck_mtx_destroy(&ulist->user_mutex, nfsrv_active_user_mutex_group);
1c79356b
A
4062}
4063
2d21ac55 4064/* Reclaim old expired user nodes from active user lists. */
1c79356b 4065void
2d21ac55 4066nfsrv_active_user_list_reclaim(void)
1c79356b 4067{
0a7de745
A
4068 struct nfs_exportfs *nxfs;
4069 struct nfs_export *nx;
4070 struct nfs_active_user_list *ulist;
4071 struct nfs_user_stat_hashtbl_head oldlist;
4072 struct nfs_user_stat_node *unode, *unode_next;
4073 struct timeval now;
4074 uint32_t tstale;
2d21ac55
A
4075
4076 LIST_INIT(&oldlist);
4077
4078 lck_rw_lock_shared(&nfsrv_export_rwlock);
4079 microtime(&now);
4080 tstale = now.tv_sec - nfsrv_user_stat_max_idle_sec;
4081 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
4082 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
4083 /* Scan through all user nodes of this export */
4084 ulist = &nx->nx_user_list;
4085 lck_mtx_lock(&ulist->user_mutex);
4086 for (unode = TAILQ_FIRST(&ulist->user_lru); unode; unode = unode_next) {
4087 unode_next = TAILQ_NEXT(unode, lru_link);
4088
4089 /* check if this node has expired */
0a7de745 4090 if (unode->tm_last >= tstale) {
2d21ac55 4091 break;
0a7de745 4092 }
2d21ac55
A
4093
4094 /* Remove node from the active user list */
4095 TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
4096 LIST_REMOVE(unode, hash_link);
4097
4098 /* Add node to temp list */
4099 LIST_INSERT_HEAD(&oldlist, unode, hash_link);
4100
4101 /* decrement node count */
b0d623f7 4102 OSAddAtomic(-1, &nfsrv_user_stat_node_count);
2d21ac55
A
4103 ulist->node_count--;
4104 }
4105 /* can unlock this export's list now */
4106 lck_mtx_unlock(&ulist->user_mutex);
4107 }
4108 }
0a7de745 4109 lck_rw_done(&nfsrv_export_rwlock);
2d21ac55
A
4110
4111 /* Free expired nodes */
0a7de745 4112 while ((unode = LIST_FIRST(&oldlist))) {
2d21ac55
A
4113 LIST_REMOVE(unode, hash_link);
4114 FREE(unode, M_TEMP);
4115 }
1c79356b
A
4116}
4117
1c79356b 4118/*
2d21ac55
A
4119 * Maps errno values to nfs error numbers.
4120 * Use NFSERR_IO as the catch all for ones not specifically defined in
4121 * RFC 1094.
1c79356b 4122 */
2d21ac55 4123static u_char nfsrv_v2errmap[] = {
0a7de745
A
4124 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4125 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4126 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
4127 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
4128 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4129 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
4130 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4131 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4132 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4133 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4134 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4135 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
4136 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
4137 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
2d21ac55 4138};
1c79356b 4139
2d21ac55
A
4140/*
4141 * Maps errno values to nfs error numbers.
4142 * Although it is not obvious whether or not NFS clients really care if
4143 * a returned error value is in the specified list for the procedure, the
4144 * safest thing to do is filter them appropriately. For Version 2, the
4145 * X/Open XNFS document is the only specification that defines error values
4146 * for each RPC (The RFC simply lists all possible error values for all RPCs),
4147 * so I have decided to not do this for Version 2.
4148 * The first entry is the default error return and the rest are the valid
4149 * errors for that RPC in increasing numeric order.
4150 */
4151static short nfsv3err_null[] = {
4152 0,
4153 0,
4154};
4155
4156static short nfsv3err_getattr[] = {
4157 NFSERR_IO,
4158 NFSERR_IO,
4159 NFSERR_STALE,
4160 NFSERR_BADHANDLE,
4161 NFSERR_SERVERFAULT,
4162 NFSERR_TRYLATER,
4163 0,
4164};
4165
4166static short nfsv3err_setattr[] = {
4167 NFSERR_IO,
4168 NFSERR_PERM,
4169 NFSERR_IO,
4170 NFSERR_ACCES,
4171 NFSERR_INVAL,
4172 NFSERR_NOSPC,
4173 NFSERR_ROFS,
4174 NFSERR_DQUOT,
4175 NFSERR_STALE,
4176 NFSERR_BADHANDLE,
4177 NFSERR_NOT_SYNC,
4178 NFSERR_SERVERFAULT,
4179 NFSERR_TRYLATER,
4180 0,
4181};
4182
4183static short nfsv3err_lookup[] = {
4184 NFSERR_IO,
4185 NFSERR_NOENT,
4186 NFSERR_IO,
4187 NFSERR_ACCES,
4188 NFSERR_NOTDIR,
4189 NFSERR_NAMETOL,
4190 NFSERR_STALE,
4191 NFSERR_BADHANDLE,
4192 NFSERR_SERVERFAULT,
4193 NFSERR_TRYLATER,
4194 0,
4195};
4196
4197static short nfsv3err_access[] = {
4198 NFSERR_IO,
4199 NFSERR_IO,
4200 NFSERR_STALE,
4201 NFSERR_BADHANDLE,
4202 NFSERR_SERVERFAULT,
4203 NFSERR_TRYLATER,
4204 0,
4205};
4206
4207static short nfsv3err_readlink[] = {
4208 NFSERR_IO,
4209 NFSERR_IO,
4210 NFSERR_ACCES,
4211 NFSERR_INVAL,
4212 NFSERR_STALE,
4213 NFSERR_BADHANDLE,
4214 NFSERR_NOTSUPP,
4215 NFSERR_SERVERFAULT,
4216 NFSERR_TRYLATER,
4217 0,
4218};
4219
4220static short nfsv3err_read[] = {
4221 NFSERR_IO,
4222 NFSERR_IO,
4223 NFSERR_NXIO,
4224 NFSERR_ACCES,
4225 NFSERR_INVAL,
4226 NFSERR_STALE,
4227 NFSERR_BADHANDLE,
4228 NFSERR_SERVERFAULT,
4229 NFSERR_TRYLATER,
4230 0,
4231};
4232
4233static short nfsv3err_write[] = {
4234 NFSERR_IO,
4235 NFSERR_IO,
4236 NFSERR_ACCES,
4237 NFSERR_INVAL,
4238 NFSERR_FBIG,
4239 NFSERR_NOSPC,
4240 NFSERR_ROFS,
4241 NFSERR_DQUOT,
4242 NFSERR_STALE,
4243 NFSERR_BADHANDLE,
4244 NFSERR_SERVERFAULT,
4245 NFSERR_TRYLATER,
4246 0,
4247};
4248
4249static short nfsv3err_create[] = {
4250 NFSERR_IO,
4251 NFSERR_IO,
4252 NFSERR_ACCES,
4253 NFSERR_EXIST,
4254 NFSERR_NOTDIR,
4255 NFSERR_NOSPC,
4256 NFSERR_ROFS,
4257 NFSERR_NAMETOL,
4258 NFSERR_DQUOT,
4259 NFSERR_STALE,
4260 NFSERR_BADHANDLE,
4261 NFSERR_NOTSUPP,
4262 NFSERR_SERVERFAULT,
4263 NFSERR_TRYLATER,
4264 0,
4265};
4266
4267static short nfsv3err_mkdir[] = {
4268 NFSERR_IO,
4269 NFSERR_IO,
4270 NFSERR_ACCES,
4271 NFSERR_EXIST,
4272 NFSERR_NOTDIR,
4273 NFSERR_NOSPC,
4274 NFSERR_ROFS,
4275 NFSERR_NAMETOL,
4276 NFSERR_DQUOT,
4277 NFSERR_STALE,
4278 NFSERR_BADHANDLE,
4279 NFSERR_NOTSUPP,
4280 NFSERR_SERVERFAULT,
4281 NFSERR_TRYLATER,
4282 0,
4283};
4284
4285static short nfsv3err_symlink[] = {
4286 NFSERR_IO,
4287 NFSERR_IO,
4288 NFSERR_ACCES,
4289 NFSERR_EXIST,
4290 NFSERR_NOTDIR,
4291 NFSERR_NOSPC,
4292 NFSERR_ROFS,
4293 NFSERR_NAMETOL,
4294 NFSERR_DQUOT,
4295 NFSERR_STALE,
4296 NFSERR_BADHANDLE,
4297 NFSERR_NOTSUPP,
4298 NFSERR_SERVERFAULT,
4299 NFSERR_TRYLATER,
4300 0,
4301};
4302
4303static short nfsv3err_mknod[] = {
4304 NFSERR_IO,
4305 NFSERR_IO,
4306 NFSERR_ACCES,
4307 NFSERR_EXIST,
4308 NFSERR_NOTDIR,
4309 NFSERR_NOSPC,
4310 NFSERR_ROFS,
4311 NFSERR_NAMETOL,
4312 NFSERR_DQUOT,
4313 NFSERR_STALE,
4314 NFSERR_BADHANDLE,
4315 NFSERR_NOTSUPP,
4316 NFSERR_SERVERFAULT,
4317 NFSERR_BADTYPE,
4318 NFSERR_TRYLATER,
4319 0,
4320};
4321
4322static short nfsv3err_remove[] = {
4323 NFSERR_IO,
4324 NFSERR_NOENT,
4325 NFSERR_IO,
4326 NFSERR_ACCES,
4327 NFSERR_NOTDIR,
4328 NFSERR_ROFS,
4329 NFSERR_NAMETOL,
4330 NFSERR_STALE,
4331 NFSERR_BADHANDLE,
4332 NFSERR_SERVERFAULT,
4333 NFSERR_TRYLATER,
4334 0,
4335};
4336
4337static short nfsv3err_rmdir[] = {
4338 NFSERR_IO,
4339 NFSERR_NOENT,
4340 NFSERR_IO,
4341 NFSERR_ACCES,
4342 NFSERR_EXIST,
4343 NFSERR_NOTDIR,
4344 NFSERR_INVAL,
4345 NFSERR_ROFS,
4346 NFSERR_NAMETOL,
4347 NFSERR_NOTEMPTY,
4348 NFSERR_STALE,
4349 NFSERR_BADHANDLE,
4350 NFSERR_NOTSUPP,
4351 NFSERR_SERVERFAULT,
4352 NFSERR_TRYLATER,
4353 0,
4354};
4355
4356static short nfsv3err_rename[] = {
4357 NFSERR_IO,
4358 NFSERR_NOENT,
4359 NFSERR_IO,
4360 NFSERR_ACCES,
4361 NFSERR_EXIST,
4362 NFSERR_XDEV,
4363 NFSERR_NOTDIR,
4364 NFSERR_ISDIR,
4365 NFSERR_INVAL,
4366 NFSERR_NOSPC,
4367 NFSERR_ROFS,
4368 NFSERR_MLINK,
4369 NFSERR_NAMETOL,
4370 NFSERR_NOTEMPTY,
4371 NFSERR_DQUOT,
4372 NFSERR_STALE,
4373 NFSERR_BADHANDLE,
4374 NFSERR_NOTSUPP,
4375 NFSERR_SERVERFAULT,
4376 NFSERR_TRYLATER,
4377 0,
4378};
4379
4380static short nfsv3err_link[] = {
4381 NFSERR_IO,
4382 NFSERR_IO,
4383 NFSERR_ACCES,
4384 NFSERR_EXIST,
4385 NFSERR_XDEV,
4386 NFSERR_NOTDIR,
4387 NFSERR_INVAL,
4388 NFSERR_NOSPC,
4389 NFSERR_ROFS,
4390 NFSERR_MLINK,
4391 NFSERR_NAMETOL,
4392 NFSERR_DQUOT,
4393 NFSERR_STALE,
4394 NFSERR_BADHANDLE,
4395 NFSERR_NOTSUPP,
4396 NFSERR_SERVERFAULT,
4397 NFSERR_TRYLATER,
4398 0,
4399};
4400
4401static short nfsv3err_readdir[] = {
4402 NFSERR_IO,
4403 NFSERR_IO,
4404 NFSERR_ACCES,
4405 NFSERR_NOTDIR,
4406 NFSERR_STALE,
4407 NFSERR_BADHANDLE,
4408 NFSERR_BAD_COOKIE,
4409 NFSERR_TOOSMALL,
4410 NFSERR_SERVERFAULT,
4411 NFSERR_TRYLATER,
4412 0,
4413};
4414
4415static short nfsv3err_readdirplus[] = {
4416 NFSERR_IO,
4417 NFSERR_IO,
4418 NFSERR_ACCES,
4419 NFSERR_NOTDIR,
4420 NFSERR_STALE,
4421 NFSERR_BADHANDLE,
4422 NFSERR_BAD_COOKIE,
4423 NFSERR_NOTSUPP,
4424 NFSERR_TOOSMALL,
4425 NFSERR_SERVERFAULT,
4426 NFSERR_TRYLATER,
4427 0,
4428};
4429
4430static short nfsv3err_fsstat[] = {
4431 NFSERR_IO,
4432 NFSERR_IO,
4433 NFSERR_STALE,
4434 NFSERR_BADHANDLE,
4435 NFSERR_SERVERFAULT,
4436 NFSERR_TRYLATER,
4437 0,
4438};
4439
4440static short nfsv3err_fsinfo[] = {
4441 NFSERR_STALE,
4442 NFSERR_STALE,
4443 NFSERR_BADHANDLE,
4444 NFSERR_SERVERFAULT,
4445 NFSERR_TRYLATER,
4446 0,
4447};
4448
4449static short nfsv3err_pathconf[] = {
4450 NFSERR_STALE,
4451 NFSERR_STALE,
4452 NFSERR_BADHANDLE,
4453 NFSERR_SERVERFAULT,
4454 NFSERR_TRYLATER,
4455 0,
4456};
4457
4458static short nfsv3err_commit[] = {
4459 NFSERR_IO,
4460 NFSERR_IO,
4461 NFSERR_STALE,
4462 NFSERR_BADHANDLE,
4463 NFSERR_SERVERFAULT,
4464 NFSERR_TRYLATER,
4465 0,
4466};
4467
4468static short *nfsrv_v3errmap[] = {
4469 nfsv3err_null,
4470 nfsv3err_getattr,
4471 nfsv3err_setattr,
4472 nfsv3err_lookup,
4473 nfsv3err_access,
4474 nfsv3err_readlink,
4475 nfsv3err_read,
4476 nfsv3err_write,
4477 nfsv3err_create,
4478 nfsv3err_mkdir,
4479 nfsv3err_symlink,
4480 nfsv3err_mknod,
4481 nfsv3err_remove,
4482 nfsv3err_rmdir,
4483 nfsv3err_rename,
4484 nfsv3err_link,
4485 nfsv3err_readdir,
4486 nfsv3err_readdirplus,
4487 nfsv3err_fsstat,
4488 nfsv3err_fsinfo,
4489 nfsv3err_pathconf,
4490 nfsv3err_commit,
4491};
4492
4493/*
4494 * Map errnos to NFS error numbers. For Version 3 also filter out error
4495 * numbers not specified for the associated procedure.
4496 */
4497int
4498nfsrv_errmap(struct nfsrv_descript *nd, int err)
4499{
4500 short *defaulterrp, *errp;
4501
4502 if (nd->nd_vers == NFS_VER2) {
0a7de745
A
4503 if (err <= (int)sizeof(nfsrv_v2errmap)) {
4504 return (int)nfsrv_v2errmap[err - 1];
4505 }
4506 return NFSERR_IO;
2d21ac55
A
4507 }
4508 /* NFSv3 */
0a7de745
A
4509 if (nd->nd_procnum > NFSPROC_COMMIT) {
4510 return err & 0xffff;
4511 }
2d21ac55
A
4512 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
4513 while (*++errp) {
0a7de745
A
4514 if (*errp == err) {
4515 return err;
4516 } else if (*errp > err) {
2d21ac55 4517 break;
0a7de745 4518 }
1c79356b 4519 }
0a7de745 4520 return (int)*defaulterrp;
1c79356b
A
4521}
4522
ea3f0419
A
4523#endif /* CONFIG_NFS_SERVER */
4524
4525#endif /* CONFIG_NFS */