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