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
, 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_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
120 if ((lockerror
= nfs_node_lock(np
)))
122 nfsm_chain_skip_tag(error
, &nmrep
);
123 nfsm_chain_get_32(error
, &nmrep
, numops
);
124 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
125 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_ACCESS
);
126 nfsm_chain_get_32(error
, &nmrep
, supported
);
127 nfsm_chain_get_32(error
, &nmrep
, access_result
);
129 if ((missing
= (*access
& ~supported
))) {
130 /* missing support for something(s) we wanted */
131 if (missing
& NFS_ACCESS_DELETE
) {
133 * If the server doesn't report DELETE (possible
134 * on UNIX systems), we'll assume that it is OK
135 * and just let any subsequent delete action fail
136 * if it really isn't deletable.
138 access_result
|= NFS_ACCESS_DELETE
;
141 /* ".zfs" subdirectories may erroneously give a denied answer for modify/delete */
142 if (nfs_access_dotzfs
) {
143 vnode_t dvp
= NULLVP
;
144 if (np
->n_flag
& NISDOTZFSCHILD
) /* may be able to create/delete snapshot dirs */
145 access_result
|= (NFS_ACCESS_MODIFY
|NFS_ACCESS_EXTEND
|NFS_ACCESS_DELETE
);
146 else if (((dvp
= vnode_getparent(NFSTOV(np
))) != NULLVP
) && (VTONFS(dvp
)->n_flag
& NISDOTZFSCHILD
))
147 access_result
|= NFS_ACCESS_DELETE
; /* may be able to delete snapshot dirs */
151 /* Some servers report DELETE support but erroneously give a denied answer. */
152 if (nfs_access_delete
&& (*access
& NFS_ACCESS_DELETE
) && !(access_result
& NFS_ACCESS_DELETE
))
153 access_result
|= NFS_ACCESS_DELETE
;
154 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
155 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
158 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
159 slot
= nfs_node_access_slot(np
, uid
, 1);
160 np
->n_accessuid
[slot
] = uid
;
162 np
->n_accessstamp
[slot
] = now
.tv_sec
;
163 np
->n_access
[slot
] = access_result
;
165 /* pass back the access returned with this request */
166 *access
= np
->n_access
[slot
];
170 nfsm_chain_cleanup(&nmreq
);
171 nfsm_chain_cleanup(&nmrep
);
183 struct nfs_vattr
*nvap
,
186 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
187 int error
= 0, status
, nfsvers
, numops
, rpcflags
= 0, acls
;
188 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
189 struct nfsm_chain nmreq
, nmrep
;
190 struct nfsreq_secinfo_args si
;
194 nfsvers
= nmp
->nm_vers
;
195 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
197 if (np
&& (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)) {
198 nfs4_default_attrs_for_referral_trigger(VTONFS(np
->n_parent
), NULL
, 0, nvap
, NULL
);
202 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
203 rpcflags
= R_RECOVER
;
205 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
206 nfsm_chain_null(&nmreq
);
207 nfsm_chain_null(&nmrep
);
211 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
212 nfsm_chain_add_compound_header(error
, &nmreq
, "getattr", numops
);
214 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
215 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fhp
, fhsize
);
217 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
218 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
219 if ((flags
& NGA_ACL
) && acls
)
220 NFS_BITMAP_SET(bitmap
, NFS_FATTR_ACL
);
221 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
222 nfsm_chain_build_done(error
, &nmreq
);
223 nfsm_assert(error
, (numops
== 0), EPROTO
);
225 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC4_COMPOUND
,
226 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
227 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
229 nfsm_chain_skip_tag(error
, &nmrep
);
230 nfsm_chain_get_32(error
, &nmrep
, numops
);
231 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
232 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
234 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
236 if ((flags
& NGA_ACL
) && acls
&& !NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_ACL
)) {
237 /* we asked for the ACL but didn't get one... assume there isn't one */
238 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
239 nvap
->nva_acl
= NULL
;
242 nfsm_chain_cleanup(&nmreq
);
243 nfsm_chain_cleanup(&nmrep
);
248 nfs4_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
250 struct nfsmount
*nmp
;
251 int error
= 0, lockerror
= ENOENT
, status
, numops
;
254 struct nfsm_chain nmreq
, nmrep
;
255 struct nfsreq_secinfo_args si
;
260 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
262 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
263 nfsm_chain_null(&nmreq
);
264 nfsm_chain_null(&nmrep
);
266 // PUTFH, GETATTR, READLINK
268 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
269 nfsm_chain_add_compound_header(error
, &nmreq
, "readlink", numops
);
271 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
272 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
274 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
275 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
277 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READLINK
);
278 nfsm_chain_build_done(error
, &nmreq
);
279 nfsm_assert(error
, (numops
== 0), EPROTO
);
281 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
283 if ((lockerror
= nfs_node_lock(np
)))
285 nfsm_chain_skip_tag(error
, &nmrep
);
286 nfsm_chain_get_32(error
, &nmrep
, numops
);
287 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
288 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
289 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
290 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READLINK
);
291 nfsm_chain_get_32(error
, &nmrep
, len
);
293 if (len
>= *buflenp
) {
294 if (np
->n_size
&& (np
->n_size
< *buflenp
))
299 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
305 nfsm_chain_cleanup(&nmreq
);
306 nfsm_chain_cleanup(&nmrep
);
317 struct nfsreq_cbinfo
*cb
,
318 struct nfsreq
**reqp
)
320 struct nfsmount
*nmp
;
321 int error
= 0, nfsvers
, numops
;
323 struct nfsm_chain nmreq
;
324 struct nfsreq_secinfo_args si
;
329 nfsvers
= nmp
->nm_vers
;
330 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
333 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
334 nfsm_chain_null(&nmreq
);
336 // PUTFH, READ, GETATTR
338 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
339 nfsm_chain_add_compound_header(error
, &nmreq
, "read", numops
);
341 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
342 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
344 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
345 nfs_get_stateid(np
, thd
, cred
, &stateid
);
346 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
347 nfsm_chain_add_64(error
, &nmreq
, offset
);
348 nfsm_chain_add_32(error
, &nmreq
, len
);
350 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
351 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
352 nfsm_chain_build_done(error
, &nmreq
);
353 nfsm_assert(error
, (numops
== 0), EPROTO
);
355 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
357 nfsm_chain_cleanup(&nmreq
);
362 nfs4_read_rpc_async_finish(
369 struct nfsmount
*nmp
;
370 int error
= 0, lockerror
, nfsvers
, numops
, status
, eof
= 0;
373 struct nfsm_chain nmrep
;
377 nfs_request_async_cancel(req
);
380 nfsvers
= nmp
->nm_vers
;
382 nfsm_chain_null(&nmrep
);
384 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
385 if (error
== EINPROGRESS
) /* async request restarted */
388 if ((lockerror
= nfs_node_lock(np
)))
390 nfsm_chain_skip_tag(error
, &nmrep
);
391 nfsm_chain_get_32(error
, &nmrep
, numops
);
392 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
393 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
394 nfsm_chain_get_32(error
, &nmrep
, eof
);
395 nfsm_chain_get_32(error
, &nmrep
, retlen
);
397 *lenp
= MIN(retlen
, *lenp
);
398 error
= nfsm_chain_get_uio(&nmrep
, *lenp
, uio
);
400 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
401 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
409 nfsm_chain_cleanup(&nmrep
);
410 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
411 microuptime(&np
->n_lastio
);
416 nfs4_write_rpc_async(
423 struct nfsreq_cbinfo
*cb
,
424 struct nfsreq
**reqp
)
426 struct nfsmount
*nmp
;
428 int error
= 0, nfsvers
, numops
;
430 struct nfsm_chain nmreq
;
431 struct nfsreq_secinfo_args si
;
436 nfsvers
= nmp
->nm_vers
;
437 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
440 /* for async mounts, don't bother sending sync write requests */
441 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
442 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
443 iomode
= NFS_WRITE_UNSTABLE
;
445 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
446 nfsm_chain_null(&nmreq
);
448 // PUTFH, WRITE, GETATTR
450 nfsm_chain_build_alloc_init(error
, &nmreq
, 25 * NFSX_UNSIGNED
+ len
);
451 nfsm_chain_add_compound_header(error
, &nmreq
, "write", numops
);
453 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
454 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
456 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_WRITE
);
457 nfs_get_stateid(np
, thd
, cred
, &stateid
);
458 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
459 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
460 nfsm_chain_add_32(error
, &nmreq
, iomode
);
461 nfsm_chain_add_32(error
, &nmreq
, len
);
463 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
465 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
466 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
467 nfsm_chain_build_done(error
, &nmreq
);
468 nfsm_assert(error
, (numops
== 0), EPROTO
);
471 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, cb
, reqp
);
473 nfsm_chain_cleanup(&nmreq
);
478 nfs4_write_rpc_async_finish(
485 struct nfsmount
*nmp
;
486 int error
= 0, lockerror
= ENOENT
, nfsvers
, numops
, status
;
487 int committed
= NFS_WRITE_FILESYNC
;
489 u_int64_t xid
, wverf
;
491 struct nfsm_chain nmrep
;
495 nfs_request_async_cancel(req
);
498 nfsvers
= nmp
->nm_vers
;
500 nfsm_chain_null(&nmrep
);
502 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
503 if (error
== EINPROGRESS
) /* async request restarted */
508 if (!error
&& (lockerror
= nfs_node_lock(np
)))
510 nfsm_chain_skip_tag(error
, &nmrep
);
511 nfsm_chain_get_32(error
, &nmrep
, numops
);
512 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
513 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_WRITE
);
514 nfsm_chain_get_32(error
, &nmrep
, rlen
);
519 nfsm_chain_get_32(error
, &nmrep
, committed
);
520 nfsm_chain_get_64(error
, &nmrep
, wverf
);
524 lck_mtx_lock(&nmp
->nm_lock
);
525 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
526 nmp
->nm_verf
= wverf
;
527 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
528 } else if (nmp
->nm_verf
!= wverf
) {
529 nmp
->nm_verf
= wverf
;
531 lck_mtx_unlock(&nmp
->nm_lock
);
532 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
533 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
537 nfsm_chain_cleanup(&nmrep
);
538 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
539 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
540 committed
= NFS_WRITE_FILESYNC
;
541 *iomodep
= committed
;
542 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)
543 microuptime(&np
->n_lastio
);
555 int error
= 0, lockerror
= ENOENT
, remove_error
= 0, status
;
556 struct nfsmount
*nmp
;
559 struct nfsm_chain nmreq
, nmrep
;
560 struct nfsreq_secinfo_args si
;
565 nfsvers
= nmp
->nm_vers
;
566 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
568 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
570 nfsm_chain_null(&nmreq
);
571 nfsm_chain_null(&nmrep
);
573 // PUTFH, REMOVE, GETATTR
575 nfsm_chain_build_alloc_init(error
, &nmreq
, 17 * NFSX_UNSIGNED
+ namelen
);
576 nfsm_chain_add_compound_header(error
, &nmreq
, "remove", numops
);
578 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
579 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
581 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_REMOVE
);
582 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
584 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
585 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
586 nfsm_chain_build_done(error
, &nmreq
);
587 nfsm_assert(error
, (numops
== 0), EPROTO
);
590 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, 0, &nmrep
, &xid
, &status
);
592 if ((lockerror
= nfs_node_lock(dnp
)))
594 nfsm_chain_skip_tag(error
, &nmrep
);
595 nfsm_chain_get_32(error
, &nmrep
, numops
);
596 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
597 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_REMOVE
);
598 remove_error
= error
;
599 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
600 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
601 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
602 if (error
&& !lockerror
)
603 NATTRINVALIDATE(dnp
);
605 nfsm_chain_cleanup(&nmreq
);
606 nfsm_chain_cleanup(&nmrep
);
609 dnp
->n_flag
|= NMODIFIED
;
610 nfs_node_unlock(dnp
);
612 if (error
== NFSERR_GRACE
) {
613 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
617 return (remove_error
);
630 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
631 struct nfsmount
*nmp
;
632 u_int64_t xid
, savedxid
;
633 struct nfsm_chain nmreq
, nmrep
;
634 struct nfsreq_secinfo_args si
;
636 nmp
= NFSTONMP(fdnp
);
639 nfsvers
= nmp
->nm_vers
;
640 if (fdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
642 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
645 NFSREQ_SECINFO_SET(&si
, fdnp
, NULL
, 0, NULL
, 0);
646 nfsm_chain_null(&nmreq
);
647 nfsm_chain_null(&nmrep
);
649 // PUTFH(FROM), SAVEFH, PUTFH(TO), RENAME, GETATTR(TO), RESTOREFH, GETATTR(FROM)
651 nfsm_chain_build_alloc_init(error
, &nmreq
, 30 * NFSX_UNSIGNED
+ fnamelen
+ tnamelen
);
652 nfsm_chain_add_compound_header(error
, &nmreq
, "rename", numops
);
654 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
655 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
657 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
659 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
660 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
662 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RENAME
);
663 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
664 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
666 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
667 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
669 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
671 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
672 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, fdnp
);
673 nfsm_chain_build_done(error
, &nmreq
);
674 nfsm_assert(error
, (numops
== 0), EPROTO
);
677 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
679 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
681 nfsm_chain_skip_tag(error
, &nmrep
);
682 nfsm_chain_get_32(error
, &nmrep
, numops
);
683 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
684 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
685 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
686 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RENAME
);
687 nfsm_chain_check_change_info(error
, &nmrep
, fdnp
);
688 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
689 /* directory attributes: if we don't get them, make sure to invalidate */
690 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
692 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
693 if (error
&& !lockerror
)
694 NATTRINVALIDATE(tdnp
);
695 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
696 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
698 nfsm_chain_loadattr(error
, &nmrep
, fdnp
, nfsvers
, &xid
);
699 if (error
&& !lockerror
)
700 NATTRINVALIDATE(fdnp
);
702 nfsm_chain_cleanup(&nmreq
);
703 nfsm_chain_cleanup(&nmrep
);
705 fdnp
->n_flag
|= NMODIFIED
;
706 tdnp
->n_flag
|= NMODIFIED
;
707 nfs_node_unlock2(fdnp
, tdnp
);
713 * NFS V4 readdir RPC.
716 nfs4_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
718 struct nfsmount
*nmp
;
719 int error
= 0, lockerror
, nfsvers
, namedattr
, rdirplus
, bigcookies
, numops
;
720 int i
, status
, more_entries
= 1, eof
, bp_dropped
= 0;
721 uint32_t nmreaddirsize
, nmrsize
;
722 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
723 uint64_t cookie
, lastcookie
, xid
, savedxid
;
724 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
726 struct nfs_vattr nvattr
, *nvattrp
;
727 struct nfs_dir_buf_header
*ndbhp
;
729 char *padstart
, padlen
;
731 uint32_t entry_attrs
[NFS_ATTR_BITMAP_LEN
];
733 struct nfsreq_secinfo_args si
;
738 nfsvers
= nmp
->nm_vers
;
739 nmreaddirsize
= nmp
->nm_readdirsize
;
740 nmrsize
= nmp
->nm_rsize
;
741 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
742 namedattr
= (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) ? 1 : 0;
743 rdirplus
= (NMFLAG(nmp
, RDIRPLUS
) || namedattr
) ? 1 : 0;
744 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
746 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
749 * Set up attribute request for entries.
750 * For READDIRPLUS functionality, get everything.
751 * Otherwise, just get what we need for struct direntry.
755 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, entry_attrs
);
756 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEHANDLE
);
759 NFS_CLEAR_ATTRIBUTES(entry_attrs
);
760 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_TYPE
);
761 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_FILEID
);
762 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_MOUNTED_ON_FILEID
);
764 NFS_BITMAP_SET(entry_attrs
, NFS_FATTR_RDATTR_ERROR
);
766 /* lock to protect access to cookie verifier */
767 if ((lockerror
= nfs_node_lock(dnp
)))
770 /* determine cookie to use, and move dp to the right offset */
771 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
772 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
773 if (ndbhp
->ndbh_count
) {
774 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
775 dp
= NFS_DIRENTRY_NEXT(dp
);
776 cookie
= dp
->d_seekoff
;
777 dp
= NFS_DIRENTRY_NEXT(dp
);
779 cookie
= bp
->nb_lblkno
;
780 /* increment with every buffer read */
781 OSAddAtomic(1, &nfsstats
.readdir_bios
);
786 * The NFS client is responsible for the "." and ".." entries in the
787 * directory. So, we put them at the start of the first buffer.
788 * Don't bother for attribute directories.
790 if (((bp
->nb_lblkno
== 0) && (ndbhp
->ndbh_count
== 0)) &&
791 !(dnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
)) {
793 fhlen
= rdirplus
? fh
.fh_len
+ 1 : 0;
794 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
797 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
799 bzero(&dp
->d_name
[namlen
+1], xlen
);
800 dp
->d_namlen
= namlen
;
801 strlcpy(dp
->d_name
, ".", namlen
+1);
802 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
804 dp
->d_reclen
= reclen
;
806 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
807 dp
= NFS_DIRENTRY_NEXT(dp
);
808 padlen
= (char*)dp
- padstart
;
810 bzero(padstart
, padlen
);
811 if (rdirplus
) /* zero out attributes */
812 bzero(NFS_DIR_BUF_NVATTR(bp
, 0), sizeof(struct nfs_vattr
));
816 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
818 bzero(&dp
->d_name
[namlen
+1], xlen
);
819 dp
->d_namlen
= namlen
;
820 strlcpy(dp
->d_name
, "..", namlen
+1);
822 dp
->d_fileno
= VTONFS(dnp
->n_parent
)->n_vattr
.nva_fileid
;
824 dp
->d_fileno
= dnp
->n_vattr
.nva_fileid
;
826 dp
->d_reclen
= reclen
;
828 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
829 dp
= NFS_DIRENTRY_NEXT(dp
);
830 padlen
= (char*)dp
- padstart
;
832 bzero(padstart
, padlen
);
833 if (rdirplus
) /* zero out attributes */
834 bzero(NFS_DIR_BUF_NVATTR(bp
, 1), sizeof(struct nfs_vattr
));
836 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
837 ndbhp
->ndbh_count
= 2;
841 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
842 * the buffer is full (or we hit EOF). Then put the remainder of the
843 * results in the next buffer(s).
845 nfsm_chain_null(&nmreq
);
846 nfsm_chain_null(&nmrep
);
847 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
849 // PUTFH, GETATTR, READDIR
851 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
852 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, numops
);
854 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
855 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
857 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
858 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
860 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READDIR
);
861 nfsm_chain_add_64(error
, &nmreq
, (cookie
<= 2) ? 0 : cookie
);
862 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
863 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
864 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
865 nfsm_chain_add_bitmap_supported(error
, &nmreq
, entry_attrs
, nmp
, dnp
);
866 nfsm_chain_build_done(error
, &nmreq
);
867 nfsm_assert(error
, (numops
== 0), EPROTO
);
868 nfs_node_unlock(dnp
);
870 error
= nfs_request(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
872 if ((lockerror
= nfs_node_lock(dnp
)))
876 nfsm_chain_skip_tag(error
, &nmrep
);
877 nfsm_chain_get_32(error
, &nmrep
, numops
);
878 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
879 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
880 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
881 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READDIR
);
882 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
883 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
886 nfs_node_unlock(dnp
);
894 /* loop through the entries packing them into the buffer */
895 while (more_entries
) {
896 /* Entry: COOKIE, NAME, FATTR */
897 nfsm_chain_get_64(error
, &nmrep
, cookie
);
898 nfsm_chain_get_32(error
, &nmrep
, namlen
);
900 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
901 /* we've got a big cookie, make sure flag is set */
902 lck_mtx_lock(&nmp
->nm_lock
);
903 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
904 lck_mtx_unlock(&nmp
->nm_lock
);
907 /* just truncate names that don't fit in direntry.d_name */
912 if (namlen
> (sizeof(dp
->d_name
)-1)) {
913 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
914 namlen
= sizeof(dp
->d_name
) - 1;
918 /* guess that fh size will be same as parent */
919 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
920 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
921 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
922 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
923 space_needed
= reclen
+ attrlen
;
924 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
925 if (space_needed
> space_free
) {
927 * We still have entries to pack, but we've
928 * run out of room in the current buffer.
929 * So we need to move to the next buffer.
930 * The block# for the next buffer is the
931 * last cookie in the current buffer.
934 ndbhp
->ndbh_flags
|= NDB_FULL
;
935 nfs_buf_release(bp
, 0);
938 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
940 /* initialize buffer */
941 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
942 ndbhp
->ndbh_flags
= 0;
943 ndbhp
->ndbh_count
= 0;
944 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
945 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
946 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
947 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
948 /* increment with every buffer read */
949 OSAddAtomic(1, &nfsstats
.readdir_bios
);
952 dp
->d_fileno
= cookie
; /* placeholder */
953 dp
->d_seekoff
= cookie
;
954 dp
->d_namlen
= namlen
;
955 dp
->d_reclen
= reclen
;
956 dp
->d_type
= DT_UNKNOWN
;
957 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
959 dp
->d_name
[namlen
] = '\0';
961 nfsm_chain_adv(error
, &nmrep
,
962 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
964 nvattrp
= rdirplus
? NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
) : &nvattr
;
965 error
= nfs4_parsefattr(&nmrep
, NULL
, nvattrp
, &fh
, NULL
, NULL
);
966 if (!error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
)) {
967 /* we do NOT want ACLs returned to us here */
968 NFS_BITMAP_CLR(nvattrp
->nva_bitmap
, NFS_FATTR_ACL
);
969 if (nvattrp
->nva_acl
) {
970 kauth_acl_free(nvattrp
->nva_acl
);
971 nvattrp
->nva_acl
= NULL
;
974 if (error
&& NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_RDATTR_ERROR
)) {
975 /* OK, we may not have gotten all of the attributes but we will use what we can. */
976 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
977 /* set this up to look like a referral trigger */
978 nfs4_default_attrs_for_referral_trigger(dnp
, dp
->d_name
, namlen
, nvattrp
, &fh
);
982 /* check for more entries after this one */
983 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
986 /* Skip any "." and ".." entries returned from server. */
987 /* Also skip any bothersome named attribute entries. */
988 if (((dp
->d_name
[0] == '.') && ((namlen
== 1) || ((namlen
== 2) && (dp
->d_name
[1] == '.')))) ||
989 (namedattr
&& (namlen
== 11) && (!strcmp(dp
->d_name
, "SUNWattr_ro") || !strcmp(dp
->d_name
, "SUNWattr_rw")))) {
994 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_TYPE
))
995 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
996 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEID
))
997 dp
->d_fileno
= nvattrp
->nva_fileid
;
999 /* fileid is already in d_fileno, so stash xid in attrs */
1000 nvattrp
->nva_fileid
= savedxid
;
1001 if (NFS_BITMAP_ISSET(nvattrp
->nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
1002 fhlen
= fh
.fh_len
+ 1;
1003 xlen
= fhlen
+ sizeof(time_t);
1004 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1005 space_needed
= reclen
+ attrlen
;
1006 if (space_needed
> space_free
) {
1007 /* didn't actually have the room... move on to next buffer */
1011 /* pack the file handle into the record */
1012 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
1013 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
1015 /* mark the file handle invalid */
1017 fhlen
= fh
.fh_len
+ 1;
1018 xlen
= fhlen
+ sizeof(time_t);
1019 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
1020 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
1022 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
1023 dp
->d_reclen
= reclen
;
1025 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
1026 ndbhp
->ndbh_count
++;
1027 lastcookie
= cookie
;
1029 /* advance to next direntry in buffer */
1030 dp
= NFS_DIRENTRY_NEXT(dp
);
1031 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
1032 /* zero out the pad bytes */
1033 padlen
= (char*)dp
- padstart
;
1035 bzero(padstart
, padlen
);
1037 /* Finally, get the eof boolean */
1038 nfsm_chain_get_32(error
, &nmrep
, eof
);
1041 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
1042 nfs_node_lock_force(dnp
);
1043 dnp
->n_eofcookie
= lastcookie
;
1044 nfs_node_unlock(dnp
);
1049 nfs_buf_release(bp
, 0);
1053 if ((lockerror
= nfs_node_lock(dnp
)))
1056 nfsm_chain_cleanup(&nmrep
);
1057 nfsm_chain_null(&nmreq
);
1060 if (bp_dropped
&& bp
)
1061 nfs_buf_release(bp
, 0);
1063 nfs_node_unlock(dnp
);
1064 nfsm_chain_cleanup(&nmreq
);
1065 nfsm_chain_cleanup(&nmrep
);
1066 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
1070 nfs4_lookup_rpc_async(
1075 struct nfsreq
**reqp
)
1077 int error
= 0, isdotdot
= 0, nfsvers
, numops
;
1078 struct nfsm_chain nmreq
;
1079 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1080 struct nfsmount
*nmp
;
1081 struct nfsreq_secinfo_args si
;
1083 nmp
= NFSTONMP(dnp
);
1086 nfsvers
= nmp
->nm_vers
;
1087 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1090 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2)) {
1092 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
1094 NFSREQ_SECINFO_SET(&si
, dnp
, dnp
->n_fhp
, dnp
->n_fhsize
, name
, namelen
);
1097 nfsm_chain_null(&nmreq
);
1099 // PUTFH, GETATTR, LOOKUP(P), GETFH, GETATTR (FH)
1101 nfsm_chain_build_alloc_init(error
, &nmreq
, 20 * NFSX_UNSIGNED
+ namelen
);
1102 nfsm_chain_add_compound_header(error
, &nmreq
, "lookup", numops
);
1104 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1105 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
1107 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1108 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
1111 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUPP
);
1113 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
1114 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
1117 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETFH
);
1119 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1120 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1121 /* some ".zfs" directories can't handle being asked for some attributes */
1122 if ((dnp
->n_flag
& NISDOTZFS
) && !isdotdot
)
1123 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1124 if ((dnp
->n_flag
& NISDOTZFSCHILD
) && isdotdot
)
1125 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1126 if (((namelen
== 4) && (name
[0] == '.') && (name
[1] == 'z') && (name
[2] == 'f') && (name
[3] == 's')))
1127 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_NAMED_ATTR
);
1128 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
1129 nfsm_chain_build_done(error
, &nmreq
);
1130 nfsm_assert(error
, (numops
== 0), EPROTO
);
1132 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1133 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, reqp
);
1135 nfsm_chain_cleanup(&nmreq
);
1141 nfs4_lookup_rpc_async_finish(
1149 struct nfs_vattr
*nvap
)
1151 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
, isdotdot
= 0;
1152 uint32_t op
= NFS_OP_LOOKUP
;
1154 struct nfsmount
*nmp
;
1155 struct nfsm_chain nmrep
;
1157 nmp
= NFSTONMP(dnp
);
1158 nfsvers
= nmp
->nm_vers
;
1159 if ((name
[0] == '.') && (name
[1] == '.') && (namelen
== 2))
1162 nfsm_chain_null(&nmrep
);
1164 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
1166 if ((lockerror
= nfs_node_lock(dnp
)))
1168 nfsm_chain_skip_tag(error
, &nmrep
);
1169 nfsm_chain_get_32(error
, &nmrep
, numops
);
1170 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1171 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1174 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
1176 nfsm_chain_op_check(error
, &nmrep
, (isdotdot
? NFS_OP_LOOKUPP
: NFS_OP_LOOKUP
));
1177 nfsmout_if(error
|| !fhp
|| !nvap
);
1178 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETFH
);
1179 nfsm_chain_get_32(error
, &nmrep
, fhp
->fh_len
);
1180 nfsm_chain_get_opaque(error
, &nmrep
, fhp
->fh_len
, fhp
->fh_data
);
1181 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1182 if ((error
== NFSERR_MOVED
) || (error
== NFSERR_INVAL
)) {
1183 /* set this up to look like a referral trigger */
1184 nfs4_default_attrs_for_referral_trigger(dnp
, name
, namelen
, nvap
, fhp
);
1188 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
1192 nfs_node_unlock(dnp
);
1193 nfsm_chain_cleanup(&nmrep
);
1194 if (!error
&& (op
== NFS_OP_LOOKUP
) && (nmp
->nm_state
& NFSSTA_NEEDSECINFO
)) {
1195 /* We still need to get SECINFO to set default for mount. */
1196 /* Do so for the first LOOKUP that returns successfully. */
1199 sec
.count
= NX_MAX_SEC_FLAVORS
;
1200 error
= nfs4_secinfo_rpc(nmp
, &req
->r_secinfo
, vfs_context_ucred(ctx
), sec
.flavors
, &sec
.count
);
1201 /* [sigh] some implementations return "illegal" error for unsupported ops */
1202 if (error
== NFSERR_OP_ILLEGAL
)
1205 /* set our default security flavor to the first in the list */
1206 lck_mtx_lock(&nmp
->nm_lock
);
1208 nmp
->nm_auth
= sec
.flavors
[0];
1209 nmp
->nm_state
&= ~NFSSTA_NEEDSECINFO
;
1210 lck_mtx_unlock(&nmp
->nm_lock
);
1224 struct nfsmount
*nmp
;
1225 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1226 u_int64_t xid
, newwverf
;
1228 struct nfsm_chain nmreq
, nmrep
;
1229 struct nfsreq_secinfo_args si
;
1232 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
1235 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1237 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
1239 nfsvers
= nmp
->nm_vers
;
1241 if (count
> UINT32_MAX
)
1246 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1247 nfsm_chain_null(&nmreq
);
1248 nfsm_chain_null(&nmrep
);
1250 // PUTFH, COMMIT, GETATTR
1252 nfsm_chain_build_alloc_init(error
, &nmreq
, 19 * NFSX_UNSIGNED
);
1253 nfsm_chain_add_compound_header(error
, &nmreq
, "commit", numops
);
1255 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1256 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1258 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_COMMIT
);
1259 nfsm_chain_add_64(error
, &nmreq
, offset
);
1260 nfsm_chain_add_32(error
, &nmreq
, count32
);
1262 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1263 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
1264 nfsm_chain_build_done(error
, &nmreq
);
1265 nfsm_assert(error
, (numops
== 0), EPROTO
);
1267 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
1268 current_thread(), cred
, &si
, 0, &nmrep
, &xid
, &status
);
1270 if ((lockerror
= nfs_node_lock(np
)))
1272 nfsm_chain_skip_tag(error
, &nmrep
);
1273 nfsm_chain_get_32(error
, &nmrep
, numops
);
1274 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1275 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_COMMIT
);
1276 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
1277 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1278 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1280 nfs_node_unlock(np
);
1282 lck_mtx_lock(&nmp
->nm_lock
);
1283 if (nmp
->nm_verf
!= newwverf
)
1284 nmp
->nm_verf
= newwverf
;
1285 if (wverf
!= newwverf
)
1286 error
= NFSERR_STALEWRITEVERF
;
1287 lck_mtx_unlock(&nmp
->nm_lock
);
1289 nfsm_chain_cleanup(&nmreq
);
1290 nfsm_chain_cleanup(&nmrep
);
1297 struct nfs_fsattr
*nfsap
,
1301 int error
= 0, lockerror
, status
, nfsvers
, numops
;
1302 struct nfsm_chain nmreq
, nmrep
;
1303 struct nfsmount
*nmp
= NFSTONMP(np
);
1304 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
1305 struct nfs_vattr nvattr
;
1306 struct nfsreq_secinfo_args si
;
1310 nfsvers
= nmp
->nm_vers
;
1311 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1314 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1315 NVATTR_INIT(&nvattr
);
1316 nfsm_chain_null(&nmreq
);
1317 nfsm_chain_null(&nmrep
);
1319 /* NFSv4: fetch "pathconf" info for this node */
1322 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
1323 nfsm_chain_add_compound_header(error
, &nmreq
, "pathconf", numops
);
1325 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1326 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1328 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1329 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
1330 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXLINK
);
1331 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MAXNAME
);
1332 NFS_BITMAP_SET(bitmap
, NFS_FATTR_NO_TRUNC
);
1333 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
1334 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
1335 NFS_BITMAP_SET(bitmap
, NFS_FATTR_CASE_PRESERVING
);
1336 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
1337 nfsm_chain_build_done(error
, &nmreq
);
1338 nfsm_assert(error
, (numops
== 0), EPROTO
);
1340 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1342 nfsm_chain_skip_tag(error
, &nmrep
);
1343 nfsm_chain_get_32(error
, &nmrep
, numops
);
1344 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1345 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1347 error
= nfs4_parsefattr(&nmrep
, nfsap
, &nvattr
, NULL
, NULL
, NULL
);
1349 if ((lockerror
= nfs_node_lock(np
)))
1352 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
1354 nfs_node_unlock(np
);
1356 NVATTR_CLEANUP(&nvattr
);
1357 nfsm_chain_cleanup(&nmreq
);
1358 nfsm_chain_cleanup(&nmrep
);
1364 struct vnop_getattr_args
/* {
1365 struct vnodeop_desc *a_desc;
1367 struct vnode_attr *a_vap;
1368 vfs_context_t a_context;
1371 struct vnode_attr
*vap
= ap
->a_vap
;
1372 struct nfsmount
*nmp
;
1373 struct nfs_vattr nva
;
1374 int error
, acls
, ngaflags
;
1376 if (!(nmp
= VTONMP(ap
->a_vp
)))
1378 acls
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
);
1380 ngaflags
= NGA_CACHED
;
1381 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
)
1382 ngaflags
|= NGA_ACL
;
1383 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, ngaflags
);
1387 /* copy what we have in nva to *a_vap */
1388 if (VATTR_IS_ACTIVE(vap
, va_rdev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_RAWDEV
)) {
1389 dev_t rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1390 VATTR_RETURN(vap
, va_rdev
, rdev
);
1392 if (VATTR_IS_ACTIVE(vap
, va_nlink
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_NUMLINKS
))
1393 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1394 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SIZE
))
1395 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1396 // VATTR_RETURN(vap, va_data_alloc, ???);
1397 // VATTR_RETURN(vap, va_total_size, ???);
1398 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_SPACE_USED
))
1399 VATTR_RETURN(vap
, va_total_alloc
, nva
.nva_bytes
);
1400 if (VATTR_IS_ACTIVE(vap
, va_uid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1401 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1402 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER
))
1403 VATTR_RETURN(vap
, va_uuuid
, nva
.nva_uuuid
);
1404 if (VATTR_IS_ACTIVE(vap
, va_gid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1405 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1406 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_OWNER_GROUP
))
1407 VATTR_RETURN(vap
, va_guuid
, nva
.nva_guuid
);
1408 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1409 if (NMFLAG(nmp
, ACLONLY
) || !NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_MODE
))
1410 VATTR_RETURN(vap
, va_mode
, 0777);
1412 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1414 if (VATTR_IS_ACTIVE(vap
, va_flags
) &&
1415 (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) ||
1416 NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) ||
1417 (nva
.nva_flags
& NFS_FFLAG_TRIGGER
))) {
1419 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_ARCHIVE
) &&
1420 (nva
.nva_flags
& NFS_FFLAG_ARCHIVED
))
1421 flags
|= SF_ARCHIVED
;
1422 if (NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_HIDDEN
) &&
1423 (nva
.nva_flags
& NFS_FFLAG_HIDDEN
))
1425 VATTR_RETURN(vap
, va_flags
, flags
);
1427 if (VATTR_IS_ACTIVE(vap
, va_create_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_CREATE
)) {
1428 vap
->va_create_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CREATE
];
1429 vap
->va_create_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CREATE
];
1430 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1432 if (VATTR_IS_ACTIVE(vap
, va_access_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_ACCESS
)) {
1433 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1434 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1435 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1437 if (VATTR_IS_ACTIVE(vap
, va_modify_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_MODIFY
)) {
1438 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1439 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1440 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1442 if (VATTR_IS_ACTIVE(vap
, va_change_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_METADATA
)) {
1443 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1444 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1445 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1447 if (VATTR_IS_ACTIVE(vap
, va_backup_time
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TIME_BACKUP
)) {
1448 vap
->va_backup_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_BACKUP
];
1449 vap
->va_backup_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_BACKUP
];
1450 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1452 if (VATTR_IS_ACTIVE(vap
, va_fileid
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_FILEID
))
1453 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1454 if (VATTR_IS_ACTIVE(vap
, va_type
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_TYPE
))
1455 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1456 if (VATTR_IS_ACTIVE(vap
, va_filerev
) && NFS_BITMAP_ISSET(nva
.nva_bitmap
, NFS_FATTR_CHANGE
))
1457 VATTR_RETURN(vap
, va_filerev
, nva
.nva_change
);
1459 if (VATTR_IS_ACTIVE(vap
, va_acl
) && acls
) {
1460 VATTR_RETURN(vap
, va_acl
, nva
.nva_acl
);
1464 // other attrs we might support someday:
1465 // VATTR_RETURN(vap, va_encoding, ??? /* potentially unnormalized UTF-8? */);
1467 NVATTR_CLEANUP(&nva
);
1474 struct vnode_attr
*vap
,
1477 struct nfsmount
*nmp
= NFSTONMP(np
);
1478 int error
= 0, setattr_error
= 0, lockerror
= ENOENT
, status
, nfsvers
, numops
;
1479 u_int64_t xid
, nextxid
;
1480 struct nfsm_chain nmreq
, nmrep
;
1481 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
1482 uint32_t getbitmap
[NFS_ATTR_BITMAP_LEN
];
1483 uint32_t setbitmap
[NFS_ATTR_BITMAP_LEN
];
1484 nfs_stateid stateid
;
1485 struct nfsreq_secinfo_args si
;
1489 nfsvers
= nmp
->nm_vers
;
1490 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
1493 if (VATTR_IS_ACTIVE(vap
, va_flags
) && (vap
->va_flags
& ~(SF_ARCHIVED
|UF_HIDDEN
))) {
1494 /* we don't support setting unsupported flags (duh!) */
1495 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
1496 return (EINVAL
); /* return EINVAL if other attributes also set */
1498 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
1501 /* don't bother requesting some changes if they don't look like they are changing */
1502 if (VATTR_IS_ACTIVE(vap
, va_uid
) && (vap
->va_uid
== np
->n_vattr
.nva_uid
))
1503 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1504 if (VATTR_IS_ACTIVE(vap
, va_gid
) && (vap
->va_gid
== np
->n_vattr
.nva_gid
))
1505 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1506 if (VATTR_IS_ACTIVE(vap
, va_uuuid
) && kauth_guid_equal(&vap
->va_uuuid
, &np
->n_vattr
.nva_uuuid
))
1507 VATTR_CLEAR_ACTIVE(vap
, va_uuuid
);
1508 if (VATTR_IS_ACTIVE(vap
, va_guuid
) && kauth_guid_equal(&vap
->va_guuid
, &np
->n_vattr
.nva_guuid
))
1509 VATTR_CLEAR_ACTIVE(vap
, va_guuid
);
1512 /* do nothing if no attributes will be sent */
1513 nfs_vattr_set_bitmap(nmp
, bitmap
, vap
);
1514 if (!bitmap
[0] && !bitmap
[1])
1517 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
1518 nfsm_chain_null(&nmreq
);
1519 nfsm_chain_null(&nmrep
);
1522 * Prepare GETATTR bitmap: if we are setting the ACL or mode, we
1523 * need to invalidate any cached ACL. And if we had an ACL cached,
1524 * we might as well also fetch the new value.
1526 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, getbitmap
);
1527 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
) ||
1528 NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
1530 NFS_BITMAP_SET(getbitmap
, NFS_FATTR_ACL
);
1534 // PUTFH, SETATTR, GETATTR
1536 nfsm_chain_build_alloc_init(error
, &nmreq
, 40 * NFSX_UNSIGNED
);
1537 nfsm_chain_add_compound_header(error
, &nmreq
, "setattr", numops
);
1539 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
1540 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1542 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SETATTR
);
1543 if (VATTR_IS_ACTIVE(vap
, va_data_size
))
1544 nfs_get_stateid(np
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &stateid
);
1546 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
1547 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
1548 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
1550 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
1551 nfsm_chain_add_bitmap_supported(error
, &nmreq
, getbitmap
, nmp
, np
);
1552 nfsm_chain_build_done(error
, &nmreq
);
1553 nfsm_assert(error
, (numops
== 0), EPROTO
);
1555 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
1557 if ((lockerror
= nfs_node_lock(np
)))
1559 nfsm_chain_skip_tag(error
, &nmrep
);
1560 nfsm_chain_get_32(error
, &nmrep
, numops
);
1561 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
1563 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SETATTR
);
1564 nfsmout_if(error
== EBADRPC
);
1565 setattr_error
= error
;
1567 bmlen
= NFS_ATTR_BITMAP_LEN
;
1568 nfsm_chain_get_bitmap(error
, &nmrep
, setbitmap
, bmlen
);
1570 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && (np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
1571 microuptime(&np
->n_lastio
);
1572 nfs_vattr_set_supported(setbitmap
, vap
);
1573 error
= setattr_error
;
1575 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
1576 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1578 NATTRINVALIDATE(np
);
1580 * We just changed the attributes and we want to make sure that we
1581 * see the latest attributes. Get the next XID. If it's not the
1582 * next XID after the SETATTR XID, then it's possible that another
1583 * RPC was in flight at the same time and it might put stale attributes
1584 * in the cache. In that case, we invalidate the attributes and set
1585 * the attribute cache XID to guarantee that newer attributes will
1589 nfs_get_xid(&nextxid
);
1590 if (nextxid
!= (xid
+ 1)) {
1591 np
->n_xid
= nextxid
;
1592 NATTRINVALIDATE(np
);
1596 nfs_node_unlock(np
);
1597 nfsm_chain_cleanup(&nmreq
);
1598 nfsm_chain_cleanup(&nmrep
);
1599 if ((setattr_error
== EINVAL
) && VATTR_IS_ACTIVE(vap
, va_acl
) && VATTR_IS_ACTIVE(vap
, va_mode
) && !NMFLAG(nmp
, ACLONLY
)) {
1601 * Some server's may not like ACL/mode combos that get sent.
1602 * If it looks like that's what the server choked on, try setting
1603 * just the ACL and not the mode (unless it looks like everything
1604 * but mode was already successfully set).
1606 if (((bitmap
[0] & setbitmap
[0]) != bitmap
[0]) ||
1607 ((bitmap
[1] & (setbitmap
[1]|NFS_FATTR_MODE
)) != bitmap
[1])) {
1608 VATTR_CLEAR_ACTIVE(vap
, va_mode
);
1617 * Wait for any pending recovery to complete.
1620 nfs_mount_state_wait_for_recovery(struct nfsmount
*nmp
)
1622 struct timespec ts
= { 1, 0 };
1623 int error
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
1625 lck_mtx_lock(&nmp
->nm_lock
);
1626 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1627 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 1)))
1629 nfs_mount_sock_thread_wake(nmp
);
1630 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1633 lck_mtx_unlock(&nmp
->nm_lock
);
1639 * We're about to use/manipulate NFS mount's open/lock state.
1640 * Wait for any pending state recovery to complete, then
1641 * mark the state as being in use (which will hold off
1642 * the recovery thread until we're done).
1645 nfs_mount_state_in_use_start(struct nfsmount
*nmp
, thread_t thd
)
1647 struct timespec ts
= { 1, 0 };
1648 int error
= 0, slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1652 lck_mtx_lock(&nmp
->nm_lock
);
1653 if (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) {
1654 lck_mtx_unlock(&nmp
->nm_lock
);
1657 while (nmp
->nm_state
& NFSSTA_RECOVER
) {
1658 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 1)))
1660 nfs_mount_sock_thread_wake(nmp
);
1661 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, slpflag
|(PZERO
-1), "nfsrecoverwait", &ts
);
1665 nmp
->nm_stateinuse
++;
1666 lck_mtx_unlock(&nmp
->nm_lock
);
1672 * We're done using/manipulating the NFS mount's open/lock
1673 * state. If the given error indicates that recovery should
1674 * be performed, we'll initiate recovery.
1677 nfs_mount_state_in_use_end(struct nfsmount
*nmp
, int error
)
1679 int restart
= nfs_mount_state_error_should_restart(error
);
1683 lck_mtx_lock(&nmp
->nm_lock
);
1684 if (restart
&& (error
!= NFSERR_OLD_STATEID
) && (error
!= NFSERR_GRACE
)) {
1685 printf("nfs_mount_state_in_use_end: error %d, initiating recovery for %s, 0x%x\n",
1686 error
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
1687 nfs_need_recover(nmp
, error
);
1689 if (nmp
->nm_stateinuse
> 0)
1690 nmp
->nm_stateinuse
--;
1692 panic("NFS mount state in use count underrun");
1693 if (!nmp
->nm_stateinuse
&& (nmp
->nm_state
& NFSSTA_RECOVER
))
1694 wakeup(&nmp
->nm_stateinuse
);
1695 lck_mtx_unlock(&nmp
->nm_lock
);
1696 if (error
== NFSERR_GRACE
)
1697 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
1703 * Does the error mean we should restart/redo a state-related operation?
1706 nfs_mount_state_error_should_restart(int error
)
1709 case NFSERR_STALE_STATEID
:
1710 case NFSERR_STALE_CLIENTID
:
1711 case NFSERR_ADMIN_REVOKED
:
1712 case NFSERR_EXPIRED
:
1713 case NFSERR_OLD_STATEID
:
1714 case NFSERR_BAD_STATEID
:
1722 * In some cases we may want to limit how many times we restart a
1723 * state-related operation - e.g. we're repeatedly getting NFSERR_GRACE.
1724 * Base the limit on the lease (as long as it's not too short).
1727 nfs_mount_state_max_restarts(struct nfsmount
*nmp
)
1729 return (MAX(nmp
->nm_fsattr
.nfsa_lease
, 60));
1733 * Does the error mean we probably lost a delegation?
1736 nfs_mount_state_error_delegation_lost(int error
)
1739 case NFSERR_STALE_STATEID
:
1740 case NFSERR_ADMIN_REVOKED
:
1741 case NFSERR_EXPIRED
:
1742 case NFSERR_OLD_STATEID
:
1743 case NFSERR_BAD_STATEID
:
1744 case NFSERR_GRACE
: /* ugh! (stupid) RFC 3530 specifically disallows CLAIM_DELEGATE_CUR during grace period? */
1752 * Mark an NFS node's open state as busy.
1755 nfs_open_state_set_busy(nfsnode_t np
, thread_t thd
)
1757 struct nfsmount
*nmp
;
1758 struct timespec ts
= {2, 0};
1759 int error
= 0, slpflag
;
1764 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1766 lck_mtx_lock(&np
->n_openlock
);
1767 while (np
->n_openflags
& N_OPENBUSY
) {
1768 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1770 np
->n_openflags
|= N_OPENWANT
;
1771 msleep(&np
->n_openflags
, &np
->n_openlock
, slpflag
, "nfs_open_state_set_busy", &ts
);
1775 np
->n_openflags
|= N_OPENBUSY
;
1776 lck_mtx_unlock(&np
->n_openlock
);
1782 * Clear an NFS node's open state busy flag and wake up
1783 * anyone wanting it.
1786 nfs_open_state_clear_busy(nfsnode_t np
)
1790 lck_mtx_lock(&np
->n_openlock
);
1791 if (!(np
->n_openflags
& N_OPENBUSY
))
1792 panic("nfs_open_state_clear_busy");
1793 wanted
= (np
->n_openflags
& N_OPENWANT
);
1794 np
->n_openflags
&= ~(N_OPENBUSY
|N_OPENWANT
);
1795 lck_mtx_unlock(&np
->n_openlock
);
1797 wakeup(&np
->n_openflags
);
1801 * Search a mount's open owner list for the owner for this credential.
1802 * If not found and "alloc" is set, then allocate a new one.
1804 struct nfs_open_owner
*
1805 nfs_open_owner_find(struct nfsmount
*nmp
, kauth_cred_t cred
, int alloc
)
1807 uid_t uid
= kauth_cred_getuid(cred
);
1808 struct nfs_open_owner
*noop
, *newnoop
= NULL
;
1811 lck_mtx_lock(&nmp
->nm_lock
);
1812 TAILQ_FOREACH(noop
, &nmp
->nm_open_owners
, noo_link
) {
1813 if (kauth_cred_getuid(noop
->noo_cred
) == uid
)
1817 if (!noop
&& !newnoop
&& alloc
) {
1818 lck_mtx_unlock(&nmp
->nm_lock
);
1819 MALLOC(newnoop
, struct nfs_open_owner
*, sizeof(struct nfs_open_owner
), M_TEMP
, M_WAITOK
);
1822 bzero(newnoop
, sizeof(*newnoop
));
1823 lck_mtx_init(&newnoop
->noo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
1824 newnoop
->noo_mount
= nmp
;
1825 kauth_cred_ref(cred
);
1826 newnoop
->noo_cred
= cred
;
1827 newnoop
->noo_name
= OSAddAtomic(1, &nfs_open_owner_seqnum
);
1828 TAILQ_INIT(&newnoop
->noo_opens
);
1831 if (!noop
&& newnoop
) {
1832 newnoop
->noo_flags
|= NFS_OPEN_OWNER_LINK
;
1833 TAILQ_INSERT_HEAD(&nmp
->nm_open_owners
, newnoop
, noo_link
);
1836 lck_mtx_unlock(&nmp
->nm_lock
);
1838 if (newnoop
&& (noop
!= newnoop
))
1839 nfs_open_owner_destroy(newnoop
);
1842 nfs_open_owner_ref(noop
);
1848 * destroy an open owner that's no longer needed
1851 nfs_open_owner_destroy(struct nfs_open_owner
*noop
)
1854 kauth_cred_unref(&noop
->noo_cred
);
1855 lck_mtx_destroy(&noop
->noo_lock
, nfs_open_grp
);
1860 * acquire a reference count on an open owner
1863 nfs_open_owner_ref(struct nfs_open_owner
*noop
)
1865 lck_mtx_lock(&noop
->noo_lock
);
1867 lck_mtx_unlock(&noop
->noo_lock
);
1871 * drop a reference count on an open owner and destroy it if
1872 * it is no longer referenced and no longer on the mount's list.
1875 nfs_open_owner_rele(struct nfs_open_owner
*noop
)
1877 lck_mtx_lock(&noop
->noo_lock
);
1878 if (noop
->noo_refcnt
< 1)
1879 panic("nfs_open_owner_rele: no refcnt");
1881 if (!noop
->noo_refcnt
&& (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1882 panic("nfs_open_owner_rele: busy");
1883 /* XXX we may potentially want to clean up idle/unused open owner structures */
1884 if (noop
->noo_refcnt
|| (noop
->noo_flags
& NFS_OPEN_OWNER_LINK
)) {
1885 lck_mtx_unlock(&noop
->noo_lock
);
1888 /* owner is no longer referenced or linked to mount, so destroy it */
1889 lck_mtx_unlock(&noop
->noo_lock
);
1890 nfs_open_owner_destroy(noop
);
1894 * Mark an open owner as busy because we are about to
1895 * start an operation that uses and updates open owner state.
1898 nfs_open_owner_set_busy(struct nfs_open_owner
*noop
, thread_t thd
)
1900 struct nfsmount
*nmp
;
1901 struct timespec ts
= {2, 0};
1902 int error
= 0, slpflag
;
1904 nmp
= noop
->noo_mount
;
1907 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
1909 lck_mtx_lock(&noop
->noo_lock
);
1910 while (noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
) {
1911 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1913 noop
->noo_flags
|= NFS_OPEN_OWNER_WANT
;
1914 msleep(noop
, &noop
->noo_lock
, slpflag
, "nfs_open_owner_set_busy", &ts
);
1918 noop
->noo_flags
|= NFS_OPEN_OWNER_BUSY
;
1919 lck_mtx_unlock(&noop
->noo_lock
);
1925 * Clear the busy flag on an open owner and wake up anyone waiting
1929 nfs_open_owner_clear_busy(struct nfs_open_owner
*noop
)
1933 lck_mtx_lock(&noop
->noo_lock
);
1934 if (!(noop
->noo_flags
& NFS_OPEN_OWNER_BUSY
))
1935 panic("nfs_open_owner_clear_busy");
1936 wanted
= (noop
->noo_flags
& NFS_OPEN_OWNER_WANT
);
1937 noop
->noo_flags
&= ~(NFS_OPEN_OWNER_BUSY
|NFS_OPEN_OWNER_WANT
);
1938 lck_mtx_unlock(&noop
->noo_lock
);
1944 * Given an open/lock owner and an error code, increment the
1945 * sequence ID if appropriate.
1948 nfs_owner_seqid_increment(struct nfs_open_owner
*noop
, struct nfs_lock_owner
*nlop
, int error
)
1951 case NFSERR_STALE_CLIENTID
:
1952 case NFSERR_STALE_STATEID
:
1953 case NFSERR_OLD_STATEID
:
1954 case NFSERR_BAD_STATEID
:
1955 case NFSERR_BAD_SEQID
:
1957 case NFSERR_RESOURCE
:
1958 case NFSERR_NOFILEHANDLE
:
1959 /* do not increment the open seqid on these errors */
1969 * Search a node's open file list for any conflicts with this request.
1970 * Also find this open owner's open file structure.
1971 * If not found and "alloc" is set, then allocate one.
1976 struct nfs_open_owner
*noop
,
1977 struct nfs_open_file
**nofpp
,
1978 uint32_t accessMode
,
1983 return nfs_open_file_find_internal(np
, noop
, nofpp
, accessMode
, denyMode
, alloc
);
1987 * Internally, allow using a provisional nodeless nofp (passed in via *nofpp)
1988 * if an existing one is not found. This is used in "create" scenarios to
1989 * officially add the provisional nofp to the node once the node is created.
1992 nfs_open_file_find_internal(
1994 struct nfs_open_owner
*noop
,
1995 struct nfs_open_file
**nofpp
,
1996 uint32_t accessMode
,
2000 struct nfs_open_file
*nofp
= NULL
, *nofp2
, *newnofp
= NULL
;
2005 lck_mtx_lock(&np
->n_openlock
);
2006 TAILQ_FOREACH(nofp2
, &np
->n_opens
, nof_link
) {
2007 if (nofp2
->nof_owner
== noop
) {
2012 if ((accessMode
& nofp2
->nof_deny
) || (denyMode
& nofp2
->nof_access
)) {
2013 /* This request conflicts with an existing open on this client. */
2014 lck_mtx_unlock(&np
->n_openlock
);
2020 * If this open owner doesn't have an open
2021 * file structure yet, we create one for it.
2023 if (!nofp
&& !*nofpp
&& !newnofp
&& alloc
) {
2024 lck_mtx_unlock(&np
->n_openlock
);
2026 MALLOC(newnofp
, struct nfs_open_file
*, sizeof(struct nfs_open_file
), M_TEMP
, M_WAITOK
);
2029 bzero(newnofp
, sizeof(*newnofp
));
2030 lck_mtx_init(&newnofp
->nof_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
2031 newnofp
->nof_owner
= noop
;
2032 nfs_open_owner_ref(noop
);
2033 newnofp
->nof_np
= np
;
2034 lck_mtx_lock(&noop
->noo_lock
);
2035 TAILQ_INSERT_HEAD(&noop
->noo_opens
, newnofp
, nof_oolink
);
2036 lck_mtx_unlock(&noop
->noo_lock
);
2042 (*nofpp
)->nof_np
= np
;
2048 TAILQ_INSERT_HEAD(&np
->n_opens
, nofp
, nof_link
);
2051 lck_mtx_unlock(&np
->n_openlock
);
2053 if (alloc
&& newnofp
&& (nofp
!= newnofp
))
2054 nfs_open_file_destroy(newnofp
);
2057 return (nofp
? 0 : ESRCH
);
2061 * Destroy an open file structure.
2064 nfs_open_file_destroy(struct nfs_open_file
*nofp
)
2066 lck_mtx_lock(&nofp
->nof_owner
->noo_lock
);
2067 TAILQ_REMOVE(&nofp
->nof_owner
->noo_opens
, nofp
, nof_oolink
);
2068 lck_mtx_unlock(&nofp
->nof_owner
->noo_lock
);
2069 nfs_open_owner_rele(nofp
->nof_owner
);
2070 lck_mtx_destroy(&nofp
->nof_lock
, nfs_open_grp
);
2075 * Mark an open file as busy because we are about to
2076 * start an operation that uses and updates open file state.
2079 nfs_open_file_set_busy(struct nfs_open_file
*nofp
, thread_t thd
)
2081 struct nfsmount
*nmp
;
2082 struct timespec ts
= {2, 0};
2083 int error
= 0, slpflag
;
2085 nmp
= nofp
->nof_owner
->noo_mount
;
2088 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
2090 lck_mtx_lock(&nofp
->nof_lock
);
2091 while (nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
) {
2092 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
2094 nofp
->nof_flags
|= NFS_OPEN_FILE_WANT
;
2095 msleep(nofp
, &nofp
->nof_lock
, slpflag
, "nfs_open_file_set_busy", &ts
);
2099 nofp
->nof_flags
|= NFS_OPEN_FILE_BUSY
;
2100 lck_mtx_unlock(&nofp
->nof_lock
);
2106 * Clear the busy flag on an open file and wake up anyone waiting
2110 nfs_open_file_clear_busy(struct nfs_open_file
*nofp
)
2114 lck_mtx_lock(&nofp
->nof_lock
);
2115 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
))
2116 panic("nfs_open_file_clear_busy");
2117 wanted
= (nofp
->nof_flags
& NFS_OPEN_FILE_WANT
);
2118 nofp
->nof_flags
&= ~(NFS_OPEN_FILE_BUSY
|NFS_OPEN_FILE_WANT
);
2119 lck_mtx_unlock(&nofp
->nof_lock
);
2125 * Add the open state for the given access/deny modes to this open file.
2128 nfs_open_file_add_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
, int delegated
)
2130 lck_mtx_lock(&nofp
->nof_lock
);
2131 nofp
->nof_access
|= accessMode
;
2132 nofp
->nof_deny
|= denyMode
;
2135 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2136 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2138 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2140 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2142 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2143 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2145 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2147 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2148 nofp
->nof_d_rw_dw
++;
2149 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2150 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2151 nofp
->nof_d_r_drw
++;
2152 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2153 nofp
->nof_d_w_drw
++;
2154 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2155 nofp
->nof_d_rw_drw
++;
2158 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2159 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2161 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2163 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2165 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2166 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2168 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2170 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2172 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2173 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2175 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2177 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2182 nofp
->nof_opencnt
++;
2183 lck_mtx_unlock(&nofp
->nof_lock
);
2187 * Find which particular open combo will be closed and report what
2188 * the new modes will be and whether the open was delegated.
2191 nfs_open_file_remove_open_find(
2192 struct nfs_open_file
*nofp
,
2193 uint32_t accessMode
,
2195 uint32_t *newAccessMode
,
2196 uint32_t *newDenyMode
,
2200 * Calculate new modes: a mode bit gets removed when there's only
2201 * one count in all the corresponding counts
2203 *newAccessMode
= nofp
->nof_access
;
2204 *newDenyMode
= nofp
->nof_deny
;
2206 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2207 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_READ
) &&
2208 ((nofp
->nof_r
+ nofp
->nof_d_r
+
2209 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2210 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2211 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2212 nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2213 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2214 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2215 if ((accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2216 (nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
) &&
2217 ((nofp
->nof_w
+ nofp
->nof_d_w
+
2218 nofp
->nof_rw
+ nofp
->nof_d_rw
+
2219 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2220 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
+
2221 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2222 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2223 *newAccessMode
&= ~NFS_OPEN_SHARE_ACCESS_WRITE
;
2224 if ((denyMode
& NFS_OPEN_SHARE_DENY_READ
) &&
2225 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) &&
2226 ((nofp
->nof_r_drw
+ nofp
->nof_d_r_drw
+
2227 nofp
->nof_w_drw
+ nofp
->nof_d_w_drw
+
2228 nofp
->nof_rw_drw
+ nofp
->nof_d_rw_drw
) == 1))
2229 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_READ
;
2230 if ((denyMode
& NFS_OPEN_SHARE_DENY_WRITE
) &&
2231 (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
) &&
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
+
2235 nofp
->nof_r_dw
+ nofp
->nof_d_r_dw
+
2236 nofp
->nof_w_dw
+ nofp
->nof_d_w_dw
+
2237 nofp
->nof_rw_dw
+ nofp
->nof_d_rw_dw
) == 1))
2238 *newDenyMode
&= ~NFS_OPEN_SHARE_DENY_WRITE
;
2240 /* Find the corresponding open access/deny mode counter. */
2241 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2242 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2243 *delegated
= (nofp
->nof_d_r
!= 0);
2244 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2245 *delegated
= (nofp
->nof_d_w
!= 0);
2246 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2247 *delegated
= (nofp
->nof_d_rw
!= 0);
2250 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2251 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2252 *delegated
= (nofp
->nof_d_r_dw
!= 0);
2253 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2254 *delegated
= (nofp
->nof_d_w_dw
!= 0);
2255 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2256 *delegated
= (nofp
->nof_d_rw_dw
!= 0);
2259 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2260 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
)
2261 *delegated
= (nofp
->nof_d_r_drw
!= 0);
2262 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
)
2263 *delegated
= (nofp
->nof_d_w_drw
!= 0);
2264 else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
)
2265 *delegated
= (nofp
->nof_d_rw_drw
!= 0);
2272 * Remove the open state for the given access/deny modes to this open file.
2275 nfs_open_file_remove_open(struct nfs_open_file
*nofp
, uint32_t accessMode
, uint32_t denyMode
)
2277 uint32_t newAccessMode
, newDenyMode
;
2280 lck_mtx_lock(&nofp
->nof_lock
);
2281 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
2283 /* Decrement the corresponding open access/deny mode counter. */
2284 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
2285 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2287 if (nofp
->nof_d_r
== 0)
2288 NP(nofp
->nof_np
, "nfs: open(R) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2292 if (nofp
->nof_r
== 0)
2293 NP(nofp
->nof_np
, "nfs: open(R) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2297 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2299 if (nofp
->nof_d_w
== 0)
2300 NP(nofp
->nof_np
, "nfs: open(W) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2304 if (nofp
->nof_w
== 0)
2305 NP(nofp
->nof_np
, "nfs: open(W) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2309 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2311 if (nofp
->nof_d_rw
== 0)
2312 NP(nofp
->nof_np
, "nfs: open(RW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2316 if (nofp
->nof_rw
== 0)
2317 NP(nofp
->nof_np
, "nfs: open(RW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2322 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
2323 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2325 if (nofp
->nof_d_r_dw
== 0)
2326 NP(nofp
->nof_np
, "nfs: open(R,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2330 if (nofp
->nof_r_dw
== 0)
2331 NP(nofp
->nof_np
, "nfs: open(R,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2335 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2337 if (nofp
->nof_d_w_dw
== 0)
2338 NP(nofp
->nof_np
, "nfs: open(W,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2342 if (nofp
->nof_w_dw
== 0)
2343 NP(nofp
->nof_np
, "nfs: open(W,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2347 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2349 if (nofp
->nof_d_rw_dw
== 0)
2350 NP(nofp
->nof_np
, "nfs: open(RW,DW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2352 nofp
->nof_d_rw_dw
--;
2354 if (nofp
->nof_rw_dw
== 0)
2355 NP(nofp
->nof_np
, "nfs: open(RW,DW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2360 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
2361 if (accessMode
== NFS_OPEN_SHARE_ACCESS_READ
) {
2363 if (nofp
->nof_d_r_drw
== 0)
2364 NP(nofp
->nof_np
, "nfs: open(R,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2366 nofp
->nof_d_r_drw
--;
2368 if (nofp
->nof_r_drw
== 0)
2369 NP(nofp
->nof_np
, "nfs: open(R,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2373 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) {
2375 if (nofp
->nof_d_w_drw
== 0)
2376 NP(nofp
->nof_np
, "nfs: open(W,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2378 nofp
->nof_d_w_drw
--;
2380 if (nofp
->nof_w_drw
== 0)
2381 NP(nofp
->nof_np
, "nfs: open(W,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2385 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2387 if (nofp
->nof_d_rw_drw
== 0)
2388 NP(nofp
->nof_np
, "nfs: open(RW,DRW) delegated count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2390 nofp
->nof_d_rw_drw
--;
2392 if (nofp
->nof_rw_drw
== 0)
2393 NP(nofp
->nof_np
, "nfs: open(RW,DRW) count underrun, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2400 /* update the modes */
2401 nofp
->nof_access
= newAccessMode
;
2402 nofp
->nof_deny
= newDenyMode
;
2403 nofp
->nof_opencnt
--;
2404 lck_mtx_unlock(&nofp
->nof_lock
);
2409 * Get the current (delegation, lock, open, default) stateid for this node.
2410 * If node has a delegation, use that stateid.
2411 * If pid has a lock, use the lockowner's stateid.
2412 * Or use the open file's stateid.
2413 * If no open file, use a default stateid of all ones.
2416 nfs_get_stateid(nfsnode_t np
, thread_t thd
, kauth_cred_t cred
, nfs_stateid
*sid
)
2418 struct nfsmount
*nmp
= NFSTONMP(np
);
2419 proc_t p
= thd
? get_bsdthreadtask_info(thd
) : current_proc(); // XXX async I/O requests don't have a thread
2420 struct nfs_open_owner
*noop
= NULL
;
2421 struct nfs_open_file
*nofp
= NULL
;
2422 struct nfs_lock_owner
*nlop
= NULL
;
2423 nfs_stateid
*s
= NULL
;
2425 if (np
->n_openflags
& N_DELEG_MASK
) {
2426 s
= &np
->n_dstateid
;
2429 nlop
= nfs_lock_owner_find(np
, p
, 0);
2430 if (nlop
&& !TAILQ_EMPTY(&nlop
->nlo_locks
)) {
2431 /* we hold locks, use lock stateid */
2432 s
= &nlop
->nlo_stateid
;
2433 } else if (((noop
= nfs_open_owner_find(nmp
, cred
, 0))) &&
2434 (nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0) == 0) &&
2435 !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) &&
2437 /* we (should) have the file open, use open stateid */
2438 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)
2439 nfs4_reopen(nofp
, thd
);
2440 if (!(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
2441 s
= &nofp
->nof_stateid
;
2446 sid
->seqid
= s
->seqid
;
2447 sid
->other
[0] = s
->other
[0];
2448 sid
->other
[1] = s
->other
[1];
2449 sid
->other
[2] = s
->other
[2];
2451 /* named attributes may not have a stateid for reads, so don't complain for them */
2452 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
2453 NP(np
, "nfs_get_stateid: no stateid");
2454 sid
->seqid
= sid
->other
[0] = sid
->other
[1] = sid
->other
[2] = 0xffffffff;
2457 nfs_lock_owner_rele(nlop
);
2459 nfs_open_owner_rele(noop
);
2464 * When we have a delegation, we may be able to perform the OPEN locally.
2465 * Perform the OPEN by checking the delegation ACE and/or checking via ACCESS.
2468 nfs4_open_delegated(
2470 struct nfs_open_file
*nofp
,
2471 uint32_t accessMode
,
2475 int error
= 0, ismember
, readtoo
= 0, authorized
= 0;
2477 struct kauth_acl_eval eval
;
2478 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2480 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2482 * Try to open it for read access too,
2483 * so the buffer cache can read data.
2486 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2491 if (accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)
2492 action
|= KAUTH_VNODE_READ_DATA
;
2493 if (accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
)
2494 action
|= KAUTH_VNODE_WRITE_DATA
;
2496 /* evaluate ACE (if we have one) */
2497 if (np
->n_dace
.ace_flags
) {
2498 eval
.ae_requested
= action
;
2499 eval
.ae_acl
= &np
->n_dace
;
2501 eval
.ae_options
= 0;
2502 if (np
->n_vattr
.nva_uid
== kauth_cred_getuid(cred
))
2503 eval
.ae_options
|= KAUTH_AEVAL_IS_OWNER
;
2504 error
= kauth_cred_ismember_gid(cred
, np
->n_vattr
.nva_gid
, &ismember
);
2505 if (!error
&& ismember
)
2506 eval
.ae_options
|= KAUTH_AEVAL_IN_GROUP
;
2508 eval
.ae_exp_gall
= KAUTH_VNODE_GENERIC_ALL_BITS
;
2509 eval
.ae_exp_gread
= KAUTH_VNODE_GENERIC_READ_BITS
;
2510 eval
.ae_exp_gwrite
= KAUTH_VNODE_GENERIC_WRITE_BITS
;
2511 eval
.ae_exp_gexec
= KAUTH_VNODE_GENERIC_EXECUTE_BITS
;
2513 error
= kauth_acl_evaluate(cred
, &eval
);
2515 if (!error
&& (eval
.ae_result
== KAUTH_RESULT_ALLOW
))
2520 /* need to ask the server via ACCESS */
2521 struct vnop_access_args naa
;
2522 naa
.a_desc
= &vnop_access_desc
;
2523 naa
.a_vp
= NFSTOV(np
);
2524 naa
.a_action
= action
;
2525 naa
.a_context
= ctx
;
2526 if (!(error
= nfs_vnop_access(&naa
)))
2532 /* try again without the extra read access */
2533 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2537 return (error
? error
: EACCES
);
2540 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 1);
2547 * Open a file with the given access/deny modes.
2549 * If we have a delegation, we may be able to handle the open locally.
2550 * Otherwise, we will always send the open RPC even if this open's mode is
2551 * a subset of all the existing opens. This makes sure that we will always
2552 * be able to do a downgrade to any of the open modes.
2554 * Note: local conflicts should have already been checked in nfs_open_file_find().
2559 struct nfs_open_file
*nofp
,
2560 uint32_t accessMode
,
2564 vnode_t vp
= NFSTOV(np
);
2566 struct componentname cn
;
2567 const char *vname
= NULL
;
2569 char smallname
[128];
2570 char *filename
= NULL
;
2571 int error
= 0, readtoo
= 0;
2574 * We can handle the OPEN ourselves if we have a delegation,
2575 * unless it's a read delegation and the open is asking for
2576 * either write access or deny read. We also don't bother to
2577 * use the delegation if it's being returned.
2579 if (np
->n_openflags
& N_DELEG_MASK
) {
2580 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
2582 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
) &&
2583 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ||
2584 (!(accessMode
& NFS_OPEN_SHARE_ACCESS_WRITE
) && !(denyMode
& NFS_OPEN_SHARE_DENY_READ
)))) {
2585 error
= nfs4_open_delegated(np
, nofp
, accessMode
, denyMode
, ctx
);
2586 nfs_open_state_clear_busy(np
);
2589 nfs_open_state_clear_busy(np
);
2593 * [sigh] We can't trust VFS to get the parent right for named
2594 * attribute nodes. (It likes to reparent the nodes after we've
2595 * created them.) Luckily we can probably get the right parent
2596 * from the n_parent we have stashed away.
2598 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
2599 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
2602 dvp
= vnode_getparent(vp
);
2603 vname
= vnode_getname(vp
);
2604 if (!dvp
|| !vname
) {
2609 filename
= &smallname
[0];
2610 namelen
= snprintf(filename
, sizeof(smallname
), "%s", vname
);
2611 if (namelen
>= sizeof(smallname
)) {
2612 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
2617 snprintf(filename
, namelen
+1, "%s", vname
);
2619 bzero(&cn
, sizeof(cn
));
2620 cn
.cn_nameptr
= filename
;
2621 cn
.cn_namelen
= namelen
;
2623 if (!(accessMode
& NFS_OPEN_SHARE_ACCESS_READ
)) {
2625 * Try to open it for read access too,
2626 * so the buffer cache can read data.
2629 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
2632 error
= nfs4_open_rpc(nofp
, ctx
, &cn
, NULL
, dvp
, &vp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
2634 if (!nfs_mount_state_error_should_restart(error
) &&
2635 (error
!= EINTR
) && (error
!= ERESTART
) && readtoo
) {
2636 /* try again without the extra read access */
2637 accessMode
&= ~NFS_OPEN_SHARE_ACCESS_READ
;
2643 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
2645 if (filename
&& (filename
!= &smallname
[0]))
2646 FREE(filename
, M_TEMP
);
2648 vnode_putname(vname
);
2656 struct vnop_mmap_args
/* {
2657 struct vnodeop_desc *a_desc;
2660 vfs_context_t a_context;
2663 vfs_context_t ctx
= ap
->a_context
;
2664 vnode_t vp
= ap
->a_vp
;
2665 nfsnode_t np
= VTONFS(vp
);
2666 int error
= 0, accessMode
, denyMode
, delegated
;
2667 struct nfsmount
*nmp
;
2668 struct nfs_open_owner
*noop
= NULL
;
2669 struct nfs_open_file
*nofp
= NULL
;
2675 if (!vnode_isreg(vp
) || !(ap
->a_fflags
& (PROT_READ
|PROT_WRITE
)))
2677 if (np
->n_flag
& NREVOKE
)
2681 * fflags contains some combination of: PROT_READ, PROT_WRITE
2682 * Since it's not possible to mmap() without having the file open for reading,
2683 * read access is always there (regardless if PROT_READ is not set).
2685 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
2686 if (ap
->a_fflags
& PROT_WRITE
)
2687 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
2688 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2690 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
2695 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2697 nfs_open_owner_rele(noop
);
2700 if (np
->n_flag
& NREVOKE
) {
2702 nfs_mount_state_in_use_end(nmp
, 0);
2703 nfs_open_owner_rele(noop
);
2707 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
2708 if (error
|| (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))) {
2709 NP(np
, "nfs_vnop_mmap: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
2712 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
2713 nfs_mount_state_in_use_end(nmp
, 0);
2714 error
= nfs4_reopen(nofp
, NULL
);
2720 error
= nfs_open_file_set_busy(nofp
, NULL
);
2727 * The open reference for mmap must mirror an existing open because
2728 * we may need to reclaim it after the file is closed.
2729 * So grab another open count matching the accessMode passed in.
2730 * If we already had an mmap open, prefer read/write without deny mode.
2731 * This means we may have to drop the current mmap open first.
2734 if (!nofp
->nof_access
) {
2735 if (accessMode
!= NFS_OPEN_SHARE_ACCESS_READ
) {
2736 /* not asking for just read access -> fail */
2740 /* we don't have the file open, so open it for read access */
2741 if (nmp
->nm_vers
< NFS_VER4
) {
2742 /* NFS v2/v3 opens are always allowed - so just add it. */
2743 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
2746 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2749 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
2754 /* determine deny mode for open */
2755 if (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) {
2756 if (nofp
->nof_d_rw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_d_rw_drw
) {
2759 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2760 else if (nofp
->nof_d_rw_dw
)
2761 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2762 else if (nofp
->nof_d_rw_drw
)
2763 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2764 } else if (nofp
->nof_rw
|| nofp
->nof_rw_dw
|| nofp
->nof_rw_drw
) {
2767 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2768 else if (nofp
->nof_rw_dw
)
2769 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2770 else if (nofp
->nof_rw_drw
)
2771 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2775 } else { /* NFS_OPEN_SHARE_ACCESS_READ */
2776 if (nofp
->nof_d_r
|| nofp
->nof_d_r_dw
|| nofp
->nof_d_r_drw
) {
2779 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2780 else if (nofp
->nof_d_r_dw
)
2781 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2782 else if (nofp
->nof_d_r_drw
)
2783 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2784 } else if (nofp
->nof_r
|| nofp
->nof_r_dw
|| nofp
->nof_r_drw
) {
2787 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
2788 else if (nofp
->nof_r_dw
)
2789 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
2790 else if (nofp
->nof_r_drw
)
2791 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
2796 if (error
) /* mmap mode without proper open mode */
2800 * If the existing mmap access is more than the new access OR the
2801 * existing access is the same and the existing deny mode is less,
2802 * then we'll stick with the existing mmap open mode.
2804 if ((nofp
->nof_mmap_access
> accessMode
) ||
2805 ((nofp
->nof_mmap_access
== accessMode
) && (nofp
->nof_mmap_deny
<= denyMode
)))
2808 /* update mmap open mode */
2809 if (nofp
->nof_mmap_access
) {
2810 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2812 if (!nfs_mount_state_error_should_restart(error
))
2813 NP(np
, "nfs_vnop_mmap: close of previous mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2814 NP(np
, "nfs_vnop_mmap: update, close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2817 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2820 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
2821 nofp
->nof_mmap_access
= accessMode
;
2822 nofp
->nof_mmap_deny
= denyMode
;
2826 nfs_open_file_clear_busy(nofp
);
2827 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2832 nfs_open_owner_rele(noop
);
2839 struct vnop_mnomap_args
/* {
2840 struct vnodeop_desc *a_desc;
2842 vfs_context_t a_context;
2845 vfs_context_t ctx
= ap
->a_context
;
2846 vnode_t vp
= ap
->a_vp
;
2847 nfsnode_t np
= VTONFS(vp
);
2848 struct nfsmount
*nmp
;
2849 struct nfs_open_file
*nofp
= NULL
;
2857 /* flush buffers/ubc before we drop the open (in case it's our last open) */
2858 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
2859 if (UBCINFOEXISTS(vp
) && (size
= ubc_getsize(vp
)))
2860 ubc_msync(vp
, 0, size
, NULL
, UBC_PUSHALL
| UBC_SYNC
);
2862 /* walk all open files and close all mmap opens */
2864 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
2867 lck_mtx_lock(&np
->n_openlock
);
2868 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
2869 if (!nofp
->nof_mmap_access
)
2871 lck_mtx_unlock(&np
->n_openlock
);
2872 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
2873 nfs_mount_state_in_use_end(nmp
, 0);
2874 error
= nfs4_reopen(nofp
, NULL
);
2879 error
= nfs_open_file_set_busy(nofp
, NULL
);
2881 lck_mtx_lock(&np
->n_openlock
);
2884 if (nofp
->nof_mmap_access
) {
2885 error
= nfs_close(np
, nofp
, nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
, ctx
);
2886 if (!nfs_mount_state_error_should_restart(error
)) {
2887 if (error
) /* not a state-operation-restarting error, so just clear the access */
2888 NP(np
, "nfs_vnop_mnomap: close of mmap mode failed: %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2889 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
2892 NP(np
, "nfs_vnop_mnomap: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
2894 nfs_open_file_clear_busy(nofp
);
2895 nfs_mount_state_in_use_end(nmp
, error
);
2898 lck_mtx_unlock(&np
->n_openlock
);
2899 nfs_mount_state_in_use_end(nmp
, error
);
2904 * Search a node's lock owner list for the owner for this process.
2905 * If not found and "alloc" is set, then allocate a new one.
2907 struct nfs_lock_owner
*
2908 nfs_lock_owner_find(nfsnode_t np
, proc_t p
, int alloc
)
2910 pid_t pid
= proc_pid(p
);
2911 struct nfs_lock_owner
*nlop
, *newnlop
= NULL
;
2914 lck_mtx_lock(&np
->n_openlock
);
2915 TAILQ_FOREACH(nlop
, &np
->n_lock_owners
, nlo_link
) {
2916 if (nlop
->nlo_pid
!= pid
)
2918 if (timevalcmp(&nlop
->nlo_pid_start
, &p
->p_start
, ==))
2920 /* stale lock owner... reuse it if we can */
2921 if (nlop
->nlo_refcnt
) {
2922 TAILQ_REMOVE(&np
->n_lock_owners
, nlop
, nlo_link
);
2923 nlop
->nlo_flags
&= ~NFS_LOCK_OWNER_LINK
;
2924 lck_mtx_unlock(&np
->n_openlock
);
2927 nlop
->nlo_pid_start
= p
->p_start
;
2928 nlop
->nlo_seqid
= 0;
2929 nlop
->nlo_stategenid
= 0;
2933 if (!nlop
&& !newnlop
&& alloc
) {
2934 lck_mtx_unlock(&np
->n_openlock
);
2935 MALLOC(newnlop
, struct nfs_lock_owner
*, sizeof(struct nfs_lock_owner
), M_TEMP
, M_WAITOK
);
2938 bzero(newnlop
, sizeof(*newnlop
));
2939 lck_mtx_init(&newnlop
->nlo_lock
, nfs_open_grp
, LCK_ATTR_NULL
);
2940 newnlop
->nlo_pid
= pid
;
2941 newnlop
->nlo_pid_start
= p
->p_start
;
2942 newnlop
->nlo_name
= OSAddAtomic(1, &nfs_lock_owner_seqnum
);
2943 TAILQ_INIT(&newnlop
->nlo_locks
);
2946 if (!nlop
&& newnlop
) {
2947 newnlop
->nlo_flags
|= NFS_LOCK_OWNER_LINK
;
2948 TAILQ_INSERT_HEAD(&np
->n_lock_owners
, newnlop
, nlo_link
);
2951 lck_mtx_unlock(&np
->n_openlock
);
2953 if (newnlop
&& (nlop
!= newnlop
))
2954 nfs_lock_owner_destroy(newnlop
);
2957 nfs_lock_owner_ref(nlop
);
2963 * destroy a lock owner that's no longer needed
2966 nfs_lock_owner_destroy(struct nfs_lock_owner
*nlop
)
2968 if (nlop
->nlo_open_owner
) {
2969 nfs_open_owner_rele(nlop
->nlo_open_owner
);
2970 nlop
->nlo_open_owner
= NULL
;
2972 lck_mtx_destroy(&nlop
->nlo_lock
, nfs_open_grp
);
2977 * acquire a reference count on a lock owner
2980 nfs_lock_owner_ref(struct nfs_lock_owner
*nlop
)
2982 lck_mtx_lock(&nlop
->nlo_lock
);
2984 lck_mtx_unlock(&nlop
->nlo_lock
);
2988 * drop a reference count on a lock owner and destroy it if
2989 * it is no longer referenced and no longer on the mount's list.
2992 nfs_lock_owner_rele(struct nfs_lock_owner
*nlop
)
2994 lck_mtx_lock(&nlop
->nlo_lock
);
2995 if (nlop
->nlo_refcnt
< 1)
2996 panic("nfs_lock_owner_rele: no refcnt");
2998 if (!nlop
->nlo_refcnt
&& (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
2999 panic("nfs_lock_owner_rele: busy");
3000 /* XXX we may potentially want to clean up idle/unused lock owner structures */
3001 if (nlop
->nlo_refcnt
|| (nlop
->nlo_flags
& NFS_LOCK_OWNER_LINK
)) {
3002 lck_mtx_unlock(&nlop
->nlo_lock
);
3005 /* owner is no longer referenced or linked to mount, so destroy it */
3006 lck_mtx_unlock(&nlop
->nlo_lock
);
3007 nfs_lock_owner_destroy(nlop
);
3011 * Mark a lock owner as busy because we are about to
3012 * start an operation that uses and updates lock owner state.
3015 nfs_lock_owner_set_busy(struct nfs_lock_owner
*nlop
, thread_t thd
)
3017 struct nfsmount
*nmp
;
3018 struct timespec ts
= {2, 0};
3019 int error
= 0, slpflag
;
3021 nmp
= nlop
->nlo_open_owner
->noo_mount
;
3024 slpflag
= (NMFLAG(nmp
, INTR
) && thd
) ? PCATCH
: 0;
3026 lck_mtx_lock(&nlop
->nlo_lock
);
3027 while (nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
) {
3028 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
3030 nlop
->nlo_flags
|= NFS_LOCK_OWNER_WANT
;
3031 msleep(nlop
, &nlop
->nlo_lock
, slpflag
, "nfs_lock_owner_set_busy", &ts
);
3035 nlop
->nlo_flags
|= NFS_LOCK_OWNER_BUSY
;
3036 lck_mtx_unlock(&nlop
->nlo_lock
);
3042 * Clear the busy flag on a lock owner and wake up anyone waiting
3046 nfs_lock_owner_clear_busy(struct nfs_lock_owner
*nlop
)
3050 lck_mtx_lock(&nlop
->nlo_lock
);
3051 if (!(nlop
->nlo_flags
& NFS_LOCK_OWNER_BUSY
))
3052 panic("nfs_lock_owner_clear_busy");
3053 wanted
= (nlop
->nlo_flags
& NFS_LOCK_OWNER_WANT
);
3054 nlop
->nlo_flags
&= ~(NFS_LOCK_OWNER_BUSY
|NFS_LOCK_OWNER_WANT
);
3055 lck_mtx_unlock(&nlop
->nlo_lock
);
3061 * Insert a held lock into a lock owner's sorted list.
3062 * (flock locks are always inserted at the head the list)
3065 nfs_lock_owner_insert_held_lock(struct nfs_lock_owner
*nlop
, struct nfs_file_lock
*newnflp
)
3067 struct nfs_file_lock
*nflp
;
3069 /* insert new lock in lock owner's held lock list */
3070 lck_mtx_lock(&nlop
->nlo_lock
);
3071 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) {
3072 TAILQ_INSERT_HEAD(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3074 TAILQ_FOREACH(nflp
, &nlop
->nlo_locks
, nfl_lolink
) {
3075 if (newnflp
->nfl_start
< nflp
->nfl_start
)
3079 TAILQ_INSERT_BEFORE(nflp
, newnflp
, nfl_lolink
);
3081 TAILQ_INSERT_TAIL(&nlop
->nlo_locks
, newnflp
, nfl_lolink
);
3083 lck_mtx_unlock(&nlop
->nlo_lock
);
3087 * Get a file lock structure for this lock owner.
3089 struct nfs_file_lock
*
3090 nfs_file_lock_alloc(struct nfs_lock_owner
*nlop
)
3092 struct nfs_file_lock
*nflp
= NULL
;
3094 lck_mtx_lock(&nlop
->nlo_lock
);
3095 if (!nlop
->nlo_alock
.nfl_owner
) {
3096 nflp
= &nlop
->nlo_alock
;
3097 nflp
->nfl_owner
= nlop
;
3099 lck_mtx_unlock(&nlop
->nlo_lock
);
3101 MALLOC(nflp
, struct nfs_file_lock
*, sizeof(struct nfs_file_lock
), M_TEMP
, M_WAITOK
);
3104 bzero(nflp
, sizeof(*nflp
));
3105 nflp
->nfl_flags
|= NFS_FILE_LOCK_ALLOC
;
3106 nflp
->nfl_owner
= nlop
;
3108 nfs_lock_owner_ref(nlop
);
3113 * destroy the given NFS file lock structure
3116 nfs_file_lock_destroy(struct nfs_file_lock
*nflp
)
3118 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3120 if (nflp
->nfl_flags
& NFS_FILE_LOCK_ALLOC
) {
3121 nflp
->nfl_owner
= NULL
;
3124 lck_mtx_lock(&nlop
->nlo_lock
);
3125 bzero(nflp
, sizeof(nflp
));
3126 lck_mtx_unlock(&nlop
->nlo_lock
);
3128 nfs_lock_owner_rele(nlop
);
3132 * Check if one file lock conflicts with another.
3133 * (nflp1 is the new lock. nflp2 is the existing lock.)
3136 nfs_file_lock_conflict(struct nfs_file_lock
*nflp1
, struct nfs_file_lock
*nflp2
, int *willsplit
)
3138 /* no conflict if lock is dead */
3139 if ((nflp1
->nfl_flags
& NFS_FILE_LOCK_DEAD
) || (nflp2
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
3141 /* no conflict if it's ours - unless the lock style doesn't match */
3142 if ((nflp1
->nfl_owner
== nflp2
->nfl_owner
) &&
3143 ((nflp1
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == (nflp2
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))) {
3144 if (willsplit
&& (nflp1
->nfl_type
!= nflp2
->nfl_type
) &&
3145 (nflp1
->nfl_start
> nflp2
->nfl_start
) &&
3146 (nflp1
->nfl_end
< nflp2
->nfl_end
))
3150 /* no conflict if ranges don't overlap */
3151 if ((nflp1
->nfl_start
> nflp2
->nfl_end
) || (nflp1
->nfl_end
< nflp2
->nfl_start
))
3153 /* no conflict if neither lock is exclusive */
3154 if ((nflp1
->nfl_type
!= F_WRLCK
) && (nflp2
->nfl_type
!= F_WRLCK
))
3161 * Send an NFSv4 LOCK RPC to the server.
3166 struct nfs_open_file
*nofp
,
3167 struct nfs_file_lock
*nflp
,
3173 struct nfs_lock_owner
*nlop
= nflp
->nfl_owner
;
3174 struct nfsmount
*nmp
;
3175 struct nfsm_chain nmreq
, nmrep
;
3178 int error
= 0, lockerror
= ENOENT
, newlocker
, numops
, status
;
3179 struct nfsreq_secinfo_args si
;
3184 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3187 newlocker
= (nlop
->nlo_stategenid
!= nmp
->nm_stategenid
);
3188 locktype
= (nflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
) ?
3189 ((nflp
->nfl_type
== F_WRLCK
) ?
3190 NFS_LOCK_TYPE_WRITEW
:
3191 NFS_LOCK_TYPE_READW
) :
3192 ((nflp
->nfl_type
== F_WRLCK
) ?
3193 NFS_LOCK_TYPE_WRITE
:
3194 NFS_LOCK_TYPE_READ
);
3196 error
= nfs_open_file_set_busy(nofp
, thd
);
3199 error
= nfs_open_owner_set_busy(nofp
->nof_owner
, thd
);
3201 nfs_open_file_clear_busy(nofp
);
3204 if (!nlop
->nlo_open_owner
) {
3205 nfs_open_owner_ref(nofp
->nof_owner
);
3206 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3209 error
= nfs_lock_owner_set_busy(nlop
, thd
);
3212 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3213 nfs_open_file_clear_busy(nofp
);
3218 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3219 nfsm_chain_null(&nmreq
);
3220 nfsm_chain_null(&nmrep
);
3222 // PUTFH, GETATTR, LOCK
3224 nfsm_chain_build_alloc_init(error
, &nmreq
, 33 * NFSX_UNSIGNED
);
3225 nfsm_chain_add_compound_header(error
, &nmreq
, "lock", numops
);
3227 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3228 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3230 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3231 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3233 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCK
);
3234 nfsm_chain_add_32(error
, &nmreq
, locktype
);
3235 nfsm_chain_add_32(error
, &nmreq
, reclaim
);
3236 nfsm_chain_add_64(error
, &nmreq
, nflp
->nfl_start
);
3237 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
));
3238 nfsm_chain_add_32(error
, &nmreq
, newlocker
);
3240 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_owner
->noo_seqid
);
3241 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
3242 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3243 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3245 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3246 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3248 nfsm_chain_build_done(error
, &nmreq
);
3249 nfsm_assert(error
, (numops
== 0), EPROTO
);
3252 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3254 if ((lockerror
= nfs_node_lock(np
)))
3256 nfsm_chain_skip_tag(error
, &nmrep
);
3257 nfsm_chain_get_32(error
, &nmrep
, numops
);
3258 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3260 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3261 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3263 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCK
);
3264 nfs_owner_seqid_increment(newlocker
? nofp
->nof_owner
: NULL
, nlop
, error
);
3265 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3267 /* Update the lock owner's stategenid once it appears the server has state for it. */
3268 /* We determine this by noting the request was successful (we got a stateid). */
3269 if (newlocker
&& !error
)
3270 nlop
->nlo_stategenid
= nmp
->nm_stategenid
;
3273 nfs_node_unlock(np
);
3274 nfs_lock_owner_clear_busy(nlop
);
3276 nfs_open_owner_clear_busy(nofp
->nof_owner
);
3277 nfs_open_file_clear_busy(nofp
);
3279 nfsm_chain_cleanup(&nmreq
);
3280 nfsm_chain_cleanup(&nmrep
);
3285 * Send an NFSv4 LOCKU RPC to the server.
3290 struct nfs_lock_owner
*nlop
,
3298 struct nfsmount
*nmp
;
3299 struct nfsm_chain nmreq
, nmrep
;
3301 int error
= 0, lockerror
= ENOENT
, numops
, status
;
3302 struct nfsreq_secinfo_args si
;
3307 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3310 error
= nfs_lock_owner_set_busy(nlop
, NULL
);
3314 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3315 nfsm_chain_null(&nmreq
);
3316 nfsm_chain_null(&nmrep
);
3318 // PUTFH, GETATTR, LOCKU
3320 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3321 nfsm_chain_add_compound_header(error
, &nmreq
, "unlock", numops
);
3323 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3324 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3326 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3327 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3329 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKU
);
3330 nfsm_chain_add_32(error
, &nmreq
, (type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3331 nfsm_chain_add_32(error
, &nmreq
, nlop
->nlo_seqid
);
3332 nfsm_chain_add_stateid(error
, &nmreq
, &nlop
->nlo_stateid
);
3333 nfsm_chain_add_64(error
, &nmreq
, start
);
3334 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3335 nfsm_chain_build_done(error
, &nmreq
);
3336 nfsm_assert(error
, (numops
== 0), EPROTO
);
3339 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
3341 if ((lockerror
= nfs_node_lock(np
)))
3343 nfsm_chain_skip_tag(error
, &nmrep
);
3344 nfsm_chain_get_32(error
, &nmrep
, numops
);
3345 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3347 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3348 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3350 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKU
);
3351 nfs_owner_seqid_increment(NULL
, nlop
, error
);
3352 nfsm_chain_get_stateid(error
, &nmrep
, &nlop
->nlo_stateid
);
3355 nfs_node_unlock(np
);
3356 nfs_lock_owner_clear_busy(nlop
);
3357 nfsm_chain_cleanup(&nmreq
);
3358 nfsm_chain_cleanup(&nmrep
);
3363 * Send an NFSv4 LOCKT RPC to the server.
3368 struct nfs_lock_owner
*nlop
,
3374 struct nfsmount
*nmp
;
3375 struct nfsm_chain nmreq
, nmrep
;
3376 uint64_t xid
, val64
= 0;
3378 int error
= 0, lockerror
, numops
, status
;
3379 struct nfsreq_secinfo_args si
;
3384 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
3388 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
3389 nfsm_chain_null(&nmreq
);
3390 nfsm_chain_null(&nmrep
);
3392 // PUTFH, GETATTR, LOCKT
3394 nfsm_chain_build_alloc_init(error
, &nmreq
, 26 * NFSX_UNSIGNED
);
3395 nfsm_chain_add_compound_header(error
, &nmreq
, "locktest", numops
);
3397 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
3398 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, np
->n_fhp
, np
->n_fhsize
);
3400 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
3401 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
3403 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOCKT
);
3404 nfsm_chain_add_32(error
, &nmreq
, (fl
->l_type
== F_WRLCK
) ? NFS_LOCK_TYPE_WRITE
: NFS_LOCK_TYPE_READ
);
3405 nfsm_chain_add_64(error
, &nmreq
, start
);
3406 nfsm_chain_add_64(error
, &nmreq
, NFS_LOCK_LENGTH(start
, end
));
3407 nfsm_chain_add_lock_owner4(error
, &nmreq
, nmp
, nlop
);
3408 nfsm_chain_build_done(error
, &nmreq
);
3409 nfsm_assert(error
, (numops
== 0), EPROTO
);
3412 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
3414 if ((lockerror
= nfs_node_lock(np
)))
3416 nfsm_chain_skip_tag(error
, &nmrep
);
3417 nfsm_chain_get_32(error
, &nmrep
, numops
);
3418 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
3420 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
3421 nfsm_chain_loadattr(error
, &nmrep
, np
, NFS_VER4
, &xid
);
3423 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOCKT
);
3424 if (error
== NFSERR_DENIED
) {
3426 nfsm_chain_get_64(error
, &nmrep
, fl
->l_start
);
3427 nfsm_chain_get_64(error
, &nmrep
, val64
);
3428 fl
->l_len
= (val64
== UINT64_MAX
) ? 0 : val64
;
3429 nfsm_chain_get_32(error
, &nmrep
, val
);
3430 fl
->l_type
= (val
== NFS_LOCK_TYPE_WRITE
) ? F_WRLCK
: F_RDLCK
;
3432 fl
->l_whence
= SEEK_SET
;
3433 } else if (!error
) {
3434 fl
->l_type
= F_UNLCK
;
3438 nfs_node_unlock(np
);
3439 nfsm_chain_cleanup(&nmreq
);
3440 nfsm_chain_cleanup(&nmrep
);
3446 * Check for any conflicts with the given lock.
3448 * Checking for a lock doesn't require the file to be opened.
3449 * So we skip all the open owner, open file, lock owner work
3450 * and just check for a conflicting lock.
3453 nfs_advlock_getlock(
3455 struct nfs_lock_owner
*nlop
,
3461 struct nfsmount
*nmp
;
3462 struct nfs_file_lock
*nflp
;
3463 int error
= 0, answered
= 0;
3470 if ((error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
))))
3473 lck_mtx_lock(&np
->n_openlock
);
3474 /* scan currently held locks for conflict */
3475 TAILQ_FOREACH(nflp
, &np
->n_locks
, nfl_link
) {
3476 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3478 if ((start
<= nflp
->nfl_end
) && (end
>= nflp
->nfl_start
) &&
3479 ((fl
->l_type
== F_WRLCK
) || (nflp
->nfl_type
== F_WRLCK
)))
3483 /* found a conflicting lock */
3484 fl
->l_type
= nflp
->nfl_type
;
3485 fl
->l_pid
= (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_FLOCK
) ? -1 : nflp
->nfl_owner
->nlo_pid
;
3486 fl
->l_start
= nflp
->nfl_start
;
3487 fl
->l_len
= NFS_FLOCK_LENGTH(nflp
->nfl_start
, nflp
->nfl_end
);
3488 fl
->l_whence
= SEEK_SET
;
3490 } else if ((np
->n_openflags
& N_DELEG_WRITE
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3492 * If we have a write delegation, we know there can't be other
3493 * locks on the server. So the answer is no conflicting lock found.
3495 fl
->l_type
= F_UNLCK
;
3498 lck_mtx_unlock(&np
->n_openlock
);
3500 nfs_mount_state_in_use_end(nmp
, 0);
3504 /* no conflict found locally, so ask the server */
3505 error
= nmp
->nm_funcs
->nf_getlock_rpc(np
, nlop
, fl
, start
, end
, ctx
);
3507 if (nfs_mount_state_in_use_end(nmp
, error
))
3513 * Acquire a file lock for the given range.
3515 * Add the lock (request) to the lock queue.
3516 * Scan the lock queue for any conflicting locks.
3517 * If a conflict is found, block or return an error.
3518 * Once end of queue is reached, send request to the server.
3519 * If the server grants the lock, scan the lock queue and
3520 * update any existing locks. Then (optionally) scan the
3521 * queue again to coalesce any locks adjacent to the new one.
3524 nfs_advlock_setlock(
3526 struct nfs_open_file
*nofp
,
3527 struct nfs_lock_owner
*nlop
,
3535 struct nfsmount
*nmp
;
3536 struct nfs_file_lock
*newnflp
, *nflp
, *nflp2
= NULL
, *nextnflp
, *flocknflp
= NULL
;
3537 struct nfs_file_lock
*coalnflp
;
3538 int error
= 0, error2
, willsplit
= 0, delay
, slpflag
, busy
= 0, inuse
= 0, restart
, inqueue
= 0;
3539 struct timespec ts
= {1, 0};
3544 slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
3546 if ((type
!= F_RDLCK
) && (type
!= F_WRLCK
))
3549 /* allocate a new lock */
3550 newnflp
= nfs_file_lock_alloc(nlop
);
3553 newnflp
->nfl_start
= start
;
3554 newnflp
->nfl_end
= end
;
3555 newnflp
->nfl_type
= type
;
3557 newnflp
->nfl_flags
|= NFS_FILE_LOCK_WAIT
;
3558 newnflp
->nfl_flags
|= style
;
3559 newnflp
->nfl_flags
|= NFS_FILE_LOCK_BLOCKED
;
3561 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && (type
== F_WRLCK
)) {
3563 * For exclusive flock-style locks, if we block waiting for the
3564 * lock, we need to first release any currently held shared
3565 * flock-style lock. So, the first thing we do is check if we
3566 * have a shared flock-style lock.
3568 nflp
= TAILQ_FIRST(&nlop
->nlo_locks
);
3569 if (nflp
&& ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_FLOCK
))
3571 if (nflp
&& (nflp
->nfl_type
!= F_RDLCK
))
3578 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3582 if (np
->n_flag
& NREVOKE
) {
3584 nfs_mount_state_in_use_end(nmp
, 0);
3588 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
3589 nfs_mount_state_in_use_end(nmp
, 0);
3591 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
3597 lck_mtx_lock(&np
->n_openlock
);
3599 /* insert new lock at beginning of list */
3600 TAILQ_INSERT_HEAD(&np
->n_locks
, newnflp
, nfl_link
);
3604 /* scan current list of locks (held and pending) for conflicts */
3605 for (nflp
= TAILQ_NEXT(newnflp
, nfl_link
); nflp
; nflp
= nextnflp
) {
3606 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
3607 if (!nfs_file_lock_conflict(newnflp
, nflp
, &willsplit
))
3610 if (!(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3614 /* Block until this lock is no longer held. */
3615 if (nflp
->nfl_blockcnt
== UINT_MAX
) {
3619 nflp
->nfl_blockcnt
++;
3622 /* release any currently held shared lock before sleeping */
3623 lck_mtx_unlock(&np
->n_openlock
);
3624 nfs_mount_state_in_use_end(nmp
, 0);
3626 error
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3629 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3631 lck_mtx_lock(&np
->n_openlock
);
3635 lck_mtx_lock(&np
->n_openlock
);
3636 /* no need to block/sleep if the conflict is gone */
3637 if (!nfs_file_lock_conflict(newnflp
, nflp
, NULL
))
3640 msleep(nflp
, &np
->n_openlock
, slpflag
, "nfs_advlock_setlock_blocked", &ts
);
3642 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3643 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3644 /* looks like we have a recover pending... restart */
3646 lck_mtx_unlock(&np
->n_openlock
);
3647 nfs_mount_state_in_use_end(nmp
, 0);
3649 lck_mtx_lock(&np
->n_openlock
);
3652 if (!error
&& (np
->n_flag
& NREVOKE
))
3654 } while (!error
&& nfs_file_lock_conflict(newnflp
, nflp
, NULL
));
3655 nflp
->nfl_blockcnt
--;
3656 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) && !nflp
->nfl_blockcnt
) {
3657 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3658 nfs_file_lock_destroy(nflp
);
3660 if (error
|| restart
)
3662 /* We have released n_openlock and we can't trust that nextnflp is still valid. */
3663 /* So, start this lock-scanning loop over from where it started. */
3664 nextnflp
= TAILQ_NEXT(newnflp
, nfl_link
);
3666 lck_mtx_unlock(&np
->n_openlock
);
3674 * It looks like this operation is splitting a lock.
3675 * We allocate a new lock now so we don't have to worry
3676 * about the allocation failing after we've updated some state.
3678 nflp2
= nfs_file_lock_alloc(nlop
);
3685 /* once scan for local conflicts is clear, send request to server */
3686 if ((error
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
))))
3691 /* do we have a delegation? (that we're not returning?) */
3692 if ((np
->n_openflags
& N_DELEG_MASK
) && !(np
->n_openflags
& N_DELEG_RETURN
)) {
3693 if (np
->n_openflags
& N_DELEG_WRITE
) {
3694 /* with a write delegation, just take the lock delegated */
3695 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DELEGATED
;
3697 /* make sure the lock owner knows its open owner */
3698 if (!nlop
->nlo_open_owner
) {
3699 nfs_open_owner_ref(nofp
->nof_owner
);
3700 nlop
->nlo_open_owner
= nofp
->nof_owner
;
3705 * If we don't have any non-delegated opens but we do have
3706 * delegated opens, then we need to first claim the delegated
3707 * opens so that the lock request on the server can be associated
3708 * with an open it knows about.
3710 if ((!nofp
->nof_rw_drw
&& !nofp
->nof_w_drw
&& !nofp
->nof_r_drw
&&
3711 !nofp
->nof_rw_dw
&& !nofp
->nof_w_dw
&& !nofp
->nof_r_dw
&&
3712 !nofp
->nof_rw
&& !nofp
->nof_w
&& !nofp
->nof_r
) &&
3713 (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
3714 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
3715 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)) {
3716 error
= nfs4_claim_delegated_state_for_open_file(nofp
, 0);
3722 if (np
->n_flag
& NREVOKE
)
3725 error
= nmp
->nm_funcs
->nf_setlock_rpc(np
, nofp
, newnflp
, 0, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3726 if (!error
|| ((error
!= NFSERR_DENIED
) && (error
!= NFSERR_GRACE
)))
3728 /* request was denied due to either conflict or grace period */
3729 if ((error
== NFSERR_DENIED
) && !(newnflp
->nfl_flags
& NFS_FILE_LOCK_WAIT
)) {
3734 /* release any currently held shared lock before sleeping */
3735 nfs_open_state_clear_busy(np
);
3737 nfs_mount_state_in_use_end(nmp
, 0);
3739 error2
= nfs_advlock_unlock(np
, nofp
, nlop
, 0, UINT64_MAX
, NFS_FILE_LOCK_STYLE_FLOCK
, ctx
);
3742 error2
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
3745 error2
= nfs_open_state_set_busy(np
, vfs_context_thread(ctx
));
3754 * Wait a little bit and send the request again.
3755 * Except for retries of blocked v2/v3 request where we've already waited a bit.
3757 if ((nmp
->nm_vers
>= NFS_VER4
) || (error
== NFSERR_GRACE
)) {
3758 if (error
== NFSERR_GRACE
)
3762 tsleep(newnflp
, slpflag
, "nfs_advlock_setlock_delay", delay
* (hz
/2));
3765 error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0);
3766 if (!error
&& (nmp
->nm_state
& NFSSTA_RECOVER
)) {
3767 /* looks like we have a recover pending... restart */
3768 nfs_open_state_clear_busy(np
);
3770 nfs_mount_state_in_use_end(nmp
, 0);
3774 if (!error
&& (np
->n_flag
& NREVOKE
))
3779 if (nfs_mount_state_error_should_restart(error
)) {
3780 /* looks like we need to restart this operation */
3782 nfs_open_state_clear_busy(np
);
3786 nfs_mount_state_in_use_end(nmp
, error
);
3791 lck_mtx_lock(&np
->n_openlock
);
3792 newnflp
->nfl_flags
&= ~NFS_FILE_LOCK_BLOCKED
;
3794 newnflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3795 if (newnflp
->nfl_blockcnt
) {
3796 /* wake up anyone blocked on this lock */
3799 /* remove newnflp from lock list and destroy */
3800 TAILQ_REMOVE(&np
->n_locks
, newnflp
, nfl_link
);
3801 nfs_file_lock_destroy(newnflp
);
3803 lck_mtx_unlock(&np
->n_openlock
);
3805 nfs_open_state_clear_busy(np
);
3807 nfs_mount_state_in_use_end(nmp
, error
);
3809 nfs_file_lock_destroy(nflp2
);
3813 /* server granted the lock */
3816 * Scan for locks to update.
3818 * Locks completely covered are killed.
3819 * At most two locks may need to be clipped.
3820 * It's possible that a single lock may need to be split.
3822 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3823 if (nflp
== newnflp
)
3825 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3827 if (nflp
->nfl_owner
!= nlop
)
3829 if ((newnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != (nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
))
3831 if ((newnflp
->nfl_start
> nflp
->nfl_end
) || (newnflp
->nfl_end
< nflp
->nfl_start
))
3833 /* here's one to update */
3834 if ((newnflp
->nfl_start
<= nflp
->nfl_start
) && (newnflp
->nfl_end
>= nflp
->nfl_end
)) {
3835 /* The entire lock is being replaced. */
3836 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3837 lck_mtx_lock(&nlop
->nlo_lock
);
3838 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3839 lck_mtx_unlock(&nlop
->nlo_lock
);
3840 /* lock will be destroyed below, if no waiters */
3841 } else if ((newnflp
->nfl_start
> nflp
->nfl_start
) && (newnflp
->nfl_end
< nflp
->nfl_end
)) {
3842 /* We're replacing a range in the middle of a lock. */
3843 /* The current lock will be split into two locks. */
3844 /* Update locks and insert new lock after current lock. */
3845 nflp2
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
3846 nflp2
->nfl_type
= nflp
->nfl_type
;
3847 nflp2
->nfl_start
= newnflp
->nfl_end
+ 1;
3848 nflp2
->nfl_end
= nflp
->nfl_end
;
3849 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3850 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, nflp2
, nfl_link
);
3851 nfs_lock_owner_insert_held_lock(nlop
, nflp2
);
3854 } else if (newnflp
->nfl_start
> nflp
->nfl_start
) {
3855 /* We're replacing the end of a lock. */
3856 nflp
->nfl_end
= newnflp
->nfl_start
- 1;
3857 } else if (newnflp
->nfl_end
< nflp
->nfl_end
) {
3858 /* We're replacing the start of a lock. */
3859 nflp
->nfl_start
= newnflp
->nfl_end
+ 1;
3861 if (nflp
->nfl_blockcnt
) {
3862 /* wake up anyone blocked on this lock */
3864 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
3865 /* remove nflp from lock list and destroy */
3866 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3867 nfs_file_lock_destroy(nflp
);
3871 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
3874 * POSIX locks should be coalesced when possible.
3876 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) && (nofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)) {
3878 * Walk through the lock queue and check each of our held locks with
3879 * the previous and next locks in the lock owner's "held lock list".
3880 * If the two locks can be coalesced, we merge the current lock into
3881 * the other (previous or next) lock. Merging this way makes sure that
3882 * lock ranges are always merged forward in the lock queue. This is
3883 * important because anyone blocked on the lock being "merged away"
3884 * will still need to block on that range and it will simply continue
3885 * checking locks that are further down the list.
3887 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3888 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3890 if (nflp
->nfl_owner
!= nlop
)
3892 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != NFS_FILE_LOCK_STYLE_POSIX
)
3894 if (((coalnflp
= TAILQ_PREV(nflp
, nfs_file_lock_queue
, nfl_lolink
))) &&
3895 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3896 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3897 (coalnflp
->nfl_end
== (nflp
->nfl_start
- 1))) {
3898 coalnflp
->nfl_end
= nflp
->nfl_end
;
3899 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3900 lck_mtx_lock(&nlop
->nlo_lock
);
3901 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3902 lck_mtx_unlock(&nlop
->nlo_lock
);
3903 } else if (((coalnflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
3904 ((coalnflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) &&
3905 (coalnflp
->nfl_type
== nflp
->nfl_type
) &&
3906 (coalnflp
->nfl_start
== (nflp
->nfl_end
+ 1))) {
3907 coalnflp
->nfl_start
= nflp
->nfl_start
;
3908 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
3909 lck_mtx_lock(&nlop
->nlo_lock
);
3910 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
3911 lck_mtx_unlock(&nlop
->nlo_lock
);
3913 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
))
3915 if (nflp
->nfl_blockcnt
) {
3916 /* wake up anyone blocked on this lock */
3919 /* remove nflp from lock list and destroy */
3920 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
3921 nfs_file_lock_destroy(nflp
);
3926 lck_mtx_unlock(&np
->n_openlock
);
3927 nfs_open_state_clear_busy(np
);
3928 nfs_mount_state_in_use_end(nmp
, error
);
3931 nfs_file_lock_destroy(nflp2
);
3936 * Release all (same style) locks within the given range.
3941 struct nfs_open_file
*nofp
,
3942 struct nfs_lock_owner
*nlop
,
3948 struct nfsmount
*nmp
;
3949 struct nfs_file_lock
*nflp
, *nextnflp
, *newnflp
= NULL
;
3950 int error
= 0, willsplit
= 0, send_unlock_rpcs
= 1;
3957 if ((error
= nfs_mount_state_in_use_start(nmp
, NULL
)))
3959 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
3960 nfs_mount_state_in_use_end(nmp
, 0);
3961 error
= nfs4_reopen(nofp
, NULL
);
3966 if ((error
= nfs_open_state_set_busy(np
, NULL
))) {
3967 nfs_mount_state_in_use_end(nmp
, error
);
3971 lck_mtx_lock(&np
->n_openlock
);
3972 if ((start
> 0) && (end
< UINT64_MAX
) && !willsplit
) {
3974 * We may need to allocate a new lock if an existing lock gets split.
3975 * So, we first scan the list to check for a split, and if there's
3976 * going to be one, we'll allocate one now.
3978 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
3979 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
3981 if (nflp
->nfl_owner
!= nlop
)
3983 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
3985 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
3987 if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
3993 lck_mtx_unlock(&np
->n_openlock
);
3994 nfs_open_state_clear_busy(np
);
3995 nfs_mount_state_in_use_end(nmp
, 0);
3996 newnflp
= nfs_file_lock_alloc(nlop
);
4004 * Free all of our locks in the given range.
4006 * Note that this process requires sending requests to the server.
4007 * Because of this, we will release the n_openlock while performing
4008 * the unlock RPCs. The N_OPENBUSY state keeps the state of *held*
4009 * locks from changing underneath us. However, other entries in the
4010 * list may be removed. So we need to be careful walking the list.
4014 * Don't unlock ranges that are held by other-style locks.
4015 * If style is posix, don't send any unlock rpcs if flock is held.
4016 * If we unlock an flock, don't send unlock rpcs for any posix-style
4017 * ranges held - instead send unlocks for the ranges not held.
4019 if ((style
== NFS_FILE_LOCK_STYLE_POSIX
) &&
4020 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4021 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
))
4022 send_unlock_rpcs
= 0;
4023 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) &&
4024 ((nflp
= TAILQ_FIRST(&nlop
->nlo_locks
))) &&
4025 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_FLOCK
) &&
4026 ((nflp
= TAILQ_NEXT(nflp
, nfl_lolink
))) &&
4027 ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
)) {
4029 int type
= TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_type
;
4030 int delegated
= (TAILQ_FIRST(&nlop
->nlo_locks
)->nfl_flags
& NFS_FILE_LOCK_DELEGATED
);
4031 while (!delegated
&& nflp
) {
4032 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) == NFS_FILE_LOCK_STYLE_POSIX
) {
4033 /* unlock the range preceding this lock */
4034 lck_mtx_unlock(&np
->n_openlock
);
4035 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, nflp
->nfl_start
-1, 0,
4036 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4037 if (nfs_mount_state_error_should_restart(error
)) {
4038 nfs_open_state_clear_busy(np
);
4039 nfs_mount_state_in_use_end(nmp
, error
);
4042 lck_mtx_lock(&np
->n_openlock
);
4045 s
= nflp
->nfl_end
+1;
4047 nflp
= TAILQ_NEXT(nflp
, nfl_lolink
);
4050 lck_mtx_unlock(&np
->n_openlock
);
4051 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, type
, s
, end
, 0,
4052 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4053 if (nfs_mount_state_error_should_restart(error
)) {
4054 nfs_open_state_clear_busy(np
);
4055 nfs_mount_state_in_use_end(nmp
, error
);
4058 lck_mtx_lock(&np
->n_openlock
);
4062 send_unlock_rpcs
= 0;
4065 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
4066 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
|NFS_FILE_LOCK_DEAD
))
4068 if (nflp
->nfl_owner
!= nlop
)
4070 if ((nflp
->nfl_flags
& NFS_FILE_LOCK_STYLE_MASK
) != style
)
4072 if ((start
> nflp
->nfl_end
) || (end
< nflp
->nfl_start
))
4074 /* here's one to unlock */
4075 if ((start
<= nflp
->nfl_start
) && (end
>= nflp
->nfl_end
)) {
4076 /* The entire lock is being unlocked. */
4077 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4078 lck_mtx_unlock(&np
->n_openlock
);
4079 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, nflp
->nfl_end
, 0,
4080 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4081 if (nfs_mount_state_error_should_restart(error
)) {
4082 nfs_open_state_clear_busy(np
);
4083 nfs_mount_state_in_use_end(nmp
, error
);
4086 lck_mtx_lock(&np
->n_openlock
);
4088 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4091 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
4092 lck_mtx_lock(&nlop
->nlo_lock
);
4093 TAILQ_REMOVE(&nlop
->nlo_locks
, nflp
, nfl_lolink
);
4094 lck_mtx_unlock(&nlop
->nlo_lock
);
4095 /* lock will be destroyed below, if no waiters */
4096 } else if ((start
> nflp
->nfl_start
) && (end
< nflp
->nfl_end
)) {
4097 /* We're unlocking a range in the middle of a lock. */
4098 /* The current lock will be split into two locks. */
4099 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4100 lck_mtx_unlock(&np
->n_openlock
);
4101 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, end
, 0,
4102 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4103 if (nfs_mount_state_error_should_restart(error
)) {
4104 nfs_open_state_clear_busy(np
);
4105 nfs_mount_state_in_use_end(nmp
, error
);
4108 lck_mtx_lock(&np
->n_openlock
);
4112 /* update locks and insert new lock after current lock */
4113 newnflp
->nfl_flags
|= (nflp
->nfl_flags
& (NFS_FILE_LOCK_STYLE_MASK
|NFS_FILE_LOCK_DELEGATED
));
4114 newnflp
->nfl_type
= nflp
->nfl_type
;
4115 newnflp
->nfl_start
= end
+ 1;
4116 newnflp
->nfl_end
= nflp
->nfl_end
;
4117 nflp
->nfl_end
= start
- 1;
4118 TAILQ_INSERT_AFTER(&np
->n_locks
, nflp
, newnflp
, nfl_link
);
4119 nfs_lock_owner_insert_held_lock(nlop
, newnflp
);
4122 } else if (start
> nflp
->nfl_start
) {
4123 /* We're unlocking the end of a lock. */
4124 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4125 lck_mtx_unlock(&np
->n_openlock
);
4126 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, start
, nflp
->nfl_end
, 0,
4127 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4128 if (nfs_mount_state_error_should_restart(error
)) {
4129 nfs_open_state_clear_busy(np
);
4130 nfs_mount_state_in_use_end(nmp
, error
);
4133 lck_mtx_lock(&np
->n_openlock
);
4135 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4138 nflp
->nfl_end
= start
- 1;
4139 } else if (end
< nflp
->nfl_end
) {
4140 /* We're unlocking the start of a lock. */
4141 if (send_unlock_rpcs
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
)) {
4142 lck_mtx_unlock(&np
->n_openlock
);
4143 error
= nmp
->nm_funcs
->nf_unlock_rpc(np
, nlop
, nflp
->nfl_type
, nflp
->nfl_start
, end
, 0,
4144 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4145 if (nfs_mount_state_error_should_restart(error
)) {
4146 nfs_open_state_clear_busy(np
);
4147 nfs_mount_state_in_use_end(nmp
, error
);
4150 lck_mtx_lock(&np
->n_openlock
);
4152 nextnflp
= TAILQ_NEXT(nflp
, nfl_link
);
4155 nflp
->nfl_start
= end
+ 1;
4157 if (nflp
->nfl_blockcnt
) {
4158 /* wake up anyone blocked on this lock */
4160 } else if (nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) {
4161 /* remove nflp from lock list and destroy */
4162 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
4163 nfs_file_lock_destroy(nflp
);
4167 lck_mtx_unlock(&np
->n_openlock
);
4168 nfs_open_state_clear_busy(np
);
4169 nfs_mount_state_in_use_end(nmp
, 0);
4172 nfs_file_lock_destroy(newnflp
);
4177 * NFSv4 advisory file locking
4181 struct vnop_advlock_args
/* {
4182 struct vnodeop_desc *a_desc;
4188 vfs_context_t a_context;
4191 vnode_t vp
= ap
->a_vp
;
4192 nfsnode_t np
= VTONFS(ap
->a_vp
);
4193 struct flock
*fl
= ap
->a_fl
;
4195 int flags
= ap
->a_flags
;
4196 vfs_context_t ctx
= ap
->a_context
;
4197 struct nfsmount
*nmp
;
4198 struct nfs_open_owner
*noop
= NULL
;
4199 struct nfs_open_file
*nofp
= NULL
;
4200 struct nfs_lock_owner
*nlop
= NULL
;
4202 uint64_t start
, end
;
4203 int error
= 0, modified
, style
;
4205 #define OFF_MAX QUAD_MAX
4207 nmp
= VTONMP(ap
->a_vp
);
4210 lck_mtx_lock(&nmp
->nm_lock
);
4211 if ((nmp
->nm_vers
<= NFS_VER3
) && (nmp
->nm_lockmode
== NFS_LOCK_MODE_DISABLED
)) {
4212 lck_mtx_unlock(&nmp
->nm_lock
);
4215 lck_mtx_unlock(&nmp
->nm_lock
);
4217 if (np
->n_flag
& NREVOKE
)
4219 vtype
= vnode_vtype(ap
->a_vp
);
4220 if (vtype
== VDIR
) /* ignore lock requests on directories */
4222 if (vtype
!= VREG
) /* anything other than regular files is invalid */
4225 /* Convert the flock structure into a start and end. */
4226 switch (fl
->l_whence
) {
4230 * Caller is responsible for adding any necessary offset
4231 * to fl->l_start when SEEK_CUR is used.
4233 lstart
= fl
->l_start
;
4236 /* need to flush, and refetch attributes to make */
4237 /* sure we have the correct end of file offset */
4238 if ((error
= nfs_node_lock(np
)))
4240 modified
= (np
->n_flag
& NMODIFIED
);
4241 nfs_node_unlock(np
);
4242 if (modified
&& ((error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1))))
4244 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
4246 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
4247 if ((np
->n_size
> OFF_MAX
) ||
4248 ((fl
->l_start
> 0) && (np
->n_size
> (u_quad_t
)(OFF_MAX
- fl
->l_start
))))
4250 lstart
= np
->n_size
+ fl
->l_start
;
4251 nfs_data_unlock(np
);
4261 if (fl
->l_len
== 0) {
4263 } else if (fl
->l_len
> 0) {
4264 if ((fl
->l_len
- 1) > (OFF_MAX
- lstart
))
4266 end
= start
- 1 + fl
->l_len
;
4267 } else { /* l_len is negative */
4268 if ((lstart
+ fl
->l_len
) < 0)
4273 if ((nmp
->nm_vers
== NFS_VER2
) && ((start
> INT32_MAX
) || (fl
->l_len
&& (end
> INT32_MAX
))))
4276 style
= (flags
& F_FLOCK
) ? NFS_FILE_LOCK_STYLE_FLOCK
: NFS_FILE_LOCK_STYLE_POSIX
;
4277 if ((style
== NFS_FILE_LOCK_STYLE_FLOCK
) && ((start
!= 0) || (end
!= UINT64_MAX
)))
4280 /* find the lock owner, alloc if not unlock */
4281 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), (op
!= F_UNLCK
));
4283 error
= (op
== F_UNLCK
) ? 0 : ENOMEM
;
4285 NP(np
, "nfs_vnop_advlock: no lock owner, error %d", error
);
4289 if (op
== F_GETLK
) {
4290 error
= nfs_advlock_getlock(np
, nlop
, fl
, start
, end
, ctx
);
4292 /* find the open owner */
4293 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
4295 NP(np
, "nfs_vnop_advlock: no open owner %d", kauth_cred_getuid(vfs_context_ucred(ctx
)));
4299 /* find the open file */
4301 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
4304 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
4305 NP(np
, "nfs_vnop_advlock: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
4308 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4309 error
= nfs4_reopen(nofp
, ((op
== F_UNLCK
) ? NULL
: vfs_context_thread(ctx
)));
4315 NP(np
, "nfs_vnop_advlock: no open file %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
4318 if (op
== F_UNLCK
) {
4319 error
= nfs_advlock_unlock(np
, nofp
, nlop
, start
, end
, style
, ctx
);
4320 } else if ((op
== F_SETLK
) || (op
== F_SETLKW
)) {
4321 if ((op
== F_SETLK
) && (flags
& F_WAIT
))
4323 error
= nfs_advlock_setlock(np
, nofp
, nlop
, op
, start
, end
, style
, fl
->l_type
, ctx
);
4325 /* not getlk, unlock or lock? */
4332 nfs_lock_owner_rele(nlop
);
4334 nfs_open_owner_rele(noop
);
4339 * Check if an open owner holds any locks on a file.
4342 nfs_check_for_locks(struct nfs_open_owner
*noop
, struct nfs_open_file
*nofp
)
4344 struct nfs_lock_owner
*nlop
;
4346 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
4347 if (nlop
->nlo_open_owner
!= noop
)
4349 if (!TAILQ_EMPTY(&nlop
->nlo_locks
))
4352 return (nlop
? 1 : 0);
4356 * Reopen simple (no deny, no locks) open state that was lost.
4359 nfs4_reopen(struct nfs_open_file
*nofp
, thread_t thd
)
4361 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4362 struct nfsmount
*nmp
= NFSTONMP(nofp
->nof_np
);
4363 nfsnode_t np
= nofp
->nof_np
;
4364 vnode_t vp
= NFSTOV(np
);
4366 struct componentname cn
;
4367 const char *vname
= NULL
;
4368 const char *name
= NULL
;
4370 char smallname
[128];
4371 char *filename
= NULL
;
4372 int error
= 0, done
= 0, slpflag
= NMFLAG(nmp
, INTR
) ? PCATCH
: 0;
4373 struct timespec ts
= { 1, 0 };
4375 lck_mtx_lock(&nofp
->nof_lock
);
4376 while (nofp
->nof_flags
& NFS_OPEN_FILE_REOPENING
) {
4377 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
4379 msleep(&nofp
->nof_flags
, &nofp
->nof_lock
, slpflag
|(PZERO
-1), "nfsreopenwait", &ts
);
4382 if (error
|| !(nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
4383 lck_mtx_unlock(&nofp
->nof_lock
);
4386 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPENING
;
4387 lck_mtx_unlock(&nofp
->nof_lock
);
4389 nfs_node_lock_force(np
);
4390 if ((vnode_vtype(vp
) != VDIR
) && np
->n_sillyrename
) {
4392 * The node's been sillyrenamed, so we need to use
4393 * the sillyrename directory/name to do the open.
4395 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
4396 dvp
= NFSTOV(nsp
->nsr_dnp
);
4397 if ((error
= vnode_get(dvp
))) {
4398 nfs_node_unlock(np
);
4401 name
= nsp
->nsr_name
;
4404 * [sigh] We can't trust VFS to get the parent right for named
4405 * attribute nodes. (It likes to reparent the nodes after we've
4406 * created them.) Luckily we can probably get the right parent
4407 * from the n_parent we have stashed away.
4409 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
4410 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
4413 dvp
= vnode_getparent(vp
);
4414 vname
= vnode_getname(vp
);
4415 if (!dvp
|| !vname
) {
4418 nfs_node_unlock(np
);
4423 filename
= &smallname
[0];
4424 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
4425 if (namelen
>= sizeof(smallname
)) {
4426 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
4431 snprintf(filename
, namelen
+1, "%s", name
);
4433 nfs_node_unlock(np
);
4434 bzero(&cn
, sizeof(cn
));
4435 cn
.cn_nameptr
= filename
;
4436 cn
.cn_namelen
= namelen
;
4440 if ((error
= nfs_mount_state_in_use_start(nmp
, thd
)))
4444 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
);
4445 if (!error
&& nofp
->nof_w
)
4446 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
);
4447 if (!error
&& nofp
->nof_r
)
4448 error
= nfs4_open_reopen_rpc(nofp
, thd
, noop
->noo_cred
, &cn
, dvp
, &vp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
);
4450 if (nfs_mount_state_in_use_end(nmp
, error
)) {
4451 if (error
== NFSERR_GRACE
)
4453 printf("nfs4_reopen: RPC failed, error %d, lost %d, %s\n", error
,
4454 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4460 if (error
&& (error
!= EINTR
) && (error
!= ERESTART
))
4461 nfs_revoke_open_state_for_node(np
);
4462 lck_mtx_lock(&nofp
->nof_lock
);
4463 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPENING
;
4465 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
4467 printf("nfs4_reopen: failed, error %d, lost %d, %s\n", error
,
4468 (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) ? 1 : 0, name
? name
: "???");
4469 lck_mtx_unlock(&nofp
->nof_lock
);
4470 if (filename
&& (filename
!= &smallname
[0]))
4471 FREE(filename
, M_TEMP
);
4473 vnode_putname(vname
);
4480 * Send a normal OPEN RPC to open/create a file.
4484 struct nfs_open_file
*nofp
,
4486 struct componentname
*cnp
,
4487 struct vnode_attr
*vap
,
4494 return (nfs4_open_rpc_internal(nofp
, ctx
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
4495 cnp
, vap
, dvp
, vpp
, create
, share_access
, share_deny
));
4499 * Send an OPEN RPC to reopen a file.
4502 nfs4_open_reopen_rpc(
4503 struct nfs_open_file
*nofp
,
4506 struct componentname
*cnp
,
4512 return (nfs4_open_rpc_internal(nofp
, NULL
, thd
, cred
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, share_access
, share_deny
));
4516 * Send an OPEN_CONFIRM RPC to confirm an OPEN.
4519 nfs4_open_confirm_rpc(
4520 struct nfsmount
*nmp
,
4524 struct nfs_open_owner
*noop
,
4528 struct nfs_vattr
*nvap
,
4531 struct nfsm_chain nmreq
, nmrep
;
4532 int error
= 0, status
, numops
;
4533 struct nfsreq_secinfo_args si
;
4535 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
4536 nfsm_chain_null(&nmreq
);
4537 nfsm_chain_null(&nmrep
);
4539 // PUTFH, OPEN_CONFIRM, GETATTR
4541 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
4542 nfsm_chain_add_compound_header(error
, &nmreq
, "open_confirm", numops
);
4544 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4545 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
4547 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_CONFIRM
);
4548 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
4549 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4551 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4552 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4553 nfsm_chain_build_done(error
, &nmreq
);
4554 nfsm_assert(error
, (numops
== 0), EPROTO
);
4556 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, &nmrep
, xidp
, &status
);
4558 nfsm_chain_skip_tag(error
, &nmrep
);
4559 nfsm_chain_get_32(error
, &nmrep
, numops
);
4560 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4562 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_CONFIRM
);
4563 nfs_owner_seqid_increment(noop
, NULL
, error
);
4564 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4565 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4567 error
= nfs4_parsefattr(&nmrep
, NULL
, nvap
, NULL
, NULL
, NULL
);
4569 nfsm_chain_cleanup(&nmreq
);
4570 nfsm_chain_cleanup(&nmrep
);
4575 * common OPEN RPC code
4577 * If create is set, ctx must be passed in.
4578 * Returns a node on success if no node passed in.
4581 nfs4_open_rpc_internal(
4582 struct nfs_open_file
*nofp
,
4586 struct componentname
*cnp
,
4587 struct vnode_attr
*vap
,
4594 struct nfsmount
*nmp
;
4595 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4596 struct nfs_vattr nvattr
;
4597 int error
= 0, open_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
4598 int nfsvers
, namedattrs
, numops
, exclusive
= 0, gotuid
, gotgid
;
4599 u_int64_t xid
, savedxid
= 0;
4600 nfsnode_t dnp
= VTONFS(dvp
);
4601 nfsnode_t np
, newnp
= NULL
;
4602 vnode_t newvp
= NULL
;
4603 struct nfsm_chain nmreq
, nmrep
;
4604 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
4605 uint32_t rflags
, delegation
, recall
;
4606 struct nfs_stateid stateid
, dstateid
, *sid
;
4608 struct nfsreq rq
, *req
= &rq
;
4609 struct nfs_dulookup dul
;
4611 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
4612 struct kauth_ace ace
;
4613 struct nfsreq_secinfo_args si
;
4621 nfsvers
= nmp
->nm_vers
;
4622 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4623 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
4626 np
= *vpp
? VTONFS(*vpp
) : NULL
;
4627 if (create
&& vap
) {
4628 exclusive
= (vap
->va_vaflags
& VA_EXCLUSIVE
);
4629 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4630 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4631 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4632 if (exclusive
&& (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
)))
4633 vap
->va_vaflags
|= VA_UTIMES_NULL
;
4635 exclusive
= gotuid
= gotgid
= 0;
4638 sid
= &nofp
->nof_stateid
;
4640 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
4644 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
4647 rflags
= delegation
= recall
= 0;
4650 slen
= sizeof(sbuf
);
4651 NVATTR_INIT(&nvattr
);
4652 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, cnp
->cn_nameptr
, cnp
->cn_namelen
);
4654 nfsm_chain_null(&nmreq
);
4655 nfsm_chain_null(&nmrep
);
4657 // PUTFH, SAVEFH, OPEN(CREATE?), GETATTR(FH), RESTOREFH, GETATTR
4659 nfsm_chain_build_alloc_init(error
, &nmreq
, 53 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
4660 nfsm_chain_add_compound_header(error
, &nmreq
, create
? "create" : "open", numops
);
4662 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
4663 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4665 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
4667 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
4668 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
4669 nfsm_chain_add_32(error
, &nmreq
, share_access
);
4670 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
4671 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
4672 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
4673 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
4674 nfsm_chain_add_32(error
, &nmreq
, create
);
4677 static uint32_t create_verf
; // XXX need a better verifier
4679 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
4680 /* insert 64 bit verifier */
4681 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4682 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4684 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
4685 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
4688 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
4689 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4691 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4692 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
4693 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
4694 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
4696 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
4698 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
4699 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
4700 nfsm_chain_build_done(error
, &nmreq
);
4701 nfsm_assert(error
, (numops
== 0), EPROTO
);
4703 error
= busyerror
= nfs_node_set_busy(dnp
, thd
);
4706 if (create
&& !namedattrs
)
4707 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4709 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, R_NOINTR
, NULL
, &req
);
4711 if (create
&& !namedattrs
)
4712 nfs_dulookup_start(&dul
, dnp
, ctx
);
4713 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4717 if (create
&& !namedattrs
)
4718 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4720 if ((lockerror
= nfs_node_lock(dnp
)))
4722 nfsm_chain_skip_tag(error
, &nmrep
);
4723 nfsm_chain_get_32(error
, &nmrep
, numops
);
4724 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
4725 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
4727 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
4728 nfs_owner_seqid_increment(noop
, NULL
, error
);
4729 nfsm_chain_get_stateid(error
, &nmrep
, sid
);
4730 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
4731 nfsm_chain_get_32(error
, &nmrep
, rflags
);
4732 bmlen
= NFS_ATTR_BITMAP_LEN
;
4733 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
4734 nfsm_chain_get_32(error
, &nmrep
, delegation
);
4736 switch (delegation
) {
4737 case NFS_OPEN_DELEGATE_NONE
:
4739 case NFS_OPEN_DELEGATE_READ
:
4740 case NFS_OPEN_DELEGATE_WRITE
:
4741 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
4742 nfsm_chain_get_32(error
, &nmrep
, recall
);
4743 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
4744 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
4745 /* if we have any trouble accepting the ACE, just invalidate it */
4746 ace_type
= ace_flags
= ace_mask
= len
= 0;
4747 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
4748 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
4749 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
4750 nfsm_chain_get_32(error
, &nmrep
, len
);
4751 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
4752 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
4753 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
4754 if (!error
&& (len
>= slen
)) {
4755 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
4762 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
4764 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
4767 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
4772 if (s
&& (s
!= sbuf
))
4779 /* At this point if we have no error, the object was created/opened. */
4782 if (create
&& vap
&& !exclusive
)
4783 nfs_vattr_set_supported(bitmap
, vap
);
4784 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4786 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
4788 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
4789 printf("nfs: open/create didn't return filehandle? %s\n", cnp
->cn_nameptr
);
4793 if (!create
&& np
&& !NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4794 // XXX for the open case, what if fh doesn't match the vnode we think we're opening?
4795 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
4796 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
4797 NP(np
, "nfs4_open_rpc: warning: file handle mismatch");
4799 /* directory attributes: if we don't get them, make sure to invalidate */
4800 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
4801 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
4802 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
4804 NATTRINVALIDATE(dnp
);
4807 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
4808 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
4810 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
4811 nfs_node_unlock(dnp
);
4813 NVATTR_CLEANUP(&nvattr
);
4814 error
= nfs4_open_confirm_rpc(nmp
, dnp
, fh
.fh_data
, fh
.fh_len
, noop
, sid
, thd
, cred
, &nvattr
, &xid
);
4817 if ((lockerror
= nfs_node_lock(dnp
)))
4822 nfsm_chain_cleanup(&nmreq
);
4823 nfsm_chain_cleanup(&nmrep
);
4825 if (!lockerror
&& create
) {
4826 if (!open_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
4827 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4828 cache_purge_negatives(dvp
);
4830 dnp
->n_flag
|= NMODIFIED
;
4831 nfs_node_unlock(dnp
);
4833 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4836 nfs_node_unlock(dnp
);
4837 if (!error
&& !np
&& fh
.fh_len
) {
4838 /* create the vnode with the filehandle and attributes */
4840 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &newnp
);
4842 newvp
= NFSTOV(newnp
);
4844 NVATTR_CLEANUP(&nvattr
);
4846 nfs_node_clear_busy(dnp
);
4847 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
4850 if (!error
&& np
&& !recall
) {
4851 /* stuff the delegation state in the node */
4852 lck_mtx_lock(&np
->n_openlock
);
4853 np
->n_openflags
&= ~N_DELEG_MASK
;
4854 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4855 np
->n_dstateid
= dstateid
;
4857 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4858 lck_mtx_lock(&nmp
->nm_lock
);
4859 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4860 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4861 lck_mtx_unlock(&nmp
->nm_lock
);
4863 lck_mtx_unlock(&np
->n_openlock
);
4865 /* give the delegation back */
4867 if (NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
4868 /* update delegation state and return it */
4869 lck_mtx_lock(&np
->n_openlock
);
4870 np
->n_openflags
&= ~N_DELEG_MASK
;
4871 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
4872 np
->n_dstateid
= dstateid
;
4874 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
4875 lck_mtx_lock(&nmp
->nm_lock
);
4876 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
4877 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
4878 lck_mtx_unlock(&nmp
->nm_lock
);
4880 lck_mtx_unlock(&np
->n_openlock
);
4881 /* don't need to send a separate delegreturn for fh */
4884 /* return np's current delegation */
4885 nfs4_delegation_return(np
, 0, thd
, cred
);
4887 if (fh
.fh_len
) /* return fh's delegation if it wasn't for np */
4888 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
4892 if (exclusive
&& (error
== NFSERR_NOTSUPP
)) {
4897 nfs_node_unlock(newnp
);
4900 } else if (create
) {
4901 nfs_node_unlock(newnp
);
4903 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
4904 if (error
&& (gotuid
|| gotgid
)) {
4905 /* it's possible the server didn't like our attempt to set IDs. */
4906 /* so, let's try it again without those */
4907 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
4908 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
4909 error
= nfs4_setattr_rpc(newnp
, vap
, ctx
);
4917 nfs_open_owner_clear_busy(noop
);
4923 * Send an OPEN RPC to claim a delegated open for a file
4926 nfs4_claim_delegated_open_rpc(
4927 struct nfs_open_file
*nofp
,
4932 struct nfsmount
*nmp
;
4933 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
4934 struct nfs_vattr nvattr
;
4935 int error
= 0, lockerror
= ENOENT
, status
;
4936 int nfsvers
, numops
;
4938 nfsnode_t np
= nofp
->nof_np
;
4939 struct nfsm_chain nmreq
, nmrep
;
4940 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
4941 uint32_t rflags
= 0, delegation
, recall
= 0;
4943 struct nfs_stateid dstateid
;
4944 char sbuf
[64], *s
= sbuf
;
4945 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
4946 struct kauth_ace ace
;
4948 const char *vname
= NULL
;
4949 const char *name
= NULL
;
4951 char smallname
[128];
4952 char *filename
= NULL
;
4953 struct nfsreq_secinfo_args si
;
4958 nfsvers
= nmp
->nm_vers
;
4960 nfs_node_lock_force(np
);
4961 if ((vnode_vtype(NFSTOV(np
)) != VDIR
) && np
->n_sillyrename
) {
4963 * The node's been sillyrenamed, so we need to use
4964 * the sillyrename directory/name to do the open.
4966 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
4967 dvp
= NFSTOV(nsp
->nsr_dnp
);
4968 if ((error
= vnode_get(dvp
))) {
4969 nfs_node_unlock(np
);
4972 name
= nsp
->nsr_name
;
4975 * [sigh] We can't trust VFS to get the parent right for named
4976 * attribute nodes. (It likes to reparent the nodes after we've
4977 * created them.) Luckily we can probably get the right parent
4978 * from the n_parent we have stashed away.
4980 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
4981 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
))))
4984 dvp
= vnode_getparent(NFSTOV(np
));
4985 vname
= vnode_getname(NFSTOV(np
));
4986 if (!dvp
|| !vname
) {
4989 nfs_node_unlock(np
);
4994 filename
= &smallname
[0];
4995 namelen
= snprintf(filename
, sizeof(smallname
), "%s", name
);
4996 if (namelen
>= sizeof(smallname
)) {
4997 MALLOC(filename
, char *, namelen
+1, M_TEMP
, M_WAITOK
);
5002 snprintf(filename
, namelen
+1, "%s", name
);
5004 nfs_node_unlock(np
);
5006 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5009 NVATTR_INIT(&nvattr
);
5010 delegation
= NFS_OPEN_DELEGATE_NONE
;
5011 dstateid
= np
->n_dstateid
;
5012 NFSREQ_SECINFO_SET(&si
, VTONFS(dvp
), NULL
, 0, filename
, namelen
);
5014 nfsm_chain_null(&nmreq
);
5015 nfsm_chain_null(&nmrep
);
5017 // PUTFH, OPEN, GETATTR(FH)
5019 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5020 nfsm_chain_add_compound_header(error
, &nmreq
, "open_claim_d", numops
);
5022 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5023 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, VTONFS(dvp
)->n_fhp
, VTONFS(dvp
)->n_fhsize
);
5025 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5026 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5027 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5028 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5029 // open owner: clientid + uid
5030 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5031 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5032 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5034 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5036 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_DELEGATE_CUR
);
5037 nfsm_chain_add_stateid(error
, &nmreq
, &np
->n_dstateid
);
5038 nfsm_chain_add_name(error
, &nmreq
, filename
, namelen
, nmp
);
5040 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5041 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5042 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5043 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5044 nfsm_chain_build_done(error
, &nmreq
);
5045 nfsm_assert(error
, (numops
== 0), EPROTO
);
5048 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5049 noop
->noo_cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5051 if ((lockerror
= nfs_node_lock(np
)))
5053 nfsm_chain_skip_tag(error
, &nmrep
);
5054 nfsm_chain_get_32(error
, &nmrep
, numops
);
5055 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5057 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5058 nfs_owner_seqid_increment(noop
, NULL
, error
);
5059 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5060 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5061 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5062 bmlen
= NFS_ATTR_BITMAP_LEN
;
5063 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5064 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5066 switch (delegation
) {
5067 case NFS_OPEN_DELEGATE_NONE
:
5068 // if (!(np->n_openflags & N_DELEG_RETURN)) /* don't warn if delegation is being returned */
5069 // printf("nfs: open delegated claim didn't return a delegation %s\n", filename ? filename : "???");
5071 case NFS_OPEN_DELEGATE_READ
:
5072 case NFS_OPEN_DELEGATE_WRITE
:
5073 if ((((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_READ
) &&
5074 (delegation
== NFS_OPEN_DELEGATE_WRITE
)) ||
5075 (((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) &&
5076 (delegation
== NFS_OPEN_DELEGATE_READ
)))
5077 printf("nfs: open delegated claim returned a different delegation type! have %s got %s %s\n",
5078 ((np
->n_openflags
& N_DELEG_MASK
) == N_DELEG_WRITE
) ? "W" : "R",
5079 (delegation
== NFS_OPEN_DELEGATE_WRITE
) ? "W" : "R", filename
? filename
: "???");
5080 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5081 nfsm_chain_get_32(error
, &nmrep
, recall
);
5082 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5083 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5084 /* if we have any trouble accepting the ACE, just invalidate it */
5085 ace_type
= ace_flags
= ace_mask
= len
= 0;
5086 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5087 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5088 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5089 nfsm_chain_get_32(error
, &nmrep
, len
);
5090 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5091 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5092 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5093 if (!error
&& (len
>= slen
)) {
5094 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5101 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5103 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5106 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5111 if (s
&& (s
!= sbuf
))
5114 /* stuff the latest delegation state in the node */
5115 lck_mtx_lock(&np
->n_openlock
);
5116 np
->n_openflags
&= ~N_DELEG_MASK
;
5117 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5118 np
->n_dstateid
= dstateid
;
5120 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5121 lck_mtx_lock(&nmp
->nm_lock
);
5122 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5123 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5124 lck_mtx_unlock(&nmp
->nm_lock
);
5126 lck_mtx_unlock(&np
->n_openlock
);
5134 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5135 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5137 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5138 printf("nfs: open reclaim didn't return filehandle? %s\n", filename
? filename
: "???");
5142 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5143 // XXX what if fh doesn't match the vnode we think we're re-opening?
5144 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5145 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5146 printf("nfs4_claim_delegated_open_rpc: warning: file handle mismatch %s\n", filename
? filename
: "???");
5148 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5150 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5151 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5153 NVATTR_CLEANUP(&nvattr
);
5154 nfsm_chain_cleanup(&nmreq
);
5155 nfsm_chain_cleanup(&nmrep
);
5157 nfs_node_unlock(np
);
5158 nfs_open_owner_clear_busy(noop
);
5159 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5162 * We're making a delegated claim.
5163 * Don't return the delegation here in case we have more to claim.
5164 * Just make sure it's queued up to be returned.
5166 nfs4_delegation_return_enqueue(np
);
5171 // printf("nfs: open claim delegated (%d, %d) succeeded for %s\n", share_access, share_deny, filename ? filename : "???");
5172 if (filename
&& (filename
!= &smallname
[0]))
5173 FREE(filename
, M_TEMP
);
5175 vnode_putname(vname
);
5182 * Send an OPEN RPC to reclaim an open file.
5185 nfs4_open_reclaim_rpc(
5186 struct nfs_open_file
*nofp
,
5190 struct nfsmount
*nmp
;
5191 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5192 struct nfs_vattr nvattr
;
5193 int error
= 0, lockerror
= ENOENT
, status
;
5194 int nfsvers
, numops
;
5196 nfsnode_t np
= nofp
->nof_np
;
5197 struct nfsm_chain nmreq
, nmrep
;
5198 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
5199 uint32_t rflags
= 0, delegation
, recall
= 0;
5201 struct nfs_stateid dstateid
;
5202 char sbuf
[64], *s
= sbuf
;
5203 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
= sizeof(sbuf
);
5204 struct kauth_ace ace
;
5205 struct nfsreq_secinfo_args si
;
5210 nfsvers
= nmp
->nm_vers
;
5212 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5215 NVATTR_INIT(&nvattr
);
5216 delegation
= NFS_OPEN_DELEGATE_NONE
;
5217 dstateid
= np
->n_dstateid
;
5218 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5220 nfsm_chain_null(&nmreq
);
5221 nfsm_chain_null(&nmrep
);
5223 // PUTFH, OPEN, GETATTR(FH)
5225 nfsm_chain_build_alloc_init(error
, &nmreq
, 48 * NFSX_UNSIGNED
);
5226 nfsm_chain_add_compound_header(error
, &nmreq
, "open_reclaim", numops
);
5228 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5229 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5231 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
5232 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5233 nfsm_chain_add_32(error
, &nmreq
, share_access
);
5234 nfsm_chain_add_32(error
, &nmreq
, share_deny
);
5235 // open owner: clientid + uid
5236 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
); // open_owner4.clientid
5237 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
5238 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
)); // open_owner4.owner
5240 nfsm_chain_add_32(error
, &nmreq
, NFS_OPEN_NOCREATE
);
5242 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_PREVIOUS
);
5243 delegation
= (np
->n_openflags
& N_DELEG_READ
) ? NFS_OPEN_DELEGATE_READ
:
5244 (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
:
5245 NFS_OPEN_DELEGATE_NONE
;
5246 nfsm_chain_add_32(error
, &nmreq
, delegation
);
5247 delegation
= NFS_OPEN_DELEGATE_NONE
;
5249 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5250 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
5251 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
5252 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, np
);
5253 nfsm_chain_build_done(error
, &nmreq
);
5254 nfsm_assert(error
, (numops
== 0), EPROTO
);
5257 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, current_thread(),
5258 noop
->noo_cred
, &si
, R_RECOVER
|R_NOINTR
, &nmrep
, &xid
, &status
);
5260 if ((lockerror
= nfs_node_lock(np
)))
5262 nfsm_chain_skip_tag(error
, &nmrep
);
5263 nfsm_chain_get_32(error
, &nmrep
, numops
);
5264 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5266 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
5267 nfs_owner_seqid_increment(noop
, NULL
, error
);
5268 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5269 nfsm_chain_check_change_info(error
, &nmrep
, np
);
5270 nfsm_chain_get_32(error
, &nmrep
, rflags
);
5271 bmlen
= NFS_ATTR_BITMAP_LEN
;
5272 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
5273 nfsm_chain_get_32(error
, &nmrep
, delegation
);
5275 switch (delegation
) {
5276 case NFS_OPEN_DELEGATE_NONE
:
5277 if (np
->n_openflags
& N_DELEG_MASK
) {
5279 * Hey! We were supposed to get our delegation back even
5280 * if it was getting immediately recalled. Bad server!
5282 * Just try to return the existing delegation.
5284 // NP(np, "nfs: open reclaim didn't return delegation?");
5285 delegation
= (np
->n_openflags
& N_DELEG_WRITE
) ? NFS_OPEN_DELEGATE_WRITE
: NFS_OPEN_DELEGATE_READ
;
5289 case NFS_OPEN_DELEGATE_READ
:
5290 case NFS_OPEN_DELEGATE_WRITE
:
5291 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
5292 nfsm_chain_get_32(error
, &nmrep
, recall
);
5293 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
5294 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
5295 /* if we have any trouble accepting the ACE, just invalidate it */
5296 ace_type
= ace_flags
= ace_mask
= len
= 0;
5297 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
5298 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
5299 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
5300 nfsm_chain_get_32(error
, &nmrep
, len
);
5301 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
5302 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
5303 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
5304 if (!error
&& (len
>= slen
)) {
5305 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
5312 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
5314 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
5317 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
5322 if (s
&& (s
!= sbuf
))
5325 /* stuff the delegation state in the node */
5326 lck_mtx_lock(&np
->n_openlock
);
5327 np
->n_openflags
&= ~N_DELEG_MASK
;
5328 np
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
5329 np
->n_dstateid
= dstateid
;
5331 if (np
->n_dlink
.tqe_next
== NFSNOLIST
) {
5332 lck_mtx_lock(&nmp
->nm_lock
);
5333 if (np
->n_dlink
.tqe_next
== NFSNOLIST
)
5334 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, np
, n_dlink
);
5335 lck_mtx_unlock(&nmp
->nm_lock
);
5337 lck_mtx_unlock(&np
->n_openlock
);
5345 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5346 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
5348 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
5349 NP(np
, "nfs: open reclaim didn't return filehandle?");
5353 if (!NFS_CMPFH(np
, fh
.fh_data
, fh
.fh_len
)) {
5354 // XXX what if fh doesn't match the vnode we think we're re-opening?
5355 // That should be pretty hard in this case, given that we are doing
5356 // the open reclaim using the file handle (and not a dir/name pair).
5357 // Solaris Named Attributes may do this due to a bug.... so don't warn for named attributes.
5358 if (!(np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
5359 NP(np
, "nfs4_open_reclaim_rpc: warning: file handle mismatch");
5361 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 1);
5363 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
5364 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
5367 // NP(np, "nfs: open reclaim (%d, %d) succeeded", share_access, share_deny);
5368 NVATTR_CLEANUP(&nvattr
);
5369 nfsm_chain_cleanup(&nmreq
);
5370 nfsm_chain_cleanup(&nmrep
);
5372 nfs_node_unlock(np
);
5373 nfs_open_owner_clear_busy(noop
);
5374 if ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
)) {
5376 nfs4_delegation_return_enqueue(np
);
5382 nfs4_open_downgrade_rpc(
5384 struct nfs_open_file
*nofp
,
5387 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5388 struct nfsmount
*nmp
;
5389 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5390 struct nfsm_chain nmreq
, nmrep
;
5392 struct nfsreq_secinfo_args si
;
5397 nfsvers
= nmp
->nm_vers
;
5399 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5402 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5403 nfsm_chain_null(&nmreq
);
5404 nfsm_chain_null(&nmrep
);
5406 // PUTFH, OPEN_DOWNGRADE, GETATTR
5408 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5409 nfsm_chain_add_compound_header(error
, &nmreq
, "open_downgrd", numops
);
5411 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5412 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5414 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN_DOWNGRADE
);
5415 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5416 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5417 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_access
);
5418 nfsm_chain_add_32(error
, &nmreq
, nofp
->nof_deny
);
5420 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5421 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5422 nfsm_chain_build_done(error
, &nmreq
);
5423 nfsm_assert(error
, (numops
== 0), EPROTO
);
5425 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
5426 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
5427 &si
, R_NOINTR
, &nmrep
, &xid
, &status
);
5429 if ((lockerror
= nfs_node_lock(np
)))
5431 nfsm_chain_skip_tag(error
, &nmrep
);
5432 nfsm_chain_get_32(error
, &nmrep
, numops
);
5433 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5435 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN_DOWNGRADE
);
5436 nfs_owner_seqid_increment(noop
, NULL
, error
);
5437 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5438 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5439 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5442 nfs_node_unlock(np
);
5443 nfs_open_owner_clear_busy(noop
);
5444 nfsm_chain_cleanup(&nmreq
);
5445 nfsm_chain_cleanup(&nmrep
);
5452 struct nfs_open_file
*nofp
,
5457 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5458 struct nfsmount
*nmp
;
5459 int error
, lockerror
= ENOENT
, status
, nfsvers
, numops
;
5460 struct nfsm_chain nmreq
, nmrep
;
5462 struct nfsreq_secinfo_args si
;
5467 nfsvers
= nmp
->nm_vers
;
5469 if ((error
= nfs_open_owner_set_busy(noop
, NULL
)))
5472 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
5473 nfsm_chain_null(&nmreq
);
5474 nfsm_chain_null(&nmrep
);
5476 // PUTFH, CLOSE, GETATTR
5478 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
5479 nfsm_chain_add_compound_header(error
, &nmreq
, "close", numops
);
5481 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5482 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5484 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CLOSE
);
5485 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
5486 nfsm_chain_add_stateid(error
, &nmreq
, &nofp
->nof_stateid
);
5488 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
5489 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
5490 nfsm_chain_build_done(error
, &nmreq
);
5491 nfsm_assert(error
, (numops
== 0), EPROTO
);
5493 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
|R_NOINTR
, &nmrep
, &xid
, &status
);
5495 if ((lockerror
= nfs_node_lock(np
)))
5497 nfsm_chain_skip_tag(error
, &nmrep
);
5498 nfsm_chain_get_32(error
, &nmrep
, numops
);
5499 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5501 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CLOSE
);
5502 nfs_owner_seqid_increment(noop
, NULL
, error
);
5503 nfsm_chain_get_stateid(error
, &nmrep
, &nofp
->nof_stateid
);
5504 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
5505 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
5508 nfs_node_unlock(np
);
5509 nfs_open_owner_clear_busy(noop
);
5510 nfsm_chain_cleanup(&nmreq
);
5511 nfsm_chain_cleanup(&nmrep
);
5517 * Claim the delegated open combinations this open file holds.
5520 nfs4_claim_delegated_state_for_open_file(struct nfs_open_file
*nofp
, int flags
)
5522 struct nfs_open_owner
*noop
= nofp
->nof_owner
;
5523 struct nfs_lock_owner
*nlop
;
5524 struct nfs_file_lock
*nflp
, *nextnflp
;
5525 struct nfsmount
*nmp
;
5526 int error
= 0, reopen
= 0;
5528 if (nofp
->nof_d_rw_drw
) {
5529 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5531 lck_mtx_lock(&nofp
->nof_lock
);
5532 nofp
->nof_rw_drw
+= nofp
->nof_d_rw_drw
;
5533 nofp
->nof_d_rw_drw
= 0;
5534 lck_mtx_unlock(&nofp
->nof_lock
);
5537 if (!error
&& nofp
->nof_d_w_drw
) {
5538 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5540 lck_mtx_lock(&nofp
->nof_lock
);
5541 nofp
->nof_w_drw
+= nofp
->nof_d_w_drw
;
5542 nofp
->nof_d_w_drw
= 0;
5543 lck_mtx_unlock(&nofp
->nof_lock
);
5546 if (!error
&& nofp
->nof_d_r_drw
) {
5547 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_BOTH
, flags
);
5549 lck_mtx_lock(&nofp
->nof_lock
);
5550 nofp
->nof_r_drw
+= nofp
->nof_d_r_drw
;
5551 nofp
->nof_d_r_drw
= 0;
5552 lck_mtx_unlock(&nofp
->nof_lock
);
5555 if (!error
&& nofp
->nof_d_rw_dw
) {
5556 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5558 lck_mtx_lock(&nofp
->nof_lock
);
5559 nofp
->nof_rw_dw
+= nofp
->nof_d_rw_dw
;
5560 nofp
->nof_d_rw_dw
= 0;
5561 lck_mtx_unlock(&nofp
->nof_lock
);
5564 if (!error
&& nofp
->nof_d_w_dw
) {
5565 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5567 lck_mtx_lock(&nofp
->nof_lock
);
5568 nofp
->nof_w_dw
+= nofp
->nof_d_w_dw
;
5569 nofp
->nof_d_w_dw
= 0;
5570 lck_mtx_unlock(&nofp
->nof_lock
);
5573 if (!error
&& nofp
->nof_d_r_dw
) {
5574 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_WRITE
, flags
);
5576 lck_mtx_lock(&nofp
->nof_lock
);
5577 nofp
->nof_r_dw
+= nofp
->nof_d_r_dw
;
5578 nofp
->nof_d_r_dw
= 0;
5579 lck_mtx_unlock(&nofp
->nof_lock
);
5582 /* non-deny-mode opens may be reopened if no locks are held */
5583 if (!error
&& nofp
->nof_d_rw
) {
5584 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5585 /* for some errors, we should just try reopening the file */
5586 if (nfs_mount_state_error_delegation_lost(error
))
5588 if (!error
|| reopen
) {
5589 lck_mtx_lock(&nofp
->nof_lock
);
5590 nofp
->nof_rw
+= nofp
->nof_d_rw
;
5592 lck_mtx_unlock(&nofp
->nof_lock
);
5595 /* if we've already set reopen, we should move these other two opens from delegated to not delegated */
5596 if ((!error
|| reopen
) && nofp
->nof_d_w
) {
5598 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5599 /* for some errors, we should just try reopening the file */
5600 if (nfs_mount_state_error_delegation_lost(error
))
5603 if (!error
|| reopen
) {
5604 lck_mtx_lock(&nofp
->nof_lock
);
5605 nofp
->nof_w
+= nofp
->nof_d_w
;
5607 lck_mtx_unlock(&nofp
->nof_lock
);
5610 if ((!error
|| reopen
) && nofp
->nof_d_r
) {
5612 error
= nfs4_claim_delegated_open_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, flags
);
5613 /* for some errors, we should just try reopening the file */
5614 if (nfs_mount_state_error_delegation_lost(error
))
5617 if (!error
|| reopen
) {
5618 lck_mtx_lock(&nofp
->nof_lock
);
5619 nofp
->nof_r
+= nofp
->nof_d_r
;
5621 lck_mtx_unlock(&nofp
->nof_lock
);
5627 * Any problems with the delegation probably indicates that we
5628 * should review/return all of our current delegation state.
5630 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5631 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5632 lck_mtx_lock(&nmp
->nm_lock
);
5633 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5634 lck_mtx_unlock(&nmp
->nm_lock
);
5636 if (reopen
&& (nfs_check_for_locks(noop
, nofp
) == 0)) {
5637 /* just reopen the file on next access */
5638 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, need reopen, %d",
5639 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5640 lck_mtx_lock(&nofp
->nof_lock
);
5641 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPEN
;
5642 lck_mtx_unlock(&nofp
->nof_lock
);
5646 NP(nofp
->nof_np
, "nfs4_claim_delegated_state_for_open_file: %d, locks prevent reopen, %d",
5647 reopen
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5650 if (!error
&& ((nmp
= NFSTONMP(nofp
->nof_np
)))) {
5651 /* claim delegated locks */
5652 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
5653 if (nlop
->nlo_open_owner
!= noop
)
5655 TAILQ_FOREACH_SAFE(nflp
, &nlop
->nlo_locks
, nfl_lolink
, nextnflp
) {
5656 /* skip dead & blocked lock requests (shouldn't be any in the held lock list) */
5657 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5659 /* skip non-delegated locks */
5660 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5662 error
= nmp
->nm_funcs
->nf_setlock_rpc(nofp
->nof_np
, nofp
, nflp
, 0, flags
, current_thread(), noop
->noo_cred
);
5664 NP(nofp
->nof_np
, "nfs: delegated lock claim (0x%llx, 0x%llx) failed %d, %d",
5665 nflp
->nfl_start
, nflp
->nfl_end
, error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5669 // NP(nofp->nof_np, "nfs: delegated lock claim (0x%llx, 0x%llx) succeeded, %d",
5670 // nflp->nfl_start, nflp->nfl_end, kauth_cred_getuid(nofp->nof_owner->noo_cred));
5678 if (!error
) /* all state claimed successfully! */
5681 /* restart if it looks like a problem more than just losing the delegation */
5682 if (!nfs_mount_state_error_delegation_lost(error
) &&
5683 ((error
== ETIMEDOUT
) || nfs_mount_state_error_should_restart(error
))) {
5684 NP(nofp
->nof_np
, "nfs delegated lock claim error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5685 if ((error
== ETIMEDOUT
) && ((nmp
= NFSTONMP(nofp
->nof_np
))))
5686 nfs_need_reconnect(nmp
);
5690 /* delegated state lost (once held but now not claimable) */
5691 NP(nofp
->nof_np
, "nfs delegated state claim error %d, state lost, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
5694 * Any problems with the delegation probably indicates that we
5695 * should review/return all of our current delegation state.
5697 if ((nmp
= NFSTONMP(nofp
->nof_np
))) {
5698 nfs4_delegation_return_enqueue(nofp
->nof_np
);
5699 lck_mtx_lock(&nmp
->nm_lock
);
5700 nfs_need_recover(nmp
, NFSERR_EXPIRED
);
5701 lck_mtx_unlock(&nmp
->nm_lock
);
5704 /* revoke all open file state */
5705 nfs_revoke_open_state_for_node(nofp
->nof_np
);
5711 * Release all open state for the given node.
5714 nfs_release_open_state_for_node(nfsnode_t np
, int force
)
5716 struct nfsmount
*nmp
= NFSTONMP(np
);
5717 struct nfs_open_file
*nofp
;
5718 struct nfs_file_lock
*nflp
, *nextnflp
;
5720 /* drop held locks */
5721 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
5722 /* skip dead & blocked lock requests */
5723 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
|NFS_FILE_LOCK_BLOCKED
))
5725 /* send an unlock if not a delegated lock */
5726 if (!force
&& nmp
&& !(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
))
5727 nmp
->nm_funcs
->nf_unlock_rpc(np
, nflp
->nfl_owner
, F_WRLCK
, nflp
->nfl_start
, nflp
->nfl_end
, R_RECOVER
,
5728 NULL
, nflp
->nfl_owner
->nlo_open_owner
->noo_cred
);
5729 /* kill/remove the lock */
5730 lck_mtx_lock(&np
->n_openlock
);
5731 nflp
->nfl_flags
|= NFS_FILE_LOCK_DEAD
;
5732 lck_mtx_lock(&nflp
->nfl_owner
->nlo_lock
);
5733 TAILQ_REMOVE(&nflp
->nfl_owner
->nlo_locks
, nflp
, nfl_lolink
);
5734 lck_mtx_unlock(&nflp
->nfl_owner
->nlo_lock
);
5735 if (nflp
->nfl_blockcnt
) {
5736 /* wake up anyone blocked on this lock */
5739 /* remove nflp from lock list and destroy */
5740 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
5741 nfs_file_lock_destroy(nflp
);
5743 lck_mtx_unlock(&np
->n_openlock
);
5746 lck_mtx_lock(&np
->n_openlock
);
5748 /* drop all opens */
5749 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5750 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)
5752 /* mark open state as lost */
5753 lck_mtx_lock(&nofp
->nof_lock
);
5754 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
5755 nofp
->nof_flags
|= NFS_OPEN_FILE_LOST
;
5756 lck_mtx_unlock(&nofp
->nof_lock
);
5757 if (!force
&& nmp
&& (nmp
->nm_vers
>= NFS_VER4
))
5758 nfs4_close_rpc(np
, nofp
, NULL
, nofp
->nof_owner
->noo_cred
, R_RECOVER
);
5761 lck_mtx_unlock(&np
->n_openlock
);
5765 * State for a node has been lost, drop it, and revoke the node.
5766 * Attempt to return any state if possible in case the server
5767 * might somehow think we hold it.
5770 nfs_revoke_open_state_for_node(nfsnode_t np
)
5772 struct nfsmount
*nmp
;
5774 /* mark node as needing to be revoked */
5775 nfs_node_lock_force(np
);
5776 if (np
->n_flag
& NREVOKE
) /* already revoked? */
5778 NP(np
, "nfs_revoke_open_state_for_node(): already revoked");
5779 nfs_node_unlock(np
);
5782 np
->n_flag
|= NREVOKE
;
5783 nfs_node_unlock(np
);
5785 nfs_release_open_state_for_node(np
, 0);
5786 NP(np
, "nfs: state lost for %p 0x%x", np
, np
->n_flag
);
5788 /* mark mount as needing a revoke scan and have the socket thread do it. */
5789 if ((nmp
= NFSTONMP(np
))) {
5790 lck_mtx_lock(&nmp
->nm_lock
);
5791 nmp
->nm_state
|= NFSSTA_REVOKE
;
5792 nfs_mount_sock_thread_wake(nmp
);
5793 lck_mtx_unlock(&nmp
->nm_lock
);
5798 * Claim the delegated open combinations that each of this node's open files hold.
5801 nfs4_claim_delegated_state_for_node(nfsnode_t np
, int flags
)
5803 struct nfs_open_file
*nofp
;
5806 lck_mtx_lock(&np
->n_openlock
);
5808 /* walk the open file list looking for opens with delegated state to claim */
5810 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
5811 if (!nofp
->nof_d_rw_drw
&& !nofp
->nof_d_w_drw
&& !nofp
->nof_d_r_drw
&&
5812 !nofp
->nof_d_rw_dw
&& !nofp
->nof_d_w_dw
&& !nofp
->nof_d_r_dw
&&
5813 !nofp
->nof_d_rw
&& !nofp
->nof_d_w
&& !nofp
->nof_d_r
)
5815 lck_mtx_unlock(&np
->n_openlock
);
5816 error
= nfs4_claim_delegated_state_for_open_file(nofp
, flags
);
5817 lck_mtx_lock(&np
->n_openlock
);
5823 lck_mtx_unlock(&np
->n_openlock
);
5829 * Mark a node as needed to have its delegation returned.
5830 * Queue it up on the delegation return queue.
5831 * Make sure the thread is running.
5834 nfs4_delegation_return_enqueue(nfsnode_t np
)
5836 struct nfsmount
*nmp
;
5842 lck_mtx_lock(&np
->n_openlock
);
5843 np
->n_openflags
|= N_DELEG_RETURN
;
5844 lck_mtx_unlock(&np
->n_openlock
);
5846 lck_mtx_lock(&nmp
->nm_lock
);
5847 if (np
->n_dreturn
.tqe_next
== NFSNOLIST
)
5848 TAILQ_INSERT_TAIL(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5849 nfs_mount_sock_thread_wake(nmp
);
5850 lck_mtx_unlock(&nmp
->nm_lock
);
5854 * return any delegation we may have for the given node
5857 nfs4_delegation_return(nfsnode_t np
, int flags
, thread_t thd
, kauth_cred_t cred
)
5859 struct nfsmount
*nmp
;
5861 nfs_stateid dstateid
;
5868 /* first, make sure the node's marked for delegation return */
5869 lck_mtx_lock(&np
->n_openlock
);
5870 np
->n_openflags
|= (N_DELEG_RETURN
|N_DELEG_RETURNING
);
5871 lck_mtx_unlock(&np
->n_openlock
);
5873 /* make sure nobody else is using the delegation state */
5874 if ((error
= nfs_open_state_set_busy(np
, NULL
)))
5877 /* claim any delegated state */
5878 if ((error
= nfs4_claim_delegated_state_for_node(np
, flags
)))
5881 /* return the delegation */
5882 lck_mtx_lock(&np
->n_openlock
);
5883 dstateid
= np
->n_dstateid
;
5884 fh
.fh_len
= np
->n_fhsize
;
5885 bcopy(np
->n_fhp
, &fh
.fh_data
, fh
.fh_len
);
5886 lck_mtx_unlock(&np
->n_openlock
);
5887 error
= nfs4_delegreturn_rpc(NFSTONMP(np
), fh
.fh_data
, fh
.fh_len
, &dstateid
, flags
, thd
, cred
);
5888 /* assume delegation is gone for all errors except ETIMEDOUT, NFSERR_*MOVED */
5889 if ((error
!= ETIMEDOUT
) && (error
!= NFSERR_MOVED
) && (error
!= NFSERR_LEASE_MOVED
)) {
5890 lck_mtx_lock(&np
->n_openlock
);
5891 np
->n_openflags
&= ~N_DELEG_MASK
;
5892 lck_mtx_lock(&nmp
->nm_lock
);
5893 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
5894 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
5895 np
->n_dlink
.tqe_next
= NFSNOLIST
;
5897 lck_mtx_unlock(&nmp
->nm_lock
);
5898 lck_mtx_unlock(&np
->n_openlock
);
5902 /* make sure it's no longer on the return queue and clear the return flags */
5903 lck_mtx_lock(&nmp
->nm_lock
);
5904 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
5905 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
5906 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
5908 lck_mtx_unlock(&nmp
->nm_lock
);
5909 lck_mtx_lock(&np
->n_openlock
);
5910 np
->n_openflags
&= ~(N_DELEG_RETURN
|N_DELEG_RETURNING
);
5911 lck_mtx_unlock(&np
->n_openlock
);
5914 NP(np
, "nfs4_delegation_return, error %d", error
);
5915 if (error
== ETIMEDOUT
)
5916 nfs_need_reconnect(nmp
);
5917 if (nfs_mount_state_error_should_restart(error
)) {
5918 /* make sure recovery happens */
5919 lck_mtx_lock(&nmp
->nm_lock
);
5920 nfs_need_recover(nmp
, nfs_mount_state_error_delegation_lost(error
) ? NFSERR_EXPIRED
: 0);
5921 lck_mtx_unlock(&nmp
->nm_lock
);
5925 nfs_open_state_clear_busy(np
);
5931 * RPC to return a delegation for a file handle
5934 nfs4_delegreturn_rpc(struct nfsmount
*nmp
, u_char
*fhp
, int fhlen
, struct nfs_stateid
*sid
, int flags
, thread_t thd
, kauth_cred_t cred
)
5936 int error
= 0, status
, numops
;
5938 struct nfsm_chain nmreq
, nmrep
;
5939 struct nfsreq_secinfo_args si
;
5941 NFSREQ_SECINFO_SET(&si
, NULL
, fhp
, fhlen
, NULL
, 0);
5942 nfsm_chain_null(&nmreq
);
5943 nfsm_chain_null(&nmrep
);
5945 // PUTFH, DELEGRETURN
5947 nfsm_chain_build_alloc_init(error
, &nmreq
, 16 * NFSX_UNSIGNED
);
5948 nfsm_chain_add_compound_header(error
, &nmreq
, "delegreturn", numops
);
5950 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
5951 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, fhp
, fhlen
);
5953 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_DELEGRETURN
);
5954 nfsm_chain_add_stateid(error
, &nmreq
, sid
);
5955 nfsm_chain_build_done(error
, &nmreq
);
5956 nfsm_assert(error
, (numops
== 0), EPROTO
);
5958 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, &si
, flags
, &nmrep
, &xid
, &status
);
5959 nfsm_chain_skip_tag(error
, &nmrep
);
5960 nfsm_chain_get_32(error
, &nmrep
, numops
);
5961 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
5962 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_DELEGRETURN
);
5964 nfsm_chain_cleanup(&nmreq
);
5965 nfsm_chain_cleanup(&nmrep
);
5972 * Just call nfs_bioread() to do the work.
5974 * Note: the exec code paths have a tendency to call VNOP_READ (and VNOP_MMAP)
5975 * without first calling VNOP_OPEN, so we make sure the file is open here.
5979 struct vnop_read_args
/* {
5980 struct vnodeop_desc *a_desc;
5984 vfs_context_t a_context;
5987 vnode_t vp
= ap
->a_vp
;
5988 vfs_context_t ctx
= ap
->a_context
;
5990 struct nfsmount
*nmp
;
5991 struct nfs_open_owner
*noop
;
5992 struct nfs_open_file
*nofp
;
5995 if (vnode_vtype(ap
->a_vp
) != VREG
)
6002 if (np
->n_flag
& NREVOKE
)
6005 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6009 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
6010 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6011 NP(np
, "nfs_vnop_read: LOST %d", kauth_cred_getuid(noop
->noo_cred
));
6014 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6015 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
6021 nfs_open_owner_rele(noop
);
6024 if (!nofp
->nof_access
) {
6025 /* we don't have the file open, so open it for read access */
6026 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6028 nfs_open_owner_rele(noop
);
6031 if (np
->n_flag
& NREVOKE
) {
6033 nfs_mount_state_in_use_end(nmp
, 0);
6034 nfs_open_owner_rele(noop
);
6037 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
6041 if (nmp
->nm_vers
< NFS_VER4
) {
6042 /* NFS v2/v3 opens are always allowed - so just add it. */
6043 nfs_open_file_add_open(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, 0);
6045 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
6049 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
6051 nfs_open_file_clear_busy(nofp
);
6052 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6057 nfs_open_owner_rele(noop
);
6060 return (nfs_bioread(VTONFS(ap
->a_vp
), ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
));
6064 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6065 * Files are created using the NFSv4 OPEN RPC. So we must open the
6066 * file to create it and then close it.
6070 struct vnop_create_args
/* {
6071 struct vnodeop_desc *a_desc;
6074 struct componentname *a_cnp;
6075 struct vnode_attr *a_vap;
6076 vfs_context_t a_context;
6079 vfs_context_t ctx
= ap
->a_context
;
6080 struct componentname
*cnp
= ap
->a_cnp
;
6081 struct vnode_attr
*vap
= ap
->a_vap
;
6082 vnode_t dvp
= ap
->a_dvp
;
6083 vnode_t
*vpp
= ap
->a_vpp
;
6084 struct nfsmount
*nmp
;
6086 int error
= 0, busyerror
= 0, accessMode
, denyMode
;
6087 struct nfs_open_owner
*noop
= NULL
;
6088 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
6095 nfs_avoid_needless_id_setting_on_create(VTONFS(dvp
), vap
, ctx
);
6097 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6102 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
6104 nfs_open_owner_rele(noop
);
6108 /* grab a provisional, nodeless open file */
6109 error
= nfs_open_file_find(NULL
, noop
, &newnofp
, 0, 0, 1);
6110 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
6111 printf("nfs_vnop_create: LOST\n");
6114 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
6115 /* This shouldn't happen given that this is a new, nodeless nofp */
6116 nfs_mount_state_in_use_end(nmp
, 0);
6117 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
6118 nfs_open_file_destroy(newnofp
);
6124 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
6127 nfs_open_file_destroy(newnofp
);
6133 * We're just trying to create the file.
6134 * We'll create/open it RW, and set NFS_OPEN_FILE_CREATE.
6136 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
6137 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
6139 /* Do the open/create */
6140 error
= nfs4_open_rpc(newnofp
, ctx
, cnp
, vap
, dvp
, vpp
, NFS_OPEN_CREATE
, accessMode
, denyMode
);
6141 if ((error
== EACCES
) && vap
&& !(vap
->va_vaflags
& VA_EXCLUSIVE
) &&
6142 VATTR_IS_ACTIVE(vap
, va_mode
) && !(vap
->va_mode
& S_IWUSR
)) {
6144 * Hmm... it looks like we may have a situation where the request was
6145 * retransmitted because we didn't get the first response which successfully
6146 * created/opened the file and then the second time we were denied the open
6147 * because the mode the file was created with doesn't allow write access.
6149 * We'll try to work around this by temporarily updating the mode and
6150 * retrying the open.
6152 struct vnode_attr vattr
;
6154 /* first make sure it's there */
6155 int error2
= nfs_lookitup(VTONFS(dvp
), cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6156 if (!error2
&& np
) {
6157 nfs_node_unlock(np
);
6159 if (vnode_vtype(NFSTOV(np
)) == VREG
) {
6161 VATTR_SET(&vattr
, va_mode
, (vap
->va_mode
| S_IWUSR
));
6162 if (!nfs4_setattr_rpc(np
, &vattr
, ctx
)) {
6163 error2
= nfs4_open_rpc(newnofp
, ctx
, cnp
, NULL
, dvp
, vpp
, NFS_OPEN_NOCREATE
, accessMode
, denyMode
);
6165 VATTR_SET(&vattr
, va_mode
, vap
->va_mode
);
6166 nfs4_setattr_rpc(np
, &vattr
, ctx
);
6177 if (!error
&& !*vpp
) {
6178 printf("nfs4_open_rpc returned without a node?\n");
6179 /* Hmmm... with no node, we have no filehandle and can't close it */
6183 /* need to cleanup our temporary nofp */
6184 nfs_open_file_clear_busy(newnofp
);
6185 nfs_open_file_destroy(newnofp
);
6189 /* After we have a node, add our open file struct to the node */
6191 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
6193 error
= nfs_open_file_find_internal(np
, noop
, &nofp
, 0, 0, 0);
6195 /* This shouldn't happen, because we passed in a new nofp to use. */
6196 printf("nfs_open_file_find_internal failed! %d\n", error
);
6198 } else if (nofp
!= newnofp
) {
6200 * Hmm... an open file struct already exists.
6201 * Mark the existing one busy and merge our open into it.
6202 * Then destroy the one we created.
6203 * Note: there's no chance of an open confict because the
6204 * open has already been granted.
6206 busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
6207 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
6208 nofp
->nof_stateid
= newnofp
->nof_stateid
;
6209 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
6210 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
6211 nfs_open_file_clear_busy(newnofp
);
6212 nfs_open_file_destroy(newnofp
);
6215 /* mark the node as holding a create-initiated open */
6216 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
6217 nofp
->nof_creator
= current_thread();
6219 if (nofp
&& !busyerror
)
6220 nfs_open_file_clear_busy(nofp
);
6221 if (nfs_mount_state_in_use_end(nmp
, error
)) {
6222 nofp
= newnofp
= NULL
;
6227 nfs_open_owner_rele(noop
);
6232 * Note: the NFSv4 CREATE RPC is for everything EXCEPT regular files.
6238 struct componentname
*cnp
,
6239 struct vnode_attr
*vap
,
6244 struct nfsmount
*nmp
;
6245 struct nfs_vattr nvattr
;
6246 int error
= 0, create_error
= EIO
, lockerror
= ENOENT
, busyerror
= ENOENT
, status
;
6247 int nfsvers
, namedattrs
, numops
;
6248 u_int64_t xid
, savedxid
= 0;
6249 nfsnode_t np
= NULL
;
6250 vnode_t newvp
= NULL
;
6251 struct nfsm_chain nmreq
, nmrep
;
6252 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
6256 struct nfsreq rq
, *req
= &rq
;
6257 struct nfs_dulookup dul
;
6258 struct nfsreq_secinfo_args si
;
6260 nmp
= NFSTONMP(dnp
);
6263 nfsvers
= nmp
->nm_vers
;
6264 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6265 if (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6268 sd
.specdata1
= sd
.specdata2
= 0;
6277 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
6279 sd
.specdata1
= major(vap
->va_rdev
);
6280 sd
.specdata2
= minor(vap
->va_rdev
);
6293 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
6295 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
6297 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6299 NFSREQ_SECINFO_SET(&si
, dnp
, NULL
, 0, NULL
, 0);
6300 NVATTR_INIT(&nvattr
);
6301 nfsm_chain_null(&nmreq
);
6302 nfsm_chain_null(&nmrep
);
6304 // PUTFH, SAVEFH, CREATE, GETATTR(FH), RESTOREFH, GETATTR
6306 nfsm_chain_build_alloc_init(error
, &nmreq
, 66 * NFSX_UNSIGNED
);
6307 nfsm_chain_add_compound_header(error
, &nmreq
, tag
, numops
);
6309 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6310 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6312 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6314 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_CREATE
);
6315 nfsm_chain_add_32(error
, &nmreq
, type
);
6316 if (type
== NFLNK
) {
6317 nfsm_chain_add_name(error
, &nmreq
, link
, strlen(link
), nmp
);
6318 } else if ((type
== NFBLK
) || (type
== NFCHR
)) {
6319 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata1
);
6320 nfsm_chain_add_32(error
, &nmreq
, sd
.specdata2
);
6322 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6323 nfsm_chain_add_fattr4(error
, &nmreq
, vap
, nmp
);
6325 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6326 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6327 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6328 nfsm_chain_add_bitmap_supported(error
, &nmreq
, bitmap
, nmp
, NULL
);
6330 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6332 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6333 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, dnp
);
6334 nfsm_chain_build_done(error
, &nmreq
);
6335 nfsm_assert(error
, (numops
== 0), EPROTO
);
6338 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6339 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6342 nfs_dulookup_start(&dul
, dnp
, ctx
);
6343 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6346 if ((lockerror
= nfs_node_lock(dnp
)))
6348 nfsm_chain_skip_tag(error
, &nmrep
);
6349 nfsm_chain_get_32(error
, &nmrep
, numops
);
6350 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6351 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6353 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_CREATE
);
6354 nfsm_chain_check_change_info(error
, &nmrep
, dnp
);
6355 bmlen
= NFS_ATTR_BITMAP_LEN
;
6356 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
6357 /* At this point if we have no error, the object was created. */
6358 /* if we don't get attributes, then we should lookitup. */
6359 create_error
= error
;
6361 nfs_vattr_set_supported(bitmap
, vap
);
6362 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6364 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6366 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
)) {
6367 printf("nfs: create/%s didn't return filehandle? %s\n", tag
, cnp
->cn_nameptr
);
6371 /* directory attributes: if we don't get them, make sure to invalidate */
6372 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6373 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6375 nfsm_chain_loadattr(error
, &nmrep
, dnp
, nfsvers
, &xid
);
6377 NATTRINVALIDATE(dnp
);
6380 nfsm_chain_cleanup(&nmreq
);
6381 nfsm_chain_cleanup(&nmrep
);
6384 if (!create_error
&& (dnp
->n_flag
& NNEGNCENTRIES
)) {
6385 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6386 cache_purge_negatives(NFSTOV(dnp
));
6388 dnp
->n_flag
|= NMODIFIED
;
6389 nfs_node_unlock(dnp
);
6390 /* nfs_getattr() will check changed and purge caches */
6391 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6394 if (!error
&& fh
.fh_len
) {
6395 /* create the vnode with the filehandle and attributes */
6397 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6401 NVATTR_CLEANUP(&nvattr
);
6404 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6407 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
6408 * if we can succeed in looking up the object.
6410 if ((create_error
== EEXIST
) || (!create_error
&& !newvp
)) {
6411 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
6414 if (vnode_vtype(newvp
) != nfstov_type(type
, nfsvers
))
6419 nfs_node_clear_busy(dnp
);
6422 nfs_node_unlock(np
);
6426 nfs_node_unlock(np
);
6434 struct vnop_mknod_args
/* {
6435 struct vnodeop_desc *a_desc;
6438 struct componentname *a_cnp;
6439 struct vnode_attr *a_vap;
6440 vfs_context_t a_context;
6443 nfsnode_t np
= NULL
;
6444 struct nfsmount
*nmp
;
6447 nmp
= VTONMP(ap
->a_dvp
);
6451 if (!VATTR_IS_ACTIVE(ap
->a_vap
, va_type
))
6453 switch (ap
->a_vap
->va_type
) {
6463 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6464 vtonfs_type(ap
->a_vap
->va_type
, nmp
->nm_vers
), NULL
, &np
);
6466 *ap
->a_vpp
= NFSTOV(np
);
6472 struct vnop_mkdir_args
/* {
6473 struct vnodeop_desc *a_desc;
6476 struct componentname *a_cnp;
6477 struct vnode_attr *a_vap;
6478 vfs_context_t a_context;
6481 nfsnode_t np
= NULL
;
6484 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6487 *ap
->a_vpp
= NFSTOV(np
);
6493 struct vnop_symlink_args
/* {
6494 struct vnodeop_desc *a_desc;
6497 struct componentname *a_cnp;
6498 struct vnode_attr *a_vap;
6500 vfs_context_t a_context;
6503 nfsnode_t np
= NULL
;
6506 error
= nfs4_create_rpc(ap
->a_context
, VTONFS(ap
->a_dvp
), ap
->a_cnp
, ap
->a_vap
,
6507 NFLNK
, ap
->a_target
, &np
);
6509 *ap
->a_vpp
= NFSTOV(np
);
6515 struct vnop_link_args
/* {
6516 struct vnodeop_desc *a_desc;
6519 struct componentname *a_cnp;
6520 vfs_context_t a_context;
6523 vfs_context_t ctx
= ap
->a_context
;
6524 vnode_t vp
= ap
->a_vp
;
6525 vnode_t tdvp
= ap
->a_tdvp
;
6526 struct componentname
*cnp
= ap
->a_cnp
;
6527 int error
= 0, lockerror
= ENOENT
, status
;
6528 struct nfsmount
*nmp
;
6529 nfsnode_t np
= VTONFS(vp
);
6530 nfsnode_t tdnp
= VTONFS(tdvp
);
6531 int nfsvers
, numops
;
6532 u_int64_t xid
, savedxid
;
6533 struct nfsm_chain nmreq
, nmrep
;
6534 struct nfsreq_secinfo_args si
;
6536 if (vnode_mount(vp
) != vnode_mount(tdvp
))
6542 nfsvers
= nmp
->nm_vers
;
6543 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6545 if (tdnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6549 * Push all writes to the server, so that the attribute cache
6550 * doesn't get "out of sync" with the server.
6551 * XXX There should be a better way!
6553 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
6555 if ((error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
))))
6558 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6559 nfsm_chain_null(&nmreq
);
6560 nfsm_chain_null(&nmrep
);
6562 // PUTFH(SOURCE), SAVEFH, PUTFH(DIR), LINK, GETATTR(DIR), RESTOREFH, GETATTR
6564 nfsm_chain_build_alloc_init(error
, &nmreq
, 29 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
6565 nfsm_chain_add_compound_header(error
, &nmreq
, "link", numops
);
6567 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6568 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6570 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
6572 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6573 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
6575 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LINK
);
6576 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
6578 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6579 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, tdnp
);
6581 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
6583 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6584 nfsm_chain_add_bitmap_supported(error
, &nmreq
, nfs_getattr_bitmap
, nmp
, np
);
6585 nfsm_chain_build_done(error
, &nmreq
);
6586 nfsm_assert(error
, (numops
== 0), EPROTO
);
6588 error
= nfs_request(tdnp
, NULL
, &nmreq
, NFSPROC4_COMPOUND
, ctx
, &si
, &nmrep
, &xid
, &status
);
6590 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
6594 nfsm_chain_skip_tag(error
, &nmrep
);
6595 nfsm_chain_get_32(error
, &nmrep
, numops
);
6596 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6597 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
6598 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6599 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LINK
);
6600 nfsm_chain_check_change_info(error
, &nmrep
, tdnp
);
6601 /* directory attributes: if we don't get them, make sure to invalidate */
6602 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6604 nfsm_chain_loadattr(error
, &nmrep
, tdnp
, nfsvers
, &xid
);
6606 NATTRINVALIDATE(tdnp
);
6607 /* link attributes: if we don't get them, make sure to invalidate */
6608 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
6609 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6611 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
6613 NATTRINVALIDATE(np
);
6615 nfsm_chain_cleanup(&nmreq
);
6616 nfsm_chain_cleanup(&nmrep
);
6618 tdnp
->n_flag
|= NMODIFIED
;
6619 /* Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. */
6620 if (error
== EEXIST
)
6622 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
6623 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
6624 cache_purge_negatives(tdvp
);
6627 nfs_node_unlock2(tdnp
, np
);
6628 nfs_node_clear_busy2(tdnp
, np
);
6634 struct vnop_rmdir_args
/* {
6635 struct vnodeop_desc *a_desc;
6638 struct componentname *a_cnp;
6639 vfs_context_t a_context;
6642 vfs_context_t ctx
= ap
->a_context
;
6643 vnode_t vp
= ap
->a_vp
;
6644 vnode_t dvp
= ap
->a_dvp
;
6645 struct componentname
*cnp
= ap
->a_cnp
;
6646 struct nfsmount
*nmp
;
6647 int error
= 0, namedattrs
;
6648 nfsnode_t np
= VTONFS(vp
);
6649 nfsnode_t dnp
= VTONFS(dvp
);
6650 struct nfs_dulookup dul
;
6652 if (vnode_vtype(vp
) != VDIR
)
6655 nmp
= NFSTONMP(dnp
);
6658 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
6660 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
6664 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
6665 nfs_dulookup_start(&dul
, dnp
, ctx
);
6668 error
= nfs4_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6669 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
6671 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
6672 /* nfs_getattr() will check changed and purge caches */
6673 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
6675 nfs_dulookup_finish(&dul
, dnp
, ctx
);
6676 nfs_node_clear_busy2(dnp
, np
);
6679 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
6681 if (error
== ENOENT
)
6685 * remove nfsnode from hash now so we can't accidentally find it
6686 * again if another object gets created with the same filehandle
6687 * before this vnode gets reclaimed
6689 lck_mtx_lock(nfs_node_hash_mutex
);
6690 if (np
->n_hflag
& NHHASHED
) {
6691 LIST_REMOVE(np
, n_hash
);
6692 np
->n_hflag
&= ~NHHASHED
;
6693 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
6695 lck_mtx_unlock(nfs_node_hash_mutex
);
6701 * NFSv4 Named Attributes
6703 * Both the extended attributes interface and the named streams interface
6704 * are backed by NFSv4 named attributes. The implementations for both use
6705 * a common set of routines in an attempt to reduce code duplication, to
6706 * increase efficiency, to increase caching of both names and data, and to
6707 * confine the complexity.
6709 * Each NFS node caches its named attribute directory's file handle.
6710 * The directory nodes for the named attribute directories are handled
6711 * exactly like regular directories (with a couple minor exceptions).
6712 * Named attribute nodes are also treated as much like regular files as
6715 * Most of the heavy lifting is done by nfs4_named_attr_get().
6719 * Get the given node's attribute directory node.
6720 * If !fetch, then only return a cached node.
6721 * Otherwise, we will attempt to fetch the node from the server.
6722 * (Note: the node should be marked busy.)
6725 nfs4_named_attr_dir_get(nfsnode_t np
, int fetch
, vfs_context_t ctx
)
6727 nfsnode_t adnp
= NULL
;
6728 struct nfsmount
*nmp
;
6729 int error
= 0, status
, numops
;
6730 struct nfsm_chain nmreq
, nmrep
;
6732 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
6734 struct nfs_vattr nvattr
;
6735 struct componentname cn
;
6736 struct nfsreq rq
, *req
= &rq
;
6737 struct nfsreq_secinfo_args si
;
6742 if (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
)
6745 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6746 NVATTR_INIT(&nvattr
);
6747 nfsm_chain_null(&nmreq
);
6748 nfsm_chain_null(&nmrep
);
6750 bzero(&cn
, sizeof(cn
));
6751 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
6752 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
6753 cn
.cn_nameiop
= LOOKUP
;
6755 if (np
->n_attrdirfh
) {
6756 // XXX can't set parent correctly (to np) yet
6757 error
= nfs_nget(nmp
->nm_mountp
, NULL
, &cn
, np
->n_attrdirfh
+1, *np
->n_attrdirfh
,
6758 NULL
, NULL
, RPCAUTH_UNKNOWN
, NG_NOCREATE
, &adnp
);
6767 // PUTFH, OPENATTR, GETATTR
6769 nfsm_chain_build_alloc_init(error
, &nmreq
, 22 * NFSX_UNSIGNED
);
6770 nfsm_chain_add_compound_header(error
, &nmreq
, "openattr", numops
);
6772 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
6773 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
6775 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
6776 nfsm_chain_add_32(error
, &nmreq
, 0);
6778 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
6779 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
6780 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
6781 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
6782 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
6783 nfsm_chain_build_done(error
, &nmreq
);
6784 nfsm_assert(error
, (numops
== 0), EPROTO
);
6786 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
6787 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
6789 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
6791 nfsm_chain_skip_tag(error
, &nmrep
);
6792 nfsm_chain_get_32(error
, &nmrep
, numops
);
6793 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
6794 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
6795 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
6797 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
6799 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
6803 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
6804 /* (re)allocate attrdir fh buffer */
6805 if (np
->n_attrdirfh
)
6806 FREE(np
->n_attrdirfh
, M_TEMP
);
6807 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
6809 if (!np
->n_attrdirfh
) {
6813 /* cache the attrdir fh in the node */
6814 *np
->n_attrdirfh
= fh
.fh_len
;
6815 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
6816 /* create node for attrdir */
6817 // XXX can't set parent correctly (to np) yet
6818 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
6820 NVATTR_CLEANUP(&nvattr
);
6821 nfsm_chain_cleanup(&nmreq
);
6822 nfsm_chain_cleanup(&nmrep
);
6825 /* sanity check that this node is an attribute directory */
6826 if (adnp
->n_vattr
.nva_type
!= VDIR
)
6828 if (!(adnp
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
))
6830 nfs_node_unlock(adnp
);
6832 vnode_put(NFSTOV(adnp
));
6834 return (error
? NULL
: adnp
);
6838 * Get the given node's named attribute node for the name given.
6840 * In an effort to increase the performance of named attribute access, we try
6841 * to reduce server requests by doing the following:
6843 * - cache the node's named attribute directory file handle in the node
6844 * - maintain a directory vnode for the attribute directory
6845 * - use name cache entries (positive and negative) to speed up lookups
6846 * - optionally open the named attribute (with the given accessMode) in the same RPC
6847 * - combine attribute directory retrieval with the lookup/open RPC
6848 * - optionally prefetch the named attribute's first block of data in the same RPC
6850 * Also, in an attempt to reduce the number of copies/variations of this code,
6851 * parts of the RPC building/processing code are conditionalized on what is
6852 * needed for any particular request (openattr, lookup vs. open, read).
6854 * Note that because we may not have the attribute directory node when we start
6855 * the lookup/open, we lock both the node and the attribute directory node.
6858 #define NFS_GET_NAMED_ATTR_CREATE 0x1
6859 #define NFS_GET_NAMED_ATTR_CREATE_GUARDED 0x2
6860 #define NFS_GET_NAMED_ATTR_TRUNCATE 0x4
6861 #define NFS_GET_NAMED_ATTR_PREFETCH 0x8
6864 nfs4_named_attr_get(
6866 struct componentname
*cnp
,
6867 uint32_t accessMode
,
6871 struct nfs_open_file
**nofpp
)
6873 struct nfsmount
*nmp
;
6874 int error
= 0, open_error
= EIO
;
6875 int inuse
= 0, adlockerror
= ENOENT
, busyerror
= ENOENT
, adbusyerror
= ENOENT
, nofpbusyerror
= ENOENT
;
6876 int create
, guarded
, prefetch
, truncate
, noopbusy
= 0;
6877 int open
, status
, numops
, hadattrdir
, negnamecache
;
6878 struct nfs_vattr nvattr
;
6879 struct vnode_attr vattr
;
6880 nfsnode_t adnp
= NULL
, anp
= NULL
;
6882 u_int64_t xid
, savedxid
= 0;
6883 struct nfsm_chain nmreq
, nmrep
;
6884 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
;
6885 uint32_t denyMode
, rflags
, delegation
, recall
, eof
, rlen
, retlen
;
6886 nfs_stateid stateid
, dstateid
;
6888 struct nfs_open_owner
*noop
= NULL
;
6889 struct nfs_open_file
*newnofp
= NULL
, *nofp
= NULL
;
6890 struct vnop_access_args naa
;
6895 uint32_t ace_type
, ace_flags
, ace_mask
, len
, slen
;
6896 struct kauth_ace ace
;
6897 struct nfsreq rq
, *req
= &rq
;
6898 struct nfsreq_secinfo_args si
;
6902 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
6905 slen
= sizeof(sbuf
);
6910 NVATTR_INIT(&nvattr
);
6911 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
6912 thd
= vfs_context_thread(ctx
);
6913 cred
= vfs_context_ucred(ctx
);
6914 create
= (flags
& NFS_GET_NAMED_ATTR_CREATE
) ? NFS_OPEN_CREATE
: NFS_OPEN_NOCREATE
;
6915 guarded
= (flags
& NFS_GET_NAMED_ATTR_CREATE_GUARDED
) ? NFS_CREATE_GUARDED
: NFS_CREATE_UNCHECKED
;
6916 truncate
= (flags
& NFS_GET_NAMED_ATTR_TRUNCATE
);
6917 prefetch
= (flags
& NFS_GET_NAMED_ATTR_PREFETCH
);
6920 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
6923 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
6924 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
6926 } else if (accessMode
== NFS_OPEN_SHARE_ACCESS_NONE
) {
6927 /* shouldn't happen... but just be safe */
6928 printf("nfs4_named_attr_get: create with no access %s\n", cnp
->cn_nameptr
);
6929 accessMode
= NFS_OPEN_SHARE_ACCESS_READ
;
6931 open
= (accessMode
!= NFS_OPEN_SHARE_ACCESS_NONE
);
6934 * We're trying to open the file.
6935 * We'll create/open it with the given access mode,
6936 * and set NFS_OPEN_FILE_CREATE.
6938 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
6939 if (prefetch
&& guarded
)
6940 prefetch
= 0; /* no sense prefetching data that can't be there */
6942 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
6947 if ((error
= busyerror
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
6950 adnp
= nfs4_named_attr_dir_get(np
, 0, ctx
);
6951 hadattrdir
= (adnp
!= NULL
);
6954 /* use the special state ID because we don't have a real one to send */
6955 stateid
.seqid
= stateid
.other
[0] = stateid
.other
[1] = stateid
.other
[2] = 0;
6956 rlen
= MIN(nmp
->nm_rsize
, nmp
->nm_biosize
);
6958 NFSREQ_SECINFO_SET(&si
, np
, NULL
, 0, NULL
, 0);
6959 nfsm_chain_null(&nmreq
);
6960 nfsm_chain_null(&nmrep
);
6963 if ((error
= adbusyerror
= nfs_node_set_busy(adnp
, vfs_context_thread(ctx
))))
6965 /* nfs_getattr() will check changed and purge caches */
6966 error
= nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
6968 error
= cache_lookup(NFSTOV(adnp
), &avp
, cnp
);
6971 /* negative cache entry */
6975 /* try dir buf cache lookup */
6976 error
= nfs_dir_buf_cache_lookup(adnp
, &anp
, cnp
, ctx
, 0);
6977 if (!error
&& anp
) {
6978 /* dir buf cache hit */
6982 if (error
!= -1) /* cache miss */
6986 /* cache hit, not really an error */
6987 OSAddAtomic(1, &nfsstats
.lookupcache_hits
);
6989 *anpp
= anp
= VTONFS(avp
);
6991 nfs_node_clear_busy(adnp
);
6992 adbusyerror
= ENOENT
;
6994 /* check for directory access */
6995 naa
.a_desc
= &vnop_access_desc
;
6996 naa
.a_vp
= NFSTOV(adnp
);
6997 naa
.a_action
= KAUTH_VNODE_SEARCH
;
6998 naa
.a_context
= ctx
;
7000 /* compute actual success/failure based on accessibility */
7001 error
= nfs_vnop_access(&naa
);
7004 /* we either found it, or hit an error */
7005 if (!error
&& guarded
) {
7006 /* found cached entry but told not to use it */
7008 vnode_put(NFSTOV(anp
));
7011 /* we're done if error or we don't need to open */
7014 /* no error and we need to open... */
7020 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
7022 nfs_open_owner_rele(noop
);
7028 /* grab an open file - possibly provisional/nodeless if cache_lookup() failed */
7029 error
= nfs_open_file_find(anp
, noop
, &newnofp
, 0, 0, 1);
7030 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
7031 printf("nfs4_named_attr_get: LOST %d %s\n", kauth_cred_getuid(noop
->noo_cred
), cnp
->cn_nameptr
);
7034 if (!error
&& (newnofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
7035 nfs_mount_state_in_use_end(nmp
, 0);
7036 error
= nfs4_reopen(newnofp
, vfs_context_thread(ctx
));
7037 nfs_open_file_destroy(newnofp
);
7043 error
= nfs_open_file_set_busy(newnofp
, vfs_context_thread(ctx
));
7046 nfs_open_file_destroy(newnofp
);
7052 * We already have the node. So we just need to open
7053 * it - which we may be able to do with a delegation.
7055 open_error
= error
= nfs4_open(anp
, newnofp
, accessMode
, denyMode
, ctx
);
7057 /* open succeeded, so our open file is no longer temporary */
7069 * We either don't have the attrdir or we didn't find the attribute
7070 * in the name cache, so we need to talk to the server.
7072 * If we don't have the attrdir, we'll need to ask the server for that too.
7073 * If the caller is requesting that the attribute be created, we need to
7074 * make sure the attrdir is created.
7075 * The caller may also request that the first block of an existing attribute
7076 * be retrieved at the same time.
7080 /* need to mark the open owner busy during the RPC */
7081 if ((error
= nfs_open_owner_set_busy(noop
, thd
)))
7087 * We'd like to get updated post-open/lookup attributes for the
7088 * directory and we may also want to prefetch some data via READ.
7089 * We'd like the READ results to be last so that we can leave the
7090 * data in the mbufs until the end.
7092 * At a minimum we're sending: PUTFH, LOOKUP/OPEN, GETATTR, PUTFH, GETATTR
7096 numops
+= 3; // also sending: OPENATTR, GETATTR, OPENATTR
7098 numops
+= 4; // also sending: SAVEFH, RESTOREFH, NVERIFY, READ
7099 nfsm_chain_build_alloc_init(error
, &nmreq
, 64 * NFSX_UNSIGNED
+ cnp
->cn_namelen
);
7100 nfsm_chain_add_compound_header(error
, &nmreq
, "getnamedattr", numops
);
7103 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7104 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7107 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7108 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7110 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7111 nfsm_chain_add_32(error
, &nmreq
, create
? 1 : 0);
7113 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7114 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7115 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7116 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7117 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7121 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPEN
);
7122 nfsm_chain_add_32(error
, &nmreq
, noop
->noo_seqid
);
7123 nfsm_chain_add_32(error
, &nmreq
, accessMode
);
7124 nfsm_chain_add_32(error
, &nmreq
, denyMode
);
7125 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
7126 nfsm_chain_add_32(error
, &nmreq
, NFSX_UNSIGNED
);
7127 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(noop
->noo_cred
));
7128 nfsm_chain_add_32(error
, &nmreq
, create
);
7130 nfsm_chain_add_32(error
, &nmreq
, guarded
);
7133 VATTR_SET(&vattr
, va_data_size
, 0);
7134 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7136 nfsm_chain_add_32(error
, &nmreq
, NFS_CLAIM_NULL
);
7137 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7140 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
7141 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
7144 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7145 NFS_COPY_ATTRIBUTES(nfs_getattr_bitmap
, bitmap
);
7146 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FILEHANDLE
);
7147 nfsm_chain_add_bitmap_masked(error
, &nmreq
, bitmap
,
7148 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7151 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SAVEFH
);
7155 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7156 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, adnp
->n_fhp
, adnp
->n_fhsize
);
7159 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
7160 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, np
->n_fhp
, np
->n_fhsize
);
7162 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_OPENATTR
);
7163 nfsm_chain_add_32(error
, &nmreq
, 0);
7166 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
7167 nfsm_chain_add_bitmap_masked(error
, &nmreq
, nfs_getattr_bitmap
,
7168 NFS_ATTR_BITMAP_LEN
, nmp
->nm_fsattr
.nfsa_supp_attr
);
7171 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RESTOREFH
);
7173 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_NVERIFY
);
7175 VATTR_SET(&vattr
, va_data_size
, 0);
7176 nfsm_chain_add_fattr4(error
, &nmreq
, &vattr
, nmp
);
7178 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_READ
);
7179 nfsm_chain_add_stateid(error
, &nmreq
, &stateid
);
7180 nfsm_chain_add_64(error
, &nmreq
, 0);
7181 nfsm_chain_add_32(error
, &nmreq
, rlen
);
7183 nfsm_chain_build_done(error
, &nmreq
);
7184 nfsm_assert(error
, (numops
== 0), EPROTO
);
7186 error
= nfs_request_async(hadattrdir
? adnp
: np
, NULL
, &nmreq
, NFSPROC4_COMPOUND
,
7187 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, open
? R_NOINTR
: 0, NULL
, &req
);
7189 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
7191 if (hadattrdir
&& ((adlockerror
= nfs_node_lock(adnp
))))
7192 error
= adlockerror
;
7194 nfsm_chain_skip_tag(error
, &nmrep
);
7195 nfsm_chain_get_32(error
, &nmrep
, numops
);
7196 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7198 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7199 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7201 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7203 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) && fh
.fh_len
) {
7204 if (!np
->n_attrdirfh
|| (*np
->n_attrdirfh
!= fh
.fh_len
)) {
7205 /* (re)allocate attrdir fh buffer */
7206 if (np
->n_attrdirfh
)
7207 FREE(np
->n_attrdirfh
, M_TEMP
);
7208 MALLOC(np
->n_attrdirfh
, u_char
*, fh
.fh_len
+1, M_TEMP
, M_WAITOK
);
7210 if (np
->n_attrdirfh
) {
7211 /* remember the attrdir fh in the node */
7212 *np
->n_attrdirfh
= fh
.fh_len
;
7213 bcopy(fh
.fh_data
, np
->n_attrdirfh
+1, fh
.fh_len
);
7214 /* create busied node for attrdir */
7215 struct componentname cn
;
7216 bzero(&cn
, sizeof(cn
));
7217 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(_PATH_FORKSPECIFIER
, const, char *); /* "/..namedfork/" */
7218 cn
.cn_namelen
= strlen(_PATH_FORKSPECIFIER
);
7219 cn
.cn_nameiop
= LOOKUP
;
7220 // XXX can't set parent correctly (to np) yet
7221 error
= nfs_nget(NFSTOMP(np
), NULL
, &cn
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, 0, &adnp
);
7224 /* set the node busy */
7225 SET(adnp
->n_flag
, NBUSY
);
7228 /* if no adnp, oh well... */
7232 NVATTR_CLEANUP(&nvattr
);
7236 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPEN
);
7237 nfs_owner_seqid_increment(noop
, NULL
, error
);
7238 nfsm_chain_get_stateid(error
, &nmrep
, &newnofp
->nof_stateid
);
7239 nfsm_chain_check_change_info(error
, &nmrep
, adnp
);
7240 nfsm_chain_get_32(error
, &nmrep
, rflags
);
7241 bmlen
= NFS_ATTR_BITMAP_LEN
;
7242 nfsm_chain_get_bitmap(error
, &nmrep
, bitmap
, bmlen
);
7243 nfsm_chain_get_32(error
, &nmrep
, delegation
);
7245 switch (delegation
) {
7246 case NFS_OPEN_DELEGATE_NONE
:
7248 case NFS_OPEN_DELEGATE_READ
:
7249 case NFS_OPEN_DELEGATE_WRITE
:
7250 nfsm_chain_get_stateid(error
, &nmrep
, &dstateid
);
7251 nfsm_chain_get_32(error
, &nmrep
, recall
);
7252 if (delegation
== NFS_OPEN_DELEGATE_WRITE
) // space (skip) XXX
7253 nfsm_chain_adv(error
, &nmrep
, 3 * NFSX_UNSIGNED
);
7254 /* if we have any trouble accepting the ACE, just invalidate it */
7255 ace_type
= ace_flags
= ace_mask
= len
= 0;
7256 nfsm_chain_get_32(error
, &nmrep
, ace_type
);
7257 nfsm_chain_get_32(error
, &nmrep
, ace_flags
);
7258 nfsm_chain_get_32(error
, &nmrep
, ace_mask
);
7259 nfsm_chain_get_32(error
, &nmrep
, len
);
7260 ace
.ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
7261 ace
.ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
7262 ace
.ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
7263 if (!error
&& (len
>= slen
)) {
7264 MALLOC(s
, char*, len
+1, M_TEMP
, M_WAITOK
);
7271 nfsm_chain_get_opaque(error
, &nmrep
, len
, s
);
7273 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(len
));
7276 if (nfs4_id2guid(s
, &ace
.ace_applicable
, (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
)))
7281 if (s
&& (s
!= sbuf
))
7288 /* At this point if we have no error, the object was created/opened. */
7291 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOOKUP
);
7293 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7295 error
= nfs4_parsefattr(&nmrep
, NULL
, &nvattr
, &fh
, NULL
, NULL
);
7297 if (!NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_FILEHANDLE
) || !fh
.fh_len
) {
7302 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SAVEFH
);
7303 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
7305 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_OPENATTR
);
7306 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
7309 nfsm_chain_loadattr(error
, &nmrep
, adnp
, nmp
->nm_vers
, &xid
);
7313 if (rflags
& NFS_OPEN_RESULT_LOCKTYPE_POSIX
)
7314 newnofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7315 if (rflags
& NFS_OPEN_RESULT_CONFIRM
) {
7317 nfs_node_unlock(adnp
);
7318 adlockerror
= ENOENT
;
7320 NVATTR_CLEANUP(&nvattr
);
7321 error
= nfs4_open_confirm_rpc(nmp
, adnp
? adnp
: np
, fh
.fh_data
, fh
.fh_len
, noop
, &newnofp
->nof_stateid
, thd
, cred
, &nvattr
, &xid
);
7324 if ((adlockerror
= nfs_node_lock(adnp
)))
7325 error
= adlockerror
;
7330 if (open
&& adnp
&& !adlockerror
) {
7331 if (!open_error
&& (adnp
->n_flag
& NNEGNCENTRIES
)) {
7332 adnp
->n_flag
&= ~NNEGNCENTRIES
;
7333 cache_purge_negatives(NFSTOV(adnp
));
7335 adnp
->n_flag
|= NMODIFIED
;
7336 nfs_node_unlock(adnp
);
7337 adlockerror
= ENOENT
;
7338 nfs_getattr(adnp
, NULL
, ctx
, NGA_CACHED
);
7340 if (adnp
&& !adlockerror
&& (error
== ENOENT
) &&
7341 (cnp
->cn_flags
& MAKEENTRY
) && (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
7342 /* add a negative entry in the name cache */
7343 cache_enter(NFSTOV(adnp
), NULL
, cnp
);
7344 adnp
->n_flag
|= NNEGNCENTRIES
;
7346 if (adnp
&& !adlockerror
) {
7347 nfs_node_unlock(adnp
);
7348 adlockerror
= ENOENT
;
7350 if (!error
&& !anp
&& fh
.fh_len
) {
7351 /* create the vnode with the filehandle and attributes */
7353 error
= nfs_nget(NFSTOMP(np
), adnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &anp
);
7356 nfs_node_unlock(anp
);
7358 if (!error
&& open
) {
7359 nfs_open_file_add_open(newnofp
, accessMode
, denyMode
, 0);
7360 /* After we have a node, add our open file struct to the node */
7362 error
= nfs_open_file_find_internal(anp
, noop
, &nofp
, 0, 0, 0);
7364 /* This shouldn't happen, because we passed in a new nofp to use. */
7365 printf("nfs_open_file_find_internal failed! %d\n", error
);
7367 } else if (nofp
!= newnofp
) {
7369 * Hmm... an open file struct already exists.
7370 * Mark the existing one busy and merge our open into it.
7371 * Then destroy the one we created.
7372 * Note: there's no chance of an open confict because the
7373 * open has already been granted.
7375 nofpbusyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7376 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
7377 nofp
->nof_stateid
= newnofp
->nof_stateid
;
7378 if (newnofp
->nof_flags
& NFS_OPEN_FILE_POSIXLOCK
)
7379 nofp
->nof_flags
|= NFS_OPEN_FILE_POSIXLOCK
;
7380 nfs_open_file_clear_busy(newnofp
);
7381 nfs_open_file_destroy(newnofp
);
7387 /* mark the node as holding a create-initiated open */
7388 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
7389 nofp
->nof_creator
= current_thread();
7395 NVATTR_CLEANUP(&nvattr
);
7396 if (open
&& ((delegation
== NFS_OPEN_DELEGATE_READ
) || (delegation
== NFS_OPEN_DELEGATE_WRITE
))) {
7397 if (!error
&& anp
&& !recall
) {
7398 /* stuff the delegation state in the node */
7399 lck_mtx_lock(&anp
->n_openlock
);
7400 anp
->n_openflags
&= ~N_DELEG_MASK
;
7401 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7402 anp
->n_dstateid
= dstateid
;
7404 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7405 lck_mtx_lock(&nmp
->nm_lock
);
7406 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7407 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7408 lck_mtx_unlock(&nmp
->nm_lock
);
7410 lck_mtx_unlock(&anp
->n_openlock
);
7412 /* give the delegation back */
7414 if (NFS_CMPFH(anp
, fh
.fh_data
, fh
.fh_len
)) {
7415 /* update delegation state and return it */
7416 lck_mtx_lock(&anp
->n_openlock
);
7417 anp
->n_openflags
&= ~N_DELEG_MASK
;
7418 anp
->n_openflags
|= ((delegation
== NFS_OPEN_DELEGATE_READ
) ? N_DELEG_READ
: N_DELEG_WRITE
);
7419 anp
->n_dstateid
= dstateid
;
7421 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
) {
7422 lck_mtx_lock(&nmp
->nm_lock
);
7423 if (anp
->n_dlink
.tqe_next
== NFSNOLIST
)
7424 TAILQ_INSERT_TAIL(&nmp
->nm_delegations
, anp
, n_dlink
);
7425 lck_mtx_unlock(&nmp
->nm_lock
);
7427 lck_mtx_unlock(&anp
->n_openlock
);
7428 /* don't need to send a separate delegreturn for fh */
7431 /* return anp's current delegation */
7432 nfs4_delegation_return(anp
, 0, thd
, cred
);
7434 if (fh
.fh_len
) /* return fh's delegation if it wasn't for anp */
7435 nfs4_delegreturn_rpc(nmp
, fh
.fh_data
, fh
.fh_len
, &dstateid
, 0, thd
, cred
);
7440 /* need to cleanup our temporary nofp */
7441 nfs_open_file_clear_busy(newnofp
);
7442 nfs_open_file_destroy(newnofp
);
7444 } else if (nofp
&& !nofpbusyerror
) {
7445 nfs_open_file_clear_busy(nofp
);
7446 nofpbusyerror
= ENOENT
;
7448 if (inuse
&& nfs_mount_state_in_use_end(nmp
, error
)) {
7450 nofp
= newnofp
= NULL
;
7451 rflags
= delegation
= recall
= eof
= rlen
= retlen
= 0;
7454 slen
= sizeof(sbuf
);
7455 nfsm_chain_cleanup(&nmreq
);
7456 nfsm_chain_cleanup(&nmrep
);
7458 vnode_put(NFSTOV(anp
));
7461 hadattrdir
= (adnp
!= NULL
);
7463 nfs_open_owner_clear_busy(noop
);
7470 nfs_open_owner_clear_busy(noop
);
7473 nfs_open_owner_rele(noop
);
7476 if (!error
&& prefetch
&& nmrep
.nmc_mhead
) {
7477 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RESTOREFH
);
7478 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_NVERIFY
);
7479 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_READ
);
7480 nfsm_chain_get_32(error
, &nmrep
, eof
);
7481 nfsm_chain_get_32(error
, &nmrep
, retlen
);
7482 if (!error
&& anp
) {
7484 * There can be one problem with doing the prefetch.
7485 * Because we don't have the node before we start the RPC, we
7486 * can't have the buffer busy while the READ is performed.
7487 * So there is a chance that other I/O occured on the same
7488 * range of data while we were performing this RPC. If that
7489 * happens, then it's possible the data we have in the READ
7490 * response is no longer up to date.
7491 * Once we have the node and the buffer, we need to make sure
7492 * that there's no chance we could be putting stale data in
7494 * So, we check if the range read is dirty or if any I/O may
7495 * have occured on it while we were performing our RPC.
7497 struct nfsbuf
*bp
= NULL
;
7501 retlen
= MIN(retlen
, rlen
);
7503 /* check if node needs size update or invalidation */
7504 if (ISSET(anp
->n_flag
, NUPDATESIZE
))
7505 nfs_data_update_size(anp
, 0);
7506 if (!(error
= nfs_node_lock(anp
))) {
7507 if (anp
->n_flag
& NNEEDINVALIDATE
) {
7508 anp
->n_flag
&= ~NNEEDINVALIDATE
;
7509 nfs_node_unlock(anp
);
7510 error
= nfs_vinvalbuf(NFSTOV(anp
), V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
7511 if (!error
) /* lets play it safe and just drop the data */
7514 nfs_node_unlock(anp
);
7518 /* calculate page mask for the range of data read */
7519 lastpg
= (trunc_page_32(retlen
) - 1) / PAGE_SIZE
;
7520 pagemask
= ((1 << (lastpg
+ 1)) - 1);
7523 error
= nfs_buf_get(anp
, 0, nmp
->nm_biosize
, thd
, NBLK_READ
|NBLK_NOWAIT
, &bp
);
7524 /* don't save the data if dirty or potential I/O conflict */
7525 if (!error
&& bp
&& !bp
->nb_dirtyoff
&& !(bp
->nb_dirty
& pagemask
) &&
7526 timevalcmp(&anp
->n_lastio
, &now
, <)) {
7527 OSAddAtomic(1, &nfsstats
.read_bios
);
7528 CLR(bp
->nb_flags
, (NB_DONE
|NB_ASYNC
));
7529 SET(bp
->nb_flags
, NB_READ
);
7531 nfsm_chain_get_opaque(error
, &nmrep
, retlen
, bp
->nb_data
);
7533 bp
->nb_error
= error
;
7534 SET(bp
->nb_flags
, NB_ERROR
);
7537 bp
->nb_endio
= rlen
;
7538 if ((retlen
> 0) && (bp
->nb_endio
< (int)retlen
))
7539 bp
->nb_endio
= retlen
;
7540 if (eof
|| (retlen
== 0)) {
7541 /* zero out the remaining data (up to EOF) */
7542 off_t rpcrem
, eofrem
, rem
;
7543 rpcrem
= (rlen
- retlen
);
7544 eofrem
= anp
->n_size
- (NBOFF(bp
) + retlen
);
7545 rem
= (rpcrem
< eofrem
) ? rpcrem
: eofrem
;
7547 bzero(bp
->nb_data
+ retlen
, rem
);
7548 } else if ((retlen
< rlen
) && !ISSET(bp
->nb_flags
, NB_ERROR
)) {
7549 /* ugh... short read ... just invalidate for now... */
7550 SET(bp
->nb_flags
, NB_INVAL
);
7553 nfs_buf_read_finish(bp
);
7554 microuptime(&anp
->n_lastio
);
7557 nfs_buf_release(bp
, 1);
7559 error
= 0; /* ignore any transient error in processing the prefetch */
7561 if (adnp
&& !adbusyerror
) {
7562 nfs_node_clear_busy(adnp
);
7563 adbusyerror
= ENOENT
;
7566 nfs_node_clear_busy(np
);
7570 vnode_put(NFSTOV(adnp
));
7571 if (error
&& *anpp
) {
7572 vnode_put(NFSTOV(*anpp
));
7575 nfsm_chain_cleanup(&nmreq
);
7576 nfsm_chain_cleanup(&nmrep
);
7581 * Remove a named attribute.
7584 nfs4_named_attr_remove(nfsnode_t np
, nfsnode_t anp
, const char *name
, vfs_context_t ctx
)
7586 nfsnode_t adnp
= NULL
;
7587 struct nfsmount
*nmp
;
7588 struct componentname cn
;
7589 struct vnop_remove_args vra
;
7590 int error
, putanp
= 0;
7596 bzero(&cn
, sizeof(cn
));
7597 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7598 cn
.cn_namelen
= strlen(name
);
7599 cn
.cn_nameiop
= DELETE
;
7603 error
= nfs4_named_attr_get(np
, &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7604 0, ctx
, &anp
, NULL
);
7605 if ((!error
&& !anp
) || (error
== ENOATTR
))
7609 vnode_put(NFSTOV(anp
));
7617 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7619 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
7620 nfs_node_clear_busy(np
);
7626 vra
.a_desc
= &vnop_remove_desc
;
7627 vra
.a_dvp
= NFSTOV(adnp
);
7628 vra
.a_vp
= NFSTOV(anp
);
7631 vra
.a_context
= ctx
;
7632 error
= nfs_vnop_remove(&vra
);
7635 vnode_put(NFSTOV(adnp
));
7637 vnode_put(NFSTOV(anp
));
7643 struct vnop_getxattr_args
/* {
7644 struct vnodeop_desc *a_desc;
7646 const char * a_name;
7650 vfs_context_t a_context;
7653 vfs_context_t ctx
= ap
->a_context
;
7654 struct nfsmount
*nmp
;
7655 struct nfs_vattr nvattr
;
7656 struct componentname cn
;
7658 int error
= 0, isrsrcfork
;
7660 nmp
= VTONMP(ap
->a_vp
);
7664 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7666 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
7669 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7670 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7673 bzero(&cn
, sizeof(cn
));
7674 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
7675 cn
.cn_namelen
= strlen(ap
->a_name
);
7676 cn
.cn_nameiop
= LOOKUP
;
7677 cn
.cn_flags
= MAKEENTRY
;
7679 /* we'll normally try to prefetch data for xattrs... the resource fork is really a stream */
7680 isrsrcfork
= (bcmp(ap
->a_name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7682 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
7683 !isrsrcfork
? NFS_GET_NAMED_ATTR_PREFETCH
: 0, ctx
, &anp
, NULL
);
7684 if ((!error
&& !anp
) || (error
== ENOENT
))
7688 error
= nfs_bioread(anp
, ap
->a_uio
, 0, ctx
);
7690 *ap
->a_size
= anp
->n_size
;
7693 vnode_put(NFSTOV(anp
));
7699 struct vnop_setxattr_args
/* {
7700 struct vnodeop_desc *a_desc;
7702 const char * a_name;
7705 vfs_context_t a_context;
7708 vfs_context_t ctx
= ap
->a_context
;
7709 int options
= ap
->a_options
;
7710 uio_t uio
= ap
->a_uio
;
7711 const char *name
= ap
->a_name
;
7712 struct nfsmount
*nmp
;
7713 struct componentname cn
;
7714 nfsnode_t anp
= NULL
;
7715 int error
= 0, closeerror
= 0, flags
, isrsrcfork
, isfinderinfo
, empty
= 0, i
;
7716 #define FINDERINFOSIZE 32
7717 uint8_t finfo
[FINDERINFOSIZE
];
7719 struct nfs_open_file
*nofp
= NULL
;
7720 char uio_buf
[ UIO_SIZEOF(1) ];
7722 struct vnop_write_args vwa
;
7724 nmp
= VTONMP(ap
->a_vp
);
7728 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7731 if ((options
& XATTR_CREATE
) && (options
& XATTR_REPLACE
))
7734 /* XXX limitation based on need to back up uio on short write */
7735 if (uio_iovcnt(uio
) > 1) {
7736 printf("nfs4_vnop_setxattr: iovcnt > 1\n");
7740 bzero(&cn
, sizeof(cn
));
7741 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(name
, const, char *);
7742 cn
.cn_namelen
= strlen(name
);
7743 cn
.cn_nameiop
= CREATE
;
7744 cn
.cn_flags
= MAKEENTRY
;
7746 isfinderinfo
= (bcmp(name
, XATTR_FINDERINFO_NAME
, sizeof(XATTR_FINDERINFO_NAME
)) == 0);
7747 isrsrcfork
= isfinderinfo
? 0 : (bcmp(name
, XATTR_RESOURCEFORK_NAME
, sizeof(XATTR_RESOURCEFORK_NAME
)) == 0);
7749 uio_setoffset(uio
, 0);
7751 if (uio_resid(uio
) != sizeof(finfo
))
7753 error
= uiomove((char*)&finfo
, sizeof(finfo
), uio
);
7756 /* setting a FinderInfo of all zeroes means remove the FinderInfo */
7758 for (i
=0, finfop
=(uint32_t*)&finfo
; i
< (int)(sizeof(finfo
)/sizeof(uint32_t)); i
++)
7763 if (empty
&& !(options
& (XATTR_CREATE
|XATTR_REPLACE
))) {
7764 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7765 if (error
== ENOENT
)
7769 /* first, let's see if we get a create/replace error */
7773 * create/open the xattr
7775 * We need to make sure not to create it if XATTR_REPLACE.
7776 * For all xattrs except the resource fork, we also want to
7777 * truncate the xattr to remove any current data. We'll do
7778 * that by setting the size to 0 on create/open.
7781 if (!(options
& XATTR_REPLACE
))
7782 flags
|= NFS_GET_NAMED_ATTR_CREATE
;
7783 if (options
& XATTR_CREATE
)
7784 flags
|= NFS_GET_NAMED_ATTR_CREATE_GUARDED
;
7786 flags
|= NFS_GET_NAMED_ATTR_TRUNCATE
;
7788 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
7789 flags
, ctx
, &anp
, &nofp
);
7794 /* grab the open state from the get/create/open */
7795 if (nofp
&& !(error
= nfs_open_file_set_busy(nofp
, NULL
))) {
7796 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
7797 nofp
->nof_creator
= NULL
;
7798 nfs_open_file_clear_busy(nofp
);
7801 /* Setting an empty FinderInfo really means remove it, skip to the close/remove */
7802 if (isfinderinfo
&& empty
)
7806 * Write the data out and flush.
7808 * For FinderInfo, we've already copied the data to finfo, so do I/O from there.
7810 vwa
.a_desc
= &vnop_write_desc
;
7811 vwa
.a_vp
= NFSTOV(anp
);
7814 vwa
.a_context
= ctx
;
7816 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
, &uio_buf
, sizeof(uio_buf
));
7817 uio_addiov(auio
, (uintptr_t)&finfo
, sizeof(finfo
));
7819 } else if (uio_resid(uio
) > 0) {
7823 error
= nfs_vnop_write(&vwa
);
7825 error
= nfs_flush(anp
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
7828 /* Close the xattr. */
7830 int busyerror
= nfs_open_file_set_busy(nofp
, NULL
);
7831 closeerror
= nfs_close(anp
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
7833 nfs_open_file_clear_busy(nofp
);
7835 if (!error
&& isfinderinfo
&& empty
) { /* Setting an empty FinderInfo really means remove it */
7836 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), anp
, name
, ctx
);
7837 if (error
== ENOENT
)
7844 vnode_put(NFSTOV(anp
));
7845 if (error
== ENOENT
)
7851 nfs4_vnop_removexattr(
7852 struct vnop_removexattr_args
/* {
7853 struct vnodeop_desc *a_desc;
7855 const char * a_name;
7857 vfs_context_t a_context;
7860 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7865 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7868 error
= nfs4_named_attr_remove(VTONFS(ap
->a_vp
), NULL
, ap
->a_name
, ap
->a_context
);
7869 if (error
== ENOENT
)
7875 nfs4_vnop_listxattr(
7876 struct vnop_listxattr_args
/* {
7877 struct vnodeop_desc *a_desc;
7882 vfs_context_t a_context;
7885 vfs_context_t ctx
= ap
->a_context
;
7886 nfsnode_t np
= VTONFS(ap
->a_vp
);
7887 uio_t uio
= ap
->a_uio
;
7888 nfsnode_t adnp
= NULL
;
7889 struct nfsmount
*nmp
;
7891 struct nfs_vattr nvattr
;
7892 uint64_t cookie
, nextcookie
, lbn
= 0;
7893 struct nfsbuf
*bp
= NULL
;
7894 struct nfs_dir_buf_header
*ndbhp
;
7895 struct direntry
*dp
;
7897 nmp
= VTONMP(ap
->a_vp
);
7901 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
7904 error
= nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
);
7907 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
7908 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
7911 if ((error
= nfs_node_set_busy(np
, vfs_context_thread(ctx
))))
7913 adnp
= nfs4_named_attr_dir_get(np
, 1, ctx
);
7914 nfs_node_clear_busy(np
);
7918 if ((error
= nfs_node_lock(adnp
)))
7921 if (adnp
->n_flag
& NNEEDINVALIDATE
) {
7922 adnp
->n_flag
&= ~NNEEDINVALIDATE
;
7924 nfs_node_unlock(adnp
);
7925 error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
7927 error
= nfs_node_lock(adnp
);
7933 * check for need to invalidate when (re)starting at beginning
7935 if (adnp
->n_flag
& NMODIFIED
) {
7937 nfs_node_unlock(adnp
);
7938 if ((error
= nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1)))
7941 nfs_node_unlock(adnp
);
7943 /* nfs_getattr() will check changed and purge caches */
7944 if ((error
= nfs_getattr(adnp
, &nvattr
, ctx
, NGA_UNCACHED
)))
7947 if (uio
&& (uio_resid(uio
) == 0))
7951 nextcookie
= lbn
= 0;
7953 while (!error
&& !done
) {
7954 OSAddAtomic(1, &nfsstats
.biocache_readdirs
);
7955 cookie
= nextcookie
;
7957 error
= nfs_buf_get(adnp
, lbn
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
7960 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
7961 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
7962 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
7963 ndbhp
->ndbh_flags
= 0;
7964 ndbhp
->ndbh_count
= 0;
7965 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
7966 ndbhp
->ndbh_ncgen
= adnp
->n_ncgen
;
7968 error
= nfs_buf_readdir(bp
, ctx
);
7969 if (error
== NFSERR_DIRBUFDROPPED
)
7972 nfs_buf_release(bp
, 1);
7973 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
7974 if (!nfs_node_lock(adnp
)) {
7976 nfs_node_unlock(adnp
);
7978 nfs_vinvalbuf(NFSTOV(adnp
), 0, ctx
, 1);
7979 if (error
== NFSERR_BAD_COOKIE
)
7986 /* go through all the entries copying/counting */
7987 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
7988 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
7989 if (!xattr_protected(dp
->d_name
)) {
7991 *ap
->a_size
+= dp
->d_namlen
+ 1;
7992 } else if (uio_resid(uio
) < (dp
->d_namlen
+ 1)) {
7995 error
= uiomove(dp
->d_name
, dp
->d_namlen
+1, uio
);
7996 if (error
&& (error
!= EFAULT
))
8000 nextcookie
= dp
->d_seekoff
;
8001 dp
= NFS_DIRENTRY_NEXT(dp
);
8004 if (i
== ndbhp
->ndbh_count
) {
8005 /* hit end of buffer, move to next buffer */
8007 /* if we also hit EOF, we're done */
8008 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
))
8011 if (!error
&& !done
&& (nextcookie
== cookie
)) {
8012 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
8015 nfs_buf_release(bp
, 1);
8019 vnode_put(NFSTOV(adnp
));
8025 nfs4_vnop_getnamedstream(
8026 struct vnop_getnamedstream_args
/* {
8027 struct vnodeop_desc *a_desc;
8031 enum nsoperation a_operation;
8033 vfs_context_t a_context;
8036 vfs_context_t ctx
= ap
->a_context
;
8037 struct nfsmount
*nmp
;
8038 struct nfs_vattr nvattr
;
8039 struct componentname cn
;
8043 nmp
= VTONMP(ap
->a_vp
);
8047 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8049 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nvattr
, ctx
, NGA_CACHED
);
8052 if (NFS_BITMAP_ISSET(nvattr
.nva_bitmap
, NFS_FATTR_NAMED_ATTR
) &&
8053 !(nvattr
.nva_flags
& NFS_FFLAG_HAS_NAMED_ATTRS
))
8056 bzero(&cn
, sizeof(cn
));
8057 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8058 cn
.cn_namelen
= strlen(ap
->a_name
);
8059 cn
.cn_nameiop
= LOOKUP
;
8060 cn
.cn_flags
= MAKEENTRY
;
8062 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_NONE
,
8063 0, ctx
, &anp
, NULL
);
8064 if ((!error
&& !anp
) || (error
== ENOENT
))
8067 *ap
->a_svpp
= NFSTOV(anp
);
8069 vnode_put(NFSTOV(anp
));
8074 nfs4_vnop_makenamedstream(
8075 struct vnop_makenamedstream_args
/* {
8076 struct vnodeop_desc *a_desc;
8081 vfs_context_t a_context;
8084 vfs_context_t ctx
= ap
->a_context
;
8085 struct nfsmount
*nmp
;
8086 struct componentname cn
;
8090 nmp
= VTONMP(ap
->a_vp
);
8094 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8097 bzero(&cn
, sizeof(cn
));
8098 cn
.cn_nameptr
= __CAST_AWAY_QUALIFIER(ap
->a_name
, const, char *);
8099 cn
.cn_namelen
= strlen(ap
->a_name
);
8100 cn
.cn_nameiop
= CREATE
;
8101 cn
.cn_flags
= MAKEENTRY
;
8103 error
= nfs4_named_attr_get(VTONFS(ap
->a_vp
), &cn
, NFS_OPEN_SHARE_ACCESS_BOTH
,
8104 NFS_GET_NAMED_ATTR_CREATE
, ctx
, &anp
, NULL
);
8105 if ((!error
&& !anp
) || (error
== ENOENT
))
8108 *ap
->a_svpp
= NFSTOV(anp
);
8110 vnode_put(NFSTOV(anp
));
8115 nfs4_vnop_removenamedstream(
8116 struct vnop_removenamedstream_args
/* {
8117 struct vnodeop_desc *a_desc;
8122 vfs_context_t a_context;
8125 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
8126 nfsnode_t np
= ap
->a_vp
? VTONFS(ap
->a_vp
) : NULL
;
8127 nfsnode_t anp
= ap
->a_svp
? VTONFS(ap
->a_svp
) : NULL
;
8133 * Given that a_svp is a named stream, checking for
8134 * named attribute support is kinda pointless.
8136 if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
))
8139 return (nfs4_named_attr_remove(np
, anp
, ap
->a_name
, ap
->a_context
));