4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
37 #include <sys/vnode.h>
39 #include <sys/errno.h>
40 #include <sys/sysmacros.h>
41 #include <sys/statvfs.h>
43 #include <sys/dirent.h>
44 #include <rpc/types.h>
46 #include <rpc/rpcsec_gss.h>
48 #include <sys/strsubr.h>
49 #include <sys/strsun.h>
53 #include <nfs/export.h>
55 #include <nfs/nfs_cmd.h>
59 * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
60 * This is used to return NFS4ERR_TOOSMALL when clients specify
61 * maxcount that isn't large enough to hold the smallest possible
64 * sizeof cookie (8 bytes) +
65 * sizeof name_len (4 bytes) +
66 * sizeof smallest (padded) name (4 bytes) +
67 * sizeof bitmap4_len (12 bytes) + NOTE: we always encode len=2 bm4
68 * sizeof attrlist4_len (4 bytes) +
69 * sizeof next boolean (4 bytes)
71 * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
72 * the smallest possible entry4 (assumes no attrs requested).
73 * sizeof nfsstat4 (4 bytes) +
74 * sizeof verifier4 (8 bytes) +
75 * sizeof entsecond_to_ry4list bool (4 bytes) +
76 * sizeof entry4 (36 bytes) +
77 * sizeof eof bool (4 bytes)
79 * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
80 * VOP_READDIR. Its value is the size of the maximum possible dirent
81 * for solaris. The DIRENT64_RECLEN macro returns the size of dirent
82 * required for a given name length. MAXNAMELEN is the maximum
83 * filename length allowed in Solaris. The first two DIRENT64_RECLEN()
84 * macros are to allow for . and .. entries -- just a minor tweak to try
85 * and guarantee that buffer we give to VOP_READDIR will be large enough
86 * to hold ., .., and the largest possible solaris dirent64.
88 #define RFS4_MINLEN_ENTRY4 36
89 #define RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
90 #define RFS4_MINLEN_RDDIR_BUF \
91 (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
97 #define nextdp(dp) ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
99 verifier4 Readdir4verf
= 0x0;
101 static nfs_ftype4 vt_to_nf4
[] = {
102 0, NF4REG
, NF4DIR
, NF4BLK
, NF4CHR
, NF4LNK
, NF4FIFO
, 0, 0, NF4SOCK
, 0
106 nfs4_readdir_getvp(vnode_t
*dvp
, char *d_name
, vnode_t
**vpp
,
107 struct exportinfo
**exi
, struct svc_req
*req
,
108 struct compound_state
*cs
, int expseudo
)
113 vnode_t
*vp
, *pre_tvp
;
115 struct exportinfo
*newexi
, *saveexi
;
120 if (error
= VOP_LOOKUP(dvp
, d_name
, &vp
, NULL
, 0, NULL
, cs
->cr
,
125 * If the directory is a referral point, don't return the
126 * attrs, instead set rdattr_error to MOVED.
128 if (vn_is_nfs_reparse(vp
, cs
->cr
) && !client_is_downrev(req
)) {
130 DTRACE_PROBE2(nfs4serv__func__referral__moved
,
131 vnode_t
*, vp
, char *, "nfs4_readdir_getvp");
132 return (NFS4ERR_MOVED
);
135 /* Is this object mounted upon? */
136 ismntpt
= vn_ismntpt(vp
);
139 * Nothing more to do if object is not a mount point or
140 * a possible LOFS shadow of an LOFS mount (which won't
141 * have v_vfsmountedhere set)
143 if (ismntpt
== 0 && dvp
->v_vfsp
== vp
->v_vfsp
&& expseudo
== 0) {
150 * Something is mounted here. Traverse and manage the
156 if ((error
= traverse(&vp
)) != 0) {
161 if (vn_is_nfs_reparse(vp
, cs
->cr
)) {
164 DTRACE_PROBE2(nfs4serv__func__referral__moved
,
165 vnode_t
*, vp
, char *, "nfs4_readdir_getvp");
166 return (NFS4ERR_MOVED
);
170 bzero(&fid
, sizeof (fid
));
171 fid
.fid_len
= MAXFIDSZ
;
174 * If VOP_FID not supported by underlying fs (mntfs, procfs,
175 * etc.), then return attrs for stub instead of VROOT object.
176 * If it fails for any other reason, then return the error.
178 if (error
= VOP_FID(vp
, &fid
, NULL
)) {
184 if (error
!= ENOSYS
&& error
!= ENOTSUP
) {
189 /* go back to vnode that is "under" mount */
195 newexi
= checkexport4(&vp
->v_vfsp
->vfs_fsid
, &fid
, vp
);
196 if (newexi
== NULL
) {
209 /* Save the exi and present the new one to checkauth4() */
213 /* Get the right cred like lookup does */
215 cs
->cr
= crdup(cs
->basecr
);
217 status
= call_checkauth4(cs
, req
);
223 /* Reset what call_checkauth4() may have set */
224 *cs
->statusp
= NFS4_OK
;
226 if (status
!= NFS4_OK
) {
228 if (status
== NFS4ERR_DELAY
)
229 status
= NFS4ERR_ACCESS
;
238 /* This is the set of pathconf data for vfs */
240 uint64_t maxfilesize
;
247 rfs4_get_pc_encode(vnode_t
*vp
, rfs4_pc_encode_t
*pce
, bitmap4 ar
, cred_t
*cr
)
252 pce
->maxfilesize
= 0;
256 if (ar
& FATTR4_MAXFILESIZE_MASK
) {
257 /* Maximum File Size */
258 error
= VOP_PATHCONF(vp
, _PC_FILESIZEBITS
, &pc_val
, cr
, NULL
);
263 * If the underlying file system does not support
264 * _PC_FILESIZEBITS, return a reasonable default. Note that
265 * error code on VOP_PATHCONF will be 0, even if the underlying
266 * file system does not support _PC_FILESIZEBITS.
268 if (pc_val
== (ulong_t
)-1) {
269 pce
->maxfilesize
= MAXOFF32_T
;
271 if (pc_val
>= (sizeof (uint64_t) * 8))
272 pce
->maxfilesize
= INT64_MAX
;
274 pce
->maxfilesize
= ((1LL << (pc_val
- 1)) - 1);
278 if (ar
& FATTR4_MAXLINK_MASK
) {
279 /* Maximum Link Count */
280 error
= VOP_PATHCONF(vp
, _PC_LINK_MAX
, &pc_val
, cr
, NULL
);
284 pce
->maxlink
= pc_val
;
287 if (ar
& FATTR4_MAXNAME_MASK
) {
288 /* Maximum Name Length */
289 error
= VOP_PATHCONF(vp
, _PC_NAME_MAX
, &pc_val
, cr
, NULL
);
293 pce
->maxname
= pc_val
;
299 /* This is the set of statvfs data that is ready for encoding */
301 uint64_t space_avail
;
303 uint64_t space_total
;
310 rfs4_get_sb_encode(vfs_t
*vfsp
, rfs4_sb_encode_t
*psbe
)
315 /* Grab the per filesystem info */
316 if (error
= VFS_STATVFS(vfsp
, &sb
)) {
320 /* Calculate space available */
321 if (sb
.f_bavail
!= (fsblkcnt64_t
)-1) {
323 (fattr4_space_avail
) sb
.f_frsize
*
324 (fattr4_space_avail
) sb
.f_bavail
;
327 (fattr4_space_avail
) sb
.f_bavail
;
330 /* Calculate space free */
331 if (sb
.f_bfree
!= (fsblkcnt64_t
)-1) {
333 (fattr4_space_free
) sb
.f_frsize
*
334 (fattr4_space_free
) sb
.f_bfree
;
337 (fattr4_space_free
) sb
.f_bfree
;
340 /* Calculate space total */
341 if (sb
.f_blocks
!= (fsblkcnt64_t
)-1) {
343 (fattr4_space_total
) sb
.f_frsize
*
344 (fattr4_space_total
) sb
.f_blocks
;
347 (fattr4_space_total
) sb
.f_blocks
;
350 /* For use later on attr encode */
351 psbe
->fa
= sb
.f_favail
;
352 psbe
->ff
= sb
.f_ffree
;
353 psbe
->ft
= sb
.f_files
;
359 * Macros to handle if we have don't have enough space for the requested
360 * attributes and this is the first entry and the
361 * requested attributes are more than the minimal useful
362 * set, reset the attributes to the minimal set and
363 * retry the encoding. If the client has asked for both
364 * mounted_on_fileid and fileid, prefer mounted_on_fileid.
366 #define MINIMAL_RD_ATTRS \
367 (FATTR4_MOUNTED_ON_FILEID_MASK| \
368 FATTR4_FILEID_MASK| \
369 FATTR4_RDATTR_ERROR_MASK)
371 #define MINIMIZE_ATTR_MASK(m) { \
372 if ((m) & FATTR4_MOUNTED_ON_FILEID_MASK) \
373 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_MOUNTED_ON_FILEID_MASK;\
375 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_FILEID_MASK; \
378 #define IS_MIN_ATTR_MASK(m) (((m) & ~MINIMAL_RD_ATTRS) == 0)
380 * If readdir only needs to return FILEID, we can take it from the
381 * dirent struct and save doing the lookup.
385 rfs4_op_readdir(nfs_argop4
*argop
, nfs_resop4
*resop
,
386 struct svc_req
*req
, struct compound_state
*cs
)
388 READDIR4args
*args
= &argop
->nfs_argop4_u
.opreaddir
;
389 READDIR4res
*resp
= &resop
->nfs_resop4_u
.opreaddir
;
390 struct exportinfo
*newexi
= NULL
;
395 vnode_t
*dvp
= cs
->vp
;
399 rfs4_sb_encode_t dsbe
, sbe
;
401 int rddir_data_len
, rddir_result_size
;
403 offset_t rddir_next_offset
;
414 uint32_t *ptr
, *ptr_redzone
;
415 uint32_t *beginning_ptr
;
416 uint32_t *lastentry_ptr
;
417 uint32_t *attrmask_ptr
;
418 uint32_t *attr_offset_ptr
;
419 uint32_t attr_length
;
422 uint32_t rddirattr_error
= 0;
424 bitmap4 ar
= args
->attr_request
& NFS4_SRV_RDDIR_SUPPORTED_ATTRS
;
426 rfs4_pc_encode_t dpce
, pce
;
431 uint_t
false = FALSE
;
435 utf8string owner
, group
;
436 int owner_error
, group_error
;
440 DTRACE_NFSV4_2(op__readdir__start
, struct compound_state
*, cs
,
441 READDIR4args
*, args
);
444 owner
.utf8string_len
= group
.utf8string_len
= 0;
445 owner
.utf8string_val
= group
.utf8string_val
= NULL
;
449 /* Maximum read and write size */
450 maxread
= maxwrite
= rfs4_tsize(req
);
453 *cs
->statusp
= resp
->status
= NFS4ERR_NOFILEHANDLE
;
458 * If there is an unshared filesystem mounted on this vnode,
459 * do not allow readdir in this directory.
461 if (vn_ismntpt(dvp
)) {
462 *cs
->statusp
= resp
->status
= NFS4ERR_ACCESS
;
466 if (dvp
->v_type
!= VDIR
) {
467 *cs
->statusp
= resp
->status
= NFS4ERR_NOTDIR
;
471 if (args
->maxcount
<= RFS4_MINLEN_RDDIR4
) {
472 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
477 * If write-only attrs are requested, then fail the readdir op
479 if (args
->attr_request
&
480 (FATTR4_TIME_MODIFY_SET_MASK
| FATTR4_TIME_ACCESS_SET_MASK
)) {
481 *cs
->statusp
= resp
->status
= NFS4ERR_INVAL
;
485 error
= VOP_ACCESS(dvp
, VREAD
, 0, cs
->cr
, NULL
);
487 *cs
->statusp
= resp
->status
= puterrno4(error
);
491 if (args
->cookieverf
!= Readdir4verf
) {
492 *cs
->statusp
= resp
->status
= NFS4ERR_NOT_SAME
;
496 /* Is there pseudo-fs work that is needed for this readdir? */
497 check_visible
= PSEUDO(cs
->exi
) ||
498 ! is_exported_sec(cs
->nfsflavor
, cs
->exi
) ||
499 cs
->access
& CS_ACCESS_LIMITED
;
501 /* Check the requested attributes and only do the work if needed */
503 if (ar
& (FATTR4_MAXFILESIZE_MASK
|
504 FATTR4_MAXLINK_MASK
|
505 FATTR4_MAXNAME_MASK
)) {
506 if (error
= rfs4_get_pc_encode(cs
->vp
, &dpce
, ar
, cs
->cr
)) {
507 *cs
->statusp
= resp
->status
= puterrno4(error
);
513 /* If there is statvfs data requested, pick it up once */
515 (FATTR4_FILES_AVAIL_MASK
|
516 FATTR4_FILES_FREE_MASK
|
517 FATTR4_FILES_TOTAL_MASK
|
518 FATTR4_FILES_AVAIL_MASK
|
519 FATTR4_FILES_FREE_MASK
|
520 FATTR4_FILES_TOTAL_MASK
)) {
521 if (error
= rfs4_get_sb_encode(dvp
->v_vfsp
, &dsbe
)) {
522 *cs
->statusp
= resp
->status
= puterrno4(error
);
529 * Max transfer size of the server is the absolute limite.
530 * If the client has decided to max out with something really
531 * tiny, then return toosmall. Otherwise, move forward and
532 * see if a single entry can be encoded.
534 tsize
= rfs4_tsize(req
);
535 if (args
->maxcount
> tsize
)
536 args
->maxcount
= tsize
;
537 else if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
) {
538 if (args
->maxcount
< RFS4_MINLEN_ENTRY4
) {
539 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
545 * How large should the mblk be for outgoing encoding.
547 if (args
->maxcount
< MAXBSIZE
)
550 mpcount
= args
->maxcount
;
553 * mp will contain the data to be sent out in the readdir reply.
554 * It will be freed after the reply has been sent.
555 * Let's roundup the data to a BYTES_PER_XDR_UNIX multiple,
556 * so that the call to xdrmblk_putmblk() never fails.
558 mp
= allocb(RNDUP(mpcount
), BPRI_MED
);
562 * The allocation of the client's requested size has
563 * failed. It may be that the size is too large for
564 * current system utilization; step down to a "common"
565 * size and wait for the allocation to occur.
567 if (mpcount
> MAXBSIZE
)
568 args
->maxcount
= mpcount
= MAXBSIZE
;
569 mp
= allocb_wait(RNDUP(mpcount
), BPRI_MED
,
570 STR_NOSIG
, &alloc_err
);
574 ASSERT(alloc_err
== 0);
578 ptr
= beginning_ptr
= (uint32_t *)mp
->b_datap
->db_base
;
581 * The "redzone" at the end of the encoding buffer is used
582 * to deal with xdr encoding length. Instead of checking
583 * each encoding of an attribute value before it is done,
584 * make the assumption that it will fit into the buffer and
585 * check occasionally.
587 * The largest block of attributes that are encoded without
588 * checking the redzone is 18 * BYTES_PER_XDR_UNIT (72 bytes)
589 * "round" to 128 as the redzone size.
591 if (args
->maxcount
< (mpcount
- 128))
593 (uint32_t *)(((char *)ptr
) + RNDUP(args
->maxcount
));
596 (uint32_t *)((((char *)ptr
) + RNDUP(mpcount
)) - 128);
599 * Set the dircount; this will be used as the size for the
600 * readdir of the underlying filesystem. First make sure
601 * that it is large enough to do a reasonable readdir (client
602 * may have short changed us - it is an advisory number);
603 * then make sure that it isn't too large.
604 * After all of that, if maxcount is "small" then just use
605 * that for the dircount number.
607 dircount
= (args
->dircount
< MAXBSIZE
) ? MAXBSIZE
: args
->dircount
;
608 dircount
= (dircount
> tsize
) ? tsize
: dircount
;
609 if (dircount
> args
->maxcount
)
610 dircount
= args
->maxcount
;
611 if (args
->maxcount
<= MAXBSIZE
) {
612 if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
)
613 dircount
= RFS4_MINLEN_RDDIR_BUF
;
615 dircount
= args
->maxcount
;
618 /* number of entries fully encoded in outgoing buffer */
621 /* ENCODE READDIR4res.cookieverf */
622 IXDR_PUT_HYPER(ptr
, Readdir4verf
);
624 rddir_data_len
= dircount
;
625 rddir_data
= kmem_alloc(rddir_data_len
, KM_NOSLEEP
);
626 if (rddir_data
== NULL
) {
627 /* The allocation failed; downsize and wait for it this time */
628 if (rddir_data_len
> MAXBSIZE
)
629 rddir_data_len
= dircount
= MAXBSIZE
;
630 rddir_data
= kmem_alloc(rddir_data_len
, KM_SLEEP
);
633 rddir_next_offset
= (offset_t
)args
->cookie
;
635 ca
= (struct sockaddr
*)svc_getrpccaller(req
->rq_xprt
)->buf
;
644 /* Move on to reading the directory contents */
645 iov
.iov_base
= rddir_data
;
646 iov
.iov_len
= rddir_data_len
;
649 uio
.uio_segflg
= UIO_SYSSPACE
;
650 uio
.uio_extflg
= UIO_COPY_CACHED
;
651 uio
.uio_loffset
= rddir_next_offset
;
652 uio
.uio_resid
= rddir_data_len
;
654 (void) VOP_RWLOCK(dvp
, V_WRITELOCK_FALSE
, NULL
);
656 error
= VOP_READDIR(dvp
, &uio
, cs
->cr
, &iseofdir
, NULL
, 0);
658 VOP_RWUNLOCK(dvp
, V_WRITELOCK_FALSE
, NULL
);
661 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
665 *cs
->statusp
= resp
->status
= puterrno4(error
);
670 rddir_result_size
= rddir_data_len
- uio
.uio_resid
;
672 /* No data were read. Check if we reached the end of the directory. */
673 if (rddir_result_size
== 0) {
674 /* encode the BOOLEAN marking no further entries */
675 IXDR_PUT_U_INT32(ptr
, false);
676 /* encode the BOOLEAN signifying end of directory */
677 IXDR_PUT_U_INT32(ptr
, iseofdir
? true : false);
678 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
679 resp
->mblk
->b_wptr
+= resp
->data_len
;
680 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
681 *cs
->statusp
= resp
->status
= NFS4_OK
;
687 for (dp
= (struct dirent64
*)rddir_data
;
688 !no_space
&& rddir_result_size
> 0; dp
= nextdp(dp
)) {
701 rddir_result_size
-= dp
->d_reclen
;
703 /* skip "." and ".." entries */
704 if (dp
->d_ino
== 0 || NFS_IS_DOTNAME(dp
->d_name
)) {
705 rddir_next_offset
= dp
->d_off
;
710 !nfs_visible_inode(cs
->exi
, dp
->d_ino
, &expseudo
)) {
711 rddir_next_offset
= dp
->d_off
;
716 * Only if the client requested attributes...
717 * If the VOP_LOOKUP fails ENOENT, then skip this entry
718 * for the readdir response. If there was another error,
719 * then set the rddirattr_error and the error will be
720 * encoded later in the "attributes" section.
726 error
= nfs4_readdir_getvp(dvp
, dp
->d_name
,
727 &vp
, &newexi
, req
, cs
, expseudo
);
728 if (error
== ENOENT
) {
729 rddir_next_offset
= dp
->d_off
;
733 rddirattr_error
= error
;
736 * The vp obtained from above may be from a
737 * different filesystem mount and the vfs-like
738 * attributes should be obtained from that
739 * different vfs; only do this if appropriate.
742 (vfs_different
= (dvp
->v_vfsp
!= vp
->v_vfsp
))) {
743 if (ar
& (FATTR4_FILES_AVAIL_MASK
|
744 FATTR4_FILES_FREE_MASK
|
745 FATTR4_FILES_TOTAL_MASK
|
746 FATTR4_FILES_AVAIL_MASK
|
747 FATTR4_FILES_FREE_MASK
|
748 FATTR4_FILES_TOTAL_MASK
)) {
750 rfs4_get_sb_encode(dvp
->v_vfsp
,
752 /* Remove attrs from encode */
753 ae
&= ~(FATTR4_FILES_AVAIL_MASK
|
754 FATTR4_FILES_FREE_MASK
|
755 FATTR4_FILES_TOTAL_MASK
|
756 FATTR4_FILES_AVAIL_MASK
|
757 FATTR4_FILES_FREE_MASK
|
758 FATTR4_FILES_TOTAL_MASK
);
759 rddirattr_error
= error
;
762 if (ar
& (FATTR4_MAXFILESIZE_MASK
|
763 FATTR4_MAXLINK_MASK
|
764 FATTR4_MAXNAME_MASK
)) {
765 if (error
= rfs4_get_pc_encode(cs
->vp
,
767 ar
&= ~(FATTR4_MAXFILESIZE_MASK
|
768 FATTR4_MAXLINK_MASK
|
769 FATTR4_MAXNAME_MASK
);
770 rddirattr_error
= error
;
776 /* encode the BOOLEAN for the existence of the next entry */
777 IXDR_PUT_U_INT32(ptr
, true);
778 /* encode the COOKIE for the entry */
779 IXDR_PUT_U_HYPER(ptr
, dp
->d_off
);
781 name
= nfscmd_convname(ca
, cs
->exi
, dp
->d_name
,
782 NFSCMD_CONV_OUTBOUND
, MAXPATHLEN
+ 1);
785 rddir_next_offset
= dp
->d_off
;
788 /* Calculate the dirent name length */
789 namelen
= strlen(name
);
791 rndup
= RNDUP(namelen
) / BYTES_PER_XDR_UNIT
;
793 /* room for LENGTH + string ? */
794 if ((ptr
+ (1 + rndup
)) > ptr_redzone
) {
799 /* encode the LENGTH of the name */
800 IXDR_PUT_U_INT32(ptr
, namelen
);
801 /* encode the RNDUP FILL first */
803 /* encode the NAME of the entry */
804 bcopy(name
, (char *)ptr
, namelen
);
805 /* now bump the ptr after... */
808 if (name
!= dp
->d_name
)
809 kmem_free(name
, MAXPATHLEN
+ 1);
812 * Keep checking on the dircount to see if we have
813 * reached the limit; from the RFC, dircount is to be
814 * the XDR encoded limit of the cookie plus name.
815 * So the count is the name, XDR_UNIT of length for
816 * that name and 2 * XDR_UNIT bytes of cookie;
817 * However, use the regular DIRENT64 to match most
820 dircount
-= DIRENT64_RECLEN(namelen
);
821 if (nents
!= 0 && dircount
< 0) {
827 * Attributes requested?
828 * Gather up the attribute info and the previous VOP_LOOKUP()
829 * succeeded; if an error occurs on the VOP_GETATTR() then
830 * return just the error (again if it is requested).
831 * Note that the previous VOP_LOOKUP() could have failed
832 * itself which leaves this code without anything for
834 * Also note that the readdir_attr_error is left in the
835 * encoding mask if requested and so is the mounted_on_fileid.
839 ae
= ar
& (FATTR4_RDATTR_ERROR_MASK
|
840 FATTR4_MOUNTED_ON_FILEID_MASK
);
844 VOP_GETATTR(vp
, &va
, 0, cs
->cr
, NULL
);
845 if (rddirattr_error
) {
846 ae
= ar
& (FATTR4_RDATTR_ERROR_MASK
|
847 FATTR4_MOUNTED_ON_FILEID_MASK
);
850 * We may lie about the object
851 * type for a referral
853 if (vn_is_nfs_reparse(vp
, cs
->cr
) &&
854 client_is_downrev(req
))
860 /* START OF ATTRIBUTE ENCODING */
862 /* encode the LENGTH of the BITMAP4 array */
863 IXDR_PUT_U_INT32(ptr
, 2);
864 /* encode the BITMAP4 */
866 IXDR_PUT_HYPER(ptr
, ae
);
867 attr_offset_ptr
= ptr
;
868 /* encode the default LENGTH of the attributes for entry */
869 IXDR_PUT_U_INT32(ptr
, 0);
871 if (ptr
> ptr_redzone
) {
876 /* Check if any of the first 32 attributes are being encoded */
877 if (ae
& 0xffffffff00000000) {
879 * Redzone check is done at the end of this section.
880 * This particular section will encode a maximum of
881 * 18 * BYTES_PER_XDR_UNIT of data
884 (FATTR4_SUPPORTED_ATTRS_MASK
|
886 FATTR4_FH_EXPIRE_TYPE_MASK
|
889 FATTR4_LINK_SUPPORT_MASK
|
890 FATTR4_SYMLINK_SUPPORT_MASK
|
891 FATTR4_NAMED_ATTR_MASK
|
893 FATTR4_UNIQUE_HANDLES_MASK
|
894 FATTR4_LEASE_TIME_MASK
|
895 FATTR4_RDATTR_ERROR_MASK
)) {
897 if (ae
& FATTR4_SUPPORTED_ATTRS_MASK
) {
898 IXDR_PUT_INT32(ptr
, 2);
900 rfs4_supported_attrs
);
902 if (ae
& FATTR4_TYPE_MASK
) {
903 uint_t ftype
= vt_to_nf4
[va
.va_type
];
904 if (dvp
->v_flag
& V_XATTRDIR
) {
905 if (va
.va_type
== VDIR
)
908 ftype
= NF4NAMEDATTR
;
910 IXDR_PUT_U_INT32(ptr
, ftype
);
912 if (ae
& FATTR4_FH_EXPIRE_TYPE_MASK
) {
913 uint_t expire_type
= FH4_PERSISTENT
;
914 IXDR_PUT_U_INT32(ptr
, expire_type
);
916 if (ae
& FATTR4_CHANGE_MASK
) {
918 NFS4_SET_FATTR4_CHANGE(change
,
920 IXDR_PUT_HYPER(ptr
, change
);
922 if (ae
& FATTR4_SIZE_MASK
) {
923 u_longlong_t size
= va
.va_size
;
924 IXDR_PUT_HYPER(ptr
, size
);
926 if (ae
& FATTR4_LINK_SUPPORT_MASK
) {
927 IXDR_PUT_U_INT32(ptr
, true);
929 if (ae
& FATTR4_SYMLINK_SUPPORT_MASK
) {
930 IXDR_PUT_U_INT32(ptr
, true);
932 if (ae
& FATTR4_NAMED_ATTR_MASK
) {
937 if (!(vp
->v_vfsp
->vfs_flag
&
941 sattr_error
= VOP_PATHCONF(vp
,
943 &pc_val
, cs
->cr
, NULL
);
944 if (sattr_error
|| pc_val
== 0)
945 (void) VOP_PATHCONF(vp
,
950 isit
= (pc_val
? TRUE
: FALSE
);
951 IXDR_PUT_U_INT32(ptr
, isit
);
953 if (ae
& FATTR4_FSID_MASK
) {
954 u_longlong_t major
, minor
;
955 struct exportinfo
*exi
;
957 exi
= newexi
? newexi
: cs
->exi
;
958 if (exi
->exi_volatile_dev
) {
959 int *pmaj
= (int *)&major
;
961 pmaj
[0] = exi
->exi_fsid
.val
[0];
962 pmaj
[1] = exi
->exi_fsid
.val
[1];
965 major
= getmajor(va
.va_fsid
);
966 minor
= getminor(va
.va_fsid
);
968 IXDR_PUT_HYPER(ptr
, major
);
969 IXDR_PUT_HYPER(ptr
, minor
);
971 if (ae
& FATTR4_UNIQUE_HANDLES_MASK
) {
972 IXDR_PUT_U_INT32(ptr
, false);
974 if (ae
& FATTR4_LEASE_TIME_MASK
) {
975 uint_t lt
= rfs4_lease_time
;
976 IXDR_PUT_U_INT32(ptr
, lt
);
978 if (ae
& FATTR4_RDATTR_ERROR_MASK
) {
980 (rddirattr_error
== 0 ?
981 0 : puterrno4(rddirattr_error
));
982 IXDR_PUT_U_INT32(ptr
, rddirattr_error
);
985 /* Check the redzone boundary */
986 if (ptr
> ptr_redzone
) {
987 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
991 MINIMIZE_ATTR_MASK(ar
);
998 * Redzone check is done at the end of this section.
999 * This particular section will encode a maximum of
1000 * 4 * BYTES_PER_XDR_UNIT of data.
1001 * NOTE: that if ACLs are supported that the
1002 * redzone calculations will need to change.
1006 FATTR4_ACLSUPPORT_MASK
|
1007 FATTR4_ARCHIVE_MASK
|
1008 FATTR4_CANSETTIME_MASK
|
1009 FATTR4_CASE_INSENSITIVE_MASK
|
1010 FATTR4_CASE_PRESERVING_MASK
|
1011 FATTR4_CHOWN_RESTRICTED_MASK
)) {
1013 if (ae
& FATTR4_ACL_MASK
) {
1016 if (ae
& FATTR4_ACLSUPPORT_MASK
) {
1019 if (ae
& FATTR4_ARCHIVE_MASK
) {
1022 if (ae
& FATTR4_CANSETTIME_MASK
) {
1023 IXDR_PUT_U_INT32(ptr
, true);
1025 if (ae
& FATTR4_CASE_INSENSITIVE_MASK
) {
1026 IXDR_PUT_U_INT32(ptr
, false);
1028 if (ae
& FATTR4_CASE_PRESERVING_MASK
) {
1029 IXDR_PUT_U_INT32(ptr
, true);
1031 if (ae
& FATTR4_CHOWN_RESTRICTED_MASK
) {
1034 (void) VOP_PATHCONF(vp
,
1035 _PC_CHOWN_RESTRICTED
,
1036 &pc_val
, cs
->cr
, NULL
);
1037 isit
= (pc_val
? TRUE
: FALSE
);
1038 IXDR_PUT_U_INT32(ptr
, isit
);
1040 /* Check the redzone boundary */
1041 if (ptr
> ptr_redzone
) {
1042 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1046 MINIMIZE_ATTR_MASK(ar
);
1048 ptr
= lastentry_ptr
;
1049 goto reencode_attrs
;
1053 * Redzone check is done before the filehandle
1057 (FATTR4_FILEHANDLE_MASK
|
1058 FATTR4_FILEID_MASK
)) {
1060 if (ae
& FATTR4_FILEHANDLE_MASK
) {
1064 char fh
[NFS_FH4_LEN
];
1068 (void) makefh4((nfs_fh4
*)&fh
, vp
,
1069 (newexi
? newexi
: cs
->exi
));
1071 if (dvp
->v_flag
& V_XATTRDIR
)
1072 set_fh4_flag((nfs_fh4
*)&fh
,
1075 if (!xdr_inline_encode_nfs_fh4(
1077 (nfs_fh4_fmt_t
*)fh
.val
)) {
1079 IS_MIN_ATTR_MASK(ar
)) {
1083 MINIMIZE_ATTR_MASK(ar
);
1085 ptr
= lastentry_ptr
;
1086 goto reencode_attrs
;
1089 if (ae
& FATTR4_FILEID_MASK
) {
1090 IXDR_PUT_HYPER(ptr
, va
.va_nodeid
);
1092 /* Check the redzone boundary */
1093 if (ptr
> ptr_redzone
) {
1094 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1098 MINIMIZE_ATTR_MASK(ar
);
1100 ptr
= lastentry_ptr
;
1101 goto reencode_attrs
;
1105 * Redzone check is done at the end of this section.
1106 * This particular section will encode a maximum of
1107 * 15 * BYTES_PER_XDR_UNIT of data.
1110 (FATTR4_FILES_AVAIL_MASK
|
1111 FATTR4_FILES_FREE_MASK
|
1112 FATTR4_FILES_TOTAL_MASK
|
1113 FATTR4_FS_LOCATIONS_MASK
|
1114 FATTR4_HIDDEN_MASK
|
1115 FATTR4_HOMOGENEOUS_MASK
|
1116 FATTR4_MAXFILESIZE_MASK
|
1117 FATTR4_MAXLINK_MASK
|
1118 FATTR4_MAXNAME_MASK
|
1119 FATTR4_MAXREAD_MASK
|
1120 FATTR4_MAXWRITE_MASK
)) {
1122 if (ae
& FATTR4_FILES_AVAIL_MASK
) {
1123 IXDR_PUT_HYPER(ptr
, sbe
.fa
);
1125 if (ae
& FATTR4_FILES_FREE_MASK
) {
1126 IXDR_PUT_HYPER(ptr
, sbe
.ff
);
1128 if (ae
& FATTR4_FILES_TOTAL_MASK
) {
1129 IXDR_PUT_HYPER(ptr
, sbe
.ft
);
1131 if (ae
& FATTR4_FS_LOCATIONS_MASK
) {
1134 if (ae
& FATTR4_HIDDEN_MASK
) {
1137 if (ae
& FATTR4_HOMOGENEOUS_MASK
) {
1138 IXDR_PUT_U_INT32(ptr
, true);
1140 if (ae
& FATTR4_MAXFILESIZE_MASK
) {
1141 IXDR_PUT_HYPER(ptr
, pce
.maxfilesize
);
1143 if (ae
& FATTR4_MAXLINK_MASK
) {
1144 IXDR_PUT_U_INT32(ptr
, pce
.maxlink
);
1146 if (ae
& FATTR4_MAXNAME_MASK
) {
1147 IXDR_PUT_U_INT32(ptr
, pce
.maxname
);
1149 if (ae
& FATTR4_MAXREAD_MASK
) {
1150 IXDR_PUT_HYPER(ptr
, maxread
);
1152 if (ae
& FATTR4_MAXWRITE_MASK
) {
1153 IXDR_PUT_HYPER(ptr
, maxwrite
);
1155 /* Check the redzone boundary */
1156 if (ptr
> ptr_redzone
) {
1157 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1161 MINIMIZE_ATTR_MASK(ar
);
1163 ptr
= lastentry_ptr
;
1164 goto reencode_attrs
;
1168 if (ae
& 0x00000000ffffffff) {
1170 * Redzone check is done at the end of this section.
1171 * This particular section will encode a maximum of
1172 * 3 * BYTES_PER_XDR_UNIT of data.
1175 (FATTR4_MIMETYPE_MASK
|
1177 FATTR4_NO_TRUNC_MASK
|
1178 FATTR4_NUMLINKS_MASK
)) {
1180 if (ae
& FATTR4_MIMETYPE_MASK
) {
1183 if (ae
& FATTR4_MODE_MASK
) {
1184 uint_t m
= va
.va_mode
;
1185 IXDR_PUT_U_INT32(ptr
, m
);
1187 if (ae
& FATTR4_NO_TRUNC_MASK
) {
1188 IXDR_PUT_U_INT32(ptr
, true);
1190 if (ae
& FATTR4_NUMLINKS_MASK
) {
1191 IXDR_PUT_U_INT32(ptr
, va
.va_nlink
);
1193 /* Check the redzone boundary */
1194 if (ptr
> ptr_redzone
) {
1195 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1199 MINIMIZE_ATTR_MASK(ar
);
1201 ptr
= lastentry_ptr
;
1202 goto reencode_attrs
;
1206 * Redzone check is done before the encoding of the
1207 * owner string since the length is indeterminate.
1209 if (ae
& FATTR4_OWNER_MASK
) {
1211 owner_error
= nfs_idmap_uid_str(
1212 va
.va_uid
, &owner
, TRUE
);
1215 lastuid
= va
.va_uid
;
1217 } else if (va
.va_uid
!= lastuid
) {
1218 if (owner
.utf8string_len
!= 0) {
1219 kmem_free(owner
.utf8string_val
,
1220 owner
.utf8string_len
);
1221 owner
.utf8string_len
= 0;
1222 owner
.utf8string_val
= NULL
;
1224 owner_error
= nfs_idmap_uid_str(
1225 va
.va_uid
, &owner
, TRUE
);
1227 lastuid
= va
.va_uid
;
1234 (owner
.utf8string_len
/
1236 + 2) > ptr_redzone
) {
1238 IS_MIN_ATTR_MASK(ar
)) {
1242 MINIMIZE_ATTR_MASK(ar
);
1244 ptr
= lastentry_ptr
;
1245 goto reencode_attrs
;
1247 /* encode the LENGTH of owner string */
1248 IXDR_PUT_U_INT32(ptr
,
1249 owner
.utf8string_len
);
1250 /* encode the RNDUP FILL first */
1251 rndup
= RNDUP(owner
.utf8string_len
) /
1254 /* encode the OWNER */
1255 bcopy(owner
.utf8string_val
, ptr
,
1256 owner
.utf8string_len
);
1261 * Redzone check is done before the encoding of the
1262 * group string since the length is indeterminate.
1264 if (ae
& FATTR4_OWNER_GROUP_MASK
) {
1267 nfs_idmap_gid_str(va
.va_gid
,
1271 lastgid
= va
.va_gid
;
1273 } else if (va
.va_gid
!= lastgid
) {
1274 if (group
.utf8string_len
!= 0) {
1276 group
.utf8string_val
,
1277 group
.utf8string_len
);
1278 group
.utf8string_len
= 0;
1279 group
.utf8string_val
= NULL
;
1282 nfs_idmap_gid_str(va
.va_gid
,
1285 lastgid
= va
.va_gid
;
1291 (group
.utf8string_len
/
1293 + 2) > ptr_redzone
) {
1295 IS_MIN_ATTR_MASK(ar
)) {
1299 MINIMIZE_ATTR_MASK(ar
);
1301 ptr
= lastentry_ptr
;
1302 goto reencode_attrs
;
1304 /* encode the LENGTH of owner string */
1305 IXDR_PUT_U_INT32(ptr
,
1306 group
.utf8string_len
);
1307 /* encode the RNDUP FILL first */
1308 rndup
= RNDUP(group
.utf8string_len
) /
1311 /* encode the OWNER */
1312 bcopy(group
.utf8string_val
, ptr
,
1313 group
.utf8string_len
);
1318 (FATTR4_QUOTA_AVAIL_HARD_MASK
|
1319 FATTR4_QUOTA_AVAIL_SOFT_MASK
|
1320 FATTR4_QUOTA_USED_MASK
)) {
1321 if (ae
& FATTR4_QUOTA_AVAIL_HARD_MASK
) {
1324 if (ae
& FATTR4_QUOTA_AVAIL_SOFT_MASK
) {
1327 if (ae
& FATTR4_QUOTA_USED_MASK
) {
1332 * Redzone check is done at the end of this section.
1333 * This particular section will encode a maximum of
1334 * 10 * BYTES_PER_XDR_UNIT of data.
1337 (FATTR4_RAWDEV_MASK
|
1338 FATTR4_SPACE_AVAIL_MASK
|
1339 FATTR4_SPACE_FREE_MASK
|
1340 FATTR4_SPACE_TOTAL_MASK
|
1341 FATTR4_SPACE_USED_MASK
|
1342 FATTR4_SYSTEM_MASK
)) {
1344 if (ae
& FATTR4_RAWDEV_MASK
) {
1347 (uint32
)getmajor(va
.va_rdev
);
1349 (uint32
)getminor(va
.va_rdev
);
1350 IXDR_PUT_U_INT32(ptr
, rd
.specdata1
);
1351 IXDR_PUT_U_INT32(ptr
, rd
.specdata2
);
1353 if (ae
& FATTR4_SPACE_AVAIL_MASK
) {
1354 IXDR_PUT_HYPER(ptr
, sbe
.space_avail
);
1356 if (ae
& FATTR4_SPACE_FREE_MASK
) {
1357 IXDR_PUT_HYPER(ptr
, sbe
.space_free
);
1359 if (ae
& FATTR4_SPACE_TOTAL_MASK
) {
1360 IXDR_PUT_HYPER(ptr
, sbe
.space_total
);
1362 if (ae
& FATTR4_SPACE_USED_MASK
) {
1364 su
= (fattr4_space_used
) DEV_BSIZE
*
1365 (fattr4_space_used
) va
.va_nblocks
;
1366 IXDR_PUT_HYPER(ptr
, su
);
1368 if (ae
& FATTR4_SYSTEM_MASK
) {
1371 /* Check the redzone boundary */
1372 if (ptr
> ptr_redzone
) {
1373 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1377 MINIMIZE_ATTR_MASK(ar
);
1379 ptr
= lastentry_ptr
;
1380 goto reencode_attrs
;
1384 * Redzone check is done at the end of this section.
1385 * This particular section will encode a maximum of
1386 * 14 * BYTES_PER_XDR_UNIT of data.
1389 (FATTR4_TIME_ACCESS_MASK
|
1390 FATTR4_TIME_ACCESS_SET_MASK
|
1391 FATTR4_TIME_BACKUP_MASK
|
1392 FATTR4_TIME_CREATE_MASK
|
1393 FATTR4_TIME_DELTA_MASK
|
1394 FATTR4_TIME_METADATA_MASK
|
1395 FATTR4_TIME_MODIFY_MASK
|
1396 FATTR4_TIME_MODIFY_SET_MASK
|
1397 FATTR4_MOUNTED_ON_FILEID_MASK
)) {
1399 if (ae
& FATTR4_TIME_ACCESS_MASK
) {
1401 (u_longlong_t
)va
.va_atime
.tv_sec
;
1403 (uint_t
)va
.va_atime
.tv_nsec
;
1404 IXDR_PUT_HYPER(ptr
, sec
);
1405 IXDR_PUT_INT32(ptr
, nsec
);
1407 if (ae
& FATTR4_TIME_ACCESS_SET_MASK
) {
1410 if (ae
& FATTR4_TIME_BACKUP_MASK
) {
1413 if (ae
& FATTR4_TIME_CREATE_MASK
) {
1416 if (ae
& FATTR4_TIME_DELTA_MASK
) {
1417 u_longlong_t sec
= 0;
1419 IXDR_PUT_HYPER(ptr
, sec
);
1420 IXDR_PUT_INT32(ptr
, nsec
);
1422 if (ae
& FATTR4_TIME_METADATA_MASK
) {
1424 (u_longlong_t
)va
.va_ctime
.tv_sec
;
1426 (uint_t
)va
.va_ctime
.tv_nsec
;
1427 IXDR_PUT_HYPER(ptr
, sec
);
1428 IXDR_PUT_INT32(ptr
, nsec
);
1430 if (ae
& FATTR4_TIME_MODIFY_MASK
) {
1432 (u_longlong_t
)va
.va_mtime
.tv_sec
;
1434 (uint_t
)va
.va_mtime
.tv_nsec
;
1435 IXDR_PUT_HYPER(ptr
, sec
);
1436 IXDR_PUT_INT32(ptr
, nsec
);
1438 if (ae
& FATTR4_TIME_MODIFY_SET_MASK
) {
1441 if (ae
& FATTR4_MOUNTED_ON_FILEID_MASK
) {
1442 IXDR_PUT_HYPER(ptr
, dp
->d_ino
);
1444 /* Check the redzone boundary */
1445 if (ptr
> ptr_redzone
) {
1446 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1450 MINIMIZE_ATTR_MASK(ar
);
1452 ptr
= lastentry_ptr
;
1453 goto reencode_attrs
;
1458 /* Reset to directory's vfs info when encoding complete */
1459 if (vfs_different
) {
1465 /* "go back" and encode the attributes' length */
1468 (char *)attr_offset_ptr
-
1470 IXDR_PUT_U_INT32(attr_offset_ptr
, attr_length
);
1473 * If there was trouble obtaining a mapping for either
1474 * the owner or group attributes, then remove them from
1475 * bitmap4 for this entry and reset the bitmap value
1476 * in the data stream.
1478 if (owner_error
|| group_error
) {
1480 ae
&= ~FATTR4_OWNER_MASK
;
1482 ae
&= ~FATTR4_OWNER_GROUP_MASK
;
1483 IXDR_PUT_HYPER(attrmask_ptr
, ae
);
1486 /* END OF ATTRIBUTE ENCODING */
1488 lastentry_ptr
= ptr
;
1490 rddir_next_offset
= dp
->d_off
;
1494 * Check for the case that another VOP_READDIR() has to be done.
1495 * - no space encoding error
1496 * - no entry successfully encoded
1497 * - still more directory to read
1499 if (!no_space
&& nents
== 0 && !iseofdir
)
1502 *cs
->statusp
= resp
->status
= NFS4_OK
;
1505 * If no_space is set then we terminated prematurely,
1506 * rewind to the last entry and this can never be EOF.
1509 ptr
= lastentry_ptr
;
1510 eof
= FALSE
; /* ended encoded prematurely */
1512 eof
= (iseofdir
? TRUE
: FALSE
);
1516 * If we have entries, always return them, otherwise only error
1517 * if we ran out of space.
1519 if (nents
|| !no_space
) {
1520 ASSERT(ptr
!= NULL
);
1521 /* encode the BOOLEAN marking no further entries */
1522 IXDR_PUT_U_INT32(ptr
, false);
1523 /* encode the BOOLEAN signifying end of directory */
1524 IXDR_PUT_U_INT32(ptr
, eof
);
1526 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
1527 resp
->mblk
->b_wptr
+= resp
->data_len
;
1532 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
1535 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
1538 if (owner
.utf8string_len
!= 0)
1539 kmem_free(owner
.utf8string_val
, owner
.utf8string_len
);
1540 if (group
.utf8string_len
!= 0)
1541 kmem_free(group
.utf8string_val
, group
.utf8string_len
);
1544 DTRACE_NFSV4_2(op__readdir__done
, struct compound_state
*, cs
,
1545 READDIR4res
*, resp
);