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