xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv_readdir.c (revision 66e150d7d3c0cb2de3c45c74612784ffd3e73de6)
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 #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 					int sattr_error;
878 
879 					if (!(vp->v_vfsp->vfs_flag &
880 					    VFS_XATTR)) {
881 						isit = FALSE;
882 					} else {
883 						sattr_error = VOP_PATHCONF(vp,
884 						    _PC_SATTR_EXISTS,
885 						    &pc_val, cs->cr, NULL);
886 						if (sattr_error || pc_val == 0)
887 							(void) VOP_PATHCONF(vp,
888 							    _PC_XATTR_EXISTS,
889 							    &pc_val,
890 							    cs->cr, NULL);
891 					}
892 					isit = (pc_val ? TRUE : FALSE);
893 					IXDR_PUT_U_INT32(ptr, isit);
894 				}
895 				if (ae & FATTR4_FSID_MASK) {
896 					u_longlong_t major, minor;
897 					struct exportinfo *exi;
898 
899 					exi = newexi ? newexi : cs->exi;
900 					if (exi->exi_volatile_dev) {
901 						int *pmaj = (int *)&major;
902 
903 						pmaj[0] = exi->exi_fsid.val[0];
904 						pmaj[1] = exi->exi_fsid.val[1];
905 						minor = 0;
906 					} else {
907 						major = getmajor(va.va_fsid);
908 						minor = getminor(va.va_fsid);
909 					}
910 					IXDR_PUT_HYPER(ptr, major);
911 					IXDR_PUT_HYPER(ptr, minor);
912 				}
913 				if (ae & FATTR4_UNIQUE_HANDLES_MASK) {
914 					IXDR_PUT_U_INT32(ptr, false);
915 				}
916 				if (ae & FATTR4_LEASE_TIME_MASK) {
917 					uint_t lt = rfs4_lease_time;
918 					IXDR_PUT_U_INT32(ptr, lt);
919 				}
920 				if (ae & FATTR4_RDATTR_ERROR_MASK) {
921 					rddirattr_error =
922 						(rddirattr_error == 0 ?
923 						0 : puterrno4(rddirattr_error));
924 					IXDR_PUT_U_INT32(ptr, rddirattr_error);
925 				}
926 
927 				/* Check the redzone boundary */
928 				if (ptr > ptr_redzone) {
929 					if (nents || IS_MIN_ATTR_MASK(ar)) {
930 						no_space = TRUE;
931 						continue;
932 					}
933 					MINIMIZE_ATTR_MASK(ar);
934 					ae = ar;
935 					ptr = lastentry_ptr;
936 					goto reencode_attrs;
937 				}
938 			}
939 			/*
940 			 * Redzone check is done at the end of this section.
941 			 * This particular section will encode a maximum of
942 			 * 4 * BYTES_PER_XDR_UNIT of data.
943 			 * NOTE: that if ACLs are supported that the
944 			 * redzone calculations will need to change.
945 			 */
946 			if (ae &
947 			    (FATTR4_ACL_MASK |
948 			    FATTR4_ACLSUPPORT_MASK |
949 			    FATTR4_ARCHIVE_MASK |
950 			    FATTR4_CANSETTIME_MASK |
951 			    FATTR4_CASE_INSENSITIVE_MASK |
952 			    FATTR4_CASE_PRESERVING_MASK |
953 			    FATTR4_CHOWN_RESTRICTED_MASK)) {
954 
955 				if (ae & FATTR4_ACL_MASK) {
956 					ASSERT(0);
957 				}
958 				if (ae & FATTR4_ACLSUPPORT_MASK) {
959 					ASSERT(0);
960 				}
961 				if (ae & FATTR4_ARCHIVE_MASK) {
962 					ASSERT(0);
963 				}
964 				if (ae & FATTR4_CANSETTIME_MASK) {
965 					IXDR_PUT_U_INT32(ptr, true);
966 				}
967 				if (ae & FATTR4_CASE_INSENSITIVE_MASK) {
968 					IXDR_PUT_U_INT32(ptr, false);
969 				}
970 				if (ae & FATTR4_CASE_PRESERVING_MASK) {
971 					IXDR_PUT_U_INT32(ptr, true);
972 				}
973 				if (ae & FATTR4_CHOWN_RESTRICTED_MASK) {
974 					uint_t isit;
975 					pc_val = FALSE;
976 					(void) VOP_PATHCONF(vp,
977 							_PC_CHOWN_RESTRICTED,
978 							&pc_val, cs->cr, NULL);
979 					isit = (pc_val ? TRUE : FALSE);
980 					IXDR_PUT_U_INT32(ptr, isit);
981 				}
982 				/* Check the redzone boundary */
983 				if (ptr > ptr_redzone) {
984 					if (nents || IS_MIN_ATTR_MASK(ar)) {
985 						no_space = TRUE;
986 						continue;
987 					}
988 					MINIMIZE_ATTR_MASK(ar);
989 					ae = ar;
990 					ptr = lastentry_ptr;
991 					goto reencode_attrs;
992 				}
993 			}
994 			/*
995 			 * Redzone check is done before the filehandle
996 			 * is encoded.
997 			 */
998 			if (ae &
999 			    (FATTR4_FILEHANDLE_MASK |
1000 			    FATTR4_FILEID_MASK)) {
1001 
1002 				if (ae & FATTR4_FILEHANDLE_MASK) {
1003 					struct {
1004 						uint_t len;
1005 						char *val;
1006 						char fh[NFS_FH4_LEN];
1007 					} fh;
1008 					fh.len = 0;
1009 					fh.val = fh.fh;
1010 					(void) makefh4((nfs_fh4 *)&fh, vp,
1011 					    (newexi ? newexi : cs->exi));
1012 
1013 					if (!xdr_inline_encode_nfs_fh4(
1014 					    &ptr, ptr_redzone,
1015 					    (nfs_fh4_fmt_t *)fh.val)) {
1016 						if (nents ||
1017 						    IS_MIN_ATTR_MASK(ar)) {
1018 							no_space = TRUE;
1019 							continue;
1020 						}
1021 						MINIMIZE_ATTR_MASK(ar);
1022 						ae = ar;
1023 						ptr = lastentry_ptr;
1024 						goto reencode_attrs;
1025 					}
1026 				}
1027 				if (ae & FATTR4_FILEID_MASK) {
1028 					IXDR_PUT_HYPER(ptr, va.va_nodeid);
1029 				}
1030 				/* Check the redzone boundary */
1031 				if (ptr > ptr_redzone) {
1032 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1033 						no_space = TRUE;
1034 						continue;
1035 					}
1036 					MINIMIZE_ATTR_MASK(ar);
1037 					ae = ar;
1038 					ptr = lastentry_ptr;
1039 					goto reencode_attrs;
1040 				}
1041 			}
1042 			/*
1043 			 * Redzone check is done at the end of this section.
1044 			 * This particular section will encode a maximum of
1045 			 * 15 * BYTES_PER_XDR_UNIT of data.
1046 			 */
1047 			if (ae &
1048 			    (FATTR4_FILES_AVAIL_MASK |
1049 			    FATTR4_FILES_FREE_MASK |
1050 			    FATTR4_FILES_TOTAL_MASK |
1051 			    FATTR4_FS_LOCATIONS_MASK |
1052 			    FATTR4_HIDDEN_MASK |
1053 			    FATTR4_HOMOGENEOUS_MASK |
1054 			    FATTR4_MAXFILESIZE_MASK |
1055 			    FATTR4_MAXLINK_MASK |
1056 			    FATTR4_MAXNAME_MASK |
1057 			    FATTR4_MAXREAD_MASK |
1058 			    FATTR4_MAXWRITE_MASK)) {
1059 
1060 				if (ae & FATTR4_FILES_AVAIL_MASK) {
1061 					IXDR_PUT_HYPER(ptr, sbe.fa);
1062 				}
1063 				if (ae & FATTR4_FILES_FREE_MASK) {
1064 					IXDR_PUT_HYPER(ptr, sbe.ff);
1065 				}
1066 				if (ae & FATTR4_FILES_TOTAL_MASK) {
1067 					IXDR_PUT_HYPER(ptr, sbe.ft);
1068 				}
1069 				if (ae & FATTR4_FS_LOCATIONS_MASK) {
1070 					ASSERT(0);
1071 				}
1072 				if (ae & FATTR4_HIDDEN_MASK) {
1073 					ASSERT(0);
1074 				}
1075 				if (ae & FATTR4_HOMOGENEOUS_MASK) {
1076 					IXDR_PUT_U_INT32(ptr, true);
1077 				}
1078 				if (ae & FATTR4_MAXFILESIZE_MASK) {
1079 					IXDR_PUT_HYPER(ptr, pce.maxfilesize);
1080 				}
1081 				if (ae & FATTR4_MAXLINK_MASK) {
1082 					IXDR_PUT_U_INT32(ptr, pce.maxlink);
1083 				}
1084 				if (ae & FATTR4_MAXNAME_MASK) {
1085 					IXDR_PUT_U_INT32(ptr, pce.maxname);
1086 				}
1087 				if (ae & FATTR4_MAXREAD_MASK) {
1088 					IXDR_PUT_HYPER(ptr, maxread);
1089 				}
1090 				if (ae & FATTR4_MAXWRITE_MASK) {
1091 					IXDR_PUT_HYPER(ptr, maxwrite);
1092 				}
1093 				/* Check the redzone boundary */
1094 				if (ptr > ptr_redzone) {
1095 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1096 						no_space = TRUE;
1097 						continue;
1098 					}
1099 					MINIMIZE_ATTR_MASK(ar);
1100 					ae = ar;
1101 					ptr = lastentry_ptr;
1102 					goto reencode_attrs;
1103 				}
1104 			}
1105 		}
1106 		if (ae & 0x00000000ffffffff) {
1107 			/*
1108 			 * Redzone check is done at the end of this section.
1109 			 * This particular section will encode a maximum of
1110 			 * 3 * BYTES_PER_XDR_UNIT of data.
1111 			 */
1112 			if (ae &
1113 			    (FATTR4_MIMETYPE_MASK |
1114 			    FATTR4_MODE_MASK |
1115 			    FATTR4_NO_TRUNC_MASK |
1116 			    FATTR4_NUMLINKS_MASK)) {
1117 
1118 				if (ae & FATTR4_MIMETYPE_MASK) {
1119 					ASSERT(0);
1120 				}
1121 				if (ae & FATTR4_MODE_MASK) {
1122 					uint_t m = va.va_mode;
1123 					IXDR_PUT_U_INT32(ptr, m);
1124 				}
1125 				if (ae & FATTR4_NO_TRUNC_MASK) {
1126 					IXDR_PUT_U_INT32(ptr, true);
1127 				}
1128 				if (ae & FATTR4_NUMLINKS_MASK) {
1129 					IXDR_PUT_U_INT32(ptr, va.va_nlink);
1130 				}
1131 				/* Check the redzone boundary */
1132 				if (ptr > ptr_redzone) {
1133 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1134 						no_space = TRUE;
1135 						continue;
1136 					}
1137 					MINIMIZE_ATTR_MASK(ar);
1138 					ae = ar;
1139 					ptr = lastentry_ptr;
1140 					goto reencode_attrs;
1141 				}
1142 			}
1143 			/*
1144 			 * Redzone check is done before the encoding of the
1145 			 * owner string since the length is indeterminate.
1146 			 */
1147 			if (ae & FATTR4_OWNER_MASK) {
1148 				if (!lu_set) {
1149 				    owner_error = nfs_idmap_uid_str(va.va_uid,
1150 								&owner, TRUE);
1151 				    if (!owner_error) {
1152 					    lu_set = TRUE;
1153 					    lastuid = va.va_uid;
1154 				    }
1155 				} else {
1156 				    if (va.va_uid != lastuid) {
1157 					if (owner.utf8string_len != 0) {
1158 					    kmem_free(owner.utf8string_val,
1159 						owner.utf8string_len);
1160 						owner.utf8string_len = 0;
1161 						owner.utf8string_val = NULL;
1162 					}
1163 					owner_error = nfs_idmap_uid_str(
1164 							va.va_uid,
1165 							&owner, TRUE);
1166 					if (!owner_error) {
1167 						lastuid = va.va_uid;
1168 					} else {
1169 						lu_set = FALSE;
1170 					}
1171 				    }
1172 				}
1173 				if (!owner_error) {
1174 					if ((ptr +
1175 					    (owner.utf8string_len /
1176 					    BYTES_PER_XDR_UNIT)
1177 					    + 2) > ptr_redzone) {
1178 						if (nents ||
1179 						    IS_MIN_ATTR_MASK(ar)) {
1180 							no_space = TRUE;
1181 							continue;
1182 						}
1183 						MINIMIZE_ATTR_MASK(ar);
1184 						ae = ar;
1185 						ptr = lastentry_ptr;
1186 						goto reencode_attrs;
1187 					}
1188 					/* encode the LENGTH of owner string */
1189 					IXDR_PUT_U_INT32(ptr,
1190 							owner.utf8string_len);
1191 					/* encode the RNDUP FILL first */
1192 					rndup = RNDUP(owner.utf8string_len) /
1193 						BYTES_PER_XDR_UNIT;
1194 					ptr[rndup - 1] = 0;
1195 					/* encode the OWNER */
1196 					bcopy(owner.utf8string_val, ptr,
1197 						owner.utf8string_len);
1198 					ptr += rndup;
1199 				}
1200 			}
1201 			/*
1202 			 * Redzone check is done before the encoding of the
1203 			 * group string since the length is indeterminate.
1204 			 */
1205 			if (ae & FATTR4_OWNER_GROUP_MASK) {
1206 				if (!lg_set) {
1207 				    group_error =
1208 					    nfs_idmap_gid_str(va.va_gid,
1209 							&group, TRUE);
1210 				    if (!group_error) {
1211 					    lg_set = TRUE;
1212 					    lastgid = va.va_gid;
1213 				    }
1214 				} else {
1215 				    if (va.va_gid != lastgid) {
1216 					if (group.utf8string_len != 0) {
1217 					    kmem_free(group.utf8string_val,
1218 						group.utf8string_len);
1219 					    group.utf8string_len = 0;
1220 					    group.utf8string_val = NULL;
1221 					}
1222 					group_error =
1223 						nfs_idmap_gid_str(va.va_gid,
1224 								&group, TRUE);
1225 					if (!group_error)
1226 						lastgid = va.va_gid;
1227 					else
1228 						lg_set = FALSE;
1229 				    }
1230 				}
1231 				if (!group_error) {
1232 					if ((ptr +
1233 					    (group.utf8string_len /
1234 					    BYTES_PER_XDR_UNIT)
1235 					    + 2) > ptr_redzone) {
1236 						if (nents ||
1237 						    IS_MIN_ATTR_MASK(ar)) {
1238 							no_space = TRUE;
1239 							continue;
1240 						}
1241 						MINIMIZE_ATTR_MASK(ar);
1242 						ae = ar;
1243 						ptr = lastentry_ptr;
1244 						goto reencode_attrs;
1245 					}
1246 					/* encode the LENGTH of owner string */
1247 					IXDR_PUT_U_INT32(ptr,
1248 							group.utf8string_len);
1249 					/* encode the RNDUP FILL first */
1250 					rndup = RNDUP(group.utf8string_len) /
1251 						BYTES_PER_XDR_UNIT;
1252 					ptr[rndup - 1] = 0;
1253 					/* encode the OWNER */
1254 					bcopy(group.utf8string_val, ptr,
1255 						group.utf8string_len);
1256 					ptr += rndup;
1257 				}
1258 			}
1259 			if (ae &
1260 			    (FATTR4_QUOTA_AVAIL_HARD_MASK |
1261 			    FATTR4_QUOTA_AVAIL_SOFT_MASK |
1262 			    FATTR4_QUOTA_USED_MASK)) {
1263 				if (ae & FATTR4_QUOTA_AVAIL_HARD_MASK) {
1264 					ASSERT(0);
1265 				}
1266 				if (ae & FATTR4_QUOTA_AVAIL_SOFT_MASK) {
1267 					ASSERT(0);
1268 				}
1269 				if (ae & FATTR4_QUOTA_USED_MASK) {
1270 					ASSERT(0);
1271 				}
1272 			}
1273 			/*
1274 			 * Redzone check is done at the end of this section.
1275 			 * This particular section will encode a maximum of
1276 			 * 10 * BYTES_PER_XDR_UNIT of data.
1277 			 */
1278 			if (ae &
1279 			    (FATTR4_RAWDEV_MASK |
1280 			    FATTR4_SPACE_AVAIL_MASK |
1281 			    FATTR4_SPACE_FREE_MASK |
1282 			    FATTR4_SPACE_TOTAL_MASK |
1283 			    FATTR4_SPACE_USED_MASK |
1284 			    FATTR4_SYSTEM_MASK)) {
1285 
1286 				if (ae & FATTR4_RAWDEV_MASK) {
1287 					fattr4_rawdev rd;
1288 					rd.specdata1 =
1289 						(uint32)getmajor(va.va_rdev);
1290 					rd.specdata2 =
1291 						(uint32)getminor(va.va_rdev);
1292 					IXDR_PUT_U_INT32(ptr, rd.specdata1);
1293 					IXDR_PUT_U_INT32(ptr, rd.specdata2);
1294 				}
1295 				if (ae & FATTR4_SPACE_AVAIL_MASK) {
1296 					IXDR_PUT_HYPER(ptr, sbe.space_avail);
1297 				}
1298 				if (ae & FATTR4_SPACE_FREE_MASK) {
1299 					IXDR_PUT_HYPER(ptr, sbe.space_free);
1300 				}
1301 				if (ae & FATTR4_SPACE_TOTAL_MASK) {
1302 					IXDR_PUT_HYPER(ptr, sbe.space_total);
1303 				}
1304 				if (ae & FATTR4_SPACE_USED_MASK) {
1305 					u_longlong_t su;
1306 					su = (fattr4_space_used) DEV_BSIZE *
1307 					    (fattr4_space_used) va.va_nblocks;
1308 					IXDR_PUT_HYPER(ptr, su);
1309 				}
1310 				if (ae & FATTR4_SYSTEM_MASK) {
1311 					ASSERT(0);
1312 				}
1313 				/* Check the redzone boundary */
1314 				if (ptr > ptr_redzone) {
1315 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1316 						no_space = TRUE;
1317 						continue;
1318 					}
1319 					MINIMIZE_ATTR_MASK(ar);
1320 					ae = ar;
1321 					ptr = lastentry_ptr;
1322 					goto reencode_attrs;
1323 				}
1324 			}
1325 			/*
1326 			 * Redzone check is done at the end of this section.
1327 			 * This particular section will encode a maximum of
1328 			 * 14 * BYTES_PER_XDR_UNIT of data.
1329 			 */
1330 			if (ae &
1331 			    (FATTR4_TIME_ACCESS_MASK |
1332 			    FATTR4_TIME_ACCESS_SET_MASK |
1333 			    FATTR4_TIME_BACKUP_MASK |
1334 			    FATTR4_TIME_CREATE_MASK |
1335 			    FATTR4_TIME_DELTA_MASK |
1336 			    FATTR4_TIME_METADATA_MASK |
1337 			    FATTR4_TIME_MODIFY_MASK |
1338 			    FATTR4_TIME_MODIFY_SET_MASK |
1339 			    FATTR4_MOUNTED_ON_FILEID_MASK)) {
1340 
1341 				if (ae & FATTR4_TIME_ACCESS_MASK) {
1342 					u_longlong_t sec =
1343 					    (u_longlong_t)va.va_atime.tv_sec;
1344 					uint_t nsec =
1345 						(uint_t)va.va_atime.tv_nsec;
1346 					IXDR_PUT_HYPER(ptr, sec);
1347 					IXDR_PUT_INT32(ptr, nsec);
1348 				}
1349 				if (ae & FATTR4_TIME_ACCESS_SET_MASK) {
1350 					ASSERT(0);
1351 				}
1352 				if (ae & FATTR4_TIME_BACKUP_MASK) {
1353 					ASSERT(0);
1354 				}
1355 				if (ae & FATTR4_TIME_CREATE_MASK) {
1356 					ASSERT(0);
1357 				}
1358 				if (ae & FATTR4_TIME_DELTA_MASK) {
1359 					u_longlong_t sec = 0;
1360 					uint_t nsec = 1000;
1361 					IXDR_PUT_HYPER(ptr, sec);
1362 					IXDR_PUT_INT32(ptr, nsec);
1363 				}
1364 				if (ae & FATTR4_TIME_METADATA_MASK) {
1365 					u_longlong_t sec =
1366 					    (u_longlong_t)va.va_ctime.tv_sec;
1367 					uint_t nsec =
1368 						(uint_t)va.va_ctime.tv_nsec;
1369 					IXDR_PUT_HYPER(ptr, sec);
1370 					IXDR_PUT_INT32(ptr, nsec);
1371 				}
1372 				if (ae & FATTR4_TIME_MODIFY_MASK) {
1373 					u_longlong_t sec =
1374 					    (u_longlong_t)va.va_mtime.tv_sec;
1375 					uint_t nsec =
1376 						(uint_t)va.va_mtime.tv_nsec;
1377 					IXDR_PUT_HYPER(ptr, sec);
1378 					IXDR_PUT_INT32(ptr, nsec);
1379 				}
1380 				if (ae & FATTR4_TIME_MODIFY_SET_MASK) {
1381 					ASSERT(0);
1382 				}
1383 				if (ae & FATTR4_MOUNTED_ON_FILEID_MASK) {
1384 					IXDR_PUT_HYPER(ptr, dp->d_ino);
1385 				}
1386 				/* Check the redzone boundary */
1387 				if (ptr > ptr_redzone) {
1388 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1389 						no_space = TRUE;
1390 						continue;
1391 					}
1392 					MINIMIZE_ATTR_MASK(ar);
1393 					ae = ar;
1394 					ptr = lastentry_ptr;
1395 					goto reencode_attrs;
1396 				}
1397 			}
1398 		}
1399 
1400 		/* Reset to directory's vfs info when encoding complete */
1401 		if (vfs_different) {
1402 			dsbe = sbe;
1403 			dpce = pce;
1404 			vfs_different = 0;
1405 		}
1406 
1407 		/* "go back" and encode the attributes' length */
1408 		attr_length =
1409 			(char *)ptr -
1410 			(char *)attr_offset_ptr -
1411 			BYTES_PER_XDR_UNIT;
1412 		IXDR_PUT_U_INT32(attr_offset_ptr, attr_length);
1413 
1414 		/*
1415 		 * If there was trouble obtaining a mapping for either
1416 		 * the owner or group attributes, then remove them from
1417 		 * bitmap4 for this entry and reset the bitmap value
1418 		 * in the data stream.
1419 		 */
1420 		if (owner_error || group_error) {
1421 			if (owner_error)
1422 				ae &= ~FATTR4_OWNER_MASK;
1423 			if (group_error)
1424 				ae &= ~FATTR4_OWNER_GROUP_MASK;
1425 			IXDR_PUT_HYPER(attrmask_ptr, ae);
1426 		}
1427 
1428 		/* END OF ATTRIBUTE ENCODING */
1429 
1430 		lastentry_ptr = ptr;
1431 		nents++;
1432 		rddir_next_offset = dp->d_off;
1433 	}
1434 
1435 	/*
1436 	 * Check for the case that another VOP_READDIR() has to be done.
1437 	 * - no space encoding error
1438 	 * - no entry successfully encoded
1439 	 * - still more directory to read
1440 	 */
1441 	if (!no_space && nents == 0 && !iseofdir)
1442 		goto readagain;
1443 
1444 	*cs->statusp = resp->status = NFS4_OK;
1445 
1446 	/*
1447 	 * If no_space is set then we terminated prematurely,
1448 	 * rewind to the last entry and this can never be EOF.
1449 	 */
1450 	if (no_space) {
1451 		ptr = lastentry_ptr;
1452 		eof = FALSE; /* ended encoded prematurely */
1453 	} else {
1454 		eof = (iseofdir ? TRUE : FALSE);
1455 	}
1456 
1457 	/*
1458 	 * If we have entries, always return them, otherwise only error
1459 	 * if we ran out of space.
1460 	 */
1461 	if (nents || !no_space) {
1462 		ASSERT(ptr != NULL);
1463 		/* encode the BOOLEAN marking no further entries */
1464 		IXDR_PUT_U_INT32(ptr, false);
1465 		/* encode the BOOLEAN signifying end of directory */
1466 		IXDR_PUT_U_INT32(ptr, eof);
1467 
1468 		resp->data_len = (char *)ptr - (char *)beginning_ptr;
1469 		resp->mblk->b_wptr += resp->data_len;
1470 	} else {
1471 		freeb(mp);
1472 		resp->mblk = NULL;
1473 		resp->data_len = 0;
1474 		*cs->statusp = resp->status = NFS4ERR_TOOSMALL;
1475 	}
1476 
1477 	kmem_free((caddr_t)rddir_data, rddir_data_len);
1478 	if (vp)
1479 		VN_RELE(vp);
1480 	if (owner.utf8string_len != 0)
1481 		kmem_free(owner.utf8string_val,	owner.utf8string_len);
1482 	if (group.utf8string_len != 0)
1483 		kmem_free(group.utf8string_val, group.utf8string_len);
1484 
1485 out:
1486 	DTRACE_NFSV4_2(op__readdir__done, struct compound_state *, cs,
1487 	    READDIR4res *, resp);
1488 }
1489