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