2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1989, 1993, 1995
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
62 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
68 #include <sys/ioctl.h>
69 #include <sys/signal.h>
71 #include <sys/namei.h>
72 #include <sys/vnode.h>
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/mount.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
82 #include <sys/vmparam.h>
84 #if !defined(NO_MOUNT_PRIVATE)
85 #include <sys/filedesc.h>
86 #endif /* NO_MOUNT_PRIVATE */
89 #include <net/route.h>
90 #include <netinet/in.h>
92 #include <nfs/rpcv2.h>
93 #include <nfs/nfsproto.h>
95 #include <nfs/nfsnode.h>
96 #include <nfs/nfsmount.h>
97 #include <nfs/xdr_subs.h>
98 #include <nfs/nfsm_subs.h>
99 #include <nfs/nfsdiskless.h>
100 #include <nfs/nqnfs.h>
102 extern int nfs_mountroot
__P((void));
104 extern int nfs_ticks
;
106 struct nfsstats nfsstats
;
107 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
109 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
111 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
112 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
113 &nfsstats
, nfsstats
, "");
117 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
119 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
123 SYSCTL_DECL(_vfs_generic_nfs
);
124 SYSCTL_NODE(_vfs_generic_nfs
, OID_AUTO
, client
, CTLFLAG_RW
, 0,
126 /* how long NFS will wait before signalling vfs that it's down. */
127 static int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
128 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_INITIAL_DELAY
,
129 initialdowndelay
, CTLFLAG_RW
, &nfs_tprintf_initial_delay
, 0, "");
130 /* how long between console messages "nfs server foo not responding" */
131 static int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
132 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_DELAY
,
133 nextdowndelay
, CTLFLAG_RW
, &nfs_tprintf_delay
, 0, "");
135 static int nfs_iosize
__P((struct nfsmount
*nmp
));
136 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
137 struct mbuf
*,char *,char *,struct vnode
**));
138 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
139 struct nameidata
*ndp
, struct proc
*p
));
140 static int nfs_start
__P(( struct mount
*mp
, int flags
,
142 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
144 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
145 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
146 caddr_t arg
, struct proc
*p
));
147 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
149 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
150 struct ucred
*cred
, struct proc
*p
));
151 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
152 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
153 struct mbuf
*nam
, struct vnode
**vpp
,
154 int *exflagsp
, struct ucred
**credanonp
));
155 static int nfs_vget
__P((struct mount
*, void *, struct vnode
**));
159 * nfs vfs operations.
161 struct vfsops nfs_vfsops
= {
175 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
177 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
181 void nfsargs_ntoh
__P((struct nfs_args
*));
183 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
185 #if !defined(NO_MOUNT_PRIVATE)
187 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
188 struct vnode
**, struct mount
**));
189 #endif /* NO_MOUNT_PRIVATE */
190 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
191 struct onfs_args
*oargs
));
193 int nfsreqqusers
= 0;
194 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
197 static int nfs_iosize(nmp
)
198 struct nfsmount
* nmp
;
203 * Calculate the size used for io buffers. Use the larger
204 * of the two sizes to minimise nfs requests but make sure
205 * that it is at least one VM page to avoid wasting buffer
206 * space and to allow easy mmapping of I/O buffers.
207 * The read/write rpc calls handle the splitting up of
208 * buffers into multiple requests if the buffer size is
209 * larger than the I/O size.
211 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
212 if (iosize
< PAGE_SIZE
)
214 return (trunc_page_32(iosize
));
217 static void nfs_convert_oargs(args
,oargs
)
218 struct nfs_args
*args
;
219 struct onfs_args
*oargs
;
221 args
->version
= NFS_ARGSVERSION
;
222 args
->addr
= oargs
->addr
;
223 args
->addrlen
= oargs
->addrlen
;
224 args
->sotype
= oargs
->sotype
;
225 args
->proto
= oargs
->proto
;
226 args
->fh
= oargs
->fh
;
227 args
->fhsize
= oargs
->fhsize
;
228 args
->flags
= oargs
->flags
;
229 args
->wsize
= oargs
->wsize
;
230 args
->rsize
= oargs
->rsize
;
231 args
->readdirsize
= oargs
->readdirsize
;
232 args
->timeo
= oargs
->timeo
;
233 args
->retrans
= oargs
->retrans
;
234 args
->maxgrouplist
= oargs
->maxgrouplist
;
235 args
->readahead
= oargs
->readahead
;
236 args
->leaseterm
= oargs
->leaseterm
;
237 args
->deadthresh
= oargs
->deadthresh
;
238 args
->hostname
= oargs
->hostname
;
245 nfs_statfs(mp
, sbp
, p
)
247 register struct statfs
*sbp
;
250 register struct vnode
*vp
;
251 register struct nfs_statfs
*sfp
;
254 register long t1
, t2
;
255 caddr_t bpos
, dpos
, cp2
;
256 struct nfsmount
*nmp
= VFSTONFS(mp
);
257 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
258 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
261 extern int nfs_mount_type
;
265 sfp
= (struct nfs_statfs
*)0;
268 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
271 cred
->cr_ngroups
= 1;
272 if (v3
&& (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
273 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
274 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
275 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
277 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
279 nfsm_postop_attr(vp
, retattr
, &xid
);
280 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
282 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
284 sbp
->f_type
= MOUNT_NFS
;
286 sbp
->f_type
= nfs_mount_type
;
288 sbp
->f_flags
= nmp
->nm_flag
;
289 sbp
->f_iosize
= nfs_iosize(nmp
);
291 sbp
->f_bsize
= NFS_FABLKSIZE
;
292 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
293 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
294 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
295 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
296 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
297 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
298 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
300 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
303 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
304 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
305 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
306 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
310 if (sbp
!= &mp
->mnt_stat
) {
311 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
312 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
315 VOP_UNLOCK(vp
, 0, p
);
321 * nfs version 3 fsinfo rpc call
324 nfs_fsinfo(nmp
, vp
, cred
, p
)
325 register struct nfsmount
*nmp
;
326 register struct vnode
*vp
;
330 register struct nfsv3_fsinfo
*fsp
;
332 register long t1
, t2
;
333 register u_long
*tl
, pref
, max
;
334 caddr_t bpos
, dpos
, cp2
;
335 int error
= 0, retattr
;
336 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
339 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
340 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
342 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
343 nfsm_postop_attr(vp
, retattr
, &xid
);
345 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
346 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
347 if (pref
< nmp
->nm_wsize
)
348 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
349 ~(NFS_FABLKSIZE
- 1);
350 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
351 if (max
< nmp
->nm_wsize
) {
352 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
353 if (nmp
->nm_wsize
== 0)
356 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
357 if (pref
< nmp
->nm_rsize
)
358 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
359 ~(NFS_FABLKSIZE
- 1);
360 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
361 if (max
< nmp
->nm_rsize
) {
362 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
363 if (nmp
->nm_rsize
== 0)
366 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
367 if (pref
< nmp
->nm_readdirsize
)
368 nmp
->nm_readdirsize
= pref
;
369 if (max
< nmp
->nm_readdirsize
) {
370 nmp
->nm_readdirsize
= max
;
372 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
379 * Mount a remote root fs via. nfs. This depends on the info in the
380 * nfs_diskless structure that has been filled in properly by some primary
382 * It goes something like this:
383 * - do enough of "ifconfig" by calling ifioctl() so that the system
384 * can talk to the server
385 * - If nfs_diskless.mygateway is filled in, use that address as
387 * - hand craft the swap nfs vnode hanging off a fake mount point
388 * if swdevt[0].sw_dev == NODEV
389 * - build the rootfs mount point and call mountnfs() to do the rest.
394 struct nfs_diskless nd
;
401 #if !defined(NO_MOUNT_PRIVATE)
402 struct mount
*mppriv
;
403 struct vnode
*vppriv
;
404 #endif /* NO_MOUNT_PRIVATE */
407 procp
= current_proc(); /* XXX */
410 * Call nfs_boot_init() to fill in the nfs_diskless struct.
411 * Note: networking must already have been configured before
414 bzero((caddr_t
) &nd
, sizeof(nd
));
415 error
= nfs_boot_init(&nd
, procp
);
417 panic("nfs_boot_init failed with %d\n", error
);
420 /* try NFSv3 first, if that fails then try NFSv2 */
424 error
= nfs_boot_getfh(&nd
, procp
, v3
);
427 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error
);
431 panic("nfs_boot_getfh(v2) failed with %d\n", error
);
435 * Create the root mount point.
437 #if !defined(NO_MOUNT_PRIVATE)
438 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
440 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
441 #endif /* NO_MOUNT_PRIVATE */
443 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error
);
447 panic("nfs_mount_diskless root failed with %d\n", error
);
449 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
451 simple_lock(&mountlist_slock
);
452 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
453 simple_unlock(&mountlist_slock
);
454 vfs_unbusy(mp
, procp
);
457 #if !defined(NO_MOUNT_PRIVATE)
458 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
459 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
460 NULL
, &vppriv
, &mppriv
);
462 panic("nfs_mount_diskless private failed with %d\n", error
);
464 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
466 simple_lock(&mountlist_slock
);
467 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
468 simple_unlock(&mountlist_slock
);
469 vfs_unbusy(mppriv
, procp
);
472 #endif /* NO_MOUNT_PRIVATE */
474 if (nd
.nd_root
.ndm_path
)
475 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
476 if (nd
.nd_private
.ndm_path
)
477 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
479 /* Get root attributes (for the time). */
480 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
481 if (error
) panic("nfs_mountroot: getattr for root");
482 n
= attr
.va_mtime
.tv_sec
;
488 * Internal version of mount system call for diskless setup.
491 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
492 struct nfs_dlmount
*ndmntp
;
498 struct nfs_args args
;
504 procp
= current_proc(); /* XXX */
506 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
507 printf("nfs_mountroot: NFS not configured");
510 mp
->mnt_flag
= mntflag
;
512 /* Initialize mount args. */
513 bzero((caddr_t
) &args
, sizeof(args
));
514 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
515 args
.addrlen
= args
.addr
->sa_len
;
516 args
.sotype
= SOCK_DGRAM
;
517 args
.fh
= ndmntp
->ndm_fh
;
518 args
.fhsize
= ndmntp
->ndm_fhlen
;
519 args
.hostname
= ndmntp
->ndm_host
;
520 args
.flags
= NFSMNT_RESVPORT
;
521 if (ndmntp
->ndm_nfsv3
)
522 args
.flags
|= NFSMNT_NFSV3
;
524 MGET(m
, M_DONTWAIT
, MT_SONAME
);
525 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
526 (m
->m_len
= args
.addr
->sa_len
));
527 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
528 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
529 mp
->mnt_vfc
->vfc_refcount
--;
531 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
532 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
533 vfs_unbusy(mp
, procp
);
535 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
538 #if 0 /* Causes incorrect reporting of "mounted on" */
539 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
545 #if !defined(NO_MOUNT_PRIVATE)
547 * Internal version of mount system call to mount "/private"
548 * separately in diskless setup
551 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
552 struct nfs_dlmount
*ndmntp
;
558 struct nfs_args args
;
563 struct vfsconf
*vfsp
;
567 procp
= current_proc(); /* XXX */
571 * mimic main()!. Temporarily set up rootvnode and other stuff so
572 * that namei works. Need to undo this because main() does it, too
574 struct filedesc
*fdp
; /* pointer to file descriptor state */
576 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
578 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
579 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
580 panic("cannot find root vnode");
582 fdp
->fd_cdir
= rootvnode
;
583 VOP_UNLOCK(rootvnode
, 0, procp
);
588 * Get vnode to be covered
590 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
592 if ((error
= namei(&nd
))) {
593 printf("nfs_mountroot: private namei failed!");
597 /* undo VREF in mimic main()! */
601 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
605 if (vp
->v_type
!= VDIR
) {
609 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
610 if (!strcmp(vfsp
->vfc_name
, "nfs"))
613 printf("nfs_mountroot: private NFS not configured");
617 if (vp
->v_mountedhere
!= NULL
) {
623 * Allocate and initialize the filesystem.
625 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
626 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
628 /* Initialize the default IO constraints */
629 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
630 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
632 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
633 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
634 LIST_INIT(&mp
->mnt_vnodelist
);
635 mp
->mnt_op
= vfsp
->vfc_vfsops
;
637 vfsp
->vfc_refcount
++;
638 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
639 mp
->mnt_flag
= mntflag
;
640 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
641 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
642 vp
->v_mountedhere
= mp
;
643 mp
->mnt_vnodecovered
= vp
;
644 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
645 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
646 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
648 /* Initialize mount args. */
649 bzero((caddr_t
) &args
, sizeof(args
));
650 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
651 args
.addrlen
= args
.addr
->sa_len
;
652 args
.sotype
= SOCK_DGRAM
;
653 args
.fh
= ndmntp
->ndm_fh
;
654 args
.fhsize
= ndmntp
->ndm_fhlen
;
655 args
.hostname
= ndmntp
->ndm_host
;
656 args
.flags
= NFSMNT_RESVPORT
;
657 if (ndmntp
->ndm_nfsv3
)
658 args
.flags
|= NFSMNT_NFSV3
;
660 MGET(m
, M_DONTWAIT
, MT_SONAME
);
661 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
662 (m
->m_len
= args
.addr
->sa_len
));
663 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
664 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
665 mp
->mnt_vfc
->vfc_refcount
--;
667 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
668 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
669 vfs_unbusy(mp
, procp
);
671 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
679 #endif /* NO_MOUNT_PRIVATE */
685 * It seems a bit dumb to copyinstr() the host and path here and then
686 * bcopy() them in mountnfs(), but I wanted to detect errors before
687 * doing the sockargs() call because sockargs() allocates an mbuf and
688 * an error after that means that I have to release the mbuf.
692 nfs_mount(mp
, path
, data
, ndp
, p
)
696 struct nameidata
*ndp
;
700 struct nfs_args args
;
703 char pth
[MNAMELEN
], hst
[MNAMELEN
];
705 u_char nfh
[NFSX_V3FHMAX
];
707 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
710 if (args
.version
!= NFS_ARGSVERSION
) {
711 #ifndef NO_COMPAT_PRELITE2
713 * If the argument version is unknown, then assume the
714 * caller is a pre-lite2 4.4BSD client and convert its
717 struct onfs_args oargs
;
718 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
721 nfs_convert_oargs(&args
,&oargs
);
722 #else /* NO_COMPAT_PRELITE2 */
723 return (EPROGMISMATCH
);
724 #endif /* !NO_COMPAT_PRELITE2 */
726 if (args
.fhsize
> NFSX_V3FHMAX
)
728 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
731 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
734 bzero(&pth
[len
], MNAMELEN
- len
);
735 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
738 bzero(&hst
[len
], MNAMELEN
- len
);
739 /* sockargs() call must be after above copyin() calls */
740 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
744 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
749 * Common code for mount and mountroot
752 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
753 register struct nfs_args
*argp
;
754 register struct mount
*mp
;
759 register struct nfsmount
*nmp
;
763 struct proc
*curproc
;
766 * turning off NQNFS until we have further testing
767 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
768 * Those have NQNFS defined out in conjunction with this
769 * returning an error. Remove when fully tested.
771 if (argp
->flags
& NFSMNT_NQNFS
) {
772 error
= NFSERR_NOTSUPP
;
777 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
778 * no sense in that context.
780 if (argp
->sotype
== SOCK_STREAM
)
781 argp
->flags
&= ~NFSMNT_NOCONN
;
783 if (mp
->mnt_flag
& MNT_UPDATE
) {
785 /* update paths, file handles, etc, here XXX */
789 MALLOC_ZONE(nmp
, struct nfsmount
*,
790 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
791 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
792 TAILQ_INIT(&nmp
->nm_uidlruhead
);
793 TAILQ_INIT(&nmp
->nm_bufq
);
794 mp
->mnt_data
= (qaddr_t
)nmp
;
798 nmp
->nm_flag
= argp
->flags
;
799 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
801 * We have to set mnt_maxsymlink to a non-zero value so
802 * that COMPAT_43 routines will know that we are setting
803 * the d_type field in directories (and can zero it for
804 * unsuspecting binaries).
806 mp
->mnt_maxsymlinklen
= 1;
807 nmp
->nm_timeo
= NFS_TIMEO
;
808 nmp
->nm_retry
= NFS_RETRANS
;
809 if (argp
->sotype
== SOCK_DGRAM
) {
810 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
811 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
813 nmp
->nm_wsize
= NFS_WSIZE
;
814 nmp
->nm_rsize
= NFS_RSIZE
;
816 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
817 nmp
->nm_numgrps
= NFS_MAXGRPS
;
818 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
819 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
820 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
821 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
822 if (nmp
->nm_tprintf_delay
< 0)
823 nmp
->nm_tprintf_delay
= 0;
824 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
825 if (nmp
->nm_tprintf_initial_delay
< 0)
826 nmp
->nm_tprintf_initial_delay
= 0;
827 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
828 nmp
->nm_inprog
= NULLVP
;
829 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
830 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
833 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
834 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
835 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
836 nmp
->nm_timeo
= NFS_MINTIMEO
;
837 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
838 nmp
->nm_timeo
= NFS_MAXTIMEO
;
841 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
842 nmp
->nm_retry
= argp
->retrans
;
843 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
844 nmp
->nm_retry
= NFS_MAXREXMIT
;
847 if (argp
->flags
& NFSMNT_NFSV3
) {
848 if (argp
->sotype
== SOCK_DGRAM
)
849 maxio
= NFS_MAXDGRAMDATA
;
853 maxio
= NFS_V2MAXDATA
;
855 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
856 nmp
->nm_wsize
= argp
->wsize
;
857 /* Round down to multiple of blocksize */
858 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
859 if (nmp
->nm_wsize
<= 0)
860 nmp
->nm_wsize
= NFS_FABLKSIZE
;
862 if (nmp
->nm_wsize
> maxio
)
863 nmp
->nm_wsize
= maxio
;
864 if (nmp
->nm_wsize
> MAXBSIZE
)
865 nmp
->nm_wsize
= MAXBSIZE
;
867 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
868 nmp
->nm_rsize
= argp
->rsize
;
869 /* Round down to multiple of blocksize */
870 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
871 if (nmp
->nm_rsize
<= 0)
872 nmp
->nm_rsize
= NFS_FABLKSIZE
;
874 if (nmp
->nm_rsize
> maxio
)
875 nmp
->nm_rsize
= maxio
;
876 if (nmp
->nm_rsize
> MAXBSIZE
)
877 nmp
->nm_rsize
= MAXBSIZE
;
879 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
880 nmp
->nm_readdirsize
= argp
->readdirsize
;
882 if (nmp
->nm_readdirsize
> maxio
)
883 nmp
->nm_readdirsize
= maxio
;
884 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
885 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
887 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
888 argp
->maxgrouplist
<= NFS_MAXGRPS
)
889 nmp
->nm_numgrps
= argp
->maxgrouplist
;
890 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
891 argp
->readahead
<= NFS_MAXRAHEAD
)
892 nmp
->nm_readahead
= argp
->readahead
;
893 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
894 argp
->leaseterm
<= NQ_MAXLEASE
)
895 nmp
->nm_leaseterm
= argp
->leaseterm
;
896 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
897 argp
->deadthresh
<= NQ_NEVERDEAD
)
898 nmp
->nm_deadthresh
= argp
->deadthresh
;
899 /* Set up the sockets and per-host congestion */
900 nmp
->nm_sotype
= argp
->sotype
;
901 nmp
->nm_soproto
= argp
->proto
;
904 * For Connection based sockets (TCP,...) defer the connect until
905 * the first request, in case the server is not responding.
907 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
908 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
912 * A reference count is needed on the nfsnode representing the
913 * remote root. If this object is not persistent, then backward
914 * traversals of the mount point (i.e. "..") will not work if
915 * the nfsnode gets flushed out of the cache. UFS does not have
916 * this problem, because one can identify root inodes by their
917 * number == ROOTINO (2).
919 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
924 * save this vnode pointer. That way nfs_unmount()
925 * does not need to call nfs_net() just get it to drop
926 * this vnode reference.
928 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
931 * Get file attributes for the mountpoint. This has the side
932 * effect of filling in (*vpp)->v_type with the correct value.
934 curproc
= current_proc();
935 error
= VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
938 * we got problems... we couldn't get the attributes
939 * from the NFS server... so the mount fails.
946 * Set the mount point's block I/O size.
947 * We really need to do this after we get info back from
948 * the server about what its preferred I/O sizes are.
950 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
951 nfs_fsinfo(nmp
, *vpp
, curproc
->p_ucred
, curproc
);
952 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
955 * Lose the lock but keep the ref.
957 VOP_UNLOCK(*vpp
, 0, curproc
);
962 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
970 * unmount system call
973 nfs_unmount(mp
, mntflags
, p
)
978 register struct nfsmount
*nmp
;
980 int error
, flags
= 0;
984 * During a force unmount we want to...
985 * Mark that we are doing a force unmount.
986 * Make the mountpoint soft.
988 if (mntflags
& MNT_FORCE
) {
990 nmp
->nm_state
|= NFSSTA_FORCE
;
991 nmp
->nm_flag
|= NFSMNT_SOFT
;
994 * Goes something like this..
995 * - Call vflush() to clear out vnodes for this file system,
996 * except for the swap files. Deal with them in 2nd pass.
997 * It will do vgone making the vnode VBAD at that time.
998 * - Decrement reference on the vnode representing remote root.
1000 * - Free up the data structures
1005 * Must handshake with nqnfs_clientd() if it is active.
1007 nmp
->nm_state
|= NFSSTA_DISMINPROG
;
1008 while (nmp
->nm_inprog
!= NULLVP
)
1009 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
1011 * vflush will check for busy vnodes on mountpoint.
1012 * Will do the right thing for MNT_FORCE. That is, we should
1013 * not get EBUSY back.
1015 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
1016 if (mntflags
& MNT_FORCE
) {
1017 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
1019 if (vp
->v_usecount
> 1) {
1020 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1023 error
= vflush(mp
, vp
, flags
);
1027 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1032 * We are now committed to the unmount.
1033 * For NQNFS, let the server daemon free the nfsmount structure.
1035 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
1036 nmp
->nm_state
|= NFSSTA_DISMNT
;
1039 * Release the root vnode reference held by mountnfs()
1040 * vflush did the vgone for us when we didn't skip over
1041 * it in the MNT_FORCE case. (Thus vp can't be locked when
1042 * called vflush in non-skip vp case.)
1045 if (!(mntflags
& MNT_FORCE
))
1047 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
1048 nfs_disconnect(nmp
);
1049 m_freem(nmp
->nm_nam
);
1051 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
1052 register struct nfsreq
*rp
;
1054 * Loop through outstanding request list and remove dangling
1055 * references to defunct nfsmount struct
1058 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1059 nfsatompanic("unmount add");
1060 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1061 nfsbtcpu
= cpu_number();
1062 nfsbtthread
= (int)(current_thread());
1065 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1066 if (rp
->r_nmp
== nmp
)
1067 rp
->r_nmp
= (struct nfsmount
*)0;
1069 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1070 nfsatompanic("unmount sub");
1072 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1078 * Return root of a filesystem
1085 register struct vnode
*vp
;
1086 struct nfsmount
*nmp
;
1092 while (error
= vget(vp
, LK_EXCLUSIVE
, current_proc())) {
1093 /* vget may return ENOENT if the dir changes while in vget */
1094 /* If that happens, try vget again, else return the error */
1095 if ((error
!= ENOENT
) || (vp
->v_id
== vpid
))
1099 if (vp
->v_type
== VNON
)
1101 vp
->v_flag
|= VROOT
;
1109 * Flush out the buffer cache
1113 nfs_sync(mp
, waitfor
, cred
, p
)
1119 register struct vnode
*vp
;
1120 int error
, allerror
= 0;
1123 * Force stale buffer cache information to be flushed.
1126 LIST_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
1129 * If the vnode that we are about to sync is no longer
1130 * associated with this mount point, start over.
1132 if (vp
->v_mount
!= mp
)
1134 if (VOP_ISLOCKED(vp
) || LIST_FIRST(&VTONFS(vp
)->n_dirtyblkhd
) == NULL
)
1136 if (vget(vp
, LK_EXCLUSIVE
, p
))
1138 didhold
= ubc_hold(vp
);
1139 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1142 VOP_UNLOCK(vp
, 0, p
);
1151 * NFS flat namespace lookup.
1152 * Currently unsupported.
1156 nfs_vget(mp
, ino
, vpp
)
1158 void *ino
; /* XXX void* or ino_t? */
1162 return (EOPNOTSUPP
);
1166 * At this point, this should never happen
1170 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1171 register struct mount
*mp
;
1176 struct ucred
**credanonp
;
1183 * Vnode pointer to File handle, should never happen either
1196 * Vfs start routine, a no-op.
1200 nfs_start(mp
, flags
, p
)
1210 * Do operations associated with quotas, not supported
1214 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1222 return (EOPNOTSUPP
);
1226 * Do that sysctl thang...
1229 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1230 size_t newlen
, struct proc
*p
)
1233 struct sysctl_req
*req
;
1236 struct nfsmount
*nmp
;
1240 * All names at this level are terminal.
1243 return ENOTDIR
; /* overloaded */
1245 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1250 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
1253 mp
= vfs_getvfs(&vc
.vc_fsid
);
1259 bzero(&vq
, sizeof(vq
));
1260 VCTLTOREQ(&vc
, req
);
1266 *oldlenp
= sizeof nfsstats
;
1270 if(*oldlenp
< sizeof nfsstats
) {
1271 *oldlenp
= sizeof nfsstats
;
1275 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1279 if(newp
&& newlen
!= sizeof nfsstats
)
1283 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
1287 if ((nmp
->nm_state
& NFSSTA_TIMEO
))
1288 vq
.vq_flags
|= VQ_NOTRESP
;
1289 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
1292 if (req
->oldptr
!= NULL
) {
1293 error
= SYSCTL_OUT(req
, &nmp
->nm_tprintf_initial_delay
,
1294 sizeof(nmp
->nm_tprintf_initial_delay
));
1298 if (req
->newptr
!= NULL
) {
1299 error
= SYSCTL_IN(req
, &nmp
->nm_tprintf_initial_delay
,
1300 sizeof(nmp
->nm_tprintf_initial_delay
));
1303 if (nmp
->nm_tprintf_initial_delay
< 0)
1304 nmp
->nm_tprintf_initial_delay
= 0;