]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vfsops.c
xnu-1699.32.7.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
1 /*
2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993, 1995
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_vfsops.c 8.12 (Berkeley) 5/20/95
65 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/conf.h>
77 #include <sys/ioctl.h>
78 #include <sys/signal.h>
79 #include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
80 #include <sys/kauth.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/malloc.h>
83 #include <sys/kernel.h>
84 #include <sys/sysctl.h>
85 #include <sys/mount_internal.h>
86 #include <sys/kpi_mbuf.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/fcntl.h>
90 #include <sys/quota.h>
91 #include <sys/priv.h>
92 #include <libkern/OSAtomic.h>
93
94 #include <sys/vm.h>
95 #include <sys/vmparam.h>
96
97 #if !defined(NO_MOUNT_PRIVATE)
98 #include <sys/filedesc.h>
99 #endif /* NO_MOUNT_PRIVATE */
100
101 #include <net/if.h>
102 #include <net/route.h>
103 #include <netinet/in.h>
104
105 #include <nfs/rpcv2.h>
106 #include <nfs/krpc.h>
107 #include <nfs/nfsproto.h>
108 #include <nfs/nfs.h>
109 #include <nfs/nfsnode.h>
110 #include <nfs/nfs_gss.h>
111 #include <nfs/nfsmount.h>
112 #include <nfs/xdr_subs.h>
113 #include <nfs/nfsm_subs.h>
114 #include <nfs/nfsdiskless.h>
115 #include <nfs/nfs_lock.h>
116 #if CONFIG_MACF
117 #include <security/mac_framework.h>
118 #endif
119
120 #include <pexpert/pexpert.h>
121
122 /*
123 * NFS client globals
124 */
125
126 int nfs_ticks;
127 static lck_grp_t *nfs_global_grp, *nfs_mount_grp;
128 lck_mtx_t *nfs_global_mutex;
129 uint32_t nfs_fs_attr_bitmap[NFS_ATTR_BITMAP_LEN];
130 uint32_t nfs_object_attr_bitmap[NFS_ATTR_BITMAP_LEN];
131 uint32_t nfs_getattr_bitmap[NFS_ATTR_BITMAP_LEN];
132 struct nfsclientidlist nfsclientids;
133
134 /* NFS requests */
135 struct nfs_reqqhead nfs_reqq;
136 lck_grp_t *nfs_request_grp;
137 lck_mtx_t *nfs_request_mutex;
138 thread_call_t nfs_request_timer_call;
139 int nfs_request_timer_on;
140 u_int32_t nfs_xid = 0;
141 u_int32_t nfs_xidwrap = 0; /* to build a (non-wrapping) 64 bit xid */
142
143 thread_call_t nfs_buf_timer_call;
144
145 /* NFSv4 */
146 lck_grp_t *nfs_open_grp;
147 uint32_t nfs_open_owner_seqnum = 0;
148 uint32_t nfs_lock_owner_seqnum = 0;
149 thread_call_t nfs4_callback_timer_call;
150 int nfs4_callback_timer_on = 0;
151
152 /* nfsiod */
153 lck_grp_t *nfsiod_lck_grp;
154 lck_mtx_t *nfsiod_mutex;
155 struct nfsiodlist nfsiodfree, nfsiodwork;
156 struct nfsiodmountlist nfsiodmounts;
157 int nfsiod_thread_count = 0;
158 int nfsiod_thread_max = NFS_DEFASYNCTHREAD;
159 int nfs_max_async_writes = NFS_DEFMAXASYNCWRITES;
160
161 int nfs_iosize = NFS_IOSIZE;
162 int nfs_access_cache_timeout = NFS_MAXATTRTIMO;
163 int nfs_access_delete = 1; /* too many servers get this wrong - workaround on by default */
164 int nfs_access_dotzfs = 1;
165 int nfs_access_for_getattr = 0;
166 int nfs_allow_async = 0;
167 int nfs_statfs_rate_limit = NFS_DEFSTATFSRATELIMIT;
168 int nfs_lockd_mounts = 0;
169 int nfs_lockd_request_sent = 0;
170 int nfs_idmap_ctrl = NFS_IDMAP_CTRL_USE_IDMAP_SERVICE;
171 int nfs_callback_port = 0;
172
173 int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
174 int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
175
176
177 int mountnfs(char *, mount_t, vfs_context_t, vnode_t *);
178 static int nfs_mount_diskless(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *, vfs_context_t);
179 #if !defined(NO_MOUNT_PRIVATE)
180 static int nfs_mount_diskless_private(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *, vfs_context_t);
181 #endif /* NO_MOUNT_PRIVATE */
182 int nfs_mount_connect(struct nfsmount *);
183 void nfs_mount_cleanup(struct nfsmount *);
184 int nfs_mountinfo_assemble(struct nfsmount *, struct xdrbuf *);
185 int nfs4_mount_update_path_with_symlink(struct nfsmount *, struct nfs_fs_path *, uint32_t, fhandle_t *, int *, fhandle_t *, vfs_context_t);
186
187 /*
188 * NFS VFS operations.
189 */
190 int nfs_vfs_mount(mount_t, vnode_t, user_addr_t, vfs_context_t);
191 int nfs_vfs_start(mount_t, int, vfs_context_t);
192 int nfs_vfs_unmount(mount_t, int, vfs_context_t);
193 int nfs_vfs_root(mount_t, vnode_t *, vfs_context_t);
194 int nfs_vfs_quotactl(mount_t, int, uid_t, caddr_t, vfs_context_t);
195 int nfs_vfs_getattr(mount_t, struct vfs_attr *, vfs_context_t);
196 int nfs_vfs_sync(mount_t, int, vfs_context_t);
197 int nfs_vfs_vget(mount_t, ino64_t, vnode_t *, vfs_context_t);
198 int nfs_vfs_vptofh(vnode_t, int *, unsigned char *, vfs_context_t);
199 int nfs_vfs_fhtovp(mount_t, int, unsigned char *, vnode_t *, vfs_context_t);
200 int nfs_vfs_init(struct vfsconf *);
201 int nfs_vfs_sysctl(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t);
202
203 struct vfsops nfs_vfsops = {
204 nfs_vfs_mount,
205 nfs_vfs_start,
206 nfs_vfs_unmount,
207 nfs_vfs_root,
208 nfs_vfs_quotactl,
209 nfs_vfs_getattr,
210 nfs_vfs_sync,
211 nfs_vfs_vget,
212 nfs_vfs_fhtovp,
213 nfs_vfs_vptofh,
214 nfs_vfs_init,
215 nfs_vfs_sysctl,
216 NULL, /* setattr */
217 { NULL, /* reserved */
218 NULL, /* reserved */
219 NULL, /* reserved */
220 NULL, /* reserved */
221 NULL, /* reserved */
222 NULL, /* reserved */
223 NULL } /* reserved */
224 };
225
226
227 /*
228 * version-specific NFS functions
229 */
230 int nfs3_mount(struct nfsmount *, vfs_context_t, nfsnode_t *);
231 int nfs4_mount(struct nfsmount *, vfs_context_t, nfsnode_t *);
232 int nfs3_fsinfo(struct nfsmount *, nfsnode_t, vfs_context_t);
233 int nfs3_update_statfs(struct nfsmount *, vfs_context_t);
234 int nfs4_update_statfs(struct nfsmount *, vfs_context_t);
235 #if !QUOTA
236 #define nfs3_getquota NULL
237 #define nfs4_getquota NULL
238 #else
239 int nfs3_getquota(struct nfsmount *, vfs_context_t, uid_t, int, struct dqblk *);
240 int nfs4_getquota(struct nfsmount *, vfs_context_t, uid_t, int, struct dqblk *);
241 #endif
242
243 struct nfs_funcs nfs3_funcs = {
244 nfs3_mount,
245 nfs3_update_statfs,
246 nfs3_getquota,
247 nfs3_access_rpc,
248 nfs3_getattr_rpc,
249 nfs3_setattr_rpc,
250 nfs3_read_rpc_async,
251 nfs3_read_rpc_async_finish,
252 nfs3_readlink_rpc,
253 nfs3_write_rpc_async,
254 nfs3_write_rpc_async_finish,
255 nfs3_commit_rpc,
256 nfs3_lookup_rpc_async,
257 nfs3_lookup_rpc_async_finish,
258 nfs3_remove_rpc,
259 nfs3_rename_rpc,
260 nfs3_setlock_rpc,
261 nfs3_unlock_rpc,
262 nfs3_getlock_rpc
263 };
264 struct nfs_funcs nfs4_funcs = {
265 nfs4_mount,
266 nfs4_update_statfs,
267 nfs4_getquota,
268 nfs4_access_rpc,
269 nfs4_getattr_rpc,
270 nfs4_setattr_rpc,
271 nfs4_read_rpc_async,
272 nfs4_read_rpc_async_finish,
273 nfs4_readlink_rpc,
274 nfs4_write_rpc_async,
275 nfs4_write_rpc_async_finish,
276 nfs4_commit_rpc,
277 nfs4_lookup_rpc_async,
278 nfs4_lookup_rpc_async_finish,
279 nfs4_remove_rpc,
280 nfs4_rename_rpc,
281 nfs4_setlock_rpc,
282 nfs4_unlock_rpc,
283 nfs4_getlock_rpc
284 };
285
286 /*
287 * Called once to initialize data structures...
288 */
289 int
290 nfs_vfs_init(__unused struct vfsconf *vfsp)
291 {
292 int i;
293
294 /*
295 * Check to see if major data structures haven't bloated.
296 */
297 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
298 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
299 printf("Try reducing NFS_SMALLFH\n");
300 }
301 if (sizeof (struct nfsmount) > NFS_MNTALLOC)
302 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
303
304 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
305 if (nfs_ticks < 1)
306 nfs_ticks = 1;
307
308 /* init async I/O thread pool state */
309 TAILQ_INIT(&nfsiodfree);
310 TAILQ_INIT(&nfsiodwork);
311 TAILQ_INIT(&nfsiodmounts);
312 nfsiod_lck_grp = lck_grp_alloc_init("nfsiod", LCK_GRP_ATTR_NULL);
313 nfsiod_mutex = lck_mtx_alloc_init(nfsiod_lck_grp, LCK_ATTR_NULL);
314
315 /* init lock groups, etc. */
316 nfs_mount_grp = lck_grp_alloc_init("nfs_mount", LCK_GRP_ATTR_NULL);
317 nfs_open_grp = lck_grp_alloc_init("nfs_open", LCK_GRP_ATTR_NULL);
318 nfs_global_grp = lck_grp_alloc_init("nfs_global", LCK_GRP_ATTR_NULL);
319
320 nfs_global_mutex = lck_mtx_alloc_init(nfs_global_grp, LCK_ATTR_NULL);
321
322 /* init request list mutex */
323 nfs_request_grp = lck_grp_alloc_init("nfs_request", LCK_GRP_ATTR_NULL);
324 nfs_request_mutex = lck_mtx_alloc_init(nfs_request_grp, LCK_ATTR_NULL);
325
326 /* initialize NFS request list */
327 TAILQ_INIT(&nfs_reqq);
328
329 nfs_nbinit(); /* Init the nfsbuf table */
330 nfs_nhinit(); /* Init the nfsnode table */
331 nfs_lockinit(); /* Init the nfs lock state */
332 nfs_gss_init(); /* Init RPCSEC_GSS security */
333
334 /* NFSv4 stuff */
335 NFS4_PER_FS_ATTRIBUTES(nfs_fs_attr_bitmap);
336 NFS4_PER_OBJECT_ATTRIBUTES(nfs_object_attr_bitmap);
337 NFS4_DEFAULT_ATTRIBUTES(nfs_getattr_bitmap);
338 for (i=0; i < NFS_ATTR_BITMAP_LEN; i++)
339 nfs_getattr_bitmap[i] &= nfs_object_attr_bitmap[i];
340 TAILQ_INIT(&nfsclientids);
341
342 /* initialize NFS timer callouts */
343 nfs_request_timer_call = thread_call_allocate(nfs_request_timer, NULL);
344 nfs_buf_timer_call = thread_call_allocate(nfs_buf_timer, NULL);
345 nfs4_callback_timer_call = thread_call_allocate(nfs4_callback_timer, NULL);
346
347 return (0);
348 }
349
350 /*
351 * nfs statfs call
352 */
353 int
354 nfs3_update_statfs(struct nfsmount *nmp, vfs_context_t ctx)
355 {
356 nfsnode_t np;
357 int error = 0, lockerror, status, nfsvers;
358 u_int64_t xid;
359 struct nfsm_chain nmreq, nmrep;
360 uint32_t val = 0;
361
362 nfsvers = nmp->nm_vers;
363 np = nmp->nm_dnp;
364 if (!np)
365 return (ENXIO);
366 if ((error = vnode_get(NFSTOV(np))))
367 return (error);
368
369 nfsm_chain_null(&nmreq);
370 nfsm_chain_null(&nmrep);
371
372 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers));
373 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
374 nfsm_chain_build_done(error, &nmreq);
375 nfsmout_if(error);
376 error = nfs_request(np, NULL, &nmreq, NFSPROC_FSSTAT, ctx, NULL, &nmrep, &xid, &status);
377 if ((lockerror = nfs_node_lock(np)))
378 error = lockerror;
379 if (nfsvers == NFS_VER3)
380 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
381 if (!lockerror)
382 nfs_node_unlock(np);
383 if (!error)
384 error = status;
385 nfsm_assert(error, NFSTONMP(np), ENXIO);
386 nfsmout_if(error);
387 lck_mtx_lock(&nmp->nm_lock);
388 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL);
389 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE);
390 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_AVAIL);
391 if (nfsvers == NFS_VER3) {
392 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_AVAIL);
393 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_TOTAL);
394 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_FREE);
395 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
396 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_total);
397 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_free);
398 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_avail);
399 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_total);
400 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_free);
401 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_avail);
402 // skip invarsec
403 } else {
404 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip tsize?
405 nfsm_chain_get_32(error, &nmrep, nmp->nm_fsattr.nfsa_bsize);
406 nfsm_chain_get_32(error, &nmrep, val);
407 nfsmout_if(error);
408 if (nmp->nm_fsattr.nfsa_bsize <= 0)
409 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
410 nmp->nm_fsattr.nfsa_space_total = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
411 nfsm_chain_get_32(error, &nmrep, val);
412 nfsmout_if(error);
413 nmp->nm_fsattr.nfsa_space_free = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
414 nfsm_chain_get_32(error, &nmrep, val);
415 nfsmout_if(error);
416 nmp->nm_fsattr.nfsa_space_avail = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
417 }
418 lck_mtx_unlock(&nmp->nm_lock);
419 nfsmout:
420 nfsm_chain_cleanup(&nmreq);
421 nfsm_chain_cleanup(&nmrep);
422 vnode_put(NFSTOV(np));
423 return (error);
424 }
425
426 int
427 nfs4_update_statfs(struct nfsmount *nmp, vfs_context_t ctx)
428 {
429 nfsnode_t np;
430 int error = 0, lockerror, status, nfsvers, numops;
431 u_int64_t xid;
432 struct nfsm_chain nmreq, nmrep;
433 uint32_t bitmap[NFS_ATTR_BITMAP_LEN];
434 struct nfs_vattr nvattr;
435 struct nfsreq_secinfo_args si;
436
437 nfsvers = nmp->nm_vers;
438 np = nmp->nm_dnp;
439 if (!np)
440 return (ENXIO);
441 if ((error = vnode_get(NFSTOV(np))))
442 return (error);
443
444 NFSREQ_SECINFO_SET(&si, np, NULL, 0, NULL, 0);
445 NVATTR_INIT(&nvattr);
446 nfsm_chain_null(&nmreq);
447 nfsm_chain_null(&nmrep);
448
449 // PUTFH + GETATTR
450 numops = 2;
451 nfsm_chain_build_alloc_init(error, &nmreq, 15 * NFSX_UNSIGNED);
452 nfsm_chain_add_compound_header(error, &nmreq, "statfs", numops);
453 numops--;
454 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
455 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
456 numops--;
457 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
458 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap, bitmap);
459 NFS4_STATFS_ATTRIBUTES(bitmap);
460 nfsm_chain_add_bitmap_supported(error, &nmreq, bitmap, nmp, np);
461 nfsm_chain_build_done(error, &nmreq);
462 nfsm_assert(error, (numops == 0), EPROTO);
463 nfsmout_if(error);
464 error = nfs_request(np, NULL, &nmreq, NFSPROC4_COMPOUND, ctx, &si, &nmrep, &xid, &status);
465 nfsm_chain_skip_tag(error, &nmrep);
466 nfsm_chain_get_32(error, &nmrep, numops);
467 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
468 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
469 nfsm_assert(error, NFSTONMP(np), ENXIO);
470 nfsmout_if(error);
471 lck_mtx_lock(&nmp->nm_lock);
472 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, NULL, NULL, NULL);
473 lck_mtx_unlock(&nmp->nm_lock);
474 nfsmout_if(error);
475 if ((lockerror = nfs_node_lock(np)))
476 error = lockerror;
477 if (!error)
478 nfs_loadattrcache(np, &nvattr, &xid, 0);
479 if (!lockerror)
480 nfs_node_unlock(np);
481 nfsm_assert(error, NFSTONMP(np), ENXIO);
482 nfsmout_if(error);
483 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
484 nfsmout:
485 NVATTR_CLEANUP(&nvattr);
486 nfsm_chain_cleanup(&nmreq);
487 nfsm_chain_cleanup(&nmrep);
488 vnode_put(NFSTOV(np));
489 return (error);
490 }
491
492
493 /*
494 * The NFS VFS_GETATTR function: "statfs"-type information is retrieved
495 * using the nf_update_statfs() function, and other attributes are cobbled
496 * together from whatever sources we can (getattr, fsinfo, pathconf).
497 */
498 int
499 nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t ctx)
500 {
501 struct nfsmount *nmp;
502 uint32_t bsize;
503 int error = 0, nfsvers;
504
505 if (!(nmp = VFSTONFS(mp)))
506 return (ENXIO);
507 nfsvers = nmp->nm_vers;
508
509 if (VFSATTR_IS_ACTIVE(fsap, f_bsize) ||
510 VFSATTR_IS_ACTIVE(fsap, f_iosize) ||
511 VFSATTR_IS_ACTIVE(fsap, f_blocks) ||
512 VFSATTR_IS_ACTIVE(fsap, f_bfree) ||
513 VFSATTR_IS_ACTIVE(fsap, f_bavail) ||
514 VFSATTR_IS_ACTIVE(fsap, f_bused) ||
515 VFSATTR_IS_ACTIVE(fsap, f_files) ||
516 VFSATTR_IS_ACTIVE(fsap, f_ffree)) {
517 int statfsrate = nfs_statfs_rate_limit;
518 int refresh = 1;
519
520 /*
521 * Are we rate-limiting statfs RPCs?
522 * (Treat values less than 1 or greater than 1,000,000 as no limit.)
523 */
524 if ((statfsrate > 0) && (statfsrate < 1000000)) {
525 struct timeval now;
526 uint32_t stamp;
527
528 microuptime(&now);
529 lck_mtx_lock(&nmp->nm_lock);
530 stamp = (now.tv_sec * statfsrate) + (now.tv_usec / (1000000/statfsrate));
531 if (stamp != nmp->nm_fsattrstamp) {
532 refresh = 1;
533 nmp->nm_fsattrstamp = stamp;
534 } else {
535 refresh = 0;
536 }
537 lck_mtx_unlock(&nmp->nm_lock);
538 }
539
540 if (refresh)
541 error = nmp->nm_funcs->nf_update_statfs(nmp, ctx);
542 if ((error == ESTALE) || (error == ETIMEDOUT))
543 error = 0;
544 if (error)
545 return (error);
546
547 lck_mtx_lock(&nmp->nm_lock);
548 VFSATTR_RETURN(fsap, f_iosize, nfs_iosize);
549 VFSATTR_RETURN(fsap, f_bsize, nmp->nm_fsattr.nfsa_bsize);
550 bsize = nmp->nm_fsattr.nfsa_bsize;
551 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL))
552 VFSATTR_RETURN(fsap, f_blocks, nmp->nm_fsattr.nfsa_space_total / bsize);
553 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE))
554 VFSATTR_RETURN(fsap, f_bfree, nmp->nm_fsattr.nfsa_space_free / bsize);
555 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_AVAIL))
556 VFSATTR_RETURN(fsap, f_bavail, nmp->nm_fsattr.nfsa_space_avail / bsize);
557 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL) &&
558 NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE))
559 VFSATTR_RETURN(fsap, f_bused,
560 (nmp->nm_fsattr.nfsa_space_total / bsize) -
561 (nmp->nm_fsattr.nfsa_space_free / bsize));
562 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_TOTAL))
563 VFSATTR_RETURN(fsap, f_files, nmp->nm_fsattr.nfsa_files_total);
564 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_FREE))
565 VFSATTR_RETURN(fsap, f_ffree, nmp->nm_fsattr.nfsa_files_free);
566 lck_mtx_unlock(&nmp->nm_lock);
567 }
568
569 if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
570 u_int32_t caps, valid;
571 nfsnode_t np = nmp->nm_dnp;
572
573 nfsm_assert(error, VFSTONFS(mp) && np, ENXIO);
574 if (error)
575 return (error);
576 lck_mtx_lock(&nmp->nm_lock);
577
578 /*
579 * The capabilities[] array defines what this volume supports.
580 *
581 * The valid[] array defines which bits this code understands
582 * the meaning of (whether the volume has that capability or not).
583 * Any zero bits here means "I don't know what you're asking about"
584 * and the caller cannot tell whether that capability is
585 * present or not.
586 */
587 caps = valid = 0;
588 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SYMLINK_SUPPORT)) {
589 valid |= VOL_CAP_FMT_SYMBOLICLINKS;
590 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_SYMLINK)
591 caps |= VOL_CAP_FMT_SYMBOLICLINKS;
592 }
593 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_LINK_SUPPORT)) {
594 valid |= VOL_CAP_FMT_HARDLINKS;
595 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_LINK)
596 caps |= VOL_CAP_FMT_HARDLINKS;
597 }
598 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE)) {
599 valid |= VOL_CAP_FMT_CASE_SENSITIVE;
600 if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE))
601 caps |= VOL_CAP_FMT_CASE_SENSITIVE;
602 }
603 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING)) {
604 valid |= VOL_CAP_FMT_CASE_PRESERVING;
605 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_PRESERVING)
606 caps |= VOL_CAP_FMT_CASE_PRESERVING;
607 }
608 /* Note: VOL_CAP_FMT_2TB_FILESIZE is actually used to test for "large file support" */
609 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
610 /* Is server's max file size at least 4GB? */
611 if (nmp->nm_fsattr.nfsa_maxfilesize >= 0x100000000ULL)
612 caps |= VOL_CAP_FMT_2TB_FILESIZE;
613 } else if (nfsvers >= NFS_VER3) {
614 /*
615 * NFSv3 and up supports 64 bits of file size.
616 * So, we'll just assume maxfilesize >= 4GB
617 */
618 caps |= VOL_CAP_FMT_2TB_FILESIZE;
619 }
620 if (nfsvers >= NFS_VER4) {
621 caps |= VOL_CAP_FMT_HIDDEN_FILES;
622 valid |= VOL_CAP_FMT_HIDDEN_FILES;
623 // VOL_CAP_FMT_OPENDENYMODES
624 // caps |= VOL_CAP_FMT_OPENDENYMODES;
625 // valid |= VOL_CAP_FMT_OPENDENYMODES;
626 }
627 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
628 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
629 // VOL_CAP_FMT_SYMBOLICLINKS |
630 // VOL_CAP_FMT_HARDLINKS |
631 // VOL_CAP_FMT_JOURNAL |
632 // VOL_CAP_FMT_JOURNAL_ACTIVE |
633 // VOL_CAP_FMT_NO_ROOT_TIMES |
634 // VOL_CAP_FMT_SPARSE_FILES |
635 // VOL_CAP_FMT_ZERO_RUNS |
636 // VOL_CAP_FMT_CASE_SENSITIVE |
637 // VOL_CAP_FMT_CASE_PRESERVING |
638 // VOL_CAP_FMT_FAST_STATFS |
639 // VOL_CAP_FMT_2TB_FILESIZE |
640 // VOL_CAP_FMT_OPENDENYMODES |
641 // VOL_CAP_FMT_HIDDEN_FILES |
642 caps;
643 fsap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
644 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
645 // VOL_CAP_FMT_SYMBOLICLINKS |
646 // VOL_CAP_FMT_HARDLINKS |
647 // VOL_CAP_FMT_JOURNAL |
648 // VOL_CAP_FMT_JOURNAL_ACTIVE |
649 // VOL_CAP_FMT_NO_ROOT_TIMES |
650 // VOL_CAP_FMT_SPARSE_FILES |
651 // VOL_CAP_FMT_ZERO_RUNS |
652 // VOL_CAP_FMT_CASE_SENSITIVE |
653 // VOL_CAP_FMT_CASE_PRESERVING |
654 VOL_CAP_FMT_FAST_STATFS |
655 VOL_CAP_FMT_2TB_FILESIZE |
656 // VOL_CAP_FMT_OPENDENYMODES |
657 // VOL_CAP_FMT_HIDDEN_FILES |
658 valid;
659
660 /*
661 * We don't support most of the interfaces.
662 *
663 * We MAY support locking, but we don't have any easy way of probing.
664 * We can tell if there's no lockd running or if locks have been
665 * disabled for a mount, so we can definitely answer NO in that case.
666 * Any attempt to send a request to lockd to test for locking support
667 * may cause the lazily-launched locking daemons to be started
668 * unnecessarily. So we avoid that. However, we do record if we ever
669 * successfully perform a lock operation on a mount point, so if it
670 * looks like lock ops have worked, we do report that we support them.
671 */
672 caps = valid = 0;
673 if (nfsvers >= NFS_VER4) {
674 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
675 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
676 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)
677 caps |= VOL_CAP_INT_EXTENDED_SECURITY;
678 valid |= VOL_CAP_INT_EXTENDED_SECURITY;
679 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR)
680 caps |= VOL_CAP_INT_EXTENDED_ATTR;
681 valid |= VOL_CAP_INT_EXTENDED_ATTR;
682 #if NAMEDSTREAMS
683 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR)
684 caps |= VOL_CAP_INT_NAMEDSTREAMS;
685 valid |= VOL_CAP_INT_NAMEDSTREAMS;
686 #endif
687 } else if (nmp->nm_lockmode == NFS_LOCK_MODE_DISABLED) {
688 /* locks disabled on this mount, so they definitely won't work */
689 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
690 } else if (nmp->nm_state & NFSSTA_LOCKSWORK) {
691 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
692 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
693 }
694 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
695 // VOL_CAP_INT_SEARCHFS |
696 // VOL_CAP_INT_ATTRLIST |
697 // VOL_CAP_INT_NFSEXPORT |
698 // VOL_CAP_INT_READDIRATTR |
699 // VOL_CAP_INT_EXCHANGEDATA |
700 // VOL_CAP_INT_COPYFILE |
701 // VOL_CAP_INT_ALLOCATE |
702 // VOL_CAP_INT_VOL_RENAME |
703 // VOL_CAP_INT_ADVLOCK |
704 // VOL_CAP_INT_FLOCK |
705 // VOL_CAP_INT_EXTENDED_SECURITY |
706 // VOL_CAP_INT_USERACCESS |
707 // VOL_CAP_INT_MANLOCK |
708 // VOL_CAP_INT_NAMEDSTREAMS |
709 // VOL_CAP_INT_EXTENDED_ATTR |
710 VOL_CAP_INT_REMOTE_EVENT |
711 caps;
712 fsap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
713 VOL_CAP_INT_SEARCHFS |
714 VOL_CAP_INT_ATTRLIST |
715 VOL_CAP_INT_NFSEXPORT |
716 VOL_CAP_INT_READDIRATTR |
717 VOL_CAP_INT_EXCHANGEDATA |
718 VOL_CAP_INT_COPYFILE |
719 VOL_CAP_INT_ALLOCATE |
720 VOL_CAP_INT_VOL_RENAME |
721 // VOL_CAP_INT_ADVLOCK |
722 // VOL_CAP_INT_FLOCK |
723 // VOL_CAP_INT_EXTENDED_SECURITY |
724 // VOL_CAP_INT_USERACCESS |
725 // VOL_CAP_INT_MANLOCK |
726 // VOL_CAP_INT_NAMEDSTREAMS |
727 // VOL_CAP_INT_EXTENDED_ATTR |
728 VOL_CAP_INT_REMOTE_EVENT |
729 valid;
730
731 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
732 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
733
734 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
735 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
736
737 VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
738 lck_mtx_unlock(&nmp->nm_lock);
739 }
740
741 if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
742 fsap->f_attributes.validattr.commonattr = 0;
743 fsap->f_attributes.validattr.volattr =
744 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
745 fsap->f_attributes.validattr.dirattr = 0;
746 fsap->f_attributes.validattr.fileattr = 0;
747 fsap->f_attributes.validattr.forkattr = 0;
748
749 fsap->f_attributes.nativeattr.commonattr = 0;
750 fsap->f_attributes.nativeattr.volattr =
751 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
752 fsap->f_attributes.nativeattr.dirattr = 0;
753 fsap->f_attributes.nativeattr.fileattr = 0;
754 fsap->f_attributes.nativeattr.forkattr = 0;
755
756 VFSATTR_SET_SUPPORTED(fsap, f_attributes);
757 }
758
759 return (error);
760 }
761
762 /*
763 * nfs version 3 fsinfo rpc call
764 */
765 int
766 nfs3_fsinfo(struct nfsmount *nmp, nfsnode_t np, vfs_context_t ctx)
767 {
768 int error = 0, lockerror, status, nmlocked = 0;
769 u_int64_t xid;
770 uint32_t val, prefsize, maxsize;
771 struct nfsm_chain nmreq, nmrep;
772
773 nfsm_chain_null(&nmreq);
774 nfsm_chain_null(&nmrep);
775
776 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nmp->nm_vers));
777 nfsm_chain_add_fh(error, &nmreq, nmp->nm_vers, np->n_fhp, np->n_fhsize);
778 nfsm_chain_build_done(error, &nmreq);
779 nfsmout_if(error);
780 error = nfs_request(np, NULL, &nmreq, NFSPROC_FSINFO, ctx, NULL, &nmrep, &xid, &status);
781 if ((lockerror = nfs_node_lock(np)))
782 error = lockerror;
783 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
784 if (!lockerror)
785 nfs_node_unlock(np);
786 if (!error)
787 error = status;
788 nfsmout_if(error);
789
790 lck_mtx_lock(&nmp->nm_lock);
791 nmlocked = 1;
792
793 nfsm_chain_get_32(error, &nmrep, maxsize);
794 nfsm_chain_get_32(error, &nmrep, prefsize);
795 nfsmout_if(error);
796 nmp->nm_fsattr.nfsa_maxread = maxsize;
797 if (prefsize < nmp->nm_rsize)
798 nmp->nm_rsize = (prefsize + NFS_FABLKSIZE - 1) &
799 ~(NFS_FABLKSIZE - 1);
800 if ((maxsize > 0) && (maxsize < nmp->nm_rsize)) {
801 nmp->nm_rsize = maxsize & ~(NFS_FABLKSIZE - 1);
802 if (nmp->nm_rsize == 0)
803 nmp->nm_rsize = maxsize;
804 }
805 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip rtmult
806
807 nfsm_chain_get_32(error, &nmrep, maxsize);
808 nfsm_chain_get_32(error, &nmrep, prefsize);
809 nfsmout_if(error);
810 nmp->nm_fsattr.nfsa_maxwrite = maxsize;
811 if (prefsize < nmp->nm_wsize)
812 nmp->nm_wsize = (prefsize + NFS_FABLKSIZE - 1) &
813 ~(NFS_FABLKSIZE - 1);
814 if ((maxsize > 0) && (maxsize < nmp->nm_wsize)) {
815 nmp->nm_wsize = maxsize & ~(NFS_FABLKSIZE - 1);
816 if (nmp->nm_wsize == 0)
817 nmp->nm_wsize = maxsize;
818 }
819 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip wtmult
820
821 nfsm_chain_get_32(error, &nmrep, prefsize);
822 nfsmout_if(error);
823 if ((prefsize > 0) && (prefsize < nmp->nm_readdirsize))
824 nmp->nm_readdirsize = prefsize;
825 if ((nmp->nm_fsattr.nfsa_maxread > 0) &&
826 (nmp->nm_fsattr.nfsa_maxread < nmp->nm_readdirsize))
827 nmp->nm_readdirsize = nmp->nm_fsattr.nfsa_maxread;
828
829 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_maxfilesize);
830
831 nfsm_chain_adv(error, &nmrep, 2 * NFSX_UNSIGNED); // skip time_delta
832
833 /* convert FS properties to our own flags */
834 nfsm_chain_get_32(error, &nmrep, val);
835 nfsmout_if(error);
836 if (val & NFSV3FSINFO_LINK)
837 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_LINK;
838 if (val & NFSV3FSINFO_SYMLINK)
839 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_SYMLINK;
840 if (val & NFSV3FSINFO_HOMOGENEOUS)
841 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_HOMOGENEOUS;
842 if (val & NFSV3FSINFO_CANSETTIME)
843 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_SET_TIME;
844 nmp->nm_state |= NFSSTA_GOTFSINFO;
845 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXREAD);
846 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXWRITE);
847 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXFILESIZE);
848 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_LINK_SUPPORT);
849 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SYMLINK_SUPPORT);
850 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_HOMOGENEOUS);
851 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CANSETTIME);
852 nfsmout:
853 if (nmlocked)
854 lck_mtx_unlock(&nmp->nm_lock);
855 nfsm_chain_cleanup(&nmreq);
856 nfsm_chain_cleanup(&nmrep);
857 return (error);
858 }
859
860 /*
861 * Mount a remote root fs via. nfs. This depends on the info in the
862 * nfs_diskless structure that has been filled in properly by some primary
863 * bootstrap.
864 * It goes something like this:
865 * - do enough of "ifconfig" by calling ifioctl() so that the system
866 * can talk to the server
867 * - If nfs_diskless.mygateway is filled in, use that address as
868 * a default gateway.
869 * - hand craft the swap nfs vnode hanging off a fake mount point
870 * if swdevt[0].sw_dev == NODEV
871 * - build the rootfs mount point and call mountnfs() to do the rest.
872 */
873 int
874 nfs_mountroot(void)
875 {
876 struct nfs_diskless nd;
877 mount_t mp = NULL;
878 vnode_t vp = NULL;
879 vfs_context_t ctx;
880 int error;
881 #if !defined(NO_MOUNT_PRIVATE)
882 mount_t mppriv = NULL;
883 vnode_t vppriv = NULL;
884 #endif /* NO_MOUNT_PRIVATE */
885 int v3, sotype;
886
887 /*
888 * Call nfs_boot_init() to fill in the nfs_diskless struct.
889 * Note: networking must already have been configured before
890 * we're called.
891 */
892 bzero((caddr_t) &nd, sizeof(nd));
893 error = nfs_boot_init(&nd);
894 if (error)
895 panic("nfs_boot_init: unable to initialize NFS root system information, "
896 "error %d, check configuration: %s\n", error, PE_boot_args());
897
898 /*
899 * Try NFSv3 first, then fallback to NFSv2.
900 * Likewise, try TCP first, then fall back to UDP.
901 */
902 v3 = 1;
903 sotype = SOCK_STREAM;
904
905 tryagain:
906 error = nfs_boot_getfh(&nd, v3, sotype);
907 if (error) {
908 if (error == EHOSTDOWN || error == EHOSTUNREACH) {
909 if (nd.nd_root.ndm_mntfrom)
910 FREE_ZONE(nd.nd_root.ndm_mntfrom,
911 MAXPATHLEN, M_NAMEI);
912 if (nd.nd_root.ndm_path)
913 FREE_ZONE(nd.nd_root.ndm_path,
914 MAXPATHLEN, M_NAMEI);
915 if (nd.nd_private.ndm_mntfrom)
916 FREE_ZONE(nd.nd_private.ndm_mntfrom,
917 MAXPATHLEN, M_NAMEI);
918 if (nd.nd_private.ndm_path)
919 FREE_ZONE(nd.nd_private.ndm_path,
920 MAXPATHLEN, M_NAMEI);
921 return (error);
922 }
923 if (v3) {
924 if (sotype == SOCK_STREAM) {
925 printf("NFS mount (v3,TCP) failed with error %d, trying UDP...\n", error);
926 sotype = SOCK_DGRAM;
927 goto tryagain;
928 }
929 printf("NFS mount (v3,UDP) failed with error %d, trying v2...\n", error);
930 v3 = 0;
931 sotype = SOCK_STREAM;
932 goto tryagain;
933 } else if (sotype == SOCK_STREAM) {
934 printf("NFS mount (v2,TCP) failed with error %d, trying UDP...\n", error);
935 sotype = SOCK_DGRAM;
936 goto tryagain;
937 } else {
938 printf("NFS mount (v2,UDP) failed with error %d, giving up...\n", error);
939 }
940 switch(error) {
941 case EPROGUNAVAIL:
942 panic("NFS mount failed: NFS server mountd not responding, check server configuration: %s", PE_boot_args());
943 case EACCES:
944 case EPERM:
945 panic("NFS mount failed: NFS server refused mount, check server configuration: %s", PE_boot_args());
946 default:
947 panic("NFS mount failed with error %d, check configuration: %s", error, PE_boot_args());
948 }
949 }
950
951 ctx = vfs_context_kernel();
952
953 /*
954 * Create the root mount point.
955 */
956 #if !defined(NO_MOUNT_PRIVATE)
957 {
958 //PWC hack until we have a real "mount" tool to remount root rw
959 int rw_root=0;
960 int flags = MNT_ROOTFS|MNT_RDONLY;
961 PE_parse_boot_argn("-rwroot_hack", &rw_root, sizeof (rw_root));
962 if(rw_root)
963 {
964 flags = MNT_ROOTFS;
965 kprintf("-rwroot_hack in effect: mounting root fs read/write\n");
966 }
967
968 if ((error = nfs_mount_diskless(&nd.nd_root, "/", flags, &vp, &mp, ctx)))
969 #else
970 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_ROOTFS, &vp, &mp, ctx)))
971 #endif /* NO_MOUNT_PRIVATE */
972 {
973 if (v3) {
974 if (sotype == SOCK_STREAM) {
975 printf("NFS root mount (v3,TCP) failed with %d, trying UDP...\n", error);
976 sotype = SOCK_DGRAM;
977 goto tryagain;
978 }
979 printf("NFS root mount (v3,UDP) failed with %d, trying v2...\n", error);
980 v3 = 0;
981 sotype = SOCK_STREAM;
982 goto tryagain;
983 } else if (sotype == SOCK_STREAM) {
984 printf("NFS root mount (v2,TCP) failed with %d, trying UDP...\n", error);
985 sotype = SOCK_DGRAM;
986 goto tryagain;
987 } else {
988 printf("NFS root mount (v2,UDP) failed with error %d, giving up...\n", error);
989 }
990 panic("NFS root mount failed with error %d, check configuration: %s\n", error, PE_boot_args());
991 }
992 }
993 printf("root on %s\n", nd.nd_root.ndm_mntfrom);
994
995 vfs_unbusy(mp);
996 mount_list_add(mp);
997 rootvp = vp;
998
999 #if !defined(NO_MOUNT_PRIVATE)
1000 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
1001 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
1002 0, &vppriv, &mppriv, ctx);
1003 if (error)
1004 panic("NFS /private mount failed with error %d, check configuration: %s\n", error, PE_boot_args());
1005 printf("private on %s\n", nd.nd_private.ndm_mntfrom);
1006
1007 vfs_unbusy(mppriv);
1008 mount_list_add(mppriv);
1009 }
1010
1011 #endif /* NO_MOUNT_PRIVATE */
1012
1013 if (nd.nd_root.ndm_mntfrom)
1014 FREE_ZONE(nd.nd_root.ndm_mntfrom, MAXPATHLEN, M_NAMEI);
1015 if (nd.nd_root.ndm_path)
1016 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
1017 if (nd.nd_private.ndm_mntfrom)
1018 FREE_ZONE(nd.nd_private.ndm_mntfrom, MAXPATHLEN, M_NAMEI);
1019 if (nd.nd_private.ndm_path)
1020 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
1021
1022 /* Get root attributes (for the time). */
1023 error = nfs_getattr(VTONFS(vp), NULL, ctx, NGA_UNCACHED);
1024 if (error)
1025 panic("NFS mount: failed to get attributes for root directory, error %d, check server", error);
1026 return (0);
1027 }
1028
1029 /*
1030 * Internal version of mount system call for diskless setup.
1031 */
1032 static int
1033 nfs_mount_diskless(
1034 struct nfs_dlmount *ndmntp,
1035 const char *mntname,
1036 int mntflag,
1037 vnode_t *vpp,
1038 mount_t *mpp,
1039 vfs_context_t ctx)
1040 {
1041 mount_t mp;
1042 int error, numcomps;
1043 char *xdrbuf, *p, *cp, *frompath, *endserverp;
1044 char uaddr[MAX_IPv4_STR_LEN];
1045 struct xdrbuf xb;
1046 uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
1047 uint32_t mflags_mask[NFS_MFLAG_BITMAP_LEN];
1048 uint32_t mflags[NFS_MFLAG_BITMAP_LEN];
1049 uint32_t argslength_offset, attrslength_offset, end_offset;
1050
1051 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_mntfrom, &mp))) {
1052 printf("nfs_mount_diskless: NFS not configured\n");
1053 return (error);
1054 }
1055
1056 mp->mnt_flag |= mntflag;
1057 if (!(mntflag & MNT_RDONLY))
1058 mp->mnt_flag &= ~MNT_RDONLY;
1059
1060 /* find the server-side path being mounted */
1061 frompath = ndmntp->ndm_mntfrom;
1062 if (*frompath == '[') { /* skip IPv6 literal address */
1063 while (*frompath && (*frompath != ']'))
1064 frompath++;
1065 if (*frompath == ']')
1066 frompath++;
1067 }
1068 while (*frompath && (*frompath != ':'))
1069 frompath++;
1070 endserverp = frompath;
1071 while (*frompath && (*frompath == ':'))
1072 frompath++;
1073 /* count fs location path components */
1074 p = frompath;
1075 while (*p && (*p == '/'))
1076 p++;
1077 numcomps = 0;
1078 while (*p) {
1079 numcomps++;
1080 while (*p && (*p != '/'))
1081 p++;
1082 while (*p && (*p == '/'))
1083 p++;
1084 }
1085
1086 /* convert address to universal address string */
1087 if (inet_ntop(AF_INET, &ndmntp->ndm_saddr.sin_addr, uaddr, sizeof(uaddr)) != uaddr) {
1088 printf("nfs_mount_diskless: bad address\n");
1089 return (EINVAL);
1090 }
1091
1092 /* prepare mount attributes */
1093 NFS_BITMAP_ZERO(mattrs, NFS_MATTR_BITMAP_LEN);
1094 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_VERSION);
1095 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOCKET_TYPE);
1096 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_PORT);
1097 NFS_BITMAP_SET(mattrs, NFS_MATTR_FH);
1098 NFS_BITMAP_SET(mattrs, NFS_MATTR_FS_LOCATIONS);
1099 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFLAGS);
1100
1101 /* prepare mount flags */
1102 NFS_BITMAP_ZERO(mflags_mask, NFS_MFLAG_BITMAP_LEN);
1103 NFS_BITMAP_ZERO(mflags, NFS_MFLAG_BITMAP_LEN);
1104 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RESVPORT);
1105 NFS_BITMAP_SET(mflags, NFS_MFLAG_RESVPORT);
1106
1107 /* build xdr buffer */
1108 xb_init_buffer(&xb, NULL, 0);
1109 xb_add_32(error, &xb, NFS_ARGSVERSION_XDR);
1110 argslength_offset = xb_offset(&xb);
1111 xb_add_32(error, &xb, 0); // args length
1112 xb_add_32(error, &xb, NFS_XDRARGS_VERSION_0);
1113 xb_add_bitmap(error, &xb, mattrs, NFS_MATTR_BITMAP_LEN);
1114 attrslength_offset = xb_offset(&xb);
1115 xb_add_32(error, &xb, 0); // attrs length
1116 xb_add_32(error, &xb, ndmntp->ndm_nfsv3 ? 3 : 2); // NFS version
1117 xb_add_string(error, &xb, ((ndmntp->ndm_sotype == SOCK_DGRAM) ? "udp" : "tcp"), 3);
1118 xb_add_32(error, &xb, ntohs(ndmntp->ndm_saddr.sin_port)); // NFS port
1119 xb_add_fh(error, &xb, &ndmntp->ndm_fh[0], ndmntp->ndm_fhlen);
1120 /* fs location */
1121 xb_add_32(error, &xb, 1); /* fs location count */
1122 xb_add_32(error, &xb, 1); /* server count */
1123 xb_add_string(error, &xb, ndmntp->ndm_mntfrom, (endserverp - ndmntp->ndm_mntfrom)); /* server name */
1124 xb_add_32(error, &xb, 1); /* address count */
1125 xb_add_string(error, &xb, uaddr, strlen(uaddr)); /* address */
1126 xb_add_32(error, &xb, 0); /* empty server info */
1127 xb_add_32(error, &xb, numcomps); /* pathname component count */
1128 p = frompath;
1129 while (*p && (*p == '/'))
1130 p++;
1131 while (*p) {
1132 cp = p;
1133 while (*p && (*p != '/'))
1134 p++;
1135 xb_add_string(error, &xb, cp, (p - cp)); /* component */
1136 if (error)
1137 break;
1138 while (*p && (*p == '/'))
1139 p++;
1140 }
1141 xb_add_32(error, &xb, 0); /* empty fsl info */
1142 xb_add_32(error, &xb, mntflag); /* MNT flags */
1143 xb_build_done(error, &xb);
1144
1145 /* update opaque counts */
1146 end_offset = xb_offset(&xb);
1147 if (!error) {
1148 error = xb_seek(&xb, argslength_offset);
1149 xb_add_32(error, &xb, end_offset - argslength_offset + XDRWORD/*version*/);
1150 }
1151 if (!error) {
1152 error = xb_seek(&xb, attrslength_offset);
1153 xb_add_32(error, &xb, end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
1154 }
1155 if (error) {
1156 printf("nfs_mount_diskless: error %d assembling mount args\n", error);
1157 xb_cleanup(&xb);
1158 return (error);
1159 }
1160 /* grab the assembled buffer */
1161 xdrbuf = xb_buffer_base(&xb);
1162 xb.xb_flags &= ~XB_CLEANUP;
1163
1164 /* do the mount */
1165 if ((error = mountnfs(xdrbuf, mp, ctx, vpp))) {
1166 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
1167 // XXX vfs_rootmountfailed(mp);
1168 mount_list_lock();
1169 mp->mnt_vtable->vfc_refcount--;
1170 mount_list_unlock();
1171 vfs_unbusy(mp);
1172 mount_lock_destroy(mp);
1173 #if CONFIG_MACF
1174 mac_mount_label_destroy(mp);
1175 #endif
1176 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
1177 } else {
1178 *mpp = mp;
1179 }
1180 xb_cleanup(&xb);
1181 return (error);
1182 }
1183
1184 #if !defined(NO_MOUNT_PRIVATE)
1185 /*
1186 * Internal version of mount system call to mount "/private"
1187 * separately in diskless setup
1188 */
1189 static int
1190 nfs_mount_diskless_private(
1191 struct nfs_dlmount *ndmntp,
1192 const char *mntname,
1193 int mntflag,
1194 vnode_t *vpp,
1195 mount_t *mpp,
1196 vfs_context_t ctx)
1197 {
1198 mount_t mp;
1199 int error, numcomps;
1200 proc_t procp;
1201 struct vfstable *vfsp;
1202 struct nameidata nd;
1203 vnode_t vp;
1204 char *xdrbuf = NULL, *p, *cp, *frompath, *endserverp;
1205 char uaddr[MAX_IPv4_STR_LEN];
1206 struct xdrbuf xb;
1207 uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
1208 uint32_t mflags_mask[NFS_MFLAG_BITMAP_LEN], mflags[NFS_MFLAG_BITMAP_LEN];
1209 uint32_t argslength_offset, attrslength_offset, end_offset;
1210
1211 procp = current_proc(); /* XXX */
1212 xb_init(&xb, 0);
1213
1214 {
1215 /*
1216 * mimic main()!. Temporarily set up rootvnode and other stuff so
1217 * that namei works. Need to undo this because main() does it, too
1218 */
1219 struct filedesc *fdp; /* pointer to file descriptor state */
1220 fdp = procp->p_fd;
1221 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
1222
1223 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
1224 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, NULL))
1225 panic("cannot find root vnode");
1226 error = vnode_ref(rootvnode);
1227 if (error) {
1228 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
1229 goto out;
1230 }
1231 fdp->fd_cdir = rootvnode;
1232 fdp->fd_rdir = NULL;
1233 }
1234
1235 /*
1236 * Get vnode to be covered
1237 */
1238 NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
1239 CAST_USER_ADDR_T(mntname), ctx);
1240 if ((error = namei(&nd))) {
1241 printf("nfs_mountroot: private namei failed!\n");
1242 goto out;
1243 }
1244 {
1245 /* undo vnode_ref() in mimic main()! */
1246 vnode_rele(rootvnode);
1247 }
1248 nameidone(&nd);
1249 vp = nd.ni_vp;
1250
1251 if ((error = VNOP_FSYNC(vp, MNT_WAIT, ctx)) ||
1252 (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
1253 vnode_put(vp);
1254 goto out;
1255 }
1256 if (vnode_vtype(vp) != VDIR) {
1257 vnode_put(vp);
1258 error = ENOTDIR;
1259 goto out;
1260 }
1261 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1262 if (!strncmp(vfsp->vfc_name, "nfs", sizeof(vfsp->vfc_name)))
1263 break;
1264 if (vfsp == NULL) {
1265 printf("nfs_mountroot: private NFS not configured\n");
1266 vnode_put(vp);
1267 error = ENODEV;
1268 goto out;
1269 }
1270 if (vnode_mountedhere(vp) != NULL) {
1271 vnode_put(vp);
1272 error = EBUSY;
1273 goto out;
1274 }
1275
1276 /*
1277 * Allocate and initialize the filesystem.
1278 */
1279 mp = _MALLOC_ZONE((u_int32_t)sizeof(struct mount), M_MOUNT, M_WAITOK);
1280 if (!mp) {
1281 printf("nfs_mountroot: unable to allocate mount structure\n");
1282 vnode_put(vp);
1283 error = ENOMEM;
1284 goto out;
1285 }
1286 bzero((char *)mp, sizeof(struct mount));
1287
1288 /* Initialize the default IO constraints */
1289 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
1290 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
1291 mp->mnt_ioflags = 0;
1292 mp->mnt_realrootvp = NULLVP;
1293 mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
1294
1295 mount_lock_init(mp);
1296 TAILQ_INIT(&mp->mnt_vnodelist);
1297 TAILQ_INIT(&mp->mnt_workerqueue);
1298 TAILQ_INIT(&mp->mnt_newvnodes);
1299 (void)vfs_busy(mp, LK_NOWAIT);
1300 TAILQ_INIT(&mp->mnt_vnodelist);
1301 mount_list_lock();
1302 vfsp->vfc_refcount++;
1303 mount_list_unlock();
1304 mp->mnt_vtable = vfsp;
1305 mp->mnt_op = vfsp->vfc_vfsops;
1306 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
1307 mp->mnt_flag = mntflag;
1308 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
1309 strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSNAMELEN-1);
1310 vp->v_mountedhere = mp;
1311 mp->mnt_vnodecovered = vp;
1312 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
1313 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
1314 (void) copystr(ndmntp->ndm_mntfrom, mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN - 1, 0);
1315 #if CONFIG_MACF
1316 mac_mount_label_init(mp);
1317 mac_mount_label_associate(ctx, mp);
1318 #endif
1319
1320 /* find the server-side path being mounted */
1321 frompath = ndmntp->ndm_mntfrom;
1322 if (*frompath == '[') { /* skip IPv6 literal address */
1323 while (*frompath && (*frompath != ']'))
1324 frompath++;
1325 if (*frompath == ']')
1326 frompath++;
1327 }
1328 while (*frompath && (*frompath != ':'))
1329 frompath++;
1330 endserverp = frompath;
1331 while (*frompath && (*frompath == ':'))
1332 frompath++;
1333 /* count fs location path components */
1334 p = frompath;
1335 while (*p && (*p == '/'))
1336 p++;
1337 numcomps = 0;
1338 while (*p) {
1339 numcomps++;
1340 while (*p && (*p != '/'))
1341 p++;
1342 while (*p && (*p == '/'))
1343 p++;
1344 }
1345
1346 /* convert address to universal address string */
1347 if (inet_ntop(AF_INET, &ndmntp->ndm_saddr.sin_addr, uaddr, sizeof(uaddr)) != uaddr) {
1348 printf("nfs_mountroot: bad address\n");
1349 error = EINVAL;
1350 goto out;
1351 }
1352
1353 /* prepare mount attributes */
1354 NFS_BITMAP_ZERO(mattrs, NFS_MATTR_BITMAP_LEN);
1355 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_VERSION);
1356 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOCKET_TYPE);
1357 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_PORT);
1358 NFS_BITMAP_SET(mattrs, NFS_MATTR_FH);
1359 NFS_BITMAP_SET(mattrs, NFS_MATTR_FS_LOCATIONS);
1360 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFLAGS);
1361
1362 /* prepare mount flags */
1363 NFS_BITMAP_ZERO(mflags_mask, NFS_MFLAG_BITMAP_LEN);
1364 NFS_BITMAP_ZERO(mflags, NFS_MFLAG_BITMAP_LEN);
1365 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RESVPORT);
1366 NFS_BITMAP_SET(mflags, NFS_MFLAG_RESVPORT);
1367
1368 /* build xdr buffer */
1369 xb_init_buffer(&xb, NULL, 0);
1370 xb_add_32(error, &xb, NFS_ARGSVERSION_XDR);
1371 argslength_offset = xb_offset(&xb);
1372 xb_add_32(error, &xb, 0); // args length
1373 xb_add_32(error, &xb, NFS_XDRARGS_VERSION_0);
1374 xb_add_bitmap(error, &xb, mattrs, NFS_MATTR_BITMAP_LEN);
1375 attrslength_offset = xb_offset(&xb);
1376 xb_add_32(error, &xb, 0); // attrs length
1377 xb_add_32(error, &xb, ndmntp->ndm_nfsv3 ? 3 : 2); // NFS version
1378 xb_add_string(error, &xb, ((ndmntp->ndm_sotype == SOCK_DGRAM) ? "udp" : "tcp"), 3);
1379 xb_add_32(error, &xb, ntohs(ndmntp->ndm_saddr.sin_port)); // NFS port
1380 xb_add_fh(error, &xb, &ndmntp->ndm_fh[0], ndmntp->ndm_fhlen);
1381 /* fs location */
1382 xb_add_32(error, &xb, 1); /* fs location count */
1383 xb_add_32(error, &xb, 1); /* server count */
1384 xb_add_string(error, &xb, ndmntp->ndm_mntfrom, (endserverp - ndmntp->ndm_mntfrom)); /* server name */
1385 xb_add_32(error, &xb, 1); /* address count */
1386 xb_add_string(error, &xb, uaddr, strlen(uaddr)); /* address */
1387 xb_add_32(error, &xb, 0); /* empty server info */
1388 xb_add_32(error, &xb, numcomps); /* pathname component count */
1389 p = frompath;
1390 while (*p && (*p == '/'))
1391 p++;
1392 while (*p) {
1393 cp = p;
1394 while (*p && (*p != '/'))
1395 p++;
1396 xb_add_string(error, &xb, cp, (p - cp)); /* component */
1397 if (error)
1398 break;
1399 while (*p && (*p == '/'))
1400 p++;
1401 }
1402 xb_add_32(error, &xb, 0); /* empty fsl info */
1403 xb_add_32(error, &xb, mntflag); /* MNT flags */
1404 xb_build_done(error, &xb);
1405
1406 /* update opaque counts */
1407 end_offset = xb_offset(&xb);
1408 if (!error) {
1409 error = xb_seek(&xb, argslength_offset);
1410 xb_add_32(error, &xb, end_offset - argslength_offset + XDRWORD/*version*/);
1411 }
1412 if (!error) {
1413 error = xb_seek(&xb, attrslength_offset);
1414 xb_add_32(error, &xb, end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
1415 }
1416 if (error) {
1417 printf("nfs_mountroot: error %d assembling mount args\n", error);
1418 goto out;
1419 }
1420 /* grab the assembled buffer */
1421 xdrbuf = xb_buffer_base(&xb);
1422 xb.xb_flags &= ~XB_CLEANUP;
1423
1424 /* do the mount */
1425 if ((error = mountnfs(xdrbuf, mp, ctx, &vp))) {
1426 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
1427 mount_list_lock();
1428 vfsp->vfc_refcount--;
1429 mount_list_unlock();
1430 vfs_unbusy(mp);
1431 mount_lock_destroy(mp);
1432 #if CONFIG_MACF
1433 mac_mount_label_destroy(mp);
1434 #endif
1435 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
1436 goto out;
1437 }
1438
1439 *mpp = mp;
1440 *vpp = vp;
1441 out:
1442 xb_cleanup(&xb);
1443 return (error);
1444 }
1445 #endif /* NO_MOUNT_PRIVATE */
1446
1447 /*
1448 * Convert old style NFS mount args to XDR.
1449 */
1450 static int
1451 nfs_convert_old_nfs_args(mount_t mp, user_addr_t data, vfs_context_t ctx, int argsversion, int inkernel, char **xdrbufp)
1452 {
1453 int error = 0, args64bit, argsize, numcomps;
1454 struct user_nfs_args args;
1455 struct nfs_args tempargs;
1456 caddr_t argsp;
1457 size_t len;
1458 u_char nfh[NFS4_FHSIZE];
1459 char *mntfrom, *endserverp, *frompath, *p, *cp;
1460 struct sockaddr_storage ss;
1461 void *sinaddr;
1462 char uaddr[MAX_IPv6_STR_LEN];
1463 uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
1464 uint32_t mflags_mask[NFS_MFLAG_BITMAP_LEN], mflags[NFS_MFLAG_BITMAP_LEN];
1465 uint32_t nfsvers, nfslockmode = 0, argslength_offset, attrslength_offset, end_offset;
1466 struct xdrbuf xb;
1467
1468 *xdrbufp = NULL;
1469
1470 /* allocate a temporary buffer for mntfrom */
1471 MALLOC_ZONE(mntfrom, char*, MAXPATHLEN, M_NAMEI, M_WAITOK);
1472 if (!mntfrom)
1473 return (ENOMEM);
1474
1475 args64bit = (inkernel || vfs_context_is64bit(ctx));
1476 argsp = args64bit ? (void*)&args : (void*)&tempargs;
1477
1478 argsize = args64bit ? sizeof(args) : sizeof(tempargs);
1479 switch (argsversion) {
1480 case 3:
1481 argsize -= NFS_ARGSVERSION4_INCSIZE;
1482 case 4:
1483 argsize -= NFS_ARGSVERSION5_INCSIZE;
1484 case 5:
1485 argsize -= NFS_ARGSVERSION6_INCSIZE;
1486 case 6:
1487 break;
1488 default:
1489 error = EPROGMISMATCH;
1490 goto nfsmout;
1491 }
1492
1493 /* read in the structure */
1494 if (inkernel)
1495 bcopy(CAST_DOWN(void *, data), argsp, argsize);
1496 else
1497 error = copyin(data, argsp, argsize);
1498 nfsmout_if(error);
1499
1500 if (!args64bit) {
1501 args.addrlen = tempargs.addrlen;
1502 args.sotype = tempargs.sotype;
1503 args.proto = tempargs.proto;
1504 args.fhsize = tempargs.fhsize;
1505 args.flags = tempargs.flags;
1506 args.wsize = tempargs.wsize;
1507 args.rsize = tempargs.rsize;
1508 args.readdirsize = tempargs.readdirsize;
1509 args.timeo = tempargs.timeo;
1510 args.retrans = tempargs.retrans;
1511 args.maxgrouplist = tempargs.maxgrouplist;
1512 args.readahead = tempargs.readahead;
1513 args.leaseterm = tempargs.leaseterm;
1514 args.deadthresh = tempargs.deadthresh;
1515 args.addr = CAST_USER_ADDR_T(tempargs.addr);
1516 args.fh = CAST_USER_ADDR_T(tempargs.fh);
1517 args.hostname = CAST_USER_ADDR_T(tempargs.hostname);
1518 if (args.version >= 4) {
1519 args.acregmin = tempargs.acregmin;
1520 args.acregmax = tempargs.acregmax;
1521 args.acdirmin = tempargs.acdirmin;
1522 args.acdirmax = tempargs.acdirmax;
1523 }
1524 if (args.version >= 5)
1525 args.auth = tempargs.auth;
1526 if (args.version >= 6)
1527 args.deadtimeout = tempargs.deadtimeout;
1528 }
1529
1530 if ((args.fhsize < 0) || (args.fhsize > NFS4_FHSIZE)) {
1531 error = EINVAL;
1532 goto nfsmout;
1533 }
1534 if (args.fhsize > 0) {
1535 if (inkernel)
1536 bcopy(CAST_DOWN(void *, args.fh), (caddr_t)nfh, args.fhsize);
1537 else
1538 error = copyin(args.fh, (caddr_t)nfh, args.fhsize);
1539 nfsmout_if(error);
1540 }
1541
1542 if (inkernel)
1543 error = copystr(CAST_DOWN(void *, args.hostname), mntfrom, MAXPATHLEN-1, &len);
1544 else
1545 error = copyinstr(args.hostname, mntfrom, MAXPATHLEN-1, &len);
1546 nfsmout_if(error);
1547 bzero(&mntfrom[len], MAXPATHLEN - len);
1548
1549 /* find the server-side path being mounted */
1550 frompath = mntfrom;
1551 if (*frompath == '[') { /* skip IPv6 literal address */
1552 while (*frompath && (*frompath != ']'))
1553 frompath++;
1554 if (*frompath == ']')
1555 frompath++;
1556 }
1557 while (*frompath && (*frompath != ':'))
1558 frompath++;
1559 endserverp = frompath;
1560 while (*frompath && (*frompath == ':'))
1561 frompath++;
1562 /* count fs location path components */
1563 p = frompath;
1564 while (*p && (*p == '/'))
1565 p++;
1566 numcomps = 0;
1567 while (*p) {
1568 numcomps++;
1569 while (*p && (*p != '/'))
1570 p++;
1571 while (*p && (*p == '/'))
1572 p++;
1573 }
1574
1575 /* copy socket address */
1576 if (inkernel)
1577 bcopy(CAST_DOWN(void *, args.addr), &ss, args.addrlen);
1578 else
1579 error = copyin(args.addr, &ss, args.addrlen);
1580 nfsmout_if(error);
1581 ss.ss_len = args.addrlen;
1582
1583 /* convert address to universal address string */
1584 if (ss.ss_family == AF_INET)
1585 sinaddr = &((struct sockaddr_in*)&ss)->sin_addr;
1586 else if (ss.ss_family == AF_INET6)
1587 sinaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
1588 else
1589 sinaddr = NULL;
1590 if (!sinaddr || (inet_ntop(ss.ss_family, sinaddr, uaddr, sizeof(uaddr)) != uaddr)) {
1591 error = EINVAL;
1592 goto nfsmout;
1593 }
1594
1595 /* prepare mount flags */
1596 NFS_BITMAP_ZERO(mflags_mask, NFS_MFLAG_BITMAP_LEN);
1597 NFS_BITMAP_ZERO(mflags, NFS_MFLAG_BITMAP_LEN);
1598 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_SOFT);
1599 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_INTR);
1600 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RESVPORT);
1601 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOCONNECT);
1602 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_DUMBTIMER);
1603 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_CALLUMNT);
1604 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RDIRPLUS);
1605 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NONEGNAMECACHE);
1606 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_MUTEJUKEBOX);
1607 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOQUOTA);
1608 if (args.flags & NFSMNT_SOFT)
1609 NFS_BITMAP_SET(mflags, NFS_MFLAG_SOFT);
1610 if (args.flags & NFSMNT_INT)
1611 NFS_BITMAP_SET(mflags, NFS_MFLAG_INTR);
1612 if (args.flags & NFSMNT_RESVPORT)
1613 NFS_BITMAP_SET(mflags, NFS_MFLAG_RESVPORT);
1614 if (args.flags & NFSMNT_NOCONN)
1615 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOCONNECT);
1616 if (args.flags & NFSMNT_DUMBTIMR)
1617 NFS_BITMAP_SET(mflags, NFS_MFLAG_DUMBTIMER);
1618 if (args.flags & NFSMNT_CALLUMNT)
1619 NFS_BITMAP_SET(mflags, NFS_MFLAG_CALLUMNT);
1620 if (args.flags & NFSMNT_RDIRPLUS)
1621 NFS_BITMAP_SET(mflags, NFS_MFLAG_RDIRPLUS);
1622 if (args.flags & NFSMNT_NONEGNAMECACHE)
1623 NFS_BITMAP_SET(mflags, NFS_MFLAG_NONEGNAMECACHE);
1624 if (args.flags & NFSMNT_MUTEJUKEBOX)
1625 NFS_BITMAP_SET(mflags, NFS_MFLAG_MUTEJUKEBOX);
1626 if (args.flags & NFSMNT_NOQUOTA)
1627 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOQUOTA);
1628
1629 /* prepare mount attributes */
1630 NFS_BITMAP_ZERO(mattrs, NFS_MATTR_BITMAP_LEN);
1631 NFS_BITMAP_SET(mattrs, NFS_MATTR_FLAGS);
1632 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_VERSION);
1633 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOCKET_TYPE);
1634 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_PORT);
1635 NFS_BITMAP_SET(mattrs, NFS_MATTR_FH);
1636 NFS_BITMAP_SET(mattrs, NFS_MATTR_FS_LOCATIONS);
1637 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFLAGS);
1638 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFROM);
1639 if (args.flags & NFSMNT_NFSV4)
1640 nfsvers = 4;
1641 else if (args.flags & NFSMNT_NFSV3)
1642 nfsvers = 3;
1643 else
1644 nfsvers = 2;
1645 if ((args.flags & NFSMNT_RSIZE) && (args.rsize > 0))
1646 NFS_BITMAP_SET(mattrs, NFS_MATTR_READ_SIZE);
1647 if ((args.flags & NFSMNT_WSIZE) && (args.wsize > 0))
1648 NFS_BITMAP_SET(mattrs, NFS_MATTR_WRITE_SIZE);
1649 if ((args.flags & NFSMNT_TIMEO) && (args.timeo > 0))
1650 NFS_BITMAP_SET(mattrs, NFS_MATTR_REQUEST_TIMEOUT);
1651 if ((args.flags & NFSMNT_RETRANS) && (args.retrans > 0))
1652 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT);
1653 if ((args.flags & NFSMNT_MAXGRPS) && (args.maxgrouplist > 0))
1654 NFS_BITMAP_SET(mattrs, NFS_MATTR_MAX_GROUP_LIST);
1655 if ((args.flags & NFSMNT_READAHEAD) && (args.readahead > 0))
1656 NFS_BITMAP_SET(mattrs, NFS_MATTR_READAHEAD);
1657 if ((args.flags & NFSMNT_READDIRSIZE) && (args.readdirsize > 0))
1658 NFS_BITMAP_SET(mattrs, NFS_MATTR_READDIR_SIZE);
1659 if ((args.flags & NFSMNT_NOLOCKS) ||
1660 (args.flags & NFSMNT_LOCALLOCKS)) {
1661 NFS_BITMAP_SET(mattrs, NFS_MATTR_LOCK_MODE);
1662 if (args.flags & NFSMNT_NOLOCKS)
1663 nfslockmode = NFS_LOCK_MODE_DISABLED;
1664 else if (args.flags & NFSMNT_LOCALLOCKS)
1665 nfslockmode = NFS_LOCK_MODE_LOCAL;
1666 else
1667 nfslockmode = NFS_LOCK_MODE_ENABLED;
1668 }
1669 if (args.version >= 4) {
1670 if ((args.flags & NFSMNT_ACREGMIN) && (args.acregmin > 0))
1671 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN);
1672 if ((args.flags & NFSMNT_ACREGMAX) && (args.acregmax > 0))
1673 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX);
1674 if ((args.flags & NFSMNT_ACDIRMIN) && (args.acdirmin > 0))
1675 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN);
1676 if ((args.flags & NFSMNT_ACDIRMAX) && (args.acdirmax > 0))
1677 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX);
1678 }
1679 if (args.version >= 5) {
1680 if ((args.flags & NFSMNT_SECFLAVOR) || (args.flags & NFSMNT_SECSYSOK))
1681 NFS_BITMAP_SET(mattrs, NFS_MATTR_SECURITY);
1682 }
1683 if (args.version >= 6) {
1684 if ((args.flags & NFSMNT_DEADTIMEOUT) && (args.deadtimeout > 0))
1685 NFS_BITMAP_SET(mattrs, NFS_MATTR_DEAD_TIMEOUT);
1686 }
1687
1688 /* build xdr buffer */
1689 xb_init_buffer(&xb, NULL, 0);
1690 xb_add_32(error, &xb, args.version);
1691 argslength_offset = xb_offset(&xb);
1692 xb_add_32(error, &xb, 0); // args length
1693 xb_add_32(error, &xb, NFS_XDRARGS_VERSION_0);
1694 xb_add_bitmap(error, &xb, mattrs, NFS_MATTR_BITMAP_LEN);
1695 attrslength_offset = xb_offset(&xb);
1696 xb_add_32(error, &xb, 0); // attrs length
1697 xb_add_bitmap(error, &xb, mflags_mask, NFS_MFLAG_BITMAP_LEN); /* mask */
1698 xb_add_bitmap(error, &xb, mflags, NFS_MFLAG_BITMAP_LEN); /* value */
1699 xb_add_32(error, &xb, nfsvers);
1700 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READ_SIZE))
1701 xb_add_32(error, &xb, args.rsize);
1702 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_WRITE_SIZE))
1703 xb_add_32(error, &xb, args.wsize);
1704 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READDIR_SIZE))
1705 xb_add_32(error, &xb, args.readdirsize);
1706 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READAHEAD))
1707 xb_add_32(error, &xb, args.readahead);
1708 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN)) {
1709 xb_add_32(error, &xb, args.acregmin);
1710 xb_add_32(error, &xb, 0);
1711 }
1712 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX)) {
1713 xb_add_32(error, &xb, args.acregmax);
1714 xb_add_32(error, &xb, 0);
1715 }
1716 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN)) {
1717 xb_add_32(error, &xb, args.acdirmin);
1718 xb_add_32(error, &xb, 0);
1719 }
1720 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX)) {
1721 xb_add_32(error, &xb, args.acdirmax);
1722 xb_add_32(error, &xb, 0);
1723 }
1724 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_LOCK_MODE))
1725 xb_add_32(error, &xb, nfslockmode);
1726 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SECURITY)) {
1727 uint32_t flavors[2], i=0;
1728 if (args.flags & NFSMNT_SECFLAVOR)
1729 flavors[i++] = args.auth;
1730 if ((args.flags & NFSMNT_SECSYSOK) && ((i == 0) || (flavors[0] != RPCAUTH_SYS)))
1731 flavors[i++] = RPCAUTH_SYS;
1732 xb_add_word_array(error, &xb, flavors, i);
1733 }
1734 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MAX_GROUP_LIST))
1735 xb_add_32(error, &xb, args.maxgrouplist);
1736 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOCKET_TYPE))
1737 xb_add_string(error, &xb, ((args.sotype == SOCK_DGRAM) ? "udp" : "tcp"), 3);
1738 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_PORT))
1739 xb_add_32(error, &xb, ((ss.ss_family == AF_INET) ?
1740 ntohs(((struct sockaddr_in*)&ss)->sin_port) :
1741 ntohs(((struct sockaddr_in6*)&ss)->sin6_port)));
1742 /* NFS_MATTR_MOUNT_PORT (not available in old args) */
1743 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_REQUEST_TIMEOUT)) {
1744 /* convert from .1s increments to time */
1745 xb_add_32(error, &xb, args.timeo/10);
1746 xb_add_32(error, &xb, (args.timeo%10)*100000000);
1747 }
1748 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT))
1749 xb_add_32(error, &xb, args.retrans);
1750 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_DEAD_TIMEOUT)) {
1751 xb_add_32(error, &xb, args.deadtimeout);
1752 xb_add_32(error, &xb, 0);
1753 }
1754 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FH))
1755 xb_add_fh(error, &xb, &nfh[0], args.fhsize);
1756 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FS_LOCATIONS)) {
1757 xb_add_32(error, &xb, 1); /* fs location count */
1758 xb_add_32(error, &xb, 1); /* server count */
1759 xb_add_string(error, &xb, mntfrom, (endserverp - mntfrom)); /* server name */
1760 xb_add_32(error, &xb, 1); /* address count */
1761 xb_add_string(error, &xb, uaddr, strlen(uaddr)); /* address */
1762 xb_add_32(error, &xb, 0); /* empty server info */
1763 xb_add_32(error, &xb, numcomps); /* pathname component count */
1764 nfsmout_if(error);
1765 p = frompath;
1766 while (*p && (*p == '/'))
1767 p++;
1768 while (*p) {
1769 cp = p;
1770 while (*p && (*p != '/'))
1771 p++;
1772 xb_add_string(error, &xb, cp, (p - cp)); /* component */
1773 nfsmout_if(error);
1774 while (*p && (*p == '/'))
1775 p++;
1776 }
1777 xb_add_32(error, &xb, 0); /* empty fsl info */
1778 }
1779 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFLAGS))
1780 xb_add_32(error, &xb, (vfs_flags(mp) & MNT_VISFLAGMASK)); /* VFS MNT_* flags */
1781 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFROM))
1782 xb_add_string(error, &xb, mntfrom, strlen(mntfrom)); /* fixed f_mntfromname */
1783 xb_build_done(error, &xb);
1784
1785 /* update opaque counts */
1786 end_offset = xb_offset(&xb);
1787 error = xb_seek(&xb, argslength_offset);
1788 xb_add_32(error, &xb, end_offset - argslength_offset + XDRWORD/*version*/);
1789 nfsmout_if(error);
1790 error = xb_seek(&xb, attrslength_offset);
1791 xb_add_32(error, &xb, end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
1792
1793 if (!error) {
1794 /* grab the assembled buffer */
1795 *xdrbufp = xb_buffer_base(&xb);
1796 xb.xb_flags &= ~XB_CLEANUP;
1797 }
1798 nfsmout:
1799 xb_cleanup(&xb);
1800 FREE_ZONE(mntfrom, MAXPATHLEN, M_NAMEI);
1801 return (error);
1802 }
1803
1804 /*
1805 * VFS Operations.
1806 *
1807 * mount system call
1808 */
1809 int
1810 nfs_vfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t ctx)
1811 {
1812 int error = 0, inkernel = vfs_iskernelmount(mp);
1813 uint32_t argsversion, argslength;
1814 char *xdrbuf = NULL;
1815
1816 /* read in version */
1817 if (inkernel)
1818 bcopy(CAST_DOWN(void *, data), &argsversion, sizeof(argsversion));
1819 else if ((error = copyin(data, &argsversion, sizeof(argsversion))))
1820 return (error);
1821
1822 /* If we have XDR args, then all values in the buffer are in network order */
1823 if (argsversion == htonl(NFS_ARGSVERSION_XDR))
1824 argsversion = NFS_ARGSVERSION_XDR;
1825
1826 switch (argsversion) {
1827 case 3:
1828 case 4:
1829 case 5:
1830 case 6:
1831 /* convert old-style args to xdr */
1832 error = nfs_convert_old_nfs_args(mp, data, ctx, argsversion, inkernel, &xdrbuf);
1833 break;
1834 case NFS_ARGSVERSION_XDR:
1835 /* copy in xdr buffer */
1836 if (inkernel)
1837 bcopy(CAST_DOWN(void *, (data + XDRWORD)), &argslength, XDRWORD);
1838 else
1839 error = copyin((data + XDRWORD), &argslength, XDRWORD);
1840 if (error)
1841 break;
1842 argslength = ntohl(argslength);
1843 /* put a reasonable limit on the size of the XDR args */
1844 if (argslength > 16*1024) {
1845 error = E2BIG;
1846 break;
1847 }
1848 /* allocate xdr buffer */
1849 xdrbuf = xb_malloc(xdr_rndup(argslength));
1850 if (!xdrbuf) {
1851 error = ENOMEM;
1852 break;
1853 }
1854 if (inkernel)
1855 bcopy(CAST_DOWN(void *, data), xdrbuf, argslength);
1856 else
1857 error = copyin(data, xdrbuf, argslength);
1858 break;
1859 default:
1860 error = EPROGMISMATCH;
1861 }
1862
1863 if (error) {
1864 if (xdrbuf)
1865 xb_free(xdrbuf);
1866 return (error);
1867 }
1868 error = mountnfs(xdrbuf, mp, ctx, &vp);
1869 return (error);
1870 }
1871
1872 /*
1873 * Common code for mount and mountroot
1874 */
1875
1876 /* Set up an NFSv2/v3 mount */
1877 int
1878 nfs3_mount(
1879 struct nfsmount *nmp,
1880 vfs_context_t ctx,
1881 nfsnode_t *npp)
1882 {
1883 int error = 0;
1884 struct nfs_vattr nvattr;
1885 u_int64_t xid;
1886
1887 *npp = NULL;
1888
1889 if (!nmp->nm_fh)
1890 return (EINVAL);
1891
1892 /*
1893 * Get file attributes for the mountpoint. These are needed
1894 * in order to properly create the root vnode.
1895 */
1896 error = nfs3_getattr_rpc(NULL, nmp->nm_mountp, nmp->nm_fh->fh_data, nmp->nm_fh->fh_len, 0,
1897 ctx, &nvattr, &xid);
1898 if (error)
1899 goto out;
1900
1901 error = nfs_nget(nmp->nm_mountp, NULL, NULL, nmp->nm_fh->fh_data, nmp->nm_fh->fh_len,
1902 &nvattr, &xid, RPCAUTH_UNKNOWN, NG_MARKROOT, npp);
1903 if (*npp)
1904 nfs_node_unlock(*npp);
1905 if (error)
1906 goto out;
1907
1908 /*
1909 * Try to make sure we have all the general info from the server.
1910 */
1911 if (nmp->nm_vers == NFS_VER2) {
1912 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME);
1913 nmp->nm_fsattr.nfsa_maxname = NFS_MAXNAMLEN;
1914 } else if (nmp->nm_vers == NFS_VER3) {
1915 /* get the NFSv3 FSINFO */
1916 error = nfs3_fsinfo(nmp, *npp, ctx);
1917 if (error)
1918 goto out;
1919 /* If the server indicates all pathconf info is */
1920 /* the same, grab a copy of that info now */
1921 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_HOMOGENEOUS) &&
1922 (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) {
1923 struct nfs_fsattr nfsa;
1924 if (!nfs3_pathconf_rpc(*npp, &nfsa, ctx)) {
1925 /* cache a copy of the results */
1926 lck_mtx_lock(&nmp->nm_lock);
1927 nfs3_pathconf_cache(nmp, &nfsa);
1928 lck_mtx_unlock(&nmp->nm_lock);
1929 }
1930 }
1931 }
1932 out:
1933 if (*npp && error) {
1934 vnode_put(NFSTOV(*npp));
1935 vnode_recycle(NFSTOV(*npp));
1936 *npp = NULL;
1937 }
1938 return (error);
1939 }
1940
1941 /*
1942 * Update an NFSv4 mount path with the contents of the symlink.
1943 *
1944 * Read the link for the given file handle.
1945 * Insert the link's components into the path.
1946 */
1947 int
1948 nfs4_mount_update_path_with_symlink(struct nfsmount *nmp, struct nfs_fs_path *nfsp, uint32_t curcomp, fhandle_t *dirfhp, int *depthp, fhandle_t *fhp, vfs_context_t ctx)
1949 {
1950 int error = 0, status, numops;
1951 uint32_t len = 0, comp, newcomp, linkcompcount;
1952 u_int64_t xid;
1953 struct nfsm_chain nmreq, nmrep;
1954 struct nfsreq rq, *req = &rq;
1955 struct nfsreq_secinfo_args si;
1956 char *link = NULL, *p, *q, ch;
1957 struct nfs_fs_path nfsp2;
1958
1959 bzero(&nfsp2, sizeof(nfsp2));
1960 if (dirfhp->fh_len)
1961 NFSREQ_SECINFO_SET(&si, NULL, dirfhp->fh_data, dirfhp->fh_len, nfsp->np_components[curcomp], 0);
1962 else
1963 NFSREQ_SECINFO_SET(&si, NULL, NULL, 0, nfsp->np_components[curcomp], 0);
1964 nfsm_chain_null(&nmreq);
1965 nfsm_chain_null(&nmrep);
1966
1967 MALLOC_ZONE(link, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1968 if (!link)
1969 error = ENOMEM;
1970
1971 // PUTFH, READLINK
1972 numops = 2;
1973 nfsm_chain_build_alloc_init(error, &nmreq, 12 * NFSX_UNSIGNED);
1974 nfsm_chain_add_compound_header(error, &nmreq, "readlink", numops);
1975 numops--;
1976 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
1977 nfsm_chain_add_fh(error, &nmreq, NFS_VER4, fhp->fh_data, fhp->fh_len);
1978 numops--;
1979 nfsm_chain_add_32(error, &nmreq, NFS_OP_READLINK);
1980 nfsm_chain_build_done(error, &nmreq);
1981 nfsm_assert(error, (numops == 0), EPROTO);
1982 nfsmout_if(error);
1983
1984 error = nfs_request_async(NULL, nmp->nm_mountp, &nmreq, NFSPROC4_COMPOUND,
1985 vfs_context_thread(ctx), vfs_context_ucred(ctx), &si, 0, NULL, &req);
1986 if (!error)
1987 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
1988
1989 nfsm_chain_skip_tag(error, &nmrep);
1990 nfsm_chain_get_32(error, &nmrep, numops);
1991 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
1992 nfsm_chain_op_check(error, &nmrep, NFS_OP_READLINK);
1993 nfsm_chain_get_32(error, &nmrep, len);
1994 nfsmout_if(error);
1995 if (len == 0)
1996 error = ENOENT;
1997 else if (len >= MAXPATHLEN)
1998 len = MAXPATHLEN - 1;
1999 nfsm_chain_get_opaque(error, &nmrep, len, link);
2000 nfsmout_if(error);
2001 /* make sure link string is terminated properly */
2002 link[len] = '\0';
2003
2004 /* count the number of components in link */
2005 p = link;
2006 while (*p && (*p == '/'))
2007 p++;
2008 linkcompcount = 0;
2009 while (*p) {
2010 linkcompcount++;
2011 while (*p && (*p != '/'))
2012 p++;
2013 while (*p && (*p == '/'))
2014 p++;
2015 }
2016
2017 /* free up used components */
2018 for (comp=0; comp <= curcomp; comp++) {
2019 if (nfsp->np_components[comp]) {
2020 FREE(nfsp->np_components[comp], M_TEMP);
2021 nfsp->np_components[comp] = NULL;
2022 }
2023 }
2024
2025 /* set up new path */
2026 nfsp2.np_compcount = nfsp->np_compcount - curcomp - 1 + linkcompcount;
2027 MALLOC(nfsp2.np_components, char **, nfsp2.np_compcount*sizeof(char*), M_TEMP, M_WAITOK|M_ZERO);
2028 if (!nfsp2.np_components) {
2029 error = ENOMEM;
2030 goto nfsmout;
2031 }
2032
2033 /* add link components */
2034 p = link;
2035 while (*p && (*p == '/'))
2036 p++;
2037 for (newcomp=0; newcomp < linkcompcount; newcomp++) {
2038 /* find end of component */
2039 q = p;
2040 while (*q && (*q != '/'))
2041 q++;
2042 MALLOC(nfsp2.np_components[newcomp], char *, q-p+1, M_TEMP, M_WAITOK|M_ZERO);
2043 if (!nfsp2.np_components[newcomp]) {
2044 error = ENOMEM;
2045 break;
2046 }
2047 ch = *q;
2048 *q = '\0';
2049 strlcpy(nfsp2.np_components[newcomp], p, q-p+1);
2050 *q = ch;
2051 p = q;
2052 while (*p && (*p == '/'))
2053 p++;
2054 }
2055 nfsmout_if(error);
2056
2057 /* add remaining components */
2058 for(comp = curcomp + 1; comp < nfsp->np_compcount; comp++,newcomp++) {
2059 nfsp2.np_components[newcomp] = nfsp->np_components[comp];
2060 nfsp->np_components[comp] = NULL;
2061 }
2062
2063 /* move new path into place */
2064 FREE(nfsp->np_components, M_TEMP);
2065 nfsp->np_components = nfsp2.np_components;
2066 nfsp->np_compcount = nfsp2.np_compcount;
2067 nfsp2.np_components = NULL;
2068
2069 /* for absolute link, let the caller now that the next dirfh is root */
2070 if (link[0] == '/') {
2071 dirfhp->fh_len = 0;
2072 *depthp = 0;
2073 }
2074 nfsmout:
2075 if (link)
2076 FREE_ZONE(link, MAXPATHLEN, M_NAMEI);
2077 if (nfsp2.np_components) {
2078 for (comp=0; comp < nfsp2.np_compcount; comp++)
2079 if (nfsp2.np_components[comp])
2080 FREE(nfsp2.np_components[comp], M_TEMP);
2081 FREE(nfsp2.np_components, M_TEMP);
2082 }
2083 nfsm_chain_cleanup(&nmreq);
2084 nfsm_chain_cleanup(&nmrep);
2085 return (error);
2086 }
2087
2088 /* Set up an NFSv4 mount */
2089 int
2090 nfs4_mount(
2091 struct nfsmount *nmp,
2092 vfs_context_t ctx,
2093 nfsnode_t *npp)
2094 {
2095 struct nfsm_chain nmreq, nmrep;
2096 int error = 0, numops, status, interval, isdotdot, loopcnt = 0, depth = 0;
2097 struct nfs_fs_path fspath, *nfsp, fspath2;
2098 uint32_t bitmap[NFS_ATTR_BITMAP_LEN], comp, comp2;
2099 fhandle_t fh, dirfh;
2100 struct nfs_vattr nvattr;
2101 u_int64_t xid;
2102 struct nfsreq rq, *req = &rq;
2103 struct nfsreq_secinfo_args si;
2104 struct nfs_sec sec;
2105 struct nfs_fs_locations nfsls;
2106
2107 *npp = NULL;
2108 fh.fh_len = dirfh.fh_len = 0;
2109 TAILQ_INIT(&nmp->nm_open_owners);
2110 TAILQ_INIT(&nmp->nm_delegations);
2111 TAILQ_INIT(&nmp->nm_dreturnq);
2112 nmp->nm_stategenid = 1;
2113 NVATTR_INIT(&nvattr);
2114 bzero(&nfsls, sizeof(nfsls));
2115 nfsm_chain_null(&nmreq);
2116 nfsm_chain_null(&nmrep);
2117
2118 /*
2119 * If no security flavors were specified we'll want to default to the server's
2120 * preferred flavor. For NFSv4.0 we need a file handle and name to get that via
2121 * SECINFO, so we'll do that on the last component of the server path we are
2122 * mounting. If we are mounting the server's root, we'll need to defer the
2123 * SECINFO call to the first successful LOOKUP request.
2124 */
2125 if (!nmp->nm_sec.count)
2126 nmp->nm_state |= NFSSTA_NEEDSECINFO;
2127
2128 /* make a copy of the current location's path */
2129 nfsp = &nmp->nm_locations.nl_locations[nmp->nm_locations.nl_current.nli_loc]->nl_path;
2130 bzero(&fspath, sizeof(fspath));
2131 fspath.np_compcount = nfsp->np_compcount;
2132 if (fspath.np_compcount > 0) {
2133 MALLOC(fspath.np_components, char **, fspath.np_compcount*sizeof(char*), M_TEMP, M_WAITOK|M_ZERO);
2134 if (!fspath.np_components) {
2135 error = ENOMEM;
2136 goto nfsmout;
2137 }
2138 for (comp=0; comp < nfsp->np_compcount; comp++) {
2139 int slen = strlen(nfsp->np_components[comp]);
2140 MALLOC(fspath.np_components[comp], char *, slen+1, M_TEMP, M_WAITOK|M_ZERO);
2141 if (!fspath.np_components[comp]) {
2142 error = ENOMEM;
2143 break;
2144 }
2145 strlcpy(fspath.np_components[comp], nfsp->np_components[comp], slen+1);
2146 }
2147 if (error)
2148 goto nfsmout;
2149 }
2150
2151 /* for mirror mounts, we can just use the file handle passed in */
2152 if (nmp->nm_fh) {
2153 dirfh.fh_len = nmp->nm_fh->fh_len;
2154 bcopy(nmp->nm_fh->fh_data, dirfh.fh_data, dirfh.fh_len);
2155 NFSREQ_SECINFO_SET(&si, NULL, dirfh.fh_data, dirfh.fh_len, NULL, 0);
2156 goto gotfh;
2157 }
2158
2159 /* otherwise, we need to get the fh for the directory we are mounting */
2160
2161 /* if no components, just get root */
2162 if (fspath.np_compcount == 0) {
2163 nocomponents:
2164 // PUTROOTFH + GETATTR(FH)
2165 NFSREQ_SECINFO_SET(&si, NULL, NULL, 0, NULL, 0);
2166 numops = 2;
2167 nfsm_chain_build_alloc_init(error, &nmreq, 9 * NFSX_UNSIGNED);
2168 nfsm_chain_add_compound_header(error, &nmreq, "mount", numops);
2169 numops--;
2170 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTROOTFH);
2171 numops--;
2172 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
2173 NFS_CLEAR_ATTRIBUTES(bitmap);
2174 NFS4_DEFAULT_ATTRIBUTES(bitmap);
2175 NFS_BITMAP_SET(bitmap, NFS_FATTR_FILEHANDLE);
2176 nfsm_chain_add_bitmap(error, &nmreq, bitmap, NFS_ATTR_BITMAP_LEN);
2177 nfsm_chain_build_done(error, &nmreq);
2178 nfsm_assert(error, (numops == 0), EPROTO);
2179 nfsmout_if(error);
2180 error = nfs_request_async(NULL, nmp->nm_mountp, &nmreq, NFSPROC4_COMPOUND,
2181 vfs_context_thread(ctx), vfs_context_ucred(ctx), &si, 0, NULL, &req);
2182 if (!error)
2183 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2184 nfsm_chain_skip_tag(error, &nmrep);
2185 nfsm_chain_get_32(error, &nmrep, numops);
2186 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTROOTFH);
2187 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
2188 nfsmout_if(error);
2189 NFS_CLEAR_ATTRIBUTES(nmp->nm_fsattr.nfsa_bitmap);
2190 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, &dirfh, NULL, NULL);
2191 if (!error && !NFS_BITMAP_ISSET(&nvattr.nva_bitmap, NFS_FATTR_FILEHANDLE)) {
2192 printf("nfs: mount didn't return filehandle?\n");
2193 error = EBADRPC;
2194 }
2195 nfsmout_if(error);
2196 nfsm_chain_cleanup(&nmrep);
2197 nfsm_chain_null(&nmreq);
2198 NVATTR_CLEANUP(&nvattr);
2199 goto gotfh;
2200 }
2201
2202 /* look up each path component */
2203 for (comp=0; comp < fspath.np_compcount; ) {
2204 isdotdot = 0;
2205 if (fspath.np_components[comp][0] == '.') {
2206 if (fspath.np_components[comp][1] == '\0') {
2207 /* skip "." */
2208 comp++;
2209 continue;
2210 }
2211 /* treat ".." specially */
2212 if ((fspath.np_components[comp][1] == '.') &&
2213 (fspath.np_components[comp][2] == '\0'))
2214 isdotdot = 1;
2215 if (isdotdot && (dirfh.fh_len == 0)) {
2216 /* ".." in root directory is same as "." */
2217 comp++;
2218 continue;
2219 }
2220 }
2221 // PUT(ROOT)FH + LOOKUP(P) + GETFH + GETATTR
2222 if (dirfh.fh_len == 0)
2223 NFSREQ_SECINFO_SET(&si, NULL, NULL, 0, isdotdot ? NULL : fspath.np_components[comp], 0);
2224 else
2225 NFSREQ_SECINFO_SET(&si, NULL, dirfh.fh_data, dirfh.fh_len, isdotdot ? NULL : fspath.np_components[comp], 0);
2226 numops = 4;
2227 nfsm_chain_build_alloc_init(error, &nmreq, 18 * NFSX_UNSIGNED);
2228 nfsm_chain_add_compound_header(error, &nmreq, "mount", numops);
2229 numops--;
2230 if (dirfh.fh_len) {
2231 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
2232 nfsm_chain_add_fh(error, &nmreq, NFS_VER4, dirfh.fh_data, dirfh.fh_len);
2233 } else {
2234 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTROOTFH);
2235 }
2236 numops--;
2237 if (isdotdot) {
2238 nfsm_chain_add_32(error, &nmreq, NFS_OP_LOOKUPP);
2239 } else {
2240 nfsm_chain_add_32(error, &nmreq, NFS_OP_LOOKUP);
2241 nfsm_chain_add_name(error, &nmreq,
2242 fspath.np_components[comp], strlen(fspath.np_components[comp]), nmp);
2243 }
2244 numops--;
2245 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETFH);
2246 numops--;
2247 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
2248 NFS_CLEAR_ATTRIBUTES(bitmap);
2249 NFS4_DEFAULT_ATTRIBUTES(bitmap);
2250 /* if no namedattr support or component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2251 if (NMFLAG(nmp, NONAMEDATTR) || !strcmp(fspath.np_components[comp], ".zfs"))
2252 NFS_BITMAP_CLR(bitmap, NFS_FATTR_NAMED_ATTR);
2253 nfsm_chain_add_bitmap(error, &nmreq, bitmap, NFS_ATTR_BITMAP_LEN);
2254 nfsm_chain_build_done(error, &nmreq);
2255 nfsm_assert(error, (numops == 0), EPROTO);
2256 nfsmout_if(error);
2257 error = nfs_request_async(NULL, nmp->nm_mountp, &nmreq, NFSPROC4_COMPOUND,
2258 vfs_context_thread(ctx), vfs_context_ucred(ctx), &si, 0, NULL, &req);
2259 if (!error)
2260 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2261 nfsm_chain_skip_tag(error, &nmrep);
2262 nfsm_chain_get_32(error, &nmrep, numops);
2263 nfsm_chain_op_check(error, &nmrep, dirfh.fh_len ? NFS_OP_PUTFH : NFS_OP_PUTROOTFH);
2264 nfsm_chain_op_check(error, &nmrep, isdotdot ? NFS_OP_LOOKUPP : NFS_OP_LOOKUP);
2265 nfsmout_if(error);
2266 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETFH);
2267 nfsm_chain_get_32(error, &nmrep, fh.fh_len);
2268 nfsm_chain_get_opaque(error, &nmrep, fh.fh_len, fh.fh_data);
2269 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
2270 if (!error) {
2271 NFS_CLEAR_ATTRIBUTES(nmp->nm_fsattr.nfsa_bitmap);
2272 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, NULL, NULL, &nfsls);
2273 }
2274 nfsm_chain_cleanup(&nmrep);
2275 nfsm_chain_null(&nmreq);
2276 if (error) {
2277 /* LOOKUP succeeded but GETATTR failed? This could be a referral. */
2278 /* Try the lookup again with a getattr for fs_locations. */
2279 nfs_fs_locations_cleanup(&nfsls);
2280 error = nfs4_get_fs_locations(nmp, NULL, dirfh.fh_data, dirfh.fh_len, fspath.np_components[comp], ctx, &nfsls);
2281 if (!error && (nfsls.nl_numlocs < 1))
2282 error = ENOENT;
2283 nfsmout_if(error);
2284 if (++loopcnt > MAXSYMLINKS) {
2285 /* too many symlink/referral redirections */
2286 error = ELOOP;
2287 goto nfsmout;
2288 }
2289 /* tear down the current connection */
2290 nfs_disconnect(nmp);
2291 /* replace fs locations */
2292 nfs_fs_locations_cleanup(&nmp->nm_locations);
2293 nmp->nm_locations = nfsls;
2294 bzero(&nfsls, sizeof(nfsls));
2295 /* initiate a connection using the new fs locations */
2296 error = nfs_mount_connect(nmp);
2297 if (!error && !(nmp->nm_locations.nl_current.nli_flags & NLI_VALID))
2298 error = EIO;
2299 nfsmout_if(error);
2300 /* add new server's remote path to beginning of our path and continue */
2301 nfsp = &nmp->nm_locations.nl_locations[nmp->nm_locations.nl_current.nli_loc]->nl_path;
2302 bzero(&fspath2, sizeof(fspath2));
2303 fspath2.np_compcount = (fspath.np_compcount - comp - 1) + nfsp->np_compcount;
2304 if (fspath2.np_compcount > 0) {
2305 MALLOC(fspath2.np_components, char **, fspath2.np_compcount*sizeof(char*), M_TEMP, M_WAITOK|M_ZERO);
2306 if (!fspath2.np_components) {
2307 error = ENOMEM;
2308 goto nfsmout;
2309 }
2310 for (comp2=0; comp2 < nfsp->np_compcount; comp2++) {
2311 int slen = strlen(nfsp->np_components[comp2]);
2312 MALLOC(fspath2.np_components[comp2], char *, slen+1, M_TEMP, M_WAITOK|M_ZERO);
2313 if (!fspath2.np_components[comp2]) {
2314 /* clean up fspath2, then error out */
2315 while (comp2 > 0) {
2316 comp2--;
2317 FREE(fspath2.np_components[comp2], M_TEMP);
2318 }
2319 FREE(fspath2.np_components, M_TEMP);
2320 error = ENOMEM;
2321 goto nfsmout;
2322 }
2323 strlcpy(fspath2.np_components[comp2], nfsp->np_components[comp2], slen+1);
2324 }
2325 if ((fspath.np_compcount - comp - 1) > 0)
2326 bcopy(&fspath.np_components[comp+1], &fspath2.np_components[nfsp->np_compcount], (fspath.np_compcount - comp - 1)*sizeof(char*));
2327 /* free up unused parts of old path (prior components and component array) */
2328 do {
2329 FREE(fspath.np_components[comp], M_TEMP);
2330 } while (comp-- > 0);
2331 FREE(fspath.np_components, M_TEMP);
2332 /* put new path in place */
2333 fspath = fspath2;
2334 }
2335 /* reset dirfh and component index */
2336 dirfh.fh_len = 0;
2337 comp = 0;
2338 NVATTR_CLEANUP(&nvattr);
2339 if (fspath.np_compcount == 0)
2340 goto nocomponents;
2341 continue;
2342 }
2343 nfsmout_if(error);
2344 /* if file handle is for a symlink, then update the path with the symlink contents */
2345 if (NFS_BITMAP_ISSET(&nvattr.nva_bitmap, NFS_FATTR_TYPE) && (nvattr.nva_type == VLNK)) {
2346 if (++loopcnt > MAXSYMLINKS)
2347 error = ELOOP;
2348 else
2349 error = nfs4_mount_update_path_with_symlink(nmp, &fspath, comp, &dirfh, &depth, &fh, ctx);
2350 nfsmout_if(error);
2351 /* directory file handle is either left the same or reset to root (if link was absolute) */
2352 /* path traversal starts at beginning of the path again */
2353 comp = 0;
2354 NVATTR_CLEANUP(&nvattr);
2355 nfs_fs_locations_cleanup(&nfsls);
2356 continue;
2357 }
2358 NVATTR_CLEANUP(&nvattr);
2359 nfs_fs_locations_cleanup(&nfsls);
2360 /* not a symlink... */
2361 if ((nmp->nm_state & NFSSTA_NEEDSECINFO) && (comp == (fspath.np_compcount-1)) && !isdotdot) {
2362 /* need to get SECINFO for the directory being mounted */
2363 if (dirfh.fh_len == 0)
2364 NFSREQ_SECINFO_SET(&si, NULL, NULL, 0, isdotdot ? NULL : fspath.np_components[comp], 0);
2365 else
2366 NFSREQ_SECINFO_SET(&si, NULL, dirfh.fh_data, dirfh.fh_len, isdotdot ? NULL : fspath.np_components[comp], 0);
2367 sec.count = NX_MAX_SEC_FLAVORS;
2368 error = nfs4_secinfo_rpc(nmp, &si, vfs_context_ucred(ctx), sec.flavors, &sec.count);
2369 /* [sigh] some implementations return "illegal" error for unsupported ops */
2370 if (error == NFSERR_OP_ILLEGAL)
2371 error = 0;
2372 nfsmout_if(error);
2373 /* set our default security flavor to the first in the list */
2374 if (sec.count)
2375 nmp->nm_auth = sec.flavors[0];
2376 nmp->nm_state &= ~NFSSTA_NEEDSECINFO;
2377 }
2378 /* advance directory file handle, component index, & update depth */
2379 dirfh = fh;
2380 comp++;
2381 if (!isdotdot) /* going down the hierarchy */
2382 depth++;
2383 else if (--depth <= 0) /* going up the hierarchy */
2384 dirfh.fh_len = 0; /* clear dirfh when we hit root */
2385 }
2386
2387 gotfh:
2388 /* get attrs for mount point root */
2389 numops = NMFLAG(nmp, NONAMEDATTR) ? 2 : 3; // PUTFH + GETATTR + OPENATTR
2390 nfsm_chain_build_alloc_init(error, &nmreq, 25 * NFSX_UNSIGNED);
2391 nfsm_chain_add_compound_header(error, &nmreq, "mount", numops);
2392 numops--;
2393 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
2394 nfsm_chain_add_fh(error, &nmreq, NFS_VER4, dirfh.fh_data, dirfh.fh_len);
2395 numops--;
2396 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
2397 NFS_CLEAR_ATTRIBUTES(bitmap);
2398 NFS4_DEFAULT_ATTRIBUTES(bitmap);
2399 /* if no namedattr support or last component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2400 if (NMFLAG(nmp, NONAMEDATTR) || ((fspath.np_compcount > 0) && !strcmp(fspath.np_components[fspath.np_compcount-1], ".zfs")))
2401 NFS_BITMAP_CLR(bitmap, NFS_FATTR_NAMED_ATTR);
2402 nfsm_chain_add_bitmap(error, &nmreq, bitmap, NFS_ATTR_BITMAP_LEN);
2403 if (!NMFLAG(nmp, NONAMEDATTR)) {
2404 numops--;
2405 nfsm_chain_add_32(error, &nmreq, NFS_OP_OPENATTR);
2406 nfsm_chain_add_32(error, &nmreq, 0);
2407 }
2408 nfsm_chain_build_done(error, &nmreq);
2409 nfsm_assert(error, (numops == 0), EPROTO);
2410 nfsmout_if(error);
2411 error = nfs_request_async(NULL, nmp->nm_mountp, &nmreq, NFSPROC4_COMPOUND,
2412 vfs_context_thread(ctx), vfs_context_ucred(ctx), &si, 0, NULL, &req);
2413 if (!error)
2414 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2415 nfsm_chain_skip_tag(error, &nmrep);
2416 nfsm_chain_get_32(error, &nmrep, numops);
2417 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
2418 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
2419 nfsmout_if(error);
2420 NFS_CLEAR_ATTRIBUTES(nmp->nm_fsattr.nfsa_bitmap);
2421 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, NULL, NULL, NULL);
2422 nfsmout_if(error);
2423 if (!NMFLAG(nmp, NONAMEDATTR)) {
2424 nfsm_chain_op_check(error, &nmrep, NFS_OP_OPENATTR);
2425 if (error == ENOENT)
2426 error = 0;
2427 /* [sigh] some implementations return "illegal" error for unsupported ops */
2428 if (error || !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_NAMED_ATTR)) {
2429 nmp->nm_fsattr.nfsa_flags &= ~NFS_FSFLAG_NAMED_ATTR;
2430 } else {
2431 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_NAMED_ATTR;
2432 }
2433 } else {
2434 nmp->nm_fsattr.nfsa_flags &= ~NFS_FSFLAG_NAMED_ATTR;
2435 }
2436 if (NMFLAG(nmp, NOACL)) /* make sure ACL support is turned off */
2437 nmp->nm_fsattr.nfsa_flags &= ~NFS_FSFLAG_ACL;
2438 if (NMFLAG(nmp, ACLONLY) && !(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL))
2439 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_ACLONLY);
2440 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_FH_EXPIRE_TYPE)) {
2441 uint32_t fhtype = ((nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_FHTYPE_MASK) >> NFS_FSFLAG_FHTYPE_SHIFT);
2442 if (fhtype != NFS_FH_PERSISTENT)
2443 printf("nfs: warning: non-persistent file handles! for %s\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname);
2444 }
2445
2446 /* make sure it's a directory */
2447 if (!NFS_BITMAP_ISSET(&nvattr.nva_bitmap, NFS_FATTR_TYPE) || (nvattr.nva_type != VDIR)) {
2448 error = ENOTDIR;
2449 goto nfsmout;
2450 }
2451
2452 /* save the NFS fsid */
2453 nmp->nm_fsid = nvattr.nva_fsid;
2454
2455 /* create the root node */
2456 error = nfs_nget(nmp->nm_mountp, NULL, NULL, dirfh.fh_data, dirfh.fh_len, &nvattr, &xid, rq.r_auth, NG_MARKROOT, npp);
2457 nfsmout_if(error);
2458
2459 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_ACL)
2460 vfs_setextendedsecurity(nmp->nm_mountp);
2461
2462 /* adjust I/O sizes to server limits */
2463 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXREAD) && (nmp->nm_fsattr.nfsa_maxread > 0)) {
2464 if (nmp->nm_fsattr.nfsa_maxread < (uint64_t)nmp->nm_rsize) {
2465 nmp->nm_rsize = nmp->nm_fsattr.nfsa_maxread & ~(NFS_FABLKSIZE - 1);
2466 if (nmp->nm_rsize == 0)
2467 nmp->nm_rsize = nmp->nm_fsattr.nfsa_maxread;
2468 }
2469 }
2470 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXWRITE) && (nmp->nm_fsattr.nfsa_maxwrite > 0)) {
2471 if (nmp->nm_fsattr.nfsa_maxwrite < (uint64_t)nmp->nm_wsize) {
2472 nmp->nm_wsize = nmp->nm_fsattr.nfsa_maxwrite & ~(NFS_FABLKSIZE - 1);
2473 if (nmp->nm_wsize == 0)
2474 nmp->nm_wsize = nmp->nm_fsattr.nfsa_maxwrite;
2475 }
2476 }
2477
2478 /* set up lease renew timer */
2479 nmp->nm_renew_timer = thread_call_allocate(nfs4_renew_timer, nmp);
2480 interval = nmp->nm_fsattr.nfsa_lease / 2;
2481 if (interval < 1)
2482 interval = 1;
2483 nfs_interval_timer_start(nmp->nm_renew_timer, interval * 1000);
2484
2485 nfsmout:
2486 if (fspath.np_components) {
2487 for (comp=0; comp < fspath.np_compcount; comp++)
2488 if (fspath.np_components[comp])
2489 FREE(fspath.np_components[comp], M_TEMP);
2490 FREE(fspath.np_components, M_TEMP);
2491 }
2492 NVATTR_CLEANUP(&nvattr);
2493 nfs_fs_locations_cleanup(&nfsls);
2494 if (*npp)
2495 nfs_node_unlock(*npp);
2496 nfsm_chain_cleanup(&nmreq);
2497 nfsm_chain_cleanup(&nmrep);
2498 return (error);
2499 }
2500
2501 /*
2502 * Thread to handle initial NFS mount connection.
2503 */
2504 void
2505 nfs_mount_connect_thread(void *arg, __unused wait_result_t wr)
2506 {
2507 struct nfsmount *nmp = arg;
2508 int error = 0, savederror = 0, slpflag = (NMFLAG(nmp, INTR) ? PCATCH : 0);
2509 int done = 0, timeo, tries, maxtries;
2510
2511 if (NM_OMFLAG(nmp, MNTQUICK)) {
2512 timeo = 8;
2513 maxtries = 1;
2514 } else {
2515 timeo = 30;
2516 maxtries = 2;
2517 }
2518
2519 for (tries = 0; tries < maxtries; tries++) {
2520 error = nfs_connect(nmp, 1, timeo);
2521 switch (error) {
2522 case ETIMEDOUT:
2523 case EAGAIN:
2524 case EPIPE:
2525 case EADDRNOTAVAIL:
2526 case ENETDOWN:
2527 case ENETUNREACH:
2528 case ENETRESET:
2529 case ECONNABORTED:
2530 case ECONNRESET:
2531 case EISCONN:
2532 case ENOTCONN:
2533 case ESHUTDOWN:
2534 case ECONNREFUSED:
2535 case EHOSTDOWN:
2536 case EHOSTUNREACH:
2537 /* just keep retrying on any of these errors */
2538 break;
2539 case 0:
2540 default:
2541 /* looks like we got an answer... */
2542 done = 1;
2543 break;
2544 }
2545
2546 /* save the best error */
2547 if (nfs_connect_error_class(error) >= nfs_connect_error_class(savederror))
2548 savederror = error;
2549 if (done) {
2550 error = savederror;
2551 break;
2552 }
2553
2554 /* pause before next attempt */
2555 if ((error = nfs_sigintr(nmp, NULL, current_thread(), 0)))
2556 break;
2557 error = tsleep(nmp, PSOCK|slpflag, "nfs_mount_connect_retry", 2*hz);
2558 if (error && (error != EWOULDBLOCK))
2559 break;
2560 error = savederror;
2561 }
2562
2563 /* update status of mount connect */
2564 lck_mtx_lock(&nmp->nm_lock);
2565 if (!nmp->nm_mounterror)
2566 nmp->nm_mounterror = error;
2567 nmp->nm_state &= ~NFSSTA_MOUNT_THREAD;
2568 lck_mtx_unlock(&nmp->nm_lock);
2569 wakeup(&nmp->nm_nss);
2570 }
2571
2572 int
2573 nfs_mount_connect(struct nfsmount *nmp)
2574 {
2575 int error = 0, slpflag;
2576 thread_t thd;
2577 struct timespec ts = { 2, 0 };
2578
2579 /*
2580 * Set up the socket. Perform initial search for a location/server/address to
2581 * connect to and negotiate any unspecified mount parameters. This work is
2582 * done on a kernel thread to satisfy reserved port usage needs.
2583 */
2584 slpflag = NMFLAG(nmp, INTR) ? PCATCH : 0;
2585 lck_mtx_lock(&nmp->nm_lock);
2586 /* set flag that the thread is running */
2587 nmp->nm_state |= NFSSTA_MOUNT_THREAD;
2588 if (kernel_thread_start(nfs_mount_connect_thread, nmp, &thd) != KERN_SUCCESS) {
2589 nmp->nm_state &= ~NFSSTA_MOUNT_THREAD;
2590 nmp->nm_mounterror = EIO;
2591 printf("nfs mount %s start socket connect thread failed\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname);
2592 } else {
2593 thread_deallocate(thd);
2594 }
2595
2596 /* wait until mount connect thread is finished/gone */
2597 while (nmp->nm_state & NFSSTA_MOUNT_THREAD) {
2598 error = msleep(&nmp->nm_nss, &nmp->nm_lock, slpflag|PSOCK, "nfsconnectthread", &ts);
2599 if ((error && (error != EWOULDBLOCK)) || ((error = nfs_sigintr(nmp, NULL, current_thread(), 1)))) {
2600 /* record error */
2601 if (!nmp->nm_mounterror)
2602 nmp->nm_mounterror = error;
2603 /* signal the thread that we are aborting */
2604 nmp->nm_sockflags |= NMSOCK_UNMOUNT;
2605 if (nmp->nm_nss)
2606 wakeup(nmp->nm_nss);
2607 /* and continue waiting on it to finish */
2608 slpflag = 0;
2609 }
2610 }
2611 lck_mtx_unlock(&nmp->nm_lock);
2612
2613 /* grab mount connect status */
2614 error = nmp->nm_mounterror;
2615
2616 return (error);
2617 }
2618
2619 /*
2620 * Common code to mount an NFS file system.
2621 */
2622 int
2623 mountnfs(
2624 char *xdrbuf,
2625 mount_t mp,
2626 vfs_context_t ctx,
2627 vnode_t *vpp)
2628 {
2629 struct nfsmount *nmp;
2630 nfsnode_t np;
2631 int error = 0;
2632 struct vfsstatfs *sbp;
2633 struct xdrbuf xb;
2634 uint32_t i, val, vers = 0, minorvers, maxio, iosize, len;
2635 uint32_t *mattrs;
2636 uint32_t *mflags_mask;
2637 uint32_t *mflags;
2638 uint32_t argslength, attrslength;
2639 struct nfs_location_index firstloc = { NLI_VALID, 0, 0, 0 };
2640
2641 /* make sure mbuf constants are set up */
2642 if (!nfs_mbuf_mhlen)
2643 nfs_mbuf_init();
2644
2645 if (vfs_flags(mp) & MNT_UPDATE) {
2646 nmp = VFSTONFS(mp);
2647 /* update paths, file handles, etc, here XXX */
2648 xb_free(xdrbuf);
2649 return (0);
2650 } else {
2651 /* allocate an NFS mount structure for this mount */
2652 MALLOC_ZONE(nmp, struct nfsmount *,
2653 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
2654 if (!nmp) {
2655 xb_free(xdrbuf);
2656 return (ENOMEM);
2657 }
2658 bzero((caddr_t)nmp, sizeof (struct nfsmount));
2659 lck_mtx_init(&nmp->nm_lock, nfs_mount_grp, LCK_ATTR_NULL);
2660 TAILQ_INIT(&nmp->nm_resendq);
2661 TAILQ_INIT(&nmp->nm_iodq);
2662 TAILQ_INIT(&nmp->nm_gsscl);
2663 LIST_INIT(&nmp->nm_monlist);
2664 vfs_setfsprivate(mp, nmp);
2665 vfs_getnewfsid(mp);
2666 nmp->nm_mountp = mp;
2667 vfs_setauthopaque(mp);
2668
2669 nfs_nhinit_finish();
2670
2671 nmp->nm_args = xdrbuf;
2672
2673 /* set up defaults */
2674 nmp->nm_vers = 0;
2675 nmp->nm_timeo = NFS_TIMEO;
2676 nmp->nm_retry = NFS_RETRANS;
2677 nmp->nm_sotype = 0;
2678 nmp->nm_sofamily = 0;
2679 nmp->nm_nfsport = 0;
2680 nmp->nm_wsize = NFS_WSIZE;
2681 nmp->nm_rsize = NFS_RSIZE;
2682 nmp->nm_readdirsize = NFS_READDIRSIZE;
2683 nmp->nm_numgrps = NFS_MAXGRPS;
2684 nmp->nm_readahead = NFS_DEFRAHEAD;
2685 nmp->nm_tprintf_delay = nfs_tprintf_delay;
2686 if (nmp->nm_tprintf_delay < 0)
2687 nmp->nm_tprintf_delay = 0;
2688 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
2689 if (nmp->nm_tprintf_initial_delay < 0)
2690 nmp->nm_tprintf_initial_delay = 0;
2691 nmp->nm_acregmin = NFS_MINATTRTIMO;
2692 nmp->nm_acregmax = NFS_MAXATTRTIMO;
2693 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
2694 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
2695 nmp->nm_auth = RPCAUTH_SYS;
2696 nmp->nm_deadtimeout = 0;
2697 NFS_BITMAP_SET(nmp->nm_flags, NFS_MFLAG_NOACL);
2698 }
2699
2700 mattrs = nmp->nm_mattrs;
2701 mflags = nmp->nm_mflags;
2702 mflags_mask = nmp->nm_mflags_mask;
2703
2704 /* set up NFS mount with args */
2705 xb_init_buffer(&xb, xdrbuf, 2*XDRWORD);
2706 xb_get_32(error, &xb, val); /* version */
2707 xb_get_32(error, &xb, argslength); /* args length */
2708 nfsmerr_if(error);
2709 xb_init_buffer(&xb, xdrbuf, argslength); /* restart parsing with actual buffer length */
2710 xb_get_32(error, &xb, val); /* version */
2711 xb_get_32(error, &xb, argslength); /* args length */
2712 xb_get_32(error, &xb, val); /* XDR args version */
2713 if (val != NFS_XDRARGS_VERSION_0)
2714 error = EINVAL;
2715 len = NFS_MATTR_BITMAP_LEN;
2716 xb_get_bitmap(error, &xb, mattrs, len); /* mount attribute bitmap */
2717 attrslength = 0;
2718 xb_get_32(error, &xb, attrslength); /* attrs length */
2719 if (!error && (attrslength > (argslength - ((4+NFS_MATTR_BITMAP_LEN+1)*XDRWORD))))
2720 error = EINVAL;
2721 nfsmerr_if(error);
2722 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FLAGS)) {
2723 len = NFS_MFLAG_BITMAP_LEN;
2724 xb_get_bitmap(error, &xb, mflags_mask, len); /* mount flag mask */
2725 len = NFS_MFLAG_BITMAP_LEN;
2726 xb_get_bitmap(error, &xb, mflags, len); /* mount flag values */
2727 if (!error) {
2728 /* clear all mask bits and OR in all the ones that are set */
2729 nmp->nm_flags[0] &= ~mflags_mask[0];
2730 nmp->nm_flags[0] |= (mflags_mask[0] & mflags[0]);
2731 }
2732 }
2733 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_VERSION)) {
2734 xb_get_32(error, &xb, vers);
2735 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_MINOR_VERSION))
2736 xb_get_32(error, &xb, minorvers);
2737 else
2738 minorvers = 0;
2739 nfsmerr_if(error);
2740 switch (vers) {
2741 case 2:
2742 nmp->nm_vers = NFS_VER2;
2743 break;
2744 case 3:
2745 nmp->nm_vers = NFS_VER3;
2746 break;
2747 case 4:
2748 switch (minorvers) {
2749 case 0:
2750 nmp->nm_vers = NFS_VER4;
2751 break;
2752 default:
2753 error = EINVAL;
2754 }
2755 break;
2756 default:
2757 error = EINVAL;
2758 }
2759 }
2760 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_MINOR_VERSION)) {
2761 /* should have also gotten NFS version (and already gotten minorvers) */
2762 if (!NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_VERSION))
2763 error = EINVAL;
2764 }
2765 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READ_SIZE))
2766 xb_get_32(error, &xb, nmp->nm_rsize);
2767 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_WRITE_SIZE))
2768 xb_get_32(error, &xb, nmp->nm_wsize);
2769 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READDIR_SIZE))
2770 xb_get_32(error, &xb, nmp->nm_readdirsize);
2771 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READAHEAD))
2772 xb_get_32(error, &xb, nmp->nm_readahead);
2773 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN)) {
2774 xb_get_32(error, &xb, nmp->nm_acregmin);
2775 xb_skip(error, &xb, XDRWORD);
2776 }
2777 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX)) {
2778 xb_get_32(error, &xb, nmp->nm_acregmax);
2779 xb_skip(error, &xb, XDRWORD);
2780 }
2781 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN)) {
2782 xb_get_32(error, &xb, nmp->nm_acdirmin);
2783 xb_skip(error, &xb, XDRWORD);
2784 }
2785 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX)) {
2786 xb_get_32(error, &xb, nmp->nm_acdirmax);
2787 xb_skip(error, &xb, XDRWORD);
2788 }
2789 nfsmerr_if(error);
2790 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_LOCK_MODE)) {
2791 xb_get_32(error, &xb, val);
2792 switch (val) {
2793 case NFS_LOCK_MODE_DISABLED:
2794 case NFS_LOCK_MODE_LOCAL:
2795 if (nmp->nm_vers >= NFS_VER4) {
2796 /* disabled/local lock mode only allowed on v2/v3 */
2797 error = EINVAL;
2798 break;
2799 }
2800 /* FALLTHROUGH */
2801 case NFS_LOCK_MODE_ENABLED:
2802 nmp->nm_lockmode = val;
2803 break;
2804 default:
2805 error = EINVAL;
2806 }
2807 }
2808 nfsmerr_if(error);
2809 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SECURITY)) {
2810 uint32_t seccnt;
2811 xb_get_32(error, &xb, seccnt);
2812 if (!error && ((seccnt < 1) || (seccnt > NX_MAX_SEC_FLAVORS)))
2813 error = EINVAL;
2814 nfsmerr_if(error);
2815 nmp->nm_sec.count = seccnt;
2816 for (i=0; i < seccnt; i++) {
2817 xb_get_32(error, &xb, nmp->nm_sec.flavors[i]);
2818 /* Check for valid security flavor */
2819 switch (nmp->nm_sec.flavors[i]) {
2820 case RPCAUTH_NONE:
2821 case RPCAUTH_SYS:
2822 case RPCAUTH_KRB5:
2823 case RPCAUTH_KRB5I:
2824 case RPCAUTH_KRB5P:
2825 break;
2826 default:
2827 error = EINVAL;
2828 }
2829 }
2830 /* start with the first flavor */
2831 nmp->nm_auth = nmp->nm_sec.flavors[0];
2832 }
2833 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MAX_GROUP_LIST))
2834 xb_get_32(error, &xb, nmp->nm_numgrps);
2835 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOCKET_TYPE)) {
2836 char sotype[6];
2837
2838 xb_get_32(error, &xb, val);
2839 if (!error && ((val < 3) || (val > 5)))
2840 error = EINVAL;
2841 nfsmerr_if(error);
2842 error = xb_get_bytes(&xb, sotype, val, 0);
2843 nfsmerr_if(error);
2844 sotype[val] = '\0';
2845 if (!strcmp(sotype, "tcp")) {
2846 nmp->nm_sotype = SOCK_STREAM;
2847 } else if (!strcmp(sotype, "udp")) {
2848 nmp->nm_sotype = SOCK_DGRAM;
2849 } else if (!strcmp(sotype, "tcp4")) {
2850 nmp->nm_sotype = SOCK_STREAM;
2851 nmp->nm_sofamily = AF_INET;
2852 } else if (!strcmp(sotype, "udp4")) {
2853 nmp->nm_sotype = SOCK_DGRAM;
2854 nmp->nm_sofamily = AF_INET;
2855 } else if (!strcmp(sotype, "tcp6")) {
2856 nmp->nm_sotype = SOCK_STREAM;
2857 nmp->nm_sofamily = AF_INET6;
2858 } else if (!strcmp(sotype, "udp6")) {
2859 nmp->nm_sotype = SOCK_DGRAM;
2860 nmp->nm_sofamily = AF_INET6;
2861 } else if (!strcmp(sotype, "inet4")) {
2862 nmp->nm_sofamily = AF_INET;
2863 } else if (!strcmp(sotype, "inet6")) {
2864 nmp->nm_sofamily = AF_INET6;
2865 } else if (!strcmp(sotype, "inet")) {
2866 nmp->nm_sofamily = 0; /* ok */
2867 } else {
2868 error = EINVAL;
2869 }
2870 if (!error && (nmp->nm_vers >= NFS_VER4) && nmp->nm_sotype &&
2871 (nmp->nm_sotype != SOCK_STREAM))
2872 error = EINVAL; /* NFSv4 is only allowed over TCP. */
2873 nfsmerr_if(error);
2874 }
2875 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_PORT))
2876 xb_get_32(error, &xb, nmp->nm_nfsport);
2877 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MOUNT_PORT))
2878 xb_get_32(error, &xb, nmp->nm_mountport);
2879 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_REQUEST_TIMEOUT)) {
2880 /* convert from time to 0.1s units */
2881 xb_get_32(error, &xb, nmp->nm_timeo);
2882 xb_get_32(error, &xb, val);
2883 nfsmerr_if(error);
2884 if (val >= 1000000000)
2885 error = EINVAL;
2886 nfsmerr_if(error);
2887 nmp->nm_timeo *= 10;
2888 nmp->nm_timeo += (val+100000000-1)/100000000;
2889 /* now convert to ticks */
2890 nmp->nm_timeo = (nmp->nm_timeo * NFS_HZ + 5) / 10;
2891 }
2892 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT)) {
2893 xb_get_32(error, &xb, val);
2894 if (!error && (val > 1))
2895 nmp->nm_retry = val;
2896 }
2897 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_DEAD_TIMEOUT)) {
2898 xb_get_32(error, &xb, nmp->nm_deadtimeout);
2899 xb_skip(error, &xb, XDRWORD);
2900 }
2901 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FH)) {
2902 nfsmerr_if(error);
2903 MALLOC(nmp->nm_fh, fhandle_t *, sizeof(fhandle_t), M_TEMP, M_WAITOK|M_ZERO);
2904 if (!nmp->nm_fh)
2905 error = ENOMEM;
2906 xb_get_32(error, &xb, nmp->nm_fh->fh_len);
2907 nfsmerr_if(error);
2908 error = xb_get_bytes(&xb, (char*)&nmp->nm_fh->fh_data[0], nmp->nm_fh->fh_len, 0);
2909 }
2910 nfsmerr_if(error);
2911 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FS_LOCATIONS)) {
2912 uint32_t loc, serv, addr, comp;
2913 struct nfs_fs_location *fsl;
2914 struct nfs_fs_server *fss;
2915 struct nfs_fs_path *fsp;
2916
2917 xb_get_32(error, &xb, nmp->nm_locations.nl_numlocs); /* fs location count */
2918 /* sanity check location count */
2919 if (!error && ((nmp->nm_locations.nl_numlocs < 1) || (nmp->nm_locations.nl_numlocs > 256)))
2920 error = EINVAL;
2921 nfsmerr_if(error);
2922 MALLOC(nmp->nm_locations.nl_locations, struct nfs_fs_location **, nmp->nm_locations.nl_numlocs * sizeof(struct nfs_fs_location*), M_TEMP, M_WAITOK|M_ZERO);
2923 if (!nmp->nm_locations.nl_locations)
2924 error = ENOMEM;
2925 for (loc = 0; loc < nmp->nm_locations.nl_numlocs; loc++) {
2926 nfsmerr_if(error);
2927 MALLOC(fsl, struct nfs_fs_location *, sizeof(struct nfs_fs_location), M_TEMP, M_WAITOK|M_ZERO);
2928 if (!fsl)
2929 error = ENOMEM;
2930 nmp->nm_locations.nl_locations[loc] = fsl;
2931 xb_get_32(error, &xb, fsl->nl_servcount); /* server count */
2932 /* sanity check server count */
2933 if (!error && ((fsl->nl_servcount < 1) || (fsl->nl_servcount > 256)))
2934 error = EINVAL;
2935 nfsmerr_if(error);
2936 MALLOC(fsl->nl_servers, struct nfs_fs_server **, fsl->nl_servcount * sizeof(struct nfs_fs_server*), M_TEMP, M_WAITOK|M_ZERO);
2937 if (!fsl->nl_servers)
2938 error = ENOMEM;
2939 for (serv = 0; serv < fsl->nl_servcount; serv++) {
2940 nfsmerr_if(error);
2941 MALLOC(fss, struct nfs_fs_server *, sizeof(struct nfs_fs_server), M_TEMP, M_WAITOK|M_ZERO);
2942 if (!fss)
2943 error = ENOMEM;
2944 fsl->nl_servers[serv] = fss;
2945 xb_get_32(error, &xb, val); /* server name length */
2946 /* sanity check server name length */
2947 if (!error && ((val < 1) || (val > MAXPATHLEN)))
2948 error = EINVAL;
2949 nfsmerr_if(error);
2950 MALLOC(fss->ns_name, char *, val+1, M_TEMP, M_WAITOK|M_ZERO);
2951 if (!fss->ns_name)
2952 error = ENOMEM;
2953 nfsmerr_if(error);
2954 error = xb_get_bytes(&xb, fss->ns_name, val, 0); /* server name */
2955 xb_get_32(error, &xb, fss->ns_addrcount); /* address count */
2956 /* sanity check address count (OK to be zero) */
2957 if (!error && (fss->ns_addrcount > 256))
2958 error = EINVAL;
2959 nfsmerr_if(error);
2960 if (fss->ns_addrcount > 0) {
2961 MALLOC(fss->ns_addresses, char **, fss->ns_addrcount * sizeof(char *), M_TEMP, M_WAITOK|M_ZERO);
2962 if (!fss->ns_addresses)
2963 error = ENOMEM;
2964 for (addr = 0; addr < fss->ns_addrcount; addr++) {
2965 xb_get_32(error, &xb, val); /* address length */
2966 /* sanity check address length */
2967 if (!error && ((val < 1) || (val > 128)))
2968 error = EINVAL;
2969 nfsmerr_if(error);
2970 MALLOC(fss->ns_addresses[addr], char *, val+1, M_TEMP, M_WAITOK|M_ZERO);
2971 if (!fss->ns_addresses[addr])
2972 error = ENOMEM;
2973 nfsmerr_if(error);
2974 error = xb_get_bytes(&xb, fss->ns_addresses[addr], val, 0); /* address */
2975 }
2976 }
2977 xb_get_32(error, &xb, val); /* server info length */
2978 xb_skip(error, &xb, val); /* skip server info */
2979 }
2980 /* get pathname */
2981 fsp = &fsl->nl_path;
2982 xb_get_32(error, &xb, fsp->np_compcount); /* component count */
2983 /* sanity check component count */
2984 if (!error && (fsp->np_compcount > MAXPATHLEN))
2985 error = EINVAL;
2986 nfsmerr_if(error);
2987 if (fsp->np_compcount) {
2988 MALLOC(fsp->np_components, char **, fsp->np_compcount * sizeof(char*), M_TEMP, M_WAITOK|M_ZERO);
2989 if (!fsp->np_components)
2990 error = ENOMEM;
2991 }
2992 for (comp = 0; comp < fsp->np_compcount; comp++) {
2993 xb_get_32(error, &xb, val); /* component length */
2994 /* sanity check component length */
2995 if (!error && (val == 0)) {
2996 /*
2997 * Apparently some people think a path with zero components should
2998 * be encoded with one zero-length component. So, just ignore any
2999 * zero length components.
3000 */
3001 comp--;
3002 fsp->np_compcount--;
3003 if (fsp->np_compcount == 0) {
3004 FREE(fsp->np_components, M_TEMP);
3005 fsp->np_components = NULL;
3006 }
3007 continue;
3008 }
3009 if (!error && ((val < 1) || (val > MAXPATHLEN)))
3010 error = EINVAL;
3011 nfsmerr_if(error);
3012 MALLOC(fsp->np_components[comp], char *, val+1, M_TEMP, M_WAITOK|M_ZERO);
3013 if (!fsp->np_components[comp])
3014 error = ENOMEM;
3015 nfsmerr_if(error);
3016 error = xb_get_bytes(&xb, fsp->np_components[comp], val, 0); /* component */
3017 }
3018 xb_get_32(error, &xb, val); /* fs location info length */
3019 xb_skip(error, &xb, val); /* skip fs location info */
3020 }
3021 }
3022 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFLAGS))
3023 xb_skip(error, &xb, XDRWORD);
3024 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFROM)) {
3025 xb_get_32(error, &xb, len);
3026 nfsmerr_if(error);
3027 val = len;
3028 if (val >= sizeof(vfs_statfs(mp)->f_mntfromname))
3029 val = sizeof(vfs_statfs(mp)->f_mntfromname) - 1;
3030 error = xb_get_bytes(&xb, vfs_statfs(mp)->f_mntfromname, val, 0);
3031 if ((len - val) > 0)
3032 xb_skip(error, &xb, len - val);
3033 nfsmerr_if(error);
3034 vfs_statfs(mp)->f_mntfromname[val] = '\0';
3035 }
3036 nfsmerr_if(error);
3037
3038 /*
3039 * Sanity check/finalize settings.
3040 */
3041
3042 if (nmp->nm_timeo < NFS_MINTIMEO)
3043 nmp->nm_timeo = NFS_MINTIMEO;
3044 else if (nmp->nm_timeo > NFS_MAXTIMEO)
3045 nmp->nm_timeo = NFS_MAXTIMEO;
3046 if (nmp->nm_retry > NFS_MAXREXMIT)
3047 nmp->nm_retry = NFS_MAXREXMIT;
3048
3049 if (nmp->nm_numgrps > NFS_MAXGRPS)
3050 nmp->nm_numgrps = NFS_MAXGRPS;
3051 if (nmp->nm_readahead > NFS_MAXRAHEAD)
3052 nmp->nm_readahead = NFS_MAXRAHEAD;
3053 if (nmp->nm_acregmin > nmp->nm_acregmax)
3054 nmp->nm_acregmin = nmp->nm_acregmax;
3055 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
3056 nmp->nm_acdirmin = nmp->nm_acdirmax;
3057
3058 /* need at least one fs location */
3059 if (nmp->nm_locations.nl_numlocs < 1)
3060 error = EINVAL;
3061 nfsmerr_if(error);
3062
3063 /* init mount's mntfromname to first location */
3064 if (!NM_OMATTR_GIVEN(nmp, MNTFROM))
3065 nfs_location_mntfromname(&nmp->nm_locations, firstloc,
3066 vfs_statfs(mp)->f_mntfromname, sizeof(vfs_statfs(mp)->f_mntfromname), 0);
3067
3068 /* Need to save the mounting credential for v4. */
3069 nmp->nm_mcred = vfs_context_ucred(ctx);
3070 if (IS_VALID_CRED(nmp->nm_mcred))
3071 kauth_cred_ref(nmp->nm_mcred);
3072
3073 /*
3074 * If a reserved port is required, check for that privilege.
3075 * (Note that mirror mounts are exempt because the privilege was
3076 * already checked for the original mount.)
3077 */
3078 if (NMFLAG(nmp, RESVPORT) && !vfs_iskernelmount(mp))
3079 error = priv_check_cred(nmp->nm_mcred, PRIV_NETINET_RESERVEDPORT, 0);
3080 nfsmerr_if(error);
3081
3082 /* do mount's initial socket connection */
3083 error = nfs_mount_connect(nmp);
3084 nfsmerr_if(error);
3085
3086 /* set up the version-specific function tables */
3087 if (nmp->nm_vers < NFS_VER4)
3088 nmp->nm_funcs = &nfs3_funcs;
3089 else
3090 nmp->nm_funcs = &nfs4_funcs;
3091
3092 /* sanity check settings now that version/connection is set */
3093 if (nmp->nm_vers == NFS_VER2) /* ignore RDIRPLUS on NFSv2 */
3094 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS);
3095 if (nmp->nm_vers >= NFS_VER4) {
3096 if (NFS_BITMAP_ISSET(nmp->nm_flags, NFS_MFLAG_ACLONLY)) /* aclonly trumps noacl */
3097 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_NOACL);
3098 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_CALLUMNT);
3099 if (nmp->nm_lockmode != NFS_LOCK_MODE_ENABLED)
3100 error = EINVAL; /* disabled/local lock mode only allowed on v2/v3 */
3101 } else {
3102 /* ignore these if not v4 */
3103 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_NOCALLBACK);
3104 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_NONAMEDATTR);
3105 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_NOACL);
3106 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_ACLONLY);
3107 if (IS_VALID_CRED(nmp->nm_mcred))
3108 kauth_cred_unref(&nmp->nm_mcred);
3109 }
3110 nfsmerr_if(error);
3111
3112 if (nmp->nm_sotype == SOCK_DGRAM) {
3113 /* I/O size defaults for UDP are different */
3114 if (!NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READ_SIZE))
3115 nmp->nm_rsize = NFS_DGRAM_RSIZE;
3116 if (!NFS_BITMAP_ISSET(mattrs, NFS_MATTR_WRITE_SIZE))
3117 nmp->nm_wsize = NFS_DGRAM_WSIZE;
3118 }
3119
3120 /* round down I/O sizes to multiple of NFS_FABLKSIZE */
3121 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
3122 if (nmp->nm_rsize <= 0)
3123 nmp->nm_rsize = NFS_FABLKSIZE;
3124 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
3125 if (nmp->nm_wsize <= 0)
3126 nmp->nm_wsize = NFS_FABLKSIZE;
3127
3128 /* and limit I/O sizes to maximum allowed */
3129 maxio = (nmp->nm_vers == NFS_VER2) ? NFS_V2MAXDATA :
3130 (nmp->nm_sotype == SOCK_DGRAM) ? NFS_MAXDGRAMDATA : NFS_MAXDATA;
3131 if (maxio > NFS_MAXBSIZE)
3132 maxio = NFS_MAXBSIZE;
3133 if (nmp->nm_rsize > maxio)
3134 nmp->nm_rsize = maxio;
3135 if (nmp->nm_wsize > maxio)
3136 nmp->nm_wsize = maxio;
3137
3138 if (nmp->nm_readdirsize > maxio)
3139 nmp->nm_readdirsize = maxio;
3140 if (nmp->nm_readdirsize > nmp->nm_rsize)
3141 nmp->nm_readdirsize = nmp->nm_rsize;
3142
3143 /* Set up the sockets and related info */
3144 if (nmp->nm_sotype == SOCK_DGRAM)
3145 TAILQ_INIT(&nmp->nm_cwndq);
3146
3147 /*
3148 * Get the root node/attributes from the NFS server and
3149 * do any basic, version-specific setup.
3150 */
3151 error = nmp->nm_funcs->nf_mount(nmp, ctx, &np);
3152 nfsmerr_if(error);
3153
3154 /*
3155 * A reference count is needed on the node representing the
3156 * remote root. If this object is not persistent, then backward
3157 * traversals of the mount point (i.e. "..") will not work if
3158 * the node gets flushed out of the cache.
3159 */
3160 nmp->nm_dnp = np;
3161 *vpp = NFSTOV(np);
3162 /* get usecount and drop iocount */
3163 error = vnode_ref(*vpp);
3164 vnode_put(*vpp);
3165 if (error) {
3166 vnode_recycle(*vpp);
3167 goto nfsmerr;
3168 }
3169
3170 /*
3171 * Do statfs to ensure static info gets set to reasonable values.
3172 */
3173 if ((error = nmp->nm_funcs->nf_update_statfs(nmp, ctx))) {
3174 int error2 = vnode_getwithref(*vpp);
3175 vnode_rele(*vpp);
3176 if (!error2)
3177 vnode_put(*vpp);
3178 vnode_recycle(*vpp);
3179 goto nfsmerr;
3180 }
3181 sbp = vfs_statfs(mp);
3182 sbp->f_bsize = nmp->nm_fsattr.nfsa_bsize;
3183 sbp->f_blocks = nmp->nm_fsattr.nfsa_space_total / sbp->f_bsize;
3184 sbp->f_bfree = nmp->nm_fsattr.nfsa_space_free / sbp->f_bsize;
3185 sbp->f_bavail = nmp->nm_fsattr.nfsa_space_avail / sbp->f_bsize;
3186 sbp->f_bused = (nmp->nm_fsattr.nfsa_space_total / sbp->f_bsize) -
3187 (nmp->nm_fsattr.nfsa_space_free / sbp->f_bsize);
3188 sbp->f_files = nmp->nm_fsattr.nfsa_files_total;
3189 sbp->f_ffree = nmp->nm_fsattr.nfsa_files_free;
3190 sbp->f_iosize = nfs_iosize;
3191
3192 /*
3193 * Calculate the size used for I/O buffers. Use the larger
3194 * of the two sizes to minimise NFS requests but make sure
3195 * that it is at least one VM page to avoid wasting buffer
3196 * space and to allow easy mmapping of I/O buffers.
3197 * The read/write RPC calls handle the splitting up of
3198 * buffers into multiple requests if the buffer size is
3199 * larger than the I/O size.
3200 */
3201 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
3202 if (iosize < PAGE_SIZE)
3203 iosize = PAGE_SIZE;
3204 nmp->nm_biosize = trunc_page_32(iosize);
3205
3206 /* For NFSv3 and greater, there is a (relatively) reliable ACCESS call. */
3207 if (nmp->nm_vers > NFS_VER2)
3208 vfs_setauthopaqueaccess(mp);
3209
3210 switch (nmp->nm_lockmode) {
3211 case NFS_LOCK_MODE_DISABLED:
3212 break;
3213 case NFS_LOCK_MODE_LOCAL:
3214 vfs_setlocklocal(nmp->nm_mountp);
3215 break;
3216 case NFS_LOCK_MODE_ENABLED:
3217 default:
3218 if (nmp->nm_vers <= NFS_VER3)
3219 nfs_lockd_mount_register(nmp);
3220 break;
3221 }
3222
3223 /* success! */
3224 lck_mtx_lock(&nmp->nm_lock);
3225 nmp->nm_state |= NFSSTA_MOUNTED;
3226 lck_mtx_unlock(&nmp->nm_lock);
3227 return (0);
3228 nfsmerr:
3229 nfs_mount_cleanup(nmp);
3230 return (error);
3231 }
3232
3233 #if CONFIG_TRIGGERS
3234
3235 /*
3236 * We've detected a file system boundary on the server and
3237 * need to mount a new file system so that our file systems
3238 * MIRROR the file systems on the server.
3239 *
3240 * Build the mount arguments for the new mount and call kernel_mount().
3241 */
3242 int
3243 nfs_mirror_mount_domount(vnode_t dvp, vnode_t vp, vfs_context_t ctx)
3244 {
3245 nfsnode_t np = VTONFS(vp);
3246 nfsnode_t dnp = VTONFS(dvp);
3247 struct nfsmount *nmp = NFSTONMP(np);
3248 char fstype[MFSTYPENAMELEN], *mntfromname = NULL, *path = NULL, *relpath, *p, *cp;
3249 int error = 0, pathbuflen = MAXPATHLEN, i, mntflags = 0, referral, skipcopy = 0;
3250 size_t nlen;
3251 struct xdrbuf xb, xbnew;
3252 uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
3253 uint32_t newmattrs[NFS_MATTR_BITMAP_LEN];
3254 uint32_t newmflags[NFS_MFLAG_BITMAP_LEN];
3255 uint32_t newmflags_mask[NFS_MFLAG_BITMAP_LEN];
3256 uint32_t argslength = 0, val, count, mlen, mlen2, rlen, relpathcomps;
3257 uint32_t argslength_offset, attrslength_offset, end_offset;
3258 uint32_t numlocs, loc, numserv, serv, numaddr, addr, numcomp, comp;
3259 char buf[XDRWORD];
3260 struct nfs_fs_locations nfsls;
3261
3262 referral = (np->n_vattr.nva_flags & NFS_FFLAG_TRIGGER_REFERRAL);
3263 if (referral)
3264 bzero(&nfsls, sizeof(nfsls));
3265
3266 xb_init(&xbnew, 0);
3267
3268 if (!nmp || (nmp->nm_state & NFSSTA_FORCE))
3269 return (ENXIO);
3270
3271 /* allocate a couple path buffers we need */
3272 MALLOC_ZONE(mntfromname, char *, pathbuflen, M_NAMEI, M_WAITOK);
3273 if (!mntfromname) {
3274 error = ENOMEM;
3275 goto nfsmerr;
3276 }
3277 MALLOC_ZONE(path, char *, pathbuflen, M_NAMEI, M_WAITOK);
3278 if (!path) {
3279 error = ENOMEM;
3280 goto nfsmerr;
3281 }
3282
3283 /* get the path for the directory being mounted on */
3284 error = vn_getpath(vp, path, &pathbuflen);
3285 if (error) {
3286 error = ENOMEM;
3287 goto nfsmerr;
3288 }
3289
3290 /*
3291 * Set up the mntfromname for the new mount based on the
3292 * current mount's mntfromname and the directory's path
3293 * relative to the current mount's mntonname.
3294 * Set up relpath to point at the relative path on the current mount.
3295 * Also, count the number of components in relpath.
3296 * We'll be adding those to each fs location path in the new args.
3297 */
3298 nlen = strlcpy(mntfromname, vfs_statfs(nmp->nm_mountp)->f_mntfromname, MAXPATHLEN);
3299 if ((nlen > 0) && (mntfromname[nlen-1] == '/')) { /* avoid double '/' in new name */
3300 mntfromname[nlen-1] = '\0';
3301 nlen--;
3302 }
3303 relpath = mntfromname + nlen;
3304 nlen = strlcat(mntfromname, path + strlen(vfs_statfs(nmp->nm_mountp)->f_mntonname), MAXPATHLEN);
3305 if (nlen >= MAXPATHLEN) {
3306 error = ENAMETOOLONG;
3307 goto nfsmerr;
3308 }
3309 /* count the number of components in relpath */
3310 p = relpath;
3311 while (*p && (*p == '/'))
3312 p++;
3313 relpathcomps = 0;
3314 while (*p) {
3315 relpathcomps++;
3316 while (*p && (*p != '/'))
3317 p++;
3318 while (*p && (*p == '/'))
3319 p++;
3320 }
3321
3322 /* grab a copy of the file system type */
3323 vfs_name(vnode_mount(vp), fstype);
3324
3325 /* for referrals, fetch the fs locations */
3326 if (referral) {
3327 const char *vname = vnode_getname(NFSTOV(np));
3328 if (!vname) {
3329 error = ENOENT;
3330 } else {
3331 error = nfs4_get_fs_locations(nmp, dnp, NULL, 0, vname, ctx, &nfsls);
3332 vnode_putname(vname);
3333 if (!error && (nfsls.nl_numlocs < 1))
3334 error = ENOENT;
3335 }
3336 nfsmerr_if(error);
3337 }
3338
3339 /* set up NFS mount args based on current mount args */
3340
3341 #define xb_copy_32(E, XBSRC, XBDST, V) \
3342 do { \
3343 if (E) break; \
3344 xb_get_32((E), (XBSRC), (V)); \
3345 if (skipcopy) break; \
3346 xb_add_32((E), (XBDST), (V)); \
3347 } while (0)
3348 #define xb_copy_opaque(E, XBSRC, XBDST) \
3349 do { \
3350 uint32_t __count, __val; \
3351 xb_copy_32((E), (XBSRC), (XBDST), __count); \
3352 if (E) break; \
3353 __count = nfsm_rndup(__count); \
3354 __count /= XDRWORD; \
3355 while (__count-- > 0) \
3356 xb_copy_32((E), (XBSRC), (XBDST), __val); \
3357 } while (0)
3358
3359 xb_init_buffer(&xb, nmp->nm_args, 2*XDRWORD);
3360 xb_get_32(error, &xb, val); /* version */
3361 xb_get_32(error, &xb, argslength); /* args length */
3362 xb_init_buffer(&xb, nmp->nm_args, argslength);
3363
3364 xb_init_buffer(&xbnew, NULL, 0);
3365 xb_copy_32(error, &xb, &xbnew, val); /* version */
3366 argslength_offset = xb_offset(&xbnew);
3367 xb_copy_32(error, &xb, &xbnew, val); /* args length */
3368 xb_copy_32(error, &xb, &xbnew, val); /* XDR args version */
3369 count = NFS_MATTR_BITMAP_LEN;
3370 xb_get_bitmap(error, &xb, mattrs, count); /* mount attribute bitmap */
3371 nfsmerr_if(error);
3372 for (i = 0; i < NFS_MATTR_BITMAP_LEN; i++)
3373 newmattrs[i] = mattrs[i];
3374 if (referral)
3375 NFS_BITMAP_SET(newmattrs, NFS_MATTR_FS_LOCATIONS);
3376 else
3377 NFS_BITMAP_SET(newmattrs, NFS_MATTR_FH);
3378 NFS_BITMAP_SET(newmattrs, NFS_MATTR_FLAGS);
3379 NFS_BITMAP_SET(newmattrs, NFS_MATTR_MNTFLAGS);
3380 NFS_BITMAP_CLR(newmattrs, NFS_MATTR_MNTFROM);
3381 xb_add_bitmap(error, &xbnew, newmattrs, NFS_MATTR_BITMAP_LEN);
3382 attrslength_offset = xb_offset(&xbnew);
3383 xb_copy_32(error, &xb, &xbnew, val); /* attrs length */
3384 NFS_BITMAP_ZERO(newmflags_mask, NFS_MFLAG_BITMAP_LEN);
3385 NFS_BITMAP_ZERO(newmflags, NFS_MFLAG_BITMAP_LEN);
3386 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FLAGS)) {
3387 count = NFS_MFLAG_BITMAP_LEN;
3388 xb_get_bitmap(error, &xb, newmflags_mask, count); /* mount flag mask bitmap */
3389 count = NFS_MFLAG_BITMAP_LEN;
3390 xb_get_bitmap(error, &xb, newmflags, count); /* mount flag bitmap */
3391 }
3392 NFS_BITMAP_SET(newmflags_mask, NFS_MFLAG_EPHEMERAL);
3393 NFS_BITMAP_SET(newmflags, NFS_MFLAG_EPHEMERAL);
3394 xb_add_bitmap(error, &xbnew, newmflags_mask, NFS_MFLAG_BITMAP_LEN);
3395 xb_add_bitmap(error, &xbnew, newmflags, NFS_MFLAG_BITMAP_LEN);
3396 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_VERSION))
3397 xb_copy_32(error, &xb, &xbnew, val);
3398 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_MINOR_VERSION))
3399 xb_copy_32(error, &xb, &xbnew, val);
3400 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READ_SIZE))
3401 xb_copy_32(error, &xb, &xbnew, val);
3402 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_WRITE_SIZE))
3403 xb_copy_32(error, &xb, &xbnew, val);
3404 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READDIR_SIZE))
3405 xb_copy_32(error, &xb, &xbnew, val);
3406 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_READAHEAD))
3407 xb_copy_32(error, &xb, &xbnew, val);
3408 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN)) {
3409 xb_copy_32(error, &xb, &xbnew, val);
3410 xb_copy_32(error, &xb, &xbnew, val);
3411 }
3412 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX)) {
3413 xb_copy_32(error, &xb, &xbnew, val);
3414 xb_copy_32(error, &xb, &xbnew, val);
3415 }
3416 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN)) {
3417 xb_copy_32(error, &xb, &xbnew, val);
3418 xb_copy_32(error, &xb, &xbnew, val);
3419 }
3420 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX)) {
3421 xb_copy_32(error, &xb, &xbnew, val);
3422 xb_copy_32(error, &xb, &xbnew, val);
3423 }
3424 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_LOCK_MODE))
3425 xb_copy_32(error, &xb, &xbnew, val);
3426 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SECURITY)) {
3427 xb_copy_32(error, &xb, &xbnew, count);
3428 while (!error && (count-- > 0))
3429 xb_copy_32(error, &xb, &xbnew, val);
3430 }
3431 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MAX_GROUP_LIST))
3432 xb_copy_32(error, &xb, &xbnew, val);
3433 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOCKET_TYPE))
3434 xb_copy_opaque(error, &xb, &xbnew);
3435 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_NFS_PORT))
3436 xb_copy_32(error, &xb, &xbnew, val);
3437 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MOUNT_PORT))
3438 xb_copy_32(error, &xb, &xbnew, val);
3439 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_REQUEST_TIMEOUT)) {
3440 xb_copy_32(error, &xb, &xbnew, val);
3441 xb_copy_32(error, &xb, &xbnew, val);
3442 }
3443 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT))
3444 xb_copy_32(error, &xb, &xbnew, val);
3445 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_DEAD_TIMEOUT)) {
3446 xb_copy_32(error, &xb, &xbnew, val);
3447 xb_copy_32(error, &xb, &xbnew, val);
3448 }
3449 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FH)) {
3450 xb_get_32(error, &xb, count);
3451 xb_skip(error, &xb, count);
3452 }
3453 if (!referral) {
3454 /* set the initial file handle to the directory's file handle */
3455 xb_add_fh(error, &xbnew, np->n_fhp, np->n_fhsize);
3456 }
3457 /* copy/extend/skip fs locations */
3458 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_FS_LOCATIONS)) {
3459 numlocs = numserv = numaddr = numcomp = 0;
3460 if (referral) /* don't copy the fs locations for a referral */
3461 skipcopy = 1;
3462 xb_copy_32(error, &xb, &xbnew, numlocs); /* location count */
3463 for (loc = 0; !error && (loc < numlocs); loc++) {
3464 xb_copy_32(error, &xb, &xbnew, numserv); /* server count */
3465 for (serv = 0; !error && (serv < numserv); serv++) {
3466 xb_copy_opaque(error, &xb, &xbnew); /* server name */
3467 xb_copy_32(error, &xb, &xbnew, numaddr); /* address count */
3468 for (addr = 0; !error && (addr < numaddr); addr++)
3469 xb_copy_opaque(error, &xb, &xbnew); /* address */
3470 xb_copy_opaque(error, &xb, &xbnew); /* server info */
3471 }
3472 /* pathname */
3473 xb_get_32(error, &xb, numcomp); /* component count */
3474 if (!skipcopy)
3475 xb_add_32(error, &xbnew, numcomp+relpathcomps); /* new component count */
3476 for (comp = 0; !error && (comp < numcomp); comp++)
3477 xb_copy_opaque(error, &xb, &xbnew); /* component */
3478 /* add additional components */
3479 for (comp = 0; !skipcopy && !error && (comp < relpathcomps); comp++) {
3480 p = relpath;
3481 while (*p && (*p == '/'))
3482 p++;
3483 while (*p && !error) {
3484 cp = p;
3485 while (*p && (*p != '/'))
3486 p++;
3487 xb_add_string(error, &xbnew, cp, (p - cp)); /* component */
3488 while (*p && (*p == '/'))
3489 p++;
3490 }
3491 }
3492 xb_copy_opaque(error, &xb, &xbnew); /* fs location info */
3493 }
3494 if (referral)
3495 skipcopy = 0;
3496 }
3497 if (referral) {
3498 /* add referral's fs locations */
3499 xb_add_32(error, &xbnew, nfsls.nl_numlocs); /* FS_LOCATIONS */
3500 for (loc = 0; !error && (loc < nfsls.nl_numlocs); loc++) {
3501 xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_servcount);
3502 for (serv = 0; !error && (serv < nfsls.nl_locations[loc]->nl_servcount); serv++) {
3503 xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_name,
3504 strlen(nfsls.nl_locations[loc]->nl_servers[serv]->ns_name));
3505 xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_addrcount);
3506 for (addr = 0; !error && (addr < nfsls.nl_locations[loc]->nl_servers[serv]->ns_addrcount); addr++)
3507 xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr],
3508 strlen(nfsls.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr]));
3509 xb_add_32(error, &xbnew, 0); /* empty server info */
3510 }
3511 xb_add_32(error, &xbnew, nfsls.nl_locations[loc]->nl_path.np_compcount);
3512 for (comp = 0; !error && (comp < nfsls.nl_locations[loc]->nl_path.np_compcount); comp++)
3513 xb_add_string(error, &xbnew, nfsls.nl_locations[loc]->nl_path.np_components[comp],
3514 strlen(nfsls.nl_locations[loc]->nl_path.np_components[comp]));
3515 xb_add_32(error, &xbnew, 0); /* empty fs location info */
3516 }
3517 }
3518 if (NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFLAGS))
3519 xb_get_32(error, &xb, mntflags);
3520 /*
3521 * We add the following mount flags to the ones for the mounted-on mount:
3522 * MNT_DONTBROWSE - to keep the mount from showing up as a separate volume
3523 * MNT_AUTOMOUNTED - to keep DiskArb from retriggering the mount after
3524 * an unmount (looking for /.autodiskmounted)
3525 */
3526 mntflags |= (MNT_AUTOMOUNTED | MNT_DONTBROWSE);
3527 xb_add_32(error, &xbnew, mntflags);
3528 if (!referral && NFS_BITMAP_ISSET(mattrs, NFS_MATTR_MNTFROM)) {
3529 /* copy mntfrom string and add relpath */
3530 rlen = strlen(relpath);
3531 xb_get_32(error, &xb, mlen);
3532 nfsmerr_if(error);
3533 mlen2 = mlen + ((relpath[0] != '/') ? 1 : 0) + rlen;
3534 xb_add_32(error, &xbnew, mlen2);
3535 count = mlen/XDRWORD;
3536 /* copy the original string */
3537 while (count-- > 0)
3538 xb_copy_32(error, &xb, &xbnew, val);
3539 if (!error && (mlen % XDRWORD)) {
3540 error = xb_get_bytes(&xb, buf, mlen%XDRWORD, 0);
3541 if (!error)
3542 error = xb_add_bytes(&xbnew, buf, mlen%XDRWORD, 1);
3543 }
3544 /* insert a '/' if the relative path doesn't start with one */
3545 if (!error && (relpath[0] != '/')) {
3546 buf[0] = '/';
3547 error = xb_add_bytes(&xbnew, buf, 1, 1);
3548 }
3549 /* add the additional relative path */
3550 if (!error)
3551 error = xb_add_bytes(&xbnew, relpath, rlen, 1);
3552 /* make sure the resulting string has the right number of pad bytes */
3553 if (!error && (mlen2 != nfsm_rndup(mlen2))) {
3554 bzero(buf, sizeof(buf));
3555 count = nfsm_rndup(mlen2) - mlen2;
3556 error = xb_add_bytes(&xbnew, buf, count, 1);
3557 }
3558 }
3559 xb_build_done(error, &xbnew);
3560
3561 /* update opaque counts */
3562 end_offset = xb_offset(&xbnew);
3563 if (!error) {
3564 error = xb_seek(&xbnew, argslength_offset);
3565 argslength = end_offset - argslength_offset + XDRWORD/*version*/;
3566 xb_add_32(error, &xbnew, argslength);
3567 }
3568 if (!error) {
3569 error = xb_seek(&xbnew, attrslength_offset);
3570 xb_add_32(error, &xbnew, end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
3571 }
3572 nfsmerr_if(error);
3573
3574 /*
3575 * For kernel_mount() call, use the existing mount flags (instead of the
3576 * original flags) because flags like MNT_NOSUID and MNT_NODEV may have
3577 * been silently enforced.
3578 */
3579 mntflags = vnode_vfsvisflags(vp);
3580 mntflags |= (MNT_AUTOMOUNTED | MNT_DONTBROWSE);
3581
3582 /* do the mount */
3583 error = kernel_mount(fstype, dvp, vp, path, xb_buffer_base(&xbnew), argslength,
3584 mntflags, KERNEL_MOUNT_PERMIT_UNMOUNT | KERNEL_MOUNT_NOAUTH, ctx);
3585
3586 nfsmerr:
3587 if (error)
3588 printf("nfs: mirror mount of %s on %s failed (%d)\n",
3589 mntfromname, path, error);
3590 /* clean up */
3591 xb_cleanup(&xbnew);
3592 if (referral)
3593 nfs_fs_locations_cleanup(&nfsls);
3594 if (path)
3595 FREE_ZONE(path, MAXPATHLEN, M_NAMEI);
3596 if (mntfromname)
3597 FREE_ZONE(mntfromname, MAXPATHLEN, M_NAMEI);
3598 if (!error)
3599 nfs_ephemeral_mount_harvester_start();
3600 return (error);
3601 }
3602
3603 /*
3604 * trigger vnode functions
3605 */
3606
3607 resolver_result_t
3608 nfs_mirror_mount_trigger_resolve(
3609 vnode_t vp,
3610 const struct componentname *cnp,
3611 enum path_operation pop,
3612 __unused int flags,
3613 __unused void *data,
3614 vfs_context_t ctx)
3615 {
3616 nfsnode_t np = VTONFS(vp);
3617 vnode_t pvp = NULLVP;
3618 int error = 0;
3619 resolver_result_t result;
3620
3621 /*
3622 * We have a trigger node that doesn't have anything mounted on it yet.
3623 * We'll do the mount if either:
3624 * (a) this isn't the last component of the path OR
3625 * (b) this is an op that looks like it should trigger the mount.
3626 */
3627 if (cnp->cn_flags & ISLASTCN) {
3628 switch (pop) {
3629 case OP_MOUNT:
3630 case OP_UNMOUNT:
3631 case OP_STATFS:
3632 case OP_LINK:
3633 case OP_UNLINK:
3634 case OP_RENAME:
3635 case OP_MKNOD:
3636 case OP_MKFIFO:
3637 case OP_SYMLINK:
3638 case OP_ACCESS:
3639 case OP_GETATTR:
3640 case OP_MKDIR:
3641 case OP_RMDIR:
3642 case OP_REVOKE:
3643 case OP_GETXATTR:
3644 case OP_LISTXATTR:
3645 /* don't perform the mount for these operations */
3646 result = vfs_resolver_result(np->n_trigseq, RESOLVER_NOCHANGE, 0);
3647 #ifdef NFS_TRIGGER_DEBUG
3648 NP(np, "nfs trigger RESOLVE: no change, last %d nameiop %d, seq %d",
3649 (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
3650 #endif
3651 return (result);
3652 case OP_OPEN:
3653 case OP_CHDIR:
3654 case OP_CHROOT:
3655 case OP_TRUNCATE:
3656 case OP_COPYFILE:
3657 case OP_PATHCONF:
3658 case OP_READLINK:
3659 case OP_SETATTR:
3660 case OP_EXCHANGEDATA:
3661 case OP_SEARCHFS:
3662 case OP_FSCTL:
3663 case OP_SETXATTR:
3664 case OP_REMOVEXATTR:
3665 default:
3666 /* go ahead and do the mount */
3667 break;
3668 }
3669 }
3670
3671 if (vnode_mountedhere(vp) != NULL) {
3672 /*
3673 * Um... there's already something mounted.
3674 * Been there. Done that. Let's just say it succeeded.
3675 */
3676 error = 0;
3677 goto skipmount;
3678 }
3679
3680 if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
3681 result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
3682 #ifdef NFS_TRIGGER_DEBUG
3683 NP(np, "nfs trigger RESOLVE: busy error %d, last %d nameiop %d, seq %d",
3684 error, (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
3685 #endif
3686 return (result);
3687 }
3688
3689 pvp = vnode_getparent(vp);
3690 if (pvp == NULLVP)
3691 error = EINVAL;
3692 if (!error)
3693 error = nfs_mirror_mount_domount(pvp, vp, ctx);
3694 skipmount:
3695 if (!error)
3696 np->n_trigseq++;
3697 result = vfs_resolver_result(np->n_trigseq, error ? RESOLVER_ERROR : RESOLVER_RESOLVED, error);
3698 #ifdef NFS_TRIGGER_DEBUG
3699 NP(np, "nfs trigger RESOLVE: %s %d, last %d nameiop %d, seq %d",
3700 error ? "error" : "resolved", error,
3701 (cnp->cn_flags & ISLASTCN) ? 1 : 0, cnp->cn_nameiop, np->n_trigseq);
3702 #endif
3703
3704 if (pvp != NULLVP)
3705 vnode_put(pvp);
3706 nfs_node_clear_busy(np);
3707 return (result);
3708 }
3709
3710 resolver_result_t
3711 nfs_mirror_mount_trigger_unresolve(
3712 vnode_t vp,
3713 int flags,
3714 __unused void *data,
3715 vfs_context_t ctx)
3716 {
3717 nfsnode_t np = VTONFS(vp);
3718 mount_t mp;
3719 int error;
3720 resolver_result_t result;
3721
3722 if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
3723 result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
3724 #ifdef NFS_TRIGGER_DEBUG
3725 NP(np, "nfs trigger UNRESOLVE: busy error %d, seq %d", error, np->n_trigseq);
3726 #endif
3727 return (result);
3728 }
3729
3730 mp = vnode_mountedhere(vp);
3731 if (!mp)
3732 error = EINVAL;
3733 if (!error)
3734 error = vfs_unmountbyfsid(&(vfs_statfs(mp)->f_fsid), flags, ctx);
3735 if (!error)
3736 np->n_trigseq++;
3737 result = vfs_resolver_result(np->n_trigseq, error ? RESOLVER_ERROR : RESOLVER_UNRESOLVED, error);
3738 #ifdef NFS_TRIGGER_DEBUG
3739 NP(np, "nfs trigger UNRESOLVE: %s %d, seq %d",
3740 error ? "error" : "unresolved", error, np->n_trigseq);
3741 #endif
3742 nfs_node_clear_busy(np);
3743 return (result);
3744 }
3745
3746 resolver_result_t
3747 nfs_mirror_mount_trigger_rearm(
3748 vnode_t vp,
3749 __unused int flags,
3750 __unused void *data,
3751 vfs_context_t ctx)
3752 {
3753 nfsnode_t np = VTONFS(vp);
3754 int error;
3755 resolver_result_t result;
3756
3757 if ((error = nfs_node_set_busy(np, vfs_context_thread(ctx)))) {
3758 result = vfs_resolver_result(np->n_trigseq, RESOLVER_ERROR, error);
3759 #ifdef NFS_TRIGGER_DEBUG
3760 NP(np, "nfs trigger REARM: busy error %d, seq %d", error, np->n_trigseq);
3761 #endif
3762 return (result);
3763 }
3764
3765 np->n_trigseq++;
3766 result = vfs_resolver_result(np->n_trigseq,
3767 vnode_mountedhere(vp) ? RESOLVER_RESOLVED : RESOLVER_UNRESOLVED, 0);
3768 #ifdef NFS_TRIGGER_DEBUG
3769 NP(np, "nfs trigger REARM: %s, seq %d",
3770 vnode_mountedhere(vp) ? "resolved" : "unresolved", np->n_trigseq);
3771 #endif
3772 nfs_node_clear_busy(np);
3773 return (result);
3774 }
3775
3776 /*
3777 * Periodically attempt to unmount ephemeral (mirror) mounts in an attempt to limit
3778 * the number of unused mounts.
3779 */
3780
3781 #define NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL 120 /* how often the harvester runs */
3782 struct nfs_ephemeral_mount_harvester_info {
3783 fsid_t fsid; /* FSID that we need to try to unmount */
3784 uint32_t mountcount; /* count of ephemeral mounts seen in scan */
3785 };
3786 /* various globals for the harvester */
3787 static thread_call_t nfs_ephemeral_mount_harvester_timer = NULL;
3788 static int nfs_ephemeral_mount_harvester_on = 0;
3789
3790 kern_return_t thread_terminate(thread_t);
3791
3792 static int
3793 nfs_ephemeral_mount_harvester_callback(mount_t mp, void *arg)
3794 {
3795 struct nfs_ephemeral_mount_harvester_info *hinfo = arg;
3796 struct nfsmount *nmp;
3797 struct timeval now;
3798
3799 if (strcmp(mp->mnt_vfsstat.f_fstypename, "nfs"))
3800 return (VFS_RETURNED);
3801 nmp = VFSTONFS(mp);
3802 if (!nmp || !NMFLAG(nmp, EPHEMERAL))
3803 return (VFS_RETURNED);
3804 hinfo->mountcount++;
3805
3806 /* avoid unmounting mounts that have been triggered within the last harvest interval */
3807 microtime(&now);
3808 if ((nmp->nm_mounttime >> 32) > ((uint32_t)now.tv_sec - NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL))
3809 return (VFS_RETURNED);
3810
3811 if (hinfo->fsid.val[0] || hinfo->fsid.val[1]) {
3812 /* attempt to unmount previously-found ephemeral mount */
3813 vfs_unmountbyfsid(&hinfo->fsid, 0, vfs_context_kernel());
3814 hinfo->fsid.val[0] = hinfo->fsid.val[1] = 0;
3815 }
3816
3817 /*
3818 * We can't call unmount here since we hold a mount iter ref
3819 * on mp so save its fsid for the next call iteration to unmount.
3820 */
3821 hinfo->fsid.val[0] = mp->mnt_vfsstat.f_fsid.val[0];
3822 hinfo->fsid.val[1] = mp->mnt_vfsstat.f_fsid.val[1];
3823
3824 return (VFS_RETURNED);
3825 }
3826
3827 /*
3828 * Spawn a thread to do the ephemeral mount harvesting.
3829 */
3830 static void
3831 nfs_ephemeral_mount_harvester_timer_func(void)
3832 {
3833 thread_t thd;
3834
3835 if (kernel_thread_start(nfs_ephemeral_mount_harvester, NULL, &thd) == KERN_SUCCESS)
3836 thread_deallocate(thd);
3837 }
3838
3839 /*
3840 * Iterate all mounts looking for NFS ephemeral mounts to try to unmount.
3841 */
3842 void
3843 nfs_ephemeral_mount_harvester(__unused void *arg, __unused wait_result_t wr)
3844 {
3845 struct nfs_ephemeral_mount_harvester_info hinfo;
3846 uint64_t deadline;
3847
3848 hinfo.mountcount = 0;
3849 hinfo.fsid.val[0] = hinfo.fsid.val[1] = 0;
3850 vfs_iterate(VFS_ITERATE_TAIL_FIRST, nfs_ephemeral_mount_harvester_callback, &hinfo);
3851 if (hinfo.fsid.val[0] || hinfo.fsid.val[1]) {
3852 /* attempt to unmount last found ephemeral mount */
3853 vfs_unmountbyfsid(&hinfo.fsid, 0, vfs_context_kernel());
3854 }
3855
3856 lck_mtx_lock(nfs_global_mutex);
3857 if (!hinfo.mountcount) {
3858 /* no more ephemeral mounts - don't need timer */
3859 nfs_ephemeral_mount_harvester_on = 0;
3860 } else {
3861 /* re-arm the timer */
3862 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL, NSEC_PER_SEC, &deadline);
3863 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer, deadline);
3864 nfs_ephemeral_mount_harvester_on = 1;
3865 }
3866 lck_mtx_unlock(nfs_global_mutex);
3867
3868 /* thread done */
3869 thread_terminate(current_thread());
3870 }
3871
3872 /*
3873 * Make sure the NFS ephemeral mount harvester timer is running.
3874 */
3875 void
3876 nfs_ephemeral_mount_harvester_start(void)
3877 {
3878 uint64_t deadline;
3879
3880 lck_mtx_lock(nfs_global_mutex);
3881 if (nfs_ephemeral_mount_harvester_on) {
3882 lck_mtx_unlock(nfs_global_mutex);
3883 return;
3884 }
3885 if (nfs_ephemeral_mount_harvester_timer == NULL)
3886 nfs_ephemeral_mount_harvester_timer = thread_call_allocate((thread_call_func_t)nfs_ephemeral_mount_harvester_timer_func, NULL);
3887 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL, NSEC_PER_SEC, &deadline);
3888 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer, deadline);
3889 nfs_ephemeral_mount_harvester_on = 1;
3890 lck_mtx_unlock(nfs_global_mutex);
3891 }
3892
3893 #endif
3894
3895 /*
3896 * Send a MOUNT protocol MOUNT request to the server to get the initial file handle (and security).
3897 */
3898 int
3899 nfs3_mount_rpc(struct nfsmount *nmp, struct sockaddr *sa, int sotype, int nfsvers, char *path, vfs_context_t ctx, int timeo, fhandle_t *fh, struct nfs_sec *sec)
3900 {
3901 int error = 0, slen, mntproto;
3902 thread_t thd = vfs_context_thread(ctx);
3903 kauth_cred_t cred = vfs_context_ucred(ctx);
3904 uint64_t xid = 0;
3905 struct nfsm_chain nmreq, nmrep;
3906 mbuf_t mreq;
3907 uint32_t mntvers, mntport, val;
3908 struct sockaddr_storage ss;
3909 struct sockaddr *saddr = (struct sockaddr*)&ss;
3910
3911 nfsm_chain_null(&nmreq);
3912 nfsm_chain_null(&nmrep);
3913
3914 mntvers = (nfsvers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
3915 mntproto = (NM_OMFLAG(nmp, MNTUDP) || (sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP;
3916 sec->count = 0;
3917
3918 bcopy(sa, saddr, min(sizeof(ss), sa->sa_len));
3919 if (saddr->sa_family == AF_INET) {
3920 if (nmp->nm_mountport)
3921 ((struct sockaddr_in*)saddr)->sin_port = htons(nmp->nm_mountport);
3922 mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
3923 } else {
3924 if (nmp->nm_mountport)
3925 ((struct sockaddr_in6*)saddr)->sin6_port = htons(nmp->nm_mountport);
3926 mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
3927 }
3928
3929 while (!mntport) {
3930 error = nfs_portmap_lookup(nmp, ctx, saddr, NULL, RPCPROG_MNT, mntvers, mntproto, timeo);
3931 nfsmout_if(error);
3932 if (saddr->sa_family == AF_INET)
3933 mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
3934 else
3935 mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
3936 if (!mntport) {
3937 /* if not found and TCP, then retry with UDP */
3938 if (mntproto == IPPROTO_UDP) {
3939 error = EPROGUNAVAIL;
3940 break;
3941 }
3942 mntproto = IPPROTO_UDP;
3943 bcopy(sa, saddr, min(sizeof(ss), sa->sa_len));
3944 }
3945 }
3946 nfsmout_if(error || !mntport);
3947
3948 /* MOUNT protocol MOUNT request */
3949 slen = strlen(path);
3950 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_UNSIGNED + nfsm_rndup(slen));
3951 nfsm_chain_add_name(error, &nmreq, path, slen, nmp);
3952 nfsm_chain_build_done(error, &nmreq);
3953 nfsmout_if(error);
3954 error = nfsm_rpchead2(nmp, (mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
3955 RPCPROG_MNT, mntvers, RPCMNT_MOUNT,
3956 RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
3957 nfsmout_if(error);
3958 nmreq.nmc_mhead = NULL;
3959 error = nfs_aux_request(nmp, thd, saddr, NULL,
3960 ((mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM),
3961 mreq, R_XID32(xid), 1, timeo, &nmrep);
3962 nfsmout_if(error);
3963 nfsm_chain_get_32(error, &nmrep, val);
3964 if (!error && val)
3965 error = val;
3966 nfsm_chain_get_fh(error, &nmrep, nfsvers, fh);
3967 if (!error && (nfsvers > NFS_VER2)) {
3968 sec->count = NX_MAX_SEC_FLAVORS;
3969 error = nfsm_chain_get_secinfo(&nmrep, &sec->flavors[0], &sec->count);
3970 }
3971 nfsmout:
3972 nfsm_chain_cleanup(&nmreq);
3973 nfsm_chain_cleanup(&nmrep);
3974 return (error);
3975 }
3976
3977
3978 /*
3979 * Send a MOUNT protocol UNMOUNT request to tell the server we've unmounted it.
3980 */
3981 void
3982 nfs3_umount_rpc(struct nfsmount *nmp, vfs_context_t ctx, int timeo)
3983 {
3984 int error = 0, slen, mntproto;
3985 thread_t thd = vfs_context_thread(ctx);
3986 kauth_cred_t cred = vfs_context_ucred(ctx);
3987 char *path;
3988 uint64_t xid = 0;
3989 struct nfsm_chain nmreq, nmrep;
3990 mbuf_t mreq;
3991 uint32_t mntvers, mntport;
3992 struct sockaddr_storage ss;
3993 struct sockaddr *saddr = (struct sockaddr*)&ss;
3994
3995 if (!nmp->nm_saddr)
3996 return;
3997
3998 nfsm_chain_null(&nmreq);
3999 nfsm_chain_null(&nmrep);
4000
4001 mntvers = (nmp->nm_vers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
4002 mntproto = (NM_OMFLAG(nmp, MNTUDP) || (nmp->nm_sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP;
4003 mntport = nmp->nm_mountport;
4004
4005 bcopy(nmp->nm_saddr, saddr, min(sizeof(ss), nmp->nm_saddr->sa_len));
4006 if (saddr->sa_family == AF_INET)
4007 ((struct sockaddr_in*)saddr)->sin_port = htons(mntport);
4008 else
4009 ((struct sockaddr_in6*)saddr)->sin6_port = htons(mntport);
4010
4011 while (!mntport) {
4012 error = nfs_portmap_lookup(nmp, ctx, saddr, NULL, RPCPROG_MNT, mntvers, mntproto, timeo);
4013 nfsmout_if(error);
4014 if (saddr->sa_family == AF_INET)
4015 mntport = ntohs(((struct sockaddr_in*)saddr)->sin_port);
4016 else
4017 mntport = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
4018 /* if not found and mntvers > VER1, then retry with VER1 */
4019 if (!mntport) {
4020 if (mntvers > RPCMNT_VER1) {
4021 mntvers = RPCMNT_VER1;
4022 } else if (mntproto == IPPROTO_TCP) {
4023 mntproto = IPPROTO_UDP;
4024 mntvers = (nmp->nm_vers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3;
4025 } else {
4026 break;
4027 }
4028 bcopy(nmp->nm_saddr, saddr, min(sizeof(ss), nmp->nm_saddr->sa_len));
4029 }
4030 }
4031 nfsmout_if(!mntport);
4032
4033 /* MOUNT protocol UNMOUNT request */
4034 path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
4035 while (*path && (*path != '/'))
4036 path++;
4037 slen = strlen(path);
4038 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_UNSIGNED + nfsm_rndup(slen));
4039 nfsm_chain_add_name(error, &nmreq, path, slen, nmp);
4040 nfsm_chain_build_done(error, &nmreq);
4041 nfsmout_if(error);
4042 error = nfsm_rpchead2(nmp, (mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
4043 RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMOUNT,
4044 RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
4045 nfsmout_if(error);
4046 nmreq.nmc_mhead = NULL;
4047 error = nfs_aux_request(nmp, thd, saddr, NULL,
4048 ((mntproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM),
4049 mreq, R_XID32(xid), 1, timeo, &nmrep);
4050 nfsmout:
4051 nfsm_chain_cleanup(&nmreq);
4052 nfsm_chain_cleanup(&nmrep);
4053 }
4054
4055 /*
4056 * unmount system call
4057 */
4058 int
4059 nfs_vfs_unmount(
4060 mount_t mp,
4061 int mntflags,
4062 __unused vfs_context_t ctx)
4063 {
4064 struct nfsmount *nmp;
4065 vnode_t vp;
4066 int error, flags = 0;
4067 struct timespec ts = { 1, 0 };
4068
4069 nmp = VFSTONFS(mp);
4070 lck_mtx_lock(&nmp->nm_lock);
4071 /*
4072 * Set the flag indicating that an unmount attempt is in progress.
4073 */
4074 nmp->nm_state |= NFSSTA_UNMOUNTING;
4075 /*
4076 * During a force unmount we want to...
4077 * Mark that we are doing a force unmount.
4078 * Make the mountpoint soft.
4079 */
4080 if (mntflags & MNT_FORCE) {
4081 flags |= FORCECLOSE;
4082 nmp->nm_state |= NFSSTA_FORCE;
4083 NFS_BITMAP_SET(nmp->nm_flags, NFS_MFLAG_SOFT);
4084 }
4085 /*
4086 * Wait for any in-progress monitored node scan to complete.
4087 */
4088 while (nmp->nm_state & NFSSTA_MONITOR_SCAN)
4089 msleep(&nmp->nm_state, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts);
4090 /*
4091 * Goes something like this..
4092 * - Call vflush() to clear out vnodes for this file system,
4093 * except for the swap files. Deal with them in 2nd pass.
4094 * - Decrement reference on the vnode representing remote root.
4095 * - Clean up the NFS mount structure.
4096 */
4097 vp = NFSTOV(nmp->nm_dnp);
4098 lck_mtx_unlock(&nmp->nm_lock);
4099
4100 /*
4101 * vflush will check for busy vnodes on mountpoint.
4102 * Will do the right thing for MNT_FORCE. That is, we should
4103 * not get EBUSY back.
4104 */
4105 error = vflush(mp, vp, SKIPSWAP | flags);
4106 if (mntflags & MNT_FORCE) {
4107 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
4108 } else {
4109 if (vnode_isinuse(vp, 1))
4110 error = EBUSY;
4111 else
4112 error = vflush(mp, vp, flags);
4113 }
4114 if (error) {
4115 lck_mtx_lock(&nmp->nm_lock);
4116 nmp->nm_state &= ~NFSSTA_UNMOUNTING;
4117 lck_mtx_unlock(&nmp->nm_lock);
4118 return (error);
4119 }
4120
4121 lck_mtx_lock(&nmp->nm_lock);
4122 nmp->nm_dnp = NULL;
4123 lck_mtx_unlock(&nmp->nm_lock);
4124
4125 /*
4126 * Release the root vnode reference held by mountnfs()
4127 */
4128 error = vnode_get(vp);
4129 vnode_rele(vp);
4130 if (!error)
4131 vnode_put(vp);
4132
4133 vflush(mp, NULLVP, FORCECLOSE);
4134
4135 nfs_mount_cleanup(nmp);
4136 return (0);
4137 }
4138
4139 /*
4140 * cleanup/destroy NFS fs locations structure
4141 */
4142 void
4143 nfs_fs_locations_cleanup(struct nfs_fs_locations *nfslsp)
4144 {
4145 struct nfs_fs_location *fsl;
4146 struct nfs_fs_server *fss;
4147 struct nfs_fs_path *fsp;
4148 uint32_t loc, serv, addr, comp;
4149
4150 /* free up fs locations */
4151 if (!nfslsp->nl_numlocs || !nfslsp->nl_locations)
4152 return;
4153
4154 for (loc = 0; loc < nfslsp->nl_numlocs; loc++) {
4155 fsl = nfslsp->nl_locations[loc];
4156 if (!fsl)
4157 continue;
4158 if ((fsl->nl_servcount > 0) && fsl->nl_servers) {
4159 for (serv = 0; serv < fsl->nl_servcount; serv++) {
4160 fss = fsl->nl_servers[serv];
4161 if (!fss)
4162 continue;
4163 if ((fss->ns_addrcount > 0) && fss->ns_addresses) {
4164 for (addr = 0; addr < fss->ns_addrcount; addr++)
4165 FREE(fss->ns_addresses[addr], M_TEMP);
4166 FREE(fss->ns_addresses, M_TEMP);
4167 }
4168 FREE(fss->ns_name, M_TEMP);
4169 FREE(fss, M_TEMP);
4170 }
4171 FREE(fsl->nl_servers, M_TEMP);
4172 }
4173 fsp = &fsl->nl_path;
4174 if (fsp->np_compcount && fsp->np_components) {
4175 for (comp = 0; comp < fsp->np_compcount; comp++)
4176 if (fsp->np_components[comp])
4177 FREE(fsp->np_components[comp], M_TEMP);
4178 FREE(fsp->np_components, M_TEMP);
4179 }
4180 FREE(fsl, M_TEMP);
4181 }
4182 FREE(nfslsp->nl_locations, M_TEMP);
4183 nfslsp->nl_numlocs = 0;
4184 nfslsp->nl_locations = NULL;
4185 }
4186
4187 /*
4188 * cleanup/destroy an nfsmount
4189 */
4190 void
4191 nfs_mount_cleanup(struct nfsmount *nmp)
4192 {
4193 struct nfsreq *req, *treq;
4194 struct nfs_reqqhead iodq;
4195 struct timespec ts = { 1, 0 };
4196 struct nfs_open_owner *noop, *nextnoop;
4197 nfsnode_t np;
4198 int docallback;
4199
4200 /* stop callbacks */
4201 if ((nmp->nm_vers >= NFS_VER4) && !NMFLAG(nmp, NOCALLBACK) && nmp->nm_cbid)
4202 nfs4_mount_callback_shutdown(nmp);
4203
4204 /* Destroy any RPCSEC_GSS contexts */
4205 if (!TAILQ_EMPTY(&nmp->nm_gsscl))
4206 nfs_gss_clnt_ctx_unmount(nmp);
4207
4208 /* mark the socket for termination */
4209 lck_mtx_lock(&nmp->nm_lock);
4210 nmp->nm_sockflags |= NMSOCK_UNMOUNT;
4211
4212 /* Have the socket thread send the unmount RPC, if requested/appropriate. */
4213 if ((nmp->nm_vers < NFS_VER4) && (nmp->nm_state & NFSSTA_MOUNTED) &&
4214 !(nmp->nm_state & NFSSTA_FORCE) && NMFLAG(nmp, CALLUMNT))
4215 nfs_mount_sock_thread_wake(nmp);
4216
4217 /* wait for the socket thread to terminate */
4218 while (nmp->nm_sockthd) {
4219 wakeup(&nmp->nm_sockthd);
4220 msleep(&nmp->nm_sockthd, &nmp->nm_lock, PZERO-1, "nfswaitsockthd", &ts);
4221 }
4222
4223 lck_mtx_unlock(&nmp->nm_lock);
4224
4225 /* tear down the socket */
4226 nfs_disconnect(nmp);
4227
4228 if (nmp->nm_mountp)
4229 vfs_setfsprivate(nmp->nm_mountp, NULL);
4230
4231 lck_mtx_lock(&nmp->nm_lock);
4232
4233 if ((nmp->nm_vers >= NFS_VER4) && !NMFLAG(nmp, NOCALLBACK) && nmp->nm_cbid) {
4234 /* clear out any pending delegation return requests */
4235 while ((np = TAILQ_FIRST(&nmp->nm_dreturnq))) {
4236 TAILQ_REMOVE(&nmp->nm_dreturnq, np, n_dreturn);
4237 np->n_dreturn.tqe_next = NFSNOLIST;
4238 }
4239 }
4240
4241 /* cancel any renew timer */
4242 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_renew_timer) {
4243 thread_call_cancel(nmp->nm_renew_timer);
4244 thread_call_free(nmp->nm_renew_timer);
4245 }
4246
4247 if (nmp->nm_saddr)
4248 FREE(nmp->nm_saddr, M_SONAME);
4249 if ((nmp->nm_vers < NFS_VER4) && nmp->nm_rqsaddr)
4250 FREE(nmp->nm_rqsaddr, M_SONAME);
4251 lck_mtx_unlock(&nmp->nm_lock);
4252
4253 if (nmp->nm_state & NFSSTA_MOUNTED)
4254 switch (nmp->nm_lockmode) {
4255 case NFS_LOCK_MODE_DISABLED:
4256 case NFS_LOCK_MODE_LOCAL:
4257 break;
4258 case NFS_LOCK_MODE_ENABLED:
4259 default:
4260 if (nmp->nm_vers <= NFS_VER3)
4261 nfs_lockd_mount_unregister(nmp);
4262 break;
4263 }
4264
4265 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_longid) {
4266 /* remove/deallocate the client ID data */
4267 lck_mtx_lock(nfs_global_mutex);
4268 TAILQ_REMOVE(&nfsclientids, nmp->nm_longid, nci_link);
4269 if (nmp->nm_longid->nci_id)
4270 FREE(nmp->nm_longid->nci_id, M_TEMP);
4271 FREE(nmp->nm_longid, M_TEMP);
4272 lck_mtx_unlock(nfs_global_mutex);
4273 }
4274
4275 /*
4276 * Loop through outstanding request list and remove dangling
4277 * references to defunct nfsmount struct
4278 */
4279 TAILQ_INIT(&iodq);
4280 lck_mtx_lock(nfs_request_mutex);
4281 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
4282 if (req->r_nmp == nmp) {
4283 lck_mtx_lock(&req->r_mtx);
4284 req->r_nmp = NULL;
4285 lck_mtx_unlock(&req->r_mtx);
4286 if (req->r_callback.rcb_func) {
4287 /* async I/O RPC needs to be finished */
4288 lck_mtx_lock(nfsiod_mutex);
4289 if (req->r_achain.tqe_next == NFSREQNOLIST)
4290 TAILQ_INSERT_TAIL(&iodq, req, r_achain);
4291 lck_mtx_unlock(nfsiod_mutex);
4292 }
4293 lck_mtx_lock(&req->r_mtx);
4294 lck_mtx_lock(&nmp->nm_lock);
4295 if (req->r_flags & R_RESENDQ) {
4296 if (req->r_rchain.tqe_next != NFSREQNOLIST) {
4297 TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain);
4298 req->r_rchain.tqe_next = NFSREQNOLIST;
4299 }
4300 req->r_flags &= ~R_RESENDQ;
4301 }
4302 lck_mtx_unlock(&nmp->nm_lock);
4303 lck_mtx_unlock(&req->r_mtx);
4304 wakeup(req);
4305 }
4306 }
4307 lck_mtx_unlock(nfs_request_mutex);
4308
4309 /* finish any async I/O RPCs queued up */
4310 lck_mtx_lock(nfsiod_mutex);
4311 TAILQ_CONCAT(&iodq, &nmp->nm_iodq, r_achain);
4312 lck_mtx_unlock(nfsiod_mutex);
4313 TAILQ_FOREACH_SAFE(req, &iodq, r_achain, treq) {
4314 TAILQ_REMOVE(&iodq, req, r_achain);
4315 req->r_achain.tqe_next = NFSREQNOLIST;
4316 lck_mtx_lock(&req->r_mtx);
4317 req->r_error = ENXIO;
4318 docallback = !(req->r_flags & R_WAITSENT);
4319 lck_mtx_unlock(&req->r_mtx);
4320 if (docallback)
4321 req->r_callback.rcb_func(req);
4322 }
4323
4324 /* clean up common state */
4325 lck_mtx_lock(&nmp->nm_lock);
4326 while ((np = LIST_FIRST(&nmp->nm_monlist))) {
4327 LIST_REMOVE(np, n_monlink);
4328 np->n_monlink.le_next = NFSNOLIST;
4329 }
4330 TAILQ_FOREACH_SAFE(noop, &nmp->nm_open_owners, noo_link, nextnoop) {
4331 TAILQ_REMOVE(&nmp->nm_open_owners, noop, noo_link);
4332 noop->noo_flags &= ~NFS_OPEN_OWNER_LINK;
4333 if (noop->noo_refcnt)
4334 continue;
4335 nfs_open_owner_destroy(noop);
4336 }
4337 lck_mtx_unlock(&nmp->nm_lock);
4338
4339 /* clean up NFSv4 state */
4340 if (nmp->nm_vers >= NFS_VER4) {
4341 lck_mtx_lock(&nmp->nm_lock);
4342 while ((np = TAILQ_FIRST(&nmp->nm_delegations))) {
4343 TAILQ_REMOVE(&nmp->nm_delegations, np, n_dlink);
4344 np->n_dlink.tqe_next = NFSNOLIST;
4345 }
4346 lck_mtx_unlock(&nmp->nm_lock);
4347 }
4348 if (IS_VALID_CRED(nmp->nm_mcred))
4349 kauth_cred_unref(&nmp->nm_mcred);
4350
4351 nfs_fs_locations_cleanup(&nmp->nm_locations);
4352
4353 if (nmp->nm_args)
4354 xb_free(nmp->nm_args);
4355 lck_mtx_destroy(&nmp->nm_lock, nfs_mount_grp);
4356 if (nmp->nm_fh)
4357 FREE(nmp->nm_fh, M_TEMP);
4358 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
4359 }
4360
4361 /*
4362 * Return root of a filesystem
4363 */
4364 int
4365 nfs_vfs_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t ctx)
4366 {
4367 vnode_t vp;
4368 struct nfsmount *nmp;
4369 int error;
4370 u_int32_t vpid;
4371
4372 nmp = VFSTONFS(mp);
4373 if (!nmp || !nmp->nm_dnp)
4374 return (ENXIO);
4375 vp = NFSTOV(nmp->nm_dnp);
4376 vpid = vnode_vid(vp);
4377 while ((error = vnode_getwithvid(vp, vpid))) {
4378 /* vnode_get() may return ENOENT if the dir changes. */
4379 /* If that happens, just try it again, else return the error. */
4380 if ((error != ENOENT) || (vnode_vid(vp) == vpid))
4381 return (error);
4382 vpid = vnode_vid(vp);
4383 }
4384 *vpp = vp;
4385 return (0);
4386 }
4387
4388 /*
4389 * Do operations associated with quotas
4390 */
4391 #if !QUOTA
4392 int
4393 nfs_vfs_quotactl(
4394 __unused mount_t mp,
4395 __unused int cmds,
4396 __unused uid_t uid,
4397 __unused caddr_t datap,
4398 __unused vfs_context_t context)
4399 {
4400 return (ENOTSUP);
4401 }
4402 #else
4403
4404 int
4405 nfs3_getquota(struct nfsmount *nmp, vfs_context_t ctx, uid_t id, int type, struct dqblk *dqb)
4406 {
4407 int error = 0, slen, timeo;
4408 int rqport = 0, rqproto, rqvers = (type == GRPQUOTA) ? RPCRQUOTA_EXT_VER : RPCRQUOTA_VER;
4409 thread_t thd = vfs_context_thread(ctx);
4410 kauth_cred_t cred = vfs_context_ucred(ctx);
4411 char *path;
4412 uint64_t xid = 0;
4413 struct nfsm_chain nmreq, nmrep;
4414 mbuf_t mreq;
4415 uint32_t val = 0, bsize = 0;
4416 struct sockaddr *rqsaddr;
4417 struct timeval now;
4418
4419 if (!nmp->nm_saddr)
4420 return (ENXIO);
4421
4422 if (NMFLAG(nmp, NOQUOTA))
4423 return (ENOTSUP);
4424
4425 if (!nmp->nm_rqsaddr)
4426 MALLOC(nmp->nm_rqsaddr, struct sockaddr *, sizeof(struct sockaddr_storage), M_SONAME, M_WAITOK|M_ZERO);
4427 if (!nmp->nm_rqsaddr)
4428 return (ENOMEM);
4429 rqsaddr = nmp->nm_rqsaddr;
4430 if (rqsaddr->sa_family == AF_INET6)
4431 rqport = ntohs(((struct sockaddr_in6*)rqsaddr)->sin6_port);
4432 else if (rqsaddr->sa_family == AF_INET)
4433 rqport = ntohs(((struct sockaddr_in*)rqsaddr)->sin_port);
4434
4435 timeo = NMFLAG(nmp, SOFT) ? 10 : 60;
4436 rqproto = IPPROTO_UDP; /* XXX should prefer TCP if mount is TCP */
4437
4438 /* check if we have a recently cached rquota port */
4439 microuptime(&now);
4440 if (!rqport || ((nmp->nm_rqsaddrstamp + 60) >= (uint32_t)now.tv_sec)) {
4441 /* send portmap request to get rquota port */
4442 bcopy(nmp->nm_saddr, rqsaddr, min(sizeof(struct sockaddr_storage), nmp->nm_saddr->sa_len));
4443 error = nfs_portmap_lookup(nmp, ctx, rqsaddr, NULL, RPCPROG_RQUOTA, rqvers, rqproto, timeo);
4444 if (error)
4445 return (error);
4446 if (rqsaddr->sa_family == AF_INET6)
4447 rqport = ntohs(((struct sockaddr_in6*)rqsaddr)->sin6_port);
4448 else if (rqsaddr->sa_family == AF_INET)
4449 rqport = ntohs(((struct sockaddr_in*)rqsaddr)->sin_port);
4450 else
4451 return (EIO);
4452 if (!rqport)
4453 return (ENOTSUP);
4454 microuptime(&now);
4455 nmp->nm_rqsaddrstamp = now.tv_sec;
4456 }
4457
4458 /* rquota request */
4459 nfsm_chain_null(&nmreq);
4460 nfsm_chain_null(&nmrep);
4461 path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
4462 while (*path && (*path != '/'))
4463 path++;
4464 slen = strlen(path);
4465 nfsm_chain_build_alloc_init(error, &nmreq, 3 * NFSX_UNSIGNED + nfsm_rndup(slen));
4466 nfsm_chain_add_name(error, &nmreq, path, slen, nmp);
4467 if (type == GRPQUOTA)
4468 nfsm_chain_add_32(error, &nmreq, type);
4469 nfsm_chain_add_32(error, &nmreq, id);
4470 nfsm_chain_build_done(error, &nmreq);
4471 nfsmout_if(error);
4472 error = nfsm_rpchead2(nmp, (rqproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
4473 RPCPROG_RQUOTA, rqvers, RPCRQUOTA_GET,
4474 RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
4475 nfsmout_if(error);
4476 nmreq.nmc_mhead = NULL;
4477 error = nfs_aux_request(nmp, thd, rqsaddr, NULL,
4478 (rqproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM,
4479 mreq, R_XID32(xid), 0, timeo, &nmrep);
4480 nfsmout_if(error);
4481
4482 /* parse rquota response */
4483 nfsm_chain_get_32(error, &nmrep, val);
4484 if (!error && (val != RQUOTA_STAT_OK)) {
4485 if (val == RQUOTA_STAT_NOQUOTA)
4486 error = ENOENT;
4487 else if (val == RQUOTA_STAT_EPERM)
4488 error = EPERM;
4489 else
4490 error = EIO;
4491 }
4492 nfsm_chain_get_32(error, &nmrep, bsize);
4493 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
4494 nfsm_chain_get_32(error, &nmrep, val);
4495 nfsmout_if(error);
4496 dqb->dqb_bhardlimit = (uint64_t)val * bsize;
4497 nfsm_chain_get_32(error, &nmrep, val);
4498 nfsmout_if(error);
4499 dqb->dqb_bsoftlimit = (uint64_t)val * bsize;
4500 nfsm_chain_get_32(error, &nmrep, val);
4501 nfsmout_if(error);
4502 dqb->dqb_curbytes = (uint64_t)val * bsize;
4503 nfsm_chain_get_32(error, &nmrep, dqb->dqb_ihardlimit);
4504 nfsm_chain_get_32(error, &nmrep, dqb->dqb_isoftlimit);
4505 nfsm_chain_get_32(error, &nmrep, dqb->dqb_curinodes);
4506 nfsm_chain_get_32(error, &nmrep, dqb->dqb_btime);
4507 nfsm_chain_get_32(error, &nmrep, dqb->dqb_itime);
4508 nfsmout_if(error);
4509 dqb->dqb_id = id;
4510 nfsmout:
4511 nfsm_chain_cleanup(&nmreq);
4512 nfsm_chain_cleanup(&nmrep);
4513 return (error);
4514 }
4515
4516 int
4517 nfs4_getquota(struct nfsmount *nmp, vfs_context_t ctx, uid_t id, int type, struct dqblk *dqb)
4518 {
4519 nfsnode_t np;
4520 int error = 0, status, nfsvers, numops;
4521 u_int64_t xid;
4522 struct nfsm_chain nmreq, nmrep;
4523 uint32_t bitmap[NFS_ATTR_BITMAP_LEN];
4524 thread_t thd = vfs_context_thread(ctx);
4525 kauth_cred_t cred = vfs_context_ucred(ctx);
4526 struct nfsreq_secinfo_args si;
4527
4528 if (type != USRQUOTA) /* NFSv4 only supports user quotas */
4529 return (ENOTSUP);
4530
4531 /* first check that the server supports any of the quota attributes */
4532 if (!NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_HARD) &&
4533 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_SOFT) &&
4534 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_USED))
4535 return (ENOTSUP);
4536
4537 /*
4538 * The credential passed to the server needs to have
4539 * an effective uid that matches the given uid.
4540 */
4541 if (id != kauth_cred_getuid(cred)) {
4542 struct posix_cred temp_pcred;
4543 posix_cred_t pcred = posix_cred_get(cred);
4544 bzero(&temp_pcred, sizeof(temp_pcred));
4545 temp_pcred.cr_uid = id;
4546 temp_pcred.cr_ngroups = pcred->cr_ngroups;
4547 bcopy(pcred->cr_groups, temp_pcred.cr_groups, sizeof(temp_pcred.cr_groups));
4548 cred = posix_cred_create(&temp_pcred);
4549 if (!IS_VALID_CRED(cred))
4550 return (ENOMEM);
4551 } else {
4552 kauth_cred_ref(cred);
4553 }
4554
4555 nfsvers = nmp->nm_vers;
4556 np = nmp->nm_dnp;
4557 if (!np)
4558 error = ENXIO;
4559 if (error || ((error = vnode_get(NFSTOV(np))))) {
4560 kauth_cred_unref(&cred);
4561 return(error);
4562 }
4563
4564 NFSREQ_SECINFO_SET(&si, np, NULL, 0, NULL, 0);
4565 nfsm_chain_null(&nmreq);
4566 nfsm_chain_null(&nmrep);
4567
4568 // PUTFH + GETATTR
4569 numops = 2;
4570 nfsm_chain_build_alloc_init(error, &nmreq, 15 * NFSX_UNSIGNED);
4571 nfsm_chain_add_compound_header(error, &nmreq, "quota", numops);
4572 numops--;
4573 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
4574 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
4575 numops--;
4576 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
4577 NFS_CLEAR_ATTRIBUTES(bitmap);
4578 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_AVAIL_HARD);
4579 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_AVAIL_SOFT);
4580 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_USED);
4581 nfsm_chain_add_bitmap_supported(error, &nmreq, bitmap, nmp, NULL);
4582 nfsm_chain_build_done(error, &nmreq);
4583 nfsm_assert(error, (numops == 0), EPROTO);
4584 nfsmout_if(error);
4585 error = nfs_request2(np, NULL, &nmreq, NFSPROC4_COMPOUND, thd, cred, &si, 0, &nmrep, &xid, &status);
4586 nfsm_chain_skip_tag(error, &nmrep);
4587 nfsm_chain_get_32(error, &nmrep, numops);
4588 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
4589 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
4590 nfsm_assert(error, NFSTONMP(np), ENXIO);
4591 nfsmout_if(error);
4592 error = nfs4_parsefattr(&nmrep, NULL, NULL, NULL, dqb, NULL);
4593 nfsmout_if(error);
4594 nfsm_assert(error, NFSTONMP(np), ENXIO);
4595 nfsmout:
4596 nfsm_chain_cleanup(&nmreq);
4597 nfsm_chain_cleanup(&nmrep);
4598 vnode_put(NFSTOV(np));
4599 kauth_cred_unref(&cred);
4600 return (error);
4601 }
4602
4603 int
4604 nfs_vfs_quotactl(mount_t mp, int cmds, uid_t uid, caddr_t datap, vfs_context_t ctx)
4605 {
4606 struct nfsmount *nmp;
4607 int cmd, type, error, nfsvers;
4608 uid_t euid = kauth_cred_getuid(vfs_context_ucred(ctx));
4609 struct dqblk *dqb = (struct dqblk*)datap;
4610
4611 if (!(nmp = VFSTONFS(mp)))
4612 return (ENXIO);
4613 nfsvers = nmp->nm_vers;
4614
4615 if (uid == ~0U)
4616 uid = euid;
4617
4618 /* we can only support Q_GETQUOTA */
4619 cmd = cmds >> SUBCMDSHIFT;
4620 switch (cmd) {
4621 case Q_GETQUOTA:
4622 break;
4623 case Q_QUOTAON:
4624 case Q_QUOTAOFF:
4625 case Q_SETQUOTA:
4626 case Q_SETUSE:
4627 case Q_SYNC:
4628 case Q_QUOTASTAT:
4629 return (ENOTSUP);
4630 default:
4631 return (EINVAL);
4632 }
4633
4634 type = cmds & SUBCMDMASK;
4635 if ((u_int)type >= MAXQUOTAS)
4636 return (EINVAL);
4637 if ((uid != euid) && ((error = vfs_context_suser(ctx))))
4638 return (error);
4639
4640 if (vfs_busy(mp, LK_NOWAIT))
4641 return (0);
4642 bzero(dqb, sizeof(*dqb));
4643 error = nmp->nm_funcs->nf_getquota(nmp, ctx, uid, type, dqb);
4644 vfs_unbusy(mp);
4645 return (error);
4646 }
4647 #endif
4648
4649 /*
4650 * Flush out the buffer cache
4651 */
4652 int nfs_sync_callout(vnode_t, void *);
4653
4654 struct nfs_sync_cargs {
4655 vfs_context_t ctx;
4656 int waitfor;
4657 int error;
4658 };
4659
4660 int
4661 nfs_sync_callout(vnode_t vp, void *arg)
4662 {
4663 struct nfs_sync_cargs *cargs = (struct nfs_sync_cargs*)arg;
4664 nfsnode_t np = VTONFS(vp);
4665 int error;
4666
4667 if (np->n_flag & NREVOKE) {
4668 vn_revoke(vp, REVOKEALL, cargs->ctx);
4669 return (VNODE_RETURNED);
4670 }
4671
4672 if (LIST_EMPTY(&np->n_dirtyblkhd))
4673 return (VNODE_RETURNED);
4674 if (np->n_wrbusy > 0)
4675 return (VNODE_RETURNED);
4676 if (np->n_bflag & (NBFLUSHINPROG|NBINVALINPROG))
4677 return (VNODE_RETURNED);
4678
4679 error = nfs_flush(np, cargs->waitfor, vfs_context_thread(cargs->ctx), 0);
4680 if (error)
4681 cargs->error = error;
4682
4683 return (VNODE_RETURNED);
4684 }
4685
4686 int
4687 nfs_vfs_sync(mount_t mp, int waitfor, vfs_context_t ctx)
4688 {
4689 struct nfs_sync_cargs cargs;
4690
4691 cargs.waitfor = waitfor;
4692 cargs.ctx = ctx;
4693 cargs.error = 0;
4694
4695 vnode_iterate(mp, 0, nfs_sync_callout, &cargs);
4696
4697 return (cargs.error);
4698 }
4699
4700 /*
4701 * NFS flat namespace lookup.
4702 * Currently unsupported.
4703 */
4704 /*ARGSUSED*/
4705 int
4706 nfs_vfs_vget(
4707 __unused mount_t mp,
4708 __unused ino64_t ino,
4709 __unused vnode_t *vpp,
4710 __unused vfs_context_t ctx)
4711 {
4712
4713 return (ENOTSUP);
4714 }
4715
4716 /*
4717 * At this point, this should never happen
4718 */
4719 /*ARGSUSED*/
4720 int
4721 nfs_vfs_fhtovp(
4722 __unused mount_t mp,
4723 __unused int fhlen,
4724 __unused unsigned char *fhp,
4725 __unused vnode_t *vpp,
4726 __unused vfs_context_t ctx)
4727 {
4728
4729 return (ENOTSUP);
4730 }
4731
4732 /*
4733 * Vnode pointer to File handle, should never happen either
4734 */
4735 /*ARGSUSED*/
4736 int
4737 nfs_vfs_vptofh(
4738 __unused vnode_t vp,
4739 __unused int *fhlenp,
4740 __unused unsigned char *fhp,
4741 __unused vfs_context_t ctx)
4742 {
4743
4744 return (ENOTSUP);
4745 }
4746
4747 /*
4748 * Vfs start routine, a no-op.
4749 */
4750 /*ARGSUSED*/
4751 int
4752 nfs_vfs_start(
4753 __unused mount_t mp,
4754 __unused int flags,
4755 __unused vfs_context_t ctx)
4756 {
4757
4758 return (0);
4759 }
4760
4761 /*
4762 * Build the mount info buffer for NFS_MOUNTINFO.
4763 */
4764 int
4765 nfs_mountinfo_assemble(struct nfsmount *nmp, struct xdrbuf *xb)
4766 {
4767 struct xdrbuf xbinfo, xborig;
4768 char sotype[6];
4769 uint32_t origargsvers, origargslength;
4770 uint32_t infolength_offset, curargsopaquelength_offset, curargslength_offset, attrslength_offset, curargs_end_offset, end_offset;
4771 uint32_t miattrs[NFS_MIATTR_BITMAP_LEN];
4772 uint32_t miflags_mask[NFS_MIFLAG_BITMAP_LEN];
4773 uint32_t miflags[NFS_MIFLAG_BITMAP_LEN];
4774 uint32_t mattrs[NFS_MATTR_BITMAP_LEN];
4775 uint32_t mflags_mask[NFS_MFLAG_BITMAP_LEN];
4776 uint32_t mflags[NFS_MFLAG_BITMAP_LEN];
4777 uint32_t loc, serv, addr, comp;
4778 int i, timeo, error = 0;
4779
4780 /* set up mount info attr and flag bitmaps */
4781 NFS_BITMAP_ZERO(miattrs, NFS_MIATTR_BITMAP_LEN);
4782 NFS_BITMAP_SET(miattrs, NFS_MIATTR_FLAGS);
4783 NFS_BITMAP_SET(miattrs, NFS_MIATTR_ORIG_ARGS);
4784 NFS_BITMAP_SET(miattrs, NFS_MIATTR_CUR_ARGS);
4785 NFS_BITMAP_SET(miattrs, NFS_MIATTR_CUR_LOC_INDEX);
4786 NFS_BITMAP_ZERO(miflags_mask, NFS_MIFLAG_BITMAP_LEN);
4787 NFS_BITMAP_ZERO(miflags, NFS_MIFLAG_BITMAP_LEN);
4788 NFS_BITMAP_SET(miflags_mask, NFS_MIFLAG_DEAD);
4789 NFS_BITMAP_SET(miflags_mask, NFS_MIFLAG_NOTRESP);
4790 NFS_BITMAP_SET(miflags_mask, NFS_MIFLAG_RECOVERY);
4791 if (nmp->nm_state & NFSSTA_DEAD)
4792 NFS_BITMAP_SET(miflags, NFS_MIFLAG_DEAD);
4793 if ((nmp->nm_state & (NFSSTA_TIMEO|NFSSTA_JUKEBOXTIMEO)) ||
4794 ((nmp->nm_state & NFSSTA_LOCKTIMEO) && (nmp->nm_lockmode == NFS_LOCK_MODE_ENABLED)))
4795 NFS_BITMAP_SET(miflags, NFS_MIFLAG_NOTRESP);
4796 if (nmp->nm_state & NFSSTA_RECOVER)
4797 NFS_BITMAP_SET(miflags, NFS_MIFLAG_RECOVERY);
4798
4799 /* get original mount args length */
4800 xb_init_buffer(&xborig, nmp->nm_args, 2*XDRWORD);
4801 xb_get_32(error, &xborig, origargsvers); /* version */
4802 xb_get_32(error, &xborig, origargslength); /* args length */
4803 nfsmerr_if(error);
4804
4805 /* set up current mount attributes bitmap */
4806 NFS_BITMAP_ZERO(mattrs, NFS_MATTR_BITMAP_LEN);
4807 NFS_BITMAP_SET(mattrs, NFS_MATTR_FLAGS);
4808 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_VERSION);
4809 if (nmp->nm_vers >= NFS_VER4)
4810 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_MINOR_VERSION);
4811 NFS_BITMAP_SET(mattrs, NFS_MATTR_READ_SIZE);
4812 NFS_BITMAP_SET(mattrs, NFS_MATTR_WRITE_SIZE);
4813 NFS_BITMAP_SET(mattrs, NFS_MATTR_READDIR_SIZE);
4814 NFS_BITMAP_SET(mattrs, NFS_MATTR_READAHEAD);
4815 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_REG_MIN);
4816 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_REG_MAX);
4817 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MIN);
4818 NFS_BITMAP_SET(mattrs, NFS_MATTR_ATTRCACHE_DIR_MAX);
4819 NFS_BITMAP_SET(mattrs, NFS_MATTR_LOCK_MODE);
4820 NFS_BITMAP_SET(mattrs, NFS_MATTR_SECURITY);
4821 NFS_BITMAP_SET(mattrs, NFS_MATTR_MAX_GROUP_LIST);
4822 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOCKET_TYPE);
4823 NFS_BITMAP_SET(mattrs, NFS_MATTR_NFS_PORT);
4824 if ((nmp->nm_vers < NFS_VER4) && nmp->nm_mountport)
4825 NFS_BITMAP_SET(mattrs, NFS_MATTR_MOUNT_PORT);
4826 NFS_BITMAP_SET(mattrs, NFS_MATTR_REQUEST_TIMEOUT);
4827 if (NMFLAG(nmp, SOFT))
4828 NFS_BITMAP_SET(mattrs, NFS_MATTR_SOFT_RETRY_COUNT);
4829 if (nmp->nm_deadtimeout)
4830 NFS_BITMAP_SET(mattrs, NFS_MATTR_DEAD_TIMEOUT);
4831 if (nmp->nm_fh)
4832 NFS_BITMAP_SET(mattrs, NFS_MATTR_FH);
4833 NFS_BITMAP_SET(mattrs, NFS_MATTR_FS_LOCATIONS);
4834 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFLAGS);
4835 if (origargsvers < NFS_ARGSVERSION_XDR)
4836 NFS_BITMAP_SET(mattrs, NFS_MATTR_MNTFROM);
4837
4838 /* set up current mount flags bitmap */
4839 /* first set the flags that we will be setting - either on OR off */
4840 NFS_BITMAP_ZERO(mflags_mask, NFS_MFLAG_BITMAP_LEN);
4841 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_SOFT);
4842 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_INTR);
4843 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RESVPORT);
4844 if (nmp->nm_sotype == SOCK_DGRAM)
4845 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOCONNECT);
4846 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_DUMBTIMER);
4847 if (nmp->nm_vers < NFS_VER4)
4848 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_CALLUMNT);
4849 if (nmp->nm_vers >= NFS_VER3)
4850 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_RDIRPLUS);
4851 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NONEGNAMECACHE);
4852 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_MUTEJUKEBOX);
4853 if (nmp->nm_vers >= NFS_VER4) {
4854 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_EPHEMERAL);
4855 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOCALLBACK);
4856 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NONAMEDATTR);
4857 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOACL);
4858 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_ACLONLY);
4859 }
4860 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NFC);
4861 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_NOQUOTA);
4862 if (nmp->nm_vers < NFS_VER4)
4863 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_MNTUDP);
4864 NFS_BITMAP_SET(mflags_mask, NFS_MFLAG_MNTQUICK);
4865 /* now set the flags that should be set */
4866 NFS_BITMAP_ZERO(mflags, NFS_MFLAG_BITMAP_LEN);
4867 if (NMFLAG(nmp, SOFT))
4868 NFS_BITMAP_SET(mflags, NFS_MFLAG_SOFT);
4869 if (NMFLAG(nmp, INTR))
4870 NFS_BITMAP_SET(mflags, NFS_MFLAG_INTR);
4871 if (NMFLAG(nmp, RESVPORT))
4872 NFS_BITMAP_SET(mflags, NFS_MFLAG_RESVPORT);
4873 if ((nmp->nm_sotype == SOCK_DGRAM) && NMFLAG(nmp, NOCONNECT))
4874 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOCONNECT);
4875 if (NMFLAG(nmp, DUMBTIMER))
4876 NFS_BITMAP_SET(mflags, NFS_MFLAG_DUMBTIMER);
4877 if ((nmp->nm_vers < NFS_VER4) && NMFLAG(nmp, CALLUMNT))
4878 NFS_BITMAP_SET(mflags, NFS_MFLAG_CALLUMNT);
4879 if ((nmp->nm_vers >= NFS_VER3) && NMFLAG(nmp, RDIRPLUS))
4880 NFS_BITMAP_SET(mflags, NFS_MFLAG_RDIRPLUS);
4881 if (NMFLAG(nmp, NONEGNAMECACHE))
4882 NFS_BITMAP_SET(mflags, NFS_MFLAG_NONEGNAMECACHE);
4883 if (NMFLAG(nmp, MUTEJUKEBOX))
4884 NFS_BITMAP_SET(mflags, NFS_MFLAG_MUTEJUKEBOX);
4885 if (nmp->nm_vers >= NFS_VER4) {
4886 if (NMFLAG(nmp, EPHEMERAL))
4887 NFS_BITMAP_SET(mflags, NFS_MFLAG_EPHEMERAL);
4888 if (NMFLAG(nmp, NOCALLBACK))
4889 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOCALLBACK);
4890 if (NMFLAG(nmp, NONAMEDATTR))
4891 NFS_BITMAP_SET(mflags, NFS_MFLAG_NONAMEDATTR);
4892 if (NMFLAG(nmp, NOACL))
4893 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOACL);
4894 if (NMFLAG(nmp, ACLONLY))
4895 NFS_BITMAP_SET(mflags, NFS_MFLAG_ACLONLY);
4896 }
4897 if (NMFLAG(nmp, NFC))
4898 NFS_BITMAP_SET(mflags, NFS_MFLAG_NFC);
4899 if (NMFLAG(nmp, NOQUOTA) || ((nmp->nm_vers >= NFS_VER4) &&
4900 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_HARD) &&
4901 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_SOFT) &&
4902 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_USED)))
4903 NFS_BITMAP_SET(mflags, NFS_MFLAG_NOQUOTA);
4904 if ((nmp->nm_vers < NFS_VER4) && NMFLAG(nmp, MNTUDP))
4905 NFS_BITMAP_SET(mflags, NFS_MFLAG_MNTUDP);
4906 if (NMFLAG(nmp, MNTQUICK))
4907 NFS_BITMAP_SET(mflags, NFS_MFLAG_MNTQUICK);
4908
4909 /* assemble info buffer: */
4910 xb_init_buffer(&xbinfo, NULL, 0);
4911 xb_add_32(error, &xbinfo, NFS_MOUNT_INFO_VERSION);
4912 infolength_offset = xb_offset(&xbinfo);
4913 xb_add_32(error, &xbinfo, 0);
4914 xb_add_bitmap(error, &xbinfo, miattrs, NFS_MIATTR_BITMAP_LEN);
4915 xb_add_bitmap(error, &xbinfo, miflags, NFS_MIFLAG_BITMAP_LEN);
4916 xb_add_32(error, &xbinfo, origargslength);
4917 if (!error)
4918 error = xb_add_bytes(&xbinfo, nmp->nm_args, origargslength, 0);
4919
4920 /* the opaque byte count for the current mount args values: */
4921 curargsopaquelength_offset = xb_offset(&xbinfo);
4922 xb_add_32(error, &xbinfo, 0);
4923
4924 /* Encode current mount args values */
4925 xb_add_32(error, &xbinfo, NFS_ARGSVERSION_XDR);
4926 curargslength_offset = xb_offset(&xbinfo);
4927 xb_add_32(error, &xbinfo, 0);
4928 xb_add_32(error, &xbinfo, NFS_XDRARGS_VERSION_0);
4929 xb_add_bitmap(error, &xbinfo, mattrs, NFS_MATTR_BITMAP_LEN);
4930 attrslength_offset = xb_offset(&xbinfo);
4931 xb_add_32(error, &xbinfo, 0);
4932 xb_add_bitmap(error, &xbinfo, mflags_mask, NFS_MFLAG_BITMAP_LEN);
4933 xb_add_bitmap(error, &xbinfo, mflags, NFS_MFLAG_BITMAP_LEN);
4934 xb_add_32(error, &xbinfo, nmp->nm_vers); /* NFS_VERSION */
4935 if (nmp->nm_vers >= NFS_VER4)
4936 xb_add_32(error, &xbinfo, 0); /* NFS_MINOR_VERSION */
4937 xb_add_32(error, &xbinfo, nmp->nm_rsize); /* READ_SIZE */
4938 xb_add_32(error, &xbinfo, nmp->nm_wsize); /* WRITE_SIZE */
4939 xb_add_32(error, &xbinfo, nmp->nm_readdirsize); /* READDIR_SIZE */
4940 xb_add_32(error, &xbinfo, nmp->nm_readahead); /* READAHEAD */
4941 xb_add_32(error, &xbinfo, nmp->nm_acregmin); /* ATTRCACHE_REG_MIN */
4942 xb_add_32(error, &xbinfo, 0); /* ATTRCACHE_REG_MIN */
4943 xb_add_32(error, &xbinfo, nmp->nm_acregmax); /* ATTRCACHE_REG_MAX */
4944 xb_add_32(error, &xbinfo, 0); /* ATTRCACHE_REG_MAX */
4945 xb_add_32(error, &xbinfo, nmp->nm_acdirmin); /* ATTRCACHE_DIR_MIN */
4946 xb_add_32(error, &xbinfo, 0); /* ATTRCACHE_DIR_MIN */
4947 xb_add_32(error, &xbinfo, nmp->nm_acdirmax); /* ATTRCACHE_DIR_MAX */
4948 xb_add_32(error, &xbinfo, 0); /* ATTRCACHE_DIR_MAX */
4949 xb_add_32(error, &xbinfo, nmp->nm_lockmode); /* LOCK_MODE */
4950 if (nmp->nm_sec.count) {
4951 xb_add_32(error, &xbinfo, nmp->nm_sec.count); /* SECURITY */
4952 nfsmerr_if(error);
4953 for (i=0; i < nmp->nm_sec.count; i++)
4954 xb_add_32(error, &xbinfo, nmp->nm_sec.flavors[i]);
4955 } else if (nmp->nm_servsec.count) {
4956 xb_add_32(error, &xbinfo, nmp->nm_servsec.count); /* SECURITY */
4957 nfsmerr_if(error);
4958 for (i=0; i < nmp->nm_servsec.count; i++)
4959 xb_add_32(error, &xbinfo, nmp->nm_servsec.flavors[i]);
4960 } else {
4961 xb_add_32(error, &xbinfo, 1); /* SECURITY */
4962 xb_add_32(error, &xbinfo, nmp->nm_auth);
4963 }
4964 xb_add_32(error, &xbinfo, nmp->nm_numgrps); /* MAX_GROUP_LIST */
4965 nfsmerr_if(error);
4966 snprintf(sotype, sizeof(sotype), "%s%s", (nmp->nm_sotype == SOCK_DGRAM) ? "udp" : "tcp",
4967 nmp->nm_sofamily ? (nmp->nm_sofamily == AF_INET) ? "4" : "6" : "");
4968 xb_add_string(error, &xbinfo, sotype, strlen(sotype)); /* SOCKET_TYPE */
4969 xb_add_32(error, &xbinfo, ntohs(((struct sockaddr_in*)nmp->nm_saddr)->sin_port)); /* NFS_PORT */
4970 if ((nmp->nm_vers < NFS_VER4) && nmp->nm_mountport)
4971 xb_add_32(error, &xbinfo, nmp->nm_mountport); /* MOUNT_PORT */
4972 timeo = (nmp->nm_timeo * 10) / NFS_HZ;
4973 xb_add_32(error, &xbinfo, timeo/10); /* REQUEST_TIMEOUT */
4974 xb_add_32(error, &xbinfo, (timeo%10)*100000000); /* REQUEST_TIMEOUT */
4975 if (NMFLAG(nmp, SOFT))
4976 xb_add_32(error, &xbinfo, nmp->nm_retry); /* SOFT_RETRY_COUNT */
4977 if (nmp->nm_deadtimeout) {
4978 xb_add_32(error, &xbinfo, nmp->nm_deadtimeout); /* DEAD_TIMEOUT */
4979 xb_add_32(error, &xbinfo, 0); /* DEAD_TIMEOUT */
4980 }
4981 if (nmp->nm_fh)
4982 xb_add_fh(error, &xbinfo, &nmp->nm_fh->fh_data[0], nmp->nm_fh->fh_len); /* FH */
4983 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_numlocs); /* FS_LOCATIONS */
4984 for (loc = 0; !error && (loc < nmp->nm_locations.nl_numlocs); loc++) {
4985 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_servcount);
4986 for (serv = 0; !error && (serv < nmp->nm_locations.nl_locations[loc]->nl_servcount); serv++) {
4987 xb_add_string(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_name,
4988 strlen(nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_name));
4989 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addrcount);
4990 for (addr = 0; !error && (addr < nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addrcount); addr++)
4991 xb_add_string(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr],
4992 strlen(nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addresses[addr]));
4993 xb_add_32(error, &xbinfo, 0); /* empty server info */
4994 }
4995 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_path.np_compcount);
4996 for (comp = 0; !error && (comp < nmp->nm_locations.nl_locations[loc]->nl_path.np_compcount); comp++)
4997 xb_add_string(error, &xbinfo, nmp->nm_locations.nl_locations[loc]->nl_path.np_components[comp],
4998 strlen(nmp->nm_locations.nl_locations[loc]->nl_path.np_components[comp]));
4999 xb_add_32(error, &xbinfo, 0); /* empty fs location info */
5000 }
5001 xb_add_32(error, &xbinfo, vfs_flags(nmp->nm_mountp)); /* MNTFLAGS */
5002 if (origargsvers < NFS_ARGSVERSION_XDR)
5003 xb_add_string(error, &xbinfo, vfs_statfs(nmp->nm_mountp)->f_mntfromname,
5004 strlen(vfs_statfs(nmp->nm_mountp)->f_mntfromname)); /* MNTFROM */
5005 curargs_end_offset = xb_offset(&xbinfo);
5006
5007 /* NFS_MIATTR_CUR_LOC_INDEX */
5008 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_current.nli_flags);
5009 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_current.nli_loc);
5010 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_current.nli_serv);
5011 xb_add_32(error, &xbinfo, nmp->nm_locations.nl_current.nli_addr);
5012
5013 xb_build_done(error, &xbinfo);
5014
5015 /* update opaque counts */
5016 end_offset = xb_offset(&xbinfo);
5017 if (!error) {
5018 error = xb_seek(&xbinfo, attrslength_offset);
5019 xb_add_32(error, &xbinfo, curargs_end_offset - attrslength_offset - XDRWORD/*don't include length field*/);
5020 }
5021 if (!error) {
5022 error = xb_seek(&xbinfo, curargslength_offset);
5023 xb_add_32(error, &xbinfo, curargs_end_offset - curargslength_offset + XDRWORD/*version*/);
5024 }
5025 if (!error) {
5026 error = xb_seek(&xbinfo, curargsopaquelength_offset);
5027 xb_add_32(error, &xbinfo, curargs_end_offset - curargslength_offset + XDRWORD/*version*/);
5028 }
5029 if (!error) {
5030 error = xb_seek(&xbinfo, infolength_offset);
5031 xb_add_32(error, &xbinfo, end_offset - infolength_offset + XDRWORD/*version*/);
5032 }
5033 nfsmerr_if(error);
5034
5035 /* copy result xdrbuf to caller */
5036 *xb = xbinfo;
5037
5038 /* and mark the local copy as not needing cleanup */
5039 xbinfo.xb_flags &= ~XB_CLEANUP;
5040 nfsmerr:
5041 xb_cleanup(&xbinfo);
5042 return (error);
5043 }
5044
5045 /*
5046 * Do that sysctl thang...
5047 */
5048 int
5049 nfs_vfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
5050 user_addr_t newp, size_t newlen, vfs_context_t ctx)
5051 {
5052 int error = 0, val, softnobrowse;
5053 struct sysctl_req *req = NULL;
5054 union union_vfsidctl vc;
5055 mount_t mp;
5056 struct nfsmount *nmp = NULL;
5057 struct vfsquery vq;
5058 boolean_t is_64_bit;
5059 fsid_t fsid;
5060 struct xdrbuf xb;
5061 #if NFSSERVER
5062 struct nfs_exportfs *nxfs;
5063 struct nfs_export *nx;
5064 struct nfs_active_user_list *ulist;
5065 struct nfs_export_stat_desc stat_desc;
5066 struct nfs_export_stat_rec statrec;
5067 struct nfs_user_stat_node *unode, *unode_next;
5068 struct nfs_user_stat_desc ustat_desc;
5069 struct nfs_user_stat_user_rec ustat_rec;
5070 struct nfs_user_stat_path_rec upath_rec;
5071 uint bytes_avail, bytes_total, recs_copied;
5072 uint numExports, totlen, pos, numRecs, count;
5073 #endif /* NFSSERVER */
5074
5075 /*
5076 * All names at this level are terminal.
5077 */
5078 if (namelen > 1)
5079 return (ENOTDIR); /* overloaded */
5080
5081 is_64_bit = vfs_context_is64bit(ctx);
5082
5083 /* common code for "new style" VFS_CTL sysctl, get the mount. */
5084 switch (name[0]) {
5085 case VFS_CTL_TIMEO:
5086 case VFS_CTL_QUERY:
5087 case VFS_CTL_NOLOCKS:
5088 req = CAST_DOWN(struct sysctl_req *, oldp);
5089 error = SYSCTL_IN(req, &vc, is_64_bit? sizeof(vc.vc64):sizeof(vc.vc32));
5090 if (error)
5091 return (error);
5092 mp = vfs_getvfs(&vc.vc32.vc_fsid); /* works for 32 and 64 */
5093 if (mp == NULL)
5094 return (ENOENT);
5095 nmp = VFSTONFS(mp);
5096 if (nmp == NULL)
5097 return (ENOENT);
5098 bzero(&vq, sizeof(vq));
5099 req->newidx = 0;
5100 if (is_64_bit) {
5101 req->newptr = vc.vc64.vc_ptr;
5102 req->newlen = (size_t)vc.vc64.vc_len;
5103 } else {
5104 req->newptr = CAST_USER_ADDR_T(vc.vc32.vc_ptr);
5105 req->newlen = vc.vc32.vc_len;
5106 }
5107 }
5108
5109 switch(name[0]) {
5110 case NFS_NFSSTATS:
5111 if (!oldp) {
5112 *oldlenp = sizeof nfsstats;
5113 return (0);
5114 }
5115
5116 if (*oldlenp < sizeof nfsstats) {
5117 *oldlenp = sizeof nfsstats;
5118 return (ENOMEM);
5119 }
5120
5121 error = copyout(&nfsstats, oldp, sizeof nfsstats);
5122 if (error)
5123 return (error);
5124
5125 if (newp && newlen != sizeof nfsstats)
5126 return (EINVAL);
5127
5128 if (newp)
5129 return copyin(newp, &nfsstats, sizeof nfsstats);
5130 return (0);
5131 case NFS_MOUNTINFO:
5132 /* read in the fsid */
5133 if (*oldlenp < sizeof(fsid))
5134 return (EINVAL);
5135 if ((error = copyin(oldp, &fsid, sizeof(fsid))))
5136 return (error);
5137 /* swizzle it back to host order */
5138 fsid.val[0] = ntohl(fsid.val[0]);
5139 fsid.val[1] = ntohl(fsid.val[1]);
5140 /* find mount and make sure it's NFS */
5141 if (((mp = vfs_getvfs(&fsid))) == NULL)
5142 return (ENOENT);
5143 if (strcmp(mp->mnt_vfsstat.f_fstypename, "nfs"))
5144 return (EINVAL);
5145 if (((nmp = VFSTONFS(mp))) == NULL)
5146 return (ENOENT);
5147 xb_init(&xb, 0);
5148 if ((error = nfs_mountinfo_assemble(nmp, &xb)))
5149 return (error);
5150 if (*oldlenp < xb.xb_u.xb_buffer.xbb_len)
5151 error = ENOMEM;
5152 else
5153 error = copyout(xb_buffer_base(&xb), oldp, xb.xb_u.xb_buffer.xbb_len);
5154 *oldlenp = xb.xb_u.xb_buffer.xbb_len;
5155 xb_cleanup(&xb);
5156 break;
5157 #if NFSSERVER
5158 case NFS_EXPORTSTATS:
5159 /* setup export stat descriptor */
5160 stat_desc.rec_vers = NFS_EXPORT_STAT_REC_VERSION;
5161
5162 if (!nfsrv_is_initialized()) {
5163 stat_desc.rec_count = 0;
5164 if (oldp && (*oldlenp >= sizeof(struct nfs_export_stat_desc)))
5165 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
5166 *oldlenp = sizeof(struct nfs_export_stat_desc);
5167 return (error);
5168 }
5169
5170 /* Count the number of exported directories */
5171 lck_rw_lock_shared(&nfsrv_export_rwlock);
5172 numExports = 0;
5173 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next)
5174 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next)
5175 numExports += 1;
5176
5177 /* update stat descriptor's export record count */
5178 stat_desc.rec_count = numExports;
5179
5180 /* calculate total size of required buffer */
5181 totlen = sizeof(struct nfs_export_stat_desc) + (numExports * sizeof(struct nfs_export_stat_rec));
5182
5183 /* Check caller's buffer */
5184 if (oldp == 0) {
5185 lck_rw_done(&nfsrv_export_rwlock);
5186 /* indicate required buffer len */
5187 *oldlenp = totlen;
5188 return (0);
5189 }
5190
5191 /* We require the caller's buffer to be at least large enough to hold the descriptor */
5192 if (*oldlenp < sizeof(struct nfs_export_stat_desc)) {
5193 lck_rw_done(&nfsrv_export_rwlock);
5194 /* indicate required buffer len */
5195 *oldlenp = totlen;
5196 return (ENOMEM);
5197 }
5198
5199 /* indicate required buffer len */
5200 *oldlenp = totlen;
5201
5202 /* check if export table is empty */
5203 if (!numExports) {
5204 lck_rw_done(&nfsrv_export_rwlock);
5205 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
5206 return (error);
5207 }
5208
5209 /* calculate how many actual export stat records fit into caller's buffer */
5210 numRecs = (*oldlenp - sizeof(struct nfs_export_stat_desc)) / sizeof(struct nfs_export_stat_rec);
5211
5212 if (!numRecs) {
5213 /* caller's buffer can only accomodate descriptor */
5214 lck_rw_done(&nfsrv_export_rwlock);
5215 stat_desc.rec_count = 0;
5216 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
5217 return (error);
5218 }
5219
5220 /* adjust to actual number of records to copyout to caller's buffer */
5221 if (numRecs > numExports)
5222 numRecs = numExports;
5223
5224 /* set actual number of records we are returning */
5225 stat_desc.rec_count = numRecs;
5226
5227 /* first copy out the stat descriptor */
5228 pos = 0;
5229 error = copyout(&stat_desc, oldp + pos, sizeof(struct nfs_export_stat_desc));
5230 if (error) {
5231 lck_rw_done(&nfsrv_export_rwlock);
5232 return (error);
5233 }
5234 pos += sizeof(struct nfs_export_stat_desc);
5235
5236 /* Loop through exported directories */
5237 count = 0;
5238 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
5239 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
5240
5241 if (count >= numRecs)
5242 break;
5243
5244 /* build exported filesystem path */
5245 snprintf(statrec.path, sizeof(statrec.path), "%s%s%s",
5246 nxfs->nxfs_path, ((nxfs->nxfs_path[1] && nx->nx_path[0]) ? "/" : ""),
5247 nx->nx_path);
5248
5249 /* build the 64-bit export stat counters */
5250 statrec.ops = ((uint64_t)nx->nx_stats.ops.hi << 32) |
5251 nx->nx_stats.ops.lo;
5252 statrec.bytes_read = ((uint64_t)nx->nx_stats.bytes_read.hi << 32) |
5253 nx->nx_stats.bytes_read.lo;
5254 statrec.bytes_written = ((uint64_t)nx->nx_stats.bytes_written.hi << 32) |
5255 nx->nx_stats.bytes_written.lo;
5256 error = copyout(&statrec, oldp + pos, sizeof(statrec));
5257 if (error) {
5258 lck_rw_done(&nfsrv_export_rwlock);
5259 return (error);
5260 }
5261 /* advance buffer position */
5262 pos += sizeof(statrec);
5263 }
5264 }
5265 lck_rw_done(&nfsrv_export_rwlock);
5266 break;
5267 case NFS_USERSTATS:
5268 /* init structures used for copying out of kernel */
5269 ustat_desc.rec_vers = NFS_USER_STAT_REC_VERSION;
5270 ustat_rec.rec_type = NFS_USER_STAT_USER_REC;
5271 upath_rec.rec_type = NFS_USER_STAT_PATH_REC;
5272
5273 /* initialize counters */
5274 bytes_total = sizeof(struct nfs_user_stat_desc);
5275 bytes_avail = *oldlenp;
5276 recs_copied = 0;
5277
5278 if (!nfsrv_is_initialized()) /* NFS server not initialized, so no stats */
5279 goto ustat_skip;
5280
5281 /* reclaim old expired user nodes */
5282 nfsrv_active_user_list_reclaim();
5283
5284 /* reserve space for the buffer descriptor */
5285 if (bytes_avail >= sizeof(struct nfs_user_stat_desc))
5286 bytes_avail -= sizeof(struct nfs_user_stat_desc);
5287 else
5288 bytes_avail = 0;
5289
5290 /* put buffer position past the buffer descriptor */
5291 pos = sizeof(struct nfs_user_stat_desc);
5292
5293 /* Loop through exported directories */
5294 lck_rw_lock_shared(&nfsrv_export_rwlock);
5295 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
5296 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
5297 /* copy out path */
5298 if (bytes_avail >= sizeof(struct nfs_user_stat_path_rec)) {
5299 snprintf(upath_rec.path, sizeof(upath_rec.path), "%s%s%s",
5300 nxfs->nxfs_path, ((nxfs->nxfs_path[1] && nx->nx_path[0]) ? "/" : ""),
5301 nx->nx_path);
5302
5303 error = copyout(&upath_rec, oldp + pos, sizeof(struct nfs_user_stat_path_rec));
5304 if (error) {
5305 /* punt */
5306 goto ustat_done;
5307 }
5308
5309 pos += sizeof(struct nfs_user_stat_path_rec);
5310 bytes_avail -= sizeof(struct nfs_user_stat_path_rec);
5311 recs_copied++;
5312 }
5313 else {
5314 /* Caller's buffer is exhausted */
5315 bytes_avail = 0;
5316 }
5317
5318 bytes_total += sizeof(struct nfs_user_stat_path_rec);
5319
5320 /* Scan through all user nodes of this export */
5321 ulist = &nx->nx_user_list;
5322 lck_mtx_lock(&ulist->user_mutex);
5323 for (unode = TAILQ_FIRST(&ulist->user_lru); unode; unode = unode_next) {
5324 unode_next = TAILQ_NEXT(unode, lru_link);
5325
5326 /* copy out node if there is space */
5327 if (bytes_avail >= sizeof(struct nfs_user_stat_user_rec)) {
5328 /* prepare a user stat rec for copying out */
5329 ustat_rec.uid = unode->uid;
5330 bcopy(&unode->sock, &ustat_rec.sock, unode->sock.ss_len);
5331 ustat_rec.ops = unode->ops;
5332 ustat_rec.bytes_read = unode->bytes_read;
5333 ustat_rec.bytes_written = unode->bytes_written;
5334 ustat_rec.tm_start = unode->tm_start;
5335 ustat_rec.tm_last = unode->tm_last;
5336
5337 error = copyout(&ustat_rec, oldp + pos, sizeof(struct nfs_user_stat_user_rec));
5338
5339 if (error) {
5340 /* punt */
5341 lck_mtx_unlock(&ulist->user_mutex);
5342 goto ustat_done;
5343 }
5344
5345 pos += sizeof(struct nfs_user_stat_user_rec);
5346 bytes_avail -= sizeof(struct nfs_user_stat_user_rec);
5347 recs_copied++;
5348 }
5349 else {
5350 /* Caller's buffer is exhausted */
5351 bytes_avail = 0;
5352 }
5353 bytes_total += sizeof(struct nfs_user_stat_user_rec);
5354 }
5355 /* can unlock this export's list now */
5356 lck_mtx_unlock(&ulist->user_mutex);
5357 }
5358 }
5359
5360 ustat_done:
5361 /* unlock the export table */
5362 lck_rw_done(&nfsrv_export_rwlock);
5363
5364 ustat_skip:
5365 /* indicate number of actual records copied */
5366 ustat_desc.rec_count = recs_copied;
5367
5368 if (!error) {
5369 /* check if there was enough room for the buffer descriptor */
5370 if (*oldlenp >= sizeof(struct nfs_user_stat_desc))
5371 error = copyout(&ustat_desc, oldp, sizeof(struct nfs_user_stat_desc));
5372 else
5373 error = ENOMEM;
5374
5375 /* always indicate required buffer size */
5376 *oldlenp = bytes_total;
5377 }
5378 break;
5379 case NFS_USERCOUNT:
5380 if (!oldp) {
5381 *oldlenp = sizeof(nfsrv_user_stat_node_count);
5382 return (0);
5383 }
5384
5385 if (*oldlenp < sizeof(nfsrv_user_stat_node_count)) {
5386 *oldlenp = sizeof(nfsrv_user_stat_node_count);
5387 return (ENOMEM);
5388 }
5389
5390 if (nfsrv_is_initialized()) {
5391 /* reclaim old expired user nodes */
5392 nfsrv_active_user_list_reclaim();
5393 }
5394
5395 error = copyout(&nfsrv_user_stat_node_count, oldp, sizeof(nfsrv_user_stat_node_count));
5396 break;
5397 #endif /* NFSSERVER */
5398 case VFS_CTL_NOLOCKS:
5399 if (req->oldptr != USER_ADDR_NULL) {
5400 lck_mtx_lock(&nmp->nm_lock);
5401 val = (nmp->nm_lockmode == NFS_LOCK_MODE_DISABLED) ? 1 : 0;
5402 lck_mtx_unlock(&nmp->nm_lock);
5403 error = SYSCTL_OUT(req, &val, sizeof(val));
5404 if (error)
5405 return (error);
5406 }
5407 if (req->newptr != USER_ADDR_NULL) {
5408 error = SYSCTL_IN(req, &val, sizeof(val));
5409 if (error)
5410 return (error);
5411 lck_mtx_lock(&nmp->nm_lock);
5412 if (nmp->nm_lockmode == NFS_LOCK_MODE_LOCAL) {
5413 /* can't toggle locks when using local locks */
5414 error = EINVAL;
5415 } else if ((nmp->nm_vers >= NFS_VER4) && val) {
5416 /* can't disable locks for NFSv4 */
5417 error = EINVAL;
5418 } else if (val) {
5419 if ((nmp->nm_vers <= NFS_VER3) && (nmp->nm_lockmode == NFS_LOCK_MODE_ENABLED))
5420 nfs_lockd_mount_unregister(nmp);
5421 nmp->nm_lockmode = NFS_LOCK_MODE_DISABLED;
5422 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
5423 } else {
5424 if ((nmp->nm_vers <= NFS_VER3) && (nmp->nm_lockmode == NFS_LOCK_MODE_DISABLED))
5425 nfs_lockd_mount_register(nmp);
5426 nmp->nm_lockmode = NFS_LOCK_MODE_ENABLED;
5427 }
5428 lck_mtx_unlock(&nmp->nm_lock);
5429 }
5430 break;
5431 case VFS_CTL_QUERY:
5432 lck_mtx_lock(&nmp->nm_lock);
5433 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
5434 softnobrowse = (NMFLAG(nmp, SOFT) && (vfs_flags(nmp->nm_mountp) & MNT_DONTBROWSE));
5435 if (!softnobrowse && (nmp->nm_state & NFSSTA_TIMEO))
5436 vq.vq_flags |= VQ_NOTRESP;
5437 if (!softnobrowse && (nmp->nm_state & NFSSTA_JUKEBOXTIMEO) && !NMFLAG(nmp, MUTEJUKEBOX))
5438 vq.vq_flags |= VQ_NOTRESP;
5439 if (!softnobrowse && (nmp->nm_state & NFSSTA_LOCKTIMEO) &&
5440 (nmp->nm_lockmode == NFS_LOCK_MODE_ENABLED))
5441 vq.vq_flags |= VQ_NOTRESP;
5442 if (nmp->nm_state & NFSSTA_DEAD)
5443 vq.vq_flags |= VQ_DEAD;
5444 lck_mtx_unlock(&nmp->nm_lock);
5445 error = SYSCTL_OUT(req, &vq, sizeof(vq));
5446 break;
5447 case VFS_CTL_TIMEO:
5448 if (req->oldptr != USER_ADDR_NULL) {
5449 lck_mtx_lock(&nmp->nm_lock);
5450 val = nmp->nm_tprintf_initial_delay;
5451 lck_mtx_unlock(&nmp->nm_lock);
5452 error = SYSCTL_OUT(req, &val, sizeof(val));
5453 if (error)
5454 return (error);
5455 }
5456 if (req->newptr != USER_ADDR_NULL) {
5457 error = SYSCTL_IN(req, &val, sizeof(val));
5458 if (error)
5459 return (error);
5460 lck_mtx_lock(&nmp->nm_lock);
5461 if (val < 0)
5462 nmp->nm_tprintf_initial_delay = 0;
5463 else
5464 nmp->nm_tprintf_initial_delay = val;
5465 lck_mtx_unlock(&nmp->nm_lock);
5466 }
5467 break;
5468 default:
5469 return (ENOTSUP);
5470 }
5471 return (error);
5472 }