2 * Copyright (c) 2000 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 $
64 * 23-May-97 Umesh Vaishampayan (umeshv@apple.com)
65 * Added the ability to mount "/private" separately.
66 * Fixed bug which caused incorrect reporting of "mounted on"
67 * directory name in case of nfs root.
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/ioctl.h>
74 #include <sys/signal.h>
76 #include <sys/namei.h>
77 #include <sys/vnode.h>
78 #include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/mount.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
88 #include <sys/vmparam.h>
90 #if !defined(NO_MOUNT_PRIVATE)
91 #include <sys/filedesc.h>
92 #endif /* NO_MOUNT_PRIVATE */
95 #include <net/route.h>
96 #include <netinet/in.h>
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
101 #include <nfs/nfsnode.h>
102 #include <nfs/nfsmount.h>
103 #include <nfs/xdr_subs.h>
104 #include <nfs/nfsm_subs.h>
105 #include <nfs/nfsdiskless.h>
106 #include <nfs/nqnfs.h>
108 extern int nfs_mountroot
__P((void));
110 extern int nfs_ticks
;
112 struct nfsstats nfsstats
;
113 static int nfs_sysctl(int *, u_int
, void *, size_t *, void *, size_t,
115 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
117 SYSCTL_NODE(_vfs
, MOUNT_NFS
, nfs
, CTLFLAG_RW
, 0, "NFS filesystem");
118 SYSCTL_STRUCT(_vfs_nfs
, NFS_NFSSTATS
, nfsstats
, CTLFLAG_RD
,
119 &nfsstats
, nfsstats
, "");
123 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
125 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &nfs_debug
, 0, "");
129 static int nfs_iosize
__P((struct nfsmount
*nmp
));
130 static int mountnfs
__P((struct nfs_args
*,struct mount
*,
131 struct mbuf
*,char *,char *,struct vnode
**));
132 static int nfs_mount
__P(( struct mount
*mp
, char *path
, caddr_t data
,
133 struct nameidata
*ndp
, struct proc
*p
));
134 static int nfs_start
__P(( struct mount
*mp
, int flags
,
136 static int nfs_unmount
__P(( struct mount
*mp
, int mntflags
,
138 static int nfs_root
__P(( struct mount
*mp
, struct vnode
**vpp
));
139 static int nfs_quotactl
__P(( struct mount
*mp
, int cmds
, uid_t uid
,
140 caddr_t arg
, struct proc
*p
));
141 static int nfs_statfs
__P(( struct mount
*mp
, struct statfs
*sbp
,
143 static int nfs_sync
__P(( struct mount
*mp
, int waitfor
,
144 struct ucred
*cred
, struct proc
*p
));
145 static int nfs_vptofh
__P(( struct vnode
*vp
, struct fid
*fhp
));
146 static int nfs_fhtovp
__P((struct mount
*mp
, struct fid
*fhp
,
147 struct mbuf
*nam
, struct vnode
**vpp
,
148 int *exflagsp
, struct ucred
**credanonp
));
149 static int nfs_vget
__P((struct mount
*, ino_t
, struct vnode
**));
153 * nfs vfs operations.
155 struct vfsops nfs_vfsops
= {
169 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
171 VFS_SET(nfs_vfsops
, nfs
, MOUNT_NFS
, VFCF_NETWORK
);
175 * This structure must be filled in by a primary bootstrap or bootstrap
176 * server for a diskless/dataless machine. It is initialized below just
177 * to ensure that it is allocated to initialized data (.data not .bss).
179 struct nfs_diskless nfs_diskless
= { 0 };
180 int nfs_diskless_valid
= 0;
182 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
184 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, diskless_valid
, CTLFLAG_RD
,
185 &nfs_diskless_valid
, 0, "");
187 SYSCTL_STRING(_vfs_nfs
, OID_AUTO
, diskless_rootpath
, CTLFLAG_RD
,
188 nfs_diskless
.root_hostnam
, 0, "");
190 SYSCTL_OPAQUE(_vfs_nfs
, OID_AUTO
, diskless_rootaddr
, CTLFLAG_RD
,
191 &nfs_diskless
.root_saddr
, sizeof nfs_diskless
.root_saddr
,
192 "%Ssockaddr_in", "");
194 SYSCTL_STRING(_vfs_nfs
, OID_AUTO
, diskless_swappath
, CTLFLAG_RD
,
195 nfs_diskless
.swap_hostnam
, 0, "");
197 SYSCTL_OPAQUE(_vfs_nfs
, OID_AUTO
, diskless_swapaddr
, CTLFLAG_RD
,
198 &nfs_diskless
.swap_saddr
, sizeof nfs_diskless
.swap_saddr
,
203 void nfsargs_ntoh
__P((struct nfs_args
*));
205 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
207 #if !defined(NO_MOUNT_PRIVATE)
209 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
210 struct vnode
**, struct mount
**));
211 #endif /* NO_MOUNT_PRIVATE */
212 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
213 struct onfs_args
*oargs
));
215 int nfsreqqusers
= 0;
216 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
219 static int nfs_iosize(nmp
)
220 struct nfsmount
* nmp
;
225 * Calculate the size used for io buffers. Use the larger
226 * of the two sizes to minimise nfs requests but make sure
227 * that it is at least one VM page to avoid wasting buffer
230 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
231 if (iosize
< PAGE_SIZE
) iosize
= PAGE_SIZE
;
233 /* XXX UPL changes for UBC do not support multiple pages */
234 iosize
= PAGE_SIZE
; /* XXX FIXME */
237 return (trunc_page(iosize
));
240 static void nfs_convert_oargs(args
,oargs
)
241 struct nfs_args
*args
;
242 struct onfs_args
*oargs
;
244 args
->version
= NFS_ARGSVERSION
;
245 args
->addr
= oargs
->addr
;
246 args
->addrlen
= oargs
->addrlen
;
247 args
->sotype
= oargs
->sotype
;
248 args
->proto
= oargs
->proto
;
249 args
->fh
= oargs
->fh
;
250 args
->fhsize
= oargs
->fhsize
;
251 args
->flags
= oargs
->flags
;
252 args
->wsize
= oargs
->wsize
;
253 args
->rsize
= oargs
->rsize
;
254 args
->readdirsize
= oargs
->readdirsize
;
255 args
->timeo
= oargs
->timeo
;
256 args
->retrans
= oargs
->retrans
;
257 args
->maxgrouplist
= oargs
->maxgrouplist
;
258 args
->readahead
= oargs
->readahead
;
259 args
->leaseterm
= oargs
->leaseterm
;
260 args
->deadthresh
= oargs
->deadthresh
;
261 args
->hostname
= oargs
->hostname
;
268 nfs_statfs(mp
, sbp
, p
)
270 register struct statfs
*sbp
;
273 register struct vnode
*vp
;
274 register struct nfs_statfs
*sfp
;
277 register long t1
, t2
;
278 caddr_t bpos
, dpos
, cp2
;
279 struct nfsmount
*nmp
= VFSTONFS(mp
);
280 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
281 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
284 extern int nfs_mount_type
;
287 sfp
= (struct nfs_statfs
*)0;
290 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
292 cred
->cr_ngroups
= 1;
293 if (v3
&& (nmp
->nm_flag
& NFSMNT_GOTFSINFO
) == 0)
294 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
295 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
296 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
298 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
);
300 nfsm_postop_attr(vp
, retattr
);
301 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
303 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
305 sbp
->f_type
= MOUNT_NFS
;
307 sbp
->f_type
= nfs_mount_type
;
309 sbp
->f_flags
= nmp
->nm_flag
;
310 sbp
->f_iosize
= nfs_iosize(nmp
);
312 sbp
->f_bsize
= NFS_FABLKSIZE
;
313 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
314 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
315 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
316 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
317 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
318 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
319 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
321 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
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
;
359 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
360 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
362 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
);
363 nfsm_postop_attr(vp
, retattr
);
365 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
366 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
367 if (pref
< nmp
->nm_wsize
)
368 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
369 ~(NFS_FABLKSIZE
- 1);
370 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
371 if (max
< nmp
->nm_wsize
) {
372 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
373 if (nmp
->nm_wsize
== 0)
376 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
377 if (pref
< nmp
->nm_rsize
)
378 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
379 ~(NFS_FABLKSIZE
- 1);
380 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
381 if (max
< nmp
->nm_rsize
) {
382 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
383 if (nmp
->nm_rsize
== 0)
386 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
387 if (pref
< nmp
->nm_readdirsize
)
388 nmp
->nm_readdirsize
= pref
;
389 if (max
< nmp
->nm_readdirsize
) {
390 nmp
->nm_readdirsize
= max
;
392 nmp
->nm_flag
|= NFSMNT_GOTFSINFO
;
399 * Mount a remote root fs via. nfs. This depends on the info in the
400 * nfs_diskless structure that has been filled in properly by some primary
402 * It goes something like this:
403 * - do enough of "ifconfig" by calling ifioctl() so that the system
404 * can talk to the server
405 * - If nfs_diskless.mygateway is filled in, use that address as
407 * - hand craft the swap nfs vnode hanging off a fake mount point
408 * if swdevt[0].sw_dev == NODEV
409 * - build the rootfs mount point and call mountnfs() to do the rest.
414 struct nfs_diskless nd
;
421 #if !defined(NO_MOUNT_PRIVATE)
422 struct mount
*mppriv
;
423 struct vnode
*vppriv
;
424 #endif /* NO_MOUNT_PRIVATE */
426 procp
= current_proc(); /* XXX */
429 * Call nfs_boot_init() to fill in the nfs_diskless struct.
430 * Side effect: Finds and configures a network interface.
432 bzero((caddr_t
) &nd
, sizeof(nd
));
433 nfs_boot_init(&nd
, procp
);
436 * Create the root mount point.
438 #if !defined(NO_MOUNT_PRIVATE)
439 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
441 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
442 #endif /* NO_MOUNT_PRIVATE */
445 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
447 simple_lock(&mountlist_slock
);
448 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
449 simple_unlock(&mountlist_slock
);
450 vfs_unbusy(mp
, procp
);
453 #if !defined(NO_MOUNT_PRIVATE)
454 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
455 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
456 NULL
, &vppriv
, &mppriv
);
459 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
461 simple_lock(&mountlist_slock
);
462 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
463 simple_unlock(&mountlist_slock
);
464 vfs_unbusy(mppriv
, procp
);
467 #endif /* NO_MOUNT_PRIVATE */
469 /* Get root attributes (for the time). */
470 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
471 if (error
) panic("nfs_mountroot: getattr for root");
472 n
= attr
.va_mtime
.tv_sec
;
478 * Internal version of mount system call for diskless setup.
481 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
482 struct nfs_dlmount
*ndmntp
;
488 struct nfs_args args
;
494 procp
= current_proc(); /* XXX */
496 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
497 printf("nfs_mountroot: NFS not configured");
500 mp
->mnt_flag
= mntflag
;
502 /* Initialize mount args. */
503 bzero((caddr_t
) &args
, sizeof(args
));
504 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
505 args
.addrlen
= args
.addr
->sa_len
;
506 args
.sotype
= SOCK_DGRAM
;
507 args
.fh
= ndmntp
->ndm_fh
;
508 args
.fhsize
= NFSX_V2FH
;
509 args
.hostname
= ndmntp
->ndm_host
;
510 args
.flags
= NFSMNT_RESVPORT
;
512 MGET(m
, M_DONTWAIT
, MT_SONAME
);
513 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
514 (m
->m_len
= args
.addr
->sa_len
));
515 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
516 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
517 mp
->mnt_vfc
->vfc_refcount
--;
518 vfs_unbusy(mp
, procp
);
519 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
522 #if 0 /* Causes incorrect reporting of "mounted on" */
523 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
529 #if !defined(NO_MOUNT_PRIVATE)
531 * Internal version of mount system call to mount "/private"
532 * separately in diskless setup
535 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
536 struct nfs_dlmount
*ndmntp
;
542 struct nfs_args args
;
547 struct vfsconf
*vfsp
;
551 procp
= current_proc(); /* XXX */
555 * mimic main()!. Temporarily set up rootvnode and other stuff so
556 * that namei works. Need to undo this because main() does it, too
558 struct filedesc
*fdp
; /* pointer to file descriptor state */
560 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
562 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
563 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
564 panic("cannot find root vnode");
565 fdp
->fd_cdir
= rootvnode
;
567 VOP_UNLOCK(rootvnode
, 0, procp
);
572 * Get vnode to be covered
574 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
576 if ((error
= namei(&nd
))) {
577 printf("nfs_mountroot: private namei failed!");
581 /* undo VREF in mimic main()! */
585 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
589 if (vp
->v_type
!= VDIR
) {
593 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
594 if (!strcmp(vfsp
->vfc_name
, "nfs"))
597 printf("nfs_mountroot: private NFS not configured");
601 if (vp
->v_mountedhere
!= NULL
) {
607 * Allocate and initialize the filesystem.
609 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
610 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
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
= NFSX_V2FH
;
634 args
.hostname
= ndmntp
->ndm_host
;
635 args
.flags
= NFSMNT_RESVPORT
;
637 MGET(m
, M_DONTWAIT
, MT_SONAME
);
638 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
639 (m
->m_len
= args
.addr
->sa_len
));
640 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
641 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
642 mp
->mnt_vfc
->vfc_refcount
--;
643 vfs_unbusy(mp
, procp
);
644 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
652 #endif /* NO_MOUNT_PRIVATE */
658 * It seems a bit dumb to copyinstr() the host and path here and then
659 * bcopy() them in mountnfs(), but I wanted to detect errors before
660 * doing the sockargs() call because sockargs() allocates an mbuf and
661 * an error after that means that I have to release the mbuf.
665 nfs_mount(mp
, path
, data
, ndp
, p
)
669 struct nameidata
*ndp
;
673 struct nfs_args args
;
676 char pth
[MNAMELEN
], hst
[MNAMELEN
];
678 u_char nfh
[NFSX_V3FHMAX
];
680 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
683 if (args
.version
!= NFS_ARGSVERSION
) {
684 #ifndef NO_COMPAT_PRELITE2
686 * If the argument version is unknown, then assume the
687 * caller is a pre-lite2 4.4BSD client and convert its
690 struct onfs_args oargs
;
691 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
694 nfs_convert_oargs(&args
,&oargs
);
695 #else /* NO_COMPAT_PRELITE2 */
696 return (EPROGMISMATCH
);
697 #endif /* !NO_COMPAT_PRELITE2 */
699 if (args
.fhsize
> NFSX_V3FHMAX
)
701 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
704 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
707 bzero(&pth
[len
], MNAMELEN
- len
);
708 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
711 bzero(&hst
[len
], MNAMELEN
- len
);
712 /* sockargs() call must be after above copyin() calls */
713 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
717 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
722 * Common code for mount and mountroot
725 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
726 register struct nfs_args
*argp
;
727 register struct mount
*mp
;
732 register struct nfsmount
*nmp
;
736 struct proc
*curproc
;
739 * turning off NQNFS until we have further testing
740 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
741 * Those have NQNFS defined out in conjunction with this
742 * returning an error. Remove when fully tested.
744 if (argp
->flags
& NFSMNT_NQNFS
) {
745 error
= NFSERR_NOTSUPP
;
749 if (mp
->mnt_flag
& MNT_UPDATE
) {
751 /* update paths, file handles, etc, here XXX */
755 MALLOC_ZONE(nmp
, struct nfsmount
*,
756 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
757 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
758 TAILQ_INIT(&nmp
->nm_uidlruhead
);
759 TAILQ_INIT(&nmp
->nm_bufq
);
760 mp
->mnt_data
= (qaddr_t
)nmp
;
764 nmp
->nm_flag
= argp
->flags
;
765 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
767 * We have to set mnt_maxsymlink to a non-zero value so
768 * that COMPAT_43 routines will know that we are setting
769 * the d_type field in directories (and can zero it for
770 * unsuspecting binaries).
772 mp
->mnt_maxsymlinklen
= 1;
773 nmp
->nm_timeo
= NFS_TIMEO
;
774 nmp
->nm_retry
= NFS_RETRANS
;
775 nmp
->nm_wsize
= NFS_WSIZE
;
776 nmp
->nm_rsize
= NFS_RSIZE
;
777 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
778 nmp
->nm_numgrps
= NFS_MAXGRPS
;
779 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
780 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
781 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
782 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
783 nmp
->nm_inprog
= NULLVP
;
784 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
785 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
789 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
790 * no sense in that context.
792 if (argp
->sotype
== SOCK_STREAM
)
793 argp
->flags
&= ~NFSMNT_NOCONN
;
795 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
796 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
797 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
798 nmp
->nm_timeo
= NFS_MINTIMEO
;
799 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
800 nmp
->nm_timeo
= NFS_MAXTIMEO
;
803 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
804 nmp
->nm_retry
= argp
->retrans
;
805 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
806 nmp
->nm_retry
= NFS_MAXREXMIT
;
809 if (argp
->flags
& NFSMNT_NFSV3
) {
810 if (argp
->sotype
== SOCK_DGRAM
)
811 maxio
= NFS_MAXDGRAMDATA
;
815 maxio
= NFS_V2MAXDATA
;
817 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
818 nmp
->nm_wsize
= argp
->wsize
;
819 /* Round down to multiple of blocksize */
820 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
821 if (nmp
->nm_wsize
<= 0)
822 nmp
->nm_wsize
= NFS_FABLKSIZE
;
824 if (nmp
->nm_wsize
> maxio
)
825 nmp
->nm_wsize
= maxio
;
826 if (nmp
->nm_wsize
> MAXBSIZE
)
827 nmp
->nm_wsize
= MAXBSIZE
;
829 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
830 nmp
->nm_rsize
= argp
->rsize
;
831 /* Round down to multiple of blocksize */
832 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
833 if (nmp
->nm_rsize
<= 0)
834 nmp
->nm_rsize
= NFS_FABLKSIZE
;
836 if (nmp
->nm_rsize
> maxio
)
837 nmp
->nm_rsize
= maxio
;
838 if (nmp
->nm_rsize
> MAXBSIZE
)
839 nmp
->nm_rsize
= MAXBSIZE
;
841 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
842 nmp
->nm_readdirsize
= argp
->readdirsize
;
844 if (nmp
->nm_readdirsize
> maxio
)
845 nmp
->nm_readdirsize
= maxio
;
846 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
847 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
849 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
850 argp
->maxgrouplist
<= NFS_MAXGRPS
)
851 nmp
->nm_numgrps
= argp
->maxgrouplist
;
852 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
853 argp
->readahead
<= NFS_MAXRAHEAD
)
854 nmp
->nm_readahead
= argp
->readahead
;
855 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
856 argp
->leaseterm
<= NQ_MAXLEASE
)
857 nmp
->nm_leaseterm
= argp
->leaseterm
;
858 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
859 argp
->deadthresh
<= NQ_NEVERDEAD
)
860 nmp
->nm_deadthresh
= argp
->deadthresh
;
861 /* Set up the sockets and per-host congestion */
862 nmp
->nm_sotype
= argp
->sotype
;
863 nmp
->nm_soproto
= argp
->proto
;
866 * For Connection based sockets (TCP,...) defer the connect until
867 * the first request, in case the server is not responding.
869 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
870 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
874 * This is silly, but it has to be set so that vinifod() works.
875 * We do not want to do an nfs_statfs() here since we can get
876 * stuck on a dead server and we are holding a lock on the mount
879 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
881 * A reference count is needed on the nfsnode representing the
882 * remote root. If this object is not persistent, then backward
883 * traversals of the mount point (i.e. "..") will not work if
884 * the nfsnode gets flushed out of the cache. UFS does not have
885 * this problem, because one can identify root inodes by their
886 * number == ROOTINO (2).
888 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
893 * save this vnode pointer. That way nfs_unmount()
894 * does not need to call nfs_net() just get it to drop
895 * this vnode reference.
897 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
900 * Get file attributes for the mountpoint. This has the side
901 * effect of filling in (*vpp)->v_type with the correct value.
903 curproc
= current_proc();
904 VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
907 * Lose the lock but keep the ref.
909 VOP_UNLOCK(*vpp
, 0, curproc
);
914 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
922 * unmount system call
925 nfs_unmount(mp
, mntflags
, p
)
930 register struct nfsmount
*nmp
;
932 int error
, flags
= 0;
934 if (mntflags
& MNT_FORCE
)
938 * Goes something like this..
939 * - Check for activity on the root vnode (other than ourselves).
940 * - Call vflush() to clear out vnodes for this file system,
941 * except for the root vnode.
942 * - Decrement reference on the vnode representing remote root.
944 * - Free up the data structures
947 * We need to decrement the ref. count on the nfsnode representing
948 * the remote root. See comment in mountnfs(). The VFS unmount()
949 * has done vput on this vnode, otherwise we would get deadlock!
952 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
953 if (vp
->v_usecount
> 1) {
954 VOP_UNLOCK(vp
, 0, p
);
959 * Must handshake with nqnfs_clientd() if it is active.
961 nmp
->nm_flag
|= NFSMNT_DISMINPROG
;
962 while (nmp
->nm_inprog
!= NULLVP
)
963 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
964 error
= vflush(mp
, vp
, flags
);
966 VOP_UNLOCK(vp
, 0, p
);
967 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
972 * We are now committed to the unmount.
973 * For NQNFS, let the server daemon free the nfsmount structure.
975 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
976 nmp
->nm_flag
|= NFSMNT_DISMNT
;
979 * Release the root vnode reference held by mountnfs()
984 m_freem(nmp
->nm_nam
);
986 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
987 register struct nfsreq
*rp
;
989 * Loop through outstanding request list and remove dangling
990 * references to defunct nfsmount struct
993 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
994 nfsatompanic("unmount add");
995 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
996 nfsbtcpu
= cpu_number();
997 nfsbtthread
= (int)(current_thread());
1000 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1001 if (rp
->r_nmp
== nmp
)
1002 rp
->r_nmp
= (struct nfsmount
*)0;
1004 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1005 nfsatompanic("unmount sub");
1007 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1013 * Return root of a filesystem
1020 register struct vnode
*vp
;
1021 struct nfsmount
*nmp
;
1026 error
= vget(vp
, LK_EXCLUSIVE
, current_proc());
1029 if (vp
->v_type
== VNON
)
1031 vp
->v_flag
|= VROOT
;
1039 * Flush out the buffer cache
1043 nfs_sync(mp
, waitfor
, cred
, p
)
1049 register struct vnode
*vp
;
1050 int error
, allerror
= 0;
1053 * Force stale buffer cache information to be flushed.
1056 for (vp
= mp
->mnt_vnodelist
.lh_first
;
1058 vp
= vp
->v_mntvnodes
.le_next
) {
1060 * If the vnode that we are about to sync is no longer
1061 * associated with this mount point, start over.
1063 if (vp
->v_mount
!= mp
)
1065 if (VOP_ISLOCKED(vp
) || vp
->v_dirtyblkhd
.lh_first
== NULL
)
1067 if (vget(vp
, LK_EXCLUSIVE
, p
))
1069 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1078 * NFS flat namespace lookup.
1079 * Currently unsupported.
1083 nfs_vget(mp
, ino
, vpp
)
1089 return (EOPNOTSUPP
);
1093 * At this point, this should never happen
1097 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1098 register struct mount
*mp
;
1103 struct ucred
**credanonp
;
1110 * Vnode pointer to File handle, should never happen either
1123 * Vfs start routine, a no-op.
1127 nfs_start(mp
, flags
, p
)
1137 * Do operations associated with quotas, not supported
1141 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1149 return (EOPNOTSUPP
);
1153 * Do that sysctl thang...
1156 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1157 size_t newlen
, struct proc
*p
)
1162 * All names at this level are terminal.
1165 return ENOTDIR
; /* overloaded */
1170 *oldlenp
= sizeof nfsstats
;
1174 if(*oldlenp
< sizeof nfsstats
) {
1175 *oldlenp
= sizeof nfsstats
;
1179 rv
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1182 if(newp
&& newlen
!= sizeof nfsstats
)
1186 return copyin(newp
, &nfsstats
, sizeof nfsstats
);