2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
65 * FreeBSD-Id: nfs_serv.c,v 1.52 1997/10/28 15:59:05 bde Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kauth.h>
72 #include <sys/unistd.h>
73 #include <sys/malloc.h>
74 #include <sys/vnode.h>
75 #include <sys/mount_internal.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/kpi_mbuf.h>
79 #include <sys/dirent.h>
81 #include <sys/kernel.h>
83 #include <sys/vnode_internal.h>
84 #include <sys/uio_internal.h>
85 #include <libkern/OSAtomic.h>
86 #include <sys/fsevents.h>
87 #include <kern/thread_call.h>
90 #include <sys/vmparam.h>
92 #include <netinet/in.h>
94 #include <nfs/nfsproto.h>
95 #include <nfs/rpcv2.h>
97 #include <nfs/xdr_subs.h>
98 #include <nfs/nfsm_subs.h>
99 #include <nfs/nfsrvcache.h>
100 #include <nfs/nfs_gss.h>
108 int nfsd_thread_count
= 0;
109 int nfsd_thread_max
= 0;
110 lck_grp_t
*nfsd_lck_grp
;
111 lck_mtx_t
*nfsd_mutex
;
112 struct nfsd_head nfsd_head
, nfsd_queue
;
114 lck_grp_t
*nfsrv_slp_rwlock_group
;
115 lck_grp_t
*nfsrv_slp_mutex_group
;
116 struct nfsrv_sockhead nfsrv_socklist
, nfsrv_sockwg
,
117 nfsrv_sockwait
, nfsrv_sockwork
;
118 struct nfsrv_sock
*nfsrv_udpsock
= NULL
;
119 struct nfsrv_sock
*nfsrv_udp6sock
= NULL
;
122 struct nfsrv_expfs_list nfsrv_exports
;
123 struct nfsrv_export_hashhead
*nfsrv_export_hashtbl
= NULL
;
124 int nfsrv_export_hash_size
= NFSRVEXPHASHSZ
;
125 u_long nfsrv_export_hash
;
126 lck_grp_t
*nfsrv_export_rwlock_group
;
127 lck_rw_t nfsrv_export_rwlock
;
130 /* NFS server file modification event generator */
131 struct nfsrv_fmod_hashhead
*nfsrv_fmod_hashtbl
;
132 u_long nfsrv_fmod_hash
;
133 lck_grp_t
*nfsrv_fmod_grp
;
134 lck_mtx_t
*nfsrv_fmod_mutex
;
135 static int nfsrv_fmod_timer_on
= 0;
136 int nfsrv_fsevents_enabled
= 1;
139 /* NFS server timers */
141 thread_call_t nfsrv_fmod_timer_call
;
143 thread_call_t nfsrv_idlesock_timer_call
;
144 thread_call_t nfsrv_wg_timer_call
;
145 int nfsrv_wg_timer_on
;
147 /* globals for the active user list */
148 uint32_t nfsrv_user_stat_enabled
= 1;
149 uint32_t nfsrv_user_stat_node_count
= 0;
150 uint32_t nfsrv_user_stat_max_idle_sec
= NFSRV_USER_STAT_DEF_IDLE_SEC
;
151 uint32_t nfsrv_user_stat_max_nodes
= NFSRV_USER_STAT_DEF_MAX_NODES
;
152 lck_grp_t
*nfsrv_active_user_mutex_group
;
154 int nfsrv_wg_delay
= NFSRV_WGATHERDELAY
* 1000;
155 int nfsrv_wg_delay_v3
= 0;
159 int nfsrv_authorize(vnode_t
,vnode_t
,kauth_action_t
,vfs_context_t
,struct nfs_export_options
*,int);
160 int nfsrv_wg_coalesce(struct nfsrv_descript
*, struct nfsrv_descript
*);
161 void nfsrv_modified(vnode_t
, vfs_context_t
);
163 extern void IOSleep(int);
164 extern int safe_getpath(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
);
167 * Initialize the data structures for the server.
170 #define NFSRV_NOT_INITIALIZED 0
171 #define NFSRV_INITIALIZING 1
172 #define NFSRV_INITIALIZED 2
173 static volatile UInt32 nfsrv_initted
= NFSRV_NOT_INITIALIZED
;
176 nfsrv_is_initialized(void)
178 return (nfsrv_initted
== NFSRV_INITIALIZED
);
184 /* make sure we init only once */
185 if (!OSCompareAndSwap(NFSRV_NOT_INITIALIZED
, NFSRV_INITIALIZING
, &nfsrv_initted
)) {
186 /* wait until initialization is complete */
187 while (!nfsrv_is_initialized())
192 if (sizeof (struct nfsrv_sock
) > NFS_SVCALLOC
)
193 printf("struct nfsrv_sock bloated (> %dbytes)\n",NFS_SVCALLOC
);
195 /* init nfsd mutex */
196 nfsd_lck_grp
= lck_grp_alloc_init("nfsd", LCK_GRP_ATTR_NULL
);
197 nfsd_mutex
= lck_mtx_alloc_init(nfsd_lck_grp
, LCK_ATTR_NULL
);
199 /* init slp rwlock */
200 nfsrv_slp_rwlock_group
= lck_grp_alloc_init("nfsrv-slp-rwlock", LCK_GRP_ATTR_NULL
);
201 nfsrv_slp_mutex_group
= lck_grp_alloc_init("nfsrv-slp-mutex", LCK_GRP_ATTR_NULL
);
203 /* init export data structures */
204 LIST_INIT(&nfsrv_exports
);
205 nfsrv_export_rwlock_group
= lck_grp_alloc_init("nfsrv-export-rwlock", LCK_GRP_ATTR_NULL
);
206 lck_rw_init(&nfsrv_export_rwlock
, nfsrv_export_rwlock_group
, LCK_ATTR_NULL
);
208 /* init active user list mutex structures */
209 nfsrv_active_user_mutex_group
= lck_grp_alloc_init("nfs-active-user-mutex", LCK_GRP_ATTR_NULL
);
211 /* init nfs server request cache mutex */
212 nfsrv_reqcache_lck_grp
= lck_grp_alloc_init("nfsrv_reqcache", LCK_GRP_ATTR_NULL
);
213 nfsrv_reqcache_mutex
= lck_mtx_alloc_init(nfsrv_reqcache_lck_grp
, LCK_ATTR_NULL
);
216 /* init NFS server file modified event generation */
217 nfsrv_fmod_hashtbl
= hashinit(NFSRVFMODHASHSZ
, M_TEMP
, &nfsrv_fmod_hash
);
218 nfsrv_fmod_grp
= lck_grp_alloc_init("nfsrv_fmod", LCK_GRP_ATTR_NULL
);
219 nfsrv_fmod_mutex
= lck_mtx_alloc_init(nfsrv_fmod_grp
, LCK_ATTR_NULL
);
222 /* initialize NFS server timer callouts */
224 nfsrv_fmod_timer_call
= thread_call_allocate(nfsrv_fmod_timer
, NULL
);
226 nfsrv_idlesock_timer_call
= thread_call_allocate(nfsrv_idlesock_timer
, NULL
);
227 nfsrv_wg_timer_call
= thread_call_allocate(nfsrv_wg_timer
, NULL
);
229 /* Init server data structures */
230 TAILQ_INIT(&nfsrv_socklist
);
231 TAILQ_INIT(&nfsrv_sockwait
);
232 TAILQ_INIT(&nfsrv_sockwork
);
233 TAILQ_INIT(&nfsrv_sockwg
);
234 TAILQ_INIT(&nfsd_head
);
235 TAILQ_INIT(&nfsd_queue
);
236 nfsrv_udpsock
= NULL
;
237 nfsrv_udp6sock
= NULL
;
239 /* Setup the up-call handling */
242 /* initialization complete */
243 nfsrv_initted
= NFSRV_INITIALIZED
;
249 * NFS version 2 and 3 server request processing functions
251 * These functions take the following parameters:
253 * struct nfsrv_descript *nd - the NFS request descriptor
254 * struct nfsrv_sock *slp - the NFS socket the request came in on
255 * vfs_context_t ctx - VFS context
256 * mbuf_t *mrepp - pointer to hold the reply mbuf list
258 * These routines generally have 3 phases:
260 * 1 - break down and validate the RPC request in the mbuf chain
261 * provided in nd->nd_nmreq.
262 * 2 - perform the vnode operations for the request
263 * (many are very similar to syscalls in vfs_syscalls.c and
264 * should therefore be kept in sync with those implementations)
265 * 3 - build the RPC reply in an mbuf chain (nmrep) and return the mbuf chain
270 * nfs v3 access service
274 struct nfsrv_descript
*nd
,
275 struct nfsrv_sock
*slp
,
279 struct nfsm_chain
*nmreq
, nmrep
;
282 struct vnode_attr vattr
;
283 struct nfs_filehandle nfh
;
285 kauth_action_t testaction
;
286 struct nfs_export
*nx
;
287 struct nfs_export_options
*nxo
;
292 nmreq
= &nd
->nd_nmreq
;
293 nfsm_chain_null(&nmrep
);
297 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
298 nfsm_chain_get_32(error
, nmreq
, nfsmode
);
300 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
303 /* update export stats */
304 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
306 /* update active user stats */
307 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
309 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
313 * Each NFS mode bit is tested separately.
315 * XXX this code is nominally correct, but returns a pessimistic
316 * rather than optimistic result. It will be necessary to add
317 * an NFS-specific interface to the vnode_authorize code to
318 * obtain good performance in the optimistic mode.
320 if (nfsmode
& NFS_ACCESS_READ
) {
321 testaction
= vnode_isdir(vp
) ? KAUTH_VNODE_LIST_DIRECTORY
: KAUTH_VNODE_READ_DATA
;
322 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
323 nfsmode
&= ~NFS_ACCESS_READ
;
325 if ((nfsmode
& NFS_ACCESS_LOOKUP
) &&
327 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
, nxo
, 0)))
328 nfsmode
&= ~NFS_ACCESS_LOOKUP
;
329 if (nfsmode
& NFS_ACCESS_MODIFY
) {
330 if (vnode_isdir(vp
)) {
332 KAUTH_VNODE_ADD_FILE
|
333 KAUTH_VNODE_ADD_SUBDIRECTORY
|
334 KAUTH_VNODE_DELETE_CHILD
;
337 KAUTH_VNODE_WRITE_DATA
;
339 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
340 nfsmode
&= ~NFS_ACCESS_MODIFY
;
342 if (nfsmode
& NFS_ACCESS_EXTEND
) {
343 if (vnode_isdir(vp
)) {
345 KAUTH_VNODE_ADD_FILE
|
346 KAUTH_VNODE_ADD_SUBDIRECTORY
;
349 KAUTH_VNODE_WRITE_DATA
|
350 KAUTH_VNODE_APPEND_DATA
;
352 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
353 nfsmode
&= ~NFS_ACCESS_EXTEND
;
357 * Note concerning NFS_ACCESS_DELETE:
358 * For hard links, the answer may be wrong if the vnode
359 * has multiple parents with different permissions.
360 * Also, some clients (e.g. MacOSX 10.3) may incorrectly
361 * interpret the missing/cleared DELETE bit.
362 * So we'll just leave the DELETE bit alone. At worst,
363 * we're telling the client it might be able to do
364 * something it really can't.
367 if ((nfsmode
& NFS_ACCESS_EXECUTE
) &&
369 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
, nxo
, 0)))
370 nfsmode
&= ~NFS_ACCESS_EXECUTE
;
372 /* get postop attributes */
373 nfsm_srv_vattr_init(&vattr
, NFS_VER3
);
374 attrerr
= vnode_getattr(vp
, &vattr
, ctx
);
378 nd
->nd_repstat
= error
;
379 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(NFS_VER3
) + NFSX_UNSIGNED
);
381 *mrepp
= nmrep
.nmc_mhead
;
382 nfsmout_on_status(nd
, error
);
383 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &vattr
);
385 nfsm_chain_add_32(error
, &nmrep
, nfsmode
);
387 nfsm_chain_build_done(error
, &nmrep
);
391 nfsm_chain_cleanup(&nmrep
);
398 * nfs getattr service
402 struct nfsrv_descript
*nd
,
403 struct nfsrv_sock
*slp
,
407 struct nfsm_chain
*nmreq
, nmrep
;
408 struct vnode_attr vattr
;
411 struct nfs_filehandle nfh
;
412 struct nfs_export
*nx
;
413 struct nfs_export_options
*nxo
;
416 nmreq
= &nd
->nd_nmreq
;
417 nfsm_chain_null(&nmrep
);
421 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
423 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
426 /* update export stats */
427 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
429 /* update active user stats */
430 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
432 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
435 nfsm_srv_vattr_init(&vattr
, nd
->nd_vers
);
436 error
= vnode_getattr(vp
, &vattr
, ctx
);
442 nd
->nd_repstat
= error
;
443 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_FATTR(nd
->nd_vers
));
445 *mrepp
= nmrep
.nmc_mhead
;
446 nfsmout_if(nd
->nd_repstat
);
447 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &vattr
);
449 nfsm_chain_build_done(error
, &nmrep
);
453 nfsm_chain_cleanup(&nmrep
);
460 * nfs setattr service
464 struct nfsrv_descript
*nd
,
465 struct nfsrv_sock
*slp
,
469 struct nfsm_chain
*nmreq
, nmrep
;
470 struct vnode_attr preattr
, postattr
;
471 struct vnode_attr vattr
, *vap
= &vattr
;
473 struct nfs_export
*nx
;
474 struct nfs_export_options
*nxo
;
475 int error
, preattrerr
, postattrerr
, gcheck
;
476 struct nfs_filehandle nfh
;
477 struct timespec guard
= { 0, 0 };
478 kauth_action_t action
;
482 preattrerr
= postattrerr
= ENOENT
;
484 nmreq
= &nd
->nd_nmreq
;
485 nfsm_chain_null(&nmrep
);
489 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
493 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
494 if (nd
->nd_vers
== NFS_VER3
) {
495 nfsm_chain_get_32(error
, nmreq
, gcheck
);
497 nfsm_chain_get_time(error
, nmreq
, nd
->nd_vers
, guard
.tv_sec
, guard
.tv_nsec
);
502 * Save the original credential UID in case they are
503 * mapped and we need to map the IDs in the attributes.
505 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
508 * Now that we have all the fields, lets do it.
510 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
513 /* update export stats */
514 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
516 /* update active user stats */
517 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
519 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
522 if (nd
->nd_vers
== NFS_VER3
) {
523 nfsm_srv_pre_vattr_init(&preattr
);
524 error
= preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
525 if (!error
&& gcheck
&& VATTR_IS_SUPPORTED(&preattr
, va_change_time
) &&
526 (preattr
.va_change_time
.tv_sec
!= guard
.tv_sec
||
527 preattr
.va_change_time
.tv_nsec
!= guard
.tv_nsec
))
528 error
= NFSERR_NOT_SYNC
;
529 if (!preattrerr
&& !VATTR_ALL_SUPPORTED(&preattr
))
535 * If the credentials were mapped, we should
536 * map the same values in the attributes.
538 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nd
->nd_cr
) != saved_uid
)) {
540 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nd
->nd_cr
));
541 if (kauth_cred_ismember_gid(nd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
542 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nd
->nd_cr
));
545 /* Authorize the attribute changes. */
546 error
= vnode_authattr(vp
, vap
, &action
, ctx
);
548 error
= nfsrv_authorize(vp
, NULL
, action
, ctx
, nxo
, 0);
550 /* set the new attributes */
552 error
= vnode_setattr(vp
, vap
, ctx
);
554 if (!error
|| (nd
->nd_vers
== NFS_VER3
)) {
555 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
556 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
566 nd
->nd_repstat
= error
;
567 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCORFATTR(nd
->nd_vers
));
569 *mrepp
= nmrep
.nmc_mhead
;
570 nfsmout_on_status(nd
, error
);
571 if (nd
->nd_vers
== NFS_VER3
)
572 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
573 preattrerr
, &preattr
, postattrerr
, &postattr
);
575 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
577 nfsm_chain_build_done(error
, &nmrep
);
579 nfsm_chain_cleanup(&nmrep
);
590 struct nfsrv_descript
*nd
,
591 struct nfsrv_sock
*slp
,
596 vnode_t vp
, dirp
= NULL
;
597 struct nfs_filehandle dnfh
, nfh
;
598 struct nfs_export
*nx
= NULL
;
599 struct nfs_export_options
*nxo
;
600 int error
, attrerr
, dirattrerr
, isdotdot
;
603 struct vnode_attr va
, dirattr
, *vap
= &va
;
604 struct nfsm_chain
*nmreq
, nmrep
;
607 attrerr
= dirattrerr
= ENOENT
;
608 nmreq
= &nd
->nd_nmreq
;
609 nfsm_chain_null(&nmrep
);
610 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
612 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
613 nfsm_chain_get_32(error
, nmreq
, len
);
614 nfsm_name_len_check(error
, nd
, len
);
617 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
619 ni
.ni_op
= OP_LOOKUP
;
621 ni
.ni_cnd
.cn_flags
= LOCKLEAF
;
622 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
623 isdotdot
= ((len
== 2) && (ni
.ni_cnd
.cn_pnbuf
[0] == '.') && (ni
.ni_cnd
.cn_pnbuf
[1] == '.'));
625 error
= nfsrv_namei(nd
, ctx
, &ni
, &dnfh
, &dirp
, &nx
, &nxo
);
627 /* update export stats */
628 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
630 /* update active user stats */
631 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
636 if (nd
->nd_vers
== NFS_VER3
) {
637 nfsm_srv_vattr_init(&dirattr
, NFS_VER3
);
638 dirattrerr
= vnode_getattr(dirp
, &dirattr
, ctx
);
647 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, (isdotdot
? &dnfh
: NULL
), vp
, ctx
, &nfh
);
649 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
650 attrerr
= vnode_getattr(vp
, vap
, ctx
);
656 nd
->nd_repstat
= error
;
657 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
658 NFSX_POSTOPORFATTR(nd
->nd_vers
) + NFSX_POSTOPATTR(nd
->nd_vers
));
660 *mrepp
= nmrep
.nmc_mhead
;
661 if (nd
->nd_repstat
) {
662 if (nd
->nd_vers
== NFS_VER3
)
663 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, dirattrerr
, &dirattr
);
666 nfsm_chain_add_fh(error
, &nmrep
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
667 if (nd
->nd_vers
== NFS_VER3
) {
668 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, vap
);
669 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, dirattrerr
, &dirattr
);
671 error
= nfsm_chain_add_fattr(nd
, &nmrep
, vap
);
674 nfsm_chain_build_done(error
, &nmrep
);
676 nfsm_chain_cleanup(&nmrep
);
683 * nfs readlink service
687 struct nfsrv_descript
*nd
,
688 struct nfsrv_sock
*slp
,
692 int error
, mpcnt
, tlen
, len
, attrerr
;
694 struct vnode_attr vattr
;
695 struct nfs_filehandle nfh
;
696 struct nfs_export
*nx
;
697 struct nfs_export_options
*nxo
;
698 struct nfsm_chain
*nmreq
, nmrep
;
701 char uio_buf
[ UIO_SIZEOF(4) ];
702 char *uio_bufp
= &uio_buf
[0];
703 int uio_buflen
= UIO_SIZEOF(4);
707 nmreq
= &nd
->nd_nmreq
;
708 nfsm_chain_null(&nmrep
);
711 len
= NFS_MAXPATHLEN
;
713 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
716 /* get mbuf list to hold symlink path */
717 error
= nfsm_mbuf_get_list(len
, &mpath
, &mpcnt
);
720 uio_buflen
= UIO_SIZEOF(mpcnt
);
721 MALLOC(uio_bufp
, char*, uio_buflen
, M_TEMP
, M_WAITOK
);
726 auio
= uio_createwithbuffer(mpcnt
, 0, UIO_SYSSPACE
, UIO_READ
, uio_bufp
, uio_buflen
);
731 for (mp
= mpath
; mp
; mp
= mbuf_next(mp
))
732 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), mbuf_len(mp
));
734 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
737 /* update export stats */
738 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
740 /* update active user stats */
741 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
743 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
746 if (vnode_vtype(vp
) != VLNK
) {
747 if (nd
->nd_vers
== NFS_VER3
)
754 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, ctx
, nxo
, 0);
756 error
= VNOP_READLINK(vp
, auio
, ctx
);
758 if (nd
->nd_vers
== NFS_VER3
) {
759 nfsm_srv_vattr_init(&vattr
, NFS_VER3
);
760 attrerr
= vnode_getattr(vp
, &vattr
, ctx
);
772 nd
->nd_repstat
= error
;
773 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_UNSIGNED
);
775 *mrepp
= nmrep
.nmc_mhead
;
776 nfsmout_on_status(nd
, error
);
777 if (nd
->nd_vers
== NFS_VER3
)
778 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &vattr
);
779 if (error
|| nd
->nd_repstat
) {
780 nfsm_chain_build_done(error
, &nmrep
);
783 if (auio
&& (uio_resid(auio
) > 0)) {
784 len
-= uio_resid(auio
);
785 tlen
= nfsm_rndup(len
);
786 nfsm_adj(mpath
, NFS_MAXPATHLEN
-tlen
, tlen
-len
);
788 nfsm_chain_add_32(error
, &nmrep
, len
);
789 nfsm_chain_build_done(error
, &nmrep
);
791 error
= mbuf_setnext(nmrep
.nmc_mcur
, mpath
);
799 if (uio_bufp
!= &uio_buf
[0])
800 FREE(uio_bufp
, M_TEMP
);
802 nfsm_chain_cleanup(&nmrep
);
813 struct nfsrv_descript
*nd
,
814 struct nfsrv_sock
*slp
,
818 int error
, attrerr
, mreadcnt
;
819 uint32_t reqlen
, maxlen
, count
, len
, tlen
, left
;
822 struct nfs_filehandle nfh
;
823 struct nfs_export
*nx
;
824 struct nfs_export_options
*nxo
;
826 char *uio_bufp
= NULL
;
827 struct vnode_attr vattr
, *vap
= &vattr
;
830 char uio_buf
[ UIO_SIZEOF(0) ];
831 struct nfsm_chain
*nmreq
, nmrep
;
835 nmreq
= &nd
->nd_nmreq
;
836 nfsm_chain_null(&nmrep
);
840 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
842 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
844 if (nd
->nd_vers
== NFS_VER3
)
845 nfsm_chain_get_64(error
, nmreq
, off
);
847 nfsm_chain_get_32(error
, nmreq
, off
);
848 nfsm_chain_get_32(error
, nmreq
, reqlen
);
849 maxlen
= NFSRV_NDMAXDATA(nd
);
853 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
856 /* update export stats */
857 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
859 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
862 if (vnode_vtype(vp
) != VREG
) {
863 if (nd
->nd_vers
== NFS_VER3
)
866 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
870 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, ctx
, nxo
, 1)))
871 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
, nxo
, 1);
873 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
874 attrerr
= vnode_getattr(vp
, vap
, ctx
);
879 if ((u_quad_t
)off
>= vap
->va_data_size
)
881 else if (((u_quad_t
)off
+ reqlen
) > vap
->va_data_size
)
882 count
= nfsm_rndup(vap
->va_data_size
- off
);
888 /* get mbuf list to hold read data */
889 error
= nfsm_mbuf_get_list(count
, &mread
, &mreadcnt
);
891 MALLOC(uio_bufp
, char *, UIO_SIZEOF(mreadcnt
), M_TEMP
, M_WAITOK
);
893 auio
= uio_createwithbuffer(mreadcnt
, off
, UIO_SYSSPACE
,
894 UIO_READ
, uio_bufp
, UIO_SIZEOF(mreadcnt
));
895 if (!uio_bufp
|| !auio
) {
899 for (m
= mread
; m
; m
= mbuf_next(m
))
900 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), mbuf_len(m
));
901 error
= VNOP_READ(vp
, auio
, IO_NODELOCKED
, ctx
);
903 auio
= uio_createwithbuffer(0, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
911 if (!error
|| (nd
->nd_vers
== NFS_VER3
)) {
912 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
913 attrerr
= vnode_getattr(vp
, vap
, ctx
);
914 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
915 error
= attrerr
; /* NFSv2 must have attributes to return */
922 /* trim off any data not actually read */
923 len
-= uio_resid(auio
);
924 tlen
= nfsm_rndup(len
);
925 if (count
!= tlen
|| tlen
!= len
)
926 nfsm_adj(mread
, count
- tlen
, tlen
- len
);
930 nd
->nd_repstat
= error
;
931 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPORFATTR(nd
->nd_vers
) + 3 * NFSX_UNSIGNED
);
933 *mrepp
= nmrep
.nmc_mhead
;
934 nfsmout_on_status(nd
, error
);
935 if (nd
->nd_vers
== NFS_VER3
)
936 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, vap
);
937 if (error
|| nd
->nd_repstat
) {
938 nfsm_chain_build_done(error
, &nmrep
);
941 if (nd
->nd_vers
== NFS_VER3
) {
942 nfsm_chain_add_32(error
, &nmrep
, len
);
943 nfsm_chain_add_32(error
, &nmrep
, (len
< reqlen
) ? TRUE
: FALSE
);
945 error
= nfsm_chain_add_fattr(nd
, &nmrep
, vap
);
947 nfsm_chain_add_32(error
, &nmrep
, len
);
948 nfsm_chain_build_done(error
, &nmrep
);
950 error
= mbuf_setnext(nmrep
.nmc_mcur
, mread
);
954 /* update export stats */
955 NFSStatAdd64(&nx
->nx_stats
.bytes_read
, len
);
957 /* update active user stats */
958 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, len
, 0);
964 if (uio_bufp
!= NULL
)
965 FREE(uio_bufp
, M_TEMP
);
967 nfsm_chain_cleanup(&nmrep
);
975 * NFS File modification reporting
977 * When the contents of a file are changed, a "content modified"
978 * fsevent needs to be issued. Normally this would be done at
979 * file close time. This is difficult for NFS because the protocol
980 * has no "close" operation. The client sends a stream of write
981 * requests that just stop. So we keep a hash table full of
982 * vnodes that have been written to recently, and issue a
983 * "content modified" fsevent only if there are no writes to
984 * a vnode for nfsrv_fmod_pendtime milliseconds.
986 int nfsrv_fmod_pending
; /* count of vnodes being written to */
987 int nfsrv_fmod_pendtime
= 1000; /* msec to wait */
988 int nfsrv_fmod_min_interval
= 100; /* msec min interval between callbacks */
991 * This function is called via the kernel's callout
992 * mechanism. Calls are made only when there are
993 * vnodes pending a fsevent creation, and no more
994 * frequently than every nfsrv_fmod_min_interval ms.
997 nfsrv_fmod_timer(__unused
void *param0
, __unused
void *param1
)
999 struct nfsrv_fmod_hashhead
*headp
, firehead
;
1000 struct nfsrv_fmod
*fp
, *nfp
, *pfp
;
1001 uint64_t timenow
, next_deadline
;
1002 int interval
= 0, i
, fmod_fire
;
1004 LIST_INIT(&firehead
);
1005 lck_mtx_lock(nfsrv_fmod_mutex
);
1007 clock_get_uptime(&timenow
);
1008 clock_interval_to_deadline(nfsrv_fmod_pendtime
, 1000 * 1000,
1012 * Scan all the hash chains
1015 for (i
= 0; i
< NFSRVFMODHASHSZ
; i
++) {
1017 * For each hash chain, look for an entry
1018 * that has exceeded the deadline.
1020 headp
= &nfsrv_fmod_hashtbl
[i
];
1021 LIST_FOREACH(fp
, headp
, fm_link
) {
1022 if (timenow
>= fp
->fm_deadline
)
1024 if (fp
->fm_deadline
< next_deadline
)
1025 next_deadline
= fp
->fm_deadline
;
1029 * If we have an entry that's exceeded the
1030 * deadline, then the same is true for all
1031 * following entries in the chain, since they're
1032 * sorted in time order.
1036 /* move each entry to the fire list */
1037 nfp
= LIST_NEXT(fp
, fm_link
);
1038 LIST_REMOVE(fp
, fm_link
);
1041 LIST_INSERT_AFTER(pfp
, fp
, fm_link
);
1043 LIST_INSERT_HEAD(&firehead
, fp
, fm_link
);
1050 lck_mtx_unlock(nfsrv_fmod_mutex
);
1052 * Fire off the content modified fsevent for each
1053 * entry and free it.
1055 LIST_FOREACH_SAFE(fp
, &firehead
, fm_link
, nfp
) {
1056 if (nfsrv_fsevents_enabled
) {
1057 fp
->fm_context
.vc_thread
= current_thread();
1058 add_fsevent(FSE_CONTENT_MODIFIED
, &fp
->fm_context
,
1059 FSE_ARG_VNODE
, fp
->fm_vp
,
1062 vnode_put(fp
->fm_vp
);
1063 kauth_cred_unref(&fp
->fm_context
.vc_ucred
);
1064 LIST_REMOVE(fp
, fm_link
);
1067 lck_mtx_lock(nfsrv_fmod_mutex
);
1068 nfsrv_fmod_pending
-= fmod_fire
;
1073 * If there are still pending entries, set up another
1074 * callout to handle them later. Set the timeout deadline
1075 * so that the callout happens when the oldest pending
1076 * entry is ready to send its fsevent.
1078 if (nfsrv_fmod_pending
> 0) {
1079 interval
= (next_deadline
- timenow
) / (1000 * 1000);
1080 if (interval
< nfsrv_fmod_min_interval
)
1081 interval
= nfsrv_fmod_min_interval
;
1084 nfsrv_fmod_timer_on
= interval
> 0;
1085 if (nfsrv_fmod_timer_on
)
1086 nfs_interval_timer_start(nfsrv_fmod_timer_call
, interval
);
1088 lck_mtx_unlock(nfsrv_fmod_mutex
);
1092 * When a vnode has been written to, enter it in the hash
1093 * table of vnodes pending creation of an fsevent. If the
1094 * callout timer isn't already running, schedule a callback
1095 * for nfsrv_fmod_pendtime msec from now.
1098 nfsrv_modified(vnode_t vp
, vfs_context_t ctx
)
1101 struct nfsrv_fmod
*fp
;
1102 struct nfsrv_fmod_hashhead
*head
;
1104 lck_mtx_lock(nfsrv_fmod_mutex
);
1107 * Compute the time in the future when the
1108 * content modified fsevent is to be issued.
1110 clock_interval_to_deadline(nfsrv_fmod_pendtime
, 1000 * 1000, &deadline
);
1113 * Check if there's already a file content change fsevent
1114 * pending for this vnode. If there is, update its
1115 * timestamp and make sure it's at the front of the hash chain.
1117 head
= &nfsrv_fmod_hashtbl
[NFSRVFMODHASH(vp
)];
1118 LIST_FOREACH(fp
, head
, fm_link
) {
1119 if (vp
== fp
->fm_vp
) {
1120 fp
->fm_deadline
= deadline
;
1121 if (fp
!= LIST_FIRST(head
)) {
1122 LIST_REMOVE(fp
, fm_link
);
1123 LIST_INSERT_HEAD(head
, fp
, fm_link
);
1125 lck_mtx_unlock(nfsrv_fmod_mutex
);
1131 * First content change fsevent for this vnode.
1132 * Allocate a new file mod entry and add it
1133 * on the front of the hash chain.
1135 if (vnode_get(vp
) != 0)
1137 MALLOC(fp
, struct nfsrv_fmod
*, sizeof(*fp
), M_TEMP
, M_WAITOK
);
1143 kauth_cred_ref(vfs_context_ucred(ctx
));
1144 fp
->fm_context
= *ctx
;
1145 fp
->fm_deadline
= deadline
;
1146 LIST_INSERT_HEAD(head
, fp
, fm_link
);
1149 * If added to an empty hash table, then set the
1150 * callout timer to go off after nfsrv_fmod_pendtime.
1152 nfsrv_fmod_pending
++;
1153 if (!nfsrv_fmod_timer_on
) {
1154 nfsrv_fmod_timer_on
= 1;
1155 nfs_interval_timer_start(nfsrv_fmod_timer_call
,
1156 nfsrv_fmod_pendtime
);
1159 lck_mtx_unlock(nfsrv_fmod_mutex
);
1162 #endif /* CONFIG_FSE */
1169 struct nfsrv_descript
*nd
,
1170 struct nfsrv_sock
*slp
,
1174 struct vnode_attr preattr
, postattr
;
1175 int error
, preattrerr
, postattrerr
;
1176 int ioflags
, len
, retlen
;
1178 int stable
= NFS_WRITE_FILESYNC
;
1181 struct nfs_filehandle nfh
;
1182 struct nfs_export
*nx
;
1183 struct nfs_export_options
*nxo
;
1185 char *uio_bufp
= NULL
;
1188 struct nfsm_chain
*nmreq
, nmrep
;
1190 if (nd
->nd_nmreq
.nmc_mhead
== NULL
) {
1196 preattrerr
= postattrerr
= ENOENT
;
1197 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1198 nmreq
= &nd
->nd_nmreq
;
1199 nfsm_chain_null(&nmrep
);
1203 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
1205 if (nd
->nd_vers
== NFS_VER3
) {
1206 nfsm_chain_get_64(error
, nmreq
, off
);
1207 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1208 nfsm_chain_get_32(error
, nmreq
, stable
);
1210 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1211 nfsm_chain_get_32(error
, nmreq
, off
);
1212 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1214 stable
= NFS_WRITE_UNSTABLE
;
1216 nfsm_chain_get_32(error
, nmreq
, len
);
1221 * For NFS Version 2, it is not obvious what a write of zero length
1222 * should do, but I might as well be consistent with Version 3,
1223 * which is to return ok so long as there are no permission problems.
1227 error
= nfsm_chain_trim_data(nmreq
, len
, &mlen
);
1232 if ((len
> NFSRV_MAXDATA
) || (len
< 0) || (mlen
< len
)) {
1236 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
1239 /* update export stats */
1240 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1242 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
1245 if (nd
->nd_vers
== NFS_VER3
) {
1246 nfsm_srv_pre_vattr_init(&preattr
);
1247 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
1249 if (vnode_vtype(vp
) != VREG
) {
1250 if (nd
->nd_vers
== NFS_VER3
)
1253 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1256 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
, nxo
, 1);
1260 for (mcount
=0, m
=nmreq
->nmc_mcur
; m
; m
= mbuf_next(m
))
1261 if (mbuf_len(m
) > 0)
1263 MALLOC(uio_bufp
, char *, UIO_SIZEOF(mcount
), M_TEMP
, M_WAITOK
);
1265 auio
= uio_createwithbuffer(mcount
, off
, UIO_SYSSPACE
, UIO_WRITE
, uio_bufp
, UIO_SIZEOF(mcount
));
1266 if (!uio_bufp
|| !auio
)
1269 for (m
= nmreq
->nmc_mcur
; m
; m
= mbuf_next(m
))
1270 if ((mlen
= mbuf_len(m
)) > 0)
1271 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), mlen
);
1273 * XXX The IO_METASYNC flag indicates that all metadata (and not just
1274 * enough to ensure data integrity) mus be written to stable storage
1275 * synchronously. (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1277 if (stable
== NFS_WRITE_UNSTABLE
)
1278 ioflags
= IO_NODELOCKED
;
1279 else if (stable
== NFS_WRITE_DATASYNC
)
1280 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1282 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1284 error
= VNOP_WRITE(vp
, auio
, ioflags
, ctx
);
1285 OSAddAtomic64(1, &nfsstats
.srvvop_writes
);
1287 /* update export stats */
1288 NFSStatAdd64(&nx
->nx_stats
.bytes_written
, len
);
1290 /* update active user stats */
1291 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, len
);
1294 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CONTENT_MODIFIED
, vp
))
1295 nfsrv_modified(vp
, ctx
);
1298 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
1299 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
1300 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
1301 error
= postattrerr
; /* NFSv2 must have attributes to return */
1306 /* assemble reply */
1307 nd
->nd_repstat
= error
;
1308 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_PREOPATTR(nd
->nd_vers
) +
1309 NFSX_POSTOPORFATTR(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
+
1310 NFSX_WRITEVERF(nd
->nd_vers
));
1312 *mrepp
= nmrep
.nmc_mhead
;
1313 nfsmout_on_status(nd
, error
);
1314 if (nd
->nd_vers
== NFS_VER3
) {
1315 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1316 preattrerr
, &preattr
, postattrerr
, &postattr
);
1317 nfsmout_if(error
|| nd
->nd_repstat
);
1318 nfsm_chain_add_32(error
, &nmrep
, retlen
);
1319 /* If nfsrv_async is set, then pretend the write was FILESYNC. */
1320 if ((stable
== NFS_WRITE_UNSTABLE
) && !nfsrv_async
)
1321 nfsm_chain_add_32(error
, &nmrep
, stable
);
1323 nfsm_chain_add_32(error
, &nmrep
, NFS_WRITE_FILESYNC
);
1324 /* write verifier */
1325 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
1326 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
1328 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
1331 nfsm_chain_build_done(error
, &nmrep
);
1334 if (uio_bufp
!= NULL
)
1335 FREE(uio_bufp
, M_TEMP
);
1337 nfsm_chain_cleanup(&nmrep
);
1344 * NFS write service with write gathering support. Called when
1345 * nfsrv_wg_delay > 0.
1346 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1347 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1351 #define NWDELAYHASH(sock, f) \
1352 (&(sock)->ns_wdelayhashtbl[(*((u_int32_t *)(f))) % NFS_WDELAYHASHSIZ])
1353 /* These macros compare nfsrv_descript structures. */
1354 #define NFSW_CONTIG(o, n) \
1355 (((o)->nd_eoff >= (n)->nd_off) && nfsrv_fhmatch(&(o)->nd_fh, &(n)->nd_fh))
1357 * XXX The following is an incorrect comparison; it fails to take into account
1358 * XXX scoping of MAC labels, but we currently lack KPI for credential
1361 #define NFSW_SAMECRED(o, n) \
1362 (!bcmp((caddr_t)(o)->nd_cr, (caddr_t)(n)->nd_cr, \
1363 sizeof (struct ucred)))
1367 struct nfsrv_descript
**ndp
,
1368 struct nfsrv_sock
*slp
,
1372 struct nfsrv_descript
*nd
, *wp
, *owp
, *swp
;
1373 struct nfs_export
*nx
;
1374 struct nfs_export_options
*nxo
;
1375 struct nfsrv_wg_delayhash
*wpp
;
1377 struct vnode_attr preattr
, postattr
;
1378 int error
, mlen
, i
, ioflags
, tlen
;
1379 int preattrerr
, postattrerr
;
1383 char *uio_bufp
= NULL
;
1386 struct nfsm_chain
*nmreq
, nmrep
;
1389 preattrerr
= postattrerr
= ENOENT
;
1390 nfsm_chain_null(&nmrep
);
1397 nmreq
= &nd
->nd_nmreq
;
1398 LIST_INIT(&nd
->nd_coalesce
);
1400 nd
->nd_stable
= NFS_WRITE_FILESYNC
;
1402 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1403 nd
->nd_time
= cur_usec
+
1404 ((nd
->nd_vers
== NFS_VER3
) ? nfsrv_wg_delay_v3
: nfsrv_wg_delay
);
1406 /* Now, get the write header... */
1407 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nd
->nd_fh
.nfh_fhp
, nd
->nd_fh
.nfh_len
);
1408 /* XXX shouldn't we be checking for invalid FHs before doing any more work? */
1410 if (nd
->nd_vers
== NFS_VER3
) {
1411 nfsm_chain_get_64(error
, nmreq
, nd
->nd_off
);
1412 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1413 nfsm_chain_get_32(error
, nmreq
, nd
->nd_stable
);
1415 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1416 nfsm_chain_get_32(error
, nmreq
, nd
->nd_off
);
1417 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1419 nd
->nd_stable
= NFS_WRITE_UNSTABLE
;
1421 nfsm_chain_get_32(error
, nmreq
, nd
->nd_len
);
1423 nd
->nd_eoff
= nd
->nd_off
+ nd
->nd_len
;
1425 if (nd
->nd_len
> 0) {
1426 error
= nfsm_chain_trim_data(nmreq
, nd
->nd_len
, &mlen
);
1432 if ((nd
->nd_len
> NFSRV_MAXDATA
) || (nd
->nd_len
< 0) || (mlen
< nd
->nd_len
)) {
1435 nd
->nd_repstat
= error
;
1436 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
1438 nd
->nd_mrep
= nmrep
.nmc_mhead
;
1439 if (nd
->nd_vers
== NFS_VER3
)
1440 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1441 preattrerr
, &preattr
, postattrerr
, &postattr
);
1443 nfsm_chain_build_done(error
, &nmrep
);
1448 * Add this entry to the hash and time queues.
1450 lck_mtx_lock(&slp
->ns_wgmutex
);
1452 wp
= slp
->ns_tq
.lh_first
;
1453 while (wp
&& wp
->nd_time
< nd
->nd_time
) {
1455 wp
= wp
->nd_tq
.le_next
;
1458 LIST_INSERT_AFTER(owp
, nd
, nd_tq
);
1460 LIST_INSERT_HEAD(&slp
->ns_tq
, nd
, nd_tq
);
1463 wpp
= NWDELAYHASH(slp
, nd
->nd_fh
.nfh_fid
);
1466 while (wp
&& !nfsrv_fhmatch(&nd
->nd_fh
, &wp
->nd_fh
)) {
1468 wp
= wp
->nd_hash
.le_next
;
1470 while (wp
&& (wp
->nd_off
< nd
->nd_off
) &&
1471 nfsrv_fhmatch(&nd
->nd_fh
, &wp
->nd_fh
)) {
1473 wp
= wp
->nd_hash
.le_next
;
1476 LIST_INSERT_AFTER(owp
, nd
, nd_hash
);
1478 * Search the hash list for overlapping entries and
1481 for(; nd
&& NFSW_CONTIG(owp
, nd
); nd
= wp
) {
1482 wp
= nd
->nd_hash
.le_next
;
1483 if (NFSW_SAMECRED(owp
, nd
))
1484 nfsrv_wg_coalesce(owp
, nd
);
1487 LIST_INSERT_HEAD(wpp
, nd
, nd_hash
);
1491 lck_mtx_lock(&slp
->ns_wgmutex
);
1495 * Now, do VNOP_WRITE()s for any one(s) that need to be done now
1496 * and generate the associated reply mbuf list(s).
1500 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1501 for (nd
= slp
->ns_tq
.lh_first
; nd
; nd
= owp
) {
1502 owp
= nd
->nd_tq
.le_next
;
1503 if (nd
->nd_time
> cur_usec
)
1507 LIST_REMOVE(nd
, nd_tq
);
1508 LIST_REMOVE(nd
, nd_hash
);
1509 nmreq
= &nd
->nd_nmreq
;
1510 preattrerr
= postattrerr
= ENOENT
;
1512 /* save the incoming uid before mapping, */
1513 /* for updating active user stats later */
1514 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1516 error
= nfsrv_fhtovp(&nd
->nd_fh
, nd
, &vp
, &nx
, &nxo
);
1518 /* update per-export stats */
1519 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1521 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
1526 if (nd
->nd_vers
== NFS_VER3
) {
1527 nfsm_srv_pre_vattr_init(&preattr
);
1528 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
1530 if (vnode_vtype(vp
) != VREG
) {
1531 if (nd
->nd_vers
== NFS_VER3
)
1534 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1539 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
, nxo
, 1);
1541 if (nd
->nd_stable
== NFS_WRITE_UNSTABLE
)
1542 ioflags
= IO_NODELOCKED
;
1543 else if (nd
->nd_stable
== NFS_WRITE_DATASYNC
)
1544 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1546 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1548 if (!error
&& ((nd
->nd_eoff
- nd
->nd_off
) > 0)) {
1549 for (i
=0, m
=nmreq
->nmc_mhead
; m
; m
= mbuf_next(m
))
1550 if (mbuf_len(m
) > 0)
1553 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
1555 auio
= uio_createwithbuffer(i
, nd
->nd_off
, UIO_SYSSPACE
,
1556 UIO_WRITE
, uio_bufp
, UIO_SIZEOF(i
));
1557 if (!uio_bufp
|| !auio
)
1560 for (m
= nmreq
->nmc_mhead
; m
; m
= mbuf_next(m
))
1561 if ((tlen
= mbuf_len(m
)) > 0)
1562 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), tlen
);
1563 error
= VNOP_WRITE(vp
, auio
, ioflags
, ctx
);
1564 OSAddAtomic64(1, &nfsstats
.srvvop_writes
);
1566 /* update export stats */
1567 NFSStatAdd64(&nx
->nx_stats
.bytes_written
, nd
->nd_len
);
1568 /* update active user stats */
1569 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, nd
->nd_len
);
1572 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CONTENT_MODIFIED
, vp
))
1573 nfsrv_modified(vp
, ctx
);
1577 FREE(uio_bufp
, M_TEMP
);
1582 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
1583 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
1588 * Loop around generating replies for all write rpcs that have
1589 * now been completed.
1594 nd
->nd_repstat
= error
;
1595 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
1596 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
1597 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1598 preattrerr
, &preattr
, postattrerr
, &postattr
);
1601 nd
->nd_repstat
= error
;
1602 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_PREOPATTR(nd
->nd_vers
) +
1603 NFSX_POSTOPORFATTR(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
+
1604 NFSX_WRITEVERF(nd
->nd_vers
));
1605 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
1606 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1607 preattrerr
, &preattr
, postattrerr
, &postattr
);
1608 nfsm_chain_add_32(error
, &nmrep
, nd
->nd_len
);
1609 nfsm_chain_add_32(error
, &nmrep
, nd
->nd_stable
);
1610 /* write verifier */
1611 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
1612 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
1613 } else if (!error
) {
1614 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
1617 nfsm_chain_build_done(error
, &nmrep
);
1619 nd
->nd_mrep
= nmrep
.nmc_mhead
;
1622 * Done. Put it at the head of the timer queue so that
1623 * the final phase can return the reply.
1627 LIST_INSERT_HEAD(&slp
->ns_tq
, nd
, nd_tq
);
1629 nd
= swp
->nd_coalesce
.lh_first
;
1631 LIST_REMOVE(nd
, nd_tq
);
1635 LIST_INSERT_HEAD(&slp
->ns_tq
, swp
, nd_tq
);
1640 * Search for a reply to return.
1642 for (nd
= slp
->ns_tq
.lh_first
; nd
; nd
= nd
->nd_tq
.le_next
)
1644 LIST_REMOVE(nd
, nd_tq
);
1645 *mrepp
= nd
->nd_mrep
;
1649 slp
->ns_wgtime
= slp
->ns_tq
.lh_first
? slp
->ns_tq
.lh_first
->nd_time
: 0;
1650 lck_mtx_unlock(&slp
->ns_wgmutex
);
1653 * If we've just created a write pending gather,
1654 * start the timer to check on it soon to make sure
1655 * the write will be completed.
1657 * Add/Remove the socket in the nfsrv_sockwg queue as needed.
1659 lck_mtx_lock(nfsd_mutex
);
1660 if (slp
->ns_wgtime
) {
1661 if (slp
->ns_wgq
.tqe_next
== SLPNOLIST
) {
1662 TAILQ_INSERT_HEAD(&nfsrv_sockwg
, slp
, ns_wgq
);
1664 if (!nfsrv_wg_timer_on
) {
1665 nfsrv_wg_timer_on
= 1;
1666 nfs_interval_timer_start(nfsrv_wg_timer_call
,
1667 NFSRV_WGATHERDELAY
);
1669 } else if (slp
->ns_wgq
.tqe_next
!= SLPNOLIST
) {
1670 TAILQ_REMOVE(&nfsrv_sockwg
, slp
, ns_wgq
);
1671 slp
->ns_wgq
.tqe_next
= SLPNOLIST
;
1673 lck_mtx_unlock(nfsd_mutex
);
1679 * Coalesce the write request nd into owp. To do this we must:
1680 * - remove nd from the queues
1681 * - merge nd->nd_nmreq into owp->nd_nmreq
1682 * - update the nd_eoff and nd_stable for owp
1683 * - put nd on owp's nd_coalesce list
1686 nfsrv_wg_coalesce(struct nfsrv_descript
*owp
, struct nfsrv_descript
*nd
)
1690 struct nfsrv_descript
*p
;
1692 LIST_REMOVE(nd
, nd_hash
);
1693 LIST_REMOVE(nd
, nd_tq
);
1694 if (owp
->nd_eoff
< nd
->nd_eoff
) {
1695 overlap
= owp
->nd_eoff
- nd
->nd_off
;
1699 mbuf_adj(nd
->nd_nmreq
.nmc_mhead
, overlap
);
1700 mp
= owp
->nd_nmreq
.nmc_mhead
;
1701 while ((mpnext
= mbuf_next(mp
)))
1703 error
= mbuf_setnext(mp
, nd
->nd_nmreq
.nmc_mhead
);
1706 owp
->nd_eoff
= nd
->nd_eoff
;
1708 mbuf_freem(nd
->nd_nmreq
.nmc_mhead
);
1710 nd
->nd_nmreq
.nmc_mhead
= NULL
;
1711 nd
->nd_nmreq
.nmc_mcur
= NULL
;
1712 if (nd
->nd_stable
== NFS_WRITE_FILESYNC
)
1713 owp
->nd_stable
= NFS_WRITE_FILESYNC
;
1714 else if ((nd
->nd_stable
== NFS_WRITE_DATASYNC
) &&
1715 (owp
->nd_stable
== NFS_WRITE_UNSTABLE
))
1716 owp
->nd_stable
= NFS_WRITE_DATASYNC
;
1717 LIST_INSERT_HEAD(&owp
->nd_coalesce
, nd
, nd_tq
);
1720 * If nd had anything else coalesced into it, transfer them
1721 * to owp, otherwise their replies will never get sent.
1723 while ((p
= nd
->nd_coalesce
.lh_first
)) {
1724 LIST_REMOVE(p
, nd_tq
);
1725 LIST_INSERT_HEAD(&owp
->nd_coalesce
, p
, nd_tq
);
1731 * Scan the write gathering queues for writes that need to be
1735 nfsrv_wg_timer(__unused
void *param0
, __unused
void *param1
)
1738 uint64_t cur_usec
, next_usec
;
1740 struct nfsrv_sock
*slp
;
1741 int writes_pending
= 0;
1744 cur_usec
= (uint64_t)now
.tv_sec
* 1000000 + (uint64_t)now
.tv_usec
;
1745 next_usec
= cur_usec
+ (NFSRV_WGATHERDELAY
* 1000);
1747 lck_mtx_lock(nfsd_mutex
);
1748 TAILQ_FOREACH(slp
, &nfsrv_sockwg
, ns_wgq
) {
1749 if (slp
->ns_wgtime
) {
1751 if (slp
->ns_wgtime
<= cur_usec
) {
1752 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1753 slp
->ns_flag
|= SLP_DOWRITES
;
1754 lck_rw_done(&slp
->ns_rwlock
);
1755 nfsrv_wakenfsd(slp
);
1758 if (slp
->ns_wgtime
< next_usec
)
1759 next_usec
= slp
->ns_wgtime
;
1763 if (writes_pending
== 0) {
1764 nfsrv_wg_timer_on
= 0;
1765 lck_mtx_unlock(nfsd_mutex
);
1768 lck_mtx_unlock(nfsd_mutex
);
1771 * Return the number of msec to wait again
1773 interval
= (next_usec
- cur_usec
) / 1000;
1776 nfs_interval_timer_start(nfsrv_wg_timer_call
, interval
);
1780 * Sort the group list in increasing numerical order.
1781 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1782 * that used to be here.)
1785 nfsrv_group_sort(gid_t
*list
, int num
)
1790 /* Insertion sort. */
1791 for (i
= 1; i
< num
; i
++) {
1793 /* find correct slot for value v, moving others up */
1794 for (j
= i
; --j
>= 0 && v
< list
[j
];)
1795 list
[j
+ 1] = list
[j
];
1801 * nfs create service
1802 * now does a truncate to 0 length via. setattr if it already exists
1806 struct nfsrv_descript
*nd
,
1807 struct nfsrv_sock
*slp
,
1811 struct vnode_attr dpreattr
, dpostattr
, postattr
;
1812 struct vnode_attr va
, *vap
= &va
;
1813 struct nameidata ni
;
1814 int error
, rdev
, dpreattrerr
, dpostattrerr
, postattrerr
;
1815 int how
, exclusive_flag
;
1816 uint32_t len
= 0, cnflags
;
1817 vnode_t vp
, dvp
, dirp
;
1818 struct nfs_filehandle nfh
;
1819 struct nfs_export
*nx
= NULL
;
1820 struct nfs_export_options
*nxo
;
1822 u_char cverf
[NFSX_V3CREATEVERF
];
1824 struct nfsm_chain
*nmreq
, nmrep
;
1827 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
1828 nmreq
= &nd
->nd_nmreq
;
1829 nfsm_chain_null(&nmrep
);
1830 vp
= dvp
= dirp
= NULL
;
1832 ni
.ni_cnd
.cn_nameiop
= 0;
1835 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1837 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
1838 nfsm_chain_get_32(error
, nmreq
, len
);
1839 nfsm_name_len_check(error
, nd
, len
);
1842 ni
.ni_cnd
.cn_nameiop
= CREATE
;
1846 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
1847 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
1849 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
1851 /* update export stats */
1852 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1854 /* update active user stats */
1855 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
1859 if (nd
->nd_vers
== NFS_VER3
) {
1860 nfsm_srv_pre_vattr_init(&dpreattr
);
1861 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
1869 ni
.ni_cnd
.cn_nameiop
= 0;
1877 if (nd
->nd_vers
== NFS_VER3
) {
1878 nfsm_chain_get_32(error
, nmreq
, how
);
1881 case NFS_CREATE_GUARDED
:
1886 case NFS_CREATE_UNCHECKED
:
1887 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
1889 case NFS_CREATE_EXCLUSIVE
:
1890 nfsm_chain_get_opaque(error
, nmreq
, NFSX_V3CREATEVERF
, cverf
);
1893 VATTR_SET(vap
, va_mode
, 0);
1896 VATTR_SET(vap
, va_type
, VREG
);
1900 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
1902 v_type
= vap
->va_type
;
1905 VATTR_SET(vap
, va_type
, v_type
);
1911 rdev
= vap
->va_data_size
;
1912 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1921 * If it doesn't exist, create it
1922 * otherwise just truncate to 0 length
1923 * should I set the mode too ??
1926 kauth_acl_t xacl
= NULL
;
1928 /* authorize before creating */
1929 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
1931 /* construct ACL and handle inheritance */
1933 error
= kauth_acl_inherit(dvp
,
1939 if (!error
&& xacl
!= NULL
)
1940 VATTR_SET(vap
, va_acl
, xacl
);
1942 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1943 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
1945 * Server policy is to alway use the mapped rpc credential for
1946 * file system object creation. This has the nice side effect of
1947 * enforcing BSD creation semantics
1949 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1950 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1952 /* validate new-file security information */
1954 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
1956 if (vap
->va_type
== VREG
|| vap
->va_type
== VSOCK
) {
1959 error
= VNOP_CREATE(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
1961 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
1963 * If some of the requested attributes weren't handled by the VNOP,
1964 * use our fallback code.
1966 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
1969 kauth_acl_free(xacl
);
1972 if (exclusive_flag
) {
1975 bcopy(cverf
, (caddr_t
)&vap
->va_access_time
,
1977 VATTR_SET_ACTIVE(vap
, va_access_time
);
1978 // skip authorization, as this is an
1979 // NFS internal implementation detail.
1980 error
= vnode_setattr(vp
, vap
, ctx
);
1984 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_CREATE_FILE
, vp
)) {
1985 add_fsevent(FSE_CREATE_FILE
, ctx
,
1992 } else if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
||
1993 vap
->va_type
== VFIFO
) {
1994 if (vap
->va_type
== VCHR
&& rdev
== (int)0xffffffff)
1995 VATTR_SET(vap
, va_type
, VFIFO
);
1996 if (vap
->va_type
!= VFIFO
) {
1997 error
= suser(nd
->nd_cr
, NULL
);
2000 VATTR_SET(vap
, va_rdev
, (dev_t
)rdev
);
2002 error
= VNOP_MKNOD(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
2005 kauth_acl_free(xacl
);
2014 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
2016 ni
.ni_op
= OP_LOOKUP
;
2018 ni
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
2019 ni
.ni_cnd
.cn_context
= ctx
;
2020 ni
.ni_startdir
= dvp
;
2022 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
2023 while ((error
= lookup(&ni
)) == ERECYCLE
) {
2024 ni
.ni_cnd
.cn_flags
= cnflags
;
2025 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
2026 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
2029 if (ni
.ni_cnd
.cn_flags
& ISSYMLINK
)
2038 * nameidone has to happen before we vnode_put(dvp)
2039 * since it may need to release the fs_nodelock on the dvp
2042 ni
.ni_cnd
.cn_nameiop
= 0;
2047 * nameidone has to happen before we vnode_put(dvp)
2048 * since it may need to release the fs_nodelock on the dvp
2051 ni
.ni_cnd
.cn_nameiop
= 0;
2055 if (!error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2056 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
,
2059 tempsize
= vap
->va_data_size
;
2061 VATTR_SET(vap
, va_data_size
, tempsize
);
2062 error
= vnode_setattr(vp
, vap
, ctx
);
2067 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, NULL
, vp
, ctx
, &nfh
);
2069 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
2070 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
2071 if (nd
->nd_vers
== NFS_VER2
)
2072 error
= postattrerr
;
2078 if (nd
->nd_vers
== NFS_VER3
) {
2079 if (exclusive_flag
&& !error
&&
2080 bcmp(cverf
, &postattr
.va_access_time
, NFSX_V3CREATEVERF
))
2082 nfsm_srv_vattr_init(&dpostattr
, NFS_VER3
);
2083 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2089 /* assemble reply */
2090 nd
->nd_repstat
= error
;
2091 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
2092 NFSX_FATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
2094 *mrepp
= nmrep
.nmc_mhead
;
2095 nfsmout_on_status(nd
, error
);
2096 if (nd
->nd_vers
== NFS_VER3
) {
2097 if (!nd
->nd_repstat
) {
2098 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2099 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
2101 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2102 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2104 nfsm_chain_add_fh(error
, &nmrep
, NFS_VER2
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2106 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
2109 nfsm_chain_build_done(error
, &nmrep
);
2110 if (ni
.ni_cnd
.cn_nameiop
) {
2112 * nameidone has to happen before we vnode_put(dvp)
2113 * since it may need to release the fs_nodelock on the dvp
2124 nfsm_chain_cleanup(&nmrep
);
2131 * nfs v3 mknod service
2135 struct nfsrv_descript
*nd
,
2136 struct nfsrv_sock
*slp
,
2140 struct vnode_attr dpreattr
, dpostattr
, postattr
;
2141 struct vnode_attr va
, *vap
= &va
;
2142 struct nameidata ni
;
2143 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
2144 uint32_t len
= 0, cnflags
;
2145 u_int32_t major
= 0, minor
= 0;
2148 vnode_t vp
, dvp
, dirp
;
2149 struct nfs_filehandle nfh
;
2150 struct nfs_export
*nx
= NULL
;
2151 struct nfs_export_options
*nxo
;
2153 kauth_acl_t xacl
= NULL
;
2154 struct nfsm_chain
*nmreq
, nmrep
;
2157 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
2158 nmreq
= &nd
->nd_nmreq
;
2159 nfsm_chain_null(&nmrep
);
2160 vp
= dvp
= dirp
= NULL
;
2161 ni
.ni_cnd
.cn_nameiop
= 0;
2163 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2165 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2166 nfsm_chain_get_32(error
, nmreq
, len
);
2167 nfsm_name_len_check(error
, nd
, len
);
2170 ni
.ni_cnd
.cn_nameiop
= CREATE
;
2174 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
2175 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
2177 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
2179 /* update export stats */
2180 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
2182 /* update active user stats */
2183 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
2187 nfsm_srv_pre_vattr_init(&dpreattr
);
2188 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
2191 ni
.ni_cnd
.cn_nameiop
= 0;
2198 nfsm_chain_get_32(error
, nmreq
, nvtype
);
2200 vtyp
= nfstov_type(nvtype
, NFS_VER3
);
2201 if (!error
&& (vtyp
!= VCHR
) && (vtyp
!= VBLK
) && (vtyp
!= VSOCK
) && (vtyp
!= VFIFO
)) {
2202 error
= NFSERR_BADTYPE
;
2207 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
2208 if ((vtyp
== VCHR
) || (vtyp
== VBLK
)) {
2209 nfsm_chain_get_32(error
, nmreq
, major
);
2210 nfsm_chain_get_32(error
, nmreq
, minor
);
2212 VATTR_SET(vap
, va_rdev
, makedev(major
, minor
));
2217 * If it doesn't exist, create it.
2223 VATTR_SET(vap
, va_type
, vtyp
);
2225 /* authorize before creating */
2226 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
2228 /* construct ACL and handle inheritance */
2230 error
= kauth_acl_inherit(dvp
,
2236 if (!error
&& xacl
!= NULL
)
2237 VATTR_SET(vap
, va_acl
, xacl
);
2239 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
2240 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
2242 * Server policy is to alway use the mapped rpc credential for
2243 * file system object creation. This has the nice side effect of
2244 * enforcing BSD creation semantics
2246 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
2247 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
2249 /* validate new-file security information */
2251 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
2256 if (vtyp
== VSOCK
) {
2257 error
= VNOP_CREATE(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
2259 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
2261 * If some of the requested attributes weren't handled by the VNOP,
2262 * use our fallback code.
2264 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
2266 if (vtyp
!= VFIFO
&& (error
= suser(nd
->nd_cr
, (u_short
*)0)))
2268 if ((error
= VNOP_MKNOD(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
)))
2275 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
2277 ni
.ni_op
= OP_LOOKUP
;
2279 ni
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
2280 ni
.ni_cnd
.cn_context
= vfs_context_current();
2281 ni
.ni_startdir
= dvp
;
2283 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
2284 while ((error
= lookup(&ni
)) == ERECYCLE
) {
2285 ni
.ni_cnd
.cn_flags
= cnflags
;
2286 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
2287 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
2291 if (ni
.ni_cnd
.cn_flags
& ISSYMLINK
)
2297 kauth_acl_free(xacl
);
2300 * nameidone has to happen before we vnode_put(dvp)
2301 * since it may need to release the fs_nodelock on the dvp
2304 ni
.ni_cnd
.cn_nameiop
= 0;
2310 error
= nfsrv_vptofh(nx
, NFS_VER3
, NULL
, vp
, ctx
, &nfh
);
2312 nfsm_srv_vattr_init(&postattr
, NFS_VER3
);
2313 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
2321 nfsm_srv_vattr_init(&dpostattr
, NFS_VER3
);
2322 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2327 /* assemble reply */
2328 nd
->nd_repstat
= error
;
2329 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(NFS_VER3
, &nfh
) +
2330 NFSX_POSTOPATTR(NFS_VER3
) + NFSX_WCCDATA(NFS_VER3
));
2332 *mrepp
= nmrep
.nmc_mhead
;
2333 nfsmout_on_status(nd
, error
);
2334 if (!nd
->nd_repstat
) {
2335 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2336 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
2338 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2339 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2341 nfsm_chain_build_done(error
, &nmrep
);
2342 if (ni
.ni_cnd
.cn_nameiop
) {
2344 * nameidone has to happen before we vnode_put(dvp)
2345 * since it may need to release the fs_nodelock on the dvp
2360 nfsm_chain_cleanup(&nmrep
);
2367 * nfs remove service
2371 struct nfsrv_descript
*nd
,
2372 struct nfsrv_sock
*slp
,
2376 struct nameidata ni
;
2377 int error
, dpreattrerr
, dpostattrerr
;
2380 vnode_t vp
, dvp
, dirp
= NULL
;
2381 struct vnode_attr dpreattr
, dpostattr
;
2382 struct nfs_filehandle nfh
;
2383 struct nfs_export
*nx
= NULL
;
2384 struct nfs_export_options
*nxo
;
2385 struct nfsm_chain
*nmreq
, nmrep
;
2388 dpreattrerr
= dpostattrerr
= ENOENT
;
2389 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2390 dvp
= vp
= dirp
= NULL
;
2391 nmreq
= &nd
->nd_nmreq
;
2392 nfsm_chain_null(&nmrep
);
2394 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2395 nfsm_chain_get_32(error
, nmreq
, len
);
2396 nfsm_name_len_check(error
, nd
, len
);
2399 ni
.ni_cnd
.cn_nameiop
= DELETE
;
2401 ni
.ni_op
= OP_UNLINK
;
2403 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
2404 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
2406 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
2408 /* update export stats */
2409 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
2411 /* update active user stats */
2412 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
2416 if (nd
->nd_vers
== NFS_VER3
) {
2417 nfsm_srv_pre_vattr_init(&dpreattr
);
2418 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
2429 if (vnode_vtype(vp
) == VDIR
)
2430 error
= EPERM
; /* POSIX */
2431 else if (vnode_isvroot(vp
))
2433 * The root of a mounted filesystem cannot be deleted.
2437 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
, nxo
, 0);
2445 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_DELETE
, dvp
)) {
2447 if ((path
= get_pathbuff()) && !vn_getpath(vp
, path
, &plen
)) {
2448 get_fse_info(vp
, &finfo
, ctx
);
2450 release_pathbuff(path
);
2455 error
= VNOP_REMOVE(dvp
, vp
, &ni
.ni_cnd
, 0, ctx
);
2460 add_fsevent(FSE_DELETE
, ctx
,
2461 FSE_ARG_STRING
, plen
, path
,
2462 FSE_ARG_FINFO
, &finfo
,
2464 release_pathbuff(path
);
2470 * nameidone has to happen before we vnode_put(dvp)
2471 * since it may need to release the fs_nodelock on the dvp
2481 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
2482 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2486 /* assemble reply */
2487 nd
->nd_repstat
= error
;
2488 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
2490 *mrepp
= nmrep
.nmc_mhead
;
2491 nfsmout_on_status(nd
, error
);
2492 if (nd
->nd_vers
== NFS_VER3
)
2493 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2494 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2496 nfsm_chain_build_done(error
, &nmrep
);
2498 nfsm_chain_cleanup(&nmrep
);
2505 * nfs rename service
2509 struct nfsrv_descript
*nd
,
2510 struct nfsrv_sock
*slp
,
2514 kauth_cred_t saved_cred
= NULL
;
2517 uint32_t fromlen
, tolen
;
2518 int fdpreattrerr
, fdpostattrerr
;
2519 int tdpreattrerr
, tdpostattrerr
;
2520 char *frompath
= NULL
, *topath
= NULL
;
2521 struct nameidata fromni
, toni
;
2522 vnode_t fvp
, tvp
, tdvp
, fdvp
, fdirp
, tdirp
;
2523 struct vnode_attr fdpreattr
, fdpostattr
;
2524 struct vnode_attr tdpreattr
, tdpostattr
;
2525 struct nfs_filehandle fnfh
, tnfh
;
2526 struct nfs_export
*fnx
, *tnx
;
2527 struct nfs_export_options
*fnxo
, *tnxo
;
2528 enum vtype fvtype
, tvtype
;
2529 int holding_mntlock
;
2531 struct nfsm_chain
*nmreq
, nmrep
;
2532 char *from_name
, *to_name
;
2534 int from_len
=0, to_len
=0;
2535 fse_info from_finfo
, to_finfo
;
2537 u_char didstats
= 0;
2541 fdpreattrerr
= fdpostattrerr
= ENOENT
;
2542 tdpreattrerr
= tdpostattrerr
= ENOENT
;
2543 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2544 fromlen
= tolen
= 0;
2545 frompath
= topath
= NULL
;
2546 fdirp
= tdirp
= NULL
;
2547 nmreq
= &nd
->nd_nmreq
;
2548 nfsm_chain_null(&nmrep
);
2551 * these need to be set before calling any code
2552 * that they may take us out through the error path.
2554 holding_mntlock
= 0;
2559 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, fnfh
.nfh_fhp
, fnfh
.nfh_len
);
2560 nfsm_chain_get_32(error
, nmreq
, fromlen
);
2561 nfsm_name_len_check(error
, nd
, fromlen
);
2563 error
= nfsm_chain_get_path_namei(nmreq
, fromlen
, &fromni
);
2565 frompath
= fromni
.ni_cnd
.cn_pnbuf
;
2567 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, tnfh
.nfh_fhp
, tnfh
.nfh_len
);
2568 nfsm_chain_get_32(error
, nmreq
, tolen
);
2569 nfsm_name_len_check(error
, nd
, tolen
);
2571 error
= nfsm_chain_get_path_namei(nmreq
, tolen
, &toni
);
2573 topath
= toni
.ni_cnd
.cn_pnbuf
;
2576 * Remember our original uid so that we can reset cr_uid before
2577 * the second nfsrv_namei() call, in case it is remapped.
2579 saved_cred
= nd
->nd_cr
;
2580 kauth_cred_ref(saved_cred
);
2582 fromni
.ni_cnd
.cn_nameiop
= DELETE
;
2584 fromni
.ni_op
= OP_UNLINK
;
2586 fromni
.ni_cnd
.cn_flags
= WANTPARENT
;
2588 fromni
.ni_cnd
.cn_pnbuf
= frompath
;
2590 fromni
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2591 fromni
.ni_cnd
.cn_flags
|= HASBUF
;
2593 error
= nfsrv_namei(nd
, ctx
, &fromni
, &fnfh
, &fdirp
, &fnx
, &fnxo
);
2596 fdvp
= fromni
.ni_dvp
;
2600 if (nd
->nd_vers
== NFS_VER3
) {
2601 nfsm_srv_pre_vattr_init(&fdpreattr
);
2602 fdpreattrerr
= vnode_getattr(fdirp
, &fdpreattr
, ctx
);
2608 fvtype
= vnode_vtype(fvp
);
2610 /* reset credential if it was remapped */
2611 if (nd
->nd_cr
!= saved_cred
) {
2612 kauth_cred_ref(saved_cred
);
2613 kauth_cred_unref(&nd
->nd_cr
);
2614 ctx
->vc_ucred
= nd
->nd_cr
= saved_cred
;
2617 toni
.ni_cnd
.cn_nameiop
= RENAME
;
2619 toni
.ni_op
= OP_RENAME
;
2621 toni
.ni_cnd
.cn_flags
= WANTPARENT
;
2623 toni
.ni_cnd
.cn_pnbuf
= topath
;
2625 toni
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2626 toni
.ni_cnd
.cn_flags
|= HASBUF
;
2629 toni
.ni_cnd
.cn_flags
|= WILLBEDIR
;
2632 error
= nfsrv_namei(nd
, ctx
, &toni
, &tnfh
, &tdirp
, &tnx
, &tnxo
);
2635 * Translate error code for rename("dir1", "dir2/.").
2637 if (error
== EISDIR
&& fvtype
== VDIR
) {
2638 if (nd
->nd_vers
== NFS_VER3
)
2649 /* update export stats once only */
2651 /* update export stats */
2652 NFSStatAdd64(&tnx
->nx_stats
.ops
, 1);
2654 /* update active user stats */
2655 nfsrv_update_user_stat(tnx
, nd
, saved_uid
, 1, 0, 0);
2661 if (nd
->nd_vers
== NFS_VER3
) {
2662 nfsm_srv_pre_vattr_init(&tdpreattr
);
2663 tdpreattrerr
= vnode_getattr(tdirp
, &tdpreattr
, ctx
);
2671 tvtype
= vnode_vtype(tvp
);
2673 if (fvtype
== VDIR
&& tvtype
!= VDIR
) {
2674 if (nd
->nd_vers
== NFS_VER3
)
2679 } else if (fvtype
!= VDIR
&& tvtype
== VDIR
) {
2680 if (nd
->nd_vers
== NFS_VER3
)
2686 if (tvtype
== VDIR
&& vnode_mountedhere(tvp
)) {
2687 if (nd
->nd_vers
== NFS_VER3
)
2695 if (nd
->nd_vers
== NFS_VER3
)
2705 * If tvp is a directory and not the same as fdvp, or tdvp is not the same as fdvp,
2706 * the node is moving between directories and we need rights to remove from the
2707 * old and add to the new.
2709 * If tvp already exists and is not a directory, we need to be allowed to delete it.
2711 * Note that we do not inherit when renaming. XXX this needs to be revisited to
2712 * implement the deferred-inherit bit.
2718 if ((tvp
!= NULL
) && vnode_isdir(tvp
)) {
2721 } else if (tdvp
!= fdvp
) {
2725 /* moving out of fdvp, must have delete rights */
2726 if ((error
= nfsrv_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
, ctx
, fnxo
, 0)) != 0)
2728 /* moving into tdvp or tvp, must have rights to add */
2729 if ((error
= nfsrv_authorize(((tvp
!= NULL
) && vnode_isdir(tvp
)) ? tvp
: tdvp
,
2731 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2732 ctx
, tnxo
, 0)) != 0)
2735 /* node staying in same directory, must be allowed to add new name */
2736 if ((error
= nfsrv_authorize(fdvp
, NULL
,
2737 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2738 ctx
, fnxo
, 0)) != 0)
2741 /* overwriting tvp */
2742 if ((tvp
!= NULL
) && !vnode_isdir(tvp
) &&
2743 ((error
= nfsrv_authorize(tvp
, tdvp
, KAUTH_VNODE_DELETE
, ctx
, tnxo
, 0)) != 0))
2746 /* XXX more checks? */
2749 /* authorization denied */
2754 if ((vnode_mount(fvp
) != vnode_mount(tdvp
)) ||
2755 (tvp
&& (vnode_mount(fvp
) != vnode_mount(tvp
)))) {
2756 if (nd
->nd_vers
== NFS_VER3
)
2763 * The following edge case is caught here:
2764 * (to cannot be a descendent of from)
2777 if (tdvp
->v_parent
== fvp
) {
2778 if (nd
->nd_vers
== NFS_VER3
)
2784 if (fvtype
== VDIR
&& vnode_mountedhere(fvp
)) {
2785 if (nd
->nd_vers
== NFS_VER3
)
2792 * If source is the same as the destination (that is the
2793 * same vnode) then there is nothing to do...
2794 * EXCEPT if the underlying file system supports case
2795 * insensitivity and is case preserving. In this case
2796 * the file system needs to handle the special case of
2797 * getting the same vnode as target (fvp) and source (tvp).
2799 * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE
2800 * and _PC_CASE_PRESERVING can have this exception, and they need to
2801 * handle the special case of getting the same vnode as target and
2802 * source. NOTE: Then the target is unlocked going into vnop_rename,
2803 * so not to cause locking problems. There is a single reference on tvp.
2805 * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE
2806 * that correct behaviour then is just to remove the source (link)
2808 if ((fvp
== tvp
) && (fdvp
== tdvp
)) {
2809 if (fromni
.ni_cnd
.cn_namelen
== toni
.ni_cnd
.cn_namelen
&&
2810 !bcmp(fromni
.ni_cnd
.cn_nameptr
, toni
.ni_cnd
.cn_nameptr
,
2811 fromni
.ni_cnd
.cn_namelen
)) {
2816 if (holding_mntlock
&& vnode_mount(fvp
) != locked_mp
) {
2818 * we're holding a reference and lock
2819 * on locked_mp, but it no longer matches
2820 * what we want to do... so drop our hold
2822 mount_unlock_renames(locked_mp
);
2823 mount_drop(locked_mp
, 0);
2824 holding_mntlock
= 0;
2826 if (tdvp
!= fdvp
&& fvtype
== VDIR
) {
2828 * serialize renames that re-shape
2829 * the tree... if holding_mntlock is
2830 * set, then we're ready to go...
2832 * first need to drop the iocounts
2833 * we picked up, second take the
2834 * lock to serialize the access,
2835 * then finally start the lookup
2836 * process over with the lock held
2838 if (!holding_mntlock
) {
2840 * need to grab a reference on
2841 * the mount point before we
2842 * drop all the iocounts... once
2843 * the iocounts are gone, the mount
2846 locked_mp
= vnode_mount(fvp
);
2847 mount_ref(locked_mp
, 0);
2849 /* make a copy of to path to pass to nfsrv_namei() again */
2850 MALLOC_ZONE(topath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2852 bcopy(toni
.ni_cnd
.cn_pnbuf
, topath
, tolen
+ 1);
2855 * nameidone has to happen before we vnode_put(tdvp)
2856 * since it may need to release the fs_nodelock on the tdvp
2864 /* make a copy of from path to pass to nfsrv_namei() again */
2865 MALLOC_ZONE(frompath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2867 bcopy(fromni
.ni_cnd
.cn_pnbuf
, frompath
, fromlen
+ 1);
2870 * nameidone has to happen before we vnode_put(fdvp)
2871 * since it may need to release the fs_nodelock on the fdvp
2886 mount_lock_renames(locked_mp
);
2887 holding_mntlock
= 1;
2892 fdpreattrerr
= tdpreattrerr
= ENOENT
;
2894 if (!topath
|| !frompath
) {
2895 /* we couldn't allocate a path, so bail */
2900 /* reset credential if it was remapped */
2901 if (nd
->nd_cr
!= saved_cred
) {
2902 kauth_cred_ref(saved_cred
);
2903 kauth_cred_unref(&nd
->nd_cr
);
2904 ctx
->vc_ucred
= nd
->nd_cr
= saved_cred
;
2911 * when we dropped the iocounts to take
2912 * the lock, we allowed the identity of
2913 * the various vnodes to change... if they did,
2914 * we may no longer be dealing with a rename
2915 * that reshapes the tree... once we're holding
2916 * the iocounts, the vnodes can't change type
2917 * so we're free to drop the lock at this point
2920 if (holding_mntlock
) {
2921 mount_unlock_renames(locked_mp
);
2922 mount_drop(locked_mp
, 0);
2923 holding_mntlock
= 0;
2927 // save these off so we can later verify that fvp is the same
2929 oname
= fvp
->v_name
;
2930 oparent
= fvp
->v_parent
;
2933 * If generating an fsevent, then
2934 * stash any pre-rename info we may need.
2937 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_RENAME
, fvp
)) {
2938 int from_truncated
= 0, to_truncated
= 0;
2940 get_fse_info(fvp
, &from_finfo
, ctx
);
2942 get_fse_info(tvp
, &to_finfo
, ctx
);
2944 from_name
= get_pathbuff();
2946 from_len
= safe_getpath(fdvp
, fromni
.ni_cnd
.cn_nameptr
, from_name
, MAXPATHLEN
, &from_truncated
);
2949 to_name
= from_name
? get_pathbuff() : NULL
;
2951 to_len
= safe_getpath(tdvp
, toni
.ni_cnd
.cn_nameptr
, to_name
, MAXPATHLEN
, &to_truncated
);
2954 if (from_truncated
|| to_truncated
) {
2955 from_finfo
.mode
|= FSE_TRUNCATED_PATH
;
2962 #else /* CONFIG_FSE */
2965 #endif /* CONFIG_FSE */
2967 error
= VNOP_RENAME(fromni
.ni_dvp
, fromni
.ni_vp
, &fromni
.ni_cnd
,
2968 toni
.ni_dvp
, toni
.ni_vp
, &toni
.ni_cnd
, ctx
);
2970 * fix up name & parent pointers. note that we first
2971 * check that fvp has the same name/parent pointers it
2972 * had before the rename call... this is a 'weak' check
2975 if (oname
== fvp
->v_name
&& oparent
== fvp
->v_parent
) {
2977 update_flags
= VNODE_UPDATE_NAME
;
2979 update_flags
|= VNODE_UPDATE_PARENT
;
2980 vnode_update_identity(fvp
, tdvp
, toni
.ni_cnd
.cn_nameptr
,
2981 toni
.ni_cnd
.cn_namelen
, toni
.ni_cnd
.cn_hash
, update_flags
);
2985 * If the rename is OK and we've got the paths
2986 * then add an fsevent.
2989 if (nfsrv_fsevents_enabled
&& !error
&& from_name
&& to_name
) {
2991 add_fsevent(FSE_RENAME
, ctx
,
2992 FSE_ARG_STRING
, from_len
, from_name
,
2993 FSE_ARG_FINFO
, &from_finfo
,
2994 FSE_ARG_STRING
, to_len
, to_name
,
2995 FSE_ARG_FINFO
, &to_finfo
,
2998 add_fsevent(FSE_RENAME
, ctx
,
2999 FSE_ARG_STRING
, from_len
, from_name
,
3000 FSE_ARG_FINFO
, &from_finfo
,
3001 FSE_ARG_STRING
, to_len
, to_name
,
3006 release_pathbuff(from_name
);
3008 release_pathbuff(to_name
);
3009 #endif /* CONFIG_FSE */
3010 from_name
= to_name
= NULL
;
3013 if (holding_mntlock
) {
3014 mount_unlock_renames(locked_mp
);
3015 mount_drop(locked_mp
, 0);
3016 holding_mntlock
= 0;
3020 * nameidone has to happen before we vnode_put(tdvp)
3021 * since it may need to release the fs_nodelock on the tdvp
3032 * nameidone has to happen before we vnode_put(fdvp)
3033 * since it may need to release the fs_nodelock on the fdvp
3044 nfsm_srv_vattr_init(&fdpostattr
, nd
->nd_vers
);
3045 fdpostattrerr
= vnode_getattr(fdirp
, &fdpostattr
, ctx
);
3050 nfsm_srv_vattr_init(&tdpostattr
, nd
->nd_vers
);
3051 tdpostattrerr
= vnode_getattr(tdirp
, &tdpostattr
, ctx
);
3057 /* assemble reply */
3058 nd
->nd_repstat
= error
;
3059 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 2 * NFSX_WCCDATA(nd
->nd_vers
));
3061 *mrepp
= nmrep
.nmc_mhead
;
3062 nfsmout_on_status(nd
, error
);
3063 if (nd
->nd_vers
== NFS_VER3
) {
3064 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3065 fdpreattrerr
, &fdpreattr
, fdpostattrerr
, &fdpostattr
);
3066 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3067 tdpreattrerr
, &tdpreattr
, tdpostattrerr
, &tdpostattr
);
3070 nfsm_chain_build_done(error
, &nmrep
);
3071 if (holding_mntlock
) {
3072 mount_unlock_renames(locked_mp
);
3073 mount_drop(locked_mp
, 0);
3077 * nameidone has to happen before we vnode_put(tdvp)
3078 * since it may need to release the fs_nodelock on the tdvp
3088 * nameidone has to happen before we vnode_put(fdvp)
3089 * since it may need to release the fs_nodelock on the fdvp
3102 FREE_ZONE(frompath
, MAXPATHLEN
, M_NAMEI
);
3104 FREE_ZONE(topath
, MAXPATHLEN
, M_NAMEI
);
3106 kauth_cred_unref(&saved_cred
);
3108 nfsm_chain_cleanup(&nmrep
);
3119 struct nfsrv_descript
*nd
,
3120 struct nfsrv_sock
*slp
,
3124 struct nameidata ni
;
3125 int error
, dpreattrerr
, dpostattrerr
, attrerr
;
3127 vnode_t vp
, xp
, dvp
, dirp
;
3128 struct vnode_attr dpreattr
, dpostattr
, attr
;
3129 struct nfs_filehandle nfh
, dnfh
;
3130 struct nfs_export
*nx
;
3131 struct nfs_export_options
*nxo
;
3132 struct nfsm_chain
*nmreq
, nmrep
;
3135 dpreattrerr
= dpostattrerr
= attrerr
= ENOENT
;
3136 vp
= xp
= dvp
= dirp
= NULL
;
3137 nmreq
= &nd
->nd_nmreq
;
3138 nfsm_chain_null(&nmrep
);
3140 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3141 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
3142 nfsm_chain_get_32(error
, nmreq
, len
);
3143 nfsm_name_len_check(error
, nd
, len
);
3145 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
3148 /* update export stats */
3149 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3151 /* update active user stats */
3152 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
3154 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
3157 /* we're not allowed to link to directories... */
3158 if (vnode_vtype(vp
) == VDIR
) {
3159 error
= EPERM
; /* POSIX */
3163 /* ...or to anything that kauth doesn't want us to (eg. immutable items) */
3164 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LINKTARGET
, ctx
, nxo
, 0)) != 0)
3167 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3171 ni
.ni_cnd
.cn_flags
= LOCKPARENT
;
3172 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3174 error
= nfsrv_namei(nd
, ctx
, &ni
, &dnfh
, &dirp
, &nx
, &nxo
);
3176 if (nd
->nd_vers
== NFS_VER3
) {
3177 nfsm_srv_pre_vattr_init(&dpreattr
);
3178 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3191 else if (vnode_mount(vp
) != vnode_mount(dvp
))
3194 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
3197 error
= VNOP_LINK(vp
, dvp
, &ni
.ni_cnd
, ctx
);
3200 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CREATE_FILE
, dvp
)) {
3201 char *target_path
= NULL
;
3202 int plen
, truncated
=0;
3205 /* build the path to the new link file */
3206 target_path
= get_pathbuff();
3208 plen
= safe_getpath(dvp
, ni
.ni_cnd
.cn_nameptr
, target_path
, MAXPATHLEN
, &truncated
);
3210 if (get_fse_info(vp
, &finfo
, ctx
) == 0) {
3212 finfo
.mode
|= FSE_TRUNCATED_PATH
;
3214 add_fsevent(FSE_CREATE_FILE
, ctx
,
3215 FSE_ARG_STRING
, plen
, target_path
,
3216 FSE_ARG_FINFO
, &finfo
,
3220 release_pathbuff(target_path
);
3226 * nameidone has to happen before we vnode_put(dvp)
3227 * since it may need to release the fs_nodelock on the dvp
3235 if (nd
->nd_vers
== NFS_VER3
) {
3236 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
3237 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
3240 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3241 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3249 /* assemble reply */
3250 nd
->nd_repstat
= error
;
3251 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3253 *mrepp
= nmrep
.nmc_mhead
;
3254 nfsmout_on_status(nd
, error
);
3255 if (nd
->nd_vers
== NFS_VER3
) {
3256 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
3257 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3258 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3261 nfsm_chain_build_done(error
, &nmrep
);
3265 nfsm_chain_cleanup(&nmrep
);
3272 * nfs symbolic link service
3276 struct nfsrv_descript
*nd
,
3277 struct nfsrv_sock
*slp
,
3281 struct vnode_attr dpreattr
, dpostattr
, postattr
;
3282 struct vnode_attr va
, *vap
= &va
;
3283 struct nameidata ni
;
3284 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
3285 uint32_t len
= 0, linkdatalen
, cnflags
;
3288 vnode_t vp
, dvp
, dirp
;
3289 struct nfs_filehandle nfh
;
3290 struct nfs_export
*nx
= NULL
;
3291 struct nfs_export_options
*nxo
;
3293 char uio_buf
[ UIO_SIZEOF(1) ];
3294 struct nfsm_chain
*nmreq
, nmrep
;
3297 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
3298 nmreq
= &nd
->nd_nmreq
;
3299 nfsm_chain_null(&nmrep
);
3303 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3305 ni
.ni_cnd
.cn_nameiop
= 0;
3308 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3309 nfsm_chain_get_32(error
, nmreq
, len
);
3310 nfsm_name_len_check(error
, nd
, len
);
3313 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3317 ni
.ni_cnd
.cn_flags
= LOCKPARENT
;
3318 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3320 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3322 /* update export stats */
3323 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3325 /* update active user stats */
3326 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3330 if (nd
->nd_vers
== NFS_VER3
) {
3331 nfsm_srv_pre_vattr_init(&dpreattr
);
3332 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3339 ni
.ni_cnd
.cn_nameiop
= 0;
3346 if (nd
->nd_vers
== NFS_VER3
)
3347 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3348 nfsm_chain_get_32(error
, nmreq
, linkdatalen
);
3349 if (!error
&& (((nd
->nd_vers
== NFS_VER2
) && (linkdatalen
> NFS_MAXPATHLEN
)) ||
3350 ((nd
->nd_vers
== NFS_VER3
) && (linkdatalen
> MAXPATHLEN
))))
3351 error
= NFSERR_NAMETOL
;
3353 MALLOC(linkdata
, caddr_t
, linkdatalen
+ 1, M_TEMP
, M_WAITOK
);
3355 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3356 &uio_buf
[0], sizeof(uio_buf
));
3357 if (!linkdata
|| !auio
) {
3361 uio_addiov(auio
, CAST_USER_ADDR_T(linkdata
), linkdatalen
);
3362 error
= nfsm_chain_get_uio(nmreq
, linkdatalen
, auio
);
3363 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
3364 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3366 *(linkdata
+ linkdatalen
) = '\0';
3372 VATTR_SET(vap
, va_type
, VLNK
);
3373 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3374 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3376 * Server policy is to alway use the mapped rpc credential for
3377 * file system object creation. This has the nice side effect of
3378 * enforcing BSD creation semantics
3380 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3381 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3383 /* authorize before creating */
3384 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
3386 /* validate given attributes */
3388 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
3391 error
= VNOP_SYMLINK(dvp
, &vp
, &ni
.ni_cnd
, vap
, linkdata
, ctx
);
3393 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
3395 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
3397 ni
.ni_op
= OP_LOOKUP
;
3399 ni
.ni_cnd
.cn_flags
&= ~(LOCKPARENT
| FOLLOW
);
3400 ni
.ni_cnd
.cn_flags
|= (NOFOLLOW
| LOCKLEAF
);
3401 ni
.ni_cnd
.cn_context
= ctx
;
3402 ni
.ni_startdir
= dvp
;
3404 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
3405 while ((error
= lookup(&ni
)) == ERECYCLE
) {
3406 ni
.ni_cnd
.cn_flags
= cnflags
;
3407 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
3408 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
3414 error
= nfsrv_vptofh(nx
, NFS_VER3
, NULL
, vp
, ctx
, &nfh
);
3416 nfsm_srv_vattr_init(&postattr
, NFS_VER3
);
3417 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
3423 if (nfsrv_fsevents_enabled
&& !error
&& vp
) {
3424 add_fsevent(FSE_CREATE_FILE
, ctx
,
3431 * nameidone has to happen before we vnode_put(dvp)
3432 * since it may need to release the fs_nodelock on the dvp
3435 ni
.ni_cnd
.cn_nameiop
= 0;
3441 FREE(linkdata
, M_TEMP
);
3445 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3446 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3452 /* assemble reply */
3453 nd
->nd_repstat
= error
;
3454 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
3455 NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3457 *mrepp
= nmrep
.nmc_mhead
;
3458 nfsmout_on_status(nd
, error
);
3459 if (nd
->nd_vers
== NFS_VER3
) {
3460 if (!nd
->nd_repstat
) {
3461 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3462 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
3464 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3465 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3468 nfsm_chain_build_done(error
, &nmrep
);
3469 if (ni
.ni_cnd
.cn_nameiop
) {
3471 * nameidone has to happen before we vnode_put(dvp)
3472 * since it may need to release the fs_nodelock on the dvp
3483 FREE(linkdata
, M_TEMP
);
3485 nfsm_chain_cleanup(&nmrep
);
3497 struct nfsrv_descript
*nd
,
3498 struct nfsrv_sock
*slp
,
3502 struct vnode_attr dpreattr
, dpostattr
, postattr
;
3503 struct vnode_attr va
, *vap
= &va
;
3504 struct nameidata ni
;
3505 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
3507 vnode_t vp
, dvp
, dirp
;
3508 struct nfs_filehandle nfh
;
3509 struct nfs_export
*nx
= NULL
;
3510 struct nfs_export_options
*nxo
;
3512 kauth_acl_t xacl
= NULL
;
3513 struct nfsm_chain
*nmreq
, nmrep
;
3516 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
3517 nmreq
= &nd
->nd_nmreq
;
3518 nfsm_chain_null(&nmrep
);
3520 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3522 ni
.ni_cnd
.cn_nameiop
= 0;
3523 vp
= dvp
= dirp
= NULL
;
3525 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3526 nfsm_chain_get_32(error
, nmreq
, len
);
3527 nfsm_name_len_check(error
, nd
, len
);
3530 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3534 ni
.ni_cnd
.cn_flags
= LOCKPARENT
;
3535 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3537 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3539 /* update export stats */
3540 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3542 /* update active user stats */
3543 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3547 if (nd
->nd_vers
== NFS_VER3
) {
3548 nfsm_srv_pre_vattr_init(&dpreattr
);
3549 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3556 ni
.ni_cnd
.cn_nameiop
= 0;
3563 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3565 VATTR_SET(vap
, va_type
, VDIR
);
3569 * nameidone has to happen before we vnode_put(dvp)
3570 * since it may need to release the fs_nodelock on the dvp
3579 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
, nxo
, 0);
3581 /* construct ACL and handle inheritance */
3583 error
= kauth_acl_inherit(dvp
,
3589 if (!error
&& xacl
!= NULL
)
3590 VATTR_SET(vap
, va_acl
, xacl
);
3593 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3594 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3596 * We don't support the S_ISGID bit for directories. Solaris and other
3597 * SRV4 derived systems might set this to get BSD semantics, which we enforce
3600 if (VATTR_IS_ACTIVE(vap
, va_mode
))
3601 vap
->va_mode
&= ~S_ISGID
;
3603 * Server policy is to alway use the mapped rpc credential for
3604 * file system object creation. This has the nice side effect of
3605 * enforcing BSD creation semantics
3607 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3608 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3610 /* validate new-file security information */
3612 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
3614 * vnode_authattr_new can return errors other than EPERM, but that's not going to
3615 * sit well with our clients so we map all errors to EPERM.
3621 error
= VNOP_MKDIR(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
3624 if (nfsrv_fsevents_enabled
&& !error
)
3625 add_fsevent(FSE_CREATE_DIR
, ctx
, FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
3628 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
3630 * If some of the requested attributes weren't handled by the VNOP,
3631 * use our fallback code.
3633 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
3636 kauth_acl_free(xacl
);
3639 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, NULL
, vp
, ctx
, &nfh
);
3641 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
3642 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
3643 if (nd
->nd_vers
== NFS_VER2
)
3644 error
= postattrerr
;
3650 * nameidone has to happen before we vnode_put(dvp)
3651 * since it may need to release the fs_nodelock on the dvp
3656 ni
.ni_cnd
.cn_nameiop
= 0;
3659 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3660 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3666 /* assemble reply */
3667 nd
->nd_repstat
= error
;
3668 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
3669 NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3671 *mrepp
= nmrep
.nmc_mhead
;
3672 nfsmout_on_status(nd
, error
);
3673 if (nd
->nd_vers
== NFS_VER3
) {
3674 if (!nd
->nd_repstat
) {
3675 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3676 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
3678 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3679 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3681 nfsm_chain_add_fh(error
, &nmrep
, NFS_VER2
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3683 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
3686 nfsm_chain_build_done(error
, &nmrep
);
3687 if (ni
.ni_cnd
.cn_nameiop
) {
3689 * nameidone has to happen before we vnode_put(dvp)
3690 * since it may need to release the fs_nodelock on the dvp
3700 nfsm_chain_cleanup(&nmrep
);
3711 struct nfsrv_descript
*nd
,
3712 struct nfsrv_sock
*slp
,
3716 int error
, dpreattrerr
, dpostattrerr
;
3719 vnode_t vp
, dvp
, dirp
;
3720 struct vnode_attr dpreattr
, dpostattr
;
3721 struct nfs_filehandle nfh
;
3722 struct nfs_export
*nx
= NULL
;
3723 struct nfs_export_options
*nxo
;
3724 struct nameidata ni
;
3725 struct nfsm_chain
*nmreq
, nmrep
;
3728 dpreattrerr
= dpostattrerr
= ENOENT
;
3729 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3730 nmreq
= &nd
->nd_nmreq
;
3731 nfsm_chain_null(&nmrep
);
3733 vp
= dvp
= dirp
= NULL
;
3735 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3736 nfsm_chain_get_32(error
, nmreq
, len
);
3737 nfsm_name_len_check(error
, nd
, len
);
3740 ni
.ni_cnd
.cn_nameiop
= DELETE
;
3742 ni
.ni_op
= OP_UNLINK
;
3744 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
3745 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3747 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3749 /* update export stats */
3750 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3752 /* update active user stats */
3753 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3757 if (nd
->nd_vers
== NFS_VER3
) {
3758 nfsm_srv_pre_vattr_init(&dpreattr
);
3759 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3770 if (vnode_vtype(vp
) != VDIR
) {
3775 * No rmdir "." please.
3782 * The root of a mounted filesystem cannot be deleted.
3784 if (vnode_isvroot(vp
))
3787 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
, nxo
, 0);
3794 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_DELETE
, dvp
)) {
3796 if ((path
= get_pathbuff()) && !vn_getpath(vp
, path
, &plen
)) {
3797 get_fse_info(vp
, &finfo
, ctx
);
3799 release_pathbuff(path
);
3803 #endif /* CONFIG_FSE */
3805 error
= VNOP_RMDIR(dvp
, vp
, &ni
.ni_cnd
, ctx
);
3810 add_fsevent(FSE_DELETE
, ctx
,
3811 FSE_ARG_STRING
, plen
, path
,
3812 FSE_ARG_FINFO
, &finfo
,
3814 release_pathbuff(path
);
3816 #endif /* CONFIG_FSE */
3820 * nameidone has to happen before we vnode_put(dvp)
3821 * since it may need to release the fs_nodelock on the dvp
3829 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3830 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3836 /* assemble reply */
3837 nd
->nd_repstat
= error
;
3838 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
3840 *mrepp
= nmrep
.nmc_mhead
;
3841 nfsmout_on_status(nd
, error
);
3842 if (nd
->nd_vers
== NFS_VER3
)
3843 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3844 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3846 nfsm_chain_build_done(error
, &nmrep
);
3850 nfsm_chain_cleanup(&nmrep
);
3857 * nfs readdir service
3858 * - mallocs what it thinks is enough to read
3859 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
3860 * - calls VNOP_READDIR()
3861 * - loops around building the reply
3862 * if the output generated exceeds count break out of loop
3863 * The nfsm_clget macro is used here so that the reply will be packed
3864 * tightly in mbuf clusters.
3865 * - it only knows that it has encountered eof when the VNOP_READDIR()
3867 * - as such one readdir rpc will return eof false although you are there
3868 * and then the next will return eof
3869 * - it trims out records with d_fileno == 0
3870 * this doesn't matter for Unix clients, but they might confuse clients
3872 * NB: It is tempting to set eof to true if the VNOP_READDIR() reads less
3873 * than requested, but this may not apply to all filesystems. For
3874 * example, client NFS does not { although it is never remote mounted
3876 * The alternate call nfsrv_readdirplus() does lookups as well.
3877 * PS: The XNFS protocol spec clearly describes what the "count"s arguments
3878 * are supposed to cover. For readdir, the count is the total number of
3879 * bytes included in everything from the directory's postopattr through
3880 * the EOF flag. For readdirplus, the maxcount is the same, and the
3881 * dircount includes all that except for the entry attributes and handles.
3885 struct nfsrv_descript
*nd
,
3886 struct nfsrv_sock
*slp
,
3890 struct direntry
*dp
;
3891 char *cpos
, *cend
, *rbuf
;
3893 struct vnode_attr attr
;
3894 struct nfs_filehandle nfh
;
3895 struct nfs_export
*nx
;
3896 struct nfs_export_options
*nxo
;
3898 char uio_buf
[ UIO_SIZEOF(1) ];
3899 int len
, nlen
, rem
, xfer
, error
, attrerr
;
3900 int siz
, count
, fullsiz
, eofflag
, nentries
;
3901 u_quad_t off
, toff
, verf
;
3903 struct nfsm_chain
*nmreq
, nmrep
;
3907 count
= nentries
= 0;
3908 nmreq
= &nd
->nd_nmreq
;
3909 nfsm_chain_null(&nmrep
);
3913 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
3915 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3916 if (nd
->nd_vers
== NFS_VER3
) {
3917 nfsm_chain_get_64(error
, nmreq
, toff
);
3918 nfsm_chain_get_64(error
, nmreq
, verf
);
3920 nfsm_chain_get_32(error
, nmreq
, toff
);
3922 nfsm_chain_get_32(error
, nmreq
, count
);
3926 siz
= ((count
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
3927 xfer
= NFSRV_NDMAXDATA(nd
);
3932 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
3935 /* update export stats */
3936 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3938 /* update active user stats */
3939 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
3941 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
3944 if (nxo
->nxo_flags
& NX_MANGLEDNAMES
|| nd
->nd_vers
== NFS_VER2
)
3945 vnopflag
|= VNODE_READDIR_NAMEMAX
;
3947 if ((nd
->nd_vers
== NFS_VER2
) || (nxo
->nxo_flags
& NX_32BITCLIENTS
))
3948 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
3950 if (nd
->nd_vers
== NFS_VER3
) {
3951 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
3952 error
= attrerr
= vnode_getattr(vp
, &attr
, ctx
);
3953 if (!error
&& toff
&& verf
&& (verf
!= attr
.va_filerev
))
3954 error
= NFSERR_BAD_COOKIE
;
3957 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, ctx
, nxo
, 0);
3960 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
3962 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3963 &uio_buf
[0], sizeof(uio_buf
));
3964 if (!rbuf
|| !auio
) {
3969 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
3970 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
3972 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, ctx
);
3973 off
= uio_offset(auio
);
3975 if (nd
->nd_vers
== NFS_VER3
) {
3976 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
3977 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
3981 if (uio_resid(auio
) != 0) {
3982 siz
-= uio_resid(auio
);
3984 /* If nothing read, return empty reply with eof set */
3989 /* assemble reply */
3990 nd
->nd_repstat
= error
;
3991 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) +
3992 NFSX_COOKIEVERF(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
);
3994 *mrepp
= nmrep
.nmc_mhead
;
3995 nfsmout_on_status(nd
, error
);
3996 if (nd
->nd_vers
== NFS_VER3
) {
3997 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
3998 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4000 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4001 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4002 nfsm_chain_build_done(error
, &nmrep
);
4008 * Check for degenerate cases of nothing useful read.
4009 * If so go try again
4013 dp
= (struct direntry
*)cpos
;
4014 while ((dp
->d_fileno
== 0) && (cpos
< cend
) && (nentries
> 0)) {
4015 cpos
+= dp
->d_reclen
;
4016 dp
= (struct direntry
*)cpos
;
4019 if ((cpos
>= cend
) || (nentries
== 0)) {
4028 /* assemble reply */
4029 nd
->nd_repstat
= error
;
4030 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) +
4031 NFSX_COOKIEVERF(nd
->nd_vers
) + siz
);
4033 *mrepp
= nmrep
.nmc_mhead
;
4034 nfsmout_on_status(nd
, error
);
4035 nmrep
.nmc_flags
|= NFSM_CHAIN_FLAG_ADD_CLUSTERS
;
4037 len
= 2 * NFSX_UNSIGNED
;
4038 if (nd
->nd_vers
== NFS_VER3
) {
4039 len
+= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
;
4040 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4041 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4045 /* Loop through the records and build reply */
4046 while ((cpos
< cend
) && (nentries
> 0)) {
4047 if (dp
->d_fileno
!= 0) {
4048 nlen
= dp
->d_namlen
;
4049 if ((nd
->nd_vers
== NFS_VER2
) && (nlen
> NFS_MAXNAMLEN
))
4050 nlen
= NFS_MAXNAMLEN
;
4051 rem
= nfsm_rndup(nlen
)-nlen
;
4052 len
+= (4 * NFSX_UNSIGNED
+ nlen
+ rem
);
4053 if (nd
->nd_vers
== NFS_VER3
)
4054 len
+= 2 * NFSX_UNSIGNED
;
4059 /* Build the directory record xdr from the direntry. */
4060 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4061 if (nd
->nd_vers
== NFS_VER3
) {
4062 nfsm_chain_add_64(error
, &nmrep
, dp
->d_fileno
);
4064 nfsm_chain_add_32(error
, &nmrep
, dp
->d_fileno
);
4066 nfsm_chain_add_string(error
, &nmrep
, dp
->d_name
, nlen
);
4067 if (nd
->nd_vers
== NFS_VER3
) {
4068 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
4069 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
4070 nfsm_chain_add_64(error
, &nmrep
, dp
->d_seekoff
);
4072 nfsm_chain_add_32(error
, &nmrep
, dp
->d_seekoff
);
4076 cpos
+= dp
->d_reclen
;
4077 dp
= (struct direntry
*)cpos
;
4080 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4081 nfsm_chain_add_32(error
, &nmrep
, eofflag
? TRUE
: FALSE
);
4089 nd
->nd_repstat
= error
;
4090 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
));
4092 *mrepp
= nmrep
.nmc_mhead
;
4093 nfsmout_on_status(nd
, error
);
4094 if (nd
->nd_vers
== NFS_VER3
)
4095 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4097 nfsm_chain_build_done(error
, &nmrep
);
4099 nfsm_chain_cleanup(&nmrep
);
4107 struct nfsrv_descript
*nd
,
4108 struct nfsrv_sock
*slp
,
4112 struct direntry
*dp
;
4113 char *cpos
, *cend
, *rbuf
;
4115 struct nfs_filehandle dnfh
, nfh
;
4116 struct nfs_export
*nx
;
4117 struct nfs_export_options
*nxo
;
4119 char uio_buf
[ UIO_SIZEOF(1) ];
4120 struct vnode_attr attr
, va
, *vap
= &va
;
4121 int len
, nlen
, rem
, xfer
, error
, attrerr
, gotfh
, gotattr
;
4122 int siz
, dircount
, maxcount
, fullsiz
, eofflag
, dirlen
, nentries
, isdotdot
;
4123 u_quad_t off
, toff
, verf
;
4125 struct nfsm_chain
*nmreq
, nmrep
;
4130 nmreq
= &nd
->nd_nmreq
;
4131 nfsm_chain_null(&nmrep
);
4134 dircount
= maxcount
= 0;
4136 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
4138 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
4139 nfsm_chain_get_64(error
, nmreq
, toff
);
4140 nfsm_chain_get_64(error
, nmreq
, verf
);
4141 nfsm_chain_get_32(error
, nmreq
, dircount
);
4142 nfsm_chain_get_32(error
, nmreq
, maxcount
);
4146 xfer
= NFSRV_NDMAXDATA(nd
);
4147 dircount
= ((dircount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
4148 if (dircount
> xfer
)
4150 fullsiz
= siz
= dircount
;
4151 maxcount
= ((maxcount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
4152 if (maxcount
> xfer
)
4155 error
= nfsrv_fhtovp(&dnfh
, nd
, &vp
, &nx
, &nxo
);
4158 /* update export stats */
4159 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4161 /* update active user stats */
4162 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4164 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4167 if (nxo
->nxo_flags
& NX_32BITCLIENTS
)
4168 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
4170 if (nxo
->nxo_flags
& NX_MANGLEDNAMES
)
4171 vnopflag
|= VNODE_READDIR_NAMEMAX
;
4173 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4174 error
= attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4175 if (!error
&& toff
&& verf
&& (verf
!= attr
.va_filerev
))
4176 error
= NFSERR_BAD_COOKIE
;
4178 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, ctx
, nxo
, 0);
4181 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
4183 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
4184 &uio_buf
[0], sizeof(uio_buf
));
4185 if (!rbuf
|| !auio
) {
4191 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
4192 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
4194 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, ctx
);
4195 off
= uio_offset(auio
);
4196 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4197 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4200 if (uio_resid(auio
) != 0) {
4201 siz
-= uio_resid(auio
);
4203 /* If nothing read, return empty reply with eof set */
4208 /* assemble reply */
4209 nd
->nd_repstat
= error
;
4210 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+
4211 NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
);
4213 *mrepp
= nmrep
.nmc_mhead
;
4214 nfsmout_on_status(nd
, error
);
4215 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4216 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4217 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4218 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4219 nfsm_chain_build_done(error
, &nmrep
);
4225 * Check for degenerate cases of nothing useful read.
4226 * If so go try again
4230 dp
= (struct direntry
*)cpos
;
4231 while ((dp
->d_fileno
== 0) && (cpos
< cend
) && (nentries
> 0)) {
4232 cpos
+= dp
->d_reclen
;
4233 dp
= (struct direntry
*)cpos
;
4236 if ((cpos
>= cend
) || (nentries
== 0)) {
4243 * Probe one of the directory entries to see if the filesystem
4246 if ((error
= VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, ctx
))) {
4247 if (error
== ENOTSUP
) /* let others get passed back */
4248 error
= NFSERR_NOTSUPP
;
4253 /* assemble reply */
4254 nd
->nd_repstat
= error
;
4255 error
= nfsrv_rephead(nd
, slp
, &nmrep
, maxcount
);
4257 *mrepp
= nmrep
.nmc_mhead
;
4258 nfsmout_on_status(nd
, error
);
4259 nmrep
.nmc_flags
|= NFSM_CHAIN_FLAG_ADD_CLUSTERS
;
4261 dirlen
= len
= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
;
4262 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4263 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4266 /* Loop through the records and build reply */
4267 while ((cpos
< cend
) && (nentries
> 0)) {
4268 if (dp
->d_fileno
!= 0) {
4269 nlen
= dp
->d_namlen
;
4270 rem
= nfsm_rndup(nlen
)-nlen
;
4271 gotfh
= gotattr
= 1;
4273 /* Got to get the vnode for lookup per entry. */
4274 if (VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, ctx
)) {
4275 /* Can't get the vnode... so no fh or attrs */
4276 gotfh
= gotattr
= 0;
4278 isdotdot
= ((dp
->d_namlen
== 2) &&
4279 (dp
->d_name
[0] == '.') && (dp
->d_name
[1] == '.'));
4280 if (nfsrv_vptofh(nx
, 0, (isdotdot
? &dnfh
: NULL
), nvp
, ctx
, &nfh
))
4282 nfsm_srv_vattr_init(vap
, NFS_VER3
);
4283 if (vnode_getattr(nvp
, vap
, ctx
))
4289 * If either the dircount or maxcount will be
4290 * exceeded, get out now. Both of these lengths
4291 * are calculated conservatively, including all
4294 len
+= 8 * NFSX_UNSIGNED
+ nlen
+ rem
;
4296 len
+= NFSX_V3FATTR
;
4298 len
+= NFSX_UNSIGNED
+ nfsm_rndup(nfh
.nfh_len
);
4299 dirlen
+= 6 * NFSX_UNSIGNED
+ nlen
+ rem
;
4300 if ((len
> maxcount
) || (dirlen
> dircount
)) {
4305 /* Build the directory record xdr from the direntry. */
4306 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4307 nfsm_chain_add_64(error
, &nmrep
, dp
->d_fileno
);
4308 nfsm_chain_add_string(error
, &nmrep
, dp
->d_name
, nlen
);
4309 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
4310 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
4311 nfsm_chain_add_64(error
, &nmrep
, dp
->d_seekoff
);
4312 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, (gotattr
? 0 : ENOENT
), vap
);
4314 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4316 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4319 cpos
+= dp
->d_reclen
;
4320 dp
= (struct direntry
*)cpos
;
4325 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4326 nfsm_chain_add_32(error
, &nmrep
, eofflag
? TRUE
: FALSE
);
4332 nd
->nd_repstat
= error
;
4333 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
);
4335 *mrepp
= nmrep
.nmc_mhead
;
4336 nfsmout_on_status(nd
, error
);
4337 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4339 nfsm_chain_build_done(error
, &nmrep
);
4343 nfsm_chain_cleanup(&nmrep
);
4350 * nfs commit service
4354 struct nfsrv_descript
*nd
,
4355 struct nfsrv_sock
*slp
,
4360 struct nfs_filehandle nfh
;
4361 struct nfs_export
*nx
;
4362 struct nfs_export_options
*nxo
;
4363 int error
, preattrerr
, postattrerr
, count
;
4364 struct vnode_attr preattr
, postattr
;
4366 struct nfsm_chain
*nmreq
, nmrep
;
4369 preattrerr
= postattrerr
= ENOENT
;
4370 nmreq
= &nd
->nd_nmreq
;
4371 nfsm_chain_null(&nmrep
);
4375 * XXX At this time VNOP_FSYNC() does not accept offset and byte
4376 * count parameters, so those arguments are useless (someday maybe).
4379 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4380 nfsm_chain_get_64(error
, nmreq
, off
);
4381 nfsm_chain_get_32(error
, nmreq
, count
);
4384 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4387 /* update export stats */
4388 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4390 /* update active user stats */
4391 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4393 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4396 nfsm_srv_pre_vattr_init(&preattr
);
4397 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
4399 error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
4401 nfsm_srv_vattr_init(&postattr
, 1);
4402 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
4408 /* assemble reply */
4409 nd
->nd_repstat
= error
;
4410 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3WCCDATA
+ NFSX_V3WRITEVERF
);
4412 *mrepp
= nmrep
.nmc_mhead
;
4413 nfsmout_on_status(nd
, error
);
4414 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
4415 preattrerr
, &preattr
, postattrerr
, &postattr
);
4416 if (!nd
->nd_repstat
) {
4417 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
4418 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
4421 nfsm_chain_build_done(error
, &nmrep
);
4423 nfsm_chain_cleanup(&nmrep
);
4430 * nfs statfs service
4434 struct nfsrv_descript
*nd
,
4435 struct nfsrv_sock
*slp
,
4442 struct vnode_attr attr
;
4443 struct nfs_filehandle nfh
;
4444 struct nfs_export
*nx
;
4445 struct nfs_export_options
*nxo
;
4447 struct nfsm_chain
*nmreq
, nmrep
;
4451 nmreq
= &nd
->nd_nmreq
;
4452 nfsm_chain_null(&nmrep
);
4456 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4458 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4461 /* update export stats */
4462 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4464 /* update active user stats */
4465 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4467 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4471 VFSATTR_WANTED(&va
, f_blocks
);
4472 VFSATTR_WANTED(&va
, f_bavail
);
4473 VFSATTR_WANTED(&va
, f_files
);
4474 VFSATTR_WANTED(&va
, f_ffree
);
4475 error
= vfs_getattr(vnode_mount(vp
), &va
, ctx
);
4476 blksize
= vnode_mount(vp
)->mnt_vfsstat
.f_bsize
;
4478 if (nd
->nd_vers
== NFS_VER3
) {
4479 nfsm_srv_vattr_init(&attr
, nd
->nd_vers
);
4480 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4487 /* assemble reply */
4488 nd
->nd_repstat
= error
;
4489 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_STATFS(nd
->nd_vers
));
4491 *mrepp
= nmrep
.nmc_mhead
;
4492 nfsmout_on_status(nd
, error
);
4493 if (nd
->nd_vers
== NFS_VER3
)
4494 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4495 nfsmout_if(nd
->nd_repstat
);
4497 if (nd
->nd_vers
== NFS_VER3
) {
4498 nfsm_chain_add_64(error
, &nmrep
, va
.f_blocks
* blksize
);
4499 nfsm_chain_add_64(error
, &nmrep
, va
.f_bfree
* blksize
);
4500 nfsm_chain_add_64(error
, &nmrep
, va
.f_bavail
* blksize
);
4501 nfsm_chain_add_64(error
, &nmrep
, va
.f_files
);
4502 nfsm_chain_add_64(error
, &nmrep
, va
.f_ffree
);
4503 nfsm_chain_add_64(error
, &nmrep
, va
.f_ffree
);
4504 nfsm_chain_add_32(error
, &nmrep
, 0); /* invarsec */
4506 nfsm_chain_add_32(error
, &nmrep
, NFS_V2MAXDATA
);
4507 nfsm_chain_add_32(error
, &nmrep
, blksize
);
4508 nfsm_chain_add_32(error
, &nmrep
, va
.f_blocks
);
4509 nfsm_chain_add_32(error
, &nmrep
, va
.f_bfree
);
4510 nfsm_chain_add_32(error
, &nmrep
, va
.f_bavail
);
4513 nfsm_chain_build_done(error
, &nmrep
);
4515 nfsm_chain_cleanup(&nmrep
);
4522 * nfs fsinfo service
4526 struct nfsrv_descript
*nd
,
4527 struct nfsrv_sock
*slp
,
4531 int error
, attrerr
, prefsize
, maxsize
;
4533 struct vnode_attr attr
;
4534 struct nfs_filehandle nfh
;
4535 struct nfs_export
*nx
;
4536 struct nfs_export_options
*nxo
;
4537 struct nfsm_chain
*nmreq
, nmrep
;
4541 nmreq
= &nd
->nd_nmreq
;
4542 nfsm_chain_null(&nmrep
);
4545 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4547 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4550 /* update export stats */
4551 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4553 /* update active user stats */
4554 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4556 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4559 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4560 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4566 /* assemble reply */
4567 nd
->nd_repstat
= error
;
4568 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+ NFSX_V3FSINFO
);
4570 *mrepp
= nmrep
.nmc_mhead
;
4571 nfsmout_on_status(nd
, error
);
4572 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4573 nfsmout_if(nd
->nd_repstat
);
4576 * XXX There should be file system VFS OP(s) to get this information.
4577 * For now, assume our usual NFS defaults.
4579 if (slp
->ns_sotype
== SOCK_DGRAM
) {
4580 maxsize
= NFS_MAXDGRAMDATA
;
4581 prefsize
= NFS_PREFDGRAMDATA
;
4583 maxsize
= prefsize
= NFSRV_MAXDATA
;
4585 nfsm_chain_add_32(error
, &nmrep
, maxsize
);
4586 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4587 nfsm_chain_add_32(error
, &nmrep
, NFS_FABLKSIZE
);
4588 nfsm_chain_add_32(error
, &nmrep
, maxsize
);
4589 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4590 nfsm_chain_add_32(error
, &nmrep
, NFS_FABLKSIZE
);
4591 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4592 nfsm_chain_add_64(error
, &nmrep
, 0xffffffffffffffffULL
);
4593 nfsm_chain_add_32(error
, &nmrep
, 0);
4594 nfsm_chain_add_32(error
, &nmrep
, 1);
4595 /* XXX link/symlink support should be taken from volume capabilities */
4596 nfsm_chain_add_32(error
, &nmrep
,
4597 NFSV3FSINFO_LINK
| NFSV3FSINFO_SYMLINK
|
4598 NFSV3FSINFO_HOMOGENEOUS
| NFSV3FSINFO_CANSETTIME
);
4601 nfsm_chain_build_done(error
, &nmrep
);
4603 nfsm_chain_cleanup(&nmrep
);
4610 * nfs pathconf service
4614 struct nfsrv_descript
*nd
,
4615 struct nfsrv_sock
*slp
,
4619 int error
, attrerr
, linkmax
, namemax
;
4620 int chownres
, notrunc
, case_sensitive
, case_preserving
;
4622 struct vnode_attr attr
;
4623 struct nfs_filehandle nfh
;
4624 struct nfs_export
*nx
;
4625 struct nfs_export_options
*nxo
;
4626 struct nfsm_chain
*nmreq
, nmrep
;
4630 nmreq
= &nd
->nd_nmreq
;
4631 nfsm_chain_null(&nmrep
);
4634 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4636 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4639 /* update export stats */
4640 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4642 /* update active user stats */
4643 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4645 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4648 error
= VNOP_PATHCONF(vp
, _PC_LINK_MAX
, &linkmax
, ctx
);
4650 error
= VNOP_PATHCONF(vp
, _PC_NAME_MAX
, &namemax
, ctx
);
4652 error
= VNOP_PATHCONF(vp
, _PC_CHOWN_RESTRICTED
, &chownres
, ctx
);
4654 error
= VNOP_PATHCONF(vp
, _PC_NO_TRUNC
, ¬runc
, ctx
);
4656 error
= VNOP_PATHCONF(vp
, _PC_CASE_SENSITIVE
, &case_sensitive
, ctx
);
4658 error
= VNOP_PATHCONF(vp
, _PC_CASE_PRESERVING
, &case_preserving
, ctx
);
4660 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4661 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4667 /* assemble reply */
4668 nd
->nd_repstat
= error
;
4669 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+ NFSX_V3PATHCONF
);
4671 *mrepp
= nmrep
.nmc_mhead
;
4672 nfsmout_on_status(nd
, error
);
4673 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4674 nfsmout_if(nd
->nd_repstat
);
4676 nfsm_chain_add_32(error
, &nmrep
, linkmax
);
4677 nfsm_chain_add_32(error
, &nmrep
, namemax
);
4678 nfsm_chain_add_32(error
, &nmrep
, notrunc
);
4679 nfsm_chain_add_32(error
, &nmrep
, chownres
);
4680 nfsm_chain_add_32(error
, &nmrep
, !case_sensitive
);
4681 nfsm_chain_add_32(error
, &nmrep
, case_preserving
);
4684 nfsm_chain_build_done(error
, &nmrep
);
4686 nfsm_chain_cleanup(&nmrep
);
4693 * Null operation, used by clients to ping server
4698 struct nfsrv_descript
*nd
,
4699 struct nfsrv_sock
*slp
,
4700 __unused vfs_context_t ctx
,
4703 int error
= NFSERR_RETVOID
;
4704 struct nfsm_chain nmrep
;
4707 * RPCSEC_GSS context setup ?
4709 if (nd
->nd_gss_context
)
4710 return(nfs_gss_svc_ctx_init(nd
, slp
, mrepp
));
4712 nfsm_chain_null(&nmrep
);
4714 /* assemble reply */
4715 nd
->nd_repstat
= error
;
4716 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 0);
4718 *mrepp
= nmrep
.nmc_mhead
;
4720 nfsm_chain_build_done(error
, &nmrep
);
4722 nfsm_chain_cleanup(&nmrep
);
4729 * No operation, used for obsolete procedures
4734 struct nfsrv_descript
*nd
,
4735 struct nfsrv_sock
*slp
,
4736 __unused vfs_context_t ctx
,
4740 struct nfsm_chain nmrep
;
4742 nfsm_chain_null(&nmrep
);
4745 error
= nd
->nd_repstat
;
4747 error
= EPROCUNAVAIL
;
4749 /* assemble reply */
4750 nd
->nd_repstat
= error
;
4751 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 0);
4753 *mrepp
= nmrep
.nmc_mhead
;
4755 nfsm_chain_build_done(error
, &nmrep
);
4757 nfsm_chain_cleanup(&nmrep
);
4763 int (*nfsrv_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
4764 struct nfsrv_sock
*slp
,
4793 * Perform access checking for vnodes obtained from file handles that would
4794 * refer to files already opened by a Unix client. You cannot just use
4795 * vnode_authorize() for two reasons.
4796 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4797 * 2 - The owner is to be given access irrespective of mode bits so that
4798 * processes that chmod after opening a file don't break. I don't like
4799 * this because it opens a security hole, but since the nfs server opens
4800 * a security hole the size of a barn door anyhow, what the heck.
4802 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, vnode_authorize()
4803 * will return EPERM instead of EACCESS. EPERM is always an error.
4810 kauth_action_t action
,
4812 struct nfs_export_options
*nxo
,
4815 struct vnode_attr vattr
;
4818 if (action
& KAUTH_VNODE_WRITE_RIGHTS
) {
4820 * Disallow write attempts on read-only exports;
4821 * unless the file is a socket or a block or character
4822 * device resident on the file system.
4824 if (nxo
->nxo_flags
& NX_READONLY
) {
4825 switch (vnode_vtype(vp
)) {
4826 case VREG
: case VDIR
: case VLNK
: case VCPLX
:
4833 error
= vnode_authorize(vp
, dvp
, action
, ctx
);
4835 * Allow certain operations for the owner (reads and writes
4836 * on files that are already open). Picking up from FreeBSD.
4838 if (override
&& (error
== EACCES
)) {
4840 VATTR_WANTED(&vattr
, va_uid
);
4841 if ((vnode_getattr(vp
, &vattr
, ctx
) == 0) &&
4842 (kauth_cred_getuid(vfs_context_ucred(ctx
)) == vattr
.va_uid
))
4848 #endif /* NFSSERVER */