2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
59 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
65 #include <sys/ioctl.h>
66 #include <sys/signal.h>
68 #include <sys/namei.h>
69 #include <sys/vnode.h>
70 #include <sys/malloc.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/mount.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
80 #include <sys/vmparam.h>
82 #if !defined(NO_MOUNT_PRIVATE)
83 #include <sys/filedesc.h>
84 #endif /* NO_MOUNT_PRIVATE */
87 #include <net/route.h>
88 #include <netinet/in.h>
90 #include <nfs/rpcv2.h>
91 #include <nfs/nfsproto.h>
93 #include <nfs/nfsnode.h>
94 #include <nfs/nfsmount.h>
95 #include <nfs/xdr_subs.h>
96 #include <nfs/nfsm_subs.h>
97 #include <nfs/nfsdiskless.h>
98 #include <nfs/nqnfs.h>
100 extern int nfs_mountroot
__P((void));
102 extern int nfs_ticks
;
104 struct nfsstats nfsstats
;
105 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
107 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
109 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
110 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
111 &nfsstats
, nfsstats
, "");
115 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
117 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
121 static int nfs_iosize
__P((struct nfsmount
*nmp
));
122 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
123 struct mbuf
*,char *,char *,struct vnode
**));
124 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
125 struct nameidata
*ndp
, struct proc
*p
));
126 static int nfs_start
__P(( struct mount
*mp
, int flags
,
128 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
130 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
131 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
132 caddr_t arg
, struct proc
*p
));
133 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
135 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
136 struct ucred
*cred
, struct proc
*p
));
137 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
138 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
139 struct mbuf
*nam
, struct vnode
**vpp
,
140 int *exflagsp
, struct ucred
**credanonp
));
141 static int nfs_vget
__P((struct mount
*, ino_t
, struct vnode
**));
145 * nfs vfs operations.
147 struct vfsops nfs_vfsops
= {
161 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
163 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
167 void nfsargs_ntoh
__P((struct nfs_args
*));
169 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
171 #if !defined(NO_MOUNT_PRIVATE)
173 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
174 struct vnode
**, struct mount
**));
175 #endif /* NO_MOUNT_PRIVATE */
176 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
177 struct onfs_args
*oargs
));
179 int nfsreqqusers
= 0;
180 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
183 static int nfs_iosize(nmp
)
184 struct nfsmount
* nmp
;
189 * Calculate the size used for io buffers. Use the larger
190 * of the two sizes to minimise nfs requests but make sure
191 * that it is at least one VM page to avoid wasting buffer
194 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
195 if (iosize
< PAGE_SIZE
)
197 return (trunc_page(iosize
));
200 static void nfs_convert_oargs(args
,oargs
)
201 struct nfs_args
*args
;
202 struct onfs_args
*oargs
;
204 args
->version
= NFS_ARGSVERSION
;
205 args
->addr
= oargs
->addr
;
206 args
->addrlen
= oargs
->addrlen
;
207 args
->sotype
= oargs
->sotype
;
208 args
->proto
= oargs
->proto
;
209 args
->fh
= oargs
->fh
;
210 args
->fhsize
= oargs
->fhsize
;
211 args
->flags
= oargs
->flags
;
212 args
->wsize
= oargs
->wsize
;
213 args
->rsize
= oargs
->rsize
;
214 args
->readdirsize
= oargs
->readdirsize
;
215 args
->timeo
= oargs
->timeo
;
216 args
->retrans
= oargs
->retrans
;
217 args
->maxgrouplist
= oargs
->maxgrouplist
;
218 args
->readahead
= oargs
->readahead
;
219 args
->leaseterm
= oargs
->leaseterm
;
220 args
->deadthresh
= oargs
->deadthresh
;
221 args
->hostname
= oargs
->hostname
;
228 nfs_statfs(mp
, sbp
, p
)
230 register struct statfs
*sbp
;
233 register struct vnode
*vp
;
234 register struct nfs_statfs
*sfp
;
237 register long t1
, t2
;
238 caddr_t bpos
, dpos
, cp2
;
239 struct nfsmount
*nmp
= VFSTONFS(mp
);
240 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
241 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
244 extern int nfs_mount_type
;
248 sfp
= (struct nfs_statfs
*)0;
251 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
254 cred
->cr_ngroups
= 1;
255 if (v3
&& (nmp
->nm_flag
& NFSMNT_GOTFSINFO
) == 0)
256 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
257 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
258 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
260 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
262 nfsm_postop_attr(vp
, retattr
, &xid
);
263 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
265 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
267 sbp
->f_type
= MOUNT_NFS
;
269 sbp
->f_type
= nfs_mount_type
;
271 sbp
->f_flags
= nmp
->nm_flag
;
272 sbp
->f_iosize
= nfs_iosize(nmp
);
274 sbp
->f_bsize
= NFS_FABLKSIZE
;
275 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
276 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
277 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
278 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
279 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
280 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
281 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
283 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
286 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
287 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
288 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
289 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
293 if (sbp
!= &mp
->mnt_stat
) {
294 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
295 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
298 VOP_UNLOCK(vp
, 0, p
);
304 * nfs version 3 fsinfo rpc call
307 nfs_fsinfo(nmp
, vp
, cred
, p
)
308 register struct nfsmount
*nmp
;
309 register struct vnode
*vp
;
313 register struct nfsv3_fsinfo
*fsp
;
315 register long t1
, t2
;
316 register u_long
*tl
, pref
, max
;
317 caddr_t bpos
, dpos
, cp2
;
318 int error
= 0, retattr
;
319 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
322 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
323 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
325 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
326 nfsm_postop_attr(vp
, retattr
, &xid
);
328 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
329 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
330 if (pref
< nmp
->nm_wsize
)
331 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
332 ~(NFS_FABLKSIZE
- 1);
333 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
334 if (max
< nmp
->nm_wsize
) {
335 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
336 if (nmp
->nm_wsize
== 0)
339 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
340 if (pref
< nmp
->nm_rsize
)
341 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
342 ~(NFS_FABLKSIZE
- 1);
343 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
344 if (max
< nmp
->nm_rsize
) {
345 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
346 if (nmp
->nm_rsize
== 0)
349 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
350 if (pref
< nmp
->nm_readdirsize
)
351 nmp
->nm_readdirsize
= pref
;
352 if (max
< nmp
->nm_readdirsize
) {
353 nmp
->nm_readdirsize
= max
;
355 nmp
->nm_flag
|= NFSMNT_GOTFSINFO
;
362 * Mount a remote root fs via. nfs. This depends on the info in the
363 * nfs_diskless structure that has been filled in properly by some primary
365 * It goes something like this:
366 * - do enough of "ifconfig" by calling ifioctl() so that the system
367 * can talk to the server
368 * - If nfs_diskless.mygateway is filled in, use that address as
370 * - hand craft the swap nfs vnode hanging off a fake mount point
371 * if swdevt[0].sw_dev == NODEV
372 * - build the rootfs mount point and call mountnfs() to do the rest.
377 struct nfs_diskless nd
;
384 #if !defined(NO_MOUNT_PRIVATE)
385 struct mount
*mppriv
;
386 struct vnode
*vppriv
;
387 #endif /* NO_MOUNT_PRIVATE */
390 procp
= current_proc(); /* XXX */
393 * Call nfs_boot_init() to fill in the nfs_diskless struct.
394 * Note: networking must already have been configured before
397 bzero((caddr_t
) &nd
, sizeof(nd
));
398 error
= nfs_boot_init(&nd
, procp
);
400 panic("nfs_boot_init failed with %d\n", error
);
403 /* try NFSv3 first, if that fails then try NFSv2 */
407 error
= nfs_boot_getfh(&nd
, procp
, v3
);
410 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error
);
414 panic("nfs_boot_getfh(v2) failed with %d\n", error
);
418 * Create the root mount point.
420 #if !defined(NO_MOUNT_PRIVATE)
421 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
423 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
424 #endif /* NO_MOUNT_PRIVATE */
426 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error
);
430 panic("nfs_mount_diskless root failed with %d\n", error
);
432 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
434 simple_lock(&mountlist_slock
);
435 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
436 simple_unlock(&mountlist_slock
);
437 vfs_unbusy(mp
, procp
);
440 #if !defined(NO_MOUNT_PRIVATE)
441 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
442 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
443 NULL
, &vppriv
, &mppriv
);
445 panic("nfs_mount_diskless private failed with %d\n", error
);
447 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
449 simple_lock(&mountlist_slock
);
450 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
451 simple_unlock(&mountlist_slock
);
452 vfs_unbusy(mppriv
, procp
);
455 #endif /* NO_MOUNT_PRIVATE */
457 if (nd
.nd_root
.ndm_path
)
458 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
459 if (nd
.nd_private
.ndm_path
)
460 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
462 /* Get root attributes (for the time). */
463 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
464 if (error
) panic("nfs_mountroot: getattr for root");
465 n
= attr
.va_mtime
.tv_sec
;
471 * Internal version of mount system call for diskless setup.
474 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
475 struct nfs_dlmount
*ndmntp
;
481 struct nfs_args args
;
487 procp
= current_proc(); /* XXX */
489 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
490 printf("nfs_mountroot: NFS not configured");
493 mp
->mnt_flag
= mntflag
;
495 /* Initialize mount args. */
496 bzero((caddr_t
) &args
, sizeof(args
));
497 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
498 args
.addrlen
= args
.addr
->sa_len
;
499 args
.sotype
= SOCK_DGRAM
;
500 args
.fh
= ndmntp
->ndm_fh
;
501 args
.fhsize
= ndmntp
->ndm_fhlen
;
502 args
.hostname
= ndmntp
->ndm_host
;
503 args
.flags
= NFSMNT_RESVPORT
;
504 if (ndmntp
->ndm_nfsv3
)
505 args
.flags
|= NFSMNT_NFSV3
;
507 MGET(m
, M_DONTWAIT
, MT_SONAME
);
508 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
509 (m
->m_len
= args
.addr
->sa_len
));
510 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
511 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
512 mp
->mnt_vfc
->vfc_refcount
--;
513 vfs_unbusy(mp
, procp
);
514 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
517 #if 0 /* Causes incorrect reporting of "mounted on" */
518 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
524 #if !defined(NO_MOUNT_PRIVATE)
526 * Internal version of mount system call to mount "/private"
527 * separately in diskless setup
530 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
531 struct nfs_dlmount
*ndmntp
;
537 struct nfs_args args
;
542 struct vfsconf
*vfsp
;
546 procp
= current_proc(); /* XXX */
550 * mimic main()!. Temporarily set up rootvnode and other stuff so
551 * that namei works. Need to undo this because main() does it, too
553 struct filedesc
*fdp
; /* pointer to file descriptor state */
555 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
557 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
558 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
559 panic("cannot find root vnode");
561 fdp
->fd_cdir
= rootvnode
;
562 VOP_UNLOCK(rootvnode
, 0, procp
);
567 * Get vnode to be covered
569 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
571 if ((error
= namei(&nd
))) {
572 printf("nfs_mountroot: private namei failed!");
576 /* undo VREF in mimic main()! */
580 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
584 if (vp
->v_type
!= VDIR
) {
588 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
589 if (!strcmp(vfsp
->vfc_name
, "nfs"))
592 printf("nfs_mountroot: private NFS not configured");
596 if (vp
->v_mountedhere
!= NULL
) {
602 * Allocate and initialize the filesystem.
604 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
605 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
607 /* Initialize the default IO constraints */
608 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
609 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
611 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
612 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
613 LIST_INIT(&mp
->mnt_vnodelist
);
614 mp
->mnt_op
= vfsp
->vfc_vfsops
;
616 vfsp
->vfc_refcount
++;
617 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
618 mp
->mnt_flag
= mntflag
;
619 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
620 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
621 vp
->v_mountedhere
= mp
;
622 mp
->mnt_vnodecovered
= vp
;
623 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
624 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
625 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
627 /* Initialize mount args. */
628 bzero((caddr_t
) &args
, sizeof(args
));
629 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
630 args
.addrlen
= args
.addr
->sa_len
;
631 args
.sotype
= SOCK_DGRAM
;
632 args
.fh
= ndmntp
->ndm_fh
;
633 args
.fhsize
= ndmntp
->ndm_fhlen
;
634 args
.hostname
= ndmntp
->ndm_host
;
635 args
.flags
= NFSMNT_RESVPORT
;
636 if (ndmntp
->ndm_nfsv3
)
637 args
.flags
|= NFSMNT_NFSV3
;
639 MGET(m
, M_DONTWAIT
, MT_SONAME
);
640 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
641 (m
->m_len
= args
.addr
->sa_len
));
642 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
643 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
644 mp
->mnt_vfc
->vfc_refcount
--;
645 vfs_unbusy(mp
, procp
);
646 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
654 #endif /* NO_MOUNT_PRIVATE */
660 * It seems a bit dumb to copyinstr() the host and path here and then
661 * bcopy() them in mountnfs(), but I wanted to detect errors before
662 * doing the sockargs() call because sockargs() allocates an mbuf and
663 * an error after that means that I have to release the mbuf.
667 nfs_mount(mp
, path
, data
, ndp
, p
)
671 struct nameidata
*ndp
;
675 struct nfs_args args
;
678 char pth
[MNAMELEN
], hst
[MNAMELEN
];
680 u_char nfh
[NFSX_V3FHMAX
];
682 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
685 if (args
.version
!= NFS_ARGSVERSION
) {
686 #ifndef NO_COMPAT_PRELITE2
688 * If the argument version is unknown, then assume the
689 * caller is a pre-lite2 4.4BSD client and convert its
692 struct onfs_args oargs
;
693 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
696 nfs_convert_oargs(&args
,&oargs
);
697 #else /* NO_COMPAT_PRELITE2 */
698 return (EPROGMISMATCH
);
699 #endif /* !NO_COMPAT_PRELITE2 */
701 if (args
.fhsize
> NFSX_V3FHMAX
)
703 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
706 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
709 bzero(&pth
[len
], MNAMELEN
- len
);
710 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
713 bzero(&hst
[len
], MNAMELEN
- len
);
714 /* sockargs() call must be after above copyin() calls */
715 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
719 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
724 * Common code for mount and mountroot
727 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
728 register struct nfs_args
*argp
;
729 register struct mount
*mp
;
734 register struct nfsmount
*nmp
;
738 struct proc
*curproc
;
741 * turning off NQNFS until we have further testing
742 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
743 * Those have NQNFS defined out in conjunction with this
744 * returning an error. Remove when fully tested.
746 if (argp
->flags
& NFSMNT_NQNFS
) {
747 error
= NFSERR_NOTSUPP
;
751 if (mp
->mnt_flag
& MNT_UPDATE
) {
753 /* update paths, file handles, etc, here XXX */
757 MALLOC_ZONE(nmp
, struct nfsmount
*,
758 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
759 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
760 TAILQ_INIT(&nmp
->nm_uidlruhead
);
761 TAILQ_INIT(&nmp
->nm_bufq
);
762 mp
->mnt_data
= (qaddr_t
)nmp
;
766 nmp
->nm_flag
= argp
->flags
;
767 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
769 * We have to set mnt_maxsymlink to a non-zero value so
770 * that COMPAT_43 routines will know that we are setting
771 * the d_type field in directories (and can zero it for
772 * unsuspecting binaries).
774 mp
->mnt_maxsymlinklen
= 1;
775 nmp
->nm_timeo
= NFS_TIMEO
;
776 nmp
->nm_retry
= NFS_RETRANS
;
777 nmp
->nm_wsize
= NFS_WSIZE
;
778 nmp
->nm_rsize
= NFS_RSIZE
;
779 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
780 nmp
->nm_numgrps
= NFS_MAXGRPS
;
781 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
782 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
783 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
784 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
785 nmp
->nm_inprog
= NULLVP
;
786 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
787 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
791 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
792 * no sense in that context.
794 if (argp
->sotype
== SOCK_STREAM
)
795 argp
->flags
&= ~NFSMNT_NOCONN
;
797 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
798 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
799 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
800 nmp
->nm_timeo
= NFS_MINTIMEO
;
801 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
802 nmp
->nm_timeo
= NFS_MAXTIMEO
;
805 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
806 nmp
->nm_retry
= argp
->retrans
;
807 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
808 nmp
->nm_retry
= NFS_MAXREXMIT
;
811 if (argp
->flags
& NFSMNT_NFSV3
) {
812 if (argp
->sotype
== SOCK_DGRAM
)
813 maxio
= NFS_MAXDGRAMDATA
;
817 maxio
= NFS_V2MAXDATA
;
819 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
820 nmp
->nm_wsize
= argp
->wsize
;
821 /* Round down to multiple of blocksize */
822 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
823 if (nmp
->nm_wsize
<= 0)
824 nmp
->nm_wsize
= NFS_FABLKSIZE
;
826 if (nmp
->nm_wsize
> maxio
)
827 nmp
->nm_wsize
= maxio
;
828 if (nmp
->nm_wsize
> MAXBSIZE
)
829 nmp
->nm_wsize
= MAXBSIZE
;
831 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
832 nmp
->nm_rsize
= argp
->rsize
;
833 /* Round down to multiple of blocksize */
834 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
835 if (nmp
->nm_rsize
<= 0)
836 nmp
->nm_rsize
= NFS_FABLKSIZE
;
838 if (nmp
->nm_rsize
> maxio
)
839 nmp
->nm_rsize
= maxio
;
840 if (nmp
->nm_rsize
> MAXBSIZE
)
841 nmp
->nm_rsize
= MAXBSIZE
;
843 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
844 nmp
->nm_readdirsize
= argp
->readdirsize
;
846 if (nmp
->nm_readdirsize
> maxio
)
847 nmp
->nm_readdirsize
= maxio
;
848 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
849 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
851 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
852 argp
->maxgrouplist
<= NFS_MAXGRPS
)
853 nmp
->nm_numgrps
= argp
->maxgrouplist
;
854 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
855 argp
->readahead
<= NFS_MAXRAHEAD
)
856 nmp
->nm_readahead
= argp
->readahead
;
857 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
858 argp
->leaseterm
<= NQ_MAXLEASE
)
859 nmp
->nm_leaseterm
= argp
->leaseterm
;
860 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
861 argp
->deadthresh
<= NQ_NEVERDEAD
)
862 nmp
->nm_deadthresh
= argp
->deadthresh
;
863 /* Set up the sockets and per-host congestion */
864 nmp
->nm_sotype
= argp
->sotype
;
865 nmp
->nm_soproto
= argp
->proto
;
868 * For Connection based sockets (TCP,...) defer the connect until
869 * the first request, in case the server is not responding.
871 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
872 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
876 * This is silly, but it has to be set so that vinifod() works.
877 * We do not want to do an nfs_statfs() here since we can get
878 * stuck on a dead server and we are holding a lock on the mount
881 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
883 * A reference count is needed on the nfsnode representing the
884 * remote root. If this object is not persistent, then backward
885 * traversals of the mount point (i.e. "..") will not work if
886 * the nfsnode gets flushed out of the cache. UFS does not have
887 * this problem, because one can identify root inodes by their
888 * number == ROOTINO (2).
890 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
895 * save this vnode pointer. That way nfs_unmount()
896 * does not need to call nfs_net() just get it to drop
897 * this vnode reference.
899 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
902 * Get file attributes for the mountpoint. This has the side
903 * effect of filling in (*vpp)->v_type with the correct value.
905 curproc
= current_proc();
906 VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
909 * Lose the lock but keep the ref.
911 VOP_UNLOCK(*vpp
, 0, curproc
);
916 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
924 * unmount system call
927 nfs_unmount(mp
, mntflags
, p
)
932 register struct nfsmount
*nmp
;
934 int error
, flags
= 0;
936 if (mntflags
& MNT_FORCE
)
940 * Goes something like this..
941 * - Call vflush() to clear out vnodes for this file system,
942 * except for the swap files. Deal with them in 2nd pass.
943 * It will do vgone making the vnode VBAD at that time.
944 * - Decrement reference on the vnode representing remote root.
946 * - Free up the data structures
951 * Must handshake with nqnfs_clientd() if it is active.
953 nmp
->nm_flag
|= NFSMNT_DISMINPROG
;
954 while (nmp
->nm_inprog
!= NULLVP
)
955 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
957 * vflush will check for busy vnodes on mountpoint.
958 * Will do the right thing for MNT_FORCE. That is, we should
959 * not get EBUSY back.
961 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
962 if (mntflags
& MNT_FORCE
)
963 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
965 if (vp
->v_usecount
> 1) {
966 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
969 error
= vflush(mp
, vp
, flags
);
973 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
978 * We are now committed to the unmount.
979 * For NQNFS, let the server daemon free the nfsmount structure.
981 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
982 nmp
->nm_flag
|= NFSMNT_DISMNT
;
985 * Release the root vnode reference held by mountnfs()
986 * vflush did the vgone for us when we didn't skip over
987 * it in the MNT_FORCE case. (Thus vp can't be locked when
988 * called vflush in non-skip vp case.)
991 if (!(mntflags
& MNT_FORCE
))
993 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
995 m_freem(nmp
->nm_nam
);
997 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
998 register struct nfsreq
*rp
;
1000 * Loop through outstanding request list and remove dangling
1001 * references to defunct nfsmount struct
1004 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1005 nfsatompanic("unmount add");
1006 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1007 nfsbtcpu
= cpu_number();
1008 nfsbtthread
= (int)(current_thread());
1011 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1012 if (rp
->r_nmp
== nmp
)
1013 rp
->r_nmp
= (struct nfsmount
*)0;
1015 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1016 nfsatompanic("unmount sub");
1018 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1024 * Return root of a filesystem
1031 register struct vnode
*vp
;
1032 struct nfsmount
*nmp
;
1037 error
= vget(vp
, LK_EXCLUSIVE
, current_proc());
1040 if (vp
->v_type
== VNON
)
1042 vp
->v_flag
|= VROOT
;
1050 * Flush out the buffer cache
1054 nfs_sync(mp
, waitfor
, cred
, p
)
1060 register struct vnode
*vp
;
1061 int error
, allerror
= 0;
1064 * Force stale buffer cache information to be flushed.
1067 for (vp
= mp
->mnt_vnodelist
.lh_first
;
1069 vp
= vp
->v_mntvnodes
.le_next
) {
1072 * If the vnode that we are about to sync is no longer
1073 * associated with this mount point, start over.
1075 if (vp
->v_mount
!= mp
)
1077 if (VOP_ISLOCKED(vp
) || vp
->v_dirtyblkhd
.lh_first
== NULL
)
1079 if (vget(vp
, LK_EXCLUSIVE
, p
))
1081 didhold
= ubc_hold(vp
);
1082 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1085 VOP_UNLOCK(vp
, 0, p
);
1094 * NFS flat namespace lookup.
1095 * Currently unsupported.
1099 nfs_vget(mp
, ino
, vpp
)
1105 return (EOPNOTSUPP
);
1109 * At this point, this should never happen
1113 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1114 register struct mount
*mp
;
1119 struct ucred
**credanonp
;
1126 * Vnode pointer to File handle, should never happen either
1139 * Vfs start routine, a no-op.
1143 nfs_start(mp
, flags
, p
)
1153 * Do operations associated with quotas, not supported
1157 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1165 return (EOPNOTSUPP
);
1169 * Do that sysctl thang...
1172 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1173 size_t newlen
, struct proc
*p
)
1178 * All names at this level are terminal.
1181 return ENOTDIR
; /* overloaded */
1186 *oldlenp
= sizeof nfsstats
;
1190 if(*oldlenp
< sizeof nfsstats
) {
1191 *oldlenp
= sizeof nfsstats
;
1195 rv
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1198 if(newp
&& newlen
!= sizeof nfsstats
)
1202 return copyin(newp
, &nfsstats
, sizeof nfsstats
);