2 * Copyright (c) 2006-2011 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", 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 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
161 slot
= nfs_node_access_slot(np
, uid
, 1);
162 np
->n_accessuid
[slot
] = uid
;
164 np
->n_accessstamp
[slot
] = now
.tv_sec
;
165 np
->n_access
[slot
] = access_result
;
167 /* pass back the access returned with this request */
168 *access
= np
->n_access
[slot
];
172 nfsm_chain_cleanup(&nmreq
);
173 nfsm_chain_cleanup(&nmrep
);
185 struct nfs_vattr
*nvap
,
188 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
189 int error
= 0, status
, nfsvers
, numops
, rpcflags
= 0, acls
;
190 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
191 struct nfsm_chain nmreq
, nmrep
;
192 struct nfsreq_secinfo_args si
;
194 if (nfs_mount_gone(nmp
))
196 nfsvers
= nmp
->nm_vers
;
197 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
199 if (np
&& (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)) {
200 nfs4_default_attrs_for_referral_trigger(VTONFS(np
->n_parent
), NULL
, 0, nvap
, NULL
);
204 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
205 rpcflags
= R_RECOVER
;
207 if (flags
& NGA_SOFT
) /* Return ETIMEDOUT if server not responding */
210 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
211 nfsm_chain_null(&nmreq
);
212 nfsm_chain_null(&nmrep
);
216 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
217 nfsm_chain_add_compound_header(error
, &nmreq
, "getattr", numops
);
219 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
220 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fhp
, fhsize
);
222 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
223 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
224 if ((flags
& NGA_ACL
) && acls
)
225 NFS_BITMAP_SET(bitmap
, NFS_FATTR_ACL
);
226 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
227 nfsm_chain_build_done(error
, &nmreq
);
228 nfsm_assert(error
, (numops
== 0), EPROTO
);
230 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC4_COMPOUND
,
231 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
232 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
234 nfsm_chain_skip_tag(error
, &nmrep
);
235 nfsm_chain_get_32(error
, &nmrep
, numops
);
236 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
237 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
239 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
241 if ((flags
& NGA_ACL
) && acls
&& !NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_ACL
)) {
242 /* we asked for the ACL but didn't get one... assume there isn't one */
243 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
244 nvap
->nva_acl
= NULL
;
247 nfsm_chain_cleanup(&nmreq
);
248 nfsm_chain_cleanup(&nmrep
);
253 nfs4_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
255 struct nfsmount
*nmp
;
256 int error
= 0, lockerror
= ENOENT
, status
, numops
;
259 struct nfsm_chain nmreq
, nmrep
;
260 struct nfsreq_secinfo_args si
;
263 if (nfs_mount_gone(nmp
))
265 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
267 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
268 nfsm_chain_null(&nmreq
);
269 nfsm_chain_null(&nmrep
);
271 // PUTFH, GETATTR, READLINK
273 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
274 nfsm_chain_add_compound_header(error
, &nmreq
, "readlink", numops
);
276 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
277 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
279 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
280 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
282 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READLINK
);
283 nfsm_chain_build_done(error
, &nmreq
);
284 nfsm_assert(error
, (numops
== 0), EPROTO
);
286 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
288 if ((lockerror
= nfs_node_lock(np
)))
290 nfsm_chain_skip_tag(error
, &nmrep
);
291 nfsm_chain_get_32(error
, &nmrep
, numops
);
292 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
293 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
294 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
295 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READLINK
);
296 nfsm_chain_get_32(error
, &nmrep
, len
);
298 if (len
>= *buflenp
) {
299 if (np
->n_size
&& (np
->n_size
< *buflenp
))
304 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
310 nfsm_chain_cleanup(&nmreq
);
311 nfsm_chain_cleanup(&nmrep
);
322 struct nfsreq_cbinfo
*cb
,
323 struct nfsreq
**reqp
)
325 struct nfsmount
*nmp
;
326 int error
= 0, nfsvers
, numops
;
328 struct nfsm_chain nmreq
;
329 struct nfsreq_secinfo_args si
;
332 if (nfs_mount_gone(nmp
))
334 nfsvers
= nmp
->nm_vers
;
335 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
338 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
339 nfsm_chain_null(&nmreq
);
341 // PUTFH, READ, GETATTR
343 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
344 nfsm_chain_add_compound_header(error
, &nmreq
, "read", numops
);
346 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
347 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
349 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
350 nfs_get_stateid(np
, thd
, cred
, &stateid
);
351 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
352 nfsm_chain_add_64(error
, &nmreq
, offset
);
353 nfsm_chain_add_32(error
, &nmreq
, len
);
355 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
356 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
357 nfsm_chain_build_done(error
, &nmreq
);
358 nfsm_assert(error
, (numops
== 0), EPROTO
);
360 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
362 nfsm_chain_cleanup(&nmreq
);
367 nfs4_read_rpc_async_finish(
374 struct nfsmount
*nmp
;
375 int error
= 0, lockerror
, nfsvers
, numops
, status
, eof
= 0;
378 struct nfsm_chain nmrep
;
381 if (nfs_mount_gone(nmp
)) {
382 nfs_request_async_cancel(req
);
385 nfsvers
= nmp
->nm_vers
;
387 nfsm_chain_null(&nmrep
);
389 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
390 if (error
== EINPROGRESS
) /* async request restarted */
393 if ((lockerror
= nfs_node_lock(np
)))
395 nfsm_chain_skip_tag(error
, &nmrep
);
396 nfsm_chain_get_32(error
, &nmrep
, numops
);
397 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
398 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
399 nfsm_chain_get_32(error
, &nmrep
, eof
);
400 nfsm_chain_get_32(error
, &nmrep
, retlen
);
402 *lenp
= MIN(retlen
, *lenp
);
403 error
= nfsm_chain_get_uio(&nmrep
, *lenp
, uio
);
405 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
406 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
414 nfsm_chain_cleanup(&nmrep
);
415 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
416 microuptime(&np
->n_lastio
);
421 nfs4_write_rpc_async(
428 struct nfsreq_cbinfo
*cb
,
429 struct nfsreq
**reqp
)
431 struct nfsmount
*nmp
;
433 int error
= 0, nfsvers
, numops
;
435 struct nfsm_chain nmreq
;
436 struct nfsreq_secinfo_args si
;
439 if (nfs_mount_gone(nmp
))
441 nfsvers
= nmp
->nm_vers
;
442 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
445 /* for async mounts, don't bother sending sync write requests */
446 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
447 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
448 iomode
= NFS_WRITE_UNSTABLE
;
450 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
451 nfsm_chain_null(&nmreq
);
453 // PUTFH, WRITE, GETATTR
455 nfsm_chain_build_alloc_init(error
, &nmreq
, 25 * NFSX_UNSIGNED
+ len
);
456 nfsm_chain_add_compound_header(error
, &nmreq
, "write", numops
);
458 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
459 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
461 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_WRITE
);
462 nfs_get_stateid(np
, thd
, cred
, &stateid
);
463 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
464 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
465 nfsm_chain_add_32(error
, &nmreq
, iomode
);
466 nfsm_chain_add_32(error
, &nmreq
, len
);
468 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
470 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
471 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
472 nfsm_chain_build_done(error
, &nmreq
);
473 nfsm_assert(error
, (numops
== 0), EPROTO
);
476 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
478 nfsm_chain_cleanup(&nmreq
);
483 nfs4_write_rpc_async_finish(
490 struct nfsmount
*nmp
;
491 int error
= 0, lockerror
= ENOENT
, nfsvers
, numops
, status
;
492 int committed
= NFS_WRITE_FILESYNC
;
494 u_int64_t xid
, wverf
;
496 struct nfsm_chain nmrep
;
499 if (nfs_mount_gone(nmp
)) {
500 nfs_request_async_cancel(req
);
503 nfsvers
= nmp
->nm_vers
;
505 nfsm_chain_null(&nmrep
);
507 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
508 if (error
== EINPROGRESS
) /* async request restarted */
511 if (nfs_mount_gone(nmp
))
513 if (!error
&& (lockerror
= nfs_node_lock(np
)))
515 nfsm_chain_skip_tag(error
, &nmrep
);
516 nfsm_chain_get_32(error
, &nmrep
, numops
);
517 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
518 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_WRITE
);
519 nfsm_chain_get_32(error
, &nmrep
, rlen
);
524 nfsm_chain_get_32(error
, &nmrep
, committed
);
525 nfsm_chain_get_64(error
, &nmrep
, wverf
);
529 lck_mtx_lock(&nmp
->nm_lock
);
530 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
531 nmp
->nm_verf
= wverf
;
532 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
533 } else if (nmp
->nm_verf
!= wverf
) {
534 nmp
->nm_verf
= wverf
;
536 lck_mtx_unlock(&nmp
->nm_lock
);
537 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
538 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
542 nfsm_chain_cleanup(&nmrep
);
543 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
544 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
545 committed
= NFS_WRITE_FILESYNC
;
546 *iomodep
= committed
;
547 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
548 microuptime(&np
->n_lastio
);
560 int error
= 0, lockerror
= ENOENT
, remove_error
= 0, status
;
561 struct nfsmount
*nmp
;
564 struct nfsm_chain nmreq
, nmrep
;
565 struct nfsreq_secinfo_args si
;
568 if (nfs_mount_gone(nmp
))
570 nfsvers
= nmp
->nm_vers
;
571 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
573 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
575 nfsm_chain_null(&nmreq
);
576 nfsm_chain_null(&nmrep
);
578 // PUTFH, REMOVE, GETATTR
580 nfsm_chain_build_alloc_init(error
, &nmreq
, 17 * NFSX_UNSIGNED
+ namelen
);
581 nfsm_chain_add_compound_header(error
, &nmreq
, "remove", numops
);
583 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
584 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
586 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_REMOVE
);
587 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
589 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
590 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
591 nfsm_chain_build_done(error
, &nmreq
);
592 nfsm_assert(error
, (numops
== 0), EPROTO
);
595 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, &nmrep
, &xid
, &status
);
597 if ((lockerror
= nfs_node_lock(dnp
)))
599 nfsm_chain_skip_tag(error
, &nmrep
);
600 nfsm_chain_get_32(error
, &nmrep
, numops
);
601 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
602 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_REMOVE
);
603 remove_error
= error
;
604 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
605 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
606 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
607 if (error
&& !lockerror
)
608 NATTRINVALIDATE(dnp
);
610 nfsm_chain_cleanup(&nmreq
);
611 nfsm_chain_cleanup(&nmrep
);
614 dnp
->n_flag
|= NMODIFIED
;
615 nfs_node_unlock(dnp
);
617 if (error
== NFSERR_GRACE
) {
618 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
622 return (remove_error
);
635 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
636 struct nfsmount
*nmp
;
637 u_int64_t xid
, savedxid
;
638 struct nfsm_chain nmreq
, nmrep
;
639 struct nfsreq_secinfo_args si
;
641 nmp
= NFSTONMP(fdnp
);
642 if (nfs_mount_gone(nmp
))
644 nfsvers
= nmp
->nm_vers
;
645 if (fdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
647 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
650 NFSREQ_SECINFO_SET(&si
, fdnp
, NULL
, 0, NULL
, 0);
651 nfsm_chain_null(&nmreq
);
652 nfsm_chain_null(&nmrep
);
654 // PUTFH(FROM), SAVEFH, PUTFH(TO), RENAME, GETATTR(TO), RESTOREFH, GETATTR(FROM)
656 nfsm_chain_build_alloc_init(error
, &nmreq
, 30 * NFSX_UNSIGNED
+ fnamelen
+ tnamelen
);
657 nfsm_chain_add_compound_header(error
, &nmreq
, "rename", numops
);
659 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
660 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
662 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
664 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
665 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
667 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RENAME
);
668 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
669 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
671 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
672 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
674 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
676 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
677 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, fdnp
);
678 nfsm_chain_build_done(error
, &nmreq
);
679 nfsm_assert(error
, (numops
== 0), EPROTO
);
682 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
684 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
686 nfsm_chain_skip_tag(error
, &nmrep
);
687 nfsm_chain_get_32(error
, &nmrep
, numops
);
688 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
689 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
690 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
691 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RENAME
);
692 nfsm_chain_check_change_info(error
, &nmrep
, fdnp
);
693 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
694 /* directory attributes: if we don't get them, make sure to invalidate */
695 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
697 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
698 if (error
&& !lockerror
)
699 NATTRINVALIDATE(tdnp
);
700 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
701 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
703 nfsm_chain_loadattr(error
, &nmrep
, fdnp
, nfsvers
, &xid
);
704 if (error
&& !lockerror
)
705 NATTRINVALIDATE(fdnp
);
707 nfsm_chain_cleanup(&nmreq
);
708 nfsm_chain_cleanup(&nmrep
);
710 fdnp
->n_flag
|= NMODIFIED
;
711 tdnp
->n_flag
|= NMODIFIED
;
712 nfs_node_unlock2(fdnp
, tdnp
);
718 * NFS V4 readdir RPC.
721 nfs4_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
723 struct nfsmount
*nmp
;
724 int error
= 0, lockerror
, nfsvers
, namedattr
, rdirplus
, bigcookies
, numops
;
725 int i
, status
, more_entries
= 1, eof
, bp_dropped
= 0;
726 uint32_t nmreaddirsize
, nmrsize
;
727 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
728 uint64_t cookie
, lastcookie
, xid
, savedxid
;
729 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
731 struct nfs_vattr nvattr
, *nvattrp
;
732 struct nfs_dir_buf_header
*ndbhp
;
734 char *padstart
, padlen
;
736 uint32_t entry_attrs
[NFS_ATTR_BITMAP_LEN
];
738 struct nfsreq_secinfo_args si
;
741 if (nfs_mount_gone(nmp
))
743 nfsvers
= nmp
->nm_vers
;
744 nmreaddirsize
= nmp
->nm_readdirsize
;
745 nmrsize
= nmp
->nm_rsize
;
746 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
747 namedattr
= (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) ? 1 : 0;
748 rdirplus
= (NMFLAG(nmp
, RDIRPLUS
) || namedattr
) ? 1 : 0;
749 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
751 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
754 * Set up attribute request for entries.
755 * For READDIRPLUS functionality, get everything.
756 * Otherwise, just get what we need for struct direntry.
760 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, entry_attrs
);
761 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEHANDLE
);
764 NFS_CLEAR_ATTRIBUTES(entry_attrs
);
765 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_TYPE
);
766 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEID
);
767 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_MOUNTED_ON_FILEID
);
769 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_RDATTR_ERROR
);
771 /* lock to protect access to cookie verifier */
772 if ((lockerror
= nfs_node_lock(dnp
)))
775 /* determine cookie to use, and move dp to the right offset */
776 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
777 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
778 if (ndbhp
->ndbh_count
) {
779 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
780 dp
= NFS_DIRENTRY_NEXT(dp
);
781 cookie
= dp
->d_seekoff
;
782 dp
= NFS_DIRENTRY_NEXT(dp
);
784 cookie
= bp
->nb_lblkno
;
785 /* increment with every buffer read */
786 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
791 * The NFS client is responsible for the "." and ".." entries in the
792 * directory. So, we put them at the start of the first buffer.
793 * Don't bother for attribute directories.
795 if (((bp
->nb_lblkno
== 0) && (ndbhp
->ndbh_count
== 0)) &&
796 !(dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)) {
798 fhlen
= rdirplus
? fh
.fh_len
+ 1 : 0;
799 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
802 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
804 bzero(&dp
->d_name
[namlen
+1], xlen
);
805 dp
->d_namlen
= namlen
;
806 strlcpy(dp
->d_name
, ".", namlen
+1);
807 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
809 dp
->d_reclen
= reclen
;
811 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
812 dp
= NFS_DIRENTRY_NEXT(dp
);
813 padlen
= (char*)dp
- padstart
;
815 bzero(padstart
, padlen
);
816 if (rdirplus
) /* zero out attributes */
817 bzero(NFS_DIR_BUF_NVATTR(bp
, 0), sizeof(struct nfs_vattr
));
821 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
823 bzero(&dp
->d_name
[namlen
+1], xlen
);
824 dp
->d_namlen
= namlen
;
825 strlcpy(dp
->d_name
, "..", namlen
+1);
827 dp
->d_fileno
= VTONFS(dnp
->n_parent
)->n_vattr
.nva_fileid
;
829 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
831 dp
->d_reclen
= reclen
;
833 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
834 dp
= NFS_DIRENTRY_NEXT(dp
);
835 padlen
= (char*)dp
- padstart
;
837 bzero(padstart
, padlen
);
838 if (rdirplus
) /* zero out attributes */
839 bzero(NFS_DIR_BUF_NVATTR(bp
, 1), sizeof(struct nfs_vattr
));
841 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
842 ndbhp
->ndbh_count
= 2;
846 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
847 * the buffer is full (or we hit EOF). Then put the remainder of the
848 * results in the next buffer(s).
850 nfsm_chain_null(&nmreq
);
851 nfsm_chain_null(&nmrep
);
852 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
854 // PUTFH, GETATTR, READDIR
856 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
857 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, numops
);
859 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
860 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
862 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
863 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
865 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READDIR
);
866 nfsm_chain_add_64(error
, &nmreq
, (cookie
<= 2) ? 0 : cookie
);
867 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
868 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
869 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
870 nfsm_chain_add_bitmap_supported(error
, &nmreq
, entry_attrs
, nmp
, dnp
);
871 nfsm_chain_build_done(error
, &nmreq
);
872 nfsm_assert(error
, (numops
== 0), EPROTO
);
873 nfs_node_unlock(dnp
);
875 error
= nfs_request(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
877 if ((lockerror
= nfs_node_lock(dnp
)))
881 nfsm_chain_skip_tag(error
, &nmrep
);
882 nfsm_chain_get_32(error
, &nmrep
, numops
);
883 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
884 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
885 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
886 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READDIR
);
887 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
888 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
891 nfs_node_unlock(dnp
);
899 /* loop through the entries packing them into the buffer */
900 while (more_entries
) {
901 /* Entry: COOKIE, NAME, FATTR */
902 nfsm_chain_get_64(error
, &nmrep
, cookie
);
903 nfsm_chain_get_32(error
, &nmrep
, namlen
);
905 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
906 /* we've got a big cookie, make sure flag is set */
907 lck_mtx_lock(&nmp
->nm_lock
);
908 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
909 lck_mtx_unlock(&nmp
->nm_lock
);
912 /* just truncate names that don't fit in direntry.d_name */
917 if (namlen
> (sizeof(dp
->d_name
)-1)) {
918 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
919 namlen
= sizeof(dp
->d_name
) - 1;
923 /* guess that fh size will be same as parent */
924 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
925 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
926 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
927 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
928 space_needed
= reclen
+ attrlen
;
929 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
930 if (space_needed
> space_free
) {
932 * We still have entries to pack, but we've
933 * run out of room in the current buffer.
934 * So we need to move to the next buffer.
935 * The block# for the next buffer is the
936 * last cookie in the current buffer.
939 ndbhp
->ndbh_flags
|= NDB_FULL
;
940 nfs_buf_release(bp
, 0);
943 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
945 /* initialize buffer */
946 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
947 ndbhp
->ndbh_flags
= 0;
948 ndbhp
->ndbh_count
= 0;
949 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
950 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
951 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
952 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
953 /* increment with every buffer read */
954 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
957 dp
->d_fileno
= cookie
; /* placeholder */
958 dp
->d_seekoff
= cookie
;
959 dp
->d_namlen
= namlen
;
960 dp
->d_reclen
= reclen
;
961 dp
->d_type
= DT_UNKNOWN
;
962 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
964 dp
->d_name
[namlen
] = '\0';
966 nfsm_chain_adv(error
, &nmrep
,
967 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
969 nvattrp
= rdirplus
? NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
) : &nvattr
;
970 error
= nfs4_parsefattr(&nmrep
, NULL
, nvattrp
, &fh
, NULL
, NULL
);
971 if (!error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
)) {
972 /* we do NOT want ACLs returned to us here */
973 NFS_BITMAP_CLR(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
);
974 if (nvattrp
->nva_acl
) {
975 kauth_acl_free(nvattrp
->nva_acl
);
976 nvattrp
->nva_acl
= NULL
;
979 if (error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_RDATTR_ERROR
)) {
980 /* OK, we may not have gotten all of the attributes but we will use what we can. */
981 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
982 /* set this up to look like a referral trigger */
983 nfs4_default_attrs_for_referral_trigger(dnp
, dp
->d_name
, namlen
, nvattrp
, &fh
);
987 /* check for more entries after this one */
988 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
991 /* Skip any "." and ".." entries returned from server. */
992 /* Also skip any bothersome named attribute entries. */
993 if (((dp
->d_name
[0] == '.') && ((namlen
== 1) || ((namlen
== 2) && (dp
->d_name
[1] == '.')))) ||
994 (namedattr
&& (namlen
== 11) && (!strcmp(dp
->d_name
, "SUNWattr_ro") || !strcmp(dp
->d_name
, "SUNWattr_rw")))) {
999 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_TYPE
))
1000 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
1001 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEID
))
1002 dp
->d_fileno
= nvattrp
->nva_fileid
;
1004 /* fileid is already in d_fileno, so stash xid in attrs */
1005 nvattrp
->nva_fileid
= savedxid
;
1006 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
1007 fhlen
= fh
.fh_len
+ 1;
1008 xlen
= fhlen
+ sizeof(time_t);
1009 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1010 space_needed
= reclen
+ attrlen
;
1011 if (space_needed
> space_free
) {
1012 /* didn't actually have the room... move on to next buffer */
1016 /* pack the file handle into the record */
1017 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
1018 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
1020 /* mark the file handle invalid */
1022 fhlen
= fh
.fh_len
+ 1;
1023 xlen
= fhlen
+ sizeof(time_t);
1024 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1025 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
1027 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
1028 dp
->d_reclen
= reclen
;
1030 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
1031 ndbhp
->ndbh_count
++;
1032 lastcookie
= cookie
;
1034 /* advance to next direntry in buffer */
1035 dp
= NFS_DIRENTRY_NEXT(dp
);
1036 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
1037 /* zero out the pad bytes */
1038 padlen
= (char*)dp
- padstart
;
1040 bzero(padstart
, padlen
);
1042 /* Finally, get the eof boolean */
1043 nfsm_chain_get_32(error
, &nmrep
, eof
);
1046 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
1047 nfs_node_lock_force(dnp
);
1048 dnp
->n_eofcookie
= lastcookie
;
1049 nfs_node_unlock(dnp
);
1054 nfs_buf_release(bp
, 0);
1058 if ((lockerror
= nfs_node_lock(dnp
)))
1061 nfsm_chain_cleanup(&nmrep
);
1062 nfsm_chain_null(&nmreq
);
1065 if (bp_dropped
&& bp
)
1066 nfs_buf_release(bp
, 0);
1068 nfs_node_unlock(dnp
);
1069 nfsm_chain_cleanup(&nmreq
);
1070 nfsm_chain_cleanup(&nmrep
);
1071 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
1075 nfs4_lookup_rpc_async(
1080 struct nfsreq
**reqp
)
1082 int error
= 0, isdotdot
= 0, nfsvers
, numops
;
1083 struct nfsm_chain nmreq
;
1084 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1085 struct nfsmount
*nmp
;
1086 struct nfsreq_secinfo_args si
;
1088 nmp
= NFSTONMP(dnp
);
1089 if (nfs_mount_gone(nmp
))
1091 nfsvers
= nmp
->nm_vers
;
1092 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1095 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2)) {
1097 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
1099 NFSREQ_SECINFO_SET(&si
, dnp
, dnp
->n_fhp
, dnp
->n_fhsize
, name
, namelen
);
1102 nfsm_chain_null(&nmreq
);
1104 // PUTFH, GETATTR, LOOKUP(P), GETFH, GETATTR (FH)
1106 nfsm_chain_build_alloc_init(error
, &nmreq
, 20 * NFSX_UNSIGNED
+ namelen
);
1107 nfsm_chain_add_compound_header(error
, &nmreq
, "lookup", numops
);
1109 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1110 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
1112 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1113 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
1116 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUPP
);
1118 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
1119 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
1122 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETFH
);
1124 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1125 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1126 /* some ".zfs" directories can't handle being asked for some attributes */
1127 if ((dnp
->n_flag
& NISDOTZFS
) && !isdotdot
)
1128 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1129 if ((dnp
->n_flag
& NISDOTZFSCHILD
) && isdotdot
)
1130 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1131 if (((namelen
== 4) && (name
[0] == '.') && (name
[1] == 'z') && (name
[2] == 'f') && (name
[3] == 's')))
1132 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1133 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
1134 nfsm_chain_build_done(error
, &nmreq
);
1135 nfsm_assert(error
, (numops
== 0), EPROTO
);
1137 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1138 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, reqp
);
1140 nfsm_chain_cleanup(&nmreq
);
1146 nfs4_lookup_rpc_async_finish(
1154 struct nfs_vattr
*nvap
)
1156 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
, isdotdot
= 0;
1157 uint32_t op
= NFS_OP_LOOKUP
;
1159 struct nfsmount
*nmp
;
1160 struct nfsm_chain nmrep
;
1162 nmp
= NFSTONMP(dnp
);
1163 nfsvers
= nmp
->nm_vers
;
1164 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2))
1167 nfsm_chain_null(&nmrep
);
1169 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
1171 if ((lockerror
= nfs_node_lock(dnp
)))
1173 nfsm_chain_skip_tag(error
, &nmrep
);
1174 nfsm_chain_get_32(error
, &nmrep
, numops
);
1175 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1176 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1179 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
1181 nfsm_chain_op_check(error
, &nmrep
, (isdotdot
? NFS_OP_LOOKUPP
: NFS_OP_LOOKUP
));
1182 nfsmout_if(error
|| !fhp
|| !nvap
);
1183 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETFH
);
1184 nfsm_chain_get_32(error
, &nmrep
, fhp
->fh_len
);
1185 nfsm_chain_get_opaque(error
, &nmrep
, fhp
->fh_len
, fhp
->fh_data
);
1186 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1187 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
1188 /* set this up to look like a referral trigger */
1189 nfs4_default_attrs_for_referral_trigger(dnp
, name
, namelen
, nvap
, fhp
);
1193 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
1197 nfs_node_unlock(dnp
);
1198 nfsm_chain_cleanup(&nmrep
);
1199 if (!error
&& (op
== NFS_OP_LOOKUP
) && (nmp
->nm_state
& NFSSTA_NEEDSECINFO
)) {
1200 /* We still need to get SECINFO to set default for mount. */
1201 /* Do so for the first LOOKUP that returns successfully. */
1204 sec
.count
= NX_MAX_SEC_FLAVORS
;
1205 error
= nfs4_secinfo_rpc(nmp
, &req
->r_secinfo
, vfs_context_ucred(ctx
), sec
.flavors
, &sec
.count
);
1206 /* [sigh] some implementations return "illegal" error for unsupported ops */
1207 if (error
== NFSERR_OP_ILLEGAL
)
1210 /* set our default security flavor to the first in the list */
1211 lck_mtx_lock(&nmp
->nm_lock
);
1213 nmp
->nm_auth
= sec
.flavors
[0];
1214 nmp
->nm_state
&= ~NFSSTA_NEEDSECINFO
;
1215 lck_mtx_unlock(&nmp
->nm_lock
);
1229 struct nfsmount
*nmp
;
1230 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1231 u_int64_t xid
, newwverf
;
1233 struct nfsm_chain nmreq
, nmrep
;
1234 struct nfsreq_secinfo_args si
;
1237 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
1238 if (nfs_mount_gone(nmp
))
1240 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1242 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
1244 nfsvers
= nmp
->nm_vers
;
1246 if (count
> UINT32_MAX
)
1251 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1252 nfsm_chain_null(&nmreq
);
1253 nfsm_chain_null(&nmrep
);
1255 // PUTFH, COMMIT, GETATTR
1257 nfsm_chain_build_alloc_init(error
, &nmreq
, 19 * NFSX_UNSIGNED
);
1258 nfsm_chain_add_compound_header(error
, &nmreq
, "commit", numops
);
1260 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1261 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1263 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_COMMIT
);
1264 nfsm_chain_add_64(error
, &nmreq
, offset
);
1265 nfsm_chain_add_32(error
, &nmreq
, count32
);
1267 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1268 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
1269 nfsm_chain_build_done(error
, &nmreq
);
1270 nfsm_assert(error
, (numops
== 0), EPROTO
);
1272 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1273 current_thread(), cred
, &si
, 0, &nmrep
, &xid
, &status
);
1275 if ((lockerror
= nfs_node_lock(np
)))
1277 nfsm_chain_skip_tag(error
, &nmrep
);
1278 nfsm_chain_get_32(error
, &nmrep
, numops
);
1279 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1280 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_COMMIT
);
1281 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
1282 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1283 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1285 nfs_node_unlock(np
);
1287 lck_mtx_lock(&nmp
->nm_lock
);
1288 if (nmp
->nm_verf
!= newwverf
)
1289 nmp
->nm_verf
= newwverf
;
1290 if (wverf
!= newwverf
)
1291 error
= NFSERR_STALEWRITEVERF
;
1292 lck_mtx_unlock(&nmp
->nm_lock
);
1294 nfsm_chain_cleanup(&nmreq
);
1295 nfsm_chain_cleanup(&nmrep
);
1302 struct nfs_fsattr
*nfsap
,
1306 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1307 struct nfsm_chain nmreq
, nmrep
;
1308 struct nfsmount
*nmp
= NFSTONMP(np
);
1309 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1310 struct nfs_vattr nvattr
;
1311 struct nfsreq_secinfo_args si
;
1313 if (nfs_mount_gone(nmp
))
1315 nfsvers
= nmp
->nm_vers
;
1316 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1319 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1320 NVATTR_INIT(&nvattr
);
1321 nfsm_chain_null(&nmreq
);
1322 nfsm_chain_null(&nmrep
);
1324 /* NFSv4: fetch "pathconf" info for this node */
1327 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
1328 nfsm_chain_add_compound_header(error
, &nmreq
, "pathconf", numops
);
1330 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1331 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1333 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1334 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1335 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXLINK
);
1336 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXNAME
);
1337 NFS_BITMAP_SET(bitmap
, NFS_FATTR_NO_TRUNC
);
1338 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
1339 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
1340 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_PRESERVING
);
1341 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
1342 nfsm_chain_build_done(error
, &nmreq
);
1343 nfsm_assert(error
, (numops
== 0), EPROTO
);
1345 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1347 nfsm_chain_skip_tag(error
, &nmrep
);
1348 nfsm_chain_get_32(error
, &nmrep
, numops
);
1349 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1350 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1352 error
= nfs4_parsefattr(&nmrep
, nfsap
, &nvattr
, NULL
, NULL
, NULL
);
1354 if ((lockerror
= nfs_node_lock(np
)))
1357 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
1359 nfs_node_unlock(np
);
1361 NVATTR_CLEANUP(&nvattr
);
1362 nfsm_chain_cleanup(&nmreq
);
1363 nfsm_chain_cleanup(&nmrep
);
1369 struct vnop_getattr_args
/* {
1370 struct vnodeop_desc *a_desc;
1372 struct vnode_attr *a_vap;
1373 vfs_context_t a_context;
1376 struct vnode_attr
*vap
= ap
->a_vap
;
1377 struct nfsmount
*nmp
;
1378 struct nfs_vattr nva
;
1379 int error
, acls
, ngaflags
;
1381 nmp
= VTONMP(ap
->a_vp
);
1382 if (nfs_mount_gone(nmp
))
1384 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
1386 ngaflags
= NGA_CACHED
;
1387 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
)
1388 ngaflags
|= NGA_ACL
;
1389 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, ngaflags
);
1393 /* copy what we have in nva to *a_vap */
1394 if (VATTR_IS_ACTIVE(vap
, va_rdev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_RAWDEV
)) {
1395 dev_t rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1396 VATTR_RETURN(vap
, va_rdev
, rdev
);
1398 if (VATTR_IS_ACTIVE(vap
, va_nlink
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_NUMLINKS
))
1399 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1400 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SIZE
))
1401 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1402 // VATTR_RETURN(vap, va_data_alloc, ???);
1403 // VATTR_RETURN(vap, va_total_size, ???);
1404 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SPACE_USED
))
1405 VATTR_RETURN(vap
, va_total_alloc
, nva
.nva_bytes
);
1406 if (VATTR_IS_ACTIVE(vap
, va_uid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1407 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1408 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1409 VATTR_RETURN(vap
, va_uuuid
, nva
.nva_uuuid
);
1410 if (VATTR_IS_ACTIVE(vap
, va_gid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1411 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1412 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1413 VATTR_RETURN(vap
, va_guuid
, nva
.nva_guuid
);
1414 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1415 if (NMFLAG(nmp
, ACLONLY
) || !NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_MODE
))
1416 VATTR_RETURN(vap
, va_mode
, 0777);
1418 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1420 if (VATTR_IS_ACTIVE(vap
, va_flags
) &&
1421 (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) ||
1422 NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) ||
1423 (nva
.nva_flags
& NFS_FFLAG_TRIGGER
))) {
1425 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) &&
1426 (nva
.nva_flags
& NFS_FFLAG_ARCHIVED
))
1427 flags
|= SF_ARCHIVED
;
1428 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) &&
1429 (nva
.nva_flags
& NFS_FFLAG_HIDDEN
))
1431 VATTR_RETURN(vap
, va_flags
, flags
);
1433 if (VATTR_IS_ACTIVE(vap
, va_create_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_CREATE
)) {
1434 vap
->va_create_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CREATE
];
1435 vap
->va_create_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CREATE
];
1436 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1438 if (VATTR_IS_ACTIVE(vap
, va_access_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_ACCESS
)) {
1439 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1440 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1441 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1443 if (VATTR_IS_ACTIVE(vap
, va_modify_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_MODIFY
)) {
1444 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1445 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1446 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1448 if (VATTR_IS_ACTIVE(vap
, va_change_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_METADATA
)) {
1449 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1450 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1451 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1453 if (VATTR_IS_ACTIVE(vap
, va_backup_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_BACKUP
)) {
1454 vap
->va_backup_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_BACKUP
];
1455 vap
->va_backup_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_BACKUP
];
1456 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1458 if (VATTR_IS_ACTIVE(vap
, va_fileid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_FILEID
))
1459 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1460 if (VATTR_IS_ACTIVE(vap
, va_type
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TYPE
))
1461 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1462 if (VATTR_IS_ACTIVE(vap
, va_filerev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_CHANGE
))
1463 VATTR_RETURN(vap
, va_filerev
, nva
.nva_change
);
1465 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
) {
1466 VATTR_RETURN(vap
, va_acl
, nva
.nva_acl
);
1470 // other attrs we might support someday:
1471 // VATTR_RETURN(vap, va_encoding, ??? /* potentially unnormalized UTF-8? */);
1473 NVATTR_CLEANUP(&nva
);
1480 struct vnode_attr
*vap
,
1483 struct nfsmount
*nmp
= NFSTONMP(np
);
1484 int error
= 0, setattr_error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
1485 u_int64_t xid
, nextxid
;
1486 struct nfsm_chain nmreq
, nmrep
;
1487 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
1488 uint32_t getbitmap
[NFS_ATTR_BITMAP_LEN
];
1489 uint32_t setbitmap
[NFS_ATTR_BITMAP_LEN
];
1490 nfs_stateid stateid
;
1491 struct nfsreq_secinfo_args si
;
1493 if (nfs_mount_gone(nmp
))
1495 nfsvers
= nmp
->nm_vers
;
1496 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1499 if (VATTR_IS_ACTIVE(vap
, va_flags
) && (vap
->va_flags
& ~(SF_ARCHIVED
|UF_HIDDEN
))) {
1500 /* we don't support setting unsupported flags (duh!) */
1501 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
1502 return (EINVAL
); /* return EINVAL if other attributes also set */
1504 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
1507 /* don't bother requesting some changes if they don't look like they are changing */
1508 if (VATTR_IS_ACTIVE(vap
, va_uid
) && (vap
->va_uid
== np
->n_vattr
.nva_uid
))
1509 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1510 if (VATTR_IS_ACTIVE(vap
, va_gid
) && (vap
->va_gid
== np
->n_vattr
.nva_gid
))
1511 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1512 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &np
->n_vattr
.nva_uuuid
))
1513 VATTR_CLEAR_ACTIVE(vap
, va_uuuid
);
1514 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &np
->n_vattr
.nva_guuid
))
1515 VATTR_CLEAR_ACTIVE(vap
, va_guuid
);
1518 /* do nothing if no attributes will be sent */
1519 nfs_vattr_set_bitmap(nmp
, bitmap
, vap
);
1520 if (!bitmap
[0] && !bitmap
[1])
1523 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1524 nfsm_chain_null(&nmreq
);
1525 nfsm_chain_null(&nmrep
);
1528 * Prepare GETATTR bitmap: if we are setting the ACL or mode, we
1529 * need to invalidate any cached ACL. And if we had an ACL cached,
1530 * we might as well also fetch the new value.
1532 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, getbitmap
);
1533 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
) ||
1534 NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
1536 NFS_BITMAP_SET(getbitmap
, NFS_FATTR_ACL
);
1540 // PUTFH, SETATTR, GETATTR
1542 nfsm_chain_build_alloc_init(error
, &nmreq
, 40 * NFSX_UNSIGNED
);
1543 nfsm_chain_add_compound_header(error
, &nmreq
, "setattr", numops
);
1545 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1546 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1548 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SETATTR
);
1549 if (VATTR_IS_ACTIVE(vap
, va_data_size
))
1550 nfs_get_stateid(np
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &stateid
);
1552 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
1553 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
1554 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
1556 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1557 nfsm_chain_add_bitmap_supported(error
, &nmreq
, getbitmap
, nmp
, np
);
1558 nfsm_chain_build_done(error
, &nmreq
);
1559 nfsm_assert(error
, (numops
== 0), EPROTO
);
1561 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1563 if ((lockerror
= nfs_node_lock(np
)))
1565 nfsm_chain_skip_tag(error
, &nmrep
);
1566 nfsm_chain_get_32(error
, &nmrep
, numops
);
1567 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1569 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SETATTR
);
1570 nfsmout_if(error
== EBADRPC
);
1571 setattr_error
= error
;
1573 bmlen
= NFS_ATTR_BITMAP_LEN
;
1574 nfsm_chain_get_bitmap(error
, &nmrep
, setbitmap
, bmlen
);
1576 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
1577 microuptime(&np
->n_lastio
);
1578 nfs_vattr_set_supported(setbitmap
, vap
);
1579 error
= setattr_error
;
1581 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1582 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1584 NATTRINVALIDATE(np
);
1586 * We just changed the attributes and we want to make sure that we
1587 * see the latest attributes. Get the next XID. If it's not the
1588 * next XID after the SETATTR XID, then it's possible that another
1589 * RPC was in flight at the same time and it might put stale attributes
1590 * in the cache. In that case, we invalidate the attributes and set
1591 * the attribute cache XID to guarantee that newer attributes will
1595 nfs_get_xid(&nextxid
);
1596 if (nextxid
!= (xid
+ 1)) {
1597 np
->n_xid
= nextxid
;
1598 NATTRINVALIDATE(np
);
1602 nfs_node_unlock(np
);
1603 nfsm_chain_cleanup(&nmreq
);
1604 nfsm_chain_cleanup(&nmrep
);
1605 if ((setattr_error
== EINVAL
) && VATTR_IS_ACTIVE(vap
, va_acl
) && VATTR_IS_ACTIVE(vap
, va_mode
) && !NMFLAG(nmp
, ACLONLY
)) {
1607 * Some server's may not like ACL/mode combos that get sent.
1608 * If it looks like that's what the server choked on, try setting
1609 * just the ACL and not the mode (unless it looks like everything
1610 * but mode was already successfully set).
1612 if (((bitmap
[0] & setbitmap
[0]) != bitmap
[0]) ||
1613 ((bitmap
[1] & (setbitmap
[1]|NFS_FATTR_MODE
)) != bitmap
[1])) {
1614 VATTR_CLEAR_ACTIVE(vap
, va_mode
);
1623 * Wait for any pending recovery to complete.
1626 nfs_mount_state_wait_for_recovery(struct nfsmount
*nmp
)
1628 struct timespec ts
= { 1, 0 };
1629 int error
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
1631 lck_mtx_lock(&nmp
->nm_lock
);
1632 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1633 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 1)))
1635 nfs_mount_sock_thread_wake(nmp
);
1636 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1639 lck_mtx_unlock(&nmp
->nm_lock
);
1645 * We're about to use/manipulate NFS mount's open/lock state.
1646 * Wait for any pending state recovery to complete, then
1647 * mark the state as being in use (which will hold off
1648 * the recovery thread until we're done).
1651 nfs_mount_state_in_use_start(struct nfsmount
*nmp
, thread_t thd
)
1653 struct timespec ts
= { 1, 0 };
1654 int error
= 0, slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1656 if (nfs_mount_gone(nmp
))
1658 lck_mtx_lock(&nmp
->nm_lock
);
1659 if (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) {
1660 lck_mtx_unlock(&nmp
->nm_lock
);
1663 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1664 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 1)))
1666 nfs_mount_sock_thread_wake(nmp
);
1667 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1671 nmp
->nm_stateinuse
++;
1672 lck_mtx_unlock(&nmp
->nm_lock
);
1678 * We're done using/manipulating the NFS mount's open/lock
1679 * state. If the given error indicates that recovery should
1680 * be performed, we'll initiate recovery.
1683 nfs_mount_state_in_use_end(struct nfsmount
*nmp
, int error
)
1685 int restart
= nfs_mount_state_error_should_restart(error
);
1687 if (nfs_mount_gone(nmp
))
1689 lck_mtx_lock(&nmp
->nm_lock
);
1690 if (restart
&& (error
!= NFSERR_OLD_STATEID
) && (error
!= NFSERR_GRACE
)) {
1691 printf("nfs_mount_state_in_use_end: error %d, initiating recovery for %s, 0x%x\n",
1692 error
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
1693 nfs_need_recover(nmp
, error
);
1695 if (nmp
->nm_stateinuse
> 0)
1696 nmp
->nm_stateinuse
--;
1698 panic("NFS mount state in use count underrun");
1699 if (!nmp
->nm_stateinuse
&& (nmp
->nm_state
& NFSSTA_RECOVER
))
1700 wakeup(&nmp
->nm_stateinuse
);
1701 lck_mtx_unlock(&nmp
->nm_lock
);
1702 if (error
== NFSERR_GRACE
)
1703 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
1709 * Does the error mean we should restart/redo a state-related operation?
1712 nfs_mount_state_error_should_restart(int error
)
1715 case NFSERR_STALE_STATEID
:
1716 case NFSERR_STALE_CLIENTID
:
1717 case NFSERR_ADMIN_REVOKED
:
1718 case NFSERR_EXPIRED
:
1719 case NFSERR_OLD_STATEID
:
1720 case NFSERR_BAD_STATEID
:
1728 * In some cases we may want to limit how many times we restart a
1729 * state-related operation - e.g. we're repeatedly getting NFSERR_GRACE.
1730 * Base the limit on the lease (as long as it's not too short).
1733 nfs_mount_state_max_restarts(struct nfsmount
*nmp
)
1735 return (MAX(nmp
->nm_fsattr
.nfsa_lease
, 60));
1739 * Does the error mean we probably lost a delegation?
1742 nfs_mount_state_error_delegation_lost(int error
)
1745 case NFSERR_STALE_STATEID
:
1746 case NFSERR_ADMIN_REVOKED
:
1747 case NFSERR_EXPIRED
:
1748 case NFSERR_OLD_STATEID
:
1749 case NFSERR_BAD_STATEID
:
1750 case NFSERR_GRACE
: /* ugh! (stupid) RFC 3530 specifically disallows CLAIM_DELEGATE_CUR during grace period? */
1758 * Mark an NFS node's open state as busy.
1761 nfs_open_state_set_busy(nfsnode_t np
, thread_t thd
)
1763 struct nfsmount
*nmp
;
1764 struct timespec ts
= {2, 0};
1765 int error
= 0, slpflag
;
1768 if (nfs_mount_gone(nmp
))
1770 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1772 lck_mtx_lock(&np
->n_openlock
);
1773 while (np
->n_openflags
& N_OPENBUSY
) {
1774 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1776 np
->n_openflags
|= N_OPENWANT
;
1777 msleep(&np
->n_openflags
, &np
->n_openlock
, slpflag
, "nfs_open_state_set_busy", &ts
);
1781 np
->n_openflags
|= N_OPENBUSY
;
1782 lck_mtx_unlock(&np
->n_openlock
);
1788 * Clear an NFS node's open state busy flag and wake up
1789 * anyone wanting it.
1792 nfs_open_state_clear_busy(nfsnode_t np
)
1796 lck_mtx_lock(&np
->n_openlock
);
1797 if (!(np
->n_openflags
& N_OPENBUSY
))
1798 panic("nfs_open_state_clear_busy");
1799 wanted
= (np
->n_openflags
& N_OPENWANT
);
1800 np
->n_openflags
&= ~(N_OPENBUSY
|N_OPENWANT
);
1801 lck_mtx_unlock(&np
->n_openlock
);
1803 wakeup(&np
->n_openflags
);
1807 * Search a mount's open owner list for the owner for this credential.
1808 * If not found and "alloc" is set, then allocate a new one.
1810 struct nfs_open_owner
*
1811 nfs_open_owner_find(struct nfsmount
*nmp
, kauth_cred_t cred
, int alloc
)
1813 uid_t uid
= kauth_cred_getuid(cred
);
1814 struct nfs_open_owner
*noop
, *newnoop
= NULL
;
1817 lck_mtx_lock(&nmp
->nm_lock
);
1818 TAILQ_FOREACH(noop
, &nmp
->nm_open_owners
, noo_link
) {
1819 if (kauth_cred_getuid(noop
->noo_cred
) == uid
)
1823 if (!noop
&& !newnoop
&& alloc
) {
1824 lck_mtx_unlock(&nmp
->nm_lock
);
1825 MALLOC(newnoop
, struct nfs_open_owner
*, sizeof(struct nfs_open_owner
), M_TEMP
, M_WAITOK
);
1828 bzero(newnoop
, sizeof(*newnoop
));
1829 lck_mtx_init(&newnoop
->noo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
1830 newnoop
->noo_mount
= nmp
;
1831 kauth_cred_ref(cred
);
1832 newnoop
->noo_cred
= cred
;
1833 newnoop
->noo_name
= OSAddAtomic(1, &nfs_open_owner_seqnum
);
1834 TAILQ_INIT(&newnoop
->noo_opens
);
1837 if (!noop
&& newnoop
) {
1838 newnoop
->noo_flags
|= NFS_OPEN_OWNER_LINK
;
1839 TAILQ_INSERT_HEAD(&nmp
->nm_open_owners
, newnoop
, noo_link
);
1842 lck_mtx_unlock(&nmp
->nm_lock
);
1844 if (newnoop
&& (noop
!= newnoop
))
1845 nfs_open_owner_destroy(newnoop
);
1848 nfs_open_owner_ref(noop
);
1854 * destroy an open owner that's no longer needed
1857 nfs_open_owner_destroy(struct nfs_open_owner
*noop
)
1860 kauth_cred_unref(&noop
->noo_cred
);
1861 lck_mtx_destroy(&noop
->noo_lock
, nfs_open_grp
);
1866 * acquire a reference count on an open owner
1869 nfs_open_owner_ref(struct nfs_open_owner
*noop
)
1871 lck_mtx_lock(&noop
->noo_lock
);
1873 lck_mtx_unlock(&noop
->noo_lock
);
1877 * drop a reference count on an open owner and destroy it if
1878 * it is no longer referenced and no longer on the mount's list.
1881 nfs_open_owner_rele(struct nfs_open_owner
*noop
)
1883 lck_mtx_lock(&noop
->noo_lock
);
1884 if (noop
->noo_refcnt
< 1)
1885 panic("nfs_open_owner_rele: no refcnt");
1887 if (!noop
->noo_refcnt
&& (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1888 panic("nfs_open_owner_rele: busy");
1889 /* XXX we may potentially want to clean up idle/unused open owner structures */
1890 if (noop
->noo_refcnt
|| (noop
->noo_flags
& NFS_OPEN_OWNER_LINK
)) {
1891 lck_mtx_unlock(&noop
->noo_lock
);
1894 /* owner is no longer referenced or linked to mount, so destroy it */
1895 lck_mtx_unlock(&noop
->noo_lock
);
1896 nfs_open_owner_destroy(noop
);
1900 * Mark an open owner as busy because we are about to
1901 * start an operation that uses and updates open owner state.
1904 nfs_open_owner_set_busy(struct nfs_open_owner
*noop
, thread_t thd
)
1906 struct nfsmount
*nmp
;
1907 struct timespec ts
= {2, 0};
1908 int error
= 0, slpflag
;
1910 nmp
= noop
->noo_mount
;
1911 if (nfs_mount_gone(nmp
))
1913 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1915 lck_mtx_lock(&noop
->noo_lock
);
1916 while (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
) {
1917 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1919 noop
->noo_flags
|= NFS_OPEN_OWNER_WANT
;
1920 msleep(noop
, &noop
->noo_lock
, slpflag
, "nfs_open_owner_set_busy", &ts
);
1924 noop
->noo_flags
|= NFS_OPEN_OWNER_BUSY
;
1925 lck_mtx_unlock(&noop
->noo_lock
);
1931 * Clear the busy flag on an open owner and wake up anyone waiting
1935 nfs_open_owner_clear_busy(struct nfs_open_owner
*noop
)
1939 lck_mtx_lock(&noop
->noo_lock
);
1940 if (!(noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1941 panic("nfs_open_owner_clear_busy");
1942 wanted
= (noop
->noo_flags
& NFS_OPEN_OWNER_WANT
);
1943 noop
->noo_flags
&= ~(NFS_OPEN_OWNER_BUSY
|NFS_OPEN_OWNER_WANT
);
1944 lck_mtx_unlock(&noop
->noo_lock
);
1950 * Given an open/lock owner and an error code, increment the
1951 * sequence ID if appropriate.
1954 nfs_owner_seqid_increment(struct nfs_open_owner
*noop
, struct nfs_lock_owner
*nlop
, int error
)
1957 case NFSERR_STALE_CLIENTID
:
1958 case NFSERR_STALE_STATEID
:
1959 case NFSERR_OLD_STATEID
:
1960 case NFSERR_BAD_STATEID
:
1961 case NFSERR_BAD_SEQID
:
1963 case NFSERR_RESOURCE
:
1964 case NFSERR_NOFILEHANDLE
:
1965 /* do not increment the open seqid on these errors */
1975 * Search a node's open file list for any conflicts with this request.
1976 * Also find this open owner's open file structure.
1977 * If not found and "alloc" is set, then allocate one.
1982 struct nfs_open_owner
*noop
,
1983 struct nfs_open_file
**nofpp
,
1984 uint32_t accessMode
,
1989 return nfs_open_file_find_internal(np
, noop
, nofpp
, accessMode
, denyMode
, alloc
);
1993 * Internally, allow using a provisional nodeless nofp (passed in via *nofpp)
1994 * if an existing one is not found. This is used in "create" scenarios to
1995 * officially add the provisional nofp to the node once the node is created.
1998 nfs_open_file_find_internal(
2000 struct nfs_open_owner
*noop
,
2001 struct nfs_open_file
**nofpp
,
2002 uint32_t accessMode
,
2006 struct nfs_open_file
*nofp
= NULL
, *nofp2
, *newnofp
= NULL
;
2011 lck_mtx_lock(&np
->n_openlock
);
2012 TAILQ_FOREACH(nofp2
, &np
->n_opens
, nof_link
) {
2013 if (nofp2
->nof_owner
== noop
) {
2018 if ((accessMode
& nofp2
->nof_deny
) || (denyMode
& nofp2
->nof_access
)) {
2019 /* This request conflicts with an existing open on this client. */
2020 lck_mtx_unlock(&np
->n_openlock
);
2026 * If this open owner doesn't have an open
2027 * file structure yet, we create one for it.
2029 if (!nofp
&& !*nofpp
&& !newnofp
&& alloc
) {
2030 lck_mtx_unlock(&np
->n_openlock
);
2032 MALLOC(newnofp
, struct nfs_open_file
*, sizeof(struct nfs_open_file
), M_TEMP
, M_WAITOK
);
2035 bzero(newnofp
, sizeof(*newnofp
));
2036 lck_mtx_init(&newnofp
->nof_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
2037 newnofp
->nof_owner
= noop
;
2038 nfs_open_owner_ref(noop
);
2039 newnofp
->nof_np
= np
;
2040 lck_mtx_lock(&noop
->noo_lock
);
2041 TAILQ_INSERT_HEAD(&noop
->noo_opens
, newnofp
, nof_oolink
);
2042 lck_mtx_unlock(&noop
->noo_lock
);
2048 (*nofpp
)->nof_np
= np
;
2054 TAILQ_INSERT_HEAD(&np
->n_opens
, nofp
, nof_link
);
2057 lck_mtx_unlock(&np
->n_openlock
);
2059 if (alloc
&& newnofp
&& (nofp
!= newnofp
))
2060 nfs_open_file_destroy(newnofp
);
2063 return (nofp
? 0 : ESRCH
);
2067 * Destroy an open file structure.
2070 nfs_open_file_destroy(struct nfs_open_file
*nofp
)
2072 lck_mtx_lock(&nofp
->nof_owner
->noo_lock
);
2073 TAILQ_REMOVE(&nofp
->nof_owner
->noo_opens
, nofp
, nof_oolink
);
2074 lck_mtx_unlock(&nofp
->nof_owner
->noo_lock
);
2075 nfs_open_owner_rele(nofp
->nof_owner
);
2076 lck_mtx_destroy(&nofp
->nof_lock
, nfs_open_grp
);
2081 * Mark an open file as busy because we are about to
2082 * start an operation that uses and updates open file state.
2085 nfs_open_file_set_busy(struct nfs_open_file
*nofp
, thread_t thd
)
2087 struct nfsmount
*nmp
;
2088 struct timespec ts
= {2, 0};
2089 int error
= 0, slpflag
;
2091 nmp
= nofp
->nof_owner
->noo_mount
;
2092 if (nfs_mount_gone(nmp
))
2094 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
2096 lck_mtx_lock(&nofp
->nof_lock
);
2097 while (nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
) {
2098 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
2100 nofp
->nof_flags
|= NFS_OPEN_FILE_WANT
;
2101 msleep(nofp
, &nofp
->nof_lock
, slpflag
, "nfs_open_file_set_busy", &ts
);
2105 nofp
->nof_flags
|= NFS_OPEN_FILE_BUSY
;
2106 lck_mtx_unlock(&nofp
->nof_lock
);
2112 * Clear the busy flag on an open file and wake up anyone waiting
2116 nfs_open_file_clear_busy(struct nfs_open_file
*nofp
)
2120 lck_mtx_lock(&nofp
->nof_lock
);
2121 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
))
2122 panic("nfs_open_file_clear_busy");
2123 wanted
= (nofp
->nof_flags
& NFS_OPEN_FILE_WANT
);
2124 nofp
->nof_flags
&= ~(NFS_OPEN_FILE_BUSY
|NFS_OPEN_FILE_WANT
);
2125 lck_mtx_unlock(&nofp
->nof_lock
);
2131 * Add the open state for the given access/deny modes to this open file.
2134 nfs_open_file_add_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
, int delegated
)
2136 lck_mtx_lock(&nofp
->nof_lock
);
2137 nofp
->nof_access
|= accessMode
;
2138 nofp
->nof_deny
|= denyMode
;
2141 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2142 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2144 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2146 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2148 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2149 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2151 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2153 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2154 nofp
->nof_d_rw_dw
++;
2155 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2156 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2157 nofp
->nof_d_r_drw
++;
2158 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2159 nofp
->nof_d_w_drw
++;
2160 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2161 nofp
->nof_d_rw_drw
++;
2164 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2165 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2167 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2169 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2171 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2172 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2174 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2176 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2178 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2179 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2181 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2183 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2188 nofp
->nof_opencnt
++;
2189 lck_mtx_unlock(&nofp
->nof_lock
);
2193 * Find which particular open combo will be closed and report what
2194 * the new modes will be and whether the open was delegated.
2197 nfs_open_file_remove_open_find(
2198 struct nfs_open_file
*nofp
,
2199 uint32_t accessMode
,
2201 uint32_t *newAccessMode
,
2202 uint32_t *newDenyMode
,
2206 * Calculate new modes: a mode bit gets removed when there's only
2207 * one count in all the corresponding counts
2209 *newAccessMode
= nofp
->nof_access
;
2210 *newDenyMode
= nofp
->nof_deny
;
2212 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2213 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2214 ((nofp
->nof_r
+ nofp
->nof_d_r
+
2215 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2216 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2217 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2218 nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2219 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2220 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2221 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2222 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2223 ((nofp
->nof_w
+ nofp
->nof_d_w
+
2224 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2225 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2226 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2227 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2228 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2229 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_WRITE
;
2230 if ((denyMode
& NFS_OPEN_SHARE_DENY_READ
) &&
2231 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) &&
2232 ((nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2233 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2234 nofp
->nof_rw_drw
+ nofp
->nof_d_rw_drw
) == 1))
2235 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_READ
;
2236 if ((denyMode
& NFS_OPEN_SHARE_DENY_WRITE
) &&
2237 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
) &&
2238 ((nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2239 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2240 nofp
->nof_rw_drw
+ nofp
->nof_d_rw_drw
+
2241 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2242 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2243 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2244 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_WRITE
;
2246 /* Find the corresponding open access/deny mode counter. */
2247 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2248 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2249 *delegated
= (nofp
->nof_d_r
!= 0);
2250 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2251 *delegated
= (nofp
->nof_d_w
!= 0);
2252 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2253 *delegated
= (nofp
->nof_d_rw
!= 0);
2256 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2257 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2258 *delegated
= (nofp
->nof_d_r_dw
!= 0);
2259 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2260 *delegated
= (nofp
->nof_d_w_dw
!= 0);
2261 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2262 *delegated
= (nofp
->nof_d_rw_dw
!= 0);
2265 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2266 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2267 *delegated
= (nofp
->nof_d_r_drw
!= 0);
2268 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2269 *delegated
= (nofp
->nof_d_w_drw
!= 0);
2270 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2271 *delegated
= (nofp
->nof_d_rw_drw
!= 0);
2278 * Remove the open state for the given access/deny modes to this open file.
2281 nfs_open_file_remove_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
)
2283 uint32_t newAccessMode
, newDenyMode
;
2286 lck_mtx_lock(&nofp
->nof_lock
);
2287 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
2289 /* Decrement the corresponding open access/deny mode counter. */
2290 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2291 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2293 if (nofp
->nof_d_r
== 0)
2294 NP(nofp
->nof_np
, "nfs: open(R) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2298 if (nofp
->nof_r
== 0)
2299 NP(nofp
->nof_np
, "nfs: open(R) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2303 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2305 if (nofp
->nof_d_w
== 0)
2306 NP(nofp
->nof_np
, "nfs: open(W) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2310 if (nofp
->nof_w
== 0)
2311 NP(nofp
->nof_np
, "nfs: open(W) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2315 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2317 if (nofp
->nof_d_rw
== 0)
2318 NP(nofp
->nof_np
, "nfs: open(RW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2322 if (nofp
->nof_rw
== 0)
2323 NP(nofp
->nof_np
, "nfs: open(RW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2328 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2329 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2331 if (nofp
->nof_d_r_dw
== 0)
2332 NP(nofp
->nof_np
, "nfs: open(R,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2336 if (nofp
->nof_r_dw
== 0)
2337 NP(nofp
->nof_np
, "nfs: open(R,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2341 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2343 if (nofp
->nof_d_w_dw
== 0)
2344 NP(nofp
->nof_np
, "nfs: open(W,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2348 if (nofp
->nof_w_dw
== 0)
2349 NP(nofp
->nof_np
, "nfs: open(W,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2353 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2355 if (nofp
->nof_d_rw_dw
== 0)
2356 NP(nofp
->nof_np
, "nfs: open(RW,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2358 nofp
->nof_d_rw_dw
--;
2360 if (nofp
->nof_rw_dw
== 0)
2361 NP(nofp
->nof_np
, "nfs: open(RW,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2366 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2367 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2369 if (nofp
->nof_d_r_drw
== 0)
2370 NP(nofp
->nof_np
, "nfs: open(R,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2372 nofp
->nof_d_r_drw
--;
2374 if (nofp
->nof_r_drw
== 0)
2375 NP(nofp
->nof_np
, "nfs: open(R,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2379 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2381 if (nofp
->nof_d_w_drw
== 0)
2382 NP(nofp
->nof_np
, "nfs: open(W,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2384 nofp
->nof_d_w_drw
--;
2386 if (nofp
->nof_w_drw
== 0)
2387 NP(nofp
->nof_np
, "nfs: open(W,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2391 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2393 if (nofp
->nof_d_rw_drw
== 0)
2394 NP(nofp
->nof_np
, "nfs: open(RW,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2396 nofp
->nof_d_rw_drw
--;
2398 if (nofp
->nof_rw_drw
== 0)
2399 NP(nofp
->nof_np
, "nfs: open(RW,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2406 /* update the modes */
2407 nofp
->nof_access
= newAccessMode
;
2408 nofp
->nof_deny
= newDenyMode
;
2409 nofp
->nof_opencnt
--;
2410 lck_mtx_unlock(&nofp
->nof_lock
);
2415 * Get the current (delegation, lock, open, default) stateid for this node.
2416 * If node has a delegation, use that stateid.
2417 * If pid has a lock, use the lockowner's stateid.
2418 * Or use the open file's stateid.
2419 * If no open file, use a default stateid of all ones.
2422 nfs_get_stateid(nfsnode_t np
, thread_t thd
, kauth_cred_t cred
, nfs_stateid
*sid
)
2424 struct nfsmount
*nmp
= NFSTONMP(np
);
2425 proc_t p
= thd
? get_bsdthreadtask_info(thd
) : current_proc(); // XXX async I/O requests don't have a thread
2426 struct nfs_open_owner
*noop
= NULL
;
2427 struct nfs_open_file
*nofp
= NULL
;
2428 struct nfs_lock_owner
*nlop
= NULL
;
2429 nfs_stateid
*s
= NULL
;
2431 if (np
->n_openflags
& N_DELEG_MASK
) {
2432 s
= &np
->n_dstateid
;
2435 nlop
= nfs_lock_owner_find(np
, p
, 0);
2436 if (nlop
&& !TAILQ_EMPTY(&nlop
->nlo_locks
)) {
2437 /* we hold locks, use lock stateid */
2438 s
= &nlop
->nlo_stateid
;
2439 } else if (((noop
= nfs_open_owner_find(nmp
, cred
, 0))) &&
2440 (nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0) == 0) &&
2441 !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) &&
2443 /* we (should) have the file open, use open stateid */
2444 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)
2445 nfs4_reopen(nofp
, thd
);
2446 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
2447 s
= &nofp
->nof_stateid
;
2452 sid
->seqid
= s
->seqid
;
2453 sid
->other
[0] = s
->other
[0];
2454 sid
->other
[1] = s
->other
[1];
2455 sid
->other
[2] = s
->other
[2];
2457 /* named attributes may not have a stateid for reads, so don't complain for them */
2458 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
2459 NP(np
, "nfs_get_stateid: no stateid");
2460 sid
->seqid
= sid
->other
[0] = sid
->other
[1] = sid
->other
[2] = 0xffffffff;
2463 nfs_lock_owner_rele(nlop
);
2465 nfs_open_owner_rele(noop
);
2470 * When we have a delegation, we may be able to perform the OPEN locally.
2471 * Perform the OPEN by checking the delegation ACE and/or checking via ACCESS.
2474 nfs4_open_delegated(
2476 struct nfs_open_file
*nofp
,
2477 uint32_t accessMode
,
2481 int error
= 0, ismember
, readtoo
= 0, authorized
= 0;
2483 struct kauth_acl_eval eval
;
2484 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2486 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2488 * Try to open it for read access too,
2489 * so the buffer cache can read data.
2492 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2497 if (accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)
2498 action
|= KAUTH_VNODE_READ_DATA
;
2499 if (accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
)
2500 action
|= KAUTH_VNODE_WRITE_DATA
;
2502 /* evaluate ACE (if we have one) */
2503 if (np
->n_dace
.ace_flags
) {
2504 eval
.ae_requested
= action
;
2505 eval
.ae_acl
= &np
->n_dace
;
2507 eval
.ae_options
= 0;
2508 if (np
->n_vattr
.nva_uid
== kauth_cred_getuid(cred
))
2509 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
2510 error
= kauth_cred_ismember_gid(cred
, np
->n_vattr
.nva_gid
, &ismember
);
2511 if (!error
&& ismember
)
2512 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
2514 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
2515 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
2516 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
2517 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
2519 error
= kauth_acl_evaluate(cred
, &eval
);
2521 if (!error
&& (eval
.ae_result
== KAUTH_RESULT_ALLOW
))
2526 /* need to ask the server via ACCESS */
2527 struct vnop_access_args naa
;
2528 naa
.a_desc
= &vnop_access_desc
;
2529 naa
.a_vp
= NFSTOV(np
);
2530 naa
.a_action
= action
;
2531 naa
.a_context
= ctx
;
2532 if (!(error
= nfs_vnop_access(&naa
)))
2538 /* try again without the extra read access */
2539 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2543 return (error
? error
: EACCES
);
2546 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 1);
2553 * Open a file with the given access/deny modes.
2555 * If we have a delegation, we may be able to handle the open locally.
2556 * Otherwise, we will always send the open RPC even if this open's mode is
2557 * a subset of all the existing opens. This makes sure that we will always
2558 * be able to do a downgrade to any of the open modes.
2560 * Note: local conflicts should have already been checked in nfs_open_file_find().
2565 struct nfs_open_file
*nofp
,
2566 uint32_t accessMode
,
2570 vnode_t vp
= NFSTOV(np
);
2572 struct componentname cn
;
2573 const char *vname
= NULL
;
2575 char smallname
[128];
2576 char *filename
= NULL
;
2577 int error
= 0, readtoo
= 0;
2580 * We can handle the OPEN ourselves if we have a delegation,
2581 * unless it's a read delegation and the open is asking for
2582 * either write access or deny read. We also don't bother to
2583 * use the delegation if it's being returned.
2585 if (np
->n_openflags
& N_DELEG_MASK
) {
2586 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
2588 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
) &&
2589 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ||
2590 (!(accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) && !(denyMode
& NFS_OPEN_SHARE_DENY_READ
)))) {
2591 error
= nfs4_open_delegated(np
, nofp
, accessMode
, denyMode
, ctx
);
2592 nfs_open_state_clear_busy(np
);
2595 nfs_open_state_clear_busy(np
);
2599 * [sigh] We can't trust VFS to get the parent right for named
2600 * attribute nodes. (It likes to reparent the nodes after we've
2601 * created them.) Luckily we can probably get the right parent
2602 * from the n_parent we have stashed away.
2604 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
2605 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
2608 dvp
= vnode_getparent(vp
);
2609 vname
= vnode_getname(vp
);
2610 if (!dvp
|| !vname
) {
2615 filename
= &smallname
[0];
2616 namelen
= snprintf(filename
, sizeof(smallname
), "%s", vname
);
2617 if (namelen
>= sizeof(smallname
)) {
2618 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
2623 snprintf(filename
, namelen
+1, "%s", vname
);
2625 bzero(&cn
, sizeof(cn
));
2626 cn
.cn_nameptr
= filename
;
2627 cn
.cn_namelen
= namelen
;
2629 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2631 * Try to open it for read access too,
2632 * so the buffer cache can read data.
2635 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2638 error
= nfs4_open_rpc(nofp
, ctx
, &cn
, NULL
, dvp
, &vp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
2640 if (!nfs_mount_state_error_should_restart(error
) &&
2641 (error
!= EINTR
) && (error
!= ERESTART
) && readtoo
) {
2642 /* try again without the extra read access */
2643 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2649 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
2651 if (filename
&& (filename
!= &smallname
[0]))
2652 FREE(filename
, M_TEMP
);
2654 vnode_putname(vname
);
2662 struct vnop_mmap_args
/* {
2663 struct vnodeop_desc *a_desc;
2666 vfs_context_t a_context;
2669 vfs_context_t ctx
= ap
->a_context
;
2670 vnode_t vp
= ap
->a_vp
;
2671 nfsnode_t np
= VTONFS(vp
);
2672 int error
= 0, accessMode
, denyMode
, delegated
;
2673 struct nfsmount
*nmp
;
2674 struct nfs_open_owner
*noop
= NULL
;
2675 struct nfs_open_file
*nofp
= NULL
;
2678 if (nfs_mount_gone(nmp
))
2681 if (!vnode_isreg(vp
) || !(ap
->a_fflags
& (PROT_READ
|PROT_WRITE
)))
2683 if (np
->n_flag
& NREVOKE
)
2687 * fflags contains some combination of: PROT_READ, PROT_WRITE
2688 * Since it's not possible to mmap() without having the file open for reading,
2689 * read access is always there (regardless if PROT_READ is not set).
2691 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
2692 if (ap
->a_fflags
& PROT_WRITE
)
2693 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
2694 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2696 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
2701 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2703 nfs_open_owner_rele(noop
);
2706 if (np
->n_flag
& NREVOKE
) {
2708 nfs_mount_state_in_use_end(nmp
, 0);
2709 nfs_open_owner_rele(noop
);
2713 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
2714 if (error
|| (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))) {
2715 NP(np
, "nfs_vnop_mmap: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
2718 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
2719 nfs_mount_state_in_use_end(nmp
, 0);
2720 error
= nfs4_reopen(nofp
, NULL
);
2726 error
= nfs_open_file_set_busy(nofp
, NULL
);
2733 * The open reference for mmap must mirror an existing open because
2734 * we may need to reclaim it after the file is closed.
2735 * So grab another open count matching the accessMode passed in.
2736 * If we already had an mmap open, prefer read/write without deny mode.
2737 * This means we may have to drop the current mmap open first.
2740 if (!nofp
->nof_access
) {
2741 if (accessMode
!= NFS_OPEN_SHARE_ACCESS_READ
) {
2742 /* not asking for just read access -> fail */
2746 /* we don't have the file open, so open it for read access */
2747 if (nmp
->nm_vers
< NFS_VER4
) {
2748 /* NFS v2/v3 opens are always allowed - so just add it. */
2749 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
2752 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2755 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
2760 /* determine deny mode for open */
2761 if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2762 if (nofp
->nof_d_rw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_d_rw_drw
) {
2765 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2766 else if (nofp
->nof_d_rw_dw
)
2767 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2768 else if (nofp
->nof_d_rw_drw
)
2769 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2770 } else if (nofp
->nof_rw
|| nofp
->nof_rw_dw
|| nofp
->nof_rw_drw
) {
2773 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2774 else if (nofp
->nof_rw_dw
)
2775 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2776 else if (nofp
->nof_rw_drw
)
2777 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2781 } else { /* NFS_OPEN_SHARE_ACCESS_READ */
2782 if (nofp
->nof_d_r
|| nofp
->nof_d_r_dw
|| nofp
->nof_d_r_drw
) {
2785 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2786 else if (nofp
->nof_d_r_dw
)
2787 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2788 else if (nofp
->nof_d_r_drw
)
2789 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2790 } else if (nofp
->nof_r
|| nofp
->nof_r_dw
|| nofp
->nof_r_drw
) {
2793 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2794 else if (nofp
->nof_r_dw
)
2795 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2796 else if (nofp
->nof_r_drw
)
2797 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2802 if (error
) /* mmap mode without proper open mode */
2806 * If the existing mmap access is more than the new access OR the
2807 * existing access is the same and the existing deny mode is less,
2808 * then we'll stick with the existing mmap open mode.
2810 if ((nofp
->nof_mmap_access
> accessMode
) ||
2811 ((nofp
->nof_mmap_access
== accessMode
) && (nofp
->nof_mmap_deny
<= denyMode
)))
2814 /* update mmap open mode */
2815 if (nofp
->nof_mmap_access
) {
2816 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2818 if (!nfs_mount_state_error_should_restart(error
))
2819 NP(np
, "nfs_vnop_mmap: close of previous mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2820 NP(np
, "nfs_vnop_mmap: update, close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2823 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2826 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
2827 nofp
->nof_mmap_access
= accessMode
;
2828 nofp
->nof_mmap_deny
= denyMode
;
2832 nfs_open_file_clear_busy(nofp
);
2833 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2838 nfs_open_owner_rele(noop
);
2842 nfs_node_lock_force(np
);
2843 if ((np
->n_flag
& NISMAPPED
) == 0) {
2844 np
->n_flag
|= NISMAPPED
;
2847 nfs_node_unlock(np
);
2849 lck_mtx_lock(&nmp
->nm_lock
);
2850 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
2851 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
2852 if (nmp
->nm_curdeadtimeout
<= 0)
2853 nmp
->nm_deadto_start
= 0;
2855 lck_mtx_unlock(&nmp
->nm_lock
);
2865 struct vnop_mnomap_args
/* {
2866 struct vnodeop_desc *a_desc;
2868 vfs_context_t a_context;
2871 vfs_context_t ctx
= ap
->a_context
;
2872 vnode_t vp
= ap
->a_vp
;
2873 nfsnode_t np
= VTONFS(vp
);
2874 struct nfsmount
*nmp
;
2875 struct nfs_open_file
*nofp
= NULL
;
2878 int is_mapped_flag
= 0;
2881 if (nfs_mount_gone(nmp
))
2884 nfs_node_lock_force(np
);
2885 if (np
->n_flag
& NISMAPPED
) {
2887 np
->n_flag
&= ~NISMAPPED
;
2889 nfs_node_unlock(np
);
2890 if (is_mapped_flag
) {
2891 lck_mtx_lock(&nmp
->nm_lock
);
2892 if (nmp
->nm_mappers
)
2895 NP(np
, "nfs_vnop_mnomap: removing mmap reference from mount, but mount has no files mmapped");
2896 lck_mtx_unlock(&nmp
->nm_lock
);
2899 /* flush buffers/ubc before we drop the open (in case it's our last open) */
2900 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
2901 if (UBCINFOEXISTS(vp
) && (size
= ubc_getsize(vp
)))
2902 ubc_msync(vp
, 0, size
, NULL
, UBC_PUSHALL
| UBC_SYNC
);
2904 /* walk all open files and close all mmap opens */
2906 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2909 lck_mtx_lock(&np
->n_openlock
);
2910 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
2911 if (!nofp
->nof_mmap_access
)
2913 lck_mtx_unlock(&np
->n_openlock
);
2914 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
2915 nfs_mount_state_in_use_end(nmp
, 0);
2916 error
= nfs4_reopen(nofp
, NULL
);
2921 error
= nfs_open_file_set_busy(nofp
, NULL
);
2923 lck_mtx_lock(&np
->n_openlock
);
2926 if (nofp
->nof_mmap_access
) {
2927 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2928 if (!nfs_mount_state_error_should_restart(error
)) {
2929 if (error
) /* not a state-operation-restarting error, so just clear the access */
2930 NP(np
, "nfs_vnop_mnomap: close of mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2931 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2934 NP(np
, "nfs_vnop_mnomap: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2936 nfs_open_file_clear_busy(nofp
);
2937 nfs_mount_state_in_use_end(nmp
, error
);
2940 lck_mtx_unlock(&np
->n_openlock
);
2941 nfs_mount_state_in_use_end(nmp
, error
);
2946 * Search a node's lock owner list for the owner for this process.
2947 * If not found and "alloc" is set, then allocate a new one.
2949 struct nfs_lock_owner
*
2950 nfs_lock_owner_find(nfsnode_t np
, proc_t p
, int alloc
)
2952 pid_t pid
= proc_pid(p
);
2953 struct nfs_lock_owner
*nlop
, *newnlop
= NULL
;
2956 lck_mtx_lock(&np
->n_openlock
);
2957 TAILQ_FOREACH(nlop
, &np
->n_lock_owners
, nlo_link
) {
2958 if (nlop
->nlo_pid
!= pid
)
2960 if (timevalcmp(&nlop
->nlo_pid_start
, &p
->p_start
, ==))
2962 /* stale lock owner... reuse it if we can */
2963 if (nlop
->nlo_refcnt
) {
2964 TAILQ_REMOVE(&np
->n_lock_owners
, nlop
, nlo_link
);
2965 nlop
->nlo_flags
&= ~NFS_LOCK_OWNER_LINK
;
2966 lck_mtx_unlock(&np
->n_openlock
);
2969 nlop
->nlo_pid_start
= p
->p_start
;
2970 nlop
->nlo_seqid
= 0;
2971 nlop
->nlo_stategenid
= 0;
2975 if (!nlop
&& !newnlop
&& alloc
) {
2976 lck_mtx_unlock(&np
->n_openlock
);
2977 MALLOC(newnlop
, struct nfs_lock_owner
*, sizeof(struct nfs_lock_owner
), M_TEMP
, M_WAITOK
);
2980 bzero(newnlop
, sizeof(*newnlop
));
2981 lck_mtx_init(&newnlop
->nlo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
2982 newnlop
->nlo_pid
= pid
;
2983 newnlop
->nlo_pid_start
= p
->p_start
;
2984 newnlop
->nlo_name
= OSAddAtomic(1, &nfs_lock_owner_seqnum
);
2985 TAILQ_INIT(&newnlop
->nlo_locks
);
2988 if (!nlop
&& newnlop
) {
2989 newnlop
->nlo_flags
|= NFS_LOCK_OWNER_LINK
;
2990 TAILQ_INSERT_HEAD(&np
->n_lock_owners
, newnlop
, nlo_link
);
2993 lck_mtx_unlock(&np
->n_openlock
);
2995 if (newnlop
&& (nlop
!= newnlop
))
2996 nfs_lock_owner_destroy(newnlop
);
2999 nfs_lock_owner_ref(nlop
);
3005 * destroy a lock owner that's no longer needed
3008 nfs_lock_owner_destroy(struct nfs_lock_owner
*nlop
)
3010 if (nlop
->nlo_open_owner
) {
3011 nfs_open_owner_rele(nlop
->nlo_open_owner
);
3012 nlop
->nlo_open_owner
= NULL
;
3014 lck_mtx_destroy(&nlop
->nlo_lock
, nfs_open_grp
);
3019 * acquire a reference count on a lock owner
3022 nfs_lock_owner_ref(struct nfs_lock_owner
*nlop
)
3024 lck_mtx_lock(&nlop
->nlo_lock
);
3026 lck_mtx_unlock(&nlop
->nlo_lock
);
3030 * drop a reference count on a lock owner and destroy it if
3031 * it is no longer referenced and no longer on the mount's list.
3034 nfs_lock_owner_rele(struct nfs_lock_owner
*nlop
)
3036 lck_mtx_lock(&nlop
->nlo_lock
);
3037 if (nlop
->nlo_refcnt
< 1)
3038 panic("nfs_lock_owner_rele: no refcnt");
3040 if (!nlop
->nlo_refcnt
&& (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
3041 panic("nfs_lock_owner_rele: busy");
3042 /* XXX we may potentially want to clean up idle/unused lock owner structures */
3043 if (nlop
->nlo_refcnt
|| (nlop
->nlo_flags
& NFS_LOCK_OWNER_LINK
)) {
3044 lck_mtx_unlock(&nlop
->nlo_lock
);
3047 /* owner is no longer referenced or linked to mount, so destroy it */
3048 lck_mtx_unlock(&nlop
->nlo_lock
);
3049 nfs_lock_owner_destroy(nlop
);
3053 * Mark a lock owner as busy because we are about to
3054 * start an operation that uses and updates lock owner state.
3057 nfs_lock_owner_set_busy(struct nfs_lock_owner
*nlop
, thread_t thd
)
3059 struct nfsmount
*nmp
;
3060 struct timespec ts
= {2, 0};
3061 int error
= 0, slpflag
;
3063 nmp
= nlop
->nlo_open_owner
->noo_mount
;
3064 if (nfs_mount_gone(nmp
))
3066 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
3068 lck_mtx_lock(&nlop
->nlo_lock
);
3069 while (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
) {
3070 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
3072 nlop
->nlo_flags
|= NFS_LOCK_OWNER_WANT
;
3073 msleep(nlop
, &nlop
->nlo_lock
, slpflag
, "nfs_lock_owner_set_busy", &ts
);
3077 nlop
->nlo_flags
|= NFS_LOCK_OWNER_BUSY
;
3078 lck_mtx_unlock(&nlop
->nlo_lock
);
3084 * Clear the busy flag on a lock owner and wake up anyone waiting
3088 nfs_lock_owner_clear_busy(struct nfs_lock_owner
*nlop
)
3092 lck_mtx_lock(&nlop
->nlo_lock
);
3093 if (!(nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
3094 panic("nfs_lock_owner_clear_busy");
3095 wanted
= (nlop
->nlo_flags
& NFS_LOCK_OWNER_WANT
);
3096 nlop
->nlo_flags
&= ~(NFS_LOCK_OWNER_BUSY
|NFS_LOCK_OWNER_WANT
);
3097 lck_mtx_unlock(&nlop
->nlo_lock
);
3103 * Insert a held lock into a lock owner's sorted list.
3104 * (flock locks are always inserted at the head the list)
3107 nfs_lock_owner_insert_held_lock(struct nfs_lock_owner
*nlop
, struct nfs_file_lock
*newnflp
)
3109 struct nfs_file_lock
*nflp
;
3111 /* insert new lock in lock owner's held lock list */
3112 lck_mtx_lock(&nlop
->nlo_lock
);
3113 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) {
3114 TAILQ_INSERT_HEAD(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3116 TAILQ_FOREACH(nflp
, &nlop
->nlo_locks
, nfl_lolink
) {
3117 if (newnflp
->nfl_start
< nflp
->nfl_start
)
3121 TAILQ_INSERT_BEFORE(nflp
, newnflp
, nfl_lolink
);
3123 TAILQ_INSERT_TAIL(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3125 lck_mtx_unlock(&nlop
->nlo_lock
);
3129 * Get a file lock structure for this lock owner.
3131 struct nfs_file_lock
*
3132 nfs_file_lock_alloc(struct nfs_lock_owner
*nlop
)
3134 struct nfs_file_lock
*nflp
= NULL
;
3136 lck_mtx_lock(&nlop
->nlo_lock
);
3137 if (!nlop
->nlo_alock
.nfl_owner
) {
3138 nflp
= &nlop
->nlo_alock
;
3139 nflp
->nfl_owner
= nlop
;
3141 lck_mtx_unlock(&nlop
->nlo_lock
);
3143 MALLOC(nflp
, struct nfs_file_lock
*, sizeof(struct nfs_file_lock
), M_TEMP
, M_WAITOK
);
3146 bzero(nflp
, sizeof(*nflp
));
3147 nflp
->nfl_flags
|= NFS_FILE_LOCK_ALLOC
;
3148 nflp
->nfl_owner
= nlop
;
3150 nfs_lock_owner_ref(nlop
);
3155 * destroy the given NFS file lock structure
3158 nfs_file_lock_destroy(struct nfs_file_lock
*nflp
)
3160 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3162 if (nflp
->nfl_flags
& NFS_FILE_LOCK_ALLOC
) {
3163 nflp
->nfl_owner
= NULL
;
3166 lck_mtx_lock(&nlop
->nlo_lock
);
3167 bzero(nflp
, sizeof(nflp
));
3168 lck_mtx_unlock(&nlop
->nlo_lock
);
3170 nfs_lock_owner_rele(nlop
);
3174 * Check if one file lock conflicts with another.
3175 * (nflp1 is the new lock. nflp2 is the existing lock.)
3178 nfs_file_lock_conflict(struct nfs_file_lock
*nflp1
, struct nfs_file_lock
*nflp2
, int *willsplit
)
3180 /* no conflict if lock is dead */
3181 if ((nflp1
->nfl_flags
& NFS_FILE_LOCK_DEAD
) || (nflp2
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
3183 /* no conflict if it's ours - unless the lock style doesn't match */
3184 if ((nflp1
->nfl_owner
== nflp2
->nfl_owner
) &&
3185 ((nflp1
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == (nflp2
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))) {
3186 if (willsplit
&& (nflp1
->nfl_type
!= nflp2
->nfl_type
) &&
3187 (nflp1
->nfl_start
> nflp2
->nfl_start
) &&
3188 (nflp1
->nfl_end
< nflp2
->nfl_end
))
3192 /* no conflict if ranges don't overlap */
3193 if ((nflp1
->nfl_start
> nflp2
->nfl_end
) || (nflp1
->nfl_end
< nflp2
->nfl_start
))
3195 /* no conflict if neither lock is exclusive */
3196 if ((nflp1
->nfl_type
!= F_WRLCK
) && (nflp2
->nfl_type
!= F_WRLCK
))
3203 * Send an NFSv4 LOCK RPC to the server.
3208 struct nfs_open_file
*nofp
,
3209 struct nfs_file_lock
*nflp
,
3215 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3216 struct nfsmount
*nmp
;
3217 struct nfsm_chain nmreq
, nmrep
;
3220 int error
= 0, lockerror
= ENOENT
, newlocker
, numops
, status
;
3221 struct nfsreq_secinfo_args si
;
3224 if (nfs_mount_gone(nmp
))
3226 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3229 newlocker
= (nlop
->nlo_stategenid
!= nmp
->nm_stategenid
);
3230 locktype
= (nflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
) ?
3231 ((nflp
->nfl_type
== F_WRLCK
) ?
3232 NFS_LOCK_TYPE_WRITEW
:
3233 NFS_LOCK_TYPE_READW
) :
3234 ((nflp
->nfl_type
== F_WRLCK
) ?
3235 NFS_LOCK_TYPE_WRITE
:
3236 NFS_LOCK_TYPE_READ
);
3238 error
= nfs_open_file_set_busy(nofp
, thd
);
3241 error
= nfs_open_owner_set_busy(nofp
->nof_owner
, thd
);
3243 nfs_open_file_clear_busy(nofp
);
3246 if (!nlop
->nlo_open_owner
) {
3247 nfs_open_owner_ref(nofp
->nof_owner
);
3248 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3251 error
= nfs_lock_owner_set_busy(nlop
, thd
);
3254 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3255 nfs_open_file_clear_busy(nofp
);
3260 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3261 nfsm_chain_null(&nmreq
);
3262 nfsm_chain_null(&nmrep
);
3264 // PUTFH, GETATTR, LOCK
3266 nfsm_chain_build_alloc_init(error
, &nmreq
, 33 * NFSX_UNSIGNED
);
3267 nfsm_chain_add_compound_header(error
, &nmreq
, "lock", numops
);
3269 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3270 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3272 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3273 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3275 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCK
);
3276 nfsm_chain_add_32(error
, &nmreq
, locktype
);
3277 nfsm_chain_add_32(error
, &nmreq
, reclaim
);
3278 nfsm_chain_add_64(error
, &nmreq
, nflp
->nfl_start
);
3279 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
));
3280 nfsm_chain_add_32(error
, &nmreq
, newlocker
);
3282 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_owner
->noo_seqid
);
3283 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
3284 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3285 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3287 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3288 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3290 nfsm_chain_build_done(error
, &nmreq
);
3291 nfsm_assert(error
, (numops
== 0), EPROTO
);
3294 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3296 if ((lockerror
= nfs_node_lock(np
)))
3298 nfsm_chain_skip_tag(error
, &nmrep
);
3299 nfsm_chain_get_32(error
, &nmrep
, numops
);
3300 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3302 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3303 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3305 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCK
);
3306 nfs_owner_seqid_increment(newlocker
? nofp
->nof_owner
: NULL
, nlop
, error
);
3307 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3309 /* Update the lock owner's stategenid once it appears the server has state for it. */
3310 /* We determine this by noting the request was successful (we got a stateid). */
3311 if (newlocker
&& !error
)
3312 nlop
->nlo_stategenid
= nmp
->nm_stategenid
;
3315 nfs_node_unlock(np
);
3316 nfs_lock_owner_clear_busy(nlop
);
3318 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3319 nfs_open_file_clear_busy(nofp
);
3321 nfsm_chain_cleanup(&nmreq
);
3322 nfsm_chain_cleanup(&nmrep
);
3327 * Send an NFSv4 LOCKU RPC to the server.
3332 struct nfs_lock_owner
*nlop
,
3340 struct nfsmount
*nmp
;
3341 struct nfsm_chain nmreq
, nmrep
;
3343 int error
= 0, lockerror
= ENOENT
, numops
, status
;
3344 struct nfsreq_secinfo_args si
;
3347 if (nfs_mount_gone(nmp
))
3349 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3352 error
= nfs_lock_owner_set_busy(nlop
, NULL
);
3356 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3357 nfsm_chain_null(&nmreq
);
3358 nfsm_chain_null(&nmrep
);
3360 // PUTFH, GETATTR, LOCKU
3362 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3363 nfsm_chain_add_compound_header(error
, &nmreq
, "unlock", numops
);
3365 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3366 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3368 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3369 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3371 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKU
);
3372 nfsm_chain_add_32(error
, &nmreq
, (type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3373 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3374 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3375 nfsm_chain_add_64(error
, &nmreq
, start
);
3376 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3377 nfsm_chain_build_done(error
, &nmreq
);
3378 nfsm_assert(error
, (numops
== 0), EPROTO
);
3381 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3383 if ((lockerror
= nfs_node_lock(np
)))
3385 nfsm_chain_skip_tag(error
, &nmrep
);
3386 nfsm_chain_get_32(error
, &nmrep
, numops
);
3387 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3389 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3390 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3392 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKU
);
3393 nfs_owner_seqid_increment(NULL
, nlop
, error
);
3394 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3397 nfs_node_unlock(np
);
3398 nfs_lock_owner_clear_busy(nlop
);
3399 nfsm_chain_cleanup(&nmreq
);
3400 nfsm_chain_cleanup(&nmrep
);
3405 * Send an NFSv4 LOCKT RPC to the server.
3410 struct nfs_lock_owner
*nlop
,
3416 struct nfsmount
*nmp
;
3417 struct nfsm_chain nmreq
, nmrep
;
3418 uint64_t xid
, val64
= 0;
3420 int error
= 0, lockerror
, numops
, status
;
3421 struct nfsreq_secinfo_args si
;
3424 if (nfs_mount_gone(nmp
))
3426 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3430 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3431 nfsm_chain_null(&nmreq
);
3432 nfsm_chain_null(&nmrep
);
3434 // PUTFH, GETATTR, LOCKT
3436 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3437 nfsm_chain_add_compound_header(error
, &nmreq
, "locktest", numops
);
3439 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3440 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3442 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3443 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3445 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKT
);
3446 nfsm_chain_add_32(error
, &nmreq
, (fl
->l_type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3447 nfsm_chain_add_64(error
, &nmreq
, start
);
3448 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3449 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3450 nfsm_chain_build_done(error
, &nmreq
);
3451 nfsm_assert(error
, (numops
== 0), EPROTO
);
3454 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
3456 if ((lockerror
= nfs_node_lock(np
)))
3458 nfsm_chain_skip_tag(error
, &nmrep
);
3459 nfsm_chain_get_32(error
, &nmrep
, numops
);
3460 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3462 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3463 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3465 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKT
);
3466 if (error
== NFSERR_DENIED
) {
3468 nfsm_chain_get_64(error
, &nmrep
, fl
->l_start
);
3469 nfsm_chain_get_64(error
, &nmrep
, val64
);
3470 fl
->l_len
= (val64
== UINT64_MAX
) ? 0 : val64
;
3471 nfsm_chain_get_32(error
, &nmrep
, val
);
3472 fl
->l_type
= (val
== NFS_LOCK_TYPE_WRITE
) ? F_WRLCK
: F_RDLCK
;
3474 fl
->l_whence
= SEEK_SET
;
3475 } else if (!error
) {
3476 fl
->l_type
= F_UNLCK
;
3480 nfs_node_unlock(np
);
3481 nfsm_chain_cleanup(&nmreq
);
3482 nfsm_chain_cleanup(&nmrep
);
3488 * Check for any conflicts with the given lock.
3490 * Checking for a lock doesn't require the file to be opened.
3491 * So we skip all the open owner, open file, lock owner work
3492 * and just check for a conflicting lock.
3495 nfs_advlock_getlock(
3497 struct nfs_lock_owner
*nlop
,
3503 struct nfsmount
*nmp
;
3504 struct nfs_file_lock
*nflp
;
3505 int error
= 0, answered
= 0;
3508 if (nfs_mount_gone(nmp
))
3512 if ((error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
))))
3515 lck_mtx_lock(&np
->n_openlock
);
3516 /* scan currently held locks for conflict */
3517 TAILQ_FOREACH(nflp
, &np
->n_locks
, nfl_link
) {
3518 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3520 if ((start
<= nflp
->nfl_end
) && (end
>= nflp
->nfl_start
) &&
3521 ((fl
->l_type
== F_WRLCK
) || (nflp
->nfl_type
== F_WRLCK
)))
3525 /* found a conflicting lock */
3526 fl
->l_type
= nflp
->nfl_type
;
3527 fl
->l_pid
= (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_FLOCK
) ? -1 : nflp
->nfl_owner
->nlo_pid
;
3528 fl
->l_start
= nflp
->nfl_start
;
3529 fl
->l_len
= NFS_FLOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
);
3530 fl
->l_whence
= SEEK_SET
;
3532 } else if ((np
->n_openflags
& N_DELEG_WRITE
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3534 * If we have a write delegation, we know there can't be other
3535 * locks on the server. So the answer is no conflicting lock found.
3537 fl
->l_type
= F_UNLCK
;
3540 lck_mtx_unlock(&np
->n_openlock
);
3542 nfs_mount_state_in_use_end(nmp
, 0);
3546 /* no conflict found locally, so ask the server */
3547 error
= nmp
->nm_funcs
->nf_getlock_rpc(np
, nlop
, fl
, start
, end
, ctx
);
3549 if (nfs_mount_state_in_use_end(nmp
, error
))
3555 * Acquire a file lock for the given range.
3557 * Add the lock (request) to the lock queue.
3558 * Scan the lock queue for any conflicting locks.
3559 * If a conflict is found, block or return an error.
3560 * Once end of queue is reached, send request to the server.
3561 * If the server grants the lock, scan the lock queue and
3562 * update any existing locks. Then (optionally) scan the
3563 * queue again to coalesce any locks adjacent to the new one.
3566 nfs_advlock_setlock(
3568 struct nfs_open_file
*nofp
,
3569 struct nfs_lock_owner
*nlop
,
3577 struct nfsmount
*nmp
;
3578 struct nfs_file_lock
*newnflp
, *nflp
, *nflp2
= NULL
, *nextnflp
, *flocknflp
= NULL
;
3579 struct nfs_file_lock
*coalnflp
;
3580 int error
= 0, error2
, willsplit
= 0, delay
, slpflag
, busy
= 0, inuse
= 0, restart
, inqueue
= 0;
3581 struct timespec ts
= {1, 0};
3584 if (nfs_mount_gone(nmp
))
3586 slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
3588 if ((type
!= F_RDLCK
) && (type
!= F_WRLCK
))
3591 /* allocate a new lock */
3592 newnflp
= nfs_file_lock_alloc(nlop
);
3595 newnflp
->nfl_start
= start
;
3596 newnflp
->nfl_end
= end
;
3597 newnflp
->nfl_type
= type
;
3599 newnflp
->nfl_flags
|= NFS_FILE_LOCK_WAIT
;
3600 newnflp
->nfl_flags
|= style
;
3601 newnflp
->nfl_flags
|= NFS_FILE_LOCK_BLOCKED
;
3603 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && (type
== F_WRLCK
)) {
3605 * For exclusive flock-style locks, if we block waiting for the
3606 * lock, we need to first release any currently held shared
3607 * flock-style lock. So, the first thing we do is check if we
3608 * have a shared flock-style lock.
3610 nflp
= TAILQ_FIRST(&nlop
->nlo_locks
);
3611 if (nflp
&& ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_FLOCK
))
3613 if (nflp
&& (nflp
->nfl_type
!= F_RDLCK
))
3620 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3624 if (np
->n_flag
& NREVOKE
) {
3626 nfs_mount_state_in_use_end(nmp
, 0);
3630 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
3631 nfs_mount_state_in_use_end(nmp
, 0);
3633 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
3639 lck_mtx_lock(&np
->n_openlock
);
3641 /* insert new lock at beginning of list */
3642 TAILQ_INSERT_HEAD(&np
->n_locks
, newnflp
, nfl_link
);
3646 /* scan current list of locks (held and pending) for conflicts */
3647 for (nflp
= TAILQ_NEXT(newnflp
, nfl_link
); nflp
; nflp
= nextnflp
) {
3648 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
3649 if (!nfs_file_lock_conflict(newnflp
, nflp
, &willsplit
))
3652 if (!(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3656 /* Block until this lock is no longer held. */
3657 if (nflp
->nfl_blockcnt
== UINT_MAX
) {
3661 nflp
->nfl_blockcnt
++;
3664 /* release any currently held shared lock before sleeping */
3665 lck_mtx_unlock(&np
->n_openlock
);
3666 nfs_mount_state_in_use_end(nmp
, 0);
3668 error
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3671 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3673 lck_mtx_lock(&np
->n_openlock
);
3677 lck_mtx_lock(&np
->n_openlock
);
3678 /* no need to block/sleep if the conflict is gone */
3679 if (!nfs_file_lock_conflict(newnflp
, nflp
, NULL
))
3682 msleep(nflp
, &np
->n_openlock
, slpflag
, "nfs_advlock_setlock_blocked", &ts
);
3684 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3685 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3686 /* looks like we have a recover pending... restart */
3688 lck_mtx_unlock(&np
->n_openlock
);
3689 nfs_mount_state_in_use_end(nmp
, 0);
3691 lck_mtx_lock(&np
->n_openlock
);
3694 if (!error
&& (np
->n_flag
& NREVOKE
))
3696 } while (!error
&& nfs_file_lock_conflict(newnflp
, nflp
, NULL
));
3697 nflp
->nfl_blockcnt
--;
3698 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) && !nflp
->nfl_blockcnt
) {
3699 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3700 nfs_file_lock_destroy(nflp
);
3702 if (error
|| restart
)
3704 /* We have released n_openlock and we can't trust that nextnflp is still valid. */
3705 /* So, start this lock-scanning loop over from where it started. */
3706 nextnflp
= TAILQ_NEXT(newnflp
, nfl_link
);
3708 lck_mtx_unlock(&np
->n_openlock
);
3716 * It looks like this operation is splitting a lock.
3717 * We allocate a new lock now so we don't have to worry
3718 * about the allocation failing after we've updated some state.
3720 nflp2
= nfs_file_lock_alloc(nlop
);
3727 /* once scan for local conflicts is clear, send request to server */
3728 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
3733 /* do we have a delegation? (that we're not returning?) */
3734 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3735 if (np
->n_openflags
& N_DELEG_WRITE
) {
3736 /* with a write delegation, just take the lock delegated */
3737 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DELEGATED
;
3739 /* make sure the lock owner knows its open owner */
3740 if (!nlop
->nlo_open_owner
) {
3741 nfs_open_owner_ref(nofp
->nof_owner
);
3742 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3747 * If we don't have any non-delegated opens but we do have
3748 * delegated opens, then we need to first claim the delegated
3749 * opens so that the lock request on the server can be associated
3750 * with an open it knows about.
3752 if ((!nofp
->nof_rw_drw
&& !nofp
->nof_w_drw
&& !nofp
->nof_r_drw
&&
3753 !nofp
->nof_rw_dw
&& !nofp
->nof_w_dw
&& !nofp
->nof_r_dw
&&
3754 !nofp
->nof_rw
&& !nofp
->nof_w
&& !nofp
->nof_r
) &&
3755 (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
3756 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
3757 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)) {
3758 error
= nfs4_claim_delegated_state_for_open_file(nofp
, 0);
3764 if (np
->n_flag
& NREVOKE
)
3767 error
= nmp
->nm_funcs
->nf_setlock_rpc(np
, nofp
, newnflp
, 0, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3768 if (!error
|| ((error
!= NFSERR_DENIED
) && (error
!= NFSERR_GRACE
)))
3770 /* request was denied due to either conflict or grace period */
3771 if ((error
== NFSERR_DENIED
) && !(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3776 /* release any currently held shared lock before sleeping */
3777 nfs_open_state_clear_busy(np
);
3779 nfs_mount_state_in_use_end(nmp
, 0);
3781 error2
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3784 error2
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3787 error2
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
));
3796 * Wait a little bit and send the request again.
3797 * Except for retries of blocked v2/v3 request where we've already waited a bit.
3799 if ((nmp
->nm_vers
>= NFS_VER4
) || (error
== NFSERR_GRACE
)) {
3800 if (error
== NFSERR_GRACE
)
3804 tsleep(newnflp
, slpflag
, "nfs_advlock_setlock_delay", delay
* (hz
/2));
3807 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3808 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3809 /* looks like we have a recover pending... restart */
3810 nfs_open_state_clear_busy(np
);
3812 nfs_mount_state_in_use_end(nmp
, 0);
3816 if (!error
&& (np
->n_flag
& NREVOKE
))
3821 if (nfs_mount_state_error_should_restart(error
)) {
3822 /* looks like we need to restart this operation */
3824 nfs_open_state_clear_busy(np
);
3828 nfs_mount_state_in_use_end(nmp
, error
);
3833 lck_mtx_lock(&np
->n_openlock
);
3834 newnflp
->nfl_flags
&= ~NFS_FILE_LOCK_BLOCKED
;
3836 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3837 if (newnflp
->nfl_blockcnt
) {
3838 /* wake up anyone blocked on this lock */
3841 /* remove newnflp from lock list and destroy */
3843 TAILQ_REMOVE(&np
->n_locks
, newnflp
, nfl_link
);
3844 nfs_file_lock_destroy(newnflp
);
3846 lck_mtx_unlock(&np
->n_openlock
);
3848 nfs_open_state_clear_busy(np
);
3850 nfs_mount_state_in_use_end(nmp
, error
);
3852 nfs_file_lock_destroy(nflp2
);
3856 /* server granted the lock */
3859 * Scan for locks to update.
3861 * Locks completely covered are killed.
3862 * At most two locks may need to be clipped.
3863 * It's possible that a single lock may need to be split.
3865 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3866 if (nflp
== newnflp
)
3868 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3870 if (nflp
->nfl_owner
!= nlop
)
3872 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))
3874 if ((newnflp
->nfl_start
> nflp
->nfl_end
) || (newnflp
->nfl_end
< nflp
->nfl_start
))
3876 /* here's one to update */
3877 if ((newnflp
->nfl_start
<= nflp
->nfl_start
) && (newnflp
->nfl_end
>= nflp
->nfl_end
)) {
3878 /* The entire lock is being replaced. */
3879 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3880 lck_mtx_lock(&nlop
->nlo_lock
);
3881 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3882 lck_mtx_unlock(&nlop
->nlo_lock
);
3883 /* lock will be destroyed below, if no waiters */
3884 } else if ((newnflp
->nfl_start
> nflp
->nfl_start
) && (newnflp
->nfl_end
< nflp
->nfl_end
)) {
3885 /* We're replacing a range in the middle of a lock. */
3886 /* The current lock will be split into two locks. */
3887 /* Update locks and insert new lock after current lock. */
3888 nflp2
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
3889 nflp2
->nfl_type
= nflp
->nfl_type
;
3890 nflp2
->nfl_start
= newnflp
->nfl_end
+ 1;
3891 nflp2
->nfl_end
= nflp
->nfl_end
;
3892 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3893 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, nflp2
, nfl_link
);
3894 nfs_lock_owner_insert_held_lock(nlop
, nflp2
);
3897 } else if (newnflp
->nfl_start
> nflp
->nfl_start
) {
3898 /* We're replacing the end of a lock. */
3899 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3900 } else if (newnflp
->nfl_end
< nflp
->nfl_end
) {
3901 /* We're replacing the start of a lock. */
3902 nflp
->nfl_start
= newnflp
->nfl_end
+ 1;
3904 if (nflp
->nfl_blockcnt
) {
3905 /* wake up anyone blocked on this lock */
3907 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
3908 /* remove nflp from lock list and destroy */
3909 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3910 nfs_file_lock_destroy(nflp
);
3914 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
3917 * POSIX locks should be coalesced when possible.
3919 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) && (nofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)) {
3921 * Walk through the lock queue and check each of our held locks with
3922 * the previous and next locks in the lock owner's "held lock list".
3923 * If the two locks can be coalesced, we merge the current lock into
3924 * the other (previous or next) lock. Merging this way makes sure that
3925 * lock ranges are always merged forward in the lock queue. This is
3926 * important because anyone blocked on the lock being "merged away"
3927 * will still need to block on that range and it will simply continue
3928 * checking locks that are further down the list.
3930 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3931 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3933 if (nflp
->nfl_owner
!= nlop
)
3935 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_POSIX
)
3937 if (((coalnflp
= TAILQ_PREV(nflp
, nfs_file_lock_queue
, nfl_lolink
))) &&
3938 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3939 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3940 (coalnflp
->nfl_end
== (nflp
->nfl_start
- 1))) {
3941 coalnflp
->nfl_end
= nflp
->nfl_end
;
3942 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3943 lck_mtx_lock(&nlop
->nlo_lock
);
3944 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3945 lck_mtx_unlock(&nlop
->nlo_lock
);
3946 } else if (((coalnflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
3947 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3948 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3949 (coalnflp
->nfl_start
== (nflp
->nfl_end
+ 1))) {
3950 coalnflp
->nfl_start
= nflp
->nfl_start
;
3951 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3952 lck_mtx_lock(&nlop
->nlo_lock
);
3953 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3954 lck_mtx_unlock(&nlop
->nlo_lock
);
3956 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
3958 if (nflp
->nfl_blockcnt
) {
3959 /* wake up anyone blocked on this lock */
3962 /* remove nflp from lock list and destroy */
3963 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3964 nfs_file_lock_destroy(nflp
);
3969 lck_mtx_unlock(&np
->n_openlock
);
3970 nfs_open_state_clear_busy(np
);
3971 nfs_mount_state_in_use_end(nmp
, error
);
3974 nfs_file_lock_destroy(nflp2
);
3979 * Release all (same style) locks within the given range.
3984 struct nfs_open_file
*nofp
,
3985 struct nfs_lock_owner
*nlop
,
3991 struct nfsmount
*nmp
;
3992 struct nfs_file_lock
*nflp
, *nextnflp
, *newnflp
= NULL
;
3993 int error
= 0, willsplit
= 0, send_unlock_rpcs
= 1;
3996 if (nfs_mount_gone(nmp
))
4000 if ((error
= nfs_mount_state_in_use_start(nmp
, NULL
)))
4002 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
4003 nfs_mount_state_in_use_end(nmp
, 0);
4004 error
= nfs4_reopen(nofp
, NULL
);
4009 if ((error
= nfs_open_state_set_busy(np
, NULL
))) {
4010 nfs_mount_state_in_use_end(nmp
, error
);
4014 lck_mtx_lock(&np
->n_openlock
);
4015 if ((start
> 0) && (end
< UINT64_MAX
) && !willsplit
) {
4017 * We may need to allocate a new lock if an existing lock gets split.
4018 * So, we first scan the list to check for a split, and if there's
4019 * going to be one, we'll allocate one now.
4021 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
4022 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
4024 if (nflp
->nfl_owner
!= nlop
)
4026 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
4028 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
4030 if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
4036 lck_mtx_unlock(&np
->n_openlock
);
4037 nfs_open_state_clear_busy(np
);
4038 nfs_mount_state_in_use_end(nmp
, 0);
4039 newnflp
= nfs_file_lock_alloc(nlop
);
4047 * Free all of our locks in the given range.
4049 * Note that this process requires sending requests to the server.
4050 * Because of this, we will release the n_openlock while performing
4051 * the unlock RPCs. The N_OPENBUSY state keeps the state of *held*
4052 * locks from changing underneath us. However, other entries in the
4053 * list may be removed. So we need to be careful walking the list.
4057 * Don't unlock ranges that are held by other-style locks.
4058 * If style is posix, don't send any unlock rpcs if flock is held.
4059 * If we unlock an flock, don't send unlock rpcs for any posix-style
4060 * ranges held - instead send unlocks for the ranges not held.
4062 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) &&
4063 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4064 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
))
4065 send_unlock_rpcs
= 0;
4066 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) &&
4067 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4068 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) &&
4069 ((nflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
4070 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
)) {
4072 int type
= TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_type
;
4073 int delegated
= (TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_flags
& NFS_FILE_LOCK_DELEGATED
);
4074 while (!delegated
&& nflp
) {
4075 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) {
4076 /* unlock the range preceding this lock */
4077 lck_mtx_unlock(&np
->n_openlock
);
4078 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, nflp
->nfl_start
-1, 0,
4079 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4080 if (nfs_mount_state_error_should_restart(error
)) {
4081 nfs_open_state_clear_busy(np
);
4082 nfs_mount_state_in_use_end(nmp
, error
);
4085 lck_mtx_lock(&np
->n_openlock
);
4088 s
= nflp
->nfl_end
+1;
4090 nflp
= TAILQ_NEXT(nflp
, nfl_lolink
);
4093 lck_mtx_unlock(&np
->n_openlock
);
4094 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, end
, 0,
4095 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4096 if (nfs_mount_state_error_should_restart(error
)) {
4097 nfs_open_state_clear_busy(np
);
4098 nfs_mount_state_in_use_end(nmp
, error
);
4101 lck_mtx_lock(&np
->n_openlock
);
4105 send_unlock_rpcs
= 0;
4108 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
4109 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
4111 if (nflp
->nfl_owner
!= nlop
)
4113 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
4115 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
4117 /* here's one to unlock */
4118 if ((start
<= nflp
->nfl_start
) && (end
>= nflp
->nfl_end
)) {
4119 /* The entire lock is being unlocked. */
4120 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4121 lck_mtx_unlock(&np
->n_openlock
);
4122 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, nflp
->nfl_end
, 0,
4123 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4124 if (nfs_mount_state_error_should_restart(error
)) {
4125 nfs_open_state_clear_busy(np
);
4126 nfs_mount_state_in_use_end(nmp
, error
);
4129 lck_mtx_lock(&np
->n_openlock
);
4131 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4134 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
4135 lck_mtx_lock(&nlop
->nlo_lock
);
4136 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
4137 lck_mtx_unlock(&nlop
->nlo_lock
);
4138 /* lock will be destroyed below, if no waiters */
4139 } else if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
4140 /* We're unlocking a range in the middle of a lock. */
4141 /* The current lock will be split into two locks. */
4142 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4143 lck_mtx_unlock(&np
->n_openlock
);
4144 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, end
, 0,
4145 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4146 if (nfs_mount_state_error_should_restart(error
)) {
4147 nfs_open_state_clear_busy(np
);
4148 nfs_mount_state_in_use_end(nmp
, error
);
4151 lck_mtx_lock(&np
->n_openlock
);
4155 /* update locks and insert new lock after current lock */
4156 newnflp
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
4157 newnflp
->nfl_type
= nflp
->nfl_type
;
4158 newnflp
->nfl_start
= end
+ 1;
4159 newnflp
->nfl_end
= nflp
->nfl_end
;
4160 nflp
->nfl_end
= start
- 1;
4161 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, newnflp
, nfl_link
);
4162 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
4165 } else if (start
> nflp
->nfl_start
) {
4166 /* We're unlocking the end of a lock. */
4167 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4168 lck_mtx_unlock(&np
->n_openlock
);
4169 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, nflp
->nfl_end
, 0,
4170 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4171 if (nfs_mount_state_error_should_restart(error
)) {
4172 nfs_open_state_clear_busy(np
);
4173 nfs_mount_state_in_use_end(nmp
, error
);
4176 lck_mtx_lock(&np
->n_openlock
);
4178 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4181 nflp
->nfl_end
= start
- 1;
4182 } else if (end
< nflp
->nfl_end
) {
4183 /* We're unlocking the start of a lock. */
4184 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4185 lck_mtx_unlock(&np
->n_openlock
);
4186 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, end
, 0,
4187 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4188 if (nfs_mount_state_error_should_restart(error
)) {
4189 nfs_open_state_clear_busy(np
);
4190 nfs_mount_state_in_use_end(nmp
, error
);
4193 lck_mtx_lock(&np
->n_openlock
);
4195 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4198 nflp
->nfl_start
= end
+ 1;
4200 if (nflp
->nfl_blockcnt
) {
4201 /* wake up anyone blocked on this lock */
4203 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
4204 /* remove nflp from lock list and destroy */
4205 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
4206 nfs_file_lock_destroy(nflp
);
4210 lck_mtx_unlock(&np
->n_openlock
);
4211 nfs_open_state_clear_busy(np
);
4212 nfs_mount_state_in_use_end(nmp
, 0);
4215 nfs_file_lock_destroy(newnflp
);
4220 * NFSv4 advisory file locking
4224 struct vnop_advlock_args
/* {
4225 struct vnodeop_desc *a_desc;
4231 vfs_context_t a_context;
4234 vnode_t vp
= ap
->a_vp
;
4235 nfsnode_t np
= VTONFS(ap
->a_vp
);
4236 struct flock
*fl
= ap
->a_fl
;
4238 int flags
= ap
->a_flags
;
4239 vfs_context_t ctx
= ap
->a_context
;
4240 struct nfsmount
*nmp
;
4241 struct nfs_open_owner
*noop
= NULL
;
4242 struct nfs_open_file
*nofp
= NULL
;
4243 struct nfs_lock_owner
*nlop
= NULL
;
4245 uint64_t start
, end
;
4246 int error
= 0, modified
, style
;
4248 #define OFF_MAX QUAD_MAX
4250 nmp
= VTONMP(ap
->a_vp
);
4251 if (nfs_mount_gone(nmp
))
4253 lck_mtx_lock(&nmp
->nm_lock
);
4254 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
)) {
4255 lck_mtx_unlock(&nmp
->nm_lock
);
4258 lck_mtx_unlock(&nmp
->nm_lock
);
4260 if (np
->n_flag
& NREVOKE
)
4262 vtype
= vnode_vtype(ap
->a_vp
);
4263 if (vtype
== VDIR
) /* ignore lock requests on directories */
4265 if (vtype
!= VREG
) /* anything other than regular files is invalid */
4268 /* Convert the flock structure into a start and end. */
4269 switch (fl
->l_whence
) {
4273 * Caller is responsible for adding any necessary offset
4274 * to fl->l_start when SEEK_CUR is used.
4276 lstart
= fl
->l_start
;
4279 /* need to flush, and refetch attributes to make */
4280 /* sure we have the correct end of file offset */
4281 if ((error
= nfs_node_lock(np
)))
4283 modified
= (np
->n_flag
& NMODIFIED
);
4284 nfs_node_unlock(np
);
4285 if (modified
&& ((error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1))))
4287 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
4289 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
4290 if ((np
->n_size
> OFF_MAX
) ||
4291 ((fl
->l_start
> 0) && (np
->n_size
> (u_quad_t
)(OFF_MAX
- fl
->l_start
))))
4293 lstart
= np
->n_size
+ fl
->l_start
;
4294 nfs_data_unlock(np
);
4304 if (fl
->l_len
== 0) {
4306 } else if (fl
->l_len
> 0) {
4307 if ((fl
->l_len
- 1) > (OFF_MAX
- lstart
))
4309 end
= start
- 1 + fl
->l_len
;
4310 } else { /* l_len is negative */
4311 if ((lstart
+ fl
->l_len
) < 0)
4316 if ((nmp
->nm_vers
== NFS_VER2
) && ((start
> INT32_MAX
) || (fl
->l_len
&& (end
> INT32_MAX
))))
4319 style
= (flags
& F_FLOCK
) ? NFS_FILE_LOCK_STYLE_FLOCK
: NFS_FILE_LOCK_STYLE_POSIX
;
4320 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && ((start
!= 0) || (end
!= UINT64_MAX
)))
4323 /* find the lock owner, alloc if not unlock */
4324 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), (op
!= F_UNLCK
));
4326 error
= (op
== F_UNLCK
) ? 0 : ENOMEM
;
4328 NP(np
, "nfs_vnop_advlock: no lock owner, error %d", error
);
4332 if (op
== F_GETLK
) {
4333 error
= nfs_advlock_getlock(np
, nlop
, fl
, start
, end
, ctx
);
4335 /* find the open owner */
4336 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
4338 NP(np
, "nfs_vnop_advlock: no open owner %d", kauth_cred_getuid(vfs_context_ucred(ctx
)));
4342 /* find the open file */
4344 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
4347 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
4348 NP(np
, "nfs_vnop_advlock: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
4351 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4352 error
= nfs4_reopen(nofp
, ((op
== F_UNLCK
) ? NULL
: vfs_context_thread(ctx
)));
4358 NP(np
, "nfs_vnop_advlock: no open file %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
4361 if (op
== F_UNLCK
) {
4362 error
= nfs_advlock_unlock(np
, nofp
, nlop
, start
, end
, style
, ctx
);
4363 } else if ((op
== F_SETLK
) || (op
== F_SETLKW
)) {
4364 if ((op
== F_SETLK
) && (flags
& F_WAIT
))
4366 error
= nfs_advlock_setlock(np
, nofp
, nlop
, op
, start
, end
, style
, fl
->l_type
, ctx
);
4368 /* not getlk, unlock or lock? */
4375 nfs_lock_owner_rele(nlop
);
4377 nfs_open_owner_rele(noop
);
4382 * Check if an open owner holds any locks on a file.
4385 nfs_check_for_locks(struct nfs_open_owner
*noop
, struct nfs_open_file
*nofp
)
4387 struct nfs_lock_owner
*nlop
;
4389 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
4390 if (nlop
->nlo_open_owner
!= noop
)
4392 if (!TAILQ_EMPTY(&nlop
->nlo_locks
))
4395 return (nlop
? 1 : 0);
4399 * Reopen simple (no deny, no locks) open state that was lost.
4402 nfs4_reopen(struct nfs_open_file
*nofp
, thread_t thd
)
4404 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4405 struct nfsmount
*nmp
= NFSTONMP(nofp
->nof_np
);
4406 nfsnode_t np
= nofp
->nof_np
;
4407 vnode_t vp
= NFSTOV(np
);
4409 struct componentname cn
;
4410 const char *vname
= NULL
;
4411 const char *name
= NULL
;
4413 char smallname
[128];
4414 char *filename
= NULL
;
4415 int error
= 0, done
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
4416 struct timespec ts
= { 1, 0 };
4418 lck_mtx_lock(&nofp
->nof_lock
);
4419 while (nofp
->nof_flags
& NFS_OPEN_FILE_REOPENING
) {
4420 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
4422 msleep(&nofp
->nof_flags
, &nofp
->nof_lock
, slpflag
|(PZERO
-1), "nfsreopenwait", &ts
);
4425 if (error
|| !(nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4426 lck_mtx_unlock(&nofp
->nof_lock
);
4429 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPENING
;
4430 lck_mtx_unlock(&nofp
->nof_lock
);
4432 nfs_node_lock_force(np
);
4433 if ((vnode_vtype(vp
) != VDIR
) && np
->n_sillyrename
) {
4435 * The node's been sillyrenamed, so we need to use
4436 * the sillyrename directory/name to do the open.
4438 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
4439 dvp
= NFSTOV(nsp
->nsr_dnp
);
4440 if ((error
= vnode_get(dvp
))) {
4441 nfs_node_unlock(np
);
4444 name
= nsp
->nsr_name
;
4447 * [sigh] We can't trust VFS to get the parent right for named
4448 * attribute nodes. (It likes to reparent the nodes after we've
4449 * created them.) Luckily we can probably get the right parent
4450 * from the n_parent we have stashed away.
4452 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
4453 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
4456 dvp
= vnode_getparent(vp
);
4457 vname
= vnode_getname(vp
);
4458 if (!dvp
|| !vname
) {
4461 nfs_node_unlock(np
);
4466 filename
= &smallname
[0];
4467 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
4468 if (namelen
>= sizeof(smallname
)) {
4469 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
4474 snprintf(filename
, namelen
+1, "%s", name
);
4476 nfs_node_unlock(np
);
4477 bzero(&cn
, sizeof(cn
));
4478 cn
.cn_nameptr
= filename
;
4479 cn
.cn_namelen
= namelen
;
4483 if ((error
= nfs_mount_state_in_use_start(nmp
, thd
)))
4487 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
);
4488 if (!error
&& nofp
->nof_w
)
4489 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
);
4490 if (!error
&& nofp
->nof_r
)
4491 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
);
4493 if (nfs_mount_state_in_use_end(nmp
, error
)) {
4494 if (error
== NFSERR_GRACE
)
4496 printf("nfs4_reopen: RPC failed, error %d, lost %d, %s\n", error
,
4497 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4503 if (error
&& (error
!= EINTR
) && (error
!= ERESTART
))
4504 nfs_revoke_open_state_for_node(np
);
4505 lck_mtx_lock(&nofp
->nof_lock
);
4506 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPENING
;
4508 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
4510 printf("nfs4_reopen: failed, error %d, lost %d, %s\n", error
,
4511 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4512 lck_mtx_unlock(&nofp
->nof_lock
);
4513 if (filename
&& (filename
!= &smallname
[0]))
4514 FREE(filename
, M_TEMP
);
4516 vnode_putname(vname
);
4523 * Send a normal OPEN RPC to open/create a file.
4527 struct nfs_open_file
*nofp
,
4529 struct componentname
*cnp
,
4530 struct vnode_attr
*vap
,
4537 return (nfs4_open_rpc_internal(nofp
, ctx
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
4538 cnp
, vap
, dvp
, vpp
, create
, share_access
, share_deny
));
4542 * Send an OPEN RPC to reopen a file.
4545 nfs4_open_reopen_rpc(
4546 struct nfs_open_file
*nofp
,
4549 struct componentname
*cnp
,
4555 return (nfs4_open_rpc_internal(nofp
, NULL
, thd
, cred
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, share_access
, share_deny
));
4559 * Send an OPEN_CONFIRM RPC to confirm an OPEN.
4562 nfs4_open_confirm_rpc(
4563 struct nfsmount
*nmp
,
4567 struct nfs_open_owner
*noop
,
4571 struct nfs_vattr
*nvap
,
4574 struct nfsm_chain nmreq
, nmrep
;
4575 int error
= 0, status
, numops
;
4576 struct nfsreq_secinfo_args si
;
4578 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
4579 nfsm_chain_null(&nmreq
);
4580 nfsm_chain_null(&nmrep
);
4582 // PUTFH, OPEN_CONFIRM, GETATTR
4584 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
4585 nfsm_chain_add_compound_header(error
, &nmreq
, "open_confirm", numops
);
4587 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4588 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
4590 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_CONFIRM
);
4591 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
4592 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4594 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4595 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4596 nfsm_chain_build_done(error
, &nmreq
);
4597 nfsm_assert(error
, (numops
== 0), EPROTO
);
4599 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, &nmrep
, xidp
, &status
);
4601 nfsm_chain_skip_tag(error
, &nmrep
);
4602 nfsm_chain_get_32(error
, &nmrep
, numops
);
4603 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4605 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_CONFIRM
);
4606 nfs_owner_seqid_increment(noop
, NULL
, error
);
4607 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4608 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4610 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
4612 nfsm_chain_cleanup(&nmreq
);
4613 nfsm_chain_cleanup(&nmrep
);
4618 * common OPEN RPC code
4620 * If create is set, ctx must be passed in.
4621 * Returns a node on success if no node passed in.
4624 nfs4_open_rpc_internal(
4625 struct nfs_open_file
*nofp
,
4629 struct componentname
*cnp
,
4630 struct vnode_attr
*vap
,
4637 struct nfsmount
*nmp
;
4638 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4639 struct nfs_vattr nvattr
;
4640 int error
= 0, open_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
4641 int nfsvers
, namedattrs
, numops
, exclusive
= 0, gotuid
, gotgid
;
4642 u_int64_t xid
, savedxid
= 0;
4643 nfsnode_t dnp
= VTONFS(dvp
);
4644 nfsnode_t np
, newnp
= NULL
;
4645 vnode_t newvp
= NULL
;
4646 struct nfsm_chain nmreq
, nmrep
;
4647 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
4648 uint32_t rflags
, delegation
, recall
;
4649 struct nfs_stateid stateid
, dstateid
, *sid
;
4651 struct nfsreq rq
, *req
= &rq
;
4652 struct nfs_dulookup dul
;
4654 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
4655 struct kauth_ace ace
;
4656 struct nfsreq_secinfo_args si
;
4662 if (nfs_mount_gone(nmp
))
4664 nfsvers
= nmp
->nm_vers
;
4665 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4666 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
4669 np
= *vpp
? VTONFS(*vpp
) : NULL
;
4670 if (create
&& vap
) {
4671 exclusive
= (vap
->va_vaflags
& VA_EXCLUSIVE
);
4672 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4673 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4674 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4675 if (exclusive
&& (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
)))
4676 vap
->va_vaflags
|= VA_UTIMES_NULL
;
4678 exclusive
= gotuid
= gotgid
= 0;
4681 sid
= &nofp
->nof_stateid
;
4683 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
4687 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
4690 rflags
= delegation
= recall
= 0;
4693 slen
= sizeof(sbuf
);
4694 NVATTR_INIT(&nvattr
);
4695 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, cnp
->cn_nameptr
, cnp
->cn_namelen
);
4697 nfsm_chain_null(&nmreq
);
4698 nfsm_chain_null(&nmrep
);
4700 // PUTFH, SAVEFH, OPEN(CREATE?), GETATTR(FH), RESTOREFH, GETATTR
4702 nfsm_chain_build_alloc_init(error
, &nmreq
, 53 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
4703 nfsm_chain_add_compound_header(error
, &nmreq
, create
? "create" : "open", numops
);
4705 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4706 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4708 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
4710 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
4711 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4712 nfsm_chain_add_32(error
, &nmreq
, share_access
);
4713 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
4714 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
4715 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
4716 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
4717 nfsm_chain_add_32(error
, &nmreq
, create
);
4720 static uint32_t create_verf
; // XXX need a better verifier
4722 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
4723 /* insert 64 bit verifier */
4724 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4725 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4727 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
4728 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
4731 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
4732 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4734 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4735 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
4736 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
4737 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
4739 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
4741 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4742 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4743 nfsm_chain_build_done(error
, &nmreq
);
4744 nfsm_assert(error
, (numops
== 0), EPROTO
);
4746 error
= busyerror
= nfs_node_set_busy(dnp
, thd
);
4749 if (create
&& !namedattrs
)
4750 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4752 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, NULL
, &req
);
4754 if (create
&& !namedattrs
)
4755 nfs_dulookup_start(&dul
, dnp
, ctx
);
4756 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4760 if (create
&& !namedattrs
)
4761 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4763 if ((lockerror
= nfs_node_lock(dnp
)))
4765 nfsm_chain_skip_tag(error
, &nmrep
);
4766 nfsm_chain_get_32(error
, &nmrep
, numops
);
4767 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4768 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
4770 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
4771 nfs_owner_seqid_increment(noop
, NULL
, error
);
4772 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4773 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
4774 nfsm_chain_get_32(error
, &nmrep
, rflags
);
4775 bmlen
= NFS_ATTR_BITMAP_LEN
;
4776 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
4777 nfsm_chain_get_32(error
, &nmrep
, delegation
);
4779 switch (delegation
) {
4780 case NFS_OPEN_DELEGATE_NONE
:
4782 case NFS_OPEN_DELEGATE_READ
:
4783 case NFS_OPEN_DELEGATE_WRITE
:
4784 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
4785 nfsm_chain_get_32(error
, &nmrep
, recall
);
4786 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
4787 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
4788 /* if we have any trouble accepting the ACE, just invalidate it */
4789 ace_type
= ace_flags
= ace_mask
= len
= 0;
4790 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
4791 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
4792 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
4793 nfsm_chain_get_32(error
, &nmrep
, len
);
4794 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
4795 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
4796 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
4797 if (!error
&& (len
>= slen
)) {
4798 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
4805 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
4807 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
4810 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
4815 if (s
&& (s
!= sbuf
))
4822 /* At this point if we have no error, the object was created/opened. */
4825 if (create
&& vap
&& !exclusive
)
4826 nfs_vattr_set_supported(bitmap
, vap
);
4827 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4829 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
4831 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
4832 printf("nfs: open/create didn't return filehandle? %s\n", cnp
->cn_nameptr
);
4836 if (!create
&& np
&& !NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4837 // XXX for the open case, what if fh doesn't match the vnode we think we're opening?
4838 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
4839 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
4840 NP(np
, "nfs4_open_rpc: warning: file handle mismatch");
4842 /* directory attributes: if we don't get them, make sure to invalidate */
4843 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
4844 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4845 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
4847 NATTRINVALIDATE(dnp
);
4850 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
4851 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
4853 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
4854 nfs_node_unlock(dnp
);
4856 NVATTR_CLEANUP(&nvattr
);
4857 error
= nfs4_open_confirm_rpc(nmp
, dnp
, fh
.fh_data
, fh
.fh_len
, noop
, sid
, thd
, cred
, &nvattr
, &xid
);
4860 if ((lockerror
= nfs_node_lock(dnp
)))
4865 nfsm_chain_cleanup(&nmreq
);
4866 nfsm_chain_cleanup(&nmrep
);
4868 if (!lockerror
&& create
) {
4869 if (!open_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
4870 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4871 cache_purge_negatives(dvp
);
4873 dnp
->n_flag
|= NMODIFIED
;
4874 nfs_node_unlock(dnp
);
4876 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4879 nfs_node_unlock(dnp
);
4880 if (!error
&& !np
&& fh
.fh_len
) {
4881 /* create the vnode with the filehandle and attributes */
4883 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &newnp
);
4885 newvp
= NFSTOV(newnp
);
4887 NVATTR_CLEANUP(&nvattr
);
4889 nfs_node_clear_busy(dnp
);
4890 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
4893 if (!error
&& np
&& !recall
) {
4894 /* stuff the delegation state in the node */
4895 lck_mtx_lock(&np
->n_openlock
);
4896 np
->n_openflags
&= ~N_DELEG_MASK
;
4897 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4898 np
->n_dstateid
= dstateid
;
4900 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4901 lck_mtx_lock(&nmp
->nm_lock
);
4902 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4903 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4904 lck_mtx_unlock(&nmp
->nm_lock
);
4906 lck_mtx_unlock(&np
->n_openlock
);
4908 /* give the delegation back */
4910 if (NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4911 /* update delegation state and return it */
4912 lck_mtx_lock(&np
->n_openlock
);
4913 np
->n_openflags
&= ~N_DELEG_MASK
;
4914 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4915 np
->n_dstateid
= dstateid
;
4917 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4918 lck_mtx_lock(&nmp
->nm_lock
);
4919 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4920 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4921 lck_mtx_unlock(&nmp
->nm_lock
);
4923 lck_mtx_unlock(&np
->n_openlock
);
4924 /* don't need to send a separate delegreturn for fh */
4927 /* return np's current delegation */
4928 nfs4_delegation_return(np
, 0, thd
, cred
);
4930 if (fh
.fh_len
) /* return fh's delegation if it wasn't for np */
4931 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
4935 if (exclusive
&& (error
== NFSERR_NOTSUPP
)) {
4940 nfs_node_unlock(newnp
);
4943 } else if (create
) {
4944 nfs_node_unlock(newnp
);
4946 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
4947 if (error
&& (gotuid
|| gotgid
)) {
4948 /* it's possible the server didn't like our attempt to set IDs. */
4949 /* so, let's try it again without those */
4950 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
4951 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
4952 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
4960 nfs_open_owner_clear_busy(noop
);
4966 * Send an OPEN RPC to claim a delegated open for a file
4969 nfs4_claim_delegated_open_rpc(
4970 struct nfs_open_file
*nofp
,
4975 struct nfsmount
*nmp
;
4976 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4977 struct nfs_vattr nvattr
;
4978 int error
= 0, lockerror
= ENOENT
, status
;
4979 int nfsvers
, numops
;
4981 nfsnode_t np
= nofp
->nof_np
;
4982 struct nfsm_chain nmreq
, nmrep
;
4983 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
4984 uint32_t rflags
= 0, delegation
, recall
= 0;
4986 struct nfs_stateid dstateid
;
4987 char sbuf
[64], *s
= sbuf
;
4988 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
4989 struct kauth_ace ace
;
4991 const char *vname
= NULL
;
4992 const char *name
= NULL
;
4994 char smallname
[128];
4995 char *filename
= NULL
;
4996 struct nfsreq_secinfo_args si
;
4999 if (nfs_mount_gone(nmp
))
5001 nfsvers
= nmp
->nm_vers
;
5003 nfs_node_lock_force(np
);
5004 if ((vnode_vtype(NFSTOV(np
)) != VDIR
) && np
->n_sillyrename
) {
5006 * The node's been sillyrenamed, so we need to use
5007 * the sillyrename directory/name to do the open.
5009 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
5010 dvp
= NFSTOV(nsp
->nsr_dnp
);
5011 if ((error
= vnode_get(dvp
))) {
5012 nfs_node_unlock(np
);
5015 name
= nsp
->nsr_name
;
5018 * [sigh] We can't trust VFS to get the parent right for named
5019 * attribute nodes. (It likes to reparent the nodes after we've
5020 * created them.) Luckily we can probably get the right parent
5021 * from the n_parent we have stashed away.
5023 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
5024 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
5027 dvp
= vnode_getparent(NFSTOV(np
));
5028 vname
= vnode_getname(NFSTOV(np
));
5029 if (!dvp
|| !vname
) {
5032 nfs_node_unlock(np
);
5037 filename
= &smallname
[0];
5038 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
5039 if (namelen
>= sizeof(smallname
)) {
5040 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
5045 snprintf(filename
, namelen
+1, "%s", name
);
5047 nfs_node_unlock(np
);
5049 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5052 NVATTR_INIT(&nvattr
);
5053 delegation
= NFS_OPEN_DELEGATE_NONE
;
5054 dstateid
= np
->n_dstateid
;
5055 NFSREQ_SECINFO_SET(&si
, VTONFS(dvp
), NULL
, 0, filename
, namelen
);
5057 nfsm_chain_null(&nmreq
);
5058 nfsm_chain_null(&nmrep
);
5060 // PUTFH, OPEN, GETATTR(FH)
5062 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5063 nfsm_chain_add_compound_header(error
, &nmreq
, "open_claim_d", numops
);
5065 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5066 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, VTONFS(dvp
)->n_fhp
, VTONFS(dvp
)->n_fhsize
);
5068 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5069 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5070 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5071 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5072 // open owner: clientid + uid
5073 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5074 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5075 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5077 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5079 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_DELEGATE_CUR
);
5080 nfsm_chain_add_stateid(error
, &nmreq
, &np
->n_dstateid
);
5081 nfsm_chain_add_name(error
, &nmreq
, filename
, namelen
, nmp
);
5083 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5084 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5085 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5086 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5087 nfsm_chain_build_done(error
, &nmreq
);
5088 nfsm_assert(error
, (numops
== 0), EPROTO
);
5091 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5092 noop
->noo_cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5094 if ((lockerror
= nfs_node_lock(np
)))
5096 nfsm_chain_skip_tag(error
, &nmrep
);
5097 nfsm_chain_get_32(error
, &nmrep
, numops
);
5098 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5100 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5101 nfs_owner_seqid_increment(noop
, NULL
, error
);
5102 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5103 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5104 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5105 bmlen
= NFS_ATTR_BITMAP_LEN
;
5106 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5107 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5109 switch (delegation
) {
5110 case NFS_OPEN_DELEGATE_NONE
:
5111 // if (!(np->n_openflags & N_DELEG_RETURN)) /* don't warn if delegation is being returned */
5112 // printf("nfs: open delegated claim didn't return a delegation %s\n", filename ? filename : "???");
5114 case NFS_OPEN_DELEGATE_READ
:
5115 case NFS_OPEN_DELEGATE_WRITE
:
5116 if ((((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_READ
) &&
5117 (delegation
== NFS_OPEN_DELEGATE_WRITE
)) ||
5118 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) &&
5119 (delegation
== NFS_OPEN_DELEGATE_READ
)))
5120 printf("nfs: open delegated claim returned a different delegation type! have %s got %s %s\n",
5121 ((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ? "W" : "R",
5122 (delegation
== NFS_OPEN_DELEGATE_WRITE
) ? "W" : "R", filename
? filename
: "???");
5123 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5124 nfsm_chain_get_32(error
, &nmrep
, recall
);
5125 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5126 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5127 /* if we have any trouble accepting the ACE, just invalidate it */
5128 ace_type
= ace_flags
= ace_mask
= len
= 0;
5129 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5130 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5131 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5132 nfsm_chain_get_32(error
, &nmrep
, len
);
5133 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5134 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5135 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5136 if (!error
&& (len
>= slen
)) {
5137 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5144 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5146 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5149 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5154 if (s
&& (s
!= sbuf
))
5157 /* stuff the latest delegation state in the node */
5158 lck_mtx_lock(&np
->n_openlock
);
5159 np
->n_openflags
&= ~N_DELEG_MASK
;
5160 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5161 np
->n_dstateid
= dstateid
;
5163 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5164 lck_mtx_lock(&nmp
->nm_lock
);
5165 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5166 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5167 lck_mtx_unlock(&nmp
->nm_lock
);
5169 lck_mtx_unlock(&np
->n_openlock
);
5177 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5178 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5180 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5181 printf("nfs: open reclaim didn't return filehandle? %s\n", filename
? filename
: "???");
5185 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5186 // XXX what if fh doesn't match the vnode we think we're re-opening?
5187 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5188 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5189 printf("nfs4_claim_delegated_open_rpc: warning: file handle mismatch %s\n", filename
? filename
: "???");
5191 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5193 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5194 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5196 NVATTR_CLEANUP(&nvattr
);
5197 nfsm_chain_cleanup(&nmreq
);
5198 nfsm_chain_cleanup(&nmrep
);
5200 nfs_node_unlock(np
);
5201 nfs_open_owner_clear_busy(noop
);
5202 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5205 * We're making a delegated claim.
5206 * Don't return the delegation here in case we have more to claim.
5207 * Just make sure it's queued up to be returned.
5209 nfs4_delegation_return_enqueue(np
);
5214 // printf("nfs: open claim delegated (%d, %d) succeeded for %s\n", share_access, share_deny, filename ? filename : "???");
5215 if (filename
&& (filename
!= &smallname
[0]))
5216 FREE(filename
, M_TEMP
);
5218 vnode_putname(vname
);
5225 * Send an OPEN RPC to reclaim an open file.
5228 nfs4_open_reclaim_rpc(
5229 struct nfs_open_file
*nofp
,
5233 struct nfsmount
*nmp
;
5234 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5235 struct nfs_vattr nvattr
;
5236 int error
= 0, lockerror
= ENOENT
, status
;
5237 int nfsvers
, numops
;
5239 nfsnode_t np
= nofp
->nof_np
;
5240 struct nfsm_chain nmreq
, nmrep
;
5241 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
5242 uint32_t rflags
= 0, delegation
, recall
= 0;
5244 struct nfs_stateid dstateid
;
5245 char sbuf
[64], *s
= sbuf
;
5246 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
5247 struct kauth_ace ace
;
5248 struct nfsreq_secinfo_args si
;
5251 if (nfs_mount_gone(nmp
))
5253 nfsvers
= nmp
->nm_vers
;
5255 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5258 NVATTR_INIT(&nvattr
);
5259 delegation
= NFS_OPEN_DELEGATE_NONE
;
5260 dstateid
= np
->n_dstateid
;
5261 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5263 nfsm_chain_null(&nmreq
);
5264 nfsm_chain_null(&nmrep
);
5266 // PUTFH, OPEN, GETATTR(FH)
5268 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5269 nfsm_chain_add_compound_header(error
, &nmreq
, "open_reclaim", numops
);
5271 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5272 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5274 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5275 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5276 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5277 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5278 // open owner: clientid + uid
5279 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5280 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5281 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5283 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5285 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_PREVIOUS
);
5286 delegation
= (np
->n_openflags
& N_DELEG_READ
) ? NFS_OPEN_DELEGATE_READ
:
5287 (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
:
5288 NFS_OPEN_DELEGATE_NONE
;
5289 nfsm_chain_add_32(error
, &nmreq
, delegation
);
5290 delegation
= NFS_OPEN_DELEGATE_NONE
;
5292 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5293 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5294 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5295 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5296 nfsm_chain_build_done(error
, &nmreq
);
5297 nfsm_assert(error
, (numops
== 0), EPROTO
);
5300 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5301 noop
->noo_cred
, &si
, R_RECOVER
|R_NOINTR
, &nmrep
, &xid
, &status
);
5303 if ((lockerror
= nfs_node_lock(np
)))
5305 nfsm_chain_skip_tag(error
, &nmrep
);
5306 nfsm_chain_get_32(error
, &nmrep
, numops
);
5307 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5309 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5310 nfs_owner_seqid_increment(noop
, NULL
, error
);
5311 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5312 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5313 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5314 bmlen
= NFS_ATTR_BITMAP_LEN
;
5315 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5316 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5318 switch (delegation
) {
5319 case NFS_OPEN_DELEGATE_NONE
:
5320 if (np
->n_openflags
& N_DELEG_MASK
) {
5322 * Hey! We were supposed to get our delegation back even
5323 * if it was getting immediately recalled. Bad server!
5325 * Just try to return the existing delegation.
5327 // NP(np, "nfs: open reclaim didn't return delegation?");
5328 delegation
= (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
: NFS_OPEN_DELEGATE_READ
;
5332 case NFS_OPEN_DELEGATE_READ
:
5333 case NFS_OPEN_DELEGATE_WRITE
:
5334 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5335 nfsm_chain_get_32(error
, &nmrep
, recall
);
5336 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5337 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5338 /* if we have any trouble accepting the ACE, just invalidate it */
5339 ace_type
= ace_flags
= ace_mask
= len
= 0;
5340 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5341 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5342 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5343 nfsm_chain_get_32(error
, &nmrep
, len
);
5344 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5345 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5346 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5347 if (!error
&& (len
>= slen
)) {
5348 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5355 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5357 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5360 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5365 if (s
&& (s
!= sbuf
))
5368 /* stuff the delegation state in the node */
5369 lck_mtx_lock(&np
->n_openlock
);
5370 np
->n_openflags
&= ~N_DELEG_MASK
;
5371 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5372 np
->n_dstateid
= dstateid
;
5374 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5375 lck_mtx_lock(&nmp
->nm_lock
);
5376 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5377 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5378 lck_mtx_unlock(&nmp
->nm_lock
);
5380 lck_mtx_unlock(&np
->n_openlock
);
5388 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5389 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5391 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5392 NP(np
, "nfs: open reclaim didn't return filehandle?");
5396 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5397 // XXX what if fh doesn't match the vnode we think we're re-opening?
5398 // That should be pretty hard in this case, given that we are doing
5399 // the open reclaim using the file handle (and not a dir/name pair).
5400 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5401 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5402 NP(np
, "nfs4_open_reclaim_rpc: warning: file handle mismatch");
5404 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5406 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5407 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5410 // NP(np, "nfs: open reclaim (%d, %d) succeeded", share_access, share_deny);
5411 NVATTR_CLEANUP(&nvattr
);
5412 nfsm_chain_cleanup(&nmreq
);
5413 nfsm_chain_cleanup(&nmrep
);
5415 nfs_node_unlock(np
);
5416 nfs_open_owner_clear_busy(noop
);
5417 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5419 nfs4_delegation_return_enqueue(np
);
5425 nfs4_open_downgrade_rpc(
5427 struct nfs_open_file
*nofp
,
5430 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5431 struct nfsmount
*nmp
;
5432 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5433 struct nfsm_chain nmreq
, nmrep
;
5435 struct nfsreq_secinfo_args si
;
5438 if (nfs_mount_gone(nmp
))
5440 nfsvers
= nmp
->nm_vers
;
5442 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5445 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5446 nfsm_chain_null(&nmreq
);
5447 nfsm_chain_null(&nmrep
);
5449 // PUTFH, OPEN_DOWNGRADE, GETATTR
5451 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5452 nfsm_chain_add_compound_header(error
, &nmreq
, "open_downgrd", numops
);
5454 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5455 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5457 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_DOWNGRADE
);
5458 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5459 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5460 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_access
);
5461 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_deny
);
5463 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5464 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5465 nfsm_chain_build_done(error
, &nmreq
);
5466 nfsm_assert(error
, (numops
== 0), EPROTO
);
5468 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
5469 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
5470 &si
, R_NOINTR
, &nmrep
, &xid
, &status
);
5472 if ((lockerror
= nfs_node_lock(np
)))
5474 nfsm_chain_skip_tag(error
, &nmrep
);
5475 nfsm_chain_get_32(error
, &nmrep
, numops
);
5476 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5478 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_DOWNGRADE
);
5479 nfs_owner_seqid_increment(noop
, NULL
, error
);
5480 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5481 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5482 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5485 nfs_node_unlock(np
);
5486 nfs_open_owner_clear_busy(noop
);
5487 nfsm_chain_cleanup(&nmreq
);
5488 nfsm_chain_cleanup(&nmrep
);
5495 struct nfs_open_file
*nofp
,
5500 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5501 struct nfsmount
*nmp
;
5502 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5503 struct nfsm_chain nmreq
, nmrep
;
5505 struct nfsreq_secinfo_args si
;
5508 if (nfs_mount_gone(nmp
))
5510 nfsvers
= nmp
->nm_vers
;
5512 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5515 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5516 nfsm_chain_null(&nmreq
);
5517 nfsm_chain_null(&nmrep
);
5519 // PUTFH, CLOSE, GETATTR
5521 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5522 nfsm_chain_add_compound_header(error
, &nmreq
, "close", numops
);
5524 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5525 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5527 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CLOSE
);
5528 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5529 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5531 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5532 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5533 nfsm_chain_build_done(error
, &nmreq
);
5534 nfsm_assert(error
, (numops
== 0), EPROTO
);
5536 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5538 if ((lockerror
= nfs_node_lock(np
)))
5540 nfsm_chain_skip_tag(error
, &nmrep
);
5541 nfsm_chain_get_32(error
, &nmrep
, numops
);
5542 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5544 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CLOSE
);
5545 nfs_owner_seqid_increment(noop
, NULL
, error
);
5546 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5547 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5548 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5551 nfs_node_unlock(np
);
5552 nfs_open_owner_clear_busy(noop
);
5553 nfsm_chain_cleanup(&nmreq
);
5554 nfsm_chain_cleanup(&nmrep
);
5560 * Claim the delegated open combinations this open file holds.
5563 nfs4_claim_delegated_state_for_open_file(struct nfs_open_file
*nofp
, int flags
)
5565 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5566 struct nfs_lock_owner
*nlop
;
5567 struct nfs_file_lock
*nflp
, *nextnflp
;
5568 struct nfsmount
*nmp
;
5569 int error
= 0, reopen
= 0;
5571 if (nofp
->nof_d_rw_drw
) {
5572 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5574 lck_mtx_lock(&nofp
->nof_lock
);
5575 nofp
->nof_rw_drw
+= nofp
->nof_d_rw_drw
;
5576 nofp
->nof_d_rw_drw
= 0;
5577 lck_mtx_unlock(&nofp
->nof_lock
);
5580 if (!error
&& nofp
->nof_d_w_drw
) {
5581 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5583 lck_mtx_lock(&nofp
->nof_lock
);
5584 nofp
->nof_w_drw
+= nofp
->nof_d_w_drw
;
5585 nofp
->nof_d_w_drw
= 0;
5586 lck_mtx_unlock(&nofp
->nof_lock
);
5589 if (!error
&& nofp
->nof_d_r_drw
) {
5590 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5592 lck_mtx_lock(&nofp
->nof_lock
);
5593 nofp
->nof_r_drw
+= nofp
->nof_d_r_drw
;
5594 nofp
->nof_d_r_drw
= 0;
5595 lck_mtx_unlock(&nofp
->nof_lock
);
5598 if (!error
&& nofp
->nof_d_rw_dw
) {
5599 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5601 lck_mtx_lock(&nofp
->nof_lock
);
5602 nofp
->nof_rw_dw
+= nofp
->nof_d_rw_dw
;
5603 nofp
->nof_d_rw_dw
= 0;
5604 lck_mtx_unlock(&nofp
->nof_lock
);
5607 if (!error
&& nofp
->nof_d_w_dw
) {
5608 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5610 lck_mtx_lock(&nofp
->nof_lock
);
5611 nofp
->nof_w_dw
+= nofp
->nof_d_w_dw
;
5612 nofp
->nof_d_w_dw
= 0;
5613 lck_mtx_unlock(&nofp
->nof_lock
);
5616 if (!error
&& nofp
->nof_d_r_dw
) {
5617 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5619 lck_mtx_lock(&nofp
->nof_lock
);
5620 nofp
->nof_r_dw
+= nofp
->nof_d_r_dw
;
5621 nofp
->nof_d_r_dw
= 0;
5622 lck_mtx_unlock(&nofp
->nof_lock
);
5625 /* non-deny-mode opens may be reopened if no locks are held */
5626 if (!error
&& nofp
->nof_d_rw
) {
5627 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5628 /* for some errors, we should just try reopening the file */
5629 if (nfs_mount_state_error_delegation_lost(error
))
5631 if (!error
|| reopen
) {
5632 lck_mtx_lock(&nofp
->nof_lock
);
5633 nofp
->nof_rw
+= nofp
->nof_d_rw
;
5635 lck_mtx_unlock(&nofp
->nof_lock
);
5638 /* if we've already set reopen, we should move these other two opens from delegated to not delegated */
5639 if ((!error
|| reopen
) && nofp
->nof_d_w
) {
5641 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5642 /* for some errors, we should just try reopening the file */
5643 if (nfs_mount_state_error_delegation_lost(error
))
5646 if (!error
|| reopen
) {
5647 lck_mtx_lock(&nofp
->nof_lock
);
5648 nofp
->nof_w
+= nofp
->nof_d_w
;
5650 lck_mtx_unlock(&nofp
->nof_lock
);
5653 if ((!error
|| reopen
) && nofp
->nof_d_r
) {
5655 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5656 /* for some errors, we should just try reopening the file */
5657 if (nfs_mount_state_error_delegation_lost(error
))
5660 if (!error
|| reopen
) {
5661 lck_mtx_lock(&nofp
->nof_lock
);
5662 nofp
->nof_r
+= nofp
->nof_d_r
;
5664 lck_mtx_unlock(&nofp
->nof_lock
);
5670 * Any problems with the delegation probably indicates that we
5671 * should review/return all of our current delegation state.
5673 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5674 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5675 lck_mtx_lock(&nmp
->nm_lock
);
5676 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5677 lck_mtx_unlock(&nmp
->nm_lock
);
5679 if (reopen
&& (nfs_check_for_locks(noop
, nofp
) == 0)) {
5680 /* just reopen the file on next access */
5681 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, need reopen, %d",
5682 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5683 lck_mtx_lock(&nofp
->nof_lock
);
5684 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPEN
;
5685 lck_mtx_unlock(&nofp
->nof_lock
);
5689 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, locks prevent reopen, %d",
5690 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5693 if (!error
&& ((nmp
= NFSTONMP(nofp
->nof_np
)))) {
5694 /* claim delegated locks */
5695 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
5696 if (nlop
->nlo_open_owner
!= noop
)
5698 TAILQ_FOREACH_SAFE(nflp
, &nlop
->nlo_locks
, nfl_lolink
, nextnflp
) {
5699 /* skip dead & blocked lock requests (shouldn't be any in the held lock list) */
5700 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5702 /* skip non-delegated locks */
5703 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5705 error
= nmp
->nm_funcs
->nf_setlock_rpc(nofp
->nof_np
, nofp
, nflp
, 0, flags
, current_thread(), noop
->noo_cred
);
5707 NP(nofp
->nof_np
, "nfs: delegated lock claim (0x%llx, 0x%llx) failed %d, %d",
5708 nflp
->nfl_start
, nflp
->nfl_end
, error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5712 // NP(nofp->nof_np, "nfs: delegated lock claim (0x%llx, 0x%llx) succeeded, %d",
5713 // nflp->nfl_start, nflp->nfl_end, kauth_cred_getuid(nofp->nof_owner->noo_cred));
5721 if (!error
) /* all state claimed successfully! */
5724 /* restart if it looks like a problem more than just losing the delegation */
5725 if (!nfs_mount_state_error_delegation_lost(error
) &&
5726 ((error
== ETIMEDOUT
) || nfs_mount_state_error_should_restart(error
))) {
5727 NP(nofp
->nof_np
, "nfs delegated lock claim error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5728 if ((error
== ETIMEDOUT
) && ((nmp
= NFSTONMP(nofp
->nof_np
))))
5729 nfs_need_reconnect(nmp
);
5733 /* delegated state lost (once held but now not claimable) */
5734 NP(nofp
->nof_np
, "nfs delegated state claim error %d, state lost, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5737 * Any problems with the delegation probably indicates that we
5738 * should review/return all of our current delegation state.
5740 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5741 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5742 lck_mtx_lock(&nmp
->nm_lock
);
5743 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5744 lck_mtx_unlock(&nmp
->nm_lock
);
5747 /* revoke all open file state */
5748 nfs_revoke_open_state_for_node(nofp
->nof_np
);
5754 * Release all open state for the given node.
5757 nfs_release_open_state_for_node(nfsnode_t np
, int force
)
5759 struct nfsmount
*nmp
= NFSTONMP(np
);
5760 struct nfs_open_file
*nofp
;
5761 struct nfs_file_lock
*nflp
, *nextnflp
;
5763 /* drop held locks */
5764 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
5765 /* skip dead & blocked lock requests */
5766 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5768 /* send an unlock if not a delegated lock */
5769 if (!force
&& nmp
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5770 nmp
->nm_funcs
->nf_unlock_rpc(np
, nflp
->nfl_owner
, F_WRLCK
, nflp
->nfl_start
, nflp
->nfl_end
, R_RECOVER
,
5771 NULL
, nflp
->nfl_owner
->nlo_open_owner
->noo_cred
);
5772 /* kill/remove the lock */
5773 lck_mtx_lock(&np
->n_openlock
);
5774 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
5775 lck_mtx_lock(&nflp
->nfl_owner
->nlo_lock
);
5776 TAILQ_REMOVE(&nflp
->nfl_owner
->nlo_locks
, nflp
, nfl_lolink
);
5777 lck_mtx_unlock(&nflp
->nfl_owner
->nlo_lock
);
5778 if (nflp
->nfl_blockcnt
) {
5779 /* wake up anyone blocked on this lock */
5782 /* remove nflp from lock list and destroy */
5783 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
5784 nfs_file_lock_destroy(nflp
);
5786 lck_mtx_unlock(&np
->n_openlock
);
5789 lck_mtx_lock(&np
->n_openlock
);
5791 /* drop all opens */
5792 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5793 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)
5795 /* mark open state as lost */
5796 lck_mtx_lock(&nofp
->nof_lock
);
5797 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
5798 nofp
->nof_flags
|= NFS_OPEN_FILE_LOST
;
5800 lck_mtx_unlock(&nofp
->nof_lock
);
5801 if (!force
&& nmp
&& (nmp
->nm_vers
>= NFS_VER4
))
5802 nfs4_close_rpc(np
, nofp
, NULL
, nofp
->nof_owner
->noo_cred
, R_RECOVER
);
5805 lck_mtx_unlock(&np
->n_openlock
);
5809 * State for a node has been lost, drop it, and revoke the node.
5810 * Attempt to return any state if possible in case the server
5811 * might somehow think we hold it.
5814 nfs_revoke_open_state_for_node(nfsnode_t np
)
5816 struct nfsmount
*nmp
;
5818 /* mark node as needing to be revoked */
5819 nfs_node_lock_force(np
);
5820 if (np
->n_flag
& NREVOKE
) /* already revoked? */
5822 NP(np
, "nfs_revoke_open_state_for_node(): already revoked");
5823 nfs_node_unlock(np
);
5826 np
->n_flag
|= NREVOKE
;
5827 nfs_node_unlock(np
);
5829 nfs_release_open_state_for_node(np
, 0);
5830 NP(np
, "nfs: state lost for %p 0x%x", np
, np
->n_flag
);
5832 /* mark mount as needing a revoke scan and have the socket thread do it. */
5833 if ((nmp
= NFSTONMP(np
))) {
5834 lck_mtx_lock(&nmp
->nm_lock
);
5835 nmp
->nm_state
|= NFSSTA_REVOKE
;
5836 nfs_mount_sock_thread_wake(nmp
);
5837 lck_mtx_unlock(&nmp
->nm_lock
);
5842 * Claim the delegated open combinations that each of this node's open files hold.
5845 nfs4_claim_delegated_state_for_node(nfsnode_t np
, int flags
)
5847 struct nfs_open_file
*nofp
;
5850 lck_mtx_lock(&np
->n_openlock
);
5852 /* walk the open file list looking for opens with delegated state to claim */
5854 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5855 if (!nofp
->nof_d_rw_drw
&& !nofp
->nof_d_w_drw
&& !nofp
->nof_d_r_drw
&&
5856 !nofp
->nof_d_rw_dw
&& !nofp
->nof_d_w_dw
&& !nofp
->nof_d_r_dw
&&
5857 !nofp
->nof_d_rw
&& !nofp
->nof_d_w
&& !nofp
->nof_d_r
)
5859 lck_mtx_unlock(&np
->n_openlock
);
5860 error
= nfs4_claim_delegated_state_for_open_file(nofp
, flags
);
5861 lck_mtx_lock(&np
->n_openlock
);
5867 lck_mtx_unlock(&np
->n_openlock
);
5873 * Mark a node as needed to have its delegation returned.
5874 * Queue it up on the delegation return queue.
5875 * Make sure the thread is running.
5878 nfs4_delegation_return_enqueue(nfsnode_t np
)
5880 struct nfsmount
*nmp
;
5883 if (nfs_mount_gone(nmp
))
5886 lck_mtx_lock(&np
->n_openlock
);
5887 np
->n_openflags
|= N_DELEG_RETURN
;
5888 lck_mtx_unlock(&np
->n_openlock
);
5890 lck_mtx_lock(&nmp
->nm_lock
);
5891 if (np
->n_dreturn
.tqe_next
== NFSNOLIST
)
5892 TAILQ_INSERT_TAIL(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5893 nfs_mount_sock_thread_wake(nmp
);
5894 lck_mtx_unlock(&nmp
->nm_lock
);
5898 * return any delegation we may have for the given node
5901 nfs4_delegation_return(nfsnode_t np
, int flags
, thread_t thd
, kauth_cred_t cred
)
5903 struct nfsmount
*nmp
;
5905 nfs_stateid dstateid
;
5909 if (nfs_mount_gone(nmp
))
5912 /* first, make sure the node's marked for delegation return */
5913 lck_mtx_lock(&np
->n_openlock
);
5914 np
->n_openflags
|= (N_DELEG_RETURN
|N_DELEG_RETURNING
);
5915 lck_mtx_unlock(&np
->n_openlock
);
5917 /* make sure nobody else is using the delegation state */
5918 if ((error
= nfs_open_state_set_busy(np
, NULL
)))
5921 /* claim any delegated state */
5922 if ((error
= nfs4_claim_delegated_state_for_node(np
, flags
)))
5925 /* return the delegation */
5926 lck_mtx_lock(&np
->n_openlock
);
5927 dstateid
= np
->n_dstateid
;
5928 fh
.fh_len
= np
->n_fhsize
;
5929 bcopy(np
->n_fhp
, &fh
.fh_data
, fh
.fh_len
);
5930 lck_mtx_unlock(&np
->n_openlock
);
5931 error
= nfs4_delegreturn_rpc(NFSTONMP(np
), fh
.fh_data
, fh
.fh_len
, &dstateid
, flags
, thd
, cred
);
5932 /* assume delegation is gone for all errors except ETIMEDOUT, NFSERR_*MOVED */
5933 if ((error
!= ETIMEDOUT
) && (error
!= NFSERR_MOVED
) && (error
!= NFSERR_LEASE_MOVED
)) {
5934 lck_mtx_lock(&np
->n_openlock
);
5935 np
->n_openflags
&= ~N_DELEG_MASK
;
5936 lck_mtx_lock(&nmp
->nm_lock
);
5937 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
5938 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
5939 np
->n_dlink
.tqe_next
= NFSNOLIST
;
5941 lck_mtx_unlock(&nmp
->nm_lock
);
5942 lck_mtx_unlock(&np
->n_openlock
);
5946 /* make sure it's no longer on the return queue and clear the return flags */
5947 lck_mtx_lock(&nmp
->nm_lock
);
5948 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
5949 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5950 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
5952 lck_mtx_unlock(&nmp
->nm_lock
);
5953 lck_mtx_lock(&np
->n_openlock
);
5954 np
->n_openflags
&= ~(N_DELEG_RETURN
|N_DELEG_RETURNING
);
5955 lck_mtx_unlock(&np
->n_openlock
);
5958 NP(np
, "nfs4_delegation_return, error %d", error
);
5959 if (error
== ETIMEDOUT
)
5960 nfs_need_reconnect(nmp
);
5961 if (nfs_mount_state_error_should_restart(error
)) {
5962 /* make sure recovery happens */
5963 lck_mtx_lock(&nmp
->nm_lock
);
5964 nfs_need_recover(nmp
, nfs_mount_state_error_delegation_lost(error
) ? NFSERR_EXPIRED
: 0);
5965 lck_mtx_unlock(&nmp
->nm_lock
);
5969 nfs_open_state_clear_busy(np
);
5975 * RPC to return a delegation for a file handle
5978 nfs4_delegreturn_rpc(struct nfsmount
*nmp
, u_char
*fhp
, int fhlen
, struct nfs_stateid
*sid
, int flags
, thread_t thd
, kauth_cred_t cred
)
5980 int error
= 0, status
, numops
;
5982 struct nfsm_chain nmreq
, nmrep
;
5983 struct nfsreq_secinfo_args si
;
5985 NFSREQ_SECINFO_SET(&si
, NULL
, fhp
, fhlen
, NULL
, 0);
5986 nfsm_chain_null(&nmreq
);
5987 nfsm_chain_null(&nmrep
);
5989 // PUTFH, DELEGRETURN
5991 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
5992 nfsm_chain_add_compound_header(error
, &nmreq
, "delegreturn", numops
);
5994 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5995 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
5997 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_DELEGRETURN
);
5998 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
5999 nfsm_chain_build_done(error
, &nmreq
);
6000 nfsm_assert(error
, (numops
== 0), EPROTO
);
6002 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
, &nmrep
, &xid
, &status
);
6003 nfsm_chain_skip_tag(error
, &nmrep
);
6004 nfsm_chain_get_32(error
, &nmrep
, numops
);
6005 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6006 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_DELEGRETURN
);
6008 nfsm_chain_cleanup(&nmreq
);
6009 nfsm_chain_cleanup(&nmrep
);
6016 * Just call nfs_bioread() to do the work.
6018 * Note: the exec code paths have a tendency to call VNOP_READ (and VNOP_MMAP)
6019 * without first calling VNOP_OPEN, so we make sure the file is open here.
6023 struct vnop_read_args
/* {
6024 struct vnodeop_desc *a_desc;
6028 vfs_context_t a_context;
6031 vnode_t vp
= ap
->a_vp
;
6032 vfs_context_t ctx
= ap
->a_context
;
6034 struct nfsmount
*nmp
;
6035 struct nfs_open_owner
*noop
;
6036 struct nfs_open_file
*nofp
;
6039 if (vnode_vtype(ap
->a_vp
) != VREG
)
6040 return (vnode_vtype(vp
) == VDIR
) ? EISDIR
: EPERM
;
6044 if (nfs_mount_gone(nmp
))
6046 if (np
->n_flag
& NREVOKE
)
6049 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6053 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
6054 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6055 NP(np
, "nfs_vnop_read: LOST %d", kauth_cred_getuid(noop
->noo_cred
));
6058 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6059 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
6065 nfs_open_owner_rele(noop
);
6068 if (!nofp
->nof_access
) {
6069 /* we don't have the file open, so open it for read access */
6070 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6072 nfs_open_owner_rele(noop
);
6075 if (np
->n_flag
& NREVOKE
) {
6077 nfs_mount_state_in_use_end(nmp
, 0);
6078 nfs_open_owner_rele(noop
);
6081 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
6085 if (nmp
->nm_vers
< NFS_VER4
) {
6086 /* NFS v2/v3 opens are always allowed - so just add it. */
6087 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
6089 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
6093 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
6095 nfs_open_file_clear_busy(nofp
);
6096 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6101 nfs_open_owner_rele(noop
);
6104 return (nfs_bioread(VTONFS(ap
->a_vp
), ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
));
6108 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6109 * Files are created using the NFSv4 OPEN RPC. So we must open the
6110 * file to create it and then close it.
6114 struct vnop_create_args
/* {
6115 struct vnodeop_desc *a_desc;
6118 struct componentname *a_cnp;
6119 struct vnode_attr *a_vap;
6120 vfs_context_t a_context;
6123 vfs_context_t ctx
= ap
->a_context
;
6124 struct componentname
*cnp
= ap
->a_cnp
;
6125 struct vnode_attr
*vap
= ap
->a_vap
;
6126 vnode_t dvp
= ap
->a_dvp
;
6127 vnode_t
*vpp
= ap
->a_vpp
;
6128 struct nfsmount
*nmp
;
6130 int error
= 0, busyerror
= 0, accessMode
, denyMode
;
6131 struct nfs_open_owner
*noop
= NULL
;
6132 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
6135 if (nfs_mount_gone(nmp
))
6139 nfs_avoid_needless_id_setting_on_create(VTONFS(dvp
), vap
, ctx
);
6141 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6146 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6148 nfs_open_owner_rele(noop
);
6152 /* grab a provisional, nodeless open file */
6153 error
= nfs_open_file_find(NULL
, noop
, &newnofp
, 0, 0, 1);
6154 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6155 printf("nfs_vnop_create: LOST\n");
6158 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6159 /* This shouldn't happen given that this is a new, nodeless nofp */
6160 nfs_mount_state_in_use_end(nmp
, 0);
6161 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
6162 nfs_open_file_destroy(newnofp
);
6168 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
6171 nfs_open_file_destroy(newnofp
);
6177 * We're just trying to create the file.
6178 * We'll create/open it RW, and set NFS_OPEN_FILE_CREATE.
6180 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
6181 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
6183 /* Do the open/create */
6184 error
= nfs4_open_rpc(newnofp
, ctx
, cnp
, vap
, dvp
, vpp
, NFS_OPEN_CREATE
, accessMode
, denyMode
);
6185 if ((error
== EACCES
) && vap
&& !(vap
->va_vaflags
& VA_EXCLUSIVE
) &&
6186 VATTR_IS_ACTIVE(vap
, va_mode
) && !(vap
->va_mode
& S_IWUSR
)) {
6188 * Hmm... it looks like we may have a situation where the request was
6189 * retransmitted because we didn't get the first response which successfully
6190 * created/opened the file and then the second time we were denied the open
6191 * because the mode the file was created with doesn't allow write access.
6193 * We'll try to work around this by temporarily updating the mode and
6194 * retrying the open.
6196 struct vnode_attr vattr
;
6198 /* first make sure it's there */
6199 int error2
= nfs_lookitup(VTONFS(dvp
), cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6200 if (!error2
&& np
) {
6201 nfs_node_unlock(np
);
6203 if (vnode_vtype(NFSTOV(np
)) == VREG
) {
6205 VATTR_SET(&vattr
, va_mode
, (vap
->va_mode
| S_IWUSR
));
6206 if (!nfs4_setattr_rpc(np
, &vattr
, ctx
)) {
6207 error2
= nfs4_open_rpc(newnofp
, ctx
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
6209 VATTR_SET(&vattr
, va_mode
, vap
->va_mode
);
6210 nfs4_setattr_rpc(np
, &vattr
, ctx
);
6221 if (!error
&& !*vpp
) {
6222 printf("nfs4_open_rpc returned without a node?\n");
6223 /* Hmmm... with no node, we have no filehandle and can't close it */
6227 /* need to cleanup our temporary nofp */
6228 nfs_open_file_clear_busy(newnofp
);
6229 nfs_open_file_destroy(newnofp
);
6233 /* After we have a node, add our open file struct to the node */
6235 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
6237 error
= nfs_open_file_find_internal(np
, noop
, &nofp
, 0, 0, 0);
6239 /* This shouldn't happen, because we passed in a new nofp to use. */
6240 printf("nfs_open_file_find_internal failed! %d\n", error
);
6242 } else if (nofp
!= newnofp
) {
6244 * Hmm... an open file struct already exists.
6245 * Mark the existing one busy and merge our open into it.
6246 * Then destroy the one we created.
6247 * Note: there's no chance of an open confict because the
6248 * open has already been granted.
6250 busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
6251 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
6252 nofp
->nof_stateid
= newnofp
->nof_stateid
;
6253 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
6254 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
6255 nfs_open_file_clear_busy(newnofp
);
6256 nfs_open_file_destroy(newnofp
);
6259 /* mark the node as holding a create-initiated open */
6260 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
6261 nofp
->nof_creator
= current_thread();
6263 if (nofp
&& !busyerror
)
6264 nfs_open_file_clear_busy(nofp
);
6265 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6266 nofp
= newnofp
= NULL
;
6271 nfs_open_owner_rele(noop
);
6276 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6282 struct componentname
*cnp
,
6283 struct vnode_attr
*vap
,
6288 struct nfsmount
*nmp
;
6289 struct nfs_vattr nvattr
;
6290 int error
= 0, create_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
6291 int nfsvers
, namedattrs
, numops
;
6292 u_int64_t xid
, savedxid
= 0;
6293 nfsnode_t np
= NULL
;
6294 vnode_t newvp
= NULL
;
6295 struct nfsm_chain nmreq
, nmrep
;
6296 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
6300 struct nfsreq rq
, *req
= &rq
;
6301 struct nfs_dulookup dul
;
6302 struct nfsreq_secinfo_args si
;
6304 nmp
= NFSTONMP(dnp
);
6305 if (nfs_mount_gone(nmp
))
6307 nfsvers
= nmp
->nm_vers
;
6308 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6309 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6312 sd
.specdata1
= sd
.specdata2
= 0;
6321 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
6323 sd
.specdata1
= major(vap
->va_rdev
);
6324 sd
.specdata2
= minor(vap
->va_rdev
);
6337 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
6339 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
6341 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6343 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
6344 NVATTR_INIT(&nvattr
);
6345 nfsm_chain_null(&nmreq
);
6346 nfsm_chain_null(&nmrep
);
6348 // PUTFH, SAVEFH, CREATE, GETATTR(FH), RESTOREFH, GETATTR
6350 nfsm_chain_build_alloc_init(error
, &nmreq
, 66 * NFSX_UNSIGNED
);
6351 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, numops
);
6353 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6354 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6356 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6358 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CREATE
);
6359 nfsm_chain_add_32(error
, &nmreq
, type
);
6360 if (type
== NFLNK
) {
6361 nfsm_chain_add_name(error
, &nmreq
, link
, strlen(link
), nmp
);
6362 } else if ((type
== NFBLK
) || (type
== NFCHR
)) {
6363 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata1
);
6364 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata2
);
6366 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6367 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
6369 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6370 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6371 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6372 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
6374 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6376 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6377 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
6378 nfsm_chain_build_done(error
, &nmreq
);
6379 nfsm_assert(error
, (numops
== 0), EPROTO
);
6382 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6383 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6386 nfs_dulookup_start(&dul
, dnp
, ctx
);
6387 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6390 if ((lockerror
= nfs_node_lock(dnp
)))
6392 nfsm_chain_skip_tag(error
, &nmrep
);
6393 nfsm_chain_get_32(error
, &nmrep
, numops
);
6394 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6395 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6397 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CREATE
);
6398 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
6399 bmlen
= NFS_ATTR_BITMAP_LEN
;
6400 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
6401 /* At this point if we have no error, the object was created. */
6402 /* if we don't get attributes, then we should lookitup. */
6403 create_error
= error
;
6405 nfs_vattr_set_supported(bitmap
, vap
);
6406 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6408 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6410 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
6411 printf("nfs: create/%s didn't return filehandle? %s\n", tag
, cnp
->cn_nameptr
);
6415 /* directory attributes: if we don't get them, make sure to invalidate */
6416 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6417 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6419 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
6421 NATTRINVALIDATE(dnp
);
6424 nfsm_chain_cleanup(&nmreq
);
6425 nfsm_chain_cleanup(&nmrep
);
6428 if (!create_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
6429 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6430 cache_purge_negatives(NFSTOV(dnp
));
6432 dnp
->n_flag
|= NMODIFIED
;
6433 nfs_node_unlock(dnp
);
6434 /* nfs_getattr() will check changed and purge caches */
6435 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6438 if (!error
&& fh
.fh_len
) {
6439 /* create the vnode with the filehandle and attributes */
6441 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6445 NVATTR_CLEANUP(&nvattr
);
6448 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6451 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
6452 * if we can succeed in looking up the object.
6454 if ((create_error
== EEXIST
) || (!create_error
&& !newvp
)) {
6455 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6458 if (vnode_vtype(newvp
) != nfstov_type(type
, nfsvers
))
6463 nfs_node_clear_busy(dnp
);
6466 nfs_node_unlock(np
);
6470 nfs_node_unlock(np
);
6478 struct vnop_mknod_args
/* {
6479 struct vnodeop_desc *a_desc;
6482 struct componentname *a_cnp;
6483 struct vnode_attr *a_vap;
6484 vfs_context_t a_context;
6487 nfsnode_t np
= NULL
;
6488 struct nfsmount
*nmp
;
6491 nmp
= VTONMP(ap
->a_dvp
);
6492 if (nfs_mount_gone(nmp
))
6495 if (!VATTR_IS_ACTIVE(ap
->a_vap
, va_type
))
6497 switch (ap
->a_vap
->va_type
) {
6507 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6508 vtonfs_type(ap
->a_vap
->va_type
, nmp
->nm_vers
), NULL
, &np
);
6510 *ap
->a_vpp
= NFSTOV(np
);
6516 struct vnop_mkdir_args
/* {
6517 struct vnodeop_desc *a_desc;
6520 struct componentname *a_cnp;
6521 struct vnode_attr *a_vap;
6522 vfs_context_t a_context;
6525 nfsnode_t np
= NULL
;
6528 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6531 *ap
->a_vpp
= NFSTOV(np
);
6537 struct vnop_symlink_args
/* {
6538 struct vnodeop_desc *a_desc;
6541 struct componentname *a_cnp;
6542 struct vnode_attr *a_vap;
6544 vfs_context_t a_context;
6547 nfsnode_t np
= NULL
;
6550 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6551 NFLNK
, ap
->a_target
, &np
);
6553 *ap
->a_vpp
= NFSTOV(np
);
6559 struct vnop_link_args
/* {
6560 struct vnodeop_desc *a_desc;
6563 struct componentname *a_cnp;
6564 vfs_context_t a_context;
6567 vfs_context_t ctx
= ap
->a_context
;
6568 vnode_t vp
= ap
->a_vp
;
6569 vnode_t tdvp
= ap
->a_tdvp
;
6570 struct componentname
*cnp
= ap
->a_cnp
;
6571 int error
= 0, lockerror
= ENOENT
, status
;
6572 struct nfsmount
*nmp
;
6573 nfsnode_t np
= VTONFS(vp
);
6574 nfsnode_t tdnp
= VTONFS(tdvp
);
6575 int nfsvers
, numops
;
6576 u_int64_t xid
, savedxid
;
6577 struct nfsm_chain nmreq
, nmrep
;
6578 struct nfsreq_secinfo_args si
;
6580 if (vnode_mount(vp
) != vnode_mount(tdvp
))
6584 if (nfs_mount_gone(nmp
))
6586 nfsvers
= nmp
->nm_vers
;
6587 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6589 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6593 * Push all writes to the server, so that the attribute cache
6594 * doesn't get "out of sync" with the server.
6595 * XXX There should be a better way!
6597 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
6599 if ((error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
))))
6602 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6603 nfsm_chain_null(&nmreq
);
6604 nfsm_chain_null(&nmrep
);
6606 // PUTFH(SOURCE), SAVEFH, PUTFH(DIR), LINK, GETATTR(DIR), RESTOREFH, GETATTR
6608 nfsm_chain_build_alloc_init(error
, &nmreq
, 29 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
6609 nfsm_chain_add_compound_header(error
, &nmreq
, "link", numops
);
6611 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6612 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6614 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6616 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6617 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
6619 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LINK
);
6620 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6622 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6623 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
6625 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6627 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6628 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
6629 nfsm_chain_build_done(error
, &nmreq
);
6630 nfsm_assert(error
, (numops
== 0), EPROTO
);
6632 error
= nfs_request(tdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
6634 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
6638 nfsm_chain_skip_tag(error
, &nmrep
);
6639 nfsm_chain_get_32(error
, &nmrep
, numops
);
6640 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6641 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6642 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6643 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LINK
);
6644 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
6645 /* directory attributes: if we don't get them, make sure to invalidate */
6646 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6648 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
6650 NATTRINVALIDATE(tdnp
);
6651 /* link attributes: if we don't get them, make sure to invalidate */
6652 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6653 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6655 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
6657 NATTRINVALIDATE(np
);
6659 nfsm_chain_cleanup(&nmreq
);
6660 nfsm_chain_cleanup(&nmrep
);
6662 tdnp
->n_flag
|= NMODIFIED
;
6663 /* Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. */
6664 if (error
== EEXIST
)
6666 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
6667 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
6668 cache_purge_negatives(tdvp
);
6671 nfs_node_unlock2(tdnp
, np
);
6672 nfs_node_clear_busy2(tdnp
, np
);
6678 struct vnop_rmdir_args
/* {
6679 struct vnodeop_desc *a_desc;
6682 struct componentname *a_cnp;
6683 vfs_context_t a_context;
6686 vfs_context_t ctx
= ap
->a_context
;
6687 vnode_t vp
= ap
->a_vp
;
6688 vnode_t dvp
= ap
->a_dvp
;
6689 struct componentname
*cnp
= ap
->a_cnp
;
6690 struct nfsmount
*nmp
;
6691 int error
= 0, namedattrs
;
6692 nfsnode_t np
= VTONFS(vp
);
6693 nfsnode_t dnp
= VTONFS(dvp
);
6694 struct nfs_dulookup dul
;
6696 if (vnode_vtype(vp
) != VDIR
)
6699 nmp
= NFSTONMP(dnp
);
6700 if (nfs_mount_gone(nmp
))
6702 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6704 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
6708 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6709 nfs_dulookup_start(&dul
, dnp
, ctx
);
6712 error
= nfs4_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6713 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
6715 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
6716 /* nfs_getattr() will check changed and purge caches */
6717 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6719 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6720 nfs_node_clear_busy2(dnp
, np
);
6723 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
6725 if (error
== ENOENT
)
6729 * remove nfsnode from hash now so we can't accidentally find it
6730 * again if another object gets created with the same filehandle
6731 * before this vnode gets reclaimed
6733 lck_mtx_lock(nfs_node_hash_mutex
);
6734 if (np
->n_hflag
& NHHASHED
) {
6735 LIST_REMOVE(np
, n_hash
);
6736 np
->n_hflag
&= ~NHHASHED
;
6737 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
6739 lck_mtx_unlock(nfs_node_hash_mutex
);
6745 * NFSv4 Named Attributes
6747 * Both the extended attributes interface and the named streams interface
6748 * are backed by NFSv4 named attributes. The implementations for both use
6749 * a common set of routines in an attempt to reduce code duplication, to
6750 * increase efficiency, to increase caching of both names and data, and to
6751 * confine the complexity.
6753 * Each NFS node caches its named attribute directory's file handle.
6754 * The directory nodes for the named attribute directories are handled
6755 * exactly like regular directories (with a couple minor exceptions).
6756 * Named attribute nodes are also treated as much like regular files as
6759 * Most of the heavy lifting is done by nfs4_named_attr_get().
6763 * Get the given node's attribute directory node.
6764 * If !fetch, then only return a cached node.
6765 * Otherwise, we will attempt to fetch the node from the server.
6766 * (Note: the node should be marked busy.)
6769 nfs4_named_attr_dir_get(nfsnode_t np
, int fetch
, vfs_context_t ctx
)
6771 nfsnode_t adnp
= NULL
;
6772 struct nfsmount
*nmp
;
6773 int error
= 0, status
, numops
;
6774 struct nfsm_chain nmreq
, nmrep
;
6776 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
6778 struct nfs_vattr nvattr
;
6779 struct componentname cn
;
6780 struct nfsreq rq
, *req
= &rq
;
6781 struct nfsreq_secinfo_args si
;
6784 if (nfs_mount_gone(nmp
))
6786 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6789 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6790 NVATTR_INIT(&nvattr
);
6791 nfsm_chain_null(&nmreq
);
6792 nfsm_chain_null(&nmrep
);
6794 bzero(&cn
, sizeof(cn
));
6795 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
6796 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
6797 cn
.cn_nameiop
= LOOKUP
;
6799 if (np
->n_attrdirfh
) {
6800 // XXX can't set parent correctly (to np) yet
6801 error
= nfs_nget(nmp
->nm_mountp
, NULL
, &cn
, np
->n_attrdirfh
+1, *np
->n_attrdirfh
,
6802 NULL
, NULL
, RPCAUTH_UNKNOWN
, NG_NOCREATE
, &adnp
);
6811 // PUTFH, OPENATTR, GETATTR
6813 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
6814 nfsm_chain_add_compound_header(error
, &nmreq
, "openattr", numops
);
6816 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6817 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
6819 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
6820 nfsm_chain_add_32(error
, &nmreq
, 0);
6822 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6823 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6824 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6825 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
6826 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
6827 nfsm_chain_build_done(error
, &nmreq
);
6828 nfsm_assert(error
, (numops
== 0), EPROTO
);
6830 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6831 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6833 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6835 nfsm_chain_skip_tag(error
, &nmrep
);
6836 nfsm_chain_get_32(error
, &nmrep
, numops
);
6837 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6838 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
6839 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6841 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6843 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
6847 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
6848 /* (re)allocate attrdir fh buffer */
6849 if (np
->n_attrdirfh
)
6850 FREE(np
->n_attrdirfh
, M_TEMP
);
6851 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
6853 if (!np
->n_attrdirfh
) {
6857 /* cache the attrdir fh in the node */
6858 *np
->n_attrdirfh
= fh
.fh_len
;
6859 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
6860 /* create node for attrdir */
6861 // XXX can't set parent correctly (to np) yet
6862 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
6864 NVATTR_CLEANUP(&nvattr
);
6865 nfsm_chain_cleanup(&nmreq
);
6866 nfsm_chain_cleanup(&nmrep
);
6869 /* sanity check that this node is an attribute directory */
6870 if (adnp
->n_vattr
.nva_type
!= VDIR
)
6872 if (!(adnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
6874 nfs_node_unlock(adnp
);
6876 vnode_put(NFSTOV(adnp
));
6878 return (error
? NULL
: adnp
);
6882 * Get the given node's named attribute node for the name given.
6884 * In an effort to increase the performance of named attribute access, we try
6885 * to reduce server requests by doing the following:
6887 * - cache the node's named attribute directory file handle in the node
6888 * - maintain a directory vnode for the attribute directory
6889 * - use name cache entries (positive and negative) to speed up lookups
6890 * - optionally open the named attribute (with the given accessMode) in the same RPC
6891 * - combine attribute directory retrieval with the lookup/open RPC
6892 * - optionally prefetch the named attribute's first block of data in the same RPC
6894 * Also, in an attempt to reduce the number of copies/variations of this code,
6895 * parts of the RPC building/processing code are conditionalized on what is
6896 * needed for any particular request (openattr, lookup vs. open, read).
6898 * Note that because we may not have the attribute directory node when we start
6899 * the lookup/open, we lock both the node and the attribute directory node.
6902 #define NFS_GET_NAMED_ATTR_CREATE 0x1
6903 #define NFS_GET_NAMED_ATTR_CREATE_GUARDED 0x2
6904 #define NFS_GET_NAMED_ATTR_TRUNCATE 0x4
6905 #define NFS_GET_NAMED_ATTR_PREFETCH 0x8
6908 nfs4_named_attr_get(
6910 struct componentname
*cnp
,
6911 uint32_t accessMode
,
6915 struct nfs_open_file
**nofpp
)
6917 struct nfsmount
*nmp
;
6918 int error
= 0, open_error
= EIO
;
6919 int inuse
= 0, adlockerror
= ENOENT
, busyerror
= ENOENT
, adbusyerror
= ENOENT
, nofpbusyerror
= ENOENT
;
6920 int create
, guarded
, prefetch
, truncate
, noopbusy
= 0;
6921 int open
, status
, numops
, hadattrdir
, negnamecache
;
6922 struct nfs_vattr nvattr
;
6923 struct vnode_attr vattr
;
6924 nfsnode_t adnp
= NULL
, anp
= NULL
;
6926 u_int64_t xid
, savedxid
= 0;
6927 struct nfsm_chain nmreq
, nmrep
;
6928 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
6929 uint32_t denyMode
, rflags
, delegation
, recall
, eof
, rlen
, retlen
;
6930 nfs_stateid stateid
, dstateid
;
6932 struct nfs_open_owner
*noop
= NULL
;
6933 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
6934 struct vnop_access_args naa
;
6939 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
6940 struct kauth_ace ace
;
6941 struct nfsreq rq
, *req
= &rq
;
6942 struct nfsreq_secinfo_args si
;
6946 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
6949 slen
= sizeof(sbuf
);
6952 if (nfs_mount_gone(nmp
))
6954 NVATTR_INIT(&nvattr
);
6955 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
6956 thd
= vfs_context_thread(ctx
);
6957 cred
= vfs_context_ucred(ctx
);
6958 create
= (flags
& NFS_GET_NAMED_ATTR_CREATE
) ? NFS_OPEN_CREATE
: NFS_OPEN_NOCREATE
;
6959 guarded
= (flags
& NFS_GET_NAMED_ATTR_CREATE_GUARDED
) ? NFS_CREATE_GUARDED
: NFS_CREATE_UNCHECKED
;
6960 truncate
= (flags
& NFS_GET_NAMED_ATTR_TRUNCATE
);
6961 prefetch
= (flags
& NFS_GET_NAMED_ATTR_PREFETCH
);
6964 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
6967 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
6968 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
6970 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_NONE
) {
6971 /* shouldn't happen... but just be safe */
6972 printf("nfs4_named_attr_get: create with no access %s\n", cnp
->cn_nameptr
);
6973 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
6975 open
= (accessMode
!= NFS_OPEN_SHARE_ACCESS_NONE
);
6978 * We're trying to open the file.
6979 * We'll create/open it with the given access mode,
6980 * and set NFS_OPEN_FILE_CREATE.
6982 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
6983 if (prefetch
&& guarded
)
6984 prefetch
= 0; /* no sense prefetching data that can't be there */
6986 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6991 if ((error
= busyerror
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
6994 adnp
= nfs4_named_attr_dir_get(np
, 0, ctx
);
6995 hadattrdir
= (adnp
!= NULL
);
6998 /* use the special state ID because we don't have a real one to send */
6999 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
7000 rlen
= MIN(nmp
->nm_rsize
, nmp
->nm_biosize
);
7002 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
7003 nfsm_chain_null(&nmreq
);
7004 nfsm_chain_null(&nmrep
);
7007 if ((error
= adbusyerror
= nfs_node_set_busy(adnp
, vfs_context_thread(ctx
))))
7009 /* nfs_getattr() will check changed and purge caches */
7010 error
= nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
7012 error
= cache_lookup(NFSTOV(adnp
), &avp
, cnp
);
7015 /* negative cache entry */
7019 /* try dir buf cache lookup */
7020 error
= nfs_dir_buf_cache_lookup(adnp
, &anp
, cnp
, ctx
, 0);
7021 if (!error
&& anp
) {
7022 /* dir buf cache hit */
7026 if (error
!= -1) /* cache miss */
7030 /* cache hit, not really an error */
7031 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
7033 *anpp
= anp
= VTONFS(avp
);
7035 nfs_node_clear_busy(adnp
);
7036 adbusyerror
= ENOENT
;
7038 /* check for directory access */
7039 naa
.a_desc
= &vnop_access_desc
;
7040 naa
.a_vp
= NFSTOV(adnp
);
7041 naa
.a_action
= KAUTH_VNODE_SEARCH
;
7042 naa
.a_context
= ctx
;
7044 /* compute actual success/failure based on accessibility */
7045 error
= nfs_vnop_access(&naa
);
7048 /* we either found it, or hit an error */
7049 if (!error
&& guarded
) {
7050 /* found cached entry but told not to use it */
7052 vnode_put(NFSTOV(anp
));
7055 /* we're done if error or we don't need to open */
7058 /* no error and we need to open... */
7064 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
7066 nfs_open_owner_rele(noop
);
7072 /* grab an open file - possibly provisional/nodeless if cache_lookup() failed */
7073 error
= nfs_open_file_find(anp
, noop
, &newnofp
, 0, 0, 1);
7074 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
7075 printf("nfs4_named_attr_get: LOST %d %s\n", kauth_cred_getuid(noop
->noo_cred
), cnp
->cn_nameptr
);
7078 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
7079 nfs_mount_state_in_use_end(nmp
, 0);
7080 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
7081 nfs_open_file_destroy(newnofp
);
7087 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
7090 nfs_open_file_destroy(newnofp
);
7096 * We already have the node. So we just need to open
7097 * it - which we may be able to do with a delegation.
7099 open_error
= error
= nfs4_open(anp
, newnofp
, accessMode
, denyMode
, ctx
);
7101 /* open succeeded, so our open file is no longer temporary */
7113 * We either don't have the attrdir or we didn't find the attribute
7114 * in the name cache, so we need to talk to the server.
7116 * If we don't have the attrdir, we'll need to ask the server for that too.
7117 * If the caller is requesting that the attribute be created, we need to
7118 * make sure the attrdir is created.
7119 * The caller may also request that the first block of an existing attribute
7120 * be retrieved at the same time.
7124 /* need to mark the open owner busy during the RPC */
7125 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
7131 * We'd like to get updated post-open/lookup attributes for the
7132 * directory and we may also want to prefetch some data via READ.
7133 * We'd like the READ results to be last so that we can leave the
7134 * data in the mbufs until the end.
7136 * At a minimum we're sending: PUTFH, LOOKUP/OPEN, GETATTR, PUTFH, GETATTR
7140 numops
+= 3; // also sending: OPENATTR, GETATTR, OPENATTR
7142 numops
+= 4; // also sending: SAVEFH, RESTOREFH, NVERIFY, READ
7143 nfsm_chain_build_alloc_init(error
, &nmreq
, 64 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
7144 nfsm_chain_add_compound_header(error
, &nmreq
, "getnamedattr", numops
);
7147 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7148 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7151 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7152 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7154 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7155 nfsm_chain_add_32(error
, &nmreq
, create
? 1 : 0);
7157 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7158 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7159 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7160 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7161 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7165 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
7166 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
7167 nfsm_chain_add_32(error
, &nmreq
, accessMode
);
7168 nfsm_chain_add_32(error
, &nmreq
, denyMode
);
7169 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
7170 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
7171 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
7172 nfsm_chain_add_32(error
, &nmreq
, create
);
7174 nfsm_chain_add_32(error
, &nmreq
, guarded
);
7177 VATTR_SET(&vattr
, va_data_size
, 0);
7178 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7180 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
7181 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7184 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
7185 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7188 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7189 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7190 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7191 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7192 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7195 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
7199 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7200 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7203 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7204 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7206 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7207 nfsm_chain_add_32(error
, &nmreq
, 0);
7210 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7211 nfsm_chain_add_bitmap_masked(error
, &nmreq
, nfs_getattr_bitmap
,
7212 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7215 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
7217 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_NVERIFY
);
7219 VATTR_SET(&vattr
, va_data_size
, 0);
7220 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7222 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
7223 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
7224 nfsm_chain_add_64(error
, &nmreq
, 0);
7225 nfsm_chain_add_32(error
, &nmreq
, rlen
);
7227 nfsm_chain_build_done(error
, &nmreq
);
7228 nfsm_assert(error
, (numops
== 0), EPROTO
);
7230 error
= nfs_request_async(hadattrdir
? adnp
: np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
7231 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, open
? R_NOINTR
: 0, NULL
, &req
);
7233 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
7235 if (hadattrdir
&& ((adlockerror
= nfs_node_lock(adnp
))))
7236 error
= adlockerror
;
7238 nfsm_chain_skip_tag(error
, &nmrep
);
7239 nfsm_chain_get_32(error
, &nmrep
, numops
);
7240 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7242 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7243 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7245 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7247 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) && fh
.fh_len
) {
7248 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
7249 /* (re)allocate attrdir fh buffer */
7250 if (np
->n_attrdirfh
)
7251 FREE(np
->n_attrdirfh
, M_TEMP
);
7252 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
7254 if (np
->n_attrdirfh
) {
7255 /* remember the attrdir fh in the node */
7256 *np
->n_attrdirfh
= fh
.fh_len
;
7257 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
7258 /* create busied node for attrdir */
7259 struct componentname cn
;
7260 bzero(&cn
, sizeof(cn
));
7261 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
7262 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
7263 cn
.cn_nameiop
= LOOKUP
;
7264 // XXX can't set parent correctly (to np) yet
7265 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
7268 /* set the node busy */
7269 SET(adnp
->n_flag
, NBUSY
);
7272 /* if no adnp, oh well... */
7276 NVATTR_CLEANUP(&nvattr
);
7280 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
7281 nfs_owner_seqid_increment(noop
, NULL
, error
);
7282 nfsm_chain_get_stateid(error
, &nmrep
, &newnofp
->nof_stateid
);
7283 nfsm_chain_check_change_info(error
, &nmrep
, adnp
);
7284 nfsm_chain_get_32(error
, &nmrep
, rflags
);
7285 bmlen
= NFS_ATTR_BITMAP_LEN
;
7286 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
7287 nfsm_chain_get_32(error
, &nmrep
, delegation
);
7289 switch (delegation
) {
7290 case NFS_OPEN_DELEGATE_NONE
:
7292 case NFS_OPEN_DELEGATE_READ
:
7293 case NFS_OPEN_DELEGATE_WRITE
:
7294 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
7295 nfsm_chain_get_32(error
, &nmrep
, recall
);
7296 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
7297 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
7298 /* if we have any trouble accepting the ACE, just invalidate it */
7299 ace_type
= ace_flags
= ace_mask
= len
= 0;
7300 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
7301 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
7302 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
7303 nfsm_chain_get_32(error
, &nmrep
, len
);
7304 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
7305 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
7306 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
7307 if (!error
&& (len
>= slen
)) {
7308 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
7315 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
7317 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
7320 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
7325 if (s
&& (s
!= sbuf
))
7332 /* At this point if we have no error, the object was created/opened. */
7335 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOOKUP
);
7337 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7339 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7341 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
7346 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
7347 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7349 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7350 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7353 nfsm_chain_loadattr(error
, &nmrep
, adnp
, nmp
->nm_vers
, &xid
);
7357 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
7358 newnofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7359 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
7361 nfs_node_unlock(adnp
);
7362 adlockerror
= ENOENT
;
7364 NVATTR_CLEANUP(&nvattr
);
7365 error
= nfs4_open_confirm_rpc(nmp
, adnp
? adnp
: np
, fh
.fh_data
, fh
.fh_len
, noop
, &newnofp
->nof_stateid
, thd
, cred
, &nvattr
, &xid
);
7368 if ((adlockerror
= nfs_node_lock(adnp
)))
7369 error
= adlockerror
;
7374 if (open
&& adnp
&& !adlockerror
) {
7375 if (!open_error
&& (adnp
->n_flag
& NNEGNCENTRIES
)) {
7376 adnp
->n_flag
&= ~NNEGNCENTRIES
;
7377 cache_purge_negatives(NFSTOV(adnp
));
7379 adnp
->n_flag
|= NMODIFIED
;
7380 nfs_node_unlock(adnp
);
7381 adlockerror
= ENOENT
;
7382 nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
7384 if (adnp
&& !adlockerror
&& (error
== ENOENT
) &&
7385 (cnp
->cn_flags
& MAKEENTRY
) && (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
7386 /* add a negative entry in the name cache */
7387 cache_enter(NFSTOV(adnp
), NULL
, cnp
);
7388 adnp
->n_flag
|= NNEGNCENTRIES
;
7390 if (adnp
&& !adlockerror
) {
7391 nfs_node_unlock(adnp
);
7392 adlockerror
= ENOENT
;
7394 if (!error
&& !anp
&& fh
.fh_len
) {
7395 /* create the vnode with the filehandle and attributes */
7397 error
= nfs_nget(NFSTOMP(np
), adnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &anp
);
7400 nfs_node_unlock(anp
);
7402 if (!error
&& open
) {
7403 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
7404 /* After we have a node, add our open file struct to the node */
7406 error
= nfs_open_file_find_internal(anp
, noop
, &nofp
, 0, 0, 0);
7408 /* This shouldn't happen, because we passed in a new nofp to use. */
7409 printf("nfs_open_file_find_internal failed! %d\n", error
);
7411 } else if (nofp
!= newnofp
) {
7413 * Hmm... an open file struct already exists.
7414 * Mark the existing one busy and merge our open into it.
7415 * Then destroy the one we created.
7416 * Note: there's no chance of an open confict because the
7417 * open has already been granted.
7419 nofpbusyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7420 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
7421 nofp
->nof_stateid
= newnofp
->nof_stateid
;
7422 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
7423 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7424 nfs_open_file_clear_busy(newnofp
);
7425 nfs_open_file_destroy(newnofp
);
7431 /* mark the node as holding a create-initiated open */
7432 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
7433 nofp
->nof_creator
= current_thread();
7439 NVATTR_CLEANUP(&nvattr
);
7440 if (open
&& ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
))) {
7441 if (!error
&& anp
&& !recall
) {
7442 /* stuff the delegation state in the node */
7443 lck_mtx_lock(&anp
->n_openlock
);
7444 anp
->n_openflags
&= ~N_DELEG_MASK
;
7445 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7446 anp
->n_dstateid
= dstateid
;
7448 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7449 lck_mtx_lock(&nmp
->nm_lock
);
7450 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7451 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7452 lck_mtx_unlock(&nmp
->nm_lock
);
7454 lck_mtx_unlock(&anp
->n_openlock
);
7456 /* give the delegation back */
7458 if (NFS_CMPFH(anp
, fh
.fh_data
, fh
.fh_len
)) {
7459 /* update delegation state and return it */
7460 lck_mtx_lock(&anp
->n_openlock
);
7461 anp
->n_openflags
&= ~N_DELEG_MASK
;
7462 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7463 anp
->n_dstateid
= dstateid
;
7465 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7466 lck_mtx_lock(&nmp
->nm_lock
);
7467 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7468 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7469 lck_mtx_unlock(&nmp
->nm_lock
);
7471 lck_mtx_unlock(&anp
->n_openlock
);
7472 /* don't need to send a separate delegreturn for fh */
7475 /* return anp's current delegation */
7476 nfs4_delegation_return(anp
, 0, thd
, cred
);
7478 if (fh
.fh_len
) /* return fh's delegation if it wasn't for anp */
7479 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
7484 /* need to cleanup our temporary nofp */
7485 nfs_open_file_clear_busy(newnofp
);
7486 nfs_open_file_destroy(newnofp
);
7488 } else if (nofp
&& !nofpbusyerror
) {
7489 nfs_open_file_clear_busy(nofp
);
7490 nofpbusyerror
= ENOENT
;
7492 if (inuse
&& nfs_mount_state_in_use_end(nmp
, error
)) {
7494 nofp
= newnofp
= NULL
;
7495 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
7498 slen
= sizeof(sbuf
);
7499 nfsm_chain_cleanup(&nmreq
);
7500 nfsm_chain_cleanup(&nmrep
);
7502 vnode_put(NFSTOV(anp
));
7505 hadattrdir
= (adnp
!= NULL
);
7507 nfs_open_owner_clear_busy(noop
);
7514 nfs_open_owner_clear_busy(noop
);
7517 nfs_open_owner_rele(noop
);
7520 if (!error
&& prefetch
&& nmrep
.nmc_mhead
) {
7521 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
7522 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_NVERIFY
);
7523 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
7524 nfsm_chain_get_32(error
, &nmrep
, eof
);
7525 nfsm_chain_get_32(error
, &nmrep
, retlen
);
7526 if (!error
&& anp
) {
7528 * There can be one problem with doing the prefetch.
7529 * Because we don't have the node before we start the RPC, we
7530 * can't have the buffer busy while the READ is performed.
7531 * So there is a chance that other I/O occured on the same
7532 * range of data while we were performing this RPC. If that
7533 * happens, then it's possible the data we have in the READ
7534 * response is no longer up to date.
7535 * Once we have the node and the buffer, we need to make sure
7536 * that there's no chance we could be putting stale data in
7538 * So, we check if the range read is dirty or if any I/O may
7539 * have occured on it while we were performing our RPC.
7541 struct nfsbuf
*bp
= NULL
;
7545 retlen
= MIN(retlen
, rlen
);
7547 /* check if node needs size update or invalidation */
7548 if (ISSET(anp
->n_flag
, NUPDATESIZE
))
7549 nfs_data_update_size(anp
, 0);
7550 if (!(error
= nfs_node_lock(anp
))) {
7551 if (anp
->n_flag
& NNEEDINVALIDATE
) {
7552 anp
->n_flag
&= ~NNEEDINVALIDATE
;
7553 nfs_node_unlock(anp
);
7554 error
= nfs_vinvalbuf(NFSTOV(anp
), V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
7555 if (!error
) /* lets play it safe and just drop the data */
7558 nfs_node_unlock(anp
);
7562 /* calculate page mask for the range of data read */
7563 lastpg
= (trunc_page_32(retlen
) - 1) / PAGE_SIZE
;
7564 pagemask
= ((1 << (lastpg
+ 1)) - 1);
7567 error
= nfs_buf_get(anp
, 0, nmp
->nm_biosize
, thd
, NBLK_READ
|NBLK_NOWAIT
, &bp
);
7568 /* don't save the data if dirty or potential I/O conflict */
7569 if (!error
&& bp
&& !bp
->nb_dirtyoff
&& !(bp
->nb_dirty
& pagemask
) &&
7570 timevalcmp(&anp
->n_lastio
, &now
, <)) {
7571 OSAddAtomic64(1, &nfsstats
.read_bios
);
7572 CLR(bp
->nb_flags
, (NB_DONE
|NB_ASYNC
));
7573 SET(bp
->nb_flags
, NB_READ
);
7575 nfsm_chain_get_opaque(error
, &nmrep
, retlen
, bp
->nb_data
);
7577 bp
->nb_error
= error
;
7578 SET(bp
->nb_flags
, NB_ERROR
);
7581 bp
->nb_endio
= rlen
;
7582 if ((retlen
> 0) && (bp
->nb_endio
< (int)retlen
))
7583 bp
->nb_endio
= retlen
;
7584 if (eof
|| (retlen
== 0)) {
7585 /* zero out the remaining data (up to EOF) */
7586 off_t rpcrem
, eofrem
, rem
;
7587 rpcrem
= (rlen
- retlen
);
7588 eofrem
= anp
->n_size
- (NBOFF(bp
) + retlen
);
7589 rem
= (rpcrem
< eofrem
) ? rpcrem
: eofrem
;
7591 bzero(bp
->nb_data
+ retlen
, rem
);
7592 } else if ((retlen
< rlen
) && !ISSET(bp
->nb_flags
, NB_ERROR
)) {
7593 /* ugh... short read ... just invalidate for now... */
7594 SET(bp
->nb_flags
, NB_INVAL
);
7597 nfs_buf_read_finish(bp
);
7598 microuptime(&anp
->n_lastio
);
7601 nfs_buf_release(bp
, 1);
7603 error
= 0; /* ignore any transient error in processing the prefetch */
7605 if (adnp
&& !adbusyerror
) {
7606 nfs_node_clear_busy(adnp
);
7607 adbusyerror
= ENOENT
;
7610 nfs_node_clear_busy(np
);
7614 vnode_put(NFSTOV(adnp
));
7615 if (error
&& *anpp
) {
7616 vnode_put(NFSTOV(*anpp
));
7619 nfsm_chain_cleanup(&nmreq
);
7620 nfsm_chain_cleanup(&nmrep
);
7625 * Remove a named attribute.
7628 nfs4_named_attr_remove(nfsnode_t np
, nfsnode_t anp
, const char *name
, vfs_context_t ctx
)
7630 nfsnode_t adnp
= NULL
;
7631 struct nfsmount
*nmp
;
7632 struct componentname cn
;
7633 struct vnop_remove_args vra
;
7634 int error
, putanp
= 0;
7637 if (nfs_mount_gone(nmp
))
7640 bzero(&cn
, sizeof(cn
));
7641 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7642 cn
.cn_namelen
= strlen(name
);
7643 cn
.cn_nameiop
= DELETE
;
7647 error
= nfs4_named_attr_get(np
, &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7648 0, ctx
, &anp
, NULL
);
7649 if ((!error
&& !anp
) || (error
== ENOATTR
))
7653 vnode_put(NFSTOV(anp
));
7661 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7663 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
7664 nfs_node_clear_busy(np
);
7670 vra
.a_desc
= &vnop_remove_desc
;
7671 vra
.a_dvp
= NFSTOV(adnp
);
7672 vra
.a_vp
= NFSTOV(anp
);
7675 vra
.a_context
= ctx
;
7676 error
= nfs_vnop_remove(&vra
);
7679 vnode_put(NFSTOV(adnp
));
7681 vnode_put(NFSTOV(anp
));
7687 struct vnop_getxattr_args
/* {
7688 struct vnodeop_desc *a_desc;
7690 const char * a_name;
7694 vfs_context_t a_context;
7697 vfs_context_t ctx
= ap
->a_context
;
7698 struct nfsmount
*nmp
;
7699 struct nfs_vattr nvattr
;
7700 struct componentname cn
;
7702 int error
= 0, isrsrcfork
;
7704 nmp
= VTONMP(ap
->a_vp
);
7705 if (nfs_mount_gone(nmp
))
7708 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7710 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
7713 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7714 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7717 bzero(&cn
, sizeof(cn
));
7718 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
7719 cn
.cn_namelen
= strlen(ap
->a_name
);
7720 cn
.cn_nameiop
= LOOKUP
;
7721 cn
.cn_flags
= MAKEENTRY
;
7723 /* we'll normally try to prefetch data for xattrs... the resource fork is really a stream */
7724 isrsrcfork
= (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7726 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7727 !isrsrcfork
? NFS_GET_NAMED_ATTR_PREFETCH
: 0, ctx
, &anp
, NULL
);
7728 if ((!error
&& !anp
) || (error
== ENOENT
))
7732 error
= nfs_bioread(anp
, ap
->a_uio
, 0, ctx
);
7734 *ap
->a_size
= anp
->n_size
;
7737 vnode_put(NFSTOV(anp
));
7743 struct vnop_setxattr_args
/* {
7744 struct vnodeop_desc *a_desc;
7746 const char * a_name;
7749 vfs_context_t a_context;
7752 vfs_context_t ctx
= ap
->a_context
;
7753 int options
= ap
->a_options
;
7754 uio_t uio
= ap
->a_uio
;
7755 const char *name
= ap
->a_name
;
7756 struct nfsmount
*nmp
;
7757 struct componentname cn
;
7758 nfsnode_t anp
= NULL
;
7759 int error
= 0, closeerror
= 0, flags
, isrsrcfork
, isfinderinfo
, empty
= 0, i
;
7760 #define FINDERINFOSIZE 32
7761 uint8_t finfo
[FINDERINFOSIZE
];
7763 struct nfs_open_file
*nofp
= NULL
;
7764 char uio_buf
[ UIO_SIZEOF(1) ];
7766 struct vnop_write_args vwa
;
7768 nmp
= VTONMP(ap
->a_vp
);
7769 if (nfs_mount_gone(nmp
))
7772 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7775 if ((options
& XATTR_CREATE
) && (options
& XATTR_REPLACE
))
7778 /* XXX limitation based on need to back up uio on short write */
7779 if (uio_iovcnt(uio
) > 1) {
7780 printf("nfs4_vnop_setxattr: iovcnt > 1\n");
7784 bzero(&cn
, sizeof(cn
));
7785 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7786 cn
.cn_namelen
= strlen(name
);
7787 cn
.cn_nameiop
= CREATE
;
7788 cn
.cn_flags
= MAKEENTRY
;
7790 isfinderinfo
= (bcmp(name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0);
7791 isrsrcfork
= isfinderinfo
? 0 : (bcmp(name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7793 uio_setoffset(uio
, 0);
7795 if (uio_resid(uio
) != sizeof(finfo
))
7797 error
= uiomove((char*)&finfo
, sizeof(finfo
), uio
);
7800 /* setting a FinderInfo of all zeroes means remove the FinderInfo */
7802 for (i
=0, finfop
=(uint32_t*)&finfo
; i
< (int)(sizeof(finfo
)/sizeof(uint32_t)); i
++)
7807 if (empty
&& !(options
& (XATTR_CREATE
|XATTR_REPLACE
))) {
7808 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7809 if (error
== ENOENT
)
7813 /* first, let's see if we get a create/replace error */
7817 * create/open the xattr
7819 * We need to make sure not to create it if XATTR_REPLACE.
7820 * For all xattrs except the resource fork, we also want to
7821 * truncate the xattr to remove any current data. We'll do
7822 * that by setting the size to 0 on create/open.
7825 if (!(options
& XATTR_REPLACE
))
7826 flags
|= NFS_GET_NAMED_ATTR_CREATE
;
7827 if (options
& XATTR_CREATE
)
7828 flags
|= NFS_GET_NAMED_ATTR_CREATE_GUARDED
;
7830 flags
|= NFS_GET_NAMED_ATTR_TRUNCATE
;
7832 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
7833 flags
, ctx
, &anp
, &nofp
);
7838 /* grab the open state from the get/create/open */
7839 if (nofp
&& !(error
= nfs_open_file_set_busy(nofp
, NULL
))) {
7840 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
7841 nofp
->nof_creator
= NULL
;
7842 nfs_open_file_clear_busy(nofp
);
7845 /* Setting an empty FinderInfo really means remove it, skip to the close/remove */
7846 if (isfinderinfo
&& empty
)
7850 * Write the data out and flush.
7852 * For FinderInfo, we've already copied the data to finfo, so do I/O from there.
7854 vwa
.a_desc
= &vnop_write_desc
;
7855 vwa
.a_vp
= NFSTOV(anp
);
7858 vwa
.a_context
= ctx
;
7860 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
, &uio_buf
, sizeof(uio_buf
));
7861 uio_addiov(auio
, (uintptr_t)&finfo
, sizeof(finfo
));
7863 } else if (uio_resid(uio
) > 0) {
7867 error
= nfs_vnop_write(&vwa
);
7869 error
= nfs_flush(anp
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
7872 /* Close the xattr. */
7874 int busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7875 closeerror
= nfs_close(anp
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
7877 nfs_open_file_clear_busy(nofp
);
7879 if (!error
&& isfinderinfo
&& empty
) { /* Setting an empty FinderInfo really means remove it */
7880 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7881 if (error
== ENOENT
)
7888 vnode_put(NFSTOV(anp
));
7889 if (error
== ENOENT
)
7895 nfs4_vnop_removexattr(
7896 struct vnop_removexattr_args
/* {
7897 struct vnodeop_desc *a_desc;
7899 const char * a_name;
7901 vfs_context_t a_context;
7904 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7907 if (nfs_mount_gone(nmp
))
7909 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7912 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), NULL
, ap
->a_name
, ap
->a_context
);
7913 if (error
== ENOENT
)
7919 nfs4_vnop_listxattr(
7920 struct vnop_listxattr_args
/* {
7921 struct vnodeop_desc *a_desc;
7926 vfs_context_t a_context;
7929 vfs_context_t ctx
= ap
->a_context
;
7930 nfsnode_t np
= VTONFS(ap
->a_vp
);
7931 uio_t uio
= ap
->a_uio
;
7932 nfsnode_t adnp
= NULL
;
7933 struct nfsmount
*nmp
;
7935 struct nfs_vattr nvattr
;
7936 uint64_t cookie
, nextcookie
, lbn
= 0;
7937 struct nfsbuf
*bp
= NULL
;
7938 struct nfs_dir_buf_header
*ndbhp
;
7939 struct direntry
*dp
;
7941 nmp
= VTONMP(ap
->a_vp
);
7942 if (nfs_mount_gone(nmp
))
7945 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7948 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
7951 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7952 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7955 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7957 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
7958 nfs_node_clear_busy(np
);
7962 if ((error
= nfs_node_lock(adnp
)))
7965 if (adnp
->n_flag
& NNEEDINVALIDATE
) {
7966 adnp
->n_flag
&= ~NNEEDINVALIDATE
;
7968 nfs_node_unlock(adnp
);
7969 error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
7971 error
= nfs_node_lock(adnp
);
7977 * check for need to invalidate when (re)starting at beginning
7979 if (adnp
->n_flag
& NMODIFIED
) {
7981 nfs_node_unlock(adnp
);
7982 if ((error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1)))
7985 nfs_node_unlock(adnp
);
7987 /* nfs_getattr() will check changed and purge caches */
7988 if ((error
= nfs_getattr(adnp
, &nvattr
, ctx
, NGA_UNCACHED
)))
7991 if (uio
&& (uio_resid(uio
) == 0))
7995 nextcookie
= lbn
= 0;
7997 while (!error
&& !done
) {
7998 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
7999 cookie
= nextcookie
;
8001 error
= nfs_buf_get(adnp
, lbn
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
8004 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
8005 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
8006 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
8007 ndbhp
->ndbh_flags
= 0;
8008 ndbhp
->ndbh_count
= 0;
8009 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
8010 ndbhp
->ndbh_ncgen
= adnp
->n_ncgen
;
8012 error
= nfs_buf_readdir(bp
, ctx
);
8013 if (error
== NFSERR_DIRBUFDROPPED
)
8016 nfs_buf_release(bp
, 1);
8017 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
8018 if (!nfs_node_lock(adnp
)) {
8020 nfs_node_unlock(adnp
);
8022 nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
8023 if (error
== NFSERR_BAD_COOKIE
)
8030 /* go through all the entries copying/counting */
8031 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
8032 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
8033 if (!xattr_protected(dp
->d_name
)) {
8035 *ap
->a_size
+= dp
->d_namlen
+ 1;
8036 } else if (uio_resid(uio
) < (dp
->d_namlen
+ 1)) {
8039 error
= uiomove(dp
->d_name
, dp
->d_namlen
+1, uio
);
8040 if (error
&& (error
!= EFAULT
))
8044 nextcookie
= dp
->d_seekoff
;
8045 dp
= NFS_DIRENTRY_NEXT(dp
);
8048 if (i
== ndbhp
->ndbh_count
) {
8049 /* hit end of buffer, move to next buffer */
8051 /* if we also hit EOF, we're done */
8052 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
))
8055 if (!error
&& !done
&& (nextcookie
== cookie
)) {
8056 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
8059 nfs_buf_release(bp
, 1);
8063 vnode_put(NFSTOV(adnp
));
8069 nfs4_vnop_getnamedstream(
8070 struct vnop_getnamedstream_args
/* {
8071 struct vnodeop_desc *a_desc;
8075 enum nsoperation a_operation;
8077 vfs_context_t a_context;
8080 vfs_context_t ctx
= ap
->a_context
;
8081 struct nfsmount
*nmp
;
8082 struct nfs_vattr nvattr
;
8083 struct componentname cn
;
8087 nmp
= VTONMP(ap
->a_vp
);
8088 if (nfs_mount_gone(nmp
))
8091 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8093 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
8096 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
8097 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
8100 bzero(&cn
, sizeof(cn
));
8101 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8102 cn
.cn_namelen
= strlen(ap
->a_name
);
8103 cn
.cn_nameiop
= LOOKUP
;
8104 cn
.cn_flags
= MAKEENTRY
;
8106 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
8107 0, ctx
, &anp
, NULL
);
8108 if ((!error
&& !anp
) || (error
== ENOENT
))
8111 *ap
->a_svpp
= NFSTOV(anp
);
8113 vnode_put(NFSTOV(anp
));
8118 nfs4_vnop_makenamedstream(
8119 struct vnop_makenamedstream_args
/* {
8120 struct vnodeop_desc *a_desc;
8125 vfs_context_t a_context;
8128 vfs_context_t ctx
= ap
->a_context
;
8129 struct nfsmount
*nmp
;
8130 struct componentname cn
;
8134 nmp
= VTONMP(ap
->a_vp
);
8135 if (nfs_mount_gone(nmp
))
8138 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8141 bzero(&cn
, sizeof(cn
));
8142 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8143 cn
.cn_namelen
= strlen(ap
->a_name
);
8144 cn
.cn_nameiop
= CREATE
;
8145 cn
.cn_flags
= MAKEENTRY
;
8147 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
8148 NFS_GET_NAMED_ATTR_CREATE
, ctx
, &anp
, NULL
);
8149 if ((!error
&& !anp
) || (error
== ENOENT
))
8152 *ap
->a_svpp
= NFSTOV(anp
);
8154 vnode_put(NFSTOV(anp
));
8159 nfs4_vnop_removenamedstream(
8160 struct vnop_removenamedstream_args
/* {
8161 struct vnodeop_desc *a_desc;
8166 vfs_context_t a_context;
8169 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
8170 nfsnode_t np
= ap
->a_vp
? VTONFS(ap
->a_vp
) : NULL
;
8171 nfsnode_t anp
= ap
->a_svp
? VTONFS(ap
->a_svp
) : NULL
;
8173 if (nfs_mount_gone(nmp
))
8177 * Given that a_svp is a named stream, checking for
8178 * named attribute support is kinda pointless.
8180 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8183 return (nfs4_named_attr_remove(np
, anp
, ap
->a_name
, ap
->a_context
));