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