nfs_vfsops.c revision 1.108 1 /* $NetBSD: nfs_vfsops.c,v 1.108 2001/09/15 20:36:40 chs 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/proc.h>
50 #include <sys/namei.h>
51 #include <sys/device.h>
52 #include <sys/vnode.h>
53 #include <sys/kernel.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/mbuf.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61
62 #include <net/if.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65
66 #include <nfs/rpcv2.h>
67 #include <nfs/nfsproto.h>
68 #include <nfs/nfsnode.h>
69 #include <nfs/nfs.h>
70 #include <nfs/nfsmount.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfs/nfsm_subs.h>
73 #include <nfs/nfsdiskless.h>
74 #include <nfs/nqnfs.h>
75 #include <nfs/nfs_var.h>
76
77 extern struct nfsstats nfsstats;
78 extern int nfs_ticks;
79
80 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
81 struct proc *));
82
83 /*
84 * nfs vfs operations.
85 */
86
87 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
88 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
89 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
90
91 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
92 &nfsv2_vnodeop_opv_desc,
93 &spec_nfsv2nodeop_opv_desc,
94 &fifo_nfsv2nodeop_opv_desc,
95 NULL,
96 };
97
98 struct vfsops nfs_vfsops = {
99 MOUNT_NFS,
100 nfs_mount,
101 nfs_start,
102 nfs_unmount,
103 nfs_root,
104 nfs_quotactl,
105 nfs_statfs,
106 nfs_sync,
107 nfs_vget,
108 nfs_fhtovp,
109 nfs_vptofh,
110 nfs_vfs_init,
111 nfs_vfs_reinit,
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; /* 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 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
590 return (EINVAL);
591 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
592 if (error)
593 return (error);
594 error = copyinstr(path, pth, MNAMELEN-1, &len);
595 if (error)
596 return (error);
597 memset(&pth[len], 0, MNAMELEN - len);
598 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
599 if (error)
600 return (error);
601 memset(&hst[len], 0, MNAMELEN - len);
602 /* sockargs() call must be after above copyin() calls */
603 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
604 if (error)
605 return (error);
606 args.fh = nfh;
607 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
608 return (error);
609 }
610
611 /*
612 * Common code for mount and mountroot
613 */
614 int
615 mountnfs(argp, mp, nam, pth, hst, vpp, p)
616 struct nfs_args *argp;
617 struct mount *mp;
618 struct mbuf *nam;
619 const char *pth, *hst;
620 struct vnode **vpp;
621 struct proc *p;
622 {
623 struct nfsmount *nmp;
624 struct nfsnode *np;
625 int error;
626 struct vattr attrs;
627 struct ucred *cr;
628
629 /*
630 * If the number of nfs iothreads to use has never
631 * been set, create a reasonable number of them.
632 */
633
634 if (nfs_niothreads < 0) {
635 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
636 nfs_getset_niothreads(TRUE);
637 }
638
639 if (mp->mnt_flag & MNT_UPDATE) {
640 nmp = VFSTONFS(mp);
641 /* update paths, file handles, etc, here XXX */
642 m_freem(nam);
643 return (0);
644 } else {
645 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
646 M_NFSMNT, M_WAITOK);
647 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
648 mp->mnt_data = (qaddr_t)nmp;
649 TAILQ_INIT(&nmp->nm_uidlruhead);
650 TAILQ_INIT(&nmp->nm_bufq);
651 }
652 vfs_getnewfsid(mp);
653 nmp->nm_mountp = mp;
654
655 #ifndef NFS_V2_ONLY
656 if (argp->flags & NFSMNT_NQNFS)
657 /*
658 * We have to set mnt_maxsymlink to a non-zero value so
659 * that COMPAT_43 routines will know that we are setting
660 * the d_type field in directories (and can zero it for
661 * unsuspecting binaries).
662 */
663 mp->mnt_maxsymlinklen = 1;
664 #endif
665
666 #ifndef NFS_V2_ONLY
667 if ((argp->flags & NFSMNT_NFSV3) == 0)
668 #endif
669 /*
670 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
671 * will fill this in.
672 */
673 nmp->nm_maxfilesize = 0xffffffffLL;
674
675 nmp->nm_timeo = NFS_TIMEO;
676 nmp->nm_retry = NFS_RETRANS;
677 nmp->nm_wsize = NFS_WSIZE;
678 nmp->nm_rsize = NFS_RSIZE;
679 nmp->nm_readdirsize = NFS_READDIRSIZE;
680 nmp->nm_numgrps = NFS_MAXGRPS;
681 nmp->nm_readahead = NFS_DEFRAHEAD;
682 nmp->nm_leaseterm = NQ_DEFLEASE;
683 nmp->nm_deadthresh = NQ_DEADTHRESH;
684 CIRCLEQ_INIT(&nmp->nm_timerhead);
685 nmp->nm_inprog = NULLVP;
686 #ifdef COMPAT_09
687 mp->mnt_stat.f_type = 2;
688 #else
689 mp->mnt_stat.f_type = 0;
690 #endif
691 mp->mnt_fs_bshift = DEV_BSHIFT;
692 mp->mnt_dev_bshift = DEV_BSHIFT;
693 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name,
694 MFSNAMELEN);
695 memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
696 memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
697 nmp->nm_nam = nam;
698
699 /* Set up the sockets and per-host congestion */
700 nmp->nm_sotype = argp->sotype;
701 nmp->nm_soproto = argp->proto;
702
703 nfs_decode_args(nmp, argp);
704
705 /*
706 * For Connection based sockets (TCP,...) defer the connect until
707 * the first request, in case the server is not responding.
708 */
709 if (nmp->nm_sotype == SOCK_DGRAM &&
710 (error = nfs_connect(nmp, (struct nfsreq *)0)))
711 goto bad;
712
713 /*
714 * This is silly, but it has to be set so that vinifod() works.
715 * We do not want to do an nfs_statfs() here since we can get
716 * stuck on a dead server and we are holding a lock on the mount
717 * point.
718 */
719 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
720 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
721 if (error)
722 goto bad;
723 *vpp = NFSTOV(np);
724 VOP_GETATTR(*vpp, &attrs, p->p_ucred, p);
725 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
726 cr = crget();
727 cr->cr_uid = attrs.va_uid;
728 cr->cr_gid = attrs.va_gid;
729 cr->cr_ngroups = 0;
730 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
731 crfree(cr);
732 }
733
734 /*
735 * A reference count is needed on the nfsnode representing the
736 * remote root. If this object is not persistent, then backward
737 * traversals of the mount point (i.e. "..") will not work if
738 * the nfsnode gets flushed out of the cache. Ufs does not have
739 * this problem, because one can identify root inodes by their
740 * number == ROOTINO (2). So, just unlock, but no rele.
741 */
742
743 nmp->nm_vnode = *vpp;
744 VOP_UNLOCK(*vpp, 0);
745
746 return (0);
747 bad:
748 nfs_disconnect(nmp);
749 free((caddr_t)nmp, M_NFSMNT);
750 m_freem(nam);
751 return (error);
752 }
753
754 /*
755 * unmount system call
756 */
757 int
758 nfs_unmount(mp, mntflags, p)
759 struct mount *mp;
760 int mntflags;
761 struct proc *p;
762 {
763 struct nfsmount *nmp;
764 struct vnode *vp;
765 int error, flags = 0;
766
767 if (mntflags & MNT_FORCE)
768 flags |= FORCECLOSE;
769 nmp = VFSTONFS(mp);
770 /*
771 * Goes something like this..
772 * - Check for activity on the root vnode (other than ourselves).
773 * - Call vflush() to clear out vnodes for this file system,
774 * except for the root vnode.
775 * - Decrement reference on the vnode representing remote root.
776 * - Close the socket
777 * - Free up the data structures
778 */
779 /*
780 * We need to decrement the ref. count on the nfsnode representing
781 * the remote root. See comment in mountnfs(). The VFS unmount()
782 * has done vput on this vnode, otherwise we would get deadlock!
783 */
784 vp = nmp->nm_vnode;
785 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
786 if (error != 0)
787 return error;
788
789 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
790 vput(vp);
791 return (EBUSY);
792 }
793
794 /*
795 * Must handshake with nqnfs_clientd() if it is active.
796 */
797 nmp->nm_iflag |= NFSMNT_DISMINPROG;
798 while (nmp->nm_inprog != NULLVP)
799 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
800 error = vflush(mp, vp, flags);
801 if (error) {
802 vput(vp);
803 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
804 return (error);
805 }
806
807 /*
808 * We are now committed to the unmount; mark the mount structure
809 * as doomed so that any sleepers kicked awake by nfs_disconnect
810 * will go away cleanly.
811 */
812 nmp->nm_iflag |= NFSMNT_DISMNT;
813
814 /*
815 * There are two reference counts to get rid of here
816 * (see comment in mountnfs()).
817 */
818 vrele(vp);
819 vput(vp);
820 vgone(vp);
821 nfs_disconnect(nmp);
822 m_freem(nmp->nm_nam);
823
824 /*
825 * For NQNFS, let the server daemon free the nfsmount structure.
826 */
827 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
828 free((caddr_t)nmp, M_NFSMNT);
829 return (0);
830 }
831
832 /*
833 * Return root of a filesystem
834 */
835 int
836 nfs_root(mp, vpp)
837 struct mount *mp;
838 struct vnode **vpp;
839 {
840 struct vnode *vp;
841 struct nfsmount *nmp;
842 int error;
843
844 nmp = VFSTONFS(mp);
845 vp = nmp->nm_vnode;
846 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
847 if (error != 0)
848 return error;
849 if (vp->v_type == VNON)
850 vp->v_type = VDIR;
851 vp->v_flag = VROOT;
852 *vpp = vp;
853 return (0);
854 }
855
856 extern int syncprt;
857
858 /*
859 * Flush out the buffer cache
860 */
861 /* ARGSUSED */
862 int
863 nfs_sync(mp, waitfor, cred, p)
864 struct mount *mp;
865 int waitfor;
866 struct ucred *cred;
867 struct proc *p;
868 {
869 struct vnode *vp;
870 int error, allerror = 0;
871
872 /*
873 * Force stale buffer cache information to be flushed.
874 */
875 loop:
876 for (vp = mp->mnt_vnodelist.lh_first;
877 vp != NULL;
878 vp = vp->v_mntvnodes.le_next) {
879 /*
880 * If the vnode that we are about to sync is no longer
881 * associated with this mount point, start over.
882 */
883 if (vp->v_mount != mp)
884 goto loop;
885 if (waitfor == MNT_LAZY ||
886 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
887 vp->v_uobj.uo_npages == 0))
888 continue;
889 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT))
890 goto loop;
891 error = VOP_FSYNC(vp, cred,
892 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
893 if (error)
894 allerror = error;
895 vput(vp);
896 }
897 return (allerror);
898 }
899
900 /*
901 * NFS flat namespace lookup.
902 * Currently unsupported.
903 */
904 /* ARGSUSED */
905 int
906 nfs_vget(mp, ino, vpp)
907 struct mount *mp;
908 ino_t ino;
909 struct vnode **vpp;
910 {
911
912 return (EOPNOTSUPP);
913 }
914
915 /*
916 * Do that sysctl thang...
917 */
918 int
919 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
920 int *name;
921 u_int namelen;
922 void *oldp;
923 size_t *oldlenp;
924 void *newp;
925 size_t newlen;
926 struct proc *p;
927 {
928 int rv;
929
930 /*
931 * All names at this level are terminal.
932 */
933 if(namelen > 1)
934 return ENOTDIR; /* overloaded */
935
936 switch(name[0]) {
937 case NFS_NFSSTATS:
938 if(!oldp) {
939 *oldlenp = sizeof nfsstats;
940 return 0;
941 }
942
943 if(*oldlenp < sizeof nfsstats) {
944 *oldlenp = sizeof nfsstats;
945 return ENOMEM;
946 }
947
948 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
949 if(rv) return rv;
950
951 if(newp && newlen != sizeof nfsstats)
952 return EINVAL;
953
954 if(newp) {
955 return copyin(newp, &nfsstats, sizeof nfsstats);
956 }
957 return 0;
958
959 case NFS_IOTHREADS:
960 nfs_getset_niothreads(0);
961
962 rv = (sysctl_int(oldp, oldlenp, newp, newlen,
963 &nfs_niothreads));
964
965 if (newp)
966 nfs_getset_niothreads(1);
967
968 return rv;
969
970 default:
971 return EOPNOTSUPP;
972 }
973 }
974
975
976 /*
977 * At this point, this should never happen
978 */
979 /* ARGSUSED */
980 int
981 nfs_fhtovp(mp, fhp, vpp)
982 struct mount *mp;
983 struct fid *fhp;
984 struct vnode **vpp;
985 {
986
987 return (EINVAL);
988 }
989
990 /* ARGSUSED */
991 int
992 nfs_checkexp(mp, nam, exflagsp, credanonp)
993 struct mount *mp;
994 struct mbuf *nam;
995 int *exflagsp;
996 struct ucred **credanonp;
997 {
998
999 return (EINVAL);
1000 }
1001
1002 /*
1003 * Vnode pointer to File handle, should never happen either
1004 */
1005 /* ARGSUSED */
1006 int
1007 nfs_vptofh(vp, fhp)
1008 struct vnode *vp;
1009 struct fid *fhp;
1010 {
1011
1012 return (EINVAL);
1013 }
1014
1015 /*
1016 * Vfs start routine, a no-op.
1017 */
1018 /* ARGSUSED */
1019 int
1020 nfs_start(mp, flags, p)
1021 struct mount *mp;
1022 int flags;
1023 struct proc *p;
1024 {
1025
1026 return (0);
1027 }
1028
1029 /*
1030 * Do operations associated with quotas, not supported
1031 */
1032 /* ARGSUSED */
1033 int
1034 nfs_quotactl(mp, cmd, uid, arg, p)
1035 struct mount *mp;
1036 int cmd;
1037 uid_t uid;
1038 caddr_t arg;
1039 struct proc *p;
1040 {
1041
1042 return (EOPNOTSUPP);
1043 }
1044