2 * Copyright (c) 2006-2019 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@
29 #include <nfs/nfs_conf.h>
33 * miscellaneous support functions for NFSv4
35 #include <sys/param.h>
37 #include <sys/kauth.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mount_internal.h>
41 #include <sys/vnode_internal.h>
42 #include <sys/kpi_mbuf.h>
43 #include <sys/socket.h>
45 #include <sys/malloc.h>
46 #include <sys/syscall.h>
47 #include <sys/ubc_internal.h>
48 #include <sys/fcntl.h>
49 #include <sys/quota.h>
50 #include <sys/domain.h>
51 #include <libkern/OSAtomic.h>
52 #include <kern/thread_call.h>
55 #include <sys/vmparam.h>
58 #include <kern/clock.h>
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
63 #include <nfs/nfsnode.h>
64 #include <nfs/xdr_subs.h>
65 #include <nfs/nfsm_subs.h>
66 #include <nfs/nfs_gss.h>
67 #include <nfs/nfsmount.h>
68 #include <nfs/nfs_lock.h>
70 #include <miscfs/specfs/specdev.h>
72 #include <netinet/in.h>
73 #include <net/kpi_interface.h>
77 * NFS_MAX_WHO is the maximum length of a string representation used
78 * in as an ace who, owner, or group. There is no explicit limit in the
79 * protocol, however the kauth routines have a limit of MAPATHLEN for
80 * strings including the trailing null character, so we impose that
81 * limit. This should be changed if kauth routines change.
83 * We also want some reasonable maximum, as 32 bits worth of string length
84 * is liable to cause problems. At the very least this limit must guarantee
85 * that any express that contains the 32 bit length from off the wire used in
86 * allocations does not overflow.
88 #define NFS_MAX_WHO MAXPATHLEN
91 * Create the unique client ID to use for this mount.
93 * Format: unique ID + en0_address + server_address + mntfromname + mntonname
95 * We could possibly use one client ID for all mounts of the same server;
96 * however, that would complicate some aspects of state management.
98 * Each mount socket connection sends a SETCLIENTID. If the ID is the same but
99 * the verifier (mounttime) changes, then all previous (mounts') state gets dropped.
101 * State is typically managed per-mount and in order to keep it that way
102 * each mount needs to use a separate client ID. However, we also need to
103 * make sure that each mount uses the same client ID each time.
105 * In an attempt to differentiate mounts we include the mntfromname and mntonname
106 * strings to the client ID (as long as they fit). We also make sure that the
107 * value does not conflict with any existing values in use (changing the unique ID).
109 * Note that info such as the server's address may change over the lifetime of the
110 * mount. But the client ID will not be updated because we don't want it changing
111 * simply because we switched to a different server address.
114 nfs4_init_clientid(struct nfsmount
*nmp
)
116 struct nfs_client_id
*ncip
, *ncip2
;
117 struct sockaddr
*saddr
;
120 struct vfsstatfs
*vsfs
;
122 static uint8_t en0addr
[6];
123 static uint8_t en0addr_set
= 0;
125 lck_mtx_lock(nfs_global_mutex
);
127 ifnet_t interface
= NULL
;
128 error
= ifnet_find_by_name("en0", &interface
);
130 error
= ifnet_lladdr_copy_bytes(interface
, en0addr
, sizeof(en0addr
));
133 printf("nfs4_init_clientid: error getting en0 address, %d\n", error
);
139 ifnet_release(interface
);
142 lck_mtx_unlock(nfs_global_mutex
);
144 MALLOC(ncip
, struct nfs_client_id
*, sizeof(struct nfs_client_id
), M_TEMP
, M_WAITOK
);
149 vsfs
= vfs_statfs(nmp
->nm_mountp
);
150 saddr
= nmp
->nm_saddr
;
151 ncip
->nci_idlen
= sizeof(uint32_t) + sizeof(en0addr
) + saddr
->sa_len
+
152 strlen(vsfs
->f_mntfromname
) + 1 + strlen(vsfs
->f_mntonname
) + 1;
153 if (ncip
->nci_idlen
> NFS4_OPAQUE_LIMIT
) {
154 ncip
->nci_idlen
= NFS4_OPAQUE_LIMIT
;
156 MALLOC(ncip
->nci_id
, char *, ncip
->nci_idlen
, M_TEMP
, M_WAITOK
);
162 *(uint32_t*)ncip
->nci_id
= 0;
163 len
= sizeof(uint32_t);
164 len2
= lmin(sizeof(en0addr
), ncip
->nci_idlen
- len
);
165 bcopy(en0addr
, &ncip
->nci_id
[len
], len2
);
166 len
+= sizeof(en0addr
);
167 len2
= lmin(saddr
->sa_len
, ncip
->nci_idlen
- len
);
168 bcopy(saddr
, &ncip
->nci_id
[len
], len2
);
170 if (len
< ncip
->nci_idlen
) {
171 len2
= strlcpy(&ncip
->nci_id
[len
], vsfs
->f_mntfromname
, ncip
->nci_idlen
- len
);
172 if (len2
< (ncip
->nci_idlen
- len
)) {
175 len
= ncip
->nci_idlen
;
178 if (len
< ncip
->nci_idlen
) {
179 len2
= strlcpy(&ncip
->nci_id
[len
], vsfs
->f_mntonname
, ncip
->nci_idlen
- len
);
180 if (len2
< (ncip
->nci_idlen
- len
)) {
183 len
= ncip
->nci_idlen
;
187 /* make sure the ID is unique, and add it to the sorted list */
188 lck_mtx_lock(nfs_global_mutex
);
189 TAILQ_FOREACH(ncip2
, &nfsclientids
, nci_link
) {
190 if (ncip
->nci_idlen
> ncip2
->nci_idlen
) {
193 if (ncip
->nci_idlen
< ncip2
->nci_idlen
) {
196 cmp
= bcmp(ncip
->nci_id
+ sizeof(uint32_t),
197 ncip2
->nci_id
+ sizeof(uint32_t),
198 ncip
->nci_idlen
- sizeof(uint32_t));
205 if (*(uint32_t*)ncip
->nci_id
> *(uint32_t*)ncip2
->nci_id
) {
208 if (*(uint32_t*)ncip
->nci_id
< *(uint32_t*)ncip2
->nci_id
) {
211 *(uint32_t*)ncip
->nci_id
+= 1;
213 if (*(uint32_t*)ncip
->nci_id
) {
214 printf("nfs client ID collision (%d) for %s on %s\n", *(uint32_t*)ncip
->nci_id
,
215 vsfs
->f_mntfromname
, vsfs
->f_mntonname
);
218 TAILQ_INSERT_BEFORE(ncip2
, ncip
, nci_link
);
220 TAILQ_INSERT_TAIL(&nfsclientids
, ncip
, nci_link
);
222 nmp
->nm_longid
= ncip
;
223 lck_mtx_unlock(nfs_global_mutex
);
232 nfs4_setclientid(struct nfsmount
*nmp
)
234 uint64_t verifier
, xid
;
235 int error
= 0, status
, numops
;
236 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
239 struct nfsm_chain nmreq
, nmrep
;
240 struct sockaddr_storage ss
;
241 void *sinaddr
= NULL
;
242 char raddr
[MAX_IPv6_STR_LEN
];
243 char uaddr
[MAX_IPv6_STR_LEN
+ 16];
247 thd
= current_thread();
248 cred
= IS_VALID_CRED(nmp
->nm_mcred
) ? nmp
->nm_mcred
: vfs_context_ucred(vfs_context_kernel());
249 kauth_cred_ref(cred
);
251 nfsm_chain_null(&nmreq
);
252 nfsm_chain_null(&nmrep
);
254 if (!nmp
->nm_longid
) {
255 error
= nfs4_init_clientid(nmp
);
260 nfsm_chain_build_alloc_init(error
, &nmreq
, 14 * NFSX_UNSIGNED
+ nmp
->nm_longid
->nci_idlen
);
261 nfsm_chain_add_compound_header(error
, &nmreq
, "setclid", nmp
->nm_minor_vers
, numops
);
263 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SETCLIENTID
);
264 /* nfs_client_id4 client; */
265 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_mounttime
);
266 nfsm_chain_add_32(error
, &nmreq
, nmp
->nm_longid
->nci_idlen
);
267 nfsm_chain_add_opaque(error
, &nmreq
, nmp
->nm_longid
->nci_id
, nmp
->nm_longid
->nci_idlen
);
269 /* cb_client4 callback; */
270 if (!NMFLAG(nmp
, NOCALLBACK
) && nmp
->nm_cbid
&& nfs4_cb_port
&&
271 !sock_getsockname(nmp
->nm_nso
->nso_so
, (struct sockaddr
*)&ss
, sizeof(ss
))) {
272 if (ss
.ss_family
== AF_INET
) {
273 sinaddr
= &((struct sockaddr_in
*)&ss
)->sin_addr
;
275 } else if (ss
.ss_family
== AF_INET6
) {
276 sinaddr
= &((struct sockaddr_in6
*)&ss
)->sin6_addr
;
277 port
= nfs4_cb_port6
;
282 if (sinaddr
&& port
&& (inet_ntop(ss
.ss_family
, sinaddr
, raddr
, sizeof(raddr
)) == raddr
)) {
283 /* assemble r_addr = universal address (nmp->nm_nso->nso_so source IP addr + port) */
284 ualen
= snprintf(uaddr
, sizeof(uaddr
), "%s.%d.%d", raddr
,
285 ((port
>> 8) & 0xff),
287 /* make sure it fit, give up if it didn't */
288 if (ualen
>= (int)sizeof(uaddr
)) {
294 /* add callback info */
295 nfsm_chain_add_32(error
, &nmreq
, NFS4_CALLBACK_PROG
); /* callback program */
296 if (ss
.ss_family
== AF_INET
) {
297 nfsm_chain_add_string(error
, &nmreq
, "tcp", 3); /* callback r_netid */
298 } else if (ss
.ss_family
== AF_INET6
) {
299 nfsm_chain_add_string(error
, &nmreq
, "tcp6", 4); /* callback r_netid */
301 nfsm_chain_add_string(error
, &nmreq
, uaddr
, ualen
); /* callback r_addr */
302 nfsm_chain_add_32(error
, &nmreq
, nmp
->nm_cbid
); /* callback_ident */
304 /* don't provide valid callback info */
305 nfsm_chain_add_32(error
, &nmreq
, 0); /* callback program */
306 nfsm_chain_add_string(error
, &nmreq
, "", 0); /* callback r_netid */
307 nfsm_chain_add_string(error
, &nmreq
, "", 0); /* callback r_addr */
308 nfsm_chain_add_32(error
, &nmreq
, 0); /* callback_ident */
310 nfsm_chain_build_done(error
, &nmreq
);
311 nfsm_assert(error
, (numops
== 0), EPROTO
);
313 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, NULL
, R_SETUP
, &nmrep
, &xid
, &status
);
314 nfsm_chain_skip_tag(error
, &nmrep
);
315 nfsm_chain_get_32(error
, &nmrep
, numops
);
316 if (!error
&& (numops
!= 1) && status
) {
319 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SETCLIENTID
);
320 if (error
== NFSERR_CLID_INUSE
) {
321 printf("nfs4_setclientid: client ID in use?\n");
324 nfsm_chain_get_64(error
, &nmrep
, nmp
->nm_clientid
);
325 nfsm_chain_get_64(error
, &nmrep
, verifier
);
326 nfsm_chain_cleanup(&nmreq
);
327 nfsm_chain_cleanup(&nmrep
);
329 // SETCLIENTID_CONFIRM
331 nfsm_chain_build_alloc_init(error
, &nmreq
, 15 * NFSX_UNSIGNED
);
332 nfsm_chain_add_compound_header(error
, &nmreq
, "setclid_conf", nmp
->nm_minor_vers
, numops
);
334 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SETCLIENTID_CONFIRM
);
335 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
336 nfsm_chain_add_64(error
, &nmreq
, verifier
);
337 nfsm_chain_build_done(error
, &nmreq
);
338 nfsm_assert(error
, (numops
== 0), EPROTO
);
340 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, NULL
, R_SETUP
, &nmrep
, &xid
, &status
);
341 nfsm_chain_skip_tag(error
, &nmrep
);
342 nfsm_chain_get_32(error
, &nmrep
, numops
);
343 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SETCLIENTID_CONFIRM
);
345 printf("nfs4_setclientid: confirm error %d\n", error
);
347 lck_mtx_lock(&nmp
->nm_lock
);
349 nmp
->nm_state
|= NFSSTA_CLIENTID
;
351 lck_mtx_unlock(&nmp
->nm_lock
);
353 nfsmout_if(error
|| !nmp
->nm_dnp
);
355 /* take the opportunity to refresh fs attributes too */
356 // PUTFH, GETATTR(FS)
358 nfsm_chain_build_alloc_init(error
, &nmreq
, 23 * NFSX_UNSIGNED
);
359 nfsm_chain_add_compound_header(error
, &nmreq
, "setclid_attr", nmp
->nm_minor_vers
, numops
);
361 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
362 nfsm_chain_add_fh(error
, &nmreq
, nmp
->nm_vers
, nmp
->nm_dnp
->n_fhp
, nmp
->nm_dnp
->n_fhsize
);
364 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
365 NFS_CLEAR_ATTRIBUTES(bitmap
);
366 NFS4_PER_FS_ATTRIBUTES(bitmap
);
367 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
368 nfsm_chain_build_done(error
, &nmreq
);
369 nfsm_assert(error
, (numops
== 0), EPROTO
);
371 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
, thd
, cred
, NULL
, R_SETUP
, &nmrep
, &xid
, &status
);
372 nfsm_chain_skip_tag(error
, &nmrep
);
373 nfsm_chain_get_32(error
, &nmrep
, numops
);
374 lck_mtx_lock(&nmp
->nm_lock
);
375 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
376 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
378 error
= nfs4_parsefattr(&nmrep
, &nmp
->nm_fsattr
, NULL
, NULL
, NULL
, NULL
);
380 lck_mtx_unlock(&nmp
->nm_lock
);
381 if (error
) { /* ignore any error from the getattr */
385 nfsm_chain_cleanup(&nmreq
);
386 nfsm_chain_cleanup(&nmrep
);
387 kauth_cred_unref(&cred
);
389 printf("nfs4_setclientid failed, %d\n", error
);
395 * renew/check lease state on server
398 nfs4_renew(struct nfsmount
*nmp
, int rpcflag
)
400 int error
= 0, status
, numops
;
402 struct nfsm_chain nmreq
, nmrep
;
405 cred
= IS_VALID_CRED(nmp
->nm_mcred
) ? nmp
->nm_mcred
: vfs_context_ucred(vfs_context_kernel());
406 kauth_cred_ref(cred
);
408 nfsm_chain_null(&nmreq
);
409 nfsm_chain_null(&nmrep
);
413 nfsm_chain_build_alloc_init(error
, &nmreq
, 8 * NFSX_UNSIGNED
);
414 nfsm_chain_add_compound_header(error
, &nmreq
, "renew", nmp
->nm_minor_vers
, numops
);
416 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_RENEW
);
417 nfsm_chain_add_64(error
, &nmreq
, nmp
->nm_clientid
);
418 nfsm_chain_build_done(error
, &nmreq
);
419 nfsm_assert(error
, (numops
== 0), EPROTO
);
421 error
= nfs_request2(NULL
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
422 current_thread(), cred
, NULL
, rpcflag
, &nmrep
, &xid
, &status
);
423 nfsm_chain_skip_tag(error
, &nmrep
);
424 nfsm_chain_get_32(error
, &nmrep
, numops
);
425 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_RENEW
);
427 nfsm_chain_cleanup(&nmreq
);
428 nfsm_chain_cleanup(&nmrep
);
429 kauth_cred_unref(&cred
);
435 * periodic timer to renew lease state on server
438 nfs4_renew_timer(void *param0
, __unused
void *param1
)
440 struct nfsmount
*nmp
= param0
;
442 int error
= 0, interval
;
444 lck_mtx_lock(&nmp
->nm_lock
);
445 clientid
= nmp
->nm_clientid
;
446 if ((nmp
->nm_state
& NFSSTA_RECOVER
) || !(nmp
->nm_sockflags
& NMSOCK_READY
)) {
447 lck_mtx_unlock(&nmp
->nm_lock
);
450 lck_mtx_unlock(&nmp
->nm_lock
);
452 error
= nfs4_renew(nmp
, R_RECOVER
);
454 if (error
== ETIMEDOUT
) {
455 nfs_need_reconnect(nmp
);
457 printf("nfs4_renew_timer: error %d\n", error
);
459 lck_mtx_lock(&nmp
->nm_lock
);
460 if (error
&& (error
!= ETIMEDOUT
) &&
461 (nmp
->nm_clientid
== clientid
) && !(nmp
->nm_state
& NFSSTA_RECOVER
)) {
462 printf("nfs4_renew_timer: error %d, initiating recovery\n", error
);
463 nfs_need_recover(nmp
, error
);
466 interval
= nmp
->nm_fsattr
.nfsa_lease
/ (error
? 4 : 2);
467 if ((interval
< 1) || (nmp
->nm_state
& NFSSTA_RECOVER
)) {
470 lck_mtx_unlock(&nmp
->nm_lock
);
471 nfs_interval_timer_start(nmp
->nm_renew_timer
, interval
* 1000);
475 * get the list of supported security flavors
477 * How we get them depends on what args we are given:
481 * YES YES Use the fh and name provided
482 * YES NO 4.1-only just use the fh provided
483 * NO YES Use the node's (or root) fh and the name provided
484 * NO NO Use the node's parent and the node's name (4.1 will just use node's fh)
487 nfs4_secinfo_rpc(struct nfsmount
*nmp
, struct nfsreq_secinfo_args
*siap
, kauth_cred_t cred
, uint32_t *sec
, int *seccountp
)
489 int error
= 0, status
, nfsvers
, numops
, fhsize
;
490 vnode_t dvp
= NULLVP
;
493 const char *vname
= NULL
, *name
;
496 struct nfsm_chain nmreq
, nmrep
;
499 if (nfs_mount_gone(nmp
)) {
502 nfsvers
= nmp
->nm_vers
;
505 nfsm_chain_null(&nmreq
);
506 nfsm_chain_null(&nmrep
);
509 fhsize
= fhp
? siap
->rsia_fhsize
: 0;
510 name
= siap
->rsia_name
;
511 namelen
= name
? siap
->rsia_namelen
: 0;
512 if (name
&& !namelen
) {
513 namelen
= strlen(name
);
516 if (!np
) { /* use PUTROOTFH */
520 fhsize
= np
->n_fhsize
;
529 nfs_node_lock_force(np
);
530 if ((vnode_vtype(NFSTOV(np
)) != VDIR
) && np
->n_sillyrename
) {
532 * The node's been sillyrenamed, so we need to use
533 * the sillyrename directory/name to do the open.
535 struct nfs_sillyrename
*nsp
= np
->n_sillyrename
;
538 if ((error
= vnode_get(dvp
))) {
544 fhsize
= dnp
->n_fhsize
;
545 name
= nsp
->nsr_name
;
546 namelen
= nsp
->nsr_namlen
;
549 * [sigh] We can't trust VFS to get the parent right for named
550 * attribute nodes. (It likes to reparent the nodes after we've
551 * created them.) Luckily we can probably get the right parent
552 * from the n_parent we have stashed away.
554 if ((np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
) &&
555 (((dvp
= np
->n_parent
)) && (error
= vnode_get(dvp
)))) {
559 dvp
= vnode_getparent(NFSTOV(np
));
561 vname
= vnode_getname(NFSTOV(np
));
562 if (!dvp
|| !vname
) {
571 fhsize
= dnp
->n_fhsize
;
573 namelen
= strnlen(vname
, MAXPATHLEN
);
578 // PUT(ROOT)FH + SECINFO
580 nfsm_chain_build_alloc_init(error
, &nmreq
,
581 4 * NFSX_UNSIGNED
+ NFSX_FH(nfsvers
) + nfsm_rndup(namelen
));
582 nfsm_chain_add_compound_header(error
, &nmreq
, "secinfo", nmp
->nm_minor_vers
, numops
);
585 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
586 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fhp
, fhsize
);
588 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTROOTFH
);
591 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_SECINFO
);
592 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
593 nfsm_chain_build_done(error
, &nmreq
);
594 nfsm_assert(error
, (numops
== 0), EPROTO
);
596 error
= nfs_request2(np
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
597 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
598 nfsm_chain_skip_tag(error
, &nmrep
);
599 nfsm_chain_get_32(error
, &nmrep
, numops
);
600 nfsm_chain_op_check(error
, &nmrep
, fhp
? NFS_OP_PUTFH
: NFS_OP_PUTROOTFH
);
601 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_SECINFO
);
603 error
= nfsm_chain_get_secinfo(&nmrep
, sec
, seccountp
);
605 nfsm_chain_cleanup(&nmreq
);
606 nfsm_chain_cleanup(&nmrep
);
608 vnode_putname(vname
);
615 #endif /* CONFIG_NFS4 */
618 * Parse an NFSv4 SECINFO array to an array of pseudo flavors.
619 * (Note: also works for MOUNTv3 security arrays.)
622 nfsm_chain_get_secinfo(struct nfsm_chain
*nmc
, uint32_t *sec
, int *seccountp
)
624 int error
= 0, secmax
, seccount
, srvcount
;
632 seccount
= srvcount
= 0;
636 nfsm_chain_get_32(error
, nmc
, srvcount
);
637 while (!error
&& (srvcount
> 0) && (seccount
< secmax
)) {
638 nfsm_chain_get_32(error
, nmc
, flavor
);
647 #endif /* CONFIG_NFS_GSS */
648 sec
[seccount
++] = flavor
;
652 /* we only recognize KRB5, KRB5I, KRB5P */
653 nfsm_chain_get_32(error
, nmc
, val
); /* OID length */
655 if (val
!= sizeof(krb5_mech_oid
)) {
656 nfsm_chain_adv(error
, nmc
, val
);
657 nfsm_chain_adv(error
, nmc
, 2 * NFSX_UNSIGNED
);
660 nfsm_chain_get_opaque(error
, nmc
, val
, oid
); /* OID bytes */
662 if (bcmp(oid
, krb5_mech_oid
, sizeof(krb5_mech_oid
))) {
663 nfsm_chain_adv(error
, nmc
, 2 * NFSX_UNSIGNED
);
666 nfsm_chain_get_32(error
, nmc
, val
); /* QOP */
667 nfsm_chain_get_32(error
, nmc
, val
); /* SERVICE */
670 case RPCSEC_GSS_SVC_NONE
:
671 sec
[seccount
++] = RPCAUTH_KRB5
;
673 case RPCSEC_GSS_SVC_INTEGRITY
:
674 sec
[seccount
++] = RPCAUTH_KRB5I
;
676 case RPCSEC_GSS_SVC_PRIVACY
:
677 sec
[seccount
++] = RPCAUTH_KRB5P
;
681 #endif /* CONFIG_NFS_GSS */
687 *seccountp
= seccount
;
694 * Fetch the FS_LOCATIONS attribute for the node found at directory/name.
697 nfs4_get_fs_locations(
698 struct nfsmount
*nmp
,
704 struct nfs_fs_locations
*nfslsp
)
706 int error
= 0, numops
, status
;
707 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
709 struct nfsreq_secinfo_args si
;
710 struct nfsm_chain nmreq
, nmrep
;
715 fhsize
= dnp
->n_fhsize
;
721 req
= zalloc(nfs_req_zone
);
722 nfsm_chain_null(&nmreq
);
723 nfsm_chain_null(&nmrep
);
725 NFSREQ_SECINFO_SET(&si
, NULL
, fhp
, fhsize
, name
, 0);
727 nfsm_chain_build_alloc_init(error
, &nmreq
, 18 * NFSX_UNSIGNED
);
728 nfsm_chain_add_compound_header(error
, &nmreq
, "fs_locations", nmp
->nm_minor_vers
, numops
);
730 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_PUTFH
);
731 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER4
, fhp
, fhsize
);
733 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_LOOKUP
);
734 nfsm_chain_add_name(error
, &nmreq
, name
, strlen(name
), nmp
);
736 nfsm_chain_add_32(error
, &nmreq
, NFS_OP_GETATTR
);
737 NFS_CLEAR_ATTRIBUTES(bitmap
);
738 NFS_BITMAP_SET(bitmap
, NFS_FATTR_FS_LOCATIONS
);
739 nfsm_chain_add_bitmap(error
, &nmreq
, bitmap
, NFS_ATTR_BITMAP_LEN
);
740 nfsm_chain_build_done(error
, &nmreq
);
741 nfsm_assert(error
, (numops
== 0), EPROTO
);
743 error
= nfs_request_async(dnp
, nmp
->nm_mountp
, &nmreq
, NFSPROC4_COMPOUND
,
744 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), &si
, 0, NULL
, &req
);
746 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
748 nfsm_chain_skip_tag(error
, &nmrep
);
749 nfsm_chain_get_32(error
, &nmrep
, numops
);
750 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_PUTFH
);
751 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_LOOKUP
);
752 nfsm_chain_op_check(error
, &nmrep
, NFS_OP_GETATTR
);
754 error
= nfs4_parsefattr(&nmrep
, NULL
, NULL
, NULL
, NULL
, nfslsp
);
756 NFS_ZFREE(nfs_req_zone
, req
);
757 nfsm_chain_cleanup(&nmrep
);
758 nfsm_chain_cleanup(&nmreq
);
763 * Referral trigger nodes may not have many attributes provided by the
764 * server, so put some default values in place.
767 nfs4_default_attrs_for_referral_trigger(
771 struct nfs_vattr
*nvap
,
778 nvap
->nva_flags
= NFS_FFLAG_TRIGGER
| NFS_FFLAG_TRIGGER_REFERRAL
;
779 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_TYPE
)) {
780 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TYPE
);
781 nvap
->nva_type
= VDIR
;
783 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_FSID
)) {
784 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_FSID
);
785 nvap
->nva_fsid
.major
= 0;
786 nvap
->nva_fsid
.minor
= 0;
788 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER
) && dnp
) {
789 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_OWNER
);
790 nvap
->nva_uid
= dnp
->n_vattr
.nva_uid
;
791 nvap
->nva_uuuid
= dnp
->n_vattr
.nva_uuuid
;
793 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER_GROUP
) && dnp
) {
794 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_OWNER_GROUP
);
795 nvap
->nva_gid
= dnp
->n_vattr
.nva_gid
;
796 nvap
->nva_guuid
= dnp
->n_vattr
.nva_guuid
;
798 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_MODE
)) {
799 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_MODE
);
800 nvap
->nva_mode
= 0777;
802 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_SIZE
)) {
803 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_SIZE
);
806 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_SPACE_USED
)) {
807 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_SPACE_USED
);
810 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_NUMLINKS
)) {
811 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_NUMLINKS
);
814 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_TIME_ACCESS
)) {
815 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_ACCESS
);
816 nvap
->nva_timesec
[NFSTIME_ACCESS
] = now
.tv_sec
;
817 nvap
->nva_timensec
[NFSTIME_ACCESS
] = now
.tv_nsec
;
819 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_TIME_MODIFY
)) {
820 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_MODIFY
);
821 nvap
->nva_timesec
[NFSTIME_MODIFY
] = now
.tv_sec
;
822 nvap
->nva_timensec
[NFSTIME_MODIFY
] = now
.tv_nsec
;
824 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_TIME_METADATA
)) {
825 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_METADATA
);
826 nvap
->nva_timesec
[NFSTIME_CHANGE
] = now
.tv_sec
;
827 nvap
->nva_timensec
[NFSTIME_CHANGE
] = now
.tv_nsec
;
829 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_FILEID
)) {
830 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_FILEID
);
831 nvap
->nva_fileid
= 42;
833 if (!NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_FILEHANDLE
) && dnp
&& name
&& fhp
) {
834 /* Build a fake filehandle made up of parent node pointer and name */
835 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_FILEHANDLE
);
836 bcopy(&dnp
, &fhp
->fh_data
[0], sizeof(dnp
));
837 len
= sizeof(fhp
->fh_data
) - sizeof(dnp
);
838 bcopy(name
, &fhp
->fh_data
[0] + sizeof(dnp
), MIN(len
, namelen
));
839 fhp
->fh_len
= sizeof(dnp
) + namelen
;
840 if (fhp
->fh_len
> (int)sizeof(fhp
->fh_data
)) {
841 fhp
->fh_len
= sizeof(fhp
->fh_data
);
847 * Set NFS bitmap according to what's set in vnode_attr (and supported by the server).
850 nfs_vattr_set_bitmap(struct nfsmount
*nmp
, uint32_t *bitmap
, struct vnode_attr
*vap
)
854 NFS_CLEAR_ATTRIBUTES(bitmap
);
855 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
856 NFS_BITMAP_SET(bitmap
, NFS_FATTR_SIZE
);
858 if (VATTR_IS_ACTIVE(vap
, va_acl
) && (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_ACL
)) {
859 NFS_BITMAP_SET(bitmap
, NFS_FATTR_ACL
);
861 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
862 NFS_BITMAP_SET(bitmap
, NFS_FATTR_ARCHIVE
);
863 NFS_BITMAP_SET(bitmap
, NFS_FATTR_HIDDEN
);
865 // NFS_BITMAP_SET(bitmap, NFS_FATTR_MIMETYPE)
866 if (VATTR_IS_ACTIVE(vap
, va_mode
) && !NMFLAG(nmp
, ACLONLY
)) {
867 NFS_BITMAP_SET(bitmap
, NFS_FATTR_MODE
);
869 if (VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
870 NFS_BITMAP_SET(bitmap
, NFS_FATTR_OWNER
);
872 if (VATTR_IS_ACTIVE(vap
, va_gid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) {
873 NFS_BITMAP_SET(bitmap
, NFS_FATTR_OWNER_GROUP
);
875 // NFS_BITMAP_SET(bitmap, NFS_FATTR_SYSTEM)
876 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
877 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_ACCESS_SET
);
878 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_MODIFY_SET
);
880 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
881 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_ACCESS_SET
);
883 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
884 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_MODIFY_SET
);
887 if (VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
888 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_BACKUP
);
890 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
891 NFS_BITMAP_SET(bitmap
, NFS_FATTR_TIME_CREATE
);
893 /* and limit to what is supported by server */
894 for (i
= 0; i
< NFS_ATTR_BITMAP_LEN
; i
++) {
895 bitmap
[i
] &= nmp
->nm_fsattr
.nfsa_supp_attr
[i
];
900 * Convert between NFSv4 and VFS ACE types
903 nfs4_ace_nfstype_to_vfstype(uint32_t nfsacetype
, int *errorp
)
905 switch (nfsacetype
) {
906 case NFS_ACE_ACCESS_ALLOWED_ACE_TYPE
:
907 return KAUTH_ACE_PERMIT
;
908 case NFS_ACE_ACCESS_DENIED_ACE_TYPE
:
909 return KAUTH_ACE_DENY
;
910 case NFS_ACE_SYSTEM_AUDIT_ACE_TYPE
:
911 return KAUTH_ACE_AUDIT
;
912 case NFS_ACE_SYSTEM_ALARM_ACE_TYPE
:
913 return KAUTH_ACE_ALARM
;
920 nfs4_ace_vfstype_to_nfstype(uint32_t vfstype
, int *errorp
)
923 case KAUTH_ACE_PERMIT
:
924 return NFS_ACE_ACCESS_ALLOWED_ACE_TYPE
;
926 return NFS_ACE_ACCESS_DENIED_ACE_TYPE
;
927 case KAUTH_ACE_AUDIT
:
928 return NFS_ACE_SYSTEM_AUDIT_ACE_TYPE
;
929 case KAUTH_ACE_ALARM
:
930 return NFS_ACE_SYSTEM_ALARM_ACE_TYPE
;
937 * Convert between NFSv4 and VFS ACE flags
940 nfs4_ace_nfsflags_to_vfsflags(uint32_t nfsflags
)
942 uint32_t vfsflags
= 0;
944 if (nfsflags
& NFS_ACE_FILE_INHERIT_ACE
) {
945 vfsflags
|= KAUTH_ACE_FILE_INHERIT
;
947 if (nfsflags
& NFS_ACE_DIRECTORY_INHERIT_ACE
) {
948 vfsflags
|= KAUTH_ACE_DIRECTORY_INHERIT
;
950 if (nfsflags
& NFS_ACE_NO_PROPAGATE_INHERIT_ACE
) {
951 vfsflags
|= KAUTH_ACE_LIMIT_INHERIT
;
953 if (nfsflags
& NFS_ACE_INHERIT_ONLY_ACE
) {
954 vfsflags
|= KAUTH_ACE_ONLY_INHERIT
;
956 if (nfsflags
& NFS_ACE_SUCCESSFUL_ACCESS_ACE_FLAG
) {
957 vfsflags
|= KAUTH_ACE_SUCCESS
;
959 if (nfsflags
& NFS_ACE_FAILED_ACCESS_ACE_FLAG
) {
960 vfsflags
|= KAUTH_ACE_FAILURE
;
962 if (nfsflags
& NFS_ACE_INHERITED_ACE
) {
963 vfsflags
|= KAUTH_ACE_INHERITED
;
970 nfs4_ace_vfsflags_to_nfsflags(uint32_t vfsflags
)
972 uint32_t nfsflags
= 0;
974 if (vfsflags
& KAUTH_ACE_FILE_INHERIT
) {
975 nfsflags
|= NFS_ACE_FILE_INHERIT_ACE
;
977 if (vfsflags
& KAUTH_ACE_DIRECTORY_INHERIT
) {
978 nfsflags
|= NFS_ACE_DIRECTORY_INHERIT_ACE
;
980 if (vfsflags
& KAUTH_ACE_LIMIT_INHERIT
) {
981 nfsflags
|= NFS_ACE_NO_PROPAGATE_INHERIT_ACE
;
983 if (vfsflags
& KAUTH_ACE_ONLY_INHERIT
) {
984 nfsflags
|= NFS_ACE_INHERIT_ONLY_ACE
;
986 if (vfsflags
& KAUTH_ACE_SUCCESS
) {
987 nfsflags
|= NFS_ACE_SUCCESSFUL_ACCESS_ACE_FLAG
;
989 if (vfsflags
& KAUTH_ACE_FAILURE
) {
990 nfsflags
|= NFS_ACE_FAILED_ACCESS_ACE_FLAG
;
992 if (vfsflags
& KAUTH_ACE_INHERITED
) {
993 nfsflags
|= NFS_ACE_INHERITED_ACE
;
1000 * Convert between NFSv4 ACE access masks and VFS access rights
1003 nfs4_ace_nfsmask_to_vfsrights(uint32_t nfsmask
)
1005 uint32_t vfsrights
= 0;
1007 if (nfsmask
& NFS_ACE_READ_DATA
) {
1008 vfsrights
|= KAUTH_VNODE_READ_DATA
;
1010 if (nfsmask
& NFS_ACE_LIST_DIRECTORY
) {
1011 vfsrights
|= KAUTH_VNODE_LIST_DIRECTORY
;
1013 if (nfsmask
& NFS_ACE_WRITE_DATA
) {
1014 vfsrights
|= KAUTH_VNODE_WRITE_DATA
;
1016 if (nfsmask
& NFS_ACE_ADD_FILE
) {
1017 vfsrights
|= KAUTH_VNODE_ADD_FILE
;
1019 if (nfsmask
& NFS_ACE_APPEND_DATA
) {
1020 vfsrights
|= KAUTH_VNODE_APPEND_DATA
;
1022 if (nfsmask
& NFS_ACE_ADD_SUBDIRECTORY
) {
1023 vfsrights
|= KAUTH_VNODE_ADD_SUBDIRECTORY
;
1025 if (nfsmask
& NFS_ACE_READ_NAMED_ATTRS
) {
1026 vfsrights
|= KAUTH_VNODE_READ_EXTATTRIBUTES
;
1028 if (nfsmask
& NFS_ACE_WRITE_NAMED_ATTRS
) {
1029 vfsrights
|= KAUTH_VNODE_WRITE_EXTATTRIBUTES
;
1031 if (nfsmask
& NFS_ACE_EXECUTE
) {
1032 vfsrights
|= KAUTH_VNODE_EXECUTE
;
1034 if (nfsmask
& NFS_ACE_DELETE_CHILD
) {
1035 vfsrights
|= KAUTH_VNODE_DELETE_CHILD
;
1037 if (nfsmask
& NFS_ACE_READ_ATTRIBUTES
) {
1038 vfsrights
|= KAUTH_VNODE_READ_ATTRIBUTES
;
1040 if (nfsmask
& NFS_ACE_WRITE_ATTRIBUTES
) {
1041 vfsrights
|= KAUTH_VNODE_WRITE_ATTRIBUTES
;
1043 if (nfsmask
& NFS_ACE_DELETE
) {
1044 vfsrights
|= KAUTH_VNODE_DELETE
;
1046 if (nfsmask
& NFS_ACE_READ_ACL
) {
1047 vfsrights
|= KAUTH_VNODE_READ_SECURITY
;
1049 if (nfsmask
& NFS_ACE_WRITE_ACL
) {
1050 vfsrights
|= KAUTH_VNODE_WRITE_SECURITY
;
1052 if (nfsmask
& NFS_ACE_WRITE_OWNER
) {
1053 vfsrights
|= KAUTH_VNODE_CHANGE_OWNER
;
1055 if (nfsmask
& NFS_ACE_SYNCHRONIZE
) {
1056 vfsrights
|= KAUTH_VNODE_SYNCHRONIZE
;
1058 if ((nfsmask
& NFS_ACE_GENERIC_READ
) == NFS_ACE_GENERIC_READ
) {
1059 vfsrights
|= KAUTH_ACE_GENERIC_READ
;
1061 if ((nfsmask
& NFS_ACE_GENERIC_WRITE
) == NFS_ACE_GENERIC_WRITE
) {
1062 vfsrights
|= KAUTH_ACE_GENERIC_WRITE
;
1064 if ((nfsmask
& NFS_ACE_GENERIC_EXECUTE
) == NFS_ACE_GENERIC_EXECUTE
) {
1065 vfsrights
|= KAUTH_ACE_GENERIC_EXECUTE
;
1072 nfs4_ace_vfsrights_to_nfsmask(uint32_t vfsrights
)
1074 uint32_t nfsmask
= 0;
1076 if (vfsrights
& KAUTH_VNODE_READ_DATA
) {
1077 nfsmask
|= NFS_ACE_READ_DATA
;
1079 if (vfsrights
& KAUTH_VNODE_LIST_DIRECTORY
) {
1080 nfsmask
|= NFS_ACE_LIST_DIRECTORY
;
1082 if (vfsrights
& KAUTH_VNODE_WRITE_DATA
) {
1083 nfsmask
|= NFS_ACE_WRITE_DATA
;
1085 if (vfsrights
& KAUTH_VNODE_ADD_FILE
) {
1086 nfsmask
|= NFS_ACE_ADD_FILE
;
1088 if (vfsrights
& KAUTH_VNODE_APPEND_DATA
) {
1089 nfsmask
|= NFS_ACE_APPEND_DATA
;
1091 if (vfsrights
& KAUTH_VNODE_ADD_SUBDIRECTORY
) {
1092 nfsmask
|= NFS_ACE_ADD_SUBDIRECTORY
;
1094 if (vfsrights
& KAUTH_VNODE_READ_EXTATTRIBUTES
) {
1095 nfsmask
|= NFS_ACE_READ_NAMED_ATTRS
;
1097 if (vfsrights
& KAUTH_VNODE_WRITE_EXTATTRIBUTES
) {
1098 nfsmask
|= NFS_ACE_WRITE_NAMED_ATTRS
;
1100 if (vfsrights
& KAUTH_VNODE_EXECUTE
) {
1101 nfsmask
|= NFS_ACE_EXECUTE
;
1103 if (vfsrights
& KAUTH_VNODE_DELETE_CHILD
) {
1104 nfsmask
|= NFS_ACE_DELETE_CHILD
;
1106 if (vfsrights
& KAUTH_VNODE_READ_ATTRIBUTES
) {
1107 nfsmask
|= NFS_ACE_READ_ATTRIBUTES
;
1109 if (vfsrights
& KAUTH_VNODE_WRITE_ATTRIBUTES
) {
1110 nfsmask
|= NFS_ACE_WRITE_ATTRIBUTES
;
1112 if (vfsrights
& KAUTH_VNODE_DELETE
) {
1113 nfsmask
|= NFS_ACE_DELETE
;
1115 if (vfsrights
& KAUTH_VNODE_READ_SECURITY
) {
1116 nfsmask
|= NFS_ACE_READ_ACL
;
1118 if (vfsrights
& KAUTH_VNODE_WRITE_SECURITY
) {
1119 nfsmask
|= NFS_ACE_WRITE_ACL
;
1121 if (vfsrights
& KAUTH_VNODE_CHANGE_OWNER
) {
1122 nfsmask
|= NFS_ACE_WRITE_OWNER
;
1124 if (vfsrights
& KAUTH_VNODE_SYNCHRONIZE
) {
1125 nfsmask
|= NFS_ACE_SYNCHRONIZE
;
1127 if (vfsrights
& KAUTH_ACE_GENERIC_READ
) {
1128 nfsmask
|= NFS_ACE_GENERIC_READ
;
1130 if (vfsrights
& KAUTH_ACE_GENERIC_WRITE
) {
1131 nfsmask
|= NFS_ACE_GENERIC_WRITE
;
1133 if (vfsrights
& KAUTH_ACE_GENERIC_EXECUTE
) {
1134 nfsmask
|= NFS_ACE_GENERIC_EXECUTE
;
1136 if (vfsrights
& KAUTH_ACE_GENERIC_ALL
) {
1137 nfsmask
|= (KAUTH_ACE_GENERIC_READ
| KAUTH_ACE_GENERIC_WRITE
| NFS_ACE_GENERIC_EXECUTE
);
1145 * mapid a wellknown identity to guid.
1146 * Return 0 on success ENOENT if id does not map and EINVAL if the id is not a well known name.
1149 nfs4_wkid2sid(const char *id
, ntsid_t
*sp
)
1151 size_t len
= strnlen(id
, MAXIDNAMELEN
);
1153 if (len
== MAXIDNAMELEN
|| id
[len
- 1] != '@') {
1157 bzero(sp
, sizeof(ntsid_t
));
1159 sp
->sid_authcount
= 1;
1160 if (!strcmp(id
, "OWNER@")) {
1162 sp
->sid_authority
[5] = 3;
1163 sp
->sid_authorities
[0] = 0;
1164 } else if (!strcmp(id
, "GROUP@")) {
1166 sp
->sid_authority
[5] = 3;
1167 sp
->sid_authorities
[0] = 1;
1168 } else if (!strcmp(id
, "EVERYONE@")) {
1170 sp
->sid_authority
[5] = 1;
1171 sp
->sid_authorities
[0] = 0;
1172 } else if (!strcmp(id
, "INTERACTIVE@")) {
1174 sp
->sid_authority
[5] = 5;
1175 sp
->sid_authorities
[0] = 4;
1176 } else if (!strcmp(id
, "NETWORK@")) {
1178 sp
->sid_authority
[5] = 5;
1179 sp
->sid_authorities
[0] = 2;
1180 } else if (!strcmp(id
, "DIALUP@")) {
1182 sp
->sid_authority
[5] = 5;
1183 sp
->sid_authorities
[0] = 1;
1184 } else if (!strcmp(id
, "BATCH@")) {
1186 sp
->sid_authority
[5] = 5;
1187 sp
->sid_authorities
[0] = 3;
1188 } else if (!strcmp(id
, "ANONYMOUS@")) {
1190 sp
->sid_authority
[5] = 5;
1191 sp
->sid_authorities
[0] = 7;
1192 } else if (!strcmp(id
, "AUTHENTICATED@")) {
1194 sp
->sid_authority
[5] = 5;
1195 sp
->sid_authorities
[0] = 11;
1196 } else if (!strcmp(id
, "SERVICE@")) {
1198 sp
->sid_authority
[5] = 5;
1199 sp
->sid_authorities
[0] = 6;
1202 sp
->sid_authority
[5] = 0;
1203 sp
->sid_authorities
[0] = 0;
1209 nfs4_fallback_name(const char *id
, int have_at
)
1212 /* must be user@domain */
1213 /* try to identify some well-known IDs */
1214 if (!strncmp(id
, "root@", 5)) {
1216 } else if (!strncmp(id
, "wheel@", 6)) {
1218 } else if (!strncmp(id
, "nobody@", 7)) {
1220 } else if (!strncmp(id
, "nfsnobody@", 10)) {
1228 nfs4_mapid_log(int error
, const char *idstr
, int isgroup
, guid_t
*gp
)
1230 if (error
&& (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_FAILED_MAPPINGS
)) {
1231 printf("nfs4_id2guid: idmap failed for %s %s error %d\n", idstr
, isgroup
? "G" : " ", error
);
1233 if (!error
&& (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_SUCCESSFUL_MAPPINGS
)) {
1234 printf("nfs4_id2guid: idmap for %s %s got guid "
1235 "%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x\n",
1236 idstr
, isgroup
? "G" : " ",
1237 gp
->g_guid
[0], gp
->g_guid
[1], gp
->g_guid
[2], gp
->g_guid
[3],
1238 gp
->g_guid
[4], gp
->g_guid
[5], gp
->g_guid
[6], gp
->g_guid
[7],
1239 gp
->g_guid
[8], gp
->g_guid
[9], gp
->g_guid
[10], gp
->g_guid
[11],
1240 gp
->g_guid
[12], gp
->g_guid
[13], gp
->g_guid
[14], gp
->g_guid
[15]);
1245 nfs4_map_domain(char *id
, char **atp
)
1248 char *dsnode
, *otw_nfs4domain
;
1249 char *new_id
= NULL
;
1250 size_t otw_domain_len
;
1251 size_t otw_id_2_at_len
;
1255 at
= strchr(id
, '@');
1257 if (at
== NULL
|| *at
!= '@') {
1261 otw_nfs4domain
= at
+ 1;
1262 otw_domain_len
= strnlen(otw_nfs4domain
, MAXPATHLEN
);
1263 otw_id_2_at_len
= at
- id
+ 1;
1265 dsnode
= zalloc(ZV_NAMEI
);
1266 /* first try to map nfs4 domain to dsnode for scoped lookups */
1267 error
= kauth_cred_nfs4domain2dsnode(otw_nfs4domain
, dsnode
);
1269 /* Success! Make new id be id@dsnode */
1270 size_t dsnode_len
= strnlen(dsnode
, MAXPATHLEN
);
1271 size_t new_id_len
= otw_id_2_at_len
+ dsnode_len
+ 1;
1274 MALLOC(new_id
, char*, new_id_len
, M_TEMP
, M_WAITOK
);
1275 tmp
= *otw_nfs4domain
;
1276 *otw_nfs4domain
= '\0'; /* Chop of the old domain */
1277 strlcpy(new_id
, id
, MAXPATHLEN
);
1278 *otw_nfs4domain
= tmp
; /* Be nice and preserve callers original id */
1279 strlcat(new_id
, dsnode
, MAXPATHLEN
);
1280 at
= strchr(new_id
, '@');
1282 /* Bummer:-( See if default nfs4 set for unscoped lookup */
1283 size_t default_domain_len
= strnlen(nfs4_default_domain
, MAXPATHLEN
);
1285 if ((otw_domain_len
== default_domain_len
) &&
1286 (strncmp(otw_nfs4domain
, nfs4_default_domain
, otw_domain_len
) == 0)) {
1287 /* Woohoo! We have matching domains, do unscoped lookups */
1291 NFS_ZFREE(ZV_NAMEI
, dsnode
);
1293 if (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_SUCCESSFUL_MAPPINGS
) {
1294 printf("nfs4_id2guid: after domain mapping id is %s\n", id
);
1302 * Map an NFSv4 ID string to a VFS guid.
1304 * Try to use the ID mapping service... but we may fallback to trying to do it ourselves.
1307 nfs4_id2guid(/*const*/ char *id
, guid_t
*guidp
, int isgroup
)
1312 char *p
, *at
, *new_id
= NULL
;
1314 *guidp
= kauth_null_guid
;
1317 * First check if it is just a simple numeric ID string or a special "XXX@" name.
1318 * If it's a number, there's no need trying to ask the IDMAP service to map it.
1319 * If it's a special "XXX@" name, we want to make sure to treat it as a group.
1325 if ((*p
< '0') || (*p
> '9')) {
1335 /* must be numeric ID (or empty) */
1336 num
= *id
? strtol(id
, NULL
, 10) : -2;
1338 error
= kauth_cred_gid2guid((gid_t
)num
, guidp
);
1340 error
= kauth_cred_uid2guid((uid_t
)num
, guidp
);
1342 nfs4_mapid_log(error
, id
, isgroup
, guidp
);
1346 /* See if this is a well known NFSv4 name */
1347 error
= nfs4_wkid2sid(id
, &sid
);
1349 error
= kauth_cred_ntsid2guid(&sid
, guidp
);
1350 nfs4_mapid_log(error
, id
, 1, guidp
);
1354 /* Handle nfs4 domain first */
1356 new_id
= nfs4_map_domain(id
, &at
);
1362 /* Now try to do actual id mapping */
1363 if (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_USE_IDMAP_SERVICE
) {
1365 * Ask the ID mapping service to map the ID string to a GUID.
1367 * [sigh] this isn't a "pwnam/grnam" it's an NFS ID string!
1370 error
= kauth_cred_grnam2guid(id
, guidp
);
1372 error
= kauth_cred_pwnam2guid(id
, guidp
);
1374 nfs4_mapid_log(error
, id
, isgroup
, guidp
);
1381 * fallback path... see if we can come up with an answer ourselves.
1383 num
= nfs4_fallback_name(id
, at
!= NULL
);
1385 error
= kauth_cred_gid2guid((gid_t
)num
, guidp
);
1387 error
= kauth_cred_uid2guid((uid_t
)num
, guidp
);
1389 nfs4_mapid_log(error
, id
, isgroup
, guidp
);
1393 /* restore @ symbol in case we clobered for unscoped lookup */
1394 if (at
&& *at
== '\0') {
1398 /* free mapped domain id string */
1400 FREE(new_id
, M_TEMP
);
1408 * mapid a wellknown identity to guid.
1409 * returns well known name for the sid or NULL if sid does not map.
1411 #define MAXWELLKNOWNID 18
1414 nfs4_sid2wkid(ntsid_t
*sp
)
1416 if ((sp
->sid_kind
== 1) && (sp
->sid_authcount
== 1)) {
1417 /* check if it's one of our well-known ACE WHO names */
1418 if (sp
->sid_authority
[5] == 0) {
1419 if (sp
->sid_authorities
[0] == 0) { // S-1-0-0
1420 return "nobody@localdomain";
1422 } else if (sp
->sid_authority
[5] == 1) {
1423 if (sp
->sid_authorities
[0] == 0) { // S-1-1-0
1426 } else if (sp
->sid_authority
[5] == 3) {
1427 if (sp
->sid_authorities
[0] == 0) { // S-1-3-0
1429 } else if (sp
->sid_authorities
[0] == 1) { // S-1-3-1
1432 } else if (sp
->sid_authority
[5] == 5) {
1433 if (sp
->sid_authorities
[0] == 1) { // S-1-5-1
1435 } else if (sp
->sid_authorities
[0] == 2) { // S-1-5-2
1437 } else if (sp
->sid_authorities
[0] == 3) { // S-1-5-3
1439 } else if (sp
->sid_authorities
[0] == 4) { // S-1-5-4
1440 return "INTERACTIVE@";
1441 } else if (sp
->sid_authorities
[0] == 6) { // S-1-5-6
1443 } else if (sp
->sid_authorities
[0] == 7) { // S-1-5-7
1444 return "ANONYMOUS@";
1445 } else if (sp
->sid_authorities
[0] == 11) { // S-1-5-11
1446 return "AUTHENTICATED@";
1454 nfs4_mapguid_log(int error
, const char *where
, guid_t
*gp
, int isgroup
, const char *idstr
)
1456 if (error
&& (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_FAILED_MAPPINGS
)) {
1457 printf("nfs4_guid2id: %s idmap failed for "
1458 "%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x %s "
1459 "error %d\n", where
,
1460 gp
->g_guid
[0], gp
->g_guid
[1], gp
->g_guid
[2], gp
->g_guid
[3],
1461 gp
->g_guid
[4], gp
->g_guid
[5], gp
->g_guid
[6], gp
->g_guid
[7],
1462 gp
->g_guid
[8], gp
->g_guid
[9], gp
->g_guid
[10], gp
->g_guid
[11],
1463 gp
->g_guid
[12], gp
->g_guid
[13], gp
->g_guid
[14], gp
->g_guid
[15],
1464 isgroup
? "G" : " ", error
);
1466 if (!error
&& (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_SUCCESSFUL_MAPPINGS
)) {
1467 printf("nfs4_guid2id: %s idmap for "
1468 "%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x_%02x%02x%02x%02x %s "
1469 "got ID %s\n", where
,
1470 gp
->g_guid
[0], gp
->g_guid
[1], gp
->g_guid
[2], gp
->g_guid
[3],
1471 gp
->g_guid
[4], gp
->g_guid
[5], gp
->g_guid
[6], gp
->g_guid
[7],
1472 gp
->g_guid
[8], gp
->g_guid
[9], gp
->g_guid
[10], gp
->g_guid
[11],
1473 gp
->g_guid
[12], gp
->g_guid
[13], gp
->g_guid
[14], gp
->g_guid
[15],
1474 isgroup
? "G" : " ", idstr
);
1479 nfs4_addv4domain(char *id
, size_t *idlen
)
1481 char *at
= NULL
, *cp
;
1487 if (id
== NULL
|| *id
== '\0') {
1491 for (cp
= id
; *cp
!= '\0'; cp
++) {
1498 have_domain
= (at
&& at
[1] != '\0');
1501 char *dsnode
= at
+ 1;
1504 char *mapped_domain
;
1506 nfs4domain
= zalloc(ZV_NAMEI
);
1507 error
= kauth_cred_dsnode2nfs4domain(dsnode
, nfs4domain
);
1509 domain_len
= strnlen(nfs4domain
, MAXPATHLEN
);
1510 mapped_domain
= nfs4domain
;
1513 domain_len
= strnlen(nfs4_default_domain
, MAXPATHLEN
);
1514 mapped_domain
= nfs4_default_domain
;
1517 /* chop off id after the '@' */
1519 /* Add our mapped_domain */
1520 idsize
= strlcat(id
, mapped_domain
, *idlen
);
1521 if (*idlen
> idsize
) {
1527 NFS_ZFREE(ZV_NAMEI
, nfs4domain
);
1528 } else if (at
== NULL
) {
1530 * If we didn't find an 'at' then cp points to the end of id passed in.
1531 * and if we have a nfs4_default_domain set. Try to append the
1532 * default domain if we have root or set ENOSPC.
1534 size_t default_domain_len
= strnlen(nfs4_default_domain
, MAXPATHLEN
);
1536 if (default_domain_len
) {
1537 strlcat(id
, "@", *idlen
);
1538 idsize
= strlcat(id
, nfs4_default_domain
, *idlen
);
1539 if (*idlen
> idsize
) {
1545 ; /* Unscoped name otw */
1549 if (!error
&& nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_SUCCESSFUL_MAPPINGS
) {
1550 printf("nfs4_guid2id: id after nfs4 domain map: %s[%zd].\n", id
, *idlen
);
1557 nfs4_fallback_id(int numid
, int isgrp
, char *buf
, size_t size
)
1559 const char *idp
= NULL
;
1561 if (!(nfs_idmap_ctrl
& NFS_IDMAP_CTRL_FALLBACK_NO_COMMON_IDS
)) {
1562 /* map well known uid's to strings */
1564 idp
= isgrp
? "wheel" : "root";
1565 } else if (numid
== -2) {
1570 /* or just use a decimal number string. */
1571 snprintf(buf
, size
- 1, "%d", numid
);
1572 buf
[size
- 1] = '\0';
1574 size_t idplen
= strlcpy(buf
, idp
, size
);
1575 if (idplen
>= size
) {
1584 * Map a VFS guid to an NFSv4 ID string.
1586 * Try to use the ID mapping service... but we may fallback to trying to do it ourselves.
1589 nfs4_guid2id(guid_t
*guidp
, char *id
, size_t *idlen
, int isgroup
)
1597 id1buf
= id1
= NULL
;
1601 * See if our guid maps to a well known NFSv4 name
1603 error
= kauth_cred_guid2ntsid(guidp
, &sid
);
1605 const char *wkid
= nfs4_sid2wkid(&sid
);
1607 len
= strnlen(wkid
, MAXWELLKNOWNID
);
1608 strlcpy(id
, wkid
, *idlen
);
1609 error
= (len
< *idlen
) ? 0 : ENOSPC
;
1611 nfs4_mapguid_log(error
, "kauth_cred_guid2ntsid", guidp
, 1, id
);
1615 nfs4_mapguid_log(error
, "kauth_cred_guid2ntsid", guidp
, isgroup
, NULL
);
1618 if (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_USE_IDMAP_SERVICE
) {
1620 * Ask the ID mapping service to map the GUID to an ID string.
1622 * [sigh] this isn't a "pwnam" it's an NFS id string!
1626 * Stupid kauth_cred_guid2pwnam() function requires that the buffer
1627 * be at least MAXPATHLEN bytes long even though most if not all ID
1628 * strings will be much much shorter than that.
1631 if (*idlen
< MAXPATHLEN
) {
1632 id1buf
= zalloc(ZV_NAMEI
);
1634 id1len
= MAXPATHLEN
;
1641 error
= kauth_cred_guid2grnam(guidp
, id1
);
1643 error
= kauth_cred_guid2pwnam(guidp
, id1
);
1646 nfs4_mapguid_log(error
, "kauth_cred2[pw|gr]nam", guidp
, isgroup
, id1
);
1654 * fallback path... see if we can come up with an answer ourselves.
1658 /* OK, let's just try mapping it to a UID/GID */
1660 error
= kauth_cred_guid2gid(guidp
, (gid_t
*)&uid
);
1662 error
= kauth_cred_guid2uid(guidp
, &uid
);
1665 char *fbidp
= nfs4_fallback_id(uid
, isgroup
, numbuf
, sizeof(numbuf
));
1666 if (fbidp
== NULL
) {
1673 error
= nfs4_addv4domain(id1
, &id1len
);
1678 /* copy idmap result to output buffer */
1679 len
= strlcpy(id
, id1
, *idlen
);
1680 if (len
>= *idlen
) {
1687 nfs4_mapguid_log(error
, "End of routine", guidp
, isgroup
, id1
);
1690 NFS_ZFREE(ZV_NAMEI
, id1buf
);
1697 * Set a vnode attr's supported bits according to the given bitmap
1700 nfs_vattr_set_supported(uint32_t *bitmap
, struct vnode_attr
*vap
)
1702 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TYPE
)) {
1703 VATTR_SET_SUPPORTED(vap
, va_type
);
1705 // if (NFS_BITMAP_ISSET(bitmap, NFS_FATTR_CHANGE))
1706 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SIZE
)) {
1707 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1709 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FSID
)) {
1710 VATTR_SET_SUPPORTED(vap
, va_fsid
);
1712 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
)) {
1713 VATTR_SET_SUPPORTED(vap
, va_acl
);
1715 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ARCHIVE
)) {
1716 VATTR_SET_SUPPORTED(vap
, va_flags
);
1718 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILEID
)) {
1719 VATTR_SET_SUPPORTED(vap
, va_fileid
);
1721 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_HIDDEN
)) {
1722 VATTR_SET_SUPPORTED(vap
, va_flags
);
1724 // if (NFS_BITMAP_ISSET(bitmap, NFS_FATTR_MIMETYPE))
1725 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
1726 VATTR_SET_SUPPORTED(vap
, va_mode
);
1728 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_NUMLINKS
)) {
1729 VATTR_SET_SUPPORTED(vap
, va_nlink
);
1731 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER
)) {
1732 VATTR_SET_SUPPORTED(vap
, va_uid
);
1733 VATTR_SET_SUPPORTED(vap
, va_uuuid
);
1735 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER_GROUP
)) {
1736 VATTR_SET_SUPPORTED(vap
, va_gid
);
1737 VATTR_SET_SUPPORTED(vap
, va_guuid
);
1739 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_RAWDEV
)) {
1740 VATTR_SET_SUPPORTED(vap
, va_rdev
);
1742 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SPACE_USED
)) {
1743 VATTR_SET_SUPPORTED(vap
, va_total_alloc
);
1745 // if (NFS_BITMAP_ISSET(bitmap, NFS_FATTR_SYSTEM))
1746 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_ACCESS
)) {
1747 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1749 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_BACKUP
)) {
1750 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1752 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_CREATE
)) {
1753 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1755 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_METADATA
)) {
1756 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1758 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_MODIFY
)) {
1759 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1764 * Parse the attributes that are in the mbuf list and store them in
1765 * the given structures.
1769 struct nfsm_chain
*nmc
,
1770 struct nfs_fsattr
*nfsap
,
1771 struct nfs_vattr
*nvap
,
1774 struct nfs_fs_locations
*nfslsp
)
1776 int error
= 0, error2
, rderror
= 0, attrbytes
;
1777 uint32_t val
, val2
, val3
, i
;
1778 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], len
;
1781 struct nfs_fsattr nfsa_dummy
;
1782 struct nfs_vattr
*nva_dummy
= NULL
;
1783 struct dqblk dqb_dummy
;
1784 kauth_acl_t acl
= NULL
;
1785 uint32_t ace_type
, ace_flags
, ace_mask
;
1786 struct nfs_fs_locations nfsls_dummy
;
1787 struct sockaddr_storage ss
;
1789 /* if not interested in some values... throw 'em into a local dummy variable */
1791 nfsap
= &nfsa_dummy
;
1794 MALLOC(nva_dummy
, struct nfs_vattr
*, sizeof(*nva_dummy
), M_TEMP
, M_WAITOK
);
1801 nfslsp
= &nfsls_dummy
;
1803 bzero(nfslsp
, sizeof(*nfslsp
));
1805 attrbytes
= val
= val2
= val3
= 0;
1807 slen
= sizeof(sbuf
);
1810 len
= NFS_ATTR_BITMAP_LEN
;
1811 nfsm_chain_get_bitmap(error
, nmc
, bitmap
, len
);
1812 /* add bits to object/fs attr bitmaps */
1813 for (i
= 0; i
< NFS_ATTR_BITMAP_LEN
; i
++) {
1814 nvap
->nva_bitmap
[i
] |= bitmap
[i
] & nfs_object_attr_bitmap
[i
];
1815 nfsap
->nfsa_bitmap
[i
] |= bitmap
[i
] & nfs_fs_attr_bitmap
[i
];
1818 nfsm_chain_get_32(error
, nmc
, attrbytes
);
1821 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SUPPORTED_ATTRS
)) {
1822 len
= NFS_ATTR_BITMAP_LEN
;
1823 nfsm_chain_get_bitmap(error
, nmc
, nfsap
->nfsa_supp_attr
, len
);
1824 attrbytes
-= (len
+ 1) * NFSX_UNSIGNED
;
1826 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TYPE
)) {
1827 nfsm_chain_get_32(error
, nmc
, val
);
1828 nvap
->nva_type
= nfstov_type(val
, NFS_VER4
);
1829 if ((val
== NFATTRDIR
) || (val
== NFNAMEDATTR
)) {
1830 nvap
->nva_flags
|= NFS_FFLAG_IS_ATTR
;
1832 nvap
->nva_flags
&= ~NFS_FFLAG_IS_ATTR
;
1834 attrbytes
-= NFSX_UNSIGNED
;
1836 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FH_EXPIRE_TYPE
)) {
1837 nfsm_chain_get_32(error
, nmc
, val
);
1839 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_FHTYPE_MASK
;
1840 nfsap
->nfsa_flags
|= val
<< NFS_FSFLAG_FHTYPE_SHIFT
;
1842 printf("nfs: warning unknown fh type: 0x%x\n", val
);
1844 attrbytes
-= NFSX_UNSIGNED
;
1846 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_CHANGE
)) {
1847 nfsm_chain_get_64(error
, nmc
, nvap
->nva_change
);
1848 attrbytes
-= 2 * NFSX_UNSIGNED
;
1850 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SIZE
)) {
1851 nfsm_chain_get_64(error
, nmc
, nvap
->nva_size
);
1852 attrbytes
-= 2 * NFSX_UNSIGNED
;
1854 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_LINK_SUPPORT
)) {
1855 nfsm_chain_get_32(error
, nmc
, val
);
1857 nfsap
->nfsa_flags
|= NFS_FSFLAG_LINK
;
1859 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_LINK
;
1861 attrbytes
-= NFSX_UNSIGNED
;
1863 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SYMLINK_SUPPORT
)) {
1864 nfsm_chain_get_32(error
, nmc
, val
);
1866 nfsap
->nfsa_flags
|= NFS_FSFLAG_SYMLINK
;
1868 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_SYMLINK
;
1870 attrbytes
-= NFSX_UNSIGNED
;
1872 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_NAMED_ATTR
)) {
1873 nfsm_chain_get_32(error
, nmc
, val
);
1875 nvap
->nva_flags
|= NFS_FFLAG_HAS_NAMED_ATTRS
;
1877 nvap
->nva_flags
&= ~NFS_FFLAG_HAS_NAMED_ATTRS
;
1879 attrbytes
-= NFSX_UNSIGNED
;
1881 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FSID
)) {
1882 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fsid
.major
);
1883 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fsid
.minor
);
1884 attrbytes
-= 4 * NFSX_UNSIGNED
;
1886 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_UNIQUE_HANDLES
)) {
1887 nfsm_chain_get_32(error
, nmc
, val
);
1889 nfsap
->nfsa_flags
|= NFS_FSFLAG_UNIQUE_FH
;
1891 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_UNIQUE_FH
;
1893 attrbytes
-= NFSX_UNSIGNED
;
1895 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_LEASE_TIME
)) {
1896 nfsm_chain_get_32(error
, nmc
, nfsap
->nfsa_lease
);
1897 attrbytes
-= NFSX_UNSIGNED
;
1899 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_RDATTR_ERROR
)) {
1900 nfsm_chain_get_32(error
, nmc
, rderror
);
1901 attrbytes
-= NFSX_UNSIGNED
;
1902 if (!rderror
) { /* no error */
1903 NFS_BITMAP_CLR(bitmap
, NFS_FATTR_RDATTR_ERROR
);
1904 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_RDATTR_ERROR
);
1907 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
)) {
1909 ace_type
= ace_flags
= ace_mask
= 0;
1910 nfsm_chain_get_32(error
, nmc
, val
); /* ACE count */
1911 if (!error
&& (val
> KAUTH_ACL_MAX_ENTRIES
)) {
1914 if (!error
&& !((acl
= kauth_acl_alloc(val
)))) {
1917 if (!error
&& acl
) {
1918 acl
->acl_entrycount
= val
;
1921 attrbytes
-= NFSX_UNSIGNED
;
1922 nfsm_assert(error
, (attrbytes
>= 0), EBADRPC
);
1923 for (i
= 0; !error
&& (i
< val
); i
++) {
1924 nfsm_chain_get_32(error
, nmc
, ace_type
);
1925 nfsm_chain_get_32(error
, nmc
, ace_flags
);
1926 nfsm_chain_get_32(error
, nmc
, ace_mask
);
1927 nfsm_chain_get_32(error
, nmc
, len
);
1928 if (!error
&& len
>= NFS_MAX_WHO
) {
1931 acl
->acl_ace
[i
].ace_flags
= nfs4_ace_nfstype_to_vfstype(ace_type
, &error
);
1932 acl
->acl_ace
[i
].ace_flags
|= nfs4_ace_nfsflags_to_vfsflags(ace_flags
);
1933 acl
->acl_ace
[i
].ace_rights
= nfs4_ace_nfsmask_to_vfsrights(ace_mask
);
1934 if (!error
&& !error2
&& (len
>= slen
)) {
1938 slen
= sizeof(sbuf
);
1940 /* Let's add a bit more if we can to the allocation as to try and avoid future allocations */
1941 MALLOC(s
, char*, (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
, M_TEMP
, M_WAITOK
);
1943 slen
= (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
;
1949 nfsm_chain_adv(error
, nmc
, nfsm_rndup(len
));
1951 nfsm_chain_get_opaque(error
, nmc
, len
, s
);
1953 if (!error
&& !error2
) {
1955 error2
= nfs4_id2guid(s
, &acl
->acl_ace
[i
].ace_applicable
,
1956 (ace_flags
& NFS_ACE_IDENTIFIER_GROUP
));
1957 if (error2
&& (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_FAILED_MAPPINGS
)) {
1958 printf("nfs4_parsefattr: ACE WHO %s is no one, no guid?, error %d\n", s
, error2
);
1961 attrbytes
-= 4 * NFSX_UNSIGNED
+ nfsm_rndup(len
);
1962 nfsm_assert(error
, (attrbytes
>= 0), EBADRPC
);
1965 if ((nvap
!= nva_dummy
) && !error2
) {
1966 nvap
->nva_acl
= acl
;
1970 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACLSUPPORT
)) {
1972 * Support ACLs if: the server supports DENY/ALLOC ACEs and
1973 * (just to be safe) FATTR_ACL is in the supported list too.
1975 nfsm_chain_get_32(error
, nmc
, val
);
1976 if ((val
& (NFS_ACL_SUPPORT_ALLOW_ACL
| NFS_ACL_SUPPORT_DENY_ACL
)) &&
1977 NFS_BITMAP_ISSET(nfsap
->nfsa_supp_attr
, NFS_FATTR_ACL
)) {
1978 nfsap
->nfsa_flags
|= NFS_FSFLAG_ACL
;
1980 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_ACL
;
1982 attrbytes
-= NFSX_UNSIGNED
;
1984 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ARCHIVE
)) { /* SF_ARCHIVED */
1985 nfsm_chain_get_32(error
, nmc
, val
);
1987 nvap
->nva_flags
|= NFS_FFLAG_ARCHIVED
;
1989 nvap
->nva_flags
&= ~NFS_FFLAG_ARCHIVED
;
1991 attrbytes
-= NFSX_UNSIGNED
;
1993 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_CANSETTIME
)) {
1994 nfsm_chain_get_32(error
, nmc
, val
);
1996 nfsap
->nfsa_flags
|= NFS_FSFLAG_SET_TIME
;
1998 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_SET_TIME
;
2000 attrbytes
-= NFSX_UNSIGNED
;
2002 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_CASE_INSENSITIVE
)) {
2003 nfsm_chain_get_32(error
, nmc
, val
);
2005 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
2007 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_CASE_INSENSITIVE
;
2009 attrbytes
-= NFSX_UNSIGNED
;
2011 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_CASE_PRESERVING
)) {
2012 nfsm_chain_get_32(error
, nmc
, val
);
2014 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
2016 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_CASE_PRESERVING
;
2018 attrbytes
-= NFSX_UNSIGNED
;
2020 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_CHOWN_RESTRICTED
)) {
2021 nfsm_chain_get_32(error
, nmc
, val
);
2023 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
2025 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_CHOWN_RESTRICTED
;
2027 attrbytes
-= NFSX_UNSIGNED
;
2029 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILEHANDLE
)) {
2030 nfsm_chain_get_32(error
, nmc
, val
);
2031 if (error
== 0 && val
> NFS_MAX_FH_SIZE
) {
2037 nfsm_chain_get_opaque(error
, nmc
, nfsm_rndup(val
), fhp
->fh_data
);
2039 nfsm_chain_adv(error
, nmc
, nfsm_rndup(val
));
2041 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(val
);
2043 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILEID
)) {
2044 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fileid
);
2045 attrbytes
-= 2 * NFSX_UNSIGNED
;
2047 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILES_AVAIL
)) {
2048 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_files_avail
);
2049 attrbytes
-= 2 * NFSX_UNSIGNED
;
2051 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILES_FREE
)) {
2052 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_files_free
);
2053 attrbytes
-= 2 * NFSX_UNSIGNED
;
2055 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FILES_TOTAL
)) {
2056 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_files_total
);
2057 attrbytes
-= 2 * NFSX_UNSIGNED
;
2059 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_FS_LOCATIONS
)) {
2060 uint32_t loc
, serv
, comp
;
2061 struct nfs_fs_location
*fsl
;
2062 struct nfs_fs_server
*fss
;
2063 struct nfs_fs_path
*fsp
;
2065 /* get root pathname */
2066 fsp
= &nfslsp
->nl_root
;
2067 nfsm_chain_get_32(error
, nmc
, fsp
->np_compcount
); /* component count */
2068 attrbytes
-= NFSX_UNSIGNED
;
2069 /* sanity check component count */
2070 if (!error
&& (fsp
->np_compcount
> MAXPATHLEN
)) {
2074 if (fsp
->np_compcount
) {
2075 MALLOC(fsp
->np_components
, char **, fsp
->np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
2076 if (!fsp
->np_components
) {
2080 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++) {
2081 nfsm_chain_get_32(error
, nmc
, val
); /* component length */
2082 /* sanity check component length */
2083 if (!error
&& (val
== 0)) {
2085 * Apparently some people think a path with zero components should
2086 * be encoded with one zero-length component. So, just ignore any
2087 * zero length components.
2090 fsp
->np_compcount
--;
2091 if (fsp
->np_compcount
== 0) {
2092 FREE(fsp
->np_components
, M_TEMP
);
2093 fsp
->np_components
= NULL
;
2095 attrbytes
-= NFSX_UNSIGNED
;
2098 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
))) {
2102 MALLOC(fsp
->np_components
[comp
], char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2103 if (!fsp
->np_components
[comp
]) {
2107 nfsm_chain_get_opaque(error
, nmc
, val
, fsp
->np_components
[comp
]); /* component */
2108 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(val
);
2110 nfsm_chain_get_32(error
, nmc
, nfslsp
->nl_numlocs
); /* fs location count */
2111 attrbytes
-= NFSX_UNSIGNED
;
2112 /* sanity check location count */
2113 if (!error
&& (nfslsp
->nl_numlocs
> 256)) {
2117 if (nfslsp
->nl_numlocs
> 0) {
2118 MALLOC(nfslsp
->nl_locations
, struct nfs_fs_location
**, nfslsp
->nl_numlocs
* sizeof(struct nfs_fs_location
*), M_TEMP
, M_WAITOK
| M_ZERO
);
2119 if (!nfslsp
->nl_locations
) {
2124 for (loc
= 0; loc
< nfslsp
->nl_numlocs
; loc
++) {
2126 MALLOC(fsl
, struct nfs_fs_location
*, sizeof(struct nfs_fs_location
), M_TEMP
, M_WAITOK
| M_ZERO
);
2130 nfslsp
->nl_locations
[loc
] = fsl
;
2131 nfsm_chain_get_32(error
, nmc
, fsl
->nl_servcount
); /* server count */
2132 attrbytes
-= NFSX_UNSIGNED
;
2133 /* sanity check server count */
2134 if (!error
&& ((fsl
->nl_servcount
< 1) || (fsl
->nl_servcount
> 256))) {
2138 MALLOC(fsl
->nl_servers
, struct nfs_fs_server
**, fsl
->nl_servcount
* sizeof(struct nfs_fs_server
*), M_TEMP
, M_WAITOK
| M_ZERO
);
2139 if (!fsl
->nl_servers
) {
2142 for (serv
= 0; serv
< fsl
->nl_servcount
; serv
++) {
2144 MALLOC(fss
, struct nfs_fs_server
*, sizeof(struct nfs_fs_server
), M_TEMP
, M_WAITOK
| M_ZERO
);
2148 fsl
->nl_servers
[serv
] = fss
;
2149 nfsm_chain_get_32(error
, nmc
, val
); /* server name length */
2150 /* sanity check server name length */
2151 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
))) {
2155 MALLOC(fss
->ns_name
, char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2156 if (!fss
->ns_name
) {
2159 nfsm_chain_get_opaque(error
, nmc
, val
, fss
->ns_name
); /* server name */
2160 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(val
);
2162 /* copy name to address if it converts to a sockaddr */
2163 if (nfs_uaddr2sockaddr(fss
->ns_name
, (struct sockaddr
*)&ss
)) {
2164 fss
->ns_addrcount
= 1;
2165 MALLOC(fss
->ns_addresses
, char **, sizeof(char *), M_TEMP
, M_WAITOK
| M_ZERO
);
2166 if (!fss
->ns_addresses
) {
2170 MALLOC(fss
->ns_addresses
[0], char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2171 if (!fss
->ns_addresses
[0]) {
2175 strlcpy(fss
->ns_addresses
[0], fss
->ns_name
, val
+ 1);
2179 fsp
= &fsl
->nl_path
;
2180 nfsm_chain_get_32(error
, nmc
, fsp
->np_compcount
); /* component count */
2181 attrbytes
-= NFSX_UNSIGNED
;
2182 /* sanity check component count */
2183 if (!error
&& (fsp
->np_compcount
> MAXPATHLEN
)) {
2187 if (fsp
->np_compcount
) {
2188 MALLOC(fsp
->np_components
, char **, fsp
->np_compcount
* sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
2189 if (!fsp
->np_components
) {
2193 for (comp
= 0; comp
< fsp
->np_compcount
; comp
++) {
2194 nfsm_chain_get_32(error
, nmc
, val
); /* component length */
2195 /* sanity check component length */
2196 if (!error
&& (val
== 0)) {
2198 * Apparently some people think a path with zero components should
2199 * be encoded with one zero-length component. So, just ignore any
2200 * zero length components.
2203 fsp
->np_compcount
--;
2204 if (fsp
->np_compcount
== 0) {
2205 FREE(fsp
->np_components
, M_TEMP
);
2206 fsp
->np_components
= NULL
;
2208 attrbytes
-= NFSX_UNSIGNED
;
2211 if (!error
&& ((val
< 1) || (val
> MAXPATHLEN
))) {
2215 MALLOC(fsp
->np_components
[comp
], char *, val
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
2216 if (!fsp
->np_components
[comp
]) {
2219 nfsm_chain_get_opaque(error
, nmc
, val
, fsp
->np_components
[comp
]); /* component */
2220 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(val
);
2223 nfsm_assert(error
, (attrbytes
>= 0), EBADRPC
);
2225 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_HIDDEN
)) { /* UF_HIDDEN */
2226 nfsm_chain_get_32(error
, nmc
, val
);
2228 nvap
->nva_flags
|= NFS_FFLAG_HIDDEN
;
2230 nvap
->nva_flags
&= ~NFS_FFLAG_HIDDEN
;
2232 attrbytes
-= NFSX_UNSIGNED
;
2234 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_HOMOGENEOUS
)) {
2235 /* XXX If NOT homogeneous, we may need to clear flags on the mount */
2236 nfsm_chain_get_32(error
, nmc
, val
);
2238 nfsap
->nfsa_flags
|= NFS_FSFLAG_HOMOGENEOUS
;
2240 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_HOMOGENEOUS
;
2242 attrbytes
-= NFSX_UNSIGNED
;
2244 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MAXFILESIZE
)) {
2245 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_maxfilesize
);
2246 attrbytes
-= 2 * NFSX_UNSIGNED
;
2248 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MAXLINK
)) {
2249 nfsm_chain_get_32(error
, nmc
, nvap
->nva_maxlink
);
2250 if (!error
&& (nfsap
->nfsa_maxlink
> INT32_MAX
)) {
2251 nfsap
->nfsa_maxlink
= INT32_MAX
;
2253 attrbytes
-= NFSX_UNSIGNED
;
2255 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MAXNAME
)) {
2256 nfsm_chain_get_32(error
, nmc
, nfsap
->nfsa_maxname
);
2257 if (!error
&& (nfsap
->nfsa_maxname
> INT32_MAX
)) {
2258 nfsap
->nfsa_maxname
= INT32_MAX
;
2260 attrbytes
-= NFSX_UNSIGNED
;
2262 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MAXREAD
)) {
2263 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_maxread
);
2264 attrbytes
-= 2 * NFSX_UNSIGNED
;
2266 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MAXWRITE
)) {
2267 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_maxwrite
);
2268 attrbytes
-= 2 * NFSX_UNSIGNED
;
2270 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MIMETYPE
)) {
2271 nfsm_chain_get_32(error
, nmc
, val
);
2272 nfsm_chain_adv(error
, nmc
, nfsm_rndup(val
));
2273 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(val
);
2275 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
2276 nfsm_chain_get_32(error
, nmc
, val
);
2277 if (val
> ALLPERMS
) {
2280 nvap
->nva_mode
= (mode_t
)val
;
2282 attrbytes
-= NFSX_UNSIGNED
;
2284 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_NO_TRUNC
)) {
2285 nfsm_chain_get_32(error
, nmc
, val
);
2287 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
2289 nfsap
->nfsa_flags
&= ~NFS_FSFLAG_NO_TRUNC
;
2291 attrbytes
-= NFSX_UNSIGNED
;
2293 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_NUMLINKS
)) {
2294 nfsm_chain_get_32(error
, nmc
, val
);
2295 nvap
->nva_nlink
= val
;
2296 attrbytes
-= NFSX_UNSIGNED
;
2298 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER
)) {
2299 nfsm_chain_get_32(error
, nmc
, len
);
2300 if (!error
&& len
>= NFS_MAX_WHO
) {
2303 if (!error
&& (len
>= slen
)) {
2307 slen
= sizeof(sbuf
);
2309 /* Let's add a bit more if we can to the allocation as to try and avoid future allocations */
2310 MALLOC(s
, char*, (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
, M_TEMP
, M_WAITOK
);
2312 slen
= (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
;
2317 nfsm_chain_get_opaque(error
, nmc
, len
, s
);
2320 error
= nfs4_id2guid(s
, &nvap
->nva_uuuid
, 0);
2322 error
= kauth_cred_guid2uid(&nvap
->nva_uuuid
, &nvap
->nva_uid
);
2325 /* unable to get either GUID or UID, set to default */
2326 nvap
->nva_uid
= (uid_t
)(-2);
2327 if (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_FAILED_MAPPINGS
) {
2328 printf("nfs4_parsefattr: owner %s is no one, no %s?, error %d\n", s
,
2329 kauth_guid_equal(&nvap
->nva_uuuid
, &kauth_null_guid
) ? "guid" : "uid",
2335 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(len
);
2337 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER_GROUP
)) {
2338 nfsm_chain_get_32(error
, nmc
, len
);
2339 if (!error
&& len
>= NFS_MAX_WHO
) {
2342 if (!error
&& (len
>= slen
)) {
2346 slen
= sizeof(sbuf
);
2348 /* Let's add a bit more if we can to the allocation as to try and avoid future allocations */
2349 MALLOC(s
, char*, (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
, M_TEMP
, M_WAITOK
);
2351 slen
= (len
+ 16 < NFS_MAX_WHO
) ? len
+ 16 : NFS_MAX_WHO
;
2356 nfsm_chain_get_opaque(error
, nmc
, len
, s
);
2359 error
= nfs4_id2guid(s
, &nvap
->nva_guuid
, 1);
2361 error
= kauth_cred_guid2gid(&nvap
->nva_guuid
, &nvap
->nva_gid
);
2364 /* unable to get either GUID or GID, set to default */
2365 nvap
->nva_gid
= (gid_t
)(-2);
2366 if (nfs_idmap_ctrl
& NFS_IDMAP_CTRL_LOG_FAILED_MAPPINGS
) {
2367 printf("nfs4_parsefattr: group %s is no one, no %s?, error %d\n", s
,
2368 kauth_guid_equal(&nvap
->nva_guuid
, &kauth_null_guid
) ? "guid" : "gid",
2374 attrbytes
-= NFSX_UNSIGNED
+ nfsm_rndup(len
);
2376 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_QUOTA_AVAIL_HARD
)) {
2377 nfsm_chain_get_64(error
, nmc
, dqbp
->dqb_bhardlimit
);
2378 attrbytes
-= 2 * NFSX_UNSIGNED
;
2380 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_QUOTA_AVAIL_SOFT
)) {
2381 nfsm_chain_get_64(error
, nmc
, dqbp
->dqb_bsoftlimit
);
2382 attrbytes
-= 2 * NFSX_UNSIGNED
;
2384 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_QUOTA_USED
)) {
2385 nfsm_chain_get_64(error
, nmc
, dqbp
->dqb_curbytes
);
2386 attrbytes
-= 2 * NFSX_UNSIGNED
;
2388 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_RAWDEV
)) {
2389 nfsm_chain_get_32(error
, nmc
, nvap
->nva_rawdev
.specdata1
);
2390 nfsm_chain_get_32(error
, nmc
, nvap
->nva_rawdev
.specdata2
);
2391 attrbytes
-= 2 * NFSX_UNSIGNED
;
2393 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SPACE_AVAIL
)) {
2394 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_space_avail
);
2395 attrbytes
-= 2 * NFSX_UNSIGNED
;
2397 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SPACE_FREE
)) {
2398 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_space_free
);
2399 attrbytes
-= 2 * NFSX_UNSIGNED
;
2401 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SPACE_TOTAL
)) {
2402 nfsm_chain_get_64(error
, nmc
, nfsap
->nfsa_space_total
);
2403 attrbytes
-= 2 * NFSX_UNSIGNED
;
2405 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SPACE_USED
)) {
2406 nfsm_chain_get_64(error
, nmc
, nvap
->nva_bytes
);
2407 attrbytes
-= 2 * NFSX_UNSIGNED
;
2409 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SYSTEM
)) {
2410 /* we'd support this if we had a flag to map it to... */
2411 nfsm_chain_adv(error
, nmc
, NFSX_UNSIGNED
);
2412 attrbytes
-= NFSX_UNSIGNED
;
2414 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_ACCESS
)) {
2415 nfsm_chain_get_64(error
, nmc
, nvap
->nva_timesec
[NFSTIME_ACCESS
]);
2416 nfsm_chain_get_32(error
, nmc
, nvap
->nva_timensec
[NFSTIME_ACCESS
]);
2417 attrbytes
-= 3 * NFSX_UNSIGNED
;
2419 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_ACCESS_SET
)) {
2420 nfsm_chain_adv(error
, nmc
, 4 * NFSX_UNSIGNED
); /* just skip it */
2421 attrbytes
-= 4 * NFSX_UNSIGNED
;
2423 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_BACKUP
)) {
2424 nfsm_chain_get_64(error
, nmc
, nvap
->nva_timesec
[NFSTIME_BACKUP
]);
2425 nfsm_chain_get_32(error
, nmc
, nvap
->nva_timensec
[NFSTIME_BACKUP
]);
2426 attrbytes
-= 3 * NFSX_UNSIGNED
;
2428 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_CREATE
)) {
2429 nfsm_chain_get_64(error
, nmc
, nvap
->nva_timesec
[NFSTIME_CREATE
]);
2430 nfsm_chain_get_32(error
, nmc
, nvap
->nva_timensec
[NFSTIME_CREATE
]);
2431 attrbytes
-= 3 * NFSX_UNSIGNED
;
2433 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_DELTA
)) { /* skip for now */
2434 nfsm_chain_adv(error
, nmc
, 3 * NFSX_UNSIGNED
);
2435 attrbytes
-= 3 * NFSX_UNSIGNED
;
2437 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_METADATA
)) {
2438 nfsm_chain_get_64(error
, nmc
, nvap
->nva_timesec
[NFSTIME_CHANGE
]);
2439 nfsm_chain_get_32(error
, nmc
, nvap
->nva_timensec
[NFSTIME_CHANGE
]);
2440 attrbytes
-= 3 * NFSX_UNSIGNED
;
2442 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_MODIFY
)) {
2443 nfsm_chain_get_64(error
, nmc
, nvap
->nva_timesec
[NFSTIME_MODIFY
]);
2444 nfsm_chain_get_32(error
, nmc
, nvap
->nva_timensec
[NFSTIME_MODIFY
]);
2445 attrbytes
-= 3 * NFSX_UNSIGNED
;
2447 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_MODIFY_SET
)) {
2448 nfsm_chain_adv(error
, nmc
, 4 * NFSX_UNSIGNED
); /* just skip it */
2449 attrbytes
-= 4 * NFSX_UNSIGNED
;
2451 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MOUNTED_ON_FILEID
)) {
2453 /* we prefer the mounted on file ID, so just replace the fileid */
2454 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fileid
);
2456 nfsm_chain_adv(error
, nmc
, 2 * NFSX_UNSIGNED
);
2458 attrbytes
-= 2 * NFSX_UNSIGNED
;
2460 /* advance over any leftover attrbytes */
2461 nfsm_assert(error
, (attrbytes
>= 0), EBADRPC
);
2462 nfsm_chain_adv(error
, nmc
, nfsm_rndup(attrbytes
));
2465 nfs_fs_locations_cleanup(nfslsp
);
2467 if (!error
&& rderror
) {
2470 /* free up temporary resources */
2471 if (s
&& (s
!= sbuf
)) {
2475 kauth_acl_free(acl
);
2477 if (error
&& nvap
->nva_acl
) {
2478 kauth_acl_free(nvap
->nva_acl
);
2479 nvap
->nva_acl
= NULL
;
2481 FREE(nva_dummy
, M_TEMP
);
2486 * Add an NFSv4 "sattr" structure to an mbuf chain
2489 nfsm_chain_add_fattr4_f(struct nfsm_chain
*nmc
, struct vnode_attr
*vap
, struct nfsmount
*nmp
)
2491 int error
= 0, attrbytes
, i
, isgroup
;
2493 uint32_t *pattrbytes
, val
, acecount
;;
2494 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
];
2500 slen
= sizeof(sbuf
);
2502 /* First calculate the bitmap... */
2503 nfs_vattr_set_bitmap(nmp
, bitmap
, vap
);
2506 * Now pack it all together:
2507 * BITMAP, #BYTES, ATTRS
2508 * Keep a pointer to the length so we can set it later.
2510 nfsm_chain_add_bitmap(error
, nmc
, bitmap
, NFS_ATTR_BITMAP_LEN
);
2512 nfsm_chain_add_32(error
, nmc
, attrbytes
);
2513 pattrbytes
= (uint32_t*)(nmc
->nmc_ptr
- NFSX_UNSIGNED
);
2515 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_SIZE
)) {
2516 nfsm_chain_add_64(error
, nmc
, vap
->va_data_size
);
2517 attrbytes
+= 2 * NFSX_UNSIGNED
;
2519 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ACL
)) {
2521 if (!acl
|| (acl
->acl_entrycount
== KAUTH_FILESEC_NOACL
)) {
2524 acecount
= acl
->acl_entrycount
;
2526 nfsm_chain_add_32(error
, nmc
, acecount
);
2527 attrbytes
+= NFSX_UNSIGNED
;
2528 for (i
= 0; !error
&& (i
< (int)acecount
); i
++) {
2529 val
= (acl
->acl_ace
[i
].ace_flags
& KAUTH_ACE_KINDMASK
);
2530 val
= nfs4_ace_vfstype_to_nfstype(val
, &error
);
2531 nfsm_chain_add_32(error
, nmc
, val
);
2532 val
= nfs4_ace_vfsflags_to_nfsflags(acl
->acl_ace
[i
].ace_flags
);
2533 isgroup
= (kauth_cred_guid2gid(&acl
->acl_ace
[i
].ace_applicable
, &gid
) == 0);
2534 val
|= (isgroup
) ? NFS_ACE_IDENTIFIER_GROUP
: 0;
2535 nfsm_chain_add_32(error
, nmc
, val
);
2536 val
= nfs4_ace_vfsrights_to_nfsmask(acl
->acl_ace
[i
].ace_rights
);
2537 nfsm_chain_add_32(error
, nmc
, val
);
2539 error
= nfs4_guid2id(&acl
->acl_ace
[i
].ace_applicable
, s
, &len
, isgroup
);
2540 if (error
== ENOSPC
) {
2546 MALLOC(s
, char*, len
, M_TEMP
, M_WAITOK
);
2549 error
= nfs4_guid2id(&acl
->acl_ace
[i
].ace_applicable
, s
, &len
, isgroup
);
2554 nfsm_chain_add_name(error
, nmc
, s
, len
, nmp
);
2555 attrbytes
+= 4 * NFSX_UNSIGNED
+ nfsm_rndup(len
);
2558 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_ARCHIVE
)) {
2559 nfsm_chain_add_32(error
, nmc
, (vap
->va_flags
& SF_ARCHIVED
) ? 1 : 0);
2560 attrbytes
+= NFSX_UNSIGNED
;
2562 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_HIDDEN
)) {
2563 nfsm_chain_add_32(error
, nmc
, (vap
->va_flags
& UF_HIDDEN
) ? 1 : 0);
2564 attrbytes
+= NFSX_UNSIGNED
;
2566 // NFS_BITMAP_ISSET(bitmap, NFS_FATTR_MIMETYPE)
2567 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_MODE
)) {
2568 nfsm_chain_add_32(error
, nmc
, vap
->va_mode
);
2569 attrbytes
+= NFSX_UNSIGNED
;
2571 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER
)) {
2573 /* if we have va_uuuid use it, otherwise use uid */
2574 if (!VATTR_IS_ACTIVE(vap
, va_uuuid
)) {
2575 error
= kauth_cred_uid2guid(vap
->va_uid
, &vap
->va_uuuid
);
2579 error
= nfs4_guid2id(&vap
->va_uuuid
, s
, &len
, 0);
2580 if (error
== ENOSPC
) {
2586 MALLOC(s
, char*, len
, M_TEMP
, M_WAITOK
);
2589 error
= nfs4_guid2id(&vap
->va_uuuid
, s
, &len
, 0);
2594 nfsm_chain_add_name(error
, nmc
, s
, len
, nmp
);
2595 attrbytes
+= NFSX_UNSIGNED
+ nfsm_rndup(len
);
2597 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_OWNER_GROUP
)) {
2599 /* if we have va_guuid use it, otherwise use gid */
2600 if (!VATTR_IS_ACTIVE(vap
, va_guuid
)) {
2601 error
= kauth_cred_gid2guid(vap
->va_gid
, &vap
->va_guuid
);
2605 error
= nfs4_guid2id(&vap
->va_guuid
, s
, &len
, 1);
2606 if (error
== ENOSPC
) {
2612 MALLOC(s
, char*, len
, M_TEMP
, M_WAITOK
);
2615 error
= nfs4_guid2id(&vap
->va_guuid
, s
, &len
, 1);
2620 nfsm_chain_add_name(error
, nmc
, s
, len
, nmp
);
2621 attrbytes
+= NFSX_UNSIGNED
+ nfsm_rndup(len
);
2623 // NFS_BITMAP_SET(bitmap, NFS_FATTR_SYSTEM)
2624 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_ACCESS_SET
)) {
2625 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2626 nfsm_chain_add_32(error
, nmc
, NFS4_TIME_SET_TO_SERVER
);
2627 attrbytes
+= NFSX_UNSIGNED
;
2629 nfsm_chain_add_32(error
, nmc
, NFS4_TIME_SET_TO_CLIENT
);
2630 nfsm_chain_add_64(error
, nmc
, vap
->va_access_time
.tv_sec
);
2631 nfsm_chain_add_32(error
, nmc
, vap
->va_access_time
.tv_nsec
);
2632 attrbytes
+= 4 * NFSX_UNSIGNED
;
2635 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_BACKUP
)) {
2636 nfsm_chain_add_64(error
, nmc
, vap
->va_backup_time
.tv_sec
);
2637 nfsm_chain_add_32(error
, nmc
, vap
->va_backup_time
.tv_nsec
);
2638 attrbytes
+= 3 * NFSX_UNSIGNED
;
2640 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_CREATE
)) {
2641 nfsm_chain_add_64(error
, nmc
, vap
->va_create_time
.tv_sec
);
2642 nfsm_chain_add_32(error
, nmc
, vap
->va_create_time
.tv_nsec
);
2643 attrbytes
+= 3 * NFSX_UNSIGNED
;
2645 if (NFS_BITMAP_ISSET(bitmap
, NFS_FATTR_TIME_MODIFY_SET
)) {
2646 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2647 nfsm_chain_add_32(error
, nmc
, NFS4_TIME_SET_TO_SERVER
);
2648 attrbytes
+= NFSX_UNSIGNED
;
2650 nfsm_chain_add_32(error
, nmc
, NFS4_TIME_SET_TO_CLIENT
);
2651 nfsm_chain_add_64(error
, nmc
, vap
->va_modify_time
.tv_sec
);
2652 nfsm_chain_add_32(error
, nmc
, vap
->va_modify_time
.tv_nsec
);
2653 attrbytes
+= 4 * NFSX_UNSIGNED
;
2657 /* Now, set the attribute data length */
2658 *pattrbytes
= txdr_unsigned(attrbytes
);
2660 if (s
&& (s
!= sbuf
)) {
2665 #endif /* CONFIG_NFS4 */
2668 * Got the given error and need to start recovery (if not already started).
2669 * Note: nmp must be locked!
2672 nfs_need_recover(struct nfsmount
*nmp
, int error
)
2674 int wake
= !(nmp
->nm_state
& NFSSTA_RECOVER
);
2676 nmp
->nm_state
|= NFSSTA_RECOVER
;
2677 if ((error
== NFSERR_ADMIN_REVOKED
) ||
2678 (error
== NFSERR_EXPIRED
) ||
2679 (error
== NFSERR_STALE_CLIENTID
)) {
2680 nmp
->nm_state
|= NFSSTA_RECOVER_EXPIRED
;
2683 nfs_mount_sock_thread_wake(nmp
);
2689 * After recovery due to state expiry, check each node and
2690 * drop any lingering delegation we thought we had.
2692 * If a node has an open that is not lost and is not marked
2693 * for reopen, then we hold onto any delegation because it is
2694 * likely newly-granted.
2697 nfs4_expired_check_delegation(nfsnode_t np
, vfs_context_t ctx
)
2699 struct nfsmount
*nmp
= NFSTONMP(np
);
2700 struct nfs_open_file
*nofp
;
2703 if ((np
->n_flag
& NREVOKE
) || !(np
->n_openflags
& N_DELEG_MASK
)) {
2707 lck_mtx_lock(&np
->n_openlock
);
2709 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
2710 if (!nofp
->nof_opencnt
) {
2713 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
2716 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
2719 /* we have an open that is not lost and not marked for reopen */
2720 // XXX print out what's keeping this node from dropping the delegation.
2721 NP(nofp
->nof_np
, "nfs4_expired_check_delegation: !drop: opencnt %d flags 0x%x access %d %d mmap %d %d",
2722 nofp
->nof_opencnt
, nofp
->nof_flags
,
2723 nofp
->nof_access
, nofp
->nof_deny
,
2724 nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
);
2730 /* need to drop a delegation */
2731 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
2732 /* remove this node from the delegation return list */
2733 lck_mtx_lock(&nmp
->nm_lock
);
2734 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
2735 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
2736 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
2738 lck_mtx_unlock(&nmp
->nm_lock
);
2740 if (np
->n_openflags
& N_DELEG_MASK
) {
2741 np
->n_openflags
&= ~N_DELEG_MASK
;
2742 lck_mtx_lock(&nmp
->nm_lock
);
2743 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
2744 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
2745 np
->n_dlink
.tqe_next
= NFSNOLIST
;
2747 lck_mtx_unlock(&nmp
->nm_lock
);
2748 nfs4_delegreturn_rpc(nmp
, np
->n_fhp
, np
->n_fhsize
, &np
->n_dstateid
,
2749 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
2753 lck_mtx_unlock(&np
->n_openlock
);
2755 #endif /* CONFIG_NFS4*/
2758 * Recover state for an NFS mount.
2760 * Iterates over all open files, reclaiming opens and lock state.
2763 nfs_recover(struct nfsmount
*nmp
)
2765 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
2766 int error
, lost
, reopen
;
2767 struct nfs_open_owner
*noop
;
2768 struct nfs_open_file
*nofp
;
2769 struct nfs_file_lock
*nflp
, *nextnflp
;
2770 struct nfs_lock_owner
*nlop
;
2771 thread_t thd
= current_thread();
2773 nfsnode_t np
, nextnp
;
2779 lck_mtx_lock(&nmp
->nm_lock
);
2781 * First, wait for the state inuse count to go to zero so
2782 * we know there are no state operations in progress.
2785 if ((error
= nfs_sigintr(nmp
, NULL
, NULL
, 1))) {
2788 if (!(nmp
->nm_sockflags
& NMSOCK_READY
)) {
2791 if (nmp
->nm_state
& (NFSSTA_FORCE
| NFSSTA_DEAD
)) {
2794 if (nmp
->nm_sockflags
& NMSOCK_UNMOUNT
) {
2800 if (nmp
->nm_stateinuse
) {
2801 msleep(&nmp
->nm_stateinuse
, &nmp
->nm_lock
, (PZERO
- 1), "nfsrecoverstartwait", &ts
);
2803 } while (nmp
->nm_stateinuse
);
2805 if (error
== EPIPE
) {
2806 printf("nfs recovery reconnecting for %s, 0x%x\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
2808 printf("nfs recovery aborted for %s, 0x%x\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
2810 lck_mtx_unlock(&nmp
->nm_lock
);
2815 if (now
.tv_sec
== nmp
->nm_recover_start
) {
2816 printf("nfs recovery throttled for %s, 0x%x\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
2817 lck_mtx_unlock(&nmp
->nm_lock
);
2818 tsleep(nfs_recover
, (PZERO
- 1), "nfsrecoverrestart", hz
);
2821 nmp
->nm_recover_start
= now
.tv_sec
;
2822 if (++nmp
->nm_stategenid
== 0) {
2823 ++nmp
->nm_stategenid
;
2825 printf("nfs recovery started for %s, 0x%x\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
2826 lck_mtx_unlock(&nmp
->nm_lock
);
2828 /* for each open owner... */
2829 TAILQ_FOREACH(noop
, &nmp
->nm_open_owners
, noo_link
) {
2830 /* for each of its opens... */
2831 TAILQ_FOREACH(nofp
, &noop
->noo_opens
, nof_oolink
) {
2832 if (!nofp
->nof_access
|| (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) || (nofp
->nof_np
->n_flag
& NREVOKE
)) {
2836 /* for NFSv2/v3, just skip straight to lock reclaim */
2837 if (nmp
->nm_vers
< NFS_VER4
) {
2841 if (nofp
->nof_rw_drw
) {
2842 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_BOTH
);
2844 if (!error
&& nofp
->nof_w_drw
) {
2845 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_BOTH
);
2847 if (!error
&& nofp
->nof_r_drw
) {
2848 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_BOTH
);
2850 if (!error
&& nofp
->nof_rw_dw
) {
2851 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_WRITE
);
2853 if (!error
&& nofp
->nof_w_dw
) {
2854 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_WRITE
);
2856 if (!error
&& nofp
->nof_r_dw
) {
2857 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_WRITE
);
2860 * deny-none opens with no locks can just be reopened (later) if reclaim fails.
2862 if (!error
&& nofp
->nof_rw
) {
2863 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
);
2864 if ((error
== NFSERR_ADMIN_REVOKED
) || (error
== NFSERR_EXPIRED
) || (error
== NFSERR_NO_GRACE
)) {
2869 if (!error
&& !reopen
&& nofp
->nof_w
) {
2870 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
);
2871 if ((error
== NFSERR_ADMIN_REVOKED
) || (error
== NFSERR_EXPIRED
) || (error
== NFSERR_NO_GRACE
)) {
2876 if (!error
&& !reopen
&& nofp
->nof_r
) {
2877 error
= nfs4_open_reclaim_rpc(nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
);
2878 if ((error
== NFSERR_ADMIN_REVOKED
) || (error
== NFSERR_EXPIRED
) || (error
== NFSERR_NO_GRACE
)) {
2885 * If we hold delegated state but we don't have any non-delegated opens,
2886 * then we should attempt to claim that state now (but don't return the
2887 * delegation unless asked to).
2889 if ((nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
2890 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
2891 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
) &&
2892 (!nofp
->nof_rw_drw
&& !nofp
->nof_w_drw
&& !nofp
->nof_r_drw
&&
2893 !nofp
->nof_rw_dw
&& !nofp
->nof_w_dw
&& !nofp
->nof_r_dw
&&
2894 !nofp
->nof_rw
&& !nofp
->nof_w
&& !nofp
->nof_r
)) {
2895 if (!error
&& !nfs_open_state_set_busy(nofp
->nof_np
, NULL
)) {
2896 error
= nfs4_claim_delegated_state_for_node(nofp
->nof_np
, R_RECOVER
);
2897 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
2900 nfs_open_state_clear_busy(nofp
->nof_np
);
2901 /* if claim didn't go well, we may need to return delegation now */
2902 if (nofp
->nof_np
->n_openflags
& N_DELEG_RETURN
) {
2903 nfs4_delegation_return(nofp
->nof_np
, R_RECOVER
, thd
, noop
->noo_cred
);
2904 if (!(nmp
->nm_sockflags
& NMSOCK_READY
)) {
2905 error
= ETIMEDOUT
; /* looks like we need a reconnect */
2912 * Handle any issue claiming open state.
2913 * Potential reopens need to first confirm that there are no locks.
2915 if (error
|| reopen
) {
2916 /* restart recovery? */
2917 if ((error
== ETIMEDOUT
) || nfs_mount_state_error_should_restart(error
)) {
2918 if (error
== ETIMEDOUT
) {
2919 nfs_need_reconnect(nmp
);
2921 tsleep(nfs_recover
, (PZERO
- 1), "nfsrecoverrestart", hz
);
2922 printf("nfs recovery restarting for %s, 0x%x, error %d\n",
2923 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
, error
);
2926 if (reopen
&& (nfs_check_for_locks(noop
, nofp
) == 0)) {
2927 /* just reopen the file on next access */
2928 NP(nofp
->nof_np
, "nfs_recover: %d, need reopen for %d %p 0x%x", reopen
,
2929 kauth_cred_getuid(noop
->noo_cred
), nofp
->nof_np
, nofp
->nof_np
->n_flag
);
2930 lck_mtx_lock(&nofp
->nof_lock
);
2931 nofp
->nof_flags
|= NFS_OPEN_FILE_REOPEN
;
2932 lck_mtx_unlock(&nofp
->nof_lock
);
2934 /* open file state lost */
2936 NP(nofp
->nof_np
, "nfs_recover: %d, can't reopen because of locks %d %p", reopen
,
2937 kauth_cred_getuid(noop
->noo_cred
), nofp
->nof_np
);
2944 /* no error, so make sure the reopen flag isn't set */
2945 lck_mtx_lock(&nofp
->nof_lock
);
2946 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
2947 lck_mtx_unlock(&nofp
->nof_lock
);
2949 #endif /* CONFIG_NFS4 */
2951 * Scan this node's lock owner list for entries with this open owner,
2952 * then walk the lock owner's held lock list recovering each lock.
2955 TAILQ_FOREACH(nlop
, &nofp
->nof_np
->n_lock_owners
, nlo_link
) {
2956 if (lost
|| reopen
) {
2959 if (nlop
->nlo_open_owner
!= noop
) {
2962 TAILQ_FOREACH_SAFE(nflp
, &nlop
->nlo_locks
, nfl_lolink
, nextnflp
) {
2963 /* skip dead & blocked lock requests (shouldn't be any in the held lock list) */
2964 if (nflp
->nfl_flags
& (NFS_FILE_LOCK_DEAD
| NFS_FILE_LOCK_BLOCKED
)) {
2967 /* skip delegated locks */
2968 if (nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
) {
2971 error
= nmp
->nm_funcs
->nf_setlock_rpc(nofp
->nof_np
, nofp
, nflp
, 1, R_RECOVER
, thd
, noop
->noo_cred
);
2973 NP(nofp
->nof_np
, "nfs: lock reclaim (0x%llx, 0x%llx) %s %d",
2974 nflp
->nfl_start
, nflp
->nfl_end
,
2975 error
? "failed" : "succeeded", error
);
2980 /* restart recovery? */
2981 if ((error
== ETIMEDOUT
) || nfs_mount_state_error_should_restart(error
)) {
2982 if (error
== ETIMEDOUT
) {
2983 nfs_need_reconnect(nmp
);
2985 tsleep(nfs_recover
, (PZERO
- 1), "nfsrecoverrestart", hz
);
2986 printf("nfs recovery restarting for %s, 0x%x, error %d\n",
2987 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
, error
);
2990 /* lock state lost - attempt to close file */
2998 * If we've determined that we need to reopen the file then we probably
2999 * didn't receive any delegation we think we hold. We should attempt to
3000 * return that delegation (and claim any delegated state).
3002 * If we hold a delegation that is marked for return, then we should
3005 if ((nofp
->nof_np
->n_openflags
& N_DELEG_RETURN
) ||
3006 (reopen
&& (nofp
->nof_np
->n_openflags
& N_DELEG_MASK
))) {
3007 nfs4_delegation_return(nofp
->nof_np
, R_RECOVER
, thd
, noop
->noo_cred
);
3008 if (!(nmp
->nm_sockflags
& NMSOCK_READY
)) {
3009 /* looks like we need a reconnect */
3010 tsleep(nfs_recover
, (PZERO
- 1), "nfsrecoverrestart", hz
);
3011 printf("nfs recovery restarting for %s, 0x%x, error %d\n",
3012 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
, error
);
3018 /* revoke open file state */
3019 NP(nofp
->nof_np
, "nfs_recover: state lost for %d %p 0x%x",
3020 kauth_cred_getuid(noop
->noo_cred
), nofp
->nof_np
, nofp
->nof_np
->n_flag
);
3021 nfs_revoke_open_state_for_node(nofp
->nof_np
);
3027 /* If state expired, make sure we're not holding onto any stale delegations */
3028 lck_mtx_lock(&nmp
->nm_lock
);
3030 if ((nmp
->nm_vers
>= NFS_VER4
) && (nmp
->nm_state
& NFSSTA_RECOVER_EXPIRED
)) {
3032 TAILQ_FOREACH_SAFE(np
, &nmp
->nm_delegations
, n_dlink
, nextnp
) {
3033 lck_mtx_unlock(&nmp
->nm_lock
);
3034 nfs4_expired_check_delegation(np
, vfs_context_kernel());
3035 lck_mtx_lock(&nmp
->nm_lock
);
3036 if (nextnp
== NFSNOLIST
) {
3042 nmp
->nm_state
&= ~(NFSSTA_RECOVER
| NFSSTA_RECOVER_EXPIRED
);
3043 wakeup(&nmp
->nm_state
);
3044 printf("nfs recovery completed for %s, 0x%x\n",
3045 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
);
3046 lck_mtx_unlock(&nmp
->nm_lock
);
3048 printf("nfs recovery failed for %s, 0x%x, error %d\n",
3049 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nmp
->nm_stategenid
, error
);
3053 #endif /* CONFIG_NFS_CLIENT */