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 (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  *      Copyright 1983, 1984, 1985, 1986, 1987, 1988, 1989  AT&T.
  28  *              All rights reserved.
  29  */
  30 
  31 /*
  32  * Copyright 2018 Nexenta Systems, Inc.
  33  */
  34 
  35 #include <sys/types.h>
  36 #include <sys/param.h>
  37 #include <sys/time.h>
  38 #include <sys/vfs.h>
  39 #include <sys/vnode.h>
  40 #include <sys/socket.h>
  41 #include <sys/errno.h>
  42 #include <sys/uio.h>
  43 #include <sys/proc.h>
  44 #include <sys/user.h>
  45 #include <sys/file.h>
  46 #include <sys/tiuser.h>
  47 #include <sys/kmem.h>
  48 #include <sys/pathname.h>
  49 #include <sys/debug.h>
  50 #include <sys/vtrace.h>
  51 #include <sys/cmn_err.h>
  52 #include <sys/acl.h>
  53 #include <sys/utsname.h>
  54 #include <sys/sdt.h>
  55 #include <netinet/in.h>
  56 #include <sys/avl.h>
  57 
  58 #include <rpc/types.h>
  59 #include <rpc/auth.h>
  60 #include <rpc/svc.h>
  61 
  62 #include <nfs/nfs.h>
  63 #include <nfs/export.h>
  64 #include <nfs/nfssys.h>
  65 #include <nfs/nfs_clnt.h>
  66 #include <nfs/nfs_acl.h>
  67 #include <nfs/nfs_log.h>
  68 #include <nfs/lm.h>
  69 #include <sys/sunddi.h>
  70 
  71 /*
  72  * exi_id support
  73  *
  74  * exi_id_next          The next exi_id available.
  75  * exi_id_overflow      The exi_id_next already overflowed, so we should
  76  *                      thoroughly check for duplicates.
  77  * exi_id_tree          AVL tree indexed by exi_id.
  78  * nfs_exi_id_lock      Lock to protect the export ID list
  79  *
  80  * All exi_id_next, exi_id_overflow, and exi_id_tree are protected by
  81  * nfs_exi_id_lock.
  82  */
  83 static int exi_id_next;
  84 static bool_t exi_id_overflow;
  85 avl_tree_t exi_id_tree;
  86 kmutex_t nfs_exi_id_lock;
  87 
  88 static int      unexport(nfs_export_t *, exportinfo_t *);
  89 static void     exportfree(exportinfo_t *);
  90 static int      loadindex(exportdata_t *);
  91 
  92 extern void     nfsauth_cache_free(exportinfo_t *);
  93 extern int      sec_svc_loadrootnames(int, int, caddr_t **, model_t);
  94 extern void     sec_svc_freerootnames(int, int, caddr_t *);
  95 
  96 static int      build_seclist_nodups(exportdata_t *, secinfo_t *, int);
  97 static void     srv_secinfo_add(secinfo_t **, int *, secinfo_t *, int, int);
  98 static void     srv_secinfo_remove(secinfo_t **, int *, secinfo_t *, int);
  99 static void     srv_secinfo_treeclimb(nfs_export_t *, exportinfo_t *,
 100                     secinfo_t *, int, bool_t);
 101 
 102 #ifdef VOLATILE_FH_TEST
 103 static struct ex_vol_rename *find_volrnm_fh(exportinfo_t *, nfs_fh4 *);
 104 static uint32_t find_volrnm_fh_id(exportinfo_t *, nfs_fh4 *);
 105 static void     free_volrnm_list(exportinfo_t *);
 106 #endif /* VOLATILE_FH_TEST */
 107 
 108 fhandle_t nullfh2;      /* for comparing V2 filehandles */
 109 
 110 /*
 111  * macro for static dtrace probes to trace server namespace ref count mods.
 112  */
 113 #define SECREF_TRACE(seclist, tag, flav, aftcnt) \
 114         DTRACE_PROBE4(nfss__i__nmspc__secref, struct secinfo *, (seclist), \
 115                 char *, (tag), int, (int)(flav), int, (int)(aftcnt))
 116 
 117 
 118 #define exptablehash(fsid, fid) (nfs_fhhash((fsid), (fid)) & (EXPTABLESIZE - 1))
 119 
 120 extern nfs_export_t *
 121 nfs_get_export(void)
 122 {
 123         nfs_globals_t *ng = zone_getspecific(nfssrv_zone_key, curzone);
 124         nfs_export_t *ne = ng->nfs_export;
 125         ASSERT(ne != NULL);
 126         return (ne);
 127 }
 128 
 129 static uint8_t
 130 xor_hash(uint8_t *data, int len)
 131 {
 132         uint8_t h = 0;
 133 
 134         while (len--)
 135                 h ^= *data++;
 136 
 137         return (h);
 138 }
 139 
 140 /*
 141  * File handle hash function, XOR over all bytes in fsid and fid.
 142  */
 143 static unsigned
 144 nfs_fhhash(fsid_t *fsid, fid_t *fid)
 145 {
 146         int len;
 147         uint8_t h;
 148 
 149         h = xor_hash((uint8_t *)fsid, sizeof (fsid_t));
 150 
 151         /*
 152          * Sanity check the length before using it
 153          * blindly in case the client trashed it.
 154          */
 155         len = fid->fid_len > NFS_FH4MAXDATA ? 0 : fid->fid_len;
 156         h ^= xor_hash((uint8_t *)fid->fid_data, len);
 157 
 158         return ((unsigned)h);
 159 }
 160 
 161 /*
 162  * Free the memory allocated within a secinfo entry.
 163  */
 164 void
 165 srv_secinfo_entry_free(struct secinfo *secp)
 166 {
 167         if (secp->s_rootcnt > 0 && secp->s_rootnames != NULL) {
 168                 sec_svc_freerootnames(secp->s_secinfo.sc_rpcnum,
 169                     secp->s_rootcnt, secp->s_rootnames);
 170                 secp->s_rootcnt = 0;
 171         }
 172 
 173         if ((secp->s_secinfo.sc_rpcnum == RPCSEC_GSS) &&
 174             (secp->s_secinfo.sc_gss_mech_type)) {
 175                 kmem_free(secp->s_secinfo.sc_gss_mech_type->elements,
 176                     secp->s_secinfo.sc_gss_mech_type->length);
 177                 kmem_free(secp->s_secinfo.sc_gss_mech_type,
 178                     sizeof (rpc_gss_OID_desc));
 179                 secp->s_secinfo.sc_gss_mech_type = NULL;
 180         }
 181 }
 182 
 183 /*
 184  * Free a list of secinfo allocated in the exportdata structure.
 185  */
 186 void
 187 srv_secinfo_list_free(struct secinfo *secinfo, int cnt)
 188 {
 189         int i;
 190 
 191         if (cnt == 0)
 192                 return;
 193 
 194         for (i = 0; i < cnt; i++)
 195                 srv_secinfo_entry_free(&secinfo[i]);
 196 
 197         kmem_free(secinfo, cnt * sizeof (struct secinfo));
 198 }
 199 
 200 /*
 201  * Allocate and copy a secinfo data from "from" to "to".
 202  *
 203  * This routine is used by srv_secinfo_add() to add a new flavor to an
 204  * ancestor's export node. The rootnames are not copied because the
 205  * allowable rootname access only applies to the explicit exported node,
 206  * not its ancestor's.
 207  *
 208  * "to" should have already been allocated and zeroed before calling
 209  * this routine.
 210  *
 211  * This routine is used under the protection of exported_lock (RW_WRITER).
 212  */
 213 void
 214 srv_secinfo_copy(struct secinfo *from, struct secinfo *to)
 215 {
 216         to->s_secinfo.sc_nfsnum = from->s_secinfo.sc_nfsnum;
 217         to->s_secinfo.sc_rpcnum = from->s_secinfo.sc_rpcnum;
 218 
 219         if (from->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
 220                 to->s_secinfo.sc_service = from->s_secinfo.sc_service;
 221                 bcopy(from->s_secinfo.sc_name, to->s_secinfo.sc_name,
 222                     strlen(from->s_secinfo.sc_name));
 223                 bcopy(from->s_secinfo.sc_gss_mech, to->s_secinfo.sc_gss_mech,
 224                     strlen(from->s_secinfo.sc_gss_mech));
 225 
 226                 /* copy mechanism oid */
 227                 to->s_secinfo.sc_gss_mech_type =
 228                     kmem_alloc(sizeof (rpc_gss_OID_desc), KM_SLEEP);
 229                 to->s_secinfo.sc_gss_mech_type->length =
 230                     from->s_secinfo.sc_gss_mech_type->length;
 231                 to->s_secinfo.sc_gss_mech_type->elements =
 232                     kmem_alloc(from->s_secinfo.sc_gss_mech_type->length,
 233                     KM_SLEEP);
 234                 bcopy(from->s_secinfo.sc_gss_mech_type->elements,
 235                     to->s_secinfo.sc_gss_mech_type->elements,
 236                     from->s_secinfo.sc_gss_mech_type->length);
 237         }
 238 
 239         to->s_refcnt = from->s_refcnt;
 240         to->s_window = from->s_window;
 241         /* no need to copy the mode bits - s_flags */
 242 }
 243 
 244 /*
 245  * Create a secinfo array without duplicates.  The condensed
 246  * flavor list is used to propagate flavor ref counts  to an
 247  * export's ancestor pseudonodes.
 248  */
 249 static int
 250 build_seclist_nodups(exportdata_t *exd, secinfo_t *nodups, int exponly)
 251 {
 252         int ccnt, c;
 253         int ncnt, n;
 254         struct secinfo *cursec;
 255 
 256         ncnt = 0;
 257         ccnt = exd->ex_seccnt;
 258         cursec = exd->ex_secinfo;
 259 
 260         for (c = 0; c < ccnt; c++) {
 261 
 262                 if (exponly && ! SEC_REF_EXPORTED(&cursec[c]))
 263                         continue;
 264 
 265                 for (n = 0; n < ncnt; n++) {
 266                         if (nodups[n].s_secinfo.sc_nfsnum ==
 267                             cursec[c].s_secinfo.sc_nfsnum)
 268                                 break;
 269                 }
 270 
 271                 /*
 272                  * The structure copy below also copys ptrs embedded
 273                  * within struct secinfo.  The ptrs are copied but
 274                  * they are never freed from the nodups array.  If
 275                  * an ancestor's secinfo array doesn't contain one
 276                  * of the nodups flavors, then the entry is properly
 277                  * copied into the ancestor's secinfo array.
 278                  * (see srv_secinfo_copy)
 279                  */
 280                 if (n == ncnt) {
 281                         nodups[n] = cursec[c];
 282                         ncnt++;
 283                 }
 284         }
 285         return (ncnt);
 286 }
 287 
 288 /*
 289  * Add the new security flavors from newdata to the current list, pcursec.
 290  * Upon return, *pcursec has the newly merged secinfo list.
 291  *
 292  * There should be at least 1 secinfo entry in newsec.
 293  *
 294  * This routine is used under the protection of exported_lock (RW_WRITER).
 295  */
 296 static void
 297 srv_secinfo_add(secinfo_t **pcursec, int *pcurcnt, secinfo_t *newsec,
 298     int newcnt, int is_pseudo)
 299 {
 300         int ccnt, c;            /* sec count in current data - curdata */
 301         int n;                  /* index for newsec  - newsecinfo */
 302         int tcnt;               /* total sec count after merge */
 303         int mcnt;               /* total sec count after merge */
 304         struct secinfo *msec;   /* merged secinfo list */
 305         struct secinfo *cursec;
 306 
 307         cursec = *pcursec;
 308         ccnt = *pcurcnt;
 309 
 310         ASSERT(newcnt > 0);
 311         tcnt = ccnt + newcnt;
 312 
 313         for (n = 0; n < newcnt; n++) {
 314                 for (c = 0; c < ccnt; c++) {
 315                         if (newsec[n].s_secinfo.sc_nfsnum ==
 316                             cursec[c].s_secinfo.sc_nfsnum) {
 317                                 cursec[c].s_refcnt += newsec[n].s_refcnt;
 318                                 SECREF_TRACE(cursec, "add_ref",
 319                                     cursec[c].s_secinfo.sc_nfsnum,
 320                                     cursec[c].s_refcnt);
 321                                 tcnt--;
 322                                 break;
 323                         }
 324                 }
 325         }
 326 
 327         if (tcnt == ccnt)
 328                 return; /* no change; no new flavors */
 329 
 330         msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
 331 
 332         /* move current secinfo list data to the new list */
 333         for (c = 0; c < ccnt; c++)
 334                 msec[c] = cursec[c];
 335 
 336         /* Add the flavor that's not in the current data */
 337         mcnt = ccnt;
 338         for (n = 0; n < newcnt; n++) {
 339                 for (c = 0; c < ccnt; c++) {
 340                         if (newsec[n].s_secinfo.sc_nfsnum ==
 341                             cursec[c].s_secinfo.sc_nfsnum)
 342                                 break;
 343                 }
 344 
 345                 /* This is the one. Add it. */
 346                 if (c == ccnt) {
 347                         srv_secinfo_copy(&newsec[n], &msec[mcnt]);
 348 
 349                         if (is_pseudo)
 350                                 msec[mcnt].s_flags = M_RO;
 351 
 352                         SECREF_TRACE(msec, "new_ref",
 353                             msec[mcnt].s_secinfo.sc_nfsnum,
 354                             msec[mcnt].s_refcnt);
 355                         mcnt++;
 356                 }
 357         }
 358 
 359         ASSERT(mcnt == tcnt);
 360 
 361         /*
 362          * Done. Update curdata. Free the old secinfo list in
 363          * curdata and return the new sec array info
 364          */
 365         if (ccnt > 0)
 366                 kmem_free(cursec, ccnt * sizeof (struct secinfo));
 367         *pcurcnt = tcnt;
 368         *pcursec = msec;
 369 }
 370 
 371 /*
 372  * For NFS V4.
 373  * Remove the security data of the unexported node from its ancestors.
 374  * Assume there is at least one flavor entry in the current sec list
 375  * (pcursec).
 376  *
 377  * This routine is used under the protection of exported_lock (RW_WRITER).
 378  *
 379  * Every element of remsec is an explicitly exported flavor.  If
 380  * srv_secinfo_remove() is called fom an exportfs error path, then
 381  * the flavor list was derived from the user's share cmdline,
 382  * and all flavors are explicit.  If it was called from the unshare path,
 383  * build_seclist_nodups() was called with the exponly flag.
 384  */
 385 static void
 386 srv_secinfo_remove(secinfo_t **pcursec, int *pcurcnt, secinfo_t *remsec,
 387     int remcnt)
 388 {
 389         int ccnt, c;            /* sec count in current data - cursec */
 390         int r;                  /* sec count in removal data - remsec */
 391         int tcnt, mcnt;         /* total sec count after removing */
 392         struct secinfo *msec;   /* final secinfo list after removing */
 393         struct secinfo *cursec;
 394 
 395         cursec = *pcursec;
 396         ccnt = *pcurcnt;
 397         tcnt = ccnt;
 398 
 399         for (r = 0; r < remcnt; r++) {
 400                 /*
 401                  * At unshare/reshare time, only explicitly shared flavor ref
 402                  * counts are decremented and propagated to ancestors.
 403                  * Implicit flavor refs came from shared descendants, and
 404                  * they must be kept.
 405                  */
 406                 if (! SEC_REF_EXPORTED(&remsec[r]))
 407                         continue;
 408 
 409                 for (c = 0; c < ccnt; c++) {
 410                         if (remsec[r].s_secinfo.sc_nfsnum ==
 411                             cursec[c].s_secinfo.sc_nfsnum) {
 412 
 413                                 /*
 414                                  * Decrement secinfo reference count by 1.
 415                                  * If this entry is invalid after decrementing
 416                                  * the count (i.e. count < 1), this entry will
 417                                  * be removed.
 418                                  */
 419                                 cursec[c].s_refcnt--;
 420 
 421                                 SECREF_TRACE(cursec, "del_ref",
 422                                     cursec[c].s_secinfo.sc_nfsnum,
 423                                     cursec[c].s_refcnt);
 424 
 425                                 ASSERT(cursec[c].s_refcnt >= 0);
 426 
 427                                 if (SEC_REF_INVALID(&cursec[c]))
 428                                         tcnt--;
 429                                 break;
 430                         }
 431                 }
 432         }
 433 
 434         ASSERT(tcnt >= 0);
 435         if (tcnt == ccnt)
 436                 return; /* no change; no flavors to remove */
 437 
 438         if (tcnt == 0) {
 439                 srv_secinfo_list_free(cursec, ccnt);
 440                 *pcurcnt = 0;
 441                 *pcursec = NULL;
 442                 return;
 443         }
 444 
 445         msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
 446 
 447         /* walk thru the given secinfo list to remove the flavors */
 448         mcnt = 0;
 449         for (c = 0; c < ccnt; c++) {
 450                 if (SEC_REF_INVALID(&cursec[c])) {
 451                         srv_secinfo_entry_free(&cursec[c]);
 452                 } else {
 453                         msec[mcnt] = cursec[c];
 454                         mcnt++;
 455                 }
 456         }
 457 
 458         ASSERT(mcnt == tcnt);
 459         /*
 460          * Done. Update curdata.
 461          * Free the existing secinfo list in curdata. All pointers
 462          * within the list have either been moved to msec or freed
 463          * if it's invalid.
 464          */
 465         kmem_free(*pcursec, ccnt * sizeof (struct secinfo));
 466         *pcursec = msec;
 467         *pcurcnt = tcnt;
 468 }
 469 
 470 
 471 /*
 472  * For the reshare case, sec flavor accounting happens in 3 steps:
 473  * 1) propagate addition of new flavor refs up the ancestor tree
 474  * 2) transfer flavor refs of descendants to new/reshared exportdata
 475  * 3) propagate removal of old flavor refs up the ancestor tree
 476  *
 477  * srv_secinfo_exp2exp() implements step 2 of a reshare.  At this point,
 478  * the new flavor list has already been propagated up through the
 479  * ancestor tree via srv_secinfo_treeclimb().
 480  *
 481  * If there is more than 1 export reference to an old flavor (i.e. some
 482  * of its children shared with this flavor), this flavor information
 483  * needs to be transferred to the new exportdata struct.  A flavor in
 484  * the old exportdata has descendant refs when its s_refcnt > 1 or it
 485  * is implicitly shared (M_SEC4_EXPORTED not set in s_flags).
 486  *
 487  * SEC_REF_EXPORTED() is only true when  M_SEC4_EXPORTED is set
 488  * SEC_REF_SELF() is only true when both M_SEC4_EXPORTED is set and s_refcnt==1
 489  *
 490  * Transferring descendant flavor refcnts happens in 2 passes:
 491  * a) flavors used before (oldsecinfo) and after (curdata->ex_secinfo) reshare
 492  * b) flavors used before but not after reshare
 493  *
 494  * This routine is used under the protection of exported_lock (RW_WRITER).
 495  */
 496 void
 497 srv_secinfo_exp2exp(exportdata_t *curdata, secinfo_t *oldsecinfo, int ocnt)
 498 {
 499         int ccnt, c;            /* sec count in current data - curdata */
 500         int o;                  /* sec count in old data - oldsecinfo */
 501         int tcnt, mcnt;         /* total sec count after the transfer */
 502         struct secinfo *msec;   /* merged secinfo list */
 503 
 504         ccnt = curdata->ex_seccnt;
 505 
 506         ASSERT(ocnt > 0);
 507         ASSERT(!(curdata->ex_flags & EX_PSEUDO));
 508 
 509         /*
 510          * If the oldsecinfo has flavors with more than 1 reference count
 511          * and the flavor is specified in the reshare, transfer the flavor
 512          * refs to the new seclist (curdata.ex_secinfo).
 513          */
 514         tcnt = ccnt + ocnt;
 515 
 516         for (o = 0; o < ocnt; o++) {
 517 
 518                 if (SEC_REF_SELF(&oldsecinfo[o])) {
 519                         tcnt--;
 520                         continue;
 521                 }
 522 
 523                 for (c = 0; c < ccnt; c++) {
 524                         if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
 525                             curdata->ex_secinfo[c].s_secinfo.sc_nfsnum) {
 526 
 527                                 /*
 528                                  * add old reference to the current
 529                                  * secinfo count
 530                                  */
 531                                 curdata->ex_secinfo[c].s_refcnt +=
 532                                     oldsecinfo[o].s_refcnt;
 533 
 534                                 /*
 535                                  * Delete the old export flavor
 536                                  * reference.  The initial reference
 537                                  * was created during srv_secinfo_add,
 538                                  * and the count is decremented below
 539                                  * to account for the initial reference.
 540                                  */
 541                                 if (SEC_REF_EXPORTED(&oldsecinfo[o]))
 542                                         curdata->ex_secinfo[c].s_refcnt--;
 543 
 544                                 SECREF_TRACE(curdata->ex_path,
 545                                     "reshare_xfer_common_child_refs",
 546                                     curdata->ex_secinfo[c].s_secinfo.sc_nfsnum,
 547                                     curdata->ex_secinfo[c].s_refcnt);
 548 
 549                                 ASSERT(curdata->ex_secinfo[c].s_refcnt >= 0);
 550 
 551                                 tcnt--;
 552                                 break;
 553                         }
 554                 }
 555         }
 556 
 557         if (tcnt == ccnt)
 558                 return; /* no more transfer to do */
 559 
 560         /*
 561          * oldsecinfo has flavors referenced by its children that are not
 562          * in the current (new) export flavor list.  Add these flavors.
 563          */
 564         msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
 565 
 566         /* move current secinfo list data to the new list */
 567         for (c = 0; c < ccnt; c++)
 568                 msec[c] = curdata->ex_secinfo[c];
 569 
 570         /*
 571          * Add the flavor that's not in the new export, but still
 572          * referenced by its children.
 573          */
 574         mcnt = ccnt;
 575         for (o = 0; o < ocnt; o++) {
 576                 if (! SEC_REF_SELF(&oldsecinfo[o])) {
 577                         for (c = 0; c < ccnt; c++) {
 578                                 if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
 579                                     curdata->ex_secinfo[c].s_secinfo.sc_nfsnum)
 580                                         break;
 581                         }
 582 
 583                         /*
 584                          * This is the one. Add it. Decrement the ref count
 585                          * by 1 if the flavor is an explicitly shared flavor
 586                          * for the oldsecinfo export node.
 587                          */
 588                         if (c == ccnt) {
 589                                 srv_secinfo_copy(&oldsecinfo[o], &msec[mcnt]);
 590                                 if (SEC_REF_EXPORTED(&oldsecinfo[o]))
 591                                         msec[mcnt].s_refcnt--;
 592 
 593                                 SECREF_TRACE(curdata,
 594                                     "reshare_xfer_implicit_child_refs",
 595                                     msec[mcnt].s_secinfo.sc_nfsnum,
 596                                     msec[mcnt].s_refcnt);
 597 
 598                                 ASSERT(msec[mcnt].s_refcnt >= 0);
 599                                 mcnt++;
 600                         }
 601                 }
 602         }
 603 
 604         ASSERT(mcnt == tcnt);
 605         /*
 606          * Done. Update curdata, free the existing secinfo list in
 607          * curdata and set the new value.
 608          */
 609         if (ccnt > 0)
 610                 kmem_free(curdata->ex_secinfo, ccnt * sizeof (struct secinfo));
 611         curdata->ex_seccnt = tcnt;
 612         curdata->ex_secinfo = msec;
 613 }
 614 
 615 /*
 616  * When unsharing an old export node and the old node becomes a pseudo node,
 617  * if there is more than 1 export reference to an old flavor (i.e. some of
 618  * its children shared with this flavor), this flavor information needs to
 619  * be transferred to the new shared node.
 620  *
 621  * This routine is used under the protection of exported_lock (RW_WRITER).
 622  */
 623 void
 624 srv_secinfo_exp2pseu(exportdata_t *curdata, exportdata_t *olddata)
 625 {
 626         int ocnt, o;            /* sec count in transfer data - trandata */
 627         int tcnt, mcnt;         /* total sec count after transfer */
 628         struct secinfo *msec;   /* merged secinfo list */
 629 
 630         ASSERT(curdata->ex_flags & EX_PSEUDO);
 631         ASSERT(curdata->ex_seccnt == 0);
 632 
 633         ocnt = olddata->ex_seccnt;
 634 
 635         /*
 636          * If the olddata has flavors with more than 1 reference count,
 637          * transfer the information to the curdata.
 638          */
 639         tcnt = ocnt;
 640 
 641         for (o = 0; o < ocnt; o++) {
 642                 if (SEC_REF_SELF(&olddata->ex_secinfo[o]))
 643                         tcnt--;
 644         }
 645 
 646         if (tcnt == 0)
 647                 return; /* no transfer to do */
 648 
 649         msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
 650 
 651         mcnt = 0;
 652         for (o = 0; o < ocnt; o++) {
 653                 if (! SEC_REF_SELF(&olddata->ex_secinfo[o])) {
 654 
 655                         /*
 656                          * Decrement the reference count by 1 if the flavor is
 657                          * an explicitly shared flavor for the olddata export
 658                          * node.
 659                          */
 660                         srv_secinfo_copy(&olddata->ex_secinfo[o], &msec[mcnt]);
 661                         msec[mcnt].s_flags = M_RO;
 662                         if (SEC_REF_EXPORTED(&olddata->ex_secinfo[o]))
 663                                 msec[mcnt].s_refcnt--;
 664 
 665                         SECREF_TRACE(curdata, "unshare_morph_pseudo",
 666                             msec[mcnt].s_secinfo.sc_nfsnum,
 667                             msec[mcnt].s_refcnt);
 668 
 669                         ASSERT(msec[mcnt].s_refcnt >= 0);
 670                         mcnt++;
 671                 }
 672         }
 673 
 674         ASSERT(mcnt == tcnt);
 675         /*
 676          * Done. Update curdata.
 677          * Free up the existing secinfo list in curdata and
 678          * set the new value.
 679          */
 680         curdata->ex_seccnt = tcnt;
 681         curdata->ex_secinfo = msec;
 682 }
 683 
 684 /*
 685  * Find for given treenode the exportinfo which has its
 686  * exp_visible linked on its exi_visible list.
 687  *
 688  * Note: We could add new pointer either to treenode or
 689  * to exp_visible, which will point there directly.
 690  * This would buy some speed for some memory.
 691  */
 692 exportinfo_t *
 693 vis2exi(treenode_t *tnode)
 694 {
 695         exportinfo_t *exi_ret = NULL;
 696 
 697         for (;;) {
 698                 tnode = tnode->tree_parent;
 699                 if (TREE_ROOT(tnode)) {
 700                         exi_ret = tnode->tree_exi;
 701                         break;
 702                 }
 703         }
 704 
 705         ASSERT(exi_ret); /* Every visible should have its home exportinfo */
 706         return (exi_ret);
 707 }
 708 
 709 /*
 710  * For NFS V4.
 711  * Add or remove the newly exported or unexported security flavors of the
 712  * given exportinfo from its ancestors upto the system root.
 713  */
 714 void
 715 srv_secinfo_treeclimb(nfs_export_t *ne, exportinfo_t *exip, secinfo_t *sec,
 716     int seccnt, bool_t isadd)
 717 {
 718         treenode_t *tnode;
 719 
 720         ASSERT(RW_WRITE_HELD(&ne->exported_lock));
 721 
 722         /*
 723          * exi_tree can be null for the zone root
 724          * which means we're already at the "top"
 725          * and there's nothing more to "climb".
 726          */
 727         tnode = exip->exi_tree;
 728         if (tnode == NULL) {
 729                 /* Should only happen for... */
 730                 ASSERT(exip == ne->exi_root);
 731                 return;
 732         }
 733 
 734         if (seccnt == 0)
 735                 return;
 736 
 737         /*
 738          * If flavors are being added and the new export root isn't
 739          * also VROOT, its implicitly allowed flavors are inherited from
 740          * its pseudonode.
 741          * Note - for VROOT exports the implicitly allowed flavors were
 742          * transferred from the PSEUDO export in exportfs()
 743          */
 744         if (isadd && !(exip->exi_vp->v_flag & VROOT) &&
 745             tnode->tree_vis->vis_seccnt > 0) {
 746                 srv_secinfo_add(&exip->exi_export.ex_secinfo,
 747                     &exip->exi_export.ex_seccnt, tnode->tree_vis->vis_secinfo,
 748                     tnode->tree_vis->vis_seccnt, FALSE);
 749         }
 750 
 751         /*
 752          * Move to parent node and propagate sec flavor
 753          * to exportinfo and to visible structures.
 754          */
 755         tnode = tnode->tree_parent;
 756 
 757         while (tnode != NULL) {
 758 
 759                 /* If there is exportinfo, update it */
 760                 if (tnode->tree_exi != NULL) {
 761                         secinfo_t **pxsec =
 762                             &tnode->tree_exi->exi_export.ex_secinfo;
 763                         int *pxcnt = &tnode->tree_exi->exi_export.ex_seccnt;
 764                         int is_pseudo = PSEUDO(tnode->tree_exi);
 765                         if (isadd)
 766                                 srv_secinfo_add(pxsec, pxcnt, sec, seccnt,
 767                                     is_pseudo);
 768                         else
 769                                 srv_secinfo_remove(pxsec, pxcnt, sec, seccnt);
 770                 }
 771 
 772                 /* Update every visible - only root node has no visible */
 773                 if (tnode->tree_vis != NULL) {
 774                         secinfo_t **pxsec = &tnode->tree_vis->vis_secinfo;
 775                         int *pxcnt = &tnode->tree_vis->vis_seccnt;
 776                         if (isadd)
 777                                 srv_secinfo_add(pxsec, pxcnt, sec, seccnt,
 778                                     FALSE);
 779                         else
 780                                 srv_secinfo_remove(pxsec, pxcnt, sec, seccnt);
 781                 }
 782                 tnode = tnode->tree_parent;
 783         }
 784 }
 785 
 786 /* hash_name is a text substitution for either fid_hash or path_hash */
 787 #define exp_hash_unlink(exi, hash_name) \
 788         if (*(exi)->hash_name.bckt == (exi)) \
 789                 *(exi)->hash_name.bckt = (exi)->hash_name.next; \
 790         if ((exi)->hash_name.prev) \
 791                 (exi)->hash_name.prev->hash_name.next = (exi)->hash_name.next; \
 792         if ((exi)->hash_name.next) \
 793                 (exi)->hash_name.next->hash_name.prev = (exi)->hash_name.prev; \
 794         (exi)->hash_name.bckt = NULL;
 795 
 796 #define exp_hash_link(exi, hash_name, bucket) \
 797         (exi)->hash_name.bckt = (bucket); \
 798         (exi)->hash_name.prev = NULL; \
 799         (exi)->hash_name.next = *(bucket); \
 800         if ((exi)->hash_name.next) \
 801                 (exi)->hash_name.next->hash_name.prev = (exi); \
 802         *(bucket) = (exi);
 803 
 804 void
 805 export_link(nfs_export_t *ne, exportinfo_t *exi)
 806 {
 807         exportinfo_t **bckt;
 808 
 809         ASSERT(RW_WRITE_HELD(&ne->exported_lock));
 810         ASSERT(exi->exi_zoneid == ne->ne_globals->nfs_zoneid);
 811 
 812         bckt = &ne->exptable[exptablehash(&exi->exi_fsid, &exi->exi_fid)];
 813         exp_hash_link(exi, fid_hash, bckt);
 814 
 815         bckt = &ne->exptable_path_hash[pkp_tab_hash(exi->exi_export.ex_path,
 816             strlen(exi->exi_export.ex_path))];
 817         exp_hash_link(exi, path_hash, bckt);
 818 }
 819 
 820 /*
 821  * Helper functions for exi_id handling
 822  */
 823 static int
 824 exi_id_compar(const void *v1, const void *v2)
 825 {
 826         const struct exportinfo *e1 = v1;
 827         const struct exportinfo *e2 = v2;
 828 
 829         if (e1->exi_id < e2->exi_id)
 830                 return (-1);
 831         if (e1->exi_id > e2->exi_id)
 832                 return (1);
 833 
 834         return (0);
 835 }
 836 
 837 int
 838 exi_id_get_next()
 839 {
 840         struct exportinfo e;
 841         int ret = exi_id_next;
 842 
 843         ASSERT(MUTEX_HELD(&nfs_exi_id_lock));
 844 
 845         do {
 846                 exi_id_next++;
 847                 if (exi_id_next == 0)
 848                         exi_id_overflow = TRUE;
 849 
 850                 if (!exi_id_overflow)
 851                         break;
 852 
 853                 if (exi_id_next == ret)
 854                         cmn_err(CE_PANIC, "exi_id exhausted");
 855 
 856                 e.exi_id = exi_id_next;
 857         } while (avl_find(&exi_id_tree, &e, NULL) != NULL);
 858 
 859         return (ret);
 860 }
 861 
 862 /*
 863  * Get the root file handle for this zone.
 864  * Called when nfs_svc() starts
 865  */
 866 int
 867 nfs_export_get_rootfh(nfs_globals_t *g)
 868 {
 869         nfs_export_t *ne = g->nfs_export;
 870         int err;
 871 
 872         ne->exi_rootfid.fid_len = MAXFIDSZ;
 873         err = vop_fid_pseudo(ne->exi_root->exi_vp, &ne->exi_rootfid);
 874         if (err != 0) {
 875                 ne->exi_rootfid.fid_len = 0;
 876                 return (err);
 877         }
 878 
 879         /* Setup the fhandle template exi_fh */
 880         ne->exi_root->exi_fh.fh_fsid = rootdir->v_vfsp->vfs_fsid;
 881         ne->exi_root->exi_fh.fh_xlen = ne->exi_rootfid.fid_len;
 882         bcopy(ne->exi_rootfid.fid_data, ne->exi_root->exi_fh.fh_xdata,
 883             ne->exi_rootfid.fid_len);
 884         ne->exi_root->exi_fh.fh_len = sizeof (ne->exi_root->exi_fh.fh_data);
 885 
 886         return (0);
 887 }
 888 
 889 void
 890 nfs_export_zone_init(nfs_globals_t *ng)
 891 {
 892         int i;
 893         nfs_export_t *ne;
 894 
 895         ne = kmem_zalloc(sizeof (*ne), KM_SLEEP);
 896 
 897         rw_init(&ne->exported_lock, NULL, RW_DEFAULT, NULL);
 898 
 899         ne->ne_globals = ng; /* "up" pointer */
 900 
 901         /*
 902          * Allocate the place holder for the public file handle, which
 903          * is all zeroes. It is initially set to the root filesystem.
 904          */
 905         ne->exi_root = kmem_zalloc(sizeof (*ne->exi_root), KM_SLEEP);
 906         ne->exi_public = ne->exi_root;
 907 
 908         ne->exi_root->exi_export.ex_flags = EX_PUBLIC;
 909         ne->exi_root->exi_export.ex_pathlen = 1;  /* length of "/" */
 910         ne->exi_root->exi_export.ex_path =
 911             kmem_alloc(ne->exi_root->exi_export.ex_pathlen + 1, KM_SLEEP);
 912         ne->exi_root->exi_export.ex_path[0] = '/';
 913         ne->exi_root->exi_export.ex_path[1] = '\0';
 914 
 915         ne->exi_root->exi_count = 1;
 916         mutex_init(&ne->exi_root->exi_lock, NULL, MUTEX_DEFAULT, NULL);
 917 
 918         ne->exi_root->exi_vp = ZONE_ROOTVP();
 919         ne->exi_root->exi_zoneid = ng->nfs_zoneid;
 920 
 921         /*
 922          * Fill in ne->exi_rootfid later, in nfs_export_get_rootfid
 923          * because we can't correctly return errors here.
 924          */
 925 
 926         /* Initialize auth cache and auth cache lock */
 927         for (i = 0; i < AUTH_TABLESIZE; i++) {
 928                 ne->exi_root->exi_cache[i] = kmem_alloc(sizeof (avl_tree_t),
 929                     KM_SLEEP);
 930                 avl_create(ne->exi_root->exi_cache[i],
 931                     nfsauth_cache_clnt_compar, sizeof (struct auth_cache_clnt),
 932                     offsetof(struct auth_cache_clnt, authc_link));
 933         }
 934         rw_init(&ne->exi_root->exi_cache_lock, NULL, RW_DEFAULT, NULL);
 935 
 936         /* setup exi_fh later, in nfs_export_get_rootfid */
 937 
 938         rw_enter(&ne->exported_lock, RW_WRITER);
 939 
 940         /* Publish the exportinfo in the hash table */
 941         export_link(ne, ne->exi_root);
 942 
 943         /* Initialize exi_id and exi_kstats */
 944         mutex_enter(&nfs_exi_id_lock);
 945         ne->exi_root->exi_id = exi_id_get_next();
 946         avl_add(&exi_id_tree, ne->exi_root);
 947         mutex_exit(&nfs_exi_id_lock);
 948 
 949         rw_exit(&ne->exported_lock);
 950         ne->ns_root = NULL;
 951 
 952         ng->nfs_export = ne;
 953 }
 954 
 955 /*
 956  * During zone shutdown, remove exports
 957  */
 958 void
 959 nfs_export_zone_shutdown(nfs_globals_t *ng)
 960 {
 961         nfs_export_t *ne = ng->nfs_export;
 962         struct exportinfo *exi, *nexi;
 963         int i, errors;
 964 
 965         rw_enter(&ne->exported_lock, RW_READER);
 966 
 967         errors = 0;
 968         for (i = 0; i < EXPTABLESIZE; i++) {
 969 
 970                 exi = ne->exptable[i];
 971                 if (exi != NULL)
 972                         exi_hold(exi);
 973 
 974                 while (exi != NULL) {
 975 
 976                         /*
 977                          * Get and hold next export before
 978                          * dropping the rwlock and unexport
 979                          */
 980                         nexi = exi->fid_hash.next;
 981                         if (nexi != NULL)
 982                                 exi_hold(nexi);
 983 
 984                         rw_exit(&ne->exported_lock);
 985 
 986                         /*
 987                          * Skip ne->exi_root which gets special
 988                          * create/destroy handling.
 989                          */
 990                         if (exi != ne->exi_root &&
 991                             unexport(ne, exi) != 0)
 992                                 errors++;
 993                         exi_rele(exi);
 994 
 995                         rw_enter(&ne->exported_lock, RW_READER);
 996                         exi = nexi;
 997                 }
 998         }
 999         if (errors > 0) {
1000                 cmn_err(CE_NOTE,
1001                     "NFS: failed un-exports in zone %d",
1002                     (int) ng->nfs_zoneid);
1003         }
1004 
1005         rw_exit(&ne->exported_lock);
1006 }
1007 
1008 void
1009 nfs_export_zone_fini(nfs_globals_t *ng)
1010 {
1011         int i;
1012         nfs_export_t *ne = ng->nfs_export;
1013         struct exportinfo *exi;
1014 
1015         ng->nfs_export = NULL;
1016 
1017         rw_enter(&ne->exported_lock, RW_WRITER);
1018 
1019         mutex_enter(&nfs_exi_id_lock);
1020         avl_remove(&exi_id_tree, ne->exi_root);
1021         mutex_exit(&nfs_exi_id_lock);
1022 
1023         export_unlink(ne, ne->exi_root);
1024 
1025         rw_exit(&ne->exported_lock);
1026 
1027         /* Deallocate the place holder for the public file handle */
1028         srv_secinfo_list_free(ne->exi_root->exi_export.ex_secinfo,
1029             ne->exi_root->exi_export.ex_seccnt);
1030         mutex_destroy(&ne->exi_root->exi_lock);
1031 
1032         rw_destroy(&ne->exi_root->exi_cache_lock);
1033         for (i = 0; i < AUTH_TABLESIZE; i++) {
1034                 avl_destroy(ne->exi_root->exi_cache[i]);
1035                 kmem_free(ne->exi_root->exi_cache[i], sizeof (avl_tree_t));
1036         }
1037 
1038         kmem_free(ne->exi_root->exi_export.ex_path,
1039             ne->exi_root->exi_export.ex_pathlen + 1);
1040         kmem_free(ne->exi_root, sizeof (*ne->exi_root));
1041 
1042         /*
1043          * The shutdown hook should have left the exi_id_tree
1044          * with nothing belonging to this zone.
1045          */
1046         mutex_enter(&nfs_exi_id_lock);
1047         i = 0;
1048         exi = avl_first(&exi_id_tree);
1049         while (exi != NULL) {
1050                 if (exi->exi_zoneid == ng->nfs_zoneid)
1051                         i++;
1052                 exi = AVL_NEXT(&exi_id_tree, exi);
1053         }
1054         mutex_exit(&nfs_exi_id_lock);
1055         if (i > 0) {
1056                 cmn_err(CE_NOTE,
1057                     "NFS: zone %d has %d export IDs left after shutdown",
1058                     (int) ng->nfs_zoneid, i);
1059         }
1060         rw_destroy(&ne->exported_lock);
1061         kmem_free(ne, sizeof (*ne));
1062 }
1063 
1064 /*
1065  * Initialization routine for export routines.
1066  * Should only be called once.
1067  */
1068 void
1069 nfs_exportinit(void)
1070 {
1071         mutex_init(&nfs_exi_id_lock, NULL, MUTEX_DEFAULT, NULL);
1072 
1073         /* exi_id handling initialization */
1074         exi_id_next = 0;
1075         exi_id_overflow = FALSE;
1076         avl_create(&exi_id_tree, exi_id_compar, sizeof (struct exportinfo),
1077             offsetof(struct exportinfo, exi_id_link));
1078 
1079         nfslog_init();
1080 }
1081 
1082 /*
1083  * Finalization routine for export routines.
1084  */
1085 void
1086 nfs_exportfini(void)
1087 {
1088         avl_destroy(&exi_id_tree);
1089         mutex_destroy(&nfs_exi_id_lock);
1090 }
1091 
1092 /*
1093  *  Check if 2 gss mechanism identifiers are the same.
1094  *
1095  *  return FALSE if not the same.
1096  *  return TRUE if the same.
1097  */
1098 static bool_t
1099 nfs_mech_equal(rpc_gss_OID mech1, rpc_gss_OID mech2)
1100 {
1101         if ((mech1->length == 0) && (mech2->length == 0))
1102                 return (TRUE);
1103 
1104         if (mech1->length != mech2->length)
1105                 return (FALSE);
1106 
1107         return (bcmp(mech1->elements, mech2->elements, mech1->length) == 0);
1108 }
1109 
1110 /*
1111  *  This routine is used by rpc to map rpc security number
1112  *  to nfs specific security flavor number.
1113  *
1114  *  The gss callback prototype is
1115  *  callback(struct svc_req *, gss_cred_id_t *, gss_ctx_id_t *,
1116  *                              rpc_gss_lock_t *, void **),
1117  *  since nfs does not use the gss_cred_id_t/gss_ctx_id_t arguments
1118  *  we cast them to void.
1119  */
1120 /*ARGSUSED*/
1121 bool_t
1122 rfs_gsscallback(struct svc_req *req, gss_cred_id_t deleg, void *gss_context,
1123     rpc_gss_lock_t *lock, void **cookie)
1124 {
1125         int i, j;
1126         rpc_gss_rawcred_t *raw_cred;
1127         struct exportinfo *exi;
1128         nfs_export_t *ne = nfs_get_export();
1129 
1130         /*
1131          * We don't deal with delegated credentials.
1132          */
1133         if (deleg != GSS_C_NO_CREDENTIAL)
1134                 return (FALSE);
1135 
1136         raw_cred = lock->raw_cred;
1137         *cookie = NULL;
1138 
1139         rw_enter(&ne->exported_lock, RW_READER);
1140 
1141         for (i = 0; i < EXPTABLESIZE; i++) {
1142                 exi = ne->exptable[i];
1143                 while (exi) {
1144                         if (exi->exi_export.ex_seccnt > 0) {
1145                                 struct secinfo *secp;
1146                                 seconfig_t *se;
1147                                 int seccnt;
1148 
1149                                 secp = exi->exi_export.ex_secinfo;
1150                                 seccnt = exi->exi_export.ex_seccnt;
1151                                 for (j = 0; j < seccnt; j++) {
1152                                         /*
1153                                          *  If there is a map of the triplet
1154                                          *  (mechanism, service, qop) between
1155                                          *  raw_cred and the exported flavor,
1156                                          *  get the psudo flavor number.
1157                                          *  Also qop should not be NULL, it
1158                                          *  should be "default" or something
1159                                          *  else.
1160                                          */
1161                                         se = &secp[j].s_secinfo;
1162                                         if ((se->sc_rpcnum == RPCSEC_GSS) &&
1163 
1164                                             (nfs_mech_equal(
1165                                             se->sc_gss_mech_type,
1166                                             raw_cred->mechanism)) &&
1167 
1168                                             (se->sc_service ==
1169                                             raw_cred->service) &&
1170                                             (raw_cred->qop == se->sc_qop)) {
1171 
1172                                                 *cookie = (void *)(uintptr_t)
1173                                                     se->sc_nfsnum;
1174                                                 goto done;
1175                                         }
1176                                 }
1177                         }
1178                         exi = exi->fid_hash.next;
1179                 }
1180         }
1181 done:
1182         rw_exit(&ne->exported_lock);
1183 
1184         /*
1185          * If no nfs pseudo number mapping can be found in the export
1186          * table, assign the nfsflavor to NFS_FLAVOR_NOMAP. In V4, we may
1187          * recover the flavor mismatch from NFS layer (NFS4ERR_WRONGSEC).
1188          *
1189          * For example:
1190          *      server first shares with krb5i;
1191          *      client mounts with krb5i;
1192          *      server re-shares with krb5p;
1193          *      client tries with krb5i, but no mapping can be found;
1194          *      rpcsec_gss module calls this routine to do the mapping,
1195          *              if this routine fails, request is rejected from
1196          *              the rpc layer.
1197          *      What we need is to let the nfs layer rejects the request.
1198          *      For V4, we can reject with NFS4ERR_WRONGSEC and the client
1199          *      may recover from it by getting the new flavor via SECINFO.
1200          *
1201          * nfs pseudo number for RPCSEC_GSS mapping (see nfssec.conf)
1202          * is owned by IANA (see RFC 2623).
1203          *
1204          * XXX NFS_FLAVOR_NOMAP is defined in Solaris to work around
1205          * the implementation issue. This number should not overlap with
1206          * any new IANA defined pseudo flavor numbers.
1207          */
1208         if (*cookie == NULL)
1209                 *cookie = (void *)NFS_FLAVOR_NOMAP;
1210 
1211         lock->locked = TRUE;
1212 
1213         return (TRUE);
1214 }
1215 
1216 
1217 /*
1218  * Exportfs system call; credentials should be checked before
1219  * calling this function.
1220  */
1221 int
1222 exportfs(struct exportfs_args *args, model_t model, cred_t *cr)
1223 {
1224         vnode_t *vp;
1225         vnode_t *dvp;
1226         struct exportdata *kex;
1227         struct exportinfo *exi = NULL;
1228         struct exportinfo *ex, *ex1, *ex2;
1229         fid_t fid;
1230         fsid_t fsid;
1231         int error;
1232         size_t allocsize;
1233         struct secinfo *sp;
1234         struct secinfo *exs;
1235         rpc_gss_callback_t cb;
1236         char *pathbuf;
1237         char *log_buffer;
1238         char *tagbuf;
1239         int callback;
1240         int allocd_seccnt;
1241         STRUCT_HANDLE(exportfs_args, uap);
1242         STRUCT_DECL(exportdata, uexi);
1243         struct secinfo newsec[MAX_FLAVORS];
1244         int newcnt;
1245         struct secinfo oldsec[MAX_FLAVORS];
1246         int oldcnt;
1247         int i;
1248         struct pathname lookpn;
1249         nfs_export_t *ne = nfs_get_export();
1250 
1251         STRUCT_SET_HANDLE(uap, model, args);
1252 
1253         /* Read in pathname from userspace */
1254         if (error = pn_get(STRUCT_FGETP(uap, dname), UIO_USERSPACE, &lookpn))
1255                 return (error);
1256 
1257         /* Walk the export list looking for that pathname */
1258         rw_enter(&ne->exported_lock, RW_READER);
1259         DTRACE_PROBE(nfss__i__exported_lock1_start);
1260         for (ex1 = ne->exptable_path_hash[pkp_tab_hash(lookpn.pn_path,
1261             strlen(lookpn.pn_path))]; ex1; ex1 = ex1->path_hash.next) {
1262                 if (ex1 != ne->exi_root && 0 ==
1263                     strcmp(ex1->exi_export.ex_path, lookpn.pn_path)) {
1264                         exi_hold(ex1);
1265                         break;
1266                 }
1267         }
1268         DTRACE_PROBE(nfss__i__exported_lock1_stop);
1269         rw_exit(&ne->exported_lock);
1270 
1271         /* Is this an unshare? */
1272         if (STRUCT_FGETP(uap, uex) == NULL) {
1273                 pn_free(&lookpn);
1274                 if (ex1 == NULL)
1275                         return (EINVAL);
1276                 error = unexport(ne, ex1);
1277                 exi_rele(ex1);
1278                 return (error);
1279         }
1280 
1281         /* It is a share or a re-share */
1282         error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1283             FOLLOW, &dvp, &vp);
1284         if (error == EINVAL) {
1285                 /*
1286                  * if fname resolves to / we get EINVAL error
1287                  * since we wanted the parent vnode. Try again
1288                  * with NULL dvp.
1289                  */
1290                 error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1291                     FOLLOW, NULL, &vp);
1292                 dvp = NULL;
1293         }
1294         if (!error && vp == NULL) {
1295                 /* Last component of fname not found */
1296                 if (dvp != NULL)
1297                         VN_RELE(dvp);
1298                 error = ENOENT;
1299         }
1300         if (error) {
1301                 pn_free(&lookpn);
1302                 if (ex1)
1303                         exi_rele(ex1);
1304                 return (error);
1305         }
1306 
1307         /*
1308          * 'vp' may be an AUTOFS node, so we perform a
1309          * VOP_ACCESS() to trigger the mount of the
1310          * intended filesystem, so we can share the intended
1311          * filesystem instead of the AUTOFS filesystem.
1312          */
1313         (void) VOP_ACCESS(vp, 0, 0, cr, NULL);
1314 
1315         /*
1316          * We're interested in the top most filesystem.
1317          * This is specially important when uap->dname is a trigger
1318          * AUTOFS node, since we're really interested in sharing the
1319          * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1320          * call not the AUTOFS node itself.
1321          */
1322         if (vn_mountedvfs(vp) != NULL) {
1323                 if (error = traverse(&vp)) {
1324                         VN_RELE(vp);
1325                         if (dvp != NULL)
1326                                 VN_RELE(dvp);
1327                         pn_free(&lookpn);
1328                         if (ex1)
1329                                 exi_rele(ex1);
1330                         return (error);
1331                 }
1332         }
1333 
1334         /* Do not allow sharing another vnode for already shared path */
1335         if (ex1 && !PSEUDO(ex1) && !VN_CMP(ex1->exi_vp, vp)) {
1336                 VN_RELE(vp);
1337                 if (dvp != NULL)
1338                         VN_RELE(dvp);
1339                 pn_free(&lookpn);
1340                 exi_rele(ex1);
1341                 return (EEXIST);
1342         }
1343         if (ex1)
1344                 exi_rele(ex1);
1345 
1346         /*
1347          * Get the vfs id
1348          */
1349         bzero(&fid, sizeof (fid));
1350         fid.fid_len = MAXFIDSZ;
1351         error = VOP_FID(vp, &fid, NULL);
1352         fsid = vp->v_vfsp->vfs_fsid;
1353 
1354         if (error) {
1355                 VN_RELE(vp);
1356                 if (dvp != NULL)
1357                         VN_RELE(dvp);
1358                 /*
1359                  * If VOP_FID returns ENOSPC then the fid supplied
1360                  * is too small.  For now we simply return EREMOTE.
1361                  */
1362                 if (error == ENOSPC)
1363                         error = EREMOTE;
1364                 pn_free(&lookpn);
1365                 return (error);
1366         }
1367 
1368         /*
1369          * Do not allow re-sharing a shared vnode under a different path
1370          * PSEUDO export has ex_path fabricated, e.g. "/tmp (pseudo)", skip it.
1371          */
1372         rw_enter(&ne->exported_lock, RW_READER);
1373         DTRACE_PROBE(nfss__i__exported_lock2_start);
1374         for (ex2 = ne->exptable[exptablehash(&fsid, &fid)]; ex2;
1375             ex2 = ex2->fid_hash.next) {
1376                 if (ex2 != ne->exi_root && !PSEUDO(ex2) &&
1377                     VN_CMP(ex2->exi_vp, vp) &&
1378                     strcmp(ex2->exi_export.ex_path, lookpn.pn_path) != 0) {
1379                         DTRACE_PROBE(nfss__i__exported_lock2_stop);
1380                         rw_exit(&ne->exported_lock);
1381                         VN_RELE(vp);
1382                         if (dvp != NULL)
1383                                 VN_RELE(dvp);
1384                         pn_free(&lookpn);
1385                         return (EEXIST);
1386                 }
1387         }
1388         DTRACE_PROBE(nfss__i__exported_lock2_stop);
1389         rw_exit(&ne->exported_lock);
1390         pn_free(&lookpn);
1391 
1392         exi = kmem_zalloc(sizeof (*exi), KM_SLEEP);
1393         exi->exi_fsid = fsid;
1394         exi->exi_fid = fid;
1395         exi->exi_vp = vp;
1396         exi->exi_count = 1;
1397         exi->exi_zoneid = crgetzoneid(cr);
1398         exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag &
1399             VSW_VOLATILEDEV) ? 1 : 0;
1400         mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1401         exi->exi_dvp = dvp;
1402 
1403         /*
1404          * Initialize auth cache and auth cache lock
1405          */
1406         for (i = 0; i < AUTH_TABLESIZE; i++) {
1407                 exi->exi_cache[i] = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1408                 avl_create(exi->exi_cache[i], nfsauth_cache_clnt_compar,
1409                     sizeof (struct auth_cache_clnt),
1410                     offsetof(struct auth_cache_clnt, authc_link));
1411         }
1412         rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL);
1413 
1414         /*
1415          * Build up the template fhandle
1416          */
1417         exi->exi_fh.fh_fsid = fsid;
1418         if (exi->exi_fid.fid_len > sizeof (exi->exi_fh.fh_xdata)) {
1419                 error = EREMOTE;
1420                 goto out1;
1421         }
1422         exi->exi_fh.fh_xlen = exi->exi_fid.fid_len;
1423         bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata,
1424             exi->exi_fid.fid_len);
1425 
1426         exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data);
1427 
1428         kex = &exi->exi_export;
1429 
1430         /*
1431          * Load in everything, and do sanity checking
1432          */
1433         STRUCT_INIT(uexi, model);
1434         if (copyin(STRUCT_FGETP(uap, uex), STRUCT_BUF(uexi),
1435             STRUCT_SIZE(uexi))) {
1436                 error = EFAULT;
1437                 goto out1;
1438         }
1439 
1440         kex->ex_version = STRUCT_FGET(uexi, ex_version);
1441         if (kex->ex_version != EX_CURRENT_VERSION) {
1442                 error = EINVAL;
1443                 cmn_err(CE_WARN,
1444                     "NFS: exportfs requires export struct version 2 - got %d\n",
1445                     kex->ex_version);
1446                 goto out1;
1447         }
1448 
1449         /*
1450          * Must have at least one security entry
1451          */
1452         kex->ex_seccnt = STRUCT_FGET(uexi, ex_seccnt);
1453         if (kex->ex_seccnt < 1) {
1454                 error = EINVAL;
1455                 goto out1;
1456         }
1457 
1458         kex->ex_path = STRUCT_FGETP(uexi, ex_path);
1459         kex->ex_pathlen = STRUCT_FGET(uexi, ex_pathlen);
1460         kex->ex_flags = STRUCT_FGET(uexi, ex_flags);
1461         kex->ex_anon = STRUCT_FGET(uexi, ex_anon);
1462         kex->ex_secinfo = STRUCT_FGETP(uexi, ex_secinfo);
1463         kex->ex_index = STRUCT_FGETP(uexi, ex_index);
1464         kex->ex_log_buffer = STRUCT_FGETP(uexi, ex_log_buffer);
1465         kex->ex_log_bufferlen = STRUCT_FGET(uexi, ex_log_bufferlen);
1466         kex->ex_tag = STRUCT_FGETP(uexi, ex_tag);
1467         kex->ex_taglen = STRUCT_FGET(uexi, ex_taglen);
1468 
1469         /*
1470          * Copy the exported pathname into
1471          * an appropriately sized buffer.
1472          */
1473         pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1474         if (copyinstr(kex->ex_path, pathbuf, MAXPATHLEN, &kex->ex_pathlen)) {
1475                 kmem_free(pathbuf, MAXPATHLEN);
1476                 error = EFAULT;
1477                 goto out1;
1478         }
1479         kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP);
1480         bcopy(pathbuf, kex->ex_path, kex->ex_pathlen);
1481         kex->ex_path[kex->ex_pathlen] = '\0';
1482         kmem_free(pathbuf, MAXPATHLEN);
1483 
1484         /*
1485          * Get the path to the logging buffer and the tag
1486          */
1487         if (kex->ex_flags & EX_LOG) {
1488                 log_buffer = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1489                 if (copyinstr(kex->ex_log_buffer, log_buffer, MAXPATHLEN,
1490                     &kex->ex_log_bufferlen)) {
1491                         kmem_free(log_buffer, MAXPATHLEN);
1492                         error = EFAULT;
1493                         goto out2;
1494                 }
1495                 kex->ex_log_buffer =
1496                     kmem_alloc(kex->ex_log_bufferlen + 1, KM_SLEEP);
1497                 bcopy(log_buffer, kex->ex_log_buffer, kex->ex_log_bufferlen);
1498                 kex->ex_log_buffer[kex->ex_log_bufferlen] = '\0';
1499                 kmem_free(log_buffer, MAXPATHLEN);
1500 
1501                 tagbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1502                 if (copyinstr(kex->ex_tag, tagbuf, MAXPATHLEN,
1503                     &kex->ex_taglen)) {
1504                         kmem_free(tagbuf, MAXPATHLEN);
1505                         error = EFAULT;
1506                         goto out3;
1507                 }
1508                 kex->ex_tag = kmem_alloc(kex->ex_taglen + 1, KM_SLEEP);
1509                 bcopy(tagbuf, kex->ex_tag, kex->ex_taglen);
1510                 kex->ex_tag[kex->ex_taglen] = '\0';
1511                 kmem_free(tagbuf, MAXPATHLEN);
1512         }
1513 
1514         /*
1515          * Load the security information for each flavor
1516          */
1517         allocsize = kex->ex_seccnt * SIZEOF_STRUCT(secinfo, model);
1518         sp = kmem_zalloc(allocsize, KM_SLEEP);
1519         if (copyin(kex->ex_secinfo, sp, allocsize)) {
1520                 kmem_free(sp, allocsize);
1521                 error = EFAULT;
1522                 goto out4;
1523         }
1524 
1525         /*
1526          * All of these nested structures need to be converted to
1527          * the kernel native format.
1528          */
1529         if (model != DATAMODEL_NATIVE) {
1530                 size_t allocsize2;
1531                 struct secinfo *sp2;
1532 
1533                 allocsize2 = kex->ex_seccnt * sizeof (struct secinfo);
1534                 sp2 = kmem_zalloc(allocsize2, KM_SLEEP);
1535 
1536                 for (i = 0; i < kex->ex_seccnt; i++) {
1537                         STRUCT_HANDLE(secinfo, usi);
1538 
1539                         STRUCT_SET_HANDLE(usi, model,
1540                             (struct secinfo *)((caddr_t)sp +
1541                             (i * SIZEOF_STRUCT(secinfo, model))));
1542                         bcopy(STRUCT_FGET(usi, s_secinfo.sc_name),
1543                             sp2[i].s_secinfo.sc_name, MAX_NAME_LEN);
1544                         sp2[i].s_secinfo.sc_nfsnum =
1545                             STRUCT_FGET(usi, s_secinfo.sc_nfsnum);
1546                         sp2[i].s_secinfo.sc_rpcnum =
1547                             STRUCT_FGET(usi, s_secinfo.sc_rpcnum);
1548                         bcopy(STRUCT_FGET(usi, s_secinfo.sc_gss_mech),
1549                             sp2[i].s_secinfo.sc_gss_mech, MAX_NAME_LEN);
1550                         sp2[i].s_secinfo.sc_gss_mech_type =
1551                             STRUCT_FGETP(usi, s_secinfo.sc_gss_mech_type);
1552                         sp2[i].s_secinfo.sc_qop =
1553                             STRUCT_FGET(usi, s_secinfo.sc_qop);
1554                         sp2[i].s_secinfo.sc_service =
1555                             STRUCT_FGET(usi, s_secinfo.sc_service);
1556 
1557                         sp2[i].s_flags = STRUCT_FGET(usi, s_flags);
1558                         sp2[i].s_window = STRUCT_FGET(usi, s_window);
1559                         sp2[i].s_rootid = STRUCT_FGET(usi, s_rootid);
1560                         sp2[i].s_rootcnt = STRUCT_FGET(usi, s_rootcnt);
1561                         sp2[i].s_rootnames = STRUCT_FGETP(usi, s_rootnames);
1562                 }
1563                 kmem_free(sp, allocsize);
1564                 sp = sp2;
1565                 allocsize = allocsize2;
1566         }
1567 
1568         kex->ex_secinfo = sp;
1569 
1570         /*
1571          * And now copy rootnames for each individual secinfo.
1572          */
1573         callback = 0;
1574         allocd_seccnt = 0;
1575         while (allocd_seccnt < kex->ex_seccnt) {
1576 
1577                 exs = &sp[allocd_seccnt];
1578                 if (exs->s_rootcnt > 0) {
1579                         if (!sec_svc_loadrootnames(exs->s_secinfo.sc_rpcnum,
1580                             exs->s_rootcnt, &exs->s_rootnames, model)) {
1581                                 error = EFAULT;
1582                                 goto out5;
1583                         }
1584                 }
1585 
1586                 if (exs->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
1587                         rpc_gss_OID mech_tmp;
1588                         STRUCT_DECL(rpc_gss_OID_s, umech_tmp);
1589                         caddr_t elements_tmp;
1590 
1591                         /* Copyin mechanism type */
1592                         STRUCT_INIT(umech_tmp, model);
1593                         mech_tmp = kmem_alloc(sizeof (*mech_tmp), KM_SLEEP);
1594                         if (copyin(exs->s_secinfo.sc_gss_mech_type,
1595                             STRUCT_BUF(umech_tmp), STRUCT_SIZE(umech_tmp))) {
1596                                 kmem_free(mech_tmp, sizeof (*mech_tmp));
1597                                 error = EFAULT;
1598                                 goto out5;
1599                         }
1600                         mech_tmp->length = STRUCT_FGET(umech_tmp, length);
1601                         mech_tmp->elements = STRUCT_FGETP(umech_tmp, elements);
1602 
1603                         elements_tmp = kmem_alloc(mech_tmp->length, KM_SLEEP);
1604                         if (copyin(mech_tmp->elements, elements_tmp,
1605                             mech_tmp->length)) {
1606                                 kmem_free(elements_tmp, mech_tmp->length);
1607                                 kmem_free(mech_tmp, sizeof (*mech_tmp));
1608                                 error = EFAULT;
1609                                 goto out5;
1610                         }
1611                         mech_tmp->elements = elements_tmp;
1612                         exs->s_secinfo.sc_gss_mech_type = mech_tmp;
1613                         allocd_seccnt++;
1614 
1615                         callback = 1;
1616                 } else
1617                         allocd_seccnt++;
1618         }
1619 
1620         /*
1621          * Init the secinfo reference count and mark these flavors
1622          * explicitly exported flavors.
1623          */
1624         for (i = 0; i < kex->ex_seccnt; i++) {
1625                 kex->ex_secinfo[i].s_flags |= M_4SEC_EXPORTED;
1626                 kex->ex_secinfo[i].s_refcnt = 1;
1627         }
1628 
1629         /*
1630          *  Set up rpcsec_gss callback routine entry if any.
1631          */
1632         if (callback) {
1633                 cb.callback = rfs_gsscallback;
1634                 cb.program = NFS_ACL_PROGRAM;
1635                 for (cb.version = NFS_ACL_VERSMIN;
1636                     cb.version <= NFS_ACL_VERSMAX; cb.version++) {
1637                         (void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1638                             (void *)&cb);
1639                 }
1640 
1641                 cb.program = NFS_PROGRAM;
1642                 for (cb.version = NFS_VERSMIN;
1643                     cb.version <= NFS_VERSMAX; cb.version++) {
1644                         (void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1645                             (void *)&cb);
1646                 }
1647         }
1648 
1649         /*
1650          * Check the index flag. Do this here to avoid holding the
1651          * lock while dealing with the index option (as we do with
1652          * the public option).
1653          */
1654         if (kex->ex_flags & EX_INDEX) {
1655                 if (!kex->ex_index) {        /* sanity check */
1656                         error = EINVAL;
1657                         goto out5;
1658                 }
1659                 if (error = loadindex(kex))
1660                         goto out5;
1661         }
1662 
1663         if (kex->ex_flags & EX_LOG) {
1664                 if (error = nfslog_setup(exi))
1665                         goto out6;
1666         }
1667 
1668         /*
1669          * Insert the new entry at the front of the export list
1670          */
1671         rw_enter(&ne->exported_lock, RW_WRITER);
1672         DTRACE_PROBE(nfss__i__exported_lock3_start);
1673 
1674         export_link(ne, exi);
1675 
1676         /*
1677          * Check the rest of the list for an old entry for the fs.
1678          * If one is found then unlink it, wait until this is the
1679          * only reference and then free it.
1680          */
1681         for (ex = exi->fid_hash.next; ex != NULL; ex = ex->fid_hash.next) {
1682                 if (ex != ne->exi_root && VN_CMP(ex->exi_vp, vp)) {
1683                         mutex_enter(&nfs_exi_id_lock);
1684                         avl_remove(&exi_id_tree, ex);
1685                         mutex_exit(&nfs_exi_id_lock);
1686                         export_unlink(ne, ex);
1687                         break;
1688                 }
1689         }
1690 
1691         /*
1692          * If the public filehandle is pointing at the
1693          * old entry, then point it back at the root.
1694          */
1695         if (ex != NULL && ex == ne->exi_public)
1696                 ne->exi_public = ne->exi_root;
1697 
1698         /*
1699          * If the public flag is on, make the global exi_public
1700          * point to this entry and turn off the public bit so that
1701          * we can distinguish it from the place holder export.
1702          */
1703         if (kex->ex_flags & EX_PUBLIC) {
1704                 ne->exi_public = exi;
1705                 kex->ex_flags &= ~EX_PUBLIC;
1706         }
1707 
1708 #ifdef VOLATILE_FH_TEST
1709         /*
1710          * Set up the volatile_id value if volatile on share.
1711          * The list of volatile renamed filehandles is always destroyed,
1712          * if the fs was reshared.
1713          */
1714         if (kex->ex_flags & EX_VOLFH)
1715                 exi->exi_volatile_id = gethrestime_sec();
1716 
1717         mutex_init(&exi->exi_vol_rename_lock, NULL, MUTEX_DEFAULT, NULL);
1718 #endif /* VOLATILE_FH_TEST */
1719 
1720         /*
1721          * If this is a new export, then climb up
1722          * the tree and check if any pseudo exports
1723          * need to be created to provide a path for
1724          * NFS v4 clients.
1725          */
1726         if (ex == NULL) {
1727                 error = treeclimb_export(exi);
1728                 if (error)
1729                         goto out7;
1730         } else {
1731                 /* If it's a re-export update namespace tree */
1732                 exi->exi_tree = ex->exi_tree;
1733                 exi->exi_tree->tree_exi = exi;
1734 
1735                 /* Update the change timestamp */
1736                 tree_update_change(ne, exi->exi_tree, NULL);
1737         }
1738 
1739         /*
1740          * build a unique flavor list from the flavors specified
1741          * in the share cmd.  unique means that each flavor only
1742          * appears once in the secinfo list -- no duplicates allowed.
1743          */
1744         newcnt = build_seclist_nodups(&exi->exi_export, newsec, FALSE);
1745 
1746         srv_secinfo_treeclimb(ne, exi, newsec, newcnt, TRUE);
1747 
1748         /*
1749          * If re-sharing an old export entry, update the secinfo data
1750          * depending on if the old entry is a pseudo node or not.
1751          */
1752         if (ex != NULL) {
1753                 oldcnt = build_seclist_nodups(&ex->exi_export, oldsec, FALSE);
1754                 if (PSEUDO(ex)) {
1755                         /*
1756                          * The dir being shared is a pseudo export root (which
1757                          * will be transformed into a real export root).  The
1758                          * flavor(s) of the new share were propagated to the
1759                          * ancestors by srv_secinfo_treeclimb() above.  Now
1760                          * transfer the implicit flavor refs from the old
1761                          * pseudo exprot root to the new (real) export root.
1762                          */
1763                         srv_secinfo_add(&exi->exi_export.ex_secinfo,
1764                             &exi->exi_export.ex_seccnt, oldsec, oldcnt, TRUE);
1765                 } else {
1766                         /*
1767                          * First transfer implicit flavor refs to new export.
1768                          * Remove old flavor refs last.
1769                          */
1770                         srv_secinfo_exp2exp(&exi->exi_export, oldsec, oldcnt);
1771                         srv_secinfo_treeclimb(ne, ex, oldsec, oldcnt, FALSE);
1772                 }
1773         }
1774 
1775         /*
1776          * If it's a re-export and the old entry has a pseudonode list,
1777          * transfer it to the new export.
1778          */
1779         if (ex != NULL && (ex->exi_visible != NULL)) {
1780                 exi->exi_visible = ex->exi_visible;
1781                 ex->exi_visible = NULL;
1782         }
1783 
1784         /*
1785          * Initialize exi_id and exi_kstats
1786          */
1787         if (ex != NULL) {
1788                 exi->exi_id = ex->exi_id;
1789         } else {
1790                 mutex_enter(&nfs_exi_id_lock);
1791                 exi->exi_id = exi_id_get_next();
1792                 mutex_exit(&nfs_exi_id_lock);
1793         }
1794         mutex_enter(&nfs_exi_id_lock);
1795         avl_add(&exi_id_tree, exi);
1796         mutex_exit(&nfs_exi_id_lock);
1797 
1798         DTRACE_PROBE(nfss__i__exported_lock3_stop);
1799         rw_exit(&ne->exported_lock);
1800 
1801         if (ne->exi_public == exi || kex->ex_flags & EX_LOG) {
1802                 /*
1803                  * Log share operation to this buffer only.
1804                  */
1805                 nfslog_share_record(exi, cr);
1806         }
1807 
1808         if (ex != NULL)
1809                 exi_rele(ex);
1810 
1811         return (0);
1812 
1813 out7:
1814         /* Unlink the new export in exptable. */
1815         export_unlink(ne, exi);
1816         DTRACE_PROBE(nfss__i__exported_lock3_stop);
1817         rw_exit(&ne->exported_lock);
1818 out6:
1819         if (kex->ex_flags & EX_INDEX)
1820                 kmem_free(kex->ex_index, strlen(kex->ex_index) + 1);
1821 out5:
1822         /* free partially completed allocation */
1823         while (--allocd_seccnt >= 0) {
1824                 exs = &kex->ex_secinfo[allocd_seccnt];
1825                 srv_secinfo_entry_free(exs);
1826         }
1827 
1828         if (kex->ex_secinfo) {
1829                 kmem_free(kex->ex_secinfo,
1830                     kex->ex_seccnt * sizeof (struct secinfo));
1831         }
1832 
1833 out4:
1834         if ((kex->ex_flags & EX_LOG) && kex->ex_tag != NULL)
1835                 kmem_free(kex->ex_tag, kex->ex_taglen + 1);
1836 out3:
1837         if ((kex->ex_flags & EX_LOG) && kex->ex_log_buffer != NULL)
1838                 kmem_free(kex->ex_log_buffer, kex->ex_log_bufferlen + 1);
1839 out2:
1840         kmem_free(kex->ex_path, kex->ex_pathlen + 1);
1841 out1:
1842         VN_RELE(vp);
1843         if (dvp != NULL)
1844                 VN_RELE(dvp);
1845         mutex_destroy(&exi->exi_lock);
1846         rw_destroy(&exi->exi_cache_lock);
1847         for (i = 0; i < AUTH_TABLESIZE; i++) {
1848                 avl_destroy(exi->exi_cache[i]);
1849                 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
1850         }
1851 
1852         kmem_free(exi, sizeof (*exi));
1853 
1854         return (error);
1855 }
1856 
1857 /*
1858  * Remove the exportinfo from the export list
1859  */
1860 void
1861 export_unlink(nfs_export_t *ne, struct exportinfo *exi)
1862 {
1863         ASSERT(RW_WRITE_HELD(&ne->exported_lock));
1864 
1865         exp_hash_unlink(exi, fid_hash);
1866         exp_hash_unlink(exi, path_hash);
1867 }
1868 
1869 /*
1870  * Unexport an exported filesystem
1871  */
1872 static int
1873 unexport(nfs_export_t *ne, struct exportinfo *exi)
1874 {
1875         struct secinfo cursec[MAX_FLAVORS];
1876         int curcnt;
1877 
1878         rw_enter(&ne->exported_lock, RW_WRITER);
1879 
1880         /* Check if exi is still linked in the export table */
1881         if (!EXP_LINKED(exi) || PSEUDO(exi)) {
1882                 rw_exit(&ne->exported_lock);
1883                 return (EINVAL);
1884         }
1885 
1886         mutex_enter(&nfs_exi_id_lock);
1887         avl_remove(&exi_id_tree, exi);
1888         mutex_exit(&nfs_exi_id_lock);
1889         export_unlink(ne, exi);
1890 
1891         /*
1892          * Remove security flavors before treeclimb_unexport() is called
1893          * because srv_secinfo_treeclimb needs the namespace tree
1894          */
1895         curcnt = build_seclist_nodups(&exi->exi_export, cursec, TRUE);
1896         srv_secinfo_treeclimb(ne, exi, cursec, curcnt, FALSE);
1897 
1898         /*
1899          * If there's a visible list, then need to leave
1900          * a pseudo export here to retain the visible list
1901          * for paths to exports below.
1902          */
1903         if (exi->exi_visible != NULL) {
1904                 struct exportinfo *newexi;
1905 
1906                 newexi = pseudo_exportfs(ne, exi->exi_vp, &exi->exi_fid,
1907                     exi->exi_visible, &exi->exi_export);
1908                 exi->exi_visible = NULL;
1909 
1910                 /* interconnect the existing treenode with the new exportinfo */
1911                 newexi->exi_tree = exi->exi_tree;
1912                 newexi->exi_tree->tree_exi = newexi;
1913 
1914                 /* Update the change timestamp */
1915                 tree_update_change(ne, exi->exi_tree, NULL);
1916         } else {
1917                 treeclimb_unexport(ne, exi);
1918         }
1919 
1920         rw_exit(&ne->exported_lock);
1921 
1922         /*
1923          * Need to call into the NFSv4 server and release all data
1924          * held on this particular export.  This is important since
1925          * the v4 server may be holding file locks or vnodes under
1926          * this export.
1927          */
1928         rfs4_clean_state_exi(ne, exi);
1929 
1930         /*
1931          * Notify the lock manager that the filesystem is being
1932          * unexported.
1933          */
1934         lm_unexport(exi);
1935 
1936         /*
1937          * If this was a public export, restore
1938          * the public filehandle to the root.
1939          */
1940         if (exi == ne->exi_public) {
1941                 ne->exi_public = ne->exi_root;
1942 
1943                 nfslog_share_record(ne->exi_public, CRED());
1944         }
1945 
1946         if (exi->exi_export.ex_flags & EX_LOG)
1947                 nfslog_unshare_record(exi, CRED());
1948 
1949         exi_rele(exi);
1950         return (0);
1951 }
1952 
1953 /*
1954  * Get file handle system call.
1955  * Takes file name and returns a file handle for it.
1956  * Credentials must be verified before calling.
1957  */
1958 int
1959 nfs_getfh(struct nfs_getfh_args *args, model_t model, cred_t *cr)
1960 {
1961         nfs_fh3 fh;
1962         char buf[NFS3_MAXFHSIZE];
1963         char *logptr, logbuf[NFS3_MAXFHSIZE];
1964         int l = NFS3_MAXFHSIZE;
1965         vnode_t *vp;
1966         vnode_t *dvp;
1967         struct exportinfo *exi;
1968         int error;
1969         int vers;
1970         STRUCT_HANDLE(nfs_getfh_args, uap);
1971 
1972 #ifdef lint
1973         model = model;          /* STRUCT macros don't always use it */
1974 #endif
1975 
1976         STRUCT_SET_HANDLE(uap, model, args);
1977 
1978         error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1979             FOLLOW, &dvp, &vp);
1980         if (error == EINVAL) {
1981                 /*
1982                  * if fname resolves to / we get EINVAL error
1983                  * since we wanted the parent vnode. Try again
1984                  * with NULL dvp.
1985                  */
1986                 error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1987                     FOLLOW, NULL, &vp);
1988                 dvp = NULL;
1989         }
1990         if (!error && vp == NULL) {
1991                 /*
1992                  * Last component of fname not found
1993                  */
1994                 if (dvp != NULL) {
1995                         VN_RELE(dvp);
1996                 }
1997                 error = ENOENT;
1998         }
1999         if (error)
2000                 return (error);
2001 
2002         /*
2003          * 'vp' may be an AUTOFS node, so we perform a
2004          * VOP_ACCESS() to trigger the mount of the
2005          * intended filesystem, so we can share the intended
2006          * filesystem instead of the AUTOFS filesystem.
2007          */
2008         (void) VOP_ACCESS(vp, 0, 0, cr, NULL);
2009 
2010         /*
2011          * We're interested in the top most filesystem.
2012          * This is specially important when uap->dname is a trigger
2013          * AUTOFS node, since we're really interested in sharing the
2014          * filesystem AUTOFS mounted as result of the VOP_ACCESS()
2015          * call not the AUTOFS node itself.
2016          */
2017         if (vn_mountedvfs(vp) != NULL) {
2018                 if (error = traverse(&vp)) {
2019                         VN_RELE(vp);
2020                         if (dvp != NULL)
2021                                 VN_RELE(dvp);
2022                         return (error);
2023                 }
2024         }
2025 
2026         vers = STRUCT_FGET(uap, vers);
2027         exi = nfs_vptoexi(dvp, vp, cr, NULL, &error, FALSE);
2028         if (!error) {
2029                 if (vers == NFS_VERSION) {
2030                         error = makefh((fhandle_t *)buf, vp, exi);
2031                         l = NFS_FHSIZE;
2032                         logptr = buf;
2033                 } else if (vers == NFS_V3) {
2034                         int i, sz, pad;
2035 
2036                         error = makefh3(&fh, vp, exi);
2037                         l = RNDUP(fh.fh3_length);
2038                         if (!error && (l > sizeof (fhandle3_t)))
2039                                 error = EREMOTE;
2040                         logptr = logbuf;
2041                         if (!error) {
2042                                 i = 0;
2043                                 sz = sizeof (fsid_t);
2044                                 bcopy(&fh.fh3_fsid, &buf[i], sz);
2045                                 i += sz;
2046 
2047                                 /*
2048                                  * For backwards compatibility, the
2049                                  * fid length may be less than
2050                                  * NFS_FHMAXDATA, but it was always
2051                                  * encoded as NFS_FHMAXDATA bytes.
2052                                  */
2053 
2054                                 sz = sizeof (ushort_t);
2055                                 bcopy(&fh.fh3_len, &buf[i], sz);
2056                                 i += sz;
2057                                 bcopy(fh.fh3_data, &buf[i], fh.fh3_len);
2058                                 i += fh.fh3_len;
2059                                 pad = (NFS_FHMAXDATA - fh.fh3_len);
2060                                 if (pad > 0) {
2061                                         bzero(&buf[i], pad);
2062                                         i += pad;
2063                                         l += pad;
2064                                 }
2065 
2066                                 sz = sizeof (ushort_t);
2067                                 bcopy(&fh.fh3_xlen, &buf[i], sz);
2068                                 i += sz;
2069                                 bcopy(fh.fh3_xdata, &buf[i], fh.fh3_xlen);
2070                                 i += fh.fh3_xlen;
2071                                 pad = (NFS_FHMAXDATA - fh.fh3_xlen);
2072                                 if (pad > 0) {
2073                                         bzero(&buf[i], pad);
2074                                         i += pad;
2075                                         l += pad;
2076                                 }
2077                         }
2078                         /*
2079                          * If we need to do NFS logging, the filehandle
2080                          * must be downsized to 32 bytes.
2081                          */
2082                         if (!error && exi->exi_export.ex_flags & EX_LOG) {
2083                                 i = 0;
2084                                 sz = sizeof (fsid_t);
2085                                 bcopy(&fh.fh3_fsid, &logbuf[i], sz);
2086                                 i += sz;
2087                                 sz = sizeof (ushort_t);
2088                                 bcopy(&fh.fh3_len, &logbuf[i], sz);
2089                                 i += sz;
2090                                 sz = NFS_FHMAXDATA;
2091                                 bcopy(fh.fh3_data, &logbuf[i], sz);
2092                                 i += sz;
2093                                 sz = sizeof (ushort_t);
2094                                 bcopy(&fh.fh3_xlen, &logbuf[i], sz);
2095                                 i += sz;
2096                                 sz = NFS_FHMAXDATA;
2097                                 bcopy(fh.fh3_xdata, &logbuf[i], sz);
2098                                 i += sz;
2099                         }
2100                 }
2101                 if (!error && exi->exi_export.ex_flags & EX_LOG) {
2102                         nfslog_getfh(exi, (fhandle_t *)logptr,
2103                             STRUCT_FGETP(uap, fname), UIO_USERSPACE, cr);
2104                 }
2105                 exi_rele(exi);
2106                 if (!error) {
2107                         if (copyout(&l, STRUCT_FGETP(uap, lenp), sizeof (int)))
2108                                 error = EFAULT;
2109                         if (copyout(buf, STRUCT_FGETP(uap, fhp), l))
2110                                 error = EFAULT;
2111                 }
2112         }
2113         VN_RELE(vp);
2114         if (dvp != NULL) {
2115                 VN_RELE(dvp);
2116         }
2117         return (error);
2118 }
2119 
2120 /*
2121  * Strategy: if vp is in the export list, then
2122  * return the associated file handle. Otherwise, ".."
2123  * once up the vp and try again, until the root of the
2124  * filesystem is reached.
2125  */
2126 struct   exportinfo *
2127 nfs_vptoexi(vnode_t *dvp, vnode_t *vp, cred_t *cr, int *walk,
2128     int *err, bool_t v4srv)
2129 {
2130         fid_t fid;
2131         int error;
2132         struct exportinfo *exi;
2133 
2134         ASSERT(vp);
2135         VN_HOLD(vp);
2136         if (dvp != NULL) {
2137                 VN_HOLD(dvp);
2138         }
2139         if (walk != NULL)
2140                 *walk = 0;
2141 
2142         for (;;) {
2143                 bzero(&fid, sizeof (fid));
2144                 fid.fid_len = MAXFIDSZ;
2145                 error = vop_fid_pseudo(vp, &fid);
2146                 if (error) {
2147                         /*
2148                          * If vop_fid_pseudo returns ENOSPC then the fid
2149                          * supplied is too small. For now we simply
2150                          * return EREMOTE.
2151                          */
2152                         if (error == ENOSPC)
2153                                 error = EREMOTE;
2154                         break;
2155                 }
2156 
2157                 if (v4srv)
2158                         exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
2159                 else
2160                         exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid);
2161 
2162                 if (exi != NULL) {
2163                         /*
2164                          * Found the export info
2165                          */
2166                         break;
2167                 }
2168 
2169                 /*
2170                  * We have just failed finding a matching export.
2171                  * If we're at the root of this filesystem, then
2172                  * it's time to stop (with failure).
2173                  */
2174                 if (vp->v_flag & VROOT) {
2175                         error = EINVAL;
2176                         break;
2177                 }
2178 
2179                 if (walk != NULL)
2180                         (*walk)++;
2181 
2182                 /*
2183                  * Now, do a ".." up vp. If dvp is supplied, use it,
2184                  * otherwise, look it up.
2185                  */
2186                 if (dvp == NULL) {
2187                         error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, cr,
2188                             NULL, NULL, NULL);
2189                         if (error)
2190                                 break;
2191                 }
2192                 VN_RELE(vp);
2193                 vp = dvp;
2194                 dvp = NULL;
2195         }
2196         VN_RELE(vp);
2197         if (dvp != NULL) {
2198                 VN_RELE(dvp);
2199         }
2200         if (error != 0) {
2201                 if (err != NULL)
2202                         *err = error;
2203                 return (NULL);
2204         }
2205         return (exi);
2206 }
2207 
2208 int
2209 chk_clnt_sec(exportinfo_t *exi, struct svc_req *req)
2210 {
2211         int i, nfsflavor;
2212         struct secinfo *sp;
2213 
2214         /*
2215          *  Get the nfs flavor number from xprt.
2216          */
2217         nfsflavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
2218 
2219         sp = exi->exi_export.ex_secinfo;
2220         for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2221                 if ((nfsflavor == sp[i].s_secinfo.sc_nfsnum) &&
2222                     SEC_REF_EXPORTED(sp + i))
2223                         return (TRUE);
2224         }
2225         return (FALSE);
2226 }
2227 
2228 /*
2229  * Make an fhandle from a vnode
2230  */
2231 int
2232 makefh(fhandle_t *fh, vnode_t *vp, exportinfo_t *exi)
2233 {
2234         int error;
2235 
2236         *fh = exi->exi_fh;   /* struct copy */
2237 
2238         error = VOP_FID(vp, (fid_t *)&fh->fh_len, NULL);
2239         if (error) {
2240                 /*
2241                  * Should be something other than EREMOTE
2242                  */
2243                 return (EREMOTE);
2244         }
2245         return (0);
2246 }
2247 
2248 /*
2249  * This routine makes an overloaded V2 fhandle which contains
2250  * sec modes.
2251  *
2252  * Note that the first four octets contain the length octet,
2253  * the status octet, and two padded octets to make them XDR
2254  * four-octet aligned.
2255  *
2256  *   1   2   3   4                                          32
2257  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2258  * | l | s |   |   |     sec_1     |...|     sec_n     |...|   |
2259  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2260  *
2261  * where
2262  *
2263  *   the status octet s indicates whether there are more security
2264  *   flavors (1 means yes, 0 means no) that require the client to
2265  *   perform another 0x81 LOOKUP to get them,
2266  *
2267  *   the length octet l is the length describing the number of
2268  *   valid octets that follow.  (l = 4 * n, where n is the number
2269  *   of security flavors sent in the current overloaded filehandle.)
2270  *
2271  *   sec_index should always be in the inclusive range: [1 - ex_seccnt],
2272  *   and it tells server where to start within the secinfo array.
2273  *   Usually it will always be 1; however, if more flavors are used
2274  *   for the public export than can be encoded in the overloaded FH
2275  *   (7 for NFS2), subsequent SNEGO MCLs will have a larger index
2276  *   so the server will pick up where it left off from the previous
2277  *   MCL reply.
2278  *
2279  *   With NFS4 support, implicitly allowed flavors are also in
2280  *   the secinfo array; however, they should not be returned in
2281  *   SNEGO MCL replies.
2282  */
2283 int
2284 makefh_ol(fhandle_t *fh, exportinfo_t *exi, uint_t sec_index)
2285 {
2286         secinfo_t sec[MAX_FLAVORS];
2287         int totalcnt, i, *ipt, cnt, seccnt, secidx, fh_max_cnt;
2288         char *c;
2289 
2290         if (fh == NULL || exi == NULL || sec_index < 1)
2291                 return (EREMOTE);
2292 
2293         /*
2294          * WebNFS clients need to know the unique set of explicitly
2295          * shared flavors in used for the public export. When
2296          * "TRUE" is passed to build_seclist_nodups(), only explicitly
2297          * shared flavors are included in the list.
2298          */
2299         seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2300         if (sec_index > seccnt)
2301                 return (EREMOTE);
2302 
2303         fh_max_cnt = (NFS_FHSIZE / sizeof (int)) - 1;
2304         totalcnt = seccnt - sec_index + 1;
2305         cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2306 
2307         c = (char *)fh;
2308         /*
2309          * Encode the length octet representing the number of
2310          * security flavors (in bytes) in this overloaded fh.
2311          */
2312         *c = cnt * sizeof (int);
2313 
2314         /*
2315          * Encode the status octet that indicates whether there
2316          * are more security flavors the client needs to get.
2317          */
2318         *(c + 1) = totalcnt > fh_max_cnt;
2319 
2320         /*
2321          * put security flavors in the overloaded fh
2322          */
2323         ipt = (int *)(c + sizeof (int32_t));
2324         secidx = sec_index - 1;
2325         for (i = 0; i < cnt; i++) {
2326                 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2327         }
2328         return (0);
2329 }
2330 
2331 /*
2332  * Make an nfs_fh3 from a vnode
2333  */
2334 int
2335 makefh3(nfs_fh3 *fh, vnode_t *vp, struct exportinfo *exi)
2336 {
2337         int error;
2338         fid_t fid;
2339 
2340         bzero(&fid, sizeof (fid));
2341         fid.fid_len = sizeof (fh->fh3_data);
2342         error = VOP_FID(vp, &fid, NULL);
2343         if (error)
2344                 return (EREMOTE);
2345 
2346         bzero(fh, sizeof (nfs_fh3));
2347         fh->fh3_fsid = exi->exi_fsid;
2348         fh->fh3_len = fid.fid_len;
2349         bcopy(fid.fid_data, fh->fh3_data, fh->fh3_len);
2350 
2351         fh->fh3_xlen = exi->exi_fid.fid_len;
2352         ASSERT(fh->fh3_xlen <= sizeof (fh->fh3_xdata));
2353         bcopy(exi->exi_fid.fid_data, fh->fh3_xdata, fh->fh3_xlen);
2354 
2355         fh->fh3_length = sizeof (fh->fh3_fsid)
2356             + sizeof (fh->fh3_len) + fh->fh3_len
2357             + sizeof (fh->fh3_xlen) + fh->fh3_xlen;
2358         fh->fh3_flags = 0;
2359 
2360         return (0);
2361 }
2362 
2363 /*
2364  * This routine makes an overloaded V3 fhandle which contains
2365  * sec modes.
2366  *
2367  *  1        4
2368  * +--+--+--+--+
2369  * |    len    |
2370  * +--+--+--+--+
2371  *                                               up to 64
2372  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2373  * |s |  |  |  |   sec_1   |   sec_2   | ... |   sec_n   |
2374  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2375  *
2376  * len = 4 * (n+1), where n is the number of security flavors
2377  * sent in the current overloaded filehandle.
2378  *
2379  * the status octet s indicates whether there are more security
2380  * mechanisms (1 means yes, 0 means no) that require the client
2381  * to perform another 0x81 LOOKUP to get them.
2382  *
2383  * Three octets are padded after the status octet.
2384  */
2385 int
2386 makefh3_ol(nfs_fh3 *fh, struct exportinfo *exi, uint_t sec_index)
2387 {
2388         secinfo_t sec[MAX_FLAVORS];
2389         int totalcnt, cnt, *ipt, i, seccnt, fh_max_cnt, secidx;
2390         char *c;
2391 
2392         if (fh == NULL || exi == NULL || sec_index < 1)
2393                 return (EREMOTE);
2394 
2395         /*
2396          * WebNFS clients need to know the unique set of explicitly
2397          * shared flavors in used for the public export. When
2398          * "TRUE" is passed to build_seclist_nodups(), only explicitly
2399          * shared flavors are included in the list.
2400          */
2401         seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2402 
2403         if (sec_index > seccnt)
2404                 return (EREMOTE);
2405 
2406         fh_max_cnt = (NFS3_FHSIZE / sizeof (int)) - 1;
2407         totalcnt = seccnt - sec_index + 1;
2408         cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2409 
2410         /*
2411          * Place the length in fh3_length representing the number
2412          * of security flavors (in bytes) in this overloaded fh.
2413          */
2414         fh->fh3_flags = FH_WEBNFS;
2415         fh->fh3_length = (cnt+1) * sizeof (int32_t);
2416 
2417         c = (char *)&fh->fh3_u.nfs_fh3_i.fh3_i;
2418         /*
2419          * Encode the status octet that indicates whether there
2420          * are more security flavors the client needs to get.
2421          */
2422         *c = totalcnt > fh_max_cnt;
2423 
2424         /*
2425          * put security flavors in the overloaded fh
2426          */
2427         secidx = sec_index - 1;
2428         ipt = (int *)(c + sizeof (int32_t));
2429         for (i = 0; i < cnt; i++) {
2430                 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2431         }
2432         return (0);
2433 }
2434 
2435 /*
2436  * Make an nfs_fh4 from a vnode
2437  */
2438 int
2439 makefh4(nfs_fh4 *fh, vnode_t *vp, struct exportinfo *exi)
2440 {
2441         int error;
2442         nfs_fh4_fmt_t *fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2443         fid_t fid;
2444 
2445         bzero(&fid, sizeof (fid));
2446         fid.fid_len = MAXFIDSZ;
2447         /*
2448          * vop_fid_pseudo() is used to set up NFSv4 namespace, so
2449          * use vop_fid_pseudo() here to get the fid instead of VOP_FID.
2450          */
2451         error = vop_fid_pseudo(vp, &fid);
2452         if (error)
2453                 return (error);
2454 
2455         fh->nfs_fh4_len = NFS_FH4_LEN;
2456 
2457         fh_fmtp->fh4_i.fhx_fsid = exi->exi_fh.fh_fsid;
2458         fh_fmtp->fh4_i.fhx_xlen = exi->exi_fh.fh_xlen;
2459 
2460         bzero(fh_fmtp->fh4_i.fhx_data, sizeof (fh_fmtp->fh4_i.fhx_data));
2461         bzero(fh_fmtp->fh4_i.fhx_xdata, sizeof (fh_fmtp->fh4_i.fhx_xdata));
2462         ASSERT(exi->exi_fh.fh_xlen <= sizeof (fh_fmtp->fh4_i.fhx_xdata));
2463         bcopy(exi->exi_fh.fh_xdata, fh_fmtp->fh4_i.fhx_xdata,
2464             exi->exi_fh.fh_xlen);
2465 
2466         fh_fmtp->fh4_len = fid.fid_len;
2467         ASSERT(fid.fid_len <= sizeof (fh_fmtp->fh4_data));
2468         bcopy(fid.fid_data, fh_fmtp->fh4_data, fid.fid_len);
2469         fh_fmtp->fh4_flag = 0;
2470 
2471 #ifdef VOLATILE_FH_TEST
2472         /*
2473          * XXX (temporary?)
2474          * Use the rnode volatile_id value to add volatility to the fh.
2475          *
2476          * For testing purposes there are currently two scenarios, based
2477          * on whether the filesystem was shared with "volatile_fh"
2478          * or "expire_on_rename". In the first case, use the value of
2479          * export struct share_time as the volatile_id. In the second
2480          * case use the vnode volatile_id value (which is set to the
2481          * time in which the file was renamed).
2482          *
2483          * Note that the above are temporary constructs for testing only
2484          * XXX
2485          */
2486         if (exi->exi_export.ex_flags & EX_VOLRNM) {
2487                 fh_fmtp->fh4_volatile_id = find_volrnm_fh_id(exi, fh);
2488         } else if (exi->exi_export.ex_flags & EX_VOLFH) {
2489                 fh_fmtp->fh4_volatile_id = exi->exi_volatile_id;
2490         } else {
2491                 fh_fmtp->fh4_volatile_id = 0;
2492         }
2493 #endif /* VOLATILE_FH_TEST */
2494 
2495         return (0);
2496 }
2497 
2498 /*
2499  * Convert an fhandle into a vnode.
2500  * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2501  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2502  * are done with it.
2503  */
2504 vnode_t *
2505 nfs_fhtovp(fhandle_t *fh, struct exportinfo *exi)
2506 {
2507         vfs_t *vfsp;
2508         vnode_t *vp;
2509         int error;
2510         fid_t *fidp;
2511 
2512         TRACE_0(TR_FAC_NFS, TR_FHTOVP_START,
2513             "fhtovp_start");
2514 
2515         if (exi == NULL) {
2516                 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2517                     "fhtovp_end:(%S)", "exi NULL");
2518                 return (NULL);  /* not exported */
2519         }
2520 
2521         ASSERT(exi->exi_vp != NULL);
2522 
2523         if (PUBLIC_FH2(fh)) {
2524                 if (exi->exi_export.ex_flags & EX_PUBLIC) {
2525                         TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2526                             "fhtovp_end:(%S)", "root not exported");
2527                         return (NULL);
2528                 }
2529                 vp = exi->exi_vp;
2530                 VN_HOLD(vp);
2531                 return (vp);
2532         }
2533 
2534         vfsp = exi->exi_vp->v_vfsp;
2535         ASSERT(vfsp != NULL);
2536         fidp = (fid_t *)&fh->fh_len;
2537 
2538         error = VFS_VGET(vfsp, &vp, fidp);
2539         if (error || vp == NULL) {
2540                 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2541                     "fhtovp_end:(%S)", "VFS_GET failed or vp NULL");
2542                 return (NULL);
2543         }
2544         TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2545             "fhtovp_end:(%S)", "end");
2546         return (vp);
2547 }
2548 
2549 /*
2550  * Convert an nfs_fh3 into a vnode.
2551  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2552  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2553  * are done with it.
2554  */
2555 vnode_t *
2556 nfs3_fhtovp(nfs_fh3 *fh, struct exportinfo *exi)
2557 {
2558         vfs_t *vfsp;
2559         vnode_t *vp;
2560         int error;
2561         fid_t *fidp;
2562 
2563         if (exi == NULL)
2564                 return (NULL);  /* not exported */
2565 
2566         ASSERT(exi->exi_vp != NULL);
2567 
2568         if (PUBLIC_FH3(fh)) {
2569                 if (exi->exi_export.ex_flags & EX_PUBLIC)
2570                         return (NULL);
2571                 vp = exi->exi_vp;
2572                 VN_HOLD(vp);
2573                 return (vp);
2574         }
2575 
2576         if (fh->fh3_length < NFS3_OLDFHSIZE ||
2577             fh->fh3_length > NFS3_MAXFHSIZE)
2578                 return (NULL);
2579 
2580         vfsp = exi->exi_vp->v_vfsp;
2581         ASSERT(vfsp != NULL);
2582         fidp = FH3TOFIDP(fh);
2583 
2584         error = VFS_VGET(vfsp, &vp, fidp);
2585         if (error || vp == NULL)
2586                 return (NULL);
2587 
2588         return (vp);
2589 }
2590 
2591 /*
2592  * Convert an nfs_fh4 into a vnode.
2593  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2594  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2595  * are done with it.
2596  */
2597 vnode_t *
2598 nfs4_fhtovp(nfs_fh4 *fh, struct exportinfo *exi, nfsstat4 *statp)
2599 {
2600         vfs_t *vfsp;
2601         vnode_t *vp = NULL;
2602         int error;
2603         fid_t *fidp;
2604         nfs_fh4_fmt_t *fh_fmtp;
2605 #ifdef VOLATILE_FH_TEST
2606         uint32_t volatile_id = 0;
2607 #endif /* VOLATILE_FH_TEST */
2608 
2609         if (exi == NULL) {
2610                 *statp = NFS4ERR_STALE;
2611                 return (NULL);  /* not exported */
2612         }
2613         ASSERT(exi->exi_vp != NULL);
2614 
2615         /* caller should have checked this */
2616         ASSERT(fh->nfs_fh4_len >= NFS_FH4_LEN);
2617 
2618         fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2619         vfsp = exi->exi_vp->v_vfsp;
2620         ASSERT(vfsp != NULL);
2621         fidp = (fid_t *)&fh_fmtp->fh4_len;
2622 
2623 #ifdef VOLATILE_FH_TEST
2624         /* XXX check if volatile - should be changed later */
2625         if (exi->exi_export.ex_flags & (EX_VOLRNM | EX_VOLFH)) {
2626                 /*
2627                  * Filesystem is shared with volatile filehandles
2628                  */
2629                 if (exi->exi_export.ex_flags & EX_VOLRNM)
2630                         volatile_id = find_volrnm_fh_id(exi, fh);
2631                 else
2632                         volatile_id = exi->exi_volatile_id;
2633 
2634                 if (fh_fmtp->fh4_volatile_id != volatile_id) {
2635                         *statp = NFS4ERR_FHEXPIRED;
2636                         return (NULL);
2637                 }
2638         }
2639         /*
2640          * XXX even if test_volatile_fh false, the fh may contain a
2641          * volatile id if obtained when the test was set.
2642          */
2643         fh_fmtp->fh4_volatile_id = (uchar_t)0;
2644 #endif /* VOLATILE_FH_TEST */
2645 
2646         error = VFS_VGET(vfsp, &vp, fidp);
2647         /*
2648          * If we can not get vp from VFS_VGET, perhaps this is
2649          * an nfs v2/v3/v4 node in an nfsv4 pseudo filesystem.
2650          * Check it out.
2651          */
2652         if (error && PSEUDO(exi))
2653                 error = nfs4_vget_pseudo(exi, &vp, fidp);
2654 
2655         if (error || vp == NULL) {
2656                 *statp = NFS4ERR_STALE;
2657                 return (NULL);
2658         }
2659         /* XXX - disgusting hack */
2660         if (vp->v_type == VNON && vp->v_flag & V_XATTRDIR)
2661                 vp->v_type = VDIR;
2662         *statp = NFS4_OK;
2663         return (vp);
2664 }
2665 
2666 /*
2667  * Find the export structure associated with the given filesystem.
2668  * If found, then increment the ref count (exi_count).
2669  */
2670 struct exportinfo *
2671 checkexport(fsid_t *fsid, fid_t *fid)
2672 {
2673         struct exportinfo *exi;
2674         nfs_export_t *ne = nfs_get_export();
2675 
2676         rw_enter(&ne->exported_lock, RW_READER);
2677         for (exi = ne->exptable[exptablehash(fsid, fid)];
2678             exi != NULL;
2679             exi = exi->fid_hash.next) {
2680                 if (exportmatch(exi, fsid, fid)) {
2681                         /*
2682                          * If this is the place holder for the
2683                          * public file handle, then return the
2684                          * real export entry for the public file
2685                          * handle.
2686                          */
2687                         if (exi->exi_export.ex_flags & EX_PUBLIC) {
2688                                 exi = ne->exi_public;
2689                         }
2690 
2691                         exi_hold(exi);
2692                         rw_exit(&ne->exported_lock);
2693                         return (exi);
2694                 }
2695         }
2696         rw_exit(&ne->exported_lock);
2697         return (NULL);
2698 }
2699 
2700 
2701 /*
2702  * "old school" version of checkexport() for NFS4.  NFS4
2703  * rfs4_compound holds exported_lock for duration of compound
2704  * processing.  This version doesn't manipulate exi_count
2705  * since NFS4 breaks fundamental assumptions in the exi_count
2706  * design.
2707  */
2708 struct exportinfo *
2709 checkexport4(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2710 {
2711         struct exportinfo *exi;
2712         nfs_export_t *ne = nfs_get_export();
2713 
2714         ASSERT(RW_LOCK_HELD(&ne->exported_lock));
2715 
2716         for (exi = ne->exptable[exptablehash(fsid, fid)];
2717             exi != NULL;
2718             exi = exi->fid_hash.next) {
2719                 if (exportmatch(exi, fsid, fid)) {
2720                         /*
2721                          * If this is the place holder for the
2722                          * public file handle, then return the
2723                          * real export entry for the public file
2724                          * handle.
2725                          */
2726                         if (exi->exi_export.ex_flags & EX_PUBLIC) {
2727                                 exi = ne->exi_public;
2728                         }
2729 
2730                         /*
2731                          * If vp is given, check if vp is the
2732                          * same vnode as the exported node.
2733                          *
2734                          * Since VOP_FID of a lofs node returns the
2735                          * fid of its real node (ufs), the exported
2736                          * node for lofs and (pseudo) ufs may have
2737                          * the same fsid and fid.
2738                          */
2739                         if (vp == NULL || vp == exi->exi_vp)
2740                                 return (exi);
2741                 }
2742         }
2743 
2744         return (NULL);
2745 }
2746 
2747 /*
2748  * Free an entire export list node
2749  */
2750 void
2751 exportfree(struct exportinfo *exi)
2752 {
2753         struct exportdata *ex;
2754         struct charset_cache *cache;
2755         int i;
2756 
2757         ex = &exi->exi_export;
2758 
2759         ASSERT(exi->exi_vp != NULL && !(exi->exi_export.ex_flags & EX_PUBLIC));
2760         VN_RELE(exi->exi_vp);
2761         if (exi->exi_dvp != NULL)
2762                 VN_RELE(exi->exi_dvp);
2763 
2764         if (ex->ex_flags & EX_INDEX)
2765                 kmem_free(ex->ex_index, strlen(ex->ex_index) + 1);
2766 
2767         kmem_free(ex->ex_path, ex->ex_pathlen + 1);
2768         nfsauth_cache_free(exi);
2769 
2770         /*
2771          * if there is a character set mapping cached, clean it up.
2772          */
2773         for (cache = exi->exi_charset; cache != NULL;
2774             cache = exi->exi_charset) {
2775                 if (cache->inbound != (kiconv_t)-1)
2776                         (void) kiconv_close(cache->inbound);
2777                 if (cache->outbound != (kiconv_t)-1)
2778                         (void) kiconv_close(cache->outbound);
2779                 exi->exi_charset = cache->next;
2780                 kmem_free(cache, sizeof (struct charset_cache));
2781         }
2782 
2783         if (exi->exi_logbuffer != NULL)
2784                 nfslog_disable(exi);
2785 
2786         if (ex->ex_flags & EX_LOG) {
2787                 kmem_free(ex->ex_log_buffer, ex->ex_log_bufferlen + 1);
2788                 kmem_free(ex->ex_tag, ex->ex_taglen + 1);
2789         }
2790 
2791         if (exi->exi_visible)
2792                 free_visible(exi->exi_visible);
2793 
2794         srv_secinfo_list_free(ex->ex_secinfo, ex->ex_seccnt);
2795 
2796 #ifdef VOLATILE_FH_TEST
2797         free_volrnm_list(exi);
2798         mutex_destroy(&exi->exi_vol_rename_lock);
2799 #endif /* VOLATILE_FH_TEST */
2800 
2801         mutex_destroy(&exi->exi_lock);
2802         rw_destroy(&exi->exi_cache_lock);
2803         /*
2804          * All nodes in the exi_cache AVL trees were removed and freed in the
2805          * nfsauth_cache_free() call above.  We will just destroy and free the
2806          * empty AVL trees here.
2807          */
2808         for (i = 0; i < AUTH_TABLESIZE; i++) {
2809                 avl_destroy(exi->exi_cache[i]);
2810                 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
2811         }
2812 
2813         kmem_free(exi, sizeof (*exi));
2814 }
2815 
2816 /*
2817  * load the index file from user space into kernel space.
2818  */
2819 static int
2820 loadindex(struct exportdata *kex)
2821 {
2822         int error;
2823         char index[MAXNAMELEN+1];
2824         size_t len;
2825 
2826         /*
2827          * copyinstr copies the complete string including the NULL and
2828          * returns the len with the NULL byte included in the calculation
2829          * as long as the max length is not exceeded.
2830          */
2831         if (error = copyinstr(kex->ex_index, index, sizeof (index), &len))
2832                 return (error);
2833 
2834         kex->ex_index = kmem_alloc(len, KM_SLEEP);
2835         bcopy(index, kex->ex_index, len);
2836 
2837         return (0);
2838 }
2839 
2840 void
2841 exi_hold(struct exportinfo *exi)
2842 {
2843         mutex_enter(&exi->exi_lock);
2844         exi->exi_count++;
2845         mutex_exit(&exi->exi_lock);
2846 }
2847 
2848 /*
2849  * When a thread completes using exi, it should call exi_rele().
2850  * exi_rele() decrements exi_count. It releases exi if exi_count == 0, i.e.
2851  * if this is the last user of exi and exi is not on exportinfo list anymore
2852  */
2853 void
2854 exi_rele(struct exportinfo *exi)
2855 {
2856         mutex_enter(&exi->exi_lock);
2857         exi->exi_count--;
2858         if (exi->exi_count == 0) {
2859                 mutex_exit(&exi->exi_lock);
2860                 exportfree(exi);
2861         } else
2862                 mutex_exit(&exi->exi_lock);
2863 }
2864 
2865 #ifdef VOLATILE_FH_TEST
2866 /*
2867  * Test for volatile fh's - add file handle to list and set its volatile id
2868  * to time it was renamed. If EX_VOLFH is also on and the fs is reshared,
2869  * the vol_rename queue is purged.
2870  *
2871  * XXX This code is for unit testing purposes only... To correctly use it, it
2872  * needs to tie a rename list to the export struct and (more
2873  * important), protect access to the exi rename list using a write lock.
2874  */
2875 
2876 /*
2877  * get the fh vol record if it's in the volatile on rename list. Don't check
2878  * volatile_id in the file handle - compare only the file handles.
2879  */
2880 static struct ex_vol_rename *
2881 find_volrnm_fh(struct exportinfo *exi, nfs_fh4 *fh4p)
2882 {
2883         struct ex_vol_rename *p = NULL;
2884         fhandle4_t *fhp;
2885 
2886         /* XXX shouldn't we assert &exported_lock held? */
2887         ASSERT(MUTEX_HELD(&exi->exi_vol_rename_lock));
2888 
2889         if (fh4p->nfs_fh4_len != NFS_FH4_LEN) {
2890                 return (NULL);
2891         }
2892         fhp = &((nfs_fh4_fmt_t *)fh4p->nfs_fh4_val)->fh4_i;
2893         for (p = exi->exi_vol_rename; p != NULL; p = p->vrn_next) {
2894                 if (bcmp(fhp, &p->vrn_fh_fmt.fh4_i,
2895                     sizeof (fhandle4_t)) == 0)
2896                         break;
2897         }
2898         return (p);
2899 }
2900 
2901 /*
2902  * get the volatile id for the fh (if there is - else return 0). Ignore the
2903  * volatile_id in the file handle - compare only the file handles.
2904  */
2905 static uint32_t
2906 find_volrnm_fh_id(struct exportinfo *exi, nfs_fh4 *fh4p)
2907 {
2908         struct ex_vol_rename *p;
2909         uint32_t volatile_id;
2910 
2911         mutex_enter(&exi->exi_vol_rename_lock);
2912         p = find_volrnm_fh(exi, fh4p);
2913         volatile_id = (p ? p->vrn_fh_fmt.fh4_volatile_id :
2914             exi->exi_volatile_id);
2915         mutex_exit(&exi->exi_vol_rename_lock);
2916         return (volatile_id);
2917 }
2918 
2919 /*
2920  * Free the volatile on rename list - will be called if a filesystem is
2921  * unshared or reshared without EX_VOLRNM
2922  */
2923 static void
2924 free_volrnm_list(struct exportinfo *exi)
2925 {
2926         struct ex_vol_rename *p, *pnext;
2927 
2928         /* no need to hold mutex lock - this one is called from exportfree */
2929         for (p = exi->exi_vol_rename; p != NULL; p = pnext) {
2930                 pnext = p->vrn_next;
2931                 kmem_free(p, sizeof (*p));
2932         }
2933         exi->exi_vol_rename = NULL;
2934 }
2935 
2936 /*
2937  * Add a file handle to the volatile on rename list.
2938  */
2939 void
2940 add_volrnm_fh(struct exportinfo *exi, vnode_t *vp)
2941 {
2942         struct ex_vol_rename *p;
2943         char fhbuf[NFS4_FHSIZE];
2944         nfs_fh4 fh4;
2945         int error;
2946 
2947         fh4.nfs_fh4_val = fhbuf;
2948         error = makefh4(&fh4, vp, exi);
2949         if ((error) || (fh4.nfs_fh4_len != sizeof (p->vrn_fh_fmt))) {
2950                 return;
2951         }
2952 
2953         mutex_enter(&exi->exi_vol_rename_lock);
2954 
2955         p = find_volrnm_fh(exi, &fh4);
2956 
2957         if (p == NULL) {
2958                 p = kmem_alloc(sizeof (*p), KM_SLEEP);
2959                 bcopy(fh4.nfs_fh4_val, &p->vrn_fh_fmt, sizeof (p->vrn_fh_fmt));
2960                 p->vrn_next = exi->exi_vol_rename;
2961                 exi->exi_vol_rename = p;
2962         }
2963 
2964         p->vrn_fh_fmt.fh4_volatile_id = gethrestime_sec();
2965         mutex_exit(&exi->exi_vol_rename_lock);
2966 }
2967 
2968 #endif /* VOLATILE_FH_TEST */