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/5.3/sys/nfsclient/nfs_vfsops.c 132902 2004-07-30 22:08:52Z phk $");
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/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/module.h>
49 #include <sys/mount.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
59 #include <vm/uma.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64
65 #include <rpc/rpcclnt.h>
66
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfsclient/nfsnode.h>
71 #include <nfsclient/nfsmount.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfsclient/nfsm_subs.h>
74 #include <nfsclient/nfsdiskless.h>
75
76 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
77 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
78 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
79 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
80
81 uma_zone_t nfsmount_zone;
82
83 struct nfsstats nfsstats;
84 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
85 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
86 &nfsstats, nfsstats, "S,nfsstats");
87 static int nfs_ip_paranoia = 1;
88 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW,
89 &nfs_ip_paranoia, 0, "");
90 #ifdef NFS_DEBUG
91 int nfs_debug;
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
93 #endif
94 static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
95 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_INITIAL_DELAY,
96 downdelayinitial, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
97 /* how long between console messages "nfs server foo not responding" */
98 static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
99 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_DELAY,
100 downdelayinterval, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
101
102 static int nfs_iosize(struct nfsmount *nmp);
103 static void nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp);
104 static int mountnfs(struct nfs_args *, struct mount *,
105 struct sockaddr *, char *, char *, struct vnode **,
106 struct ucred *cred);
107 static vfs_omount_t nfs_omount;
108 static vfs_unmount_t nfs_unmount;
109 static vfs_root_t nfs_root;
110 static vfs_statfs_t nfs_statfs;
111 static vfs_sync_t nfs_sync;
112 static vfs_sysctl_t nfs_sysctl;
113
114 /*
115 * nfs vfs operations.
116 */
117 static struct vfsops nfs_vfsops = {
118 .vfs_init = nfs_init,
119 .vfs_omount = nfs_omount,
120 .vfs_root = nfs_root,
121 .vfs_statfs = nfs_statfs,
122 .vfs_sync = nfs_sync,
123 .vfs_uninit = nfs_uninit,
124 .vfs_unmount = nfs_unmount,
125 .vfs_sysctl = nfs_sysctl,
126 };
127 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
128
129 /* So that loader and kldload(2) can find us, wherever we are.. */
130 MODULE_VERSION(nfs, 1);
131
132 static struct nfs_rpcops nfs_rpcops = {
133 nfs_readrpc,
134 nfs_writerpc,
135 nfs_writebp,
136 nfs_readlinkrpc,
137 nfs_invaldir,
138 nfs_commit,
139 };
140
141 /*
142 * This structure must be filled in by a primary bootstrap or bootstrap
143 * server for a diskless/dataless machine. It is initialized below just
144 * to ensure that it is allocated to initialized data (.data not .bss).
145 */
146 struct nfs_diskless nfs_diskless = { { { 0 } } };
147 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
148 int nfs_diskless_valid = 0;
149
150 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
151 &nfs_diskless_valid, 0, "");
152
153 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
154 nfsv3_diskless.root_hostnam, 0, "");
155
156 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
157 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
158 "%Ssockaddr_in", "");
159
160
161 void nfsargs_ntoh(struct nfs_args *);
162 static int nfs_mountdiskless(char *, char *, int,
163 struct sockaddr_in *, struct nfs_args *,
164 struct thread *, struct vnode **, struct mount *);
165 static void nfs_convert_diskless(void);
166 static void nfs_convert_oargs(struct nfs_args *args,
167 struct onfs_args *oargs);
168
169 static int
170 nfs_iosize(struct nfsmount *nmp)
171 {
172 int iosize;
173
174 /*
175 * Calculate the size used for io buffers. Use the larger
176 * of the two sizes to minimise nfs requests but make sure
177 * that it is at least one VM page to avoid wasting buffer
178 * space.
179 */
180 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
181 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
182 return iosize;
183 }
184
185 static void
186 nfs_convert_oargs(struct nfs_args *args, struct onfs_args *oargs)
187 {
188
189 args->version = NFS_ARGSVERSION;
190 args->addr = oargs->addr;
191 args->addrlen = oargs->addrlen;
192 args->sotype = oargs->sotype;
193 args->proto = oargs->proto;
194 args->fh = oargs->fh;
195 args->fhsize = oargs->fhsize;
196 args->flags = oargs->flags;
197 args->wsize = oargs->wsize;
198 args->rsize = oargs->rsize;
199 args->readdirsize = oargs->readdirsize;
200 args->timeo = oargs->timeo;
201 args->retrans = oargs->retrans;
202 args->maxgrouplist = oargs->maxgrouplist;
203 args->readahead = oargs->readahead;
204 args->deadthresh = oargs->deadthresh;
205 args->hostname = oargs->hostname;
206 }
207
208 static void
209 nfs_convert_diskless(void)
210 {
211
212 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
213 sizeof(struct ifaliasreq));
214 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
215 sizeof(struct sockaddr_in));
216 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
217 nfsv3_diskless.root_fhsize = NFSX_V2FH;
218 bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V2FH);
219 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
220 sizeof(struct sockaddr_in));
221 bcopy(nfs_diskless.root_hostnam, nfsv3_diskless.root_hostnam, MNAMELEN);
222 nfsv3_diskless.root_time = nfs_diskless.root_time;
223 bcopy(nfs_diskless.my_hostnam, nfsv3_diskless.my_hostnam,
224 MAXHOSTNAMELEN);
225 nfs_diskless_valid = 3;
226 }
227
228 /*
229 * nfs statfs call
230 */
231 static int
232 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
233 {
234 struct vnode *vp;
235 struct nfs_statfs *sfp;
236 caddr_t bpos, dpos;
237 struct nfsmount *nmp = VFSTONFS(mp);
238 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
239 struct mbuf *mreq, *mrep, *md, *mb;
240 struct nfsnode *np;
241 u_quad_t tquad;
242 int bsize;
243
244 #ifndef nolint
245 sfp = NULL;
246 #endif
247 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
248 if (error)
249 return (error);
250 vp = NFSTOV(np);
251 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
252 (void)nfs_fsinfo(nmp, vp, td->td_ucred, td);
253 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
254 mreq = nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
255 mb = mreq;
256 bpos = mtod(mb, caddr_t);
257 nfsm_fhtom(vp, v3);
258 nfsm_request(vp, NFSPROC_FSSTAT, td, td->td_ucred);
259 if (v3)
260 nfsm_postop_attr(vp, retattr);
261 if (error) {
262 if (mrep != NULL)
263 m_freem(mrep);
264 goto nfsmout;
265 }
266 sfp = nfsm_dissect(struct nfs_statfs *, NFSX_STATFS(v3));
267 sbp->f_flags = nmp->nm_flag;
268 sbp->f_iosize = nfs_iosize(nmp);
269 if (v3) {
270 for (bsize = NFS_FABLKSIZE; ; bsize *= 2) {
271 sbp->f_bsize = bsize;
272 tquad = fxdr_hyper(&sfp->sf_tbytes);
273 if (((long)(tquad / bsize) > LONG_MAX) ||
274 ((long)(tquad / bsize) < LONG_MIN))
275 continue;
276 sbp->f_blocks = tquad / bsize;
277 tquad = fxdr_hyper(&sfp->sf_fbytes);
278 if (((long)(tquad / bsize) > LONG_MAX) ||
279 ((long)(tquad / bsize) < LONG_MIN))
280 continue;
281 sbp->f_bfree = tquad / bsize;
282 tquad = fxdr_hyper(&sfp->sf_abytes);
283 if (((long)(tquad / bsize) > LONG_MAX) ||
284 ((long)(tquad / bsize) < LONG_MIN))
285 continue;
286 sbp->f_bavail = tquad / bsize;
287 sbp->f_files = (fxdr_unsigned(int32_t,
288 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
289 sbp->f_ffree = (fxdr_unsigned(int32_t,
290 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
291 break;
292 }
293 } else {
294 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
295 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
296 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
297 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
298 sbp->f_files = 0;
299 sbp->f_ffree = 0;
300 }
301 if (sbp != &mp->mnt_stat) {
302 sbp->f_type = mp->mnt_vfc->vfc_typenum;
303 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
304 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
305 }
306 m_freem(mrep);
307 nfsmout:
308 vput(vp);
309 return (error);
310 }
311
312 /*
313 * nfs version 3 fsinfo rpc call
314 */
315 int
316 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
317 struct thread *td)
318 {
319 struct nfsv3_fsinfo *fsp;
320 u_int32_t pref, max;
321 caddr_t bpos, dpos;
322 int error = 0, retattr;
323 struct mbuf *mreq, *mrep, *md, *mb;
324 u_int64_t maxfsize;
325
326 nfsstats.rpccnt[NFSPROC_FSINFO]++;
327 mreq = nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
328 mb = mreq;
329 bpos = mtod(mb, caddr_t);
330 nfsm_fhtom(vp, 1);
331 nfsm_request(vp, NFSPROC_FSINFO, td, cred);
332 nfsm_postop_attr(vp, retattr);
333 if (!error) {
334 fsp = nfsm_dissect(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
335 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
336 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
337 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
338 ~(NFS_FABLKSIZE - 1);
339 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
340 if (max < nmp->nm_wsize && max > 0) {
341 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
342 if (nmp->nm_wsize == 0)
343 nmp->nm_wsize = max;
344 }
345 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
346 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
347 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
348 ~(NFS_FABLKSIZE - 1);
349 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
350 if (max < nmp->nm_rsize && max > 0) {
351 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
352 if (nmp->nm_rsize == 0)
353 nmp->nm_rsize = max;
354 }
355 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
356 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
357 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
358 ~(NFS_DIRBLKSIZ - 1);
359 if (max < nmp->nm_readdirsize && max > 0) {
360 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
361 if (nmp->nm_readdirsize == 0)
362 nmp->nm_readdirsize = max;
363 }
364 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
365 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
366 nmp->nm_maxfilesize = maxfsize;
367 nmp->nm_mountp->mnt_stat.f_iosize = nfs_iosize(nmp);
368 nmp->nm_state |= NFSSTA_GOTFSINFO;
369 }
370 m_freem(mrep);
371 nfsmout:
372 return (error);
373 }
374
375 /*
376 * Mount a remote root fs via. nfs. This depends on the info in the
377 * nfs_diskless structure that has been filled in properly by some primary
378 * bootstrap.
379 * It goes something like this:
380 * - do enough of "ifconfig" by calling ifioctl() so that the system
381 * can talk to the server
382 * - If nfs_diskless.mygateway is filled in, use that address as
383 * a default gateway.
384 * - build the rootfs mount point and call mountnfs() to do the rest.
385 */
386 int
387 nfs_mountroot(struct mount *mp, struct thread *td)
388 {
389 struct nfsv3_diskless *nd = &nfsv3_diskless;
390 struct socket *so;
391 struct vnode *vp;
392 int error, i;
393 u_long l;
394 char buf[128];
395
396 GIANT_REQUIRED; /* XXX until socket locking done */
397
398 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
399 bootpc_init(); /* use bootp to get nfs_diskless filled in */
400 #elif defined(NFS_ROOT)
401 nfs_setup_diskless();
402 #endif
403
404 if (nfs_diskless_valid == 0)
405 return (-1);
406 if (nfs_diskless_valid == 1)
407 nfs_convert_diskless();
408
409 /*
410 * XXX splnet, so networks will receive...
411 */
412 splnet();
413
414 /*
415 * Do enough of ifconfig(8) so that the critical net interface can
416 * talk to the server.
417 */
418 error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0,
419 td->td_ucred, td);
420 if (error)
421 panic("nfs_mountroot: socreate(%04x): %d",
422 nd->myif.ifra_addr.sa_family, error);
423
424 #if 0 /* XXX Bad idea */
425 /*
426 * We might not have been told the right interface, so we pass
427 * over the first ten interfaces of the same kind, until we get
428 * one of them configured.
429 */
430
431 for (i = strlen(nd->myif.ifra_name) - 1;
432 nd->myif.ifra_name[i] >= '' &&
433 nd->myif.ifra_name[i] <= '9';
434 nd->myif.ifra_name[i] ++) {
435 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
436 if(!error)
437 break;
438 }
439 #endif
440 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
441 if (error)
442 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
443 soclose(so);
444
445 /*
446 * If the gateway field is filled in, set it as the default route.
447 * Note that pxeboot will set a default route of 0 if the route
448 * is not set by the DHCP server. Check also for a value of 0
449 * to avoid panicking inappropriately in that situation.
450 */
451 if (nd->mygateway.sin_len != 0 &&
452 nd->mygateway.sin_addr.s_addr != 0) {
453 struct sockaddr_in mask, sin;
454
455 bzero((caddr_t)&mask, sizeof(mask));
456 sin = mask;
457 sin.sin_family = AF_INET;
458 sin.sin_len = sizeof(sin);
459 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
460 (struct sockaddr *)&nd->mygateway,
461 (struct sockaddr *)&mask,
462 RTF_UP | RTF_GATEWAY, NULL);
463 if (error)
464 panic("nfs_mountroot: RTM_ADD: %d", error);
465 }
466
467 /*
468 * Create the rootfs mount point.
469 */
470 nd->root_args.fh = nd->root_fh;
471 nd->root_args.fhsize = nd->root_fhsize;
472 l = ntohl(nd->root_saddr.sin_addr.s_addr);
473 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
474 (l >> 24) & 0xff, (l >> 16) & 0xff,
475 (l >> 8) & 0xff, (l >> 0) & 0xff, nd->root_hostnam);
476 printf("NFS ROOT: %s\n", buf);
477 if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
478 &nd->root_saddr, &nd->root_args, td, &vp, mp)) != 0) {
479 return (error);
480 }
481
482 /*
483 * This is not really an nfs issue, but it is much easier to
484 * set hostname here and then let the "/etc/rc.xxx" files
485 * mount the right /var based upon its preset value.
486 */
487 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
488 hostname[MAXHOSTNAMELEN - 1] = '\0';
489 for (i = 0; i < MAXHOSTNAMELEN; i++)
490 if (hostname[i] == '\0')
491 break;
492 inittodr(ntohl(nd->root_time));
493 return (0);
494 }
495
496 /*
497 * Internal version of mount system call for diskless setup.
498 */
499 static int
500 nfs_mountdiskless(char *path, char *which, int mountflag,
501 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
502 struct vnode **vpp, struct mount *mp)
503 {
504 struct sockaddr *nam;
505 int error;
506
507 mp->mnt_kern_flag = 0;
508 mp->mnt_flag = mountflag;
509 nam = sodupsockaddr((struct sockaddr *)sin, M_WAITOK);
510 if ((error = mountnfs(args, mp, nam, which, path, vpp,
511 td->td_ucred)) != 0) {
512 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
513 return (error);
514 }
515 (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
516 return (0);
517 }
518
519 static void
520 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp)
521 {
522 int s;
523 int adjsock;
524 int maxio;
525
526 s = splnet();
527 /*
528 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
529 * no sense in that context.
530 */
531 if (argp->sotype == SOCK_STREAM)
532 nmp->nm_flag &= ~NFSMNT_NOCONN;
533
534 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
535 if ((argp->flags & NFSMNT_NFSV3) == 0)
536 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
537
538 /* Re-bind if rsrvd port requested and wasn't on one */
539 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
540 && (argp->flags & NFSMNT_RESVPORT);
541 /* Also re-bind if we're switching to/from a connected UDP socket */
542 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
543 (argp->flags & NFSMNT_NOCONN));
544
545 /* Update flags atomically. Don't change the lock bits. */
546 nmp->nm_flag = argp->flags | nmp->nm_flag;
547 splx(s);
548
549 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
550 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
551 if (nmp->nm_timeo < NFS_MINTIMEO)
552 nmp->nm_timeo = NFS_MINTIMEO;
553 else if (nmp->nm_timeo > NFS_MAXTIMEO)
554 nmp->nm_timeo = NFS_MAXTIMEO;
555 }
556
557 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
558 nmp->nm_retry = argp->retrans;
559 if (nmp->nm_retry > NFS_MAXREXMIT)
560 nmp->nm_retry = NFS_MAXREXMIT;
561 }
562
563 if (argp->flags & NFSMNT_NFSV3) {
564 if (argp->sotype == SOCK_DGRAM)
565 maxio = NFS_MAXDGRAMDATA;
566 else
567 maxio = NFS_MAXDATA;
568 } else
569 maxio = NFS_V2MAXDATA;
570
571 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
572 nmp->nm_wsize = argp->wsize;
573 /* Round down to multiple of blocksize */
574 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
575 if (nmp->nm_wsize <= 0)
576 nmp->nm_wsize = NFS_FABLKSIZE;
577 }
578 if (nmp->nm_wsize > maxio)
579 nmp->nm_wsize = maxio;
580 if (nmp->nm_wsize > MAXBSIZE)
581 nmp->nm_wsize = MAXBSIZE;
582
583 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
584 nmp->nm_rsize = argp->rsize;
585 /* Round down to multiple of blocksize */
586 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
587 if (nmp->nm_rsize <= 0)
588 nmp->nm_rsize = NFS_FABLKSIZE;
589 }
590 if (nmp->nm_rsize > maxio)
591 nmp->nm_rsize = maxio;
592 if (nmp->nm_rsize > MAXBSIZE)
593 nmp->nm_rsize = MAXBSIZE;
594
595 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
596 nmp->nm_readdirsize = argp->readdirsize;
597 }
598 if (nmp->nm_readdirsize > maxio)
599 nmp->nm_readdirsize = maxio;
600 if (nmp->nm_readdirsize > nmp->nm_rsize)
601 nmp->nm_readdirsize = nmp->nm_rsize;
602
603 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
604 nmp->nm_acregmin = argp->acregmin;
605 else
606 nmp->nm_acregmin = NFS_MINATTRTIMO;
607 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
608 nmp->nm_acregmax = argp->acregmax;
609 else
610 nmp->nm_acregmax = NFS_MAXATTRTIMO;
611 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
612 nmp->nm_acdirmin = argp->acdirmin;
613 else
614 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
615 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
616 nmp->nm_acdirmax = argp->acdirmax;
617 else
618 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
619 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
620 nmp->nm_acdirmin = nmp->nm_acdirmax;
621 if (nmp->nm_acregmin > nmp->nm_acregmax)
622 nmp->nm_acregmin = nmp->nm_acregmax;
623
624 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
625 if (argp->maxgrouplist <= NFS_MAXGRPS)
626 nmp->nm_numgrps = argp->maxgrouplist;
627 else
628 nmp->nm_numgrps = NFS_MAXGRPS;
629 }
630 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
631 if (argp->readahead <= NFS_MAXRAHEAD)
632 nmp->nm_readahead = argp->readahead;
633 else
634 nmp->nm_readahead = NFS_MAXRAHEAD;
635 }
636 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
637 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
638 nmp->nm_deadthresh = argp->deadthresh;
639 else
640 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
641 }
642
643 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
644 (nmp->nm_soproto != argp->proto));
645 nmp->nm_sotype = argp->sotype;
646 nmp->nm_soproto = argp->proto;
647
648 if (nmp->nm_so && adjsock) {
649 nfs_safedisconnect(nmp);
650 if (nmp->nm_sotype == SOCK_DGRAM)
651 while (nfs_connect(nmp, NULL)) {
652 printf("nfs_args: retrying connect\n");
653 (void) tsleep((caddr_t)&lbolt,
654 PSOCK, "nfscon", 0);
655 }
656 }
657 }
658
659 /*
660 * VFS Operations.
661 *
662 * mount system call
663 * It seems a bit dumb to copyinstr() the host and path here and then
664 * bcopy() them in mountnfs(), but I wanted to detect errors before
665 * doing the sockargs() call because sockargs() allocates an mbuf and
666 * an error after that means that I have to release the mbuf.
667 */
668 /* ARGSUSED */
669 static int
670 nfs_omount(struct mount *mp, char *path, caddr_t data, struct thread *td)
671 {
672 int error;
673 struct nfs_args args;
674 struct sockaddr *nam;
675 struct vnode *vp;
676 char hst[MNAMELEN];
677 size_t len;
678 u_char nfh[NFSX_V3FHMAX];
679
680 if (path == NULL)
681 return (nfs_mountroot(mp, td));
682 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
683 if (error)
684 return (error);
685 if (args.version != NFS_ARGSVERSION) {
686 #ifdef COMPAT_PRELITE2
687 /*
688 * If the argument version is unknown, then assume the
689 * caller is a pre-lite2 4.4BSD client and convert its
690 * arguments.
691 */
692 struct onfs_args oargs;
693 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
694 if (error)
695 return (error);
696 nfs_convert_oargs(&args,&oargs);
697 #else /* !COMPAT_PRELITE2 */
698 return (EPROGMISMATCH);
699 #endif /* COMPAT_PRELITE2 */
700 }
701 if (mp->mnt_flag & MNT_UPDATE) {
702 struct nfsmount *nmp = VFSTONFS(mp);
703
704 if (nmp == NULL)
705 return (EIO);
706 /*
707 * When doing an update, we can't change from or to
708 * v3, switch lockd strategies or change cookie translation
709 */
710 args.flags = (args.flags &
711 ~(NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/)) |
712 (nmp->nm_flag &
713 (NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/));
714 nfs_decode_args(nmp, &args);
715 return (0);
716 }
717
718 /*
719 * Make the nfs_ip_paranoia sysctl serve as the default connection
720 * or no-connection mode for those protocols that support
721 * no-connection mode (the flag will be cleared later for protocols
722 * that do not support no-connection mode). This will allow a client
723 * to receive replies from a different IP then the request was
724 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid),
725 * not 0.
726 */
727 if (nfs_ip_paranoia == 0)
728 args.flags |= NFSMNT_NOCONN;
729 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
730 return (EINVAL);
731 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
732 if (error)
733 return (error);
734 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
735 if (error)
736 return (error);
737 bzero(&hst[len], MNAMELEN - len);
738 /* sockargs() call must be after above copyin() calls */
739 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
740 if (error)
741 return (error);
742 args.fh = nfh;
743 error = mountnfs(&args, mp, nam, path, hst, &vp, td->td_ucred);
744 return (error);
745 }
746
747 /*
748 * Common code for mount and mountroot
749 */
750 static int
751 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
752 char *pth, char *hst, struct vnode **vpp, struct ucred *cred)
753 {
754 struct nfsmount *nmp;
755 struct nfsnode *np;
756 int error;
757 struct vattr attrs;
758
759 if (mp->mnt_flag & MNT_UPDATE) {
760 nmp = VFSTONFS(mp);
761 /* update paths, file handles, etc, here XXX */
762 FREE(nam, M_SONAME);
763 return (0);
764 } else {
765 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
766 bzero((caddr_t)nmp, sizeof (struct nfsmount));
767 TAILQ_INIT(&nmp->nm_bufq);
768 mp->mnt_data = (qaddr_t)nmp;
769 }
770 vfs_getnewfsid(mp);
771 nmp->nm_mountp = mp;
772
773 /*
774 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
775 * high, depending on whether we end up with negative offsets in
776 * the client or server somewhere. 2GB-1 may be safer.
777 *
778 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
779 * that we can handle until we find out otherwise.
780 * XXX Our "safe" limit on the client is what we can store in our
781 * buffer cache using signed(!) block numbers.
782 */
783 if ((argp->flags & NFSMNT_NFSV3) == 0)
784 nmp->nm_maxfilesize = 0xffffffffLL;
785 else
786 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
787
788 nmp->nm_timeo = NFS_TIMEO;
789 nmp->nm_retry = NFS_RETRANS;
790 if ((argp->flags & NFSMNT_NFSV3) && argp->sotype == SOCK_STREAM) {
791 nmp->nm_wsize = nmp->nm_rsize = NFS_MAXDATA;
792 } else {
793 nmp->nm_wsize = NFS_WSIZE;
794 nmp->nm_rsize = NFS_RSIZE;
795 }
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: 3384cb542f20740f356ee5edc3430134
|