]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_vfsops.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
65 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
1c79356b 66 */
2d21ac55
A
67/*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
1c79356b
A
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/conf.h>
77#include <sys/ioctl.h>
78#include <sys/signal.h>
91447636
A
79#include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
80#include <sys/kauth.h>
81#include <sys/vnode_internal.h>
1c79356b
A
82#include <sys/malloc.h>
83#include <sys/kernel.h>
84#include <sys/sysctl.h>
91447636
A
85#include <sys/mount_internal.h>
86#include <sys/kpi_mbuf.h>
1c79356b
A
87#include <sys/socket.h>
88#include <sys/socketvar.h>
91447636 89#include <sys/fcntl.h>
2d21ac55 90#include <sys/quota.h>
91447636 91#include <libkern/OSAtomic.h>
1c79356b
A
92
93#include <sys/vm.h>
94#include <sys/vmparam.h>
95
96#if !defined(NO_MOUNT_PRIVATE)
97#include <sys/filedesc.h>
98#endif /* NO_MOUNT_PRIVATE */
99
100#include <net/if.h>
101#include <net/route.h>
102#include <netinet/in.h>
103
104#include <nfs/rpcv2.h>
2d21ac55 105#include <nfs/krpc.h>
1c79356b
A
106#include <nfs/nfsproto.h>
107#include <nfs/nfs.h>
108#include <nfs/nfsnode.h>
2d21ac55 109#include <nfs/nfs_gss.h>
1c79356b
A
110#include <nfs/nfsmount.h>
111#include <nfs/xdr_subs.h>
112#include <nfs/nfsm_subs.h>
113#include <nfs/nfsdiskless.h>
91447636 114#include <nfs/nfs_lock.h>
2d21ac55
A
115#if CONFIG_MACF
116#include <security/mac_framework.h>
1c79356b 117#endif
1c79356b 118
2d21ac55 119#include <pexpert/pexpert.h>
1c79356b 120
2d21ac55
A
121/*
122 * NFS client globals
123 */
124
125int nfs_ticks;
b0d623f7
A
126static lck_grp_t *nfs_global_grp, *nfs_mount_grp;
127lck_mtx_t *nfs_global_mutex;
2d21ac55
A
128uint32_t nfs_fs_attr_bitmap[NFS_ATTR_BITMAP_LEN];
129uint32_t nfs_object_attr_bitmap[NFS_ATTR_BITMAP_LEN];
130uint32_t nfs_getattr_bitmap[NFS_ATTR_BITMAP_LEN];
b0d623f7 131struct nfsclientidlist nfsclientids;
2d21ac55
A
132
133/* NFS requests */
134struct nfs_reqqhead nfs_reqq;
135lck_grp_t *nfs_request_grp;
136lck_mtx_t *nfs_request_mutex;
137thread_call_t nfs_request_timer_call;
138int nfs_request_timer_on;
b0d623f7
A
139u_int32_t nfs_xid = 0;
140u_int32_t nfs_xidwrap = 0; /* to build a (non-wrapping) 64 bit xid */
2d21ac55
A
141
142thread_call_t nfs_buf_timer_call;
143
b0d623f7
A
144/* NFSv4 */
145lck_grp_t *nfs_open_grp;
146uint32_t nfs_open_owner_seqnum = 0;
147uint32_t nfs_lock_owner_seqnum = 0;
148thread_call_t nfs4_callback_timer_call;
149int nfs4_callback_timer_on = 0;
150
2d21ac55
A
151/* nfsiod */
152lck_grp_t *nfsiod_lck_grp;
153lck_mtx_t *nfsiod_mutex;
154struct nfsiodlist nfsiodfree, nfsiodwork;
155struct nfsiodmountlist nfsiodmounts;
156int nfsiod_thread_count = 0;
157int nfsiod_thread_max = NFS_DEFASYNCTHREAD;
158int nfs_max_async_writes = NFS_DEFMAXASYNCWRITES;
159
160int nfs_iosize = NFS_IOSIZE;
161int nfs_access_cache_timeout = NFS_MAXATTRTIMO;
b0d623f7 162int nfs_access_delete = 0;
2d21ac55
A
163int nfs_allow_async = 0;
164int nfs_statfs_rate_limit = NFS_DEFSTATFSRATELIMIT;
165int nfs_lockd_mounts = 0;
166int nfs_lockd_request_sent = 0;
167
168int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
169int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
170
171
b0d623f7 172int mountnfs(struct user_nfs_args *,mount_t,mbuf_t,vfs_context_t,vnode_t *);
2d21ac55
A
173static int nfs_mount_diskless(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *, vfs_context_t);
174#if !defined(NO_MOUNT_PRIVATE)
175static int nfs_mount_diskless_private(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *, vfs_context_t);
176#endif /* NO_MOUNT_PRIVATE */
1c79356b
A
177
178/*
2d21ac55 179 * NFS VFS operations.
1c79356b 180 */
b0d623f7
A
181int nfs_vfs_mount(mount_t, vnode_t, user_addr_t, vfs_context_t);
182int nfs_vfs_start(mount_t, int, vfs_context_t);
183int nfs_vfs_unmount(mount_t, int, vfs_context_t);
184int nfs_vfs_root(mount_t, vnode_t *, vfs_context_t);
185int nfs_vfs_quotactl(mount_t, int, uid_t, caddr_t, vfs_context_t);
186int nfs_vfs_getattr(mount_t, struct vfs_attr *, vfs_context_t);
187int nfs_vfs_sync(mount_t, int, vfs_context_t);
188int nfs_vfs_vget(mount_t, ino64_t, vnode_t *, vfs_context_t);
189int nfs_vfs_vptofh(vnode_t, int *, unsigned char *, vfs_context_t);
190int nfs_vfs_fhtovp(mount_t, int, unsigned char *, vnode_t *, vfs_context_t);
191int nfs_vfs_init(struct vfsconf *);
192int nfs_vfs_sysctl(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t);
2d21ac55 193
1c79356b 194struct vfsops nfs_vfsops = {
2d21ac55
A
195 nfs_vfs_mount,
196 nfs_vfs_start,
197 nfs_vfs_unmount,
198 nfs_vfs_root,
199 nfs_vfs_quotactl,
91447636 200 nfs_vfs_getattr,
2d21ac55
A
201 nfs_vfs_sync,
202 nfs_vfs_vget,
203 nfs_vfs_fhtovp,
204 nfs_vfs_vptofh,
205 nfs_vfs_init,
206 nfs_vfs_sysctl,
207 NULL, /* setattr */
208 { NULL, /* reserved */
209 NULL, /* reserved */
210 NULL, /* reserved */
211 NULL, /* reserved */
212 NULL, /* reserved */
213 NULL, /* reserved */
214 NULL } /* reserved */
1c79356b 215};
1c79356b 216
1c79356b 217
2d21ac55
A
218/*
219 * version-specific NFS functions
220 */
b0d623f7
A
221int nfs3_mount(struct nfsmount *, vfs_context_t, struct user_nfs_args *, nfsnode_t *);
222int nfs4_mount(struct nfsmount *, vfs_context_t, struct user_nfs_args *, nfsnode_t *);
223int nfs3_fsinfo(struct nfsmount *, nfsnode_t, vfs_context_t);
224int nfs3_update_statfs(struct nfsmount *, vfs_context_t);
225int nfs4_update_statfs(struct nfsmount *, vfs_context_t);
2d21ac55
A
226#if !QUOTA
227#define nfs3_getquota NULL
228#define nfs4_getquota NULL
229#else
b0d623f7
A
230int nfs3_getquota(struct nfsmount *, vfs_context_t, uid_t, int, struct dqblk *);
231int nfs4_getquota(struct nfsmount *, vfs_context_t, uid_t, int, struct dqblk *);
2d21ac55 232#endif
1c79356b 233
2d21ac55
A
234struct nfs_funcs nfs3_funcs = {
235 nfs3_mount,
236 nfs3_update_statfs,
237 nfs3_getquota,
238 nfs3_access_rpc,
239 nfs3_getattr_rpc,
240 nfs3_setattr_rpc,
241 nfs3_read_rpc_async,
242 nfs3_read_rpc_async_finish,
243 nfs3_readlink_rpc,
244 nfs3_write_rpc_async,
245 nfs3_write_rpc_async_finish,
246 nfs3_commit_rpc,
247 nfs3_lookup_rpc_async,
248 nfs3_lookup_rpc_async_finish,
249 nfs3_remove_rpc,
250 nfs3_rename_rpc
251 };
252struct nfs_funcs nfs4_funcs = {
253 nfs4_mount,
254 nfs4_update_statfs,
255 nfs4_getquota,
256 nfs4_access_rpc,
257 nfs4_getattr_rpc,
258 nfs4_setattr_rpc,
259 nfs4_read_rpc_async,
260 nfs4_read_rpc_async_finish,
261 nfs4_readlink_rpc,
262 nfs4_write_rpc_async,
263 nfs4_write_rpc_async_finish,
264 nfs4_commit_rpc,
265 nfs4_lookup_rpc_async,
266 nfs4_lookup_rpc_async_finish,
267 nfs4_remove_rpc,
268 nfs4_rename_rpc
269 };
270
271/*
272 * Called once to initialize data structures...
273 */
b0d623f7
A
274int
275nfs_vfs_init(__unused struct vfsconf *vfsp)
1c79356b 276{
2d21ac55 277 int i;
1c79356b
A
278
279 /*
2d21ac55 280 * Check to see if major data structures haven't bloated.
1c79356b 281 */
2d21ac55
A
282 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
283 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
284 printf("Try reducing NFS_SMALLFH\n");
285 }
b0d623f7 286 if (sizeof (struct nfsmount) > NFS_MNTALLOC)
2d21ac55 287 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
2d21ac55
A
288
289 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
290 if (nfs_ticks < 1)
291 nfs_ticks = 1;
292
293 /* init async I/O thread pool state */
294 TAILQ_INIT(&nfsiodfree);
295 TAILQ_INIT(&nfsiodwork);
296 TAILQ_INIT(&nfsiodmounts);
297 nfsiod_lck_grp = lck_grp_alloc_init("nfsiod", LCK_GRP_ATTR_NULL);
298 nfsiod_mutex = lck_mtx_alloc_init(nfsiod_lck_grp, LCK_ATTR_NULL);
299
b0d623f7 300 /* init lock groups, etc. */
2d21ac55 301 nfs_mount_grp = lck_grp_alloc_init("nfs_mount", LCK_GRP_ATTR_NULL);
b0d623f7
A
302 nfs_open_grp = lck_grp_alloc_init("nfs_open", LCK_GRP_ATTR_NULL);
303 nfs_global_grp = lck_grp_alloc_init("nfs_global", LCK_GRP_ATTR_NULL);
304
305 nfs_global_mutex = lck_mtx_alloc_init(nfs_global_grp, LCK_ATTR_NULL);
2d21ac55
A
306
307 /* init request list mutex */
308 nfs_request_grp = lck_grp_alloc_init("nfs_request", LCK_GRP_ATTR_NULL);
309 nfs_request_mutex = lck_mtx_alloc_init(nfs_request_grp, LCK_ATTR_NULL);
310
311 /* initialize NFS request list */
312 TAILQ_INIT(&nfs_reqq);
313
314 nfs_nbinit(); /* Init the nfsbuf table */
315 nfs_nhinit(); /* Init the nfsnode table */
316 nfs_lockinit(); /* Init the nfs lock state */
317 nfs_gss_init(); /* Init RPCSEC_GSS security */
318
319 /* NFSv4 stuff */
320 NFS4_PER_FS_ATTRIBUTES(nfs_fs_attr_bitmap);
321 NFS4_PER_OBJECT_ATTRIBUTES(nfs_object_attr_bitmap);
322 NFS4_DEFAULT_ATTRIBUTES(nfs_getattr_bitmap);
323 for (i=0; i < NFS_ATTR_BITMAP_LEN; i++)
324 nfs_getattr_bitmap[i] &= nfs_object_attr_bitmap[i];
b0d623f7 325 TAILQ_INIT(&nfsclientids);
2d21ac55
A
326
327 /* initialize NFS timer callouts */
328 nfs_request_timer_call = thread_call_allocate(nfs_request_timer, NULL);
329 nfs_buf_timer_call = thread_call_allocate(nfs_buf_timer, NULL);
b0d623f7 330 nfs4_callback_timer_call = thread_call_allocate(nfs4_callback_timer, NULL);
2d21ac55 331
2d21ac55 332 return (0);
1c79356b
A
333}
334
1c79356b
A
335/*
336 * nfs statfs call
337 */
b0d623f7 338int
2d21ac55 339nfs3_update_statfs(struct nfsmount *nmp, vfs_context_t ctx)
1c79356b 340{
2d21ac55
A
341 nfsnode_t np;
342 int error = 0, lockerror, status, nfsvers;
fa4905b1 343 u_int64_t xid;
2d21ac55 344 struct nfsm_chain nmreq, nmrep;
b0d623f7 345 uint32_t val = 0;
1c79356b 346
2d21ac55
A
347 nfsvers = nmp->nm_vers;
348 np = nmp->nm_dnp;
b0d623f7
A
349 if (!np)
350 return (ENXIO);
2d21ac55 351 if ((error = vnode_get(NFSTOV(np))))
b0d623f7 352 return (error);
91447636 353
2d21ac55
A
354 nfsm_chain_null(&nmreq);
355 nfsm_chain_null(&nmrep);
356
357 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers));
358 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
359 nfsm_chain_build_done(error, &nmreq);
360 nfsmout_if(error);
361 error = nfs_request(np, NULL, &nmreq, NFSPROC_FSSTAT, ctx,
362 &nmrep, &xid, &status);
b0d623f7 363 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
364 error = lockerror;
365 if (nfsvers == NFS_VER3)
366 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
367 if (!lockerror)
b0d623f7 368 nfs_node_unlock(np);
2d21ac55
A
369 if (!error)
370 error = status;
371 nfsm_assert(error, NFSTONMP(np), ENXIO);
372 nfsmout_if(error);
373 lck_mtx_lock(&nmp->nm_lock);
374 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL);
375 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE);
376 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_AVAIL);
377 if (nfsvers == NFS_VER3) {
378 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_AVAIL);
379 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_TOTAL);
380 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_FREE);
381 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
382 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_total);
383 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_free);
384 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_space_avail);
385 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_total);
386 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_free);
387 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_files_avail);
388 // skip invarsec
389 } else {
390 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip tsize?
391 nfsm_chain_get_32(error, &nmrep, nmp->nm_fsattr.nfsa_bsize);
392 nfsm_chain_get_32(error, &nmrep, val);
393 nfsmout_if(error);
394 if (nmp->nm_fsattr.nfsa_bsize <= 0)
395 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
396 nmp->nm_fsattr.nfsa_space_total = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
397 nfsm_chain_get_32(error, &nmrep, val);
398 nfsmout_if(error);
399 nmp->nm_fsattr.nfsa_space_free = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
400 nfsm_chain_get_32(error, &nmrep, val);
401 nfsmout_if(error);
402 nmp->nm_fsattr.nfsa_space_avail = (uint64_t)val * nmp->nm_fsattr.nfsa_bsize;
91447636 403 }
2d21ac55
A
404 lck_mtx_unlock(&nmp->nm_lock);
405nfsmout:
406 nfsm_chain_cleanup(&nmreq);
407 nfsm_chain_cleanup(&nmrep);
408 vnode_put(NFSTOV(np));
409 return (error);
410}
e5568f75 411
b0d623f7 412int
2d21ac55
A
413nfs4_update_statfs(struct nfsmount *nmp, vfs_context_t ctx)
414{
415 nfsnode_t np;
416 int error = 0, lockerror, status, nfsvers, numops;
417 u_int64_t xid;
418 struct nfsm_chain nmreq, nmrep;
419 uint32_t bitmap[NFS_ATTR_BITMAP_LEN];
420 struct nfs_vattr nvattr;
e5568f75 421
2d21ac55
A
422 nfsvers = nmp->nm_vers;
423 np = nmp->nm_dnp;
b0d623f7
A
424 if (!np)
425 return (ENXIO);
2d21ac55 426 if ((error = vnode_get(NFSTOV(np))))
b0d623f7 427 return (error);
e5568f75 428
2d21ac55
A
429 nfsm_chain_null(&nmreq);
430 nfsm_chain_null(&nmrep);
431
432 // PUTFH + GETATTR
433 numops = 2;
434 nfsm_chain_build_alloc_init(error, &nmreq, 15 * NFSX_UNSIGNED);
435 nfsm_chain_add_compound_header(error, &nmreq, "statfs", numops);
436 numops--;
437 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
438 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
439 numops--;
440 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
441 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap, bitmap);
442 NFS4_STATFS_ATTRIBUTES(bitmap);
443 nfsm_chain_add_bitmap_masked(error, &nmreq, bitmap,
444 NFS_ATTR_BITMAP_LEN, nmp->nm_fsattr.nfsa_supp_attr);
445 nfsm_chain_build_done(error, &nmreq);
446 nfsm_assert(error, (numops == 0), EPROTO);
447 nfsmout_if(error);
448 error = nfs_request(np, NULL, &nmreq, NFSPROC4_COMPOUND, ctx, &nmrep, &xid, &status);
449 nfsm_chain_skip_tag(error, &nmrep);
450 nfsm_chain_get_32(error, &nmrep, numops);
451 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
452 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
453 nfsm_assert(error, NFSTONMP(np), ENXIO);
454 nfsmout_if(error);
455 lck_mtx_lock(&nmp->nm_lock);
456 NFS_CLEAR_ATTRIBUTES(nvattr.nva_bitmap);
457 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, NULL, NULL);
458 lck_mtx_unlock(&nmp->nm_lock);
459 nfsmout_if(error);
b0d623f7 460 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
461 error = lockerror;
462 if (!error)
463 nfs_loadattrcache(np, &nvattr, &xid, 0);
464 if (!lockerror)
b0d623f7 465 nfs_node_unlock(np);
2d21ac55
A
466 nfsm_assert(error, NFSTONMP(np), ENXIO);
467 nfsmout_if(error);
468 nmp->nm_fsattr.nfsa_bsize = NFS_FABLKSIZE;
469nfsmout:
470 nfsm_chain_cleanup(&nmreq);
471 nfsm_chain_cleanup(&nmrep);
472 vnode_put(NFSTOV(np));
91447636
A
473 return (error);
474}
475
2d21ac55 476
91447636 477/*
2d21ac55
A
478 * The NFS VFS_GETATTR function: "statfs"-type information is retrieved
479 * using the nf_update_statfs() function, and other attributes are cobbled
480 * together from whatever sources we can (getattr, fsinfo, pathconf).
91447636 481 */
b0d623f7 482int
2d21ac55 483nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t ctx)
91447636 484{
2d21ac55
A
485 struct nfsmount *nmp;
486 uint32_t bsize;
487 int error = 0, nfsvers;
488
489 if (!(nmp = VFSTONFS(mp)))
490 return (ENXIO);
491 nfsvers = nmp->nm_vers;
492
91447636
A
493 if (VFSATTR_IS_ACTIVE(fsap, f_bsize) ||
494 VFSATTR_IS_ACTIVE(fsap, f_iosize) ||
495 VFSATTR_IS_ACTIVE(fsap, f_blocks) ||
496 VFSATTR_IS_ACTIVE(fsap, f_bfree) ||
497 VFSATTR_IS_ACTIVE(fsap, f_bavail) ||
498 VFSATTR_IS_ACTIVE(fsap, f_bused) ||
499 VFSATTR_IS_ACTIVE(fsap, f_files) ||
500 VFSATTR_IS_ACTIVE(fsap, f_ffree)) {
2d21ac55
A
501 int statfsrate = nfs_statfs_rate_limit;
502 int refresh = 1;
91447636 503
2d21ac55
A
504 /*
505 * Are we rate-limiting statfs RPCs?
506 * (Treat values less than 1 or greater than 1,000,000 as no limit.)
507 */
508 if ((statfsrate > 0) && (statfsrate < 1000000)) {
509 struct timeval now;
510 uint32_t stamp;
511
512 microuptime(&now);
513 lck_mtx_lock(&nmp->nm_lock);
514 stamp = (now.tv_sec * statfsrate) + (now.tv_usec / (1000000/statfsrate));
515 if (stamp != nmp->nm_fsattrstamp) {
516 refresh = 1;
517 nmp->nm_fsattrstamp = stamp;
518 } else {
519 refresh = 0;
520 }
521 lck_mtx_unlock(&nmp->nm_lock);
91447636 522 }
2d21ac55
A
523
524 if (refresh)
525 error = nmp->nm_funcs->nf_update_statfs(nmp, ctx);
526 if ((error == ESTALE) || (error == ETIMEDOUT))
527 error = 0;
528 if (error)
529 return (error);
530
531 lck_mtx_lock(&nmp->nm_lock);
532 VFSATTR_RETURN(fsap, f_iosize, nfs_iosize);
533 VFSATTR_RETURN(fsap, f_bsize, nmp->nm_fsattr.nfsa_bsize);
534 bsize = nmp->nm_fsattr.nfsa_bsize;
535 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL))
536 VFSATTR_RETURN(fsap, f_blocks, nmp->nm_fsattr.nfsa_space_total / bsize);
537 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE))
538 VFSATTR_RETURN(fsap, f_bfree, nmp->nm_fsattr.nfsa_space_free / bsize);
539 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_AVAIL))
540 VFSATTR_RETURN(fsap, f_bavail, nmp->nm_fsattr.nfsa_space_avail / bsize);
541 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_TOTAL) &&
542 NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SPACE_FREE))
543 VFSATTR_RETURN(fsap, f_bused,
544 (nmp->nm_fsattr.nfsa_space_total / bsize) -
545 (nmp->nm_fsattr.nfsa_space_free / bsize));
546 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_TOTAL))
547 VFSATTR_RETURN(fsap, f_files, nmp->nm_fsattr.nfsa_files_total);
548 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_FILES_FREE))
549 VFSATTR_RETURN(fsap, f_ffree, nmp->nm_fsattr.nfsa_files_free);
550 lck_mtx_unlock(&nmp->nm_lock);
91447636
A
551 }
552
553 if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
91447636 554 u_int32_t caps, valid;
b0d623f7 555 nfsnode_t np = nmp->nm_dnp;
91447636 556
b0d623f7 557 nfsm_assert(error, VFSTONFS(mp) && np, ENXIO);
2d21ac55
A
558 if (error)
559 return (error);
2d21ac55 560 lck_mtx_lock(&nmp->nm_lock);
91447636
A
561
562 /*
563 * The capabilities[] array defines what this volume supports.
564 *
565 * The valid[] array defines which bits this code understands
566 * the meaning of (whether the volume has that capability or not).
567 * Any zero bits here means "I don't know what you're asking about"
568 * and the caller cannot tell whether that capability is
569 * present or not.
570 */
571 caps = valid = 0;
2d21ac55
A
572 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SYMLINK_SUPPORT)) {
573 valid |= VOL_CAP_FMT_SYMBOLICLINKS;
574 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_SYMLINK)
575 caps |= VOL_CAP_FMT_SYMBOLICLINKS;
576 }
577 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_LINK_SUPPORT)) {
578 valid |= VOL_CAP_FMT_HARDLINKS;
579 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_LINK)
580 caps |= VOL_CAP_FMT_HARDLINKS;
581 }
582 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE)) {
583 valid |= VOL_CAP_FMT_CASE_SENSITIVE;
584 if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE))
585 caps |= VOL_CAP_FMT_CASE_SENSITIVE;
586 }
587 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING)) {
588 valid |= VOL_CAP_FMT_CASE_PRESERVING;
589 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_PRESERVING)
590 caps |= VOL_CAP_FMT_CASE_PRESERVING;
591 }
593a1d5f 592 /* Note: VOL_CAP_FMT_2TB_FILESIZE is actually used to test for "large file support" */
2d21ac55 593 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
593a1d5f
A
594 /* Is server's max file size at least 4GB? */
595 if (nmp->nm_fsattr.nfsa_maxfilesize >= 0x100000000ULL)
91447636 596 caps |= VOL_CAP_FMT_2TB_FILESIZE;
2d21ac55
A
597 } else if (nfsvers >= NFS_VER3) {
598 /*
599 * NFSv3 and up supports 64 bits of file size.
593a1d5f 600 * So, we'll just assume maxfilesize >= 4GB
2d21ac55
A
601 */
602 caps |= VOL_CAP_FMT_2TB_FILESIZE;
603 }
604 if (nfsvers >= NFS_VER4) {
605 caps |= VOL_CAP_FMT_HIDDEN_FILES;
606 valid |= VOL_CAP_FMT_HIDDEN_FILES;
607 // VOL_CAP_FMT_OPENDENYMODES
91447636
A
608 }
609 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
610 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
611 // VOL_CAP_FMT_SYMBOLICLINKS |
612 // VOL_CAP_FMT_HARDLINKS |
613 // VOL_CAP_FMT_JOURNAL |
614 // VOL_CAP_FMT_JOURNAL_ACTIVE |
615 // VOL_CAP_FMT_NO_ROOT_TIMES |
616 // VOL_CAP_FMT_SPARSE_FILES |
617 // VOL_CAP_FMT_ZERO_RUNS |
618 // VOL_CAP_FMT_CASE_SENSITIVE |
619 // VOL_CAP_FMT_CASE_PRESERVING |
620 // VOL_CAP_FMT_FAST_STATFS |
621 // VOL_CAP_FMT_2TB_FILESIZE |
2d21ac55
A
622 // VOL_CAP_FMT_OPENDENYMODES |
623 // VOL_CAP_FMT_HIDDEN_FILES |
91447636
A
624 caps;
625 fsap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
626 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
627 // VOL_CAP_FMT_SYMBOLICLINKS |
628 // VOL_CAP_FMT_HARDLINKS |
629 // VOL_CAP_FMT_JOURNAL |
630 // VOL_CAP_FMT_JOURNAL_ACTIVE |
631 // VOL_CAP_FMT_NO_ROOT_TIMES |
632 // VOL_CAP_FMT_SPARSE_FILES |
633 // VOL_CAP_FMT_ZERO_RUNS |
634 // VOL_CAP_FMT_CASE_SENSITIVE |
635 // VOL_CAP_FMT_CASE_PRESERVING |
636 VOL_CAP_FMT_FAST_STATFS |
637 VOL_CAP_FMT_2TB_FILESIZE |
2d21ac55
A
638 // VOL_CAP_FMT_OPENDENYMODES |
639 // VOL_CAP_FMT_HIDDEN_FILES |
91447636
A
640 valid;
641
642 /*
643 * We don't support most of the interfaces.
644 *
645 * We MAY support locking, but we don't have any easy way of probing.
646 * We can tell if there's no lockd running or if locks have been
647 * disabled for a mount, so we can definitely answer NO in that case.
648 * Any attempt to send a request to lockd to test for locking support
649 * may cause the lazily-launched locking daemons to be started
650 * unnecessarily. So we avoid that. However, we do record if we ever
651 * successfully perform a lock operation on a mount point, so if it
652 * looks like lock ops have worked, we do report that we support them.
653 */
654 caps = valid = 0;
2d21ac55
A
655 if (nfsvers >= NFS_VER4) {
656 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
657 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
658 // VOL_CAP_INT_EXTENDED_SECURITY
659 // VOL_CAP_INT_NAMEDSTREAMS
660 // VOL_CAP_INT_EXTENDED_ATTR
661 } else if ((nmp->nm_flag & NFSMNT_NOLOCKS)) {
91447636
A
662 /* locks disabled on this mount, so they definitely won't work */
663 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
664 } else if (nmp->nm_state & NFSSTA_LOCKSWORK) {
665 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
666 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
667 }
668 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
669 // VOL_CAP_INT_SEARCHFS |
670 // VOL_CAP_INT_ATTRLIST |
671 // VOL_CAP_INT_NFSEXPORT |
672 // VOL_CAP_INT_READDIRATTR |
673 // VOL_CAP_INT_EXCHANGEDATA |
674 // VOL_CAP_INT_COPYFILE |
675 // VOL_CAP_INT_ALLOCATE |
676 // VOL_CAP_INT_VOL_RENAME |
677 // VOL_CAP_INT_ADVLOCK |
678 // VOL_CAP_INT_FLOCK |
679 // VOL_CAP_INT_EXTENDED_SECURITY |
680 // VOL_CAP_INT_USERACCESS |
2d21ac55
A
681 // VOL_CAP_INT_MANLOCK |
682 // VOL_CAP_INT_NAMEDSTREAMS |
683 // VOL_CAP_INT_EXTENDED_ATTR |
91447636
A
684 caps;
685 fsap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
686 VOL_CAP_INT_SEARCHFS |
687 VOL_CAP_INT_ATTRLIST |
688 VOL_CAP_INT_NFSEXPORT |
689 VOL_CAP_INT_READDIRATTR |
690 VOL_CAP_INT_EXCHANGEDATA |
691 VOL_CAP_INT_COPYFILE |
692 VOL_CAP_INT_ALLOCATE |
693 VOL_CAP_INT_VOL_RENAME |
694 // VOL_CAP_INT_ADVLOCK |
695 // VOL_CAP_INT_FLOCK |
696 // VOL_CAP_INT_EXTENDED_SECURITY |
697 // VOL_CAP_INT_USERACCESS |
2d21ac55
A
698 // VOL_CAP_INT_MANLOCK |
699 // VOL_CAP_INT_NAMEDSTREAMS |
700 // VOL_CAP_INT_EXTENDED_ATTR |
91447636
A
701 valid;
702
703 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
704 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
705
706 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
707 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
708
709 VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
2d21ac55 710 lck_mtx_unlock(&nmp->nm_lock);
91447636
A
711 }
712
713 if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
714 fsap->f_attributes.validattr.commonattr = 0;
715 fsap->f_attributes.validattr.volattr =
716 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
717 fsap->f_attributes.validattr.dirattr = 0;
718 fsap->f_attributes.validattr.fileattr = 0;
719 fsap->f_attributes.validattr.forkattr = 0;
720
721 fsap->f_attributes.nativeattr.commonattr = 0;
722 fsap->f_attributes.nativeattr.volattr =
723 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
724 fsap->f_attributes.nativeattr.dirattr = 0;
725 fsap->f_attributes.nativeattr.fileattr = 0;
726 fsap->f_attributes.nativeattr.forkattr = 0;
727
728 VFSATTR_SET_SUPPORTED(fsap, f_attributes);
729 }
730
1c79356b
A
731 return (error);
732}
733
734/*
735 * nfs version 3 fsinfo rpc call
736 */
b0d623f7 737int
2d21ac55 738nfs3_fsinfo(struct nfsmount *nmp, nfsnode_t np, vfs_context_t ctx)
1c79356b 739{
b0d623f7 740 int error = 0, lockerror, status, nmlocked = 0;
fa4905b1 741 u_int64_t xid;
b0d623f7 742 uint32_t val, prefsize, maxsize;
2d21ac55
A
743 struct nfsm_chain nmreq, nmrep;
744
745 nfsm_chain_null(&nmreq);
746 nfsm_chain_null(&nmrep);
747
748 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nmp->nm_vers));
749 nfsm_chain_add_fh(error, &nmreq, nmp->nm_vers, np->n_fhp, np->n_fhsize);
750 nfsm_chain_build_done(error, &nmreq);
751 nfsmout_if(error);
752 error = nfs_request(np, NULL, &nmreq, NFSPROC_FSINFO, ctx,
753 &nmrep, &xid, &status);
b0d623f7 754 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
755 error = lockerror;
756 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
757 if (!lockerror)
b0d623f7 758 nfs_node_unlock(np);
2d21ac55
A
759 if (!error)
760 error = status;
761 nfsmout_if(error);
762
763 lck_mtx_lock(&nmp->nm_lock);
764 nmlocked = 1;
765
766 nfsm_chain_get_32(error, &nmrep, maxsize);
767 nfsm_chain_get_32(error, &nmrep, prefsize);
768 nfsmout_if(error);
769 nmp->nm_fsattr.nfsa_maxread = maxsize;
770 if (prefsize < nmp->nm_rsize)
771 nmp->nm_rsize = (prefsize + NFS_FABLKSIZE - 1) &
772 ~(NFS_FABLKSIZE - 1);
773 if (maxsize < nmp->nm_rsize) {
774 nmp->nm_rsize = maxsize & ~(NFS_FABLKSIZE - 1);
775 if (nmp->nm_rsize == 0)
776 nmp->nm_rsize = maxsize;
777 }
778 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip rtmult
779
780 nfsm_chain_get_32(error, &nmrep, maxsize);
781 nfsm_chain_get_32(error, &nmrep, prefsize);
782 nfsmout_if(error);
783 nmp->nm_fsattr.nfsa_maxwrite = maxsize;
784 if (prefsize < nmp->nm_wsize)
785 nmp->nm_wsize = (prefsize + NFS_FABLKSIZE - 1) &
786 ~(NFS_FABLKSIZE - 1);
787 if (maxsize < nmp->nm_wsize) {
788 nmp->nm_wsize = maxsize & ~(NFS_FABLKSIZE - 1);
789 if (nmp->nm_wsize == 0)
790 nmp->nm_wsize = maxsize;
1c79356b 791 }
2d21ac55
A
792 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); // skip wtmult
793
794 nfsm_chain_get_32(error, &nmrep, prefsize);
795 nfsmout_if(error);
796 if (prefsize < nmp->nm_readdirsize)
797 nmp->nm_readdirsize = prefsize;
798 if (maxsize < nmp->nm_readdirsize)
799 nmp->nm_readdirsize = maxsize;
800
593a1d5f 801 nfsm_chain_get_64(error, &nmrep, nmp->nm_fsattr.nfsa_maxfilesize);
2d21ac55
A
802
803 nfsm_chain_adv(error, &nmrep, 2 * NFSX_UNSIGNED); // skip time_delta
804
805 /* convert FS properties to our own flags */
806 nfsm_chain_get_32(error, &nmrep, val);
807 nfsmout_if(error);
808 if (val & NFSV3FSINFO_LINK)
809 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_LINK;
810 if (val & NFSV3FSINFO_SYMLINK)
811 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_SYMLINK;
812 if (val & NFSV3FSINFO_HOMOGENEOUS)
813 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_HOMOGENEOUS;
814 if (val & NFSV3FSINFO_CANSETTIME)
815 nmp->nm_fsattr.nfsa_flags |= NFS_FSFLAG_SET_TIME;
816 nmp->nm_state |= NFSSTA_GOTFSINFO;
817 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXREAD);
818 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXWRITE);
819 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXFILESIZE);
820 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_LINK_SUPPORT);
821 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_SYMLINK_SUPPORT);
822 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_HOMOGENEOUS);
823 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CANSETTIME);
824nfsmout:
825 if (nmlocked)
826 lck_mtx_unlock(&nmp->nm_lock);
827 nfsm_chain_cleanup(&nmreq);
828 nfsm_chain_cleanup(&nmrep);
1c79356b
A
829 return (error);
830}
831
832/*
833 * Mount a remote root fs via. nfs. This depends on the info in the
834 * nfs_diskless structure that has been filled in properly by some primary
835 * bootstrap.
836 * It goes something like this:
837 * - do enough of "ifconfig" by calling ifioctl() so that the system
838 * can talk to the server
839 * - If nfs_diskless.mygateway is filled in, use that address as
840 * a default gateway.
841 * - hand craft the swap nfs vnode hanging off a fake mount point
842 * if swdevt[0].sw_dev == NODEV
843 * - build the rootfs mount point and call mountnfs() to do the rest.
844 */
845int
2d21ac55 846nfs_mountroot(void)
1c79356b
A
847{
848 struct nfs_diskless nd;
91447636 849 struct nfs_vattr nvattr;
2d21ac55
A
850 mount_t mp = NULL;
851 vnode_t vp = NULL;
852 vfs_context_t ctx;
1c79356b
A
853 int error;
854#if !defined(NO_MOUNT_PRIVATE)
2d21ac55
A
855 mount_t mppriv = NULL;
856 vnode_t vppriv = NULL;
1c79356b 857#endif /* NO_MOUNT_PRIVATE */
91447636 858 int v3, sotype;
1c79356b 859
1c79356b
A
860 /*
861 * Call nfs_boot_init() to fill in the nfs_diskless struct.
9bccf70c
A
862 * Note: networking must already have been configured before
863 * we're called.
1c79356b
A
864 */
865 bzero((caddr_t) &nd, sizeof(nd));
2d21ac55 866 error = nfs_boot_init(&nd);
9bccf70c
A
867 if (error) {
868 panic("nfs_boot_init failed with %d\n", error);
869 }
1c79356b 870
91447636
A
871 /*
872 * Try NFSv3 first, then fallback to NFSv2.
873 * Likewise, try TCP first, then fall back to UDP.
874 */
90556fb8 875 v3 = 1;
91447636 876 sotype = SOCK_STREAM;
90556fb8
A
877
878tryagain:
2d21ac55 879 error = nfs_boot_getfh(&nd, v3, sotype);
90556fb8 880 if (error) {
4a249263 881 if (error == EHOSTDOWN || error == EHOSTUNREACH) {
b0d623f7
A
882 if (nd.nd_root.ndm_mntfrom)
883 FREE_ZONE(nd.nd_root.ndm_mntfrom,
884 MAXPATHLEN, M_NAMEI);
4a249263 885 if (nd.nd_root.ndm_path)
2d21ac55 886 FREE_ZONE(nd.nd_root.ndm_path,
4a249263 887 MAXPATHLEN, M_NAMEI);
b0d623f7
A
888 if (nd.nd_private.ndm_mntfrom)
889 FREE_ZONE(nd.nd_private.ndm_mntfrom,
890 MAXPATHLEN, M_NAMEI);
4a249263 891 if (nd.nd_private.ndm_path)
2d21ac55 892 FREE_ZONE(nd.nd_private.ndm_path,
4a249263
A
893 MAXPATHLEN, M_NAMEI);
894 return (error);
895 }
90556fb8 896 if (v3) {
91447636
A
897 if (sotype == SOCK_STREAM) {
898 printf("nfs_boot_getfh(v3,TCP) failed with %d, trying UDP...\n", error);
899 sotype = SOCK_DGRAM;
900 goto tryagain;
901 }
902 printf("nfs_boot_getfh(v3,UDP) failed with %d, trying v2...\n", error);
90556fb8 903 v3 = 0;
91447636
A
904 sotype = SOCK_STREAM;
905 goto tryagain;
906 } else if (sotype == SOCK_STREAM) {
907 printf("nfs_boot_getfh(v2,TCP) failed with %d, trying UDP...\n", error);
908 sotype = SOCK_DGRAM;
90556fb8
A
909 goto tryagain;
910 }
2d21ac55
A
911 switch(error) {
912 case EPROGUNAVAIL:
913 panic("nfs_boot_getfh(v2,UDP) failed: NFS server mountd not responding - check server configuration: %s", PE_boot_args());
914 case EACCES:
915 case EPERM:
916 panic("nfs_boot_getfh(v2,UDP) failed: NFS server refused mount - check server configuration: %s", PE_boot_args());
917 default:
918 panic("nfs_boot_getfh(v2,UDP) failed with %d: %s", error, PE_boot_args());
919 }
90556fb8
A
920 }
921
2d21ac55
A
922 ctx = vfs_context_kernel();
923
1c79356b
A
924 /*
925 * Create the root mount point.
926 */
927#if !defined(NO_MOUNT_PRIVATE)
2d21ac55
A
928 {
929 //PWC hack until we have a real "mount" tool to remount root rw
930 int rw_root=0;
931 int flags = MNT_ROOTFS|MNT_RDONLY;
593a1d5f 932 PE_parse_boot_argn("-rwroot_hack", &rw_root, sizeof (rw_root));
2d21ac55
A
933 if(rw_root)
934 {
935 flags = MNT_ROOTFS;
936 kprintf("-rwroot_hack in effect: mounting root fs read/write\n");
937 }
938
939 if ((error = nfs_mount_diskless(&nd.nd_root, "/", flags, &vp, &mp, ctx)))
1c79356b 940#else
2d21ac55 941 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_ROOTFS, &vp, &mp, ctx)))
1c79356b 942#endif /* NO_MOUNT_PRIVATE */
91447636 943 {
90556fb8 944 if (v3) {
91447636
A
945 if (sotype == SOCK_STREAM) {
946 printf("nfs_mount_diskless(v3,TCP) failed with %d, trying UDP...\n", error);
947 sotype = SOCK_DGRAM;
948 goto tryagain;
949 }
950 printf("nfs_mount_diskless(v3,UDP) failed with %d, trying v2...\n", error);
90556fb8 951 v3 = 0;
91447636
A
952 sotype = SOCK_STREAM;
953 goto tryagain;
954 } else if (sotype == SOCK_STREAM) {
955 printf("nfs_mount_diskless(v2,TCP) failed with %d, trying UDP...\n", error);
956 sotype = SOCK_DGRAM;
90556fb8
A
957 goto tryagain;
958 }
2d21ac55
A
959 panic("nfs_mount_diskless(v2,UDP) root failed with %d: %s\n", error, PE_boot_args());
960 }
1c79356b 961 }
b0d623f7 962 printf("root on %s\n", nd.nd_root.ndm_mntfrom);
1c79356b 963
91447636
A
964 vfs_unbusy(mp);
965 mount_list_add(mp);
1c79356b
A
966 rootvp = vp;
967
968#if !defined(NO_MOUNT_PRIVATE)
969 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
970 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
2d21ac55 971 0, &vppriv, &mppriv, ctx);
9bccf70c 972 if (error) {
90556fb8 973 panic("nfs_mount_diskless private failed with %d\n", error);
9bccf70c 974 }
b0d623f7 975 printf("private on %s\n", nd.nd_private.ndm_mntfrom);
2d21ac55 976
91447636
A
977 vfs_unbusy(mppriv);
978 mount_list_add(mppriv);
1c79356b
A
979 }
980
981#endif /* NO_MOUNT_PRIVATE */
982
b0d623f7
A
983 if (nd.nd_root.ndm_mntfrom)
984 FREE_ZONE(nd.nd_root.ndm_mntfrom, MAXPATHLEN, M_NAMEI);
90556fb8
A
985 if (nd.nd_root.ndm_path)
986 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
b0d623f7
A
987 if (nd.nd_private.ndm_mntfrom)
988 FREE_ZONE(nd.nd_private.ndm_mntfrom, MAXPATHLEN, M_NAMEI);
90556fb8
A
989 if (nd.nd_private.ndm_path)
990 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
991
1c79356b 992 /* Get root attributes (for the time). */
b0d623f7 993 error = nfs_getattr(VTONFS(vp), &nvattr, ctx, NGA_UNCACHED);
1c79356b 994 if (error) panic("nfs_mountroot: getattr for root");
1c79356b
A
995 return (0);
996}
997
998/*
999 * Internal version of mount system call for diskless setup.
1000 */
1001static int
91447636
A
1002nfs_mount_diskless(
1003 struct nfs_dlmount *ndmntp,
1004 const char *mntname,
1005 int mntflag,
1006 vnode_t *vpp,
2d21ac55
A
1007 mount_t *mpp,
1008 vfs_context_t ctx)
1c79356b 1009{
91447636
A
1010 struct user_nfs_args args;
1011 mount_t mp;
1012 mbuf_t m;
1c79356b 1013 int error;
1c79356b 1014
b0d623f7 1015 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_mntfrom, &mp))) {
91447636 1016 printf("nfs_mount_diskless: NFS not configured");
1c79356b
A
1017 return (error);
1018 }
91447636
A
1019
1020 mp->mnt_flag |= mntflag;
1021 if (!(mntflag & MNT_RDONLY))
1022 mp->mnt_flag &= ~MNT_RDONLY;
1c79356b
A
1023
1024 /* Initialize mount args. */
1025 bzero((caddr_t) &args, sizeof(args));
91447636
A
1026 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
1027 args.addrlen = ndmntp->ndm_saddr.sin_len;
1028 args.sotype = ndmntp->ndm_sotype;
1029 args.fh = CAST_USER_ADDR_T(&ndmntp->ndm_fh[0]);
90556fb8 1030 args.fhsize = ndmntp->ndm_fhlen;
b0d623f7 1031 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_mntfrom);
1c79356b 1032 args.flags = NFSMNT_RESVPORT;
90556fb8
A
1033 if (ndmntp->ndm_nfsv3)
1034 args.flags |= NFSMNT_NFSV3;
1c79356b 1035
91447636
A
1036 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
1037 if (error) {
1038 printf("nfs_mount_diskless: mbuf_get(soname) failed");
1039 return (error);
1040 }
1041 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
2d21ac55
A
1042 bcopy(&ndmntp->ndm_saddr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
1043 if ((error = mountnfs(&args, mp, m, ctx, vpp))) {
91447636
A
1044 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
1045 // XXX vfs_rootmountfailed(mp);
1046 mount_list_lock();
1047 mp->mnt_vtable->vfc_refcount--;
1048 mount_list_unlock();
1049 vfs_unbusy(mp);
1050 mount_lock_destroy(mp);
2d21ac55
A
1051#if CONFIG_MACF
1052 mac_mount_label_destroy(mp);
1053#endif
91447636 1054 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
1c79356b
A
1055 return (error);
1056 }
1c79356b
A
1057 *mpp = mp;
1058 return (0);
1059}
1060
1061#if !defined(NO_MOUNT_PRIVATE)
1062/*
1063 * Internal version of mount system call to mount "/private"
1064 * separately in diskless setup
1065 */
1066static int
91447636
A
1067nfs_mount_diskless_private(
1068 struct nfs_dlmount *ndmntp,
1069 const char *mntname,
1070 int mntflag,
1071 vnode_t *vpp,
2d21ac55
A
1072 mount_t *mpp,
1073 vfs_context_t ctx)
1c79356b 1074{
91447636
A
1075 struct user_nfs_args args;
1076 mount_t mp;
1077 mbuf_t m;
1c79356b 1078 int error;
91447636
A
1079 proc_t procp;
1080 struct vfstable *vfsp;
1c79356b 1081 struct nameidata nd;
91447636 1082 vnode_t vp;
1c79356b
A
1083
1084 procp = current_proc(); /* XXX */
1085
1086 {
1087 /*
1088 * mimic main()!. Temporarily set up rootvnode and other stuff so
1089 * that namei works. Need to undo this because main() does it, too
1090 */
1091 struct filedesc *fdp; /* pointer to file descriptor state */
1092 fdp = procp->p_fd;
91447636 1093 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
1c79356b
A
1094
1095 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
91447636 1096 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, NULL))
1c79356b 1097 panic("cannot find root vnode");
91447636
A
1098 error = vnode_ref(rootvnode);
1099 if (error) {
1100 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
2d21ac55 1101 goto out;
91447636 1102 }
1c79356b 1103 fdp->fd_cdir = rootvnode;
1c79356b
A
1104 fdp->fd_rdir = NULL;
1105 }
1106
1107 /*
1108 * Get vnode to be covered
1109 */
b0d623f7 1110 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
2d21ac55 1111 CAST_USER_ADDR_T(mntname), ctx);
1c79356b 1112 if ((error = namei(&nd))) {
91447636 1113 printf("nfs_mountroot: private namei failed!\n");
2d21ac55 1114 goto out;
1c79356b
A
1115 }
1116 {
91447636
A
1117 /* undo vnode_ref() in mimic main()! */
1118 vnode_rele(rootvnode);
1c79356b 1119 }
91447636 1120 nameidone(&nd);
1c79356b 1121 vp = nd.ni_vp;
91447636 1122
2d21ac55 1123 if ((error = VNOP_FSYNC(vp, MNT_WAIT, ctx)) ||
91447636
A
1124 (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
1125 vnode_put(vp);
2d21ac55 1126 goto out;
1c79356b 1127 }
91447636
A
1128 if (vnode_vtype(vp) != VDIR) {
1129 vnode_put(vp);
2d21ac55
A
1130 error = ENOTDIR;
1131 goto out;
1c79356b
A
1132 }
1133 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2d21ac55 1134 if (!strncmp(vfsp->vfc_name, "nfs", sizeof(vfsp->vfc_name)))
1c79356b
A
1135 break;
1136 if (vfsp == NULL) {
91447636
A
1137 printf("nfs_mountroot: private NFS not configured\n");
1138 vnode_put(vp);
2d21ac55
A
1139 error = ENODEV;
1140 goto out;
1c79356b 1141 }
91447636
A
1142 if (vnode_mountedhere(vp) != NULL) {
1143 vnode_put(vp);
2d21ac55
A
1144 error = EBUSY;
1145 goto out;
1c79356b
A
1146 }
1147
1148 /*
1149 * Allocate and initialize the filesystem.
1150 */
b0d623f7 1151 mp = _MALLOC_ZONE((u_int32_t)sizeof(struct mount), M_MOUNT, M_WAITOK);
91447636
A
1152 if (!mp) {
1153 printf("nfs_mountroot: unable to allocate mount structure\n");
1154 vnode_put(vp);
2d21ac55
A
1155 error = ENOMEM;
1156 goto out;
91447636 1157 }
b0d623f7 1158 bzero((char *)mp, sizeof(struct mount));
0b4e3aa0 1159
55e303ae
A
1160 /* Initialize the default IO constraints */
1161 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
1162 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
2d21ac55
A
1163 mp->mnt_ioflags = 0;
1164 mp->mnt_realrootvp = NULLVP;
1165 mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
0b4e3aa0 1166
91447636
A
1167 mount_lock_init(mp);
1168 TAILQ_INIT(&mp->mnt_vnodelist);
1169 TAILQ_INIT(&mp->mnt_workerqueue);
1170 TAILQ_INIT(&mp->mnt_newvnodes);
1171 (void)vfs_busy(mp, LK_NOWAIT);
1172 TAILQ_INIT(&mp->mnt_vnodelist);
1173 mount_list_lock();
1c79356b 1174 vfsp->vfc_refcount++;
91447636
A
1175 mount_list_unlock();
1176 mp->mnt_vtable = vfsp;
1177 mp->mnt_op = vfsp->vfc_vfsops;
1178 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
1c79356b
A
1179 mp->mnt_flag = mntflag;
1180 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
91447636 1181 strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSNAMELEN-1);
1c79356b
A
1182 vp->v_mountedhere = mp;
1183 mp->mnt_vnodecovered = vp;
91447636 1184 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
b0d623f7
A
1185 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
1186 (void) copystr(ndmntp->ndm_mntfrom, mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN - 1, 0);
2d21ac55
A
1187#if CONFIG_MACF
1188 mac_mount_label_init(mp);
1189 mac_mount_label_associate(ctx, mp);
1190#endif
1c79356b
A
1191
1192 /* Initialize mount args. */
1193 bzero((caddr_t) &args, sizeof(args));
91447636
A
1194 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
1195 args.addrlen = ndmntp->ndm_saddr.sin_len;
1196 args.sotype = ndmntp->ndm_sotype;
1197 args.fh = CAST_USER_ADDR_T(ndmntp->ndm_fh);
90556fb8 1198 args.fhsize = ndmntp->ndm_fhlen;
b0d623f7 1199 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_mntfrom);
1c79356b 1200 args.flags = NFSMNT_RESVPORT;
90556fb8
A
1201 if (ndmntp->ndm_nfsv3)
1202 args.flags |= NFSMNT_NFSV3;
1c79356b 1203
91447636
A
1204 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
1205 if (error) {
1206 printf("nfs_mount_diskless_private: mbuf_get(soname) failed");
2d21ac55 1207 goto out;
91447636
A
1208 }
1209 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
2d21ac55
A
1210 bcopy(&ndmntp->ndm_saddr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
1211 if ((error = mountnfs(&args, mp, m, ctx, &vp))) {
91447636
A
1212 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
1213 mount_list_lock();
1214 vfsp->vfc_refcount--;
1215 mount_list_unlock();
1216 vfs_unbusy(mp);
1217 mount_lock_destroy(mp);
2d21ac55
A
1218#if CONFIG_MACF
1219 mac_mount_label_destroy(mp);
1220#endif
55e303ae 1221 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
2d21ac55 1222 goto out;
1c79356b
A
1223 }
1224
1225 *mpp = mp;
1226 *vpp = vp;
2d21ac55
A
1227out:
1228 return (error);
1c79356b
A
1229}
1230#endif /* NO_MOUNT_PRIVATE */
1231
1232/*
1233 * VFS Operations.
1234 *
1235 * mount system call
1c79356b 1236 */
b0d623f7 1237int
2d21ac55 1238nfs_vfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t ctx)
1c79356b 1239{
91447636
A
1240 int error, argsvers;
1241 struct user_nfs_args args;
1242 struct nfs_args tempargs;
1243 mbuf_t nam;
55e303ae 1244 size_t len;
1c79356b 1245 u_char nfh[NFSX_V3FHMAX];
91447636 1246 char *mntfrom;
1c79356b 1247
91447636 1248 error = copyin(data, (caddr_t)&argsvers, sizeof (argsvers));
1c79356b
A
1249 if (error)
1250 return (error);
91447636
A
1251
1252 switch (argsvers) {
1253 case 3:
2d21ac55 1254 if (vfs_context_is64bit(ctx))
91447636
A
1255 error = copyin(data, (caddr_t)&args, sizeof (struct user_nfs_args3));
1256 else
1257 error = copyin(data, (caddr_t)&tempargs, sizeof (struct nfs_args3));
1258 break;
1259 case 4:
2d21ac55
A
1260 if (vfs_context_is64bit(ctx))
1261 error = copyin(data, (caddr_t)&args, sizeof (struct user_nfs_args4));
1262 else
1263 error = copyin(data, (caddr_t)&tempargs, sizeof (struct nfs_args4));
1264 break;
1265 case 5:
b0d623f7
A
1266 if (vfs_context_is64bit(ctx))
1267 error = copyin(data, (caddr_t)&args, sizeof (struct user_nfs_args5));
1268 else
1269 error = copyin(data, (caddr_t)&tempargs, sizeof (struct nfs_args5));
1270 break;
1271 case 6:
2d21ac55 1272 if (vfs_context_is64bit(ctx))
91447636
A
1273 error = copyin(data, (caddr_t)&args, sizeof (args));
1274 else
1275 error = copyin(data, (caddr_t)&tempargs, sizeof (tempargs));
1276 break;
1277 default:
1c79356b 1278 return (EPROGMISMATCH);
1c79356b 1279 }
1c79356b
A
1280 if (error)
1281 return (error);
91447636 1282
2d21ac55 1283 if (!vfs_context_is64bit(ctx)) {
91447636
A
1284 args.version = tempargs.version;
1285 args.addrlen = tempargs.addrlen;
1286 args.sotype = tempargs.sotype;
1287 args.proto = tempargs.proto;
1288 args.fhsize = tempargs.fhsize;
1289 args.flags = tempargs.flags;
1290 args.wsize = tempargs.wsize;
1291 args.rsize = tempargs.rsize;
1292 args.readdirsize = tempargs.readdirsize;
1293 args.timeo = tempargs.timeo;
1294 args.retrans = tempargs.retrans;
1295 args.maxgrouplist = tempargs.maxgrouplist;
1296 args.readahead = tempargs.readahead;
1297 args.leaseterm = tempargs.leaseterm;
1298 args.deadthresh = tempargs.deadthresh;
1299 args.addr = CAST_USER_ADDR_T(tempargs.addr);
1300 args.fh = CAST_USER_ADDR_T(tempargs.fh);
1301 args.hostname = CAST_USER_ADDR_T(tempargs.hostname);
1302 if (argsvers >= 4) {
1303 args.acregmin = tempargs.acregmin;
1304 args.acregmax = tempargs.acregmax;
1305 args.acdirmin = tempargs.acdirmin;
1306 args.acdirmax = tempargs.acdirmax;
1307 }
2d21ac55
A
1308 if (argsvers >= 5)
1309 args.auth = tempargs.auth;
b0d623f7
A
1310 if (argsvers >= 6)
1311 args.deadtimeout = tempargs.deadtimeout;
91447636
A
1312 }
1313
b36670ce 1314 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
91447636 1315 return (EINVAL);
2d21ac55
A
1316 if (args.fhsize > 0) {
1317 error = copyin(args.fh, (caddr_t)nfh, args.fhsize);
1318 if (error)
1319 return (error);
1320 }
91447636
A
1321
1322 mntfrom = &vfs_statfs(mp)->f_mntfromname[0];
1323 error = copyinstr(args.hostname, mntfrom, MAXPATHLEN-1, &len);
1c79356b
A
1324 if (error)
1325 return (error);
91447636
A
1326 bzero(&mntfrom[len], MAXPATHLEN - len);
1327
1c79356b 1328 /* sockargs() call must be after above copyin() calls */
91447636 1329 error = sockargs(&nam, args.addr, args.addrlen, MBUF_TYPE_SONAME);
1c79356b
A
1330 if (error)
1331 return (error);
91447636
A
1332
1333 args.fh = CAST_USER_ADDR_T(&nfh[0]);
2d21ac55 1334 error = mountnfs(&args, mp, nam, ctx, &vp);
1c79356b
A
1335 return (error);
1336}
1337
1338/*
1339 * Common code for mount and mountroot
1340 */
2d21ac55 1341
b0d623f7 1342int
2d21ac55
A
1343nfs3_mount(
1344 struct nfsmount *nmp,
1345 vfs_context_t ctx,
1346 struct user_nfs_args *argp,
1347 nfsnode_t *npp)
1348{
1349 int error = 0;
1350 struct nfs_vattr nvattr;
1351 u_int64_t xid;
1352 u_char *fhp;
1353
1354 *npp = NULL;
1355
1356 /*
1357 * Get file attributes for the mountpoint. These are needed
1358 * in order to properly create the root vnode.
1359 */
2d21ac55
A
1360 fhp = CAST_DOWN(u_char *, argp->fh);
1361 error = nfs3_getattr_rpc(NULL, nmp->nm_mountp, fhp, argp->fhsize,
1362 ctx, &nvattr, &xid);
1363 if (error)
1364 goto out;
1365
1366 error = nfs_nget(nmp->nm_mountp, NULL, NULL, fhp, argp->fhsize,
1367 &nvattr, &xid, NG_MARKROOT, npp);
1368 if (*npp)
b0d623f7 1369 nfs_node_unlock(*npp);
2d21ac55
A
1370 if (error)
1371 goto out;
1372
1373 /*
1374 * Try to make sure we have all the general info from the server.
1375 */
1376 if (nmp->nm_vers == NFS_VER2) {
1377 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME);
1378 nmp->nm_fsattr.nfsa_maxname = NFS_MAXNAMLEN;
1379 } else if (nmp->nm_vers == NFS_VER3) {
1380 /* get the NFSv3 FSINFO */
1381 error = nfs3_fsinfo(nmp, *npp, ctx);
1382 if (error)
1383 goto out;
1384 /* If the server indicates all pathconf info is */
1385 /* the same, grab a copy of that info now */
1386 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_HOMOGENEOUS) &&
1387 (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) {
1388 struct nfs_fsattr nfsa;
1389 if (!nfs3_pathconf_rpc(*npp, &nfsa, ctx)) {
1390 /* cache a copy of the results */
1391 lck_mtx_lock(&nmp->nm_lock);
1392 nfs3_pathconf_cache(nmp, &nfsa);
1393 lck_mtx_unlock(&nmp->nm_lock);
1394 }
1395 }
1396 }
1397out:
1398 if (*npp && error) {
1399 vnode_put(NFSTOV(*npp));
b0d623f7 1400 vnode_recycle(NFSTOV(*npp));
2d21ac55
A
1401 *npp = NULL;
1402 }
1403 return (error);
1404}
1405
b0d623f7 1406int
2d21ac55
A
1407nfs4_mount(
1408 struct nfsmount *nmp,
1409 vfs_context_t ctx,
1410 __unused struct user_nfs_args *argp,
1411 nfsnode_t *npp)
1412{
1413 struct nfsm_chain nmreq, nmrep;
1414 int error = 0, numops, status, interval;
1415 char *path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
1416 char *name, *nextname;
1417 fhandle_t fh;
1418 struct nfs_vattr nvattr;
1419 u_int64_t xid;
2d21ac55
A
1420
1421 *npp = NULL;
1422 fh.fh_len = 0;
b0d623f7
A
1423 TAILQ_INIT(&nmp->nm_open_owners);
1424 TAILQ_INIT(&nmp->nm_recallq);
1425 nmp->nm_stategenid = 1;
2d21ac55
A
1426
1427 /* look up path to get fh and attrs for mount point root */
1428 numops = 2; // PUTROOTFH + LOOKUP* + GETATTR
1429 while (*path && (*path != '/'))
1430 path++;
1431 name = path;
1432 while (*name) {
1433 while (*name && (*name == '/'))
1434 name++;
1435 if (!*name)
1436 break;
1437 nextname = name;
1438 while (*nextname && (*nextname != '/'))
1439 nextname++;
1440 numops++;
1441 name = nextname;
1442 }
1443 nfsm_chain_build_alloc_init(error, &nmreq, 25 * NFSX_UNSIGNED);
1444 nfsm_chain_add_compound_header(error, &nmreq, "mount", numops);
1445 numops--;
1446 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTROOTFH);
1447 // (LOOKUP)*
1448 name = path;
1449 while (*name) {
1450 while (*name && (*name == '/'))
1451 name++;
1452 if (!*name)
1453 break;
1454 nextname = name;
1455 while (*nextname && (*nextname != '/'))
1456 nextname++;
1457 numops--;
1458 nfsm_chain_add_32(error, &nmreq, NFS_OP_LOOKUP);
1459 nfsm_chain_add_string(error, &nmreq, name, nextname - name);
1460 name = nextname;
1461 }
1462 numops--;
1463 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
1464 NFS4_DEFAULT_ATTRIBUTES(nmp->nm_fsattr.nfsa_supp_attr);
1465 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_FILEHANDLE);
1466 nfsm_chain_add_bitmap(error, &nmreq, nmp->nm_fsattr.nfsa_supp_attr, NFS_ATTR_BITMAP_LEN);
1467 nfsm_chain_build_done(error, &nmreq);
1468 nfsm_assert(error, (numops == 0), EPROTO);
1469 nfsmout_if(error);
1470 error = nfs_request(NULL, nmp->nm_mountp, &nmreq, NFSPROC4_COMPOUND, ctx, &nmrep, &xid, &status);
1471 nfsm_chain_skip_tag(error, &nmrep);
1472 nfsm_chain_get_32(error, &nmrep, numops);
1473 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTROOTFH);
1474 name = path;
1475 while (*name) {
1476 while (*name && (*name == '/'))
1477 name++;
1478 if (!*name)
1479 break;
1480 nextname = name;
1481 while (*nextname && (*nextname != '/'))
1482 nextname++;
1483 nfsm_chain_op_check(error, &nmrep, NFS_OP_LOOKUP);
1484 name = nextname;
1485 }
1486 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
1487 nfsmout_if(error);
1488 NFS_CLEAR_ATTRIBUTES(nmp->nm_fsattr.nfsa_bitmap);
1489 NFS_CLEAR_ATTRIBUTES(&nvattr.nva_bitmap);
1490 error = nfs4_parsefattr(&nmrep, &nmp->nm_fsattr, &nvattr, &fh, NULL);
1491 if (!error && !NFS_BITMAP_ISSET(&nvattr.nva_bitmap, NFS_FATTR_FILEHANDLE)) {
1492 printf("nfs: mount didn't return filehandle?\n");
1493 error = EBADRPC;
1494 }
1495 nfsmout_if(error);
1496
1497 error = nfs_nget(nmp->nm_mountp, NULL, NULL, fh.fh_data, fh.fh_len, &nvattr, &xid, NG_MARKROOT, npp);
1498 nfsmout_if(error);
1499
2d21ac55
A
1500 /* adjust I/O sizes to server limits */
1501 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXREAD)) {
1502 if (nmp->nm_fsattr.nfsa_maxread < (uint64_t)nmp->nm_rsize) {
1503 nmp->nm_rsize = nmp->nm_fsattr.nfsa_maxread & ~(NFS_FABLKSIZE - 1);
1504 if (nmp->nm_rsize == 0)
1505 nmp->nm_rsize = nmp->nm_fsattr.nfsa_maxread;
1506 }
1507 }
1508 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXWRITE)) {
1509 if (nmp->nm_fsattr.nfsa_maxwrite < (uint64_t)nmp->nm_wsize) {
1510 nmp->nm_wsize = nmp->nm_fsattr.nfsa_maxwrite & ~(NFS_FABLKSIZE - 1);
1511 if (nmp->nm_wsize == 0)
1512 nmp->nm_wsize = nmp->nm_fsattr.nfsa_maxwrite;
1513 }
1514 }
1515
1516 /* set up lease renew timer */
1517 nmp->nm_renew_timer = thread_call_allocate(nfs4_renew_timer, nmp);
1518 interval = nmp->nm_fsattr.nfsa_lease / 2;
1519 if (interval < 1)
1520 interval = 1;
1521 nfs_interval_timer_start(nmp->nm_renew_timer, interval * 1000);
1522
1523nfsmout:
1524 if (*npp)
b0d623f7 1525 nfs_node_unlock(*npp);
2d21ac55
A
1526 return (error);
1527}
1528
b0d623f7 1529int
91447636
A
1530mountnfs(
1531 struct user_nfs_args *argp,
1532 mount_t mp,
1533 mbuf_t nam,
2d21ac55 1534 vfs_context_t ctx,
91447636 1535 vnode_t *vpp)
1c79356b 1536{
91447636 1537 struct nfsmount *nmp;
2d21ac55 1538 nfsnode_t np;
b0d623f7
A
1539 int error;
1540 uint32_t maxio, iosize;
2d21ac55
A
1541 struct vfsstatfs *sbp;
1542 struct timespec ts = { 1, 0 };
0c530ab8 1543
55e303ae
A
1544 /*
1545 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
1546 * no sense in that context.
1547 */
1548 if (argp->sotype == SOCK_STREAM)
1549 argp->flags &= ~NFSMNT_NOCONN;
2d21ac55 1550
91447636 1551 if (vfs_flags(mp) & MNT_UPDATE) {
1c79356b
A
1552 nmp = VFSTONFS(mp);
1553 /* update paths, file handles, etc, here XXX */
91447636 1554 mbuf_freem(nam);
1c79356b
A
1555 return (0);
1556 } else {
1557 MALLOC_ZONE(nmp, struct nfsmount *,
1558 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
91447636
A
1559 if (!nmp) {
1560 mbuf_freem(nam);
1561 return (ENOMEM);
1562 }
1c79356b 1563 bzero((caddr_t)nmp, sizeof (struct nfsmount));
2d21ac55
A
1564 lck_mtx_init(&nmp->nm_lock, nfs_mount_grp, LCK_ATTR_NULL);
1565 TAILQ_INIT(&nmp->nm_resendq);
1566 TAILQ_INIT(&nmp->nm_iodq);
1567 TAILQ_INIT(&nmp->nm_gsscl);
91447636 1568 vfs_setfsprivate(mp, nmp);
2d21ac55
A
1569
1570 nfs_nhinit_finish();
1c79356b 1571 }
2d21ac55 1572 lck_mtx_lock(&nmp->nm_lock);
91447636
A
1573
1574 /* setup defaults */
2d21ac55 1575 nmp->nm_vers = NFS_VER2;
1c79356b
A
1576 nmp->nm_timeo = NFS_TIMEO;
1577 nmp->nm_retry = NFS_RETRANS;
55e303ae
A
1578 if (argp->sotype == SOCK_DGRAM) {
1579 nmp->nm_wsize = NFS_DGRAM_WSIZE;
1580 nmp->nm_rsize = NFS_DGRAM_RSIZE;
1581 } else {
1582 nmp->nm_wsize = NFS_WSIZE;
1583 nmp->nm_rsize = NFS_RSIZE;
1584 }
1c79356b
A
1585 nmp->nm_readdirsize = NFS_READDIRSIZE;
1586 nmp->nm_numgrps = NFS_MAXGRPS;
1587 nmp->nm_readahead = NFS_DEFRAHEAD;
55e303ae
A
1588 nmp->nm_tprintf_delay = nfs_tprintf_delay;
1589 if (nmp->nm_tprintf_delay < 0)
1590 nmp->nm_tprintf_delay = 0;
1591 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
1592 if (nmp->nm_tprintf_initial_delay < 0)
1593 nmp->nm_tprintf_initial_delay = 0;
91447636
A
1594 nmp->nm_acregmin = NFS_MINATTRTIMO;
1595 nmp->nm_acregmax = NFS_MAXATTRTIMO;
1596 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
1597 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
2d21ac55 1598 nmp->nm_auth = RPCAUTH_SYS;
b0d623f7 1599 nmp->nm_deadtimeout = 0;
91447636
A
1600
1601 vfs_getnewfsid(mp);
1602 nmp->nm_mountp = mp;
1603 vfs_setauthopaque(mp);
1604 nmp->nm_flag = argp->flags;
1c79356b
A
1605 nmp->nm_nam = nam;
1606
2d21ac55
A
1607 if (argp->flags & NFSMNT_NFSV4) {
1608 nmp->nm_vers = NFS_VER4;
1609 /* NFSv4 is only allowed over TCP. */
1610 if (argp->sotype != SOCK_STREAM) {
1611 error = EINVAL;
1612 goto bad;
1613 }
1614 } else if (argp->flags & NFSMNT_NFSV3)
1615 nmp->nm_vers = NFS_VER3;
1616
1617 if (nmp->nm_vers == NFS_VER2)
1618 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1619
1c79356b
A
1620 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
1621 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
1622 if (nmp->nm_timeo < NFS_MINTIMEO)
1623 nmp->nm_timeo = NFS_MINTIMEO;
1624 else if (nmp->nm_timeo > NFS_MAXTIMEO)
1625 nmp->nm_timeo = NFS_MAXTIMEO;
1626 }
1627
1628 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
1629 nmp->nm_retry = argp->retrans;
1630 if (nmp->nm_retry > NFS_MAXREXMIT)
1631 nmp->nm_retry = NFS_MAXREXMIT;
1632 }
1633
2d21ac55 1634 if (nmp->nm_vers != NFS_VER2) {
1c79356b
A
1635 if (argp->sotype == SOCK_DGRAM)
1636 maxio = NFS_MAXDGRAMDATA;
1637 else
1638 maxio = NFS_MAXDATA;
1639 } else
1640 maxio = NFS_V2MAXDATA;
1641
1642 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
1643 nmp->nm_wsize = argp->wsize;
1644 /* Round down to multiple of blocksize */
1645 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
1646 if (nmp->nm_wsize <= 0)
1647 nmp->nm_wsize = NFS_FABLKSIZE;
1648 }
1649 if (nmp->nm_wsize > maxio)
1650 nmp->nm_wsize = maxio;
0c530ab8
A
1651 if (nmp->nm_wsize > NFS_MAXBSIZE)
1652 nmp->nm_wsize = NFS_MAXBSIZE;
1c79356b
A
1653
1654 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
1655 nmp->nm_rsize = argp->rsize;
1656 /* Round down to multiple of blocksize */
1657 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
1658 if (nmp->nm_rsize <= 0)
1659 nmp->nm_rsize = NFS_FABLKSIZE;
1660 }
1661 if (nmp->nm_rsize > maxio)
1662 nmp->nm_rsize = maxio;
0c530ab8
A
1663 if (nmp->nm_rsize > NFS_MAXBSIZE)
1664 nmp->nm_rsize = NFS_MAXBSIZE;
1c79356b
A
1665
1666 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
1667 nmp->nm_readdirsize = argp->readdirsize;
1668 }
1669 if (nmp->nm_readdirsize > maxio)
1670 nmp->nm_readdirsize = maxio;
1671 if (nmp->nm_readdirsize > nmp->nm_rsize)
1672 nmp->nm_readdirsize = nmp->nm_rsize;
1673
1674 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
1675 argp->maxgrouplist <= NFS_MAXGRPS)
1676 nmp->nm_numgrps = argp->maxgrouplist;
1677 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
1678 argp->readahead <= NFS_MAXRAHEAD)
1679 nmp->nm_readahead = argp->readahead;
2d21ac55
A
1680 if (argp->flags & NFSMNT_READAHEAD)
1681 nmp->nm_readahead = argp->readahead;
1682 if (nmp->nm_readahead < 0)
1683 nmp->nm_readahead = 0;
1684 else if (nmp->nm_readahead > NFS_MAXRAHEAD)
1685 nmp->nm_readahead = NFS_MAXRAHEAD;
91447636
A
1686
1687 if (argp->version >= 4) {
1688 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
1689 nmp->nm_acregmin = argp->acregmin;
1690 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
1691 nmp->nm_acregmax = argp->acregmax;
1692 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
1693 nmp->nm_acdirmin = argp->acdirmin;
1694 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
1695 nmp->nm_acdirmax = argp->acdirmax;
1696 if (nmp->nm_acregmin > nmp->nm_acregmax)
1697 nmp->nm_acregmin = nmp->nm_acregmax;
1698 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
1699 nmp->nm_acdirmin = nmp->nm_acdirmax;
1700 }
2d21ac55
A
1701 if (argp->version >= 5) {
1702 if (argp->flags & NFSMNT_SECFLAVOR) {
1703 /*
1704 * Check for valid security flavor
1705 */
1706 switch (argp->auth) {
1707 case RPCAUTH_SYS:
1708 case RPCAUTH_KRB5:
1709 case RPCAUTH_KRB5I:
1710 case RPCAUTH_KRB5P:
1711 nmp->nm_auth = argp->auth;
1712 break;
1713 default:
1714 error = EINVAL;
1715 goto bad;
1716 }
1717 }
1718 }
b0d623f7
A
1719 if (argp->version >= 6) {
1720 if (argp->flags & NFSMNT_DEADTIMEOUT)
1721 nmp->nm_deadtimeout = argp->deadtimeout;
1722 }
1723 if ((nmp->nm_flag & NFSMNT_DEADTIMEOUT) && (nmp->nm_deadtimeout <= 0))
1724 nmp->nm_flag &= ~NFSMNT_DEADTIMEOUT;
91447636 1725
2d21ac55
A
1726 /* set up the version-specific function tables */
1727 if (nmp->nm_vers < NFS_VER4)
1728 nmp->nm_funcs = &nfs3_funcs;
1729 else
1730 nmp->nm_funcs = &nfs4_funcs;
1731
1732 /* Set up the sockets and related info */
1c79356b
A
1733 nmp->nm_sotype = argp->sotype;
1734 nmp->nm_soproto = argp->proto;
2d21ac55
A
1735 if (nmp->nm_sotype == SOCK_DGRAM)
1736 TAILQ_INIT(&nmp->nm_cwndq);
1737
1738 lck_mtx_unlock(&nmp->nm_lock);
1c79356b 1739
91447636 1740 /* make sure mbuf constants are set up */
2d21ac55 1741 if (!nfs_mbuf_mhlen)
91447636
A
1742 nfs_mbuf_init();
1743
b0d623f7
A
1744 if (nmp->nm_vers >= NFS_VER4) {
1745 struct timeval now;
1746 microtime(&now);
1747 nmp->nm_mounttime = ((uint64_t)now.tv_sec << 32) | now.tv_usec;
1748 nmp->nm_mcred = vfs_context_ucred(ctx);
1749 if (IS_VALID_CRED(nmp->nm_mcred))
1750 kauth_cred_ref(nmp->nm_mcred);
1751 nfs4_mount_callback_setup(nmp);
1752 }
2d21ac55
A
1753
1754 /* set up the socket */
b0d623f7 1755 if ((error = nfs_connect(nmp, 1)))
1c79356b
A
1756 goto bad;
1757
91447636 1758 /*
2d21ac55
A
1759 * Get the root node/attributes from the NFS server and
1760 * do any basic, version-specific setup.
91447636 1761 */
2d21ac55
A
1762 error = nmp->nm_funcs->nf_mount(nmp, ctx, argp, &np);
1763 if (error)
91447636 1764 goto bad;
91447636 1765
1c79356b 1766 /*
2d21ac55 1767 * A reference count is needed on the node representing the
1c79356b
A
1768 * remote root. If this object is not persistent, then backward
1769 * traversals of the mount point (i.e. "..") will not work if
2d21ac55 1770 * the node gets flushed out of the cache.
1c79356b 1771 */
2d21ac55
A
1772 nmp->nm_dnp = np;
1773 *vpp = NFSTOV(np);
1774 /* get usecount and drop iocount */
1775 error = vnode_ref(*vpp);
1776 vnode_put(*vpp);
b0d623f7
A
1777 if (error) {
1778 vnode_recycle(*vpp);
1c79356b 1779 goto bad;
b0d623f7 1780 }
1c79356b
A
1781
1782 /*
2d21ac55 1783 * Do statfs to ensure static info gets set to reasonable values.
1c79356b 1784 */
b0d623f7
A
1785 if ((error = nmp->nm_funcs->nf_update_statfs(nmp, ctx))) {
1786 int error2 = vnode_getwithref(*vpp);
1787 vnode_rele(*vpp);
1788 if (!error2)
1789 vnode_put(*vpp);
1790 vnode_recycle(*vpp);
55e303ae 1791 goto bad;
b0d623f7 1792 }
2d21ac55
A
1793 sbp = vfs_statfs(mp);
1794 sbp->f_bsize = nmp->nm_fsattr.nfsa_bsize;
1795 sbp->f_blocks = nmp->nm_fsattr.nfsa_space_total / sbp->f_bsize;
1796 sbp->f_bfree = nmp->nm_fsattr.nfsa_space_free / sbp->f_bsize;
1797 sbp->f_bavail = nmp->nm_fsattr.nfsa_space_avail / sbp->f_bsize;
1798 sbp->f_bused = (nmp->nm_fsattr.nfsa_space_total / sbp->f_bsize) -
1799 (nmp->nm_fsattr.nfsa_space_free / sbp->f_bsize);
1800 sbp->f_files = nmp->nm_fsattr.nfsa_files_total;
1801 sbp->f_ffree = nmp->nm_fsattr.nfsa_files_free;
1802 sbp->f_iosize = nfs_iosize;
55e303ae
A
1803
1804 /*
2d21ac55
A
1805 * Calculate the size used for I/O buffers. Use the larger
1806 * of the two sizes to minimise NFS requests but make sure
1807 * that it is at least one VM page to avoid wasting buffer
1808 * space and to allow easy mmapping of I/O buffers.
1809 * The read/write RPC calls handle the splitting up of
1810 * buffers into multiple requests if the buffer size is
1811 * larger than the I/O size.
55e303ae 1812 */
2d21ac55
A
1813 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
1814 if (iosize < PAGE_SIZE)
1815 iosize = PAGE_SIZE;
1816 nmp->nm_biosize = trunc_page_32(iosize);
1c79356b
A
1817
1818 /*
91447636
A
1819 * V3 mounts give us a (relatively) reliable remote access(2)
1820 * call, so advertise the fact.
1821 *
1822 * XXX this may not be the best way to go, as the granularity
1823 * offered isn't a good match to our needs.
1c79356b 1824 */
2d21ac55 1825 if (nmp->nm_vers != NFS_VER2)
91447636
A
1826 vfs_setauthopaqueaccess(mp);
1827
2d21ac55
A
1828 if (nmp->nm_flag & NFSMNT_LOCALLOCKS)
1829 vfs_setlocklocal(nmp->nm_mountp);
1830 if (!(nmp->nm_flag & (NFSMNT_NOLOCKS|NFSMNT_LOCALLOCKS)))
1831 nfs_lockd_mount_change(1);
1c79356b 1832
2d21ac55 1833 lck_mtx_lock(&nmp->nm_lock);
4a249263 1834 nmp->nm_state |= NFSSTA_MOUNTED;
2d21ac55 1835 lck_mtx_unlock(&nmp->nm_lock);
1c79356b
A
1836 return (0);
1837bad:
2d21ac55
A
1838 /* mark the socket for termination */
1839 lck_mtx_lock(&nmp->nm_lock);
1840 nmp->nm_sockflags |= NMSOCK_UNMOUNT;
1841 /* wait for any socket poking to complete */
1842 while (nmp->nm_sockflags & NMSOCK_POKE)
1843 msleep(&nmp->nm_sockflags, &nmp->nm_lock, PZERO-1, "nfswaitpoke", &ts);
1844 /* wait for the socket thread to terminate */
1845 while (nmp->nm_sockthd) {
1846 wakeup(&nmp->nm_sockthd);
1847 msleep(&nmp->nm_sockthd, &nmp->nm_lock, PZERO-1, "nfswaitsockthd", &ts);
1848 }
1849 /* tear down the socket */
1850 lck_mtx_unlock(&nmp->nm_lock);
1c79356b 1851 nfs_disconnect(nmp);
b0d623f7
A
1852 if (nmp->nm_vers >= NFS_VER4) {
1853 if (nmp->nm_cbid)
1854 nfs4_mount_callback_shutdown(nmp);
1855 if (nmp->nm_renew_timer) {
1856 thread_call_cancel(nmp->nm_renew_timer);
1857 thread_call_free(nmp->nm_renew_timer);
1858 }
1859 if (nmp->nm_longid) {
1860 /* remove/deallocate the client ID data */
1861 lck_mtx_lock(nfs_global_mutex);
1862 TAILQ_REMOVE(&nfsclientids, nmp->nm_longid, nci_link);
1863 if (nmp->nm_longid->nci_id)
1864 FREE(nmp->nm_longid->nci_id, M_TEMP);
1865 FREE(nmp->nm_longid, M_TEMP);
1866 lck_mtx_unlock(nfs_global_mutex);
1867 }
1868 if (IS_VALID_CRED(nmp->nm_mcred))
1869 kauth_cred_unref(&nmp->nm_mcred);
2d21ac55
A
1870 }
1871 lck_mtx_destroy(&nmp->nm_lock, nfs_mount_grp);
55e303ae 1872 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
91447636 1873 mbuf_freem(nam);
1c79356b
A
1874 return (error);
1875}
1876
b0d623f7
A
1877void
1878nfs3_umount_rpc(struct nfsmount *nmp, vfs_context_t ctx, int timeo)
1879{
1880 int error = 0, auth_len, slen;
1881 thread_t thd = vfs_context_thread(ctx);
1882 kauth_cred_t cred = vfs_context_ucred(ctx);
1883 char *path;
1884 uint64_t xid = 0;
1885 struct nfsm_chain nmreq, nmrep;
1886 mbuf_t mreq;
1887 uint32_t mntport = 0;
1888 struct sockaddr *nam = mbuf_data(nmp->nm_nam);
1889 struct sockaddr_in saddr;
1890
1891 bcopy(nam, &saddr, min(sizeof(saddr), nam->sa_len));
1892 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1893 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1894 5 * NFSX_UNSIGNED;
1895 nfsm_chain_null(&nmreq);
1896 nfsm_chain_null(&nmrep);
1897
1898 /* send portmap request to get mountd port */
1899 saddr.sin_port = htons(PMAPPORT);
1900 nfsm_chain_build_alloc_init(error, &nmreq, 4*NFSX_UNSIGNED);
1901 nfsm_chain_add_32(error, &nmreq, RPCPROG_MNT);
1902 nfsm_chain_add_32(error, &nmreq, RPCMNT_VER1);
1903 nfsm_chain_add_32(error, &nmreq, IPPROTO_UDP);
1904 nfsm_chain_add_32(error, &nmreq, 0);
1905 nfsm_chain_build_done(error, &nmreq);
1906 nfsmout_if(error);
1907 error = nfsm_rpchead2(SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
1908 RPCAUTH_SYS, auth_len, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
1909 nfsmout_if(error);
1910 nmreq.nmc_mhead = NULL;
1911 error = nfs_aux_request(nmp, thd, &saddr, mreq, R_XID32(xid), 0, timeo, &nmrep);
1912 nfsmout_if(error);
1913
1914 /* grab mountd port from portmap response */
1915 nfsm_chain_get_32(error, &nmrep, mntport);
1916 nfsmout_if(error);
1917 nfsm_chain_cleanup(&nmreq);
1918 nfsm_chain_cleanup(&nmrep);
1919 xid = 0;
1920
1921 /* MOUNT protocol UNMOUNT request */
1922 saddr.sin_port = htons(mntport);
1923 path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
1924 while (*path && (*path != '/'))
1925 path++;
1926 slen = strlen(path);
1927 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_UNSIGNED + nfsm_rndup(slen));
1928 nfsm_chain_add_string(error, &nmreq, path, slen);
1929 nfsm_chain_build_done(error, &nmreq);
1930 nfsmout_if(error);
1931 error = nfsm_rpchead2(SOCK_DGRAM, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMOUNT,
1932 RPCAUTH_SYS, auth_len, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
1933 nfsmout_if(error);
1934 nmreq.nmc_mhead = NULL;
1935 error = nfs_aux_request(nmp, thd, &saddr, mreq, R_XID32(xid), 1, timeo, &nmrep);
1936nfsmout:
1937 nfsm_chain_cleanup(&nmreq);
1938 nfsm_chain_cleanup(&nmrep);
1939}
1c79356b
A
1940
1941/*
1942 * unmount system call
1943 */
b0d623f7 1944int
2d21ac55 1945nfs_vfs_unmount(
91447636
A
1946 mount_t mp,
1947 int mntflags,
2d21ac55 1948 __unused vfs_context_t ctx)
1c79356b 1949{
2d21ac55 1950 struct nfsmount *nmp;
91447636 1951 vnode_t vp;
2d21ac55
A
1952 int error, flags = 0, docallback;
1953 struct nfsreq *req, *treq;
1954 struct nfs_reqqhead iodq;
1955 struct timespec ts = { 1, 0 };
b0d623f7
A
1956 struct nfs_open_owner *noop, *nextnoop;
1957 nfsnode_t np;
1c79356b 1958
1c79356b 1959 nmp = VFSTONFS(mp);
2d21ac55 1960 lck_mtx_lock(&nmp->nm_lock);
55e303ae
A
1961 /*
1962 * During a force unmount we want to...
1963 * Mark that we are doing a force unmount.
1964 * Make the mountpoint soft.
1965 */
1966 if (mntflags & MNT_FORCE) {
1967 flags |= FORCECLOSE;
1968 nmp->nm_state |= NFSSTA_FORCE;
1969 nmp->nm_flag |= NFSMNT_SOFT;
1970 }
1c79356b
A
1971 /*
1972 * Goes something like this..
1c79356b 1973 * - Call vflush() to clear out vnodes for this file system,
0b4e3aa0 1974 * except for the swap files. Deal with them in 2nd pass.
1c79356b
A
1975 * - Decrement reference on the vnode representing remote root.
1976 * - Close the socket
1977 * - Free up the data structures
1c79356b 1978 */
2d21ac55
A
1979 vp = NFSTOV(nmp->nm_dnp);
1980 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7 1981
0b4e3aa0 1982 /*
2d21ac55 1983 * vflush will check for busy vnodes on mountpoint.
0b4e3aa0
A
1984 * Will do the right thing for MNT_FORCE. That is, we should
1985 * not get EBUSY back.
1986 */
1987 error = vflush(mp, vp, SKIPSWAP | flags);
55e303ae 1988 if (mntflags & MNT_FORCE) {
fa4905b1 1989 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
55e303ae 1990 } else {
91447636 1991 if (vnode_isinuse(vp, 1))
0b4e3aa0 1992 return (EBUSY);
0b4e3aa0
A
1993 error = vflush(mp, vp, flags);
1994 }
91447636 1995 if (error)
1c79356b 1996 return (error);
1c79356b 1997
2d21ac55 1998 lck_mtx_lock(&nmp->nm_lock);
4a249263 1999 nmp->nm_state &= ~NFSSTA_MOUNTED;
b0d623f7 2000 nmp->nm_dnp = NULL;
2d21ac55 2001 lck_mtx_unlock(&nmp->nm_lock);
1c79356b
A
2002
2003 /*
2004 * Release the root vnode reference held by mountnfs()
2005 */
b0d623f7 2006 error = vnode_get(vp);
91447636 2007 vnode_rele(vp);
b0d623f7
A
2008 if (!error)
2009 vnode_put(vp);
91447636 2010
2d21ac55
A
2011 vflush(mp, NULLVP, FORCECLOSE);
2012
2013 /*
2014 * Destroy any RPCSEC_GSS contexts
2015 */
2016 if (!TAILQ_EMPTY(&nmp->nm_gsscl))
2017 nfs_gss_clnt_ctx_unmount(nmp, mntflags);
2018
2d21ac55
A
2019 /* mark the socket for termination */
2020 lck_mtx_lock(&nmp->nm_lock);
2021 nmp->nm_sockflags |= NMSOCK_UNMOUNT;
2022
b0d623f7
A
2023 /* stop callbacks */
2024 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_cbid)
2025 nfs4_mount_callback_shutdown(nmp);
2026
2d21ac55
A
2027 /* wait for any socket poking to complete */
2028 while (nmp->nm_sockflags & NMSOCK_POKE)
2029 msleep(&nmp->nm_sockflags, &nmp->nm_lock, PZERO-1, "nfswaitpoke", &ts);
2030
b0d623f7
A
2031 /* Have the socket thread send the unmount RPC, if requested/appropriate. */
2032 if ((nmp->nm_vers < NFS_VER4) && !(mntflags & MNT_FORCE) && (nmp->nm_flag & NFSMNT_CALLUMNT))
2033 nfs_mount_sock_thread_wake(nmp);
2034
2d21ac55
A
2035 /* wait for the socket thread to terminate */
2036 while (nmp->nm_sockthd) {
2037 wakeup(&nmp->nm_sockthd);
2038 msleep(&nmp->nm_sockthd, &nmp->nm_lock, PZERO-1, "nfswaitsockthd", &ts);
2039 }
2040
2d21ac55 2041 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
2042
2043 /* tear down the socket */
1c79356b 2044 nfs_disconnect(nmp);
b0d623f7
A
2045
2046 vfs_setfsprivate(mp, NULL);
2047
2d21ac55
A
2048 lck_mtx_lock(&nmp->nm_lock);
2049
b0d623f7
A
2050 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_cbid) {
2051 /* clear out any pending recall requests */
2052 while ((np = TAILQ_FIRST(&nmp->nm_recallq))) {
2053 TAILQ_REMOVE(&nmp->nm_recallq, np, n_dlink);
2054 np->n_dlink.tqe_next = NFSNOLIST;
2055 }
2056 }
2057
2d21ac55 2058 /* cancel any renew timer */
b0d623f7 2059 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_renew_timer) {
2d21ac55
A
2060 thread_call_cancel(nmp->nm_renew_timer);
2061 thread_call_free(nmp->nm_renew_timer);
2062 }
2063
91447636 2064 mbuf_freem(nmp->nm_nam);
2d21ac55 2065 lck_mtx_unlock(&nmp->nm_lock);
1c79356b 2066
b0d623f7 2067 if ((nmp->nm_vers < NFS_VER4) && !(nmp->nm_flag & (NFSMNT_NOLOCKS|NFSMNT_LOCALLOCKS)))
2d21ac55
A
2068 nfs_lockd_mount_change(-1);
2069
b0d623f7
A
2070 if ((nmp->nm_vers >= NFS_VER4) && nmp->nm_longid) {
2071 /* remove/deallocate the client ID data */
2072 lck_mtx_lock(nfs_global_mutex);
2073 TAILQ_REMOVE(&nfsclientids, nmp->nm_longid, nci_link);
2074 if (nmp->nm_longid->nci_id)
2075 FREE(nmp->nm_longid->nci_id, M_TEMP);
2076 FREE(nmp->nm_longid, M_TEMP);
2077 lck_mtx_unlock(nfs_global_mutex);
2078 }
2079
2d21ac55
A
2080 /*
2081 * Loop through outstanding request list and remove dangling
2082 * references to defunct nfsmount struct
2083 */
2084 TAILQ_INIT(&iodq);
2085 lck_mtx_lock(nfs_request_mutex);
2086 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
2087 if (req->r_nmp == nmp) {
2088 lck_mtx_lock(&req->r_mtx);
2089 req->r_nmp = NULL;
2090 lck_mtx_unlock(&req->r_mtx);
2091 if (req->r_callback.rcb_func) {
2092 /* async I/O RPC needs to be finished */
2093 lck_mtx_lock(nfsiod_mutex);
2094 if (req->r_achain.tqe_next == NFSREQNOLIST)
2095 TAILQ_INSERT_TAIL(&iodq, req, r_achain);
2096 lck_mtx_unlock(nfsiod_mutex);
2097 }
b0d623f7 2098 lck_mtx_lock(&req->r_mtx);
2d21ac55 2099 lck_mtx_lock(&nmp->nm_lock);
b0d623f7
A
2100 if (req->r_flags & R_RESENDQ) {
2101 if (req->r_rchain.tqe_next != NFSREQNOLIST) {
2102 TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain);
2103 req->r_rchain.tqe_next = NFSREQNOLIST;
2104 }
2d21ac55
A
2105 req->r_flags &= ~R_RESENDQ;
2106 }
2107 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7 2108 lck_mtx_unlock(&req->r_mtx);
2d21ac55 2109 wakeup(req);
ccc36f2f 2110 }
1c79356b 2111 }
2d21ac55
A
2112 lck_mtx_unlock(nfs_request_mutex);
2113
2114 /* finish any async I/O RPCs queued up */
2115 lck_mtx_lock(nfsiod_mutex);
2116 TAILQ_CONCAT(&iodq, &nmp->nm_iodq, r_achain);
2117 lck_mtx_unlock(nfsiod_mutex);
2118 TAILQ_FOREACH_SAFE(req, &iodq, r_achain, treq) {
2119 TAILQ_REMOVE(&iodq, req, r_achain);
2120 req->r_achain.tqe_next = NFSREQNOLIST;
2121 lck_mtx_lock(&req->r_mtx);
2122 req->r_error = ENXIO;
2123 docallback = !(req->r_flags & R_WAITSENT);
2124 lck_mtx_unlock(&req->r_mtx);
2125 if (docallback)
2126 req->r_callback.rcb_func(req);
2127 }
2128
b0d623f7
A
2129 /* clean up open owner list */
2130 if (nmp->nm_vers >= NFS_VER4) {
2131 lck_mtx_lock(&nmp->nm_lock);
2132 TAILQ_FOREACH_SAFE(noop, &nmp->nm_open_owners, noo_link, nextnoop) {
2133 TAILQ_REMOVE(&nmp->nm_open_owners, noop, noo_link);
2134 noop->noo_flags &= ~NFS_OPEN_OWNER_LINK;
2135 if (noop->noo_refcnt)
2136 continue;
2137 nfs_open_owner_destroy(noop);
2138 }
2139 lck_mtx_unlock(&nmp->nm_lock);
2140 if (IS_VALID_CRED(nmp->nm_mcred))
2141 kauth_cred_unref(&nmp->nm_mcred);
2142 }
2143
2d21ac55
A
2144 lck_mtx_destroy(&nmp->nm_lock, nfs_mount_grp);
2145 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1c79356b
A
2146 return (0);
2147}
2148
2149/*
2150 * Return root of a filesystem
2151 */
b0d623f7 2152int
2d21ac55 2153nfs_vfs_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t ctx)
1c79356b 2154{
91447636 2155 vnode_t vp;
1c79356b 2156 struct nfsmount *nmp;
91447636 2157 int error;
b0d623f7 2158 u_int32_t vpid;
1c79356b
A
2159
2160 nmp = VFSTONFS(mp);
b0d623f7
A
2161 if (!nmp || !nmp->nm_dnp)
2162 return (ENXIO);
2d21ac55 2163 vp = NFSTOV(nmp->nm_dnp);
91447636
A
2164 vpid = vnode_vid(vp);
2165 while ((error = vnode_getwithvid(vp, vpid))) {
2166 /* vnode_get() may return ENOENT if the dir changes. */
2167 /* If that happens, just try it again, else return the error. */
2168 if ((error != ENOENT) || (vnode_vid(vp) == vpid))
55e303ae 2169 return (error);
91447636 2170 vpid = vnode_vid(vp);
55e303ae 2171 }
1c79356b
A
2172 *vpp = vp;
2173 return (0);
2174}
2175
2d21ac55
A
2176/*
2177 * Do operations associated with quotas
2178 */
2179#if !QUOTA
b0d623f7 2180int
2d21ac55
A
2181nfs_vfs_quotactl(
2182 __unused mount_t mp,
2183 __unused int cmds,
2184 __unused uid_t uid,
2185 __unused caddr_t datap,
2186 __unused vfs_context_t context)
2187{
2188 return (ENOTSUP);
2189}
2190#else
2d21ac55 2191
b0d623f7
A
2192int
2193nfs3_getquota(struct nfsmount *nmp, vfs_context_t ctx, uid_t id, int type, struct dqblk *dqb)
2d21ac55
A
2194{
2195 int error = 0, auth_len, slen, timeo;
2196 int rqvers = (type == GRPQUOTA) ? RPCRQUOTA_EXT_VER : RPCRQUOTA_VER;
2197 thread_t thd = vfs_context_thread(ctx);
2198 kauth_cred_t cred = vfs_context_ucred(ctx);
2199 char *path;
2200 uint64_t xid = 0;
2201 struct nfsm_chain nmreq, nmrep;
2202 mbuf_t mreq;
b0d623f7 2203 uint32_t val = 0, bsize = 0;
2d21ac55
A
2204 struct sockaddr *nam = mbuf_data(nmp->nm_nam);
2205 struct sockaddr_in saddr;
2206 struct timeval now;
2207
2208 bcopy(nam, &saddr, min(sizeof(saddr), nam->sa_len));
2209 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
2210 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
2211 5 * NFSX_UNSIGNED;
2212 timeo = (nmp->nm_flag & NFSMNT_SOFT) ? 10 : 60;
2213 nfsm_chain_null(&nmreq);
2214 nfsm_chain_null(&nmrep);
2215
2216 /* check if we have a recently cached rquota port */
2217 if (nmp->nm_rqport) {
2218 microuptime(&now);
2219 if ((nmp->nm_rqportstamp + 60) >= (uint32_t)now.tv_sec)
2220 goto got_rqport;
2221 }
2222
2223 /* send portmap request to get rquota port */
2224 saddr.sin_port = htons(PMAPPORT);
2225 nfsm_chain_build_alloc_init(error, &nmreq, 4*NFSX_UNSIGNED);
2226 nfsm_chain_add_32(error, &nmreq, RPCPROG_RQUOTA);
2227 nfsm_chain_add_32(error, &nmreq, rqvers);
2228 nfsm_chain_add_32(error, &nmreq, IPPROTO_UDP);
2229 nfsm_chain_add_32(error, &nmreq, 0);
2230 nfsm_chain_build_done(error, &nmreq);
2231 nfsmout_if(error);
2232 error = nfsm_rpchead2(SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
2233 RPCAUTH_SYS, auth_len, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
2234 nfsmout_if(error);
2235 nmreq.nmc_mhead = NULL;
b0d623f7 2236 error = nfs_aux_request(nmp, thd, &saddr, mreq, R_XID32(xid), 0, timeo, &nmrep);
2d21ac55
A
2237 nfsmout_if(error);
2238
2239 /* grab rquota port from portmap response */
2240 nfsm_chain_get_32(error, &nmrep, val);
2241 nfsmout_if(error);
2242 nmp->nm_rqport = val;
2243 microuptime(&now);
2244 nmp->nm_rqportstamp = now.tv_sec;
2245 nfsm_chain_cleanup(&nmreq);
2246 nfsm_chain_cleanup(&nmrep);
2247 xid = 0;
2248
2249got_rqport:
2250 /* rquota request */
2251 saddr.sin_port = htons(nmp->nm_rqport);
2252 path = &vfs_statfs(nmp->nm_mountp)->f_mntfromname[0];
2253 while (*path && (*path != '/'))
2254 path++;
2255 slen = strlen(path);
2256 nfsm_chain_build_alloc_init(error, &nmreq, 3 * NFSX_UNSIGNED + nfsm_rndup(slen));
2257 nfsm_chain_add_string(error, &nmreq, path, slen);
2258 if (type == GRPQUOTA)
2259 nfsm_chain_add_32(error, &nmreq, type);
2260 nfsm_chain_add_32(error, &nmreq, id);
2261 nfsm_chain_build_done(error, &nmreq);
2262 nfsmout_if(error);
2263 error = nfsm_rpchead2(SOCK_DGRAM, RPCPROG_RQUOTA, rqvers, RPCRQUOTA_GET,
2264 RPCAUTH_SYS, auth_len, cred, NULL, nmreq.nmc_mhead, &xid, &mreq);
2265 nfsmout_if(error);
2266 nmreq.nmc_mhead = NULL;
b0d623f7 2267 error = nfs_aux_request(nmp, thd, &saddr, mreq, R_XID32(xid), 0, timeo, &nmrep);
2d21ac55
A
2268 nfsmout_if(error);
2269
2270 /* parse rquota response */
2271 nfsm_chain_get_32(error, &nmrep, val);
2272 if (!error && (val != RQUOTA_STAT_OK)) {
2273 if (val == RQUOTA_STAT_NOQUOTA)
2274 error = ENOENT;
2275 else if (val == RQUOTA_STAT_EPERM)
2276 error = EPERM;
2277 else
2278 error = EIO;
2279 }
2280 nfsm_chain_get_32(error, &nmrep, bsize);
2281 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
2282 nfsm_chain_get_32(error, &nmrep, val);
2283 nfsmout_if(error);
2284 dqb->dqb_bhardlimit = (uint64_t)val * bsize;
2285 nfsm_chain_get_32(error, &nmrep, val);
2286 nfsmout_if(error);
2287 dqb->dqb_bsoftlimit = (uint64_t)val * bsize;
2288 nfsm_chain_get_32(error, &nmrep, val);
2289 nfsmout_if(error);
2290 dqb->dqb_curbytes = (uint64_t)val * bsize;
2291 nfsm_chain_get_32(error, &nmrep, dqb->dqb_ihardlimit);
2292 nfsm_chain_get_32(error, &nmrep, dqb->dqb_isoftlimit);
2293 nfsm_chain_get_32(error, &nmrep, dqb->dqb_curinodes);
2294 nfsm_chain_get_32(error, &nmrep, dqb->dqb_btime);
2295 nfsm_chain_get_32(error, &nmrep, dqb->dqb_itime);
2296 nfsmout_if(error);
2297 dqb->dqb_id = id;
2298nfsmout:
2299 nfsm_chain_cleanup(&nmreq);
2300 nfsm_chain_cleanup(&nmrep);
2301 return (error);
2302}
2303
b0d623f7
A
2304int
2305nfs4_getquota(struct nfsmount *nmp, vfs_context_t ctx, uid_t id, int type, struct dqblk *dqb)
2d21ac55
A
2306{
2307 nfsnode_t np;
2308 int error = 0, status, nfsvers, numops;
2309 u_int64_t xid;
2310 struct nfsm_chain nmreq, nmrep;
2311 uint32_t bitmap[NFS_ATTR_BITMAP_LEN];
2312 thread_t thd = vfs_context_thread(ctx);
2313 kauth_cred_t cred = vfs_context_ucred(ctx);
2314
2315 if (type != USRQUOTA) /* NFSv4 only supports user quotas */
2316 return (ENOTSUP);
2317
2318 /* first check that the server supports any of the quota attributes */
2319 if (!NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_HARD) &&
2320 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_AVAIL_SOFT) &&
2321 !NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_supp_attr, NFS_FATTR_QUOTA_USED))
2322 return (ENOTSUP);
2323
2324 /*
2325 * The credential passed to the server needs to have
2326 * an effective uid that matches the given uid.
2327 */
2328 if (id != kauth_cred_getuid(cred)) {
2329 struct ucred temp_cred;
2330 bzero(&temp_cred, sizeof(temp_cred));
2331 temp_cred.cr_uid = id;
2332 temp_cred.cr_ngroups = cred->cr_ngroups;
2333 bcopy(cred->cr_groups, temp_cred.cr_groups, sizeof(temp_cred.cr_groups));
2334 cred = kauth_cred_create(&temp_cred);
2335 if (!IS_VALID_CRED(cred))
2336 return (ENOMEM);
2337 } else {
2338 kauth_cred_ref(cred);
2339 }
2340
2341 nfsvers = nmp->nm_vers;
2342 np = nmp->nm_dnp;
b0d623f7
A
2343 if (!np)
2344 error = ENXIO;
2345 if (error || ((error = vnode_get(NFSTOV(np))))) {
2d21ac55
A
2346 kauth_cred_unref(&cred);
2347 return(error);
2348 }
2349
2350 nfsm_chain_null(&nmreq);
2351 nfsm_chain_null(&nmrep);
2352
2353 // PUTFH + GETATTR
2354 numops = 2;
2355 nfsm_chain_build_alloc_init(error, &nmreq, 15 * NFSX_UNSIGNED);
2356 nfsm_chain_add_compound_header(error, &nmreq, "quota", numops);
2357 numops--;
2358 nfsm_chain_add_32(error, &nmreq, NFS_OP_PUTFH);
2359 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
2360 numops--;
2361 nfsm_chain_add_32(error, &nmreq, NFS_OP_GETATTR);
2362 NFS_CLEAR_ATTRIBUTES(bitmap);
2363 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_AVAIL_HARD);
2364 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_AVAIL_SOFT);
2365 NFS_BITMAP_SET(bitmap, NFS_FATTR_QUOTA_USED);
2366 nfsm_chain_add_bitmap_masked(error, &nmreq, bitmap,
2367 NFS_ATTR_BITMAP_LEN, nmp->nm_fsattr.nfsa_supp_attr);
2368 nfsm_chain_build_done(error, &nmreq);
2369 nfsm_assert(error, (numops == 0), EPROTO);
2370 nfsmout_if(error);
2371 error = nfs_request2(np, NULL, &nmreq, NFSPROC4_COMPOUND, thd, cred, 0, &nmrep, &xid, &status);
2372 nfsm_chain_skip_tag(error, &nmrep);
2373 nfsm_chain_get_32(error, &nmrep, numops);
2374 nfsm_chain_op_check(error, &nmrep, NFS_OP_PUTFH);
2375 nfsm_chain_op_check(error, &nmrep, NFS_OP_GETATTR);
2376 nfsm_assert(error, NFSTONMP(np), ENXIO);
2377 nfsmout_if(error);
2378 error = nfs4_parsefattr(&nmrep, NULL, NULL, NULL, dqb);
2379 nfsmout_if(error);
2380 nfsm_assert(error, NFSTONMP(np), ENXIO);
2381nfsmout:
2382 nfsm_chain_cleanup(&nmreq);
2383 nfsm_chain_cleanup(&nmrep);
2384 vnode_put(NFSTOV(np));
2385 kauth_cred_unref(&cred);
2386 return (error);
2387}
2388
b0d623f7 2389int
2d21ac55
A
2390nfs_vfs_quotactl(mount_t mp, int cmds, uid_t uid, caddr_t datap, vfs_context_t ctx)
2391{
2392 struct nfsmount *nmp;
2393 int cmd, type, error, nfsvers;
2394 uid_t ruid = vfs_context_ucred(ctx)->cr_ruid;
2395 struct dqblk *dqb = (struct dqblk*)datap;
2396
2397 if (!(nmp = VFSTONFS(mp)))
2398 return (ENXIO);
2399 nfsvers = nmp->nm_vers;
2400
2401 if (uid == ~0U)
2402 uid = ruid;
2403
2404 /* we can only support Q_GETQUOTA */
2405 cmd = cmds >> SUBCMDSHIFT;
2406 switch (cmd) {
2407 case Q_GETQUOTA:
2408 break;
2409 case Q_QUOTAON:
2410 case Q_QUOTAOFF:
2411 case Q_SETQUOTA:
2412 case Q_SETUSE:
2413 case Q_SYNC:
2414 case Q_QUOTASTAT:
2415 return (ENOTSUP);
2416 default:
2417 return (EINVAL);
2418 }
2419
2420 type = cmds & SUBCMDMASK;
2421 if ((u_int)type >= MAXQUOTAS)
2422 return (EINVAL);
2423 if ((uid != ruid) && ((error = vfs_context_suser(ctx))))
2424 return (error);
2425
2426 if (vfs_busy(mp, LK_NOWAIT))
2427 return (0);
2428 bzero(dqb, sizeof(*dqb));
2429 error = nmp->nm_funcs->nf_getquota(nmp, ctx, uid, type, dqb);
2430 vfs_unbusy(mp);
2431 return (error);
2432}
2433#endif
2434
1c79356b
A
2435/*
2436 * Flush out the buffer cache
2437 */
b0d623f7 2438int nfs_sync_callout(vnode_t, void *);
91447636
A
2439
2440struct nfs_sync_cargs {
2d21ac55
A
2441 thread_t thd;
2442 int waitfor;
2443 int error;
91447636
A
2444};
2445
b0d623f7 2446int
91447636 2447nfs_sync_callout(vnode_t vp, void *arg)
1c79356b 2448{
91447636
A
2449 struct nfs_sync_cargs *cargs = (struct nfs_sync_cargs*)arg;
2450 int error;
1c79356b 2451
91447636
A
2452 if (LIST_EMPTY(&VTONFS(vp)->n_dirtyblkhd))
2453 return (VNODE_RETURNED);
2d21ac55
A
2454 if (VTONFS(vp)->n_wrbusy > 0)
2455 return (VNODE_RETURNED);
2456 if (VTONFS(vp)->n_bflag & (NBFLUSHINPROG|NBINVALINPROG))
91447636
A
2457 return (VNODE_RETURNED);
2458
2d21ac55 2459 error = nfs_flush(VTONFS(vp), cargs->waitfor, cargs->thd, 0);
91447636
A
2460 if (error)
2461 cargs->error = error;
2462
2463 return (VNODE_RETURNED);
2464}
2465
b0d623f7 2466int
2d21ac55 2467nfs_vfs_sync(mount_t mp, int waitfor, vfs_context_t ctx)
91447636
A
2468{
2469 struct nfs_sync_cargs cargs;
2470
2471 cargs.waitfor = waitfor;
2d21ac55 2472 cargs.thd = vfs_context_thread(ctx);
91447636
A
2473 cargs.error = 0;
2474
2475 vnode_iterate(mp, 0, nfs_sync_callout, &cargs);
2476
2477 return (cargs.error);
1c79356b
A
2478}
2479
2480/*
2481 * NFS flat namespace lookup.
2482 * Currently unsupported.
2483 */
91447636 2484/*ARGSUSED*/
b0d623f7 2485int
2d21ac55 2486nfs_vfs_vget(
91447636
A
2487 __unused mount_t mp,
2488 __unused ino64_t ino,
2489 __unused vnode_t *vpp,
2d21ac55 2490 __unused vfs_context_t ctx)
1c79356b
A
2491{
2492
91447636 2493 return (ENOTSUP);
1c79356b
A
2494}
2495
2496/*
2497 * At this point, this should never happen
2498 */
91447636 2499/*ARGSUSED*/
b0d623f7 2500int
2d21ac55 2501nfs_vfs_fhtovp(
91447636
A
2502 __unused mount_t mp,
2503 __unused int fhlen,
2504 __unused unsigned char *fhp,
2505 __unused vnode_t *vpp,
2d21ac55 2506 __unused vfs_context_t ctx)
1c79356b
A
2507{
2508
91447636 2509 return (ENOTSUP);
1c79356b
A
2510}
2511
2512/*
2513 * Vnode pointer to File handle, should never happen either
2514 */
91447636 2515/*ARGSUSED*/
b0d623f7 2516int
2d21ac55 2517nfs_vfs_vptofh(
91447636
A
2518 __unused vnode_t vp,
2519 __unused int *fhlenp,
2520 __unused unsigned char *fhp,
2d21ac55 2521 __unused vfs_context_t ctx)
1c79356b
A
2522{
2523
91447636 2524 return (ENOTSUP);
1c79356b
A
2525}
2526
2527/*
2528 * Vfs start routine, a no-op.
2529 */
91447636 2530/*ARGSUSED*/
b0d623f7 2531int
2d21ac55 2532nfs_vfs_start(
91447636
A
2533 __unused mount_t mp,
2534 __unused int flags,
2d21ac55 2535 __unused vfs_context_t ctx)
1c79356b
A
2536{
2537
2538 return (0);
2539}
2540
1c79356b
A
2541/*
2542 * Do that sysctl thang...
2543 */
b0d623f7 2544int
2d21ac55
A
2545nfs_vfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
2546 user_addr_t newp, size_t newlen, vfs_context_t ctx)
1c79356b 2547{
cf7d32b8 2548 int error = 0, val, softnobrowse;
91447636 2549 struct sysctl_req *req = NULL;
b0d623f7 2550 union union_vfsidctl vc;
91447636
A
2551 mount_t mp;
2552 struct nfsmount *nmp = NULL;
55e303ae 2553 struct vfsquery vq;
91447636 2554 boolean_t is_64_bit;
2d21ac55
A
2555#if NFSSERVER
2556 struct nfs_exportfs *nxfs;
2557 struct nfs_export *nx;
2558 struct nfs_active_user_list *ulist;
2559 struct nfs_export_stat_desc stat_desc;
2560 struct nfs_export_stat_rec statrec;
2561 struct nfs_user_stat_node *unode, *unode_next;
2562 struct nfs_user_stat_desc ustat_desc;
2563 struct nfs_user_stat_user_rec ustat_rec;
2564 struct nfs_user_stat_path_rec upath_rec;
2565 uint bytes_avail, bytes_total, recs_copied;
2566 uint numExports, totlen, pos, numRecs, count;
2567#endif /* NFSSERVER */
1c79356b
A
2568
2569 /*
2570 * All names at this level are terminal.
2571 */
2d21ac55
A
2572 if (namelen > 1)
2573 return (ENOTDIR); /* overloaded */
1c79356b 2574
2d21ac55 2575 is_64_bit = vfs_context_is64bit(ctx);
91447636 2576
55e303ae
A
2577 /* common code for "new style" VFS_CTL sysctl, get the mount. */
2578 switch (name[0]) {
2579 case VFS_CTL_TIMEO:
2580 case VFS_CTL_QUERY:
e5568f75 2581 case VFS_CTL_NOLOCKS:
91447636 2582 req = CAST_DOWN(struct sysctl_req *, oldp);
b0d623f7
A
2583 error = SYSCTL_IN(req, &vc, is_64_bit? sizeof(vc.vc64):sizeof(vc.vc32));
2584 if (error)
2585 return (error);
2586 mp = vfs_getvfs(&vc.vc32.vc_fsid); /* works for 32 and 64 */
55e303ae
A
2587 if (mp == NULL)
2588 return (ENOENT);
2589 nmp = VFSTONFS(mp);
2590 if (nmp == NULL)
2591 return (ENOENT);
2592 bzero(&vq, sizeof(vq));
91447636
A
2593 req->newidx = 0;
2594 if (is_64_bit) {
b0d623f7
A
2595 req->newptr = vc.vc64.vc_ptr;
2596 req->newlen = (size_t)vc.vc64.vc_len;
2d21ac55 2597 } else {
b0d623f7
A
2598 req->newptr = CAST_USER_ADDR_T(vc.vc32.vc_ptr);
2599 req->newlen = vc.vc32.vc_len;
91447636 2600 }
55e303ae
A
2601 }
2602
1c79356b
A
2603 switch(name[0]) {
2604 case NFS_NFSSTATS:
2d21ac55 2605 if (!oldp) {
1c79356b 2606 *oldlenp = sizeof nfsstats;
2d21ac55 2607 return (0);
1c79356b
A
2608 }
2609
2d21ac55 2610 if (*oldlenp < sizeof nfsstats) {
1c79356b 2611 *oldlenp = sizeof nfsstats;
2d21ac55 2612 return (ENOMEM);
1c79356b
A
2613 }
2614
55e303ae
A
2615 error = copyout(&nfsstats, oldp, sizeof nfsstats);
2616 if (error)
2617 return (error);
1c79356b 2618
2d21ac55
A
2619 if (newp && newlen != sizeof nfsstats)
2620 return (EINVAL);
1c79356b 2621
2d21ac55 2622 if (newp)
1c79356b 2623 return copyin(newp, &nfsstats, sizeof nfsstats);
2d21ac55
A
2624 return (0);
2625#if NFSSERVER
2626 case NFS_EXPORTSTATS:
2627 /* setup export stat descriptor */
2628 stat_desc.rec_vers = NFS_EXPORT_STAT_REC_VERSION;
2629
2630 if (!nfsrv_is_initialized()) {
2631 stat_desc.rec_count = 0;
2632 if (oldp && (*oldlenp >= sizeof(struct nfs_export_stat_desc)))
2633 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
2634 *oldlenp = sizeof(struct nfs_export_stat_desc);
2635 return (error);
2636 }
2637
2638 /* Count the number of exported directories */
2639 lck_rw_lock_shared(&nfsrv_export_rwlock);
2640 numExports = 0;
2641 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next)
2642 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next)
2643 numExports += 1;
2644
2645 /* update stat descriptor's export record count */
2646 stat_desc.rec_count = numExports;
2647
2648 /* calculate total size of required buffer */
2649 totlen = sizeof(struct nfs_export_stat_desc) + (numExports * sizeof(struct nfs_export_stat_rec));
2650
2651 /* Check caller's buffer */
2652 if (oldp == 0) {
2653 lck_rw_done(&nfsrv_export_rwlock);
2654 /* indicate required buffer len */
2655 *oldlenp = totlen;
2656 return (0);
2657 }
2658
2659 /* We require the caller's buffer to be at least large enough to hold the descriptor */
2660 if (*oldlenp < sizeof(struct nfs_export_stat_desc)) {
2661 lck_rw_done(&nfsrv_export_rwlock);
2662 /* indicate required buffer len */
2663 *oldlenp = totlen;
2664 return (ENOMEM);
2665 }
2666
2667 /* indicate required buffer len */
2668 *oldlenp = totlen;
2669
2670 /* check if export table is empty */
2671 if (!numExports) {
2672 lck_rw_done(&nfsrv_export_rwlock);
2673 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
2674 return (error);
2675 }
2676
2677 /* calculate how many actual export stat records fit into caller's buffer */
2678 numRecs = (*oldlenp - sizeof(struct nfs_export_stat_desc)) / sizeof(struct nfs_export_stat_rec);
2679
2680 if (!numRecs) {
2681 /* caller's buffer can only accomodate descriptor */
2682 lck_rw_done(&nfsrv_export_rwlock);
2683 stat_desc.rec_count = 0;
2684 error = copyout(&stat_desc, oldp, sizeof(struct nfs_export_stat_desc));
2685 return (error);
2686 }
2687
2688 /* adjust to actual number of records to copyout to caller's buffer */
2689 if (numRecs > numExports)
2690 numRecs = numExports;
2691
2692 /* set actual number of records we are returning */
2693 stat_desc.rec_count = numRecs;
2694
2695 /* first copy out the stat descriptor */
2696 pos = 0;
2697 error = copyout(&stat_desc, oldp + pos, sizeof(struct nfs_export_stat_desc));
2698 if (error) {
2699 lck_rw_done(&nfsrv_export_rwlock);
2700 return (error);
2701 }
2702 pos += sizeof(struct nfs_export_stat_desc);
2703
2704 /* Loop through exported directories */
2705 count = 0;
2706 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
2707 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
2708
2709 if (count >= numRecs)
2710 break;
2711
2712 /* build exported filesystem path */
2713 snprintf(statrec.path, sizeof(statrec.path), "%s%s%s",
2714 nxfs->nxfs_path, ((nxfs->nxfs_path[1] && nx->nx_path[0]) ? "/" : ""),
2715 nx->nx_path);
2716
2717 /* build the 64-bit export stat counters */
2718 statrec.ops = ((uint64_t)nx->nx_stats.ops.hi << 32) |
2719 nx->nx_stats.ops.lo;
2720 statrec.bytes_read = ((uint64_t)nx->nx_stats.bytes_read.hi << 32) |
2721 nx->nx_stats.bytes_read.lo;
2722 statrec.bytes_written = ((uint64_t)nx->nx_stats.bytes_written.hi << 32) |
2723 nx->nx_stats.bytes_written.lo;
2724 error = copyout(&statrec, oldp + pos, sizeof(statrec));
2725 if (error) {
2726 lck_rw_done(&nfsrv_export_rwlock);
2727 return (error);
2728 }
2729 /* advance buffer position */
2730 pos += sizeof(statrec);
2731 }
2732 }
2733 lck_rw_done(&nfsrv_export_rwlock);
2734 break;
2735 case NFS_USERSTATS:
2736 /* init structures used for copying out of kernel */
2737 ustat_desc.rec_vers = NFS_USER_STAT_REC_VERSION;
2738 ustat_rec.rec_type = NFS_USER_STAT_USER_REC;
2739 upath_rec.rec_type = NFS_USER_STAT_PATH_REC;
2740
2741 /* initialize counters */
2742 bytes_total = sizeof(struct nfs_user_stat_desc);
2743 bytes_avail = *oldlenp;
2744 recs_copied = 0;
2745
2746 if (!nfsrv_is_initialized()) /* NFS server not initialized, so no stats */
2747 goto ustat_skip;
2748
2749 /* reclaim old expired user nodes */
2750 nfsrv_active_user_list_reclaim();
2751
2752 /* reserve space for the buffer descriptor */
2753 if (bytes_avail >= sizeof(struct nfs_user_stat_desc))
2754 bytes_avail -= sizeof(struct nfs_user_stat_desc);
2755 else
2756 bytes_avail = 0;
2757
2758 /* put buffer position past the buffer descriptor */
2759 pos = sizeof(struct nfs_user_stat_desc);
2760
2761 /* Loop through exported directories */
2762 lck_rw_lock_shared(&nfsrv_export_rwlock);
2763 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
2764 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
2765 /* copy out path */
2766 if (bytes_avail >= sizeof(struct nfs_user_stat_path_rec)) {
2767 snprintf(upath_rec.path, sizeof(upath_rec.path), "%s%s%s",
2768 nxfs->nxfs_path, ((nxfs->nxfs_path[1] && nx->nx_path[0]) ? "/" : ""),
2769 nx->nx_path);
2770
2771 error = copyout(&upath_rec, oldp + pos, sizeof(struct nfs_user_stat_path_rec));
2772 if (error) {
2773 /* punt */
2774 goto ustat_done;
2775 }
2776
2777 pos += sizeof(struct nfs_user_stat_path_rec);
2778 bytes_avail -= sizeof(struct nfs_user_stat_path_rec);
2779 recs_copied++;
2780 }
2781 else {
2782 /* Caller's buffer is exhausted */
2783 bytes_avail = 0;
2784 }
2785
2786 bytes_total += sizeof(struct nfs_user_stat_path_rec);
2787
2788 /* Scan through all user nodes of this export */
2789 ulist = &nx->nx_user_list;
2790 lck_mtx_lock(&ulist->user_mutex);
2791 for (unode = TAILQ_FIRST(&ulist->user_lru); unode; unode = unode_next) {
2792 unode_next = TAILQ_NEXT(unode, lru_link);
2793
2794 /* copy out node if there is space */
2795 if (bytes_avail >= sizeof(struct nfs_user_stat_user_rec)) {
2796 /* prepare a user stat rec for copying out */
2797 ustat_rec.uid = unode->uid;
2798 bcopy(&unode->sock, &ustat_rec.sock, unode->sock.ss_len);
2799 ustat_rec.ops = unode->ops;
2800 ustat_rec.bytes_read = unode->bytes_read;
2801 ustat_rec.bytes_written = unode->bytes_written;
2802 ustat_rec.tm_start = unode->tm_start;
2803 ustat_rec.tm_last = unode->tm_last;
2804
2805 error = copyout(&ustat_rec, oldp + pos, sizeof(struct nfs_user_stat_user_rec));
2806
2807 if (error) {
2808 /* punt */
2809 lck_mtx_unlock(&ulist->user_mutex);
2810 goto ustat_done;
2811 }
2812
2813 pos += sizeof(struct nfs_user_stat_user_rec);
2814 bytes_avail -= sizeof(struct nfs_user_stat_user_rec);
2815 recs_copied++;
2816 }
2817 else {
2818 /* Caller's buffer is exhausted */
2819 bytes_avail = 0;
2820 }
2821 bytes_total += sizeof(struct nfs_user_stat_user_rec);
2822 }
2823 /* can unlock this export's list now */
2824 lck_mtx_unlock(&ulist->user_mutex);
2825 }
2826 }
2827
2828ustat_done:
2829 /* unlock the export table */
2830 lck_rw_done(&nfsrv_export_rwlock);
2831
2832ustat_skip:
2833 /* indicate number of actual records copied */
2834 ustat_desc.rec_count = recs_copied;
2835
2836 if (!error) {
2837 /* check if there was enough room for the buffer descriptor */
2838 if (*oldlenp >= sizeof(struct nfs_user_stat_desc))
2839 error = copyout(&ustat_desc, oldp, sizeof(struct nfs_user_stat_desc));
2840 else
2841 error = ENOMEM;
2842
2843 /* always indicate required buffer size */
2844 *oldlenp = bytes_total;
2845 }
2846 break;
2847 case NFS_USERCOUNT:
2848 if (!oldp) {
2849 *oldlenp = sizeof(nfsrv_user_stat_node_count);
2850 return (0);
2851 }
2852
2853 if (*oldlenp < sizeof(nfsrv_user_stat_node_count)) {
2854 *oldlenp = sizeof(nfsrv_user_stat_node_count);
2855 return (ENOMEM);
1c79356b 2856 }
2d21ac55
A
2857
2858 if (nfsrv_is_initialized()) {
2859 /* reclaim old expired user nodes */
2860 nfsrv_active_user_list_reclaim();
2861 }
2862
2863 error = copyout(&nfsrv_user_stat_node_count, oldp, sizeof(nfsrv_user_stat_node_count));
2864 break;
2865#endif /* NFSSERVER */
e5568f75 2866 case VFS_CTL_NOLOCKS:
91447636 2867 if (req->oldptr != USER_ADDR_NULL) {
2d21ac55
A
2868 lck_mtx_lock(&nmp->nm_lock);
2869 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
2870 lck_mtx_unlock(&nmp->nm_lock);
e5568f75
A
2871 error = SYSCTL_OUT(req, &val, sizeof(val));
2872 if (error)
2873 return (error);
2874 }
91447636 2875 if (req->newptr != USER_ADDR_NULL) {
e5568f75
A
2876 error = SYSCTL_IN(req, &val, sizeof(val));
2877 if (error)
2878 return (error);
2d21ac55
A
2879 lck_mtx_lock(&nmp->nm_lock);
2880 if (nmp->nm_flag & NFSMNT_LOCALLOCKS) {
2881 /* can't toggle locks when using local locks */
2882 error = EINVAL;
2883 } else if (val) {
2884 if (!(nmp->nm_flag & NFSMNT_NOLOCKS))
2885 nfs_lockd_mount_change(-1);
e5568f75 2886 nmp->nm_flag |= NFSMNT_NOLOCKS;
2d21ac55
A
2887 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
2888 } else {
2889 if (nmp->nm_flag & NFSMNT_NOLOCKS)
2890 nfs_lockd_mount_change(1);
e5568f75 2891 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
2d21ac55
A
2892 }
2893 lck_mtx_unlock(&nmp->nm_lock);
e5568f75
A
2894 }
2895 break;
55e303ae 2896 case VFS_CTL_QUERY:
2d21ac55 2897 lck_mtx_lock(&nmp->nm_lock);
cf7d32b8
A
2898 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
2899 softnobrowse = ((nmp->nm_flag & NFSMNT_SOFT) && (vfs_flags(nmp->nm_mountp) & MNT_DONTBROWSE));
b0d623f7
A
2900 if (!softnobrowse && (nmp->nm_state & NFSSTA_TIMEO))
2901 vq.vq_flags |= VQ_NOTRESP;
2902 if (!softnobrowse && (nmp->nm_state & NFSSTA_JUKEBOXTIMEO) &&
2903 !(nmp->nm_flag & NFSMNT_MUTEJUKEBOX))
55e303ae 2904 vq.vq_flags |= VQ_NOTRESP;
b0d623f7
A
2905 if (!softnobrowse && (nmp->nm_state & NFSSTA_LOCKTIMEO) &&
2906 !(nmp->nm_flag & (NFSMNT_NOLOCKS|NFSMNT_LOCALLOCKS)))
2d21ac55 2907 vq.vq_flags |= VQ_NOTRESP;
b0d623f7
A
2908 if (nmp->nm_state & NFSSTA_DEAD)
2909 vq.vq_flags |= VQ_DEAD;
2d21ac55 2910 lck_mtx_unlock(&nmp->nm_lock);
55e303ae
A
2911 error = SYSCTL_OUT(req, &vq, sizeof(vq));
2912 break;
2913 case VFS_CTL_TIMEO:
91447636 2914 if (req->oldptr != USER_ADDR_NULL) {
2d21ac55
A
2915 lck_mtx_lock(&nmp->nm_lock);
2916 val = nmp->nm_tprintf_initial_delay;
2917 lck_mtx_unlock(&nmp->nm_lock);
2918 error = SYSCTL_OUT(req, &val, sizeof(val));
55e303ae
A
2919 if (error)
2920 return (error);
2921 }
91447636 2922 if (req->newptr != USER_ADDR_NULL) {
2d21ac55 2923 error = SYSCTL_IN(req, &val, sizeof(val));
55e303ae
A
2924 if (error)
2925 return (error);
2d21ac55
A
2926 lck_mtx_lock(&nmp->nm_lock);
2927 if (val < 0)
55e303ae 2928 nmp->nm_tprintf_initial_delay = 0;
2d21ac55
A
2929 else
2930 nmp->nm_tprintf_initial_delay = val;
2931 lck_mtx_unlock(&nmp->nm_lock);
55e303ae
A
2932 }
2933 break;
1c79356b 2934 default:
55e303ae 2935 return (ENOTSUP);
1c79356b 2936 }
55e303ae 2937 return (error);
1c79356b 2938}