2 * Copyright (c) 2000-2001 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 * This structure must be filled in by a primary bootstrap or bootstrap
168 * server for a diskless/dataless machine. It is initialized below just
169 * to ensure that it is allocated to initialized data (.data not .bss).
171 struct nfs_diskless nfs_diskless
= { 0 };
172 int nfs_diskless_valid
= 0;
174 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
176 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, diskless_valid
, CTLFLAG_RD
,
177 &nfs_diskless_valid
, 0, "");
179 SYSCTL_STRING(_vfs_nfs
, OID_AUTO
, diskless_rootpath
, CTLFLAG_RD
,
180 nfs_diskless
.root_hostnam
, 0, "");
182 SYSCTL_OPAQUE(_vfs_nfs
, OID_AUTO
, diskless_rootaddr
, CTLFLAG_RD
,
183 &nfs_diskless
.root_saddr
, sizeof nfs_diskless
.root_saddr
,
184 "%Ssockaddr_in", "");
186 SYSCTL_STRING(_vfs_nfs
, OID_AUTO
, diskless_swappath
, CTLFLAG_RD
,
187 nfs_diskless
.swap_hostnam
, 0, "");
189 SYSCTL_OPAQUE(_vfs_nfs
, OID_AUTO
, diskless_swapaddr
, CTLFLAG_RD
,
190 &nfs_diskless
.swap_saddr
, sizeof nfs_diskless
.swap_saddr
,
195 void nfsargs_ntoh
__P((struct nfs_args
*));
197 nfs_mount_diskless
__P((struct nfs_dlmount
*, char *, int, struct vnode
**,
199 #if !defined(NO_MOUNT_PRIVATE)
201 nfs_mount_diskless_private
__P((struct nfs_dlmount
*, char *, int,
202 struct vnode
**, struct mount
**));
203 #endif /* NO_MOUNT_PRIVATE */
204 static void nfs_convert_oargs
__P((struct nfs_args
*args
,
205 struct onfs_args
*oargs
));
207 int nfsreqqusers
= 0;
208 extern int nfsbtlen
, nfsbtcpu
, nfsbtthread
, nfsbt
[32];
211 static int nfs_iosize(nmp
)
212 struct nfsmount
* nmp
;
217 * Calculate the size used for io buffers. Use the larger
218 * of the two sizes to minimise nfs requests but make sure
219 * that it is at least one VM page to avoid wasting buffer
222 iosize
= max(nmp
->nm_rsize
, nmp
->nm_wsize
);
223 if (iosize
< PAGE_SIZE
)
225 return (trunc_page(iosize
));
228 static void nfs_convert_oargs(args
,oargs
)
229 struct nfs_args
*args
;
230 struct onfs_args
*oargs
;
232 args
->version
= NFS_ARGSVERSION
;
233 args
->addr
= oargs
->addr
;
234 args
->addrlen
= oargs
->addrlen
;
235 args
->sotype
= oargs
->sotype
;
236 args
->proto
= oargs
->proto
;
237 args
->fh
= oargs
->fh
;
238 args
->fhsize
= oargs
->fhsize
;
239 args
->flags
= oargs
->flags
;
240 args
->wsize
= oargs
->wsize
;
241 args
->rsize
= oargs
->rsize
;
242 args
->readdirsize
= oargs
->readdirsize
;
243 args
->timeo
= oargs
->timeo
;
244 args
->retrans
= oargs
->retrans
;
245 args
->maxgrouplist
= oargs
->maxgrouplist
;
246 args
->readahead
= oargs
->readahead
;
247 args
->leaseterm
= oargs
->leaseterm
;
248 args
->deadthresh
= oargs
->deadthresh
;
249 args
->hostname
= oargs
->hostname
;
256 nfs_statfs(mp
, sbp
, p
)
258 register struct statfs
*sbp
;
261 register struct vnode
*vp
;
262 register struct nfs_statfs
*sfp
;
265 register long t1
, t2
;
266 caddr_t bpos
, dpos
, cp2
;
267 struct nfsmount
*nmp
= VFSTONFS(mp
);
268 int error
= 0, v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
), retattr
;
269 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
272 extern int nfs_mount_type
;
276 sfp
= (struct nfs_statfs
*)0;
279 if (error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
))
282 cred
->cr_ngroups
= 1;
283 if (v3
&& (nmp
->nm_flag
& NFSMNT_GOTFSINFO
) == 0)
284 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
285 nfsstats
.rpccnt
[NFSPROC_FSSTAT
]++;
286 nfsm_reqhead(vp
, NFSPROC_FSSTAT
, NFSX_FH(v3
));
288 nfsm_request(vp
, NFSPROC_FSSTAT
, p
, cred
, &xid
);
290 nfsm_postop_attr(vp
, retattr
, &xid
);
291 nfsm_dissect(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
293 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
295 sbp
->f_type
= MOUNT_NFS
;
297 sbp
->f_type
= nfs_mount_type
;
299 sbp
->f_flags
= nmp
->nm_flag
;
300 sbp
->f_iosize
= nfs_iosize(nmp
);
302 sbp
->f_bsize
= NFS_FABLKSIZE
;
303 fxdr_hyper(&sfp
->sf_tbytes
, &tquad
);
304 sbp
->f_blocks
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
305 fxdr_hyper(&sfp
->sf_fbytes
, &tquad
);
306 sbp
->f_bfree
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
307 fxdr_hyper(&sfp
->sf_abytes
, &tquad
);
308 sbp
->f_bavail
= (long)(tquad
/ ((u_quad_t
)NFS_FABLKSIZE
));
309 sbp
->f_files
= (fxdr_unsigned(long, sfp
->sf_tfiles
.nfsuquad
[1])
311 sbp
->f_ffree
= (fxdr_unsigned(long, sfp
->sf_ffiles
.nfsuquad
[1])
314 sbp
->f_bsize
= fxdr_unsigned(long, sfp
->sf_bsize
);
315 sbp
->f_blocks
= fxdr_unsigned(long, sfp
->sf_blocks
);
316 sbp
->f_bfree
= fxdr_unsigned(long, sfp
->sf_bfree
);
317 sbp
->f_bavail
= fxdr_unsigned(long, sfp
->sf_bavail
);
321 if (sbp
!= &mp
->mnt_stat
) {
322 bcopy(mp
->mnt_stat
.f_mntonname
, sbp
->f_mntonname
, MNAMELEN
);
323 bcopy(mp
->mnt_stat
.f_mntfromname
, sbp
->f_mntfromname
, MNAMELEN
);
326 VOP_UNLOCK(vp
, 0, p
);
332 * nfs version 3 fsinfo rpc call
335 nfs_fsinfo(nmp
, vp
, cred
, p
)
336 register struct nfsmount
*nmp
;
337 register struct vnode
*vp
;
341 register struct nfsv3_fsinfo
*fsp
;
343 register long t1
, t2
;
344 register u_long
*tl
, pref
, max
;
345 caddr_t bpos
, dpos
, cp2
;
346 int error
= 0, retattr
;
347 struct mbuf
*mreq
, *mrep
, *md
, *mb
, *mb2
;
350 nfsstats
.rpccnt
[NFSPROC_FSINFO
]++;
351 nfsm_reqhead(vp
, NFSPROC_FSINFO
, NFSX_FH(1));
353 nfsm_request(vp
, NFSPROC_FSINFO
, p
, cred
, &xid
);
354 nfsm_postop_attr(vp
, retattr
, &xid
);
356 nfsm_dissect(fsp
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
357 pref
= fxdr_unsigned(u_long
, fsp
->fs_wtpref
);
358 if (pref
< nmp
->nm_wsize
)
359 nmp
->nm_wsize
= (pref
+ NFS_FABLKSIZE
- 1) &
360 ~(NFS_FABLKSIZE
- 1);
361 max
= fxdr_unsigned(u_long
, fsp
->fs_wtmax
);
362 if (max
< nmp
->nm_wsize
) {
363 nmp
->nm_wsize
= max
& ~(NFS_FABLKSIZE
- 1);
364 if (nmp
->nm_wsize
== 0)
367 pref
= fxdr_unsigned(u_long
, fsp
->fs_rtpref
);
368 if (pref
< nmp
->nm_rsize
)
369 nmp
->nm_rsize
= (pref
+ NFS_FABLKSIZE
- 1) &
370 ~(NFS_FABLKSIZE
- 1);
371 max
= fxdr_unsigned(u_long
, fsp
->fs_rtmax
);
372 if (max
< nmp
->nm_rsize
) {
373 nmp
->nm_rsize
= max
& ~(NFS_FABLKSIZE
- 1);
374 if (nmp
->nm_rsize
== 0)
377 pref
= fxdr_unsigned(u_long
, fsp
->fs_dtpref
);
378 if (pref
< nmp
->nm_readdirsize
)
379 nmp
->nm_readdirsize
= pref
;
380 if (max
< nmp
->nm_readdirsize
) {
381 nmp
->nm_readdirsize
= max
;
383 nmp
->nm_flag
|= NFSMNT_GOTFSINFO
;
390 * Mount a remote root fs via. nfs. This depends on the info in the
391 * nfs_diskless structure that has been filled in properly by some primary
393 * It goes something like this:
394 * - do enough of "ifconfig" by calling ifioctl() so that the system
395 * can talk to the server
396 * - If nfs_diskless.mygateway is filled in, use that address as
398 * - hand craft the swap nfs vnode hanging off a fake mount point
399 * if swdevt[0].sw_dev == NODEV
400 * - build the rootfs mount point and call mountnfs() to do the rest.
405 struct nfs_diskless nd
;
412 #if !defined(NO_MOUNT_PRIVATE)
413 struct mount
*mppriv
;
414 struct vnode
*vppriv
;
415 #endif /* NO_MOUNT_PRIVATE */
417 procp
= current_proc(); /* XXX */
420 * Call nfs_boot_init() to fill in the nfs_diskless struct.
421 * Note: networking must already have been configured before
424 bzero((caddr_t
) &nd
, sizeof(nd
));
425 error
= nfs_boot_init(&nd
, procp
);
427 panic("nfs_boot_init failed with %d\n", error
);
431 * Create the root mount point.
433 #if !defined(NO_MOUNT_PRIVATE)
434 if ((error
= nfs_mount_diskless(&nd
.nd_root
, "/", MNT_RDONLY
, &vp
, &mp
))) {
436 if (error
= nfs_mount_diskless(&nd
.nd_root
, "/", NULL
, &vp
, &mp
)) {
437 #endif /* NO_MOUNT_PRIVATE */
438 panic("nfs_mount_diskless failed with %d\n", error
);
440 printf("root on %s\n", (char *)&nd
.nd_root
.ndm_host
);
442 simple_lock(&mountlist_slock
);
443 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
444 simple_unlock(&mountlist_slock
);
445 vfs_unbusy(mp
, procp
);
448 #if !defined(NO_MOUNT_PRIVATE)
449 if (nd
.nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
450 error
= nfs_mount_diskless_private(&nd
.nd_private
, "/private",
451 NULL
, &vppriv
, &mppriv
);
453 panic("nfs_mount_diskless failed with %d\n", error
);
455 printf("private on %s\n", (char *)&nd
.nd_private
.ndm_host
);
457 simple_lock(&mountlist_slock
);
458 CIRCLEQ_INSERT_TAIL(&mountlist
, mppriv
, mnt_list
);
459 simple_unlock(&mountlist_slock
);
460 vfs_unbusy(mppriv
, procp
);
463 #endif /* NO_MOUNT_PRIVATE */
465 /* Get root attributes (for the time). */
466 error
= VOP_GETATTR(vp
, &attr
, procp
->p_ucred
, procp
);
467 if (error
) panic("nfs_mountroot: getattr for root");
468 n
= attr
.va_mtime
.tv_sec
;
474 * Internal version of mount system call for diskless setup.
477 nfs_mount_diskless(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
478 struct nfs_dlmount
*ndmntp
;
484 struct nfs_args args
;
490 procp
= current_proc(); /* XXX */
492 if ((error
= vfs_rootmountalloc("nfs", ndmntp
->ndm_host
, &mp
))) {
493 printf("nfs_mountroot: NFS not configured");
496 mp
->mnt_flag
= mntflag
;
498 /* Initialize mount args. */
499 bzero((caddr_t
) &args
, sizeof(args
));
500 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
501 args
.addrlen
= args
.addr
->sa_len
;
502 args
.sotype
= SOCK_DGRAM
;
503 args
.fh
= ndmntp
->ndm_fh
;
504 args
.fhsize
= NFSX_V2FH
; /* need to try v3, then v2 */
505 args
.hostname
= ndmntp
->ndm_host
;
506 args
.flags
= NFSMNT_RESVPORT
;
508 MGET(m
, M_DONTWAIT
, MT_SONAME
);
509 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
510 (m
->m_len
= args
.addr
->sa_len
));
511 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, vpp
))) {
512 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
513 mp
->mnt_vfc
->vfc_refcount
--;
514 vfs_unbusy(mp
, procp
);
515 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
518 #if 0 /* Causes incorrect reporting of "mounted on" */
519 (void) copystr(args
.hostname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
525 #if !defined(NO_MOUNT_PRIVATE)
527 * Internal version of mount system call to mount "/private"
528 * separately in diskless setup
531 nfs_mount_diskless_private(ndmntp
, mntname
, mntflag
, vpp
, mpp
)
532 struct nfs_dlmount
*ndmntp
;
538 struct nfs_args args
;
543 struct vfsconf
*vfsp
;
547 procp
= current_proc(); /* XXX */
551 * mimic main()!. Temporarily set up rootvnode and other stuff so
552 * that namei works. Need to undo this because main() does it, too
554 struct filedesc
*fdp
; /* pointer to file descriptor state */
556 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
558 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
559 if (VFS_ROOT(mountlist
.cqh_first
, &rootvnode
))
560 panic("cannot find root vnode");
562 fdp
->fd_cdir
= rootvnode
;
563 VOP_UNLOCK(rootvnode
, 0, procp
);
568 * Get vnode to be covered
570 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
572 if ((error
= namei(&nd
))) {
573 printf("nfs_mountroot: private namei failed!");
577 /* undo VREF in mimic main()! */
581 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
585 if (vp
->v_type
!= VDIR
) {
589 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
590 if (!strcmp(vfsp
->vfc_name
, "nfs"))
593 printf("nfs_mountroot: private NFS not configured");
597 if (vp
->v_mountedhere
!= NULL
) {
603 * Allocate and initialize the filesystem.
605 mp
= _MALLOC_ZONE((u_long
)sizeof(struct mount
), M_MOUNT
, M_WAITOK
);
606 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
608 /* Initialize the default IO constraints */
609 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
610 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
612 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
613 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
614 LIST_INIT(&mp
->mnt_vnodelist
);
615 mp
->mnt_op
= vfsp
->vfc_vfsops
;
617 vfsp
->vfc_refcount
++;
618 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
619 mp
->mnt_flag
= mntflag
;
620 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
621 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
622 vp
->v_mountedhere
= mp
;
623 mp
->mnt_vnodecovered
= vp
;
624 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
625 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
626 (void) copystr(ndmntp
->ndm_host
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, 0);
628 /* Initialize mount args. */
629 bzero((caddr_t
) &args
, sizeof(args
));
630 args
.addr
= (struct sockaddr
*)&ndmntp
->ndm_saddr
;
631 args
.addrlen
= args
.addr
->sa_len
;
632 args
.sotype
= SOCK_DGRAM
;
633 args
.fh
= ndmntp
->ndm_fh
;
634 args
.fhsize
= NFSX_V2FH
;
635 args
.hostname
= ndmntp
->ndm_host
;
636 args
.flags
= NFSMNT_RESVPORT
;
638 MGET(m
, M_DONTWAIT
, MT_SONAME
);
639 bcopy((caddr_t
)args
.addr
, mtod(m
, caddr_t
),
640 (m
->m_len
= args
.addr
->sa_len
));
641 if ((error
= mountnfs(&args
, mp
, m
, mntname
, args
.hostname
, &vp
))) {
642 printf("nfs_mountroot: mount %s failed: %d", mntname
, error
);
643 mp
->mnt_vfc
->vfc_refcount
--;
644 vfs_unbusy(mp
, procp
);
645 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
653 #endif /* NO_MOUNT_PRIVATE */
659 * It seems a bit dumb to copyinstr() the host and path here and then
660 * bcopy() them in mountnfs(), but I wanted to detect errors before
661 * doing the sockargs() call because sockargs() allocates an mbuf and
662 * an error after that means that I have to release the mbuf.
666 nfs_mount(mp
, path
, data
, ndp
, p
)
670 struct nameidata
*ndp
;
674 struct nfs_args args
;
677 char pth
[MNAMELEN
], hst
[MNAMELEN
];
679 u_char nfh
[NFSX_V3FHMAX
];
681 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct nfs_args
));
684 if (args
.version
!= NFS_ARGSVERSION
) {
685 #ifndef NO_COMPAT_PRELITE2
687 * If the argument version is unknown, then assume the
688 * caller is a pre-lite2 4.4BSD client and convert its
691 struct onfs_args oargs
;
692 error
= copyin(data
, (caddr_t
)&oargs
, sizeof (struct onfs_args
));
695 nfs_convert_oargs(&args
,&oargs
);
696 #else /* NO_COMPAT_PRELITE2 */
697 return (EPROGMISMATCH
);
698 #endif /* !NO_COMPAT_PRELITE2 */
700 if (args
.fhsize
> NFSX_V3FHMAX
)
702 error
= copyin((caddr_t
)args
.fh
, (caddr_t
)nfh
, args
.fhsize
);
705 error
= copyinstr(path
, pth
, MNAMELEN
-1, &len
);
708 bzero(&pth
[len
], MNAMELEN
- len
);
709 error
= copyinstr(args
.hostname
, hst
, MNAMELEN
-1, &len
);
712 bzero(&hst
[len
], MNAMELEN
- len
);
713 /* sockargs() call must be after above copyin() calls */
714 error
= sockargs(&nam
, (caddr_t
)args
.addr
, args
.addrlen
, MT_SONAME
);
718 error
= mountnfs(&args
, mp
, nam
, pth
, hst
, &vp
);
723 * Common code for mount and mountroot
726 mountnfs(argp
, mp
, nam
, pth
, hst
, vpp
)
727 register struct nfs_args
*argp
;
728 register struct mount
*mp
;
733 register struct nfsmount
*nmp
;
737 struct proc
*curproc
;
740 * turning off NQNFS until we have further testing
741 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
742 * Those have NQNFS defined out in conjunction with this
743 * returning an error. Remove when fully tested.
745 if (argp
->flags
& NFSMNT_NQNFS
) {
746 error
= NFSERR_NOTSUPP
;
750 if (mp
->mnt_flag
& MNT_UPDATE
) {
752 /* update paths, file handles, etc, here XXX */
756 MALLOC_ZONE(nmp
, struct nfsmount
*,
757 sizeof (struct nfsmount
), M_NFSMNT
, M_WAITOK
);
758 bzero((caddr_t
)nmp
, sizeof (struct nfsmount
));
759 TAILQ_INIT(&nmp
->nm_uidlruhead
);
760 TAILQ_INIT(&nmp
->nm_bufq
);
761 mp
->mnt_data
= (qaddr_t
)nmp
;
765 nmp
->nm_flag
= argp
->flags
;
766 if (nmp
->nm_flag
& NFSMNT_NQNFS
)
768 * We have to set mnt_maxsymlink to a non-zero value so
769 * that COMPAT_43 routines will know that we are setting
770 * the d_type field in directories (and can zero it for
771 * unsuspecting binaries).
773 mp
->mnt_maxsymlinklen
= 1;
774 nmp
->nm_timeo
= NFS_TIMEO
;
775 nmp
->nm_retry
= NFS_RETRANS
;
776 nmp
->nm_wsize
= NFS_WSIZE
;
777 nmp
->nm_rsize
= NFS_RSIZE
;
778 nmp
->nm_readdirsize
= NFS_READDIRSIZE
;
779 nmp
->nm_numgrps
= NFS_MAXGRPS
;
780 nmp
->nm_readahead
= NFS_DEFRAHEAD
;
781 nmp
->nm_leaseterm
= NQ_DEFLEASE
;
782 nmp
->nm_deadthresh
= NQ_DEADTHRESH
;
783 CIRCLEQ_INIT(&nmp
->nm_timerhead
);
784 nmp
->nm_inprog
= NULLVP
;
785 bcopy(hst
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
786 bcopy(pth
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
);
790 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
791 * no sense in that context.
793 if (argp
->sotype
== SOCK_STREAM
)
794 argp
->flags
&= ~NFSMNT_NOCONN
;
796 if ((argp
->flags
& NFSMNT_TIMEO
) && argp
->timeo
> 0) {
797 nmp
->nm_timeo
= (argp
->timeo
* NFS_HZ
+ 5) / 10;
798 if (nmp
->nm_timeo
< NFS_MINTIMEO
)
799 nmp
->nm_timeo
= NFS_MINTIMEO
;
800 else if (nmp
->nm_timeo
> NFS_MAXTIMEO
)
801 nmp
->nm_timeo
= NFS_MAXTIMEO
;
804 if ((argp
->flags
& NFSMNT_RETRANS
) && argp
->retrans
> 1) {
805 nmp
->nm_retry
= argp
->retrans
;
806 if (nmp
->nm_retry
> NFS_MAXREXMIT
)
807 nmp
->nm_retry
= NFS_MAXREXMIT
;
810 if (argp
->flags
& NFSMNT_NFSV3
) {
811 if (argp
->sotype
== SOCK_DGRAM
)
812 maxio
= NFS_MAXDGRAMDATA
;
816 maxio
= NFS_V2MAXDATA
;
818 if ((argp
->flags
& NFSMNT_WSIZE
) && argp
->wsize
> 0) {
819 nmp
->nm_wsize
= argp
->wsize
;
820 /* Round down to multiple of blocksize */
821 nmp
->nm_wsize
&= ~(NFS_FABLKSIZE
- 1);
822 if (nmp
->nm_wsize
<= 0)
823 nmp
->nm_wsize
= NFS_FABLKSIZE
;
825 if (nmp
->nm_wsize
> maxio
)
826 nmp
->nm_wsize
= maxio
;
827 if (nmp
->nm_wsize
> MAXBSIZE
)
828 nmp
->nm_wsize
= MAXBSIZE
;
830 if ((argp
->flags
& NFSMNT_RSIZE
) && argp
->rsize
> 0) {
831 nmp
->nm_rsize
= argp
->rsize
;
832 /* Round down to multiple of blocksize */
833 nmp
->nm_rsize
&= ~(NFS_FABLKSIZE
- 1);
834 if (nmp
->nm_rsize
<= 0)
835 nmp
->nm_rsize
= NFS_FABLKSIZE
;
837 if (nmp
->nm_rsize
> maxio
)
838 nmp
->nm_rsize
= maxio
;
839 if (nmp
->nm_rsize
> MAXBSIZE
)
840 nmp
->nm_rsize
= MAXBSIZE
;
842 if ((argp
->flags
& NFSMNT_READDIRSIZE
) && argp
->readdirsize
> 0) {
843 nmp
->nm_readdirsize
= argp
->readdirsize
;
845 if (nmp
->nm_readdirsize
> maxio
)
846 nmp
->nm_readdirsize
= maxio
;
847 if (nmp
->nm_readdirsize
> nmp
->nm_rsize
)
848 nmp
->nm_readdirsize
= nmp
->nm_rsize
;
850 if ((argp
->flags
& NFSMNT_MAXGRPS
) && argp
->maxgrouplist
>= 0 &&
851 argp
->maxgrouplist
<= NFS_MAXGRPS
)
852 nmp
->nm_numgrps
= argp
->maxgrouplist
;
853 if ((argp
->flags
& NFSMNT_READAHEAD
) && argp
->readahead
>= 0 &&
854 argp
->readahead
<= NFS_MAXRAHEAD
)
855 nmp
->nm_readahead
= argp
->readahead
;
856 if ((argp
->flags
& NFSMNT_LEASETERM
) && argp
->leaseterm
>= 2 &&
857 argp
->leaseterm
<= NQ_MAXLEASE
)
858 nmp
->nm_leaseterm
= argp
->leaseterm
;
859 if ((argp
->flags
& NFSMNT_DEADTHRESH
) && argp
->deadthresh
>= 1 &&
860 argp
->deadthresh
<= NQ_NEVERDEAD
)
861 nmp
->nm_deadthresh
= argp
->deadthresh
;
862 /* Set up the sockets and per-host congestion */
863 nmp
->nm_sotype
= argp
->sotype
;
864 nmp
->nm_soproto
= argp
->proto
;
867 * For Connection based sockets (TCP,...) defer the connect until
868 * the first request, in case the server is not responding.
870 if (nmp
->nm_sotype
== SOCK_DGRAM
&&
871 (error
= nfs_connect(nmp
, (struct nfsreq
*)0)))
875 * This is silly, but it has to be set so that vinifod() works.
876 * We do not want to do an nfs_statfs() here since we can get
877 * stuck on a dead server and we are holding a lock on the mount
880 mp
->mnt_stat
.f_iosize
= nfs_iosize(nmp
);
882 * A reference count is needed on the nfsnode representing the
883 * remote root. If this object is not persistent, then backward
884 * traversals of the mount point (i.e. "..") will not work if
885 * the nfsnode gets flushed out of the cache. UFS does not have
886 * this problem, because one can identify root inodes by their
887 * number == ROOTINO (2).
889 error
= nfs_nget(mp
, (nfsfh_t
*)argp
->fh
, argp
->fhsize
, &np
);
894 * save this vnode pointer. That way nfs_unmount()
895 * does not need to call nfs_net() just get it to drop
896 * this vnode reference.
898 nmp
->nm_dvp
= *vpp
= NFSTOV(np
);
901 * Get file attributes for the mountpoint. This has the side
902 * effect of filling in (*vpp)->v_type with the correct value.
904 curproc
= current_proc();
905 VOP_GETATTR(*vpp
, &attrs
, curproc
->p_ucred
, curproc
);
908 * Lose the lock but keep the ref.
910 VOP_UNLOCK(*vpp
, 0, curproc
);
915 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
923 * unmount system call
926 nfs_unmount(mp
, mntflags
, p
)
931 register struct nfsmount
*nmp
;
933 int error
, flags
= 0;
935 if (mntflags
& MNT_FORCE
)
939 * Goes something like this..
940 * - Call vflush() to clear out vnodes for this file system,
941 * except for the swap files. Deal with them in 2nd pass.
942 * It will do vgone making the vnode VBAD at that time.
943 * - Decrement reference on the vnode representing remote root.
945 * - Free up the data structures
950 * Must handshake with nqnfs_clientd() if it is active.
952 nmp
->nm_flag
|= NFSMNT_DISMINPROG
;
953 while (nmp
->nm_inprog
!= NULLVP
)
954 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfsdism", 0);
956 * vflush will check for busy vnodes on mountpoint.
957 * Will do the right thing for MNT_FORCE. That is, we should
958 * not get EBUSY back.
960 error
= vflush(mp
, vp
, SKIPSWAP
| flags
);
961 if (mntflags
& MNT_FORCE
)
962 error
= vflush(mp
, NULLVP
, flags
); /* locks vp in the process */
964 if (vp
->v_usecount
> 1) {
965 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
968 error
= vflush(mp
, vp
, flags
);
972 nmp
->nm_flag
&= ~NFSMNT_DISMINPROG
;
977 * We are now committed to the unmount.
978 * For NQNFS, let the server daemon free the nfsmount structure.
980 if (nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
))
981 nmp
->nm_flag
|= NFSMNT_DISMNT
;
984 * Release the root vnode reference held by mountnfs()
985 * vflush did the vgone for us when we didn't skip over
986 * it in the MNT_FORCE case. (Thus vp can't be locked when
987 * called vflush in non-skip vp case.)
990 if (!(mntflags
& MNT_FORCE
))
992 mp
->mnt_data
= 0; /* don't want to end up using stale vp */
994 m_freem(nmp
->nm_nam
);
996 if ((nmp
->nm_flag
& (NFSMNT_NQNFS
| NFSMNT_KERB
)) == 0) {
997 register struct nfsreq
*rp
;
999 * Loop through outstanding request list and remove dangling
1000 * references to defunct nfsmount struct
1003 if (hw_atomic_add(&nfsreqqusers
, 1) != 1)
1004 nfsatompanic("unmount add");
1005 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
1006 nfsbtcpu
= cpu_number();
1007 nfsbtthread
= (int)(current_thread());
1010 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
1011 if (rp
->r_nmp
== nmp
)
1012 rp
->r_nmp
= (struct nfsmount
*)0;
1014 if (hw_atomic_sub(&nfsreqqusers
, 1) != 0)
1015 nfsatompanic("unmount sub");
1017 _FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
1023 * Return root of a filesystem
1030 register struct vnode
*vp
;
1031 struct nfsmount
*nmp
;
1036 error
= vget(vp
, LK_EXCLUSIVE
, current_proc());
1039 if (vp
->v_type
== VNON
)
1041 vp
->v_flag
|= VROOT
;
1049 * Flush out the buffer cache
1053 nfs_sync(mp
, waitfor
, cred
, p
)
1059 register struct vnode
*vp
;
1060 int error
, allerror
= 0;
1063 * Force stale buffer cache information to be flushed.
1066 for (vp
= mp
->mnt_vnodelist
.lh_first
;
1068 vp
= vp
->v_mntvnodes
.le_next
) {
1071 * If the vnode that we are about to sync is no longer
1072 * associated with this mount point, start over.
1074 if (vp
->v_mount
!= mp
)
1076 if (VOP_ISLOCKED(vp
) || vp
->v_dirtyblkhd
.lh_first
== NULL
)
1078 if (vget(vp
, LK_EXCLUSIVE
, p
))
1080 didhold
= ubc_hold(vp
);
1081 error
= VOP_FSYNC(vp
, cred
, waitfor
, p
);
1084 VOP_UNLOCK(vp
, 0, p
);
1093 * NFS flat namespace lookup.
1094 * Currently unsupported.
1098 nfs_vget(mp
, ino
, vpp
)
1104 return (EOPNOTSUPP
);
1108 * At this point, this should never happen
1112 nfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1113 register struct mount
*mp
;
1118 struct ucred
**credanonp
;
1125 * Vnode pointer to File handle, should never happen either
1138 * Vfs start routine, a no-op.
1142 nfs_start(mp
, flags
, p
)
1152 * Do operations associated with quotas, not supported
1156 nfs_quotactl(mp
, cmd
, uid
, arg
, p
)
1164 return (EOPNOTSUPP
);
1168 * Do that sysctl thang...
1171 nfs_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
,
1172 size_t newlen
, struct proc
*p
)
1177 * All names at this level are terminal.
1180 return ENOTDIR
; /* overloaded */
1185 *oldlenp
= sizeof nfsstats
;
1189 if(*oldlenp
< sizeof nfsstats
) {
1190 *oldlenp
= sizeof nfsstats
;
1194 rv
= copyout(&nfsstats
, oldp
, sizeof nfsstats
);
1197 if(newp
&& newlen
!= sizeof nfsstats
)
1201 return copyin(newp
, &nfsstats
, sizeof nfsstats
);