2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
59 * FreeBSD-Id: nfs_serv.c,v 1.52 1997/10/28 15:59:05 bde Exp $
63 * nfs version 2 and 3 server calls to vnode ops
64 * - these routines generally have 3 phases
65 * 1 - break down and validate rpc request in mbuf list
66 * 2 - do the vnode ops for the request
67 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
68 * 3 - build the rpc reply in an mbuf list
70 * - do not mix the phases, since the nfsm_?? macros can return failures
71 * on a bad rpc or similar and do not do any vnode_rele()s or vnode_put()s
73 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
74 * error number iff error != 0 whereas
75 * returning an error from the server function implies a fatal error
76 * such as a badly constructed rpc request that should be dropped without
78 * For Version 3, nfsm_reply() does not return for the error case, since
79 * most version 3 rpcs return more than the status for error cases.
82 #include <sys/param.h>
83 #include <sys/systm.h>
85 #include <sys/kauth.h>
86 #include <sys/unistd.h>
87 #include <sys/malloc.h>
88 #include <sys/vnode.h>
89 #include <sys/mount_internal.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/kpi_mbuf.h>
93 #include <sys/dirent.h>
95 #include <sys/kernel.h>
96 #include <sys/sysctl.h>
98 #include <sys/vnode_internal.h>
99 #include <sys/uio_internal.h>
100 #include <libkern/OSAtomic.h>
103 #include <sys/vmparam.h>
105 #include <nfs/nfsproto.h>
106 #include <nfs/rpcv2.h>
108 #include <nfs/xdr_subs.h>
109 #include <nfs/nfsm_subs.h>
111 nfstype nfsv3_type
[9] = { NFNON
, NFREG
, NFDIR
, NFBLK
, NFCHR
, NFLNK
, NFSOCK
,
114 nfstype nfsv2_type
[9] = { NFNON
, NFREG
, NFDIR
, NFBLK
, NFCHR
, NFLNK
, NFNON
,
117 extern u_long nfs_xdrneg1
;
118 extern u_long nfs_false
, nfs_true
;
119 extern enum vtype nv3tov_type
[8];
120 extern struct nfsstats nfsstats
;
122 int nfsrvw_procrastinate
= NFS_GATHERDELAY
* 1000;
123 int nfsrvw_procrastinate_v3
= 0;
127 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
128 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, async
, CTLFLAG_RW
, &nfs_async
, 0, "");
131 static int nfsrv_authorize(vnode_t
,vnode_t
,kauth_action_t
,vfs_context_t
,struct nfs_export_options
*,int);
132 static void nfsrvw_coalesce(struct nfsrv_descript
*, struct nfsrv_descript
*);
134 #define THREAD_SAFE_FS(VP) \
135 ((VP)->v_mount ? (VP)->v_mount->mnt_vtable->vfc_threadsafe : 0)
138 * nfs v3 access service
141 nfsrv3_access(nfsd
, slp
, procp
, mrq
)
142 struct nfsrv_descript
*nfsd
;
143 struct nfssvc_sock
*slp
;
147 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
148 mbuf_t nam
= nfsd
->nd_nam
;
149 caddr_t dpos
= nfsd
->nd_dpos
;
151 struct nfs_filehandle nfh
;
155 int error
= 0, getret
;
157 mbuf_t mb
, mreq
, mb2
;
158 struct vnode_attr vattr
, *vap
= &vattr
;
160 kauth_action_t testaction
;
161 struct vfs_context context
;
162 struct nfs_export
*nx
;
163 struct nfs_export_options
*nxo
;
166 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
167 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
168 nfsm_reply(NFSX_UNSIGNED
);
169 nfsm_srvpostop_attr(1, NULL
);
172 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
174 nfsm_reply(NFSX_UNSIGNED
);
175 nfsm_srvpostop_attr(1, NULL
);
178 nfsmode
= fxdr_unsigned(u_long
, *tl
);
180 context
.vc_proc
= procp
;
181 context
.vc_ucred
= nfsd
->nd_cr
;
184 * Each NFS mode bit is tested separately.
186 * XXX this code is nominally correct, but returns a pessimistic
187 * rather than optimistic result. It will be necessary to add
188 * an NFS-specific interface to the vnode_authorize code to
189 * obtain good performance in the optimistic mode.
191 if (nfsmode
& NFSV3ACCESS_READ
) {
192 if (vnode_isdir(vp
)) {
194 KAUTH_VNODE_LIST_DIRECTORY
|
195 KAUTH_VNODE_READ_EXTATTRIBUTES
;
198 KAUTH_VNODE_READ_DATA
|
199 KAUTH_VNODE_READ_EXTATTRIBUTES
;
201 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
202 nfsmode
&= ~NFSV3ACCESS_READ
;
204 if ((nfsmode
& NFSV3ACCESS_LOOKUP
) &&
206 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_SEARCH
, &context
, nxo
, 0)))
207 nfsmode
&= ~NFSV3ACCESS_LOOKUP
;
208 if (nfsmode
& NFSV3ACCESS_MODIFY
) {
209 if (vnode_isdir(vp
)) {
211 KAUTH_VNODE_ADD_FILE
|
212 KAUTH_VNODE_ADD_SUBDIRECTORY
|
213 KAUTH_VNODE_DELETE_CHILD
;
216 KAUTH_VNODE_WRITE_DATA
;
218 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
219 nfsmode
&= ~NFSV3ACCESS_MODIFY
;
221 if (nfsmode
& NFSV3ACCESS_EXTEND
) {
222 if (vnode_isdir(vp
)) {
224 KAUTH_VNODE_ADD_FILE
|
225 KAUTH_VNODE_ADD_SUBDIRECTORY
;
228 KAUTH_VNODE_WRITE_DATA
|
229 KAUTH_VNODE_APPEND_DATA
;
231 if (nfsrv_authorize(vp
, NULL
, testaction
, &context
, nxo
, 0))
232 nfsmode
&= ~NFSV3ACCESS_EXTEND
;
236 * Note concerning NFSV3ACCESS_DELETE:
237 * For hard links, the answer may be wrong if the vnode
238 * has multiple parents with different permissions.
239 * Also, some clients (e.g. MacOSX 10.3) may incorrectly
240 * interpret the missing/cleared DELETE bit.
241 * So we'll just leave the DELETE bit alone. At worst,
242 * we're telling the client it might be able to do
243 * something it really can't.
246 if ((nfsmode
& NFSV3ACCESS_EXECUTE
) &&
248 nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
, nxo
, 0)))
249 nfsmode
&= ~NFSV3ACCESS_EXECUTE
;
251 nfsm_srv_vattr_init(vap
, 1);
252 getret
= vnode_getattr(vp
, vap
, &context
);
254 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED
);
255 nfsm_srvpostop_attr(getret
, vap
);
256 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
257 *tl
= txdr_unsigned(nfsmode
);
263 * nfs getattr service
266 nfsrv_getattr(nfsd
, slp
, procp
, mrq
)
267 struct nfsrv_descript
*nfsd
;
268 struct nfssvc_sock
*slp
;
272 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
273 mbuf_t nam
= nfsd
->nd_nam
;
274 caddr_t dpos
= nfsd
->nd_dpos
;
275 struct nfs_fattr
*fp
;
276 struct vnode_attr va
;
277 struct vnode_attr
*vap
= &va
;
279 struct nfs_filehandle nfh
;
285 mbuf_t mb
, mb2
, mreq
;
286 struct vfs_context context
;
287 struct nfs_export
*nx
;
288 struct nfs_export_options
*nxo
;
289 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
292 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
296 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
301 context
.vc_proc
= procp
;
302 context
.vc_ucred
= nfsd
->nd_cr
;
304 nfsm_srv_vattr_init(vap
, v3
);
305 error
= vnode_getattr(vp
, vap
, &context
);
307 nfsm_reply(NFSX_FATTR(v3
));
310 nfsm_build(fp
, struct nfs_fattr
*, NFSX_FATTR(v3
));
311 nfsm_srvfillattr(vap
, fp
);
317 * nfs setattr service
320 nfsrv_setattr(nfsd
, slp
, procp
, mrq
)
321 struct nfsrv_descript
*nfsd
;
322 struct nfssvc_sock
*slp
;
326 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
327 mbuf_t nam
= nfsd
->nd_nam
;
328 caddr_t dpos
= nfsd
->nd_dpos
;
329 struct vnode_attr preat
;
330 struct vnode_attr postat
;
331 struct vnode_attr va
;
332 struct vnode_attr
*vap
= &va
;
333 struct nfsv2_sattr
*sp
;
334 struct nfs_fattr
*fp
;
336 struct nfs_filehandle nfh
;
337 struct nfs_export
*nx
;
338 struct nfs_export_options
*nxo
;
342 int error
= 0, preat_ret
= 1, postat_ret
= 1;
343 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), gcheck
= 0;
345 mbuf_t mb
, mb2
, mreq
;
346 struct timespec guard
;
347 struct vfs_context context
;
348 kauth_action_t action
;
355 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
356 gcheck
= fxdr_unsigned(int, *tl
);
358 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
359 fxdr_nfsv3time(tl
, &guard
);
362 nfsm_dissect(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
364 * Nah nah nah nah na nah
365 * There is a bug in the Sun client that puts 0xffff in the mode
366 * field of sattr when it should put in 0xffffffff. The u_short
367 * doesn't sign extend.
368 * --> check the low order 2 bytes for 0xffff
370 if ((fxdr_unsigned(int, sp
->sa_mode
) & 0xffff) != 0xffff)
371 VATTR_SET(vap
, va_mode
, nfstov_mode(sp
->sa_mode
));
372 if (sp
->sa_uid
!= nfs_xdrneg1
)
373 VATTR_SET(vap
, va_uid
, fxdr_unsigned(uid_t
, sp
->sa_uid
));
374 if (sp
->sa_gid
!= nfs_xdrneg1
)
375 VATTR_SET(vap
, va_gid
, fxdr_unsigned(gid_t
, sp
->sa_gid
));
376 if (sp
->sa_size
!= nfs_xdrneg1
)
377 VATTR_SET(vap
, va_data_size
, fxdr_unsigned(u_quad_t
, sp
->sa_size
));
378 if (sp
->sa_atime
.nfsv2_sec
!= nfs_xdrneg1
) {
379 fxdr_nfsv2time(&sp
->sa_atime
, &vap
->va_access_time
);
380 VATTR_SET_ACTIVE(vap
, va_access_time
);
382 if (sp
->sa_mtime
.nfsv2_sec
!= nfs_xdrneg1
) {
383 fxdr_nfsv2time(&sp
->sa_mtime
, &vap
->va_modify_time
);
384 VATTR_SET_ACTIVE(vap
, va_modify_time
);
389 * Save the original credential UID in case they are
390 * mapped and we need to map the IDs in the attributes.
392 saved_uid
= kauth_cred_getuid(nfsd
->nd_cr
);
395 * Now that we have all the fields, lets do it.
397 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
398 nfsm_reply(2 * NFSX_UNSIGNED
);
399 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
402 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
404 nfsm_reply(2 * NFSX_UNSIGNED
);
405 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
409 context
.vc_proc
= procp
;
410 context
.vc_ucred
= nfsd
->nd_cr
;
413 nfsm_srv_pre_vattr_init(&preat
, v3
);
414 error
= preat_ret
= vnode_getattr(vp
, &preat
, &context
);
415 if (!error
&& gcheck
&& VATTR_IS_SUPPORTED(&preat
, va_change_time
) &&
416 (preat
.va_change_time
.tv_sec
!= guard
.tv_sec
||
417 preat
.va_change_time
.tv_nsec
!= guard
.tv_nsec
))
418 error
= NFSERR_NOT_SYNC
;
419 if (!preat_ret
&& !VATTR_ALL_SUPPORTED(&preat
))
423 nfsm_reply(NFSX_WCCDATA(v3
));
424 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
430 * If the credentials were mapped, we should
431 * map the same values in the attributes.
433 if ((vap
->va_uid
== saved_uid
) && (kauth_cred_getuid(nfsd
->nd_cr
) != saved_uid
)) {
435 VATTR_SET(vap
, va_uid
, kauth_cred_getuid(nfsd
->nd_cr
));
436 if (kauth_cred_ismember_gid(nfsd
->nd_cr
, vap
->va_gid
, &ismember
) || !ismember
)
437 VATTR_SET(vap
, va_gid
, kauth_cred_getgid(nfsd
->nd_cr
));
441 * Authorize the attribute changes.
443 if (((error
= vnode_authattr(vp
, vap
, &action
, &context
))) ||
444 ((error
= nfsrv_authorize(vp
, NULL
, action
, &context
, nxo
, 0))))
446 error
= vnode_setattr(vp
, vap
, &context
);
448 nfsm_srv_vattr_init(&postat
, v3
);
449 postat_ret
= vnode_getattr(vp
, &postat
, &context
);
454 nfsm_reply(NFSX_WCCORFATTR(v3
));
456 nfsm_srvwcc_data(preat_ret
, &preat
, postat_ret
, &postat
);
459 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
460 nfsm_srvfillattr(&postat
, fp
);
470 nfsrv_lookup(nfsd
, slp
, procp
, mrq
)
471 struct nfsrv_descript
*nfsd
;
472 struct nfssvc_sock
*slp
;
476 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
477 mbuf_t nam
= nfsd
->nd_nam
;
478 caddr_t dpos
= nfsd
->nd_dpos
;
479 struct nfs_fattr
*fp
;
480 struct nameidata nd
, *ndp
= &nd
;
481 /* XXX Revisit when enabling WebNFS */
482 #ifdef WEBNFS_ENABLED
483 struct nameidata ind
;
485 vnode_t vp
, dirp
= NULL
;
486 struct nfs_filehandle dnfh
, nfh
;
487 struct nfs_export
*nx
;
488 struct nfs_export_options
*nxo
;
493 int error
= 0, len
, dirattr_ret
= 1, isdotdot
;
494 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), pubflag
;
496 mbuf_t mb
, mb2
, mreq
;
497 struct vnode_attr va
, dirattr
, *vap
= &va
;
498 struct vfs_context context
;
500 context
.vc_proc
= procp
;
501 context
.vc_ucred
= nfsd
->nd_cr
;
503 nfsm_srvmtofh(&dnfh
);
504 nfsm_srvnamesiz(len
, v3
);
506 pubflag
= nfs_ispublicfh(&dnfh
);
508 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
509 nd
.ni_cnd
.cn_flags
= LOCKLEAF
;
510 error
= nfsm_path_mbuftond(&md
, &dpos
, v3
, pubflag
, &len
, &nd
);
511 isdotdot
= ((len
== 2) && (nd
.ni_cnd
.cn_pnbuf
[0] == '.') && (nd
.ni_cnd
.cn_pnbuf
[1] == '.'));
513 error
= nfs_namei(nfsd
, &context
, &nd
, &dnfh
, nam
, pubflag
, &dirp
, &nx
, &nxo
);
515 /* XXX Revisit when enabling WebNFS */
516 #ifdef WEBNFS_ENABLED
517 if (!error
&& pubflag
) {
518 if (vnode_vtype(nd
.ni_vp
) == VDIR
&& nfs_pub
.np_index
!= NULL
) {
520 * Setup call to lookup() to see if we can find
521 * the index file. Arguably, this doesn't belong
525 ind
.ni_pathlen
= strlen(nfs_pub
.np_index
);
526 ind
.ni_cnd
.cn_nameptr
= ind
.ni_cnd
.cn_pnbuf
=
528 ind
.ni_startdir
= nd
.ni_vp
;
529 ind
.ni_usedvp
= nd
.ni_vp
;
531 if (!(error
= lookup(&ind
))) {
533 * Found an index file. Get rid of
534 * the old references.
539 vnode_put(nd
.ni_startdir
);
545 * If the public filehandle was used, check that this lookup
546 * didn't result in a filehandle outside the publicly exported
550 if (!error
&& vnode_mount(ndp
->ni_vp
) != nfs_pub
.np_mount
) {
560 nfsm_srv_vattr_init(&dirattr
, v3
);
561 dirattr_ret
= vnode_getattr(dirp
, &dirattr
, &context
);
567 nfsm_reply(NFSX_POSTOPATTR(v3
));
568 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
574 error
= nfsrv_vptofh(nx
, !v3
, (isdotdot
? &dnfh
: NULL
), vp
, &context
, &nfh
);
576 nfsm_srv_vattr_init(vap
, v3
);
577 error
= vnode_getattr(vp
, vap
, &context
);
580 nfsm_reply(NFSX_SRVFH(v3
, &nfh
) + NFSX_POSTOPORFATTR(v3
) + NFSX_POSTOPATTR(v3
));
582 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
585 nfsm_srvfhtom(&nfh
, v3
);
587 nfsm_srvpostop_attr(0, vap
);
588 nfsm_srvpostop_attr(dirattr_ret
, &dirattr
);
590 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
591 nfsm_srvfillattr(vap
, fp
);
598 * nfs readlink service
601 nfsrv_readlink(nfsd
, slp
, procp
, mrq
)
602 struct nfsrv_descript
*nfsd
;
603 struct nfssvc_sock
*slp
;
607 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
608 mbuf_t nam
= nfsd
->nd_nam
;
609 caddr_t dpos
= nfsd
->nd_dpos
;
614 int error
= 0, i
, tlen
, len
, getret
= 1;
615 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
617 mbuf_t mb
, mb2
, mp2
, mp3
, mreq
;
619 struct vnode_attr attr
;
620 struct nfs_filehandle nfh
;
621 struct nfs_export
*nx
;
622 struct nfs_export_options
*nxo
;
624 char uio_buf
[ UIO_SIZEOF(4) ];
625 char *uio_bufp
= &uio_buf
[0];
626 int uio_buflen
= UIO_SIZEOF(4);
628 struct vfs_context context
;
636 while (len
< NFS_MAXPATHLEN
) {
638 if ((error
= mbuf_mclget(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mp
)))
640 mblen
= mbuf_maxlen(mp
);
641 mbuf_setlen(mp
, mblen
);
645 if ((error
= mbuf_setnext(mp2
, mp
))) {
651 if ((len
+ mblen
) > NFS_MAXPATHLEN
) {
652 mbuf_setlen(mp
, NFS_MAXPATHLEN
- len
);
653 len
= NFS_MAXPATHLEN
;
659 uio_buflen
= UIO_SIZEOF(i
);
660 MALLOC(uio_bufp
, char*, uio_buflen
, M_TEMP
, M_WAITOK
);
664 nfsm_reply(2 * NFSX_UNSIGNED
);
665 nfsm_srvpostop_attr(1, NULL
);
669 uiop
= uio_createwithbuffer(i
, 0, UIO_SYSSPACE
, UIO_READ
, uio_bufp
, uio_buflen
);
673 if (uio_bufp
!= &uio_buf
[0]) {
674 FREE(uio_bufp
, M_TEMP
);
675 uio_bufp
= &uio_buf
[0];
677 nfsm_reply(2 * NFSX_UNSIGNED
);
678 nfsm_srvpostop_attr(1, NULL
);
683 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), mbuf_len(mp
));
687 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
689 if (uio_bufp
!= &uio_buf
[0]) {
690 FREE(uio_bufp
, M_TEMP
);
691 uio_bufp
= &uio_buf
[0];
693 nfsm_reply(2 * NFSX_UNSIGNED
);
694 nfsm_srvpostop_attr(1, NULL
);
697 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
700 if (uio_bufp
!= &uio_buf
[0]) {
701 FREE(uio_bufp
, M_TEMP
);
702 uio_bufp
= &uio_buf
[0];
704 nfsm_reply(2 * NFSX_UNSIGNED
);
705 nfsm_srvpostop_attr(1, NULL
);
708 if (vnode_vtype(vp
) != VLNK
) {
716 context
.vc_proc
= procp
;
717 context
.vc_ucred
= nfsd
->nd_cr
;
719 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, &context
, nxo
, 0)))
721 error
= VNOP_READLINK(vp
, uiop
, &context
);
725 nfsm_srv_vattr_init(&attr
, v3
);
726 getret
= vnode_getattr(vp
, &attr
, &context
);
734 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_UNSIGNED
);
736 nfsm_srvpostop_attr(getret
, &attr
);
738 if (uio_bufp
!= &uio_buf
[0])
739 FREE(uio_bufp
, M_TEMP
);
744 if (uiop
&& (uio_resid(uiop
) > 0)) {
745 // LP64todo - fix this
746 len
-= uio_resid(uiop
);
747 tlen
= nfsm_rndup(len
);
748 nfsm_adj(mp3
, NFS_MAXPATHLEN
-tlen
, tlen
-len
);
750 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
751 *tl
= txdr_unsigned(len
);
752 mbuf_setnext(mb
, mp3
);
755 if (uio_bufp
!= &uio_buf
[0])
756 FREE(uio_bufp
, M_TEMP
);
764 nfsrv_read(nfsd
, slp
, procp
, mrq
)
765 struct nfsrv_descript
*nfsd
;
766 struct nfssvc_sock
*slp
;
770 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
771 mbuf_t nam
= nfsd
->nd_nam
;
772 caddr_t dpos
= nfsd
->nd_dpos
;
774 struct nfs_fattr
*fp
;
779 int error
= 0, count
, len
, left
, siz
, tlen
, getret
;
780 int v3
= (nfsd
->nd_flag
& ND_NFSV3
), reqlen
, maxlen
;
782 mbuf_t mb
, mb2
, mreq
;
785 struct nfs_filehandle nfh
;
786 struct nfs_export
*nx
;
787 struct nfs_export_options
*nxo
;
789 char *uio_bufp
= NULL
;
790 struct vnode_attr va
, *vap
= &va
;
792 char uio_buf
[ UIO_SIZEOF(0) ];
793 struct vfs_context context
;
797 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
798 fxdr_hyper(tl
, &off
);
800 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
801 off
= (off_t
)fxdr_unsigned(u_long
, *tl
);
803 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
804 reqlen
= fxdr_unsigned(u_long
, *tl
);
805 maxlen
= NFS_SRVMAXDATA(nfsd
);
809 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
810 nfsm_reply(2 * NFSX_UNSIGNED
);
811 nfsm_srvpostop_attr(1, NULL
);
814 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
816 nfsm_reply(2 * NFSX_UNSIGNED
);
817 nfsm_srvpostop_attr(1, NULL
);
820 if (vnode_vtype(vp
) != VREG
) {
824 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
827 context
.vc_proc
= procp
;
828 context
.vc_ucred
= nfsd
->nd_cr
;
831 if ((error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_READ_DATA
, &context
, nxo
, 1)))
832 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
, nxo
, 1);
834 nfsm_srv_vattr_init(vap
, v3
);
835 getret
= vnode_getattr(vp
, vap
, &context
);
840 nfsm_reply(NFSX_POSTOPATTR(v3
));
841 nfsm_srvpostop_attr(getret
, vap
);
844 if ((u_quad_t
)off
>= vap
->va_data_size
)
846 else if (((u_quad_t
)off
+ reqlen
) > vap
->va_data_size
)
847 count
= nfsm_rndup(vap
->va_data_size
- off
);
850 nfsm_reply(NFSX_POSTOPORFATTR(v3
) + 3 * NFSX_UNSIGNED
+nfsm_rndup(count
));
852 nfsm_build(tl
, u_long
*, NFSX_V3FATTR
+ 4 * NFSX_UNSIGNED
);
854 fp
= (struct nfs_fattr
*)tl
;
855 tl
+= (NFSX_V3FATTR
/ sizeof (u_long
));
857 nfsm_build(tl
, u_long
*, NFSX_V2FATTR
+ NFSX_UNSIGNED
);
858 fp
= (struct nfs_fattr
*)tl
;
859 tl
+= (NFSX_V2FATTR
/ sizeof (u_long
));
864 * Generate the mbuf list with the uio_iov ref. to it.
869 siz
= min(mbuf_trailingspace(m
), left
);
876 if ((error
= mbuf_mclget(MBUF_WAITOK
, MBUF_TYPE_DATA
, &m
)))
882 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
887 uiop
= uio_createwithbuffer(i
, off
, UIO_SYSSPACE
, UIO_READ
,
888 uio_bufp
, UIO_SIZEOF(i
));
898 panic("nfsrv_read iov");
899 siz
= min(mbuf_trailingspace(m
), left
);
902 uio_addiov(uiop
, CAST_USER_ADDR_T((char *)mbuf_data(m
) + tlen
), siz
);
903 mbuf_setlen(m
, tlen
+ siz
);
909 error
= VNOP_READ(vp
, uiop
, IO_NODELOCKED
, &context
);
910 off
= uio_offset(uiop
);
913 * This may seem a little weird that we drop the whole
914 * successful read if we get an error on the getattr.
915 * The reason is because we've already set up the reply
916 * to have postop attrs and omitting these optional bits
917 * would require shifting all the data in the reply.
919 * It would be more correct if we would simply drop the
920 * postop attrs if the getattr fails. We might be able to
921 * do that easier if we allocated separate mbufs for the data.
923 nfsm_srv_vattr_init(vap
, v3
);
924 if (error
|| (getret
= vnode_getattr(vp
, vap
, &context
))) {
929 nfsm_reply(NFSX_POSTOPATTR(v3
));
930 nfsm_srvpostop_attr(getret
, vap
);
931 if (uio_bufp
!= NULL
) {
932 FREE(uio_bufp
, M_TEMP
);
937 uiop
= uio_createwithbuffer(0, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
944 nfsm_srvfillattr(vap
, fp
);
945 // LP64todo - fix this
946 len
-= uio_resid(uiop
);
947 tlen
= nfsm_rndup(len
);
948 if (count
!= tlen
|| tlen
!= len
)
949 nfsm_adj(mb
, count
- tlen
, tlen
- len
);
951 *tl
++ = txdr_unsigned(len
);
957 *tl
= txdr_unsigned(len
);
959 if (uio_bufp
!= NULL
) {
960 FREE(uio_bufp
, M_TEMP
);
969 nfsrv_write(nfsd
, slp
, procp
, mrq
)
970 struct nfsrv_descript
*nfsd
;
971 struct nfssvc_sock
*slp
;
975 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
976 mbuf_t nam
= nfsd
->nd_nam
;
977 caddr_t dpos
= nfsd
->nd_dpos
;
980 struct nfs_fattr
*fp
;
981 struct vnode_attr va
, forat
;
982 struct vnode_attr
*vap
= &va
;
986 int error
= 0, len
, forat_ret
= 1;
987 int ioflags
, aftat_ret
= 1, retlen
, zeroing
, adjust
, tlen
;
988 int stable
= NFSV3WRITE_FILESYNC
;
989 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
991 mbuf_t mb
, mb2
, mreq
;
993 struct nfs_filehandle nfh
;
994 struct nfs_export
*nx
;
995 struct nfs_export_options
*nxo
;
998 char *uio_bufp
= NULL
;
999 struct vfs_context context
;
1005 nfsm_srvmtofh(&nfh
);
1007 nfsm_dissect(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1008 fxdr_hyper(tl
, &off
);
1010 stable
= fxdr_unsigned(int, *tl
++);
1012 nfsm_dissect(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1013 off
= (off_t
)fxdr_unsigned(u_long
, *++tl
);
1016 stable
= NFSV3WRITE_UNSTABLE
;
1018 retlen
= len
= fxdr_unsigned(long, *tl
);
1022 * For NFS Version 2, it is not obvious what a write of zero length
1023 * should do, but I might as well be consistent with Version 3,
1024 * which is to return ok so long as there are no permission problems.
1032 tpos
= mbuf_data(mp
);
1033 tlen
= mbuf_len(mp
);
1034 adjust
= dpos
- tpos
;
1036 mbuf_setlen(mp
, tlen
);
1037 if (tlen
> 0 && adjust
> 0) {
1039 if ((error
= mbuf_setdata(mp
, tpos
, tlen
))) {
1040 nfsm_reply(2 * NFSX_UNSIGNED
);
1041 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1048 else if ((tlen
= mbuf_len(mp
)) > 0) {
1051 mbuf_setlen(mp
, tlen
- (i
- len
));
1054 if (mbuf_len(mp
) > 0)
1060 if (len
> NFS_MAXDATA
|| len
< 0 || i
< len
) {
1062 nfsm_reply(2 * NFSX_UNSIGNED
);
1063 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1066 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
1067 nfsm_reply(2 * NFSX_UNSIGNED
);
1068 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1071 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
1073 nfsm_reply(2 * NFSX_UNSIGNED
);
1074 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1077 context
.vc_proc
= procp
;
1078 context
.vc_ucred
= nfsd
->nd_cr
;
1081 nfsm_srv_pre_vattr_init(&forat
, v3
);
1082 forat_ret
= vnode_getattr(vp
, &forat
, &context
);
1084 if (vnode_vtype(vp
) != VREG
) {
1088 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1091 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
, nxo
, 1);
1095 nfsm_reply(NFSX_WCCDATA(v3
));
1096 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1101 MALLOC(uio_bufp
, char *, UIO_SIZEOF(count
), M_TEMP
, M_WAITOK
);
1105 nfsm_reply(NFSX_WCCDATA(v3
));
1106 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1109 uiop
= uio_createwithbuffer(count
, off
, UIO_SYSSPACE
, UIO_WRITE
, uio_bufp
, UIO_SIZEOF(count
));
1113 nfsm_reply(NFSX_WCCDATA(v3
));
1114 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1115 if (uio_bufp
!= NULL
) {
1116 FREE(uio_bufp
, M_TEMP
);
1122 if ((tlen
= mbuf_len(mp
)) > 0)
1123 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), tlen
);
1129 * The IO_METASYNC flag indicates that all metadata (and not just
1130 * enough to ensure data integrity) mus be written to stable storage
1132 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1134 if (stable
== NFSV3WRITE_UNSTABLE
)
1135 ioflags
= IO_NODELOCKED
;
1136 else if (stable
== NFSV3WRITE_DATASYNC
)
1137 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1139 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1141 error
= VNOP_WRITE(vp
, uiop
, ioflags
, &context
);
1142 OSAddAtomic(1, (SInt32
*)(SInt32
*)&nfsstats
.srvvop_writes
);
1144 nfsm_srv_vattr_init(vap
, v3
);
1145 aftat_ret
= vnode_getattr(vp
, vap
, &context
);
1149 nfsm_reply(NFSX_PREOPATTR(v3
) + NFSX_POSTOPORFATTR(v3
) +
1150 2 * NFSX_UNSIGNED
+ NFSX_WRITEVERF(v3
));
1152 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, vap
);
1154 if (uio_bufp
!= NULL
) {
1155 FREE(uio_bufp
, M_TEMP
);
1159 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1160 *tl
++ = txdr_unsigned(retlen
);
1162 * If nfs_async is set, then pretend the write was FILESYNC.
1164 if (stable
== NFSV3WRITE_UNSTABLE
&& !nfs_async
)
1165 *tl
++ = txdr_unsigned(stable
);
1167 *tl
++ = txdr_unsigned(NFSV3WRITE_FILESYNC
);
1169 * Actually, there is no need to txdr these fields,
1170 * but it may make the values more human readable,
1171 * for debugging purposes.
1173 *tl
++ = txdr_unsigned(boottime_sec());
1174 *tl
= txdr_unsigned(0);
1176 nfsm_build(fp
, struct nfs_fattr
*, NFSX_V2FATTR
);
1177 nfsm_srvfillattr(vap
, fp
);
1180 if (uio_bufp
!= NULL
) {
1181 FREE(uio_bufp
, M_TEMP
);
1187 * NFS write service with write gathering support. Called when
1188 * nfsrvw_procrastinate > 0.
1189 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1190 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1194 nfsrv_writegather(ndp
, slp
, procp
, mrq
)
1195 struct nfsrv_descript
**ndp
;
1196 struct nfssvc_sock
*slp
;
1201 struct nfsrv_descript
*wp
, *nfsd
, *owp
, *swp
;
1202 struct nfs_export
*nx
;
1203 struct nfs_export_options
*nxo
;
1204 struct nfs_fattr
*fp
;
1206 struct nfsrvw_delayhash
*wpp
;
1208 struct vnode_attr va
, forat
;
1211 caddr_t bpos
, dpos
, tpos
;
1212 int error
= 0, len
, forat_ret
= 1;
1213 int ioflags
, aftat_ret
= 1, adjust
, v3
, zeroing
, tlen
;
1215 mbuf_t mb
, mb2
, mreq
, mrep
, md
;
1218 char *uio_bufp
= NULL
;
1221 struct vfs_context context
;
1223 context
.vc_proc
= procp
;
1234 mrep
= nfsd
->nd_mrep
;
1236 dpos
= nfsd
->nd_dpos
;
1238 context
.vc_ucred
= cred
;
1239 v3
= (nfsd
->nd_flag
& ND_NFSV3
);
1240 LIST_INIT(&nfsd
->nd_coalesce
);
1241 nfsd
->nd_mreq
= NULL
;
1242 nfsd
->nd_stable
= NFSV3WRITE_FILESYNC
;
1244 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1245 nfsd
->nd_time
= cur_usec
+
1246 (v3
? nfsrvw_procrastinate_v3
: nfsrvw_procrastinate
);
1249 * Now, get the write header..
1251 nfsm_srvmtofh(&nfsd
->nd_fh
);
1252 /* XXX shouldn't we be checking for invalid FHs before doing any more work? */
1254 nfsm_dissect(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1255 fxdr_hyper(tl
, &nfsd
->nd_off
);
1257 nfsd
->nd_stable
= fxdr_unsigned(int, *tl
++);
1259 nfsm_dissect(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1260 nfsd
->nd_off
= (off_t
)fxdr_unsigned(u_long
, *++tl
);
1263 nfsd
->nd_stable
= NFSV3WRITE_UNSTABLE
;
1265 len
= fxdr_unsigned(long, *tl
);
1267 nfsd
->nd_eoff
= nfsd
->nd_off
+ len
;
1270 * Trim the header out of the mbuf list and trim off any trailing
1271 * junk so that the mbuf list has only the write data.
1279 tpos
= mbuf_data(mp
);
1280 tlen
= mbuf_len(mp
);
1281 adjust
= dpos
- tpos
;
1283 mbuf_setlen(mp
, tlen
);
1284 if (tlen
> 0 && adjust
> 0) {
1286 if ((error
= mbuf_setdata(mp
, tpos
, tlen
)))
1293 tlen
= mbuf_len(mp
);
1296 mbuf_setlen(mp
, tlen
- (i
- len
));
1302 if (len
> NFS_MAXDATA
|| len
< 0 || i
< len
) {
1307 nfsm_writereply(2 * NFSX_UNSIGNED
, v3
);
1309 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1310 nfsd
->nd_mreq
= mreq
;
1311 nfsd
->nd_mrep
= NULL
;
1316 * Add this entry to the hash and time queues.
1318 lck_mtx_lock(&slp
->ns_wgmutex
);
1320 wp
= slp
->ns_tq
.lh_first
;
1321 while (wp
&& wp
->nd_time
< nfsd
->nd_time
) {
1323 wp
= wp
->nd_tq
.le_next
;
1326 LIST_INSERT_AFTER(owp
, nfsd
, nd_tq
);
1328 LIST_INSERT_HEAD(&slp
->ns_tq
, nfsd
, nd_tq
);
1330 if (nfsd
->nd_mrep
) {
1331 wpp
= NWDELAYHASH(slp
, nfsd
->nd_fh
.nfh_fid
);
1334 while (wp
&& !nfsrv_fhmatch(&nfsd
->nd_fh
, &wp
->nd_fh
)) {
1336 wp
= wp
->nd_hash
.le_next
;
1338 while (wp
&& (wp
->nd_off
< nfsd
->nd_off
) &&
1339 nfsrv_fhmatch(&nfsd
->nd_fh
, &wp
->nd_fh
)) {
1341 wp
= wp
->nd_hash
.le_next
;
1344 LIST_INSERT_AFTER(owp
, nfsd
, nd_hash
);
1347 * Search the hash list for overlapping entries and
1350 for(; nfsd
&& NFSW_CONTIG(owp
, nfsd
); nfsd
= wp
) {
1351 wp
= nfsd
->nd_hash
.le_next
;
1352 if (NFSW_SAMECRED(owp
, nfsd
))
1353 nfsrvw_coalesce(owp
, nfsd
);
1356 LIST_INSERT_HEAD(wpp
, nfsd
, nd_hash
);
1360 lck_mtx_lock(&slp
->ns_wgmutex
);
1364 * Now, do VNOP_WRITE()s for any one(s) that need to be done now
1365 * and generate the associated reply mbuf list(s).
1369 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1370 for (nfsd
= slp
->ns_tq
.lh_first
; nfsd
; nfsd
= owp
) {
1371 owp
= nfsd
->nd_tq
.le_next
;
1372 if (nfsd
->nd_time
> cur_usec
)
1376 LIST_REMOVE(nfsd
, nd_tq
);
1377 LIST_REMOVE(nfsd
, nd_hash
);
1378 mrep
= nfsd
->nd_mrep
;
1379 nfsd
->nd_mrep
= NULL
;
1380 v3
= (nfsd
->nd_flag
& ND_NFSV3
);
1381 forat_ret
= aftat_ret
= 1;
1382 error
= nfsrv_fhtovp(&nfsd
->nd_fh
, nfsd
->nd_nam
, TRUE
, &vp
, &nx
, &nxo
);
1384 error
= nfsrv_credcheck(nfsd
, nx
, nxo
);
1389 context
.vc_ucred
= cred
;
1392 nfsm_srv_pre_vattr_init(&forat
, v3
);
1393 forat_ret
= vnode_getattr(vp
, &forat
, &context
);
1395 if (vnode_vtype(vp
) != VREG
) {
1399 error
= (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EACCES
;
1404 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_WRITE_DATA
, &context
, nxo
, 1);
1407 if (nfsd
->nd_stable
== NFSV3WRITE_UNSTABLE
)
1408 ioflags
= IO_NODELOCKED
;
1409 else if (nfsd
->nd_stable
== NFSV3WRITE_DATASYNC
)
1410 ioflags
= (IO_SYNC
| IO_NODELOCKED
);
1412 ioflags
= (IO_METASYNC
| IO_SYNC
| IO_NODELOCKED
);
1414 if (!error
&& ((nfsd
->nd_eoff
- nfsd
->nd_off
) > 0)) {
1418 if (mbuf_len(mp
) > 0)
1423 MALLOC(uio_bufp
, char *, UIO_SIZEOF(i
), M_TEMP
, M_WAITOK
);
1425 uiop
= uio_createwithbuffer(i
, nfsd
->nd_off
, UIO_SYSSPACE
,
1426 UIO_WRITE
, uio_bufp
, UIO_SIZEOF(i
));
1427 if (!uio_bufp
|| !uiop
)
1432 if ((tlen
= mbuf_len(mp
)) > 0)
1433 uio_addiov(uiop
, CAST_USER_ADDR_T((caddr_t
)mbuf_data(mp
)), tlen
);
1436 error
= VNOP_WRITE(vp
, uiop
, ioflags
, &context
);
1437 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvvop_writes
);
1440 FREE(uio_bufp
, M_TEMP
);
1447 nfsm_srv_pre_vattr_init(&va
, v3
);
1448 aftat_ret
= vnode_getattr(vp
, &va
, &context
);
1453 * Loop around generating replies for all write rpcs that have
1454 * now been completed.
1459 nfsm_writereply(NFSX_WCCDATA(v3
), v3
);
1461 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1464 nfsm_writereply(NFSX_PREOPATTR(v3
) +
1465 NFSX_POSTOPORFATTR(v3
) + 2 * NFSX_UNSIGNED
+
1466 NFSX_WRITEVERF(v3
), v3
);
1468 nfsm_srvwcc_data(forat_ret
, &forat
, aftat_ret
, &va
);
1469 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1470 *tl
++ = txdr_unsigned(nfsd
->nd_len
);
1471 *tl
++ = txdr_unsigned(swp
->nd_stable
);
1473 * Actually, there is no need to txdr these fields,
1474 * but it may make the values more human readable,
1475 * for debugging purposes.
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
;
3730 struct nfs_export
*nx
;
3731 struct nfs_export_options
*nxo
;
3733 char uio_buf
[ UIO_SIZEOF(1) ];
3734 struct vnode_attr va
, at
, *vap
= &va
;
3735 struct nfs_fattr
*fp
;
3736 int len
, nlen
, rem
, xfer
, tsiz
, i
, error
= 0, getret
= 1;
3737 int siz
, dircount
, maxcount
, fullsiz
, eofflag
, dirlen
, nentries
= 0, isdotdot
;
3738 u_quad_t off
, toff
, verf
;
3741 struct vfs_context context
;
3743 vnopflag
= VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
;
3745 nfsm_srvmtofh(&dnfh
);
3746 nfsm_dissect(tl
, u_long
*, 6 * NFSX_UNSIGNED
);
3747 fxdr_hyper(tl
, &toff
);
3749 fxdr_hyper(tl
, &verf
);
3751 dircount
= fxdr_unsigned(int, *tl
++);
3752 maxcount
= fxdr_unsigned(int, *tl
);
3754 xfer
= NFS_SRVMAXDATA(nfsd
);
3755 dircount
= ((dircount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
3756 if (dircount
> xfer
)
3758 fullsiz
= siz
= dircount
;
3759 maxcount
= ((maxcount
+ DIRBLKSIZ
- 1) & ~(DIRBLKSIZ
- 1));
3760 if (maxcount
> xfer
)
3763 if ((error
= nfsrv_fhtovp(&dnfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
3764 nfsm_reply(NFSX_UNSIGNED
);
3765 nfsm_srvpostop_attr(getret
, &at
);
3768 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
3770 nfsm_reply(NFSX_UNSIGNED
);
3771 nfsm_srvpostop_attr(getret
, &at
);
3774 context
.vc_proc
= procp
;
3775 context
.vc_ucred
= nfsd
->nd_cr
;
3776 if (nxo
->nxo_flags
& NX_32BITCLIENTS
)
3777 vnopflag
|= VNODE_READDIR_SEEKOFF32
;
3778 nfsm_srv_vattr_init(&at
, 1);
3779 error
= getret
= vnode_getattr(vp
, &at
, &context
);
3780 if (!error
&& toff
&& verf
&& verf
!= at
.va_filerev
)
3781 error
= NFSERR_BAD_COOKIE
;
3783 error
= nfsrv_authorize(vp
, NULL
, KAUTH_VNODE_LIST_DIRECTORY
, &context
, nxo
, 0);
3787 nfsm_reply(NFSX_V3POSTOPATTR
);
3788 nfsm_srvpostop_attr(getret
, &at
);
3791 MALLOC(rbuf
, caddr_t
, siz
, M_TEMP
, M_WAITOK
);
3796 nfsm_reply(NFSX_V3POSTOPATTR
);
3797 nfsm_srvpostop_attr(getret
, &at
);
3800 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3801 &uio_buf
[0], sizeof(uio_buf
));
3807 nfsm_reply(NFSX_V3POSTOPATTR
);
3808 nfsm_srvpostop_attr(getret
, &at
);
3812 uio_reset(auio
, off
, UIO_SYSSPACE
, UIO_READ
);
3813 uio_addiov(auio
, CAST_USER_ADDR_T(rbuf
), fullsiz
);
3815 error
= VNOP_READDIR(vp
, auio
, vnopflag
, &eofflag
, &nentries
, &context
);
3816 off
= uio_offset(auio
);
3817 nfsm_srv_vattr_init(&at
, 1);
3818 getret
= vnode_getattr(vp
, &at
, &context
);
3826 nfsm_reply(NFSX_V3POSTOPATTR
);
3827 nfsm_srvpostop_attr(getret
, &at
);
3830 if (uio_resid(auio
) != 0) {
3831 // LP64todo - fix this
3832 siz
-= uio_resid(auio
);
3835 * If nothing read, return eof
3841 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+
3843 nfsm_srvpostop_attr(getret
, &at
);
3844 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
3845 txdr_hyper(&at
.va_filerev
, tl
);
3855 * Check for degenerate cases of nothing useful read.
3856 * If so go try again
3860 dp
= (struct direntry
*)cpos
;
3861 while (dp
->d_fileno
== 0 && cpos
< cend
&& nentries
> 0) {
3862 cpos
+= dp
->d_reclen
;
3863 dp
= (struct direntry
*)cpos
;
3866 if (cpos
>= cend
|| nentries
== 0) {
3873 * Probe one of the directory entries to see if the filesystem
3876 if ((error
= VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, &context
))) {
3877 if (error
== ENOTSUP
) /* let others get passed back */
3878 error
= NFSERR_NOTSUPP
;
3882 nfsm_reply(NFSX_V3POSTOPATTR
);
3883 nfsm_srvpostop_attr(getret
, &at
);
3888 dirlen
= len
= NFSX_V3POSTOPATTR
+ NFSX_V3COOKIEVERF
+ 2 * NFSX_UNSIGNED
;
3889 nfsm_reply(maxcount
);
3890 nfsm_srvpostop_attr(getret
, &at
);
3891 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3892 txdr_hyper(&at
.va_filerev
, tl
);
3895 be
= bp
+ mbuf_trailingspace(mp
);
3897 /* Loop through the records and build reply */
3898 while (cpos
< cend
&& nentries
> 0) {
3899 if (dp
->d_fileno
!= 0) {
3900 nlen
= dp
->d_namlen
;
3901 rem
= nfsm_rndup(nlen
)-nlen
;
3904 * Got to get the vnode for lookup per entry.
3906 if (VFS_VGET(vnode_mount(vp
), (ino64_t
)dp
->d_fileno
, &nvp
, &context
))
3908 isdotdot
= ((dp
->d_namlen
== 2) &&
3909 (dp
->d_name
[0] == '.') && (dp
->d_name
[1] == '.'));
3910 if (nfsrv_vptofh(nx
, 0, (isdotdot
? &dnfh
: NULL
), nvp
, &context
, nfhp
)) {
3911 // XXX file handle is optional, so we should be able to
3912 // XXX return this entry without the file handle
3916 nfsm_srv_vattr_init(vap
, 1);
3917 if (vnode_getattr(nvp
, vap
, &context
)) {
3918 // XXX attributes are optional, so we should be able to
3919 // XXX return this entry without the attributes
3926 * If either the dircount or maxcount will be
3927 * exceeded, get out now. Both of these lengths
3928 * are calculated conservatively, including all
3931 len
+= (8 * NFSX_UNSIGNED
+ nlen
+ rem
+ nfsm_rndup(nfhp
->nfh_len
) +
3933 dirlen
+= (6 * NFSX_UNSIGNED
+ nlen
+ rem
);
3934 if ((len
> maxcount
) || (dirlen
> dircount
)) {
3940 * Build the directory record xdr from
3941 * the direntry entry.
3943 fp
= (struct nfs_fattr
*)&fl
.fl_fattr
;
3944 nfsm_srvfillattr(vap
, fp
);
3945 fl
.fl_fhsize
= txdr_unsigned(nfhp
->nfh_len
);
3946 fl
.fl_fhok
= nfs_true
;
3947 fl
.fl_postopok
= nfs_true
;
3948 if (vnopflag
& VNODE_READDIR_SEEKOFF32
)
3949 dp
->d_seekoff
&= 0x00000000ffffffffULL
;
3950 txdr_hyper(&dp
->d_seekoff
, &fl
.fl_off
);
3954 bp
+= NFSX_UNSIGNED
;
3957 txdr_hyper(&dp
->d_fileno
, &tquad
);
3958 *tl
= tquad
.nfsuquad
[0];
3959 bp
+= NFSX_UNSIGNED
;
3961 *tl
= tquad
.nfsuquad
[1];
3962 bp
+= NFSX_UNSIGNED
;
3965 *tl
= txdr_unsigned(nlen
);
3966 bp
+= NFSX_UNSIGNED
;
3968 /* And loop around copying the name */
3973 if ((bp
+ xfer
) > be
)
3977 bcopy(cp
, bp
, tsiz
);
3983 /* And null pad to a long boundary */
3984 for (i
= 0; i
< rem
; i
++)
3988 * Now copy the flrep structure out.
3990 xfer
= sizeof(struct flrep
) - sizeof(fl
.fl_nfh
) + fl
.fl_fhsize
;
3994 if ((bp
+ xfer
) > be
)
3998 bcopy(cp
, bp
, tsiz
);
4006 cpos
+= dp
->d_reclen
;
4007 dp
= (struct direntry
*)cpos
;
4014 bp
+= NFSX_UNSIGNED
;
4020 bp
+= NFSX_UNSIGNED
;
4023 mbuf_setlen(mp
, bp
- (char*)mbuf_data(mp
));
4025 mbuf_setlen(mp
, mbuf_len(mp
) + (bp
- bpos
));
4034 * nfs commit service
4037 nfsrv_commit(nfsd
, slp
, procp
, mrq
)
4038 struct nfsrv_descript
*nfsd
;
4039 struct nfssvc_sock
*slp
;
4043 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4044 mbuf_t nam
= nfsd
->nd_nam
;
4045 caddr_t dpos
= nfsd
->nd_dpos
;
4046 struct vnode_attr bfor
, aft
;
4048 struct nfs_filehandle nfh
;
4049 struct nfs_export
*nx
;
4050 struct nfs_export_options
*nxo
;
4054 int error
= 0, for_ret
= 1, aft_ret
= 1, count
;
4056 mbuf_t mb
, mb2
, mreq
;
4058 struct vfs_context context
;
4060 nfsm_srvmtofh(&nfh
);
4061 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
4064 * XXX At this time VNOP_FSYNC() does not accept offset and byte
4065 * count parameters, so these arguments are useless (someday maybe).
4067 fxdr_hyper(tl
, &off
);
4069 count
= fxdr_unsigned(int, *tl
);
4070 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4071 nfsm_reply(2 * NFSX_UNSIGNED
);
4072 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4075 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4077 nfsm_reply(2 * NFSX_UNSIGNED
);
4078 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4081 context
.vc_proc
= procp
;
4082 context
.vc_ucred
= nfsd
->nd_cr
;
4084 nfsm_srv_pre_vattr_init(&bfor
, 1);
4085 for_ret
= vnode_getattr(vp
, &bfor
, &context
);
4086 error
= VNOP_FSYNC(vp
, MNT_WAIT
, &context
);
4087 nfsm_srv_vattr_init(&aft
, 1);
4088 aft_ret
= vnode_getattr(vp
, &aft
, &context
);
4090 nfsm_reply(NFSX_V3WCCDATA
+ NFSX_V3WRITEVERF
);
4091 nfsm_srvwcc_data(for_ret
, &bfor
, aft_ret
, &aft
);
4093 nfsm_build(tl
, u_long
*, NFSX_V3WRITEVERF
);
4094 *tl
++ = txdr_unsigned(boottime_sec());
4095 *tl
= txdr_unsigned(0);
4103 * nfs statfs service
4106 nfsrv_statfs(nfsd
, slp
, procp
, mrq
)
4107 struct nfsrv_descript
*nfsd
;
4108 struct nfssvc_sock
*slp
;
4112 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4113 mbuf_t nam
= nfsd
->nd_nam
;
4114 caddr_t dpos
= nfsd
->nd_dpos
;
4116 struct nfs_statfs
*sfp
;
4120 int error
= 0, getret
= 1;
4121 int v3
= (nfsd
->nd_flag
& ND_NFSV3
);
4123 mbuf_t mb
, mb2
, mreq
;
4125 struct vnode_attr at
;
4126 struct nfs_filehandle nfh
;
4127 struct nfs_export
*nx
;
4128 struct nfs_export_options
*nxo
;
4131 struct vfs_context context
;
4133 nfsm_srvmtofh(&nfh
);
4134 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4135 nfsm_reply(NFSX_UNSIGNED
);
4136 nfsm_srvpostop_attr(getret
, &at
);
4139 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4141 nfsm_reply(NFSX_UNSIGNED
);
4142 nfsm_srvpostop_attr(getret
, &at
);
4145 context
.vc_proc
= procp
;
4146 context
.vc_ucred
= nfsd
->nd_cr
;
4149 VFSATTR_WANTED(&va
, f_blocks
);
4150 VFSATTR_WANTED(&va
, f_bavail
);
4151 VFSATTR_WANTED(&va
, f_files
);
4152 VFSATTR_WANTED(&va
, f_ffree
);
4153 error
= vfs_getattr(vnode_mount(vp
), &va
, &context
);
4154 blksize
= vnode_mount(vp
)->mnt_vfsstat
.f_bsize
;
4155 nfsm_srv_vattr_init(&at
, v3
);
4156 getret
= vnode_getattr(vp
, &at
, &context
);
4158 nfsm_reply(NFSX_POSTOPATTR(v3
) + NFSX_STATFS(v3
));
4160 nfsm_srvpostop_attr(getret
, &at
);
4163 nfsm_build(sfp
, struct nfs_statfs
*, NFSX_STATFS(v3
));
4165 tval
= (u_quad_t
)(va
.f_blocks
* blksize
);
4166 txdr_hyper(&tval
, &sfp
->sf_tbytes
);
4167 tval
= (u_quad_t
)(va
.f_bfree
* blksize
);
4168 txdr_hyper(&tval
, &sfp
->sf_fbytes
);
4169 tval
= (u_quad_t
)(va
.f_bavail
* blksize
);
4170 txdr_hyper(&tval
, &sfp
->sf_abytes
);
4171 txdr_hyper(&va
.f_files
, &sfp
->sf_tfiles
);
4172 txdr_hyper(&va
.f_ffree
, &sfp
->sf_ffiles
);
4173 txdr_hyper(&va
.f_ffree
, &sfp
->sf_afiles
);
4174 sfp
->sf_invarsec
= 0;
4176 sfp
->sf_tsize
= txdr_unsigned(NFS_V2MAXDATA
);
4177 sfp
->sf_bsize
= txdr_unsigned((unsigned)blksize
);
4178 sfp
->sf_blocks
= txdr_unsigned((unsigned)va
.f_blocks
);
4179 sfp
->sf_bfree
= txdr_unsigned((unsigned)va
.f_bfree
);
4180 sfp
->sf_bavail
= txdr_unsigned((unsigned)va
.f_bavail
);
4187 * nfs fsinfo service
4190 nfsrv_fsinfo(nfsd
, slp
, procp
, mrq
)
4191 struct nfsrv_descript
*nfsd
;
4192 struct nfssvc_sock
*slp
;
4196 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4197 mbuf_t nam
= nfsd
->nd_nam
;
4198 caddr_t dpos
= nfsd
->nd_dpos
;
4200 struct nfsv3_fsinfo
*sip
;
4203 int error
= 0, getret
= 1, prefsize
, maxsize
;
4205 mbuf_t mb
, mb2
, mreq
;
4207 struct vnode_attr at
;
4208 struct nfs_filehandle nfh
;
4209 struct nfs_export
*nx
;
4210 struct nfs_export_options
*nxo
;
4211 struct vfs_context context
;
4213 nfsm_srvmtofh(&nfh
);
4214 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4215 nfsm_reply(NFSX_UNSIGNED
);
4216 nfsm_srvpostop_attr(getret
, &at
);
4219 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4221 nfsm_reply(NFSX_UNSIGNED
);
4222 nfsm_srvpostop_attr(getret
, &at
);
4225 context
.vc_proc
= procp
;
4226 context
.vc_ucred
= nfsd
->nd_cr
;
4228 nfsm_srv_vattr_init(&at
, 1);
4229 getret
= vnode_getattr(vp
, &at
, &context
);
4231 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3FSINFO
);
4232 nfsm_srvpostop_attr(getret
, &at
);
4233 nfsm_build(sip
, struct nfsv3_fsinfo
*, NFSX_V3FSINFO
);
4237 * There should be file system VFS OP(s) to get this information.
4238 * For now, assume our usual NFS defaults.
4240 if (slp
->ns_sotype
== SOCK_DGRAM
) {
4241 maxsize
= NFS_MAXDGRAMDATA
;
4242 prefsize
= NFS_PREFDGRAMDATA
;
4244 maxsize
= prefsize
= NFS_MAXDATA
;
4245 sip
->fs_rtmax
= txdr_unsigned(maxsize
);
4246 sip
->fs_rtpref
= txdr_unsigned(prefsize
);
4247 sip
->fs_rtmult
= txdr_unsigned(NFS_FABLKSIZE
);
4248 sip
->fs_wtmax
= txdr_unsigned(maxsize
);
4249 sip
->fs_wtpref
= txdr_unsigned(prefsize
);
4250 sip
->fs_wtmult
= txdr_unsigned(NFS_FABLKSIZE
);
4251 sip
->fs_dtpref
= txdr_unsigned(prefsize
);
4252 sip
->fs_maxfilesize
.nfsuquad
[0] = 0xffffffff;
4253 sip
->fs_maxfilesize
.nfsuquad
[1] = 0xffffffff;
4254 sip
->fs_timedelta
.nfsv3_sec
= 0;
4255 sip
->fs_timedelta
.nfsv3_nsec
= txdr_unsigned(1);
4256 sip
->fs_properties
= txdr_unsigned(NFSV3FSINFO_LINK
|
4257 NFSV3FSINFO_SYMLINK
| NFSV3FSINFO_HOMOGENEOUS
|
4258 NFSV3FSINFO_CANSETTIME
);
4264 * nfs pathconf service
4267 nfsrv_pathconf(nfsd
, slp
, procp
, mrq
)
4268 struct nfsrv_descript
*nfsd
;
4269 struct nfssvc_sock
*slp
;
4273 mbuf_t mrep
= nfsd
->nd_mrep
, md
= nfsd
->nd_md
;
4274 mbuf_t nam
= nfsd
->nd_nam
;
4275 caddr_t dpos
= nfsd
->nd_dpos
;
4277 struct nfsv3_pathconf
*pc
;
4280 int error
= 0, getret
= 1, linkmax
, namemax
;
4281 int chownres
, notrunc
, case_sensitive
, case_preserving
;
4283 mbuf_t mb
, mb2
, mreq
;
4285 struct vnode_attr at
;
4286 struct nfs_filehandle nfh
;
4287 struct nfs_export
*nx
;
4288 struct nfs_export_options
*nxo
;
4289 struct vfs_context context
;
4291 nfsm_srvmtofh(&nfh
);
4292 if ((error
= nfsrv_fhtovp(&nfh
, nam
, TRUE
, &vp
, &nx
, &nxo
))) {
4293 nfsm_reply(NFSX_UNSIGNED
);
4294 nfsm_srvpostop_attr(getret
, &at
);
4297 if ((error
= nfsrv_credcheck(nfsd
, nx
, nxo
))) {
4299 nfsm_reply(NFSX_UNSIGNED
);
4300 nfsm_srvpostop_attr(getret
, &at
);
4303 context
.vc_proc
= procp
;
4304 context
.vc_ucred
= nfsd
->nd_cr
;
4306 error
= VNOP_PATHCONF(vp
, _PC_LINK_MAX
, &linkmax
, &context
);
4308 error
= VNOP_PATHCONF(vp
, _PC_NAME_MAX
, &namemax
, &context
);
4310 error
= VNOP_PATHCONF(vp
, _PC_CHOWN_RESTRICTED
, &chownres
, &context
);
4312 error
= VNOP_PATHCONF(vp
, _PC_NO_TRUNC
, ¬runc
, &context
);
4314 error
= VNOP_PATHCONF(vp
, _PC_CASE_SENSITIVE
, &case_sensitive
, &context
);
4316 error
= VNOP_PATHCONF(vp
, _PC_CASE_PRESERVING
, &case_preserving
, &context
);
4317 nfsm_srv_vattr_init(&at
, 1);
4318 getret
= vnode_getattr(vp
, &at
, &context
);
4320 nfsm_reply(NFSX_V3POSTOPATTR
+ NFSX_V3PATHCONF
);
4321 nfsm_srvpostop_attr(getret
, &at
);
4324 nfsm_build(pc
, struct nfsv3_pathconf
*, NFSX_V3PATHCONF
);
4326 pc
->pc_linkmax
= txdr_unsigned(linkmax
);
4327 pc
->pc_namemax
= txdr_unsigned(namemax
);
4328 pc
->pc_notrunc
= txdr_unsigned(notrunc
);
4329 pc
->pc_chownrestricted
= txdr_unsigned(chownres
);
4330 pc
->pc_caseinsensitive
= txdr_unsigned(!case_sensitive
);
4331 pc
->pc_casepreserving
= txdr_unsigned(case_preserving
);
4338 * Null operation, used by clients to ping server
4343 struct nfsrv_descript
*nfsd
,
4344 struct nfssvc_sock
*slp
,
4345 __unused proc_t procp
,
4348 mbuf_t mrep
= nfsd
->nd_mrep
;
4350 int error
= NFSERR_RETVOID
;
4359 * No operation, used for obsolete procedures
4364 struct nfsrv_descript
*nfsd
,
4365 struct nfssvc_sock
*slp
,
4366 __unused proc_t procp
,
4369 mbuf_t mrep
= nfsd
->nd_mrep
;
4374 if (nfsd
->nd_repstat
)
4375 error
= nfsd
->nd_repstat
;
4377 error
= EPROCUNAVAIL
;
4384 * Perform access checking for vnodes obtained from file handles that would
4385 * refer to files already opened by a Unix client. You cannot just use
4386 * vnode_authorize() for two reasons.
4387 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4388 * 2 - The owner is to be given access irrespective of mode bits so that
4389 * processes that chmod after opening a file don't break. I don't like
4390 * this because it opens a security hole, but since the nfs server opens
4391 * a security hole the size of a barn door anyhow, what the heck.
4393 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, vnode_authorize()
4394 * will return EPERM instead of EACCESS. EPERM is always an error.
4401 kauth_action_t action
,
4402 vfs_context_t context
,
4403 struct nfs_export_options
*nxo
,
4406 struct vnode_attr vattr
;
4409 if (action
& KAUTH_VNODE_WRITE_RIGHTS
) {
4411 * Disallow write attempts on read-only exports;
4412 * unless the file is a socket or a block or character
4413 * device resident on the file system.
4415 if (nxo
->nxo_flags
& NX_READONLY
) {
4416 switch (vnode_vtype(vp
)) {
4417 case VREG
: case VDIR
: case VLNK
: case VCPLX
:
4424 error
= vnode_authorize(vp
, dvp
, action
, context
);
4426 * Allow certain operations for the owner (reads and writes
4427 * on files that are already open). Picking up from FreeBSD.
4429 if (override
&& (error
== EACCES
)) {
4431 VATTR_WANTED(&vattr
, va_uid
);
4432 if ((vnode_getattr(vp
, &vattr
, context
) == 0) &&
4433 (kauth_cred_getuid(vfs_context_ucred(context
)) == vattr
.va_uid
))
4438 #endif /* NFS_NOSERVER */