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,
 108                 struct compound_state *cs, 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
 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
 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
 386 rfs4_op_readdir(nfs_argop4 *argop, nfs_resop4 *resop,
 387         struct svc_req *req, 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         int expseudo = 0;
 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 expseudo */
 692                 expseudo = 0;
 693 
 694                 if (vp) {
 695                         VN_RELE(vp);
 696                         vp = NULL;
 697                 }
 698 
 699                 if (newexi) {
 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, &expseudo)) {
 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, expseudo);
 731                 if (error == ENOENT) {
 732                         rddir_next_offset = dp->d_off;
 733                         continue;
 734                 }
 735 
 736                 rddirattr_error = error;
 737 
 738                 /*
 739                  * The vp obtained from above may be from a
 740                  * different filesystem mount and the vfs-like
 741                  * attributes should be obtained from that
 742                  * different vfs; only do this if appropriate.
 743                  */
 744                 if (vp &&
 745                     (vfs_different = (dvp->v_vfsp != vp->v_vfsp))) {
 746                         if (ar & (FATTR4_FILES_AVAIL_MASK |
 747                             FATTR4_FILES_FREE_MASK |
 748                             FATTR4_FILES_TOTAL_MASK |
 749                             FATTR4_FILES_AVAIL_MASK |
 750                             FATTR4_FILES_FREE_MASK |
 751                             FATTR4_FILES_TOTAL_MASK)) {
 752                                 if (error =
 753                                     rfs4_get_sb_encode(dvp->v_vfsp,
 754                                     &sbe)) {
 755                                         /* Remove attrs from encode */
 756                                         ae &= ~(FATTR4_FILES_AVAIL_MASK |
 757                                             FATTR4_FILES_FREE_MASK |
 758                                             FATTR4_FILES_TOTAL_MASK |
 759                                             FATTR4_FILES_AVAIL_MASK |
 760                                             FATTR4_FILES_FREE_MASK |
 761                                             FATTR4_FILES_TOTAL_MASK);
 762                                         rddirattr_error = error;
 763                                 }
 764                         }
 765                         if (ar & (FATTR4_MAXFILESIZE_MASK |
 766                             FATTR4_MAXLINK_MASK |
 767                             FATTR4_MAXNAME_MASK)) {
 768                                 if (error = rfs4_get_pc_encode(cs->vp,
 769                                     &pce, ar, cs->cr)) {
 770                                         ar &= ~(FATTR4_MAXFILESIZE_MASK |
 771                                             FATTR4_MAXLINK_MASK |
 772                                             FATTR4_MAXNAME_MASK);
 773                                         rddirattr_error = error;
 774                                 }
 775                         }
 776                 }
 777 
 778 reencode_attrs:
 779                 /* encode the BOOLEAN for the existence of the next entry */
 780                 IXDR_PUT_U_INT32(ptr, true);
 781                 /* encode the COOKIE for the entry */
 782                 IXDR_PUT_U_HYPER(ptr, dp->d_off);
 783 
 784                 name = nfscmd_convname(ca, cs->exi, dp->d_name,
 785                     NFSCMD_CONV_OUTBOUND, MAXPATHLEN + 1);
 786 
 787                 if (name == NULL) {
 788                         rddir_next_offset = dp->d_off;
 789                         continue;
 790                 }
 791                 /* Calculate the dirent name length */
 792                 namelen = strlen(name);
 793 
 794                 rndup = RNDUP(namelen) / BYTES_PER_XDR_UNIT;
 795 
 796                 /* room for LENGTH + string ? */
 797                 if ((ptr + (1 + rndup)) > ptr_redzone) {
 798                         no_space = TRUE;
 799                         continue;
 800                 }
 801 
 802                 /* encode the LENGTH of the name */
 803                 IXDR_PUT_U_INT32(ptr, namelen);
 804                 /* encode the RNDUP FILL first */
 805                 ptr[rndup - 1] = 0;
 806                 /* encode the NAME of the entry */
 807                 bcopy(name, (char *)ptr, namelen);
 808                 /* now bump the ptr after... */
 809                 ptr += rndup;
 810 
 811                 if (name != dp->d_name)
 812                         kmem_free(name, MAXPATHLEN + 1);
 813 
 814                 /*
 815                  * Keep checking on the dircount to see if we have
 816                  * reached the limit; from the RFC, dircount is to be
 817                  * the XDR encoded limit of the cookie plus name.
 818                  * So the count is the name, XDR_UNIT of length for
 819                  * that name and 2 * XDR_UNIT bytes of cookie;
 820                  * However, use the regular DIRENT64 to match most
 821                  * client's APIs.
 822                  */
 823                 dircount -= DIRENT64_RECLEN(namelen);
 824                 if (nents != 0 && dircount < 0) {
 825                         no_space = TRUE;
 826                         continue;
 827                 }
 828 
 829                 /*
 830                  * Attributes requested?
 831                  * Gather up the attribute info and the previous VOP_LOOKUP()
 832                  * succeeded; if an error occurs on the VOP_GETATTR() then
 833                  * return just the error (again if it is requested).
 834                  * Note that the previous VOP_LOOKUP() could have failed
 835                  * itself which leaves this code without anything for
 836                  * a VOP_GETATTR().
 837                  * Also note that the readdir_attr_error is left in the
 838                  * encoding mask if requested and so is the mounted_on_fileid.
 839                  */
 840                 if (ae != 0) {
 841                         if (!vp) {
 842                                 ae = ar & (FATTR4_RDATTR_ERROR_MASK |
 843                                     FATTR4_MOUNTED_ON_FILEID_MASK);
 844                         } else {
 845                                 va.va_mask = AT_ALL;
 846                                 rddirattr_error =
 847                                     VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
 848                                 if (rddirattr_error) {
 849                                         ae = ar & (FATTR4_RDATTR_ERROR_MASK |
 850                                             FATTR4_MOUNTED_ON_FILEID_MASK);
 851                                 } else {
 852                                         /*
 853                                          * We may lie about the object
 854                                          * type for a referral
 855                                          */
 856                                         if (vn_is_nfs_reparse(vp, cs->cr) &&
 857                                             client_is_downrev(req))
 858                                                 va.va_type = VLNK;
 859                                 }
 860                         }
 861                 }
 862 
 863                 /* START OF ATTRIBUTE ENCODING */
 864 
 865                 /* encode the LENGTH of the BITMAP4 array */
 866                 IXDR_PUT_U_INT32(ptr, 2);
 867                 /* encode the BITMAP4 */
 868                 attrmask_ptr = ptr;
 869                 IXDR_PUT_HYPER(ptr, ae);
 870                 attr_offset_ptr = ptr;
 871                 /* encode the default LENGTH of the attributes for entry */
 872                 IXDR_PUT_U_INT32(ptr, 0);
 873 
 874                 if (ptr > ptr_redzone) {
 875                         no_space = TRUE;
 876                         continue;
 877                 }
 878 
 879                 /* Check if any of the first 32 attributes are being encoded */
 880                 if (ae & 0xffffffff00000000) {
 881                         /*
 882                          * Redzone check is done at the end of this section.
 883                          * This particular section will encode a maximum of
 884                          * 18 * BYTES_PER_XDR_UNIT of data
 885                          */
 886                         if (ae &
 887                             (FATTR4_SUPPORTED_ATTRS_MASK |
 888                             FATTR4_TYPE_MASK |
 889                             FATTR4_FH_EXPIRE_TYPE_MASK |
 890                             FATTR4_CHANGE_MASK |
 891                             FATTR4_SIZE_MASK |
 892                             FATTR4_LINK_SUPPORT_MASK |
 893                             FATTR4_SYMLINK_SUPPORT_MASK |
 894                             FATTR4_NAMED_ATTR_MASK |
 895                             FATTR4_FSID_MASK |
 896                             FATTR4_UNIQUE_HANDLES_MASK |
 897                             FATTR4_LEASE_TIME_MASK |
 898                             FATTR4_RDATTR_ERROR_MASK)) {
 899 
 900                                 if (ae & FATTR4_SUPPORTED_ATTRS_MASK) {
 901                                         IXDR_PUT_INT32(ptr, 2);
 902                                         IXDR_PUT_HYPER(ptr,
 903                                             rfs4_supported_attrs);
 904                                 }
 905                                 if (ae & FATTR4_TYPE_MASK) {
 906                                         uint_t ftype = vt_to_nf4[va.va_type];
 907                                         if (dvp->v_flag & V_XATTRDIR) {
 908                                                 if (va.va_type == VDIR)
 909                                                         ftype = NF4ATTRDIR;
 910                                                 else
 911                                                         ftype = NF4NAMEDATTR;
 912                                         }
 913                                         IXDR_PUT_U_INT32(ptr, ftype);
 914                                 }
 915                                 if (ae & FATTR4_FH_EXPIRE_TYPE_MASK) {
 916                                         uint_t expire_type = FH4_PERSISTENT;
 917                                         IXDR_PUT_U_INT32(ptr, expire_type);
 918                                 }
 919                                 if (ae & FATTR4_CHANGE_MASK) {
 920                                         u_longlong_t change;
 921                                         NFS4_SET_FATTR4_CHANGE(change,
 922                                             va.va_ctime);
 923                                         IXDR_PUT_HYPER(ptr, change);
 924                                 }
 925                                 if (ae & FATTR4_SIZE_MASK) {
 926                                         u_longlong_t size = va.va_size;
 927                                         IXDR_PUT_HYPER(ptr, size);
 928                                 }
 929                                 if (ae & FATTR4_LINK_SUPPORT_MASK) {
 930                                         IXDR_PUT_U_INT32(ptr, true);
 931                                 }
 932                                 if (ae & FATTR4_SYMLINK_SUPPORT_MASK) {
 933                                         IXDR_PUT_U_INT32(ptr, true);
 934                                 }
 935                                 if (ae & FATTR4_NAMED_ATTR_MASK) {
 936                                         uint_t isit;
 937                                         pc_val = FALSE;
 938                                         int sattr_error;
 939 
 940                                         if (!(vp->v_vfsp->vfs_flag &
 941                                             VFS_XATTR)) {
 942                                                 isit = FALSE;
 943                                         } else {
 944                                                 sattr_error = VOP_PATHCONF(vp,
 945                                                     _PC_SATTR_EXISTS,
 946                                                     &pc_val, cs->cr, NULL);
 947                                                 if (sattr_error || pc_val == 0)
 948                                                         (void) VOP_PATHCONF(vp,
 949                                                             _PC_XATTR_EXISTS,
 950                                                             &pc_val,
 951                                                             cs->cr, NULL);
 952                                         }
 953                                         isit = (pc_val ? TRUE : FALSE);
 954                                         IXDR_PUT_U_INT32(ptr, isit);
 955                                 }
 956                                 if (ae & FATTR4_FSID_MASK) {
 957                                         u_longlong_t major, minor;
 958                                         struct exportinfo *exi;
 959 
 960                                         exi = newexi ? newexi : cs->exi;
 961                                         if (exi->exi_volatile_dev) {
 962                                                 int *pmaj = (int *)&major;
 963 
 964                                                 pmaj[0] = exi->exi_fsid.val[0];
 965                                                 pmaj[1] = exi->exi_fsid.val[1];
 966                                                 minor = 0;
 967                                         } else {
 968                                                 major = getmajor(va.va_fsid);
 969                                                 minor = getminor(va.va_fsid);
 970                                         }
 971                                         IXDR_PUT_HYPER(ptr, major);
 972                                         IXDR_PUT_HYPER(ptr, minor);
 973                                 }
 974                                 if (ae & FATTR4_UNIQUE_HANDLES_MASK) {
 975                                         IXDR_PUT_U_INT32(ptr, false);
 976                                 }
 977                                 if (ae & FATTR4_LEASE_TIME_MASK) {
 978                                         uint_t lt = rfs4_lease_time;
 979                                         IXDR_PUT_U_INT32(ptr, lt);
 980                                 }
 981                                 if (ae & FATTR4_RDATTR_ERROR_MASK) {
 982                                         rddirattr_error =
 983                                             (rddirattr_error == 0 ?
 984                                             0 : puterrno4(rddirattr_error));
 985                                         IXDR_PUT_U_INT32(ptr, rddirattr_error);
 986                                 }
 987 
 988                                 /* Check the redzone boundary */
 989                                 if (ptr > ptr_redzone) {
 990                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
 991                                                 no_space = TRUE;
 992                                                 continue;
 993                                         }
 994                                         MINIMIZE_ATTR_MASK(ar);
 995                                         ae = ar;
 996                                         ptr = lastentry_ptr;
 997                                         goto reencode_attrs;
 998                                 }
 999                         }
1000                         /*
1001                          * Redzone check is done at the end of this section.
1002                          * This particular section will encode a maximum of
1003                          * 4 * BYTES_PER_XDR_UNIT of data.
1004                          * NOTE: that if ACLs are supported that the
1005                          * redzone calculations will need to change.
1006                          */
1007                         if (ae &
1008                             (FATTR4_ACL_MASK |
1009                             FATTR4_ACLSUPPORT_MASK |
1010                             FATTR4_ARCHIVE_MASK |
1011                             FATTR4_CANSETTIME_MASK |
1012                             FATTR4_CASE_INSENSITIVE_MASK |
1013                             FATTR4_CASE_PRESERVING_MASK |
1014                             FATTR4_CHOWN_RESTRICTED_MASK)) {
1015 
1016                                 if (ae & FATTR4_ACL_MASK) {
1017                                         ASSERT(0);
1018                                 }
1019                                 if (ae & FATTR4_ACLSUPPORT_MASK) {
1020                                         ASSERT(0);
1021                                 }
1022                                 if (ae & FATTR4_ARCHIVE_MASK) {
1023                                         ASSERT(0);
1024                                 }
1025                                 if (ae & FATTR4_CANSETTIME_MASK) {
1026                                         IXDR_PUT_U_INT32(ptr, true);
1027                                 }
1028                                 if (ae & FATTR4_CASE_INSENSITIVE_MASK) {
1029                                         IXDR_PUT_U_INT32(ptr, false);
1030                                 }
1031                                 if (ae & FATTR4_CASE_PRESERVING_MASK) {
1032                                         IXDR_PUT_U_INT32(ptr, true);
1033                                 }
1034                                 if (ae & FATTR4_CHOWN_RESTRICTED_MASK) {
1035                                         uint_t isit;
1036                                         pc_val = FALSE;
1037                                         (void) VOP_PATHCONF(vp,
1038                                             _PC_CHOWN_RESTRICTED,
1039                                             &pc_val, cs->cr, NULL);
1040                                         isit = (pc_val ? TRUE : FALSE);
1041                                         IXDR_PUT_U_INT32(ptr, isit);
1042                                 }
1043                                 /* Check the redzone boundary */
1044                                 if (ptr > ptr_redzone) {
1045                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1046                                                 no_space = TRUE;
1047                                                 continue;
1048                                         }
1049                                         MINIMIZE_ATTR_MASK(ar);
1050                                         ae = ar;
1051                                         ptr = lastentry_ptr;
1052                                         goto reencode_attrs;
1053                                 }
1054                         }
1055                         /*
1056                          * Redzone check is done before the filehandle
1057                          * is encoded.
1058                          */
1059                         if (ae &
1060                             (FATTR4_FILEHANDLE_MASK |
1061                             FATTR4_FILEID_MASK)) {
1062 
1063                                 if (ae & FATTR4_FILEHANDLE_MASK) {
1064                                         struct {
1065                                                 uint_t len;
1066                                                 char *val;
1067                                                 char fh[NFS_FH4_LEN];
1068                                         } fh;
1069                                         fh.len = 0;
1070                                         fh.val = fh.fh;
1071                                         (void) makefh4((nfs_fh4 *)&fh, vp,
1072                                             (newexi ? newexi : cs->exi));
1073 
1074                                         if (dvp->v_flag & V_XATTRDIR)
1075                                                 set_fh4_flag((nfs_fh4 *)&fh,
1076                                                     FH4_NAMEDATTR);
1077 
1078                                         if (!xdr_inline_encode_nfs_fh4(
1079                                             &ptr, ptr_redzone,
1080                                             (nfs_fh4_fmt_t *)fh.val)) {
1081                                                 if (nents ||
1082                                                     IS_MIN_ATTR_MASK(ar)) {
1083                                                         no_space = TRUE;
1084                                                         continue;
1085                                                 }
1086                                                 MINIMIZE_ATTR_MASK(ar);
1087                                                 ae = ar;
1088                                                 ptr = lastentry_ptr;
1089                                                 goto reencode_attrs;
1090                                         }
1091                                 }
1092                                 if (ae & FATTR4_FILEID_MASK) {
1093                                         IXDR_PUT_HYPER(ptr, va.va_nodeid);
1094                                 }
1095                                 /* Check the redzone boundary */
1096                                 if (ptr > ptr_redzone) {
1097                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1098                                                 no_space = TRUE;
1099                                                 continue;
1100                                         }
1101                                         MINIMIZE_ATTR_MASK(ar);
1102                                         ae = ar;
1103                                         ptr = lastentry_ptr;
1104                                         goto reencode_attrs;
1105                                 }
1106                         }
1107                         /*
1108                          * Redzone check is done at the end of this section.
1109                          * This particular section will encode a maximum of
1110                          * 15 * BYTES_PER_XDR_UNIT of data.
1111                          */
1112                         if (ae &
1113                             (FATTR4_FILES_AVAIL_MASK |
1114                             FATTR4_FILES_FREE_MASK |
1115                             FATTR4_FILES_TOTAL_MASK |
1116                             FATTR4_FS_LOCATIONS_MASK |
1117                             FATTR4_HIDDEN_MASK |
1118                             FATTR4_HOMOGENEOUS_MASK |
1119                             FATTR4_MAXFILESIZE_MASK |
1120                             FATTR4_MAXLINK_MASK |
1121                             FATTR4_MAXNAME_MASK |
1122                             FATTR4_MAXREAD_MASK |
1123                             FATTR4_MAXWRITE_MASK)) {
1124 
1125                                 if (ae & FATTR4_FILES_AVAIL_MASK) {
1126                                         IXDR_PUT_HYPER(ptr, sbe.fa);
1127                                 }
1128                                 if (ae & FATTR4_FILES_FREE_MASK) {
1129                                         IXDR_PUT_HYPER(ptr, sbe.ff);
1130                                 }
1131                                 if (ae & FATTR4_FILES_TOTAL_MASK) {
1132                                         IXDR_PUT_HYPER(ptr, sbe.ft);
1133                                 }
1134                                 if (ae & FATTR4_FS_LOCATIONS_MASK) {
1135                                         ASSERT(0);
1136                                 }
1137                                 if (ae & FATTR4_HIDDEN_MASK) {
1138                                         ASSERT(0);
1139                                 }
1140                                 if (ae & FATTR4_HOMOGENEOUS_MASK) {
1141                                         IXDR_PUT_U_INT32(ptr, true);
1142                                 }
1143                                 if (ae & FATTR4_MAXFILESIZE_MASK) {
1144                                         IXDR_PUT_HYPER(ptr, pce.maxfilesize);
1145                                 }
1146                                 if (ae & FATTR4_MAXLINK_MASK) {
1147                                         IXDR_PUT_U_INT32(ptr, pce.maxlink);
1148                                 }
1149                                 if (ae & FATTR4_MAXNAME_MASK) {
1150                                         IXDR_PUT_U_INT32(ptr, pce.maxname);
1151                                 }
1152                                 if (ae & FATTR4_MAXREAD_MASK) {
1153                                         IXDR_PUT_HYPER(ptr, maxread);
1154                                 }
1155                                 if (ae & FATTR4_MAXWRITE_MASK) {
1156                                         IXDR_PUT_HYPER(ptr, maxwrite);
1157                                 }
1158                                 /* Check the redzone boundary */
1159                                 if (ptr > ptr_redzone) {
1160                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1161                                                 no_space = TRUE;
1162                                                 continue;
1163                                         }
1164                                         MINIMIZE_ATTR_MASK(ar);
1165                                         ae = ar;
1166                                         ptr = lastentry_ptr;
1167                                         goto reencode_attrs;
1168                                 }
1169                         }
1170                 }
1171                 if (ae & 0x00000000ffffffff) {
1172                         /*
1173                          * Redzone check is done at the end of this section.
1174                          * This particular section will encode a maximum of
1175                          * 3 * BYTES_PER_XDR_UNIT of data.
1176                          */
1177                         if (ae &
1178                             (FATTR4_MIMETYPE_MASK |
1179                             FATTR4_MODE_MASK |
1180                             FATTR4_NO_TRUNC_MASK |
1181                             FATTR4_NUMLINKS_MASK)) {
1182 
1183                                 if (ae & FATTR4_MIMETYPE_MASK) {
1184                                         ASSERT(0);
1185                                 }
1186                                 if (ae & FATTR4_MODE_MASK) {
1187                                         uint_t m = va.va_mode;
1188                                         IXDR_PUT_U_INT32(ptr, m);
1189                                 }
1190                                 if (ae & FATTR4_NO_TRUNC_MASK) {
1191                                         IXDR_PUT_U_INT32(ptr, true);
1192                                 }
1193                                 if (ae & FATTR4_NUMLINKS_MASK) {
1194                                         IXDR_PUT_U_INT32(ptr, va.va_nlink);
1195                                 }
1196                                 /* Check the redzone boundary */
1197                                 if (ptr > ptr_redzone) {
1198                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1199                                                 no_space = TRUE;
1200                                                 continue;
1201                                         }
1202                                         MINIMIZE_ATTR_MASK(ar);
1203                                         ae = ar;
1204                                         ptr = lastentry_ptr;
1205                                         goto reencode_attrs;
1206                                 }
1207                         }
1208                         /*
1209                          * Redzone check is done before the encoding of the
1210                          * owner string since the length is indeterminate.
1211                          */
1212                         if (ae & FATTR4_OWNER_MASK) {
1213                                 if (!lu_set) {
1214                                         owner_error = nfs_idmap_uid_str(
1215                                             va.va_uid, &owner, TRUE);
1216                                         if (!owner_error) {
1217                                                 lu_set = TRUE;
1218                                                 lastuid = va.va_uid;
1219                                         }
1220                                 } else  if (va.va_uid != lastuid) {
1221                                         if (owner.utf8string_len != 0) {
1222                                                 kmem_free(owner.utf8string_val,
1223                                                     owner.utf8string_len);
1224                                                 owner.utf8string_len = 0;
1225                                                 owner.utf8string_val = NULL;
1226                                         }
1227                                         owner_error = nfs_idmap_uid_str(
1228                                             va.va_uid, &owner, TRUE);
1229                                         if (!owner_error) {
1230                                                 lastuid = va.va_uid;
1231                                         } else {
1232                                                 lu_set = FALSE;
1233                                         }
1234                                 }
1235                                 if (!owner_error) {
1236                                         if ((ptr +
1237                                             (owner.utf8string_len /
1238                                             BYTES_PER_XDR_UNIT)
1239                                             + 2) > ptr_redzone) {
1240                                                 if (nents ||
1241                                                     IS_MIN_ATTR_MASK(ar)) {
1242                                                         no_space = TRUE;
1243                                                         continue;
1244                                                 }
1245                                                 MINIMIZE_ATTR_MASK(ar);
1246                                                 ae = ar;
1247                                                 ptr = lastentry_ptr;
1248                                                 goto reencode_attrs;
1249                                         }
1250                                         /* encode the LENGTH of owner string */
1251                                         IXDR_PUT_U_INT32(ptr,
1252                                             owner.utf8string_len);
1253                                         /* encode the RNDUP FILL first */
1254                                         rndup = RNDUP(owner.utf8string_len) /
1255                                             BYTES_PER_XDR_UNIT;
1256                                         ptr[rndup - 1] = 0;
1257                                         /* encode the OWNER */
1258                                         bcopy(owner.utf8string_val, ptr,
1259                                             owner.utf8string_len);
1260                                         ptr += rndup;
1261                                 }
1262                         }
1263                         /*
1264                          * Redzone check is done before the encoding of the
1265                          * group string since the length is indeterminate.
1266                          */
1267                         if (ae & FATTR4_OWNER_GROUP_MASK) {
1268                                 if (!lg_set) {
1269                                         group_error =
1270                                             nfs_idmap_gid_str(va.va_gid,
1271                                             &group, TRUE);
1272                                         if (!group_error) {
1273                                                 lg_set = TRUE;
1274                                                 lastgid = va.va_gid;
1275                                         }
1276                                 } else if (va.va_gid != lastgid) {
1277                                         if (group.utf8string_len != 0) {
1278                                                 kmem_free(
1279                                                     group.utf8string_val,
1280                                                     group.utf8string_len);
1281                                                 group.utf8string_len = 0;
1282                                                 group.utf8string_val = NULL;
1283                                         }
1284                                         group_error =
1285                                             nfs_idmap_gid_str(va.va_gid,
1286                                             &group, TRUE);
1287                                         if (!group_error)
1288                                                 lastgid = va.va_gid;
1289                                         else
1290                                                 lg_set = FALSE;
1291                                 }
1292                                 if (!group_error) {
1293                                         if ((ptr +
1294                                             (group.utf8string_len /
1295                                             BYTES_PER_XDR_UNIT)
1296                                             + 2) > ptr_redzone) {
1297                                                 if (nents ||
1298                                                     IS_MIN_ATTR_MASK(ar)) {
1299                                                         no_space = TRUE;
1300                                                         continue;
1301                                                 }
1302                                                 MINIMIZE_ATTR_MASK(ar);
1303                                                 ae = ar;
1304                                                 ptr = lastentry_ptr;
1305                                                 goto reencode_attrs;
1306                                         }
1307                                         /* encode the LENGTH of owner string */
1308                                         IXDR_PUT_U_INT32(ptr,
1309                                             group.utf8string_len);
1310                                         /* encode the RNDUP FILL first */
1311                                         rndup = RNDUP(group.utf8string_len) /
1312                                             BYTES_PER_XDR_UNIT;
1313                                         ptr[rndup - 1] = 0;
1314                                         /* encode the OWNER */
1315                                         bcopy(group.utf8string_val, ptr,
1316                                             group.utf8string_len);
1317                                         ptr += rndup;
1318                                 }
1319                         }
1320                         if (ae &
1321                             (FATTR4_QUOTA_AVAIL_HARD_MASK |
1322                             FATTR4_QUOTA_AVAIL_SOFT_MASK |
1323                             FATTR4_QUOTA_USED_MASK)) {
1324                                 if (ae & FATTR4_QUOTA_AVAIL_HARD_MASK) {
1325                                         ASSERT(0);
1326                                 }
1327                                 if (ae & FATTR4_QUOTA_AVAIL_SOFT_MASK) {
1328                                         ASSERT(0);
1329                                 }
1330                                 if (ae & FATTR4_QUOTA_USED_MASK) {
1331                                         ASSERT(0);
1332                                 }
1333                         }
1334                         /*
1335                          * Redzone check is done at the end of this section.
1336                          * This particular section will encode a maximum of
1337                          * 10 * BYTES_PER_XDR_UNIT of data.
1338                          */
1339                         if (ae &
1340                             (FATTR4_RAWDEV_MASK |
1341                             FATTR4_SPACE_AVAIL_MASK |
1342                             FATTR4_SPACE_FREE_MASK |
1343                             FATTR4_SPACE_TOTAL_MASK |
1344                             FATTR4_SPACE_USED_MASK |
1345                             FATTR4_SYSTEM_MASK)) {
1346 
1347                                 if (ae & FATTR4_RAWDEV_MASK) {
1348                                         fattr4_rawdev rd;
1349                                         rd.specdata1 =
1350                                             (uint32)getmajor(va.va_rdev);
1351                                         rd.specdata2 =
1352                                             (uint32)getminor(va.va_rdev);
1353                                         IXDR_PUT_U_INT32(ptr, rd.specdata1);
1354                                         IXDR_PUT_U_INT32(ptr, rd.specdata2);
1355                                 }
1356                                 if (ae & FATTR4_SPACE_AVAIL_MASK) {
1357                                         IXDR_PUT_HYPER(ptr, sbe.space_avail);
1358                                 }
1359                                 if (ae & FATTR4_SPACE_FREE_MASK) {
1360                                         IXDR_PUT_HYPER(ptr, sbe.space_free);
1361                                 }
1362                                 if (ae & FATTR4_SPACE_TOTAL_MASK) {
1363                                         IXDR_PUT_HYPER(ptr, sbe.space_total);
1364                                 }
1365                                 if (ae & FATTR4_SPACE_USED_MASK) {
1366                                         u_longlong_t su;
1367                                         su = (fattr4_space_used) DEV_BSIZE *
1368                                             (fattr4_space_used) va.va_nblocks;
1369                                         IXDR_PUT_HYPER(ptr, su);
1370                                 }
1371                                 if (ae & FATTR4_SYSTEM_MASK) {
1372                                         ASSERT(0);
1373                                 }
1374                                 /* Check the redzone boundary */
1375                                 if (ptr > ptr_redzone) {
1376                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1377                                                 no_space = TRUE;
1378                                                 continue;
1379                                         }
1380                                         MINIMIZE_ATTR_MASK(ar);
1381                                         ae = ar;
1382                                         ptr = lastentry_ptr;
1383                                         goto reencode_attrs;
1384                                 }
1385                         }
1386                         /*
1387                          * Redzone check is done at the end of this section.
1388                          * This particular section will encode a maximum of
1389                          * 14 * BYTES_PER_XDR_UNIT of data.
1390                          */
1391                         if (ae &
1392                             (FATTR4_TIME_ACCESS_MASK |
1393                             FATTR4_TIME_ACCESS_SET_MASK |
1394                             FATTR4_TIME_BACKUP_MASK |
1395                             FATTR4_TIME_CREATE_MASK |
1396                             FATTR4_TIME_DELTA_MASK |
1397                             FATTR4_TIME_METADATA_MASK |
1398                             FATTR4_TIME_MODIFY_MASK |
1399                             FATTR4_TIME_MODIFY_SET_MASK |
1400                             FATTR4_MOUNTED_ON_FILEID_MASK)) {
1401 
1402                                 if (ae & FATTR4_TIME_ACCESS_MASK) {
1403                                         u_longlong_t sec =
1404                                             (u_longlong_t)va.va_atime.tv_sec;
1405                                         uint_t nsec =
1406                                             (uint_t)va.va_atime.tv_nsec;
1407                                         IXDR_PUT_HYPER(ptr, sec);
1408                                         IXDR_PUT_INT32(ptr, nsec);
1409                                 }
1410                                 if (ae & FATTR4_TIME_ACCESS_SET_MASK) {
1411                                         ASSERT(0);
1412                                 }
1413                                 if (ae & FATTR4_TIME_BACKUP_MASK) {
1414                                         ASSERT(0);
1415                                 }
1416                                 if (ae & FATTR4_TIME_CREATE_MASK) {
1417                                         ASSERT(0);
1418                                 }
1419                                 if (ae & FATTR4_TIME_DELTA_MASK) {
1420                                         u_longlong_t sec = 0;
1421                                         uint_t nsec = 1000;
1422                                         IXDR_PUT_HYPER(ptr, sec);
1423                                         IXDR_PUT_INT32(ptr, nsec);
1424                                 }
1425                                 if (ae & FATTR4_TIME_METADATA_MASK) {
1426                                         u_longlong_t sec =
1427                                             (u_longlong_t)va.va_ctime.tv_sec;
1428                                         uint_t nsec =
1429                                             (uint_t)va.va_ctime.tv_nsec;
1430                                         IXDR_PUT_HYPER(ptr, sec);
1431                                         IXDR_PUT_INT32(ptr, nsec);
1432                                 }
1433                                 if (ae & FATTR4_TIME_MODIFY_MASK) {
1434                                         u_longlong_t sec =
1435                                             (u_longlong_t)va.va_mtime.tv_sec;
1436                                         uint_t nsec =
1437                                             (uint_t)va.va_mtime.tv_nsec;
1438                                         IXDR_PUT_HYPER(ptr, sec);
1439                                         IXDR_PUT_INT32(ptr, nsec);
1440                                 }
1441                                 if (ae & FATTR4_TIME_MODIFY_SET_MASK) {
1442                                         ASSERT(0);
1443                                 }
1444                                 if (ae & FATTR4_MOUNTED_ON_FILEID_MASK) {
1445                                         IXDR_PUT_HYPER(ptr, dp->d_ino);
1446                                 }
1447                                 /* Check the redzone boundary */
1448                                 if (ptr > ptr_redzone) {
1449                                         if (nents || IS_MIN_ATTR_MASK(ar)) {
1450                                                 no_space = TRUE;
1451                                                 continue;
1452                                         }
1453                                         MINIMIZE_ATTR_MASK(ar);
1454                                         ae = ar;
1455                                         ptr = lastentry_ptr;
1456                                         goto reencode_attrs;
1457                                 }
1458                         }
1459                 }
1460 
1461                 /* Reset to directory's vfs info when encoding complete */
1462                 if (vfs_different) {
1463                         dsbe = sbe;
1464                         dpce = pce;
1465                         vfs_different = 0;
1466                 }
1467 
1468                 /* "go back" and encode the attributes' length */
1469                 attr_length =
1470                     (char *)ptr -
1471                     (char *)attr_offset_ptr -
1472                     BYTES_PER_XDR_UNIT;
1473                 IXDR_PUT_U_INT32(attr_offset_ptr, attr_length);
1474 
1475                 /*
1476                  * If there was trouble obtaining a mapping for either
1477                  * the owner or group attributes, then remove them from
1478                  * bitmap4 for this entry and reset the bitmap value
1479                  * in the data stream.
1480                  */
1481                 if (owner_error || group_error) {
1482                         if (owner_error)
1483                                 ae &= ~FATTR4_OWNER_MASK;
1484                         if (group_error)
1485                                 ae &= ~FATTR4_OWNER_GROUP_MASK;
1486                         IXDR_PUT_HYPER(attrmask_ptr, ae);
1487                 }
1488 
1489                 /* END OF ATTRIBUTE ENCODING */
1490 
1491                 lastentry_ptr = ptr;
1492                 nents++;
1493                 rddir_next_offset = dp->d_off;
1494         }
1495 
1496         if (newexi) {
1497                 exi_rele(newexi);
1498                 newexi = NULL;
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 }