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