]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_subs.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_subs.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
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_subs.c 8.8 (Berkeley) 5/22/95
65 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
66 */
67
68 /*
69 * These functions support the macros and help fiddle mbuf chains for
70 * the nfs op functions. They do things like create the rpc header and
71 * copy data between mbuf chains and uio lists.
72 */
73 #include <sys/param.h>
74 #include <sys/proc.h>
75 #include <sys/kauth.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/mount_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/kpi_mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/stat.h>
83 #include <sys/malloc.h>
84 #include <sys/syscall.h>
85 #include <sys/sysctl.h>
86 #include <sys/ubc_internal.h>
87 #include <sys/fcntl.h>
88 #include <sys/uio_internal.h>
89 #include <sys/domain.h>
90 #include <libkern/OSAtomic.h>
91
92 #include <sys/vm.h>
93 #include <sys/vmparam.h>
94
95 #include <sys/time.h>
96 #include <kern/clock.h>
97
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
100 #include <nfs/nfs.h>
101 #include <nfs/nfsnode.h>
102 #include <nfs/xdr_subs.h>
103 #include <nfs/nfsm_subs.h>
104 #include <nfs/nfsmount.h>
105 #include <nfs/nfsrtt.h>
106 #include <nfs/nfs_lock.h>
107
108 #include <miscfs/specfs/specdev.h>
109
110 #include <netinet/in.h>
111 #if ISO
112 #include <netiso/iso.h>
113 #endif
114
115 #include <sys/kdebug.h>
116
117 SYSCTL_DECL(_vfs_generic);
118 SYSCTL_NODE(_vfs_generic, OID_AUTO, nfs, CTLFLAG_RW, 0, "nfs hinge");
119
120 #define FSDBG(A, B, C, D, E) \
121 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
122 (int)(B), (int)(C), (int)(D), (int)(E), 0)
123 #define FSDBG_TOP(A, B, C, D, E) \
124 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
125 (int)(B), (int)(C), (int)(D), (int)(E), 0)
126 #define FSDBG_BOT(A, B, C, D, E) \
127 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
128 (int)(B), (int)(C), (int)(D), (int)(E), 0)
129 /*
130 * Data items converted to xdr at startup, since they are constant
131 * This is kinda hokey, but may save a little time doing byte swaps
132 */
133 u_long nfs_xdrneg1;
134 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
135 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
136 rpc_auth_kerb;
137 u_long nfs_prog, nfs_true, nfs_false;
138 __private_extern__ int nfs_mbuf_mlen = 0, nfs_mbuf_mhlen = 0,
139 nfs_mbuf_minclsize = 0, nfs_mbuf_mclbytes = 0;
140
141 /* And other global data */
142 static u_long nfs_xid = 0;
143 u_long nfs_xidwrap = 0; /* to build a (non-wwrapping) 64 bit xid */
144 static enum vtype nv2tov_type[8]= {
145 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
146 };
147 enum vtype nv3tov_type[8]= {
148 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
149 };
150
151 int nfs_mount_type;
152 int nfs_ticks;
153
154 lck_grp_t *nfsd_lck_grp;
155 lck_grp_attr_t *nfsd_lck_grp_attr;
156 lck_attr_t *nfsd_lck_attr;
157 lck_mtx_t *nfsd_mutex;
158
159 lck_grp_attr_t *nfs_slp_group_attr;
160 lck_attr_t *nfs_slp_lock_attr;
161 lck_grp_t *nfs_slp_rwlock_group;
162 lck_grp_t *nfs_slp_mutex_group;
163
164 struct nfs_reqq nfs_reqq;
165 struct nfssvc_sockhead nfssvc_sockhead, nfssvc_deadsockhead;
166 struct nfsd_head nfsd_head;
167 int nfsd_head_flag;
168
169 struct nfsexpfslist nfs_exports;
170 struct nfsexphashhead *nfsexphashtbl;
171 u_long nfsexphash;
172 lck_grp_attr_t *nfs_export_group_attr;
173 lck_attr_t *nfs_export_lock_attr;
174 lck_grp_t *nfs_export_rwlock_group;
175 lck_rw_t nfs_export_rwlock;
176
177 #ifndef NFS_NOSERVER
178 /*
179 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
180 */
181 int nfsv3_procid[NFS_NPROCS] = {
182 NFSPROC_NULL,
183 NFSPROC_GETATTR,
184 NFSPROC_SETATTR,
185 NFSPROC_NOOP,
186 NFSPROC_LOOKUP,
187 NFSPROC_READLINK,
188 NFSPROC_READ,
189 NFSPROC_NOOP,
190 NFSPROC_WRITE,
191 NFSPROC_CREATE,
192 NFSPROC_REMOVE,
193 NFSPROC_RENAME,
194 NFSPROC_LINK,
195 NFSPROC_SYMLINK,
196 NFSPROC_MKDIR,
197 NFSPROC_RMDIR,
198 NFSPROC_READDIR,
199 NFSPROC_FSSTAT,
200 NFSPROC_NOOP,
201 NFSPROC_NOOP,
202 NFSPROC_NOOP,
203 NFSPROC_NOOP,
204 NFSPROC_NOOP
205 };
206
207 #endif /* NFS_NOSERVER */
208 /*
209 * and the reverse mapping from generic to Version 2 procedure numbers
210 */
211 int nfsv2_procid[NFS_NPROCS] = {
212 NFSV2PROC_NULL,
213 NFSV2PROC_GETATTR,
214 NFSV2PROC_SETATTR,
215 NFSV2PROC_LOOKUP,
216 NFSV2PROC_NOOP,
217 NFSV2PROC_READLINK,
218 NFSV2PROC_READ,
219 NFSV2PROC_WRITE,
220 NFSV2PROC_CREATE,
221 NFSV2PROC_MKDIR,
222 NFSV2PROC_SYMLINK,
223 NFSV2PROC_CREATE,
224 NFSV2PROC_REMOVE,
225 NFSV2PROC_RMDIR,
226 NFSV2PROC_RENAME,
227 NFSV2PROC_LINK,
228 NFSV2PROC_READDIR,
229 NFSV2PROC_NOOP,
230 NFSV2PROC_STATFS,
231 NFSV2PROC_NOOP,
232 NFSV2PROC_NOOP,
233 NFSV2PROC_NOOP,
234 NFSV2PROC_NOOP
235 };
236
237 #ifndef NFS_NOSERVER
238 /*
239 * Maps errno values to nfs error numbers.
240 * Use NFSERR_IO as the catch all for ones not specifically defined in
241 * RFC 1094.
242 */
243 static u_char nfsrv_v2errmap[ELAST] = {
244 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
245 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
246 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
247 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
248 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
249 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
250 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
251 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
252 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
253 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
254 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
255 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
256 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
257 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
258 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
259 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
260 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
261 };
262
263 /*
264 * Maps errno values to nfs error numbers.
265 * Although it is not obvious whether or not NFS clients really care if
266 * a returned error value is in the specified list for the procedure, the
267 * safest thing to do is filter them appropriately. For Version 2, the
268 * X/Open XNFS document is the only specification that defines error values
269 * for each RPC (The RFC simply lists all possible error values for all RPCs),
270 * so I have decided to not do this for Version 2.
271 * The first entry is the default error return and the rest are the valid
272 * errors for that RPC in increasing numeric order.
273 */
274 static short nfsv3err_null[] = {
275 0,
276 0,
277 };
278
279 static short nfsv3err_getattr[] = {
280 NFSERR_IO,
281 NFSERR_IO,
282 NFSERR_STALE,
283 NFSERR_BADHANDLE,
284 NFSERR_SERVERFAULT,
285 0,
286 };
287
288 static short nfsv3err_setattr[] = {
289 NFSERR_IO,
290 NFSERR_PERM,
291 NFSERR_IO,
292 NFSERR_ACCES,
293 NFSERR_INVAL,
294 NFSERR_NOSPC,
295 NFSERR_ROFS,
296 NFSERR_DQUOT,
297 NFSERR_STALE,
298 NFSERR_BADHANDLE,
299 NFSERR_NOT_SYNC,
300 NFSERR_SERVERFAULT,
301 0,
302 };
303
304 static short nfsv3err_lookup[] = {
305 NFSERR_IO,
306 NFSERR_NOENT,
307 NFSERR_IO,
308 NFSERR_ACCES,
309 NFSERR_NOTDIR,
310 NFSERR_NAMETOL,
311 NFSERR_STALE,
312 NFSERR_BADHANDLE,
313 NFSERR_SERVERFAULT,
314 0,
315 };
316
317 static short nfsv3err_access[] = {
318 NFSERR_IO,
319 NFSERR_IO,
320 NFSERR_STALE,
321 NFSERR_BADHANDLE,
322 NFSERR_SERVERFAULT,
323 0,
324 };
325
326 static short nfsv3err_readlink[] = {
327 NFSERR_IO,
328 NFSERR_IO,
329 NFSERR_ACCES,
330 NFSERR_INVAL,
331 NFSERR_STALE,
332 NFSERR_BADHANDLE,
333 NFSERR_NOTSUPP,
334 NFSERR_SERVERFAULT,
335 0,
336 };
337
338 static short nfsv3err_read[] = {
339 NFSERR_IO,
340 NFSERR_IO,
341 NFSERR_NXIO,
342 NFSERR_ACCES,
343 NFSERR_INVAL,
344 NFSERR_STALE,
345 NFSERR_BADHANDLE,
346 NFSERR_SERVERFAULT,
347 0,
348 };
349
350 static short nfsv3err_write[] = {
351 NFSERR_IO,
352 NFSERR_IO,
353 NFSERR_ACCES,
354 NFSERR_INVAL,
355 NFSERR_FBIG,
356 NFSERR_NOSPC,
357 NFSERR_ROFS,
358 NFSERR_DQUOT,
359 NFSERR_STALE,
360 NFSERR_BADHANDLE,
361 NFSERR_SERVERFAULT,
362 0,
363 };
364
365 static short nfsv3err_create[] = {
366 NFSERR_IO,
367 NFSERR_IO,
368 NFSERR_ACCES,
369 NFSERR_EXIST,
370 NFSERR_NOTDIR,
371 NFSERR_NOSPC,
372 NFSERR_ROFS,
373 NFSERR_NAMETOL,
374 NFSERR_DQUOT,
375 NFSERR_STALE,
376 NFSERR_BADHANDLE,
377 NFSERR_NOTSUPP,
378 NFSERR_SERVERFAULT,
379 0,
380 };
381
382 static short nfsv3err_mkdir[] = {
383 NFSERR_IO,
384 NFSERR_IO,
385 NFSERR_ACCES,
386 NFSERR_EXIST,
387 NFSERR_NOTDIR,
388 NFSERR_NOSPC,
389 NFSERR_ROFS,
390 NFSERR_NAMETOL,
391 NFSERR_DQUOT,
392 NFSERR_STALE,
393 NFSERR_BADHANDLE,
394 NFSERR_NOTSUPP,
395 NFSERR_SERVERFAULT,
396 0,
397 };
398
399 static short nfsv3err_symlink[] = {
400 NFSERR_IO,
401 NFSERR_IO,
402 NFSERR_ACCES,
403 NFSERR_EXIST,
404 NFSERR_NOTDIR,
405 NFSERR_NOSPC,
406 NFSERR_ROFS,
407 NFSERR_NAMETOL,
408 NFSERR_DQUOT,
409 NFSERR_STALE,
410 NFSERR_BADHANDLE,
411 NFSERR_NOTSUPP,
412 NFSERR_SERVERFAULT,
413 0,
414 };
415
416 static short nfsv3err_mknod[] = {
417 NFSERR_IO,
418 NFSERR_IO,
419 NFSERR_ACCES,
420 NFSERR_EXIST,
421 NFSERR_NOTDIR,
422 NFSERR_NOSPC,
423 NFSERR_ROFS,
424 NFSERR_NAMETOL,
425 NFSERR_DQUOT,
426 NFSERR_STALE,
427 NFSERR_BADHANDLE,
428 NFSERR_NOTSUPP,
429 NFSERR_SERVERFAULT,
430 NFSERR_BADTYPE,
431 0,
432 };
433
434 static short nfsv3err_remove[] = {
435 NFSERR_IO,
436 NFSERR_NOENT,
437 NFSERR_IO,
438 NFSERR_ACCES,
439 NFSERR_NOTDIR,
440 NFSERR_ROFS,
441 NFSERR_NAMETOL,
442 NFSERR_STALE,
443 NFSERR_BADHANDLE,
444 NFSERR_SERVERFAULT,
445 0,
446 };
447
448 static short nfsv3err_rmdir[] = {
449 NFSERR_IO,
450 NFSERR_NOENT,
451 NFSERR_IO,
452 NFSERR_ACCES,
453 NFSERR_EXIST,
454 NFSERR_NOTDIR,
455 NFSERR_INVAL,
456 NFSERR_ROFS,
457 NFSERR_NAMETOL,
458 NFSERR_NOTEMPTY,
459 NFSERR_STALE,
460 NFSERR_BADHANDLE,
461 NFSERR_NOTSUPP,
462 NFSERR_SERVERFAULT,
463 0,
464 };
465
466 static short nfsv3err_rename[] = {
467 NFSERR_IO,
468 NFSERR_NOENT,
469 NFSERR_IO,
470 NFSERR_ACCES,
471 NFSERR_EXIST,
472 NFSERR_XDEV,
473 NFSERR_NOTDIR,
474 NFSERR_ISDIR,
475 NFSERR_INVAL,
476 NFSERR_NOSPC,
477 NFSERR_ROFS,
478 NFSERR_MLINK,
479 NFSERR_NAMETOL,
480 NFSERR_NOTEMPTY,
481 NFSERR_DQUOT,
482 NFSERR_STALE,
483 NFSERR_BADHANDLE,
484 NFSERR_NOTSUPP,
485 NFSERR_SERVERFAULT,
486 0,
487 };
488
489 static short nfsv3err_link[] = {
490 NFSERR_IO,
491 NFSERR_IO,
492 NFSERR_ACCES,
493 NFSERR_EXIST,
494 NFSERR_XDEV,
495 NFSERR_NOTDIR,
496 NFSERR_INVAL,
497 NFSERR_NOSPC,
498 NFSERR_ROFS,
499 NFSERR_MLINK,
500 NFSERR_NAMETOL,
501 NFSERR_DQUOT,
502 NFSERR_STALE,
503 NFSERR_BADHANDLE,
504 NFSERR_NOTSUPP,
505 NFSERR_SERVERFAULT,
506 0,
507 };
508
509 static short nfsv3err_readdir[] = {
510 NFSERR_IO,
511 NFSERR_IO,
512 NFSERR_ACCES,
513 NFSERR_NOTDIR,
514 NFSERR_STALE,
515 NFSERR_BADHANDLE,
516 NFSERR_BAD_COOKIE,
517 NFSERR_TOOSMALL,
518 NFSERR_SERVERFAULT,
519 0,
520 };
521
522 static short nfsv3err_readdirplus[] = {
523 NFSERR_IO,
524 NFSERR_IO,
525 NFSERR_ACCES,
526 NFSERR_NOTDIR,
527 NFSERR_STALE,
528 NFSERR_BADHANDLE,
529 NFSERR_BAD_COOKIE,
530 NFSERR_NOTSUPP,
531 NFSERR_TOOSMALL,
532 NFSERR_SERVERFAULT,
533 0,
534 };
535
536 static short nfsv3err_fsstat[] = {
537 NFSERR_IO,
538 NFSERR_IO,
539 NFSERR_STALE,
540 NFSERR_BADHANDLE,
541 NFSERR_SERVERFAULT,
542 0,
543 };
544
545 static short nfsv3err_fsinfo[] = {
546 NFSERR_STALE,
547 NFSERR_STALE,
548 NFSERR_BADHANDLE,
549 NFSERR_SERVERFAULT,
550 0,
551 };
552
553 static short nfsv3err_pathconf[] = {
554 NFSERR_STALE,
555 NFSERR_STALE,
556 NFSERR_BADHANDLE,
557 NFSERR_SERVERFAULT,
558 0,
559 };
560
561 static short nfsv3err_commit[] = {
562 NFSERR_IO,
563 NFSERR_IO,
564 NFSERR_STALE,
565 NFSERR_BADHANDLE,
566 NFSERR_SERVERFAULT,
567 0,
568 };
569
570 static short *nfsrv_v3errmap[] = {
571 nfsv3err_null,
572 nfsv3err_getattr,
573 nfsv3err_setattr,
574 nfsv3err_lookup,
575 nfsv3err_access,
576 nfsv3err_readlink,
577 nfsv3err_read,
578 nfsv3err_write,
579 nfsv3err_create,
580 nfsv3err_mkdir,
581 nfsv3err_symlink,
582 nfsv3err_mknod,
583 nfsv3err_remove,
584 nfsv3err_rmdir,
585 nfsv3err_rename,
586 nfsv3err_link,
587 nfsv3err_readdir,
588 nfsv3err_readdirplus,
589 nfsv3err_fsstat,
590 nfsv3err_fsinfo,
591 nfsv3err_pathconf,
592 nfsv3err_commit,
593 };
594
595 #endif /* NFS_NOSERVER */
596
597 extern struct nfsrtt nfsrtt;
598 extern struct nfsstats nfsstats;
599 extern nfstype nfsv2_type[9];
600 extern nfstype nfsv3_type[9];
601 extern struct nfsnodehashhead *nfsnodehashtbl;
602 extern u_long nfsnodehash;
603
604
605 LIST_HEAD(nfsnodehashhead, nfsnode);
606
607 /*
608 * Create the header for an rpc request packet
609 * The hsiz is the size of the rest of the nfs request header.
610 * (just used to decide if a cluster is a good idea)
611 */
612 int
613 nfsm_reqh(int hsiz, caddr_t *bposp, mbuf_t *mbp)
614 {
615 int error;
616
617 *mbp = NULL;
618 if (hsiz >= nfs_mbuf_minclsize)
619 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, mbp);
620 else
621 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, mbp);
622 if (error)
623 return (error);
624 *bposp = mbuf_data(*mbp);
625 return (0);
626 }
627
628 /*
629 * Build the RPC header and fill in the authorization info.
630 * The authorization string argument is only used when the credentials
631 * come from outside of the kernel.
632 * Returns the head of the mbuf list.
633 */
634 int
635 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
636 verf_str, mrest, mrest_len, mbp, xidp, mreqp)
637 kauth_cred_t cr;
638 int nmflag;
639 int procid;
640 int auth_type;
641 int auth_len;
642 char *auth_str;
643 int verf_len;
644 char *verf_str;
645 mbuf_t mrest;
646 int mrest_len;
647 mbuf_t *mbp;
648 u_long *xidp;
649 mbuf_t *mreqp;
650 {
651 mbuf_t mb;
652 u_long *tl;
653 caddr_t bpos;
654 int i, error, len;
655 mbuf_t mreq, mb2;
656 int siz, grpsiz, authsiz, mlen;
657 struct timeval tv;
658
659 authsiz = nfsm_rndup(auth_len);
660 len = authsiz + 10 * NFSX_UNSIGNED;
661 if (len >= nfs_mbuf_minclsize) {
662 error = mbuf_getpacket(MBUF_WAITOK, &mb);
663 } else {
664 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
665 if (!error) {
666 if (len < nfs_mbuf_mhlen)
667 mbuf_align_32(mb, len);
668 else
669 mbuf_align_32(mb, 8 * NFSX_UNSIGNED);
670 }
671 }
672 if (error) {
673 /* unable to allocate packet */
674 /* XXX nfsstat? */
675 return (error);
676 }
677 mreq = mb;
678 bpos = mbuf_data(mb);
679
680 /*
681 * First the RPC header.
682 */
683 nfsm_build(tl, u_long *, 8 * NFSX_UNSIGNED);
684
685 /*
686 * derive initial xid from system time
687 */
688 if (!nfs_xid) {
689 /*
690 * Note: it's OK if this code inits nfs_xid to 0 (for example,
691 * due to a broken clock) because we immediately increment it
692 * and we guarantee to never use xid 0. So, nfs_xid should only
693 * ever be 0 the first time this function is called.
694 */
695 microtime(&tv);
696 nfs_xid = tv.tv_sec << 12;
697 }
698 /*
699 * Skip zero xid if it should ever happen.
700 */
701 if (++nfs_xid == 0) {
702 nfs_xidwrap++;
703 nfs_xid++;
704 }
705
706 *tl++ = *xidp = txdr_unsigned(nfs_xid);
707 *tl++ = rpc_call;
708 *tl++ = rpc_vers;
709 *tl++ = txdr_unsigned(NFS_PROG);
710 if (nmflag & NFSMNT_NFSV3)
711 *tl++ = txdr_unsigned(NFS_VER3);
712 else
713 *tl++ = txdr_unsigned(NFS_VER2);
714 if (nmflag & NFSMNT_NFSV3)
715 *tl++ = txdr_unsigned(procid);
716 else
717 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
718
719 /*
720 * And then the authorization cred.
721 */
722 *tl++ = txdr_unsigned(auth_type);
723 *tl = txdr_unsigned(authsiz);
724 switch (auth_type) {
725 case RPCAUTH_UNIX:
726 nfsm_build(tl, u_long *, auth_len);
727 *tl++ = 0; /* stamp ?? */
728 *tl++ = 0; /* NULL hostname */
729 *tl++ = txdr_unsigned(kauth_cred_getuid(cr));
730 *tl++ = txdr_unsigned(cr->cr_groups[0]);
731 grpsiz = (auth_len >> 2) - 5;
732 *tl++ = txdr_unsigned(grpsiz);
733 for (i = 1; i <= grpsiz; i++)
734 *tl++ = txdr_unsigned(cr->cr_groups[i]);
735 break;
736 case RPCAUTH_KERB4:
737 siz = auth_len;
738 mlen = mbuf_len(mb);
739 while (siz > 0) {
740 if (mbuf_trailingspace(mb) == 0) {
741 mb2 = NULL;
742 if (siz >= nfs_mbuf_minclsize)
743 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
744 else
745 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
746 if (!error)
747 error = mbuf_setnext(mb, mb2);
748 if (error) {
749 mbuf_freem(mreq);
750 return (error);
751 }
752 mb = mb2;
753 mlen = 0;
754 bpos = mbuf_data(mb);
755 }
756 i = min(siz, mbuf_trailingspace(mb));
757 bcopy(auth_str, bpos, i);
758 mlen += i;
759 mbuf_setlen(mb, mlen);
760 auth_str += i;
761 bpos += i;
762 siz -= i;
763 }
764 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
765 for (i = 0; i < siz; i++)
766 *bpos++ = '\0';
767 mlen += siz;
768 mbuf_setlen(mb, mlen);
769 }
770 break;
771 };
772
773 /*
774 * And the verifier...
775 */
776 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
777 if (verf_str) {
778 mlen = mbuf_len(mb);
779 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
780 *tl = txdr_unsigned(verf_len);
781 siz = verf_len;
782 while (siz > 0) {
783 if (mbuf_trailingspace(mb) == 0) {
784 mb2 = NULL;
785 if (siz >= nfs_mbuf_minclsize)
786 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
787 else
788 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
789 if (!error)
790 error = mbuf_setnext(mb, mb2);
791 if (error) {
792 mbuf_freem(mreq);
793 return (error);
794 }
795 mb = mb2;
796 mlen = 0;
797 bpos = mbuf_data(mb);
798 }
799 i = min(siz, mbuf_trailingspace(mb));
800 bcopy(verf_str, bpos, i);
801 mlen += i;
802 mbuf_setlen(mb, mlen);
803 verf_str += i;
804 bpos += i;
805 siz -= i;
806 }
807 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
808 for (i = 0; i < siz; i++)
809 *bpos++ = '\0';
810 mlen += siz;
811 mbuf_setlen(mb, mlen);
812 }
813 } else {
814 *tl++ = txdr_unsigned(RPCAUTH_NULL);
815 *tl = 0;
816 }
817 error = mbuf_pkthdr_setrcvif(mreq, 0);
818 if (!error)
819 error = mbuf_setnext(mb, mrest);
820 if (error) {
821 mbuf_freem(mreq);
822 return (error);
823 }
824 mbuf_pkthdr_setlen(mreq, authsiz + 10 * NFSX_UNSIGNED + mrest_len);
825 *mbp = mb;
826 *mreqp = mreq;
827 return (0);
828 }
829
830 /*
831 * copies mbuf chain to the uio scatter/gather list
832 */
833 int
834 nfsm_mbuftouio(mrep, uiop, siz, dpos)
835 mbuf_t *mrep;
836 struct uio *uiop;
837 int siz;
838 caddr_t *dpos;
839 {
840 char *mbufcp, *uiocp;
841 int xfer, left, len;
842 mbuf_t mp;
843 long uiosiz, rem;
844 int error = 0;
845
846 mp = *mrep;
847 mbufcp = *dpos;
848 len = (caddr_t)mbuf_data(mp) + mbuf_len(mp) - mbufcp;
849 rem = nfsm_rndup(siz)-siz;
850 while (siz > 0) {
851 if (uiop->uio_iovcnt <= 0 || uiop->uio_iovs.iov32p == NULL)
852 return (EFBIG);
853 // LP64todo - fix this!
854 left = uio_iov_len(uiop);
855 uiocp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
856 if (left > siz)
857 left = siz;
858 uiosiz = left;
859 while (left > 0) {
860 while (len == 0) {
861 mp = mbuf_next(mp);
862 if (mp == NULL)
863 return (EBADRPC);
864 mbufcp = mbuf_data(mp);
865 len = mbuf_len(mp);
866 }
867 xfer = (left > len) ? len : left;
868 if (UIO_SEG_IS_USER_SPACE(uiop->uio_segflg))
869 copyout(mbufcp, CAST_USER_ADDR_T(uiocp), xfer);
870 else
871 bcopy(mbufcp, uiocp, xfer);
872 left -= xfer;
873 len -= xfer;
874 mbufcp += xfer;
875 uiocp += xfer;
876 uiop->uio_offset += xfer;
877 uio_uio_resid_add(uiop, -xfer);
878 }
879 if (uio_iov_len(uiop) <= (size_t)siz) {
880 uiop->uio_iovcnt--;
881 uio_next_iov(uiop);
882 } else {
883 uio_iov_base_add(uiop, uiosiz);
884 uio_iov_len_add(uiop, -uiosiz);
885 }
886 siz -= uiosiz;
887 }
888 *dpos = mbufcp;
889 *mrep = mp;
890 if (rem > 0) {
891 if (len < rem)
892 error = nfs_adv(mrep, dpos, rem, len);
893 else
894 *dpos += rem;
895 }
896 return (error);
897 }
898
899 /*
900 * copies a uio scatter/gather list to an mbuf chain.
901 * NOTE: can ony handle iovcnt == 1
902 */
903 int
904 nfsm_uiotombuf(uiop, mq, siz, bpos)
905 struct uio *uiop;
906 mbuf_t *mq;
907 int siz;
908 caddr_t *bpos;
909 {
910 char *uiocp;
911 mbuf_t mp, mp2;
912 int xfer, left, mlen, mplen;
913 int uiosiz, clflg, rem, error;
914 char *cp;
915
916 if (uiop->uio_iovcnt != 1)
917 panic("nfsm_uiotombuf: iovcnt != 1");
918
919 if (siz > nfs_mbuf_mlen) /* or should it >= MCLBYTES ?? */
920 clflg = 1;
921 else
922 clflg = 0;
923 rem = nfsm_rndup(siz)-siz;
924 mp = mp2 = *mq;
925 mplen = mbuf_len(mp);
926 while (siz > 0) {
927 // LP64todo - fix this!
928 left = uio_iov_len(uiop);
929 uiocp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
930 if (left > siz)
931 left = siz;
932 uiosiz = left;
933 while (left > 0) {
934 mlen = mbuf_trailingspace(mp);
935 if (mlen == 0) {
936 mp = NULL;
937 if (clflg)
938 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
939 else
940 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
941 if (!error)
942 error = mbuf_setnext(mp2, mp);
943 if (error)
944 return (error);
945 mplen = 0;
946 mp2 = mp;
947 mlen = mbuf_trailingspace(mp);
948 }
949 xfer = (left > mlen) ? mlen : left;
950 if (UIO_SEG_IS_USER_SPACE(uiop->uio_segflg))
951 copyin(CAST_USER_ADDR_T(uiocp), (caddr_t)mbuf_data(mp) + mplen, xfer);
952 else
953 bcopy(uiocp, (caddr_t)mbuf_data(mp) + mplen, xfer);
954 mplen += xfer;
955 mbuf_setlen(mp, mplen);
956 left -= xfer;
957 uiocp += xfer;
958 uiop->uio_offset += xfer;
959 uio_uio_resid_add(uiop, -xfer);
960 }
961 uio_iov_base_add(uiop, uiosiz);
962 uio_iov_len_add(uiop, -uiosiz);
963 siz -= uiosiz;
964 }
965 if (rem > 0) {
966 if (rem > mbuf_trailingspace(mp)) {
967 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
968 if (!error)
969 error = mbuf_setnext(mp2, mp);
970 if (error)
971 return (error);
972 mplen = 0;
973 }
974 cp = (caddr_t)mbuf_data(mp) + mplen;
975 for (left = 0; left < rem; left++)
976 *cp++ = '\0';
977 mplen += rem;
978 mbuf_setlen(mp, mplen);
979 *bpos = cp;
980 } else {
981 *bpos = (caddr_t)mbuf_data(mp) + mplen;
982 }
983 *mq = mp;
984 return (0);
985 }
986
987 /*
988 * Help break down an mbuf chain by setting the first siz bytes contiguous
989 * pointed to by returned val.
990 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
991 * cases. (The macros use the vars. dpos and dpos2)
992 */
993 int
994 nfsm_disct(mdp, dposp, siz, left, cp2)
995 mbuf_t *mdp;
996 caddr_t *dposp;
997 int siz;
998 int left;
999 caddr_t *cp2;
1000 {
1001 mbuf_t mp, mp2;
1002 int siz2, xfer, error, mp2len;
1003 caddr_t p, mp2data;
1004
1005 mp = *mdp;
1006 while (left == 0) {
1007 *mdp = mp = mbuf_next(mp);
1008 if (mp == NULL)
1009 return (EBADRPC);
1010 left = mbuf_len(mp);
1011 *dposp = mbuf_data(mp);
1012 }
1013 if (left >= siz) {
1014 *cp2 = *dposp;
1015 *dposp += siz;
1016 } else if (mbuf_next(mp) == NULL) {
1017 return (EBADRPC);
1018 } else if (siz > nfs_mbuf_mhlen) {
1019 panic("nfs S too big");
1020 } else {
1021 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp2);
1022 if (error)
1023 return (error);
1024 error = mbuf_setnext(mp2, mbuf_next(mp));
1025 if (!error)
1026 error = mbuf_setnext(mp, mp2);
1027 if (error) {
1028 mbuf_free(mp2);
1029 return (error);
1030 }
1031 mbuf_setlen(mp, mbuf_len(mp) - left);
1032 mp = mp2;
1033 *cp2 = p = mbuf_data(mp);
1034 bcopy(*dposp, p, left); /* Copy what was left */
1035 siz2 = siz-left;
1036 p += left;
1037 mp2 = mbuf_next(mp);
1038 mp2data = mbuf_data(mp2);
1039 mp2len = mbuf_len(mp2);
1040 /* Loop around copying up the siz2 bytes */
1041 while (siz2 > 0) {
1042 if (mp2 == NULL)
1043 return (EBADRPC);
1044 xfer = (siz2 > mp2len) ? mp2len : siz2;
1045 if (xfer > 0) {
1046 bcopy(mp2data, p, xfer);
1047 mp2data += xfer;
1048 mp2len -= xfer;
1049 mbuf_setdata(mp2, mp2data, mp2len);
1050 p += xfer;
1051 siz2 -= xfer;
1052 }
1053 if (siz2 > 0) {
1054 mp2 = mbuf_next(mp2);
1055 mp2data = mbuf_data(mp2);
1056 mp2len = mbuf_len(mp2);
1057 }
1058 }
1059 mbuf_setlen(mp, siz);
1060 *mdp = mp2;
1061 *dposp = mp2data;
1062 }
1063 return (0);
1064 }
1065
1066 /*
1067 * Advance the position in the mbuf chain.
1068 */
1069 int
1070 nfs_adv(mdp, dposp, offs, left)
1071 mbuf_t *mdp;
1072 caddr_t *dposp;
1073 int offs;
1074 int left;
1075 {
1076 mbuf_t m;
1077 int s;
1078
1079 m = *mdp;
1080 s = left;
1081 while (s < offs) {
1082 offs -= s;
1083 m = mbuf_next(m);
1084 if (m == NULL)
1085 return (EBADRPC);
1086 s = mbuf_len(m);
1087 }
1088 *mdp = m;
1089 *dposp = (caddr_t)mbuf_data(m) + offs;
1090 return (0);
1091 }
1092
1093 /*
1094 * Copy a string into mbufs for the hard cases...
1095 */
1096 int
1097 nfsm_strtmbuf(mb, bpos, cp, siz)
1098 mbuf_t *mb;
1099 char **bpos;
1100 char *cp;
1101 long siz;
1102 {
1103 mbuf_t m1 = NULL, m2;
1104 long left, xfer, len, tlen, mlen;
1105 u_long *tl;
1106 int putsize, error;
1107
1108 putsize = 1;
1109 m2 = *mb;
1110 left = mbuf_trailingspace(m2);
1111 if (left >= NFSX_UNSIGNED) {
1112 tl = ((u_long *)(*bpos));
1113 *tl++ = txdr_unsigned(siz);
1114 putsize = 0;
1115 left -= NFSX_UNSIGNED;
1116 len = mbuf_len(m2);
1117 len += NFSX_UNSIGNED;
1118 mbuf_setlen(m2, len);
1119 if (left > 0) {
1120 bcopy(cp, (caddr_t) tl, left);
1121 siz -= left;
1122 cp += left;
1123 len += left;
1124 mbuf_setlen(m2, len);
1125 left = 0;
1126 }
1127 }
1128 /* Loop around adding mbufs */
1129 while (siz > 0) {
1130 m1 = NULL;
1131 if (siz > nfs_mbuf_mlen)
1132 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &m1);
1133 else
1134 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &m1);
1135 if (!error)
1136 error = mbuf_setnext(m2, m1);
1137 if (error)
1138 return (error);
1139 mlen = mbuf_maxlen(m1);
1140 mbuf_setlen(m1, mlen);
1141 m2 = m1;
1142 tl = mbuf_data(m1);
1143 tlen = 0;
1144 if (putsize) {
1145 *tl++ = txdr_unsigned(siz);
1146 mlen -= NFSX_UNSIGNED;
1147 mbuf_setlen(m1, mlen);
1148 tlen = NFSX_UNSIGNED;
1149 putsize = 0;
1150 }
1151 if (siz < mlen) {
1152 len = nfsm_rndup(siz);
1153 xfer = siz;
1154 if (xfer < len)
1155 *(tl+(xfer>>2)) = 0;
1156 } else {
1157 xfer = len = mlen;
1158 }
1159 bcopy(cp, (caddr_t) tl, xfer);
1160 mbuf_setlen(m1, len + tlen);
1161 siz -= xfer;
1162 cp += xfer;
1163 }
1164 *mb = m1;
1165 *bpos = (caddr_t)mbuf_data(m1) + mbuf_len(m1);
1166 return (0);
1167 }
1168
1169 /*
1170 * Called once to initialize data structures...
1171 */
1172 int
1173 nfs_init(struct vfsconf *vfsp)
1174 {
1175 int i;
1176
1177 /*
1178 * Check to see if major data structures haven't bloated.
1179 */
1180 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1181 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1182 printf("Try reducing NFS_SMALLFH\n");
1183 }
1184 if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
1185 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
1186 printf("Try reducing NFS_MUIDHASHSIZ\n");
1187 }
1188 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1189 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1190 printf("Try reducing NFS_UIDHASHSIZ\n");
1191 }
1192 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1193 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1194 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1195 }
1196
1197 nfs_mount_type = vfsp->vfc_typenum;
1198 nfsrtt.pos = 0;
1199 rpc_vers = txdr_unsigned(RPC_VER2);
1200 rpc_call = txdr_unsigned(RPC_CALL);
1201 rpc_reply = txdr_unsigned(RPC_REPLY);
1202 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1203 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1204 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1205 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1206 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1207 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1208 nfs_prog = txdr_unsigned(NFS_PROG);
1209 nfs_true = txdr_unsigned(TRUE);
1210 nfs_false = txdr_unsigned(FALSE);
1211 nfs_xdrneg1 = txdr_unsigned(-1);
1212
1213 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1214 if (nfs_ticks < 1)
1215 nfs_ticks = 1;
1216 /* Ensure async daemons disabled */
1217 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1218 nfs_iodwant[i] = NULL;
1219 nfs_iodmount[i] = (struct nfsmount *)0;
1220 }
1221 /* init nfsiod mutex */
1222 nfs_iod_lck_grp_attr = lck_grp_attr_alloc_init();
1223 lck_grp_attr_setstat(nfs_iod_lck_grp_attr);
1224 nfs_iod_lck_grp = lck_grp_alloc_init("nfs_iod", nfs_iod_lck_grp_attr);
1225 nfs_iod_lck_attr = lck_attr_alloc_init();
1226 nfs_iod_mutex = lck_mtx_alloc_init(nfs_iod_lck_grp, nfs_iod_lck_attr);
1227
1228 nfs_nbinit(); /* Init the nfsbuf table */
1229 nfs_nhinit(); /* Init the nfsnode table */
1230 nfs_lockinit(); /* Init the nfs lock state */
1231
1232 #ifndef NFS_NOSERVER
1233 /* init nfsd mutex */
1234 nfsd_lck_grp_attr = lck_grp_attr_alloc_init();
1235 lck_grp_attr_setstat(nfsd_lck_grp_attr);
1236 nfsd_lck_grp = lck_grp_alloc_init("nfsd", nfsd_lck_grp_attr);
1237 nfsd_lck_attr = lck_attr_alloc_init();
1238 nfsd_mutex = lck_mtx_alloc_init(nfsd_lck_grp, nfsd_lck_attr);
1239
1240 /* init slp rwlock */
1241 nfs_slp_lock_attr = lck_attr_alloc_init();
1242 nfs_slp_group_attr = lck_grp_attr_alloc_init();
1243 nfs_slp_rwlock_group = lck_grp_alloc_init("nfs-slp-rwlock", nfs_slp_group_attr);
1244 nfs_slp_mutex_group = lck_grp_alloc_init("nfs-slp-mutex", nfs_slp_group_attr);
1245
1246 /* init export data structures */
1247 nfsexphashtbl = hashinit(8, M_TEMP, &nfsexphash);
1248 LIST_INIT(&nfs_exports);
1249 nfs_export_lock_attr = lck_attr_alloc_init();
1250 nfs_export_group_attr = lck_grp_attr_alloc_init();
1251 nfs_export_rwlock_group = lck_grp_alloc_init("nfs-export-rwlock", nfs_export_group_attr);
1252 lck_rw_init(&nfs_export_rwlock, nfs_export_rwlock_group, nfs_export_lock_attr);
1253
1254 lck_mtx_lock(nfsd_mutex);
1255 nfsrv_init(0); /* Init server data structures */
1256 nfsrv_initcache(); /* Init the server request cache */
1257 lck_mtx_unlock(nfsd_mutex);
1258 #endif
1259
1260 /*
1261 * Initialize reply list and start timer
1262 */
1263 TAILQ_INIT(&nfs_reqq);
1264
1265 nfs_timer(0);
1266
1267 vfsp->vfc_refcount++; /* make us non-unloadable */
1268 return (0);
1269 }
1270
1271 /*
1272 * initialize NFS's cache of mbuf constants
1273 */
1274 void
1275 nfs_mbuf_init(void)
1276 {
1277 struct mbuf_stat ms;
1278
1279 mbuf_stats(&ms);
1280 nfs_mbuf_mlen = ms.mlen;
1281 nfs_mbuf_mhlen = ms.mhlen;
1282 nfs_mbuf_minclsize = ms.minclsize;
1283 nfs_mbuf_mclbytes = ms.mclbytes;
1284 }
1285
1286 /*
1287 * Parse the attributes that are in the mbuf list and store them in *nvap.
1288 */
1289 int
1290 nfs_parsefattr(mbuf_t *mdp, caddr_t *dposp, int v3, struct nfs_vattr *nvap)
1291 {
1292 struct nfs_fattr *fp;
1293 long t1;
1294 caddr_t cp2;
1295 int error = 0, rdev;
1296 mbuf_t md;
1297 enum vtype vtype;
1298 u_short vmode;
1299
1300 md = *mdp;
1301 t1 = ((caddr_t)mbuf_data(md) + mbuf_len(md)) - *dposp;
1302 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))) {
1303 return (error);
1304 }
1305 fp = (struct nfs_fattr *)cp2;
1306 if (v3) {
1307 vtype = nfsv3tov_type(fp->fa_type);
1308 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1309 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1310 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1311 } else {
1312 vtype = nfsv2tov_type(fp->fa_type);
1313 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1314 /*
1315 * XXX
1316 *
1317 * The duplicate information returned in fa_type and fa_mode
1318 * is an ambiguity in the NFS version 2 protocol.
1319 *
1320 * VREG should be taken literally as a regular file. If a
1321 * server intents to return some type information differently
1322 * in the upper bits of the mode field (e.g. for sockets, or
1323 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1324 * leave the examination of the mode bits even in the VREG
1325 * case to avoid breakage for bogus servers, but we make sure
1326 * that there are actually type bits set in the upper part of
1327 * fa_mode (and failing that, trust the va_type field).
1328 *
1329 * NFSv3 cleared the issue, and requires fa_mode to not
1330 * contain any type information (while also introduing sockets
1331 * and FIFOs for fa_type).
1332 */
1333 if (vtype == VNON || (vtype == VREG && (vmode & S_IFMT) != 0))
1334 vtype = IFTOVT(vmode);
1335 rdev = fxdr_unsigned(long, fp->fa2_rdev);
1336 /*
1337 * Really ugly NFSv2 kludge.
1338 */
1339 if (vtype == VCHR && rdev == (int)0xffffffff)
1340 vtype = VFIFO;
1341 }
1342
1343 nvap->nva_type = vtype;
1344 nvap->nva_mode = (vmode & 07777);
1345 nvap->nva_rdev = (dev_t)rdev;
1346 nvap->nva_nlink = (uint64_t)fxdr_unsigned(u_long, fp->fa_nlink);
1347 nvap->nva_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1348 nvap->nva_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1349 if (v3) {
1350 fxdr_hyper(&fp->fa3_size, &nvap->nva_size);
1351 nvap->nva_blocksize = 16*1024;
1352 fxdr_hyper(&fp->fa3_used, &nvap->nva_bytes);
1353 fxdr_hyper(&fp->fa3_fileid, &nvap->nva_fileid);
1354 fxdr_nfsv3time(&fp->fa3_atime, &nvap->nva_atime);
1355 fxdr_nfsv3time(&fp->fa3_mtime, &nvap->nva_mtime);
1356 fxdr_nfsv3time(&fp->fa3_ctime, &nvap->nva_ctime);
1357 } else {
1358 nvap->nva_size = fxdr_unsigned(u_long, fp->fa2_size);
1359 nvap->nva_blocksize = fxdr_unsigned(long, fp->fa2_blocksize);
1360 nvap->nva_bytes = fxdr_unsigned(long, fp->fa2_blocks) * NFS_FABLKSIZE;
1361 nvap->nva_fileid = (uint64_t)fxdr_unsigned(u_long, fp->fa2_fileid);
1362 fxdr_nfsv2time(&fp->fa2_atime, &nvap->nva_atime);
1363 fxdr_nfsv2time(&fp->fa2_mtime, &nvap->nva_mtime);
1364 fxdr_nfsv2time(&fp->fa2_ctime, &nvap->nva_ctime);
1365 }
1366
1367 return (0);
1368 }
1369
1370 /*
1371 * Load the attribute cache (that lives in the nfsnode entry) with
1372 * the value pointed to by nvap, unless the file type in the attribute
1373 * cache doesn't match the file type in the nvap, in which case log a
1374 * warning and return ESTALE.
1375 *
1376 * If the dontshrink flag is set, then it's not safe to call ubc_setsize()
1377 * to shrink the size of the file.
1378 */
1379 int
1380 nfs_loadattrcache(
1381 struct nfsnode *np,
1382 struct nfs_vattr *nvap,
1383 u_int64_t *xidp,
1384 int dontshrink)
1385 {
1386 mount_t mp;
1387 vnode_t vp;
1388 struct timeval now;
1389 struct nfs_vattr *npnvap;
1390
1391 if (np->n_flag & NINIT) {
1392 vp = NULL;
1393 mp = np->n_mount;
1394 } else {
1395 vp = NFSTOV(np);
1396 mp = vnode_mount(vp);
1397 }
1398
1399 FSDBG_TOP(527, vp, np, *xidp >> 32, *xidp);
1400
1401 if (!VFSTONFS(mp)) {
1402 FSDBG_BOT(527, ENXIO, 1, 0, *xidp);
1403 return (ENXIO);
1404 }
1405
1406 if (*xidp < np->n_xid) {
1407 /*
1408 * We have already updated attributes with a response from
1409 * a later request. The attributes we have here are probably
1410 * stale so we drop them (just return). However, our
1411 * out-of-order receipt could be correct - if the requests were
1412 * processed out of order at the server. Given the uncertainty
1413 * we invalidate our cached attributes. *xidp is zeroed here
1414 * to indicate the attributes were dropped - only getattr
1415 * cares - it needs to retry the rpc.
1416 */
1417 NATTRINVALIDATE(np);
1418 FSDBG_BOT(527, 0, np, np->n_xid, *xidp);
1419 *xidp = 0;
1420 return (0);
1421 }
1422
1423 if (vp && (nvap->nva_type != vnode_vtype(vp))) {
1424 /*
1425 * The filehandle has changed type on us. This can be
1426 * caused by either the server not having unique filehandles
1427 * or because another client has removed the previous
1428 * filehandle and a new object (of a different type)
1429 * has been created with the same filehandle.
1430 *
1431 * We can't simply switch the type on the vnode because
1432 * there may be type-specific fields that need to be
1433 * cleaned up or set up.
1434 *
1435 * So, what should we do with this vnode?
1436 *
1437 * About the best we can do is log a warning and return
1438 * an error. ESTALE is about the closest error, but it
1439 * is a little strange that we come up with this error
1440 * internally instead of simply passing it through from
1441 * the server. Hopefully, the vnode will be reclaimed
1442 * soon so the filehandle can be reincarnated as the new
1443 * object type.
1444 */
1445 printf("nfs loadattrcache vnode changed type, was %d now %d\n",
1446 vnode_vtype(vp), nvap->nva_type);
1447 FSDBG_BOT(527, ESTALE, 3, 0, *xidp);
1448 return (ESTALE);
1449 }
1450
1451 microuptime(&now);
1452 np->n_attrstamp = now.tv_sec;
1453 np->n_xid = *xidp;
1454
1455 npnvap = &np->n_vattr;
1456 nvap->nva_fsid = vfs_statfs(mp)->f_fsid.val[0];
1457 bcopy((caddr_t)nvap, (caddr_t)npnvap, sizeof(*nvap));
1458
1459 if (vp) {
1460 if (nvap->nva_size != np->n_size) {
1461 FSDBG(527, vp, nvap->nva_size, np->n_size,
1462 (nvap->nva_type == VREG) |
1463 (np->n_flag & NMODIFIED ? 6 : 4));
1464 if (nvap->nva_type == VREG) {
1465 u_quad_t orig_size = np->n_size;
1466 if (np->n_flag & NMODIFIED) {
1467 if (nvap->nva_size < np->n_size)
1468 nvap->nva_size = np->n_size;
1469 else
1470 np->n_size = nvap->nva_size;
1471 } else
1472 np->n_size = nvap->nva_size;
1473 if (!UBCINFOEXISTS(vp) ||
1474 (dontshrink && np->n_size < (u_quad_t)ubc_getsize(vp))) {
1475 nvap->nva_size = np->n_size = orig_size;
1476 NATTRINVALIDATE(np);
1477 } else {
1478 ubc_setsize(vp, (off_t)np->n_size); /* XXX */
1479 }
1480 } else
1481 np->n_size = nvap->nva_size;
1482 }
1483 } else {
1484 np->n_size = nvap->nva_size;
1485 }
1486
1487 if (np->n_flag & NCHG) {
1488 if (np->n_flag & NACC)
1489 nvap->nva_atime = np->n_atim;
1490 if (np->n_flag & NUPD)
1491 nvap->nva_mtime = np->n_mtim;
1492 }
1493
1494 FSDBG_BOT(527, 0, np, 0, *xidp);
1495 return (0);
1496 }
1497
1498 /*
1499 * Calculate the attribute timeout based on
1500 * how recently the file has been modified.
1501 */
1502 int
1503 nfs_attrcachetimeout(vnode_t vp)
1504 {
1505 struct nfsnode *np = VTONFS(vp);
1506 struct nfsmount *nmp;
1507 struct timeval now;
1508 int isdir, timeo;
1509
1510 if (!(nmp = VFSTONFS(vnode_mount(vp))))
1511 return (0);
1512
1513 isdir = vnode_isdir(vp);
1514
1515 if ((np)->n_flag & NMODIFIED)
1516 timeo = isdir ? nmp->nm_acdirmin : nmp->nm_acregmin;
1517 else {
1518 /* Note that if the client and server clocks are way out of sync, */
1519 /* timeout will probably get clamped to a min or max value */
1520 microtime(&now);
1521 timeo = (now.tv_sec - (np)->n_mtime.tv_sec) / 10;
1522 if (isdir) {
1523 if (timeo < nmp->nm_acdirmin)
1524 timeo = nmp->nm_acdirmin;
1525 else if (timeo > nmp->nm_acdirmax)
1526 timeo = nmp->nm_acdirmax;
1527 } else {
1528 if (timeo < nmp->nm_acregmin)
1529 timeo = nmp->nm_acregmin;
1530 else if (timeo > nmp->nm_acregmax)
1531 timeo = nmp->nm_acregmax;
1532 }
1533 }
1534
1535 return (timeo);
1536 }
1537
1538 /*
1539 * Check the time stamp
1540 * If the cache is valid, copy contents to *nvaper and return 0
1541 * otherwise return an error
1542 */
1543 int
1544 nfs_getattrcache(vp, nvaper)
1545 vnode_t vp;
1546 struct nfs_vattr *nvaper;
1547 {
1548 struct nfsnode *np = VTONFS(vp);
1549 struct nfs_vattr *nvap;
1550 struct timeval nowup;
1551 int32_t timeo;
1552
1553 if (!NATTRVALID(np)) {
1554 FSDBG(528, vp, 0, 0, 0);
1555 OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_misses);
1556 return (ENOENT);
1557 }
1558
1559 timeo = nfs_attrcachetimeout(vp);
1560
1561 microuptime(&nowup);
1562 if ((nowup.tv_sec - np->n_attrstamp) >= timeo) {
1563 FSDBG(528, vp, 0, 0, 1);
1564 OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_misses);
1565 return (ENOENT);
1566 }
1567 FSDBG(528, vp, 0, 0, 2);
1568 OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_hits);
1569 nvap = &np->n_vattr;
1570
1571 if (nvap->nva_size != np->n_size) {
1572 FSDBG(528, vp, nvap->nva_size, np->n_size,
1573 (nvap->nva_type == VREG) |
1574 (np->n_flag & NMODIFIED ? 6 : 4));
1575 if (nvap->nva_type == VREG) {
1576 if (np->n_flag & NMODIFIED) {
1577 if (nvap->nva_size < np->n_size)
1578 nvap->nva_size = np->n_size;
1579 else
1580 np->n_size = nvap->nva_size;
1581 } else
1582 np->n_size = nvap->nva_size;
1583 ubc_setsize(vp, (off_t)np->n_size); /* XXX */
1584 } else
1585 np->n_size = nvap->nva_size;
1586 }
1587
1588 bcopy((caddr_t)nvap, (caddr_t)nvaper, sizeof(struct nfs_vattr));
1589 if (np->n_flag & NCHG) {
1590 if (np->n_flag & NACC)
1591 nvaper->nva_atime = np->n_atim;
1592 if (np->n_flag & NUPD)
1593 nvaper->nva_mtime = np->n_mtim;
1594 }
1595 return (0);
1596 }
1597
1598 #ifndef NFS_NOSERVER
1599 /*
1600 * Extract a lookup path from the given mbufs and store it in
1601 * a newly allocated buffer saved in the given nameidata structure.
1602 * exptected string length given as *lenp and final string length
1603 * (after any WebNFS processing) is returned in *lenp.
1604 */
1605 int
1606 nfsm_path_mbuftond(
1607 mbuf_t *mdp,
1608 caddr_t *dposp,
1609 __unused int v3,
1610 __unused int pubflag,
1611 int* lenp,
1612 struct nameidata *ndp)
1613 {
1614 int i, len, len2, rem, error = 0;
1615 mbuf_t md;
1616 char *fromcp, *tocp;
1617 struct componentname *cnp = &ndp->ni_cnd;
1618 /* XXX Revisit when enabling WebNFS */
1619 #ifdef WEBNFS_ENABLED
1620 int webcnt = 0, digitcnt = 0;
1621 char hexdigits[2];
1622 #endif
1623
1624 len = *lenp;
1625 if (len > (MAXPATHLEN - 1))
1626 return (ENAMETOOLONG);
1627
1628 /*
1629 * Get a buffer for the name to be translated, and copy the
1630 * name into the buffer.
1631 */
1632 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
1633 if (!cnp->cn_pnbuf)
1634 return (ENOMEM);
1635 cnp->cn_pnlen = MAXPATHLEN;
1636 cnp->cn_flags |= HASBUF;
1637
1638 /*
1639 * Copy the name from the mbuf list to the string
1640 *
1641 * Along the way, take note of any WebNFS characters
1642 * and convert any % escapes.
1643 */
1644 fromcp = *dposp;
1645 tocp = cnp->cn_pnbuf;
1646 md = *mdp;
1647 rem = (caddr_t)mbuf_data(md) + mbuf_len(md) - fromcp;
1648 for (i = 1; i <= len; i++) {
1649 while (rem == 0) {
1650 md = mbuf_next(md);
1651 if (md == NULL) {
1652 error = EBADRPC;
1653 goto out;
1654 }
1655 fromcp = mbuf_data(md);
1656 rem = mbuf_len(md);
1657 }
1658 /* XXX Revisit when enabling WebNFS */
1659 #ifdef WEBNFS_ENABLED
1660 if (pubflag) {
1661 if ((i == 1) && ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START)) {
1662 switch ((unsigned char)*fromcp) {
1663 case WEBNFS_NATIVE_CHAR:
1664 /*
1665 * 'Native' path for us is the same
1666 * as a path according to the NFS spec,
1667 * just skip the escape char.
1668 */
1669 webcnt++;
1670 fromcp++;
1671 rem--;
1672 /* next iteration of for loop */
1673 continue;
1674 /*
1675 * More may be added in the future, range 0x80-0xff.
1676 * Don't currently support security query lookup (0x81).
1677 */
1678 default:
1679 error = EIO;
1680 goto out;
1681 }
1682 }
1683 if (digitcnt) {
1684 /* We're expecting hex digits */
1685 if (!ISHEX(*fromcp)) {
1686 error = ENOENT;
1687 goto out;
1688 }
1689 digitcnt--;
1690 hexdigits[digitcnt ? 0 : 1] = *fromcp++;
1691 if (!digitcnt)
1692 *tocp++ = HEXSTRTOI(hexdigits);
1693 rem--;
1694 /* next iteration of for loop */
1695 continue;
1696 } else if (*fromcp == WEBNFS_ESC_CHAR) {
1697 /*
1698 * We can't really look at the next couple
1699 * bytes here safely/easily, so we note that
1700 * the next two characters should be hex
1701 * digits and later save them in hexdigits[].
1702 * When we've got both, we'll convert it.
1703 */
1704 digitcnt = 2;
1705 webcnt += 2;
1706 fromcp++;
1707 rem--;
1708 /* next iteration of for loop */
1709 continue;
1710 }
1711 }
1712 if (*fromcp == '\0' || (!pubflag && *fromcp == '/'))
1713 #else
1714 if (*fromcp == '\0' || *fromcp == '/')
1715 #endif
1716 {
1717 error = EACCES;
1718 goto out;
1719 }
1720 *tocp++ = *fromcp++;
1721 rem--;
1722 }
1723 *tocp = '\0';
1724 *mdp = md;
1725 *dposp = fromcp;
1726 len2 = nfsm_rndup(len)-len;
1727 if (len2 > 0) {
1728 if (rem >= len2)
1729 *dposp += len2;
1730 else if ((error = nfs_adv(mdp, dposp, len2, rem)) != 0)
1731 goto out;
1732 }
1733
1734 /* XXX Revisit when enabling WebNFS */
1735 #ifdef WEBNFS_ENABLED
1736 if (pubflag) {
1737 if (digitcnt) {
1738 /* The string ended in the middle of an escape! */
1739 error = ENOENT;
1740 goto out;
1741 }
1742 len -= webcnt;
1743 }
1744 #endif
1745
1746 out:
1747 if (error) {
1748 if (cnp->cn_pnbuf)
1749 FREE_ZONE(cnp->cn_pnbuf, MAXPATHLEN, M_NAMEI);
1750 cnp->cn_flags &= ~HASBUF;
1751 } else {
1752 ndp->ni_pathlen = len;
1753 *lenp = len;
1754 }
1755 return (error);
1756 }
1757
1758 /*
1759 * Set up nameidata for a lookup() call and do it.
1760 *
1761 * If pubflag is set, this call is done for a lookup operation on the
1762 * public filehandle. In that case we allow crossing mountpoints and
1763 * absolute pathnames. However, the caller is expected to check that
1764 * the lookup result is within the public fs, and deny access if
1765 * it is not.
1766 */
1767 int
1768 nfs_namei(
1769 struct nfsrv_descript *nfsd,
1770 struct vfs_context *ctx,
1771 struct nameidata *ndp,
1772 struct nfs_filehandle *nfhp,
1773 mbuf_t nam,
1774 int pubflag,
1775 vnode_t *retdirp,
1776 struct nfs_export **nxp,
1777 struct nfs_export_options **nxop)
1778 {
1779 /* XXX Revisit when enabling WebNFS */
1780 #ifdef WEBNFS_ENABLED
1781 char *cp;
1782 uio_t auio;
1783 char uio_buf[ UIO_SIZEOF(1) ];
1784 int linklen, olen = ndp->ni_pathlen;
1785 #endif
1786 vnode_t dp;
1787 int error;
1788 struct componentname *cnp = &ndp->ni_cnd;
1789 char *tmppn;
1790
1791 *retdirp = NULL;
1792
1793 /*
1794 * Extract and set starting directory.
1795 */
1796 error = nfsrv_fhtovp(nfhp, nam, pubflag, &dp, nxp, nxop);
1797 if (error)
1798 goto out;
1799 error = nfsrv_credcheck(nfsd, *nxp, *nxop);
1800 if (error || (vnode_vtype(dp) != VDIR)) {
1801 vnode_put(dp);
1802 error = ENOTDIR;
1803 goto out;
1804 }
1805
1806 ctx->vc_ucred = nfsd->nd_cr;
1807 ndp->ni_cnd.cn_context = ctx;
1808
1809 if (*nxop && ((*nxop)->nxo_flags & NX_READONLY))
1810 cnp->cn_flags |= RDONLY;
1811
1812 *retdirp = dp;
1813
1814 /* XXX Revisit when enabling WebNFS */
1815 #ifdef WEBNFS_ENABLED
1816 if (pubflag) {
1817 ndp->ni_rootdir = rootvnode;
1818 ndp->ni_loopcnt = 0;
1819 if (cnp->cn_pnbuf[0] == '/') {
1820 vnode_put(dp);
1821 dp = rootvnode;
1822 error = vnode_get(dp);
1823 if (error) {
1824 *retdirp = NULL;
1825 goto out;
1826 }
1827 }
1828 } else {
1829 cnp->cn_flags |= NOCROSSMOUNT;
1830 }
1831 #else
1832 cnp->cn_flags |= NOCROSSMOUNT;
1833 #endif
1834
1835 ndp->ni_usedvp = dp;
1836
1837 for (;;) {
1838 cnp->cn_nameptr = cnp->cn_pnbuf;
1839 ndp->ni_startdir = dp;
1840 /*
1841 * And call lookup() to do the real work
1842 */
1843 error = lookup(ndp);
1844 if (error)
1845 break;
1846 /*
1847 * Check for encountering a symbolic link
1848 */
1849 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1850 return (0);
1851 } else {
1852 if ((cnp->cn_flags & FSNODELOCKHELD)) {
1853 cnp->cn_flags &= ~FSNODELOCKHELD;
1854 unlock_fsnode(ndp->ni_dvp, NULL);
1855 }
1856 /* XXX Revisit when enabling WebNFS */
1857 #ifdef WEBNFS_ENABLED
1858 if (!pubflag) {
1859 #endif
1860 if (cnp->cn_flags & (LOCKPARENT | WANTPARENT))
1861 vnode_put(ndp->ni_dvp);
1862 if (ndp->ni_vp) {
1863 vnode_put(ndp->ni_vp);
1864 ndp->ni_vp = NULL;
1865 }
1866 error = EINVAL;
1867 break;
1868 /* XXX Revisit when enabling WebNFS */
1869 #ifdef WEBNFS_ENABLED
1870 }
1871
1872 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1873 vnode_put(ndp->ni_vp);
1874 ndp->ni_vp = NULL;
1875 error = ELOOP;
1876 break;
1877 }
1878 /* XXX assert(olen <= MAXPATHLEN - 1); */
1879 if (ndp->ni_pathlen > 1) {
1880 MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1881 if (!cp) {
1882 vnode_put(ndp->ni_vp);
1883 ndp->ni_vp = NULL;
1884 error = ENOMEM;
1885 break;
1886 }
1887 } else {
1888 cp = cnp->cn_pnbuf;
1889 }
1890 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
1891 &uio_buf[0], sizeof(uio_buf));
1892 if (!auio) {
1893 vnode_put(ndp->ni_vp);
1894 ndp->ni_vp = NULL;
1895 if (ndp->ni_pathlen > 1)
1896 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1897 error = ENOMEM;
1898 break;
1899 }
1900 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1901 error = VNOP_READLINK(ndp->ni_vp, auio, cnp->cn_context);
1902 if (error) {
1903 badlink:
1904 vnode_put(ndp->ni_vp);
1905 ndp->ni_vp = NULL;
1906 if (ndp->ni_pathlen > 1)
1907 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1908 break;
1909 }
1910 linklen = MAXPATHLEN - uio_resid(auio);
1911 if (linklen == 0) {
1912 error = ENOENT;
1913 goto badlink;
1914 }
1915 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1916 error = ENAMETOOLONG;
1917 goto badlink;
1918 }
1919 if (ndp->ni_pathlen > 1) {
1920 long len = cnp->cn_pnlen;
1921 tmppn = cnp->cn_pnbuf;
1922 cnp->cn_pnbuf = cp;
1923 cnp->cn_pnlen = olen + 1;
1924 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1925 FREE_ZONE(tmppn, len, M_NAMEI);
1926 } else
1927 cnp->cn_pnbuf[linklen] = '\0';
1928 ndp->ni_pathlen += linklen;
1929
1930 vnode_put(ndp->ni_vp);
1931 dp = ndp->ni_dvp;
1932 ndp->ni_dvp = NULL;
1933
1934 /*
1935 * Check if root directory should replace current directory.
1936 */
1937 if (cnp->cn_pnbuf[0] == '/') {
1938 vnode_put(dp);
1939 dp = ndp->ni_rootdir;
1940 error = vnode_get(dp);
1941 if (error)
1942 break;
1943 }
1944 #endif
1945 }
1946 }
1947 out:
1948 tmppn = cnp->cn_pnbuf;
1949 cnp->cn_pnbuf = NULL;
1950 cnp->cn_flags &= ~HASBUF;
1951 FREE_ZONE(tmppn, cnp->cn_pnlen, M_NAMEI);
1952
1953 return (error);
1954 }
1955
1956 /*
1957 * A fiddled version of m_adj() that ensures null fill to a long
1958 * boundary and only trims off the back end
1959 */
1960 void
1961 nfsm_adj(mp, len, nul)
1962 mbuf_t mp;
1963 int len;
1964 int nul;
1965 {
1966 mbuf_t m, mnext;
1967 int count, i, mlen;
1968 char *cp;
1969
1970 /*
1971 * Trim from tail. Scan the mbuf chain,
1972 * calculating its length and finding the last mbuf.
1973 * If the adjustment only affects this mbuf, then just
1974 * adjust and return. Otherwise, rescan and truncate
1975 * after the remaining size.
1976 */
1977 count = 0;
1978 m = mp;
1979 for (;;) {
1980 mlen = mbuf_len(m);
1981 count += mlen;
1982 mnext = mbuf_next(m);
1983 if (mnext == NULL)
1984 break;
1985 m = mnext;
1986 }
1987 if (mlen > len) {
1988 mlen -= len;
1989 mbuf_setlen(m, mlen);
1990 if (nul > 0) {
1991 cp = (caddr_t)mbuf_data(m) + mlen - nul;
1992 for (i = 0; i < nul; i++)
1993 *cp++ = '\0';
1994 }
1995 return;
1996 }
1997 count -= len;
1998 if (count < 0)
1999 count = 0;
2000 /*
2001 * Correct length for chain is "count".
2002 * Find the mbuf with last data, adjust its length,
2003 * and toss data from remaining mbufs on chain.
2004 */
2005 for (m = mp; m; m = mbuf_next(m)) {
2006 mlen = mbuf_len(m);
2007 if (mlen >= count) {
2008 mlen = count;
2009 mbuf_setlen(m, count);
2010 if (nul > 0) {
2011 cp = (caddr_t)mbuf_data(m) + mlen - nul;
2012 for (i = 0; i < nul; i++)
2013 *cp++ = '\0';
2014 }
2015 break;
2016 }
2017 count -= mlen;
2018 }
2019 for (m = mbuf_next(m); m; m = mbuf_next(m))
2020 mbuf_setlen(m, 0);
2021 }
2022
2023 /*
2024 * Make these functions instead of macros, so that the kernel text size
2025 * doesn't get too big...
2026 */
2027 void
2028 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2029 struct nfsrv_descript *nfsd;
2030 int before_ret;
2031 struct vnode_attr *before_vap;
2032 int after_ret;
2033 struct vnode_attr *after_vap;
2034 mbuf_t *mbp;
2035 char **bposp;
2036 {
2037 mbuf_t mb = *mbp, mb2;
2038 char *bpos = *bposp;
2039 u_long *tl;
2040
2041 if (before_ret) {
2042 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
2043 *tl = nfs_false;
2044 } else {
2045 nfsm_build(tl, u_long *, 7 * NFSX_UNSIGNED);
2046 *tl++ = nfs_true;
2047 txdr_hyper(&(before_vap->va_data_size), tl);
2048 tl += 2;
2049 txdr_nfsv3time(&(before_vap->va_modify_time), tl);
2050 tl += 2;
2051 txdr_nfsv3time(&(before_vap->va_change_time), tl);
2052 }
2053 *bposp = bpos;
2054 *mbp = mb;
2055 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2056 }
2057
2058 void
2059 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2060 struct nfsrv_descript *nfsd;
2061 int after_ret;
2062 struct vnode_attr *after_vap;
2063 mbuf_t *mbp;
2064 char **bposp;
2065 {
2066 mbuf_t mb = *mbp, mb2;
2067 char *bpos = *bposp;
2068 u_long *tl;
2069 struct nfs_fattr *fp;
2070
2071 if (after_ret) {
2072 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
2073 *tl = nfs_false;
2074 } else {
2075 nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FATTR);
2076 *tl++ = nfs_true;
2077 fp = (struct nfs_fattr *)tl;
2078 nfsm_srvfattr(nfsd, after_vap, fp);
2079 }
2080 *mbp = mb;
2081 *bposp = bpos;
2082 }
2083
2084 void
2085 nfsm_srvfattr(nfsd, vap, fp)
2086 struct nfsrv_descript *nfsd;
2087 struct vnode_attr *vap;
2088 struct nfs_fattr *fp;
2089 {
2090
2091 // XXX Should we assert here that all fields are supported?
2092
2093 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2094 fp->fa_uid = txdr_unsigned(vap->va_uid);
2095 fp->fa_gid = txdr_unsigned(vap->va_gid);
2096 if (nfsd->nd_flag & ND_NFSV3) {
2097 fp->fa_type = vtonfsv3_type(vap->va_type);
2098 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2099 txdr_hyper(&vap->va_data_size, &fp->fa3_size);
2100 txdr_hyper(&vap->va_data_alloc, &fp->fa3_used);
2101 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2102 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2103 fp->fa3_fsid.nfsuquad[0] = 0;
2104 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2105 txdr_hyper(&vap->va_fileid, &fp->fa3_fileid);
2106 txdr_nfsv3time(&vap->va_access_time, &fp->fa3_atime);
2107 txdr_nfsv3time(&vap->va_modify_time, &fp->fa3_mtime);
2108 txdr_nfsv3time(&vap->va_change_time, &fp->fa3_ctime);
2109 } else {
2110 fp->fa_type = vtonfsv2_type(vap->va_type);
2111 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2112 fp->fa2_size = txdr_unsigned(vap->va_data_size);
2113 fp->fa2_blocksize = txdr_unsigned(vap->va_iosize);
2114 if (vap->va_type == VFIFO)
2115 fp->fa2_rdev = 0xffffffff;
2116 else
2117 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2118 fp->fa2_blocks = txdr_unsigned(vap->va_data_alloc / NFS_FABLKSIZE);
2119 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2120 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2121 txdr_nfsv2time(&vap->va_access_time, &fp->fa2_atime);
2122 txdr_nfsv2time(&vap->va_modify_time, &fp->fa2_mtime);
2123 txdr_nfsv2time(&vap->va_change_time, &fp->fa2_ctime);
2124 }
2125 }
2126
2127 /*
2128 * Build hash lists of net addresses and hang them off the NFS export.
2129 * Called by nfsrv_export() to set up the lists of export addresses.
2130 */
2131 static int
2132 nfsrv_hang_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
2133 {
2134 struct nfs_export_net_args nxna;
2135 struct nfs_netopt *no;
2136 struct radix_node_head *rnh;
2137 struct radix_node *rn;
2138 struct sockaddr *saddr, *smask;
2139 struct domain *dom;
2140 int i, error;
2141 unsigned int net;
2142 user_addr_t uaddr;
2143 kauth_cred_t cred;
2144 struct ucred temp_cred;
2145
2146 uaddr = unxa->nxa_nets;
2147 for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
2148 error = copyin(uaddr, &nxna, sizeof(nxna));
2149 if (error)
2150 return (error);
2151
2152 if (nxna.nxna_flags & (NX_MAPROOT|NX_MAPALL)) {
2153 bzero(&temp_cred, sizeof(temp_cred));
2154 temp_cred.cr_uid = nxna.nxna_cred.cr_uid;
2155 temp_cred.cr_ngroups = nxna.nxna_cred.cr_ngroups;
2156 for (i=0; i < nxna.nxna_cred.cr_ngroups && i < NGROUPS; i++)
2157 temp_cred.cr_groups[i] = nxna.nxna_cred.cr_groups[i];
2158
2159 cred = kauth_cred_create(&temp_cred);
2160 if (!cred)
2161 return (ENOMEM);
2162 } else {
2163 cred = NULL;
2164 }
2165
2166 if (nxna.nxna_addr.ss_len == 0) {
2167 /* No address means this is a default/world export */
2168 if (nx->nx_flags & NX_DEFAULTEXPORT)
2169 return (EEXIST);
2170 nx->nx_flags |= NX_DEFAULTEXPORT;
2171 nx->nx_defopt.nxo_flags = nxna.nxna_flags;
2172 nx->nx_defopt.nxo_cred = cred;
2173 nx->nx_expcnt++;
2174 continue;
2175 }
2176
2177 i = sizeof(struct nfs_netopt);
2178 i += nxna.nxna_addr.ss_len + nxna.nxna_mask.ss_len;
2179 MALLOC(no, struct nfs_netopt *, i, M_NETADDR, M_WAITOK);
2180 if (!no)
2181 return (ENOMEM);
2182 bzero(no, sizeof(struct nfs_netopt));
2183 no->no_opt.nxo_flags = nxna.nxna_flags;
2184 no->no_opt.nxo_cred = cred;
2185
2186 saddr = (struct sockaddr *)(no + 1);
2187 bcopy(&nxna.nxna_addr, saddr, nxna.nxna_addr.ss_len);
2188 if (nxna.nxna_mask.ss_len) {
2189 smask = (struct sockaddr *)((caddr_t)saddr + nxna.nxna_addr.ss_len);
2190 bcopy(&nxna.nxna_mask, smask, nxna.nxna_mask.ss_len);
2191 } else {
2192 smask = NULL;
2193 }
2194 i = saddr->sa_family;
2195 if ((rnh = nx->nx_rtable[i]) == 0) {
2196 /*
2197 * Seems silly to initialize every AF when most are not
2198 * used, do so on demand here
2199 */
2200 for (dom = domains; dom; dom = dom->dom_next)
2201 if (dom->dom_family == i && dom->dom_rtattach) {
2202 dom->dom_rtattach((void **)&nx->nx_rtable[i],
2203 dom->dom_rtoffset);
2204 break;
2205 }
2206 if ((rnh = nx->nx_rtable[i]) == 0) {
2207 kauth_cred_rele(cred);
2208 _FREE(no, M_NETADDR);
2209 return (ENOBUFS);
2210 }
2211 }
2212 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, no->no_rnodes);
2213 if (rn == 0) {
2214 /*
2215 * One of the reasons that rnh_addaddr may fail is that
2216 * the entry already exists. To check for this case, we
2217 * look up the entry to see if it is there. If so, we
2218 * do not need to make a new entry but do continue.
2219 */
2220 int matched = 0;
2221 rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
2222 if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 &&
2223 (((struct nfs_netopt *)rn)->no_opt.nxo_flags == nxna.nxna_flags)) {
2224 kauth_cred_t cred2 = ((struct nfs_netopt *)rn)->no_opt.nxo_cred;
2225 if (cred && cred2 && (cred->cr_uid == cred2->cr_uid) &&
2226 (cred->cr_ngroups == cred2->cr_ngroups)) {
2227 for (i=0; i < cred2->cr_ngroups && i < NGROUPS; i++)
2228 if (cred->cr_groups[i] != cred2->cr_groups[i])
2229 break;
2230 if (i >= cred2->cr_ngroups || i >= NGROUPS)
2231 matched = 1;
2232 }
2233 }
2234 kauth_cred_rele(cred);
2235 _FREE(no, M_NETADDR);
2236 if (matched)
2237 continue;
2238 return (EPERM);
2239 }
2240 nx->nx_expcnt++;
2241 }
2242
2243 return (0);
2244 }
2245
2246 /*
2247 * In order to properly track an export's netopt count, we need to pass
2248 * an additional argument to nfsrv_free_netopt() so that it can decrement
2249 * the export's netopt count.
2250 */
2251 struct nfsrv_free_netopt_arg {
2252 uint32_t *cnt;
2253 struct radix_node_head *rnh;
2254 };
2255
2256 static int
2257 nfsrv_free_netopt(struct radix_node *rn, void *w)
2258 {
2259 struct nfsrv_free_netopt_arg *fna = (struct nfsrv_free_netopt_arg *)w;
2260 struct radix_node_head *rnh = fna->rnh;
2261 uint32_t *cnt = fna->cnt;
2262 struct nfs_netopt *nno = (struct nfs_netopt *)rn;
2263
2264 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
2265 if (nno->no_opt.nxo_cred)
2266 kauth_cred_rele(nno->no_opt.nxo_cred);
2267 _FREE((caddr_t)rn, M_NETADDR);
2268 *cnt -= 1;
2269 return (0);
2270 }
2271
2272 /*
2273 * Free the net address hash lists that are hanging off the mount points.
2274 */
2275 static void
2276 nfsrv_free_addrlist(struct nfs_export *nx)
2277 {
2278 int i;
2279 struct radix_node_head *rnh;
2280 struct nfsrv_free_netopt_arg fna;
2281
2282 for (i = 0; i <= AF_MAX; i++)
2283 if ( (rnh = nx->nx_rtable[i]) ) {
2284 fna.rnh = rnh;
2285 fna.cnt = &nx->nx_expcnt;
2286 (*rnh->rnh_walktree)(rnh, nfsrv_free_netopt, (caddr_t)&fna);
2287 _FREE((caddr_t)rnh, M_RTABLE);
2288 nx->nx_rtable[i] = 0;
2289 }
2290 }
2291
2292 void enablequotas(struct mount *mp, vfs_context_t ctx); // XXX
2293
2294 int
2295 nfsrv_export(struct user_nfs_export_args *unxa, struct vfs_context *ctx)
2296 {
2297 int error = 0, pathlen;
2298 struct nfs_exportfs *nxfs, *nxfs2, *nxfs3;
2299 struct nfs_export *nx, *nx2, *nx3;
2300 struct nfs_filehandle nfh;
2301 struct nameidata mnd, xnd;
2302 vnode_t mvp = NULL, xvp = NULL;
2303 mount_t mp;
2304 char path[MAXPATHLEN];
2305 int expisroot;
2306
2307 if (unxa->nxa_flags & NXA_DELETE_ALL) {
2308 /* delete all exports on all file systems */
2309 lck_rw_lock_exclusive(&nfs_export_rwlock);
2310 while ((nxfs = LIST_FIRST(&nfs_exports))) {
2311 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
2312 if (mp)
2313 mp->mnt_flag &= ~MNT_EXPORTED;
2314 /* delete all exports on this file system */
2315 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
2316 LIST_REMOVE(nx, nx_next);
2317 LIST_REMOVE(nx, nx_hash);
2318 /* delete all netopts for this export */
2319 nfsrv_free_addrlist(nx);
2320 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2321 if (nx->nx_defopt.nxo_cred) {
2322 kauth_cred_rele(nx->nx_defopt.nxo_cred);
2323 nx->nx_defopt.nxo_cred = NULL;
2324 }
2325 FREE(nx->nx_path, M_TEMP);
2326 FREE(nx, M_TEMP);
2327 }
2328 LIST_REMOVE(nxfs, nxfs_next);
2329 FREE(nxfs->nxfs_path, M_TEMP);
2330 FREE(nxfs, M_TEMP);
2331 }
2332 lck_rw_done(&nfs_export_rwlock);
2333 return (0);
2334 }
2335
2336 error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, (size_t *)&pathlen);
2337 if (error)
2338 return (error);
2339
2340 lck_rw_lock_exclusive(&nfs_export_rwlock);
2341
2342 // first check if we've already got an exportfs with the given ID
2343 LIST_FOREACH(nxfs, &nfs_exports, nxfs_next) {
2344 if (nxfs->nxfs_id == unxa->nxa_fsid)
2345 break;
2346 }
2347 if (nxfs) {
2348 /* verify exported FS path matches given path */
2349 if (strcmp(path, nxfs->nxfs_path)) {
2350 error = EEXIST;
2351 goto unlock_out;
2352 }
2353 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
2354 /* find exported FS root vnode */
2355 NDINIT(&mnd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
2356 UIO_SYSSPACE, nxfs->nxfs_path, ctx);
2357 error = namei(&mnd);
2358 if (error)
2359 goto unlock_out;
2360 mvp = mnd.ni_vp;
2361 /* make sure it's (still) the root of a file system */
2362 if ((mvp->v_flag & VROOT) == 0) {
2363 error = EINVAL;
2364 goto out;
2365 }
2366 /* sanity check: this should be same mount */
2367 if (mp != vnode_mount(mvp)) {
2368 error = EINVAL;
2369 goto out;
2370 }
2371 } else {
2372 /* no current exported file system with that ID */
2373 if (!(unxa->nxa_flags & NXA_ADD)) {
2374 error = ENOENT;
2375 goto unlock_out;
2376 }
2377
2378 /* find exported FS root vnode */
2379 NDINIT(&mnd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
2380 UIO_SYSSPACE, path, ctx);
2381 error = namei(&mnd);
2382 if (error)
2383 goto unlock_out;
2384 mvp = mnd.ni_vp;
2385 /* make sure it's the root of a file system */
2386 if ((mvp->v_flag & VROOT) == 0) {
2387 error = EINVAL;
2388 goto out;
2389 }
2390 mp = vnode_mount(mvp);
2391
2392 /* make sure the file system is NFS-exportable */
2393 nfh.nfh_len = NFS_MAX_FID_SIZE;
2394 error = VFS_VPTOFH(mvp, &nfh.nfh_len, &nfh.nfh_fid[0], NULL);
2395 if (!error && (nfh.nfh_len > (int)NFS_MAX_FID_SIZE))
2396 error = EIO;
2397 if (error)
2398 goto out;
2399
2400 /* add an exportfs for it */
2401 MALLOC(nxfs, struct nfs_exportfs *, sizeof(struct nfs_exportfs), M_TEMP, M_WAITOK);
2402 if (!nxfs) {
2403 error = ENOMEM;
2404 goto out;
2405 }
2406 bzero(nxfs, sizeof(struct nfs_exportfs));
2407 nxfs->nxfs_id = unxa->nxa_fsid;
2408 MALLOC(nxfs->nxfs_path, char*, pathlen, M_TEMP, M_WAITOK);
2409 if (!nxfs->nxfs_path) {
2410 FREE(nxfs, M_TEMP);
2411 error = ENOMEM;
2412 goto out;
2413 }
2414 bcopy(path, nxfs->nxfs_path, pathlen);
2415 /* insert into list in reverse-sorted order */
2416 nxfs3 = NULL;
2417 LIST_FOREACH(nxfs2, &nfs_exports, nxfs_next) {
2418 if (strcmp(nxfs->nxfs_path, nxfs2->nxfs_path) > 0)
2419 break;
2420 nxfs3 = nxfs2;
2421 }
2422 if (nxfs2)
2423 LIST_INSERT_BEFORE(nxfs2, nxfs, nxfs_next);
2424 else if (nxfs3)
2425 LIST_INSERT_AFTER(nxfs3, nxfs, nxfs_next);
2426 else
2427 LIST_INSERT_HEAD(&nfs_exports, nxfs, nxfs_next);
2428
2429 /* make sure any quotas are enabled before we export the file system */
2430 enablequotas(mp, ctx);
2431 }
2432
2433 if (unxa->nxa_exppath) {
2434 error = copyinstr(unxa->nxa_exppath, path, MAXPATHLEN, (size_t *)&pathlen);
2435 if (error)
2436 goto out;
2437 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
2438 if (nx->nx_id == unxa->nxa_expid)
2439 break;
2440 }
2441 if (nx) {
2442 /* verify exported FS path matches given path */
2443 if (strcmp(path, nx->nx_path)) {
2444 error = EEXIST;
2445 goto out;
2446 }
2447 } else {
2448 /* no current export with that ID */
2449 if (!(unxa->nxa_flags & NXA_ADD)) {
2450 error = ENOENT;
2451 goto out;
2452 }
2453 /* add an export for it */
2454 MALLOC(nx, struct nfs_export *, sizeof(struct nfs_export), M_TEMP, M_WAITOK);
2455 if (!nx) {
2456 error = ENOMEM;
2457 goto out1;
2458 }
2459 bzero(nx, sizeof(struct nfs_export));
2460 nx->nx_id = unxa->nxa_expid;
2461 nx->nx_fs = nxfs;
2462 MALLOC(nx->nx_path, char*, pathlen, M_TEMP, M_WAITOK);
2463 if (!nx->nx_path) {
2464 error = ENOMEM;
2465 FREE(nx, M_TEMP);
2466 nx = NULL;
2467 goto out1;
2468 }
2469 bcopy(path, nx->nx_path, pathlen);
2470 /* insert into list in reverse-sorted order */
2471 nx3 = NULL;
2472 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
2473 if (strcmp(nx->nx_path, nx2->nx_path) > 0)
2474 break;
2475 nx3 = nx2;
2476 }
2477 if (nx2)
2478 LIST_INSERT_BEFORE(nx2, nx, nx_next);
2479 else if (nx3)
2480 LIST_INSERT_AFTER(nx3, nx, nx_next);
2481 else
2482 LIST_INSERT_HEAD(&nxfs->nxfs_exports, nx, nx_next);
2483 /* insert into hash */
2484 LIST_INSERT_HEAD(NFSEXPHASH(nxfs->nxfs_id, nx->nx_id), nx, nx_hash);
2485
2486 /*
2487 * We don't allow nested exports. Check if the new entry
2488 * nests with the entries before and after or if there's an
2489 * entry for the file system root and subdirs.
2490 */
2491 error = 0;
2492 if ((nx3 && !strncmp(nx3->nx_path, nx->nx_path, pathlen - 1) &&
2493 (nx3->nx_path[pathlen-1] == '/')) ||
2494 (nx2 && !strncmp(nx2->nx_path, nx->nx_path, strlen(nx2->nx_path)) &&
2495 (nx->nx_path[strlen(nx2->nx_path)] == '/')))
2496 error = EINVAL;
2497 if (!error) {
2498 /* check export conflict with fs root export and vice versa */
2499 expisroot = !nx->nx_path[0] ||
2500 ((nx->nx_path[0] == '.') && !nx->nx_path[1]);
2501 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
2502 if (expisroot) {
2503 if (nx2 != nx)
2504 break;
2505 } else if (!nx2->nx_path[0])
2506 break;
2507 else if ((nx2->nx_path[0] == '.') && !nx2->nx_path[1])
2508 break;
2509 }
2510 if (nx2)
2511 error = EINVAL;
2512 }
2513 if (error) {
2514 printf("nfsrv_export: attempt to register nested exports: %s/%s\n",
2515 nxfs->nxfs_path, nx->nx_path);
2516 goto out1;
2517 }
2518
2519 /* find export root vnode */
2520 if (!nx->nx_path[0] || ((nx->nx_path[0] == '.') && !nx->nx_path[1])) {
2521 /* exporting file system's root directory */
2522 xvp = mvp;
2523 vnode_get(xvp);
2524 } else {
2525 xnd.ni_cnd.cn_nameiop = LOOKUP;
2526 xnd.ni_cnd.cn_flags = LOCKLEAF;
2527 xnd.ni_pathlen = pathlen - 1;
2528 xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf = path;
2529 xnd.ni_startdir = mvp;
2530 xnd.ni_usedvp = mvp;
2531 xnd.ni_cnd.cn_context = ctx;
2532 error = lookup(&xnd);
2533 if (error)
2534 goto out1;
2535 xvp = xnd.ni_vp;
2536 }
2537
2538 if (vnode_vtype(xvp) != VDIR) {
2539 error = EINVAL;
2540 vnode_put(xvp);
2541 goto out1;
2542 }
2543
2544 /* grab file handle */
2545 nx->nx_fh.nfh_xh.nxh_version = NFS_FH_VERSION;
2546 nx->nx_fh.nfh_xh.nxh_fsid = nx->nx_fs->nxfs_id;
2547 nx->nx_fh.nfh_xh.nxh_expid = nx->nx_id;
2548 nx->nx_fh.nfh_xh.nxh_flags = 0;
2549 nx->nx_fh.nfh_xh.nxh_reserved = 0;
2550 nx->nx_fh.nfh_len = NFS_MAX_FID_SIZE;
2551 error = VFS_VPTOFH(xvp, &nx->nx_fh.nfh_len, &nx->nx_fh.nfh_fid[0], NULL);
2552 if (!error && (nx->nx_fh.nfh_len > (int)NFS_MAX_FID_SIZE)) {
2553 error = EIO;
2554 } else {
2555 nx->nx_fh.nfh_xh.nxh_fidlen = nx->nx_fh.nfh_len;
2556 nx->nx_fh.nfh_len += sizeof(nx->nx_fh.nfh_xh);
2557 }
2558
2559 vnode_put(xvp);
2560 if (error)
2561 goto out1;
2562 }
2563 } else {
2564 nx = NULL;
2565 }
2566
2567 /* perform the export changes */
2568 if (unxa->nxa_flags & NXA_DELETE) {
2569 if (!nx) {
2570 /* delete all exports on this file system */
2571 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
2572 LIST_REMOVE(nx, nx_next);
2573 LIST_REMOVE(nx, nx_hash);
2574 /* delete all netopts for this export */
2575 nfsrv_free_addrlist(nx);
2576 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2577 if (nx->nx_defopt.nxo_cred) {
2578 kauth_cred_rele(nx->nx_defopt.nxo_cred);
2579 nx->nx_defopt.nxo_cred = NULL;
2580 }
2581 FREE(nx->nx_path, M_TEMP);
2582 FREE(nx, M_TEMP);
2583 }
2584 goto out1;
2585 } else {
2586 /* delete all netopts for this export */
2587 nfsrv_free_addrlist(nx);
2588 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2589 if (nx->nx_defopt.nxo_cred) {
2590 kauth_cred_rele(nx->nx_defopt.nxo_cred);
2591 nx->nx_defopt.nxo_cred = NULL;
2592 }
2593 }
2594 }
2595 if (unxa->nxa_flags & NXA_ADD) {
2596 error = nfsrv_hang_addrlist(nx, unxa);
2597 if (!error)
2598 mp->mnt_flag |= MNT_EXPORTED;
2599 }
2600
2601 out1:
2602 if (nx && !nx->nx_expcnt) {
2603 /* export has no export options */
2604 LIST_REMOVE(nx, nx_next);
2605 LIST_REMOVE(nx, nx_hash);
2606 FREE(nx->nx_path, M_TEMP);
2607 FREE(nx, M_TEMP);
2608 }
2609 if (LIST_EMPTY(&nxfs->nxfs_exports)) {
2610 /* exported file system has no more exports */
2611 LIST_REMOVE(nxfs, nxfs_next);
2612 FREE(nxfs->nxfs_path, M_TEMP);
2613 FREE(nxfs, M_TEMP);
2614 mp->mnt_flag &= ~MNT_EXPORTED;
2615 }
2616
2617 out:
2618 if (mvp) {
2619 vnode_put(mvp);
2620 nameidone(&mnd);
2621 }
2622 unlock_out:
2623 lck_rw_done(&nfs_export_rwlock);
2624 return (error);
2625 }
2626
2627 static struct nfs_export_options *
2628 nfsrv_export_lookup(struct nfs_export *nx, mbuf_t nam)
2629 {
2630 struct nfs_export_options *nxo = NULL;
2631 struct nfs_netopt *no = NULL;
2632 struct radix_node_head *rnh;
2633 struct sockaddr *saddr;
2634
2635 /* Lookup in the export list first. */
2636 if (nam != NULL) {
2637 saddr = mbuf_data(nam);
2638 rnh = nx->nx_rtable[saddr->sa_family];
2639 if (rnh != NULL) {
2640 no = (struct nfs_netopt *)
2641 (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
2642 if (no && no->no_rnodes->rn_flags & RNF_ROOT)
2643 no = NULL;
2644 if (no)
2645 nxo = &no->no_opt;
2646 }
2647 }
2648 /* If no address match, use the default if it exists. */
2649 if ((nxo == NULL) && (nx->nx_flags & NX_DEFAULTEXPORT))
2650 nxo = &nx->nx_defopt;
2651 return (nxo);
2652 }
2653
2654 /* find an export for the given handle */
2655 static struct nfs_export *
2656 nfsrv_fhtoexport(struct nfs_filehandle *nfhp)
2657 {
2658 struct nfs_export *nx;
2659 nx = NFSEXPHASH(nfhp->nfh_xh.nxh_fsid, nfhp->nfh_xh.nxh_expid)->lh_first;
2660 for (; nx; nx = LIST_NEXT(nx, nx_hash)) {
2661 if (nx->nx_fs->nxfs_id != nfhp->nfh_xh.nxh_fsid)
2662 continue;
2663 if (nx->nx_id != nfhp->nfh_xh.nxh_expid)
2664 continue;
2665 break;
2666 }
2667 return nx;
2668 }
2669
2670 /*
2671 * nfsrv_fhtovp() - convert FH to vnode and export info
2672 */
2673 int
2674 nfsrv_fhtovp(
2675 struct nfs_filehandle *nfhp,
2676 mbuf_t nam,
2677 __unused int pubflag,
2678 vnode_t *vpp,
2679 struct nfs_export **nxp,
2680 struct nfs_export_options **nxop)
2681 {
2682 int error;
2683 struct mount *mp;
2684
2685 *vpp = NULL;
2686 *nxp = NULL;
2687 *nxop = NULL;
2688
2689 if (nfhp->nfh_xh.nxh_version != NFS_FH_VERSION) {
2690 /* file handle format not supported */
2691 return (ESTALE);
2692 }
2693 if (nfhp->nfh_len > NFS_MAX_FH_SIZE)
2694 return (EBADRPC);
2695 if (nfhp->nfh_len < (int)sizeof(nfhp->nfh_xh))
2696 return (ESTALE);
2697 if (nfhp->nfh_xh.nxh_flags & NXHF_INVALIDFH)
2698 return (ESTALE);
2699
2700 /* XXX Revisit when enabling WebNFS */
2701 #ifdef WEBNFS_ENABLED
2702 if (nfs_ispublicfh(nfhp)) {
2703 if (!pubflag || !nfs_pub.np_valid)
2704 return (ESTALE);
2705 nfhp = &nfs_pub.np_handle;
2706 }
2707 #endif
2708
2709 *nxp = nfsrv_fhtoexport(nfhp);
2710 if (!*nxp)
2711 return (ESTALE);
2712
2713 /* Get the export option structure for this <export, client> tuple. */
2714 *nxop = nfsrv_export_lookup(*nxp, nam);
2715 if (nam && (*nxop == NULL))
2716 return (EACCES);
2717
2718 /* find mount structure */
2719 mp = vfs_getvfs_by_mntonname((*nxp)->nx_fs->nxfs_path);
2720 if (!mp)
2721 return (ESTALE);
2722
2723 error = VFS_FHTOVP(mp, nfhp->nfh_xh.nxh_fidlen, &nfhp->nfh_fid[0], vpp, NULL);
2724 if (error)
2725 return (error);
2726 /* vnode pointer should be good at this point or ... */
2727 if (*vpp == NULL)
2728 return (ESTALE);
2729 return (0);
2730 }
2731
2732 /*
2733 * nfsrv_credcheck() - check/map credentials according to given export options
2734 */
2735 int
2736 nfsrv_credcheck(
2737 struct nfsrv_descript *nfsd,
2738 __unused struct nfs_export *nx,
2739 struct nfs_export_options *nxo)
2740 {
2741 if (nxo && nxo->nxo_cred) {
2742 if ((nxo->nxo_flags & NX_MAPALL) ||
2743 ((nxo->nxo_flags & NX_MAPROOT) && !suser(nfsd->nd_cr, NULL))) {
2744 kauth_cred_rele(nfsd->nd_cr);
2745 nfsd->nd_cr = nxo->nxo_cred;
2746 kauth_cred_ref(nfsd->nd_cr);
2747 }
2748 }
2749 return (0);
2750 }
2751
2752
2753 /*
2754 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2755 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2756 * transformed this to all zeroes in both cases, so check for it.
2757 */
2758 int
2759 nfs_ispublicfh(struct nfs_filehandle *nfhp)
2760 {
2761 char *cp = (char *)nfhp;
2762 unsigned int i;
2763
2764 if (nfhp->nfh_len == 0)
2765 return (TRUE);
2766 if (nfhp->nfh_len != NFSX_V2FH)
2767 return (FALSE);
2768 for (i = 0; i < NFSX_V2FH; i++)
2769 if (*cp++ != 0)
2770 return (FALSE);
2771 return (TRUE);
2772 }
2773
2774 /*
2775 * nfsrv_vptofh() - convert vnode to file handle for given export
2776 *
2777 * If the caller is passing in a vnode for a ".." directory entry,
2778 * they can pass a directory NFS file handle (dnfhp) which will be
2779 * checked against the root export file handle. If it matches, we
2780 * refuse to provide the file handle for the out-of-export directory.
2781 */
2782 int
2783 nfsrv_vptofh(
2784 struct nfs_export *nx,
2785 int v2,
2786 struct nfs_filehandle *dnfhp,
2787 vnode_t vp,
2788 struct vfs_context *ctx,
2789 struct nfs_filehandle *nfhp)
2790 {
2791 int error;
2792
2793 nfhp->nfh_xh.nxh_version = NFS_FH_VERSION;
2794 nfhp->nfh_xh.nxh_fsid = nx->nx_fs->nxfs_id;
2795 nfhp->nfh_xh.nxh_expid = nx->nx_id;
2796 nfhp->nfh_xh.nxh_flags = 0;
2797 nfhp->nfh_xh.nxh_reserved = 0;
2798
2799 if (v2)
2800 bzero(&nfhp->nfh_fid[0], NFSV2_MAX_FID_SIZE);
2801
2802 /* if directory FH matches export root, return invalid FH */
2803 if (dnfhp && nfsrv_fhmatch(dnfhp, &nx->nx_fh)) {
2804 nfhp->nfh_len = v2 ? NFSX_V2FH : sizeof(nfhp->nfh_xh);
2805 nfhp->nfh_xh.nxh_fidlen = 0;
2806 nfhp->nfh_xh.nxh_flags = NXHF_INVALIDFH;
2807 return (0);
2808 }
2809
2810 nfhp->nfh_len = v2 ? NFSV2_MAX_FID_SIZE : NFS_MAX_FID_SIZE;
2811 error = VFS_VPTOFH(vp, &nfhp->nfh_len, &nfhp->nfh_fid[0], ctx);
2812 if (error)
2813 return (error);
2814 if (nfhp->nfh_len > (int)(v2 ? NFSV2_MAX_FID_SIZE : NFS_MAX_FID_SIZE))
2815 return (EOVERFLOW);
2816 nfhp->nfh_xh.nxh_fidlen = nfhp->nfh_len;
2817 nfhp->nfh_len += sizeof(nfhp->nfh_xh);
2818 if (v2 && (nfhp->nfh_len < NFSX_V2FH))
2819 nfhp->nfh_len = NFSX_V2FH;
2820
2821 return (0);
2822 }
2823
2824 int
2825 nfsrv_fhmatch(struct nfs_filehandle *fh1, struct nfs_filehandle *fh2)
2826 {
2827 int len1, len2;
2828
2829 len1 = sizeof(fh1->nfh_xh) + fh1->nfh_xh.nxh_fidlen;
2830 len2 = sizeof(fh2->nfh_xh) + fh2->nfh_xh.nxh_fidlen;
2831 if (len1 != len2)
2832 return (0);
2833 if (bcmp(&fh1->nfh_xh, &fh2->nfh_xh, len1))
2834 return (0);
2835 return (1);
2836 }
2837
2838 #endif /* NFS_NOSERVER */
2839 /*
2840 * This function compares two net addresses by family and returns TRUE
2841 * if they are the same host.
2842 * If there is any doubt, return FALSE.
2843 * The AF_INET family is handled as a special case so that address mbufs
2844 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2845 */
2846 int
2847 netaddr_match(family, haddr, nam)
2848 int family;
2849 union nethostaddr *haddr;
2850 mbuf_t nam;
2851 {
2852 struct sockaddr_in *inetaddr;
2853
2854 switch (family) {
2855 case AF_INET:
2856 inetaddr = mbuf_data(nam);
2857 if (inetaddr->sin_family == AF_INET &&
2858 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2859 return (1);
2860 break;
2861 #if ISO
2862 case AF_ISO:
2863 {
2864 struct sockaddr_iso *isoaddr1, *isoaddr2;
2865
2866 isoaddr1 = mbuf_data(nam);
2867 isoaddr2 = mbuf_data(haddr->had_nam);
2868 if (isoaddr1->siso_family == AF_ISO &&
2869 isoaddr1->siso_nlen > 0 &&
2870 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2871 SAME_ISOADDR(isoaddr1, isoaddr2))
2872 return (1);
2873 break;
2874 }
2875 #endif /* ISO */
2876 default:
2877 break;
2878 };
2879 return (0);
2880 }
2881
2882 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
2883 /*
2884 * This function finds the directory cookie that corresponds to the
2885 * logical byte offset given.
2886 */
2887 nfsuint64 *
2888 nfs_getcookie(np, off, add)
2889 struct nfsnode *np;
2890 off_t off;
2891 int add;
2892 {
2893 struct nfsdmap *dp, *dp2;
2894 int pos;
2895
2896 pos = off / NFS_DIRBLKSIZ;
2897 if (pos == 0) {
2898 #if DIAGNOSTIC
2899 if (add)
2900 panic("nfs getcookie add at 0");
2901 #endif
2902 return (&nfs_nullcookie);
2903 }
2904 pos--;
2905 dp = np->n_cookies.lh_first;
2906 if (!dp) {
2907 if (add) {
2908 MALLOC_ZONE(dp, struct nfsdmap *, sizeof(struct nfsdmap),
2909 M_NFSDIROFF, M_WAITOK);
2910 if (!dp)
2911 return ((nfsuint64 *)0);
2912 dp->ndm_eocookie = 0;
2913 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2914 } else
2915 return ((nfsuint64 *)0);
2916 }
2917 while (pos >= NFSNUMCOOKIES) {
2918 pos -= NFSNUMCOOKIES;
2919 if (dp->ndm_list.le_next) {
2920 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2921 pos >= dp->ndm_eocookie)
2922 return ((nfsuint64 *)0);
2923 dp = dp->ndm_list.le_next;
2924 } else if (add) {
2925 MALLOC_ZONE(dp2, struct nfsdmap *, sizeof(struct nfsdmap),
2926 M_NFSDIROFF, M_WAITOK);
2927 if (!dp2)
2928 return ((nfsuint64 *)0);
2929 dp2->ndm_eocookie = 0;
2930 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2931 dp = dp2;
2932 } else
2933 return ((nfsuint64 *)0);
2934 }
2935 if (pos >= dp->ndm_eocookie) {
2936 if (add)
2937 dp->ndm_eocookie = pos + 1;
2938 else
2939 return ((nfsuint64 *)0);
2940 }
2941 return (&dp->ndm_cookies[pos]);
2942 }
2943
2944 /*
2945 * Invalidate cached directory information, except for the actual directory
2946 * blocks (which are invalidated separately).
2947 * Done mainly to avoid the use of stale offset cookies.
2948 */
2949 void
2950 nfs_invaldir(vp)
2951 vnode_t vp;
2952 {
2953 struct nfsnode *np = VTONFS(vp);
2954
2955 #if DIAGNOSTIC
2956 if (vnode_vtype(vp) != VDIR)
2957 panic("nfs: invaldir not dir");
2958 #endif
2959 np->n_direofoffset = 0;
2960 np->n_cookieverf.nfsuquad[0] = 0;
2961 np->n_cookieverf.nfsuquad[1] = 0;
2962 if (np->n_cookies.lh_first)
2963 np->n_cookies.lh_first->ndm_eocookie = 0;
2964 }
2965
2966 #ifndef NFS_NOSERVER
2967 /*
2968 * Map errnos to NFS error numbers. For Version 3 also filter out error
2969 * numbers not specified for the associated procedure.
2970 */
2971 int
2972 nfsrv_errmap(nd, err)
2973 struct nfsrv_descript *nd;
2974 int err;
2975 {
2976 short *defaulterrp, *errp;
2977
2978 if (nd->nd_flag & ND_NFSV3) {
2979 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2980 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2981 while (*++errp) {
2982 if (*errp == err)
2983 return (err);
2984 else if (*errp > err)
2985 break;
2986 }
2987 return ((int)*defaulterrp);
2988 } else
2989 return (err & 0xffff);
2990 }
2991 if (err <= ELAST)
2992 return ((int)nfsrv_v2errmap[err - 1]);
2993 return (NFSERR_IO);
2994 }
2995
2996 #endif /* NFS_NOSERVER */
2997