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