2 * Copyright (c) 2006-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * vnode op calls for NFS version 4
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/resourcevar.h>
36 #include <sys/proc_internal.h>
37 #include <sys/kauth.h>
38 #include <sys/mount_internal.h>
39 #include <sys/malloc.h>
40 #include <sys/kpi_mbuf.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/dirent.h>
44 #include <sys/fcntl.h>
45 #include <sys/lockf.h>
46 #include <sys/ubc_internal.h>
48 #include <sys/signalvar.h>
49 #include <sys/uio_internal.h>
50 #include <sys/xattr.h>
51 #include <sys/paths.h>
53 #include <vfs/vfs_support.h>
58 #include <kern/clock.h>
59 #include <libkern/OSAtomic.h>
61 #include <miscfs/fifofs/fifo.h>
62 #include <miscfs/specfs/specdev.h>
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
67 #include <nfs/nfsnode.h>
68 #include <nfs/nfs_gss.h>
69 #include <nfs/nfsmount.h>
70 #include <nfs/nfs_lock.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfs/nfsm_subs.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <vm/vm_kern.h>
79 #include <kern/task.h>
80 #include <kern/sched_prim.h>
83 nfs4_access_rpc(nfsnode_t np
, u_int32_t
*access
, int rpcflags
, vfs_context_t ctx
)
85 int error
= 0, lockerror
= ENOENT
, status
, numops
, slot
;
87 struct nfsm_chain nmreq
, nmrep
;
89 uint32_t access_result
= 0, supported
= 0, missing
;
90 struct nfsmount
*nmp
= NFSTONMP(np
);
91 int nfsvers
= nmp
->nm_vers
;
93 struct nfsreq_secinfo_args si
;
95 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
98 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
99 nfsm_chain_null(&nmreq
);
100 nfsm_chain_null(&nmrep
);
102 // PUTFH, ACCESS, GETATTR
104 nfsm_chain_build_alloc_init(error
, &nmreq
, 17 * NFSX_UNSIGNED
);
105 nfsm_chain_add_compound_header(error
, &nmreq
, "access", nmp
->nm_minor_vers
, numops
);
107 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
108 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
110 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_ACCESS
);
111 nfsm_chain_add_32(error
, &nmreq
, *access
);
113 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
114 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
115 nfsm_chain_build_done(error
, &nmreq
);
116 nfsm_assert(error
, (numops
== 0), EPROTO
);
118 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
119 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
120 &si
, rpcflags
, &nmrep
, &xid
, &status
);
122 if ((lockerror
= nfs_node_lock(np
)))
124 nfsm_chain_skip_tag(error
, &nmrep
);
125 nfsm_chain_get_32(error
, &nmrep
, numops
);
126 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
127 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_ACCESS
);
128 nfsm_chain_get_32(error
, &nmrep
, supported
);
129 nfsm_chain_get_32(error
, &nmrep
, access_result
);
131 if ((missing
= (*access
& ~supported
))) {
132 /* missing support for something(s) we wanted */
133 if (missing
& NFS_ACCESS_DELETE
) {
135 * If the server doesn't report DELETE (possible
136 * on UNIX systems), we'll assume that it is OK
137 * and just let any subsequent delete action fail
138 * if it really isn't deletable.
140 access_result
|= NFS_ACCESS_DELETE
;
143 /* ".zfs" subdirectories may erroneously give a denied answer for modify/delete */
144 if (nfs_access_dotzfs
) {
145 vnode_t dvp
= NULLVP
;
146 if (np
->n_flag
& NISDOTZFSCHILD
) /* may be able to create/delete snapshot dirs */
147 access_result
|= (NFS_ACCESS_MODIFY
|NFS_ACCESS_EXTEND
|NFS_ACCESS_DELETE
);
148 else if (((dvp
= vnode_getparent(NFSTOV(np
))) != NULLVP
) && (VTONFS(dvp
)->n_flag
& NISDOTZFSCHILD
))
149 access_result
|= NFS_ACCESS_DELETE
; /* may be able to delete snapshot dirs */
153 /* Some servers report DELETE support but erroneously give a denied answer. */
154 if (nfs_access_delete
&& (*access
& NFS_ACCESS_DELETE
) && !(access_result
& NFS_ACCESS_DELETE
))
155 access_result
|= NFS_ACCESS_DELETE
;
156 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
157 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
160 if (nfs_mount_gone(nmp
)) {
165 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
166 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
168 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
170 slot
= nfs_node_access_slot(np
, uid
, 1);
171 np
->n_accessuid
[slot
] = uid
;
173 np
->n_accessstamp
[slot
] = now
.tv_sec
;
174 np
->n_access
[slot
] = access_result
;
176 /* pass back the access returned with this request */
177 *access
= np
->n_access
[slot
];
181 nfsm_chain_cleanup(&nmreq
);
182 nfsm_chain_cleanup(&nmrep
);
194 struct nfs_vattr
*nvap
,
197 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
198 int error
= 0, status
, nfsvers
, numops
, rpcflags
= 0, acls
;
199 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
200 struct nfsm_chain nmreq
, nmrep
;
201 struct nfsreq_secinfo_args si
;
203 if (nfs_mount_gone(nmp
))
205 nfsvers
= nmp
->nm_vers
;
206 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
208 if (np
&& (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)) {
209 nfs4_default_attrs_for_referral_trigger(VTONFS(np
->n_parent
), NULL
, 0, nvap
, NULL
);
213 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
214 rpcflags
= R_RECOVER
;
216 if (flags
& NGA_SOFT
) /* Return ETIMEDOUT if server not responding */
219 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
220 nfsm_chain_null(&nmreq
);
221 nfsm_chain_null(&nmrep
);
225 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
226 nfsm_chain_add_compound_header(error
, &nmreq
, "getattr", nmp
->nm_minor_vers
, numops
);
228 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
229 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fhp
, fhsize
);
231 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
232 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
233 if ((flags
& NGA_ACL
) && acls
)
234 NFS_BITMAP_SET(bitmap
, NFS_FATTR_ACL
);
235 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
236 nfsm_chain_build_done(error
, &nmreq
);
237 nfsm_assert(error
, (numops
== 0), EPROTO
);
239 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC4_COMPOUND
,
240 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
241 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
243 nfsm_chain_skip_tag(error
, &nmrep
);
244 nfsm_chain_get_32(error
, &nmrep
, numops
);
245 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
246 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
248 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
250 if ((flags
& NGA_ACL
) && acls
&& !NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_ACL
)) {
251 /* we asked for the ACL but didn't get one... assume there isn't one */
252 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
253 nvap
->nva_acl
= NULL
;
256 nfsm_chain_cleanup(&nmreq
);
257 nfsm_chain_cleanup(&nmrep
);
262 nfs4_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
264 struct nfsmount
*nmp
;
265 int error
= 0, lockerror
= ENOENT
, status
, numops
;
268 struct nfsm_chain nmreq
, nmrep
;
269 struct nfsreq_secinfo_args si
;
272 if (nfs_mount_gone(nmp
))
274 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
276 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
277 nfsm_chain_null(&nmreq
);
278 nfsm_chain_null(&nmrep
);
280 // PUTFH, GETATTR, READLINK
282 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
283 nfsm_chain_add_compound_header(error
, &nmreq
, "readlink", nmp
->nm_minor_vers
, numops
);
285 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
286 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
288 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
289 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
291 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READLINK
);
292 nfsm_chain_build_done(error
, &nmreq
);
293 nfsm_assert(error
, (numops
== 0), EPROTO
);
295 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
297 if ((lockerror
= nfs_node_lock(np
)))
299 nfsm_chain_skip_tag(error
, &nmrep
);
300 nfsm_chain_get_32(error
, &nmrep
, numops
);
301 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
302 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
303 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
304 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READLINK
);
305 nfsm_chain_get_32(error
, &nmrep
, len
);
307 if (len
>= *buflenp
) {
308 if (np
->n_size
&& (np
->n_size
< *buflenp
))
313 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
319 nfsm_chain_cleanup(&nmreq
);
320 nfsm_chain_cleanup(&nmrep
);
331 struct nfsreq_cbinfo
*cb
,
332 struct nfsreq
**reqp
)
334 struct nfsmount
*nmp
;
335 int error
= 0, nfsvers
, numops
;
337 struct nfsm_chain nmreq
;
338 struct nfsreq_secinfo_args si
;
341 if (nfs_mount_gone(nmp
))
343 nfsvers
= nmp
->nm_vers
;
344 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
347 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
348 nfsm_chain_null(&nmreq
);
350 // PUTFH, READ, GETATTR
352 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
353 nfsm_chain_add_compound_header(error
, &nmreq
, "read", nmp
->nm_minor_vers
, numops
);
355 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
356 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
358 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
359 nfs_get_stateid(np
, thd
, cred
, &stateid
);
360 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
361 nfsm_chain_add_64(error
, &nmreq
, offset
);
362 nfsm_chain_add_32(error
, &nmreq
, len
);
364 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
365 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
366 nfsm_chain_build_done(error
, &nmreq
);
367 nfsm_assert(error
, (numops
== 0), EPROTO
);
369 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
371 nfsm_chain_cleanup(&nmreq
);
376 nfs4_read_rpc_async_finish(
383 struct nfsmount
*nmp
;
384 int error
= 0, lockerror
, nfsvers
, numops
, status
, eof
= 0;
387 struct nfsm_chain nmrep
;
390 if (nfs_mount_gone(nmp
)) {
391 nfs_request_async_cancel(req
);
394 nfsvers
= nmp
->nm_vers
;
396 nfsm_chain_null(&nmrep
);
398 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
399 if (error
== EINPROGRESS
) /* async request restarted */
402 if ((lockerror
= nfs_node_lock(np
)))
404 nfsm_chain_skip_tag(error
, &nmrep
);
405 nfsm_chain_get_32(error
, &nmrep
, numops
);
406 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
407 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
408 nfsm_chain_get_32(error
, &nmrep
, eof
);
409 nfsm_chain_get_32(error
, &nmrep
, retlen
);
411 *lenp
= MIN(retlen
, *lenp
);
412 error
= nfsm_chain_get_uio(&nmrep
, *lenp
, uio
);
414 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
415 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
423 nfsm_chain_cleanup(&nmrep
);
424 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
425 microuptime(&np
->n_lastio
);
430 nfs4_write_rpc_async(
437 struct nfsreq_cbinfo
*cb
,
438 struct nfsreq
**reqp
)
440 struct nfsmount
*nmp
;
442 int error
= 0, nfsvers
, numops
;
444 struct nfsm_chain nmreq
;
445 struct nfsreq_secinfo_args si
;
448 if (nfs_mount_gone(nmp
))
450 nfsvers
= nmp
->nm_vers
;
451 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
454 /* for async mounts, don't bother sending sync write requests */
455 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
456 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
457 iomode
= NFS_WRITE_UNSTABLE
;
459 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
460 nfsm_chain_null(&nmreq
);
462 // PUTFH, WRITE, GETATTR
464 nfsm_chain_build_alloc_init(error
, &nmreq
, 25 * NFSX_UNSIGNED
+ len
);
465 nfsm_chain_add_compound_header(error
, &nmreq
, "write", nmp
->nm_minor_vers
, numops
);
467 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
468 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
470 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_WRITE
);
471 nfs_get_stateid(np
, thd
, cred
, &stateid
);
472 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
473 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
474 nfsm_chain_add_32(error
, &nmreq
, iomode
);
475 nfsm_chain_add_32(error
, &nmreq
, len
);
477 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
479 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
480 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
481 nfsm_chain_build_done(error
, &nmreq
);
482 nfsm_assert(error
, (numops
== 0), EPROTO
);
485 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
487 nfsm_chain_cleanup(&nmreq
);
492 nfs4_write_rpc_async_finish(
499 struct nfsmount
*nmp
;
500 int error
= 0, lockerror
= ENOENT
, nfsvers
, numops
, status
;
501 int committed
= NFS_WRITE_FILESYNC
;
503 u_int64_t xid
, wverf
;
505 struct nfsm_chain nmrep
;
508 if (nfs_mount_gone(nmp
)) {
509 nfs_request_async_cancel(req
);
512 nfsvers
= nmp
->nm_vers
;
514 nfsm_chain_null(&nmrep
);
516 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
517 if (error
== EINPROGRESS
) /* async request restarted */
520 if (nfs_mount_gone(nmp
))
522 if (!error
&& (lockerror
= nfs_node_lock(np
)))
524 nfsm_chain_skip_tag(error
, &nmrep
);
525 nfsm_chain_get_32(error
, &nmrep
, numops
);
526 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
527 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_WRITE
);
528 nfsm_chain_get_32(error
, &nmrep
, rlen
);
533 nfsm_chain_get_32(error
, &nmrep
, committed
);
534 nfsm_chain_get_64(error
, &nmrep
, wverf
);
538 lck_mtx_lock(&nmp
->nm_lock
);
539 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
540 nmp
->nm_verf
= wverf
;
541 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
542 } else if (nmp
->nm_verf
!= wverf
) {
543 nmp
->nm_verf
= wverf
;
545 lck_mtx_unlock(&nmp
->nm_lock
);
546 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
547 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
551 nfsm_chain_cleanup(&nmrep
);
552 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
553 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
554 committed
= NFS_WRITE_FILESYNC
;
555 *iomodep
= committed
;
556 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
557 microuptime(&np
->n_lastio
);
569 int error
= 0, lockerror
= ENOENT
, remove_error
= 0, status
;
570 struct nfsmount
*nmp
;
573 struct nfsm_chain nmreq
, nmrep
;
574 struct nfsreq_secinfo_args si
;
577 if (nfs_mount_gone(nmp
))
579 nfsvers
= nmp
->nm_vers
;
580 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
582 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
584 nfsm_chain_null(&nmreq
);
585 nfsm_chain_null(&nmrep
);
587 // PUTFH, REMOVE, GETATTR
589 nfsm_chain_build_alloc_init(error
, &nmreq
, 17 * NFSX_UNSIGNED
+ namelen
);
590 nfsm_chain_add_compound_header(error
, &nmreq
, "remove", nmp
->nm_minor_vers
, numops
);
592 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
593 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
595 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_REMOVE
);
596 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
598 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
599 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
600 nfsm_chain_build_done(error
, &nmreq
);
601 nfsm_assert(error
, (numops
== 0), EPROTO
);
604 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, &nmrep
, &xid
, &status
);
606 if ((lockerror
= nfs_node_lock(dnp
)))
608 nfsm_chain_skip_tag(error
, &nmrep
);
609 nfsm_chain_get_32(error
, &nmrep
, numops
);
610 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
611 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_REMOVE
);
612 remove_error
= error
;
613 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
614 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
615 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
616 if (error
&& !lockerror
)
617 NATTRINVALIDATE(dnp
);
619 nfsm_chain_cleanup(&nmreq
);
620 nfsm_chain_cleanup(&nmrep
);
623 dnp
->n_flag
|= NMODIFIED
;
624 nfs_node_unlock(dnp
);
626 if (error
== NFSERR_GRACE
) {
627 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
631 return (remove_error
);
644 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
645 struct nfsmount
*nmp
;
646 u_int64_t xid
, savedxid
;
647 struct nfsm_chain nmreq
, nmrep
;
648 struct nfsreq_secinfo_args si
;
650 nmp
= NFSTONMP(fdnp
);
651 if (nfs_mount_gone(nmp
))
653 nfsvers
= nmp
->nm_vers
;
654 if (fdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
656 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
659 NFSREQ_SECINFO_SET(&si
, fdnp
, NULL
, 0, NULL
, 0);
660 nfsm_chain_null(&nmreq
);
661 nfsm_chain_null(&nmrep
);
663 // PUTFH(FROM), SAVEFH, PUTFH(TO), RENAME, GETATTR(TO), RESTOREFH, GETATTR(FROM)
665 nfsm_chain_build_alloc_init(error
, &nmreq
, 30 * NFSX_UNSIGNED
+ fnamelen
+ tnamelen
);
666 nfsm_chain_add_compound_header(error
, &nmreq
, "rename", nmp
->nm_minor_vers
, numops
);
668 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
669 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
671 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
673 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
674 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
676 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RENAME
);
677 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
678 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
680 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
681 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
683 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
685 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
686 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, fdnp
);
687 nfsm_chain_build_done(error
, &nmreq
);
688 nfsm_assert(error
, (numops
== 0), EPROTO
);
691 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
693 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
695 nfsm_chain_skip_tag(error
, &nmrep
);
696 nfsm_chain_get_32(error
, &nmrep
, numops
);
697 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
698 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
699 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
700 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RENAME
);
701 nfsm_chain_check_change_info(error
, &nmrep
, fdnp
);
702 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
703 /* directory attributes: if we don't get them, make sure to invalidate */
704 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
706 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
707 if (error
&& !lockerror
)
708 NATTRINVALIDATE(tdnp
);
709 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
710 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
712 nfsm_chain_loadattr(error
, &nmrep
, fdnp
, nfsvers
, &xid
);
713 if (error
&& !lockerror
)
714 NATTRINVALIDATE(fdnp
);
716 nfsm_chain_cleanup(&nmreq
);
717 nfsm_chain_cleanup(&nmrep
);
719 fdnp
->n_flag
|= NMODIFIED
;
720 tdnp
->n_flag
|= NMODIFIED
;
721 nfs_node_unlock2(fdnp
, tdnp
);
727 * NFS V4 readdir RPC.
730 nfs4_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
732 struct nfsmount
*nmp
;
733 int error
= 0, lockerror
, nfsvers
, namedattr
, rdirplus
, bigcookies
, numops
;
734 int i
, status
, more_entries
= 1, eof
, bp_dropped
= 0;
735 uint32_t nmreaddirsize
, nmrsize
;
736 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
737 uint64_t cookie
, lastcookie
, xid
, savedxid
;
738 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
740 struct nfs_vattr nvattr
, *nvattrp
;
741 struct nfs_dir_buf_header
*ndbhp
;
743 char *padstart
, padlen
;
745 uint32_t entry_attrs
[NFS_ATTR_BITMAP_LEN
];
747 struct nfsreq_secinfo_args si
;
750 if (nfs_mount_gone(nmp
))
752 nfsvers
= nmp
->nm_vers
;
753 nmreaddirsize
= nmp
->nm_readdirsize
;
754 nmrsize
= nmp
->nm_rsize
;
755 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
756 namedattr
= (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) ? 1 : 0;
757 rdirplus
= (NMFLAG(nmp
, RDIRPLUS
) || namedattr
) ? 1 : 0;
758 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
760 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
763 * Set up attribute request for entries.
764 * For READDIRPLUS functionality, get everything.
765 * Otherwise, just get what we need for struct direntry.
769 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, entry_attrs
);
770 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEHANDLE
);
773 NFS_CLEAR_ATTRIBUTES(entry_attrs
);
774 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_TYPE
);
775 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEID
);
776 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_MOUNTED_ON_FILEID
);
778 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_RDATTR_ERROR
);
780 /* lock to protect access to cookie verifier */
781 if ((lockerror
= nfs_node_lock(dnp
)))
784 /* determine cookie to use, and move dp to the right offset */
785 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
786 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
787 if (ndbhp
->ndbh_count
) {
788 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
789 dp
= NFS_DIRENTRY_NEXT(dp
);
790 cookie
= dp
->d_seekoff
;
791 dp
= NFS_DIRENTRY_NEXT(dp
);
793 cookie
= bp
->nb_lblkno
;
794 /* increment with every buffer read */
795 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
800 * The NFS client is responsible for the "." and ".." entries in the
801 * directory. So, we put them at the start of the first buffer.
802 * Don't bother for attribute directories.
804 if (((bp
->nb_lblkno
== 0) && (ndbhp
->ndbh_count
== 0)) &&
805 !(dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)) {
807 fhlen
= rdirplus
? fh
.fh_len
+ 1 : 0;
808 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
811 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
813 bzero(&dp
->d_name
[namlen
+1], xlen
);
814 dp
->d_namlen
= namlen
;
815 strlcpy(dp
->d_name
, ".", namlen
+1);
816 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
818 dp
->d_reclen
= reclen
;
820 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
821 dp
= NFS_DIRENTRY_NEXT(dp
);
822 padlen
= (char*)dp
- padstart
;
824 bzero(padstart
, padlen
);
825 if (rdirplus
) /* zero out attributes */
826 bzero(NFS_DIR_BUF_NVATTR(bp
, 0), sizeof(struct nfs_vattr
));
830 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
832 bzero(&dp
->d_name
[namlen
+1], xlen
);
833 dp
->d_namlen
= namlen
;
834 strlcpy(dp
->d_name
, "..", namlen
+1);
836 dp
->d_fileno
= VTONFS(dnp
->n_parent
)->n_vattr
.nva_fileid
;
838 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
840 dp
->d_reclen
= reclen
;
842 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
843 dp
= NFS_DIRENTRY_NEXT(dp
);
844 padlen
= (char*)dp
- padstart
;
846 bzero(padstart
, padlen
);
847 if (rdirplus
) /* zero out attributes */
848 bzero(NFS_DIR_BUF_NVATTR(bp
, 1), sizeof(struct nfs_vattr
));
850 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
851 ndbhp
->ndbh_count
= 2;
855 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
856 * the buffer is full (or we hit EOF). Then put the remainder of the
857 * results in the next buffer(s).
859 nfsm_chain_null(&nmreq
);
860 nfsm_chain_null(&nmrep
);
861 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
863 // PUTFH, GETATTR, READDIR
865 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
866 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, nmp
->nm_minor_vers
, numops
);
868 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
869 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
871 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
872 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
874 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READDIR
);
875 nfsm_chain_add_64(error
, &nmreq
, (cookie
<= 2) ? 0 : cookie
);
876 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
877 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
878 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
879 nfsm_chain_add_bitmap_supported(error
, &nmreq
, entry_attrs
, nmp
, dnp
);
880 nfsm_chain_build_done(error
, &nmreq
);
881 nfsm_assert(error
, (numops
== 0), EPROTO
);
882 nfs_node_unlock(dnp
);
884 error
= nfs_request(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
886 if ((lockerror
= nfs_node_lock(dnp
)))
890 nfsm_chain_skip_tag(error
, &nmrep
);
891 nfsm_chain_get_32(error
, &nmrep
, numops
);
892 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
893 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
894 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
895 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READDIR
);
896 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
897 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
900 nfs_node_unlock(dnp
);
908 /* loop through the entries packing them into the buffer */
909 while (more_entries
) {
910 /* Entry: COOKIE, NAME, FATTR */
911 nfsm_chain_get_64(error
, &nmrep
, cookie
);
912 nfsm_chain_get_32(error
, &nmrep
, namlen
);
914 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
915 /* we've got a big cookie, make sure flag is set */
916 lck_mtx_lock(&nmp
->nm_lock
);
917 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
918 lck_mtx_unlock(&nmp
->nm_lock
);
921 /* just truncate names that don't fit in direntry.d_name */
926 if (namlen
> (sizeof(dp
->d_name
)-1)) {
927 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
928 namlen
= sizeof(dp
->d_name
) - 1;
932 /* guess that fh size will be same as parent */
933 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
934 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
935 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
936 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
937 space_needed
= reclen
+ attrlen
;
938 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
939 if (space_needed
> space_free
) {
941 * We still have entries to pack, but we've
942 * run out of room in the current buffer.
943 * So we need to move to the next buffer.
944 * The block# for the next buffer is the
945 * last cookie in the current buffer.
948 ndbhp
->ndbh_flags
|= NDB_FULL
;
949 nfs_buf_release(bp
, 0);
952 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
954 /* initialize buffer */
955 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
956 ndbhp
->ndbh_flags
= 0;
957 ndbhp
->ndbh_count
= 0;
958 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
959 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
960 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
961 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
962 /* increment with every buffer read */
963 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
966 dp
->d_fileno
= cookie
; /* placeholder */
967 dp
->d_seekoff
= cookie
;
968 dp
->d_namlen
= namlen
;
969 dp
->d_reclen
= reclen
;
970 dp
->d_type
= DT_UNKNOWN
;
971 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
973 dp
->d_name
[namlen
] = '\0';
975 nfsm_chain_adv(error
, &nmrep
,
976 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
978 nvattrp
= rdirplus
? NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
) : &nvattr
;
979 error
= nfs4_parsefattr(&nmrep
, NULL
, nvattrp
, &fh
, NULL
, NULL
);
980 if (!error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
)) {
981 /* we do NOT want ACLs returned to us here */
982 NFS_BITMAP_CLR(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
);
983 if (nvattrp
->nva_acl
) {
984 kauth_acl_free(nvattrp
->nva_acl
);
985 nvattrp
->nva_acl
= NULL
;
988 if (error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_RDATTR_ERROR
)) {
989 /* OK, we may not have gotten all of the attributes but we will use what we can. */
990 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
991 /* set this up to look like a referral trigger */
992 nfs4_default_attrs_for_referral_trigger(dnp
, dp
->d_name
, namlen
, nvattrp
, &fh
);
996 /* check for more entries after this one */
997 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
1000 /* Skip any "." and ".." entries returned from server. */
1001 /* Also skip any bothersome named attribute entries. */
1002 if (((dp
->d_name
[0] == '.') && ((namlen
== 1) || ((namlen
== 2) && (dp
->d_name
[1] == '.')))) ||
1003 (namedattr
&& (namlen
== 11) && (!strcmp(dp
->d_name
, "SUNWattr_ro") || !strcmp(dp
->d_name
, "SUNWattr_rw")))) {
1004 lastcookie
= cookie
;
1008 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_TYPE
))
1009 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
1010 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEID
))
1011 dp
->d_fileno
= nvattrp
->nva_fileid
;
1013 /* fileid is already in d_fileno, so stash xid in attrs */
1014 nvattrp
->nva_fileid
= savedxid
;
1015 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
1016 fhlen
= fh
.fh_len
+ 1;
1017 xlen
= fhlen
+ sizeof(time_t);
1018 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1019 space_needed
= reclen
+ attrlen
;
1020 if (space_needed
> space_free
) {
1021 /* didn't actually have the room... move on to next buffer */
1025 /* pack the file handle into the record */
1026 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
1027 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
1029 /* mark the file handle invalid */
1031 fhlen
= fh
.fh_len
+ 1;
1032 xlen
= fhlen
+ sizeof(time_t);
1033 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1034 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
1036 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
1037 dp
->d_reclen
= reclen
;
1039 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
1040 ndbhp
->ndbh_count
++;
1041 lastcookie
= cookie
;
1043 /* advance to next direntry in buffer */
1044 dp
= NFS_DIRENTRY_NEXT(dp
);
1045 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
1046 /* zero out the pad bytes */
1047 padlen
= (char*)dp
- padstart
;
1049 bzero(padstart
, padlen
);
1051 /* Finally, get the eof boolean */
1052 nfsm_chain_get_32(error
, &nmrep
, eof
);
1055 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
1056 nfs_node_lock_force(dnp
);
1057 dnp
->n_eofcookie
= lastcookie
;
1058 nfs_node_unlock(dnp
);
1063 nfs_buf_release(bp
, 0);
1067 if ((lockerror
= nfs_node_lock(dnp
)))
1070 nfsm_chain_cleanup(&nmrep
);
1071 nfsm_chain_null(&nmreq
);
1074 if (bp_dropped
&& bp
)
1075 nfs_buf_release(bp
, 0);
1077 nfs_node_unlock(dnp
);
1078 nfsm_chain_cleanup(&nmreq
);
1079 nfsm_chain_cleanup(&nmrep
);
1080 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
1084 nfs4_lookup_rpc_async(
1089 struct nfsreq
**reqp
)
1091 int error
= 0, isdotdot
= 0, nfsvers
, numops
;
1092 struct nfsm_chain nmreq
;
1093 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1094 struct nfsmount
*nmp
;
1095 struct nfsreq_secinfo_args si
;
1097 nmp
= NFSTONMP(dnp
);
1098 if (nfs_mount_gone(nmp
))
1100 nfsvers
= nmp
->nm_vers
;
1101 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1104 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2)) {
1106 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
1108 NFSREQ_SECINFO_SET(&si
, dnp
, dnp
->n_fhp
, dnp
->n_fhsize
, name
, namelen
);
1111 nfsm_chain_null(&nmreq
);
1113 // PUTFH, GETATTR, LOOKUP(P), GETFH, GETATTR (FH)
1115 nfsm_chain_build_alloc_init(error
, &nmreq
, 20 * NFSX_UNSIGNED
+ namelen
);
1116 nfsm_chain_add_compound_header(error
, &nmreq
, "lookup", nmp
->nm_minor_vers
, numops
);
1118 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1119 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
1121 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1122 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
1125 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUPP
);
1127 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
1128 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
1131 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETFH
);
1133 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1134 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1135 /* some ".zfs" directories can't handle being asked for some attributes */
1136 if ((dnp
->n_flag
& NISDOTZFS
) && !isdotdot
)
1137 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1138 if ((dnp
->n_flag
& NISDOTZFSCHILD
) && isdotdot
)
1139 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1140 if (((namelen
== 4) && (name
[0] == '.') && (name
[1] == 'z') && (name
[2] == 'f') && (name
[3] == 's')))
1141 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1142 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
1143 nfsm_chain_build_done(error
, &nmreq
);
1144 nfsm_assert(error
, (numops
== 0), EPROTO
);
1146 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1147 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, reqp
);
1149 nfsm_chain_cleanup(&nmreq
);
1155 nfs4_lookup_rpc_async_finish(
1163 struct nfs_vattr
*nvap
)
1165 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
, isdotdot
= 0;
1166 uint32_t op
= NFS_OP_LOOKUP
;
1168 struct nfsmount
*nmp
;
1169 struct nfsm_chain nmrep
;
1171 nmp
= NFSTONMP(dnp
);
1174 nfsvers
= nmp
->nm_vers
;
1175 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2))
1178 nfsm_chain_null(&nmrep
);
1180 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
1182 if ((lockerror
= nfs_node_lock(dnp
)))
1184 nfsm_chain_skip_tag(error
, &nmrep
);
1185 nfsm_chain_get_32(error
, &nmrep
, numops
);
1186 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1187 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1190 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
1192 nfsm_chain_op_check(error
, &nmrep
, (isdotdot
? NFS_OP_LOOKUPP
: NFS_OP_LOOKUP
));
1193 nfsmout_if(error
|| !fhp
|| !nvap
);
1194 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETFH
);
1195 nfsm_chain_get_32(error
, &nmrep
, fhp
->fh_len
);
1196 nfsm_chain_get_opaque(error
, &nmrep
, fhp
->fh_len
, fhp
->fh_data
);
1197 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1198 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
1199 /* set this up to look like a referral trigger */
1200 nfs4_default_attrs_for_referral_trigger(dnp
, name
, namelen
, nvap
, fhp
);
1204 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
1208 nfs_node_unlock(dnp
);
1209 nfsm_chain_cleanup(&nmrep
);
1210 if (!error
&& (op
== NFS_OP_LOOKUP
) && (nmp
->nm_state
& NFSSTA_NEEDSECINFO
)) {
1211 /* We still need to get SECINFO to set default for mount. */
1212 /* Do so for the first LOOKUP that returns successfully. */
1215 sec
.count
= NX_MAX_SEC_FLAVORS
;
1216 error
= nfs4_secinfo_rpc(nmp
, &req
->r_secinfo
, vfs_context_ucred(ctx
), sec
.flavors
, &sec
.count
);
1217 /* [sigh] some implementations return "illegal" error for unsupported ops */
1218 if (error
== NFSERR_OP_ILLEGAL
)
1221 /* set our default security flavor to the first in the list */
1222 lck_mtx_lock(&nmp
->nm_lock
);
1224 nmp
->nm_auth
= sec
.flavors
[0];
1225 nmp
->nm_state
&= ~NFSSTA_NEEDSECINFO
;
1226 lck_mtx_unlock(&nmp
->nm_lock
);
1240 struct nfsmount
*nmp
;
1241 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1242 u_int64_t xid
, newwverf
;
1244 struct nfsm_chain nmreq
, nmrep
;
1245 struct nfsreq_secinfo_args si
;
1248 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
1249 if (nfs_mount_gone(nmp
))
1251 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1253 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
1255 nfsvers
= nmp
->nm_vers
;
1257 if (count
> UINT32_MAX
)
1262 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1263 nfsm_chain_null(&nmreq
);
1264 nfsm_chain_null(&nmrep
);
1266 // PUTFH, COMMIT, GETATTR
1268 nfsm_chain_build_alloc_init(error
, &nmreq
, 19 * NFSX_UNSIGNED
);
1269 nfsm_chain_add_compound_header(error
, &nmreq
, "commit", nmp
->nm_minor_vers
, numops
);
1271 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1272 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1274 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_COMMIT
);
1275 nfsm_chain_add_64(error
, &nmreq
, offset
);
1276 nfsm_chain_add_32(error
, &nmreq
, count32
);
1278 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1279 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
1280 nfsm_chain_build_done(error
, &nmreq
);
1281 nfsm_assert(error
, (numops
== 0), EPROTO
);
1283 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1284 current_thread(), cred
, &si
, 0, &nmrep
, &xid
, &status
);
1286 if ((lockerror
= nfs_node_lock(np
)))
1288 nfsm_chain_skip_tag(error
, &nmrep
);
1289 nfsm_chain_get_32(error
, &nmrep
, numops
);
1290 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1291 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_COMMIT
);
1292 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
1293 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1294 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1296 nfs_node_unlock(np
);
1298 lck_mtx_lock(&nmp
->nm_lock
);
1299 if (nmp
->nm_verf
!= newwverf
)
1300 nmp
->nm_verf
= newwverf
;
1301 if (wverf
!= newwverf
)
1302 error
= NFSERR_STALEWRITEVERF
;
1303 lck_mtx_unlock(&nmp
->nm_lock
);
1305 nfsm_chain_cleanup(&nmreq
);
1306 nfsm_chain_cleanup(&nmrep
);
1313 struct nfs_fsattr
*nfsap
,
1317 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1318 struct nfsm_chain nmreq
, nmrep
;
1319 struct nfsmount
*nmp
= NFSTONMP(np
);
1320 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1321 struct nfs_vattr nvattr
;
1322 struct nfsreq_secinfo_args si
;
1324 if (nfs_mount_gone(nmp
))
1326 nfsvers
= nmp
->nm_vers
;
1327 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1330 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1331 NVATTR_INIT(&nvattr
);
1332 nfsm_chain_null(&nmreq
);
1333 nfsm_chain_null(&nmrep
);
1335 /* NFSv4: fetch "pathconf" info for this node */
1338 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
1339 nfsm_chain_add_compound_header(error
, &nmreq
, "pathconf", nmp
->nm_minor_vers
, numops
);
1341 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1342 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1344 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1345 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1346 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXLINK
);
1347 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXNAME
);
1348 NFS_BITMAP_SET(bitmap
, NFS_FATTR_NO_TRUNC
);
1349 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
1350 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
1351 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_PRESERVING
);
1352 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
1353 nfsm_chain_build_done(error
, &nmreq
);
1354 nfsm_assert(error
, (numops
== 0), EPROTO
);
1356 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1358 nfsm_chain_skip_tag(error
, &nmrep
);
1359 nfsm_chain_get_32(error
, &nmrep
, numops
);
1360 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1361 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1363 error
= nfs4_parsefattr(&nmrep
, nfsap
, &nvattr
, NULL
, NULL
, NULL
);
1365 if ((lockerror
= nfs_node_lock(np
)))
1368 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
1370 nfs_node_unlock(np
);
1372 NVATTR_CLEANUP(&nvattr
);
1373 nfsm_chain_cleanup(&nmreq
);
1374 nfsm_chain_cleanup(&nmrep
);
1380 struct vnop_getattr_args
/* {
1381 struct vnodeop_desc *a_desc;
1383 struct vnode_attr *a_vap;
1384 vfs_context_t a_context;
1387 struct vnode_attr
*vap
= ap
->a_vap
;
1388 struct nfsmount
*nmp
;
1389 struct nfs_vattr nva
;
1390 int error
, acls
, ngaflags
;
1392 nmp
= VTONMP(ap
->a_vp
);
1393 if (nfs_mount_gone(nmp
))
1395 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
1397 ngaflags
= NGA_CACHED
;
1398 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
)
1399 ngaflags
|= NGA_ACL
;
1400 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, ngaflags
);
1404 /* copy what we have in nva to *a_vap */
1405 if (VATTR_IS_ACTIVE(vap
, va_rdev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_RAWDEV
)) {
1406 dev_t rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1407 VATTR_RETURN(vap
, va_rdev
, rdev
);
1409 if (VATTR_IS_ACTIVE(vap
, va_nlink
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_NUMLINKS
))
1410 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1411 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SIZE
))
1412 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1413 // VATTR_RETURN(vap, va_data_alloc, ???);
1414 // VATTR_RETURN(vap, va_total_size, ???);
1415 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SPACE_USED
))
1416 VATTR_RETURN(vap
, va_total_alloc
, nva
.nva_bytes
);
1417 if (VATTR_IS_ACTIVE(vap
, va_uid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1418 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1419 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1420 VATTR_RETURN(vap
, va_uuuid
, nva
.nva_uuuid
);
1421 if (VATTR_IS_ACTIVE(vap
, va_gid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1422 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1423 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1424 VATTR_RETURN(vap
, va_guuid
, nva
.nva_guuid
);
1425 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1426 if (NMFLAG(nmp
, ACLONLY
) || !NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_MODE
))
1427 VATTR_RETURN(vap
, va_mode
, 0777);
1429 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1431 if (VATTR_IS_ACTIVE(vap
, va_flags
) &&
1432 (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) ||
1433 NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) ||
1434 (nva
.nva_flags
& NFS_FFLAG_TRIGGER
))) {
1436 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) &&
1437 (nva
.nva_flags
& NFS_FFLAG_ARCHIVED
))
1438 flags
|= SF_ARCHIVED
;
1439 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) &&
1440 (nva
.nva_flags
& NFS_FFLAG_HIDDEN
))
1442 VATTR_RETURN(vap
, va_flags
, flags
);
1444 if (VATTR_IS_ACTIVE(vap
, va_create_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_CREATE
)) {
1445 vap
->va_create_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CREATE
];
1446 vap
->va_create_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CREATE
];
1447 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1449 if (VATTR_IS_ACTIVE(vap
, va_access_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_ACCESS
)) {
1450 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1451 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1452 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1454 if (VATTR_IS_ACTIVE(vap
, va_modify_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_MODIFY
)) {
1455 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1456 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1457 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1459 if (VATTR_IS_ACTIVE(vap
, va_change_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_METADATA
)) {
1460 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1461 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1462 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1464 if (VATTR_IS_ACTIVE(vap
, va_backup_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_BACKUP
)) {
1465 vap
->va_backup_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_BACKUP
];
1466 vap
->va_backup_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_BACKUP
];
1467 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1469 if (VATTR_IS_ACTIVE(vap
, va_fileid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_FILEID
))
1470 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1471 if (VATTR_IS_ACTIVE(vap
, va_type
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TYPE
))
1472 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1473 if (VATTR_IS_ACTIVE(vap
, va_filerev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_CHANGE
))
1474 VATTR_RETURN(vap
, va_filerev
, nva
.nva_change
);
1476 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
) {
1477 VATTR_RETURN(vap
, va_acl
, nva
.nva_acl
);
1481 // other attrs we might support someday:
1482 // VATTR_RETURN(vap, va_encoding, ??? /* potentially unnormalized UTF-8? */);
1484 NVATTR_CLEANUP(&nva
);
1491 struct vnode_attr
*vap
,
1494 struct nfsmount
*nmp
= NFSTONMP(np
);
1495 int error
= 0, setattr_error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
1496 u_int64_t xid
, nextxid
;
1497 struct nfsm_chain nmreq
, nmrep
;
1498 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
1499 uint32_t getbitmap
[NFS_ATTR_BITMAP_LEN
];
1500 uint32_t setbitmap
[NFS_ATTR_BITMAP_LEN
];
1501 nfs_stateid stateid
;
1502 struct nfsreq_secinfo_args si
;
1504 if (nfs_mount_gone(nmp
))
1506 nfsvers
= nmp
->nm_vers
;
1507 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1510 if (VATTR_IS_ACTIVE(vap
, va_flags
) && (vap
->va_flags
& ~(SF_ARCHIVED
|UF_HIDDEN
))) {
1511 /* we don't support setting unsupported flags (duh!) */
1512 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
1513 return (EINVAL
); /* return EINVAL if other attributes also set */
1515 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
1518 /* don't bother requesting some changes if they don't look like they are changing */
1519 if (VATTR_IS_ACTIVE(vap
, va_uid
) && (vap
->va_uid
== np
->n_vattr
.nva_uid
))
1520 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1521 if (VATTR_IS_ACTIVE(vap
, va_gid
) && (vap
->va_gid
== np
->n_vattr
.nva_gid
))
1522 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1523 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &np
->n_vattr
.nva_uuuid
))
1524 VATTR_CLEAR_ACTIVE(vap
, va_uuuid
);
1525 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &np
->n_vattr
.nva_guuid
))
1526 VATTR_CLEAR_ACTIVE(vap
, va_guuid
);
1529 /* do nothing if no attributes will be sent */
1530 nfs_vattr_set_bitmap(nmp
, bitmap
, vap
);
1531 if (!bitmap
[0] && !bitmap
[1])
1534 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1535 nfsm_chain_null(&nmreq
);
1536 nfsm_chain_null(&nmrep
);
1539 * Prepare GETATTR bitmap: if we are setting the ACL or mode, we
1540 * need to invalidate any cached ACL. And if we had an ACL cached,
1541 * we might as well also fetch the new value.
1543 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, getbitmap
);
1544 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
) ||
1545 NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
1547 NFS_BITMAP_SET(getbitmap
, NFS_FATTR_ACL
);
1551 // PUTFH, SETATTR, GETATTR
1553 nfsm_chain_build_alloc_init(error
, &nmreq
, 40 * NFSX_UNSIGNED
);
1554 nfsm_chain_add_compound_header(error
, &nmreq
, "setattr", nmp
->nm_minor_vers
, numops
);
1556 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1557 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1559 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SETATTR
);
1560 if (VATTR_IS_ACTIVE(vap
, va_data_size
))
1561 nfs_get_stateid(np
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &stateid
);
1563 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
1564 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
1565 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
1567 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1568 nfsm_chain_add_bitmap_supported(error
, &nmreq
, getbitmap
, nmp
, np
);
1569 nfsm_chain_build_done(error
, &nmreq
);
1570 nfsm_assert(error
, (numops
== 0), EPROTO
);
1572 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1574 if ((lockerror
= nfs_node_lock(np
)))
1576 nfsm_chain_skip_tag(error
, &nmrep
);
1577 nfsm_chain_get_32(error
, &nmrep
, numops
);
1578 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1580 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SETATTR
);
1581 nfsmout_if(error
== EBADRPC
);
1582 setattr_error
= error
;
1584 bmlen
= NFS_ATTR_BITMAP_LEN
;
1585 nfsm_chain_get_bitmap(error
, &nmrep
, setbitmap
, bmlen
);
1587 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
1588 microuptime(&np
->n_lastio
);
1589 nfs_vattr_set_supported(setbitmap
, vap
);
1590 error
= setattr_error
;
1592 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1593 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1595 NATTRINVALIDATE(np
);
1597 * We just changed the attributes and we want to make sure that we
1598 * see the latest attributes. Get the next XID. If it's not the
1599 * next XID after the SETATTR XID, then it's possible that another
1600 * RPC was in flight at the same time and it might put stale attributes
1601 * in the cache. In that case, we invalidate the attributes and set
1602 * the attribute cache XID to guarantee that newer attributes will
1606 nfs_get_xid(&nextxid
);
1607 if (nextxid
!= (xid
+ 1)) {
1608 np
->n_xid
= nextxid
;
1609 NATTRINVALIDATE(np
);
1613 nfs_node_unlock(np
);
1614 nfsm_chain_cleanup(&nmreq
);
1615 nfsm_chain_cleanup(&nmrep
);
1616 if ((setattr_error
== EINVAL
) && VATTR_IS_ACTIVE(vap
, va_acl
) && VATTR_IS_ACTIVE(vap
, va_mode
) && !NMFLAG(nmp
, ACLONLY
)) {
1618 * Some server's may not like ACL/mode combos that get sent.
1619 * If it looks like that's what the server choked on, try setting
1620 * just the ACL and not the mode (unless it looks like everything
1621 * but mode was already successfully set).
1623 if (((bitmap
[0] & setbitmap
[0]) != bitmap
[0]) ||
1624 ((bitmap
[1] & (setbitmap
[1]|NFS_FATTR_MODE
)) != bitmap
[1])) {
1625 VATTR_CLEAR_ACTIVE(vap
, va_mode
);
1634 * Wait for any pending recovery to complete.
1637 nfs_mount_state_wait_for_recovery(struct nfsmount
*nmp
)
1639 struct timespec ts
= { 1, 0 };
1640 int error
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
1642 lck_mtx_lock(&nmp
->nm_lock
);
1643 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1644 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 1)))
1646 nfs_mount_sock_thread_wake(nmp
);
1647 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1650 lck_mtx_unlock(&nmp
->nm_lock
);
1656 * We're about to use/manipulate NFS mount's open/lock state.
1657 * Wait for any pending state recovery to complete, then
1658 * mark the state as being in use (which will hold off
1659 * the recovery thread until we're done).
1662 nfs_mount_state_in_use_start(struct nfsmount
*nmp
, thread_t thd
)
1664 struct timespec ts
= { 1, 0 };
1665 int error
= 0, slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1667 if (nfs_mount_gone(nmp
))
1669 lck_mtx_lock(&nmp
->nm_lock
);
1670 if (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) {
1671 lck_mtx_unlock(&nmp
->nm_lock
);
1674 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1675 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 1)))
1677 nfs_mount_sock_thread_wake(nmp
);
1678 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1682 nmp
->nm_stateinuse
++;
1683 lck_mtx_unlock(&nmp
->nm_lock
);
1689 * We're done using/manipulating the NFS mount's open/lock
1690 * state. If the given error indicates that recovery should
1691 * be performed, we'll initiate recovery.
1694 nfs_mount_state_in_use_end(struct nfsmount
*nmp
, int error
)
1696 int restart
= nfs_mount_state_error_should_restart(error
);
1698 if (nfs_mount_gone(nmp
))
1700 lck_mtx_lock(&nmp
->nm_lock
);
1701 if (restart
&& (error
!= NFSERR_OLD_STATEID
) && (error
!= NFSERR_GRACE
)) {
1702 printf("nfs_mount_state_in_use_end: error %d, initiating recovery for %s, 0x%x\n",
1703 error
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
1704 nfs_need_recover(nmp
, error
);
1706 if (nmp
->nm_stateinuse
> 0)
1707 nmp
->nm_stateinuse
--;
1709 panic("NFS mount state in use count underrun");
1710 if (!nmp
->nm_stateinuse
&& (nmp
->nm_state
& NFSSTA_RECOVER
))
1711 wakeup(&nmp
->nm_stateinuse
);
1712 lck_mtx_unlock(&nmp
->nm_lock
);
1713 if (error
== NFSERR_GRACE
)
1714 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
1720 * Does the error mean we should restart/redo a state-related operation?
1723 nfs_mount_state_error_should_restart(int error
)
1726 case NFSERR_STALE_STATEID
:
1727 case NFSERR_STALE_CLIENTID
:
1728 case NFSERR_ADMIN_REVOKED
:
1729 case NFSERR_EXPIRED
:
1730 case NFSERR_OLD_STATEID
:
1731 case NFSERR_BAD_STATEID
:
1739 * In some cases we may want to limit how many times we restart a
1740 * state-related operation - e.g. we're repeatedly getting NFSERR_GRACE.
1741 * Base the limit on the lease (as long as it's not too short).
1744 nfs_mount_state_max_restarts(struct nfsmount
*nmp
)
1746 return (MAX(nmp
->nm_fsattr
.nfsa_lease
, 60));
1750 * Does the error mean we probably lost a delegation?
1753 nfs_mount_state_error_delegation_lost(int error
)
1756 case NFSERR_STALE_STATEID
:
1757 case NFSERR_ADMIN_REVOKED
:
1758 case NFSERR_EXPIRED
:
1759 case NFSERR_OLD_STATEID
:
1760 case NFSERR_BAD_STATEID
:
1761 case NFSERR_GRACE
: /* ugh! (stupid) RFC 3530 specifically disallows CLAIM_DELEGATE_CUR during grace period? */
1769 * Mark an NFS node's open state as busy.
1772 nfs_open_state_set_busy(nfsnode_t np
, thread_t thd
)
1774 struct nfsmount
*nmp
;
1775 struct timespec ts
= {2, 0};
1776 int error
= 0, slpflag
;
1779 if (nfs_mount_gone(nmp
))
1781 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1783 lck_mtx_lock(&np
->n_openlock
);
1784 while (np
->n_openflags
& N_OPENBUSY
) {
1785 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1787 np
->n_openflags
|= N_OPENWANT
;
1788 msleep(&np
->n_openflags
, &np
->n_openlock
, slpflag
, "nfs_open_state_set_busy", &ts
);
1792 np
->n_openflags
|= N_OPENBUSY
;
1793 lck_mtx_unlock(&np
->n_openlock
);
1799 * Clear an NFS node's open state busy flag and wake up
1800 * anyone wanting it.
1803 nfs_open_state_clear_busy(nfsnode_t np
)
1807 lck_mtx_lock(&np
->n_openlock
);
1808 if (!(np
->n_openflags
& N_OPENBUSY
))
1809 panic("nfs_open_state_clear_busy");
1810 wanted
= (np
->n_openflags
& N_OPENWANT
);
1811 np
->n_openflags
&= ~(N_OPENBUSY
|N_OPENWANT
);
1812 lck_mtx_unlock(&np
->n_openlock
);
1814 wakeup(&np
->n_openflags
);
1818 * Search a mount's open owner list for the owner for this credential.
1819 * If not found and "alloc" is set, then allocate a new one.
1821 struct nfs_open_owner
*
1822 nfs_open_owner_find(struct nfsmount
*nmp
, kauth_cred_t cred
, int alloc
)
1824 uid_t uid
= kauth_cred_getuid(cred
);
1825 struct nfs_open_owner
*noop
, *newnoop
= NULL
;
1828 lck_mtx_lock(&nmp
->nm_lock
);
1829 TAILQ_FOREACH(noop
, &nmp
->nm_open_owners
, noo_link
) {
1830 if (kauth_cred_getuid(noop
->noo_cred
) == uid
)
1834 if (!noop
&& !newnoop
&& alloc
) {
1835 lck_mtx_unlock(&nmp
->nm_lock
);
1836 MALLOC(newnoop
, struct nfs_open_owner
*, sizeof(struct nfs_open_owner
), M_TEMP
, M_WAITOK
);
1839 bzero(newnoop
, sizeof(*newnoop
));
1840 lck_mtx_init(&newnoop
->noo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
1841 newnoop
->noo_mount
= nmp
;
1842 kauth_cred_ref(cred
);
1843 newnoop
->noo_cred
= cred
;
1844 newnoop
->noo_name
= OSAddAtomic(1, &nfs_open_owner_seqnum
);
1845 TAILQ_INIT(&newnoop
->noo_opens
);
1848 if (!noop
&& newnoop
) {
1849 newnoop
->noo_flags
|= NFS_OPEN_OWNER_LINK
;
1850 TAILQ_INSERT_HEAD(&nmp
->nm_open_owners
, newnoop
, noo_link
);
1853 lck_mtx_unlock(&nmp
->nm_lock
);
1855 if (newnoop
&& (noop
!= newnoop
))
1856 nfs_open_owner_destroy(newnoop
);
1859 nfs_open_owner_ref(noop
);
1865 * destroy an open owner that's no longer needed
1868 nfs_open_owner_destroy(struct nfs_open_owner
*noop
)
1871 kauth_cred_unref(&noop
->noo_cred
);
1872 lck_mtx_destroy(&noop
->noo_lock
, nfs_open_grp
);
1877 * acquire a reference count on an open owner
1880 nfs_open_owner_ref(struct nfs_open_owner
*noop
)
1882 lck_mtx_lock(&noop
->noo_lock
);
1884 lck_mtx_unlock(&noop
->noo_lock
);
1888 * drop a reference count on an open owner and destroy it if
1889 * it is no longer referenced and no longer on the mount's list.
1892 nfs_open_owner_rele(struct nfs_open_owner
*noop
)
1894 lck_mtx_lock(&noop
->noo_lock
);
1895 if (noop
->noo_refcnt
< 1)
1896 panic("nfs_open_owner_rele: no refcnt");
1898 if (!noop
->noo_refcnt
&& (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1899 panic("nfs_open_owner_rele: busy");
1900 /* XXX we may potentially want to clean up idle/unused open owner structures */
1901 if (noop
->noo_refcnt
|| (noop
->noo_flags
& NFS_OPEN_OWNER_LINK
)) {
1902 lck_mtx_unlock(&noop
->noo_lock
);
1905 /* owner is no longer referenced or linked to mount, so destroy it */
1906 lck_mtx_unlock(&noop
->noo_lock
);
1907 nfs_open_owner_destroy(noop
);
1911 * Mark an open owner as busy because we are about to
1912 * start an operation that uses and updates open owner state.
1915 nfs_open_owner_set_busy(struct nfs_open_owner
*noop
, thread_t thd
)
1917 struct nfsmount
*nmp
;
1918 struct timespec ts
= {2, 0};
1919 int error
= 0, slpflag
;
1921 nmp
= noop
->noo_mount
;
1922 if (nfs_mount_gone(nmp
))
1924 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1926 lck_mtx_lock(&noop
->noo_lock
);
1927 while (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
) {
1928 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1930 noop
->noo_flags
|= NFS_OPEN_OWNER_WANT
;
1931 msleep(noop
, &noop
->noo_lock
, slpflag
, "nfs_open_owner_set_busy", &ts
);
1935 noop
->noo_flags
|= NFS_OPEN_OWNER_BUSY
;
1936 lck_mtx_unlock(&noop
->noo_lock
);
1942 * Clear the busy flag on an open owner and wake up anyone waiting
1946 nfs_open_owner_clear_busy(struct nfs_open_owner
*noop
)
1950 lck_mtx_lock(&noop
->noo_lock
);
1951 if (!(noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1952 panic("nfs_open_owner_clear_busy");
1953 wanted
= (noop
->noo_flags
& NFS_OPEN_OWNER_WANT
);
1954 noop
->noo_flags
&= ~(NFS_OPEN_OWNER_BUSY
|NFS_OPEN_OWNER_WANT
);
1955 lck_mtx_unlock(&noop
->noo_lock
);
1961 * Given an open/lock owner and an error code, increment the
1962 * sequence ID if appropriate.
1965 nfs_owner_seqid_increment(struct nfs_open_owner
*noop
, struct nfs_lock_owner
*nlop
, int error
)
1968 case NFSERR_STALE_CLIENTID
:
1969 case NFSERR_STALE_STATEID
:
1970 case NFSERR_OLD_STATEID
:
1971 case NFSERR_BAD_STATEID
:
1972 case NFSERR_BAD_SEQID
:
1974 case NFSERR_RESOURCE
:
1975 case NFSERR_NOFILEHANDLE
:
1976 /* do not increment the open seqid on these errors */
1986 * Search a node's open file list for any conflicts with this request.
1987 * Also find this open owner's open file structure.
1988 * If not found and "alloc" is set, then allocate one.
1993 struct nfs_open_owner
*noop
,
1994 struct nfs_open_file
**nofpp
,
1995 uint32_t accessMode
,
2000 return nfs_open_file_find_internal(np
, noop
, nofpp
, accessMode
, denyMode
, alloc
);
2004 * Internally, allow using a provisional nodeless nofp (passed in via *nofpp)
2005 * if an existing one is not found. This is used in "create" scenarios to
2006 * officially add the provisional nofp to the node once the node is created.
2009 nfs_open_file_find_internal(
2011 struct nfs_open_owner
*noop
,
2012 struct nfs_open_file
**nofpp
,
2013 uint32_t accessMode
,
2017 struct nfs_open_file
*nofp
= NULL
, *nofp2
, *newnofp
= NULL
;
2022 lck_mtx_lock(&np
->n_openlock
);
2023 TAILQ_FOREACH(nofp2
, &np
->n_opens
, nof_link
) {
2024 if (nofp2
->nof_owner
== noop
) {
2029 if ((accessMode
& nofp2
->nof_deny
) || (denyMode
& nofp2
->nof_access
)) {
2030 /* This request conflicts with an existing open on this client. */
2031 lck_mtx_unlock(&np
->n_openlock
);
2037 * If this open owner doesn't have an open
2038 * file structure yet, we create one for it.
2040 if (!nofp
&& !*nofpp
&& !newnofp
&& alloc
) {
2041 lck_mtx_unlock(&np
->n_openlock
);
2043 MALLOC(newnofp
, struct nfs_open_file
*, sizeof(struct nfs_open_file
), M_TEMP
, M_WAITOK
);
2046 bzero(newnofp
, sizeof(*newnofp
));
2047 lck_mtx_init(&newnofp
->nof_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
2048 newnofp
->nof_owner
= noop
;
2049 nfs_open_owner_ref(noop
);
2050 newnofp
->nof_np
= np
;
2051 lck_mtx_lock(&noop
->noo_lock
);
2052 TAILQ_INSERT_HEAD(&noop
->noo_opens
, newnofp
, nof_oolink
);
2053 lck_mtx_unlock(&noop
->noo_lock
);
2059 (*nofpp
)->nof_np
= np
;
2065 TAILQ_INSERT_HEAD(&np
->n_opens
, nofp
, nof_link
);
2068 lck_mtx_unlock(&np
->n_openlock
);
2070 if (alloc
&& newnofp
&& (nofp
!= newnofp
))
2071 nfs_open_file_destroy(newnofp
);
2074 return (nofp
? 0 : ESRCH
);
2078 * Destroy an open file structure.
2081 nfs_open_file_destroy(struct nfs_open_file
*nofp
)
2083 lck_mtx_lock(&nofp
->nof_owner
->noo_lock
);
2084 TAILQ_REMOVE(&nofp
->nof_owner
->noo_opens
, nofp
, nof_oolink
);
2085 lck_mtx_unlock(&nofp
->nof_owner
->noo_lock
);
2086 nfs_open_owner_rele(nofp
->nof_owner
);
2087 lck_mtx_destroy(&nofp
->nof_lock
, nfs_open_grp
);
2092 * Mark an open file as busy because we are about to
2093 * start an operation that uses and updates open file state.
2096 nfs_open_file_set_busy(struct nfs_open_file
*nofp
, thread_t thd
)
2098 struct nfsmount
*nmp
;
2099 struct timespec ts
= {2, 0};
2100 int error
= 0, slpflag
;
2102 nmp
= nofp
->nof_owner
->noo_mount
;
2103 if (nfs_mount_gone(nmp
))
2105 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
2107 lck_mtx_lock(&nofp
->nof_lock
);
2108 while (nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
) {
2109 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
2111 nofp
->nof_flags
|= NFS_OPEN_FILE_WANT
;
2112 msleep(nofp
, &nofp
->nof_lock
, slpflag
, "nfs_open_file_set_busy", &ts
);
2116 nofp
->nof_flags
|= NFS_OPEN_FILE_BUSY
;
2117 lck_mtx_unlock(&nofp
->nof_lock
);
2123 * Clear the busy flag on an open file and wake up anyone waiting
2127 nfs_open_file_clear_busy(struct nfs_open_file
*nofp
)
2131 lck_mtx_lock(&nofp
->nof_lock
);
2132 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
))
2133 panic("nfs_open_file_clear_busy");
2134 wanted
= (nofp
->nof_flags
& NFS_OPEN_FILE_WANT
);
2135 nofp
->nof_flags
&= ~(NFS_OPEN_FILE_BUSY
|NFS_OPEN_FILE_WANT
);
2136 lck_mtx_unlock(&nofp
->nof_lock
);
2142 * Add the open state for the given access/deny modes to this open file.
2145 nfs_open_file_add_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
, int delegated
)
2147 lck_mtx_lock(&nofp
->nof_lock
);
2148 nofp
->nof_access
|= accessMode
;
2149 nofp
->nof_deny
|= denyMode
;
2152 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2153 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2155 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2157 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2159 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2160 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2162 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2164 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2165 nofp
->nof_d_rw_dw
++;
2166 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2167 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2168 nofp
->nof_d_r_drw
++;
2169 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2170 nofp
->nof_d_w_drw
++;
2171 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2172 nofp
->nof_d_rw_drw
++;
2175 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2176 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2178 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2180 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2182 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2183 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2185 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2187 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2189 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2190 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2192 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2194 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2199 nofp
->nof_opencnt
++;
2200 lck_mtx_unlock(&nofp
->nof_lock
);
2204 * Find which particular open combo will be closed and report what
2205 * the new modes will be and whether the open was delegated.
2208 nfs_open_file_remove_open_find(
2209 struct nfs_open_file
*nofp
,
2210 uint32_t accessMode
,
2212 uint32_t *newAccessMode
,
2213 uint32_t *newDenyMode
,
2217 * Calculate new modes: a mode bit gets removed when there's only
2218 * one count in all the corresponding counts
2220 *newAccessMode
= nofp
->nof_access
;
2221 *newDenyMode
= nofp
->nof_deny
;
2223 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2224 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2225 ((nofp
->nof_r
+ nofp
->nof_d_r
+
2226 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2227 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2228 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2229 nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2230 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2231 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2232 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2233 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2234 ((nofp
->nof_w
+ nofp
->nof_d_w
+
2235 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2236 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2237 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2238 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2239 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2240 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_WRITE
;
2241 if ((denyMode
& NFS_OPEN_SHARE_DENY_READ
) &&
2242 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) &&
2243 ((nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2244 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2245 nofp
->nof_rw_drw
+ nofp
->nof_d_rw_drw
) == 1))
2246 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_READ
;
2247 if ((denyMode
& NFS_OPEN_SHARE_DENY_WRITE
) &&
2248 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
) &&
2249 ((nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2250 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2251 nofp
->nof_rw_drw
+ nofp
->nof_d_rw_drw
+
2252 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2253 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2254 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2255 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_WRITE
;
2257 /* Find the corresponding open access/deny mode counter. */
2258 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2259 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2260 *delegated
= (nofp
->nof_d_r
!= 0);
2261 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2262 *delegated
= (nofp
->nof_d_w
!= 0);
2263 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2264 *delegated
= (nofp
->nof_d_rw
!= 0);
2267 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2268 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2269 *delegated
= (nofp
->nof_d_r_dw
!= 0);
2270 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2271 *delegated
= (nofp
->nof_d_w_dw
!= 0);
2272 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2273 *delegated
= (nofp
->nof_d_rw_dw
!= 0);
2276 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2277 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2278 *delegated
= (nofp
->nof_d_r_drw
!= 0);
2279 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2280 *delegated
= (nofp
->nof_d_w_drw
!= 0);
2281 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2282 *delegated
= (nofp
->nof_d_rw_drw
!= 0);
2289 * Remove the open state for the given access/deny modes to this open file.
2292 nfs_open_file_remove_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
)
2294 uint32_t newAccessMode
, newDenyMode
;
2297 lck_mtx_lock(&nofp
->nof_lock
);
2298 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
2300 /* Decrement the corresponding open access/deny mode counter. */
2301 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2302 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2304 if (nofp
->nof_d_r
== 0)
2305 NP(nofp
->nof_np
, "nfs: open(R) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2309 if (nofp
->nof_r
== 0)
2310 NP(nofp
->nof_np
, "nfs: open(R) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2314 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2316 if (nofp
->nof_d_w
== 0)
2317 NP(nofp
->nof_np
, "nfs: open(W) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2321 if (nofp
->nof_w
== 0)
2322 NP(nofp
->nof_np
, "nfs: open(W) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2326 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2328 if (nofp
->nof_d_rw
== 0)
2329 NP(nofp
->nof_np
, "nfs: open(RW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2333 if (nofp
->nof_rw
== 0)
2334 NP(nofp
->nof_np
, "nfs: open(RW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2339 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2340 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2342 if (nofp
->nof_d_r_dw
== 0)
2343 NP(nofp
->nof_np
, "nfs: open(R,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2347 if (nofp
->nof_r_dw
== 0)
2348 NP(nofp
->nof_np
, "nfs: open(R,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2352 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2354 if (nofp
->nof_d_w_dw
== 0)
2355 NP(nofp
->nof_np
, "nfs: open(W,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2359 if (nofp
->nof_w_dw
== 0)
2360 NP(nofp
->nof_np
, "nfs: open(W,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2364 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2366 if (nofp
->nof_d_rw_dw
== 0)
2367 NP(nofp
->nof_np
, "nfs: open(RW,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2369 nofp
->nof_d_rw_dw
--;
2371 if (nofp
->nof_rw_dw
== 0)
2372 NP(nofp
->nof_np
, "nfs: open(RW,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2377 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2378 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2380 if (nofp
->nof_d_r_drw
== 0)
2381 NP(nofp
->nof_np
, "nfs: open(R,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2383 nofp
->nof_d_r_drw
--;
2385 if (nofp
->nof_r_drw
== 0)
2386 NP(nofp
->nof_np
, "nfs: open(R,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2390 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2392 if (nofp
->nof_d_w_drw
== 0)
2393 NP(nofp
->nof_np
, "nfs: open(W,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2395 nofp
->nof_d_w_drw
--;
2397 if (nofp
->nof_w_drw
== 0)
2398 NP(nofp
->nof_np
, "nfs: open(W,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2402 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2404 if (nofp
->nof_d_rw_drw
== 0)
2405 NP(nofp
->nof_np
, "nfs: open(RW,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2407 nofp
->nof_d_rw_drw
--;
2409 if (nofp
->nof_rw_drw
== 0)
2410 NP(nofp
->nof_np
, "nfs: open(RW,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2417 /* update the modes */
2418 nofp
->nof_access
= newAccessMode
;
2419 nofp
->nof_deny
= newDenyMode
;
2420 nofp
->nof_opencnt
--;
2421 lck_mtx_unlock(&nofp
->nof_lock
);
2426 * Get the current (delegation, lock, open, default) stateid for this node.
2427 * If node has a delegation, use that stateid.
2428 * If pid has a lock, use the lockowner's stateid.
2429 * Or use the open file's stateid.
2430 * If no open file, use a default stateid of all ones.
2433 nfs_get_stateid(nfsnode_t np
, thread_t thd
, kauth_cred_t cred
, nfs_stateid
*sid
)
2435 struct nfsmount
*nmp
= NFSTONMP(np
);
2436 proc_t p
= thd
? get_bsdthreadtask_info(thd
) : current_proc(); // XXX async I/O requests don't have a thread
2437 struct nfs_open_owner
*noop
= NULL
;
2438 struct nfs_open_file
*nofp
= NULL
;
2439 struct nfs_lock_owner
*nlop
= NULL
;
2440 nfs_stateid
*s
= NULL
;
2442 if (np
->n_openflags
& N_DELEG_MASK
) {
2443 s
= &np
->n_dstateid
;
2446 nlop
= nfs_lock_owner_find(np
, p
, 0);
2447 if (nlop
&& !TAILQ_EMPTY(&nlop
->nlo_locks
)) {
2448 /* we hold locks, use lock stateid */
2449 s
= &nlop
->nlo_stateid
;
2450 } else if (((noop
= nfs_open_owner_find(nmp
, cred
, 0))) &&
2451 (nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0) == 0) &&
2452 !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) &&
2454 /* we (should) have the file open, use open stateid */
2455 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)
2456 nfs4_reopen(nofp
, thd
);
2457 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
2458 s
= &nofp
->nof_stateid
;
2463 sid
->seqid
= s
->seqid
;
2464 sid
->other
[0] = s
->other
[0];
2465 sid
->other
[1] = s
->other
[1];
2466 sid
->other
[2] = s
->other
[2];
2468 /* named attributes may not have a stateid for reads, so don't complain for them */
2469 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
2470 NP(np
, "nfs_get_stateid: no stateid");
2471 sid
->seqid
= sid
->other
[0] = sid
->other
[1] = sid
->other
[2] = 0xffffffff;
2474 nfs_lock_owner_rele(nlop
);
2476 nfs_open_owner_rele(noop
);
2481 * When we have a delegation, we may be able to perform the OPEN locally.
2482 * Perform the OPEN by checking the delegation ACE and/or checking via ACCESS.
2485 nfs4_open_delegated(
2487 struct nfs_open_file
*nofp
,
2488 uint32_t accessMode
,
2492 int error
= 0, ismember
, readtoo
= 0, authorized
= 0;
2494 struct kauth_acl_eval eval
;
2495 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2497 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2499 * Try to open it for read access too,
2500 * so the buffer cache can read data.
2503 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2508 if (accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)
2509 action
|= KAUTH_VNODE_READ_DATA
;
2510 if (accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
)
2511 action
|= KAUTH_VNODE_WRITE_DATA
;
2513 /* evaluate ACE (if we have one) */
2514 if (np
->n_dace
.ace_flags
) {
2515 eval
.ae_requested
= action
;
2516 eval
.ae_acl
= &np
->n_dace
;
2518 eval
.ae_options
= 0;
2519 if (np
->n_vattr
.nva_uid
== kauth_cred_getuid(cred
))
2520 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
2521 error
= kauth_cred_ismember_gid(cred
, np
->n_vattr
.nva_gid
, &ismember
);
2522 if (!error
&& ismember
)
2523 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
2525 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
2526 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
2527 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
2528 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
2530 error
= kauth_acl_evaluate(cred
, &eval
);
2532 if (!error
&& (eval
.ae_result
== KAUTH_RESULT_ALLOW
))
2537 /* need to ask the server via ACCESS */
2538 struct vnop_access_args naa
;
2539 naa
.a_desc
= &vnop_access_desc
;
2540 naa
.a_vp
= NFSTOV(np
);
2541 naa
.a_action
= action
;
2542 naa
.a_context
= ctx
;
2543 if (!(error
= nfs_vnop_access(&naa
)))
2549 /* try again without the extra read access */
2550 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2554 return (error
? error
: EACCES
);
2557 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 1);
2564 * Open a file with the given access/deny modes.
2566 * If we have a delegation, we may be able to handle the open locally.
2567 * Otherwise, we will always send the open RPC even if this open's mode is
2568 * a subset of all the existing opens. This makes sure that we will always
2569 * be able to do a downgrade to any of the open modes.
2571 * Note: local conflicts should have already been checked in nfs_open_file_find().
2576 struct nfs_open_file
*nofp
,
2577 uint32_t accessMode
,
2581 vnode_t vp
= NFSTOV(np
);
2583 struct componentname cn
;
2584 const char *vname
= NULL
;
2586 char smallname
[128];
2587 char *filename
= NULL
;
2588 int error
= 0, readtoo
= 0;
2591 * We can handle the OPEN ourselves if we have a delegation,
2592 * unless it's a read delegation and the open is asking for
2593 * either write access or deny read. We also don't bother to
2594 * use the delegation if it's being returned.
2596 if (np
->n_openflags
& N_DELEG_MASK
) {
2597 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
2599 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
) &&
2600 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ||
2601 (!(accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) && !(denyMode
& NFS_OPEN_SHARE_DENY_READ
)))) {
2602 error
= nfs4_open_delegated(np
, nofp
, accessMode
, denyMode
, ctx
);
2603 nfs_open_state_clear_busy(np
);
2606 nfs_open_state_clear_busy(np
);
2610 * [sigh] We can't trust VFS to get the parent right for named
2611 * attribute nodes. (It likes to reparent the nodes after we've
2612 * created them.) Luckily we can probably get the right parent
2613 * from the n_parent we have stashed away.
2615 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
2616 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
2619 dvp
= vnode_getparent(vp
);
2620 vname
= vnode_getname(vp
);
2621 if (!dvp
|| !vname
) {
2626 filename
= &smallname
[0];
2627 namelen
= snprintf(filename
, sizeof(smallname
), "%s", vname
);
2628 if (namelen
>= sizeof(smallname
)) {
2629 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
2634 snprintf(filename
, namelen
+1, "%s", vname
);
2636 bzero(&cn
, sizeof(cn
));
2637 cn
.cn_nameptr
= filename
;
2638 cn
.cn_namelen
= namelen
;
2640 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2642 * Try to open it for read access too,
2643 * so the buffer cache can read data.
2646 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2649 error
= nfs4_open_rpc(nofp
, ctx
, &cn
, NULL
, dvp
, &vp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
2651 if (!nfs_mount_state_error_should_restart(error
) &&
2652 (error
!= EINTR
) && (error
!= ERESTART
) && readtoo
) {
2653 /* try again without the extra read access */
2654 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2660 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
2662 if (filename
&& (filename
!= &smallname
[0]))
2663 FREE(filename
, M_TEMP
);
2665 vnode_putname(vname
);
2673 struct vnop_mmap_args
/* {
2674 struct vnodeop_desc *a_desc;
2677 vfs_context_t a_context;
2680 vfs_context_t ctx
= ap
->a_context
;
2681 vnode_t vp
= ap
->a_vp
;
2682 nfsnode_t np
= VTONFS(vp
);
2683 int error
= 0, accessMode
, denyMode
, delegated
;
2684 struct nfsmount
*nmp
;
2685 struct nfs_open_owner
*noop
= NULL
;
2686 struct nfs_open_file
*nofp
= NULL
;
2689 if (nfs_mount_gone(nmp
))
2692 if (!vnode_isreg(vp
) || !(ap
->a_fflags
& (PROT_READ
|PROT_WRITE
)))
2694 if (np
->n_flag
& NREVOKE
)
2698 * fflags contains some combination of: PROT_READ, PROT_WRITE
2699 * Since it's not possible to mmap() without having the file open for reading,
2700 * read access is always there (regardless if PROT_READ is not set).
2702 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
2703 if (ap
->a_fflags
& PROT_WRITE
)
2704 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
2705 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2707 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
2712 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2714 nfs_open_owner_rele(noop
);
2717 if (np
->n_flag
& NREVOKE
) {
2719 nfs_mount_state_in_use_end(nmp
, 0);
2720 nfs_open_owner_rele(noop
);
2724 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
2725 if (error
|| (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))) {
2726 NP(np
, "nfs_vnop_mmap: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
2729 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
2730 nfs_mount_state_in_use_end(nmp
, 0);
2731 error
= nfs4_reopen(nofp
, NULL
);
2737 error
= nfs_open_file_set_busy(nofp
, NULL
);
2744 * The open reference for mmap must mirror an existing open because
2745 * we may need to reclaim it after the file is closed.
2746 * So grab another open count matching the accessMode passed in.
2747 * If we already had an mmap open, prefer read/write without deny mode.
2748 * This means we may have to drop the current mmap open first.
2750 * N.B. We should have an open for the mmap, because, mmap was
2751 * called on an open descriptor, or we've created an open for read
2752 * from reading the first page for execve. However, if we piggy
2753 * backed on an existing NFS_OPEN_SHARE_ACCESS_READ/NFS_OPEN_SHARE_DENY_NONE
2754 * that open may have closed.
2757 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2758 if (nofp
->nof_flags
& NFS_OPEN_FILE_NEEDCLOSE
) {
2759 /* We shouldn't get here. We've already open the file for execve */
2760 NP(np
, "nfs_vnop_mmap: File already needs close access: 0x%x, cred: %d thread: %lld",
2761 nofp
->nof_access
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
), thread_tid(vfs_context_thread(ctx
)));
2764 * mmapings for execve are just for read. Get out with EPERM if the accessMode is not ACCESS_READ
2765 * or the access would be denied. Other accesses should have an open descriptor for the mapping.
2767 if (accessMode
!= NFS_OPEN_SHARE_ACCESS_READ
|| (accessMode
& nofp
->nof_deny
)) {
2768 /* not asking for just read access -> fail */
2772 /* we don't have the file open, so open it for read access */
2773 if (nmp
->nm_vers
< NFS_VER4
) {
2774 /* NFS v2/v3 opens are always allowed - so just add it. */
2775 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
2778 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2781 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
2786 /* determine deny mode for open */
2787 if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2788 if (nofp
->nof_d_rw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_d_rw_drw
) {
2791 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2792 else if (nofp
->nof_d_rw_dw
)
2793 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2794 else if (nofp
->nof_d_rw_drw
)
2795 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2796 } else if (nofp
->nof_rw
|| nofp
->nof_rw_dw
|| nofp
->nof_rw_drw
) {
2799 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2800 else if (nofp
->nof_rw_dw
)
2801 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2802 else if (nofp
->nof_rw_drw
)
2803 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2807 } else { /* NFS_OPEN_SHARE_ACCESS_READ */
2808 if (nofp
->nof_d_r
|| nofp
->nof_d_r_dw
|| nofp
->nof_d_r_drw
) {
2811 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2812 else if (nofp
->nof_d_r_dw
)
2813 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2814 else if (nofp
->nof_d_r_drw
)
2815 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2816 } else if (nofp
->nof_r
|| nofp
->nof_r_dw
|| nofp
->nof_r_drw
) {
2819 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2820 else if (nofp
->nof_r_dw
)
2821 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2822 else if (nofp
->nof_r_drw
)
2823 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2824 } else if (nofp
->nof_d_rw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_d_rw_drw
) {
2826 * This clause and the one below is to co-opt a read write access
2827 * for a read only mmaping. We probably got here in that an
2828 * existing rw open for an executable file already exists.
2831 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
2833 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2834 else if (nofp
->nof_d_rw_dw
)
2835 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2836 else if (nofp
->nof_d_rw_drw
)
2837 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2838 } else if (nofp
->nof_rw
|| nofp
->nof_rw_dw
|| nofp
->nof_rw_drw
) {
2840 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
2842 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2843 else if (nofp
->nof_rw_dw
)
2844 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2845 else if (nofp
->nof_rw_drw
)
2846 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2851 if (error
) /* mmap mode without proper open mode */
2855 * If the existing mmap access is more than the new access OR the
2856 * existing access is the same and the existing deny mode is less,
2857 * then we'll stick with the existing mmap open mode.
2859 if ((nofp
->nof_mmap_access
> accessMode
) ||
2860 ((nofp
->nof_mmap_access
== accessMode
) && (nofp
->nof_mmap_deny
<= denyMode
)))
2863 /* update mmap open mode */
2864 if (nofp
->nof_mmap_access
) {
2865 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2867 if (!nfs_mount_state_error_should_restart(error
))
2868 NP(np
, "nfs_vnop_mmap: close of previous mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2869 NP(np
, "nfs_vnop_mmap: update, close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2872 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2875 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
2876 nofp
->nof_mmap_access
= accessMode
;
2877 nofp
->nof_mmap_deny
= denyMode
;
2881 nfs_open_file_clear_busy(nofp
);
2882 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2887 nfs_open_owner_rele(noop
);
2891 nfs_node_lock_force(np
);
2892 if ((np
->n_flag
& NISMAPPED
) == 0) {
2893 np
->n_flag
|= NISMAPPED
;
2896 nfs_node_unlock(np
);
2898 lck_mtx_lock(&nmp
->nm_lock
);
2899 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
2900 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
2901 if (nmp
->nm_curdeadtimeout
<= 0)
2902 nmp
->nm_deadto_start
= 0;
2904 lck_mtx_unlock(&nmp
->nm_lock
);
2914 struct vnop_mnomap_args
/* {
2915 struct vnodeop_desc *a_desc;
2917 vfs_context_t a_context;
2920 vfs_context_t ctx
= ap
->a_context
;
2921 vnode_t vp
= ap
->a_vp
;
2922 nfsnode_t np
= VTONFS(vp
);
2923 struct nfsmount
*nmp
;
2924 struct nfs_open_file
*nofp
= NULL
;
2927 int is_mapped_flag
= 0;
2930 if (nfs_mount_gone(nmp
))
2933 nfs_node_lock_force(np
);
2934 if (np
->n_flag
& NISMAPPED
) {
2936 np
->n_flag
&= ~NISMAPPED
;
2938 nfs_node_unlock(np
);
2939 if (is_mapped_flag
) {
2940 lck_mtx_lock(&nmp
->nm_lock
);
2941 if (nmp
->nm_mappers
)
2944 NP(np
, "nfs_vnop_mnomap: removing mmap reference from mount, but mount has no files mmapped");
2945 lck_mtx_unlock(&nmp
->nm_lock
);
2948 /* flush buffers/ubc before we drop the open (in case it's our last open) */
2949 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
2950 if (UBCINFOEXISTS(vp
) && (size
= ubc_getsize(vp
)))
2951 ubc_msync(vp
, 0, size
, NULL
, UBC_PUSHALL
| UBC_SYNC
);
2953 /* walk all open files and close all mmap opens */
2955 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2958 lck_mtx_lock(&np
->n_openlock
);
2959 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
2960 if (!nofp
->nof_mmap_access
)
2962 lck_mtx_unlock(&np
->n_openlock
);
2963 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
2964 nfs_mount_state_in_use_end(nmp
, 0);
2965 error
= nfs4_reopen(nofp
, NULL
);
2970 error
= nfs_open_file_set_busy(nofp
, NULL
);
2972 lck_mtx_lock(&np
->n_openlock
);
2975 if (nofp
->nof_mmap_access
) {
2976 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2977 if (!nfs_mount_state_error_should_restart(error
)) {
2978 if (error
) /* not a state-operation-restarting error, so just clear the access */
2979 NP(np
, "nfs_vnop_mnomap: close of mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2980 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2983 NP(np
, "nfs_vnop_mnomap: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2985 nfs_open_file_clear_busy(nofp
);
2986 nfs_mount_state_in_use_end(nmp
, error
);
2989 lck_mtx_unlock(&np
->n_openlock
);
2990 nfs_mount_state_in_use_end(nmp
, error
);
2995 * Search a node's lock owner list for the owner for this process.
2996 * If not found and "alloc" is set, then allocate a new one.
2998 struct nfs_lock_owner
*
2999 nfs_lock_owner_find(nfsnode_t np
, proc_t p
, int alloc
)
3001 pid_t pid
= proc_pid(p
);
3002 struct nfs_lock_owner
*nlop
, *newnlop
= NULL
;
3005 lck_mtx_lock(&np
->n_openlock
);
3006 TAILQ_FOREACH(nlop
, &np
->n_lock_owners
, nlo_link
) {
3007 if (nlop
->nlo_pid
!= pid
)
3009 if (timevalcmp(&nlop
->nlo_pid_start
, &p
->p_start
, ==))
3011 /* stale lock owner... reuse it if we can */
3012 if (nlop
->nlo_refcnt
) {
3013 TAILQ_REMOVE(&np
->n_lock_owners
, nlop
, nlo_link
);
3014 nlop
->nlo_flags
&= ~NFS_LOCK_OWNER_LINK
;
3015 lck_mtx_unlock(&np
->n_openlock
);
3018 nlop
->nlo_pid_start
= p
->p_start
;
3019 nlop
->nlo_seqid
= 0;
3020 nlop
->nlo_stategenid
= 0;
3024 if (!nlop
&& !newnlop
&& alloc
) {
3025 lck_mtx_unlock(&np
->n_openlock
);
3026 MALLOC(newnlop
, struct nfs_lock_owner
*, sizeof(struct nfs_lock_owner
), M_TEMP
, M_WAITOK
);
3029 bzero(newnlop
, sizeof(*newnlop
));
3030 lck_mtx_init(&newnlop
->nlo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
3031 newnlop
->nlo_pid
= pid
;
3032 newnlop
->nlo_pid_start
= p
->p_start
;
3033 newnlop
->nlo_name
= OSAddAtomic(1, &nfs_lock_owner_seqnum
);
3034 TAILQ_INIT(&newnlop
->nlo_locks
);
3037 if (!nlop
&& newnlop
) {
3038 newnlop
->nlo_flags
|= NFS_LOCK_OWNER_LINK
;
3039 TAILQ_INSERT_HEAD(&np
->n_lock_owners
, newnlop
, nlo_link
);
3042 lck_mtx_unlock(&np
->n_openlock
);
3044 if (newnlop
&& (nlop
!= newnlop
))
3045 nfs_lock_owner_destroy(newnlop
);
3048 nfs_lock_owner_ref(nlop
);
3054 * destroy a lock owner that's no longer needed
3057 nfs_lock_owner_destroy(struct nfs_lock_owner
*nlop
)
3059 if (nlop
->nlo_open_owner
) {
3060 nfs_open_owner_rele(nlop
->nlo_open_owner
);
3061 nlop
->nlo_open_owner
= NULL
;
3063 lck_mtx_destroy(&nlop
->nlo_lock
, nfs_open_grp
);
3068 * acquire a reference count on a lock owner
3071 nfs_lock_owner_ref(struct nfs_lock_owner
*nlop
)
3073 lck_mtx_lock(&nlop
->nlo_lock
);
3075 lck_mtx_unlock(&nlop
->nlo_lock
);
3079 * drop a reference count on a lock owner and destroy it if
3080 * it is no longer referenced and no longer on the mount's list.
3083 nfs_lock_owner_rele(struct nfs_lock_owner
*nlop
)
3085 lck_mtx_lock(&nlop
->nlo_lock
);
3086 if (nlop
->nlo_refcnt
< 1)
3087 panic("nfs_lock_owner_rele: no refcnt");
3089 if (!nlop
->nlo_refcnt
&& (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
3090 panic("nfs_lock_owner_rele: busy");
3091 /* XXX we may potentially want to clean up idle/unused lock owner structures */
3092 if (nlop
->nlo_refcnt
|| (nlop
->nlo_flags
& NFS_LOCK_OWNER_LINK
)) {
3093 lck_mtx_unlock(&nlop
->nlo_lock
);
3096 /* owner is no longer referenced or linked to mount, so destroy it */
3097 lck_mtx_unlock(&nlop
->nlo_lock
);
3098 nfs_lock_owner_destroy(nlop
);
3102 * Mark a lock owner as busy because we are about to
3103 * start an operation that uses and updates lock owner state.
3106 nfs_lock_owner_set_busy(struct nfs_lock_owner
*nlop
, thread_t thd
)
3108 struct nfsmount
*nmp
;
3109 struct timespec ts
= {2, 0};
3110 int error
= 0, slpflag
;
3112 nmp
= nlop
->nlo_open_owner
->noo_mount
;
3113 if (nfs_mount_gone(nmp
))
3115 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
3117 lck_mtx_lock(&nlop
->nlo_lock
);
3118 while (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
) {
3119 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
3121 nlop
->nlo_flags
|= NFS_LOCK_OWNER_WANT
;
3122 msleep(nlop
, &nlop
->nlo_lock
, slpflag
, "nfs_lock_owner_set_busy", &ts
);
3126 nlop
->nlo_flags
|= NFS_LOCK_OWNER_BUSY
;
3127 lck_mtx_unlock(&nlop
->nlo_lock
);
3133 * Clear the busy flag on a lock owner and wake up anyone waiting
3137 nfs_lock_owner_clear_busy(struct nfs_lock_owner
*nlop
)
3141 lck_mtx_lock(&nlop
->nlo_lock
);
3142 if (!(nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
3143 panic("nfs_lock_owner_clear_busy");
3144 wanted
= (nlop
->nlo_flags
& NFS_LOCK_OWNER_WANT
);
3145 nlop
->nlo_flags
&= ~(NFS_LOCK_OWNER_BUSY
|NFS_LOCK_OWNER_WANT
);
3146 lck_mtx_unlock(&nlop
->nlo_lock
);
3152 * Insert a held lock into a lock owner's sorted list.
3153 * (flock locks are always inserted at the head the list)
3156 nfs_lock_owner_insert_held_lock(struct nfs_lock_owner
*nlop
, struct nfs_file_lock
*newnflp
)
3158 struct nfs_file_lock
*nflp
;
3160 /* insert new lock in lock owner's held lock list */
3161 lck_mtx_lock(&nlop
->nlo_lock
);
3162 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) {
3163 TAILQ_INSERT_HEAD(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3165 TAILQ_FOREACH(nflp
, &nlop
->nlo_locks
, nfl_lolink
) {
3166 if (newnflp
->nfl_start
< nflp
->nfl_start
)
3170 TAILQ_INSERT_BEFORE(nflp
, newnflp
, nfl_lolink
);
3172 TAILQ_INSERT_TAIL(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3174 lck_mtx_unlock(&nlop
->nlo_lock
);
3178 * Get a file lock structure for this lock owner.
3180 struct nfs_file_lock
*
3181 nfs_file_lock_alloc(struct nfs_lock_owner
*nlop
)
3183 struct nfs_file_lock
*nflp
= NULL
;
3185 lck_mtx_lock(&nlop
->nlo_lock
);
3186 if (!nlop
->nlo_alock
.nfl_owner
) {
3187 nflp
= &nlop
->nlo_alock
;
3188 nflp
->nfl_owner
= nlop
;
3190 lck_mtx_unlock(&nlop
->nlo_lock
);
3192 MALLOC(nflp
, struct nfs_file_lock
*, sizeof(struct nfs_file_lock
), M_TEMP
, M_WAITOK
);
3195 bzero(nflp
, sizeof(*nflp
));
3196 nflp
->nfl_flags
|= NFS_FILE_LOCK_ALLOC
;
3197 nflp
->nfl_owner
= nlop
;
3199 nfs_lock_owner_ref(nlop
);
3204 * destroy the given NFS file lock structure
3207 nfs_file_lock_destroy(struct nfs_file_lock
*nflp
)
3209 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3211 if (nflp
->nfl_flags
& NFS_FILE_LOCK_ALLOC
) {
3212 nflp
->nfl_owner
= NULL
;
3215 lck_mtx_lock(&nlop
->nlo_lock
);
3216 bzero(nflp
, sizeof(*nflp
));
3217 lck_mtx_unlock(&nlop
->nlo_lock
);
3219 nfs_lock_owner_rele(nlop
);
3223 * Check if one file lock conflicts with another.
3224 * (nflp1 is the new lock. nflp2 is the existing lock.)
3227 nfs_file_lock_conflict(struct nfs_file_lock
*nflp1
, struct nfs_file_lock
*nflp2
, int *willsplit
)
3229 /* no conflict if lock is dead */
3230 if ((nflp1
->nfl_flags
& NFS_FILE_LOCK_DEAD
) || (nflp2
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
3232 /* no conflict if it's ours - unless the lock style doesn't match */
3233 if ((nflp1
->nfl_owner
== nflp2
->nfl_owner
) &&
3234 ((nflp1
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == (nflp2
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))) {
3235 if (willsplit
&& (nflp1
->nfl_type
!= nflp2
->nfl_type
) &&
3236 (nflp1
->nfl_start
> nflp2
->nfl_start
) &&
3237 (nflp1
->nfl_end
< nflp2
->nfl_end
))
3241 /* no conflict if ranges don't overlap */
3242 if ((nflp1
->nfl_start
> nflp2
->nfl_end
) || (nflp1
->nfl_end
< nflp2
->nfl_start
))
3244 /* no conflict if neither lock is exclusive */
3245 if ((nflp1
->nfl_type
!= F_WRLCK
) && (nflp2
->nfl_type
!= F_WRLCK
))
3252 * Send an NFSv4 LOCK RPC to the server.
3257 struct nfs_open_file
*nofp
,
3258 struct nfs_file_lock
*nflp
,
3264 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3265 struct nfsmount
*nmp
;
3266 struct nfsm_chain nmreq
, nmrep
;
3269 int error
= 0, lockerror
= ENOENT
, newlocker
, numops
, status
;
3270 struct nfsreq_secinfo_args si
;
3273 if (nfs_mount_gone(nmp
))
3275 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3278 newlocker
= (nlop
->nlo_stategenid
!= nmp
->nm_stategenid
);
3279 locktype
= (nflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
) ?
3280 ((nflp
->nfl_type
== F_WRLCK
) ?
3281 NFS_LOCK_TYPE_WRITEW
:
3282 NFS_LOCK_TYPE_READW
) :
3283 ((nflp
->nfl_type
== F_WRLCK
) ?
3284 NFS_LOCK_TYPE_WRITE
:
3285 NFS_LOCK_TYPE_READ
);
3287 error
= nfs_open_file_set_busy(nofp
, thd
);
3290 error
= nfs_open_owner_set_busy(nofp
->nof_owner
, thd
);
3292 nfs_open_file_clear_busy(nofp
);
3295 if (!nlop
->nlo_open_owner
) {
3296 nfs_open_owner_ref(nofp
->nof_owner
);
3297 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3300 error
= nfs_lock_owner_set_busy(nlop
, thd
);
3303 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3304 nfs_open_file_clear_busy(nofp
);
3309 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3310 nfsm_chain_null(&nmreq
);
3311 nfsm_chain_null(&nmrep
);
3313 // PUTFH, GETATTR, LOCK
3315 nfsm_chain_build_alloc_init(error
, &nmreq
, 33 * NFSX_UNSIGNED
);
3316 nfsm_chain_add_compound_header(error
, &nmreq
, "lock", nmp
->nm_minor_vers
, numops
);
3318 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3319 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3321 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3322 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3324 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCK
);
3325 nfsm_chain_add_32(error
, &nmreq
, locktype
);
3326 nfsm_chain_add_32(error
, &nmreq
, reclaim
);
3327 nfsm_chain_add_64(error
, &nmreq
, nflp
->nfl_start
);
3328 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
));
3329 nfsm_chain_add_32(error
, &nmreq
, newlocker
);
3331 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_owner
->noo_seqid
);
3332 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
3333 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3334 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3336 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3337 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3339 nfsm_chain_build_done(error
, &nmreq
);
3340 nfsm_assert(error
, (numops
== 0), EPROTO
);
3343 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3345 if ((lockerror
= nfs_node_lock(np
)))
3347 nfsm_chain_skip_tag(error
, &nmrep
);
3348 nfsm_chain_get_32(error
, &nmrep
, numops
);
3349 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3351 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3352 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3354 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCK
);
3355 nfs_owner_seqid_increment(newlocker
? nofp
->nof_owner
: NULL
, nlop
, error
);
3356 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3358 /* Update the lock owner's stategenid once it appears the server has state for it. */
3359 /* We determine this by noting the request was successful (we got a stateid). */
3360 if (newlocker
&& !error
)
3361 nlop
->nlo_stategenid
= nmp
->nm_stategenid
;
3364 nfs_node_unlock(np
);
3365 nfs_lock_owner_clear_busy(nlop
);
3367 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3368 nfs_open_file_clear_busy(nofp
);
3370 nfsm_chain_cleanup(&nmreq
);
3371 nfsm_chain_cleanup(&nmrep
);
3376 * Send an NFSv4 LOCKU RPC to the server.
3381 struct nfs_lock_owner
*nlop
,
3389 struct nfsmount
*nmp
;
3390 struct nfsm_chain nmreq
, nmrep
;
3392 int error
= 0, lockerror
= ENOENT
, numops
, status
;
3393 struct nfsreq_secinfo_args si
;
3396 if (nfs_mount_gone(nmp
))
3398 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3401 error
= nfs_lock_owner_set_busy(nlop
, NULL
);
3405 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3406 nfsm_chain_null(&nmreq
);
3407 nfsm_chain_null(&nmrep
);
3409 // PUTFH, GETATTR, LOCKU
3411 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3412 nfsm_chain_add_compound_header(error
, &nmreq
, "unlock", nmp
->nm_minor_vers
, numops
);
3414 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3415 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3417 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3418 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3420 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKU
);
3421 nfsm_chain_add_32(error
, &nmreq
, (type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3422 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3423 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3424 nfsm_chain_add_64(error
, &nmreq
, start
);
3425 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3426 nfsm_chain_build_done(error
, &nmreq
);
3427 nfsm_assert(error
, (numops
== 0), EPROTO
);
3430 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3432 if ((lockerror
= nfs_node_lock(np
)))
3434 nfsm_chain_skip_tag(error
, &nmrep
);
3435 nfsm_chain_get_32(error
, &nmrep
, numops
);
3436 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3438 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3439 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3441 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKU
);
3442 nfs_owner_seqid_increment(NULL
, nlop
, error
);
3443 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3446 nfs_node_unlock(np
);
3447 nfs_lock_owner_clear_busy(nlop
);
3448 nfsm_chain_cleanup(&nmreq
);
3449 nfsm_chain_cleanup(&nmrep
);
3454 * Send an NFSv4 LOCKT RPC to the server.
3459 struct nfs_lock_owner
*nlop
,
3465 struct nfsmount
*nmp
;
3466 struct nfsm_chain nmreq
, nmrep
;
3467 uint64_t xid
, val64
= 0;
3469 int error
= 0, lockerror
, numops
, status
;
3470 struct nfsreq_secinfo_args si
;
3473 if (nfs_mount_gone(nmp
))
3475 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3479 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3480 nfsm_chain_null(&nmreq
);
3481 nfsm_chain_null(&nmrep
);
3483 // PUTFH, GETATTR, LOCKT
3485 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3486 nfsm_chain_add_compound_header(error
, &nmreq
, "locktest", nmp
->nm_minor_vers
, numops
);
3488 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3489 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3491 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3492 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3494 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKT
);
3495 nfsm_chain_add_32(error
, &nmreq
, (fl
->l_type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3496 nfsm_chain_add_64(error
, &nmreq
, start
);
3497 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3498 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3499 nfsm_chain_build_done(error
, &nmreq
);
3500 nfsm_assert(error
, (numops
== 0), EPROTO
);
3503 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
3505 if ((lockerror
= nfs_node_lock(np
)))
3507 nfsm_chain_skip_tag(error
, &nmrep
);
3508 nfsm_chain_get_32(error
, &nmrep
, numops
);
3509 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3511 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3512 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3514 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKT
);
3515 if (error
== NFSERR_DENIED
) {
3517 nfsm_chain_get_64(error
, &nmrep
, fl
->l_start
);
3518 nfsm_chain_get_64(error
, &nmrep
, val64
);
3519 fl
->l_len
= (val64
== UINT64_MAX
) ? 0 : val64
;
3520 nfsm_chain_get_32(error
, &nmrep
, val
);
3521 fl
->l_type
= (val
== NFS_LOCK_TYPE_WRITE
) ? F_WRLCK
: F_RDLCK
;
3523 fl
->l_whence
= SEEK_SET
;
3524 } else if (!error
) {
3525 fl
->l_type
= F_UNLCK
;
3529 nfs_node_unlock(np
);
3530 nfsm_chain_cleanup(&nmreq
);
3531 nfsm_chain_cleanup(&nmrep
);
3537 * Check for any conflicts with the given lock.
3539 * Checking for a lock doesn't require the file to be opened.
3540 * So we skip all the open owner, open file, lock owner work
3541 * and just check for a conflicting lock.
3544 nfs_advlock_getlock(
3546 struct nfs_lock_owner
*nlop
,
3552 struct nfsmount
*nmp
;
3553 struct nfs_file_lock
*nflp
;
3554 int error
= 0, answered
= 0;
3557 if (nfs_mount_gone(nmp
))
3561 if ((error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
))))
3564 lck_mtx_lock(&np
->n_openlock
);
3565 /* scan currently held locks for conflict */
3566 TAILQ_FOREACH(nflp
, &np
->n_locks
, nfl_link
) {
3567 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3569 if ((start
<= nflp
->nfl_end
) && (end
>= nflp
->nfl_start
) &&
3570 ((fl
->l_type
== F_WRLCK
) || (nflp
->nfl_type
== F_WRLCK
)))
3574 /* found a conflicting lock */
3575 fl
->l_type
= nflp
->nfl_type
;
3576 fl
->l_pid
= (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_FLOCK
) ? -1 : nflp
->nfl_owner
->nlo_pid
;
3577 fl
->l_start
= nflp
->nfl_start
;
3578 fl
->l_len
= NFS_FLOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
);
3579 fl
->l_whence
= SEEK_SET
;
3581 } else if ((np
->n_openflags
& N_DELEG_WRITE
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3583 * If we have a write delegation, we know there can't be other
3584 * locks on the server. So the answer is no conflicting lock found.
3586 fl
->l_type
= F_UNLCK
;
3589 lck_mtx_unlock(&np
->n_openlock
);
3591 nfs_mount_state_in_use_end(nmp
, 0);
3595 /* no conflict found locally, so ask the server */
3596 error
= nmp
->nm_funcs
->nf_getlock_rpc(np
, nlop
, fl
, start
, end
, ctx
);
3598 if (nfs_mount_state_in_use_end(nmp
, error
))
3604 * Acquire a file lock for the given range.
3606 * Add the lock (request) to the lock queue.
3607 * Scan the lock queue for any conflicting locks.
3608 * If a conflict is found, block or return an error.
3609 * Once end of queue is reached, send request to the server.
3610 * If the server grants the lock, scan the lock queue and
3611 * update any existing locks. Then (optionally) scan the
3612 * queue again to coalesce any locks adjacent to the new one.
3615 nfs_advlock_setlock(
3617 struct nfs_open_file
*nofp
,
3618 struct nfs_lock_owner
*nlop
,
3626 struct nfsmount
*nmp
;
3627 struct nfs_file_lock
*newnflp
, *nflp
, *nflp2
= NULL
, *nextnflp
, *flocknflp
= NULL
;
3628 struct nfs_file_lock
*coalnflp
;
3629 int error
= 0, error2
, willsplit
= 0, delay
, slpflag
, busy
= 0, inuse
= 0, restart
, inqueue
= 0;
3630 struct timespec ts
= {1, 0};
3633 if (nfs_mount_gone(nmp
))
3635 slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
3637 if ((type
!= F_RDLCK
) && (type
!= F_WRLCK
))
3640 /* allocate a new lock */
3641 newnflp
= nfs_file_lock_alloc(nlop
);
3644 newnflp
->nfl_start
= start
;
3645 newnflp
->nfl_end
= end
;
3646 newnflp
->nfl_type
= type
;
3648 newnflp
->nfl_flags
|= NFS_FILE_LOCK_WAIT
;
3649 newnflp
->nfl_flags
|= style
;
3650 newnflp
->nfl_flags
|= NFS_FILE_LOCK_BLOCKED
;
3652 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && (type
== F_WRLCK
)) {
3654 * For exclusive flock-style locks, if we block waiting for the
3655 * lock, we need to first release any currently held shared
3656 * flock-style lock. So, the first thing we do is check if we
3657 * have a shared flock-style lock.
3659 nflp
= TAILQ_FIRST(&nlop
->nlo_locks
);
3660 if (nflp
&& ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_FLOCK
))
3662 if (nflp
&& (nflp
->nfl_type
!= F_RDLCK
))
3669 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3673 if (np
->n_flag
& NREVOKE
) {
3675 nfs_mount_state_in_use_end(nmp
, 0);
3679 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
3680 nfs_mount_state_in_use_end(nmp
, 0);
3682 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
3688 lck_mtx_lock(&np
->n_openlock
);
3690 /* insert new lock at beginning of list */
3691 TAILQ_INSERT_HEAD(&np
->n_locks
, newnflp
, nfl_link
);
3695 /* scan current list of locks (held and pending) for conflicts */
3696 for (nflp
= TAILQ_NEXT(newnflp
, nfl_link
); nflp
; nflp
= nextnflp
) {
3697 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
3698 if (!nfs_file_lock_conflict(newnflp
, nflp
, &willsplit
))
3701 if (!(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3705 /* Block until this lock is no longer held. */
3706 if (nflp
->nfl_blockcnt
== UINT_MAX
) {
3710 nflp
->nfl_blockcnt
++;
3713 /* release any currently held shared lock before sleeping */
3714 lck_mtx_unlock(&np
->n_openlock
);
3715 nfs_mount_state_in_use_end(nmp
, 0);
3717 error
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3720 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3722 lck_mtx_lock(&np
->n_openlock
);
3726 lck_mtx_lock(&np
->n_openlock
);
3727 /* no need to block/sleep if the conflict is gone */
3728 if (!nfs_file_lock_conflict(newnflp
, nflp
, NULL
))
3731 msleep(nflp
, &np
->n_openlock
, slpflag
, "nfs_advlock_setlock_blocked", &ts
);
3733 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3734 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3735 /* looks like we have a recover pending... restart */
3737 lck_mtx_unlock(&np
->n_openlock
);
3738 nfs_mount_state_in_use_end(nmp
, 0);
3740 lck_mtx_lock(&np
->n_openlock
);
3743 if (!error
&& (np
->n_flag
& NREVOKE
))
3745 } while (!error
&& nfs_file_lock_conflict(newnflp
, nflp
, NULL
));
3746 nflp
->nfl_blockcnt
--;
3747 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) && !nflp
->nfl_blockcnt
) {
3748 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3749 nfs_file_lock_destroy(nflp
);
3751 if (error
|| restart
)
3753 /* We have released n_openlock and we can't trust that nextnflp is still valid. */
3754 /* So, start this lock-scanning loop over from where it started. */
3755 nextnflp
= TAILQ_NEXT(newnflp
, nfl_link
);
3757 lck_mtx_unlock(&np
->n_openlock
);
3765 * It looks like this operation is splitting a lock.
3766 * We allocate a new lock now so we don't have to worry
3767 * about the allocation failing after we've updated some state.
3769 nflp2
= nfs_file_lock_alloc(nlop
);
3776 /* once scan for local conflicts is clear, send request to server */
3777 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
3782 /* do we have a delegation? (that we're not returning?) */
3783 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3784 if (np
->n_openflags
& N_DELEG_WRITE
) {
3785 /* with a write delegation, just take the lock delegated */
3786 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DELEGATED
;
3788 /* make sure the lock owner knows its open owner */
3789 if (!nlop
->nlo_open_owner
) {
3790 nfs_open_owner_ref(nofp
->nof_owner
);
3791 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3796 * If we don't have any non-delegated opens but we do have
3797 * delegated opens, then we need to first claim the delegated
3798 * opens so that the lock request on the server can be associated
3799 * with an open it knows about.
3801 if ((!nofp
->nof_rw_drw
&& !nofp
->nof_w_drw
&& !nofp
->nof_r_drw
&&
3802 !nofp
->nof_rw_dw
&& !nofp
->nof_w_dw
&& !nofp
->nof_r_dw
&&
3803 !nofp
->nof_rw
&& !nofp
->nof_w
&& !nofp
->nof_r
) &&
3804 (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
3805 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
3806 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)) {
3807 error
= nfs4_claim_delegated_state_for_open_file(nofp
, 0);
3813 if (np
->n_flag
& NREVOKE
)
3816 error
= nmp
->nm_funcs
->nf_setlock_rpc(np
, nofp
, newnflp
, 0, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3817 if (!error
|| ((error
!= NFSERR_DENIED
) && (error
!= NFSERR_GRACE
)))
3819 /* request was denied due to either conflict or grace period */
3820 if ((error
== NFSERR_DENIED
) && !(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3825 /* release any currently held shared lock before sleeping */
3826 nfs_open_state_clear_busy(np
);
3828 nfs_mount_state_in_use_end(nmp
, 0);
3830 error2
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3833 error2
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3836 error2
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
));
3845 * Wait a little bit and send the request again.
3846 * Except for retries of blocked v2/v3 request where we've already waited a bit.
3848 if ((nmp
->nm_vers
>= NFS_VER4
) || (error
== NFSERR_GRACE
)) {
3849 if (error
== NFSERR_GRACE
)
3853 tsleep(newnflp
, slpflag
, "nfs_advlock_setlock_delay", delay
* (hz
/2));
3856 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3857 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3858 /* looks like we have a recover pending... restart */
3859 nfs_open_state_clear_busy(np
);
3861 nfs_mount_state_in_use_end(nmp
, 0);
3865 if (!error
&& (np
->n_flag
& NREVOKE
))
3870 if (nfs_mount_state_error_should_restart(error
)) {
3871 /* looks like we need to restart this operation */
3873 nfs_open_state_clear_busy(np
);
3877 nfs_mount_state_in_use_end(nmp
, error
);
3882 lck_mtx_lock(&np
->n_openlock
);
3883 newnflp
->nfl_flags
&= ~NFS_FILE_LOCK_BLOCKED
;
3885 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3886 if (newnflp
->nfl_blockcnt
) {
3887 /* wake up anyone blocked on this lock */
3890 /* remove newnflp from lock list and destroy */
3892 TAILQ_REMOVE(&np
->n_locks
, newnflp
, nfl_link
);
3893 nfs_file_lock_destroy(newnflp
);
3895 lck_mtx_unlock(&np
->n_openlock
);
3897 nfs_open_state_clear_busy(np
);
3899 nfs_mount_state_in_use_end(nmp
, error
);
3901 nfs_file_lock_destroy(nflp2
);
3905 /* server granted the lock */
3908 * Scan for locks to update.
3910 * Locks completely covered are killed.
3911 * At most two locks may need to be clipped.
3912 * It's possible that a single lock may need to be split.
3914 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3915 if (nflp
== newnflp
)
3917 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3919 if (nflp
->nfl_owner
!= nlop
)
3921 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))
3923 if ((newnflp
->nfl_start
> nflp
->nfl_end
) || (newnflp
->nfl_end
< nflp
->nfl_start
))
3925 /* here's one to update */
3926 if ((newnflp
->nfl_start
<= nflp
->nfl_start
) && (newnflp
->nfl_end
>= nflp
->nfl_end
)) {
3927 /* The entire lock is being replaced. */
3928 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3929 lck_mtx_lock(&nlop
->nlo_lock
);
3930 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3931 lck_mtx_unlock(&nlop
->nlo_lock
);
3932 /* lock will be destroyed below, if no waiters */
3933 } else if ((newnflp
->nfl_start
> nflp
->nfl_start
) && (newnflp
->nfl_end
< nflp
->nfl_end
)) {
3934 /* We're replacing a range in the middle of a lock. */
3935 /* The current lock will be split into two locks. */
3936 /* Update locks and insert new lock after current lock. */
3937 nflp2
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
3938 nflp2
->nfl_type
= nflp
->nfl_type
;
3939 nflp2
->nfl_start
= newnflp
->nfl_end
+ 1;
3940 nflp2
->nfl_end
= nflp
->nfl_end
;
3941 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3942 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, nflp2
, nfl_link
);
3943 nfs_lock_owner_insert_held_lock(nlop
, nflp2
);
3946 } else if (newnflp
->nfl_start
> nflp
->nfl_start
) {
3947 /* We're replacing the end of a lock. */
3948 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3949 } else if (newnflp
->nfl_end
< nflp
->nfl_end
) {
3950 /* We're replacing the start of a lock. */
3951 nflp
->nfl_start
= newnflp
->nfl_end
+ 1;
3953 if (nflp
->nfl_blockcnt
) {
3954 /* wake up anyone blocked on this lock */
3956 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
3957 /* remove nflp from lock list and destroy */
3958 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3959 nfs_file_lock_destroy(nflp
);
3963 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
3966 * POSIX locks should be coalesced when possible.
3968 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) && (nofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)) {
3970 * Walk through the lock queue and check each of our held locks with
3971 * the previous and next locks in the lock owner's "held lock list".
3972 * If the two locks can be coalesced, we merge the current lock into
3973 * the other (previous or next) lock. Merging this way makes sure that
3974 * lock ranges are always merged forward in the lock queue. This is
3975 * important because anyone blocked on the lock being "merged away"
3976 * will still need to block on that range and it will simply continue
3977 * checking locks that are further down the list.
3979 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3980 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3982 if (nflp
->nfl_owner
!= nlop
)
3984 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_POSIX
)
3986 if (((coalnflp
= TAILQ_PREV(nflp
, nfs_file_lock_queue
, nfl_lolink
))) &&
3987 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3988 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3989 (coalnflp
->nfl_end
== (nflp
->nfl_start
- 1))) {
3990 coalnflp
->nfl_end
= nflp
->nfl_end
;
3991 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3992 lck_mtx_lock(&nlop
->nlo_lock
);
3993 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3994 lck_mtx_unlock(&nlop
->nlo_lock
);
3995 } else if (((coalnflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
3996 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3997 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3998 (coalnflp
->nfl_start
== (nflp
->nfl_end
+ 1))) {
3999 coalnflp
->nfl_start
= nflp
->nfl_start
;
4000 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
4001 lck_mtx_lock(&nlop
->nlo_lock
);
4002 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
4003 lck_mtx_unlock(&nlop
->nlo_lock
);
4005 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
4007 if (nflp
->nfl_blockcnt
) {
4008 /* wake up anyone blocked on this lock */
4011 /* remove nflp from lock list and destroy */
4012 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
4013 nfs_file_lock_destroy(nflp
);
4018 lck_mtx_unlock(&np
->n_openlock
);
4019 nfs_open_state_clear_busy(np
);
4020 nfs_mount_state_in_use_end(nmp
, error
);
4023 nfs_file_lock_destroy(nflp2
);
4028 * Release all (same style) locks within the given range.
4033 struct nfs_open_file
*nofp
,
4034 struct nfs_lock_owner
*nlop
,
4040 struct nfsmount
*nmp
;
4041 struct nfs_file_lock
*nflp
, *nextnflp
, *newnflp
= NULL
;
4042 int error
= 0, willsplit
= 0, send_unlock_rpcs
= 1;
4045 if (nfs_mount_gone(nmp
))
4049 if ((error
= nfs_mount_state_in_use_start(nmp
, NULL
)))
4051 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
4052 nfs_mount_state_in_use_end(nmp
, 0);
4053 error
= nfs4_reopen(nofp
, NULL
);
4058 if ((error
= nfs_open_state_set_busy(np
, NULL
))) {
4059 nfs_mount_state_in_use_end(nmp
, error
);
4063 lck_mtx_lock(&np
->n_openlock
);
4064 if ((start
> 0) && (end
< UINT64_MAX
) && !willsplit
) {
4066 * We may need to allocate a new lock if an existing lock gets split.
4067 * So, we first scan the list to check for a split, and if there's
4068 * going to be one, we'll allocate one now.
4070 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
4071 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
4073 if (nflp
->nfl_owner
!= nlop
)
4075 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
4077 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
4079 if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
4085 lck_mtx_unlock(&np
->n_openlock
);
4086 nfs_open_state_clear_busy(np
);
4087 nfs_mount_state_in_use_end(nmp
, 0);
4088 newnflp
= nfs_file_lock_alloc(nlop
);
4096 * Free all of our locks in the given range.
4098 * Note that this process requires sending requests to the server.
4099 * Because of this, we will release the n_openlock while performing
4100 * the unlock RPCs. The N_OPENBUSY state keeps the state of *held*
4101 * locks from changing underneath us. However, other entries in the
4102 * list may be removed. So we need to be careful walking the list.
4106 * Don't unlock ranges that are held by other-style locks.
4107 * If style is posix, don't send any unlock rpcs if flock is held.
4108 * If we unlock an flock, don't send unlock rpcs for any posix-style
4109 * ranges held - instead send unlocks for the ranges not held.
4111 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) &&
4112 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4113 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
))
4114 send_unlock_rpcs
= 0;
4115 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) &&
4116 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4117 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) &&
4118 ((nflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
4119 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
)) {
4121 int type
= TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_type
;
4122 int delegated
= (TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_flags
& NFS_FILE_LOCK_DELEGATED
);
4123 while (!delegated
&& nflp
) {
4124 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) {
4125 /* unlock the range preceding this lock */
4126 lck_mtx_unlock(&np
->n_openlock
);
4127 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, nflp
->nfl_start
-1, 0,
4128 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4129 if (nfs_mount_state_error_should_restart(error
)) {
4130 nfs_open_state_clear_busy(np
);
4131 nfs_mount_state_in_use_end(nmp
, error
);
4134 lck_mtx_lock(&np
->n_openlock
);
4137 s
= nflp
->nfl_end
+1;
4139 nflp
= TAILQ_NEXT(nflp
, nfl_lolink
);
4142 lck_mtx_unlock(&np
->n_openlock
);
4143 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, end
, 0,
4144 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4145 if (nfs_mount_state_error_should_restart(error
)) {
4146 nfs_open_state_clear_busy(np
);
4147 nfs_mount_state_in_use_end(nmp
, error
);
4150 lck_mtx_lock(&np
->n_openlock
);
4154 send_unlock_rpcs
= 0;
4157 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
4158 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
4160 if (nflp
->nfl_owner
!= nlop
)
4162 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
4164 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
4166 /* here's one to unlock */
4167 if ((start
<= nflp
->nfl_start
) && (end
>= nflp
->nfl_end
)) {
4168 /* The entire lock is being unlocked. */
4169 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4170 lck_mtx_unlock(&np
->n_openlock
);
4171 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, nflp
->nfl_end
, 0,
4172 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4173 if (nfs_mount_state_error_should_restart(error
)) {
4174 nfs_open_state_clear_busy(np
);
4175 nfs_mount_state_in_use_end(nmp
, error
);
4178 lck_mtx_lock(&np
->n_openlock
);
4180 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4183 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
4184 lck_mtx_lock(&nlop
->nlo_lock
);
4185 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
4186 lck_mtx_unlock(&nlop
->nlo_lock
);
4187 /* lock will be destroyed below, if no waiters */
4188 } else if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
4189 /* We're unlocking a range in the middle of a lock. */
4190 /* The current lock will be split into two locks. */
4191 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4192 lck_mtx_unlock(&np
->n_openlock
);
4193 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, end
, 0,
4194 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4195 if (nfs_mount_state_error_should_restart(error
)) {
4196 nfs_open_state_clear_busy(np
);
4197 nfs_mount_state_in_use_end(nmp
, error
);
4200 lck_mtx_lock(&np
->n_openlock
);
4204 /* update locks and insert new lock after current lock */
4205 newnflp
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
4206 newnflp
->nfl_type
= nflp
->nfl_type
;
4207 newnflp
->nfl_start
= end
+ 1;
4208 newnflp
->nfl_end
= nflp
->nfl_end
;
4209 nflp
->nfl_end
= start
- 1;
4210 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, newnflp
, nfl_link
);
4211 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
4214 } else if (start
> nflp
->nfl_start
) {
4215 /* We're unlocking the end of a lock. */
4216 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4217 lck_mtx_unlock(&np
->n_openlock
);
4218 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, nflp
->nfl_end
, 0,
4219 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4220 if (nfs_mount_state_error_should_restart(error
)) {
4221 nfs_open_state_clear_busy(np
);
4222 nfs_mount_state_in_use_end(nmp
, error
);
4225 lck_mtx_lock(&np
->n_openlock
);
4227 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4230 nflp
->nfl_end
= start
- 1;
4231 } else if (end
< nflp
->nfl_end
) {
4232 /* We're unlocking the start of a lock. */
4233 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4234 lck_mtx_unlock(&np
->n_openlock
);
4235 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, end
, 0,
4236 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4237 if (nfs_mount_state_error_should_restart(error
)) {
4238 nfs_open_state_clear_busy(np
);
4239 nfs_mount_state_in_use_end(nmp
, error
);
4242 lck_mtx_lock(&np
->n_openlock
);
4244 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4247 nflp
->nfl_start
= end
+ 1;
4249 if (nflp
->nfl_blockcnt
) {
4250 /* wake up anyone blocked on this lock */
4252 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
4253 /* remove nflp from lock list and destroy */
4254 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
4255 nfs_file_lock_destroy(nflp
);
4259 lck_mtx_unlock(&np
->n_openlock
);
4260 nfs_open_state_clear_busy(np
);
4261 nfs_mount_state_in_use_end(nmp
, 0);
4264 nfs_file_lock_destroy(newnflp
);
4269 * NFSv4 advisory file locking
4273 struct vnop_advlock_args
/* {
4274 struct vnodeop_desc *a_desc;
4280 vfs_context_t a_context;
4283 vnode_t vp
= ap
->a_vp
;
4284 nfsnode_t np
= VTONFS(ap
->a_vp
);
4285 struct flock
*fl
= ap
->a_fl
;
4287 int flags
= ap
->a_flags
;
4288 vfs_context_t ctx
= ap
->a_context
;
4289 struct nfsmount
*nmp
;
4290 struct nfs_open_owner
*noop
= NULL
;
4291 struct nfs_open_file
*nofp
= NULL
;
4292 struct nfs_lock_owner
*nlop
= NULL
;
4294 uint64_t start
, end
;
4295 int error
= 0, modified
, style
;
4297 #define OFF_MAX QUAD_MAX
4299 nmp
= VTONMP(ap
->a_vp
);
4300 if (nfs_mount_gone(nmp
))
4302 lck_mtx_lock(&nmp
->nm_lock
);
4303 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
)) {
4304 lck_mtx_unlock(&nmp
->nm_lock
);
4307 lck_mtx_unlock(&nmp
->nm_lock
);
4309 if (np
->n_flag
& NREVOKE
)
4311 vtype
= vnode_vtype(ap
->a_vp
);
4312 if (vtype
== VDIR
) /* ignore lock requests on directories */
4314 if (vtype
!= VREG
) /* anything other than regular files is invalid */
4317 /* Convert the flock structure into a start and end. */
4318 switch (fl
->l_whence
) {
4322 * Caller is responsible for adding any necessary offset
4323 * to fl->l_start when SEEK_CUR is used.
4325 lstart
= fl
->l_start
;
4328 /* need to flush, and refetch attributes to make */
4329 /* sure we have the correct end of file offset */
4330 if ((error
= nfs_node_lock(np
)))
4332 modified
= (np
->n_flag
& NMODIFIED
);
4333 nfs_node_unlock(np
);
4334 if (modified
&& ((error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1))))
4336 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
4338 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
4339 if ((np
->n_size
> OFF_MAX
) ||
4340 ((fl
->l_start
> 0) && (np
->n_size
> (u_quad_t
)(OFF_MAX
- fl
->l_start
))))
4342 lstart
= np
->n_size
+ fl
->l_start
;
4343 nfs_data_unlock(np
);
4353 if (fl
->l_len
== 0) {
4355 } else if (fl
->l_len
> 0) {
4356 if ((fl
->l_len
- 1) > (OFF_MAX
- lstart
))
4358 end
= start
- 1 + fl
->l_len
;
4359 } else { /* l_len is negative */
4360 if ((lstart
+ fl
->l_len
) < 0)
4365 if ((nmp
->nm_vers
== NFS_VER2
) && ((start
> INT32_MAX
) || (fl
->l_len
&& (end
> INT32_MAX
))))
4368 style
= (flags
& F_FLOCK
) ? NFS_FILE_LOCK_STYLE_FLOCK
: NFS_FILE_LOCK_STYLE_POSIX
;
4369 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && ((start
!= 0) || (end
!= UINT64_MAX
)))
4372 /* find the lock owner, alloc if not unlock */
4373 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), (op
!= F_UNLCK
));
4375 error
= (op
== F_UNLCK
) ? 0 : ENOMEM
;
4377 NP(np
, "nfs_vnop_advlock: no lock owner, error %d", error
);
4381 if (op
== F_GETLK
) {
4382 error
= nfs_advlock_getlock(np
, nlop
, fl
, start
, end
, ctx
);
4384 /* find the open owner */
4385 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
4387 NP(np
, "nfs_vnop_advlock: no open owner %d", kauth_cred_getuid(vfs_context_ucred(ctx
)));
4391 /* find the open file */
4393 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
4396 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
4397 NP(np
, "nfs_vnop_advlock: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
4400 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4401 error
= nfs4_reopen(nofp
, ((op
== F_UNLCK
) ? NULL
: vfs_context_thread(ctx
)));
4407 NP(np
, "nfs_vnop_advlock: no open file %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
4410 if (op
== F_UNLCK
) {
4411 error
= nfs_advlock_unlock(np
, nofp
, nlop
, start
, end
, style
, ctx
);
4412 } else if ((op
== F_SETLK
) || (op
== F_SETLKW
)) {
4413 if ((op
== F_SETLK
) && (flags
& F_WAIT
))
4415 error
= nfs_advlock_setlock(np
, nofp
, nlop
, op
, start
, end
, style
, fl
->l_type
, ctx
);
4417 /* not getlk, unlock or lock? */
4424 nfs_lock_owner_rele(nlop
);
4426 nfs_open_owner_rele(noop
);
4431 * Check if an open owner holds any locks on a file.
4434 nfs_check_for_locks(struct nfs_open_owner
*noop
, struct nfs_open_file
*nofp
)
4436 struct nfs_lock_owner
*nlop
;
4438 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
4439 if (nlop
->nlo_open_owner
!= noop
)
4441 if (!TAILQ_EMPTY(&nlop
->nlo_locks
))
4444 return (nlop
? 1 : 0);
4448 * Reopen simple (no deny, no locks) open state that was lost.
4451 nfs4_reopen(struct nfs_open_file
*nofp
, thread_t thd
)
4453 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4454 struct nfsmount
*nmp
= NFSTONMP(nofp
->nof_np
);
4455 nfsnode_t np
= nofp
->nof_np
;
4456 vnode_t vp
= NFSTOV(np
);
4458 struct componentname cn
;
4459 const char *vname
= NULL
;
4460 const char *name
= NULL
;
4462 char smallname
[128];
4463 char *filename
= NULL
;
4464 int error
= 0, done
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
4465 struct timespec ts
= { 1, 0 };
4467 lck_mtx_lock(&nofp
->nof_lock
);
4468 while (nofp
->nof_flags
& NFS_OPEN_FILE_REOPENING
) {
4469 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
4471 msleep(&nofp
->nof_flags
, &nofp
->nof_lock
, slpflag
|(PZERO
-1), "nfsreopenwait", &ts
);
4474 if (error
|| !(nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4475 lck_mtx_unlock(&nofp
->nof_lock
);
4478 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPENING
;
4479 lck_mtx_unlock(&nofp
->nof_lock
);
4481 nfs_node_lock_force(np
);
4482 if ((vnode_vtype(vp
) != VDIR
) && np
->n_sillyrename
) {
4484 * The node's been sillyrenamed, so we need to use
4485 * the sillyrename directory/name to do the open.
4487 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
4488 dvp
= NFSTOV(nsp
->nsr_dnp
);
4489 if ((error
= vnode_get(dvp
))) {
4490 nfs_node_unlock(np
);
4493 name
= nsp
->nsr_name
;
4496 * [sigh] We can't trust VFS to get the parent right for named
4497 * attribute nodes. (It likes to reparent the nodes after we've
4498 * created them.) Luckily we can probably get the right parent
4499 * from the n_parent we have stashed away.
4501 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
4502 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
4505 dvp
= vnode_getparent(vp
);
4506 vname
= vnode_getname(vp
);
4507 if (!dvp
|| !vname
) {
4510 nfs_node_unlock(np
);
4515 filename
= &smallname
[0];
4516 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
4517 if (namelen
>= sizeof(smallname
)) {
4518 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
4523 snprintf(filename
, namelen
+1, "%s", name
);
4525 nfs_node_unlock(np
);
4526 bzero(&cn
, sizeof(cn
));
4527 cn
.cn_nameptr
= filename
;
4528 cn
.cn_namelen
= namelen
;
4532 if ((error
= nfs_mount_state_in_use_start(nmp
, thd
)))
4536 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
);
4537 if (!error
&& nofp
->nof_w
)
4538 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
);
4539 if (!error
&& nofp
->nof_r
)
4540 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
);
4542 if (nfs_mount_state_in_use_end(nmp
, error
)) {
4543 if (error
== NFSERR_GRACE
)
4545 printf("nfs4_reopen: RPC failed, error %d, lost %d, %s\n", error
,
4546 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4552 if (error
&& (error
!= EINTR
) && (error
!= ERESTART
))
4553 nfs_revoke_open_state_for_node(np
);
4554 lck_mtx_lock(&nofp
->nof_lock
);
4555 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPENING
;
4557 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
4559 printf("nfs4_reopen: failed, error %d, lost %d, %s\n", error
,
4560 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4561 lck_mtx_unlock(&nofp
->nof_lock
);
4562 if (filename
&& (filename
!= &smallname
[0]))
4563 FREE(filename
, M_TEMP
);
4565 vnode_putname(vname
);
4572 * Send a normal OPEN RPC to open/create a file.
4576 struct nfs_open_file
*nofp
,
4578 struct componentname
*cnp
,
4579 struct vnode_attr
*vap
,
4586 return (nfs4_open_rpc_internal(nofp
, ctx
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
4587 cnp
, vap
, dvp
, vpp
, create
, share_access
, share_deny
));
4591 * Send an OPEN RPC to reopen a file.
4594 nfs4_open_reopen_rpc(
4595 struct nfs_open_file
*nofp
,
4598 struct componentname
*cnp
,
4604 return (nfs4_open_rpc_internal(nofp
, NULL
, thd
, cred
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, share_access
, share_deny
));
4608 * Send an OPEN_CONFIRM RPC to confirm an OPEN.
4611 nfs4_open_confirm_rpc(
4612 struct nfsmount
*nmp
,
4616 struct nfs_open_owner
*noop
,
4620 struct nfs_vattr
*nvap
,
4623 struct nfsm_chain nmreq
, nmrep
;
4624 int error
= 0, status
, numops
;
4625 struct nfsreq_secinfo_args si
;
4627 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
4628 nfsm_chain_null(&nmreq
);
4629 nfsm_chain_null(&nmrep
);
4631 // PUTFH, OPEN_CONFIRM, GETATTR
4633 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
4634 nfsm_chain_add_compound_header(error
, &nmreq
, "open_confirm", nmp
->nm_minor_vers
, numops
);
4636 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4637 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
4639 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_CONFIRM
);
4640 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
4641 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4643 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4644 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4645 nfsm_chain_build_done(error
, &nmreq
);
4646 nfsm_assert(error
, (numops
== 0), EPROTO
);
4648 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, &nmrep
, xidp
, &status
);
4650 nfsm_chain_skip_tag(error
, &nmrep
);
4651 nfsm_chain_get_32(error
, &nmrep
, numops
);
4652 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4654 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_CONFIRM
);
4655 nfs_owner_seqid_increment(noop
, NULL
, error
);
4656 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4657 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4659 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
4661 nfsm_chain_cleanup(&nmreq
);
4662 nfsm_chain_cleanup(&nmrep
);
4667 * common OPEN RPC code
4669 * If create is set, ctx must be passed in.
4670 * Returns a node on success if no node passed in.
4673 nfs4_open_rpc_internal(
4674 struct nfs_open_file
*nofp
,
4678 struct componentname
*cnp
,
4679 struct vnode_attr
*vap
,
4686 struct nfsmount
*nmp
;
4687 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4688 struct nfs_vattr nvattr
;
4689 int error
= 0, open_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
4690 int nfsvers
, namedattrs
, numops
, exclusive
= 0, gotuid
, gotgid
;
4691 u_int64_t xid
, savedxid
= 0;
4692 nfsnode_t dnp
= VTONFS(dvp
);
4693 nfsnode_t np
, newnp
= NULL
;
4694 vnode_t newvp
= NULL
;
4695 struct nfsm_chain nmreq
, nmrep
;
4696 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
4697 uint32_t rflags
, delegation
, recall
;
4698 struct nfs_stateid stateid
, dstateid
, *sid
;
4700 struct nfsreq rq
, *req
= &rq
;
4701 struct nfs_dulookup dul
;
4703 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
4704 struct kauth_ace ace
;
4705 struct nfsreq_secinfo_args si
;
4711 if (nfs_mount_gone(nmp
))
4713 nfsvers
= nmp
->nm_vers
;
4714 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4715 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
4718 np
= *vpp
? VTONFS(*vpp
) : NULL
;
4719 if (create
&& vap
) {
4720 exclusive
= (vap
->va_vaflags
& VA_EXCLUSIVE
);
4721 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4722 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4723 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4724 if (exclusive
&& (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
)))
4725 vap
->va_vaflags
|= VA_UTIMES_NULL
;
4727 exclusive
= gotuid
= gotgid
= 0;
4730 sid
= &nofp
->nof_stateid
;
4732 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
4736 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
4739 rflags
= delegation
= recall
= 0;
4742 slen
= sizeof(sbuf
);
4743 NVATTR_INIT(&nvattr
);
4744 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, cnp
->cn_nameptr
, cnp
->cn_namelen
);
4746 nfsm_chain_null(&nmreq
);
4747 nfsm_chain_null(&nmrep
);
4749 // PUTFH, SAVEFH, OPEN(CREATE?), GETATTR(FH), RESTOREFH, GETATTR
4751 nfsm_chain_build_alloc_init(error
, &nmreq
, 53 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
4752 nfsm_chain_add_compound_header(error
, &nmreq
, create
? "create" : "open", nmp
->nm_minor_vers
, numops
);
4754 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4755 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4757 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
4759 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
4760 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4761 nfsm_chain_add_32(error
, &nmreq
, share_access
);
4762 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
4763 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
4764 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
4765 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
4766 nfsm_chain_add_32(error
, &nmreq
, create
);
4769 static uint32_t create_verf
; // XXX need a better verifier
4771 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
4772 /* insert 64 bit verifier */
4773 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4774 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4776 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
4777 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
4780 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
4781 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4783 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4784 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
4785 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
4786 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
4788 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
4790 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4791 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4792 nfsm_chain_build_done(error
, &nmreq
);
4793 nfsm_assert(error
, (numops
== 0), EPROTO
);
4795 error
= busyerror
= nfs_node_set_busy(dnp
, thd
);
4798 if (create
&& !namedattrs
)
4799 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4801 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, NULL
, &req
);
4803 if (create
&& !namedattrs
)
4804 nfs_dulookup_start(&dul
, dnp
, ctx
);
4805 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4809 if (create
&& !namedattrs
)
4810 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4812 if ((lockerror
= nfs_node_lock(dnp
)))
4814 nfsm_chain_skip_tag(error
, &nmrep
);
4815 nfsm_chain_get_32(error
, &nmrep
, numops
);
4816 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4817 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
4819 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
4820 nfs_owner_seqid_increment(noop
, NULL
, error
);
4821 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4822 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
4823 nfsm_chain_get_32(error
, &nmrep
, rflags
);
4824 bmlen
= NFS_ATTR_BITMAP_LEN
;
4825 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
4826 nfsm_chain_get_32(error
, &nmrep
, delegation
);
4828 switch (delegation
) {
4829 case NFS_OPEN_DELEGATE_NONE
:
4831 case NFS_OPEN_DELEGATE_READ
:
4832 case NFS_OPEN_DELEGATE_WRITE
:
4833 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
4834 nfsm_chain_get_32(error
, &nmrep
, recall
);
4835 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
4836 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
4837 /* if we have any trouble accepting the ACE, just invalidate it */
4838 ace_type
= ace_flags
= ace_mask
= len
= 0;
4839 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
4840 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
4841 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
4842 nfsm_chain_get_32(error
, &nmrep
, len
);
4843 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
4844 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
4845 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
4846 if (!error
&& (len
>= slen
)) {
4847 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
4854 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
4856 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
4859 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
4864 if (s
&& (s
!= sbuf
))
4871 /* At this point if we have no error, the object was created/opened. */
4874 if (create
&& vap
&& !exclusive
)
4875 nfs_vattr_set_supported(bitmap
, vap
);
4876 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4878 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
4880 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
4881 printf("nfs: open/create didn't return filehandle? %s\n", cnp
->cn_nameptr
);
4885 if (!create
&& np
&& !NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4886 // XXX for the open case, what if fh doesn't match the vnode we think we're opening?
4887 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
4888 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
4889 NP(np
, "nfs4_open_rpc: warning: file handle mismatch");
4891 /* directory attributes: if we don't get them, make sure to invalidate */
4892 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
4893 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4894 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
4896 NATTRINVALIDATE(dnp
);
4899 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
4900 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
4902 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
4903 nfs_node_unlock(dnp
);
4905 NVATTR_CLEANUP(&nvattr
);
4906 error
= nfs4_open_confirm_rpc(nmp
, dnp
, fh
.fh_data
, fh
.fh_len
, noop
, sid
, thd
, cred
, &nvattr
, &xid
);
4909 if ((lockerror
= nfs_node_lock(dnp
)))
4914 nfsm_chain_cleanup(&nmreq
);
4915 nfsm_chain_cleanup(&nmrep
);
4917 if (!lockerror
&& create
) {
4918 if (!open_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
4919 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4920 cache_purge_negatives(dvp
);
4922 dnp
->n_flag
|= NMODIFIED
;
4923 nfs_node_unlock(dnp
);
4925 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4928 nfs_node_unlock(dnp
);
4929 if (!error
&& !np
&& fh
.fh_len
) {
4930 /* create the vnode with the filehandle and attributes */
4932 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &newnp
);
4934 newvp
= NFSTOV(newnp
);
4936 NVATTR_CLEANUP(&nvattr
);
4938 nfs_node_clear_busy(dnp
);
4939 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
4942 if (!error
&& np
&& !recall
) {
4943 /* stuff the delegation state in the node */
4944 lck_mtx_lock(&np
->n_openlock
);
4945 np
->n_openflags
&= ~N_DELEG_MASK
;
4946 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4947 np
->n_dstateid
= dstateid
;
4949 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4950 lck_mtx_lock(&nmp
->nm_lock
);
4951 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4952 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4953 lck_mtx_unlock(&nmp
->nm_lock
);
4955 lck_mtx_unlock(&np
->n_openlock
);
4957 /* give the delegation back */
4959 if (NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4960 /* update delegation state and return it */
4961 lck_mtx_lock(&np
->n_openlock
);
4962 np
->n_openflags
&= ~N_DELEG_MASK
;
4963 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4964 np
->n_dstateid
= dstateid
;
4966 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4967 lck_mtx_lock(&nmp
->nm_lock
);
4968 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4969 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4970 lck_mtx_unlock(&nmp
->nm_lock
);
4972 lck_mtx_unlock(&np
->n_openlock
);
4973 /* don't need to send a separate delegreturn for fh */
4976 /* return np's current delegation */
4977 nfs4_delegation_return(np
, 0, thd
, cred
);
4979 if (fh
.fh_len
) /* return fh's delegation if it wasn't for np */
4980 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
4984 if (exclusive
&& (error
== NFSERR_NOTSUPP
)) {
4989 nfs_node_unlock(newnp
);
4992 } else if (create
) {
4993 nfs_node_unlock(newnp
);
4995 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
4996 if (error
&& (gotuid
|| gotgid
)) {
4997 /* it's possible the server didn't like our attempt to set IDs. */
4998 /* so, let's try it again without those */
4999 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
5000 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
5001 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
5009 nfs_open_owner_clear_busy(noop
);
5015 * Send an OPEN RPC to claim a delegated open for a file
5018 nfs4_claim_delegated_open_rpc(
5019 struct nfs_open_file
*nofp
,
5024 struct nfsmount
*nmp
;
5025 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5026 struct nfs_vattr nvattr
;
5027 int error
= 0, lockerror
= ENOENT
, status
;
5028 int nfsvers
, numops
;
5030 nfsnode_t np
= nofp
->nof_np
;
5031 struct nfsm_chain nmreq
, nmrep
;
5032 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
5033 uint32_t rflags
= 0, delegation
, recall
= 0;
5035 struct nfs_stateid dstateid
;
5036 char sbuf
[64], *s
= sbuf
;
5037 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
5038 struct kauth_ace ace
;
5040 const char *vname
= NULL
;
5041 const char *name
= NULL
;
5043 char smallname
[128];
5044 char *filename
= NULL
;
5045 struct nfsreq_secinfo_args si
;
5048 if (nfs_mount_gone(nmp
))
5050 nfsvers
= nmp
->nm_vers
;
5052 nfs_node_lock_force(np
);
5053 if ((vnode_vtype(NFSTOV(np
)) != VDIR
) && np
->n_sillyrename
) {
5055 * The node's been sillyrenamed, so we need to use
5056 * the sillyrename directory/name to do the open.
5058 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
5059 dvp
= NFSTOV(nsp
->nsr_dnp
);
5060 if ((error
= vnode_get(dvp
))) {
5061 nfs_node_unlock(np
);
5064 name
= nsp
->nsr_name
;
5067 * [sigh] We can't trust VFS to get the parent right for named
5068 * attribute nodes. (It likes to reparent the nodes after we've
5069 * created them.) Luckily we can probably get the right parent
5070 * from the n_parent we have stashed away.
5072 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
5073 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
5076 dvp
= vnode_getparent(NFSTOV(np
));
5077 vname
= vnode_getname(NFSTOV(np
));
5078 if (!dvp
|| !vname
) {
5081 nfs_node_unlock(np
);
5086 filename
= &smallname
[0];
5087 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
5088 if (namelen
>= sizeof(smallname
)) {
5089 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
5092 nfs_node_unlock(np
);
5095 snprintf(filename
, namelen
+1, "%s", name
);
5097 nfs_node_unlock(np
);
5099 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5101 NVATTR_INIT(&nvattr
);
5102 delegation
= NFS_OPEN_DELEGATE_NONE
;
5103 dstateid
= np
->n_dstateid
;
5104 NFSREQ_SECINFO_SET(&si
, VTONFS(dvp
), NULL
, 0, filename
, namelen
);
5106 nfsm_chain_null(&nmreq
);
5107 nfsm_chain_null(&nmrep
);
5109 // PUTFH, OPEN, GETATTR(FH)
5111 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5112 nfsm_chain_add_compound_header(error
, &nmreq
, "open_claim_d", nmp
->nm_minor_vers
, numops
);
5114 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5115 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, VTONFS(dvp
)->n_fhp
, VTONFS(dvp
)->n_fhsize
);
5117 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5118 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5119 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5120 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5121 // open owner: clientid + uid
5122 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5123 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5124 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5126 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5128 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_DELEGATE_CUR
);
5129 nfsm_chain_add_stateid(error
, &nmreq
, &np
->n_dstateid
);
5130 nfsm_chain_add_name(error
, &nmreq
, filename
, namelen
, nmp
);
5132 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5133 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5134 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5135 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5136 nfsm_chain_build_done(error
, &nmreq
);
5137 nfsm_assert(error
, (numops
== 0), EPROTO
);
5140 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5141 noop
->noo_cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5143 if ((lockerror
= nfs_node_lock(np
)))
5145 nfsm_chain_skip_tag(error
, &nmrep
);
5146 nfsm_chain_get_32(error
, &nmrep
, numops
);
5147 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5149 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5150 nfs_owner_seqid_increment(noop
, NULL
, error
);
5151 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5152 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5153 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5154 bmlen
= NFS_ATTR_BITMAP_LEN
;
5155 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5156 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5158 switch (delegation
) {
5159 case NFS_OPEN_DELEGATE_NONE
:
5160 // if (!(np->n_openflags & N_DELEG_RETURN)) /* don't warn if delegation is being returned */
5161 // printf("nfs: open delegated claim didn't return a delegation %s\n", filename ? filename : "???");
5163 case NFS_OPEN_DELEGATE_READ
:
5164 case NFS_OPEN_DELEGATE_WRITE
:
5165 if ((((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_READ
) &&
5166 (delegation
== NFS_OPEN_DELEGATE_WRITE
)) ||
5167 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) &&
5168 (delegation
== NFS_OPEN_DELEGATE_READ
)))
5169 printf("nfs: open delegated claim returned a different delegation type! have %s got %s %s\n",
5170 ((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ? "W" : "R",
5171 (delegation
== NFS_OPEN_DELEGATE_WRITE
) ? "W" : "R", filename
? filename
: "???");
5172 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5173 nfsm_chain_get_32(error
, &nmrep
, recall
);
5174 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5175 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5176 /* if we have any trouble accepting the ACE, just invalidate it */
5177 ace_type
= ace_flags
= ace_mask
= len
= 0;
5178 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5179 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5180 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5181 nfsm_chain_get_32(error
, &nmrep
, len
);
5182 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5183 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5184 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5185 if (!error
&& (len
>= slen
)) {
5186 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5193 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5195 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5198 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5203 if (s
&& (s
!= sbuf
))
5206 /* stuff the latest delegation state in the node */
5207 lck_mtx_lock(&np
->n_openlock
);
5208 np
->n_openflags
&= ~N_DELEG_MASK
;
5209 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5210 np
->n_dstateid
= dstateid
;
5212 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5213 lck_mtx_lock(&nmp
->nm_lock
);
5214 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5215 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5216 lck_mtx_unlock(&nmp
->nm_lock
);
5218 lck_mtx_unlock(&np
->n_openlock
);
5226 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5227 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5229 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5230 printf("nfs: open reclaim didn't return filehandle? %s\n", filename
? filename
: "???");
5234 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5235 // XXX what if fh doesn't match the vnode we think we're re-opening?
5236 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5237 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5238 printf("nfs4_claim_delegated_open_rpc: warning: file handle mismatch %s\n", filename
? filename
: "???");
5240 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5242 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5243 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5245 NVATTR_CLEANUP(&nvattr
);
5246 nfsm_chain_cleanup(&nmreq
);
5247 nfsm_chain_cleanup(&nmrep
);
5249 nfs_node_unlock(np
);
5250 nfs_open_owner_clear_busy(noop
);
5251 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5254 * We're making a delegated claim.
5255 * Don't return the delegation here in case we have more to claim.
5256 * Just make sure it's queued up to be returned.
5258 nfs4_delegation_return_enqueue(np
);
5263 // printf("nfs: open claim delegated (%d, %d) succeeded for %s\n", share_access, share_deny, filename ? filename : "???");
5264 if (filename
&& (filename
!= &smallname
[0]))
5265 FREE(filename
, M_TEMP
);
5267 vnode_putname(vname
);
5274 * Send an OPEN RPC to reclaim an open file.
5277 nfs4_open_reclaim_rpc(
5278 struct nfs_open_file
*nofp
,
5282 struct nfsmount
*nmp
;
5283 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5284 struct nfs_vattr nvattr
;
5285 int error
= 0, lockerror
= ENOENT
, status
;
5286 int nfsvers
, numops
;
5288 nfsnode_t np
= nofp
->nof_np
;
5289 struct nfsm_chain nmreq
, nmrep
;
5290 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
5291 uint32_t rflags
= 0, delegation
, recall
= 0;
5293 struct nfs_stateid dstateid
;
5294 char sbuf
[64], *s
= sbuf
;
5295 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
5296 struct kauth_ace ace
;
5297 struct nfsreq_secinfo_args si
;
5300 if (nfs_mount_gone(nmp
))
5302 nfsvers
= nmp
->nm_vers
;
5304 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5307 NVATTR_INIT(&nvattr
);
5308 delegation
= NFS_OPEN_DELEGATE_NONE
;
5309 dstateid
= np
->n_dstateid
;
5310 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5312 nfsm_chain_null(&nmreq
);
5313 nfsm_chain_null(&nmrep
);
5315 // PUTFH, OPEN, GETATTR(FH)
5317 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5318 nfsm_chain_add_compound_header(error
, &nmreq
, "open_reclaim", nmp
->nm_minor_vers
, numops
);
5320 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5321 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5323 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5324 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5325 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5326 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5327 // open owner: clientid + uid
5328 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5329 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5330 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5332 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5334 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_PREVIOUS
);
5335 delegation
= (np
->n_openflags
& N_DELEG_READ
) ? NFS_OPEN_DELEGATE_READ
:
5336 (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
:
5337 NFS_OPEN_DELEGATE_NONE
;
5338 nfsm_chain_add_32(error
, &nmreq
, delegation
);
5339 delegation
= NFS_OPEN_DELEGATE_NONE
;
5341 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5342 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5343 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5344 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5345 nfsm_chain_build_done(error
, &nmreq
);
5346 nfsm_assert(error
, (numops
== 0), EPROTO
);
5349 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5350 noop
->noo_cred
, &si
, R_RECOVER
|R_NOINTR
, &nmrep
, &xid
, &status
);
5352 if ((lockerror
= nfs_node_lock(np
)))
5354 nfsm_chain_skip_tag(error
, &nmrep
);
5355 nfsm_chain_get_32(error
, &nmrep
, numops
);
5356 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5358 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5359 nfs_owner_seqid_increment(noop
, NULL
, error
);
5360 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5361 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5362 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5363 bmlen
= NFS_ATTR_BITMAP_LEN
;
5364 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5365 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5367 switch (delegation
) {
5368 case NFS_OPEN_DELEGATE_NONE
:
5369 if (np
->n_openflags
& N_DELEG_MASK
) {
5371 * Hey! We were supposed to get our delegation back even
5372 * if it was getting immediately recalled. Bad server!
5374 * Just try to return the existing delegation.
5376 // NP(np, "nfs: open reclaim didn't return delegation?");
5377 delegation
= (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
: NFS_OPEN_DELEGATE_READ
;
5381 case NFS_OPEN_DELEGATE_READ
:
5382 case NFS_OPEN_DELEGATE_WRITE
:
5383 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5384 nfsm_chain_get_32(error
, &nmrep
, recall
);
5385 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5386 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5387 /* if we have any trouble accepting the ACE, just invalidate it */
5388 ace_type
= ace_flags
= ace_mask
= len
= 0;
5389 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5390 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5391 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5392 nfsm_chain_get_32(error
, &nmrep
, len
);
5393 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5394 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5395 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5396 if (!error
&& (len
>= slen
)) {
5397 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5404 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5406 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5409 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5414 if (s
&& (s
!= sbuf
))
5417 /* stuff the delegation state in the node */
5418 lck_mtx_lock(&np
->n_openlock
);
5419 np
->n_openflags
&= ~N_DELEG_MASK
;
5420 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5421 np
->n_dstateid
= dstateid
;
5423 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5424 lck_mtx_lock(&nmp
->nm_lock
);
5425 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5426 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5427 lck_mtx_unlock(&nmp
->nm_lock
);
5429 lck_mtx_unlock(&np
->n_openlock
);
5437 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5438 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5440 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5441 NP(np
, "nfs: open reclaim didn't return filehandle?");
5445 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5446 // XXX what if fh doesn't match the vnode we think we're re-opening?
5447 // That should be pretty hard in this case, given that we are doing
5448 // the open reclaim using the file handle (and not a dir/name pair).
5449 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5450 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5451 NP(np
, "nfs4_open_reclaim_rpc: warning: file handle mismatch");
5453 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5455 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5456 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5459 // NP(np, "nfs: open reclaim (%d, %d) succeeded", share_access, share_deny);
5460 NVATTR_CLEANUP(&nvattr
);
5461 nfsm_chain_cleanup(&nmreq
);
5462 nfsm_chain_cleanup(&nmrep
);
5464 nfs_node_unlock(np
);
5465 nfs_open_owner_clear_busy(noop
);
5466 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5468 nfs4_delegation_return_enqueue(np
);
5474 nfs4_open_downgrade_rpc(
5476 struct nfs_open_file
*nofp
,
5479 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5480 struct nfsmount
*nmp
;
5481 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5482 struct nfsm_chain nmreq
, nmrep
;
5484 struct nfsreq_secinfo_args si
;
5487 if (nfs_mount_gone(nmp
))
5489 nfsvers
= nmp
->nm_vers
;
5491 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5494 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5495 nfsm_chain_null(&nmreq
);
5496 nfsm_chain_null(&nmrep
);
5498 // PUTFH, OPEN_DOWNGRADE, GETATTR
5500 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5501 nfsm_chain_add_compound_header(error
, &nmreq
, "open_downgrd", nmp
->nm_minor_vers
, numops
);
5503 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5504 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5506 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_DOWNGRADE
);
5507 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5508 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5509 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_access
);
5510 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_deny
);
5512 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5513 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5514 nfsm_chain_build_done(error
, &nmreq
);
5515 nfsm_assert(error
, (numops
== 0), EPROTO
);
5517 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
5518 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
5519 &si
, R_NOINTR
, &nmrep
, &xid
, &status
);
5521 if ((lockerror
= nfs_node_lock(np
)))
5523 nfsm_chain_skip_tag(error
, &nmrep
);
5524 nfsm_chain_get_32(error
, &nmrep
, numops
);
5525 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5527 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_DOWNGRADE
);
5528 nfs_owner_seqid_increment(noop
, NULL
, error
);
5529 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5530 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5531 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5534 nfs_node_unlock(np
);
5535 nfs_open_owner_clear_busy(noop
);
5536 nfsm_chain_cleanup(&nmreq
);
5537 nfsm_chain_cleanup(&nmrep
);
5544 struct nfs_open_file
*nofp
,
5549 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5550 struct nfsmount
*nmp
;
5551 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5552 struct nfsm_chain nmreq
, nmrep
;
5554 struct nfsreq_secinfo_args si
;
5557 if (nfs_mount_gone(nmp
))
5559 nfsvers
= nmp
->nm_vers
;
5561 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5564 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5565 nfsm_chain_null(&nmreq
);
5566 nfsm_chain_null(&nmrep
);
5568 // PUTFH, CLOSE, GETATTR
5570 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5571 nfsm_chain_add_compound_header(error
, &nmreq
, "close", nmp
->nm_minor_vers
, numops
);
5573 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5574 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5576 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CLOSE
);
5577 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5578 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5580 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5581 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5582 nfsm_chain_build_done(error
, &nmreq
);
5583 nfsm_assert(error
, (numops
== 0), EPROTO
);
5585 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5587 if ((lockerror
= nfs_node_lock(np
)))
5589 nfsm_chain_skip_tag(error
, &nmrep
);
5590 nfsm_chain_get_32(error
, &nmrep
, numops
);
5591 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5593 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CLOSE
);
5594 nfs_owner_seqid_increment(noop
, NULL
, error
);
5595 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5596 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5597 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5600 nfs_node_unlock(np
);
5601 nfs_open_owner_clear_busy(noop
);
5602 nfsm_chain_cleanup(&nmreq
);
5603 nfsm_chain_cleanup(&nmrep
);
5609 * Claim the delegated open combinations this open file holds.
5612 nfs4_claim_delegated_state_for_open_file(struct nfs_open_file
*nofp
, int flags
)
5614 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5615 struct nfs_lock_owner
*nlop
;
5616 struct nfs_file_lock
*nflp
, *nextnflp
;
5617 struct nfsmount
*nmp
;
5618 int error
= 0, reopen
= 0;
5620 if (nofp
->nof_d_rw_drw
) {
5621 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5623 lck_mtx_lock(&nofp
->nof_lock
);
5624 nofp
->nof_rw_drw
+= nofp
->nof_d_rw_drw
;
5625 nofp
->nof_d_rw_drw
= 0;
5626 lck_mtx_unlock(&nofp
->nof_lock
);
5629 if (!error
&& nofp
->nof_d_w_drw
) {
5630 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5632 lck_mtx_lock(&nofp
->nof_lock
);
5633 nofp
->nof_w_drw
+= nofp
->nof_d_w_drw
;
5634 nofp
->nof_d_w_drw
= 0;
5635 lck_mtx_unlock(&nofp
->nof_lock
);
5638 if (!error
&& nofp
->nof_d_r_drw
) {
5639 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5641 lck_mtx_lock(&nofp
->nof_lock
);
5642 nofp
->nof_r_drw
+= nofp
->nof_d_r_drw
;
5643 nofp
->nof_d_r_drw
= 0;
5644 lck_mtx_unlock(&nofp
->nof_lock
);
5647 if (!error
&& nofp
->nof_d_rw_dw
) {
5648 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5650 lck_mtx_lock(&nofp
->nof_lock
);
5651 nofp
->nof_rw_dw
+= nofp
->nof_d_rw_dw
;
5652 nofp
->nof_d_rw_dw
= 0;
5653 lck_mtx_unlock(&nofp
->nof_lock
);
5656 if (!error
&& nofp
->nof_d_w_dw
) {
5657 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5659 lck_mtx_lock(&nofp
->nof_lock
);
5660 nofp
->nof_w_dw
+= nofp
->nof_d_w_dw
;
5661 nofp
->nof_d_w_dw
= 0;
5662 lck_mtx_unlock(&nofp
->nof_lock
);
5665 if (!error
&& nofp
->nof_d_r_dw
) {
5666 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5668 lck_mtx_lock(&nofp
->nof_lock
);
5669 nofp
->nof_r_dw
+= nofp
->nof_d_r_dw
;
5670 nofp
->nof_d_r_dw
= 0;
5671 lck_mtx_unlock(&nofp
->nof_lock
);
5674 /* non-deny-mode opens may be reopened if no locks are held */
5675 if (!error
&& nofp
->nof_d_rw
) {
5676 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5677 /* for some errors, we should just try reopening the file */
5678 if (nfs_mount_state_error_delegation_lost(error
))
5680 if (!error
|| reopen
) {
5681 lck_mtx_lock(&nofp
->nof_lock
);
5682 nofp
->nof_rw
+= nofp
->nof_d_rw
;
5684 lck_mtx_unlock(&nofp
->nof_lock
);
5687 /* if we've already set reopen, we should move these other two opens from delegated to not delegated */
5688 if ((!error
|| reopen
) && nofp
->nof_d_w
) {
5690 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5691 /* for some errors, we should just try reopening the file */
5692 if (nfs_mount_state_error_delegation_lost(error
))
5695 if (!error
|| reopen
) {
5696 lck_mtx_lock(&nofp
->nof_lock
);
5697 nofp
->nof_w
+= nofp
->nof_d_w
;
5699 lck_mtx_unlock(&nofp
->nof_lock
);
5702 if ((!error
|| reopen
) && nofp
->nof_d_r
) {
5704 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5705 /* for some errors, we should just try reopening the file */
5706 if (nfs_mount_state_error_delegation_lost(error
))
5709 if (!error
|| reopen
) {
5710 lck_mtx_lock(&nofp
->nof_lock
);
5711 nofp
->nof_r
+= nofp
->nof_d_r
;
5713 lck_mtx_unlock(&nofp
->nof_lock
);
5719 * Any problems with the delegation probably indicates that we
5720 * should review/return all of our current delegation state.
5722 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5723 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5724 lck_mtx_lock(&nmp
->nm_lock
);
5725 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5726 lck_mtx_unlock(&nmp
->nm_lock
);
5728 if (reopen
&& (nfs_check_for_locks(noop
, nofp
) == 0)) {
5729 /* just reopen the file on next access */
5730 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, need reopen, %d",
5731 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5732 lck_mtx_lock(&nofp
->nof_lock
);
5733 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPEN
;
5734 lck_mtx_unlock(&nofp
->nof_lock
);
5738 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, locks prevent reopen, %d",
5739 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5742 if (!error
&& ((nmp
= NFSTONMP(nofp
->nof_np
)))) {
5743 /* claim delegated locks */
5744 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
5745 if (nlop
->nlo_open_owner
!= noop
)
5747 TAILQ_FOREACH_SAFE(nflp
, &nlop
->nlo_locks
, nfl_lolink
, nextnflp
) {
5748 /* skip dead & blocked lock requests (shouldn't be any in the held lock list) */
5749 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5751 /* skip non-delegated locks */
5752 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5754 error
= nmp
->nm_funcs
->nf_setlock_rpc(nofp
->nof_np
, nofp
, nflp
, 0, flags
, current_thread(), noop
->noo_cred
);
5756 NP(nofp
->nof_np
, "nfs: delegated lock claim (0x%llx, 0x%llx) failed %d, %d",
5757 nflp
->nfl_start
, nflp
->nfl_end
, error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5761 // NP(nofp->nof_np, "nfs: delegated lock claim (0x%llx, 0x%llx) succeeded, %d",
5762 // nflp->nfl_start, nflp->nfl_end, kauth_cred_getuid(nofp->nof_owner->noo_cred));
5770 if (!error
) /* all state claimed successfully! */
5773 /* restart if it looks like a problem more than just losing the delegation */
5774 if (!nfs_mount_state_error_delegation_lost(error
) &&
5775 ((error
== ETIMEDOUT
) || nfs_mount_state_error_should_restart(error
))) {
5776 NP(nofp
->nof_np
, "nfs delegated lock claim error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5777 if ((error
== ETIMEDOUT
) && ((nmp
= NFSTONMP(nofp
->nof_np
))))
5778 nfs_need_reconnect(nmp
);
5782 /* delegated state lost (once held but now not claimable) */
5783 NP(nofp
->nof_np
, "nfs delegated state claim error %d, state lost, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5786 * Any problems with the delegation probably indicates that we
5787 * should review/return all of our current delegation state.
5789 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5790 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5791 lck_mtx_lock(&nmp
->nm_lock
);
5792 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5793 lck_mtx_unlock(&nmp
->nm_lock
);
5796 /* revoke all open file state */
5797 nfs_revoke_open_state_for_node(nofp
->nof_np
);
5803 * Release all open state for the given node.
5806 nfs_release_open_state_for_node(nfsnode_t np
, int force
)
5808 struct nfsmount
*nmp
= NFSTONMP(np
);
5809 struct nfs_open_file
*nofp
;
5810 struct nfs_file_lock
*nflp
, *nextnflp
;
5812 /* drop held locks */
5813 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
5814 /* skip dead & blocked lock requests */
5815 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5817 /* send an unlock if not a delegated lock */
5818 if (!force
&& nmp
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5819 nmp
->nm_funcs
->nf_unlock_rpc(np
, nflp
->nfl_owner
, F_WRLCK
, nflp
->nfl_start
, nflp
->nfl_end
, R_RECOVER
,
5820 NULL
, nflp
->nfl_owner
->nlo_open_owner
->noo_cred
);
5821 /* kill/remove the lock */
5822 lck_mtx_lock(&np
->n_openlock
);
5823 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
5824 lck_mtx_lock(&nflp
->nfl_owner
->nlo_lock
);
5825 TAILQ_REMOVE(&nflp
->nfl_owner
->nlo_locks
, nflp
, nfl_lolink
);
5826 lck_mtx_unlock(&nflp
->nfl_owner
->nlo_lock
);
5827 if (nflp
->nfl_blockcnt
) {
5828 /* wake up anyone blocked on this lock */
5831 /* remove nflp from lock list and destroy */
5832 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
5833 nfs_file_lock_destroy(nflp
);
5835 lck_mtx_unlock(&np
->n_openlock
);
5838 lck_mtx_lock(&np
->n_openlock
);
5840 /* drop all opens */
5841 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5842 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)
5844 /* mark open state as lost */
5845 lck_mtx_lock(&nofp
->nof_lock
);
5846 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
5847 nofp
->nof_flags
|= NFS_OPEN_FILE_LOST
;
5849 lck_mtx_unlock(&nofp
->nof_lock
);
5850 if (!force
&& nmp
&& (nmp
->nm_vers
>= NFS_VER4
))
5851 nfs4_close_rpc(np
, nofp
, NULL
, nofp
->nof_owner
->noo_cred
, R_RECOVER
);
5854 lck_mtx_unlock(&np
->n_openlock
);
5858 * State for a node has been lost, drop it, and revoke the node.
5859 * Attempt to return any state if possible in case the server
5860 * might somehow think we hold it.
5863 nfs_revoke_open_state_for_node(nfsnode_t np
)
5865 struct nfsmount
*nmp
;
5867 /* mark node as needing to be revoked */
5868 nfs_node_lock_force(np
);
5869 if (np
->n_flag
& NREVOKE
) /* already revoked? */
5871 NP(np
, "nfs_revoke_open_state_for_node(): already revoked");
5872 nfs_node_unlock(np
);
5875 np
->n_flag
|= NREVOKE
;
5876 nfs_node_unlock(np
);
5878 nfs_release_open_state_for_node(np
, 0);
5879 NP(np
, "nfs: state lost for %p 0x%x", np
, np
->n_flag
);
5881 /* mark mount as needing a revoke scan and have the socket thread do it. */
5882 if ((nmp
= NFSTONMP(np
))) {
5883 lck_mtx_lock(&nmp
->nm_lock
);
5884 nmp
->nm_state
|= NFSSTA_REVOKE
;
5885 nfs_mount_sock_thread_wake(nmp
);
5886 lck_mtx_unlock(&nmp
->nm_lock
);
5891 * Claim the delegated open combinations that each of this node's open files hold.
5894 nfs4_claim_delegated_state_for_node(nfsnode_t np
, int flags
)
5896 struct nfs_open_file
*nofp
;
5899 lck_mtx_lock(&np
->n_openlock
);
5901 /* walk the open file list looking for opens with delegated state to claim */
5903 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5904 if (!nofp
->nof_d_rw_drw
&& !nofp
->nof_d_w_drw
&& !nofp
->nof_d_r_drw
&&
5905 !nofp
->nof_d_rw_dw
&& !nofp
->nof_d_w_dw
&& !nofp
->nof_d_r_dw
&&
5906 !nofp
->nof_d_rw
&& !nofp
->nof_d_w
&& !nofp
->nof_d_r
)
5908 lck_mtx_unlock(&np
->n_openlock
);
5909 error
= nfs4_claim_delegated_state_for_open_file(nofp
, flags
);
5910 lck_mtx_lock(&np
->n_openlock
);
5916 lck_mtx_unlock(&np
->n_openlock
);
5922 * Mark a node as needed to have its delegation returned.
5923 * Queue it up on the delegation return queue.
5924 * Make sure the thread is running.
5927 nfs4_delegation_return_enqueue(nfsnode_t np
)
5929 struct nfsmount
*nmp
;
5932 if (nfs_mount_gone(nmp
))
5935 lck_mtx_lock(&np
->n_openlock
);
5936 np
->n_openflags
|= N_DELEG_RETURN
;
5937 lck_mtx_unlock(&np
->n_openlock
);
5939 lck_mtx_lock(&nmp
->nm_lock
);
5940 if (np
->n_dreturn
.tqe_next
== NFSNOLIST
)
5941 TAILQ_INSERT_TAIL(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5942 nfs_mount_sock_thread_wake(nmp
);
5943 lck_mtx_unlock(&nmp
->nm_lock
);
5947 * return any delegation we may have for the given node
5950 nfs4_delegation_return(nfsnode_t np
, int flags
, thread_t thd
, kauth_cred_t cred
)
5952 struct nfsmount
*nmp
;
5954 nfs_stateid dstateid
;
5958 if (nfs_mount_gone(nmp
))
5961 /* first, make sure the node's marked for delegation return */
5962 lck_mtx_lock(&np
->n_openlock
);
5963 np
->n_openflags
|= (N_DELEG_RETURN
|N_DELEG_RETURNING
);
5964 lck_mtx_unlock(&np
->n_openlock
);
5966 /* make sure nobody else is using the delegation state */
5967 if ((error
= nfs_open_state_set_busy(np
, NULL
)))
5970 /* claim any delegated state */
5971 if ((error
= nfs4_claim_delegated_state_for_node(np
, flags
)))
5974 /* return the delegation */
5975 lck_mtx_lock(&np
->n_openlock
);
5976 dstateid
= np
->n_dstateid
;
5977 fh
.fh_len
= np
->n_fhsize
;
5978 bcopy(np
->n_fhp
, &fh
.fh_data
, fh
.fh_len
);
5979 lck_mtx_unlock(&np
->n_openlock
);
5980 error
= nfs4_delegreturn_rpc(NFSTONMP(np
), fh
.fh_data
, fh
.fh_len
, &dstateid
, flags
, thd
, cred
);
5981 /* assume delegation is gone for all errors except ETIMEDOUT, NFSERR_*MOVED */
5982 if ((error
!= ETIMEDOUT
) && (error
!= NFSERR_MOVED
) && (error
!= NFSERR_LEASE_MOVED
)) {
5983 lck_mtx_lock(&np
->n_openlock
);
5984 np
->n_openflags
&= ~N_DELEG_MASK
;
5985 lck_mtx_lock(&nmp
->nm_lock
);
5986 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
5987 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
5988 np
->n_dlink
.tqe_next
= NFSNOLIST
;
5990 lck_mtx_unlock(&nmp
->nm_lock
);
5991 lck_mtx_unlock(&np
->n_openlock
);
5995 /* make sure it's no longer on the return queue and clear the return flags */
5996 lck_mtx_lock(&nmp
->nm_lock
);
5997 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
5998 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5999 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
6001 lck_mtx_unlock(&nmp
->nm_lock
);
6002 lck_mtx_lock(&np
->n_openlock
);
6003 np
->n_openflags
&= ~(N_DELEG_RETURN
|N_DELEG_RETURNING
);
6004 lck_mtx_unlock(&np
->n_openlock
);
6007 NP(np
, "nfs4_delegation_return, error %d", error
);
6008 if (error
== ETIMEDOUT
)
6009 nfs_need_reconnect(nmp
);
6010 if (nfs_mount_state_error_should_restart(error
)) {
6011 /* make sure recovery happens */
6012 lck_mtx_lock(&nmp
->nm_lock
);
6013 nfs_need_recover(nmp
, nfs_mount_state_error_delegation_lost(error
) ? NFSERR_EXPIRED
: 0);
6014 lck_mtx_unlock(&nmp
->nm_lock
);
6018 nfs_open_state_clear_busy(np
);
6024 * RPC to return a delegation for a file handle
6027 nfs4_delegreturn_rpc(struct nfsmount
*nmp
, u_char
*fhp
, int fhlen
, struct nfs_stateid
*sid
, int flags
, thread_t thd
, kauth_cred_t cred
)
6029 int error
= 0, status
, numops
;
6031 struct nfsm_chain nmreq
, nmrep
;
6032 struct nfsreq_secinfo_args si
;
6034 NFSREQ_SECINFO_SET(&si
, NULL
, fhp
, fhlen
, NULL
, 0);
6035 nfsm_chain_null(&nmreq
);
6036 nfsm_chain_null(&nmrep
);
6038 // PUTFH, DELEGRETURN
6040 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
6041 nfsm_chain_add_compound_header(error
, &nmreq
, "delegreturn", nmp
->nm_minor_vers
, numops
);
6043 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6044 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
6046 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_DELEGRETURN
);
6047 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
6048 nfsm_chain_build_done(error
, &nmreq
);
6049 nfsm_assert(error
, (numops
== 0), EPROTO
);
6051 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
, &nmrep
, &xid
, &status
);
6052 nfsm_chain_skip_tag(error
, &nmrep
);
6053 nfsm_chain_get_32(error
, &nmrep
, numops
);
6054 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6055 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_DELEGRETURN
);
6057 nfsm_chain_cleanup(&nmreq
);
6058 nfsm_chain_cleanup(&nmrep
);
6065 * Just call nfs_bioread() to do the work.
6067 * Note: the exec code paths have a tendency to call VNOP_READ (and VNOP_MMAP)
6068 * without first calling VNOP_OPEN, so we make sure the file is open here.
6072 struct vnop_read_args
/* {
6073 struct vnodeop_desc *a_desc;
6077 vfs_context_t a_context;
6080 vnode_t vp
= ap
->a_vp
;
6081 vfs_context_t ctx
= ap
->a_context
;
6083 struct nfsmount
*nmp
;
6084 struct nfs_open_owner
*noop
;
6085 struct nfs_open_file
*nofp
;
6088 if (vnode_vtype(ap
->a_vp
) != VREG
)
6089 return (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EPERM
;
6093 if (nfs_mount_gone(nmp
))
6095 if (np
->n_flag
& NREVOKE
)
6098 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6102 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
6103 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6104 NP(np
, "nfs_vnop_read: LOST %d", kauth_cred_getuid(noop
->noo_cred
));
6107 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6108 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
6114 nfs_open_owner_rele(noop
);
6118 * Since the read path is a hot path, if we already have
6119 * read access, lets go and try and do the read, without
6120 * busying the mount and open file node for this open owner.
6122 * N.B. This is inherently racy w.r.t. an execve using
6123 * an already open file, in that the read at the end of
6124 * this routine will be racing with a potential close.
6125 * The code below ultimately has the same problem. In practice
6126 * this does not seem to be an issue.
6128 if (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
) {
6129 nfs_open_owner_rele(noop
);
6132 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6134 nfs_open_owner_rele(noop
);
6138 * If we don't have a file already open with the access we need (read) then
6139 * we need to open one. Otherwise we just co-opt an open. We might not already
6140 * have access because we're trying to read the first page of the
6143 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
6145 nfs_mount_state_in_use_end(nmp
, 0);
6146 nfs_open_owner_rele(noop
);
6149 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
)) {
6150 /* we don't have the file open, so open it for read access if we're not denied */
6151 if (nofp
->nof_flags
& NFS_OPEN_FILE_NEEDCLOSE
) {
6152 NP(np
, "nfs_vnop_read: File already needs close access: 0x%x, cred: %d thread: %lld",
6153 nofp
->nof_access
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
), thread_tid(vfs_context_thread(ctx
)));
6155 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) {
6156 nfs_open_file_clear_busy(nofp
);
6157 nfs_mount_state_in_use_end(nmp
, 0);
6158 nfs_open_owner_rele(noop
);
6161 if (np
->n_flag
& NREVOKE
) {
6163 nfs_open_file_clear_busy(nofp
);
6164 nfs_mount_state_in_use_end(nmp
, 0);
6165 nfs_open_owner_rele(noop
);
6168 if (nmp
->nm_vers
< NFS_VER4
) {
6169 /* NFS v2/v3 opens are always allowed - so just add it. */
6170 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
6172 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
6175 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
6178 nfs_open_file_clear_busy(nofp
);
6179 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6183 nfs_open_owner_rele(noop
);
6187 return (nfs_bioread(VTONFS(ap
->a_vp
), ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
));
6191 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6192 * Files are created using the NFSv4 OPEN RPC. So we must open the
6193 * file to create it and then close it.
6197 struct vnop_create_args
/* {
6198 struct vnodeop_desc *a_desc;
6201 struct componentname *a_cnp;
6202 struct vnode_attr *a_vap;
6203 vfs_context_t a_context;
6206 vfs_context_t ctx
= ap
->a_context
;
6207 struct componentname
*cnp
= ap
->a_cnp
;
6208 struct vnode_attr
*vap
= ap
->a_vap
;
6209 vnode_t dvp
= ap
->a_dvp
;
6210 vnode_t
*vpp
= ap
->a_vpp
;
6211 struct nfsmount
*nmp
;
6213 int error
= 0, busyerror
= 0, accessMode
, denyMode
;
6214 struct nfs_open_owner
*noop
= NULL
;
6215 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
6218 if (nfs_mount_gone(nmp
))
6222 nfs_avoid_needless_id_setting_on_create(VTONFS(dvp
), vap
, ctx
);
6224 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6229 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6231 nfs_open_owner_rele(noop
);
6235 /* grab a provisional, nodeless open file */
6236 error
= nfs_open_file_find(NULL
, noop
, &newnofp
, 0, 0, 1);
6237 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6238 printf("nfs_vnop_create: LOST\n");
6241 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6242 /* This shouldn't happen given that this is a new, nodeless nofp */
6243 nfs_mount_state_in_use_end(nmp
, 0);
6244 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
6245 nfs_open_file_destroy(newnofp
);
6251 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
6254 nfs_open_file_destroy(newnofp
);
6260 * We're just trying to create the file.
6261 * We'll create/open it RW, and set NFS_OPEN_FILE_CREATE.
6263 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
6264 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
6266 /* Do the open/create */
6267 error
= nfs4_open_rpc(newnofp
, ctx
, cnp
, vap
, dvp
, vpp
, NFS_OPEN_CREATE
, accessMode
, denyMode
);
6268 if ((error
== EACCES
) && vap
&& !(vap
->va_vaflags
& VA_EXCLUSIVE
) &&
6269 VATTR_IS_ACTIVE(vap
, va_mode
) && !(vap
->va_mode
& S_IWUSR
)) {
6271 * Hmm... it looks like we may have a situation where the request was
6272 * retransmitted because we didn't get the first response which successfully
6273 * created/opened the file and then the second time we were denied the open
6274 * because the mode the file was created with doesn't allow write access.
6276 * We'll try to work around this by temporarily updating the mode and
6277 * retrying the open.
6279 struct vnode_attr vattr
;
6281 /* first make sure it's there */
6282 int error2
= nfs_lookitup(VTONFS(dvp
), cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6283 if (!error2
&& np
) {
6284 nfs_node_unlock(np
);
6286 if (vnode_vtype(NFSTOV(np
)) == VREG
) {
6288 VATTR_SET(&vattr
, va_mode
, (vap
->va_mode
| S_IWUSR
));
6289 if (!nfs4_setattr_rpc(np
, &vattr
, ctx
)) {
6290 error2
= nfs4_open_rpc(newnofp
, ctx
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
6292 VATTR_SET(&vattr
, va_mode
, vap
->va_mode
);
6293 nfs4_setattr_rpc(np
, &vattr
, ctx
);
6304 if (!error
&& !*vpp
) {
6305 printf("nfs4_open_rpc returned without a node?\n");
6306 /* Hmmm... with no node, we have no filehandle and can't close it */
6310 /* need to cleanup our temporary nofp */
6311 nfs_open_file_clear_busy(newnofp
);
6312 nfs_open_file_destroy(newnofp
);
6316 /* After we have a node, add our open file struct to the node */
6318 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
6320 error
= nfs_open_file_find_internal(np
, noop
, &nofp
, 0, 0, 0);
6322 /* This shouldn't happen, because we passed in a new nofp to use. */
6323 printf("nfs_open_file_find_internal failed! %d\n", error
);
6325 } else if (nofp
!= newnofp
) {
6327 * Hmm... an open file struct already exists.
6328 * Mark the existing one busy and merge our open into it.
6329 * Then destroy the one we created.
6330 * Note: there's no chance of an open confict because the
6331 * open has already been granted.
6333 busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
6334 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
6335 nofp
->nof_stateid
= newnofp
->nof_stateid
;
6336 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
6337 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
6338 nfs_open_file_clear_busy(newnofp
);
6339 nfs_open_file_destroy(newnofp
);
6342 /* mark the node as holding a create-initiated open */
6343 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
6344 nofp
->nof_creator
= current_thread();
6346 if (nofp
&& !busyerror
)
6347 nfs_open_file_clear_busy(nofp
);
6348 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6349 nofp
= newnofp
= NULL
;
6354 nfs_open_owner_rele(noop
);
6359 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6365 struct componentname
*cnp
,
6366 struct vnode_attr
*vap
,
6371 struct nfsmount
*nmp
;
6372 struct nfs_vattr nvattr
;
6373 int error
= 0, create_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
6374 int nfsvers
, namedattrs
, numops
;
6375 u_int64_t xid
, savedxid
= 0;
6376 nfsnode_t np
= NULL
;
6377 vnode_t newvp
= NULL
;
6378 struct nfsm_chain nmreq
, nmrep
;
6379 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
6383 struct nfsreq rq
, *req
= &rq
;
6384 struct nfs_dulookup dul
;
6385 struct nfsreq_secinfo_args si
;
6387 nmp
= NFSTONMP(dnp
);
6388 if (nfs_mount_gone(nmp
))
6390 nfsvers
= nmp
->nm_vers
;
6391 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6392 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6395 sd
.specdata1
= sd
.specdata2
= 0;
6404 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
6406 sd
.specdata1
= major(vap
->va_rdev
);
6407 sd
.specdata2
= minor(vap
->va_rdev
);
6420 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
6422 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
6424 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6426 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
6427 NVATTR_INIT(&nvattr
);
6428 nfsm_chain_null(&nmreq
);
6429 nfsm_chain_null(&nmrep
);
6431 // PUTFH, SAVEFH, CREATE, GETATTR(FH), RESTOREFH, GETATTR
6433 nfsm_chain_build_alloc_init(error
, &nmreq
, 66 * NFSX_UNSIGNED
);
6434 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, nmp
->nm_minor_vers
, numops
);
6436 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6437 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6439 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6441 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CREATE
);
6442 nfsm_chain_add_32(error
, &nmreq
, type
);
6443 if (type
== NFLNK
) {
6444 nfsm_chain_add_name(error
, &nmreq
, link
, strlen(link
), nmp
);
6445 } else if ((type
== NFBLK
) || (type
== NFCHR
)) {
6446 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata1
);
6447 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata2
);
6449 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6450 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
6452 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6453 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6454 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6455 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
6457 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6459 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6460 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
6461 nfsm_chain_build_done(error
, &nmreq
);
6462 nfsm_assert(error
, (numops
== 0), EPROTO
);
6465 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6466 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6469 nfs_dulookup_start(&dul
, dnp
, ctx
);
6470 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6473 if ((lockerror
= nfs_node_lock(dnp
)))
6475 nfsm_chain_skip_tag(error
, &nmrep
);
6476 nfsm_chain_get_32(error
, &nmrep
, numops
);
6477 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6478 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6480 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CREATE
);
6481 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
6482 bmlen
= NFS_ATTR_BITMAP_LEN
;
6483 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
6484 /* At this point if we have no error, the object was created. */
6485 /* if we don't get attributes, then we should lookitup. */
6486 create_error
= error
;
6488 nfs_vattr_set_supported(bitmap
, vap
);
6489 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6491 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6493 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
6494 printf("nfs: create/%s didn't return filehandle? %s\n", tag
, cnp
->cn_nameptr
);
6498 /* directory attributes: if we don't get them, make sure to invalidate */
6499 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6500 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6502 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
6504 NATTRINVALIDATE(dnp
);
6507 nfsm_chain_cleanup(&nmreq
);
6508 nfsm_chain_cleanup(&nmrep
);
6511 if (!create_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
6512 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6513 cache_purge_negatives(NFSTOV(dnp
));
6515 dnp
->n_flag
|= NMODIFIED
;
6516 nfs_node_unlock(dnp
);
6517 /* nfs_getattr() will check changed and purge caches */
6518 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6521 if (!error
&& fh
.fh_len
) {
6522 /* create the vnode with the filehandle and attributes */
6524 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6528 NVATTR_CLEANUP(&nvattr
);
6531 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6534 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
6535 * if we can succeed in looking up the object.
6537 if ((create_error
== EEXIST
) || (!create_error
&& !newvp
)) {
6538 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6541 if (vnode_vtype(newvp
) != nfstov_type(type
, nfsvers
))
6546 nfs_node_clear_busy(dnp
);
6549 nfs_node_unlock(np
);
6553 nfs_node_unlock(np
);
6561 struct vnop_mknod_args
/* {
6562 struct vnodeop_desc *a_desc;
6565 struct componentname *a_cnp;
6566 struct vnode_attr *a_vap;
6567 vfs_context_t a_context;
6570 nfsnode_t np
= NULL
;
6571 struct nfsmount
*nmp
;
6574 nmp
= VTONMP(ap
->a_dvp
);
6575 if (nfs_mount_gone(nmp
))
6578 if (!VATTR_IS_ACTIVE(ap
->a_vap
, va_type
))
6580 switch (ap
->a_vap
->va_type
) {
6590 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6591 vtonfs_type(ap
->a_vap
->va_type
, nmp
->nm_vers
), NULL
, &np
);
6593 *ap
->a_vpp
= NFSTOV(np
);
6599 struct vnop_mkdir_args
/* {
6600 struct vnodeop_desc *a_desc;
6603 struct componentname *a_cnp;
6604 struct vnode_attr *a_vap;
6605 vfs_context_t a_context;
6608 nfsnode_t np
= NULL
;
6611 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6614 *ap
->a_vpp
= NFSTOV(np
);
6620 struct vnop_symlink_args
/* {
6621 struct vnodeop_desc *a_desc;
6624 struct componentname *a_cnp;
6625 struct vnode_attr *a_vap;
6627 vfs_context_t a_context;
6630 nfsnode_t np
= NULL
;
6633 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6634 NFLNK
, ap
->a_target
, &np
);
6636 *ap
->a_vpp
= NFSTOV(np
);
6642 struct vnop_link_args
/* {
6643 struct vnodeop_desc *a_desc;
6646 struct componentname *a_cnp;
6647 vfs_context_t a_context;
6650 vfs_context_t ctx
= ap
->a_context
;
6651 vnode_t vp
= ap
->a_vp
;
6652 vnode_t tdvp
= ap
->a_tdvp
;
6653 struct componentname
*cnp
= ap
->a_cnp
;
6654 int error
= 0, lockerror
= ENOENT
, status
;
6655 struct nfsmount
*nmp
;
6656 nfsnode_t np
= VTONFS(vp
);
6657 nfsnode_t tdnp
= VTONFS(tdvp
);
6658 int nfsvers
, numops
;
6659 u_int64_t xid
, savedxid
;
6660 struct nfsm_chain nmreq
, nmrep
;
6661 struct nfsreq_secinfo_args si
;
6663 if (vnode_mount(vp
) != vnode_mount(tdvp
))
6667 if (nfs_mount_gone(nmp
))
6669 nfsvers
= nmp
->nm_vers
;
6670 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6672 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6676 * Push all writes to the server, so that the attribute cache
6677 * doesn't get "out of sync" with the server.
6678 * XXX There should be a better way!
6680 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
6682 if ((error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
))))
6685 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6686 nfsm_chain_null(&nmreq
);
6687 nfsm_chain_null(&nmrep
);
6689 // PUTFH(SOURCE), SAVEFH, PUTFH(DIR), LINK, GETATTR(DIR), RESTOREFH, GETATTR
6691 nfsm_chain_build_alloc_init(error
, &nmreq
, 29 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
6692 nfsm_chain_add_compound_header(error
, &nmreq
, "link", nmp
->nm_minor_vers
, numops
);
6694 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6695 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6697 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6699 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6700 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
6702 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LINK
);
6703 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6705 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6706 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
6708 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6710 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6711 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
6712 nfsm_chain_build_done(error
, &nmreq
);
6713 nfsm_assert(error
, (numops
== 0), EPROTO
);
6715 error
= nfs_request(tdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
6717 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
6721 nfsm_chain_skip_tag(error
, &nmrep
);
6722 nfsm_chain_get_32(error
, &nmrep
, numops
);
6723 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6724 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6725 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6726 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LINK
);
6727 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
6728 /* directory attributes: if we don't get them, make sure to invalidate */
6729 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6731 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
6733 NATTRINVALIDATE(tdnp
);
6734 /* link attributes: if we don't get them, make sure to invalidate */
6735 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6736 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6738 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
6740 NATTRINVALIDATE(np
);
6742 nfsm_chain_cleanup(&nmreq
);
6743 nfsm_chain_cleanup(&nmrep
);
6745 tdnp
->n_flag
|= NMODIFIED
;
6746 /* Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. */
6747 if (error
== EEXIST
)
6749 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
6750 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
6751 cache_purge_negatives(tdvp
);
6754 nfs_node_unlock2(tdnp
, np
);
6755 nfs_node_clear_busy2(tdnp
, np
);
6761 struct vnop_rmdir_args
/* {
6762 struct vnodeop_desc *a_desc;
6765 struct componentname *a_cnp;
6766 vfs_context_t a_context;
6769 vfs_context_t ctx
= ap
->a_context
;
6770 vnode_t vp
= ap
->a_vp
;
6771 vnode_t dvp
= ap
->a_dvp
;
6772 struct componentname
*cnp
= ap
->a_cnp
;
6773 struct nfsmount
*nmp
;
6774 int error
= 0, namedattrs
;
6775 nfsnode_t np
= VTONFS(vp
);
6776 nfsnode_t dnp
= VTONFS(dvp
);
6777 struct nfs_dulookup dul
;
6779 if (vnode_vtype(vp
) != VDIR
)
6782 nmp
= NFSTONMP(dnp
);
6783 if (nfs_mount_gone(nmp
))
6785 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6787 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
6791 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6792 nfs_dulookup_start(&dul
, dnp
, ctx
);
6795 error
= nfs4_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6796 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
6798 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
6799 /* nfs_getattr() will check changed and purge caches */
6800 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6802 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6803 nfs_node_clear_busy2(dnp
, np
);
6806 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
6808 if (error
== ENOENT
)
6812 * remove nfsnode from hash now so we can't accidentally find it
6813 * again if another object gets created with the same filehandle
6814 * before this vnode gets reclaimed
6816 lck_mtx_lock(nfs_node_hash_mutex
);
6817 if (np
->n_hflag
& NHHASHED
) {
6818 LIST_REMOVE(np
, n_hash
);
6819 np
->n_hflag
&= ~NHHASHED
;
6820 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
6822 lck_mtx_unlock(nfs_node_hash_mutex
);
6828 * NFSv4 Named Attributes
6830 * Both the extended attributes interface and the named streams interface
6831 * are backed by NFSv4 named attributes. The implementations for both use
6832 * a common set of routines in an attempt to reduce code duplication, to
6833 * increase efficiency, to increase caching of both names and data, and to
6834 * confine the complexity.
6836 * Each NFS node caches its named attribute directory's file handle.
6837 * The directory nodes for the named attribute directories are handled
6838 * exactly like regular directories (with a couple minor exceptions).
6839 * Named attribute nodes are also treated as much like regular files as
6842 * Most of the heavy lifting is done by nfs4_named_attr_get().
6846 * Get the given node's attribute directory node.
6847 * If !fetch, then only return a cached node.
6848 * Otherwise, we will attempt to fetch the node from the server.
6849 * (Note: the node should be marked busy.)
6852 nfs4_named_attr_dir_get(nfsnode_t np
, int fetch
, vfs_context_t ctx
)
6854 nfsnode_t adnp
= NULL
;
6855 struct nfsmount
*nmp
;
6856 int error
= 0, status
, numops
;
6857 struct nfsm_chain nmreq
, nmrep
;
6859 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
6861 struct nfs_vattr nvattr
;
6862 struct componentname cn
;
6863 struct nfsreq rq
, *req
= &rq
;
6864 struct nfsreq_secinfo_args si
;
6867 if (nfs_mount_gone(nmp
))
6869 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6872 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6873 NVATTR_INIT(&nvattr
);
6874 nfsm_chain_null(&nmreq
);
6875 nfsm_chain_null(&nmrep
);
6877 bzero(&cn
, sizeof(cn
));
6878 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
6879 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
6880 cn
.cn_nameiop
= LOOKUP
;
6882 if (np
->n_attrdirfh
) {
6883 // XXX can't set parent correctly (to np) yet
6884 error
= nfs_nget(nmp
->nm_mountp
, NULL
, &cn
, np
->n_attrdirfh
+1, *np
->n_attrdirfh
,
6885 NULL
, NULL
, RPCAUTH_UNKNOWN
, NG_NOCREATE
, &adnp
);
6894 // PUTFH, OPENATTR, GETATTR
6896 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
6897 nfsm_chain_add_compound_header(error
, &nmreq
, "openattr", nmp
->nm_minor_vers
, numops
);
6899 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6900 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
6902 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
6903 nfsm_chain_add_32(error
, &nmreq
, 0);
6905 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6906 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6907 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6908 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
6909 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
6910 nfsm_chain_build_done(error
, &nmreq
);
6911 nfsm_assert(error
, (numops
== 0), EPROTO
);
6913 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6914 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6916 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6918 nfsm_chain_skip_tag(error
, &nmrep
);
6919 nfsm_chain_get_32(error
, &nmrep
, numops
);
6920 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6921 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
6922 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6924 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6926 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
6930 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
6931 /* (re)allocate attrdir fh buffer */
6932 if (np
->n_attrdirfh
)
6933 FREE(np
->n_attrdirfh
, M_TEMP
);
6934 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
6936 if (!np
->n_attrdirfh
) {
6940 /* cache the attrdir fh in the node */
6941 *np
->n_attrdirfh
= fh
.fh_len
;
6942 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
6943 /* create node for attrdir */
6944 // XXX can't set parent correctly (to np) yet
6945 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
6947 NVATTR_CLEANUP(&nvattr
);
6948 nfsm_chain_cleanup(&nmreq
);
6949 nfsm_chain_cleanup(&nmrep
);
6952 /* sanity check that this node is an attribute directory */
6953 if (adnp
->n_vattr
.nva_type
!= VDIR
)
6955 if (!(adnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
6957 nfs_node_unlock(adnp
);
6959 vnode_put(NFSTOV(adnp
));
6961 return (error
? NULL
: adnp
);
6965 * Get the given node's named attribute node for the name given.
6967 * In an effort to increase the performance of named attribute access, we try
6968 * to reduce server requests by doing the following:
6970 * - cache the node's named attribute directory file handle in the node
6971 * - maintain a directory vnode for the attribute directory
6972 * - use name cache entries (positive and negative) to speed up lookups
6973 * - optionally open the named attribute (with the given accessMode) in the same RPC
6974 * - combine attribute directory retrieval with the lookup/open RPC
6975 * - optionally prefetch the named attribute's first block of data in the same RPC
6977 * Also, in an attempt to reduce the number of copies/variations of this code,
6978 * parts of the RPC building/processing code are conditionalized on what is
6979 * needed for any particular request (openattr, lookup vs. open, read).
6981 * Note that because we may not have the attribute directory node when we start
6982 * the lookup/open, we lock both the node and the attribute directory node.
6985 #define NFS_GET_NAMED_ATTR_CREATE 0x1
6986 #define NFS_GET_NAMED_ATTR_CREATE_GUARDED 0x2
6987 #define NFS_GET_NAMED_ATTR_TRUNCATE 0x4
6988 #define NFS_GET_NAMED_ATTR_PREFETCH 0x8
6991 nfs4_named_attr_get(
6993 struct componentname
*cnp
,
6994 uint32_t accessMode
,
6998 struct nfs_open_file
**nofpp
)
7000 struct nfsmount
*nmp
;
7001 int error
= 0, open_error
= EIO
;
7002 int inuse
= 0, adlockerror
= ENOENT
, busyerror
= ENOENT
, adbusyerror
= ENOENT
, nofpbusyerror
= ENOENT
;
7003 int create
, guarded
, prefetch
, truncate
, noopbusy
= 0;
7004 int open
, status
, numops
, hadattrdir
, negnamecache
;
7005 struct nfs_vattr nvattr
;
7006 struct vnode_attr vattr
;
7007 nfsnode_t adnp
= NULL
, anp
= NULL
;
7009 u_int64_t xid
, savedxid
= 0;
7010 struct nfsm_chain nmreq
, nmrep
;
7011 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
7012 uint32_t denyMode
, rflags
, delegation
, recall
, eof
, rlen
, retlen
;
7013 nfs_stateid stateid
, dstateid
;
7015 struct nfs_open_owner
*noop
= NULL
;
7016 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
7017 struct vnop_access_args naa
;
7022 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
7023 struct kauth_ace ace
;
7024 struct nfsreq rq
, *req
= &rq
;
7025 struct nfsreq_secinfo_args si
;
7029 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
7032 slen
= sizeof(sbuf
);
7035 if (nfs_mount_gone(nmp
))
7037 NVATTR_INIT(&nvattr
);
7038 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
7039 thd
= vfs_context_thread(ctx
);
7040 cred
= vfs_context_ucred(ctx
);
7041 create
= (flags
& NFS_GET_NAMED_ATTR_CREATE
) ? NFS_OPEN_CREATE
: NFS_OPEN_NOCREATE
;
7042 guarded
= (flags
& NFS_GET_NAMED_ATTR_CREATE_GUARDED
) ? NFS_CREATE_GUARDED
: NFS_CREATE_UNCHECKED
;
7043 truncate
= (flags
& NFS_GET_NAMED_ATTR_TRUNCATE
);
7044 prefetch
= (flags
& NFS_GET_NAMED_ATTR_PREFETCH
);
7047 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
7050 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7051 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7053 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_NONE
) {
7054 /* shouldn't happen... but just be safe */
7055 printf("nfs4_named_attr_get: create with no access %s\n", cnp
->cn_nameptr
);
7056 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
7058 open
= (accessMode
!= NFS_OPEN_SHARE_ACCESS_NONE
);
7061 * We're trying to open the file.
7062 * We'll create/open it with the given access mode,
7063 * and set NFS_OPEN_FILE_CREATE.
7065 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
7066 if (prefetch
&& guarded
)
7067 prefetch
= 0; /* no sense prefetching data that can't be there */
7069 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
7074 if ((error
= busyerror
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7077 adnp
= nfs4_named_attr_dir_get(np
, 0, ctx
);
7078 hadattrdir
= (adnp
!= NULL
);
7081 /* use the special state ID because we don't have a real one to send */
7082 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
7083 rlen
= MIN(nmp
->nm_rsize
, nmp
->nm_biosize
);
7085 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
7086 nfsm_chain_null(&nmreq
);
7087 nfsm_chain_null(&nmrep
);
7090 if ((error
= adbusyerror
= nfs_node_set_busy(adnp
, vfs_context_thread(ctx
))))
7092 /* nfs_getattr() will check changed and purge caches */
7093 error
= nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
7095 error
= cache_lookup(NFSTOV(adnp
), &avp
, cnp
);
7098 /* negative cache entry */
7102 /* try dir buf cache lookup */
7103 error
= nfs_dir_buf_cache_lookup(adnp
, &anp
, cnp
, ctx
, 0);
7104 if (!error
&& anp
) {
7105 /* dir buf cache hit */
7109 if (error
!= -1) /* cache miss */
7113 /* cache hit, not really an error */
7114 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
7116 *anpp
= anp
= VTONFS(avp
);
7118 nfs_node_clear_busy(adnp
);
7119 adbusyerror
= ENOENT
;
7121 /* check for directory access */
7122 naa
.a_desc
= &vnop_access_desc
;
7123 naa
.a_vp
= NFSTOV(adnp
);
7124 naa
.a_action
= KAUTH_VNODE_SEARCH
;
7125 naa
.a_context
= ctx
;
7127 /* compute actual success/failure based on accessibility */
7128 error
= nfs_vnop_access(&naa
);
7131 /* we either found it, or hit an error */
7132 if (!error
&& guarded
) {
7133 /* found cached entry but told not to use it */
7135 vnode_put(NFSTOV(anp
));
7138 /* we're done if error or we don't need to open */
7141 /* no error and we need to open... */
7147 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
7149 nfs_open_owner_rele(noop
);
7155 /* grab an open file - possibly provisional/nodeless if cache_lookup() failed */
7156 error
= nfs_open_file_find(anp
, noop
, &newnofp
, 0, 0, 1);
7157 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
7158 printf("nfs4_named_attr_get: LOST %d %s\n", kauth_cred_getuid(noop
->noo_cred
), cnp
->cn_nameptr
);
7161 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
7162 nfs_mount_state_in_use_end(nmp
, 0);
7163 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
7164 nfs_open_file_destroy(newnofp
);
7170 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
7173 nfs_open_file_destroy(newnofp
);
7179 * We already have the node. So we just need to open
7180 * it - which we may be able to do with a delegation.
7182 open_error
= error
= nfs4_open(anp
, newnofp
, accessMode
, denyMode
, ctx
);
7184 /* open succeeded, so our open file is no longer temporary */
7196 * We either don't have the attrdir or we didn't find the attribute
7197 * in the name cache, so we need to talk to the server.
7199 * If we don't have the attrdir, we'll need to ask the server for that too.
7200 * If the caller is requesting that the attribute be created, we need to
7201 * make sure the attrdir is created.
7202 * The caller may also request that the first block of an existing attribute
7203 * be retrieved at the same time.
7207 /* need to mark the open owner busy during the RPC */
7208 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
7214 * We'd like to get updated post-open/lookup attributes for the
7215 * directory and we may also want to prefetch some data via READ.
7216 * We'd like the READ results to be last so that we can leave the
7217 * data in the mbufs until the end.
7219 * At a minimum we're sending: PUTFH, LOOKUP/OPEN, GETATTR, PUTFH, GETATTR
7223 numops
+= 3; // also sending: OPENATTR, GETATTR, OPENATTR
7225 numops
+= 4; // also sending: SAVEFH, RESTOREFH, NVERIFY, READ
7226 nfsm_chain_build_alloc_init(error
, &nmreq
, 64 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
7227 nfsm_chain_add_compound_header(error
, &nmreq
, "getnamedattr", nmp
->nm_minor_vers
, numops
);
7230 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7231 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7234 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7235 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7237 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7238 nfsm_chain_add_32(error
, &nmreq
, create
? 1 : 0);
7240 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7241 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7242 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7243 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7244 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7248 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
7249 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
7250 nfsm_chain_add_32(error
, &nmreq
, accessMode
);
7251 nfsm_chain_add_32(error
, &nmreq
, denyMode
);
7252 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
7253 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
7254 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
7255 nfsm_chain_add_32(error
, &nmreq
, create
);
7257 nfsm_chain_add_32(error
, &nmreq
, guarded
);
7260 VATTR_SET(&vattr
, va_data_size
, 0);
7261 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7263 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
7264 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7267 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
7268 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7271 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7272 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7273 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7274 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7275 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7278 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
7282 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7283 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7286 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7287 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7289 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7290 nfsm_chain_add_32(error
, &nmreq
, 0);
7293 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7294 nfsm_chain_add_bitmap_masked(error
, &nmreq
, nfs_getattr_bitmap
,
7295 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7298 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
7300 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_NVERIFY
);
7302 VATTR_SET(&vattr
, va_data_size
, 0);
7303 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7305 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
7306 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
7307 nfsm_chain_add_64(error
, &nmreq
, 0);
7308 nfsm_chain_add_32(error
, &nmreq
, rlen
);
7310 nfsm_chain_build_done(error
, &nmreq
);
7311 nfsm_assert(error
, (numops
== 0), EPROTO
);
7313 error
= nfs_request_async(hadattrdir
? adnp
: np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
7314 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, open
? R_NOINTR
: 0, NULL
, &req
);
7316 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
7318 if (hadattrdir
&& ((adlockerror
= nfs_node_lock(adnp
))))
7319 error
= adlockerror
;
7321 nfsm_chain_skip_tag(error
, &nmrep
);
7322 nfsm_chain_get_32(error
, &nmrep
, numops
);
7323 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7325 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7326 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7328 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7330 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) && fh
.fh_len
) {
7331 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
7332 /* (re)allocate attrdir fh buffer */
7333 if (np
->n_attrdirfh
)
7334 FREE(np
->n_attrdirfh
, M_TEMP
);
7335 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
7337 if (np
->n_attrdirfh
) {
7338 /* remember the attrdir fh in the node */
7339 *np
->n_attrdirfh
= fh
.fh_len
;
7340 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
7341 /* create busied node for attrdir */
7342 struct componentname cn
;
7343 bzero(&cn
, sizeof(cn
));
7344 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
7345 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
7346 cn
.cn_nameiop
= LOOKUP
;
7347 // XXX can't set parent correctly (to np) yet
7348 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
7351 /* set the node busy */
7352 SET(adnp
->n_flag
, NBUSY
);
7355 /* if no adnp, oh well... */
7359 NVATTR_CLEANUP(&nvattr
);
7363 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
7364 nfs_owner_seqid_increment(noop
, NULL
, error
);
7365 nfsm_chain_get_stateid(error
, &nmrep
, &newnofp
->nof_stateid
);
7366 nfsm_chain_check_change_info(error
, &nmrep
, adnp
);
7367 nfsm_chain_get_32(error
, &nmrep
, rflags
);
7368 bmlen
= NFS_ATTR_BITMAP_LEN
;
7369 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
7370 nfsm_chain_get_32(error
, &nmrep
, delegation
);
7372 switch (delegation
) {
7373 case NFS_OPEN_DELEGATE_NONE
:
7375 case NFS_OPEN_DELEGATE_READ
:
7376 case NFS_OPEN_DELEGATE_WRITE
:
7377 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
7378 nfsm_chain_get_32(error
, &nmrep
, recall
);
7379 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
7380 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
7381 /* if we have any trouble accepting the ACE, just invalidate it */
7382 ace_type
= ace_flags
= ace_mask
= len
= 0;
7383 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
7384 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
7385 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
7386 nfsm_chain_get_32(error
, &nmrep
, len
);
7387 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
7388 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
7389 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
7390 if (!error
&& (len
>= slen
)) {
7391 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
7398 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
7400 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
7403 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
7408 if (s
&& (s
!= sbuf
))
7415 /* At this point if we have no error, the object was created/opened. */
7418 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOOKUP
);
7420 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7422 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7424 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
7429 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
7430 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7432 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7433 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7436 nfsm_chain_loadattr(error
, &nmrep
, adnp
, nmp
->nm_vers
, &xid
);
7440 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
7441 newnofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7442 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
7444 nfs_node_unlock(adnp
);
7445 adlockerror
= ENOENT
;
7447 NVATTR_CLEANUP(&nvattr
);
7448 error
= nfs4_open_confirm_rpc(nmp
, adnp
? adnp
: np
, fh
.fh_data
, fh
.fh_len
, noop
, &newnofp
->nof_stateid
, thd
, cred
, &nvattr
, &xid
);
7451 if ((adlockerror
= nfs_node_lock(adnp
)))
7452 error
= adlockerror
;
7457 if (open
&& adnp
&& !adlockerror
) {
7458 if (!open_error
&& (adnp
->n_flag
& NNEGNCENTRIES
)) {
7459 adnp
->n_flag
&= ~NNEGNCENTRIES
;
7460 cache_purge_negatives(NFSTOV(adnp
));
7462 adnp
->n_flag
|= NMODIFIED
;
7463 nfs_node_unlock(adnp
);
7464 adlockerror
= ENOENT
;
7465 nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
7467 if (adnp
&& !adlockerror
&& (error
== ENOENT
) &&
7468 (cnp
->cn_flags
& MAKEENTRY
) && (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
7469 /* add a negative entry in the name cache */
7470 cache_enter(NFSTOV(adnp
), NULL
, cnp
);
7471 adnp
->n_flag
|= NNEGNCENTRIES
;
7473 if (adnp
&& !adlockerror
) {
7474 nfs_node_unlock(adnp
);
7475 adlockerror
= ENOENT
;
7477 if (!error
&& !anp
&& fh
.fh_len
) {
7478 /* create the vnode with the filehandle and attributes */
7480 error
= nfs_nget(NFSTOMP(np
), adnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &anp
);
7483 nfs_node_unlock(anp
);
7485 if (!error
&& open
) {
7486 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
7487 /* After we have a node, add our open file struct to the node */
7489 error
= nfs_open_file_find_internal(anp
, noop
, &nofp
, 0, 0, 0);
7491 /* This shouldn't happen, because we passed in a new nofp to use. */
7492 printf("nfs_open_file_find_internal failed! %d\n", error
);
7494 } else if (nofp
!= newnofp
) {
7496 * Hmm... an open file struct already exists.
7497 * Mark the existing one busy and merge our open into it.
7498 * Then destroy the one we created.
7499 * Note: there's no chance of an open confict because the
7500 * open has already been granted.
7502 nofpbusyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7503 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
7504 nofp
->nof_stateid
= newnofp
->nof_stateid
;
7505 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
7506 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7507 nfs_open_file_clear_busy(newnofp
);
7508 nfs_open_file_destroy(newnofp
);
7514 /* mark the node as holding a create-initiated open */
7515 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
7516 nofp
->nof_creator
= current_thread();
7522 NVATTR_CLEANUP(&nvattr
);
7523 if (open
&& ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
))) {
7524 if (!error
&& anp
&& !recall
) {
7525 /* stuff the delegation state in the node */
7526 lck_mtx_lock(&anp
->n_openlock
);
7527 anp
->n_openflags
&= ~N_DELEG_MASK
;
7528 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7529 anp
->n_dstateid
= dstateid
;
7531 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7532 lck_mtx_lock(&nmp
->nm_lock
);
7533 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7534 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7535 lck_mtx_unlock(&nmp
->nm_lock
);
7537 lck_mtx_unlock(&anp
->n_openlock
);
7539 /* give the delegation back */
7541 if (NFS_CMPFH(anp
, fh
.fh_data
, fh
.fh_len
)) {
7542 /* update delegation state and return it */
7543 lck_mtx_lock(&anp
->n_openlock
);
7544 anp
->n_openflags
&= ~N_DELEG_MASK
;
7545 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7546 anp
->n_dstateid
= dstateid
;
7548 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7549 lck_mtx_lock(&nmp
->nm_lock
);
7550 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7551 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7552 lck_mtx_unlock(&nmp
->nm_lock
);
7554 lck_mtx_unlock(&anp
->n_openlock
);
7555 /* don't need to send a separate delegreturn for fh */
7558 /* return anp's current delegation */
7559 nfs4_delegation_return(anp
, 0, thd
, cred
);
7561 if (fh
.fh_len
) /* return fh's delegation if it wasn't for anp */
7562 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
7567 /* need to cleanup our temporary nofp */
7568 nfs_open_file_clear_busy(newnofp
);
7569 nfs_open_file_destroy(newnofp
);
7571 } else if (nofp
&& !nofpbusyerror
) {
7572 nfs_open_file_clear_busy(nofp
);
7573 nofpbusyerror
= ENOENT
;
7575 if (inuse
&& nfs_mount_state_in_use_end(nmp
, error
)) {
7577 nofp
= newnofp
= NULL
;
7578 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
7581 slen
= sizeof(sbuf
);
7582 nfsm_chain_cleanup(&nmreq
);
7583 nfsm_chain_cleanup(&nmrep
);
7585 vnode_put(NFSTOV(anp
));
7588 hadattrdir
= (adnp
!= NULL
);
7590 nfs_open_owner_clear_busy(noop
);
7597 nfs_open_owner_clear_busy(noop
);
7600 nfs_open_owner_rele(noop
);
7603 if (!error
&& prefetch
&& nmrep
.nmc_mhead
) {
7604 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
7605 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_NVERIFY
);
7606 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
7607 nfsm_chain_get_32(error
, &nmrep
, eof
);
7608 nfsm_chain_get_32(error
, &nmrep
, retlen
);
7609 if (!error
&& anp
) {
7611 * There can be one problem with doing the prefetch.
7612 * Because we don't have the node before we start the RPC, we
7613 * can't have the buffer busy while the READ is performed.
7614 * So there is a chance that other I/O occured on the same
7615 * range of data while we were performing this RPC. If that
7616 * happens, then it's possible the data we have in the READ
7617 * response is no longer up to date.
7618 * Once we have the node and the buffer, we need to make sure
7619 * that there's no chance we could be putting stale data in
7621 * So, we check if the range read is dirty or if any I/O may
7622 * have occured on it while we were performing our RPC.
7624 struct nfsbuf
*bp
= NULL
;
7628 retlen
= MIN(retlen
, rlen
);
7630 /* check if node needs size update or invalidation */
7631 if (ISSET(anp
->n_flag
, NUPDATESIZE
))
7632 nfs_data_update_size(anp
, 0);
7633 if (!(error
= nfs_node_lock(anp
))) {
7634 if (anp
->n_flag
& NNEEDINVALIDATE
) {
7635 anp
->n_flag
&= ~NNEEDINVALIDATE
;
7636 nfs_node_unlock(anp
);
7637 error
= nfs_vinvalbuf(NFSTOV(anp
), V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
7638 if (!error
) /* lets play it safe and just drop the data */
7641 nfs_node_unlock(anp
);
7645 /* calculate page mask for the range of data read */
7646 lastpg
= (trunc_page_32(retlen
) - 1) / PAGE_SIZE
;
7647 pagemask
= ((1 << (lastpg
+ 1)) - 1);
7650 error
= nfs_buf_get(anp
, 0, nmp
->nm_biosize
, thd
, NBLK_READ
|NBLK_NOWAIT
, &bp
);
7651 /* don't save the data if dirty or potential I/O conflict */
7652 if (!error
&& bp
&& !bp
->nb_dirtyoff
&& !(bp
->nb_dirty
& pagemask
) &&
7653 timevalcmp(&anp
->n_lastio
, &now
, <)) {
7654 OSAddAtomic64(1, &nfsstats
.read_bios
);
7655 CLR(bp
->nb_flags
, (NB_DONE
|NB_ASYNC
));
7656 SET(bp
->nb_flags
, NB_READ
);
7658 nfsm_chain_get_opaque(error
, &nmrep
, retlen
, bp
->nb_data
);
7660 bp
->nb_error
= error
;
7661 SET(bp
->nb_flags
, NB_ERROR
);
7664 bp
->nb_endio
= rlen
;
7665 if ((retlen
> 0) && (bp
->nb_endio
< (int)retlen
))
7666 bp
->nb_endio
= retlen
;
7667 if (eof
|| (retlen
== 0)) {
7668 /* zero out the remaining data (up to EOF) */
7669 off_t rpcrem
, eofrem
, rem
;
7670 rpcrem
= (rlen
- retlen
);
7671 eofrem
= anp
->n_size
- (NBOFF(bp
) + retlen
);
7672 rem
= (rpcrem
< eofrem
) ? rpcrem
: eofrem
;
7674 bzero(bp
->nb_data
+ retlen
, rem
);
7675 } else if ((retlen
< rlen
) && !ISSET(bp
->nb_flags
, NB_ERROR
)) {
7676 /* ugh... short read ... just invalidate for now... */
7677 SET(bp
->nb_flags
, NB_INVAL
);
7680 nfs_buf_read_finish(bp
);
7681 microuptime(&anp
->n_lastio
);
7684 nfs_buf_release(bp
, 1);
7686 error
= 0; /* ignore any transient error in processing the prefetch */
7688 if (adnp
&& !adbusyerror
) {
7689 nfs_node_clear_busy(adnp
);
7690 adbusyerror
= ENOENT
;
7693 nfs_node_clear_busy(np
);
7697 vnode_put(NFSTOV(adnp
));
7698 if (error
&& *anpp
) {
7699 vnode_put(NFSTOV(*anpp
));
7702 nfsm_chain_cleanup(&nmreq
);
7703 nfsm_chain_cleanup(&nmrep
);
7708 * Remove a named attribute.
7711 nfs4_named_attr_remove(nfsnode_t np
, nfsnode_t anp
, const char *name
, vfs_context_t ctx
)
7713 nfsnode_t adnp
= NULL
;
7714 struct nfsmount
*nmp
;
7715 struct componentname cn
;
7716 struct vnop_remove_args vra
;
7717 int error
, putanp
= 0;
7720 if (nfs_mount_gone(nmp
))
7723 bzero(&cn
, sizeof(cn
));
7724 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7725 cn
.cn_namelen
= strlen(name
);
7726 cn
.cn_nameiop
= DELETE
;
7730 error
= nfs4_named_attr_get(np
, &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7731 0, ctx
, &anp
, NULL
);
7732 if ((!error
&& !anp
) || (error
== ENOATTR
))
7736 vnode_put(NFSTOV(anp
));
7744 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7746 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
7747 nfs_node_clear_busy(np
);
7753 vra
.a_desc
= &vnop_remove_desc
;
7754 vra
.a_dvp
= NFSTOV(adnp
);
7755 vra
.a_vp
= NFSTOV(anp
);
7758 vra
.a_context
= ctx
;
7759 error
= nfs_vnop_remove(&vra
);
7762 vnode_put(NFSTOV(adnp
));
7764 vnode_put(NFSTOV(anp
));
7770 struct vnop_getxattr_args
/* {
7771 struct vnodeop_desc *a_desc;
7773 const char * a_name;
7777 vfs_context_t a_context;
7780 vfs_context_t ctx
= ap
->a_context
;
7781 struct nfsmount
*nmp
;
7782 struct nfs_vattr nvattr
;
7783 struct componentname cn
;
7785 int error
= 0, isrsrcfork
;
7787 nmp
= VTONMP(ap
->a_vp
);
7788 if (nfs_mount_gone(nmp
))
7791 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7793 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
7796 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7797 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7800 bzero(&cn
, sizeof(cn
));
7801 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
7802 cn
.cn_namelen
= strlen(ap
->a_name
);
7803 cn
.cn_nameiop
= LOOKUP
;
7804 cn
.cn_flags
= MAKEENTRY
;
7806 /* we'll normally try to prefetch data for xattrs... the resource fork is really a stream */
7807 isrsrcfork
= (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7809 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7810 !isrsrcfork
? NFS_GET_NAMED_ATTR_PREFETCH
: 0, ctx
, &anp
, NULL
);
7811 if ((!error
&& !anp
) || (error
== ENOENT
))
7815 error
= nfs_bioread(anp
, ap
->a_uio
, 0, ctx
);
7817 *ap
->a_size
= anp
->n_size
;
7820 vnode_put(NFSTOV(anp
));
7826 struct vnop_setxattr_args
/* {
7827 struct vnodeop_desc *a_desc;
7829 const char * a_name;
7832 vfs_context_t a_context;
7835 vfs_context_t ctx
= ap
->a_context
;
7836 int options
= ap
->a_options
;
7837 uio_t uio
= ap
->a_uio
;
7838 const char *name
= ap
->a_name
;
7839 struct nfsmount
*nmp
;
7840 struct componentname cn
;
7841 nfsnode_t anp
= NULL
;
7842 int error
= 0, closeerror
= 0, flags
, isrsrcfork
, isfinderinfo
, empty
= 0, i
;
7843 #define FINDERINFOSIZE 32
7844 uint8_t finfo
[FINDERINFOSIZE
];
7846 struct nfs_open_file
*nofp
= NULL
;
7847 char uio_buf
[ UIO_SIZEOF(1) ];
7849 struct vnop_write_args vwa
;
7851 nmp
= VTONMP(ap
->a_vp
);
7852 if (nfs_mount_gone(nmp
))
7855 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7858 if ((options
& XATTR_CREATE
) && (options
& XATTR_REPLACE
))
7861 /* XXX limitation based on need to back up uio on short write */
7862 if (uio_iovcnt(uio
) > 1) {
7863 printf("nfs4_vnop_setxattr: iovcnt > 1\n");
7867 bzero(&cn
, sizeof(cn
));
7868 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7869 cn
.cn_namelen
= strlen(name
);
7870 cn
.cn_nameiop
= CREATE
;
7871 cn
.cn_flags
= MAKEENTRY
;
7873 isfinderinfo
= (bcmp(name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0);
7874 isrsrcfork
= isfinderinfo
? 0 : (bcmp(name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7876 uio_setoffset(uio
, 0);
7878 if (uio_resid(uio
) != sizeof(finfo
))
7880 error
= uiomove((char*)&finfo
, sizeof(finfo
), uio
);
7883 /* setting a FinderInfo of all zeroes means remove the FinderInfo */
7885 for (i
=0, finfop
=(uint32_t*)&finfo
; i
< (int)(sizeof(finfo
)/sizeof(uint32_t)); i
++)
7890 if (empty
&& !(options
& (XATTR_CREATE
|XATTR_REPLACE
))) {
7891 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7892 if (error
== ENOENT
)
7896 /* first, let's see if we get a create/replace error */
7900 * create/open the xattr
7902 * We need to make sure not to create it if XATTR_REPLACE.
7903 * For all xattrs except the resource fork, we also want to
7904 * truncate the xattr to remove any current data. We'll do
7905 * that by setting the size to 0 on create/open.
7908 if (!(options
& XATTR_REPLACE
))
7909 flags
|= NFS_GET_NAMED_ATTR_CREATE
;
7910 if (options
& XATTR_CREATE
)
7911 flags
|= NFS_GET_NAMED_ATTR_CREATE_GUARDED
;
7913 flags
|= NFS_GET_NAMED_ATTR_TRUNCATE
;
7915 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
7916 flags
, ctx
, &anp
, &nofp
);
7921 /* grab the open state from the get/create/open */
7922 if (nofp
&& !(error
= nfs_open_file_set_busy(nofp
, NULL
))) {
7923 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
7924 nofp
->nof_creator
= NULL
;
7925 nfs_open_file_clear_busy(nofp
);
7928 /* Setting an empty FinderInfo really means remove it, skip to the close/remove */
7929 if (isfinderinfo
&& empty
)
7933 * Write the data out and flush.
7935 * For FinderInfo, we've already copied the data to finfo, so do I/O from there.
7937 vwa
.a_desc
= &vnop_write_desc
;
7938 vwa
.a_vp
= NFSTOV(anp
);
7941 vwa
.a_context
= ctx
;
7943 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
, &uio_buf
, sizeof(uio_buf
));
7944 uio_addiov(auio
, (uintptr_t)&finfo
, sizeof(finfo
));
7946 } else if (uio_resid(uio
) > 0) {
7950 error
= nfs_vnop_write(&vwa
);
7952 error
= nfs_flush(anp
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
7955 /* Close the xattr. */
7957 int busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7958 closeerror
= nfs_close(anp
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
7960 nfs_open_file_clear_busy(nofp
);
7962 if (!error
&& isfinderinfo
&& empty
) { /* Setting an empty FinderInfo really means remove it */
7963 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7964 if (error
== ENOENT
)
7971 vnode_put(NFSTOV(anp
));
7972 if (error
== ENOENT
)
7978 nfs4_vnop_removexattr(
7979 struct vnop_removexattr_args
/* {
7980 struct vnodeop_desc *a_desc;
7982 const char * a_name;
7984 vfs_context_t a_context;
7987 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7990 if (nfs_mount_gone(nmp
))
7992 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7995 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), NULL
, ap
->a_name
, ap
->a_context
);
7996 if (error
== ENOENT
)
8002 nfs4_vnop_listxattr(
8003 struct vnop_listxattr_args
/* {
8004 struct vnodeop_desc *a_desc;
8009 vfs_context_t a_context;
8012 vfs_context_t ctx
= ap
->a_context
;
8013 nfsnode_t np
= VTONFS(ap
->a_vp
);
8014 uio_t uio
= ap
->a_uio
;
8015 nfsnode_t adnp
= NULL
;
8016 struct nfsmount
*nmp
;
8018 struct nfs_vattr nvattr
;
8019 uint64_t cookie
, nextcookie
, lbn
= 0;
8020 struct nfsbuf
*bp
= NULL
;
8021 struct nfs_dir_buf_header
*ndbhp
;
8022 struct direntry
*dp
;
8024 nmp
= VTONMP(ap
->a_vp
);
8025 if (nfs_mount_gone(nmp
))
8028 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8031 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
8034 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
8035 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
8038 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
8040 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
8041 nfs_node_clear_busy(np
);
8045 if ((error
= nfs_node_lock(adnp
)))
8048 if (adnp
->n_flag
& NNEEDINVALIDATE
) {
8049 adnp
->n_flag
&= ~NNEEDINVALIDATE
;
8051 nfs_node_unlock(adnp
);
8052 error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
8054 error
= nfs_node_lock(adnp
);
8060 * check for need to invalidate when (re)starting at beginning
8062 if (adnp
->n_flag
& NMODIFIED
) {
8064 nfs_node_unlock(adnp
);
8065 if ((error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1)))
8068 nfs_node_unlock(adnp
);
8070 /* nfs_getattr() will check changed and purge caches */
8071 if ((error
= nfs_getattr(adnp
, &nvattr
, ctx
, NGA_UNCACHED
)))
8074 if (uio
&& (uio_resid(uio
) == 0))
8078 nextcookie
= lbn
= 0;
8080 while (!error
&& !done
) {
8081 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
8082 cookie
= nextcookie
;
8084 error
= nfs_buf_get(adnp
, lbn
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
8087 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
8088 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
8089 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
8090 ndbhp
->ndbh_flags
= 0;
8091 ndbhp
->ndbh_count
= 0;
8092 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
8093 ndbhp
->ndbh_ncgen
= adnp
->n_ncgen
;
8095 error
= nfs_buf_readdir(bp
, ctx
);
8096 if (error
== NFSERR_DIRBUFDROPPED
)
8099 nfs_buf_release(bp
, 1);
8100 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
8101 if (!nfs_node_lock(adnp
)) {
8103 nfs_node_unlock(adnp
);
8105 nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
8106 if (error
== NFSERR_BAD_COOKIE
)
8113 /* go through all the entries copying/counting */
8114 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
8115 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
8116 if (!xattr_protected(dp
->d_name
)) {
8118 *ap
->a_size
+= dp
->d_namlen
+ 1;
8119 } else if (uio_resid(uio
) < (dp
->d_namlen
+ 1)) {
8122 error
= uiomove(dp
->d_name
, dp
->d_namlen
+1, uio
);
8123 if (error
&& (error
!= EFAULT
))
8127 nextcookie
= dp
->d_seekoff
;
8128 dp
= NFS_DIRENTRY_NEXT(dp
);
8131 if (i
== ndbhp
->ndbh_count
) {
8132 /* hit end of buffer, move to next buffer */
8134 /* if we also hit EOF, we're done */
8135 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
))
8138 if (!error
&& !done
&& (nextcookie
== cookie
)) {
8139 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
8142 nfs_buf_release(bp
, 1);
8146 vnode_put(NFSTOV(adnp
));
8152 nfs4_vnop_getnamedstream(
8153 struct vnop_getnamedstream_args
/* {
8154 struct vnodeop_desc *a_desc;
8158 enum nsoperation a_operation;
8160 vfs_context_t a_context;
8163 vfs_context_t ctx
= ap
->a_context
;
8164 struct nfsmount
*nmp
;
8165 struct nfs_vattr nvattr
;
8166 struct componentname cn
;
8170 nmp
= VTONMP(ap
->a_vp
);
8171 if (nfs_mount_gone(nmp
))
8174 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8176 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
8179 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
8180 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
8183 bzero(&cn
, sizeof(cn
));
8184 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8185 cn
.cn_namelen
= strlen(ap
->a_name
);
8186 cn
.cn_nameiop
= LOOKUP
;
8187 cn
.cn_flags
= MAKEENTRY
;
8189 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
8190 0, ctx
, &anp
, NULL
);
8191 if ((!error
&& !anp
) || (error
== ENOENT
))
8194 *ap
->a_svpp
= NFSTOV(anp
);
8196 vnode_put(NFSTOV(anp
));
8201 nfs4_vnop_makenamedstream(
8202 struct vnop_makenamedstream_args
/* {
8203 struct vnodeop_desc *a_desc;
8208 vfs_context_t a_context;
8211 vfs_context_t ctx
= ap
->a_context
;
8212 struct nfsmount
*nmp
;
8213 struct componentname cn
;
8217 nmp
= VTONMP(ap
->a_vp
);
8218 if (nfs_mount_gone(nmp
))
8221 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8224 bzero(&cn
, sizeof(cn
));
8225 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8226 cn
.cn_namelen
= strlen(ap
->a_name
);
8227 cn
.cn_nameiop
= CREATE
;
8228 cn
.cn_flags
= MAKEENTRY
;
8230 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
8231 NFS_GET_NAMED_ATTR_CREATE
, ctx
, &anp
, NULL
);
8232 if ((!error
&& !anp
) || (error
== ENOENT
))
8235 *ap
->a_svpp
= NFSTOV(anp
);
8237 vnode_put(NFSTOV(anp
));
8242 nfs4_vnop_removenamedstream(
8243 struct vnop_removenamedstream_args
/* {
8244 struct vnodeop_desc *a_desc;
8249 vfs_context_t a_context;
8252 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
8253 nfsnode_t np
= ap
->a_vp
? VTONFS(ap
->a_vp
) : NULL
;
8254 nfsnode_t anp
= ap
->a_svp
? VTONFS(ap
->a_svp
) : NULL
;
8256 if (nfs_mount_gone(nmp
))
8260 * Given that a_svp is a named stream, checking for
8261 * named attribute support is kinda pointless.
8263 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8266 return (nfs4_named_attr_remove(np
, anp
, ap
->a_name
, ap
->a_context
));