]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vfsops.c
8caffeca53b0fd9fd17a4c322a40a02af0ef75d6
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
59 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
60 */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/conf.h>
65 #include <sys/ioctl.h>
66 #include <sys/signal.h>
67 #include <sys/proc.h>
68 #include <sys/namei.h>
69 #include <sys/vnode.h>
70 #include <sys/malloc.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/mount.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77
78 #include <sys/vm.h>
79 #include <sys/vmparam.h>
80
81 #if !defined(NO_MOUNT_PRIVATE)
82 #include <sys/filedesc.h>
83 #endif /* NO_MOUNT_PRIVATE */
84
85 #include <net/if.h>
86 #include <net/route.h>
87 #include <netinet/in.h>
88
89 #include <nfs/rpcv2.h>
90 #include <nfs/nfsproto.h>
91 #include <nfs/nfs.h>
92 #include <nfs/nfsnode.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/xdr_subs.h>
95 #include <nfs/nfsm_subs.h>
96 #include <nfs/nfsdiskless.h>
97 #include <nfs/nqnfs.h>
98
99 extern int nfs_mountroot __P((void));
100
101 extern int nfs_ticks;
102 extern int nfs_mount_type;
103 extern int nfs_resv_mounts;
104
105 struct nfsstats nfsstats;
106 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
107 struct proc *);
108 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
109 #ifdef notyet
110 SYSCTL_NODE(_vfs, MOUNT_NFS, nfs, CTLFLAG_RW, 0, "NFS filesystem");
111 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
112 &nfsstats, nfsstats, "");
113 #endif
114 #if NFSDIAG
115 int nfs_debug;
116 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
117 #ifdef notyet
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
119 #endif
120 #endif
121
122 SYSCTL_DECL(_vfs_generic_nfs);
123 SYSCTL_NODE(_vfs_generic_nfs, OID_AUTO, client, CTLFLAG_RW, 0,
124 "nfs client hinge");
125 /* how long NFS will wait before signalling vfs that it's down. */
126 static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
127 SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_INITIAL_DELAY,
128 initialdowndelay, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
129 /* how long between console messages "nfs server foo not responding" */
130 static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
131 SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_DELAY,
132 nextdowndelay, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
133
134 static int nfs_iosize __P((struct nfsmount *nmp));
135 static int mountnfs __P((struct nfs_args *,struct mount *,
136 struct mbuf *,char *,char *,struct vnode **));
137 static int nfs_mount __P(( struct mount *mp, char *path, caddr_t data,
138 struct nameidata *ndp, struct proc *p));
139 static int nfs_start __P(( struct mount *mp, int flags,
140 struct proc *p));
141 static int nfs_unmount __P(( struct mount *mp, int mntflags,
142 struct proc *p));
143 static int nfs_root __P(( struct mount *mp, struct vnode **vpp));
144 static int nfs_quotactl __P(( struct mount *mp, int cmds, uid_t uid,
145 caddr_t arg, struct proc *p));
146 static int nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
147 struct proc *p));
148 static int nfs_sync __P(( struct mount *mp, int waitfor,
149 struct ucred *cred, struct proc *p));
150 static int nfs_vptofh __P(( struct vnode *vp, struct fid *fhp));
151 static int nfs_fhtovp __P((struct mount *mp, struct fid *fhp,
152 struct mbuf *nam, struct vnode **vpp,
153 int *exflagsp, struct ucred **credanonp));
154 static int nfs_vget __P((struct mount *, void *, struct vnode **));
155
156
157 /*
158 * nfs vfs operations.
159 */
160 struct vfsops nfs_vfsops = {
161 nfs_mount,
162 nfs_start,
163 nfs_unmount,
164 nfs_root,
165 nfs_quotactl,
166 nfs_statfs,
167 nfs_sync,
168 nfs_vget,
169 nfs_fhtovp,
170 nfs_vptofh,
171 nfs_init,
172 nfs_sysctl
173 };
174 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
175 #ifdef notyet
176 VFS_SET(nfs_vfsops, nfs, MOUNT_NFS, VFCF_NETWORK);
177 #endif
178
179
180 void nfsargs_ntoh __P((struct nfs_args *));
181 static int
182 nfs_mount_diskless __P((struct nfs_dlmount *, char *, int, struct vnode **,
183 struct mount **));
184 #if !defined(NO_MOUNT_PRIVATE)
185 static int
186 nfs_mount_diskless_private __P((struct nfs_dlmount *, char *, int,
187 struct vnode **, struct mount **));
188 #endif /* NO_MOUNT_PRIVATE */
189 static void nfs_convert_oargs __P((struct nfs_args *args,
190 struct onfs_args *oargs));
191 #if NFSDIAG
192 int nfsreqqusers = 0;
193 extern int nfsbtlen, nfsbtcpu, nfsbtthread, nfsbt[32];
194 #endif
195
196 static int nfs_iosize(nmp)
197 struct nfsmount* nmp;
198 {
199 int iosize;
200
201 /*
202 * Calculate the size used for io buffers. Use the larger
203 * of the two sizes to minimise nfs requests but make sure
204 * that it is at least one VM page to avoid wasting buffer
205 * space and to allow easy mmapping of I/O buffers.
206 * The read/write rpc calls handle the splitting up of
207 * buffers into multiple requests if the buffer size is
208 * larger than the I/O size.
209 */
210 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
211 if (iosize < PAGE_SIZE)
212 iosize = PAGE_SIZE;
213 return (trunc_page_32(iosize));
214 }
215
216 static void nfs_convert_oargs(args,oargs)
217 struct nfs_args *args;
218 struct onfs_args *oargs;
219 {
220 args->version = NFS_ARGSVERSION;
221 args->addr = oargs->addr;
222 args->addrlen = oargs->addrlen;
223 args->sotype = oargs->sotype;
224 args->proto = oargs->proto;
225 args->fh = oargs->fh;
226 args->fhsize = oargs->fhsize;
227 args->flags = oargs->flags;
228 args->wsize = oargs->wsize;
229 args->rsize = oargs->rsize;
230 args->readdirsize = oargs->readdirsize;
231 args->timeo = oargs->timeo;
232 args->retrans = oargs->retrans;
233 args->maxgrouplist = oargs->maxgrouplist;
234 args->readahead = oargs->readahead;
235 args->leaseterm = oargs->leaseterm;
236 args->deadthresh = oargs->deadthresh;
237 args->hostname = oargs->hostname;
238 }
239
240 /*
241 * nfs statfs call
242 */
243 int
244 nfs_statfs(mp, sbp, p)
245 struct mount *mp;
246 register struct statfs *sbp;
247 struct proc *p;
248 {
249 register struct vnode *vp;
250 register struct nfs_statfs *sfp;
251 register caddr_t cp;
252 register u_long *tl;
253 register long t1, t2;
254 caddr_t bpos, dpos, cp2;
255 struct nfsmount *nmp = VFSTONFS(mp);
256 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
257 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
258 struct ucred *cred;
259 extern int nfs_mount_type;
260 u_int64_t xid;
261
262 #ifndef nolint
263 sfp = (struct nfs_statfs *)0;
264 #endif
265 vp = nmp->nm_dvp;
266 if (error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p))
267 return(error);
268 cred = crget();
269 cred->cr_ngroups = 1;
270 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
271 (void)nfs_fsinfo(nmp, vp, cred, p);
272 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
273 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
274 nfsm_fhtom(vp, v3);
275 nfsm_request(vp, NFSPROC_FSSTAT, p, cred, &xid);
276 if (v3 && mrep)
277 nfsm_postop_attr(vp, retattr, &xid);
278 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
279
280 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
281 #ifdef notyet
282 sbp->f_type = MOUNT_NFS;
283 #else
284 sbp->f_type = nfs_mount_type;
285 #endif
286 sbp->f_flags = nmp->nm_flag;
287 sbp->f_iosize = nfs_iosize(nmp);
288 if (v3) {
289 /*
290 * Adjust block size to get total block count to fit in a long.
291 * If we can't increase block size enough, clamp to max long.
292 */
293 u_quad_t tquad, tquad2, bsize;
294 bsize = NFS_FABLKSIZE;
295
296 fxdr_hyper(&sfp->sf_tbytes, &tquad);
297 tquad /= bsize;
298 while ((tquad & ~0x7fffffff) && (bsize < 0x40000000)) {
299 bsize <<= 1;
300 tquad >>= 1;
301 }
302 sbp->f_blocks = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
303
304 fxdr_hyper(&sfp->sf_fbytes, &tquad);
305 tquad /= bsize;
306 sbp->f_bfree = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
307
308 fxdr_hyper(&sfp->sf_abytes, &tquad);
309 tquad /= bsize;
310 sbp->f_bavail = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
311
312 sbp->f_bsize = (long)bsize;
313
314 /* adjust file slots too... */
315 fxdr_hyper(&sfp->sf_tfiles, &tquad);
316 fxdr_hyper(&sfp->sf_ffiles, &tquad2);
317 while (tquad & ~0x7fffffff) {
318 tquad >>= 1;
319 tquad2 >>= 1;
320 }
321 sbp->f_files = tquad;
322 sbp->f_ffree = tquad2;
323 } else {
324 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
325 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
326 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
327 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
328 sbp->f_files = 0;
329 sbp->f_ffree = 0;
330 }
331 if (sbp != &mp->mnt_stat) {
332 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
333 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
334 }
335 nfsm_reqdone;
336 VOP_UNLOCK(vp, 0, p);
337 crfree(cred);
338 return (error);
339 }
340
341 /*
342 * nfs version 3 fsinfo rpc call
343 */
344 int
345 nfs_fsinfo(nmp, vp, cred, p)
346 register struct nfsmount *nmp;
347 register struct vnode *vp;
348 struct ucred *cred;
349 struct proc *p;
350 {
351 register struct nfsv3_fsinfo *fsp;
352 register caddr_t cp;
353 register long t1, t2;
354 register u_long *tl, pref, max;
355 caddr_t bpos, dpos, cp2;
356 int error = 0, retattr;
357 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
358 u_int64_t xid;
359
360 nfsstats.rpccnt[NFSPROC_FSINFO]++;
361 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
362 nfsm_fhtom(vp, 1);
363 nfsm_request(vp, NFSPROC_FSINFO, p, cred, &xid);
364 if (mrep) {
365 nfsm_postop_attr(vp, retattr, &xid);
366 }
367 if (!error) {
368 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
369 pref = fxdr_unsigned(u_long, fsp->fs_wtpref);
370 if (pref < nmp->nm_wsize)
371 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
372 ~(NFS_FABLKSIZE - 1);
373 max = fxdr_unsigned(u_long, fsp->fs_wtmax);
374 if (max < nmp->nm_wsize) {
375 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
376 if (nmp->nm_wsize == 0)
377 nmp->nm_wsize = max;
378 }
379 pref = fxdr_unsigned(u_long, fsp->fs_rtpref);
380 if (pref < nmp->nm_rsize)
381 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
382 ~(NFS_FABLKSIZE - 1);
383 max = fxdr_unsigned(u_long, fsp->fs_rtmax);
384 if (max < nmp->nm_rsize) {
385 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
386 if (nmp->nm_rsize == 0)
387 nmp->nm_rsize = max;
388 }
389 pref = fxdr_unsigned(u_long, fsp->fs_dtpref);
390 if (pref < nmp->nm_readdirsize)
391 nmp->nm_readdirsize = pref;
392 if (max < nmp->nm_readdirsize) {
393 nmp->nm_readdirsize = max;
394 }
395 nmp->nm_state |= NFSSTA_GOTFSINFO;
396 }
397 nfsm_reqdone;
398 return (error);
399 }
400
401 /*
402 * Mount a remote root fs via. nfs. This depends on the info in the
403 * nfs_diskless structure that has been filled in properly by some primary
404 * bootstrap.
405 * It goes something like this:
406 * - do enough of "ifconfig" by calling ifioctl() so that the system
407 * can talk to the server
408 * - If nfs_diskless.mygateway is filled in, use that address as
409 * a default gateway.
410 * - hand craft the swap nfs vnode hanging off a fake mount point
411 * if swdevt[0].sw_dev == NODEV
412 * - build the rootfs mount point and call mountnfs() to do the rest.
413 */
414 int
415 nfs_mountroot()
416 {
417 struct nfs_diskless nd;
418 struct vattr attr;
419 struct mount *mp;
420 struct vnode *vp;
421 struct proc *procp;
422 long n;
423 int error;
424 #if !defined(NO_MOUNT_PRIVATE)
425 struct mount *mppriv;
426 struct vnode *vppriv;
427 #endif /* NO_MOUNT_PRIVATE */
428 int v3;
429
430 procp = current_proc(); /* XXX */
431
432 /*
433 * Call nfs_boot_init() to fill in the nfs_diskless struct.
434 * Note: networking must already have been configured before
435 * we're called.
436 */
437 bzero((caddr_t) &nd, sizeof(nd));
438 error = nfs_boot_init(&nd, procp);
439 if (error) {
440 panic("nfs_boot_init failed with %d\n", error);
441 }
442
443 /* try NFSv3 first, if that fails then try NFSv2 */
444 v3 = 1;
445
446 tryagain:
447 error = nfs_boot_getfh(&nd, procp, v3);
448 if (error) {
449 if (error == EHOSTDOWN || error == EHOSTUNREACH) {
450 if (nd.nd_root.ndm_path)
451 FREE_ZONE(nd.nd_root.ndm_path,
452 MAXPATHLEN, M_NAMEI);
453 if (nd.nd_private.ndm_path)
454 FREE_ZONE(nd.nd_private.ndm_path,
455 MAXPATHLEN, M_NAMEI);
456 return (error);
457 }
458 if (v3) {
459 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error);
460 v3 = 0;
461 goto tryagain;
462 }
463 panic("nfs_boot_getfh(v2) failed with %d\n", error);
464 }
465
466 /*
467 * Create the root mount point.
468 */
469 #if !defined(NO_MOUNT_PRIVATE)
470 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_RDONLY, &vp, &mp))) {
471 #else
472 if (error = nfs_mount_diskless(&nd.nd_root, "/", NULL, &vp, &mp)) {
473 #endif /* NO_MOUNT_PRIVATE */
474 if (v3) {
475 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error);
476 v3 = 0;
477 goto tryagain;
478 }
479 panic("nfs_mount_diskless root failed with %d\n", error);
480 }
481 printf("root on %s\n", (char *)&nd.nd_root.ndm_host);
482
483 simple_lock(&mountlist_slock);
484 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
485 simple_unlock(&mountlist_slock);
486 vfs_unbusy(mp, procp);
487 rootvp = vp;
488
489 #if !defined(NO_MOUNT_PRIVATE)
490 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
491 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
492 NULL, &vppriv, &mppriv);
493 if (error) {
494 panic("nfs_mount_diskless private failed with %d\n", error);
495 }
496 printf("private on %s\n", (char *)&nd.nd_private.ndm_host);
497
498 simple_lock(&mountlist_slock);
499 CIRCLEQ_INSERT_TAIL(&mountlist, mppriv, mnt_list);
500 simple_unlock(&mountlist_slock);
501 vfs_unbusy(mppriv, procp);
502 }
503
504 #endif /* NO_MOUNT_PRIVATE */
505
506 if (nd.nd_root.ndm_path)
507 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
508 if (nd.nd_private.ndm_path)
509 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
510
511 /* Get root attributes (for the time). */
512 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
513 if (error) panic("nfs_mountroot: getattr for root");
514 n = attr.va_mtime.tv_sec;
515 inittodr(n);
516 return (0);
517 }
518
519 /*
520 * Internal version of mount system call for diskless setup.
521 */
522 static int
523 nfs_mount_diskless(ndmntp, mntname, mntflag, vpp, mpp)
524 struct nfs_dlmount *ndmntp;
525 char *mntname;
526 int mntflag;
527 struct vnode **vpp;
528 struct mount **mpp;
529 {
530 struct nfs_args args;
531 struct mount *mp;
532 struct mbuf *m;
533 int error;
534 struct proc *procp;
535
536 procp = current_proc(); /* XXX */
537
538 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_host, &mp))) {
539 printf("nfs_mountroot: NFS not configured");
540 return (error);
541 }
542 mp->mnt_flag = mntflag;
543
544 /* Initialize mount args. */
545 bzero((caddr_t) &args, sizeof(args));
546 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
547 args.addrlen = args.addr->sa_len;
548 args.sotype = SOCK_DGRAM;
549 args.fh = ndmntp->ndm_fh;
550 args.fhsize = ndmntp->ndm_fhlen;
551 args.hostname = ndmntp->ndm_host;
552 args.flags = NFSMNT_RESVPORT;
553 if (ndmntp->ndm_nfsv3)
554 args.flags |= NFSMNT_NFSV3;
555
556 MGET(m, M_DONTWAIT, MT_SONAME);
557 bcopy((caddr_t)args.addr, mtod(m, caddr_t),
558 (m->m_len = args.addr->sa_len));
559 if ((error = mountnfs(&args, mp, m, mntname, args.hostname, vpp))) {
560 printf("nfs_mountroot: mount %s failed: %d", mntname, error);
561 mp->mnt_vfc->vfc_refcount--;
562
563 if (mp->mnt_kern_flag & MNTK_IO_XINFO)
564 FREE(mp->mnt_xinfo_ptr, M_TEMP);
565 vfs_unbusy(mp, procp);
566
567 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
568 return (error);
569 }
570 #if 0 /* Causes incorrect reporting of "mounted on" */
571 (void) copystr(args.hostname, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
572 #endif /* 0 */
573 *mpp = mp;
574 return (0);
575 }
576
577 #if !defined(NO_MOUNT_PRIVATE)
578 /*
579 * Internal version of mount system call to mount "/private"
580 * separately in diskless setup
581 */
582 static int
583 nfs_mount_diskless_private(ndmntp, mntname, mntflag, vpp, mpp)
584 struct nfs_dlmount *ndmntp;
585 char *mntname;
586 int mntflag;
587 struct vnode **vpp;
588 struct mount **mpp;
589 {
590 struct nfs_args args;
591 struct mount *mp;
592 struct mbuf *m;
593 int error;
594 struct proc *procp;
595 struct vfsconf *vfsp;
596 struct nameidata nd;
597 struct vnode *vp;
598
599 procp = current_proc(); /* XXX */
600
601 {
602 /*
603 * mimic main()!. Temporarily set up rootvnode and other stuff so
604 * that namei works. Need to undo this because main() does it, too
605 */
606 struct filedesc *fdp; /* pointer to file descriptor state */
607 fdp = procp->p_fd;
608 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
609
610 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
611 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
612 panic("cannot find root vnode");
613 VREF(rootvnode);
614 fdp->fd_cdir = rootvnode;
615 VOP_UNLOCK(rootvnode, 0, procp);
616 fdp->fd_rdir = NULL;
617 }
618
619 /*
620 * Get vnode to be covered
621 */
622 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
623 mntname, procp);
624 if ((error = namei(&nd))) {
625 printf("nfs_mountroot: private namei failed!");
626 return (error);
627 }
628 {
629 /* undo VREF in mimic main()! */
630 vrele(rootvnode);
631 }
632 vp = nd.ni_vp;
633 if ((error = vinvalbuf(vp, V_SAVE, procp->p_ucred, procp, 0, 0))) {
634 vput(vp);
635 return (error);
636 }
637 if (vp->v_type != VDIR) {
638 vput(vp);
639 return (ENOTDIR);
640 }
641 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
642 if (!strcmp(vfsp->vfc_name, "nfs"))
643 break;
644 if (vfsp == NULL) {
645 printf("nfs_mountroot: private NFS not configured");
646 vput(vp);
647 return (ENODEV);
648 }
649 if (vp->v_mountedhere != NULL) {
650 vput(vp);
651 return (EBUSY);
652 }
653
654 /*
655 * Allocate and initialize the filesystem.
656 */
657 mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
658 bzero((char *)mp, (u_long)sizeof(struct mount));
659
660 /* Initialize the default IO constraints */
661 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
662 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
663
664 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
665 (void)vfs_busy(mp, LK_NOWAIT, 0, procp);
666 LIST_INIT(&mp->mnt_vnodelist);
667 mp->mnt_op = vfsp->vfc_vfsops;
668 mp->mnt_vfc = vfsp;
669 vfsp->vfc_refcount++;
670 mp->mnt_stat.f_type = vfsp->vfc_typenum;
671 mp->mnt_flag = mntflag;
672 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
673 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
674 vp->v_mountedhere = mp;
675 mp->mnt_vnodecovered = vp;
676 mp->mnt_stat.f_owner = procp->p_ucred->cr_uid;
677 (void) copystr(mntname, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
678 (void) copystr(ndmntp->ndm_host, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
679
680 /* Initialize mount args. */
681 bzero((caddr_t) &args, sizeof(args));
682 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
683 args.addrlen = args.addr->sa_len;
684 args.sotype = SOCK_DGRAM;
685 args.fh = ndmntp->ndm_fh;
686 args.fhsize = ndmntp->ndm_fhlen;
687 args.hostname = ndmntp->ndm_host;
688 args.flags = NFSMNT_RESVPORT;
689 if (ndmntp->ndm_nfsv3)
690 args.flags |= NFSMNT_NFSV3;
691
692 MGET(m, M_DONTWAIT, MT_SONAME);
693 bcopy((caddr_t)args.addr, mtod(m, caddr_t),
694 (m->m_len = args.addr->sa_len));
695 if ((error = mountnfs(&args, mp, m, mntname, args.hostname, &vp))) {
696 printf("nfs_mountroot: mount %s failed: %d", mntname, error);
697 mp->mnt_vfc->vfc_refcount--;
698
699 if (mp->mnt_kern_flag & MNTK_IO_XINFO)
700 FREE(mp->mnt_xinfo_ptr, M_TEMP);
701 vfs_unbusy(mp, procp);
702
703 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
704 return (error);
705 }
706
707 *mpp = mp;
708 *vpp = vp;
709 return (0);
710 }
711 #endif /* NO_MOUNT_PRIVATE */
712
713 /*
714 * VFS Operations.
715 *
716 * mount system call
717 * It seems a bit dumb to copyinstr() the host and path here and then
718 * bcopy() them in mountnfs(), but I wanted to detect errors before
719 * doing the sockargs() call because sockargs() allocates an mbuf and
720 * an error after that means that I have to release the mbuf.
721 */
722 /* ARGSUSED */
723 static int
724 nfs_mount(mp, path, data, ndp, p)
725 struct mount *mp;
726 char *path;
727 caddr_t data;
728 struct nameidata *ndp;
729 struct proc *p;
730 {
731 int error;
732 struct nfs_args args;
733 struct mbuf *nam;
734 struct vnode *vp;
735 char pth[MNAMELEN], hst[MNAMELEN];
736 size_t len;
737 u_char nfh[NFSX_V3FHMAX];
738
739 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
740 if (error)
741 return (error);
742 if (args.version != NFS_ARGSVERSION) {
743 #ifndef NO_COMPAT_PRELITE2
744 /*
745 * If the argument version is unknown, then assume the
746 * caller is a pre-lite2 4.4BSD client and convert its
747 * arguments.
748 */
749 struct onfs_args oargs;
750 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
751 if (error)
752 return (error);
753 nfs_convert_oargs(&args,&oargs);
754 #else /* NO_COMPAT_PRELITE2 */
755 return (EPROGMISMATCH);
756 #endif /* !NO_COMPAT_PRELITE2 */
757 }
758 if (args.fhsize > NFSX_V3FHMAX)
759 return (EINVAL);
760 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
761 if (error)
762 return (error);
763 error = copyinstr(path, pth, MNAMELEN-1, &len);
764 if (error)
765 return (error);
766 bzero(&pth[len], MNAMELEN - len);
767 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
768 if (error)
769 return (error);
770 bzero(&hst[len], MNAMELEN - len);
771 /* sockargs() call must be after above copyin() calls */
772 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
773 if (error)
774 return (error);
775 args.fh = nfh;
776 error = mountnfs(&args, mp, nam, pth, hst, &vp);
777 return (error);
778 }
779
780 /*
781 * Common code for mount and mountroot
782 */
783 static int
784 mountnfs(argp, mp, nam, pth, hst, vpp)
785 register struct nfs_args *argp;
786 register struct mount *mp;
787 struct mbuf *nam;
788 char *pth, *hst;
789 struct vnode **vpp;
790 {
791 register struct nfsmount *nmp;
792 struct nfsnode *np;
793 int error, maxio;
794 struct vattr attrs;
795 struct proc *curproc;
796
797 /*
798 * turning off NQNFS until we have further testing
799 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
800 * Those have NQNFS defined out in conjunction with this
801 * returning an error. Remove when fully tested.
802 */
803 if (argp->flags & NFSMNT_NQNFS) {
804 error = NFSERR_NOTSUPP;
805 goto bad2;
806 }
807
808 /*
809 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
810 * no sense in that context.
811 */
812 if (argp->sotype == SOCK_STREAM)
813 argp->flags &= ~NFSMNT_NOCONN;
814
815 if (mp->mnt_flag & MNT_UPDATE) {
816 nmp = VFSTONFS(mp);
817 /* update paths, file handles, etc, here XXX */
818 m_freem(nam);
819 return (0);
820 } else {
821 MALLOC_ZONE(nmp, struct nfsmount *,
822 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
823 bzero((caddr_t)nmp, sizeof (struct nfsmount));
824 TAILQ_INIT(&nmp->nm_uidlruhead);
825 TAILQ_INIT(&nmp->nm_bufq);
826 mp->mnt_data = (qaddr_t)nmp;
827 }
828 vfs_getnewfsid(mp);
829 nmp->nm_mountp = mp;
830 nmp->nm_flag = argp->flags;
831 if (nmp->nm_flag & NFSMNT_NQNFS)
832 /*
833 * We have to set mnt_maxsymlink to a non-zero value so
834 * that COMPAT_43 routines will know that we are setting
835 * the d_type field in directories (and can zero it for
836 * unsuspecting binaries).
837 */
838 mp->mnt_maxsymlinklen = 1;
839 nmp->nm_timeo = NFS_TIMEO;
840 nmp->nm_retry = NFS_RETRANS;
841 if (argp->sotype == SOCK_DGRAM) {
842 nmp->nm_wsize = NFS_DGRAM_WSIZE;
843 nmp->nm_rsize = NFS_DGRAM_RSIZE;
844 } else {
845 nmp->nm_wsize = NFS_WSIZE;
846 nmp->nm_rsize = NFS_RSIZE;
847 }
848 nmp->nm_readdirsize = NFS_READDIRSIZE;
849 nmp->nm_numgrps = NFS_MAXGRPS;
850 nmp->nm_readahead = NFS_DEFRAHEAD;
851 nmp->nm_leaseterm = NQ_DEFLEASE;
852 nmp->nm_deadthresh = NQ_DEADTHRESH;
853 nmp->nm_tprintf_delay = nfs_tprintf_delay;
854 if (nmp->nm_tprintf_delay < 0)
855 nmp->nm_tprintf_delay = 0;
856 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
857 if (nmp->nm_tprintf_initial_delay < 0)
858 nmp->nm_tprintf_initial_delay = 0;
859 CIRCLEQ_INIT(&nmp->nm_timerhead);
860 nmp->nm_inprog = NULLVP;
861 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
862 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
863 nmp->nm_nam = nam;
864
865 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
866 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
867 if (nmp->nm_timeo < NFS_MINTIMEO)
868 nmp->nm_timeo = NFS_MINTIMEO;
869 else if (nmp->nm_timeo > NFS_MAXTIMEO)
870 nmp->nm_timeo = NFS_MAXTIMEO;
871 }
872
873 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
874 nmp->nm_retry = argp->retrans;
875 if (nmp->nm_retry > NFS_MAXREXMIT)
876 nmp->nm_retry = NFS_MAXREXMIT;
877 }
878
879 if (argp->flags & NFSMNT_NFSV3) {
880 if (argp->sotype == SOCK_DGRAM)
881 maxio = NFS_MAXDGRAMDATA;
882 else
883 maxio = NFS_MAXDATA;
884 } else
885 maxio = NFS_V2MAXDATA;
886
887 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
888 nmp->nm_wsize = argp->wsize;
889 /* Round down to multiple of blocksize */
890 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
891 if (nmp->nm_wsize <= 0)
892 nmp->nm_wsize = NFS_FABLKSIZE;
893 }
894 if (nmp->nm_wsize > maxio)
895 nmp->nm_wsize = maxio;
896 if (nmp->nm_wsize > MAXBSIZE)
897 nmp->nm_wsize = MAXBSIZE;
898
899 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
900 nmp->nm_rsize = argp->rsize;
901 /* Round down to multiple of blocksize */
902 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
903 if (nmp->nm_rsize <= 0)
904 nmp->nm_rsize = NFS_FABLKSIZE;
905 }
906 if (nmp->nm_rsize > maxio)
907 nmp->nm_rsize = maxio;
908 if (nmp->nm_rsize > MAXBSIZE)
909 nmp->nm_rsize = MAXBSIZE;
910
911 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
912 nmp->nm_readdirsize = argp->readdirsize;
913 }
914 if (nmp->nm_readdirsize > maxio)
915 nmp->nm_readdirsize = maxio;
916 if (nmp->nm_readdirsize > nmp->nm_rsize)
917 nmp->nm_readdirsize = nmp->nm_rsize;
918
919 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
920 argp->maxgrouplist <= NFS_MAXGRPS)
921 nmp->nm_numgrps = argp->maxgrouplist;
922 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
923 argp->readahead <= NFS_MAXRAHEAD)
924 nmp->nm_readahead = argp->readahead;
925 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
926 argp->leaseterm <= NQ_MAXLEASE)
927 nmp->nm_leaseterm = argp->leaseterm;
928 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
929 argp->deadthresh <= NQ_NEVERDEAD)
930 nmp->nm_deadthresh = argp->deadthresh;
931 /* Set up the sockets and per-host congestion */
932 nmp->nm_sotype = argp->sotype;
933 nmp->nm_soproto = argp->proto;
934
935 /*
936 * For Connection based sockets (TCP,...) defer the connect until
937 * the first request, in case the server is not responding.
938 */
939 if (nmp->nm_sotype == SOCK_DGRAM &&
940 (error = nfs_connect(nmp, (struct nfsreq *)0)))
941 goto bad;
942
943 /*
944 * A reference count is needed on the nfsnode representing the
945 * remote root. If this object is not persistent, then backward
946 * traversals of the mount point (i.e. "..") will not work if
947 * the nfsnode gets flushed out of the cache. UFS does not have
948 * this problem, because one can identify root inodes by their
949 * number == ROOTINO (2).
950 */
951 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
952 if (error)
953 goto bad;
954
955 /*
956 * save this vnode pointer. That way nfs_unmount()
957 * does not need to call nfs_net() just get it to drop
958 * this vnode reference.
959 */
960 nmp->nm_dvp = *vpp = NFSTOV(np);
961
962 /*
963 * Get file attributes for the mountpoint. This has the side
964 * effect of filling in (*vpp)->v_type with the correct value.
965 */
966 curproc = current_proc();
967 error = VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc);
968 if (error) {
969 /*
970 * we got problems... we couldn't get the attributes
971 * from the NFS server... so the mount fails.
972 */
973 vput(*vpp);
974 goto bad;
975 }
976
977 /*
978 * Set the mount point's block I/O size.
979 * We really need to do this after we get info back from
980 * the server about what its preferred I/O sizes are.
981 */
982 if (nmp->nm_flag & NFSMNT_NFSV3)
983 nfs_fsinfo(nmp, *vpp, curproc->p_ucred, curproc);
984 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
985
986 /*
987 * Lose the lock but keep the ref.
988 */
989 VOP_UNLOCK(*vpp, 0, curproc);
990
991 if (nmp->nm_flag & NFSMNT_RESVPORT)
992 nfs_resv_mounts++;
993 nmp->nm_state |= NFSSTA_MOUNTED;
994 return (0);
995 bad:
996 nfs_disconnect(nmp);
997 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
998 bad2:
999 m_freem(nam);
1000 return (error);
1001 }
1002
1003
1004 /*
1005 * unmount system call
1006 */
1007 static int
1008 nfs_unmount(mp, mntflags, p)
1009 struct mount *mp;
1010 int mntflags;
1011 struct proc *p;
1012 {
1013 register struct nfsmount *nmp;
1014 struct vnode *vp;
1015 int error, flags = 0;
1016
1017 nmp = VFSTONFS(mp);
1018 /*
1019 * During a force unmount we want to...
1020 * Mark that we are doing a force unmount.
1021 * Make the mountpoint soft.
1022 */
1023 if (mntflags & MNT_FORCE) {
1024 flags |= FORCECLOSE;
1025 nmp->nm_state |= NFSSTA_FORCE;
1026 nmp->nm_flag |= NFSMNT_SOFT;
1027 }
1028 /*
1029 * Goes something like this..
1030 * - Call vflush() to clear out vnodes for this file system,
1031 * except for the swap files. Deal with them in 2nd pass.
1032 * It will do vgone making the vnode VBAD at that time.
1033 * - Decrement reference on the vnode representing remote root.
1034 * - Close the socket
1035 * - Free up the data structures
1036 */
1037 vp = nmp->nm_dvp;
1038
1039 /*
1040 * Must handshake with nqnfs_clientd() if it is active.
1041 */
1042 nmp->nm_state |= NFSSTA_DISMINPROG;
1043 while (nmp->nm_inprog != NULLVP)
1044 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
1045 /*
1046 * vflush will check for busy vnodes on mountpoint.
1047 * Will do the right thing for MNT_FORCE. That is, we should
1048 * not get EBUSY back.
1049 */
1050 error = vflush(mp, vp, SKIPSWAP | flags);
1051 if (mntflags & MNT_FORCE) {
1052 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
1053 } else {
1054 if (vp->v_usecount > 1) {
1055 nmp->nm_state &= ~NFSSTA_DISMINPROG;
1056 return (EBUSY);
1057 }
1058 error = vflush(mp, vp, flags);
1059 }
1060
1061 if (error) {
1062 nmp->nm_state &= ~NFSSTA_DISMINPROG;
1063 return (error);
1064 }
1065
1066 /*
1067 * We are now committed to the unmount.
1068 * For NQNFS, let the server daemon free the nfsmount structure.
1069 */
1070 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
1071 nmp->nm_state |= NFSSTA_DISMNT;
1072 nmp->nm_state &= ~NFSSTA_MOUNTED;
1073 if (nmp->nm_flag & NFSMNT_RESVPORT) {
1074 if (--nfs_resv_mounts == 0)
1075 nfs_bind_resv_thread_wake();
1076 }
1077
1078 /*
1079 * Release the root vnode reference held by mountnfs()
1080 * vflush did the vgone for us when we didn't skip over
1081 * it in the MNT_FORCE case. (Thus vp can't be locked when
1082 * called vflush in non-skip vp case.)
1083 */
1084 vrele(vp);
1085 if (!(mntflags & MNT_FORCE))
1086 vgone(vp);
1087 mp->mnt_data = 0; /* don't want to end up using stale vp */
1088 nfs_disconnect(nmp);
1089 m_freem(nmp->nm_nam);
1090
1091 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) {
1092 register struct nfsreq *rp;
1093 /*
1094 * Loop through outstanding request list and remove dangling
1095 * references to defunct nfsmount struct
1096 */
1097 #if NFSDIAG && 0
1098 if (hw_atomic_add(&nfsreqqusers, 1) != 1)
1099 nfsatompanic("unmount add");
1100 nfsbtlen = backtrace(&nfsbt, sizeof(nfsbt));
1101 nfsbtcpu = cpu_number();
1102 nfsbtthread = (int)(current_thread());
1103 #endif
1104
1105 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
1106 if (rp->r_nmp == nmp)
1107 rp->r_nmp = (struct nfsmount *)0;
1108 #if NFSDIAG && 0
1109 if (hw_atomic_sub(&nfsreqqusers, 1) != 0)
1110 nfsatompanic("unmount sub");
1111 #endif
1112 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1113 }
1114 return (0);
1115 }
1116
1117 /*
1118 * Return root of a filesystem
1119 */
1120 static int
1121 nfs_root(mp, vpp)
1122 struct mount *mp;
1123 struct vnode **vpp;
1124 {
1125 register struct vnode *vp;
1126 struct nfsmount *nmp;
1127 int error, vpid;
1128
1129 nmp = VFSTONFS(mp);
1130 vp = nmp->nm_dvp;
1131 vpid = vp->v_id;
1132 while (error = vget(vp, LK_EXCLUSIVE, current_proc())) {
1133 /* vget may return ENOENT if the dir changes while in vget */
1134 /* If that happens, try vget again, else return the error */
1135 if ((error != ENOENT) || (vp->v_id == vpid))
1136 return (error);
1137 vpid = vp->v_id;
1138 }
1139 if (vp->v_type == VNON)
1140 vp->v_type = VDIR;
1141 vp->v_flag |= VROOT;
1142 *vpp = vp;
1143 return (0);
1144 }
1145
1146 extern int syncprt;
1147
1148 /*
1149 * Flush out the buffer cache
1150 */
1151 /* ARGSUSED */
1152 static int
1153 nfs_sync(mp, waitfor, cred, p)
1154 struct mount *mp;
1155 int waitfor;
1156 struct ucred *cred;
1157 struct proc *p;
1158 {
1159 register struct vnode *vp;
1160 int error, allerror = 0;
1161
1162 /*
1163 * Force stale buffer cache information to be flushed.
1164 */
1165 loop:
1166 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1167 int didhold;
1168 /*
1169 * If the vnode that we are about to sync is no longer
1170 * associated with this mount point, start over.
1171 */
1172 if (vp->v_mount != mp)
1173 goto loop;
1174 if (VOP_ISLOCKED(vp) || LIST_FIRST(&VTONFS(vp)->n_dirtyblkhd) == NULL)
1175 continue;
1176 if (vget(vp, LK_EXCLUSIVE, p))
1177 goto loop;
1178 didhold = ubc_hold(vp);
1179 error = VOP_FSYNC(vp, cred, waitfor, p);
1180 if (error)
1181 allerror = error;
1182 VOP_UNLOCK(vp, 0, p);
1183 if (didhold)
1184 ubc_rele(vp);
1185 vrele(vp);
1186 }
1187 return (allerror);
1188 }
1189
1190 /*
1191 * NFS flat namespace lookup.
1192 * Currently unsupported.
1193 */
1194 /* ARGSUSED */
1195 static int
1196 nfs_vget(mp, ino, vpp)
1197 struct mount *mp;
1198 void *ino; /* XXX void* or ino_t? */
1199 struct vnode **vpp;
1200 {
1201
1202 return (EOPNOTSUPP);
1203 }
1204
1205 /*
1206 * At this point, this should never happen
1207 */
1208 /* ARGSUSED */
1209 static int
1210 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1211 register struct mount *mp;
1212 struct fid *fhp;
1213 struct mbuf *nam;
1214 struct vnode **vpp;
1215 int *exflagsp;
1216 struct ucred **credanonp;
1217 {
1218
1219 return (EINVAL);
1220 }
1221
1222 /*
1223 * Vnode pointer to File handle, should never happen either
1224 */
1225 /* ARGSUSED */
1226 static int
1227 nfs_vptofh(vp, fhp)
1228 struct vnode *vp;
1229 struct fid *fhp;
1230 {
1231
1232 return (EINVAL);
1233 }
1234
1235 /*
1236 * Vfs start routine, a no-op.
1237 */
1238 /* ARGSUSED */
1239 static int
1240 nfs_start(mp, flags, p)
1241 struct mount *mp;
1242 int flags;
1243 struct proc *p;
1244 {
1245
1246 return (0);
1247 }
1248
1249 /*
1250 * Do operations associated with quotas, not supported
1251 */
1252 /* ARGSUSED */
1253 static int
1254 nfs_quotactl(mp, cmd, uid, arg, p)
1255 struct mount *mp;
1256 int cmd;
1257 uid_t uid;
1258 caddr_t arg;
1259 struct proc *p;
1260 {
1261
1262 return (EOPNOTSUPP);
1263 }
1264
1265 /*
1266 * Do that sysctl thang...
1267 */
1268 static int
1269 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1270 size_t newlen, struct proc *p)
1271 {
1272 int error = 0, val;
1273 struct sysctl_req *req;
1274 struct vfsidctl vc;
1275 struct mount *mp;
1276 struct nfsmount *nmp;
1277 struct vfsquery vq;
1278
1279 /*
1280 * All names at this level are terminal.
1281 */
1282 if(namelen > 1)
1283 return ENOTDIR; /* overloaded */
1284
1285 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1286 switch (name[0]) {
1287 case VFS_CTL_TIMEO:
1288 case VFS_CTL_QUERY:
1289 case VFS_CTL_NOLOCKS:
1290 req = oldp;
1291 error = SYSCTL_IN(req, &vc, sizeof(vc));
1292 if (error)
1293 return (error);
1294 mp = vfs_getvfs(&vc.vc_fsid);
1295 if (mp == NULL)
1296 return (ENOENT);
1297 nmp = VFSTONFS(mp);
1298 if (nmp == NULL)
1299 return (ENOENT);
1300 bzero(&vq, sizeof(vq));
1301 VCTLTOREQ(&vc, req);
1302 }
1303
1304 switch(name[0]) {
1305 case NFS_NFSSTATS:
1306 if(!oldp) {
1307 *oldlenp = sizeof nfsstats;
1308 return 0;
1309 }
1310
1311 if(*oldlenp < sizeof nfsstats) {
1312 *oldlenp = sizeof nfsstats;
1313 return ENOMEM;
1314 }
1315
1316 error = copyout(&nfsstats, oldp, sizeof nfsstats);
1317 if (error)
1318 return (error);
1319
1320 if(newp && newlen != sizeof nfsstats)
1321 return EINVAL;
1322
1323 if(newp) {
1324 return copyin(newp, &nfsstats, sizeof nfsstats);
1325 }
1326 return 0;
1327 case VFS_CTL_NOLOCKS:
1328 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1329 if (req->oldptr != NULL) {
1330 error = SYSCTL_OUT(req, &val, sizeof(val));
1331 if (error)
1332 return (error);
1333 }
1334 if (req->newptr != NULL) {
1335 error = SYSCTL_IN(req, &val, sizeof(val));
1336 if (error)
1337 return (error);
1338 if (val)
1339 nmp->nm_flag |= NFSMNT_NOLOCKS;
1340 else
1341 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1342 }
1343 break;
1344 case VFS_CTL_QUERY:
1345 if ((nmp->nm_state & NFSSTA_TIMEO))
1346 vq.vq_flags |= VQ_NOTRESP;
1347 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1348 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1349 vq.vq_flags |= VQ_NOTRESPLOCK;
1350 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1351 break;
1352 case VFS_CTL_TIMEO:
1353 if (req->oldptr != NULL) {
1354 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1355 sizeof(nmp->nm_tprintf_initial_delay));
1356 if (error)
1357 return (error);
1358 }
1359 if (req->newptr != NULL) {
1360 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1361 sizeof(nmp->nm_tprintf_initial_delay));
1362 if (error)
1363 return (error);
1364 if (nmp->nm_tprintf_initial_delay < 0)
1365 nmp->nm_tprintf_initial_delay = 0;
1366 }
1367 break;
1368 default:
1369 return (ENOTSUP);
1370 }
1371 return (error);
1372 }
1373