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