xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv_readdir.c (revision e067707b65c63939ade45e268f2b0dff6f65f75d)
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 ((ptr +
1004 					    (fh.len / BYTES_PER_XDR_UNIT) + 1)
1005 					    > ptr_redzone) {
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 					IXDR_PUT_U_INT32(ptr, fh.len);
1017 					/* encode the RNDUP FILL first */
1018 					rndup = RNDUP(fh.len) /
1019 						BYTES_PER_XDR_UNIT;
1020 					ptr[rndup - 1] = 0;
1021 					bcopy(fh.fh, ptr, fh.len);
1022 					ptr += rndup;
1023 				}
1024 				if (ae & FATTR4_FILEID_MASK) {
1025 					IXDR_PUT_HYPER(ptr, va.va_nodeid);
1026 				}
1027 				/* Check the redzone boundary */
1028 				if (ptr > ptr_redzone) {
1029 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1030 						no_space = TRUE;
1031 						continue;
1032 					}
1033 					MINIMIZE_ATTR_MASK(ar);
1034 					ae = ar;
1035 					ptr = lastentry_ptr;
1036 					goto reencode_attrs;
1037 				}
1038 			}
1039 			/*
1040 			 * Redzone check is done at the end of this section.
1041 			 * This particular section will encode a maximum of
1042 			 * 15 * BYTES_PER_XDR_UNIT of data.
1043 			 */
1044 			if (ae &
1045 			    (FATTR4_FILES_AVAIL_MASK |
1046 			    FATTR4_FILES_FREE_MASK |
1047 			    FATTR4_FILES_TOTAL_MASK |
1048 			    FATTR4_FS_LOCATIONS_MASK |
1049 			    FATTR4_HIDDEN_MASK |
1050 			    FATTR4_HOMOGENEOUS_MASK |
1051 			    FATTR4_MAXFILESIZE_MASK |
1052 			    FATTR4_MAXLINK_MASK |
1053 			    FATTR4_MAXNAME_MASK |
1054 			    FATTR4_MAXREAD_MASK |
1055 			    FATTR4_MAXWRITE_MASK)) {
1056 
1057 				if (ae & FATTR4_FILES_AVAIL_MASK) {
1058 					IXDR_PUT_HYPER(ptr, sbe.fa);
1059 				}
1060 				if (ae & FATTR4_FILES_FREE_MASK) {
1061 					IXDR_PUT_HYPER(ptr, sbe.ff);
1062 				}
1063 				if (ae & FATTR4_FILES_TOTAL_MASK) {
1064 					IXDR_PUT_HYPER(ptr, sbe.ft);
1065 				}
1066 				if (ae & FATTR4_FS_LOCATIONS_MASK) {
1067 					ASSERT(0);
1068 				}
1069 				if (ae & FATTR4_HIDDEN_MASK) {
1070 					ASSERT(0);
1071 				}
1072 				if (ae & FATTR4_HOMOGENEOUS_MASK) {
1073 					IXDR_PUT_U_INT32(ptr, true);
1074 				}
1075 				if (ae & FATTR4_MAXFILESIZE_MASK) {
1076 					IXDR_PUT_HYPER(ptr, pce.maxfilesize);
1077 				}
1078 				if (ae & FATTR4_MAXLINK_MASK) {
1079 					IXDR_PUT_U_INT32(ptr, pce.maxlink);
1080 				}
1081 				if (ae & FATTR4_MAXNAME_MASK) {
1082 					IXDR_PUT_U_INT32(ptr, pce.maxname);
1083 				}
1084 				if (ae & FATTR4_MAXREAD_MASK) {
1085 					IXDR_PUT_HYPER(ptr, maxread);
1086 				}
1087 				if (ae & FATTR4_MAXWRITE_MASK) {
1088 					IXDR_PUT_HYPER(ptr, maxwrite);
1089 				}
1090 				/* Check the redzone boundary */
1091 				if (ptr > ptr_redzone) {
1092 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1093 						no_space = TRUE;
1094 						continue;
1095 					}
1096 					MINIMIZE_ATTR_MASK(ar);
1097 					ae = ar;
1098 					ptr = lastentry_ptr;
1099 					goto reencode_attrs;
1100 				}
1101 			}
1102 		}
1103 		if (ae & 0x00000000ffffffff) {
1104 			/*
1105 			 * Redzone check is done at the end of this section.
1106 			 * This particular section will encode a maximum of
1107 			 * 3 * BYTES_PER_XDR_UNIT of data.
1108 			 */
1109 			if (ae &
1110 			    (FATTR4_MIMETYPE_MASK |
1111 			    FATTR4_MODE_MASK |
1112 			    FATTR4_NO_TRUNC_MASK |
1113 			    FATTR4_NUMLINKS_MASK)) {
1114 
1115 				if (ae & FATTR4_MIMETYPE_MASK) {
1116 					ASSERT(0);
1117 				}
1118 				if (ae & FATTR4_MODE_MASK) {
1119 					uint_t m = va.va_mode;
1120 					IXDR_PUT_U_INT32(ptr, m);
1121 				}
1122 				if (ae & FATTR4_NO_TRUNC_MASK) {
1123 					IXDR_PUT_U_INT32(ptr, true);
1124 				}
1125 				if (ae & FATTR4_NUMLINKS_MASK) {
1126 					IXDR_PUT_U_INT32(ptr, va.va_nlink);
1127 				}
1128 				/* Check the redzone boundary */
1129 				if (ptr > ptr_redzone) {
1130 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1131 						no_space = TRUE;
1132 						continue;
1133 					}
1134 					MINIMIZE_ATTR_MASK(ar);
1135 					ae = ar;
1136 					ptr = lastentry_ptr;
1137 					goto reencode_attrs;
1138 				}
1139 			}
1140 			/*
1141 			 * Redzone check is done before the encoding of the
1142 			 * owner string since the length is indeterminate.
1143 			 */
1144 			if (ae & FATTR4_OWNER_MASK) {
1145 				if (!lu_set) {
1146 				    owner_error = nfs_idmap_uid_str(va.va_uid,
1147 								&owner, TRUE);
1148 				    if (!owner_error) {
1149 					    lu_set = TRUE;
1150 					    lastuid = va.va_uid;
1151 				    }
1152 				} else {
1153 				    if (va.va_uid != lastuid) {
1154 					if (owner.utf8string_len != 0) {
1155 					    kmem_free(owner.utf8string_val,
1156 						owner.utf8string_len);
1157 						owner.utf8string_len = 0;
1158 						owner.utf8string_val = NULL;
1159 					}
1160 					owner_error = nfs_idmap_uid_str(
1161 							va.va_uid,
1162 							&owner, TRUE);
1163 					if (!owner_error) {
1164 						lastuid = va.va_uid;
1165 					} else {
1166 						lu_set = FALSE;
1167 					}
1168 				    }
1169 				}
1170 				if (!owner_error) {
1171 					if ((ptr +
1172 					    (owner.utf8string_len /
1173 					    BYTES_PER_XDR_UNIT)
1174 					    + 2) > ptr_redzone) {
1175 						if (nents ||
1176 						    IS_MIN_ATTR_MASK(ar)) {
1177 							no_space = TRUE;
1178 							continue;
1179 						}
1180 						MINIMIZE_ATTR_MASK(ar);
1181 						ae = ar;
1182 						ptr = lastentry_ptr;
1183 						goto reencode_attrs;
1184 					}
1185 					/* encode the LENGTH of owner string */
1186 					IXDR_PUT_U_INT32(ptr,
1187 							owner.utf8string_len);
1188 					/* encode the RNDUP FILL first */
1189 					rndup = RNDUP(owner.utf8string_len) /
1190 						BYTES_PER_XDR_UNIT;
1191 					ptr[rndup - 1] = 0;
1192 					/* encode the OWNER */
1193 					bcopy(owner.utf8string_val, ptr,
1194 						owner.utf8string_len);
1195 					ptr += rndup;
1196 				}
1197 			}
1198 			/*
1199 			 * Redzone check is done before the encoding of the
1200 			 * group string since the length is indeterminate.
1201 			 */
1202 			if (ae & FATTR4_OWNER_GROUP_MASK) {
1203 				if (!lg_set) {
1204 				    group_error =
1205 					    nfs_idmap_gid_str(va.va_gid,
1206 							&group, TRUE);
1207 				    if (!group_error) {
1208 					    lg_set = TRUE;
1209 					    lastgid = va.va_gid;
1210 				    }
1211 				} else {
1212 				    if (va.va_gid != lastgid) {
1213 					if (group.utf8string_len != 0) {
1214 					    kmem_free(group.utf8string_val,
1215 						group.utf8string_len);
1216 					    group.utf8string_len = 0;
1217 					    group.utf8string_val = NULL;
1218 					}
1219 					group_error =
1220 						nfs_idmap_gid_str(va.va_gid,
1221 								&group, TRUE);
1222 					if (!group_error)
1223 						lastgid = va.va_gid;
1224 					else
1225 						lg_set = FALSE;
1226 				    }
1227 				}
1228 				if (!group_error) {
1229 					if ((ptr +
1230 					    (group.utf8string_len /
1231 					    BYTES_PER_XDR_UNIT)
1232 					    + 2) > ptr_redzone) {
1233 						if (nents ||
1234 						    IS_MIN_ATTR_MASK(ar)) {
1235 							no_space = TRUE;
1236 							continue;
1237 						}
1238 						MINIMIZE_ATTR_MASK(ar);
1239 						ae = ar;
1240 						ptr = lastentry_ptr;
1241 						goto reencode_attrs;
1242 					}
1243 					/* encode the LENGTH of owner string */
1244 					IXDR_PUT_U_INT32(ptr,
1245 							group.utf8string_len);
1246 					/* encode the RNDUP FILL first */
1247 					rndup = RNDUP(group.utf8string_len) /
1248 						BYTES_PER_XDR_UNIT;
1249 					ptr[rndup - 1] = 0;
1250 					/* encode the OWNER */
1251 					bcopy(group.utf8string_val, ptr,
1252 						group.utf8string_len);
1253 					ptr += rndup;
1254 				}
1255 			}
1256 			if (ae &
1257 			    (FATTR4_QUOTA_AVAIL_HARD_MASK |
1258 			    FATTR4_QUOTA_AVAIL_SOFT_MASK |
1259 			    FATTR4_QUOTA_USED_MASK)) {
1260 				if (ae & FATTR4_QUOTA_AVAIL_HARD_MASK) {
1261 					ASSERT(0);
1262 				}
1263 				if (ae & FATTR4_QUOTA_AVAIL_SOFT_MASK) {
1264 					ASSERT(0);
1265 				}
1266 				if (ae & FATTR4_QUOTA_USED_MASK) {
1267 					ASSERT(0);
1268 				}
1269 			}
1270 			/*
1271 			 * Redzone check is done at the end of this section.
1272 			 * This particular section will encode a maximum of
1273 			 * 10 * BYTES_PER_XDR_UNIT of data.
1274 			 */
1275 			if (ae &
1276 			    (FATTR4_RAWDEV_MASK |
1277 			    FATTR4_SPACE_AVAIL_MASK |
1278 			    FATTR4_SPACE_FREE_MASK |
1279 			    FATTR4_SPACE_TOTAL_MASK |
1280 			    FATTR4_SPACE_USED_MASK |
1281 			    FATTR4_SYSTEM_MASK)) {
1282 
1283 				if (ae & FATTR4_RAWDEV_MASK) {
1284 					fattr4_rawdev rd;
1285 					rd.specdata1 =
1286 						(uint32)getmajor(va.va_rdev);
1287 					rd.specdata2 =
1288 						(uint32)getminor(va.va_rdev);
1289 					IXDR_PUT_U_INT32(ptr, rd.specdata1);
1290 					IXDR_PUT_U_INT32(ptr, rd.specdata2);
1291 				}
1292 				if (ae & FATTR4_SPACE_AVAIL_MASK) {
1293 					IXDR_PUT_HYPER(ptr, sbe.space_avail);
1294 				}
1295 				if (ae & FATTR4_SPACE_FREE_MASK) {
1296 					IXDR_PUT_HYPER(ptr, sbe.space_free);
1297 				}
1298 				if (ae & FATTR4_SPACE_TOTAL_MASK) {
1299 					IXDR_PUT_HYPER(ptr, sbe.space_total);
1300 				}
1301 				if (ae & FATTR4_SPACE_USED_MASK) {
1302 					u_longlong_t su;
1303 					su = (fattr4_space_used) DEV_BSIZE *
1304 					    (fattr4_space_used) va.va_nblocks;
1305 					IXDR_PUT_HYPER(ptr, su);
1306 				}
1307 				if (ae & FATTR4_SYSTEM_MASK) {
1308 					ASSERT(0);
1309 				}
1310 				/* Check the redzone boundary */
1311 				if (ptr > ptr_redzone) {
1312 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1313 						no_space = TRUE;
1314 						continue;
1315 					}
1316 					MINIMIZE_ATTR_MASK(ar);
1317 					ae = ar;
1318 					ptr = lastentry_ptr;
1319 					goto reencode_attrs;
1320 				}
1321 			}
1322 			/*
1323 			 * Redzone check is done at the end of this section.
1324 			 * This particular section will encode a maximum of
1325 			 * 14 * BYTES_PER_XDR_UNIT of data.
1326 			 */
1327 			if (ae &
1328 			    (FATTR4_TIME_ACCESS_MASK |
1329 			    FATTR4_TIME_ACCESS_SET_MASK |
1330 			    FATTR4_TIME_BACKUP_MASK |
1331 			    FATTR4_TIME_CREATE_MASK |
1332 			    FATTR4_TIME_DELTA_MASK |
1333 			    FATTR4_TIME_METADATA_MASK |
1334 			    FATTR4_TIME_MODIFY_MASK |
1335 			    FATTR4_TIME_MODIFY_SET_MASK |
1336 			    FATTR4_MOUNTED_ON_FILEID_MASK)) {
1337 
1338 				if (ae & FATTR4_TIME_ACCESS_MASK) {
1339 					u_longlong_t sec =
1340 					    (u_longlong_t)va.va_atime.tv_sec;
1341 					uint_t nsec =
1342 						(uint_t)va.va_atime.tv_nsec;
1343 					IXDR_PUT_HYPER(ptr, sec);
1344 					IXDR_PUT_INT32(ptr, nsec);
1345 				}
1346 				if (ae & FATTR4_TIME_ACCESS_SET_MASK) {
1347 					ASSERT(0);
1348 				}
1349 				if (ae & FATTR4_TIME_BACKUP_MASK) {
1350 					ASSERT(0);
1351 				}
1352 				if (ae & FATTR4_TIME_CREATE_MASK) {
1353 					ASSERT(0);
1354 				}
1355 				if (ae & FATTR4_TIME_DELTA_MASK) {
1356 					u_longlong_t sec = 0;
1357 					uint_t nsec = 1000;
1358 					IXDR_PUT_HYPER(ptr, sec);
1359 					IXDR_PUT_INT32(ptr, nsec);
1360 				}
1361 				if (ae & FATTR4_TIME_METADATA_MASK) {
1362 					u_longlong_t sec =
1363 					    (u_longlong_t)va.va_ctime.tv_sec;
1364 					uint_t nsec =
1365 						(uint_t)va.va_ctime.tv_nsec;
1366 					IXDR_PUT_HYPER(ptr, sec);
1367 					IXDR_PUT_INT32(ptr, nsec);
1368 				}
1369 				if (ae & FATTR4_TIME_MODIFY_MASK) {
1370 					u_longlong_t sec =
1371 					    (u_longlong_t)va.va_mtime.tv_sec;
1372 					uint_t nsec =
1373 						(uint_t)va.va_mtime.tv_nsec;
1374 					IXDR_PUT_HYPER(ptr, sec);
1375 					IXDR_PUT_INT32(ptr, nsec);
1376 				}
1377 				if (ae & FATTR4_TIME_MODIFY_SET_MASK) {
1378 					ASSERT(0);
1379 				}
1380 				if (ae & FATTR4_MOUNTED_ON_FILEID_MASK) {
1381 					IXDR_PUT_HYPER(ptr, dp->d_ino);
1382 				}
1383 				/* Check the redzone boundary */
1384 				if (ptr > ptr_redzone) {
1385 					if (nents || IS_MIN_ATTR_MASK(ar)) {
1386 						no_space = TRUE;
1387 						continue;
1388 					}
1389 					MINIMIZE_ATTR_MASK(ar);
1390 					ae = ar;
1391 					ptr = lastentry_ptr;
1392 					goto reencode_attrs;
1393 				}
1394 			}
1395 		}
1396 
1397 		/* Reset to directory's vfs info when encoding complete */
1398 		if (vfs_different) {
1399 			dsbe = sbe;
1400 			dpce = pce;
1401 			vfs_different = 0;
1402 		}
1403 
1404 		/* "go back" and encode the attributes' length */
1405 		attr_length =
1406 			(char *)ptr -
1407 			(char *)attr_offset_ptr -
1408 			BYTES_PER_XDR_UNIT;
1409 		IXDR_PUT_U_INT32(attr_offset_ptr, attr_length);
1410 
1411 		/*
1412 		 * If there was trouble obtaining a mapping for either
1413 		 * the owner or group attributes, then remove them from
1414 		 * bitmap4 for this entry and reset the bitmap value
1415 		 * in the data stream.
1416 		 */
1417 		if (owner_error || group_error) {
1418 			if (owner_error)
1419 				ae &= ~FATTR4_OWNER_MASK;
1420 			if (group_error)
1421 				ae &= ~FATTR4_OWNER_GROUP_MASK;
1422 			IXDR_PUT_HYPER(attrmask_ptr, ae);
1423 		}
1424 
1425 		/* END OF ATTRIBUTE ENCODING */
1426 
1427 		lastentry_ptr = ptr;
1428 		nents++;
1429 		rddir_next_offset = dp->d_off;
1430 	}
1431 
1432 	/*
1433 	 * Check for the case that another VOP_READDIR() has to be done.
1434 	 * - no space encoding error
1435 	 * - no entry successfully encoded
1436 	 * - still more directory to read
1437 	 */
1438 	if (!no_space && nents == 0 && !iseofdir)
1439 		goto readagain;
1440 
1441 	*cs->statusp = resp->status = NFS4_OK;
1442 
1443 	/*
1444 	 * If no_space is set then we terminated prematurely,
1445 	 * rewind to the last entry and this can never be EOF.
1446 	 */
1447 	if (no_space) {
1448 		ptr = lastentry_ptr;
1449 		eof = FALSE; /* ended encoded prematurely */
1450 	} else {
1451 		eof = (iseofdir ? TRUE : FALSE);
1452 	}
1453 
1454 	/*
1455 	 * If we have entries, always return them, otherwise only error
1456 	 * if we ran out of space.
1457 	 */
1458 	if (nents || !no_space) {
1459 		ASSERT(ptr != NULL);
1460 		/* encode the BOOLEAN marking no further entries */
1461 		IXDR_PUT_U_INT32(ptr, false);
1462 		/* encode the BOOLEAN signifying end of directory */
1463 		IXDR_PUT_U_INT32(ptr, eof);
1464 
1465 		resp->data_len = (char *)ptr - (char *)beginning_ptr;
1466 		resp->mblk->b_wptr += resp->data_len;
1467 	} else {
1468 		freeb(mp);
1469 		resp->mblk = NULL;
1470 		resp->data_len = 0;
1471 		*cs->statusp = resp->status = NFS4ERR_TOOSMALL;
1472 	}
1473 
1474 	kmem_free((caddr_t)rddir_data, rddir_data_len);
1475 	if (vp)
1476 		VN_RELE(vp);
1477 	if (owner.utf8string_len != 0)
1478 		kmem_free(owner.utf8string_val,	owner.utf8string_len);
1479 	if (group.utf8string_len != 0)
1480 		kmem_free(group.utf8string_val, group.utf8string_len);
1481 }
1482