2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
65 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
68 #include <sys/param.h>
69 #include <sys/systm.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>
87 #include <sys/vmparam.h>
89 #if !defined(NO_MOUNT_PRIVATE)
90 #include <sys/filedesc.h>
91 #endif /* NO_MOUNT_PRIVATE */
94 #include <net/route.h>
95 #include <netinet/in.h>
97 #include <nfs/rpcv2.h>
98 #include <nfs/nfsproto.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>
107 extern int nfs_mountroot(void);
109 extern int nfs_ticks
;
110 extern int nfs_mount_type
;
111 extern int nfs_resv_mounts
;
113 struct nfsstats nfsstats
;
114 static 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 */
117 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
118 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
119 &nfsstats
, nfsstats
, "");
122 SYSCTL_DECL(_vfs_generic_nfs
);
123 SYSCTL_NODE(_vfs_generic_nfs
, OID_AUTO
, client
, CTLFLAG_RW
, 0,
125 /* how long NFS will wait before signalling vfs that it's down. */
126 static int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
127 SYSCTL_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" */
130 static int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
131 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_DELAY
,
132 nextdowndelay
, CTLFLAG_RW
, &nfs_tprintf_delay
, 0, "");
134 static int nfs_iosize(struct nfsmount
*nmp
);
135 static int mountnfs(struct user_nfs_args
*,mount_t
,mbuf_t
,proc_t
,vnode_t
*);
136 static int nfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t context
);
137 static int nfs_start(mount_t mp
, int flags
, vfs_context_t context
);
138 static int nfs_unmount(mount_t mp
, int mntflags
, vfs_context_t context
);
139 static int nfs_root(mount_t mp
, vnode_t
*vpp
, vfs_context_t context
);
140 static int nfs_statfs(mount_t mp
, struct vfsstatfs
*sbp
, vfs_context_t context
);
141 static int nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
);
142 static int nfs_sync( mount_t mp
, int waitfor
, vfs_context_t context
);
143 static int nfs_vptofh(vnode_t vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
144 static int nfs_fhtovp(mount_t mp
, int fhlen
, unsigned char *fhp
, vnode_t
*vpp
, vfs_context_t context
);
145 static int nfs_vget(mount_t
, ino64_t
, vnode_t
*, vfs_context_t context
);
149 * nfs vfs operations.
151 struct vfsops nfs_vfsops
= {
169 nfs_mount_diskless(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*);
170 #if !defined(NO_MOUNT_PRIVATE)
172 nfs_mount_diskless_private(struct nfs_dlmount
*, const char *, int, vnode_t
*, mount_t
*);
173 #endif /* NO_MOUNT_PRIVATE */
175 static int nfs_iosize(nmp
)
176 struct nfsmount
* nmp
;
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.
189 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
190 if (iosize
< PAGE_SIZE
)
192 return (trunc_page_32(iosize
));
199 nfs_statfs(mount_t mp
, struct vfsstatfs
*sbp
, vfs_context_t context
)
201 proc_t p
= vfs_context_proc(context
);
203 struct nfs_statfs
*sfp
;
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
;
213 struct ucred temp_cred
;
216 sfp
= (struct nfs_statfs
*)0;
219 if ((error
= vnode_get(vp
)))
222 bzero(&temp_cred
, sizeof(temp_cred
));
223 temp_cred
.cr_ngroups
= 1;
224 cred
= kauth_cred_create(&temp_cred
);
226 if (v3
&& (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
227 nfs_fsinfo(nmp
, vp
, cred
, p
);
228 nfsm_reqhead(NFSX_FH(v3
));
230 kauth_cred_rele(cred
);
234 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_FSSTAT
]);
236 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
238 nfsm_postop_attr_update(vp
, v3
, retattr
, &xid
);
239 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
241 sbp
->f_flags
= nmp
->nm_flag
;
242 sbp
->f_iosize
= nfs_iosize(nmp
);
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.
248 u_quad_t tquad
, tquad2
, bsize
;
249 bsize
= NFS_FABLKSIZE
;
251 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
253 while ((tquad
& ~0x7fffffff) && (bsize
< 0x40000000)) {
257 sbp
->f_blocks
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
259 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
261 sbp
->f_bfree
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
263 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
265 sbp
->f_bavail
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
267 sbp
->f_bsize
= (long)bsize
;
269 /* adjust file slots too... */
270 fxdr_hyper(&sfp
->sf_tfiles
, &tquad
);
271 fxdr_hyper(&sfp
->sf_ffiles
, &tquad2
);
272 while (tquad
& ~0x7fffffff) {
276 sbp
->f_files
= tquad
;
277 sbp
->f_ffree
= tquad2
;
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
);
287 kauth_cred_rele(cred
);
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.
297 nfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
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
)) {
311 error
= nfs_statfs(mp
, &sb
, context
);
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
);
324 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
325 struct nfsmount
*nmp
;
326 struct nfsv3_pathconf pc
;
327 u_int32_t caps
, valid
;
331 if (!(nmp
= VFSTONFS(mp
)))
334 v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
);
337 * The capabilities[] array defines what this volume supports.
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
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
))))
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
))))
373 /* all files have the same pathconf info, */
374 /* so cache a copy of the results */
375 nfs_pathconf_cache(nmp
, &pc
);
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
;
388 /* Is server's max file size at least 2TB? */
389 if (nmp
->nm_fsinfo
.maxfilesize
>= 0x20000000000ULL
)
390 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
393 * NFSv3 supports 64 bits of file size.
394 * Without FSINFO from the server, we'll
395 * just assume maxfilesize >= 2TB
397 caps
|= VOL_CAP_FMT_2TB_FILESIZE
;
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 |
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
|
430 * We don't support most of the interfaces.
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.
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
;
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 |
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 |
479 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
480 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
482 fsap
->f_capabilities
.capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
483 fsap
->f_capabilities
.valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
485 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
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;
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;
503 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
510 * nfs version 3 fsinfo rpc call
513 nfs_fsinfo(nmp
, vp
, cred
, p
)
514 struct nfsmount
*nmp
;
519 struct nfsv3_fsinfo
*fsp
;
523 int prefsize
, maxsize
;
524 caddr_t bpos
, dpos
, cp2
;
525 int error
= 0, retattr
;
526 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
529 nfsm_reqhead(NFSX_FH(1));
532 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_FSINFO
]);
534 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
536 nfsm_postop_attr_update(vp
, 1, retattr
, &xid
);
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
;
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
;
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
;
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
;
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
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
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.
590 struct nfs_diskless nd
;
591 struct nfs_vattr nvattr
;
596 #if !defined(NO_MOUNT_PRIVATE)
599 #endif /* NO_MOUNT_PRIVATE */
602 procp
= current_proc(); /* XXX */
605 * Call nfs_boot_init() to fill in the nfs_diskless struct.
606 * Note: networking must already have been configured before
609 bzero((caddr_t
) &nd
, sizeof(nd
));
610 error
= nfs_boot_init(&nd
, procp
);
612 panic("nfs_boot_init failed with %d\n", error
);
616 * Try NFSv3 first, then fallback to NFSv2.
617 * Likewise, try TCP first, then fall back to UDP.
620 sotype
= SOCK_STREAM
;
623 error
= nfs_boot_getfh(&nd
, procp
, v3
, sotype
);
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
);
635 if (sotype
== SOCK_STREAM
) {
636 printf("nfs_boot_getfh(v3,TCP) failed with %d, trying UDP...\n", error
);
640 printf("nfs_boot_getfh(v3,UDP) failed with %d, trying v2...\n", error
);
642 sotype
= SOCK_STREAM
;
644 } else if (sotype
== SOCK_STREAM
) {
645 printf("nfs_boot_getfh(v2,TCP) failed with %d, trying UDP...\n", error
);
649 panic("nfs_boot_getfh(v2,UDP) failed with %d\n", error
);
653 * Create the root mount point.
655 #if !defined(NO_MOUNT_PRIVATE)
656 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
|MNT_ROOTFS
, &vp
, &mp
)))
658 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_ROOTFS
, &vp
, &mp
)))
659 #endif /* NO_MOUNT_PRIVATE */
662 if (sotype
== SOCK_STREAM
) {
663 printf("nfs_mount_diskless(v3,TCP) failed with %d, trying UDP...\n", error
);
667 printf("nfs_mount_diskless(v3,UDP) failed with %d, trying v2...\n", error
);
669 sotype
= SOCK_STREAM
;
671 } else if (sotype
== SOCK_STREAM
) {
672 printf("nfs_mount_diskless(v2,TCP) failed with %d, trying UDP...\n", error
);
676 panic("nfs_mount_diskless(v2,UDP) root failed with %d\n", error
);
678 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
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
);
689 panic("nfs_mount_diskless private failed with %d\n", error
);
691 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
694 mount_list_add(mppriv
);
697 #endif /* NO_MOUNT_PRIVATE */
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
);
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");
711 * Internal version of mount system call for diskless setup.
715 struct nfs_dlmount
*ndmntp
,
721 struct user_nfs_args args
;
727 procp
= current_proc(); /* XXX */
729 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
730 printf("nfs_mount_diskless: NFS not configured");
734 mp
->mnt_flag
|= mntflag
;
735 if (!(mntflag
& MNT_RDONLY
))
736 mp
->mnt_flag
&= ~MNT_RDONLY
;
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
;
750 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &m
);
752 printf("nfs_mount_diskless: mbuf_get(soname) failed");
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);
761 mp
->mnt_vtable
->vfc_refcount
--;
764 mount_lock_destroy(mp
);
765 FREE_ZONE(mp
, sizeof(struct mount
), M_MOUNT
);
772 #if !defined(NO_MOUNT_PRIVATE)
774 * Internal version of mount system call to mount "/private"
775 * separately in diskless setup
778 nfs_mount_diskless_private(
779 struct nfs_dlmount
*ndmntp
,
785 struct user_nfs_args args
;
790 struct vfstable
*vfsp
;
793 struct vfs_context context
;
795 procp
= current_proc(); /* XXX */
796 context
.vc_proc
= procp
;
797 context
.vc_ucred
= kauth_cred_get();
801 * mimic main()!. Temporarily set up rootvnode and other stuff so
802 * that namei works. Need to undo this because main() does it, too
804 struct filedesc
*fdp
; /* pointer to file descriptor state */
806 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
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
);
813 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
816 fdp
->fd_cdir
= rootvnode
;
821 * Get vnode to be covered
823 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
,
825 if ((error
= namei(&nd
))) {
826 printf("nfs_mountroot: private namei failed!\n");
830 /* undo vnode_ref() in mimic main()! */
831 vnode_rele(rootvnode
);
836 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, &context
)) ||
837 (error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
841 if (vnode_vtype(vp
) != VDIR
) {
845 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
846 if (!strcmp(vfsp
->vfc_name
, "nfs"))
849 printf("nfs_mountroot: private NFS not configured\n");
853 if (vnode_mountedhere(vp
) != NULL
) {
859 * Allocate and initialize the filesystem.
861 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
863 printf("nfs_mountroot: unable to allocate mount structure\n");
867 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
869 /* Initialize the default IO constraints */
870 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
871 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
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
);
880 vfsp
->vfc_refcount
++;
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);
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
;
906 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &m
);
908 printf("nfs_mount_diskless_private: mbuf_get(soname) failed");
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
);
916 vfsp
->vfc_refcount
--;
919 mount_lock_destroy(mp
);
920 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
928 #endif /* NO_MOUNT_PRIVATE */
936 nfs_mount(mount_t mp
, vnode_t vp
, user_addr_t data
, vfs_context_t context
)
938 proc_t p
= vfs_context_proc(context
);
940 struct user_nfs_args args
;
941 struct nfs_args tempargs
;
944 u_char nfh
[NFSX_V3FHMAX
];
947 error
= copyin(data
, (caddr_t
)&argsvers
, sizeof (argsvers
));
953 if (vfs_context_is64bit(context
))
954 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct user_nfs_args3
));
956 error
= copyin(data
, (caddr_t
)&tempargs
, sizeof (struct nfs_args3
));
959 if (vfs_context_is64bit(context
))
960 error
= copyin(data
, (caddr_t
)&args
, sizeof (args
));
962 error
= copyin(data
, (caddr_t
)&tempargs
, sizeof (tempargs
));
965 return (EPROGMISMATCH
);
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
);
990 args
.acregmin
= tempargs
.acregmin
;
991 args
.acregmax
= tempargs
.acregmax
;
992 args
.acdirmin
= tempargs
.acdirmin
;
993 args
.acdirmax
= tempargs
.acdirmax
;
997 if (args
.fhsize
< 0 || args
.fhsize
> NFSX_V3FHMAX
)
999 error
= copyin(args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
1003 mntfrom
= &vfs_statfs(mp
)->f_mntfromname
[0];
1004 error
= copyinstr(args
.hostname
, mntfrom
, MAXPATHLEN
-1, &len
);
1007 bzero(&mntfrom
[len
], MAXPATHLEN
- len
);
1009 /* sockargs() call must be after above copyin() calls */
1010 error
= sockargs(&nam
, args
.addr
, args
.addrlen
, MBUF_TYPE_SONAME
);
1014 args
.fh
= CAST_USER_ADDR_T(&nfh
[0]);
1015 error
= mountnfs(&args
, mp
, nam
, p
, &vp
);
1020 * Common code for mount and mountroot
1024 struct user_nfs_args
*argp
,
1030 struct nfsmount
*nmp
;
1033 struct nfs_vattr nvattrs
;
1034 struct vfs_context context
; /* XXX get from caller? */
1038 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
1039 * no sense in that context.
1041 if (argp
->sotype
== SOCK_STREAM
)
1042 argp
->flags
&= ~NFSMNT_NOCONN
;
1044 if (vfs_flags(mp
) & MNT_UPDATE
) {
1046 /* update paths, file handles, etc, here XXX */
1050 MALLOC_ZONE(nmp
, struct nfsmount
*,
1051 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
1056 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
1057 TAILQ_INIT(&nmp
->nm_uidlruhead
);
1058 TAILQ_INIT(&nmp
->nm_bufq
);
1059 vfs_setfsprivate(mp
, nmp
);
1062 /* setup defaults */
1063 nmp
->nm_timeo
= NFS_TIMEO
;
1064 nmp
->nm_retry
= NFS_RETRANS
;
1065 if (argp
->sotype
== SOCK_DGRAM
) {
1066 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
1067 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
1069 nmp
->nm_wsize
= NFS_WSIZE
;
1070 nmp
->nm_rsize
= NFS_RSIZE
;
1072 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
1073 nmp
->nm_numgrps
= NFS_MAXGRPS
;
1074 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
1075 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
1076 if (nmp
->nm_tprintf_delay
< 0)
1077 nmp
->nm_tprintf_delay
= 0;
1078 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
1079 if (nmp
->nm_tprintf_initial_delay
< 0)
1080 nmp
->nm_tprintf_initial_delay
= 0;
1081 nmp
->nm_acregmin
= NFS_MINATTRTIMO
;
1082 nmp
->nm_acregmax
= NFS_MAXATTRTIMO
;
1083 nmp
->nm_acdirmin
= NFS_MINDIRATTRTIMO
;
1084 nmp
->nm_acdirmax
= NFS_MAXDIRATTRTIMO
;
1087 nmp
->nm_mountp
= mp
;
1088 vfs_setauthopaque(mp
);
1089 nmp
->nm_flag
= argp
->flags
;
1092 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
1093 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
1094 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
1095 nmp
->nm_timeo
= NFS_MINTIMEO
;
1096 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
1097 nmp
->nm_timeo
= NFS_MAXTIMEO
;
1100 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
1101 nmp
->nm_retry
= argp
->retrans
;
1102 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
1103 nmp
->nm_retry
= NFS_MAXREXMIT
;
1106 if (argp
->flags
& NFSMNT_NFSV3
) {
1107 if (argp
->sotype
== SOCK_DGRAM
)
1108 maxio
= NFS_MAXDGRAMDATA
;
1110 maxio
= NFS_MAXDATA
;
1112 maxio
= NFS_V2MAXDATA
;
1114 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
1115 nmp
->nm_wsize
= argp
->wsize
;
1116 /* Round down to multiple of blocksize */
1117 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
1118 if (nmp
->nm_wsize
<= 0)
1119 nmp
->nm_wsize
= NFS_FABLKSIZE
;
1121 if (nmp
->nm_wsize
> maxio
)
1122 nmp
->nm_wsize
= maxio
;
1123 if (nmp
->nm_wsize
> MAXBSIZE
)
1124 nmp
->nm_wsize
= MAXBSIZE
;
1126 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
1127 nmp
->nm_rsize
= argp
->rsize
;
1128 /* Round down to multiple of blocksize */
1129 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
1130 if (nmp
->nm_rsize
<= 0)
1131 nmp
->nm_rsize
= NFS_FABLKSIZE
;
1133 if (nmp
->nm_rsize
> maxio
)
1134 nmp
->nm_rsize
= maxio
;
1135 if (nmp
->nm_rsize
> MAXBSIZE
)
1136 nmp
->nm_rsize
= MAXBSIZE
;
1138 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
1139 nmp
->nm_readdirsize
= argp
->readdirsize
;
1141 if (nmp
->nm_readdirsize
> maxio
)
1142 nmp
->nm_readdirsize
= maxio
;
1143 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
1144 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
1146 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
1147 argp
->maxgrouplist
<= NFS_MAXGRPS
)
1148 nmp
->nm_numgrps
= argp
->maxgrouplist
;
1149 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
1150 argp
->readahead
<= NFS_MAXRAHEAD
)
1151 nmp
->nm_readahead
= argp
->readahead
;
1153 if (argp
->version
>= 4) {
1154 if ((argp
->flags
& NFSMNT_ACREGMIN
) && argp
->acregmin
>= 0)
1155 nmp
->nm_acregmin
= argp
->acregmin
;
1156 if ((argp
->flags
& NFSMNT_ACREGMAX
) && argp
->acregmax
>= 0)
1157 nmp
->nm_acregmax
= argp
->acregmax
;
1158 if ((argp
->flags
& NFSMNT_ACDIRMIN
) && argp
->acdirmin
>= 0)
1159 nmp
->nm_acdirmin
= argp
->acdirmin
;
1160 if ((argp
->flags
& NFSMNT_ACDIRMAX
) && argp
->acdirmax
>= 0)
1161 nmp
->nm_acdirmax
= argp
->acdirmax
;
1162 if (nmp
->nm_acregmin
> nmp
->nm_acregmax
)
1163 nmp
->nm_acregmin
= nmp
->nm_acregmax
;
1164 if (nmp
->nm_acdirmin
> nmp
->nm_acdirmax
)
1165 nmp
->nm_acdirmin
= nmp
->nm_acdirmax
;
1168 /* Set up the sockets and per-host congestion */
1169 nmp
->nm_sotype
= argp
->sotype
;
1170 nmp
->nm_soproto
= argp
->proto
;
1172 /* make sure mbuf constants are set up */
1177 * For Connection based sockets (TCP,...) defer the connect until
1178 * the first request, in case the server is not responding.
1180 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
1181 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
1185 * Get file attributes for the mountpoint. These are needed
1186 * in order to properly create the root vnode.
1188 // LP64todo - fix CAST_DOWN of argp->fh
1189 error
= nfs_getattr_no_vnode(mp
, CAST_DOWN(caddr_t
, argp
->fh
), argp
->fhsize
,
1190 proc_ucred(p
), p
, &nvattrs
, &xid
);
1193 * we got problems... we couldn't get the attributes
1194 * from the NFS server... so the mount fails.
1200 * A reference count is needed on the nfsnode representing the
1201 * remote root. If this object is not persistent, then backward
1202 * traversals of the mount point (i.e. "..") will not work if
1203 * the nfsnode gets flushed out of the cache. UFS does not have
1204 * this problem, because one can identify root inodes by their
1205 * number == ROOTINO (2).
1207 error
= nfs_nget(mp
, NULL
, NULL
, CAST_DOWN(caddr_t
, argp
->fh
), argp
->fhsize
,
1208 &nvattrs
, &xid
, NG_MARKROOT
, &np
);
1213 * save this vnode pointer. That way nfs_unmount()
1214 * does not need to call nfs_nget() just get it to drop
1215 * this vnode reference.
1217 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
1218 /* get usecount and drop iocount */
1219 error
= vnode_ref(*vpp
);
1227 * Set the mount point's block I/O size.
1228 * We really need to do this after we get info back from
1229 * the server about what its preferred I/O sizes are.
1231 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
1232 nfs_fsinfo(nmp
, *vpp
, proc_ucred(p
), p
);
1233 vfs_statfs(mp
)->f_iosize
= nfs_iosize(nmp
);
1236 * V3 mounts give us a (relatively) reliable remote access(2)
1237 * call, so advertise the fact.
1239 * XXX this may not be the best way to go, as the granularity
1240 * offered isn't a good match to our needs.
1242 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
1243 vfs_setauthopaqueaccess(mp
);
1246 * Do statfs to ensure static info gets set to reasonable values.
1248 context
.vc_proc
= p
;
1249 context
.vc_ucred
= proc_ucred(p
);
1250 nfs_statfs(mp
, vfs_statfs(mp
), &context
);
1252 if (nmp
->nm_flag
& NFSMNT_RESVPORT
)
1254 nmp
->nm_state
|= NFSSTA_MOUNTED
;
1257 nfs_disconnect(nmp
);
1258 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1265 * unmount system call
1271 __unused vfs_context_t context
)
1273 register struct nfsmount
*nmp
;
1275 int error
, flags
= 0;
1279 * During a force unmount we want to...
1280 * Mark that we are doing a force unmount.
1281 * Make the mountpoint soft.
1283 if (mntflags
& MNT_FORCE
) {
1284 flags
|= FORCECLOSE
;
1285 nmp
->nm_state
|= NFSSTA_FORCE
;
1286 nmp
->nm_flag
|= NFSMNT_SOFT
;
1289 * Goes something like this..
1290 * - Call vflush() to clear out vnodes for this file system,
1291 * except for the swap files. Deal with them in 2nd pass.
1292 * - Decrement reference on the vnode representing remote root.
1293 * - Close the socket
1294 * - Free up the data structures
1299 * vflush will check for busy vnodes on mountpoint.
1300 * Will do the right thing for MNT_FORCE. That is, we should
1301 * not get EBUSY back.
1303 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
1304 if (mntflags
& MNT_FORCE
) {
1305 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
1307 if (vnode_isinuse(vp
, 1))
1309 error
= vflush(mp
, vp
, flags
);
1314 nmp
->nm_state
&= ~NFSSTA_MOUNTED
;
1315 if (nmp
->nm_flag
& NFSMNT_RESVPORT
) {
1316 if (--nfs_resv_mounts
== 0)
1317 nfs_bind_resv_thread_wake();
1321 * Release the root vnode reference held by mountnfs()
1325 (void)vflush(mp
, NULLVP
, FORCECLOSE
);
1326 vfs_setfsprivate(mp
, 0); /* don't want to end up using stale vp */
1328 nfs_disconnect(nmp
);
1329 mbuf_freem(nmp
->nm_nam
);
1331 if ((nmp
->nm_flag
& NFSMNT_KERB
) == 0) {
1334 * Loop through outstanding request list and remove dangling
1335 * references to defunct nfsmount struct
1337 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1338 if (rp
->r_nmp
== nmp
)
1339 rp
->r_nmp
= (struct nfsmount
*)0;
1340 /* Need to wake up any rcvlock waiters so they notice the unmount. */
1341 if (nmp
->nm_state
& NFSSTA_WANTRCV
) {
1342 nmp
->nm_state
&= ~NFSSTA_WANTRCV
;
1343 wakeup(&nmp
->nm_state
);
1345 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1351 * Return root of a filesystem
1354 nfs_root(mount_t mp
, vnode_t
*vpp
, __unused vfs_context_t context
)
1357 struct nfsmount
*nmp
;
1363 vpid
= vnode_vid(vp
);
1364 while ((error
= vnode_getwithvid(vp
, vpid
))) {
1365 /* vnode_get() may return ENOENT if the dir changes. */
1366 /* If that happens, just try it again, else return the error. */
1367 if ((error
!= ENOENT
) || (vnode_vid(vp
) == vpid
))
1369 vpid
= vnode_vid(vp
);
1376 * Flush out the buffer cache
1379 struct nfs_sync_cargs
{
1380 vfs_context_t context
;
1386 nfs_sync_callout(vnode_t vp
, void *arg
)
1388 struct nfs_sync_cargs
*cargs
= (struct nfs_sync_cargs
*)arg
;
1391 if (LIST_EMPTY(&VTONFS(vp
)->n_dirtyblkhd
))
1392 return (VNODE_RETURNED
);
1393 if (VTONFS(vp
)->n_flag
& NWRBUSY
)
1394 return (VNODE_RETURNED
);
1396 error
= nfs_flush(vp
, cargs
->waitfor
,
1397 vfs_context_ucred(cargs
->context
),
1398 vfs_context_proc(cargs
->context
), 0);
1400 cargs
->error
= error
;
1402 return (VNODE_RETURNED
);
1406 nfs_sync(mount_t mp
, int waitfor
, vfs_context_t context
)
1408 struct nfs_sync_cargs cargs
;
1410 cargs
.waitfor
= waitfor
;
1411 cargs
.context
= context
;
1414 vnode_iterate(mp
, 0, nfs_sync_callout
, &cargs
);
1416 return (cargs
.error
);
1420 * NFS flat namespace lookup.
1421 * Currently unsupported.
1426 __unused mount_t mp
,
1427 __unused ino64_t ino
,
1428 __unused vnode_t
*vpp
,
1429 __unused vfs_context_t context
)
1436 * At this point, this should never happen
1441 __unused mount_t mp
,
1443 __unused
unsigned char *fhp
,
1444 __unused vnode_t
*vpp
,
1445 __unused vfs_context_t context
)
1452 * Vnode pointer to File handle, should never happen either
1457 __unused vnode_t vp
,
1458 __unused
int *fhlenp
,
1459 __unused
unsigned char *fhp
,
1460 __unused vfs_context_t context
)
1467 * Vfs start routine, a no-op.
1472 __unused mount_t mp
,
1474 __unused vfs_context_t context
)
1481 * Do that sysctl thang...
1484 nfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1485 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1488 struct sysctl_req
*req
= NULL
;
1490 struct user_vfsidctl user_vc
;
1492 struct nfsmount
*nmp
= NULL
;
1494 boolean_t is_64_bit
;
1497 * All names at this level are terminal.
1500 return ENOTDIR
; /* overloaded */
1502 is_64_bit
= vfs_context_is64bit(context
);
1504 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1508 case VFS_CTL_NOLOCKS
:
1509 req
= CAST_DOWN(struct sysctl_req
*, oldp
);
1511 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
1514 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
1517 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
1520 mp
= vfs_getvfs(&vc
.vc_fsid
);
1527 bzero(&vq
, sizeof(vq
));
1530 req
->newptr
= user_vc
.vc_ptr
;
1531 req
->newlen
= (size_t)user_vc
.vc_len
;
1534 req
->newptr
= CAST_USER_ADDR_T(vc
.vc_ptr
);
1535 req
->newlen
= vc
.vc_len
;
1542 *oldlenp
= sizeof nfsstats
;
1546 if(*oldlenp
< sizeof nfsstats
) {
1547 *oldlenp
= sizeof nfsstats
;
1551 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1555 if(newp
&& newlen
!= sizeof nfsstats
)
1559 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
1562 case VFS_CTL_NOLOCKS
:
1563 val
= (nmp
->nm_flag
& NFSMNT_NOLOCKS
) ? 1 : 0;
1564 if (req
->oldptr
!= USER_ADDR_NULL
) {
1565 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
1569 if (req
->newptr
!= USER_ADDR_NULL
) {
1570 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
1574 nmp
->nm_flag
|= NFSMNT_NOLOCKS
;
1576 nmp
->nm_flag
&= ~NFSMNT_NOLOCKS
;
1580 if (nmp
->nm_state
& NFSSTA_TIMEO
)
1581 vq
.vq_flags
|= VQ_NOTRESP
;
1582 if (!(nmp
->nm_flag
& NFSMNT_NOLOCKS
) &&
1583 (nmp
->nm_state
& NFSSTA_LOCKTIMEO
))
1584 vq
.vq_flags
|= VQ_NOTRESPLOCK
;
1585 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
1588 if (req
->oldptr
!= USER_ADDR_NULL
) {
1589 error
= SYSCTL_OUT(req
, &nmp
->nm_tprintf_initial_delay
,
1590 sizeof(nmp
->nm_tprintf_initial_delay
));
1594 if (req
->newptr
!= USER_ADDR_NULL
) {
1595 error
= SYSCTL_IN(req
, &nmp
->nm_tprintf_initial_delay
,
1596 sizeof(nmp
->nm_tprintf_initial_delay
));
1599 if (nmp
->nm_tprintf_initial_delay
< 0)
1600 nmp
->nm_tprintf_initial_delay
= 0;