1 /*-
2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/6.0/sys/nfsclient/nfs_vfsops.c 147280 2005-06-10 23:50:41Z green $");
37
38 #include "opt_bootp.h"
39 #include "opt_nfsroot.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/bio.h>
45 #include <sys/buf.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/module.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61 #include <vm/uma.h>
62
63 #include <net/if.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66
67 #include <rpc/rpcclnt.h>
68
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfsclient/nfsm_subs.h>
76 #include <nfsclient/nfsdiskless.h>
77
78 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
79 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
80 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
81 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
82 MALLOC_DEFINE(M_NFSDIRECTIO, "NFS DirectIO", "NFS Direct IO async write state");
83
84 uma_zone_t nfsmount_zone;
85
86 struct nfsstats nfsstats;
87 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
88 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
89 &nfsstats, nfsstats, "S,nfsstats");
90 static int nfs_ip_paranoia = 1;
91 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW,
92 &nfs_ip_paranoia, 0, "");
93 #ifdef NFS_DEBUG
94 int nfs_debug;
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
96 #endif
97 static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
98 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_INITIAL_DELAY,
99 downdelayinitial, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
100 /* how long between console messages "nfs server foo not responding" */
101 static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
102 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_DELAY,
103 downdelayinterval, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
104
105 static int nfs_iosize(struct nfsmount *nmp);
106 static void nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp);
107 static int mountnfs(struct nfs_args *, struct mount *,
108 struct sockaddr *, char *, struct vnode **,
109 struct ucred *cred);
110 static vfs_mount_t nfs_mount;
111 static vfs_cmount_t nfs_cmount;
112 static vfs_unmount_t nfs_unmount;
113 static vfs_root_t nfs_root;
114 static vfs_statfs_t nfs_statfs;
115 static vfs_sync_t nfs_sync;
116 static vfs_sysctl_t nfs_sysctl;
117
118 /*
119 * nfs vfs operations.
120 */
121 static struct vfsops nfs_vfsops = {
122 .vfs_init = nfs_init,
123 .vfs_mount = nfs_mount,
124 .vfs_cmount = nfs_cmount,
125 .vfs_root = nfs_root,
126 .vfs_statfs = nfs_statfs,
127 .vfs_sync = nfs_sync,
128 .vfs_uninit = nfs_uninit,
129 .vfs_unmount = nfs_unmount,
130 .vfs_sysctl = nfs_sysctl,
131 };
132 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
133
134 /* So that loader and kldload(2) can find us, wherever we are.. */
135 MODULE_VERSION(nfs, 1);
136
137 static struct nfs_rpcops nfs_rpcops = {
138 nfs_readrpc,
139 nfs_writerpc,
140 nfs_writebp,
141 nfs_readlinkrpc,
142 nfs_invaldir,
143 nfs_commit,
144 };
145
146 /*
147 * This structure must be filled in by a primary bootstrap or bootstrap
148 * server for a diskless/dataless machine. It is initialized below just
149 * to ensure that it is allocated to initialized data (.data not .bss).
150 */
151 struct nfs_diskless nfs_diskless = { { { 0 } } };
152 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
153 int nfs_diskless_valid = 0;
154
155 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
156 &nfs_diskless_valid, 0, "");
157
158 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
159 nfsv3_diskless.root_hostnam, 0, "");
160
161 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
162 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
163 "%Ssockaddr_in", "");
164
165
166 void nfsargs_ntoh(struct nfs_args *);
167 static int nfs_mountdiskless(char *, int,
168 struct sockaddr_in *, struct nfs_args *,
169 struct thread *, struct vnode **, struct mount *);
170 static void nfs_convert_diskless(void);
171 static void nfs_convert_oargs(struct nfs_args *args,
172 struct onfs_args *oargs);
173
174 static int
175 nfs_iosize(struct nfsmount *nmp)
176 {
177 int iosize;
178
179 /*
180 * Calculate the size used for io buffers. Use the larger
181 * of the two sizes to minimise nfs requests but make sure
182 * that it is at least one VM page to avoid wasting buffer
183 * space.
184 */
185 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
186 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
187 return iosize;
188 }
189
190 static void
191 nfs_convert_oargs(struct nfs_args *args, struct onfs_args *oargs)
192 {
193
194 args->version = NFS_ARGSVERSION;
195 args->addr = oargs->addr;
196 args->addrlen = oargs->addrlen;
197 args->sotype = oargs->sotype;
198 args->proto = oargs->proto;
199 args->fh = oargs->fh;
200 args->fhsize = oargs->fhsize;
201 args->flags = oargs->flags;
202 args->wsize = oargs->wsize;
203 args->rsize = oargs->rsize;
204 args->readdirsize = oargs->readdirsize;
205 args->timeo = oargs->timeo;
206 args->retrans = oargs->retrans;
207 args->maxgrouplist = oargs->maxgrouplist;
208 args->readahead = oargs->readahead;
209 args->deadthresh = oargs->deadthresh;
210 args->hostname = oargs->hostname;
211 }
212
213 static void
214 nfs_convert_diskless(void)
215 {
216
217 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
218 sizeof(struct ifaliasreq));
219 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
220 sizeof(struct sockaddr_in));
221 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
222 nfsv3_diskless.root_fhsize = NFSX_V2FH;
223 bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V2FH);
224 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
225 sizeof(struct sockaddr_in));
226 bcopy(nfs_diskless.root_hostnam, nfsv3_diskless.root_hostnam, MNAMELEN);
227 nfsv3_diskless.root_time = nfs_diskless.root_time;
228 bcopy(nfs_diskless.my_hostnam, nfsv3_diskless.my_hostnam,
229 MAXHOSTNAMELEN);
230 nfs_diskless_valid = 3;
231 }
232
233 /*
234 * nfs statfs call
235 */
236 static int
237 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
238 {
239 struct vnode *vp;
240 struct nfs_statfs *sfp;
241 caddr_t bpos, dpos;
242 struct nfsmount *nmp = VFSTONFS(mp);
243 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
244 struct mbuf *mreq, *mrep, *md, *mb;
245 struct nfsnode *np;
246 u_quad_t tquad;
247
248 #ifndef nolint
249 sfp = NULL;
250 #endif
251 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
252 if (error)
253 return (error);
254 vp = NFSTOV(np);
255 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
256 (void)nfs_fsinfo(nmp, vp, td->td_ucred, td);
257 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
258 mreq = nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
259 mb = mreq;
260 bpos = mtod(mb, caddr_t);
261 nfsm_fhtom(vp, v3);
262 nfsm_request(vp, NFSPROC_FSSTAT, td, td->td_ucred);
263 if (v3)
264 nfsm_postop_attr(vp, retattr);
265 if (error) {
266 if (mrep != NULL)
267 m_freem(mrep);
268 goto nfsmout;
269 }
270 sfp = nfsm_dissect(struct nfs_statfs *, NFSX_STATFS(v3));
271 sbp->f_iosize = nfs_iosize(nmp);
272 if (v3) {
273 sbp->f_bsize = NFS_FABLKSIZE;
274 tquad = fxdr_hyper(&sfp->sf_tbytes);
275 sbp->f_blocks = tquad / NFS_FABLKSIZE;
276 tquad = fxdr_hyper(&sfp->sf_fbytes);
277 sbp->f_bfree = tquad / NFS_FABLKSIZE;
278 tquad = fxdr_hyper(&sfp->sf_abytes);
279 sbp->f_bavail = tquad / NFS_FABLKSIZE;
280 sbp->f_files = (fxdr_unsigned(int32_t,
281 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
282 sbp->f_ffree = (fxdr_unsigned(int32_t,
283 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
284 } else {
285 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
286 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
287 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
288 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
289 sbp->f_files = 0;
290 sbp->f_ffree = 0;
291 }
292 m_freem(mrep);
293 nfsmout:
294 vput(vp);
295 return (error);
296 }
297
298 /*
299 * nfs version 3 fsinfo rpc call
300 */
301 int
302 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
303 struct thread *td)
304 {
305 struct nfsv3_fsinfo *fsp;
306 u_int32_t pref, max;
307 caddr_t bpos, dpos;
308 int error = 0, retattr;
309 struct mbuf *mreq, *mrep, *md, *mb;
310 u_int64_t maxfsize;
311
312 nfsstats.rpccnt[NFSPROC_FSINFO]++;
313 mreq = nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
314 mb = mreq;
315 bpos = mtod(mb, caddr_t);
316 nfsm_fhtom(vp, 1);
317 nfsm_request(vp, NFSPROC_FSINFO, td, cred);
318 nfsm_postop_attr(vp, retattr);
319 if (!error) {
320 fsp = nfsm_dissect(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
321 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
322 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
323 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
324 ~(NFS_FABLKSIZE - 1);
325 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
326 if (max < nmp->nm_wsize && max > 0) {
327 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
328 if (nmp->nm_wsize == 0)
329 nmp->nm_wsize = max;
330 }
331 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
332 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
333 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
334 ~(NFS_FABLKSIZE - 1);
335 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
336 if (max < nmp->nm_rsize && max > 0) {
337 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
338 if (nmp->nm_rsize == 0)
339 nmp->nm_rsize = max;
340 }
341 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
342 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
343 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
344 ~(NFS_DIRBLKSIZ - 1);
345 if (max < nmp->nm_readdirsize && max > 0) {
346 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
347 if (nmp->nm_readdirsize == 0)
348 nmp->nm_readdirsize = max;
349 }
350 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
351 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
352 nmp->nm_maxfilesize = maxfsize;
353 nmp->nm_mountp->mnt_stat.f_iosize = nfs_iosize(nmp);
354 nmp->nm_state |= NFSSTA_GOTFSINFO;
355 }
356 m_freem(mrep);
357 nfsmout:
358 return (error);
359 }
360
361 /*
362 * Mount a remote root fs via. nfs. This depends on the info in the
363 * nfs_diskless structure that has been filled in properly by some primary
364 * bootstrap.
365 * It goes something like this:
366 * - do enough of "ifconfig" by calling ifioctl() so that the system
367 * can talk to the server
368 * - If nfs_diskless.mygateway is filled in, use that address as
369 * a default gateway.
370 * - build the rootfs mount point and call mountnfs() to do the rest.
371 *
372 * It is assumed to be safe to read, modify, and write the nfsv3_diskless
373 * structure, as well as other global NFS client variables here, as
374 * nfs_mountroot() will be called once in the boot before any other NFS
375 * client activity occurs.
376 */
377 int
378 nfs_mountroot(struct mount *mp, struct thread *td)
379 {
380 struct nfsv3_diskless *nd = &nfsv3_diskless;
381 struct socket *so;
382 struct vnode *vp;
383 int error, i;
384 u_long l;
385 char buf[128];
386
387 NET_ASSERT_GIANT();
388
389 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
390 bootpc_init(); /* use bootp to get nfs_diskless filled in */
391 #elif defined(NFS_ROOT)
392 nfs_setup_diskless();
393 #endif
394
395 if (nfs_diskless_valid == 0)
396 return (-1);
397 if (nfs_diskless_valid == 1)
398 nfs_convert_diskless();
399
400 /*
401 * XXX splnet, so networks will receive...
402 */
403 splnet();
404
405 /*
406 * Do enough of ifconfig(8) so that the critical net interface can
407 * talk to the server.
408 */
409 error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0,
410 td->td_ucred, td);
411 if (error)
412 panic("nfs_mountroot: socreate(%04x): %d",
413 nd->myif.ifra_addr.sa_family, error);
414
415 #if 0 /* XXX Bad idea */
416 /*
417 * We might not have been told the right interface, so we pass
418 * over the first ten interfaces of the same kind, until we get
419 * one of them configured.
420 */
421
422 for (i = strlen(nd->myif.ifra_name) - 1;
423 nd->myif.ifra_name[i] >= '' &&
424 nd->myif.ifra_name[i] <= '9';
425 nd->myif.ifra_name[i] ++) {
426 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
427 if(!error)
428 break;
429 }
430 #endif
431 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
432 if (error)
433 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
434 soclose(so);
435
436 /*
437 * If the gateway field is filled in, set it as the default route.
438 * Note that pxeboot will set a default route of 0 if the route
439 * is not set by the DHCP server. Check also for a value of 0
440 * to avoid panicking inappropriately in that situation.
441 */
442 if (nd->mygateway.sin_len != 0 &&
443 nd->mygateway.sin_addr.s_addr != 0) {
444 struct sockaddr_in mask, sin;
445
446 bzero((caddr_t)&mask, sizeof(mask));
447 sin = mask;
448 sin.sin_family = AF_INET;
449 sin.sin_len = sizeof(sin);
450 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
451 (struct sockaddr *)&nd->mygateway,
452 (struct sockaddr *)&mask,
453 RTF_UP | RTF_GATEWAY, NULL);
454 if (error)
455 panic("nfs_mountroot: RTM_ADD: %d", error);
456 }
457
458 /*
459 * Create the rootfs mount point.
460 */
461 nd->root_args.fh = nd->root_fh;
462 nd->root_args.fhsize = nd->root_fhsize;
463 l = ntohl(nd->root_saddr.sin_addr.s_addr);
464 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
465 (l >> 24) & 0xff, (l >> 16) & 0xff,
466 (l >> 8) & 0xff, (l >> 0) & 0xff, nd->root_hostnam);
467 printf("NFS ROOT: %s\n", buf);
468 if ((error = nfs_mountdiskless(buf, MNT_RDONLY,
469 &nd->root_saddr, &nd->root_args, td, &vp, mp)) != 0) {
470 return (error);
471 }
472
473 /*
474 * This is not really an nfs issue, but it is much easier to
475 * set hostname here and then let the "/etc/rc.xxx" files
476 * mount the right /var based upon its preset value.
477 */
478 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
479 hostname[MAXHOSTNAMELEN - 1] = '\0';
480 for (i = 0; i < MAXHOSTNAMELEN; i++)
481 if (hostname[i] == '\0')
482 break;
483 inittodr(ntohl(nd->root_time));
484 return (0);
485 }
486
487 /*
488 * Internal version of mount system call for diskless setup.
489 */
490 static int
491 nfs_mountdiskless(char *path, int mountflag,
492 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
493 struct vnode **vpp, struct mount *mp)
494 {
495 struct sockaddr *nam;
496 int error;
497
498 mp->mnt_kern_flag = 0;
499 mp->mnt_flag = mountflag;
500 nam = sodupsockaddr((struct sockaddr *)sin, M_WAITOK);
501 if ((error = mountnfs(args, mp, nam, path, vpp,
502 td->td_ucred)) != 0) {
503 printf("nfs_mountroot: mount %s on /: %d\n", path, error);
504 return (error);
505 }
506 return (0);
507 }
508
509 static void
510 nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp)
511 {
512 int s;
513 int adjsock;
514 int maxio;
515
516 s = splnet();
517
518 /*
519 * Set read-only flag if requested; otherwise, clear it if this is
520 * an update. If this is not an update, then either the read-only
521 * flag is already clear, or this is a root mount and it was set
522 * intentionally at some previous point.
523 */
524 if (vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL) == 0)
525 mp->mnt_flag |= MNT_RDONLY;
526 else if (mp->mnt_flag & MNT_UPDATE)
527 mp->mnt_flag &= ~MNT_RDONLY;
528
529 /*
530 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
531 * no sense in that context.
532 */
533 if (argp->sotype == SOCK_STREAM)
534 nmp->nm_flag &= ~NFSMNT_NOCONN;
535
536 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
537 if ((argp->flags & NFSMNT_NFSV3) == 0)
538 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
539
540 /* Re-bind if rsrvd port requested and wasn't on one */
541 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
542 && (argp->flags & NFSMNT_RESVPORT);
543 /* Also re-bind if we're switching to/from a connected UDP socket */
544 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
545 (argp->flags & NFSMNT_NOCONN));
546
547 /* Update flags atomically. Don't change the lock bits. */
548 nmp->nm_flag = argp->flags | nmp->nm_flag;
549 splx(s);
550
551 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
552 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
553 if (nmp->nm_timeo < NFS_MINTIMEO)
554 nmp->nm_timeo = NFS_MINTIMEO;
555 else if (nmp->nm_timeo > NFS_MAXTIMEO)
556 nmp->nm_timeo = NFS_MAXTIMEO;
557 }
558
559 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
560 nmp->nm_retry = argp->retrans;
561 if (nmp->nm_retry > NFS_MAXREXMIT)
562 nmp->nm_retry = NFS_MAXREXMIT;
563 }
564
565 if (argp->flags & NFSMNT_NFSV3) {
566 if (argp->sotype == SOCK_DGRAM)
567 maxio = NFS_MAXDGRAMDATA;
568 else
569 maxio = NFS_MAXDATA;
570 } else
571 maxio = NFS_V2MAXDATA;
572
573 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
574 nmp->nm_wsize = argp->wsize;
575 /* Round down to multiple of blocksize */
576 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
577 if (nmp->nm_wsize <= 0)
578 nmp->nm_wsize = NFS_FABLKSIZE;
579 }
580 if (nmp->nm_wsize > maxio)
581 nmp->nm_wsize = maxio;
582 if (nmp->nm_wsize > MAXBSIZE)
583 nmp->nm_wsize = MAXBSIZE;
584
585 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
586 nmp->nm_rsize = argp->rsize;
587 /* Round down to multiple of blocksize */
588 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
589 if (nmp->nm_rsize <= 0)
590 nmp->nm_rsize = NFS_FABLKSIZE;
591 }
592 if (nmp->nm_rsize > maxio)
593 nmp->nm_rsize = maxio;
594 if (nmp->nm_rsize > MAXBSIZE)
595 nmp->nm_rsize = MAXBSIZE;
596
597 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
598 nmp->nm_readdirsize = argp->readdirsize;
599 }
600 if (nmp->nm_readdirsize > maxio)
601 nmp->nm_readdirsize = maxio;
602 if (nmp->nm_readdirsize > nmp->nm_rsize)
603 nmp->nm_readdirsize = nmp->nm_rsize;
604
605 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
606 nmp->nm_acregmin = argp->acregmin;
607 else
608 nmp->nm_acregmin = NFS_MINATTRTIMO;
609 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
610 nmp->nm_acregmax = argp->acregmax;
611 else
612 nmp->nm_acregmax = NFS_MAXATTRTIMO;
613 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
614 nmp->nm_acdirmin = argp->acdirmin;
615 else
616 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
617 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
618 nmp->nm_acdirmax = argp->acdirmax;
619 else
620 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
621 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
622 nmp->nm_acdirmin = nmp->nm_acdirmax;
623 if (nmp->nm_acregmin > nmp->nm_acregmax)
624 nmp->nm_acregmin = nmp->nm_acregmax;
625
626 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
627 if (argp->maxgrouplist <= NFS_MAXGRPS)
628 nmp->nm_numgrps = argp->maxgrouplist;
629 else
630 nmp->nm_numgrps = NFS_MAXGRPS;
631 }
632 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
633 if (argp->readahead <= NFS_MAXRAHEAD)
634 nmp->nm_readahead = argp->readahead;
635 else
636 nmp->nm_readahead = NFS_MAXRAHEAD;
637 }
638 if ((argp->flags & NFSMNT_WCOMMITSIZE) && argp->wcommitsize >= 0) {
639 if (argp->wcommitsize < nmp->nm_wsize)
640 nmp->nm_wcommitsize = nmp->nm_wsize;
641 else
642 nmp->nm_wcommitsize = argp->wcommitsize;
643 }
644 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
645 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
646 nmp->nm_deadthresh = argp->deadthresh;
647 else
648 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
649 }
650
651 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
652 (nmp->nm_soproto != argp->proto));
653 nmp->nm_sotype = argp->sotype;
654 nmp->nm_soproto = argp->proto;
655
656 if (nmp->nm_so && adjsock) {
657 nfs_safedisconnect(nmp);
658 if (nmp->nm_sotype == SOCK_DGRAM)
659 while (nfs_connect(nmp, NULL)) {
660 printf("nfs_args: retrying connect\n");
661 (void) tsleep((caddr_t)&lbolt,
662 PSOCK, "nfscon", 0);
663 }
664 }
665 }
666
667 static const char *nfs_opts[] = { "from", "nfs_args", NULL };
668
669 /*
670 * VFS Operations.
671 *
672 * mount system call
673 * It seems a bit dumb to copyinstr() the host and path here and then
674 * bcopy() them in mountnfs(), but I wanted to detect errors before
675 * doing the sockargs() call because sockargs() allocates an mbuf and
676 * an error after that means that I have to release the mbuf.
677 */
678 /* ARGSUSED */
679 static int
680 nfs_mount(struct mount *mp, struct thread *td)
681 {
682 int error;
683 struct nfs_args args;
684 struct sockaddr *nam;
685 struct vnode *vp;
686 char hst[MNAMELEN];
687 size_t len;
688 u_char nfh[NFSX_V3FHMAX];
689
690 if (vfs_filteropt(mp->mnt_optnew, nfs_opts))
691 return (EINVAL);
692
693 if (mp->mnt_flag & MNT_ROOTFS)
694 return (nfs_mountroot(mp, td));
695
696 error = vfs_copyopt(mp->mnt_optnew, "nfs_args", &args, sizeof args);
697 if (error)
698 return (error);
699
700 if (args.version != NFS_ARGSVERSION) {
701 return (EPROGMISMATCH);
702 }
703 if (mp->mnt_flag & MNT_UPDATE) {
704 struct nfsmount *nmp = VFSTONFS(mp);
705
706 if (nmp == NULL)
707 return (EIO);
708 /*
709 * When doing an update, we can't change from or to
710 * v3, switch lockd strategies or change cookie translation
711 */
712 args.flags = (args.flags &
713 ~(NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/)) |
714 (nmp->nm_flag &
715 (NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/));
716 nfs_decode_args(mp, nmp, &args);
717 return (0);
718 }
719
720 /*
721 * Make the nfs_ip_paranoia sysctl serve as the default connection
722 * or no-connection mode for those protocols that support
723 * no-connection mode (the flag will be cleared later for protocols
724 * that do not support no-connection mode). This will allow a client
725 * to receive replies from a different IP then the request was
726 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid),
727 * not 0.
728 */
729 if (nfs_ip_paranoia == 0)
730 args.flags |= NFSMNT_NOCONN;
731 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
732 return (EINVAL);
733 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
734 if (error)
735 return (error);
736 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
737 if (error)
738 return (error);
739 bzero(&hst[len], MNAMELEN - len);
740 /* sockargs() call must be after above copyin() calls */
741 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
742 if (error)
743 return (error);
744 args.fh = nfh;
745 error = mountnfs(&args, mp, nam, hst, &vp, td->td_ucred);
746 return (error);
747 }
748
749
750 /*
751 * VFS Operations.
752 *
753 * mount system call
754 * It seems a bit dumb to copyinstr() the host and path here and then
755 * bcopy() them in mountnfs(), but I wanted to detect errors before
756 * doing the sockargs() call because sockargs() allocates an mbuf and
757 * an error after that means that I have to release the mbuf.
758 */
759 /* ARGSUSED */
760 static int
761 nfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
762 {
763 int error;
764 struct nfs_args args;
765
766 error = copyin(data, &args, sizeof (struct nfs_args));
767 if (error)
768 return (error);
769
770 ma = mount_arg(ma, "nfs_args", &args, sizeof args);
771
772 error = kernel_mount(ma, flags);
773
774 return (error);
775 }
776
777 /*
778 * Common code for mount and mountroot
779 */
780 static int
781 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
782 char *hst, struct vnode **vpp, struct ucred *cred)
783 {
784 struct nfsmount *nmp;
785 struct nfsnode *np;
786 int error;
787 struct vattr attrs;
788
789 if (mp->mnt_flag & MNT_UPDATE) {
790 nmp = VFSTONFS(mp);
791 /* update paths, file handles, etc, here XXX */
792 FREE(nam, M_SONAME);
793 return (0);
794 } else {
795 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
796 bzero((caddr_t)nmp, sizeof (struct nfsmount));
797 TAILQ_INIT(&nmp->nm_bufq);
798 mp->mnt_data = (qaddr_t)nmp;
799 }
800 vfs_getnewfsid(mp);
801 nmp->nm_mountp = mp;
802
803 /*
804 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
805 * high, depending on whether we end up with negative offsets in
806 * the client or server somewhere. 2GB-1 may be safer.
807 *
808 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
809 * that we can handle until we find out otherwise.
810 * XXX Our "safe" limit on the client is what we can store in our
811 * buffer cache using signed(!) block numbers.
812 */
813 if ((argp->flags & NFSMNT_NFSV3) == 0)
814 nmp->nm_maxfilesize = 0xffffffffLL;
815 else
816 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
817
818 nmp->nm_timeo = NFS_TIMEO;
819 nmp->nm_retry = NFS_RETRANS;
820 if ((argp->flags & NFSMNT_NFSV3) && argp->sotype == SOCK_STREAM) {
821 nmp->nm_wsize = nmp->nm_rsize = NFS_MAXDATA;
822 } else {
823 nmp->nm_wsize = NFS_WSIZE;
824 nmp->nm_rsize = NFS_RSIZE;
825 }
826 nmp->nm_wcommitsize = hibufspace / (desiredvnodes / 1000);
827 nmp->nm_readdirsize = NFS_READDIRSIZE;
828 nmp->nm_numgrps = NFS_MAXGRPS;
829 nmp->nm_readahead = NFS_DEFRAHEAD;
830 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
831 nmp->nm_tprintf_delay = nfs_tprintf_delay;
832 if (nmp->nm_tprintf_delay < 0)
833 nmp->nm_tprintf_delay = 0;
834 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
835 if (nmp->nm_tprintf_initial_delay < 0)
836 nmp->nm_tprintf_initial_delay = 0;
837 nmp->nm_fhsize = argp->fhsize;
838 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
839 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
840 nmp->nm_nam = nam;
841 /* Set up the sockets and per-host congestion */
842 nmp->nm_sotype = argp->sotype;
843 nmp->nm_soproto = argp->proto;
844 nmp->nm_rpcops = &nfs_rpcops;
845
846 nfs_decode_args(mp, nmp, argp);
847
848 if (nmp->nm_sotype == SOCK_STREAM)
849 mtx_init(&nmp->nm_nfstcpstate.mtx, "NFS/TCP state lock",
850 NULL, MTX_DEF);
851
852 /*
853 * For Connection based sockets (TCP,...) defer the connect until
854 * the first request, in case the server is not responding.
855 */
856 if (nmp->nm_sotype == SOCK_DGRAM &&
857 (error = nfs_connect(nmp, NULL)))
858 goto bad;
859
860 /*
861 * This is silly, but it has to be set so that vinifod() works.
862 * We do not want to do an nfs_statfs() here since we can get
863 * stuck on a dead server and we are holding a lock on the mount
864 * point.
865 */
866 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
867 /*
868 * A reference count is needed on the nfsnode representing the
869 * remote root. If this object is not persistent, then backward
870 * traversals of the mount point (i.e. "..") will not work if
871 * the nfsnode gets flushed out of the cache. Ufs does not have
872 * this problem, because one can identify root inodes by their
873 * number == ROOTINO (2).
874 */
875 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
876 if (error)
877 goto bad;
878 *vpp = NFSTOV(np);
879
880 /*
881 * Get file attributes and transfer parameters for the
882 * mountpoint. This has the side effect of filling in
883 * (*vpp)->v_type with the correct value.
884 */
885 if (argp->flags & NFSMNT_NFSV3)
886 nfs_fsinfo(nmp, *vpp, curthread->td_ucred, curthread);
887 else
888 VOP_GETATTR(*vpp, &attrs, curthread->td_ucred, curthread);
889
890 /*
891 * Lose the lock but keep the ref.
892 */
893 VOP_UNLOCK(*vpp, 0, curthread);
894
895 return (0);
896 bad:
897 if (nmp->nm_sotype == SOCK_STREAM)
898 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
899 nfs_disconnect(nmp);
900 uma_zfree(nfsmount_zone, nmp);
901 FREE(nam, M_SONAME);
902 return (error);
903 }
904
905 /*
906 * unmount system call
907 */
908 static int
909 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
910 {
911 struct nfsmount *nmp;
912 int error, flags = 0;
913
914 if (mntflags & MNT_FORCE)
915 flags |= FORCECLOSE;
916 nmp = VFSTONFS(mp);
917 /*
918 * Goes something like this..
919 * - Call vflush() to clear out vnodes for this filesystem
920 * - Close the socket
921 * - Free up the data structures
922 */
923 /* In the forced case, cancel any outstanding requests. */
924 if (flags & FORCECLOSE) {
925 error = nfs_nmcancelreqs(nmp);
926 if (error)
927 return (error);
928 }
929 /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
930 error = vflush(mp, 1, flags, td);
931 if (error)
932 return (error);
933
934 /*
935 * We are now committed to the unmount.
936 */
937 nfs_disconnect(nmp);
938 FREE(nmp->nm_nam, M_SONAME);
939
940 if (nmp->nm_sotype == SOCK_STREAM)
941 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
942
943 uma_zfree(nfsmount_zone, nmp);
944 return (0);
945 }
946
947 /*
948 * Return root of a filesystem
949 */
950 static int
951 nfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
952 {
953 struct vnode *vp;
954 struct nfsmount *nmp;
955 struct nfsnode *np;
956 int error;
957
958 nmp = VFSTONFS(mp);
959 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
960 if (error)
961 return (error);
962 vp = NFSTOV(np);
963 /*
964 * Get transfer parameters and attributes for root vnode once.
965 */
966 if ((nmp->nm_state & NFSSTA_GOTFSINFO) == 0 &&
967 (nmp->nm_flag & NFSMNT_NFSV3)) {
968 nfs_fsinfo(nmp, vp, curthread->td_ucred, curthread);
969 }
970 if (vp->v_type == VNON)
971 vp->v_type = VDIR;
972 vp->v_vflag |= VV_ROOT;
973 *vpp = vp;
974 return (0);
975 }
976
977 /*
978 * Flush out the buffer cache
979 */
980 /* ARGSUSED */
981 static int
982 nfs_sync(struct mount *mp, int waitfor, struct thread *td)
983 {
984 struct vnode *vp, *nvp;
985 int error, allerror = 0;
986
987 /*
988 * Force stale buffer cache information to be flushed.
989 */
990 MNT_ILOCK(mp);
991 loop:
992 MNT_VNODE_FOREACH(vp, mp, nvp) {
993 VI_LOCK(vp);
994 MNT_IUNLOCK(mp);
995 if (VOP_ISLOCKED(vp, NULL) ||
996 vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
997 waitfor == MNT_LAZY) {
998 VI_UNLOCK(vp);
999 MNT_ILOCK(mp);
1000 continue;
1001 }
1002 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
1003 MNT_ILOCK(mp);
1004 goto loop;
1005 }
1006 error = VOP_FSYNC(vp, waitfor, td);
1007 if (error)
1008 allerror = error;
1009 VOP_UNLOCK(vp, 0, td);
1010 vrele(vp);
1011
1012 MNT_ILOCK(mp);
1013 }
1014 MNT_IUNLOCK(mp);
1015 return (allerror);
1016 }
1017
1018 static int
1019 nfs_sysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
1020 {
1021 struct nfsmount *nmp = VFSTONFS(mp);
1022 struct vfsquery vq;
1023 int error;
1024
1025 bzero(&vq, sizeof(vq));
1026 switch (op) {
1027 #if 0
1028 case VFS_CTL_NOLOCKS:
1029 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1030 if (req->oldptr != NULL) {
1031 error = SYSCTL_OUT(req, &val, sizeof(val));
1032 if (error)
1033 return (error);
1034 }
1035 if (req->newptr != NULL) {
1036 error = SYSCTL_IN(req, &val, sizeof(val));
1037 if (error)
1038 return (error);
1039 if (val)
1040 nmp->nm_flag |= NFSMNT_NOLOCKS;
1041 else
1042 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1043 }
1044 break;
1045 #endif
1046 case VFS_CTL_QUERY:
1047 if (nmp->nm_state & NFSSTA_TIMEO)
1048 vq.vq_flags |= VQ_NOTRESP;
1049 #if 0
1050 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1051 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1052 vq.vq_flags |= VQ_NOTRESPLOCK;
1053 #endif
1054 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1055 break;
1056 case VFS_CTL_TIMEO:
1057 if (req->oldptr != NULL) {
1058 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1059 sizeof(nmp->nm_tprintf_initial_delay));
1060 if (error)
1061 return (error);
1062 }
1063 if (req->newptr != NULL) {
1064 error = vfs_suser(mp, req->td);
1065 if (error)
1066 return (error);
1067 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1068 sizeof(nmp->nm_tprintf_initial_delay));
1069 if (error)
1070 return (error);
1071 if (nmp->nm_tprintf_initial_delay < 0)
1072 nmp->nm_tprintf_initial_delay = 0;
1073 }
1074 break;
1075 default:
1076 return (ENOTSUP);
1077 }
1078 return (0);
1079 }
Cache object: d3fbe592427ace3eb8fa00b241d83c00
|