2 * Copyright (c) 2000-2020 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 #include <nfs/nfs_conf.h>
72 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
73 * support for mandatory and extensible security protections. This notice
74 * is included in support of clause 2.2 (b) of the Apple Public License,
78 #include <sys/param.h>
79 #include <sys/systm.h>
81 #include <sys/ioctl.h>
82 #include <sys/signal.h>
83 #include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
84 #include <sys/kauth.h>
85 #include <sys/vnode_internal.h>
86 #include <sys/malloc.h>
87 #include <sys/kernel.h>
88 #include <sys/sysctl.h>
89 #include <sys/mount_internal.h>
90 #include <sys/kpi_mbuf.h>
91 #include <sys/socket.h>
93 #include <sys/socketvar.h>
94 #include <sys/fcntl.h>
95 #include <sys/quota.h>
97 #include <libkern/OSAtomic.h>
100 #include <sys/vmparam.h>
102 #if !defined(NO_MOUNT_PRIVATE)
103 #include <sys/filedesc.h>
104 #endif /* NO_MOUNT_PRIVATE */
107 #include <net/route.h>
108 #include <netinet/in.h>
110 #include <nfs/rpcv2.h>
111 #include <nfs/krpc.h>
112 #include <nfs/nfsproto.h>
114 #include <nfs/nfsnode.h>
115 #include <nfs/nfs_gss.h>
116 #include <nfs/nfsmount.h>
117 #include <nfs/xdr_subs.h>
118 #include <nfs/nfsm_subs.h>
119 #include <nfs/nfsdiskless.h>
120 #include <nfs/nfs_lock.h>
122 #include <security/mac_framework.h>
125 #include <pexpert/pexpert.h>
127 #define NFS_VFS_DBG(...) NFS_DBG(NFS_FAC_VFS, 7, ## __VA_ARGS__)
133 ZONE_DECLARE(nfsmnt_zone
, "NFS mount",
134 sizeof(struct nfsmount
), ZC_ZFREE_CLEARMEM
);
137 static LCK_GRP_DECLARE(nfs_global_grp
, "nfs_global");
138 static LCK_GRP_DECLARE(nfs_mount_grp
, "nfs_mount");
139 LCK_MTX_DECLARE(nfs_global_mutex
, &nfs_global_grp
);
140 uint32_t nfs_fs_attr_bitmap
[NFS_ATTR_BITMAP_LEN
];
141 uint32_t nfs_object_attr_bitmap
[NFS_ATTR_BITMAP_LEN
];
142 uint32_t nfs_getattr_bitmap
[NFS_ATTR_BITMAP_LEN
];
143 uint32_t nfs4_getattr_write_bitmap
[NFS_ATTR_BITMAP_LEN
];
144 struct nfsclientidlist nfsclientids
;
147 struct nfs_reqqhead nfs_reqq
;
148 LCK_GRP_DECLARE(nfs_request_grp
, "nfs_request");
149 LCK_MTX_DECLARE(nfs_request_mutex
, &nfs_request_grp
);
150 thread_call_t nfs_request_timer_call
;
151 int nfs_request_timer_on
;
152 u_int64_t nfs_xid
= 0;
153 u_int64_t nfs_xidwrap
= 0; /* to build a (non-wrapping) 64 bit xid */
155 thread_call_t nfs_buf_timer_call
;
158 LCK_GRP_DECLARE(nfs_open_grp
, "nfs_open");
159 uint32_t nfs_open_owner_seqnum
= 0;
160 uint32_t nfs_lock_owner_seqnum
= 0;
161 thread_call_t nfs4_callback_timer_call
;
162 int nfs4_callback_timer_on
= 0;
163 char nfs4_default_domain
[MAXPATHLEN
];
166 static LCK_GRP_DECLARE(nfsiod_lck_grp
, "nfsiod");
167 LCK_MTX_DECLARE(nfsiod_mutex
, &nfsiod_lck_grp
);
168 struct nfsiodlist nfsiodfree
, nfsiodwork
;
169 struct nfsiodmountlist nfsiodmounts
;
170 int nfsiod_thread_count
= 0;
171 int nfsiod_thread_max
= NFS_DEFASYNCTHREAD
;
172 int nfs_max_async_writes
= NFS_DEFMAXASYNCWRITES
;
174 int nfs_iosize
= NFS_IOSIZE
;
175 int nfs_access_cache_timeout
= NFS_MAXATTRTIMO
;
176 int nfs_access_delete
= 1; /* too many servers get this wrong - workaround on by default */
177 int nfs_access_dotzfs
= 1;
178 int nfs_access_for_getattr
= 0;
179 int nfs_allow_async
= 0;
180 int nfs_statfs_rate_limit
= NFS_DEFSTATFSRATELIMIT
;
181 int nfs_lockd_mounts
= 0;
182 int nfs_lockd_request_sent
= 0;
183 int nfs_idmap_ctrl
= NFS_IDMAP_CTRL_USE_IDMAP_SERVICE
;
184 int nfs_callback_port
= 0;
186 int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
187 int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
190 int mountnfs(char *, mount_t
, vfs_context_t
, vnode_t
*);
192 static int nfs_mount_diskless(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*, vfs_context_t
);
193 #if !defined(NO_MOUNT_PRIVATE)
194 static int nfs_mount_diskless_private(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*, vfs_context_t
);
195 #endif /* NO_MOUNT_PRIVATE */
197 int nfs_mount_connect(struct nfsmount
*);
198 void nfs_mount_drain_and_cleanup(struct nfsmount
*);
199 void nfs_mount_cleanup(struct nfsmount
*);
200 int nfs_mountinfo_assemble(struct nfsmount
*, struct xdrbuf
*);
201 int nfs4_mount_update_path_with_symlink(struct nfsmount
*, struct nfs_fs_path
*, uint32_t, fhandle_t
*, int *, fhandle_t
*, vfs_context_t
);
204 * NFS VFS operations.
206 int nfs_vfs_mount(mount_t
, vnode_t
, user_addr_t
, vfs_context_t
);
207 int nfs_vfs_start(mount_t
, int, vfs_context_t
);
208 int nfs_vfs_unmount(mount_t
, int, vfs_context_t
);
209 int nfs_vfs_root(mount_t
, vnode_t
*, vfs_context_t
);
210 int nfs_vfs_quotactl(mount_t
, int, uid_t
, caddr_t
, vfs_context_t
);
211 int nfs_vfs_getattr(mount_t
, struct vfs_attr
*, vfs_context_t
);
212 int nfs_vfs_sync(mount_t
, int, vfs_context_t
);
213 int nfs_vfs_vget(mount_t
, ino64_t
, vnode_t
*, vfs_context_t
);
214 int nfs_vfs_vptofh(vnode_t
, int *, unsigned char *, vfs_context_t
);
215 int nfs_vfs_fhtovp(mount_t
, int, unsigned char *, vnode_t
*, vfs_context_t
);
216 int nfs_vfs_init(struct vfsconf
*);
217 int nfs_vfs_sysctl(int *, u_int
, user_addr_t
, size_t *, user_addr_t
, size_t, vfs_context_t
);
219 const struct vfsops nfs_vfsops
= {
220 .vfs_mount
= nfs_vfs_mount
,
221 .vfs_start
= nfs_vfs_start
,
222 .vfs_unmount
= nfs_vfs_unmount
,
223 .vfs_root
= nfs_vfs_root
,
224 .vfs_quotactl
= nfs_vfs_quotactl
,
225 .vfs_getattr
= nfs_vfs_getattr
,
226 .vfs_sync
= nfs_vfs_sync
,
227 .vfs_vget
= nfs_vfs_vget
,
228 .vfs_fhtovp
= nfs_vfs_fhtovp
,
229 .vfs_vptofh
= nfs_vfs_vptofh
,
230 .vfs_init
= nfs_vfs_init
,
231 .vfs_sysctl
= nfs_vfs_sysctl
,
232 // We do not support the remaining VFS ops
237 * version-specific NFS functions
239 int nfs3_mount(struct nfsmount
*, vfs_context_t
, nfsnode_t
*);
240 int nfs4_mount(struct nfsmount
*, vfs_context_t
, nfsnode_t
*);
241 int nfs3_fsinfo(struct nfsmount
*, nfsnode_t
, vfs_context_t
);
242 int nfs3_update_statfs(struct nfsmount
*, vfs_context_t
);
243 int nfs4_update_statfs(struct nfsmount
*, vfs_context_t
);
245 #define nfs3_getquota NULL
246 #define nfs4_getquota NULL
248 int nfs3_getquota(struct nfsmount
*, vfs_context_t
, uid_t
, int, struct dqblk
*);
249 int nfs4_getquota(struct nfsmount
*, vfs_context_t
, uid_t
, int, struct dqblk
*);
252 const struct nfs_funcs nfs3_funcs
= {
253 .nf_mount
= nfs3_mount
,
254 .nf_update_statfs
= nfs3_update_statfs
,
255 .nf_getquota
= nfs3_getquota
,
256 .nf_access_rpc
= nfs3_access_rpc
,
257 .nf_getattr_rpc
= nfs3_getattr_rpc
,
258 .nf_setattr_rpc
= nfs3_setattr_rpc
,
259 .nf_read_rpc_async
= nfs3_read_rpc_async
,
260 .nf_read_rpc_async_finish
= nfs3_read_rpc_async_finish
,
261 .nf_readlink_rpc
= nfs3_readlink_rpc
,
262 .nf_write_rpc_async
= nfs3_write_rpc_async
,
263 .nf_write_rpc_async_finish
= nfs3_write_rpc_async_finish
,
264 .nf_commit_rpc
= nfs3_commit_rpc
,
265 .nf_lookup_rpc_async
= nfs3_lookup_rpc_async
,
266 .nf_lookup_rpc_async_finish
= nfs3_lookup_rpc_async_finish
,
267 .nf_remove_rpc
= nfs3_remove_rpc
,
268 .nf_rename_rpc
= nfs3_rename_rpc
,
269 .nf_setlock_rpc
= nfs3_setlock_rpc
,
270 .nf_unlock_rpc
= nfs3_unlock_rpc
,
271 .nf_getlock_rpc
= nfs3_getlock_rpc
274 const struct nfs_funcs nfs4_funcs
= {
275 .nf_mount
= nfs4_mount
,
276 .nf_update_statfs
= nfs4_update_statfs
,
277 .nf_getquota
= nfs4_getquota
,
278 .nf_access_rpc
= nfs4_access_rpc
,
279 .nf_getattr_rpc
= nfs4_getattr_rpc
,
280 .nf_setattr_rpc
= nfs4_setattr_rpc
,
281 .nf_read_rpc_async
= nfs4_read_rpc_async
,
282 .nf_read_rpc_async_finish
= nfs4_read_rpc_async_finish
,
283 .nf_readlink_rpc
= nfs4_readlink_rpc
,
284 .nf_write_rpc_async
= nfs4_write_rpc_async
,
285 .nf_write_rpc_async_finish
= nfs4_write_rpc_async_finish
,
286 .nf_commit_rpc
= nfs4_commit_rpc
,
287 .nf_lookup_rpc_async
= nfs4_lookup_rpc_async
,
288 .nf_lookup_rpc_async_finish
= nfs4_lookup_rpc_async_finish
,
289 .nf_remove_rpc
= nfs4_remove_rpc
,
290 .nf_rename_rpc
= nfs4_rename_rpc
,
291 .nf_setlock_rpc
= nfs4_setlock_rpc
,
292 .nf_unlock_rpc
= nfs4_unlock_rpc
,
293 .nf_getlock_rpc
= nfs4_getlock_rpc
298 * Called once to initialize data structures...
301 nfs_vfs_init(__unused
struct vfsconf
*vfsp
)
307 * Check to see if major data structures haven't bloated.
309 if (sizeof(struct nfsnode
) > NFS_NODEALLOC
) {
310 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC
);
311 printf("Try reducing NFS_SMALLFH\n");
313 if (sizeof(struct nfsmount
) > NFS_MNTALLOC
) {
314 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC
);
317 nfs_ticks
= (hz
* NFS_TICKINTVL
+ 500) / 1000;
322 /* init async I/O thread pool state */
323 TAILQ_INIT(&nfsiodfree
);
324 TAILQ_INIT(&nfsiodwork
);
325 TAILQ_INIT(&nfsiodmounts
);
327 /* initialize NFS request list */
328 TAILQ_INIT(&nfs_reqq
);
330 nfs_nbinit(); /* Init the nfsbuf table */
332 nfs_gss_init(); /* Init RPCSEC_GSS security */
337 NFS4_PER_FS_ATTRIBUTES(nfs_fs_attr_bitmap
);
338 NFS4_PER_OBJECT_ATTRIBUTES(nfs_object_attr_bitmap
);
339 NFS4_DEFAULT_WRITE_ATTRIBUTES(nfs4_getattr_write_bitmap
);
340 NFS4_DEFAULT_ATTRIBUTES(nfs_getattr_bitmap
);
341 for (i
= 0; i
< NFS_ATTR_BITMAP_LEN
; i
++) {
342 nfs_getattr_bitmap
[i
] &= nfs_object_attr_bitmap
[i
];
343 nfs4_getattr_write_bitmap
[i
] &= nfs_object_attr_bitmap
[i
];
345 TAILQ_INIT(&nfsclientids
);
348 /* initialize NFS timer callouts */
349 nfs_request_timer_call
= thread_call_allocate(nfs_request_timer
, NULL
);
350 nfs_buf_timer_call
= thread_call_allocate(nfs_buf_timer
, NULL
);
352 nfs4_callback_timer_call
= thread_call_allocate(nfs4_callback_timer
, NULL
);
362 nfs3_update_statfs(struct nfsmount
*nmp
, vfs_context_t ctx
)
365 int error
= 0, lockerror
, status
, nfsvers
;
367 struct nfsm_chain nmreq
, nmrep
;
370 nfsvers
= nmp
->nm_vers
;
375 if ((error
= vnode_get(NFSTOV(np
)))) {
379 nfsm_chain_null(&nmreq
);
380 nfsm_chain_null(&nmrep
);
382 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
383 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
384 nfsm_chain_build_done(error
, &nmreq
);
386 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_FSSTAT
, vfs_context_thread(ctx
),
387 vfs_context_ucred(ctx
), NULL
, R_SOFT
, &nmrep
, &xid
, &status
);
388 if (error
== ETIMEDOUT
) {
391 if ((lockerror
= nfs_node_lock(np
))) {
394 if (nfsvers
== NFS_VER3
) {
395 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
403 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
405 lck_mtx_lock(&nmp
->nm_lock
);
406 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_TOTAL
);
407 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_FREE
);
408 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_AVAIL
);
409 if (nfsvers
== NFS_VER3
) {
410 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_AVAIL
);
411 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_TOTAL
);
412 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_FREE
);
413 nmp
->nm_fsattr
.nfsa_bsize
= NFS_FABLKSIZE
;
414 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_space_total
);
415 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_space_free
);
416 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_space_avail
);
417 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_files_total
);
418 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_files_free
);
419 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_files_avail
);
422 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
); // skip tsize?
423 nfsm_chain_get_32(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_bsize
);
424 nfsm_chain_get_32(error
, &nmrep
, val
);
426 if (nmp
->nm_fsattr
.nfsa_bsize
<= 0) {
427 nmp
->nm_fsattr
.nfsa_bsize
= NFS_FABLKSIZE
;
429 nmp
->nm_fsattr
.nfsa_space_total
= (uint64_t)val
* nmp
->nm_fsattr
.nfsa_bsize
;
430 nfsm_chain_get_32(error
, &nmrep
, val
);
432 nmp
->nm_fsattr
.nfsa_space_free
= (uint64_t)val
* nmp
->nm_fsattr
.nfsa_bsize
;
433 nfsm_chain_get_32(error
, &nmrep
, val
);
435 nmp
->nm_fsattr
.nfsa_space_avail
= (uint64_t)val
* nmp
->nm_fsattr
.nfsa_bsize
;
437 lck_mtx_unlock(&nmp
->nm_lock
);
439 nfsm_chain_cleanup(&nmreq
);
440 nfsm_chain_cleanup(&nmrep
);
441 vnode_put(NFSTOV(np
));
447 nfs4_update_statfs(struct nfsmount
*nmp
, vfs_context_t ctx
)
450 int error
= 0, lockerror
, status
, nfsvers
, numops
;
452 struct nfsm_chain nmreq
, nmrep
;
453 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
454 struct nfs_vattr nvattr
;
455 struct nfsreq_secinfo_args si
;
457 nfsvers
= nmp
->nm_vers
;
462 if ((error
= vnode_get(NFSTOV(np
)))) {
466 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
467 NVATTR_INIT(&nvattr
);
468 nfsm_chain_null(&nmreq
);
469 nfsm_chain_null(&nmrep
);
473 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
474 nfsm_chain_add_compound_header(error
, &nmreq
, "statfs", nmp
->nm_minor_vers
, numops
);
476 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
477 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
479 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
480 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
481 NFS4_STATFS_ATTRIBUTES(bitmap
);
482 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
483 nfsm_chain_build_done(error
, &nmreq
);
484 nfsm_assert(error
, (numops
== 0), EPROTO
);
486 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
487 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
488 NULL
, R_SOFT
, &nmrep
, &xid
, &status
);
489 nfsm_chain_skip_tag(error
, &nmrep
);
490 nfsm_chain_get_32(error
, &nmrep
, numops
);
491 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
492 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
493 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
495 lck_mtx_lock(&nmp
->nm_lock
);
496 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, NULL
);
497 lck_mtx_unlock(&nmp
->nm_lock
);
499 if ((lockerror
= nfs_node_lock(np
))) {
503 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
508 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
510 nmp
->nm_fsattr
.nfsa_bsize
= NFS_FABLKSIZE
;
512 NVATTR_CLEANUP(&nvattr
);
513 nfsm_chain_cleanup(&nmreq
);
514 nfsm_chain_cleanup(&nmrep
);
515 vnode_put(NFSTOV(np
));
518 #endif /* CONFIG_NFS4 */
522 * Return an NFS volume name from the mntfrom name.
525 nfs_get_volname(struct mount
*mp
, char *volname
, size_t len
, __unused vfs_context_t ctx
)
527 const char *ptr
, *cptr
;
528 const char *mntfrom
= mp
->mnt_vfsstat
.f_mntfromname
;
532 mflen
= strnlen(mntfrom
, MAXPATHLEN
+ 1);
534 if (mflen
> MAXPATHLEN
|| mflen
== 0) {
535 strlcpy(volname
, "Bad volname", len
);
539 /* Move back over trailing slashes */
540 for (ptr
= &mntfrom
[mflen
- 1]; ptr
!= mntfrom
&& *ptr
== '/'; ptr
--) {
544 /* Find first character after the last slash */
546 for (size_t i
= 0; i
< mflen
; i
++) {
547 if (mntfrom
[i
] == '/') {
548 ptr
= &mntfrom
[i
+ 1];
550 /* And the first character after the first colon */
551 else if (cptr
== NULL
&& mntfrom
[i
] == ':') {
552 cptr
= &mntfrom
[i
+ 1];
557 * No slash or nothing after the last slash
558 * use everything past the first colon
560 if (ptr
== NULL
|| *ptr
== '\0') {
563 /* Otherwise use the mntfrom name */
568 mflen
= &mntfrom
[mflen
] - ptr
;
569 len
= mflen
+ 1 < len
? mflen
+ 1 : len
;
571 strlcpy(volname
, ptr
, len
);
576 * The NFS VFS_GETATTR function: "statfs"-type information is retrieved
577 * using the nf_update_statfs() function, and other attributes are cobbled
578 * together from whatever sources we can (getattr, fsinfo, pathconf).
581 nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t ctx
)
583 struct nfsmount
*nmp
;
585 int error
= 0, nfsvers
;
588 if (nfs_mount_gone(nmp
)) {
591 nfsvers
= nmp
->nm_vers
;
593 if (VFSATTR_IS_ACTIVE(fsap
, f_bsize
) ||
594 VFSATTR_IS_ACTIVE(fsap
, f_iosize
) ||
595 VFSATTR_IS_ACTIVE(fsap
, f_blocks
) ||
596 VFSATTR_IS_ACTIVE(fsap
, f_bfree
) ||
597 VFSATTR_IS_ACTIVE(fsap
, f_bavail
) ||
598 VFSATTR_IS_ACTIVE(fsap
, f_bused
) ||
599 VFSATTR_IS_ACTIVE(fsap
, f_files
) ||
600 VFSATTR_IS_ACTIVE(fsap
, f_ffree
)) {
601 int statfsrate
= nfs_statfs_rate_limit
;
605 * Are we rate-limiting statfs RPCs?
606 * (Treat values less than 1 or greater than 1,000,000 as no limit.)
608 if ((statfsrate
> 0) && (statfsrate
< 1000000)) {
613 lck_mtx_lock(&nmp
->nm_lock
);
614 stamp
= (now
.tv_sec
* statfsrate
) + (now
.tv_usec
/ (1000000 / statfsrate
));
615 if (stamp
!= nmp
->nm_fsattrstamp
) {
617 nmp
->nm_fsattrstamp
= stamp
;
621 lck_mtx_unlock(&nmp
->nm_lock
);
624 if (refresh
&& !nfs_use_cache(nmp
)) {
625 error
= nmp
->nm_funcs
->nf_update_statfs(nmp
, ctx
);
627 if ((error
== ESTALE
) || (error
== ETIMEDOUT
)) {
634 lck_mtx_lock(&nmp
->nm_lock
);
635 VFSATTR_RETURN(fsap
, f_iosize
, nfs_iosize
);
636 VFSATTR_RETURN(fsap
, f_bsize
, nmp
->nm_fsattr
.nfsa_bsize
);
637 bsize
= nmp
->nm_fsattr
.nfsa_bsize
;
638 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_TOTAL
)) {
639 VFSATTR_RETURN(fsap
, f_blocks
, nmp
->nm_fsattr
.nfsa_space_total
/ bsize
);
641 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_FREE
)) {
642 VFSATTR_RETURN(fsap
, f_bfree
, nmp
->nm_fsattr
.nfsa_space_free
/ bsize
);
644 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_AVAIL
)) {
645 VFSATTR_RETURN(fsap
, f_bavail
, nmp
->nm_fsattr
.nfsa_space_avail
/ bsize
);
647 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_TOTAL
) &&
648 NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SPACE_FREE
)) {
649 VFSATTR_RETURN(fsap
, f_bused
,
650 (nmp
->nm_fsattr
.nfsa_space_total
/ bsize
) -
651 (nmp
->nm_fsattr
.nfsa_space_free
/ bsize
));
653 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_TOTAL
)) {
654 VFSATTR_RETURN(fsap
, f_files
, nmp
->nm_fsattr
.nfsa_files_total
);
656 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_FILES_FREE
)) {
657 VFSATTR_RETURN(fsap
, f_ffree
, nmp
->nm_fsattr
.nfsa_files_free
);
659 lck_mtx_unlock(&nmp
->nm_lock
);
662 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
663 /*%%% IF fail over support is implemented we may need to take nm_lock */
664 nfs_get_volname(mp
, fsap
->f_vol_name
, MAXPATHLEN
, ctx
);
665 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
667 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)
669 u_int32_t caps
, valid
;
670 nfsnode_t np
= nmp
->nm_dnp
;
672 nfsm_assert(error
, VFSTONFS(mp
) && np
, ENXIO
);
676 lck_mtx_lock(&nmp
->nm_lock
);
679 * The capabilities[] array defines what this volume supports.
681 * The valid[] array defines which bits this code understands
682 * the meaning of (whether the volume has that capability or
683 * not). Any zero bits here means "I don't know what you're
684 * asking about" and the caller cannot tell whether that
685 * capability is present or not.
688 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SYMLINK_SUPPORT
)) {
689 valid
|= VOL_CAP_FMT_SYMBOLICLINKS
;
690 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_SYMLINK
) {
691 caps
|= VOL_CAP_FMT_SYMBOLICLINKS
;
694 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_LINK_SUPPORT
)) {
695 valid
|= VOL_CAP_FMT_HARDLINKS
;
696 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_LINK
) {
697 caps
|= VOL_CAP_FMT_HARDLINKS
;
700 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
)) {
701 valid
|= VOL_CAP_FMT_CASE_SENSITIVE
;
702 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
)) {
703 caps
|= VOL_CAP_FMT_CASE_SENSITIVE
;
706 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
)) {
707 valid
|= VOL_CAP_FMT_CASE_PRESERVING
;
708 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) {
709 caps
|= VOL_CAP_FMT_CASE_PRESERVING
;
712 /* Note: VOL_CAP_FMT_2TB_FILESIZE is actually used to test for "large file support" */
713 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
714 /* Is server's max file size at least 4GB? */
715 if (nmp
->nm_fsattr
.nfsa_maxfilesize
>= 0x100000000ULL
) {
716 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
718 } else if (nfsvers
>= NFS_VER3
) {
720 * NFSv3 and up supports 64 bits of file size.
721 * So, we'll just assume maxfilesize >= 4GB
723 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
726 if (nfsvers
>= NFS_VER4
) {
727 caps
|= VOL_CAP_FMT_HIDDEN_FILES
;
728 valid
|= VOL_CAP_FMT_HIDDEN_FILES
;
729 // VOL_CAP_FMT_OPENDENYMODES
730 // caps |= VOL_CAP_FMT_OPENDENYMODES;
731 // valid |= VOL_CAP_FMT_OPENDENYMODES;
734 // no version of nfs supports immutable files
735 caps
|= VOL_CAP_FMT_NO_IMMUTABLE_FILES
;
736 valid
|= VOL_CAP_FMT_NO_IMMUTABLE_FILES
;
738 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] =
739 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
740 // VOL_CAP_FMT_SYMBOLICLINKS |
741 // VOL_CAP_FMT_HARDLINKS |
742 // VOL_CAP_FMT_JOURNAL |
743 // VOL_CAP_FMT_JOURNAL_ACTIVE |
744 // VOL_CAP_FMT_NO_ROOT_TIMES |
745 // VOL_CAP_FMT_SPARSE_FILES |
746 // VOL_CAP_FMT_ZERO_RUNS |
747 // VOL_CAP_FMT_CASE_SENSITIVE |
748 // VOL_CAP_FMT_CASE_PRESERVING |
749 // VOL_CAP_FMT_FAST_STATFS |
750 // VOL_CAP_FMT_2TB_FILESIZE |
751 // VOL_CAP_FMT_OPENDENYMODES |
752 // VOL_CAP_FMT_HIDDEN_FILES |
754 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] =
755 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
756 // VOL_CAP_FMT_SYMBOLICLINKS |
757 // VOL_CAP_FMT_HARDLINKS |
758 // VOL_CAP_FMT_JOURNAL |
759 // VOL_CAP_FMT_JOURNAL_ACTIVE |
760 // VOL_CAP_FMT_NO_ROOT_TIMES |
761 // VOL_CAP_FMT_SPARSE_FILES |
762 // VOL_CAP_FMT_ZERO_RUNS |
763 // VOL_CAP_FMT_CASE_SENSITIVE |
764 // VOL_CAP_FMT_CASE_PRESERVING |
765 VOL_CAP_FMT_FAST_STATFS
|
766 VOL_CAP_FMT_2TB_FILESIZE
|
767 // VOL_CAP_FMT_OPENDENYMODES |
768 // VOL_CAP_FMT_HIDDEN_FILES |
772 * We don't support most of the interfaces.
774 * We MAY support locking, but we don't have any easy way of
775 * probing. We can tell if there's no lockd running or if
776 * locks have been disabled for a mount, so we can definitely
777 * answer NO in that case. Any attempt to send a request to
778 * lockd to test for locking support may cause the lazily-
779 * launched locking daemons to be started unnecessarily. So
780 * we avoid that. However, we do record if we ever successfully
781 * perform a lock operation on a mount point, so if it looks
782 * like lock ops have worked, we do report that we support them.
786 if (nfsvers
>= NFS_VER4
) {
787 caps
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
788 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
789 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
) {
790 caps
|= VOL_CAP_INT_EXTENDED_SECURITY
;
792 valid
|= VOL_CAP_INT_EXTENDED_SECURITY
;
793 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
) {
794 caps
|= VOL_CAP_INT_EXTENDED_ATTR
;
796 valid
|= VOL_CAP_INT_EXTENDED_ATTR
;
798 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
) {
799 caps
|= VOL_CAP_INT_NAMEDSTREAMS
;
801 valid
|= VOL_CAP_INT_NAMEDSTREAMS
;
805 if (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
) {
806 /* locks disabled on this mount, so they definitely won't work */
807 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
808 } else if (nmp
->nm_state
& NFSSTA_LOCKSWORK
) {
809 caps
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
810 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
812 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] =
813 // VOL_CAP_INT_SEARCHFS |
814 // VOL_CAP_INT_ATTRLIST |
815 // VOL_CAP_INT_NFSEXPORT |
816 // VOL_CAP_INT_READDIRATTR |
817 // VOL_CAP_INT_EXCHANGEDATA |
818 // VOL_CAP_INT_COPYFILE |
819 // VOL_CAP_INT_ALLOCATE |
820 // VOL_CAP_INT_VOL_RENAME |
821 // VOL_CAP_INT_ADVLOCK |
822 // VOL_CAP_INT_FLOCK |
823 // VOL_CAP_INT_EXTENDED_SECURITY |
824 // VOL_CAP_INT_USERACCESS |
825 // VOL_CAP_INT_MANLOCK |
826 // VOL_CAP_INT_NAMEDSTREAMS |
827 // VOL_CAP_INT_EXTENDED_ATTR |
828 VOL_CAP_INT_REMOTE_EVENT
|
830 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] =
831 VOL_CAP_INT_SEARCHFS
|
832 VOL_CAP_INT_ATTRLIST
|
833 VOL_CAP_INT_NFSEXPORT
|
834 VOL_CAP_INT_READDIRATTR
|
835 VOL_CAP_INT_EXCHANGEDATA
|
836 VOL_CAP_INT_COPYFILE
|
837 VOL_CAP_INT_ALLOCATE
|
838 VOL_CAP_INT_VOL_RENAME
|
839 // VOL_CAP_INT_ADVLOCK |
840 // VOL_CAP_INT_FLOCK |
841 // VOL_CAP_INT_EXTENDED_SECURITY |
842 // VOL_CAP_INT_USERACCESS |
843 // VOL_CAP_INT_MANLOCK |
844 // VOL_CAP_INT_NAMEDSTREAMS |
845 // VOL_CAP_INT_EXTENDED_ATTR |
846 VOL_CAP_INT_REMOTE_EVENT
|
849 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
850 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
852 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
853 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
855 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
856 lck_mtx_unlock(&nmp
->nm_lock
);
859 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
860 fsap
->f_attributes
.validattr
.commonattr
= 0;
861 fsap
->f_attributes
.validattr
.volattr
=
862 ATTR_VOL_NAME
| ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
863 fsap
->f_attributes
.validattr
.dirattr
= 0;
864 fsap
->f_attributes
.validattr
.fileattr
= 0;
865 fsap
->f_attributes
.validattr
.forkattr
= 0;
867 fsap
->f_attributes
.nativeattr
.commonattr
= 0;
868 fsap
->f_attributes
.nativeattr
.volattr
=
869 ATTR_VOL_NAME
| ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
870 fsap
->f_attributes
.nativeattr
.dirattr
= 0;
871 fsap
->f_attributes
.nativeattr
.fileattr
= 0;
872 fsap
->f_attributes
.nativeattr
.forkattr
= 0;
874 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
881 * nfs version 3 fsinfo rpc call
884 nfs3_fsinfo(struct nfsmount
*nmp
, nfsnode_t np
, vfs_context_t ctx
)
886 int error
= 0, lockerror
, status
, nmlocked
= 0;
888 uint32_t val
, prefsize
, maxsize
;
889 struct nfsm_chain nmreq
, nmrep
;
891 nfsm_chain_null(&nmreq
);
892 nfsm_chain_null(&nmrep
);
894 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nmp
->nm_vers
));
895 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
896 nfsm_chain_build_done(error
, &nmreq
);
898 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_FSINFO
, ctx
, NULL
, &nmrep
, &xid
, &status
);
899 if ((lockerror
= nfs_node_lock(np
))) {
902 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
911 lck_mtx_lock(&nmp
->nm_lock
);
914 nfsm_chain_get_32(error
, &nmrep
, maxsize
);
915 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
917 nmp
->nm_fsattr
.nfsa_maxread
= maxsize
;
918 if (prefsize
< nmp
->nm_rsize
) {
919 nmp
->nm_rsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
920 ~(NFS_FABLKSIZE
- 1);
922 if ((maxsize
> 0) && (maxsize
< nmp
->nm_rsize
)) {
923 nmp
->nm_rsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
924 if (nmp
->nm_rsize
== 0) {
925 nmp
->nm_rsize
= maxsize
;
928 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
); // skip rtmult
930 nfsm_chain_get_32(error
, &nmrep
, maxsize
);
931 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
933 nmp
->nm_fsattr
.nfsa_maxwrite
= maxsize
;
934 if (prefsize
< nmp
->nm_wsize
) {
935 nmp
->nm_wsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
936 ~(NFS_FABLKSIZE
- 1);
938 if ((maxsize
> 0) && (maxsize
< nmp
->nm_wsize
)) {
939 nmp
->nm_wsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
940 if (nmp
->nm_wsize
== 0) {
941 nmp
->nm_wsize
= maxsize
;
944 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
); // skip wtmult
946 nfsm_chain_get_32(error
, &nmrep
, prefsize
);
948 if ((prefsize
> 0) && (prefsize
< nmp
->nm_readdirsize
)) {
949 nmp
->nm_readdirsize
= prefsize
;
951 if ((nmp
->nm_fsattr
.nfsa_maxread
> 0) &&
952 (nmp
->nm_fsattr
.nfsa_maxread
< nmp
->nm_readdirsize
)) {
953 nmp
->nm_readdirsize
= nmp
->nm_fsattr
.nfsa_maxread
;
956 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_fsattr
.nfsa_maxfilesize
);
958 nfsm_chain_adv(error
, &nmrep
, 2 * NFSX_UNSIGNED
); // skip time_delta
960 /* convert FS properties to our own flags */
961 nfsm_chain_get_32(error
, &nmrep
, val
);
963 if (val
& NFSV3FSINFO_LINK
) {
964 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_LINK
;
966 if (val
& NFSV3FSINFO_SYMLINK
) {
967 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_SYMLINK
;
969 if (val
& NFSV3FSINFO_HOMOGENEOUS
) {
970 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_HOMOGENEOUS
;
972 if (val
& NFSV3FSINFO_CANSETTIME
) {
973 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_SET_TIME
;
975 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
976 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXREAD
);
977 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXWRITE
);
978 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
);
979 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_LINK_SUPPORT
);
980 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_SYMLINK_SUPPORT
);
981 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_HOMOGENEOUS
);
982 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CANSETTIME
);
985 lck_mtx_unlock(&nmp
->nm_lock
);
987 nfsm_chain_cleanup(&nmreq
);
988 nfsm_chain_cleanup(&nmrep
);
993 * Mount a remote root fs via. nfs. This depends on the info in the
994 * nfs_diskless structure that has been filled in properly by some primary
996 * It goes something like this:
997 * - do enough of "ifconfig" by calling ifioctl() so that the system
998 * can talk to the server
999 * - If nfs_diskless.mygateway is filled in, use that address as
1000 * a default gateway.
1001 * - hand craft the swap nfs vnode hanging off a fake mount point
1002 * if swdevt[0].sw_dev == NODEV
1003 * - build the rootfs mount point and call mountnfs() to do the rest.
1009 struct nfs_diskless nd
;
1014 #if !defined(NO_MOUNT_PRIVATE)
1015 mount_t mppriv
= NULL
;
1016 vnode_t vppriv
= NULL
;
1017 #endif /* NO_MOUNT_PRIVATE */
1021 * Call nfs_boot_init() to fill in the nfs_diskless struct.
1022 * Note: networking must already have been configured before
1025 bzero((caddr_t
) &nd
, sizeof(nd
));
1026 error
= nfs_boot_init(&nd
);
1028 panic("nfs_boot_init: unable to initialize NFS root system information, "
1029 "error %d, check configuration: %s\n", error
, PE_boot_args());
1033 * Try NFSv3 first, then fallback to NFSv2.
1034 * Likewise, try TCP first, then fall back to UDP.
1037 sotype
= SOCK_STREAM
;
1040 error
= nfs_boot_getfh(&nd
, v3
, sotype
);
1042 if (error
== EHOSTDOWN
|| error
== EHOSTUNREACH
) {
1043 if (nd
.nd_root
.ndm_mntfrom
) {
1044 NFS_ZFREE(ZV_NAMEI
, nd
.nd_root
.ndm_mntfrom
);
1046 if (nd
.nd_root
.ndm_path
) {
1047 NFS_ZFREE(ZV_NAMEI
, nd
.nd_root
.ndm_path
);
1049 if (nd
.nd_private
.ndm_mntfrom
) {
1050 NFS_ZFREE(ZV_NAMEI
, nd
.nd_private
.ndm_mntfrom
);
1052 if (nd
.nd_private
.ndm_path
) {
1053 NFS_ZFREE(ZV_NAMEI
, nd
.nd_private
.ndm_path
);
1058 if (sotype
== SOCK_STREAM
) {
1059 printf("NFS mount (v3,TCP) failed with error %d, trying UDP...\n", error
);
1060 sotype
= SOCK_DGRAM
;
1063 printf("NFS mount (v3,UDP) failed with error %d, trying v2...\n", error
);
1065 sotype
= SOCK_STREAM
;
1067 } else if (sotype
== SOCK_STREAM
) {
1068 printf("NFS mount (v2,TCP) failed with error %d, trying UDP...\n", error
);
1069 sotype
= SOCK_DGRAM
;
1072 printf("NFS mount (v2,UDP) failed with error %d, giving up...\n", error
);
1076 panic("NFS mount failed: NFS server mountd not responding, check server configuration: %s", PE_boot_args());
1079 panic("NFS mount failed: NFS server refused mount, check server configuration: %s", PE_boot_args());
1081 panic("NFS mount failed with error %d, check configuration: %s", error
, PE_boot_args());
1085 ctx
= vfs_context_kernel();
1088 * Create the root mount point.
1090 #if !defined(NO_MOUNT_PRIVATE)
1092 //PWC hack until we have a real "mount" tool to remount root rw
1094 int flags
= MNT_ROOTFS
| MNT_RDONLY
;
1095 PE_parse_boot_argn("-rwroot_hack", &rw_root
, sizeof(rw_root
));
1098 kprintf("-rwroot_hack in effect: mounting root fs read/write\n");
1101 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", flags
, &vp
, &mp
, ctx
)))
1103 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_ROOTFS
, &vp
, &mp
, ctx
)))
1104 #endif /* NO_MOUNT_PRIVATE */
1107 if (sotype
== SOCK_STREAM
) {
1108 printf("NFS root mount (v3,TCP) failed with %d, trying UDP...\n", error
);
1109 sotype
= SOCK_DGRAM
;
1112 printf("NFS root mount (v3,UDP) failed with %d, trying v2...\n", error
);
1114 sotype
= SOCK_STREAM
;
1116 } else if (sotype
== SOCK_STREAM
) {
1117 printf("NFS root mount (v2,TCP) failed with %d, trying UDP...\n", error
);
1118 sotype
= SOCK_DGRAM
;
1121 printf("NFS root mount (v2,UDP) failed with error %d, giving up...\n", error
);
1123 panic("NFS root mount failed with error %d, check configuration: %s\n", error
, PE_boot_args());
1126 printf("root on %s\n", nd
.nd_root
.ndm_mntfrom
);
1132 #if !defined(NO_MOUNT_PRIVATE)
1133 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
1134 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
1135 0, &vppriv
, &mppriv
, ctx
);
1137 panic("NFS /private mount failed with error %d, check configuration: %s\n", error
, PE_boot_args());
1139 printf("private on %s\n", nd
.nd_private
.ndm_mntfrom
);
1142 mount_list_add(mppriv
);
1145 #endif /* NO_MOUNT_PRIVATE */
1147 if (nd
.nd_root
.ndm_mntfrom
) {
1148 NFS_ZFREE(ZV_NAMEI
, nd
.nd_root
.ndm_mntfrom
);
1150 if (nd
.nd_root
.ndm_path
) {
1151 NFS_ZFREE(ZV_NAMEI
, nd
.nd_root
.ndm_path
);
1153 if (nd
.nd_private
.ndm_mntfrom
) {
1154 NFS_ZFREE(ZV_NAMEI
, nd
.nd_private
.ndm_mntfrom
);
1156 if (nd
.nd_private
.ndm_path
) {
1157 NFS_ZFREE(ZV_NAMEI
, nd
.nd_private
.ndm_path
);
1160 /* Get root attributes (for the time). */
1161 error
= nfs_getattr(VTONFS(vp
), NULL
, ctx
, NGA_UNCACHED
);
1163 panic("NFS mount: failed to get attributes for root directory, error %d, check server", error
);
1169 * Internal version of mount system call for diskless setup.
1173 struct nfs_dlmount
*ndmntp
,
1174 const char *mntname
,
1181 int error
, numcomps
;
1182 char *xdrbuf
, *p
, *cp
, *frompath
, *endserverp
;
1183 char uaddr
[MAX_IPv4_STR_LEN
];
1185 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1186 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
];
1187 uint32_t mflags
[NFS_MFLAG_BITMAP_LEN
];
1188 uint64_t argslength_offset
, attrslength_offset
, end_offset
;
1190 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_mntfrom
, &mp
))) {
1191 printf("nfs_mount_diskless: NFS not configured\n");
1195 mp
->mnt_flag
|= mntflag
;
1196 if (!(mntflag
& MNT_RDONLY
)) {
1197 mp
->mnt_flag
&= ~MNT_RDONLY
;
1200 /* find the server-side path being mounted */
1201 frompath
= ndmntp
->ndm_mntfrom
;
1202 if (*frompath
== '[') { /* skip IPv6 literal address */
1203 while (*frompath
&& (*frompath
!= ']')) {
1206 if (*frompath
== ']') {
1210 while (*frompath
&& (*frompath
!= ':')) {
1213 endserverp
= frompath
;
1214 while (*frompath
&& (*frompath
== ':')) {
1217 /* count fs location path components */
1219 while (*p
&& (*p
== '/')) {
1225 while (*p
&& (*p
!= '/')) {
1228 while (*p
&& (*p
== '/')) {
1233 /* convert address to universal address string */
1234 if (inet_ntop(AF_INET
, &ndmntp
->ndm_saddr
.sin_addr
, uaddr
, sizeof(uaddr
)) != uaddr
) {
1235 printf("nfs_mount_diskless: bad address\n");
1239 /* prepare mount attributes */
1240 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1241 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1242 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1243 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1244 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1245 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1246 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1248 /* prepare mount flags */
1249 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1250 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1251 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1252 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1254 /* build xdr buffer */
1255 xb_init_buffer(&xb
, NULL
, 0);
1256 xb_add_32(error
, &xb
, NFS_ARGSVERSION_XDR
);
1257 argslength_offset
= xb_offset(&xb
);
1258 xb_add_32(error
, &xb
, 0); // args length
1259 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1260 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1261 attrslength_offset
= xb_offset(&xb
);
1262 xb_add_32(error
, &xb
, 0); // attrs length
1263 xb_add_32(error
, &xb
, ndmntp
->ndm_nfsv3
? 3 : 2); // NFS version
1264 xb_add_string(error
, &xb
, ((ndmntp
->ndm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1265 xb_add_32(error
, &xb
, ntohs(ndmntp
->ndm_saddr
.sin_port
)); // NFS port
1266 xb_add_fh(error
, &xb
, &ndmntp
->ndm_fh
[0], ndmntp
->ndm_fhlen
);
1268 xb_add_32(error
, &xb
, 1); /* fs location count */
1269 xb_add_32(error
, &xb
, 1); /* server count */
1270 xb_add_string(error
, &xb
, ndmntp
->ndm_mntfrom
, (endserverp
- ndmntp
->ndm_mntfrom
)); /* server name */
1271 xb_add_32(error
, &xb
, 1); /* address count */
1272 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1273 xb_add_32(error
, &xb
, 0); /* empty server info */
1274 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1276 while (*p
&& (*p
== '/')) {
1281 while (*p
&& (*p
!= '/')) {
1284 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
1288 while (*p
&& (*p
== '/')) {
1292 xb_add_32(error
, &xb
, 0); /* empty fsl info */
1293 xb_add_32(error
, &xb
, mntflag
); /* MNT flags */
1294 xb_build_done(error
, &xb
);
1296 /* update opaque counts */
1297 end_offset
= xb_offset(&xb
);
1299 error
= xb_seek(&xb
, argslength_offset
);
1300 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
1303 error
= xb_seek(&xb
, attrslength_offset
);
1304 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
1307 printf("nfs_mount_diskless: error %d assembling mount args\n", error
);
1311 /* grab the assembled buffer */
1312 xdrbuf
= xb_buffer_base(&xb
);
1313 xb
.xb_flags
&= ~XB_CLEANUP
;
1316 if ((error
= mountnfs(xdrbuf
, mp
, ctx
, vpp
))) {
1317 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
1318 // XXX vfs_rootmountfailed(mp);
1320 mp
->mnt_vtable
->vfc_refcount
--;
1321 mount_list_unlock();
1323 mount_lock_destroy(mp
);
1325 mac_mount_label_destroy(mp
);
1327 NFS_ZFREE(mount_zone
, mp
);
1335 #if !defined(NO_MOUNT_PRIVATE)
1337 * Internal version of mount system call to mount "/private"
1338 * separately in diskless setup
1341 nfs_mount_diskless_private(
1342 struct nfs_dlmount
*ndmntp
,
1343 const char *mntname
,
1350 int error
, numcomps
;
1352 struct vfstable
*vfsp
;
1353 struct nameidata nd
;
1355 char *xdrbuf
= NULL
, *p
, *cp
, *frompath
, *endserverp
;
1356 char uaddr
[MAX_IPv4_STR_LEN
];
1358 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1359 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
], mflags
[NFS_MFLAG_BITMAP_LEN
];
1360 uint64_t argslength_offset
, attrslength_offset
, end_offset
;
1362 procp
= current_proc(); /* XXX */
1363 xb_init(&xb
, XDRBUF_NONE
);
1367 * mimic main()!. Temporarily set up rootvnode and other stuff so
1368 * that namei works. Need to undo this because main() does it, too
1370 struct filedesc
*fdp
; /* pointer to file descriptor state */
1372 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
1374 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
1375 if (VFS_ROOT(mountlist
.tqh_first
, &rootvnode
, NULL
)) {
1376 panic("cannot find root vnode");
1378 error
= vnode_ref(rootvnode
);
1380 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
1383 fdp
->fd_cdir
= rootvnode
;
1384 fdp
->fd_rdir
= NULL
;
1388 * Get vnode to be covered
1390 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
1391 CAST_USER_ADDR_T(mntname
), ctx
);
1394 /* undo vnode_ref() in mimic main()! */
1395 vnode_rele(rootvnode
);
1398 printf("nfs_mountroot: private namei failed!\n");
1404 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
)) ||
1405 (error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
1409 if (vnode_vtype(vp
) != VDIR
) {
1414 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
1415 if (!strncmp(vfsp
->vfc_name
, "nfs", sizeof(vfsp
->vfc_name
))) {
1420 printf("nfs_mountroot: private NFS not configured\n");
1425 if (vnode_mountedhere(vp
) != NULL
) {
1432 * Allocate and initialize the filesystem.
1434 mp
= zalloc_flags(mount_zone
, Z_WAITOK
| Z_ZERO
);
1435 /* Initialize the default IO constraints */
1436 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
1437 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
1438 mp
->mnt_ioflags
= 0;
1439 mp
->mnt_realrootvp
= NULLVP
;
1440 mp
->mnt_authcache_ttl
= 0; /* Allways go to our lookup */
1442 mount_lock_init(mp
);
1443 TAILQ_INIT(&mp
->mnt_vnodelist
);
1444 TAILQ_INIT(&mp
->mnt_workerqueue
);
1445 TAILQ_INIT(&mp
->mnt_newvnodes
);
1446 (void)vfs_busy(mp
, LK_NOWAIT
);
1447 TAILQ_INIT(&mp
->mnt_vnodelist
);
1449 vfsp
->vfc_refcount
++;
1450 mount_list_unlock();
1451 mp
->mnt_vtable
= vfsp
;
1452 mp
->mnt_op
= vfsp
->vfc_vfsops
;
1453 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
1454 mp
->mnt_flag
= mntflag
;
1455 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
1456 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
- 1);
1457 vp
->v_mountedhere
= mp
;
1458 mp
->mnt_vnodecovered
= vp
;
1460 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(kauth_cred_get());
1461 (void) copystr(mntname
, mp
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
- 1, 0);
1462 (void) copystr(ndmntp
->ndm_mntfrom
, mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
- 1, 0);
1464 mac_mount_label_init(mp
);
1465 mac_mount_label_associate(ctx
, mp
);
1468 /* find the server-side path being mounted */
1469 frompath
= ndmntp
->ndm_mntfrom
;
1470 if (*frompath
== '[') { /* skip IPv6 literal address */
1471 while (*frompath
&& (*frompath
!= ']')) {
1474 if (*frompath
== ']') {
1478 while (*frompath
&& (*frompath
!= ':')) {
1481 endserverp
= frompath
;
1482 while (*frompath
&& (*frompath
== ':')) {
1485 /* count fs location path components */
1487 while (*p
&& (*p
== '/')) {
1493 while (*p
&& (*p
!= '/')) {
1496 while (*p
&& (*p
== '/')) {
1501 /* convert address to universal address string */
1502 if (inet_ntop(AF_INET
, &ndmntp
->ndm_saddr
.sin_addr
, uaddr
, sizeof(uaddr
)) != uaddr
) {
1503 printf("nfs_mountroot: bad address\n");
1508 /* prepare mount attributes */
1509 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1510 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1511 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1512 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1513 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1514 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1515 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1517 /* prepare mount flags */
1518 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1519 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1520 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1521 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1523 /* build xdr buffer */
1524 xb_init_buffer(&xb
, NULL
, 0);
1525 xb_add_32(error
, &xb
, NFS_ARGSVERSION_XDR
);
1526 argslength_offset
= xb_offset(&xb
);
1527 xb_add_32(error
, &xb
, 0); // args length
1528 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1529 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1530 attrslength_offset
= xb_offset(&xb
);
1531 xb_add_32(error
, &xb
, 0); // attrs length
1532 xb_add_32(error
, &xb
, ndmntp
->ndm_nfsv3
? 3 : 2); // NFS version
1533 xb_add_string(error
, &xb
, ((ndmntp
->ndm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1534 xb_add_32(error
, &xb
, ntohs(ndmntp
->ndm_saddr
.sin_port
)); // NFS port
1535 xb_add_fh(error
, &xb
, &ndmntp
->ndm_fh
[0], ndmntp
->ndm_fhlen
);
1537 xb_add_32(error
, &xb
, 1); /* fs location count */
1538 xb_add_32(error
, &xb
, 1); /* server count */
1539 xb_add_string(error
, &xb
, ndmntp
->ndm_mntfrom
, (endserverp
- ndmntp
->ndm_mntfrom
)); /* server name */
1540 xb_add_32(error
, &xb
, 1); /* address count */
1541 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1542 xb_add_32(error
, &xb
, 0); /* empty server info */
1543 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1545 while (*p
&& (*p
== '/')) {
1550 while (*p
&& (*p
!= '/')) {
1553 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
1557 while (*p
&& (*p
== '/')) {
1561 xb_add_32(error
, &xb
, 0); /* empty fsl info */
1562 xb_add_32(error
, &xb
, mntflag
); /* MNT flags */
1563 xb_build_done(error
, &xb
);
1565 /* update opaque counts */
1566 end_offset
= xb_offset(&xb
);
1568 error
= xb_seek(&xb
, argslength_offset
);
1569 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
1572 error
= xb_seek(&xb
, attrslength_offset
);
1573 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
1576 printf("nfs_mountroot: error %d assembling mount args\n", error
);
1579 /* grab the assembled buffer */
1580 xdrbuf
= xb_buffer_base(&xb
);
1581 xb
.xb_flags
&= ~XB_CLEANUP
;
1584 if ((error
= mountnfs(xdrbuf
, mp
, ctx
, &vp
))) {
1585 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
1586 vnode_put(mp
->mnt_vnodecovered
);
1588 vfsp
->vfc_refcount
--;
1589 mount_list_unlock();
1591 mount_lock_destroy(mp
);
1593 mac_mount_label_destroy(mp
);
1595 NFS_ZFREE(mount_zone
, mp
);
1605 #endif /* NO_MOUNT_PRIVATE */
1610 * Convert old style NFS mount args to XDR.
1613 nfs_convert_old_nfs_args(mount_t mp
, user_addr_t data
, vfs_context_t ctx
, int argsversion
, int inkernel
, char **xdrbufp
)
1615 int error
= 0, args64bit
, argsize
, numcomps
;
1616 struct user_nfs_args args
;
1617 struct nfs_args tempargs
;
1620 u_char nfh
[NFS4_FHSIZE
];
1621 char *mntfrom
, *endserverp
, *frompath
, *p
, *cp
;
1622 struct sockaddr_storage ss
;
1623 void *sinaddr
= NULL
;
1624 char uaddr
[MAX_IPv6_STR_LEN
];
1625 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
1626 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
], mflags
[NFS_MFLAG_BITMAP_LEN
];
1627 uint32_t nfsvers
, nfslockmode
= 0;
1628 size_t argslength_offset
, attrslength_offset
, end_offset
;
1633 /* allocate a temporary buffer for mntfrom */
1634 mntfrom
= zalloc(ZV_NAMEI
);
1636 args64bit
= (inkernel
|| vfs_context_is64bit(ctx
));
1637 argsp
= args64bit
? (void*)&args
: (void*)&tempargs
;
1639 argsize
= args64bit
? sizeof(args
) : sizeof(tempargs
);
1640 switch (argsversion
) {
1642 argsize
-= NFS_ARGSVERSION4_INCSIZE
;
1645 argsize
-= NFS_ARGSVERSION5_INCSIZE
;
1648 argsize
-= NFS_ARGSVERSION6_INCSIZE
;
1653 error
= EPROGMISMATCH
;
1657 /* read in the structure */
1659 bcopy(CAST_DOWN(void *, data
), argsp
, argsize
);
1661 error
= copyin(data
, argsp
, argsize
);
1666 args
.addrlen
= tempargs
.addrlen
;
1667 args
.sotype
= tempargs
.sotype
;
1668 args
.proto
= tempargs
.proto
;
1669 args
.fhsize
= tempargs
.fhsize
;
1670 args
.flags
= tempargs
.flags
;
1671 args
.wsize
= tempargs
.wsize
;
1672 args
.rsize
= tempargs
.rsize
;
1673 args
.readdirsize
= tempargs
.readdirsize
;
1674 args
.timeo
= tempargs
.timeo
;
1675 args
.retrans
= tempargs
.retrans
;
1676 args
.maxgrouplist
= tempargs
.maxgrouplist
;
1677 args
.readahead
= tempargs
.readahead
;
1678 args
.leaseterm
= tempargs
.leaseterm
;
1679 args
.deadthresh
= tempargs
.deadthresh
;
1680 args
.addr
= CAST_USER_ADDR_T(tempargs
.addr
);
1681 args
.fh
= CAST_USER_ADDR_T(tempargs
.fh
);
1682 args
.hostname
= CAST_USER_ADDR_T(tempargs
.hostname
);
1683 if (args
.version
>= 4) {
1684 args
.acregmin
= tempargs
.acregmin
;
1685 args
.acregmax
= tempargs
.acregmax
;
1686 args
.acdirmin
= tempargs
.acdirmin
;
1687 args
.acdirmax
= tempargs
.acdirmax
;
1689 if (args
.version
>= 5) {
1690 args
.auth
= tempargs
.auth
;
1692 if (args
.version
>= 6) {
1693 args
.deadtimeout
= tempargs
.deadtimeout
;
1697 if ((args
.fhsize
< 0) || (args
.fhsize
> NFS4_FHSIZE
)) {
1701 if (args
.fhsize
> 0) {
1703 bcopy(CAST_DOWN(void *, args
.fh
), (caddr_t
)nfh
, args
.fhsize
);
1705 error
= copyin(args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
1711 error
= copystr(CAST_DOWN(void *, args
.hostname
), mntfrom
, MAXPATHLEN
- 1, &len
);
1713 error
= copyinstr(args
.hostname
, mntfrom
, MAXPATHLEN
- 1, &len
);
1716 bzero(&mntfrom
[len
], MAXPATHLEN
- len
);
1718 /* find the server-side path being mounted */
1720 if (*frompath
== '[') { /* skip IPv6 literal address */
1721 while (*frompath
&& (*frompath
!= ']')) {
1724 if (*frompath
== ']') {
1728 while (*frompath
&& (*frompath
!= ':')) {
1731 endserverp
= frompath
;
1732 while (*frompath
&& (*frompath
== ':')) {
1735 /* count fs location path components */
1737 while (*p
&& (*p
== '/')) {
1743 while (*p
&& (*p
!= '/')) {
1746 while (*p
&& (*p
== '/')) {
1751 /* copy socket address */
1753 bcopy(CAST_DOWN(void *, args
.addr
), &ss
, args
.addrlen
);
1755 if (args
.addrlen
> sizeof(struct sockaddr_storage
)) {
1758 error
= copyin(args
.addr
, &ss
, args
.addrlen
);
1762 ss
.ss_len
= args
.addrlen
;
1764 /* convert address to universal address string */
1765 if (ss
.ss_family
== AF_INET
) {
1766 if (ss
.ss_len
!= sizeof(struct sockaddr_in
)) {
1769 sinaddr
= &((struct sockaddr_in
*)&ss
)->sin_addr
;
1771 } else if (ss
.ss_family
== AF_INET6
) {
1772 if (ss
.ss_len
!= sizeof(struct sockaddr_in6
)) {
1775 sinaddr
= &((struct sockaddr_in6
*)&ss
)->sin6_addr
;
1782 if (!sinaddr
|| (inet_ntop(ss
.ss_family
, sinaddr
, uaddr
, sizeof(uaddr
)) != uaddr
)) {
1787 /* prepare mount flags */
1788 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
1789 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
1790 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_SOFT
);
1791 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_INTR
);
1792 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
1793 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCONNECT
);
1794 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_DUMBTIMER
);
1795 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_CALLUMNT
);
1796 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RDIRPLUS
);
1797 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NONEGNAMECACHE
);
1798 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MUTEJUKEBOX
);
1799 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOQUOTA
);
1800 if (args
.flags
& NFSMNT_SOFT
) {
1801 NFS_BITMAP_SET(mflags
, NFS_MFLAG_SOFT
);
1803 if (args
.flags
& NFSMNT_INT
) {
1804 NFS_BITMAP_SET(mflags
, NFS_MFLAG_INTR
);
1806 if (args
.flags
& NFSMNT_RESVPORT
) {
1807 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
1809 if (args
.flags
& NFSMNT_NOCONN
) {
1810 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCONNECT
);
1812 if (args
.flags
& NFSMNT_DUMBTIMR
) {
1813 NFS_BITMAP_SET(mflags
, NFS_MFLAG_DUMBTIMER
);
1815 if (args
.flags
& NFSMNT_CALLUMNT
) {
1816 NFS_BITMAP_SET(mflags
, NFS_MFLAG_CALLUMNT
);
1818 if (args
.flags
& NFSMNT_RDIRPLUS
) {
1819 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RDIRPLUS
);
1821 if (args
.flags
& NFSMNT_NONEGNAMECACHE
) {
1822 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NONEGNAMECACHE
);
1824 if (args
.flags
& NFSMNT_MUTEJUKEBOX
) {
1825 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MUTEJUKEBOX
);
1827 if (args
.flags
& NFSMNT_NOQUOTA
) {
1828 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOQUOTA
);
1831 /* prepare mount attributes */
1832 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
1833 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FLAGS
);
1834 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
1835 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
1836 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
1837 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
1838 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
1839 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
1840 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFROM
);
1841 if (args
.flags
& NFSMNT_NFSV4
) {
1843 } else if (args
.flags
& NFSMNT_NFSV3
) {
1848 if ((args
.flags
& NFSMNT_RSIZE
) && (args
.rsize
> 0)) {
1849 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READ_SIZE
);
1851 if ((args
.flags
& NFSMNT_WSIZE
) && (args
.wsize
> 0)) {
1852 NFS_BITMAP_SET(mattrs
, NFS_MATTR_WRITE_SIZE
);
1854 if ((args
.flags
& NFSMNT_TIMEO
) && (args
.timeo
> 0)) {
1855 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
);
1857 if ((args
.flags
& NFSMNT_RETRANS
) && (args
.retrans
> 0)) {
1858 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
);
1860 if ((args
.flags
& NFSMNT_MAXGRPS
) && (args
.maxgrouplist
> 0)) {
1861 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
);
1863 if ((args
.flags
& NFSMNT_READAHEAD
) && (args
.readahead
> 0)) {
1864 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READAHEAD
);
1866 if ((args
.flags
& NFSMNT_READDIRSIZE
) && (args
.readdirsize
> 0)) {
1867 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READDIR_SIZE
);
1869 if ((args
.flags
& NFSMNT_NOLOCKS
) ||
1870 (args
.flags
& NFSMNT_LOCALLOCKS
)) {
1871 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCK_MODE
);
1872 if (args
.flags
& NFSMNT_NOLOCKS
) {
1873 nfslockmode
= NFS_LOCK_MODE_DISABLED
;
1874 } else if (args
.flags
& NFSMNT_LOCALLOCKS
) {
1875 nfslockmode
= NFS_LOCK_MODE_LOCAL
;
1877 nfslockmode
= NFS_LOCK_MODE_ENABLED
;
1880 if (args
.version
>= 4) {
1881 if ((args
.flags
& NFSMNT_ACREGMIN
) && (args
.acregmin
> 0)) {
1882 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
);
1884 if ((args
.flags
& NFSMNT_ACREGMAX
) && (args
.acregmax
> 0)) {
1885 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
);
1887 if ((args
.flags
& NFSMNT_ACDIRMIN
) && (args
.acdirmin
> 0)) {
1888 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
);
1890 if ((args
.flags
& NFSMNT_ACDIRMAX
) && (args
.acdirmax
> 0)) {
1891 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
);
1894 if (args
.version
>= 5) {
1895 if ((args
.flags
& NFSMNT_SECFLAVOR
) || (args
.flags
& NFSMNT_SECSYSOK
)) {
1896 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SECURITY
);
1899 if (args
.version
>= 6) {
1900 if ((args
.flags
& NFSMNT_DEADTIMEOUT
) && (args
.deadtimeout
> 0)) {
1901 NFS_BITMAP_SET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
);
1905 /* build xdr buffer */
1906 xb_init_buffer(&xb
, NULL
, 0);
1907 xb_add_32(error
, &xb
, args
.version
);
1908 argslength_offset
= xb_offset(&xb
);
1909 xb_add_32(error
, &xb
, 0); // args length
1910 xb_add_32(error
, &xb
, NFS_XDRARGS_VERSION_0
);
1911 xb_add_bitmap(error
, &xb
, mattrs
, NFS_MATTR_BITMAP_LEN
);
1912 attrslength_offset
= xb_offset(&xb
);
1913 xb_add_32(error
, &xb
, 0); // attrs length
1914 xb_add_bitmap(error
, &xb
, mflags_mask
, NFS_MFLAG_BITMAP_LEN
); /* mask */
1915 xb_add_bitmap(error
, &xb
, mflags
, NFS_MFLAG_BITMAP_LEN
); /* value */
1916 xb_add_32(error
, &xb
, nfsvers
);
1917 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
)) {
1918 xb_add_32(error
, &xb
, args
.rsize
);
1920 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
)) {
1921 xb_add_32(error
, &xb
, args
.wsize
);
1923 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
)) {
1924 xb_add_32(error
, &xb
, args
.readdirsize
);
1926 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
)) {
1927 xb_add_32(error
, &xb
, args
.readahead
);
1929 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
1930 xb_add_32(error
, &xb
, args
.acregmin
);
1931 xb_add_32(error
, &xb
, 0);
1933 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
1934 xb_add_32(error
, &xb
, args
.acregmax
);
1935 xb_add_32(error
, &xb
, 0);
1937 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
1938 xb_add_32(error
, &xb
, args
.acdirmin
);
1939 xb_add_32(error
, &xb
, 0);
1941 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
1942 xb_add_32(error
, &xb
, args
.acdirmax
);
1943 xb_add_32(error
, &xb
, 0);
1945 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
)) {
1946 xb_add_32(error
, &xb
, nfslockmode
);
1948 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
1949 uint32_t flavors
[2], i
= 0;
1950 if (args
.flags
& NFSMNT_SECFLAVOR
) {
1951 flavors
[i
++] = args
.auth
;
1953 if ((args
.flags
& NFSMNT_SECSYSOK
) && ((i
== 0) || (flavors
[0] != RPCAUTH_SYS
))) {
1954 flavors
[i
++] = RPCAUTH_SYS
;
1956 xb_add_word_array(error
, &xb
, flavors
, i
);
1958 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
)) {
1959 xb_add_32(error
, &xb
, args
.maxgrouplist
);
1961 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
)) {
1962 xb_add_string(error
, &xb
, ((args
.sotype
== SOCK_DGRAM
) ? "udp" : "tcp"), 3);
1964 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
)) {
1965 xb_add_32(error
, &xb
, ((ss
.ss_family
== AF_INET
) ?
1966 ntohs(((struct sockaddr_in
*)&ss
)->sin_port
) :
1967 ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
)));
1969 /* NFS_MATTR_MOUNT_PORT (not available in old args) */
1970 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
1971 /* convert from .1s increments to time */
1972 xb_add_32(error
, &xb
, args
.timeo
/ 10);
1973 xb_add_32(error
, &xb
, (args
.timeo
% 10) * 100000000);
1975 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
)) {
1976 xb_add_32(error
, &xb
, args
.retrans
);
1978 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
1979 xb_add_32(error
, &xb
, args
.deadtimeout
);
1980 xb_add_32(error
, &xb
, 0);
1982 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
)) {
1983 xb_add_fh(error
, &xb
, &nfh
[0], args
.fhsize
);
1985 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
1986 xb_add_32(error
, &xb
, 1); /* fs location count */
1987 xb_add_32(error
, &xb
, 1); /* server count */
1988 xb_add_string(error
, &xb
, mntfrom
, (endserverp
- mntfrom
)); /* server name */
1989 xb_add_32(error
, &xb
, 1); /* address count */
1990 xb_add_string(error
, &xb
, uaddr
, strlen(uaddr
)); /* address */
1991 xb_add_32(error
, &xb
, 0); /* empty server info */
1992 xb_add_32(error
, &xb
, numcomps
); /* pathname component count */
1995 while (*p
&& (*p
== '/')) {
2000 while (*p
&& (*p
!= '/')) {
2003 xb_add_string(error
, &xb
, cp
, (p
- cp
)); /* component */
2005 while (*p
&& (*p
== '/')) {
2009 xb_add_32(error
, &xb
, 0); /* empty fsl info */
2011 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
)) {
2012 xb_add_32(error
, &xb
, (vfs_flags(mp
) & MNT_VISFLAGMASK
)); /* VFS MNT_* flags */
2014 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
)) {
2015 xb_add_string(error
, &xb
, mntfrom
, strlen(mntfrom
)); /* fixed f_mntfromname */
2017 xb_build_done(error
, &xb
);
2019 /* update opaque counts */
2020 end_offset
= xb_offset(&xb
);
2021 error
= xb_seek(&xb
, argslength_offset
);
2022 xb_add_32(error
, &xb
, end_offset
- argslength_offset
+ XDRWORD
/*version*/);
2024 error
= xb_seek(&xb
, attrslength_offset
);
2025 xb_add_32(error
, &xb
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
2028 /* grab the assembled buffer */
2029 *xdrbufp
= xb_buffer_base(&xb
);
2030 xb
.xb_flags
&= ~XB_CLEANUP
;
2034 NFS_ZFREE(ZV_NAMEI
, mntfrom
);
2044 nfs_vfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t ctx
)
2046 int error
= 0, inkernel
= vfs_iskernelmount(mp
);
2047 uint32_t argsversion
, argslength
;
2048 char *xdrbuf
= NULL
;
2050 /* read in version */
2052 bcopy(CAST_DOWN(void *, data
), &argsversion
, sizeof(argsversion
));
2053 } else if ((error
= copyin(data
, &argsversion
, sizeof(argsversion
)))) {
2057 /* If we have XDR args, then all values in the buffer are in network order */
2058 if (argsversion
== htonl(NFS_ARGSVERSION_XDR
)) {
2059 argsversion
= NFS_ARGSVERSION_XDR
;
2062 switch (argsversion
) {
2067 /* convert old-style args to xdr */
2068 error
= nfs_convert_old_nfs_args(mp
, data
, ctx
, argsversion
, inkernel
, &xdrbuf
);
2070 case NFS_ARGSVERSION_XDR
:
2071 /* copy in xdr buffer */
2073 bcopy(CAST_DOWN(void *, (data
+ XDRWORD
)), &argslength
, XDRWORD
);
2075 error
= copyin((data
+ XDRWORD
), &argslength
, XDRWORD
);
2080 argslength
= ntohl(argslength
);
2081 /* put a reasonable limit on the size of the XDR args */
2082 if (argslength
> 16 * 1024) {
2086 /* allocate xdr buffer */
2087 xdrbuf
= xb_malloc(xdr_rndup(argslength
));
2093 bcopy(CAST_DOWN(void *, data
), xdrbuf
, argslength
);
2095 error
= copyin(data
, xdrbuf
, argslength
);
2099 /* Recheck buffer size to avoid double fetch vulnerability */
2101 uint32_t _version
, _length
;
2102 xb_init_buffer(&xb
, xdrbuf
, 2 * XDRWORD
);
2103 xb_get_32(error
, &xb
, _version
); /* version */
2104 xb_get_32(error
, &xb
, _length
); /* args length */
2105 if (_length
!= argslength
) {
2106 printf("nfs: actual buffer length (%u) does not match the initial value (%u)\n", _length
, argslength
);
2114 error
= EPROGMISMATCH
;
2123 error
= mountnfs(xdrbuf
, mp
, ctx
, &vp
);
2128 * Common code for mount and mountroot
2131 /* Set up an NFSv2/v3 mount */
2134 struct nfsmount
*nmp
,
2139 struct nfs_vattr nvattr
;
2149 * Get file attributes for the mountpoint. These are needed
2150 * in order to properly create the root vnode.
2152 error
= nfs3_getattr_rpc(NULL
, nmp
->nm_mountp
, nmp
->nm_fh
->fh_data
, nmp
->nm_fh
->fh_len
, 0,
2153 ctx
, &nvattr
, &xid
);
2158 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, nmp
->nm_fh
->fh_data
, nmp
->nm_fh
->fh_len
,
2159 &nvattr
, &xid
, RPCAUTH_UNKNOWN
, NG_MARKROOT
, npp
);
2161 nfs_node_unlock(*npp
);
2168 * Try to make sure we have all the general info from the server.
2170 if (nmp
->nm_vers
== NFS_VER2
) {
2171 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
2172 nmp
->nm_fsattr
.nfsa_maxname
= NFS_MAXNAMLEN
;
2173 } else if (nmp
->nm_vers
== NFS_VER3
) {
2174 /* get the NFSv3 FSINFO */
2175 error
= nfs3_fsinfo(nmp
, *npp
, ctx
);
2179 /* grab a copy of root info now (even if server does not support FSF_HOMOGENEOUS) */
2180 struct nfs_fsattr nfsa
;
2181 if (!nfs3_pathconf_rpc(*npp
, &nfsa
, ctx
)) {
2182 /* cache a copy of the results */
2183 lck_mtx_lock(&nmp
->nm_lock
);
2184 nfs3_pathconf_cache(nmp
, &nfsa
);
2185 lck_mtx_unlock(&nmp
->nm_lock
);
2189 if (*npp
&& error
) {
2190 vnode_put(NFSTOV(*npp
));
2191 vnode_recycle(NFSTOV(*npp
));
2199 * Update an NFSv4 mount path with the contents of the symlink.
2201 * Read the link for the given file handle.
2202 * Insert the link's components into the path.
2205 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
)
2207 int error
= 0, status
, numops
;
2208 uint32_t len
= 0, comp
, newcomp
, linkcompcount
;
2210 struct nfsm_chain nmreq
, nmrep
;
2211 struct nfsreq rq
, *req
= &rq
;
2212 struct nfsreq_secinfo_args si
;
2213 char *link
= NULL
, *p
, *q
, ch
;
2214 struct nfs_fs_path nfsp2
;
2216 bzero(&nfsp2
, sizeof(nfsp2
));
2217 if (dirfhp
->fh_len
) {
2218 NFSREQ_SECINFO_SET(&si
, NULL
, dirfhp
->fh_data
, dirfhp
->fh_len
, nfsp
->np_components
[curcomp
], 0);
2220 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, nfsp
->np_components
[curcomp
], 0);
2222 nfsm_chain_null(&nmreq
);
2223 nfsm_chain_null(&nmrep
);
2225 link
= zalloc(ZV_NAMEI
);
2229 nfsm_chain_build_alloc_init(error
, &nmreq
, 12 * NFSX_UNSIGNED
);
2230 nfsm_chain_add_compound_header(error
, &nmreq
, "readlink", nmp
->nm_minor_vers
, numops
);
2232 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
2233 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, fhp
->fh_data
, fhp
->fh_len
);
2235 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READLINK
);
2236 nfsm_chain_build_done(error
, &nmreq
);
2237 nfsm_assert(error
, (numops
== 0), EPROTO
);
2240 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2241 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2243 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2246 nfsm_chain_skip_tag(error
, &nmrep
);
2247 nfsm_chain_get_32(error
, &nmrep
, numops
);
2248 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
2249 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READLINK
);
2250 nfsm_chain_get_32(error
, &nmrep
, len
);
2254 } else if (len
>= MAXPATHLEN
) {
2255 len
= MAXPATHLEN
- 1;
2257 nfsm_chain_get_opaque(error
, &nmrep
, len
, link
);
2259 /* make sure link string is terminated properly */
2262 /* count the number of components in link */
2264 while (*p
&& (*p
== '/')) {
2270 while (*p
&& (*p
!= '/')) {
2273 while (*p
&& (*p
== '/')) {
2278 /* free up used components */
2279 for (comp
= 0; comp
<= curcomp
; comp
++) {
2280 if (nfsp
->np_components
[comp
]) {
2281 FREE(nfsp
->np_components
[comp
], M_TEMP
);
2282 nfsp
->np_components
[comp
] = NULL
;
2286 /* set up new path */
2287 nfsp2
.np_compcount
= nfsp
->np_compcount
- curcomp
- 1 + linkcompcount
;
2288 MALLOC(nfsp2
.np_components
, char **, nfsp2
.np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
2289 if (!nfsp2
.np_components
) {
2294 /* add link components */
2296 while (*p
&& (*p
== '/')) {
2299 for (newcomp
= 0; newcomp
< linkcompcount
; newcomp
++) {
2300 /* find end of component */
2302 while (*q
&& (*q
!= '/')) {
2305 MALLOC(nfsp2
.np_components
[newcomp
], char *, q
- p
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2306 if (!nfsp2
.np_components
[newcomp
]) {
2312 strlcpy(nfsp2
.np_components
[newcomp
], p
, q
- p
+ 1);
2315 while (*p
&& (*p
== '/')) {
2321 /* add remaining components */
2322 for (comp
= curcomp
+ 1; comp
< nfsp
->np_compcount
; comp
++, newcomp
++) {
2323 nfsp2
.np_components
[newcomp
] = nfsp
->np_components
[comp
];
2324 nfsp
->np_components
[comp
] = NULL
;
2327 /* move new path into place */
2328 FREE(nfsp
->np_components
, M_TEMP
);
2329 nfsp
->np_components
= nfsp2
.np_components
;
2330 nfsp
->np_compcount
= nfsp2
.np_compcount
;
2331 nfsp2
.np_components
= NULL
;
2333 /* for absolute link, let the caller now that the next dirfh is root */
2334 if (link
[0] == '/') {
2340 NFS_ZFREE(ZV_NAMEI
, link
);
2342 if (nfsp2
.np_components
) {
2343 for (comp
= 0; comp
< nfsp2
.np_compcount
; comp
++) {
2344 if (nfsp2
.np_components
[comp
]) {
2345 FREE(nfsp2
.np_components
[comp
], M_TEMP
);
2348 FREE(nfsp2
.np_components
, M_TEMP
);
2350 nfsm_chain_cleanup(&nmreq
);
2351 nfsm_chain_cleanup(&nmrep
);
2355 /* Set up an NFSv4 mount */
2358 struct nfsmount
*nmp
,
2362 struct nfsm_chain nmreq
, nmrep
;
2363 int error
= 0, numops
, status
, interval
, isdotdot
, loopcnt
= 0, depth
= 0;
2364 struct nfs_fs_path fspath
, *nfsp
, fspath2
;
2365 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], comp
, comp2
;
2366 fhandle_t fh
, dirfh
;
2367 struct nfs_vattr nvattr
;
2369 struct nfsreq rq
, *req
= &rq
;
2370 struct nfsreq_secinfo_args si
;
2372 struct nfs_fs_locations nfsls
;
2375 fh
.fh_len
= dirfh
.fh_len
= 0;
2376 lck_mtx_init(&nmp
->nm_timer_lock
, &nfs_mount_grp
, LCK_ATTR_NULL
);
2377 TAILQ_INIT(&nmp
->nm_open_owners
);
2378 TAILQ_INIT(&nmp
->nm_delegations
);
2379 TAILQ_INIT(&nmp
->nm_dreturnq
);
2380 nmp
->nm_stategenid
= 1;
2381 NVATTR_INIT(&nvattr
);
2382 bzero(&nfsls
, sizeof(nfsls
));
2383 nfsm_chain_null(&nmreq
);
2384 nfsm_chain_null(&nmrep
);
2387 * If no security flavors were specified we'll want to default to the server's
2388 * preferred flavor. For NFSv4.0 we need a file handle and name to get that via
2389 * SECINFO, so we'll do that on the last component of the server path we are
2390 * mounting. If we are mounting the server's root, we'll need to defer the
2391 * SECINFO call to the first successful LOOKUP request.
2393 if (!nmp
->nm_sec
.count
) {
2394 nmp
->nm_state
|= NFSSTA_NEEDSECINFO
;
2397 /* make a copy of the current location's path */
2398 nfsp
= &nmp
->nm_locations
.nl_locations
[nmp
->nm_locations
.nl_current
.nli_loc
]->nl_path
;
2399 bzero(&fspath
, sizeof(fspath
));
2400 fspath
.np_compcount
= nfsp
->np_compcount
;
2401 if (fspath
.np_compcount
> 0) {
2402 MALLOC(fspath
.np_components
, char **, fspath
.np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
2403 if (!fspath
.np_components
) {
2407 for (comp
= 0; comp
< nfsp
->np_compcount
; comp
++) {
2408 size_t slen
= strlen(nfsp
->np_components
[comp
]);
2409 MALLOC(fspath
.np_components
[comp
], char *, slen
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2410 if (!fspath
.np_components
[comp
]) {
2414 strlcpy(fspath
.np_components
[comp
], nfsp
->np_components
[comp
], slen
+ 1);
2421 /* for mirror mounts, we can just use the file handle passed in */
2423 dirfh
.fh_len
= nmp
->nm_fh
->fh_len
;
2424 bcopy(nmp
->nm_fh
->fh_data
, dirfh
.fh_data
, dirfh
.fh_len
);
2425 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, NULL
, 0);
2429 /* otherwise, we need to get the fh for the directory we are mounting */
2431 /* if no components, just get root */
2432 if (fspath
.np_compcount
== 0) {
2434 // PUTROOTFH + GETATTR(FH)
2435 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, NULL
, 0);
2437 nfsm_chain_build_alloc_init(error
, &nmreq
, 9 * NFSX_UNSIGNED
);
2438 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2440 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTROOTFH
);
2442 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2443 NFS_CLEAR_ATTRIBUTES(bitmap
);
2444 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2445 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
2446 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2447 nfsm_chain_build_done(error
, &nmreq
);
2448 nfsm_assert(error
, (numops
== 0), EPROTO
);
2450 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2451 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2453 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2455 nfsm_chain_skip_tag(error
, &nmrep
);
2456 nfsm_chain_get_32(error
, &nmrep
, numops
);
2457 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTROOTFH
);
2458 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2460 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2461 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, &dirfh
, NULL
, NULL
);
2462 if (!error
&& !NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
2463 printf("nfs: mount didn't return filehandle?\n");
2467 nfsm_chain_cleanup(&nmrep
);
2468 nfsm_chain_null(&nmreq
);
2469 NVATTR_CLEANUP(&nvattr
);
2473 /* look up each path component */
2474 for (comp
= 0; comp
< fspath
.np_compcount
;) {
2476 if (fspath
.np_components
[comp
][0] == '.') {
2477 if (fspath
.np_components
[comp
][1] == '\0') {
2482 /* treat ".." specially */
2483 if ((fspath
.np_components
[comp
][1] == '.') &&
2484 (fspath
.np_components
[comp
][2] == '\0')) {
2487 if (isdotdot
&& (dirfh
.fh_len
== 0)) {
2488 /* ".." in root directory is same as "." */
2493 // PUT(ROOT)FH + LOOKUP(P) + GETFH + GETATTR
2494 if (dirfh
.fh_len
== 0) {
2495 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2497 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2500 nfsm_chain_build_alloc_init(error
, &nmreq
, 18 * NFSX_UNSIGNED
);
2501 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2504 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
2505 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, dirfh
.fh_data
, dirfh
.fh_len
);
2507 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTROOTFH
);
2511 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUPP
);
2513 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
2514 nfsm_chain_add_name(error
, &nmreq
,
2515 fspath
.np_components
[comp
], strlen(fspath
.np_components
[comp
]), nmp
);
2518 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETFH
);
2520 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2521 NFS_CLEAR_ATTRIBUTES(bitmap
);
2522 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2523 /* if no namedattr support or component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2524 if (!NMFLAG(nmp
, NAMEDATTR
) || !strcmp(fspath
.np_components
[comp
], ".zfs")) {
2525 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
2527 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2528 nfsm_chain_build_done(error
, &nmreq
);
2529 nfsm_assert(error
, (numops
== 0), EPROTO
);
2531 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2532 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2534 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2536 nfsm_chain_skip_tag(error
, &nmrep
);
2537 nfsm_chain_get_32(error
, &nmrep
, numops
);
2538 nfsm_chain_op_check(error
, &nmrep
, dirfh
.fh_len
? NFS_OP_PUTFH
: NFS_OP_PUTROOTFH
);
2539 nfsm_chain_op_check(error
, &nmrep
, isdotdot
? NFS_OP_LOOKUPP
: NFS_OP_LOOKUP
);
2541 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETFH
);
2542 nfsm_chain_get_32(error
, &nmrep
, fh
.fh_len
);
2543 if (fh
.fh_len
> sizeof(fh
.fh_data
)) {
2547 nfsm_chain_get_opaque(error
, &nmrep
, fh
.fh_len
, fh
.fh_data
);
2548 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2550 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2551 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, &nfsls
);
2553 nfsm_chain_cleanup(&nmrep
);
2554 nfsm_chain_null(&nmreq
);
2556 /* LOOKUP succeeded but GETATTR failed? This could be a referral. */
2557 /* Try the lookup again with a getattr for fs_locations. */
2558 nfs_fs_locations_cleanup(&nfsls
);
2559 error
= nfs4_get_fs_locations(nmp
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, fspath
.np_components
[comp
], ctx
, &nfsls
);
2560 if (!error
&& (nfsls
.nl_numlocs
< 1)) {
2564 if (++loopcnt
> MAXSYMLINKS
) {
2565 /* too many symlink/referral redirections */
2569 /* tear down the current connection */
2570 nfs_disconnect(nmp
);
2571 /* replace fs locations */
2572 nfs_fs_locations_cleanup(&nmp
->nm_locations
);
2573 nmp
->nm_locations
= nfsls
;
2574 bzero(&nfsls
, sizeof(nfsls
));
2575 /* initiate a connection using the new fs locations */
2576 error
= nfs_mount_connect(nmp
);
2577 if (!error
&& !(nmp
->nm_locations
.nl_current
.nli_flags
& NLI_VALID
)) {
2581 /* add new server's remote path to beginning of our path and continue */
2582 nfsp
= &nmp
->nm_locations
.nl_locations
[nmp
->nm_locations
.nl_current
.nli_loc
]->nl_path
;
2583 bzero(&fspath2
, sizeof(fspath2
));
2584 fspath2
.np_compcount
= (fspath
.np_compcount
- comp
- 1) + nfsp
->np_compcount
;
2585 if (fspath2
.np_compcount
> 0) {
2586 MALLOC(fspath2
.np_components
, char **, fspath2
.np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
2587 if (!fspath2
.np_components
) {
2591 for (comp2
= 0; comp2
< nfsp
->np_compcount
; comp2
++) {
2592 size_t slen
= strlen(nfsp
->np_components
[comp2
]);
2593 MALLOC(fspath2
.np_components
[comp2
], char *, slen
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2594 if (!fspath2
.np_components
[comp2
]) {
2595 /* clean up fspath2, then error out */
2598 FREE(fspath2
.np_components
[comp2
], M_TEMP
);
2600 FREE(fspath2
.np_components
, M_TEMP
);
2604 strlcpy(fspath2
.np_components
[comp2
], nfsp
->np_components
[comp2
], slen
+ 1);
2606 if ((fspath
.np_compcount
- comp
- 1) > 0) {
2607 bcopy(&fspath
.np_components
[comp
+ 1], &fspath2
.np_components
[nfsp
->np_compcount
], (fspath
.np_compcount
- comp
- 1) * sizeof(char*));
2609 /* free up unused parts of old path (prior components and component array) */
2611 FREE(fspath
.np_components
[comp
], M_TEMP
);
2612 } while (comp
-- > 0);
2613 FREE(fspath
.np_components
, M_TEMP
);
2614 /* put new path in place */
2617 /* reset dirfh and component index */
2620 NVATTR_CLEANUP(&nvattr
);
2621 if (fspath
.np_compcount
== 0) {
2627 /* if file handle is for a symlink, then update the path with the symlink contents */
2628 if (NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_TYPE
) && (nvattr
.nva_type
== VLNK
)) {
2629 if (++loopcnt
> MAXSYMLINKS
) {
2632 error
= nfs4_mount_update_path_with_symlink(nmp
, &fspath
, comp
, &dirfh
, &depth
, &fh
, ctx
);
2635 /* directory file handle is either left the same or reset to root (if link was absolute) */
2636 /* path traversal starts at beginning of the path again */
2638 NVATTR_CLEANUP(&nvattr
);
2639 nfs_fs_locations_cleanup(&nfsls
);
2642 NVATTR_CLEANUP(&nvattr
);
2643 nfs_fs_locations_cleanup(&nfsls
);
2644 /* not a symlink... */
2645 if ((nmp
->nm_state
& NFSSTA_NEEDSECINFO
) && (comp
== (fspath
.np_compcount
- 1)) && !isdotdot
) {
2646 /* need to get SECINFO for the directory being mounted */
2647 if (dirfh
.fh_len
== 0) {
2648 NFSREQ_SECINFO_SET(&si
, NULL
, NULL
, 0, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2650 NFSREQ_SECINFO_SET(&si
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, isdotdot
? NULL
: fspath
.np_components
[comp
], 0);
2652 sec
.count
= NX_MAX_SEC_FLAVORS
;
2653 error
= nfs4_secinfo_rpc(nmp
, &si
, vfs_context_ucred(ctx
), sec
.flavors
, &sec
.count
);
2654 /* [sigh] some implementations return "illegal" error for unsupported ops */
2655 if (error
== NFSERR_OP_ILLEGAL
) {
2659 /* set our default security flavor to the first in the list */
2661 nmp
->nm_auth
= sec
.flavors
[0];
2663 nmp
->nm_state
&= ~NFSSTA_NEEDSECINFO
;
2665 /* advance directory file handle, component index, & update depth */
2668 if (!isdotdot
) { /* going down the hierarchy */
2670 } else if (--depth
<= 0) { /* going up the hierarchy */
2671 dirfh
.fh_len
= 0; /* clear dirfh when we hit root */
2676 /* get attrs for mount point root */
2677 numops
= NMFLAG(nmp
, NAMEDATTR
) ? 3 : 2; // PUTFH + GETATTR + OPENATTR
2678 nfsm_chain_build_alloc_init(error
, &nmreq
, 25 * NFSX_UNSIGNED
);
2679 nfsm_chain_add_compound_header(error
, &nmreq
, "mount", nmp
->nm_minor_vers
, numops
);
2681 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
2682 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, dirfh
.fh_data
, dirfh
.fh_len
);
2684 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
2685 NFS_CLEAR_ATTRIBUTES(bitmap
);
2686 NFS4_DEFAULT_ATTRIBUTES(bitmap
);
2687 /* if no namedattr support or last component is ".zfs", clear NFS_FATTR_NAMED_ATTR */
2688 if (!NMFLAG(nmp
, NAMEDATTR
) || ((fspath
.np_compcount
> 0) && !strcmp(fspath
.np_components
[fspath
.np_compcount
- 1], ".zfs"))) {
2689 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
2691 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2692 if (NMFLAG(nmp
, NAMEDATTR
)) {
2694 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
2695 nfsm_chain_add_32(error
, &nmreq
, 0);
2697 nfsm_chain_build_done(error
, &nmreq
);
2698 nfsm_assert(error
, (numops
== 0), EPROTO
);
2700 error
= nfs_request_async(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
2701 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
2703 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2705 nfsm_chain_skip_tag(error
, &nmrep
);
2706 nfsm_chain_get_32(error
, &nmrep
, numops
);
2707 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
2708 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
2710 NFS_CLEAR_ATTRIBUTES(nmp
->nm_fsattr
.nfsa_bitmap
);
2711 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, &nvattr
, NULL
, NULL
, NULL
);
2713 if (NMFLAG(nmp
, NAMEDATTR
)) {
2714 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
2715 if (error
== ENOENT
) {
2718 /* [sigh] some implementations return "illegal" error for unsupported ops */
2719 if (error
|| !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_NAMED_ATTR
)) {
2720 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_NAMED_ATTR
;
2722 nmp
->nm_fsattr
.nfsa_flags
|= NFS_FSFLAG_NAMED_ATTR
;
2725 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_NAMED_ATTR
;
2727 if (NMFLAG(nmp
, NOACL
)) { /* make sure ACL support is turned off */
2728 nmp
->nm_fsattr
.nfsa_flags
&= ~NFS_FSFLAG_ACL
;
2730 if (NMFLAG(nmp
, ACLONLY
) && !(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
)) {
2731 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
);
2733 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_FH_EXPIRE_TYPE
)) {
2734 uint32_t fhtype
= ((nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_FHTYPE_MASK
) >> NFS_FSFLAG_FHTYPE_SHIFT
);
2735 if (fhtype
!= NFS_FH_PERSISTENT
) {
2736 printf("nfs: warning: non-persistent file handles! for %s\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
2740 /* make sure it's a directory */
2741 if (!NFS_BITMAP_ISSET(&nvattr
.nva_bitmap
, NFS_FATTR_TYPE
) || (nvattr
.nva_type
!= VDIR
)) {
2746 /* save the NFS fsid */
2747 nmp
->nm_fsid
= nvattr
.nva_fsid
;
2749 /* create the root node */
2750 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, dirfh
.fh_data
, dirfh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MARKROOT
, npp
);
2753 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
) {
2754 vfs_setextendedsecurity(nmp
->nm_mountp
);
2757 /* adjust I/O sizes to server limits */
2758 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXREAD
) && (nmp
->nm_fsattr
.nfsa_maxread
> 0)) {
2759 if (nmp
->nm_fsattr
.nfsa_maxread
< (uint64_t)nmp
->nm_rsize
) {
2760 nmp
->nm_rsize
= nmp
->nm_fsattr
.nfsa_maxread
& ~(NFS_FABLKSIZE
- 1);
2761 if (nmp
->nm_rsize
== 0) {
2762 nmp
->nm_rsize
= nmp
->nm_fsattr
.nfsa_maxread
;
2766 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXWRITE
) && (nmp
->nm_fsattr
.nfsa_maxwrite
> 0)) {
2767 if (nmp
->nm_fsattr
.nfsa_maxwrite
< (uint64_t)nmp
->nm_wsize
) {
2768 nmp
->nm_wsize
= nmp
->nm_fsattr
.nfsa_maxwrite
& ~(NFS_FABLKSIZE
- 1);
2769 if (nmp
->nm_wsize
== 0) {
2770 nmp
->nm_wsize
= nmp
->nm_fsattr
.nfsa_maxwrite
;
2775 /* set up lease renew timer */
2776 nmp
->nm_renew_timer
= thread_call_allocate_with_options(nfs4_renew_timer
, nmp
, THREAD_CALL_PRIORITY_HIGH
, THREAD_CALL_OPTIONS_ONCE
);
2777 interval
= nmp
->nm_fsattr
.nfsa_lease
/ 2;
2781 nfs_interval_timer_start(nmp
->nm_renew_timer
, interval
* 1000);
2784 if (fspath
.np_components
) {
2785 for (comp
= 0; comp
< fspath
.np_compcount
; comp
++) {
2786 if (fspath
.np_components
[comp
]) {
2787 FREE(fspath
.np_components
[comp
], M_TEMP
);
2790 FREE(fspath
.np_components
, M_TEMP
);
2792 NVATTR_CLEANUP(&nvattr
);
2793 nfs_fs_locations_cleanup(&nfsls
);
2795 nfs_node_unlock(*npp
);
2797 nfsm_chain_cleanup(&nmreq
);
2798 nfsm_chain_cleanup(&nmrep
);
2801 #endif /* CONFIG_NFS4 */
2804 * Thread to handle initial NFS mount connection.
2807 nfs_mount_connect_thread(void *arg
, __unused wait_result_t wr
)
2809 struct nfsmount
*nmp
= arg
;
2810 int error
= 0, savederror
= 0, slpflag
= (NMFLAG(nmp
, INTR
) ? PCATCH
: 0);
2811 int done
= 0, timeo
, tries
, maxtries
;
2813 if (NM_OMFLAG(nmp
, MNTQUICK
)) {
2821 for (tries
= 0; tries
< maxtries
; tries
++) {
2822 error
= nfs_connect(nmp
, 1, timeo
);
2839 /* just keep retrying on any of these errors */
2843 /* looks like we got an answer... */
2848 /* save the best error */
2849 if (nfs_connect_error_class(error
) >= nfs_connect_error_class(savederror
)) {
2857 /* pause before next attempt */
2858 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 0))) {
2861 error
= tsleep(nmp
, PSOCK
| slpflag
, "nfs_mount_connect_retry", 2 * hz
);
2862 if (error
&& (error
!= EWOULDBLOCK
)) {
2868 /* update status of mount connect */
2869 lck_mtx_lock(&nmp
->nm_lock
);
2870 if (!nmp
->nm_mounterror
) {
2871 nmp
->nm_mounterror
= error
;
2873 nmp
->nm_state
&= ~NFSSTA_MOUNT_THREAD
;
2874 lck_mtx_unlock(&nmp
->nm_lock
);
2875 wakeup(&nmp
->nm_nss
);
2879 nfs_mount_connect(struct nfsmount
*nmp
)
2881 int error
= 0, slpflag
;
2883 struct timespec ts
= { .tv_sec
= 2, .tv_nsec
= 0 };
2886 * Set up the socket. Perform initial search for a location/server/address to
2887 * connect to and negotiate any unspecified mount parameters. This work is
2888 * done on a kernel thread to satisfy reserved port usage needs.
2890 slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
2891 lck_mtx_lock(&nmp
->nm_lock
);
2892 /* set flag that the thread is running */
2893 nmp
->nm_state
|= NFSSTA_MOUNT_THREAD
;
2894 if (kernel_thread_start(nfs_mount_connect_thread
, nmp
, &thd
) != KERN_SUCCESS
) {
2895 nmp
->nm_state
&= ~NFSSTA_MOUNT_THREAD
;
2896 nmp
->nm_mounterror
= EIO
;
2897 printf("nfs mount %s start socket connect thread failed\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
2899 thread_deallocate(thd
);
2902 /* wait until mount connect thread is finished/gone */
2903 while (nmp
->nm_state
& NFSSTA_MOUNT_THREAD
) {
2904 error
= msleep(&nmp
->nm_nss
, &nmp
->nm_lock
, slpflag
| PSOCK
, "nfsconnectthread", &ts
);
2905 if ((error
&& (error
!= EWOULDBLOCK
)) || ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 1)))) {
2907 if (!nmp
->nm_mounterror
) {
2908 nmp
->nm_mounterror
= error
;
2910 /* signal the thread that we are aborting */
2911 nmp
->nm_sockflags
|= NMSOCK_UNMOUNT
;
2913 wakeup(nmp
->nm_nss
);
2915 /* and continue waiting on it to finish */
2919 lck_mtx_unlock(&nmp
->nm_lock
);
2921 /* grab mount connect status */
2922 error
= nmp
->nm_mounterror
;
2927 /* Table of maximum minor version for a given version */
2928 uint32_t maxminorverstab
[] = {
2929 0, /* Version 0 (does not exist) */
2930 0, /* Version 1 (does not exist) */
2936 #define NFS_MAX_SUPPORTED_VERSION ((long)(sizeof (maxminorverstab) / sizeof (uint32_t) - 1))
2937 #define NFS_MAX_SUPPORTED_MINOR_VERSION(v) ((long)(maxminorverstab[(v)]))
2939 #define DEFAULT_NFS_MIN_VERS VER2PVER(2, 0)
2940 #define DEFAULT_NFS_MAX_VERS VER2PVER(3, 0)
2943 * Common code to mount an NFS file system.
2952 struct nfsmount
*nmp
;
2955 struct vfsstatfs
*sbp
;
2957 uint32_t i
, val
, maxio
, iosize
, len
;
2959 uint32_t *mflags_mask
;
2961 uint32_t argslength
, attrslength
;
2962 uid_t set_owner
= 0;
2963 struct nfs_location_index firstloc
= {
2964 .nli_flags
= NLI_VALID
,
2969 static const struct nfs_etype nfs_default_etypes
= {
2970 .count
= NFS_MAX_ETYPES
,
2971 .selected
= NFS_MAX_ETYPES
,
2972 .etypes
= { NFS_AES256_CTS_HMAC_SHA1_96
,
2973 NFS_AES128_CTS_HMAC_SHA1_96
,
2974 NFS_DES3_CBC_SHA1_KD
}
2977 /* make sure mbuf constants are set up */
2978 if (!nfs_mbuf_mhlen
) {
2982 if (vfs_flags(mp
) & MNT_UPDATE
) {
2984 /* update paths, file handles, etc, here XXX */
2988 /* allocate an NFS mount structure for this mount */
2989 nmp
= zalloc_flags(nfsmnt_zone
, Z_WAITOK
| Z_ZERO
);
2990 lck_mtx_init(&nmp
->nm_lock
, &nfs_mount_grp
, LCK_ATTR_NULL
);
2991 TAILQ_INIT(&nmp
->nm_resendq
);
2992 TAILQ_INIT(&nmp
->nm_iodq
);
2993 TAILQ_INIT(&nmp
->nm_gsscl
);
2994 LIST_INIT(&nmp
->nm_monlist
);
2995 vfs_setfsprivate(mp
, nmp
);
2997 nmp
->nm_mountp
= mp
;
2998 vfs_setauthopaque(mp
);
3000 * Disable cache_lookup_path for NFS. NFS lookup always needs
3001 * to be called to check if the directory attribute cache is
3002 * valid and possibly purge the directory before calling
3005 vfs_setauthcache_ttl(mp
, 0);
3007 nfs_nhinit_finish();
3009 nmp
->nm_args
= xdrbuf
;
3011 /* set up defaults */
3014 nmp
->nm_min_vers
= DEFAULT_NFS_MIN_VERS
;
3015 nmp
->nm_max_vers
= DEFAULT_NFS_MAX_VERS
;
3016 nmp
->nm_timeo
= NFS_TIMEO
;
3017 nmp
->nm_retry
= NFS_RETRANS
;
3019 nmp
->nm_sofamily
= 0;
3020 nmp
->nm_nfsport
= 0;
3021 nmp
->nm_wsize
= NFS_WSIZE
;
3022 nmp
->nm_rsize
= NFS_RSIZE
;
3023 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
3024 nmp
->nm_numgrps
= NFS_MAXGRPS
;
3025 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
3026 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
3027 if (nmp
->nm_tprintf_delay
< 0) {
3028 nmp
->nm_tprintf_delay
= 0;
3030 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
3031 if (nmp
->nm_tprintf_initial_delay
< 0) {
3032 nmp
->nm_tprintf_initial_delay
= 0;
3034 nmp
->nm_acregmin
= NFS_MINATTRTIMO
;
3035 nmp
->nm_acregmax
= NFS_MAXATTRTIMO
;
3036 nmp
->nm_acdirmin
= NFS_MINDIRATTRTIMO
;
3037 nmp
->nm_acdirmax
= NFS_MAXDIRATTRTIMO
;
3038 nmp
->nm_etype
= nfs_default_etypes
;
3039 nmp
->nm_auth
= RPCAUTH_SYS
;
3040 nmp
->nm_iodlink
.tqe_next
= NFSNOLIST
;
3041 nmp
->nm_deadtimeout
= 0;
3042 nmp
->nm_curdeadtimeout
= 0;
3043 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
); /* enable RDIRPLUS by default. It will be reverted later in case NFSv2 is used */
3044 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
3045 nmp
->nm_realm
= NULL
;
3046 nmp
->nm_principal
= NULL
;
3047 nmp
->nm_sprinc
= NULL
;
3050 mattrs
= nmp
->nm_mattrs
;
3051 mflags
= nmp
->nm_mflags
;
3052 mflags_mask
= nmp
->nm_mflags_mask
;
3054 /* set up NFS mount with args */
3055 xb_init_buffer(&xb
, xdrbuf
, 2 * XDRWORD
);
3056 xb_get_32(error
, &xb
, val
); /* version */
3057 xb_get_32(error
, &xb
, argslength
); /* args length */
3059 xb_init_buffer(&xb
, xdrbuf
, argslength
); /* restart parsing with actual buffer length */
3060 xb_get_32(error
, &xb
, val
); /* version */
3061 xb_get_32(error
, &xb
, argslength
); /* args length */
3062 xb_get_32(error
, &xb
, val
); /* XDR args version */
3063 if (val
!= NFS_XDRARGS_VERSION_0
|| argslength
< ((4 + NFS_MATTR_BITMAP_LEN
+ 1) * XDRWORD
)) {
3066 len
= NFS_MATTR_BITMAP_LEN
;
3067 xb_get_bitmap(error
, &xb
, mattrs
, len
); /* mount attribute bitmap */
3069 xb_get_32(error
, &xb
, attrslength
); /* attrs length */
3070 if (!error
&& (attrslength
> (argslength
- ((4 + NFS_MATTR_BITMAP_LEN
+ 1) * XDRWORD
)))) {
3074 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FLAGS
)) {
3075 len
= NFS_MFLAG_BITMAP_LEN
;
3076 xb_get_bitmap(error
, &xb
, mflags_mask
, len
); /* mount flag mask */
3077 len
= NFS_MFLAG_BITMAP_LEN
;
3078 xb_get_bitmap(error
, &xb
, mflags
, len
); /* mount flag values */
3080 /* clear all mask bits and OR in all the ones that are set */
3081 nmp
->nm_flags
[0] &= ~mflags_mask
[0];
3082 nmp
->nm_flags
[0] |= (mflags_mask
[0] & mflags
[0]);
3085 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
)) {
3086 /* Can't specify a single version and a range */
3087 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
)) {
3090 xb_get_32(error
, &xb
, nmp
->nm_vers
);
3091 if (nmp
->nm_vers
> NFS_MAX_SUPPORTED_VERSION
||
3092 nmp
->nm_vers
< NFS_VER2
) {
3095 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
)) {
3096 xb_get_32(error
, &xb
, nmp
->nm_minor_vers
);
3098 nmp
->nm_minor_vers
= maxminorverstab
[nmp
->nm_vers
];
3100 if (nmp
->nm_minor_vers
> maxminorverstab
[nmp
->nm_vers
]) {
3103 nmp
->nm_max_vers
= nmp
->nm_min_vers
=
3104 VER2PVER(nmp
->nm_vers
, nmp
->nm_minor_vers
);
3106 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
)) {
3107 /* should have also gotten NFS version (and already gotten minor version) */
3108 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
)) {
3112 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
)) {
3113 xb_get_32(error
, &xb
, nmp
->nm_min_vers
);
3114 xb_get_32(error
, &xb
, nmp
->nm_max_vers
);
3115 if ((nmp
->nm_min_vers
> nmp
->nm_max_vers
) ||
3116 (PVER2MAJOR(nmp
->nm_max_vers
) > NFS_MAX_SUPPORTED_VERSION
) ||
3117 (PVER2MINOR(nmp
->nm_min_vers
) > maxminorverstab
[PVER2MAJOR(nmp
->nm_min_vers
)]) ||
3118 (PVER2MINOR(nmp
->nm_max_vers
) > maxminorverstab
[PVER2MAJOR(nmp
->nm_max_vers
)])) {
3122 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
)) {
3123 xb_get_32(error
, &xb
, nmp
->nm_rsize
);
3125 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
)) {
3126 xb_get_32(error
, &xb
, nmp
->nm_wsize
);
3128 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
)) {
3129 xb_get_32(error
, &xb
, nmp
->nm_readdirsize
);
3131 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
)) {
3132 xb_get_32(error
, &xb
, nmp
->nm_readahead
);
3134 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
3135 xb_get_32(error
, &xb
, nmp
->nm_acregmin
);
3136 xb_skip(error
, &xb
, XDRWORD
);
3138 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
3139 xb_get_32(error
, &xb
, nmp
->nm_acregmax
);
3140 xb_skip(error
, &xb
, XDRWORD
);
3142 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
3143 xb_get_32(error
, &xb
, nmp
->nm_acdirmin
);
3144 xb_skip(error
, &xb
, XDRWORD
);
3146 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
3147 xb_get_32(error
, &xb
, nmp
->nm_acdirmax
);
3148 xb_skip(error
, &xb
, XDRWORD
);
3151 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
)) {
3152 xb_get_32(error
, &xb
, val
);
3154 case NFS_LOCK_MODE_DISABLED
:
3155 case NFS_LOCK_MODE_LOCAL
:
3157 if (nmp
->nm_vers
>= NFS_VER4
) {
3158 /* disabled/local lock mode only allowed on v2/v3 */
3164 case NFS_LOCK_MODE_ENABLED
:
3165 nmp
->nm_lockmode
= val
;
3172 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
3174 xb_get_32(error
, &xb
, seccnt
);
3175 if (!error
&& ((seccnt
< 1) || (seccnt
> NX_MAX_SEC_FLAVORS
))) {
3179 nmp
->nm_sec
.count
= seccnt
;
3180 for (i
= 0; i
< seccnt
; i
++) {
3181 xb_get_32(error
, &xb
, nmp
->nm_sec
.flavors
[i
]);
3182 /* Check for valid security flavor */
3183 switch (nmp
->nm_sec
.flavors
[i
]) {
3194 /* start with the first flavor */
3195 nmp
->nm_auth
= nmp
->nm_sec
.flavors
[0];
3197 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_KERB_ETYPE
)) {
3199 xb_get_32(error
, &xb
, etypecnt
);
3200 if (!error
&& ((etypecnt
< 1) || (etypecnt
> NFS_MAX_ETYPES
))) {
3204 nmp
->nm_etype
.count
= etypecnt
;
3205 xb_get_32(error
, &xb
, nmp
->nm_etype
.selected
);
3208 nmp
->nm_etype
.selected
= etypecnt
; /* Nothing is selected yet, so set selected to count */
3209 for (i
= 0; i
< etypecnt
; i
++) {
3210 xb_get_32(error
, &xb
, nmp
->nm_etype
.etypes
[i
]);
3211 /* Check for valid encryption type */
3212 switch (nmp
->nm_etype
.etypes
[i
]) {
3213 case NFS_DES3_CBC_SHA1_KD
:
3214 case NFS_AES128_CTS_HMAC_SHA1_96
:
3215 case NFS_AES256_CTS_HMAC_SHA1_96
:
3223 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
)) {
3224 xb_get_32(error
, &xb
, nmp
->nm_numgrps
);
3226 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
)) {
3230 xb_get_32(error
, &xb
, val
);
3231 if (!error
&& ((val
< 3) || (val
> sizeof(sotype
)))) {
3235 error
= xb_get_bytes(&xb
, sotype
, val
, 0);
3238 if (!strcmp(sotype
, "tcp")) {
3239 nmp
->nm_sotype
= SOCK_STREAM
;
3240 } else if (!strcmp(sotype
, "udp")) {
3241 nmp
->nm_sotype
= SOCK_DGRAM
;
3242 } else if (!strcmp(sotype
, "tcp4")) {
3243 nmp
->nm_sotype
= SOCK_STREAM
;
3244 nmp
->nm_sofamily
= AF_INET
;
3245 } else if (!strcmp(sotype
, "udp4")) {
3246 nmp
->nm_sotype
= SOCK_DGRAM
;
3247 nmp
->nm_sofamily
= AF_INET
;
3248 } else if (!strcmp(sotype
, "tcp6")) {
3249 nmp
->nm_sotype
= SOCK_STREAM
;
3250 nmp
->nm_sofamily
= AF_INET6
;
3251 } else if (!strcmp(sotype
, "udp6")) {
3252 nmp
->nm_sotype
= SOCK_DGRAM
;
3253 nmp
->nm_sofamily
= AF_INET6
;
3254 } else if (!strcmp(sotype
, "inet4")) {
3255 nmp
->nm_sofamily
= AF_INET
;
3256 } else if (!strcmp(sotype
, "inet6")) {
3257 nmp
->nm_sofamily
= AF_INET6
;
3258 } else if (!strcmp(sotype
, "inet")) {
3259 nmp
->nm_sofamily
= 0; /* ok */
3260 } else if (!strcmp(sotype
, "ticotsord")) {
3261 nmp
->nm_sofamily
= AF_LOCAL
;
3262 nmp
->nm_sotype
= SOCK_STREAM
;
3263 } else if (!strcmp(sotype
, "ticlts")) {
3264 nmp
->nm_sofamily
= AF_LOCAL
;
3265 nmp
->nm_sotype
= SOCK_DGRAM
;
3270 if (!error
&& (nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_sotype
&&
3271 (nmp
->nm_sotype
!= SOCK_STREAM
)) {
3272 error
= EINVAL
; /* NFSv4 is only allowed over TCP. */
3276 NFS_VFS_DBG("EINVAL sotype = \"%s\"\n", sotype
);
3280 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
)) {
3281 xb_get_32(error
, &xb
, val
);
3282 if (NFS_PORT_INVALID(val
)) {
3286 nmp
->nm_nfsport
= (in_port_t
)val
;
3288 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MOUNT_PORT
)) {
3289 xb_get_32(error
, &xb
, val
);
3290 if (NFS_PORT_INVALID(val
)) {
3294 nmp
->nm_mountport
= (in_port_t
)val
;
3296 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
3297 /* convert from time to 0.1s units */
3298 xb_get_32(error
, &xb
, nmp
->nm_timeo
);
3299 xb_get_32(error
, &xb
, val
);
3301 if (val
>= 1000000000) {
3305 nmp
->nm_timeo
*= 10;
3306 nmp
->nm_timeo
+= (val
+ 100000000 - 1) / 100000000;
3307 /* now convert to ticks */
3308 nmp
->nm_timeo
= (nmp
->nm_timeo
* NFS_HZ
+ 5) / 10;
3310 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
)) {
3311 xb_get_32(error
, &xb
, val
);
3312 if (!error
&& (val
> 1)) {
3313 nmp
->nm_retry
= val
;
3316 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
3317 xb_get_32(error
, &xb
, nmp
->nm_deadtimeout
);
3318 xb_skip(error
, &xb
, XDRWORD
);
3320 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
)) {
3322 nmp
->nm_fh
= zalloc(nfs_fhandle_zone
);
3323 xb_get_32(error
, &xb
, nmp
->nm_fh
->fh_len
);
3325 if ((size_t)nmp
->nm_fh
->fh_len
> sizeof(nmp
->nm_fh
->fh_data
)) {
3328 error
= xb_get_bytes(&xb
, (char*)&nmp
->nm_fh
->fh_data
[0], nmp
->nm_fh
->fh_len
, 0);
3332 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
3333 uint32_t loc
, serv
, addr
, comp
;
3334 struct nfs_fs_location
*fsl
;
3335 struct nfs_fs_server
*fss
;
3336 struct nfs_fs_path
*fsp
;
3338 xb_get_32(error
, &xb
, nmp
->nm_locations
.nl_numlocs
); /* fs location count */
3339 /* sanity check location count */
3340 if (!error
&& ((nmp
->nm_locations
.nl_numlocs
< 1) || (nmp
->nm_locations
.nl_numlocs
> 256))) {
3341 NFS_VFS_DBG("Invalid number of fs_locations: %d", nmp
->nm_locations
.nl_numlocs
);
3345 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
);
3346 if (!nmp
->nm_locations
.nl_locations
) {
3349 for (loc
= 0; loc
< nmp
->nm_locations
.nl_numlocs
; loc
++) {
3351 MALLOC(fsl
, struct nfs_fs_location
*, sizeof(struct nfs_fs_location
), M_TEMP
, M_WAITOK
| M_ZERO
);
3355 nmp
->nm_locations
.nl_locations
[loc
] = fsl
;
3356 xb_get_32(error
, &xb
, fsl
->nl_servcount
); /* server count */
3357 /* sanity check server count */
3358 if (!error
&& ((fsl
->nl_servcount
< 1) || (fsl
->nl_servcount
> 256))) {
3359 NFS_VFS_DBG("Invalid server count %d", fsl
->nl_servcount
);
3363 MALLOC(fsl
->nl_servers
, struct nfs_fs_server
**, fsl
->nl_servcount
* sizeof(struct nfs_fs_server
*), M_TEMP
, M_WAITOK
| M_ZERO
);
3364 if (!fsl
->nl_servers
) {
3366 NFS_VFS_DBG("Server count = %d, error = %d\n", fsl
->nl_servcount
, error
);
3368 for (serv
= 0; serv
< fsl
->nl_servcount
; serv
++) {
3370 MALLOC(fss
, struct nfs_fs_server
*, sizeof(struct nfs_fs_server
), M_TEMP
, M_WAITOK
| M_ZERO
);
3374 fsl
->nl_servers
[serv
] = fss
;
3375 xb_get_32(error
, &xb
, val
); /* server name length */
3376 /* sanity check server name length */
3377 if (!error
&& (val
> MAXPATHLEN
)) {
3378 NFS_VFS_DBG("Invalid server name length %d", val
);
3382 MALLOC(fss
->ns_name
, char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3383 if (!fss
->ns_name
) {
3387 error
= xb_get_bytes(&xb
, fss
->ns_name
, val
, 0); /* server name */
3388 xb_get_32(error
, &xb
, fss
->ns_addrcount
); /* address count */
3389 /* sanity check address count (OK to be zero) */
3390 if (!error
&& (fss
->ns_addrcount
> 256)) {
3391 NFS_VFS_DBG("Invalid address count %d", fss
->ns_addrcount
);
3395 if (fss
->ns_addrcount
> 0) {
3396 MALLOC(fss
->ns_addresses
, char **, fss
->ns_addrcount
* sizeof(char *), M_TEMP
, M_WAITOK
| M_ZERO
);
3397 if (!fss
->ns_addresses
) {
3400 for (addr
= 0; addr
< fss
->ns_addrcount
; addr
++) {
3401 xb_get_32(error
, &xb
, val
); /* address length */
3402 /* sanity check address length */
3403 if (!error
&& val
> 128) {
3404 NFS_VFS_DBG("Invalid address length %d", val
);
3408 MALLOC(fss
->ns_addresses
[addr
], char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3409 if (!fss
->ns_addresses
[addr
]) {
3413 error
= xb_get_bytes(&xb
, fss
->ns_addresses
[addr
], val
, 0); /* address */
3416 xb_get_32(error
, &xb
, val
); /* server info length */
3417 xb_skip(error
, &xb
, val
); /* skip server info */
3420 fsp
= &fsl
->nl_path
;
3421 xb_get_32(error
, &xb
, fsp
->np_compcount
); /* component count */
3422 /* sanity check component count */
3423 if (!error
&& (fsp
->np_compcount
> MAXPATHLEN
)) {
3424 NFS_VFS_DBG("Invalid component count %d", fsp
->np_compcount
);
3428 if (fsp
->np_compcount
) {
3429 MALLOC(fsp
->np_components
, char **, fsp
->np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
3430 if (!fsp
->np_components
) {
3434 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++) {
3435 xb_get_32(error
, &xb
, val
); /* component length */
3436 /* sanity check component length */
3437 if (!error
&& (val
== 0)) {
3439 * Apparently some people think a path with zero components should
3440 * be encoded with one zero-length component. So, just ignore any
3441 * zero length components.
3444 fsp
->np_compcount
--;
3445 if (fsp
->np_compcount
== 0) {
3446 FREE(fsp
->np_components
, M_TEMP
);
3447 fsp
->np_components
= NULL
;
3451 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
))) {
3452 NFS_VFS_DBG("Invalid component path length %d", val
);
3456 MALLOC(fsp
->np_components
[comp
], char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3457 if (!fsp
->np_components
[comp
]) {
3461 error
= xb_get_bytes(&xb
, fsp
->np_components
[comp
], val
, 0); /* component */
3463 xb_get_32(error
, &xb
, val
); /* fs location info length */
3464 NFS_VFS_DBG("Skipping fs location info bytes %d", val
);
3465 xb_skip(error
, &xb
, xdr_rndup(val
)); /* skip fs location info */
3468 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
)) {
3469 xb_skip(error
, &xb
, XDRWORD
);
3471 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
)) {
3472 xb_get_32(error
, &xb
, len
);
3475 if (val
>= sizeof(vfs_statfs(mp
)->f_mntfromname
)) {
3476 val
= sizeof(vfs_statfs(mp
)->f_mntfromname
) - 1;
3478 error
= xb_get_bytes(&xb
, vfs_statfs(mp
)->f_mntfromname
, val
, 0);
3479 if ((len
- val
) > 0) {
3480 xb_skip(error
, &xb
, len
- val
);
3483 vfs_statfs(mp
)->f_mntfromname
[val
] = '\0';
3487 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REALM
)) {
3488 xb_get_32(error
, &xb
, len
);
3489 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
))) {
3493 /* allocate an extra byte for a leading '@' if its not already prepended to the realm */
3494 MALLOC(nmp
->nm_realm
, char *, len
+ 2, M_TEMP
, M_WAITOK
| M_ZERO
);
3495 if (!nmp
->nm_realm
) {
3499 error
= xb_get_bytes(&xb
, nmp
->nm_realm
, len
, 0);
3500 if (error
== 0 && *nmp
->nm_realm
!= '@') {
3501 bcopy(nmp
->nm_realm
, &nmp
->nm_realm
[1], len
);
3502 nmp
->nm_realm
[0] = '@';
3507 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_PRINCIPAL
)) {
3508 xb_get_32(error
, &xb
, len
);
3509 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
))) {
3513 MALLOC(nmp
->nm_principal
, char *, len
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3514 if (!nmp
->nm_principal
) {
3518 error
= xb_get_bytes(&xb
, nmp
->nm_principal
, len
, 0);
3522 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SVCPRINCIPAL
)) {
3523 xb_get_32(error
, &xb
, len
);
3524 if (!error
&& ((len
< 1) || (len
> MAXPATHLEN
))) {
3528 MALLOC(nmp
->nm_sprinc
, char *, len
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3529 if (!nmp
->nm_sprinc
) {
3533 error
= xb_get_bytes(&xb
, nmp
->nm_sprinc
, len
, 0);
3537 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_NFS_PORT
)) {
3538 if (nmp
->nm_nfsport
) {
3540 NFS_VFS_DBG("Can't have ports specified over incompatible socket families");
3543 xb_get_32(error
, &xb
, len
);
3544 if (!error
&& ((len
< 1) || (len
> sizeof(((struct sockaddr_un
*)0)->sun_path
)))) {
3548 MALLOC(nmp
->nm_nfs_localport
, char *, len
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3549 if (!nmp
->nm_nfs_localport
) {
3553 error
= xb_get_bytes(&xb
, nmp
->nm_nfs_localport
, len
, 0);
3554 nmp
->nm_sofamily
= AF_LOCAL
;
3555 nmp
->nm_nfsport
= 1; /* We use the now deprecated tpcmux port to indcate that we have an AF_LOCAL port */
3556 NFS_VFS_DBG("Setting nfs local port %s (%d)\n", nmp
->nm_nfs_localport
, nmp
->nm_nfsport
);
3558 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_MOUNT_PORT
)) {
3559 if (nmp
->nm_mountport
) {
3561 NFS_VFS_DBG("Can't have ports specified over mulitple socket families");
3564 xb_get_32(error
, &xb
, len
);
3565 if (!error
&& ((len
< 1) || (len
> sizeof(((struct sockaddr_un
*)0)->sun_path
)))) {
3569 MALLOC(nmp
->nm_mount_localport
, char *, len
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
3570 if (!nmp
->nm_mount_localport
) {
3574 error
= xb_get_bytes(&xb
, nmp
->nm_mount_localport
, len
, 0);
3575 nmp
->nm_sofamily
= AF_LOCAL
;
3576 nmp
->nm_mountport
= 1; /* We use the now deprecated tpcmux port to indcate that we have an AF_LOCAL port */
3577 NFS_VFS_DBG("Setting mount local port %s (%d)\n", nmp
->nm_mount_localport
, nmp
->nm_mountport
);
3579 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SET_MOUNT_OWNER
)) {
3580 xb_get_32(error
, &xb
, set_owner
);
3582 error
= vfs_context_suser(ctx
);
3584 * root can set owner to whatever, user can set owner to self
3586 if ((error
) && (set_owner
== kauth_cred_getuid(vfs_context_ucred(ctx
)))) {
3587 /* ok for non-root can set owner to self */
3594 * Sanity check/finalize settings.
3597 if (nmp
->nm_timeo
< NFS_MINTIMEO
) {
3598 nmp
->nm_timeo
= NFS_MINTIMEO
;
3599 } else if (nmp
->nm_timeo
> NFS_MAXTIMEO
) {
3600 nmp
->nm_timeo
= NFS_MAXTIMEO
;
3602 if (nmp
->nm_retry
> NFS_MAXREXMIT
) {
3603 nmp
->nm_retry
= NFS_MAXREXMIT
;
3606 if (nmp
->nm_numgrps
> NFS_MAXGRPS
) {
3607 nmp
->nm_numgrps
= NFS_MAXGRPS
;
3609 if (nmp
->nm_readahead
> NFS_MAXRAHEAD
) {
3610 nmp
->nm_readahead
= NFS_MAXRAHEAD
;
3612 if (nmp
->nm_acregmin
> nmp
->nm_acregmax
) {
3613 nmp
->nm_acregmin
= nmp
->nm_acregmax
;
3615 if (nmp
->nm_acdirmin
> nmp
->nm_acdirmax
) {
3616 nmp
->nm_acdirmin
= nmp
->nm_acdirmax
;
3619 /* need at least one fs location */
3620 if (nmp
->nm_locations
.nl_numlocs
< 1) {
3625 if (!NM_OMATTR_GIVEN(nmp
, MNTFROM
)) {
3626 /* init mount's mntfromname to first location */
3627 nfs_location_mntfromname(&nmp
->nm_locations
, firstloc
,
3628 vfs_statfs(mp
)->f_mntfromname
,
3629 sizeof(vfs_statfs(mp
)->f_mntfromname
), 0);
3632 /* Need to save the mounting credential for v4. */
3633 nmp
->nm_mcred
= vfs_context_ucred(ctx
);
3634 if (IS_VALID_CRED(nmp
->nm_mcred
)) {
3635 kauth_cred_ref(nmp
->nm_mcred
);
3639 * If a reserved port is required, check for that privilege.
3640 * (Note that mirror mounts are exempt because the privilege was
3641 * already checked for the original mount.)
3643 if (NMFLAG(nmp
, RESVPORT
) && !vfs_iskernelmount(mp
)) {
3644 error
= priv_check_cred(nmp
->nm_mcred
, PRIV_NETINET_RESERVEDPORT
, 0);
3648 /* set up the version-specific function tables */
3649 if (nmp
->nm_vers
< NFS_VER4
) {
3650 nmp
->nm_funcs
= &nfs3_funcs
;
3653 nmp
->nm_funcs
= &nfs4_funcs
;
3655 /* don't go any further if we don't support NFS4 */
3656 nmp
->nm_funcs
= NULL
;
3662 /* do mount's initial socket connection */
3663 error
= nfs_mount_connect(nmp
);
3666 /* sanity check settings now that version/connection is set */
3667 if (nmp
->nm_vers
== NFS_VER2
) { /* ignore RDIRPLUS on NFSv2 */
3668 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
3671 if (nmp
->nm_vers
>= NFS_VER4
) {
3672 if (NFS_BITMAP_ISSET(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
)) { /* aclonly trumps noacl */
3673 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
3675 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_CALLUMNT
);
3676 if (nmp
->nm_lockmode
!= NFS_LOCK_MODE_ENABLED
) {
3677 error
= EINVAL
; /* disabled/local lock mode only allowed on v2/v3 */
3681 /* ignore these if not v4 */
3682 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOCALLBACK
);
3683 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NAMEDATTR
);
3684 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_NOACL
);
3685 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_ACLONLY
);
3691 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
3692 /* I/O size defaults for UDP are different */
3693 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
)) {
3694 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
3696 if (!NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
)) {
3697 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
3701 /* round down I/O sizes to multiple of NFS_FABLKSIZE */
3702 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
3703 if (nmp
->nm_rsize
<= 0) {
3704 nmp
->nm_rsize
= NFS_FABLKSIZE
;
3706 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
3707 if (nmp
->nm_wsize
<= 0) {
3708 nmp
->nm_wsize
= NFS_FABLKSIZE
;
3711 /* and limit I/O sizes to maximum allowed */
3712 maxio
= (nmp
->nm_vers
== NFS_VER2
) ? NFS_V2MAXDATA
:
3713 (nmp
->nm_sotype
== SOCK_DGRAM
) ? NFS_MAXDGRAMDATA
: NFS_MAXDATA
;
3714 if (maxio
> NFS_MAXBSIZE
) {
3715 maxio
= NFS_MAXBSIZE
;
3717 if (nmp
->nm_rsize
> maxio
) {
3718 nmp
->nm_rsize
= maxio
;
3720 if (nmp
->nm_wsize
> maxio
) {
3721 nmp
->nm_wsize
= maxio
;
3724 if (nmp
->nm_readdirsize
> maxio
) {
3725 nmp
->nm_readdirsize
= maxio
;
3727 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
) {
3728 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
3731 /* Set up the sockets and related info */
3732 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
3733 TAILQ_INIT(&nmp
->nm_cwndq
);
3736 if (nmp
->nm_saddr
->sa_family
== AF_LOCAL
) {
3737 struct sockaddr_un
*un
= (struct sockaddr_un
*)nmp
->nm_saddr
;
3739 int n
= snprintf(vfs_statfs(mp
)->f_mntfromname
, sizeof(vfs_statfs(mp
)->f_mntfromname
), "<%s>:", un
->sun_path
);
3741 if (n
> 0 && (size_t)n
< sizeof(vfs_statfs(mp
)->f_mntfromname
)) {
3742 size
= sizeof(vfs_statfs(mp
)->f_mntfromname
) - n
;
3743 nfs_location_mntfromname(&nmp
->nm_locations
, firstloc
,
3744 &vfs_statfs(mp
)->f_mntfromname
[n
], size
, 1);
3750 * Get the root node/attributes from the NFS server and
3751 * do any basic, version-specific setup.
3753 error
= nmp
->nm_funcs
->nf_mount(nmp
, ctx
, &np
);
3757 * A reference count is needed on the node representing the
3758 * remote root. If this object is not persistent, then backward
3759 * traversals of the mount point (i.e. "..") will not work if
3760 * the node gets flushed out of the cache.
3766 /* get usecount and drop iocount */
3767 error
= vnode_ref(*vpp
);
3770 vnode_recycle(*vpp
);
3775 * Do statfs to ensure static info gets set to reasonable values.
3777 if ((error
= nmp
->nm_funcs
->nf_update_statfs(nmp
, ctx
))) {
3778 int error2
= vnode_getwithref(*vpp
);
3783 vnode_recycle(*vpp
);
3786 sbp
= vfs_statfs(mp
);
3787 sbp
->f_bsize
= nmp
->nm_fsattr
.nfsa_bsize
;
3788 sbp
->f_blocks
= nmp
->nm_fsattr
.nfsa_space_total
/ sbp
->f_bsize
;
3789 sbp
->f_bfree
= nmp
->nm_fsattr
.nfsa_space_free
/ sbp
->f_bsize
;
3790 sbp
->f_bavail
= nmp
->nm_fsattr
.nfsa_space_avail
/ sbp
->f_bsize
;
3791 sbp
->f_bused
= (nmp
->nm_fsattr
.nfsa_space_total
/ sbp
->f_bsize
) -
3792 (nmp
->nm_fsattr
.nfsa_space_free
/ sbp
->f_bsize
);
3793 sbp
->f_files
= nmp
->nm_fsattr
.nfsa_files_total
;
3794 sbp
->f_ffree
= nmp
->nm_fsattr
.nfsa_files_free
;
3795 sbp
->f_iosize
= nfs_iosize
;
3797 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SET_MOUNT_OWNER
)) {
3798 sbp
->f_owner
= set_owner
;
3802 * Calculate the size used for I/O buffers. Use the larger
3803 * of the two sizes to minimise NFS requests but make sure
3804 * that it is at least one VM page to avoid wasting buffer
3805 * space and to allow easy mmapping of I/O buffers.
3806 * The read/write RPC calls handle the splitting up of
3807 * buffers into multiple requests if the buffer size is
3808 * larger than the I/O size.
3810 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
3811 if (iosize
< PAGE_SIZE
) {
3814 nmp
->nm_biosize
= trunc_page_32(iosize
);
3816 /* For NFSv3 and greater, there is a (relatively) reliable ACCESS call. */
3817 if (nmp
->nm_vers
> NFS_VER2
&& !NMFLAG(nmp
, NOOPAQUE_AUTH
)
3819 vfs_setauthopaqueaccess(mp
);
3822 switch (nmp
->nm_lockmode
) {
3823 case NFS_LOCK_MODE_DISABLED
:
3825 case NFS_LOCK_MODE_LOCAL
:
3826 vfs_setlocklocal(nmp
->nm_mountp
);
3828 case NFS_LOCK_MODE_ENABLED
:
3830 if (nmp
->nm_vers
<= NFS_VER3
) {
3831 nfs_lockd_mount_register(nmp
);
3838 lck_mtx_lock(&nmp
->nm_lock
);
3839 nmp
->nm_state
|= NFSSTA_MOUNTED
;
3840 lck_mtx_unlock(&nmp
->nm_lock
);
3843 nfs_mount_drain_and_cleanup(nmp
);
3850 * We've detected a file system boundary on the server and
3851 * need to mount a new file system so that our file systems
3852 * MIRROR the file systems on the server.
3854 * Build the mount arguments for the new mount and call kernel_mount().
3857 nfs_mirror_mount_domount(vnode_t dvp
, vnode_t vp
, vfs_context_t ctx
)
3859 nfsnode_t np
= VTONFS(vp
);
3861 nfsnode_t dnp
= VTONFS(dvp
);
3863 struct nfsmount
*nmp
= NFSTONMP(np
);
3864 char fstype
[MFSTYPENAMELEN
], *mntfromname
= NULL
, *path
= NULL
, *relpath
, *p
, *cp
;
3865 int error
= 0, pathbuflen
= MAXPATHLEN
, i
, mntflags
= 0, referral
, skipcopy
= 0;
3866 size_t nlen
, rlen
, mlen
, mlen2
, count
;
3867 struct xdrbuf xb
, xbnew
;
3868 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
3869 uint32_t newmattrs
[NFS_MATTR_BITMAP_LEN
];
3870 uint32_t newmflags
[NFS_MFLAG_BITMAP_LEN
];
3871 uint32_t newmflags_mask
[NFS_MFLAG_BITMAP_LEN
];
3872 uint32_t val
, relpathcomps
;
3873 uint64_t argslength
= 0, argslength_offset
, attrslength_offset
, end_offset
;
3874 uint32_t numlocs
, loc
, numserv
, serv
, numaddr
, addr
, numcomp
, comp
;
3876 struct nfs_fs_locations nfsls
;
3878 referral
= (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
);
3880 bzero(&nfsls
, sizeof(nfsls
));
3883 xb_init(&xbnew
, XDRBUF_NONE
);
3885 if (!nmp
|| (nmp
->nm_state
& (NFSSTA_FORCE
| NFSSTA_DEAD
))) {
3889 /* allocate a couple path buffers we need */
3890 mntfromname
= zalloc(ZV_NAMEI
);
3891 path
= zalloc(ZV_NAMEI
);
3893 /* get the path for the directory being mounted on */
3894 error
= vn_getpath(vp
, path
, &pathbuflen
);
3901 * Set up the mntfromname for the new mount based on the
3902 * current mount's mntfromname and the directory's path
3903 * relative to the current mount's mntonname.
3904 * Set up relpath to point at the relative path on the current mount.
3905 * Also, count the number of components in relpath.
3906 * We'll be adding those to each fs location path in the new args.
3908 nlen
= strlcpy(mntfromname
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, MAXPATHLEN
);
3909 if ((nlen
> 0) && (mntfromname
[nlen
- 1] == '/')) { /* avoid double '/' in new name */
3910 mntfromname
[nlen
- 1] = '\0';
3913 relpath
= mntfromname
+ nlen
;
3914 nlen
= strlcat(mntfromname
, path
+ strlen(vfs_statfs(nmp
->nm_mountp
)->f_mntonname
), MAXPATHLEN
);
3915 if (nlen
>= MAXPATHLEN
) {
3916 error
= ENAMETOOLONG
;
3919 /* count the number of components in relpath */
3921 while (*p
&& (*p
== '/')) {
3927 while (*p
&& (*p
!= '/')) {
3930 while (*p
&& (*p
== '/')) {
3935 /* grab a copy of the file system type */
3936 vfs_name(vnode_mount(vp
), fstype
);
3938 /* for referrals, fetch the fs locations */
3940 const char *vname
= vnode_getname(NFSTOV(np
));
3946 error
= nfs4_get_fs_locations(nmp
, dnp
, NULL
, 0, vname
, ctx
, &nfsls
);
3947 vnode_putname(vname
);
3948 if (!error
&& (nfsls
.nl_numlocs
< 1)) {
3956 /* set up NFS mount args based on current mount args */
3958 #define xb_copy_32(E, XBSRC, XBDST, V) \
3961 xb_get_32((E), (XBSRC), (V)); \
3962 if (skipcopy) break; \
3963 xb_add_32((E), (XBDST), (V)); \
3965 #define xb_copy_opaque(E, XBSRC, XBDST) \
3967 uint32_t __count = 0, __val; \
3968 xb_copy_32((E), (XBSRC), (XBDST), __count); \
3970 __count = nfsm_rndup(__count); \
3971 __count /= XDRWORD; \
3972 while (__count-- > 0) \
3973 xb_copy_32((E), (XBSRC), (XBDST), __val); \
3976 xb_init_buffer(&xb
, nmp
->nm_args
, 2 * XDRWORD
);
3977 xb_get_32(error
, &xb
, val
); /* version */
3978 xb_get_32(error
, &xb
, argslength
); /* args length */
3979 xb_init_buffer(&xb
, nmp
->nm_args
, argslength
);
3981 xb_init_buffer(&xbnew
, NULL
, 0);
3982 xb_copy_32(error
, &xb
, &xbnew
, val
); /* version */
3983 argslength_offset
= xb_offset(&xbnew
);
3984 xb_copy_32(error
, &xb
, &xbnew
, val
); /* args length */
3985 xb_copy_32(error
, &xb
, &xbnew
, val
); /* XDR args version */
3986 count
= NFS_MATTR_BITMAP_LEN
;
3987 xb_get_bitmap(error
, &xb
, mattrs
, count
); /* mount attribute bitmap */
3989 for (i
= 0; i
< NFS_MATTR_BITMAP_LEN
; i
++) {
3990 newmattrs
[i
] = mattrs
[i
];
3993 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FS_LOCATIONS
);
3994 NFS_BITMAP_CLR(newmattrs
, NFS_MATTR_MNTFROM
);
3996 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FH
);
3998 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_FLAGS
);
3999 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_MNTFLAGS
);
4000 NFS_BITMAP_SET(newmattrs
, NFS_MATTR_SET_MOUNT_OWNER
);
4001 xb_add_bitmap(error
, &xbnew
, newmattrs
, NFS_MATTR_BITMAP_LEN
);
4002 attrslength_offset
= xb_offset(&xbnew
);
4003 xb_copy_32(error
, &xb
, &xbnew
, val
); /* attrs length */
4004 NFS_BITMAP_ZERO(newmflags_mask
, NFS_MFLAG_BITMAP_LEN
);
4005 NFS_BITMAP_ZERO(newmflags
, NFS_MFLAG_BITMAP_LEN
);
4006 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FLAGS
)) {
4007 count
= NFS_MFLAG_BITMAP_LEN
;
4008 xb_get_bitmap(error
, &xb
, newmflags_mask
, count
); /* mount flag mask bitmap */
4009 count
= NFS_MFLAG_BITMAP_LEN
;
4010 xb_get_bitmap(error
, &xb
, newmflags
, count
); /* mount flag bitmap */
4012 NFS_BITMAP_SET(newmflags_mask
, NFS_MFLAG_EPHEMERAL
);
4013 NFS_BITMAP_SET(newmflags
, NFS_MFLAG_EPHEMERAL
);
4014 xb_add_bitmap(error
, &xbnew
, newmflags_mask
, NFS_MFLAG_BITMAP_LEN
);
4015 xb_add_bitmap(error
, &xbnew
, newmflags
, NFS_MFLAG_BITMAP_LEN
);
4016 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION
)) {
4017 xb_copy_32(error
, &xb
, &xbnew
, val
);
4019 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
)) {
4020 xb_copy_32(error
, &xb
, &xbnew
, val
);
4022 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_VERSION_RANGE
)) {
4023 xb_copy_32(error
, &xb
, &xbnew
, val
);
4024 xb_copy_32(error
, &xb
, &xbnew
, val
);
4026 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READ_SIZE
)) {
4027 xb_copy_32(error
, &xb
, &xbnew
, val
);
4029 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_WRITE_SIZE
)) {
4030 xb_copy_32(error
, &xb
, &xbnew
, val
);
4032 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READDIR_SIZE
)) {
4033 xb_copy_32(error
, &xb
, &xbnew
, val
);
4035 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_READAHEAD
)) {
4036 xb_copy_32(error
, &xb
, &xbnew
, val
);
4038 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
)) {
4039 xb_copy_32(error
, &xb
, &xbnew
, val
);
4040 xb_copy_32(error
, &xb
, &xbnew
, val
);
4042 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
)) {
4043 xb_copy_32(error
, &xb
, &xbnew
, val
);
4044 xb_copy_32(error
, &xb
, &xbnew
, val
);
4046 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
)) {
4047 xb_copy_32(error
, &xb
, &xbnew
, val
);
4048 xb_copy_32(error
, &xb
, &xbnew
, val
);
4050 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
)) {
4051 xb_copy_32(error
, &xb
, &xbnew
, val
);
4052 xb_copy_32(error
, &xb
, &xbnew
, val
);
4054 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCK_MODE
)) {
4055 xb_copy_32(error
, &xb
, &xbnew
, val
);
4057 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SECURITY
)) {
4058 xb_copy_32(error
, &xb
, &xbnew
, count
);
4059 while (!error
&& (count
-- > 0)) {
4060 xb_copy_32(error
, &xb
, &xbnew
, val
);
4063 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_KERB_ETYPE
)) {
4064 xb_copy_32(error
, &xb
, &xbnew
, count
);
4065 xb_add_32(error
, &xbnew
, -1);
4066 while (!error
&& (count
-- > 0)) {
4067 xb_copy_32(error
, &xb
, &xbnew
, val
);
4070 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
)) {
4071 xb_copy_32(error
, &xb
, &xbnew
, val
);
4073 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOCKET_TYPE
)) {
4074 xb_copy_opaque(error
, &xb
, &xbnew
);
4076 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_NFS_PORT
)) {
4077 xb_copy_32(error
, &xb
, &xbnew
, val
);
4079 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MOUNT_PORT
)) {
4080 xb_copy_32(error
, &xb
, &xbnew
, val
);
4082 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
)) {
4083 xb_copy_32(error
, &xb
, &xbnew
, val
);
4084 xb_copy_32(error
, &xb
, &xbnew
, val
);
4086 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
)) {
4087 xb_copy_32(error
, &xb
, &xbnew
, val
);
4089 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
)) {
4090 xb_copy_32(error
, &xb
, &xbnew
, val
);
4091 xb_copy_32(error
, &xb
, &xbnew
, val
);
4093 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FH
)) {
4094 xb_get_32(error
, &xb
, count
);
4095 xb_skip(error
, &xb
, count
);
4098 /* set the initial file handle to the directory's file handle */
4099 xb_add_fh(error
, &xbnew
, np
->n_fhp
, np
->n_fhsize
);
4101 /* copy/extend/skip fs locations */
4102 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_FS_LOCATIONS
)) {
4103 numlocs
= numserv
= numaddr
= numcomp
= 0;
4104 if (referral
) { /* don't copy the fs locations for a referral */
4107 xb_copy_32(error
, &xb
, &xbnew
, numlocs
); /* location count */
4108 for (loc
= 0; !error
&& (loc
< numlocs
); loc
++) {
4109 xb_copy_32(error
, &xb
, &xbnew
, numserv
); /* server count */
4110 for (serv
= 0; !error
&& (serv
< numserv
); serv
++) {
4111 xb_copy_opaque(error
, &xb
, &xbnew
); /* server name */
4112 xb_copy_32(error
, &xb
, &xbnew
, numaddr
); /* address count */
4113 for (addr
= 0; !error
&& (addr
< numaddr
); addr
++) {
4114 xb_copy_opaque(error
, &xb
, &xbnew
); /* address */
4116 xb_copy_opaque(error
, &xb
, &xbnew
); /* server info */
4119 xb_get_32(error
, &xb
, numcomp
); /* component count */
4121 uint64_t totalcomps
= numcomp
+ relpathcomps
;
4123 /* set error to ERANGE in the event of overflow */
4124 if (totalcomps
> UINT32_MAX
) {
4125 nfsmerr_if((error
= ERANGE
));
4128 xb_add_32(error
, &xbnew
, (uint32_t) totalcomps
); /* new component count */
4130 for (comp
= 0; !error
&& (comp
< numcomp
); comp
++) {
4131 xb_copy_opaque(error
, &xb
, &xbnew
); /* component */
4133 /* add additional components */
4135 while (*p
&& (*p
== '/')) {
4138 while (*p
&& !error
) {
4140 while (*p
&& (*p
!= '/')) {
4143 xb_add_string(error
, &xbnew
, cp
, (p
- cp
)); /* component */
4144 while (*p
&& (*p
== '/')) {
4148 xb_copy_opaque(error
, &xb
, &xbnew
); /* fs location info */
4155 /* add referral's fs locations */
4156 xb_add_32(error
, &xbnew
, nfsls
.nl_numlocs
); /* FS_LOCATIONS */
4157 for (loc
= 0; !error
&& (loc
< nfsls
.nl_numlocs
); loc
++) {
4158 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servcount
);
4159 for (serv
= 0; !error
&& (serv
< nfsls
.nl_locations
[loc
]->nl_servcount
); serv
++) {
4160 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
,
4161 strlen(nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
));
4162 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
);
4163 for (addr
= 0; !error
&& (addr
< nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
); addr
++) {
4164 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
],
4165 strlen(nfsls
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
]));
4167 xb_add_32(error
, &xbnew
, 0); /* empty server info */
4169 xb_add_32(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_path
.np_compcount
);
4170 for (comp
= 0; !error
&& (comp
< nfsls
.nl_locations
[loc
]->nl_path
.np_compcount
); comp
++) {
4171 xb_add_string(error
, &xbnew
, nfsls
.nl_locations
[loc
]->nl_path
.np_components
[comp
],
4172 strlen(nfsls
.nl_locations
[loc
]->nl_path
.np_components
[comp
]));
4174 xb_add_32(error
, &xbnew
, 0); /* empty fs location info */
4177 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFLAGS
)) {
4178 xb_get_32(error
, &xb
, mntflags
);
4181 * We add the following mount flags to the ones for the mounted-on mount:
4182 * MNT_DONTBROWSE - to keep the mount from showing up as a separate volume
4183 * MNT_AUTOMOUNTED - to keep DiskArb from retriggering the mount after
4184 * an unmount (looking for /.autodiskmounted)
4186 mntflags
|= (MNT_AUTOMOUNTED
| MNT_DONTBROWSE
);
4187 xb_add_32(error
, &xbnew
, mntflags
);
4188 if (!referral
&& NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MNTFROM
)) {
4189 /* copy mntfrom string and add relpath */
4190 rlen
= strlen(relpath
);
4191 xb_get_32(error
, &xb
, mlen
);
4193 mlen2
= mlen
+ ((relpath
[0] != '/') ? 1 : 0) + rlen
;
4194 xb_add_32(error
, &xbnew
, mlen2
);
4195 count
= mlen
/ XDRWORD
;
4196 /* copy the original string */
4197 while (count
-- > 0) {
4198 xb_copy_32(error
, &xb
, &xbnew
, val
);
4200 if (!error
&& (mlen
% XDRWORD
)) {
4201 error
= xb_get_bytes(&xb
, buf
, mlen
% XDRWORD
, 0);
4203 error
= xb_add_bytes(&xbnew
, buf
, mlen
% XDRWORD
, 1);
4206 /* insert a '/' if the relative path doesn't start with one */
4207 if (!error
&& (relpath
[0] != '/')) {
4209 error
= xb_add_bytes(&xbnew
, buf
, 1, 1);
4211 /* add the additional relative path */
4213 error
= xb_add_bytes(&xbnew
, relpath
, rlen
, 1);
4215 /* make sure the resulting string has the right number of pad bytes */
4216 if (!error
&& (mlen2
!= nfsm_rndup(mlen2
))) {
4217 bzero(buf
, sizeof(buf
));
4218 count
= nfsm_rndup(mlen2
) - mlen2
;
4219 error
= xb_add_bytes(&xbnew
, buf
, count
, 1);
4223 * The following string copies rely on the fact that we already validated
4224 * these data when creating the initial mount point.
4226 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REALM
)) {
4227 xb_add_string(error
, &xbnew
, nmp
->nm_realm
, strlen(nmp
->nm_realm
));
4229 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_PRINCIPAL
)) {
4230 xb_add_string(error
, &xbnew
, nmp
->nm_principal
, strlen(nmp
->nm_principal
));
4232 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SVCPRINCIPAL
)) {
4233 xb_add_string(error
, &xbnew
, nmp
->nm_sprinc
, strlen(nmp
->nm_sprinc
));
4235 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_NFS_PORT
)) {
4236 xb_add_string(error
, &xbnew
, nmp
->nm_nfs_localport
, strlen(nmp
->nm_nfs_localport
));
4238 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_MOUNT_PORT
)) {
4239 xb_add_string(error
, &xbnew
, nmp
->nm_mount_localport
, strlen(nmp
->nm_mount_localport
));
4241 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SET_MOUNT_OWNER
)) {
4242 /* drop embedded owner value */
4243 xb_get_32(error
, &xb
, count
);
4245 /* New mount always gets same owner as this mount */
4246 xb_add_32(error
, &xbnew
, vnode_mount(vp
)->mnt_vfsstat
.f_owner
);
4247 xb_build_done(error
, &xbnew
);
4249 /* update opaque counts */
4250 end_offset
= xb_offset(&xbnew
);
4252 error
= xb_seek(&xbnew
, argslength_offset
);
4253 argslength
= end_offset
- argslength_offset
+ XDRWORD
/*version*/;
4254 xb_add_32(error
, &xbnew
, argslength
);
4257 error
= xb_seek(&xbnew
, attrslength_offset
);
4258 xb_add_32(error
, &xbnew
, end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
4263 * For kernel_mount() call, use the existing mount flags (instead of the
4264 * original flags) because flags like MNT_NOSUID and MNT_NODEV may have
4265 * been silently enforced. Also, in terms of MACF, the _kernel_ is
4266 * performing the mount (and enforcing all of the mount options), so we
4267 * use the kernel context for the mount call.
4269 mntflags
= vnode_vfsvisflags(vp
);
4270 mntflags
|= (MNT_AUTOMOUNTED
| MNT_DONTBROWSE
);
4271 ctx
= vfs_context_kernel();
4274 error
= kernel_mount(fstype
, dvp
, vp
, path
, xb_buffer_base(&xbnew
), argslength
,
4275 mntflags
, KERNEL_MOUNT_PERMIT_UNMOUNT
| KERNEL_MOUNT_NOAUTH
, ctx
);
4279 printf("nfs: mirror mount of %s on %s failed (%d)\n",
4280 mntfromname
, path
, error
);
4285 nfs_fs_locations_cleanup(&nfsls
);
4288 NFS_ZFREE(ZV_NAMEI
, path
);
4291 NFS_ZFREE(ZV_NAMEI
, mntfromname
);
4294 nfs_ephemeral_mount_harvester_start();
4300 * trigger vnode functions
4302 #define NFS_TRIGGER_DEBUG 1
4305 nfs_mirror_mount_trigger_resolve(
4307 const struct componentname
*cnp
,
4308 enum path_operation pop
,
4310 __unused
void *data
,
4313 nfsnode_t np
= VTONFS(vp
);
4314 vnode_t pvp
= NULLVP
;
4317 resolver_result_t result
;
4320 * We have a trigger node that doesn't have anything mounted on it yet.
4321 * We'll do the mount if either:
4322 * (a) this isn't the last component of the path OR
4323 * (b) this is an op that looks like it should trigger the mount.
4325 if (cnp
->cn_flags
& ISLASTCN
) {
4343 /* don't perform the mount for these operations */
4344 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_NOCHANGE
, 0);
4345 #ifdef NFS_TRIGGER_DEBUG
4346 NP(np
, "nfs trigger RESOLVE: no change, last %d nameiop %d, seq %d",
4347 (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
4358 case OP_EXCHANGEDATA
:
4362 case OP_REMOVEXATTR
:
4364 /* go ahead and do the mount */
4369 if (vnode_mountedhere(vp
) != NULL
) {
4371 * Um... there's already something mounted.
4372 * Been there. Done that. Let's just say it succeeded.
4378 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
4379 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
4380 #ifdef NFS_TRIGGER_DEBUG
4381 NP(np
, "nfs trigger RESOLVE: busy error %d, last %d nameiop %d, seq %d",
4382 error
, (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
4388 /* Check again, in case the mount happened while we were setting busy */
4389 if (vnode_mountedhere(vp
) != NULL
) {
4390 /* Been there. Done that. Let's just say it succeeded. */
4394 nfs_node_lock_force(np
);
4395 if (np
->n_flag
& NDISARMTRIGGER
) {
4397 nfs_node_unlock(np
);
4400 nfs_node_unlock(np
);
4402 pvp
= vnode_getparent(vp
);
4403 if (pvp
== NULLVP
) {
4407 error
= nfs_mirror_mount_domount(pvp
, vp
, ctx
);
4413 result
= vfs_resolver_result(np
->n_trigseq
, error
? RESOLVER_ERROR
: RESOLVER_RESOLVED
, error
);
4414 #ifdef NFS_TRIGGER_DEBUG
4415 NP(np
, "nfs trigger RESOLVE: %s %d, last %d nameiop %d, seq %d",
4416 error
? "error" : "resolved", error
,
4417 (cnp
->cn_flags
& ISLASTCN
) ? 1 : 0, cnp
->cn_nameiop
, np
->n_trigseq
);
4420 if (pvp
!= NULLVP
) {
4424 nfs_node_clear_busy(np
);
4430 nfs_mirror_mount_trigger_unresolve(
4433 __unused
void *data
,
4436 nfsnode_t np
= VTONFS(vp
);
4439 resolver_result_t result
;
4441 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
4442 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
4443 #ifdef NFS_TRIGGER_DEBUG
4444 NP(np
, "nfs trigger UNRESOLVE: busy error %d, seq %d", error
, np
->n_trigseq
);
4449 mp
= vnode_mountedhere(vp
);
4454 error
= vfs_unmountbyfsid(&(vfs_statfs(mp
)->f_fsid
), flags
, ctx
);
4459 result
= vfs_resolver_result(np
->n_trigseq
, error
? RESOLVER_ERROR
: RESOLVER_UNRESOLVED
, error
);
4460 #ifdef NFS_TRIGGER_DEBUG
4461 NP(np
, "nfs trigger UNRESOLVE: %s %d, seq %d",
4462 error
? "error" : "unresolved", error
, np
->n_trigseq
);
4464 nfs_node_clear_busy(np
);
4469 nfs_mirror_mount_trigger_rearm(
4472 __unused
void *data
,
4475 nfsnode_t np
= VTONFS(vp
);
4477 resolver_result_t result
;
4479 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
)))) {
4480 result
= vfs_resolver_result(np
->n_trigseq
, RESOLVER_ERROR
, error
);
4481 #ifdef NFS_TRIGGER_DEBUG
4482 NP(np
, "nfs trigger REARM: busy error %d, seq %d", error
, np
->n_trigseq
);
4488 result
= vfs_resolver_result(np
->n_trigseq
,
4489 vnode_mountedhere(vp
) ? RESOLVER_RESOLVED
: RESOLVER_UNRESOLVED
, 0);
4490 #ifdef NFS_TRIGGER_DEBUG
4491 NP(np
, "nfs trigger REARM: %s, seq %d",
4492 vnode_mountedhere(vp
) ? "resolved" : "unresolved", np
->n_trigseq
);
4494 nfs_node_clear_busy(np
);
4499 * Periodically attempt to unmount ephemeral (mirror) mounts in an attempt to limit
4500 * the number of unused mounts.
4503 #define NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL 120 /* how often the harvester runs */
4504 struct nfs_ephemeral_mount_harvester_info
{
4505 fsid_t fsid
; /* FSID that we need to try to unmount */
4506 uint32_t mountcount
; /* count of ephemeral mounts seen in scan */
4508 /* various globals for the harvester */
4509 static thread_call_t nfs_ephemeral_mount_harvester_timer
= NULL
;
4510 static int nfs_ephemeral_mount_harvester_on
= 0;
4512 kern_return_t
thread_terminate(thread_t
);
4515 nfs_ephemeral_mount_harvester_callback(mount_t mp
, void *arg
)
4517 struct nfs_ephemeral_mount_harvester_info
*hinfo
= arg
;
4518 struct nfsmount
*nmp
;
4521 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs")) {
4522 return VFS_RETURNED
;
4525 if (!nmp
|| !NMFLAG(nmp
, EPHEMERAL
)
4527 return VFS_RETURNED
;
4529 hinfo
->mountcount
++;
4531 /* avoid unmounting mounts that have been triggered within the last harvest interval */
4533 if ((nmp
->nm_mounttime
>> 32) > ((uint32_t)now
.tv_sec
- NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
)) {
4534 return VFS_RETURNED
;
4537 if (hinfo
->fsid
.val
[0] || hinfo
->fsid
.val
[1]) {
4538 /* attempt to unmount previously-found ephemeral mount */
4539 vfs_unmountbyfsid(&hinfo
->fsid
, 0, vfs_context_kernel());
4540 hinfo
->fsid
.val
[0] = hinfo
->fsid
.val
[1] = 0;
4544 * We can't call unmount here since we hold a mount iter ref
4545 * on mp so save its fsid for the next call iteration to unmount.
4547 hinfo
->fsid
.val
[0] = mp
->mnt_vfsstat
.f_fsid
.val
[0];
4548 hinfo
->fsid
.val
[1] = mp
->mnt_vfsstat
.f_fsid
.val
[1];
4550 return VFS_RETURNED
;
4554 * Spawn a thread to do the ephemeral mount harvesting.
4557 nfs_ephemeral_mount_harvester_timer_func(void)
4561 if (kernel_thread_start(nfs_ephemeral_mount_harvester
, NULL
, &thd
) == KERN_SUCCESS
) {
4562 thread_deallocate(thd
);
4567 * Iterate all mounts looking for NFS ephemeral mounts to try to unmount.
4570 nfs_ephemeral_mount_harvester(__unused
void *arg
, __unused wait_result_t wr
)
4572 struct nfs_ephemeral_mount_harvester_info hinfo
;
4575 hinfo
.mountcount
= 0;
4576 hinfo
.fsid
.val
[0] = hinfo
.fsid
.val
[1] = 0;
4577 vfs_iterate(VFS_ITERATE_TAIL_FIRST
, nfs_ephemeral_mount_harvester_callback
, &hinfo
);
4578 if (hinfo
.fsid
.val
[0] || hinfo
.fsid
.val
[1]) {
4579 /* attempt to unmount last found ephemeral mount */
4580 vfs_unmountbyfsid(&hinfo
.fsid
, 0, vfs_context_kernel());
4583 lck_mtx_lock(&nfs_global_mutex
);
4584 if (!hinfo
.mountcount
) {
4585 /* no more ephemeral mounts - don't need timer */
4586 nfs_ephemeral_mount_harvester_on
= 0;
4588 /* re-arm the timer */
4589 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
, NSEC_PER_SEC
, &deadline
);
4590 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer
, deadline
);
4591 nfs_ephemeral_mount_harvester_on
= 1;
4593 lck_mtx_unlock(&nfs_global_mutex
);
4596 thread_terminate(current_thread());
4600 * Make sure the NFS ephemeral mount harvester timer is running.
4603 nfs_ephemeral_mount_harvester_start(void)
4607 lck_mtx_lock(&nfs_global_mutex
);
4608 if (nfs_ephemeral_mount_harvester_on
) {
4609 lck_mtx_unlock(&nfs_global_mutex
);
4612 if (nfs_ephemeral_mount_harvester_timer
== NULL
) {
4613 nfs_ephemeral_mount_harvester_timer
= thread_call_allocate((thread_call_func_t
)nfs_ephemeral_mount_harvester_timer_func
, NULL
);
4615 clock_interval_to_deadline(NFS_EPHEMERAL_MOUNT_HARVEST_INTERVAL
, NSEC_PER_SEC
, &deadline
);
4616 thread_call_enter_delayed(nfs_ephemeral_mount_harvester_timer
, deadline
);
4617 nfs_ephemeral_mount_harvester_on
= 1;
4618 lck_mtx_unlock(&nfs_global_mutex
);
4624 * Send a STAT protocol request to the server to verify statd is running.
4625 * rpc-statd service, which responsible to provide locks for the NFS server, is disabled by default on Ubuntu.
4626 * Please see Radar 45969553 for more info.
4629 nfs3_check_lockmode(struct nfsmount
*nmp
, struct sockaddr
*sa
, int sotype
, int timeo
)
4631 struct sockaddr_storage ss
;
4632 int error
, port
= 0;
4634 if (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
) {
4635 if (sa
->sa_len
> sizeof(ss
)) {
4638 bcopy(sa
, &ss
, MIN(sa
->sa_len
, sizeof(ss
)));
4639 error
= nfs_portmap_lookup(nmp
, vfs_context_current(), (struct sockaddr
*)&ss
, NULL
, RPCPROG_STAT
, RPCMNT_VER1
, NM_OMFLAG(nmp
, MNTUDP
) ? SOCK_DGRAM
: sotype
, timeo
);
4641 if (ss
.ss_family
== AF_INET
) {
4642 port
= ntohs(((struct sockaddr_in
*)&ss
)->sin_port
);
4643 } else if (ss
.ss_family
== AF_INET6
) {
4644 port
= ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
);
4645 } else if (ss
.ss_family
== AF_LOCAL
) {
4646 port
= (((struct sockaddr_un
*)&ss
)->sun_path
[0] != '\0');
4650 printf("nfs: STAT(NSM) rpc service is not available, unable to mount with current lock mode.\n");
4651 return EPROGUNAVAIL
;
4659 * Send a MOUNT protocol MOUNT request to the server to get the initial file handle (and security).
4662 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
)
4664 int error
= 0, mntproto
;
4665 thread_t thd
= vfs_context_thread(ctx
);
4666 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4669 struct nfsm_chain nmreq
, nmrep
;
4671 uint32_t mntvers
, mntport
, val
;
4672 struct sockaddr_storage ss
;
4673 struct sockaddr
*saddr
= (struct sockaddr
*)&ss
;
4674 struct sockaddr_un
*sun
= (struct sockaddr_un
*)saddr
;
4676 nfsm_chain_null(&nmreq
);
4677 nfsm_chain_null(&nmrep
);
4679 mntvers
= (nfsvers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4680 mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
4683 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
4684 if (saddr
->sa_family
== AF_INET
) {
4685 if (nmp
->nm_mountport
) {
4686 ((struct sockaddr_in
*)saddr
)->sin_port
= htons(nmp
->nm_mountport
);
4688 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4689 } else if (saddr
->sa_family
== AF_INET6
) {
4690 if (nmp
->nm_mountport
) {
4691 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(nmp
->nm_mountport
);
4693 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4694 } else { /* Local domain socket */
4695 mntport
= ((struct sockaddr_un
*)saddr
)->sun_path
[0]; /* Do we have and address? */
4696 mntproto
= IPPROTO_TCP
; /* XXX rpcbind only listens on streams sockets for now */
4700 error
= nfs_portmap_lookup(nmp
, ctx
, saddr
, NULL
, RPCPROG_MNT
, mntvers
,
4701 mntproto
== IPPROTO_UDP
? SOCK_DGRAM
: SOCK_STREAM
, timeo
);
4703 if (saddr
->sa_family
== AF_INET
) {
4704 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4705 } else if (saddr
->sa_family
== AF_INET6
) {
4706 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4707 } else if (saddr
->sa_family
== AF_LOCAL
) {
4708 mntport
= ((struct sockaddr_un
*)saddr
)->sun_path
[0];
4711 /* if not found and TCP, then retry with UDP */
4712 if (mntproto
== IPPROTO_UDP
) {
4713 error
= EPROGUNAVAIL
;
4716 mntproto
= IPPROTO_UDP
;
4717 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
4718 if (saddr
->sa_family
== AF_LOCAL
) {
4719 strlcpy(sun
->sun_path
, RPCB_TICLTS_PATH
, sizeof(sun
->sun_path
));
4723 nfsmout_if(error
|| !mntport
);
4725 /* MOUNT protocol MOUNT request */
4726 slen
= strlen(path
);
4727 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_UNSIGNED
+ nfsm_rndup(slen
));
4728 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
4729 nfsm_chain_build_done(error
, &nmreq
);
4731 error
= nfsm_rpchead2(nmp
, (mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4732 RPCPROG_MNT
, mntvers
, RPCMNT_MOUNT
,
4733 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
4735 nmreq
.nmc_mhead
= NULL
;
4736 error
= nfs_aux_request(nmp
, thd
, saddr
, NULL
,
4737 ((mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
),
4738 mreq
, R_XID32(xid
), 1, timeo
, &nmrep
);
4740 nfsm_chain_get_32(error
, &nmrep
, val
);
4741 if (!error
&& val
) {
4745 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fh
);
4746 if (!error
&& (nfsvers
> NFS_VER2
)) {
4747 sec
->count
= NX_MAX_SEC_FLAVORS
;
4748 error
= nfsm_chain_get_secinfo(&nmrep
, &sec
->flavors
[0], &sec
->count
);
4751 nfsm_chain_cleanup(&nmreq
);
4752 nfsm_chain_cleanup(&nmrep
);
4758 * Send a MOUNT protocol UNMOUNT request to tell the server we've unmounted it.
4761 nfs3_umount_rpc(struct nfsmount
*nmp
, vfs_context_t ctx
, int timeo
)
4763 int error
= 0, mntproto
;
4764 thread_t thd
= vfs_context_thread(ctx
);
4765 kauth_cred_t cred
= vfs_context_ucred(ctx
);
4769 struct nfsm_chain nmreq
, nmrep
;
4773 struct sockaddr_storage ss
;
4774 struct sockaddr
*saddr
= (struct sockaddr
*)&ss
;
4776 if (!nmp
->nm_saddr
) {
4780 nfsm_chain_null(&nmreq
);
4781 nfsm_chain_null(&nmrep
);
4783 mntvers
= (nmp
->nm_vers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4784 mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (nmp
->nm_sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
4785 mntport
= nmp
->nm_mountport
;
4787 bcopy(nmp
->nm_saddr
, saddr
, min(sizeof(ss
), nmp
->nm_saddr
->sa_len
));
4788 if (saddr
->sa_family
== AF_INET
) {
4789 ((struct sockaddr_in
*)saddr
)->sin_port
= htons(mntport
);
4790 } else if (saddr
->sa_family
== AF_INET6
) {
4791 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(mntport
);
4792 } else { /* Local domain socket */
4793 mntport
= ((struct sockaddr_un
*)saddr
)->sun_path
[0]; /* Do we have and address? */
4797 error
= nfs_portmap_lookup(nmp
, ctx
, saddr
, NULL
, RPCPROG_MNT
, mntvers
, mntproto
, timeo
);
4799 if (saddr
->sa_family
== AF_INET
) {
4800 mntport
= ntohs(((struct sockaddr_in
*)saddr
)->sin_port
);
4801 } else if (saddr
->sa_family
== AF_INET6
) {
4802 mntport
= ntohs(((struct sockaddr_in6
*)saddr
)->sin6_port
);
4803 } else { /* Local domain socket */
4804 mntport
= ((struct sockaddr_un
*)saddr
)->sun_path
[0]; /* Do we have and address? */
4806 /* if not found and mntvers > VER1, then retry with VER1 */
4808 if (mntvers
> RPCMNT_VER1
) {
4809 mntvers
= RPCMNT_VER1
;
4810 } else if (mntproto
== IPPROTO_TCP
) {
4811 mntproto
= IPPROTO_UDP
;
4812 mntvers
= (nmp
->nm_vers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
4816 bcopy(nmp
->nm_saddr
, saddr
, min(sizeof(ss
), nmp
->nm_saddr
->sa_len
));
4819 nfsmout_if(!mntport
);
4821 /* MOUNT protocol UNMOUNT request */
4822 path
= &vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
[0];
4823 while (*path
&& (*path
!= '/')) {
4826 slen
= strlen(path
);
4827 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_UNSIGNED
+ nfsm_rndup(slen
));
4828 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
4829 nfsm_chain_build_done(error
, &nmreq
);
4831 error
= nfsm_rpchead2(nmp
, (mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
4832 RPCPROG_MNT
, RPCMNT_VER1
, RPCMNT_UMOUNT
,
4833 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
4835 nmreq
.nmc_mhead
= NULL
;
4836 error
= nfs_aux_request(nmp
, thd
, saddr
, NULL
,
4837 ((mntproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
),
4838 mreq
, R_XID32(xid
), 1, timeo
, &nmrep
);
4840 nfsm_chain_cleanup(&nmreq
);
4841 nfsm_chain_cleanup(&nmrep
);
4845 * unmount system call
4851 __unused vfs_context_t ctx
)
4853 struct nfsmount
*nmp
;
4855 int error
, flags
= 0;
4856 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
4859 lck_mtx_lock(&nmp
->nm_lock
);
4861 * Set the flag indicating that an unmount attempt is in progress.
4863 nmp
->nm_state
|= NFSSTA_UNMOUNTING
;
4865 * During a force unmount we want to...
4866 * Mark that we are doing a force unmount.
4867 * Make the mountpoint soft.
4869 if (mntflags
& MNT_FORCE
) {
4870 flags
|= FORCECLOSE
;
4871 nmp
->nm_state
|= NFSSTA_FORCE
;
4872 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_SOFT
);
4875 * Wait for any in-progress monitored node scan to complete.
4877 while (nmp
->nm_state
& NFSSTA_MONITOR_SCAN
) {
4878 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitmonscan", &ts
);
4881 * Goes something like this..
4882 * - Call vflush() to clear out vnodes for this file system,
4883 * except for the swap files. Deal with them in 2nd pass.
4884 * - Decrement reference on the vnode representing remote root.
4885 * - Clean up the NFS mount structure.
4887 vp
= NFSTOV(nmp
->nm_dnp
);
4888 lck_mtx_unlock(&nmp
->nm_lock
);
4891 * vflush will check for busy vnodes on mountpoint.
4892 * Will do the right thing for MNT_FORCE. That is, we should
4893 * not get EBUSY back.
4895 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
4896 if (mntflags
& MNT_FORCE
) {
4897 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
4899 if (vnode_isinuse(vp
, 1)) {
4902 error
= vflush(mp
, vp
, flags
);
4906 lck_mtx_lock(&nmp
->nm_lock
);
4907 nmp
->nm_state
&= ~NFSSTA_UNMOUNTING
;
4908 lck_mtx_unlock(&nmp
->nm_lock
);
4912 lck_mtx_lock(&nmp
->nm_lock
);
4914 lck_mtx_unlock(&nmp
->nm_lock
);
4917 * Release the root vnode reference held by mountnfs()
4919 error
= vnode_get(vp
);
4925 vflush(mp
, NULLVP
, FORCECLOSE
);
4927 /* Wait for all other references to be released and free the mount */
4928 nfs_mount_drain_and_cleanup(nmp
);
4934 * cleanup/destroy NFS fs locations structure
4937 nfs_fs_locations_cleanup(struct nfs_fs_locations
*nfslsp
)
4939 struct nfs_fs_location
*fsl
;
4940 struct nfs_fs_server
*fss
;
4941 struct nfs_fs_path
*fsp
;
4942 uint32_t loc
, serv
, addr
, comp
;
4944 /* free up fs locations */
4945 if (!nfslsp
->nl_numlocs
|| !nfslsp
->nl_locations
) {
4949 for (loc
= 0; loc
< nfslsp
->nl_numlocs
; loc
++) {
4950 fsl
= nfslsp
->nl_locations
[loc
];
4954 if ((fsl
->nl_servcount
> 0) && fsl
->nl_servers
) {
4955 for (serv
= 0; serv
< fsl
->nl_servcount
; serv
++) {
4956 fss
= fsl
->nl_servers
[serv
];
4960 if ((fss
->ns_addrcount
> 0) && fss
->ns_addresses
) {
4961 for (addr
= 0; addr
< fss
->ns_addrcount
; addr
++) {
4962 FREE(fss
->ns_addresses
[addr
], M_TEMP
);
4964 FREE(fss
->ns_addresses
, M_TEMP
);
4966 FREE(fss
->ns_name
, M_TEMP
);
4969 FREE(fsl
->nl_servers
, M_TEMP
);
4971 fsp
= &fsl
->nl_path
;
4972 if (fsp
->np_compcount
&& fsp
->np_components
) {
4973 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++) {
4974 if (fsp
->np_components
[comp
]) {
4975 FREE(fsp
->np_components
[comp
], M_TEMP
);
4978 FREE(fsp
->np_components
, M_TEMP
);
4982 FREE(nfslsp
->nl_locations
, M_TEMP
);
4983 nfslsp
->nl_numlocs
= 0;
4984 nfslsp
->nl_locations
= NULL
;
4988 nfs_mount_rele(struct nfsmount
*nmp
)
4992 lck_mtx_lock(&nmp
->nm_lock
);
4993 if (nmp
->nm_ref
< 1) {
4994 panic("nfs zombie mount underflow\n");
4997 if (nmp
->nm_ref
== 0) {
4998 wup
= nmp
->nm_state
& NFSSTA_MOUNT_DRAIN
;
5000 lck_mtx_unlock(&nmp
->nm_lock
);
5002 wakeup(&nmp
->nm_ref
);
5007 nfs_mount_drain_and_cleanup(struct nfsmount
*nmp
)
5009 lck_mtx_lock(&nmp
->nm_lock
);
5010 nmp
->nm_state
|= NFSSTA_MOUNT_DRAIN
;
5011 while (nmp
->nm_ref
> 0) {
5012 msleep(&nmp
->nm_ref
, &nmp
->nm_lock
, PZERO
- 1, "nfs_mount_drain", NULL
);
5014 assert(nmp
->nm_ref
== 0);
5015 lck_mtx_unlock(&nmp
->nm_lock
);
5016 nfs_mount_cleanup(nmp
);
5023 nfs_mount_zombie(struct nfsmount
*nmp
, int nm_state_flags
)
5025 struct nfsreq
*req
, *treq
;
5026 struct nfs_reqqhead iodq
, resendq
;
5027 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
5028 struct nfs_open_owner
*noop
, *nextnoop
;
5032 lck_mtx_lock(&nmp
->nm_lock
);
5033 nmp
->nm_state
|= nm_state_flags
;
5035 lck_mtx_unlock(&nmp
->nm_lock
);
5037 /* stop callbacks */
5038 if ((nmp
->nm_vers
>= NFS_VER4
) && !NMFLAG(nmp
, NOCALLBACK
) && nmp
->nm_cbid
) {
5039 nfs4_mount_callback_shutdown(nmp
);
5043 /* Destroy any RPCSEC_GSS contexts */
5044 nfs_gss_clnt_ctx_unmount(nmp
);
5047 /* mark the socket for termination */
5048 lck_mtx_lock(&nmp
->nm_lock
);
5049 nmp
->nm_sockflags
|= NMSOCK_UNMOUNT
;
5051 /* Have the socket thread send the unmount RPC, if requested/appropriate. */
5052 if ((nmp
->nm_vers
< NFS_VER4
) && (nmp
->nm_state
& NFSSTA_MOUNTED
) &&
5053 !(nmp
->nm_state
& (NFSSTA_FORCE
| NFSSTA_DEAD
)) && NMFLAG(nmp
, CALLUMNT
)) {
5054 nfs_mount_sock_thread_wake(nmp
);
5057 /* wait for the socket thread to terminate */
5058 while (nmp
->nm_sockthd
&& current_thread() != nmp
->nm_sockthd
) {
5059 wakeup(&nmp
->nm_sockthd
);
5060 msleep(&nmp
->nm_sockthd
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitsockthd", &ts
);
5062 lck_mtx_unlock(&nmp
->nm_lock
);
5064 /* tear down the socket */
5065 nfs_disconnect(nmp
);
5067 lck_mtx_lock(&nmp
->nm_lock
);
5070 if ((nmp
->nm_vers
>= NFS_VER4
) && !NMFLAG(nmp
, NOCALLBACK
) && nmp
->nm_cbid
) {
5071 /* clear out any pending delegation return requests */
5072 while ((np
= TAILQ_FIRST(&nmp
->nm_dreturnq
))) {
5073 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5074 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
5078 /* cancel any renew timer */
5079 if ((nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_renew_timer
) {
5080 lck_mtx_lock(&nmp
->nm_timer_lock
);
5081 thread_call_cancel(nmp
->nm_renew_timer
);
5082 thread_call_free(nmp
->nm_renew_timer
);
5083 nmp
->nm_renew_timer
= NULL
;
5084 lck_mtx_unlock(&nmp
->nm_timer_lock
);
5088 lck_mtx_unlock(&nmp
->nm_lock
);
5090 if (nmp
->nm_state
& NFSSTA_MOUNTED
) {
5091 switch (nmp
->nm_lockmode
) {
5092 case NFS_LOCK_MODE_DISABLED
:
5093 case NFS_LOCK_MODE_LOCAL
:
5095 case NFS_LOCK_MODE_ENABLED
:
5097 if (nmp
->nm_vers
<= NFS_VER3
) {
5098 nfs_lockd_mount_unregister(nmp
);
5099 nmp
->nm_lockmode
= NFS_LOCK_MODE_DISABLED
;
5106 if ((nmp
->nm_vers
>= NFS_VER4
) && nmp
->nm_longid
) {
5107 /* remove/deallocate the client ID data */
5108 lck_mtx_lock(&nfs_global_mutex
);
5109 TAILQ_REMOVE(&nfsclientids
, nmp
->nm_longid
, nci_link
);
5110 if (nmp
->nm_longid
->nci_id
) {
5111 FREE(nmp
->nm_longid
->nci_id
, M_TEMP
);
5113 FREE(nmp
->nm_longid
, M_TEMP
);
5114 nmp
->nm_longid
= NULL
;
5115 lck_mtx_unlock(&nfs_global_mutex
);
5119 * Be sure all requests for this mount are completed
5120 * and removed from the resend queue.
5122 TAILQ_INIT(&resendq
);
5123 lck_mtx_lock(&nfs_request_mutex
);
5124 TAILQ_FOREACH(req
, &nfs_reqq
, r_chain
) {
5125 if (req
->r_nmp
== nmp
) {
5126 lck_mtx_lock(&req
->r_mtx
);
5127 if (!req
->r_error
&& req
->r_nmrep
.nmc_mhead
== NULL
) {
5130 if (req
->r_flags
& R_RESENDQ
) {
5131 lck_mtx_lock(&nmp
->nm_lock
);
5132 if ((req
->r_flags
& R_RESENDQ
) && req
->r_rchain
.tqe_next
!= NFSREQNOLIST
) {
5133 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
5134 req
->r_flags
&= ~R_RESENDQ
;
5135 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
5137 * Queue up the request so that we can unreference them
5138 * with out holding nfs_request_mutex
5140 TAILQ_INSERT_TAIL(&resendq
, req
, r_rchain
);
5142 lck_mtx_unlock(&nmp
->nm_lock
);
5145 lck_mtx_unlock(&req
->r_mtx
);
5148 lck_mtx_unlock(&nfs_request_mutex
);
5150 /* Since we've drop the request mutex we can now safely unreference the request */
5151 TAILQ_FOREACH_SAFE(req
, &resendq
, r_rchain
, treq
) {
5152 TAILQ_REMOVE(&resendq
, req
, r_rchain
);
5153 /* Make sure we don't try and remove again in nfs_request_destroy */
5154 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
5155 nfs_request_rele(req
);
5159 * Now handle and outstanding async requests. We need to walk the
5160 * request queue again this time with the nfsiod_mutex held. No
5161 * other iods can grab our requests until we've put them on our own
5162 * local iod queue for processing.
5165 lck_mtx_lock(&nfs_request_mutex
);
5166 lck_mtx_lock(&nfsiod_mutex
);
5167 TAILQ_FOREACH(req
, &nfs_reqq
, r_chain
) {
5168 if (req
->r_nmp
== nmp
) {
5169 lck_mtx_lock(&req
->r_mtx
);
5170 if (req
->r_callback
.rcb_func
5171 && !(req
->r_flags
& R_WAITSENT
) && !(req
->r_flags
& R_IOD
)) {
5173 * Since R_IOD is not set then we need to handle it. If
5174 * we're not on a list add it to our iod queue. Otherwise
5175 * we must already be on nm_iodq which is added to our
5176 * local queue below.
5177 * %%% We should really keep a back pointer to our iod queue
5180 req
->r_flags
|= R_IOD
;
5181 if (req
->r_achain
.tqe_next
== NFSREQNOLIST
) {
5182 TAILQ_INSERT_TAIL(&iodq
, req
, r_achain
);
5185 lck_mtx_unlock(&req
->r_mtx
);
5189 /* finish any async I/O RPCs queued up */
5190 if (nmp
->nm_iodlink
.tqe_next
!= NFSNOLIST
) {
5191 TAILQ_REMOVE(&nfsiodmounts
, nmp
, nm_iodlink
);
5193 TAILQ_CONCAT(&iodq
, &nmp
->nm_iodq
, r_achain
);
5194 lck_mtx_unlock(&nfsiod_mutex
);
5195 lck_mtx_unlock(&nfs_request_mutex
);
5197 TAILQ_FOREACH_SAFE(req
, &iodq
, r_achain
, treq
) {
5198 TAILQ_REMOVE(&iodq
, req
, r_achain
);
5199 req
->r_achain
.tqe_next
= NFSREQNOLIST
;
5200 lck_mtx_lock(&req
->r_mtx
);
5201 docallback
= !(req
->r_flags
& R_WAITSENT
);
5202 lck_mtx_unlock(&req
->r_mtx
);
5204 req
->r_callback
.rcb_func(req
);
5208 /* clean up common state */
5209 lck_mtx_lock(&nmp
->nm_lock
);
5210 while ((np
= LIST_FIRST(&nmp
->nm_monlist
))) {
5211 LIST_REMOVE(np
, n_monlink
);
5212 np
->n_monlink
.le_next
= NFSNOLIST
;
5214 TAILQ_FOREACH_SAFE(noop
, &nmp
->nm_open_owners
, noo_link
, nextnoop
) {
5215 os_ref_count_t newcount
;
5217 TAILQ_REMOVE(&nmp
->nm_open_owners
, noop
, noo_link
);
5218 noop
->noo_flags
&= ~NFS_OPEN_OWNER_LINK
;
5219 newcount
= os_ref_release_locked(&noop
->noo_refcnt
);
5224 nfs_open_owner_destroy(noop
);
5226 lck_mtx_unlock(&nmp
->nm_lock
);
5229 /* clean up NFSv4 state */
5230 if (nmp
->nm_vers
>= NFS_VER4
) {
5231 lck_mtx_lock(&nmp
->nm_lock
);
5232 while ((np
= TAILQ_FIRST(&nmp
->nm_delegations
))) {
5233 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
5234 np
->n_dlink
.tqe_next
= NFSNOLIST
;
5236 lck_mtx_unlock(&nmp
->nm_lock
);
5239 nfs_mount_rele(nmp
);
5243 * cleanup/destroy an nfsmount
5246 nfs_mount_cleanup(struct nfsmount
*nmp
)
5252 nfs_mount_zombie(nmp
, 0);
5254 NFS_VFS_DBG("Unmounting %s from %s\n",
5255 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
5256 vfs_statfs(nmp
->nm_mountp
)->f_mntonname
);
5257 NFS_VFS_DBG("nfs state = 0x%8.8x\n", nmp
->nm_state
);
5258 NFS_VFS_DBG("nfs socket flags = 0x%8.8x\n", nmp
->nm_sockflags
);
5259 NFS_VFS_DBG("nfs mount ref count is %d\n", nmp
->nm_ref
);
5260 NFS_VFS_DBG("mount ref count is %d\n", nmp
->nm_mountp
->mnt_count
);
5262 if (nmp
->nm_mountp
) {
5263 vfs_setfsprivate(nmp
->nm_mountp
, NULL
);
5266 lck_mtx_lock(&nmp
->nm_lock
);
5268 panic("Some one has grabbed a ref %d state flags = 0x%8.8x\n", nmp
->nm_ref
, nmp
->nm_state
);
5271 if (nmp
->nm_saddr
) {
5272 FREE(nmp
->nm_saddr
, M_SONAME
);
5274 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_rqsaddr
) {
5275 FREE(nmp
->nm_rqsaddr
, M_SONAME
);
5278 if (IS_VALID_CRED(nmp
->nm_mcred
)) {
5279 kauth_cred_unref(&nmp
->nm_mcred
);
5282 nfs_fs_locations_cleanup(&nmp
->nm_locations
);
5284 if (nmp
->nm_realm
) {
5285 FREE(nmp
->nm_realm
, M_TEMP
);
5287 if (nmp
->nm_principal
) {
5288 FREE(nmp
->nm_principal
, M_TEMP
);
5290 if (nmp
->nm_sprinc
) {
5291 FREE(nmp
->nm_sprinc
, M_TEMP
);
5295 xb_free(nmp
->nm_args
);
5298 lck_mtx_unlock(&nmp
->nm_lock
);
5300 lck_mtx_destroy(&nmp
->nm_lock
, &nfs_mount_grp
);
5302 NFS_ZFREE(nfs_fhandle_zone
, nmp
->nm_fh
);
5306 if (nmp
->nm_vers
>= NFS_VER4
) {
5307 lck_mtx_destroy(&nmp
->nm_timer_lock
, &nfs_mount_grp
);
5312 NFS_ZFREE(nfsmnt_zone
, nmp
);
5316 * Return root of a filesystem
5319 nfs_vfs_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t ctx
)
5322 struct nfsmount
*nmp
;
5327 if (!nmp
|| !nmp
->nm_dnp
) {
5330 vp
= NFSTOV(nmp
->nm_dnp
);
5331 vpid
= vnode_vid(vp
);
5332 while ((error
= vnode_getwithvid(vp
, vpid
))) {
5333 /* vnode_get() may return ENOENT if the dir changes. */
5334 /* If that happens, just try it again, else return the error. */
5335 if ((error
!= ENOENT
) || (vnode_vid(vp
) == vpid
)) {
5338 vpid
= vnode_vid(vp
);
5345 * Do operations associated with quotas
5350 __unused mount_t mp
,
5353 __unused caddr_t datap
,
5354 __unused vfs_context_t context
)
5361 nfs_sa_getport(struct sockaddr
*sa
, int *error
)
5365 if (sa
->sa_family
== AF_INET6
) {
5366 port
= ntohs(((struct sockaddr_in6
*)sa
)->sin6_port
);
5367 } else if (sa
->sa_family
== AF_INET
) {
5368 port
= ntohs(((struct sockaddr_in
*)sa
)->sin_port
);
5377 nfs_sa_setport(struct sockaddr
*sa
, in_port_t port
)
5379 if (sa
->sa_family
== AF_INET6
) {
5380 ((struct sockaddr_in6
*)sa
)->sin6_port
= htons(port
);
5381 } else if (sa
->sa_family
== AF_INET
) {
5382 ((struct sockaddr_in
*)sa
)->sin_port
= htons(port
);
5387 nfs3_getquota(struct nfsmount
*nmp
, vfs_context_t ctx
, uid_t id
, int type
, struct dqblk
*dqb
)
5389 int error
= 0, timeo
;
5390 int rqproto
, rqvers
= (type
== GRPQUOTA
) ? RPCRQUOTA_EXT_VER
: RPCRQUOTA_VER
;
5391 in_port_t rqport
= 0;
5392 thread_t thd
= vfs_context_thread(ctx
);
5393 kauth_cred_t cred
= vfs_context_ucred(ctx
);
5395 uint64_t slen
, xid
= 0;
5396 struct nfsm_chain nmreq
, nmrep
;
5398 uint32_t val
= 0, bsize
= 0;
5399 struct sockaddr
*rqsaddr
;
5401 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
5403 if (!nmp
->nm_saddr
) {
5407 if (NMFLAG(nmp
, NOQUOTA
) || nmp
->nm_saddr
->sa_family
== AF_LOCAL
/* XXX for now */) {
5412 * Allocate an address for rquotad if needed
5414 if (!nmp
->nm_rqsaddr
) {
5417 MALLOC(rqsaddr
, struct sockaddr
*, sizeof(struct sockaddr_storage
), M_SONAME
, M_WAITOK
| M_ZERO
);
5418 bcopy(nmp
->nm_saddr
, rqsaddr
, min(sizeof(struct sockaddr_storage
), nmp
->nm_saddr
->sa_len
));
5419 /* Set the port to zero, will call rpcbind to get the port below */
5420 nfs_sa_setport(rqsaddr
, 0);
5423 lck_mtx_lock(&nmp
->nm_lock
);
5424 if (!nmp
->nm_rqsaddr
) {
5425 nmp
->nm_rqsaddr
= rqsaddr
;
5426 nmp
->nm_rqsaddrstamp
= now
.tv_sec
;
5430 lck_mtx_unlock(&nmp
->nm_lock
);
5432 FREE(rqsaddr
, M_SONAME
);
5436 timeo
= NMFLAG(nmp
, SOFT
) ? 10 : 60;
5437 rqproto
= IPPROTO_UDP
; /* XXX should prefer TCP if mount is TCP */
5439 /* check if we have a recently cached rquota port */
5441 lck_mtx_lock(&nmp
->nm_lock
);
5442 rqsaddr
= nmp
->nm_rqsaddr
;
5443 rqport
= nfs_sa_getport(rqsaddr
, &error
);
5444 while (!error
&& (!rqport
|| ((nmp
->nm_rqsaddrstamp
+ 60) <= (uint32_t)now
.tv_sec
))) {
5445 error
= nfs_sigintr(nmp
, NULL
, thd
, 1);
5447 lck_mtx_unlock(&nmp
->nm_lock
);
5450 if (nmp
->nm_state
& NFSSTA_RQUOTAINPROG
) {
5451 nmp
->nm_state
|= NFSSTA_WANTRQUOTA
;
5452 msleep(&nmp
->nm_rqsaddr
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitrquotaaddr", &ts
);
5453 rqport
= nfs_sa_getport(rqsaddr
, &error
);
5456 nmp
->nm_state
|= NFSSTA_RQUOTAINPROG
;
5457 lck_mtx_unlock(&nmp
->nm_lock
);
5459 /* send portmap request to get rquota port */
5460 error
= nfs_portmap_lookup(nmp
, ctx
, rqsaddr
, NULL
, RPCPROG_RQUOTA
, rqvers
, rqproto
, timeo
);
5464 rqport
= nfs_sa_getport(rqsaddr
, &error
);
5471 * We overload PMAPPORT for the port if rquotad is not
5472 * currently registered or up at the server. In the
5473 * while loop above, port will be set and we will defer
5474 * for a bit. Perhaps the service isn't online yet.
5476 * Note that precludes using indirect, but we're not doing
5480 nfs_sa_setport(rqsaddr
, rqport
);
5483 nmp
->nm_rqsaddrstamp
= now
.tv_sec
;
5485 lck_mtx_lock(&nmp
->nm_lock
);
5486 nmp
->nm_state
&= ~NFSSTA_RQUOTAINPROG
;
5487 if (nmp
->nm_state
& NFSSTA_WANTRQUOTA
) {
5488 nmp
->nm_state
&= ~NFSSTA_WANTRQUOTA
;
5489 wakeup(&nmp
->nm_rqsaddr
);
5492 lck_mtx_unlock(&nmp
->nm_lock
);
5497 /* Using PMAPPORT for unavailabe rquota service */
5498 if (rqport
== PMAPPORT
) {
5502 /* rquota request */
5503 nfsm_chain_null(&nmreq
);
5504 nfsm_chain_null(&nmrep
);
5505 path
= &vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
[0];
5506 while (*path
&& (*path
!= '/')) {
5509 slen
= strlen(path
);
5510 nfsm_chain_build_alloc_init(error
, &nmreq
, 3 * NFSX_UNSIGNED
+ nfsm_rndup(slen
));
5511 nfsm_chain_add_name(error
, &nmreq
, path
, slen
, nmp
);
5512 if (type
== GRPQUOTA
) {
5513 nfsm_chain_add_32(error
, &nmreq
, type
);
5515 nfsm_chain_add_32(error
, &nmreq
, id
);
5516 nfsm_chain_build_done(error
, &nmreq
);
5518 error
= nfsm_rpchead2(nmp
, (rqproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
5519 RPCPROG_RQUOTA
, rqvers
, RPCRQUOTA_GET
,
5520 RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
, &xid
, &mreq
);
5522 nmreq
.nmc_mhead
= NULL
;
5523 error
= nfs_aux_request(nmp
, thd
, rqsaddr
, NULL
,
5524 (rqproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
5525 mreq
, R_XID32(xid
), 0, timeo
, &nmrep
);
5528 /* parse rquota response */
5529 nfsm_chain_get_32(error
, &nmrep
, val
);
5530 if (!error
&& (val
!= RQUOTA_STAT_OK
)) {
5531 if (val
== RQUOTA_STAT_NOQUOTA
) {
5533 } else if (val
== RQUOTA_STAT_EPERM
) {
5539 nfsm_chain_get_32(error
, &nmrep
, bsize
);
5540 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
5541 nfsm_chain_get_32(error
, &nmrep
, val
);
5543 dqb
->dqb_bhardlimit
= (uint64_t)val
* bsize
;
5544 nfsm_chain_get_32(error
, &nmrep
, val
);
5546 dqb
->dqb_bsoftlimit
= (uint64_t)val
* bsize
;
5547 nfsm_chain_get_32(error
, &nmrep
, val
);
5549 dqb
->dqb_curbytes
= (uint64_t)val
* bsize
;
5550 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_ihardlimit
);
5551 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_isoftlimit
);
5552 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_curinodes
);
5553 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_btime
);
5554 nfsm_chain_get_32(error
, &nmrep
, dqb
->dqb_itime
);
5558 nfsm_chain_cleanup(&nmreq
);
5559 nfsm_chain_cleanup(&nmrep
);
5564 nfs4_getquota(struct nfsmount
*nmp
, vfs_context_t ctx
, uid_t id
, int type
, struct dqblk
*dqb
)
5567 int error
= 0, status
, nfsvers
, numops
;
5569 struct nfsm_chain nmreq
, nmrep
;
5570 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
5571 thread_t thd
= vfs_context_thread(ctx
);
5572 kauth_cred_t cred
= vfs_context_ucred(ctx
);
5573 struct nfsreq_secinfo_args si
;
5575 if (type
!= USRQUOTA
) { /* NFSv4 only supports user quotas */
5579 /* first check that the server supports any of the quota attributes */
5580 if (!NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_HARD
) &&
5581 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_SOFT
) &&
5582 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_USED
)) {
5587 * The credential passed to the server needs to have
5588 * an effective uid that matches the given uid.
5590 if (id
!= kauth_cred_getuid(cred
)) {
5591 struct posix_cred temp_pcred
;
5592 posix_cred_t pcred
= posix_cred_get(cred
);
5593 bzero(&temp_pcred
, sizeof(temp_pcred
));
5594 temp_pcred
.cr_uid
= id
;
5595 temp_pcred
.cr_ngroups
= pcred
->cr_ngroups
;
5596 bcopy(pcred
->cr_groups
, temp_pcred
.cr_groups
, sizeof(temp_pcred
.cr_groups
));
5597 cred
= posix_cred_create(&temp_pcred
);
5598 if (!IS_VALID_CRED(cred
)) {
5602 kauth_cred_ref(cred
);
5605 nfsvers
= nmp
->nm_vers
;
5610 if (error
|| ((error
= vnode_get(NFSTOV(np
))))) {
5611 kauth_cred_unref(&cred
);
5615 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5616 nfsm_chain_null(&nmreq
);
5617 nfsm_chain_null(&nmrep
);
5621 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
5622 nfsm_chain_add_compound_header(error
, &nmreq
, "quota", nmp
->nm_minor_vers
, numops
);
5624 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5625 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5627 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5628 NFS_CLEAR_ATTRIBUTES(bitmap
);
5629 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_AVAIL_HARD
);
5630 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_AVAIL_SOFT
);
5631 NFS_BITMAP_SET(bitmap
, NFS_FATTR_QUOTA_USED
);
5632 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
5633 nfsm_chain_build_done(error
, &nmreq
);
5634 nfsm_assert(error
, (numops
== 0), EPROTO
);
5636 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, &nmrep
, &xid
, &status
);
5637 nfsm_chain_skip_tag(error
, &nmrep
);
5638 nfsm_chain_get_32(error
, &nmrep
, numops
);
5639 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5640 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5641 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
5643 error
= nfs4_parsefattr(&nmrep
, NULL
, NULL
, NULL
, dqb
, NULL
);
5645 nfsm_assert(error
, NFSTONMP(np
), ENXIO
);
5647 nfsm_chain_cleanup(&nmreq
);
5648 nfsm_chain_cleanup(&nmrep
);
5649 vnode_put(NFSTOV(np
));
5650 kauth_cred_unref(&cred
);
5653 #endif /* CONFIG_NFS4 */
5655 nfs_vfs_quotactl(mount_t mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t ctx
)
5657 struct nfsmount
*nmp
;
5658 int cmd
, type
, error
, nfsvers
;
5659 uid_t euid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
5660 struct dqblk
*dqb
= (struct dqblk
*)datap
;
5663 if (nfs_mount_gone(nmp
)) {
5666 nfsvers
= nmp
->nm_vers
;
5672 /* we can only support Q_GETQUOTA */
5673 cmd
= cmds
>> SUBCMDSHIFT
;
5688 type
= cmds
& SUBCMDMASK
;
5689 if ((u_int
)type
>= MAXQUOTAS
) {
5692 if ((uid
!= euid
) && ((error
= vfs_context_suser(ctx
)))) {
5696 if (vfs_busy(mp
, LK_NOWAIT
)) {
5699 bzero(dqb
, sizeof(*dqb
));
5700 error
= nmp
->nm_funcs
->nf_getquota(nmp
, ctx
, uid
, type
, dqb
);
5707 * Flush out the buffer cache
5709 int nfs_sync_callout(vnode_t
, void *);
5711 struct nfs_sync_cargs
{
5718 nfs_sync_callout(vnode_t vp
, void *arg
)
5720 struct nfs_sync_cargs
*cargs
= (struct nfs_sync_cargs
*)arg
;
5721 nfsnode_t np
= VTONFS(vp
);
5724 if (np
->n_flag
& NREVOKE
) {
5725 vn_revoke(vp
, REVOKEALL
, cargs
->ctx
);
5726 return VNODE_RETURNED
;
5729 if (LIST_EMPTY(&np
->n_dirtyblkhd
)) {
5730 return VNODE_RETURNED
;
5732 if (np
->n_wrbusy
> 0) {
5733 return VNODE_RETURNED
;
5735 if (np
->n_bflag
& (NBFLUSHINPROG
| NBINVALINPROG
)) {
5736 return VNODE_RETURNED
;
5739 error
= nfs_flush(np
, cargs
->waitfor
, vfs_context_thread(cargs
->ctx
), 0);
5741 cargs
->error
= error
;
5744 return VNODE_RETURNED
;
5748 nfs_vfs_sync(mount_t mp
, int waitfor
, vfs_context_t ctx
)
5750 struct nfs_sync_cargs cargs
;
5752 cargs
.waitfor
= waitfor
;
5756 vnode_iterate(mp
, 0, nfs_sync_callout
, &cargs
);
5762 * NFS flat namespace lookup.
5763 * Currently unsupported.
5768 __unused mount_t mp
,
5769 __unused ino64_t ino
,
5770 __unused vnode_t
*vpp
,
5771 __unused vfs_context_t ctx
)
5777 * At this point, this should never happen
5782 __unused mount_t mp
,
5784 __unused
unsigned char *fhp
,
5785 __unused vnode_t
*vpp
,
5786 __unused vfs_context_t ctx
)
5792 * Vnode pointer to File handle, should never happen either
5797 __unused vnode_t vp
,
5798 __unused
int *fhlenp
,
5799 __unused
unsigned char *fhp
,
5800 __unused vfs_context_t ctx
)
5806 * Vfs start routine, a no-op.
5811 __unused mount_t mp
,
5813 __unused vfs_context_t ctx
)
5819 * Build the mount info buffer for NFS_MOUNTINFO.
5822 nfs_mountinfo_assemble(struct nfsmount
*nmp
, struct xdrbuf
*xb
)
5824 struct xdrbuf xbinfo
, xborig
;
5826 uint32_t origargsvers
, origargslength
;
5827 size_t infolength_offset
, curargsopaquelength_offset
, curargslength_offset
, attrslength_offset
, curargs_end_offset
, end_offset
;
5828 uint32_t miattrs
[NFS_MIATTR_BITMAP_LEN
];
5829 uint32_t miflags_mask
[NFS_MIFLAG_BITMAP_LEN
];
5830 uint32_t miflags
[NFS_MIFLAG_BITMAP_LEN
];
5831 uint32_t mattrs
[NFS_MATTR_BITMAP_LEN
];
5832 uint32_t mflags_mask
[NFS_MFLAG_BITMAP_LEN
];
5833 uint32_t mflags
[NFS_MFLAG_BITMAP_LEN
];
5834 uint32_t loc
, serv
, addr
, comp
;
5835 int i
, timeo
, error
= 0;
5837 /* set up mount info attr and flag bitmaps */
5838 NFS_BITMAP_ZERO(miattrs
, NFS_MIATTR_BITMAP_LEN
);
5839 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_FLAGS
);
5840 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_ORIG_ARGS
);
5841 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_CUR_ARGS
);
5842 NFS_BITMAP_SET(miattrs
, NFS_MIATTR_CUR_LOC_INDEX
);
5843 NFS_BITMAP_ZERO(miflags_mask
, NFS_MIFLAG_BITMAP_LEN
);
5844 NFS_BITMAP_ZERO(miflags
, NFS_MIFLAG_BITMAP_LEN
);
5845 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_DEAD
);
5846 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_NOTRESP
);
5847 NFS_BITMAP_SET(miflags_mask
, NFS_MIFLAG_RECOVERY
);
5848 if (nmp
->nm_state
& NFSSTA_DEAD
) {
5849 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_DEAD
);
5851 if ((nmp
->nm_state
& (NFSSTA_TIMEO
| NFSSTA_JUKEBOXTIMEO
)) ||
5852 ((nmp
->nm_state
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
))) {
5853 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_NOTRESP
);
5855 if (nmp
->nm_state
& NFSSTA_RECOVER
) {
5856 NFS_BITMAP_SET(miflags
, NFS_MIFLAG_RECOVERY
);
5859 /* get original mount args length */
5860 xb_init_buffer(&xborig
, nmp
->nm_args
, 2 * XDRWORD
);
5861 xb_get_32(error
, &xborig
, origargsvers
); /* version */
5862 xb_get_32(error
, &xborig
, origargslength
); /* args length */
5865 /* set up current mount attributes bitmap */
5866 NFS_BITMAP_ZERO(mattrs
, NFS_MATTR_BITMAP_LEN
);
5867 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FLAGS
);
5868 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_VERSION
);
5870 if (nmp
->nm_vers
>= NFS_VER4
) {
5871 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_MINOR_VERSION
);
5874 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READ_SIZE
);
5875 NFS_BITMAP_SET(mattrs
, NFS_MATTR_WRITE_SIZE
);
5876 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READDIR_SIZE
);
5877 NFS_BITMAP_SET(mattrs
, NFS_MATTR_READAHEAD
);
5878 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MIN
);
5879 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_REG_MAX
);
5880 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MIN
);
5881 NFS_BITMAP_SET(mattrs
, NFS_MATTR_ATTRCACHE_DIR_MAX
);
5882 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCK_MODE
);
5883 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SECURITY
);
5884 if (nmp
->nm_etype
.selected
< nmp
->nm_etype
.count
) {
5885 NFS_BITMAP_SET(mattrs
, NFS_MATTR_KERB_ETYPE
);
5887 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MAX_GROUP_LIST
);
5888 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOCKET_TYPE
);
5889 if (nmp
->nm_saddr
->sa_family
!= AF_LOCAL
) {
5890 NFS_BITMAP_SET(mattrs
, NFS_MATTR_NFS_PORT
);
5892 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_mountport
&& !nmp
->nm_mount_localport
) {
5893 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MOUNT_PORT
);
5895 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REQUEST_TIMEOUT
);
5896 if (NMFLAG(nmp
, SOFT
)) {
5897 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SOFT_RETRY_COUNT
);
5899 if (nmp
->nm_deadtimeout
) {
5900 NFS_BITMAP_SET(mattrs
, NFS_MATTR_DEAD_TIMEOUT
);
5903 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FH
);
5905 NFS_BITMAP_SET(mattrs
, NFS_MATTR_FS_LOCATIONS
);
5906 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFLAGS
);
5907 if (origargsvers
< NFS_ARGSVERSION_XDR
) {
5908 NFS_BITMAP_SET(mattrs
, NFS_MATTR_MNTFROM
);
5910 if (nmp
->nm_realm
) {
5911 NFS_BITMAP_SET(mattrs
, NFS_MATTR_REALM
);
5913 if (nmp
->nm_principal
) {
5914 NFS_BITMAP_SET(mattrs
, NFS_MATTR_PRINCIPAL
);
5916 if (nmp
->nm_sprinc
) {
5917 NFS_BITMAP_SET(mattrs
, NFS_MATTR_SVCPRINCIPAL
);
5919 if (nmp
->nm_nfs_localport
) {
5920 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCAL_NFS_PORT
);
5922 if ((nmp
->nm_vers
< NFS_VER4
) && nmp
->nm_mount_localport
) {
5923 NFS_BITMAP_SET(mattrs
, NFS_MATTR_LOCAL_MOUNT_PORT
);
5926 /* set up current mount flags bitmap */
5927 /* first set the flags that we will be setting - either on OR off */
5928 NFS_BITMAP_ZERO(mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
5929 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_SOFT
);
5930 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_INTR
);
5931 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RESVPORT
);
5932 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
5933 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCONNECT
);
5935 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_DUMBTIMER
);
5936 if (nmp
->nm_vers
< NFS_VER4
) {
5937 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_CALLUMNT
);
5939 if (nmp
->nm_vers
>= NFS_VER3
) {
5940 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_RDIRPLUS
);
5942 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NONEGNAMECACHE
);
5943 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MUTEJUKEBOX
);
5945 if (nmp
->nm_vers
>= NFS_VER4
) {
5946 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_EPHEMERAL
);
5947 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOCALLBACK
);
5948 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NAMEDATTR
);
5949 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOACL
);
5950 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_ACLONLY
);
5953 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NFC
);
5954 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_NOQUOTA
);
5955 if (nmp
->nm_vers
< NFS_VER4
) {
5956 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MNTUDP
);
5958 NFS_BITMAP_SET(mflags_mask
, NFS_MFLAG_MNTQUICK
);
5959 /* now set the flags that should be set */
5960 NFS_BITMAP_ZERO(mflags
, NFS_MFLAG_BITMAP_LEN
);
5961 if (NMFLAG(nmp
, SOFT
)) {
5962 NFS_BITMAP_SET(mflags
, NFS_MFLAG_SOFT
);
5964 if (NMFLAG(nmp
, INTR
)) {
5965 NFS_BITMAP_SET(mflags
, NFS_MFLAG_INTR
);
5967 if (NMFLAG(nmp
, RESVPORT
)) {
5968 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RESVPORT
);
5970 if ((nmp
->nm_sotype
== SOCK_DGRAM
) && NMFLAG(nmp
, NOCONNECT
)) {
5971 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCONNECT
);
5973 if (NMFLAG(nmp
, DUMBTIMER
)) {
5974 NFS_BITMAP_SET(mflags
, NFS_MFLAG_DUMBTIMER
);
5976 if ((nmp
->nm_vers
< NFS_VER4
) && NMFLAG(nmp
, CALLUMNT
)) {
5977 NFS_BITMAP_SET(mflags
, NFS_MFLAG_CALLUMNT
);
5979 if ((nmp
->nm_vers
>= NFS_VER3
) && NMFLAG(nmp
, RDIRPLUS
)) {
5980 NFS_BITMAP_SET(mflags
, NFS_MFLAG_RDIRPLUS
);
5982 if (NMFLAG(nmp
, NONEGNAMECACHE
)) {
5983 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NONEGNAMECACHE
);
5985 if (NMFLAG(nmp
, MUTEJUKEBOX
)) {
5986 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MUTEJUKEBOX
);
5989 if (nmp
->nm_vers
>= NFS_VER4
) {
5990 if (NMFLAG(nmp
, EPHEMERAL
)) {
5991 NFS_BITMAP_SET(mflags
, NFS_MFLAG_EPHEMERAL
);
5993 if (NMFLAG(nmp
, NOCALLBACK
)) {
5994 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOCALLBACK
);
5996 if (NMFLAG(nmp
, NAMEDATTR
)) {
5997 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NAMEDATTR
);
5999 if (NMFLAG(nmp
, NOACL
)) {
6000 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOACL
);
6002 if (NMFLAG(nmp
, ACLONLY
)) {
6003 NFS_BITMAP_SET(mflags
, NFS_MFLAG_ACLONLY
);
6007 if (NMFLAG(nmp
, NFC
)) {
6008 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NFC
);
6010 if (NMFLAG(nmp
, NOQUOTA
) || ((nmp
->nm_vers
>= NFS_VER4
) &&
6011 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_HARD
) &&
6012 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_AVAIL_SOFT
) &&
6013 !NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_supp_attr
, NFS_FATTR_QUOTA_USED
))) {
6014 NFS_BITMAP_SET(mflags
, NFS_MFLAG_NOQUOTA
);
6016 if ((nmp
->nm_vers
< NFS_VER4
) && NMFLAG(nmp
, MNTUDP
)) {
6017 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MNTUDP
);
6019 if (NMFLAG(nmp
, MNTQUICK
)) {
6020 NFS_BITMAP_SET(mflags
, NFS_MFLAG_MNTQUICK
);
6023 /* assemble info buffer: */
6024 xb_init_buffer(&xbinfo
, NULL
, 0);
6025 xb_add_32(error
, &xbinfo
, NFS_MOUNT_INFO_VERSION
);
6026 infolength_offset
= xb_offset(&xbinfo
);
6027 xb_add_32(error
, &xbinfo
, 0);
6028 xb_add_bitmap(error
, &xbinfo
, miattrs
, NFS_MIATTR_BITMAP_LEN
);
6029 xb_add_bitmap(error
, &xbinfo
, miflags
, NFS_MIFLAG_BITMAP_LEN
);
6030 xb_add_32(error
, &xbinfo
, origargslength
);
6032 error
= xb_add_bytes(&xbinfo
, nmp
->nm_args
, origargslength
, 0);
6035 /* the opaque byte count for the current mount args values: */
6036 curargsopaquelength_offset
= xb_offset(&xbinfo
);
6037 xb_add_32(error
, &xbinfo
, 0);
6039 /* Encode current mount args values */
6040 xb_add_32(error
, &xbinfo
, NFS_ARGSVERSION_XDR
);
6041 curargslength_offset
= xb_offset(&xbinfo
);
6042 xb_add_32(error
, &xbinfo
, 0);
6043 xb_add_32(error
, &xbinfo
, NFS_XDRARGS_VERSION_0
);
6044 xb_add_bitmap(error
, &xbinfo
, mattrs
, NFS_MATTR_BITMAP_LEN
);
6045 attrslength_offset
= xb_offset(&xbinfo
);
6046 xb_add_32(error
, &xbinfo
, 0);
6047 xb_add_bitmap(error
, &xbinfo
, mflags_mask
, NFS_MFLAG_BITMAP_LEN
);
6048 xb_add_bitmap(error
, &xbinfo
, mflags
, NFS_MFLAG_BITMAP_LEN
);
6049 xb_add_32(error
, &xbinfo
, nmp
->nm_vers
); /* NFS_VERSION */
6051 if (nmp
->nm_vers
>= NFS_VER4
) {
6052 xb_add_32(error
, &xbinfo
, nmp
->nm_minor_vers
); /* NFS_MINOR_VERSION */
6055 xb_add_32(error
, &xbinfo
, nmp
->nm_rsize
); /* READ_SIZE */
6056 xb_add_32(error
, &xbinfo
, nmp
->nm_wsize
); /* WRITE_SIZE */
6057 xb_add_32(error
, &xbinfo
, nmp
->nm_readdirsize
); /* READDIR_SIZE */
6058 xb_add_32(error
, &xbinfo
, nmp
->nm_readahead
); /* READAHEAD */
6059 xb_add_32(error
, &xbinfo
, nmp
->nm_acregmin
); /* ATTRCACHE_REG_MIN */
6060 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_REG_MIN */
6061 xb_add_32(error
, &xbinfo
, nmp
->nm_acregmax
); /* ATTRCACHE_REG_MAX */
6062 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_REG_MAX */
6063 xb_add_32(error
, &xbinfo
, nmp
->nm_acdirmin
); /* ATTRCACHE_DIR_MIN */
6064 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_DIR_MIN */
6065 xb_add_32(error
, &xbinfo
, nmp
->nm_acdirmax
); /* ATTRCACHE_DIR_MAX */
6066 xb_add_32(error
, &xbinfo
, 0); /* ATTRCACHE_DIR_MAX */
6067 xb_add_32(error
, &xbinfo
, nmp
->nm_lockmode
); /* LOCK_MODE */
6068 if (nmp
->nm_sec
.count
) {
6069 xb_add_32(error
, &xbinfo
, nmp
->nm_sec
.count
); /* SECURITY */
6071 for (i
= 0; i
< nmp
->nm_sec
.count
; i
++) {
6072 xb_add_32(error
, &xbinfo
, nmp
->nm_sec
.flavors
[i
]);
6074 } else if (nmp
->nm_servsec
.count
) {
6075 xb_add_32(error
, &xbinfo
, nmp
->nm_servsec
.count
); /* SECURITY */
6077 for (i
= 0; i
< nmp
->nm_servsec
.count
; i
++) {
6078 xb_add_32(error
, &xbinfo
, nmp
->nm_servsec
.flavors
[i
]);
6081 xb_add_32(error
, &xbinfo
, 1); /* SECURITY */
6082 xb_add_32(error
, &xbinfo
, nmp
->nm_auth
);
6084 if (nmp
->nm_etype
.selected
< nmp
->nm_etype
.count
) {
6085 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.count
);
6086 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.selected
);
6087 for (uint32_t j
= 0; j
< nmp
->nm_etype
.count
; j
++) {
6088 xb_add_32(error
, &xbinfo
, nmp
->nm_etype
.etypes
[j
]);
6092 xb_add_32(error
, &xbinfo
, nmp
->nm_numgrps
); /* MAX_GROUP_LIST */
6095 switch (nmp
->nm_saddr
->sa_family
) {
6098 snprintf(sotype
, sizeof(sotype
), "%s%s", (nmp
->nm_sotype
== SOCK_DGRAM
) ? "udp" : "tcp",
6099 nmp
->nm_sofamily
? (nmp
->nm_sofamily
== AF_INET
) ? "4" : "6" : "");
6100 xb_add_string(error
, &xbinfo
, sotype
, strlen(sotype
)); /* SOCKET_TYPE */
6101 xb_add_32(error
, &xbinfo
, ntohs(((struct sockaddr_in
*)nmp
->nm_saddr
)->sin_port
)); /* NFS_PORT */
6102 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_MOUNT_PORT
)) {
6103 xb_add_32(error
, &xbinfo
, nmp
->nm_mountport
); /* MOUNT_PORT */
6107 strlcpy(sotype
, (nmp
->nm_sotype
== SOCK_DGRAM
) ? "ticlts" : "ticotsord", sizeof(sotype
));
6108 xb_add_string(error
, &xbinfo
, sotype
, strlen(sotype
));
6111 NFS_VFS_DBG("Unsupported address family %d\n", nmp
->nm_saddr
->sa_family
);
6112 printf("Unsupported address family %d\n", nmp
->nm_saddr
->sa_family
);
6117 timeo
= (nmp
->nm_timeo
* 10) / NFS_HZ
;
6118 xb_add_32(error
, &xbinfo
, timeo
/ 10); /* REQUEST_TIMEOUT */
6119 xb_add_32(error
, &xbinfo
, (timeo
% 10) * 100000000); /* REQUEST_TIMEOUT */
6120 if (NMFLAG(nmp
, SOFT
)) {
6121 xb_add_32(error
, &xbinfo
, nmp
->nm_retry
); /* SOFT_RETRY_COUNT */
6123 if (nmp
->nm_deadtimeout
) {
6124 xb_add_32(error
, &xbinfo
, nmp
->nm_deadtimeout
); /* DEAD_TIMEOUT */
6125 xb_add_32(error
, &xbinfo
, 0); /* DEAD_TIMEOUT */
6128 xb_add_fh(error
, &xbinfo
, &nmp
->nm_fh
->fh_data
[0], nmp
->nm_fh
->fh_len
); /* FH */
6130 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_numlocs
); /* FS_LOCATIONS */
6131 for (loc
= 0; !error
&& (loc
< nmp
->nm_locations
.nl_numlocs
); loc
++) {
6132 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servcount
);
6133 for (serv
= 0; !error
&& (serv
< nmp
->nm_locations
.nl_locations
[loc
]->nl_servcount
); serv
++) {
6134 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
,
6135 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
));
6136 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
);
6137 for (addr
= 0; !error
&& (addr
< nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
); addr
++) {
6138 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
],
6139 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addresses
[addr
]));
6141 xb_add_32(error
, &xbinfo
, 0); /* empty server info */
6143 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_compcount
);
6144 for (comp
= 0; !error
&& (comp
< nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_compcount
); comp
++) {
6145 xb_add_string(error
, &xbinfo
, nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_components
[comp
],
6146 strlen(nmp
->nm_locations
.nl_locations
[loc
]->nl_path
.np_components
[comp
]));
6148 xb_add_32(error
, &xbinfo
, 0); /* empty fs location info */
6150 xb_add_32(error
, &xbinfo
, vfs_flags(nmp
->nm_mountp
)); /* MNTFLAGS */
6151 if (origargsvers
< NFS_ARGSVERSION_XDR
) {
6152 xb_add_string(error
, &xbinfo
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
6153 strlen(vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
)); /* MNTFROM */
6155 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_REALM
)) {
6156 xb_add_string(error
, &xbinfo
, nmp
->nm_realm
, strlen(nmp
->nm_realm
));
6158 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_PRINCIPAL
)) {
6159 xb_add_string(error
, &xbinfo
, nmp
->nm_principal
, strlen(nmp
->nm_principal
));
6161 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_SVCPRINCIPAL
)) {
6162 xb_add_string(error
, &xbinfo
, nmp
->nm_sprinc
, strlen(nmp
->nm_sprinc
));
6164 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_NFS_PORT
)) {
6165 struct sockaddr_un
*un
= (struct sockaddr_un
*)nmp
->nm_saddr
;
6166 xb_add_string(error
, &xbinfo
, un
->sun_path
, strlen(un
->sun_path
));
6168 if (NFS_BITMAP_ISSET(mattrs
, NFS_MATTR_LOCAL_MOUNT_PORT
)) {
6169 xb_add_string(error
, &xbinfo
, nmp
->nm_mount_localport
, strlen(nmp
->nm_mount_localport
));
6171 curargs_end_offset
= xb_offset(&xbinfo
);
6173 /* NFS_MIATTR_CUR_LOC_INDEX */
6174 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_flags
);
6175 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_loc
);
6176 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_serv
);
6177 xb_add_32(error
, &xbinfo
, nmp
->nm_locations
.nl_current
.nli_addr
);
6179 xb_build_done(error
, &xbinfo
);
6181 /* update opaque counts */
6182 end_offset
= xb_offset(&xbinfo
);
6184 error
= xb_seek(&xbinfo
, attrslength_offset
);
6185 xb_add_32(error
, &xbinfo
, curargs_end_offset
- attrslength_offset
- XDRWORD
/*don't include length field*/);
6188 error
= xb_seek(&xbinfo
, curargslength_offset
);
6189 xb_add_32(error
, &xbinfo
, curargs_end_offset
- curargslength_offset
+ XDRWORD
/*version*/);
6192 error
= xb_seek(&xbinfo
, curargsopaquelength_offset
);
6193 xb_add_32(error
, &xbinfo
, curargs_end_offset
- curargslength_offset
+ XDRWORD
/*version*/);
6196 error
= xb_seek(&xbinfo
, infolength_offset
);
6197 xb_add_32(error
, &xbinfo
, end_offset
- infolength_offset
+ XDRWORD
/*version*/);
6201 /* copy result xdrbuf to caller */
6204 /* and mark the local copy as not needing cleanup */
6205 xbinfo
.xb_flags
&= ~XB_CLEANUP
;
6207 xb_cleanup(&xbinfo
);
6212 * Do that sysctl thang...
6215 nfs_vfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
6216 user_addr_t newp
, size_t newlen
, vfs_context_t ctx
)
6219 struct sysctl_req
*req
= NULL
;
6220 union union_vfsidctl vc
;
6222 struct nfsmount
*nmp
= NULL
;
6225 boolean_t is_64_bit
;
6228 struct netfs_status
*nsp
= NULL
;
6230 uint totlen
, count
, numThreads
;
6231 #if CONFIG_NFS_SERVER
6233 struct nfs_exportfs
*nxfs
;
6234 struct nfs_export
*nx
;
6235 struct nfs_active_user_list
*ulist
;
6236 struct nfs_export_stat_desc stat_desc
= {};
6237 struct nfs_export_stat_rec statrec
;
6238 struct nfs_user_stat_node
*unode
, *unode_next
;
6239 struct nfs_user_stat_desc ustat_desc
= {};
6240 struct nfs_user_stat_user_rec ustat_rec
;
6241 struct nfs_user_stat_path_rec upath_rec
;
6242 uint bytes_total
, recs_copied
;
6244 size_t bytes_avail
, numRecs
;
6245 #endif /* CONFIG_NFS_SERVER */
6248 * All names at this level are terminal.
6251 return ENOTDIR
; /* overloaded */
6253 is_64_bit
= vfs_context_is64bit(ctx
);
6255 /* common code for "new style" VFS_CTL sysctl, get the mount. */
6258 case VFS_CTL_NOLOCKS
:
6259 case VFS_CTL_NSTATUS
:
6260 #if defined(XNU_TARGET_OS_OSX)
6262 #endif /* XNU_TARGET_OS_OSX */
6263 req
= CAST_DOWN(struct sysctl_req
*, oldp
);
6267 error
= SYSCTL_IN(req
, &vc
, is_64_bit
? sizeof(vc
.vc64
):sizeof(vc
.vc32
));
6271 mp
= vfs_getvfs(&vc
.vc32
.vc_fsid
); /* works for 32 and 64 */
6279 bzero(&vq
, sizeof(vq
));
6282 req
->newptr
= vc
.vc64
.vc_ptr
;
6283 req
->newlen
= (size_t)vc
.vc64
.vc_len
;
6285 req
->newptr
= CAST_USER_ADDR_T(vc
.vc32
.vc_ptr
);
6286 req
->newlen
= vc
.vc32
.vc_len
;
6289 #if !defined(XNU_TARGET_OS_OSX)
6292 #endif /* ! XNU_TARGET_OS_OSX */
6298 *oldlenp
= sizeof nfsstats
;
6302 if (*oldlenp
< sizeof nfsstats
) {
6303 *oldlenp
= sizeof nfsstats
;
6307 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
6312 if (newp
&& newlen
!= sizeof nfsstats
) {
6317 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
6320 case NFS_NFSZEROSTATS
:
6321 bzero(&nfsstats
, sizeof nfsstats
);
6324 /* read in the fsid */
6325 if (*oldlenp
< sizeof(fsid
)) {
6328 if ((error
= copyin(oldp
, &fsid
, sizeof(fsid
)))) {
6331 /* swizzle it back to host order */
6332 fsid
.val
[0] = ntohl(fsid
.val
[0]);
6333 fsid
.val
[1] = ntohl(fsid
.val
[1]);
6334 /* find mount and make sure it's NFS */
6335 if (((mp
= vfs_getvfs(&fsid
))) == NULL
) {
6338 if (strcmp(mp
->mnt_vfsstat
.f_fstypename
, "nfs")) {
6341 if (((nmp
= VFSTONFS(mp
))) == NULL
) {
6344 xb_init(&xb
, XDRBUF_NONE
);
6345 if ((error
= nfs_mountinfo_assemble(nmp
, &xb
))) {
6348 if (*oldlenp
< xb
.xb_u
.xb_buffer
.xbb_len
) {
6351 error
= copyout(xb_buffer_base(&xb
), oldp
, xb
.xb_u
.xb_buffer
.xbb_len
);
6353 *oldlenp
= xb
.xb_u
.xb_buffer
.xbb_len
;
6356 #if CONFIG_NFS_SERVER
6357 case NFS_EXPORTSTATS
:
6358 /* setup export stat descriptor */
6359 stat_desc
.rec_vers
= NFS_EXPORT_STAT_REC_VERSION
;
6361 if (!nfsrv_is_initialized()) {
6362 stat_desc
.rec_count
= 0;
6363 if (oldp
&& (*oldlenp
>= sizeof(struct nfs_export_stat_desc
))) {
6364 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
6366 *oldlenp
= sizeof(struct nfs_export_stat_desc
);
6370 /* Count the number of exported directories */
6371 lck_rw_lock_shared(&nfsrv_export_rwlock
);
6373 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
)
6374 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
)
6377 /* update stat descriptor's export record count */
6378 stat_desc
.rec_count
= numExports
;
6380 /* calculate total size of required buffer */
6381 totlen
= sizeof(struct nfs_export_stat_desc
) + (numExports
* sizeof(struct nfs_export_stat_rec
));
6383 /* Check caller's buffer */
6385 lck_rw_done(&nfsrv_export_rwlock
);
6386 /* indicate required buffer len */
6391 /* We require the caller's buffer to be at least large enough to hold the descriptor */
6392 if (*oldlenp
< sizeof(struct nfs_export_stat_desc
)) {
6393 lck_rw_done(&nfsrv_export_rwlock
);
6394 /* indicate required buffer len */
6399 /* indicate required buffer len */
6402 /* check if export table is empty */
6404 lck_rw_done(&nfsrv_export_rwlock
);
6405 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
6409 /* calculate how many actual export stat records fit into caller's buffer */
6410 numRecs
= (*oldlenp
- sizeof(struct nfs_export_stat_desc
)) / sizeof(struct nfs_export_stat_rec
);
6413 /* caller's buffer can only accomodate descriptor */
6414 lck_rw_done(&nfsrv_export_rwlock
);
6415 stat_desc
.rec_count
= 0;
6416 error
= copyout(&stat_desc
, oldp
, sizeof(struct nfs_export_stat_desc
));
6420 /* adjust to actual number of records to copyout to caller's buffer */
6421 if (numRecs
> numExports
) {
6422 numRecs
= numExports
;
6425 /* set actual number of records we are returning */
6426 stat_desc
.rec_count
= numRecs
;
6428 /* first copy out the stat descriptor */
6430 error
= copyout(&stat_desc
, oldp
+ pos
, sizeof(struct nfs_export_stat_desc
));
6432 lck_rw_done(&nfsrv_export_rwlock
);
6435 pos
+= sizeof(struct nfs_export_stat_desc
);
6437 /* Loop through exported directories */
6439 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
6440 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
6441 if (count
>= numRecs
) {
6445 /* build exported filesystem path */
6446 memset(statrec
.path
, 0, sizeof(statrec
.path
));
6447 snprintf(statrec
.path
, sizeof(statrec
.path
), "%s%s%s",
6448 nxfs
->nxfs_path
, ((nxfs
->nxfs_path
[1] && nx
->nx_path
[0]) ? "/" : ""),
6451 /* build the 64-bit export stat counters */
6452 statrec
.ops
= ((uint64_t)nx
->nx_stats
.ops
.hi
<< 32) |
6453 nx
->nx_stats
.ops
.lo
;
6454 statrec
.bytes_read
= ((uint64_t)nx
->nx_stats
.bytes_read
.hi
<< 32) |
6455 nx
->nx_stats
.bytes_read
.lo
;
6456 statrec
.bytes_written
= ((uint64_t)nx
->nx_stats
.bytes_written
.hi
<< 32) |
6457 nx
->nx_stats
.bytes_written
.lo
;
6458 error
= copyout(&statrec
, oldp
+ pos
, sizeof(statrec
));
6460 lck_rw_done(&nfsrv_export_rwlock
);
6463 /* advance buffer position */
6464 pos
+= sizeof(statrec
);
6467 lck_rw_done(&nfsrv_export_rwlock
);
6470 /* init structures used for copying out of kernel */
6471 ustat_desc
.rec_vers
= NFS_USER_STAT_REC_VERSION
;
6472 ustat_rec
.rec_type
= NFS_USER_STAT_USER_REC
;
6473 upath_rec
.rec_type
= NFS_USER_STAT_PATH_REC
;
6475 /* initialize counters */
6476 bytes_total
= sizeof(struct nfs_user_stat_desc
);
6477 bytes_avail
= *oldlenp
;
6480 if (!nfsrv_is_initialized()) { /* NFS server not initialized, so no stats */
6484 /* reclaim old expired user nodes */
6485 nfsrv_active_user_list_reclaim();
6487 /* reserve space for the buffer descriptor */
6488 if (bytes_avail
>= sizeof(struct nfs_user_stat_desc
)) {
6489 bytes_avail
-= sizeof(struct nfs_user_stat_desc
);
6494 /* put buffer position past the buffer descriptor */
6495 pos
= sizeof(struct nfs_user_stat_desc
);
6497 /* Loop through exported directories */
6498 lck_rw_lock_shared(&nfsrv_export_rwlock
);
6499 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
6500 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
6502 if (bytes_avail
>= sizeof(struct nfs_user_stat_path_rec
)) {
6503 memset(upath_rec
.path
, 0, sizeof(upath_rec
.path
));
6504 snprintf(upath_rec
.path
, sizeof(upath_rec
.path
), "%s%s%s",
6505 nxfs
->nxfs_path
, ((nxfs
->nxfs_path
[1] && nx
->nx_path
[0]) ? "/" : ""),
6508 error
= copyout(&upath_rec
, oldp
+ pos
, sizeof(struct nfs_user_stat_path_rec
));
6514 pos
+= sizeof(struct nfs_user_stat_path_rec
);
6515 bytes_avail
-= sizeof(struct nfs_user_stat_path_rec
);
6518 /* Caller's buffer is exhausted */
6522 bytes_total
+= sizeof(struct nfs_user_stat_path_rec
);
6524 /* Scan through all user nodes of this export */
6525 ulist
= &nx
->nx_user_list
;
6526 lck_mtx_lock(&ulist
->user_mutex
);
6527 for (unode
= TAILQ_FIRST(&ulist
->user_lru
); unode
; unode
= unode_next
) {
6528 unode_next
= TAILQ_NEXT(unode
, lru_link
);
6530 /* copy out node if there is space */
6531 if (bytes_avail
>= sizeof(struct nfs_user_stat_user_rec
)) {
6532 /* prepare a user stat rec for copying out */
6533 ustat_rec
.uid
= unode
->uid
;
6534 memset(&ustat_rec
.sock
, 0, sizeof(ustat_rec
.sock
));
6535 bcopy(&unode
->sock
, &ustat_rec
.sock
, unode
->sock
.ss_len
);
6536 ustat_rec
.ops
= unode
->ops
;
6537 ustat_rec
.bytes_read
= unode
->bytes_read
;
6538 ustat_rec
.bytes_written
= unode
->bytes_written
;
6539 ustat_rec
.tm_start
= unode
->tm_start
;
6540 ustat_rec
.tm_last
= unode
->tm_last
;
6542 error
= copyout(&ustat_rec
, oldp
+ pos
, sizeof(struct nfs_user_stat_user_rec
));
6546 lck_mtx_unlock(&ulist
->user_mutex
);
6550 pos
+= sizeof(struct nfs_user_stat_user_rec
);
6551 bytes_avail
-= sizeof(struct nfs_user_stat_user_rec
);
6554 /* Caller's buffer is exhausted */
6557 bytes_total
+= sizeof(struct nfs_user_stat_user_rec
);
6559 /* can unlock this export's list now */
6560 lck_mtx_unlock(&ulist
->user_mutex
);
6565 /* unlock the export table */
6566 lck_rw_done(&nfsrv_export_rwlock
);
6569 /* indicate number of actual records copied */
6570 ustat_desc
.rec_count
= recs_copied
;
6573 /* check if there was enough room for the buffer descriptor */
6574 if (*oldlenp
>= sizeof(struct nfs_user_stat_desc
)) {
6575 error
= copyout(&ustat_desc
, oldp
, sizeof(struct nfs_user_stat_desc
));
6580 /* always indicate required buffer size */
6581 *oldlenp
= bytes_total
;
6586 *oldlenp
= sizeof(nfsrv_user_stat_node_count
);
6590 if (*oldlenp
< sizeof(nfsrv_user_stat_node_count
)) {
6591 *oldlenp
= sizeof(nfsrv_user_stat_node_count
);
6595 if (nfsrv_is_initialized()) {
6596 /* reclaim old expired user nodes */
6597 nfsrv_active_user_list_reclaim();
6600 error
= copyout(&nfsrv_user_stat_node_count
, oldp
, sizeof(nfsrv_user_stat_node_count
));
6602 #endif /* CONFIG_NFS_SERVER */
6603 case VFS_CTL_NOLOCKS
:
6604 if (req
->oldptr
!= USER_ADDR_NULL
) {
6605 lck_mtx_lock(&nmp
->nm_lock
);
6606 val
= (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
) ? 1 : 0;
6607 lck_mtx_unlock(&nmp
->nm_lock
);
6608 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
6613 if (req
->newptr
!= USER_ADDR_NULL
) {
6614 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
6618 lck_mtx_lock(&nmp
->nm_lock
);
6619 if (nmp
->nm_lockmode
== NFS_LOCK_MODE_LOCAL
) {
6620 /* can't toggle locks when using local locks */
6623 } else if ((nmp
->nm_vers
>= NFS_VER4
) && val
) {
6624 /* can't disable locks for NFSv4 */
6628 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
)) {
6629 nfs_lockd_mount_unregister(nmp
);
6631 nmp
->nm_lockmode
= NFS_LOCK_MODE_DISABLED
;
6632 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
6634 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
)) {
6635 nfs_lockd_mount_register(nmp
);
6637 nmp
->nm_lockmode
= NFS_LOCK_MODE_ENABLED
;
6639 lck_mtx_unlock(&nmp
->nm_lock
);
6642 #if defined(XNU_TARGET_OS_OSX)
6644 lck_mtx_lock(&nmp
->nm_lock
);
6645 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
6646 int softnobrowse
= (NMFLAG(nmp
, SOFT
) && (vfs_flags(nmp
->nm_mountp
) & MNT_DONTBROWSE
));
6647 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_TIMEO
)) {
6648 vq
.vq_flags
|= VQ_NOTRESP
;
6650 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_JUKEBOXTIMEO
) && !NMFLAG(nmp
, MUTEJUKEBOX
)) {
6651 vq
.vq_flags
|= VQ_NOTRESP
;
6653 if (!softnobrowse
&& (nmp
->nm_state
& NFSSTA_LOCKTIMEO
) &&
6654 (nmp
->nm_lockmode
== NFS_LOCK_MODE_ENABLED
)) {
6655 vq
.vq_flags
|= VQ_NOTRESP
;
6657 if (nmp
->nm_state
& NFSSTA_DEAD
) {
6658 vq
.vq_flags
|= VQ_DEAD
;
6660 lck_mtx_unlock(&nmp
->nm_lock
);
6661 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
6663 #endif /* XNU_TARGET_OS_OSX */
6665 if (req
->oldptr
!= USER_ADDR_NULL
) {
6666 lck_mtx_lock(&nmp
->nm_lock
);
6667 val
= nmp
->nm_tprintf_initial_delay
;
6668 lck_mtx_unlock(&nmp
->nm_lock
);
6669 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
6674 if (req
->newptr
!= USER_ADDR_NULL
) {
6675 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
6679 lck_mtx_lock(&nmp
->nm_lock
);
6681 nmp
->nm_tprintf_initial_delay
= 0;
6683 nmp
->nm_tprintf_initial_delay
= val
;
6685 lck_mtx_unlock(&nmp
->nm_lock
);
6688 case VFS_CTL_NSTATUS
:
6690 * Return the status of this mount. This is much more
6691 * information than VFS_CTL_QUERY. In addition to the
6692 * vq_flags return the significant mount options along
6693 * with the list of threads blocked on the mount and
6694 * how long the threads have been waiting.
6697 lck_mtx_lock(&nfs_request_mutex
);
6698 lck_mtx_lock(&nmp
->nm_lock
);
6701 * Count the number of requests waiting for a reply.
6702 * Note: there could be multiple requests from the same thread.
6705 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
6706 if (rq
->r_nmp
== nmp
) {
6711 /* Calculate total size of result buffer */
6712 totlen
= sizeof(struct netfs_status
) + (numThreads
* sizeof(uint64_t));
6714 if (req
->oldptr
== USER_ADDR_NULL
) { // Caller is querying buffer size
6715 lck_mtx_unlock(&nmp
->nm_lock
);
6716 lck_mtx_unlock(&nfs_request_mutex
);
6717 return SYSCTL_OUT(req
, NULL
, totlen
);
6719 if (req
->oldlen
< totlen
) { // Check if caller's buffer is big enough
6720 lck_mtx_unlock(&nmp
->nm_lock
);
6721 lck_mtx_unlock(&nfs_request_mutex
);
6725 MALLOC(nsp
, struct netfs_status
*, totlen
, M_TEMP
, M_WAITOK
| M_ZERO
);
6727 lck_mtx_unlock(&nmp
->nm_lock
);
6728 lck_mtx_unlock(&nfs_request_mutex
);
6731 timeoutmask
= NFSSTA_TIMEO
| NFSSTA_LOCKTIMEO
| NFSSTA_JUKEBOXTIMEO
;
6732 if (nmp
->nm_state
& timeoutmask
) {
6733 nsp
->ns_status
|= VQ_NOTRESP
;
6735 if (nmp
->nm_state
& NFSSTA_DEAD
) {
6736 nsp
->ns_status
|= VQ_DEAD
;
6739 (void) nfs_mountopts(nmp
, nsp
->ns_mountopts
, sizeof(nsp
->ns_mountopts
));
6740 nsp
->ns_threadcount
= numThreads
;
6743 * Get the thread ids of threads waiting for a reply
6744 * and find the longest wait time.
6746 if (numThreads
> 0) {
6753 sendtime
= now
.tv_sec
;
6754 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
6755 if (rq
->r_nmp
== nmp
) {
6756 if (rq
->r_start
< sendtime
) {
6757 sendtime
= rq
->r_start
;
6759 // A thread_id of zero is used to represent an async I/O request.
6760 nsp
->ns_threadids
[count
] =
6761 rq
->r_thread
? thread_tid(rq
->r_thread
) : 0;
6762 if (++count
>= numThreads
) {
6767 waittime
= now
.tv_sec
- sendtime
;
6768 nsp
->ns_waittime
= waittime
> UINT32_MAX
? UINT32_MAX
: (uint32_t)waittime
;
6771 lck_mtx_unlock(&nmp
->nm_lock
);
6772 lck_mtx_unlock(&nfs_request_mutex
);
6774 error
= SYSCTL_OUT(req
, nsp
, totlen
);
6783 #endif /* CONFIG_NFS_CLIENT */