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