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