]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vfsops.c
65e8f579c94df33a81011a1ab4790782b59d3e64
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1989, 1993, 1995
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
60 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/conf.h>
66 #include <sys/ioctl.h>
67 #include <sys/signal.h>
68 #include <sys/proc_internal.h> /* for fs rooting to update rootdir in fdp */
69 #include <sys/kauth.h>
70 #include <sys/vnode_internal.h>
71 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/mount_internal.h>
75 #include <sys/kpi_mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/fcntl.h>
79 #include <libkern/OSAtomic.h>
80
81 #include <sys/vm.h>
82 #include <sys/vmparam.h>
83
84 #if !defined(NO_MOUNT_PRIVATE)
85 #include <sys/filedesc.h>
86 #endif /* NO_MOUNT_PRIVATE */
87
88 #include <net/if.h>
89 #include <net/route.h>
90 #include <netinet/in.h>
91
92 #include <nfs/rpcv2.h>
93 #include <nfs/nfsproto.h>
94 #include <nfs/nfs.h>
95 #include <nfs/nfsnode.h>
96 #include <nfs/nfsmount.h>
97 #include <nfs/xdr_subs.h>
98 #include <nfs/nfsm_subs.h>
99 #include <nfs/nfsdiskless.h>
100 #include <nfs/nfs_lock.h>
101
102 extern int nfs_mountroot(void);
103
104 extern int nfs_ticks;
105 extern int nfs_mount_type;
106 extern int nfs_resv_mounts;
107
108 struct nfsstats nfsstats;
109 static int nfs_sysctl(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t);
110 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
111 #ifdef notyet
112 SYSCTL_NODE(_vfs, MOUNT_NFS, nfs, CTLFLAG_RW, 0, "NFS filesystem");
113 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
114 &nfsstats, nfsstats, "");
115 #endif
116
117 SYSCTL_DECL(_vfs_generic_nfs);
118 SYSCTL_NODE(_vfs_generic_nfs, OID_AUTO, client, CTLFLAG_RW, 0,
119 "nfs client hinge");
120 /* how long NFS will wait before signalling vfs that it's down. */
121 static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
122 SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_INITIAL_DELAY,
123 initialdowndelay, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
124 /* how long between console messages "nfs server foo not responding" */
125 static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
126 SYSCTL_INT(_vfs_generic_nfs_client, NFS_TPRINTF_DELAY,
127 nextdowndelay, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
128
129 static int nfs_iosize(struct nfsmount *nmp);
130 static int mountnfs(struct user_nfs_args *,mount_t,mbuf_t,proc_t,vnode_t *);
131 static int nfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t context);
132 static int nfs_start(mount_t mp, int flags, vfs_context_t context);
133 static int nfs_unmount(mount_t mp, int mntflags, vfs_context_t context);
134 static int nfs_root(mount_t mp, vnode_t *vpp, vfs_context_t context);
135 static int nfs_statfs(mount_t mp, struct vfsstatfs *sbp, vfs_context_t context);
136 static int nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context);
137 static int nfs_sync( mount_t mp, int waitfor, vfs_context_t context);
138 static int nfs_vptofh(vnode_t vp, int *fhlenp, unsigned char *fhp, vfs_context_t context);
139 static int nfs_fhtovp(mount_t mp, int fhlen, unsigned char *fhp, vnode_t *vpp, vfs_context_t context);
140 static int nfs_vget(mount_t , ino64_t, vnode_t *, vfs_context_t context);
141
142
143 /*
144 * nfs vfs operations.
145 */
146 struct vfsops nfs_vfsops = {
147 nfs_mount,
148 nfs_start,
149 nfs_unmount,
150 nfs_root,
151 NULL, /* quotactl */
152 nfs_vfs_getattr,
153 nfs_sync,
154 nfs_vget,
155 nfs_fhtovp,
156 nfs_vptofh,
157 nfs_init,
158 nfs_sysctl,
159 NULL /* setattr */
160 };
161
162
163 static int
164 nfs_mount_diskless(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *);
165 #if !defined(NO_MOUNT_PRIVATE)
166 static int
167 nfs_mount_diskless_private(struct nfs_dlmount *, const char *, int, vnode_t *, mount_t *);
168 #endif /* NO_MOUNT_PRIVATE */
169
170 static int nfs_iosize(nmp)
171 struct nfsmount* nmp;
172 {
173 int iosize;
174
175 /*
176 * Calculate the size used for io buffers. Use the larger
177 * of the two sizes to minimise nfs requests but make sure
178 * that it is at least one VM page to avoid wasting buffer
179 * space and to allow easy mmapping of I/O buffers.
180 * The read/write rpc calls handle the splitting up of
181 * buffers into multiple requests if the buffer size is
182 * larger than the I/O size.
183 */
184 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
185 if (iosize < PAGE_SIZE)
186 iosize = PAGE_SIZE;
187 return (trunc_page_32(iosize));
188 }
189
190 /*
191 * nfs statfs call
192 */
193 int
194 nfs_statfs(mount_t mp, struct vfsstatfs *sbp, vfs_context_t context)
195 {
196 proc_t p = vfs_context_proc(context);
197 vnode_t vp;
198 struct nfs_statfs *sfp;
199 caddr_t cp;
200 u_long *tl;
201 long t1, t2;
202 caddr_t bpos, dpos, cp2;
203 struct nfsmount *nmp = VFSTONFS(mp);
204 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
205 mbuf_t mreq, mrep, md, mb, mb2;
206 u_int64_t xid;
207 kauth_cred_t cred;
208 struct ucred temp_cred;
209
210 #ifndef nolint
211 sfp = (struct nfs_statfs *)0;
212 #endif
213 vp = nmp->nm_dvp;
214 if ((error = vnode_get(vp)))
215 return(error);
216
217 bzero(&temp_cred, sizeof(temp_cred));
218 temp_cred.cr_ngroups = 1;
219 cred = kauth_cred_create(&temp_cred);
220
221 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
222 nfs_fsinfo(nmp, vp, cred, p);
223 nfsm_reqhead(NFSX_FH(v3));
224 if (error) {
225 kauth_cred_rele(cred);
226 vnode_put(vp);
227 return (error);
228 }
229 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_FSSTAT]);
230 nfsm_fhtom(vp, v3);
231 nfsm_request(vp, NFSPROC_FSSTAT, p, cred, &xid);
232 if (v3 && mrep)
233 nfsm_postop_attr_update(vp, v3, retattr, &xid);
234 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
235
236 sbp->f_flags = nmp->nm_flag;
237 sbp->f_iosize = nfs_iosize(nmp);
238 if (v3) {
239 /*
240 * Adjust block size to get total block count to fit in a long.
241 * If we can't increase block size enough, clamp to max long.
242 */
243 u_quad_t tquad, tquad2, bsize;
244 bsize = NFS_FABLKSIZE;
245
246 fxdr_hyper(&sfp->sf_tbytes, &tquad);
247 tquad /= bsize;
248 while ((tquad & ~0x7fffffff) && (bsize < 0x40000000)) {
249 bsize <<= 1;
250 tquad >>= 1;
251 }
252 sbp->f_blocks = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
253
254 fxdr_hyper(&sfp->sf_fbytes, &tquad);
255 tquad /= bsize;
256 sbp->f_bfree = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
257
258 fxdr_hyper(&sfp->sf_abytes, &tquad);
259 tquad /= bsize;
260 sbp->f_bavail = (tquad & ~0x7fffffff) ? 0x7fffffff : (long)tquad;
261
262 sbp->f_bsize = (long)bsize;
263
264 /* adjust file slots too... */
265 fxdr_hyper(&sfp->sf_tfiles, &tquad);
266 fxdr_hyper(&sfp->sf_ffiles, &tquad2);
267 while (tquad & ~0x7fffffff) {
268 tquad >>= 1;
269 tquad2 >>= 1;
270 }
271 sbp->f_files = tquad;
272 sbp->f_ffree = tquad2;
273 } else {
274 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
275 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
276 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
277 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
278 sbp->f_files = 0;
279 sbp->f_ffree = 0;
280 }
281 nfsm_reqdone;
282 kauth_cred_rele(cred);
283 vnode_put(vp);
284 return (error);
285 }
286
287 /*
288 * The nfs_statfs code is complicated, and used by mountnfs(), so leave it as-is
289 * and handle VFS_GETATTR by calling nfs_statfs and copying fields.
290 */
291 static int
292 nfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
293 {
294 int error = 0;
295
296 if (VFSATTR_IS_ACTIVE(fsap, f_bsize) ||
297 VFSATTR_IS_ACTIVE(fsap, f_iosize) ||
298 VFSATTR_IS_ACTIVE(fsap, f_blocks) ||
299 VFSATTR_IS_ACTIVE(fsap, f_bfree) ||
300 VFSATTR_IS_ACTIVE(fsap, f_bavail) ||
301 VFSATTR_IS_ACTIVE(fsap, f_bused) ||
302 VFSATTR_IS_ACTIVE(fsap, f_files) ||
303 VFSATTR_IS_ACTIVE(fsap, f_ffree)) {
304 struct vfsstatfs sb;
305
306 error = nfs_statfs(mp, &sb, context);
307 if (!error) {
308 VFSATTR_RETURN(fsap, f_bsize, sb.f_bsize);
309 VFSATTR_RETURN(fsap, f_iosize, sb.f_iosize);
310 VFSATTR_RETURN(fsap, f_blocks, sb.f_blocks);
311 VFSATTR_RETURN(fsap, f_bfree, sb.f_bfree);
312 VFSATTR_RETURN(fsap, f_bavail, sb.f_bavail);
313 VFSATTR_RETURN(fsap, f_bused, sb.f_blocks - sb.f_bfree);
314 VFSATTR_RETURN(fsap, f_files, sb.f_files);
315 VFSATTR_RETURN(fsap, f_ffree, sb.f_ffree);
316 }
317 }
318
319 if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
320 struct nfsmount *nmp;
321 struct nfsv3_pathconf pc;
322 u_int32_t caps, valid;
323 vnode_t vp;
324 int v3;
325
326 if (!(nmp = VFSTONFS(mp)))
327 return (ENXIO);
328 vp = nmp->nm_dvp;
329 v3 = (nmp->nm_flag & NFSMNT_NFSV3);
330
331 /*
332 * The capabilities[] array defines what this volume supports.
333 *
334 * The valid[] array defines which bits this code understands
335 * the meaning of (whether the volume has that capability or not).
336 * Any zero bits here means "I don't know what you're asking about"
337 * and the caller cannot tell whether that capability is
338 * present or not.
339 */
340 caps = valid = 0;
341 if (v3) {
342 /* try to get fsinfo if we haven't already */
343 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
344 nfs_fsinfo(nmp, vp, vfs_context_ucred(context),
345 vfs_context_proc(context));
346 if (!(nmp = VFSTONFS(vnode_mount(vp))))
347 return (ENXIO);
348 }
349 if (nmp->nm_state & NFSSTA_GOTFSINFO) {
350 /* fsinfo indicates (non)support of links and symlinks */
351 valid |= VOL_CAP_FMT_SYMBOLICLINKS |
352 VOL_CAP_FMT_HARDLINKS;
353 if (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_SYMLINK)
354 caps |= VOL_CAP_FMT_SYMBOLICLINKS;
355 if (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_LINK)
356 caps |= VOL_CAP_FMT_HARDLINKS;
357 /* if fsinfo indicates all pathconf info is the same, */
358 /* we can use it to report case attributes */
359 if ((nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS) &&
360 !(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
361 /* no cached pathconf info, try to get now */
362 error = nfs_pathconfrpc(vp, &pc,
363 vfs_context_ucred(context),
364 vfs_context_proc(context));
365 if (!(nmp = VFSTONFS(vnode_mount(vp))))
366 return (ENXIO);
367 if (!error) {
368 /* all files have the same pathconf info, */
369 /* so cache a copy of the results */
370 nfs_pathconf_cache(nmp, &pc);
371 }
372 }
373 if (nmp->nm_state & NFSSTA_GOTPATHCONF) {
374 valid |= VOL_CAP_FMT_CASE_SENSITIVE |
375 VOL_CAP_FMT_CASE_PRESERVING;
376 if (!(nmp->nm_fsinfo.pcflags &
377 NFSPCINFO_CASE_INSENSITIVE))
378 caps |= VOL_CAP_FMT_CASE_SENSITIVE;
379 if (nmp->nm_fsinfo.pcflags &
380 NFSPCINFO_CASE_PRESERVING)
381 caps |= VOL_CAP_FMT_CASE_PRESERVING;
382 }
383 /* Is server's max file size at least 2TB? */
384 if (nmp->nm_fsinfo.maxfilesize >= 0x20000000000ULL)
385 caps |= VOL_CAP_FMT_2TB_FILESIZE;
386 } else {
387 /*
388 * NFSv3 supports 64 bits of file size.
389 * Without FSINFO from the server, we'll
390 * just assume maxfilesize >= 2TB
391 */
392 caps |= VOL_CAP_FMT_2TB_FILESIZE;
393 }
394 }
395 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
396 // VOL_CAP_FMT_PERSISTENTOBJECTIDS |
397 // VOL_CAP_FMT_SYMBOLICLINKS |
398 // VOL_CAP_FMT_HARDLINKS |
399 // VOL_CAP_FMT_JOURNAL |
400 // VOL_CAP_FMT_JOURNAL_ACTIVE |
401 // VOL_CAP_FMT_NO_ROOT_TIMES |
402 // VOL_CAP_FMT_SPARSE_FILES |
403 // VOL_CAP_FMT_ZERO_RUNS |
404 // VOL_CAP_FMT_CASE_SENSITIVE |
405 // VOL_CAP_FMT_CASE_PRESERVING |
406 // VOL_CAP_FMT_FAST_STATFS |
407 // VOL_CAP_FMT_2TB_FILESIZE |
408 caps;
409 fsap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
410 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
411 // VOL_CAP_FMT_SYMBOLICLINKS |
412 // VOL_CAP_FMT_HARDLINKS |
413 // VOL_CAP_FMT_JOURNAL |
414 // VOL_CAP_FMT_JOURNAL_ACTIVE |
415 // VOL_CAP_FMT_NO_ROOT_TIMES |
416 // VOL_CAP_FMT_SPARSE_FILES |
417 // VOL_CAP_FMT_ZERO_RUNS |
418 // VOL_CAP_FMT_CASE_SENSITIVE |
419 // VOL_CAP_FMT_CASE_PRESERVING |
420 VOL_CAP_FMT_FAST_STATFS |
421 VOL_CAP_FMT_2TB_FILESIZE |
422 valid;
423
424 /*
425 * We don't support most of the interfaces.
426 *
427 * We MAY support locking, but we don't have any easy way of probing.
428 * We can tell if there's no lockd running or if locks have been
429 * disabled for a mount, so we can definitely answer NO in that case.
430 * Any attempt to send a request to lockd to test for locking support
431 * may cause the lazily-launched locking daemons to be started
432 * unnecessarily. So we avoid that. However, we do record if we ever
433 * successfully perform a lock operation on a mount point, so if it
434 * looks like lock ops have worked, we do report that we support them.
435 */
436 caps = valid = 0;
437 if ((!nfslockdvnode && !nfslockdwaiting) ||
438 (nmp->nm_flag & NFSMNT_NOLOCKS)) {
439 /* locks disabled on this mount, so they definitely won't work */
440 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
441 } else if (nmp->nm_state & NFSSTA_LOCKSWORK) {
442 caps = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
443 valid = VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK;
444 }
445 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
446 // VOL_CAP_INT_SEARCHFS |
447 // VOL_CAP_INT_ATTRLIST |
448 // VOL_CAP_INT_NFSEXPORT |
449 // VOL_CAP_INT_READDIRATTR |
450 // VOL_CAP_INT_EXCHANGEDATA |
451 // VOL_CAP_INT_COPYFILE |
452 // VOL_CAP_INT_ALLOCATE |
453 // VOL_CAP_INT_VOL_RENAME |
454 // VOL_CAP_INT_ADVLOCK |
455 // VOL_CAP_INT_FLOCK |
456 // VOL_CAP_INT_EXTENDED_SECURITY |
457 // VOL_CAP_INT_USERACCESS |
458 caps;
459 fsap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
460 VOL_CAP_INT_SEARCHFS |
461 VOL_CAP_INT_ATTRLIST |
462 VOL_CAP_INT_NFSEXPORT |
463 VOL_CAP_INT_READDIRATTR |
464 VOL_CAP_INT_EXCHANGEDATA |
465 VOL_CAP_INT_COPYFILE |
466 VOL_CAP_INT_ALLOCATE |
467 VOL_CAP_INT_VOL_RENAME |
468 // VOL_CAP_INT_ADVLOCK |
469 // VOL_CAP_INT_FLOCK |
470 // VOL_CAP_INT_EXTENDED_SECURITY |
471 // VOL_CAP_INT_USERACCESS |
472 valid;
473
474 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
475 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
476
477 fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
478 fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
479
480 VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
481 }
482
483 if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
484 fsap->f_attributes.validattr.commonattr = 0;
485 fsap->f_attributes.validattr.volattr =
486 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
487 fsap->f_attributes.validattr.dirattr = 0;
488 fsap->f_attributes.validattr.fileattr = 0;
489 fsap->f_attributes.validattr.forkattr = 0;
490
491 fsap->f_attributes.nativeattr.commonattr = 0;
492 fsap->f_attributes.nativeattr.volattr =
493 ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES;
494 fsap->f_attributes.nativeattr.dirattr = 0;
495 fsap->f_attributes.nativeattr.fileattr = 0;
496 fsap->f_attributes.nativeattr.forkattr = 0;
497
498 VFSATTR_SET_SUPPORTED(fsap, f_attributes);
499 }
500
501 return (error);
502 }
503
504 /*
505 * nfs version 3 fsinfo rpc call
506 */
507 int
508 nfs_fsinfo(nmp, vp, cred, p)
509 struct nfsmount *nmp;
510 vnode_t vp;
511 kauth_cred_t cred;
512 proc_t p;
513 {
514 struct nfsv3_fsinfo *fsp;
515 caddr_t cp;
516 long t1, t2;
517 u_long *tl;
518 int prefsize, maxsize;
519 caddr_t bpos, dpos, cp2;
520 int error = 0, retattr;
521 mbuf_t mreq, mrep, md, mb, mb2;
522 u_int64_t xid;
523
524 nfsm_reqhead(NFSX_FH(1));
525 if (error)
526 return (error);
527 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_FSINFO]);
528 nfsm_fhtom(vp, 1);
529 nfsm_request(vp, NFSPROC_FSINFO, p, cred, &xid);
530 if (mrep) {
531 nfsm_postop_attr_update(vp, 1, retattr, &xid);
532 }
533 if (!error) {
534 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
535 prefsize = fxdr_unsigned(u_long, fsp->fs_wtpref);
536 if (prefsize < nmp->nm_wsize)
537 nmp->nm_wsize = (prefsize + NFS_FABLKSIZE - 1) &
538 ~(NFS_FABLKSIZE - 1);
539 maxsize = fxdr_unsigned(u_long, fsp->fs_wtmax);
540 if (maxsize < nmp->nm_wsize) {
541 nmp->nm_wsize = maxsize & ~(NFS_FABLKSIZE - 1);
542 if (nmp->nm_wsize == 0)
543 nmp->nm_wsize = maxsize;
544 }
545 prefsize = fxdr_unsigned(u_long, fsp->fs_rtpref);
546 if (prefsize < nmp->nm_rsize)
547 nmp->nm_rsize = (prefsize + NFS_FABLKSIZE - 1) &
548 ~(NFS_FABLKSIZE - 1);
549 maxsize = fxdr_unsigned(u_long, fsp->fs_rtmax);
550 if (maxsize < nmp->nm_rsize) {
551 nmp->nm_rsize = maxsize & ~(NFS_FABLKSIZE - 1);
552 if (nmp->nm_rsize == 0)
553 nmp->nm_rsize = maxsize;
554 }
555 prefsize = fxdr_unsigned(u_long, fsp->fs_dtpref);
556 if (prefsize < nmp->nm_readdirsize)
557 nmp->nm_readdirsize = prefsize;
558 if (maxsize < nmp->nm_readdirsize) {
559 nmp->nm_readdirsize = maxsize;
560 }
561 fxdr_hyper(&fsp->fs_maxfilesize, &nmp->nm_fsinfo.maxfilesize);
562 nmp->nm_fsinfo.fsproperties = fxdr_unsigned(u_long, fsp->fs_properties);
563 nmp->nm_state |= NFSSTA_GOTFSINFO;
564 }
565 nfsm_reqdone;
566 return (error);
567 }
568
569 /*
570 * Mount a remote root fs via. nfs. This depends on the info in the
571 * nfs_diskless structure that has been filled in properly by some primary
572 * bootstrap.
573 * It goes something like this:
574 * - do enough of "ifconfig" by calling ifioctl() so that the system
575 * can talk to the server
576 * - If nfs_diskless.mygateway is filled in, use that address as
577 * a default gateway.
578 * - hand craft the swap nfs vnode hanging off a fake mount point
579 * if swdevt[0].sw_dev == NODEV
580 * - build the rootfs mount point and call mountnfs() to do the rest.
581 */
582 int
583 nfs_mountroot()
584 {
585 struct nfs_diskless nd;
586 struct nfs_vattr nvattr;
587 mount_t mp;
588 vnode_t vp;
589 proc_t procp;
590 int error;
591 #if !defined(NO_MOUNT_PRIVATE)
592 mount_t mppriv;
593 vnode_t vppriv;
594 #endif /* NO_MOUNT_PRIVATE */
595 int v3, sotype;
596
597 procp = current_proc(); /* XXX */
598
599 /*
600 * Call nfs_boot_init() to fill in the nfs_diskless struct.
601 * Note: networking must already have been configured before
602 * we're called.
603 */
604 bzero((caddr_t) &nd, sizeof(nd));
605 error = nfs_boot_init(&nd, procp);
606 if (error) {
607 panic("nfs_boot_init failed with %d\n", error);
608 }
609
610 /*
611 * Try NFSv3 first, then fallback to NFSv2.
612 * Likewise, try TCP first, then fall back to UDP.
613 */
614 v3 = 1;
615 sotype = SOCK_STREAM;
616
617 tryagain:
618 error = nfs_boot_getfh(&nd, procp, v3, sotype);
619 if (error) {
620 if (error == EHOSTDOWN || error == EHOSTUNREACH) {
621 if (nd.nd_root.ndm_path)
622 FREE_ZONE(nd.nd_root.ndm_path,
623 MAXPATHLEN, M_NAMEI);
624 if (nd.nd_private.ndm_path)
625 FREE_ZONE(nd.nd_private.ndm_path,
626 MAXPATHLEN, M_NAMEI);
627 return (error);
628 }
629 if (v3) {
630 if (sotype == SOCK_STREAM) {
631 printf("nfs_boot_getfh(v3,TCP) failed with %d, trying UDP...\n", error);
632 sotype = SOCK_DGRAM;
633 goto tryagain;
634 }
635 printf("nfs_boot_getfh(v3,UDP) failed with %d, trying v2...\n", error);
636 v3 = 0;
637 sotype = SOCK_STREAM;
638 goto tryagain;
639 } else if (sotype == SOCK_STREAM) {
640 printf("nfs_boot_getfh(v2,TCP) failed with %d, trying UDP...\n", error);
641 sotype = SOCK_DGRAM;
642 goto tryagain;
643 }
644 panic("nfs_boot_getfh(v2,UDP) failed with %d\n", error);
645 }
646
647 /*
648 * Create the root mount point.
649 */
650 #if !defined(NO_MOUNT_PRIVATE)
651 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_RDONLY|MNT_ROOTFS, &vp, &mp)))
652 #else
653 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_ROOTFS, &vp, &mp)))
654 #endif /* NO_MOUNT_PRIVATE */
655 {
656 if (v3) {
657 if (sotype == SOCK_STREAM) {
658 printf("nfs_mount_diskless(v3,TCP) failed with %d, trying UDP...\n", error);
659 sotype = SOCK_DGRAM;
660 goto tryagain;
661 }
662 printf("nfs_mount_diskless(v3,UDP) failed with %d, trying v2...\n", error);
663 v3 = 0;
664 sotype = SOCK_STREAM;
665 goto tryagain;
666 } else if (sotype == SOCK_STREAM) {
667 printf("nfs_mount_diskless(v2,TCP) failed with %d, trying UDP...\n", error);
668 sotype = SOCK_DGRAM;
669 goto tryagain;
670 }
671 panic("nfs_mount_diskless(v2,UDP) root failed with %d\n", error);
672 }
673 printf("root on %s\n", (char *)&nd.nd_root.ndm_host);
674
675 vfs_unbusy(mp);
676 mount_list_add(mp);
677 rootvp = vp;
678
679 #if !defined(NO_MOUNT_PRIVATE)
680 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
681 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
682 0, &vppriv, &mppriv);
683 if (error) {
684 panic("nfs_mount_diskless private failed with %d\n", error);
685 }
686 printf("private on %s\n", (char *)&nd.nd_private.ndm_host);
687
688 vfs_unbusy(mppriv);
689 mount_list_add(mppriv);
690 }
691
692 #endif /* NO_MOUNT_PRIVATE */
693
694 if (nd.nd_root.ndm_path)
695 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
696 if (nd.nd_private.ndm_path)
697 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
698
699 /* Get root attributes (for the time). */
700 error = nfs_getattr(vp, &nvattr, kauth_cred_get(), procp);
701 if (error) panic("nfs_mountroot: getattr for root");
702 return (0);
703 }
704
705 /*
706 * Internal version of mount system call for diskless setup.
707 */
708 static int
709 nfs_mount_diskless(
710 struct nfs_dlmount *ndmntp,
711 const char *mntname,
712 int mntflag,
713 vnode_t *vpp,
714 mount_t *mpp)
715 {
716 struct user_nfs_args args;
717 mount_t mp;
718 mbuf_t m;
719 int error;
720 proc_t procp;
721
722 procp = current_proc(); /* XXX */
723
724 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_host, &mp))) {
725 printf("nfs_mount_diskless: NFS not configured");
726 return (error);
727 }
728
729 mp->mnt_flag |= mntflag;
730 if (!(mntflag & MNT_RDONLY))
731 mp->mnt_flag &= ~MNT_RDONLY;
732
733 /* Initialize mount args. */
734 bzero((caddr_t) &args, sizeof(args));
735 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
736 args.addrlen = ndmntp->ndm_saddr.sin_len;
737 args.sotype = ndmntp->ndm_sotype;
738 args.fh = CAST_USER_ADDR_T(&ndmntp->ndm_fh[0]);
739 args.fhsize = ndmntp->ndm_fhlen;
740 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_host);
741 args.flags = NFSMNT_RESVPORT;
742 if (ndmntp->ndm_nfsv3)
743 args.flags |= NFSMNT_NFSV3;
744
745 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
746 if (error) {
747 printf("nfs_mount_diskless: mbuf_get(soname) failed");
748 return (error);
749 }
750 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
751 bcopy((caddr_t)args.addr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
752 if ((error = mountnfs(&args, mp, m, procp, vpp))) {
753 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
754 // XXX vfs_rootmountfailed(mp);
755 mount_list_lock();
756 mp->mnt_vtable->vfc_refcount--;
757 mount_list_unlock();
758 vfs_unbusy(mp);
759 mount_lock_destroy(mp);
760 FREE_ZONE(mp, sizeof(struct mount), M_MOUNT);
761 return (error);
762 }
763 *mpp = mp;
764 return (0);
765 }
766
767 #if !defined(NO_MOUNT_PRIVATE)
768 /*
769 * Internal version of mount system call to mount "/private"
770 * separately in diskless setup
771 */
772 static int
773 nfs_mount_diskless_private(
774 struct nfs_dlmount *ndmntp,
775 const char *mntname,
776 int mntflag,
777 vnode_t *vpp,
778 mount_t *mpp)
779 {
780 struct user_nfs_args args;
781 mount_t mp;
782 mbuf_t m;
783 int error;
784 proc_t procp;
785 struct vfstable *vfsp;
786 struct nameidata nd;
787 vnode_t vp;
788 struct vfs_context context;
789
790 procp = current_proc(); /* XXX */
791 context.vc_proc = procp;
792 context.vc_ucred = kauth_cred_get();
793
794 {
795 /*
796 * mimic main()!. Temporarily set up rootvnode and other stuff so
797 * that namei works. Need to undo this because main() does it, too
798 */
799 struct filedesc *fdp; /* pointer to file descriptor state */
800 fdp = procp->p_fd;
801 mountlist.tqh_first->mnt_flag |= MNT_ROOTFS;
802
803 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
804 if (VFS_ROOT(mountlist.tqh_first, &rootvnode, NULL))
805 panic("cannot find root vnode");
806 error = vnode_ref(rootvnode);
807 if (error) {
808 printf("nfs_mountroot: vnode_ref() failed on root vnode!\n");
809 return (error);
810 }
811 fdp->fd_cdir = rootvnode;
812 fdp->fd_rdir = NULL;
813 }
814
815 /*
816 * Get vnode to be covered
817 */
818 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
819 mntname, &context);
820 if ((error = namei(&nd))) {
821 printf("nfs_mountroot: private namei failed!\n");
822 return (error);
823 }
824 {
825 /* undo vnode_ref() in mimic main()! */
826 vnode_rele(rootvnode);
827 }
828 nameidone(&nd);
829 vp = nd.ni_vp;
830
831 if ((error = VNOP_FSYNC(vp, MNT_WAIT, &context)) ||
832 (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
833 vnode_put(vp);
834 return (error);
835 }
836 if (vnode_vtype(vp) != VDIR) {
837 vnode_put(vp);
838 return (ENOTDIR);
839 }
840 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
841 if (!strcmp(vfsp->vfc_name, "nfs"))
842 break;
843 if (vfsp == NULL) {
844 printf("nfs_mountroot: private NFS not configured\n");
845 vnode_put(vp);
846 return (ENODEV);
847 }
848 if (vnode_mountedhere(vp) != NULL) {
849 vnode_put(vp);
850 return (EBUSY);
851 }
852
853 /*
854 * Allocate and initialize the filesystem.
855 */
856 mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
857 if (!mp) {
858 printf("nfs_mountroot: unable to allocate mount structure\n");
859 vnode_put(vp);
860 return (ENOMEM);
861 }
862 bzero((char *)mp, (u_long)sizeof(struct mount));
863
864 /* Initialize the default IO constraints */
865 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
866 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
867
868 mount_lock_init(mp);
869 TAILQ_INIT(&mp->mnt_vnodelist);
870 TAILQ_INIT(&mp->mnt_workerqueue);
871 TAILQ_INIT(&mp->mnt_newvnodes);
872 (void)vfs_busy(mp, LK_NOWAIT);
873 TAILQ_INIT(&mp->mnt_vnodelist);
874 mount_list_lock();
875 vfsp->vfc_refcount++;
876 mount_list_unlock();
877 mp->mnt_vtable = vfsp;
878 mp->mnt_op = vfsp->vfc_vfsops;
879 // mp->mnt_stat.f_type = vfsp->vfc_typenum;
880 mp->mnt_flag = mntflag;
881 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
882 strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSNAMELEN-1);
883 vp->v_mountedhere = mp;
884 mp->mnt_vnodecovered = vp;
885 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
886 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MNAMELEN - 1, 0);
887 (void) copystr(ndmntp->ndm_host, mp->mnt_vfsstat.f_mntfromname, MNAMELEN - 1, 0);
888
889 /* Initialize mount args. */
890 bzero((caddr_t) &args, sizeof(args));
891 args.addr = CAST_USER_ADDR_T(&ndmntp->ndm_saddr);
892 args.addrlen = ndmntp->ndm_saddr.sin_len;
893 args.sotype = ndmntp->ndm_sotype;
894 args.fh = CAST_USER_ADDR_T(ndmntp->ndm_fh);
895 args.fhsize = ndmntp->ndm_fhlen;
896 args.hostname = CAST_USER_ADDR_T(ndmntp->ndm_host);
897 args.flags = NFSMNT_RESVPORT;
898 if (ndmntp->ndm_nfsv3)
899 args.flags |= NFSMNT_NFSV3;
900
901 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &m);
902 if (error) {
903 printf("nfs_mount_diskless_private: mbuf_get(soname) failed");
904 return (error);
905 }
906 mbuf_setlen(m, ndmntp->ndm_saddr.sin_len);
907 bcopy((caddr_t)args.addr, mbuf_data(m), ndmntp->ndm_saddr.sin_len);
908 if ((error = mountnfs(&args, mp, m, procp, &vp))) {
909 printf("nfs_mountroot: mount %s failed: %d\n", mntname, error);
910 mount_list_lock();
911 vfsp->vfc_refcount--;
912 mount_list_unlock();
913 vfs_unbusy(mp);
914 mount_lock_destroy(mp);
915 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
916 return (error);
917 }
918
919 *mpp = mp;
920 *vpp = vp;
921 return (0);
922 }
923 #endif /* NO_MOUNT_PRIVATE */
924
925 /*
926 * VFS Operations.
927 *
928 * mount system call
929 */
930 static int
931 nfs_mount(mount_t mp, vnode_t vp, user_addr_t data, vfs_context_t context)
932 {
933 proc_t p = vfs_context_proc(context);
934 int error, argsvers;
935 struct user_nfs_args args;
936 struct nfs_args tempargs;
937 mbuf_t nam;
938 size_t len;
939 u_char nfh[NFSX_V3FHMAX];
940 char *mntfrom;
941
942 error = copyin(data, (caddr_t)&argsvers, sizeof (argsvers));
943 if (error)
944 return (error);
945
946 switch (argsvers) {
947 case 3:
948 if (vfs_context_is64bit(context))
949 error = copyin(data, (caddr_t)&args, sizeof (struct user_nfs_args3));
950 else
951 error = copyin(data, (caddr_t)&tempargs, sizeof (struct nfs_args3));
952 break;
953 case 4:
954 if (vfs_context_is64bit(context))
955 error = copyin(data, (caddr_t)&args, sizeof (args));
956 else
957 error = copyin(data, (caddr_t)&tempargs, sizeof (tempargs));
958 break;
959 default:
960 return (EPROGMISMATCH);
961 }
962 if (error)
963 return (error);
964
965 if (!vfs_context_is64bit(context)) {
966 args.version = tempargs.version;
967 args.addrlen = tempargs.addrlen;
968 args.sotype = tempargs.sotype;
969 args.proto = tempargs.proto;
970 args.fhsize = tempargs.fhsize;
971 args.flags = tempargs.flags;
972 args.wsize = tempargs.wsize;
973 args.rsize = tempargs.rsize;
974 args.readdirsize = tempargs.readdirsize;
975 args.timeo = tempargs.timeo;
976 args.retrans = tempargs.retrans;
977 args.maxgrouplist = tempargs.maxgrouplist;
978 args.readahead = tempargs.readahead;
979 args.leaseterm = tempargs.leaseterm;
980 args.deadthresh = tempargs.deadthresh;
981 args.addr = CAST_USER_ADDR_T(tempargs.addr);
982 args.fh = CAST_USER_ADDR_T(tempargs.fh);
983 args.hostname = CAST_USER_ADDR_T(tempargs.hostname);
984 if (argsvers >= 4) {
985 args.acregmin = tempargs.acregmin;
986 args.acregmax = tempargs.acregmax;
987 args.acdirmin = tempargs.acdirmin;
988 args.acdirmax = tempargs.acdirmax;
989 }
990 }
991
992 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
993 return (EINVAL);
994 error = copyin(args.fh, (caddr_t)nfh, args.fhsize);
995 if (error)
996 return (error);
997
998 mntfrom = &vfs_statfs(mp)->f_mntfromname[0];
999 error = copyinstr(args.hostname, mntfrom, MAXPATHLEN-1, &len);
1000 if (error)
1001 return (error);
1002 bzero(&mntfrom[len], MAXPATHLEN - len);
1003
1004 /* sockargs() call must be after above copyin() calls */
1005 error = sockargs(&nam, args.addr, args.addrlen, MBUF_TYPE_SONAME);
1006 if (error)
1007 return (error);
1008
1009 args.fh = CAST_USER_ADDR_T(&nfh[0]);
1010 error = mountnfs(&args, mp, nam, p, &vp);
1011 return (error);
1012 }
1013
1014 /*
1015 * Common code for mount and mountroot
1016 */
1017 static int
1018 mountnfs(
1019 struct user_nfs_args *argp,
1020 mount_t mp,
1021 mbuf_t nam,
1022 proc_t p,
1023 vnode_t *vpp)
1024 {
1025 struct nfsmount *nmp;
1026 struct nfsnode *np;
1027 int error, maxio;
1028 struct nfs_vattr nvattrs;
1029 struct vfs_context context; /* XXX get from caller? */
1030 u_int64_t xid;
1031
1032 /*
1033 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
1034 * no sense in that context.
1035 */
1036 if (argp->sotype == SOCK_STREAM)
1037 argp->flags &= ~NFSMNT_NOCONN;
1038
1039 if (vfs_flags(mp) & MNT_UPDATE) {
1040 nmp = VFSTONFS(mp);
1041 /* update paths, file handles, etc, here XXX */
1042 mbuf_freem(nam);
1043 return (0);
1044 } else {
1045 MALLOC_ZONE(nmp, struct nfsmount *,
1046 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
1047 if (!nmp) {
1048 mbuf_freem(nam);
1049 return (ENOMEM);
1050 }
1051 bzero((caddr_t)nmp, sizeof (struct nfsmount));
1052 TAILQ_INIT(&nmp->nm_uidlruhead);
1053 TAILQ_INIT(&nmp->nm_bufq);
1054 vfs_setfsprivate(mp, nmp);
1055 }
1056
1057 /* setup defaults */
1058 nmp->nm_timeo = NFS_TIMEO;
1059 nmp->nm_retry = NFS_RETRANS;
1060 if (argp->sotype == SOCK_DGRAM) {
1061 nmp->nm_wsize = NFS_DGRAM_WSIZE;
1062 nmp->nm_rsize = NFS_DGRAM_RSIZE;
1063 } else {
1064 nmp->nm_wsize = NFS_WSIZE;
1065 nmp->nm_rsize = NFS_RSIZE;
1066 }
1067 nmp->nm_readdirsize = NFS_READDIRSIZE;
1068 nmp->nm_numgrps = NFS_MAXGRPS;
1069 nmp->nm_readahead = NFS_DEFRAHEAD;
1070 nmp->nm_tprintf_delay = nfs_tprintf_delay;
1071 if (nmp->nm_tprintf_delay < 0)
1072 nmp->nm_tprintf_delay = 0;
1073 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
1074 if (nmp->nm_tprintf_initial_delay < 0)
1075 nmp->nm_tprintf_initial_delay = 0;
1076 nmp->nm_acregmin = NFS_MINATTRTIMO;
1077 nmp->nm_acregmax = NFS_MAXATTRTIMO;
1078 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
1079 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
1080
1081 vfs_getnewfsid(mp);
1082 nmp->nm_mountp = mp;
1083 vfs_setauthopaque(mp);
1084 nmp->nm_flag = argp->flags;
1085 nmp->nm_nam = nam;
1086
1087 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
1088 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
1089 if (nmp->nm_timeo < NFS_MINTIMEO)
1090 nmp->nm_timeo = NFS_MINTIMEO;
1091 else if (nmp->nm_timeo > NFS_MAXTIMEO)
1092 nmp->nm_timeo = NFS_MAXTIMEO;
1093 }
1094
1095 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
1096 nmp->nm_retry = argp->retrans;
1097 if (nmp->nm_retry > NFS_MAXREXMIT)
1098 nmp->nm_retry = NFS_MAXREXMIT;
1099 }
1100
1101 if (argp->flags & NFSMNT_NFSV3) {
1102 if (argp->sotype == SOCK_DGRAM)
1103 maxio = NFS_MAXDGRAMDATA;
1104 else
1105 maxio = NFS_MAXDATA;
1106 } else
1107 maxio = NFS_V2MAXDATA;
1108
1109 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
1110 nmp->nm_wsize = argp->wsize;
1111 /* Round down to multiple of blocksize */
1112 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
1113 if (nmp->nm_wsize <= 0)
1114 nmp->nm_wsize = NFS_FABLKSIZE;
1115 }
1116 if (nmp->nm_wsize > maxio)
1117 nmp->nm_wsize = maxio;
1118 if (nmp->nm_wsize > MAXBSIZE)
1119 nmp->nm_wsize = MAXBSIZE;
1120
1121 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
1122 nmp->nm_rsize = argp->rsize;
1123 /* Round down to multiple of blocksize */
1124 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
1125 if (nmp->nm_rsize <= 0)
1126 nmp->nm_rsize = NFS_FABLKSIZE;
1127 }
1128 if (nmp->nm_rsize > maxio)
1129 nmp->nm_rsize = maxio;
1130 if (nmp->nm_rsize > MAXBSIZE)
1131 nmp->nm_rsize = MAXBSIZE;
1132
1133 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
1134 nmp->nm_readdirsize = argp->readdirsize;
1135 }
1136 if (nmp->nm_readdirsize > maxio)
1137 nmp->nm_readdirsize = maxio;
1138 if (nmp->nm_readdirsize > nmp->nm_rsize)
1139 nmp->nm_readdirsize = nmp->nm_rsize;
1140
1141 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
1142 argp->maxgrouplist <= NFS_MAXGRPS)
1143 nmp->nm_numgrps = argp->maxgrouplist;
1144 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
1145 argp->readahead <= NFS_MAXRAHEAD)
1146 nmp->nm_readahead = argp->readahead;
1147
1148 if (argp->version >= 4) {
1149 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
1150 nmp->nm_acregmin = argp->acregmin;
1151 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
1152 nmp->nm_acregmax = argp->acregmax;
1153 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
1154 nmp->nm_acdirmin = argp->acdirmin;
1155 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
1156 nmp->nm_acdirmax = argp->acdirmax;
1157 if (nmp->nm_acregmin > nmp->nm_acregmax)
1158 nmp->nm_acregmin = nmp->nm_acregmax;
1159 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
1160 nmp->nm_acdirmin = nmp->nm_acdirmax;
1161 }
1162
1163 /* Set up the sockets and per-host congestion */
1164 nmp->nm_sotype = argp->sotype;
1165 nmp->nm_soproto = argp->proto;
1166
1167 /* make sure mbuf constants are set up */
1168 if (!nfs_mbuf_mlen)
1169 nfs_mbuf_init();
1170
1171 /*
1172 * For Connection based sockets (TCP,...) defer the connect until
1173 * the first request, in case the server is not responding.
1174 */
1175 if (nmp->nm_sotype == SOCK_DGRAM &&
1176 (error = nfs_connect(nmp, (struct nfsreq *)0)))
1177 goto bad;
1178
1179 /*
1180 * Get file attributes for the mountpoint. These are needed
1181 * in order to properly create the root vnode.
1182 */
1183 // LP64todo - fix CAST_DOWN of argp->fh
1184 error = nfs_getattr_no_vnode(mp, CAST_DOWN(caddr_t, argp->fh), argp->fhsize,
1185 proc_ucred(p), p, &nvattrs, &xid);
1186 if (error) {
1187 /*
1188 * we got problems... we couldn't get the attributes
1189 * from the NFS server... so the mount fails.
1190 */
1191 goto bad;
1192 }
1193
1194 /*
1195 * A reference count is needed on the nfsnode representing the
1196 * remote root. If this object is not persistent, then backward
1197 * traversals of the mount point (i.e. "..") will not work if
1198 * the nfsnode gets flushed out of the cache. UFS does not have
1199 * this problem, because one can identify root inodes by their
1200 * number == ROOTINO (2).
1201 */
1202 error = nfs_nget(mp, NULL, NULL, CAST_DOWN(caddr_t, argp->fh), argp->fhsize,
1203 &nvattrs, &xid, NG_MARKROOT, &np);
1204 if (error)
1205 goto bad;
1206
1207 /*
1208 * save this vnode pointer. That way nfs_unmount()
1209 * does not need to call nfs_nget() just get it to drop
1210 * this vnode reference.
1211 */
1212 nmp->nm_dvp = *vpp = NFSTOV(np);
1213 /* get usecount and drop iocount */
1214 error = vnode_ref(*vpp);
1215 if (error) {
1216 vnode_put(*vpp);
1217 goto bad;
1218 }
1219 vnode_put(*vpp);
1220
1221 /*
1222 * Set the mount point's block I/O size.
1223 * We really need to do this after we get info back from
1224 * the server about what its preferred I/O sizes are.
1225 */
1226 if (nmp->nm_flag & NFSMNT_NFSV3)
1227 nfs_fsinfo(nmp, *vpp, proc_ucred(p), p);
1228 vfs_statfs(mp)->f_iosize = nfs_iosize(nmp);
1229
1230 /*
1231 * V3 mounts give us a (relatively) reliable remote access(2)
1232 * call, so advertise the fact.
1233 *
1234 * XXX this may not be the best way to go, as the granularity
1235 * offered isn't a good match to our needs.
1236 */
1237 if (nmp->nm_flag & NFSMNT_NFSV3)
1238 vfs_setauthopaqueaccess(mp);
1239
1240 /*
1241 * Do statfs to ensure static info gets set to reasonable values.
1242 */
1243 context.vc_proc = p;
1244 context.vc_ucred = proc_ucred(p);
1245 nfs_statfs(mp, vfs_statfs(mp), &context);
1246
1247 if (nmp->nm_flag & NFSMNT_RESVPORT)
1248 nfs_resv_mounts++;
1249 nmp->nm_state |= NFSSTA_MOUNTED;
1250 return (0);
1251 bad:
1252 nfs_disconnect(nmp);
1253 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1254 mbuf_freem(nam);
1255 return (error);
1256 }
1257
1258
1259 /*
1260 * unmount system call
1261 */
1262 static int
1263 nfs_unmount(
1264 mount_t mp,
1265 int mntflags,
1266 __unused vfs_context_t context)
1267 {
1268 register struct nfsmount *nmp;
1269 vnode_t vp;
1270 int error, flags = 0;
1271
1272 nmp = VFSTONFS(mp);
1273 /*
1274 * During a force unmount we want to...
1275 * Mark that we are doing a force unmount.
1276 * Make the mountpoint soft.
1277 */
1278 if (mntflags & MNT_FORCE) {
1279 flags |= FORCECLOSE;
1280 nmp->nm_state |= NFSSTA_FORCE;
1281 nmp->nm_flag |= NFSMNT_SOFT;
1282 }
1283 /*
1284 * Goes something like this..
1285 * - Call vflush() to clear out vnodes for this file system,
1286 * except for the swap files. Deal with them in 2nd pass.
1287 * - Decrement reference on the vnode representing remote root.
1288 * - Close the socket
1289 * - Free up the data structures
1290 */
1291 vp = nmp->nm_dvp;
1292
1293 /*
1294 * vflush will check for busy vnodes on mountpoint.
1295 * Will do the right thing for MNT_FORCE. That is, we should
1296 * not get EBUSY back.
1297 */
1298 error = vflush(mp, vp, SKIPSWAP | flags);
1299 if (mntflags & MNT_FORCE) {
1300 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
1301 } else {
1302 if (vnode_isinuse(vp, 1))
1303 return (EBUSY);
1304 error = vflush(mp, vp, flags);
1305 }
1306 if (error)
1307 return (error);
1308
1309 nmp->nm_state &= ~NFSSTA_MOUNTED;
1310 if (nmp->nm_flag & NFSMNT_RESVPORT) {
1311 if (--nfs_resv_mounts == 0)
1312 nfs_bind_resv_thread_wake();
1313 }
1314
1315 /*
1316 * Release the root vnode reference held by mountnfs()
1317 */
1318 vnode_rele(vp);
1319
1320 (void)vflush(mp, NULLVP, FORCECLOSE);
1321 vfs_setfsprivate(mp, 0); /* don't want to end up using stale vp */
1322
1323 nfs_disconnect(nmp);
1324 mbuf_freem(nmp->nm_nam);
1325
1326 if ((nmp->nm_flag & NFSMNT_KERB) == 0) {
1327 struct nfsreq *rp;
1328 /*
1329 * Loop through outstanding request list and remove dangling
1330 * references to defunct nfsmount struct
1331 */
1332 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
1333 if (rp->r_nmp == nmp)
1334 rp->r_nmp = (struct nfsmount *)0;
1335 /* Need to wake up any rcvlock waiters so they notice the unmount. */
1336 if (nmp->nm_state & NFSSTA_WANTRCV) {
1337 nmp->nm_state &= ~NFSSTA_WANTRCV;
1338 wakeup(&nmp->nm_state);
1339 }
1340 FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1341 }
1342 return (0);
1343 }
1344
1345 /*
1346 * Return root of a filesystem
1347 */
1348 static int
1349 nfs_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t context)
1350 {
1351 vnode_t vp;
1352 struct nfsmount *nmp;
1353 int error;
1354 u_long vpid;
1355
1356 nmp = VFSTONFS(mp);
1357 vp = nmp->nm_dvp;
1358 vpid = vnode_vid(vp);
1359 while ((error = vnode_getwithvid(vp, vpid))) {
1360 /* vnode_get() may return ENOENT if the dir changes. */
1361 /* If that happens, just try it again, else return the error. */
1362 if ((error != ENOENT) || (vnode_vid(vp) == vpid))
1363 return (error);
1364 vpid = vnode_vid(vp);
1365 }
1366 *vpp = vp;
1367 return (0);
1368 }
1369
1370 /*
1371 * Flush out the buffer cache
1372 */
1373
1374 struct nfs_sync_cargs {
1375 vfs_context_t context;
1376 int waitfor;
1377 int error;
1378 };
1379
1380 static int
1381 nfs_sync_callout(vnode_t vp, void *arg)
1382 {
1383 struct nfs_sync_cargs *cargs = (struct nfs_sync_cargs*)arg;
1384 int error;
1385
1386 if (LIST_EMPTY(&VTONFS(vp)->n_dirtyblkhd))
1387 return (VNODE_RETURNED);
1388 if (VTONFS(vp)->n_flag & NWRBUSY)
1389 return (VNODE_RETURNED);
1390
1391 error = nfs_flush(vp, cargs->waitfor,
1392 vfs_context_ucred(cargs->context),
1393 vfs_context_proc(cargs->context), 0);
1394 if (error)
1395 cargs->error = error;
1396
1397 return (VNODE_RETURNED);
1398 }
1399
1400 static int
1401 nfs_sync(mount_t mp, int waitfor, vfs_context_t context)
1402 {
1403 struct nfs_sync_cargs cargs;
1404
1405 cargs.waitfor = waitfor;
1406 cargs.context = context;
1407 cargs.error = 0;
1408
1409 vnode_iterate(mp, 0, nfs_sync_callout, &cargs);
1410
1411 return (cargs.error);
1412 }
1413
1414 /*
1415 * NFS flat namespace lookup.
1416 * Currently unsupported.
1417 */
1418 /*ARGSUSED*/
1419 static int
1420 nfs_vget(
1421 __unused mount_t mp,
1422 __unused ino64_t ino,
1423 __unused vnode_t *vpp,
1424 __unused vfs_context_t context)
1425 {
1426
1427 return (ENOTSUP);
1428 }
1429
1430 /*
1431 * At this point, this should never happen
1432 */
1433 /*ARGSUSED*/
1434 static int
1435 nfs_fhtovp(
1436 __unused mount_t mp,
1437 __unused int fhlen,
1438 __unused unsigned char *fhp,
1439 __unused vnode_t *vpp,
1440 __unused vfs_context_t context)
1441 {
1442
1443 return (ENOTSUP);
1444 }
1445
1446 /*
1447 * Vnode pointer to File handle, should never happen either
1448 */
1449 /*ARGSUSED*/
1450 static int
1451 nfs_vptofh(
1452 __unused vnode_t vp,
1453 __unused int *fhlenp,
1454 __unused unsigned char *fhp,
1455 __unused vfs_context_t context)
1456 {
1457
1458 return (ENOTSUP);
1459 }
1460
1461 /*
1462 * Vfs start routine, a no-op.
1463 */
1464 /*ARGSUSED*/
1465 static int
1466 nfs_start(
1467 __unused mount_t mp,
1468 __unused int flags,
1469 __unused vfs_context_t context)
1470 {
1471
1472 return (0);
1473 }
1474
1475 /*
1476 * Do that sysctl thang...
1477 */
1478 static int
1479 nfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
1480 user_addr_t newp, size_t newlen, vfs_context_t context)
1481 {
1482 int error = 0, val;
1483 struct sysctl_req *req = NULL;
1484 struct vfsidctl vc;
1485 struct user_vfsidctl user_vc;
1486 mount_t mp;
1487 struct nfsmount *nmp = NULL;
1488 struct vfsquery vq;
1489 boolean_t is_64_bit;
1490
1491 /*
1492 * All names at this level are terminal.
1493 */
1494 if(namelen > 1)
1495 return ENOTDIR; /* overloaded */
1496
1497 is_64_bit = vfs_context_is64bit(context);
1498
1499 /* common code for "new style" VFS_CTL sysctl, get the mount. */
1500 switch (name[0]) {
1501 case VFS_CTL_TIMEO:
1502 case VFS_CTL_QUERY:
1503 case VFS_CTL_NOLOCKS:
1504 req = CAST_DOWN(struct sysctl_req *, oldp);
1505 if (is_64_bit) {
1506 error = SYSCTL_IN(req, &user_vc, sizeof(user_vc));
1507 if (error)
1508 return (error);
1509 mp = vfs_getvfs(&user_vc.vc_fsid);
1510 }
1511 else {
1512 error = SYSCTL_IN(req, &vc, sizeof(vc));
1513 if (error)
1514 return (error);
1515 mp = vfs_getvfs(&vc.vc_fsid);
1516 }
1517 if (mp == NULL)
1518 return (ENOENT);
1519 nmp = VFSTONFS(mp);
1520 if (nmp == NULL)
1521 return (ENOENT);
1522 bzero(&vq, sizeof(vq));
1523 req->newidx = 0;
1524 if (is_64_bit) {
1525 req->newptr = user_vc.vc_ptr;
1526 req->newlen = (size_t)user_vc.vc_len;
1527 }
1528 else {
1529 req->newptr = CAST_USER_ADDR_T(vc.vc_ptr);
1530 req->newlen = vc.vc_len;
1531 }
1532 }
1533
1534 switch(name[0]) {
1535 case NFS_NFSSTATS:
1536 if(!oldp) {
1537 *oldlenp = sizeof nfsstats;
1538 return 0;
1539 }
1540
1541 if(*oldlenp < sizeof nfsstats) {
1542 *oldlenp = sizeof nfsstats;
1543 return ENOMEM;
1544 }
1545
1546 error = copyout(&nfsstats, oldp, sizeof nfsstats);
1547 if (error)
1548 return (error);
1549
1550 if(newp && newlen != sizeof nfsstats)
1551 return EINVAL;
1552
1553 if(newp) {
1554 return copyin(newp, &nfsstats, sizeof nfsstats);
1555 }
1556 return 0;
1557 case VFS_CTL_NOLOCKS:
1558 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1559 if (req->oldptr != USER_ADDR_NULL) {
1560 error = SYSCTL_OUT(req, &val, sizeof(val));
1561 if (error)
1562 return (error);
1563 }
1564 if (req->newptr != USER_ADDR_NULL) {
1565 error = SYSCTL_IN(req, &val, sizeof(val));
1566 if (error)
1567 return (error);
1568 if (val)
1569 nmp->nm_flag |= NFSMNT_NOLOCKS;
1570 else
1571 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1572 }
1573 break;
1574 case VFS_CTL_QUERY:
1575 if (nmp->nm_state & NFSSTA_TIMEO)
1576 vq.vq_flags |= VQ_NOTRESP;
1577 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1578 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1579 vq.vq_flags |= VQ_NOTRESPLOCK;
1580 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1581 break;
1582 case VFS_CTL_TIMEO:
1583 if (req->oldptr != USER_ADDR_NULL) {
1584 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1585 sizeof(nmp->nm_tprintf_initial_delay));
1586 if (error)
1587 return (error);
1588 }
1589 if (req->newptr != USER_ADDR_NULL) {
1590 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1591 sizeof(nmp->nm_tprintf_initial_delay));
1592 if (error)
1593 return (error);
1594 if (nmp->nm_tprintf_initial_delay < 0)
1595 nmp->nm_tprintf_initial_delay = 0;
1596 }
1597 break;
1598 default:
1599 return (ENOTSUP);
1600 }
1601 return (error);
1602 }
1603