Sun Nov 8 01:14:20 2015 UTC ()
Pull up following revision(s) (requested by pgoyette in ticket #1021):
	sys/nfs/nfs_vfsops.c: revision 1.231
Don't forget to call nfs_fini() when we're finished.  Without this,
we leave a dangling pool nfsrvdescpl around.


(riz)
diff -r1.229.2.1 -r1.229.2.2 src/sys/nfs/nfs_vfsops.c

cvs diff -r1.229.2.1 -r1.229.2.2 src/sys/nfs/nfs_vfsops.c (switch to unified diff)

--- src/sys/nfs/nfs_vfsops.c 2015/11/04 17:32:00 1.229.2.1
+++ src/sys/nfs/nfs_vfsops.c 2015/11/08 01:14:20 1.229.2.2
@@ -1,1172 +1,1173 @@ @@ -1,1172 +1,1173 @@
1/* $NetBSD: nfs_vfsops.c,v 1.229.2.1 2015/11/04 17:32:00 riz Exp $ */ 1/* $NetBSD: nfs_vfsops.c,v 1.229.2.2 2015/11/08 01:14:20 riz Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1989, 1993, 1995 4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph. 8 * Rick Macklem at The University of Guelph.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors 18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 * 33 *
34 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95 34 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.229.2.1 2015/11/04 17:32:00 riz Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.229.2.2 2015/11/08 01:14:20 riz Exp $");
39 39
40#if defined(_KERNEL_OPT) 40#if defined(_KERNEL_OPT)
41#include "opt_nfs.h" 41#include "opt_nfs.h"
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <sys/ioctl.h> 45#include <sys/ioctl.h>
46#include <sys/signal.h> 46#include <sys/signal.h>
47#include <sys/proc.h> 47#include <sys/proc.h>
48#include <sys/namei.h> 48#include <sys/namei.h>
49#include <sys/device.h> 49#include <sys/device.h>
50#include <sys/vnode.h> 50#include <sys/vnode.h>
51#include <sys/kernel.h> 51#include <sys/kernel.h>
52#include <sys/mount.h> 52#include <sys/mount.h>
53#include <sys/buf.h> 53#include <sys/buf.h>
54#include <sys/mbuf.h> 54#include <sys/mbuf.h>
55#include <sys/dirent.h> 55#include <sys/dirent.h>
56#include <sys/socket.h> 56#include <sys/socket.h>
57#include <sys/socketvar.h> 57#include <sys/socketvar.h>
58#include <sys/sysctl.h> 58#include <sys/sysctl.h>
59#include <sys/systm.h> 59#include <sys/systm.h>
60#include <sys/timetc.h> 60#include <sys/timetc.h>
61#include <sys/kauth.h> 61#include <sys/kauth.h>
62#include <sys/module.h> 62#include <sys/module.h>
63 63
64#include <net/if.h> 64#include <net/if.h>
65#include <net/route.h> 65#include <net/route.h>
66#include <netinet/in.h> 66#include <netinet/in.h>
67 67
68#include <nfs/rpcv2.h> 68#include <nfs/rpcv2.h>
69#include <nfs/nfsproto.h> 69#include <nfs/nfsproto.h>
70#include <nfs/nfsnode.h> 70#include <nfs/nfsnode.h>
71#include <nfs/nfs.h> 71#include <nfs/nfs.h>
72#include <nfs/nfsmount.h> 72#include <nfs/nfsmount.h>
73#include <nfs/xdr_subs.h> 73#include <nfs/xdr_subs.h>
74#include <nfs/nfsm_subs.h> 74#include <nfs/nfsm_subs.h>
75#include <nfs/nfsdiskless.h> 75#include <nfs/nfsdiskless.h>
76#include <nfs/nfs_var.h> 76#include <nfs/nfs_var.h>
77 77
78MODULE(MODULE_CLASS_VFS, nfs, NULL); 78MODULE(MODULE_CLASS_VFS, nfs, NULL);
79 79
80extern struct nfsstats nfsstats; 80extern struct nfsstats nfsstats;
81extern int nfs_ticks; 81extern int nfs_ticks;
82 82
83/* 83/*
84 * keep a count of the nfs mounts to generate ficticious drive names 84 * keep a count of the nfs mounts to generate ficticious drive names
85 * for the per drive stats. 85 * for the per drive stats.
86 */ 86 */
87unsigned int nfs_mount_count = 0; 87unsigned int nfs_mount_count = 0;
88 88
89int nfs_commitsize; 89int nfs_commitsize;
90 90
91/* 91/*
92 * nfs vfs operations. 92 * nfs vfs operations.
93 */ 93 */
94 94
95extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc; 95extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
96extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc; 96extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
97extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc; 97extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
98 98
99const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = { 99const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
100 &nfsv2_vnodeop_opv_desc, 100 &nfsv2_vnodeop_opv_desc,
101 &spec_nfsv2nodeop_opv_desc, 101 &spec_nfsv2nodeop_opv_desc,
102 &fifo_nfsv2nodeop_opv_desc, 102 &fifo_nfsv2nodeop_opv_desc,
103 NULL, 103 NULL,
104}; 104};
105 105
106struct vfsops nfs_vfsops = { 106struct vfsops nfs_vfsops = {
107 .vfs_name = MOUNT_NFS, 107 .vfs_name = MOUNT_NFS,
108 .vfs_min_mount_data = sizeof (struct nfs_args), 108 .vfs_min_mount_data = sizeof (struct nfs_args),
109 .vfs_mount = nfs_mount, 109 .vfs_mount = nfs_mount,
110 .vfs_start = nfs_start, 110 .vfs_start = nfs_start,
111 .vfs_unmount = nfs_unmount, 111 .vfs_unmount = nfs_unmount,
112 .vfs_root = nfs_root, 112 .vfs_root = nfs_root,
113 .vfs_quotactl = (void *)eopnotsupp, 113 .vfs_quotactl = (void *)eopnotsupp,
114 .vfs_statvfs = nfs_statvfs, 114 .vfs_statvfs = nfs_statvfs,
115 .vfs_sync = nfs_sync, 115 .vfs_sync = nfs_sync,
116 .vfs_loadvnode = nfs_loadvnode, 116 .vfs_loadvnode = nfs_loadvnode,
117 .vfs_vget = nfs_vget, 117 .vfs_vget = nfs_vget,
118 .vfs_fhtovp = nfs_fhtovp, 118 .vfs_fhtovp = nfs_fhtovp,
119 .vfs_vptofh = nfs_vptofh, 119 .vfs_vptofh = nfs_vptofh,
120 .vfs_init = nfs_vfs_init, 120 .vfs_init = nfs_vfs_init,
121 .vfs_done = nfs_vfs_done, 121 .vfs_done = nfs_vfs_done,
122 .vfs_mountroot = nfs_mountroot, 122 .vfs_mountroot = nfs_mountroot,
123 .vfs_snapshot = (void *)eopnotsupp, 123 .vfs_snapshot = (void *)eopnotsupp,
124 .vfs_extattrctl = vfs_stdextattrctl, 124 .vfs_extattrctl = vfs_stdextattrctl,
125 .vfs_suspendctl = (void *)eopnotsupp, 125 .vfs_suspendctl = (void *)eopnotsupp,
126 .vfs_renamelock_enter = genfs_renamelock_enter, 126 .vfs_renamelock_enter = genfs_renamelock_enter,
127 .vfs_renamelock_exit = genfs_renamelock_exit, 127 .vfs_renamelock_exit = genfs_renamelock_exit,
128 .vfs_fsync = (void *)eopnotsupp, 128 .vfs_fsync = (void *)eopnotsupp,
129 .vfs_opv_descs = nfs_vnodeopv_descs 129 .vfs_opv_descs = nfs_vnodeopv_descs
130}; 130};
131 131
132extern u_int32_t nfs_procids[NFS_NPROCS]; 132extern u_int32_t nfs_procids[NFS_NPROCS];
133extern u_int32_t nfs_prog, nfs_vers; 133extern u_int32_t nfs_prog, nfs_vers;
134static struct sysctllog *nfs_clog; 134static struct sysctllog *nfs_clog;
135 135
136static int nfs_mount_diskless(struct nfs_dlmount *, const char *, 136static int nfs_mount_diskless(struct nfs_dlmount *, const char *,
137 struct mount **, struct vnode **, struct lwp *); 137 struct mount **, struct vnode **, struct lwp *);
138static void nfs_sysctl_init(void); 138static void nfs_sysctl_init(void);
139static void nfs_sysctl_fini(void); 139static void nfs_sysctl_fini(void);
140 140
141static int 141static int
142nfs_modcmd(modcmd_t cmd, void *arg) 142nfs_modcmd(modcmd_t cmd, void *arg)
143{ 143{
144 int error; 144 int error;
145 145
146 switch (cmd) { 146 switch (cmd) {
147 case MODULE_CMD_INIT: 147 case MODULE_CMD_INIT:
148 error = vfs_attach(&nfs_vfsops); 148 error = vfs_attach(&nfs_vfsops);
149 if (error == 0) { 149 if (error == 0) {
150 nfs_sysctl_init(); 150 nfs_sysctl_init();
151 } 151 }
152 return error; 152 return error;
153 case MODULE_CMD_FINI: 153 case MODULE_CMD_FINI:
154 error = vfs_detach(&nfs_vfsops); 154 error = vfs_detach(&nfs_vfsops);
155 if (error == 0) { 155 if (error == 0) {
156 nfs_sysctl_fini(); 156 nfs_sysctl_fini();
157 } 157 }
158 return error; 158 return error;
159 default: 159 default:
160 return ENOTTY; 160 return ENOTTY;
161 } 161 }
162} 162}
163 163
164/* 164/*
165 * nfs statvfs call 165 * nfs statvfs call
166 */ 166 */
167int 167int
168nfs_statvfs(struct mount *mp, struct statvfs *sbp) 168nfs_statvfs(struct mount *mp, struct statvfs *sbp)
169{ 169{
170 struct lwp *l = curlwp; 170 struct lwp *l = curlwp;
171 struct vnode *vp; 171 struct vnode *vp;
172 struct nfs_statfs *sfp; 172 struct nfs_statfs *sfp;
173 char *cp; 173 char *cp;
174 u_int32_t *tl; 174 u_int32_t *tl;
175 int32_t t1, t2; 175 int32_t t1, t2;
176 char *bpos, *dpos, *cp2; 176 char *bpos, *dpos, *cp2;
177 struct nfsmount *nmp = VFSTONFS(mp); 177 struct nfsmount *nmp = VFSTONFS(mp);
178 int error = 0, retattr; 178 int error = 0, retattr;
179#ifdef NFS_V2_ONLY 179#ifdef NFS_V2_ONLY
180 const int v3 = 0; 180 const int v3 = 0;
181#else 181#else
182 int v3 = (nmp->nm_flag & NFSMNT_NFSV3); 182 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
183#endif 183#endif
184 struct mbuf *mreq, *mrep = NULL, *md, *mb; 184 struct mbuf *mreq, *mrep = NULL, *md, *mb;
185 kauth_cred_t cred; 185 kauth_cred_t cred;
186 u_quad_t tquad; 186 u_quad_t tquad;
187 struct nfsnode *np; 187 struct nfsnode *np;
188 188
189#ifndef nolint 189#ifndef nolint
190 sfp = (struct nfs_statfs *)0; 190 sfp = (struct nfs_statfs *)0;
191#endif 191#endif
192 vp = nmp->nm_vnode; 192 vp = nmp->nm_vnode;
193 np = VTONFS(vp); 193 np = VTONFS(vp);
194 cred = kauth_cred_alloc(); 194 cred = kauth_cred_alloc();
195#ifndef NFS_V2_ONLY 195#ifndef NFS_V2_ONLY
196 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0) 196 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
197 (void)nfs_fsinfo(nmp, vp, cred, l); 197 (void)nfs_fsinfo(nmp, vp, cred, l);
198#endif 198#endif
199 nfsstats.rpccnt[NFSPROC_FSSTAT]++; 199 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
200 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3)); 200 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
201 nfsm_fhtom(np, v3); 201 nfsm_fhtom(np, v3);
202 nfsm_request(np, NFSPROC_FSSTAT, l, cred); 202 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
203 if (v3) 203 if (v3)
204 nfsm_postop_attr(vp, retattr, 0); 204 nfsm_postop_attr(vp, retattr, 0);
205 if (error) { 205 if (error) {
206 if (mrep != NULL) { 206 if (mrep != NULL) {
207 if (mrep->m_next != NULL) 207 if (mrep->m_next != NULL)
208 printf("nfs_vfsops: nfs_statvfs would lose buffers\n"); 208 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
209 m_freem(mrep); 209 m_freem(mrep);
210 } 210 }
211 goto nfsmout; 211 goto nfsmout;
212 } 212 }
213 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 213 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
214 sbp->f_flag = nmp->nm_flag; 214 sbp->f_flag = nmp->nm_flag;
215 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize); 215 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
216 if (v3) { 216 if (v3) {
217 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE; 217 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
218 tquad = fxdr_hyper(&sfp->sf_tbytes); 218 tquad = fxdr_hyper(&sfp->sf_tbytes);
219 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 219 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
220 tquad = fxdr_hyper(&sfp->sf_fbytes); 220 tquad = fxdr_hyper(&sfp->sf_fbytes);
221 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 221 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
222 tquad = fxdr_hyper(&sfp->sf_abytes); 222 tquad = fxdr_hyper(&sfp->sf_abytes);
223 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 223 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
224 sbp->f_bresvd = sbp->f_bfree - tquad; 224 sbp->f_bresvd = sbp->f_bfree - tquad;
225 sbp->f_bavail = tquad; 225 sbp->f_bavail = tquad;
226 /* Handle older NFS servers returning negative values */ 226 /* Handle older NFS servers returning negative values */
227 if ((quad_t)sbp->f_bavail < 0) 227 if ((quad_t)sbp->f_bavail < 0)
228 sbp->f_bavail = 0; 228 sbp->f_bavail = 0;
229 tquad = fxdr_hyper(&sfp->sf_tfiles); 229 tquad = fxdr_hyper(&sfp->sf_tfiles);
230 sbp->f_files = tquad; 230 sbp->f_files = tquad;
231 tquad = fxdr_hyper(&sfp->sf_ffiles); 231 tquad = fxdr_hyper(&sfp->sf_ffiles);
232 sbp->f_ffree = tquad; 232 sbp->f_ffree = tquad;
233 sbp->f_favail = tquad; 233 sbp->f_favail = tquad;
234 sbp->f_fresvd = 0; 234 sbp->f_fresvd = 0;
235 sbp->f_namemax = NFS_MAXNAMLEN; 235 sbp->f_namemax = NFS_MAXNAMLEN;
236 } else { 236 } else {
237 sbp->f_bsize = NFS_FABLKSIZE; 237 sbp->f_bsize = NFS_FABLKSIZE;
238 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize); 238 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
239 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks); 239 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
240 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree); 240 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
241 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail); 241 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
242 sbp->f_fresvd = 0; 242 sbp->f_fresvd = 0;
243 sbp->f_files = 0; 243 sbp->f_files = 0;
244 sbp->f_ffree = 0; 244 sbp->f_ffree = 0;
245 sbp->f_favail = 0; 245 sbp->f_favail = 0;
246 sbp->f_fresvd = 0; 246 sbp->f_fresvd = 0;
247 sbp->f_namemax = NFS_MAXNAMLEN; 247 sbp->f_namemax = NFS_MAXNAMLEN;
248 } 248 }
249 copy_statvfs_info(sbp, mp); 249 copy_statvfs_info(sbp, mp);
250 nfsm_reqdone; 250 nfsm_reqdone;
251 kauth_cred_free(cred); 251 kauth_cred_free(cred);
252 return (error); 252 return (error);
253} 253}
254 254
255#ifndef NFS_V2_ONLY 255#ifndef NFS_V2_ONLY
256/* 256/*
257 * nfs version 3 fsinfo rpc call 257 * nfs version 3 fsinfo rpc call
258 */ 258 */
259int 259int
260nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, kauth_cred_t cred, struct lwp *l) 260nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, kauth_cred_t cred, struct lwp *l)
261{ 261{
262 struct nfsv3_fsinfo *fsp; 262 struct nfsv3_fsinfo *fsp;
263 char *cp; 263 char *cp;
264 int32_t t1, t2; 264 int32_t t1, t2;
265 u_int32_t *tl, pref, xmax; 265 u_int32_t *tl, pref, xmax;
266 char *bpos, *dpos, *cp2; 266 char *bpos, *dpos, *cp2;
267 int error = 0, retattr; 267 int error = 0, retattr;
268 struct mbuf *mreq, *mrep, *md, *mb; 268 struct mbuf *mreq, *mrep, *md, *mb;
269 u_int64_t maxfsize; 269 u_int64_t maxfsize;
270 struct nfsnode *np = VTONFS(vp); 270 struct nfsnode *np = VTONFS(vp);
271 271
272 nfsstats.rpccnt[NFSPROC_FSINFO]++; 272 nfsstats.rpccnt[NFSPROC_FSINFO]++;
273 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1)); 273 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
274 nfsm_fhtom(np, 1); 274 nfsm_fhtom(np, 1);
275 nfsm_request(np, NFSPROC_FSINFO, l, cred); 275 nfsm_request(np, NFSPROC_FSINFO, l, cred);
276 nfsm_postop_attr(vp, retattr, 0); 276 nfsm_postop_attr(vp, retattr, 0);
277 if (!error) { 277 if (!error) {
278 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 278 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
279 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref); 279 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
280 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 && 280 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
281 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE) 281 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
282 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) & 282 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
283 ~(NFS_FABLKSIZE - 1); 283 ~(NFS_FABLKSIZE - 1);
284 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax); 284 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
285 if (xmax < nmp->nm_wsize && xmax > 0) { 285 if (xmax < nmp->nm_wsize && xmax > 0) {
286 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1); 286 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
287 if (nmp->nm_wsize == 0) 287 if (nmp->nm_wsize == 0)
288 nmp->nm_wsize = xmax; 288 nmp->nm_wsize = xmax;
289 } 289 }
290 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref); 290 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
291 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 && 291 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
292 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE) 292 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
293 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) & 293 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
294 ~(NFS_FABLKSIZE - 1); 294 ~(NFS_FABLKSIZE - 1);
295 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax); 295 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
296 if (xmax < nmp->nm_rsize && xmax > 0) { 296 if (xmax < nmp->nm_rsize && xmax > 0) {
297 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1); 297 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
298 if (nmp->nm_rsize == 0) 298 if (nmp->nm_rsize == 0)
299 nmp->nm_rsize = xmax; 299 nmp->nm_rsize = xmax;
300 } 300 }
301 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref); 301 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
302 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ) 302 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
303 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) & 303 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
304 ~(NFS_DIRFRAGSIZ - 1); 304 ~(NFS_DIRFRAGSIZ - 1);
305 if (xmax < nmp->nm_readdirsize && xmax > 0) { 305 if (xmax < nmp->nm_readdirsize && xmax > 0) {
306 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1); 306 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
307 if (nmp->nm_readdirsize == 0) 307 if (nmp->nm_readdirsize == 0)
308 nmp->nm_readdirsize = xmax; 308 nmp->nm_readdirsize = xmax;
309 } 309 }
310 /* XXX */ 310 /* XXX */
311 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1; 311 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
312 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize); 312 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
313 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize) 313 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
314 nmp->nm_maxfilesize = maxfsize; 314 nmp->nm_maxfilesize = maxfsize;
315 nmp->nm_mountp->mnt_fs_bshift = 315 nmp->nm_mountp->mnt_fs_bshift =
316 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1; 316 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
317 nmp->nm_iflag |= NFSMNT_GOTFSINFO; 317 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
318 } 318 }
319 nfsm_reqdone; 319 nfsm_reqdone;
320 return (error); 320 return (error);
321} 321}
322#endif 322#endif
323 323
324/* 324/*
325 * Mount a remote root fs via. NFS. It goes like this: 325 * Mount a remote root fs via. NFS. It goes like this:
326 * - Call nfs_boot_init() to fill in the nfs_diskless struct 326 * - Call nfs_boot_init() to fill in the nfs_diskless struct
327 * - build the rootfs mount point and call mountnfs() to do the rest. 327 * - build the rootfs mount point and call mountnfs() to do the rest.
328 */ 328 */
329int 329int
330nfs_mountroot(void) 330nfs_mountroot(void)
331{ 331{
332 struct timespec ts; 332 struct timespec ts;
333 struct nfs_diskless *nd; 333 struct nfs_diskless *nd;
334 struct vattr attr; 334 struct vattr attr;
335 struct mount *mp; 335 struct mount *mp;
336 struct vnode *vp; 336 struct vnode *vp;
337 struct lwp *l; 337 struct lwp *l;
338 long n; 338 long n;
339 int error; 339 int error;
340 340
341 l = curlwp; /* XXX */ 341 l = curlwp; /* XXX */
342 342
343 if (device_class(root_device) != DV_IFNET) 343 if (device_class(root_device) != DV_IFNET)
344 return (ENODEV); 344 return (ENODEV);
345 345
346 /* 346 /*
347 * XXX time must be non-zero when we init the interface or else 347 * XXX time must be non-zero when we init the interface or else
348 * the arp code will wedge. [Fixed now in if_ether.c] 348 * the arp code will wedge. [Fixed now in if_ether.c]
349 * However, the NFS attribute cache gives false "hits" when the 349 * However, the NFS attribute cache gives false "hits" when the
350 * current time < nfs_attrtimeo(nmp, np) so keep this in for now. 350 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
351 */ 351 */
352 if (time_second < NFS_MAXATTRTIMO) { 352 if (time_second < NFS_MAXATTRTIMO) {
353 ts.tv_sec = NFS_MAXATTRTIMO; 353 ts.tv_sec = NFS_MAXATTRTIMO;
354 ts.tv_nsec = 0; 354 ts.tv_nsec = 0;
355 tc_setclock(&ts); 355 tc_setclock(&ts);
356 } 356 }
357 357
358 /* 358 /*
359 * Call nfs_boot_init() to fill in the nfs_diskless struct. 359 * Call nfs_boot_init() to fill in the nfs_diskless struct.
360 * Side effect: Finds and configures a network interface. 360 * Side effect: Finds and configures a network interface.
361 */ 361 */
362 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP); 362 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
363 error = nfs_boot_init(nd, l); 363 error = nfs_boot_init(nd, l);
364 if (error) { 364 if (error) {
365 kmem_free(nd, sizeof(*nd)); 365 kmem_free(nd, sizeof(*nd));
366 return (error); 366 return (error);
367 } 367 }
368 368
369 /* 369 /*
370 * Create the root mount point. 370 * Create the root mount point.
371 */ 371 */
372 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l); 372 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
373 if (error) 373 if (error)
374 goto out; 374 goto out;
375 printf("root on %s\n", nd->nd_root.ndm_host); 375 printf("root on %s\n", nd->nd_root.ndm_host);
376 376
377 /* 377 /*
378 * Link it into the mount list. 378 * Link it into the mount list.
379 */ 379 */
380 mountlist_append(mp); 380 mountlist_append(mp);
381 rootvp = vp; 381 rootvp = vp;
382 mp->mnt_vnodecovered = NULLVP; 382 mp->mnt_vnodecovered = NULLVP;
383 vfs_unbusy(mp, false, NULL); 383 vfs_unbusy(mp, false, NULL);
384 384
385 /* Get root attributes (for the time). */ 385 /* Get root attributes (for the time). */
386 vn_lock(vp, LK_SHARED | LK_RETRY); 386 vn_lock(vp, LK_SHARED | LK_RETRY);
387 error = VOP_GETATTR(vp, &attr, l->l_cred); 387 error = VOP_GETATTR(vp, &attr, l->l_cred);
388 VOP_UNLOCK(vp); 388 VOP_UNLOCK(vp);
389 if (error) 389 if (error)
390 panic("nfs_mountroot: getattr for root"); 390 panic("nfs_mountroot: getattr for root");
391 n = attr.va_atime.tv_sec; 391 n = attr.va_atime.tv_sec;
392#ifdef DEBUG 392#ifdef DEBUG
393 printf("root time: 0x%lx\n", n); 393 printf("root time: 0x%lx\n", n);
394#endif 394#endif
395 setrootfstime(n); 395 setrootfstime(n);
396 396
397out: 397out:
398 if (error) 398 if (error)
399 nfs_boot_cleanup(nd, l); 399 nfs_boot_cleanup(nd, l);
400 kmem_free(nd, sizeof(*nd)); 400 kmem_free(nd, sizeof(*nd));
401 return (error); 401 return (error);
402} 402}
403 403
404/* 404/*
405 * Internal version of mount system call for diskless setup. 405 * Internal version of mount system call for diskless setup.
406 * Separate function because we used to call it twice. 406 * Separate function because we used to call it twice.
407 * (once for root and once for swap) 407 * (once for root and once for swap)
408 */ 408 */
409static int 409static int
410nfs_mount_diskless(struct nfs_dlmount *ndmntp, const char *mntname, struct mount **mpp, struct vnode **vpp, struct lwp *l) 410nfs_mount_diskless(struct nfs_dlmount *ndmntp, const char *mntname, struct mount **mpp, struct vnode **vpp, struct lwp *l)
411 /* mntname: mount point name */ 411 /* mntname: mount point name */
412{ 412{
413 struct mount *mp; 413 struct mount *mp;
414 struct mbuf *m; 414 struct mbuf *m;
415 int error; 415 int error;
416 416
417 vfs_rootmountalloc(MOUNT_NFS, mntname, &mp); 417 vfs_rootmountalloc(MOUNT_NFS, mntname, &mp);
418 418
419 mp->mnt_op = &nfs_vfsops; 419 mp->mnt_op = &nfs_vfsops;
420 420
421 /* 421 /*
422 * Historical practice expects NFS root file systems to 422 * Historical practice expects NFS root file systems to
423 * be initially mounted r/w. 423 * be initially mounted r/w.
424 */ 424 */
425 mp->mnt_flag &= ~MNT_RDONLY; 425 mp->mnt_flag &= ~MNT_RDONLY;
426 426
427 /* Get mbuf for server sockaddr. */ 427 /* Get mbuf for server sockaddr. */
428 m = m_get(M_WAIT, MT_SONAME); 428 m = m_get(M_WAIT, MT_SONAME);
429 if (m == NULL) 429 if (m == NULL)
430 panic("nfs_mountroot: mget soname for %s", mntname); 430 panic("nfs_mountroot: mget soname for %s", mntname);
431 MCLAIM(m, &nfs_mowner); 431 MCLAIM(m, &nfs_mowner);
432 memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr, 432 memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr,
433 (m->m_len = ndmntp->ndm_args.addr->sa_len)); 433 (m->m_len = ndmntp->ndm_args.addr->sa_len));
434 434
435 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname, 435 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
436 ndmntp->ndm_args.hostname, vpp, l); 436 ndmntp->ndm_args.hostname, vpp, l);
437 if (error) { 437 if (error) {
438 vfs_unbusy(mp, false, NULL); 438 vfs_unbusy(mp, false, NULL);
439 vfs_destroy(mp); 439 vfs_destroy(mp);
440 printf("nfs_mountroot: mount %s failed: %d\n", 440 printf("nfs_mountroot: mount %s failed: %d\n",
441 mntname, error); 441 mntname, error);
442 } else 442 } else
443 *mpp = mp; 443 *mpp = mp;
444 444
445 return (error); 445 return (error);
446} 446}
447 447
448void 448void
449nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, struct lwp *l) 449nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, struct lwp *l)
450{ 450{
451 int s; 451 int s;
452 int adjsock; 452 int adjsock;
453 int maxio; 453 int maxio;
454 454
455 s = splsoftnet(); 455 s = splsoftnet();
456 456
457 /* 457 /*
458 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes 458 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
459 * no sense in that context. 459 * no sense in that context.
460 */ 460 */
461 if (argp->sotype == SOCK_STREAM) 461 if (argp->sotype == SOCK_STREAM)
462 argp->flags &= ~NFSMNT_NOCONN; 462 argp->flags &= ~NFSMNT_NOCONN;
463 463
464 /* 464 /*
465 * Cookie translation is not needed for v2, silently ignore it. 465 * Cookie translation is not needed for v2, silently ignore it.
466 */ 466 */
467 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) == 467 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
468 NFSMNT_XLATECOOKIE) 468 NFSMNT_XLATECOOKIE)
469 argp->flags &= ~NFSMNT_XLATECOOKIE; 469 argp->flags &= ~NFSMNT_XLATECOOKIE;
470 470
471 /* Re-bind if rsrvd port requested and wasn't on one */ 471 /* Re-bind if rsrvd port requested and wasn't on one */
472 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 472 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
473 && (argp->flags & NFSMNT_RESVPORT); 473 && (argp->flags & NFSMNT_RESVPORT);
474 /* Also re-bind if we're switching to/from a connected UDP socket */ 474 /* Also re-bind if we're switching to/from a connected UDP socket */
475 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 475 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
476 (argp->flags & NFSMNT_NOCONN)); 476 (argp->flags & NFSMNT_NOCONN));
477 477
478 /* Update flags. */ 478 /* Update flags. */
479 nmp->nm_flag = argp->flags; 479 nmp->nm_flag = argp->flags;
480 splx(s); 480 splx(s);
481 481
482 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 482 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
483 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 483 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
484 if (nmp->nm_timeo < NFS_MINTIMEO) 484 if (nmp->nm_timeo < NFS_MINTIMEO)
485 nmp->nm_timeo = NFS_MINTIMEO; 485 nmp->nm_timeo = NFS_MINTIMEO;
486 else if (nmp->nm_timeo > NFS_MAXTIMEO) 486 else if (nmp->nm_timeo > NFS_MAXTIMEO)
487 nmp->nm_timeo = NFS_MAXTIMEO; 487 nmp->nm_timeo = NFS_MAXTIMEO;
488 } 488 }
489 489
490 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 490 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
491 nmp->nm_retry = argp->retrans; 491 nmp->nm_retry = argp->retrans;
492 if (nmp->nm_retry > NFS_MAXREXMIT) 492 if (nmp->nm_retry > NFS_MAXREXMIT)
493 nmp->nm_retry = NFS_MAXREXMIT; 493 nmp->nm_retry = NFS_MAXREXMIT;
494 } 494 }
495 495
496#ifndef NFS_V2_ONLY 496#ifndef NFS_V2_ONLY
497 if (argp->flags & NFSMNT_NFSV3) { 497 if (argp->flags & NFSMNT_NFSV3) {
498 if (argp->sotype == SOCK_DGRAM) 498 if (argp->sotype == SOCK_DGRAM)
499 maxio = NFS_MAXDGRAMDATA; 499 maxio = NFS_MAXDGRAMDATA;
500 else 500 else
501 maxio = NFS_MAXDATA; 501 maxio = NFS_MAXDATA;
502 } else 502 } else
503#endif 503#endif
504 maxio = NFS_V2MAXDATA; 504 maxio = NFS_V2MAXDATA;
505 505
506 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 506 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
507 int osize = nmp->nm_wsize; 507 int osize = nmp->nm_wsize;
508 nmp->nm_wsize = argp->wsize; 508 nmp->nm_wsize = argp->wsize;
509 /* Round down to multiple of blocksize */ 509 /* Round down to multiple of blocksize */
510 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 510 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
511 if (nmp->nm_wsize <= 0) 511 if (nmp->nm_wsize <= 0)
512 nmp->nm_wsize = NFS_FABLKSIZE; 512 nmp->nm_wsize = NFS_FABLKSIZE;
513 adjsock |= (nmp->nm_wsize != osize); 513 adjsock |= (nmp->nm_wsize != osize);
514 } 514 }
515 if (nmp->nm_wsize > maxio) 515 if (nmp->nm_wsize > maxio)
516 nmp->nm_wsize = maxio; 516 nmp->nm_wsize = maxio;
517 if (nmp->nm_wsize > MAXBSIZE) 517 if (nmp->nm_wsize > MAXBSIZE)
518 nmp->nm_wsize = MAXBSIZE; 518 nmp->nm_wsize = MAXBSIZE;
519 519
520 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 520 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
521 int osize = nmp->nm_rsize; 521 int osize = nmp->nm_rsize;
522 nmp->nm_rsize = argp->rsize; 522 nmp->nm_rsize = argp->rsize;
523 /* Round down to multiple of blocksize */ 523 /* Round down to multiple of blocksize */
524 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 524 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
525 if (nmp->nm_rsize <= 0) 525 if (nmp->nm_rsize <= 0)
526 nmp->nm_rsize = NFS_FABLKSIZE; 526 nmp->nm_rsize = NFS_FABLKSIZE;
527 adjsock |= (nmp->nm_rsize != osize); 527 adjsock |= (nmp->nm_rsize != osize);
528 } 528 }
529 if (nmp->nm_rsize > maxio) 529 if (nmp->nm_rsize > maxio)
530 nmp->nm_rsize = maxio; 530 nmp->nm_rsize = maxio;
531 if (nmp->nm_rsize > MAXBSIZE) 531 if (nmp->nm_rsize > MAXBSIZE)
532 nmp->nm_rsize = MAXBSIZE; 532 nmp->nm_rsize = MAXBSIZE;
533 533
534 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 534 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
535 nmp->nm_readdirsize = argp->readdirsize; 535 nmp->nm_readdirsize = argp->readdirsize;
536 /* Round down to multiple of minimum blocksize */ 536 /* Round down to multiple of minimum blocksize */
537 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1); 537 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
538 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ) 538 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
539 nmp->nm_readdirsize = NFS_DIRFRAGSIZ; 539 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
540 /* Bigger than buffer size makes no sense */ 540 /* Bigger than buffer size makes no sense */
541 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ) 541 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
542 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 542 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
543 } else if (argp->flags & NFSMNT_RSIZE) 543 } else if (argp->flags & NFSMNT_RSIZE)
544 nmp->nm_readdirsize = nmp->nm_rsize; 544 nmp->nm_readdirsize = nmp->nm_rsize;
545 545
546 if (nmp->nm_readdirsize > maxio) 546 if (nmp->nm_readdirsize > maxio)
547 nmp->nm_readdirsize = maxio; 547 nmp->nm_readdirsize = maxio;
548 548
549 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 && 549 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
550 argp->maxgrouplist <= NFS_MAXGRPS) 550 argp->maxgrouplist <= NFS_MAXGRPS)
551 nmp->nm_numgrps = argp->maxgrouplist; 551 nmp->nm_numgrps = argp->maxgrouplist;
552 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 && 552 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
553 argp->readahead <= NFS_MAXRAHEAD) 553 argp->readahead <= NFS_MAXRAHEAD)
554 nmp->nm_readahead = argp->readahead; 554 nmp->nm_readahead = argp->readahead;
555 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 && 555 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
556 argp->deadthresh <= NFS_NEVERDEAD) 556 argp->deadthresh <= NFS_NEVERDEAD)
557 nmp->nm_deadthresh = argp->deadthresh; 557 nmp->nm_deadthresh = argp->deadthresh;
558 558
559 adjsock |= ((nmp->nm_sotype != argp->sotype) || 559 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
560 (nmp->nm_soproto != argp->proto)); 560 (nmp->nm_soproto != argp->proto));
561 nmp->nm_sotype = argp->sotype; 561 nmp->nm_sotype = argp->sotype;
562 nmp->nm_soproto = argp->proto; 562 nmp->nm_soproto = argp->proto;
563 563
564 if (nmp->nm_so && adjsock) { 564 if (nmp->nm_so && adjsock) {
565 nfs_safedisconnect(nmp); 565 nfs_safedisconnect(nmp);
566 if (nmp->nm_sotype == SOCK_DGRAM) 566 if (nmp->nm_sotype == SOCK_DGRAM)
567 while (nfs_connect(nmp, (struct nfsreq *)0, l)) { 567 while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
568 printf("nfs_args: retrying connect\n"); 568 printf("nfs_args: retrying connect\n");
569 kpause("nfscn3", false, hz, NULL); 569 kpause("nfscn3", false, hz, NULL);
570 } 570 }
571 } 571 }
572} 572}
573 573
574/* 574/*
575 * VFS Operations. 575 * VFS Operations.
576 * 576 *
577 * mount system call 577 * mount system call
578 * It seems a bit dumb to copyinstr() the host and path here and then 578 * It seems a bit dumb to copyinstr() the host and path here and then
579 * memcpy() them in mountnfs(), but I wanted to detect errors before 579 * memcpy() them in mountnfs(), but I wanted to detect errors before
580 * doing the sockargs() call because sockargs() allocates an mbuf and 580 * doing the sockargs() call because sockargs() allocates an mbuf and
581 * an error after that means that I have to release the mbuf. 581 * an error after that means that I have to release the mbuf.
582 */ 582 */
583/* ARGSUSED */ 583/* ARGSUSED */
584int 584int
585nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 585nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
586{ 586{
587 struct lwp *l = curlwp; 587 struct lwp *l = curlwp;
588 int error; 588 int error;
589 struct nfs_args *args = data; 589 struct nfs_args *args = data;
590 struct mbuf *nam; 590 struct mbuf *nam;
591 struct nfsmount *nmp = VFSTONFS(mp); 591 struct nfsmount *nmp = VFSTONFS(mp);
592 struct sockaddr *sa; 592 struct sockaddr *sa;
593 struct vnode *vp; 593 struct vnode *vp;
594 char *pth, *hst; 594 char *pth, *hst;
595 size_t len; 595 size_t len;
596 u_char *nfh; 596 u_char *nfh;
597 597
598 if (args == NULL) 598 if (args == NULL)
599 return EINVAL; 599 return EINVAL;
600 if (*data_len < sizeof *args) 600 if (*data_len < sizeof *args)
601 return EINVAL; 601 return EINVAL;
602 602
603 if (mp->mnt_flag & MNT_GETARGS) { 603 if (mp->mnt_flag & MNT_GETARGS) {
604 604
605 if (nmp == NULL) 605 if (nmp == NULL)
606 return (EIO); 606 return (EIO);
607 if (args->addr != NULL) { 607 if (args->addr != NULL) {
608 sa = mtod(nmp->nm_nam, struct sockaddr *); 608 sa = mtod(nmp->nm_nam, struct sockaddr *);
609 error = copyout(sa, args->addr, sa->sa_len); 609 error = copyout(sa, args->addr, sa->sa_len);
610 if (error) 610 if (error)
611 return (error); 611 return (error);
612 args->addrlen = sa->sa_len; 612 args->addrlen = sa->sa_len;
613 } else 613 } else
614 args->addrlen = 0; 614 args->addrlen = 0;
615 615
616 args->version = NFS_ARGSVERSION; 616 args->version = NFS_ARGSVERSION;
617 args->sotype = nmp->nm_sotype; 617 args->sotype = nmp->nm_sotype;
618 args->proto = nmp->nm_soproto; 618 args->proto = nmp->nm_soproto;
619 args->fh = NULL; 619 args->fh = NULL;
620 args->fhsize = 0; 620 args->fhsize = 0;
621 args->flags = nmp->nm_flag; 621 args->flags = nmp->nm_flag;
622 args->wsize = nmp->nm_wsize; 622 args->wsize = nmp->nm_wsize;
623 args->rsize = nmp->nm_rsize; 623 args->rsize = nmp->nm_rsize;
624 args->readdirsize = nmp->nm_readdirsize; 624 args->readdirsize = nmp->nm_readdirsize;
625 args->timeo = nmp->nm_timeo; 625 args->timeo = nmp->nm_timeo;
626 args->retrans = nmp->nm_retry; 626 args->retrans = nmp->nm_retry;
627 args->maxgrouplist = nmp->nm_numgrps; 627 args->maxgrouplist = nmp->nm_numgrps;
628 args->readahead = nmp->nm_readahead; 628 args->readahead = nmp->nm_readahead;
629 args->leaseterm = 0; /* dummy */ 629 args->leaseterm = 0; /* dummy */
630 args->deadthresh = nmp->nm_deadthresh; 630 args->deadthresh = nmp->nm_deadthresh;
631 args->hostname = NULL; 631 args->hostname = NULL;
632 *data_len = sizeof *args; 632 *data_len = sizeof *args;
633 return 0; 633 return 0;
634 } 634 }
635 635
636 if (args->version != NFS_ARGSVERSION) 636 if (args->version != NFS_ARGSVERSION)
637 return (EPROGMISMATCH); 637 return (EPROGMISMATCH);
638 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB)) 638 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
639 return (EPROGUNAVAIL); 639 return (EPROGUNAVAIL);
640#ifdef NFS_V2_ONLY 640#ifdef NFS_V2_ONLY
641 if (args->flags & NFSMNT_NFSV3) 641 if (args->flags & NFSMNT_NFSV3)
642 return (EPROGMISMATCH); 642 return (EPROGMISMATCH);
643#endif 643#endif
644 if (mp->mnt_flag & MNT_UPDATE) { 644 if (mp->mnt_flag & MNT_UPDATE) {
645 if (nmp == NULL) 645 if (nmp == NULL)
646 return (EIO); 646 return (EIO);
647 /* 647 /*
648 * When doing an update, we can't change from or to 648 * When doing an update, we can't change from or to
649 * v3, or change cookie translation 649 * v3, or change cookie translation
650 */ 650 */
651 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) | 651 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
652 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)); 652 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
653 nfs_decode_args(nmp, args, l); 653 nfs_decode_args(nmp, args, l);
654 return (0); 654 return (0);
655 } 655 }
656 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX) 656 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
657 return (EINVAL); 657 return (EINVAL);
658 nfh = malloc(NFSX_V3FHMAX, M_TEMP, M_WAITOK); 658 nfh = malloc(NFSX_V3FHMAX, M_TEMP, M_WAITOK);
659 error = copyin(args->fh, nfh, args->fhsize); 659 error = copyin(args->fh, nfh, args->fhsize);
660 if (error) 660 if (error)
661 goto free_nfh; 661 goto free_nfh;
662 pth = malloc(MNAMELEN, M_TEMP, M_WAITOK); 662 pth = malloc(MNAMELEN, M_TEMP, M_WAITOK);
663 error = copyinstr(path, pth, MNAMELEN - 1, &len); 663 error = copyinstr(path, pth, MNAMELEN - 1, &len);
664 if (error) 664 if (error)
665 goto free_pth; 665 goto free_pth;
666 memset(&pth[len], 0, MNAMELEN - len); 666 memset(&pth[len], 0, MNAMELEN - len);
667 hst = malloc(MNAMELEN, M_TEMP, M_WAITOK); 667 hst = malloc(MNAMELEN, M_TEMP, M_WAITOK);
668 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len); 668 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
669 if (error) 669 if (error)
670 goto free_hst; 670 goto free_hst;
671 memset(&hst[len], 0, MNAMELEN - len); 671 memset(&hst[len], 0, MNAMELEN - len);
672 /* sockargs() call must be after above copyin() calls */ 672 /* sockargs() call must be after above copyin() calls */
673 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME); 673 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
674 if (error) 674 if (error)
675 goto free_hst; 675 goto free_hst;
676 MCLAIM(nam, &nfs_mowner); 676 MCLAIM(nam, &nfs_mowner);
677 args->fh = nfh; 677 args->fh = nfh;
678 error = mountnfs(args, mp, nam, pth, hst, &vp, l); 678 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
679 679
680free_hst: 680free_hst:
681 free(hst, M_TEMP); 681 free(hst, M_TEMP);
682free_pth: 682free_pth:
683 free(pth, M_TEMP); 683 free(pth, M_TEMP);
684free_nfh: 684free_nfh:
685 free(nfh, M_TEMP); 685 free(nfh, M_TEMP);
686 686
687 return (error); 687 return (error);
688} 688}
689 689
690/* 690/*
691 * Common code for mount and mountroot 691 * Common code for mount and mountroot
692 */ 692 */
693int 693int
694mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, const char *pth, const char *hst, struct vnode **vpp, struct lwp *l) 694mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, const char *pth, const char *hst, struct vnode **vpp, struct lwp *l)
695{ 695{
696 struct nfsmount *nmp; 696 struct nfsmount *nmp;
697 struct nfsnode *np; 697 struct nfsnode *np;
698 struct vnode *vp; 698 struct vnode *vp;
699 int error; 699 int error;
700 struct vattr *attrs; 700 struct vattr *attrs;
701 kauth_cred_t cr; 701 kauth_cred_t cr;
702 char iosname[IOSTATNAMELEN]; 702 char iosname[IOSTATNAMELEN];
703 703
704 /* 704 /*
705 * If the number of nfs iothreads to use has never 705 * If the number of nfs iothreads to use has never
706 * been set, create a reasonable number of them. 706 * been set, create a reasonable number of them.
707 */ 707 */
708 708
709 if (nfs_niothreads < 0) { 709 if (nfs_niothreads < 0) {
710 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS); 710 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
711 } 711 }
712 712
713 if (mp->mnt_flag & MNT_UPDATE) { 713 if (mp->mnt_flag & MNT_UPDATE) {
714 nmp = VFSTONFS(mp); 714 nmp = VFSTONFS(mp);
715 /* update paths, file handles, etc, here XXX */ 715 /* update paths, file handles, etc, here XXX */
716 m_freem(nam); 716 m_freem(nam);
717 return (0); 717 return (0);
718 } else { 718 } else {
719 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP); 719 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
720 mp->mnt_data = nmp; 720 mp->mnt_data = nmp;
721 TAILQ_INIT(&nmp->nm_uidlruhead); 721 TAILQ_INIT(&nmp->nm_uidlruhead);
722 TAILQ_INIT(&nmp->nm_bufq); 722 TAILQ_INIT(&nmp->nm_bufq);
723 rw_init(&nmp->nm_writeverflock); 723 rw_init(&nmp->nm_writeverflock);
724 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE); 724 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
725 cv_init(&nmp->nm_rcvcv, "nfsrcv"); 725 cv_init(&nmp->nm_rcvcv, "nfsrcv");
726 cv_init(&nmp->nm_sndcv, "nfssnd"); 726 cv_init(&nmp->nm_sndcv, "nfssnd");
727 cv_init(&nmp->nm_aiocv, "nfsaio"); 727 cv_init(&nmp->nm_aiocv, "nfsaio");
728 cv_init(&nmp->nm_disconcv, "nfsdis"); 728 cv_init(&nmp->nm_disconcv, "nfsdis");
729 } 729 }
730 vfs_getnewfsid(mp); 730 vfs_getnewfsid(mp);
731 nmp->nm_mountp = mp; 731 nmp->nm_mountp = mp;
732 732
733#ifndef NFS_V2_ONLY 733#ifndef NFS_V2_ONLY
734 if ((argp->flags & NFSMNT_NFSV3) == 0) 734 if ((argp->flags & NFSMNT_NFSV3) == 0)
735#endif 735#endif
736 { 736 {
737 if (argp->fhsize != NFSX_V2FH) { 737 if (argp->fhsize != NFSX_V2FH) {
738 return EINVAL; 738 return EINVAL;
739 } 739 }
740 } 740 }
741 741
742 /* 742 /*
743 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo 743 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
744 * will overwrite this. 744 * will overwrite this.
745 */ 745 */
746 nmp->nm_maxfilesize = 0xffffffffLL; 746 nmp->nm_maxfilesize = 0xffffffffLL;
747 747
748 nmp->nm_timeo = NFS_TIMEO; 748 nmp->nm_timeo = NFS_TIMEO;
749 nmp->nm_retry = NFS_RETRANS; 749 nmp->nm_retry = NFS_RETRANS;
750 nmp->nm_wsize = NFS_WSIZE; 750 nmp->nm_wsize = NFS_WSIZE;
751 nmp->nm_rsize = NFS_RSIZE; 751 nmp->nm_rsize = NFS_RSIZE;
752 nmp->nm_readdirsize = NFS_READDIRSIZE; 752 nmp->nm_readdirsize = NFS_READDIRSIZE;
753 nmp->nm_numgrps = NFS_MAXGRPS; 753 nmp->nm_numgrps = NFS_MAXGRPS;
754 nmp->nm_readahead = NFS_DEFRAHEAD; 754 nmp->nm_readahead = NFS_DEFRAHEAD;
755 nmp->nm_deadthresh = NFS_DEFDEADTHRESH; 755 nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
756 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, 756 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
757 mp->mnt_op->vfs_name, mp, l); 757 mp->mnt_op->vfs_name, mp, l);
758 if (error) 758 if (error)
759 goto bad; 759 goto bad;
760 nmp->nm_nam = nam; 760 nmp->nm_nam = nam;
761 761
762 /* Set up the sockets and per-host congestion */ 762 /* Set up the sockets and per-host congestion */
763 nmp->nm_sotype = argp->sotype; 763 nmp->nm_sotype = argp->sotype;
764 nmp->nm_soproto = argp->proto; 764 nmp->nm_soproto = argp->proto;
765 765
766 nfs_decode_args(nmp, argp, l); 766 nfs_decode_args(nmp, argp, l);
767 767
768 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1; 768 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
769 mp->mnt_dev_bshift = DEV_BSHIFT; 769 mp->mnt_dev_bshift = DEV_BSHIFT;
770 770
771 /* 771 /*
772 * For Connection based sockets (TCP,...) defer the connect until 772 * For Connection based sockets (TCP,...) defer the connect until
773 * the first request, in case the server is not responding. 773 * the first request, in case the server is not responding.
774 */ 774 */
775 if (nmp->nm_sotype == SOCK_DGRAM && 775 if (nmp->nm_sotype == SOCK_DGRAM &&
776 (error = nfs_connect(nmp, (struct nfsreq *)0, l))) 776 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
777 goto bad; 777 goto bad;
778 778
779 /* 779 /*
780 * This is silly, but it has to be set so that vinifod() works. 780 * This is silly, but it has to be set so that vinifod() works.
781 * We do not want to do an nfs_statvfs() here since we can get 781 * We do not want to do an nfs_statvfs() here since we can get
782 * stuck on a dead server and we are holding a lock on the mount 782 * stuck on a dead server and we are holding a lock on the mount
783 * point. 783 * point.
784 */ 784 */
785 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 785 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
786 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np); 786 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
787 if (error) 787 if (error)
788 goto bad; 788 goto bad;
789 vp = NFSTOV(np); 789 vp = NFSTOV(np);
790 attrs = malloc(sizeof(struct vattr), M_TEMP, M_WAITOK); 790 attrs = malloc(sizeof(struct vattr), M_TEMP, M_WAITOK);
791 VOP_GETATTR(vp, attrs, l->l_cred); 791 VOP_GETATTR(vp, attrs, l->l_cred);
792 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) { 792 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
793 cr = kauth_cred_alloc(); 793 cr = kauth_cred_alloc();
794 kauth_cred_setuid(cr, attrs->va_uid); 794 kauth_cred_setuid(cr, attrs->va_uid);
795 kauth_cred_seteuid(cr, attrs->va_uid); 795 kauth_cred_seteuid(cr, attrs->va_uid);
796 kauth_cred_setsvuid(cr, attrs->va_uid); 796 kauth_cred_setsvuid(cr, attrs->va_uid);
797 kauth_cred_setgid(cr, attrs->va_gid); 797 kauth_cred_setgid(cr, attrs->va_gid);
798 kauth_cred_setegid(cr, attrs->va_gid); 798 kauth_cred_setegid(cr, attrs->va_gid);
799 kauth_cred_setsvgid(cr, attrs->va_gid); 799 kauth_cred_setsvgid(cr, attrs->va_gid);
800 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr); 800 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
801 kauth_cred_free(cr); 801 kauth_cred_free(cr);
802 } 802 }
803 free(attrs, M_TEMP); 803 free(attrs, M_TEMP);
804 804
805 /* 805 /*
806 * A reference count is needed on the nfsnode representing the 806 * A reference count is needed on the nfsnode representing the
807 * remote root. If this object is not persistent, then backward 807 * remote root. If this object is not persistent, then backward
808 * traversals of the mount point (i.e. "..") will not work if 808 * traversals of the mount point (i.e. "..") will not work if
809 * the nfsnode gets flushed out of the cache. Ufs does not have 809 * the nfsnode gets flushed out of the cache. Ufs does not have
810 * this problem, because one can identify root inodes by their 810 * this problem, because one can identify root inodes by their
811 * number == UFS_ROOTINO (2). So, just unlock, but no rele. 811 * number == UFS_ROOTINO (2). So, just unlock, but no rele.
812 */ 812 */
813 813
814 nmp->nm_vnode = vp; 814 nmp->nm_vnode = vp;
815 if (vp->v_type == VNON) 815 if (vp->v_type == VNON)
816 vp->v_type = VDIR; 816 vp->v_type = VDIR;
817 vp->v_vflag |= VV_ROOT; 817 vp->v_vflag |= VV_ROOT;
818 VOP_UNLOCK(vp); 818 VOP_UNLOCK(vp);
819 *vpp = vp; 819 *vpp = vp;
820 820
821 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++); 821 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
822 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname); 822 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
823 823
824 return (0); 824 return (0);
825bad: 825bad:
826 nfs_disconnect(nmp); 826 nfs_disconnect(nmp);
827 rw_destroy(&nmp->nm_writeverflock); 827 rw_destroy(&nmp->nm_writeverflock);
828 mutex_destroy(&nmp->nm_lock); 828 mutex_destroy(&nmp->nm_lock);
829 cv_destroy(&nmp->nm_rcvcv); 829 cv_destroy(&nmp->nm_rcvcv);
830 cv_destroy(&nmp->nm_sndcv); 830 cv_destroy(&nmp->nm_sndcv);
831 cv_destroy(&nmp->nm_aiocv); 831 cv_destroy(&nmp->nm_aiocv);
832 cv_destroy(&nmp->nm_disconcv); 832 cv_destroy(&nmp->nm_disconcv);
833 kmem_free(nmp, sizeof(*nmp)); 833 kmem_free(nmp, sizeof(*nmp));
834 m_freem(nam); 834 m_freem(nam);
835 return (error); 835 return (error);
836} 836}
837 837
838/* 838/*
839 * unmount system call 839 * unmount system call
840 */ 840 */
841int 841int
842nfs_unmount(struct mount *mp, int mntflags) 842nfs_unmount(struct mount *mp, int mntflags)
843{ 843{
844 struct nfsmount *nmp = VFSTONFS(mp); 844 struct nfsmount *nmp = VFSTONFS(mp);
845 struct vnode *vp; 845 struct vnode *vp;
846 int error, flags = 0; 846 int error, flags = 0;
847 847
848 if (mntflags & MNT_FORCE) { 848 if (mntflags & MNT_FORCE) {
849 mutex_enter(&nmp->nm_lock); 849 mutex_enter(&nmp->nm_lock);
850 flags |= FORCECLOSE; 850 flags |= FORCECLOSE;
851 nmp->nm_iflag |= NFSMNT_DISMNTFORCE; 851 nmp->nm_iflag |= NFSMNT_DISMNTFORCE;
852 mutex_exit(&nmp->nm_lock); 852 mutex_exit(&nmp->nm_lock);
853 853
854 } 854 }
855 855
856 /* 856 /*
857 * Goes something like this.. 857 * Goes something like this..
858 * - Check for activity on the root vnode (other than ourselves). 858 * - Check for activity on the root vnode (other than ourselves).
859 * - Call vflush() to clear out vnodes for this file system, 859 * - Call vflush() to clear out vnodes for this file system,
860 * except for the root vnode. 860 * except for the root vnode.
861 * - Decrement reference on the vnode representing remote root. 861 * - Decrement reference on the vnode representing remote root.
862 * - Close the socket 862 * - Close the socket
863 * - Free up the data structures 863 * - Free up the data structures
864 */ 864 */
865 /* 865 /*
866 * We need to decrement the ref. count on the nfsnode representing 866 * We need to decrement the ref. count on the nfsnode representing
867 * the remote root. See comment in mountnfs(). 867 * the remote root. See comment in mountnfs().
868 */ 868 */
869 vp = nmp->nm_vnode; 869 vp = nmp->nm_vnode;
870 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 870 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
871 if (error != 0) 871 if (error != 0)
872 goto err; 872 goto err;
873 873
874 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 1) { 874 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 1) {
875 VOP_UNLOCK(vp); 875 VOP_UNLOCK(vp);
876 error = EBUSY; 876 error = EBUSY;
877 goto err; 877 goto err;
878 } 878 }
879 879
880 error = vflush(mp, vp, flags); 880 error = vflush(mp, vp, flags);
881 if (error) { 881 if (error) {
882 VOP_UNLOCK(vp); 882 VOP_UNLOCK(vp);
883 goto err; 883 goto err;
884 } 884 }
885 885
886 /* 886 /*
887 * We are now committed to the unmount; mark the mount structure 887 * We are now committed to the unmount; mark the mount structure
888 * as doomed so that any sleepers kicked awake by nfs_disconnect 888 * as doomed so that any sleepers kicked awake by nfs_disconnect
889 * will go away cleanly. 889 * will go away cleanly.
890 */ 890 */
891 nmp->nm_iflag |= NFSMNT_DISMNT; 891 nmp->nm_iflag |= NFSMNT_DISMNT;
892 892
893 /* 893 /*
894 * No new async I/O will be added, but await for pending 894 * No new async I/O will be added, but await for pending
895 * ones to drain. 895 * ones to drain.
896 */ 896 */
897 while (nfs_iodbusy(nmp)) 897 while (nfs_iodbusy(nmp))
898 kpause("nfsumnt", false, hz, NULL); 898 kpause("nfsumnt", false, hz, NULL);
899 899
900 /* 900 /*
901 * Clean up the stats... note that we carefully avoid decrementing 901 * Clean up the stats... note that we carefully avoid decrementing
902 * nfs_mount_count here for good reason - we may not be unmounting 902 * nfs_mount_count here for good reason - we may not be unmounting
903 * the last thing mounted. 903 * the last thing mounted.
904 */ 904 */
905 iostat_free(nmp->nm_stats); 905 iostat_free(nmp->nm_stats);
906 906
907 /* 907 /*
908 * There is one reference count to get rid of here 908 * There is one reference count to get rid of here
909 * (see comment in mountnfs()). 909 * (see comment in mountnfs()).
910 */ 910 */
911 VOP_UNLOCK(vp); 911 VOP_UNLOCK(vp);
912 vgone(vp); 912 vgone(vp);
913 nfs_disconnect(nmp); 913 nfs_disconnect(nmp);
914 m_freem(nmp->nm_nam); 914 m_freem(nmp->nm_nam);
915 915
916 rw_destroy(&nmp->nm_writeverflock); 916 rw_destroy(&nmp->nm_writeverflock);
917 mutex_destroy(&nmp->nm_lock); 917 mutex_destroy(&nmp->nm_lock);
918 cv_destroy(&nmp->nm_rcvcv); 918 cv_destroy(&nmp->nm_rcvcv);
919 cv_destroy(&nmp->nm_sndcv); 919 cv_destroy(&nmp->nm_sndcv);
920 cv_destroy(&nmp->nm_aiocv); 920 cv_destroy(&nmp->nm_aiocv);
921 cv_destroy(&nmp->nm_disconcv); 921 cv_destroy(&nmp->nm_disconcv);
922 kmem_free(nmp, sizeof(*nmp)); 922 kmem_free(nmp, sizeof(*nmp));
923 return (0); 923 return (0);
924 924
925err: 925err:
926 if (mntflags & MNT_FORCE) { 926 if (mntflags & MNT_FORCE) {
927 mutex_enter(&nmp->nm_lock); 927 mutex_enter(&nmp->nm_lock);
928 nmp->nm_iflag &= ~NFSMNT_DISMNTFORCE;  928 nmp->nm_iflag &= ~NFSMNT_DISMNTFORCE;
929 mutex_exit(&nmp->nm_lock); 929 mutex_exit(&nmp->nm_lock);
930 } 930 }
931 931
932 return error; 932 return error;
933} 933}
934 934
935/* 935/*
936 * Return root of a filesystem 936 * Return root of a filesystem
937 */ 937 */
938int 938int
939nfs_root(struct mount *mp, struct vnode **vpp) 939nfs_root(struct mount *mp, struct vnode **vpp)
940{ 940{
941 struct vnode *vp; 941 struct vnode *vp;
942 struct nfsmount *nmp; 942 struct nfsmount *nmp;
943 int error; 943 int error;
944 944
945 nmp = VFSTONFS(mp); 945 nmp = VFSTONFS(mp);
946 vp = nmp->nm_vnode; 946 vp = nmp->nm_vnode;
947 vref(vp); 947 vref(vp);
948 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 948 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
949 if (error != 0) { 949 if (error != 0) {
950 vrele(vp); 950 vrele(vp);
951 return error; 951 return error;
952 } 952 }
953 *vpp = vp; 953 *vpp = vp;
954 return (0); 954 return (0);
955} 955}
956 956
957extern int syncprt; 957extern int syncprt;
958 958
959static bool 959static bool
960nfs_sync_selector(void *cl, struct vnode *vp) 960nfs_sync_selector(void *cl, struct vnode *vp)
961{ 961{
962 962
963 return !LIST_EMPTY(&vp->v_dirtyblkhd) || !UVM_OBJ_IS_CLEAN(&vp->v_uobj); 963 return !LIST_EMPTY(&vp->v_dirtyblkhd) || !UVM_OBJ_IS_CLEAN(&vp->v_uobj);
964} 964}
965 965
966/* 966/*
967 * Flush out the buffer cache 967 * Flush out the buffer cache
968 */ 968 */
969/* ARGSUSED */ 969/* ARGSUSED */
970int 970int
971nfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 971nfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
972{ 972{
973 struct vnode *vp; 973 struct vnode *vp;
974 struct vnode_iterator *marker; 974 struct vnode_iterator *marker;
975 int error, allerror = 0; 975 int error, allerror = 0;
976 976
977 /* 977 /*
978 * Force stale buffer cache information to be flushed. 978 * Force stale buffer cache information to be flushed.
979 */ 979 */
980 vfs_vnode_iterator_init(mp, &marker); 980 vfs_vnode_iterator_init(mp, &marker);
981 while ((vp = vfs_vnode_iterator_next(marker, nfs_sync_selector, 981 while ((vp = vfs_vnode_iterator_next(marker, nfs_sync_selector,
982 NULL))) 982 NULL)))
983 { 983 {
984 error = vn_lock(vp, LK_EXCLUSIVE); 984 error = vn_lock(vp, LK_EXCLUSIVE);
985 if (error) { 985 if (error) {
986 vrele(vp); 986 vrele(vp);
987 continue; 987 continue;
988 } 988 }
989 error = VOP_FSYNC(vp, cred, 989 error = VOP_FSYNC(vp, cred,
990 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0); 990 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
991 if (error) 991 if (error)
992 allerror = error; 992 allerror = error;
993 vput(vp); 993 vput(vp);
994 } 994 }
995 vfs_vnode_iterator_destroy(marker); 995 vfs_vnode_iterator_destroy(marker);
996 return allerror; 996 return allerror;
997} 997}
998 998
999/* 999/*
1000 * NFS flat namespace lookup. 1000 * NFS flat namespace lookup.
1001 * Currently unsupported. 1001 * Currently unsupported.
1002 */ 1002 */
1003/* ARGSUSED */ 1003/* ARGSUSED */
1004int 1004int
1005nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 1005nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1006{ 1006{
1007 1007
1008 return (EOPNOTSUPP); 1008 return (EOPNOTSUPP);
1009} 1009}
1010 1010
1011/* 1011/*
1012 * Do that sysctl thang... 1012 * Do that sysctl thang...
1013 */ 1013 */
1014static int 1014static int
1015sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS) 1015sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1016{ 1016{
1017 struct sysctlnode node; 1017 struct sysctlnode node;
1018 int val; 1018 int val;
1019 int error; 1019 int error;
1020 1020
1021 val = nfs_niothreads; 1021 val = nfs_niothreads;
1022 node = *rnode; 1022 node = *rnode;
1023 node.sysctl_data = &val; 1023 node.sysctl_data = &val;
1024 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1024 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1025 if (error || newp == NULL) 1025 if (error || newp == NULL)
1026 return error; 1026 return error;
1027 1027
1028 return nfs_set_niothreads(val); 1028 return nfs_set_niothreads(val);
1029} 1029}
1030 1030
1031static void 1031static void
1032nfs_sysctl_init(void) 1032nfs_sysctl_init(void)
1033{ 1033{
1034 1034
1035 sysctl_createv(&nfs_clog, 0, NULL, NULL, 1035 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1036 CTLFLAG_PERMANENT, 1036 CTLFLAG_PERMANENT,
1037 CTLTYPE_NODE, "nfs", 1037 CTLTYPE_NODE, "nfs",
1038 SYSCTL_DESCR("NFS vfs options"), 1038 SYSCTL_DESCR("NFS vfs options"),
1039 NULL, 0, NULL, 0, 1039 NULL, 0, NULL, 0,
1040 CTL_VFS, 2, CTL_EOL); 1040 CTL_VFS, 2, CTL_EOL);
1041 /* 1041 /*
1042 * XXX the "2" above could be dynamic, thereby eliminating one 1042 * XXX the "2" above could be dynamic, thereby eliminating one
1043 * more instance of the "number to vfs" mapping problem, but 1043 * more instance of the "number to vfs" mapping problem, but
1044 * "2" is the order as taken from sys/mount.h 1044 * "2" is the order as taken from sys/mount.h
1045 */ 1045 */
1046 1046
1047 sysctl_createv(&nfs_clog, 0, NULL, NULL, 1047 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1048 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1048 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1049 CTLTYPE_STRUCT, "nfsstats", 1049 CTLTYPE_STRUCT, "nfsstats",
1050 SYSCTL_DESCR("NFS operation statistics"), 1050 SYSCTL_DESCR("NFS operation statistics"),
1051 NULL, 0, &nfsstats, sizeof(nfsstats), 1051 NULL, 0, &nfsstats, sizeof(nfsstats),
1052 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL); 1052 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1053 sysctl_createv(&nfs_clog, 0, NULL, NULL, 1053 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1054 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1054 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1055 CTLTYPE_INT, "iothreads", 1055 CTLTYPE_INT, "iothreads",
1056 SYSCTL_DESCR("Number of NFS client processes desired"), 1056 SYSCTL_DESCR("Number of NFS client processes desired"),
1057 sysctl_vfs_nfs_iothreads, 0, NULL, 0, 1057 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1058 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL); 1058 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1059} 1059}
1060 1060
1061static void 1061static void
1062nfs_sysctl_fini(void) 1062nfs_sysctl_fini(void)
1063{ 1063{
1064 1064
1065 sysctl_teardown(&nfs_clog); 1065 sysctl_teardown(&nfs_clog);
1066} 1066}
1067 1067
1068/* ARGSUSED */ 1068/* ARGSUSED */
1069int 1069int
1070nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp) 1070nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1071{ 1071{
1072 size_t fidsize; 1072 size_t fidsize;
1073 size_t fhsize; 1073 size_t fhsize;
1074 struct nfsnode *np; 1074 struct nfsnode *np;
1075 int error; 1075 int error;
1076 struct vattr va; 1076 struct vattr va;
1077 1077
1078 fidsize = fid->fid_len; 1078 fidsize = fid->fid_len;
1079 if (fidsize < sizeof(*fid)) { 1079 if (fidsize < sizeof(*fid)) {
1080 return EINVAL; 1080 return EINVAL;
1081 } 1081 }
1082 fhsize = fidsize - sizeof(*fid); 1082 fhsize = fidsize - sizeof(*fid);
1083 if ((fhsize % NFSX_UNSIGNED) != 0) { 1083 if ((fhsize % NFSX_UNSIGNED) != 0) {
1084 return EINVAL; 1084 return EINVAL;
1085 } 1085 }
1086 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) { 1086 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1087 if (fhsize > NFSX_V3FHMAX || fhsize == 0) { 1087 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1088 return EINVAL; 1088 return EINVAL;
1089 } 1089 }
1090 } else { 1090 } else {
1091 if (fhsize != NFSX_V2FH) { 1091 if (fhsize != NFSX_V2FH) {
1092 return EINVAL; 1092 return EINVAL;
1093 } 1093 }
1094 } 1094 }
1095 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np); 1095 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1096 if (error) { 1096 if (error) {
1097 return error; 1097 return error;
1098 } 1098 }
1099 *vpp = NFSTOV(np); 1099 *vpp = NFSTOV(np);
1100 error = VOP_GETATTR(*vpp, &va, kauth_cred_get()); 1100 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1101 if (error != 0) { 1101 if (error != 0) {
1102 vput(*vpp); 1102 vput(*vpp);
1103 *vpp = NULLVP; 1103 *vpp = NULLVP;
1104 } 1104 }
1105 return error; 1105 return error;
1106} 1106}
1107 1107
1108/* ARGSUSED */ 1108/* ARGSUSED */
1109int 1109int
1110nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize) 1110nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1111{ 1111{
1112 struct nfsnode *np; 1112 struct nfsnode *np;
1113 struct fid *fid; 1113 struct fid *fid;
1114 size_t fidsize; 1114 size_t fidsize;
1115 int error = 0; 1115 int error = 0;
1116 1116
1117 np = VTONFS(vp); 1117 np = VTONFS(vp);
1118 fidsize = sizeof(*fid) + np->n_fhsize; 1118 fidsize = sizeof(*fid) + np->n_fhsize;
1119 if (*bufsize < fidsize) { 1119 if (*bufsize < fidsize) {
1120 error = E2BIG; 1120 error = E2BIG;
1121 } 1121 }
1122 *bufsize = fidsize; 1122 *bufsize = fidsize;
1123 if (error == 0) { 1123 if (error == 0) {
1124 struct fid fid_store; 1124 struct fid fid_store;
1125 1125
1126 fid = &fid_store; 1126 fid = &fid_store;
1127 memset(fid, 0, sizeof(*fid)); 1127 memset(fid, 0, sizeof(*fid));
1128 fid->fid_len = fidsize; 1128 fid->fid_len = fidsize;
1129 memcpy(buf, fid, sizeof(*fid)); 1129 memcpy(buf, fid, sizeof(*fid));
1130 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize); 1130 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1131 } 1131 }
1132 return error; 1132 return error;
1133} 1133}
1134 1134
1135/* 1135/*
1136 * Vfs start routine, a no-op. 1136 * Vfs start routine, a no-op.
1137 */ 1137 */
1138/* ARGSUSED */ 1138/* ARGSUSED */
1139int 1139int
1140nfs_start(struct mount *mp, int flags) 1140nfs_start(struct mount *mp, int flags)
1141{ 1141{
1142 1142
1143 return (0); 1143 return (0);
1144} 1144}
1145 1145
1146/* 1146/*
1147 * Called once at VFS init to initialize client-specific data structures. 1147 * Called once at VFS init to initialize client-specific data structures.
1148 */ 1148 */
1149void 1149void
1150nfs_vfs_init(void) 1150nfs_vfs_init(void)
1151{ 1151{
1152 1152
1153 /* Initialize NFS server / client shared data. */ 1153 /* Initialize NFS server / client shared data. */
1154 nfs_init(); 1154 nfs_init();
1155 nfs_node_init(); 1155 nfs_node_init();
1156 1156
1157 /* Initialize the kqueue structures */ 1157 /* Initialize the kqueue structures */
1158 nfs_kqinit(); 1158 nfs_kqinit();
1159 /* Initialize the iod structures */ 1159 /* Initialize the iod structures */
1160 nfs_iodinit(); 1160 nfs_iodinit();
1161 1161
1162 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4); 1162 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
1163} 1163}
1164 1164
1165void 1165void
1166nfs_vfs_done(void) 1166nfs_vfs_done(void)
1167{ 1167{
1168 1168
1169 nfs_node_done(); 1169 nfs_node_done();
1170 nfs_kqfini(); 1170 nfs_kqfini();
1171 nfs_iodfini(); 1171 nfs_iodfini();
 1172 nfs_fini();
1172} 1173}