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