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