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.2/sys/nfsclient/nfs_vfsops.c 163172 2006-10-09 19:47:17Z tegge $");
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 struct ifreq ir;
384 int error, i;
385 u_long l;
386 char buf[128];
387 char *cp;
388
389 NET_ASSERT_GIANT();
390
391 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
392 bootpc_init(); /* use bootp to get nfs_diskless filled in */
393 #elif defined(NFS_ROOT)
394 nfs_setup_diskless();
395 #endif
396
397 if (nfs_diskless_valid == 0)
398 return (-1);
399 if (nfs_diskless_valid == 1)
400 nfs_convert_diskless();
401
402 /*
403 * XXX splnet, so networks will receive...
404 */
405 splnet();
406
407 /*
408 * Do enough of ifconfig(8) so that the critical net interface can
409 * talk to the server.
410 */
411 error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0,
412 td->td_ucred, td);
413 if (error)
414 panic("nfs_mountroot: socreate(%04x): %d",
415 nd->myif.ifra_addr.sa_family, error);
416
417 #if 0 /* XXX Bad idea */
418 /*
419 * We might not have been told the right interface, so we pass
420 * over the first ten interfaces of the same kind, until we get
421 * one of them configured.
422 */
423
424 for (i = strlen(nd->myif.ifra_name) - 1;
425 nd->myif.ifra_name[i] >= '' &&
426 nd->myif.ifra_name[i] <= '9';
427 nd->myif.ifra_name[i] ++) {
428 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
429 if(!error)
430 break;
431 }
432 #endif
433 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
434 if (error)
435 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
436 if ((cp = getenv("boot.netif.mtu")) != NULL) {
437 ir.ifr_mtu = strtol(cp, NULL, 10);
438 bcopy(nd->myif.ifra_name, ir.ifr_name, IFNAMSIZ);
439 freeenv(cp);
440 error = ifioctl(so, SIOCSIFMTU, (caddr_t)&ir, td);
441 if (error)
442 printf("nfs_mountroot: SIOCSIFMTU: %d", error);
443 }
444 soclose(so);
445
446 /*
447 * If the gateway field is filled in, set it as the default route.
448 * Note that pxeboot will set a default route of 0 if the route
449 * is not set by the DHCP server. Check also for a value of 0
450 * to avoid panicking inappropriately in that situation.
451 */
452 if (nd->mygateway.sin_len != 0 &&
453 nd->mygateway.sin_addr.s_addr != 0) {
454 struct sockaddr_in mask, sin;
455
456 bzero((caddr_t)&mask, sizeof(mask));
457 sin = mask;
458 sin.sin_family = AF_INET;
459 sin.sin_len = sizeof(sin);
460 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
461 (struct sockaddr *)&nd->mygateway,
462 (struct sockaddr *)&mask,
463 RTF_UP | RTF_GATEWAY, NULL);
464 if (error)
465 panic("nfs_mountroot: RTM_ADD: %d", error);
466 }
467
468 /*
469 * Create the rootfs mount point.
470 */
471 nd->root_args.fh = nd->root_fh;
472 nd->root_args.fhsize = nd->root_fhsize;
473 l = ntohl(nd->root_saddr.sin_addr.s_addr);
474 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
475 (l >> 24) & 0xff, (l >> 16) & 0xff,
476 (l >> 8) & 0xff, (l >> 0) & 0xff, nd->root_hostnam);
477 printf("NFS ROOT: %s\n", buf);
478 if ((error = nfs_mountdiskless(buf, MNT_RDONLY,
479 &nd->root_saddr, &nd->root_args, td, &vp, mp)) != 0) {
480 return (error);
481 }
482
483 /*
484 * This is not really an nfs issue, but it is much easier to
485 * set hostname here and then let the "/etc/rc.xxx" files
486 * mount the right /var based upon its preset value.
487 */
488 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
489 hostname[MAXHOSTNAMELEN - 1] = '\0';
490 for (i = 0; i < MAXHOSTNAMELEN; i++)
491 if (hostname[i] == '\0')
492 break;
493 inittodr(ntohl(nd->root_time));
494 return (0);
495 }
496
497 /*
498 * Internal version of mount system call for diskless setup.
499 */
500 static int
501 nfs_mountdiskless(char *path, int mountflag,
502 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
503 struct vnode **vpp, struct mount *mp)
504 {
505 struct sockaddr *nam;
506 int error;
507
508 MNT_ILOCK(mp);
509 mp->mnt_kern_flag = 0;
510 mp->mnt_flag = mountflag;
511 MNT_IUNLOCK(mp);
512 nam = sodupsockaddr((struct sockaddr *)sin, M_WAITOK);
513 if ((error = mountnfs(args, mp, nam, path, vpp,
514 td->td_ucred)) != 0) {
515 printf("nfs_mountroot: mount %s on /: %d\n", path, error);
516 return (error);
517 }
518 return (0);
519 }
520
521 static void
522 nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp)
523 {
524 int s;
525 int adjsock;
526 int maxio;
527
528 s = splnet();
529
530 /*
531 * Set read-only flag if requested; otherwise, clear it if this is
532 * an update. If this is not an update, then either the read-only
533 * flag is already clear, or this is a root mount and it was set
534 * intentionally at some previous point.
535 */
536 if (vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL) == 0) {
537 MNT_ILOCK(mp);
538 mp->mnt_flag |= MNT_RDONLY;
539 MNT_IUNLOCK(mp);
540 } else if (mp->mnt_flag & MNT_UPDATE) {
541 MNT_ILOCK(mp);
542 mp->mnt_flag &= ~MNT_RDONLY;
543 MNT_IUNLOCK(mp);
544 }
545
546 /*
547 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
548 * no sense in that context. Also, set up appropriate retransmit
549 * and soft timeout behavior.
550 */
551 if (argp->sotype == SOCK_STREAM) {
552 nmp->nm_flag &= ~NFSMNT_NOCONN;
553 nmp->nm_flag |= NFSMNT_DUMBTIMR;
554 nmp->nm_timeo = NFS_MAXTIMEO;
555 nmp->nm_retry = NFS_RETRANS_TCP;
556 }
557
558 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
559 if ((argp->flags & NFSMNT_NFSV3) == 0)
560 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
561
562 /* Re-bind if rsrvd port requested and wasn't on one */
563 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
564 && (argp->flags & NFSMNT_RESVPORT);
565 /* Also re-bind if we're switching to/from a connected UDP socket */
566 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
567 (argp->flags & NFSMNT_NOCONN));
568
569 /* Update flags atomically. Don't change the lock bits. */
570 nmp->nm_flag = argp->flags | nmp->nm_flag;
571 splx(s);
572
573 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
574 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
575 if (nmp->nm_timeo < NFS_MINTIMEO)
576 nmp->nm_timeo = NFS_MINTIMEO;
577 else if (nmp->nm_timeo > NFS_MAXTIMEO)
578 nmp->nm_timeo = NFS_MAXTIMEO;
579 }
580
581 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
582 nmp->nm_retry = argp->retrans;
583 if (nmp->nm_retry > NFS_MAXREXMIT)
584 nmp->nm_retry = NFS_MAXREXMIT;
585 }
586
587 if (argp->flags & NFSMNT_NFSV3) {
588 if (argp->sotype == SOCK_DGRAM)
589 maxio = NFS_MAXDGRAMDATA;
590 else
591 maxio = NFS_MAXDATA;
592 } else
593 maxio = NFS_V2MAXDATA;
594
595 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
596 nmp->nm_wsize = argp->wsize;
597 /* Round down to multiple of blocksize */
598 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
599 if (nmp->nm_wsize <= 0)
600 nmp->nm_wsize = NFS_FABLKSIZE;
601 }
602 if (nmp->nm_wsize > maxio)
603 nmp->nm_wsize = maxio;
604 if (nmp->nm_wsize > MAXBSIZE)
605 nmp->nm_wsize = MAXBSIZE;
606
607 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
608 nmp->nm_rsize = argp->rsize;
609 /* Round down to multiple of blocksize */
610 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
611 if (nmp->nm_rsize <= 0)
612 nmp->nm_rsize = NFS_FABLKSIZE;
613 }
614 if (nmp->nm_rsize > maxio)
615 nmp->nm_rsize = maxio;
616 if (nmp->nm_rsize > MAXBSIZE)
617 nmp->nm_rsize = MAXBSIZE;
618
619 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
620 nmp->nm_readdirsize = argp->readdirsize;
621 }
622 if (nmp->nm_readdirsize > maxio)
623 nmp->nm_readdirsize = maxio;
624 if (nmp->nm_readdirsize > nmp->nm_rsize)
625 nmp->nm_readdirsize = nmp->nm_rsize;
626
627 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
628 nmp->nm_acregmin = argp->acregmin;
629 else
630 nmp->nm_acregmin = NFS_MINATTRTIMO;
631 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
632 nmp->nm_acregmax = argp->acregmax;
633 else
634 nmp->nm_acregmax = NFS_MAXATTRTIMO;
635 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
636 nmp->nm_acdirmin = argp->acdirmin;
637 else
638 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
639 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
640 nmp->nm_acdirmax = argp->acdirmax;
641 else
642 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
643 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
644 nmp->nm_acdirmin = nmp->nm_acdirmax;
645 if (nmp->nm_acregmin > nmp->nm_acregmax)
646 nmp->nm_acregmin = nmp->nm_acregmax;
647
648 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
649 if (argp->maxgrouplist <= NFS_MAXGRPS)
650 nmp->nm_numgrps = argp->maxgrouplist;
651 else
652 nmp->nm_numgrps = NFS_MAXGRPS;
653 }
654 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
655 if (argp->readahead <= NFS_MAXRAHEAD)
656 nmp->nm_readahead = argp->readahead;
657 else
658 nmp->nm_readahead = NFS_MAXRAHEAD;
659 }
660 if ((argp->flags & NFSMNT_WCOMMITSIZE) && argp->wcommitsize >= 0) {
661 if (argp->wcommitsize < nmp->nm_wsize)
662 nmp->nm_wcommitsize = nmp->nm_wsize;
663 else
664 nmp->nm_wcommitsize = argp->wcommitsize;
665 }
666 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
667 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
668 nmp->nm_deadthresh = argp->deadthresh;
669 else
670 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
671 }
672
673 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
674 (nmp->nm_soproto != argp->proto));
675 nmp->nm_sotype = argp->sotype;
676 nmp->nm_soproto = argp->proto;
677
678 if (nmp->nm_so && adjsock) {
679 nfs_safedisconnect(nmp);
680 if (nmp->nm_sotype == SOCK_DGRAM)
681 while (nfs_connect(nmp, NULL)) {
682 printf("nfs_args: retrying connect\n");
683 (void) tsleep((caddr_t)&lbolt,
684 PSOCK, "nfscon", 0);
685 }
686 }
687 }
688
689 static const char *nfs_opts[] = { "from", "nfs_args", NULL };
690
691 /*
692 * VFS Operations.
693 *
694 * mount system call
695 * It seems a bit dumb to copyinstr() the host and path here and then
696 * bcopy() them in mountnfs(), but I wanted to detect errors before
697 * doing the sockargs() call because sockargs() allocates an mbuf and
698 * an error after that means that I have to release the mbuf.
699 */
700 /* ARGSUSED */
701 static int
702 nfs_mount(struct mount *mp, struct thread *td)
703 {
704 int error;
705 struct nfs_args args;
706 struct sockaddr *nam;
707 struct vnode *vp;
708 char hst[MNAMELEN];
709 size_t len;
710 u_char nfh[NFSX_V3FHMAX];
711
712 if (vfs_filteropt(mp->mnt_optnew, nfs_opts))
713 return (EINVAL);
714
715 if (mp->mnt_flag & MNT_ROOTFS)
716 return (nfs_mountroot(mp, td));
717
718 error = vfs_copyopt(mp->mnt_optnew, "nfs_args", &args, sizeof args);
719 if (error)
720 return (error);
721
722 if (args.version != NFS_ARGSVERSION) {
723 return (EPROGMISMATCH);
724 }
725 if (mp->mnt_flag & MNT_UPDATE) {
726 struct nfsmount *nmp = VFSTONFS(mp);
727
728 if (nmp == NULL)
729 return (EIO);
730 /*
731 * When doing an update, we can't change from or to
732 * v3, switch lockd strategies or change cookie translation
733 */
734 args.flags = (args.flags &
735 ~(NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/)) |
736 (nmp->nm_flag &
737 (NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/));
738 nfs_decode_args(mp, nmp, &args);
739 return (0);
740 }
741
742 /*
743 * Make the nfs_ip_paranoia sysctl serve as the default connection
744 * or no-connection mode for those protocols that support
745 * no-connection mode (the flag will be cleared later for protocols
746 * that do not support no-connection mode). This will allow a client
747 * to receive replies from a different IP then the request was
748 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid),
749 * not 0.
750 */
751 if (nfs_ip_paranoia == 0)
752 args.flags |= NFSMNT_NOCONN;
753 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
754 return (EINVAL);
755 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
756 if (error)
757 return (error);
758 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
759 if (error)
760 return (error);
761 bzero(&hst[len], MNAMELEN - len);
762 /* sockargs() call must be after above copyin() calls */
763 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
764 if (error)
765 return (error);
766 args.fh = nfh;
767 error = mountnfs(&args, mp, nam, hst, &vp, td->td_ucred);
768 return (error);
769 }
770
771
772 /*
773 * VFS Operations.
774 *
775 * mount system call
776 * It seems a bit dumb to copyinstr() the host and path here and then
777 * bcopy() them in mountnfs(), but I wanted to detect errors before
778 * doing the sockargs() call because sockargs() allocates an mbuf and
779 * an error after that means that I have to release the mbuf.
780 */
781 /* ARGSUSED */
782 static int
783 nfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
784 {
785 int error;
786 struct nfs_args args;
787
788 error = copyin(data, &args, sizeof (struct nfs_args));
789 if (error)
790 return (error);
791
792 ma = mount_arg(ma, "nfs_args", &args, sizeof args);
793
794 error = kernel_mount(ma, flags);
795
796 return (error);
797 }
798
799 /*
800 * Common code for mount and mountroot
801 */
802 static int
803 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
804 char *hst, struct vnode **vpp, struct ucred *cred)
805 {
806 struct nfsmount *nmp;
807 struct nfsnode *np;
808 int error;
809 struct vattr attrs;
810
811 if (mp->mnt_flag & MNT_UPDATE) {
812 nmp = VFSTONFS(mp);
813 /* update paths, file handles, etc, here XXX */
814 FREE(nam, M_SONAME);
815 return (0);
816 } else {
817 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
818 bzero((caddr_t)nmp, sizeof (struct nfsmount));
819 TAILQ_INIT(&nmp->nm_bufq);
820 mp->mnt_data = (qaddr_t)nmp;
821 }
822 vfs_getnewfsid(mp);
823 nmp->nm_mountp = mp;
824
825 /*
826 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
827 * high, depending on whether we end up with negative offsets in
828 * the client or server somewhere. 2GB-1 may be safer.
829 *
830 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
831 * that we can handle until we find out otherwise.
832 * XXX Our "safe" limit on the client is what we can store in our
833 * buffer cache using signed(!) block numbers.
834 */
835 if ((argp->flags & NFSMNT_NFSV3) == 0)
836 nmp->nm_maxfilesize = 0xffffffffLL;
837 else
838 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
839
840 nmp->nm_timeo = NFS_TIMEO;
841 nmp->nm_retry = NFS_RETRANS;
842 if ((argp->flags & NFSMNT_NFSV3) && argp->sotype == SOCK_STREAM) {
843 nmp->nm_wsize = nmp->nm_rsize = NFS_MAXDATA;
844 } else {
845 nmp->nm_wsize = NFS_WSIZE;
846 nmp->nm_rsize = NFS_RSIZE;
847 }
848 nmp->nm_wcommitsize = hibufspace / (desiredvnodes / 1000);
849 nmp->nm_readdirsize = NFS_READDIRSIZE;
850 nmp->nm_numgrps = NFS_MAXGRPS;
851 nmp->nm_readahead = NFS_DEFRAHEAD;
852 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
853 nmp->nm_tprintf_delay = nfs_tprintf_delay;
854 if (nmp->nm_tprintf_delay < 0)
855 nmp->nm_tprintf_delay = 0;
856 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
857 if (nmp->nm_tprintf_initial_delay < 0)
858 nmp->nm_tprintf_initial_delay = 0;
859 nmp->nm_fhsize = argp->fhsize;
860 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
861 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
862 nmp->nm_nam = nam;
863 /* Set up the sockets and per-host congestion */
864 nmp->nm_sotype = argp->sotype;
865 nmp->nm_soproto = argp->proto;
866 nmp->nm_rpcops = &nfs_rpcops;
867
868 nfs_decode_args(mp, nmp, argp);
869
870 if (nmp->nm_sotype == SOCK_STREAM)
871 mtx_init(&nmp->nm_nfstcpstate.mtx, "NFS/TCP state lock",
872 NULL, MTX_DEF);
873
874 /*
875 * For Connection based sockets (TCP,...) defer the connect until
876 * the first request, in case the server is not responding.
877 */
878 if (nmp->nm_sotype == SOCK_DGRAM &&
879 (error = nfs_connect(nmp, NULL)))
880 goto bad;
881
882 /*
883 * This is silly, but it has to be set so that vinifod() works.
884 * We do not want to do an nfs_statfs() here since we can get
885 * stuck on a dead server and we are holding a lock on the mount
886 * point.
887 */
888 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
889 /*
890 * A reference count is needed on the nfsnode representing the
891 * remote root. If this object is not persistent, then backward
892 * traversals of the mount point (i.e. "..") will not work if
893 * the nfsnode gets flushed out of the cache. Ufs does not have
894 * this problem, because one can identify root inodes by their
895 * number == ROOTINO (2).
896 */
897 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
898 if (error)
899 goto bad;
900 *vpp = NFSTOV(np);
901
902 /*
903 * Get file attributes and transfer parameters for the
904 * mountpoint. This has the side effect of filling in
905 * (*vpp)->v_type with the correct value.
906 */
907 if (argp->flags & NFSMNT_NFSV3)
908 nfs_fsinfo(nmp, *vpp, curthread->td_ucred, curthread);
909 else
910 VOP_GETATTR(*vpp, &attrs, curthread->td_ucred, curthread);
911
912 /*
913 * Lose the lock but keep the ref.
914 */
915 VOP_UNLOCK(*vpp, 0, curthread);
916
917 return (0);
918 bad:
919 if (nmp->nm_sotype == SOCK_STREAM)
920 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
921 nfs_disconnect(nmp);
922 uma_zfree(nfsmount_zone, nmp);
923 FREE(nam, M_SONAME);
924 return (error);
925 }
926
927 /*
928 * unmount system call
929 */
930 static int
931 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
932 {
933 struct nfsmount *nmp;
934 int error, flags = 0;
935
936 if (mntflags & MNT_FORCE)
937 flags |= FORCECLOSE;
938 nmp = VFSTONFS(mp);
939 /*
940 * Goes something like this..
941 * - Call vflush() to clear out vnodes for this filesystem
942 * - Close the socket
943 * - Free up the data structures
944 */
945 /* In the forced case, cancel any outstanding requests. */
946 if (flags & FORCECLOSE) {
947 error = nfs_nmcancelreqs(nmp);
948 if (error)
949 return (error);
950 }
951 /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
952 error = vflush(mp, 1, flags, td);
953 if (error)
954 return (error);
955
956 /*
957 * We are now committed to the unmount.
958 */
959 nfs_disconnect(nmp);
960 FREE(nmp->nm_nam, M_SONAME);
961
962 if (nmp->nm_sotype == SOCK_STREAM)
963 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
964
965 uma_zfree(nfsmount_zone, nmp);
966 return (0);
967 }
968
969 /*
970 * Return root of a filesystem
971 */
972 static int
973 nfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
974 {
975 struct vnode *vp;
976 struct nfsmount *nmp;
977 struct nfsnode *np;
978 int error;
979
980 nmp = VFSTONFS(mp);
981 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
982 if (error)
983 return (error);
984 vp = NFSTOV(np);
985 /*
986 * Get transfer parameters and attributes for root vnode once.
987 */
988 if ((nmp->nm_state & NFSSTA_GOTFSINFO) == 0 &&
989 (nmp->nm_flag & NFSMNT_NFSV3)) {
990 nfs_fsinfo(nmp, vp, curthread->td_ucred, curthread);
991 }
992 if (vp->v_type == VNON)
993 vp->v_type = VDIR;
994 vp->v_vflag |= VV_ROOT;
995 *vpp = vp;
996 return (0);
997 }
998
999 /*
1000 * Flush out the buffer cache
1001 */
1002 /* ARGSUSED */
1003 static int
1004 nfs_sync(struct mount *mp, int waitfor, struct thread *td)
1005 {
1006 struct vnode *vp, *mvp;
1007 int error, allerror = 0;
1008
1009 /*
1010 * Force stale buffer cache information to be flushed.
1011 */
1012 MNT_ILOCK(mp);
1013 loop:
1014 MNT_VNODE_FOREACH(vp, mp, mvp) {
1015 VI_LOCK(vp);
1016 MNT_IUNLOCK(mp);
1017 if (VOP_ISLOCKED(vp, NULL) ||
1018 vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1019 waitfor == MNT_LAZY) {
1020 VI_UNLOCK(vp);
1021 MNT_ILOCK(mp);
1022 continue;
1023 }
1024 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
1025 MNT_ILOCK(mp);
1026 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1027 goto loop;
1028 }
1029 error = VOP_FSYNC(vp, waitfor, td);
1030 if (error)
1031 allerror = error;
1032 VOP_UNLOCK(vp, 0, td);
1033 vrele(vp);
1034
1035 MNT_ILOCK(mp);
1036 }
1037 MNT_IUNLOCK(mp);
1038 return (allerror);
1039 }
1040
1041 static int
1042 nfs_sysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
1043 {
1044 struct nfsmount *nmp = VFSTONFS(mp);
1045 struct vfsquery vq;
1046 int error;
1047
1048 bzero(&vq, sizeof(vq));
1049 switch (op) {
1050 #if 0
1051 case VFS_CTL_NOLOCKS:
1052 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1053 if (req->oldptr != NULL) {
1054 error = SYSCTL_OUT(req, &val, sizeof(val));
1055 if (error)
1056 return (error);
1057 }
1058 if (req->newptr != NULL) {
1059 error = SYSCTL_IN(req, &val, sizeof(val));
1060 if (error)
1061 return (error);
1062 if (val)
1063 nmp->nm_flag |= NFSMNT_NOLOCKS;
1064 else
1065 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1066 }
1067 break;
1068 #endif
1069 case VFS_CTL_QUERY:
1070 if (nmp->nm_state & NFSSTA_TIMEO)
1071 vq.vq_flags |= VQ_NOTRESP;
1072 #if 0
1073 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1074 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1075 vq.vq_flags |= VQ_NOTRESPLOCK;
1076 #endif
1077 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1078 break;
1079 case VFS_CTL_TIMEO:
1080 if (req->oldptr != NULL) {
1081 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1082 sizeof(nmp->nm_tprintf_initial_delay));
1083 if (error)
1084 return (error);
1085 }
1086 if (req->newptr != NULL) {
1087 error = vfs_suser(mp, req->td);
1088 if (error)
1089 return (error);
1090 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1091 sizeof(nmp->nm_tprintf_initial_delay));
1092 if (error)
1093 return (error);
1094 if (nmp->nm_tprintf_initial_delay < 0)
1095 nmp->nm_tprintf_initial_delay = 0;
1096 }
1097 break;
1098 default:
1099 return (ENOTSUP);
1100 }
1101 return (0);
1102 }
Cache object: 54b1dd1a6d27062753d76f57c732a9ac
|