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