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