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