]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_subs.c
a2ea80c49110204251e39998e88bfd286a6e25ea
[apple/xnu.git] / bsd / nfs / nfs_subs.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * This code is derived from software contributed to Berkeley by
36 * Rick Macklem at The University of Guelph.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
67 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
68 */
69
70 /*
71 * These functions support the macros and help fiddle mbuf chains for
72 * the nfs op functions. They do things like create the rpc header and
73 * copy data between mbuf chains and uio lists.
74 */
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/kauth.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/mount_internal.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/kpi_mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/stat.h>
85 #include <sys/malloc.h>
86 #include <sys/syscall.h>
87 #include <sys/sysctl.h>
88 #include <sys/ubc_internal.h>
89 #include <sys/fcntl.h>
90 #include <sys/uio_internal.h>
91 #include <sys/domain.h>
92 #include <libkern/OSAtomic.h>
93
94 #include <sys/vm.h>
95 #include <sys/vmparam.h>
96
97 #include <sys/time.h>
98 #include <kern/clock.h>
99
100 #include <nfs/rpcv2.h>
101 #include <nfs/nfsproto.h>
102 #include <nfs/nfs.h>
103 #include <nfs/nfsnode.h>
104 #include <nfs/xdr_subs.h>
105 #include <nfs/nfsm_subs.h>
106 #include <nfs/nfsmount.h>
107 #include <nfs/nfsrtt.h>
108 #include <nfs/nfs_lock.h>
109
110 #include <miscfs/specfs/specdev.h>
111
112 #include <netinet/in.h>
113 #if ISO
114 #include <netiso/iso.h>
115 #endif
116
117 #include <sys/kdebug.h>
118
119 SYSCTL_DECL(_vfs_generic);
120 SYSCTL_NODE(_vfs_generic, OID_AUTO, nfs, CTLFLAG_RW, 0, "nfs hinge");
121
122 #define FSDBG(A, B, C, D, E) \
123 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
124 (int)(B), (int)(C), (int)(D), (int)(E), 0)
125 #define FSDBG_TOP(A, B, C, D, E) \
126 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
127 (int)(B), (int)(C), (int)(D), (int)(E), 0)
128 #define FSDBG_BOT(A, B, C, D, E) \
129 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
130 (int)(B), (int)(C), (int)(D), (int)(E), 0)
131 /*
132 * Data items converted to xdr at startup, since they are constant
133 * This is kinda hokey, but may save a little time doing byte swaps
134 */
135 u_long nfs_xdrneg1;
136 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
137 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
138 rpc_auth_kerb;
139 u_long nfs_prog, nfs_true, nfs_false;
140 __private_extern__ int nfs_mbuf_mlen = 0, nfs_mbuf_mhlen = 0,
141 nfs_mbuf_minclsize = 0, nfs_mbuf_mclbytes = 0;
142
143 /* And other global data */
144 static u_long nfs_xid = 0;
145 u_long nfs_xidwrap = 0; /* to build a (non-wwrapping) 64 bit xid */
146 static enum vtype nv2tov_type[8]= {
147 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
148 };
149 enum vtype nv3tov_type[8]= {
150 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
151 };
152
153 int nfs_mount_type;
154 int nfs_ticks;
155
156 lck_grp_t *nfsd_lck_grp;
157 lck_grp_attr_t *nfsd_lck_grp_attr;
158 lck_attr_t *nfsd_lck_attr;
159 lck_mtx_t *nfsd_mutex;
160
161 lck_grp_attr_t *nfs_slp_group_attr;
162 lck_attr_t *nfs_slp_lock_attr;
163 lck_grp_t *nfs_slp_rwlock_group;
164 lck_grp_t *nfs_slp_mutex_group;
165
166 struct nfs_reqq nfs_reqq;
167 struct nfssvc_sockhead nfssvc_sockhead, nfssvc_deadsockhead;
168 struct nfsd_head nfsd_head;
169 int nfsd_head_flag;
170
171 struct nfsexpfslist nfs_exports;
172 struct nfsexphashhead *nfsexphashtbl;
173 u_long nfsexphash;
174 lck_grp_attr_t *nfs_export_group_attr;
175 lck_attr_t *nfs_export_lock_attr;
176 lck_grp_t *nfs_export_rwlock_group;
177 lck_rw_t nfs_export_rwlock;
178
179 #ifndef NFS_NOSERVER
180 /*
181 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
182 */
183 int nfsv3_procid[NFS_NPROCS] = {
184 NFSPROC_NULL,
185 NFSPROC_GETATTR,
186 NFSPROC_SETATTR,
187 NFSPROC_NOOP,
188 NFSPROC_LOOKUP,
189 NFSPROC_READLINK,
190 NFSPROC_READ,
191 NFSPROC_NOOP,
192 NFSPROC_WRITE,
193 NFSPROC_CREATE,
194 NFSPROC_REMOVE,
195 NFSPROC_RENAME,
196 NFSPROC_LINK,
197 NFSPROC_SYMLINK,
198 NFSPROC_MKDIR,
199 NFSPROC_RMDIR,
200 NFSPROC_READDIR,
201 NFSPROC_FSSTAT,
202 NFSPROC_NOOP,
203 NFSPROC_NOOP,
204 NFSPROC_NOOP,
205 NFSPROC_NOOP,
206 NFSPROC_NOOP
207 };
208
209 #endif /* NFS_NOSERVER */
210 /*
211 * and the reverse mapping from generic to Version 2 procedure numbers
212 */
213 int nfsv2_procid[NFS_NPROCS] = {
214 NFSV2PROC_NULL,
215 NFSV2PROC_GETATTR,
216 NFSV2PROC_SETATTR,
217 NFSV2PROC_LOOKUP,
218 NFSV2PROC_NOOP,
219 NFSV2PROC_READLINK,
220 NFSV2PROC_READ,
221 NFSV2PROC_WRITE,
222 NFSV2PROC_CREATE,
223 NFSV2PROC_MKDIR,
224 NFSV2PROC_SYMLINK,
225 NFSV2PROC_CREATE,
226 NFSV2PROC_REMOVE,
227 NFSV2PROC_RMDIR,
228 NFSV2PROC_RENAME,
229 NFSV2PROC_LINK,
230 NFSV2PROC_READDIR,
231 NFSV2PROC_NOOP,
232 NFSV2PROC_STATFS,
233 NFSV2PROC_NOOP,
234 NFSV2PROC_NOOP,
235 NFSV2PROC_NOOP,
236 NFSV2PROC_NOOP
237 };
238
239 #ifndef NFS_NOSERVER
240 /*
241 * Maps errno values to nfs error numbers.
242 * Use NFSERR_IO as the catch all for ones not specifically defined in
243 * RFC 1094.
244 */
245 static u_char nfsrv_v2errmap[ELAST] = {
246 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
247 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
248 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
249 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
250 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
251 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
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_IO, NFSERR_IO, NFSERR_IO,
257 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
258 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
259 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
260 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
261 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
262 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
263 };
264
265 /*
266 * Maps errno values to nfs error numbers.
267 * Although it is not obvious whether or not NFS clients really care if
268 * a returned error value is in the specified list for the procedure, the
269 * safest thing to do is filter them appropriately. For Version 2, the
270 * X/Open XNFS document is the only specification that defines error values
271 * for each RPC (The RFC simply lists all possible error values for all RPCs),
272 * so I have decided to not do this for Version 2.
273 * The first entry is the default error return and the rest are the valid
274 * errors for that RPC in increasing numeric order.
275 */
276 static short nfsv3err_null[] = {
277 0,
278 0,
279 };
280
281 static short nfsv3err_getattr[] = {
282 NFSERR_IO,
283 NFSERR_IO,
284 NFSERR_STALE,
285 NFSERR_BADHANDLE,
286 NFSERR_SERVERFAULT,
287 0,
288 };
289
290 static short nfsv3err_setattr[] = {
291 NFSERR_IO,
292 NFSERR_PERM,
293 NFSERR_IO,
294 NFSERR_ACCES,
295 NFSERR_INVAL,
296 NFSERR_NOSPC,
297 NFSERR_ROFS,
298 NFSERR_DQUOT,
299 NFSERR_STALE,
300 NFSERR_BADHANDLE,
301 NFSERR_NOT_SYNC,
302 NFSERR_SERVERFAULT,
303 0,
304 };
305
306 static short nfsv3err_lookup[] = {
307 NFSERR_IO,
308 NFSERR_NOENT,
309 NFSERR_IO,
310 NFSERR_ACCES,
311 NFSERR_NOTDIR,
312 NFSERR_NAMETOL,
313 NFSERR_STALE,
314 NFSERR_BADHANDLE,
315 NFSERR_SERVERFAULT,
316 0,
317 };
318
319 static short nfsv3err_access[] = {
320 NFSERR_IO,
321 NFSERR_IO,
322 NFSERR_STALE,
323 NFSERR_BADHANDLE,
324 NFSERR_SERVERFAULT,
325 0,
326 };
327
328 static short nfsv3err_readlink[] = {
329 NFSERR_IO,
330 NFSERR_IO,
331 NFSERR_ACCES,
332 NFSERR_INVAL,
333 NFSERR_STALE,
334 NFSERR_BADHANDLE,
335 NFSERR_NOTSUPP,
336 NFSERR_SERVERFAULT,
337 0,
338 };
339
340 static short nfsv3err_read[] = {
341 NFSERR_IO,
342 NFSERR_IO,
343 NFSERR_NXIO,
344 NFSERR_ACCES,
345 NFSERR_INVAL,
346 NFSERR_STALE,
347 NFSERR_BADHANDLE,
348 NFSERR_SERVERFAULT,
349 0,
350 };
351
352 static short nfsv3err_write[] = {
353 NFSERR_IO,
354 NFSERR_IO,
355 NFSERR_ACCES,
356 NFSERR_INVAL,
357 NFSERR_FBIG,
358 NFSERR_NOSPC,
359 NFSERR_ROFS,
360 NFSERR_DQUOT,
361 NFSERR_STALE,
362 NFSERR_BADHANDLE,
363 NFSERR_SERVERFAULT,
364 0,
365 };
366
367 static short nfsv3err_create[] = {
368 NFSERR_IO,
369 NFSERR_IO,
370 NFSERR_ACCES,
371 NFSERR_EXIST,
372 NFSERR_NOTDIR,
373 NFSERR_NOSPC,
374 NFSERR_ROFS,
375 NFSERR_NAMETOL,
376 NFSERR_DQUOT,
377 NFSERR_STALE,
378 NFSERR_BADHANDLE,
379 NFSERR_NOTSUPP,
380 NFSERR_SERVERFAULT,
381 0,
382 };
383
384 static short nfsv3err_mkdir[] = {
385 NFSERR_IO,
386 NFSERR_IO,
387 NFSERR_ACCES,
388 NFSERR_EXIST,
389 NFSERR_NOTDIR,
390 NFSERR_NOSPC,
391 NFSERR_ROFS,
392 NFSERR_NAMETOL,
393 NFSERR_DQUOT,
394 NFSERR_STALE,
395 NFSERR_BADHANDLE,
396 NFSERR_NOTSUPP,
397 NFSERR_SERVERFAULT,
398 0,
399 };
400
401 static short nfsv3err_symlink[] = {
402 NFSERR_IO,
403 NFSERR_IO,
404 NFSERR_ACCES,
405 NFSERR_EXIST,
406 NFSERR_NOTDIR,
407 NFSERR_NOSPC,
408 NFSERR_ROFS,
409 NFSERR_NAMETOL,
410 NFSERR_DQUOT,
411 NFSERR_STALE,
412 NFSERR_BADHANDLE,
413 NFSERR_NOTSUPP,
414 NFSERR_SERVERFAULT,
415 0,
416 };
417
418 static short nfsv3err_mknod[] = {
419 NFSERR_IO,
420 NFSERR_IO,
421 NFSERR_ACCES,
422 NFSERR_EXIST,
423 NFSERR_NOTDIR,
424 NFSERR_NOSPC,
425 NFSERR_ROFS,
426 NFSERR_NAMETOL,
427 NFSERR_DQUOT,
428 NFSERR_STALE,
429 NFSERR_BADHANDLE,
430 NFSERR_NOTSUPP,
431 NFSERR_SERVERFAULT,
432 NFSERR_BADTYPE,
433 0,
434 };
435
436 static short nfsv3err_remove[] = {
437 NFSERR_IO,
438 NFSERR_NOENT,
439 NFSERR_IO,
440 NFSERR_ACCES,
441 NFSERR_NOTDIR,
442 NFSERR_ROFS,
443 NFSERR_NAMETOL,
444 NFSERR_STALE,
445 NFSERR_BADHANDLE,
446 NFSERR_SERVERFAULT,
447 0,
448 };
449
450 static short nfsv3err_rmdir[] = {
451 NFSERR_IO,
452 NFSERR_NOENT,
453 NFSERR_IO,
454 NFSERR_ACCES,
455 NFSERR_EXIST,
456 NFSERR_NOTDIR,
457 NFSERR_INVAL,
458 NFSERR_ROFS,
459 NFSERR_NAMETOL,
460 NFSERR_NOTEMPTY,
461 NFSERR_STALE,
462 NFSERR_BADHANDLE,
463 NFSERR_NOTSUPP,
464 NFSERR_SERVERFAULT,
465 0,
466 };
467
468 static short nfsv3err_rename[] = {
469 NFSERR_IO,
470 NFSERR_NOENT,
471 NFSERR_IO,
472 NFSERR_ACCES,
473 NFSERR_EXIST,
474 NFSERR_XDEV,
475 NFSERR_NOTDIR,
476 NFSERR_ISDIR,
477 NFSERR_INVAL,
478 NFSERR_NOSPC,
479 NFSERR_ROFS,
480 NFSERR_MLINK,
481 NFSERR_NAMETOL,
482 NFSERR_NOTEMPTY,
483 NFSERR_DQUOT,
484 NFSERR_STALE,
485 NFSERR_BADHANDLE,
486 NFSERR_NOTSUPP,
487 NFSERR_SERVERFAULT,
488 0,
489 };
490
491 static short nfsv3err_link[] = {
492 NFSERR_IO,
493 NFSERR_IO,
494 NFSERR_ACCES,
495 NFSERR_EXIST,
496 NFSERR_XDEV,
497 NFSERR_NOTDIR,
498 NFSERR_INVAL,
499 NFSERR_NOSPC,
500 NFSERR_ROFS,
501 NFSERR_MLINK,
502 NFSERR_NAMETOL,
503 NFSERR_DQUOT,
504 NFSERR_STALE,
505 NFSERR_BADHANDLE,
506 NFSERR_NOTSUPP,
507 NFSERR_SERVERFAULT,
508 0,
509 };
510
511 static short nfsv3err_readdir[] = {
512 NFSERR_IO,
513 NFSERR_IO,
514 NFSERR_ACCES,
515 NFSERR_NOTDIR,
516 NFSERR_STALE,
517 NFSERR_BADHANDLE,
518 NFSERR_BAD_COOKIE,
519 NFSERR_TOOSMALL,
520 NFSERR_SERVERFAULT,
521 0,
522 };
523
524 static short nfsv3err_readdirplus[] = {
525 NFSERR_IO,
526 NFSERR_IO,
527 NFSERR_ACCES,
528 NFSERR_NOTDIR,
529 NFSERR_STALE,
530 NFSERR_BADHANDLE,
531 NFSERR_BAD_COOKIE,
532 NFSERR_NOTSUPP,
533 NFSERR_TOOSMALL,
534 NFSERR_SERVERFAULT,
535 0,
536 };
537
538 static short nfsv3err_fsstat[] = {
539 NFSERR_IO,
540 NFSERR_IO,
541 NFSERR_STALE,
542 NFSERR_BADHANDLE,
543 NFSERR_SERVERFAULT,
544 0,
545 };
546
547 static short nfsv3err_fsinfo[] = {
548 NFSERR_STALE,
549 NFSERR_STALE,
550 NFSERR_BADHANDLE,
551 NFSERR_SERVERFAULT,
552 0,
553 };
554
555 static short nfsv3err_pathconf[] = {
556 NFSERR_STALE,
557 NFSERR_STALE,
558 NFSERR_BADHANDLE,
559 NFSERR_SERVERFAULT,
560 0,
561 };
562
563 static short nfsv3err_commit[] = {
564 NFSERR_IO,
565 NFSERR_IO,
566 NFSERR_STALE,
567 NFSERR_BADHANDLE,
568 NFSERR_SERVERFAULT,
569 0,
570 };
571
572 static short *nfsrv_v3errmap[] = {
573 nfsv3err_null,
574 nfsv3err_getattr,
575 nfsv3err_setattr,
576 nfsv3err_lookup,
577 nfsv3err_access,
578 nfsv3err_readlink,
579 nfsv3err_read,
580 nfsv3err_write,
581 nfsv3err_create,
582 nfsv3err_mkdir,
583 nfsv3err_symlink,
584 nfsv3err_mknod,
585 nfsv3err_remove,
586 nfsv3err_rmdir,
587 nfsv3err_rename,
588 nfsv3err_link,
589 nfsv3err_readdir,
590 nfsv3err_readdirplus,
591 nfsv3err_fsstat,
592 nfsv3err_fsinfo,
593 nfsv3err_pathconf,
594 nfsv3err_commit,
595 };
596
597 #endif /* NFS_NOSERVER */
598
599 extern struct nfsrtt nfsrtt;
600 extern struct nfsstats nfsstats;
601 extern nfstype nfsv2_type[9];
602 extern nfstype nfsv3_type[9];
603 extern struct nfsnodehashhead *nfsnodehashtbl;
604 extern u_long nfsnodehash;
605
606
607 LIST_HEAD(nfsnodehashhead, nfsnode);
608
609 /*
610 * Create the header for an rpc request packet
611 * The hsiz is the size of the rest of the nfs request header.
612 * (just used to decide if a cluster is a good idea)
613 */
614 int
615 nfsm_reqh(int hsiz, caddr_t *bposp, mbuf_t *mbp)
616 {
617 int error;
618
619 *mbp = NULL;
620 if (hsiz >= nfs_mbuf_minclsize)
621 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, mbp);
622 else
623 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, mbp);
624 if (error)
625 return (error);
626 *bposp = mbuf_data(*mbp);
627 return (0);
628 }
629
630 /*
631 * Build the RPC header and fill in the authorization info.
632 * The authorization string argument is only used when the credentials
633 * come from outside of the kernel.
634 * Returns the head of the mbuf list.
635 */
636 int
637 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
638 verf_str, mrest, mrest_len, mbp, xidp, mreqp)
639 kauth_cred_t cr;
640 int nmflag;
641 int procid;
642 int auth_type;
643 int auth_len;
644 char *auth_str;
645 int verf_len;
646 char *verf_str;
647 mbuf_t mrest;
648 int mrest_len;
649 mbuf_t *mbp;
650 u_long *xidp;
651 mbuf_t *mreqp;
652 {
653 mbuf_t mb;
654 u_long *tl;
655 caddr_t bpos;
656 int i, error, len;
657 mbuf_t mreq, mb2;
658 int siz, grpsiz, authsiz, mlen;
659 struct timeval tv;
660
661 authsiz = nfsm_rndup(auth_len);
662 len = authsiz + 10 * NFSX_UNSIGNED;
663 if (len >= nfs_mbuf_minclsize) {
664 error = mbuf_getpacket(MBUF_WAITOK, &mb);
665 } else {
666 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
667 if (!error) {
668 if (len < nfs_mbuf_mhlen)
669 mbuf_align_32(mb, len);
670 else
671 mbuf_align_32(mb, 8 * NFSX_UNSIGNED);
672 }
673 }
674 if (error) {
675 /* unable to allocate packet */
676 /* XXX nfsstat? */
677 return (error);
678 }
679 mreq = mb;
680 bpos = mbuf_data(mb);
681
682 /*
683 * First the RPC header.
684 */
685 nfsm_build(tl, u_long *, 8 * NFSX_UNSIGNED);
686
687 /*
688 * derive initial xid from system time
689 */
690 if (!nfs_xid) {
691 /*
692 * Note: it's OK if this code inits nfs_xid to 0 (for example,
693 * due to a broken clock) because we immediately increment it
694 * and we guarantee to never use xid 0. So, nfs_xid should only
695 * ever be 0 the first time this function is called.
696 */
697 microtime(&tv);
698 nfs_xid = tv.tv_sec << 12;
699 }
700 /*
701 * Skip zero xid if it should ever happen.
702 */
703 if (++nfs_xid == 0) {
704 nfs_xidwrap++;
705 nfs_xid++;
706 }
707
708 *tl++ = *xidp = txdr_unsigned(nfs_xid);
709 *tl++ = rpc_call;
710 *tl++ = rpc_vers;
711 *tl++ = txdr_unsigned(NFS_PROG);
712 if (nmflag & NFSMNT_NFSV3)
713 *tl++ = txdr_unsigned(NFS_VER3);
714 else
715 *tl++ = txdr_unsigned(NFS_VER2);
716 if (nmflag & NFSMNT_NFSV3)
717 *tl++ = txdr_unsigned(procid);
718 else
719 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
720
721 /*
722 * And then the authorization cred.
723 */
724 *tl++ = txdr_unsigned(auth_type);
725 *tl = txdr_unsigned(authsiz);
726 switch (auth_type) {
727 case RPCAUTH_UNIX:
728 nfsm_build(tl, u_long *, auth_len);
729 *tl++ = 0; /* stamp ?? */
730 *tl++ = 0; /* NULL hostname */
731 *tl++ = txdr_unsigned(kauth_cred_getuid(cr));
732 *tl++ = txdr_unsigned(cr->cr_groups[0]);
733 grpsiz = (auth_len >> 2) - 5;
734 *tl++ = txdr_unsigned(grpsiz);
735 for (i = 1; i <= grpsiz; i++)
736 *tl++ = txdr_unsigned(cr->cr_groups[i]);
737 break;
738 case RPCAUTH_KERB4:
739 siz = auth_len;
740 mlen = mbuf_len(mb);
741 while (siz > 0) {
742 if (mbuf_trailingspace(mb) == 0) {
743 mb2 = NULL;
744 if (siz >= nfs_mbuf_minclsize)
745 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
746 else
747 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
748 if (!error)
749 error = mbuf_setnext(mb, mb2);
750 if (error) {
751 mbuf_freem(mreq);
752 return (error);
753 }
754 mb = mb2;
755 mlen = 0;
756 bpos = mbuf_data(mb);
757 }
758 i = min(siz, mbuf_trailingspace(mb));
759 bcopy(auth_str, bpos, i);
760 mlen += i;
761 mbuf_setlen(mb, mlen);
762 auth_str += i;
763 bpos += i;
764 siz -= i;
765 }
766 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
767 for (i = 0; i < siz; i++)
768 *bpos++ = '\0';
769 mlen += siz;
770 mbuf_setlen(mb, mlen);
771 }
772 break;
773 };
774
775 /*
776 * And the verifier...
777 */
778 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
779 if (verf_str) {
780 mlen = mbuf_len(mb);
781 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
782 *tl = txdr_unsigned(verf_len);
783 siz = verf_len;
784 while (siz > 0) {
785 if (mbuf_trailingspace(mb) == 0) {
786 mb2 = NULL;
787 if (siz >= nfs_mbuf_minclsize)
788 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
789 else
790 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2);
791 if (!error)
792 error = mbuf_setnext(mb, mb2);
793 if (error) {
794 mbuf_freem(mreq);
795 return (error);
796 }
797 mb = mb2;
798 mlen = 0;
799 bpos = mbuf_data(mb);
800 }
801 i = min(siz, mbuf_trailingspace(mb));
802 bcopy(verf_str, bpos, i);
803 mlen += i;
804 mbuf_setlen(mb, mlen);
805 verf_str += i;
806 bpos += i;
807 siz -= i;
808 }
809 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
810 for (i = 0; i < siz; i++)
811 *bpos++ = '\0';
812 mlen += siz;
813 mbuf_setlen(mb, mlen);
814 }
815 } else {
816 *tl++ = txdr_unsigned(RPCAUTH_NULL);
817 *tl = 0;
818 }
819 error = mbuf_pkthdr_setrcvif(mreq, 0);
820 if (!error)
821 error = mbuf_setnext(mb, mrest);
822 if (error) {
823 mbuf_freem(mreq);
824 return (error);
825 }
826 mbuf_pkthdr_setlen(mreq, authsiz + 10 * NFSX_UNSIGNED + mrest_len);
827 *mbp = mb;
828 *mreqp = mreq;
829 return (0);
830 }
831
832 /*
833 * copies mbuf chain to the uio scatter/gather list
834 */
835 int
836 nfsm_mbuftouio(mrep, uiop, siz, dpos)
837 mbuf_t *mrep;
838 struct uio *uiop;
839 int siz;
840 caddr_t *dpos;
841 {
842 char *mbufcp, *uiocp;
843 int xfer, left, len;
844 mbuf_t mp;
845 long uiosiz, rem;
846 int error = 0;
847
848 mp = *mrep;
849 mbufcp = *dpos;
850 len = (caddr_t)mbuf_data(mp) + mbuf_len(mp) - mbufcp;
851 rem = nfsm_rndup(siz)-siz;
852 while (siz > 0) {
853 if (uiop->uio_iovcnt <= 0 || uiop->uio_iovs.iov32p == NULL)
854 return (EFBIG);
855 // LP64todo - fix this!
856 left = uio_iov_len(uiop);
857 uiocp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
858 if (left > siz)
859 left = siz;
860 uiosiz = left;
861 while (left > 0) {
862 while (len == 0) {
863 mp = mbuf_next(mp);
864 if (mp == NULL)
865 return (EBADRPC);
866 mbufcp = mbuf_data(mp);
867 len = mbuf_len(mp);
868 }
869 xfer = (left > len) ? len : left;
870 if (UIO_SEG_IS_USER_SPACE(uiop->uio_segflg))
871 copyout(mbufcp, CAST_USER_ADDR_T(uiocp), xfer);
872 else
873 bcopy(mbufcp, uiocp, xfer);
874 left -= xfer;
875 len -= xfer;
876 mbufcp += xfer;
877 uiocp += xfer;
878 uiop->uio_offset += xfer;
879 uio_uio_resid_add(uiop, -xfer);
880 }
881 if (uio_iov_len(uiop) <= (size_t)siz) {
882 uiop->uio_iovcnt--;
883 uio_next_iov(uiop);
884 } else {
885 uio_iov_base_add(uiop, uiosiz);
886 uio_iov_len_add(uiop, -uiosiz);
887 }
888 siz -= uiosiz;
889 }
890 *dpos = mbufcp;
891 *mrep = mp;
892 if (rem > 0) {
893 if (len < rem)
894 error = nfs_adv(mrep, dpos, rem, len);
895 else
896 *dpos += rem;
897 }
898 return (error);
899 }
900
901 /*
902 * copies a uio scatter/gather list to an mbuf chain.
903 * NOTE: can ony handle iovcnt == 1
904 */
905 int
906 nfsm_uiotombuf(uiop, mq, siz, bpos)
907 struct uio *uiop;
908 mbuf_t *mq;
909 int siz;
910 caddr_t *bpos;
911 {
912 char *uiocp;
913 mbuf_t mp, mp2;
914 int xfer, left, mlen, mplen;
915 int uiosiz, clflg, rem, error;
916 char *cp;
917
918 if (uiop->uio_iovcnt != 1)
919 panic("nfsm_uiotombuf: iovcnt != 1");
920
921 if (siz > nfs_mbuf_mlen) /* or should it >= MCLBYTES ?? */
922 clflg = 1;
923 else
924 clflg = 0;
925 rem = nfsm_rndup(siz)-siz;
926 mp = mp2 = *mq;
927 mplen = mbuf_len(mp);
928 while (siz > 0) {
929 // LP64todo - fix this!
930 left = uio_iov_len(uiop);
931 uiocp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
932 if (left > siz)
933 left = siz;
934 uiosiz = left;
935 while (left > 0) {
936 mlen = mbuf_trailingspace(mp);
937 if (mlen == 0) {
938 mp = NULL;
939 if (clflg)
940 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
941 else
942 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
943 if (!error)
944 error = mbuf_setnext(mp2, mp);
945 if (error)
946 return (error);
947 mplen = 0;
948 mp2 = mp;
949 mlen = mbuf_trailingspace(mp);
950 }
951 xfer = (left > mlen) ? mlen : left;
952 if (UIO_SEG_IS_USER_SPACE(uiop->uio_segflg))
953 copyin(CAST_USER_ADDR_T(uiocp), (caddr_t)mbuf_data(mp) + mplen, xfer);
954 else
955 bcopy(uiocp, (caddr_t)mbuf_data(mp) + mplen, xfer);
956 mplen += xfer;
957 mbuf_setlen(mp, mplen);
958 left -= xfer;
959 uiocp += xfer;
960 uiop->uio_offset += xfer;
961 uio_uio_resid_add(uiop, -xfer);
962 }
963 uio_iov_base_add(uiop, uiosiz);
964 uio_iov_len_add(uiop, -uiosiz);
965 siz -= uiosiz;
966 }
967 if (rem > 0) {
968 if (rem > mbuf_trailingspace(mp)) {
969 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp);
970 if (!error)
971 error = mbuf_setnext(mp2, mp);
972 if (error)
973 return (error);
974 mplen = 0;
975 }
976 cp = (caddr_t)mbuf_data(mp) + mplen;
977 for (left = 0; left < rem; left++)
978 *cp++ = '\0';
979 mplen += rem;
980 mbuf_setlen(mp, mplen);
981 *bpos = cp;
982 } else {
983 *bpos = (caddr_t)mbuf_data(mp) + mplen;
984 }
985 *mq = mp;
986 return (0);
987 }
988
989 /*
990 * Help break down an mbuf chain by setting the first siz bytes contiguous
991 * pointed to by returned val.
992 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
993 * cases. (The macros use the vars. dpos and dpos2)
994 */
995 int
996 nfsm_disct(mdp, dposp, siz, left, cp2)
997 mbuf_t *mdp;
998 caddr_t *dposp;
999 int siz;
1000 int left;
1001 caddr_t *cp2;
1002 {
1003 mbuf_t mp, mp2;
1004 int siz2, xfer, error, mp2len;
1005 caddr_t p, mp2data;
1006
1007 mp = *mdp;
1008 while (left == 0) {
1009 *mdp = mp = mbuf_next(mp);
1010 if (mp == NULL)
1011 return (EBADRPC);
1012 left = mbuf_len(mp);
1013 *dposp = mbuf_data(mp);
1014 }
1015 if (left >= siz) {
1016 *cp2 = *dposp;
1017 *dposp += siz;
1018 } else if (mbuf_next(mp) == NULL) {
1019 return (EBADRPC);
1020 } else if (siz > nfs_mbuf_mhlen) {
1021 panic("nfs S too big");
1022 } else {
1023 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mp2);
1024 if (error)
1025 return (error);
1026 error = mbuf_setnext(mp2, mbuf_next(mp));
1027 if (!error)
1028 error = mbuf_setnext(mp, mp2);
1029 if (error) {
1030 mbuf_free(mp2);
1031 return (error);
1032 }
1033 mbuf_setlen(mp, mbuf_len(mp) - left);
1034 mp = mp2;
1035 *cp2 = p = mbuf_data(mp);
1036 bcopy(*dposp, p, left); /* Copy what was left */
1037 siz2 = siz-left;
1038 p += left;
1039 mp2 = mbuf_next(mp);
1040 mp2data = mbuf_data(mp2);
1041 mp2len = mbuf_len(mp2);
1042 /* Loop around copying up the siz2 bytes */
1043 while (siz2 > 0) {
1044 if (mp2 == NULL)
1045 return (EBADRPC);
1046 xfer = (siz2 > mp2len) ? mp2len : siz2;
1047 if (xfer > 0) {
1048 bcopy(mp2data, p, xfer);
1049 mp2data += xfer;
1050 mp2len -= xfer;
1051 mbuf_setdata(mp2, mp2data, mp2len);
1052 p += xfer;
1053 siz2 -= xfer;
1054 }
1055 if (siz2 > 0) {
1056 mp2 = mbuf_next(mp2);
1057 mp2data = mbuf_data(mp2);
1058 mp2len = mbuf_len(mp2);
1059 }
1060 }
1061 mbuf_setlen(mp, siz);
1062 *mdp = mp2;
1063 *dposp = mp2data;
1064 }
1065 return (0);
1066 }
1067
1068 /*
1069 * Advance the position in the mbuf chain.
1070 */
1071 int
1072 nfs_adv(mdp, dposp, offs, left)
1073 mbuf_t *mdp;
1074 caddr_t *dposp;
1075 int offs;
1076 int left;
1077 {
1078 mbuf_t m;
1079 int s;
1080
1081 m = *mdp;
1082 s = left;
1083 while (s < offs) {
1084 offs -= s;
1085 m = mbuf_next(m);
1086 if (m == NULL)
1087 return (EBADRPC);
1088 s = mbuf_len(m);
1089 }
1090 *mdp = m;
1091 *dposp = (caddr_t)mbuf_data(m) + offs;
1092 return (0);
1093 }
1094
1095 /*
1096 * Copy a string into mbufs for the hard cases...
1097 */
1098 int
1099 nfsm_strtmbuf(mb, bpos, cp, siz)
1100 mbuf_t *mb;
1101 char **bpos;
1102 char *cp;
1103 long siz;
1104 {
1105 mbuf_t m1 = NULL, m2;
1106 long left, xfer, len, tlen, mlen;
1107 u_long *tl;
1108 int putsize, error;
1109
1110 putsize = 1;
1111 m2 = *mb;
1112 left = mbuf_trailingspace(m2);
1113 if (left >= NFSX_UNSIGNED) {
1114 tl = ((u_long *)(*bpos));
1115 *tl++ = txdr_unsigned(siz);
1116 putsize = 0;
1117 left -= NFSX_UNSIGNED;
1118 len = mbuf_len(m2);
1119 len += NFSX_UNSIGNED;
1120 mbuf_setlen(m2, len);
1121 if (left > 0) {
1122 bcopy(cp, (caddr_t) tl, left);
1123 siz -= left;
1124 cp += left;
1125 len += left;
1126 mbuf_setlen(m2, len);
1127 left = 0;
1128 }
1129 }
1130 /* Loop around adding mbufs */
1131 while (siz > 0) {
1132 m1 = NULL;
1133 if (siz > nfs_mbuf_mlen)
1134 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &m1);
1135 else
1136 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &m1);
1137 if (!error)
1138 error = mbuf_setnext(m2, m1);
1139 if (error)
1140 return (error);
1141 mlen = mbuf_maxlen(m1);
1142 mbuf_setlen(m1, mlen);
1143 m2 = m1;
1144 tl = mbuf_data(m1);
1145 tlen = 0;
1146 if (putsize) {
1147 *tl++ = txdr_unsigned(siz);
1148 mlen -= NFSX_UNSIGNED;
1149 mbuf_setlen(m1, mlen);
1150 tlen = NFSX_UNSIGNED;
1151 putsize = 0;
1152 }
1153 if (siz < mlen) {
1154 len = nfsm_rndup(siz);
1155 xfer = siz;
1156 if (xfer < len)
1157 *(tl+(xfer>>2)) = 0;
1158 } else {
1159 xfer = len = mlen;
1160 }
1161 bcopy(cp, (caddr_t) tl, xfer);
1162 mbuf_setlen(m1, len + tlen);
1163 siz -= xfer;
1164 cp += xfer;
1165 }
1166 *mb = m1;
1167 *bpos = (caddr_t)mbuf_data(m1) + mbuf_len(m1);
1168 return (0);
1169 }
1170
1171 /*
1172 * Called once to initialize data structures...
1173 */
1174 int
1175 nfs_init(struct vfsconf *vfsp)
1176 {
1177 int i;
1178
1179 /*
1180 * Check to see if major data structures haven't bloated.
1181 */
1182 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1183 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1184 printf("Try reducing NFS_SMALLFH\n");
1185 }
1186 if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
1187 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
1188 printf("Try reducing NFS_MUIDHASHSIZ\n");
1189 }
1190 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1191 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1192 printf("Try reducing NFS_UIDHASHSIZ\n");
1193 }
1194 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1195 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1196 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1197 }
1198
1199 nfs_mount_type = vfsp->vfc_typenum;
1200 nfsrtt.pos = 0;
1201 rpc_vers = txdr_unsigned(RPC_VER2);
1202 rpc_call = txdr_unsigned(RPC_CALL);
1203 rpc_reply = txdr_unsigned(RPC_REPLY);
1204 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1205 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1206 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1207 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1208 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1209 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1210 nfs_prog = txdr_unsigned(NFS_PROG);
1211 nfs_true = txdr_unsigned(TRUE);
1212 nfs_false = txdr_unsigned(FALSE);
1213 nfs_xdrneg1 = txdr_unsigned(-1);
1214
1215 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1216 if (nfs_ticks < 1)
1217 nfs_ticks = 1;
1218 /* Ensure async daemons disabled */
1219 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1220 nfs_iodwant[i] = NULL;
1221 nfs_iodmount[i] = (struct nfsmount *)0;
1222 }
1223 /* init nfsiod mutex */
1224 nfs_iod_lck_grp_attr = lck_grp_attr_alloc_init();
1225 nfs_iod_lck_grp = lck_grp_alloc_init("nfs_iod", nfs_iod_lck_grp_attr);
1226 nfs_iod_lck_attr = lck_attr_alloc_init();
1227 nfs_iod_mutex = lck_mtx_alloc_init(nfs_iod_lck_grp, nfs_iod_lck_attr);
1228
1229 nfs_nbinit(); /* Init the nfsbuf table */
1230 nfs_nhinit(); /* Init the nfsnode table */
1231 nfs_lockinit(); /* Init the nfs lock state */
1232
1233 #ifndef NFS_NOSERVER
1234 /* init nfsd mutex */
1235 nfsd_lck_grp_attr = lck_grp_attr_alloc_init();
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 = htonl(NFS_FH_VERSION);
2546 nx->nx_fh.nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
2547 nx->nx_fh.nfh_xh.nxh_expid = htonl(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 uint32_t fsid, expid;
2660
2661 fsid = ntohl(nfhp->nfh_xh.nxh_fsid);
2662 expid = ntohl(nfhp->nfh_xh.nxh_expid);
2663 nx = NFSEXPHASH(fsid, expid)->lh_first;
2664 for (; nx; nx = LIST_NEXT(nx, nx_hash)) {
2665 if (nx->nx_fs->nxfs_id != fsid)
2666 continue;
2667 if (nx->nx_id != expid)
2668 continue;
2669 break;
2670 }
2671 return nx;
2672 }
2673
2674 /*
2675 * nfsrv_fhtovp() - convert FH to vnode and export info
2676 */
2677 int
2678 nfsrv_fhtovp(
2679 struct nfs_filehandle *nfhp,
2680 mbuf_t nam,
2681 __unused int pubflag,
2682 vnode_t *vpp,
2683 struct nfs_export **nxp,
2684 struct nfs_export_options **nxop)
2685 {
2686 int error;
2687 struct mount *mp;
2688 uint32_t v;
2689
2690 *vpp = NULL;
2691 *nxp = NULL;
2692 *nxop = NULL;
2693
2694 v = ntohl(nfhp->nfh_xh.nxh_version);
2695 if (v != NFS_FH_VERSION) {
2696 /* file handle format not supported */
2697 return (ESTALE);
2698 }
2699 if (nfhp->nfh_len > NFS_MAX_FH_SIZE)
2700 return (EBADRPC);
2701 if (nfhp->nfh_len < (int)sizeof(nfhp->nfh_xh))
2702 return (ESTALE);
2703 v = ntohs(nfhp->nfh_xh.nxh_flags);
2704 if (v & NXHF_INVALIDFH)
2705 return (ESTALE);
2706
2707 /* XXX Revisit when enabling WebNFS */
2708 #ifdef WEBNFS_ENABLED
2709 if (nfs_ispublicfh(nfhp)) {
2710 if (!pubflag || !nfs_pub.np_valid)
2711 return (ESTALE);
2712 nfhp = &nfs_pub.np_handle;
2713 }
2714 #endif
2715
2716 *nxp = nfsrv_fhtoexport(nfhp);
2717 if (!*nxp)
2718 return (ESTALE);
2719
2720 /* Get the export option structure for this <export, client> tuple. */
2721 *nxop = nfsrv_export_lookup(*nxp, nam);
2722 if (nam && (*nxop == NULL))
2723 return (EACCES);
2724
2725 /* find mount structure */
2726 mp = vfs_getvfs_by_mntonname((*nxp)->nx_fs->nxfs_path);
2727 if (!mp)
2728 return (ESTALE);
2729
2730 error = VFS_FHTOVP(mp, nfhp->nfh_xh.nxh_fidlen, &nfhp->nfh_fid[0], vpp, NULL);
2731 if (error)
2732 return (error);
2733 /* vnode pointer should be good at this point or ... */
2734 if (*vpp == NULL)
2735 return (ESTALE);
2736 return (0);
2737 }
2738
2739 /*
2740 * nfsrv_credcheck() - check/map credentials according to given export options
2741 */
2742 int
2743 nfsrv_credcheck(
2744 struct nfsrv_descript *nfsd,
2745 __unused struct nfs_export *nx,
2746 struct nfs_export_options *nxo)
2747 {
2748 if (nxo && nxo->nxo_cred) {
2749 if ((nxo->nxo_flags & NX_MAPALL) ||
2750 ((nxo->nxo_flags & NX_MAPROOT) && !suser(nfsd->nd_cr, NULL))) {
2751 kauth_cred_rele(nfsd->nd_cr);
2752 nfsd->nd_cr = nxo->nxo_cred;
2753 kauth_cred_ref(nfsd->nd_cr);
2754 }
2755 }
2756 return (0);
2757 }
2758
2759
2760 /*
2761 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2762 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2763 * transformed this to all zeroes in both cases, so check for it.
2764 */
2765 int
2766 nfs_ispublicfh(struct nfs_filehandle *nfhp)
2767 {
2768 char *cp = (char *)nfhp;
2769 unsigned int i;
2770
2771 if (nfhp->nfh_len == 0)
2772 return (TRUE);
2773 if (nfhp->nfh_len != NFSX_V2FH)
2774 return (FALSE);
2775 for (i = 0; i < NFSX_V2FH; i++)
2776 if (*cp++ != 0)
2777 return (FALSE);
2778 return (TRUE);
2779 }
2780
2781 /*
2782 * nfsrv_vptofh() - convert vnode to file handle for given export
2783 *
2784 * If the caller is passing in a vnode for a ".." directory entry,
2785 * they can pass a directory NFS file handle (dnfhp) which will be
2786 * checked against the root export file handle. If it matches, we
2787 * refuse to provide the file handle for the out-of-export directory.
2788 */
2789 int
2790 nfsrv_vptofh(
2791 struct nfs_export *nx,
2792 int v2,
2793 struct nfs_filehandle *dnfhp,
2794 vnode_t vp,
2795 struct vfs_context *ctx,
2796 struct nfs_filehandle *nfhp)
2797 {
2798 int error;
2799
2800 nfhp->nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
2801 nfhp->nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
2802 nfhp->nfh_xh.nxh_expid = htonl(nx->nx_id);
2803 nfhp->nfh_xh.nxh_flags = 0;
2804 nfhp->nfh_xh.nxh_reserved = 0;
2805
2806 if (v2)
2807 bzero(&nfhp->nfh_fid[0], NFSV2_MAX_FID_SIZE);
2808
2809 /* if directory FH matches export root, return invalid FH */
2810 if (dnfhp && nfsrv_fhmatch(dnfhp, &nx->nx_fh)) {
2811 nfhp->nfh_len = v2 ? NFSX_V2FH : sizeof(nfhp->nfh_xh);
2812 nfhp->nfh_xh.nxh_fidlen = 0;
2813 nfhp->nfh_xh.nxh_flags = htons(NXHF_INVALIDFH);
2814 return (0);
2815 }
2816
2817 nfhp->nfh_len = v2 ? NFSV2_MAX_FID_SIZE : NFS_MAX_FID_SIZE;
2818 error = VFS_VPTOFH(vp, &nfhp->nfh_len, &nfhp->nfh_fid[0], ctx);
2819 if (error)
2820 return (error);
2821 if (nfhp->nfh_len > (int)(v2 ? NFSV2_MAX_FID_SIZE : NFS_MAX_FID_SIZE))
2822 return (EOVERFLOW);
2823 nfhp->nfh_xh.nxh_fidlen = nfhp->nfh_len;
2824 nfhp->nfh_len += sizeof(nfhp->nfh_xh);
2825 if (v2 && (nfhp->nfh_len < NFSX_V2FH))
2826 nfhp->nfh_len = NFSX_V2FH;
2827
2828 return (0);
2829 }
2830
2831 int
2832 nfsrv_fhmatch(struct nfs_filehandle *fh1, struct nfs_filehandle *fh2)
2833 {
2834 int len1, len2;
2835
2836 len1 = sizeof(fh1->nfh_xh) + fh1->nfh_xh.nxh_fidlen;
2837 len2 = sizeof(fh2->nfh_xh) + fh2->nfh_xh.nxh_fidlen;
2838 if (len1 != len2)
2839 return (0);
2840 if (bcmp(&fh1->nfh_xh, &fh2->nfh_xh, len1))
2841 return (0);
2842 return (1);
2843 }
2844
2845 #endif /* NFS_NOSERVER */
2846 /*
2847 * This function compares two net addresses by family and returns TRUE
2848 * if they are the same host.
2849 * If there is any doubt, return FALSE.
2850 * The AF_INET family is handled as a special case so that address mbufs
2851 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2852 */
2853 int
2854 netaddr_match(family, haddr, nam)
2855 int family;
2856 union nethostaddr *haddr;
2857 mbuf_t nam;
2858 {
2859 struct sockaddr_in *inetaddr;
2860
2861 switch (family) {
2862 case AF_INET:
2863 inetaddr = mbuf_data(nam);
2864 if (inetaddr->sin_family == AF_INET &&
2865 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2866 return (1);
2867 break;
2868 #if ISO
2869 case AF_ISO:
2870 {
2871 struct sockaddr_iso *isoaddr1, *isoaddr2;
2872
2873 isoaddr1 = mbuf_data(nam);
2874 isoaddr2 = mbuf_data(haddr->had_nam);
2875 if (isoaddr1->siso_family == AF_ISO &&
2876 isoaddr1->siso_nlen > 0 &&
2877 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2878 SAME_ISOADDR(isoaddr1, isoaddr2))
2879 return (1);
2880 break;
2881 }
2882 #endif /* ISO */
2883 default:
2884 break;
2885 };
2886 return (0);
2887 }
2888
2889 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
2890 /*
2891 * This function finds the directory cookie that corresponds to the
2892 * logical byte offset given.
2893 */
2894 nfsuint64 *
2895 nfs_getcookie(np, off, add)
2896 struct nfsnode *np;
2897 off_t off;
2898 int add;
2899 {
2900 struct nfsdmap *dp, *dp2;
2901 int pos;
2902
2903 pos = off / NFS_DIRBLKSIZ;
2904 if (pos == 0) {
2905 #if DIAGNOSTIC
2906 if (add)
2907 panic("nfs getcookie add at 0");
2908 #endif
2909 return (&nfs_nullcookie);
2910 }
2911 pos--;
2912 dp = np->n_cookies.lh_first;
2913 if (!dp) {
2914 if (add) {
2915 MALLOC_ZONE(dp, struct nfsdmap *, sizeof(struct nfsdmap),
2916 M_NFSDIROFF, M_WAITOK);
2917 if (!dp)
2918 return ((nfsuint64 *)0);
2919 dp->ndm_eocookie = 0;
2920 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2921 } else
2922 return ((nfsuint64 *)0);
2923 }
2924 while (pos >= NFSNUMCOOKIES) {
2925 pos -= NFSNUMCOOKIES;
2926 if (dp->ndm_list.le_next) {
2927 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2928 pos >= dp->ndm_eocookie)
2929 return ((nfsuint64 *)0);
2930 dp = dp->ndm_list.le_next;
2931 } else if (add) {
2932 MALLOC_ZONE(dp2, struct nfsdmap *, sizeof(struct nfsdmap),
2933 M_NFSDIROFF, M_WAITOK);
2934 if (!dp2)
2935 return ((nfsuint64 *)0);
2936 dp2->ndm_eocookie = 0;
2937 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2938 dp = dp2;
2939 } else
2940 return ((nfsuint64 *)0);
2941 }
2942 if (pos >= dp->ndm_eocookie) {
2943 if (add)
2944 dp->ndm_eocookie = pos + 1;
2945 else
2946 return ((nfsuint64 *)0);
2947 }
2948 return (&dp->ndm_cookies[pos]);
2949 }
2950
2951 /*
2952 * Invalidate cached directory information, except for the actual directory
2953 * blocks (which are invalidated separately).
2954 * Done mainly to avoid the use of stale offset cookies.
2955 */
2956 void
2957 nfs_invaldir(vp)
2958 vnode_t vp;
2959 {
2960 struct nfsnode *np = VTONFS(vp);
2961
2962 #if DIAGNOSTIC
2963 if (vnode_vtype(vp) != VDIR)
2964 panic("nfs: invaldir not dir");
2965 #endif
2966 np->n_direofoffset = 0;
2967 np->n_cookieverf.nfsuquad[0] = 0;
2968 np->n_cookieverf.nfsuquad[1] = 0;
2969 if (np->n_cookies.lh_first)
2970 np->n_cookies.lh_first->ndm_eocookie = 0;
2971 }
2972
2973 /*
2974 * The write verifier has changed (probably due to a server reboot), so all
2975 * NB_NEEDCOMMIT blocks will have to be written again. Since they are on the
2976 * dirty block list as NB_DELWRI, all this takes is clearing the NB_NEEDCOMMIT
2977 * flag. Once done the new write verifier can be set for the mount point.
2978 */
2979 static int
2980 nfs_clearcommit_callout(vnode_t vp, __unused void *arg)
2981 {
2982 struct nfsnode *np = VTONFS(vp);
2983 struct nfsbuflists blist;
2984 struct nfsbuf *bp;
2985
2986 lck_mtx_lock(nfs_buf_mutex);
2987 if (nfs_buf_iterprepare(np, &blist, NBI_DIRTY)) {
2988 lck_mtx_unlock(nfs_buf_mutex);
2989 return (VNODE_RETURNED);
2990 }
2991 LIST_FOREACH(bp, &blist, nb_vnbufs) {
2992 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0))
2993 continue;
2994 if ((bp->nb_flags & (NB_DELWRI | NB_NEEDCOMMIT))
2995 == (NB_DELWRI | NB_NEEDCOMMIT)) {
2996 bp->nb_flags &= ~NB_NEEDCOMMIT;
2997 np->n_needcommitcnt--;
2998 }
2999 nfs_buf_drop(bp);
3000 }
3001 CHECK_NEEDCOMMITCNT(np);
3002 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
3003 lck_mtx_unlock(nfs_buf_mutex);
3004 return (VNODE_RETURNED);
3005 }
3006
3007 void
3008 nfs_clearcommit(mount_t mp)
3009 {
3010 vnode_iterate(mp, VNODE_NOLOCK_INTERNAL, nfs_clearcommit_callout, NULL);
3011 }
3012
3013 #ifndef NFS_NOSERVER
3014 /*
3015 * Map errnos to NFS error numbers. For Version 3 also filter out error
3016 * numbers not specified for the associated procedure.
3017 */
3018 int
3019 nfsrv_errmap(nd, err)
3020 struct nfsrv_descript *nd;
3021 int err;
3022 {
3023 short *defaulterrp, *errp;
3024
3025 if (nd->nd_flag & ND_NFSV3) {
3026 if (nd->nd_procnum <= NFSPROC_COMMIT) {
3027 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
3028 while (*++errp) {
3029 if (*errp == err)
3030 return (err);
3031 else if (*errp > err)
3032 break;
3033 }
3034 return ((int)*defaulterrp);
3035 } else
3036 return (err & 0xffff);
3037 }
3038 if (err <= ELAST)
3039 return ((int)nfsrv_v2errmap[err - 1]);
3040 return (NFSERR_IO);
3041 }
3042
3043 #endif /* NFS_NOSERVER */
3044