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
;
105 extern int nfs_mount_type
;
106 extern int nfs_resv_mounts
;
108 struct nfsstats nfsstats
;
109 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
111 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
113 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
114 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
115 &nfsstats
, nfsstats
, "");
119 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
121 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
125 SYSCTL_DECL(_vfs_generic_nfs
);
126 SYSCTL_NODE(_vfs_generic_nfs
, OID_AUTO
, client
, CTLFLAG_RW
, 0,
128 /* how long NFS will wait before signalling vfs that it's down. */
129 static int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
130 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_INITIAL_DELAY
,
131 initialdowndelay
, CTLFLAG_RW
, &nfs_tprintf_initial_delay
, 0, "");
132 /* how long between console messages "nfs server foo not responding" */
133 static int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
134 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_DELAY
,
135 nextdowndelay
, CTLFLAG_RW
, &nfs_tprintf_delay
, 0, "");
137 static int nfs_iosize
__P((struct nfsmount
*nmp
));
138 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
139 struct mbuf
*,char *,char *,struct vnode
**));
140 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
141 struct nameidata
*ndp
, struct proc
*p
));
142 static int nfs_start
__P(( struct mount
*mp
, int flags
,
144 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
146 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
147 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
148 caddr_t arg
, struct proc
*p
));
149 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
151 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
152 struct ucred
*cred
, struct proc
*p
));
153 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
154 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
155 struct mbuf
*nam
, struct vnode
**vpp
,
156 int *exflagsp
, struct ucred
**credanonp
));
157 static int nfs_vget
__P((struct mount
*, void *, struct vnode
**));
161 * nfs vfs operations.
163 struct vfsops nfs_vfsops
= {
177 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
179 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
183 void nfsargs_ntoh
__P((struct nfs_args
*));
185 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
187 #if !defined(NO_MOUNT_PRIVATE)
189 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
190 struct vnode
**, struct mount
**));
191 #endif /* NO_MOUNT_PRIVATE */
192 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
193 struct onfs_args
*oargs
));
195 int nfsreqqusers
= 0;
196 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
199 static int nfs_iosize(nmp
)
200 struct nfsmount
* nmp
;
205 * Calculate the size used for io buffers. Use the larger
206 * of the two sizes to minimise nfs requests but make sure
207 * that it is at least one VM page to avoid wasting buffer
208 * space and to allow easy mmapping of I/O buffers.
209 * The read/write rpc calls handle the splitting up of
210 * buffers into multiple requests if the buffer size is
211 * larger than the I/O size.
213 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
214 if (iosize
< PAGE_SIZE
)
216 return (trunc_page_32(iosize
));
219 static void nfs_convert_oargs(args
,oargs
)
220 struct nfs_args
*args
;
221 struct onfs_args
*oargs
;
223 args
->version
= NFS_ARGSVERSION
;
224 args
->addr
= oargs
->addr
;
225 args
->addrlen
= oargs
->addrlen
;
226 args
->sotype
= oargs
->sotype
;
227 args
->proto
= oargs
->proto
;
228 args
->fh
= oargs
->fh
;
229 args
->fhsize
= oargs
->fhsize
;
230 args
->flags
= oargs
->flags
;
231 args
->wsize
= oargs
->wsize
;
232 args
->rsize
= oargs
->rsize
;
233 args
->readdirsize
= oargs
->readdirsize
;
234 args
->timeo
= oargs
->timeo
;
235 args
->retrans
= oargs
->retrans
;
236 args
->maxgrouplist
= oargs
->maxgrouplist
;
237 args
->readahead
= oargs
->readahead
;
238 args
->leaseterm
= oargs
->leaseterm
;
239 args
->deadthresh
= oargs
->deadthresh
;
240 args
->hostname
= oargs
->hostname
;
247 nfs_statfs(mp
, sbp
, p
)
249 register struct statfs
*sbp
;
252 register struct vnode
*vp
;
253 register struct nfs_statfs
*sfp
;
256 register long t1
, t2
;
257 caddr_t bpos
, dpos
, cp2
;
258 struct nfsmount
*nmp
= VFSTONFS(mp
);
259 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
260 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
263 extern int nfs_mount_type
;
267 sfp
= (struct nfs_statfs
*)0;
270 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
273 cred
->cr_ngroups
= 1;
274 if (v3
&& (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
275 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
276 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
277 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
279 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
281 nfsm_postop_attr(vp
, retattr
, &xid
);
282 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
284 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
286 sbp
->f_type
= MOUNT_NFS
;
288 sbp
->f_type
= nfs_mount_type
;
290 sbp
->f_flags
= nmp
->nm_flag
;
291 sbp
->f_iosize
= nfs_iosize(nmp
);
293 sbp
->f_bsize
= NFS_FABLKSIZE
;
294 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
295 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
296 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
297 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
298 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
299 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
300 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
302 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
305 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
306 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
307 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
308 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
312 if (sbp
!= &mp
->mnt_stat
) {
313 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
314 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
317 VOP_UNLOCK(vp
, 0, p
);
323 * nfs version 3 fsinfo rpc call
326 nfs_fsinfo(nmp
, vp
, cred
, p
)
327 register struct nfsmount
*nmp
;
328 register struct vnode
*vp
;
332 register struct nfsv3_fsinfo
*fsp
;
334 register long t1
, t2
;
335 register u_long
*tl
, pref
, max
;
336 caddr_t bpos
, dpos
, cp2
;
337 int error
= 0, retattr
;
338 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
341 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
342 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
344 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
345 nfsm_postop_attr(vp
, retattr
, &xid
);
347 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
348 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
349 if (pref
< nmp
->nm_wsize
)
350 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
351 ~(NFS_FABLKSIZE
- 1);
352 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
353 if (max
< nmp
->nm_wsize
) {
354 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
355 if (nmp
->nm_wsize
== 0)
358 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
359 if (pref
< nmp
->nm_rsize
)
360 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
361 ~(NFS_FABLKSIZE
- 1);
362 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
363 if (max
< nmp
->nm_rsize
) {
364 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
365 if (nmp
->nm_rsize
== 0)
368 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
369 if (pref
< nmp
->nm_readdirsize
)
370 nmp
->nm_readdirsize
= pref
;
371 if (max
< nmp
->nm_readdirsize
) {
372 nmp
->nm_readdirsize
= max
;
374 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
381 * Mount a remote root fs via. nfs. This depends on the info in the
382 * nfs_diskless structure that has been filled in properly by some primary
384 * It goes something like this:
385 * - do enough of "ifconfig" by calling ifioctl() so that the system
386 * can talk to the server
387 * - If nfs_diskless.mygateway is filled in, use that address as
389 * - hand craft the swap nfs vnode hanging off a fake mount point
390 * if swdevt[0].sw_dev == NODEV
391 * - build the rootfs mount point and call mountnfs() to do the rest.
396 struct nfs_diskless nd
;
403 #if !defined(NO_MOUNT_PRIVATE)
404 struct mount
*mppriv
;
405 struct vnode
*vppriv
;
406 #endif /* NO_MOUNT_PRIVATE */
409 procp
= current_proc(); /* XXX */
412 * Call nfs_boot_init() to fill in the nfs_diskless struct.
413 * Note: networking must already have been configured before
416 bzero((caddr_t
) &nd
, sizeof(nd
));
417 error
= nfs_boot_init(&nd
, procp
);
419 panic("nfs_boot_init failed with %d\n", error
);
422 /* try NFSv3 first, if that fails then try NFSv2 */
426 error
= nfs_boot_getfh(&nd
, procp
, v3
);
428 if (error
== EHOSTDOWN
|| error
== EHOSTUNREACH
) {
429 if (nd
.nd_root
.ndm_path
)
430 FREE_ZONE(nd
.nd_root
.ndm_path
,
431 MAXPATHLEN
, M_NAMEI
);
432 if (nd
.nd_private
.ndm_path
)
433 FREE_ZONE(nd
.nd_private
.ndm_path
,
434 MAXPATHLEN
, M_NAMEI
);
438 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error
);
442 panic("nfs_boot_getfh(v2) failed with %d\n", error
);
446 * Create the root mount point.
448 #if !defined(NO_MOUNT_PRIVATE)
449 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
451 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
452 #endif /* NO_MOUNT_PRIVATE */
454 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error
);
458 panic("nfs_mount_diskless root failed with %d\n", error
);
460 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
462 simple_lock(&mountlist_slock
);
463 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
464 simple_unlock(&mountlist_slock
);
465 vfs_unbusy(mp
, procp
);
468 #if !defined(NO_MOUNT_PRIVATE)
469 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
470 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
471 NULL
, &vppriv
, &mppriv
);
473 panic("nfs_mount_diskless private failed with %d\n", error
);
475 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
477 simple_lock(&mountlist_slock
);
478 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
479 simple_unlock(&mountlist_slock
);
480 vfs_unbusy(mppriv
, procp
);
483 #endif /* NO_MOUNT_PRIVATE */
485 if (nd
.nd_root
.ndm_path
)
486 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
487 if (nd
.nd_private
.ndm_path
)
488 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
490 /* Get root attributes (for the time). */
491 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
492 if (error
) panic("nfs_mountroot: getattr for root");
493 n
= attr
.va_mtime
.tv_sec
;
499 * Internal version of mount system call for diskless setup.
502 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
503 struct nfs_dlmount
*ndmntp
;
509 struct nfs_args args
;
515 procp
= current_proc(); /* XXX */
517 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
518 printf("nfs_mountroot: NFS not configured");
521 mp
->mnt_flag
= mntflag
;
523 /* Initialize mount args. */
524 bzero((caddr_t
) &args
, sizeof(args
));
525 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
526 args
.addrlen
= args
.addr
->sa_len
;
527 args
.sotype
= SOCK_DGRAM
;
528 args
.fh
= ndmntp
->ndm_fh
;
529 args
.fhsize
= ndmntp
->ndm_fhlen
;
530 args
.hostname
= ndmntp
->ndm_host
;
531 args
.flags
= NFSMNT_RESVPORT
;
532 if (ndmntp
->ndm_nfsv3
)
533 args
.flags
|= NFSMNT_NFSV3
;
535 MGET(m
, M_DONTWAIT
, MT_SONAME
);
536 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
537 (m
->m_len
= args
.addr
->sa_len
));
538 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
539 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
540 mp
->mnt_vfc
->vfc_refcount
--;
542 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
543 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
544 vfs_unbusy(mp
, procp
);
546 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
549 #if 0 /* Causes incorrect reporting of "mounted on" */
550 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
556 #if !defined(NO_MOUNT_PRIVATE)
558 * Internal version of mount system call to mount "/private"
559 * separately in diskless setup
562 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
563 struct nfs_dlmount
*ndmntp
;
569 struct nfs_args args
;
574 struct vfsconf
*vfsp
;
578 procp
= current_proc(); /* XXX */
582 * mimic main()!. Temporarily set up rootvnode and other stuff so
583 * that namei works. Need to undo this because main() does it, too
585 struct filedesc
*fdp
; /* pointer to file descriptor state */
587 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
589 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
590 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
591 panic("cannot find root vnode");
593 fdp
->fd_cdir
= rootvnode
;
594 VOP_UNLOCK(rootvnode
, 0, procp
);
599 * Get vnode to be covered
601 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
603 if ((error
= namei(&nd
))) {
604 printf("nfs_mountroot: private namei failed!");
608 /* undo VREF in mimic main()! */
612 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
616 if (vp
->v_type
!= VDIR
) {
620 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
621 if (!strcmp(vfsp
->vfc_name
, "nfs"))
624 printf("nfs_mountroot: private NFS not configured");
628 if (vp
->v_mountedhere
!= NULL
) {
634 * Allocate and initialize the filesystem.
636 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
637 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
639 /* Initialize the default IO constraints */
640 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
641 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
643 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
644 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
645 LIST_INIT(&mp
->mnt_vnodelist
);
646 mp
->mnt_op
= vfsp
->vfc_vfsops
;
648 vfsp
->vfc_refcount
++;
649 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
650 mp
->mnt_flag
= mntflag
;
651 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
652 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
653 vp
->v_mountedhere
= mp
;
654 mp
->mnt_vnodecovered
= vp
;
655 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
656 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
657 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
659 /* Initialize mount args. */
660 bzero((caddr_t
) &args
, sizeof(args
));
661 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
662 args
.addrlen
= args
.addr
->sa_len
;
663 args
.sotype
= SOCK_DGRAM
;
664 args
.fh
= ndmntp
->ndm_fh
;
665 args
.fhsize
= ndmntp
->ndm_fhlen
;
666 args
.hostname
= ndmntp
->ndm_host
;
667 args
.flags
= NFSMNT_RESVPORT
;
668 if (ndmntp
->ndm_nfsv3
)
669 args
.flags
|= NFSMNT_NFSV3
;
671 MGET(m
, M_DONTWAIT
, MT_SONAME
);
672 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
673 (m
->m_len
= args
.addr
->sa_len
));
674 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
675 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
676 mp
->mnt_vfc
->vfc_refcount
--;
678 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
679 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
680 vfs_unbusy(mp
, procp
);
682 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
690 #endif /* NO_MOUNT_PRIVATE */
696 * It seems a bit dumb to copyinstr() the host and path here and then
697 * bcopy() them in mountnfs(), but I wanted to detect errors before
698 * doing the sockargs() call because sockargs() allocates an mbuf and
699 * an error after that means that I have to release the mbuf.
703 nfs_mount(mp
, path
, data
, ndp
, p
)
707 struct nameidata
*ndp
;
711 struct nfs_args args
;
714 char pth
[MNAMELEN
], hst
[MNAMELEN
];
716 u_char nfh
[NFSX_V3FHMAX
];
718 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
721 if (args
.version
!= NFS_ARGSVERSION
) {
722 #ifndef NO_COMPAT_PRELITE2
724 * If the argument version is unknown, then assume the
725 * caller is a pre-lite2 4.4BSD client and convert its
728 struct onfs_args oargs
;
729 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
732 nfs_convert_oargs(&args
,&oargs
);
733 #else /* NO_COMPAT_PRELITE2 */
734 return (EPROGMISMATCH
);
735 #endif /* !NO_COMPAT_PRELITE2 */
737 if (args
.fhsize
> NFSX_V3FHMAX
)
739 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
742 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
745 bzero(&pth
[len
], MNAMELEN
- len
);
746 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
749 bzero(&hst
[len
], MNAMELEN
- len
);
750 /* sockargs() call must be after above copyin() calls */
751 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
755 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
760 * Common code for mount and mountroot
763 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
764 register struct nfs_args
*argp
;
765 register struct mount
*mp
;
770 register struct nfsmount
*nmp
;
774 struct proc
*curproc
;
777 * turning off NQNFS until we have further testing
778 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
779 * Those have NQNFS defined out in conjunction with this
780 * returning an error. Remove when fully tested.
782 if (argp
->flags
& NFSMNT_NQNFS
) {
783 error
= NFSERR_NOTSUPP
;
788 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
789 * no sense in that context.
791 if (argp
->sotype
== SOCK_STREAM
)
792 argp
->flags
&= ~NFSMNT_NOCONN
;
794 if (mp
->mnt_flag
& MNT_UPDATE
) {
796 /* update paths, file handles, etc, here XXX */
800 MALLOC_ZONE(nmp
, struct nfsmount
*,
801 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
802 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
803 TAILQ_INIT(&nmp
->nm_uidlruhead
);
804 TAILQ_INIT(&nmp
->nm_bufq
);
805 mp
->mnt_data
= (qaddr_t
)nmp
;
809 nmp
->nm_flag
= argp
->flags
;
810 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
812 * We have to set mnt_maxsymlink to a non-zero value so
813 * that COMPAT_43 routines will know that we are setting
814 * the d_type field in directories (and can zero it for
815 * unsuspecting binaries).
817 mp
->mnt_maxsymlinklen
= 1;
818 nmp
->nm_timeo
= NFS_TIMEO
;
819 nmp
->nm_retry
= NFS_RETRANS
;
820 if (argp
->sotype
== SOCK_DGRAM
) {
821 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
822 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
824 nmp
->nm_wsize
= NFS_WSIZE
;
825 nmp
->nm_rsize
= NFS_RSIZE
;
827 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
828 nmp
->nm_numgrps
= NFS_MAXGRPS
;
829 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
830 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
831 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
832 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
833 if (nmp
->nm_tprintf_delay
< 0)
834 nmp
->nm_tprintf_delay
= 0;
835 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
836 if (nmp
->nm_tprintf_initial_delay
< 0)
837 nmp
->nm_tprintf_initial_delay
= 0;
838 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
839 nmp
->nm_inprog
= NULLVP
;
840 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
841 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
844 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
845 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
846 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
847 nmp
->nm_timeo
= NFS_MINTIMEO
;
848 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
849 nmp
->nm_timeo
= NFS_MAXTIMEO
;
852 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
853 nmp
->nm_retry
= argp
->retrans
;
854 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
855 nmp
->nm_retry
= NFS_MAXREXMIT
;
858 if (argp
->flags
& NFSMNT_NFSV3
) {
859 if (argp
->sotype
== SOCK_DGRAM
)
860 maxio
= NFS_MAXDGRAMDATA
;
864 maxio
= NFS_V2MAXDATA
;
866 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
867 nmp
->nm_wsize
= argp
->wsize
;
868 /* Round down to multiple of blocksize */
869 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
870 if (nmp
->nm_wsize
<= 0)
871 nmp
->nm_wsize
= NFS_FABLKSIZE
;
873 if (nmp
->nm_wsize
> maxio
)
874 nmp
->nm_wsize
= maxio
;
875 if (nmp
->nm_wsize
> MAXBSIZE
)
876 nmp
->nm_wsize
= MAXBSIZE
;
878 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
879 nmp
->nm_rsize
= argp
->rsize
;
880 /* Round down to multiple of blocksize */
881 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
882 if (nmp
->nm_rsize
<= 0)
883 nmp
->nm_rsize
= NFS_FABLKSIZE
;
885 if (nmp
->nm_rsize
> maxio
)
886 nmp
->nm_rsize
= maxio
;
887 if (nmp
->nm_rsize
> MAXBSIZE
)
888 nmp
->nm_rsize
= MAXBSIZE
;
890 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
891 nmp
->nm_readdirsize
= argp
->readdirsize
;
893 if (nmp
->nm_readdirsize
> maxio
)
894 nmp
->nm_readdirsize
= maxio
;
895 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
896 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
898 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
899 argp
->maxgrouplist
<= NFS_MAXGRPS
)
900 nmp
->nm_numgrps
= argp
->maxgrouplist
;
901 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
902 argp
->readahead
<= NFS_MAXRAHEAD
)
903 nmp
->nm_readahead
= argp
->readahead
;
904 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
905 argp
->leaseterm
<= NQ_MAXLEASE
)
906 nmp
->nm_leaseterm
= argp
->leaseterm
;
907 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
908 argp
->deadthresh
<= NQ_NEVERDEAD
)
909 nmp
->nm_deadthresh
= argp
->deadthresh
;
910 /* Set up the sockets and per-host congestion */
911 nmp
->nm_sotype
= argp
->sotype
;
912 nmp
->nm_soproto
= argp
->proto
;
915 * For Connection based sockets (TCP,...) defer the connect until
916 * the first request, in case the server is not responding.
918 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
919 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
923 * A reference count is needed on the nfsnode representing the
924 * remote root. If this object is not persistent, then backward
925 * traversals of the mount point (i.e. "..") will not work if
926 * the nfsnode gets flushed out of the cache. UFS does not have
927 * this problem, because one can identify root inodes by their
928 * number == ROOTINO (2).
930 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
935 * save this vnode pointer. That way nfs_unmount()
936 * does not need to call nfs_net() just get it to drop
937 * this vnode reference.
939 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
942 * Get file attributes for the mountpoint. This has the side
943 * effect of filling in (*vpp)->v_type with the correct value.
945 curproc
= current_proc();
946 error
= VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
949 * we got problems... we couldn't get the attributes
950 * from the NFS server... so the mount fails.
957 * Set the mount point's block I/O size.
958 * We really need to do this after we get info back from
959 * the server about what its preferred I/O sizes are.
961 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
962 nfs_fsinfo(nmp
, *vpp
, curproc
->p_ucred
, curproc
);
963 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
966 * Lose the lock but keep the ref.
968 VOP_UNLOCK(*vpp
, 0, curproc
);
970 if (nmp
->nm_flag
& NFSMNT_RESVPORT
)
972 nmp
->nm_state
|= NFSSTA_MOUNTED
;
976 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
984 * unmount system call
987 nfs_unmount(mp
, mntflags
, p
)
992 register struct nfsmount
*nmp
;
994 int error
, flags
= 0;
998 * During a force unmount we want to...
999 * Mark that we are doing a force unmount.
1000 * Make the mountpoint soft.
1002 if (mntflags
& MNT_FORCE
) {
1003 flags
|= FORCECLOSE
;
1004 nmp
->nm_state
|= NFSSTA_FORCE
;
1005 nmp
->nm_flag
|= NFSMNT_SOFT
;
1008 * Goes something like this..
1009 * - Call vflush() to clear out vnodes for this file system,
1010 * except for the swap files. Deal with them in 2nd pass.
1011 * It will do vgone making the vnode VBAD at that time.
1012 * - Decrement reference on the vnode representing remote root.
1013 * - Close the socket
1014 * - Free up the data structures
1019 * Must handshake with nqnfs_clientd() if it is active.
1021 nmp
->nm_state
|= NFSSTA_DISMINPROG
;
1022 while (nmp
->nm_inprog
!= NULLVP
)
1023 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
1025 * vflush will check for busy vnodes on mountpoint.
1026 * Will do the right thing for MNT_FORCE. That is, we should
1027 * not get EBUSY back.
1029 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
1030 if (mntflags
& MNT_FORCE
) {
1031 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
1033 if (vp
->v_usecount
> 1) {
1034 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1037 error
= vflush(mp
, vp
, flags
);
1041 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1046 * We are now committed to the unmount.
1047 * For NQNFS, let the server daemon free the nfsmount structure.
1049 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
1050 nmp
->nm_state
|= NFSSTA_DISMNT
;
1051 nmp
->nm_state
&= ~NFSSTA_MOUNTED
;
1052 if (nmp
->nm_flag
& NFSMNT_RESVPORT
) {
1053 if (--nfs_resv_mounts
== 0)
1054 nfs_bind_resv_thread_wake();
1058 * Release the root vnode reference held by mountnfs()
1059 * vflush did the vgone for us when we didn't skip over
1060 * it in the MNT_FORCE case. (Thus vp can't be locked when
1061 * called vflush in non-skip vp case.)
1064 if (!(mntflags
& MNT_FORCE
))
1066 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
1067 nfs_disconnect(nmp
);
1068 m_freem(nmp
->nm_nam
);
1070 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
1071 register struct nfsreq
*rp
;
1073 * Loop through outstanding request list and remove dangling
1074 * references to defunct nfsmount struct
1077 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1078 nfsatompanic("unmount add");
1079 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1080 nfsbtcpu
= cpu_number();
1081 nfsbtthread
= (int)(current_thread());
1084 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1085 if (rp
->r_nmp
== nmp
)
1086 rp
->r_nmp
= (struct nfsmount
*)0;
1088 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1089 nfsatompanic("unmount sub");
1091 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1097 * Return root of a filesystem
1104 register struct vnode
*vp
;
1105 struct nfsmount
*nmp
;
1111 while (error
= vget(vp
, LK_EXCLUSIVE
, current_proc())) {
1112 /* vget may return ENOENT if the dir changes while in vget */
1113 /* If that happens, try vget again, else return the error */
1114 if ((error
!= ENOENT
) || (vp
->v_id
== vpid
))
1118 if (vp
->v_type
== VNON
)
1120 vp
->v_flag
|= VROOT
;
1128 * Flush out the buffer cache
1132 nfs_sync(mp
, waitfor
, cred
, p
)
1138 register struct vnode
*vp
;
1139 int error
, allerror
= 0;
1142 * Force stale buffer cache information to be flushed.
1145 LIST_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
1148 * If the vnode that we are about to sync is no longer
1149 * associated with this mount point, start over.
1151 if (vp
->v_mount
!= mp
)
1153 if (VOP_ISLOCKED(vp
) || LIST_FIRST(&VTONFS(vp
)->n_dirtyblkhd
) == NULL
)
1155 if (vget(vp
, LK_EXCLUSIVE
, p
))
1157 didhold
= ubc_hold(vp
);
1158 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1161 VOP_UNLOCK(vp
, 0, p
);
1170 * NFS flat namespace lookup.
1171 * Currently unsupported.
1175 nfs_vget(mp
, ino
, vpp
)
1177 void *ino
; /* XXX void* or ino_t? */
1181 return (EOPNOTSUPP
);
1185 * At this point, this should never happen
1189 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1190 register struct mount
*mp
;
1195 struct ucred
**credanonp
;
1202 * Vnode pointer to File handle, should never happen either
1215 * Vfs start routine, a no-op.
1219 nfs_start(mp
, flags
, p
)
1229 * Do operations associated with quotas, not supported
1233 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1241 return (EOPNOTSUPP
);
1245 * Do that sysctl thang...
1248 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1249 size_t newlen
, struct proc
*p
)
1252 struct sysctl_req
*req
;
1255 struct nfsmount
*nmp
;
1259 * All names at this level are terminal.
1262 return ENOTDIR
; /* overloaded */
1264 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1269 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
1272 mp
= vfs_getvfs(&vc
.vc_fsid
);
1278 bzero(&vq
, sizeof(vq
));
1279 VCTLTOREQ(&vc
, req
);
1285 *oldlenp
= sizeof nfsstats
;
1289 if(*oldlenp
< sizeof nfsstats
) {
1290 *oldlenp
= sizeof nfsstats
;
1294 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1298 if(newp
&& newlen
!= sizeof nfsstats
)
1302 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
1306 if ((nmp
->nm_state
& NFSSTA_TIMEO
))
1307 vq
.vq_flags
|= VQ_NOTRESP
;
1308 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
1311 if (req
->oldptr
!= NULL
) {
1312 error
= SYSCTL_OUT(req
, &nmp
->nm_tprintf_initial_delay
,
1313 sizeof(nmp
->nm_tprintf_initial_delay
));
1317 if (req
->newptr
!= NULL
) {
1318 error
= SYSCTL_IN(req
, &nmp
->nm_tprintf_initial_delay
,
1319 sizeof(nmp
->nm_tprintf_initial_delay
));
1322 if (nmp
->nm_tprintf_initial_delay
< 0)
1323 nmp
->nm_tprintf_initial_delay
= 0;