2 * Copyright (c) 2000-2004 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>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
79 #include <sys/vmparam.h>
81 #if !defined(NO_MOUNT_PRIVATE)
82 #include <sys/filedesc.h>
83 #endif /* NO_MOUNT_PRIVATE */
86 #include <net/route.h>
87 #include <netinet/in.h>
89 #include <nfs/rpcv2.h>
90 #include <nfs/nfsproto.h>
92 #include <nfs/nfsnode.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/xdr_subs.h>
95 #include <nfs/nfsm_subs.h>
96 #include <nfs/nfsdiskless.h>
97 #include <nfs/nqnfs.h>
99 extern int nfs_mountroot
__P((void));
101 extern int nfs_ticks
;
102 extern int nfs_mount_type
;
103 extern int nfs_resv_mounts
;
105 struct nfsstats nfsstats
;
106 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
108 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
110 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
111 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
112 &nfsstats
, nfsstats
, "");
116 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
118 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
122 SYSCTL_DECL(_vfs_generic_nfs
);
123 SYSCTL_NODE(_vfs_generic_nfs
, OID_AUTO
, client
, CTLFLAG_RW
, 0,
125 /* how long NFS will wait before signalling vfs that it's down. */
126 static int nfs_tprintf_initial_delay
= NFS_TPRINTF_INITIAL_DELAY
;
127 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_INITIAL_DELAY
,
128 initialdowndelay
, CTLFLAG_RW
, &nfs_tprintf_initial_delay
, 0, "");
129 /* how long between console messages "nfs server foo not responding" */
130 static int nfs_tprintf_delay
= NFS_TPRINTF_DELAY
;
131 SYSCTL_INT(_vfs_generic_nfs_client
, NFS_TPRINTF_DELAY
,
132 nextdowndelay
, CTLFLAG_RW
, &nfs_tprintf_delay
, 0, "");
134 static int nfs_iosize
__P((struct nfsmount
*nmp
));
135 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
136 struct mbuf
*,char *,char *,struct vnode
**));
137 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
138 struct nameidata
*ndp
, struct proc
*p
));
139 static int nfs_start
__P(( struct mount
*mp
, int flags
,
141 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
143 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
144 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
145 caddr_t arg
, struct proc
*p
));
146 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
148 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
149 struct ucred
*cred
, struct proc
*p
));
150 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
151 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
152 struct mbuf
*nam
, struct vnode
**vpp
,
153 int *exflagsp
, struct ucred
**credanonp
));
154 static int nfs_vget
__P((struct mount
*, void *, struct vnode
**));
158 * nfs vfs operations.
160 struct vfsops nfs_vfsops
= {
174 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
176 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
180 void nfsargs_ntoh
__P((struct nfs_args
*));
182 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
184 #if !defined(NO_MOUNT_PRIVATE)
186 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
187 struct vnode
**, struct mount
**));
188 #endif /* NO_MOUNT_PRIVATE */
189 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
190 struct onfs_args
*oargs
));
192 int nfsreqqusers
= 0;
193 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
196 static int nfs_iosize(nmp
)
197 struct nfsmount
* nmp
;
202 * Calculate the size used for io buffers. Use the larger
203 * of the two sizes to minimise nfs requests but make sure
204 * that it is at least one VM page to avoid wasting buffer
205 * space and to allow easy mmapping of I/O buffers.
206 * The read/write rpc calls handle the splitting up of
207 * buffers into multiple requests if the buffer size is
208 * larger than the I/O size.
210 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
211 if (iosize
< PAGE_SIZE
)
213 return (trunc_page_32(iosize
));
216 static void nfs_convert_oargs(args
,oargs
)
217 struct nfs_args
*args
;
218 struct onfs_args
*oargs
;
220 args
->version
= NFS_ARGSVERSION
;
221 args
->addr
= oargs
->addr
;
222 args
->addrlen
= oargs
->addrlen
;
223 args
->sotype
= oargs
->sotype
;
224 args
->proto
= oargs
->proto
;
225 args
->fh
= oargs
->fh
;
226 args
->fhsize
= oargs
->fhsize
;
227 args
->flags
= oargs
->flags
;
228 args
->wsize
= oargs
->wsize
;
229 args
->rsize
= oargs
->rsize
;
230 args
->readdirsize
= oargs
->readdirsize
;
231 args
->timeo
= oargs
->timeo
;
232 args
->retrans
= oargs
->retrans
;
233 args
->maxgrouplist
= oargs
->maxgrouplist
;
234 args
->readahead
= oargs
->readahead
;
235 args
->leaseterm
= oargs
->leaseterm
;
236 args
->deadthresh
= oargs
->deadthresh
;
237 args
->hostname
= oargs
->hostname
;
244 nfs_statfs(mp
, sbp
, p
)
246 register struct statfs
*sbp
;
249 register struct vnode
*vp
;
250 register struct nfs_statfs
*sfp
;
253 register long t1
, t2
;
254 caddr_t bpos
, dpos
, cp2
;
255 struct nfsmount
*nmp
= VFSTONFS(mp
);
256 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
257 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
259 extern int nfs_mount_type
;
263 sfp
= (struct nfs_statfs
*)0;
266 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
269 cred
->cr_ngroups
= 1;
270 if (v3
&& (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
271 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
272 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
273 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
275 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
277 nfsm_postop_attr(vp
, retattr
, &xid
);
278 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
280 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
282 sbp
->f_type
= MOUNT_NFS
;
284 sbp
->f_type
= nfs_mount_type
;
286 sbp
->f_flags
= nmp
->nm_flag
;
287 sbp
->f_iosize
= nfs_iosize(nmp
);
290 * Adjust block size to get total block count to fit in a long.
291 * If we can't increase block size enough, clamp to max long.
293 u_quad_t tquad
, tquad2
, bsize
;
294 bsize
= NFS_FABLKSIZE
;
296 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
298 while ((tquad
& ~0x7fffffff) && (bsize
< 0x40000000)) {
302 sbp
->f_blocks
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
304 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
306 sbp
->f_bfree
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
308 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
310 sbp
->f_bavail
= (tquad
& ~0x7fffffff) ? 0x7fffffff : (long)tquad
;
312 sbp
->f_bsize
= (long)bsize
;
314 /* adjust file slots too... */
315 fxdr_hyper(&sfp
->sf_tfiles
, &tquad
);
316 fxdr_hyper(&sfp
->sf_ffiles
, &tquad2
);
317 while (tquad
& ~0x7fffffff) {
321 sbp
->f_files
= tquad
;
322 sbp
->f_ffree
= tquad2
;
324 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
325 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
326 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
327 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
331 if (sbp
!= &mp
->mnt_stat
) {
332 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
333 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
336 VOP_UNLOCK(vp
, 0, p
);
342 * nfs version 3 fsinfo rpc call
345 nfs_fsinfo(nmp
, vp
, cred
, p
)
346 register struct nfsmount
*nmp
;
347 register struct vnode
*vp
;
351 register struct nfsv3_fsinfo
*fsp
;
353 register long t1
, t2
;
354 register u_long
*tl
, pref
, max
;
355 caddr_t bpos
, dpos
, cp2
;
356 int error
= 0, retattr
;
357 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
360 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
361 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
363 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
365 nfsm_postop_attr(vp
, retattr
, &xid
);
368 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
369 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
370 if (pref
< nmp
->nm_wsize
)
371 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
372 ~(NFS_FABLKSIZE
- 1);
373 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
374 if (max
< nmp
->nm_wsize
) {
375 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
376 if (nmp
->nm_wsize
== 0)
379 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
380 if (pref
< nmp
->nm_rsize
)
381 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
382 ~(NFS_FABLKSIZE
- 1);
383 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
384 if (max
< nmp
->nm_rsize
) {
385 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
386 if (nmp
->nm_rsize
== 0)
389 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
390 if (pref
< nmp
->nm_readdirsize
)
391 nmp
->nm_readdirsize
= pref
;
392 if (max
< nmp
->nm_readdirsize
) {
393 nmp
->nm_readdirsize
= max
;
395 nmp
->nm_state
|= NFSSTA_GOTFSINFO
;
402 * Mount a remote root fs via. nfs. This depends on the info in the
403 * nfs_diskless structure that has been filled in properly by some primary
405 * It goes something like this:
406 * - do enough of "ifconfig" by calling ifioctl() so that the system
407 * can talk to the server
408 * - If nfs_diskless.mygateway is filled in, use that address as
410 * - hand craft the swap nfs vnode hanging off a fake mount point
411 * if swdevt[0].sw_dev == NODEV
412 * - build the rootfs mount point and call mountnfs() to do the rest.
417 struct nfs_diskless nd
;
424 #if !defined(NO_MOUNT_PRIVATE)
425 struct mount
*mppriv
;
426 struct vnode
*vppriv
;
427 #endif /* NO_MOUNT_PRIVATE */
430 procp
= current_proc(); /* XXX */
433 * Call nfs_boot_init() to fill in the nfs_diskless struct.
434 * Note: networking must already have been configured before
437 bzero((caddr_t
) &nd
, sizeof(nd
));
438 error
= nfs_boot_init(&nd
, procp
);
440 panic("nfs_boot_init failed with %d\n", error
);
443 /* try NFSv3 first, if that fails then try NFSv2 */
447 error
= nfs_boot_getfh(&nd
, procp
, v3
);
449 if (error
== EHOSTDOWN
|| error
== EHOSTUNREACH
) {
450 if (nd
.nd_root
.ndm_path
)
451 FREE_ZONE(nd
.nd_root
.ndm_path
,
452 MAXPATHLEN
, M_NAMEI
);
453 if (nd
.nd_private
.ndm_path
)
454 FREE_ZONE(nd
.nd_private
.ndm_path
,
455 MAXPATHLEN
, M_NAMEI
);
459 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error
);
463 panic("nfs_boot_getfh(v2) failed with %d\n", error
);
467 * Create the root mount point.
469 #if !defined(NO_MOUNT_PRIVATE)
470 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
472 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
473 #endif /* NO_MOUNT_PRIVATE */
475 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error
);
479 panic("nfs_mount_diskless root failed with %d\n", error
);
481 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
483 simple_lock(&mountlist_slock
);
484 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
485 simple_unlock(&mountlist_slock
);
486 vfs_unbusy(mp
, procp
);
489 #if !defined(NO_MOUNT_PRIVATE)
490 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
491 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
492 NULL
, &vppriv
, &mppriv
);
494 panic("nfs_mount_diskless private failed with %d\n", error
);
496 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
498 simple_lock(&mountlist_slock
);
499 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
500 simple_unlock(&mountlist_slock
);
501 vfs_unbusy(mppriv
, procp
);
504 #endif /* NO_MOUNT_PRIVATE */
506 if (nd
.nd_root
.ndm_path
)
507 FREE_ZONE(nd
.nd_root
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
508 if (nd
.nd_private
.ndm_path
)
509 FREE_ZONE(nd
.nd_private
.ndm_path
, MAXPATHLEN
, M_NAMEI
);
511 /* Get root attributes (for the time). */
512 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
513 if (error
) panic("nfs_mountroot: getattr for root");
514 n
= attr
.va_mtime
.tv_sec
;
520 * Internal version of mount system call for diskless setup.
523 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
524 struct nfs_dlmount
*ndmntp
;
530 struct nfs_args args
;
536 procp
= current_proc(); /* XXX */
538 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
539 printf("nfs_mountroot: NFS not configured");
542 mp
->mnt_flag
= mntflag
;
544 /* Initialize mount args. */
545 bzero((caddr_t
) &args
, sizeof(args
));
546 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
547 args
.addrlen
= args
.addr
->sa_len
;
548 args
.sotype
= SOCK_DGRAM
;
549 args
.fh
= ndmntp
->ndm_fh
;
550 args
.fhsize
= ndmntp
->ndm_fhlen
;
551 args
.hostname
= ndmntp
->ndm_host
;
552 args
.flags
= NFSMNT_RESVPORT
;
553 if (ndmntp
->ndm_nfsv3
)
554 args
.flags
|= NFSMNT_NFSV3
;
556 MGET(m
, M_DONTWAIT
, MT_SONAME
);
557 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
558 (m
->m_len
= args
.addr
->sa_len
));
559 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
560 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
561 mp
->mnt_vfc
->vfc_refcount
--;
563 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
564 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
565 vfs_unbusy(mp
, procp
);
567 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
570 #if 0 /* Causes incorrect reporting of "mounted on" */
571 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
577 #if !defined(NO_MOUNT_PRIVATE)
579 * Internal version of mount system call to mount "/private"
580 * separately in diskless setup
583 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
584 struct nfs_dlmount
*ndmntp
;
590 struct nfs_args args
;
595 struct vfsconf
*vfsp
;
599 procp
= current_proc(); /* XXX */
603 * mimic main()!. Temporarily set up rootvnode and other stuff so
604 * that namei works. Need to undo this because main() does it, too
606 struct filedesc
*fdp
; /* pointer to file descriptor state */
608 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
610 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
611 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
612 panic("cannot find root vnode");
614 fdp
->fd_cdir
= rootvnode
;
615 VOP_UNLOCK(rootvnode
, 0, procp
);
620 * Get vnode to be covered
622 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
624 if ((error
= namei(&nd
))) {
625 printf("nfs_mountroot: private namei failed!");
629 /* undo VREF in mimic main()! */
633 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
637 if (vp
->v_type
!= VDIR
) {
641 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
642 if (!strcmp(vfsp
->vfc_name
, "nfs"))
645 printf("nfs_mountroot: private NFS not configured");
649 if (vp
->v_mountedhere
!= NULL
) {
655 * Allocate and initialize the filesystem.
657 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
658 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
660 /* Initialize the default IO constraints */
661 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
662 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
664 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
665 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
666 LIST_INIT(&mp
->mnt_vnodelist
);
667 mp
->mnt_op
= vfsp
->vfc_vfsops
;
669 vfsp
->vfc_refcount
++;
670 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
671 mp
->mnt_flag
= mntflag
;
672 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
673 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
674 vp
->v_mountedhere
= mp
;
675 mp
->mnt_vnodecovered
= vp
;
676 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
677 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
678 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
680 /* Initialize mount args. */
681 bzero((caddr_t
) &args
, sizeof(args
));
682 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
683 args
.addrlen
= args
.addr
->sa_len
;
684 args
.sotype
= SOCK_DGRAM
;
685 args
.fh
= ndmntp
->ndm_fh
;
686 args
.fhsize
= ndmntp
->ndm_fhlen
;
687 args
.hostname
= ndmntp
->ndm_host
;
688 args
.flags
= NFSMNT_RESVPORT
;
689 if (ndmntp
->ndm_nfsv3
)
690 args
.flags
|= NFSMNT_NFSV3
;
692 MGET(m
, M_DONTWAIT
, MT_SONAME
);
693 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
694 (m
->m_len
= args
.addr
->sa_len
));
695 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
696 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
697 mp
->mnt_vfc
->vfc_refcount
--;
699 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
700 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
701 vfs_unbusy(mp
, procp
);
703 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
711 #endif /* NO_MOUNT_PRIVATE */
717 * It seems a bit dumb to copyinstr() the host and path here and then
718 * bcopy() them in mountnfs(), but I wanted to detect errors before
719 * doing the sockargs() call because sockargs() allocates an mbuf and
720 * an error after that means that I have to release the mbuf.
724 nfs_mount(mp
, path
, data
, ndp
, p
)
728 struct nameidata
*ndp
;
732 struct nfs_args args
;
735 char pth
[MNAMELEN
], hst
[MNAMELEN
];
737 u_char nfh
[NFSX_V3FHMAX
];
739 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
742 if (args
.version
!= NFS_ARGSVERSION
) {
743 #ifndef NO_COMPAT_PRELITE2
745 * If the argument version is unknown, then assume the
746 * caller is a pre-lite2 4.4BSD client and convert its
749 struct onfs_args oargs
;
750 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
753 nfs_convert_oargs(&args
,&oargs
);
754 #else /* NO_COMPAT_PRELITE2 */
755 return (EPROGMISMATCH
);
756 #endif /* !NO_COMPAT_PRELITE2 */
758 if (args
.fhsize
> NFSX_V3FHMAX
)
760 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
763 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
766 bzero(&pth
[len
], MNAMELEN
- len
);
767 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
770 bzero(&hst
[len
], MNAMELEN
- len
);
771 /* sockargs() call must be after above copyin() calls */
772 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
776 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
781 * Common code for mount and mountroot
784 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
785 register struct nfs_args
*argp
;
786 register struct mount
*mp
;
791 register struct nfsmount
*nmp
;
795 struct proc
*curproc
;
798 * turning off NQNFS until we have further testing
799 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
800 * Those have NQNFS defined out in conjunction with this
801 * returning an error. Remove when fully tested.
803 if (argp
->flags
& NFSMNT_NQNFS
) {
804 error
= NFSERR_NOTSUPP
;
809 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
810 * no sense in that context.
812 if (argp
->sotype
== SOCK_STREAM
)
813 argp
->flags
&= ~NFSMNT_NOCONN
;
815 if (mp
->mnt_flag
& MNT_UPDATE
) {
817 /* update paths, file handles, etc, here XXX */
821 MALLOC_ZONE(nmp
, struct nfsmount
*,
822 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
823 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
824 TAILQ_INIT(&nmp
->nm_uidlruhead
);
825 TAILQ_INIT(&nmp
->nm_bufq
);
826 mp
->mnt_data
= (qaddr_t
)nmp
;
830 nmp
->nm_flag
= argp
->flags
;
831 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
833 * We have to set mnt_maxsymlink to a non-zero value so
834 * that COMPAT_43 routines will know that we are setting
835 * the d_type field in directories (and can zero it for
836 * unsuspecting binaries).
838 mp
->mnt_maxsymlinklen
= 1;
839 nmp
->nm_timeo
= NFS_TIMEO
;
840 nmp
->nm_retry
= NFS_RETRANS
;
841 if (argp
->sotype
== SOCK_DGRAM
) {
842 nmp
->nm_wsize
= NFS_DGRAM_WSIZE
;
843 nmp
->nm_rsize
= NFS_DGRAM_RSIZE
;
845 nmp
->nm_wsize
= NFS_WSIZE
;
846 nmp
->nm_rsize
= NFS_RSIZE
;
848 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
849 nmp
->nm_numgrps
= NFS_MAXGRPS
;
850 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
851 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
852 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
853 nmp
->nm_tprintf_delay
= nfs_tprintf_delay
;
854 if (nmp
->nm_tprintf_delay
< 0)
855 nmp
->nm_tprintf_delay
= 0;
856 nmp
->nm_tprintf_initial_delay
= nfs_tprintf_initial_delay
;
857 if (nmp
->nm_tprintf_initial_delay
< 0)
858 nmp
->nm_tprintf_initial_delay
= 0;
859 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
860 nmp
->nm_inprog
= NULLVP
;
861 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
862 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
865 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
866 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
867 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
868 nmp
->nm_timeo
= NFS_MINTIMEO
;
869 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
870 nmp
->nm_timeo
= NFS_MAXTIMEO
;
873 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
874 nmp
->nm_retry
= argp
->retrans
;
875 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
876 nmp
->nm_retry
= NFS_MAXREXMIT
;
879 if (argp
->flags
& NFSMNT_NFSV3
) {
880 if (argp
->sotype
== SOCK_DGRAM
)
881 maxio
= NFS_MAXDGRAMDATA
;
885 maxio
= NFS_V2MAXDATA
;
887 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
888 nmp
->nm_wsize
= argp
->wsize
;
889 /* Round down to multiple of blocksize */
890 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
891 if (nmp
->nm_wsize
<= 0)
892 nmp
->nm_wsize
= NFS_FABLKSIZE
;
894 if (nmp
->nm_wsize
> maxio
)
895 nmp
->nm_wsize
= maxio
;
896 if (nmp
->nm_wsize
> MAXBSIZE
)
897 nmp
->nm_wsize
= MAXBSIZE
;
899 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
900 nmp
->nm_rsize
= argp
->rsize
;
901 /* Round down to multiple of blocksize */
902 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
903 if (nmp
->nm_rsize
<= 0)
904 nmp
->nm_rsize
= NFS_FABLKSIZE
;
906 if (nmp
->nm_rsize
> maxio
)
907 nmp
->nm_rsize
= maxio
;
908 if (nmp
->nm_rsize
> MAXBSIZE
)
909 nmp
->nm_rsize
= MAXBSIZE
;
911 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
912 nmp
->nm_readdirsize
= argp
->readdirsize
;
914 if (nmp
->nm_readdirsize
> maxio
)
915 nmp
->nm_readdirsize
= maxio
;
916 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
917 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
919 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
920 argp
->maxgrouplist
<= NFS_MAXGRPS
)
921 nmp
->nm_numgrps
= argp
->maxgrouplist
;
922 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
923 argp
->readahead
<= NFS_MAXRAHEAD
)
924 nmp
->nm_readahead
= argp
->readahead
;
925 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
926 argp
->leaseterm
<= NQ_MAXLEASE
)
927 nmp
->nm_leaseterm
= argp
->leaseterm
;
928 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
929 argp
->deadthresh
<= NQ_NEVERDEAD
)
930 nmp
->nm_deadthresh
= argp
->deadthresh
;
931 /* Set up the sockets and per-host congestion */
932 nmp
->nm_sotype
= argp
->sotype
;
933 nmp
->nm_soproto
= argp
->proto
;
936 * For Connection based sockets (TCP,...) defer the connect until
937 * the first request, in case the server is not responding.
939 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
940 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
944 * A reference count is needed on the nfsnode representing the
945 * remote root. If this object is not persistent, then backward
946 * traversals of the mount point (i.e. "..") will not work if
947 * the nfsnode gets flushed out of the cache. UFS does not have
948 * this problem, because one can identify root inodes by their
949 * number == ROOTINO (2).
951 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
956 * save this vnode pointer. That way nfs_unmount()
957 * does not need to call nfs_net() just get it to drop
958 * this vnode reference.
960 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
963 * Get file attributes for the mountpoint. This has the side
964 * effect of filling in (*vpp)->v_type with the correct value.
966 curproc
= current_proc();
967 error
= VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
970 * we got problems... we couldn't get the attributes
971 * from the NFS server... so the mount fails.
978 * Set the mount point's block I/O size.
979 * We really need to do this after we get info back from
980 * the server about what its preferred I/O sizes are.
982 if (nmp
->nm_flag
& NFSMNT_NFSV3
)
983 nfs_fsinfo(nmp
, *vpp
, curproc
->p_ucred
, curproc
);
984 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
987 * Lose the lock but keep the ref.
989 VOP_UNLOCK(*vpp
, 0, curproc
);
991 if (nmp
->nm_flag
& NFSMNT_RESVPORT
)
993 nmp
->nm_state
|= NFSSTA_MOUNTED
;
997 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1005 * unmount system call
1008 nfs_unmount(mp
, mntflags
, p
)
1013 register struct nfsmount
*nmp
;
1015 int error
, flags
= 0;
1019 * During a force unmount we want to...
1020 * Mark that we are doing a force unmount.
1021 * Make the mountpoint soft.
1023 if (mntflags
& MNT_FORCE
) {
1024 flags
|= FORCECLOSE
;
1025 nmp
->nm_state
|= NFSSTA_FORCE
;
1026 nmp
->nm_flag
|= NFSMNT_SOFT
;
1029 * Goes something like this..
1030 * - Call vflush() to clear out vnodes for this file system,
1031 * except for the swap files. Deal with them in 2nd pass.
1032 * It will do vgone making the vnode VBAD at that time.
1033 * - Decrement reference on the vnode representing remote root.
1034 * - Close the socket
1035 * - Free up the data structures
1040 * Must handshake with nqnfs_clientd() if it is active.
1042 nmp
->nm_state
|= NFSSTA_DISMINPROG
;
1043 while (nmp
->nm_inprog
!= NULLVP
)
1044 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
1046 * vflush will check for busy vnodes on mountpoint.
1047 * Will do the right thing for MNT_FORCE. That is, we should
1048 * not get EBUSY back.
1050 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
1051 if (mntflags
& MNT_FORCE
) {
1052 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
1054 if (vp
->v_usecount
> 1) {
1055 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1058 error
= vflush(mp
, vp
, flags
);
1062 nmp
->nm_state
&= ~NFSSTA_DISMINPROG
;
1067 * We are now committed to the unmount.
1068 * For NQNFS, let the server daemon free the nfsmount structure.
1070 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
1071 nmp
->nm_state
|= NFSSTA_DISMNT
;
1072 nmp
->nm_state
&= ~NFSSTA_MOUNTED
;
1073 if (nmp
->nm_flag
& NFSMNT_RESVPORT
) {
1074 if (--nfs_resv_mounts
== 0)
1075 nfs_bind_resv_thread_wake();
1079 * Release the root vnode reference held by mountnfs()
1080 * vflush did the vgone for us when we didn't skip over
1081 * it in the MNT_FORCE case. (Thus vp can't be locked when
1082 * called vflush in non-skip vp case.)
1085 if (!(mntflags
& MNT_FORCE
))
1087 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
1088 nfs_disconnect(nmp
);
1089 m_freem(nmp
->nm_nam
);
1091 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
1092 register struct nfsreq
*rp
;
1094 * Loop through outstanding request list and remove dangling
1095 * references to defunct nfsmount struct
1098 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1099 nfsatompanic("unmount add");
1100 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1101 nfsbtcpu
= cpu_number();
1102 nfsbtthread
= (int)(current_thread());
1105 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1106 if (rp
->r_nmp
== nmp
)
1107 rp
->r_nmp
= (struct nfsmount
*)0;
1109 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1110 nfsatompanic("unmount sub");
1112 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1118 * Return root of a filesystem
1125 register struct vnode
*vp
;
1126 struct nfsmount
*nmp
;
1132 while (error
= vget(vp
, LK_EXCLUSIVE
, current_proc())) {
1133 /* vget may return ENOENT if the dir changes while in vget */
1134 /* If that happens, try vget again, else return the error */
1135 if ((error
!= ENOENT
) || (vp
->v_id
== vpid
))
1139 if (vp
->v_type
== VNON
)
1141 vp
->v_flag
|= VROOT
;
1149 * Flush out the buffer cache
1153 nfs_sync(mp
, waitfor
, cred
, p
)
1159 register struct vnode
*vp
;
1160 int error
, allerror
= 0;
1163 * Force stale buffer cache information to be flushed.
1166 LIST_FOREACH(vp
, &mp
->mnt_vnodelist
, v_mntvnodes
) {
1169 * If the vnode that we are about to sync is no longer
1170 * associated with this mount point, start over.
1172 if (vp
->v_mount
!= mp
)
1174 if (VOP_ISLOCKED(vp
) || LIST_FIRST(&VTONFS(vp
)->n_dirtyblkhd
) == NULL
)
1176 if (vget(vp
, LK_EXCLUSIVE
, p
))
1178 didhold
= ubc_hold(vp
);
1179 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1182 VOP_UNLOCK(vp
, 0, p
);
1191 * NFS flat namespace lookup.
1192 * Currently unsupported.
1196 nfs_vget(mp
, ino
, vpp
)
1198 void *ino
; /* XXX void* or ino_t? */
1202 return (EOPNOTSUPP
);
1206 * At this point, this should never happen
1210 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1211 register struct mount
*mp
;
1216 struct ucred
**credanonp
;
1223 * Vnode pointer to File handle, should never happen either
1236 * Vfs start routine, a no-op.
1240 nfs_start(mp
, flags
, p
)
1250 * Do operations associated with quotas, not supported
1254 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1262 return (EOPNOTSUPP
);
1266 * Do that sysctl thang...
1269 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1270 size_t newlen
, struct proc
*p
)
1273 struct sysctl_req
*req
;
1276 struct nfsmount
*nmp
;
1280 * All names at this level are terminal.
1283 return ENOTDIR
; /* overloaded */
1285 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1289 case VFS_CTL_NOLOCKS
:
1291 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
1294 mp
= vfs_getvfs(&vc
.vc_fsid
);
1300 bzero(&vq
, sizeof(vq
));
1301 VCTLTOREQ(&vc
, req
);
1307 *oldlenp
= sizeof nfsstats
;
1311 if(*oldlenp
< sizeof nfsstats
) {
1312 *oldlenp
= sizeof nfsstats
;
1316 error
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1320 if(newp
&& newlen
!= sizeof nfsstats
)
1324 return copyin(newp
, &nfsstats
, sizeof nfsstats
);
1327 case VFS_CTL_NOLOCKS
:
1328 val
= (nmp
->nm_flag
& NFSMNT_NOLOCKS
) ? 1 : 0;
1329 if (req
->oldptr
!= NULL
) {
1330 error
= SYSCTL_OUT(req
, &val
, sizeof(val
));
1334 if (req
->newptr
!= NULL
) {
1335 error
= SYSCTL_IN(req
, &val
, sizeof(val
));
1339 nmp
->nm_flag
|= NFSMNT_NOLOCKS
;
1341 nmp
->nm_flag
&= ~NFSMNT_NOLOCKS
;
1345 if ((nmp
->nm_state
& NFSSTA_TIMEO
))
1346 vq
.vq_flags
|= VQ_NOTRESP
;
1347 if (!(nmp
->nm_flag
& NFSMNT_NOLOCKS
) &&
1348 (nmp
->nm_state
& NFSSTA_LOCKTIMEO
))
1349 vq
.vq_flags
|= VQ_NOTRESPLOCK
;
1350 error
= SYSCTL_OUT(req
, &vq
, sizeof(vq
));
1353 if (req
->oldptr
!= NULL
) {
1354 error
= SYSCTL_OUT(req
, &nmp
->nm_tprintf_initial_delay
,
1355 sizeof(nmp
->nm_tprintf_initial_delay
));
1359 if (req
->newptr
!= NULL
) {
1360 error
= SYSCTL_IN(req
, &nmp
->nm_tprintf_initial_delay
,
1361 sizeof(nmp
->nm_tprintf_initial_delay
));
1364 if (nmp
->nm_tprintf_initial_delay
< 0)
1365 nmp
->nm_tprintf_initial_delay
= 0;