2 * Copyright (c) 2000-2016 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>
91 #include <sys/vmparam.h>
93 #include <sys/fcntl.h>
95 #include <netinet/in.h>
97 #include <nfs/nfsproto.h>
98 #include <nfs/rpcv2.h>
100 #include <nfs/xdr_subs.h>
101 #include <nfs/nfsm_subs.h>
102 #include <nfs/nfsrvcache.h>
103 #include <nfs/nfs_gss.h>
106 #include <security/mac.h>
107 #include <security/mac_framework.h>
116 int nfsd_thread_count
= 0;
117 int nfsd_thread_max
= 0;
118 lck_grp_t
*nfsd_lck_grp
;
119 lck_mtx_t
*nfsd_mutex
;
120 struct nfsd_head nfsd_head
, nfsd_queue
;
122 lck_grp_t
*nfsrv_slp_rwlock_group
;
123 lck_grp_t
*nfsrv_slp_mutex_group
;
124 struct nfsrv_sockhead nfsrv_socklist
, nfsrv_sockwg
,
125 nfsrv_sockwait
, nfsrv_sockwork
;
126 struct nfsrv_sock
*nfsrv_udpsock
= NULL
;
127 struct nfsrv_sock
*nfsrv_udp6sock
= NULL
;
130 struct nfsrv_expfs_list nfsrv_exports
;
131 struct nfsrv_export_hashhead
*nfsrv_export_hashtbl
= NULL
;
132 int nfsrv_export_hash_size
= NFSRVEXPHASHSZ
;
133 u_long nfsrv_export_hash
;
134 lck_grp_t
*nfsrv_export_rwlock_group
;
135 lck_rw_t nfsrv_export_rwlock
;
138 /* NFS server file modification event generator */
139 struct nfsrv_fmod_hashhead
*nfsrv_fmod_hashtbl
;
140 u_long nfsrv_fmod_hash
;
141 lck_grp_t
*nfsrv_fmod_grp
;
142 lck_mtx_t
*nfsrv_fmod_mutex
;
143 static int nfsrv_fmod_timer_on
= 0;
144 int nfsrv_fsevents_enabled
= 1;
147 /* NFS server timers */
149 thread_call_t nfsrv_fmod_timer_call
;
151 thread_call_t nfsrv_idlesock_timer_call
;
152 thread_call_t nfsrv_wg_timer_call
;
153 int nfsrv_wg_timer_on
;
155 /* globals for the active user list */
156 uint32_t nfsrv_user_stat_enabled
= 1;
157 uint32_t nfsrv_user_stat_node_count
= 0;
158 uint32_t nfsrv_user_stat_max_idle_sec
= NFSRV_USER_STAT_DEF_IDLE_SEC
;
159 uint32_t nfsrv_user_stat_max_nodes
= NFSRV_USER_STAT_DEF_MAX_NODES
;
160 lck_grp_t
*nfsrv_active_user_mutex_group
;
162 int nfsrv_wg_delay
= NFSRV_WGATHERDELAY
* 1000;
163 int nfsrv_wg_delay_v3
= 0;
167 int nfsrv_authorize(vnode_t
,vnode_t
,kauth_action_t
,vfs_context_t
,struct nfs_export_options
*,int);
168 int nfsrv_wg_coalesce(struct nfsrv_descript
*, struct nfsrv_descript
*);
169 void nfsrv_modified(vnode_t
, vfs_context_t
);
171 extern void IOSleep(int);
172 extern int safe_getpath(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
);
175 * Initialize the data structures for the server.
178 #define NFSRV_NOT_INITIALIZED 0
179 #define NFSRV_INITIALIZING 1
180 #define NFSRV_INITIALIZED 2
181 static volatile UInt32 nfsrv_initted
= NFSRV_NOT_INITIALIZED
;
184 nfsrv_is_initialized(void)
186 return (nfsrv_initted
== NFSRV_INITIALIZED
);
192 /* make sure we init only once */
193 if (!OSCompareAndSwap(NFSRV_NOT_INITIALIZED
, NFSRV_INITIALIZING
, &nfsrv_initted
)) {
194 /* wait until initialization is complete */
195 while (!nfsrv_is_initialized())
200 if (sizeof (struct nfsrv_sock
) > NFS_SVCALLOC
)
201 printf("struct nfsrv_sock bloated (> %dbytes)\n",NFS_SVCALLOC
);
203 /* init nfsd mutex */
204 nfsd_lck_grp
= lck_grp_alloc_init("nfsd", LCK_GRP_ATTR_NULL
);
205 nfsd_mutex
= lck_mtx_alloc_init(nfsd_lck_grp
, LCK_ATTR_NULL
);
207 /* init slp rwlock */
208 nfsrv_slp_rwlock_group
= lck_grp_alloc_init("nfsrv-slp-rwlock", LCK_GRP_ATTR_NULL
);
209 nfsrv_slp_mutex_group
= lck_grp_alloc_init("nfsrv-slp-mutex", LCK_GRP_ATTR_NULL
);
211 /* init export data structures */
212 LIST_INIT(&nfsrv_exports
);
213 nfsrv_export_rwlock_group
= lck_grp_alloc_init("nfsrv-export-rwlock", LCK_GRP_ATTR_NULL
);
214 lck_rw_init(&nfsrv_export_rwlock
, nfsrv_export_rwlock_group
, LCK_ATTR_NULL
);
216 /* init active user list mutex structures */
217 nfsrv_active_user_mutex_group
= lck_grp_alloc_init("nfs-active-user-mutex", LCK_GRP_ATTR_NULL
);
219 /* init nfs server request cache mutex */
220 nfsrv_reqcache_lck_grp
= lck_grp_alloc_init("nfsrv_reqcache", LCK_GRP_ATTR_NULL
);
221 nfsrv_reqcache_mutex
= lck_mtx_alloc_init(nfsrv_reqcache_lck_grp
, LCK_ATTR_NULL
);
224 /* init NFS server file modified event generation */
225 nfsrv_fmod_hashtbl
= hashinit(NFSRVFMODHASHSZ
, M_TEMP
, &nfsrv_fmod_hash
);
226 nfsrv_fmod_grp
= lck_grp_alloc_init("nfsrv_fmod", LCK_GRP_ATTR_NULL
);
227 nfsrv_fmod_mutex
= lck_mtx_alloc_init(nfsrv_fmod_grp
, LCK_ATTR_NULL
);
230 /* initialize NFS server timer callouts */
232 nfsrv_fmod_timer_call
= thread_call_allocate(nfsrv_fmod_timer
, NULL
);
234 nfsrv_idlesock_timer_call
= thread_call_allocate(nfsrv_idlesock_timer
, NULL
);
235 nfsrv_wg_timer_call
= thread_call_allocate(nfsrv_wg_timer
, NULL
);
237 /* Init server data structures */
238 TAILQ_INIT(&nfsrv_socklist
);
239 TAILQ_INIT(&nfsrv_sockwait
);
240 TAILQ_INIT(&nfsrv_sockwork
);
241 TAILQ_INIT(&nfsrv_sockwg
);
242 TAILQ_INIT(&nfsd_head
);
243 TAILQ_INIT(&nfsd_queue
);
244 nfsrv_udpsock
= NULL
;
245 nfsrv_udp6sock
= NULL
;
247 /* Setup the up-call handling */
250 /* initialization complete */
251 nfsrv_initted
= NFSRV_INITIALIZED
;
257 * NFS version 2 and 3 server request processing functions
259 * These functions take the following parameters:
261 * struct nfsrv_descript *nd - the NFS request descriptor
262 * struct nfsrv_sock *slp - the NFS socket the request came in on
263 * vfs_context_t ctx - VFS context
264 * mbuf_t *mrepp - pointer to hold the reply mbuf list
266 * These routines generally have 3 phases:
268 * 1 - break down and validate the RPC request in the mbuf chain
269 * provided in nd->nd_nmreq.
270 * 2 - perform the vnode operations for the request
271 * (many are very similar to syscalls in vfs_syscalls.c and
272 * should therefore be kept in sync with those implementations)
273 * 3 - build the RPC reply in an mbuf chain (nmrep) and return the mbuf chain
278 * nfs v3 access service
282 struct nfsrv_descript
*nd
,
283 struct nfsrv_sock
*slp
,
287 struct nfsm_chain
*nmreq
, nmrep
;
290 struct vnode_attr vattr
;
291 struct nfs_filehandle nfh
;
293 kauth_action_t testaction
;
294 struct nfs_export
*nx
;
295 struct nfs_export_options
*nxo
;
300 nmreq
= &nd
->nd_nmreq
;
301 nfsm_chain_null(&nmrep
);
305 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
306 nfsm_chain_get_32(error
, nmreq
, nfsmode
);
308 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
311 /* update export stats */
312 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
314 /* update active user stats */
315 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
317 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
321 * Each NFS mode bit is tested separately.
323 * XXX this code is nominally correct, but returns a pessimistic
324 * rather than optimistic result. It will be necessary to add
325 * an NFS-specific interface to the vnode_authorize code to
326 * obtain good performance in the optimistic mode.
328 if (nfsmode
& NFS_ACCESS_READ
) {
329 testaction
= vnode_isdir(vp
) ? KAUTH_VNODE_LIST_DIRECTORY
: KAUTH_VNODE_READ_DATA
;
330 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
331 nfsmode
&= ~NFS_ACCESS_READ
;
333 if ((nfsmode
& NFS_ACCESS_LOOKUP
) &&
335 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
, nxo
, 0)))
336 nfsmode
&= ~NFS_ACCESS_LOOKUP
;
337 if (nfsmode
& NFS_ACCESS_MODIFY
) {
338 if (vnode_isdir(vp
)) {
340 KAUTH_VNODE_ADD_FILE
|
341 KAUTH_VNODE_ADD_SUBDIRECTORY
|
342 KAUTH_VNODE_DELETE_CHILD
;
345 KAUTH_VNODE_WRITE_DATA
;
347 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
348 nfsmode
&= ~NFS_ACCESS_MODIFY
;
350 if (nfsmode
& NFS_ACCESS_EXTEND
) {
351 if (vnode_isdir(vp
)) {
353 KAUTH_VNODE_ADD_FILE
|
354 KAUTH_VNODE_ADD_SUBDIRECTORY
;
357 KAUTH_VNODE_WRITE_DATA
|
358 KAUTH_VNODE_APPEND_DATA
;
360 if (nfsrv_authorize(vp
, NULL
, testaction
, ctx
, nxo
, 0))
361 nfsmode
&= ~NFS_ACCESS_EXTEND
;
365 * Note concerning NFS_ACCESS_DELETE:
366 * For hard links, the answer may be wrong if the vnode
367 * has multiple parents with different permissions.
368 * Also, some clients (e.g. MacOSX 10.3) may incorrectly
369 * interpret the missing/cleared DELETE bit.
370 * So we'll just leave the DELETE bit alone. At worst,
371 * we're telling the client it might be able to do
372 * something it really can't.
375 if ((nfsmode
& NFS_ACCESS_EXECUTE
) &&
377 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
, nxo
, 0)))
378 nfsmode
&= ~NFS_ACCESS_EXECUTE
;
380 /* get postop attributes */
381 nfsm_srv_vattr_init(&vattr
, NFS_VER3
);
382 attrerr
= vnode_getattr(vp
, &vattr
, ctx
);
386 nd
->nd_repstat
= error
;
387 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(NFS_VER3
) + NFSX_UNSIGNED
);
389 *mrepp
= nmrep
.nmc_mhead
;
390 nfsmout_on_status(nd
, error
);
391 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &vattr
);
393 nfsm_chain_add_32(error
, &nmrep
, nfsmode
);
395 nfsm_chain_build_done(error
, &nmrep
);
399 nfsm_chain_cleanup(&nmrep
);
406 * nfs getattr service
410 struct nfsrv_descript
*nd
,
411 struct nfsrv_sock
*slp
,
415 struct nfsm_chain
*nmreq
, nmrep
;
416 struct vnode_attr vattr
;
419 struct nfs_filehandle nfh
;
420 struct nfs_export
*nx
;
421 struct nfs_export_options
*nxo
;
424 nmreq
= &nd
->nd_nmreq
;
425 nfsm_chain_null(&nmrep
);
429 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
431 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
434 /* update export stats */
435 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
437 /* update active user stats */
438 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
440 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
443 nfsm_srv_vattr_init(&vattr
, nd
->nd_vers
);
444 error
= vnode_getattr(vp
, &vattr
, ctx
);
450 nd
->nd_repstat
= error
;
451 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_FATTR(nd
->nd_vers
));
453 *mrepp
= nmrep
.nmc_mhead
;
454 nfsmout_if(nd
->nd_repstat
);
455 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &vattr
);
457 nfsm_chain_build_done(error
, &nmrep
);
461 nfsm_chain_cleanup(&nmrep
);
468 * nfs setattr service
472 struct nfsrv_descript
*nd
,
473 struct nfsrv_sock
*slp
,
477 struct nfsm_chain
*nmreq
, nmrep
;
478 struct vnode_attr preattr
, postattr
;
479 struct vnode_attr vattr
, *vap
= &vattr
;
481 struct nfs_export
*nx
;
482 struct nfs_export_options
*nxo
;
483 int error
, preattrerr
, postattrerr
, gcheck
;
484 struct nfs_filehandle nfh
;
485 struct timespec guard
= { 0, 0 };
486 kauth_action_t action
;
490 preattrerr
= postattrerr
= ENOENT
;
492 nmreq
= &nd
->nd_nmreq
;
493 nfsm_chain_null(&nmrep
);
497 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
501 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
502 if (nd
->nd_vers
== NFS_VER3
) {
503 nfsm_chain_get_32(error
, nmreq
, gcheck
);
505 nfsm_chain_get_time(error
, nmreq
, nd
->nd_vers
, guard
.tv_sec
, guard
.tv_nsec
);
510 * Save the original credential UID in case they are
511 * mapped and we need to map the IDs in the attributes.
513 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
516 * Now that we have all the fields, lets do it.
518 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
521 /* update export stats */
522 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
524 /* update active user stats */
525 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
527 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
530 if (nd
->nd_vers
== NFS_VER3
) {
531 nfsm_srv_pre_vattr_init(&preattr
);
532 error
= preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
533 if (!error
&& gcheck
&& VATTR_IS_SUPPORTED(&preattr
, va_change_time
) &&
534 (preattr
.va_change_time
.tv_sec
!= guard
.tv_sec
||
535 preattr
.va_change_time
.tv_nsec
!= guard
.tv_nsec
))
536 error
= NFSERR_NOT_SYNC
;
537 if (!preattrerr
&& !VATTR_ALL_SUPPORTED(&preattr
))
543 * If the credentials were mapped, we should
544 * map the same values in the attributes.
546 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nd
->nd_cr
) != saved_uid
)) {
548 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nd
->nd_cr
));
549 if (kauth_cred_ismember_gid(nd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
550 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nd
->nd_cr
));
553 /* Authorize the attribute changes. */
554 error
= vnode_authattr(vp
, vap
, &action
, ctx
);
556 error
= nfsrv_authorize(vp
, NULL
, action
, ctx
, nxo
, 0);
561 if (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
)) {
562 error
= mac_vnode_check_setowner(ctx
, vp
,
563 VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: -1,
564 VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: -1);
567 if (!error
&& VATTR_IS_ACTIVE(vap
, va_mode
)) {
568 error
= mac_vnode_check_setmode(ctx
, vp
, (mode_t
)vap
->va_mode
);
571 if (!error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
572 /* NOTE: File has not been open for NFS case, so NOCRED for filecred */
573 error
= mac_vnode_check_truncate(ctx
, NOCRED
, vp
);
575 /* set utimes case */
576 if (!error
&& (VATTR_IS_ACTIVE(vap
, va_access_time
) || VATTR_IS_ACTIVE(vap
, va_modify_time
))) {
577 struct timespec current_time
;
578 nanotime(¤t_time
);
580 error
= mac_vnode_check_setutimes(ctx
, vp
,
581 VATTR_IS_ACTIVE(vap
, va_access_time
) ? vap
->va_access_time
: current_time
,
582 VATTR_IS_ACTIVE(vap
, va_modify_time
) ? vap
->va_modify_time
: current_time
);
586 /* set the new attributes */
588 error
= vnode_setattr(vp
, vap
, ctx
);
590 if (!error
|| (nd
->nd_vers
== NFS_VER3
)) {
591 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
592 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
602 nd
->nd_repstat
= error
;
603 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCORFATTR(nd
->nd_vers
));
605 *mrepp
= nmrep
.nmc_mhead
;
606 nfsmout_on_status(nd
, error
);
607 if (nd
->nd_vers
== NFS_VER3
)
608 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
609 preattrerr
, &preattr
, postattrerr
, &postattr
);
611 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
613 nfsm_chain_build_done(error
, &nmrep
);
615 nfsm_chain_cleanup(&nmrep
);
626 struct nfsrv_descript
*nd
,
627 struct nfsrv_sock
*slp
,
632 vnode_t vp
, dirp
= NULL
;
633 struct nfs_filehandle dnfh
, nfh
;
634 struct nfs_export
*nx
= NULL
;
635 struct nfs_export_options
*nxo
;
636 int error
, attrerr
, dirattrerr
, isdotdot
;
639 struct vnode_attr va
, dirattr
, *vap
= &va
;
640 struct nfsm_chain
*nmreq
, nmrep
;
643 attrerr
= dirattrerr
= ENOENT
;
644 nmreq
= &nd
->nd_nmreq
;
645 nfsm_chain_null(&nmrep
);
646 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
648 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
649 nfsm_chain_get_32(error
, nmreq
, len
);
650 nfsm_name_len_check(error
, nd
, len
);
653 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
655 ni
.ni_op
= OP_LOOKUP
;
657 ni
.ni_cnd
.cn_flags
= LOCKLEAF
;
658 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
659 isdotdot
= ((len
== 2) && (ni
.ni_cnd
.cn_pnbuf
[0] == '.') && (ni
.ni_cnd
.cn_pnbuf
[1] == '.'));
661 error
= nfsrv_namei(nd
, ctx
, &ni
, &dnfh
, &dirp
, &nx
, &nxo
);
663 /* update export stats */
664 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
666 /* update active user stats */
667 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
672 if (nd
->nd_vers
== NFS_VER3
) {
673 nfsm_srv_vattr_init(&dirattr
, NFS_VER3
);
674 dirattrerr
= vnode_getattr(dirp
, &dirattr
, ctx
);
683 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, (isdotdot
? &dnfh
: NULL
), vp
, ctx
, &nfh
);
685 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
686 attrerr
= vnode_getattr(vp
, vap
, ctx
);
692 nd
->nd_repstat
= error
;
693 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
694 NFSX_POSTOPORFATTR(nd
->nd_vers
) + NFSX_POSTOPATTR(nd
->nd_vers
));
696 *mrepp
= nmrep
.nmc_mhead
;
697 if (nd
->nd_repstat
) {
698 if (nd
->nd_vers
== NFS_VER3
)
699 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, dirattrerr
, &dirattr
);
702 nfsm_chain_add_fh(error
, &nmrep
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
703 if (nd
->nd_vers
== NFS_VER3
) {
704 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, vap
);
705 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, dirattrerr
, &dirattr
);
707 error
= nfsm_chain_add_fattr(nd
, &nmrep
, vap
);
710 nfsm_chain_build_done(error
, &nmrep
);
712 nfsm_chain_cleanup(&nmrep
);
719 * nfs readlink service
723 struct nfsrv_descript
*nd
,
724 struct nfsrv_sock
*slp
,
728 int error
, mpcnt
, tlen
, len
, attrerr
;
730 struct vnode_attr vattr
;
731 struct nfs_filehandle nfh
;
732 struct nfs_export
*nx
;
733 struct nfs_export_options
*nxo
;
734 struct nfsm_chain
*nmreq
, nmrep
;
737 char uio_buf
[ UIO_SIZEOF(4) ];
738 char *uio_bufp
= &uio_buf
[0];
739 int uio_buflen
= UIO_SIZEOF(4);
743 nmreq
= &nd
->nd_nmreq
;
744 nfsm_chain_null(&nmrep
);
747 len
= NFS_MAXPATHLEN
;
749 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
752 /* get mbuf list to hold symlink path */
753 error
= nfsm_mbuf_get_list(len
, &mpath
, &mpcnt
);
756 uio_buflen
= UIO_SIZEOF(mpcnt
);
757 MALLOC(uio_bufp
, char*, uio_buflen
, M_TEMP
, M_WAITOK
);
762 auio
= uio_createwithbuffer(mpcnt
, 0, UIO_SYSSPACE
, UIO_READ
, uio_bufp
, uio_buflen
);
767 for (mp
= mpath
; mp
; mp
= mbuf_next(mp
))
768 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), mbuf_len(mp
));
770 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
773 /* update export stats */
774 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
776 /* update active user stats */
777 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
779 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
782 if (vnode_vtype(vp
) != VLNK
) {
783 if (nd
->nd_vers
== NFS_VER3
)
790 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, ctx
, nxo
, 0);
792 error
= VNOP_READLINK(vp
, auio
, ctx
);
794 if (nd
->nd_vers
== NFS_VER3
) {
795 nfsm_srv_vattr_init(&vattr
, NFS_VER3
);
796 attrerr
= vnode_getattr(vp
, &vattr
, ctx
);
808 nd
->nd_repstat
= error
;
809 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_UNSIGNED
);
811 *mrepp
= nmrep
.nmc_mhead
;
812 nfsmout_on_status(nd
, error
);
813 if (nd
->nd_vers
== NFS_VER3
)
814 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &vattr
);
815 if (error
|| nd
->nd_repstat
) {
816 nfsm_chain_build_done(error
, &nmrep
);
819 if (auio
&& (uio_resid(auio
) > 0)) {
820 len
-= uio_resid(auio
);
821 tlen
= nfsm_rndup(len
);
822 nfsm_adj(mpath
, NFS_MAXPATHLEN
-tlen
, tlen
-len
);
824 nfsm_chain_add_32(error
, &nmrep
, len
);
825 nfsm_chain_build_done(error
, &nmrep
);
827 error
= mbuf_setnext(nmrep
.nmc_mcur
, mpath
);
835 if (uio_bufp
!= &uio_buf
[0])
836 FREE(uio_bufp
, M_TEMP
);
838 nfsm_chain_cleanup(&nmrep
);
849 struct nfsrv_descript
*nd
,
850 struct nfsrv_sock
*slp
,
854 int error
, attrerr
, mreadcnt
;
855 uint32_t reqlen
, maxlen
, count
, len
, tlen
, left
;
858 struct nfs_filehandle nfh
;
859 struct nfs_export
*nx
;
860 struct nfs_export_options
*nxo
;
862 char *uio_bufp
= NULL
;
863 struct vnode_attr vattr
, *vap
= &vattr
;
866 char uio_buf
[ UIO_SIZEOF(0) ];
867 struct nfsm_chain
*nmreq
, nmrep
;
871 nmreq
= &nd
->nd_nmreq
;
872 nfsm_chain_null(&nmrep
);
876 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
878 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
880 if (nd
->nd_vers
== NFS_VER3
)
881 nfsm_chain_get_64(error
, nmreq
, off
);
883 nfsm_chain_get_32(error
, nmreq
, off
);
884 nfsm_chain_get_32(error
, nmreq
, reqlen
);
885 maxlen
= NFSRV_NDMAXDATA(nd
);
889 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
892 /* update export stats */
893 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
895 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
898 if (vnode_vtype(vp
) != VREG
) {
899 if (nd
->nd_vers
== NFS_VER3
)
902 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
906 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, ctx
, nxo
, 1)))
907 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
, nxo
, 1);
909 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
910 attrerr
= vnode_getattr(vp
, vap
, ctx
);
915 if ((u_quad_t
)off
>= vap
->va_data_size
)
917 else if (((u_quad_t
)off
+ reqlen
) > vap
->va_data_size
)
918 count
= nfsm_rndup(vap
->va_data_size
- off
);
924 /* get mbuf list to hold read data */
925 error
= nfsm_mbuf_get_list(count
, &mread
, &mreadcnt
);
927 MALLOC(uio_bufp
, char *, UIO_SIZEOF(mreadcnt
), M_TEMP
, M_WAITOK
);
929 auio
= uio_createwithbuffer(mreadcnt
, off
, UIO_SYSSPACE
,
930 UIO_READ
, uio_bufp
, UIO_SIZEOF(mreadcnt
));
931 if (!uio_bufp
|| !auio
) {
935 for (m
= mread
; m
; m
= mbuf_next(m
))
936 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), mbuf_len(m
));
937 error
= VNOP_READ(vp
, auio
, IO_NODELOCKED
, ctx
);
939 auio
= uio_createwithbuffer(0, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
947 if (!error
|| (nd
->nd_vers
== NFS_VER3
)) {
948 nfsm_srv_vattr_init(vap
, nd
->nd_vers
);
949 attrerr
= vnode_getattr(vp
, vap
, ctx
);
950 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
951 error
= attrerr
; /* NFSv2 must have attributes to return */
958 /* trim off any data not actually read */
959 len
-= uio_resid(auio
);
960 tlen
= nfsm_rndup(len
);
961 if (count
!= tlen
|| tlen
!= len
)
962 nfsm_adj(mread
, count
- tlen
, tlen
- len
);
966 nd
->nd_repstat
= error
;
967 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPORFATTR(nd
->nd_vers
) + 3 * NFSX_UNSIGNED
);
969 *mrepp
= nmrep
.nmc_mhead
;
970 nfsmout_on_status(nd
, error
);
971 if (nd
->nd_vers
== NFS_VER3
)
972 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, vap
);
973 if (error
|| nd
->nd_repstat
) {
974 nfsm_chain_build_done(error
, &nmrep
);
977 if (nd
->nd_vers
== NFS_VER3
) {
978 nfsm_chain_add_32(error
, &nmrep
, len
);
979 nfsm_chain_add_32(error
, &nmrep
, (len
< reqlen
) ? TRUE
: FALSE
);
981 error
= nfsm_chain_add_fattr(nd
, &nmrep
, vap
);
983 nfsm_chain_add_32(error
, &nmrep
, len
);
984 nfsm_chain_build_done(error
, &nmrep
);
986 error
= mbuf_setnext(nmrep
.nmc_mcur
, mread
);
990 /* update export stats */
991 NFSStatAdd64(&nx
->nx_stats
.bytes_read
, len
);
993 /* update active user stats */
994 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, len
, 0);
1000 if (uio_bufp
!= NULL
)
1001 FREE(uio_bufp
, M_TEMP
);
1003 nfsm_chain_cleanup(&nmrep
);
1011 * NFS File modification reporting
1013 * When the contents of a file are changed, a "content modified"
1014 * fsevent needs to be issued. Normally this would be done at
1015 * file close time. This is difficult for NFS because the protocol
1016 * has no "close" operation. The client sends a stream of write
1017 * requests that just stop. So we keep a hash table full of
1018 * vnodes that have been written to recently, and issue a
1019 * "content modified" fsevent only if there are no writes to
1020 * a vnode for nfsrv_fmod_pendtime milliseconds.
1022 int nfsrv_fmod_pending
; /* count of vnodes being written to */
1023 int nfsrv_fmod_pendtime
= 1000; /* msec to wait */
1024 int nfsrv_fmod_min_interval
= 100; /* msec min interval between callbacks */
1027 * This function is called via the kernel's callout
1028 * mechanism. Calls are made only when there are
1029 * vnodes pending a fsevent creation, and no more
1030 * frequently than every nfsrv_fmod_min_interval ms.
1033 nfsrv_fmod_timer(__unused
void *param0
, __unused
void *param1
)
1035 struct nfsrv_fmod_hashhead
*headp
, firehead
;
1036 struct nfsrv_fmod
*fp
, *nfp
, *pfp
;
1037 uint64_t timenow
, next_deadline
;
1038 int interval
= 0, i
, fmod_fire
;
1040 LIST_INIT(&firehead
);
1041 lck_mtx_lock(nfsrv_fmod_mutex
);
1043 clock_get_uptime(&timenow
);
1044 clock_interval_to_deadline(nfsrv_fmod_pendtime
, 1000 * 1000,
1048 * Scan all the hash chains
1051 for (i
= 0; i
< NFSRVFMODHASHSZ
; i
++) {
1053 * For each hash chain, look for an entry
1054 * that has exceeded the deadline.
1056 headp
= &nfsrv_fmod_hashtbl
[i
];
1057 LIST_FOREACH(fp
, headp
, fm_link
) {
1058 if (timenow
>= fp
->fm_deadline
)
1060 if (fp
->fm_deadline
< next_deadline
)
1061 next_deadline
= fp
->fm_deadline
;
1065 * If we have an entry that's exceeded the
1066 * deadline, then the same is true for all
1067 * following entries in the chain, since they're
1068 * sorted in time order.
1072 /* move each entry to the fire list */
1073 nfp
= LIST_NEXT(fp
, fm_link
);
1074 LIST_REMOVE(fp
, fm_link
);
1077 LIST_INSERT_AFTER(pfp
, fp
, fm_link
);
1079 LIST_INSERT_HEAD(&firehead
, fp
, fm_link
);
1086 lck_mtx_unlock(nfsrv_fmod_mutex
);
1088 * Fire off the content modified fsevent for each
1089 * entry and free it.
1091 LIST_FOREACH_SAFE(fp
, &firehead
, fm_link
, nfp
) {
1092 if (nfsrv_fsevents_enabled
) {
1093 fp
->fm_context
.vc_thread
= current_thread();
1094 add_fsevent(FSE_CONTENT_MODIFIED
, &fp
->fm_context
,
1095 FSE_ARG_VNODE
, fp
->fm_vp
,
1098 vnode_put(fp
->fm_vp
);
1099 kauth_cred_unref(&fp
->fm_context
.vc_ucred
);
1100 LIST_REMOVE(fp
, fm_link
);
1103 lck_mtx_lock(nfsrv_fmod_mutex
);
1104 nfsrv_fmod_pending
-= fmod_fire
;
1109 * If there are still pending entries, set up another
1110 * callout to handle them later. Set the timeout deadline
1111 * so that the callout happens when the oldest pending
1112 * entry is ready to send its fsevent.
1114 if (nfsrv_fmod_pending
> 0) {
1115 interval
= (next_deadline
- timenow
) / (1000 * 1000);
1116 if (interval
< nfsrv_fmod_min_interval
)
1117 interval
= nfsrv_fmod_min_interval
;
1120 nfsrv_fmod_timer_on
= interval
> 0;
1121 if (nfsrv_fmod_timer_on
)
1122 nfs_interval_timer_start(nfsrv_fmod_timer_call
, interval
);
1124 lck_mtx_unlock(nfsrv_fmod_mutex
);
1128 * When a vnode has been written to, enter it in the hash
1129 * table of vnodes pending creation of an fsevent. If the
1130 * callout timer isn't already running, schedule a callback
1131 * for nfsrv_fmod_pendtime msec from now.
1134 nfsrv_modified(vnode_t vp
, vfs_context_t ctx
)
1137 struct nfsrv_fmod
*fp
;
1138 struct nfsrv_fmod_hashhead
*head
;
1140 lck_mtx_lock(nfsrv_fmod_mutex
);
1143 * Compute the time in the future when the
1144 * content modified fsevent is to be issued.
1146 clock_interval_to_deadline(nfsrv_fmod_pendtime
, 1000 * 1000, &deadline
);
1149 * Check if there's already a file content change fsevent
1150 * pending for this vnode. If there is, update its
1151 * timestamp and make sure it's at the front of the hash chain.
1153 head
= &nfsrv_fmod_hashtbl
[NFSRVFMODHASH(vp
)];
1154 LIST_FOREACH(fp
, head
, fm_link
) {
1155 if (vp
== fp
->fm_vp
) {
1156 fp
->fm_deadline
= deadline
;
1157 if (fp
!= LIST_FIRST(head
)) {
1158 LIST_REMOVE(fp
, fm_link
);
1159 LIST_INSERT_HEAD(head
, fp
, fm_link
);
1161 lck_mtx_unlock(nfsrv_fmod_mutex
);
1167 * First content change fsevent for this vnode.
1168 * Allocate a new file mod entry and add it
1169 * on the front of the hash chain.
1171 if (vnode_get(vp
) != 0)
1173 MALLOC(fp
, struct nfsrv_fmod
*, sizeof(*fp
), M_TEMP
, M_WAITOK
);
1179 kauth_cred_ref(vfs_context_ucred(ctx
));
1180 fp
->fm_context
= *ctx
;
1181 fp
->fm_deadline
= deadline
;
1182 LIST_INSERT_HEAD(head
, fp
, fm_link
);
1185 * If added to an empty hash table, then set the
1186 * callout timer to go off after nfsrv_fmod_pendtime.
1188 nfsrv_fmod_pending
++;
1189 if (!nfsrv_fmod_timer_on
) {
1190 nfsrv_fmod_timer_on
= 1;
1191 nfs_interval_timer_start(nfsrv_fmod_timer_call
,
1192 nfsrv_fmod_pendtime
);
1195 lck_mtx_unlock(nfsrv_fmod_mutex
);
1198 #endif /* CONFIG_FSE */
1205 struct nfsrv_descript
*nd
,
1206 struct nfsrv_sock
*slp
,
1210 struct vnode_attr preattr
, postattr
;
1211 int error
, preattrerr
, postattrerr
;
1212 int ioflags
, len
, retlen
;
1214 int stable
= NFS_WRITE_FILESYNC
;
1217 struct nfs_filehandle nfh
;
1218 struct nfs_export
*nx
;
1219 struct nfs_export_options
*nxo
;
1221 char *uio_bufp
= NULL
;
1224 struct nfsm_chain
*nmreq
, nmrep
;
1226 if (nd
->nd_nmreq
.nmc_mhead
== NULL
) {
1232 preattrerr
= postattrerr
= ENOENT
;
1233 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1234 nmreq
= &nd
->nd_nmreq
;
1235 nfsm_chain_null(&nmrep
);
1239 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
1241 if (nd
->nd_vers
== NFS_VER3
) {
1242 nfsm_chain_get_64(error
, nmreq
, off
);
1243 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1244 nfsm_chain_get_32(error
, nmreq
, stable
);
1246 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1247 nfsm_chain_get_32(error
, nmreq
, off
);
1248 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1250 stable
= NFS_WRITE_UNSTABLE
;
1252 nfsm_chain_get_32(error
, nmreq
, len
);
1257 * For NFS Version 2, it is not obvious what a write of zero length
1258 * should do, but I might as well be consistent with Version 3,
1259 * which is to return ok so long as there are no permission problems.
1263 error
= nfsm_chain_trim_data(nmreq
, len
, &mlen
);
1268 if ((len
> NFSRV_MAXDATA
) || (len
< 0) || (mlen
< len
)) {
1272 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
1275 /* update export stats */
1276 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1278 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
1281 if (nd
->nd_vers
== NFS_VER3
) {
1282 nfsm_srv_pre_vattr_init(&preattr
);
1283 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
1285 if (vnode_vtype(vp
) != VREG
) {
1286 if (nd
->nd_vers
== NFS_VER3
)
1289 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1292 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
, nxo
, 1);
1297 error
= mac_vnode_check_open(ctx
, vp
, FWRITE
);
1301 /* XXXab: Do we need to do this?! */
1302 error
= mac_vnode_check_write(ctx
, vfs_context_ucred(ctx
), vp
);
1311 for (mcount
=0, m
=nmreq
->nmc_mcur
; m
; m
= mbuf_next(m
))
1312 if (mbuf_len(m
) > 0)
1314 MALLOC(uio_bufp
, char *, UIO_SIZEOF(mcount
), M_TEMP
, M_WAITOK
);
1316 auio
= uio_createwithbuffer(mcount
, off
, UIO_SYSSPACE
, UIO_WRITE
, uio_bufp
, UIO_SIZEOF(mcount
));
1317 if (!uio_bufp
|| !auio
)
1320 for (m
= nmreq
->nmc_mcur
; m
; m
= mbuf_next(m
))
1321 if ((mlen
= mbuf_len(m
)) > 0)
1322 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), mlen
);
1324 * XXX The IO_METASYNC flag indicates that all metadata (and not just
1325 * enough to ensure data integrity) mus be written to stable storage
1326 * synchronously. (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1328 if (stable
== NFS_WRITE_UNSTABLE
)
1329 ioflags
= IO_NODELOCKED
;
1330 else if (stable
== NFS_WRITE_DATASYNC
)
1331 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1333 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1335 error
= VNOP_WRITE(vp
, auio
, ioflags
, ctx
);
1336 OSAddAtomic64(1, &nfsstats
.srvvop_writes
);
1338 /* update export stats */
1339 NFSStatAdd64(&nx
->nx_stats
.bytes_written
, len
);
1341 /* update active user stats */
1342 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, len
);
1345 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CONTENT_MODIFIED
, vp
))
1346 nfsrv_modified(vp
, ctx
);
1349 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
1350 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
1351 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
1352 error
= postattrerr
; /* NFSv2 must have attributes to return */
1357 /* assemble reply */
1358 nd
->nd_repstat
= error
;
1359 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_PREOPATTR(nd
->nd_vers
) +
1360 NFSX_POSTOPORFATTR(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
+
1361 NFSX_WRITEVERF(nd
->nd_vers
));
1363 *mrepp
= nmrep
.nmc_mhead
;
1364 nfsmout_on_status(nd
, error
);
1365 if (nd
->nd_vers
== NFS_VER3
) {
1366 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1367 preattrerr
, &preattr
, postattrerr
, &postattr
);
1368 nfsmout_if(error
|| nd
->nd_repstat
);
1369 nfsm_chain_add_32(error
, &nmrep
, retlen
);
1370 /* If nfsrv_async is set, then pretend the write was FILESYNC. */
1371 if ((stable
== NFS_WRITE_UNSTABLE
) && !nfsrv_async
)
1372 nfsm_chain_add_32(error
, &nmrep
, stable
);
1374 nfsm_chain_add_32(error
, &nmrep
, NFS_WRITE_FILESYNC
);
1375 /* write verifier */
1376 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
1377 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
1379 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
1382 nfsm_chain_build_done(error
, &nmrep
);
1385 if (uio_bufp
!= NULL
)
1386 FREE(uio_bufp
, M_TEMP
);
1388 nfsm_chain_cleanup(&nmrep
);
1395 * NFS write service with write gathering support. Called when
1396 * nfsrv_wg_delay > 0.
1397 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1398 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1402 #define NWDELAYHASH(sock, f) \
1403 (&(sock)->ns_wdelayhashtbl[(*((u_int32_t *)(f))) % NFS_WDELAYHASHSIZ])
1404 /* These macros compare nfsrv_descript structures. */
1405 #define NFSW_CONTIG(o, n) \
1406 (((o)->nd_eoff >= (n)->nd_off) && nfsrv_fhmatch(&(o)->nd_fh, &(n)->nd_fh))
1408 * XXX The following is an incorrect comparison; it fails to take into account
1409 * XXX scoping of MAC labels, but we currently lack KPI for credential
1412 #define NFSW_SAMECRED(o, n) \
1413 (!bcmp((caddr_t)(o)->nd_cr, (caddr_t)(n)->nd_cr, \
1414 sizeof (struct ucred)))
1418 struct nfsrv_descript
**ndp
,
1419 struct nfsrv_sock
*slp
,
1423 struct nfsrv_descript
*nd
, *wp
, *owp
, *swp
;
1424 struct nfs_export
*nx
;
1425 struct nfs_export_options
*nxo
;
1426 struct nfsrv_wg_delayhash
*wpp
;
1428 struct vnode_attr preattr
, postattr
;
1429 int error
, mlen
, i
, ioflags
, tlen
;
1430 int preattrerr
, postattrerr
;
1434 char *uio_bufp
= NULL
;
1437 struct nfsm_chain
*nmreq
, nmrep
;
1440 preattrerr
= postattrerr
= ENOENT
;
1441 nfsm_chain_null(&nmrep
);
1448 nmreq
= &nd
->nd_nmreq
;
1449 LIST_INIT(&nd
->nd_coalesce
);
1451 nd
->nd_stable
= NFS_WRITE_FILESYNC
;
1453 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1454 nd
->nd_time
= cur_usec
+
1455 ((nd
->nd_vers
== NFS_VER3
) ? nfsrv_wg_delay_v3
: nfsrv_wg_delay
);
1457 /* Now, get the write header... */
1458 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nd
->nd_fh
.nfh_fhp
, nd
->nd_fh
.nfh_len
);
1459 /* XXX shouldn't we be checking for invalid FHs before doing any more work? */
1461 if (nd
->nd_vers
== NFS_VER3
) {
1462 nfsm_chain_get_64(error
, nmreq
, nd
->nd_off
);
1463 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1464 nfsm_chain_get_32(error
, nmreq
, nd
->nd_stable
);
1466 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1467 nfsm_chain_get_32(error
, nmreq
, nd
->nd_off
);
1468 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
1470 nd
->nd_stable
= NFS_WRITE_UNSTABLE
;
1472 nfsm_chain_get_32(error
, nmreq
, nd
->nd_len
);
1474 nd
->nd_eoff
= nd
->nd_off
+ nd
->nd_len
;
1476 if (nd
->nd_len
> 0) {
1477 error
= nfsm_chain_trim_data(nmreq
, nd
->nd_len
, &mlen
);
1483 if ((nd
->nd_len
> NFSRV_MAXDATA
) || (nd
->nd_len
< 0) || (mlen
< nd
->nd_len
)) {
1486 nd
->nd_repstat
= error
;
1487 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
1489 nd
->nd_mrep
= nmrep
.nmc_mhead
;
1490 if (nd
->nd_vers
== NFS_VER3
)
1491 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1492 preattrerr
, &preattr
, postattrerr
, &postattr
);
1494 nfsm_chain_build_done(error
, &nmrep
);
1499 * Add this entry to the hash and time queues.
1501 lck_mtx_lock(&slp
->ns_wgmutex
);
1503 wp
= slp
->ns_tq
.lh_first
;
1504 while (wp
&& wp
->nd_time
< nd
->nd_time
) {
1506 wp
= wp
->nd_tq
.le_next
;
1509 LIST_INSERT_AFTER(owp
, nd
, nd_tq
);
1511 LIST_INSERT_HEAD(&slp
->ns_tq
, nd
, nd_tq
);
1514 wpp
= NWDELAYHASH(slp
, nd
->nd_fh
.nfh_fid
);
1517 while (wp
&& !nfsrv_fhmatch(&nd
->nd_fh
, &wp
->nd_fh
)) {
1519 wp
= wp
->nd_hash
.le_next
;
1521 while (wp
&& (wp
->nd_off
< nd
->nd_off
) &&
1522 nfsrv_fhmatch(&nd
->nd_fh
, &wp
->nd_fh
)) {
1524 wp
= wp
->nd_hash
.le_next
;
1527 LIST_INSERT_AFTER(owp
, nd
, nd_hash
);
1529 * Search the hash list for overlapping entries and
1532 for(; nd
&& NFSW_CONTIG(owp
, nd
); nd
= wp
) {
1533 wp
= nd
->nd_hash
.le_next
;
1534 if (NFSW_SAMECRED(owp
, nd
))
1535 nfsrv_wg_coalesce(owp
, nd
);
1538 LIST_INSERT_HEAD(wpp
, nd
, nd_hash
);
1542 lck_mtx_lock(&slp
->ns_wgmutex
);
1546 * Now, do VNOP_WRITE()s for any one(s) that need to be done now
1547 * and generate the associated reply mbuf list(s).
1551 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1552 for (nd
= slp
->ns_tq
.lh_first
; nd
; nd
= owp
) {
1553 owp
= nd
->nd_tq
.le_next
;
1554 if (nd
->nd_time
> cur_usec
)
1558 LIST_REMOVE(nd
, nd_tq
);
1559 LIST_REMOVE(nd
, nd_hash
);
1560 nmreq
= &nd
->nd_nmreq
;
1561 preattrerr
= postattrerr
= ENOENT
;
1563 /* save the incoming uid before mapping, */
1564 /* for updating active user stats later */
1565 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1567 error
= nfsrv_fhtovp(&nd
->nd_fh
, nd
, &vp
, &nx
, &nxo
);
1569 /* update per-export stats */
1570 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1572 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
1577 if (nd
->nd_vers
== NFS_VER3
) {
1578 nfsm_srv_pre_vattr_init(&preattr
);
1579 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
1581 if (vnode_vtype(vp
) != VREG
) {
1582 if (nd
->nd_vers
== NFS_VER3
)
1585 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1590 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, ctx
, nxo
, 1);
1592 if (nd
->nd_stable
== NFS_WRITE_UNSTABLE
)
1593 ioflags
= IO_NODELOCKED
;
1594 else if (nd
->nd_stable
== NFS_WRITE_DATASYNC
)
1595 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1597 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1599 if (!error
&& ((nd
->nd_eoff
- nd
->nd_off
) > 0)) {
1600 for (i
=0, m
=nmreq
->nmc_mhead
; m
; m
= mbuf_next(m
))
1601 if (mbuf_len(m
) > 0)
1604 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
1606 auio
= uio_createwithbuffer(i
, nd
->nd_off
, UIO_SYSSPACE
,
1607 UIO_WRITE
, uio_bufp
, UIO_SIZEOF(i
));
1608 if (!uio_bufp
|| !auio
)
1611 for (m
= nmreq
->nmc_mhead
; m
; m
= mbuf_next(m
))
1612 if ((tlen
= mbuf_len(m
)) > 0)
1613 uio_addiov(auio
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(m
)), tlen
);
1614 error
= VNOP_WRITE(vp
, auio
, ioflags
, ctx
);
1615 OSAddAtomic64(1, &nfsstats
.srvvop_writes
);
1617 /* update export stats */
1618 NFSStatAdd64(&nx
->nx_stats
.bytes_written
, nd
->nd_len
);
1619 /* update active user stats */
1620 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, nd
->nd_len
);
1623 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CONTENT_MODIFIED
, vp
))
1624 nfsrv_modified(vp
, ctx
);
1628 FREE(uio_bufp
, M_TEMP
);
1633 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
1634 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
1639 * Loop around generating replies for all write rpcs that have
1640 * now been completed.
1645 nd
->nd_repstat
= error
;
1646 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
1647 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
1648 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1649 preattrerr
, &preattr
, postattrerr
, &postattr
);
1652 nd
->nd_repstat
= error
;
1653 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_PREOPATTR(nd
->nd_vers
) +
1654 NFSX_POSTOPORFATTR(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
+
1655 NFSX_WRITEVERF(nd
->nd_vers
));
1656 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
1657 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
1658 preattrerr
, &preattr
, postattrerr
, &postattr
);
1659 nfsm_chain_add_32(error
, &nmrep
, nd
->nd_len
);
1660 nfsm_chain_add_32(error
, &nmrep
, nd
->nd_stable
);
1661 /* write verifier */
1662 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
1663 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
1664 } else if (!error
) {
1665 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
1668 nfsm_chain_build_done(error
, &nmrep
);
1670 nd
->nd_mrep
= nmrep
.nmc_mhead
;
1673 * Done. Put it at the head of the timer queue so that
1674 * the final phase can return the reply.
1678 LIST_INSERT_HEAD(&slp
->ns_tq
, nd
, nd_tq
);
1680 nd
= swp
->nd_coalesce
.lh_first
;
1682 LIST_REMOVE(nd
, nd_tq
);
1686 LIST_INSERT_HEAD(&slp
->ns_tq
, swp
, nd_tq
);
1691 * Search for a reply to return.
1693 for (nd
= slp
->ns_tq
.lh_first
; nd
; nd
= nd
->nd_tq
.le_next
)
1695 LIST_REMOVE(nd
, nd_tq
);
1696 *mrepp
= nd
->nd_mrep
;
1700 slp
->ns_wgtime
= slp
->ns_tq
.lh_first
? slp
->ns_tq
.lh_first
->nd_time
: 0;
1701 lck_mtx_unlock(&slp
->ns_wgmutex
);
1704 * If we've just created a write pending gather,
1705 * start the timer to check on it soon to make sure
1706 * the write will be completed.
1708 * Add/Remove the socket in the nfsrv_sockwg queue as needed.
1710 lck_mtx_lock(nfsd_mutex
);
1711 if (slp
->ns_wgtime
) {
1712 if (slp
->ns_wgq
.tqe_next
== SLPNOLIST
) {
1713 TAILQ_INSERT_HEAD(&nfsrv_sockwg
, slp
, ns_wgq
);
1715 if (!nfsrv_wg_timer_on
) {
1716 nfsrv_wg_timer_on
= 1;
1717 nfs_interval_timer_start(nfsrv_wg_timer_call
,
1718 NFSRV_WGATHERDELAY
);
1720 } else if (slp
->ns_wgq
.tqe_next
!= SLPNOLIST
) {
1721 TAILQ_REMOVE(&nfsrv_sockwg
, slp
, ns_wgq
);
1722 slp
->ns_wgq
.tqe_next
= SLPNOLIST
;
1724 lck_mtx_unlock(nfsd_mutex
);
1730 * Coalesce the write request nd into owp. To do this we must:
1731 * - remove nd from the queues
1732 * - merge nd->nd_nmreq into owp->nd_nmreq
1733 * - update the nd_eoff and nd_stable for owp
1734 * - put nd on owp's nd_coalesce list
1737 nfsrv_wg_coalesce(struct nfsrv_descript
*owp
, struct nfsrv_descript
*nd
)
1741 struct nfsrv_descript
*p
;
1743 LIST_REMOVE(nd
, nd_hash
);
1744 LIST_REMOVE(nd
, nd_tq
);
1745 if (owp
->nd_eoff
< nd
->nd_eoff
) {
1746 overlap
= owp
->nd_eoff
- nd
->nd_off
;
1750 mbuf_adj(nd
->nd_nmreq
.nmc_mhead
, overlap
);
1751 mp
= owp
->nd_nmreq
.nmc_mhead
;
1752 while ((mpnext
= mbuf_next(mp
)))
1754 error
= mbuf_setnext(mp
, nd
->nd_nmreq
.nmc_mhead
);
1757 owp
->nd_eoff
= nd
->nd_eoff
;
1759 mbuf_freem(nd
->nd_nmreq
.nmc_mhead
);
1761 nd
->nd_nmreq
.nmc_mhead
= NULL
;
1762 nd
->nd_nmreq
.nmc_mcur
= NULL
;
1763 if (nd
->nd_stable
== NFS_WRITE_FILESYNC
)
1764 owp
->nd_stable
= NFS_WRITE_FILESYNC
;
1765 else if ((nd
->nd_stable
== NFS_WRITE_DATASYNC
) &&
1766 (owp
->nd_stable
== NFS_WRITE_UNSTABLE
))
1767 owp
->nd_stable
= NFS_WRITE_DATASYNC
;
1768 LIST_INSERT_HEAD(&owp
->nd_coalesce
, nd
, nd_tq
);
1771 * If nd had anything else coalesced into it, transfer them
1772 * to owp, otherwise their replies will never get sent.
1774 while ((p
= nd
->nd_coalesce
.lh_first
)) {
1775 LIST_REMOVE(p
, nd_tq
);
1776 LIST_INSERT_HEAD(&owp
->nd_coalesce
, p
, nd_tq
);
1782 * Scan the write gathering queues for writes that need to be
1786 nfsrv_wg_timer(__unused
void *param0
, __unused
void *param1
)
1789 uint64_t cur_usec
, next_usec
;
1791 struct nfsrv_sock
*slp
;
1792 int writes_pending
= 0;
1795 cur_usec
= (uint64_t)now
.tv_sec
* 1000000 + (uint64_t)now
.tv_usec
;
1796 next_usec
= cur_usec
+ (NFSRV_WGATHERDELAY
* 1000);
1798 lck_mtx_lock(nfsd_mutex
);
1799 TAILQ_FOREACH(slp
, &nfsrv_sockwg
, ns_wgq
) {
1800 if (slp
->ns_wgtime
) {
1802 if (slp
->ns_wgtime
<= cur_usec
) {
1803 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1804 slp
->ns_flag
|= SLP_DOWRITES
;
1805 lck_rw_done(&slp
->ns_rwlock
);
1806 nfsrv_wakenfsd(slp
);
1809 if (slp
->ns_wgtime
< next_usec
)
1810 next_usec
= slp
->ns_wgtime
;
1814 if (writes_pending
== 0) {
1815 nfsrv_wg_timer_on
= 0;
1816 lck_mtx_unlock(nfsd_mutex
);
1819 lck_mtx_unlock(nfsd_mutex
);
1822 * Return the number of msec to wait again
1824 interval
= (next_usec
- cur_usec
) / 1000;
1827 nfs_interval_timer_start(nfsrv_wg_timer_call
, interval
);
1831 * Sort the group list in increasing numerical order.
1832 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1833 * that used to be here.)
1836 nfsrv_group_sort(gid_t
*list
, int num
)
1841 /* Insertion sort. */
1842 for (i
= 1; i
< num
; i
++) {
1844 /* find correct slot for value v, moving others up */
1845 for (j
= i
; --j
>= 0 && v
< list
[j
];)
1846 list
[j
+ 1] = list
[j
];
1852 * nfs create service
1853 * now does a truncate to 0 length via. setattr if it already exists
1857 struct nfsrv_descript
*nd
,
1858 struct nfsrv_sock
*slp
,
1862 struct vnode_attr dpreattr
, dpostattr
, postattr
;
1863 struct vnode_attr va
, *vap
= &va
;
1864 struct nameidata ni
;
1865 int error
, rdev
, dpreattrerr
, dpostattrerr
, postattrerr
;
1866 int how
, exclusive_flag
;
1867 uint32_t len
= 0, cnflags
;
1868 vnode_t vp
, dvp
, dirp
;
1869 struct nfs_filehandle nfh
;
1870 struct nfs_export
*nx
= NULL
;
1871 struct nfs_export_options
*nxo
;
1873 u_char cverf
[NFSX_V3CREATEVERF
];
1875 struct nfsm_chain
*nmreq
, nmrep
;
1878 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
1879 nmreq
= &nd
->nd_nmreq
;
1880 nfsm_chain_null(&nmrep
);
1881 vp
= dvp
= dirp
= NULL
;
1883 ni
.ni_cnd
.cn_nameiop
= 0;
1886 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
1888 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
1889 nfsm_chain_get_32(error
, nmreq
, len
);
1890 nfsm_name_len_check(error
, nd
, len
);
1893 ni
.ni_cnd
.cn_nameiop
= CREATE
;
1897 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
1898 ni
.ni_cnd
.cn_ndp
= &ni
;
1900 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
1902 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
1904 /* update export stats */
1905 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
1907 /* update active user stats */
1908 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
1912 if (nd
->nd_vers
== NFS_VER3
) {
1913 nfsm_srv_pre_vattr_init(&dpreattr
);
1914 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
1922 ni
.ni_cnd
.cn_nameiop
= 0;
1930 if (nd
->nd_vers
== NFS_VER3
) {
1931 nfsm_chain_get_32(error
, nmreq
, how
);
1934 case NFS_CREATE_GUARDED
:
1939 case NFS_CREATE_UNCHECKED
:
1940 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
1942 case NFS_CREATE_EXCLUSIVE
:
1943 nfsm_chain_get_opaque(error
, nmreq
, NFSX_V3CREATEVERF
, cverf
);
1946 VATTR_SET(vap
, va_mode
, 0);
1949 VATTR_SET(vap
, va_type
, VREG
);
1953 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
1955 v_type
= vap
->va_type
;
1958 VATTR_SET(vap
, va_type
, v_type
);
1964 rdev
= vap
->va_data_size
;
1965 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1974 * If it doesn't exist, create it
1975 * otherwise just truncate to 0 length
1976 * should I set the mode too ??
1979 kauth_acl_t xacl
= NULL
;
1981 /* authorize before creating */
1982 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
1984 /* construct ACL and handle inheritance */
1986 error
= kauth_acl_inherit(dvp
,
1992 if (!error
&& xacl
!= NULL
)
1993 VATTR_SET(vap
, va_acl
, xacl
);
1995 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1996 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
1998 * Server policy is to alway use the mapped rpc credential for
1999 * file system object creation. This has the nice side effect of
2000 * enforcing BSD creation semantics
2002 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
2003 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
2005 /* validate new-file security information */
2007 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
2010 error
= vn_authorize_create(dvp
, &ni
.ni_cnd
, vap
, ctx
, NULL
);
2015 if (vap
->va_type
== VREG
|| vap
->va_type
== VSOCK
) {
2018 error
= VNOP_CREATE(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
2020 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
2022 * If some of the requested attributes weren't handled by the VNOP,
2023 * use our fallback code.
2025 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
2028 kauth_acl_free(xacl
);
2031 if (exclusive_flag
) {
2034 bcopy(cverf
, (caddr_t
)&vap
->va_access_time
,
2036 VATTR_SET_ACTIVE(vap
, va_access_time
);
2037 // skip authorization, as this is an
2038 // NFS internal implementation detail.
2039 error
= vnode_setattr(vp
, vap
, ctx
);
2043 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_CREATE_FILE
, vp
)) {
2044 add_fsevent(FSE_CREATE_FILE
, ctx
,
2051 } else if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
||
2052 vap
->va_type
== VFIFO
) {
2053 if (vap
->va_type
== VCHR
&& rdev
== (int)0xffffffff)
2054 VATTR_SET(vap
, va_type
, VFIFO
);
2055 if (vap
->va_type
!= VFIFO
) {
2056 error
= suser(nd
->nd_cr
, NULL
);
2059 VATTR_SET(vap
, va_rdev
, (dev_t
)rdev
);
2061 error
= VNOP_MKNOD(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
2064 kauth_acl_free(xacl
);
2073 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
2075 ni
.ni_op
= OP_LOOKUP
;
2077 ni
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
2078 ni
.ni_cnd
.cn_context
= ctx
;
2079 ni
.ni_startdir
= dvp
;
2081 ni
.ni_rootdir
= rootvnode
;
2082 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
2083 while ((error
= lookup(&ni
)) == ERECYCLE
) {
2084 ni
.ni_cnd
.cn_flags
= cnflags
;
2085 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
2086 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
2089 if (ni
.ni_cnd
.cn_flags
& ISSYMLINK
)
2098 * nameidone has to happen before we vnode_put(dvp)
2099 * since it may need to release the fs_nodelock on the dvp
2102 ni
.ni_cnd
.cn_nameiop
= 0;
2107 * nameidone has to happen before we vnode_put(dvp)
2108 * since it may need to release the fs_nodelock on the dvp
2111 ni
.ni_cnd
.cn_nameiop
= 0;
2116 if (!error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2117 /* NOTE: File has not been open for NFS case, so NOCRED for filecred */
2118 error
= mac_vnode_check_truncate(ctx
, NOCRED
, vp
);
2123 if (!error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2124 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
,
2127 tempsize
= vap
->va_data_size
;
2129 VATTR_SET(vap
, va_data_size
, tempsize
);
2130 error
= vnode_setattr(vp
, vap
, ctx
);
2135 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, NULL
, vp
, ctx
, &nfh
);
2137 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
2138 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
2139 if (nd
->nd_vers
== NFS_VER2
)
2140 error
= postattrerr
;
2146 if (nd
->nd_vers
== NFS_VER3
) {
2147 if (exclusive_flag
&& !error
&&
2148 bcmp(cverf
, &postattr
.va_access_time
, NFSX_V3CREATEVERF
))
2150 nfsm_srv_vattr_init(&dpostattr
, NFS_VER3
);
2151 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2157 /* assemble reply */
2158 nd
->nd_repstat
= error
;
2159 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
2160 NFSX_FATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
2162 *mrepp
= nmrep
.nmc_mhead
;
2163 nfsmout_on_status(nd
, error
);
2164 if (nd
->nd_vers
== NFS_VER3
) {
2165 if (!nd
->nd_repstat
) {
2166 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2167 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
2169 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2170 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2172 nfsm_chain_add_fh(error
, &nmrep
, NFS_VER2
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2174 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
2177 nfsm_chain_build_done(error
, &nmrep
);
2178 if (ni
.ni_cnd
.cn_nameiop
) {
2180 * nameidone has to happen before we vnode_put(dvp)
2181 * since it may need to release the fs_nodelock on the dvp
2192 nfsm_chain_cleanup(&nmrep
);
2199 * nfs v3 mknod service
2203 struct nfsrv_descript
*nd
,
2204 struct nfsrv_sock
*slp
,
2208 struct vnode_attr dpreattr
, dpostattr
, postattr
;
2209 struct vnode_attr va
, *vap
= &va
;
2210 struct nameidata ni
;
2211 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
2212 uint32_t len
= 0, cnflags
;
2213 u_int32_t major
= 0, minor
= 0;
2216 vnode_t vp
, dvp
, dirp
;
2217 struct nfs_filehandle nfh
;
2218 struct nfs_export
*nx
= NULL
;
2219 struct nfs_export_options
*nxo
;
2221 kauth_acl_t xacl
= NULL
;
2222 struct nfsm_chain
*nmreq
, nmrep
;
2225 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
2226 nmreq
= &nd
->nd_nmreq
;
2227 nfsm_chain_null(&nmrep
);
2228 vp
= dvp
= dirp
= NULL
;
2229 ni
.ni_cnd
.cn_nameiop
= 0;
2231 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2233 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2234 nfsm_chain_get_32(error
, nmreq
, len
);
2235 nfsm_name_len_check(error
, nd
, len
);
2238 ni
.ni_cnd
.cn_nameiop
= CREATE
;
2242 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
2243 ni
.ni_cnd
.cn_ndp
= &ni
;
2244 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
2246 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
2248 /* update export stats */
2249 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
2251 /* update active user stats */
2252 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
2256 nfsm_srv_pre_vattr_init(&dpreattr
);
2257 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
2260 ni
.ni_cnd
.cn_nameiop
= 0;
2267 nfsm_chain_get_32(error
, nmreq
, nvtype
);
2269 vtyp
= nfstov_type(nvtype
, NFS_VER3
);
2270 if (!error
&& (vtyp
!= VCHR
) && (vtyp
!= VBLK
) && (vtyp
!= VSOCK
) && (vtyp
!= VFIFO
)) {
2271 error
= NFSERR_BADTYPE
;
2276 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
2277 if ((vtyp
== VCHR
) || (vtyp
== VBLK
)) {
2278 nfsm_chain_get_32(error
, nmreq
, major
);
2279 nfsm_chain_get_32(error
, nmreq
, minor
);
2281 VATTR_SET(vap
, va_rdev
, makedev(major
, minor
));
2286 * If it doesn't exist, create it.
2292 VATTR_SET(vap
, va_type
, vtyp
);
2294 /* authorize before creating */
2295 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
2297 /* construct ACL and handle inheritance */
2299 error
= kauth_acl_inherit(dvp
,
2305 if (!error
&& xacl
!= NULL
)
2306 VATTR_SET(vap
, va_acl
, xacl
);
2308 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
2309 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
2311 * Server policy is to alway use the mapped rpc credential for
2312 * file system object creation. This has the nice side effect of
2313 * enforcing BSD creation semantics
2315 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
2316 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
2318 /* validate new-file security information */
2320 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
2322 error
= vn_authorize_create(dvp
, &ni
.ni_cnd
, vap
, ctx
, NULL
);
2329 if (vtyp
== VSOCK
) {
2330 error
= VNOP_CREATE(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
2332 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
2334 * If some of the requested attributes weren't handled by the VNOP,
2335 * use our fallback code.
2337 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
2339 if (vtyp
!= VFIFO
&& (error
= suser(nd
->nd_cr
, (u_short
*)0)))
2341 if ((error
= VNOP_MKNOD(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
)))
2348 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
2350 ni
.ni_op
= OP_LOOKUP
;
2352 ni
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
2353 ni
.ni_cnd
.cn_context
= vfs_context_current();
2354 ni
.ni_startdir
= dvp
;
2356 ni
.ni_rootdir
= rootvnode
;
2357 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
2358 while ((error
= lookup(&ni
)) == ERECYCLE
) {
2359 ni
.ni_cnd
.cn_flags
= cnflags
;
2360 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
2361 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
2365 if (ni
.ni_cnd
.cn_flags
& ISSYMLINK
)
2371 kauth_acl_free(xacl
);
2374 * nameidone has to happen before we vnode_put(dvp)
2375 * since it may need to release the fs_nodelock on the dvp
2378 ni
.ni_cnd
.cn_nameiop
= 0;
2384 error
= nfsrv_vptofh(nx
, NFS_VER3
, NULL
, vp
, ctx
, &nfh
);
2386 nfsm_srv_vattr_init(&postattr
, NFS_VER3
);
2387 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
2395 nfsm_srv_vattr_init(&dpostattr
, NFS_VER3
);
2396 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2401 /* assemble reply */
2402 nd
->nd_repstat
= error
;
2403 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(NFS_VER3
, &nfh
) +
2404 NFSX_POSTOPATTR(NFS_VER3
) + NFSX_WCCDATA(NFS_VER3
));
2406 *mrepp
= nmrep
.nmc_mhead
;
2407 nfsmout_on_status(nd
, error
);
2408 if (!nd
->nd_repstat
) {
2409 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2410 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
2412 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2413 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2415 nfsm_chain_build_done(error
, &nmrep
);
2416 if (ni
.ni_cnd
.cn_nameiop
) {
2418 * nameidone has to happen before we vnode_put(dvp)
2419 * since it may need to release the fs_nodelock on the dvp
2434 nfsm_chain_cleanup(&nmrep
);
2441 * nfs remove service
2445 struct nfsrv_descript
*nd
,
2446 struct nfsrv_sock
*slp
,
2450 struct nameidata ni
;
2451 int error
, dpreattrerr
, dpostattrerr
;
2454 vnode_t vp
, dvp
, dirp
= NULL
;
2455 struct vnode_attr dpreattr
, dpostattr
;
2456 struct nfs_filehandle nfh
;
2457 struct nfs_export
*nx
= NULL
;
2458 struct nfs_export_options
*nxo
;
2459 struct nfsm_chain
*nmreq
, nmrep
;
2462 dpreattrerr
= dpostattrerr
= ENOENT
;
2463 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2464 dvp
= vp
= dirp
= NULL
;
2465 nmreq
= &nd
->nd_nmreq
;
2466 nfsm_chain_null(&nmrep
);
2468 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
2469 nfsm_chain_get_32(error
, nmreq
, len
);
2470 nfsm_name_len_check(error
, nd
, len
);
2473 ni
.ni_cnd
.cn_nameiop
= DELETE
;
2475 ni
.ni_op
= OP_UNLINK
;
2477 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
2478 ni
.ni_cnd
.cn_ndp
= &ni
;
2479 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
2481 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
2483 /* update export stats */
2484 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
2486 /* update active user stats */
2487 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
2491 if (nd
->nd_vers
== NFS_VER3
) {
2492 nfsm_srv_pre_vattr_init(&dpreattr
);
2493 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
2504 if (vnode_vtype(vp
) == VDIR
)
2505 error
= EPERM
; /* POSIX */
2506 else if (vnode_isvroot(vp
))
2508 * The root of a mounted filesystem cannot be deleted.
2512 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
, nxo
, 0);
2515 error
= vn_authorize_unlink(dvp
, vp
, &ni
.ni_cnd
, ctx
, NULL
);
2526 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_DELETE
, dvp
)) {
2528 if ((path
= get_pathbuff()) && !vn_getpath(vp
, path
, &plen
)) {
2529 get_fse_info(vp
, &finfo
, ctx
);
2531 release_pathbuff(path
);
2536 error
= VNOP_REMOVE(dvp
, vp
, &ni
.ni_cnd
, 0, ctx
);
2541 add_fsevent(FSE_DELETE
, ctx
,
2542 FSE_ARG_STRING
, plen
, path
,
2543 FSE_ARG_FINFO
, &finfo
,
2545 release_pathbuff(path
);
2551 * nameidone has to happen before we vnode_put(dvp)
2552 * since it may need to release the fs_nodelock on the dvp
2562 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
2563 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
2567 /* assemble reply */
2568 nd
->nd_repstat
= error
;
2569 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
2571 *mrepp
= nmrep
.nmc_mhead
;
2572 nfsmout_on_status(nd
, error
);
2573 if (nd
->nd_vers
== NFS_VER3
)
2574 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
2575 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
2577 nfsm_chain_build_done(error
, &nmrep
);
2579 nfsm_chain_cleanup(&nmrep
);
2586 * nfs rename service
2590 struct nfsrv_descript
*nd
,
2591 struct nfsrv_sock
*slp
,
2595 kauth_cred_t saved_cred
= NULL
;
2598 uint32_t fromlen
, tolen
;
2599 int fdpreattrerr
, fdpostattrerr
;
2600 int tdpreattrerr
, tdpostattrerr
;
2601 char *frompath
= NULL
, *topath
= NULL
;
2602 struct nameidata fromni
, toni
;
2603 vnode_t fvp
, tvp
, tdvp
, fdvp
, fdirp
, tdirp
;
2604 struct vnode_attr fdpreattr
, fdpostattr
;
2605 struct vnode_attr tdpreattr
, tdpostattr
;
2606 struct nfs_filehandle fnfh
, tnfh
;
2607 struct nfs_export
*fnx
, *tnx
;
2608 struct nfs_export_options
*fnxo
, *tnxo
;
2609 enum vtype fvtype
, tvtype
;
2610 int holding_mntlock
;
2612 struct nfsm_chain
*nmreq
, nmrep
;
2613 char *from_name
, *to_name
;
2615 int from_len
=0, to_len
=0;
2616 fse_info from_finfo
, to_finfo
;
2618 u_char didstats
= 0;
2622 fdpreattrerr
= fdpostattrerr
= ENOENT
;
2623 tdpreattrerr
= tdpostattrerr
= ENOENT
;
2624 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
2625 fromlen
= tolen
= 0;
2626 frompath
= topath
= NULL
;
2627 fdirp
= tdirp
= NULL
;
2628 nmreq
= &nd
->nd_nmreq
;
2629 nfsm_chain_null(&nmrep
);
2632 * these need to be set before calling any code
2633 * that they may take us out through the error path.
2635 holding_mntlock
= 0;
2640 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, fnfh
.nfh_fhp
, fnfh
.nfh_len
);
2641 nfsm_chain_get_32(error
, nmreq
, fromlen
);
2642 nfsm_name_len_check(error
, nd
, fromlen
);
2644 error
= nfsm_chain_get_path_namei(nmreq
, fromlen
, &fromni
);
2646 frompath
= fromni
.ni_cnd
.cn_pnbuf
;
2648 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, tnfh
.nfh_fhp
, tnfh
.nfh_len
);
2649 nfsm_chain_get_32(error
, nmreq
, tolen
);
2650 nfsm_name_len_check(error
, nd
, tolen
);
2652 error
= nfsm_chain_get_path_namei(nmreq
, tolen
, &toni
);
2654 topath
= toni
.ni_cnd
.cn_pnbuf
;
2657 * Remember our original uid so that we can reset cr_uid before
2658 * the second nfsrv_namei() call, in case it is remapped.
2660 saved_cred
= nd
->nd_cr
;
2661 kauth_cred_ref(saved_cred
);
2663 fromni
.ni_cnd
.cn_nameiop
= DELETE
;
2665 fromni
.ni_op
= OP_UNLINK
;
2667 fromni
.ni_cnd
.cn_flags
= WANTPARENT
;
2669 fromni
.ni_cnd
.cn_pnbuf
= frompath
;
2671 fromni
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2672 fromni
.ni_cnd
.cn_flags
|= HASBUF
;
2673 fromni
.ni_cnd
.cn_ndp
= &fromni
;
2675 error
= nfsrv_namei(nd
, ctx
, &fromni
, &fnfh
, &fdirp
, &fnx
, &fnxo
);
2678 fdvp
= fromni
.ni_dvp
;
2682 if (nd
->nd_vers
== NFS_VER3
) {
2683 nfsm_srv_pre_vattr_init(&fdpreattr
);
2684 fdpreattrerr
= vnode_getattr(fdirp
, &fdpreattr
, ctx
);
2690 fvtype
= vnode_vtype(fvp
);
2692 /* reset credential if it was remapped */
2693 if (nd
->nd_cr
!= saved_cred
) {
2694 kauth_cred_ref(saved_cred
);
2695 kauth_cred_unref(&nd
->nd_cr
);
2696 ctx
->vc_ucred
= nd
->nd_cr
= saved_cred
;
2699 toni
.ni_cnd
.cn_nameiop
= RENAME
;
2701 toni
.ni_op
= OP_RENAME
;
2703 toni
.ni_cnd
.cn_flags
= WANTPARENT
;
2705 toni
.ni_cnd
.cn_pnbuf
= topath
;
2707 toni
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2708 toni
.ni_cnd
.cn_flags
|= HASBUF
;
2709 toni
.ni_cnd
.cn_ndp
= &toni
;
2712 toni
.ni_cnd
.cn_flags
|= WILLBEDIR
;
2715 error
= nfsrv_namei(nd
, ctx
, &toni
, &tnfh
, &tdirp
, &tnx
, &tnxo
);
2718 * Translate error code for rename("dir1", "dir2/.").
2720 if (error
== EISDIR
&& fvtype
== VDIR
) {
2721 if (nd
->nd_vers
== NFS_VER3
)
2732 /* update export stats once only */
2734 /* update export stats */
2735 NFSStatAdd64(&tnx
->nx_stats
.ops
, 1);
2737 /* update active user stats */
2738 nfsrv_update_user_stat(tnx
, nd
, saved_uid
, 1, 0, 0);
2744 if (nd
->nd_vers
== NFS_VER3
) {
2745 nfsm_srv_pre_vattr_init(&tdpreattr
);
2746 tdpreattrerr
= vnode_getattr(tdirp
, &tdpreattr
, ctx
);
2754 tvtype
= vnode_vtype(tvp
);
2756 if (fvtype
== VDIR
&& tvtype
!= VDIR
) {
2757 if (nd
->nd_vers
== NFS_VER3
)
2762 } else if (fvtype
!= VDIR
&& tvtype
== VDIR
) {
2763 if (nd
->nd_vers
== NFS_VER3
)
2769 if (tvtype
== VDIR
&& vnode_mountedhere(tvp
)) {
2770 if (nd
->nd_vers
== NFS_VER3
)
2778 if (nd
->nd_vers
== NFS_VER3
)
2788 * If tvp is a directory and not the same as fdvp, or tdvp is not the same as fdvp,
2789 * the node is moving between directories and we need rights to remove from the
2790 * old and add to the new.
2792 * If tvp already exists and is not a directory, we need to be allowed to delete it.
2794 * Note that we do not inherit when renaming. XXX this needs to be revisited to
2795 * implement the deferred-inherit bit.
2801 if ((tvp
!= NULL
) && vnode_isdir(tvp
)) {
2804 } else if (tdvp
!= fdvp
) {
2808 /* moving out of fdvp, must have delete rights */
2809 if ((error
= nfsrv_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
, ctx
, fnxo
, 0)) != 0)
2811 /* moving into tdvp or tvp, must have rights to add */
2812 if ((error
= nfsrv_authorize(((tvp
!= NULL
) && vnode_isdir(tvp
)) ? tvp
: tdvp
,
2814 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2815 ctx
, tnxo
, 0)) != 0)
2818 /* node staying in same directory, must be allowed to add new name */
2819 if ((error
= nfsrv_authorize(fdvp
, NULL
,
2820 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2821 ctx
, fnxo
, 0)) != 0)
2824 /* overwriting tvp */
2825 if ((tvp
!= NULL
) && !vnode_isdir(tvp
) &&
2826 ((error
= nfsrv_authorize(tvp
, tdvp
, KAUTH_VNODE_DELETE
, ctx
, tnxo
, 0)) != 0))
2830 ((error
= vn_authorize_rename(fdvp
, fvp
, &fromni
.ni_cnd
, tdvp
, tvp
, &toni
.ni_cnd
, ctx
, NULL
)) != 0)) {
2835 /* XXX more checks? */
2838 /* authorization denied */
2843 if ((vnode_mount(fvp
) != vnode_mount(tdvp
)) ||
2844 (tvp
&& (vnode_mount(fvp
) != vnode_mount(tvp
)))) {
2845 if (nd
->nd_vers
== NFS_VER3
)
2852 * The following edge case is caught here:
2853 * (to cannot be a descendent of from)
2866 if (tdvp
->v_parent
== fvp
) {
2867 if (nd
->nd_vers
== NFS_VER3
)
2873 if (fvtype
== VDIR
&& vnode_mountedhere(fvp
)) {
2874 if (nd
->nd_vers
== NFS_VER3
)
2881 * If source is the same as the destination (that is the
2882 * same vnode) then there is nothing to do...
2883 * EXCEPT if the underlying file system supports case
2884 * insensitivity and is case preserving. In this case
2885 * the file system needs to handle the special case of
2886 * getting the same vnode as target (fvp) and source (tvp).
2888 * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE
2889 * and _PC_CASE_PRESERVING can have this exception, and they need to
2890 * handle the special case of getting the same vnode as target and
2891 * source. NOTE: Then the target is unlocked going into vnop_rename,
2892 * so not to cause locking problems. There is a single reference on tvp.
2894 * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE
2895 * that correct behaviour then is just to remove the source (link)
2897 if ((fvp
== tvp
) && (fdvp
== tdvp
)) {
2898 if (fromni
.ni_cnd
.cn_namelen
== toni
.ni_cnd
.cn_namelen
&&
2899 !bcmp(fromni
.ni_cnd
.cn_nameptr
, toni
.ni_cnd
.cn_nameptr
,
2900 fromni
.ni_cnd
.cn_namelen
)) {
2905 if (holding_mntlock
&& vnode_mount(fvp
) != locked_mp
) {
2907 * we're holding a reference and lock
2908 * on locked_mp, but it no longer matches
2909 * what we want to do... so drop our hold
2911 mount_unlock_renames(locked_mp
);
2912 mount_drop(locked_mp
, 0);
2913 holding_mntlock
= 0;
2915 if (tdvp
!= fdvp
&& fvtype
== VDIR
) {
2917 * serialize renames that re-shape
2918 * the tree... if holding_mntlock is
2919 * set, then we're ready to go...
2921 * first need to drop the iocounts
2922 * we picked up, second take the
2923 * lock to serialize the access,
2924 * then finally start the lookup
2925 * process over with the lock held
2927 if (!holding_mntlock
) {
2929 * need to grab a reference on
2930 * the mount point before we
2931 * drop all the iocounts... once
2932 * the iocounts are gone, the mount
2935 locked_mp
= vnode_mount(fvp
);
2936 mount_ref(locked_mp
, 0);
2938 /* make a copy of to path to pass to nfsrv_namei() again */
2939 MALLOC_ZONE(topath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2941 bcopy(toni
.ni_cnd
.cn_pnbuf
, topath
, tolen
+ 1);
2944 * nameidone has to happen before we vnode_put(tdvp)
2945 * since it may need to release the fs_nodelock on the tdvp
2953 /* make a copy of from path to pass to nfsrv_namei() again */
2954 MALLOC_ZONE(frompath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2956 bcopy(fromni
.ni_cnd
.cn_pnbuf
, frompath
, fromlen
+ 1);
2959 * nameidone has to happen before we vnode_put(fdvp)
2960 * since it may need to release the fs_nodelock on the fdvp
2975 mount_lock_renames(locked_mp
);
2976 holding_mntlock
= 1;
2981 fdpreattrerr
= tdpreattrerr
= ENOENT
;
2983 if (!topath
|| !frompath
) {
2984 /* we couldn't allocate a path, so bail */
2989 /* reset credential if it was remapped */
2990 if (nd
->nd_cr
!= saved_cred
) {
2991 kauth_cred_ref(saved_cred
);
2992 kauth_cred_unref(&nd
->nd_cr
);
2993 ctx
->vc_ucred
= nd
->nd_cr
= saved_cred
;
3000 * when we dropped the iocounts to take
3001 * the lock, we allowed the identity of
3002 * the various vnodes to change... if they did,
3003 * we may no longer be dealing with a rename
3004 * that reshapes the tree... once we're holding
3005 * the iocounts, the vnodes can't change type
3006 * so we're free to drop the lock at this point
3009 if (holding_mntlock
) {
3010 mount_unlock_renames(locked_mp
);
3011 mount_drop(locked_mp
, 0);
3012 holding_mntlock
= 0;
3016 // save these off so we can later verify that fvp is the same
3018 oname
= fvp
->v_name
;
3019 oparent
= fvp
->v_parent
;
3022 * If generating an fsevent, then
3023 * stash any pre-rename info we may need.
3026 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_RENAME
, fvp
)) {
3027 int from_truncated
= 0, to_truncated
= 0;
3029 get_fse_info(fvp
, &from_finfo
, ctx
);
3031 get_fse_info(tvp
, &to_finfo
, ctx
);
3033 from_name
= get_pathbuff();
3035 from_len
= safe_getpath(fdvp
, fromni
.ni_cnd
.cn_nameptr
, from_name
, MAXPATHLEN
, &from_truncated
);
3038 to_name
= from_name
? get_pathbuff() : NULL
;
3040 to_len
= safe_getpath(tdvp
, toni
.ni_cnd
.cn_nameptr
, to_name
, MAXPATHLEN
, &to_truncated
);
3043 if (from_truncated
|| to_truncated
) {
3044 from_finfo
.mode
|= FSE_TRUNCATED_PATH
;
3051 #else /* CONFIG_FSE */
3054 #endif /* CONFIG_FSE */
3056 error
= VNOP_RENAME(fromni
.ni_dvp
, fromni
.ni_vp
, &fromni
.ni_cnd
,
3057 toni
.ni_dvp
, toni
.ni_vp
, &toni
.ni_cnd
, ctx
);
3059 * fix up name & parent pointers. note that we first
3060 * check that fvp has the same name/parent pointers it
3061 * had before the rename call... this is a 'weak' check
3064 if (oname
== fvp
->v_name
&& oparent
== fvp
->v_parent
) {
3066 update_flags
= VNODE_UPDATE_NAME
;
3068 update_flags
|= VNODE_UPDATE_PARENT
;
3069 vnode_update_identity(fvp
, tdvp
, toni
.ni_cnd
.cn_nameptr
,
3070 toni
.ni_cnd
.cn_namelen
, toni
.ni_cnd
.cn_hash
, update_flags
);
3074 * If the rename is OK and we've got the paths
3075 * then add an fsevent.
3078 if (nfsrv_fsevents_enabled
&& !error
&& from_name
&& to_name
) {
3080 add_fsevent(FSE_RENAME
, ctx
,
3081 FSE_ARG_STRING
, from_len
, from_name
,
3082 FSE_ARG_FINFO
, &from_finfo
,
3083 FSE_ARG_STRING
, to_len
, to_name
,
3084 FSE_ARG_FINFO
, &to_finfo
,
3087 add_fsevent(FSE_RENAME
, ctx
,
3088 FSE_ARG_STRING
, from_len
, from_name
,
3089 FSE_ARG_FINFO
, &from_finfo
,
3090 FSE_ARG_STRING
, to_len
, to_name
,
3095 release_pathbuff(from_name
);
3097 release_pathbuff(to_name
);
3098 #endif /* CONFIG_FSE */
3099 from_name
= to_name
= NULL
;
3102 if (holding_mntlock
) {
3103 mount_unlock_renames(locked_mp
);
3104 mount_drop(locked_mp
, 0);
3105 holding_mntlock
= 0;
3109 * nameidone has to happen before we vnode_put(tdvp)
3110 * since it may need to release the fs_nodelock on the tdvp
3121 * nameidone has to happen before we vnode_put(fdvp)
3122 * since it may need to release the fs_nodelock on the fdvp
3133 nfsm_srv_vattr_init(&fdpostattr
, nd
->nd_vers
);
3134 fdpostattrerr
= vnode_getattr(fdirp
, &fdpostattr
, ctx
);
3139 nfsm_srv_vattr_init(&tdpostattr
, nd
->nd_vers
);
3140 tdpostattrerr
= vnode_getattr(tdirp
, &tdpostattr
, ctx
);
3146 /* assemble reply */
3147 nd
->nd_repstat
= error
;
3148 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 2 * NFSX_WCCDATA(nd
->nd_vers
));
3150 *mrepp
= nmrep
.nmc_mhead
;
3151 nfsmout_on_status(nd
, error
);
3152 if (nd
->nd_vers
== NFS_VER3
) {
3153 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3154 fdpreattrerr
, &fdpreattr
, fdpostattrerr
, &fdpostattr
);
3155 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3156 tdpreattrerr
, &tdpreattr
, tdpostattrerr
, &tdpostattr
);
3159 nfsm_chain_build_done(error
, &nmrep
);
3160 if (holding_mntlock
) {
3161 mount_unlock_renames(locked_mp
);
3162 mount_drop(locked_mp
, 0);
3166 * nameidone has to happen before we vnode_put(tdvp)
3167 * since it may need to release the fs_nodelock on the tdvp
3177 * nameidone has to happen before we vnode_put(fdvp)
3178 * since it may need to release the fs_nodelock on the fdvp
3191 FREE_ZONE(frompath
, MAXPATHLEN
, M_NAMEI
);
3193 FREE_ZONE(topath
, MAXPATHLEN
, M_NAMEI
);
3195 kauth_cred_unref(&saved_cred
);
3197 nfsm_chain_cleanup(&nmrep
);
3208 struct nfsrv_descript
*nd
,
3209 struct nfsrv_sock
*slp
,
3213 struct nameidata ni
;
3214 int error
, dpreattrerr
, dpostattrerr
, attrerr
;
3216 vnode_t vp
, xp
, dvp
, dirp
;
3217 struct vnode_attr dpreattr
, dpostattr
, attr
;
3218 struct nfs_filehandle nfh
, dnfh
;
3219 struct nfs_export
*nx
;
3220 struct nfs_export_options
*nxo
;
3221 struct nfsm_chain
*nmreq
, nmrep
;
3224 dpreattrerr
= dpostattrerr
= attrerr
= ENOENT
;
3225 vp
= xp
= dvp
= dirp
= NULL
;
3226 nmreq
= &nd
->nd_nmreq
;
3227 nfsm_chain_null(&nmrep
);
3229 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3230 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
3231 nfsm_chain_get_32(error
, nmreq
, len
);
3232 nfsm_name_len_check(error
, nd
, len
);
3234 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
3237 /* update export stats */
3238 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3240 /* update active user stats */
3241 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
3243 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
3246 /* we're not allowed to link to directories... */
3247 if (vnode_vtype(vp
) == VDIR
) {
3248 error
= EPERM
; /* POSIX */
3252 /* ...or to anything that kauth doesn't want us to (eg. immutable items) */
3253 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LINKTARGET
, ctx
, nxo
, 0)) != 0)
3256 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3260 ni
.ni_cnd
.cn_flags
= LOCKPARENT
;
3261 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3263 error
= nfsrv_namei(nd
, ctx
, &ni
, &dnfh
, &dirp
, &nx
, &nxo
);
3265 if (nd
->nd_vers
== NFS_VER3
) {
3266 nfsm_srv_pre_vattr_init(&dpreattr
);
3267 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3280 else if (vnode_mount(vp
) != vnode_mount(dvp
))
3283 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
3287 error
= mac_vnode_check_link(ctx
, dvp
, vp
, &ni
.ni_cnd
);
3293 error
= VNOP_LINK(vp
, dvp
, &ni
.ni_cnd
, ctx
);
3296 if (nfsrv_fsevents_enabled
&& !error
&& need_fsevent(FSE_CREATE_FILE
, dvp
)) {
3297 char *target_path
= NULL
;
3298 int plen
, truncated
=0;
3301 /* build the path to the new link file */
3302 target_path
= get_pathbuff();
3304 plen
= safe_getpath(dvp
, ni
.ni_cnd
.cn_nameptr
, target_path
, MAXPATHLEN
, &truncated
);
3306 if (get_fse_info(vp
, &finfo
, ctx
) == 0) {
3308 finfo
.mode
|= FSE_TRUNCATED_PATH
;
3310 add_fsevent(FSE_CREATE_FILE
, ctx
,
3311 FSE_ARG_STRING
, plen
, target_path
,
3312 FSE_ARG_FINFO
, &finfo
,
3316 release_pathbuff(target_path
);
3322 * nameidone has to happen before we vnode_put(dvp)
3323 * since it may need to release the fs_nodelock on the dvp
3331 if (nd
->nd_vers
== NFS_VER3
) {
3332 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
3333 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
3336 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3337 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3345 /* assemble reply */
3346 nd
->nd_repstat
= error
;
3347 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3349 *mrepp
= nmrep
.nmc_mhead
;
3350 nfsmout_on_status(nd
, error
);
3351 if (nd
->nd_vers
== NFS_VER3
) {
3352 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
3353 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3354 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3357 nfsm_chain_build_done(error
, &nmrep
);
3361 nfsm_chain_cleanup(&nmrep
);
3368 * nfs symbolic link service
3372 struct nfsrv_descript
*nd
,
3373 struct nfsrv_sock
*slp
,
3377 struct vnode_attr dpreattr
, dpostattr
, postattr
;
3378 struct vnode_attr va
, *vap
= &va
;
3379 struct nameidata ni
;
3380 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
3381 uint32_t len
= 0, linkdatalen
, cnflags
;
3384 vnode_t vp
, dvp
, dirp
;
3385 struct nfs_filehandle nfh
;
3386 struct nfs_export
*nx
= NULL
;
3387 struct nfs_export_options
*nxo
;
3389 char uio_buf
[ UIO_SIZEOF(1) ];
3390 struct nfsm_chain
*nmreq
, nmrep
;
3393 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
3394 nmreq
= &nd
->nd_nmreq
;
3395 nfsm_chain_null(&nmrep
);
3399 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3401 ni
.ni_cnd
.cn_nameiop
= 0;
3404 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3405 nfsm_chain_get_32(error
, nmreq
, len
);
3406 nfsm_name_len_check(error
, nd
, len
);
3409 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3413 ni
.ni_cnd
.cn_flags
= LOCKPARENT
;
3415 ni
.ni_cnd
.cn_ndp
= &ni
;
3416 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3418 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3420 /* update export stats */
3421 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3423 /* update active user stats */
3424 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3428 if (nd
->nd_vers
== NFS_VER3
) {
3429 nfsm_srv_pre_vattr_init(&dpreattr
);
3430 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3437 ni
.ni_cnd
.cn_nameiop
= 0;
3444 if (nd
->nd_vers
== NFS_VER3
)
3445 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3446 nfsm_chain_get_32(error
, nmreq
, linkdatalen
);
3447 if (!error
&& (((nd
->nd_vers
== NFS_VER2
) && (linkdatalen
> NFS_MAXPATHLEN
)) ||
3448 ((nd
->nd_vers
== NFS_VER3
) && (linkdatalen
> MAXPATHLEN
))))
3449 error
= NFSERR_NAMETOL
;
3451 MALLOC(linkdata
, caddr_t
, linkdatalen
+ 1, M_TEMP
, M_WAITOK
);
3453 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3454 &uio_buf
[0], sizeof(uio_buf
));
3455 if (!linkdata
|| !auio
) {
3459 uio_addiov(auio
, CAST_USER_ADDR_T(linkdata
), linkdatalen
);
3460 error
= nfsm_chain_get_uio(nmreq
, linkdatalen
, auio
);
3461 if (!error
&& (nd
->nd_vers
== NFS_VER2
))
3462 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3464 *(linkdata
+ linkdatalen
) = '\0';
3470 VATTR_SET(vap
, va_type
, VLNK
);
3471 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3472 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3474 * Server policy is to alway use the mapped rpc credential for
3475 * file system object creation. This has the nice side effect of
3476 * enforcing BSD creation semantics
3478 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3479 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3481 /* authorize before creating */
3482 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, ctx
, nxo
, 0);
3484 /* validate given attributes */
3486 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
3488 error
= vn_authorize_create(dvp
, &ni
.ni_cnd
, vap
, ctx
, NULL
);
3494 error
= VNOP_SYMLINK(dvp
, &vp
, &ni
.ni_cnd
, vap
, linkdata
, ctx
);
3496 if (!error
&& (nd
->nd_vers
== NFS_VER3
)) {
3498 ni
.ni_cnd
.cn_nameiop
= LOOKUP
;
3500 ni
.ni_op
= OP_LOOKUP
;
3502 ni
.ni_cnd
.cn_flags
&= ~(LOCKPARENT
| FOLLOW
);
3503 ni
.ni_cnd
.cn_flags
|= (NOFOLLOW
| LOCKLEAF
);
3504 ni
.ni_cnd
.cn_context
= ctx
;
3505 ni
.ni_startdir
= dvp
;
3507 ni
.ni_rootdir
= rootvnode
;
3508 cnflags
= ni
.ni_cnd
.cn_flags
; /* store in case we have to restore */
3509 while ((error
= lookup(&ni
)) == ERECYCLE
) {
3510 ni
.ni_cnd
.cn_flags
= cnflags
;
3511 ni
.ni_cnd
.cn_nameptr
= ni
.ni_cnd
.cn_pnbuf
;
3512 ni
.ni_usedvp
= ni
.ni_dvp
= ni
.ni_startdir
= dvp
;
3518 error
= nfsrv_vptofh(nx
, NFS_VER3
, NULL
, vp
, ctx
, &nfh
);
3520 nfsm_srv_vattr_init(&postattr
, NFS_VER3
);
3521 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
3527 if (nfsrv_fsevents_enabled
&& !error
&& vp
) {
3528 add_fsevent(FSE_CREATE_FILE
, ctx
,
3535 * nameidone has to happen before we vnode_put(dvp)
3536 * since it may need to release the fs_nodelock on the dvp
3539 ni
.ni_cnd
.cn_nameiop
= 0;
3545 FREE(linkdata
, M_TEMP
);
3549 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3550 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3556 /* assemble reply */
3557 nd
->nd_repstat
= error
;
3558 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
3559 NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3561 *mrepp
= nmrep
.nmc_mhead
;
3562 nfsmout_on_status(nd
, error
);
3563 if (nd
->nd_vers
== NFS_VER3
) {
3564 if (!nd
->nd_repstat
) {
3565 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3566 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
3568 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3569 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3572 nfsm_chain_build_done(error
, &nmrep
);
3573 if (ni
.ni_cnd
.cn_nameiop
) {
3575 * nameidone has to happen before we vnode_put(dvp)
3576 * since it may need to release the fs_nodelock on the dvp
3587 FREE(linkdata
, M_TEMP
);
3589 nfsm_chain_cleanup(&nmrep
);
3601 struct nfsrv_descript
*nd
,
3602 struct nfsrv_sock
*slp
,
3606 struct vnode_attr dpreattr
, dpostattr
, postattr
;
3607 struct vnode_attr va
, *vap
= &va
;
3608 struct nameidata ni
;
3609 int error
, dpreattrerr
, dpostattrerr
, postattrerr
;
3611 vnode_t vp
, dvp
, dirp
;
3612 struct nfs_filehandle nfh
;
3613 struct nfs_export
*nx
= NULL
;
3614 struct nfs_export_options
*nxo
;
3616 kauth_acl_t xacl
= NULL
;
3617 struct nfsm_chain
*nmreq
, nmrep
;
3620 dpreattrerr
= dpostattrerr
= postattrerr
= ENOENT
;
3621 nmreq
= &nd
->nd_nmreq
;
3622 nfsm_chain_null(&nmrep
);
3624 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3626 ni
.ni_cnd
.cn_nameiop
= 0;
3627 vp
= dvp
= dirp
= NULL
;
3629 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3630 nfsm_chain_get_32(error
, nmreq
, len
);
3631 nfsm_name_len_check(error
, nd
, len
);
3634 ni
.ni_cnd
.cn_nameiop
= CREATE
;
3638 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| WILLBEDIR
;
3639 ni
.ni_cnd
.cn_ndp
= &ni
;
3640 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3642 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3644 /* update export stats */
3645 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3647 /* update active user stats */
3648 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3652 if (nd
->nd_vers
== NFS_VER3
) {
3653 nfsm_srv_pre_vattr_init(&dpreattr
);
3654 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3661 ni
.ni_cnd
.cn_nameiop
= 0;
3668 error
= nfsm_chain_get_sattr(nd
, nmreq
, vap
);
3670 VATTR_SET(vap
, va_type
, VDIR
);
3674 * nameidone has to happen before we vnode_put(dvp)
3675 * since it may need to release the fs_nodelock on the dvp
3684 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_SUBDIRECTORY
, ctx
, nxo
, 0);
3686 /* construct ACL and handle inheritance */
3688 error
= kauth_acl_inherit(dvp
,
3694 if (!error
&& xacl
!= NULL
)
3695 VATTR_SET(vap
, va_acl
, xacl
);
3698 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3699 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3701 * We don't support the S_ISGID bit for directories. Solaris and other
3702 * SRV4 derived systems might set this to get BSD semantics, which we enforce
3705 if (VATTR_IS_ACTIVE(vap
, va_mode
))
3706 vap
->va_mode
&= ~S_ISGID
;
3708 * Server policy is to alway use the mapped rpc credential for
3709 * file system object creation. This has the nice side effect of
3710 * enforcing BSD creation semantics
3712 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3713 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3715 /* validate new-file security information */
3717 error
= vnode_authattr_new(dvp
, vap
, 0, ctx
);
3719 * vnode_authattr_new can return errors other than EPERM, but that's not going to
3720 * sit well with our clients so we map all errors to EPERM.
3726 error
= vn_authorize_mkdir(dvp
, &ni
.ni_cnd
, vap
, ctx
, NULL
);
3732 error
= VNOP_MKDIR(dvp
, &vp
, &ni
.ni_cnd
, vap
, ctx
);
3735 if (nfsrv_fsevents_enabled
&& !error
)
3736 add_fsevent(FSE_CREATE_DIR
, ctx
, FSE_ARG_VNODE
, vp
, FSE_ARG_DONE
);
3739 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
3741 * If some of the requested attributes weren't handled by the VNOP,
3742 * use our fallback code.
3744 error
= vnode_setattr_fallback(vp
, vap
, ctx
);
3747 kauth_acl_free(xacl
);
3750 error
= nfsrv_vptofh(nx
, nd
->nd_vers
, NULL
, vp
, ctx
, &nfh
);
3752 nfsm_srv_vattr_init(&postattr
, nd
->nd_vers
);
3753 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
3754 if (nd
->nd_vers
== NFS_VER2
)
3755 error
= postattrerr
;
3761 * nameidone has to happen before we vnode_put(dvp)
3762 * since it may need to release the fs_nodelock on the dvp
3767 ni
.ni_cnd
.cn_nameiop
= 0;
3770 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3771 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3777 /* assemble reply */
3778 nd
->nd_repstat
= error
;
3779 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_SRVFH(nd
->nd_vers
, &nfh
) +
3780 NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_WCCDATA(nd
->nd_vers
));
3782 *mrepp
= nmrep
.nmc_mhead
;
3783 nfsmout_on_status(nd
, error
);
3784 if (nd
->nd_vers
== NFS_VER3
) {
3785 if (!nd
->nd_repstat
) {
3786 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3787 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, postattrerr
, &postattr
);
3789 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3790 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3792 nfsm_chain_add_fh(error
, &nmrep
, NFS_VER2
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3794 error
= nfsm_chain_add_fattr(nd
, &nmrep
, &postattr
);
3797 nfsm_chain_build_done(error
, &nmrep
);
3798 if (ni
.ni_cnd
.cn_nameiop
) {
3800 * nameidone has to happen before we vnode_put(dvp)
3801 * since it may need to release the fs_nodelock on the dvp
3811 nfsm_chain_cleanup(&nmrep
);
3822 struct nfsrv_descript
*nd
,
3823 struct nfsrv_sock
*slp
,
3827 int error
, dpreattrerr
, dpostattrerr
;
3830 vnode_t vp
, dvp
, dirp
;
3831 struct vnode_attr dpreattr
, dpostattr
;
3832 struct nfs_filehandle nfh
;
3833 struct nfs_export
*nx
= NULL
;
3834 struct nfs_export_options
*nxo
;
3835 struct nameidata ni
;
3836 struct nfsm_chain
*nmreq
, nmrep
;
3839 dpreattrerr
= dpostattrerr
= ENOENT
;
3840 saved_uid
= kauth_cred_getuid(nd
->nd_cr
);
3841 nmreq
= &nd
->nd_nmreq
;
3842 nfsm_chain_null(&nmrep
);
3844 vp
= dvp
= dirp
= NULL
;
3846 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
3847 nfsm_chain_get_32(error
, nmreq
, len
);
3848 nfsm_name_len_check(error
, nd
, len
);
3851 ni
.ni_cnd
.cn_nameiop
= DELETE
;
3853 ni
.ni_op
= OP_UNLINK
;
3855 ni
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
3856 ni
.ni_cnd
.cn_ndp
= &ni
;
3857 error
= nfsm_chain_get_path_namei(nmreq
, len
, &ni
);
3859 error
= nfsrv_namei(nd
, ctx
, &ni
, &nfh
, &dirp
, &nx
, &nxo
);
3861 /* update export stats */
3862 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
3864 /* update active user stats */
3865 nfsrv_update_user_stat(nx
, nd
, saved_uid
, 1, 0, 0);
3869 if (nd
->nd_vers
== NFS_VER3
) {
3870 nfsm_srv_pre_vattr_init(&dpreattr
);
3871 dpreattrerr
= vnode_getattr(dirp
, &dpreattr
, ctx
);
3882 if (vnode_vtype(vp
) != VDIR
) {
3887 * No rmdir "." please.
3894 * The root of a mounted filesystem cannot be deleted.
3896 if (vnode_isvroot(vp
))
3899 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, ctx
, nxo
, 0);
3901 error
= vn_authorize_rmdir(dvp
, vp
, &ni
.ni_cnd
, ctx
, NULL
);
3912 if (nfsrv_fsevents_enabled
&& need_fsevent(FSE_DELETE
, dvp
)) {
3914 if ((path
= get_pathbuff()) && !vn_getpath(vp
, path
, &plen
)) {
3915 get_fse_info(vp
, &finfo
, ctx
);
3917 release_pathbuff(path
);
3921 #endif /* CONFIG_FSE */
3923 error
= VNOP_RMDIR(dvp
, vp
, &ni
.ni_cnd
, ctx
);
3928 add_fsevent(FSE_DELETE
, ctx
,
3929 FSE_ARG_STRING
, plen
, path
,
3930 FSE_ARG_FINFO
, &finfo
,
3932 release_pathbuff(path
);
3934 #endif /* CONFIG_FSE */
3938 * nameidone has to happen before we vnode_put(dvp)
3939 * since it may need to release the fs_nodelock on the dvp
3947 nfsm_srv_vattr_init(&dpostattr
, nd
->nd_vers
);
3948 dpostattrerr
= vnode_getattr(dirp
, &dpostattr
, ctx
);
3954 /* assemble reply */
3955 nd
->nd_repstat
= error
;
3956 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_WCCDATA(nd
->nd_vers
));
3958 *mrepp
= nmrep
.nmc_mhead
;
3959 nfsmout_on_status(nd
, error
);
3960 if (nd
->nd_vers
== NFS_VER3
)
3961 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
3962 dpreattrerr
, &dpreattr
, dpostattrerr
, &dpostattr
);
3964 nfsm_chain_build_done(error
, &nmrep
);
3968 nfsm_chain_cleanup(&nmrep
);
3975 * nfs readdir service
3976 * - mallocs what it thinks is enough to read
3977 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
3978 * - calls VNOP_READDIR()
3979 * - loops around building the reply
3980 * if the output generated exceeds count break out of loop
3981 * The nfsm_clget macro is used here so that the reply will be packed
3982 * tightly in mbuf clusters.
3983 * - it only knows that it has encountered eof when the VNOP_READDIR()
3985 * - as such one readdir rpc will return eof false although you are there
3986 * and then the next will return eof
3987 * - it trims out records with d_fileno == 0
3988 * this doesn't matter for Unix clients, but they might confuse clients
3990 * NB: It is tempting to set eof to true if the VNOP_READDIR() reads less
3991 * than requested, but this may not apply to all filesystems. For
3992 * example, client NFS does not { although it is never remote mounted
3994 * The alternate call nfsrv_readdirplus() does lookups as well.
3995 * PS: The XNFS protocol spec clearly describes what the "count"s arguments
3996 * are supposed to cover. For readdir, the count is the total number of
3997 * bytes included in everything from the directory's postopattr through
3998 * the EOF flag. For readdirplus, the maxcount is the same, and the
3999 * dircount includes all that except for the entry attributes and handles.
4003 struct nfsrv_descript
*nd
,
4004 struct nfsrv_sock
*slp
,
4008 struct direntry
*dp
;
4009 char *cpos
, *cend
, *rbuf
;
4011 struct vnode_attr attr
;
4012 struct nfs_filehandle nfh
;
4013 struct nfs_export
*nx
;
4014 struct nfs_export_options
*nxo
;
4016 char uio_buf
[ UIO_SIZEOF(1) ];
4017 int len
, nlen
, rem
, xfer
, error
, attrerr
;
4018 int siz
, count
, fullsiz
, eofflag
, nentries
;
4019 u_quad_t off
, toff
, verf
;
4021 struct nfsm_chain
*nmreq
, nmrep
;
4025 count
= nentries
= 0;
4026 nmreq
= &nd
->nd_nmreq
;
4027 nfsm_chain_null(&nmrep
);
4031 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
4033 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4034 if (nd
->nd_vers
== NFS_VER3
) {
4035 nfsm_chain_get_64(error
, nmreq
, toff
);
4036 nfsm_chain_get_64(error
, nmreq
, verf
);
4038 nfsm_chain_get_32(error
, nmreq
, toff
);
4040 nfsm_chain_get_32(error
, nmreq
, count
);
4044 siz
= ((count
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
4045 xfer
= NFSRV_NDMAXDATA(nd
);
4050 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4053 /* update export stats */
4054 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4056 /* update active user stats */
4057 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4059 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4062 if (nxo
->nxo_flags
& NX_MANGLEDNAMES
|| nd
->nd_vers
== NFS_VER2
)
4063 vnopflag
|= VNODE_READDIR_NAMEMAX
;
4065 if ((nd
->nd_vers
== NFS_VER2
) || (nxo
->nxo_flags
& NX_32BITCLIENTS
))
4066 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
4068 if (nd
->nd_vers
== NFS_VER3
) {
4069 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4070 error
= attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4071 if (!error
&& toff
&& verf
&& (verf
!= attr
.va_filerev
))
4072 error
= NFSERR_BAD_COOKIE
;
4075 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, ctx
, nxo
, 0);
4078 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
4080 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
4081 &uio_buf
[0], sizeof(uio_buf
));
4082 if (!rbuf
|| !auio
) {
4087 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
4088 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
4090 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, ctx
);
4091 off
= uio_offset(auio
);
4093 if (nd
->nd_vers
== NFS_VER3
) {
4094 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4095 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4099 if (uio_resid(auio
) != 0) {
4100 siz
-= uio_resid(auio
);
4102 /* If nothing read, return empty reply with eof set */
4107 /* assemble reply */
4108 nd
->nd_repstat
= error
;
4109 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) +
4110 NFSX_COOKIEVERF(nd
->nd_vers
) + 2 * NFSX_UNSIGNED
);
4112 *mrepp
= nmrep
.nmc_mhead
;
4113 nfsmout_on_status(nd
, error
);
4114 if (nd
->nd_vers
== NFS_VER3
) {
4115 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4116 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4118 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4119 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4120 nfsm_chain_build_done(error
, &nmrep
);
4126 * Check for degenerate cases of nothing useful read.
4127 * If so go try again
4131 dp
= (struct direntry
*)cpos
;
4132 while ((dp
->d_fileno
== 0) && (cpos
< cend
) && (nentries
> 0)) {
4133 cpos
+= dp
->d_reclen
;
4134 dp
= (struct direntry
*)cpos
;
4137 if ((cpos
>= cend
) || (nentries
== 0)) {
4146 /* assemble reply */
4147 nd
->nd_repstat
= error
;
4148 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) +
4149 NFSX_COOKIEVERF(nd
->nd_vers
) + siz
);
4151 *mrepp
= nmrep
.nmc_mhead
;
4152 nfsmout_on_status(nd
, error
);
4153 nmrep
.nmc_flags
|= NFSM_CHAIN_FLAG_ADD_CLUSTERS
;
4155 len
= 2 * NFSX_UNSIGNED
;
4156 if (nd
->nd_vers
== NFS_VER3
) {
4157 len
+= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
;
4158 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4159 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4163 /* Loop through the records and build reply */
4164 while ((cpos
< cend
) && (nentries
> 0)) {
4165 if (dp
->d_fileno
!= 0) {
4166 nlen
= dp
->d_namlen
;
4167 if ((nd
->nd_vers
== NFS_VER2
) && (nlen
> NFS_MAXNAMLEN
))
4168 nlen
= NFS_MAXNAMLEN
;
4169 rem
= nfsm_rndup(nlen
)-nlen
;
4170 len
+= (4 * NFSX_UNSIGNED
+ nlen
+ rem
);
4171 if (nd
->nd_vers
== NFS_VER3
)
4172 len
+= 2 * NFSX_UNSIGNED
;
4177 /* Build the directory record xdr from the direntry. */
4178 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4179 if (nd
->nd_vers
== NFS_VER3
) {
4180 nfsm_chain_add_64(error
, &nmrep
, dp
->d_fileno
);
4182 nfsm_chain_add_32(error
, &nmrep
, dp
->d_fileno
);
4184 nfsm_chain_add_string(error
, &nmrep
, dp
->d_name
, nlen
);
4185 if (nd
->nd_vers
== NFS_VER3
) {
4186 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
4187 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
4188 nfsm_chain_add_64(error
, &nmrep
, dp
->d_seekoff
);
4190 nfsm_chain_add_32(error
, &nmrep
, dp
->d_seekoff
);
4194 cpos
+= dp
->d_reclen
;
4195 dp
= (struct direntry
*)cpos
;
4198 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4199 nfsm_chain_add_32(error
, &nmrep
, eofflag
? TRUE
: FALSE
);
4207 nd
->nd_repstat
= error
;
4208 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
));
4210 *mrepp
= nmrep
.nmc_mhead
;
4211 nfsmout_on_status(nd
, error
);
4212 if (nd
->nd_vers
== NFS_VER3
)
4213 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4215 nfsm_chain_build_done(error
, &nmrep
);
4217 nfsm_chain_cleanup(&nmrep
);
4225 struct nfsrv_descript
*nd
,
4226 struct nfsrv_sock
*slp
,
4230 struct direntry
*dp
;
4231 char *cpos
, *cend
, *rbuf
;
4233 struct nfs_filehandle dnfh
, nfh
;
4234 struct nfs_export
*nx
;
4235 struct nfs_export_options
*nxo
;
4237 char uio_buf
[ UIO_SIZEOF(1) ];
4238 struct vnode_attr attr
, va
, *vap
= &va
;
4239 int len
, nlen
, rem
, xfer
, error
, attrerr
, gotfh
, gotattr
;
4240 int siz
, dircount
, maxcount
, fullsiz
, eofflag
, dirlen
, nentries
, isdotdot
;
4241 u_quad_t off
, toff
, verf
;
4243 struct nfsm_chain
*nmreq
, nmrep
;
4248 nmreq
= &nd
->nd_nmreq
;
4249 nfsm_chain_null(&nmrep
);
4252 dircount
= maxcount
= 0;
4254 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
4256 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, dnfh
.nfh_fhp
, dnfh
.nfh_len
);
4257 nfsm_chain_get_64(error
, nmreq
, toff
);
4258 nfsm_chain_get_64(error
, nmreq
, verf
);
4259 nfsm_chain_get_32(error
, nmreq
, dircount
);
4260 nfsm_chain_get_32(error
, nmreq
, maxcount
);
4264 xfer
= NFSRV_NDMAXDATA(nd
);
4265 dircount
= ((dircount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
4266 if (dircount
> xfer
)
4268 fullsiz
= siz
= dircount
;
4269 maxcount
= ((maxcount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
4270 if (maxcount
> xfer
)
4273 error
= nfsrv_fhtovp(&dnfh
, nd
, &vp
, &nx
, &nxo
);
4276 /* update export stats */
4277 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4279 /* update active user stats */
4280 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4282 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4285 if (nxo
->nxo_flags
& NX_32BITCLIENTS
)
4286 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
4288 if (nxo
->nxo_flags
& NX_MANGLEDNAMES
)
4289 vnopflag
|= VNODE_READDIR_NAMEMAX
;
4291 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4292 error
= attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4293 if (!error
&& toff
&& verf
&& (verf
!= attr
.va_filerev
))
4294 error
= NFSERR_BAD_COOKIE
;
4296 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, ctx
, nxo
, 0);
4299 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
4301 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
4302 &uio_buf
[0], sizeof(uio_buf
));
4303 if (!rbuf
|| !auio
) {
4309 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
4310 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
4312 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, ctx
);
4313 off
= uio_offset(auio
);
4314 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4315 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4318 if (uio_resid(auio
) != 0) {
4319 siz
-= uio_resid(auio
);
4321 /* If nothing read, return empty reply with eof set */
4326 /* assemble reply */
4327 nd
->nd_repstat
= error
;
4328 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+
4329 NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
);
4331 *mrepp
= nmrep
.nmc_mhead
;
4332 nfsmout_on_status(nd
, error
);
4333 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4334 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4335 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4336 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4337 nfsm_chain_build_done(error
, &nmrep
);
4343 * Check for degenerate cases of nothing useful read.
4344 * If so go try again
4348 dp
= (struct direntry
*)cpos
;
4349 while ((dp
->d_fileno
== 0) && (cpos
< cend
) && (nentries
> 0)) {
4350 cpos
+= dp
->d_reclen
;
4351 dp
= (struct direntry
*)cpos
;
4354 if ((cpos
>= cend
) || (nentries
== 0)) {
4361 * Probe one of the directory entries to see if the filesystem
4364 if ((error
= VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, ctx
))) {
4365 if (error
== ENOTSUP
) /* let others get passed back */
4366 error
= NFSERR_NOTSUPP
;
4371 /* assemble reply */
4372 nd
->nd_repstat
= error
;
4373 error
= nfsrv_rephead(nd
, slp
, &nmrep
, maxcount
);
4375 *mrepp
= nmrep
.nmc_mhead
;
4376 nfsmout_on_status(nd
, error
);
4377 nmrep
.nmc_flags
|= NFSM_CHAIN_FLAG_ADD_CLUSTERS
;
4379 dirlen
= len
= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
;
4380 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4381 nfsm_chain_add_64(error
, &nmrep
, attr
.va_filerev
);
4384 /* Loop through the records and build reply */
4385 while ((cpos
< cend
) && (nentries
> 0)) {
4386 if (dp
->d_fileno
!= 0) {
4387 nlen
= dp
->d_namlen
;
4388 rem
= nfsm_rndup(nlen
)-nlen
;
4389 gotfh
= gotattr
= 1;
4391 /* Got to get the vnode for lookup per entry. */
4392 if (VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, ctx
)) {
4393 /* Can't get the vnode... so no fh or attrs */
4394 gotfh
= gotattr
= 0;
4396 isdotdot
= ((dp
->d_namlen
== 2) &&
4397 (dp
->d_name
[0] == '.') && (dp
->d_name
[1] == '.'));
4398 if (nfsrv_vptofh(nx
, 0, (isdotdot
? &dnfh
: NULL
), nvp
, ctx
, &nfh
))
4400 nfsm_srv_vattr_init(vap
, NFS_VER3
);
4401 if (vnode_getattr(nvp
, vap
, ctx
))
4407 * If either the dircount or maxcount will be
4408 * exceeded, get out now. Both of these lengths
4409 * are calculated conservatively, including all
4412 len
+= 8 * NFSX_UNSIGNED
+ nlen
+ rem
;
4414 len
+= NFSX_V3FATTR
;
4416 len
+= NFSX_UNSIGNED
+ nfsm_rndup(nfh
.nfh_len
);
4417 dirlen
+= 6 * NFSX_UNSIGNED
+ nlen
+ rem
;
4418 if ((len
> maxcount
) || (dirlen
> dircount
)) {
4423 /* Build the directory record xdr from the direntry. */
4424 nfsm_chain_add_32(error
, &nmrep
, TRUE
);
4425 nfsm_chain_add_64(error
, &nmrep
, dp
->d_fileno
);
4426 nfsm_chain_add_string(error
, &nmrep
, dp
->d_name
, nlen
);
4427 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
4428 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
4429 nfsm_chain_add_64(error
, &nmrep
, dp
->d_seekoff
);
4430 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, (gotattr
? 0 : ENOENT
), vap
);
4432 nfsm_chain_add_postop_fh(error
, &nmrep
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4434 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4437 cpos
+= dp
->d_reclen
;
4438 dp
= (struct direntry
*)cpos
;
4443 nfsm_chain_add_32(error
, &nmrep
, FALSE
);
4444 nfsm_chain_add_32(error
, &nmrep
, eofflag
? TRUE
: FALSE
);
4450 nd
->nd_repstat
= error
;
4451 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
);
4453 *mrepp
= nmrep
.nmc_mhead
;
4454 nfsmout_on_status(nd
, error
);
4455 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4457 nfsm_chain_build_done(error
, &nmrep
);
4461 nfsm_chain_cleanup(&nmrep
);
4468 * nfs commit service
4472 struct nfsrv_descript
*nd
,
4473 struct nfsrv_sock
*slp
,
4478 struct nfs_filehandle nfh
;
4479 struct nfs_export
*nx
;
4480 struct nfs_export_options
*nxo
;
4481 int error
, preattrerr
, postattrerr
, count
;
4482 struct vnode_attr preattr
, postattr
;
4484 struct nfsm_chain
*nmreq
, nmrep
;
4487 preattrerr
= postattrerr
= ENOENT
;
4488 nmreq
= &nd
->nd_nmreq
;
4489 nfsm_chain_null(&nmrep
);
4493 * XXX At this time VNOP_FSYNC() does not accept offset and byte
4494 * count parameters, so those arguments are useless (someday maybe).
4497 nfsm_chain_get_fh_ptr(error
, nmreq
, NFS_VER3
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4498 nfsm_chain_get_64(error
, nmreq
, off
);
4499 nfsm_chain_get_32(error
, nmreq
, count
);
4502 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4505 /* update export stats */
4506 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4508 /* update active user stats */
4509 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4511 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4514 nfsm_srv_pre_vattr_init(&preattr
);
4515 preattrerr
= vnode_getattr(vp
, &preattr
, ctx
);
4517 error
= VNOP_FSYNC(vp
, MNT_WAIT
, ctx
);
4519 nfsm_srv_vattr_init(&postattr
, 1);
4520 postattrerr
= vnode_getattr(vp
, &postattr
, ctx
);
4526 /* assemble reply */
4527 nd
->nd_repstat
= error
;
4528 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3WCCDATA
+ NFSX_V3WRITEVERF
);
4530 *mrepp
= nmrep
.nmc_mhead
;
4531 nfsmout_on_status(nd
, error
);
4532 nfsm_chain_add_wcc_data(error
, nd
, &nmrep
,
4533 preattrerr
, &preattr
, postattrerr
, &postattr
);
4534 if (!nd
->nd_repstat
) {
4535 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_sec
);
4536 nfsm_chain_add_32(error
, &nmrep
, nx
->nx_exptime
.tv_usec
);
4539 nfsm_chain_build_done(error
, &nmrep
);
4541 nfsm_chain_cleanup(&nmrep
);
4548 * nfs statfs service
4552 struct nfsrv_descript
*nd
,
4553 struct nfsrv_sock
*slp
,
4560 struct vnode_attr attr
;
4561 struct nfs_filehandle nfh
;
4562 struct nfs_export
*nx
;
4563 struct nfs_export_options
*nxo
;
4565 struct nfsm_chain
*nmreq
, nmrep
;
4569 nmreq
= &nd
->nd_nmreq
;
4570 nfsm_chain_null(&nmrep
);
4574 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4576 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4579 /* update export stats */
4580 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4582 /* update active user stats */
4583 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4585 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4589 VFSATTR_WANTED(&va
, f_blocks
);
4590 VFSATTR_WANTED(&va
, f_bavail
);
4591 VFSATTR_WANTED(&va
, f_files
);
4592 VFSATTR_WANTED(&va
, f_ffree
);
4593 error
= vfs_getattr(vnode_mount(vp
), &va
, ctx
);
4594 blksize
= vnode_mount(vp
)->mnt_vfsstat
.f_bsize
;
4596 if (nd
->nd_vers
== NFS_VER3
) {
4597 nfsm_srv_vattr_init(&attr
, nd
->nd_vers
);
4598 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4605 /* assemble reply */
4606 nd
->nd_repstat
= error
;
4607 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_POSTOPATTR(nd
->nd_vers
) + NFSX_STATFS(nd
->nd_vers
));
4609 *mrepp
= nmrep
.nmc_mhead
;
4610 nfsmout_on_status(nd
, error
);
4611 if (nd
->nd_vers
== NFS_VER3
)
4612 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4613 nfsmout_if(nd
->nd_repstat
);
4615 if (nd
->nd_vers
== NFS_VER3
) {
4616 nfsm_chain_add_64(error
, &nmrep
, va
.f_blocks
* blksize
);
4617 nfsm_chain_add_64(error
, &nmrep
, va
.f_bfree
* blksize
);
4618 nfsm_chain_add_64(error
, &nmrep
, va
.f_bavail
* blksize
);
4619 nfsm_chain_add_64(error
, &nmrep
, va
.f_files
);
4620 nfsm_chain_add_64(error
, &nmrep
, va
.f_ffree
);
4621 nfsm_chain_add_64(error
, &nmrep
, va
.f_ffree
);
4622 nfsm_chain_add_32(error
, &nmrep
, 0); /* invarsec */
4624 nfsm_chain_add_32(error
, &nmrep
, NFS_V2MAXDATA
);
4625 nfsm_chain_add_32(error
, &nmrep
, blksize
);
4626 nfsm_chain_add_32(error
, &nmrep
, va
.f_blocks
);
4627 nfsm_chain_add_32(error
, &nmrep
, va
.f_bfree
);
4628 nfsm_chain_add_32(error
, &nmrep
, va
.f_bavail
);
4631 nfsm_chain_build_done(error
, &nmrep
);
4633 nfsm_chain_cleanup(&nmrep
);
4640 * nfs fsinfo service
4644 struct nfsrv_descript
*nd
,
4645 struct nfsrv_sock
*slp
,
4649 int error
, attrerr
, prefsize
, maxsize
;
4651 struct vnode_attr attr
;
4652 struct nfs_filehandle nfh
;
4653 struct nfs_export
*nx
;
4654 struct nfs_export_options
*nxo
;
4655 struct nfsm_chain
*nmreq
, nmrep
;
4659 nmreq
= &nd
->nd_nmreq
;
4660 nfsm_chain_null(&nmrep
);
4663 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4665 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4668 /* update export stats */
4669 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4671 /* update active user stats */
4672 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4674 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4677 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4678 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4684 /* assemble reply */
4685 nd
->nd_repstat
= error
;
4686 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+ NFSX_V3FSINFO
);
4688 *mrepp
= nmrep
.nmc_mhead
;
4689 nfsmout_on_status(nd
, error
);
4690 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4691 nfsmout_if(nd
->nd_repstat
);
4694 * XXX There should be file system VFS OP(s) to get this information.
4695 * For now, assume our usual NFS defaults.
4697 if (slp
->ns_sotype
== SOCK_DGRAM
) {
4698 maxsize
= NFS_MAXDGRAMDATA
;
4699 prefsize
= NFS_PREFDGRAMDATA
;
4701 maxsize
= prefsize
= NFSRV_MAXDATA
;
4703 nfsm_chain_add_32(error
, &nmrep
, maxsize
);
4704 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4705 nfsm_chain_add_32(error
, &nmrep
, NFS_FABLKSIZE
);
4706 nfsm_chain_add_32(error
, &nmrep
, maxsize
);
4707 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4708 nfsm_chain_add_32(error
, &nmrep
, NFS_FABLKSIZE
);
4709 nfsm_chain_add_32(error
, &nmrep
, prefsize
);
4710 nfsm_chain_add_64(error
, &nmrep
, 0xffffffffffffffffULL
);
4711 nfsm_chain_add_32(error
, &nmrep
, 0);
4712 nfsm_chain_add_32(error
, &nmrep
, 1);
4713 /* XXX link/symlink support should be taken from volume capabilities */
4714 nfsm_chain_add_32(error
, &nmrep
,
4715 NFSV3FSINFO_LINK
| NFSV3FSINFO_SYMLINK
|
4716 NFSV3FSINFO_HOMOGENEOUS
| NFSV3FSINFO_CANSETTIME
);
4719 nfsm_chain_build_done(error
, &nmrep
);
4721 nfsm_chain_cleanup(&nmrep
);
4728 * nfs pathconf service
4732 struct nfsrv_descript
*nd
,
4733 struct nfsrv_sock
*slp
,
4737 int error
, attrerr
, linkmax
, namemax
;
4738 int chownres
, notrunc
, case_sensitive
, case_preserving
;
4740 struct vnode_attr attr
;
4741 struct nfs_filehandle nfh
;
4742 struct nfs_export
*nx
;
4743 struct nfs_export_options
*nxo
;
4744 struct nfsm_chain
*nmreq
, nmrep
;
4748 nmreq
= &nd
->nd_nmreq
;
4749 nfsm_chain_null(&nmrep
);
4752 nfsm_chain_get_fh_ptr(error
, nmreq
, nd
->nd_vers
, nfh
.nfh_fhp
, nfh
.nfh_len
);
4754 error
= nfsrv_fhtovp(&nfh
, nd
, &vp
, &nx
, &nxo
);
4757 /* update export stats */
4758 NFSStatAdd64(&nx
->nx_stats
.ops
, 1);
4760 /* update active user stats */
4761 nfsrv_update_user_stat(nx
, nd
, kauth_cred_getuid(nd
->nd_cr
), 1, 0, 0);
4763 error
= nfsrv_credcheck(nd
, ctx
, nx
, nxo
);
4766 error
= VNOP_PATHCONF(vp
, _PC_LINK_MAX
, &linkmax
, ctx
);
4768 error
= VNOP_PATHCONF(vp
, _PC_NAME_MAX
, &namemax
, ctx
);
4770 error
= VNOP_PATHCONF(vp
, _PC_CHOWN_RESTRICTED
, &chownres
, ctx
);
4772 error
= VNOP_PATHCONF(vp
, _PC_NO_TRUNC
, ¬runc
, ctx
);
4774 error
= VNOP_PATHCONF(vp
, _PC_CASE_SENSITIVE
, &case_sensitive
, ctx
);
4776 error
= VNOP_PATHCONF(vp
, _PC_CASE_PRESERVING
, &case_preserving
, ctx
);
4778 nfsm_srv_vattr_init(&attr
, NFS_VER3
);
4779 attrerr
= vnode_getattr(vp
, &attr
, ctx
);
4785 /* assemble reply */
4786 nd
->nd_repstat
= error
;
4787 error
= nfsrv_rephead(nd
, slp
, &nmrep
, NFSX_V3POSTOPATTR
+ NFSX_V3PATHCONF
);
4789 *mrepp
= nmrep
.nmc_mhead
;
4790 nfsmout_on_status(nd
, error
);
4791 nfsm_chain_add_postop_attr(error
, nd
, &nmrep
, attrerr
, &attr
);
4792 nfsmout_if(nd
->nd_repstat
);
4794 nfsm_chain_add_32(error
, &nmrep
, linkmax
);
4795 nfsm_chain_add_32(error
, &nmrep
, namemax
);
4796 nfsm_chain_add_32(error
, &nmrep
, notrunc
);
4797 nfsm_chain_add_32(error
, &nmrep
, chownres
);
4798 nfsm_chain_add_32(error
, &nmrep
, !case_sensitive
);
4799 nfsm_chain_add_32(error
, &nmrep
, case_preserving
);
4802 nfsm_chain_build_done(error
, &nmrep
);
4804 nfsm_chain_cleanup(&nmrep
);
4811 * Null operation, used by clients to ping server
4816 struct nfsrv_descript
*nd
,
4817 struct nfsrv_sock
*slp
,
4818 __unused vfs_context_t ctx
,
4821 int error
= NFSERR_RETVOID
;
4822 struct nfsm_chain nmrep
;
4825 * RPCSEC_GSS context setup ?
4827 if (nd
->nd_gss_context
)
4828 return(nfs_gss_svc_ctx_init(nd
, slp
, mrepp
));
4830 nfsm_chain_null(&nmrep
);
4832 /* assemble reply */
4833 nd
->nd_repstat
= error
;
4834 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 0);
4836 *mrepp
= nmrep
.nmc_mhead
;
4838 nfsm_chain_build_done(error
, &nmrep
);
4840 nfsm_chain_cleanup(&nmrep
);
4847 * No operation, used for obsolete procedures
4852 struct nfsrv_descript
*nd
,
4853 struct nfsrv_sock
*slp
,
4854 __unused vfs_context_t ctx
,
4858 struct nfsm_chain nmrep
;
4860 nfsm_chain_null(&nmrep
);
4863 error
= nd
->nd_repstat
;
4865 error
= EPROCUNAVAIL
;
4867 /* assemble reply */
4868 nd
->nd_repstat
= error
;
4869 error
= nfsrv_rephead(nd
, slp
, &nmrep
, 0);
4871 *mrepp
= nmrep
.nmc_mhead
;
4873 nfsm_chain_build_done(error
, &nmrep
);
4875 nfsm_chain_cleanup(&nmrep
);
4881 int (*nfsrv_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
4882 struct nfsrv_sock
*slp
,
4911 * Perform access checking for vnodes obtained from file handles that would
4912 * refer to files already opened by a Unix client. You cannot just use
4913 * vnode_authorize() for two reasons.
4914 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4915 * 2 - The owner is to be given access irrespective of mode bits so that
4916 * processes that chmod after opening a file don't break. I don't like
4917 * this because it opens a security hole, but since the nfs server opens
4918 * a security hole the size of a barn door anyhow, what the heck.
4920 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, vnode_authorize()
4921 * will return EPERM instead of EACCESS. EPERM is always an error.
4928 kauth_action_t action
,
4930 struct nfs_export_options
*nxo
,
4933 struct vnode_attr vattr
;
4936 if (action
& KAUTH_VNODE_WRITE_RIGHTS
) {
4938 * Disallow write attempts on read-only exports;
4939 * unless the file is a socket or a block or character
4940 * device resident on the file system.
4942 if (nxo
->nxo_flags
& NX_READONLY
) {
4943 switch (vnode_vtype(vp
)) {
4944 case VREG
: case VDIR
: case VLNK
: case VCPLX
:
4951 error
= vnode_authorize(vp
, dvp
, action
, ctx
);
4953 * Allow certain operations for the owner (reads and writes
4954 * on files that are already open). Picking up from FreeBSD.
4956 if (override
&& (error
== EACCES
)) {
4958 VATTR_WANTED(&vattr
, va_uid
);
4959 if ((vnode_getattr(vp
, &vattr
, ctx
) == 0) &&
4960 (kauth_cred_getuid(vfs_context_ucred(ctx
)) == vattr
.va_uid
))
4966 #endif /* NFSSERVER */