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 DIRENT_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 DIRENT_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 (DIRENT_RECLEN(1) + DIRENT_RECLEN(2) + DIRENT_RECLEN(MAXNAMELEN))
98 #define nextdp(dp) ((struct dirent *)((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 * RFC 7530 Section 8.3.2
108 * Supported attributes for READDIR for the root of an absent filesystem.
110 #define ABSENT_FS_ATTRS (FATTR4_FS_LOCATIONS_MASK | FATTR4_FSID_MASK | \
111 FATTR4_MOUNTED_ON_FILEID_MASK | FATTR4_RDATTR_ERROR_MASK)
114 nfs4_readdir_getvp(vnode_t
*dvp
, char *d_name
, vnode_t
**vpp
,
115 struct exportinfo
**exi
, struct svc_req
*req
, struct compound_state
*cs
,
116 int expseudo
, bool_t
*migrated
)
121 vnode_t
*vp
, *pre_tvp
;
123 struct exportinfo
*newexi
, *saveexi
;
128 if (error
= fop_lookup(dvp
, d_name
, &vp
, NULL
, 0, NULL
, cs
->cr
,
132 /* referral point ? */
133 if (vn_is_nfs_reparse(vp
, cs
->cr
) && !client_is_downrev(req
)) {
134 DTRACE_PROBE2(nfs4serv__func__referral__moved
,
135 vnode_t
*, vp
, char *, "nfs4_readdir_getvp");
141 /* Is this object mounted upon? */
142 ismntpt
= vn_ismntpt(vp
);
145 * Nothing more to do if object is not a mount point or
146 * a possible LOFS shadow of an LOFS mount (which won't
147 * have v_vfsmountedhere set)
149 if (ismntpt
== 0 && dvp
->v_vfsp
== vp
->v_vfsp
&& expseudo
== 0) {
156 * Something is mounted here. Traverse and manage the
162 if ((error
= traverse(&vp
)) != 0) {
169 bzero(&fid
, sizeof (fid
));
170 fid
.fid_len
= MAXFIDSZ
;
173 * If fop_fid not supported by underlying fs (mntfs, procfs,
174 * etc.), then return attrs for stub instead of VROOT object.
175 * If it fails for any other reason, then return the error.
177 if (error
= fop_fid(vp
, &fid
, NULL
)) {
183 if (error
!= ENOSYS
&& error
!= ENOTSUP
) {
188 /* go back to vnode that is "under" mount */
194 newexi
= checkexport4(&vp
->v_vfsp
->vfs_fsid
, &fid
, vp
);
195 if (newexi
== NULL
) {
208 /* Save the exi and present the new one to checkauth4() */
212 /* Get the right cred like lookup does */
214 cs
->cr
= crdup(cs
->basecr
);
216 status
= call_checkauth4(cs
, req
);
222 /* Reset what call_checkauth4() may have set */
223 *cs
->statusp
= NFS4_OK
;
225 if (status
!= NFS4_OK
) {
227 if (status
== NFS4ERR_DELAY
)
228 status
= NFS4ERR_ACCESS
;
237 /* This is the set of pathconf data for vfs */
239 uint64_t maxfilesize
;
246 rfs4_get_pc_encode(vnode_t
*vp
, rfs4_pc_encode_t
*pce
, bitmap4 ar
, cred_t
*cr
)
251 pce
->maxfilesize
= 0;
255 if (ar
& FATTR4_MAXFILESIZE_MASK
) {
256 /* Maximum File Size */
257 error
= fop_pathconf(vp
, _PC_FILESIZEBITS
, &pc_val
, cr
, NULL
);
262 * If the underlying file system does not support
263 * _PC_FILESIZEBITS, return a reasonable default. Note that
264 * error code on fop_pathconf will be 0, even if the underlying
265 * file system does not support _PC_FILESIZEBITS.
267 if (pc_val
== (ulong_t
)-1) {
268 pce
->maxfilesize
= MAXOFF_T
;
270 if (pc_val
>= (sizeof (uint64_t) * 8))
271 pce
->maxfilesize
= INT64_MAX
;
273 pce
->maxfilesize
= ((1LL << (pc_val
- 1)) - 1);
277 if (ar
& FATTR4_MAXLINK_MASK
) {
278 /* Maximum Link Count */
279 error
= fop_pathconf(vp
, _PC_LINK_MAX
, &pc_val
, cr
, NULL
);
283 pce
->maxlink
= pc_val
;
286 if (ar
& FATTR4_MAXNAME_MASK
) {
287 /* Maximum Name Length */
288 error
= fop_pathconf(vp
, _PC_NAME_MAX
, &pc_val
, cr
, NULL
);
292 pce
->maxname
= pc_val
;
298 /* This is the set of statvfs data that is ready for encoding */
300 uint64_t space_avail
;
302 uint64_t space_total
;
309 rfs4_get_sb_encode(vfs_t
*vfsp
, rfs4_sb_encode_t
*psbe
)
314 /* Grab the per filesystem info */
315 if (error
= VFS_STATVFS(vfsp
, &sb
)) {
319 /* Calculate space available */
320 if (sb
.f_bavail
!= (fsblkcnt64_t
)-1) {
322 (fattr4_space_avail
) sb
.f_frsize
*
323 (fattr4_space_avail
) sb
.f_bavail
;
326 (fattr4_space_avail
) sb
.f_bavail
;
329 /* Calculate space free */
330 if (sb
.f_bfree
!= (fsblkcnt64_t
)-1) {
332 (fattr4_space_free
) sb
.f_frsize
*
333 (fattr4_space_free
) sb
.f_bfree
;
336 (fattr4_space_free
) sb
.f_bfree
;
339 /* Calculate space total */
340 if (sb
.f_blocks
!= (fsblkcnt64_t
)-1) {
342 (fattr4_space_total
) sb
.f_frsize
*
343 (fattr4_space_total
) sb
.f_blocks
;
346 (fattr4_space_total
) sb
.f_blocks
;
349 /* For use later on attr encode */
350 psbe
->fa
= sb
.f_favail
;
351 psbe
->ff
= sb
.f_ffree
;
352 psbe
->ft
= sb
.f_files
;
357 extern void rfs4_free_fs_locations4(fs_locations4
*);
360 * If readdir only needs to return FILEID, we can take it from the
361 * dirent struct and save doing the lookup.
365 rfs4_op_readdir(nfs_argop4
*argop
, nfs_resop4
*resop
, struct svc_req
*req
,
366 struct compound_state
*cs
)
368 READDIR4args
*args
= &argop
->nfs_argop4_u
.opreaddir
;
369 READDIR4res
*resp
= &resop
->nfs_resop4_u
.opreaddir
;
370 struct exportinfo
*newexi
= NULL
;
375 vnode_t
*dvp
= cs
->vp
;
379 rfs4_sb_encode_t dsbe
, sbe
;
381 int rddir_data_len
, rddir_result_size
;
383 offset_t rddir_next_offset
;
392 struct exp_visible
*visp
;
394 uint32_t *ptr
, *ptr_redzone
;
395 uint32_t *beginning_ptr
;
396 uint32_t *lastentry_ptr
;
397 uint32_t *attrmask_ptr
;
398 uint32_t *attr_offset_ptr
;
399 uint32_t attr_length
;
402 uint32_t rddirattr_error
= 0;
404 bitmap4 ar
= args
->attr_request
& NFS4_SRV_RDDIR_SUPPORTED_ATTRS
;
406 rfs4_pc_encode_t dpce
, pce
;
413 utf8string owner
, group
;
414 int owner_error
, group_error
;
417 nfsstat4 status
= NFS4_OK
;
419 DTRACE_NFSV4_2(op__readdir__start
, struct compound_state
*, cs
,
420 READDIR4args
*, args
);
423 owner
.utf8string_len
= group
.utf8string_len
= 0;
424 owner
.utf8string_val
= group
.utf8string_val
= NULL
;
428 /* Maximum read and write size */
429 maxread
= maxwrite
= rfs4_tsize(req
);
432 *cs
->statusp
= resp
->status
= NFS4ERR_NOFILEHANDLE
;
437 * If there is an unshared filesystem mounted on this vnode,
438 * do not allow readdir in this directory.
440 if (vn_ismntpt(dvp
)) {
441 *cs
->statusp
= resp
->status
= NFS4ERR_ACCESS
;
445 if (dvp
->v_type
!= VDIR
) {
446 *cs
->statusp
= resp
->status
= NFS4ERR_NOTDIR
;
450 if (args
->maxcount
<= RFS4_MINLEN_RDDIR4
) {
451 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
456 * If write-only attrs are requested, then fail the readdir op
458 if (args
->attr_request
&
459 (FATTR4_TIME_MODIFY_SET_MASK
| FATTR4_TIME_ACCESS_SET_MASK
)) {
460 *cs
->statusp
= resp
->status
= NFS4ERR_INVAL
;
464 error
= fop_access(dvp
, VREAD
, 0, cs
->cr
, NULL
);
466 *cs
->statusp
= resp
->status
= puterrno4(error
);
470 if (args
->cookieverf
!= Readdir4verf
) {
471 *cs
->statusp
= resp
->status
= NFS4ERR_NOT_SAME
;
475 /* Is there pseudo-fs work that is needed for this readdir? */
476 check_visible
= PSEUDO(cs
->exi
) ||
477 ! is_exported_sec(cs
->nfsflavor
, cs
->exi
) ||
478 cs
->access
& CS_ACCESS_LIMITED
;
480 /* Check the requested attributes and only do the work if needed */
482 if (ar
& (FATTR4_MAXFILESIZE_MASK
|
483 FATTR4_MAXLINK_MASK
|
484 FATTR4_MAXNAME_MASK
)) {
485 if (error
= rfs4_get_pc_encode(cs
->vp
, &dpce
, ar
, cs
->cr
)) {
486 *cs
->statusp
= resp
->status
= puterrno4(error
);
492 /* If there is statvfs data requested, pick it up once */
494 (FATTR4_FILES_AVAIL_MASK
|
495 FATTR4_FILES_FREE_MASK
|
496 FATTR4_FILES_TOTAL_MASK
|
497 FATTR4_FILES_AVAIL_MASK
|
498 FATTR4_FILES_FREE_MASK
|
499 FATTR4_FILES_TOTAL_MASK
)) {
500 if (error
= rfs4_get_sb_encode(dvp
->v_vfsp
, &dsbe
)) {
501 *cs
->statusp
= resp
->status
= puterrno4(error
);
508 * Max transfer size of the server is the absolute limite.
509 * If the client has decided to max out with something really
510 * tiny, then return toosmall. Otherwise, move forward and
511 * see if a single entry can be encoded.
513 tsize
= rfs4_tsize(req
);
514 if (args
->maxcount
> tsize
)
515 args
->maxcount
= tsize
;
516 else if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
) {
517 if (args
->maxcount
< RFS4_MINLEN_ENTRY4
) {
518 *cs
->statusp
= resp
->status
= NFS4ERR_TOOSMALL
;
524 * How large should the mblk be for outgoing encoding.
526 if (args
->maxcount
< MAXBSIZE
)
529 mpcount
= args
->maxcount
;
532 * mp will contain the data to be sent out in the readdir reply.
533 * It will be freed after the reply has been sent.
534 * Let's roundup the data to a BYTES_PER_XDR_UNIX multiple,
535 * so that the call to xdrmblk_putmblk() never fails.
537 mp
= allocb(RNDUP(mpcount
), BPRI_MED
);
541 * The allocation of the client's requested size has
542 * failed. It may be that the size is too large for
543 * current system utilization; step down to a "common"
544 * size and wait for the allocation to occur.
546 if (mpcount
> MAXBSIZE
)
547 args
->maxcount
= mpcount
= MAXBSIZE
;
548 mp
= allocb_wait(RNDUP(mpcount
), BPRI_MED
,
549 STR_NOSIG
, &alloc_err
);
553 ASSERT(alloc_err
== 0);
557 ptr
= beginning_ptr
= (uint32_t *)mp
->b_datap
->db_base
;
560 * The "redzone" at the end of the encoding buffer is used
561 * to deal with xdr encoding length. Instead of checking
562 * each encoding of an attribute value before it is done,
563 * make the assumption that it will fit into the buffer and
564 * check occasionally.
566 * The largest block of attributes that are encoded without
567 * checking the redzone is 18 * BYTES_PER_XDR_UNIT (72 bytes)
568 * "round" to 128 as the redzone size.
570 if (args
->maxcount
< (mpcount
- 128))
572 (uint32_t *)(((char *)ptr
) + RNDUP(args
->maxcount
));
575 (uint32_t *)((((char *)ptr
) + RNDUP(mpcount
)) - 128);
578 * Set the dircount; this will be used as the size for the
579 * readdir of the underlying filesystem. First make sure
580 * that it is large enough to do a reasonable readdir (client
581 * may have short changed us - it is an advisory number);
582 * then make sure that it isn't too large.
583 * After all of that, if maxcount is "small" then just use
584 * that for the dircount number.
586 dircount
= (args
->dircount
< MAXBSIZE
) ? MAXBSIZE
: args
->dircount
;
587 dircount
= (dircount
> tsize
) ? tsize
: dircount
;
588 if (dircount
> args
->maxcount
)
589 dircount
= args
->maxcount
;
590 if (args
->maxcount
<= MAXBSIZE
) {
591 if (args
->maxcount
< RFS4_MINLEN_RDDIR_BUF
)
592 dircount
= RFS4_MINLEN_RDDIR_BUF
;
594 dircount
= args
->maxcount
;
597 /* number of entries fully encoded in outgoing buffer */
600 /* ENCODE READDIR4res.cookieverf */
601 IXDR_PUT_HYPER(ptr
, Readdir4verf
);
603 rddir_data_len
= dircount
;
604 rddir_data
= kmem_alloc(rddir_data_len
, KM_NOSLEEP
);
605 if (rddir_data
== NULL
) {
606 /* The allocation failed; downsize and wait for it this time */
607 if (rddir_data_len
> MAXBSIZE
)
608 rddir_data_len
= dircount
= MAXBSIZE
;
609 rddir_data
= kmem_alloc(rddir_data_len
, KM_SLEEP
);
612 rddir_next_offset
= (offset_t
)args
->cookie
;
614 ca
= (struct sockaddr
*)svc_getrpccaller(req
->rq_xprt
)->buf
;
623 /* Move on to reading the directory contents */
624 iov
.iov_base
= rddir_data
;
625 iov
.iov_len
= rddir_data_len
;
628 uio
.uio_segflg
= UIO_SYSSPACE
;
629 uio
.uio_extflg
= UIO_COPY_CACHED
;
630 uio
.uio_loffset
= rddir_next_offset
;
631 uio
.uio_resid
= rddir_data_len
;
633 (void) fop_rwlock(dvp
, V_WRITELOCK_FALSE
, NULL
);
635 error
= fop_readdir(dvp
, &uio
, cs
->cr
, &iseofdir
, NULL
, 0);
637 fop_rwunlock(dvp
, V_WRITELOCK_FALSE
, NULL
);
640 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
644 *cs
->statusp
= resp
->status
= puterrno4(error
);
649 rddir_result_size
= rddir_data_len
- uio
.uio_resid
;
651 /* No data were read. Check if we reached the end of the directory. */
652 if (rddir_result_size
== 0) {
653 /* encode the BOOLEAN marking no further entries */
654 IXDR_PUT_U_INT32(ptr
, false);
655 /* encode the BOOLEAN signifying end of directory */
656 IXDR_PUT_U_INT32(ptr
, iseofdir
? true : false);
657 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
658 resp
->mblk
->b_wptr
+= resp
->data_len
;
659 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
660 *cs
->statusp
= resp
->status
= NFS4_OK
;
666 for (dp
= (struct dirent
*)rddir_data
;
667 !no_space
&& rddir_result_size
> 0; dp
= nextdp(dp
)) {
668 bool_t fs_migrated
= FALSE
;
681 rddir_result_size
-= dp
->d_reclen
;
683 /* skip "." and ".." entries */
684 if (dp
->d_ino
== 0 || NFS_IS_DOTNAME(dp
->d_name
)) {
685 rddir_next_offset
= dp
->d_off
;
690 !nfs_visible_inode(cs
->exi
, dp
->d_ino
, &visp
)) {
691 rddir_next_offset
= dp
->d_off
;
696 * Only if the client requested attributes...
697 * If the fop_lookup fails ENOENT, then skip this entry
698 * for the readdir response. If there was another error,
699 * then set the rddirattr_error and the error will be
700 * encoded later in the "attributes" section.
706 error
= nfs4_readdir_getvp(dvp
, dp
->d_name
,
707 &vp
, &newexi
, req
, cs
,
708 visp
!= NULL
? visp
->vis_exported
: 0, &fs_migrated
);
709 if (error
== ENOENT
) {
710 rddir_next_offset
= dp
->d_off
;
714 rddirattr_error
= error
;
717 * The vp obtained from above may be from a
718 * different filesystem mount and the vfs-like
719 * attributes should be obtained from that
720 * different vfs; only do this if appropriate.
723 (vfs_different
= (dvp
->v_vfsp
!= vp
->v_vfsp
))) {
724 if (ae
& (FATTR4_FILES_AVAIL_MASK
|
725 FATTR4_FILES_FREE_MASK
|
726 FATTR4_FILES_TOTAL_MASK
|
727 FATTR4_FILES_AVAIL_MASK
|
728 FATTR4_FILES_FREE_MASK
|
729 FATTR4_FILES_TOTAL_MASK
)) {
731 rfs4_get_sb_encode(dvp
->v_vfsp
,
733 /* Remove attrs from encode */
734 ae
&= ~(FATTR4_FILES_AVAIL_MASK
|
735 FATTR4_FILES_FREE_MASK
|
736 FATTR4_FILES_TOTAL_MASK
|
737 FATTR4_FILES_AVAIL_MASK
|
738 FATTR4_FILES_FREE_MASK
|
739 FATTR4_FILES_TOTAL_MASK
);
740 rddirattr_error
= error
;
743 if (ae
& (FATTR4_MAXFILESIZE_MASK
|
744 FATTR4_MAXLINK_MASK
|
745 FATTR4_MAXNAME_MASK
)) {
746 if (error
= rfs4_get_pc_encode(cs
->vp
,
748 ae
&= ~(FATTR4_MAXFILESIZE_MASK
|
749 FATTR4_MAXLINK_MASK
|
750 FATTR4_MAXNAME_MASK
);
751 rddirattr_error
= error
;
757 /* encode the BOOLEAN for the existence of the next entry */
758 IXDR_PUT_U_INT32(ptr
, true);
759 /* encode the COOKIE for the entry */
760 IXDR_PUT_U_HYPER(ptr
, dp
->d_off
);
762 name
= nfscmd_convname(ca
, cs
->exi
, dp
->d_name
,
763 NFSCMD_CONV_OUTBOUND
, MAXPATHLEN
+ 1);
766 rddir_next_offset
= dp
->d_off
;
769 /* Calculate the dirent name length */
770 namelen
= strlen(name
);
772 rndup
= RNDUP(namelen
) / BYTES_PER_XDR_UNIT
;
774 /* room for LENGTH + string ? */
775 if ((ptr
+ (1 + rndup
)) > ptr_redzone
) {
780 /* encode the LENGTH of the name */
781 IXDR_PUT_U_INT32(ptr
, namelen
);
782 /* encode the RNDUP FILL first */
784 /* encode the NAME of the entry */
785 bcopy(name
, (char *)ptr
, namelen
);
786 /* now bump the ptr after... */
789 if (name
!= dp
->d_name
)
790 kmem_free(name
, MAXPATHLEN
+ 1);
793 * Keep checking on the dircount to see if we have
794 * reached the limit; from the RFC, dircount is to be
795 * the XDR encoded limit of the cookie plus name.
796 * So the count is the name, XDR_UNIT of length for
797 * that name and 2 * XDR_UNIT bytes of cookie;
798 * However, use the regular DIRENT64 to match most
801 dircount
-= DIRENT_RECLEN(namelen
);
802 if (nents
!= 0 && dircount
< 0) {
808 * Attributes requested?
809 * Gather up the attribute info and the previous fop_lookup()
810 * succeeded; if an error occurs on the fop_getattr() then
811 * return just the error (again if it is requested).
812 * Note that the previous fop_lookup() could have failed
813 * itself which leaves this code without anything for
815 * Also note that the readdir_attr_error is left in the
816 * encoding mask if requested and so is the mounted_on_fileid.
820 ae
= ar
& (FATTR4_RDATTR_ERROR_MASK
|
821 FATTR4_MOUNTED_ON_FILEID_MASK
);
822 } else if (fs_migrated
) {
824 * RFC 7530 Section 8.3.2
825 * Restrict to actually available attributes in
826 * case of an absent filesystem.
828 ae
&= ABSENT_FS_ATTRS
;
830 if (!(ae
& FATTR4_FS_LOCATIONS_MASK
))
831 rddirattr_error
= NFS4ERR_MOVED
;
833 va
.va_mask
= VATTR_ALL
;
835 fop_getattr(vp
, &va
, 0, cs
->cr
, NULL
);
836 if (rddirattr_error
) {
837 ae
= ae
& (FATTR4_RDATTR_ERROR_MASK
|
838 FATTR4_MOUNTED_ON_FILEID_MASK
);
841 * We may lie about the object
842 * type for a referral
844 if (vn_is_nfs_reparse(vp
, cs
->cr
) &&
845 client_is_downrev(req
))
850 /* RFC 7530 Section 8.3.2 and 16.24.4 */
851 if (rddirattr_error
!= 0 &&
852 !(ae
& FATTR4_RDATTR_ERROR_MASK
)) {
853 status
= puterrno4(rddirattr_error
);
858 /* START OF ATTRIBUTE ENCODING */
860 /* encode the LENGTH of the BITMAP4 array */
861 IXDR_PUT_U_INT32(ptr
, 2);
862 /* encode the BITMAP4 */
864 IXDR_PUT_HYPER(ptr
, ae
);
865 attr_offset_ptr
= ptr
;
866 /* encode the default LENGTH of the attributes for entry */
867 IXDR_PUT_U_INT32(ptr
, 0);
869 if (ptr
> ptr_redzone
) {
874 /* Check if any of the first 32 attributes are being encoded */
875 if (ae
& 0xffffffff00000000) {
877 * Redzone check is done at the end of this section.
878 * This particular section will encode a maximum of
879 * 18 * BYTES_PER_XDR_UNIT of data
882 (FATTR4_SUPPORTED_ATTRS_MASK
|
884 FATTR4_FH_EXPIRE_TYPE_MASK
|
887 FATTR4_LINK_SUPPORT_MASK
|
888 FATTR4_SYMLINK_SUPPORT_MASK
|
889 FATTR4_NAMED_ATTR_MASK
|
891 FATTR4_UNIQUE_HANDLES_MASK
|
892 FATTR4_LEASE_TIME_MASK
|
893 FATTR4_RDATTR_ERROR_MASK
)) {
895 if (ae
& FATTR4_SUPPORTED_ATTRS_MASK
) {
896 IXDR_PUT_INT32(ptr
, 2);
898 rfs4_supported_attrs
);
900 if (ae
& FATTR4_TYPE_MASK
) {
901 uint_t ftype
= vt_to_nf4
[va
.va_type
];
902 if (dvp
->v_flag
& V_XATTRDIR
) {
903 if (va
.va_type
== VDIR
)
906 ftype
= NF4NAMEDATTR
;
908 IXDR_PUT_U_INT32(ptr
, ftype
);
910 if (ae
& FATTR4_FH_EXPIRE_TYPE_MASK
) {
911 uint_t expire_type
= FH4_PERSISTENT
;
912 IXDR_PUT_U_INT32(ptr
, expire_type
);
914 if (ae
& FATTR4_CHANGE_MASK
) {
916 NFS4_SET_FATTR4_CHANGE(change
,
920 NFS4_SET_FATTR4_CHANGE(visch
,
925 IXDR_PUT_HYPER(ptr
, change
);
927 if (ae
& FATTR4_SIZE_MASK
) {
928 u_longlong_t size
= va
.va_size
;
929 IXDR_PUT_HYPER(ptr
, size
);
931 if (ae
& FATTR4_LINK_SUPPORT_MASK
) {
932 IXDR_PUT_U_INT32(ptr
, true);
934 if (ae
& FATTR4_SYMLINK_SUPPORT_MASK
) {
935 IXDR_PUT_U_INT32(ptr
, true);
937 if (ae
& FATTR4_NAMED_ATTR_MASK
) {
942 if (!(vp
->v_vfsp
->vfs_flag
&
946 sattr_error
= fop_pathconf(vp
,
948 &pc_val
, cs
->cr
, NULL
);
949 if (sattr_error
|| pc_val
== 0)
950 (void) fop_pathconf(vp
,
955 isit
= (pc_val
? TRUE
: FALSE
);
956 IXDR_PUT_U_INT32(ptr
, isit
);
958 if (ae
& FATTR4_FSID_MASK
) {
959 u_longlong_t major
, minor
;
960 struct exportinfo
*exi
;
962 exi
= newexi
? newexi
: cs
->exi
;
966 } else if (exi
->exi_volatile_dev
) {
967 int *pmaj
= (int *)&major
;
969 pmaj
[0] = exi
->exi_fsid
.val
[0];
970 pmaj
[1] = exi
->exi_fsid
.val
[1];
973 major
= getmajor(va
.va_fsid
);
974 minor
= getminor(va
.va_fsid
);
976 IXDR_PUT_HYPER(ptr
, major
);
977 IXDR_PUT_HYPER(ptr
, minor
);
979 if (ae
& FATTR4_UNIQUE_HANDLES_MASK
) {
980 IXDR_PUT_U_INT32(ptr
, false);
982 if (ae
& FATTR4_LEASE_TIME_MASK
) {
983 uint_t lt
= rfs4_lease_time
;
984 IXDR_PUT_U_INT32(ptr
, lt
);
986 if (ae
& FATTR4_RDATTR_ERROR_MASK
) {
988 (rddirattr_error
== 0 ?
989 0 : puterrno4(rddirattr_error
));
990 IXDR_PUT_U_INT32(ptr
, rddirattr_error
);
993 /* Check the redzone boundary */
994 if (ptr
> ptr_redzone
) {
1000 * Redzone check is done at the end of this section.
1001 * This particular section will encode a maximum of
1002 * 4 * BYTES_PER_XDR_UNIT of data.
1003 * NOTE: that if ACLs are supported that the
1004 * redzone calculations will need to change.
1008 FATTR4_ACLSUPPORT_MASK
|
1009 FATTR4_ARCHIVE_MASK
|
1010 FATTR4_CANSETTIME_MASK
|
1011 FATTR4_CASE_INSENSITIVE_MASK
|
1012 FATTR4_CASE_PRESERVING_MASK
|
1013 FATTR4_CHOWN_RESTRICTED_MASK
)) {
1015 if (ae
& FATTR4_ACL_MASK
) {
1018 if (ae
& FATTR4_ACLSUPPORT_MASK
) {
1021 if (ae
& FATTR4_ARCHIVE_MASK
) {
1024 if (ae
& FATTR4_CANSETTIME_MASK
) {
1025 IXDR_PUT_U_INT32(ptr
, true);
1027 if (ae
& FATTR4_CASE_INSENSITIVE_MASK
) {
1028 IXDR_PUT_U_INT32(ptr
, false);
1030 if (ae
& FATTR4_CASE_PRESERVING_MASK
) {
1031 IXDR_PUT_U_INT32(ptr
, true);
1033 if (ae
& FATTR4_CHOWN_RESTRICTED_MASK
) {
1036 (void) fop_pathconf(vp
,
1037 _PC_CHOWN_RESTRICTED
,
1038 &pc_val
, cs
->cr
, NULL
);
1039 isit
= (pc_val
? TRUE
: FALSE
);
1040 IXDR_PUT_U_INT32(ptr
, isit
);
1042 /* Check the redzone boundary */
1043 if (ptr
> ptr_redzone
) {
1049 * Redzone check is done before the filehandle
1053 (FATTR4_FILEHANDLE_MASK
|
1054 FATTR4_FILEID_MASK
)) {
1056 if (ae
& FATTR4_FILEHANDLE_MASK
) {
1060 char fh
[NFS_FH4_LEN
];
1064 (void) makefh4((nfs_fh4
*)&fh
, vp
,
1065 (newexi
? newexi
: cs
->exi
));
1067 if (dvp
->v_flag
& V_XATTRDIR
)
1068 set_fh4_flag((nfs_fh4
*)&fh
,
1071 if (!xdr_inline_encode_nfs_fh4(
1073 (nfs_fh4_fmt_t
*)fh
.val
)) {
1078 if (ae
& FATTR4_FILEID_MASK
) {
1079 IXDR_PUT_HYPER(ptr
, va
.va_nodeid
);
1081 /* Check the redzone boundary */
1082 if (ptr
> ptr_redzone
) {
1089 * Redzone check is done at the end of this section.
1090 * This particular section will encode a maximum of
1091 * 6 * BYTES_PER_XDR_UNIT of data.
1094 (FATTR4_FILES_AVAIL_MASK
|
1095 FATTR4_FILES_FREE_MASK
|
1096 FATTR4_FILES_TOTAL_MASK
)) {
1098 if (ae
& FATTR4_FILES_AVAIL_MASK
) {
1099 IXDR_PUT_HYPER(ptr
, sbe
.fa
);
1101 if (ae
& FATTR4_FILES_FREE_MASK
) {
1102 IXDR_PUT_HYPER(ptr
, sbe
.ff
);
1104 if (ae
& FATTR4_FILES_TOTAL_MASK
) {
1105 IXDR_PUT_HYPER(ptr
, sbe
.ft
);
1108 /* Check the redzone boundary */
1109 if (ptr
> ptr_redzone
) {
1116 * Handle fs_locations separately.
1117 * This can be quite slow for referrals. Usually
1118 * clients don't use this attribute in readdir.
1120 if (ae
& FATTR4_FS_LOCATIONS_MASK
) {
1126 (void) memset(&fs
, 0, sizeof (fs_locations4
));
1129 p
= fetch_referral(vp
, cs
->cr
);
1132 kmem_free(p
, sizeof (fs_locations4
));
1135 ASSERT(ptr_redzone
>= ptr
);
1136 ASSERT((uintptr_t)ptr_redzone
- (uintptr_t)ptr
1138 xdrmem_create(&xdr
, (caddr_t
)ptr
,
1139 (uintptr_t)ptr_redzone
- (uintptr_t)ptr
,
1142 pushed
= xdr_fattr4_fs_locations(&xdr
, &fs
);
1144 ptr
= (uint32_t *)((char *)ptr
+
1148 rfs4_free_fs_locations4(&fs
);
1150 if (!pushed
|| ptr
> ptr_redzone
) {
1157 (FATTR4_HIDDEN_MASK
|
1158 FATTR4_HOMOGENEOUS_MASK
|
1159 FATTR4_MAXFILESIZE_MASK
|
1160 FATTR4_MAXLINK_MASK
|
1161 FATTR4_MAXNAME_MASK
|
1162 FATTR4_MAXREAD_MASK
|
1163 FATTR4_MAXWRITE_MASK
)) {
1165 if (ae
& FATTR4_HIDDEN_MASK
) {
1168 if (ae
& FATTR4_HOMOGENEOUS_MASK
) {
1169 IXDR_PUT_U_INT32(ptr
, true);
1171 if (ae
& FATTR4_MAXFILESIZE_MASK
) {
1172 IXDR_PUT_HYPER(ptr
, pce
.maxfilesize
);
1174 if (ae
& FATTR4_MAXLINK_MASK
) {
1175 IXDR_PUT_U_INT32(ptr
, pce
.maxlink
);
1177 if (ae
& FATTR4_MAXNAME_MASK
) {
1178 IXDR_PUT_U_INT32(ptr
, pce
.maxname
);
1180 if (ae
& FATTR4_MAXREAD_MASK
) {
1181 IXDR_PUT_HYPER(ptr
, maxread
);
1183 if (ae
& FATTR4_MAXWRITE_MASK
) {
1184 IXDR_PUT_HYPER(ptr
, maxwrite
);
1186 /* Check the redzone boundary */
1187 if (ptr
> ptr_redzone
) {
1194 if (ae
& 0x00000000ffffffff) {
1196 * Redzone check is done at the end of this section.
1197 * This particular section will encode a maximum of
1198 * 3 * BYTES_PER_XDR_UNIT of data.
1201 (FATTR4_MIMETYPE_MASK
|
1203 FATTR4_NO_TRUNC_MASK
|
1204 FATTR4_NUMLINKS_MASK
)) {
1206 if (ae
& FATTR4_MIMETYPE_MASK
) {
1209 if (ae
& FATTR4_MODE_MASK
) {
1210 uint_t m
= va
.va_mode
;
1211 IXDR_PUT_U_INT32(ptr
, m
);
1213 if (ae
& FATTR4_NO_TRUNC_MASK
) {
1214 IXDR_PUT_U_INT32(ptr
, true);
1216 if (ae
& FATTR4_NUMLINKS_MASK
) {
1217 IXDR_PUT_U_INT32(ptr
, va
.va_nlink
);
1219 /* Check the redzone boundary */
1220 if (ptr
> ptr_redzone
) {
1226 * Redzone check is done before the encoding of the
1227 * owner string since the length is indeterminate.
1229 if (ae
& FATTR4_OWNER_MASK
) {
1231 owner_error
= nfs_idmap_uid_str(
1232 va
.va_uid
, &owner
, TRUE
);
1235 lastuid
= va
.va_uid
;
1237 } else if (va
.va_uid
!= lastuid
) {
1238 if (owner
.utf8string_len
!= 0) {
1239 kmem_free(owner
.utf8string_val
,
1240 owner
.utf8string_len
);
1241 owner
.utf8string_len
= 0;
1242 owner
.utf8string_val
= NULL
;
1244 owner_error
= nfs_idmap_uid_str(
1245 va
.va_uid
, &owner
, TRUE
);
1247 lastuid
= va
.va_uid
;
1254 (owner
.utf8string_len
/
1256 + 2) > ptr_redzone
) {
1260 /* encode the LENGTH of owner string */
1261 IXDR_PUT_U_INT32(ptr
,
1262 owner
.utf8string_len
);
1263 /* encode the RNDUP FILL first */
1264 rndup
= RNDUP(owner
.utf8string_len
) /
1267 /* encode the OWNER */
1268 bcopy(owner
.utf8string_val
, ptr
,
1269 owner
.utf8string_len
);
1274 * Redzone check is done before the encoding of the
1275 * group string since the length is indeterminate.
1277 if (ae
& FATTR4_OWNER_GROUP_MASK
) {
1280 nfs_idmap_gid_str(va
.va_gid
,
1284 lastgid
= va
.va_gid
;
1286 } else if (va
.va_gid
!= lastgid
) {
1287 if (group
.utf8string_len
!= 0) {
1289 group
.utf8string_val
,
1290 group
.utf8string_len
);
1291 group
.utf8string_len
= 0;
1292 group
.utf8string_val
= NULL
;
1295 nfs_idmap_gid_str(va
.va_gid
,
1298 lastgid
= va
.va_gid
;
1304 (group
.utf8string_len
/
1306 + 2) > ptr_redzone
) {
1310 /* encode the LENGTH of owner string */
1311 IXDR_PUT_U_INT32(ptr
,
1312 group
.utf8string_len
);
1313 /* encode the RNDUP FILL first */
1314 rndup
= RNDUP(group
.utf8string_len
) /
1317 /* encode the OWNER */
1318 bcopy(group
.utf8string_val
, ptr
,
1319 group
.utf8string_len
);
1324 (FATTR4_QUOTA_AVAIL_HARD_MASK
|
1325 FATTR4_QUOTA_AVAIL_SOFT_MASK
|
1326 FATTR4_QUOTA_USED_MASK
)) {
1327 if (ae
& FATTR4_QUOTA_AVAIL_HARD_MASK
) {
1330 if (ae
& FATTR4_QUOTA_AVAIL_SOFT_MASK
) {
1333 if (ae
& FATTR4_QUOTA_USED_MASK
) {
1338 * Redzone check is done at the end of this section.
1339 * This particular section will encode a maximum of
1340 * 10 * BYTES_PER_XDR_UNIT of data.
1343 (FATTR4_RAWDEV_MASK
|
1344 FATTR4_SPACE_AVAIL_MASK
|
1345 FATTR4_SPACE_FREE_MASK
|
1346 FATTR4_SPACE_TOTAL_MASK
|
1347 FATTR4_SPACE_USED_MASK
|
1348 FATTR4_SYSTEM_MASK
)) {
1350 if (ae
& FATTR4_RAWDEV_MASK
) {
1353 (uint32
)getmajor(va
.va_rdev
);
1355 (uint32
)getminor(va
.va_rdev
);
1356 IXDR_PUT_U_INT32(ptr
, rd
.specdata1
);
1357 IXDR_PUT_U_INT32(ptr
, rd
.specdata2
);
1359 if (ae
& FATTR4_SPACE_AVAIL_MASK
) {
1360 IXDR_PUT_HYPER(ptr
, sbe
.space_avail
);
1362 if (ae
& FATTR4_SPACE_FREE_MASK
) {
1363 IXDR_PUT_HYPER(ptr
, sbe
.space_free
);
1365 if (ae
& FATTR4_SPACE_TOTAL_MASK
) {
1366 IXDR_PUT_HYPER(ptr
, sbe
.space_total
);
1368 if (ae
& FATTR4_SPACE_USED_MASK
) {
1370 su
= (fattr4_space_used
) DEV_BSIZE
*
1371 (fattr4_space_used
) va
.va_nblocks
;
1372 IXDR_PUT_HYPER(ptr
, su
);
1374 if (ae
& FATTR4_SYSTEM_MASK
) {
1377 /* Check the redzone boundary */
1378 if (ptr
> ptr_redzone
) {
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
) {
1452 /* Reset to directory's vfs info when encoding complete */
1453 if (vfs_different
) {
1459 /* "go back" and encode the attributes' length */
1462 (char *)attr_offset_ptr
-
1464 IXDR_PUT_U_INT32(attr_offset_ptr
, attr_length
);
1467 * If there was trouble obtaining a mapping for either
1468 * the owner or group attributes, then remove them from
1469 * bitmap4 for this entry and reset the bitmap value
1470 * in the data stream.
1472 if (owner_error
|| group_error
) {
1474 ae
&= ~FATTR4_OWNER_MASK
;
1476 ae
&= ~FATTR4_OWNER_GROUP_MASK
;
1477 IXDR_PUT_HYPER(attrmask_ptr
, ae
);
1480 /* END OF ATTRIBUTE ENCODING */
1482 lastentry_ptr
= ptr
;
1484 rddir_next_offset
= dp
->d_off
;
1488 * Check for the case that another fop_readdir() has to be done.
1489 * - no space encoding error
1490 * - no entry successfully encoded
1491 * - still more directory to read
1493 if (!no_space
&& nents
== 0 && !iseofdir
)
1497 * If no_space is set then we terminated prematurely,
1498 * rewind to the last entry and this can never be EOF.
1501 ptr
= lastentry_ptr
;
1502 eof
= FALSE
; /* ended encoded prematurely */
1504 eof
= (iseofdir
? TRUE
: FALSE
);
1508 * If we have entries, always return them, otherwise only error
1509 * if we ran out of space.
1511 if (nents
|| !no_space
) {
1512 ASSERT(ptr
!= NULL
);
1513 /* encode the BOOLEAN marking no further entries */
1514 IXDR_PUT_U_INT32(ptr
, false);
1515 /* encode the BOOLEAN signifying end of directory */
1516 IXDR_PUT_U_INT32(ptr
, eof
);
1518 resp
->data_len
= (char *)ptr
- (char *)beginning_ptr
;
1519 resp
->mblk
->b_wptr
+= resp
->data_len
;
1521 status
= NFS4ERR_TOOSMALL
;
1525 if (status
!= NFS4_OK
) {
1531 *cs
->statusp
= resp
->status
= status
;
1533 kmem_free((caddr_t
)rddir_data
, rddir_data_len
);
1536 if (owner
.utf8string_len
!= 0)
1537 kmem_free(owner
.utf8string_val
, owner
.utf8string_len
);
1538 if (group
.utf8string_len
!= 0)
1539 kmem_free(group
.utf8string_val
, group
.utf8string_len
);
1542 DTRACE_NFSV4_2(op__readdir__done
, struct compound_state
*, cs
,
1543 READDIR4res
*, resp
);