xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv_readdir.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/cred.h>
35 #include <sys/buf.h>
36 #include <sys/vfs.h>
37 #include <sys/vnode.h>
38 #include <sys/uio.h>
39 #include <sys/errno.h>
40 #include <sys/sysmacros.h>
41 #include <sys/statvfs.h>
42 #include <sys/kmem.h>
43 #include <sys/dirent.h>
44 #include <rpc/types.h>
45 #include <rpc/auth.h>
46 #include <rpc/rpcsec_gss.h>
47 #include <rpc/svc.h>
48 #include <sys/strsubr.h>
49 #include <sys/strsun.h>
50 #include <sys/sdt.h>
51 
52 #include <nfs/nfs.h>
53 #include <nfs/export.h>
54 #include <nfs/nfs4.h>
55 #include <nfs/nfs_cmd.h>
56 
57 
58 /*
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
62  *	XDR encoded dirent.
63  *
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)
70  *
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)
78  *
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.
87  */
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))
92 
93 
94 #ifdef	nextdp
95 #undef nextdp
96 #endif
97 #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
98 
99 verifier4	Readdir4verf = 0x0;
100 
101 static nfs_ftype4 vt_to_nf4[] = {
102 	0, NF4REG, NF4DIR, NF4BLK, NF4CHR, NF4LNK, NF4FIFO, 0, 0, NF4SOCK, 0
103 };
104 
105 /*
106  * RFC 7530 Section 8.3.2
107  * Supported attributes for READDIR for the root of an absent filesystem.
108  */
109 #define	ABSENT_FS_ATTRS (FATTR4_FS_LOCATIONS_MASK | FATTR4_FSID_MASK | \
110 	    FATTR4_MOUNTED_ON_FILEID_MASK | FATTR4_RDATTR_ERROR_MASK)
111 
112 int
113 nfs4_readdir_getvp(vnode_t *dvp, char *d_name, vnode_t **vpp,
114     struct exportinfo **exi, struct svc_req *req, struct compound_state *cs,
115     int expseudo, bool_t *migrated)
116 {
117 	int error;
118 	int ismntpt;
119 	fid_t fid;
120 	vnode_t *vp, *pre_tvp;
121 	nfsstat4 status;
122 	struct exportinfo *newexi, *saveexi;
123 	cred_t *scr;
124 
125 	*vpp = vp = NULL;
126 
127 	error = VOP_LOOKUP(dvp, d_name, &vp, NULL, 0, NULL, cs->cr,
128 	    NULL, NULL, NULL);
129 	if (error != 0)
130 		return (error);
131 
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");
136 		*migrated = TRUE;
137 		*vpp = vp;
138 		return (0);
139 	}
140 
141 	/* Is this object mounted upon? */
142 	ismntpt = vn_ismntpt(vp);
143 
144 	/*
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)
148 	 */
149 	if (ismntpt == 0 && dvp->v_vfsp == vp->v_vfsp && expseudo == 0) {
150 		*vpp = vp;
151 		return (0);
152 	}
153 
154 	if (ismntpt) {
155 		/*
156 		 * Something is mounted here. Traverse and manage the
157 		 * namespace
158 		 */
159 		pre_tvp = vp;
160 		VN_HOLD(pre_tvp);
161 
162 		if ((error = traverse(&vp)) != 0) {
163 			VN_RELE(vp);
164 			VN_RELE(pre_tvp);
165 			return (error);
166 		}
167 	}
168 
169 	bzero(&fid, sizeof (fid));
170 	fid.fid_len = MAXFIDSZ;
171 
172 	/*
173 	 * If VOP_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.
176 	 */
177 	error = VOP_FID(vp, &fid, NULL);
178 	if (error != 0) {
179 		if (ismntpt == 0) {
180 			VN_RELE(vp);
181 			return (error);
182 		}
183 
184 		if (error != ENOSYS && error != ENOTSUP) {
185 			VN_RELE(vp);
186 			VN_RELE(pre_tvp);
187 			return (error);
188 		}
189 		/* go back to vnode that is "under" mount */
190 		VN_RELE(vp);
191 		*vpp = pre_tvp;
192 		return (0);
193 	}
194 
195 	newexi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
196 	if (newexi == NULL) {
197 		if (ismntpt == 0) {
198 			*vpp = vp;
199 		} else {
200 			VN_RELE(vp);
201 			*vpp = pre_tvp;
202 		}
203 		return (0);
204 	}
205 
206 	if (ismntpt)
207 		VN_RELE(pre_tvp);
208 
209 	/* Save the exi and present the new one to checkauth4() */
210 	saveexi = cs->exi;
211 	cs->exi = newexi;
212 
213 	/* Get the right cred like lookup does */
214 	scr = cs->cr;
215 	cs->cr = crdup(cs->basecr);
216 
217 	status = call_checkauth4(cs, req);
218 
219 	crfree(cs->cr);
220 	cs->cr = scr;
221 	cs->exi = saveexi;
222 
223 	/* Reset what call_checkauth4() may have set */
224 	*cs->statusp = NFS4_OK;
225 
226 	if (status != NFS4_OK) {
227 		VN_RELE(vp);
228 		if (status == NFS4ERR_DELAY)
229 			status = NFS4ERR_ACCESS;
230 		return (status);
231 	}
232 	*vpp = vp;
233 	*exi = newexi;
234 
235 	return (0);
236 }
237 
238 /* This is the set of pathconf data for vfs */
239 typedef struct {
240 	uint64_t maxfilesize;
241 	uint32_t maxlink;
242 	uint32_t maxname;
243 } rfs4_pc_encode_t;
244 
245 
246 static int
247 rfs4_get_pc_encode(vnode_t *vp, rfs4_pc_encode_t *pce, bitmap4 ar, cred_t *cr)
248 {
249 	int error;
250 	ulong_t pc_val;
251 
252 	pce->maxfilesize = 0;
253 	pce->maxlink = 0;
254 	pce->maxname = 0;
255 
256 	if (ar & FATTR4_MAXFILESIZE_MASK) {
257 		/* Maximum File Size */
258 		error = VOP_PATHCONF(vp, _PC_FILESIZEBITS, &pc_val, cr, NULL);
259 		if (error)
260 			return (error);
261 
262 		/*
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.
267 		 */
268 		if (pc_val == (ulong_t)-1) {
269 			pce->maxfilesize = MAXOFF32_T;
270 		} else {
271 			if (pc_val >= (sizeof (uint64_t) * 8))
272 				pce->maxfilesize = INT64_MAX;
273 			else
274 				pce->maxfilesize = ((1LL << (pc_val - 1)) - 1);
275 		}
276 	}
277 
278 	if (ar & FATTR4_MAXLINK_MASK) {
279 		/* Maximum Link Count */
280 		error = VOP_PATHCONF(vp, _PC_LINK_MAX, &pc_val, cr, NULL);
281 		if (error)
282 			return (error);
283 
284 		pce->maxlink = pc_val;
285 	}
286 
287 	if (ar & FATTR4_MAXNAME_MASK) {
288 		/* Maximum Name Length */
289 		error = VOP_PATHCONF(vp, _PC_NAME_MAX, &pc_val, cr, NULL);
290 		if (error)
291 			return (error);
292 
293 		pce->maxname = pc_val;
294 	}
295 
296 	return (0);
297 }
298 
299 /* This is the set of statvfs data that is ready for encoding */
300 typedef struct {
301 	uint64_t space_avail;
302 	uint64_t space_free;
303 	uint64_t space_total;
304 	u_longlong_t fa;
305 	u_longlong_t ff;
306 	u_longlong_t ft;
307 } rfs4_sb_encode_t;
308 
309 static int
310 rfs4_get_sb_encode(vfs_t *vfsp, rfs4_sb_encode_t *psbe)
311 {
312 	int error;
313 	struct statvfs64 sb;
314 
315 	/* Grab the per filesystem info */
316 	error = VFS_STATVFS(vfsp, &sb);
317 	if (error != 0) {
318 		return (error);
319 	}
320 
321 	/* Calculate space available */
322 	if (sb.f_bavail != (fsblkcnt64_t)-1) {
323 		psbe->space_avail =
324 		    (fattr4_space_avail) sb.f_frsize *
325 		    (fattr4_space_avail) sb.f_bavail;
326 	} else {
327 		psbe->space_avail =
328 		    (fattr4_space_avail) sb.f_bavail;
329 	}
330 
331 	/* Calculate space free */
332 	if (sb.f_bfree != (fsblkcnt64_t)-1) {
333 		psbe->space_free =
334 		    (fattr4_space_free) sb.f_frsize *
335 		    (fattr4_space_free) sb.f_bfree;
336 	} else {
337 		psbe->space_free =
338 		    (fattr4_space_free) sb.f_bfree;
339 	}
340 
341 	/* Calculate space total */
342 	if (sb.f_blocks != (fsblkcnt64_t)-1) {
343 		psbe->space_total =
344 		    (fattr4_space_total) sb.f_frsize *
345 		    (fattr4_space_total) sb.f_blocks;
346 	} else {
347 		psbe->space_total =
348 		    (fattr4_space_total) sb.f_blocks;
349 	}
350 
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;
355 
356 	return (0);
357 }
358 
359 extern void rfs4_free_fs_locations4(fs_locations4 *);
360 
361 /*
362  * If readdir only needs to return FILEID, we can take it from the
363  * dirent struct and save doing the lookup.
364  */
365 /* ARGSUSED */
366 void
367 rfs4_op_readdir(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
368     struct compound_state *cs)
369 {
370 	READDIR4args *args = &argop->nfs_argop4_u.opreaddir;
371 	READDIR4res *resp = &resop->nfs_resop4_u.opreaddir;
372 	struct exportinfo *newexi = NULL;
373 	int error;
374 	mblk_t *mp;
375 	uint_t mpcount;
376 	int alloc_err = 0;
377 	vnode_t *dvp = cs->vp;
378 	vnode_t *vp;
379 	vattr_t va;
380 	struct dirent64 *dp;
381 	rfs4_sb_encode_t dsbe, sbe;
382 	int vfs_different;
383 	int rddir_data_len, rddir_result_size;
384 	caddr_t rddir_data;
385 	offset_t rddir_next_offset;
386 	int dircount;
387 	int no_space;
388 	int iseofdir;
389 	uint_t eof;
390 	struct iovec iov;
391 	struct uio uio;
392 	int tsize;
393 	int check_visible;
394 	struct exp_visible *visp;
395 
396 	uint32_t *ptr, *ptr_redzone;
397 	uint32_t *beginning_ptr;
398 	uint32_t *lastentry_ptr;
399 	uint32_t *attrmask_ptr;
400 	uint32_t *attr_offset_ptr;
401 	uint32_t attr_length;
402 	uint32_t rndup;
403 	uint32_t namelen;
404 	uint32_t rddirattr_error = 0;
405 	int nents;
406 	bitmap4 ar = args->attr_request & NFS4_SRV_RDDIR_SUPPORTED_ATTRS;
407 	bitmap4 ae;
408 	rfs4_pc_encode_t dpce, pce;
409 	ulong_t pc_val;
410 	uint64_t maxread;
411 	uint64_t maxwrite;
412 	uint_t true = TRUE;
413 	uint_t false = FALSE;
414 	uid_t lastuid;
415 	gid_t lastgid;
416 	int lu_set, lg_set;
417 	utf8string owner, group;
418 	int owner_error, group_error;
419 	struct sockaddr *ca;
420 	char *name = NULL;
421 	nfsstat4 status = NFS4_OK;
422 
423 	DTRACE_NFSV4_2(op__readdir__start, struct compound_state *, cs,
424 	    READDIR4args *, args);
425 
426 	lu_set = lg_set = 0;
427 	owner.utf8string_len = group.utf8string_len = 0;
428 	owner.utf8string_val = group.utf8string_val = NULL;
429 
430 	resp->mblk = NULL;
431 
432 	/* Maximum read and write size */
433 	maxread = maxwrite = rfs4_tsize(req);
434 
435 	if (dvp == NULL) {
436 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
437 		goto out;
438 	}
439 
440 	/*
441 	 * If there is an unshared filesystem mounted on this vnode,
442 	 * do not allow readdir in this directory.
443 	 */
444 	if (vn_ismntpt(dvp)) {
445 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
446 		goto out;
447 	}
448 
449 	if (dvp->v_type != VDIR) {
450 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
451 		goto out;
452 	}
453 
454 	if (args->maxcount <= RFS4_MINLEN_RDDIR4) {
455 		*cs->statusp = resp->status = NFS4ERR_TOOSMALL;
456 		goto out;
457 	}
458 
459 	/*
460 	 * If write-only attrs are requested, then fail the readdir op
461 	 */
462 	if (args->attr_request &
463 	    (FATTR4_TIME_MODIFY_SET_MASK | FATTR4_TIME_ACCESS_SET_MASK)) {
464 		*cs->statusp = resp->status = NFS4ERR_INVAL;
465 		goto out;
466 	}
467 
468 	error = VOP_ACCESS(dvp, VREAD, 0, cs->cr, NULL);
469 	if (error) {
470 		*cs->statusp = resp->status = puterrno4(error);
471 		goto out;
472 	}
473 
474 	if (args->cookieverf != Readdir4verf) {
475 		*cs->statusp = resp->status = NFS4ERR_NOT_SAME;
476 		goto out;
477 	}
478 
479 	/* Is there pseudo-fs work that is needed for this readdir? */
480 	check_visible = PSEUDO(cs->exi) ||
481 	    ! is_exported_sec(cs->nfsflavor, cs->exi) ||
482 	    cs->access & CS_ACCESS_LIMITED;
483 
484 	/* Check the requested attributes and only do the work if needed */
485 
486 	if (ar & (FATTR4_MAXFILESIZE_MASK |
487 	    FATTR4_MAXLINK_MASK |
488 	    FATTR4_MAXNAME_MASK)) {
489 		error = rfs4_get_pc_encode(cs->vp, &dpce, ar, cs->cr);
490 		if (error != 0) {
491 			*cs->statusp = resp->status = puterrno4(error);
492 			goto out;
493 		}
494 		pce = dpce;
495 	}
496 
497 	/* If there is statvfs data requested, pick it up once */
498 	if (ar &
499 	    (FATTR4_FILES_AVAIL_MASK |
500 	    FATTR4_FILES_FREE_MASK |
501 	    FATTR4_FILES_TOTAL_MASK |
502 	    FATTR4_FILES_AVAIL_MASK |
503 	    FATTR4_FILES_FREE_MASK |
504 	    FATTR4_FILES_TOTAL_MASK)) {
505 		error = rfs4_get_sb_encode(dvp->v_vfsp, &dsbe);
506 		if (error != 0) {
507 			*cs->statusp = resp->status = puterrno4(error);
508 			goto out;
509 		}
510 		sbe = dsbe;
511 	}
512 
513 	/*
514 	 * Max transfer size of the server is the absolute limite.
515 	 * If the client has decided to max out with something really
516 	 * tiny, then return toosmall.  Otherwise, move forward and
517 	 * see if a single entry can be encoded.
518 	 */
519 	tsize = rfs4_tsize(req);
520 	if (args->maxcount > tsize)
521 		args->maxcount = tsize;
522 	else if (args->maxcount < RFS4_MINLEN_RDDIR_BUF) {
523 		if (args->maxcount < RFS4_MINLEN_ENTRY4) {
524 			*cs->statusp = resp->status = NFS4ERR_TOOSMALL;
525 			goto out;
526 		}
527 	}
528 
529 	/*
530 	 * How large should the mblk be for outgoing encoding.
531 	 */
532 	if (args->maxcount < MAXBSIZE)
533 		mpcount = MAXBSIZE;
534 	else
535 		mpcount = args->maxcount;
536 
537 	/*
538 	 * mp will contain the data to be sent out in the readdir reply.
539 	 * It will be freed after the reply has been sent.
540 	 * Let's roundup the data to a BYTES_PER_XDR_UNIX multiple,
541 	 * so that the call to xdrmblk_putmblk() never fails.
542 	 */
543 	mp = allocb(RNDUP(mpcount), BPRI_MED);
544 
545 	if (mp == NULL) {
546 		/*
547 		 * The allocation of the client's requested size has
548 		 * failed.  It may be that the size is too large for
549 		 * current system utilization; step down to a "common"
550 		 * size and wait for the allocation to occur.
551 		 */
552 		if (mpcount > MAXBSIZE)
553 			args->maxcount = mpcount = MAXBSIZE;
554 		mp = allocb_wait(RNDUP(mpcount), BPRI_MED,
555 		    STR_NOSIG, &alloc_err);
556 	}
557 
558 	ASSERT(mp != NULL);
559 	ASSERT(alloc_err == 0);
560 
561 	resp->mblk = mp;
562 
563 	ptr = beginning_ptr = (uint32_t *)mp->b_datap->db_base;
564 
565 	/*
566 	 * The "redzone" at the end of the encoding buffer is used
567 	 * to deal with xdr encoding length.  Instead of checking
568 	 * each encoding of an attribute value before it is done,
569 	 * make the assumption that it will fit into the buffer and
570 	 * check occasionally.
571 	 *
572 	 * The largest block of attributes that are encoded without
573 	 * checking the redzone is 18 * BYTES_PER_XDR_UNIT (72 bytes)
574 	 * "round" to 128 as the redzone size.
575 	 */
576 	if (args->maxcount < (mpcount - 128))
577 		ptr_redzone =
578 		    (uint32_t *)(((char *)ptr) + RNDUP(args->maxcount));
579 	else
580 		ptr_redzone =
581 		    (uint32_t *)((((char *)ptr) + RNDUP(mpcount)) - 128);
582 
583 	/*
584 	 * Set the dircount; this will be used as the size for the
585 	 * readdir of the underlying filesystem.  First make sure
586 	 * that it is large enough to do a reasonable readdir (client
587 	 * may have short changed us - it is an advisory number);
588 	 * then make sure that it isn't too large.
589 	 * After all of that, if maxcount is "small" then just use
590 	 * that for the dircount number.
591 	 */
592 	dircount = (args->dircount < MAXBSIZE) ? MAXBSIZE : args->dircount;
593 	dircount = (dircount > tsize) ? tsize : dircount;
594 	if (dircount > args->maxcount)
595 		dircount = args->maxcount;
596 	if (args->maxcount <= MAXBSIZE) {
597 		if (args->maxcount < RFS4_MINLEN_RDDIR_BUF)
598 			dircount = RFS4_MINLEN_RDDIR_BUF;
599 		else
600 			dircount = args->maxcount;
601 	}
602 
603 	/* number of entries fully encoded in outgoing buffer */
604 	nents = 0;
605 
606 	/* ENCODE READDIR4res.cookieverf */
607 	IXDR_PUT_HYPER(ptr, Readdir4verf);
608 
609 	rddir_data_len = dircount;
610 	rddir_data = kmem_alloc(rddir_data_len, KM_NOSLEEP);
611 	if (rddir_data == NULL) {
612 		/* The allocation failed; downsize and wait for it this time */
613 		if (rddir_data_len > MAXBSIZE)
614 			rddir_data_len = dircount = MAXBSIZE;
615 		rddir_data = kmem_alloc(rddir_data_len, KM_SLEEP);
616 	}
617 
618 	rddir_next_offset = (offset_t)args->cookie;
619 
620 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
621 
622 readagain:
623 
624 	no_space = FALSE;
625 	iseofdir = FALSE;
626 
627 	vp = NULL;
628 
629 	/* Move on to reading the directory contents */
630 	iov.iov_base = rddir_data;
631 	iov.iov_len = rddir_data_len;
632 	uio.uio_iov = &iov;
633 	uio.uio_iovcnt = 1;
634 	uio.uio_segflg = UIO_SYSSPACE;
635 	uio.uio_extflg = UIO_COPY_CACHED;
636 	uio.uio_loffset = rddir_next_offset;
637 	uio.uio_resid = rddir_data_len;
638 
639 	(void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
640 
641 	error = VOP_READDIR(dvp, &uio, cs->cr, &iseofdir, NULL, 0);
642 
643 	VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
644 
645 	if (error) {
646 		kmem_free((caddr_t)rddir_data, rddir_data_len);
647 		freeb(resp->mblk);
648 		resp->mblk = NULL;
649 		resp->data_len = 0;
650 		*cs->statusp = resp->status = puterrno4(error);
651 		goto out;
652 	}
653 
654 
655 	rddir_result_size = rddir_data_len - uio.uio_resid;
656 
657 	/* No data were read. Check if we reached the end of the directory. */
658 	if (rddir_result_size == 0) {
659 		/* encode the BOOLEAN marking no further entries */
660 		IXDR_PUT_U_INT32(ptr, false);
661 		/* encode the BOOLEAN signifying end of directory */
662 		IXDR_PUT_U_INT32(ptr, iseofdir ? true : false);
663 		resp->data_len = (char *)ptr - (char *)beginning_ptr;
664 		resp->mblk->b_wptr += resp->data_len;
665 		kmem_free((caddr_t)rddir_data, rddir_data_len);
666 		*cs->statusp = resp->status = NFS4_OK;
667 		goto out;
668 	}
669 
670 	lastentry_ptr = ptr;
671 	no_space = 0;
672 	for (dp = (struct dirent64 *)rddir_data;
673 	    !no_space && rddir_result_size > 0; dp = nextdp(dp)) {
674 		bool_t fs_migrated = FALSE;
675 
676 		/* reset visp */
677 		visp = NULL;
678 
679 		if (vp) {
680 			VN_RELE(vp);
681 			vp = NULL;
682 		}
683 
684 		if (newexi)
685 			newexi = NULL;
686 
687 		rddir_result_size -= dp->d_reclen;
688 
689 		/* skip "." and ".." entries */
690 		if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) {
691 			rddir_next_offset = dp->d_off;
692 			continue;
693 		}
694 
695 		if (check_visible &&
696 		    !nfs_visible_inode(cs->exi, dp->d_ino, &visp)) {
697 			rddir_next_offset = dp->d_off;
698 			continue;
699 		}
700 
701 		/*
702 		 * Only if the client requested attributes...
703 		 * If the VOP_LOOKUP fails ENOENT, then skip this entry
704 		 * for the readdir response.  If there was another error,
705 		 * then set the rddirattr_error and the error will be
706 		 * encoded later in the "attributes" section.
707 		 */
708 		ae = ar;
709 		if (ar == 0)
710 			goto reencode_attrs;
711 
712 		error = nfs4_readdir_getvp(dvp, dp->d_name,
713 		    &vp, &newexi, req, cs,
714 		    visp != NULL ? visp->vis_exported : 0, &fs_migrated);
715 		if (error == ENOENT) {
716 			rddir_next_offset = dp->d_off;
717 			continue;
718 		}
719 
720 		rddirattr_error = error;
721 
722 		/*
723 		 * The vp obtained from above may be from a
724 		 * different filesystem mount and the vfs-like
725 		 * attributes should be obtained from that
726 		 * different vfs; only do this if appropriate.
727 		 */
728 		if (vp &&
729 		    (vfs_different = (dvp->v_vfsp != vp->v_vfsp))) {
730 			if (ae & (FATTR4_FILES_AVAIL_MASK |
731 			    FATTR4_FILES_FREE_MASK |
732 			    FATTR4_FILES_TOTAL_MASK |
733 			    FATTR4_FILES_AVAIL_MASK |
734 			    FATTR4_FILES_FREE_MASK |
735 			    FATTR4_FILES_TOTAL_MASK)) {
736 				error = rfs4_get_sb_encode(dvp->v_vfsp, &sbe);
737 				if (error != 0) {
738 					/* Remove attrs from encode */
739 					ae &= ~(FATTR4_FILES_AVAIL_MASK |
740 					    FATTR4_FILES_FREE_MASK |
741 					    FATTR4_FILES_TOTAL_MASK |
742 					    FATTR4_FILES_AVAIL_MASK |
743 					    FATTR4_FILES_FREE_MASK |
744 					    FATTR4_FILES_TOTAL_MASK);
745 					rddirattr_error = error;
746 				}
747 			}
748 			if (ae & (FATTR4_MAXFILESIZE_MASK |
749 			    FATTR4_MAXLINK_MASK |
750 			    FATTR4_MAXNAME_MASK)) {
751 				error = rfs4_get_pc_encode(cs->vp,
752 				    &pce, ae, cs->cr);
753 				if (error != 0) {
754 					ae &= ~(FATTR4_MAXFILESIZE_MASK |
755 					    FATTR4_MAXLINK_MASK |
756 					    FATTR4_MAXNAME_MASK);
757 					rddirattr_error = error;
758 				}
759 			}
760 		}
761 
762 reencode_attrs:
763 		/* encode the BOOLEAN for the existence of the next entry */
764 		IXDR_PUT_U_INT32(ptr, true);
765 		/* encode the COOKIE for the entry */
766 		IXDR_PUT_U_HYPER(ptr, dp->d_off);
767 
768 		name = nfscmd_convname(ca, cs->exi, dp->d_name,
769 		    NFSCMD_CONV_OUTBOUND, MAXPATHLEN + 1);
770 
771 		if (name == NULL) {
772 			rddir_next_offset = dp->d_off;
773 			continue;
774 		}
775 		/* Calculate the dirent name length */
776 		namelen = strlen(name);
777 
778 		rndup = RNDUP(namelen) / BYTES_PER_XDR_UNIT;
779 
780 		/* room for LENGTH + string ? */
781 		if ((ptr + (1 + rndup)) > ptr_redzone) {
782 			no_space = TRUE;
783 			continue;
784 		}
785 
786 		/* encode the LENGTH of the name */
787 		IXDR_PUT_U_INT32(ptr, namelen);
788 		/* encode the RNDUP FILL first */
789 		ptr[rndup - 1] = 0;
790 		/* encode the NAME of the entry */
791 		bcopy(name, (char *)ptr, namelen);
792 		/* now bump the ptr after... */
793 		ptr += rndup;
794 
795 		if (name != dp->d_name)
796 			kmem_free(name, MAXPATHLEN + 1);
797 
798 		/*
799 		 * Keep checking on the dircount to see if we have
800 		 * reached the limit; from the RFC, dircount is to be
801 		 * the XDR encoded limit of the cookie plus name.
802 		 * So the count is the name, XDR_UNIT of length for
803 		 * that name and 2 * XDR_UNIT bytes of cookie;
804 		 * However, use the regular DIRENT64 to match most
805 		 * client's APIs.
806 		 */
807 		dircount -= DIRENT64_RECLEN(namelen);
808 		if (nents != 0 && dircount < 0) {
809 			no_space = TRUE;
810 			continue;
811 		}
812 
813 		/*
814 		 * Attributes requested?
815 		 * Gather up the attribute info and the previous VOP_LOOKUP()
816 		 * succeeded; if an error occurs on the VOP_GETATTR() then
817 		 * return just the error (again if it is requested).
818 		 * Note that the previous VOP_LOOKUP() could have failed
819 		 * itself which leaves this code without anything for
820 		 * a VOP_GETATTR().
821 		 * Also note that the readdir_attr_error is left in the
822 		 * encoding mask if requested and so is the mounted_on_fileid.
823 		 */
824 		if (ae != 0) {
825 			if (!vp) {
826 				ae = ar & (FATTR4_RDATTR_ERROR_MASK |
827 				    FATTR4_MOUNTED_ON_FILEID_MASK);
828 			} else if (fs_migrated) {
829 				/*
830 				 * RFC 7530 Section 8.3.2
831 				 * Restrict to actually available attributes in
832 				 * case of an absent filesystem.
833 				 */
834 				ae &= ABSENT_FS_ATTRS;
835 
836 				if (!(ae & FATTR4_FS_LOCATIONS_MASK))
837 					rddirattr_error = NFS4ERR_MOVED;
838 			} else {
839 				va.va_mask = AT_ALL;
840 				rddirattr_error =
841 				    VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
842 				if (rddirattr_error) {
843 					ae = ae & (FATTR4_RDATTR_ERROR_MASK |
844 					    FATTR4_MOUNTED_ON_FILEID_MASK);
845 				} else {
846 					/*
847 					 * We may lie about the object
848 					 * type for a referral
849 					 */
850 					if (vn_is_nfs_reparse(vp, cs->cr) &&
851 					    client_is_downrev(req))
852 						va.va_type = VLNK;
853 				}
854 			}
855 
856 			/* RFC 7530 Section 8.3.2 and 16.24.4 */
857 			if (rddirattr_error != 0 &&
858 			    !(ae & FATTR4_RDATTR_ERROR_MASK)) {
859 				status = puterrno4(rddirattr_error);
860 				goto out_free;
861 			}
862 		}
863 
864 		/* START OF ATTRIBUTE ENCODING */
865 
866 		/* encode the LENGTH of the BITMAP4 array */
867 		IXDR_PUT_U_INT32(ptr, 2);
868 		/* encode the BITMAP4 */
869 		attrmask_ptr = ptr;
870 		IXDR_PUT_HYPER(ptr, ae);
871 		attr_offset_ptr = ptr;
872 		/* encode the default LENGTH of the attributes for entry */
873 		IXDR_PUT_U_INT32(ptr, 0);
874 
875 		if (ptr > ptr_redzone) {
876 			no_space = TRUE;
877 			continue;
878 		}
879 
880 		/* Check if any of the first 32 attributes are being encoded */
881 		if (ae & 0xffffffff00000000) {
882 			/*
883 			 * Redzone check is done at the end of this section.
884 			 * This particular section will encode a maximum of
885 			 * 18 * BYTES_PER_XDR_UNIT of data
886 			 */
887 			if (ae &
888 			    (FATTR4_SUPPORTED_ATTRS_MASK |
889 			    FATTR4_TYPE_MASK |
890 			    FATTR4_FH_EXPIRE_TYPE_MASK |
891 			    FATTR4_CHANGE_MASK |
892 			    FATTR4_SIZE_MASK |
893 			    FATTR4_LINK_SUPPORT_MASK |
894 			    FATTR4_SYMLINK_SUPPORT_MASK |
895 			    FATTR4_NAMED_ATTR_MASK |
896 			    FATTR4_FSID_MASK |
897 			    FATTR4_UNIQUE_HANDLES_MASK |
898 			    FATTR4_LEASE_TIME_MASK |
899 			    FATTR4_RDATTR_ERROR_MASK)) {
900 
901 				if (ae & FATTR4_SUPPORTED_ATTRS_MASK) {
902 					IXDR_PUT_INT32(ptr, 2);
903 					IXDR_PUT_HYPER(ptr,
904 					    rfs4_supported_attrs);
905 				}
906 				if (ae & FATTR4_TYPE_MASK) {
907 					uint_t ftype = vt_to_nf4[va.va_type];
908 					if (dvp->v_flag & V_XATTRDIR) {
909 						if (va.va_type == VDIR)
910 							ftype = NF4ATTRDIR;
911 						else
912 							ftype = NF4NAMEDATTR;
913 					}
914 					IXDR_PUT_U_INT32(ptr, ftype);
915 				}
916 				if (ae & FATTR4_FH_EXPIRE_TYPE_MASK) {
917 					uint_t expire_type = FH4_PERSISTENT;
918 					IXDR_PUT_U_INT32(ptr, expire_type);
919 				}
920 				if (ae & FATTR4_CHANGE_MASK) {
921 					u_longlong_t change;
922 					NFS4_SET_FATTR4_CHANGE(change,
923 					    va.va_ctime);
924 					if (visp != NULL) {
925 						u_longlong_t visch;
926 						NFS4_SET_FATTR4_CHANGE(visch,
927 						    visp->vis_change);
928 						if (visch > change)
929 							change = visch;
930 					}
931 					IXDR_PUT_HYPER(ptr, change);
932 				}
933 				if (ae & FATTR4_SIZE_MASK) {
934 					u_longlong_t size = va.va_size;
935 					IXDR_PUT_HYPER(ptr, size);
936 				}
937 				if (ae & FATTR4_LINK_SUPPORT_MASK) {
938 					IXDR_PUT_U_INT32(ptr, true);
939 				}
940 				if (ae & FATTR4_SYMLINK_SUPPORT_MASK) {
941 					IXDR_PUT_U_INT32(ptr, true);
942 				}
943 				if (ae & FATTR4_NAMED_ATTR_MASK) {
944 					uint_t isit;
945 					pc_val = FALSE;
946 					int sattr_error;
947 
948 					if (!(vp->v_vfsp->vfs_flag &
949 					    VFS_XATTR)) {
950 						isit = FALSE;
951 					} else {
952 						sattr_error = VOP_PATHCONF(vp,
953 						    _PC_SATTR_EXISTS,
954 						    &pc_val, cs->cr, NULL);
955 						if (sattr_error || pc_val == 0)
956 							(void) VOP_PATHCONF(vp,
957 							    _PC_XATTR_EXISTS,
958 							    &pc_val,
959 							    cs->cr, NULL);
960 					}
961 					isit = (pc_val ? TRUE : FALSE);
962 					IXDR_PUT_U_INT32(ptr, isit);
963 				}
964 				if (ae & FATTR4_FSID_MASK) {
965 					u_longlong_t major, minor;
966 					struct exportinfo *exi;
967 
968 					exi = newexi ? newexi : cs->exi;
969 					if (fs_migrated) {
970 						major = 1;
971 						minor = 0;
972 					} else if (exi->exi_volatile_dev) {
973 						int *pmaj = (int *)&major;
974 
975 						pmaj[0] = exi->exi_fsid.val[0];
976 						pmaj[1] = exi->exi_fsid.val[1];
977 						minor = 0;
978 					} else {
979 						major = getmajor(va.va_fsid);
980 						minor = getminor(va.va_fsid);
981 					}
982 					IXDR_PUT_HYPER(ptr, major);
983 					IXDR_PUT_HYPER(ptr, minor);
984 				}
985 				if (ae & FATTR4_UNIQUE_HANDLES_MASK) {
986 					IXDR_PUT_U_INT32(ptr, false);
987 				}
988 				if (ae & FATTR4_LEASE_TIME_MASK) {
989 					uint_t lt = rfs4_lease_time;
990 					IXDR_PUT_U_INT32(ptr, lt);
991 				}
992 				if (ae & FATTR4_RDATTR_ERROR_MASK) {
993 					rddirattr_error =
994 					    (rddirattr_error == 0 ?
995 					    0 : puterrno4(rddirattr_error));
996 					IXDR_PUT_U_INT32(ptr, rddirattr_error);
997 				}
998 
999 				/* Check the redzone boundary */
1000 				if (ptr > ptr_redzone) {
1001 					no_space = TRUE;
1002 					break;
1003 				}
1004 			}
1005 			/*
1006 			 * Redzone check is done at the end of this section.
1007 			 * This particular section will encode a maximum of
1008 			 * 4 * BYTES_PER_XDR_UNIT of data.
1009 			 * NOTE: that if ACLs are supported that the
1010 			 * redzone calculations will need to change.
1011 			 */
1012 			if (ae &
1013 			    (FATTR4_ACL_MASK |
1014 			    FATTR4_ACLSUPPORT_MASK |
1015 			    FATTR4_ARCHIVE_MASK |
1016 			    FATTR4_CANSETTIME_MASK |
1017 			    FATTR4_CASE_INSENSITIVE_MASK |
1018 			    FATTR4_CASE_PRESERVING_MASK |
1019 			    FATTR4_CHOWN_RESTRICTED_MASK)) {
1020 
1021 				if (ae & FATTR4_ACL_MASK) {
1022 					ASSERT(0);
1023 				}
1024 				if (ae & FATTR4_ACLSUPPORT_MASK) {
1025 					ASSERT(0);
1026 				}
1027 				if (ae & FATTR4_ARCHIVE_MASK) {
1028 					ASSERT(0);
1029 				}
1030 				if (ae & FATTR4_CANSETTIME_MASK) {
1031 					IXDR_PUT_U_INT32(ptr, true);
1032 				}
1033 				if (ae & FATTR4_CASE_INSENSITIVE_MASK) {
1034 					IXDR_PUT_U_INT32(ptr, false);
1035 				}
1036 				if (ae & FATTR4_CASE_PRESERVING_MASK) {
1037 					IXDR_PUT_U_INT32(ptr, true);
1038 				}
1039 				if (ae & FATTR4_CHOWN_RESTRICTED_MASK) {
1040 					uint_t isit;
1041 					pc_val = FALSE;
1042 					(void) VOP_PATHCONF(vp,
1043 					    _PC_CHOWN_RESTRICTED,
1044 					    &pc_val, cs->cr, NULL);
1045 					isit = (pc_val ? TRUE : FALSE);
1046 					IXDR_PUT_U_INT32(ptr, isit);
1047 				}
1048 				/* Check the redzone boundary */
1049 				if (ptr > ptr_redzone) {
1050 					no_space = TRUE;
1051 					break;
1052 				}
1053 			}
1054 			/*
1055 			 * Redzone check is done before the filehandle
1056 			 * is encoded.
1057 			 */
1058 			if (ae &
1059 			    (FATTR4_FILEHANDLE_MASK |
1060 			    FATTR4_FILEID_MASK)) {
1061 
1062 				if (ae & FATTR4_FILEHANDLE_MASK) {
1063 					struct {
1064 						uint_t len;
1065 						char *val;
1066 						char fh[NFS_FH4_LEN];
1067 					} fh;
1068 					fh.len = 0;
1069 					fh.val = fh.fh;
1070 					(void) makefh4((nfs_fh4 *)&fh, vp,
1071 					    (newexi ? newexi : cs->exi));
1072 
1073 					if (dvp->v_flag & V_XATTRDIR)
1074 						set_fh4_flag((nfs_fh4 *)&fh,
1075 						    FH4_NAMEDATTR);
1076 
1077 					if (!xdr_inline_encode_nfs_fh4(
1078 					    &ptr, ptr_redzone,
1079 					    (nfs_fh4_fmt_t *)fh.val)) {
1080 						no_space = TRUE;
1081 						break;
1082 					}
1083 				}
1084 				if (ae & FATTR4_FILEID_MASK) {
1085 					IXDR_PUT_HYPER(ptr, va.va_nodeid);
1086 				}
1087 				/* Check the redzone boundary */
1088 				if (ptr > ptr_redzone) {
1089 					no_space = TRUE;
1090 					break;
1091 				}
1092 			}
1093 
1094 			/*
1095 			 * Redzone check is done at the end of this section.
1096 			 * This particular section will encode a maximum of
1097 			 * 6 * BYTES_PER_XDR_UNIT of data.
1098 			 */
1099 			if (ae &
1100 			    (FATTR4_FILES_AVAIL_MASK |
1101 			    FATTR4_FILES_FREE_MASK |
1102 			    FATTR4_FILES_TOTAL_MASK)) {
1103 
1104 				if (ae & FATTR4_FILES_AVAIL_MASK) {
1105 					IXDR_PUT_HYPER(ptr, sbe.fa);
1106 				}
1107 				if (ae & FATTR4_FILES_FREE_MASK) {
1108 					IXDR_PUT_HYPER(ptr, sbe.ff);
1109 				}
1110 				if (ae & FATTR4_FILES_TOTAL_MASK) {
1111 					IXDR_PUT_HYPER(ptr, sbe.ft);
1112 				}
1113 
1114 				/* Check the redzone boundary */
1115 				if (ptr > ptr_redzone) {
1116 					no_space = TRUE;
1117 					break;
1118 				}
1119 			}
1120 
1121 			/*
1122 			 * Handle fs_locations separately.
1123 			 * This can be quite slow for referrals. Usually
1124 			 * clients don't use this attribute in readdir.
1125 			 */
1126 			if (ae & FATTR4_FS_LOCATIONS_MASK) {
1127 				fs_locations4 *p;
1128 				fs_locations4 fs;
1129 				bool_t pushed;
1130 				XDR xdr;
1131 
1132 				(void) memset(&fs, 0, sizeof (fs_locations4));
1133 
1134 				ASSERT(vp);
1135 				p = fetch_referral(vp, cs->cr);
1136 				if (p != NULL) {
1137 					fs = *p;
1138 					kmem_free(p, sizeof (fs_locations4));
1139 				}
1140 
1141 				ASSERT(ptr_redzone >= ptr);
1142 				ASSERT((uintptr_t)ptr_redzone - (uintptr_t)ptr
1143 				    <= UINT_MAX);
1144 				xdrmem_create(&xdr, (caddr_t)ptr,
1145 				    (uintptr_t)ptr_redzone - (uintptr_t)ptr,
1146 				    XDR_ENCODE);
1147 
1148 				pushed = xdr_fattr4_fs_locations(&xdr, &fs);
1149 				if (pushed)
1150 					ptr = (uint32_t *)((char *)ptr +
1151 					    xdr_getpos(&xdr));
1152 
1153 				xdr_destroy(&xdr);
1154 				rfs4_free_fs_locations4(&fs);
1155 
1156 				if (!pushed || ptr > ptr_redzone) {
1157 					no_space = TRUE;
1158 					break;
1159 				}
1160 			}
1161 
1162 			if (ae &
1163 			    (FATTR4_HIDDEN_MASK |
1164 			    FATTR4_HOMOGENEOUS_MASK |
1165 			    FATTR4_MAXFILESIZE_MASK |
1166 			    FATTR4_MAXLINK_MASK |
1167 			    FATTR4_MAXNAME_MASK |
1168 			    FATTR4_MAXREAD_MASK |
1169 			    FATTR4_MAXWRITE_MASK)) {
1170 
1171 				if (ae & FATTR4_HIDDEN_MASK) {
1172 					ASSERT(0);
1173 				}
1174 				if (ae & FATTR4_HOMOGENEOUS_MASK) {
1175 					IXDR_PUT_U_INT32(ptr, true);
1176 				}
1177 				if (ae & FATTR4_MAXFILESIZE_MASK) {
1178 					IXDR_PUT_HYPER(ptr, pce.maxfilesize);
1179 				}
1180 				if (ae & FATTR4_MAXLINK_MASK) {
1181 					IXDR_PUT_U_INT32(ptr, pce.maxlink);
1182 				}
1183 				if (ae & FATTR4_MAXNAME_MASK) {
1184 					IXDR_PUT_U_INT32(ptr, pce.maxname);
1185 				}
1186 				if (ae & FATTR4_MAXREAD_MASK) {
1187 					IXDR_PUT_HYPER(ptr, maxread);
1188 				}
1189 				if (ae & FATTR4_MAXWRITE_MASK) {
1190 					IXDR_PUT_HYPER(ptr, maxwrite);
1191 				}
1192 				/* Check the redzone boundary */
1193 				if (ptr > ptr_redzone) {
1194 					no_space = TRUE;
1195 					break;
1196 				}
1197 			}
1198 		}
1199 
1200 		if (ae & 0x00000000ffffffff) {
1201 			/*
1202 			 * Redzone check is done at the end of this section.
1203 			 * This particular section will encode a maximum of
1204 			 * 3 * BYTES_PER_XDR_UNIT of data.
1205 			 */
1206 			if (ae &
1207 			    (FATTR4_MIMETYPE_MASK |
1208 			    FATTR4_MODE_MASK |
1209 			    FATTR4_NO_TRUNC_MASK |
1210 			    FATTR4_NUMLINKS_MASK)) {
1211 
1212 				if (ae & FATTR4_MIMETYPE_MASK) {
1213 					ASSERT(0);
1214 				}
1215 				if (ae & FATTR4_MODE_MASK) {
1216 					uint_t m = va.va_mode;
1217 					IXDR_PUT_U_INT32(ptr, m);
1218 				}
1219 				if (ae & FATTR4_NO_TRUNC_MASK) {
1220 					IXDR_PUT_U_INT32(ptr, true);
1221 				}
1222 				if (ae & FATTR4_NUMLINKS_MASK) {
1223 					IXDR_PUT_U_INT32(ptr, va.va_nlink);
1224 				}
1225 				/* Check the redzone boundary */
1226 				if (ptr > ptr_redzone) {
1227 					no_space = TRUE;
1228 					break;
1229 				}
1230 			}
1231 			/*
1232 			 * Redzone check is done before the encoding of the
1233 			 * owner string since the length is indeterminate.
1234 			 */
1235 			if (ae & FATTR4_OWNER_MASK) {
1236 				if (!lu_set) {
1237 					owner_error = nfs_idmap_uid_str(
1238 					    va.va_uid, &owner, TRUE);
1239 					if (!owner_error) {
1240 						lu_set = TRUE;
1241 						lastuid = va.va_uid;
1242 					}
1243 				} else if (va.va_uid != lastuid) {
1244 					if (owner.utf8string_len != 0) {
1245 						kmem_free(owner.utf8string_val,
1246 						    owner.utf8string_len);
1247 						owner.utf8string_len = 0;
1248 						owner.utf8string_val = NULL;
1249 					}
1250 					owner_error = nfs_idmap_uid_str(
1251 					    va.va_uid, &owner, TRUE);
1252 					if (!owner_error) {
1253 						lastuid = va.va_uid;
1254 					} else {
1255 						lu_set = FALSE;
1256 					}
1257 				}
1258 				if (!owner_error) {
1259 					if ((ptr +
1260 					    (owner.utf8string_len /
1261 					    BYTES_PER_XDR_UNIT)
1262 					    + 2) > ptr_redzone) {
1263 						no_space = TRUE;
1264 						break;
1265 					}
1266 					/* encode the LENGTH of owner string */
1267 					IXDR_PUT_U_INT32(ptr,
1268 					    owner.utf8string_len);
1269 					/* encode the RNDUP FILL first */
1270 					rndup = RNDUP(owner.utf8string_len) /
1271 					    BYTES_PER_XDR_UNIT;
1272 					ptr[rndup - 1] = 0;
1273 					/* encode the OWNER */
1274 					bcopy(owner.utf8string_val, ptr,
1275 					    owner.utf8string_len);
1276 					ptr += rndup;
1277 				}
1278 			}
1279 			/*
1280 			 * Redzone check is done before the encoding of the
1281 			 * group string since the length is indeterminate.
1282 			 */
1283 			if (ae & FATTR4_OWNER_GROUP_MASK) {
1284 				if (!lg_set) {
1285 					group_error =
1286 					    nfs_idmap_gid_str(va.va_gid,
1287 					    &group, TRUE);
1288 					if (!group_error) {
1289 						lg_set = TRUE;
1290 						lastgid = va.va_gid;
1291 					}
1292 				} else if (va.va_gid != lastgid) {
1293 					if (group.utf8string_len != 0) {
1294 						kmem_free(
1295 						    group.utf8string_val,
1296 						    group.utf8string_len);
1297 						group.utf8string_len = 0;
1298 						group.utf8string_val = NULL;
1299 					}
1300 					group_error =
1301 					    nfs_idmap_gid_str(va.va_gid,
1302 					    &group, TRUE);
1303 					if (!group_error)
1304 						lastgid = va.va_gid;
1305 					else
1306 						lg_set = FALSE;
1307 				}
1308 				if (!group_error) {
1309 					if ((ptr +
1310 					    (group.utf8string_len /
1311 					    BYTES_PER_XDR_UNIT)
1312 					    + 2) > ptr_redzone) {
1313 						no_space = TRUE;
1314 						break;
1315 					}
1316 					/* encode the LENGTH of owner string */
1317 					IXDR_PUT_U_INT32(ptr,
1318 					    group.utf8string_len);
1319 					/* encode the RNDUP FILL first */
1320 					rndup = RNDUP(group.utf8string_len) /
1321 					    BYTES_PER_XDR_UNIT;
1322 					ptr[rndup - 1] = 0;
1323 					/* encode the OWNER */
1324 					bcopy(group.utf8string_val, ptr,
1325 					    group.utf8string_len);
1326 					ptr += rndup;
1327 				}
1328 			}
1329 			if (ae &
1330 			    (FATTR4_QUOTA_AVAIL_HARD_MASK |
1331 			    FATTR4_QUOTA_AVAIL_SOFT_MASK |
1332 			    FATTR4_QUOTA_USED_MASK)) {
1333 				if (ae & FATTR4_QUOTA_AVAIL_HARD_MASK) {
1334 					ASSERT(0);
1335 				}
1336 				if (ae & FATTR4_QUOTA_AVAIL_SOFT_MASK) {
1337 					ASSERT(0);
1338 				}
1339 				if (ae & FATTR4_QUOTA_USED_MASK) {
1340 					ASSERT(0);
1341 				}
1342 			}
1343 			/*
1344 			 * Redzone check is done at the end of this section.
1345 			 * This particular section will encode a maximum of
1346 			 * 10 * BYTES_PER_XDR_UNIT of data.
1347 			 */
1348 			if (ae &
1349 			    (FATTR4_RAWDEV_MASK |
1350 			    FATTR4_SPACE_AVAIL_MASK |
1351 			    FATTR4_SPACE_FREE_MASK |
1352 			    FATTR4_SPACE_TOTAL_MASK |
1353 			    FATTR4_SPACE_USED_MASK |
1354 			    FATTR4_SYSTEM_MASK)) {
1355 
1356 				if (ae & FATTR4_RAWDEV_MASK) {
1357 					fattr4_rawdev rd;
1358 					rd.specdata1 =
1359 					    (uint32)getmajor(va.va_rdev);
1360 					rd.specdata2 =
1361 					    (uint32)getminor(va.va_rdev);
1362 					IXDR_PUT_U_INT32(ptr, rd.specdata1);
1363 					IXDR_PUT_U_INT32(ptr, rd.specdata2);
1364 				}
1365 				if (ae & FATTR4_SPACE_AVAIL_MASK) {
1366 					IXDR_PUT_HYPER(ptr, sbe.space_avail);
1367 				}
1368 				if (ae & FATTR4_SPACE_FREE_MASK) {
1369 					IXDR_PUT_HYPER(ptr, sbe.space_free);
1370 				}
1371 				if (ae & FATTR4_SPACE_TOTAL_MASK) {
1372 					IXDR_PUT_HYPER(ptr, sbe.space_total);
1373 				}
1374 				if (ae & FATTR4_SPACE_USED_MASK) {
1375 					u_longlong_t su;
1376 					su = (fattr4_space_used) DEV_BSIZE *
1377 					    (fattr4_space_used) va.va_nblocks;
1378 					IXDR_PUT_HYPER(ptr, su);
1379 				}
1380 				if (ae & FATTR4_SYSTEM_MASK) {
1381 					ASSERT(0);
1382 				}
1383 				/* Check the redzone boundary */
1384 				if (ptr > ptr_redzone) {
1385 					no_space = TRUE;
1386 					break;
1387 				}
1388 			}
1389 			/*
1390 			 * Redzone check is done at the end of this section.
1391 			 * This particular section will encode a maximum of
1392 			 * 14 * BYTES_PER_XDR_UNIT of data.
1393 			 */
1394 			if (ae &
1395 			    (FATTR4_TIME_ACCESS_MASK |
1396 			    FATTR4_TIME_ACCESS_SET_MASK |
1397 			    FATTR4_TIME_BACKUP_MASK |
1398 			    FATTR4_TIME_CREATE_MASK |
1399 			    FATTR4_TIME_DELTA_MASK |
1400 			    FATTR4_TIME_METADATA_MASK |
1401 			    FATTR4_TIME_MODIFY_MASK |
1402 			    FATTR4_TIME_MODIFY_SET_MASK |
1403 			    FATTR4_MOUNTED_ON_FILEID_MASK)) {
1404 
1405 				if (ae & FATTR4_TIME_ACCESS_MASK) {
1406 					u_longlong_t sec =
1407 					    (u_longlong_t)va.va_atime.tv_sec;
1408 					uint_t nsec =
1409 					    (uint_t)va.va_atime.tv_nsec;
1410 					IXDR_PUT_HYPER(ptr, sec);
1411 					IXDR_PUT_INT32(ptr, nsec);
1412 				}
1413 				if (ae & FATTR4_TIME_ACCESS_SET_MASK) {
1414 					ASSERT(0);
1415 				}
1416 				if (ae & FATTR4_TIME_BACKUP_MASK) {
1417 					ASSERT(0);
1418 				}
1419 				if (ae & FATTR4_TIME_CREATE_MASK) {
1420 					ASSERT(0);
1421 				}
1422 				if (ae & FATTR4_TIME_DELTA_MASK) {
1423 					u_longlong_t sec = 0;
1424 					uint_t nsec = 1000;
1425 					IXDR_PUT_HYPER(ptr, sec);
1426 					IXDR_PUT_INT32(ptr, nsec);
1427 				}
1428 				if (ae & FATTR4_TIME_METADATA_MASK) {
1429 					u_longlong_t sec =
1430 					    (u_longlong_t)va.va_ctime.tv_sec;
1431 					uint_t nsec =
1432 					    (uint_t)va.va_ctime.tv_nsec;
1433 					IXDR_PUT_HYPER(ptr, sec);
1434 					IXDR_PUT_INT32(ptr, nsec);
1435 				}
1436 				if (ae & FATTR4_TIME_MODIFY_MASK) {
1437 					u_longlong_t sec =
1438 					    (u_longlong_t)va.va_mtime.tv_sec;
1439 					uint_t nsec =
1440 					    (uint_t)va.va_mtime.tv_nsec;
1441 					IXDR_PUT_HYPER(ptr, sec);
1442 					IXDR_PUT_INT32(ptr, nsec);
1443 				}
1444 				if (ae & FATTR4_TIME_MODIFY_SET_MASK) {
1445 					ASSERT(0);
1446 				}
1447 				if (ae & FATTR4_MOUNTED_ON_FILEID_MASK) {
1448 					IXDR_PUT_HYPER(ptr, dp->d_ino);
1449 				}
1450 				/* Check the redzone boundary */
1451 				if (ptr > ptr_redzone) {
1452 					no_space = TRUE;
1453 					break;
1454 				}
1455 			}
1456 		}
1457 
1458 		/* Reset to directory's vfs info when encoding complete */
1459 		if (vfs_different) {
1460 			dsbe = sbe;
1461 			dpce = pce;
1462 			vfs_different = 0;
1463 		}
1464 
1465 		/* "go back" and encode the attributes' length */
1466 		attr_length =
1467 		    (char *)ptr -
1468 		    (char *)attr_offset_ptr -
1469 		    BYTES_PER_XDR_UNIT;
1470 		IXDR_PUT_U_INT32(attr_offset_ptr, attr_length);
1471 
1472 		/*
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.
1477 		 */
1478 		if (owner_error || group_error) {
1479 			if (owner_error)
1480 				ae &= ~FATTR4_OWNER_MASK;
1481 			if (group_error)
1482 				ae &= ~FATTR4_OWNER_GROUP_MASK;
1483 			IXDR_PUT_HYPER(attrmask_ptr, ae);
1484 		}
1485 
1486 		/* END OF ATTRIBUTE ENCODING */
1487 
1488 		lastentry_ptr = ptr;
1489 		nents++;
1490 		rddir_next_offset = dp->d_off;
1491 	}
1492 
1493 	/*
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
1498 	 */
1499 	if (!no_space && nents == 0 && !iseofdir)
1500 		goto readagain;
1501 
1502 	/*
1503 	 * If no_space is set then we terminated prematurely,
1504 	 * rewind to the last entry and this can never be EOF.
1505 	 */
1506 	if (no_space) {
1507 		ptr = lastentry_ptr;
1508 		eof = FALSE; /* ended encoded prematurely */
1509 	} else {
1510 		eof = (iseofdir ? TRUE : FALSE);
1511 	}
1512 
1513 	/*
1514 	 * If we have entries, always return them, otherwise only error
1515 	 * if we ran out of space.
1516 	 */
1517 	if (nents || !no_space) {
1518 		ASSERT(ptr != NULL);
1519 		/* encode the BOOLEAN marking no further entries */
1520 		IXDR_PUT_U_INT32(ptr, false);
1521 		/* encode the BOOLEAN signifying end of directory */
1522 		IXDR_PUT_U_INT32(ptr, eof);
1523 
1524 		resp->data_len = (char *)ptr - (char *)beginning_ptr;
1525 		resp->mblk->b_wptr += resp->data_len;
1526 	} else {
1527 		status = NFS4ERR_TOOSMALL;
1528 	}
1529 
1530 out_free:
1531 	if (status != NFS4_OK) {
1532 		freeb(mp);
1533 		resp->mblk = NULL;
1534 		resp->data_len = 0;
1535 	}
1536 
1537 	*cs->statusp = resp->status = status;
1538 
1539 	kmem_free((caddr_t)rddir_data, rddir_data_len);
1540 	if (vp)
1541 		VN_RELE(vp);
1542 	if (owner.utf8string_len != 0)
1543 		kmem_free(owner.utf8string_val,	owner.utf8string_len);
1544 	if (group.utf8string_len != 0)
1545 		kmem_free(group.utf8string_val, group.utf8string_len);
1546 
1547 out:
1548 	DTRACE_NFSV4_2(op__readdir__done, struct compound_state *, cs,
1549 	    READDIR4res *, resp);
1550 }
1551