nfs_vfsops.c revision 1.101.2.2 1 /* $NetBSD: nfs_vfsops.c,v 1.101.2.2 2001/06/21 20:09:36 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
39 */
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_compat_netbsd.h"
43 #include "opt_nfs.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/signal.h>
49 #include <sys/lwp.h>
50 #include <sys/proc.h>
51 #include <sys/namei.h>
52 #include <sys/device.h>
53 #include <sys/vnode.h>
54 #include <sys/kernel.h>
55 #include <sys/mount.h>
56 #include <sys/buf.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
62
63 #include <net/if.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/nfsnode.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nfsmount.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfs/nfsm_subs.h>
74 #include <nfs/nfsdiskless.h>
75 #include <nfs/nqnfs.h>
76 #include <nfs/nfs_var.h>
77
78 extern struct nfsstats nfsstats;
79 extern int nfs_ticks;
80
81 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
82 struct proc *));
83
84 /*
85 * nfs vfs operations.
86 */
87
88 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
89 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
90 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
91
92 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
93 &nfsv2_vnodeop_opv_desc,
94 &spec_nfsv2nodeop_opv_desc,
95 &fifo_nfsv2nodeop_opv_desc,
96 NULL,
97 };
98
99 struct vfsops nfs_vfsops = {
100 MOUNT_NFS,
101 nfs_mount,
102 nfs_start,
103 nfs_unmount,
104 nfs_root,
105 nfs_quotactl,
106 nfs_statfs,
107 nfs_sync,
108 nfs_vget,
109 nfs_fhtovp,
110 nfs_vptofh,
111 nfs_vfs_init,
112 nfs_vfs_done,
113 nfs_sysctl,
114 nfs_mountroot,
115 nfs_checkexp,
116 nfs_vnodeopv_descs,
117 };
118
119 extern u_int32_t nfs_procids[NFS_NPROCS];
120 extern u_int32_t nfs_prog, nfs_vers;
121
122 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
123 struct mount **, struct vnode **, struct proc *));
124
125 #define TRUE 1
126 #define FALSE 0
127
128 /*
129 * nfs statfs call
130 */
131 int
132 nfs_statfs(mp, sbp, p)
133 struct mount *mp;
134 struct statfs *sbp;
135 struct proc *p;
136 {
137 struct vnode *vp;
138 struct nfs_statfs *sfp;
139 caddr_t cp;
140 u_int32_t *tl;
141 int32_t t1, t2;
142 caddr_t bpos, dpos, cp2;
143 struct nfsmount *nmp = VFSTONFS(mp);
144 int error = 0, retattr;
145 #ifdef NFS_V2_ONLY
146 const int v3 = 0;
147 #else
148 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
149 #endif
150 struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
151 struct ucred *cred;
152 u_quad_t tquad;
153
154 #ifndef nolint
155 sfp = (struct nfs_statfs *)0;
156 #endif
157 vp = nmp->nm_vnode;
158 cred = crget();
159 cred->cr_ngroups = 0;
160 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
161 (void)nfs_fsinfo(nmp, vp, cred, p);
162 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
163 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
164 nfsm_fhtom(vp, v3);
165 nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
166 if (v3)
167 nfsm_postop_attr(vp, retattr);
168 if (error) {
169 if (mrep != NULL)
170 m_free(mrep);
171 goto nfsmout;
172 }
173 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
174 #ifdef COMPAT_09
175 sbp->f_type = 2;
176 #else
177 sbp->f_type = 0;
178 #endif
179 sbp->f_flags = nmp->nm_flag;
180 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
181 if (v3) {
182 sbp->f_bsize = NFS_FABLKSIZE;
183 tquad = fxdr_hyper(&sfp->sf_tbytes);
184 sbp->f_blocks = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
185 tquad = fxdr_hyper(&sfp->sf_fbytes);
186 sbp->f_bfree = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
187 tquad = fxdr_hyper(&sfp->sf_abytes);
188 sbp->f_bavail = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
189 tquad = fxdr_hyper(&sfp->sf_tfiles);
190 sbp->f_files = (long)tquad;
191 tquad = fxdr_hyper(&sfp->sf_ffiles);
192 sbp->f_ffree = (long)tquad;
193 } else {
194 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
195 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
196 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
197 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
198 sbp->f_files = 0;
199 sbp->f_ffree = 0;
200 }
201 if (sbp != &mp->mnt_stat) {
202 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
203 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
204 }
205 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
206 nfsm_reqdone;
207 crfree(cred);
208 return (error);
209 }
210
211 #ifndef NFS_V2_ONLY
212 /*
213 * nfs version 3 fsinfo rpc call
214 */
215 int
216 nfs_fsinfo(nmp, vp, cred, p)
217 struct nfsmount *nmp;
218 struct vnode *vp;
219 struct ucred *cred;
220 struct proc *p;
221 {
222 struct nfsv3_fsinfo *fsp;
223 caddr_t cp;
224 int32_t t1, t2;
225 u_int32_t *tl, pref, max;
226 caddr_t bpos, dpos, cp2;
227 int error = 0, retattr;
228 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
229 u_int64_t maxfsize;
230
231 nfsstats.rpccnt[NFSPROC_FSINFO]++;
232 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
233 nfsm_fhtom(vp, 1);
234 nfsm_request(vp, NFSPROC_FSINFO, p, cred);
235 nfsm_postop_attr(vp, retattr);
236 if (!error) {
237 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
238 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
239 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
240 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
241 ~(NFS_FABLKSIZE - 1);
242 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
243 if (max < nmp->nm_wsize && max > 0) {
244 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
245 if (nmp->nm_wsize == 0)
246 nmp->nm_wsize = max;
247 }
248 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
249 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
250 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
251 ~(NFS_FABLKSIZE - 1);
252 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
253 if (max < nmp->nm_rsize && max > 0) {
254 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
255 if (nmp->nm_rsize == 0)
256 nmp->nm_rsize = max;
257 }
258 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
259 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
260 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
261 ~(NFS_DIRFRAGSIZ - 1);
262 if (max < nmp->nm_readdirsize && max > 0) {
263 nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
264 if (nmp->nm_readdirsize == 0)
265 nmp->nm_readdirsize = max;
266 }
267 /* XXX */
268 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
269 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
270 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
271 nmp->nm_maxfilesize = maxfsize;
272 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
273 }
274 nfsm_reqdone;
275 return (error);
276 }
277 #endif
278
279 /*
280 * Mount a remote root fs via. NFS. It goes like this:
281 * - Call nfs_boot_init() to fill in the nfs_diskless struct
282 * - build the rootfs mount point and call mountnfs() to do the rest.
283 */
284 int
285 nfs_mountroot()
286 {
287 struct nfs_diskless *nd;
288 struct vattr attr;
289 struct mount *mp;
290 struct vnode *vp;
291 struct proc *procp;
292 long n;
293 int error;
294
295 procp = curproc->l_proc; /* XXX */
296
297 if (root_device->dv_class != DV_IFNET)
298 return (ENODEV);
299
300 /*
301 * XXX time must be non-zero when we init the interface or else
302 * the arp code will wedge. [Fixed now in if_ether.c]
303 * However, the NFS attribute cache gives false "hits" when
304 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
305 */
306 if (time.tv_sec < NFS_MAXATTRTIMO)
307 time.tv_sec = NFS_MAXATTRTIMO;
308
309 /*
310 * Call nfs_boot_init() to fill in the nfs_diskless struct.
311 * Side effect: Finds and configures a network interface.
312 */
313 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
314 memset((caddr_t)nd, 0, sizeof(*nd));
315 error = nfs_boot_init(nd, procp);
316 if (error) {
317 free(nd, M_NFSMNT);
318 return (error);
319 }
320
321 /*
322 * Create the root mount point.
323 */
324 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
325 if (error)
326 goto out;
327 printf("root on %s\n", nd->nd_root.ndm_host);
328
329 /*
330 * Link it into the mount list.
331 */
332 simple_lock(&mountlist_slock);
333 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
334 simple_unlock(&mountlist_slock);
335 rootvp = vp;
336 mp->mnt_vnodecovered = NULLVP;
337 vfs_unbusy(mp);
338
339 /* Get root attributes (for the time). */
340 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
341 if (error)
342 panic("nfs_mountroot: getattr for root");
343 n = attr.va_atime.tv_sec;
344 #ifdef DEBUG
345 printf("root time: 0x%lx\n", n);
346 #endif
347 inittodr(n);
348
349 out:
350 if (error)
351 nfs_boot_cleanup(nd, procp);
352 free(nd, M_NFSMNT);
353 return (error);
354 }
355
356 /*
357 * Internal version of mount system call for diskless setup.
358 * Separate function because we used to call it twice.
359 * (once for root and once for swap)
360 */
361 static int
362 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
363 struct nfs_dlmount *ndmntp;
364 const char *mntname; /* mount point name */
365 struct mount **mpp;
366 struct vnode **vpp;
367 struct proc *p;
368 {
369 struct mount *mp;
370 struct mbuf *m;
371 int error;
372
373 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
374
375 mp->mnt_op = &nfs_vfsops;
376
377 /*
378 * Historical practice expects NFS root file systems to
379 * be initially mounted r/w.
380 */
381 mp->mnt_flag &= ~MNT_RDONLY;
382
383 /* Get mbuf for server sockaddr. */
384 m = m_get(M_WAIT, MT_SONAME);
385 if (m == NULL)
386 panic("nfs_mountroot: mget soname for %s", mntname);
387 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
388 (m->m_len = ndmntp->ndm_args.addr->sa_len));
389
390 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
391 ndmntp->ndm_args.hostname, vpp, p);
392 if (error) {
393 mp->mnt_op->vfs_refcount--;
394 vfs_unbusy(mp);
395 printf("nfs_mountroot: mount %s failed: %d\n",
396 mntname, error);
397 free(mp, M_MOUNT);
398 } else
399 *mpp = mp;
400
401 return (error);
402 }
403
404 void
405 nfs_decode_args(nmp, argp)
406 struct nfsmount *nmp;
407 struct nfs_args *argp;
408 {
409 int s;
410 int adjsock;
411 int maxio;
412
413 s = splsoftnet();
414
415 /*
416 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
417 * no sense in that context.
418 */
419 if (argp->sotype == SOCK_STREAM)
420 argp->flags &= ~NFSMNT_NOCONN;
421
422 /*
423 * Cookie translation is not needed for v2, silently ignore it.
424 */
425 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
426 NFSMNT_XLATECOOKIE)
427 argp->flags &= ~NFSMNT_XLATECOOKIE;
428
429 /* Re-bind if rsrvd port requested and wasn't on one */
430 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
431 && (argp->flags & NFSMNT_RESVPORT);
432 /* Also re-bind if we're switching to/from a connected UDP socket */
433 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
434 (argp->flags & NFSMNT_NOCONN));
435
436 /* Update flags. */
437 nmp->nm_flag = argp->flags;
438 splx(s);
439
440 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
441 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
442 if (nmp->nm_timeo < NFS_MINTIMEO)
443 nmp->nm_timeo = NFS_MINTIMEO;
444 else if (nmp->nm_timeo > NFS_MAXTIMEO)
445 nmp->nm_timeo = NFS_MAXTIMEO;
446 }
447
448 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
449 nmp->nm_retry = argp->retrans;
450 if (nmp->nm_retry > NFS_MAXREXMIT)
451 nmp->nm_retry = NFS_MAXREXMIT;
452 }
453
454 #ifndef NFS_V2_ONLY
455 if (argp->flags & NFSMNT_NFSV3) {
456 if (argp->sotype == SOCK_DGRAM)
457 maxio = NFS_MAXDGRAMDATA;
458 else
459 maxio = NFS_MAXDATA;
460 } else
461 #endif
462 maxio = NFS_V2MAXDATA;
463
464 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
465 int osize = nmp->nm_wsize;
466 nmp->nm_wsize = argp->wsize;
467 /* Round down to multiple of blocksize */
468 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
469 if (nmp->nm_wsize <= 0)
470 nmp->nm_wsize = NFS_FABLKSIZE;
471 adjsock |= (nmp->nm_wsize != osize);
472 }
473 if (nmp->nm_wsize > maxio)
474 nmp->nm_wsize = maxio;
475 if (nmp->nm_wsize > MAXBSIZE)
476 nmp->nm_wsize = MAXBSIZE;
477
478 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
479 int osize = nmp->nm_rsize;
480 nmp->nm_rsize = argp->rsize;
481 /* Round down to multiple of blocksize */
482 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
483 if (nmp->nm_rsize <= 0)
484 nmp->nm_rsize = NFS_FABLKSIZE;
485 adjsock |= (nmp->nm_rsize != osize);
486 }
487 if (nmp->nm_rsize > maxio)
488 nmp->nm_rsize = maxio;
489 if (nmp->nm_rsize > MAXBSIZE)
490 nmp->nm_rsize = MAXBSIZE;
491
492 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
493 nmp->nm_readdirsize = argp->readdirsize;
494 /* Round down to multiple of minimum blocksize */
495 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
496 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
497 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
498 /* Bigger than buffer size makes no sense */
499 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
500 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
501 } else if (argp->flags & NFSMNT_RSIZE)
502 nmp->nm_readdirsize = nmp->nm_rsize;
503
504 if (nmp->nm_readdirsize > maxio)
505 nmp->nm_readdirsize = maxio;
506
507 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
508 argp->maxgrouplist <= NFS_MAXGRPS)
509 nmp->nm_numgrps = argp->maxgrouplist;
510 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
511 argp->readahead <= NFS_MAXRAHEAD)
512 nmp->nm_readahead = argp->readahead;
513 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
514 argp->leaseterm <= NQ_MAXLEASE)
515 nmp->nm_leaseterm = argp->leaseterm;
516 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
517 argp->deadthresh <= NQ_NEVERDEAD)
518 nmp->nm_deadthresh = argp->deadthresh;
519
520 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
521 (nmp->nm_soproto != argp->proto));
522 nmp->nm_sotype = argp->sotype;
523 nmp->nm_soproto = argp->proto;
524
525 if (nmp->nm_so && adjsock) {
526 nfs_safedisconnect(nmp);
527 if (nmp->nm_sotype == SOCK_DGRAM)
528 while (nfs_connect(nmp, (struct nfsreq *)0)) {
529 printf("nfs_args: retrying connect\n");
530 (void) tsleep((caddr_t)&lbolt,
531 PSOCK, "nfscn3", 0);
532 }
533 }
534 }
535
536 /*
537 * VFS Operations.
538 *
539 * mount system call
540 * It seems a bit dumb to copyinstr() the host and path here and then
541 * memcpy() them in mountnfs(), but I wanted to detect errors before
542 * doing the sockargs() call because sockargs() allocates an mbuf and
543 * an error after that means that I have to release the mbuf.
544 */
545 /* ARGSUSED */
546 int
547 nfs_mount(mp, path, data, ndp, p)
548 struct mount *mp;
549 const char *path;
550 void *data;
551 struct nameidata *ndp;
552 struct proc *p;
553 {
554 int error;
555 struct nfs_args args;
556 struct mbuf *nam;
557 struct vnode *vp;
558 char pth[MNAMELEN], hst[MNAMELEN];
559 size_t len;
560 u_char nfh[NFSX_V3FHMAX];
561
562 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
563 if (error)
564 return (error);
565 if (args.version != NFS_ARGSVERSION)
566 return (EPROGMISMATCH);
567 #ifdef NFS_V2_ONLY
568 if (args.flags & NFSMNT_NQNFS)
569 return (EPROGUNAVAIL);
570 if (args.flags & NFSMNT_NFSV3)
571 return (EPROGMISMATCH);
572 #endif
573 if (mp->mnt_flag & MNT_UPDATE) {
574 struct nfsmount *nmp = VFSTONFS(mp);
575
576 if (nmp == NULL)
577 return (EIO);
578 /*
579 * When doing an update, we can't change from or to
580 * v3 and/or nqnfs, or change cookie translation
581 */
582 args.flags = (args.flags &
583 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
584 (nmp->nm_flag &
585 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
586 nfs_decode_args(nmp, &args);
587 return (0);
588 }
589 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
590 if (error)
591 return (error);
592 error = copyinstr(path, pth, MNAMELEN-1, &len);
593 if (error)
594 return (error);
595 memset(&pth[len], 0, MNAMELEN - len);
596 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
597 if (error)
598 return (error);
599 memset(&hst[len], 0, MNAMELEN - len);
600 /* sockargs() call must be after above copyin() calls */
601 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
602 if (error)
603 return (error);
604 args.fh = nfh;
605 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
606 return (error);
607 }
608
609 /*
610 * Common code for mount and mountroot
611 */
612 int
613 mountnfs(argp, mp, nam, pth, hst, vpp, p)
614 struct nfs_args *argp;
615 struct mount *mp;
616 struct mbuf *nam;
617 const char *pth, *hst;
618 struct vnode **vpp;
619 struct proc *p;
620 {
621 struct nfsmount *nmp;
622 struct nfsnode *np;
623 int error;
624 struct vattr attrs;
625 struct ucred *cr;
626
627 /*
628 * If the number of nfs iothreads to use has never
629 * been set, create a reasonable number of them.
630 */
631
632 if (nfs_niothreads < 0) {
633 nfs_niothreads = 4;
634 nfs_getset_niothreads(TRUE);
635 }
636
637 if (mp->mnt_flag & MNT_UPDATE) {
638 nmp = VFSTONFS(mp);
639 /* update paths, file handles, etc, here XXX */
640 m_freem(nam);
641 return (0);
642 } else {
643 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
644 M_NFSMNT, M_WAITOK);
645 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
646 mp->mnt_data = (qaddr_t)nmp;
647 TAILQ_INIT(&nmp->nm_uidlruhead);
648 TAILQ_INIT(&nmp->nm_bufq);
649 }
650 vfs_getnewfsid(mp);
651 nmp->nm_mountp = mp;
652
653 #ifndef NFS_V2_ONLY
654 if (argp->flags & NFSMNT_NQNFS)
655 /*
656 * We have to set mnt_maxsymlink to a non-zero value so
657 * that COMPAT_43 routines will know that we are setting
658 * the d_type field in directories (and can zero it for
659 * unsuspecting binaries).
660 */
661 mp->mnt_maxsymlinklen = 1;
662 #endif
663
664 #ifndef NFS_V2_ONLY
665 if ((argp->flags & NFSMNT_NFSV3) == 0)
666 #endif
667 /*
668 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
669 * will fill this in.
670 */
671 nmp->nm_maxfilesize = 0xffffffffLL;
672
673 nmp->nm_timeo = NFS_TIMEO;
674 nmp->nm_retry = NFS_RETRANS;
675 nmp->nm_wsize = NFS_WSIZE;
676 nmp->nm_rsize = NFS_RSIZE;
677 nmp->nm_readdirsize = NFS_READDIRSIZE;
678 nmp->nm_numgrps = NFS_MAXGRPS;
679 nmp->nm_readahead = NFS_DEFRAHEAD;
680 nmp->nm_leaseterm = NQ_DEFLEASE;
681 nmp->nm_deadthresh = NQ_DEADTHRESH;
682 CIRCLEQ_INIT(&nmp->nm_timerhead);
683 nmp->nm_inprog = NULLVP;
684 #ifdef COMPAT_09
685 mp->mnt_stat.f_type = 2;
686 #else
687 mp->mnt_stat.f_type = 0;
688 #endif
689 mp->mnt_fs_bshift = DEV_BSHIFT;
690 mp->mnt_dev_bshift = -1;
691 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name,
692 MFSNAMELEN);
693 memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
694 memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
695 nmp->nm_nam = nam;
696
697 /* Set up the sockets and per-host congestion */
698 nmp->nm_sotype = argp->sotype;
699 nmp->nm_soproto = argp->proto;
700
701 nfs_decode_args(nmp, argp);
702
703 /*
704 * For Connection based sockets (TCP,...) defer the connect until
705 * the first request, in case the server is not responding.
706 */
707 if (nmp->nm_sotype == SOCK_DGRAM &&
708 (error = nfs_connect(nmp, (struct nfsreq *)0)))
709 goto bad;
710
711 /*
712 * This is silly, but it has to be set so that vinifod() works.
713 * We do not want to do an nfs_statfs() here since we can get
714 * stuck on a dead server and we are holding a lock on the mount
715 * point.
716 */
717 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
718 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
719 if (error)
720 goto bad;
721 *vpp = NFSTOV(np);
722 VOP_GETATTR(*vpp, &attrs, p->p_ucred, p);
723 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
724 cr = crget();
725 cr->cr_uid = attrs.va_uid;
726 cr->cr_gid = attrs.va_gid;
727 cr->cr_ngroups = 0;
728 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
729 crfree(cr);
730 }
731
732 /*
733 * A reference count is needed on the nfsnode representing the
734 * remote root. If this object is not persistent, then backward
735 * traversals of the mount point (i.e. "..") will not work if
736 * the nfsnode gets flushed out of the cache. Ufs does not have
737 * this problem, because one can identify root inodes by their
738 * number == ROOTINO (2). So, just unlock, but no rele.
739 */
740
741 nmp->nm_vnode = *vpp;
742 VOP_UNLOCK(*vpp, 0);
743
744 return (0);
745 bad:
746 nfs_disconnect(nmp);
747 free((caddr_t)nmp, M_NFSMNT);
748 m_freem(nam);
749 return (error);
750 }
751
752 /*
753 * unmount system call
754 */
755 int
756 nfs_unmount(mp, mntflags, p)
757 struct mount *mp;
758 int mntflags;
759 struct proc *p;
760 {
761 struct nfsmount *nmp;
762 struct vnode *vp;
763 int error, flags = 0;
764
765 if (mntflags & MNT_FORCE)
766 flags |= FORCECLOSE;
767 nmp = VFSTONFS(mp);
768 /*
769 * Goes something like this..
770 * - Check for activity on the root vnode (other than ourselves).
771 * - Call vflush() to clear out vnodes for this file system,
772 * except for the root vnode.
773 * - Decrement reference on the vnode representing remote root.
774 * - Close the socket
775 * - Free up the data structures
776 */
777 /*
778 * We need to decrement the ref. count on the nfsnode representing
779 * the remote root. See comment in mountnfs(). The VFS unmount()
780 * has done vput on this vnode, otherwise we would get deadlock!
781 */
782 vp = nmp->nm_vnode;
783 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
784 if (error != 0)
785 return error;
786
787 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
788 vput(vp);
789 return (EBUSY);
790 }
791
792 /*
793 * Must handshake with nqnfs_clientd() if it is active.
794 */
795 nmp->nm_iflag |= NFSMNT_DISMINPROG;
796 while (nmp->nm_inprog != NULLVP)
797 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
798 error = vflush(mp, vp, flags);
799 if (error) {
800 vput(vp);
801 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
802 return (error);
803 }
804
805 /*
806 * We are now committed to the unmount; mark the mount structure
807 * as doomed so that any sleepers kicked awake by nfs_disconnect
808 * will go away cleanly.
809 */
810 nmp->nm_iflag |= NFSMNT_DISMNT;
811
812 /*
813 * There are two reference counts to get rid of here
814 * (see comment in mountnfs()).
815 */
816 vrele(vp);
817 vput(vp);
818 vgone(vp);
819 nfs_disconnect(nmp);
820 m_freem(nmp->nm_nam);
821
822 /*
823 * For NQNFS, let the server daemon free the nfsmount structure.
824 */
825 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
826 free((caddr_t)nmp, M_NFSMNT);
827 return (0);
828 }
829
830 /*
831 * Return root of a filesystem
832 */
833 int
834 nfs_root(mp, vpp)
835 struct mount *mp;
836 struct vnode **vpp;
837 {
838 struct vnode *vp;
839 struct nfsmount *nmp;
840 int error;
841
842 nmp = VFSTONFS(mp);
843 vp = nmp->nm_vnode;
844 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
845 if (error != 0)
846 return error;
847 if (vp->v_type == VNON)
848 vp->v_type = VDIR;
849 vp->v_flag = VROOT;
850 *vpp = vp;
851 return (0);
852 }
853
854 extern int syncprt;
855
856 /*
857 * Flush out the buffer cache
858 */
859 /* ARGSUSED */
860 int
861 nfs_sync(mp, waitfor, cred, p)
862 struct mount *mp;
863 int waitfor;
864 struct ucred *cred;
865 struct proc *p;
866 {
867 struct vnode *vp;
868 int error, allerror = 0;
869
870 /*
871 * Force stale buffer cache information to be flushed.
872 */
873 loop:
874 for (vp = mp->mnt_vnodelist.lh_first;
875 vp != NULL;
876 vp = vp->v_mntvnodes.le_next) {
877 /*
878 * If the vnode that we are about to sync is no longer
879 * associated with this mount point, start over.
880 */
881 if (vp->v_mount != mp)
882 goto loop;
883 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
884 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
885 vp->v_uvm.u_obj.uo_npages == 0))
886 continue;
887 if (vget(vp, LK_EXCLUSIVE))
888 goto loop;
889 error = VOP_FSYNC(vp, cred,
890 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
891 if (error)
892 allerror = error;
893 vput(vp);
894 }
895 return (allerror);
896 }
897
898 /*
899 * NFS flat namespace lookup.
900 * Currently unsupported.
901 */
902 /* ARGSUSED */
903 int
904 nfs_vget(mp, ino, vpp)
905 struct mount *mp;
906 ino_t ino;
907 struct vnode **vpp;
908 {
909
910 return (EOPNOTSUPP);
911 }
912
913 /*
914 * Do that sysctl thang...
915 */
916 int
917 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
918 int *name;
919 u_int namelen;
920 void *oldp;
921 size_t *oldlenp;
922 void *newp;
923 size_t newlen;
924 struct proc *p;
925 {
926 int rv;
927
928 /*
929 * All names at this level are terminal.
930 */
931 if(namelen > 1)
932 return ENOTDIR; /* overloaded */
933
934 switch(name[0]) {
935 case NFS_NFSSTATS:
936 if(!oldp) {
937 *oldlenp = sizeof nfsstats;
938 return 0;
939 }
940
941 if(*oldlenp < sizeof nfsstats) {
942 *oldlenp = sizeof nfsstats;
943 return ENOMEM;
944 }
945
946 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
947 if(rv) return rv;
948
949 if(newp && newlen != sizeof nfsstats)
950 return EINVAL;
951
952 if(newp) {
953 return copyin(newp, &nfsstats, sizeof nfsstats);
954 }
955 return 0;
956
957 case NFS_IOTHREADS:
958 nfs_getset_niothreads(0);
959
960 rv = (sysctl_int(oldp, oldlenp, newp, newlen,
961 &nfs_niothreads));
962
963 if (newp)
964 nfs_getset_niothreads(1);
965
966 return rv;
967
968 default:
969 return EOPNOTSUPP;
970 }
971 }
972
973
974 /*
975 * At this point, this should never happen
976 */
977 /* ARGSUSED */
978 int
979 nfs_fhtovp(mp, fhp, vpp)
980 struct mount *mp;
981 struct fid *fhp;
982 struct vnode **vpp;
983 {
984
985 return (EINVAL);
986 }
987
988 /* ARGSUSED */
989 int
990 nfs_checkexp(mp, nam, exflagsp, credanonp)
991 struct mount *mp;
992 struct mbuf *nam;
993 int *exflagsp;
994 struct ucred **credanonp;
995 {
996
997 return (EINVAL);
998 }
999
1000 /*
1001 * Vnode pointer to File handle, should never happen either
1002 */
1003 /* ARGSUSED */
1004 int
1005 nfs_vptofh(vp, fhp)
1006 struct vnode *vp;
1007 struct fid *fhp;
1008 {
1009
1010 return (EINVAL);
1011 }
1012
1013 /*
1014 * Vfs start routine, a no-op.
1015 */
1016 /* ARGSUSED */
1017 int
1018 nfs_start(mp, flags, p)
1019 struct mount *mp;
1020 int flags;
1021 struct proc *p;
1022 {
1023
1024 return (0);
1025 }
1026
1027 /*
1028 * Do operations associated with quotas, not supported
1029 */
1030 /* ARGSUSED */
1031 int
1032 nfs_quotactl(mp, cmd, uid, arg, p)
1033 struct mount *mp;
1034 int cmd;
1035 uid_t uid;
1036 caddr_t arg;
1037 struct proc *p;
1038 {
1039
1040 return (EOPNOTSUPP);
1041 }
1042