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