1 /* $Id: nfs_vfsops.c,v 1.38 2003/11/05 14:59:01 rees Exp $ */
2
3 /*-
4 * copyright (c) 2003
5 * the regents of the university of michigan
6 * all rights reserved
7 *
8 * permission is granted to use, copy, create derivative works and redistribute
9 * this software and such derivative works for any purpose, so long as the name
10 * of the university of michigan is not used in any advertising or publicity
11 * pertaining to the use or distribution of this software without specific,
12 * written prior authorization. if the above copyright notice or any other
13 * identification of the university of michigan is included in any copy of any
14 * portion of this software, then the disclaimer below must also be included.
15 *
16 * this software is provided as is, without representation from the university
17 * of michigan as to its fitness for any purpose, and without warranty by the
18 * university of michigan of any kind, either express or implied, including
19 * without limitation the implied warranties of merchantability and fitness for
20 * a particular purpose. the regents of the university of michigan shall not be
21 * liable for any damages, including special, indirect, incidental, or
22 * consequential damages, with respect to any claim arising out of or in
23 * connection with the use of the software, even if it has been or is hereafter
24 * advised of the possibility of such damages.
25 */
26
27 /*-
28 * Copyright (c) 1989, 1993, 1995
29 * The Regents of the University of California. All rights reserved.
30 *
31 * This code is derived from software contributed to Berkeley by
32 * Rick Macklem at The University of Guelph.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
59 */
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD: src/sys/nfs4client/nfs4_vfsops.c,v 1.36 2008/10/23 15:53:51 des Exp $");
63
64 #include "opt_bootp.h"
65 #include "opt_nfsroot.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/kthread.h>
71 #include <sys/limits.h>
72 #include <sys/lock.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/module.h>
76 #include <sys/mount.h>
77 #include <sys/proc.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sockio.h>
81 #include <sys/sysctl.h>
82 #include <sys/unistd.h>
83 #include <sys/vnode.h>
84 #include <sys/signalvar.h>
85
86 #include <vm/vm.h>
87 #include <vm/vm_extern.h>
88 #include <vm/uma.h>
89
90 #include <net/if.h>
91 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94
95 #include <rpc/rpcclnt.h>
96
97 #include <nfs/rpcv2.h>
98 #include <nfs/nfsproto.h>
99 #include <nfsclient/nfs.h>
100 #include <nfs4client/nfs4.h>
101 #include <nfsclient/nfsnode.h>
102 #include <nfsclient/nfsmount.h>
103 #include <nfs/xdr_subs.h>
104 #include <nfsclient/nfsm_subs.h>
105 #include <nfsclient/nfsdiskless.h>
106
107 #include <nfs4client/nfs4m_subs.h>
108 #include <nfs4client/nfs4_vfs.h>
109
110 #include <nfs4client/nfs4_dev.h>
111 #include <nfs4client/nfs4_idmap.h>
112
113 SYSCTL_NODE(_vfs, OID_AUTO, nfs4, CTLFLAG_RW, 0, "NFS4 filesystem");
114 SYSCTL_STRUCT(_vfs_nfs4, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
115 &nfsstats, nfsstats, "S,nfsstats");
116
117 static void nfs4_decode_args(struct nfsmount *nmp, struct nfs_args *argp);
118 static void nfs4_daemon(void *arg);
119 static int mountnfs(struct nfs_args *, struct mount *,
120 struct sockaddr *, char *, struct vnode **,
121 struct ucred *cred);
122 static int nfs4_do_setclientid(struct nfsmount *nmp, struct ucred *cred);
123 static vfs_mount_t nfs4_mount;
124 static vfs_cmount_t nfs4_cmount;
125 static vfs_unmount_t nfs4_unmount;
126 static vfs_root_t nfs4_root;
127 static vfs_statfs_t nfs4_statfs;
128 static vfs_sync_t nfs4_sync;
129
130 static int fake_wchan;
131
132 /*
133 * nfs vfs operations.
134 */
135 static struct vfsops nfs4_vfsops = {
136 .vfs_init = nfs4_init,
137 .vfs_mount = nfs4_mount,
138 .vfs_cmount = nfs4_cmount,
139 .vfs_root = nfs4_root,
140 .vfs_statfs = nfs4_statfs,
141 .vfs_sync = nfs4_sync,
142 .vfs_uninit = nfs4_uninit,
143 .vfs_unmount = nfs4_unmount,
144 };
145 VFS_SET(nfs4_vfsops, nfs4, VFCF_NETWORK);
146
147 static struct nfs_rpcops nfs4_rpcops = {
148 nfs4_readrpc,
149 nfs4_writerpc,
150 nfs4_writebp,
151 nfs4_readlinkrpc,
152 nfs4_invaldir,
153 nfs4_commit,
154 };
155
156 /* So that loader and kldload(2) can find us, wherever we are.. */
157 MODULE_VERSION(nfs4, 1);
158
159 void nfsargs_ntoh(struct nfs_args *);
160
161 int
162 nfs4_init(struct vfsconf *vfsp)
163 {
164
165 rpcclnt_init();
166 nfs4dev_init();
167 idmap_init();
168 nfsm_v4init();
169
170 return (0);
171 }
172
173 int
174 nfs4_uninit(struct vfsconf *vfsp)
175 {
176
177 rpcclnt_uninit();
178 nfs4dev_uninit();
179 idmap_uninit();
180
181 return (0);
182 }
183
184 /*
185 * nfs statfs call
186 */
187 static int
188 nfs4_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
189 {
190 struct vnode *vp;
191 struct nfs_statfs *sfp;
192 caddr_t bpos, dpos;
193 struct nfsmount *nmp = VFSTONFS(mp);
194 int error = 0;
195 struct mbuf *mreq, *mrep = NULL, *md, *mb;
196 struct nfsnode *np;
197 struct nfs4_compound cp;
198 struct nfs4_oparg_getattr ga;
199 struct nfsv4_fattr *fap = &ga.fa;
200
201 #ifndef nolint
202 sfp = NULL;
203 #endif
204 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np, LK_EXCLUSIVE);
205 if (error)
206 return (error);
207 vp = NFSTOV(np);
208 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
209 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, NFSX_FH(1));
210 mb = mreq;
211 bpos = mtod(mb, caddr_t);
212
213 ga.bm = &nfsv4_fsattrbm;
214 nfs_v4initcompound(&cp);
215
216 nfsm_v4build_compound(&cp, "statfs()");
217 nfsm_v4build_putfh(&cp, vp);
218 nfsm_v4build_getattr(&cp, &ga);
219 nfsm_v4build_finalize(&cp);
220
221 nfsm_request(vp, NFSV4PROC_COMPOUND, td, td->td_ucred);
222 if (error != 0)
223 goto nfsmout;
224
225 nfsm_v4dissect_compound(&cp);
226 nfsm_v4dissect_putfh(&cp);
227 nfsm_v4dissect_getattr(&cp, &ga);
228
229 nfs4_vfsop_statfs(fap, sbp, mp);
230
231 nfsmout:
232 error = nfs_v4postop(&cp, error);
233
234 vput(vp);
235 if (mrep != NULL)
236 m_freem(mrep);
237
238 return (error);
239 }
240
241 static void
242 nfs4_decode_args(struct nfsmount *nmp, struct nfs_args *argp)
243 {
244 int s;
245 int adjsock;
246 int maxio;
247
248 s = splnet();
249
250 /*
251 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
252 * no sense in that context. Also, set appropriate retransmit
253 * and soft timeout behavior.
254 */
255 if (argp->sotype == SOCK_STREAM) {
256 nmp->nm_flag &= ~NFSMNT_NOCONN;
257 nmp->nm_flag |= NFSMNT_DUMBTIMR;
258 nmp->nm_timeo = NFS_MAXTIMEO;
259 nmp->nm_retry = NFS_RETRANS_TCP;
260 }
261
262 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
263
264 /* Re-bind if rsrvd port requested and wasn't on one */
265 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
266 && (argp->flags & NFSMNT_RESVPORT);
267 /* Also re-bind if we're switching to/from a connected UDP socket */
268 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
269 (argp->flags & NFSMNT_NOCONN));
270
271 /* Update flags atomically. Don't change the lock bits. */
272 nmp->nm_flag = argp->flags | nmp->nm_flag;
273 splx(s);
274
275 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
276 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
277 if (nmp->nm_timeo < NFS_MINTIMEO)
278 nmp->nm_timeo = NFS_MINTIMEO;
279 else if (nmp->nm_timeo > NFS_MAXTIMEO)
280 nmp->nm_timeo = NFS_MAXTIMEO;
281 }
282
283 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
284 nmp->nm_retry = argp->retrans;
285 if (nmp->nm_retry > NFS_MAXREXMIT)
286 nmp->nm_retry = NFS_MAXREXMIT;
287 }
288
289 if (argp->flags & NFSMNT_NFSV3) {
290 if (argp->sotype == SOCK_DGRAM)
291 maxio = NFS_MAXDGRAMDATA;
292 else
293 maxio = NFS_MAXDATA;
294 } else
295 maxio = NFS_V2MAXDATA;
296
297 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
298 nmp->nm_wsize = argp->wsize;
299 /* Round down to multiple of blocksize */
300 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
301 if (nmp->nm_wsize <= 0)
302 nmp->nm_wsize = NFS_FABLKSIZE;
303 }
304 if (nmp->nm_wsize > maxio)
305 nmp->nm_wsize = maxio;
306 if (nmp->nm_wsize > MAXBSIZE)
307 nmp->nm_wsize = MAXBSIZE;
308
309 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
310 nmp->nm_rsize = argp->rsize;
311 /* Round down to multiple of blocksize */
312 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
313 if (nmp->nm_rsize <= 0)
314 nmp->nm_rsize = NFS_FABLKSIZE;
315 }
316 if (nmp->nm_rsize > maxio)
317 nmp->nm_rsize = maxio;
318 if (nmp->nm_rsize > MAXBSIZE)
319 nmp->nm_rsize = MAXBSIZE;
320
321 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
322 nmp->nm_readdirsize = argp->readdirsize;
323 }
324 if (nmp->nm_readdirsize > maxio)
325 nmp->nm_readdirsize = maxio;
326 if (nmp->nm_readdirsize > nmp->nm_rsize)
327 nmp->nm_readdirsize = nmp->nm_rsize;
328
329 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
330 nmp->nm_acregmin = argp->acregmin;
331 else
332 nmp->nm_acregmin = NFS_MINATTRTIMO;
333 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
334 nmp->nm_acregmax = argp->acregmax;
335 else
336 nmp->nm_acregmax = NFS_MAXATTRTIMO;
337 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
338 nmp->nm_acdirmin = argp->acdirmin;
339 else
340 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
341 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
342 nmp->nm_acdirmax = argp->acdirmax;
343 else
344 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
345 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
346 nmp->nm_acdirmin = nmp->nm_acdirmax;
347 if (nmp->nm_acregmin > nmp->nm_acregmax)
348 nmp->nm_acregmin = nmp->nm_acregmax;
349
350 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
351 if (argp->maxgrouplist <= NFS_MAXGRPS)
352 nmp->nm_numgrps = argp->maxgrouplist;
353 else
354 nmp->nm_numgrps = NFS_MAXGRPS;
355 }
356 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
357 if (argp->readahead <= NFS_MAXRAHEAD)
358 nmp->nm_readahead = argp->readahead;
359 else
360 nmp->nm_readahead = NFS_MAXRAHEAD;
361 }
362 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
363 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
364 nmp->nm_deadthresh = argp->deadthresh;
365 else
366 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
367 }
368
369 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
370 (nmp->nm_soproto != argp->proto));
371 nmp->nm_sotype = argp->sotype;
372 nmp->nm_soproto = argp->proto;
373
374 if (nmp->nm_rpcclnt.rc_so && adjsock) {
375 nfs_safedisconnect(nmp);
376 if (nmp->nm_sotype == SOCK_DGRAM) {
377 while (nfs4_connect(nmp)) {
378 printf("nfs4_decode_args: retrying connect\n");
379 (void)tsleep(&fake_wchan, PSOCK, "nfscon", hz);
380 }
381 }
382 }
383 }
384
385 /*
386 * VFS Operations.
387 *
388 * mount system call
389 * It seems a bit dumb to copyinstr() the host and path here and then
390 * bcopy() them in mountnfs(), but I wanted to detect errors before
391 * doing the sockargs() call because sockargs() allocates an mbuf and
392 * an error after that means that I have to release the mbuf.
393 */
394 /* ARGSUSED */
395 static int
396 nfs4_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
397 {
398 struct nfs_args args;
399 int error;
400
401 error = copyin(data, &args, sizeof(struct nfs_args));
402 if (error)
403 return (error);
404 ma = mount_arg(ma, "nfs_args", &args, sizeof args);
405 error = kernel_mount(ma, flags);
406 return (error);
407 }
408
409 static int
410 nfs4_mount(struct mount *mp, struct thread *td)
411 {
412 int error;
413 struct nfs_args args;
414 struct sockaddr *nam;
415 struct vnode *vp;
416 char hst[MNAMELEN];
417 size_t len;
418
419 if (mp->mnt_flag & MNT_ROOTFS) {
420 printf("nfs4_mountroot not supported\n");
421 return (EINVAL);
422 }
423 error = vfs_copyopt(mp->mnt_optnew, "nfs_args", &args, sizeof args);
424 if (error)
425 return (error);
426
427 if (args.version != NFS_ARGSVERSION)
428 return (EPROGMISMATCH);
429 if (mp->mnt_flag & MNT_UPDATE) {
430 struct nfsmount *nmp = VFSTONFS(mp);
431
432 if (nmp == NULL)
433 return (EIO);
434 /*
435 * When doing an update, we can't change from or to
436 * v3, switch lockd strategies or change cookie translation
437 */
438 args.flags = (args.flags &
439 ~(NFSMNT_NFSV3 | NFSMNT_NFSV4 | NFSMNT_NOLOCKD)) |
440 (nmp->nm_flag &
441 (NFSMNT_NFSV3 | NFSMNT_NFSV4 | NFSMNT_NOLOCKD));
442 nfs4_decode_args(nmp, &args);
443 return (0);
444 }
445
446 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
447 if (error)
448 return (error);
449 bzero(&hst[len], MNAMELEN - len);
450 /* sockargs() call must be after above copyin() calls */
451 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
452 if (error)
453 return (error);
454 error = mountnfs(&args, mp, nam, hst, &vp, td->td_ucred);
455 return (error);
456 }
457
458 /*
459 * renew should be done async
460 * should re-scan mount queue each time
461 */
462 struct proc *nfs4_daemonproc;
463
464 static int
465 nfs4_do_renew(struct nfsmount *nmp, struct ucred *cred)
466 {
467 struct nfs4_compound cp;
468 struct mbuf *mreq, *mrep = NULL, *md, *mb;
469 caddr_t bpos, dpos;
470 int error;
471
472 mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, sizeof(uint64_t));
473 mb = mreq;
474 bpos = mtod(mb, caddr_t);
475
476 nfs_v4initcompound(&cp);
477
478 nfsm_v4build_compound(&cp, "nfs4_do_renew()");
479 nfsm_v4build_renew(&cp, nmp->nm_clientid);
480 nfsm_v4build_finalize(&cp);
481
482 nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
483 if (error != 0)
484 goto nfsmout;
485
486 nfsm_v4dissect_compound(&cp);
487 nfsm_v4dissect_renew(&cp);
488 nmp->nm_last_renewal = time_second;
489 return (0);
490
491 nfsmout:
492 error = nfs_v4postop(&cp, error);
493
494 /* XXX */
495 if (mrep != NULL)
496 m_freem(mrep);
497 return (error);
498 }
499
500 static void
501 nfs4_daemon(void *arg)
502 {
503 struct mount *mp;
504 struct nfsmount *nmp;
505 int nmounts;
506
507 while (1) {
508 nmounts = 0;
509 mtx_lock(&mountlist_mtx);
510 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
511 if (strcmp(mp->mnt_vfc->vfc_name, "nfs4") != 0)
512 continue;
513 nmounts++;
514 nmp = VFSTONFS(mp);
515 if (time_second < nmp->nm_last_renewal + nmp->nm_lease_time - 4)
516 continue;
517 mtx_unlock(&mountlist_mtx);
518 mtx_lock(&Giant);
519 nfs4_do_renew(nmp, (struct ucred *) arg);
520 mtx_unlock(&Giant);
521 mtx_lock(&mountlist_mtx);
522 }
523 mtx_unlock(&mountlist_mtx);
524
525 /* Must kill the daemon here, or module unload will cause a panic */
526 if (nmounts == 0) {
527 mtx_lock(&Giant);
528 nfs4_daemonproc = NULL;
529 mtx_unlock(&Giant);
530 /*printf("nfsv4 renewd exiting\n");*/
531 kproc_exit(0);
532 }
533 tsleep(&nfs4_daemonproc, PVFS, "nfs4", 2 * hz);
534 }
535 }
536
537 /*
538 * Common code for mount and mountroot
539 */
540 static int
541 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
542 char *hst, struct vnode **vpp, struct ucred *cred)
543 {
544 struct nfsmount *nmp;
545 char *rpth, *cp1, *cp2;
546 int nlkup = 0, error;
547 struct nfs4_compound cp;
548 struct mbuf *mreq, *mrep = NULL, *md, *mb;
549 caddr_t bpos, dpos;
550 struct nfs4_oparg_lookup lkup;
551 struct nfs4_oparg_getfh gfh;
552 struct nfs4_oparg_getattr ga;
553 struct thread *td = curthread; /* XXX */
554
555 if (mp->mnt_flag & MNT_UPDATE) {
556 nmp = VFSTONFS(mp);
557 /* update paths, file handles, etc, here XXX */
558 free(nam, M_SONAME);
559 return (0);
560 } else {
561 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
562 bzero((caddr_t)nmp, sizeof (struct nfsmount));
563 TAILQ_INIT(&nmp->nm_bufq);
564 mp->mnt_data = nmp;
565 }
566
567 vfs_getnewfsid(mp);
568 nmp->nm_mountp = mp;
569 mtx_init(&nmp->nm_mtx, "NFS4mount lock", NULL, MTX_DEF);
570
571 nmp->nm_maxfilesize = 0xffffffffLL;
572 nmp->nm_timeo = NFS_TIMEO;
573 nmp->nm_retry = NFS_RETRANS;
574 nmp->nm_wsize = NFS_WSIZE;
575 nmp->nm_rsize = NFS_RSIZE;
576 nmp->nm_readdirsize = NFS_READDIRSIZE;
577 nmp->nm_numgrps = NFS_MAXGRPS;
578 nmp->nm_readahead = NFS_DEFRAHEAD;
579 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
580 vfs_mountedfrom(mp, hst);
581 nmp->nm_nam = nam;
582 /* Set up the sockets and per-host congestion */
583 nmp->nm_sotype = argp->sotype;
584 nmp->nm_soproto = argp->proto;
585 nmp->nm_rpcops = &nfs4_rpcops;
586 /* XXX */
587 mp->mnt_stat.f_iosize = PAGE_SIZE;
588
589 argp->flags |= (NFSMNT_NFSV3 | NFSMNT_NFSV4);
590
591 nfs4_decode_args(nmp, argp);
592
593 if ((error = nfs4_connect(nmp)))
594 goto bad;
595
596 mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
597 mb = mreq;
598 bpos = mtod(mb, caddr_t);
599
600 ga.bm = &nfsv4_fsinfobm;
601 nfs_v4initcompound(&cp);
602
603 /* Get remote path */
604 rpth = hst;
605 strsep(&rpth, ":");
606
607 nfsm_v4build_compound(&cp, "mountnfs()");
608 nfsm_v4build_putrootfh(&cp);
609 for (cp1 = rpth; cp1 && *cp1; cp1 = cp2) {
610 while (*cp1 == '/')
611 cp1++;
612 if (!*cp1)
613 break;
614 for (cp2 = cp1; *cp2 && *cp2 != '/'; cp2++)
615 ;
616 lkup.name = cp1;
617 lkup.namelen = cp2 - cp1;
618 nfsm_v4build_lookup(&cp, &lkup);
619 nlkup++;
620 }
621 nfsm_v4build_getfh(&cp, &gfh);
622 nfsm_v4build_getattr(&cp, &ga);
623 nfsm_v4build_finalize(&cp);
624
625 nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, td, cred);
626 if (error != 0)
627 goto nfsmout;
628
629 nfsm_v4dissect_compound(&cp);
630 nfsm_v4dissect_putrootfh(&cp);
631 while (nlkup--)
632 nfsm_v4dissect_lookup(&cp);
633 nfsm_v4dissect_getfh(&cp, &gfh);
634 nfsm_v4dissect_getattr(&cp, &ga);
635
636 nfs4_vfsop_fsinfo(&ga.fa, nmp);
637 nmp->nm_state |= NFSSTA_GOTFSINFO;
638
639 /* Copy root fh into nfsmount. */
640 nmp->nm_fhsize = gfh.fh_len;
641 bcopy(&gfh.fh_val, nmp->nm_fh, nmp->nm_fhsize);
642 nmp->nm_last_renewal = time_second;
643
644 if ((error = nfs4_do_setclientid(nmp, cred)) != 0)
645 goto nfsmout;
646
647 /* Start renewd if it isn't already running */
648 if (nfs4_daemonproc == NULL)
649 kproc_create(nfs4_daemon, crdup(cred), &nfs4_daemonproc,
650 (RFPROC|RFMEM), 0, "nfs4rd");
651
652 return (0);
653 nfsmout:
654 error = nfs_v4postop(&cp, error);
655
656 /* XXX */
657 if (mrep != NULL)
658 m_freem(mrep);
659 bad:
660 mtx_destroy(&nmp->nm_mtx);
661 nfs4_disconnect(nmp);
662 uma_zfree(nfsmount_zone, nmp);
663 free(nam, M_SONAME);
664
665 return (error);
666 }
667
668 /*
669 * unmount system call
670 */
671 static int
672 nfs4_unmount(struct mount *mp, int mntflags, struct thread *td)
673 {
674 struct nfsmount *nmp;
675 int error, flags = 0;
676
677 if (mntflags & MNT_FORCE)
678 flags |= FORCECLOSE;
679 nmp = VFSTONFS(mp);
680 /*
681 * Goes something like this..
682 * - Call vflush(, td) to clear out vnodes for this filesystem
683 * - Close the socket
684 * - Free up the data structures
685 */
686 /* In the forced case, cancel any outstanding requests. */
687 if (flags & FORCECLOSE) {
688 error = nfs_nmcancelreqs(nmp);
689 if (error)
690 return (error);
691 nfs4dev_purge();
692 }
693
694 error = vflush(mp, 0, flags, td);
695 if (error)
696 return (error);
697
698 /*
699 * We are now committed to the unmount.
700 */
701 nfs4_disconnect(nmp);
702 free(nmp->nm_nam, M_SONAME);
703
704 /* XXX there's a race condition here for SMP */
705 wakeup(&nfs4_daemonproc);
706
707 mtx_destroy(&nmp->nm_mtx);
708 uma_zfree(nfsmount_zone, nmp);
709 return (0);
710 }
711
712 /*
713 * Return root of a filesystem
714 */
715 static int
716 nfs4_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
717 {
718 struct vnode *vp;
719 struct nfsmount *nmp;
720 struct nfsnode *np;
721 int error;
722
723 nmp = VFSTONFS(mp);
724 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np,
725 LK_EXCLUSIVE);
726 if (error)
727 return (error);
728 vp = NFSTOV(np);
729 if (vp->v_type == VNON)
730 vp->v_type = VDIR;
731 vp->v_vflag |= VV_ROOT;
732 *vpp = vp;
733
734 return (0);
735 }
736
737 /*
738 * Flush out the buffer cache
739 */
740 static int
741 nfs4_sync(struct mount *mp, int waitfor, struct thread *td)
742 {
743 struct vnode *vp, *mvp;
744 int error, allerror = 0;
745
746 /*
747 * Force stale buffer cache information to be flushed.
748 */
749 MNT_ILOCK(mp);
750 loop:
751 MNT_VNODE_FOREACH(vp, mp, mvp) {
752 VI_LOCK(vp);
753 MNT_IUNLOCK(mp);
754 /* XXX racy bv_cnt check. */
755 if (VOP_ISLOCKED(vp) || vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
756 waitfor == MNT_LAZY) {
757 VI_UNLOCK(vp);
758 MNT_ILOCK(mp);
759 continue;
760 }
761 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
762 MNT_ILOCK(mp);
763 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
764 goto loop;
765 }
766 error = VOP_FSYNC(vp, waitfor, td);
767 if (error)
768 allerror = error;
769 VOP_UNLOCK(vp, 0);
770 vrele(vp);
771
772 MNT_ILOCK(mp);
773 }
774 MNT_IUNLOCK(mp);
775 return (allerror);
776 }
777
778 static int
779 nfs4_do_setclientid(struct nfsmount *nmp, struct ucred *cred)
780 {
781 struct nfs4_oparg_setclientid scid;
782 struct nfs4_compound cp;
783 struct mbuf *mreq, *mrep = NULL, *md, *mb;
784 caddr_t bpos, dpos;
785 struct route ro;
786 char *ipsrc = NULL, uaddr[24], name[24];
787 int try = 0;
788 static unsigned long seq;
789 int error;
790
791 #ifndef NFS4_USE_RPCCLNT
792 return (0);
793 #endif
794 if (nmp->nm_clientid) {
795 printf("nfs4_do_setclientid: already have clientid!\n");
796 error = 0;
797 goto nfsmout;
798 }
799
800 /* Try not to re-use clientids */
801 if (seq == 0)
802 seq = time_second;
803
804 #ifdef NFS4_USE_RPCCLNT
805 scid.cb_netid = (nmp->nm_rpcclnt.rc_sotype == SOCK_STREAM) ? "tcp" : "udp";
806 #endif
807 scid.cb_netid = "tcp";
808 scid.cb_netidlen = 3;
809 scid.cb_prog = 0x1234; /* XXX */
810
811 /* Do a route lookup to find our source address for talking to this server */
812 bzero(&ro, sizeof ro);
813
814 #ifdef NFS4_USE_RPCCLNT
815 ro.ro_dst = *nmp->nm_rpcclnt.rc_name;
816 #endif
817 /* XXX MRT NFS uses table 0 */
818 in_rtalloc(&ro, 0);
819 if (ro.ro_rt == NULL) {
820 error = EHOSTUNREACH;
821 goto nfsmout;
822 }
823 ipsrc = inet_ntoa(IA_SIN(ifatoia(ro.ro_rt->rt_ifa))->sin_addr);
824 sprintf(uaddr, "%s.12.48", ipsrc);
825 scid.cb_univaddr = uaddr;
826 scid.cb_univaddrlen = strlen(uaddr);
827 RTFREE(ro.ro_rt);
828
829 try_again:
830 sprintf(name, "%s-%d", ipsrc, (int) ((seq + try) % 1000000L));
831 scid.namelen = strlen(name);
832 scid.name = name;
833 nfs_v4initcompound(&cp);
834
835 mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
836 mb = mreq;
837 bpos = mtod(mb, caddr_t);
838
839 nfsm_v4build_compound(&cp, "nfs4_do_setclientid()");
840 nfsm_v4build_setclientid(&cp, &scid);
841 nfsm_v4build_finalize(&cp);
842
843 nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
844 if (error != 0)
845 goto nfsmout;
846
847 nfsm_v4dissect_compound(&cp);
848 nfsm_v4dissect_setclientid(&cp, &scid);
849 nmp->nm_clientid = scid.clientid;
850
851 error = nfs_v4postop(&cp, error);
852
853 /* Confirm */
854 m_freem(mrep);
855 mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
856 mb = mreq;
857 bpos = mtod(mb, caddr_t);
858
859 nfs_v4initcompound(&cp);
860
861 nfsm_v4build_compound(&cp, "nfs4_do_setclientid() (confirm)");
862 nfsm_v4build_setclientid_confirm(&cp, &scid);
863 nfsm_v4build_finalize(&cp);
864
865 nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
866 if (error != 0)
867 goto nfsmout;
868
869 nfsm_v4dissect_compound(&cp);
870 nfsm_v4dissect_setclientid_confirm(&cp);
871
872 nfsmout:
873 error = nfs_v4postop(&cp, error);
874
875 if (mrep)
876 m_freem(mrep);
877 if (error == NFSERR_CLID_INUSE && (++try < NFS4_SETCLIENTID_MAXTRIES))
878 goto try_again;
879
880 return (error);
881 }
882
|