]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/nfs/nfs_vfsops.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
65 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
66 */
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/conf.h>
71#include <sys/ioctl.h>
72#include <sys/signal.h>
73#include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
74#include <sys/kauth.h>
75#include <sys/vnode_internal.h>
76#include <sys/malloc.h>
77#include <sys/kernel.h>
78#include <sys/sysctl.h>
79#include <sys/mount_internal.h>
80#include <sys/kpi_mbuf.h>
81#include <sys/socket.h>
82#include <sys/socketvar.h>
83#include <sys/fcntl.h>
84#include <libkern/OSAtomic.h>
85
86#include <sys/vm.h>
87#include <sys/vmparam.h>
88
89#if !defined(NO_MOUNT_PRIVATE)
90#include <sys/filedesc.h>
91#endif /* NO_MOUNT_PRIVATE */
92
93#include <net/if.h>
94#include <net/route.h>
95#include <netinet/in.h>
96
97#include <nfs/rpcv2.h>
98#include <nfs/nfsproto.h>
99#include <nfs/nfs.h>
100#include <nfs/nfsnode.h>
101#include <nfs/nfsmount.h>
102#include <nfs/xdr_subs.h>
103#include <nfs/nfsm_subs.h>
104#include <nfs/nfsdiskless.h>
105#include <nfs/nfs_lock.h>
106
107extern int nfs_mountroot(void);
108
109extern int nfs_ticks;
110extern int nfs_mount_type;
111extern int nfs_resv_mounts;
112
113struct nfsstats nfsstats;
114static int nfs_sysctl(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t);
115/* XXX CSM 11/25/97 Upgrade sysctl.h someday */
116#ifdef notyet
117SYSCTL_NODE(_vfs, MOUNT_NFS, nfs, CTLFLAG_RW, 0, "NFS filesystem");
118SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
119 &nfsstats, nfsstats, "");
120#endif
121
122SYSCTL_DECL(_vfs_generic_nfs);
123SYSCTL_NODE(_vfs_generic_nfs, OID_AUTO, client, CTLFLAG_RW, 0,
124 "nfs client hinge");
125/* how long NFS will wait before signalling vfs that it's down. */
126static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
127SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_INITIAL_DELAY,
128 initialdowndelay, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
129/* how long between console messages "nfs server foo not responding" */
130static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
131SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_DELAY,
132 nextdowndelay, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
133
134static int nfs_biosize(struct nfsmount *);
135static int mountnfs(struct user_nfs_args *,mount_t,mbuf_t,proc_t,vnode_t *);
136static int nfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t context);
137static int nfs_start(mount_t mp, int flags, vfs_context_t context);
138static int nfs_unmount(mount_t mp, int mntflags, vfs_context_t context);
139static int nfs_root(mount_t mp, vnode_t *vpp, vfs_context_t context);
140static int nfs_statfs(mount_t mp, struct vfsstatfs *sbp, vfs_context_t context);
141static int nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context);
142static int nfs_sync( mount_t mp, int waitfor, vfs_context_t context);
143static int nfs_vptofh(vnode_t vp, int *fhlenp, unsigned char *fhp, vfs_context_t context);
144static int nfs_fhtovp(mount_t mp, int fhlen, unsigned char *fhp, vnode_t *vpp, vfs_context_t context);
145static int nfs_vget(mount_t , ino64_t, vnode_t *, vfs_context_t context);
146
147
148/*
149 * nfs vfs operations.
150 */
151struct vfsops nfs_vfsops = {
152 nfs_mount,
153 nfs_start,
154 nfs_unmount,
155 nfs_root,
156 NULL, /* quotactl */
157 nfs_vfs_getattr,
158 nfs_sync,
159 nfs_vget,
160 nfs_fhtovp,
161 nfs_vptofh,
162 nfs_init,
163 nfs_sysctl,
164 NULL /* setattr */
165};
166
167
168static int
169nfs_mount_diskless(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *);
170#if !defined(NO_MOUNT_PRIVATE)
171static int
172nfs_mount_diskless_private(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *);
173#endif /* NO_MOUNT_PRIVATE */
174
175static int
176nfs_biosize(struct nfsmount *nmp)
177{
178 int iosize;
179
180 /*
181 * Calculate the size used for io buffers. Use the larger
182 * of the two sizes to minimise nfs requests but make sure
183 * that it is at least one VM page to avoid wasting buffer
184 * space and to allow easy mmapping of I/O buffers.
185 * The read/write rpc calls handle the splitting up of
186 * buffers into multiple requests if the buffer size is
187 * larger than the I/O size.
188 */
189 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
190 if (iosize < PAGE_SIZE)
191 iosize = PAGE_SIZE;
192 return (trunc_page_32(iosize));
193}
194
195/*
196 * nfs statfs call
197 */
198int
199nfs_statfs(mount_t mp, struct vfsstatfs *sbp, vfs_context_t context)
200{
201 proc_t p = vfs_context_proc(context);
202 vnode_t vp;
203 struct nfs_statfs *sfp;
204 caddr_t cp;
205 u_long *tl;
206 long t1, t2;
207 caddr_t bpos, dpos, cp2;
208 struct nfsmount *nmp = VFSTONFS(mp);
209 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
210 mbuf_t mreq, mrep, md, mb, mb2;
211 u_int64_t xid;
212 kauth_cred_t cred;
213 struct ucred temp_cred;
214
215#ifndef nolint
216 sfp = (struct nfs_statfs *)0;
217#endif
218 vp = nmp->nm_dvp;
219 if ((error = vnode_get(vp)))
220 return(error);
221
222 bzero(&temp_cred, sizeof(temp_cred));
223 temp_cred.cr_ngroups = 1;
224 cred = kauth_cred_create(&temp_cred);
225
226 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
227 nfs_fsinfo(nmp, vp, cred, p);
228 nfsm_reqhead(NFSX_FH(v3));
229 if (error) {
230 kauth_cred_unref(&cred);
231 vnode_put(vp);
232 return (error);
233 }
234 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_FSSTAT]);
235 nfsm_fhtom(vp, v3);
236 nfsm_request(vp, NFSPROC_FSSTAT, p, cred, &xid);
237 if (v3 && mrep)
238 nfsm_postop_attr_update(vp, v3, retattr, &xid);
239 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
240
241 sbp->f_flags = nmp->nm_flag;
242 sbp->f_iosize = NFS_IOSIZE;
243 if (v3) {
244 /*
245 * Adjust block size to get total block count to fit in a long.
246 * If we can't increase block size enough, clamp to max long.
247 */
248 u_quad_t tquad, tquad2, bsize;
249 bsize = NFS_FABLKSIZE;
250
251 fxdr_hyper(&sfp->sf_tbytes, &tquad);
252 tquad /= bsize;
253 while ((tquad & ~0x7fffffff) && (bsize < 0x40000000)) {
254 bsize <<= 1;
255 tquad >>= 1;
256 }
257 sbp->f_blocks = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
258
259 fxdr_hyper(&sfp->sf_fbytes, &tquad);
260 tquad /= bsize;
261 sbp->f_bfree = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
262
263 fxdr_hyper(&sfp->sf_abytes, &tquad);
264 tquad /= bsize;
265 sbp->f_bavail = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
266
267 sbp->f_bsize = (long)bsize;
268
269 /* adjust file slots too... */
270 fxdr_hyper(&sfp->sf_tfiles, &tquad);
271 fxdr_hyper(&sfp->sf_ffiles, &tquad2);
272 while (tquad & ~0x7fffffff) {
273 tquad >>= 1;
274 tquad2 >>= 1;
275 }
276 sbp->f_files = tquad;
277 sbp->f_ffree = tquad2;
278 } else {
279 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
280 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
281 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
282 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
283 sbp->f_files = 0;
284 sbp->f_ffree = 0;
285 }
286 nfsm_reqdone;
287 kauth_cred_unref(&cred);
288 vnode_put(vp);
289 return (error);
290}
291
292/*
293 * The nfs_statfs code is complicated, and used by mountnfs(), so leave it as-is
294 * and handle VFS_GETATTR by calling nfs_statfs and copying fields.
295 */
296static int
297nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
298{
299 int error = 0;
300
301 if (VFSATTR_IS_ACTIVE(fsap, f_bsize) ||
302 VFSATTR_IS_ACTIVE(fsap, f_iosize) ||
303 VFSATTR_IS_ACTIVE(fsap, f_blocks) ||
304 VFSATTR_IS_ACTIVE(fsap, f_bfree) ||
305 VFSATTR_IS_ACTIVE(fsap, f_bavail) ||
306 VFSATTR_IS_ACTIVE(fsap, f_bused) ||
307 VFSATTR_IS_ACTIVE(fsap, f_files) ||
308 VFSATTR_IS_ACTIVE(fsap, f_ffree)) {
309 struct vfsstatfs sb;
310
311 error = nfs_statfs(mp, &sb, context);
312 if (!error) {
313 VFSATTR_RETURN(fsap, f_bsize, sb.f_bsize);
314 VFSATTR_RETURN(fsap, f_iosize, sb.f_iosize);
315 VFSATTR_RETURN(fsap, f_blocks, sb.f_blocks);
316 VFSATTR_RETURN(fsap, f_bfree, sb.f_bfree);
317 VFSATTR_RETURN(fsap, f_bavail, sb.f_bavail);
318 VFSATTR_RETURN(fsap, f_bused, sb.f_blocks - sb.f_bfree);
319 VFSATTR_RETURN(fsap, f_files, sb.f_files);
320 VFSATTR_RETURN(fsap, f_ffree, sb.f_ffree);
321 }
322 }
323
324 if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
325 struct nfsmount *nmp;
326 struct nfsv3_pathconf pc;
327 u_int32_t caps, valid;
328 vnode_t vp;
329 int v3;
330
331 if (!(nmp = VFSTONFS(mp)))
332 return (ENXIO);
333 vp = nmp->nm_dvp;
334 v3 = (nmp->nm_flag & NFSMNT_NFSV3);
335
336 /*
337 * The capabilities[] array defines what this volume supports.
338 *
339 * The valid[] array defines which bits this code understands
340 * the meaning of (whether the volume has that capability or not).
341 * Any zero bits here means "I don't know what you're asking about"
342 * and the caller cannot tell whether that capability is
343 * present or not.
344 */
345 caps = valid = 0;
346 if (v3) {
347 /* try to get fsinfo if we haven't already */
348 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
349 nfs_fsinfo(nmp, vp, vfs_context_ucred(context),
350 vfs_context_proc(context));
351 if (!(nmp = VFSTONFS(vnode_mount(vp))))
352 return (ENXIO);
353 }
354 if (nmp->nm_state & NFSSTA_GOTFSINFO) {
355 /* fsinfo indicates (non)support of links and symlinks */
356 valid |= VOL_CAP_FMT_SYMBOLICLINKS |
357 VOL_CAP_FMT_HARDLINKS;
358 if (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_SYMLINK)
359 caps |= VOL_CAP_FMT_SYMBOLICLINKS;
360 if (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_LINK)
361 caps |= VOL_CAP_FMT_HARDLINKS;
362 /* if fsinfo indicates all pathconf info is the same, */
363 /* we can use it to report case attributes */
364 if ((nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS) &&
365 !(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
366 /* no cached pathconf info, try to get now */
367 error = nfs_pathconfrpc(vp, &pc,
368 vfs_context_ucred(context),
369 vfs_context_proc(context));
370 if (!(nmp = VFSTONFS(vnode_mount(vp))))
371 return (ENXIO);
372 if (!error) {
373 /* all files have the same pathconf info, */
374 /* so cache a copy of the results */
375 nfs_pathconf_cache(nmp, &pc);
376 }
377 }
378 if (nmp->nm_state & NFSSTA_GOTPATHCONF) {
379 valid |= VOL_CAP_FMT_CASE_SENSITIVE |
380 VOL_CAP_FMT_CASE_PRESERVING;
381 if (!(nmp->nm_fsinfo.pcflags &
382 NFSPCINFO_CASE_INSENSITIVE))
383 caps |= VOL_CAP_FMT_CASE_SENSITIVE;
384 if (nmp->nm_fsinfo.pcflags &
385 NFSPCINFO_CASE_PRESERVING)
386 caps |= VOL_CAP_FMT_CASE_PRESERVING;
387 }
388 /* Is server's max file size at least 2TB? */
389 if (nmp->nm_fsinfo.maxfilesize >= 0x20000000000ULL)
390 caps |= VOL_CAP_FMT_2TB_FILESIZE;
391 } else {
392 /*
393 * NFSv3 supports 64 bits of file size.
394 * Without FSINFO from the server, we'll
395 * just assume maxfilesize >= 2TB
396 */
397 caps |= VOL_CAP_FMT_2TB_FILESIZE;
398 }
399 }
400 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
401 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
402 // VOL_CAP_FMT_SYMBOLICLINKS |
403 // VOL_CAP_FMT_HARDLINKS |
404 // VOL_CAP_FMT_JOURNAL |
405 // VOL_CAP_FMT_JOURNAL_ACTIVE |
406 // VOL_CAP_FMT_NO_ROOT_TIMES |
407 // VOL_CAP_FMT_SPARSE_FILES |
408 // VOL_CAP_FMT_ZERO_RUNS |
409 // VOL_CAP_FMT_CASE_SENSITIVE |
410 // VOL_CAP_FMT_CASE_PRESERVING |
411 // VOL_CAP_FMT_FAST_STATFS |
412 // VOL_CAP_FMT_2TB_FILESIZE |
413 caps;
414 fsap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
415 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
416 // VOL_CAP_FMT_SYMBOLICLINKS |
417 // VOL_CAP_FMT_HARDLINKS |
418 // VOL_CAP_FMT_JOURNAL |
419 // VOL_CAP_FMT_JOURNAL_ACTIVE |
420 // VOL_CAP_FMT_NO_ROOT_TIMES |
421 // VOL_CAP_FMT_SPARSE_FILES |
422 // VOL_CAP_FMT_ZERO_RUNS |
423 // VOL_CAP_FMT_CASE_SENSITIVE |
424 // VOL_CAP_FMT_CASE_PRESERVING |
425 VOL_CAP_FMT_FAST_STATFS |
426 VOL_CAP_FMT_2TB_FILESIZE |
427 valid;
428
429 /*
430 * We don't support most of the interfaces.
431 *
432 * We MAY support locking, but we don't have any easy way of probing.
433 * We can tell if there's no lockd running or if locks have been
434 * disabled for a mount, so we can definitely answer NO in that case.
435 * Any attempt to send a request to lockd to test for locking support
436 * may cause the lazily-launched locking daemons to be started
437 * unnecessarily. So we avoid that. However, we do record if we ever
438 * successfully perform a lock operation on a mount point, so if it
439 * looks like lock ops have worked, we do report that we support them.
440 */
441 caps = valid = 0;
442 if ((!nfslockdvnode && !nfslockdwaiting) ||
443 (nmp->nm_flag & NFSMNT_NOLOCKS)) {
444 /* locks disabled on this mount, so they definitely won't work */
445 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
446 } else if (nmp->nm_state & NFSSTA_LOCKSWORK) {
447 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
448 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
449 }
450 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
451 // VOL_CAP_INT_SEARCHFS |
452 // VOL_CAP_INT_ATTRLIST |
453 // VOL_CAP_INT_NFSEXPORT |
454 // VOL_CAP_INT_READDIRATTR |
455 // VOL_CAP_INT_EXCHANGEDATA |
456 // VOL_CAP_INT_COPYFILE |
457 // VOL_CAP_INT_ALLOCATE |
458 // VOL_CAP_INT_VOL_RENAME |
459 // VOL_CAP_INT_ADVLOCK |
460 // VOL_CAP_INT_FLOCK |
461 // VOL_CAP_INT_EXTENDED_SECURITY |
462 // VOL_CAP_INT_USERACCESS |
463 caps;
464 fsap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
465 VOL_CAP_INT_SEARCHFS |
466 VOL_CAP_INT_ATTRLIST |
467 VOL_CAP_INT_NFSEXPORT |
468 VOL_CAP_INT_READDIRATTR |
469 VOL_CAP_INT_EXCHANGEDATA |
470 VOL_CAP_INT_COPYFILE |
471 VOL_CAP_INT_ALLOCATE |
472 VOL_CAP_INT_VOL_RENAME |
473 // VOL_CAP_INT_ADVLOCK |
474 // VOL_CAP_INT_FLOCK |
475 // VOL_CAP_INT_EXTENDED_SECURITY |
476 // VOL_CAP_INT_USERACCESS |
477 valid;
478
479 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
480 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
481
482 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
483 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
484
485 VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
486 }
487
488 if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
489 fsap->f_attributes.validattr.commonattr = 0;
490 fsap->f_attributes.validattr.volattr =
491 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
492 fsap->f_attributes.validattr.dirattr = 0;
493 fsap->f_attributes.validattr.fileattr = 0;
494 fsap->f_attributes.validattr.forkattr = 0;
495
496 fsap->f_attributes.nativeattr.commonattr = 0;
497 fsap->f_attributes.nativeattr.volattr =
498 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
499 fsap->f_attributes.nativeattr.dirattr = 0;
500 fsap->f_attributes.nativeattr.fileattr = 0;
501 fsap->f_attributes.nativeattr.forkattr = 0;
502
503 VFSATTR_SET_SUPPORTED(fsap, f_attributes);
504 }
505
506 return (error);
507}
508
509/*
510 * nfs version 3 fsinfo rpc call
511 */
512int
513nfs_fsinfo(nmp, vp, cred, p)
514 struct nfsmount *nmp;
515 vnode_t vp;
516 kauth_cred_t cred;
517 proc_t p;
518{
519 struct nfsv3_fsinfo *fsp;
520 caddr_t cp;
521 long t1, t2;
522 u_long *tl;
523 int prefsize, maxsize;
524 caddr_t bpos, dpos, cp2;
525 int error = 0, retattr;
526 mbuf_t mreq, mrep, md, mb, mb2;
527 u_int64_t xid;
528
529 nfsm_reqhead(NFSX_FH(1));
530 if (error)
531 return (error);
532 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_FSINFO]);
533 nfsm_fhtom(vp, 1);
534 nfsm_request(vp, NFSPROC_FSINFO, p, cred, &xid);
535 if (mrep) {
536 nfsm_postop_attr_update(vp, 1, retattr, &xid);
537 }
538 if (!error) {
539 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
540 prefsize = fxdr_unsigned(u_long, fsp->fs_wtpref);
541 if (prefsize < nmp->nm_wsize)
542 nmp->nm_wsize = (prefsize + NFS_FABLKSIZE - 1) &
543 ~(NFS_FABLKSIZE - 1);
544 maxsize = fxdr_unsigned(u_long, fsp->fs_wtmax);
545 if (maxsize < nmp->nm_wsize) {
546 nmp->nm_wsize = maxsize & ~(NFS_FABLKSIZE - 1);
547 if (nmp->nm_wsize == 0)
548 nmp->nm_wsize = maxsize;
549 }
550 prefsize = fxdr_unsigned(u_long, fsp->fs_rtpref);
551 if (prefsize < nmp->nm_rsize)
552 nmp->nm_rsize = (prefsize + NFS_FABLKSIZE - 1) &
553 ~(NFS_FABLKSIZE - 1);
554 maxsize = fxdr_unsigned(u_long, fsp->fs_rtmax);
555 if (maxsize < nmp->nm_rsize) {
556 nmp->nm_rsize = maxsize & ~(NFS_FABLKSIZE - 1);
557 if (nmp->nm_rsize == 0)
558 nmp->nm_rsize = maxsize;
559 }
560 prefsize = fxdr_unsigned(u_long, fsp->fs_dtpref);
561 if (prefsize < nmp->nm_readdirsize)
562 nmp->nm_readdirsize = prefsize;
563 if (maxsize < nmp->nm_readdirsize) {
564 nmp->nm_readdirsize = maxsize;
565 }
566 fxdr_hyper(&fsp->fs_maxfilesize, &nmp->nm_fsinfo.maxfilesize);
567 nmp->nm_fsinfo.fsproperties = fxdr_unsigned(u_long, fsp->fs_properties);
568 nmp->nm_state |= NFSSTA_GOTFSINFO;
569 }
570 nfsm_reqdone;
571 return (error);
572}
573
574/*
575 * Mount a remote root fs via. nfs. This depends on the info in the
576 * nfs_diskless structure that has been filled in properly by some primary
577 * bootstrap.
578 * It goes something like this:
579 * - do enough of "ifconfig" by calling ifioctl() so that the system
580 * can talk to the server
581 * - If nfs_diskless.mygateway is filled in, use that address as
582 * a default gateway.
583 * - hand craft the swap nfs vnode hanging off a fake mount point
584 * if swdevt[0].sw_dev == NODEV
585 * - build the rootfs mount point and call mountnfs() to do the rest.
586 */
587int
588nfs_mountroot()
589{
590 struct nfs_diskless nd;
591 struct nfs_vattr nvattr;
592 mount_t mp;
593 vnode_t vp;
594 proc_t procp;
595 int error;
596#if !defined(NO_MOUNT_PRIVATE)
597 mount_t mppriv;
598 vnode_t vppriv;
599#endif /* NO_MOUNT_PRIVATE */
600 int v3, sotype;
601
602 procp = current_proc(); /* XXX */
603
604 /*
605 * Call nfs_boot_init() to fill in the nfs_diskless struct.
606 * Note: networking must already have been configured before
607 * we're called.
608 */
609 bzero((caddr_t) &nd, sizeof(nd));
610 error = nfs_boot_init(&nd, procp);
611 if (error) {
612 panic("nfs_boot_init failed with %d\n", error);
613 }
614
615 /*
616 * Try NFSv3 first, then fallback to NFSv2.
617 * Likewise, try TCP first, then fall back to UDP.
618 */
619 v3 = 1;
620 sotype = SOCK_STREAM;
621
622tryagain:
623 error = nfs_boot_getfh(&nd, procp, v3, sotype);
624 if (error) {
625 if (error == EHOSTDOWN || error == EHOSTUNREACH) {
626 if (nd.nd_root.ndm_path)
627 FREE_ZONE(nd.nd_root.ndm_path,
628 MAXPATHLEN, M_NAMEI);
629 if (nd.nd_private.ndm_path)
630 FREE_ZONE(nd.nd_private.ndm_path,
631 MAXPATHLEN, M_NAMEI);
632 return (error);
633 }
634 if (v3) {
635 if (sotype == SOCK_STREAM) {
636 printf("nfs_boot_getfh(v3,TCP) failed with %d, trying UDP...\n", error);
637 sotype = SOCK_DGRAM;
638 goto tryagain;
639 }
640 printf("nfs_boot_getfh(v3,UDP) failed with %d, trying v2...\n", error);
641 v3 = 0;
642 sotype = SOCK_STREAM;
643 goto tryagain;
644 } else if (sotype == SOCK_STREAM) {
645 printf("nfs_boot_getfh(v2,TCP) failed with %d, trying UDP...\n", error);
646 sotype = SOCK_DGRAM;
647 goto tryagain;
648 }
649 panic("nfs_boot_getfh(v2,UDP) failed with %d\n", error);
650 }
651
652 /*
653 * Create the root mount point.
654 */
655#if !defined(NO_MOUNT_PRIVATE)
656 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_RDONLY|MNT_ROOTFS, &vp, &mp)))
657#else
658 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_ROOTFS, &vp, &mp)))
659#endif /* NO_MOUNT_PRIVATE */
660 {
661 if (v3) {
662 if (sotype == SOCK_STREAM) {
663 printf("nfs_mount_diskless(v3,TCP) failed with %d, trying UDP...\n", error);
664 sotype = SOCK_DGRAM;
665 goto tryagain;
666 }
667 printf("nfs_mount_diskless(v3,UDP) failed with %d, trying v2...\n", error);
668 v3 = 0;
669 sotype = SOCK_STREAM;
670 goto tryagain;
671 } else if (sotype == SOCK_STREAM) {
672 printf("nfs_mount_diskless(v2,TCP) failed with %d, trying UDP...\n", error);
673 sotype = SOCK_DGRAM;
674 goto tryagain;
675 }
676 panic("nfs_mount_diskless(v2,UDP) root failed with %d\n", error);
677 }
678 printf("root on %s\n", (char *)&nd.nd_root.ndm_host);
679
680 vfs_unbusy(mp);
681 mount_list_add(mp);
682 rootvp = vp;
683
684#if !defined(NO_MOUNT_PRIVATE)
685 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
686 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
687 0, &vppriv, &mppriv);
688 if (error) {
689 panic("nfs_mount_diskless private failed with %d\n", error);
690 }
691 printf("private on %s\n", (char *)&nd.nd_private.ndm_host);
692
693 vfs_unbusy(mppriv);
694 mount_list_add(mppriv);
695 }
696
697#endif /* NO_MOUNT_PRIVATE */
698
699 if (nd.nd_root.ndm_path)
700 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
701 if (nd.nd_private.ndm_path)
702 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
703
704 /* Get root attributes (for the time). */
705 error = nfs_getattr(vp, &nvattr, kauth_cred_get(), procp);
706 if (error) panic("nfs_mountroot: getattr for root");
707 return (0);
708}
709
710/*
711 * Internal version of mount system call for diskless setup.
712 */
713static int
714nfs_mount_diskless(
715 struct nfs_dlmount *ndmntp,
716 const char *mntname,
717 int mntflag,
718 vnode_t *vpp,
719 mount_t *mpp)
720{
721 struct user_nfs_args args;
722 mount_t mp;
723 mbuf_t m;
724 int error;
725 proc_t procp;
726
727 procp = current_proc(); /* XXX */
728
729 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_host, &mp))) {
730 printf("nfs_mount_diskless: NFS not configured");
731 return (error);
732 }
733
734 mp->mnt_flag |= mntflag;
735 if (!(mntflag & MNT_RDONLY))
736 mp->mnt_flag &= ~MNT_RDONLY;
737
738 /* Initialize mount args. */
739 bzero((caddr_t) &args, sizeof(args));
740 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
741 args.addrlen = ndmntp->ndm_saddr.sin_len;
742 args.sotype = ndmntp->ndm_sotype;
743 args.fh = CAST_USER_ADDR_T(&ndmntp->ndm_fh[0]);
744 args.fhsize = ndmntp->ndm_fhlen;
745 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_host);
746 args.flags = NFSMNT_RESVPORT;
747 if (ndmntp->ndm_nfsv3)
748 args.flags |= NFSMNT_NFSV3;
749
750 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
751 if (error) {
752 printf("nfs_mount_diskless: mbuf_get(soname) failed");
753 return (error);
754 }
755 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
756 bcopy((caddr_t)args.addr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
757 if ((error = mountnfs(&args, mp, m, procp, vpp))) {
758 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
759 // XXX vfs_rootmountfailed(mp);
760 mount_list_lock();
761 mp->mnt_vtable->vfc_refcount--;
762 mount_list_unlock();
763 vfs_unbusy(mp);
764 mount_lock_destroy(mp);
765 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
766 return (error);
767 }
768 *mpp = mp;
769 return (0);
770}
771
772#if !defined(NO_MOUNT_PRIVATE)
773/*
774 * Internal version of mount system call to mount "/private"
775 * separately in diskless setup
776 */
777static int
778nfs_mount_diskless_private(
779 struct nfs_dlmount *ndmntp,
780 const char *mntname,
781 int mntflag,
782 vnode_t *vpp,
783 mount_t *mpp)
784{
785 struct user_nfs_args args;
786 mount_t mp;
787 mbuf_t m;
788 int error;
789 proc_t procp;
790 struct vfstable *vfsp;
791 struct nameidata nd;
792 vnode_t vp;
793 struct vfs_context context;
794
795 procp = current_proc(); /* XXX */
796 context.vc_proc = procp;
797 context.vc_ucred = kauth_cred_get();
798
799 {
800 /*
801 * mimic main()!. Temporarily set up rootvnode and other stuff so
802 * that namei works. Need to undo this because main() does it, too
803 */
804 struct filedesc *fdp; /* pointer to file descriptor state */
805 fdp = procp->p_fd;
806 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
807
808 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
809 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, NULL))
810 panic("cannot find root vnode");
811 error = vnode_ref(rootvnode);
812 if (error) {
813 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
814 return (error);
815 }
816 fdp->fd_cdir = rootvnode;
817 fdp->fd_rdir = NULL;
818 }
819
820 /*
821 * Get vnode to be covered
822 */
823 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
824 mntname, &context);
825 if ((error = namei(&nd))) {
826 printf("nfs_mountroot: private namei failed!\n");
827 return (error);
828 }
829 {
830 /* undo vnode_ref() in mimic main()! */
831 vnode_rele(rootvnode);
832 }
833 nameidone(&nd);
834 vp = nd.ni_vp;
835
836 if ((error = VNOP_FSYNC(vp, MNT_WAIT, &context)) ||
837 (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
838 vnode_put(vp);
839 return (error);
840 }
841 if (vnode_vtype(vp) != VDIR) {
842 vnode_put(vp);
843 return (ENOTDIR);
844 }
845 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
846 if (!strcmp(vfsp->vfc_name, "nfs"))
847 break;
848 if (vfsp == NULL) {
849 printf("nfs_mountroot: private NFS not configured\n");
850 vnode_put(vp);
851 return (ENODEV);
852 }
853 if (vnode_mountedhere(vp) != NULL) {
854 vnode_put(vp);
855 return (EBUSY);
856 }
857
858 /*
859 * Allocate and initialize the filesystem.
860 */
861 mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
862 if (!mp) {
863 printf("nfs_mountroot: unable to allocate mount structure\n");
864 vnode_put(vp);
865 return (ENOMEM);
866 }
867 bzero((char *)mp, (u_long)sizeof(struct mount));
868
869 /* Initialize the default IO constraints */
870 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
871 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
872
873 mount_lock_init(mp);
874 TAILQ_INIT(&mp->mnt_vnodelist);
875 TAILQ_INIT(&mp->mnt_workerqueue);
876 TAILQ_INIT(&mp->mnt_newvnodes);
877 (void)vfs_busy(mp, LK_NOWAIT);
878 TAILQ_INIT(&mp->mnt_vnodelist);
879 mount_list_lock();
880 vfsp->vfc_refcount++;
881 mount_list_unlock();
882 mp->mnt_vtable = vfsp;
883 mp->mnt_op = vfsp->vfc_vfsops;
884 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
885 mp->mnt_flag = mntflag;
886 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
887 strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSNAMELEN-1);
888 vp->v_mountedhere = mp;
889 mp->mnt_vnodecovered = vp;
890 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
891 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MNAMELEN - 1, 0);
892 (void) copystr(ndmntp->ndm_host, mp->mnt_vfsstat.f_mntfromname, MNAMELEN - 1, 0);
893
894 /* Initialize mount args. */
895 bzero((caddr_t) &args, sizeof(args));
896 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
897 args.addrlen = ndmntp->ndm_saddr.sin_len;
898 args.sotype = ndmntp->ndm_sotype;
899 args.fh = CAST_USER_ADDR_T(ndmntp->ndm_fh);
900 args.fhsize = ndmntp->ndm_fhlen;
901 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_host);
902 args.flags = NFSMNT_RESVPORT;
903 if (ndmntp->ndm_nfsv3)
904 args.flags |= NFSMNT_NFSV3;
905
906 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
907 if (error) {
908 printf("nfs_mount_diskless_private: mbuf_get(soname) failed");
909 return (error);
910 }
911 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
912 bcopy((caddr_t)args.addr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
913 if ((error = mountnfs(&args, mp, m, procp, &vp))) {
914 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
915 mount_list_lock();
916 vfsp->vfc_refcount--;
917 mount_list_unlock();
918 vfs_unbusy(mp);
919 mount_lock_destroy(mp);
920 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
921 return (error);
922 }
923
924 *mpp = mp;
925 *vpp = vp;
926 return (0);
927}
928#endif /* NO_MOUNT_PRIVATE */
929
930/*
931 * VFS Operations.
932 *
933 * mount system call
934 */
935static int
936nfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t context)
937{
938 proc_t p = vfs_context_proc(context);
939 int error, argsvers;
940 struct user_nfs_args args;
941 struct nfs_args tempargs;
942 mbuf_t nam;
943 size_t len;
944 u_char nfh[NFSX_V3FHMAX];
945 char *mntfrom;
946
947 error = copyin(data, (caddr_t)&argsvers, sizeof (argsvers));
948 if (error)
949 return (error);
950
951 switch (argsvers) {
952 case 3:
953 if (vfs_context_is64bit(context))
954 error = copyin(data, (caddr_t)&args, sizeof (struct user_nfs_args3));
955 else
956 error = copyin(data, (caddr_t)&tempargs, sizeof (struct nfs_args3));
957 break;
958 case 4:
959 if (vfs_context_is64bit(context))
960 error = copyin(data, (caddr_t)&args, sizeof (args));
961 else
962 error = copyin(data, (caddr_t)&tempargs, sizeof (tempargs));
963 break;
964 default:
965 return (EPROGMISMATCH);
966 }
967 if (error)
968 return (error);
969
970 if (!vfs_context_is64bit(context)) {
971 args.version = tempargs.version;
972 args.addrlen = tempargs.addrlen;
973 args.sotype = tempargs.sotype;
974 args.proto = tempargs.proto;
975 args.fhsize = tempargs.fhsize;
976 args.flags = tempargs.flags;
977 args.wsize = tempargs.wsize;
978 args.rsize = tempargs.rsize;
979 args.readdirsize = tempargs.readdirsize;
980 args.timeo = tempargs.timeo;
981 args.retrans = tempargs.retrans;
982 args.maxgrouplist = tempargs.maxgrouplist;
983 args.readahead = tempargs.readahead;
984 args.leaseterm = tempargs.leaseterm;
985 args.deadthresh = tempargs.deadthresh;
986 args.addr = CAST_USER_ADDR_T(tempargs.addr);
987 args.fh = CAST_USER_ADDR_T(tempargs.fh);
988 args.hostname = CAST_USER_ADDR_T(tempargs.hostname);
989 if (argsvers >= 4) {
990 args.acregmin = tempargs.acregmin;
991 args.acregmax = tempargs.acregmax;
992 args.acdirmin = tempargs.acdirmin;
993 args.acdirmax = tempargs.acdirmax;
994 }
995 }
996
997 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
998 return (EINVAL);
999 error = copyin(args.fh, (caddr_t)nfh, args.fhsize);
1000 if (error)
1001 return (error);
1002
1003 mntfrom = &vfs_statfs(mp)->f_mntfromname[0];
1004 error = copyinstr(args.hostname, mntfrom, MAXPATHLEN-1, &len);
1005 if (error)
1006 return (error);
1007 bzero(&mntfrom[len], MAXPATHLEN - len);
1008
1009 /* sockargs() call must be after above copyin() calls */
1010 error = sockargs(&nam, args.addr, args.addrlen, MBUF_TYPE_SONAME);
1011 if (error)
1012 return (error);
1013
1014 args.fh = CAST_USER_ADDR_T(&nfh[0]);
1015 error = mountnfs(&args, mp, nam, p, &vp);
1016 return (error);
1017}
1018
1019/*
1020 * Common code for mount and mountroot
1021 */
1022static int
1023mountnfs(
1024 struct user_nfs_args *argp,
1025 mount_t mp,
1026 mbuf_t nam,
1027 proc_t p,
1028 vnode_t *vpp)
1029{
1030 struct nfsmount *nmp;
1031 struct nfsnode *np;
1032 int error, maxio;
1033 struct nfs_vattr nvattrs;
1034 struct vfs_context context; /* XXX get from caller? */
1035 u_int64_t xid;
1036
1037 /* up front because of reference */
1038 context.vc_ucred = kauth_cred_proc_ref(p);
1039
1040 /*
1041 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
1042 * no sense in that context.
1043 */
1044 if (argp->sotype == SOCK_STREAM)
1045 argp->flags &= ~NFSMNT_NOCONN;
1046
1047 if (vfs_flags(mp) & MNT_UPDATE) {
1048 nmp = VFSTONFS(mp);
1049 /* update paths, file handles, etc, here XXX */
1050 mbuf_freem(nam);
1051 kauth_cred_unref(&context.vc_ucred);
1052 return (0);
1053 } else {
1054 MALLOC_ZONE(nmp, struct nfsmount *,
1055 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
1056 if (!nmp) {
1057 mbuf_freem(nam);
1058 kauth_cred_unref(&context.vc_ucred);
1059 return (ENOMEM);
1060 }
1061 bzero((caddr_t)nmp, sizeof (struct nfsmount));
1062 TAILQ_INIT(&nmp->nm_uidlruhead);
1063 TAILQ_INIT(&nmp->nm_bufq);
1064 vfs_setfsprivate(mp, nmp);
1065 }
1066
1067 /* setup defaults */
1068 nmp->nm_timeo = NFS_TIMEO;
1069 nmp->nm_retry = NFS_RETRANS;
1070 if (argp->sotype == SOCK_DGRAM) {
1071 nmp->nm_wsize = NFS_DGRAM_WSIZE;
1072 nmp->nm_rsize = NFS_DGRAM_RSIZE;
1073 } else {
1074 nmp->nm_wsize = NFS_WSIZE;
1075 nmp->nm_rsize = NFS_RSIZE;
1076 }
1077 nmp->nm_readdirsize = NFS_READDIRSIZE;
1078 nmp->nm_numgrps = NFS_MAXGRPS;
1079 nmp->nm_readahead = NFS_DEFRAHEAD;
1080 nmp->nm_tprintf_delay = nfs_tprintf_delay;
1081 if (nmp->nm_tprintf_delay < 0)
1082 nmp->nm_tprintf_delay = 0;
1083 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
1084 if (nmp->nm_tprintf_initial_delay < 0)
1085 nmp->nm_tprintf_initial_delay = 0;
1086 nmp->nm_acregmin = NFS_MINATTRTIMO;
1087 nmp->nm_acregmax = NFS_MAXATTRTIMO;
1088 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
1089 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
1090
1091 vfs_getnewfsid(mp);
1092 nmp->nm_mountp = mp;
1093 vfs_setauthopaque(mp);
1094 nmp->nm_flag = argp->flags;
1095 nmp->nm_nam = nam;
1096
1097 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
1098 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
1099 if (nmp->nm_timeo < NFS_MINTIMEO)
1100 nmp->nm_timeo = NFS_MINTIMEO;
1101 else if (nmp->nm_timeo > NFS_MAXTIMEO)
1102 nmp->nm_timeo = NFS_MAXTIMEO;
1103 }
1104
1105 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
1106 nmp->nm_retry = argp->retrans;
1107 if (nmp->nm_retry > NFS_MAXREXMIT)
1108 nmp->nm_retry = NFS_MAXREXMIT;
1109 }
1110
1111 if (argp->flags & NFSMNT_NFSV3) {
1112 if (argp->sotype == SOCK_DGRAM)
1113 maxio = NFS_MAXDGRAMDATA;
1114 else
1115 maxio = NFS_MAXDATA;
1116 } else
1117 maxio = NFS_V2MAXDATA;
1118
1119 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
1120 nmp->nm_wsize = argp->wsize;
1121 /* Round down to multiple of blocksize */
1122 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
1123 if (nmp->nm_wsize <= 0)
1124 nmp->nm_wsize = NFS_FABLKSIZE;
1125 }
1126 if (nmp->nm_wsize > maxio)
1127 nmp->nm_wsize = maxio;
1128 if (nmp->nm_wsize > NFS_MAXBSIZE)
1129 nmp->nm_wsize = NFS_MAXBSIZE;
1130
1131 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
1132 nmp->nm_rsize = argp->rsize;
1133 /* Round down to multiple of blocksize */
1134 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
1135 if (nmp->nm_rsize <= 0)
1136 nmp->nm_rsize = NFS_FABLKSIZE;
1137 }
1138 if (nmp->nm_rsize > maxio)
1139 nmp->nm_rsize = maxio;
1140 if (nmp->nm_rsize > NFS_MAXBSIZE)
1141 nmp->nm_rsize = NFS_MAXBSIZE;
1142
1143 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
1144 nmp->nm_readdirsize = argp->readdirsize;
1145 }
1146 if (nmp->nm_readdirsize > maxio)
1147 nmp->nm_readdirsize = maxio;
1148 if (nmp->nm_readdirsize > nmp->nm_rsize)
1149 nmp->nm_readdirsize = nmp->nm_rsize;
1150
1151 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
1152 argp->maxgrouplist <= NFS_MAXGRPS)
1153 nmp->nm_numgrps = argp->maxgrouplist;
1154 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
1155 argp->readahead <= NFS_MAXRAHEAD)
1156 nmp->nm_readahead = argp->readahead;
1157
1158 if (argp->version >= 4) {
1159 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
1160 nmp->nm_acregmin = argp->acregmin;
1161 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
1162 nmp->nm_acregmax = argp->acregmax;
1163 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
1164 nmp->nm_acdirmin = argp->acdirmin;
1165 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
1166 nmp->nm_acdirmax = argp->acdirmax;
1167 if (nmp->nm_acregmin > nmp->nm_acregmax)
1168 nmp->nm_acregmin = nmp->nm_acregmax;
1169 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
1170 nmp->nm_acdirmin = nmp->nm_acdirmax;
1171 }
1172
1173 /* Set up the sockets and per-host congestion */
1174 nmp->nm_sotype = argp->sotype;
1175 nmp->nm_soproto = argp->proto;
1176
1177 /* make sure mbuf constants are set up */
1178 if (!nfs_mbuf_mlen)
1179 nfs_mbuf_init();
1180
1181 /*
1182 * For Connection based sockets (TCP,...) defer the connect until
1183 * the first request, in case the server is not responding.
1184 */
1185 if (nmp->nm_sotype == SOCK_DGRAM &&
1186 (error = nfs_connect(nmp, (struct nfsreq *)0)))
1187 goto bad;
1188
1189 /*
1190 * Get file attributes for the mountpoint. These are needed
1191 * in order to properly create the root vnode.
1192 */
1193 // LP64todo - fix CAST_DOWN of argp->fh
1194 error = nfs_getattr_no_vnode(mp, CAST_DOWN(caddr_t, argp->fh), argp->fhsize,
1195 context.vc_ucred, p, &nvattrs, &xid);
1196 if (error) {
1197 /*
1198 * we got problems... we couldn't get the attributes
1199 * from the NFS server... so the mount fails.
1200 */
1201 goto bad;
1202 }
1203
1204 /*
1205 * A reference count is needed on the nfsnode representing the
1206 * remote root. If this object is not persistent, then backward
1207 * traversals of the mount point (i.e. "..") will not work if
1208 * the nfsnode gets flushed out of the cache. UFS does not have
1209 * this problem, because one can identify root inodes by their
1210 * number == ROOTINO (2).
1211 */
1212 error = nfs_nget(mp, NULL, NULL, CAST_DOWN(caddr_t, argp->fh), argp->fhsize,
1213 &nvattrs, &xid, NG_MARKROOT, &np);
1214 if (error)
1215 goto bad;
1216
1217 /*
1218 * save this vnode pointer. That way nfs_unmount()
1219 * does not need to call nfs_nget() just get it to drop
1220 * this vnode reference.
1221 */
1222 nmp->nm_dvp = *vpp = NFSTOV(np);
1223 /* get usecount and drop iocount */
1224 error = vnode_ref(*vpp);
1225 if (error) {
1226 vnode_put(*vpp);
1227 goto bad;
1228 }
1229 vnode_put(*vpp);
1230
1231 /*
1232 * Set the mount point's block I/O size.
1233 * We really need to do this after we get info back from
1234 * the server about what its preferred I/O sizes are.
1235 */
1236 if (nmp->nm_flag & NFSMNT_NFSV3)
1237 nfs_fsinfo(nmp, *vpp, context.vc_ucred, p);
1238 nmp->nm_biosize = nfs_biosize(nmp);
1239 vfs_statfs(mp)->f_iosize = NFS_IOSIZE;
1240
1241 /*
1242 * V3 mounts give us a (relatively) reliable remote access(2)
1243 * call, so advertise the fact.
1244 *
1245 * XXX this may not be the best way to go, as the granularity
1246 * offered isn't a good match to our needs.
1247 */
1248 if (nmp->nm_flag & NFSMNT_NFSV3)
1249 vfs_setauthopaqueaccess(mp);
1250
1251 /*
1252 * Do statfs to ensure static info gets set to reasonable values.
1253 */
1254 context.vc_proc = p;
1255 nfs_statfs(mp, vfs_statfs(mp), &context);
1256
1257 if (nmp->nm_flag & NFSMNT_RESVPORT)
1258 nfs_resv_mounts++;
1259 nmp->nm_state |= NFSSTA_MOUNTED;
1260 kauth_cred_unref(&context.vc_ucred);
1261 return (0);
1262bad:
1263 nfs_disconnect(nmp);
1264 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1265 mbuf_freem(nam);
1266 kauth_cred_unref(&context.vc_ucred);
1267 return (error);
1268}
1269
1270
1271/*
1272 * unmount system call
1273 */
1274static int
1275nfs_unmount(
1276 mount_t mp,
1277 int mntflags,
1278 __unused vfs_context_t context)
1279{
1280 register struct nfsmount *nmp;
1281 vnode_t vp;
1282 int error, flags = 0;
1283
1284 nmp = VFSTONFS(mp);
1285 /*
1286 * During a force unmount we want to...
1287 * Mark that we are doing a force unmount.
1288 * Make the mountpoint soft.
1289 */
1290 if (mntflags & MNT_FORCE) {
1291 flags |= FORCECLOSE;
1292 nmp->nm_state |= NFSSTA_FORCE;
1293 nmp->nm_flag |= NFSMNT_SOFT;
1294 }
1295 /*
1296 * Goes something like this..
1297 * - Call vflush() to clear out vnodes for this file system,
1298 * except for the swap files. Deal with them in 2nd pass.
1299 * - Decrement reference on the vnode representing remote root.
1300 * - Close the socket
1301 * - Free up the data structures
1302 */
1303 vp = nmp->nm_dvp;
1304
1305 /*
1306 * vflush will check for busy vnodes on mountpoint.
1307 * Will do the right thing for MNT_FORCE. That is, we should
1308 * not get EBUSY back.
1309 */
1310 error = vflush(mp, vp, SKIPSWAP | flags);
1311 if (mntflags & MNT_FORCE) {
1312 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
1313 } else {
1314 if (vnode_isinuse(vp, 1))
1315 return (EBUSY);
1316 error = vflush(mp, vp, flags);
1317 }
1318 if (error)
1319 return (error);
1320
1321 nmp->nm_state &= ~NFSSTA_MOUNTED;
1322 if (nmp->nm_flag & NFSMNT_RESVPORT) {
1323 if (--nfs_resv_mounts == 0)
1324 nfs_bind_resv_thread_wake();
1325 }
1326
1327 /*
1328 * Release the root vnode reference held by mountnfs()
1329 */
1330 vnode_rele(vp);
1331
1332 (void)vflush(mp, NULLVP, FORCECLOSE);
1333 vfs_setfsprivate(mp, 0); /* don't want to end up using stale vp */
1334
1335 nfs_disconnect(nmp);
1336 mbuf_freem(nmp->nm_nam);
1337
1338 if ((nmp->nm_flag & NFSMNT_KERB) == 0) {
1339 struct nfsreq *rp;
1340 /*
1341 * Loop through outstanding request list and remove dangling
1342 * references to defunct nfsmount struct
1343 */
1344 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
1345 if (rp->r_nmp == nmp)
1346 rp->r_nmp = (struct nfsmount *)0;
1347 /* Need to wake up any rcvlock waiters so they notice the unmount. */
1348 if (nmp->nm_state & NFSSTA_WANTRCV) {
1349 nmp->nm_state &= ~NFSSTA_WANTRCV;
1350 wakeup(&nmp->nm_state);
1351 }
1352 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1353 }
1354 return (0);
1355}
1356
1357/*
1358 * Return root of a filesystem
1359 */
1360static int
1361nfs_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t context)
1362{
1363 vnode_t vp;
1364 struct nfsmount *nmp;
1365 int error;
1366 u_long vpid;
1367
1368 nmp = VFSTONFS(mp);
1369 vp = nmp->nm_dvp;
1370 vpid = vnode_vid(vp);
1371 while ((error = vnode_getwithvid(vp, vpid))) {
1372 /* vnode_get() may return ENOENT if the dir changes. */
1373 /* If that happens, just try it again, else return the error. */
1374 if ((error != ENOENT) || (vnode_vid(vp) == vpid))
1375 return (error);
1376 vpid = vnode_vid(vp);
1377 }
1378 *vpp = vp;
1379 return (0);
1380}
1381
1382/*
1383 * Flush out the buffer cache
1384 */
1385
1386struct nfs_sync_cargs {
1387 vfs_context_t context;
1388 int waitfor;
1389 int error;
1390};
1391
1392static int
1393nfs_sync_callout(vnode_t vp, void *arg)
1394{
1395 struct nfs_sync_cargs *cargs = (struct nfs_sync_cargs*)arg;
1396 int error;
1397
1398 if (LIST_EMPTY(&VTONFS(vp)->n_dirtyblkhd))
1399 return (VNODE_RETURNED);
1400 if (VTONFS(vp)->n_flag & NWRBUSY)
1401 return (VNODE_RETURNED);
1402
1403 error = nfs_flush(vp, cargs->waitfor,
1404 vfs_context_ucred(cargs->context),
1405 vfs_context_proc(cargs->context), 0);
1406 if (error)
1407 cargs->error = error;
1408
1409 return (VNODE_RETURNED);
1410}
1411
1412static int
1413nfs_sync(mount_t mp, int waitfor, vfs_context_t context)
1414{
1415 struct nfs_sync_cargs cargs;
1416
1417 cargs.waitfor = waitfor;
1418 cargs.context = context;
1419 cargs.error = 0;
1420
1421 vnode_iterate(mp, 0, nfs_sync_callout, &cargs);
1422
1423 return (cargs.error);
1424}
1425
1426/*
1427 * NFS flat namespace lookup.
1428 * Currently unsupported.
1429 */
1430/*ARGSUSED*/
1431static int
1432nfs_vget(
1433 __unused mount_t mp,
1434 __unused ino64_t ino,
1435 __unused vnode_t *vpp,
1436 __unused vfs_context_t context)
1437{
1438
1439 return (ENOTSUP);
1440}
1441
1442/*
1443 * At this point, this should never happen
1444 */
1445/*ARGSUSED*/
1446static int
1447nfs_fhtovp(
1448 __unused mount_t mp,
1449 __unused int fhlen,
1450 __unused unsigned char *fhp,
1451 __unused vnode_t *vpp,
1452 __unused vfs_context_t context)
1453{
1454
1455 return (ENOTSUP);
1456}
1457
1458/*
1459 * Vnode pointer to File handle, should never happen either
1460 */
1461/*ARGSUSED*/
1462static int
1463nfs_vptofh(
1464 __unused vnode_t vp,
1465 __unused int *fhlenp,
1466 __unused unsigned char *fhp,
1467 __unused vfs_context_t context)
1468{
1469
1470 return (ENOTSUP);
1471}
1472
1473/*
1474 * Vfs start routine, a no-op.
1475 */
1476/*ARGSUSED*/
1477static int
1478nfs_start(
1479 __unused mount_t mp,
1480 __unused int flags,
1481 __unused vfs_context_t context)
1482{
1483
1484 return (0);
1485}
1486
1487/*
1488 * Do that sysctl thang...
1489 */
1490static int
1491nfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
1492 user_addr_t newp, size_t newlen, vfs_context_t context)
1493{
1494 int error = 0, val;
1495 struct sysctl_req *req = NULL;
1496 struct vfsidctl vc;
1497 struct user_vfsidctl user_vc;
1498 mount_t mp;
1499 struct nfsmount *nmp = NULL;
1500 struct vfsquery vq;
1501 boolean_t is_64_bit;
1502
1503 /*
1504 * All names at this level are terminal.
1505 */
1506 if(namelen > 1)
1507 return ENOTDIR; /* overloaded */
1508
1509 is_64_bit = vfs_context_is64bit(context);
1510
1511 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1512 switch (name[0]) {
1513 case VFS_CTL_TIMEO:
1514 case VFS_CTL_QUERY:
1515 case VFS_CTL_NOLOCKS:
1516 req = CAST_DOWN(struct sysctl_req *, oldp);
1517 if (is_64_bit) {
1518 error = SYSCTL_IN(req, &user_vc, sizeof(user_vc));
1519 if (error)
1520 return (error);
1521 mp = vfs_getvfs(&user_vc.vc_fsid);
1522 }
1523 else {
1524 error = SYSCTL_IN(req, &vc, sizeof(vc));
1525 if (error)
1526 return (error);
1527 mp = vfs_getvfs(&vc.vc_fsid);
1528 }
1529 if (mp == NULL)
1530 return (ENOENT);
1531 nmp = VFSTONFS(mp);
1532 if (nmp == NULL)
1533 return (ENOENT);
1534 bzero(&vq, sizeof(vq));
1535 req->newidx = 0;
1536 if (is_64_bit) {
1537 req->newptr = user_vc.vc_ptr;
1538 req->newlen = (size_t)user_vc.vc_len;
1539 }
1540 else {
1541 req->newptr = CAST_USER_ADDR_T(vc.vc_ptr);
1542 req->newlen = vc.vc_len;
1543 }
1544 }
1545
1546 switch(name[0]) {
1547 case NFS_NFSSTATS:
1548 if(!oldp) {
1549 *oldlenp = sizeof nfsstats;
1550 return 0;
1551 }
1552
1553 if(*oldlenp < sizeof nfsstats) {
1554 *oldlenp = sizeof nfsstats;
1555 return ENOMEM;
1556 }
1557
1558 error = copyout(&nfsstats, oldp, sizeof nfsstats);
1559 if (error)
1560 return (error);
1561
1562 if(newp && newlen != sizeof nfsstats)
1563 return EINVAL;
1564
1565 if(newp) {
1566 return copyin(newp, &nfsstats, sizeof nfsstats);
1567 }
1568 return 0;
1569 case VFS_CTL_NOLOCKS:
1570 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1571 if (req->oldptr != USER_ADDR_NULL) {
1572 error = SYSCTL_OUT(req, &val, sizeof(val));
1573 if (error)
1574 return (error);
1575 }
1576 if (req->newptr != USER_ADDR_NULL) {
1577 error = SYSCTL_IN(req, &val, sizeof(val));
1578 if (error)
1579 return (error);
1580 if (val)
1581 nmp->nm_flag |= NFSMNT_NOLOCKS;
1582 else
1583 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1584 }
1585 break;
1586 case VFS_CTL_QUERY:
1587 if (nmp->nm_state & NFSSTA_TIMEO)
1588 vq.vq_flags |= VQ_NOTRESP;
1589 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1590 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1591 vq.vq_flags |= VQ_NOTRESPLOCK;
1592 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1593 break;
1594 case VFS_CTL_TIMEO:
1595 if (req->oldptr != USER_ADDR_NULL) {
1596 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1597 sizeof(nmp->nm_tprintf_initial_delay));
1598 if (error)
1599 return (error);
1600 }
1601 if (req->newptr != USER_ADDR_NULL) {
1602 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1603 sizeof(nmp->nm_tprintf_initial_delay));
1604 if (error)
1605 return (error);
1606 if (nmp->nm_tprintf_initial_delay < 0)
1607 nmp->nm_tprintf_initial_delay = 0;
1608 }
1609 break;
1610 default:
1611 return (ENOTSUP);
1612 }
1613 return (error);
1614}
1615