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