2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1989, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * Rick Macklem at The University of Guelph.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
67 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/ioctl.h>
74 #include <sys/signal.h>
75 #include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
76 #include <sys/kauth.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/mount_internal.h>
82 #include <sys/kpi_mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/fcntl.h>
86 #include <libkern/OSAtomic.h>
89 #include <sys/vmparam.h>
91 #if !defined(NO_MOUNT_PRIVATE)
92 #include <sys/filedesc.h>
93 #endif /* NO_MOUNT_PRIVATE */
96 #include <net/route.h>
97 #include <netinet/in.h>
99 #include <nfs/rpcv2.h>
100 #include <nfs/nfsproto.h>
102 #include <nfs/nfsnode.h>
103 #include <nfs/nfsmount.h>
104 #include <nfs/xdr_subs.h>
105 #include <nfs/nfsm_subs.h>
106 #include <nfs/nfsdiskless.h>
107 #include <nfs/nfs_lock.h>
109 extern int nfs_mountroot(void);
111 extern int nfs_ticks
;
112 extern int nfs_mount_type
;
113 extern int nfs_resv_mounts
;
115 struct nfsstats nfsstats
;
116 static int nfs_sysctl(int *, u_int
, user_addr_t
, size_t *, user_addr_t
, size_t, vfs_context_t
);
117 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
119 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
120 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
121 &nfsstats
, nfsstats
, "");
124 SYSCTL_DECL(_vfs_generic_nfs
);
125 SYSCTL_NODE(_vfs_generic_nfs
, OID_AUTO
, client
, CTLFLAG_RW
, 0,
127 /* how long NFS will wait before signalling vfs that it's down. */
128 static int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
129 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_INITIAL_DELAY
,
130 initialdowndelay
, CTLFLAG_RW
, &nfs_tprintf_initial_delay
, 0, "");
131 /* how long between console messages "nfs server foo not responding" */
132 static int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
133 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_DELAY
,
134 nextdowndelay
, CTLFLAG_RW
, &nfs_tprintf_delay
, 0, "");
136 static int nfs_iosize(struct nfsmount
*nmp
);
137 static int mountnfs(struct user_nfs_args
*,mount_t
,mbuf_t
,proc_t
,vnode_t
*);
138 static int nfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t context
);
139 static int nfs_start(mount_t mp
, int flags
, vfs_context_t context
);
140 static int nfs_unmount(mount_t mp
, int mntflags
, vfs_context_t context
);
141 static int nfs_root(mount_t mp
, vnode_t
*vpp
, vfs_context_t context
);
142 static int nfs_statfs(mount_t mp
, struct vfsstatfs
*sbp
, vfs_context_t context
);
143 static int nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
);
144 static int nfs_sync( mount_t mp
, int waitfor
, vfs_context_t context
);
145 static int nfs_vptofh(vnode_t vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
146 static int nfs_fhtovp(mount_t mp
, int fhlen
, unsigned char *fhp
, vnode_t
*vpp
, vfs_context_t context
);
147 static int nfs_vget(mount_t
, ino64_t
, vnode_t
*, vfs_context_t context
);
151 * nfs vfs operations.
153 struct vfsops nfs_vfsops
= {
171 nfs_mount_diskless(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*);
172 #if !defined(NO_MOUNT_PRIVATE)
174 nfs_mount_diskless_private(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*);
175 #endif /* NO_MOUNT_PRIVATE */
177 static int nfs_iosize(nmp
)
178 struct nfsmount
* nmp
;
183 * Calculate the size used for io buffers. Use the larger
184 * of the two sizes to minimise nfs requests but make sure
185 * that it is at least one VM page to avoid wasting buffer
186 * space and to allow easy mmapping of I/O buffers.
187 * The read/write rpc calls handle the splitting up of
188 * buffers into multiple requests if the buffer size is
189 * larger than the I/O size.
191 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
192 if (iosize
< PAGE_SIZE
)
194 return (trunc_page_32(iosize
));
201 nfs_statfs(mount_t mp
, struct vfsstatfs
*sbp
, vfs_context_t context
)
203 proc_t p
= vfs_context_proc(context
);
205 struct nfs_statfs
*sfp
;
209 caddr_t bpos
, dpos
, cp2
;
210 struct nfsmount
*nmp
= VFSTONFS(mp
);
211 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
212 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
215 struct ucred temp_cred
;
218 sfp
= (struct nfs_statfs
*)0;
221 if ((error
= vnode_get(vp
)))
224 bzero(&temp_cred
, sizeof(temp_cred
));
225 temp_cred
.cr_ngroups
= 1;
226 cred
= kauth_cred_create(&temp_cred
);
228 if (v3
&& (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
229 nfs_fsinfo(nmp
, vp
, cred
, p
);
230 nfsm_reqhead(NFSX_FH(v3
));
232 kauth_cred_rele(cred
);
236 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_FSSTAT
]);
238 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
240 nfsm_postop_attr_update(vp
, v3
, retattr
, &xid
);
241 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
243 sbp
->f_flags
= nmp
->nm_flag
;
244 sbp
->f_iosize
= nfs_iosize(nmp
);
247 * Adjust block size to get total block count to fit in a long.
248 * If we can't increase block size enough, clamp to max long.
250 u_quad_t tquad
, tquad2
, bsize
;
251 bsize
= NFS_FABLKSIZE
;
253 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
255 while ((tquad
& ~0x7fffffff) && (bsize
< 0x40000000)) {
259 sbp
->f_blocks
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
261 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
263 sbp
->f_bfree
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
265 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
267 sbp
->f_bavail
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
269 sbp
->f_bsize
= (long)bsize
;
271 /* adjust file slots too... */
272 fxdr_hyper(&sfp
->sf_tfiles
, &tquad
);
273 fxdr_hyper(&sfp
->sf_ffiles
, &tquad2
);
274 while (tquad
& ~0x7fffffff) {
278 sbp
->f_files
= tquad
;
279 sbp
->f_ffree
= tquad2
;
281 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
282 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
283 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
284 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
289 kauth_cred_rele(cred
);
295 * The nfs_statfs code is complicated, and used by mountnfs(), so leave it as-is
296 * and handle VFS_GETATTR by calling nfs_statfs and copying fields.
299 nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
303 if (VFSATTR_IS_ACTIVE(fsap
, f_bsize
) ||
304 VFSATTR_IS_ACTIVE(fsap
, f_iosize
) ||
305 VFSATTR_IS_ACTIVE(fsap
, f_blocks
) ||
306 VFSATTR_IS_ACTIVE(fsap
, f_bfree
) ||
307 VFSATTR_IS_ACTIVE(fsap
, f_bavail
) ||
308 VFSATTR_IS_ACTIVE(fsap
, f_bused
) ||
309 VFSATTR_IS_ACTIVE(fsap
, f_files
) ||
310 VFSATTR_IS_ACTIVE(fsap
, f_ffree
)) {
313 error
= nfs_statfs(mp
, &sb
, context
);
315 VFSATTR_RETURN(fsap
, f_bsize
, sb
.f_bsize
);
316 VFSATTR_RETURN(fsap
, f_iosize
, sb
.f_iosize
);
317 VFSATTR_RETURN(fsap
, f_blocks
, sb
.f_blocks
);
318 VFSATTR_RETURN(fsap
, f_bfree
, sb
.f_bfree
);
319 VFSATTR_RETURN(fsap
, f_bavail
, sb
.f_bavail
);
320 VFSATTR_RETURN(fsap
, f_bused
, sb
.f_blocks
- sb
.f_bfree
);
321 VFSATTR_RETURN(fsap
, f_files
, sb
.f_files
);
322 VFSATTR_RETURN(fsap
, f_ffree
, sb
.f_ffree
);
326 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
327 struct nfsmount
*nmp
;
328 struct nfsv3_pathconf pc
;
329 u_int32_t caps
, valid
;
333 if (!(nmp
= VFSTONFS(mp
)))
336 v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
);
339 * The capabilities[] array defines what this volume supports.
341 * The valid[] array defines which bits this code understands
342 * the meaning of (whether the volume has that capability or not).
343 * Any zero bits here means "I don't know what you're asking about"
344 * and the caller cannot tell whether that capability is
349 /* try to get fsinfo if we haven't already */
350 if (!(nmp
->nm_state
& NFSSTA_GOTFSINFO
)) {
351 nfs_fsinfo(nmp
, vp
, vfs_context_ucred(context
),
352 vfs_context_proc(context
));
353 if (!(nmp
= VFSTONFS(vnode_mount(vp
))))
356 if (nmp
->nm_state
& NFSSTA_GOTFSINFO
) {
357 /* fsinfo indicates (non)support of links and symlinks */
358 valid
|= VOL_CAP_FMT_SYMBOLICLINKS
|
359 VOL_CAP_FMT_HARDLINKS
;
360 if (nmp
->nm_fsinfo
.fsproperties
& NFSV3FSINFO_SYMLINK
)
361 caps
|= VOL_CAP_FMT_SYMBOLICLINKS
;
362 if (nmp
->nm_fsinfo
.fsproperties
& NFSV3FSINFO_LINK
)
363 caps
|= VOL_CAP_FMT_HARDLINKS
;
364 /* if fsinfo indicates all pathconf info is the same, */
365 /* we can use it to report case attributes */
366 if ((nmp
->nm_fsinfo
.fsproperties
& NFSV3FSINFO_HOMOGENEOUS
) &&
367 !(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
368 /* no cached pathconf info, try to get now */
369 error
= nfs_pathconfrpc(vp
, &pc
,
370 vfs_context_ucred(context
),
371 vfs_context_proc(context
));
372 if (!(nmp
= VFSTONFS(vnode_mount(vp
))))
375 /* all files have the same pathconf info, */
376 /* so cache a copy of the results */
377 nfs_pathconf_cache(nmp
, &pc
);
380 if (nmp
->nm_state
& NFSSTA_GOTPATHCONF
) {
381 valid
|= VOL_CAP_FMT_CASE_SENSITIVE
|
382 VOL_CAP_FMT_CASE_PRESERVING
;
383 if (!(nmp
->nm_fsinfo
.pcflags
&
384 NFSPCINFO_CASE_INSENSITIVE
))
385 caps
|= VOL_CAP_FMT_CASE_SENSITIVE
;
386 if (nmp
->nm_fsinfo
.pcflags
&
387 NFSPCINFO_CASE_PRESERVING
)
388 caps
|= VOL_CAP_FMT_CASE_PRESERVING
;
390 /* Is server's max file size at least 2TB? */
391 if (nmp
->nm_fsinfo
.maxfilesize
>= 0x20000000000ULL
)
392 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
395 * NFSv3 supports 64 bits of file size.
396 * Without FSINFO from the server, we'll
397 * just assume maxfilesize >= 2TB
399 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
402 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] =
403 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
404 // VOL_CAP_FMT_SYMBOLICLINKS |
405 // VOL_CAP_FMT_HARDLINKS |
406 // VOL_CAP_FMT_JOURNAL |
407 // VOL_CAP_FMT_JOURNAL_ACTIVE |
408 // VOL_CAP_FMT_NO_ROOT_TIMES |
409 // VOL_CAP_FMT_SPARSE_FILES |
410 // VOL_CAP_FMT_ZERO_RUNS |
411 // VOL_CAP_FMT_CASE_SENSITIVE |
412 // VOL_CAP_FMT_CASE_PRESERVING |
413 // VOL_CAP_FMT_FAST_STATFS |
414 // VOL_CAP_FMT_2TB_FILESIZE |
416 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] =
417 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
418 // VOL_CAP_FMT_SYMBOLICLINKS |
419 // VOL_CAP_FMT_HARDLINKS |
420 // VOL_CAP_FMT_JOURNAL |
421 // VOL_CAP_FMT_JOURNAL_ACTIVE |
422 // VOL_CAP_FMT_NO_ROOT_TIMES |
423 // VOL_CAP_FMT_SPARSE_FILES |
424 // VOL_CAP_FMT_ZERO_RUNS |
425 // VOL_CAP_FMT_CASE_SENSITIVE |
426 // VOL_CAP_FMT_CASE_PRESERVING |
427 VOL_CAP_FMT_FAST_STATFS
|
428 VOL_CAP_FMT_2TB_FILESIZE
|
432 * We don't support most of the interfaces.
434 * We MAY support locking, but we don't have any easy way of probing.
435 * We can tell if there's no lockd running or if locks have been
436 * disabled for a mount, so we can definitely answer NO in that case.
437 * Any attempt to send a request to lockd to test for locking support
438 * may cause the lazily-launched locking daemons to be started
439 * unnecessarily. So we avoid that. However, we do record if we ever
440 * successfully perform a lock operation on a mount point, so if it
441 * looks like lock ops have worked, we do report that we support them.
444 if ((!nfslockdvnode
&& !nfslockdwaiting
) ||
445 (nmp
->nm_flag
& NFSMNT_NOLOCKS
)) {
446 /* locks disabled on this mount, so they definitely won't work */
447 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
448 } else if (nmp
->nm_state
& NFSSTA_LOCKSWORK
) {
449 caps
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
450 valid
= VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
;
452 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_INTERFACES
] =
453 // VOL_CAP_INT_SEARCHFS |
454 // VOL_CAP_INT_ATTRLIST |
455 // VOL_CAP_INT_NFSEXPORT |
456 // VOL_CAP_INT_READDIRATTR |
457 // VOL_CAP_INT_EXCHANGEDATA |
458 // VOL_CAP_INT_COPYFILE |
459 // VOL_CAP_INT_ALLOCATE |
460 // VOL_CAP_INT_VOL_RENAME |
461 // VOL_CAP_INT_ADVLOCK |
462 // VOL_CAP_INT_FLOCK |
463 // VOL_CAP_INT_EXTENDED_SECURITY |
464 // VOL_CAP_INT_USERACCESS |
466 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_INTERFACES
] =
467 VOL_CAP_INT_SEARCHFS
|
468 VOL_CAP_INT_ATTRLIST
|
469 VOL_CAP_INT_NFSEXPORT
|
470 VOL_CAP_INT_READDIRATTR
|
471 VOL_CAP_INT_EXCHANGEDATA
|
472 VOL_CAP_INT_COPYFILE
|
473 VOL_CAP_INT_ALLOCATE
|
474 VOL_CAP_INT_VOL_RENAME
|
475 // VOL_CAP_INT_ADVLOCK |
476 // VOL_CAP_INT_FLOCK |
477 // VOL_CAP_INT_EXTENDED_SECURITY |
478 // VOL_CAP_INT_USERACCESS |
481 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
482 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
484 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
485 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
487 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
490 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
491 fsap
->f_attributes
.validattr
.commonattr
= 0;
492 fsap
->f_attributes
.validattr
.volattr
=
493 ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
494 fsap
->f_attributes
.validattr
.dirattr
= 0;
495 fsap
->f_attributes
.validattr
.fileattr
= 0;
496 fsap
->f_attributes
.validattr
.forkattr
= 0;
498 fsap
->f_attributes
.nativeattr
.commonattr
= 0;
499 fsap
->f_attributes
.nativeattr
.volattr
=
500 ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
501 fsap
->f_attributes
.nativeattr
.dirattr
= 0;
502 fsap
->f_attributes
.nativeattr
.fileattr
= 0;
503 fsap
->f_attributes
.nativeattr
.forkattr
= 0;
505 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
512 * nfs version 3 fsinfo rpc call
515 nfs_fsinfo(nmp
, vp
, cred
, p
)
516 struct nfsmount
*nmp
;
521 struct nfsv3_fsinfo
*fsp
;
525 int prefsize
, maxsize
;
526 caddr_t bpos
, dpos
, cp2
;
527 int error
= 0, retattr
;
528 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
531 nfsm_reqhead(NFSX_FH(1));
534 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_FSINFO
]);
536 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
538 nfsm_postop_attr_update(vp
, 1, retattr
, &xid
);
541 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
542 prefsize
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
543 if (prefsize
< nmp
->nm_wsize
)
544 nmp
->nm_wsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
545 ~(NFS_FABLKSIZE
- 1);
546 maxsize
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
547 if (maxsize
< nmp
->nm_wsize
) {
548 nmp
->nm_wsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
549 if (nmp
->nm_wsize
== 0)
550 nmp
->nm_wsize
= maxsize
;
552 prefsize
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
553 if (prefsize
< nmp
->nm_rsize
)
554 nmp
->nm_rsize
= (prefsize
+ NFS_FABLKSIZE
- 1) &
555 ~(NFS_FABLKSIZE
- 1);
556 maxsize
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
557 if (maxsize
< nmp
->nm_rsize
) {
558 nmp
->nm_rsize
= maxsize
& ~(NFS_FABLKSIZE
- 1);
559 if (nmp
->nm_rsize
== 0)
560 nmp
->nm_rsize
= maxsize
;
562 prefsize
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
563 if (prefsize
< nmp
->nm_readdirsize
)
564 nmp
->nm_readdirsize
= prefsize
;
565 if (maxsize
< nmp
->nm_readdirsize
) {
566 nmp
->nm_readdirsize
= maxsize
;
568 fxdr_hyper(&fsp
->fs_maxfilesize
, &nmp
->nm_fsinfo
.maxfilesize
);
569 nmp
->nm_fsinfo
.fsproperties
= fxdr_unsigned(u_long
, fsp
->fs_properties
);
570 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
577 * Mount a remote root fs via. nfs. This depends on the info in the
578 * nfs_diskless structure that has been filled in properly by some primary
580 * It goes something like this:
581 * - do enough of "ifconfig" by calling ifioctl() so that the system
582 * can talk to the server
583 * - If nfs_diskless.mygateway is filled in, use that address as
585 * - hand craft the swap nfs vnode hanging off a fake mount point
586 * if swdevt[0].sw_dev == NODEV
587 * - build the rootfs mount point and call mountnfs() to do the rest.
592 struct nfs_diskless nd
;
593 struct nfs_vattr nvattr
;
598 #if !defined(NO_MOUNT_PRIVATE)
601 #endif /* NO_MOUNT_PRIVATE */
604 procp
= current_proc(); /* XXX */
607 * Call nfs_boot_init() to fill in the nfs_diskless struct.
608 * Note: networking must already have been configured before
611 bzero((caddr_t
) &nd
, sizeof(nd
));
612 error
= nfs_boot_init(&nd
, procp
);
614 panic("nfs_boot_init failed with %d\n", error
);
618 * Try NFSv3 first, then fallback to NFSv2.
619 * Likewise, try TCP first, then fall back to UDP.
622 sotype
= SOCK_STREAM
;
625 error
= nfs_boot_getfh(&nd
, procp
, v3
, sotype
);
627 if (error
== EHOSTDOWN
|| error
== EHOSTUNREACH
) {
628 if (nd
.nd_root
.ndm_path
)
629 FREE_ZONE(nd
.nd_root
.ndm_path
,
630 MAXPATHLEN
, M_NAMEI
);
631 if (nd
.nd_private
.ndm_path
)
632 FREE_ZONE(nd
.nd_private
.ndm_path
,
633 MAXPATHLEN
, M_NAMEI
);
637 if (sotype
== SOCK_STREAM
) {
638 printf("nfs_boot_getfh(v3,TCP) failed with %d, trying UDP...\n", error
);
642 printf("nfs_boot_getfh(v3,UDP) failed with %d, trying v2...\n", error
);
644 sotype
= SOCK_STREAM
;
646 } else if (sotype
== SOCK_STREAM
) {
647 printf("nfs_boot_getfh(v2,TCP) failed with %d, trying UDP...\n", error
);
651 panic("nfs_boot_getfh(v2,UDP) failed with %d\n", error
);
655 * Create the root mount point.
657 #if !defined(NO_MOUNT_PRIVATE)
658 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
|MNT_ROOTFS
, &vp
, &mp
)))
660 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_ROOTFS
, &vp
, &mp
)))
661 #endif /* NO_MOUNT_PRIVATE */
664 if (sotype
== SOCK_STREAM
) {
665 printf("nfs_mount_diskless(v3,TCP) failed with %d, trying UDP...\n", error
);
669 printf("nfs_mount_diskless(v3,UDP) failed with %d, trying v2...\n", error
);
671 sotype
= SOCK_STREAM
;
673 } else if (sotype
== SOCK_STREAM
) {
674 printf("nfs_mount_diskless(v2,TCP) failed with %d, trying UDP...\n", error
);
678 panic("nfs_mount_diskless(v2,UDP) root failed with %d\n", error
);
680 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
686 #if !defined(NO_MOUNT_PRIVATE)
687 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
688 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
689 0, &vppriv
, &mppriv
);
691 panic("nfs_mount_diskless private failed with %d\n", error
);
693 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
696 mount_list_add(mppriv
);
699 #endif /* NO_MOUNT_PRIVATE */
701 if (nd
.nd_root
.ndm_path
)
702 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
703 if (nd
.nd_private
.ndm_path
)
704 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
706 /* Get root attributes (for the time). */
707 error
= nfs_getattr(vp
, &nvattr
, kauth_cred_get(), procp
);
708 if (error
) panic("nfs_mountroot: getattr for root");
713 * Internal version of mount system call for diskless setup.
717 struct nfs_dlmount
*ndmntp
,
723 struct user_nfs_args args
;
729 procp
= current_proc(); /* XXX */
731 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
732 printf("nfs_mount_diskless: NFS not configured");
736 mp
->mnt_flag
|= mntflag
;
737 if (!(mntflag
& MNT_RDONLY
))
738 mp
->mnt_flag
&= ~MNT_RDONLY
;
740 /* Initialize mount args. */
741 bzero((caddr_t
) &args
, sizeof(args
));
742 args
.addr
= CAST_USER_ADDR_T(&ndmntp
->ndm_saddr
);
743 args
.addrlen
= ndmntp
->ndm_saddr
.sin_len
;
744 args
.sotype
= ndmntp
->ndm_sotype
;
745 args
.fh
= CAST_USER_ADDR_T(&ndmntp
->ndm_fh
[0]);
746 args
.fhsize
= ndmntp
->ndm_fhlen
;
747 args
.hostname
= CAST_USER_ADDR_T(ndmntp
->ndm_host
);
748 args
.flags
= NFSMNT_RESVPORT
;
749 if (ndmntp
->ndm_nfsv3
)
750 args
.flags
|= NFSMNT_NFSV3
;
752 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &m
);
754 printf("nfs_mount_diskless: mbuf_get(soname) failed");
757 mbuf_setlen(m
, ndmntp
->ndm_saddr
.sin_len
);
758 bcopy((caddr_t
)args
.addr
, mbuf_data(m
), ndmntp
->ndm_saddr
.sin_len
);
759 if ((error
= mountnfs(&args
, mp
, m
, procp
, vpp
))) {
760 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
761 // XXX vfs_rootmountfailed(mp);
763 mp
->mnt_vtable
->vfc_refcount
--;
766 mount_lock_destroy(mp
);
767 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
774 #if !defined(NO_MOUNT_PRIVATE)
776 * Internal version of mount system call to mount "/private"
777 * separately in diskless setup
780 nfs_mount_diskless_private(
781 struct nfs_dlmount
*ndmntp
,
787 struct user_nfs_args args
;
792 struct vfstable
*vfsp
;
795 struct vfs_context context
;
797 procp
= current_proc(); /* XXX */
798 context
.vc_proc
= procp
;
799 context
.vc_ucred
= kauth_cred_get();
803 * mimic main()!. Temporarily set up rootvnode and other stuff so
804 * that namei works. Need to undo this because main() does it, too
806 struct filedesc
*fdp
; /* pointer to file descriptor state */
808 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
810 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
811 if (VFS_ROOT(mountlist
.tqh_first
, &rootvnode
, NULL
))
812 panic("cannot find root vnode");
813 error
= vnode_ref(rootvnode
);
815 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
818 fdp
->fd_cdir
= rootvnode
;
823 * Get vnode to be covered
825 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
,
827 if ((error
= namei(&nd
))) {
828 printf("nfs_mountroot: private namei failed!\n");
832 /* undo vnode_ref() in mimic main()! */
833 vnode_rele(rootvnode
);
838 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, &context
)) ||
839 (error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
843 if (vnode_vtype(vp
) != VDIR
) {
847 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
848 if (!strcmp(vfsp
->vfc_name
, "nfs"))
851 printf("nfs_mountroot: private NFS not configured\n");
855 if (vnode_mountedhere(vp
) != NULL
) {
861 * Allocate and initialize the filesystem.
863 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
865 printf("nfs_mountroot: unable to allocate mount structure\n");
869 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
871 /* Initialize the default IO constraints */
872 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
873 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
876 TAILQ_INIT(&mp
->mnt_vnodelist
);
877 TAILQ_INIT(&mp
->mnt_workerqueue
);
878 TAILQ_INIT(&mp
->mnt_newvnodes
);
879 (void)vfs_busy(mp
, LK_NOWAIT
);
880 TAILQ_INIT(&mp
->mnt_vnodelist
);
882 vfsp
->vfc_refcount
++;
884 mp
->mnt_vtable
= vfsp
;
885 mp
->mnt_op
= vfsp
->vfc_vfsops
;
886 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
887 mp
->mnt_flag
= mntflag
;
888 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
889 strncpy(mp
->mnt_vfsstat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
-1);
890 vp
->v_mountedhere
= mp
;
891 mp
->mnt_vnodecovered
= vp
;
892 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(kauth_cred_get());
893 (void) copystr(mntname
, mp
->mnt_vfsstat
.f_mntonname
, MNAMELEN
- 1, 0);
894 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_vfsstat
.f_mntfromname
, MNAMELEN
- 1, 0);
896 /* Initialize mount args. */
897 bzero((caddr_t
) &args
, sizeof(args
));
898 args
.addr
= CAST_USER_ADDR_T(&ndmntp
->ndm_saddr
);
899 args
.addrlen
= ndmntp
->ndm_saddr
.sin_len
;
900 args
.sotype
= ndmntp
->ndm_sotype
;
901 args
.fh
= CAST_USER_ADDR_T(ndmntp
->ndm_fh
);
902 args
.fhsize
= ndmntp
->ndm_fhlen
;
903 args
.hostname
= CAST_USER_ADDR_T(ndmntp
->ndm_host
);
904 args
.flags
= NFSMNT_RESVPORT
;
905 if (ndmntp
->ndm_nfsv3
)
906 args
.flags
|= NFSMNT_NFSV3
;
908 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &m
);
910 printf("nfs_mount_diskless_private: mbuf_get(soname) failed");
913 mbuf_setlen(m
, ndmntp
->ndm_saddr
.sin_len
);
914 bcopy((caddr_t
)args
.addr
, mbuf_data(m
), ndmntp
->ndm_saddr
.sin_len
);
915 if ((error
= mountnfs(&args
, mp
, m
, procp
, &vp
))) {
916 printf("nfs_mountroot: mount %s failed: %d\n", mntname
, error
);
918 vfsp
->vfc_refcount
--;
921 mount_lock_destroy(mp
);
922 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
930 #endif /* NO_MOUNT_PRIVATE */
938 nfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t context
)
940 proc_t p
= vfs_context_proc(context
);
942 struct user_nfs_args args
;
943 struct nfs_args tempargs
;
946 u_char nfh
[NFSX_V3FHMAX
];
949 error
= copyin(data
, (caddr_t
)&argsvers
, sizeof (argsvers
));
955 if (vfs_context_is64bit(context
))
956 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct user_nfs_args3
));
958 error
= copyin(data
, (caddr_t
)&tempargs
, sizeof (struct nfs_args3
));
961 if (vfs_context_is64bit(context
))
962 error
= copyin(data
, (caddr_t
)&args
, sizeof (args
));
964 error
= copyin(data
, (caddr_t
)&tempargs
, sizeof (tempargs
));
967 return (EPROGMISMATCH
);
972 if (!vfs_context_is64bit(context
)) {
973 args
.version
= tempargs
.version
;
974 args
.addrlen
= tempargs
.addrlen
;
975 args
.sotype
= tempargs
.sotype
;
976 args
.proto
= tempargs
.proto
;
977 args
.fhsize
= tempargs
.fhsize
;
978 args
.flags
= tempargs
.flags
;
979 args
.wsize
= tempargs
.wsize
;
980 args
.rsize
= tempargs
.rsize
;
981 args
.readdirsize
= tempargs
.readdirsize
;
982 args
.timeo
= tempargs
.timeo
;
983 args
.retrans
= tempargs
.retrans
;
984 args
.maxgrouplist
= tempargs
.maxgrouplist
;
985 args
.readahead
= tempargs
.readahead
;
986 args
.leaseterm
= tempargs
.leaseterm
;
987 args
.deadthresh
= tempargs
.deadthresh
;
988 args
.addr
= CAST_USER_ADDR_T(tempargs
.addr
);
989 args
.fh
= CAST_USER_ADDR_T(tempargs
.fh
);
990 args
.hostname
= CAST_USER_ADDR_T(tempargs
.hostname
);
992 args
.acregmin
= tempargs
.acregmin
;
993 args
.acregmax
= tempargs
.acregmax
;
994 args
.acdirmin
= tempargs
.acdirmin
;
995 args
.acdirmax
= tempargs
.acdirmax
;
999 if (args
.fhsize
< 0 || args
.fhsize
> NFSX_V3FHMAX
)
1001 error
= copyin(args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
1005 mntfrom
= &vfs_statfs(mp
)->f_mntfromname
[0];
1006 error
= copyinstr(args
.hostname
, mntfrom
, MAXPATHLEN
-1, &len
);
1009 bzero(&mntfrom
[len
], MAXPATHLEN
- len
);
1011 /* sockargs() call must be after above copyin() calls */
1012 error
= sockargs(&nam
, args
.addr
, args
.addrlen
, MBUF_TYPE_SONAME
);
1016 args
.fh
= CAST_USER_ADDR_T(&nfh
[0]);
1017 error
= mountnfs(&args
, mp
, nam
, p
, &vp
);
1022 * Common code for mount and mountroot
1026 struct user_nfs_args
*argp
,
1032 struct nfsmount
*nmp
;
1035 struct nfs_vattr nvattrs
;
1036 struct vfs_context context
; /* XXX get from caller? */
1040 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
1041 * no sense in that context.
1043 if (argp
->sotype
== SOCK_STREAM
)
1044 argp
->flags
&= ~NFSMNT_NOCONN
;
1046 if (vfs_flags(mp
) & MNT_UPDATE
) {
1048 /* update paths, file handles, etc, here XXX */
1052 MALLOC_ZONE(nmp
, struct nfsmount
*,
1053 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
1058 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
1059 TAILQ_INIT(&nmp
->nm_uidlruhead
);
1060 TAILQ_INIT(&nmp
->nm_bufq
);
1061 vfs_setfsprivate(mp
, nmp
);
1064 /* setup defaults */
1065 nmp
->nm_timeo
= NFS_TIMEO
;
1066 nmp
->nm_retry
= NFS_RETRANS
;
1067 if (argp
->sotype
== SOCK_DGRAM
) {
1068 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
1069 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
1071 nmp
->nm_wsize
= NFS_WSIZE
;
1072 nmp
->nm_rsize
= NFS_RSIZE
;
1074 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
1075 nmp
->nm_numgrps
= NFS_MAXGRPS
;
1076 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
1077 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
1078 if (nmp
->nm_tprintf_delay
< 0)
1079 nmp
->nm_tprintf_delay
= 0;
1080 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
1081 if (nmp
->nm_tprintf_initial_delay
< 0)
1082 nmp
->nm_tprintf_initial_delay
= 0;
1083 nmp
->nm_acregmin
= NFS_MINATTRTIMO
;
1084 nmp
->nm_acregmax
= NFS_MAXATTRTIMO
;
1085 nmp
->nm_acdirmin
= NFS_MINDIRATTRTIMO
;
1086 nmp
->nm_acdirmax
= NFS_MAXDIRATTRTIMO
;
1089 nmp
->nm_mountp
= mp
;
1090 vfs_setauthopaque(mp
);
1091 nmp
->nm_flag
= argp
->flags
;
1094 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
1095 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
1096 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
1097 nmp
->nm_timeo
= NFS_MINTIMEO
;
1098 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
1099 nmp
->nm_timeo
= NFS_MAXTIMEO
;
1102 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
1103 nmp
->nm_retry
= argp
->retrans
;
1104 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
1105 nmp
->nm_retry
= NFS_MAXREXMIT
;
1108 if (argp
->flags
& NFSMNT_NFSV3
) {
1109 if (argp
->sotype
== SOCK_DGRAM
)
1110 maxio
= NFS_MAXDGRAMDATA
;
1112 maxio
= NFS_MAXDATA
;
1114 maxio
= NFS_V2MAXDATA
;
1116 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
1117 nmp
->nm_wsize
= argp
->wsize
;
1118 /* Round down to multiple of blocksize */
1119 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
1120 if (nmp
->nm_wsize
<= 0)
1121 nmp
->nm_wsize
= NFS_FABLKSIZE
;
1123 if (nmp
->nm_wsize
> maxio
)
1124 nmp
->nm_wsize
= maxio
;
1125 if (nmp
->nm_wsize
> MAXBSIZE
)
1126 nmp
->nm_wsize
= MAXBSIZE
;
1128 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
1129 nmp
->nm_rsize
= argp
->rsize
;
1130 /* Round down to multiple of blocksize */
1131 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
1132 if (nmp
->nm_rsize
<= 0)
1133 nmp
->nm_rsize
= NFS_FABLKSIZE
;
1135 if (nmp
->nm_rsize
> maxio
)
1136 nmp
->nm_rsize
= maxio
;
1137 if (nmp
->nm_rsize
> MAXBSIZE
)
1138 nmp
->nm_rsize
= MAXBSIZE
;
1140 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
1141 nmp
->nm_readdirsize
= argp
->readdirsize
;
1143 if (nmp
->nm_readdirsize
> maxio
)
1144 nmp
->nm_readdirsize
= maxio
;
1145 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
1146 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
1148 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
1149 argp
->maxgrouplist
<= NFS_MAXGRPS
)
1150 nmp
->nm_numgrps
= argp
->maxgrouplist
;
1151 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
1152 argp
->readahead
<= NFS_MAXRAHEAD
)
1153 nmp
->nm_readahead
= argp
->readahead
;
1155 if (argp
->version
>= 4) {
1156 if ((argp
->flags
& NFSMNT_ACREGMIN
) && argp
->acregmin
>= 0)
1157 nmp
->nm_acregmin
= argp
->acregmin
;
1158 if ((argp
->flags
& NFSMNT_ACREGMAX
) && argp
->acregmax
>= 0)
1159 nmp
->nm_acregmax
= argp
->acregmax
;
1160 if ((argp
->flags
& NFSMNT_ACDIRMIN
) && argp
->acdirmin
>= 0)
1161 nmp
->nm_acdirmin
= argp
->acdirmin
;
1162 if ((argp
->flags
& NFSMNT_ACDIRMAX
) && argp
->acdirmax
>= 0)
1163 nmp
->nm_acdirmax
= argp
->acdirmax
;
1164 if (nmp
->nm_acregmin
> nmp
->nm_acregmax
)
1165 nmp
->nm_acregmin
= nmp
->nm_acregmax
;
1166 if (nmp
->nm_acdirmin
> nmp
->nm_acdirmax
)
1167 nmp
->nm_acdirmin
= nmp
->nm_acdirmax
;
1170 /* Set up the sockets and per-host congestion */
1171 nmp
->nm_sotype
= argp
->sotype
;
1172 nmp
->nm_soproto
= argp
->proto
;
1174 /* make sure mbuf constants are set up */
1179 * For Connection based sockets (TCP,...) defer the connect until
1180 * the first request, in case the server is not responding.
1182 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
1183 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
1187 * Get file attributes for the mountpoint. These are needed
1188 * in order to properly create the root vnode.
1190 // LP64todo - fix CAST_DOWN of argp->fh
1191 error
= nfs_getattr_no_vnode(mp
, CAST_DOWN(caddr_t
, argp
->fh
), argp
->fhsize
,
1192 proc_ucred(p
), p
, &nvattrs
, &xid
);
1195 * we got problems... we couldn't get the attributes
1196 * from the NFS server... so the mount fails.
1202 * A reference count is needed on the nfsnode representing the
1203 * remote root. If this object is not persistent, then backward
1204 * traversals of the mount point (i.e. "..") will not work if
1205 * the nfsnode gets flushed out of the cache. UFS does not have
1206 * this problem, because one can identify root inodes by their
1207 * number == ROOTINO (2).
1209 error
= nfs_nget(mp
, NULL
, NULL
, CAST_DOWN(caddr_t
, argp
->fh
), argp
->fhsize
,
1210 &nvattrs
, &xid
, NG_MARKROOT
, &np
);
1215 * save this vnode pointer. That way nfs_unmount()
1216 * does not need to call nfs_nget() just get it to drop
1217 * this vnode reference.
1219 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
1220 /* get usecount and drop iocount */
1221 error
= vnode_ref(*vpp
);
1229 * Set the mount point's block I/O size.
1230 * We really need to do this after we get info back from
1231 * the server about what its preferred I/O sizes are.
1233 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
1234 nfs_fsinfo(nmp
, *vpp
, proc_ucred(p
), p
);
1235 vfs_statfs(mp
)->f_iosize
= nfs_iosize(nmp
);
1238 * V3 mounts give us a (relatively) reliable remote access(2)
1239 * call, so advertise the fact.
1241 * XXX this may not be the best way to go, as the granularity
1242 * offered isn't a good match to our needs.
1244 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
1245 vfs_setauthopaqueaccess(mp
);
1248 * Do statfs to ensure static info gets set to reasonable values.
1250 context
.vc_proc
= p
;
1251 context
.vc_ucred
= proc_ucred(p
);
1252 nfs_statfs(mp
, vfs_statfs(mp
), &context
);
1254 if (nmp
->nm_flag
& NFSMNT_RESVPORT
)
1256 nmp
->nm_state
|= NFSSTA_MOUNTED
;
1259 nfs_disconnect(nmp
);
1260 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1267 * unmount system call
1273 __unused vfs_context_t context
)
1275 register struct nfsmount
*nmp
;
1277 int error
, flags
= 0;
1281 * During a force unmount we want to...
1282 * Mark that we are doing a force unmount.
1283 * Make the mountpoint soft.
1285 if (mntflags
& MNT_FORCE
) {
1286 flags
|= FORCECLOSE
;
1287 nmp
->nm_state
|= NFSSTA_FORCE
;
1288 nmp
->nm_flag
|= NFSMNT_SOFT
;
1291 * Goes something like this..
1292 * - Call vflush() to clear out vnodes for this file system,
1293 * except for the swap files. Deal with them in 2nd pass.
1294 * - Decrement reference on the vnode representing remote root.
1295 * - Close the socket
1296 * - Free up the data structures
1301 * vflush will check for busy vnodes on mountpoint.
1302 * Will do the right thing for MNT_FORCE. That is, we should
1303 * not get EBUSY back.
1305 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
1306 if (mntflags
& MNT_FORCE
) {
1307 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
1309 if (vnode_isinuse(vp
, 1))
1311 error
= vflush(mp
, vp
, flags
);
1316 nmp
->nm_state
&= ~NFSSTA_MOUNTED
;
1317 if (nmp
->nm_flag
& NFSMNT_RESVPORT
) {
1318 if (--nfs_resv_mounts
== 0)
1319 nfs_bind_resv_thread_wake();
1323 * Release the root vnode reference held by mountnfs()
1327 (void)vflush(mp
, NULLVP
, FORCECLOSE
);
1328 vfs_setfsprivate(mp
, 0); /* don't want to end up using stale vp */
1330 nfs_disconnect(nmp
);
1331 mbuf_freem(nmp
->nm_nam
);
1333 if ((nmp
->nm_flag
& NFSMNT_KERB
) == 0) {
1336 * Loop through outstanding request list and remove dangling
1337 * references to defunct nfsmount struct
1339 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1340 if (rp
->r_nmp
== nmp
)
1341 rp
->r_nmp
= (struct nfsmount
*)0;
1342 /* Need to wake up any rcvlock waiters so they notice the unmount. */
1343 if (nmp
->nm_state
& NFSSTA_WANTRCV
) {
1344 nmp
->nm_state
&= ~NFSSTA_WANTRCV
;
1345 wakeup(&nmp
->nm_state
);
1347 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1353 * Return root of a filesystem
1356 nfs_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t context
)
1359 struct nfsmount
*nmp
;
1365 vpid
= vnode_vid(vp
);
1366 while ((error
= vnode_getwithvid(vp
, vpid
))) {
1367 /* vnode_get() may return ENOENT if the dir changes. */
1368 /* If that happens, just try it again, else return the error. */
1369 if ((error
!= ENOENT
) || (vnode_vid(vp
) == vpid
))
1371 vpid
= vnode_vid(vp
);
1378 * Flush out the buffer cache
1381 struct nfs_sync_cargs
{
1382 vfs_context_t context
;
1388 nfs_sync_callout(vnode_t vp
, void *arg
)
1390 struct nfs_sync_cargs
*cargs
= (struct nfs_sync_cargs
*)arg
;
1393 if (LIST_EMPTY(&VTONFS(vp
)->n_dirtyblkhd
))
1394 return (VNODE_RETURNED
);
1395 if (VTONFS(vp
)->n_flag
& NWRBUSY
)
1396 return (VNODE_RETURNED
);
1398 error
= nfs_flush(vp
, cargs
->waitfor
,
1399 vfs_context_ucred(cargs
->context
),
1400 vfs_context_proc(cargs
->context
), 0);
1402 cargs
->error
= error
;
1404 return (VNODE_RETURNED
);
1408 nfs_sync(mount_t mp
, int waitfor
, vfs_context_t context
)
1410 struct nfs_sync_cargs cargs
;
1412 cargs
.waitfor
= waitfor
;
1413 cargs
.context
= context
;
1416 vnode_iterate(mp
, 0, nfs_sync_callout
, &cargs
);
1418 return (cargs
.error
);
1422 * NFS flat namespace lookup.
1423 * Currently unsupported.
1428 __unused mount_t mp
,
1429 __unused ino64_t ino
,
1430 __unused vnode_t
*vpp
,
1431 __unused vfs_context_t context
)
1438 * At this point, this should never happen
1443 __unused mount_t mp
,
1445 __unused
unsigned char *fhp
,
1446 __unused vnode_t
*vpp
,
1447 __unused vfs_context_t context
)
1454 * Vnode pointer to File handle, should never happen either
1459 __unused vnode_t vp
,
1460 __unused
int *fhlenp
,
1461 __unused
unsigned char *fhp
,
1462 __unused vfs_context_t context
)
1469 * Vfs start routine, a no-op.
1474 __unused mount_t mp
,
1476 __unused vfs_context_t context
)
1483 * Do that sysctl thang...
1486 nfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1487 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1490 struct sysctl_req
*req
= NULL
;
1492 struct user_vfsidctl user_vc
;
1494 struct nfsmount
*nmp
= NULL
;
1496 boolean_t is_64_bit
;
1499 * All names at this level are terminal.
1502 return ENOTDIR
; /* overloaded */
1504 is_64_bit
= vfs_context_is64bit(context
);
1506 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1510 case VFS_CTL_NOLOCKS
:
1511 req
= CAST_DOWN(struct sysctl_req
*, oldp
);
1513 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
1516 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
1519 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
1522 mp
= vfs_getvfs(&vc
.vc_fsid
);
1529 bzero(&vq
, sizeof(vq
));
1532 req
->newptr
= user_vc
.vc_ptr
;
1533 req
->newlen
= (size_t)user_vc
.vc_len
;
1536 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
1537 req
->newlen
= vc
.vc_len
;
1544 *oldlenp
= sizeof nfsstats
;
1548 if(*oldlenp
< sizeof nfsstats
) {
1549 *oldlenp
= sizeof nfsstats
;
1553 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1557 if(newp
&& newlen
!= sizeof nfsstats
)
1561 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
1564 case VFS_CTL_NOLOCKS
:
1565 val
= (nmp
->nm_flag
& NFSMNT_NOLOCKS
) ? 1 : 0;
1566 if (req
->oldptr
!= USER_ADDR_NULL
) {
1567 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
1571 if (req
->newptr
!= USER_ADDR_NULL
) {
1572 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
1576 nmp
->nm_flag
|= NFSMNT_NOLOCKS
;
1578 nmp
->nm_flag
&= ~NFSMNT_NOLOCKS
;
1582 if (nmp
->nm_state
& NFSSTA_TIMEO
)
1583 vq
.vq_flags
|= VQ_NOTRESP
;
1584 if (!(nmp
->nm_flag
& NFSMNT_NOLOCKS
) &&
1585 (nmp
->nm_state
& NFSSTA_LOCKTIMEO
))
1586 vq
.vq_flags
|= VQ_NOTRESPLOCK
;
1587 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
1590 if (req
->oldptr
!= USER_ADDR_NULL
) {
1591 error
= SYSCTL_OUT(req
, &nmp
->nm_tprintf_initial_delay
,
1592 sizeof(nmp
->nm_tprintf_initial_delay
));
1596 if (req
->newptr
!= USER_ADDR_NULL
) {
1597 error
= SYSCTL_IN(req
, &nmp
->nm_tprintf_initial_delay
,
1598 sizeof(nmp
->nm_tprintf_initial_delay
));
1601 if (nmp
->nm_tprintf_initial_delay
< 0)
1602 nmp
->nm_tprintf_initial_delay
= 0;