2 * Copyright (c) 2000-2002 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>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
83 #include <sys/vmparam.h>
85 #if !defined(NO_MOUNT_PRIVATE)
86 #include <sys/filedesc.h>
87 #endif /* NO_MOUNT_PRIVATE */
90 #include <net/route.h>
91 #include <netinet/in.h>
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
96 #include <nfs/nfsnode.h>
97 #include <nfs/nfsmount.h>
98 #include <nfs/xdr_subs.h>
99 #include <nfs/nfsm_subs.h>
100 #include <nfs/nfsdiskless.h>
101 #include <nfs/nqnfs.h>
103 extern int nfs_mountroot
__P((void));
105 extern int nfs_ticks
;
107 struct nfsstats nfsstats
;
108 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
110 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
112 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
113 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
114 &nfsstats
, nfsstats
, "");
118 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
120 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
124 static int nfs_iosize
__P((struct nfsmount
*nmp
));
125 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
126 struct mbuf
*,char *,char *,struct vnode
**));
127 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
128 struct nameidata
*ndp
, struct proc
*p
));
129 static int nfs_start
__P(( struct mount
*mp
, int flags
,
131 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
133 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
134 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
135 caddr_t arg
, struct proc
*p
));
136 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
138 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
139 struct ucred
*cred
, struct proc
*p
));
140 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
141 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
142 struct mbuf
*nam
, struct vnode
**vpp
,
143 int *exflagsp
, struct ucred
**credanonp
));
144 static int nfs_vget
__P((struct mount
*, ino_t
, struct vnode
**));
148 * nfs vfs operations.
150 struct vfsops nfs_vfsops
= {
164 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
166 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
170 void nfsargs_ntoh
__P((struct nfs_args
*));
172 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
174 #if !defined(NO_MOUNT_PRIVATE)
176 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
177 struct vnode
**, struct mount
**));
178 #endif /* NO_MOUNT_PRIVATE */
179 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
180 struct onfs_args
*oargs
));
182 int nfsreqqusers
= 0;
183 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
186 static int nfs_iosize(nmp
)
187 struct nfsmount
* nmp
;
192 * Calculate the size used for io buffers. Use the larger
193 * of the two sizes to minimise nfs requests but make sure
194 * that it is at least one VM page to avoid wasting buffer
197 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
198 if (iosize
< PAGE_SIZE
)
200 return (trunc_page_32(iosize
));
203 static void nfs_convert_oargs(args
,oargs
)
204 struct nfs_args
*args
;
205 struct onfs_args
*oargs
;
207 args
->version
= NFS_ARGSVERSION
;
208 args
->addr
= oargs
->addr
;
209 args
->addrlen
= oargs
->addrlen
;
210 args
->sotype
= oargs
->sotype
;
211 args
->proto
= oargs
->proto
;
212 args
->fh
= oargs
->fh
;
213 args
->fhsize
= oargs
->fhsize
;
214 args
->flags
= oargs
->flags
;
215 args
->wsize
= oargs
->wsize
;
216 args
->rsize
= oargs
->rsize
;
217 args
->readdirsize
= oargs
->readdirsize
;
218 args
->timeo
= oargs
->timeo
;
219 args
->retrans
= oargs
->retrans
;
220 args
->maxgrouplist
= oargs
->maxgrouplist
;
221 args
->readahead
= oargs
->readahead
;
222 args
->leaseterm
= oargs
->leaseterm
;
223 args
->deadthresh
= oargs
->deadthresh
;
224 args
->hostname
= oargs
->hostname
;
231 nfs_statfs(mp
, sbp
, p
)
233 register struct statfs
*sbp
;
236 register struct vnode
*vp
;
237 register struct nfs_statfs
*sfp
;
240 register long t1
, t2
;
241 caddr_t bpos
, dpos
, cp2
;
242 struct nfsmount
*nmp
= VFSTONFS(mp
);
243 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
244 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
247 extern int nfs_mount_type
;
251 sfp
= (struct nfs_statfs
*)0;
254 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
257 cred
->cr_ngroups
= 1;
258 if (v3
&& (nmp
->nm_flag
& NFSMNT_GOTFSINFO
) == 0)
259 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
260 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
261 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
263 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
265 nfsm_postop_attr(vp
, retattr
, &xid
);
266 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
268 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
270 sbp
->f_type
= MOUNT_NFS
;
272 sbp
->f_type
= nfs_mount_type
;
274 sbp
->f_flags
= nmp
->nm_flag
;
275 sbp
->f_iosize
= nfs_iosize(nmp
);
277 sbp
->f_bsize
= NFS_FABLKSIZE
;
278 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
279 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
280 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
281 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
282 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
283 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
284 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
286 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
289 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
290 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
291 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
292 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
296 if (sbp
!= &mp
->mnt_stat
) {
297 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
298 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
301 VOP_UNLOCK(vp
, 0, p
);
307 * nfs version 3 fsinfo rpc call
310 nfs_fsinfo(nmp
, vp
, cred
, p
)
311 register struct nfsmount
*nmp
;
312 register struct vnode
*vp
;
316 register struct nfsv3_fsinfo
*fsp
;
318 register long t1
, t2
;
319 register u_long
*tl
, pref
, max
;
320 caddr_t bpos
, dpos
, cp2
;
321 int error
= 0, retattr
;
322 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
325 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
326 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
328 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
329 nfsm_postop_attr(vp
, retattr
, &xid
);
331 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
332 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
333 if (pref
< nmp
->nm_wsize
)
334 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
335 ~(NFS_FABLKSIZE
- 1);
336 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
337 if (max
< nmp
->nm_wsize
) {
338 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
339 if (nmp
->nm_wsize
== 0)
342 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
343 if (pref
< nmp
->nm_rsize
)
344 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
345 ~(NFS_FABLKSIZE
- 1);
346 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
347 if (max
< nmp
->nm_rsize
) {
348 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
349 if (nmp
->nm_rsize
== 0)
352 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
353 if (pref
< nmp
->nm_readdirsize
)
354 nmp
->nm_readdirsize
= pref
;
355 if (max
< nmp
->nm_readdirsize
) {
356 nmp
->nm_readdirsize
= max
;
358 nmp
->nm_flag
|= NFSMNT_GOTFSINFO
;
365 * Mount a remote root fs via. nfs. This depends on the info in the
366 * nfs_diskless structure that has been filled in properly by some primary
368 * It goes something like this:
369 * - do enough of "ifconfig" by calling ifioctl() so that the system
370 * can talk to the server
371 * - If nfs_diskless.mygateway is filled in, use that address as
373 * - hand craft the swap nfs vnode hanging off a fake mount point
374 * if swdevt[0].sw_dev == NODEV
375 * - build the rootfs mount point and call mountnfs() to do the rest.
380 struct nfs_diskless nd
;
387 #if !defined(NO_MOUNT_PRIVATE)
388 struct mount
*mppriv
;
389 struct vnode
*vppriv
;
390 #endif /* NO_MOUNT_PRIVATE */
393 procp
= current_proc(); /* XXX */
396 * Call nfs_boot_init() to fill in the nfs_diskless struct.
397 * Note: networking must already have been configured before
400 bzero((caddr_t
) &nd
, sizeof(nd
));
401 error
= nfs_boot_init(&nd
, procp
);
403 panic("nfs_boot_init failed with %d\n", error
);
406 /* try NFSv3 first, if that fails then try NFSv2 */
410 error
= nfs_boot_getfh(&nd
, procp
, v3
);
413 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error
);
417 panic("nfs_boot_getfh(v2) failed with %d\n", error
);
421 * Create the root mount point.
423 #if !defined(NO_MOUNT_PRIVATE)
424 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
426 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
427 #endif /* NO_MOUNT_PRIVATE */
429 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error
);
433 panic("nfs_mount_diskless root failed with %d\n", error
);
435 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
437 simple_lock(&mountlist_slock
);
438 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
439 simple_unlock(&mountlist_slock
);
440 vfs_unbusy(mp
, procp
);
443 #if !defined(NO_MOUNT_PRIVATE)
444 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
445 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
446 NULL
, &vppriv
, &mppriv
);
448 panic("nfs_mount_diskless private failed with %d\n", error
);
450 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
452 simple_lock(&mountlist_slock
);
453 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
454 simple_unlock(&mountlist_slock
);
455 vfs_unbusy(mppriv
, procp
);
458 #endif /* NO_MOUNT_PRIVATE */
460 if (nd
.nd_root
.ndm_path
)
461 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
462 if (nd
.nd_private
.ndm_path
)
463 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
465 /* Get root attributes (for the time). */
466 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
467 if (error
) panic("nfs_mountroot: getattr for root");
468 n
= attr
.va_mtime
.tv_sec
;
474 * Internal version of mount system call for diskless setup.
477 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
478 struct nfs_dlmount
*ndmntp
;
484 struct nfs_args args
;
490 procp
= current_proc(); /* XXX */
492 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
493 printf("nfs_mountroot: NFS not configured");
496 mp
->mnt_flag
= mntflag
;
498 /* Initialize mount args. */
499 bzero((caddr_t
) &args
, sizeof(args
));
500 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
501 args
.addrlen
= args
.addr
->sa_len
;
502 args
.sotype
= SOCK_DGRAM
;
503 args
.fh
= ndmntp
->ndm_fh
;
504 args
.fhsize
= ndmntp
->ndm_fhlen
;
505 args
.hostname
= ndmntp
->ndm_host
;
506 args
.flags
= NFSMNT_RESVPORT
;
507 if (ndmntp
->ndm_nfsv3
)
508 args
.flags
|= NFSMNT_NFSV3
;
510 MGET(m
, M_DONTWAIT
, MT_SONAME
);
511 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
512 (m
->m_len
= args
.addr
->sa_len
));
513 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
514 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
515 mp
->mnt_vfc
->vfc_refcount
--;
516 vfs_unbusy(mp
, procp
);
517 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
520 #if 0 /* Causes incorrect reporting of "mounted on" */
521 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
527 #if !defined(NO_MOUNT_PRIVATE)
529 * Internal version of mount system call to mount "/private"
530 * separately in diskless setup
533 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
534 struct nfs_dlmount
*ndmntp
;
540 struct nfs_args args
;
545 struct vfsconf
*vfsp
;
549 procp
= current_proc(); /* XXX */
553 * mimic main()!. Temporarily set up rootvnode and other stuff so
554 * that namei works. Need to undo this because main() does it, too
556 struct filedesc
*fdp
; /* pointer to file descriptor state */
558 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
560 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
561 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
562 panic("cannot find root vnode");
564 fdp
->fd_cdir
= rootvnode
;
565 VOP_UNLOCK(rootvnode
, 0, procp
);
570 * Get vnode to be covered
572 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
574 if ((error
= namei(&nd
))) {
575 printf("nfs_mountroot: private namei failed!");
579 /* undo VREF in mimic main()! */
583 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
587 if (vp
->v_type
!= VDIR
) {
591 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
592 if (!strcmp(vfsp
->vfc_name
, "nfs"))
595 printf("nfs_mountroot: private NFS not configured");
599 if (vp
->v_mountedhere
!= NULL
) {
605 * Allocate and initialize the filesystem.
607 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
608 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
610 /* Initialize the default IO constraints */
611 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
612 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
614 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
615 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
616 LIST_INIT(&mp
->mnt_vnodelist
);
617 mp
->mnt_op
= vfsp
->vfc_vfsops
;
619 vfsp
->vfc_refcount
++;
620 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
621 mp
->mnt_flag
= mntflag
;
622 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
623 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
624 vp
->v_mountedhere
= mp
;
625 mp
->mnt_vnodecovered
= vp
;
626 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
627 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
628 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
630 /* Initialize mount args. */
631 bzero((caddr_t
) &args
, sizeof(args
));
632 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
633 args
.addrlen
= args
.addr
->sa_len
;
634 args
.sotype
= SOCK_DGRAM
;
635 args
.fh
= ndmntp
->ndm_fh
;
636 args
.fhsize
= ndmntp
->ndm_fhlen
;
637 args
.hostname
= ndmntp
->ndm_host
;
638 args
.flags
= NFSMNT_RESVPORT
;
639 if (ndmntp
->ndm_nfsv3
)
640 args
.flags
|= NFSMNT_NFSV3
;
642 MGET(m
, M_DONTWAIT
, MT_SONAME
);
643 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
644 (m
->m_len
= args
.addr
->sa_len
));
645 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
646 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
647 mp
->mnt_vfc
->vfc_refcount
--;
648 vfs_unbusy(mp
, procp
);
649 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
657 #endif /* NO_MOUNT_PRIVATE */
663 * It seems a bit dumb to copyinstr() the host and path here and then
664 * bcopy() them in mountnfs(), but I wanted to detect errors before
665 * doing the sockargs() call because sockargs() allocates an mbuf and
666 * an error after that means that I have to release the mbuf.
670 nfs_mount(mp
, path
, data
, ndp
, p
)
674 struct nameidata
*ndp
;
678 struct nfs_args args
;
681 char pth
[MNAMELEN
], hst
[MNAMELEN
];
683 u_char nfh
[NFSX_V3FHMAX
];
685 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
688 if (args
.version
!= NFS_ARGSVERSION
) {
689 #ifndef NO_COMPAT_PRELITE2
691 * If the argument version is unknown, then assume the
692 * caller is a pre-lite2 4.4BSD client and convert its
695 struct onfs_args oargs
;
696 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
699 nfs_convert_oargs(&args
,&oargs
);
700 #else /* NO_COMPAT_PRELITE2 */
701 return (EPROGMISMATCH
);
702 #endif /* !NO_COMPAT_PRELITE2 */
704 if (args
.fhsize
> NFSX_V3FHMAX
)
706 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
709 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
712 bzero(&pth
[len
], MNAMELEN
- len
);
713 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
716 bzero(&hst
[len
], MNAMELEN
- len
);
717 /* sockargs() call must be after above copyin() calls */
718 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
722 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
727 * Common code for mount and mountroot
730 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
731 register struct nfs_args
*argp
;
732 register struct mount
*mp
;
737 register struct nfsmount
*nmp
;
741 struct proc
*curproc
;
744 * turning off NQNFS until we have further testing
745 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
746 * Those have NQNFS defined out in conjunction with this
747 * returning an error. Remove when fully tested.
749 if (argp
->flags
& NFSMNT_NQNFS
) {
750 error
= NFSERR_NOTSUPP
;
754 if (mp
->mnt_flag
& MNT_UPDATE
) {
756 /* update paths, file handles, etc, here XXX */
760 MALLOC_ZONE(nmp
, struct nfsmount
*,
761 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
762 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
763 TAILQ_INIT(&nmp
->nm_uidlruhead
);
764 TAILQ_INIT(&nmp
->nm_bufq
);
765 mp
->mnt_data
= (qaddr_t
)nmp
;
769 nmp
->nm_flag
= argp
->flags
;
770 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
772 * We have to set mnt_maxsymlink to a non-zero value so
773 * that COMPAT_43 routines will know that we are setting
774 * the d_type field in directories (and can zero it for
775 * unsuspecting binaries).
777 mp
->mnt_maxsymlinklen
= 1;
778 nmp
->nm_timeo
= NFS_TIMEO
;
779 nmp
->nm_retry
= NFS_RETRANS
;
780 nmp
->nm_wsize
= NFS_WSIZE
;
781 nmp
->nm_rsize
= NFS_RSIZE
;
782 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
783 nmp
->nm_numgrps
= NFS_MAXGRPS
;
784 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
785 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
786 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
787 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
788 nmp
->nm_inprog
= NULLVP
;
789 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
790 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
794 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
795 * no sense in that context.
797 if (argp
->sotype
== SOCK_STREAM
)
798 argp
->flags
&= ~NFSMNT_NOCONN
;
800 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
801 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
802 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
803 nmp
->nm_timeo
= NFS_MINTIMEO
;
804 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
805 nmp
->nm_timeo
= NFS_MAXTIMEO
;
808 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
809 nmp
->nm_retry
= argp
->retrans
;
810 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
811 nmp
->nm_retry
= NFS_MAXREXMIT
;
814 if (argp
->flags
& NFSMNT_NFSV3
) {
815 if (argp
->sotype
== SOCK_DGRAM
)
816 maxio
= NFS_MAXDGRAMDATA
;
820 maxio
= NFS_V2MAXDATA
;
822 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
823 nmp
->nm_wsize
= argp
->wsize
;
824 /* Round down to multiple of blocksize */
825 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
826 if (nmp
->nm_wsize
<= 0)
827 nmp
->nm_wsize
= NFS_FABLKSIZE
;
829 if (nmp
->nm_wsize
> maxio
)
830 nmp
->nm_wsize
= maxio
;
831 if (nmp
->nm_wsize
> MAXBSIZE
)
832 nmp
->nm_wsize
= MAXBSIZE
;
834 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
835 nmp
->nm_rsize
= argp
->rsize
;
836 /* Round down to multiple of blocksize */
837 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
838 if (nmp
->nm_rsize
<= 0)
839 nmp
->nm_rsize
= NFS_FABLKSIZE
;
841 if (nmp
->nm_rsize
> maxio
)
842 nmp
->nm_rsize
= maxio
;
843 if (nmp
->nm_rsize
> MAXBSIZE
)
844 nmp
->nm_rsize
= MAXBSIZE
;
846 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
847 nmp
->nm_readdirsize
= argp
->readdirsize
;
849 if (nmp
->nm_readdirsize
> maxio
)
850 nmp
->nm_readdirsize
= maxio
;
851 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
852 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
854 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
855 argp
->maxgrouplist
<= NFS_MAXGRPS
)
856 nmp
->nm_numgrps
= argp
->maxgrouplist
;
857 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
858 argp
->readahead
<= NFS_MAXRAHEAD
)
859 nmp
->nm_readahead
= argp
->readahead
;
860 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
861 argp
->leaseterm
<= NQ_MAXLEASE
)
862 nmp
->nm_leaseterm
= argp
->leaseterm
;
863 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
864 argp
->deadthresh
<= NQ_NEVERDEAD
)
865 nmp
->nm_deadthresh
= argp
->deadthresh
;
866 /* Set up the sockets and per-host congestion */
867 nmp
->nm_sotype
= argp
->sotype
;
868 nmp
->nm_soproto
= argp
->proto
;
871 * For Connection based sockets (TCP,...) defer the connect until
872 * the first request, in case the server is not responding.
874 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
875 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
879 * This is silly, but it has to be set so that vinifod() works.
880 * We do not want to do an nfs_statfs() here since we can get
881 * stuck on a dead server and we are holding a lock on the mount
884 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
886 * A reference count is needed on the nfsnode representing the
887 * remote root. If this object is not persistent, then backward
888 * traversals of the mount point (i.e. "..") will not work if
889 * the nfsnode gets flushed out of the cache. UFS does not have
890 * this problem, because one can identify root inodes by their
891 * number == ROOTINO (2).
893 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
898 * save this vnode pointer. That way nfs_unmount()
899 * does not need to call nfs_net() just get it to drop
900 * this vnode reference.
902 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
905 * Get file attributes for the mountpoint. This has the side
906 * effect of filling in (*vpp)->v_type with the correct value.
908 curproc
= current_proc();
909 VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
912 * Lose the lock but keep the ref.
914 VOP_UNLOCK(*vpp
, 0, curproc
);
919 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
927 * unmount system call
930 nfs_unmount(mp
, mntflags
, p
)
935 register struct nfsmount
*nmp
;
937 int error
, flags
= 0;
939 if (mntflags
& MNT_FORCE
)
943 * Goes something like this..
944 * - Call vflush() to clear out vnodes for this file system,
945 * except for the swap files. Deal with them in 2nd pass.
946 * It will do vgone making the vnode VBAD at that time.
947 * - Decrement reference on the vnode representing remote root.
949 * - Free up the data structures
954 * Must handshake with nqnfs_clientd() if it is active.
956 nmp
->nm_flag
|= NFSMNT_DISMINPROG
;
957 while (nmp
->nm_inprog
!= NULLVP
)
958 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
960 * vflush will check for busy vnodes on mountpoint.
961 * Will do the right thing for MNT_FORCE. That is, we should
962 * not get EBUSY back.
964 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
965 if (mntflags
& MNT_FORCE
)
966 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
968 if (vp
->v_usecount
> 1) {
969 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
972 error
= vflush(mp
, vp
, flags
);
976 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
981 * We are now committed to the unmount.
982 * For NQNFS, let the server daemon free the nfsmount structure.
984 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
985 nmp
->nm_flag
|= NFSMNT_DISMNT
;
988 * Release the root vnode reference held by mountnfs()
989 * vflush did the vgone for us when we didn't skip over
990 * it in the MNT_FORCE case. (Thus vp can't be locked when
991 * called vflush in non-skip vp case.)
994 if (!(mntflags
& MNT_FORCE
))
996 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
998 m_freem(nmp
->nm_nam
);
1000 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
1001 register struct nfsreq
*rp
;
1003 * Loop through outstanding request list and remove dangling
1004 * references to defunct nfsmount struct
1007 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1008 nfsatompanic("unmount add");
1009 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1010 nfsbtcpu
= cpu_number();
1011 nfsbtthread
= (int)(current_thread());
1014 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1015 if (rp
->r_nmp
== nmp
)
1016 rp
->r_nmp
= (struct nfsmount
*)0;
1018 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1019 nfsatompanic("unmount sub");
1021 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1027 * Return root of a filesystem
1034 register struct vnode
*vp
;
1035 struct nfsmount
*nmp
;
1040 error
= vget(vp
, LK_EXCLUSIVE
, current_proc());
1043 if (vp
->v_type
== VNON
)
1045 vp
->v_flag
|= VROOT
;
1053 * Flush out the buffer cache
1057 nfs_sync(mp
, waitfor
, cred
, p
)
1063 register struct vnode
*vp
;
1064 int error
, allerror
= 0;
1067 * Force stale buffer cache information to be flushed.
1070 for (vp
= mp
->mnt_vnodelist
.lh_first
;
1072 vp
= vp
->v_mntvnodes
.le_next
) {
1075 * If the vnode that we are about to sync is no longer
1076 * associated with this mount point, start over.
1078 if (vp
->v_mount
!= mp
)
1080 if (VOP_ISLOCKED(vp
) || vp
->v_dirtyblkhd
.lh_first
== NULL
)
1082 if (vget(vp
, LK_EXCLUSIVE
, p
))
1084 didhold
= ubc_hold(vp
);
1085 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1088 VOP_UNLOCK(vp
, 0, p
);
1097 * NFS flat namespace lookup.
1098 * Currently unsupported.
1102 nfs_vget(mp
, ino
, vpp
)
1108 return (EOPNOTSUPP
);
1112 * At this point, this should never happen
1116 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1117 register struct mount
*mp
;
1122 struct ucred
**credanonp
;
1129 * Vnode pointer to File handle, should never happen either
1142 * Vfs start routine, a no-op.
1146 nfs_start(mp
, flags
, p
)
1156 * Do operations associated with quotas, not supported
1160 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1168 return (EOPNOTSUPP
);
1172 * Do that sysctl thang...
1175 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1176 size_t newlen
, struct proc
*p
)
1181 * All names at this level are terminal.
1184 return ENOTDIR
; /* overloaded */
1189 *oldlenp
= sizeof nfsstats
;
1193 if(*oldlenp
< sizeof nfsstats
) {
1194 *oldlenp
= sizeof nfsstats
;
1198 rv
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1201 if(newp
&& newlen
!= sizeof nfsstats
)
1205 return copyin(newp
, &nfsstats
, sizeof nfsstats
);