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