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/stdbool.h>
34 #include <sys/systm.h>
38 #include <sys/vnode.h>
40 #include <sys/errno.h>
41 #include <sys/sysmacros.h>
42 #include <sys/statvfs.h>
44 #include <sys/dirent.h>
45 #include <rpc/types.h>
47 #include <rpc/rpcsec_gss.h>
49 #include <sys/strsubr.h>
50 #include <sys/strsun.h>
54 #include <nfs/export.h>
56 #include <nfs/nfs_cmd.h>
60 * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
61 * This is used to return NFS4ERR_TOOSMALL when clients specify
62 * maxcount that isn't large enough to hold the smallest possible
65 * sizeof cookie (8 bytes) +
66 * sizeof name_len (4 bytes) +
67 * sizeof smallest (padded) name (4 bytes) +
68 * sizeof bitmap4_len (12 bytes) + NOTE: we always encode len=2 bm4
69 * sizeof attrlist4_len (4 bytes) +
70 * sizeof next boolean (4 bytes)
72 * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
73 * the smallest possible entry4 (assumes no attrs requested).
74 * sizeof nfsstat4 (4 bytes) +
75 * sizeof verifier4 (8 bytes) +
76 * sizeof entsecond_to_ry4list bool (4 bytes) +
77 * sizeof entry4 (36 bytes) +
78 * sizeof eof bool (4 bytes)
80 * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
81 * fop_readdir. Its value is the size of the maximum possible dirent
82 * for solaris. The DIRENT64_RECLEN macro returns the size of dirent
83 * required for a given name length. MAXNAMELEN is the maximum
84 * filename length allowed in Solaris. The first two DIRENT64_RECLEN()
85 * macros are to allow for . and .. entries -- just a minor tweak to try
86 * and guarantee that buffer we give to fop_readdir will be large enough
87 * to hold ., .., and the largest possible solaris dirent64.
89 #define RFS4_MINLEN_ENTRY4 36
90 #define RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
91 #define RFS4_MINLEN_RDDIR_BUF \
92 (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
98 #define nextdp(dp) ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
100 verifier4 Readdir4verf
= 0x0;
102 static nfs_ftype4 vt_to_nf4
[] = {
103 0, NF4REG
, NF4DIR
, NF4BLK
, NF4CHR
, NF4LNK
, NF4FIFO
, 0, 0, NF4SOCK
, 0
107 nfs4_readdir_getvp(vnode_t
*dvp
, char *d_name
, vnode_t
**vpp
,
108 struct exportinfo
**exi
, struct svc_req
*req
, struct compound_state
*cs
,
114 vnode_t
*vp
, *pre_tvp
;
116 struct exportinfo
*newexi
, *saveexi
;
121 if (error
= fop_lookup(dvp
, d_name
, &vp
, NULL
, 0, NULL
, cs
->cr
,
126 * If the directory is a referral point, don't return the
127 * attrs, instead set rdattr_error to MOVED.
129 if (vn_is_nfs_reparse(vp
, cs
->cr
) && !client_is_downrev(req
)) {
131 DTRACE_PROBE2(nfs4serv__func__referral__moved
,
132 vnode_t
*, vp
, char *, "nfs4_readdir_getvp");
133 return (NFS4ERR_MOVED
);
136 /* Is this object mounted upon? */
137 ismntpt
= vn_ismntpt(vp
);
140 * Nothing more to do if object is not a mount point or
141 * a possible LOFS shadow of an LOFS mount (which won't
142 * have v_vfsmountedhere set)
144 if (ismntpt
== 0 && dvp
->v_vfsp
== vp
->v_vfsp
&& expseudo
== 0) {
151 * Something is mounted here. Traverse and manage the
157 if ((error
= traverse(&vp
)) != 0) {
162 if (vn_is_nfs_reparse(vp
, cs
->cr
)) {
165 DTRACE_PROBE2(nfs4serv__func__referral__moved
,
166 vnode_t
*, vp
, char *, "nfs4_readdir_getvp");
167 return (NFS4ERR_MOVED
);
171 bzero(&fid
, sizeof (fid
));
172 fid
.fid_len
= MAXFIDSZ
;
175 * If fop_fid not supported by underlying fs (mntfs, procfs,
176 * etc.), then return attrs for stub instead of VROOT object.
177 * If it fails for any other reason, then return the error.
179 if (error
= fop_fid(vp
, &fid
, NULL
)) {
185 if (error
!= ENOSYS
&& error
!= ENOTSUP
) {
190 /* go back to vnode that is "under" mount */
196 newexi
= checkexport4(&vp
->v_vfsp
->vfs_fsid
, &fid
, vp
);
197 if (newexi
== NULL
) {
210 /* Save the exi and present the new one to checkauth4() */
214 /* Get the right cred like lookup does */
216 cs
->cr
= crdup(cs
->basecr
);
218 status
= call_checkauth4(cs
, req
);
224 /* Reset what call_checkauth4() may have set */
225 *cs
->statusp
= NFS4_OK
;
227 if (status
!= NFS4_OK
) {
229 if (status
== NFS4ERR_DELAY
)
230 status
= NFS4ERR_ACCESS
;
239 /* This is the set of pathconf data for vfs */
241 uint64_t maxfilesize
;
248 rfs4_get_pc_encode(vnode_t
*vp
, rfs4_pc_encode_t
*pce
, bitmap4 ar
, cred_t
*cr
)
253 pce
->maxfilesize
= 0;
257 if (ar
& FATTR4_MAXFILESIZE_MASK
) {
258 /* Maximum File Size */
259 error
= fop_pathconf(vp
, _PC_FILESIZEBITS
, &pc_val
, cr
, NULL
);
264 * If the underlying file system does not support
265 * _PC_FILESIZEBITS, return a reasonable default. Note that
266 * error code on fop_pathconf will be 0, even if the underlying
267 * file system does not support _PC_FILESIZEBITS.
269 if (pc_val
== (ulong_t
)-1) {
270 pce
->maxfilesize
= MAXOFF32_T
;
272 if (pc_val
>= (sizeof (uint64_t) * 8))
273 pce
->maxfilesize
= INT64_MAX
;
275 pce
->maxfilesize
= ((1LL << (pc_val
- 1)) - 1);
279 if (ar
& FATTR4_MAXLINK_MASK
) {
280 /* Maximum Link Count */
281 error
= fop_pathconf(vp
, _PC_LINK_MAX
, &pc_val
, cr
, NULL
);
285 pce
->maxlink
= pc_val
;
288 if (ar
& FATTR4_MAXNAME_MASK
) {
289 /* Maximum Name Length */
290 error
= fop_pathconf(vp
, _PC_NAME_MAX
, &pc_val
, cr
, NULL
);
294 pce
->maxname
= pc_val
;
300 /* This is the set of statvfs data that is ready for encoding */
302 uint64_t space_avail
;
304 uint64_t space_total
;
311 rfs4_get_sb_encode(vfs_t
*vfsp
, rfs4_sb_encode_t
*psbe
)
316 /* Grab the per filesystem info */
317 if (error
= VFS_STATVFS(vfsp
, &sb
)) {
321 /* Calculate space available */
322 if (sb
.f_bavail
!= (fsblkcnt64_t
)-1) {
324 (fattr4_space_avail
) sb
.f_frsize
*
325 (fattr4_space_avail
) sb
.f_bavail
;
328 (fattr4_space_avail
) sb
.f_bavail
;
331 /* Calculate space free */
332 if (sb
.f_bfree
!= (fsblkcnt64_t
)-1) {
334 (fattr4_space_free
) sb
.f_frsize
*
335 (fattr4_space_free
) sb
.f_bfree
;
338 (fattr4_space_free
) sb
.f_bfree
;
341 /* Calculate space total */
342 if (sb
.f_blocks
!= (fsblkcnt64_t
)-1) {
344 (fattr4_space_total
) sb
.f_frsize
*
345 (fattr4_space_total
) sb
.f_blocks
;
348 (fattr4_space_total
) sb
.f_blocks
;
351 /* For use later on attr encode */
352 psbe
->fa
= sb
.f_favail
;
353 psbe
->ff
= sb
.f_ffree
;
354 psbe
->ft
= sb
.f_files
;
360 * Macros to handle if we have don't have enough space for the requested
361 * attributes and this is the first entry and the
362 * requested attributes are more than the minimal useful
363 * set, reset the attributes to the minimal set and
364 * retry the encoding. If the client has asked for both
365 * mounted_on_fileid and fileid, prefer mounted_on_fileid.
367 #define MINIMAL_RD_ATTRS \
368 (FATTR4_MOUNTED_ON_FILEID_MASK| \
369 FATTR4_FILEID_MASK| \
370 FATTR4_RDATTR_ERROR_MASK)
372 #define MINIMIZE_ATTR_MASK(m) { \
373 if ((m) & FATTR4_MOUNTED_ON_FILEID_MASK) \
374 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_MOUNTED_ON_FILEID_MASK;\
376 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_FILEID_MASK; \
379 #define IS_MIN_ATTR_MASK(m) (((m) & ~MINIMAL_RD_ATTRS) == 0)
381 * If readdir only needs to return FILEID, we can take it from the
382 * dirent struct and save doing the lookup.
386 rfs4_op_readdir(nfs_argop4
*argop
, nfs_resop4
*resop
, struct svc_req
*req
,
387 struct compound_state
*cs
)
389 READDIR4args
*args
= &argop
->nfs_argop4_u
.opreaddir
;
390 READDIR4res
*resp
= &resop
->nfs_resop4_u
.opreaddir
;
391 struct exportinfo
*newexi
= NULL
;
396 vnode_t
*dvp
= cs
->vp
;
400 rfs4_sb_encode_t dsbe
, sbe
;
402 int rddir_data_len
, rddir_result_size
;
404 offset_t rddir_next_offset
;
413 struct exp_visible
*visp
;
415 uint32_t *ptr
, *ptr_redzone
;
416 uint32_t *beginning_ptr
;
417 uint32_t *lastentry_ptr
;
418 uint32_t *attrmask_ptr
;
419 uint32_t *attr_offset_ptr
;
420 uint32_t attr_length
;
423 uint32_t rddirattr_error
= 0;
425 bitmap4 ar
= args
->attr_request
& NFS4_SRV_RDDIR_SUPPORTED_ATTRS
;
427 rfs4_pc_encode_t dpce
, pce
;
434 utf8string owner
, group
;
435 int owner_error
, group_error
;
439 DTRACE_NFSV4_2(op__readdir__start
, struct compound_state
*, cs
,
440 READDIR4args
*, args
);
443 owner
.utf8string_len
= group
.utf8string_len
= 0;
444 owner
.utf8string_val
= group
.utf8string_val
= NULL
;
448 /* Maximum read and write size */
449 maxread
= maxwrite
= rfs4_tsize(req
);
452 *cs
->statusp
= resp
->status
= NFS4ERR_NOFILEHANDLE
;
457 * If there is an unshared filesystem mounted on this vnode,
458 * do not allow readdir in this directory.
460 if (vn_ismntpt(dvp
)) {
461 *cs
->statusp
= resp
->status
= NFS4ERR_ACCESS
;
465 if (dvp
->v_type
!= VDIR
) {
466 *cs
->statusp
= resp
->status
= NFS4ERR_NOTDIR
;
470 if (args
->maxcount
<= RFS4_MINLEN_RDDIR4
) {
471 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
476 * If write-only attrs are requested, then fail the readdir op
478 if (args
->attr_request
&
479 (FATTR4_TIME_MODIFY_SET_MASK
| FATTR4_TIME_ACCESS_SET_MASK
)) {
480 *cs
->statusp
= resp
->status
= NFS4ERR_INVAL
;
484 error
= fop_access(dvp
, VREAD
, 0, cs
->cr
, NULL
);
486 *cs
->statusp
= resp
->status
= puterrno4(error
);
490 if (args
->cookieverf
!= Readdir4verf
) {
491 *cs
->statusp
= resp
->status
= NFS4ERR_NOT_SAME
;
495 /* Is there pseudo-fs work that is needed for this readdir? */
496 check_visible
= PSEUDO(cs
->exi
) ||
497 ! is_exported_sec(cs
->nfsflavor
, cs
->exi
) ||
498 cs
->access
& CS_ACCESS_LIMITED
;
500 /* Check the requested attributes and only do the work if needed */
502 if (ar
& (FATTR4_MAXFILESIZE_MASK
|
503 FATTR4_MAXLINK_MASK
|
504 FATTR4_MAXNAME_MASK
)) {
505 if (error
= rfs4_get_pc_encode(cs
->vp
, &dpce
, ar
, cs
->cr
)) {
506 *cs
->statusp
= resp
->status
= puterrno4(error
);
512 /* If there is statvfs data requested, pick it up once */
514 (FATTR4_FILES_AVAIL_MASK
|
515 FATTR4_FILES_FREE_MASK
|
516 FATTR4_FILES_TOTAL_MASK
|
517 FATTR4_FILES_AVAIL_MASK
|
518 FATTR4_FILES_FREE_MASK
|
519 FATTR4_FILES_TOTAL_MASK
)) {
520 if (error
= rfs4_get_sb_encode(dvp
->v_vfsp
, &dsbe
)) {
521 *cs
->statusp
= resp
->status
= puterrno4(error
);
528 * Max transfer size of the server is the absolute limite.
529 * If the client has decided to max out with something really
530 * tiny, then return toosmall. Otherwise, move forward and
531 * see if a single entry can be encoded.
533 tsize
= rfs4_tsize(req
);
534 if (args
->maxcount
> tsize
)
535 args
->maxcount
= tsize
;
536 else if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
) {
537 if (args
->maxcount
< RFS4_MINLEN_ENTRY4
) {
538 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
544 * How large should the mblk be for outgoing encoding.
546 if (args
->maxcount
< MAXBSIZE
)
549 mpcount
= args
->maxcount
;
552 * mp will contain the data to be sent out in the readdir reply.
553 * It will be freed after the reply has been sent.
554 * Let's roundup the data to a BYTES_PER_XDR_UNIX multiple,
555 * so that the call to xdrmblk_putmblk() never fails.
557 mp
= allocb(RNDUP(mpcount
), BPRI_MED
);
561 * The allocation of the client's requested size has
562 * failed. It may be that the size is too large for
563 * current system utilization; step down to a "common"
564 * size and wait for the allocation to occur.
566 if (mpcount
> MAXBSIZE
)
567 args
->maxcount
= mpcount
= MAXBSIZE
;
568 mp
= allocb_wait(RNDUP(mpcount
), BPRI_MED
,
569 STR_NOSIG
, &alloc_err
);
573 ASSERT(alloc_err
== 0);
577 ptr
= beginning_ptr
= (uint32_t *)mp
->b_datap
->db_base
;
580 * The "redzone" at the end of the encoding buffer is used
581 * to deal with xdr encoding length. Instead of checking
582 * each encoding of an attribute value before it is done,
583 * make the assumption that it will fit into the buffer and
584 * check occasionally.
586 * The largest block of attributes that are encoded without
587 * checking the redzone is 18 * BYTES_PER_XDR_UNIT (72 bytes)
588 * "round" to 128 as the redzone size.
590 if (args
->maxcount
< (mpcount
- 128))
592 (uint32_t *)(((char *)ptr
) + RNDUP(args
->maxcount
));
595 (uint32_t *)((((char *)ptr
) + RNDUP(mpcount
)) - 128);
598 * Set the dircount; this will be used as the size for the
599 * readdir of the underlying filesystem. First make sure
600 * that it is large enough to do a reasonable readdir (client
601 * may have short changed us - it is an advisory number);
602 * then make sure that it isn't too large.
603 * After all of that, if maxcount is "small" then just use
604 * that for the dircount number.
606 dircount
= (args
->dircount
< MAXBSIZE
) ? MAXBSIZE
: args
->dircount
;
607 dircount
= (dircount
> tsize
) ? tsize
: dircount
;
608 if (dircount
> args
->maxcount
)
609 dircount
= args
->maxcount
;
610 if (args
->maxcount
<= MAXBSIZE
) {
611 if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
)
612 dircount
= RFS4_MINLEN_RDDIR_BUF
;
614 dircount
= args
->maxcount
;
617 /* number of entries fully encoded in outgoing buffer */
620 /* ENCODE READDIR4res.cookieverf */
621 IXDR_PUT_HYPER(ptr
, Readdir4verf
);
623 rddir_data_len
= dircount
;
624 rddir_data
= kmem_alloc(rddir_data_len
, KM_NOSLEEP
);
625 if (rddir_data
== NULL
) {
626 /* The allocation failed; downsize and wait for it this time */
627 if (rddir_data_len
> MAXBSIZE
)
628 rddir_data_len
= dircount
= MAXBSIZE
;
629 rddir_data
= kmem_alloc(rddir_data_len
, KM_SLEEP
);
632 rddir_next_offset
= (offset_t
)args
->cookie
;
634 ca
= (struct sockaddr
*)svc_getrpccaller(req
->rq_xprt
)->buf
;
643 /* Move on to reading the directory contents */
644 iov
.iov_base
= rddir_data
;
645 iov
.iov_len
= rddir_data_len
;
648 uio
.uio_segflg
= UIO_SYSSPACE
;
649 uio
.uio_extflg
= UIO_COPY_CACHED
;
650 uio
.uio_loffset
= rddir_next_offset
;
651 uio
.uio_resid
= rddir_data_len
;
653 (void) fop_rwlock(dvp
, V_WRITELOCK_FALSE
, NULL
);
655 error
= fop_readdir(dvp
, &uio
, cs
->cr
, &iseofdir
, NULL
, 0);
657 fop_rwunlock(dvp
, V_WRITELOCK_FALSE
, NULL
);
660 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
664 *cs
->statusp
= resp
->status
= puterrno4(error
);
669 rddir_result_size
= rddir_data_len
- uio
.uio_resid
;
671 /* No data were read. Check if we reached the end of the directory. */
672 if (rddir_result_size
== 0) {
673 /* encode the BOOLEAN marking no further entries */
674 IXDR_PUT_U_INT32(ptr
, false);
675 /* encode the BOOLEAN signifying end of directory */
676 IXDR_PUT_U_INT32(ptr
, iseofdir
? true : false);
677 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
678 resp
->mblk
->b_wptr
+= resp
->data_len
;
679 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
680 *cs
->statusp
= resp
->status
= NFS4_OK
;
686 for (dp
= (struct dirent64
*)rddir_data
;
687 !no_space
&& rddir_result_size
> 0; dp
= nextdp(dp
)) {
700 rddir_result_size
-= dp
->d_reclen
;
702 /* skip "." and ".." entries */
703 if (dp
->d_ino
== 0 || NFS_IS_DOTNAME(dp
->d_name
)) {
704 rddir_next_offset
= dp
->d_off
;
709 !nfs_visible_inode(cs
->exi
, dp
->d_ino
, &visp
)) {
710 rddir_next_offset
= dp
->d_off
;
715 * Only if the client requested attributes...
716 * If the fop_lookup fails ENOENT, then skip this entry
717 * for the readdir response. If there was another error,
718 * then set the rddirattr_error and the error will be
719 * encoded later in the "attributes" section.
725 error
= nfs4_readdir_getvp(dvp
, dp
->d_name
,
726 &vp
, &newexi
, req
, cs
,
727 visp
!= NULL
? visp
->vis_exported
: 0);
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 fop_lookup()
829 * succeeded; if an error occurs on the fop_getattr() then
830 * return just the error (again if it is requested).
831 * Note that the previous fop_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 fop_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
,
922 NFS4_SET_FATTR4_CHANGE(visch
,
927 IXDR_PUT_HYPER(ptr
, change
);
929 if (ae
& FATTR4_SIZE_MASK
) {
930 u_longlong_t size
= va
.va_size
;
931 IXDR_PUT_HYPER(ptr
, size
);
933 if (ae
& FATTR4_LINK_SUPPORT_MASK
) {
934 IXDR_PUT_U_INT32(ptr
, true);
936 if (ae
& FATTR4_SYMLINK_SUPPORT_MASK
) {
937 IXDR_PUT_U_INT32(ptr
, true);
939 if (ae
& FATTR4_NAMED_ATTR_MASK
) {
944 if (!(vp
->v_vfsp
->vfs_flag
&
948 sattr_error
= fop_pathconf(vp
,
950 &pc_val
, cs
->cr
, NULL
);
951 if (sattr_error
|| pc_val
== 0)
952 (void) fop_pathconf(vp
,
957 isit
= (pc_val
? TRUE
: FALSE
);
958 IXDR_PUT_U_INT32(ptr
, isit
);
960 if (ae
& FATTR4_FSID_MASK
) {
961 u_longlong_t major
, minor
;
962 struct exportinfo
*exi
;
964 exi
= newexi
? newexi
: cs
->exi
;
965 if (exi
->exi_volatile_dev
) {
966 int *pmaj
= (int *)&major
;
968 pmaj
[0] = exi
->exi_fsid
.val
[0];
969 pmaj
[1] = exi
->exi_fsid
.val
[1];
972 major
= getmajor(va
.va_fsid
);
973 minor
= getminor(va
.va_fsid
);
975 IXDR_PUT_HYPER(ptr
, major
);
976 IXDR_PUT_HYPER(ptr
, minor
);
978 if (ae
& FATTR4_UNIQUE_HANDLES_MASK
) {
979 IXDR_PUT_U_INT32(ptr
, false);
981 if (ae
& FATTR4_LEASE_TIME_MASK
) {
982 uint_t lt
= rfs4_lease_time
;
983 IXDR_PUT_U_INT32(ptr
, lt
);
985 if (ae
& FATTR4_RDATTR_ERROR_MASK
) {
987 (rddirattr_error
== 0 ?
988 0 : puterrno4(rddirattr_error
));
989 IXDR_PUT_U_INT32(ptr
, rddirattr_error
);
992 /* Check the redzone boundary */
993 if (ptr
> ptr_redzone
) {
994 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
998 MINIMIZE_ATTR_MASK(ar
);
1000 ptr
= lastentry_ptr
;
1001 goto reencode_attrs
;
1005 * Redzone check is done at the end of this section.
1006 * This particular section will encode a maximum of
1007 * 4 * BYTES_PER_XDR_UNIT of data.
1008 * NOTE: that if ACLs are supported that the
1009 * redzone calculations will need to change.
1013 FATTR4_ACLSUPPORT_MASK
|
1014 FATTR4_ARCHIVE_MASK
|
1015 FATTR4_CANSETTIME_MASK
|
1016 FATTR4_CASE_INSENSITIVE_MASK
|
1017 FATTR4_CASE_PRESERVING_MASK
|
1018 FATTR4_CHOWN_RESTRICTED_MASK
)) {
1020 if (ae
& FATTR4_ACL_MASK
) {
1023 if (ae
& FATTR4_ACLSUPPORT_MASK
) {
1026 if (ae
& FATTR4_ARCHIVE_MASK
) {
1029 if (ae
& FATTR4_CANSETTIME_MASK
) {
1030 IXDR_PUT_U_INT32(ptr
, true);
1032 if (ae
& FATTR4_CASE_INSENSITIVE_MASK
) {
1033 IXDR_PUT_U_INT32(ptr
, false);
1035 if (ae
& FATTR4_CASE_PRESERVING_MASK
) {
1036 IXDR_PUT_U_INT32(ptr
, true);
1038 if (ae
& FATTR4_CHOWN_RESTRICTED_MASK
) {
1041 (void) fop_pathconf(vp
,
1042 _PC_CHOWN_RESTRICTED
,
1043 &pc_val
, cs
->cr
, NULL
);
1044 isit
= (pc_val
? TRUE
: FALSE
);
1045 IXDR_PUT_U_INT32(ptr
, isit
);
1047 /* Check the redzone boundary */
1048 if (ptr
> ptr_redzone
) {
1049 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1053 MINIMIZE_ATTR_MASK(ar
);
1055 ptr
= lastentry_ptr
;
1056 goto reencode_attrs
;
1060 * Redzone check is done before the filehandle
1064 (FATTR4_FILEHANDLE_MASK
|
1065 FATTR4_FILEID_MASK
)) {
1067 if (ae
& FATTR4_FILEHANDLE_MASK
) {
1071 char fh
[NFS_FH4_LEN
];
1075 (void) makefh4((nfs_fh4
*)&fh
, vp
,
1076 (newexi
? newexi
: cs
->exi
));
1078 if (dvp
->v_flag
& V_XATTRDIR
)
1079 set_fh4_flag((nfs_fh4
*)&fh
,
1082 if (!xdr_inline_encode_nfs_fh4(
1084 (nfs_fh4_fmt_t
*)fh
.val
)) {
1086 IS_MIN_ATTR_MASK(ar
)) {
1090 MINIMIZE_ATTR_MASK(ar
);
1092 ptr
= lastentry_ptr
;
1093 goto reencode_attrs
;
1096 if (ae
& FATTR4_FILEID_MASK
) {
1097 IXDR_PUT_HYPER(ptr
, va
.va_nodeid
);
1099 /* Check the redzone boundary */
1100 if (ptr
> ptr_redzone
) {
1101 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1105 MINIMIZE_ATTR_MASK(ar
);
1107 ptr
= lastentry_ptr
;
1108 goto reencode_attrs
;
1112 * Redzone check is done at the end of this section.
1113 * This particular section will encode a maximum of
1114 * 15 * BYTES_PER_XDR_UNIT of data.
1117 (FATTR4_FILES_AVAIL_MASK
|
1118 FATTR4_FILES_FREE_MASK
|
1119 FATTR4_FILES_TOTAL_MASK
|
1120 FATTR4_FS_LOCATIONS_MASK
|
1121 FATTR4_HIDDEN_MASK
|
1122 FATTR4_HOMOGENEOUS_MASK
|
1123 FATTR4_MAXFILESIZE_MASK
|
1124 FATTR4_MAXLINK_MASK
|
1125 FATTR4_MAXNAME_MASK
|
1126 FATTR4_MAXREAD_MASK
|
1127 FATTR4_MAXWRITE_MASK
)) {
1129 if (ae
& FATTR4_FILES_AVAIL_MASK
) {
1130 IXDR_PUT_HYPER(ptr
, sbe
.fa
);
1132 if (ae
& FATTR4_FILES_FREE_MASK
) {
1133 IXDR_PUT_HYPER(ptr
, sbe
.ff
);
1135 if (ae
& FATTR4_FILES_TOTAL_MASK
) {
1136 IXDR_PUT_HYPER(ptr
, sbe
.ft
);
1138 if (ae
& FATTR4_FS_LOCATIONS_MASK
) {
1141 if (ae
& FATTR4_HIDDEN_MASK
) {
1144 if (ae
& FATTR4_HOMOGENEOUS_MASK
) {
1145 IXDR_PUT_U_INT32(ptr
, true);
1147 if (ae
& FATTR4_MAXFILESIZE_MASK
) {
1148 IXDR_PUT_HYPER(ptr
, pce
.maxfilesize
);
1150 if (ae
& FATTR4_MAXLINK_MASK
) {
1151 IXDR_PUT_U_INT32(ptr
, pce
.maxlink
);
1153 if (ae
& FATTR4_MAXNAME_MASK
) {
1154 IXDR_PUT_U_INT32(ptr
, pce
.maxname
);
1156 if (ae
& FATTR4_MAXREAD_MASK
) {
1157 IXDR_PUT_HYPER(ptr
, maxread
);
1159 if (ae
& FATTR4_MAXWRITE_MASK
) {
1160 IXDR_PUT_HYPER(ptr
, maxwrite
);
1162 /* Check the redzone boundary */
1163 if (ptr
> ptr_redzone
) {
1164 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1168 MINIMIZE_ATTR_MASK(ar
);
1170 ptr
= lastentry_ptr
;
1171 goto reencode_attrs
;
1175 if (ae
& 0x00000000ffffffff) {
1177 * Redzone check is done at the end of this section.
1178 * This particular section will encode a maximum of
1179 * 3 * BYTES_PER_XDR_UNIT of data.
1182 (FATTR4_MIMETYPE_MASK
|
1184 FATTR4_NO_TRUNC_MASK
|
1185 FATTR4_NUMLINKS_MASK
)) {
1187 if (ae
& FATTR4_MIMETYPE_MASK
) {
1190 if (ae
& FATTR4_MODE_MASK
) {
1191 uint_t m
= va
.va_mode
;
1192 IXDR_PUT_U_INT32(ptr
, m
);
1194 if (ae
& FATTR4_NO_TRUNC_MASK
) {
1195 IXDR_PUT_U_INT32(ptr
, true);
1197 if (ae
& FATTR4_NUMLINKS_MASK
) {
1198 IXDR_PUT_U_INT32(ptr
, va
.va_nlink
);
1200 /* Check the redzone boundary */
1201 if (ptr
> ptr_redzone
) {
1202 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1206 MINIMIZE_ATTR_MASK(ar
);
1208 ptr
= lastentry_ptr
;
1209 goto reencode_attrs
;
1213 * Redzone check is done before the encoding of the
1214 * owner string since the length is indeterminate.
1216 if (ae
& FATTR4_OWNER_MASK
) {
1218 owner_error
= nfs_idmap_uid_str(
1219 va
.va_uid
, &owner
, TRUE
);
1222 lastuid
= va
.va_uid
;
1224 } else if (va
.va_uid
!= lastuid
) {
1225 if (owner
.utf8string_len
!= 0) {
1226 kmem_free(owner
.utf8string_val
,
1227 owner
.utf8string_len
);
1228 owner
.utf8string_len
= 0;
1229 owner
.utf8string_val
= NULL
;
1231 owner_error
= nfs_idmap_uid_str(
1232 va
.va_uid
, &owner
, TRUE
);
1234 lastuid
= va
.va_uid
;
1241 (owner
.utf8string_len
/
1243 + 2) > ptr_redzone
) {
1245 IS_MIN_ATTR_MASK(ar
)) {
1249 MINIMIZE_ATTR_MASK(ar
);
1251 ptr
= lastentry_ptr
;
1252 goto reencode_attrs
;
1254 /* encode the LENGTH of owner string */
1255 IXDR_PUT_U_INT32(ptr
,
1256 owner
.utf8string_len
);
1257 /* encode the RNDUP FILL first */
1258 rndup
= RNDUP(owner
.utf8string_len
) /
1261 /* encode the OWNER */
1262 bcopy(owner
.utf8string_val
, ptr
,
1263 owner
.utf8string_len
);
1268 * Redzone check is done before the encoding of the
1269 * group string since the length is indeterminate.
1271 if (ae
& FATTR4_OWNER_GROUP_MASK
) {
1274 nfs_idmap_gid_str(va
.va_gid
,
1278 lastgid
= va
.va_gid
;
1280 } else if (va
.va_gid
!= lastgid
) {
1281 if (group
.utf8string_len
!= 0) {
1283 group
.utf8string_val
,
1284 group
.utf8string_len
);
1285 group
.utf8string_len
= 0;
1286 group
.utf8string_val
= NULL
;
1289 nfs_idmap_gid_str(va
.va_gid
,
1292 lastgid
= va
.va_gid
;
1298 (group
.utf8string_len
/
1300 + 2) > ptr_redzone
) {
1302 IS_MIN_ATTR_MASK(ar
)) {
1306 MINIMIZE_ATTR_MASK(ar
);
1308 ptr
= lastentry_ptr
;
1309 goto reencode_attrs
;
1311 /* encode the LENGTH of owner string */
1312 IXDR_PUT_U_INT32(ptr
,
1313 group
.utf8string_len
);
1314 /* encode the RNDUP FILL first */
1315 rndup
= RNDUP(group
.utf8string_len
) /
1318 /* encode the OWNER */
1319 bcopy(group
.utf8string_val
, ptr
,
1320 group
.utf8string_len
);
1325 (FATTR4_QUOTA_AVAIL_HARD_MASK
|
1326 FATTR4_QUOTA_AVAIL_SOFT_MASK
|
1327 FATTR4_QUOTA_USED_MASK
)) {
1328 if (ae
& FATTR4_QUOTA_AVAIL_HARD_MASK
) {
1331 if (ae
& FATTR4_QUOTA_AVAIL_SOFT_MASK
) {
1334 if (ae
& FATTR4_QUOTA_USED_MASK
) {
1339 * Redzone check is done at the end of this section.
1340 * This particular section will encode a maximum of
1341 * 10 * BYTES_PER_XDR_UNIT of data.
1344 (FATTR4_RAWDEV_MASK
|
1345 FATTR4_SPACE_AVAIL_MASK
|
1346 FATTR4_SPACE_FREE_MASK
|
1347 FATTR4_SPACE_TOTAL_MASK
|
1348 FATTR4_SPACE_USED_MASK
|
1349 FATTR4_SYSTEM_MASK
)) {
1351 if (ae
& FATTR4_RAWDEV_MASK
) {
1354 (uint32
)getmajor(va
.va_rdev
);
1356 (uint32
)getminor(va
.va_rdev
);
1357 IXDR_PUT_U_INT32(ptr
, rd
.specdata1
);
1358 IXDR_PUT_U_INT32(ptr
, rd
.specdata2
);
1360 if (ae
& FATTR4_SPACE_AVAIL_MASK
) {
1361 IXDR_PUT_HYPER(ptr
, sbe
.space_avail
);
1363 if (ae
& FATTR4_SPACE_FREE_MASK
) {
1364 IXDR_PUT_HYPER(ptr
, sbe
.space_free
);
1366 if (ae
& FATTR4_SPACE_TOTAL_MASK
) {
1367 IXDR_PUT_HYPER(ptr
, sbe
.space_total
);
1369 if (ae
& FATTR4_SPACE_USED_MASK
) {
1371 su
= (fattr4_space_used
) DEV_BSIZE
*
1372 (fattr4_space_used
) va
.va_nblocks
;
1373 IXDR_PUT_HYPER(ptr
, su
);
1375 if (ae
& FATTR4_SYSTEM_MASK
) {
1378 /* Check the redzone boundary */
1379 if (ptr
> ptr_redzone
) {
1380 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1384 MINIMIZE_ATTR_MASK(ar
);
1386 ptr
= lastentry_ptr
;
1387 goto reencode_attrs
;
1391 * Redzone check is done at the end of this section.
1392 * This particular section will encode a maximum of
1393 * 14 * BYTES_PER_XDR_UNIT of data.
1396 (FATTR4_TIME_ACCESS_MASK
|
1397 FATTR4_TIME_ACCESS_SET_MASK
|
1398 FATTR4_TIME_BACKUP_MASK
|
1399 FATTR4_TIME_CREATE_MASK
|
1400 FATTR4_TIME_DELTA_MASK
|
1401 FATTR4_TIME_METADATA_MASK
|
1402 FATTR4_TIME_MODIFY_MASK
|
1403 FATTR4_TIME_MODIFY_SET_MASK
|
1404 FATTR4_MOUNTED_ON_FILEID_MASK
)) {
1406 if (ae
& FATTR4_TIME_ACCESS_MASK
) {
1408 (u_longlong_t
)va
.va_atime
.tv_sec
;
1410 (uint_t
)va
.va_atime
.tv_nsec
;
1411 IXDR_PUT_HYPER(ptr
, sec
);
1412 IXDR_PUT_INT32(ptr
, nsec
);
1414 if (ae
& FATTR4_TIME_ACCESS_SET_MASK
) {
1417 if (ae
& FATTR4_TIME_BACKUP_MASK
) {
1420 if (ae
& FATTR4_TIME_CREATE_MASK
) {
1423 if (ae
& FATTR4_TIME_DELTA_MASK
) {
1424 u_longlong_t sec
= 0;
1426 IXDR_PUT_HYPER(ptr
, sec
);
1427 IXDR_PUT_INT32(ptr
, nsec
);
1429 if (ae
& FATTR4_TIME_METADATA_MASK
) {
1431 (u_longlong_t
)va
.va_ctime
.tv_sec
;
1433 (uint_t
)va
.va_ctime
.tv_nsec
;
1434 IXDR_PUT_HYPER(ptr
, sec
);
1435 IXDR_PUT_INT32(ptr
, nsec
);
1437 if (ae
& FATTR4_TIME_MODIFY_MASK
) {
1439 (u_longlong_t
)va
.va_mtime
.tv_sec
;
1441 (uint_t
)va
.va_mtime
.tv_nsec
;
1442 IXDR_PUT_HYPER(ptr
, sec
);
1443 IXDR_PUT_INT32(ptr
, nsec
);
1445 if (ae
& FATTR4_TIME_MODIFY_SET_MASK
) {
1448 if (ae
& FATTR4_MOUNTED_ON_FILEID_MASK
) {
1449 IXDR_PUT_HYPER(ptr
, dp
->d_ino
);
1451 /* Check the redzone boundary */
1452 if (ptr
> ptr_redzone
) {
1453 if (nents
|| IS_MIN_ATTR_MASK(ar
)) {
1457 MINIMIZE_ATTR_MASK(ar
);
1459 ptr
= lastentry_ptr
;
1460 goto reencode_attrs
;
1465 /* Reset to directory's vfs info when encoding complete */
1466 if (vfs_different
) {
1472 /* "go back" and encode the attributes' length */
1475 (char *)attr_offset_ptr
-
1477 IXDR_PUT_U_INT32(attr_offset_ptr
, attr_length
);
1480 * If there was trouble obtaining a mapping for either
1481 * the owner or group attributes, then remove them from
1482 * bitmap4 for this entry and reset the bitmap value
1483 * in the data stream.
1485 if (owner_error
|| group_error
) {
1487 ae
&= ~FATTR4_OWNER_MASK
;
1489 ae
&= ~FATTR4_OWNER_GROUP_MASK
;
1490 IXDR_PUT_HYPER(attrmask_ptr
, ae
);
1493 /* END OF ATTRIBUTE ENCODING */
1495 lastentry_ptr
= ptr
;
1497 rddir_next_offset
= dp
->d_off
;
1501 * Check for the case that another fop_readdir() has to be done.
1502 * - no space encoding error
1503 * - no entry successfully encoded
1504 * - still more directory to read
1506 if (!no_space
&& nents
== 0 && !iseofdir
)
1509 *cs
->statusp
= resp
->status
= NFS4_OK
;
1512 * If no_space is set then we terminated prematurely,
1513 * rewind to the last entry and this can never be EOF.
1516 ptr
= lastentry_ptr
;
1517 eof
= FALSE
; /* ended encoded prematurely */
1519 eof
= (iseofdir
? TRUE
: FALSE
);
1523 * If we have entries, always return them, otherwise only error
1524 * if we ran out of space.
1526 if (nents
|| !no_space
) {
1527 ASSERT(ptr
!= NULL
);
1528 /* encode the BOOLEAN marking no further entries */
1529 IXDR_PUT_U_INT32(ptr
, false);
1530 /* encode the BOOLEAN signifying end of directory */
1531 IXDR_PUT_U_INT32(ptr
, eof
);
1533 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
1534 resp
->mblk
->b_wptr
+= resp
->data_len
;
1539 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
1542 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
1545 if (owner
.utf8string_len
!= 0)
1546 kmem_free(owner
.utf8string_val
, owner
.utf8string_len
);
1547 if (group
.utf8string_len
!= 0)
1548 kmem_free(group
.utf8string_val
, group
.utf8string_len
);
1551 DTRACE_NFSV4_2(op__readdir__done
, struct compound_state
*, cs
,
1552 READDIR4res
*, resp
);