2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * Rick Macklem at The University of Guelph.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
67 * FreeBSD-Id: nfs_serv.c,v 1.52 1997/10/28 15:59:05 bde Exp $
71 * nfs version 2 and 3 server calls to vnode ops
72 * - these routines generally have 3 phases
73 * 1 - break down and validate rpc request in mbuf list
74 * 2 - do the vnode ops for the request
75 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
76 * 3 - build the rpc reply in an mbuf list
78 * - do not mix the phases, since the nfsm_?? macros can return failures
79 * on a bad rpc or similar and do not do any vnode_rele()s or vnode_put()s
81 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
82 * error number iff error != 0 whereas
83 * returning an error from the server function implies a fatal error
84 * such as a badly constructed rpc request that should be dropped without
86 * For Version 3, nfsm_reply() does not return for the error case, since
87 * most version 3 rpcs return more than the status for error cases.
90 #include <sys/param.h>
91 #include <sys/systm.h>
93 #include <sys/kauth.h>
94 #include <sys/unistd.h>
95 #include <sys/malloc.h>
96 #include <sys/vnode.h>
97 #include <sys/mount_internal.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/kpi_mbuf.h>
101 #include <sys/dirent.h>
102 #include <sys/stat.h>
103 #include <sys/kernel.h>
104 #include <sys/sysctl.h>
106 #include <sys/vnode_internal.h>
107 #include <sys/uio_internal.h>
108 #include <libkern/OSAtomic.h>
111 #include <sys/vmparam.h>
113 #include <nfs/nfsproto.h>
114 #include <nfs/rpcv2.h>
116 #include <nfs/xdr_subs.h>
117 #include <nfs/nfsm_subs.h>
119 nfstype nfsv3_type
[9] = { NFNON
, NFREG
, NFDIR
, NFBLK
, NFCHR
, NFLNK
, NFSOCK
,
122 nfstype nfsv2_type
[9] = { NFNON
, NFREG
, NFDIR
, NFBLK
, NFCHR
, NFLNK
, NFNON
,
125 extern u_long nfs_xdrneg1
;
126 extern u_long nfs_false
, nfs_true
;
127 extern enum vtype nv3tov_type
[8];
128 extern struct nfsstats nfsstats
;
130 int nfsrvw_procrastinate
= NFS_GATHERDELAY
* 1000;
131 int nfsrvw_procrastinate_v3
= 0;
135 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
136 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, async
, CTLFLAG_RW
, &nfs_async
, 0, "");
139 static int nfsrv_authorize(vnode_t
,vnode_t
,kauth_action_t
,vfs_context_t
,struct nfs_export_options
*,int);
140 static void nfsrvw_coalesce(struct nfsrv_descript
*, struct nfsrv_descript
*);
142 #define THREAD_SAFE_FS(VP) \
143 ((VP)->v_mount ? (VP)->v_mount->mnt_vtable->vfc_threadsafe : 0)
146 * nfs v3 access service
149 nfsrv3_access(nfsd
, slp
, procp
, mrq
)
150 struct nfsrv_descript
*nfsd
;
151 struct nfssvc_sock
*slp
;
155 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
156 mbuf_t nam
= nfsd
->nd_nam
;
157 caddr_t dpos
= nfsd
->nd_dpos
;
159 struct nfs_filehandle nfh
;
163 int error
= 0, getret
;
165 mbuf_t mb
, mreq
, mb2
;
166 struct vnode_attr vattr
, *vap
= &vattr
;
168 kauth_action_t testaction
;
169 struct vfs_context context
;
170 struct nfs_export
*nx
;
171 struct nfs_export_options
*nxo
;
174 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
175 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
176 nfsm_reply(NFSX_UNSIGNED
);
177 nfsm_srvpostop_attr(1, NULL
);
180 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
182 nfsm_reply(NFSX_UNSIGNED
);
183 nfsm_srvpostop_attr(1, NULL
);
186 nfsmode
= fxdr_unsigned(u_long
, *tl
);
188 context
.vc_proc
= procp
;
189 context
.vc_ucred
= nfsd
->nd_cr
;
192 * Each NFS mode bit is tested separately.
194 * XXX this code is nominally correct, but returns a pessimistic
195 * rather than optimistic result. It will be necessary to add
196 * an NFS-specific interface to the vnode_authorize code to
197 * obtain good performance in the optimistic mode.
199 if (nfsmode
& NFSV3ACCESS_READ
) {
200 if (vnode_isdir(vp
)) {
202 KAUTH_VNODE_LIST_DIRECTORY
|
203 KAUTH_VNODE_READ_EXTATTRIBUTES
;
206 KAUTH_VNODE_READ_DATA
|
207 KAUTH_VNODE_READ_EXTATTRIBUTES
;
209 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
210 nfsmode
&= ~NFSV3ACCESS_READ
;
212 if ((nfsmode
& NFSV3ACCESS_LOOKUP
) &&
214 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, &context
, nxo
, 0)))
215 nfsmode
&= ~NFSV3ACCESS_LOOKUP
;
216 if (nfsmode
& NFSV3ACCESS_MODIFY
) {
217 if (vnode_isdir(vp
)) {
219 KAUTH_VNODE_ADD_FILE
|
220 KAUTH_VNODE_ADD_SUBDIRECTORY
|
221 KAUTH_VNODE_DELETE_CHILD
;
224 KAUTH_VNODE_WRITE_DATA
;
226 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
227 nfsmode
&= ~NFSV3ACCESS_MODIFY
;
229 if (nfsmode
& NFSV3ACCESS_EXTEND
) {
230 if (vnode_isdir(vp
)) {
232 KAUTH_VNODE_ADD_FILE
|
233 KAUTH_VNODE_ADD_SUBDIRECTORY
;
236 KAUTH_VNODE_WRITE_DATA
|
237 KAUTH_VNODE_APPEND_DATA
;
239 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
240 nfsmode
&= ~NFSV3ACCESS_EXTEND
;
244 * Note concerning NFSV3ACCESS_DELETE:
245 * For hard links, the answer may be wrong if the vnode
246 * has multiple parents with different permissions.
247 * Also, some clients (e.g. MacOSX 10.3) may incorrectly
248 * interpret the missing/cleared DELETE bit.
249 * So we'll just leave the DELETE bit alone. At worst,
250 * we're telling the client it might be able to do
251 * something it really can't.
254 if ((nfsmode
& NFSV3ACCESS_EXECUTE
) &&
256 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
, nxo
, 0)))
257 nfsmode
&= ~NFSV3ACCESS_EXECUTE
;
259 nfsm_srv_vattr_init(vap
, 1);
260 getret
= vnode_getattr(vp
, vap
, &context
);
262 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED
);
263 nfsm_srvpostop_attr(getret
, vap
);
264 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
265 *tl
= txdr_unsigned(nfsmode
);
271 * nfs getattr service
274 nfsrv_getattr(nfsd
, slp
, procp
, mrq
)
275 struct nfsrv_descript
*nfsd
;
276 struct nfssvc_sock
*slp
;
280 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
281 mbuf_t nam
= nfsd
->nd_nam
;
282 caddr_t dpos
= nfsd
->nd_dpos
;
283 struct nfs_fattr
*fp
;
284 struct vnode_attr va
;
285 struct vnode_attr
*vap
= &va
;
287 struct nfs_filehandle nfh
;
293 mbuf_t mb
, mb2
, mreq
;
294 struct vfs_context context
;
295 struct nfs_export
*nx
;
296 struct nfs_export_options
*nxo
;
297 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
300 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
304 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
309 context
.vc_proc
= procp
;
310 context
.vc_ucred
= nfsd
->nd_cr
;
312 nfsm_srv_vattr_init(vap
, v3
);
313 error
= vnode_getattr(vp
, vap
, &context
);
315 nfsm_reply(NFSX_FATTR(v3
));
318 nfsm_build(fp
, struct nfs_fattr
*, NFSX_FATTR(v3
));
319 nfsm_srvfillattr(vap
, fp
);
325 * nfs setattr service
328 nfsrv_setattr(nfsd
, slp
, procp
, mrq
)
329 struct nfsrv_descript
*nfsd
;
330 struct nfssvc_sock
*slp
;
334 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
335 mbuf_t nam
= nfsd
->nd_nam
;
336 caddr_t dpos
= nfsd
->nd_dpos
;
337 struct vnode_attr preat
;
338 struct vnode_attr postat
;
339 struct vnode_attr va
;
340 struct vnode_attr
*vap
= &va
;
341 struct nfsv2_sattr
*sp
;
342 struct nfs_fattr
*fp
;
344 struct nfs_filehandle nfh
;
345 struct nfs_export
*nx
;
346 struct nfs_export_options
*nxo
;
350 int error
= 0, preat_ret
= 1, postat_ret
= 1;
351 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), gcheck
= 0;
353 mbuf_t mb
, mb2
, mreq
;
354 struct timespec guard
;
355 struct vfs_context context
;
356 kauth_action_t action
;
363 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
364 gcheck
= fxdr_unsigned(int, *tl
);
366 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
367 fxdr_nfsv3time(tl
, &guard
);
370 nfsm_dissect(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
372 * Nah nah nah nah na nah
373 * There is a bug in the Sun client that puts 0xffff in the mode
374 * field of sattr when it should put in 0xffffffff. The u_short
375 * doesn't sign extend.
376 * --> check the low order 2 bytes for 0xffff
378 if ((fxdr_unsigned(int, sp
->sa_mode
) & 0xffff) != 0xffff)
379 VATTR_SET(vap
, va_mode
, nfstov_mode(sp
->sa_mode
));
380 if (sp
->sa_uid
!= nfs_xdrneg1
)
381 VATTR_SET(vap
, va_uid
, fxdr_unsigned(uid_t
, sp
->sa_uid
));
382 if (sp
->sa_gid
!= nfs_xdrneg1
)
383 VATTR_SET(vap
, va_gid
, fxdr_unsigned(gid_t
, sp
->sa_gid
));
384 if (sp
->sa_size
!= nfs_xdrneg1
)
385 VATTR_SET(vap
, va_data_size
, fxdr_unsigned(u_quad_t
, sp
->sa_size
));
386 if (sp
->sa_atime
.nfsv2_sec
!= nfs_xdrneg1
) {
387 fxdr_nfsv2time(&sp
->sa_atime
, &vap
->va_access_time
);
388 VATTR_SET_ACTIVE(vap
, va_access_time
);
390 if (sp
->sa_mtime
.nfsv2_sec
!= nfs_xdrneg1
) {
391 fxdr_nfsv2time(&sp
->sa_mtime
, &vap
->va_modify_time
);
392 VATTR_SET_ACTIVE(vap
, va_modify_time
);
397 * Save the original credential UID in case they are
398 * mapped and we need to map the IDs in the attributes.
400 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
403 * Now that we have all the fields, lets do it.
405 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
406 nfsm_reply(2 * NFSX_UNSIGNED
);
407 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
410 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
412 nfsm_reply(2 * NFSX_UNSIGNED
);
413 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
417 context
.vc_proc
= procp
;
418 context
.vc_ucred
= nfsd
->nd_cr
;
421 nfsm_srv_pre_vattr_init(&preat
, v3
);
422 error
= preat_ret
= vnode_getattr(vp
, &preat
, &context
);
423 if (!error
&& gcheck
&& VATTR_IS_SUPPORTED(&preat
, va_change_time
) &&
424 (preat
.va_change_time
.tv_sec
!= guard
.tv_sec
||
425 preat
.va_change_time
.tv_nsec
!= guard
.tv_nsec
))
426 error
= NFSERR_NOT_SYNC
;
427 if (!preat_ret
&& !VATTR_ALL_SUPPORTED(&preat
))
431 nfsm_reply(NFSX_WCCDATA(v3
));
432 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
438 * If the credentials were mapped, we should
439 * map the same values in the attributes.
441 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
443 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
444 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
445 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
449 * Authorize the attribute changes.
451 if (((error
= vnode_authattr(vp
, vap
, &action
, &context
))) ||
452 ((error
= nfsrv_authorize(vp
, NULL
, action
, &context
, nxo
, 0))))
454 error
= vnode_setattr(vp
, vap
, &context
);
456 nfsm_srv_vattr_init(&postat
, v3
);
457 postat_ret
= vnode_getattr(vp
, &postat
, &context
);
462 nfsm_reply(NFSX_WCCORFATTR(v3
));
464 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
467 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
468 nfsm_srvfillattr(&postat
, fp
);
478 nfsrv_lookup(nfsd
, slp
, procp
, mrq
)
479 struct nfsrv_descript
*nfsd
;
480 struct nfssvc_sock
*slp
;
484 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
485 mbuf_t nam
= nfsd
->nd_nam
;
486 caddr_t dpos
= nfsd
->nd_dpos
;
487 struct nfs_fattr
*fp
;
488 struct nameidata nd
, *ndp
= &nd
;
489 /* XXX Revisit when enabling WebNFS */
490 #ifdef WEBNFS_ENABLED
491 struct nameidata ind
;
493 vnode_t vp
, dirp
= NULL
;
494 struct nfs_filehandle dnfh
, nfh
;
495 struct nfs_export
*nx
;
496 struct nfs_export_options
*nxo
;
501 int error
= 0, len
, dirattr_ret
= 1, isdotdot
;
502 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), pubflag
;
504 mbuf_t mb
, mb2
, mreq
;
505 struct vnode_attr va
, dirattr
, *vap
= &va
;
506 struct vfs_context context
;
508 context
.vc_proc
= procp
;
509 context
.vc_ucred
= nfsd
->nd_cr
;
511 nfsm_srvmtofh(&dnfh
);
512 nfsm_srvnamesiz(len
, v3
);
514 pubflag
= nfs_ispublicfh(&dnfh
);
516 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
517 nd
.ni_cnd
.cn_flags
= LOCKLEAF
;
518 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, pubflag
, &len
, &nd
);
519 isdotdot
= ((len
== 2) && (nd
.ni_cnd
.cn_pnbuf
[0] == '.') && (nd
.ni_cnd
.cn_pnbuf
[1] == '.'));
521 error
= nfs_namei(nfsd
, &context
, &nd
, &dnfh
, nam
, pubflag
, &dirp
, &nx
, &nxo
);
523 /* XXX Revisit when enabling WebNFS */
524 #ifdef WEBNFS_ENABLED
525 if (!error
&& pubflag
) {
526 if (vnode_vtype(nd
.ni_vp
) == VDIR
&& nfs_pub
.np_index
!= NULL
) {
528 * Setup call to lookup() to see if we can find
529 * the index file. Arguably, this doesn't belong
533 ind
.ni_pathlen
= strlen(nfs_pub
.np_index
);
534 ind
.ni_cnd
.cn_nameptr
= ind
.ni_cnd
.cn_pnbuf
=
536 ind
.ni_startdir
= nd
.ni_vp
;
537 ind
.ni_usedvp
= nd
.ni_vp
;
539 if (!(error
= lookup(&ind
))) {
541 * Found an index file. Get rid of
542 * the old references.
547 vnode_put(nd
.ni_startdir
);
553 * If the public filehandle was used, check that this lookup
554 * didn't result in a filehandle outside the publicly exported
558 if (!error
&& vnode_mount(ndp
->ni_vp
) != nfs_pub
.np_mount
) {
568 nfsm_srv_vattr_init(&dirattr
, v3
);
569 dirattr_ret
= vnode_getattr(dirp
, &dirattr
, &context
);
575 nfsm_reply(NFSX_POSTOPATTR(v3
));
576 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
582 error
= nfsrv_vptofh(nx
, !v3
, (isdotdot
? &dnfh
: NULL
), vp
, &context
, &nfh
);
584 nfsm_srv_vattr_init(vap
, v3
);
585 error
= vnode_getattr(vp
, vap
, &context
);
588 nfsm_reply(NFSX_SRVFH(v3
, &nfh
) + NFSX_POSTOPORFATTR(v3
) + NFSX_POSTOPATTR(v3
));
590 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
593 nfsm_srvfhtom(&nfh
, v3
);
595 nfsm_srvpostop_attr(0, vap
);
596 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
598 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
599 nfsm_srvfillattr(vap
, fp
);
606 * nfs readlink service
609 nfsrv_readlink(nfsd
, slp
, procp
, mrq
)
610 struct nfsrv_descript
*nfsd
;
611 struct nfssvc_sock
*slp
;
615 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
616 mbuf_t nam
= nfsd
->nd_nam
;
617 caddr_t dpos
= nfsd
->nd_dpos
;
622 int error
= 0, i
, tlen
, len
, getret
= 1;
623 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
625 mbuf_t mb
, mb2
, mp2
, mp3
, mreq
;
627 struct vnode_attr attr
;
628 struct nfs_filehandle nfh
;
629 struct nfs_export
*nx
;
630 struct nfs_export_options
*nxo
;
632 char uio_buf
[ UIO_SIZEOF(4) ];
633 char *uio_bufp
= &uio_buf
[0];
634 int uio_buflen
= UIO_SIZEOF(4);
636 struct vfs_context context
;
644 while (len
< NFS_MAXPATHLEN
) {
646 if ((error
= mbuf_mclget(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mp
)))
648 mblen
= mbuf_maxlen(mp
);
649 mbuf_setlen(mp
, mblen
);
653 if ((error
= mbuf_setnext(mp2
, mp
))) {
659 if ((len
+ mblen
) > NFS_MAXPATHLEN
) {
660 mbuf_setlen(mp
, NFS_MAXPATHLEN
- len
);
661 len
= NFS_MAXPATHLEN
;
667 uio_buflen
= UIO_SIZEOF(i
);
668 MALLOC(uio_bufp
, char*, uio_buflen
, M_TEMP
, M_WAITOK
);
672 nfsm_reply(2 * NFSX_UNSIGNED
);
673 nfsm_srvpostop_attr(1, NULL
);
677 uiop
= uio_createwithbuffer(i
, 0, UIO_SYSSPACE
, UIO_READ
, uio_bufp
, uio_buflen
);
681 if (uio_bufp
!= &uio_buf
[0]) {
682 FREE(uio_bufp
, M_TEMP
);
683 uio_bufp
= &uio_buf
[0];
685 nfsm_reply(2 * NFSX_UNSIGNED
);
686 nfsm_srvpostop_attr(1, NULL
);
691 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), mbuf_len(mp
));
695 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
697 if (uio_bufp
!= &uio_buf
[0]) {
698 FREE(uio_bufp
, M_TEMP
);
699 uio_bufp
= &uio_buf
[0];
701 nfsm_reply(2 * NFSX_UNSIGNED
);
702 nfsm_srvpostop_attr(1, NULL
);
705 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
708 if (uio_bufp
!= &uio_buf
[0]) {
709 FREE(uio_bufp
, M_TEMP
);
710 uio_bufp
= &uio_buf
[0];
712 nfsm_reply(2 * NFSX_UNSIGNED
);
713 nfsm_srvpostop_attr(1, NULL
);
716 if (vnode_vtype(vp
) != VLNK
) {
724 context
.vc_proc
= procp
;
725 context
.vc_ucred
= nfsd
->nd_cr
;
727 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, &context
, nxo
, 0)))
729 error
= VNOP_READLINK(vp
, uiop
, &context
);
733 nfsm_srv_vattr_init(&attr
, v3
);
734 getret
= vnode_getattr(vp
, &attr
, &context
);
742 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_UNSIGNED
);
744 nfsm_srvpostop_attr(getret
, &attr
);
746 if (uio_bufp
!= &uio_buf
[0])
747 FREE(uio_bufp
, M_TEMP
);
752 if (uiop
&& (uio_resid(uiop
) > 0)) {
753 // LP64todo - fix this
754 len
-= uio_resid(uiop
);
755 tlen
= nfsm_rndup(len
);
756 nfsm_adj(mp3
, NFS_MAXPATHLEN
-tlen
, tlen
-len
);
758 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
759 *tl
= txdr_unsigned(len
);
760 mbuf_setnext(mb
, mp3
);
763 if (uio_bufp
!= &uio_buf
[0])
764 FREE(uio_bufp
, M_TEMP
);
772 nfsrv_read(nfsd
, slp
, procp
, mrq
)
773 struct nfsrv_descript
*nfsd
;
774 struct nfssvc_sock
*slp
;
778 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
779 mbuf_t nam
= nfsd
->nd_nam
;
780 caddr_t dpos
= nfsd
->nd_dpos
;
782 struct nfs_fattr
*fp
;
787 int error
= 0, count
, len
, left
, siz
, tlen
, getret
;
788 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), reqlen
, maxlen
;
790 mbuf_t mb
, mb2
, mreq
;
793 struct nfs_filehandle nfh
;
794 struct nfs_export
*nx
;
795 struct nfs_export_options
*nxo
;
797 char *uio_bufp
= NULL
;
798 struct vnode_attr va
, *vap
= &va
;
800 char uio_buf
[ UIO_SIZEOF(0) ];
801 struct vfs_context context
;
805 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
806 fxdr_hyper(tl
, &off
);
808 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
809 off
= (off_t
)fxdr_unsigned(u_long
, *tl
);
811 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
812 reqlen
= fxdr_unsigned(u_long
, *tl
);
813 maxlen
= NFS_SRVMAXDATA(nfsd
);
817 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
818 nfsm_reply(2 * NFSX_UNSIGNED
);
819 nfsm_srvpostop_attr(1, NULL
);
822 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
824 nfsm_reply(2 * NFSX_UNSIGNED
);
825 nfsm_srvpostop_attr(1, NULL
);
828 if (vnode_vtype(vp
) != VREG
) {
832 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
835 context
.vc_proc
= procp
;
836 context
.vc_ucred
= nfsd
->nd_cr
;
839 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, &context
, nxo
, 1)))
840 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
, nxo
, 1);
842 nfsm_srv_vattr_init(vap
, v3
);
843 getret
= vnode_getattr(vp
, vap
, &context
);
848 nfsm_reply(NFSX_POSTOPATTR(v3
));
849 nfsm_srvpostop_attr(getret
, vap
);
852 if ((u_quad_t
)off
>= vap
->va_data_size
)
854 else if (((u_quad_t
)off
+ reqlen
) > vap
->va_data_size
)
855 count
= nfsm_rndup(vap
->va_data_size
- off
);
858 nfsm_reply(NFSX_POSTOPORFATTR(v3
) + 3 * NFSX_UNSIGNED
+nfsm_rndup(count
));
860 nfsm_build(tl
, u_long
*, NFSX_V3FATTR
+ 4 * NFSX_UNSIGNED
);
862 fp
= (struct nfs_fattr
*)tl
;
863 tl
+= (NFSX_V3FATTR
/ sizeof (u_long
));
865 nfsm_build(tl
, u_long
*, NFSX_V2FATTR
+ NFSX_UNSIGNED
);
866 fp
= (struct nfs_fattr
*)tl
;
867 tl
+= (NFSX_V2FATTR
/ sizeof (u_long
));
872 * Generate the mbuf list with the uio_iov ref. to it.
877 siz
= min(mbuf_trailingspace(m
), left
);
884 if ((error
= mbuf_mclget(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
)))
890 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
895 uiop
= uio_createwithbuffer(i
, off
, UIO_SYSSPACE
, UIO_READ
,
896 uio_bufp
, UIO_SIZEOF(i
));
906 panic("nfsrv_read iov");
907 siz
= min(mbuf_trailingspace(m
), left
);
910 uio_addiov(uiop
, CAST_USER_ADDR_T((char *)mbuf_data(m
) + tlen
), siz
);
911 mbuf_setlen(m
, tlen
+ siz
);
917 error
= VNOP_READ(vp
, uiop
, IO_NODELOCKED
, &context
);
918 off
= uio_offset(uiop
);
921 * This may seem a little weird that we drop the whole
922 * successful read if we get an error on the getattr.
923 * The reason is because we've already set up the reply
924 * to have postop attrs and omitting these optional bits
925 * would require shifting all the data in the reply.
927 * It would be more correct if we would simply drop the
928 * postop attrs if the getattr fails. We might be able to
929 * do that easier if we allocated separate mbufs for the data.
931 nfsm_srv_vattr_init(vap
, v3
);
932 if (error
|| (getret
= vnode_getattr(vp
, vap
, &context
))) {
937 nfsm_reply(NFSX_POSTOPATTR(v3
));
938 nfsm_srvpostop_attr(getret
, vap
);
939 if (uio_bufp
!= NULL
) {
940 FREE(uio_bufp
, M_TEMP
);
945 uiop
= uio_createwithbuffer(0, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
952 nfsm_srvfillattr(vap
, fp
);
953 // LP64todo - fix this
954 len
-= uio_resid(uiop
);
955 tlen
= nfsm_rndup(len
);
956 if (count
!= tlen
|| tlen
!= len
)
957 nfsm_adj(mb
, count
- tlen
, tlen
- len
);
959 *tl
++ = txdr_unsigned(len
);
965 *tl
= txdr_unsigned(len
);
967 if (uio_bufp
!= NULL
) {
968 FREE(uio_bufp
, M_TEMP
);
977 nfsrv_write(nfsd
, slp
, procp
, mrq
)
978 struct nfsrv_descript
*nfsd
;
979 struct nfssvc_sock
*slp
;
983 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
984 mbuf_t nam
= nfsd
->nd_nam
;
985 caddr_t dpos
= nfsd
->nd_dpos
;
988 struct nfs_fattr
*fp
;
989 struct vnode_attr va
, forat
;
990 struct vnode_attr
*vap
= &va
;
994 int error
= 0, len
, forat_ret
= 1;
995 int ioflags
, aftat_ret
= 1, retlen
, zeroing
, adjust
, tlen
;
996 int stable
= NFSV3WRITE_FILESYNC
;
997 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
999 mbuf_t mb
, mb2
, mreq
;
1001 struct nfs_filehandle nfh
;
1002 struct nfs_export
*nx
;
1003 struct nfs_export_options
*nxo
;
1006 char *uio_bufp
= NULL
;
1007 struct vfs_context context
;
1013 nfsm_srvmtofh(&nfh
);
1015 nfsm_dissect(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1016 fxdr_hyper(tl
, &off
);
1018 stable
= fxdr_unsigned(int, *tl
++);
1020 nfsm_dissect(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1021 off
= (off_t
)fxdr_unsigned(u_long
, *++tl
);
1024 stable
= NFSV3WRITE_UNSTABLE
;
1026 retlen
= len
= fxdr_unsigned(long, *tl
);
1030 * For NFS Version 2, it is not obvious what a write of zero length
1031 * should do, but I might as well be consistent with Version 3,
1032 * which is to return ok so long as there are no permission problems.
1040 tpos
= mbuf_data(mp
);
1041 tlen
= mbuf_len(mp
);
1042 adjust
= dpos
- tpos
;
1044 mbuf_setlen(mp
, tlen
);
1045 if (tlen
> 0 && adjust
> 0) {
1047 if ((error
= mbuf_setdata(mp
, tpos
, tlen
))) {
1048 nfsm_reply(2 * NFSX_UNSIGNED
);
1049 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1056 else if ((tlen
= mbuf_len(mp
)) > 0) {
1059 mbuf_setlen(mp
, tlen
- (i
- len
));
1062 if (mbuf_len(mp
) > 0)
1068 if (len
> NFS_MAXDATA
|| len
< 0 || i
< len
) {
1070 nfsm_reply(2 * NFSX_UNSIGNED
);
1071 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1074 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
1075 nfsm_reply(2 * NFSX_UNSIGNED
);
1076 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1079 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
1081 nfsm_reply(2 * NFSX_UNSIGNED
);
1082 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1085 context
.vc_proc
= procp
;
1086 context
.vc_ucred
= nfsd
->nd_cr
;
1089 nfsm_srv_pre_vattr_init(&forat
, v3
);
1090 forat_ret
= vnode_getattr(vp
, &forat
, &context
);
1092 if (vnode_vtype(vp
) != VREG
) {
1096 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1099 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
, nxo
, 1);
1103 nfsm_reply(NFSX_WCCDATA(v3
));
1104 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1109 MALLOC(uio_bufp
, char *, UIO_SIZEOF(count
), M_TEMP
, M_WAITOK
);
1113 nfsm_reply(NFSX_WCCDATA(v3
));
1114 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1117 uiop
= uio_createwithbuffer(count
, off
, UIO_SYSSPACE
, UIO_WRITE
, uio_bufp
, UIO_SIZEOF(count
));
1121 nfsm_reply(NFSX_WCCDATA(v3
));
1122 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1123 if (uio_bufp
!= NULL
) {
1124 FREE(uio_bufp
, M_TEMP
);
1130 if ((tlen
= mbuf_len(mp
)) > 0)
1131 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), tlen
);
1137 * The IO_METASYNC flag indicates that all metadata (and not just
1138 * enough to ensure data integrity) mus be written to stable storage
1140 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1142 if (stable
== NFSV3WRITE_UNSTABLE
)
1143 ioflags
= IO_NODELOCKED
;
1144 else if (stable
== NFSV3WRITE_DATASYNC
)
1145 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1147 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1149 error
= VNOP_WRITE(vp
, uiop
, ioflags
, &context
);
1150 OSAddAtomic(1, (SInt32
*)(SInt32
*)&nfsstats
.srvvop_writes
);
1152 nfsm_srv_vattr_init(vap
, v3
);
1153 aftat_ret
= vnode_getattr(vp
, vap
, &context
);
1157 nfsm_reply(NFSX_PREOPATTR(v3
) + NFSX_POSTOPORFATTR(v3
) +
1158 2 * NFSX_UNSIGNED
+ NFSX_WRITEVERF(v3
));
1160 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1162 if (uio_bufp
!= NULL
) {
1163 FREE(uio_bufp
, M_TEMP
);
1167 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1168 *tl
++ = txdr_unsigned(retlen
);
1170 * If nfs_async is set, then pretend the write was FILESYNC.
1172 if (stable
== NFSV3WRITE_UNSTABLE
&& !nfs_async
)
1173 *tl
++ = txdr_unsigned(stable
);
1175 *tl
++ = txdr_unsigned(NFSV3WRITE_FILESYNC
);
1176 /* write verifier */
1177 *tl
++ = txdr_unsigned(boottime_sec());
1178 *tl
= txdr_unsigned(0);
1180 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
1181 nfsm_srvfillattr(vap
, fp
);
1184 if (uio_bufp
!= NULL
) {
1185 FREE(uio_bufp
, M_TEMP
);
1191 * NFS write service with write gathering support. Called when
1192 * nfsrvw_procrastinate > 0.
1193 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1194 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1198 nfsrv_writegather(ndp
, slp
, procp
, mrq
)
1199 struct nfsrv_descript
**ndp
;
1200 struct nfssvc_sock
*slp
;
1205 struct nfsrv_descript
*wp
, *nfsd
, *owp
, *swp
;
1206 struct nfs_export
*nx
;
1207 struct nfs_export_options
*nxo
;
1208 struct nfs_fattr
*fp
;
1210 struct nfsrvw_delayhash
*wpp
;
1212 struct vnode_attr va
, forat
;
1215 caddr_t bpos
, dpos
, tpos
;
1216 int error
= 0, len
, forat_ret
= 1;
1217 int ioflags
, aftat_ret
= 1, adjust
, v3
, zeroing
, tlen
;
1219 mbuf_t mb
, mb2
, mreq
, mrep
, md
;
1222 char *uio_bufp
= NULL
;
1225 struct vfs_context context
;
1227 context
.vc_proc
= procp
;
1238 mrep
= nfsd
->nd_mrep
;
1240 dpos
= nfsd
->nd_dpos
;
1242 context
.vc_ucred
= cred
;
1243 v3
= (nfsd
->nd_flag
& ND_NFSV3
);
1244 LIST_INIT(&nfsd
->nd_coalesce
);
1245 nfsd
->nd_mreq
= NULL
;
1246 nfsd
->nd_stable
= NFSV3WRITE_FILESYNC
;
1248 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1249 nfsd
->nd_time
= cur_usec
+
1250 (v3
? nfsrvw_procrastinate_v3
: nfsrvw_procrastinate
);
1253 * Now, get the write header..
1255 nfsm_srvmtofh(&nfsd
->nd_fh
);
1256 /* XXX shouldn't we be checking for invalid FHs before doing any more work? */
1258 nfsm_dissect(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1259 fxdr_hyper(tl
, &nfsd
->nd_off
);
1261 nfsd
->nd_stable
= fxdr_unsigned(int, *tl
++);
1263 nfsm_dissect(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1264 nfsd
->nd_off
= (off_t
)fxdr_unsigned(u_long
, *++tl
);
1267 nfsd
->nd_stable
= NFSV3WRITE_UNSTABLE
;
1269 len
= fxdr_unsigned(long, *tl
);
1271 nfsd
->nd_eoff
= nfsd
->nd_off
+ len
;
1274 * Trim the header out of the mbuf list and trim off any trailing
1275 * junk so that the mbuf list has only the write data.
1283 tpos
= mbuf_data(mp
);
1284 tlen
= mbuf_len(mp
);
1285 adjust
= dpos
- tpos
;
1287 mbuf_setlen(mp
, tlen
);
1288 if (tlen
> 0 && adjust
> 0) {
1290 if ((error
= mbuf_setdata(mp
, tpos
, tlen
)))
1297 tlen
= mbuf_len(mp
);
1300 mbuf_setlen(mp
, tlen
- (i
- len
));
1306 if (len
> NFS_MAXDATA
|| len
< 0 || i
< len
) {
1311 nfsm_writereply(2 * NFSX_UNSIGNED
, v3
);
1313 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1314 nfsd
->nd_mreq
= mreq
;
1315 nfsd
->nd_mrep
= NULL
;
1320 * Add this entry to the hash and time queues.
1322 lck_mtx_lock(&slp
->ns_wgmutex
);
1324 wp
= slp
->ns_tq
.lh_first
;
1325 while (wp
&& wp
->nd_time
< nfsd
->nd_time
) {
1327 wp
= wp
->nd_tq
.le_next
;
1330 LIST_INSERT_AFTER(owp
, nfsd
, nd_tq
);
1332 LIST_INSERT_HEAD(&slp
->ns_tq
, nfsd
, nd_tq
);
1334 if (nfsd
->nd_mrep
) {
1335 wpp
= NWDELAYHASH(slp
, nfsd
->nd_fh
.nfh_fid
);
1338 while (wp
&& !nfsrv_fhmatch(&nfsd
->nd_fh
, &wp
->nd_fh
)) {
1340 wp
= wp
->nd_hash
.le_next
;
1342 while (wp
&& (wp
->nd_off
< nfsd
->nd_off
) &&
1343 nfsrv_fhmatch(&nfsd
->nd_fh
, &wp
->nd_fh
)) {
1345 wp
= wp
->nd_hash
.le_next
;
1348 LIST_INSERT_AFTER(owp
, nfsd
, nd_hash
);
1351 * Search the hash list for overlapping entries and
1354 for(; nfsd
&& NFSW_CONTIG(owp
, nfsd
); nfsd
= wp
) {
1355 wp
= nfsd
->nd_hash
.le_next
;
1356 if (NFSW_SAMECRED(owp
, nfsd
))
1357 nfsrvw_coalesce(owp
, nfsd
);
1360 LIST_INSERT_HEAD(wpp
, nfsd
, nd_hash
);
1364 lck_mtx_lock(&slp
->ns_wgmutex
);
1368 * Now, do VNOP_WRITE()s for any one(s) that need to be done now
1369 * and generate the associated reply mbuf list(s).
1373 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1374 for (nfsd
= slp
->ns_tq
.lh_first
; nfsd
; nfsd
= owp
) {
1375 owp
= nfsd
->nd_tq
.le_next
;
1376 if (nfsd
->nd_time
> cur_usec
)
1380 LIST_REMOVE(nfsd
, nd_tq
);
1381 LIST_REMOVE(nfsd
, nd_hash
);
1382 mrep
= nfsd
->nd_mrep
;
1383 nfsd
->nd_mrep
= NULL
;
1384 v3
= (nfsd
->nd_flag
& ND_NFSV3
);
1385 forat_ret
= aftat_ret
= 1;
1386 error
= nfsrv_fhtovp(&nfsd
->nd_fh
, nfsd
->nd_nam
, TRUE
, &vp
, &nx
, &nxo
);
1388 error
= nfsrv_credcheck(nfsd
, nx
, nxo
);
1393 context
.vc_ucred
= cred
;
1396 nfsm_srv_pre_vattr_init(&forat
, v3
);
1397 forat_ret
= vnode_getattr(vp
, &forat
, &context
);
1399 if (vnode_vtype(vp
) != VREG
) {
1403 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1408 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
, nxo
, 1);
1411 if (nfsd
->nd_stable
== NFSV3WRITE_UNSTABLE
)
1412 ioflags
= IO_NODELOCKED
;
1413 else if (nfsd
->nd_stable
== NFSV3WRITE_DATASYNC
)
1414 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1416 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1418 if (!error
&& ((nfsd
->nd_eoff
- nfsd
->nd_off
) > 0)) {
1422 if (mbuf_len(mp
) > 0)
1427 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
1429 uiop
= uio_createwithbuffer(i
, nfsd
->nd_off
, UIO_SYSSPACE
,
1430 UIO_WRITE
, uio_bufp
, UIO_SIZEOF(i
));
1431 if (!uio_bufp
|| !uiop
)
1436 if ((tlen
= mbuf_len(mp
)) > 0)
1437 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), tlen
);
1440 error
= VNOP_WRITE(vp
, uiop
, ioflags
, &context
);
1441 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvvop_writes
);
1444 FREE(uio_bufp
, M_TEMP
);
1451 nfsm_srv_pre_vattr_init(&va
, v3
);
1452 aftat_ret
= vnode_getattr(vp
, &va
, &context
);
1457 * Loop around generating replies for all write rpcs that have
1458 * now been completed.
1463 nfsm_writereply(NFSX_WCCDATA(v3
), v3
);
1465 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1468 nfsm_writereply(NFSX_PREOPATTR(v3
) +
1469 NFSX_POSTOPORFATTR(v3
) + 2 * NFSX_UNSIGNED
+
1470 NFSX_WRITEVERF(v3
), v3
);
1472 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1473 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1474 *tl
++ = txdr_unsigned(nfsd
->nd_len
);
1475 *tl
++ = txdr_unsigned(swp
->nd_stable
);
1476 /* write verifier */
1477 *tl
++ = txdr_unsigned(boottime_sec());
1478 *tl
= txdr_unsigned(0);
1480 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
1481 nfsm_srvfillattr(&va
, fp
);
1484 nfsd
->nd_mreq
= mreq
;
1486 panic("nfsrv_write: nd_mrep not free");
1489 * Done. Put it at the head of the timer queue so that
1490 * the final phase can return the reply.
1494 LIST_INSERT_HEAD(&slp
->ns_tq
, nfsd
, nd_tq
);
1496 nfsd
= swp
->nd_coalesce
.lh_first
;
1498 LIST_REMOVE(nfsd
, nd_tq
);
1502 LIST_INSERT_HEAD(&slp
->ns_tq
, swp
, nd_tq
);
1507 * Search for a reply to return.
1509 for (nfsd
= slp
->ns_tq
.lh_first
; nfsd
; nfsd
= nfsd
->nd_tq
.le_next
)
1510 if (nfsd
->nd_mreq
) {
1511 LIST_REMOVE(nfsd
, nd_tq
);
1512 *mrq
= nfsd
->nd_mreq
;
1516 slp
->ns_wgtime
= slp
->ns_tq
.lh_first
? slp
->ns_tq
.lh_first
->nd_time
: 0;
1517 lck_mtx_unlock(&slp
->ns_wgmutex
);
1522 * Coalesce the write request nfsd into owp. To do this we must:
1523 * - remove nfsd from the queues
1524 * - merge nfsd->nd_mrep into owp->nd_mrep
1525 * - update the nd_eoff and nd_stable for owp
1526 * - put nfsd on owp's nd_coalesce list
1530 struct nfsrv_descript
*owp
,
1531 struct nfsrv_descript
*nfsd
)
1535 struct nfsrv_descript
*p
;
1537 LIST_REMOVE(nfsd
, nd_hash
);
1538 LIST_REMOVE(nfsd
, nd_tq
);
1539 if (owp
->nd_eoff
< nfsd
->nd_eoff
) {
1540 overlap
= owp
->nd_eoff
- nfsd
->nd_off
;
1542 panic("nfsrv_coalesce: bad off");
1544 mbuf_adj(nfsd
->nd_mrep
, overlap
);
1546 while ((mpnext
= mbuf_next(mp
)))
1548 error
= mbuf_setnext(mp
, nfsd
->nd_mrep
);
1550 panic("nfsrvw_coalesce: mbuf_setnext failed: %d", error
);
1551 owp
->nd_eoff
= nfsd
->nd_eoff
;
1553 mbuf_freem(nfsd
->nd_mrep
);
1555 nfsd
->nd_mrep
= NULL
;
1556 if (nfsd
->nd_stable
== NFSV3WRITE_FILESYNC
)
1557 owp
->nd_stable
= NFSV3WRITE_FILESYNC
;
1558 else if (nfsd
->nd_stable
== NFSV3WRITE_DATASYNC
&&
1559 owp
->nd_stable
== NFSV3WRITE_UNSTABLE
)
1560 owp
->nd_stable
= NFSV3WRITE_DATASYNC
;
1561 LIST_INSERT_HEAD(&owp
->nd_coalesce
, nfsd
, nd_tq
);
1564 * If nfsd had anything else coalesced into it, transfer them
1565 * to owp, otherwise their replies will never get sent.
1567 for (p
= nfsd
->nd_coalesce
.lh_first
; p
;
1568 p
= nfsd
->nd_coalesce
.lh_first
) {
1569 LIST_REMOVE(p
, nd_tq
);
1570 LIST_INSERT_HEAD(&owp
->nd_coalesce
, p
, nd_tq
);
1575 * Sort the group list in increasing numerical order.
1576 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1577 * that used to be here.)
1582 nfsrvw_sort(list
, num
)
1589 /* Insertion sort. */
1590 for (i
= 1; i
< num
; i
++) {
1592 /* find correct slot for value v, moving others up */
1593 for (j
= i
; --j
>= 0 && v
< list
[j
];)
1594 list
[j
+ 1] = list
[j
];
1600 * copy credentials making sure that the result can be compared with bcmp().
1605 nfsrv_setcred(kauth_cred_t incred
, kauth_cred_t outcred
)
1609 bzero((caddr_t
)outcred
, sizeof (*outcred
));
1610 outcred
->cr_ref
= 1;
1611 outcred
->cr_uid
= kauth_cred_getuid(incred
);
1612 outcred
->cr_ngroups
= incred
->cr_ngroups
;
1613 for (i
= 0; i
< incred
->cr_ngroups
; i
++)
1614 outcred
->cr_groups
[i
] = incred
->cr_groups
[i
];
1615 nfsrvw_sort(outcred
->cr_groups
, outcred
->cr_ngroups
);
1619 * nfs create service
1620 * now does a truncate to 0 length via. setattr if it already exists
1623 nfsrv_create(nfsd
, slp
, procp
, mrq
)
1624 struct nfsrv_descript
*nfsd
;
1625 struct nfssvc_sock
*slp
;
1629 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
1630 mbuf_t nam
= nfsd
->nd_nam
;
1631 caddr_t dpos
= nfsd
->nd_dpos
;
1632 struct nfs_fattr
*fp
;
1633 struct vnode_attr dirfor
, diraft
, postat
;
1634 struct vnode_attr va
;
1635 struct vnode_attr
*vap
= &va
;
1636 struct nfsv2_sattr
*sp
;
1638 struct nameidata nd
;
1642 int error
= 0, rdev
, len
, tsize
, dirfor_ret
= 1, diraft_ret
= 1;
1643 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), how
, exclusive_flag
= 0;
1645 mbuf_t mb
, mb2
, mreq
;
1646 vnode_t vp
, dvp
, dirp
= NULL
;
1647 struct nfs_filehandle nfh
;
1648 struct nfs_export
*nx
;
1649 struct nfs_export_options
*nxo
;
1651 u_char cverf
[NFSX_V3CREATEVERF
];
1652 struct vfs_context context
;
1655 context
.vc_proc
= procp
;
1656 context
.vc_ucred
= nfsd
->nd_cr
;
1659 * Save the original credential UID in case they are
1660 * mapped and we need to map the IDs in the attributes.
1662 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
1667 nd
.ni_cnd
.cn_nameiop
= 0;
1669 nfsm_srvmtofh(&nfh
);
1670 nfsm_srvnamesiz(len
, v3
);
1672 nd
.ni_cnd
.cn_nameiop
= CREATE
;
1673 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
1674 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
1676 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
1679 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
1680 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
1687 nd
.ni_cnd
.cn_nameiop
= 0;
1688 nfsm_reply(NFSX_WCCDATA(v3
));
1689 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
1700 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1701 how
= fxdr_unsigned(int, *tl
);
1703 case NFSV3CREATE_GUARDED
:
1708 case NFSV3CREATE_UNCHECKED
:
1711 case NFSV3CREATE_EXCLUSIVE
:
1712 nfsm_dissect(cp
, caddr_t
, NFSX_V3CREATEVERF
);
1713 bcopy(cp
, cverf
, NFSX_V3CREATEVERF
);
1716 VATTR_SET(vap
, va_mode
, 0);
1719 VATTR_SET(vap
, va_type
, VREG
);
1723 nfsm_dissect(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
1724 v_type
= IFTOVT(fxdr_unsigned(u_long
, sp
->sa_mode
));
1727 VATTR_SET(vap
, va_type
, v_type
);
1728 VATTR_SET(vap
, va_mode
, nfstov_mode(sp
->sa_mode
));
1732 tsize
= fxdr_unsigned(long, sp
->sa_size
);
1734 VATTR_SET(vap
, va_data_size
, (u_quad_t
)tsize
);
1739 rdev
= fxdr_unsigned(long, sp
->sa_size
);
1747 * If it doesn't exist, create it
1748 * otherwise just truncate to 0 length
1749 * should I set the mode too ??
1752 kauth_acl_t xacl
= NULL
;
1755 * If the credentials were mapped, we should
1756 * map the same values in the attributes.
1758 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
1760 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
1761 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
1762 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
1765 /* authorize before creating */
1766 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
, nxo
, 0);
1768 /* construct ACL and handle inheritance */
1770 error
= kauth_acl_inherit(dvp
,
1776 if (!error
&& xacl
!= NULL
)
1777 VATTR_SET(vap
, va_acl
, xacl
);
1779 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1780 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
1782 /* validate new-file security information */
1784 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
1785 if (error
&& (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
))) {
1787 * Most NFS servers just ignore the UID/GID attributes, so we
1788 * try ignoring them if that'll help the request succeed.
1790 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1791 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1792 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
1796 if (vap
->va_type
== VREG
|| vap
->va_type
== VSOCK
) {
1799 error
= VNOP_CREATE(dvp
, &vp
, &nd
.ni_cnd
, vap
, &context
);
1801 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
1803 * If some of the requested attributes weren't handled by the VNOP,
1804 * use our fallback code.
1806 error
= vnode_setattr_fallback(vp
, vap
, &context
);
1809 kauth_acl_free(xacl
);
1812 if (exclusive_flag
) {
1815 bcopy(cverf
, (caddr_t
)&vap
->va_access_time
,
1817 VATTR_SET_ACTIVE(vap
, va_access_time
);
1818 // skip authorization, as this is an
1819 // NFS internal implementation detail.
1820 error
= vnode_setattr(vp
, vap
, &context
);
1824 } else if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
||
1825 vap
->va_type
== VFIFO
) {
1826 if (vap
->va_type
== VCHR
&& rdev
== (int)0xffffffff)
1827 VATTR_SET(vap
, va_type
, VFIFO
);
1828 if (vap
->va_type
!= VFIFO
&&
1829 (error
= suser(nfsd
->nd_cr
, (u_short
*)0))) {
1832 VATTR_SET(vap
, va_rdev
, (dev_t
)rdev
);
1834 error
= VNOP_MKNOD(dvp
, &vp
, &nd
.ni_cnd
, vap
, &context
);
1837 kauth_acl_free(xacl
);
1847 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
1848 nd
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
1849 nd
.ni_cnd
.cn_context
= &context
;
1850 nd
.ni_startdir
= dvp
;
1852 error
= lookup(&nd
);
1854 if (nd
.ni_cnd
.cn_flags
& ISSYMLINK
)
1864 * nameidone has to happen before we vnode_put(dvp)
1865 * since it may need to release the fs_nodelock on the dvp
1868 nd
.ni_cnd
.cn_nameiop
= 0;
1873 * nameidone has to happen before we vnode_put(dvp)
1874 * since it may need to release the fs_nodelock on the dvp
1877 nd
.ni_cnd
.cn_nameiop
= 0;
1881 if (!error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1882 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
,
1885 tempsize
= vap
->va_data_size
;
1887 VATTR_SET(vap
, va_data_size
, tempsize
);
1888 error
= vnode_setattr(vp
, vap
, &context
);
1893 error
= nfsrv_vptofh(nx
, !v3
, NULL
, vp
, &context
, &nfh
);
1895 nfsm_srv_vattr_init(&postat
, v3
);
1896 error
= vnode_getattr(vp
, &postat
, &context
);
1903 if (exclusive_flag
&& !error
&&
1904 bcmp(cverf
, (caddr_t
)&postat
.va_access_time
, NFSX_V3CREATEVERF
))
1906 nfsm_srv_vattr_init(&diraft
, v3
);
1907 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
1911 nfsm_reply(NFSX_SRVFH(v3
, &nfh
) + NFSX_FATTR(v3
) + NFSX_WCCDATA(v3
));
1915 nfsm_srvpostop_fh(&nfh
);
1916 nfsm_srvpostop_attr(0, &postat
);
1918 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
1920 nfsm_srvfhtom(&nfh
, v3
);
1921 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
1922 nfsm_srvfillattr(&postat
, fp
);
1926 if (nd
.ni_cnd
.cn_nameiop
) {
1928 * nameidone has to happen before we vnode_put(dvp)
1929 * since it may need to release the fs_nodelock on the dvp
1943 * nfs v3 mknod service
1946 nfsrv_mknod(nfsd
, slp
, procp
, mrq
)
1947 struct nfsrv_descript
*nfsd
;
1948 struct nfssvc_sock
*slp
;
1952 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
1953 mbuf_t nam
= nfsd
->nd_nam
;
1954 caddr_t dpos
= nfsd
->nd_dpos
;
1955 struct vnode_attr dirfor
, diraft
, postat
;
1956 struct vnode_attr va
;
1957 struct vnode_attr
*vap
= &va
;
1959 struct nameidata nd
;
1962 int error
= 0, len
, dirfor_ret
= 1, diraft_ret
= 1;
1963 u_long major
, minor
;
1966 mbuf_t mb
, mb2
, mreq
;
1967 vnode_t vp
, dvp
, dirp
= NULL
;
1968 struct nfs_filehandle nfh
;
1969 struct nfs_export
*nx
;
1970 struct nfs_export_options
*nxo
;
1971 struct vfs_context hacked_context
; /* XXX should we have this? */
1972 struct vfs_context context
;
1974 kauth_acl_t xacl
= NULL
;
1976 context
.vc_proc
= procp
;
1977 context
.vc_ucred
= nfsd
->nd_cr
;
1978 hacked_context
.vc_proc
= procp
;
1979 hacked_context
.vc_ucred
= proc_ucred(procp
);
1982 * Save the original credential UID in case they are
1983 * mapped and we need to map the IDs in the attributes.
1985 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
1988 nd
.ni_cnd
.cn_nameiop
= 0;
1989 nfsm_srvmtofh(&nfh
);
1990 nfsm_srvnamesiz(len
, 1);
1992 nd
.ni_cnd
.cn_nameiop
= CREATE
;
1993 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
1994 error
= nfsm_path_mbuftond(&md
, &dpos
, 1, FALSE
, &len
, &nd
);
1996 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
1998 nfsm_srv_pre_vattr_init(&dirfor
, 1);
1999 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
2002 nd
.ni_cnd
.cn_nameiop
= 0;
2003 nfsm_reply(NFSX_WCCDATA(1));
2004 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2012 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2013 vtyp
= nfsv3tov_type(*tl
);
2014 if (vtyp
!= VCHR
&& vtyp
!= VBLK
&& vtyp
!= VSOCK
&& vtyp
!= VFIFO
) {
2015 error
= NFSERR_BADTYPE
;
2021 if (vtyp
== VCHR
|| vtyp
== VBLK
) {
2022 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2023 major
= fxdr_unsigned(u_long
, *tl
++);
2024 minor
= fxdr_unsigned(u_long
, *tl
);
2025 VATTR_SET(vap
, va_rdev
, makedev(major
, minor
));
2029 * If it doesn't exist, create it.
2035 VATTR_SET(vap
, va_type
, vtyp
);
2038 * If the credentials were mapped, we should
2039 * map the same values in the attributes.
2041 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
2043 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
2044 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
2045 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
2048 /* authorize before creating */
2049 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
, nxo
, 0);
2051 /* construct ACL and handle inheritance */
2053 error
= kauth_acl_inherit(dvp
,
2059 if (!error
&& xacl
!= NULL
)
2060 VATTR_SET(vap
, va_acl
, xacl
);
2062 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
2063 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
2065 /* validate new-file security information */
2067 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
2068 if (error
&& (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
))) {
2070 * Most NFS servers just ignore the UID/GID attributes, so we
2071 * try ignoring them if that'll help the request succeed.
2073 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
2074 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
2075 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
2079 if (vtyp
== VSOCK
) {
2080 error
= VNOP_CREATE(dvp
, &vp
, &nd
.ni_cnd
, vap
, &context
);
2082 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
2084 * If some of the requested attributes weren't handled by the VNOP,
2085 * use our fallback code.
2087 error
= vnode_setattr_fallback(vp
, vap
, &context
);
2089 if (vtyp
!= VFIFO
&& (error
= suser(nfsd
->nd_cr
, (u_short
*)0))) {
2092 if ((error
= VNOP_MKNOD(dvp
, &vp
, &nd
.ni_cnd
, vap
, &context
))) {
2100 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
2101 nd
.ni_cnd
.cn_flags
&= ~LOCKPARENT
;
2102 nd
.ni_cnd
.cn_context
= &hacked_context
;
2103 nd
.ni_startdir
= dvp
;
2105 error
= lookup(&nd
);
2108 if (nd
.ni_cnd
.cn_flags
& ISSYMLINK
)
2114 kauth_acl_free(xacl
);
2117 * nameidone has to happen before we vnode_put(dvp)
2118 * since it may need to release the fs_nodelock on the dvp
2121 nd
.ni_cnd
.cn_nameiop
= 0;
2126 error
= nfsrv_vptofh(nx
, 0, NULL
, vp
, &context
, &nfh
);
2128 nfsm_srv_vattr_init(&postat
, 1);
2129 error
= vnode_getattr(vp
, &postat
, &context
);
2135 nfsm_srv_vattr_init(&diraft
, 1);
2136 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
2140 nfsm_reply(NFSX_SRVFH(1, &nfh
) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2142 nfsm_srvpostop_fh(&nfh
);
2143 nfsm_srvpostop_attr(0, &postat
);
2145 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2148 if (nd
.ni_cnd
.cn_nameiop
) {
2150 * nameidone has to happen before we vnode_put(dvp)
2151 * since it may need to release the fs_nodelock on the dvp
2165 * nfs remove service
2168 nfsrv_remove(nfsd
, slp
, procp
, mrq
)
2169 struct nfsrv_descript
*nfsd
;
2170 struct nfssvc_sock
*slp
;
2174 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
2175 mbuf_t nam
= nfsd
->nd_nam
;
2176 caddr_t dpos
= nfsd
->nd_dpos
;
2177 struct nameidata nd
;
2181 int error
= 0, len
, dirfor_ret
= 1, diraft_ret
= 1;
2182 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
2185 vnode_t vp
, dvp
, dirp
= NULL
;
2186 struct vnode_attr dirfor
, diraft
;
2187 struct nfs_filehandle nfh
;
2188 struct nfs_export
*nx
;
2189 struct nfs_export_options
*nxo
;
2190 struct vfs_context context
;
2192 context
.vc_proc
= procp
;
2193 context
.vc_ucred
= nfsd
->nd_cr
;
2196 nfsm_srvmtofh(&nfh
);
2197 nfsm_srvnamesiz(len
, v3
);
2199 nd
.ni_cnd
.cn_nameiop
= DELETE
;
2200 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
2201 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
2203 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
2206 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
2207 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
2217 if (vnode_vtype(vp
) == VDIR
)
2218 error
= EPERM
; /* POSIX */
2219 else if (vnode_isvroot(vp
))
2221 * The root of a mounted filesystem cannot be deleted.
2225 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, &context
, nxo
, 0);
2228 error
= VNOP_REMOVE(dvp
, vp
, &nd
.ni_cnd
, 0, &context
);
2231 * nameidone has to happen before we vnode_put(dvp)
2232 * since it may need to release the fs_nodelock on the dvp
2240 nfsm_srv_vattr_init(&diraft
, v3
);
2241 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
2244 nfsm_reply(NFSX_WCCDATA(v3
));
2246 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2254 * nfs rename service
2257 nfsrv_rename(nfsd
, slp
, procp
, mrq
)
2258 struct nfsrv_descript
*nfsd
;
2259 struct nfssvc_sock
*slp
;
2263 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
2264 mbuf_t nam
= nfsd
->nd_nam
;
2265 caddr_t dpos
= nfsd
->nd_dpos
;
2266 kauth_cred_t saved_cred
= NULL
;
2270 int error
= 0, fromlen
, tolen
;
2271 int fdirfor_ret
= 1, fdiraft_ret
= 1;
2272 int tdirfor_ret
= 1, tdiraft_ret
= 1;
2273 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
2274 char *cp2
, *frompath
= NULL
, *topath
= NULL
;
2276 struct nameidata fromnd
, tond
;
2277 vnode_t fvp
, tvp
, tdvp
, fdvp
, fdirp
= NULL
;
2278 vnode_t tdirp
= NULL
;
2279 struct vnode_attr fdirfor
, fdiraft
, tdirfor
, tdiraft
;
2280 struct nfs_filehandle fnfh
, tnfh
;
2281 struct nfs_export
*fnx
, *tnx
;
2282 struct nfs_export_options
*fnxo
, *tnxo
;
2283 enum vtype fvtype
, tvtype
;
2284 int holding_mntlock
;
2286 struct vfs_context context
;
2288 context
.vc_proc
= procp
;
2289 context
.vc_ucred
= nfsd
->nd_cr
;
2296 * these need to be set before
2297 * calling any nfsm_xxxx macros
2298 * since they may take us out
2299 * through the error path
2301 holding_mntlock
= 0;
2306 nfsm_srvmtofh(&fnfh
);
2307 nfsm_srvnamesiz(fromlen
, v3
);
2308 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &fromlen
, &fromnd
);
2313 frompath
= fromnd
.ni_cnd
.cn_pnbuf
;
2314 nfsm_srvmtofh(&tnfh
);
2315 nfsm_strsiz(tolen
, NFS_MAXNAMLEN
, v3
);
2316 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &tolen
, &tond
);
2319 FREE_ZONE(frompath
, MAXPATHLEN
, M_NAMEI
);
2322 topath
= tond
.ni_cnd
.cn_pnbuf
;
2325 * Remember our original uid so that we can reset cr_uid before
2326 * the second nfs_namei() call, in case it is remapped.
2328 saved_cred
= nfsd
->nd_cr
;
2329 kauth_cred_ref(saved_cred
);
2331 fromnd
.ni_cnd
.cn_nameiop
= DELETE
;
2332 fromnd
.ni_cnd
.cn_flags
= WANTPARENT
;
2334 fromnd
.ni_cnd
.cn_pnbuf
= frompath
;
2336 fromnd
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2337 fromnd
.ni_cnd
.cn_flags
|= HASBUF
;
2339 error
= nfs_namei(nfsd
, &context
, &fromnd
, &fnfh
, nam
, FALSE
, &fdirp
, &fnx
, &fnxo
);
2342 fdvp
= fromnd
.ni_dvp
;
2347 nfsm_srv_pre_vattr_init(&fdirfor
, v3
);
2348 fdirfor_ret
= vnode_getattr(fdirp
, &fdirfor
, &context
);
2354 fvtype
= vnode_vtype(fvp
);
2356 /* reset credential if it was remapped */
2357 if (nfsd
->nd_cr
!= saved_cred
) {
2358 kauth_cred_rele(nfsd
->nd_cr
);
2359 nfsd
->nd_cr
= saved_cred
;
2360 kauth_cred_ref(nfsd
->nd_cr
);
2363 tond
.ni_cnd
.cn_nameiop
= RENAME
;
2364 tond
.ni_cnd
.cn_flags
= WANTPARENT
;
2366 tond
.ni_cnd
.cn_pnbuf
= topath
;
2368 tond
.ni_cnd
.cn_pnlen
= MAXPATHLEN
;
2369 tond
.ni_cnd
.cn_flags
|= HASBUF
;
2372 tond
.ni_cnd
.cn_flags
|= WILLBEDIR
;
2374 error
= nfs_namei(nfsd
, &context
, &tond
, &tnfh
, nam
, FALSE
, &tdirp
, &tnx
, &tnxo
);
2377 * Translate error code for rename("dir1", "dir2/.").
2379 if (error
== EISDIR
&& fvtype
== VDIR
) {
2392 nfsm_srv_pre_vattr_init(&tdirfor
, v3
);
2393 tdirfor_ret
= vnode_getattr(tdirp
, &tdirfor
, &context
);
2401 tvtype
= vnode_vtype(tvp
);
2403 if (fvtype
== VDIR
&& tvtype
!= VDIR
) {
2409 } else if (fvtype
!= VDIR
&& tvtype
== VDIR
) {
2416 if (tvtype
== VDIR
&& vnode_mountedhere(tvp
)) {
2435 * If tvp is a directory and not the same as fdvp, or tdvp is not the same as fdvp,
2436 * the node is moving between directories and we need rights to remove from the
2437 * old and add to the new.
2439 * If tvp already exists and is not a directory, we need to be allowed to delete it.
2441 * Note that we do not inherit when renaming. XXX this needs to be revisited to
2442 * implement the deferred-inherit bit.
2448 if ((tvp
!= NULL
) && vnode_isdir(tvp
)) {
2451 } else if (tdvp
!= fdvp
) {
2455 /* moving out of fdvp, must have delete rights */
2456 if ((error
= nfsrv_authorize(fvp
, fdvp
, KAUTH_VNODE_DELETE
, &context
, fnxo
, 0)) != 0)
2458 /* moving into tdvp or tvp, must have rights to add */
2459 if ((error
= nfsrv_authorize(((tvp
!= NULL
) && vnode_isdir(tvp
)) ? tvp
: tdvp
,
2461 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2462 &context
, tnxo
, 0)) != 0)
2465 /* node staying in same directory, must be allowed to add new name */
2466 if ((error
= nfsrv_authorize(fdvp
, NULL
,
2467 vnode_isdir(fvp
) ? KAUTH_VNODE_ADD_SUBDIRECTORY
: KAUTH_VNODE_ADD_FILE
,
2468 &context
, fnxo
, 0)) != 0)
2471 /* overwriting tvp */
2472 if ((tvp
!= NULL
) && !vnode_isdir(tvp
) &&
2473 ((error
= nfsrv_authorize(tvp
, tdvp
, KAUTH_VNODE_DELETE
, &context
, tnxo
, 0)) != 0))
2476 /* XXX more checks? */
2479 /* authorization denied */
2484 if ((vnode_mount(fvp
) != vnode_mount(tdvp
)) ||
2485 (tvp
&& (vnode_mount(fvp
) != vnode_mount(tvp
)))) {
2493 * The following edge case is caught here:
2494 * (to cannot be a descendent of from)
2507 if (tdvp
->v_parent
== fvp
) {
2514 if (fvtype
== VDIR
&& vnode_mountedhere(fvp
)) {
2522 * If source is the same as the destination (that is the
2523 * same vnode) then there is nothing to do...
2524 * EXCEPT if the underlying file system supports case
2525 * insensitivity and is case preserving. In this case
2526 * the file system needs to handle the special case of
2527 * getting the same vnode as target (fvp) and source (tvp).
2529 * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE
2530 * and _PC_CASE_PRESERVING can have this exception, and they need to
2531 * handle the special case of getting the same vnode as target and
2532 * source. NOTE: Then the target is unlocked going into vnop_rename,
2533 * so not to cause locking problems. There is a single reference on tvp.
2535 * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE
2536 * that correct behaviour then is just to remove the source (link)
2538 if ((fvp
== tvp
) && (fdvp
== tdvp
)) {
2539 if (fromnd
.ni_cnd
.cn_namelen
== tond
.ni_cnd
.cn_namelen
&&
2540 !bcmp(fromnd
.ni_cnd
.cn_nameptr
, tond
.ni_cnd
.cn_nameptr
,
2541 fromnd
.ni_cnd
.cn_namelen
)) {
2546 if (holding_mntlock
&& vnode_mount(fvp
) != locked_mp
) {
2548 * we're holding a reference and lock
2549 * on locked_mp, but it no longer matches
2550 * what we want to do... so drop our hold
2552 mount_unlock_renames(locked_mp
);
2553 mount_drop(locked_mp
, 0);
2554 holding_mntlock
= 0;
2556 if (tdvp
!= fdvp
&& fvtype
== VDIR
) {
2558 * serialize renames that re-shape
2559 * the tree... if holding_mntlock is
2560 * set, then we're ready to go...
2562 * first need to drop the iocounts
2563 * we picked up, second take the
2564 * lock to serialize the access,
2565 * then finally start the lookup
2566 * process over with the lock held
2568 if (!holding_mntlock
) {
2570 * need to grab a reference on
2571 * the mount point before we
2572 * drop all the iocounts... once
2573 * the iocounts are gone, the mount
2576 locked_mp
= vnode_mount(fvp
);
2577 mount_ref(locked_mp
, 0);
2579 /* make a copy of to path to pass to nfs_namei() again */
2580 MALLOC_ZONE(topath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2582 bcopy(tond
.ni_cnd
.cn_pnbuf
, topath
, tolen
+ 1);
2585 * nameidone has to happen before we vnode_put(tdvp)
2586 * since it may need to release the fs_nodelock on the tdvp
2594 /* make a copy of from path to pass to nfs_namei() again */
2595 MALLOC_ZONE(frompath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2597 bcopy(fromnd
.ni_cnd
.cn_pnbuf
, frompath
, fromlen
+ 1);
2600 * nameidone has to happen before we vnode_put(fdvp)
2601 * since it may need to release the fs_nodelock on the fdvp
2616 mount_lock_renames(locked_mp
);
2617 holding_mntlock
= 1;
2622 fdirfor_ret
= tdirfor_ret
= 1;
2624 if (!topath
|| !frompath
) {
2625 /* we couldn't allocate a path, so bail */
2634 * when we dropped the iocounts to take
2635 * the lock, we allowed the identity of
2636 * the various vnodes to change... if they did,
2637 * we may no longer be dealing with a rename
2638 * that reshapes the tree... once we're holding
2639 * the iocounts, the vnodes can't change type
2640 * so we're free to drop the lock at this point
2643 if (holding_mntlock
) {
2644 mount_unlock_renames(locked_mp
);
2645 mount_drop(locked_mp
, 0);
2646 holding_mntlock
= 0;
2650 // save these off so we can later verify that fvp is the same
2653 oname
= fvp
->v_name
;
2654 oparent
= fvp
->v_parent
;
2656 error
= VNOP_RENAME(fromnd
.ni_dvp
, fromnd
.ni_vp
, &fromnd
.ni_cnd
,
2657 tond
.ni_dvp
, tond
.ni_vp
, &tond
.ni_cnd
, &context
);
2659 * fix up name & parent pointers. note that we first
2660 * check that fvp has the same name/parent pointers it
2661 * had before the rename call... this is a 'weak' check
2664 if (oname
== fvp
->v_name
&& oparent
== fvp
->v_parent
) {
2666 update_flags
= VNODE_UPDATE_NAME
;
2668 update_flags
|= VNODE_UPDATE_PARENT
;
2669 vnode_update_identity(fvp
, tdvp
, tond
.ni_cnd
.cn_nameptr
, tond
.ni_cnd
.cn_namelen
, tond
.ni_cnd
.cn_hash
, update_flags
);
2672 if (holding_mntlock
) {
2673 mount_unlock_renames(locked_mp
);
2674 mount_drop(locked_mp
, 0);
2675 holding_mntlock
= 0;
2679 * nameidone has to happen before we vnode_put(tdvp)
2680 * since it may need to release the fs_nodelock on the tdvp
2691 * nameidone has to happen before we vnode_put(fdvp)
2692 * since it may need to release the fs_nodelock on the fdvp
2703 nfsm_srv_vattr_init(&fdiraft
, v3
);
2704 fdiraft_ret
= vnode_getattr(fdirp
, &fdiraft
, &context
);
2709 nfsm_srv_vattr_init(&tdiraft
, v3
);
2710 tdiraft_ret
= vnode_getattr(tdirp
, &tdiraft
, &context
);
2714 nfsm_reply(2 * NFSX_WCCDATA(v3
));
2716 nfsm_srvwcc_data(fdirfor_ret
, &fdirfor
, fdiraft_ret
, &fdiraft
);
2717 nfsm_srvwcc_data(tdirfor_ret
, &tdirfor
, tdiraft_ret
, &tdiraft
);
2720 FREE_ZONE(frompath
, MAXPATHLEN
, M_NAMEI
);
2722 FREE_ZONE(topath
, MAXPATHLEN
, M_NAMEI
);
2724 kauth_cred_rele(saved_cred
);
2728 if (holding_mntlock
) {
2729 mount_unlock_renames(locked_mp
);
2730 mount_drop(locked_mp
, 0);
2734 * nameidone has to happen before we vnode_put(tdvp)
2735 * since it may need to release the fs_nodelock on the tdvp
2745 * nameidone has to happen before we vnode_put(fdvp)
2746 * since it may need to release the fs_nodelock on the fdvp
2759 FREE_ZONE(frompath
, MAXPATHLEN
, M_NAMEI
);
2761 FREE_ZONE(topath
, MAXPATHLEN
, M_NAMEI
);
2763 kauth_cred_rele(saved_cred
);
2771 nfsrv_link(nfsd
, slp
, procp
, mrq
)
2772 struct nfsrv_descript
*nfsd
;
2773 struct nfssvc_sock
*slp
;
2777 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
2778 mbuf_t nam
= nfsd
->nd_nam
;
2779 caddr_t dpos
= nfsd
->nd_dpos
;
2780 struct nameidata nd
;
2784 int error
= 0, len
, dirfor_ret
= 1, diraft_ret
= 1;
2785 int getret
= 1, v3
= (nfsd
->nd_flag
& ND_NFSV3
);
2788 vnode_t vp
, xp
, dvp
, dirp
= NULL
;
2789 struct vnode_attr dirfor
, diraft
, at
;
2790 struct nfs_filehandle nfh
, dnfh
;
2791 struct nfs_export
*nx
;
2792 struct nfs_export_options
*nxo
;
2793 struct vfs_context context
;
2795 vp
= xp
= dvp
= NULL
;
2796 nfsm_srvmtofh(&nfh
);
2797 nfsm_srvmtofh(&dnfh
);
2798 nfsm_srvnamesiz(len
, v3
);
2799 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
2800 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_WCCDATA(v3
));
2801 nfsm_srvpostop_attr(getret
, &at
);
2802 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2805 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
2807 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_WCCDATA(v3
));
2808 nfsm_srvpostop_attr(getret
, &at
);
2809 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2813 /* we're not allowed to link to directories... */
2814 if (vnode_vtype(vp
) == VDIR
) {
2815 error
= EPERM
; /* POSIX */
2819 context
.vc_proc
= procp
;
2820 context
.vc_ucred
= nfsd
->nd_cr
;
2822 /* ...or to anything that kauth doesn't want us to (eg. immutable items) */
2823 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LINKTARGET
, &context
, nxo
, 0)) != 0)
2826 nd
.ni_cnd
.cn_nameiop
= CREATE
;
2827 nd
.ni_cnd
.cn_flags
= LOCKPARENT
;
2828 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
2830 error
= nfs_namei(nfsd
, &context
, &nd
, &dnfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
2833 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
2834 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
2847 else if (vnode_mount(vp
) != vnode_mount(dvp
))
2850 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
, nxo
, 0);
2853 error
= VNOP_LINK(vp
, dvp
, &nd
.ni_cnd
, &context
);
2856 * nameidone has to happen before we vnode_put(dvp)
2857 * since it may need to release the fs_nodelock on the dvp
2866 nfsm_srv_vattr_init(&at
, v3
);
2867 getret
= vnode_getattr(vp
, &at
, &context
);
2870 nfsm_srv_vattr_init(&diraft
, v3
);
2871 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
2876 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_WCCDATA(v3
));
2878 nfsm_srvpostop_attr(getret
, &at
);
2879 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
2887 * nfs symbolic link service
2890 nfsrv_symlink(nfsd
, slp
, procp
, mrq
)
2891 struct nfsrv_descript
*nfsd
;
2892 struct nfssvc_sock
*slp
;
2896 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
2897 mbuf_t nam
= nfsd
->nd_nam
;
2898 caddr_t dpos
= nfsd
->nd_dpos
;
2899 struct vnode_attr dirfor
, diraft
, postat
;
2900 struct nameidata nd
;
2901 struct vnode_attr va
;
2902 struct vnode_attr
*vap
= &va
;
2905 struct nfsv2_sattr
*sp
;
2906 char *bpos
, *linkdata
= NULL
, *cp2
;
2907 int error
= 0, len
, linkdatalen
;
2908 int dirfor_ret
= 1, diraft_ret
= 1;
2909 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
2910 mbuf_t mb
, mreq
, mb2
;
2911 vnode_t vp
, dvp
, dirp
= NULL
;
2912 struct nfs_filehandle nfh
;
2913 struct nfs_export
*nx
;
2914 struct nfs_export_options
*nxo
;
2916 char uio_buf
[ UIO_SIZEOF(1) ];
2917 struct vfs_context context
;
2920 context
.vc_proc
= procp
;
2921 context
.vc_ucred
= nfsd
->nd_cr
;
2924 * Save the original credential UID in case they are
2925 * mapped and we need to map the IDs in the attributes.
2927 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
2929 nd
.ni_cnd
.cn_nameiop
= 0;
2931 nfsm_srvmtofh(&nfh
);
2932 nfsm_srvnamesiz(len
, v3
);
2934 nd
.ni_cnd
.cn_nameiop
= CREATE
;
2935 nd
.ni_cnd
.cn_flags
= LOCKPARENT
;
2936 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
2938 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
2941 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
2942 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
2949 nd
.ni_cnd
.cn_nameiop
= 0;
2958 nfsm_strsiz(linkdatalen
, NFS_MAXPATHLEN
, v3
);
2959 MALLOC(linkdata
, caddr_t
, linkdatalen
+ 1, M_TEMP
, M_WAITOK
);
2962 nd
.ni_cnd
.cn_nameiop
= 0;
2963 vnode_put(nd
.ni_dvp
);
2964 vnode_put(nd
.ni_vp
);
2968 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2969 &uio_buf
[0], sizeof(uio_buf
));
2972 nd
.ni_cnd
.cn_nameiop
= 0;
2973 vnode_put(nd
.ni_dvp
);
2974 vnode_put(nd
.ni_vp
);
2978 uio_addiov(auio
, CAST_USER_ADDR_T(linkdata
), linkdatalen
);
2979 nfsm_mtouio(auio
, linkdatalen
);
2981 nfsm_dissect(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
2982 VATTR_SET(vap
, va_mode
, fxdr_unsigned(u_short
, sp
->sa_mode
));
2984 *(linkdata
+ linkdatalen
) = '\0';
2991 * If the credentials were mapped, we should
2992 * map the same values in the attributes.
2994 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
2996 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
2997 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
2998 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
3000 VATTR_SET(vap
, va_type
, VLNK
);
3001 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3002 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3004 /* authorize before creating */
3005 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_FILE
, &context
, nxo
, 0);
3007 /* validate given attributes */
3009 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
3010 if (error
&& (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
))) {
3012 * Most NFS servers just ignore the UID/GID attributes, so we
3013 * try ignoring them if that'll help the request succeed.
3015 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3016 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3017 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
3021 error
= VNOP_SYMLINK(dvp
, &vp
, &nd
.ni_cnd
, vap
, linkdata
, &context
);
3025 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
3026 nd
.ni_cnd
.cn_flags
&= ~(LOCKPARENT
| FOLLOW
);
3027 nd
.ni_cnd
.cn_flags
|= (NOFOLLOW
| LOCKLEAF
);
3028 nd
.ni_cnd
.cn_context
= &context
;
3029 nd
.ni_startdir
= dvp
;
3031 error
= lookup(&nd
);
3036 error
= nfsrv_vptofh(nx
, !v3
, NULL
, vp
, &context
, &nfh
);
3038 nfsm_srv_vattr_init(&postat
, v3
);
3039 error
= vnode_getattr(vp
, &postat
, &context
);
3045 * nameidone has to happen before we vnode_put(dvp)
3046 * since it may need to release the fs_nodelock on the dvp
3049 nd
.ni_cnd
.cn_nameiop
= 0;
3056 FREE(linkdata
, M_TEMP
);
3058 nfsm_srv_vattr_init(&diraft
, v3
);
3059 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
3062 nfsm_reply(NFSX_SRVFH(v3
, &nfh
) + NFSX_POSTOPATTR(v3
) + NFSX_WCCDATA(v3
));
3065 nfsm_srvpostop_fh(&nfh
);
3066 nfsm_srvpostop_attr(0, &postat
);
3068 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
3072 if (nd
.ni_cnd
.cn_nameiop
) {
3074 * nameidone has to happen before we vnode_put(dvp)
3075 * since it may need to release the fs_nodelock on the dvp
3086 FREE(linkdata
, M_TEMP
);
3094 nfsrv_mkdir(nfsd
, slp
, procp
, mrq
)
3095 struct nfsrv_descript
*nfsd
;
3096 struct nfssvc_sock
*slp
;
3100 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
3101 mbuf_t nam
= nfsd
->nd_nam
;
3102 caddr_t dpos
= nfsd
->nd_dpos
;
3103 struct vnode_attr dirfor
, diraft
, postat
;
3104 struct vnode_attr va
;
3105 struct vnode_attr
*vap
= &va
;
3106 struct nfs_fattr
*fp
;
3107 struct nameidata nd
;
3113 int dirfor_ret
= 1, diraft_ret
= 1;
3114 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
3116 mbuf_t mb
, mb2
, mreq
;
3117 vnode_t vp
, dvp
, dirp
= NULL
;
3118 struct nfs_filehandle nfh
;
3119 struct nfs_export
*nx
;
3120 struct nfs_export_options
*nxo
;
3121 struct vfs_context context
;
3123 kauth_acl_t xacl
= NULL
;
3125 context
.vc_proc
= procp
;
3126 context
.vc_ucred
= nfsd
->nd_cr
;
3129 * Save the original credential UID in case they are
3130 * mapped and we need to map the IDs in the attributes.
3132 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
3134 nd
.ni_cnd
.cn_nameiop
= 0;
3136 nfsm_srvmtofh(&nfh
);
3137 nfsm_srvnamesiz(len
, v3
);
3139 nd
.ni_cnd
.cn_nameiop
= CREATE
;
3140 nd
.ni_cnd
.cn_flags
= LOCKPARENT
;
3141 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
3143 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
3146 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
3147 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
3154 nd
.ni_cnd
.cn_nameiop
= 0;
3155 nfsm_reply(NFSX_WCCDATA(v3
));
3156 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
3168 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3169 VATTR_SET(vap
, va_mode
, nfstov_mode(*tl
++));
3171 VATTR_SET(vap
, va_type
, VDIR
);
3175 * nameidone has to happen before we vnode_put(dvp)
3176 * since it may need to release the fs_nodelock on the dvp
3187 * If the credentials were mapped, we should
3188 * map the same values in the attributes.
3190 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
3192 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
3193 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
3194 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
3197 error
= nfsrv_authorize(dvp
, NULL
, KAUTH_VNODE_ADD_SUBDIRECTORY
, &context
, nxo
, 0);
3199 /* construct ACL and handle inheritance */
3201 error
= kauth_acl_inherit(dvp
,
3207 if (!error
&& xacl
!= NULL
)
3208 VATTR_SET(vap
, va_acl
, xacl
);
3210 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
3211 VATTR_CLEAR_ACTIVE(vap
, va_access_time
);
3213 /* validate new-file security information */
3215 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
3216 if (error
&& (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
))) {
3218 * Most NFS servers just ignore the UID/GID attributes, so we
3219 * try ignoring them if that'll help the request succeed.
3221 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3222 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3223 error
= vnode_authattr_new(dvp
, vap
, 0, &context
);
3228 error
= VNOP_MKDIR(dvp
, &vp
, &nd
.ni_cnd
, vap
, &context
);
3230 if (!error
&& !VATTR_ALL_SUPPORTED(vap
))
3232 * If some of the requested attributes weren't handled by the VNOP,
3233 * use our fallback code.
3235 error
= vnode_setattr_fallback(vp
, vap
, &context
);
3238 kauth_acl_free(xacl
);
3241 error
= nfsrv_vptofh(nx
, !v3
, NULL
, vp
, &context
, &nfh
);
3243 nfsm_srv_vattr_init(&postat
, v3
);
3244 error
= vnode_getattr(vp
, &postat
, &context
);
3250 * nameidone has to happen before we vnode_put(dvp)
3251 * since it may need to release the fs_nodelock on the dvp
3257 nd
.ni_cnd
.cn_nameiop
= 0;
3260 nfsm_srv_vattr_init(&diraft
, v3
);
3261 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
3264 nfsm_reply(NFSX_SRVFH(v3
, &nfh
) + NFSX_POSTOPATTR(v3
) + NFSX_WCCDATA(v3
));
3267 nfsm_srvpostop_fh(&nfh
);
3268 nfsm_srvpostop_attr(0, &postat
);
3270 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
3272 nfsm_srvfhtom(&nfh
, v3
);
3273 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
3274 nfsm_srvfillattr(&postat
, fp
);
3278 if (nd
.ni_cnd
.cn_nameiop
) {
3280 * nameidone has to happen before we vnode_put(dvp)
3281 * since it may need to release the fs_nodelock on the dvp
3297 nfsrv_rmdir(nfsd
, slp
, procp
, mrq
)
3298 struct nfsrv_descript
*nfsd
;
3299 struct nfssvc_sock
*slp
;
3303 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
3304 mbuf_t nam
= nfsd
->nd_nam
;
3305 caddr_t dpos
= nfsd
->nd_dpos
;
3310 int dirfor_ret
= 1, diraft_ret
= 1;
3311 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
3314 vnode_t vp
, dvp
, dirp
= NULL
;
3315 struct vnode_attr dirfor
, diraft
;
3316 struct nfs_filehandle nfh
;
3317 struct nfs_export
*nx
;
3318 struct nfs_export_options
*nxo
;
3319 struct nameidata nd
;
3320 struct vfs_context context
;
3322 context
.vc_proc
= procp
;
3323 context
.vc_ucred
= nfsd
->nd_cr
;
3326 nfsm_srvmtofh(&nfh
);
3327 nfsm_srvnamesiz(len
, v3
);
3329 nd
.ni_cnd
.cn_nameiop
= DELETE
;
3330 nd
.ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
3331 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, FALSE
, &len
, &nd
);
3333 error
= nfs_namei(nfsd
, &context
, &nd
, &nfh
, nam
, FALSE
, &dirp
, &nx
, &nxo
);
3336 nfsm_srv_pre_vattr_init(&dirfor
, v3
);
3337 dirfor_ret
= vnode_getattr(dirp
, &dirfor
, &context
);
3344 nfsm_reply(NFSX_WCCDATA(v3
));
3345 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
3353 if (vnode_vtype(vp
) != VDIR
) {
3358 * No rmdir "." please.
3365 * The root of a mounted filesystem cannot be deleted.
3367 if (vnode_isvroot(vp
))
3370 error
= nfsrv_authorize(vp
, dvp
, KAUTH_VNODE_DELETE
, &context
, nxo
, 0);
3372 error
= VNOP_RMDIR(dvp
, vp
, &nd
.ni_cnd
, &context
);
3375 * nameidone has to happen before we vnode_put(dvp)
3376 * since it may need to release the fs_nodelock on the dvp
3384 nfsm_srv_vattr_init(&diraft
, v3
);
3385 diraft_ret
= vnode_getattr(dirp
, &diraft
, &context
);
3388 nfsm_reply(NFSX_WCCDATA(v3
));
3390 nfsm_srvwcc_data(dirfor_ret
, &dirfor
, diraft_ret
, &diraft
);
3398 * nfs readdir service
3399 * - mallocs what it thinks is enough to read
3400 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
3401 * - calls VNOP_READDIR()
3402 * - loops around building the reply
3403 * if the output generated exceeds count break out of loop
3404 * The nfsm_clget macro is used here so that the reply will be packed
3405 * tightly in mbuf clusters.
3406 * - it only knows that it has encountered eof when the VNOP_READDIR()
3408 * - as such one readdir rpc will return eof false although you are there
3409 * and then the next will return eof
3410 * - it trims out records with d_fileno == 0
3411 * this doesn't matter for Unix clients, but they might confuse clients
3413 * NB: It is tempting to set eof to true if the VNOP_READDIR() reads less
3414 * than requested, but this may not apply to all filesystems. For
3415 * example, client NFS does not { although it is never remote mounted
3417 * The alternate call nfsrv_readdirplus() does lookups as well.
3418 * PS: The XNFS protocol spec clearly describes what the "count"s arguments
3419 * are supposed to cover. For readdir, the count is the total number of
3420 * bytes included in everything from the directory's postopattr through
3421 * the EOF flag. For readdirplus, the maxcount is the same, and the
3422 * dircount includes all that except for the entry attributes and handles.
3426 nfsrv_readdir(nfsd
, slp
, procp
, mrq
)
3427 struct nfsrv_descript
*nfsd
;
3428 struct nfssvc_sock
*slp
;
3432 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
3433 mbuf_t nam
= nfsd
->nd_nam
;
3434 caddr_t dpos
= nfsd
->nd_dpos
;
3437 struct direntry
*dp
;
3442 mbuf_t mb
, mb2
, mreq
, mp2
;
3443 char *cpos
, *cend
, *cp2
, *rbuf
;
3445 struct vnode_attr at
;
3446 struct nfs_filehandle nfh
;
3447 struct nfs_export
*nx
;
3448 struct nfs_export_options
*nxo
;
3450 char uio_buf
[ UIO_SIZEOF(1) ];
3451 int len
, nlen
, rem
, xfer
, tsiz
, i
, error
= 0, getret
= 1;
3452 int siz
, count
, fullsiz
, eofflag
, nentries
= 0;
3453 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
3454 u_quad_t off
, toff
, verf
;
3457 struct vfs_context context
;
3459 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
3461 nfsm_srvmtofh(&nfh
);
3463 nfsm_dissect(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
3464 fxdr_hyper(tl
, &toff
);
3466 fxdr_hyper(tl
, &verf
);
3469 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3470 toff
= fxdr_unsigned(u_quad_t
, *tl
++);
3473 count
= fxdr_unsigned(int, *tl
);
3474 siz
= ((count
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
3475 xfer
= NFS_SRVMAXDATA(nfsd
);
3479 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
3480 nfsm_reply(NFSX_UNSIGNED
);
3481 nfsm_srvpostop_attr(getret
, &at
);
3484 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
3486 nfsm_reply(NFSX_UNSIGNED
);
3487 nfsm_srvpostop_attr(getret
, &at
);
3490 context
.vc_proc
= procp
;
3491 context
.vc_ucred
= nfsd
->nd_cr
;
3492 if (!v3
|| (nxo
->nxo_flags
& NX_32BITCLIENTS
))
3493 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
3495 nfsm_srv_vattr_init(&at
, v3
);
3496 error
= getret
= vnode_getattr(vp
, &at
, &context
);
3497 if (!error
&& toff
&& verf
&& verf
!= at
.va_filerev
)
3498 error
= NFSERR_BAD_COOKIE
;
3501 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, &context
, nxo
, 0);
3504 nfsm_reply(NFSX_POSTOPATTR(v3
));
3505 nfsm_srvpostop_attr(getret
, &at
);
3508 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
3512 nfsm_reply(NFSX_POSTOPATTR(v3
));
3513 nfsm_srvpostop_attr(getret
, &at
);
3516 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3517 &uio_buf
[0], sizeof(uio_buf
));
3522 nfsm_reply(NFSX_POSTOPATTR(v3
));
3523 nfsm_srvpostop_attr(getret
, &at
);
3527 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
3528 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
3531 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, &context
);
3532 off
= uio_offset(auio
);
3535 nfsm_srv_vattr_init(&at
, v3
);
3536 getret
= vnode_getattr(vp
, &at
, &context
);
3543 nfsm_reply(NFSX_POSTOPATTR(v3
));
3544 nfsm_srvpostop_attr(getret
, &at
);
3547 if (uio_resid(auio
) != 0) {
3548 // LP64todo - fix this
3549 siz
-= uio_resid(auio
);
3552 * If nothing read, return eof
3557 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_COOKIEVERF(v3
) +
3560 nfsm_srvpostop_attr(getret
, &at
);
3561 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
3562 txdr_hyper(&at
.va_filerev
, tl
);
3565 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3574 * Check for degenerate cases of nothing useful read.
3575 * If so go try again
3579 dp
= (struct direntry
*)cpos
;
3580 while (dp
->d_fileno
== 0 && cpos
< cend
&& nentries
> 0) {
3581 cpos
+= dp
->d_reclen
;
3582 dp
= (struct direntry
*)cpos
;
3585 if (cpos
>= cend
|| nentries
== 0) {
3592 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_COOKIEVERF(v3
) + siz
);
3594 len
= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
;
3595 nfsm_srvpostop_attr(getret
, &at
);
3596 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3597 txdr_hyper(&at
.va_filerev
, tl
);
3599 len
= 2 * NFSX_UNSIGNED
;
3602 be
= bp
+ mbuf_trailingspace(mp
);
3604 /* Loop through the records and build reply */
3605 while (cpos
< cend
&& nentries
> 0) {
3606 if (dp
->d_fileno
!= 0) {
3607 nlen
= dp
->d_namlen
;
3608 if (!v3
&& (nlen
> NFS_MAXNAMLEN
))
3609 nlen
= NFS_MAXNAMLEN
;
3610 rem
= nfsm_rndup(nlen
)-nlen
;
3611 len
+= (4 * NFSX_UNSIGNED
+ nlen
+ rem
);
3613 len
+= 2 * NFSX_UNSIGNED
;
3619 * Build the directory record xdr from
3620 * the direntry entry.
3624 bp
+= NFSX_UNSIGNED
;
3627 txdr_hyper(&dp
->d_fileno
, &tquad
);
3628 *tl
= tquad
.nfsuquad
[0];
3629 bp
+= NFSX_UNSIGNED
;
3631 *tl
= tquad
.nfsuquad
[1];
3632 bp
+= NFSX_UNSIGNED
;
3634 *tl
= txdr_unsigned(dp
->d_fileno
);
3635 bp
+= NFSX_UNSIGNED
;
3638 *tl
= txdr_unsigned(nlen
);
3639 bp
+= NFSX_UNSIGNED
;
3641 /* And loop around copying the name */
3650 bcopy(cp
, bp
, tsiz
);
3656 /* And null pad to a long boundary */
3657 for (i
= 0; i
< rem
; i
++)
3660 /* Finish off the record with the cookie */
3663 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
3664 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
3665 txdr_hyper(&dp
->d_seekoff
, &tquad
);
3666 *tl
= tquad
.nfsuquad
[0];
3667 bp
+= NFSX_UNSIGNED
;
3669 *tl
= tquad
.nfsuquad
[1];
3670 bp
+= NFSX_UNSIGNED
;
3672 *tl
= txdr_unsigned(dp
->d_seekoff
);
3673 bp
+= NFSX_UNSIGNED
;
3676 cpos
+= dp
->d_reclen
;
3677 dp
= (struct direntry
*)cpos
;
3682 bp
+= NFSX_UNSIGNED
;
3688 bp
+= NFSX_UNSIGNED
;
3691 mbuf_setlen(mp
, bp
- (char*)mbuf_data(mp
));
3693 mbuf_setlen(mp
, mbuf_len(mp
) + (bp
- bpos
));
3702 u_long fl_fattr
[NFSX_V3FATTR
/ sizeof (u_long
)];
3705 u_long fl_nfh
[NFSX_V3FHMAX
/ sizeof (u_long
)];
3709 nfsrv_readdirplus(nfsd
, slp
, procp
, mrq
)
3710 struct nfsrv_descript
*nfsd
;
3711 struct nfssvc_sock
*slp
;
3715 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
3716 mbuf_t nam
= nfsd
->nd_nam
;
3717 caddr_t dpos
= nfsd
->nd_dpos
;
3720 struct direntry
*dp
;
3725 mbuf_t mb
, mb2
, mreq
, mp2
;
3726 char *cpos
, *cend
, *cp2
, *rbuf
;
3729 struct nfs_filehandle dnfh
, *nfhp
= (struct nfs_filehandle
*)&fl
.fl_fhsize
;
3731 struct nfs_export
*nx
;
3732 struct nfs_export_options
*nxo
;
3734 char uio_buf
[ UIO_SIZEOF(1) ];
3735 struct vnode_attr va
, at
, *vap
= &va
;
3736 struct nfs_fattr
*fp
;
3737 int len
, nlen
, rem
, xfer
, tsiz
, i
, error
= 0, getret
= 1;
3738 int siz
, count
, fullsiz
, eofflag
, dirlen
, nentries
= 0, isdotdot
;
3739 u_quad_t off
, toff
, verf
;
3742 struct vfs_context context
;
3744 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
3746 nfsm_srvmtofh(&dnfh
);
3747 nfsm_dissect(tl
, u_long
*, 6 * NFSX_UNSIGNED
);
3748 fxdr_hyper(tl
, &toff
);
3750 fxdr_hyper(tl
, &verf
);
3752 siz
= fxdr_unsigned(int, *tl
++);
3753 count
= fxdr_unsigned(int, *tl
);
3755 siz
= ((siz
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
3756 xfer
= NFS_SRVMAXDATA(nfsd
);
3760 if ((error
= nfsrv_fhtovp(&dnfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
3761 nfsm_reply(NFSX_UNSIGNED
);
3762 nfsm_srvpostop_attr(getret
, &at
);
3765 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
3767 nfsm_reply(NFSX_UNSIGNED
);
3768 nfsm_srvpostop_attr(getret
, &at
);
3771 context
.vc_proc
= procp
;
3772 context
.vc_ucred
= nfsd
->nd_cr
;
3773 if (nxo
->nxo_flags
& NX_32BITCLIENTS
)
3774 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
3775 nfsm_srv_vattr_init(&at
, 1);
3776 error
= getret
= vnode_getattr(vp
, &at
, &context
);
3777 if (!error
&& toff
&& verf
&& verf
!= at
.va_filerev
)
3778 error
= NFSERR_BAD_COOKIE
;
3780 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, &context
, nxo
, 0);
3784 nfsm_reply(NFSX_V3POSTOPATTR
);
3785 nfsm_srvpostop_attr(getret
, &at
);
3788 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
3793 nfsm_reply(NFSX_V3POSTOPATTR
);
3794 nfsm_srvpostop_attr(getret
, &at
);
3797 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3798 &uio_buf
[0], sizeof(uio_buf
));
3804 nfsm_reply(NFSX_V3POSTOPATTR
);
3805 nfsm_srvpostop_attr(getret
, &at
);
3809 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
3810 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
3812 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, &context
);
3813 off
= uio_offset(auio
);
3814 nfsm_srv_vattr_init(&at
, 1);
3815 getret
= vnode_getattr(vp
, &at
, &context
);
3823 nfsm_reply(NFSX_V3POSTOPATTR
);
3824 nfsm_srvpostop_attr(getret
, &at
);
3827 if (uio_resid(auio
) != 0) {
3828 // LP64todo - fix this
3829 siz
-= uio_resid(auio
);
3832 * If nothing read, return eof
3838 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+
3840 nfsm_srvpostop_attr(getret
, &at
);
3841 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
3842 txdr_hyper(&at
.va_filerev
, tl
);
3852 * Check for degenerate cases of nothing useful read.
3853 * If so go try again
3857 dp
= (struct direntry
*)cpos
;
3858 while (dp
->d_fileno
== 0 && cpos
< cend
&& nentries
> 0) {
3859 cpos
+= dp
->d_reclen
;
3860 dp
= (struct direntry
*)cpos
;
3863 if (cpos
>= cend
|| nentries
== 0) {
3870 * Probe one of the directory entries to see if the filesystem
3873 if ((error
= VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, &context
))) {
3874 if (error
== ENOTSUP
) /* let others get passed back */
3875 error
= NFSERR_NOTSUPP
;
3879 nfsm_reply(NFSX_V3POSTOPATTR
);
3880 nfsm_srvpostop_attr(getret
, &at
);
3885 dirlen
= len
= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
;
3887 nfsm_srvpostop_attr(getret
, &at
);
3888 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3889 txdr_hyper(&at
.va_filerev
, tl
);
3892 be
= bp
+ mbuf_trailingspace(mp
);
3894 /* Loop through the records and build reply */
3895 while (cpos
< cend
&& nentries
> 0) {
3896 if (dp
->d_fileno
!= 0) {
3897 nlen
= dp
->d_namlen
;
3898 rem
= nfsm_rndup(nlen
)-nlen
;
3901 * Got to get the vnode for lookup per entry.
3903 if (VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, &context
))
3905 isdotdot
= ((dp
->d_namlen
== 2) &&
3906 (dp
->d_name
[0] == '.') && (dp
->d_name
[1] == '.'));
3907 if (nfsrv_vptofh(nx
, 0, (isdotdot
? &dnfh
: NULL
), nvp
, &context
, nfhp
)) {
3908 // XXX file handle is optional, so we should be able to
3909 // XXX return this entry without the file handle
3913 nfsm_srv_vattr_init(vap
, 1);
3914 if (vnode_getattr(nvp
, vap
, &context
)) {
3915 // XXX attributes are optional, so we should be able to
3916 // XXX return this entry without the attributes
3923 * If either the dircount or maxcount will be
3924 * exceeded, get out now. Both of these lengths
3925 * are calculated conservatively, including all
3928 len
+= (8 * NFSX_UNSIGNED
+ nlen
+ rem
+ nfhp
->nfh_len
+
3930 dirlen
+= (6 * NFSX_UNSIGNED
+ nlen
+ rem
);
3931 if (len
> count
|| dirlen
> fullsiz
) {
3937 * Build the directory record xdr from
3938 * the direntry entry.
3940 fp
= (struct nfs_fattr
*)&fl
.fl_fattr
;
3941 nfsm_srvfillattr(vap
, fp
);
3942 fhsize
= nfhp
->nfh_len
;
3943 fl
.fl_fhsize
= txdr_unsigned(fhsize
);
3944 fl
.fl_fhok
= nfs_true
;
3945 fl
.fl_postopok
= nfs_true
;
3946 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
3947 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
3948 txdr_hyper(&dp
->d_seekoff
, &fl
.fl_off
);
3952 bp
+= NFSX_UNSIGNED
;
3955 txdr_hyper(&dp
->d_fileno
, &tquad
);
3956 *tl
= tquad
.nfsuquad
[0];
3957 bp
+= NFSX_UNSIGNED
;
3959 *tl
= tquad
.nfsuquad
[1];
3960 bp
+= NFSX_UNSIGNED
;
3963 *tl
= txdr_unsigned(nlen
);
3964 bp
+= NFSX_UNSIGNED
;
3966 /* And loop around copying the name */
3971 if ((bp
+ xfer
) > be
)
3975 bcopy(cp
, bp
, tsiz
);
3981 /* And null pad to a long boundary */
3982 for (i
= 0; i
< rem
; i
++)
3986 * Now copy the flrep structure out.
3988 xfer
= sizeof(struct flrep
) - sizeof(fl
.fl_nfh
) + fhsize
;
3992 if ((bp
+ xfer
) > be
)
3996 bcopy(cp
, bp
, tsiz
);
4004 cpos
+= dp
->d_reclen
;
4005 dp
= (struct direntry
*)cpos
;
4012 bp
+= NFSX_UNSIGNED
;
4018 bp
+= NFSX_UNSIGNED
;
4021 mbuf_setlen(mp
, bp
- (char*)mbuf_data(mp
));
4023 mbuf_setlen(mp
, mbuf_len(mp
) + (bp
- bpos
));
4032 * nfs commit service
4035 nfsrv_commit(nfsd
, slp
, procp
, mrq
)
4036 struct nfsrv_descript
*nfsd
;
4037 struct nfssvc_sock
*slp
;
4041 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4042 mbuf_t nam
= nfsd
->nd_nam
;
4043 caddr_t dpos
= nfsd
->nd_dpos
;
4044 struct vnode_attr bfor
, aft
;
4046 struct nfs_filehandle nfh
;
4047 struct nfs_export
*nx
;
4048 struct nfs_export_options
*nxo
;
4052 int error
= 0, for_ret
= 1, aft_ret
= 1, count
;
4054 mbuf_t mb
, mb2
, mreq
;
4056 struct vfs_context context
;
4058 nfsm_srvmtofh(&nfh
);
4059 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
4062 * XXX At this time VNOP_FSYNC() does not accept offset and byte
4063 * count parameters, so these arguments are useless (someday maybe).
4065 fxdr_hyper(tl
, &off
);
4067 count
= fxdr_unsigned(int, *tl
);
4068 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4069 nfsm_reply(2 * NFSX_UNSIGNED
);
4070 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4073 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4075 nfsm_reply(2 * NFSX_UNSIGNED
);
4076 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4079 context
.vc_proc
= procp
;
4080 context
.vc_ucred
= nfsd
->nd_cr
;
4082 nfsm_srv_pre_vattr_init(&bfor
, 1);
4083 for_ret
= vnode_getattr(vp
, &bfor
, &context
);
4084 error
= VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
4085 nfsm_srv_vattr_init(&aft
, 1);
4086 aft_ret
= vnode_getattr(vp
, &aft
, &context
);
4088 nfsm_reply(NFSX_V3WCCDATA
+ NFSX_V3WRITEVERF
);
4089 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4091 nfsm_build(tl
, u_long
*, NFSX_V3WRITEVERF
);
4092 *tl
++ = txdr_unsigned(boottime_sec());
4093 *tl
= txdr_unsigned(0);
4101 * nfs statfs service
4104 nfsrv_statfs(nfsd
, slp
, procp
, mrq
)
4105 struct nfsrv_descript
*nfsd
;
4106 struct nfssvc_sock
*slp
;
4110 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4111 mbuf_t nam
= nfsd
->nd_nam
;
4112 caddr_t dpos
= nfsd
->nd_dpos
;
4114 struct nfs_statfs
*sfp
;
4118 int error
= 0, getret
= 1;
4119 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
4121 mbuf_t mb
, mb2
, mreq
;
4123 struct vnode_attr at
;
4124 struct nfs_filehandle nfh
;
4125 struct nfs_export
*nx
;
4126 struct nfs_export_options
*nxo
;
4129 struct vfs_context context
;
4131 nfsm_srvmtofh(&nfh
);
4132 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4133 nfsm_reply(NFSX_UNSIGNED
);
4134 nfsm_srvpostop_attr(getret
, &at
);
4137 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4139 nfsm_reply(NFSX_UNSIGNED
);
4140 nfsm_srvpostop_attr(getret
, &at
);
4143 context
.vc_proc
= procp
;
4144 context
.vc_ucred
= nfsd
->nd_cr
;
4147 VFSATTR_WANTED(&va
, f_blocks
);
4148 VFSATTR_WANTED(&va
, f_bavail
);
4149 VFSATTR_WANTED(&va
, f_files
);
4150 VFSATTR_WANTED(&va
, f_ffree
);
4151 error
= vfs_getattr(vnode_mount(vp
), &va
, &context
);
4152 blksize
= vnode_mount(vp
)->mnt_vfsstat
.f_bsize
;
4153 nfsm_srv_vattr_init(&at
, v3
);
4154 getret
= vnode_getattr(vp
, &at
, &context
);
4156 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_STATFS(v3
));
4158 nfsm_srvpostop_attr(getret
, &at
);
4161 nfsm_build(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
4163 tval
= (u_quad_t
)(va
.f_blocks
* blksize
);
4164 txdr_hyper(&tval
, &sfp
->sf_tbytes
);
4165 tval
= (u_quad_t
)(va
.f_bfree
* blksize
);
4166 txdr_hyper(&tval
, &sfp
->sf_fbytes
);
4167 tval
= (u_quad_t
)(va
.f_bavail
* blksize
);
4168 txdr_hyper(&tval
, &sfp
->sf_abytes
);
4169 txdr_hyper(&va
.f_files
, &sfp
->sf_tfiles
);
4170 txdr_hyper(&va
.f_ffree
, &sfp
->sf_ffiles
);
4171 txdr_hyper(&va
.f_ffree
, &sfp
->sf_afiles
);
4172 sfp
->sf_invarsec
= 0;
4174 sfp
->sf_tsize
= txdr_unsigned(NFS_V2MAXDATA
);
4175 sfp
->sf_bsize
= txdr_unsigned((unsigned)blksize
);
4176 sfp
->sf_blocks
= txdr_unsigned((unsigned)va
.f_blocks
);
4177 sfp
->sf_bfree
= txdr_unsigned((unsigned)va
.f_bfree
);
4178 sfp
->sf_bavail
= txdr_unsigned((unsigned)va
.f_bavail
);
4185 * nfs fsinfo service
4188 nfsrv_fsinfo(nfsd
, slp
, procp
, mrq
)
4189 struct nfsrv_descript
*nfsd
;
4190 struct nfssvc_sock
*slp
;
4194 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4195 mbuf_t nam
= nfsd
->nd_nam
;
4196 caddr_t dpos
= nfsd
->nd_dpos
;
4198 struct nfsv3_fsinfo
*sip
;
4201 int error
= 0, getret
= 1, prefsize
, maxsize
;
4203 mbuf_t mb
, mb2
, mreq
;
4205 struct vnode_attr at
;
4206 struct nfs_filehandle nfh
;
4207 struct nfs_export
*nx
;
4208 struct nfs_export_options
*nxo
;
4209 struct vfs_context context
;
4211 nfsm_srvmtofh(&nfh
);
4212 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4213 nfsm_reply(NFSX_UNSIGNED
);
4214 nfsm_srvpostop_attr(getret
, &at
);
4217 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4219 nfsm_reply(NFSX_UNSIGNED
);
4220 nfsm_srvpostop_attr(getret
, &at
);
4223 context
.vc_proc
= procp
;
4224 context
.vc_ucred
= nfsd
->nd_cr
;
4226 nfsm_srv_vattr_init(&at
, 1);
4227 getret
= vnode_getattr(vp
, &at
, &context
);
4229 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3FSINFO
);
4230 nfsm_srvpostop_attr(getret
, &at
);
4231 nfsm_build(sip
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
4235 * There should be file system VFS OP(s) to get this information.
4236 * For now, assume our usual NFS defaults.
4238 if (slp
->ns_sotype
== SOCK_DGRAM
) {
4239 maxsize
= NFS_MAXDGRAMDATA
;
4240 prefsize
= NFS_PREFDGRAMDATA
;
4242 maxsize
= prefsize
= NFS_MAXDATA
;
4243 sip
->fs_rtmax
= txdr_unsigned(maxsize
);
4244 sip
->fs_rtpref
= txdr_unsigned(prefsize
);
4245 sip
->fs_rtmult
= txdr_unsigned(NFS_FABLKSIZE
);
4246 sip
->fs_wtmax
= txdr_unsigned(maxsize
);
4247 sip
->fs_wtpref
= txdr_unsigned(prefsize
);
4248 sip
->fs_wtmult
= txdr_unsigned(NFS_FABLKSIZE
);
4249 sip
->fs_dtpref
= txdr_unsigned(prefsize
);
4250 sip
->fs_maxfilesize
.nfsuquad
[0] = 0xffffffff;
4251 sip
->fs_maxfilesize
.nfsuquad
[1] = 0xffffffff;
4252 sip
->fs_timedelta
.nfsv3_sec
= 0;
4253 sip
->fs_timedelta
.nfsv3_nsec
= txdr_unsigned(1);
4254 sip
->fs_properties
= txdr_unsigned(NFSV3FSINFO_LINK
|
4255 NFSV3FSINFO_SYMLINK
| NFSV3FSINFO_HOMOGENEOUS
|
4256 NFSV3FSINFO_CANSETTIME
);
4262 * nfs pathconf service
4265 nfsrv_pathconf(nfsd
, slp
, procp
, mrq
)
4266 struct nfsrv_descript
*nfsd
;
4267 struct nfssvc_sock
*slp
;
4271 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4272 mbuf_t nam
= nfsd
->nd_nam
;
4273 caddr_t dpos
= nfsd
->nd_dpos
;
4275 struct nfsv3_pathconf
*pc
;
4278 int error
= 0, getret
= 1, linkmax
, namemax
;
4279 int chownres
, notrunc
, case_sensitive
, case_preserving
;
4281 mbuf_t mb
, mb2
, mreq
;
4283 struct vnode_attr at
;
4284 struct nfs_filehandle nfh
;
4285 struct nfs_export
*nx
;
4286 struct nfs_export_options
*nxo
;
4287 struct vfs_context context
;
4289 nfsm_srvmtofh(&nfh
);
4290 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4291 nfsm_reply(NFSX_UNSIGNED
);
4292 nfsm_srvpostop_attr(getret
, &at
);
4295 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4297 nfsm_reply(NFSX_UNSIGNED
);
4298 nfsm_srvpostop_attr(getret
, &at
);
4301 context
.vc_proc
= procp
;
4302 context
.vc_ucred
= nfsd
->nd_cr
;
4304 error
= VNOP_PATHCONF(vp
, _PC_LINK_MAX
, &linkmax
, &context
);
4306 error
= VNOP_PATHCONF(vp
, _PC_NAME_MAX
, &namemax
, &context
);
4308 error
= VNOP_PATHCONF(vp
, _PC_CHOWN_RESTRICTED
, &chownres
, &context
);
4310 error
= VNOP_PATHCONF(vp
, _PC_NO_TRUNC
, ¬runc
, &context
);
4312 error
= VNOP_PATHCONF(vp
, _PC_CASE_SENSITIVE
, &case_sensitive
, &context
);
4314 error
= VNOP_PATHCONF(vp
, _PC_CASE_PRESERVING
, &case_preserving
, &context
);
4315 nfsm_srv_vattr_init(&at
, 1);
4316 getret
= vnode_getattr(vp
, &at
, &context
);
4318 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3PATHCONF
);
4319 nfsm_srvpostop_attr(getret
, &at
);
4322 nfsm_build(pc
, struct nfsv3_pathconf
*, NFSX_V3PATHCONF
);
4324 pc
->pc_linkmax
= txdr_unsigned(linkmax
);
4325 pc
->pc_namemax
= txdr_unsigned(namemax
);
4326 pc
->pc_notrunc
= txdr_unsigned(notrunc
);
4327 pc
->pc_chownrestricted
= txdr_unsigned(chownres
);
4328 pc
->pc_caseinsensitive
= txdr_unsigned(!case_sensitive
);
4329 pc
->pc_casepreserving
= txdr_unsigned(case_preserving
);
4336 * Null operation, used by clients to ping server
4341 struct nfsrv_descript
*nfsd
,
4342 struct nfssvc_sock
*slp
,
4343 __unused proc_t procp
,
4346 mbuf_t mrep
= nfsd
->nd_mrep
;
4348 int error
= NFSERR_RETVOID
;
4357 * No operation, used for obsolete procedures
4362 struct nfsrv_descript
*nfsd
,
4363 struct nfssvc_sock
*slp
,
4364 __unused proc_t procp
,
4367 mbuf_t mrep
= nfsd
->nd_mrep
;
4372 if (nfsd
->nd_repstat
)
4373 error
= nfsd
->nd_repstat
;
4375 error
= EPROCUNAVAIL
;
4382 * Perform access checking for vnodes obtained from file handles that would
4383 * refer to files already opened by a Unix client. You cannot just use
4384 * vnode_authorize() for two reasons.
4385 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4386 * 2 - The owner is to be given access irrespective of mode bits so that
4387 * processes that chmod after opening a file don't break. I don't like
4388 * this because it opens a security hole, but since the nfs server opens
4389 * a security hole the size of a barn door anyhow, what the heck.
4391 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, vnode_authorize()
4392 * will return EPERM instead of EACCESS. EPERM is always an error.
4399 kauth_action_t action
,
4400 vfs_context_t context
,
4401 struct nfs_export_options
*nxo
,
4404 struct vnode_attr vattr
;
4407 if (action
& KAUTH_VNODE_WRITE_RIGHTS
) {
4409 * Disallow write attempts on read-only exports;
4410 * unless the file is a socket or a block or character
4411 * device resident on the file system.
4413 if (nxo
->nxo_flags
& NX_READONLY
) {
4414 switch (vnode_vtype(vp
)) {
4415 case VREG
: case VDIR
: case VLNK
: case VCPLX
:
4422 error
= vnode_authorize(vp
, dvp
, action
, context
);
4424 * Allow certain operations for the owner (reads and writes
4425 * on files that are already open). Picking up from FreeBSD.
4427 if (override
&& (error
== EACCES
)) {
4429 VATTR_WANTED(&vattr
, va_uid
);
4430 if ((vnode_getattr(vp
, &vattr
, context
) == 0) &&
4431 (kauth_cred_getuid(vfs_context_ucred(context
)) == vattr
.va_uid
))
4436 #endif /* NFS_NOSERVER */