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