2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
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 $
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,
74 #include <sys/param.h>
75 #include <sys/systm.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>
92 #include <libkern/OSAtomic.h>
95 #include <sys/vmparam.h>
97 #if !defined(NO_MOUNT_PRIVATE)
98 #include <sys/filedesc.h>
99 #endif /* NO_MOUNT_PRIVATE */
102 #include <net/route.h>
103 #include <netinet/in.h>
105 #include <nfs/rpcv2.h>
106 #include <nfs/krpc.h>
107 #include <nfs/nfsproto.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>
117 #include <security/mac_framework.h>
120 #include <pexpert/pexpert.h>
122 #define NFS_VFS_DBG(...) NFS_DBG(NFS_FAC_VFS, 7, ## __VA_ARGS__)
129 static lck_grp_t
*nfs_global_grp
, *nfs_mount_grp
;
130 lck_mtx_t
*nfs_global_mutex
;
131 uint32_t nfs_fs_attr_bitmap
[NFS_ATTR_BITMAP_LEN
];
132 uint32_t nfs_object_attr_bitmap
[NFS_ATTR_BITMAP_LEN
];
133 uint32_t nfs_getattr_bitmap
[NFS_ATTR_BITMAP_LEN
];
134 struct nfsclientidlist nfsclientids
;
137 struct nfs_reqqhead nfs_reqq
;
138 lck_grp_t
*nfs_request_grp
;
139 lck_mtx_t
*nfs_request_mutex
;
140 thread_call_t nfs_request_timer_call
;
141 int nfs_request_timer_on
;
142 u_int32_t nfs_xid
= 0;
143 u_int32_t nfs_xidwrap
= 0; /* to build a (non-wrapping) 64 bit xid */
145 thread_call_t nfs_buf_timer_call
;
148 lck_grp_t
*nfs_open_grp
;
149 uint32_t nfs_open_owner_seqnum
= 0;
150 uint32_t nfs_lock_owner_seqnum
= 0;
151 thread_call_t nfs4_callback_timer_call
;
152 int nfs4_callback_timer_on
= 0;
153 char nfs4_domain
[MAXPATHLEN
];
156 lck_grp_t
*nfsiod_lck_grp
;
157 lck_mtx_t
*nfsiod_mutex
;
158 struct nfsiodlist nfsiodfree
, nfsiodwork
;
159 struct nfsiodmountlist nfsiodmounts
;
160 int nfsiod_thread_count
= 0;
161 int nfsiod_thread_max
= NFS_DEFASYNCTHREAD
;
162 int nfs_max_async_writes
= NFS_DEFMAXASYNCWRITES
;
164 int nfs_iosize
= NFS_IOSIZE
;
165 int nfs_access_cache_timeout
= NFS_MAXATTRTIMO
;
166 int nfs_access_delete
= 1; /* too many servers get this wrong - workaround on by default */
167 int nfs_access_dotzfs
= 1;
168 int nfs_access_for_getattr
= 0;
169 int nfs_allow_async
= 0;
170 int nfs_statfs_rate_limit
= NFS_DEFSTATFSRATELIMIT
;
171 int nfs_lockd_mounts
= 0;
172 int nfs_lockd_request_sent
= 0;
173 int nfs_idmap_ctrl
= NFS_IDMAP_CTRL_USE_IDMAP_SERVICE
;
174 int nfs_callback_port
= 0;
176 int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
177 int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
180 int mountnfs(char *, mount_t
, vfs_context_t
, vnode_t
*);
181 static int nfs_mount_diskless(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*, vfs_context_t
);
182 #if !defined(NO_MOUNT_PRIVATE)
183 static int nfs_mount_diskless_private(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*, vfs_context_t
);
184 #endif /* NO_MOUNT_PRIVATE */
185 int nfs_mount_connect(struct nfsmount
*);
186 void nfs_mount_drain_and_cleanup(struct nfsmount
*);
187 void nfs_mount_cleanup(struct nfsmount
*);
188 int nfs_mountinfo_assemble(struct nfsmount
*, struct xdrbuf
*);
189 int nfs4_mount_update_path_with_symlink(struct nfsmount
*, struct nfs_fs_path
*, uint32_t, fhandle_t
*, int *, fhandle_t
*, vfs_context_t
);
192 * NFS VFS operations.
194 int nfs_vfs_mount(mount_t
, vnode_t
, user_addr_t
, vfs_context_t
);
195 int nfs_vfs_start(mount_t
, int, vfs_context_t
);
196 int nfs_vfs_unmount(mount_t
, int, vfs_context_t
);
197 int nfs_vfs_root(mount_t
, vnode_t
*, vfs_context_t
);
198 int nfs_vfs_quotactl(mount_t
, int, uid_t
, caddr_t
, vfs_context_t
);
199 int nfs_vfs_getattr(mount_t
, struct vfs_attr
*, vfs_context_t
);
200 int nfs_vfs_sync(mount_t
, int, vfs_context_t
);
201 int nfs_vfs_vget(mount_t
, ino64_t
, vnode_t
*, vfs_context_t
);
202 int nfs_vfs_vptofh(vnode_t
, int *, unsigned char *, vfs_context_t
);
203 int nfs_vfs_fhtovp(mount_t
, int, unsigned char *, vnode_t
*, vfs_context_t
);
204 int nfs_vfs_init(struct vfsconf
*);
205 int nfs_vfs_sysctl(int *, u_int
, user_addr_t
, size_t *, user_addr_t
, size_t, vfs_context_t
);
207 struct vfsops nfs_vfsops
= {
208 .vfs_mount
= nfs_vfs_mount
,
209 .vfs_start
= nfs_vfs_start
,
210 .vfs_unmount
= nfs_vfs_unmount
,
211 .vfs_root
= nfs_vfs_root
,
212 .vfs_quotactl
= nfs_vfs_quotactl
,
213 .vfs_getattr
= nfs_vfs_getattr
,
214 .vfs_sync
= nfs_vfs_sync
,
215 .vfs_vget
= nfs_vfs_vget
,
216 .vfs_fhtovp
= nfs_vfs_fhtovp
,
217 .vfs_vptofh
= nfs_vfs_vptofh
,
218 .vfs_init
= nfs_vfs_init
,
219 .vfs_sysctl
= nfs_vfs_sysctl
,
220 // We do not support the remaining VFS ops
225 * version-specific NFS functions
227 int nfs3_mount(struct nfsmount
*, vfs_context_t
, nfsnode_t
*);
228 int nfs4_mount(struct nfsmount
*, vfs_context_t
, nfsnode_t
*);
229 int nfs3_fsinfo(struct nfsmount
*, nfsnode_t
, vfs_context_t
);
230 int nfs3_update_statfs(struct nfsmount
*, vfs_context_t
);
231 int nfs4_update_statfs(struct nfsmount
*, vfs_context_t
);
233 #define nfs3_getquota NULL
234 #define nfs4_getquota NULL
236 int nfs3_getquota(struct nfsmount
*, vfs_context_t
, uid_t
, int, struct dqblk
*);
237 int nfs4_getquota(struct nfsmount
*, vfs_context_t
, uid_t
, int, struct dqblk
*);
240 struct nfs_funcs nfs3_funcs
= {
248 nfs3_read_rpc_async_finish
,
250 nfs3_write_rpc_async
,
251 nfs3_write_rpc_async_finish
,
253 nfs3_lookup_rpc_async
,
254 nfs3_lookup_rpc_async_finish
,
261 struct nfs_funcs nfs4_funcs
= {
269 nfs4_read_rpc_async_finish
,
271 nfs4_write_rpc_async
,
272 nfs4_write_rpc_async_finish
,
274 nfs4_lookup_rpc_async
,
275 nfs4_lookup_rpc_async_finish
,
284 * Called once to initialize data structures...
287 nfs_vfs_init(__unused
struct vfsconf
*vfsp
)
292 * Check to see if major data structures haven't bloated.
294 if (sizeof (struct nfsnode
) > NFS_NODEALLOC
) {
295 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC
);
296 printf("Try reducing NFS_SMALLFH\n");
298 if (sizeof (struct nfsmount
) > NFS_MNTALLOC
)
299 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC
);
301 nfs_ticks
= (hz
* NFS_TICKINTVL
+ 500) / 1000;
305 /* init async I/O thread pool state */
306 TAILQ_INIT(&nfsiodfree
);
307 TAILQ_INIT(&nfsiodwork
);
308 TAILQ_INIT(&nfsiodmounts
);
309 nfsiod_lck_grp
= lck_grp_alloc_init("nfsiod", LCK_GRP_ATTR_NULL
);
310 nfsiod_mutex
= lck_mtx_alloc_init(nfsiod_lck_grp
, LCK_ATTR_NULL
);
312 /* init lock groups, etc. */
313 nfs_mount_grp
= lck_grp_alloc_init("nfs_mount", LCK_GRP_ATTR_NULL
);
314 nfs_open_grp
= lck_grp_alloc_init("nfs_open", LCK_GRP_ATTR_NULL
);
315 nfs_global_grp
= lck_grp_alloc_init("nfs_global", LCK_GRP_ATTR_NULL
);
317 nfs_global_mutex
= lck_mtx_alloc_init(nfs_global_grp
, LCK_ATTR_NULL
);
319 /* init request list mutex */
320 nfs_request_grp
= lck_grp_alloc_init("nfs_request", LCK_GRP_ATTR_NULL
);
321 nfs_request_mutex
= lck_mtx_alloc_init(nfs_request_grp
, LCK_ATTR_NULL
);
323 /* initialize NFS request list */
324 TAILQ_INIT(&nfs_reqq
);
326 nfs_nbinit(); /* Init the nfsbuf table */
327 nfs_nhinit(); /* Init the nfsnode table */
328 nfs_lockinit(); /* Init the nfs lock state */
329 nfs_gss_init(); /* Init RPCSEC_GSS security */
332 NFS4_PER_FS_ATTRIBUTES(nfs_fs_attr_bitmap
);
333 NFS4_PER_OBJECT_ATTRIBUTES(nfs_object_attr_bitmap
);
334 NFS4_DEFAULT_ATTRIBUTES(nfs_getattr_bitmap
);
335 for (i
=0; i
< NFS_ATTR_BITMAP_LEN
; i
++)
336 nfs_getattr_bitmap
[i
] &= nfs_object_attr_bitmap
[i
];
337 TAILQ_INIT(&nfsclientids
);
339 /* initialize NFS timer callouts */
340 nfs_request_timer_call
= thread_call_allocate(nfs_request_timer
, NULL
);
341 nfs_buf_timer_call
= thread_call_allocate(nfs_buf_timer
, NULL
);
342 nfs4_callback_timer_call
= thread_call_allocate(nfs4_callback_timer
, NULL
);
351 nfs3_update_statfs(struct nfsmount
*nmp
, vfs_context_t ctx
)
354 int error
= 0, lockerror
, status
, nfsvers
;
356 struct nfsm_chain nmreq
, nmrep
;
359 nfsvers
= nmp
->nm_vers
;
363 if ((error
= vnode_get(NFSTOV(np
))))
366 nfsm_chain_null(&nmreq
);
367 nfsm_chain_null(&nmrep
);
369 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
370 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
371 nfsm_chain_build_done(error
, &nmreq
);
373 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_FSSTAT
, vfs_context_thread(ctx
),
374 vfs_context_ucred(ctx
), NULL
, R_SOFT
, &nmrep
, &xid
, &status
);
375 if (error
== ETIMEDOUT
)
377 if ((lockerror
= nfs_node_lock(np
)))
379 if (nfsvers
== NFS_VER3
)
380 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
385 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
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
);
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
);
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
);
413 nmp
->nm_fsattr
.nfsa_space_free
= (uint64_t)val
* nmp
->nm_fsattr
.nfsa_bsize
;
414 nfsm_chain_get_32(error
, &nmrep
, val
);
416 nmp
->nm_fsattr
.nfsa_space_avail
= (uint64_t)val
* nmp
->nm_fsattr
.nfsa_bsize
;
418 lck_mtx_unlock(&nmp
->nm_lock
);
420 nfsm_chain_cleanup(&nmreq
);
421 nfsm_chain_cleanup(&nmrep
);
422 vnode_put(NFSTOV(np
));
427 nfs4_update_statfs(struct nfsmount
*nmp
, vfs_context_t ctx
)
430 int error
= 0, lockerror
, status
, nfsvers
, numops
;
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
;
437 nfsvers
= nmp
->nm_vers
;
441 if ((error
= vnode_get(NFSTOV(np
))))
444 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
445 NVATTR_INIT(&nvattr
);
446 nfsm_chain_null(&nmreq
);
447 nfsm_chain_null(&nmrep
);
451 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
452 nfsm_chain_add_compound_header(error
, &nmreq
, "statfs", nmp
->nm_minor_vers
, 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
);
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
);
464 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
465 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
466 NULL
, R_SOFT
, &nmrep
, &xid
, &status
);
467 nfsm_chain_skip_tag(error
, &nmrep
);
468 nfsm_chain_get_32(error
, &nmrep
, numops
);
469 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
470 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
471 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
473 lck_mtx_lock(&nmp
->nm_lock
);
474 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, NULL
);
475 lck_mtx_unlock(&nmp
->nm_lock
);
477 if ((lockerror
= nfs_node_lock(np
)))
480 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
483 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
485 nmp
->nm_fsattr
.nfsa_bsize
= NFS_FABLKSIZE
;
487 NVATTR_CLEANUP(&nvattr
);
488 nfsm_chain_cleanup(&nmreq
);
489 nfsm_chain_cleanup(&nmrep
);
490 vnode_put(NFSTOV(np
));
496 * The NFS VFS_GETATTR function: "statfs"-type information is retrieved
497 * using the nf_update_statfs() function, and other attributes are cobbled
498 * together from whatever sources we can (getattr, fsinfo, pathconf).
501 nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t ctx
)
503 struct nfsmount
*nmp
;
505 int error
= 0, nfsvers
;
508 if (nfs_mount_gone(nmp
))
510 nfsvers
= nmp
->nm_vers
;
512 if (VFSATTR_IS_ACTIVE(fsap
, f_bsize
) ||
513 VFSATTR_IS_ACTIVE(fsap
, f_iosize
) ||
514 VFSATTR_IS_ACTIVE(fsap
, f_blocks
) ||
515 VFSATTR_IS_ACTIVE(fsap
, f_bfree
) ||
516 VFSATTR_IS_ACTIVE(fsap
, f_bavail
) ||
517 VFSATTR_IS_ACTIVE(fsap
, f_bused
) ||
518 VFSATTR_IS_ACTIVE(fsap
, f_files
) ||
519 VFSATTR_IS_ACTIVE(fsap
, f_ffree
)) {
520 int statfsrate
= nfs_statfs_rate_limit
;
524 * Are we rate-limiting statfs RPCs?
525 * (Treat values less than 1 or greater than 1,000,000 as no limit.)
527 if ((statfsrate
> 0) && (statfsrate
< 1000000)) {
532 lck_mtx_lock(&nmp
->nm_lock
);
533 stamp
= (now
.tv_sec
* statfsrate
) + (now
.tv_usec
/ (1000000/statfsrate
));
534 if (stamp
!= nmp
->nm_fsattrstamp
) {
536 nmp
->nm_fsattrstamp
= stamp
;
540 lck_mtx_unlock(&nmp
->nm_lock
);
543 if (refresh
&& !nfs_use_cache(nmp
))
544 error
= nmp
->nm_funcs
->nf_update_statfs(nmp
, ctx
);
545 if ((error
== ESTALE
) || (error
== ETIMEDOUT
))
550 lck_mtx_lock(&nmp
->nm_lock
);
551 VFSATTR_RETURN(fsap
, f_iosize
, nfs_iosize
);
552 VFSATTR_RETURN(fsap
, f_bsize
, nmp
->nm_fsattr
.nfsa_bsize
);
553 bsize
= nmp
->nm_fsattr
.nfsa_bsize
;
554 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_TOTAL
))
555 VFSATTR_RETURN(fsap
, f_blocks
, nmp
->nm_fsattr
.nfsa_space_total
/ bsize
);
556 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_FREE
))
557 VFSATTR_RETURN(fsap
, f_bfree
, nmp
->nm_fsattr
.nfsa_space_free
/ bsize
);
558 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_AVAIL
))
559 VFSATTR_RETURN(fsap
, f_bavail
, nmp
->nm_fsattr
.nfsa_space_avail
/ bsize
);
560 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_TOTAL
) &&
561 NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_FREE
))
562 VFSATTR_RETURN(fsap
, f_bused
,
563 (nmp
->nm_fsattr
.nfsa_space_total
/ bsize
) -
564 (nmp
->nm_fsattr
.nfsa_space_free
/ bsize
));
565 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_TOTAL
))
566 VFSATTR_RETURN(fsap
, f_files
, nmp
->nm_fsattr
.nfsa_files_total
);
567 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_FREE
))
568 VFSATTR_RETURN(fsap
, f_ffree
, nmp
->nm_fsattr
.nfsa_files_free
);
569 lck_mtx_unlock(&nmp
->nm_lock
);
572 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
573 u_int32_t caps
, valid
;
574 nfsnode_t np
= nmp
->nm_dnp
;
576 nfsm_assert(error
, VFSTONFS(mp
) && np
, ENXIO
);
579 lck_mtx_lock(&nmp
->nm_lock
);
582 * The capabilities[] array defines what this volume supports.
584 * The valid[] array defines which bits this code understands
585 * the meaning of (whether the volume has that capability or not).
586 * Any zero bits here means "I don't know what you're asking about"
587 * and the caller cannot tell whether that capability is
591 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SYMLINK_SUPPORT
)) {
592 valid
|= VOL_CAP_FMT_SYMBOLICLINKS
;
593 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_SYMLINK
)
594 caps
|= VOL_CAP_FMT_SYMBOLICLINKS
;
596 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_LINK_SUPPORT
)) {
597 valid
|= VOL_CAP_FMT_HARDLINKS
;
598 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_LINK
)
599 caps
|= VOL_CAP_FMT_HARDLINKS
;
601 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
)) {
602 valid
|= VOL_CAP_FMT_CASE_SENSITIVE
;
603 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
))
604 caps
|= VOL_CAP_FMT_CASE_SENSITIVE
;
606 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
)) {
607 valid
|= VOL_CAP_FMT_CASE_PRESERVING
;
608 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
)
609 caps
|= VOL_CAP_FMT_CASE_PRESERVING
;
611 /* Note: VOL_CAP_FMT_2TB_FILESIZE is actually used to test for "large file support" */
612 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
613 /* Is server's max file size at least 4GB? */
614 if (nmp
->nm_fsattr
.nfsa_maxfilesize
>= 0x100000000ULL
)
615 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
616 } else if (nfsvers
>= NFS_VER3
) {
618 * NFSv3 and up supports 64 bits of file size.
619 * So, we'll just assume maxfilesize >= 4GB
621 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
623 if (nfsvers
>= NFS_VER4
) {
624 caps
|= VOL_CAP_FMT_HIDDEN_FILES
;
625 valid
|= VOL_CAP_FMT_HIDDEN_FILES
;
626 // VOL_CAP_FMT_OPENDENYMODES
627 // caps |= VOL_CAP_FMT_OPENDENYMODES;
628 // valid |= VOL_CAP_FMT_OPENDENYMODES;
630 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] =
631 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
632 // VOL_CAP_FMT_SYMBOLICLINKS |
633 // VOL_CAP_FMT_HARDLINKS |
634 // VOL_CAP_FMT_JOURNAL |
635 // VOL_CAP_FMT_JOURNAL_ACTIVE |
636 // VOL_CAP_FMT_NO_ROOT_TIMES |
637 // VOL_CAP_FMT_SPARSE_FILES |
638 // VOL_CAP_FMT_ZERO_RUNS |
639 // VOL_CAP_FMT_CASE_SENSITIVE |
640 // VOL_CAP_FMT_CASE_PRESERVING |
641 // VOL_CAP_FMT_FAST_STATFS |
642 // VOL_CAP_FMT_2TB_FILESIZE |
643 // VOL_CAP_FMT_OPENDENYMODES |
644 // VOL_CAP_FMT_HIDDEN_FILES |
646 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] =
647 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
648 // VOL_CAP_FMT_SYMBOLICLINKS |
649 // VOL_CAP_FMT_HARDLINKS |
650 // VOL_CAP_FMT_JOURNAL |
651 // VOL_CAP_FMT_JOURNAL_ACTIVE |
652 // VOL_CAP_FMT_NO_ROOT_TIMES |
653 // VOL_CAP_FMT_SPARSE_FILES |
654 // VOL_CAP_FMT_ZERO_RUNS |
655 // VOL_CAP_FMT_CASE_SENSITIVE |
656 // VOL_CAP_FMT_CASE_PRESERVING |
657 VOL_CAP_FMT_FAST_STATFS
|
658 VOL_CAP_FMT_2TB_FILESIZE
|
659 // VOL_CAP_FMT_OPENDENYMODES |
660 // VOL_CAP_FMT_HIDDEN_FILES |
664 * We don't support most of the interfaces.
666 * We MAY support locking, but we don't have any easy way of probing.
667 * We can tell if there's no lockd running or if locks have been
668 * disabled for a mount, so we can definitely answer NO in that case.
669 * Any attempt to send a request to lockd to test for locking support
670 * may cause the lazily-launched locking daemons to be started
671 * unnecessarily. So we avoid that. However, we do record if we ever
672 * successfully perform a lock operation on a mount point, so if it
673 * looks like lock ops have worked, we do report that we support them.
676 if (nfsvers
>= NFS_VER4
) {
677 caps
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
678 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
679 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
)
680 caps
|= VOL_CAP_INT_EXTENDED_SECURITY
;
681 valid
|= VOL_CAP_INT_EXTENDED_SECURITY
;
682 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
)
683 caps
|= VOL_CAP_INT_EXTENDED_ATTR
;
684 valid
|= VOL_CAP_INT_EXTENDED_ATTR
;
686 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
)
687 caps
|= VOL_CAP_INT_NAMEDSTREAMS
;
688 valid
|= VOL_CAP_INT_NAMEDSTREAMS
;
690 } else if (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
) {
691 /* locks disabled on this mount, so they definitely won't work */
692 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
693 } else if (nmp
->nm_state
& NFSSTA_LOCKSWORK
) {
694 caps
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
695 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
697 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] =
698 // VOL_CAP_INT_SEARCHFS |
699 // VOL_CAP_INT_ATTRLIST |
700 // VOL_CAP_INT_NFSEXPORT |
701 // VOL_CAP_INT_READDIRATTR |
702 // VOL_CAP_INT_EXCHANGEDATA |
703 // VOL_CAP_INT_COPYFILE |
704 // VOL_CAP_INT_ALLOCATE |
705 // VOL_CAP_INT_VOL_RENAME |
706 // VOL_CAP_INT_ADVLOCK |
707 // VOL_CAP_INT_FLOCK |
708 // VOL_CAP_INT_EXTENDED_SECURITY |
709 // VOL_CAP_INT_USERACCESS |
710 // VOL_CAP_INT_MANLOCK |
711 // VOL_CAP_INT_NAMEDSTREAMS |
712 // VOL_CAP_INT_EXTENDED_ATTR |
713 VOL_CAP_INT_REMOTE_EVENT
|
715 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] =
716 VOL_CAP_INT_SEARCHFS
|
717 VOL_CAP_INT_ATTRLIST
|
718 VOL_CAP_INT_NFSEXPORT
|
719 VOL_CAP_INT_READDIRATTR
|
720 VOL_CAP_INT_EXCHANGEDATA
|
721 VOL_CAP_INT_COPYFILE
|
722 VOL_CAP_INT_ALLOCATE
|
723 VOL_CAP_INT_VOL_RENAME
|
724 // VOL_CAP_INT_ADVLOCK |
725 // VOL_CAP_INT_FLOCK |
726 // VOL_CAP_INT_EXTENDED_SECURITY |
727 // VOL_CAP_INT_USERACCESS |
728 // VOL_CAP_INT_MANLOCK |
729 // VOL_CAP_INT_NAMEDSTREAMS |
730 // VOL_CAP_INT_EXTENDED_ATTR |
731 VOL_CAP_INT_REMOTE_EVENT
|
734 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
735 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
737 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
738 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
740 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
741 lck_mtx_unlock(&nmp
->nm_lock
);
744 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
745 fsap
->f_attributes
.validattr
.commonattr
= 0;
746 fsap
->f_attributes
.validattr
.volattr
=
747 ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
748 fsap
->f_attributes
.validattr
.dirattr
= 0;
749 fsap
->f_attributes
.validattr
.fileattr
= 0;
750 fsap
->f_attributes
.validattr
.forkattr
= 0;
752 fsap
->f_attributes
.nativeattr
.commonattr
= 0;
753 fsap
->f_attributes
.nativeattr
.volattr
=
754 ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
755 fsap
->f_attributes
.nativeattr
.dirattr
= 0;
756 fsap
->f_attributes
.nativeattr
.fileattr
= 0;
757 fsap
->f_attributes
.nativeattr
.forkattr
= 0;
759 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
766 * nfs version 3 fsinfo rpc call
769 nfs3_fsinfo(struct nfsmount
*nmp
, nfsnode_t np
, vfs_context_t ctx
)
771 int error
= 0, lockerror
, status
, nmlocked
= 0;
773 uint32_t val
, prefsize
, maxsize
;
774 struct nfsm_chain nmreq
, nmrep
;
776 nfsm_chain_null(&nmreq
);
777 nfsm_chain_null(&nmrep
);
779 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nmp
->nm_vers
));
780 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
781 nfsm_chain_build_done(error
, &nmreq
);
783 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_FSINFO
, ctx
, NULL
, &nmrep
, &xid
, &status
);
784 if ((lockerror
= nfs_node_lock(np
)))
786 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
793 lck_mtx_lock(&nmp
->nm_lock
);
796 nfsm_chain_get_32(error
, &nmrep
, maxsize
);
797 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
799 nmp
->nm_fsattr
.nfsa_maxread
= maxsize
;
800 if (prefsize
< nmp
->nm_rsize
)
801 nmp
->nm_rsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
802 ~(NFS_FABLKSIZE
- 1);
803 if ((maxsize
> 0) && (maxsize
< nmp
->nm_rsize
)) {
804 nmp
->nm_rsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
805 if (nmp
->nm_rsize
== 0)
806 nmp
->nm_rsize
= maxsize
;
808 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
); // skip rtmult
810 nfsm_chain_get_32(error
, &nmrep
, maxsize
);
811 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
813 nmp
->nm_fsattr
.nfsa_maxwrite
= maxsize
;
814 if (prefsize
< nmp
->nm_wsize
)
815 nmp
->nm_wsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
816 ~(NFS_FABLKSIZE
- 1);
817 if ((maxsize
> 0) && (maxsize
< nmp
->nm_wsize
)) {
818 nmp
->nm_wsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
819 if (nmp
->nm_wsize
== 0)
820 nmp
->nm_wsize
= maxsize
;
822 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
); // skip wtmult
824 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
826 if ((prefsize
> 0) && (prefsize
< nmp
->nm_readdirsize
))
827 nmp
->nm_readdirsize
= prefsize
;
828 if ((nmp
->nm_fsattr
.nfsa_maxread
> 0) &&
829 (nmp
->nm_fsattr
.nfsa_maxread
< nmp
->nm_readdirsize
))
830 nmp
->nm_readdirsize
= nmp
->nm_fsattr
.nfsa_maxread
;
832 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_maxfilesize
);
834 nfsm_chain_adv(error
, &nmrep
, 2 * NFSX_UNSIGNED
); // skip time_delta
836 /* convert FS properties to our own flags */
837 nfsm_chain_get_32(error
, &nmrep
, val
);
839 if (val
& NFSV3FSINFO_LINK
)
840 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_LINK
;
841 if (val
& NFSV3FSINFO_SYMLINK
)
842 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_SYMLINK
;
843 if (val
& NFSV3FSINFO_HOMOGENEOUS
)
844 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_HOMOGENEOUS
;
845 if (val
& NFSV3FSINFO_CANSETTIME
)
846 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_SET_TIME
;
847 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
848 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXREAD
);
849 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXWRITE
);
850 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
);
851 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_LINK_SUPPORT
);
852 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SYMLINK_SUPPORT
);
853 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_HOMOGENEOUS
);
854 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CANSETTIME
);
857 lck_mtx_unlock(&nmp
->nm_lock
);
858 nfsm_chain_cleanup(&nmreq
);
859 nfsm_chain_cleanup(&nmrep
);
864 * Mount a remote root fs via. nfs. This depends on the info in the
865 * nfs_diskless structure that has been filled in properly by some primary
867 * It goes something like this:
868 * - do enough of "ifconfig" by calling ifioctl() so that the system
869 * can talk to the server
870 * - If nfs_diskless.mygateway is filled in, use that address as
872 * - hand craft the swap nfs vnode hanging off a fake mount point
873 * if swdevt[0].sw_dev == NODEV
874 * - build the rootfs mount point and call mountnfs() to do the rest.
879 struct nfs_diskless nd
;
884 #if !defined(NO_MOUNT_PRIVATE)
885 mount_t mppriv
= NULL
;
886 vnode_t vppriv
= NULL
;
887 #endif /* NO_MOUNT_PRIVATE */
891 * Call nfs_boot_init() to fill in the nfs_diskless struct.
892 * Note: networking must already have been configured before
895 bzero((caddr_t
) &nd
, sizeof(nd
));
896 error
= nfs_boot_init(&nd
);
898 panic("nfs_boot_init: unable to initialize NFS root system information, "
899 "error %d, check configuration: %s\n", error
, PE_boot_args());
902 * Try NFSv3 first, then fallback to NFSv2.
903 * Likewise, try TCP first, then fall back to UDP.
906 sotype
= SOCK_STREAM
;
909 error
= nfs_boot_getfh(&nd
, v3
, sotype
);
911 if (error
== EHOSTDOWN
|| error
== EHOSTUNREACH
) {
912 if (nd
.nd_root
.ndm_mntfrom
)
913 FREE_ZONE(nd
.nd_root
.ndm_mntfrom
,
914 MAXPATHLEN
, M_NAMEI
);
915 if (nd
.nd_root
.ndm_path
)
916 FREE_ZONE(nd
.nd_root
.ndm_path
,
917 MAXPATHLEN
, M_NAMEI
);
918 if (nd
.nd_private
.ndm_mntfrom
)
919 FREE_ZONE(nd
.nd_private
.ndm_mntfrom
,
920 MAXPATHLEN
, M_NAMEI
);
921 if (nd
.nd_private
.ndm_path
)
922 FREE_ZONE(nd
.nd_private
.ndm_path
,
923 MAXPATHLEN
, M_NAMEI
);
927 if (sotype
== SOCK_STREAM
) {
928 printf("NFS mount (v3,TCP) failed with error %d, trying UDP...\n", error
);
932 printf("NFS mount (v3,UDP) failed with error %d, trying v2...\n", error
);
934 sotype
= SOCK_STREAM
;
936 } else if (sotype
== SOCK_STREAM
) {
937 printf("NFS mount (v2,TCP) failed with error %d, trying UDP...\n", error
);
941 printf("NFS mount (v2,UDP) failed with error %d, giving up...\n", error
);
945 panic("NFS mount failed: NFS server mountd not responding, check server configuration: %s", PE_boot_args());
948 panic("NFS mount failed: NFS server refused mount, check server configuration: %s", PE_boot_args());
950 panic("NFS mount failed with error %d, check configuration: %s", error
, PE_boot_args());
954 ctx
= vfs_context_kernel();
957 * Create the root mount point.
959 #if !defined(NO_MOUNT_PRIVATE)
961 //PWC hack until we have a real "mount" tool to remount root rw
963 int flags
= MNT_ROOTFS
|MNT_RDONLY
;
964 PE_parse_boot_argn("-rwroot_hack", &rw_root
, sizeof (rw_root
));
968 kprintf("-rwroot_hack in effect: mounting root fs read/write\n");
971 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", flags
, &vp
, &mp
, ctx
)))
973 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_ROOTFS
, &vp
, &mp
, ctx
)))
974 #endif /* NO_MOUNT_PRIVATE */
977 if (sotype
== SOCK_STREAM
) {
978 printf("NFS root mount (v3,TCP) failed with %d, trying UDP...\n", error
);
982 printf("NFS root mount (v3,UDP) failed with %d, trying v2...\n", error
);
984 sotype
= SOCK_STREAM
;
986 } else if (sotype
== SOCK_STREAM
) {
987 printf("NFS root mount (v2,TCP) failed with %d, trying UDP...\n", error
);
991 printf("NFS root mount (v2,UDP) failed with error %d, giving up...\n", error
);
993 panic("NFS root mount failed with error %d, check configuration: %s\n", error
, PE_boot_args());
996 printf("root on %s\n", nd
.nd_root
.ndm_mntfrom
);
1002 #if !defined(NO_MOUNT_PRIVATE)
1003 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
1004 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
1005 0, &vppriv
, &mppriv
, ctx
);
1007 panic("NFS /private mount failed with error %d, check configuration: %s\n", error
, PE_boot_args());
1008 printf("private on %s\n", nd
.nd_private
.ndm_mntfrom
);
1011 mount_list_add(mppriv
);
1014 #endif /* NO_MOUNT_PRIVATE */
1016 if (nd
.nd_root
.ndm_mntfrom
)
1017 FREE_ZONE(nd
.nd_root
.ndm_mntfrom
, MAXPATHLEN
, M_NAMEI
);
1018 if (nd
.nd_root
.ndm_path
)
1019 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
1020 if (nd
.nd_private
.ndm_mntfrom
)
1021 FREE_ZONE(nd
.nd_private
.ndm_mntfrom
, MAXPATHLEN
, M_NAMEI
);
1022 if (nd
.nd_private
.ndm_path
)
1023 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
1025 /* Get root attributes (for the time). */
1026 error
= nfs_getattr(VTONFS(vp
), NULL
, ctx
, NGA_UNCACHED
);
1028 panic("NFS mount: failed to get attributes for root directory, error %d, check server", error
);
1033 * Internal version of mount system call for diskless setup.
1037 struct nfs_dlmount
*ndmntp
,
1038 const char *mntname
,
1045 int error
, numcomps
;
1046 char *xdrbuf
, *p
, *cp
, *frompath
, *endserverp
;
1047 char uaddr
[MAX_IPv4_STR_LEN
];
1049 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1050 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
];
1051 uint32_t mflags
[NFS_MFLAG_BITMAP_LEN
];
1052 uint32_t argslength_offset
, attrslength_offset
, end_offset
;
1054 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_mntfrom
, &mp
))) {
1055 printf("nfs_mount_diskless: NFS not configured\n");
1059 mp
->mnt_flag
|= mntflag
;
1060 if (!(mntflag
& MNT_RDONLY
))
1061 mp
->mnt_flag
&= ~MNT_RDONLY
;
1063 /* find the server-side path being mounted */
1064 frompath
= ndmntp
->ndm_mntfrom
;
1065 if (*frompath
== '[') { /* skip IPv6 literal address */
1066 while (*frompath
&& (*frompath
!= ']'))
1068 if (*frompath
== ']')
1071 while (*frompath
&& (*frompath
!= ':'))
1073 endserverp
= frompath
;
1074 while (*frompath
&& (*frompath
== ':'))
1076 /* count fs location path components */
1078 while (*p
&& (*p
== '/'))
1083 while (*p
&& (*p
!= '/'))
1085 while (*p
&& (*p
== '/'))
1089 /* convert address to universal address string */
1090 if (inet_ntop(AF_INET
, &ndmntp
->ndm_saddr
.sin_addr
, uaddr
, sizeof(uaddr
)) != uaddr
) {
1091 printf("nfs_mount_diskless: bad address\n");
1095 /* prepare mount attributes */
1096 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1097 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1098 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1099 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1100 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1101 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1102 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1104 /* prepare mount flags */
1105 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1106 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1107 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1108 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1110 /* build xdr buffer */
1111 xb_init_buffer(&xb
, NULL
, 0);
1112 xb_add_32(error
, &xb
, NFS_ARGSVERSION_XDR
);
1113 argslength_offset
= xb_offset(&xb
);
1114 xb_add_32(error
, &xb
, 0); // args length
1115 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1116 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1117 attrslength_offset
= xb_offset(&xb
);
1118 xb_add_32(error
, &xb
, 0); // attrs length
1119 xb_add_32(error
, &xb
, ndmntp
->ndm_nfsv3
? 3 : 2); // NFS version
1120 xb_add_string(error
, &xb
, ((ndmntp
->ndm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1121 xb_add_32(error
, &xb
, ntohs(ndmntp
->ndm_saddr
.sin_port
)); // NFS port
1122 xb_add_fh(error
, &xb
, &ndmntp
->ndm_fh
[0], ndmntp
->ndm_fhlen
);
1124 xb_add_32(error
, &xb
, 1); /* fs location count */
1125 xb_add_32(error
, &xb
, 1); /* server count */
1126 xb_add_string(error
, &xb
, ndmntp
->ndm_mntfrom
, (endserverp
- ndmntp
->ndm_mntfrom
)); /* server name */
1127 xb_add_32(error
, &xb
, 1); /* address count */
1128 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1129 xb_add_32(error
, &xb
, 0); /* empty server info */
1130 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1132 while (*p
&& (*p
== '/'))
1136 while (*p
&& (*p
!= '/'))
1138 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
1141 while (*p
&& (*p
== '/'))
1144 xb_add_32(error
, &xb
, 0); /* empty fsl info */
1145 xb_add_32(error
, &xb
, mntflag
); /* MNT flags */
1146 xb_build_done(error
, &xb
);
1148 /* update opaque counts */
1149 end_offset
= xb_offset(&xb
);
1151 error
= xb_seek(&xb
, argslength_offset
);
1152 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
1155 error
= xb_seek(&xb
, attrslength_offset
);
1156 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
1159 printf("nfs_mount_diskless: error %d assembling mount args\n", error
);
1163 /* grab the assembled buffer */
1164 xdrbuf
= xb_buffer_base(&xb
);
1165 xb
.xb_flags
&= ~XB_CLEANUP
;
1168 if ((error
= mountnfs(xdrbuf
, mp
, ctx
, vpp
))) {
1169 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
1170 // XXX vfs_rootmountfailed(mp);
1172 mp
->mnt_vtable
->vfc_refcount
--;
1173 mount_list_unlock();
1175 mount_lock_destroy(mp
);
1177 mac_mount_label_destroy(mp
);
1179 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
1187 #if !defined(NO_MOUNT_PRIVATE)
1189 * Internal version of mount system call to mount "/private"
1190 * separately in diskless setup
1193 nfs_mount_diskless_private(
1194 struct nfs_dlmount
*ndmntp
,
1195 const char *mntname
,
1202 int error
, numcomps
;
1204 struct vfstable
*vfsp
;
1205 struct nameidata nd
;
1207 char *xdrbuf
= NULL
, *p
, *cp
, *frompath
, *endserverp
;
1208 char uaddr
[MAX_IPv4_STR_LEN
];
1210 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1211 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
], mflags
[NFS_MFLAG_BITMAP_LEN
];
1212 uint32_t argslength_offset
, attrslength_offset
, end_offset
;
1214 procp
= current_proc(); /* XXX */
1219 * mimic main()!. Temporarily set up rootvnode and other stuff so
1220 * that namei works. Need to undo this because main() does it, too
1222 struct filedesc
*fdp
; /* pointer to file descriptor state */
1224 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
1226 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
1227 if (VFS_ROOT(mountlist
.tqh_first
, &rootvnode
, NULL
))
1228 panic("cannot find root vnode");
1229 error
= vnode_ref(rootvnode
);
1231 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
1234 fdp
->fd_cdir
= rootvnode
;
1235 fdp
->fd_rdir
= NULL
;
1239 * Get vnode to be covered
1241 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
1242 CAST_USER_ADDR_T(mntname
), ctx
);
1243 if ((error
= namei(&nd
))) {
1244 printf("nfs_mountroot: private namei failed!\n");
1248 /* undo vnode_ref() in mimic main()! */
1249 vnode_rele(rootvnode
);
1254 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
)) ||
1255 (error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
1259 if (vnode_vtype(vp
) != VDIR
) {
1264 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
1265 if (!strncmp(vfsp
->vfc_name
, "nfs", sizeof(vfsp
->vfc_name
)))
1268 printf("nfs_mountroot: private NFS not configured\n");
1273 if (vnode_mountedhere(vp
) != NULL
) {
1280 * Allocate and initialize the filesystem.
1282 mp
= _MALLOC_ZONE((u_int32_t
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
1284 printf("nfs_mountroot: unable to allocate mount structure\n");
1289 bzero((char *)mp
, sizeof(struct mount
));
1291 /* Initialize the default IO constraints */
1292 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
1293 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
1294 mp
->mnt_ioflags
= 0;
1295 mp
->mnt_realrootvp
= NULLVP
;
1296 mp
->mnt_authcache_ttl
= CACHED_LOOKUP_RIGHT_TTL
;
1298 mount_lock_init(mp
);
1299 TAILQ_INIT(&mp
->mnt_vnodelist
);
1300 TAILQ_INIT(&mp
->mnt_workerqueue
);
1301 TAILQ_INIT(&mp
->mnt_newvnodes
);
1302 (void)vfs_busy(mp
, LK_NOWAIT
);
1303 TAILQ_INIT(&mp
->mnt_vnodelist
);
1305 vfsp
->vfc_refcount
++;
1306 mount_list_unlock();
1307 mp
->mnt_vtable
= vfsp
;
1308 mp
->mnt_op
= vfsp
->vfc_vfsops
;
1309 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
1310 mp
->mnt_flag
= mntflag
;
1311 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
1312 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
-1);
1313 vp
->v_mountedhere
= mp
;
1314 mp
->mnt_vnodecovered
= vp
;
1316 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(kauth_cred_get());
1317 (void) copystr(mntname
, mp
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
- 1, 0);
1318 (void) copystr(ndmntp
->ndm_mntfrom
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, 0);
1320 mac_mount_label_init(mp
);
1321 mac_mount_label_associate(ctx
, mp
);
1324 /* find the server-side path being mounted */
1325 frompath
= ndmntp
->ndm_mntfrom
;
1326 if (*frompath
== '[') { /* skip IPv6 literal address */
1327 while (*frompath
&& (*frompath
!= ']'))
1329 if (*frompath
== ']')
1332 while (*frompath
&& (*frompath
!= ':'))
1334 endserverp
= frompath
;
1335 while (*frompath
&& (*frompath
== ':'))
1337 /* count fs location path components */
1339 while (*p
&& (*p
== '/'))
1344 while (*p
&& (*p
!= '/'))
1346 while (*p
&& (*p
== '/'))
1350 /* convert address to universal address string */
1351 if (inet_ntop(AF_INET
, &ndmntp
->ndm_saddr
.sin_addr
, uaddr
, sizeof(uaddr
)) != uaddr
) {
1352 printf("nfs_mountroot: bad address\n");
1357 /* prepare mount attributes */
1358 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1359 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1360 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1361 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1362 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1363 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1364 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1366 /* prepare mount flags */
1367 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1368 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1369 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1370 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1372 /* build xdr buffer */
1373 xb_init_buffer(&xb
, NULL
, 0);
1374 xb_add_32(error
, &xb
, NFS_ARGSVERSION_XDR
);
1375 argslength_offset
= xb_offset(&xb
);
1376 xb_add_32(error
, &xb
, 0); // args length
1377 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1378 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1379 attrslength_offset
= xb_offset(&xb
);
1380 xb_add_32(error
, &xb
, 0); // attrs length
1381 xb_add_32(error
, &xb
, ndmntp
->ndm_nfsv3
? 3 : 2); // NFS version
1382 xb_add_string(error
, &xb
, ((ndmntp
->ndm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1383 xb_add_32(error
, &xb
, ntohs(ndmntp
->ndm_saddr
.sin_port
)); // NFS port
1384 xb_add_fh(error
, &xb
, &ndmntp
->ndm_fh
[0], ndmntp
->ndm_fhlen
);
1386 xb_add_32(error
, &xb
, 1); /* fs location count */
1387 xb_add_32(error
, &xb
, 1); /* server count */
1388 xb_add_string(error
, &xb
, ndmntp
->ndm_mntfrom
, (endserverp
- ndmntp
->ndm_mntfrom
)); /* server name */
1389 xb_add_32(error
, &xb
, 1); /* address count */
1390 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1391 xb_add_32(error
, &xb
, 0); /* empty server info */
1392 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1394 while (*p
&& (*p
== '/'))
1398 while (*p
&& (*p
!= '/'))
1400 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
1403 while (*p
&& (*p
== '/'))
1406 xb_add_32(error
, &xb
, 0); /* empty fsl info */
1407 xb_add_32(error
, &xb
, mntflag
); /* MNT flags */
1408 xb_build_done(error
, &xb
);
1410 /* update opaque counts */
1411 end_offset
= xb_offset(&xb
);
1413 error
= xb_seek(&xb
, argslength_offset
);
1414 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
1417 error
= xb_seek(&xb
, attrslength_offset
);
1418 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
1421 printf("nfs_mountroot: error %d assembling mount args\n", error
);
1424 /* grab the assembled buffer */
1425 xdrbuf
= xb_buffer_base(&xb
);
1426 xb
.xb_flags
&= ~XB_CLEANUP
;
1429 if ((error
= mountnfs(xdrbuf
, mp
, ctx
, &vp
))) {
1430 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
1431 vnode_put(mp
->mnt_vnodecovered
);
1433 vfsp
->vfc_refcount
--;
1434 mount_list_unlock();
1436 mount_lock_destroy(mp
);
1438 mac_mount_label_destroy(mp
);
1440 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
1450 #endif /* NO_MOUNT_PRIVATE */
1453 * Convert old style NFS mount args to XDR.
1456 nfs_convert_old_nfs_args(mount_t mp
, user_addr_t data
, vfs_context_t ctx
, int argsversion
, int inkernel
, char **xdrbufp
)
1458 int error
= 0, args64bit
, argsize
, numcomps
;
1459 struct user_nfs_args args
;
1460 struct nfs_args tempargs
;
1463 u_char nfh
[NFS4_FHSIZE
];
1464 char *mntfrom
, *endserverp
, *frompath
, *p
, *cp
;
1465 struct sockaddr_storage ss
;
1467 char uaddr
[MAX_IPv6_STR_LEN
];
1468 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1469 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
], mflags
[NFS_MFLAG_BITMAP_LEN
];
1470 uint32_t nfsvers
, nfslockmode
= 0, argslength_offset
, attrslength_offset
, end_offset
;
1475 /* allocate a temporary buffer for mntfrom */
1476 MALLOC_ZONE(mntfrom
, char*, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1480 args64bit
= (inkernel
|| vfs_context_is64bit(ctx
));
1481 argsp
= args64bit
? (void*)&args
: (void*)&tempargs
;
1483 argsize
= args64bit
? sizeof(args
) : sizeof(tempargs
);
1484 switch (argsversion
) {
1486 argsize
-= NFS_ARGSVERSION4_INCSIZE
;
1488 argsize
-= NFS_ARGSVERSION5_INCSIZE
;
1490 argsize
-= NFS_ARGSVERSION6_INCSIZE
;
1494 error
= EPROGMISMATCH
;
1498 /* read in the structure */
1500 bcopy(CAST_DOWN(void *, data
), argsp
, argsize
);
1502 error
= copyin(data
, argsp
, argsize
);
1506 args
.addrlen
= tempargs
.addrlen
;
1507 args
.sotype
= tempargs
.sotype
;
1508 args
.proto
= tempargs
.proto
;
1509 args
.fhsize
= tempargs
.fhsize
;
1510 args
.flags
= tempargs
.flags
;
1511 args
.wsize
= tempargs
.wsize
;
1512 args
.rsize
= tempargs
.rsize
;
1513 args
.readdirsize
= tempargs
.readdirsize
;
1514 args
.timeo
= tempargs
.timeo
;
1515 args
.retrans
= tempargs
.retrans
;
1516 args
.maxgrouplist
= tempargs
.maxgrouplist
;
1517 args
.readahead
= tempargs
.readahead
;
1518 args
.leaseterm
= tempargs
.leaseterm
;
1519 args
.deadthresh
= tempargs
.deadthresh
;
1520 args
.addr
= CAST_USER_ADDR_T(tempargs
.addr
);
1521 args
.fh
= CAST_USER_ADDR_T(tempargs
.fh
);
1522 args
.hostname
= CAST_USER_ADDR_T(tempargs
.hostname
);
1523 if (args
.version
>= 4) {
1524 args
.acregmin
= tempargs
.acregmin
;
1525 args
.acregmax
= tempargs
.acregmax
;
1526 args
.acdirmin
= tempargs
.acdirmin
;
1527 args
.acdirmax
= tempargs
.acdirmax
;
1529 if (args
.version
>= 5)
1530 args
.auth
= tempargs
.auth
;
1531 if (args
.version
>= 6)
1532 args
.deadtimeout
= tempargs
.deadtimeout
;
1535 if ((args
.fhsize
< 0) || (args
.fhsize
> NFS4_FHSIZE
)) {
1539 if (args
.fhsize
> 0) {
1541 bcopy(CAST_DOWN(void *, args
.fh
), (caddr_t
)nfh
, args
.fhsize
);
1543 error
= copyin(args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
1548 error
= copystr(CAST_DOWN(void *, args
.hostname
), mntfrom
, MAXPATHLEN
-1, &len
);
1550 error
= copyinstr(args
.hostname
, mntfrom
, MAXPATHLEN
-1, &len
);
1552 bzero(&mntfrom
[len
], MAXPATHLEN
- len
);
1554 /* find the server-side path being mounted */
1556 if (*frompath
== '[') { /* skip IPv6 literal address */
1557 while (*frompath
&& (*frompath
!= ']'))
1559 if (*frompath
== ']')
1562 while (*frompath
&& (*frompath
!= ':'))
1564 endserverp
= frompath
;
1565 while (*frompath
&& (*frompath
== ':'))
1567 /* count fs location path components */
1569 while (*p
&& (*p
== '/'))
1574 while (*p
&& (*p
!= '/'))
1576 while (*p
&& (*p
== '/'))
1580 /* copy socket address */
1582 bcopy(CAST_DOWN(void *, args
.addr
), &ss
, args
.addrlen
);
1584 if ((size_t)args
.addrlen
> sizeof (struct sockaddr_storage
))
1587 error
= copyin(args
.addr
, &ss
, args
.addrlen
);
1590 ss
.ss_len
= args
.addrlen
;
1592 /* convert address to universal address string */
1593 if (ss
.ss_family
== AF_INET
)
1594 sinaddr
= &((struct sockaddr_in
*)&ss
)->sin_addr
;
1595 else if (ss
.ss_family
== AF_INET6
)
1596 sinaddr
= &((struct sockaddr_in6
*)&ss
)->sin6_addr
;
1599 if (!sinaddr
|| (inet_ntop(ss
.ss_family
, sinaddr
, uaddr
, sizeof(uaddr
)) != uaddr
)) {
1604 /* prepare mount flags */
1605 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1606 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1607 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_SOFT
);
1608 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_INTR
);
1609 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1610 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCONNECT
);
1611 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_DUMBTIMER
);
1612 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_CALLUMNT
);
1613 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RDIRPLUS
);
1614 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NONEGNAMECACHE
);
1615 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MUTEJUKEBOX
);
1616 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOQUOTA
);
1617 if (args
.flags
& NFSMNT_SOFT
)
1618 NFS_BITMAP_SET(mflags
, NFS_MFLAG_SOFT
);
1619 if (args
.flags
& NFSMNT_INT
)
1620 NFS_BITMAP_SET(mflags
, NFS_MFLAG_INTR
);
1621 if (args
.flags
& NFSMNT_RESVPORT
)
1622 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1623 if (args
.flags
& NFSMNT_NOCONN
)
1624 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCONNECT
);
1625 if (args
.flags
& NFSMNT_DUMBTIMR
)
1626 NFS_BITMAP_SET(mflags
, NFS_MFLAG_DUMBTIMER
);
1627 if (args
.flags
& NFSMNT_CALLUMNT
)
1628 NFS_BITMAP_SET(mflags
, NFS_MFLAG_CALLUMNT
);
1629 if (args
.flags
& NFSMNT_RDIRPLUS
)
1630 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RDIRPLUS
);
1631 if (args
.flags
& NFSMNT_NONEGNAMECACHE
)
1632 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NONEGNAMECACHE
);
1633 if (args
.flags
& NFSMNT_MUTEJUKEBOX
)
1634 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MUTEJUKEBOX
);
1635 if (args
.flags
& NFSMNT_NOQUOTA
)
1636 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOQUOTA
);
1638 /* prepare mount attributes */
1639 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1640 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FLAGS
);
1641 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1642 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1643 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1644 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1645 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1646 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1647 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFROM
);
1648 if (args
.flags
& NFSMNT_NFSV4
)
1650 else if (args
.flags
& NFSMNT_NFSV3
)
1654 if ((args
.flags
& NFSMNT_RSIZE
) && (args
.rsize
> 0))
1655 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READ_SIZE
);
1656 if ((args
.flags
& NFSMNT_WSIZE
) && (args
.wsize
> 0))
1657 NFS_BITMAP_SET(mattrs
, NFS_MATTR_WRITE_SIZE
);
1658 if ((args
.flags
& NFSMNT_TIMEO
) && (args
.timeo
> 0))
1659 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
);
1660 if ((args
.flags
& NFSMNT_RETRANS
) && (args
.retrans
> 0))
1661 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
);
1662 if ((args
.flags
& NFSMNT_MAXGRPS
) && (args
.maxgrouplist
> 0))
1663 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
);
1664 if ((args
.flags
& NFSMNT_READAHEAD
) && (args
.readahead
> 0))
1665 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READAHEAD
);
1666 if ((args
.flags
& NFSMNT_READDIRSIZE
) && (args
.readdirsize
> 0))
1667 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READDIR_SIZE
);
1668 if ((args
.flags
& NFSMNT_NOLOCKS
) ||
1669 (args
.flags
& NFSMNT_LOCALLOCKS
)) {
1670 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCK_MODE
);
1671 if (args
.flags
& NFSMNT_NOLOCKS
)
1672 nfslockmode
= NFS_LOCK_MODE_DISABLED
;
1673 else if (args
.flags
& NFSMNT_LOCALLOCKS
)
1674 nfslockmode
= NFS_LOCK_MODE_LOCAL
;
1676 nfslockmode
= NFS_LOCK_MODE_ENABLED
;
1678 if (args
.version
>= 4) {
1679 if ((args
.flags
& NFSMNT_ACREGMIN
) && (args
.acregmin
> 0))
1680 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
);
1681 if ((args
.flags
& NFSMNT_ACREGMAX
) && (args
.acregmax
> 0))
1682 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
);
1683 if ((args
.flags
& NFSMNT_ACDIRMIN
) && (args
.acdirmin
> 0))
1684 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
);
1685 if ((args
.flags
& NFSMNT_ACDIRMAX
) && (args
.acdirmax
> 0))
1686 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
);
1688 if (args
.version
>= 5) {
1689 if ((args
.flags
& NFSMNT_SECFLAVOR
) || (args
.flags
& NFSMNT_SECSYSOK
))
1690 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SECURITY
);
1692 if (args
.version
>= 6) {
1693 if ((args
.flags
& NFSMNT_DEADTIMEOUT
) && (args
.deadtimeout
> 0))
1694 NFS_BITMAP_SET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
);
1697 /* build xdr buffer */
1698 xb_init_buffer(&xb
, NULL
, 0);
1699 xb_add_32(error
, &xb
, args
.version
);
1700 argslength_offset
= xb_offset(&xb
);
1701 xb_add_32(error
, &xb
, 0); // args length
1702 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1703 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1704 attrslength_offset
= xb_offset(&xb
);
1705 xb_add_32(error
, &xb
, 0); // attrs length
1706 xb_add_bitmap(error
, &xb
, mflags_mask
, NFS_MFLAG_BITMAP_LEN
); /* mask */
1707 xb_add_bitmap(error
, &xb
, mflags
, NFS_MFLAG_BITMAP_LEN
); /* value */
1708 xb_add_32(error
, &xb
, nfsvers
);
1709 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
))
1710 xb_add_32(error
, &xb
, args
.rsize
);
1711 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
))
1712 xb_add_32(error
, &xb
, args
.wsize
);
1713 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
))
1714 xb_add_32(error
, &xb
, args
.readdirsize
);
1715 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
))
1716 xb_add_32(error
, &xb
, args
.readahead
);
1717 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
1718 xb_add_32(error
, &xb
, args
.acregmin
);
1719 xb_add_32(error
, &xb
, 0);
1721 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
1722 xb_add_32(error
, &xb
, args
.acregmax
);
1723 xb_add_32(error
, &xb
, 0);
1725 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
1726 xb_add_32(error
, &xb
, args
.acdirmin
);
1727 xb_add_32(error
, &xb
, 0);
1729 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
1730 xb_add_32(error
, &xb
, args
.acdirmax
);
1731 xb_add_32(error
, &xb
, 0);
1733 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
))
1734 xb_add_32(error
, &xb
, nfslockmode
);
1735 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
1736 uint32_t flavors
[2], i
=0;
1737 if (args
.flags
& NFSMNT_SECFLAVOR
)
1738 flavors
[i
++] = args
.auth
;
1739 if ((args
.flags
& NFSMNT_SECSYSOK
) && ((i
== 0) || (flavors
[0] != RPCAUTH_SYS
)))
1740 flavors
[i
++] = RPCAUTH_SYS
;
1741 xb_add_word_array(error
, &xb
, flavors
, i
);
1743 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
))
1744 xb_add_32(error
, &xb
, args
.maxgrouplist
);
1745 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
))
1746 xb_add_string(error
, &xb
, ((args
.sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1747 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
))
1748 xb_add_32(error
, &xb
, ((ss
.ss_family
== AF_INET
) ?
1749 ntohs(((struct sockaddr_in
*)&ss
)->sin_port
) :
1750 ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
)));
1751 /* NFS_MATTR_MOUNT_PORT (not available in old args) */
1752 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
1753 /* convert from .1s increments to time */
1754 xb_add_32(error
, &xb
, args
.timeo
/10);
1755 xb_add_32(error
, &xb
, (args
.timeo%10
)*100000000);
1757 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
))
1758 xb_add_32(error
, &xb
, args
.retrans
);
1759 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
1760 xb_add_32(error
, &xb
, args
.deadtimeout
);
1761 xb_add_32(error
, &xb
, 0);
1763 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
))
1764 xb_add_fh(error
, &xb
, &nfh
[0], args
.fhsize
);
1765 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
1766 xb_add_32(error
, &xb
, 1); /* fs location count */
1767 xb_add_32(error
, &xb
, 1); /* server count */
1768 xb_add_string(error
, &xb
, mntfrom
, (endserverp
- mntfrom
)); /* server name */
1769 xb_add_32(error
, &xb
, 1); /* address count */
1770 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1771 xb_add_32(error
, &xb
, 0); /* empty server info */
1772 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1775 while (*p
&& (*p
== '/'))
1779 while (*p
&& (*p
!= '/'))
1781 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
1783 while (*p
&& (*p
== '/'))
1786 xb_add_32(error
, &xb
, 0); /* empty fsl info */
1788 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
))
1789 xb_add_32(error
, &xb
, (vfs_flags(mp
) & MNT_VISFLAGMASK
)); /* VFS MNT_* flags */
1790 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
))
1791 xb_add_string(error
, &xb
, mntfrom
, strlen(mntfrom
)); /* fixed f_mntfromname */
1792 xb_build_done(error
, &xb
);
1794 /* update opaque counts */
1795 end_offset
= xb_offset(&xb
);
1796 error
= xb_seek(&xb
, argslength_offset
);
1797 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
1799 error
= xb_seek(&xb
, attrslength_offset
);
1800 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
1803 /* grab the assembled buffer */
1804 *xdrbufp
= xb_buffer_base(&xb
);
1805 xb
.xb_flags
&= ~XB_CLEANUP
;
1809 FREE_ZONE(mntfrom
, MAXPATHLEN
, M_NAMEI
);
1819 nfs_vfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t ctx
)
1821 int error
= 0, inkernel
= vfs_iskernelmount(mp
);
1822 uint32_t argsversion
, argslength
;
1823 char *xdrbuf
= NULL
;
1825 /* read in version */
1827 bcopy(CAST_DOWN(void *, data
), &argsversion
, sizeof(argsversion
));
1828 else if ((error
= copyin(data
, &argsversion
, sizeof(argsversion
))))
1831 /* If we have XDR args, then all values in the buffer are in network order */
1832 if (argsversion
== htonl(NFS_ARGSVERSION_XDR
))
1833 argsversion
= NFS_ARGSVERSION_XDR
;
1835 switch (argsversion
) {
1840 /* convert old-style args to xdr */
1841 error
= nfs_convert_old_nfs_args(mp
, data
, ctx
, argsversion
, inkernel
, &xdrbuf
);
1843 case NFS_ARGSVERSION_XDR
:
1844 /* copy in xdr buffer */
1846 bcopy(CAST_DOWN(void *, (data
+ XDRWORD
)), &argslength
, XDRWORD
);
1848 error
= copyin((data
+ XDRWORD
), &argslength
, XDRWORD
);
1851 argslength
= ntohl(argslength
);
1852 /* put a reasonable limit on the size of the XDR args */
1853 if (argslength
> 16*1024) {
1857 /* allocate xdr buffer */
1858 xdrbuf
= xb_malloc(xdr_rndup(argslength
));
1864 bcopy(CAST_DOWN(void *, data
), xdrbuf
, argslength
);
1866 error
= copyin(data
, xdrbuf
, argslength
);
1869 error
= EPROGMISMATCH
;
1877 error
= mountnfs(xdrbuf
, mp
, ctx
, &vp
);
1882 * Common code for mount and mountroot
1885 /* Set up an NFSv2/v3 mount */
1888 struct nfsmount
*nmp
,
1893 struct nfs_vattr nvattr
;
1902 * Get file attributes for the mountpoint. These are needed
1903 * in order to properly create the root vnode.
1905 error
= nfs3_getattr_rpc(NULL
, nmp
->nm_mountp
, nmp
->nm_fh
->fh_data
, nmp
->nm_fh
->fh_len
, 0,
1906 ctx
, &nvattr
, &xid
);
1910 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, nmp
->nm_fh
->fh_data
, nmp
->nm_fh
->fh_len
,
1911 &nvattr
, &xid
, RPCAUTH_UNKNOWN
, NG_MARKROOT
, npp
);
1913 nfs_node_unlock(*npp
);
1918 * Try to make sure we have all the general info from the server.
1920 if (nmp
->nm_vers
== NFS_VER2
) {
1921 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
1922 nmp
->nm_fsattr
.nfsa_maxname
= NFS_MAXNAMLEN
;
1923 } else if (nmp
->nm_vers
== NFS_VER3
) {
1924 /* get the NFSv3 FSINFO */
1925 error
= nfs3_fsinfo(nmp
, *npp
, ctx
);
1928 /* If the server indicates all pathconf info is */
1929 /* the same, grab a copy of that info now */
1930 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_HOMOGENEOUS
) &&
1931 (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
1932 struct nfs_fsattr nfsa
;
1933 if (!nfs3_pathconf_rpc(*npp
, &nfsa
, ctx
)) {
1934 /* cache a copy of the results */
1935 lck_mtx_lock(&nmp
->nm_lock
);
1936 nfs3_pathconf_cache(nmp
, &nfsa
);
1937 lck_mtx_unlock(&nmp
->nm_lock
);
1942 if (*npp
&& error
) {
1943 vnode_put(NFSTOV(*npp
));
1944 vnode_recycle(NFSTOV(*npp
));
1951 * Update an NFSv4 mount path with the contents of the symlink.
1953 * Read the link for the given file handle.
1954 * Insert the link's components into the path.
1957 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
)
1959 int error
= 0, status
, numops
;
1960 uint32_t len
= 0, comp
, newcomp
, linkcompcount
;
1962 struct nfsm_chain nmreq
, nmrep
;
1963 struct nfsreq rq
, *req
= &rq
;
1964 struct nfsreq_secinfo_args si
;
1965 char *link
= NULL
, *p
, *q
, ch
;
1966 struct nfs_fs_path nfsp2
;
1968 bzero(&nfsp2
, sizeof(nfsp2
));
1970 NFSREQ_SECINFO_SET(&si
, NULL
, dirfhp
->fh_data
, dirfhp
->fh_len
, nfsp
->np_components
[curcomp
], 0);
1972 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, nfsp
->np_components
[curcomp
], 0);
1973 nfsm_chain_null(&nmreq
);
1974 nfsm_chain_null(&nmrep
);
1976 MALLOC_ZONE(link
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1982 nfsm_chain_build_alloc_init(error
, &nmreq
, 12 * NFSX_UNSIGNED
);
1983 nfsm_chain_add_compound_header(error
, &nmreq
, "readlink", nmp
->nm_minor_vers
, numops
);
1985 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1986 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, fhp
->fh_data
, fhp
->fh_len
);
1988 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READLINK
);
1989 nfsm_chain_build_done(error
, &nmreq
);
1990 nfsm_assert(error
, (numops
== 0), EPROTO
);
1993 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
1994 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
1996 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
1998 nfsm_chain_skip_tag(error
, &nmrep
);
1999 nfsm_chain_get_32(error
, &nmrep
, numops
);
2000 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
2001 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READLINK
);
2002 nfsm_chain_get_32(error
, &nmrep
, len
);
2006 else if (len
>= MAXPATHLEN
)
2007 len
= MAXPATHLEN
- 1;
2008 nfsm_chain_get_opaque(error
, &nmrep
, len
, link
);
2010 /* make sure link string is terminated properly */
2013 /* count the number of components in link */
2015 while (*p
&& (*p
== '/'))
2020 while (*p
&& (*p
!= '/'))
2022 while (*p
&& (*p
== '/'))
2026 /* free up used components */
2027 for (comp
=0; comp
<= curcomp
; comp
++) {
2028 if (nfsp
->np_components
[comp
]) {
2029 FREE(nfsp
->np_components
[comp
], M_TEMP
);
2030 nfsp
->np_components
[comp
] = NULL
;
2034 /* set up new path */
2035 nfsp2
.np_compcount
= nfsp
->np_compcount
- curcomp
- 1 + linkcompcount
;
2036 MALLOC(nfsp2
.np_components
, char **, nfsp2
.np_compcount
*sizeof(char*), M_TEMP
, M_WAITOK
|M_ZERO
);
2037 if (!nfsp2
.np_components
) {
2042 /* add link components */
2044 while (*p
&& (*p
== '/'))
2046 for (newcomp
=0; newcomp
< linkcompcount
; newcomp
++) {
2047 /* find end of component */
2049 while (*q
&& (*q
!= '/'))
2051 MALLOC(nfsp2
.np_components
[newcomp
], char *, q
-p
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
2052 if (!nfsp2
.np_components
[newcomp
]) {
2058 strlcpy(nfsp2
.np_components
[newcomp
], p
, q
-p
+1);
2061 while (*p
&& (*p
== '/'))
2066 /* add remaining components */
2067 for(comp
= curcomp
+ 1; comp
< nfsp
->np_compcount
; comp
++,newcomp
++) {
2068 nfsp2
.np_components
[newcomp
] = nfsp
->np_components
[comp
];
2069 nfsp
->np_components
[comp
] = NULL
;
2072 /* move new path into place */
2073 FREE(nfsp
->np_components
, M_TEMP
);
2074 nfsp
->np_components
= nfsp2
.np_components
;
2075 nfsp
->np_compcount
= nfsp2
.np_compcount
;
2076 nfsp2
.np_components
= NULL
;
2078 /* for absolute link, let the caller now that the next dirfh is root */
2079 if (link
[0] == '/') {
2085 FREE_ZONE(link
, MAXPATHLEN
, M_NAMEI
);
2086 if (nfsp2
.np_components
) {
2087 for (comp
=0; comp
< nfsp2
.np_compcount
; comp
++)
2088 if (nfsp2
.np_components
[comp
])
2089 FREE(nfsp2
.np_components
[comp
], M_TEMP
);
2090 FREE(nfsp2
.np_components
, M_TEMP
);
2092 nfsm_chain_cleanup(&nmreq
);
2093 nfsm_chain_cleanup(&nmrep
);
2097 /* Set up an NFSv4 mount */
2100 struct nfsmount
*nmp
,
2104 struct nfsm_chain nmreq
, nmrep
;
2105 int error
= 0, numops
, status
, interval
, isdotdot
, loopcnt
= 0, depth
= 0;
2106 struct nfs_fs_path fspath
, *nfsp
, fspath2
;
2107 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], comp
, comp2
;
2108 fhandle_t fh
, dirfh
;
2109 struct nfs_vattr nvattr
;
2111 struct nfsreq rq
, *req
= &rq
;
2112 struct nfsreq_secinfo_args si
;
2114 struct nfs_fs_locations nfsls
;
2117 fh
.fh_len
= dirfh
.fh_len
= 0;
2118 TAILQ_INIT(&nmp
->nm_open_owners
);
2119 TAILQ_INIT(&nmp
->nm_delegations
);
2120 TAILQ_INIT(&nmp
->nm_dreturnq
);
2121 nmp
->nm_stategenid
= 1;
2122 NVATTR_INIT(&nvattr
);
2123 bzero(&nfsls
, sizeof(nfsls
));
2124 nfsm_chain_null(&nmreq
);
2125 nfsm_chain_null(&nmrep
);
2128 * If no security flavors were specified we'll want to default to the server's
2129 * preferred flavor. For NFSv4.0 we need a file handle and name to get that via
2130 * SECINFO, so we'll do that on the last component of the server path we are
2131 * mounting. If we are mounting the server's root, we'll need to defer the
2132 * SECINFO call to the first successful LOOKUP request.
2134 if (!nmp
->nm_sec
.count
)
2135 nmp
->nm_state
|= NFSSTA_NEEDSECINFO
;
2137 /* make a copy of the current location's path */
2138 nfsp
= &nmp
->nm_locations
.nl_locations
[nmp
->nm_locations
.nl_current
.nli_loc
]->nl_path
;
2139 bzero(&fspath
, sizeof(fspath
));
2140 fspath
.np_compcount
= nfsp
->np_compcount
;
2141 if (fspath
.np_compcount
> 0) {
2142 MALLOC(fspath
.np_components
, char **, fspath
.np_compcount
*sizeof(char*), M_TEMP
, M_WAITOK
|M_ZERO
);
2143 if (!fspath
.np_components
) {
2147 for (comp
=0; comp
< nfsp
->np_compcount
; comp
++) {
2148 int slen
= strlen(nfsp
->np_components
[comp
]);
2149 MALLOC(fspath
.np_components
[comp
], char *, slen
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
2150 if (!fspath
.np_components
[comp
]) {
2154 strlcpy(fspath
.np_components
[comp
], nfsp
->np_components
[comp
], slen
+1);
2160 /* for mirror mounts, we can just use the file handle passed in */
2162 dirfh
.fh_len
= nmp
->nm_fh
->fh_len
;
2163 bcopy(nmp
->nm_fh
->fh_data
, dirfh
.fh_data
, dirfh
.fh_len
);
2164 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, NULL
, 0);
2168 /* otherwise, we need to get the fh for the directory we are mounting */
2170 /* if no components, just get root */
2171 if (fspath
.np_compcount
== 0) {
2173 // PUTROOTFH + GETATTR(FH)
2174 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, NULL
, 0);
2176 nfsm_chain_build_alloc_init(error
, &nmreq
, 9 * NFSX_UNSIGNED
);
2177 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2179 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTROOTFH
);
2181 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2182 NFS_CLEAR_ATTRIBUTES(bitmap
);
2183 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2184 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
2185 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2186 nfsm_chain_build_done(error
, &nmreq
);
2187 nfsm_assert(error
, (numops
== 0), EPROTO
);
2189 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2190 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2192 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2193 nfsm_chain_skip_tag(error
, &nmrep
);
2194 nfsm_chain_get_32(error
, &nmrep
, numops
);
2195 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTROOTFH
);
2196 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2198 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2199 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, &dirfh
, NULL
, NULL
);
2200 if (!error
&& !NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
2201 printf("nfs: mount didn't return filehandle?\n");
2205 nfsm_chain_cleanup(&nmrep
);
2206 nfsm_chain_null(&nmreq
);
2207 NVATTR_CLEANUP(&nvattr
);
2211 /* look up each path component */
2212 for (comp
=0; comp
< fspath
.np_compcount
; ) {
2214 if (fspath
.np_components
[comp
][0] == '.') {
2215 if (fspath
.np_components
[comp
][1] == '\0') {
2220 /* treat ".." specially */
2221 if ((fspath
.np_components
[comp
][1] == '.') &&
2222 (fspath
.np_components
[comp
][2] == '\0'))
2224 if (isdotdot
&& (dirfh
.fh_len
== 0)) {
2225 /* ".." in root directory is same as "." */
2230 // PUT(ROOT)FH + LOOKUP(P) + GETFH + GETATTR
2231 if (dirfh
.fh_len
== 0)
2232 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2234 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2236 nfsm_chain_build_alloc_init(error
, &nmreq
, 18 * NFSX_UNSIGNED
);
2237 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2240 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
2241 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, dirfh
.fh_data
, dirfh
.fh_len
);
2243 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTROOTFH
);
2247 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUPP
);
2249 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
2250 nfsm_chain_add_name(error
, &nmreq
,
2251 fspath
.np_components
[comp
], strlen(fspath
.np_components
[comp
]), nmp
);
2254 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETFH
);
2256 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2257 NFS_CLEAR_ATTRIBUTES(bitmap
);
2258 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2259 /* if no namedattr support or component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2260 if (NMFLAG(nmp
, NONAMEDATTR
) || !strcmp(fspath
.np_components
[comp
], ".zfs"))
2261 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
2262 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2263 nfsm_chain_build_done(error
, &nmreq
);
2264 nfsm_assert(error
, (numops
== 0), EPROTO
);
2266 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2267 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2269 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2270 nfsm_chain_skip_tag(error
, &nmrep
);
2271 nfsm_chain_get_32(error
, &nmrep
, numops
);
2272 nfsm_chain_op_check(error
, &nmrep
, dirfh
.fh_len
? NFS_OP_PUTFH
: NFS_OP_PUTROOTFH
);
2273 nfsm_chain_op_check(error
, &nmrep
, isdotdot
? NFS_OP_LOOKUPP
: NFS_OP_LOOKUP
);
2275 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETFH
);
2276 nfsm_chain_get_32(error
, &nmrep
, fh
.fh_len
);
2277 nfsm_chain_get_opaque(error
, &nmrep
, fh
.fh_len
, fh
.fh_data
);
2278 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2280 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2281 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, &nfsls
);
2283 nfsm_chain_cleanup(&nmrep
);
2284 nfsm_chain_null(&nmreq
);
2286 /* LOOKUP succeeded but GETATTR failed? This could be a referral. */
2287 /* Try the lookup again with a getattr for fs_locations. */
2288 nfs_fs_locations_cleanup(&nfsls
);
2289 error
= nfs4_get_fs_locations(nmp
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, fspath
.np_components
[comp
], ctx
, &nfsls
);
2290 if (!error
&& (nfsls
.nl_numlocs
< 1))
2293 if (++loopcnt
> MAXSYMLINKS
) {
2294 /* too many symlink/referral redirections */
2298 /* tear down the current connection */
2299 nfs_disconnect(nmp
);
2300 /* replace fs locations */
2301 nfs_fs_locations_cleanup(&nmp
->nm_locations
);
2302 nmp
->nm_locations
= nfsls
;
2303 bzero(&nfsls
, sizeof(nfsls
));
2304 /* initiate a connection using the new fs locations */
2305 error
= nfs_mount_connect(nmp
);
2306 if (!error
&& !(nmp
->nm_locations
.nl_current
.nli_flags
& NLI_VALID
))
2309 /* add new server's remote path to beginning of our path and continue */
2310 nfsp
= &nmp
->nm_locations
.nl_locations
[nmp
->nm_locations
.nl_current
.nli_loc
]->nl_path
;
2311 bzero(&fspath2
, sizeof(fspath2
));
2312 fspath2
.np_compcount
= (fspath
.np_compcount
- comp
- 1) + nfsp
->np_compcount
;
2313 if (fspath2
.np_compcount
> 0) {
2314 MALLOC(fspath2
.np_components
, char **, fspath2
.np_compcount
*sizeof(char*), M_TEMP
, M_WAITOK
|M_ZERO
);
2315 if (!fspath2
.np_components
) {
2319 for (comp2
=0; comp2
< nfsp
->np_compcount
; comp2
++) {
2320 int slen
= strlen(nfsp
->np_components
[comp2
]);
2321 MALLOC(fspath2
.np_components
[comp2
], char *, slen
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
2322 if (!fspath2
.np_components
[comp2
]) {
2323 /* clean up fspath2, then error out */
2326 FREE(fspath2
.np_components
[comp2
], M_TEMP
);
2328 FREE(fspath2
.np_components
, M_TEMP
);
2332 strlcpy(fspath2
.np_components
[comp2
], nfsp
->np_components
[comp2
], slen
+1);
2334 if ((fspath
.np_compcount
- comp
- 1) > 0)
2335 bcopy(&fspath
.np_components
[comp
+1], &fspath2
.np_components
[nfsp
->np_compcount
], (fspath
.np_compcount
- comp
- 1)*sizeof(char*));
2336 /* free up unused parts of old path (prior components and component array) */
2338 FREE(fspath
.np_components
[comp
], M_TEMP
);
2339 } while (comp
-- > 0);
2340 FREE(fspath
.np_components
, M_TEMP
);
2341 /* put new path in place */
2344 /* reset dirfh and component index */
2347 NVATTR_CLEANUP(&nvattr
);
2348 if (fspath
.np_compcount
== 0)
2353 /* if file handle is for a symlink, then update the path with the symlink contents */
2354 if (NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_TYPE
) && (nvattr
.nva_type
== VLNK
)) {
2355 if (++loopcnt
> MAXSYMLINKS
)
2358 error
= nfs4_mount_update_path_with_symlink(nmp
, &fspath
, comp
, &dirfh
, &depth
, &fh
, ctx
);
2360 /* directory file handle is either left the same or reset to root (if link was absolute) */
2361 /* path traversal starts at beginning of the path again */
2363 NVATTR_CLEANUP(&nvattr
);
2364 nfs_fs_locations_cleanup(&nfsls
);
2367 NVATTR_CLEANUP(&nvattr
);
2368 nfs_fs_locations_cleanup(&nfsls
);
2369 /* not a symlink... */
2370 if ((nmp
->nm_state
& NFSSTA_NEEDSECINFO
) && (comp
== (fspath
.np_compcount
-1)) && !isdotdot
) {
2371 /* need to get SECINFO for the directory being mounted */
2372 if (dirfh
.fh_len
== 0)
2373 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2375 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2376 sec
.count
= NX_MAX_SEC_FLAVORS
;
2377 error
= nfs4_secinfo_rpc(nmp
, &si
, vfs_context_ucred(ctx
), sec
.flavors
, &sec
.count
);
2378 /* [sigh] some implementations return "illegal" error for unsupported ops */
2379 if (error
== NFSERR_OP_ILLEGAL
)
2382 /* set our default security flavor to the first in the list */
2384 nmp
->nm_auth
= sec
.flavors
[0];
2385 nmp
->nm_state
&= ~NFSSTA_NEEDSECINFO
;
2387 /* advance directory file handle, component index, & update depth */
2390 if (!isdotdot
) /* going down the hierarchy */
2392 else if (--depth
<= 0) /* going up the hierarchy */
2393 dirfh
.fh_len
= 0; /* clear dirfh when we hit root */
2397 /* get attrs for mount point root */
2398 numops
= NMFLAG(nmp
, NONAMEDATTR
) ? 2 : 3; // PUTFH + GETATTR + OPENATTR
2399 nfsm_chain_build_alloc_init(error
, &nmreq
, 25 * NFSX_UNSIGNED
);
2400 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2402 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
2403 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, dirfh
.fh_data
, dirfh
.fh_len
);
2405 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2406 NFS_CLEAR_ATTRIBUTES(bitmap
);
2407 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2408 /* if no namedattr support or last component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2409 if (NMFLAG(nmp
, NONAMEDATTR
) || ((fspath
.np_compcount
> 0) && !strcmp(fspath
.np_components
[fspath
.np_compcount
-1], ".zfs")))
2410 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
2411 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2412 if (!NMFLAG(nmp
, NONAMEDATTR
)) {
2414 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
2415 nfsm_chain_add_32(error
, &nmreq
, 0);
2417 nfsm_chain_build_done(error
, &nmreq
);
2418 nfsm_assert(error
, (numops
== 0), EPROTO
);
2420 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2421 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2423 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2424 nfsm_chain_skip_tag(error
, &nmrep
);
2425 nfsm_chain_get_32(error
, &nmrep
, numops
);
2426 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
2427 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2429 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2430 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, NULL
);
2432 if (!NMFLAG(nmp
, NONAMEDATTR
)) {
2433 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
2434 if (error
== ENOENT
)
2436 /* [sigh] some implementations return "illegal" error for unsupported ops */
2437 if (error
|| !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_NAMED_ATTR
)) {
2438 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_NAMED_ATTR
;
2440 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_NAMED_ATTR
;
2443 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_NAMED_ATTR
;
2445 if (NMFLAG(nmp
, NOACL
)) /* make sure ACL support is turned off */
2446 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_ACL
;
2447 if (NMFLAG(nmp
, ACLONLY
) && !(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
))
2448 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
);
2449 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_FH_EXPIRE_TYPE
)) {
2450 uint32_t fhtype
= ((nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_FHTYPE_MASK
) >> NFS_FSFLAG_FHTYPE_SHIFT
);
2451 if (fhtype
!= NFS_FH_PERSISTENT
)
2452 printf("nfs: warning: non-persistent file handles! for %s\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
2455 /* make sure it's a directory */
2456 if (!NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_TYPE
) || (nvattr
.nva_type
!= VDIR
)) {
2461 /* save the NFS fsid */
2462 nmp
->nm_fsid
= nvattr
.nva_fsid
;
2464 /* create the root node */
2465 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MARKROOT
, npp
);
2468 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
)
2469 vfs_setextendedsecurity(nmp
->nm_mountp
);
2471 /* adjust I/O sizes to server limits */
2472 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXREAD
) && (nmp
->nm_fsattr
.nfsa_maxread
> 0)) {
2473 if (nmp
->nm_fsattr
.nfsa_maxread
< (uint64_t)nmp
->nm_rsize
) {
2474 nmp
->nm_rsize
= nmp
->nm_fsattr
.nfsa_maxread
& ~(NFS_FABLKSIZE
- 1);
2475 if (nmp
->nm_rsize
== 0)
2476 nmp
->nm_rsize
= nmp
->nm_fsattr
.nfsa_maxread
;
2479 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXWRITE
) && (nmp
->nm_fsattr
.nfsa_maxwrite
> 0)) {
2480 if (nmp
->nm_fsattr
.nfsa_maxwrite
< (uint64_t)nmp
->nm_wsize
) {
2481 nmp
->nm_wsize
= nmp
->nm_fsattr
.nfsa_maxwrite
& ~(NFS_FABLKSIZE
- 1);
2482 if (nmp
->nm_wsize
== 0)
2483 nmp
->nm_wsize
= nmp
->nm_fsattr
.nfsa_maxwrite
;
2487 /* set up lease renew timer */
2488 nmp
->nm_renew_timer
= thread_call_allocate(nfs4_renew_timer
, nmp
);
2489 interval
= nmp
->nm_fsattr
.nfsa_lease
/ 2;
2492 nfs_interval_timer_start(nmp
->nm_renew_timer
, interval
* 1000);
2495 if (fspath
.np_components
) {
2496 for (comp
=0; comp
< fspath
.np_compcount
; comp
++)
2497 if (fspath
.np_components
[comp
])
2498 FREE(fspath
.np_components
[comp
], M_TEMP
);
2499 FREE(fspath
.np_components
, M_TEMP
);
2501 NVATTR_CLEANUP(&nvattr
);
2502 nfs_fs_locations_cleanup(&nfsls
);
2504 nfs_node_unlock(*npp
);
2505 nfsm_chain_cleanup(&nmreq
);
2506 nfsm_chain_cleanup(&nmrep
);
2511 * Thread to handle initial NFS mount connection.
2514 nfs_mount_connect_thread(void *arg
, __unused wait_result_t wr
)
2516 struct nfsmount
*nmp
= arg
;
2517 int error
= 0, savederror
= 0, slpflag
= (NMFLAG(nmp
, INTR
) ? PCATCH
: 0);
2518 int done
= 0, timeo
, tries
, maxtries
;
2520 if (NM_OMFLAG(nmp
, MNTQUICK
)) {
2528 for (tries
= 0; tries
< maxtries
; tries
++) {
2529 error
= nfs_connect(nmp
, 1, timeo
);
2546 /* just keep retrying on any of these errors */
2550 /* looks like we got an answer... */
2555 /* save the best error */
2556 if (nfs_connect_error_class(error
) >= nfs_connect_error_class(savederror
))
2563 /* pause before next attempt */
2564 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 0)))
2566 error
= tsleep(nmp
, PSOCK
|slpflag
, "nfs_mount_connect_retry", 2*hz
);
2567 if (error
&& (error
!= EWOULDBLOCK
))
2572 /* update status of mount connect */
2573 lck_mtx_lock(&nmp
->nm_lock
);
2574 if (!nmp
->nm_mounterror
)
2575 nmp
->nm_mounterror
= error
;
2576 nmp
->nm_state
&= ~NFSSTA_MOUNT_THREAD
;
2577 lck_mtx_unlock(&nmp
->nm_lock
);
2578 wakeup(&nmp
->nm_nss
);
2582 nfs_mount_connect(struct nfsmount
*nmp
)
2584 int error
= 0, slpflag
;
2586 struct timespec ts
= { 2, 0 };
2589 * Set up the socket. Perform initial search for a location/server/address to
2590 * connect to and negotiate any unspecified mount parameters. This work is
2591 * done on a kernel thread to satisfy reserved port usage needs.
2593 slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
2594 lck_mtx_lock(&nmp
->nm_lock
);
2595 /* set flag that the thread is running */
2596 nmp
->nm_state
|= NFSSTA_MOUNT_THREAD
;
2597 if (kernel_thread_start(nfs_mount_connect_thread
, nmp
, &thd
) != KERN_SUCCESS
) {
2598 nmp
->nm_state
&= ~NFSSTA_MOUNT_THREAD
;
2599 nmp
->nm_mounterror
= EIO
;
2600 printf("nfs mount %s start socket connect thread failed\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
2602 thread_deallocate(thd
);
2605 /* wait until mount connect thread is finished/gone */
2606 while (nmp
->nm_state
& NFSSTA_MOUNT_THREAD
) {
2607 error
= msleep(&nmp
->nm_nss
, &nmp
->nm_lock
, slpflag
|PSOCK
, "nfsconnectthread", &ts
);
2608 if ((error
&& (error
!= EWOULDBLOCK
)) || ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 1)))) {
2610 if (!nmp
->nm_mounterror
)
2611 nmp
->nm_mounterror
= error
;
2612 /* signal the thread that we are aborting */
2613 nmp
->nm_sockflags
|= NMSOCK_UNMOUNT
;
2615 wakeup(nmp
->nm_nss
);
2616 /* and continue waiting on it to finish */
2620 lck_mtx_unlock(&nmp
->nm_lock
);
2622 /* grab mount connect status */
2623 error
= nmp
->nm_mounterror
;
2628 /* Table of maximum minor version for a given version */
2629 uint32_t maxminorverstab
[] = {
2630 0, /* Version 0 (does not exist) */
2631 0, /* Version 1 (does not exist) */
2637 #define NFS_MAX_SUPPORTED_VERSION ((long)(sizeof (maxminorverstab) / sizeof (uint32_t) - 1))
2638 #define NFS_MAX_SUPPORTED_MINOR_VERSION(v) ((long)(maxminorverstab[(v)]))
2640 #define DEFAULT_NFS_MIN_VERS VER2PVER(2, 0)
2641 #define DEFAULT_NFS_MAX_VERS VER2PVER(3, 0)
2644 * Common code to mount an NFS file system.
2653 struct nfsmount
*nmp
;
2656 struct vfsstatfs
*sbp
;
2658 uint32_t i
, val
, maxio
, iosize
, len
;
2660 uint32_t *mflags_mask
;
2662 uint32_t argslength
, attrslength
;
2663 struct nfs_location_index firstloc
= { NLI_VALID
, 0, 0, 0 };
2664 static const struct nfs_etype nfs_default_etypes
= {
2665 .count
= NFS_MAX_ETYPES
,
2666 .selected
= NFS_MAX_ETYPES
,
2667 .etypes
= { NFS_AES256_CTS_HMAC_SHA1_96
,
2668 NFS_AES128_CTS_HMAC_SHA1_96
,
2669 NFS_DES3_CBC_SHA1_KD
2672 /* make sure mbuf constants are set up */
2673 if (!nfs_mbuf_mhlen
)
2676 if (vfs_flags(mp
) & MNT_UPDATE
) {
2678 /* update paths, file handles, etc, here XXX */
2682 /* allocate an NFS mount structure for this mount */
2683 MALLOC_ZONE(nmp
, struct nfsmount
*,
2684 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
2689 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
2690 lck_mtx_init(&nmp
->nm_lock
, nfs_mount_grp
, LCK_ATTR_NULL
);
2691 TAILQ_INIT(&nmp
->nm_resendq
);
2692 TAILQ_INIT(&nmp
->nm_iodq
);
2693 TAILQ_INIT(&nmp
->nm_gsscl
);
2694 LIST_INIT(&nmp
->nm_monlist
);
2695 vfs_setfsprivate(mp
, nmp
);
2697 nmp
->nm_mountp
= mp
;
2698 vfs_setauthopaque(mp
);
2700 nfs_nhinit_finish();
2702 nmp
->nm_args
= xdrbuf
;
2704 /* set up defaults */
2707 nmp
->nm_min_vers
= DEFAULT_NFS_MIN_VERS
;
2708 nmp
->nm_max_vers
= DEFAULT_NFS_MAX_VERS
;
2709 nmp
->nm_timeo
= NFS_TIMEO
;
2710 nmp
->nm_retry
= NFS_RETRANS
;
2712 nmp
->nm_sofamily
= 0;
2713 nmp
->nm_nfsport
= 0;
2714 nmp
->nm_wsize
= NFS_WSIZE
;
2715 nmp
->nm_rsize
= NFS_RSIZE
;
2716 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
2717 nmp
->nm_numgrps
= NFS_MAXGRPS
;
2718 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
2719 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
2720 if (nmp
->nm_tprintf_delay
< 0)
2721 nmp
->nm_tprintf_delay
= 0;
2722 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
2723 if (nmp
->nm_tprintf_initial_delay
< 0)
2724 nmp
->nm_tprintf_initial_delay
= 0;
2725 nmp
->nm_acregmin
= NFS_MINATTRTIMO
;
2726 nmp
->nm_acregmax
= NFS_MAXATTRTIMO
;
2727 nmp
->nm_acdirmin
= NFS_MINDIRATTRTIMO
;
2728 nmp
->nm_acdirmax
= NFS_MAXDIRATTRTIMO
;
2729 nmp
->nm_etype
= nfs_default_etypes
;
2730 nmp
->nm_auth
= RPCAUTH_SYS
;
2731 nmp
->nm_iodlink
.tqe_next
= NFSNOLIST
;
2732 nmp
->nm_deadtimeout
= 0;
2733 nmp
->nm_curdeadtimeout
= 0;
2734 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
2735 nmp
->nm_realm
= NULL
;
2736 nmp
->nm_principal
= NULL
;
2737 nmp
->nm_sprinc
= NULL
;
2740 mattrs
= nmp
->nm_mattrs
;
2741 mflags
= nmp
->nm_mflags
;
2742 mflags_mask
= nmp
->nm_mflags_mask
;
2744 /* set up NFS mount with args */
2745 xb_init_buffer(&xb
, xdrbuf
, 2*XDRWORD
);
2746 xb_get_32(error
, &xb
, val
); /* version */
2747 xb_get_32(error
, &xb
, argslength
); /* args length */
2749 xb_init_buffer(&xb
, xdrbuf
, argslength
); /* restart parsing with actual buffer length */
2750 xb_get_32(error
, &xb
, val
); /* version */
2751 xb_get_32(error
, &xb
, argslength
); /* args length */
2752 xb_get_32(error
, &xb
, val
); /* XDR args version */
2753 if (val
!= NFS_XDRARGS_VERSION_0
)
2755 len
= NFS_MATTR_BITMAP_LEN
;
2756 xb_get_bitmap(error
, &xb
, mattrs
, len
); /* mount attribute bitmap */
2758 xb_get_32(error
, &xb
, attrslength
); /* attrs length */
2759 if (!error
&& (attrslength
> (argslength
- ((4+NFS_MATTR_BITMAP_LEN
+1)*XDRWORD
))))
2762 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FLAGS
)) {
2763 len
= NFS_MFLAG_BITMAP_LEN
;
2764 xb_get_bitmap(error
, &xb
, mflags_mask
, len
); /* mount flag mask */
2765 len
= NFS_MFLAG_BITMAP_LEN
;
2766 xb_get_bitmap(error
, &xb
, mflags
, len
); /* mount flag values */
2768 /* clear all mask bits and OR in all the ones that are set */
2769 nmp
->nm_flags
[0] &= ~mflags_mask
[0];
2770 nmp
->nm_flags
[0] |= (mflags_mask
[0] & mflags
[0]);
2773 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
)) {
2774 /* Can't specify a single version and a range */
2775 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
))
2777 xb_get_32(error
, &xb
, nmp
->nm_vers
);
2778 if (nmp
->nm_vers
> NFS_MAX_SUPPORTED_VERSION
||
2779 nmp
->nm_vers
< NFS_VER2
)
2781 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
))
2782 xb_get_32(error
, &xb
, nmp
->nm_minor_vers
);
2784 nmp
->nm_minor_vers
= maxminorverstab
[nmp
->nm_vers
];
2785 if (nmp
->nm_minor_vers
> maxminorverstab
[nmp
->nm_vers
])
2787 nmp
->nm_max_vers
= nmp
->nm_min_vers
=
2788 VER2PVER(nmp
->nm_vers
, nmp
->nm_minor_vers
);
2790 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
)) {
2791 /* should have also gotten NFS version (and already gotten minor version) */
2792 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
))
2795 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
)) {
2796 xb_get_32(error
, &xb
, nmp
->nm_min_vers
);
2797 xb_get_32(error
, &xb
, nmp
->nm_max_vers
);
2798 if ((nmp
->nm_min_vers
> nmp
->nm_max_vers
) ||
2799 (PVER2MAJOR(nmp
->nm_max_vers
) > NFS_MAX_SUPPORTED_VERSION
) ||
2800 (PVER2MINOR(nmp
->nm_min_vers
) > maxminorverstab
[PVER2MAJOR(nmp
->nm_min_vers
)]) ||
2801 (PVER2MINOR(nmp
->nm_max_vers
) > maxminorverstab
[PVER2MAJOR(nmp
->nm_max_vers
)]))
2804 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
))
2805 xb_get_32(error
, &xb
, nmp
->nm_rsize
);
2806 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
))
2807 xb_get_32(error
, &xb
, nmp
->nm_wsize
);
2808 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
))
2809 xb_get_32(error
, &xb
, nmp
->nm_readdirsize
);
2810 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
))
2811 xb_get_32(error
, &xb
, nmp
->nm_readahead
);
2812 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
2813 xb_get_32(error
, &xb
, nmp
->nm_acregmin
);
2814 xb_skip(error
, &xb
, XDRWORD
);
2816 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
2817 xb_get_32(error
, &xb
, nmp
->nm_acregmax
);
2818 xb_skip(error
, &xb
, XDRWORD
);
2820 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
2821 xb_get_32(error
, &xb
, nmp
->nm_acdirmin
);
2822 xb_skip(error
, &xb
, XDRWORD
);
2824 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
2825 xb_get_32(error
, &xb
, nmp
->nm_acdirmax
);
2826 xb_skip(error
, &xb
, XDRWORD
);
2829 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
)) {
2830 xb_get_32(error
, &xb
, val
);
2832 case NFS_LOCK_MODE_DISABLED
:
2833 case NFS_LOCK_MODE_LOCAL
:
2834 if (nmp
->nm_vers
>= NFS_VER4
) {
2835 /* disabled/local lock mode only allowed on v2/v3 */
2840 case NFS_LOCK_MODE_ENABLED
:
2841 nmp
->nm_lockmode
= val
;
2848 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
2850 xb_get_32(error
, &xb
, seccnt
);
2851 if (!error
&& ((seccnt
< 1) || (seccnt
> NX_MAX_SEC_FLAVORS
)))
2854 nmp
->nm_sec
.count
= seccnt
;
2855 for (i
=0; i
< seccnt
; i
++) {
2856 xb_get_32(error
, &xb
, nmp
->nm_sec
.flavors
[i
]);
2857 /* Check for valid security flavor */
2858 switch (nmp
->nm_sec
.flavors
[i
]) {
2869 /* start with the first flavor */
2870 nmp
->nm_auth
= nmp
->nm_sec
.flavors
[0];
2872 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_KERB_ETYPE
)) {
2874 xb_get_32(error
, &xb
, etypecnt
);
2875 if (!error
&& ((etypecnt
< 1) || (etypecnt
> NFS_MAX_ETYPES
)))
2878 nmp
->nm_etype
.count
= etypecnt
;
2879 xb_get_32(error
, &xb
, nmp
->nm_etype
.selected
);
2882 nmp
->nm_etype
.selected
= etypecnt
; /* Nothing is selected yet, so set selected to count */
2883 for (i
=0; i
< etypecnt
; i
++) {
2884 xb_get_32(error
, &xb
, nmp
->nm_etype
.etypes
[i
]);
2885 /* Check for valid encryption type */
2886 switch (nmp
->nm_etype
.etypes
[i
]) {
2887 case NFS_DES3_CBC_SHA1_KD
:
2888 case NFS_AES128_CTS_HMAC_SHA1_96
:
2889 case NFS_AES256_CTS_HMAC_SHA1_96
:
2897 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
))
2898 xb_get_32(error
, &xb
, nmp
->nm_numgrps
);
2899 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
)) {
2902 xb_get_32(error
, &xb
, val
);
2903 if (!error
&& ((val
< 3) || (val
> 5)))
2906 error
= xb_get_bytes(&xb
, sotype
, val
, 0);
2909 if (!strcmp(sotype
, "tcp")) {
2910 nmp
->nm_sotype
= SOCK_STREAM
;
2911 } else if (!strcmp(sotype
, "udp")) {
2912 nmp
->nm_sotype
= SOCK_DGRAM
;
2913 } else if (!strcmp(sotype
, "tcp4")) {
2914 nmp
->nm_sotype
= SOCK_STREAM
;
2915 nmp
->nm_sofamily
= AF_INET
;
2916 } else if (!strcmp(sotype
, "udp4")) {
2917 nmp
->nm_sotype
= SOCK_DGRAM
;
2918 nmp
->nm_sofamily
= AF_INET
;
2919 } else if (!strcmp(sotype
, "tcp6")) {
2920 nmp
->nm_sotype
= SOCK_STREAM
;
2921 nmp
->nm_sofamily
= AF_INET6
;
2922 } else if (!strcmp(sotype
, "udp6")) {
2923 nmp
->nm_sotype
= SOCK_DGRAM
;
2924 nmp
->nm_sofamily
= AF_INET6
;
2925 } else if (!strcmp(sotype
, "inet4")) {
2926 nmp
->nm_sofamily
= AF_INET
;
2927 } else if (!strcmp(sotype
, "inet6")) {
2928 nmp
->nm_sofamily
= AF_INET6
;
2929 } else if (!strcmp(sotype
, "inet")) {
2930 nmp
->nm_sofamily
= 0; /* ok */
2934 if (!error
&& (nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_sotype
&&
2935 (nmp
->nm_sotype
!= SOCK_STREAM
))
2936 error
= EINVAL
; /* NFSv4 is only allowed over TCP. */
2939 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
))
2940 xb_get_32(error
, &xb
, nmp
->nm_nfsport
);
2941 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MOUNT_PORT
))
2942 xb_get_32(error
, &xb
, nmp
->nm_mountport
);
2943 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
2944 /* convert from time to 0.1s units */
2945 xb_get_32(error
, &xb
, nmp
->nm_timeo
);
2946 xb_get_32(error
, &xb
, val
);
2948 if (val
>= 1000000000)
2951 nmp
->nm_timeo
*= 10;
2952 nmp
->nm_timeo
+= (val
+100000000-1)/100000000;
2953 /* now convert to ticks */
2954 nmp
->nm_timeo
= (nmp
->nm_timeo
* NFS_HZ
+ 5) / 10;
2956 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
)) {
2957 xb_get_32(error
, &xb
, val
);
2958 if (!error
&& (val
> 1))
2959 nmp
->nm_retry
= val
;
2961 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
2962 xb_get_32(error
, &xb
, nmp
->nm_deadtimeout
);
2963 xb_skip(error
, &xb
, XDRWORD
);
2965 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
)) {
2967 MALLOC(nmp
->nm_fh
, fhandle_t
*, sizeof(fhandle_t
), M_TEMP
, M_WAITOK
|M_ZERO
);
2970 xb_get_32(error
, &xb
, nmp
->nm_fh
->fh_len
);
2972 if (nmp
->nm_fh
->fh_len
< 0 ||
2973 (size_t)nmp
->nm_fh
->fh_len
> sizeof(nmp
->nm_fh
->fh_data
))
2976 error
= xb_get_bytes(&xb
, (char*)&nmp
->nm_fh
->fh_data
[0], nmp
->nm_fh
->fh_len
, 0);
2979 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
2980 uint32_t loc
, serv
, addr
, comp
;
2981 struct nfs_fs_location
*fsl
;
2982 struct nfs_fs_server
*fss
;
2983 struct nfs_fs_path
*fsp
;
2985 xb_get_32(error
, &xb
, nmp
->nm_locations
.nl_numlocs
); /* fs location count */
2986 /* sanity check location count */
2987 if (!error
&& ((nmp
->nm_locations
.nl_numlocs
< 1) || (nmp
->nm_locations
.nl_numlocs
> 256)))
2990 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
);
2991 if (!nmp
->nm_locations
.nl_locations
)
2993 for (loc
= 0; loc
< nmp
->nm_locations
.nl_numlocs
; loc
++) {
2995 MALLOC(fsl
, struct nfs_fs_location
*, sizeof(struct nfs_fs_location
), M_TEMP
, M_WAITOK
|M_ZERO
);
2998 nmp
->nm_locations
.nl_locations
[loc
] = fsl
;
2999 xb_get_32(error
, &xb
, fsl
->nl_servcount
); /* server count */
3000 /* sanity check server count */
3001 if (!error
&& ((fsl
->nl_servcount
< 1) || (fsl
->nl_servcount
> 256)))
3004 MALLOC(fsl
->nl_servers
, struct nfs_fs_server
**, fsl
->nl_servcount
* sizeof(struct nfs_fs_server
*), M_TEMP
, M_WAITOK
|M_ZERO
);
3005 if (!fsl
->nl_servers
)
3007 for (serv
= 0; serv
< fsl
->nl_servcount
; serv
++) {
3009 MALLOC(fss
, struct nfs_fs_server
*, sizeof(struct nfs_fs_server
), M_TEMP
, M_WAITOK
|M_ZERO
);
3012 fsl
->nl_servers
[serv
] = fss
;
3013 xb_get_32(error
, &xb
, val
); /* server name length */
3014 /* sanity check server name length */
3015 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
)))
3018 MALLOC(fss
->ns_name
, char *, val
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
3022 error
= xb_get_bytes(&xb
, fss
->ns_name
, val
, 0); /* server name */
3023 xb_get_32(error
, &xb
, fss
->ns_addrcount
); /* address count */
3024 /* sanity check address count (OK to be zero) */
3025 if (!error
&& (fss
->ns_addrcount
> 256))
3028 if (fss
->ns_addrcount
> 0) {
3029 MALLOC(fss
->ns_addresses
, char **, fss
->ns_addrcount
* sizeof(char *), M_TEMP
, M_WAITOK
|M_ZERO
);
3030 if (!fss
->ns_addresses
)
3032 for (addr
= 0; addr
< fss
->ns_addrcount
; addr
++) {
3033 xb_get_32(error
, &xb
, val
); /* address length */
3034 /* sanity check address length */
3035 if (!error
&& ((val
< 1) || (val
> 128)))
3038 MALLOC(fss
->ns_addresses
[addr
], char *, val
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
3039 if (!fss
->ns_addresses
[addr
])
3042 error
= xb_get_bytes(&xb
, fss
->ns_addresses
[addr
], val
, 0); /* address */
3045 xb_get_32(error
, &xb
, val
); /* server info length */
3046 xb_skip(error
, &xb
, val
); /* skip server info */
3049 fsp
= &fsl
->nl_path
;
3050 xb_get_32(error
, &xb
, fsp
->np_compcount
); /* component count */
3051 /* sanity check component count */
3052 if (!error
&& (fsp
->np_compcount
> MAXPATHLEN
))
3055 if (fsp
->np_compcount
) {
3056 MALLOC(fsp
->np_components
, char **, fsp
->np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
|M_ZERO
);
3057 if (!fsp
->np_components
)
3060 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++) {
3061 xb_get_32(error
, &xb
, val
); /* component length */
3062 /* sanity check component length */
3063 if (!error
&& (val
== 0)) {
3065 * Apparently some people think a path with zero components should
3066 * be encoded with one zero-length component. So, just ignore any
3067 * zero length components.
3070 fsp
->np_compcount
--;
3071 if (fsp
->np_compcount
== 0) {
3072 FREE(fsp
->np_components
, M_TEMP
);
3073 fsp
->np_components
= NULL
;
3077 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
)))
3080 MALLOC(fsp
->np_components
[comp
], char *, val
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
3081 if (!fsp
->np_components
[comp
])
3084 error
= xb_get_bytes(&xb
, fsp
->np_components
[comp
], val
, 0); /* component */
3086 xb_get_32(error
, &xb
, val
); /* fs location info length */
3087 xb_skip(error
, &xb
, val
); /* skip fs location info */
3090 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
))
3091 xb_skip(error
, &xb
, XDRWORD
);
3092 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
)) {
3093 xb_get_32(error
, &xb
, len
);
3096 if (val
>= sizeof(vfs_statfs(mp
)->f_mntfromname
))
3097 val
= sizeof(vfs_statfs(mp
)->f_mntfromname
) - 1;
3098 error
= xb_get_bytes(&xb
, vfs_statfs(mp
)->f_mntfromname
, val
, 0);
3099 if ((len
- val
) > 0)
3100 xb_skip(error
, &xb
, len
- val
);
3102 vfs_statfs(mp
)->f_mntfromname
[val
] = '\0';
3106 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REALM
)) {
3107 xb_get_32(error
, &xb
, len
);
3108 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
)))
3111 /* allocate an extra byte for a leading '@' if its not already prepended to the realm */
3112 MALLOC(nmp
->nm_realm
, char *, len
+2, M_TEMP
, M_WAITOK
|M_ZERO
);
3116 error
= xb_get_bytes(&xb
, nmp
->nm_realm
, len
, 0);
3117 if (error
== 0 && *nmp
->nm_realm
!= '@') {
3118 bcopy(nmp
->nm_realm
, &nmp
->nm_realm
[1], len
);
3119 nmp
->nm_realm
[0] = '@';
3124 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_PRINCIPAL
)) {
3125 xb_get_32(error
, &xb
, len
);
3126 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
)))
3129 MALLOC(nmp
->nm_principal
, char *, len
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
3130 if (!nmp
->nm_principal
)
3133 error
= xb_get_bytes(&xb
, nmp
->nm_principal
, len
, 0);
3137 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SVCPRINCIPAL
)) {
3138 xb_get_32(error
, &xb
, len
);
3139 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
)))
3142 MALLOC(nmp
->nm_sprinc
, char *, len
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
3143 if (!nmp
->nm_sprinc
)
3146 error
= xb_get_bytes(&xb
, nmp
->nm_sprinc
, len
, 0);
3151 * Sanity check/finalize settings.
3154 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
3155 nmp
->nm_timeo
= NFS_MINTIMEO
;
3156 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
3157 nmp
->nm_timeo
= NFS_MAXTIMEO
;
3158 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
3159 nmp
->nm_retry
= NFS_MAXREXMIT
;
3161 if (nmp
->nm_numgrps
> NFS_MAXGRPS
)
3162 nmp
->nm_numgrps
= NFS_MAXGRPS
;
3163 if (nmp
->nm_readahead
> NFS_MAXRAHEAD
)
3164 nmp
->nm_readahead
= NFS_MAXRAHEAD
;
3165 if (nmp
->nm_acregmin
> nmp
->nm_acregmax
)
3166 nmp
->nm_acregmin
= nmp
->nm_acregmax
;
3167 if (nmp
->nm_acdirmin
> nmp
->nm_acdirmax
)
3168 nmp
->nm_acdirmin
= nmp
->nm_acdirmax
;
3170 /* need at least one fs location */
3171 if (nmp
->nm_locations
.nl_numlocs
< 1)
3175 /* init mount's mntfromname to first location */
3176 if (!NM_OMATTR_GIVEN(nmp
, MNTFROM
))
3177 nfs_location_mntfromname(&nmp
->nm_locations
, firstloc
,
3178 vfs_statfs(mp
)->f_mntfromname
, sizeof(vfs_statfs(mp
)->f_mntfromname
), 0);
3180 /* Need to save the mounting credential for v4. */
3181 nmp
->nm_mcred
= vfs_context_ucred(ctx
);
3182 if (IS_VALID_CRED(nmp
->nm_mcred
))
3183 kauth_cred_ref(nmp
->nm_mcred
);
3186 * If a reserved port is required, check for that privilege.
3187 * (Note that mirror mounts are exempt because the privilege was
3188 * already checked for the original mount.)
3190 if (NMFLAG(nmp
, RESVPORT
) && !vfs_iskernelmount(mp
))
3191 error
= priv_check_cred(nmp
->nm_mcred
, PRIV_NETINET_RESERVEDPORT
, 0);
3194 /* do mount's initial socket connection */
3195 error
= nfs_mount_connect(nmp
);
3198 /* set up the version-specific function tables */
3199 if (nmp
->nm_vers
< NFS_VER4
)
3200 nmp
->nm_funcs
= &nfs3_funcs
;
3202 nmp
->nm_funcs
= &nfs4_funcs
;
3204 /* sanity check settings now that version/connection is set */
3205 if (nmp
->nm_vers
== NFS_VER2
) /* ignore RDIRPLUS on NFSv2 */
3206 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
3207 if (nmp
->nm_vers
>= NFS_VER4
) {
3208 if (NFS_BITMAP_ISSET(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
)) /* aclonly trumps noacl */
3209 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
3210 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_CALLUMNT
);
3211 if (nmp
->nm_lockmode
!= NFS_LOCK_MODE_ENABLED
)
3212 error
= EINVAL
; /* disabled/local lock mode only allowed on v2/v3 */
3214 /* ignore these if not v4 */
3215 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOCALLBACK
);
3216 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NONAMEDATTR
);
3217 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
3218 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
);
3222 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
3223 /* I/O size defaults for UDP are different */
3224 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
))
3225 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
3226 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
))
3227 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
3230 /* round down I/O sizes to multiple of NFS_FABLKSIZE */
3231 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
3232 if (nmp
->nm_rsize
<= 0)
3233 nmp
->nm_rsize
= NFS_FABLKSIZE
;
3234 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
3235 if (nmp
->nm_wsize
<= 0)
3236 nmp
->nm_wsize
= NFS_FABLKSIZE
;
3238 /* and limit I/O sizes to maximum allowed */
3239 maxio
= (nmp
->nm_vers
== NFS_VER2
) ? NFS_V2MAXDATA
:
3240 (nmp
->nm_sotype
== SOCK_DGRAM
) ? NFS_MAXDGRAMDATA
: NFS_MAXDATA
;
3241 if (maxio
> NFS_MAXBSIZE
)
3242 maxio
= NFS_MAXBSIZE
;
3243 if (nmp
->nm_rsize
> maxio
)
3244 nmp
->nm_rsize
= maxio
;
3245 if (nmp
->nm_wsize
> maxio
)
3246 nmp
->nm_wsize
= maxio
;
3248 if (nmp
->nm_readdirsize
> maxio
)
3249 nmp
->nm_readdirsize
= maxio
;
3250 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
3251 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
3253 /* Set up the sockets and related info */
3254 if (nmp
->nm_sotype
== SOCK_DGRAM
)
3255 TAILQ_INIT(&nmp
->nm_cwndq
);
3258 * Get the root node/attributes from the NFS server and
3259 * do any basic, version-specific setup.
3261 error
= nmp
->nm_funcs
->nf_mount(nmp
, ctx
, &np
);
3265 * A reference count is needed on the node representing the
3266 * remote root. If this object is not persistent, then backward
3267 * traversals of the mount point (i.e. "..") will not work if
3268 * the node gets flushed out of the cache.
3272 /* get usecount and drop iocount */
3273 error
= vnode_ref(*vpp
);
3276 vnode_recycle(*vpp
);
3281 * Do statfs to ensure static info gets set to reasonable values.
3283 if ((error
= nmp
->nm_funcs
->nf_update_statfs(nmp
, ctx
))) {
3284 int error2
= vnode_getwithref(*vpp
);
3288 vnode_recycle(*vpp
);
3291 sbp
= vfs_statfs(mp
);
3292 sbp
->f_bsize
= nmp
->nm_fsattr
.nfsa_bsize
;
3293 sbp
->f_blocks
= nmp
->nm_fsattr
.nfsa_space_total
/ sbp
->f_bsize
;
3294 sbp
->f_bfree
= nmp
->nm_fsattr
.nfsa_space_free
/ sbp
->f_bsize
;
3295 sbp
->f_bavail
= nmp
->nm_fsattr
.nfsa_space_avail
/ sbp
->f_bsize
;
3296 sbp
->f_bused
= (nmp
->nm_fsattr
.nfsa_space_total
/ sbp
->f_bsize
) -
3297 (nmp
->nm_fsattr
.nfsa_space_free
/ sbp
->f_bsize
);
3298 sbp
->f_files
= nmp
->nm_fsattr
.nfsa_files_total
;
3299 sbp
->f_ffree
= nmp
->nm_fsattr
.nfsa_files_free
;
3300 sbp
->f_iosize
= nfs_iosize
;
3303 * Calculate the size used for I/O buffers. Use the larger
3304 * of the two sizes to minimise NFS requests but make sure
3305 * that it is at least one VM page to avoid wasting buffer
3306 * space and to allow easy mmapping of I/O buffers.
3307 * The read/write RPC calls handle the splitting up of
3308 * buffers into multiple requests if the buffer size is
3309 * larger than the I/O size.
3311 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
3312 if (iosize
< PAGE_SIZE
)
3314 nmp
->nm_biosize
= trunc_page_32(iosize
);
3316 /* For NFSv3 and greater, there is a (relatively) reliable ACCESS call. */
3317 if (nmp
->nm_vers
> NFS_VER2
)
3318 vfs_setauthopaqueaccess(mp
);
3320 switch (nmp
->nm_lockmode
) {
3321 case NFS_LOCK_MODE_DISABLED
:
3323 case NFS_LOCK_MODE_LOCAL
:
3324 vfs_setlocklocal(nmp
->nm_mountp
);
3326 case NFS_LOCK_MODE_ENABLED
:
3328 if (nmp
->nm_vers
<= NFS_VER3
)
3329 nfs_lockd_mount_register(nmp
);
3334 lck_mtx_lock(&nmp
->nm_lock
);
3335 nmp
->nm_state
|= NFSSTA_MOUNTED
;
3336 lck_mtx_unlock(&nmp
->nm_lock
);
3339 nfs_mount_drain_and_cleanup(nmp
);
3346 * We've detected a file system boundary on the server and
3347 * need to mount a new file system so that our file systems
3348 * MIRROR the file systems on the server.
3350 * Build the mount arguments for the new mount and call kernel_mount().
3353 nfs_mirror_mount_domount(vnode_t dvp
, vnode_t vp
, vfs_context_t ctx
)
3355 nfsnode_t np
= VTONFS(vp
);
3356 nfsnode_t dnp
= VTONFS(dvp
);
3357 struct nfsmount
*nmp
= NFSTONMP(np
);
3358 char fstype
[MFSTYPENAMELEN
], *mntfromname
= NULL
, *path
= NULL
, *relpath
, *p
, *cp
;
3359 int error
= 0, pathbuflen
= MAXPATHLEN
, i
, mntflags
= 0, referral
, skipcopy
= 0;
3361 struct xdrbuf xb
, xbnew
;
3362 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
3363 uint32_t newmattrs
[NFS_MATTR_BITMAP_LEN
];
3364 uint32_t newmflags
[NFS_MFLAG_BITMAP_LEN
];
3365 uint32_t newmflags_mask
[NFS_MFLAG_BITMAP_LEN
];
3366 uint32_t argslength
= 0, val
, count
, mlen
, mlen2
, rlen
, relpathcomps
;
3367 uint32_t argslength_offset
, attrslength_offset
, end_offset
;
3368 uint32_t numlocs
, loc
, numserv
, serv
, numaddr
, addr
, numcomp
, comp
;
3370 struct nfs_fs_locations nfsls
;
3372 referral
= (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
);
3374 bzero(&nfsls
, sizeof(nfsls
));
3378 if (!nmp
|| (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)))
3381 /* allocate a couple path buffers we need */
3382 MALLOC_ZONE(mntfromname
, char *, pathbuflen
, M_NAMEI
, M_WAITOK
);
3387 MALLOC_ZONE(path
, char *, pathbuflen
, M_NAMEI
, M_WAITOK
);
3393 /* get the path for the directory being mounted on */
3394 error
= vn_getpath(vp
, path
, &pathbuflen
);
3401 * Set up the mntfromname for the new mount based on the
3402 * current mount's mntfromname and the directory's path
3403 * relative to the current mount's mntonname.
3404 * Set up relpath to point at the relative path on the current mount.
3405 * Also, count the number of components in relpath.
3406 * We'll be adding those to each fs location path in the new args.
3408 nlen
= strlcpy(mntfromname
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, MAXPATHLEN
);
3409 if ((nlen
> 0) && (mntfromname
[nlen
-1] == '/')) { /* avoid double '/' in new name */
3410 mntfromname
[nlen
-1] = '\0';
3413 relpath
= mntfromname
+ nlen
;
3414 nlen
= strlcat(mntfromname
, path
+ strlen(vfs_statfs(nmp
->nm_mountp
)->f_mntonname
), MAXPATHLEN
);
3415 if (nlen
>= MAXPATHLEN
) {
3416 error
= ENAMETOOLONG
;
3419 /* count the number of components in relpath */
3421 while (*p
&& (*p
== '/'))
3426 while (*p
&& (*p
!= '/'))
3428 while (*p
&& (*p
== '/'))
3432 /* grab a copy of the file system type */
3433 vfs_name(vnode_mount(vp
), fstype
);
3435 /* for referrals, fetch the fs locations */
3437 const char *vname
= vnode_getname(NFSTOV(np
));
3441 error
= nfs4_get_fs_locations(nmp
, dnp
, NULL
, 0, vname
, ctx
, &nfsls
);
3442 vnode_putname(vname
);
3443 if (!error
&& (nfsls
.nl_numlocs
< 1))
3449 /* set up NFS mount args based on current mount args */
3451 #define xb_copy_32(E, XBSRC, XBDST, V) \
3454 xb_get_32((E), (XBSRC), (V)); \
3455 if (skipcopy) break; \
3456 xb_add_32((E), (XBDST), (V)); \
3458 #define xb_copy_opaque(E, XBSRC, XBDST) \
3460 uint32_t __count, __val; \
3461 xb_copy_32((E), (XBSRC), (XBDST), __count); \
3463 __count = nfsm_rndup(__count); \
3464 __count /= XDRWORD; \
3465 while (__count-- > 0) \
3466 xb_copy_32((E), (XBSRC), (XBDST), __val); \
3469 xb_init_buffer(&xb
, nmp
->nm_args
, 2*XDRWORD
);
3470 xb_get_32(error
, &xb
, val
); /* version */
3471 xb_get_32(error
, &xb
, argslength
); /* args length */
3472 xb_init_buffer(&xb
, nmp
->nm_args
, argslength
);
3474 xb_init_buffer(&xbnew
, NULL
, 0);
3475 xb_copy_32(error
, &xb
, &xbnew
, val
); /* version */
3476 argslength_offset
= xb_offset(&xbnew
);
3477 xb_copy_32(error
, &xb
, &xbnew
, val
); /* args length */
3478 xb_copy_32(error
, &xb
, &xbnew
, val
); /* XDR args version */
3479 count
= NFS_MATTR_BITMAP_LEN
;
3480 xb_get_bitmap(error
, &xb
, mattrs
, count
); /* mount attribute bitmap */
3482 for (i
= 0; i
< NFS_MATTR_BITMAP_LEN
; i
++)
3483 newmattrs
[i
] = mattrs
[i
];
3485 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FS_LOCATIONS
);
3487 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FH
);
3488 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FLAGS
);
3489 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_MNTFLAGS
);
3490 NFS_BITMAP_CLR(newmattrs
, NFS_MATTR_MNTFROM
);
3491 xb_add_bitmap(error
, &xbnew
, newmattrs
, NFS_MATTR_BITMAP_LEN
);
3492 attrslength_offset
= xb_offset(&xbnew
);
3493 xb_copy_32(error
, &xb
, &xbnew
, val
); /* attrs length */
3494 NFS_BITMAP_ZERO(newmflags_mask
, NFS_MFLAG_BITMAP_LEN
);
3495 NFS_BITMAP_ZERO(newmflags
, NFS_MFLAG_BITMAP_LEN
);
3496 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FLAGS
)) {
3497 count
= NFS_MFLAG_BITMAP_LEN
;
3498 xb_get_bitmap(error
, &xb
, newmflags_mask
, count
); /* mount flag mask bitmap */
3499 count
= NFS_MFLAG_BITMAP_LEN
;
3500 xb_get_bitmap(error
, &xb
, newmflags
, count
); /* mount flag bitmap */
3502 NFS_BITMAP_SET(newmflags_mask
, NFS_MFLAG_EPHEMERAL
);
3503 NFS_BITMAP_SET(newmflags
, NFS_MFLAG_EPHEMERAL
);
3504 xb_add_bitmap(error
, &xbnew
, newmflags_mask
, NFS_MFLAG_BITMAP_LEN
);
3505 xb_add_bitmap(error
, &xbnew
, newmflags
, NFS_MFLAG_BITMAP_LEN
);
3506 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
))
3507 xb_copy_32(error
, &xb
, &xbnew
, val
);
3508 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
))
3509 xb_copy_32(error
, &xb
, &xbnew
, val
);
3510 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
)) {
3511 xb_copy_32(error
, &xb
, &xbnew
, val
);
3512 xb_copy_32(error
, &xb
, &xbnew
, val
);
3514 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
))
3515 xb_copy_32(error
, &xb
, &xbnew
, val
);
3516 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
))
3517 xb_copy_32(error
, &xb
, &xbnew
, val
);
3518 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
))
3519 xb_copy_32(error
, &xb
, &xbnew
, val
);
3520 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
))
3521 xb_copy_32(error
, &xb
, &xbnew
, val
);
3522 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
3523 xb_copy_32(error
, &xb
, &xbnew
, val
);
3524 xb_copy_32(error
, &xb
, &xbnew
, val
);
3526 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
3527 xb_copy_32(error
, &xb
, &xbnew
, val
);
3528 xb_copy_32(error
, &xb
, &xbnew
, val
);
3530 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
3531 xb_copy_32(error
, &xb
, &xbnew
, val
);
3532 xb_copy_32(error
, &xb
, &xbnew
, val
);
3534 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
3535 xb_copy_32(error
, &xb
, &xbnew
, val
);
3536 xb_copy_32(error
, &xb
, &xbnew
, val
);
3538 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
))
3539 xb_copy_32(error
, &xb
, &xbnew
, val
);
3540 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
3541 xb_copy_32(error
, &xb
, &xbnew
, count
);
3542 while (!error
&& (count
-- > 0))
3543 xb_copy_32(error
, &xb
, &xbnew
, val
);
3545 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_KERB_ETYPE
)) {
3546 xb_copy_32(error
, &xb
, &xbnew
, count
);
3547 xb_add_32(error
, &xbnew
, -1);
3548 while (!error
&& (count
-- > 0))
3549 xb_copy_32(error
, &xb
, &xbnew
, val
);
3551 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
))
3552 xb_copy_32(error
, &xb
, &xbnew
, val
);
3553 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
))
3554 xb_copy_opaque(error
, &xb
, &xbnew
);
3555 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
))
3556 xb_copy_32(error
, &xb
, &xbnew
, val
);
3557 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MOUNT_PORT
))
3558 xb_copy_32(error
, &xb
, &xbnew
, val
);
3559 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
3560 xb_copy_32(error
, &xb
, &xbnew
, val
);
3561 xb_copy_32(error
, &xb
, &xbnew
, val
);
3563 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
))
3564 xb_copy_32(error
, &xb
, &xbnew
, val
);
3565 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
3566 xb_copy_32(error
, &xb
, &xbnew
, val
);
3567 xb_copy_32(error
, &xb
, &xbnew
, val
);
3569 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
)) {
3570 xb_get_32(error
, &xb
, count
);
3571 xb_skip(error
, &xb
, count
);
3574 /* set the initial file handle to the directory's file handle */
3575 xb_add_fh(error
, &xbnew
, np
->n_fhp
, np
->n_fhsize
);
3577 /* copy/extend/skip fs locations */
3578 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
3579 numlocs
= numserv
= numaddr
= numcomp
= 0;
3580 if (referral
) /* don't copy the fs locations for a referral */
3582 xb_copy_32(error
, &xb
, &xbnew
, numlocs
); /* location count */
3583 for (loc
= 0; !error
&& (loc
< numlocs
); loc
++) {
3584 xb_copy_32(error
, &xb
, &xbnew
, numserv
); /* server count */
3585 for (serv
= 0; !error
&& (serv
< numserv
); serv
++) {
3586 xb_copy_opaque(error
, &xb
, &xbnew
); /* server name */
3587 xb_copy_32(error
, &xb
, &xbnew
, numaddr
); /* address count */
3588 for (addr
= 0; !error
&& (addr
< numaddr
); addr
++)
3589 xb_copy_opaque(error
, &xb
, &xbnew
); /* address */
3590 xb_copy_opaque(error
, &xb
, &xbnew
); /* server info */
3593 xb_get_32(error
, &xb
, numcomp
); /* component count */
3595 xb_add_32(error
, &xbnew
, numcomp
+relpathcomps
); /* new component count */
3596 for (comp
= 0; !error
&& (comp
< numcomp
); comp
++)
3597 xb_copy_opaque(error
, &xb
, &xbnew
); /* component */
3598 /* add additional components */
3599 for (comp
= 0; !skipcopy
&& !error
&& (comp
< relpathcomps
); comp
++) {
3601 while (*p
&& (*p
== '/'))
3603 while (*p
&& !error
) {
3605 while (*p
&& (*p
!= '/'))
3607 xb_add_string(error
, &xbnew
, cp
, (p
- cp
)); /* component */
3608 while (*p
&& (*p
== '/'))
3612 xb_copy_opaque(error
, &xb
, &xbnew
); /* fs location info */
3618 /* add referral's fs locations */
3619 xb_add_32(error
, &xbnew
, nfsls
.nl_numlocs
); /* FS_LOCATIONS */
3620 for (loc
= 0; !error
&& (loc
< nfsls
.nl_numlocs
); loc
++) {
3621 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servcount
);
3622 for (serv
= 0; !error
&& (serv
< nfsls
.nl_locations
[loc
]->nl_servcount
); serv
++) {
3623 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
,
3624 strlen(nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
));
3625 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
);
3626 for (addr
= 0; !error
&& (addr
< nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
); addr
++)
3627 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
],
3628 strlen(nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
]));
3629 xb_add_32(error
, &xbnew
, 0); /* empty server info */
3631 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_path
.np_compcount
);
3632 for (comp
= 0; !error
&& (comp
< nfsls
.nl_locations
[loc
]->nl_path
.np_compcount
); comp
++)
3633 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_path
.np_components
[comp
],
3634 strlen(nfsls
.nl_locations
[loc
]->nl_path
.np_components
[comp
]));
3635 xb_add_32(error
, &xbnew
, 0); /* empty fs location info */
3638 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
))
3639 xb_get_32(error
, &xb
, mntflags
);
3641 * We add the following mount flags to the ones for the mounted-on mount:
3642 * MNT_DONTBROWSE - to keep the mount from showing up as a separate volume
3643 * MNT_AUTOMOUNTED - to keep DiskArb from retriggering the mount after
3644 * an unmount (looking for /.autodiskmounted)
3646 mntflags
|= (MNT_AUTOMOUNTED
| MNT_DONTBROWSE
);
3647 xb_add_32(error
, &xbnew
, mntflags
);
3648 if (!referral
&& NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
)) {
3649 /* copy mntfrom string and add relpath */
3650 rlen
= strlen(relpath
);
3651 xb_get_32(error
, &xb
, mlen
);
3653 mlen2
= mlen
+ ((relpath
[0] != '/') ? 1 : 0) + rlen
;
3654 xb_add_32(error
, &xbnew
, mlen2
);
3655 count
= mlen
/XDRWORD
;
3656 /* copy the original string */
3658 xb_copy_32(error
, &xb
, &xbnew
, val
);
3659 if (!error
&& (mlen
% XDRWORD
)) {
3660 error
= xb_get_bytes(&xb
, buf
, mlen%XDRWORD
, 0);
3662 error
= xb_add_bytes(&xbnew
, buf
, mlen%XDRWORD
, 1);
3664 /* insert a '/' if the relative path doesn't start with one */
3665 if (!error
&& (relpath
[0] != '/')) {
3667 error
= xb_add_bytes(&xbnew
, buf
, 1, 1);
3669 /* add the additional relative path */
3671 error
= xb_add_bytes(&xbnew
, relpath
, rlen
, 1);
3672 /* make sure the resulting string has the right number of pad bytes */
3673 if (!error
&& (mlen2
!= nfsm_rndup(mlen2
))) {
3674 bzero(buf
, sizeof(buf
));
3675 count
= nfsm_rndup(mlen2
) - mlen2
;
3676 error
= xb_add_bytes(&xbnew
, buf
, count
, 1);
3679 xb_build_done(error
, &xbnew
);
3681 /* update opaque counts */
3682 end_offset
= xb_offset(&xbnew
);
3684 error
= xb_seek(&xbnew
, argslength_offset
);
3685 argslength
= end_offset
- argslength_offset
+ XDRWORD
/*version*/;
3686 xb_add_32(error
, &xbnew
, argslength
);
3689 error
= xb_seek(&xbnew
, attrslength_offset
);
3690 xb_add_32(error
, &xbnew
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
3695 * For kernel_mount() call, use the existing mount flags (instead of the
3696 * original flags) because flags like MNT_NOSUID and MNT_NODEV may have
3697 * been silently enforced.
3699 mntflags
= vnode_vfsvisflags(vp
);
3700 mntflags
|= (MNT_AUTOMOUNTED
| MNT_DONTBROWSE
);
3703 error
= kernel_mount(fstype
, dvp
, vp
, path
, xb_buffer_base(&xbnew
), argslength
,
3704 mntflags
, KERNEL_MOUNT_PERMIT_UNMOUNT
| KERNEL_MOUNT_NOAUTH
, ctx
);
3708 printf("nfs: mirror mount of %s on %s failed (%d)\n",
3709 mntfromname
, path
, error
);
3713 nfs_fs_locations_cleanup(&nfsls
);
3715 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
3717 FREE_ZONE(mntfromname
, MAXPATHLEN
, M_NAMEI
);
3719 nfs_ephemeral_mount_harvester_start();
3724 * trigger vnode functions
3728 nfs_mirror_mount_trigger_resolve(
3730 const struct componentname
*cnp
,
3731 enum path_operation pop
,
3733 __unused
void *data
,
3736 nfsnode_t np
= VTONFS(vp
);
3737 vnode_t pvp
= NULLVP
;
3739 resolver_result_t result
;
3742 * We have a trigger node that doesn't have anything mounted on it yet.
3743 * We'll do the mount if either:
3744 * (a) this isn't the last component of the path OR
3745 * (b) this is an op that looks like it should trigger the mount.
3747 if (cnp
->cn_flags
& ISLASTCN
) {
3765 /* don't perform the mount for these operations */
3766 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_NOCHANGE
, 0);
3767 #ifdef NFS_TRIGGER_DEBUG
3768 NP(np
, "nfs trigger RESOLVE: no change, last %d nameiop %d, seq %d",
3769 (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
3780 case OP_EXCHANGEDATA
:
3784 case OP_REMOVEXATTR
:
3786 /* go ahead and do the mount */
3791 if (vnode_mountedhere(vp
) != NULL
) {
3793 * Um... there's already something mounted.
3794 * Been there. Done that. Let's just say it succeeded.
3800 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
3801 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
3802 #ifdef NFS_TRIGGER_DEBUG
3803 NP(np
, "nfs trigger RESOLVE: busy error %d, last %d nameiop %d, seq %d",
3804 error
, (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
3809 pvp
= vnode_getparent(vp
);
3813 error
= nfs_mirror_mount_domount(pvp
, vp
, ctx
);
3817 result
= vfs_resolver_result(np
->n_trigseq
, error
? RESOLVER_ERROR
: RESOLVER_RESOLVED
, error
);
3818 #ifdef NFS_TRIGGER_DEBUG
3819 NP(np
, "nfs trigger RESOLVE: %s %d, last %d nameiop %d, seq %d",
3820 error
? "error" : "resolved", error
,
3821 (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
3826 nfs_node_clear_busy(np
);
3831 nfs_mirror_mount_trigger_unresolve(
3834 __unused
void *data
,
3837 nfsnode_t np
= VTONFS(vp
);
3840 resolver_result_t result
;
3842 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
3843 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
3844 #ifdef NFS_TRIGGER_DEBUG
3845 NP(np
, "nfs trigger UNRESOLVE: busy error %d, seq %d", error
, np
->n_trigseq
);
3850 mp
= vnode_mountedhere(vp
);
3854 error
= vfs_unmountbyfsid(&(vfs_statfs(mp
)->f_fsid
), flags
, ctx
);
3857 result
= vfs_resolver_result(np
->n_trigseq
, error
? RESOLVER_ERROR
: RESOLVER_UNRESOLVED
, error
);
3858 #ifdef NFS_TRIGGER_DEBUG
3859 NP(np
, "nfs trigger UNRESOLVE: %s %d, seq %d",
3860 error
? "error" : "unresolved", error
, np
->n_trigseq
);
3862 nfs_node_clear_busy(np
);
3867 nfs_mirror_mount_trigger_rearm(
3870 __unused
void *data
,
3873 nfsnode_t np
= VTONFS(vp
);
3875 resolver_result_t result
;
3877 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
3878 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
3879 #ifdef NFS_TRIGGER_DEBUG
3880 NP(np
, "nfs trigger REARM: busy error %d, seq %d", error
, np
->n_trigseq
);
3886 result
= vfs_resolver_result(np
->n_trigseq
,
3887 vnode_mountedhere(vp
) ? RESOLVER_RESOLVED
: RESOLVER_UNRESOLVED
, 0);
3888 #ifdef NFS_TRIGGER_DEBUG
3889 NP(np
, "nfs trigger REARM: %s, seq %d",
3890 vnode_mountedhere(vp
) ? "resolved" : "unresolved", np
->n_trigseq
);
3892 nfs_node_clear_busy(np
);
3897 * Periodically attempt to unmount ephemeral (mirror) mounts in an attempt to limit
3898 * the number of unused mounts.
3901 #define NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL 120 /* how often the harvester runs */
3902 struct nfs_ephemeral_mount_harvester_info
{
3903 fsid_t fsid
; /* FSID that we need to try to unmount */
3904 uint32_t mountcount
; /* count of ephemeral mounts seen in scan */
3906 /* various globals for the harvester */
3907 static thread_call_t nfs_ephemeral_mount_harvester_timer
= NULL
;
3908 static int nfs_ephemeral_mount_harvester_on
= 0;
3910 kern_return_t
thread_terminate(thread_t
);
3913 nfs_ephemeral_mount_harvester_callback(mount_t mp
, void *arg
)
3915 struct nfs_ephemeral_mount_harvester_info
*hinfo
= arg
;
3916 struct nfsmount
*nmp
;
3919 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs"))
3920 return (VFS_RETURNED
);
3922 if (!nmp
|| !NMFLAG(nmp
, EPHEMERAL
))
3923 return (VFS_RETURNED
);
3924 hinfo
->mountcount
++;
3926 /* avoid unmounting mounts that have been triggered within the last harvest interval */
3928 if ((nmp
->nm_mounttime
>> 32) > ((uint32_t)now
.tv_sec
- NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
))
3929 return (VFS_RETURNED
);
3931 if (hinfo
->fsid
.val
[0] || hinfo
->fsid
.val
[1]) {
3932 /* attempt to unmount previously-found ephemeral mount */
3933 vfs_unmountbyfsid(&hinfo
->fsid
, 0, vfs_context_kernel());
3934 hinfo
->fsid
.val
[0] = hinfo
->fsid
.val
[1] = 0;
3938 * We can't call unmount here since we hold a mount iter ref
3939 * on mp so save its fsid for the next call iteration to unmount.
3941 hinfo
->fsid
.val
[0] = mp
->mnt_vfsstat
.f_fsid
.val
[0];
3942 hinfo
->fsid
.val
[1] = mp
->mnt_vfsstat
.f_fsid
.val
[1];
3944 return (VFS_RETURNED
);
3948 * Spawn a thread to do the ephemeral mount harvesting.
3951 nfs_ephemeral_mount_harvester_timer_func(void)
3955 if (kernel_thread_start(nfs_ephemeral_mount_harvester
, NULL
, &thd
) == KERN_SUCCESS
)
3956 thread_deallocate(thd
);
3960 * Iterate all mounts looking for NFS ephemeral mounts to try to unmount.
3963 nfs_ephemeral_mount_harvester(__unused
void *arg
, __unused wait_result_t wr
)
3965 struct nfs_ephemeral_mount_harvester_info hinfo
;
3968 hinfo
.mountcount
= 0;
3969 hinfo
.fsid
.val
[0] = hinfo
.fsid
.val
[1] = 0;
3970 vfs_iterate(VFS_ITERATE_TAIL_FIRST
, nfs_ephemeral_mount_harvester_callback
, &hinfo
);
3971 if (hinfo
.fsid
.val
[0] || hinfo
.fsid
.val
[1]) {
3972 /* attempt to unmount last found ephemeral mount */
3973 vfs_unmountbyfsid(&hinfo
.fsid
, 0, vfs_context_kernel());
3976 lck_mtx_lock(nfs_global_mutex
);
3977 if (!hinfo
.mountcount
) {
3978 /* no more ephemeral mounts - don't need timer */
3979 nfs_ephemeral_mount_harvester_on
= 0;
3981 /* re-arm the timer */
3982 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
, NSEC_PER_SEC
, &deadline
);
3983 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer
, deadline
);
3984 nfs_ephemeral_mount_harvester_on
= 1;
3986 lck_mtx_unlock(nfs_global_mutex
);
3989 thread_terminate(current_thread());
3993 * Make sure the NFS ephemeral mount harvester timer is running.
3996 nfs_ephemeral_mount_harvester_start(void)
4000 lck_mtx_lock(nfs_global_mutex
);
4001 if (nfs_ephemeral_mount_harvester_on
) {
4002 lck_mtx_unlock(nfs_global_mutex
);
4005 if (nfs_ephemeral_mount_harvester_timer
== NULL
)
4006 nfs_ephemeral_mount_harvester_timer
= thread_call_allocate((thread_call_func_t
)nfs_ephemeral_mount_harvester_timer_func
, NULL
);
4007 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
, NSEC_PER_SEC
, &deadline
);
4008 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer
, deadline
);
4009 nfs_ephemeral_mount_harvester_on
= 1;
4010 lck_mtx_unlock(nfs_global_mutex
);
4016 * Send a MOUNT protocol MOUNT request to the server to get the initial file handle (and security).
4019 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
)
4021 int error
= 0, slen
, mntproto
;
4022 thread_t thd
= vfs_context_thread(ctx
);
4023 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4025 struct nfsm_chain nmreq
, nmrep
;
4027 uint32_t mntvers
, mntport
, val
;
4028 struct sockaddr_storage ss
;
4029 struct sockaddr
*saddr
= (struct sockaddr
*)&ss
;
4031 nfsm_chain_null(&nmreq
);
4032 nfsm_chain_null(&nmrep
);
4034 mntvers
= (nfsvers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4035 mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
4038 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
4039 if (saddr
->sa_family
== AF_INET
) {
4040 if (nmp
->nm_mountport
)
4041 ((struct sockaddr_in
*)saddr
)->sin_port
= htons(nmp
->nm_mountport
);
4042 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4044 if (nmp
->nm_mountport
)
4045 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(nmp
->nm_mountport
);
4046 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4050 error
= nfs_portmap_lookup(nmp
, ctx
, saddr
, NULL
, RPCPROG_MNT
, mntvers
, mntproto
, timeo
);
4052 if (saddr
->sa_family
== AF_INET
)
4053 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4055 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4057 /* if not found and TCP, then retry with UDP */
4058 if (mntproto
== IPPROTO_UDP
) {
4059 error
= EPROGUNAVAIL
;
4062 mntproto
= IPPROTO_UDP
;
4063 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
4066 nfsmout_if(error
|| !mntport
);
4068 /* MOUNT protocol MOUNT request */
4069 slen
= strlen(path
);
4070 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_UNSIGNED
+ nfsm_rndup(slen
));
4071 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
4072 nfsm_chain_build_done(error
, &nmreq
);
4074 error
= nfsm_rpchead2(nmp
, (mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4075 RPCPROG_MNT
, mntvers
, RPCMNT_MOUNT
,
4076 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
4078 nmreq
.nmc_mhead
= NULL
;
4079 error
= nfs_aux_request(nmp
, thd
, saddr
, NULL
,
4080 ((mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
),
4081 mreq
, R_XID32(xid
), 1, timeo
, &nmrep
);
4083 nfsm_chain_get_32(error
, &nmrep
, val
);
4086 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fh
);
4087 if (!error
&& (nfsvers
> NFS_VER2
)) {
4088 sec
->count
= NX_MAX_SEC_FLAVORS
;
4089 error
= nfsm_chain_get_secinfo(&nmrep
, &sec
->flavors
[0], &sec
->count
);
4092 nfsm_chain_cleanup(&nmreq
);
4093 nfsm_chain_cleanup(&nmrep
);
4099 * Send a MOUNT protocol UNMOUNT request to tell the server we've unmounted it.
4102 nfs3_umount_rpc(struct nfsmount
*nmp
, vfs_context_t ctx
, int timeo
)
4104 int error
= 0, slen
, mntproto
;
4105 thread_t thd
= vfs_context_thread(ctx
);
4106 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4109 struct nfsm_chain nmreq
, nmrep
;
4111 uint32_t mntvers
, mntport
;
4112 struct sockaddr_storage ss
;
4113 struct sockaddr
*saddr
= (struct sockaddr
*)&ss
;
4118 nfsm_chain_null(&nmreq
);
4119 nfsm_chain_null(&nmrep
);
4121 mntvers
= (nmp
->nm_vers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4122 mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (nmp
->nm_sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
4123 mntport
= nmp
->nm_mountport
;
4125 bcopy(nmp
->nm_saddr
, saddr
, min(sizeof(ss
), nmp
->nm_saddr
->sa_len
));
4126 if (saddr
->sa_family
== AF_INET
)
4127 ((struct sockaddr_in
*)saddr
)->sin_port
= htons(mntport
);
4129 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(mntport
);
4132 error
= nfs_portmap_lookup(nmp
, ctx
, saddr
, NULL
, RPCPROG_MNT
, mntvers
, mntproto
, timeo
);
4134 if (saddr
->sa_family
== AF_INET
)
4135 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4137 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4138 /* if not found and mntvers > VER1, then retry with VER1 */
4140 if (mntvers
> RPCMNT_VER1
) {
4141 mntvers
= RPCMNT_VER1
;
4142 } else if (mntproto
== IPPROTO_TCP
) {
4143 mntproto
= IPPROTO_UDP
;
4144 mntvers
= (nmp
->nm_vers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4148 bcopy(nmp
->nm_saddr
, saddr
, min(sizeof(ss
), nmp
->nm_saddr
->sa_len
));
4151 nfsmout_if(!mntport
);
4153 /* MOUNT protocol UNMOUNT request */
4154 path
= &vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
[0];
4155 while (*path
&& (*path
!= '/'))
4157 slen
= strlen(path
);
4158 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_UNSIGNED
+ nfsm_rndup(slen
));
4159 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
4160 nfsm_chain_build_done(error
, &nmreq
);
4162 error
= nfsm_rpchead2(nmp
, (mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4163 RPCPROG_MNT
, RPCMNT_VER1
, RPCMNT_UMOUNT
,
4164 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
4166 nmreq
.nmc_mhead
= NULL
;
4167 error
= nfs_aux_request(nmp
, thd
, saddr
, NULL
,
4168 ((mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
),
4169 mreq
, R_XID32(xid
), 1, timeo
, &nmrep
);
4171 nfsm_chain_cleanup(&nmreq
);
4172 nfsm_chain_cleanup(&nmrep
);
4176 * unmount system call
4182 __unused vfs_context_t ctx
)
4184 struct nfsmount
*nmp
;
4186 int error
, flags
= 0;
4187 struct timespec ts
= { 1, 0 };
4190 lck_mtx_lock(&nmp
->nm_lock
);
4192 * Set the flag indicating that an unmount attempt is in progress.
4194 nmp
->nm_state
|= NFSSTA_UNMOUNTING
;
4196 * During a force unmount we want to...
4197 * Mark that we are doing a force unmount.
4198 * Make the mountpoint soft.
4200 if (mntflags
& MNT_FORCE
) {
4201 flags
|= FORCECLOSE
;
4202 nmp
->nm_state
|= NFSSTA_FORCE
;
4203 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_SOFT
);
4206 * Wait for any in-progress monitored node scan to complete.
4208 while (nmp
->nm_state
& NFSSTA_MONITOR_SCAN
)
4209 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, PZERO
-1, "nfswaitmonscan", &ts
);
4211 * Goes something like this..
4212 * - Call vflush() to clear out vnodes for this file system,
4213 * except for the swap files. Deal with them in 2nd pass.
4214 * - Decrement reference on the vnode representing remote root.
4215 * - Clean up the NFS mount structure.
4217 vp
= NFSTOV(nmp
->nm_dnp
);
4218 lck_mtx_unlock(&nmp
->nm_lock
);
4221 * vflush will check for busy vnodes on mountpoint.
4222 * Will do the right thing for MNT_FORCE. That is, we should
4223 * not get EBUSY back.
4225 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
4226 if (mntflags
& MNT_FORCE
) {
4227 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
4229 if (vnode_isinuse(vp
, 1))
4232 error
= vflush(mp
, vp
, flags
);
4235 lck_mtx_lock(&nmp
->nm_lock
);
4236 nmp
->nm_state
&= ~NFSSTA_UNMOUNTING
;
4237 lck_mtx_unlock(&nmp
->nm_lock
);
4241 lck_mtx_lock(&nmp
->nm_lock
);
4243 lck_mtx_unlock(&nmp
->nm_lock
);
4246 * Release the root vnode reference held by mountnfs()
4248 error
= vnode_get(vp
);
4253 vflush(mp
, NULLVP
, FORCECLOSE
);
4255 /* Wait for all other references to be released and free the mount */
4256 nfs_mount_drain_and_cleanup(nmp
);
4262 * cleanup/destroy NFS fs locations structure
4265 nfs_fs_locations_cleanup(struct nfs_fs_locations
*nfslsp
)
4267 struct nfs_fs_location
*fsl
;
4268 struct nfs_fs_server
*fss
;
4269 struct nfs_fs_path
*fsp
;
4270 uint32_t loc
, serv
, addr
, comp
;
4272 /* free up fs locations */
4273 if (!nfslsp
->nl_numlocs
|| !nfslsp
->nl_locations
)
4276 for (loc
= 0; loc
< nfslsp
->nl_numlocs
; loc
++) {
4277 fsl
= nfslsp
->nl_locations
[loc
];
4280 if ((fsl
->nl_servcount
> 0) && fsl
->nl_servers
) {
4281 for (serv
= 0; serv
< fsl
->nl_servcount
; serv
++) {
4282 fss
= fsl
->nl_servers
[serv
];
4285 if ((fss
->ns_addrcount
> 0) && fss
->ns_addresses
) {
4286 for (addr
= 0; addr
< fss
->ns_addrcount
; addr
++)
4287 FREE(fss
->ns_addresses
[addr
], M_TEMP
);
4288 FREE(fss
->ns_addresses
, M_TEMP
);
4290 FREE(fss
->ns_name
, M_TEMP
);
4293 FREE(fsl
->nl_servers
, M_TEMP
);
4295 fsp
= &fsl
->nl_path
;
4296 if (fsp
->np_compcount
&& fsp
->np_components
) {
4297 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++)
4298 if (fsp
->np_components
[comp
])
4299 FREE(fsp
->np_components
[comp
], M_TEMP
);
4300 FREE(fsp
->np_components
, M_TEMP
);
4304 FREE(nfslsp
->nl_locations
, M_TEMP
);
4305 nfslsp
->nl_numlocs
= 0;
4306 nfslsp
->nl_locations
= NULL
;
4310 nfs_mount_rele(struct nfsmount
*nmp
)
4314 lck_mtx_lock(&nmp
->nm_lock
);
4315 if (nmp
->nm_ref
< 1)
4316 panic("nfs zombie mount underflow\n");
4318 if (nmp
->nm_ref
== 0)
4319 wup
= nmp
->nm_state
& NFSSTA_MOUNT_DRAIN
;
4320 lck_mtx_unlock(&nmp
->nm_lock
);
4322 wakeup(&nmp
->nm_ref
);
4326 nfs_mount_drain_and_cleanup(struct nfsmount
*nmp
)
4328 lck_mtx_lock(&nmp
->nm_lock
);
4329 nmp
->nm_state
|= NFSSTA_MOUNT_DRAIN
;
4330 while (nmp
->nm_ref
> 0) {
4331 msleep(&nmp
->nm_ref
, &nmp
->nm_lock
, PZERO
-1, "nfs_mount_drain", NULL
);
4333 assert(nmp
->nm_ref
== 0);
4334 lck_mtx_unlock(&nmp
->nm_lock
);
4335 nfs_mount_cleanup(nmp
);
4342 nfs_mount_zombie(struct nfsmount
*nmp
, int nm_state_flags
)
4344 struct nfsreq
*req
, *treq
;
4345 struct nfs_reqqhead iodq
, resendq
;
4346 struct timespec ts
= { 1, 0 };
4347 struct nfs_open_owner
*noop
, *nextnoop
;
4351 lck_mtx_lock(&nmp
->nm_lock
);
4352 nmp
->nm_state
|= nm_state_flags
;
4354 lck_mtx_unlock(&nmp
->nm_lock
);
4356 /* stop callbacks */
4357 if ((nmp
->nm_vers
>= NFS_VER4
) && !NMFLAG(nmp
, NOCALLBACK
) && nmp
->nm_cbid
)
4358 nfs4_mount_callback_shutdown(nmp
);
4360 /* Destroy any RPCSEC_GSS contexts */
4361 nfs_gss_clnt_ctx_unmount(nmp
);
4363 /* mark the socket for termination */
4364 lck_mtx_lock(&nmp
->nm_lock
);
4365 nmp
->nm_sockflags
|= NMSOCK_UNMOUNT
;
4367 /* Have the socket thread send the unmount RPC, if requested/appropriate. */
4368 if ((nmp
->nm_vers
< NFS_VER4
) && (nmp
->nm_state
& NFSSTA_MOUNTED
) &&
4369 !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) && NMFLAG(nmp
, CALLUMNT
))
4370 nfs_mount_sock_thread_wake(nmp
);
4372 /* wait for the socket thread to terminate */
4373 while (nmp
->nm_sockthd
&& current_thread() != nmp
->nm_sockthd
) {
4374 wakeup(&nmp
->nm_sockthd
);
4375 msleep(&nmp
->nm_sockthd
, &nmp
->nm_lock
, PZERO
-1, "nfswaitsockthd", &ts
);
4377 lck_mtx_unlock(&nmp
->nm_lock
);
4379 /* tear down the socket */
4380 nfs_disconnect(nmp
);
4382 lck_mtx_lock(&nmp
->nm_lock
);
4384 if ((nmp
->nm_vers
>= NFS_VER4
) && !NMFLAG(nmp
, NOCALLBACK
) && nmp
->nm_cbid
) {
4385 /* clear out any pending delegation return requests */
4386 while ((np
= TAILQ_FIRST(&nmp
->nm_dreturnq
))) {
4387 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
4388 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
4392 /* cancel any renew timer */
4393 if ((nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_renew_timer
) {
4394 thread_call_cancel(nmp
->nm_renew_timer
);
4395 thread_call_free(nmp
->nm_renew_timer
);
4398 lck_mtx_unlock(&nmp
->nm_lock
);
4400 if (nmp
->nm_state
& NFSSTA_MOUNTED
)
4401 switch (nmp
->nm_lockmode
) {
4402 case NFS_LOCK_MODE_DISABLED
:
4403 case NFS_LOCK_MODE_LOCAL
:
4405 case NFS_LOCK_MODE_ENABLED
:
4407 if (nmp
->nm_vers
<= NFS_VER3
) {
4408 nfs_lockd_mount_unregister(nmp
);
4409 nmp
->nm_lockmode
= NFS_LOCK_MODE_DISABLED
;
4414 if ((nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_longid
) {
4415 /* remove/deallocate the client ID data */
4416 lck_mtx_lock(nfs_global_mutex
);
4417 TAILQ_REMOVE(&nfsclientids
, nmp
->nm_longid
, nci_link
);
4418 if (nmp
->nm_longid
->nci_id
)
4419 FREE(nmp
->nm_longid
->nci_id
, M_TEMP
);
4420 FREE(nmp
->nm_longid
, M_TEMP
);
4421 lck_mtx_unlock(nfs_global_mutex
);
4425 * Be sure all requests for this mount are completed
4426 * and removed from the resend queue.
4428 TAILQ_INIT(&resendq
);
4429 lck_mtx_lock(nfs_request_mutex
);
4430 TAILQ_FOREACH(req
, &nfs_reqq
, r_chain
) {
4431 if (req
->r_nmp
== nmp
) {
4432 lck_mtx_lock(&req
->r_mtx
);
4433 if (!req
->r_error
&& req
->r_nmrep
.nmc_mhead
== NULL
)
4435 if (req
->r_flags
& R_RESENDQ
) {
4436 lck_mtx_lock(&nmp
->nm_lock
);
4437 req
->r_flags
&= ~R_RESENDQ
;
4438 if (req
->r_rchain
.tqe_next
!= NFSREQNOLIST
) {
4439 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
4441 * Queue up the request so that we can unreference them
4442 * with out holding nfs_request_mutex
4444 TAILQ_INSERT_TAIL(&resendq
, req
, r_rchain
);
4446 lck_mtx_unlock(&nmp
->nm_lock
);
4449 lck_mtx_unlock(&req
->r_mtx
);
4452 lck_mtx_unlock(nfs_request_mutex
);
4454 /* Since we've drop the request mutex we can now safely unreference the request */
4455 TAILQ_FOREACH_SAFE(req
, &resendq
, r_rchain
, treq
) {
4456 TAILQ_REMOVE(&resendq
, req
, r_rchain
);
4457 nfs_request_rele(req
);
4461 * Now handle and outstanding async requests. We need to walk the
4462 * request queue again this time with the nfsiod_mutex held. No
4463 * other iods can grab our requests until we've put them on our own
4464 * local iod queue for processing.
4467 lck_mtx_lock(nfs_request_mutex
);
4468 lck_mtx_lock(nfsiod_mutex
);
4469 TAILQ_FOREACH(req
, &nfs_reqq
, r_chain
) {
4470 if (req
->r_nmp
== nmp
) {
4471 lck_mtx_lock(&req
->r_mtx
);
4472 if (req
->r_callback
.rcb_func
4473 && !(req
->r_flags
& R_WAITSENT
) && !(req
->r_flags
& R_IOD
)) {
4475 * Since R_IOD is not set then we need to handle it. If
4476 * we're not on a list add it to our iod queue. Otherwise
4477 * we must already be on nm_iodq which is added to our
4478 * local queue below.
4479 * %%% We should really keep a back pointer to our iod queue
4482 req
->r_flags
|= R_IOD
;
4483 if (req
->r_achain
.tqe_next
== NFSREQNOLIST
) {
4484 TAILQ_INSERT_TAIL(&iodq
, req
, r_achain
);
4487 lck_mtx_unlock(&req
->r_mtx
);
4491 /* finish any async I/O RPCs queued up */
4492 if (nmp
->nm_iodlink
.tqe_next
!= NFSNOLIST
)
4493 TAILQ_REMOVE(&nfsiodmounts
, nmp
, nm_iodlink
);
4494 TAILQ_CONCAT(&iodq
, &nmp
->nm_iodq
, r_achain
);
4495 lck_mtx_unlock(nfsiod_mutex
);
4496 lck_mtx_unlock(nfs_request_mutex
);
4498 TAILQ_FOREACH_SAFE(req
, &iodq
, r_achain
, treq
) {
4499 TAILQ_REMOVE(&iodq
, req
, r_achain
);
4500 req
->r_achain
.tqe_next
= NFSREQNOLIST
;
4501 lck_mtx_lock(&req
->r_mtx
);
4502 docallback
= !(req
->r_flags
& R_WAITSENT
);
4503 lck_mtx_unlock(&req
->r_mtx
);
4505 req
->r_callback
.rcb_func(req
);
4508 /* clean up common state */
4509 lck_mtx_lock(&nmp
->nm_lock
);
4510 while ((np
= LIST_FIRST(&nmp
->nm_monlist
))) {
4511 LIST_REMOVE(np
, n_monlink
);
4512 np
->n_monlink
.le_next
= NFSNOLIST
;
4514 TAILQ_FOREACH_SAFE(noop
, &nmp
->nm_open_owners
, noo_link
, nextnoop
) {
4515 TAILQ_REMOVE(&nmp
->nm_open_owners
, noop
, noo_link
);
4516 noop
->noo_flags
&= ~NFS_OPEN_OWNER_LINK
;
4517 if (noop
->noo_refcnt
)
4519 nfs_open_owner_destroy(noop
);
4521 lck_mtx_unlock(&nmp
->nm_lock
);
4523 /* clean up NFSv4 state */
4524 if (nmp
->nm_vers
>= NFS_VER4
) {
4525 lck_mtx_lock(&nmp
->nm_lock
);
4526 while ((np
= TAILQ_FIRST(&nmp
->nm_delegations
))) {
4527 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
4528 np
->n_dlink
.tqe_next
= NFSNOLIST
;
4530 lck_mtx_unlock(&nmp
->nm_lock
);
4533 nfs_mount_rele(nmp
);
4537 * cleanup/destroy an nfsmount
4540 nfs_mount_cleanup(struct nfsmount
*nmp
)
4545 nfs_mount_zombie(nmp
, 0);
4547 NFS_VFS_DBG("Unmounting %s from %s\n",
4548 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
4549 vfs_statfs(nmp
->nm_mountp
)->f_mntonname
);
4550 NFS_VFS_DBG("nfs state = 0x%8.8x\n", nmp
->nm_state
);
4551 NFS_VFS_DBG("nfs socket flags = 0x%8.8x\n", nmp
->nm_sockflags
);
4552 NFS_VFS_DBG("nfs mount ref count is %d\n", nmp
->nm_ref
);
4553 NFS_VFS_DBG("mount ref count is %d\n", nmp
->nm_mountp
->mnt_count
);
4556 vfs_setfsprivate(nmp
->nm_mountp
, NULL
);
4558 lck_mtx_lock(&nmp
->nm_lock
);
4560 panic("Some one has grabbed a ref %d state flags = 0x%8.8x\n", nmp
->nm_ref
, nmp
->nm_state
);
4563 FREE(nmp
->nm_saddr
, M_SONAME
);
4564 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_rqsaddr
)
4565 FREE(nmp
->nm_rqsaddr
, M_SONAME
);
4567 if (IS_VALID_CRED(nmp
->nm_mcred
))
4568 kauth_cred_unref(&nmp
->nm_mcred
);
4570 nfs_fs_locations_cleanup(&nmp
->nm_locations
);
4573 FREE(nmp
->nm_realm
, M_TEMP
);
4574 if (nmp
->nm_principal
)
4575 FREE(nmp
->nm_principal
, M_TEMP
);
4577 FREE(nmp
->nm_sprinc
, M_TEMP
);
4580 xb_free(nmp
->nm_args
);
4582 lck_mtx_unlock(&nmp
->nm_lock
);
4584 lck_mtx_destroy(&nmp
->nm_lock
, nfs_mount_grp
);
4586 FREE(nmp
->nm_fh
, M_TEMP
);
4587 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
4591 * Return root of a filesystem
4594 nfs_vfs_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t ctx
)
4597 struct nfsmount
*nmp
;
4602 if (!nmp
|| !nmp
->nm_dnp
)
4604 vp
= NFSTOV(nmp
->nm_dnp
);
4605 vpid
= vnode_vid(vp
);
4606 while ((error
= vnode_getwithvid(vp
, vpid
))) {
4607 /* vnode_get() may return ENOENT if the dir changes. */
4608 /* If that happens, just try it again, else return the error. */
4609 if ((error
!= ENOENT
) || (vnode_vid(vp
) == vpid
))
4611 vpid
= vnode_vid(vp
);
4618 * Do operations associated with quotas
4623 __unused mount_t mp
,
4626 __unused caddr_t datap
,
4627 __unused vfs_context_t context
)
4634 nfs3_getquota(struct nfsmount
*nmp
, vfs_context_t ctx
, uid_t id
, int type
, struct dqblk
*dqb
)
4636 int error
= 0, slen
, timeo
;
4637 int rqport
= 0, rqproto
, rqvers
= (type
== GRPQUOTA
) ? RPCRQUOTA_EXT_VER
: RPCRQUOTA_VER
;
4638 thread_t thd
= vfs_context_thread(ctx
);
4639 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4642 struct nfsm_chain nmreq
, nmrep
;
4644 uint32_t val
= 0, bsize
= 0;
4645 struct sockaddr
*rqsaddr
;
4651 if (NMFLAG(nmp
, NOQUOTA
))
4654 if (!nmp
->nm_rqsaddr
)
4655 MALLOC(nmp
->nm_rqsaddr
, struct sockaddr
*, sizeof(struct sockaddr_storage
), M_SONAME
, M_WAITOK
|M_ZERO
);
4656 if (!nmp
->nm_rqsaddr
)
4658 rqsaddr
= nmp
->nm_rqsaddr
;
4659 if (rqsaddr
->sa_family
== AF_INET6
)
4660 rqport
= ntohs(((struct sockaddr_in6
*)rqsaddr
)->sin6_port
);
4661 else if (rqsaddr
->sa_family
== AF_INET
)
4662 rqport
= ntohs(((struct sockaddr_in
*)rqsaddr
)->sin_port
);
4664 timeo
= NMFLAG(nmp
, SOFT
) ? 10 : 60;
4665 rqproto
= IPPROTO_UDP
; /* XXX should prefer TCP if mount is TCP */
4667 /* check if we have a recently cached rquota port */
4669 if (!rqport
|| ((nmp
->nm_rqsaddrstamp
+ 60) >= (uint32_t)now
.tv_sec
)) {
4670 /* send portmap request to get rquota port */
4671 bcopy(nmp
->nm_saddr
, rqsaddr
, min(sizeof(struct sockaddr_storage
), nmp
->nm_saddr
->sa_len
));
4672 error
= nfs_portmap_lookup(nmp
, ctx
, rqsaddr
, NULL
, RPCPROG_RQUOTA
, rqvers
, rqproto
, timeo
);
4675 if (rqsaddr
->sa_family
== AF_INET6
)
4676 rqport
= ntohs(((struct sockaddr_in6
*)rqsaddr
)->sin6_port
);
4677 else if (rqsaddr
->sa_family
== AF_INET
)
4678 rqport
= ntohs(((struct sockaddr_in
*)rqsaddr
)->sin_port
);
4684 nmp
->nm_rqsaddrstamp
= now
.tv_sec
;
4687 /* rquota request */
4688 nfsm_chain_null(&nmreq
);
4689 nfsm_chain_null(&nmrep
);
4690 path
= &vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
[0];
4691 while (*path
&& (*path
!= '/'))
4693 slen
= strlen(path
);
4694 nfsm_chain_build_alloc_init(error
, &nmreq
, 3 * NFSX_UNSIGNED
+ nfsm_rndup(slen
));
4695 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
4696 if (type
== GRPQUOTA
)
4697 nfsm_chain_add_32(error
, &nmreq
, type
);
4698 nfsm_chain_add_32(error
, &nmreq
, id
);
4699 nfsm_chain_build_done(error
, &nmreq
);
4701 error
= nfsm_rpchead2(nmp
, (rqproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4702 RPCPROG_RQUOTA
, rqvers
, RPCRQUOTA_GET
,
4703 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
4705 nmreq
.nmc_mhead
= NULL
;
4706 error
= nfs_aux_request(nmp
, thd
, rqsaddr
, NULL
,
4707 (rqproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4708 mreq
, R_XID32(xid
), 0, timeo
, &nmrep
);
4711 /* parse rquota response */
4712 nfsm_chain_get_32(error
, &nmrep
, val
);
4713 if (!error
&& (val
!= RQUOTA_STAT_OK
)) {
4714 if (val
== RQUOTA_STAT_NOQUOTA
)
4716 else if (val
== RQUOTA_STAT_EPERM
)
4721 nfsm_chain_get_32(error
, &nmrep
, bsize
);
4722 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
4723 nfsm_chain_get_32(error
, &nmrep
, val
);
4725 dqb
->dqb_bhardlimit
= (uint64_t)val
* bsize
;
4726 nfsm_chain_get_32(error
, &nmrep
, val
);
4728 dqb
->dqb_bsoftlimit
= (uint64_t)val
* bsize
;
4729 nfsm_chain_get_32(error
, &nmrep
, val
);
4731 dqb
->dqb_curbytes
= (uint64_t)val
* bsize
;
4732 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_ihardlimit
);
4733 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_isoftlimit
);
4734 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_curinodes
);
4735 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_btime
);
4736 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_itime
);
4740 nfsm_chain_cleanup(&nmreq
);
4741 nfsm_chain_cleanup(&nmrep
);
4746 nfs4_getquota(struct nfsmount
*nmp
, vfs_context_t ctx
, uid_t id
, int type
, struct dqblk
*dqb
)
4749 int error
= 0, status
, nfsvers
, numops
;
4751 struct nfsm_chain nmreq
, nmrep
;
4752 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
4753 thread_t thd
= vfs_context_thread(ctx
);
4754 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4755 struct nfsreq_secinfo_args si
;
4757 if (type
!= USRQUOTA
) /* NFSv4 only supports user quotas */
4760 /* first check that the server supports any of the quota attributes */
4761 if (!NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_HARD
) &&
4762 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_SOFT
) &&
4763 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_USED
))
4767 * The credential passed to the server needs to have
4768 * an effective uid that matches the given uid.
4770 if (id
!= kauth_cred_getuid(cred
)) {
4771 struct posix_cred temp_pcred
;
4772 posix_cred_t pcred
= posix_cred_get(cred
);
4773 bzero(&temp_pcred
, sizeof(temp_pcred
));
4774 temp_pcred
.cr_uid
= id
;
4775 temp_pcred
.cr_ngroups
= pcred
->cr_ngroups
;
4776 bcopy(pcred
->cr_groups
, temp_pcred
.cr_groups
, sizeof(temp_pcred
.cr_groups
));
4777 cred
= posix_cred_create(&temp_pcred
);
4778 if (!IS_VALID_CRED(cred
))
4781 kauth_cred_ref(cred
);
4784 nfsvers
= nmp
->nm_vers
;
4788 if (error
|| ((error
= vnode_get(NFSTOV(np
))))) {
4789 kauth_cred_unref(&cred
);
4793 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
4794 nfsm_chain_null(&nmreq
);
4795 nfsm_chain_null(&nmrep
);
4799 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
4800 nfsm_chain_add_compound_header(error
, &nmreq
, "quota", nmp
->nm_minor_vers
, numops
);
4802 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4803 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4805 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4806 NFS_CLEAR_ATTRIBUTES(bitmap
);
4807 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_AVAIL_HARD
);
4808 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_AVAIL_SOFT
);
4809 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_USED
);
4810 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
4811 nfsm_chain_build_done(error
, &nmreq
);
4812 nfsm_assert(error
, (numops
== 0), EPROTO
);
4814 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, &nmrep
, &xid
, &status
);
4815 nfsm_chain_skip_tag(error
, &nmrep
);
4816 nfsm_chain_get_32(error
, &nmrep
, numops
);
4817 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4818 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4819 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
4821 error
= nfs4_parsefattr(&nmrep
, NULL
, NULL
, NULL
, dqb
, NULL
);
4823 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
4825 nfsm_chain_cleanup(&nmreq
);
4826 nfsm_chain_cleanup(&nmrep
);
4827 vnode_put(NFSTOV(np
));
4828 kauth_cred_unref(&cred
);
4833 nfs_vfs_quotactl(mount_t mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t ctx
)
4835 struct nfsmount
*nmp
;
4836 int cmd
, type
, error
, nfsvers
;
4837 uid_t euid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
4838 struct dqblk
*dqb
= (struct dqblk
*)datap
;
4841 if (nfs_mount_gone(nmp
))
4843 nfsvers
= nmp
->nm_vers
;
4848 /* we can only support Q_GETQUOTA */
4849 cmd
= cmds
>> SUBCMDSHIFT
;
4864 type
= cmds
& SUBCMDMASK
;
4865 if ((u_int
)type
>= MAXQUOTAS
)
4867 if ((uid
!= euid
) && ((error
= vfs_context_suser(ctx
))))
4870 if (vfs_busy(mp
, LK_NOWAIT
))
4872 bzero(dqb
, sizeof(*dqb
));
4873 error
= nmp
->nm_funcs
->nf_getquota(nmp
, ctx
, uid
, type
, dqb
);
4880 * Flush out the buffer cache
4882 int nfs_sync_callout(vnode_t
, void *);
4884 struct nfs_sync_cargs
{
4891 nfs_sync_callout(vnode_t vp
, void *arg
)
4893 struct nfs_sync_cargs
*cargs
= (struct nfs_sync_cargs
*)arg
;
4894 nfsnode_t np
= VTONFS(vp
);
4897 if (np
->n_flag
& NREVOKE
) {
4898 vn_revoke(vp
, REVOKEALL
, cargs
->ctx
);
4899 return (VNODE_RETURNED
);
4902 if (LIST_EMPTY(&np
->n_dirtyblkhd
))
4903 return (VNODE_RETURNED
);
4904 if (np
->n_wrbusy
> 0)
4905 return (VNODE_RETURNED
);
4906 if (np
->n_bflag
& (NBFLUSHINPROG
|NBINVALINPROG
))
4907 return (VNODE_RETURNED
);
4909 error
= nfs_flush(np
, cargs
->waitfor
, vfs_context_thread(cargs
->ctx
), 0);
4911 cargs
->error
= error
;
4913 return (VNODE_RETURNED
);
4917 nfs_vfs_sync(mount_t mp
, int waitfor
, vfs_context_t ctx
)
4919 struct nfs_sync_cargs cargs
;
4921 cargs
.waitfor
= waitfor
;
4925 vnode_iterate(mp
, 0, nfs_sync_callout
, &cargs
);
4927 return (cargs
.error
);
4931 * NFS flat namespace lookup.
4932 * Currently unsupported.
4937 __unused mount_t mp
,
4938 __unused ino64_t ino
,
4939 __unused vnode_t
*vpp
,
4940 __unused vfs_context_t ctx
)
4947 * At this point, this should never happen
4952 __unused mount_t mp
,
4954 __unused
unsigned char *fhp
,
4955 __unused vnode_t
*vpp
,
4956 __unused vfs_context_t ctx
)
4963 * Vnode pointer to File handle, should never happen either
4968 __unused vnode_t vp
,
4969 __unused
int *fhlenp
,
4970 __unused
unsigned char *fhp
,
4971 __unused vfs_context_t ctx
)
4978 * Vfs start routine, a no-op.
4983 __unused mount_t mp
,
4985 __unused vfs_context_t ctx
)
4992 * Build the mount info buffer for NFS_MOUNTINFO.
4995 nfs_mountinfo_assemble(struct nfsmount
*nmp
, struct xdrbuf
*xb
)
4997 struct xdrbuf xbinfo
, xborig
;
4999 uint32_t origargsvers
, origargslength
;
5000 uint32_t infolength_offset
, curargsopaquelength_offset
, curargslength_offset
, attrslength_offset
, curargs_end_offset
, end_offset
;
5001 uint32_t miattrs
[NFS_MIATTR_BITMAP_LEN
];
5002 uint32_t miflags_mask
[NFS_MIFLAG_BITMAP_LEN
];
5003 uint32_t miflags
[NFS_MIFLAG_BITMAP_LEN
];
5004 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
5005 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
];
5006 uint32_t mflags
[NFS_MFLAG_BITMAP_LEN
];
5007 uint32_t loc
, serv
, addr
, comp
;
5008 int i
, timeo
, error
= 0;
5010 /* set up mount info attr and flag bitmaps */
5011 NFS_BITMAP_ZERO(miattrs
, NFS_MIATTR_BITMAP_LEN
);
5012 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_FLAGS
);
5013 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_ORIG_ARGS
);
5014 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_CUR_ARGS
);
5015 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_CUR_LOC_INDEX
);
5016 NFS_BITMAP_ZERO(miflags_mask
, NFS_MIFLAG_BITMAP_LEN
);
5017 NFS_BITMAP_ZERO(miflags
, NFS_MIFLAG_BITMAP_LEN
);
5018 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_DEAD
);
5019 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_NOTRESP
);
5020 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_RECOVERY
);
5021 if (nmp
->nm_state
& NFSSTA_DEAD
)
5022 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_DEAD
);
5023 if ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_JUKEBOXTIMEO
)) ||
5024 ((nmp
->nm_state
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
)))
5025 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_NOTRESP
);
5026 if (nmp
->nm_state
& NFSSTA_RECOVER
)
5027 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_RECOVERY
);
5029 /* get original mount args length */
5030 xb_init_buffer(&xborig
, nmp
->nm_args
, 2*XDRWORD
);
5031 xb_get_32(error
, &xborig
, origargsvers
); /* version */
5032 xb_get_32(error
, &xborig
, origargslength
); /* args length */
5035 /* set up current mount attributes bitmap */
5036 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
5037 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FLAGS
);
5038 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
5039 if (nmp
->nm_vers
>= NFS_VER4
)
5040 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
);
5041 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READ_SIZE
);
5042 NFS_BITMAP_SET(mattrs
, NFS_MATTR_WRITE_SIZE
);
5043 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READDIR_SIZE
);
5044 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READAHEAD
);
5045 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
);
5046 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
);
5047 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
);
5048 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
);
5049 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCK_MODE
);
5050 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SECURITY
);
5051 if (nmp
->nm_etype
.selected
< nmp
->nm_etype
.count
)
5052 NFS_BITMAP_SET(mattrs
, NFS_MATTR_KERB_ETYPE
);
5053 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
);
5054 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
5055 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
5056 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_mountport
)
5057 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MOUNT_PORT
);
5058 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
);
5059 if (NMFLAG(nmp
, SOFT
))
5060 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
);
5061 if (nmp
->nm_deadtimeout
)
5062 NFS_BITMAP_SET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
);
5064 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
5065 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
5066 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
5067 if (origargsvers
< NFS_ARGSVERSION_XDR
)
5068 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFROM
);
5070 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REALM
);
5071 if (nmp
->nm_principal
)
5072 NFS_BITMAP_SET(mattrs
, NFS_MATTR_PRINCIPAL
);
5074 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SVCPRINCIPAL
);
5076 /* set up current mount flags bitmap */
5077 /* first set the flags that we will be setting - either on OR off */
5078 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
5079 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_SOFT
);
5080 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_INTR
);
5081 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
5082 if (nmp
->nm_sotype
== SOCK_DGRAM
)
5083 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCONNECT
);
5084 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_DUMBTIMER
);
5085 if (nmp
->nm_vers
< NFS_VER4
)
5086 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_CALLUMNT
);
5087 if (nmp
->nm_vers
>= NFS_VER3
)
5088 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RDIRPLUS
);
5089 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NONEGNAMECACHE
);
5090 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MUTEJUKEBOX
);
5091 if (nmp
->nm_vers
>= NFS_VER4
) {
5092 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_EPHEMERAL
);
5093 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCALLBACK
);
5094 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NONAMEDATTR
);
5095 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOACL
);
5096 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_ACLONLY
);
5098 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NFC
);
5099 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOQUOTA
);
5100 if (nmp
->nm_vers
< NFS_VER4
)
5101 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MNTUDP
);
5102 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MNTQUICK
);
5103 /* now set the flags that should be set */
5104 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
5105 if (NMFLAG(nmp
, SOFT
))
5106 NFS_BITMAP_SET(mflags
, NFS_MFLAG_SOFT
);
5107 if (NMFLAG(nmp
, INTR
))
5108 NFS_BITMAP_SET(mflags
, NFS_MFLAG_INTR
);
5109 if (NMFLAG(nmp
, RESVPORT
))
5110 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
5111 if ((nmp
->nm_sotype
== SOCK_DGRAM
) && NMFLAG(nmp
, NOCONNECT
))
5112 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCONNECT
);
5113 if (NMFLAG(nmp
, DUMBTIMER
))
5114 NFS_BITMAP_SET(mflags
, NFS_MFLAG_DUMBTIMER
);
5115 if ((nmp
->nm_vers
< NFS_VER4
) && NMFLAG(nmp
, CALLUMNT
))
5116 NFS_BITMAP_SET(mflags
, NFS_MFLAG_CALLUMNT
);
5117 if ((nmp
->nm_vers
>= NFS_VER3
) && NMFLAG(nmp
, RDIRPLUS
))
5118 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RDIRPLUS
);
5119 if (NMFLAG(nmp
, NONEGNAMECACHE
))
5120 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NONEGNAMECACHE
);
5121 if (NMFLAG(nmp
, MUTEJUKEBOX
))
5122 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MUTEJUKEBOX
);
5123 if (nmp
->nm_vers
>= NFS_VER4
) {
5124 if (NMFLAG(nmp
, EPHEMERAL
))
5125 NFS_BITMAP_SET(mflags
, NFS_MFLAG_EPHEMERAL
);
5126 if (NMFLAG(nmp
, NOCALLBACK
))
5127 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCALLBACK
);
5128 if (NMFLAG(nmp
, NONAMEDATTR
))
5129 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NONAMEDATTR
);
5130 if (NMFLAG(nmp
, NOACL
))
5131 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOACL
);
5132 if (NMFLAG(nmp
, ACLONLY
))
5133 NFS_BITMAP_SET(mflags
, NFS_MFLAG_ACLONLY
);
5135 if (NMFLAG(nmp
, NFC
))
5136 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NFC
);
5137 if (NMFLAG(nmp
, NOQUOTA
) || ((nmp
->nm_vers
>= NFS_VER4
) &&
5138 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_HARD
) &&
5139 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_SOFT
) &&
5140 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_USED
)))
5141 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOQUOTA
);
5142 if ((nmp
->nm_vers
< NFS_VER4
) && NMFLAG(nmp
, MNTUDP
))
5143 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MNTUDP
);
5144 if (NMFLAG(nmp
, MNTQUICK
))
5145 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MNTQUICK
);
5147 /* assemble info buffer: */
5148 xb_init_buffer(&xbinfo
, NULL
, 0);
5149 xb_add_32(error
, &xbinfo
, NFS_MOUNT_INFO_VERSION
);
5150 infolength_offset
= xb_offset(&xbinfo
);
5151 xb_add_32(error
, &xbinfo
, 0);
5152 xb_add_bitmap(error
, &xbinfo
, miattrs
, NFS_MIATTR_BITMAP_LEN
);
5153 xb_add_bitmap(error
, &xbinfo
, miflags
, NFS_MIFLAG_BITMAP_LEN
);
5154 xb_add_32(error
, &xbinfo
, origargslength
);
5156 error
= xb_add_bytes(&xbinfo
, nmp
->nm_args
, origargslength
, 0);
5158 /* the opaque byte count for the current mount args values: */
5159 curargsopaquelength_offset
= xb_offset(&xbinfo
);
5160 xb_add_32(error
, &xbinfo
, 0);
5162 /* Encode current mount args values */
5163 xb_add_32(error
, &xbinfo
, NFS_ARGSVERSION_XDR
);
5164 curargslength_offset
= xb_offset(&xbinfo
);
5165 xb_add_32(error
, &xbinfo
, 0);
5166 xb_add_32(error
, &xbinfo
, NFS_XDRARGS_VERSION_0
);
5167 xb_add_bitmap(error
, &xbinfo
, mattrs
, NFS_MATTR_BITMAP_LEN
);
5168 attrslength_offset
= xb_offset(&xbinfo
);
5169 xb_add_32(error
, &xbinfo
, 0);
5170 xb_add_bitmap(error
, &xbinfo
, mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
5171 xb_add_bitmap(error
, &xbinfo
, mflags
, NFS_MFLAG_BITMAP_LEN
);
5172 xb_add_32(error
, &xbinfo
, nmp
->nm_vers
); /* NFS_VERSION */
5173 if (nmp
->nm_vers
>= NFS_VER4
)
5174 xb_add_32(error
, &xbinfo
, nmp
->nm_minor_vers
); /* NFS_MINOR_VERSION */
5175 xb_add_32(error
, &xbinfo
, nmp
->nm_rsize
); /* READ_SIZE */
5176 xb_add_32(error
, &xbinfo
, nmp
->nm_wsize
); /* WRITE_SIZE */
5177 xb_add_32(error
, &xbinfo
, nmp
->nm_readdirsize
); /* READDIR_SIZE */
5178 xb_add_32(error
, &xbinfo
, nmp
->nm_readahead
); /* READAHEAD */
5179 xb_add_32(error
, &xbinfo
, nmp
->nm_acregmin
); /* ATTRCACHE_REG_MIN */
5180 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_REG_MIN */
5181 xb_add_32(error
, &xbinfo
, nmp
->nm_acregmax
); /* ATTRCACHE_REG_MAX */
5182 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_REG_MAX */
5183 xb_add_32(error
, &xbinfo
, nmp
->nm_acdirmin
); /* ATTRCACHE_DIR_MIN */
5184 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_DIR_MIN */
5185 xb_add_32(error
, &xbinfo
, nmp
->nm_acdirmax
); /* ATTRCACHE_DIR_MAX */
5186 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_DIR_MAX */
5187 xb_add_32(error
, &xbinfo
, nmp
->nm_lockmode
); /* LOCK_MODE */
5188 if (nmp
->nm_sec
.count
) {
5189 xb_add_32(error
, &xbinfo
, nmp
->nm_sec
.count
); /* SECURITY */
5191 for (i
=0; i
< nmp
->nm_sec
.count
; i
++)
5192 xb_add_32(error
, &xbinfo
, nmp
->nm_sec
.flavors
[i
]);
5193 } else if (nmp
->nm_servsec
.count
) {
5194 xb_add_32(error
, &xbinfo
, nmp
->nm_servsec
.count
); /* SECURITY */
5196 for (i
=0; i
< nmp
->nm_servsec
.count
; i
++)
5197 xb_add_32(error
, &xbinfo
, nmp
->nm_servsec
.flavors
[i
]);
5199 xb_add_32(error
, &xbinfo
, 1); /* SECURITY */
5200 xb_add_32(error
, &xbinfo
, nmp
->nm_auth
);
5202 if (nmp
->nm_etype
.selected
< nmp
->nm_etype
.count
) {
5203 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.count
);
5204 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.selected
);
5205 for (uint32_t j
=0; j
< nmp
->nm_etype
.count
; j
++)
5206 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.etypes
[j
]);
5209 xb_add_32(error
, &xbinfo
, nmp
->nm_numgrps
); /* MAX_GROUP_LIST */
5211 snprintf(sotype
, sizeof(sotype
), "%s%s", (nmp
->nm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp",
5212 nmp
->nm_sofamily
? (nmp
->nm_sofamily
== AF_INET
) ? "4" : "6" : "");
5213 xb_add_string(error
, &xbinfo
, sotype
, strlen(sotype
)); /* SOCKET_TYPE */
5214 xb_add_32(error
, &xbinfo
, ntohs(((struct sockaddr_in
*)nmp
->nm_saddr
)->sin_port
)); /* NFS_PORT */
5215 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_mountport
)
5216 xb_add_32(error
, &xbinfo
, nmp
->nm_mountport
); /* MOUNT_PORT */
5217 timeo
= (nmp
->nm_timeo
* 10) / NFS_HZ
;
5218 xb_add_32(error
, &xbinfo
, timeo
/10); /* REQUEST_TIMEOUT */
5219 xb_add_32(error
, &xbinfo
, (timeo%10
)*100000000); /* REQUEST_TIMEOUT */
5220 if (NMFLAG(nmp
, SOFT
))
5221 xb_add_32(error
, &xbinfo
, nmp
->nm_retry
); /* SOFT_RETRY_COUNT */
5222 if (nmp
->nm_deadtimeout
) {
5223 xb_add_32(error
, &xbinfo
, nmp
->nm_deadtimeout
); /* DEAD_TIMEOUT */
5224 xb_add_32(error
, &xbinfo
, 0); /* DEAD_TIMEOUT */
5227 xb_add_fh(error
, &xbinfo
, &nmp
->nm_fh
->fh_data
[0], nmp
->nm_fh
->fh_len
); /* FH */
5228 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_numlocs
); /* FS_LOCATIONS */
5229 for (loc
= 0; !error
&& (loc
< nmp
->nm_locations
.nl_numlocs
); loc
++) {
5230 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servcount
);
5231 for (serv
= 0; !error
&& (serv
< nmp
->nm_locations
.nl_locations
[loc
]->nl_servcount
); serv
++) {
5232 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
,
5233 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
));
5234 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
);
5235 for (addr
= 0; !error
&& (addr
< nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
); addr
++)
5236 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
],
5237 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
]));
5238 xb_add_32(error
, &xbinfo
, 0); /* empty server info */
5240 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_compcount
);
5241 for (comp
= 0; !error
&& (comp
< nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_compcount
); comp
++)
5242 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_components
[comp
],
5243 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_components
[comp
]));
5244 xb_add_32(error
, &xbinfo
, 0); /* empty fs location info */
5246 xb_add_32(error
, &xbinfo
, vfs_flags(nmp
->nm_mountp
)); /* MNTFLAGS */
5247 if (origargsvers
< NFS_ARGSVERSION_XDR
)
5248 xb_add_string(error
, &xbinfo
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
5249 strlen(vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
)); /* MNTFROM */
5250 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REALM
))
5251 xb_add_string(error
, &xbinfo
, nmp
->nm_realm
, strlen(nmp
->nm_realm
));
5252 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_PRINCIPAL
))
5253 xb_add_string(error
, &xbinfo
, nmp
->nm_principal
, strlen(nmp
->nm_principal
));
5254 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SVCPRINCIPAL
))
5255 xb_add_string(error
, &xbinfo
, nmp
->nm_sprinc
, strlen(nmp
->nm_sprinc
));
5257 curargs_end_offset
= xb_offset(&xbinfo
);
5259 /* NFS_MIATTR_CUR_LOC_INDEX */
5260 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_flags
);
5261 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_loc
);
5262 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_serv
);
5263 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_addr
);
5265 xb_build_done(error
, &xbinfo
);
5267 /* update opaque counts */
5268 end_offset
= xb_offset(&xbinfo
);
5270 error
= xb_seek(&xbinfo
, attrslength_offset
);
5271 xb_add_32(error
, &xbinfo
, curargs_end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
5274 error
= xb_seek(&xbinfo
, curargslength_offset
);
5275 xb_add_32(error
, &xbinfo
, curargs_end_offset
- curargslength_offset
+ XDRWORD
/*version*/);
5278 error
= xb_seek(&xbinfo
, curargsopaquelength_offset
);
5279 xb_add_32(error
, &xbinfo
, curargs_end_offset
- curargslength_offset
+ XDRWORD
/*version*/);
5282 error
= xb_seek(&xbinfo
, infolength_offset
);
5283 xb_add_32(error
, &xbinfo
, end_offset
- infolength_offset
+ XDRWORD
/*version*/);
5287 /* copy result xdrbuf to caller */
5290 /* and mark the local copy as not needing cleanup */
5291 xbinfo
.xb_flags
&= ~XB_CLEANUP
;
5293 xb_cleanup(&xbinfo
);
5298 * Do that sysctl thang...
5301 nfs_vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
5302 user_addr_t newp
, size_t newlen
, vfs_context_t ctx
)
5306 struct sysctl_req
*req
= NULL
;
5307 union union_vfsidctl vc
;
5309 struct nfsmount
*nmp
= NULL
;
5312 boolean_t is_64_bit
;
5315 struct netfs_status
*nsp
= NULL
;
5317 uint pos
, totlen
, count
, numThreads
;
5319 struct nfs_exportfs
*nxfs
;
5320 struct nfs_export
*nx
;
5321 struct nfs_active_user_list
*ulist
;
5322 struct nfs_export_stat_desc stat_desc
;
5323 struct nfs_export_stat_rec statrec
;
5324 struct nfs_user_stat_node
*unode
, *unode_next
;
5325 struct nfs_user_stat_desc ustat_desc
;
5326 struct nfs_user_stat_user_rec ustat_rec
;
5327 struct nfs_user_stat_path_rec upath_rec
;
5328 uint bytes_avail
, bytes_total
, recs_copied
;
5329 uint numExports
, numRecs
;
5330 #endif /* NFSSERVER */
5333 * All names at this level are terminal.
5336 return (ENOTDIR
); /* overloaded */
5338 is_64_bit
= vfs_context_is64bit(ctx
);
5340 /* common code for "new style" VFS_CTL sysctl, get the mount. */
5343 case VFS_CTL_NOLOCKS
:
5344 case VFS_CTL_NSTATUS
:
5346 req
= CAST_DOWN(struct sysctl_req
*, oldp
);
5350 error
= SYSCTL_IN(req
, &vc
, is_64_bit
? sizeof(vc
.vc64
):sizeof(vc
.vc32
));
5353 mp
= vfs_getvfs(&vc
.vc32
.vc_fsid
); /* works for 32 and 64 */
5359 bzero(&vq
, sizeof(vq
));
5362 req
->newptr
= vc
.vc64
.vc_ptr
;
5363 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
5365 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
5366 req
->newlen
= vc
.vc32
.vc_len
;
5374 *oldlenp
= sizeof nfsstats
;
5378 if (*oldlenp
< sizeof nfsstats
) {
5379 *oldlenp
= sizeof nfsstats
;
5383 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
5387 if (newp
&& newlen
!= sizeof nfsstats
)
5391 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
5394 /* read in the fsid */
5395 if (*oldlenp
< sizeof(fsid
))
5397 if ((error
= copyin(oldp
, &fsid
, sizeof(fsid
))))
5399 /* swizzle it back to host order */
5400 fsid
.val
[0] = ntohl(fsid
.val
[0]);
5401 fsid
.val
[1] = ntohl(fsid
.val
[1]);
5402 /* find mount and make sure it's NFS */
5403 if (((mp
= vfs_getvfs(&fsid
))) == NULL
)
5405 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs"))
5407 if (((nmp
= VFSTONFS(mp
))) == NULL
)
5410 if ((error
= nfs_mountinfo_assemble(nmp
, &xb
)))
5412 if (*oldlenp
< xb
.xb_u
.xb_buffer
.xbb_len
)
5415 error
= copyout(xb_buffer_base(&xb
), oldp
, xb
.xb_u
.xb_buffer
.xbb_len
);
5416 *oldlenp
= xb
.xb_u
.xb_buffer
.xbb_len
;
5420 case NFS_EXPORTSTATS
:
5421 /* setup export stat descriptor */
5422 stat_desc
.rec_vers
= NFS_EXPORT_STAT_REC_VERSION
;
5424 if (!nfsrv_is_initialized()) {
5425 stat_desc
.rec_count
= 0;
5426 if (oldp
&& (*oldlenp
>= sizeof(struct nfs_export_stat_desc
)))
5427 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
5428 *oldlenp
= sizeof(struct nfs_export_stat_desc
);
5432 /* Count the number of exported directories */
5433 lck_rw_lock_shared(&nfsrv_export_rwlock
);
5435 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
)
5436 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
)
5439 /* update stat descriptor's export record count */
5440 stat_desc
.rec_count
= numExports
;
5442 /* calculate total size of required buffer */
5443 totlen
= sizeof(struct nfs_export_stat_desc
) + (numExports
* sizeof(struct nfs_export_stat_rec
));
5445 /* Check caller's buffer */
5447 lck_rw_done(&nfsrv_export_rwlock
);
5448 /* indicate required buffer len */
5453 /* We require the caller's buffer to be at least large enough to hold the descriptor */
5454 if (*oldlenp
< sizeof(struct nfs_export_stat_desc
)) {
5455 lck_rw_done(&nfsrv_export_rwlock
);
5456 /* indicate required buffer len */
5461 /* indicate required buffer len */
5464 /* check if export table is empty */
5466 lck_rw_done(&nfsrv_export_rwlock
);
5467 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
5471 /* calculate how many actual export stat records fit into caller's buffer */
5472 numRecs
= (*oldlenp
- sizeof(struct nfs_export_stat_desc
)) / sizeof(struct nfs_export_stat_rec
);
5475 /* caller's buffer can only accomodate descriptor */
5476 lck_rw_done(&nfsrv_export_rwlock
);
5477 stat_desc
.rec_count
= 0;
5478 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
5482 /* adjust to actual number of records to copyout to caller's buffer */
5483 if (numRecs
> numExports
)
5484 numRecs
= numExports
;
5486 /* set actual number of records we are returning */
5487 stat_desc
.rec_count
= numRecs
;
5489 /* first copy out the stat descriptor */
5491 error
= copyout(&stat_desc
, oldp
+ pos
, sizeof(struct nfs_export_stat_desc
));
5493 lck_rw_done(&nfsrv_export_rwlock
);
5496 pos
+= sizeof(struct nfs_export_stat_desc
);
5498 /* Loop through exported directories */
5500 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
5501 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
5503 if (count
>= numRecs
)
5506 /* build exported filesystem path */
5507 snprintf(statrec
.path
, sizeof(statrec
.path
), "%s%s%s",
5508 nxfs
->nxfs_path
, ((nxfs
->nxfs_path
[1] && nx
->nx_path
[0]) ? "/" : ""),
5511 /* build the 64-bit export stat counters */
5512 statrec
.ops
= ((uint64_t)nx
->nx_stats
.ops
.hi
<< 32) |
5513 nx
->nx_stats
.ops
.lo
;
5514 statrec
.bytes_read
= ((uint64_t)nx
->nx_stats
.bytes_read
.hi
<< 32) |
5515 nx
->nx_stats
.bytes_read
.lo
;
5516 statrec
.bytes_written
= ((uint64_t)nx
->nx_stats
.bytes_written
.hi
<< 32) |
5517 nx
->nx_stats
.bytes_written
.lo
;
5518 error
= copyout(&statrec
, oldp
+ pos
, sizeof(statrec
));
5520 lck_rw_done(&nfsrv_export_rwlock
);
5523 /* advance buffer position */
5524 pos
+= sizeof(statrec
);
5527 lck_rw_done(&nfsrv_export_rwlock
);
5530 /* init structures used for copying out of kernel */
5531 ustat_desc
.rec_vers
= NFS_USER_STAT_REC_VERSION
;
5532 ustat_rec
.rec_type
= NFS_USER_STAT_USER_REC
;
5533 upath_rec
.rec_type
= NFS_USER_STAT_PATH_REC
;
5535 /* initialize counters */
5536 bytes_total
= sizeof(struct nfs_user_stat_desc
);
5537 bytes_avail
= *oldlenp
;
5540 if (!nfsrv_is_initialized()) /* NFS server not initialized, so no stats */
5543 /* reclaim old expired user nodes */
5544 nfsrv_active_user_list_reclaim();
5546 /* reserve space for the buffer descriptor */
5547 if (bytes_avail
>= sizeof(struct nfs_user_stat_desc
))
5548 bytes_avail
-= sizeof(struct nfs_user_stat_desc
);
5552 /* put buffer position past the buffer descriptor */
5553 pos
= sizeof(struct nfs_user_stat_desc
);
5555 /* Loop through exported directories */
5556 lck_rw_lock_shared(&nfsrv_export_rwlock
);
5557 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
5558 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
5560 if (bytes_avail
>= sizeof(struct nfs_user_stat_path_rec
)) {
5561 snprintf(upath_rec
.path
, sizeof(upath_rec
.path
), "%s%s%s",
5562 nxfs
->nxfs_path
, ((nxfs
->nxfs_path
[1] && nx
->nx_path
[0]) ? "/" : ""),
5565 error
= copyout(&upath_rec
, oldp
+ pos
, sizeof(struct nfs_user_stat_path_rec
));
5571 pos
+= sizeof(struct nfs_user_stat_path_rec
);
5572 bytes_avail
-= sizeof(struct nfs_user_stat_path_rec
);
5576 /* Caller's buffer is exhausted */
5580 bytes_total
+= sizeof(struct nfs_user_stat_path_rec
);
5582 /* Scan through all user nodes of this export */
5583 ulist
= &nx
->nx_user_list
;
5584 lck_mtx_lock(&ulist
->user_mutex
);
5585 for (unode
= TAILQ_FIRST(&ulist
->user_lru
); unode
; unode
= unode_next
) {
5586 unode_next
= TAILQ_NEXT(unode
, lru_link
);
5588 /* copy out node if there is space */
5589 if (bytes_avail
>= sizeof(struct nfs_user_stat_user_rec
)) {
5590 /* prepare a user stat rec for copying out */
5591 ustat_rec
.uid
= unode
->uid
;
5592 bcopy(&unode
->sock
, &ustat_rec
.sock
, unode
->sock
.ss_len
);
5593 ustat_rec
.ops
= unode
->ops
;
5594 ustat_rec
.bytes_read
= unode
->bytes_read
;
5595 ustat_rec
.bytes_written
= unode
->bytes_written
;
5596 ustat_rec
.tm_start
= unode
->tm_start
;
5597 ustat_rec
.tm_last
= unode
->tm_last
;
5599 error
= copyout(&ustat_rec
, oldp
+ pos
, sizeof(struct nfs_user_stat_user_rec
));
5603 lck_mtx_unlock(&ulist
->user_mutex
);
5607 pos
+= sizeof(struct nfs_user_stat_user_rec
);
5608 bytes_avail
-= sizeof(struct nfs_user_stat_user_rec
);
5612 /* Caller's buffer is exhausted */
5615 bytes_total
+= sizeof(struct nfs_user_stat_user_rec
);
5617 /* can unlock this export's list now */
5618 lck_mtx_unlock(&ulist
->user_mutex
);
5623 /* unlock the export table */
5624 lck_rw_done(&nfsrv_export_rwlock
);
5627 /* indicate number of actual records copied */
5628 ustat_desc
.rec_count
= recs_copied
;
5631 /* check if there was enough room for the buffer descriptor */
5632 if (*oldlenp
>= sizeof(struct nfs_user_stat_desc
))
5633 error
= copyout(&ustat_desc
, oldp
, sizeof(struct nfs_user_stat_desc
));
5637 /* always indicate required buffer size */
5638 *oldlenp
= bytes_total
;
5643 *oldlenp
= sizeof(nfsrv_user_stat_node_count
);
5647 if (*oldlenp
< sizeof(nfsrv_user_stat_node_count
)) {
5648 *oldlenp
= sizeof(nfsrv_user_stat_node_count
);
5652 if (nfsrv_is_initialized()) {
5653 /* reclaim old expired user nodes */
5654 nfsrv_active_user_list_reclaim();
5657 error
= copyout(&nfsrv_user_stat_node_count
, oldp
, sizeof(nfsrv_user_stat_node_count
));
5659 #endif /* NFSSERVER */
5660 case VFS_CTL_NOLOCKS
:
5661 if (req
->oldptr
!= USER_ADDR_NULL
) {
5662 lck_mtx_lock(&nmp
->nm_lock
);
5663 val
= (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
) ? 1 : 0;
5664 lck_mtx_unlock(&nmp
->nm_lock
);
5665 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
5669 if (req
->newptr
!= USER_ADDR_NULL
) {
5670 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
5673 lck_mtx_lock(&nmp
->nm_lock
);
5674 if (nmp
->nm_lockmode
== NFS_LOCK_MODE_LOCAL
) {
5675 /* can't toggle locks when using local locks */
5677 } else if ((nmp
->nm_vers
>= NFS_VER4
) && val
) {
5678 /* can't disable locks for NFSv4 */
5681 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
))
5682 nfs_lockd_mount_unregister(nmp
);
5683 nmp
->nm_lockmode
= NFS_LOCK_MODE_DISABLED
;
5684 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
5686 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
))
5687 nfs_lockd_mount_register(nmp
);
5688 nmp
->nm_lockmode
= NFS_LOCK_MODE_ENABLED
;
5690 lck_mtx_unlock(&nmp
->nm_lock
);
5694 lck_mtx_lock(&nmp
->nm_lock
);
5695 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
5696 softnobrowse
= (NMFLAG(nmp
, SOFT
) && (vfs_flags(nmp
->nm_mountp
) & MNT_DONTBROWSE
));
5697 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_TIMEO
))
5698 vq
.vq_flags
|= VQ_NOTRESP
;
5699 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_JUKEBOXTIMEO
) && !NMFLAG(nmp
, MUTEJUKEBOX
))
5700 vq
.vq_flags
|= VQ_NOTRESP
;
5701 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_LOCKTIMEO
) &&
5702 (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
))
5703 vq
.vq_flags
|= VQ_NOTRESP
;
5704 if (nmp
->nm_state
& NFSSTA_DEAD
)
5705 vq
.vq_flags
|= VQ_DEAD
;
5706 lck_mtx_unlock(&nmp
->nm_lock
);
5707 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
5710 if (req
->oldptr
!= USER_ADDR_NULL
) {
5711 lck_mtx_lock(&nmp
->nm_lock
);
5712 val
= nmp
->nm_tprintf_initial_delay
;
5713 lck_mtx_unlock(&nmp
->nm_lock
);
5714 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
5718 if (req
->newptr
!= USER_ADDR_NULL
) {
5719 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
5722 lck_mtx_lock(&nmp
->nm_lock
);
5724 nmp
->nm_tprintf_initial_delay
= 0;
5726 nmp
->nm_tprintf_initial_delay
= val
;
5727 lck_mtx_unlock(&nmp
->nm_lock
);
5730 case VFS_CTL_NSTATUS
:
5732 * Return the status of this mount. This is much more
5733 * information than VFS_CTL_QUERY. In addition to the
5734 * vq_flags return the significant mount options along
5735 * with the list of threads blocked on the mount and
5736 * how long the threads have been waiting.
5739 lck_mtx_lock(nfs_request_mutex
);
5740 lck_mtx_lock(&nmp
->nm_lock
);
5743 * Count the number of requests waiting for a reply.
5744 * Note: there could be multiple requests from the same thread.
5747 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
5748 if (rq
->r_nmp
== nmp
)
5752 /* Calculate total size of result buffer */
5753 totlen
= sizeof(struct netfs_status
) + (numThreads
* sizeof(uint64_t));
5755 if (req
->oldptr
== USER_ADDR_NULL
) { // Caller is querying buffer size
5756 lck_mtx_unlock(&nmp
->nm_lock
);
5757 lck_mtx_unlock(nfs_request_mutex
);
5758 return SYSCTL_OUT(req
, NULL
, totlen
);
5760 if (req
->oldlen
< totlen
) { // Check if caller's buffer is big enough
5761 lck_mtx_unlock(&nmp
->nm_lock
);
5762 lck_mtx_unlock(nfs_request_mutex
);
5766 MALLOC(nsp
, struct netfs_status
*, totlen
, M_TEMP
, M_WAITOK
|M_ZERO
);
5768 lck_mtx_unlock(&nmp
->nm_lock
);
5769 lck_mtx_unlock(nfs_request_mutex
);
5772 timeoutmask
= NFSSTA_TIMEO
| NFSSTA_LOCKTIMEO
| NFSSTA_JUKEBOXTIMEO
;
5773 if (nmp
->nm_state
& timeoutmask
)
5774 nsp
->ns_status
|= VQ_NOTRESP
;
5775 if (nmp
->nm_state
& NFSSTA_DEAD
)
5776 nsp
->ns_status
|= VQ_DEAD
;
5778 (void) nfs_mountopts(nmp
, nsp
->ns_mountopts
, sizeof(nsp
->ns_mountopts
));
5779 nsp
->ns_threadcount
= numThreads
;
5782 * Get the thread ids of threads waiting for a reply
5783 * and find the longest wait time.
5785 if (numThreads
> 0) {
5791 sendtime
= now
.tv_sec
;
5792 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
5793 if (rq
->r_nmp
== nmp
) {
5794 if (rq
->r_start
< sendtime
)
5795 sendtime
= rq
->r_start
;
5796 // A thread_id of zero is used to represent an async I/O request.
5797 nsp
->ns_threadids
[count
] =
5798 rq
->r_thread
? thread_tid(rq
->r_thread
) : 0;
5799 if (++count
>= numThreads
)
5803 nsp
->ns_waittime
= now
.tv_sec
- sendtime
;
5806 lck_mtx_unlock(&nmp
->nm_lock
);
5807 lck_mtx_unlock(nfs_request_mutex
);
5809 error
= SYSCTL_OUT(req
, nsp
, totlen
);