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