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  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
  27  */
  28 
  29 /*
  30  *      Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
  31  *      All Rights Reserved
  32  */
  33 
  34 /*
  35  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  36  */
  37 
  38 /*
  39  * Copyright (c) 2014, STRATO AG. All rights reserved.
  40  */
  41 
  42 #include <sys/param.h>
  43 #include <sys/types.h>
  44 #include <sys/systm.h>
  45 #include <sys/cred.h>
  46 #include <sys/time.h>
  47 #include <sys/vnode.h>
  48 #include <sys/vfs.h>
  49 #include <sys/vfs_opreg.h>
  50 #include <sys/file.h>
  51 #include <sys/filio.h>
  52 #include <sys/uio.h>
  53 #include <sys/buf.h>
  54 #include <sys/mman.h>
  55 #include <sys/pathname.h>
  56 #include <sys/dirent.h>
  57 #include <sys/debug.h>
  58 #include <sys/vmsystm.h>
  59 #include <sys/fcntl.h>
  60 #include <sys/flock.h>
  61 #include <sys/swap.h>
  62 #include <sys/errno.h>
  63 #include <sys/strsubr.h>
  64 #include <sys/sysmacros.h>
  65 #include <sys/kmem.h>
  66 #include <sys/cmn_err.h>
  67 #include <sys/pathconf.h>
  68 #include <sys/utsname.h>
  69 #include <sys/dnlc.h>
  70 #include <sys/acl.h>
  71 #include <sys/systeminfo.h>
  72 #include <sys/policy.h>
  73 #include <sys/sdt.h>
  74 #include <sys/list.h>
  75 #include <sys/stat.h>
  76 #include <sys/zone.h>
  77 
  78 #include <rpc/types.h>
  79 #include <rpc/auth.h>
  80 #include <rpc/clnt.h>
  81 
  82 #include <nfs/nfs.h>
  83 #include <nfs/nfs_clnt.h>
  84 #include <nfs/nfs_acl.h>
  85 #include <nfs/lm.h>
  86 #include <nfs/nfs4.h>
  87 #include <nfs/nfs4_kprot.h>
  88 #include <nfs/rnode4.h>
  89 #include <nfs/nfs4_clnt.h>
  90 
  91 #include <vm/hat.h>
  92 #include <vm/as.h>
  93 #include <vm/page.h>
  94 #include <vm/pvn.h>
  95 #include <vm/seg.h>
  96 #include <vm/seg_map.h>
  97 #include <vm/seg_kpm.h>
  98 #include <vm/seg_vn.h>
  99 
 100 #include <fs/fs_subr.h>
 101 
 102 #include <sys/ddi.h>
 103 #include <sys/int_fmtio.h>
 104 #include <sys/fs/autofs.h>
 105 
 106 typedef struct {
 107         nfs4_ga_res_t   *di_garp;
 108         cred_t          *di_cred;
 109         hrtime_t        di_time_call;
 110 } dirattr_info_t;
 111 
 112 typedef enum nfs4_acl_op {
 113         NFS4_ACL_GET,
 114         NFS4_ACL_SET
 115 } nfs4_acl_op_t;
 116 
 117 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi);
 118 
 119 static void     nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
 120                         char *, dirattr_info_t *);
 121 
 122 static void     nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
 123                     nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
 124                     nfs4_error_t *, int *);
 125 static int      nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
 126                         cred_t *);
 127 static int      nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
 128                         stable_how4 *);
 129 static int      nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
 130                         cred_t *, bool_t, struct uio *);
 131 static int      nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
 132                         vsecattr_t *);
 133 static int      nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
 134 static int      nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
 135 static int      nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
 136 static int      nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
 137 static int      nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
 138 static int      nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
 139                         int, vnode_t **, cred_t *);
 140 static int      nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
 141                         cred_t *, int, int, enum createmode4, int);
 142 static int      nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
 143                         caller_context_t *);
 144 static int      nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
 145                         vnode_t *, char *, cred_t *, nfsstat4 *);
 146 static int      nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
 147                         vnode_t *, char *, cred_t *, nfsstat4 *);
 148 static int      do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
 149 static void     nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
 150 static int      nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
 151 static int      nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
 152                         page_t *[], size_t, struct seg *, caddr_t,
 153                         enum seg_rw, cred_t *);
 154 static void     nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
 155                         cred_t *);
 156 static int      nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
 157                         int, cred_t *);
 158 static int      nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
 159                         int, cred_t *);
 160 static int      nfs4_commit(vnode_t *, offset4, count4, cred_t *);
 161 static void     nfs4_set_mod(vnode_t *);
 162 static void     nfs4_get_commit(vnode_t *);
 163 static void     nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
 164 static int      nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
 165 static int      nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
 166 static int      nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
 167                         cred_t *);
 168 static void     do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
 169                         cred_t *);
 170 static int      nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
 171                         hrtime_t, vnode_t *, cred_t *);
 172 static int      nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
 173 static int      nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
 174 static void     nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
 175                         u_offset_t);
 176 static int      nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
 177 static int      nfs4_block_and_wait(clock_t *, rnode4_t *);
 178 static cred_t  *state_to_cred(nfs4_open_stream_t *);
 179 static void     denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
 180 static pid_t    lo_to_pid(lock_owner4 *);
 181 static void     nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
 182                         cred_t *, nfs4_lock_owner_t *);
 183 static void     push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
 184                         nfs4_lock_owner_t *);
 185 static int      open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
 186 static void     nfs4_delmap_callback(struct as *, void *, uint_t);
 187 static void     nfs4_free_delmapcall(nfs4_delmapcall_t *);
 188 static nfs4_delmapcall_t        *nfs4_init_delmapcall();
 189 static int      nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
 190 static int      nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
 191 static int      nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
 192                         uid_t, gid_t, int);
 193 
 194 /*
 195  * Routines that implement the setting of v4 args for the misc. ops
 196  */
 197 static void     nfs4args_lock_free(nfs_argop4 *);
 198 static void     nfs4args_lockt_free(nfs_argop4 *);
 199 static void     nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
 200                         int, rnode4_t *, cred_t *, bitmap4, int *,
 201                         nfs4_stateid_types_t *);
 202 static void     nfs4args_setattr_free(nfs_argop4 *);
 203 static int      nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
 204                         bitmap4);
 205 static void     nfs4args_verify_free(nfs_argop4 *);
 206 static void     nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
 207                         WRITE4args **, nfs4_stateid_types_t *);
 208 
 209 /*
 210  * These are the vnode ops functions that implement the vnode interface to
 211  * the networked file system.  See more comments below at nfs4_vnodeops.
 212  */
 213 static int      nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
 214 static int      nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
 215                         caller_context_t *);
 216 static int      nfs4_read(vnode_t *, struct uio *, int, cred_t *,
 217                         caller_context_t *);
 218 static int      nfs4_write(vnode_t *, struct uio *, int, cred_t *,
 219                         caller_context_t *);
 220 static int      nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
 221                         caller_context_t *);
 222 static int      nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
 223                         caller_context_t *);
 224 static int      nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
 225 static int      nfs4_readlink(vnode_t *, struct uio *, cred_t *,
 226                         caller_context_t *);
 227 static int      nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
 228 static int      nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
 229                         int, vnode_t **, cred_t *, int, caller_context_t *,
 230                         vsecattr_t *);
 231 static int      nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
 232                         int);
 233 static int      nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
 234                         caller_context_t *, int);
 235 static int      nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
 236                         caller_context_t *, int);
 237 static int      nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
 238                         cred_t *, caller_context_t *, int, vsecattr_t *);
 239 static int      nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
 240                         caller_context_t *, int);
 241 static int      nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
 242                         cred_t *, caller_context_t *, int);
 243 static int      nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
 244                         caller_context_t *, int);
 245 static int      nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
 246 static int      nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
 247                         page_t *[], size_t, struct seg *, caddr_t,
 248                         enum seg_rw, cred_t *, caller_context_t *);
 249 static int      nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
 250                         caller_context_t *);
 251 static int      nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
 252                         uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
 253 static int      nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
 254                         uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
 255 static int      nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
 256 static int      nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
 257                         struct flk_callback *, cred_t *, caller_context_t *);
 258 static int      nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
 259                         cred_t *, caller_context_t *);
 260 static int      nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
 261                         uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
 262 static int      nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
 263                         cred_t *, caller_context_t *);
 264 static void     nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
 265                         caller_context_t *);
 266 static int      nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
 267                         caller_context_t *);
 268 /*
 269  * These vnode ops are required to be called from outside this source file,
 270  * e.g. by ephemeral mount stub vnode ops, and so may not be declared
 271  * as static.
 272  */
 273 int     nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
 274             caller_context_t *);
 275 void    nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
 276 int     nfs4_lookup(vnode_t *, char *, vnode_t **,
 277             struct pathname *, int, vnode_t *, cred_t *,
 278             caller_context_t *, int *, pathname_t *);
 279 int     nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
 280 int     nfs4_rwlock(vnode_t *, int, caller_context_t *);
 281 void    nfs4_rwunlock(vnode_t *, int, caller_context_t *);
 282 int     nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
 283 int     nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
 284             caller_context_t *);
 285 int     nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
 286             caller_context_t *);
 287 int     nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
 288             caller_context_t *);
 289 
 290 /*
 291  * Used for nfs4_commit_vp() to indicate if we should
 292  * wait on pending writes.
 293  */
 294 #define NFS4_WRITE_NOWAIT       0
 295 #define NFS4_WRITE_WAIT         1
 296 
 297 /*
 298  * Error flags used to pass information about certain special errors
 299  * which need to be handled specially.
 300  */
 301 #define NFS_EOF                 -98
 302 #define NFS_VERF_MISMATCH       -97
 303 
 304 /*
 305  * Flags used to differentiate between which operation drove the
 306  * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
 307  */
 308 #define NFS4_CLOSE_OP           0x1
 309 #define NFS4_DELMAP_OP          0x2
 310 #define NFS4_INACTIVE_OP        0x3
 311 
 312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
 313 
 314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
 315 #define ALIGN64(x, ptr, sz)                                             \
 316         x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1);           \
 317         if (x) {                                                        \
 318                 x = sizeof (uint64_t) - (x);                            \
 319                 sz -= (x);                                              \
 320                 ptr += (x);                                             \
 321         }
 322 
 323 #ifdef DEBUG
 324 int nfs4_client_attr_debug = 0;
 325 int nfs4_client_state_debug = 0;
 326 int nfs4_client_shadow_debug = 0;
 327 int nfs4_client_lock_debug = 0;
 328 int nfs4_seqid_sync = 0;
 329 int nfs4_client_map_debug = 0;
 330 static int nfs4_pageio_debug = 0;
 331 int nfs4_client_inactive_debug = 0;
 332 int nfs4_client_recov_debug = 0;
 333 int nfs4_client_failover_debug = 0;
 334 int nfs4_client_call_debug = 0;
 335 int nfs4_client_lookup_debug = 0;
 336 int nfs4_client_zone_debug = 0;
 337 int nfs4_lost_rqst_debug = 0;
 338 int nfs4_rdattrerr_debug = 0;
 339 int nfs4_open_stream_debug = 0;
 340 
 341 int nfs4read_error_inject;
 342 
 343 static int nfs4_create_misses = 0;
 344 
 345 static int nfs4_readdir_cache_shorts = 0;
 346 static int nfs4_readdir_readahead = 0;
 347 
 348 static int nfs4_bio_do_stop = 0;
 349 
 350 static int nfs4_lostpage = 0;   /* number of times we lost original page */
 351 
 352 int nfs4_mmap_debug = 0;
 353 
 354 static int nfs4_pathconf_cache_hits = 0;
 355 static int nfs4_pathconf_cache_misses = 0;
 356 
 357 int nfs4close_all_cnt;
 358 int nfs4close_one_debug = 0;
 359 int nfs4close_notw_debug = 0;
 360 
 361 int denied_to_flk_debug = 0;
 362 void *lockt_denied_debug;
 363 
 364 #endif
 365 
 366 /*
 367  * In milliseconds. Should be less than half of the lease time or better,
 368  * less than one second.
 369  */
 370 int nfs4_base_wait_time = 20;
 371 
 372 /*
 373  * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
 374  * or NFS4ERR_RESOURCE.
 375  */
 376 static int confirm_retry_sec = 30;
 377 
 378 static int nfs4_lookup_neg_cache = 1;
 379 
 380 /*
 381  * number of pages to read ahead
 382  * optimized for 100 base-T.
 383  */
 384 static int nfs4_nra = 4;
 385 
 386 static int nfs4_do_symlink_cache = 1;
 387 
 388 static int nfs4_pathconf_disable_cache = 0;
 389 
 390 /*
 391  * These are the vnode ops routines which implement the vnode interface to
 392  * the networked file system.  These routines just take their parameters,
 393  * make them look networkish by putting the right info into interface structs,
 394  * and then calling the appropriate remote routine(s) to do the work.
 395  *
 396  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
 397  * we purge the directory cache relative to that vnode.  This way, the
 398  * user won't get burned by the cache repeatedly.  See <nfs/rnode4.h> for
 399  * more details on rnode locking.
 400  */
 401 
 402 struct vnodeops *nfs4_vnodeops;
 403 
 404 const fs_operation_def_t nfs4_vnodeops_template[] = {
 405         VOPNAME_OPEN,           { .vop_open = nfs4_open },
 406         VOPNAME_CLOSE,          { .vop_close = nfs4_close },
 407         VOPNAME_READ,           { .vop_read = nfs4_read },
 408         VOPNAME_WRITE,          { .vop_write = nfs4_write },
 409         VOPNAME_IOCTL,          { .vop_ioctl = nfs4_ioctl },
 410         VOPNAME_GETATTR,        { .vop_getattr = nfs4_getattr },
 411         VOPNAME_SETATTR,        { .vop_setattr = nfs4_setattr },
 412         VOPNAME_ACCESS,         { .vop_access = nfs4_access },
 413         VOPNAME_LOOKUP,         { .vop_lookup = nfs4_lookup },
 414         VOPNAME_CREATE,         { .vop_create = nfs4_create },
 415         VOPNAME_REMOVE,         { .vop_remove = nfs4_remove },
 416         VOPNAME_LINK,           { .vop_link = nfs4_link },
 417         VOPNAME_RENAME,         { .vop_rename = nfs4_rename },
 418         VOPNAME_MKDIR,          { .vop_mkdir = nfs4_mkdir },
 419         VOPNAME_RMDIR,          { .vop_rmdir = nfs4_rmdir },
 420         VOPNAME_READDIR,        { .vop_readdir = nfs4_readdir },
 421         VOPNAME_SYMLINK,        { .vop_symlink = nfs4_symlink },
 422         VOPNAME_READLINK,       { .vop_readlink = nfs4_readlink },
 423         VOPNAME_FSYNC,          { .vop_fsync = nfs4_fsync },
 424         VOPNAME_INACTIVE,       { .vop_inactive = nfs4_inactive },
 425         VOPNAME_FID,            { .vop_fid = nfs4_fid },
 426         VOPNAME_RWLOCK,         { .vop_rwlock = nfs4_rwlock },
 427         VOPNAME_RWUNLOCK,       { .vop_rwunlock = nfs4_rwunlock },
 428         VOPNAME_SEEK,           { .vop_seek = nfs4_seek },
 429         VOPNAME_FRLOCK,         { .vop_frlock = nfs4_frlock },
 430         VOPNAME_SPACE,          { .vop_space = nfs4_space },
 431         VOPNAME_REALVP,         { .vop_realvp = nfs4_realvp },
 432         VOPNAME_GETPAGE,        { .vop_getpage = nfs4_getpage },
 433         VOPNAME_PUTPAGE,        { .vop_putpage = nfs4_putpage },
 434         VOPNAME_MAP,            { .vop_map = nfs4_map },
 435         VOPNAME_ADDMAP,         { .vop_addmap = nfs4_addmap },
 436         VOPNAME_DELMAP,         { .vop_delmap = nfs4_delmap },
 437         /* no separate nfs4_dump */
 438         VOPNAME_DUMP,           { .vop_dump = nfs_dump },
 439         VOPNAME_PATHCONF,       { .vop_pathconf = nfs4_pathconf },
 440         VOPNAME_PAGEIO,         { .vop_pageio = nfs4_pageio },
 441         VOPNAME_DISPOSE,        { .vop_dispose = nfs4_dispose },
 442         VOPNAME_SETSECATTR,     { .vop_setsecattr = nfs4_setsecattr },
 443         VOPNAME_GETSECATTR,     { .vop_getsecattr = nfs4_getsecattr },
 444         VOPNAME_SHRLOCK,        { .vop_shrlock = nfs4_shrlock },
 445         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
 446         NULL,                   NULL
 447 };
 448 
 449 /*
 450  * The following are subroutines and definitions to set args or get res
 451  * for the different nfsv4 ops
 452  */
 453 
 454 void
 455 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
 456 {
 457         int             i;
 458 
 459         for (i = 0; i < arglen; i++) {
 460                 if (argop[i].argop == OP_LOOKUP) {
 461                         kmem_free(
 462                             argop[i].nfs_argop4_u.oplookup.
 463                             objname.utf8string_val,
 464                             argop[i].nfs_argop4_u.oplookup.
 465                             objname.utf8string_len);
 466                 }
 467         }
 468 }
 469 
 470 static void
 471 nfs4args_lock_free(nfs_argop4 *argop)
 472 {
 473         locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
 474 
 475         if (locker->new_lock_owner == TRUE) {
 476                 open_to_lock_owner4 *open_owner;
 477 
 478                 open_owner = &locker->locker4_u.open_owner;
 479                 if (open_owner->lock_owner.owner_val != NULL) {
 480                         kmem_free(open_owner->lock_owner.owner_val,
 481                             open_owner->lock_owner.owner_len);
 482                 }
 483         }
 484 }
 485 
 486 static void
 487 nfs4args_lockt_free(nfs_argop4 *argop)
 488 {
 489         lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
 490 
 491         if (lowner->owner_val != NULL) {
 492                 kmem_free(lowner->owner_val, lowner->owner_len);
 493         }
 494 }
 495 
 496 static void
 497 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
 498     rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
 499     nfs4_stateid_types_t *sid_types)
 500 {
 501         fattr4          *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
 502         mntinfo4_t      *mi;
 503 
 504         argop->argop = OP_SETATTR;
 505         /*
 506          * The stateid is set to 0 if client is not modifying the size
 507          * and otherwise to whatever nfs4_get_stateid() returns.
 508          *
 509          * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
 510          * state struct could be found for the process/file pair.  We may
 511          * want to change this in the future (by OPENing the file).  See
 512          * bug # 4474852.
 513          */
 514         if (vap->va_mask & AT_SIZE) {
 515 
 516                 ASSERT(rp != NULL);
 517                 mi = VTOMI4(RTOV4(rp));
 518 
 519                 argop->nfs_argop4_u.opsetattr.stateid =
 520                     nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
 521                     OP_SETATTR, sid_types, FALSE);
 522         } else {
 523                 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
 524                     sizeof (stateid4));
 525         }
 526 
 527         *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
 528         if (*error)
 529                 bzero(attr, sizeof (*attr));
 530 }
 531 
 532 static void
 533 nfs4args_setattr_free(nfs_argop4 *argop)
 534 {
 535         nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
 536 }
 537 
 538 static int
 539 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
 540     bitmap4 supp)
 541 {
 542         fattr4 *attr;
 543         int error = 0;
 544 
 545         argop->argop = op;
 546         switch (op) {
 547         case OP_VERIFY:
 548                 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
 549                 break;
 550         case OP_NVERIFY:
 551                 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
 552                 break;
 553         default:
 554                 return (EINVAL);
 555         }
 556         if (!error)
 557                 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
 558         if (error)
 559                 bzero(attr, sizeof (*attr));
 560         return (error);
 561 }
 562 
 563 static void
 564 nfs4args_verify_free(nfs_argop4 *argop)
 565 {
 566         switch (argop->argop) {
 567         case OP_VERIFY:
 568                 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
 569                 break;
 570         case OP_NVERIFY:
 571                 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
 572                 break;
 573         default:
 574                 break;
 575         }
 576 }
 577 
 578 static void
 579 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
 580     WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
 581 {
 582         WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
 583         mntinfo4_t *mi = VTOMI4(RTOV4(rp));
 584 
 585         argop->argop = OP_WRITE;
 586         wargs->stable = stable;
 587         wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
 588             mi, OP_WRITE, sid_tp);
 589         wargs->mblk = NULL;
 590         *wargs_pp = wargs;
 591 }
 592 
 593 void
 594 nfs4args_copen_free(OPEN4cargs *open_args)
 595 {
 596         if (open_args->owner.owner_val) {
 597                 kmem_free(open_args->owner.owner_val,
 598                     open_args->owner.owner_len);
 599         }
 600         if ((open_args->opentype == OPEN4_CREATE) &&
 601             (open_args->mode != EXCLUSIVE4)) {
 602                 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
 603         }
 604 }
 605 
 606 /*
 607  * XXX:  This is referenced in modstubs.s
 608  */
 609 struct vnodeops *
 610 nfs4_getvnodeops(void)
 611 {
 612         return (nfs4_vnodeops);
 613 }
 614 
 615 /*
 616  * The OPEN operation opens a regular file.
 617  */
 618 /*ARGSUSED3*/
 619 static int
 620 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
 621 {
 622         vnode_t *dvp = NULL;
 623         rnode4_t *rp, *drp;
 624         int error;
 625         int just_been_created;
 626         char fn[MAXNAMELEN];
 627 
 628         NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
 629         if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
 630                 return (EIO);
 631         rp = VTOR4(*vpp);
 632 
 633         /*
 634          * Check to see if opening something besides a regular file;
 635          * if so skip the OTW call
 636          */
 637         if ((*vpp)->v_type != VREG) {
 638                 error = nfs4_open_non_reg_file(vpp, flag, cr);
 639                 return (error);
 640         }
 641 
 642         /*
 643          * XXX - would like a check right here to know if the file is
 644          * executable or not, so as to skip OTW
 645          */
 646 
 647         if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
 648                 return (error);
 649 
 650         drp = VTOR4(dvp);
 651         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
 652                 return (EINTR);
 653 
 654         if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
 655                 nfs_rw_exit(&drp->r_rwlock);
 656                 return (error);
 657         }
 658 
 659         /*
 660          * See if this file has just been CREATEd.
 661          * If so, clear the flag and update the dnlc, which was previously
 662          * skipped in nfs4_create.
 663          * XXX need better serilization on this.
 664          * XXX move this into the nf4open_otw call, after we have
 665          * XXX acquired the open owner seqid sync.
 666          */
 667         mutex_enter(&rp->r_statev4_lock);
 668         if (rp->created_v4) {
 669                 rp->created_v4 = 0;
 670                 mutex_exit(&rp->r_statev4_lock);
 671 
 672                 dnlc_update(dvp, fn, *vpp);
 673                 /* This is needed so we don't bump the open ref count */
 674                 just_been_created = 1;
 675         } else {
 676                 mutex_exit(&rp->r_statev4_lock);
 677                 just_been_created = 0;
 678         }
 679 
 680         /*
 681          * If caller specified O_TRUNC/FTRUNC, then be sure to set
 682          * FWRITE (to drive successful setattr(size=0) after open)
 683          */
 684         if (flag & FTRUNC)
 685                 flag |= FWRITE;
 686 
 687         error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
 688             just_been_created);
 689 
 690         if (!error && !((*vpp)->v_flag & VROOT))
 691                 dnlc_update(dvp, fn, *vpp);
 692 
 693         nfs_rw_exit(&drp->r_rwlock);
 694 
 695         /* release the hold from vtodv */
 696         VN_RELE(dvp);
 697 
 698         /* exchange the shadow for the master vnode, if needed */
 699 
 700         if (error == 0 && IS_SHADOW(*vpp, rp))
 701                 sv_exchange(vpp);
 702 
 703         return (error);
 704 }
 705 
 706 /*
 707  * See if there's a "lost open" request to be saved and recovered.
 708  */
 709 static void
 710 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
 711     nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
 712     vnode_t *dvp, OPEN4cargs *open_args)
 713 {
 714         vfs_t *vfsp;
 715         char *srccfp;
 716 
 717         vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
 718 
 719         if (error != ETIMEDOUT && error != EINTR &&
 720             !NFS4_FRC_UNMT_ERR(error, vfsp)) {
 721                 lost_rqstp->lr_op = 0;
 722                 return;
 723         }
 724 
 725         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
 726             "nfs4open_save_lost_rqst: error %d", error));
 727 
 728         lost_rqstp->lr_op = OP_OPEN;
 729 
 730         /*
 731          * The vp (if it is not NULL) and dvp are held and rele'd via
 732          * the recovery code.  See nfs4_save_lost_rqst.
 733          */
 734         lost_rqstp->lr_vp = vp;
 735         lost_rqstp->lr_dvp = dvp;
 736         lost_rqstp->lr_oop = oop;
 737         lost_rqstp->lr_osp = NULL;
 738         lost_rqstp->lr_lop = NULL;
 739         lost_rqstp->lr_cr = cr;
 740         lost_rqstp->lr_flk = NULL;
 741         lost_rqstp->lr_oacc = open_args->share_access;
 742         lost_rqstp->lr_odeny = open_args->share_deny;
 743         lost_rqstp->lr_oclaim = open_args->claim;
 744         if (open_args->claim == CLAIM_DELEGATE_CUR) {
 745                 lost_rqstp->lr_ostateid =
 746                     open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
 747                 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
 748         } else {
 749                 srccfp = open_args->open_claim4_u.cfile;
 750         }
 751         lost_rqstp->lr_ofile.utf8string_len = 0;
 752         lost_rqstp->lr_ofile.utf8string_val = NULL;
 753         (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
 754         lost_rqstp->lr_putfirst = FALSE;
 755 }
 756 
 757 struct nfs4_excl_time {
 758         uint32 seconds;
 759         uint32 nseconds;
 760 };
 761 
 762 /*
 763  * The OPEN operation creates and/or opens a regular file
 764  *
 765  * ARGSUSED
 766  */
 767 static int
 768 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
 769     vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
 770     enum createmode4 createmode, int file_just_been_created)
 771 {
 772         rnode4_t *rp;
 773         rnode4_t *drp = VTOR4(dvp);
 774         vnode_t *vp = NULL;
 775         vnode_t *vpi = *vpp;
 776         bool_t needrecov = FALSE;
 777 
 778         int doqueue = 1;
 779 
 780         COMPOUND4args_clnt args;
 781         COMPOUND4res_clnt res;
 782         nfs_argop4 *argop;
 783         nfs_resop4 *resop;
 784         int argoplist_size;
 785         int idx_open, idx_fattr;
 786 
 787         GETFH4res *gf_res = NULL;
 788         OPEN4res *op_res = NULL;
 789         nfs4_ga_res_t *garp;
 790         fattr4 *attr = NULL;
 791         struct nfs4_excl_time verf;
 792         bool_t did_excl_setup = FALSE;
 793         int created_osp;
 794 
 795         OPEN4cargs *open_args;
 796         nfs4_open_owner_t       *oop = NULL;
 797         nfs4_open_stream_t      *osp = NULL;
 798         seqid4 seqid = 0;
 799         bool_t retry_open = FALSE;
 800         nfs4_recov_state_t recov_state;
 801         nfs4_lost_rqst_t lost_rqst;
 802         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
 803         hrtime_t t;
 804         int acc = 0;
 805         cred_t *cred_otw = NULL;        /* cred used to do the RPC call */
 806         cred_t *ncr = NULL;
 807 
 808         nfs4_sharedfh_t *otw_sfh;
 809         nfs4_sharedfh_t *orig_sfh;
 810         int fh_differs = 0;
 811         int numops, setgid_flag;
 812         int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
 813 
 814         /*
 815          * Make sure we properly deal with setting the right gid on
 816          * a newly created file to reflect the parent's setgid bit
 817          */
 818         setgid_flag = 0;
 819         if (create_flag && in_va) {
 820 
 821                 /*
 822                  * If there is grpid mount flag used or
 823                  * the parent's directory has the setgid bit set
 824                  * _and_ the client was able to get a valid mapping
 825                  * for the parent dir's owner_group, we want to
 826                  * append NVERIFY(owner_group == dva.va_gid) and
 827                  * SETATTR to the CREATE compound.
 828                  */
 829                 mutex_enter(&drp->r_statelock);
 830                 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
 831                     drp->r_attr.va_mode & VSGID) &&
 832                     drp->r_attr.va_gid != GID_NOBODY) {
 833                         in_va->va_mask |= AT_GID;
 834                         in_va->va_gid = drp->r_attr.va_gid;
 835                         setgid_flag = 1;
 836                 }
 837                 mutex_exit(&drp->r_statelock);
 838         }
 839 
 840         /*
 841          * Normal/non-create compound:
 842          * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
 843          *
 844          * Open(create) compound no setgid:
 845          * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
 846          * RESTOREFH + GETATTR
 847          *
 848          * Open(create) setgid:
 849          * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
 850          * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
 851          * NVERIFY(grp) + SETATTR
 852          */
 853         if (setgid_flag) {
 854                 numops = 10;
 855                 idx_open = 1;
 856                 idx_fattr = 3;
 857         } else if (create_flag) {
 858                 numops = 7;
 859                 idx_open = 2;
 860                 idx_fattr = 4;
 861         } else {
 862                 numops = 4;
 863                 idx_open = 1;
 864                 idx_fattr = 3;
 865         }
 866 
 867         args.array_len = numops;
 868         argoplist_size = numops * sizeof (nfs_argop4);
 869         argop = kmem_alloc(argoplist_size, KM_SLEEP);
 870 
 871         NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
 872             "open %s open flag 0x%x cred %p", file_name, open_flag,
 873             (void *)cr));
 874 
 875         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
 876         if (create_flag) {
 877                 /*
 878                  * We are to create a file.  Initialize the passed in vnode
 879                  * pointer.
 880                  */
 881                 vpi = NULL;
 882         } else {
 883                 /*
 884                  * Check to see if the client owns a read delegation and is
 885                  * trying to open for write.  If so, then return the delegation
 886                  * to avoid the server doing a cb_recall and returning DELAY.
 887                  * NB - we don't use the statev4_lock here because we'd have
 888                  * to drop the lock anyway and the result would be stale.
 889                  */
 890                 if ((open_flag & FWRITE) &&
 891                     VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
 892                         (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
 893 
 894                 /*
 895                  * If the file has a delegation, then do an access check up
 896                  * front.  This avoids having to an access check later after
 897                  * we've already done start_op, which could deadlock.
 898                  */
 899                 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
 900                         if (open_flag & FREAD &&
 901                             nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
 902                                 acc |= VREAD;
 903                         if (open_flag & FWRITE &&
 904                             nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
 905                                 acc |= VWRITE;
 906                 }
 907         }
 908 
 909         drp = VTOR4(dvp);
 910 
 911         recov_state.rs_flags = 0;
 912         recov_state.rs_num_retry_despite_err = 0;
 913         cred_otw = cr;
 914 
 915 recov_retry:
 916         fh_differs = 0;
 917         nfs4_error_zinit(&e);
 918 
 919         e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
 920         if (e.error) {
 921                 if (ncr != NULL)
 922                         crfree(ncr);
 923                 kmem_free(argop, argoplist_size);
 924                 return (e.error);
 925         }
 926 
 927         args.ctag = TAG_OPEN;
 928         args.array_len = numops;
 929         args.array = argop;
 930 
 931         /* putfh directory fh */
 932         argop[0].argop = OP_CPUTFH;
 933         argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
 934 
 935         /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
 936         argop[idx_open].argop = OP_COPEN;
 937         open_args = &argop[idx_open].nfs_argop4_u.opcopen;
 938         open_args->claim = CLAIM_NULL;
 939 
 940         /* name of file */
 941         open_args->open_claim4_u.cfile = file_name;
 942         open_args->owner.owner_len = 0;
 943         open_args->owner.owner_val = NULL;
 944 
 945         if (create_flag) {
 946                 /* CREATE a file */
 947                 open_args->opentype = OPEN4_CREATE;
 948                 open_args->mode = createmode;
 949                 if (createmode == EXCLUSIVE4) {
 950                         if (did_excl_setup == FALSE) {
 951                                 verf.seconds = zone_get_hostid(NULL);
 952                                 if (verf.seconds != 0)
 953                                         verf.nseconds = newnum();
 954                                 else {
 955                                         timestruc_t now;
 956 
 957                                         gethrestime(&now);
 958                                         verf.seconds = now.tv_sec;
 959                                         verf.nseconds = now.tv_nsec;
 960                                 }
 961                                 /*
 962                                  * Since the server will use this value for the
 963                                  * mtime, make sure that it can't overflow. Zero
 964                                  * out the MSB. The actual value does not matter
 965                                  * here, only its uniqeness.
 966                                  */
 967                                 verf.seconds &= INT32_MAX;
 968                                 did_excl_setup = TRUE;
 969                         }
 970 
 971                         /* Now copy over verifier to OPEN4args. */
 972                         open_args->createhow4_u.createverf = *(uint64_t *)&verf;
 973                 } else {
 974                         int v_error;
 975                         bitmap4 supp_attrs;
 976                         servinfo4_t *svp;
 977 
 978                         attr = &open_args->createhow4_u.createattrs;
 979 
 980                         svp = drp->r_server;
 981                         (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
 982                         supp_attrs = svp->sv_supp_attrs;
 983                         nfs_rw_exit(&svp->sv_lock);
 984 
 985                         /* GUARDED4 or UNCHECKED4 */
 986                         v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
 987                             supp_attrs);
 988                         if (v_error) {
 989                                 bzero(attr, sizeof (*attr));
 990                                 nfs4args_copen_free(open_args);
 991                                 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
 992                                     &recov_state, FALSE);
 993                                 if (ncr != NULL)
 994                                         crfree(ncr);
 995                                 kmem_free(argop, argoplist_size);
 996                                 return (v_error);
 997                         }
 998                 }
 999         } else {
1000                 /* NO CREATE */
1001                 open_args->opentype = OPEN4_NOCREATE;
1002         }
1003 
1004         if (recov_state.rs_sp != NULL) {
1005                 mutex_enter(&recov_state.rs_sp->s_lock);
1006                 open_args->owner.clientid = recov_state.rs_sp->clientid;
1007                 mutex_exit(&recov_state.rs_sp->s_lock);
1008         } else {
1009                 /* XXX should we just fail here? */
1010                 open_args->owner.clientid = 0;
1011         }
1012 
1013         /*
1014          * This increments oop's ref count or creates a temporary 'just_created'
1015          * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1016          * completes.
1017          */
1018         mutex_enter(&VTOMI4(dvp)->mi_lock);
1019 
1020         /* See if a permanent or just created open owner exists */
1021         oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1022         if (!oop) {
1023                 /*
1024                  * This open owner does not exist so create a temporary
1025                  * just created one.
1026                  */
1027                 oop = create_open_owner(cr, VTOMI4(dvp));
1028                 ASSERT(oop != NULL);
1029         }
1030         mutex_exit(&VTOMI4(dvp)->mi_lock);
1031 
1032         /* this length never changes, do alloc before seqid sync */
1033         open_args->owner.owner_len = sizeof (oop->oo_name);
1034         open_args->owner.owner_val =
1035             kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1036 
1037         e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1038         if (e.error == EAGAIN) {
1039                 open_owner_rele(oop);
1040                 nfs4args_copen_free(open_args);
1041                 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1042                 if (ncr != NULL) {
1043                         crfree(ncr);
1044                         ncr = NULL;
1045                 }
1046                 goto recov_retry;
1047         }
1048 
1049         /* Check to see if we need to do the OTW call */
1050         if (!create_flag) {
1051                 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1052                     file_just_been_created, &e.error, acc, &recov_state)) {
1053 
1054                         /*
1055                          * The OTW open is not necessary.  Either
1056                          * the open can succeed without it (eg.
1057                          * delegation, error == 0) or the open
1058                          * must fail due to an access failure
1059                          * (error != 0).  In either case, tidy
1060                          * up and return.
1061                          */
1062 
1063                         nfs4_end_open_seqid_sync(oop);
1064                         open_owner_rele(oop);
1065                         nfs4args_copen_free(open_args);
1066                         nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1067                         if (ncr != NULL)
1068                                 crfree(ncr);
1069                         kmem_free(argop, argoplist_size);
1070                         return (e.error);
1071                 }
1072         }
1073 
1074         bcopy(&oop->oo_name, open_args->owner.owner_val,
1075             open_args->owner.owner_len);
1076 
1077         seqid = nfs4_get_open_seqid(oop) + 1;
1078         open_args->seqid = seqid;
1079         open_args->share_access = 0;
1080         if (open_flag & FREAD)
1081                 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1082         if (open_flag & FWRITE)
1083                 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1084         open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1085 
1086 
1087 
1088         /*
1089          * getfh w/sanity check for idx_open/idx_fattr
1090          */
1091         ASSERT((idx_open + 1) == (idx_fattr - 1));
1092         argop[idx_open + 1].argop = OP_GETFH;
1093 
1094         /* getattr */
1095         argop[idx_fattr].argop = OP_GETATTR;
1096         argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1097         argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1098 
1099         if (setgid_flag) {
1100                 vattr_t _v;
1101                 servinfo4_t *svp;
1102                 bitmap4 supp_attrs;
1103 
1104                 svp = drp->r_server;
1105                 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1106                 supp_attrs = svp->sv_supp_attrs;
1107                 nfs_rw_exit(&svp->sv_lock);
1108 
1109                 /*
1110                  * For setgid case, we need to:
1111                  * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1112                  */
1113                 argop[4].argop = OP_SAVEFH;
1114 
1115                 argop[5].argop = OP_CPUTFH;
1116                 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1117 
1118                 argop[6].argop = OP_GETATTR;
1119                 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1120                 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1121 
1122                 argop[7].argop = OP_RESTOREFH;
1123 
1124                 /*
1125                  * nverify
1126                  */
1127                 _v.va_mask = AT_GID;
1128                 _v.va_gid = in_va->va_gid;
1129                 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1130                     supp_attrs))) {
1131 
1132                         /*
1133                          * setattr
1134                          *
1135                          * We _know_ we're not messing with AT_SIZE or
1136                          * AT_XTIME, so no need for stateid or flags.
1137                          * Also we specify NULL rp since we're only
1138                          * interested in setting owner_group attributes.
1139                          */
1140                         nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1141                             supp_attrs, &e.error, 0);
1142                         if (e.error)
1143                                 nfs4args_verify_free(&argop[8]);
1144                 }
1145 
1146                 if (e.error) {
1147                         /*
1148                          * XXX - Revisit the last argument to nfs4_end_op()
1149                          *       once 5020486 is fixed.
1150                          */
1151                         nfs4_end_open_seqid_sync(oop);
1152                         open_owner_rele(oop);
1153                         nfs4args_copen_free(open_args);
1154                         nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1155                         if (ncr != NULL)
1156                                 crfree(ncr);
1157                         kmem_free(argop, argoplist_size);
1158                         return (e.error);
1159                 }
1160         } else if (create_flag) {
1161                 argop[1].argop = OP_SAVEFH;
1162 
1163                 argop[5].argop = OP_RESTOREFH;
1164 
1165                 argop[6].argop = OP_GETATTR;
1166                 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1167                 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1168         }
1169 
1170         NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1171             "nfs4open_otw: %s call, nm %s, rp %s",
1172             needrecov ? "recov" : "first", file_name,
1173             rnode4info(VTOR4(dvp))));
1174 
1175         t = gethrtime();
1176 
1177         rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1178 
1179         if (!e.error && nfs4_need_to_bump_seqid(&res))
1180                 nfs4_set_open_seqid(seqid, oop, args.ctag);
1181 
1182         needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1183 
1184         if (e.error || needrecov) {
1185                 bool_t abort = FALSE;
1186 
1187                 if (needrecov) {
1188                         nfs4_bseqid_entry_t *bsep = NULL;
1189 
1190                         nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1191                             cred_otw, vpi, dvp, open_args);
1192 
1193                         if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1194                                 bsep = nfs4_create_bseqid_entry(oop, NULL,
1195                                     vpi, 0, args.ctag, open_args->seqid);
1196                                 num_bseqid_retry--;
1197                         }
1198 
1199                         abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1200                             NULL, lost_rqst.lr_op == OP_OPEN ?
1201                             &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1202 
1203                         if (bsep)
1204                                 kmem_free(bsep, sizeof (*bsep));
1205                         /* give up if we keep getting BAD_SEQID */
1206                         if (num_bseqid_retry == 0)
1207                                 abort = TRUE;
1208                         if (abort == TRUE && e.error == 0)
1209                                 e.error = geterrno4(res.status);
1210                 }
1211                 nfs4_end_open_seqid_sync(oop);
1212                 open_owner_rele(oop);
1213                 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1214                 nfs4args_copen_free(open_args);
1215                 if (setgid_flag) {
1216                         nfs4args_verify_free(&argop[8]);
1217                         nfs4args_setattr_free(&argop[9]);
1218                 }
1219                 if (!e.error)
1220                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1221                 if (ncr != NULL) {
1222                         crfree(ncr);
1223                         ncr = NULL;
1224                 }
1225                 if (!needrecov || abort == TRUE || e.error == EINTR ||
1226                     NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1227                         kmem_free(argop, argoplist_size);
1228                         return (e.error);
1229                 }
1230                 goto recov_retry;
1231         }
1232 
1233         /*
1234          * Will check and update lease after checking the rflag for
1235          * OPEN_CONFIRM in the successful OPEN call.
1236          */
1237         if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1238 
1239                 /*
1240                  * XXX what if we're crossing mount points from server1:/drp
1241                  * to server2:/drp/rp.
1242                  */
1243 
1244                 /* Signal our end of use of the open seqid */
1245                 nfs4_end_open_seqid_sync(oop);
1246 
1247                 /*
1248                  * This will destroy the open owner if it was just created,
1249                  * and no one else has put a reference on it.
1250                  */
1251                 open_owner_rele(oop);
1252                 if (create_flag && (createmode != EXCLUSIVE4) &&
1253                     res.status == NFS4ERR_BADOWNER)
1254                         nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1255 
1256                 e.error = geterrno4(res.status);
1257                 nfs4args_copen_free(open_args);
1258                 if (setgid_flag) {
1259                         nfs4args_verify_free(&argop[8]);
1260                         nfs4args_setattr_free(&argop[9]);
1261                 }
1262                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1263                 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1264                 /*
1265                  * If the reply is NFS4ERR_ACCESS, it may be because
1266                  * we are root (no root net access).  If the real uid
1267                  * is not root, then retry with the real uid instead.
1268                  */
1269                 if (ncr != NULL) {
1270                         crfree(ncr);
1271                         ncr = NULL;
1272                 }
1273                 if (res.status == NFS4ERR_ACCESS &&
1274                     (ncr = crnetadjust(cred_otw)) != NULL) {
1275                         cred_otw = ncr;
1276                         goto recov_retry;
1277                 }
1278                 kmem_free(argop, argoplist_size);
1279                 return (e.error);
1280         }
1281 
1282         resop = &res.array[idx_open];  /* open res */
1283         op_res = &resop->nfs_resop4_u.opopen;
1284 
1285 #ifdef DEBUG
1286         /*
1287          * verify attrset bitmap
1288          */
1289         if (create_flag &&
1290             (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1291                 /* make sure attrset returned is what we asked for */
1292                 /* XXX Ignore this 'error' for now */
1293                 if (attr->attrmask != op_res->attrset)
1294                         /* EMPTY */;
1295         }
1296 #endif
1297 
1298         if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1299                 mutex_enter(&VTOMI4(dvp)->mi_lock);
1300                 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1301                 mutex_exit(&VTOMI4(dvp)->mi_lock);
1302         }
1303 
1304         resop = &res.array[idx_open + 1];  /* getfh res */
1305         gf_res = &resop->nfs_resop4_u.opgetfh;
1306 
1307         otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1308 
1309         /*
1310          * The open stateid has been updated on the server but not
1311          * on the client yet.  There is a path: makenfs4node->nfs4_attr_cache->
1312          * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1313          * WRITE call.  That, however, will use the old stateid, so go ahead
1314          * and upate the open stateid now, before any call to makenfs4node.
1315          */
1316         if (vpi) {
1317                 nfs4_open_stream_t      *tmp_osp;
1318                 rnode4_t                *tmp_rp = VTOR4(vpi);
1319 
1320                 tmp_osp = find_open_stream(oop, tmp_rp);
1321                 if (tmp_osp) {
1322                         tmp_osp->open_stateid = op_res->stateid;
1323                         mutex_exit(&tmp_osp->os_sync_lock);
1324                         open_stream_rele(tmp_osp, tmp_rp);
1325                 }
1326 
1327                 /*
1328                  * We must determine if the file handle given by the otw open
1329                  * is the same as the file handle which was passed in with
1330                  * *vpp.  This case can be reached if the file we are trying
1331                  * to open has been removed and another file has been created
1332                  * having the same file name.  The passed in vnode is released
1333                  * later.
1334                  */
1335                 orig_sfh = VTOR4(vpi)->r_fh;
1336                 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1337         }
1338 
1339         garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1340 
1341         if (create_flag || fh_differs) {
1342                 int rnode_err = 0;
1343 
1344                 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1345                     dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1346 
1347                 if (e.error)
1348                         PURGE_ATTRCACHE4(vp);
1349                 /*
1350                  * For the newly created vp case, make sure the rnode
1351                  * isn't bad before using it.
1352                  */
1353                 mutex_enter(&(VTOR4(vp))->r_statelock);
1354                 if (VTOR4(vp)->r_flags & R4RECOVERR)
1355                         rnode_err = EIO;
1356                 mutex_exit(&(VTOR4(vp))->r_statelock);
1357 
1358                 if (rnode_err) {
1359                         nfs4_end_open_seqid_sync(oop);
1360                         nfs4args_copen_free(open_args);
1361                         if (setgid_flag) {
1362                                 nfs4args_verify_free(&argop[8]);
1363                                 nfs4args_setattr_free(&argop[9]);
1364                         }
1365                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1366                         nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1367                             needrecov);
1368                         open_owner_rele(oop);
1369                         VN_RELE(vp);
1370                         if (ncr != NULL)
1371                                 crfree(ncr);
1372                         sfh4_rele(&otw_sfh);
1373                         kmem_free(argop, argoplist_size);
1374                         return (EIO);
1375                 }
1376         } else {
1377                 vp = vpi;
1378         }
1379         sfh4_rele(&otw_sfh);
1380 
1381         /*
1382          * It seems odd to get a full set of attrs and then not update
1383          * the object's attrcache in the non-create case.  Create case uses
1384          * the attrs since makenfs4node checks to see if the attrs need to
1385          * be updated (and then updates them).  The non-create case should
1386          * update attrs also.
1387          */
1388         if (! create_flag && ! fh_differs && !e.error) {
1389                 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1390         }
1391 
1392         nfs4_error_zinit(&e);
1393         if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1394                 /* This does not do recovery for vp explicitly. */
1395                 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1396                     &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1397 
1398                 if (e.error || e.stat) {
1399                         nfs4_end_open_seqid_sync(oop);
1400                         nfs4args_copen_free(open_args);
1401                         if (setgid_flag) {
1402                                 nfs4args_verify_free(&argop[8]);
1403                                 nfs4args_setattr_free(&argop[9]);
1404                         }
1405                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1406                         nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1407                             needrecov);
1408                         open_owner_rele(oop);
1409                         if (create_flag || fh_differs) {
1410                                 /* rele the makenfs4node */
1411                                 VN_RELE(vp);
1412                         }
1413                         if (ncr != NULL) {
1414                                 crfree(ncr);
1415                                 ncr = NULL;
1416                         }
1417                         if (retry_open == TRUE) {
1418                                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1419                                     "nfs4open_otw: retry the open since OPEN "
1420                                     "CONFIRM failed with error %d stat %d",
1421                                     e.error, e.stat));
1422                                 if (create_flag && createmode == GUARDED4) {
1423                                         NFS4_DEBUG(nfs4_client_recov_debug,
1424                                             (CE_NOTE, "nfs4open_otw: switch "
1425                                             "createmode from GUARDED4 to "
1426                                             "UNCHECKED4"));
1427                                         createmode = UNCHECKED4;
1428                                 }
1429                                 goto recov_retry;
1430                         }
1431                         if (!e.error) {
1432                                 if (create_flag && (createmode != EXCLUSIVE4) &&
1433                                     e.stat == NFS4ERR_BADOWNER)
1434                                         nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1435 
1436                                 e.error = geterrno4(e.stat);
1437                         }
1438                         kmem_free(argop, argoplist_size);
1439                         return (e.error);
1440                 }
1441         }
1442 
1443         rp = VTOR4(vp);
1444 
1445         mutex_enter(&rp->r_statev4_lock);
1446         if (create_flag)
1447                 rp->created_v4 = 1;
1448         mutex_exit(&rp->r_statev4_lock);
1449 
1450         mutex_enter(&oop->oo_lock);
1451         /* Doesn't matter if 'oo_just_created' already was set as this */
1452         oop->oo_just_created = NFS4_PERM_CREATED;
1453         if (oop->oo_cred_otw)
1454                 crfree(oop->oo_cred_otw);
1455         oop->oo_cred_otw = cred_otw;
1456         crhold(oop->oo_cred_otw);
1457         mutex_exit(&oop->oo_lock);
1458 
1459         /* returns with 'os_sync_lock' held */
1460         osp = find_or_create_open_stream(oop, rp, &created_osp);
1461         if (!osp) {
1462                 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1463                     "nfs4open_otw: failed to create an open stream"));
1464                 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1465                     "signal our end of use of the open seqid"));
1466 
1467                 nfs4_end_open_seqid_sync(oop);
1468                 open_owner_rele(oop);
1469                 nfs4args_copen_free(open_args);
1470                 if (setgid_flag) {
1471                         nfs4args_verify_free(&argop[8]);
1472                         nfs4args_setattr_free(&argop[9]);
1473                 }
1474                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1475                 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1476                 if (create_flag || fh_differs)
1477                         VN_RELE(vp);
1478                 if (ncr != NULL)
1479                         crfree(ncr);
1480 
1481                 kmem_free(argop, argoplist_size);
1482                 return (EINVAL);
1483 
1484         }
1485 
1486         osp->open_stateid = op_res->stateid;
1487 
1488         if (open_flag & FREAD)
1489                 osp->os_share_acc_read++;
1490         if (open_flag & FWRITE)
1491                 osp->os_share_acc_write++;
1492         osp->os_share_deny_none++;
1493 
1494         /*
1495          * Need to reset this bitfield for the possible case where we were
1496          * going to OTW CLOSE the file, got a non-recoverable error, and before
1497          * we could retry the CLOSE, OPENed the file again.
1498          */
1499         ASSERT(osp->os_open_owner->oo_seqid_inuse);
1500         osp->os_final_close = 0;
1501         osp->os_force_close = 0;
1502 #ifdef DEBUG
1503         if (osp->os_failed_reopen)
1504                 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1505                     " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1506                     (void *)osp, (void *)cr, rnode4info(rp)));
1507 #endif
1508         osp->os_failed_reopen = 0;
1509 
1510         mutex_exit(&osp->os_sync_lock);
1511 
1512         nfs4_end_open_seqid_sync(oop);
1513 
1514         if (created_osp && recov_state.rs_sp != NULL) {
1515                 mutex_enter(&recov_state.rs_sp->s_lock);
1516                 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1517                 mutex_exit(&recov_state.rs_sp->s_lock);
1518         }
1519 
1520         /* get rid of our reference to find oop */
1521         open_owner_rele(oop);
1522 
1523         open_stream_rele(osp, rp);
1524 
1525         /* accept delegation, if any */
1526         nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1527 
1528         nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1529 
1530         if (createmode == EXCLUSIVE4 &&
1531             (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1532                 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1533                     " EXCLUSIVE4: sending a SETATTR"));
1534                 /*
1535                  * If doing an exclusive create, then generate
1536                  * a SETATTR to set the initial attributes.
1537                  * Try to set the mtime and the atime to the
1538                  * server's current time.  It is somewhat
1539                  * expected that these fields will be used to
1540                  * store the exclusive create cookie.  If not,
1541                  * server implementors will need to know that
1542                  * a SETATTR will follow an exclusive create
1543                  * and the cookie should be destroyed if
1544                  * appropriate.
1545                  *
1546                  * The AT_GID and AT_SIZE bits are turned off
1547                  * so that the SETATTR request will not attempt
1548                  * to process these.  The gid will be set
1549                  * separately if appropriate.  The size is turned
1550                  * off because it is assumed that a new file will
1551                  * be created empty and if the file wasn't empty,
1552                  * then the exclusive create will have failed
1553                  * because the file must have existed already.
1554                  * Therefore, no truncate operation is needed.
1555                  */
1556                 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1557                 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1558 
1559                 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1560                 if (e.error) {
1561                         /*
1562                          * Couldn't correct the attributes of
1563                          * the newly created file and the
1564                          * attributes are wrong.  Remove the
1565                          * file and return an error to the
1566                          * application.
1567                          */
1568                         /* XXX will this take care of client state ? */
1569                         NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1570                             "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1571                             " remove file", e.error));
1572                         VN_RELE(vp);
1573                         (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1574                         /*
1575                          * Since we've reled the vnode and removed
1576                          * the file we now need to return the error.
1577                          * At this point we don't want to update the
1578                          * dircaches, call nfs4_waitfor_purge_complete
1579                          * or set vpp to vp so we need to skip these
1580                          * as well.
1581                          */
1582                         goto skip_update_dircaches;
1583                 }
1584         }
1585 
1586         /*
1587          * If we created or found the correct vnode, due to create_flag or
1588          * fh_differs being set, then update directory cache attribute, readdir
1589          * and dnlc caches.
1590          */
1591         if (create_flag || fh_differs) {
1592                 dirattr_info_t dinfo, *dinfop;
1593 
1594                 /*
1595                  * Make sure getattr succeeded before using results.
1596                  * note: op 7 is getattr(dir) for both flavors of
1597                  * open(create).
1598                  */
1599                 if (create_flag && res.status == NFS4_OK) {
1600                         dinfo.di_time_call = t;
1601                         dinfo.di_cred = cr;
1602                         dinfo.di_garp =
1603                             &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1604                         dinfop = &dinfo;
1605                 } else {
1606                         dinfop = NULL;
1607                 }
1608 
1609                 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1610                     dinfop);
1611         }
1612 
1613         /*
1614          * If the page cache for this file was flushed from actions
1615          * above, it was done asynchronously and if that is true,
1616          * there is a need to wait here for it to complete.  This must
1617          * be done outside of start_fop/end_fop.
1618          */
1619         (void) nfs4_waitfor_purge_complete(vp);
1620 
1621         /*
1622          * It is implicit that we are in the open case (create_flag == 0) since
1623          * fh_differs can only be set to a non-zero value in the open case.
1624          */
1625         if (fh_differs != 0 && vpi != NULL)
1626                 VN_RELE(vpi);
1627 
1628         /*
1629          * Be sure to set *vpp to the correct value before returning.
1630          */
1631         *vpp = vp;
1632 
1633 skip_update_dircaches:
1634 
1635         nfs4args_copen_free(open_args);
1636         if (setgid_flag) {
1637                 nfs4args_verify_free(&argop[8]);
1638                 nfs4args_setattr_free(&argop[9]);
1639         }
1640         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1641 
1642         if (ncr)
1643                 crfree(ncr);
1644         kmem_free(argop, argoplist_size);
1645         return (e.error);
1646 }
1647 
1648 /*
1649  * Reopen an open instance.  cf. nfs4open_otw().
1650  *
1651  * Errors are returned by the nfs4_error_t parameter.
1652  * - ep->error contains an errno value or zero.
1653  * - if it is zero, ep->stat is set to an NFS status code, if any.
1654  *   If the file could not be reopened, but the caller should continue, the
1655  *   file is marked dead and no error values are returned.  If the caller
1656  *   should stop recovering open files and start over, either the ep->error
1657  *   value or ep->stat will indicate an error (either something that requires
1658  *   recovery or EAGAIN).  Note that some recovery (e.g., expired volatile
1659  *   filehandles) may be handled silently by this routine.
1660  * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1661  *   will be started, so the caller should not do it.
1662  *
1663  * Gotos:
1664  * - kill_file : reopen failed in such a fashion to constitute marking the
1665  *    file dead and setting the open stream's 'os_failed_reopen' as 1.  This
1666  *   is for cases where recovery is not possible.
1667  * - failed_reopen : same as above, except that the file has already been
1668  *   marked dead, so no need to do it again.
1669  * - bailout : reopen failed but we are able to recover and retry the reopen -
1670  *   either within this function immediately or via the calling function.
1671  */
1672 
1673 void
1674 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1675     open_claim_type4 claim, bool_t frc_use_claim_previous,
1676     bool_t is_recov)
1677 {
1678         COMPOUND4args_clnt args;
1679         COMPOUND4res_clnt res;
1680         nfs_argop4 argop[4];
1681         nfs_resop4 *resop;
1682         OPEN4res *op_res = NULL;
1683         OPEN4cargs *open_args;
1684         GETFH4res *gf_res;
1685         rnode4_t *rp = VTOR4(vp);
1686         int doqueue = 1;
1687         cred_t *cr = NULL, *cred_otw = NULL;
1688         nfs4_open_owner_t *oop = NULL;
1689         seqid4 seqid;
1690         nfs4_ga_res_t *garp;
1691         char fn[MAXNAMELEN];
1692         nfs4_recov_state_t recov = {NULL, 0};
1693         nfs4_lost_rqst_t lost_rqst;
1694         mntinfo4_t *mi = VTOMI4(vp);
1695         bool_t abort;
1696         char *failed_msg = "";
1697         int fh_different;
1698         hrtime_t t;
1699         nfs4_bseqid_entry_t *bsep = NULL;
1700 
1701         ASSERT(nfs4_consistent_type(vp));
1702         ASSERT(nfs_zone() == mi->mi_zone);
1703 
1704         nfs4_error_zinit(ep);
1705 
1706         /* this is the cred used to find the open owner */
1707         cr = state_to_cred(osp);
1708         if (cr == NULL) {
1709                 failed_msg = "Couldn't reopen: no cred";
1710                 goto kill_file;
1711         }
1712         /* use this cred for OTW operations */
1713         cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1714 
1715 top:
1716         nfs4_error_zinit(ep);
1717 
1718         if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1719                 /* File system has been unmounted, quit */
1720                 ep->error = EIO;
1721                 failed_msg = "Couldn't reopen: file system has been unmounted";
1722                 goto kill_file;
1723         }
1724 
1725         oop = osp->os_open_owner;
1726 
1727         ASSERT(oop != NULL);
1728         if (oop == NULL) {      /* be defensive in non-DEBUG */
1729                 failed_msg = "can't reopen: no open owner";
1730                 goto kill_file;
1731         }
1732         open_owner_hold(oop);
1733 
1734         ep->error = nfs4_start_open_seqid_sync(oop, mi);
1735         if (ep->error) {
1736                 open_owner_rele(oop);
1737                 oop = NULL;
1738                 goto bailout;
1739         }
1740 
1741         /*
1742          * If the rnode has a delegation and the delegation has been
1743          * recovered and the server didn't request a recall and the caller
1744          * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1745          * recovery) and the rnode hasn't been marked dead, then install
1746          * the delegation stateid in the open stream.  Otherwise, proceed
1747          * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1748          */
1749         mutex_enter(&rp->r_statev4_lock);
1750         if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1751             !rp->r_deleg_return_pending &&
1752             (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1753             !rp->r_deleg_needs_recall &&
1754             claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1755             !(rp->r_flags & R4RECOVERR)) {
1756                 mutex_enter(&osp->os_sync_lock);
1757                 osp->os_delegation = 1;
1758                 osp->open_stateid = rp->r_deleg_stateid;
1759                 mutex_exit(&osp->os_sync_lock);
1760                 mutex_exit(&rp->r_statev4_lock);
1761                 goto bailout;
1762         }
1763         mutex_exit(&rp->r_statev4_lock);
1764 
1765         /*
1766          * If the file failed recovery, just quit.  This failure need not
1767          * affect other reopens, so don't return an error.
1768          */
1769         mutex_enter(&rp->r_statelock);
1770         if (rp->r_flags & R4RECOVERR) {
1771                 mutex_exit(&rp->r_statelock);
1772                 ep->error = 0;
1773                 goto failed_reopen;
1774         }
1775         mutex_exit(&rp->r_statelock);
1776 
1777         /*
1778          * argop is empty here
1779          *
1780          * PUTFH, OPEN, GETATTR
1781          */
1782         args.ctag = TAG_REOPEN;
1783         args.array_len = 4;
1784         args.array = argop;
1785 
1786         NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1787             "nfs4_reopen: file is type %d, id %s",
1788             vp->v_type, rnode4info(VTOR4(vp))));
1789 
1790         argop[0].argop = OP_CPUTFH;
1791 
1792         if (claim != CLAIM_PREVIOUS) {
1793                 /*
1794                  * if this is a file mount then
1795                  * use the mntinfo parentfh
1796                  */
1797                 argop[0].nfs_argop4_u.opcputfh.sfh =
1798                     (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1799                     VTOSV(vp)->sv_dfh;
1800         } else {
1801                 /* putfh fh to reopen */
1802                 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1803         }
1804 
1805         argop[1].argop = OP_COPEN;
1806         open_args = &argop[1].nfs_argop4_u.opcopen;
1807         open_args->claim = claim;
1808 
1809         if (claim == CLAIM_NULL) {
1810 
1811                 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1812                         nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1813                             "failed for vp 0x%p for CLAIM_NULL with %m",
1814                             (void *)vp);
1815                         failed_msg = "Couldn't reopen: vtoname failed for "
1816                             "CLAIM_NULL";
1817                         /* nothing allocated yet */
1818                         goto kill_file;
1819                 }
1820 
1821                 open_args->open_claim4_u.cfile = fn;
1822         } else if (claim == CLAIM_PREVIOUS) {
1823 
1824                 /*
1825                  * We have two cases to deal with here:
1826                  * 1) We're being called to reopen files in order to satisfy
1827                  *    a lock operation request which requires us to explicitly
1828                  *    reopen files which were opened under a delegation.  If
1829                  *    we're in recovery, we *must* use CLAIM_PREVIOUS.  In
1830                  *    that case, frc_use_claim_previous is TRUE and we must
1831                  *    use the rnode's current delegation type (r_deleg_type).
1832                  * 2) We're reopening files during some form of recovery.
1833                  *    In this case, frc_use_claim_previous is FALSE and we
1834                  *    use the delegation type appropriate for recovery
1835                  *    (r_deleg_needs_recovery).
1836                  */
1837                 mutex_enter(&rp->r_statev4_lock);
1838                 open_args->open_claim4_u.delegate_type =
1839                     frc_use_claim_previous ?
1840                     rp->r_deleg_type :
1841                     rp->r_deleg_needs_recovery;
1842                 mutex_exit(&rp->r_statev4_lock);
1843 
1844         } else if (claim == CLAIM_DELEGATE_CUR) {
1845 
1846                 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1847                         nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1848                             "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1849                             "with %m", (void *)vp);
1850                         failed_msg = "Couldn't reopen: vtoname failed for "
1851                             "CLAIM_DELEGATE_CUR";
1852                         /* nothing allocated yet */
1853                         goto kill_file;
1854                 }
1855 
1856                 mutex_enter(&rp->r_statev4_lock);
1857                 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1858                     rp->r_deleg_stateid;
1859                 mutex_exit(&rp->r_statev4_lock);
1860 
1861                 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1862         }
1863         open_args->opentype = OPEN4_NOCREATE;
1864         open_args->owner.clientid = mi2clientid(mi);
1865         open_args->owner.owner_len = sizeof (oop->oo_name);
1866         open_args->owner.owner_val =
1867             kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1868         bcopy(&oop->oo_name, open_args->owner.owner_val,
1869             open_args->owner.owner_len);
1870         open_args->share_access = 0;
1871         open_args->share_deny = 0;
1872 
1873         mutex_enter(&osp->os_sync_lock);
1874         NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1875             "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1876             "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1877             (void *)osp, (void *)rp, osp->os_share_acc_read,
1878             osp->os_share_acc_write, osp->os_open_ref_count,
1879             osp->os_mmap_read, osp->os_mmap_write, claim));
1880 
1881         if (osp->os_share_acc_read || osp->os_mmap_read)
1882                 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1883         if (osp->os_share_acc_write || osp->os_mmap_write)
1884                 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1885         if (osp->os_share_deny_read)
1886                 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1887         if (osp->os_share_deny_write)
1888                 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1889         mutex_exit(&osp->os_sync_lock);
1890 
1891         seqid = nfs4_get_open_seqid(oop) + 1;
1892         open_args->seqid = seqid;
1893 
1894         /* Construct the getfh part of the compound */
1895         argop[2].argop = OP_GETFH;
1896 
1897         /* Construct the getattr part of the compound */
1898         argop[3].argop = OP_GETATTR;
1899         argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1900         argop[3].nfs_argop4_u.opgetattr.mi = mi;
1901 
1902         t = gethrtime();
1903 
1904         rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1905 
1906         if (ep->error) {
1907                 if (!is_recov && !frc_use_claim_previous &&
1908                     (ep->error == EINTR || ep->error == ETIMEDOUT ||
1909                     NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1910                         nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1911                             cred_otw, vp, NULL, open_args);
1912                         abort = nfs4_start_recovery(ep,
1913                             VTOMI4(vp), vp, NULL, NULL,
1914                             lost_rqst.lr_op == OP_OPEN ?
1915                             &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1916                         nfs4args_copen_free(open_args);
1917                         goto bailout;
1918                 }
1919 
1920                 nfs4args_copen_free(open_args);
1921 
1922                 if (ep->error == EACCES && cred_otw != cr) {
1923                         crfree(cred_otw);
1924                         cred_otw = cr;
1925                         crhold(cred_otw);
1926                         nfs4_end_open_seqid_sync(oop);
1927                         open_owner_rele(oop);
1928                         oop = NULL;
1929                         goto top;
1930                 }
1931                 if (ep->error == ETIMEDOUT)
1932                         goto bailout;
1933                 failed_msg = "Couldn't reopen: rpc error";
1934                 goto kill_file;
1935         }
1936 
1937         if (nfs4_need_to_bump_seqid(&res))
1938                 nfs4_set_open_seqid(seqid, oop, args.ctag);
1939 
1940         switch (res.status) {
1941         case NFS4_OK:
1942                 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1943                         mutex_enter(&rp->r_statelock);
1944                         rp->r_delay_interval = 0;
1945                         mutex_exit(&rp->r_statelock);
1946                 }
1947                 break;
1948         case NFS4ERR_BAD_SEQID:
1949                 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1950                     args.ctag, open_args->seqid);
1951 
1952                 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1953                     NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1954                     NULL, OP_OPEN, bsep, NULL, NULL);
1955 
1956                 nfs4args_copen_free(open_args);
1957                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1958                 nfs4_end_open_seqid_sync(oop);
1959                 open_owner_rele(oop);
1960                 oop = NULL;
1961                 kmem_free(bsep, sizeof (*bsep));
1962 
1963                 goto kill_file;
1964         case NFS4ERR_NO_GRACE:
1965                 nfs4args_copen_free(open_args);
1966                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1967                 nfs4_end_open_seqid_sync(oop);
1968                 open_owner_rele(oop);
1969                 oop = NULL;
1970                 if (claim == CLAIM_PREVIOUS) {
1971                         /*
1972                          * Retry as a plain open. We don't need to worry about
1973                          * checking the changeinfo: it is acceptable for a
1974                          * client to re-open a file and continue processing
1975                          * (in the absence of locks).
1976                          */
1977                         NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1978                             "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1979                             "will retry as CLAIM_NULL"));
1980                         claim = CLAIM_NULL;
1981                         nfs4_mi_kstat_inc_no_grace(mi);
1982                         goto top;
1983                 }
1984                 failed_msg =
1985                     "Couldn't reopen: tried reclaim outside grace period. ";
1986                 goto kill_file;
1987         case NFS4ERR_GRACE:
1988                 nfs4_set_grace_wait(mi);
1989                 nfs4args_copen_free(open_args);
1990                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1991                 nfs4_end_open_seqid_sync(oop);
1992                 open_owner_rele(oop);
1993                 oop = NULL;
1994                 ep->error = nfs4_wait_for_grace(mi, &recov);
1995                 if (ep->error != 0)
1996                         goto bailout;
1997                 goto top;
1998         case NFS4ERR_DELAY:
1999                 nfs4_set_delay_wait(vp);
2000                 nfs4args_copen_free(open_args);
2001                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2002                 nfs4_end_open_seqid_sync(oop);
2003                 open_owner_rele(oop);
2004                 oop = NULL;
2005                 ep->error = nfs4_wait_for_delay(vp, &recov);
2006                 nfs4_mi_kstat_inc_delay(mi);
2007                 if (ep->error != 0)
2008                         goto bailout;
2009                 goto top;
2010         case NFS4ERR_FHEXPIRED:
2011                 /* recover filehandle and retry */
2012                 abort = nfs4_start_recovery(ep,
2013                     mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2014                 nfs4args_copen_free(open_args);
2015                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2016                 nfs4_end_open_seqid_sync(oop);
2017                 open_owner_rele(oop);
2018                 oop = NULL;
2019                 if (abort == FALSE)
2020                         goto top;
2021                 failed_msg = "Couldn't reopen: recovery aborted";
2022                 goto kill_file;
2023         case NFS4ERR_RESOURCE:
2024         case NFS4ERR_STALE_CLIENTID:
2025         case NFS4ERR_WRONGSEC:
2026         case NFS4ERR_EXPIRED:
2027                 /*
2028                  * Do not mark the file dead and let the calling
2029                  * function initiate recovery.
2030                  */
2031                 nfs4args_copen_free(open_args);
2032                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2033                 nfs4_end_open_seqid_sync(oop);
2034                 open_owner_rele(oop);
2035                 oop = NULL;
2036                 goto bailout;
2037         case NFS4ERR_ACCESS:
2038                 if (cred_otw != cr) {
2039                         crfree(cred_otw);
2040                         cred_otw = cr;
2041                         crhold(cred_otw);
2042                         nfs4args_copen_free(open_args);
2043                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2044                         nfs4_end_open_seqid_sync(oop);
2045                         open_owner_rele(oop);
2046                         oop = NULL;
2047                         goto top;
2048                 }
2049                 /* fall through */
2050         default:
2051                 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2052                     "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2053                     (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2054                     rnode4info(VTOR4(vp))));
2055                 failed_msg = "Couldn't reopen: NFSv4 error";
2056                 nfs4args_copen_free(open_args);
2057                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2058                 goto kill_file;
2059         }
2060 
2061         resop = &res.array[1];  /* open res */
2062         op_res = &resop->nfs_resop4_u.opopen;
2063 
2064         garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2065 
2066         /*
2067          * Check if the path we reopened really is the same
2068          * file. We could end up in a situation where the file
2069          * was removed and a new file created with the same name.
2070          */
2071         resop = &res.array[2];
2072         gf_res = &resop->nfs_resop4_u.opgetfh;
2073         (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2074         fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2075         if (fh_different) {
2076                 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2077                     mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2078                         /* Oops, we don't have the same file */
2079                         if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2080                                 failed_msg = "Couldn't reopen: Persistent "
2081                                     "file handle changed";
2082                         else
2083                                 failed_msg = "Couldn't reopen: Volatile "
2084                                     "(no expire on open) file handle changed";
2085 
2086                         nfs4args_copen_free(open_args);
2087                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2088                         nfs_rw_exit(&mi->mi_fh_lock);
2089                         goto kill_file;
2090 
2091                 } else {
2092                         /*
2093                          * We have volatile file handles that don't compare.
2094                          * If the fids are the same then we assume that the
2095                          * file handle expired but the rnode still refers to
2096                          * the same file object.
2097                          *
2098                          * First check that we have fids or not.
2099                          * If we don't we have a dumb server so we will
2100                          * just assume every thing is ok for now.
2101                          */
2102                         if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2103                             rp->r_attr.va_mask & AT_NODEID &&
2104                             rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2105                                 /*
2106                                  * We have fids, but they don't
2107                                  * compare. So kill the file.
2108                                  */
2109                                 failed_msg =
2110                                     "Couldn't reopen: file handle changed"
2111                                     " due to mismatched fids";
2112                                 nfs4args_copen_free(open_args);
2113                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
2114                                     (caddr_t)&res);
2115                                 nfs_rw_exit(&mi->mi_fh_lock);
2116                                 goto kill_file;
2117                         } else {
2118                                 /*
2119                                  * We have volatile file handles that refers
2120                                  * to the same file (at least they have the
2121                                  * same fid) or we don't have fids so we
2122                                  * can't tell. :(. We'll be a kind and accepting
2123                                  * client so we'll update the rnode's file
2124                                  * handle with the otw handle.
2125                                  *
2126                                  * We need to drop mi->mi_fh_lock since
2127                                  * sh4_update acquires it. Since there is
2128                                  * only one recovery thread there is no
2129                                  * race.
2130                                  */
2131                                 nfs_rw_exit(&mi->mi_fh_lock);
2132                                 sfh4_update(rp->r_fh, &gf_res->object);
2133                         }
2134                 }
2135         } else {
2136                 nfs_rw_exit(&mi->mi_fh_lock);
2137         }
2138 
2139         ASSERT(nfs4_consistent_type(vp));
2140 
2141         /*
2142          * If the server wanted an OPEN_CONFIRM but that fails, just start
2143          * over.  Presumably if there is a persistent error it will show up
2144          * when we resend the OPEN.
2145          */
2146         if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2147                 bool_t retry_open = FALSE;
2148 
2149                 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2150                     cred_otw, is_recov, &retry_open,
2151                     oop, FALSE, ep, NULL);
2152                 if (ep->error || ep->stat) {
2153                         nfs4args_copen_free(open_args);
2154                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2155                         nfs4_end_open_seqid_sync(oop);
2156                         open_owner_rele(oop);
2157                         oop = NULL;
2158                         goto top;
2159                 }
2160         }
2161 
2162         mutex_enter(&osp->os_sync_lock);
2163         osp->open_stateid = op_res->stateid;
2164         osp->os_delegation = 0;
2165         /*
2166          * Need to reset this bitfield for the possible case where we were
2167          * going to OTW CLOSE the file, got a non-recoverable error, and before
2168          * we could retry the CLOSE, OPENed the file again.
2169          */
2170         ASSERT(osp->os_open_owner->oo_seqid_inuse);
2171         osp->os_final_close = 0;
2172         osp->os_force_close = 0;
2173         if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2174                 osp->os_dc_openacc = open_args->share_access;
2175         mutex_exit(&osp->os_sync_lock);
2176 
2177         nfs4_end_open_seqid_sync(oop);
2178 
2179         /* accept delegation, if any */
2180         nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2181 
2182         nfs4args_copen_free(open_args);
2183 
2184         nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2185 
2186         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2187 
2188         ASSERT(nfs4_consistent_type(vp));
2189 
2190         open_owner_rele(oop);
2191         crfree(cr);
2192         crfree(cred_otw);
2193         return;
2194 
2195 kill_file:
2196         nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2197 failed_reopen:
2198         NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2199             "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2200             (void *)osp, (void *)cr, rnode4info(rp)));
2201         mutex_enter(&osp->os_sync_lock);
2202         osp->os_failed_reopen = 1;
2203         mutex_exit(&osp->os_sync_lock);
2204 bailout:
2205         if (oop != NULL) {
2206                 nfs4_end_open_seqid_sync(oop);
2207                 open_owner_rele(oop);
2208         }
2209         if (cr != NULL)
2210                 crfree(cr);
2211         if (cred_otw != NULL)
2212                 crfree(cred_otw);
2213 }
2214 
2215 /* for . and .. OPENs */
2216 /* ARGSUSED */
2217 static int
2218 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2219 {
2220         rnode4_t *rp;
2221         nfs4_ga_res_t gar;
2222 
2223         ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2224 
2225         /*
2226          * If close-to-open consistency checking is turned off or
2227          * if there is no cached data, we can avoid
2228          * the over the wire getattr.  Otherwise, force a
2229          * call to the server to get fresh attributes and to
2230          * check caches. This is required for close-to-open
2231          * consistency.
2232          */
2233         rp = VTOR4(*vpp);
2234         if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2235             (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2236                 return (0);
2237 
2238         gar.n4g_va.va_mask = AT_ALL;
2239         return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2240 }
2241 
2242 /*
2243  * CLOSE a file
2244  */
2245 /* ARGSUSED */
2246 static int
2247 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2248         caller_context_t *ct)
2249 {
2250         rnode4_t        *rp;
2251         int              error = 0;
2252         int              r_error = 0;
2253         int              n4error = 0;
2254         nfs4_error_t     e = { 0, NFS4_OK, RPC_SUCCESS };
2255 
2256         /*
2257          * Remove client state for this (lockowner, file) pair.
2258          * Issue otw v4 call to have the server do the same.
2259          */
2260 
2261         rp = VTOR4(vp);
2262 
2263         /*
2264          * zone_enter(2) prevents processes from changing zones with NFS files
2265          * open; if we happen to get here from the wrong zone we can't do
2266          * anything over the wire.
2267          */
2268         if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2269                 /*
2270                  * We could attempt to clean up locks, except we're sure
2271                  * that the current process didn't acquire any locks on
2272                  * the file: any attempt to lock a file belong to another zone
2273                  * will fail, and one can't lock an NFS file and then change
2274                  * zones, as that fails too.
2275                  *
2276                  * Returning an error here is the sane thing to do.  A
2277                  * subsequent call to VN_RELE() which translates to a
2278                  * nfs4_inactive() will clean up state: if the zone of the
2279                  * vnode's origin is still alive and kicking, the inactive
2280                  * thread will handle the request (from the correct zone), and
2281                  * everything (minus the OTW close call) should be OK.  If the
2282                  * zone is going away nfs4_async_inactive() will throw away
2283                  * delegations, open streams and cached pages inline.
2284                  */
2285                 return (EIO);
2286         }
2287 
2288         /*
2289          * If we are using local locking for this filesystem, then
2290          * release all of the SYSV style record locks.  Otherwise,
2291          * we are doing network locking and we need to release all
2292          * of the network locks.  All of the locks held by this
2293          * process on this file are released no matter what the
2294          * incoming reference count is.
2295          */
2296         if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2297                 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2298                 cleanshares(vp, ttoproc(curthread)->p_pid);
2299         } else
2300                 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2301 
2302         if (e.error) {
2303                 struct lm_sysid *lmsid;
2304                 lmsid = nfs4_find_sysid(VTOMI4(vp));
2305                 if (lmsid == NULL) {
2306                         DTRACE_PROBE2(unknown__sysid, int, e.error,
2307                             vnode_t *, vp);
2308                 } else {
2309                         cleanlocks(vp, ttoproc(curthread)->p_pid,
2310                             (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2311                 }
2312                 return (e.error);
2313         }
2314 
2315         if (count > 1)
2316                 return (0);
2317 
2318         /*
2319          * If the file has been `unlinked', then purge the
2320          * DNLC so that this vnode will get reycled quicker
2321          * and the .nfs* file on the server will get removed.
2322          */
2323         if (rp->r_unldvp != NULL)
2324                 dnlc_purge_vp(vp);
2325 
2326         /*
2327          * If the file was open for write and there are pages,
2328          * do a synchronous flush and commit of all of the
2329          * dirty and uncommitted pages.
2330          */
2331         ASSERT(!e.error);
2332         if ((flag & FWRITE) && nfs4_has_pages(vp))
2333                 error = nfs4_putpage_commit(vp, 0, 0, cr);
2334 
2335         mutex_enter(&rp->r_statelock);
2336         r_error = rp->r_error;
2337         rp->r_error = 0;
2338         mutex_exit(&rp->r_statelock);
2339 
2340         /*
2341          * If this file type is one for which no explicit 'open' was
2342          * done, then bail now (ie. no need for protocol 'close'). If
2343          * there was an error w/the vm subsystem, return _that_ error,
2344          * otherwise, return any errors that may've been reported via
2345          * the rnode.
2346          */
2347         if (vp->v_type != VREG)
2348                 return (error ? error : r_error);
2349 
2350         /*
2351          * The sync putpage commit may have failed above, but since
2352          * we're working w/a regular file, we need to do the protocol
2353          * 'close' (nfs4close_one will figure out if an otw close is
2354          * needed or not). Report any errors _after_ doing the protocol
2355          * 'close'.
2356          */
2357         nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2358         n4error = e.error ? e.error : geterrno4(e.stat);
2359 
2360         /*
2361          * Error reporting prio (Hi -> Lo)
2362          *
2363          *   i) nfs4_putpage_commit (error)
2364          *  ii) rnode's (r_error)
2365          * iii) nfs4close_one (n4error)
2366          */
2367         return (error ? error : (r_error ? r_error : n4error));
2368 }
2369 
2370 /*
2371  * Initialize *lost_rqstp.
2372  */
2373 
2374 static void
2375 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2376     nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2377     vnode_t *vp)
2378 {
2379         if (error != ETIMEDOUT && error != EINTR &&
2380             !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2381                 lost_rqstp->lr_op = 0;
2382                 return;
2383         }
2384 
2385         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2386             "nfs4close_save_lost_rqst: error %d", error));
2387 
2388         lost_rqstp->lr_op = OP_CLOSE;
2389         /*
2390          * The vp is held and rele'd via the recovery code.
2391          * See nfs4_save_lost_rqst.
2392          */
2393         lost_rqstp->lr_vp = vp;
2394         lost_rqstp->lr_dvp = NULL;
2395         lost_rqstp->lr_oop = oop;
2396         lost_rqstp->lr_osp = osp;
2397         ASSERT(osp != NULL);
2398         ASSERT(mutex_owned(&osp->os_sync_lock));
2399         osp->os_pending_close = 1;
2400         lost_rqstp->lr_lop = NULL;
2401         lost_rqstp->lr_cr = cr;
2402         lost_rqstp->lr_flk = NULL;
2403         lost_rqstp->lr_putfirst = FALSE;
2404 }
2405 
2406 /*
2407  * Assumes you already have the open seqid sync grabbed as well as the
2408  * 'os_sync_lock'.  Note: this will release the open seqid sync and
2409  * 'os_sync_lock' if client recovery starts.  Calling functions have to
2410  * be prepared to handle this.
2411  *
2412  * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2413  * was needed and was started, and that the calling function should retry
2414  * this function; otherwise it is returned as 0.
2415  *
2416  * Errors are returned via the nfs4_error_t parameter.
2417  */
2418 static void
2419 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2420     nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2421     nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2422 {
2423         COMPOUND4args_clnt args;
2424         COMPOUND4res_clnt res;
2425         CLOSE4args *close_args;
2426         nfs_resop4 *resop;
2427         nfs_argop4 argop[3];
2428         int doqueue = 1;
2429         mntinfo4_t *mi;
2430         seqid4 seqid;
2431         vnode_t *vp;
2432         bool_t needrecov = FALSE;
2433         nfs4_lost_rqst_t lost_rqst;
2434         hrtime_t t;
2435 
2436         ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2437 
2438         ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2439 
2440         NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2441 
2442         /* Only set this to 1 if recovery is started */
2443         *recov = 0;
2444 
2445         /* do the OTW call to close the file */
2446 
2447         if (close_type == CLOSE_RESEND)
2448                 args.ctag = TAG_CLOSE_LOST;
2449         else if (close_type == CLOSE_AFTER_RESEND)
2450                 args.ctag = TAG_CLOSE_UNDO;
2451         else
2452                 args.ctag = TAG_CLOSE;
2453 
2454         args.array_len = 3;
2455         args.array = argop;
2456 
2457         vp = RTOV4(rp);
2458 
2459         mi = VTOMI4(vp);
2460 
2461         /* putfh target fh */
2462         argop[0].argop = OP_CPUTFH;
2463         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2464 
2465         argop[1].argop = OP_GETATTR;
2466         argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2467         argop[1].nfs_argop4_u.opgetattr.mi = mi;
2468 
2469         argop[2].argop = OP_CLOSE;
2470         close_args = &argop[2].nfs_argop4_u.opclose;
2471 
2472         seqid = nfs4_get_open_seqid(oop) + 1;
2473 
2474         close_args->seqid = seqid;
2475         close_args->open_stateid = osp->open_stateid;
2476 
2477         NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2478             "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2479             rnode4info(rp)));
2480 
2481         t = gethrtime();
2482 
2483         rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2484 
2485         if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2486                 nfs4_set_open_seqid(seqid, oop, args.ctag);
2487         }
2488 
2489         needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2490         if (ep->error && !needrecov) {
2491                 /*
2492                  * if there was an error and no recovery is to be done
2493                  * then then set up the file to flush its cache if
2494                  * needed for the next caller.
2495                  */
2496                 mutex_enter(&rp->r_statelock);
2497                 PURGE_ATTRCACHE4_LOCKED(rp);
2498                 rp->r_flags &= ~R4WRITEMODIFIED;
2499                 mutex_exit(&rp->r_statelock);
2500                 return;
2501         }
2502 
2503         if (needrecov) {
2504                 bool_t abort;
2505                 nfs4_bseqid_entry_t *bsep = NULL;
2506 
2507                 if (close_type != CLOSE_RESEND)
2508                         nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2509                             osp, cred_otw, vp);
2510 
2511                 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2512                         bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2513                             0, args.ctag, close_args->seqid);
2514 
2515                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2516                     "nfs4close_otw: initiating recovery. error %d "
2517                     "res.status %d", ep->error, res.status));
2518 
2519                 /*
2520                  * Drop the 'os_sync_lock' here so we don't hit
2521                  * a potential recursive mutex_enter via an
2522                  * 'open_stream_hold()'.
2523                  */
2524                 mutex_exit(&osp->os_sync_lock);
2525                 *have_sync_lockp = 0;
2526                 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2527                     (close_type != CLOSE_RESEND &&
2528                     lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2529                     OP_CLOSE, bsep, NULL, NULL);
2530 
2531                 /* drop open seq sync, and let the calling function regrab it */
2532                 nfs4_end_open_seqid_sync(oop);
2533                 *did_start_seqid_syncp = 0;
2534 
2535                 if (bsep)
2536                         kmem_free(bsep, sizeof (*bsep));
2537                 /*
2538                  * For signals, the caller wants to quit, so don't say to
2539                  * retry.  For forced unmount, if it's a user thread, it
2540                  * wants to quit.  If it's a recovery thread, the retry
2541                  * will happen higher-up on the call stack.  Either way,
2542                  * don't say to retry.
2543                  */
2544                 if (abort == FALSE && ep->error != EINTR &&
2545                     !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2546                     close_type != CLOSE_RESEND &&
2547                     close_type != CLOSE_AFTER_RESEND)
2548                         *recov = 1;
2549                 else
2550                         *recov = 0;
2551 
2552                 if (!ep->error)
2553                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2554                 return;
2555         }
2556 
2557         if (res.status) {
2558                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2559                 return;
2560         }
2561 
2562         mutex_enter(&rp->r_statev4_lock);
2563         rp->created_v4 = 0;
2564         mutex_exit(&rp->r_statev4_lock);
2565 
2566         resop = &res.array[2];
2567         osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2568         osp->os_valid = 0;
2569 
2570         /*
2571          * This removes the reference obtained at OPEN; ie, when the
2572          * open stream structure was created.
2573          *
2574          * We don't have to worry about calling 'open_stream_rele'
2575          * since we our currently holding a reference to the open
2576          * stream which means the count cannot go to 0 with this
2577          * decrement.
2578          */
2579         ASSERT(osp->os_ref_count >= 2);
2580         osp->os_ref_count--;
2581 
2582         if (!ep->error)
2583                 nfs4_attr_cache(vp,
2584                     &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2585                     t, cred_otw, TRUE, NULL);
2586 
2587         NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2588             " returning %d", ep->error));
2589 
2590         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2591 }
2592 
2593 /* ARGSUSED */
2594 static int
2595 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2596     caller_context_t *ct)
2597 {
2598         rnode4_t *rp;
2599         u_offset_t off;
2600         offset_t diff;
2601         uint_t on;
2602         uint_t n;
2603         caddr_t base;
2604         uint_t flags;
2605         int error;
2606         mntinfo4_t *mi;
2607 
2608         rp = VTOR4(vp);
2609 
2610         ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2611 
2612         if (IS_SHADOW(vp, rp))
2613                 vp = RTOV4(rp);
2614 
2615         if (vp->v_type != VREG)
2616                 return (EISDIR);
2617 
2618         mi = VTOMI4(vp);
2619 
2620         if (nfs_zone() != mi->mi_zone)
2621                 return (EIO);
2622 
2623         if (uiop->uio_resid == 0)
2624                 return (0);
2625 
2626         if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2627                 return (EINVAL);
2628 
2629         mutex_enter(&rp->r_statelock);
2630         if (rp->r_flags & R4RECOVERRP)
2631                 error = (rp->r_error ? rp->r_error : EIO);
2632         else
2633                 error = 0;
2634         mutex_exit(&rp->r_statelock);
2635         if (error)
2636                 return (error);
2637 
2638         /*
2639          * Bypass VM if caching has been disabled (e.g., locking) or if
2640          * using client-side direct I/O and the file is not mmap'd and
2641          * there are no cached pages.
2642          */
2643         if ((vp->v_flag & VNOCACHE) ||
2644             (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2645             rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2646                 size_t resid = 0;
2647 
2648                 return (nfs4read(vp, NULL, uiop->uio_loffset,
2649                     uiop->uio_resid, &resid, cr, FALSE, uiop));
2650         }
2651 
2652         error = 0;
2653 
2654         do {
2655                 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2656                 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2657                 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2658 
2659                 if (error = nfs4_validate_caches(vp, cr))
2660                         break;
2661 
2662                 mutex_enter(&rp->r_statelock);
2663                 while (rp->r_flags & R4INCACHEPURGE) {
2664                         if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2665                                 mutex_exit(&rp->r_statelock);
2666                                 return (EINTR);
2667                         }
2668                 }
2669                 diff = rp->r_size - uiop->uio_loffset;
2670                 mutex_exit(&rp->r_statelock);
2671                 if (diff <= 0)
2672                         break;
2673                 if (diff < n)
2674                         n = (uint_t)diff;
2675 
2676                 if (vpm_enable) {
2677                         /*
2678                          * Copy data.
2679                          */
2680                         error = vpm_data_copy(vp, off + on, n, uiop,
2681                             1, NULL, 0, S_READ);
2682                 } else {
2683                         base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2684                             S_READ);
2685 
2686                         error = uiomove(base + on, n, UIO_READ, uiop);
2687                 }
2688 
2689                 if (!error) {
2690                         /*
2691                          * If read a whole block or read to eof,
2692                          * won't need this buffer again soon.
2693                          */
2694                         mutex_enter(&rp->r_statelock);
2695                         if (n + on == MAXBSIZE ||
2696                             uiop->uio_loffset == rp->r_size)
2697                                 flags = SM_DONTNEED;
2698                         else
2699                                 flags = 0;
2700                         mutex_exit(&rp->r_statelock);
2701                         if (vpm_enable) {
2702                                 error = vpm_sync_pages(vp, off, n, flags);
2703                         } else {
2704                                 error = segmap_release(segkmap, base, flags);
2705                         }
2706                 } else {
2707                         if (vpm_enable) {
2708                                 (void) vpm_sync_pages(vp, off, n, 0);
2709                         } else {
2710                                 (void) segmap_release(segkmap, base, 0);
2711                         }
2712                 }
2713         } while (!error && uiop->uio_resid > 0);
2714 
2715         return (error);
2716 }
2717 
2718 /* ARGSUSED */
2719 static int
2720 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2721     caller_context_t *ct)
2722 {
2723         rlim64_t limit = uiop->uio_llimit;
2724         rnode4_t *rp;
2725         u_offset_t off;
2726         caddr_t base;
2727         uint_t flags;
2728         int remainder;
2729         size_t n;
2730         int on;
2731         int error;
2732         int resid;
2733         u_offset_t offset;
2734         mntinfo4_t *mi;
2735         uint_t bsize;
2736 
2737         rp = VTOR4(vp);
2738 
2739         if (IS_SHADOW(vp, rp))
2740                 vp = RTOV4(rp);
2741 
2742         if (vp->v_type != VREG)
2743                 return (EISDIR);
2744 
2745         mi = VTOMI4(vp);
2746 
2747         if (nfs_zone() != mi->mi_zone)
2748                 return (EIO);
2749 
2750         if (uiop->uio_resid == 0)
2751                 return (0);
2752 
2753         mutex_enter(&rp->r_statelock);
2754         if (rp->r_flags & R4RECOVERRP)
2755                 error = (rp->r_error ? rp->r_error : EIO);
2756         else
2757                 error = 0;
2758         mutex_exit(&rp->r_statelock);
2759         if (error)
2760                 return (error);
2761 
2762         if (ioflag & FAPPEND) {
2763                 struct vattr va;
2764 
2765                 /*
2766                  * Must serialize if appending.
2767                  */
2768                 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2769                         nfs_rw_exit(&rp->r_rwlock);
2770                         if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2771                             INTR4(vp)))
2772                                 return (EINTR);
2773                 }
2774 
2775                 va.va_mask = AT_SIZE;
2776                 error = nfs4getattr(vp, &va, cr);
2777                 if (error)
2778                         return (error);
2779                 uiop->uio_loffset = va.va_size;
2780         }
2781 
2782         offset = uiop->uio_loffset + uiop->uio_resid;
2783 
2784         if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2785                 return (EINVAL);
2786 
2787         if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2788                 limit = MAXOFFSET_T;
2789 
2790         /*
2791          * Check to make sure that the process will not exceed
2792          * its limit on file size.  It is okay to write up to
2793          * the limit, but not beyond.  Thus, the write which
2794          * reaches the limit will be short and the next write
2795          * will return an error.
2796          */
2797         remainder = 0;
2798         if (offset > uiop->uio_llimit) {
2799                 remainder = offset - uiop->uio_llimit;
2800                 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2801                 if (uiop->uio_resid <= 0) {
2802                         proc_t *p = ttoproc(curthread);
2803 
2804                         uiop->uio_resid += remainder;
2805                         mutex_enter(&p->p_lock);
2806                         (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2807                             p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2808                         mutex_exit(&p->p_lock);
2809                         return (EFBIG);
2810                 }
2811         }
2812 
2813         /* update the change attribute, if we have a write delegation */
2814 
2815         mutex_enter(&rp->r_statev4_lock);
2816         if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2817                 rp->r_deleg_change++;
2818 
2819         mutex_exit(&rp->r_statev4_lock);
2820 
2821         if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2822                 return (EINTR);
2823 
2824         /*
2825          * Bypass VM if caching has been disabled (e.g., locking) or if
2826          * using client-side direct I/O and the file is not mmap'd and
2827          * there are no cached pages.
2828          */
2829         if ((vp->v_flag & VNOCACHE) ||
2830             (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2831             rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2832                 size_t bufsize;
2833                 int count;
2834                 u_offset_t org_offset;
2835                 stable_how4 stab_comm;
2836 nfs4_fwrite:
2837                 if (rp->r_flags & R4STALE) {
2838                         resid = uiop->uio_resid;
2839                         offset = uiop->uio_loffset;
2840                         error = rp->r_error;
2841                         /*
2842                          * A close may have cleared r_error, if so,
2843                          * propagate ESTALE error return properly
2844                          */
2845                         if (error == 0)
2846                                 error = ESTALE;
2847                         goto bottom;
2848                 }
2849 
2850                 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2851                 base = kmem_alloc(bufsize, KM_SLEEP);
2852                 do {
2853                         if (ioflag & FDSYNC)
2854                                 stab_comm = DATA_SYNC4;
2855                         else
2856                                 stab_comm = FILE_SYNC4;
2857                         resid = uiop->uio_resid;
2858                         offset = uiop->uio_loffset;
2859                         count = MIN(uiop->uio_resid, bufsize);
2860                         org_offset = uiop->uio_loffset;
2861                         error = uiomove(base, count, UIO_WRITE, uiop);
2862                         if (!error) {
2863                                 error = nfs4write(vp, base, org_offset,
2864                                     count, cr, &stab_comm);
2865                                 if (!error) {
2866                                         mutex_enter(&rp->r_statelock);
2867                                         if (rp->r_size < uiop->uio_loffset)
2868                                                 rp->r_size = uiop->uio_loffset;
2869                                         mutex_exit(&rp->r_statelock);
2870                                 }
2871                         }
2872                 } while (!error && uiop->uio_resid > 0);
2873                 kmem_free(base, bufsize);
2874                 goto bottom;
2875         }
2876 
2877         bsize = vp->v_vfsp->vfs_bsize;
2878 
2879         do {
2880                 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2881                 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2882                 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2883 
2884                 resid = uiop->uio_resid;
2885                 offset = uiop->uio_loffset;
2886 
2887                 if (rp->r_flags & R4STALE) {
2888                         error = rp->r_error;
2889                         /*
2890                          * A close may have cleared r_error, if so,
2891                          * propagate ESTALE error return properly
2892                          */
2893                         if (error == 0)
2894                                 error = ESTALE;
2895                         break;
2896                 }
2897 
2898                 /*
2899                  * Don't create dirty pages faster than they
2900                  * can be cleaned so that the system doesn't
2901                  * get imbalanced.  If the async queue is
2902                  * maxed out, then wait for it to drain before
2903                  * creating more dirty pages.  Also, wait for
2904                  * any threads doing pagewalks in the vop_getattr
2905                  * entry points so that they don't block for
2906                  * long periods.
2907                  */
2908                 mutex_enter(&rp->r_statelock);
2909                 while ((mi->mi_max_threads != 0 &&
2910                     rp->r_awcount > 2 * mi->mi_max_threads) ||
2911                     rp->r_gcount > 0) {
2912                         if (INTR4(vp)) {
2913                                 klwp_t *lwp = ttolwp(curthread);
2914 
2915                                 if (lwp != NULL)
2916                                         lwp->lwp_nostop++;
2917                                 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2918                                         mutex_exit(&rp->r_statelock);
2919                                         if (lwp != NULL)
2920                                                 lwp->lwp_nostop--;
2921                                         error = EINTR;
2922                                         goto bottom;
2923                                 }
2924                                 if (lwp != NULL)
2925                                         lwp->lwp_nostop--;
2926                         } else
2927                                 cv_wait(&rp->r_cv, &rp->r_statelock);
2928                 }
2929                 mutex_exit(&rp->r_statelock);
2930 
2931                 /*
2932                  * Touch the page and fault it in if it is not in core
2933                  * before segmap_getmapflt or vpm_data_copy can lock it.
2934                  * This is to avoid the deadlock if the buffer is mapped
2935                  * to the same file through mmap which we want to write.
2936                  */
2937                 uio_prefaultpages((long)n, uiop);
2938 
2939                 if (vpm_enable) {
2940                         /*
2941                          * It will use kpm mappings, so no need to
2942                          * pass an address.
2943                          */
2944                         error = writerp4(rp, NULL, n, uiop, 0);
2945                 } else  {
2946                         if (segmap_kpm) {
2947                                 int pon = uiop->uio_loffset & PAGEOFFSET;
2948                                 size_t pn = MIN(PAGESIZE - pon,
2949                                     uiop->uio_resid);
2950                                 int pagecreate;
2951 
2952                                 mutex_enter(&rp->r_statelock);
2953                                 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2954                                     uiop->uio_loffset + pn >= rp->r_size);
2955                                 mutex_exit(&rp->r_statelock);
2956 
2957                                 base = segmap_getmapflt(segkmap, vp, off + on,
2958                                     pn, !pagecreate, S_WRITE);
2959 
2960                                 error = writerp4(rp, base + pon, n, uiop,
2961                                     pagecreate);
2962 
2963                         } else {
2964                                 base = segmap_getmapflt(segkmap, vp, off + on,
2965                                     n, 0, S_READ);
2966                                 error = writerp4(rp, base + on, n, uiop, 0);
2967                         }
2968                 }
2969 
2970                 if (!error) {
2971                         if (mi->mi_flags & MI4_NOAC)
2972                                 flags = SM_WRITE;
2973                         else if ((uiop->uio_loffset % bsize) == 0 ||
2974                             IS_SWAPVP(vp)) {
2975                                 /*
2976                                  * Have written a whole block.
2977                                  * Start an asynchronous write
2978                                  * and mark the buffer to
2979                                  * indicate that it won't be
2980                                  * needed again soon.
2981                                  */
2982                                 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2983                         } else
2984                                 flags = 0;
2985                         if ((ioflag & (FSYNC|FDSYNC)) ||
2986                             (rp->r_flags & R4OUTOFSPACE)) {
2987                                 flags &= ~SM_ASYNC;
2988                                 flags |= SM_WRITE;
2989                         }
2990                         if (vpm_enable) {
2991                                 error = vpm_sync_pages(vp, off, n, flags);
2992                         } else {
2993                                 error = segmap_release(segkmap, base, flags);
2994                         }
2995                 } else {
2996                         if (vpm_enable) {
2997                                 (void) vpm_sync_pages(vp, off, n, 0);
2998                         } else {
2999                                 (void) segmap_release(segkmap, base, 0);
3000                         }
3001                         /*
3002                          * In the event that we got an access error while
3003                          * faulting in a page for a write-only file just
3004                          * force a write.
3005                          */
3006                         if (error == EACCES)
3007                                 goto nfs4_fwrite;
3008                 }
3009         } while (!error && uiop->uio_resid > 0);
3010 
3011 bottom:
3012         if (error) {
3013                 uiop->uio_resid = resid + remainder;
3014                 uiop->uio_loffset = offset;
3015         } else {
3016                 uiop->uio_resid += remainder;
3017 
3018                 mutex_enter(&rp->r_statev4_lock);
3019                 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3020                         gethrestime(&rp->r_attr.va_mtime);
3021                         rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3022                 }
3023                 mutex_exit(&rp->r_statev4_lock);
3024         }
3025 
3026         nfs_rw_exit(&rp->r_lkserlock);
3027 
3028         return (error);
3029 }
3030 
3031 /*
3032  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3033  */
3034 static int
3035 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3036     int flags, cred_t *cr)
3037 {
3038         struct buf *bp;
3039         int error;
3040         page_t *savepp;
3041         uchar_t fsdata;
3042         stable_how4 stab_comm;
3043 
3044         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3045         bp = pageio_setup(pp, len, vp, flags);
3046         ASSERT(bp != NULL);
3047 
3048         /*
3049          * pageio_setup should have set b_addr to 0.  This
3050          * is correct since we want to do I/O on a page
3051          * boundary.  bp_mapin will use this addr to calculate
3052          * an offset, and then set b_addr to the kernel virtual
3053          * address it allocated for us.
3054          */
3055         ASSERT(bp->b_un.b_addr == 0);
3056 
3057         bp->b_edev = 0;
3058         bp->b_dev = 0;
3059         bp->b_lblkno = lbtodb(off);
3060         bp->b_file = vp;
3061         bp->b_offset = (offset_t)off;
3062         bp_mapin(bp);
3063 
3064         if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3065             freemem > desfree)
3066                 stab_comm = UNSTABLE4;
3067         else
3068                 stab_comm = FILE_SYNC4;
3069 
3070         error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3071 
3072         bp_mapout(bp);
3073         pageio_done(bp);
3074 
3075         if (stab_comm == UNSTABLE4)
3076                 fsdata = C_DELAYCOMMIT;
3077         else
3078                 fsdata = C_NOCOMMIT;
3079 
3080         savepp = pp;
3081         do {
3082                 pp->p_fsdata = fsdata;
3083         } while ((pp = pp->p_next) != savepp);
3084 
3085         return (error);
3086 }
3087 
3088 /*
3089  */
3090 static int
3091 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3092 {
3093         nfs4_open_owner_t       *oop;
3094         nfs4_open_stream_t      *osp;
3095         rnode4_t                *rp = VTOR4(vp);
3096         mntinfo4_t              *mi = VTOMI4(vp);
3097         int                     reopen_needed;
3098 
3099         ASSERT(nfs_zone() == mi->mi_zone);
3100 
3101 
3102         oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3103         if (!oop)
3104                 return (EIO);
3105 
3106         /* returns with 'os_sync_lock' held */
3107         osp = find_open_stream(oop, rp);
3108         if (!osp) {
3109                 open_owner_rele(oop);
3110                 return (EIO);
3111         }
3112 
3113         if (osp->os_failed_reopen) {
3114                 mutex_exit(&osp->os_sync_lock);
3115                 open_stream_rele(osp, rp);
3116                 open_owner_rele(oop);
3117                 return (EIO);
3118         }
3119 
3120         /*
3121          * Determine whether a reopen is needed.  If this
3122          * is a delegation open stream, then the os_delegation bit
3123          * should be set.
3124          */
3125 
3126         reopen_needed = osp->os_delegation;
3127 
3128         mutex_exit(&osp->os_sync_lock);
3129         open_owner_rele(oop);
3130 
3131         if (reopen_needed) {
3132                 nfs4_error_zinit(ep);
3133                 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3134                 mutex_enter(&osp->os_sync_lock);
3135                 if (ep->error || ep->stat || osp->os_failed_reopen) {
3136                         mutex_exit(&osp->os_sync_lock);
3137                         open_stream_rele(osp, rp);
3138                         return (EIO);
3139                 }
3140                 mutex_exit(&osp->os_sync_lock);
3141         }
3142         open_stream_rele(osp, rp);
3143 
3144         return (0);
3145 }
3146 
3147 /*
3148  * Write to file.  Writes to remote server in largest size
3149  * chunks that the server can handle.  Write is synchronous.
3150  */
3151 static int
3152 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3153     stable_how4 *stab_comm)
3154 {
3155         mntinfo4_t *mi;
3156         COMPOUND4args_clnt args;
3157         COMPOUND4res_clnt res;
3158         WRITE4args *wargs;
3159         WRITE4res *wres;
3160         nfs_argop4 argop[2];
3161         nfs_resop4 *resop;
3162         int tsize;
3163         stable_how4 stable;
3164         rnode4_t *rp;
3165         int doqueue = 1;
3166         bool_t needrecov;
3167         nfs4_recov_state_t recov_state;
3168         nfs4_stateid_types_t sid_types;
3169         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3170         int recov;
3171 
3172         rp = VTOR4(vp);
3173         mi = VTOMI4(vp);
3174 
3175         ASSERT(nfs_zone() == mi->mi_zone);
3176 
3177         stable = *stab_comm;
3178         *stab_comm = FILE_SYNC4;
3179 
3180         needrecov = FALSE;
3181         recov_state.rs_flags = 0;
3182         recov_state.rs_num_retry_despite_err = 0;
3183         nfs4_init_stateid_types(&sid_types);
3184 
3185         /* Is curthread the recovery thread? */
3186         mutex_enter(&mi->mi_lock);
3187         recov = (mi->mi_recovthread == curthread);
3188         mutex_exit(&mi->mi_lock);
3189 
3190 recov_retry:
3191         args.ctag = TAG_WRITE;
3192         args.array_len = 2;
3193         args.array = argop;
3194 
3195         if (!recov) {
3196                 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3197                     &recov_state, NULL);
3198                 if (e.error)
3199                         return (e.error);
3200         }
3201 
3202         /* 0. putfh target fh */
3203         argop[0].argop = OP_CPUTFH;
3204         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3205 
3206         /* 1. write */
3207         nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3208 
3209         do {
3210 
3211                 wargs->offset = (offset4)offset;
3212                 wargs->data_val = base;
3213 
3214                 if (mi->mi_io_kstats) {
3215                         mutex_enter(&mi->mi_lock);
3216                         kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3217                         mutex_exit(&mi->mi_lock);
3218                 }
3219 
3220                 if ((vp->v_flag & VNOCACHE) ||
3221                     (rp->r_flags & R4DIRECTIO) ||
3222                     (mi->mi_flags & MI4_DIRECTIO))
3223                         tsize = MIN(mi->mi_stsize, count);
3224                 else
3225                         tsize = MIN(mi->mi_curwrite, count);
3226                 wargs->data_len = (uint_t)tsize;
3227                 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3228 
3229                 if (mi->mi_io_kstats) {
3230                         mutex_enter(&mi->mi_lock);
3231                         kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3232                         mutex_exit(&mi->mi_lock);
3233                 }
3234 
3235                 if (!recov) {
3236                         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3237                         if (e.error && !needrecov) {
3238                                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3239                                     &recov_state, needrecov);
3240                                 return (e.error);
3241                         }
3242                 } else {
3243                         if (e.error)
3244                                 return (e.error);
3245                 }
3246 
3247                 /*
3248                  * Do handling of OLD_STATEID outside
3249                  * of the normal recovery framework.
3250                  *
3251                  * If write receives a BAD stateid error while using a
3252                  * delegation stateid, retry using the open stateid (if it
3253                  * exists).  If it doesn't have an open stateid, reopen the
3254                  * file first, then retry.
3255                  */
3256                 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3257                     sid_types.cur_sid_type != SPEC_SID) {
3258                         nfs4_save_stateid(&wargs->stateid, &sid_types);
3259                         if (!recov)
3260                                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3261                                     &recov_state, needrecov);
3262                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3263                         goto recov_retry;
3264                 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3265                     sid_types.cur_sid_type == DEL_SID) {
3266                         nfs4_save_stateid(&wargs->stateid, &sid_types);
3267                         mutex_enter(&rp->r_statev4_lock);
3268                         rp->r_deleg_return_pending = TRUE;
3269                         mutex_exit(&rp->r_statev4_lock);
3270                         if (nfs4rdwr_check_osid(vp, &e, cr)) {
3271                                 if (!recov)
3272                                         nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3273                                             &recov_state, needrecov);
3274                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3275                                     (caddr_t)&res);
3276                                 return (EIO);
3277                         }
3278                         if (!recov)
3279                                 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3280                                     &recov_state, needrecov);
3281                         /* hold needed for nfs4delegreturn_thread */
3282                         VN_HOLD(vp);
3283                         nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3284                             NFS4_DR_DISCARD), FALSE);
3285                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3286                         goto recov_retry;
3287                 }
3288 
3289                 if (needrecov) {
3290                         bool_t abort;
3291 
3292                         NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3293                             "nfs4write: client got error %d, res.status %d"
3294                             ", so start recovery", e.error, res.status));
3295 
3296                         abort = nfs4_start_recovery(&e,
3297                             VTOMI4(vp), vp, NULL, &wargs->stateid,
3298                             NULL, OP_WRITE, NULL, NULL, NULL);
3299                         if (!e.error) {
3300                                 e.error = geterrno4(res.status);
3301                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3302                                     (caddr_t)&res);
3303                         }
3304                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3305                             &recov_state, needrecov);
3306                         if (abort == FALSE)
3307                                 goto recov_retry;
3308                         return (e.error);
3309                 }
3310 
3311                 if (res.status) {
3312                         e.error = geterrno4(res.status);
3313                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3314                         if (!recov)
3315                                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3316                                     &recov_state, needrecov);
3317                         return (e.error);
3318                 }
3319 
3320                 resop = &res.array[1];      /* write res */
3321                 wres = &resop->nfs_resop4_u.opwrite;
3322 
3323                 if ((int)wres->count > tsize) {
3324                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3325 
3326                         zcmn_err(getzoneid(), CE_WARN,
3327                             "nfs4write: server wrote %u, requested was %u",
3328                             (int)wres->count, tsize);
3329                         if (!recov)
3330                                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3331                                     &recov_state, needrecov);
3332                         return (EIO);
3333                 }
3334                 if (wres->committed == UNSTABLE4) {
3335                         *stab_comm = UNSTABLE4;
3336                         if (wargs->stable == DATA_SYNC4 ||
3337                             wargs->stable == FILE_SYNC4) {
3338                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3339                                     (caddr_t)&res);
3340                                 zcmn_err(getzoneid(), CE_WARN,
3341                                     "nfs4write: server %s did not commit "
3342                                     "to stable storage",
3343                                     rp->r_server->sv_hostname);
3344                                 if (!recov)
3345                                         nfs4_end_fop(VTOMI4(vp), vp, NULL,
3346                                             OH_WRITE, &recov_state, needrecov);
3347                                 return (EIO);
3348                         }
3349                 }
3350 
3351                 tsize = (int)wres->count;
3352                 count -= tsize;
3353                 base += tsize;
3354                 offset += tsize;
3355                 if (mi->mi_io_kstats) {
3356                         mutex_enter(&mi->mi_lock);
3357                         KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3358                         KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3359                             tsize;
3360                         mutex_exit(&mi->mi_lock);
3361                 }
3362                 lwp_stat_update(LWP_STAT_OUBLK, 1);
3363                 mutex_enter(&rp->r_statelock);
3364                 if (rp->r_flags & R4HAVEVERF) {
3365                         if (rp->r_writeverf != wres->writeverf) {
3366                                 nfs4_set_mod(vp);
3367                                 rp->r_writeverf = wres->writeverf;
3368                         }
3369                 } else {
3370                         rp->r_writeverf = wres->writeverf;
3371                         rp->r_flags |= R4HAVEVERF;
3372                 }
3373                 PURGE_ATTRCACHE4_LOCKED(rp);
3374                 rp->r_flags |= R4WRITEMODIFIED;
3375                 gethrestime(&rp->r_attr.va_mtime);
3376                 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3377                 mutex_exit(&rp->r_statelock);
3378                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3379         } while (count);
3380 
3381         if (!recov)
3382                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3383                     needrecov);
3384 
3385         return (e.error);
3386 }
3387 
3388 /*
3389  * Read from a file.  Reads data in largest chunks our interface can handle.
3390  */
3391 static int
3392 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3393     size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3394 {
3395         mntinfo4_t *mi;
3396         COMPOUND4args_clnt args;
3397         COMPOUND4res_clnt res;
3398         READ4args *rargs;
3399         nfs_argop4 argop[2];
3400         int tsize;
3401         int doqueue;
3402         rnode4_t *rp;
3403         int data_len;
3404         bool_t is_eof;
3405         bool_t needrecov = FALSE;
3406         nfs4_recov_state_t recov_state;
3407         nfs4_stateid_types_t sid_types;
3408         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3409 
3410         rp = VTOR4(vp);
3411         mi = VTOMI4(vp);
3412         doqueue = 1;
3413 
3414         ASSERT(nfs_zone() == mi->mi_zone);
3415 
3416         args.ctag = async ? TAG_READAHEAD : TAG_READ;
3417 
3418         args.array_len = 2;
3419         args.array = argop;
3420 
3421         nfs4_init_stateid_types(&sid_types);
3422 
3423         recov_state.rs_flags = 0;
3424         recov_state.rs_num_retry_despite_err = 0;
3425 
3426 recov_retry:
3427         e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3428             &recov_state, NULL);
3429         if (e.error)
3430                 return (e.error);
3431 
3432         /* putfh target fh */
3433         argop[0].argop = OP_CPUTFH;
3434         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3435 
3436         /* read */
3437         argop[1].argop = OP_READ;
3438         rargs = &argop[1].nfs_argop4_u.opread;
3439         rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3440             OP_READ, &sid_types, async);
3441 
3442         do {
3443                 if (mi->mi_io_kstats) {
3444                         mutex_enter(&mi->mi_lock);
3445                         kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3446                         mutex_exit(&mi->mi_lock);
3447                 }
3448 
3449                 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3450                     "nfs4read: %s call, rp %s",
3451                     needrecov ? "recov" : "first",
3452                     rnode4info(rp)));
3453 
3454                 if ((vp->v_flag & VNOCACHE) ||
3455                     (rp->r_flags & R4DIRECTIO) ||
3456                     (mi->mi_flags & MI4_DIRECTIO))
3457                         tsize = MIN(mi->mi_tsize, count);
3458                 else
3459                         tsize = MIN(mi->mi_curread, count);
3460 
3461                 rargs->offset = (offset4)offset;
3462                 rargs->count = (count4)tsize;
3463                 rargs->res_data_val_alt = NULL;
3464                 rargs->res_mblk = NULL;
3465                 rargs->res_uiop = NULL;
3466                 rargs->res_maxsize = 0;
3467                 rargs->wlist = NULL;
3468 
3469                 if (uiop)
3470                         rargs->res_uiop = uiop;
3471                 else
3472                         rargs->res_data_val_alt = base;
3473                 rargs->res_maxsize = tsize;
3474 
3475                 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3476 #ifdef  DEBUG
3477                 if (nfs4read_error_inject) {
3478                         res.status = nfs4read_error_inject;
3479                         nfs4read_error_inject = 0;
3480                 }
3481 #endif
3482 
3483                 if (mi->mi_io_kstats) {
3484                         mutex_enter(&mi->mi_lock);
3485                         kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3486                         mutex_exit(&mi->mi_lock);
3487                 }
3488 
3489                 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3490                 if (e.error != 0 && !needrecov) {
3491                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3492                             &recov_state, needrecov);
3493                         return (e.error);
3494                 }
3495 
3496                 /*
3497                  * Do proper retry for OLD and BAD stateid errors outside
3498                  * of the normal recovery framework.  There are two differences
3499                  * between async and sync reads.  The first is that we allow
3500                  * retry on BAD_STATEID for async reads, but not sync reads.
3501                  * The second is that we mark the file dead for a failed
3502                  * attempt with a special stateid for sync reads, but just
3503                  * return EIO for async reads.
3504                  *
3505                  * If a sync read receives a BAD stateid error while using a
3506                  * delegation stateid, retry using the open stateid (if it
3507                  * exists).  If it doesn't have an open stateid, reopen the
3508                  * file first, then retry.
3509                  */
3510                 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3511                     res.status == NFS4ERR_BAD_STATEID) && async) {
3512                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3513                             &recov_state, needrecov);
3514                         if (sid_types.cur_sid_type == SPEC_SID) {
3515                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3516                                     (caddr_t)&res);
3517                                 return (EIO);
3518                         }
3519                         nfs4_save_stateid(&rargs->stateid, &sid_types);
3520                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3521                         goto recov_retry;
3522                 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3523                     !async && sid_types.cur_sid_type != SPEC_SID) {
3524                         nfs4_save_stateid(&rargs->stateid, &sid_types);
3525                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3526                             &recov_state, needrecov);
3527                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3528                         goto recov_retry;
3529                 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3530                     sid_types.cur_sid_type == DEL_SID) {
3531                         nfs4_save_stateid(&rargs->stateid, &sid_types);
3532                         mutex_enter(&rp->r_statev4_lock);
3533                         rp->r_deleg_return_pending = TRUE;
3534                         mutex_exit(&rp->r_statev4_lock);
3535                         if (nfs4rdwr_check_osid(vp, &e, cr)) {
3536                                 nfs4_end_fop(mi, vp, NULL, OH_READ,
3537                                     &recov_state, needrecov);
3538                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3539                                     (caddr_t)&res);
3540                                 return (EIO);
3541                         }
3542                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3543                             &recov_state, needrecov);
3544                         /* hold needed for nfs4delegreturn_thread */
3545                         VN_HOLD(vp);
3546                         nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3547                             NFS4_DR_DISCARD), FALSE);
3548                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3549                         goto recov_retry;
3550                 }
3551                 if (needrecov) {
3552                         bool_t abort;
3553 
3554                         NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3555                             "nfs4read: initiating recovery\n"));
3556                         abort = nfs4_start_recovery(&e,
3557                             mi, vp, NULL, &rargs->stateid,
3558                             NULL, OP_READ, NULL, NULL, NULL);
3559                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3560                             &recov_state, needrecov);
3561                         /*
3562                          * Do not retry if we got OLD_STATEID using a special
3563                          * stateid.  This avoids looping with a broken server.
3564                          */
3565                         if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3566                             sid_types.cur_sid_type == SPEC_SID)
3567                                 abort = TRUE;
3568 
3569                         if (abort == FALSE) {
3570                                 /*
3571                                  * Need to retry all possible stateids in
3572                                  * case the recovery error wasn't stateid
3573                                  * related or the stateids have become
3574                                  * stale (server reboot).
3575                                  */
3576                                 nfs4_init_stateid_types(&sid_types);
3577                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3578                                     (caddr_t)&res);
3579                                 goto recov_retry;
3580                         }
3581 
3582                         if (!e.error) {
3583                                 e.error = geterrno4(res.status);
3584                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
3585                                     (caddr_t)&res);
3586                         }
3587                         return (e.error);
3588                 }
3589 
3590                 if (res.status) {
3591                         e.error = geterrno4(res.status);
3592                         nfs4_end_fop(mi, vp, NULL, OH_READ,
3593                             &recov_state, needrecov);
3594                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3595                         return (e.error);
3596                 }
3597 
3598                 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3599                 count -= data_len;
3600                 if (base)
3601                         base += data_len;
3602                 offset += data_len;
3603                 if (mi->mi_io_kstats) {
3604                         mutex_enter(&mi->mi_lock);
3605                         KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3606                         KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3607                         mutex_exit(&mi->mi_lock);
3608                 }
3609                 lwp_stat_update(LWP_STAT_INBLK, 1);
3610                 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3611                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3612 
3613         } while (count && !is_eof);
3614 
3615         *residp = count;
3616 
3617         nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3618 
3619         return (e.error);
3620 }
3621 
3622 /* ARGSUSED */
3623 static int
3624 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3625         caller_context_t *ct)
3626 {
3627         if (nfs_zone() != VTOMI4(vp)->mi_zone)
3628                 return (EIO);
3629         switch (cmd) {
3630                 case _FIODIRECTIO:
3631                         return (nfs4_directio(vp, (int)arg, cr));
3632                 default:
3633                         return (ENOTTY);
3634         }
3635 }
3636 
3637 /* ARGSUSED */
3638 int
3639 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3640     caller_context_t *ct)
3641 {
3642         int error;
3643         rnode4_t *rp = VTOR4(vp);
3644 
3645         if (nfs_zone() != VTOMI4(vp)->mi_zone)
3646                 return (EIO);
3647         /*
3648          * If it has been specified that the return value will
3649          * just be used as a hint, and we are only being asked
3650          * for size, fsid or rdevid, then return the client's
3651          * notion of these values without checking to make sure
3652          * that the attribute cache is up to date.
3653          * The whole point is to avoid an over the wire GETATTR
3654          * call.
3655          */
3656         if (flags & ATTR_HINT) {
3657                 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3658                         mutex_enter(&rp->r_statelock);
3659                         if (vap->va_mask & AT_SIZE)
3660                                 vap->va_size = rp->r_size;
3661                         if (vap->va_mask & AT_FSID)
3662                                 vap->va_fsid = rp->r_attr.va_fsid;
3663                         if (vap->va_mask & AT_RDEV)
3664                                 vap->va_rdev = rp->r_attr.va_rdev;
3665                         mutex_exit(&rp->r_statelock);
3666                         return (0);
3667                 }
3668         }
3669 
3670         /*
3671          * Only need to flush pages if asking for the mtime
3672          * and if there any dirty pages or any outstanding
3673          * asynchronous (write) requests for this file.
3674          */
3675         if (vap->va_mask & AT_MTIME) {
3676                 rp = VTOR4(vp);
3677                 if (nfs4_has_pages(vp)) {
3678                         mutex_enter(&rp->r_statev4_lock);
3679                         if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3680                                 mutex_exit(&rp->r_statev4_lock);
3681                                 if (rp->r_flags & R4DIRTY ||
3682                                     rp->r_awcount > 0) {
3683                                         mutex_enter(&rp->r_statelock);
3684                                         rp->r_gcount++;
3685                                         mutex_exit(&rp->r_statelock);
3686                                         error =
3687                                             nfs4_putpage(vp, (u_offset_t)0,
3688                                             0, 0, cr, NULL);
3689                                         mutex_enter(&rp->r_statelock);
3690                                         if (error && (error == ENOSPC ||
3691                                             error == EDQUOT)) {
3692                                                 if (!rp->r_error)
3693                                                         rp->r_error = error;
3694                                         }
3695                                         if (--rp->r_gcount == 0)
3696                                                 cv_broadcast(&rp->r_cv);
3697                                         mutex_exit(&rp->r_statelock);
3698                                 }
3699                         } else {
3700                                 mutex_exit(&rp->r_statev4_lock);
3701                         }
3702                 }
3703         }
3704         return (nfs4getattr(vp, vap, cr));
3705 }
3706 
3707 int
3708 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3709 {
3710         /*
3711          * If these are the only two bits cleared
3712          * on the server then return 0 (OK) else
3713          * return 1 (BAD).
3714          */
3715         on_client &= ~(S_ISUID|S_ISGID);
3716         if (on_client == from_server)
3717                 return (0);
3718         else
3719                 return (1);
3720 }
3721 
3722 /*ARGSUSED4*/
3723 static int
3724 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3725     caller_context_t *ct)
3726 {
3727         int error;
3728 
3729         if (vap->va_mask & AT_NOSET)
3730                 return (EINVAL);
3731 
3732         if (nfs_zone() != VTOMI4(vp)->mi_zone)
3733                 return (EIO);
3734 
3735         /*
3736          * Don't call secpolicy_vnode_setattr, the client cannot
3737          * use its cached attributes to make security decisions
3738          * as the server may be faking mode bits or mapping uid/gid.
3739          * Always just let the server to the checking.
3740          * If we provide the ability to remove basic priviledges
3741          * to setattr (e.g. basic without chmod) then we will
3742          * need to add a check here before calling the server.
3743          */
3744         error = nfs4setattr(vp, vap, flags, cr, NULL);
3745 
3746         if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3747                 vnevent_truncate(vp, ct);
3748 
3749         return (error);
3750 }
3751 
3752 /*
3753  * To replace the "guarded" version 3 setattr, we use two types of compound
3754  * setattr requests:
3755  * 1. The "normal" setattr, used when the size of the file isn't being
3756  *    changed - { Putfh <fh>; Setattr; Getattr }/
3757  * 2. If the size is changed, precede Setattr with: Getattr; Verify
3758  *    with only ctime as the argument. If the server ctime differs from
3759  *    what is cached on the client, the verify will fail, but we would
3760  *    already have the ctime from the preceding getattr, so just set it
3761  *    and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3762  *      Setattr; Getattr }.
3763  *
3764  * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3765  * this setattr and NULL if they are not.
3766  */
3767 static int
3768 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3769     vsecattr_t *vsap)
3770 {
3771         COMPOUND4args_clnt args;
3772         COMPOUND4res_clnt res, *resp = NULL;
3773         nfs4_ga_res_t *garp = NULL;
3774         int numops = 3;                 /* { Putfh; Setattr; Getattr } */
3775         nfs_argop4 argop[5];
3776         int verify_argop = -1;
3777         int setattr_argop = 1;
3778         nfs_resop4 *resop;
3779         vattr_t va;
3780         rnode4_t *rp;
3781         int doqueue = 1;
3782         uint_t mask = vap->va_mask;
3783         mode_t omode;
3784         vsecattr_t *vsp;
3785         timestruc_t ctime;
3786         bool_t needrecov = FALSE;
3787         nfs4_recov_state_t recov_state;
3788         nfs4_stateid_types_t sid_types;
3789         stateid4 stateid;
3790         hrtime_t t;
3791         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3792         servinfo4_t *svp;
3793         bitmap4 supp_attrs;
3794 
3795         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3796         rp = VTOR4(vp);
3797         nfs4_init_stateid_types(&sid_types);
3798 
3799         /*
3800          * Only need to flush pages if there are any pages and
3801          * if the file is marked as dirty in some fashion.  The
3802          * file must be flushed so that we can accurately
3803          * determine the size of the file and the cached data
3804          * after the SETATTR returns.  A file is considered to
3805          * be dirty if it is either marked with R4DIRTY, has
3806          * outstanding i/o's active, or is mmap'd.  In this
3807          * last case, we can't tell whether there are dirty
3808          * pages, so we flush just to be sure.
3809          */
3810         if (nfs4_has_pages(vp) &&
3811             ((rp->r_flags & R4DIRTY) ||
3812             rp->r_count > 0 ||
3813             rp->r_mapcnt > 0)) {
3814                 ASSERT(vp->v_type != VCHR);
3815                 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3816                 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3817                         mutex_enter(&rp->r_statelock);
3818                         if (!rp->r_error)
3819                                 rp->r_error = e.error;
3820                         mutex_exit(&rp->r_statelock);
3821                 }
3822         }
3823 
3824         if (mask & AT_SIZE) {
3825                 /*
3826                  * Verification setattr compound for non-deleg AT_SIZE:
3827                  *      { Putfh; Getattr; Verify; Setattr; Getattr }
3828                  * Set ctime local here (outside the do_again label)
3829                  * so that subsequent retries (after failed VERIFY)
3830                  * will use ctime from GETATTR results (from failed
3831                  * verify compound) as VERIFY arg.
3832                  * If file has delegation, then VERIFY(time_metadata)
3833                  * is of little added value, so don't bother.
3834                  */
3835                 mutex_enter(&rp->r_statev4_lock);
3836                 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3837                     rp->r_deleg_return_pending) {
3838                         numops = 5;
3839                         ctime = rp->r_attr.va_ctime;
3840                 }
3841                 mutex_exit(&rp->r_statev4_lock);
3842         }
3843 
3844         recov_state.rs_flags = 0;
3845         recov_state.rs_num_retry_despite_err = 0;
3846 
3847         args.ctag = TAG_SETATTR;
3848 do_again:
3849 recov_retry:
3850         setattr_argop = numops - 2;
3851 
3852         args.array = argop;
3853         args.array_len = numops;
3854 
3855         e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3856         if (e.error)
3857                 return (e.error);
3858 
3859 
3860         /* putfh target fh */
3861         argop[0].argop = OP_CPUTFH;
3862         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3863 
3864         if (numops == 5) {
3865                 /*
3866                  * We only care about the ctime, but need to get mtime
3867                  * and size for proper cache update.
3868                  */
3869                 /* getattr */
3870                 argop[1].argop = OP_GETATTR;
3871                 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3872                 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3873 
3874                 /* verify - set later in loop */
3875                 verify_argop = 2;
3876         }
3877 
3878         /* setattr */
3879         svp = rp->r_server;
3880         (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3881         supp_attrs = svp->sv_supp_attrs;
3882         nfs_rw_exit(&svp->sv_lock);
3883 
3884         nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3885             supp_attrs, &e.error, &sid_types);
3886         stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3887         if (e.error) {
3888                 /* req time field(s) overflow - return immediately */
3889                 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3890                 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3891                     opsetattr.obj_attributes);
3892                 return (e.error);
3893         }
3894         omode = rp->r_attr.va_mode;
3895 
3896         /* getattr */
3897         argop[numops-1].argop = OP_GETATTR;
3898         argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3899         /*
3900          * If we are setting the ACL (indicated only by vsap != NULL), request
3901          * the ACL in this getattr.  The ACL returned from this getattr will be
3902          * used in updating the ACL cache.
3903          */
3904         if (vsap != NULL)
3905                 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3906                     FATTR4_ACL_MASK;
3907         argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3908 
3909         /*
3910          * setattr iterates if the object size is set and the cached ctime
3911          * does not match the file ctime. In that case, verify the ctime first.
3912          */
3913 
3914         do {
3915                 if (verify_argop != -1) {
3916                         /*
3917                          * Verify that the ctime match before doing setattr.
3918                          */
3919                         va.va_mask = AT_CTIME;
3920                         va.va_ctime = ctime;
3921                         svp = rp->r_server;
3922                         (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3923                         supp_attrs = svp->sv_supp_attrs;
3924                         nfs_rw_exit(&svp->sv_lock);
3925                         e.error = nfs4args_verify(&argop[verify_argop], &va,
3926                             OP_VERIFY, supp_attrs);
3927                         if (e.error) {
3928                                 /* req time field(s) overflow - return */
3929                                 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3930                                     needrecov);
3931                                 break;
3932                         }
3933                 }
3934 
3935                 doqueue = 1;
3936 
3937                 t = gethrtime();
3938 
3939                 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3940 
3941                 /*
3942                  * Purge the access cache and ACL cache if changing either the
3943                  * owner of the file, the group owner, or the mode.  These may
3944                  * change the access permissions of the file, so purge old
3945                  * information and start over again.
3946                  */
3947                 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3948                         (void) nfs4_access_purge_rp(rp);
3949                         if (rp->r_secattr != NULL) {
3950                                 mutex_enter(&rp->r_statelock);
3951                                 vsp = rp->r_secattr;
3952                                 rp->r_secattr = NULL;
3953                                 mutex_exit(&rp->r_statelock);
3954                                 if (vsp != NULL)
3955                                         nfs4_acl_free_cache(vsp);
3956                         }
3957                 }
3958 
3959                 /*
3960                  * If res.array_len == numops, then everything succeeded,
3961                  * except for possibly the final getattr.  If only the
3962                  * last getattr failed, give up, and don't try recovery.
3963                  */
3964                 if (res.array_len == numops) {
3965                         nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3966                             needrecov);
3967                         if (! e.error)
3968                                 resp = &res;
3969                         break;
3970                 }
3971 
3972                 /*
3973                  * if either rpc call failed or completely succeeded - done
3974                  */
3975                 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3976                 if (e.error) {
3977                         PURGE_ATTRCACHE4(vp);
3978                         if (!needrecov) {
3979                                 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3980                                     needrecov);
3981                                 break;
3982                         }
3983                 }
3984 
3985                 /*
3986                  * Do proper retry for OLD_STATEID outside of the normal
3987                  * recovery framework.
3988                  */
3989                 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3990                     sid_types.cur_sid_type != SPEC_SID &&
3991                     sid_types.cur_sid_type != NO_SID) {
3992                         nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3993                             needrecov);
3994                         nfs4_save_stateid(&stateid, &sid_types);
3995                         nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3996                             opsetattr.obj_attributes);
3997                         if (verify_argop != -1) {
3998                                 nfs4args_verify_free(&argop[verify_argop]);
3999                                 verify_argop = -1;
4000                         }
4001                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4002                         goto recov_retry;
4003                 }
4004 
4005                 if (needrecov) {
4006                         bool_t abort;
4007 
4008                         abort = nfs4_start_recovery(&e,
4009                             VTOMI4(vp), vp, NULL, NULL, NULL,
4010                             OP_SETATTR, NULL, NULL, NULL);
4011                         nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4012                             needrecov);
4013                         /*
4014                          * Do not retry if we failed with OLD_STATEID using
4015                          * a special stateid.  This is done to avoid looping
4016                          * with a broken server.
4017                          */
4018                         if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4019                             (sid_types.cur_sid_type == SPEC_SID ||
4020                             sid_types.cur_sid_type == NO_SID))
4021                                 abort = TRUE;
4022                         if (!e.error) {
4023                                 if (res.status == NFS4ERR_BADOWNER)
4024                                         nfs4_log_badowner(VTOMI4(vp),
4025                                             OP_SETATTR);
4026 
4027                                 e.error = geterrno4(res.status);
4028                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
4029                                     (caddr_t)&res);
4030                         }
4031                         nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4032                             opsetattr.obj_attributes);
4033                         if (verify_argop != -1) {
4034                                 nfs4args_verify_free(&argop[verify_argop]);
4035                                 verify_argop = -1;
4036                         }
4037                         if (abort == FALSE) {
4038                                 /*
4039                                  * Need to retry all possible stateids in
4040                                  * case the recovery error wasn't stateid
4041                                  * related or the stateids have become
4042                                  * stale (server reboot).
4043                                  */
4044                                 nfs4_init_stateid_types(&sid_types);
4045                                 goto recov_retry;
4046                         }
4047                         return (e.error);
4048                 }
4049 
4050                 /*
4051                  * Need to call nfs4_end_op before nfs4getattr to
4052                  * avoid potential nfs4_start_op deadlock. See RFE
4053                  * 4777612.  Calls to nfs4_invalidate_pages() and
4054                  * nfs4_purge_stale_fh() might also generate over the
4055                  * wire calls which my cause nfs4_start_op() deadlock.
4056                  */
4057                 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4058 
4059                 /*
4060                  * Check to update lease.
4061                  */
4062                 resp = &res;
4063                 if (res.status == NFS4_OK) {
4064                         break;
4065                 }
4066 
4067                 /*
4068                  * Check if verify failed to see if try again
4069                  */
4070                 if ((verify_argop == -1) || (res.array_len != 3)) {
4071                         /*
4072                          * can't continue...
4073                          */
4074                         if (res.status == NFS4ERR_BADOWNER)
4075                                 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4076 
4077                         e.error = geterrno4(res.status);
4078                 } else {
4079                         /*
4080                          * When the verify request fails, the client ctime is
4081                          * not in sync with the server. This is the same as
4082                          * the version 3 "not synchronized" error, and we
4083                          * handle it in a similar manner (XXX do we need to???).
4084                          * Use the ctime returned in the first getattr for
4085                          * the input to the next verify.
4086                          * If we couldn't get the attributes, then we give up
4087                          * because we can't complete the operation as required.
4088                          */
4089                         garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4090                 }
4091                 if (e.error) {
4092                         PURGE_ATTRCACHE4(vp);
4093                         nfs4_purge_stale_fh(e.error, vp, cr);
4094                 } else {
4095                         /*
4096                          * retry with a new verify value
4097                          */
4098                         ctime = garp->n4g_va.va_ctime;
4099                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4100                         resp = NULL;
4101                 }
4102                 if (!e.error) {
4103                         nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4104                             opsetattr.obj_attributes);
4105                         if (verify_argop != -1) {
4106                                 nfs4args_verify_free(&argop[verify_argop]);
4107                                 verify_argop = -1;
4108                         }
4109                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4110                         goto do_again;
4111                 }
4112         } while (!e.error);
4113 
4114         if (e.error) {
4115                 /*
4116                  * If we are here, rfs4call has an irrecoverable error - return
4117                  */
4118                 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4119                     opsetattr.obj_attributes);
4120                 if (verify_argop != -1) {
4121                         nfs4args_verify_free(&argop[verify_argop]);
4122                         verify_argop = -1;
4123                 }
4124                 if (resp)
4125                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4126                 return (e.error);
4127         }
4128 
4129 
4130 
4131         /*
4132          * If changing the size of the file, invalidate
4133          * any local cached data which is no longer part
4134          * of the file.  We also possibly invalidate the
4135          * last page in the file.  We could use
4136          * pvn_vpzero(), but this would mark the page as
4137          * modified and require it to be written back to
4138          * the server for no particularly good reason.
4139          * This way, if we access it, then we bring it
4140          * back in.  A read should be cheaper than a
4141          * write.
4142          */
4143         if (mask & AT_SIZE) {
4144                 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4145         }
4146 
4147         /* either no error or one of the postop getattr failed */
4148 
4149         /*
4150          * XXX Perform a simplified version of wcc checking. Instead of
4151          * have another getattr to get pre-op, just purge cache if
4152          * any of the ops prior to and including the getattr failed.
4153          * If the getattr succeeded then update the attrcache accordingly.
4154          */
4155 
4156         garp = NULL;
4157         if (res.status == NFS4_OK) {
4158                 /*
4159                  * Last getattr
4160                  */
4161                 resop = &res.array[numops - 1];
4162                 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4163         }
4164         /*
4165          * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4166          * rather than filling it.  See the function itself for details.
4167          */
4168         e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4169         if (garp != NULL) {
4170                 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4171                         nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4172                         vs_ace4_destroy(&garp->n4g_vsa);
4173                 } else {
4174                         if (vsap != NULL) {
4175                                 /*
4176                                  * The ACL was supposed to be set and to be
4177                                  * returned in the last getattr of this
4178                                  * compound, but for some reason the getattr
4179                                  * result doesn't contain the ACL.  In this
4180                                  * case, purge the ACL cache.
4181                                  */
4182                                 if (rp->r_secattr != NULL) {
4183                                         mutex_enter(&rp->r_statelock);
4184                                         vsp = rp->r_secattr;
4185                                         rp->r_secattr = NULL;
4186                                         mutex_exit(&rp->r_statelock);
4187                                         if (vsp != NULL)
4188                                                 nfs4_acl_free_cache(vsp);
4189                                 }
4190                         }
4191                 }
4192         }
4193 
4194         if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4195                 /*
4196                  * Set the size, rather than relying on getting it updated
4197                  * via a GETATTR.  With delegations the client tries to
4198                  * suppress GETATTR calls.
4199                  */
4200                 mutex_enter(&rp->r_statelock);
4201                 rp->r_size = vap->va_size;
4202                 mutex_exit(&rp->r_statelock);
4203         }
4204 
4205         /*
4206          * Can free up request args and res
4207          */
4208         nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4209             opsetattr.obj_attributes);
4210         if (verify_argop != -1) {
4211                 nfs4args_verify_free(&argop[verify_argop]);
4212                 verify_argop = -1;
4213         }
4214         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4215 
4216         /*
4217          * Some servers will change the mode to clear the setuid
4218          * and setgid bits when changing the uid or gid.  The
4219          * client needs to compensate appropriately.
4220          */
4221         if (mask & (AT_UID | AT_GID)) {
4222                 int terror, do_setattr;
4223 
4224                 do_setattr = 0;
4225                 va.va_mask = AT_MODE;
4226                 terror = nfs4getattr(vp, &va, cr);
4227                 if (!terror &&
4228                     (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4229                     (!(mask & AT_MODE) && va.va_mode != omode))) {
4230                         va.va_mask = AT_MODE;
4231                         if (mask & AT_MODE) {
4232                                 /*
4233                                  * We asked the mode to be changed and what
4234                                  * we just got from the server in getattr is
4235                                  * not what we wanted it to be, so set it now.
4236                                  */
4237                                 va.va_mode = vap->va_mode;
4238                                 do_setattr = 1;
4239                         } else {
4240                                 /*
4241                                  * We did not ask the mode to be changed,
4242                                  * Check to see that the server just cleared
4243                                  * I_SUID and I_GUID from it. If not then
4244                                  * set mode to omode with UID/GID cleared.
4245                                  */
4246                                 if (nfs4_compare_modes(va.va_mode, omode)) {
4247                                         omode &= ~(S_ISUID|S_ISGID);
4248                                         va.va_mode = omode;
4249                                         do_setattr = 1;
4250                                 }
4251                         }
4252 
4253                         if (do_setattr)
4254                                 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4255                 }
4256         }
4257 
4258         return (e.error);
4259 }
4260 
4261 /* ARGSUSED */
4262 static int
4263 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4264 {
4265         COMPOUND4args_clnt args;
4266         COMPOUND4res_clnt res;
4267         int doqueue;
4268         uint32_t acc, resacc, argacc;
4269         rnode4_t *rp;
4270         cred_t *cred, *ncr, *ncrfree = NULL;
4271         nfs4_access_type_t cacc;
4272         int num_ops;
4273         nfs_argop4 argop[3];
4274         nfs_resop4 *resop;
4275         bool_t needrecov = FALSE, do_getattr;
4276         nfs4_recov_state_t recov_state;
4277         int rpc_error;
4278         hrtime_t t;
4279         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4280         mntinfo4_t *mi = VTOMI4(vp);
4281 
4282         if (nfs_zone() != mi->mi_zone)
4283                 return (EIO);
4284 
4285         acc = 0;
4286         if (mode & VREAD)
4287                 acc |= ACCESS4_READ;
4288         if (mode & VWRITE) {
4289                 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4290                         return (EROFS);
4291                 if (vp->v_type == VDIR)
4292                         acc |= ACCESS4_DELETE;
4293                 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4294         }
4295         if (mode & VEXEC) {
4296                 if (vp->v_type == VDIR)
4297                         acc |= ACCESS4_LOOKUP;
4298                 else
4299                         acc |= ACCESS4_EXECUTE;
4300         }
4301 
4302         if (VTOR4(vp)->r_acache != NULL) {
4303                 e.error = nfs4_validate_caches(vp, cr);
4304                 if (e.error)
4305                         return (e.error);
4306         }
4307 
4308         rp = VTOR4(vp);
4309         if (vp->v_type == VDIR)
4310                 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4311                     ACCESS4_EXTEND | ACCESS4_LOOKUP;
4312         else
4313                 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4314                     ACCESS4_EXECUTE;
4315         recov_state.rs_flags = 0;
4316         recov_state.rs_num_retry_despite_err = 0;
4317 
4318         cred = cr;
4319         /*
4320          * ncr and ncrfree both initially
4321          * point to the memory area returned
4322          * by crnetadjust();
4323          * ncrfree not NULL when exiting means
4324          * that we need to release it
4325          */
4326         ncr = crnetadjust(cred);
4327         ncrfree = ncr;
4328 
4329 tryagain:
4330         cacc = nfs4_access_check(rp, acc, cred);
4331         if (cacc == NFS4_ACCESS_ALLOWED) {
4332                 if (ncrfree != NULL)
4333                         crfree(ncrfree);
4334                 return (0);
4335         }
4336         if (cacc == NFS4_ACCESS_DENIED) {
4337                 /*
4338                  * If the cred can be adjusted, try again
4339                  * with the new cred.
4340                  */
4341                 if (ncr != NULL) {
4342                         cred = ncr;
4343                         ncr = NULL;
4344                         goto tryagain;
4345                 }
4346                 if (ncrfree != NULL)
4347                         crfree(ncrfree);
4348                 return (EACCES);
4349         }
4350 
4351 recov_retry:
4352         /*
4353          * Don't take with r_statev4_lock here. r_deleg_type could
4354          * change as soon as lock is released.  Since it is an int,
4355          * there is no atomicity issue.
4356          */
4357         do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4358         num_ops = do_getattr ? 3 : 2;
4359 
4360         args.ctag = TAG_ACCESS;
4361 
4362         args.array_len = num_ops;
4363         args.array = argop;
4364 
4365         if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4366             &recov_state, NULL)) {
4367                 if (ncrfree != NULL)
4368                         crfree(ncrfree);
4369                 return (e.error);
4370         }
4371 
4372         /* putfh target fh */
4373         argop[0].argop = OP_CPUTFH;
4374         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4375 
4376         /* access */
4377         argop[1].argop = OP_ACCESS;
4378         argop[1].nfs_argop4_u.opaccess.access = argacc;
4379 
4380         /* getattr */
4381         if (do_getattr) {
4382                 argop[2].argop = OP_GETATTR;
4383                 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4384                 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4385         }
4386 
4387         NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4388             "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4389             rnode4info(VTOR4(vp))));
4390 
4391         doqueue = 1;
4392         t = gethrtime();
4393         rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4394         rpc_error = e.error;
4395 
4396         needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4397         if (needrecov) {
4398                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4399                     "nfs4_access: initiating recovery\n"));
4400 
4401                 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4402                     NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4403                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4404                             &recov_state, needrecov);
4405                         if (!e.error)
4406                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
4407                                     (caddr_t)&res);
4408                         goto recov_retry;
4409                 }
4410         }
4411         nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4412 
4413         if (e.error)
4414                 goto out;
4415 
4416         if (res.status) {
4417                 e.error = geterrno4(res.status);
4418                 /*
4419                  * This might generate over the wire calls throught
4420                  * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4421                  * here to avoid a deadlock.
4422                  */
4423                 nfs4_purge_stale_fh(e.error, vp, cr);
4424                 goto out;
4425         }
4426         resop = &res.array[1];      /* access res */
4427 
4428         resacc = resop->nfs_resop4_u.opaccess.access;
4429 
4430         if (do_getattr) {
4431                 resop++;        /* getattr res */
4432                 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4433                     t, cr, FALSE, NULL);
4434         }
4435 
4436         if (!e.error) {
4437                 nfs4_access_cache(rp, argacc, resacc, cred);
4438                 /*
4439                  * we just cached results with cred; if cred is the
4440                  * adjusted credentials from crnetadjust, we do not want
4441                  * to release them before exiting: hence setting ncrfree
4442                  * to NULL
4443                  */
4444                 if (cred != cr)
4445                         ncrfree = NULL;
4446                 /* XXX check the supported bits too? */
4447                 if ((acc & resacc) != acc) {
4448                         /*
4449                          * The following code implements the semantic
4450                          * that a setuid root program has *at least* the
4451                          * permissions of the user that is running the
4452                          * program.  See rfs3call() for more portions
4453                          * of the implementation of this functionality.
4454                          */
4455                         /* XXX-LP */
4456                         if (ncr != NULL) {
4457                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
4458                                     (caddr_t)&res);
4459                                 cred = ncr;
4460                                 ncr = NULL;
4461                                 goto tryagain;
4462                         }
4463                         e.error = EACCES;
4464                 }
4465         }
4466 
4467 out:
4468         if (!rpc_error)
4469                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4470 
4471         if (ncrfree != NULL)
4472                 crfree(ncrfree);
4473 
4474         return (e.error);
4475 }
4476 
4477 /* ARGSUSED */
4478 static int
4479 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4480 {
4481         COMPOUND4args_clnt args;
4482         COMPOUND4res_clnt res;
4483         int doqueue;
4484         rnode4_t *rp;
4485         nfs_argop4 argop[3];
4486         nfs_resop4 *resop;
4487         READLINK4res *lr_res;
4488         nfs4_ga_res_t *garp;
4489         uint_t len;
4490         char *linkdata;
4491         bool_t needrecov = FALSE;
4492         nfs4_recov_state_t recov_state;
4493         hrtime_t t;
4494         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4495 
4496         if (nfs_zone() != VTOMI4(vp)->mi_zone)
4497                 return (EIO);
4498         /*
4499          * Can't readlink anything other than a symbolic link.
4500          */
4501         if (vp->v_type != VLNK)
4502                 return (EINVAL);
4503 
4504         rp = VTOR4(vp);
4505         if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4506                 e.error = nfs4_validate_caches(vp, cr);
4507                 if (e.error)
4508                         return (e.error);
4509                 mutex_enter(&rp->r_statelock);
4510                 if (rp->r_symlink.contents != NULL) {
4511                         e.error = uiomove(rp->r_symlink.contents,
4512                             rp->r_symlink.len, UIO_READ, uiop);
4513                         mutex_exit(&rp->r_statelock);
4514                         return (e.error);
4515                 }
4516                 mutex_exit(&rp->r_statelock);
4517         }
4518         recov_state.rs_flags = 0;
4519         recov_state.rs_num_retry_despite_err = 0;
4520 
4521 recov_retry:
4522         args.array_len = 3;
4523         args.array = argop;
4524         args.ctag = TAG_READLINK;
4525 
4526         e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4527         if (e.error) {
4528                 return (e.error);
4529         }
4530 
4531         /* 0. putfh symlink fh */
4532         argop[0].argop = OP_CPUTFH;
4533         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4534 
4535         /* 1. readlink */
4536         argop[1].argop = OP_READLINK;
4537 
4538         /* 2. getattr */
4539         argop[2].argop = OP_GETATTR;
4540         argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4541         argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4542 
4543         doqueue = 1;
4544 
4545         NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4546             "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4547             rnode4info(VTOR4(vp))));
4548 
4549         t = gethrtime();
4550 
4551         rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4552 
4553         needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4554         if (needrecov) {
4555                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4556                     "nfs4_readlink: initiating recovery\n"));
4557 
4558                 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4559                     NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4560                         if (!e.error)
4561                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
4562                                     (caddr_t)&res);
4563 
4564                         nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4565                             needrecov);
4566                         goto recov_retry;
4567                 }
4568         }
4569 
4570         nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4571 
4572         if (e.error)
4573                 return (e.error);
4574 
4575         /*
4576          * There is an path in the code below which calls
4577          * nfs4_purge_stale_fh(), which may generate otw calls through
4578          * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4579          * here to avoid nfs4_start_op() deadlock.
4580          */
4581 
4582         if (res.status && (res.array_len < args.array_len)) {
4583                 /*
4584                  * either Putfh or Link failed
4585                  */
4586                 e.error = geterrno4(res.status);
4587                 nfs4_purge_stale_fh(e.error, vp, cr);
4588                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4589                 return (e.error);
4590         }
4591 
4592         resop = &res.array[1];      /* readlink res */
4593         lr_res = &resop->nfs_resop4_u.opreadlink;
4594 
4595         /*
4596          * treat symlink names as data
4597          */
4598         linkdata = utf8_to_str(&lr_res->link, &len, NULL);
4599         if (linkdata != NULL) {
4600                 int uio_len = len - 1;
4601                 /* len includes null byte, which we won't uiomove */
4602                 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4603                 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4604                         mutex_enter(&rp->r_statelock);
4605                         if (rp->r_symlink.contents == NULL) {
4606                                 rp->r_symlink.contents = linkdata;
4607                                 rp->r_symlink.len = uio_len;
4608                                 rp->r_symlink.size = len;
4609                                 mutex_exit(&rp->r_statelock);
4610                         } else {
4611                                 mutex_exit(&rp->r_statelock);
4612                                 kmem_free(linkdata, len);
4613                         }
4614                 } else {
4615                         kmem_free(linkdata, len);
4616                 }
4617         }
4618         if (res.status == NFS4_OK) {
4619                 resop++;        /* getattr res */
4620                 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4621         }
4622         e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4623 
4624         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4625 
4626         /*
4627          * The over the wire error for attempting to readlink something
4628          * other than a symbolic link is ENXIO.  However, we need to
4629          * return EINVAL instead of ENXIO, so we map it here.
4630          */
4631         return (e.error == ENXIO ? EINVAL : e.error);
4632 }
4633 
4634 /*
4635  * Flush local dirty pages to stable storage on the server.
4636  *
4637  * If FNODSYNC is specified, then there is nothing to do because
4638  * metadata changes are not cached on the client before being
4639  * sent to the server.
4640  */
4641 /* ARGSUSED */
4642 static int
4643 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4644 {
4645         int error;
4646 
4647         if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4648                 return (0);
4649         if (nfs_zone() != VTOMI4(vp)->mi_zone)
4650                 return (EIO);
4651         error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4652         if (!error)
4653                 error = VTOR4(vp)->r_error;
4654         return (error);
4655 }
4656 
4657 /*
4658  * Weirdness: if the file was removed or the target of a rename
4659  * operation while it was open, it got renamed instead.  Here we
4660  * remove the renamed file.
4661  */
4662 /* ARGSUSED */
4663 void
4664 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4665 {
4666         rnode4_t *rp;
4667 
4668         ASSERT(vp != DNLC_NO_VNODE);
4669 
4670         rp = VTOR4(vp);
4671 
4672         if (IS_SHADOW(vp, rp)) {
4673                 sv_inactive(vp);
4674                 return;
4675         }
4676 
4677         /*
4678          * If this is coming from the wrong zone, we let someone in the right
4679          * zone take care of it asynchronously.  We can get here due to
4680          * VN_RELE() being called from pageout() or fsflush().  This call may
4681          * potentially turn into an expensive no-op if, for instance, v_count
4682          * gets incremented in the meantime, but it's still correct.
4683          */
4684         if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4685                 nfs4_async_inactive(vp, cr);
4686                 return;
4687         }
4688 
4689         /*
4690          * Some of the cleanup steps might require over-the-wire
4691          * operations.  Since VOP_INACTIVE can get called as a result of
4692          * other over-the-wire operations (e.g., an attribute cache update
4693          * can lead to a DNLC purge), doing those steps now would lead to a
4694          * nested call to the recovery framework, which can deadlock.  So
4695          * do any over-the-wire cleanups asynchronously, in a separate
4696          * thread.
4697          */
4698 
4699         mutex_enter(&rp->r_os_lock);
4700         mutex_enter(&rp->r_statelock);
4701         mutex_enter(&rp->r_statev4_lock);
4702 
4703         if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4704                 mutex_exit(&rp->r_statev4_lock);
4705                 mutex_exit(&rp->r_statelock);
4706                 mutex_exit(&rp->r_os_lock);
4707                 nfs4_async_inactive(vp, cr);
4708                 return;
4709         }
4710 
4711         if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4712             rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4713                 mutex_exit(&rp->r_statev4_lock);
4714                 mutex_exit(&rp->r_statelock);
4715                 mutex_exit(&rp->r_os_lock);
4716                 nfs4_async_inactive(vp, cr);
4717                 return;
4718         }
4719 
4720         if (rp->r_unldvp != NULL) {
4721                 mutex_exit(&rp->r_statev4_lock);
4722                 mutex_exit(&rp->r_statelock);
4723                 mutex_exit(&rp->r_os_lock);
4724                 nfs4_async_inactive(vp, cr);
4725                 return;
4726         }
4727         mutex_exit(&rp->r_statev4_lock);
4728         mutex_exit(&rp->r_statelock);
4729         mutex_exit(&rp->r_os_lock);
4730 
4731         rp4_addfree(rp, cr);
4732 }
4733 
4734 /*
4735  * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4736  * various bits of state.  The caller must not refer to vp after this call.
4737  */
4738 
4739 void
4740 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4741 {
4742         rnode4_t *rp = VTOR4(vp);
4743         nfs4_recov_state_t recov_state;
4744         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4745         vnode_t *unldvp;
4746         char *unlname;
4747         cred_t *unlcred;
4748         COMPOUND4args_clnt args;
4749         COMPOUND4res_clnt res, *resp;
4750         nfs_argop4 argop[2];
4751         int doqueue;
4752 #ifdef DEBUG
4753         char *name;
4754 #endif
4755 
4756         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4757         ASSERT(!IS_SHADOW(vp, rp));
4758 
4759 #ifdef DEBUG
4760         name = fn_name(VTOSV(vp)->sv_name);
4761         NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4762             "release vnode %s", name));
4763         kmem_free(name, MAXNAMELEN);
4764 #endif
4765 
4766         if (vp->v_type == VREG) {
4767                 bool_t recov_failed = FALSE;
4768 
4769                 e.error = nfs4close_all(vp, cr);
4770                 if (e.error) {
4771                         /* Check to see if recovery failed */
4772                         mutex_enter(&(VTOMI4(vp)->mi_lock));
4773                         if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4774                                 recov_failed = TRUE;
4775                         mutex_exit(&(VTOMI4(vp)->mi_lock));
4776                         if (!recov_failed) {
4777                                 mutex_enter(&rp->r_statelock);
4778                                 if (rp->r_flags & R4RECOVERR)
4779                                         recov_failed = TRUE;
4780                                 mutex_exit(&rp->r_statelock);
4781                         }
4782                         if (recov_failed) {
4783                                 NFS4_DEBUG(nfs4_client_recov_debug,
4784                                     (CE_NOTE, "nfs4_inactive_otw: "
4785                                     "close failed (recovery failure)"));
4786                         }
4787                 }
4788         }
4789 
4790 redo:
4791         if (rp->r_unldvp == NULL) {
4792                 rp4_addfree(rp, cr);
4793                 return;
4794         }
4795 
4796         /*
4797          * Save the vnode pointer for the directory where the
4798          * unlinked-open file got renamed, then set it to NULL
4799          * to prevent another thread from getting here before
4800          * we're done with the remove.  While we have the
4801          * statelock, make local copies of the pertinent rnode
4802          * fields.  If we weren't to do this in an atomic way, the
4803          * the unl* fields could become inconsistent with respect
4804          * to each other due to a race condition between this
4805          * code and nfs_remove().  See bug report 1034328.
4806          */
4807         mutex_enter(&rp->r_statelock);
4808         if (rp->r_unldvp == NULL) {
4809                 mutex_exit(&rp->r_statelock);
4810                 rp4_addfree(rp, cr);
4811                 return;
4812         }
4813 
4814         unldvp = rp->r_unldvp;
4815         rp->r_unldvp = NULL;
4816         unlname = rp->r_unlname;
4817         rp->r_unlname = NULL;
4818         unlcred = rp->r_unlcred;
4819         rp->r_unlcred = NULL;
4820         mutex_exit(&rp->r_statelock);
4821 
4822         /*
4823          * If there are any dirty pages left, then flush
4824          * them.  This is unfortunate because they just
4825          * may get thrown away during the remove operation,
4826          * but we have to do this for correctness.
4827          */
4828         if (nfs4_has_pages(vp) &&
4829             ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4830                 ASSERT(vp->v_type != VCHR);
4831                 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4832                 if (e.error) {
4833                         mutex_enter(&rp->r_statelock);
4834                         if (!rp->r_error)
4835                                 rp->r_error = e.error;
4836                         mutex_exit(&rp->r_statelock);
4837                 }
4838         }
4839 
4840         recov_state.rs_flags = 0;
4841         recov_state.rs_num_retry_despite_err = 0;
4842 recov_retry_remove:
4843         /*
4844          * Do the remove operation on the renamed file
4845          */
4846         args.ctag = TAG_INACTIVE;
4847 
4848         /*
4849          * Remove ops: putfh dir; remove
4850          */
4851         args.array_len = 2;
4852         args.array = argop;
4853 
4854         e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4855         if (e.error) {
4856                 kmem_free(unlname, MAXNAMELEN);
4857                 crfree(unlcred);
4858                 VN_RELE(unldvp);
4859                 /*
4860                  * Try again; this time around r_unldvp will be NULL, so we'll
4861                  * just call rp4_addfree() and return.
4862                  */
4863                 goto redo;
4864         }
4865 
4866         /* putfh directory */
4867         argop[0].argop = OP_CPUTFH;
4868         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4869 
4870         /* remove */
4871         argop[1].argop = OP_CREMOVE;
4872         argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4873 
4874         doqueue = 1;
4875         resp = &res;
4876 
4877 #if 0 /* notyet */
4878         /*
4879          * Can't do this yet.  We may be being called from
4880          * dnlc_purge_XXX while that routine is holding a
4881          * mutex lock to the nc_rele list.  The calls to
4882          * nfs3_cache_wcc_data may result in calls to
4883          * dnlc_purge_XXX.  This will result in a deadlock.
4884          */
4885         rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4886         if (e.error) {
4887                 PURGE_ATTRCACHE4(unldvp);
4888                 resp = NULL;
4889         } else if (res.status) {
4890                 e.error = geterrno4(res.status);
4891                 PURGE_ATTRCACHE4(unldvp);
4892                 /*
4893                  * This code is inactive right now
4894                  * but if made active there should
4895                  * be a nfs4_end_op() call before
4896                  * nfs4_purge_stale_fh to avoid start_op()
4897                  * deadlock. See BugId: 4948726
4898                  */
4899                 nfs4_purge_stale_fh(error, unldvp, cr);
4900         } else {
4901                 nfs_resop4 *resop;
4902                 REMOVE4res *rm_res;
4903 
4904                 resop = &res.array[1];
4905                 rm_res = &resop->nfs_resop4_u.opremove;
4906                 /*
4907                  * Update directory cache attribute,
4908                  * readdir and dnlc caches.
4909                  */
4910                 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4911         }
4912 #else
4913         rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4914 
4915         PURGE_ATTRCACHE4(unldvp);
4916 #endif
4917 
4918         if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4919                 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4920                     NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4921                         if (!e.error)
4922                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
4923                                     (caddr_t)&res);
4924                         nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4925                             &recov_state, TRUE);
4926                         goto recov_retry_remove;
4927                 }
4928         }
4929         nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4930 
4931         /*
4932          * Release stuff held for the remove
4933          */
4934         VN_RELE(unldvp);
4935         if (!e.error && resp)
4936                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4937 
4938         kmem_free(unlname, MAXNAMELEN);
4939         crfree(unlcred);
4940         goto redo;
4941 }
4942 
4943 /*
4944  * Remote file system operations having to do with directory manipulation.
4945  */
4946 /* ARGSUSED3 */
4947 int
4948 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4949     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4950     int *direntflags, pathname_t *realpnp)
4951 {
4952         int error;
4953         vnode_t *vp, *avp = NULL;
4954         rnode4_t *drp;
4955 
4956         *vpp = NULL;
4957         if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4958                 return (EPERM);
4959         /*
4960          * if LOOKUP_XATTR, must replace dvp (object) with
4961          * object's attrdir before continuing with lookup
4962          */
4963         if (flags & LOOKUP_XATTR) {
4964                 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4965                 if (error)
4966                         return (error);
4967 
4968                 dvp = avp;
4969 
4970                 /*
4971                  * If lookup is for "", just return dvp now.  The attrdir
4972                  * has already been activated (from nfs4lookup_xattr), and
4973                  * the caller will RELE the original dvp -- not
4974                  * the attrdir.  So, set vpp and return.
4975                  * Currently, when the LOOKUP_XATTR flag is
4976                  * passed to VOP_LOOKUP, the name is always empty, and
4977                  * shortcircuiting here avoids 3 unneeded lock/unlock
4978                  * pairs.
4979                  *
4980                  * If a non-empty name was provided, then it is the
4981                  * attribute name, and it will be looked up below.
4982                  */
4983                 if (*nm == '\0') {
4984                         *vpp = dvp;
4985                         return (0);
4986                 }
4987 
4988                 /*
4989                  * The vfs layer never sends a name when asking for the
4990                  * attrdir, so we should never get here (unless of course
4991                  * name is passed at some time in future -- at which time
4992                  * we'll blow up here).
4993                  */
4994                 ASSERT(0);
4995         }
4996 
4997         drp = VTOR4(dvp);
4998         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4999                 return (EINTR);
5000 
5001         error = nfs4lookup(dvp, nm, vpp, cr, 0);
5002         nfs_rw_exit(&drp->r_rwlock);
5003 
5004         /*
5005          * If vnode is a device, create special vnode.
5006          */
5007         if (!error && ISVDEV((*vpp)->v_type)) {
5008                 vp = *vpp;
5009                 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5010                 VN_RELE(vp);
5011         }
5012 
5013         return (error);
5014 }
5015 
5016 /* ARGSUSED */
5017 static int
5018 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5019 {
5020         int error;
5021         rnode4_t *drp;
5022         int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5023         mntinfo4_t *mi;
5024 
5025         mi = VTOMI4(dvp);
5026         if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5027             !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5028                 return (EINVAL);
5029 
5030         drp = VTOR4(dvp);
5031         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5032                 return (EINTR);
5033 
5034         mutex_enter(&drp->r_statelock);
5035         /*
5036          * If the server doesn't support xattrs just return EINVAL
5037          */
5038         if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5039                 mutex_exit(&drp->r_statelock);
5040                 nfs_rw_exit(&drp->r_rwlock);
5041                 return (EINVAL);
5042         }
5043 
5044         /*
5045          * If there is a cached xattr directory entry,
5046          * use it as long as the attributes are valid. If the
5047          * attributes are not valid, take the simple approach and
5048          * free the cached value and re-fetch a new value.
5049          *
5050          * We don't negative entry cache for now, if we did we
5051          * would need to check if the file has changed on every
5052          * lookup. But xattrs don't exist very often and failing
5053          * an openattr is not much more expensive than and NVERIFY or GETATTR
5054          * so do an openattr over the wire for now.
5055          */
5056         if (drp->r_xattr_dir != NULL) {
5057                 if (ATTRCACHE4_VALID(dvp)) {
5058                         VN_HOLD(drp->r_xattr_dir);
5059                         *vpp = drp->r_xattr_dir;
5060                         mutex_exit(&drp->r_statelock);
5061                         nfs_rw_exit(&drp->r_rwlock);
5062                         return (0);
5063                 }
5064                 VN_RELE(drp->r_xattr_dir);
5065                 drp->r_xattr_dir = NULL;
5066         }
5067         mutex_exit(&drp->r_statelock);
5068 
5069         error = nfs4openattr(dvp, vpp, cflag, cr);
5070 
5071         nfs_rw_exit(&drp->r_rwlock);
5072 
5073         return (error);
5074 }
5075 
5076 static int
5077 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5078 {
5079         int error;
5080         rnode4_t *drp;
5081 
5082         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5083 
5084         /*
5085          * If lookup is for "", just return dvp.  Don't need
5086          * to send it over the wire, look it up in the dnlc,
5087          * or perform any access checks.
5088          */
5089         if (*nm == '\0') {
5090                 VN_HOLD(dvp);
5091                 *vpp = dvp;
5092                 return (0);
5093         }
5094 
5095         /*
5096          * Can't do lookups in non-directories.
5097          */
5098         if (dvp->v_type != VDIR)
5099                 return (ENOTDIR);
5100 
5101         /*
5102          * If lookup is for ".", just return dvp.  Don't need
5103          * to send it over the wire or look it up in the dnlc,
5104          * just need to check access.
5105          */
5106         if (nm[0] == '.' && nm[1] == '\0') {
5107                 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5108                 if (error)
5109                         return (error);
5110                 VN_HOLD(dvp);
5111                 *vpp = dvp;
5112                 return (0);
5113         }
5114 
5115         drp = VTOR4(dvp);
5116         if (!(drp->r_flags & R4LOOKUP)) {
5117                 mutex_enter(&drp->r_statelock);
5118                 drp->r_flags |= R4LOOKUP;
5119                 mutex_exit(&drp->r_statelock);
5120         }
5121 
5122         *vpp = NULL;
5123         /*
5124          * Lookup this name in the DNLC.  If there is no entry
5125          * lookup over the wire.
5126          */
5127         if (!skipdnlc)
5128                 *vpp = dnlc_lookup(dvp, nm);
5129         if (*vpp == NULL) {
5130                 /*
5131                  * We need to go over the wire to lookup the name.
5132                  */
5133                 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5134         }
5135 
5136         /*
5137          * We hit on the dnlc
5138          */
5139         if (*vpp != DNLC_NO_VNODE ||
5140             (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5141                 /*
5142                  * But our attrs may not be valid.
5143                  */
5144                 if (ATTRCACHE4_VALID(dvp)) {
5145                         error = nfs4_waitfor_purge_complete(dvp);
5146                         if (error) {
5147                                 VN_RELE(*vpp);
5148                                 *vpp = NULL;
5149                                 return (error);
5150                         }
5151 
5152                         /*
5153                          * If after the purge completes, check to make sure
5154                          * our attrs are still valid.
5155                          */
5156                         if (ATTRCACHE4_VALID(dvp)) {
5157                                 /*
5158                                  * If we waited for a purge we may have
5159                                  * lost our vnode so look it up again.
5160                                  */
5161                                 VN_RELE(*vpp);
5162                                 *vpp = dnlc_lookup(dvp, nm);
5163                                 if (*vpp == NULL)
5164                                         return (nfs4lookupnew_otw(dvp,
5165                                             nm, vpp, cr));
5166 
5167                                 /*
5168                                  * The access cache should almost always hit
5169                                  */
5170                                 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5171 
5172                                 if (error) {
5173                                         VN_RELE(*vpp);
5174                                         *vpp = NULL;
5175                                         return (error);
5176                                 }
5177                                 if (*vpp == DNLC_NO_VNODE) {
5178                                         VN_RELE(*vpp);
5179                                         *vpp = NULL;
5180                                         return (ENOENT);
5181                                 }
5182                                 return (0);
5183                         }
5184                 }
5185         }
5186 
5187         ASSERT(*vpp != NULL);
5188 
5189         /*
5190          * We may have gotten here we have one of the following cases:
5191          *      1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5192          *              need to validate them.
5193          *      2) vpp == DNLC_NO_VNODE, a negative entry that we always
5194          *              must validate.
5195          *
5196          * Go to the server and check if the directory has changed, if
5197          * it hasn't we are done and can use the dnlc entry.
5198          */
5199         return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5200 }
5201 
5202 /*
5203  * Go to the server and check if the directory has changed, if
5204  * it hasn't we are done and can use the dnlc entry.  If it
5205  * has changed we get a new copy of its attributes and check
5206  * the access for VEXEC, then relookup the filename and
5207  * get its filehandle and attributes.
5208  *
5209  * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5210  *      if the NVERIFY failed we must
5211  *              purge the caches
5212  *              cache new attributes (will set r_time_attr_inval)
5213  *              cache new access
5214  *              recheck VEXEC access
5215  *              add name to dnlc, possibly negative
5216  *              if LOOKUP succeeded
5217  *                      cache new attributes
5218  *      else
5219  *              set a new r_time_attr_inval for dvp
5220  *              check to make sure we have access
5221  *
5222  * The vpp returned is the vnode passed in if the directory is valid,
5223  * a new vnode if successful lookup, or NULL on error.
5224  */
5225 static int
5226 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5227 {
5228         COMPOUND4args_clnt args;
5229         COMPOUND4res_clnt res;
5230         fattr4 *ver_fattr;
5231         fattr4_change dchange;
5232         int32_t *ptr;
5233         int argoplist_size  = 7 * sizeof (nfs_argop4);
5234         nfs_argop4 *argop;
5235         int doqueue;
5236         mntinfo4_t *mi;
5237         nfs4_recov_state_t recov_state;
5238         hrtime_t t;
5239         int isdotdot;
5240         vnode_t *nvp;
5241         nfs_fh4 *fhp;
5242         nfs4_sharedfh_t *sfhp;
5243         nfs4_access_type_t cacc;
5244         rnode4_t *nrp;
5245         rnode4_t *drp = VTOR4(dvp);
5246         nfs4_ga_res_t *garp = NULL;
5247         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5248 
5249         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5250         ASSERT(nm != NULL);
5251         ASSERT(nm[0] != '\0');
5252         ASSERT(dvp->v_type == VDIR);
5253         ASSERT(nm[0] != '.' || nm[1] != '\0');
5254         ASSERT(*vpp != NULL);
5255 
5256         if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5257                 isdotdot = 1;
5258                 args.ctag = TAG_LOOKUP_VPARENT;
5259         } else {
5260                 /*
5261                  * If dvp were a stub, it should have triggered and caused
5262                  * a mount for us to get this far.
5263                  */
5264                 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5265 
5266                 isdotdot = 0;
5267                 args.ctag = TAG_LOOKUP_VALID;
5268         }
5269 
5270         mi = VTOMI4(dvp);
5271         recov_state.rs_flags = 0;
5272         recov_state.rs_num_retry_despite_err = 0;
5273 
5274         nvp = NULL;
5275 
5276         /* Save the original mount point security information */
5277         (void) save_mnt_secinfo(mi->mi_curr_serv);
5278 
5279 recov_retry:
5280         e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5281             &recov_state, NULL);
5282         if (e.error) {
5283                 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5284                 VN_RELE(*vpp);
5285                 *vpp = NULL;
5286                 return (e.error);
5287         }
5288 
5289         argop = kmem_alloc(argoplist_size, KM_SLEEP);
5290 
5291         /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5292         args.array_len = 7;
5293         args.array = argop;
5294 
5295         /* 0. putfh file */
5296         argop[0].argop = OP_CPUTFH;
5297         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5298 
5299         /* 1. nverify the change info */
5300         argop[1].argop = OP_NVERIFY;
5301         ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5302         ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5303         ver_fattr->attrlist4 = (char *)&dchange;
5304         ptr = (int32_t *)&dchange;
5305         IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5306         ver_fattr->attrlist4_len = sizeof (fattr4_change);
5307 
5308         /* 2. getattr directory */
5309         argop[2].argop = OP_GETATTR;
5310         argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5311         argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5312 
5313         /* 3. access directory */
5314         argop[3].argop = OP_ACCESS;
5315         argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5316             ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5317 
5318         /* 4. lookup name */
5319         if (isdotdot) {
5320                 argop[4].argop = OP_LOOKUPP;
5321         } else {
5322                 argop[4].argop = OP_CLOOKUP;
5323                 argop[4].nfs_argop4_u.opclookup.cname = nm;
5324         }
5325 
5326         /* 5. resulting file handle */
5327         argop[5].argop = OP_GETFH;
5328 
5329         /* 6. resulting file attributes */
5330         argop[6].argop = OP_GETATTR;
5331         argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5332         argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5333 
5334         doqueue = 1;
5335         t = gethrtime();
5336 
5337         rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5338 
5339         if (!isdotdot && res.status == NFS4ERR_MOVED) {
5340                 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5341                 if (e.error != 0 && *vpp != NULL)
5342                         VN_RELE(*vpp);
5343                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5344                     &recov_state, FALSE);
5345                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5346                 kmem_free(argop, argoplist_size);
5347                 return (e.error);
5348         }
5349 
5350         if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5351                 /*
5352                  * For WRONGSEC of a non-dotdot case, send secinfo directly
5353                  * from this thread, do not go thru the recovery thread since
5354                  * we need the nm information.
5355                  *
5356                  * Not doing dotdot case because there is no specification
5357                  * for (PUTFH, SECINFO "..") yet.
5358                  */
5359                 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5360                         if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5361                                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5362                                     &recov_state, FALSE);
5363                         else
5364                                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5365                                     &recov_state, TRUE);
5366                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5367                         kmem_free(argop, argoplist_size);
5368                         if (!e.error)
5369                                 goto recov_retry;
5370                         (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5371                         VN_RELE(*vpp);
5372                         *vpp = NULL;
5373                         return (e.error);
5374                 }
5375 
5376                 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5377                     OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5378                         nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5379                             &recov_state, TRUE);
5380 
5381                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5382                         kmem_free(argop, argoplist_size);
5383                         goto recov_retry;
5384                 }
5385         }
5386 
5387         nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5388 
5389         if (e.error || res.array_len == 0) {
5390                 /*
5391                  * If e.error isn't set, then reply has no ops (or we couldn't
5392                  * be here).  The only legal way to reply without an op array
5393                  * is via NFS4ERR_MINOR_VERS_MISMATCH.  An ops array should
5394                  * be in the reply for all other status values.
5395                  *
5396                  * For valid replies without an ops array, return ENOTSUP
5397                  * (geterrno4 xlation of VERS_MISMATCH).  For illegal replies,
5398                  * return EIO -- don't trust status.
5399                  */
5400                 if (e.error == 0)
5401                         e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5402                             ENOTSUP : EIO;
5403                 VN_RELE(*vpp);
5404                 *vpp = NULL;
5405                 kmem_free(argop, argoplist_size);
5406                 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5407                 return (e.error);
5408         }
5409 
5410         if (res.status != NFS4ERR_SAME) {
5411                 e.error = geterrno4(res.status);
5412 
5413                 /*
5414                  * The NVERIFY "failed" so the directory has changed
5415                  * First make sure PUTFH succeeded and NVERIFY "failed"
5416                  * cleanly.
5417                  */
5418                 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5419                     (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5420                         nfs4_purge_stale_fh(e.error, dvp, cr);
5421                         VN_RELE(*vpp);
5422                         *vpp = NULL;
5423                         goto exit;
5424                 }
5425 
5426                 /*
5427                  * We know the NVERIFY "failed" so we must:
5428                  *      purge the caches (access and indirectly dnlc if needed)
5429                  */
5430                 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5431 
5432                 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5433                         nfs4_purge_stale_fh(e.error, dvp, cr);
5434                         VN_RELE(*vpp);
5435                         *vpp = NULL;
5436                         goto exit;
5437                 }
5438 
5439                 /*
5440                  * Install new cached attributes for the directory
5441                  */
5442                 nfs4_attr_cache(dvp,
5443                     &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5444                     t, cr, FALSE, NULL);
5445 
5446                 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5447                         nfs4_purge_stale_fh(e.error, dvp, cr);
5448                         VN_RELE(*vpp);
5449                         *vpp = NULL;
5450                         e.error = geterrno4(res.status);
5451                         goto exit;
5452                 }
5453 
5454                 /*
5455                  * Now we know the directory is valid,
5456                  * cache new directory access
5457                  */
5458                 nfs4_access_cache(drp,
5459                     args.array[3].nfs_argop4_u.opaccess.access,
5460                     res.array[3].nfs_resop4_u.opaccess.access, cr);
5461 
5462                 /*
5463                  * recheck VEXEC access
5464                  */
5465                 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5466                 if (cacc != NFS4_ACCESS_ALLOWED) {
5467                         /*
5468                          * Directory permissions might have been revoked
5469                          */
5470                         if (cacc == NFS4_ACCESS_DENIED) {
5471                                 e.error = EACCES;
5472                                 VN_RELE(*vpp);
5473                                 *vpp = NULL;
5474                                 goto exit;
5475                         }
5476 
5477                         /*
5478                          * Somehow we must not have asked for enough
5479                          * so try a singleton ACCESS, should never happen.
5480                          */
5481                         e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5482                         if (e.error) {
5483                                 VN_RELE(*vpp);
5484                                 *vpp = NULL;
5485                                 goto exit;
5486                         }
5487                 }
5488 
5489                 e.error = geterrno4(res.status);
5490                 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5491                         /*
5492                          * The lookup failed, probably no entry
5493                          */
5494                         if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5495                                 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5496                         } else {
5497                                 /*
5498                                  * Might be some other error, so remove
5499                                  * the dnlc entry to make sure we start all
5500                                  * over again, next time.
5501                                  */
5502                                 dnlc_remove(dvp, nm);
5503                         }
5504                         VN_RELE(*vpp);
5505                         *vpp = NULL;
5506                         goto exit;
5507                 }
5508 
5509                 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5510                         /*
5511                          * The file exists but we can't get its fh for
5512                          * some unknown reason.  Remove it from the dnlc
5513                          * and error out to be safe.
5514                          */
5515                         dnlc_remove(dvp, nm);
5516                         VN_RELE(*vpp);
5517                         *vpp = NULL;
5518                         goto exit;
5519                 }
5520                 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5521                 if (fhp->nfs_fh4_len == 0) {
5522                         /*
5523                          * The file exists but a bogus fh
5524                          * some unknown reason.  Remove it from the dnlc
5525                          * and error out to be safe.
5526                          */
5527                         e.error = ENOENT;
5528                         dnlc_remove(dvp, nm);
5529                         VN_RELE(*vpp);
5530                         *vpp = NULL;
5531                         goto exit;
5532                 }
5533                 sfhp = sfh4_get(fhp, mi);
5534 
5535                 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5536                         garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5537 
5538                 /*
5539                  * Make the new rnode
5540                  */
5541                 if (isdotdot) {
5542                         e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5543                         if (e.error) {
5544                                 sfh4_rele(&sfhp);
5545                                 VN_RELE(*vpp);
5546                                 *vpp = NULL;
5547                                 goto exit;
5548                         }
5549                         /*
5550                          * XXX if nfs4_make_dotdot uses an existing rnode
5551                          * XXX it doesn't update the attributes.
5552                          * XXX for now just save them again to save an OTW
5553                          */
5554                         nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5555                 } else {
5556                         nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5557                             dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5558                         /*
5559                          * If v_type == VNON, then garp was NULL because
5560                          * the last op in the compound failed and makenfs4node
5561                          * could not find the vnode for sfhp. It created
5562                          * a new vnode, so we have nothing to purge here.
5563                          */
5564                         if (nvp->v_type == VNON) {
5565                                 vattr_t vattr;
5566 
5567                                 vattr.va_mask = AT_TYPE;
5568                                 /*
5569                                  * N.B. We've already called nfs4_end_fop above.
5570                                  */
5571                                 e.error = nfs4getattr(nvp, &vattr, cr);
5572                                 if (e.error) {
5573                                         sfh4_rele(&sfhp);
5574                                         VN_RELE(*vpp);
5575                                         *vpp = NULL;
5576                                         VN_RELE(nvp);
5577                                         goto exit;
5578                                 }
5579                                 nvp->v_type = vattr.va_type;
5580                         }
5581                 }
5582                 sfh4_rele(&sfhp);
5583 
5584                 nrp = VTOR4(nvp);
5585                 mutex_enter(&nrp->r_statev4_lock);
5586                 if (!nrp->created_v4) {
5587                         mutex_exit(&nrp->r_statev4_lock);
5588                         dnlc_update(dvp, nm, nvp);
5589                 } else
5590                         mutex_exit(&nrp->r_statev4_lock);
5591 
5592                 VN_RELE(*vpp);
5593                 *vpp = nvp;
5594         } else {
5595                 hrtime_t now;
5596                 hrtime_t delta = 0;
5597 
5598                 e.error = 0;
5599 
5600                 /*
5601                  * Because the NVERIFY "succeeded" we know that the
5602                  * directory attributes are still valid
5603                  * so update r_time_attr_inval
5604                  */
5605                 now = gethrtime();
5606                 mutex_enter(&drp->r_statelock);
5607                 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5608                         delta = now - drp->r_time_attr_saved;
5609                         if (delta < mi->mi_acdirmin)
5610                                 delta = mi->mi_acdirmin;
5611                         else if (delta > mi->mi_acdirmax)
5612                                 delta = mi->mi_acdirmax;
5613                 }
5614                 drp->r_time_attr_inval = now + delta;
5615                 mutex_exit(&drp->r_statelock);
5616                 dnlc_update(dvp, nm, *vpp);
5617 
5618                 /*
5619                  * Even though we have a valid directory attr cache
5620                  * and dnlc entry, we may not have access.
5621                  * This should almost always hit the cache.
5622                  */
5623                 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5624                 if (e.error) {
5625                         VN_RELE(*vpp);
5626                         *vpp = NULL;
5627                 }
5628 
5629                 if (*vpp == DNLC_NO_VNODE) {
5630                         VN_RELE(*vpp);
5631                         *vpp = NULL;
5632                         e.error = ENOENT;
5633                 }
5634         }
5635 
5636 exit:
5637         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5638         kmem_free(argop, argoplist_size);
5639         (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5640         return (e.error);
5641 }
5642 
5643 /*
5644  * We need to go over the wire to lookup the name, but
5645  * while we are there verify the directory has not
5646  * changed but if it has, get new attributes and check access
5647  *
5648  * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5649  *                                      NVERIFY GETATTR ACCESS
5650  *
5651  * With the results:
5652  *      if the NVERIFY failed we must purge the caches, add new attributes,
5653  *              and cache new access.
5654  *      set a new r_time_attr_inval
5655  *      add name to dnlc, possibly negative
5656  *      if LOOKUP succeeded
5657  *              cache new attributes
5658  */
5659 static int
5660 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5661 {
5662         COMPOUND4args_clnt args;
5663         COMPOUND4res_clnt res;
5664         fattr4 *ver_fattr;
5665         fattr4_change dchange;
5666         int32_t *ptr;
5667         nfs4_ga_res_t *garp = NULL;
5668         int argoplist_size  = 9 * sizeof (nfs_argop4);
5669         nfs_argop4 *argop;
5670         int doqueue;
5671         mntinfo4_t *mi;
5672         nfs4_recov_state_t recov_state;
5673         hrtime_t t;
5674         int isdotdot;
5675         vnode_t *nvp;
5676         nfs_fh4 *fhp;
5677         nfs4_sharedfh_t *sfhp;
5678         nfs4_access_type_t cacc;
5679         rnode4_t *nrp;
5680         rnode4_t *drp = VTOR4(dvp);
5681         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5682 
5683         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5684         ASSERT(nm != NULL);
5685         ASSERT(nm[0] != '\0');
5686         ASSERT(dvp->v_type == VDIR);
5687         ASSERT(nm[0] != '.' || nm[1] != '\0');
5688         ASSERT(*vpp == NULL);
5689 
5690         if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5691                 isdotdot = 1;
5692                 args.ctag = TAG_LOOKUP_PARENT;
5693         } else {
5694                 /*
5695                  * If dvp were a stub, it should have triggered and caused
5696                  * a mount for us to get this far.
5697                  */
5698                 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5699 
5700                 isdotdot = 0;
5701                 args.ctag = TAG_LOOKUP;
5702         }
5703 
5704         mi = VTOMI4(dvp);
5705         recov_state.rs_flags = 0;
5706         recov_state.rs_num_retry_despite_err = 0;
5707 
5708         nvp = NULL;
5709 
5710         /* Save the original mount point security information */
5711         (void) save_mnt_secinfo(mi->mi_curr_serv);
5712 
5713 recov_retry:
5714         e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5715             &recov_state, NULL);
5716         if (e.error) {
5717                 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5718                 return (e.error);
5719         }
5720 
5721         argop = kmem_alloc(argoplist_size, KM_SLEEP);
5722 
5723         /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5724         args.array_len = 9;
5725         args.array = argop;
5726 
5727         /* 0. putfh file */
5728         argop[0].argop = OP_CPUTFH;
5729         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5730 
5731         /* 1. savefh for the nverify */
5732         argop[1].argop = OP_SAVEFH;
5733 
5734         /* 2. lookup name */
5735         if (isdotdot) {
5736                 argop[2].argop = OP_LOOKUPP;
5737         } else {
5738                 argop[2].argop = OP_CLOOKUP;
5739                 argop[2].nfs_argop4_u.opclookup.cname = nm;
5740         }
5741 
5742         /* 3. resulting file handle */
5743         argop[3].argop = OP_GETFH;
5744 
5745         /* 4. resulting file attributes */
5746         argop[4].argop = OP_GETATTR;
5747         argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5748         argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5749 
5750         /* 5. restorefh back the directory for the nverify */
5751         argop[5].argop = OP_RESTOREFH;
5752 
5753         /* 6. nverify the change info */
5754         argop[6].argop = OP_NVERIFY;
5755         ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5756         ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5757         ver_fattr->attrlist4 = (char *)&dchange;
5758         ptr = (int32_t *)&dchange;
5759         IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5760         ver_fattr->attrlist4_len = sizeof (fattr4_change);
5761 
5762         /* 7. getattr directory */
5763         argop[7].argop = OP_GETATTR;
5764         argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5765         argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5766 
5767         /* 8. access directory */
5768         argop[8].argop = OP_ACCESS;
5769         argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5770             ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5771 
5772         doqueue = 1;
5773         t = gethrtime();
5774 
5775         rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5776 
5777         if (!isdotdot && res.status == NFS4ERR_MOVED) {
5778                 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5779                 if (e.error != 0 && *vpp != NULL)
5780                         VN_RELE(*vpp);
5781                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5782                     &recov_state, FALSE);
5783                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5784                 kmem_free(argop, argoplist_size);
5785                 return (e.error);
5786         }
5787 
5788         if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5789                 /*
5790                  * For WRONGSEC of a non-dotdot case, send secinfo directly
5791                  * from this thread, do not go thru the recovery thread since
5792                  * we need the nm information.
5793                  *
5794                  * Not doing dotdot case because there is no specification
5795                  * for (PUTFH, SECINFO "..") yet.
5796                  */
5797                 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5798                         if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5799                                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5800                                     &recov_state, FALSE);
5801                         else
5802                                 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5803                                     &recov_state, TRUE);
5804                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5805                         kmem_free(argop, argoplist_size);
5806                         if (!e.error)
5807                                 goto recov_retry;
5808                         (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5809                         return (e.error);
5810                 }
5811 
5812                 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5813                     OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5814                         nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5815                             &recov_state, TRUE);
5816 
5817                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5818                         kmem_free(argop, argoplist_size);
5819                         goto recov_retry;
5820                 }
5821         }
5822 
5823         nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5824 
5825         if (e.error || res.array_len == 0) {
5826                 /*
5827                  * If e.error isn't set, then reply has no ops (or we couldn't
5828                  * be here).  The only legal way to reply without an op array
5829                  * is via NFS4ERR_MINOR_VERS_MISMATCH.  An ops array should
5830                  * be in the reply for all other status values.
5831                  *
5832                  * For valid replies without an ops array, return ENOTSUP
5833                  * (geterrno4 xlation of VERS_MISMATCH).  For illegal replies,
5834                  * return EIO -- don't trust status.
5835                  */
5836                 if (e.error == 0)
5837                         e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5838                             ENOTSUP : EIO;
5839 
5840                 kmem_free(argop, argoplist_size);
5841                 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5842                 return (e.error);
5843         }
5844 
5845         e.error = geterrno4(res.status);
5846 
5847         /*
5848          * The PUTFH and SAVEFH may have failed.
5849          */
5850         if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5851             (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5852                 nfs4_purge_stale_fh(e.error, dvp, cr);
5853                 goto exit;
5854         }
5855 
5856         /*
5857          * Check if the file exists, if it does delay entering
5858          * into the dnlc until after we update the directory
5859          * attributes so we don't cause it to get purged immediately.
5860          */
5861         if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5862                 /*
5863                  * The lookup failed, probably no entry
5864                  */
5865                 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5866                         dnlc_update(dvp, nm, DNLC_NO_VNODE);
5867                 goto exit;
5868         }
5869 
5870         if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5871                 /*
5872                  * The file exists but we can't get its fh for
5873                  * some unknown reason. Error out to be safe.
5874                  */
5875                 goto exit;
5876         }
5877 
5878         fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5879         if (fhp->nfs_fh4_len == 0) {
5880                 /*
5881                  * The file exists but a bogus fh
5882                  * some unknown reason.  Error out to be safe.
5883                  */
5884                 e.error = EIO;
5885                 goto exit;
5886         }
5887         sfhp = sfh4_get(fhp, mi);
5888 
5889         if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5890                 sfh4_rele(&sfhp);
5891                 goto exit;
5892         }
5893         garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5894 
5895         /*
5896          * The RESTOREFH may have failed
5897          */
5898         if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5899                 sfh4_rele(&sfhp);
5900                 e.error = EIO;
5901                 goto exit;
5902         }
5903 
5904         if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5905                 /*
5906                  * First make sure the NVERIFY failed as we expected,
5907                  * if it didn't then be conservative and error out
5908                  * as we can't trust the directory.
5909                  */
5910                 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5911                         sfh4_rele(&sfhp);
5912                         e.error = EIO;
5913                         goto exit;
5914                 }
5915 
5916                 /*
5917                  * We know the NVERIFY "failed" so the directory has changed,
5918                  * so we must:
5919                  *      purge the caches (access and indirectly dnlc if needed)
5920                  */
5921                 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5922 
5923                 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5924                         sfh4_rele(&sfhp);
5925                         goto exit;
5926                 }
5927                 nfs4_attr_cache(dvp,
5928                     &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5929                     t, cr, FALSE, NULL);
5930 
5931                 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5932                         nfs4_purge_stale_fh(e.error, dvp, cr);
5933                         sfh4_rele(&sfhp);
5934                         e.error = geterrno4(res.status);
5935                         goto exit;
5936                 }
5937 
5938                 /*
5939                  * Now we know the directory is valid,
5940                  * cache new directory access
5941                  */
5942                 nfs4_access_cache(drp,
5943                     args.array[8].nfs_argop4_u.opaccess.access,
5944                     res.array[8].nfs_resop4_u.opaccess.access, cr);
5945 
5946                 /*
5947                  * recheck VEXEC access
5948                  */
5949                 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5950                 if (cacc != NFS4_ACCESS_ALLOWED) {
5951                         /*
5952                          * Directory permissions might have been revoked
5953                          */
5954                         if (cacc == NFS4_ACCESS_DENIED) {
5955                                 sfh4_rele(&sfhp);
5956                                 e.error = EACCES;
5957                                 goto exit;
5958                         }
5959 
5960                         /*
5961                          * Somehow we must not have asked for enough
5962                          * so try a singleton ACCESS should never happen
5963                          */
5964                         e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5965                         if (e.error) {
5966                                 sfh4_rele(&sfhp);
5967                                 goto exit;
5968                         }
5969                 }
5970 
5971                 e.error = geterrno4(res.status);
5972         } else {
5973                 hrtime_t now;
5974                 hrtime_t delta = 0;
5975 
5976                 e.error = 0;
5977 
5978                 /*
5979                  * Because the NVERIFY "succeeded" we know that the
5980                  * directory attributes are still valid
5981                  * so update r_time_attr_inval
5982                  */
5983                 now = gethrtime();
5984                 mutex_enter(&drp->r_statelock);
5985                 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5986                         delta = now - drp->r_time_attr_saved;
5987                         if (delta < mi->mi_acdirmin)
5988                                 delta = mi->mi_acdirmin;
5989                         else if (delta > mi->mi_acdirmax)
5990                                 delta = mi->mi_acdirmax;
5991                 }
5992                 drp->r_time_attr_inval = now + delta;
5993                 mutex_exit(&drp->r_statelock);
5994 
5995                 /*
5996                  * Even though we have a valid directory attr cache,
5997                  * we may not have access.
5998                  * This should almost always hit the cache.
5999                  */
6000                 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
6001                 if (e.error) {
6002                         sfh4_rele(&sfhp);
6003                         goto exit;
6004                 }
6005         }
6006 
6007         /*
6008          * Now we have successfully completed the lookup, if the
6009          * directory has changed we now have the valid attributes.
6010          * We also know we have directory access.
6011          * Create the new rnode and insert it in the dnlc.
6012          */
6013         if (isdotdot) {
6014                 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6015                 if (e.error) {
6016                         sfh4_rele(&sfhp);
6017                         goto exit;
6018                 }
6019                 /*
6020                  * XXX if nfs4_make_dotdot uses an existing rnode
6021                  * XXX it doesn't update the attributes.
6022                  * XXX for now just save them again to save an OTW
6023                  */
6024                 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6025         } else {
6026                 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6027                     dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6028         }
6029         sfh4_rele(&sfhp);
6030 
6031         nrp = VTOR4(nvp);
6032         mutex_enter(&nrp->r_statev4_lock);
6033         if (!nrp->created_v4) {
6034                 mutex_exit(&nrp->r_statev4_lock);
6035                 dnlc_update(dvp, nm, nvp);
6036         } else
6037                 mutex_exit(&nrp->r_statev4_lock);
6038 
6039         *vpp = nvp;
6040 
6041 exit:
6042         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6043         kmem_free(argop, argoplist_size);
6044         (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6045         return (e.error);
6046 }
6047 
6048 #ifdef DEBUG
6049 void
6050 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6051 {
6052         uint_t i, len;
6053         zoneid_t zoneid = getzoneid();
6054         char *s;
6055 
6056         zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6057         for (i = 0; i < argcnt; i++) {
6058                 nfs_argop4 *op = &argbase[i];
6059                 switch (op->argop) {
6060                 case OP_CPUTFH:
6061                 case OP_PUTFH:
6062                         zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6063                         break;
6064                 case OP_PUTROOTFH:
6065                         zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6066                         break;
6067                 case OP_CLOOKUP:
6068                         s = op->nfs_argop4_u.opclookup.cname;
6069                         zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6070                         break;
6071                 case OP_LOOKUP:
6072                         s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6073                             &len, NULL);
6074                         zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6075                         kmem_free(s, len);
6076                         break;
6077                 case OP_LOOKUPP:
6078                         zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6079                         break;
6080                 case OP_GETFH:
6081                         zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6082                         break;
6083                 case OP_GETATTR:
6084                         zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6085                         break;
6086                 case OP_OPENATTR:
6087                         zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6088                         break;
6089                 default:
6090                         zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6091                             op->argop);
6092                         break;
6093                 }
6094         }
6095 }
6096 #endif
6097 
6098 /*
6099  * nfs4lookup_setup - constructs a multi-lookup compound request.
6100  *
6101  * Given the path "nm1/nm2/.../nmn", the following compound requests
6102  * may be created:
6103  *
6104  * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6105  * is faster, for now.
6106  *
6107  * l4_getattrs indicates the type of compound requested.
6108  *
6109  * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6110  *
6111  *      compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ...  Lookup {nmn} }
6112  *
6113  *   total number of ops is n + 1.
6114  *
6115  * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6116  *      attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6117  *      before the last component, and only get attributes
6118  *      for the last component.  Note that the second-to-last
6119  *      pathname component is XATTR_RPATH, which does NOT go
6120  *      over-the-wire as a lookup.
6121  *
6122  *      compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6123  *              Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6124  *
6125  *   and total number of ops is n + 5.
6126  *
6127  * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6128  *      attribute directory: create lookups plus an OPENATTR
6129  *      replacing the last lookup.  Note that the last pathname
6130  *      component is XATTR_RPATH, which does NOT go over-the-wire
6131  *      as a lookup.
6132  *
6133  *      compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6134  *              Openattr; Getfh; Getattr }
6135  *
6136  *   and total number of ops is n + 5.
6137  *
6138  * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6139  *      nodes too.
6140  *
6141  *      compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6142  *              Lookup {nm2}; ...  Lookup {nmn}; Getfh; Getattr }
6143  *
6144  *   and total number of ops is 3*n + 1.
6145  *
6146  * All cases: returns the index in the arg array of the final LOOKUP op, or
6147  * -1 if no LOOKUPs were used.
6148  */
6149 int
6150 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6151 {
6152         enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6153         nfs_argop4 *argbase, *argop;
6154         int arglen, argcnt;
6155         int n = 1;      /* number of components */
6156         int nga = 1;    /* number of Getattr's in request */
6157         char c = '\0', *s, *p;
6158         int lookup_idx = -1;
6159         int argoplist_size;
6160 
6161         /* set lookuparg response result to 0 */
6162         lookupargp->resp->status = NFS4_OK;
6163 
6164         /* skip leading "/" or "." e.g. ".//./" if there is */
6165         for (; ; nm++) {
6166                 if (*nm != '/' && *nm != '.')
6167                         break;
6168 
6169                 /* ".." is counted as 1 component */
6170                 if (*nm == '.' && *(nm + 1) != '/')
6171                         break;
6172         }
6173 
6174         /*
6175          * Find n = number of components - nm must be null terminated
6176          * Skip "." components.
6177          */
6178         if (*nm != '\0')
6179                 for (n = 1, s = nm; *s != '\0'; s++) {
6180                         if ((*s == '/') && (*(s + 1) != '/') &&
6181                             (*(s + 1) != '\0') &&
6182                             !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6183                             *(s + 2) == '\0')))
6184                                 n++;
6185                 }
6186         else
6187                 n = 0;
6188 
6189         /*
6190          * nga is number of components that need Getfh+Getattr
6191          */
6192         switch (l4_getattrs) {
6193         case LKP4_NO_ATTRIBUTES:
6194                 nga = 0;
6195                 break;
6196         case LKP4_ALL_ATTRIBUTES:
6197                 nga = n;
6198                 /*
6199                  * Always have at least 1 getfh, getattr pair
6200                  */
6201                 if (nga == 0)
6202                         nga++;
6203                 break;
6204         case LKP4_LAST_ATTRDIR:
6205         case LKP4_LAST_NAMED_ATTR:
6206                 nga = n+1;
6207                 break;
6208         }
6209 
6210         /*
6211          * If change to use the filehandle attr instead of getfh
6212          * the following line can be deleted.
6213          */
6214         nga *= 2;
6215 
6216         /*
6217          * calculate number of ops in request as
6218          * header + trailer + lookups + getattrs
6219          */
6220         arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6221 
6222         argoplist_size = arglen * sizeof (nfs_argop4);
6223         argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6224         lookupargp->argsp->array = argop;
6225 
6226         argcnt = lookupargp->header_len;
6227         argop += argcnt;
6228 
6229         /*
6230          * loop and create a lookup op and possibly getattr/getfh for
6231          * each component. Skip "." components.
6232          */
6233         for (s = nm; *s != '\0'; s = p) {
6234                 /*
6235                  * Set up a pathname struct for each component if needed
6236                  */
6237                 while (*s == '/')
6238                         s++;
6239                 if (*s == '\0')
6240                         break;
6241 
6242                 for (p = s; (*p != '/') && (*p != '\0'); p++)
6243                         ;
6244                 c = *p;
6245                 *p = '\0';
6246 
6247                 if (s[0] == '.' && s[1] == '\0') {
6248                         *p = c;
6249                         continue;
6250                 }
6251                 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6252                     strcmp(s, XATTR_RPATH) == 0) {
6253                         /* getfh XXX may not be needed in future */
6254                         argop->argop = OP_GETFH;
6255                         argop++;
6256                         argcnt++;
6257 
6258                         /* getattr */
6259                         argop->argop = OP_GETATTR;
6260                         argop->nfs_argop4_u.opgetattr.attr_request =
6261                             lookupargp->ga_bits;
6262                         argop->nfs_argop4_u.opgetattr.mi =
6263                             lookupargp->mi;
6264                         argop++;
6265                         argcnt++;
6266 
6267                         /* openattr */
6268                         argop->argop = OP_OPENATTR;
6269                 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6270                     strcmp(s, XATTR_RPATH) == 0) {
6271                         /* openattr */
6272                         argop->argop = OP_OPENATTR;
6273                         argop++;
6274                         argcnt++;
6275 
6276                         /* getfh XXX may not be needed in future */
6277                         argop->argop = OP_GETFH;
6278                         argop++;
6279                         argcnt++;
6280 
6281                         /* getattr */
6282                         argop->argop = OP_GETATTR;
6283                         argop->nfs_argop4_u.opgetattr.attr_request =
6284                             lookupargp->ga_bits;
6285                         argop->nfs_argop4_u.opgetattr.mi =
6286                             lookupargp->mi;
6287                         argop++;
6288                         argcnt++;
6289                         *p = c;
6290                         continue;
6291                 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6292                         /* lookupp */
6293                         argop->argop = OP_LOOKUPP;
6294                 } else {
6295                         /* lookup */
6296                         argop->argop = OP_LOOKUP;
6297                         (void) str_to_utf8(s,
6298                             &argop->nfs_argop4_u.oplookup.objname);
6299                 }
6300                 lookup_idx = argcnt;
6301                 argop++;
6302                 argcnt++;
6303 
6304                 *p = c;
6305 
6306                 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6307                         /* getfh XXX may not be needed in future */
6308                         argop->argop = OP_GETFH;
6309                         argop++;
6310                         argcnt++;
6311 
6312                         /* getattr */
6313                         argop->argop = OP_GETATTR;
6314                         argop->nfs_argop4_u.opgetattr.attr_request =
6315                             lookupargp->ga_bits;
6316                         argop->nfs_argop4_u.opgetattr.mi =
6317                             lookupargp->mi;
6318                         argop++;
6319                         argcnt++;
6320                 }
6321         }
6322 
6323         if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6324             ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6325                 if (needgetfh) {
6326                         /* stick in a post-lookup getfh */
6327                         argop->argop = OP_GETFH;
6328                         argcnt++;
6329                         argop++;
6330                 }
6331                 /* post-lookup getattr */
6332                 argop->argop = OP_GETATTR;
6333                 argop->nfs_argop4_u.opgetattr.attr_request =
6334                     lookupargp->ga_bits;
6335                 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6336                 argcnt++;
6337         }
6338         argcnt += lookupargp->trailer_len;   /* actual op count */
6339         lookupargp->argsp->array_len = argcnt;
6340         lookupargp->arglen = arglen;
6341 
6342 #ifdef DEBUG
6343         if (nfs4_client_lookup_debug)
6344                 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6345 #endif
6346 
6347         return (lookup_idx);
6348 }
6349 
6350 static int
6351 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6352 {
6353         COMPOUND4args_clnt      args;
6354         COMPOUND4res_clnt       res;
6355         GETFH4res       *gf_res = NULL;
6356         nfs_argop4      argop[4];
6357         nfs_resop4      *resop = NULL;
6358         nfs4_sharedfh_t *sfhp;
6359         hrtime_t t;
6360         nfs4_error_t    e;
6361 
6362         rnode4_t        *drp;
6363         int             doqueue = 1;
6364         vnode_t         *vp;
6365         int             needrecov = 0;
6366         nfs4_recov_state_t recov_state;
6367 
6368         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6369 
6370         *avp = NULL;
6371         recov_state.rs_flags = 0;
6372         recov_state.rs_num_retry_despite_err = 0;
6373 
6374 recov_retry:
6375         /* COMPOUND: putfh, openattr, getfh, getattr */
6376         args.array_len = 4;
6377         args.array = argop;
6378         args.ctag = TAG_OPENATTR;
6379 
6380         e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6381         if (e.error)
6382                 return (e.error);
6383 
6384         drp = VTOR4(dvp);
6385 
6386         /* putfh */
6387         argop[0].argop = OP_CPUTFH;
6388         argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6389 
6390         /* openattr */
6391         argop[1].argop = OP_OPENATTR;
6392         argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6393 
6394         /* getfh */
6395         argop[2].argop = OP_GETFH;
6396 
6397         /* getattr */
6398         argop[3].argop = OP_GETATTR;
6399         argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6400         argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6401 
6402         NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6403             "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6404             rnode4info(drp)));
6405 
6406         t = gethrtime();
6407 
6408         rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6409 
6410         needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6411         if (needrecov) {
6412                 bool_t abort;
6413 
6414                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6415                     "nfs4openattr: initiating recovery\n"));
6416 
6417                 abort = nfs4_start_recovery(&e,
6418                     VTOMI4(dvp), dvp, NULL, NULL, NULL,
6419                     OP_OPENATTR, NULL, NULL, NULL);
6420                 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6421                 if (!e.error) {
6422                         e.error = geterrno4(res.status);
6423                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6424                 }
6425                 if (abort == FALSE)
6426                         goto recov_retry;
6427                 return (e.error);
6428         }
6429 
6430         if (e.error) {
6431                 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6432                 return (e.error);
6433         }
6434 
6435         if (res.status) {
6436                 /*
6437                  * If OTW errro is NOTSUPP, then it should be
6438                  * translated to EINVAL.  All Solaris file system
6439                  * implementations return EINVAL to the syscall layer
6440                  * when the attrdir cannot be created due to an
6441                  * implementation restriction or noxattr mount option.
6442                  */
6443                 if (res.status == NFS4ERR_NOTSUPP) {
6444                         mutex_enter(&drp->r_statelock);
6445                         if (drp->r_xattr_dir)
6446                                 VN_RELE(drp->r_xattr_dir);
6447                         VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6448                         drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6449                         mutex_exit(&drp->r_statelock);
6450 
6451                         e.error = EINVAL;
6452                 } else {
6453                         e.error = geterrno4(res.status);
6454                 }
6455 
6456                 if (e.error) {
6457                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6458                         nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6459                             needrecov);
6460                         return (e.error);
6461                 }
6462         }
6463 
6464         resop = &res.array[0];  /* putfh res */
6465         ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6466 
6467         resop = &res.array[1];  /* openattr res */
6468         ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6469 
6470         resop = &res.array[2];  /* getfh res */
6471         gf_res = &resop->nfs_resop4_u.opgetfh;
6472         if (gf_res->object.nfs_fh4_len == 0) {
6473                 *avp = NULL;
6474                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6475                 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6476                 return (ENOENT);
6477         }
6478 
6479         sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6480         vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6481             dvp->v_vfsp, t, cr, dvp,
6482             fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6483         sfh4_rele(&sfhp);
6484 
6485         if (e.error)
6486                 PURGE_ATTRCACHE4(vp);
6487 
6488         mutex_enter(&vp->v_lock);
6489         vp->v_flag |= V_XATTRDIR;
6490         mutex_exit(&vp->v_lock);
6491 
6492         *avp = vp;
6493 
6494         mutex_enter(&drp->r_statelock);
6495         if (drp->r_xattr_dir)
6496                 VN_RELE(drp->r_xattr_dir);
6497         VN_HOLD(vp);
6498         drp->r_xattr_dir = vp;
6499 
6500         /*
6501          * Invalidate pathconf4 cache because r_xattr_dir is no longer
6502          * NULL.  xattrs could be created at any time, and we have no
6503          * way to update pc4_xattr_exists in the base object if/when
6504          * it happens.
6505          */
6506         drp->r_pathconf.pc4_xattr_valid = 0;
6507 
6508         mutex_exit(&drp->r_statelock);
6509 
6510         nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6511 
6512         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6513 
6514         return (0);
6515 }
6516 
6517 /* ARGSUSED */
6518 static int
6519 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6520         int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6521         vsecattr_t *vsecp)
6522 {
6523         int error;
6524         vnode_t *vp = NULL;
6525         rnode4_t *rp;
6526         struct vattr vattr;
6527         rnode4_t *drp;
6528         vnode_t *tempvp;
6529         enum createmode4 createmode;
6530         bool_t must_trunc = FALSE;
6531         int     truncating = 0;
6532 
6533         if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6534                 return (EPERM);
6535         if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6536                 return (EINVAL);
6537         }
6538 
6539         /* . and .. have special meaning in the protocol, reject them. */
6540 
6541         if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6542                 return (EISDIR);
6543 
6544         drp = VTOR4(dvp);
6545 
6546         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6547                 return (EINTR);
6548 
6549 top:
6550         /*
6551          * We make a copy of the attributes because the caller does not
6552          * expect us to change what va points to.
6553          */
6554         vattr = *va;
6555 
6556         /*
6557          * If the pathname is "", then dvp is the root vnode of
6558          * a remote file mounted over a local directory.
6559          * All that needs to be done is access
6560          * checking and truncation.  Note that we avoid doing
6561          * open w/ create because the parent directory might
6562          * be in pseudo-fs and the open would fail.
6563          */
6564         if (*nm == '\0') {
6565                 error = 0;
6566                 VN_HOLD(dvp);
6567                 vp = dvp;
6568                 must_trunc = TRUE;
6569         } else {
6570                 /*
6571                  * We need to go over the wire, just to be sure whether the
6572                  * file exists or not.  Using the DNLC can be dangerous in
6573                  * this case when making a decision regarding existence.
6574                  */
6575                 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6576         }
6577 
6578         if (exclusive)
6579                 createmode = EXCLUSIVE4;
6580         else
6581                 createmode = GUARDED4;
6582 
6583         /*
6584          * error would be set if the file does not exist on the
6585          * server, so lets go create it.
6586          */
6587         if (error) {
6588                 goto create_otw;
6589         }
6590 
6591         /*
6592          * File does exist on the server
6593          */
6594         if (exclusive == EXCL)
6595                 error = EEXIST;
6596         else if (vp->v_type == VDIR && (mode & VWRITE))
6597                 error = EISDIR;
6598         else {
6599                 /*
6600                  * If vnode is a device, create special vnode.
6601                  */
6602                 if (ISVDEV(vp->v_type)) {
6603                         tempvp = vp;
6604                         vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6605                         VN_RELE(tempvp);
6606                 }
6607                 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6608                         if ((vattr.va_mask & AT_SIZE) &&
6609                             vp->v_type == VREG) {
6610                                 rp = VTOR4(vp);
6611                                 /*
6612                                  * Check here for large file handled
6613                                  * by LF-unaware process (as
6614                                  * ufs_create() does)
6615                                  */
6616                                 if (!(flags & FOFFMAX)) {
6617                                         mutex_enter(&rp->r_statelock);
6618                                         if (rp->r_size > MAXOFF32_T)
6619                                                 error = EOVERFLOW;
6620                                         mutex_exit(&rp->r_statelock);
6621                                 }
6622 
6623                                 /* if error is set then we need to return */
6624                                 if (error) {
6625                                         nfs_rw_exit(&drp->r_rwlock);
6626                                         VN_RELE(vp);
6627                                         return (error);
6628                                 }
6629 
6630                                 if (must_trunc) {
6631                                         vattr.va_mask = AT_SIZE;
6632                                         error = nfs4setattr(vp, &vattr, 0, cr,
6633                                             NULL);
6634                                 } else {
6635                                 /*
6636                                  * we know we have a regular file that already
6637                                  * exists and we may end up truncating the file
6638                                  * as a result of the open_otw, so flush out
6639                                  * any dirty pages for this file first.
6640                                  */
6641                                         if (nfs4_has_pages(vp) &&
6642                                             ((rp->r_flags & R4DIRTY) ||
6643                                             rp->r_count > 0 ||
6644                                             rp->r_mapcnt > 0)) {
6645                                                 error = nfs4_putpage(vp,
6646                                                     (offset_t)0, 0, 0, cr, ct);
6647                                                 if (error && (error == ENOSPC ||
6648                                                     error == EDQUOT)) {
6649                                                         mutex_enter(
6650                                                             &rp->r_statelock);
6651                                                         if (!rp->r_error)
6652                                                                 rp->r_error =
6653                                                                     error;
6654                                                         mutex_exit(
6655                                                             &rp->r_statelock);
6656                                                 }
6657                                         }
6658                                         vattr.va_mask = (AT_SIZE |
6659                                             AT_TYPE | AT_MODE);
6660                                         vattr.va_type = VREG;
6661                                         createmode = UNCHECKED4;
6662                                         truncating = 1;
6663                                         goto create_otw;
6664                                 }
6665                         }
6666                 }
6667         }
6668         nfs_rw_exit(&drp->r_rwlock);
6669         if (error) {
6670                 VN_RELE(vp);
6671         } else {
6672                 vnode_t *tvp;
6673                 rnode4_t *trp;
6674                 tvp = vp;
6675                 if (vp->v_type == VREG) {
6676                         trp = VTOR4(vp);
6677                         if (IS_SHADOW(vp, trp))
6678                                 tvp = RTOV4(trp);
6679                 }
6680 
6681                 if (must_trunc) {
6682                         /*
6683                          * existing file got truncated, notify.
6684                          */
6685                         vnevent_create(tvp, ct);
6686                 }
6687 
6688                 *vpp = vp;
6689         }
6690         return (error);
6691 
6692 create_otw:
6693         dnlc_remove(dvp, nm);
6694 
6695         ASSERT(vattr.va_mask & AT_TYPE);
6696 
6697         /*
6698          * If not a regular file let nfs4mknod() handle it.
6699          */
6700         if (vattr.va_type != VREG) {
6701                 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6702                 nfs_rw_exit(&drp->r_rwlock);
6703                 return (error);
6704         }
6705 
6706         /*
6707          * It _is_ a regular file.
6708          */
6709         ASSERT(vattr.va_mask & AT_MODE);
6710         if (MANDMODE(vattr.va_mode)) {
6711                 nfs_rw_exit(&drp->r_rwlock);
6712                 return (EACCES);
6713         }
6714 
6715         /*
6716          * If this happens to be a mknod of a regular file, then flags will
6717          * have neither FREAD or FWRITE.  However, we must set at least one
6718          * for the call to nfs4open_otw.  If it's open(O_CREAT) driving
6719          * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6720          * set (based on openmode specified by app).
6721          */
6722         if ((flags & (FREAD|FWRITE)) == 0)
6723                 flags |= (FREAD|FWRITE);
6724 
6725         error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6726 
6727         if (vp != NULL) {
6728                 /* if create was successful, throw away the file's pages */
6729                 if (!error && (vattr.va_mask & AT_SIZE))
6730                         nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6731                             cr);
6732                 /* release the lookup hold */
6733                 VN_RELE(vp);
6734                 vp = NULL;
6735         }
6736 
6737         /*
6738          * validate that we opened a regular file. This handles a misbehaving
6739          * server that returns an incorrect FH.
6740          */
6741         if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6742                 error = EISDIR;
6743                 VN_RELE(*vpp);
6744         }
6745 
6746         /*
6747          * If this is not an exclusive create, then the CREATE
6748          * request will be made with the GUARDED mode set.  This
6749          * means that the server will return EEXIST if the file
6750          * exists.  The file could exist because of a retransmitted
6751          * request.  In this case, we recover by starting over and
6752          * checking to see whether the file exists.  This second
6753          * time through it should and a CREATE request will not be
6754          * sent.
6755          *
6756          * This handles the problem of a dangling CREATE request
6757          * which contains attributes which indicate that the file
6758          * should be truncated.  This retransmitted request could
6759          * possibly truncate valid data in the file if not caught
6760          * by the duplicate request mechanism on the server or if
6761          * not caught by other means.  The scenario is:
6762          *
6763          * Client transmits CREATE request with size = 0
6764          * Client times out, retransmits request.
6765          * Response to the first request arrives from the server
6766          *  and the client proceeds on.
6767          * Client writes data to the file.
6768          * The server now processes retransmitted CREATE request
6769          *  and truncates file.
6770          *
6771          * The use of the GUARDED CREATE request prevents this from
6772          * happening because the retransmitted CREATE would fail
6773          * with EEXIST and would not truncate the file.
6774          */
6775         if (error == EEXIST && exclusive == NONEXCL) {
6776 #ifdef DEBUG
6777                 nfs4_create_misses++;
6778 #endif
6779                 goto top;
6780         }
6781         nfs_rw_exit(&drp->r_rwlock);
6782         if (truncating && !error && *vpp) {
6783                 vnode_t *tvp;
6784                 rnode4_t *trp;
6785                 /*
6786                  * existing file got truncated, notify.
6787                  */
6788                 tvp = *vpp;
6789                 trp = VTOR4(tvp);
6790                 if (IS_SHADOW(tvp, trp))
6791                         tvp = RTOV4(trp);
6792                 vnevent_create(tvp, ct);
6793         }
6794         return (error);
6795 }
6796 
6797 /*
6798  * Create compound (for mkdir, mknod, symlink):
6799  * { Putfh <dfh>; Create; Getfh; Getattr }
6800  * It's okay if setattr failed to set gid - this is not considered
6801  * an error, but purge attrs in that case.
6802  */
6803 static int
6804 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6805     vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6806 {
6807         int need_end_op = FALSE;
6808         COMPOUND4args_clnt args;
6809         COMPOUND4res_clnt res, *resp = NULL;
6810         nfs_argop4 *argop;
6811         nfs_resop4 *resop;
6812         int doqueue;
6813         mntinfo4_t *mi;
6814         rnode4_t *drp = VTOR4(dvp);
6815         change_info4 *cinfo;
6816         GETFH4res *gf_res;
6817         struct vattr vattr;
6818         vnode_t *vp;
6819         fattr4 *crattr;
6820         bool_t needrecov = FALSE;
6821         nfs4_recov_state_t recov_state;
6822         nfs4_sharedfh_t *sfhp = NULL;
6823         hrtime_t t;
6824         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6825         int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6826         dirattr_info_t dinfo, *dinfop;
6827         servinfo4_t *svp;
6828         bitmap4 supp_attrs;
6829 
6830         ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6831             type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6832 
6833         mi = VTOMI4(dvp);
6834 
6835         /*
6836          * Make sure we properly deal with setting the right gid
6837          * on a new directory to reflect the parent's setgid bit
6838          */
6839         setgid_flag = 0;
6840         if (type == NF4DIR) {
6841                 struct vattr dva;
6842 
6843                 va->va_mode &= ~VSGID;
6844                 dva.va_mask = AT_MODE | AT_GID;
6845                 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6846 
6847                         /*
6848                          * If the parent's directory has the setgid bit set
6849                          * _and_ the client was able to get a valid mapping
6850                          * for the parent dir's owner_group, we want to
6851                          * append NVERIFY(owner_group == dva.va_gid) and
6852                          * SETTATTR to the CREATE compound.
6853                          */
6854                         if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6855                                 setgid_flag = 1;
6856                                 va->va_mode |= VSGID;
6857                                 if (dva.va_gid != GID_NOBODY) {
6858                                         va->va_mask |= AT_GID;
6859                                         va->va_gid = dva.va_gid;
6860                                 }
6861                         }
6862                 }
6863         }
6864 
6865         /*
6866          * Create ops:
6867          *      0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6868          *      5:restorefh(dir) 6:getattr(dir)
6869          *
6870          * if (setgid)
6871          *      0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6872          *      4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6873          *      8:nverify 9:setattr
6874          */
6875         if (setgid_flag) {
6876                 numops = 10;
6877                 idx_create = 1;
6878                 idx_fattr = 3;
6879         } else {
6880                 numops = 7;
6881                 idx_create = 2;
6882                 idx_fattr = 4;
6883         }
6884 
6885         ASSERT(nfs_zone() == mi->mi_zone);
6886         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6887                 return (EINTR);
6888         }
6889         recov_state.rs_flags = 0;
6890         recov_state.rs_num_retry_despite_err = 0;
6891 
6892         argoplist_size = numops * sizeof (nfs_argop4);
6893         argop = kmem_alloc(argoplist_size, KM_SLEEP);
6894 
6895 recov_retry:
6896         if (type == NF4LNK)
6897                 args.ctag = TAG_SYMLINK;
6898         else if (type == NF4DIR)
6899                 args.ctag = TAG_MKDIR;
6900         else
6901                 args.ctag = TAG_MKNOD;
6902 
6903         args.array_len = numops;
6904         args.array = argop;
6905 
6906         if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6907                 nfs_rw_exit(&drp->r_rwlock);
6908                 kmem_free(argop, argoplist_size);
6909                 return (e.error);
6910         }
6911         need_end_op = TRUE;
6912 
6913 
6914         /* 0: putfh directory */
6915         argop[0].argop = OP_CPUTFH;
6916         argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6917 
6918         /* 1/2: Create object */
6919         argop[idx_create].argop = OP_CCREATE;
6920         argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6921         argop[idx_create].nfs_argop4_u.opccreate.type = type;
6922         if (type == NF4LNK) {
6923                 /*
6924                  * symlink, treat name as data
6925                  */
6926                 ASSERT(data != NULL);
6927                 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6928                     (char *)data;
6929         }
6930         if (type == NF4BLK || type == NF4CHR) {
6931                 ASSERT(data != NULL);
6932                 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6933                     *((specdata4 *)data);
6934         }
6935 
6936         crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6937 
6938         svp = drp->r_server;
6939         (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6940         supp_attrs = svp->sv_supp_attrs;
6941         nfs_rw_exit(&svp->sv_lock);
6942 
6943         if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6944                 nfs_rw_exit(&drp->r_rwlock);
6945                 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6946                 e.error = EINVAL;
6947                 kmem_free(argop, argoplist_size);
6948                 return (e.error);
6949         }
6950 
6951         /* 2/3: getfh fh of created object */
6952         ASSERT(idx_create + 1 == idx_fattr - 1);
6953         argop[idx_create + 1].argop = OP_GETFH;
6954 
6955         /* 3/4: getattr of new object */
6956         argop[idx_fattr].argop = OP_GETATTR;
6957         argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6958         argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6959 
6960         if (setgid_flag) {
6961                 vattr_t _v;
6962 
6963                 argop[4].argop = OP_SAVEFH;
6964 
6965                 argop[5].argop = OP_CPUTFH;
6966                 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6967 
6968                 argop[6].argop = OP_GETATTR;
6969                 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6970                 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6971 
6972                 argop[7].argop = OP_RESTOREFH;
6973 
6974                 /*
6975                  * nverify
6976                  *
6977                  * XXX - Revisit the last argument to nfs4_end_op()
6978                  *       once 5020486 is fixed.
6979                  */
6980                 _v.va_mask = AT_GID;
6981                 _v.va_gid = va->va_gid;
6982                 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6983                     supp_attrs)) {
6984                         nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6985                         nfs_rw_exit(&drp->r_rwlock);
6986                         nfs4_fattr4_free(crattr);
6987                         kmem_free(argop, argoplist_size);
6988                         return (e.error);
6989                 }
6990 
6991                 /*
6992                  * setattr
6993                  *
6994                  * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6995                  * so no need for stateid or flags. Also we specify NULL
6996                  * rp since we're only interested in setting owner_group
6997                  * attributes.
6998                  */
6999                 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
7000                     &e.error, 0);
7001 
7002                 if (e.error) {
7003                         nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7004                         nfs_rw_exit(&drp->r_rwlock);
7005                         nfs4_fattr4_free(crattr);
7006                         nfs4args_verify_free(&argop[8]);
7007                         kmem_free(argop, argoplist_size);
7008                         return (e.error);
7009                 }
7010         } else {
7011                 argop[1].argop = OP_SAVEFH;
7012 
7013                 argop[5].argop = OP_RESTOREFH;
7014 
7015                 argop[6].argop = OP_GETATTR;
7016                 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7017                 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7018         }
7019 
7020         dnlc_remove(dvp, nm);
7021 
7022         doqueue = 1;
7023         t = gethrtime();
7024         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7025 
7026         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7027         if (e.error) {
7028                 PURGE_ATTRCACHE4(dvp);
7029                 if (!needrecov)
7030                         goto out;
7031         }
7032 
7033         if (needrecov) {
7034                 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7035                     OP_CREATE, NULL, NULL, NULL) == FALSE) {
7036                         nfs4_end_op(mi, dvp, NULL, &recov_state,
7037                             needrecov);
7038                         need_end_op = FALSE;
7039                         nfs4_fattr4_free(crattr);
7040                         if (setgid_flag) {
7041                                 nfs4args_verify_free(&argop[8]);
7042                                 nfs4args_setattr_free(&argop[9]);
7043                         }
7044                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7045                         goto recov_retry;
7046                 }
7047         }
7048 
7049         resp = &res;
7050 
7051         if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7052 
7053                 if (res.status == NFS4ERR_BADOWNER)
7054                         nfs4_log_badowner(mi, OP_CREATE);
7055 
7056                 e.error = geterrno4(res.status);
7057 
7058                 /*
7059                  * This check is left over from when create was implemented
7060                  * using a setattr op (instead of createattrs).  If the
7061                  * putfh/create/getfh failed, the error was returned.  If
7062                  * setattr/getattr failed, we keep going.
7063                  *
7064                  * It might be better to get rid of the GETFH also, and just
7065                  * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7066                  * Then if any of the operations failed, we could return the
7067                  * error now, and remove much of the error code below.
7068                  */
7069                 if (res.array_len <= idx_fattr) {
7070                         /*
7071                          * Either Putfh, Create or Getfh failed.
7072                          */
7073                         PURGE_ATTRCACHE4(dvp);
7074                         /*
7075                          * nfs4_purge_stale_fh() may generate otw calls through
7076                          * nfs4_invalidate_pages. Hence the need to call
7077                          * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7078                          */
7079                         nfs4_end_op(mi, dvp, NULL, &recov_state,
7080                             needrecov);
7081                         need_end_op = FALSE;
7082                         nfs4_purge_stale_fh(e.error, dvp, cr);
7083                         goto out;
7084                 }
7085         }
7086 
7087         resop = &res.array[idx_create];     /* create res */
7088         cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7089 
7090         resop = &res.array[idx_create + 1]; /* getfh res */
7091         gf_res = &resop->nfs_resop4_u.opgetfh;
7092 
7093         sfhp = sfh4_get(&gf_res->object, mi);
7094         if (e.error) {
7095                 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7096                     fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7097                 if (vp->v_type == VNON) {
7098                         vattr.va_mask = AT_TYPE;
7099                         /*
7100                          * Need to call nfs4_end_op before nfs4getattr to avoid
7101                          * potential nfs4_start_op deadlock. See RFE 4777612.
7102                          */
7103                         nfs4_end_op(mi, dvp, NULL, &recov_state,
7104                             needrecov);
7105                         need_end_op = FALSE;
7106                         e.error = nfs4getattr(vp, &vattr, cr);
7107                         if (e.error) {
7108                                 VN_RELE(vp);
7109                                 *vpp = NULL;
7110                                 goto out;
7111                         }
7112                         vp->v_type = vattr.va_type;
7113                 }
7114                 e.error = 0;
7115         } else {
7116                 *vpp = vp = makenfs4node(sfhp,
7117                     &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7118                     dvp->v_vfsp, t, cr,
7119                     dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7120         }
7121 
7122         /*
7123          * If compound succeeded, then update dir attrs
7124          */
7125         if (res.status == NFS4_OK) {
7126                 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7127                 dinfo.di_cred = cr;
7128                 dinfo.di_time_call = t;
7129                 dinfop = &dinfo;
7130         } else
7131                 dinfop = NULL;
7132 
7133         /* Update directory cache attribute, readdir and dnlc caches */
7134         nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7135 
7136 out:
7137         if (sfhp != NULL)
7138                 sfh4_rele(&sfhp);
7139         nfs_rw_exit(&drp->r_rwlock);
7140         nfs4_fattr4_free(crattr);
7141         if (setgid_flag) {
7142                 nfs4args_verify_free(&argop[8]);
7143                 nfs4args_setattr_free(&argop[9]);
7144         }
7145         if (resp)
7146                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7147         if (need_end_op)
7148                 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7149 
7150         kmem_free(argop, argoplist_size);
7151         return (e.error);
7152 }
7153 
7154 /* ARGSUSED */
7155 static int
7156 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7157     int mode, vnode_t **vpp, cred_t *cr)
7158 {
7159         int error;
7160         vnode_t *vp;
7161         nfs_ftype4 type;
7162         specdata4 spec, *specp = NULL;
7163 
7164         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7165 
7166         switch (va->va_type) {
7167         case VCHR:
7168         case VBLK:
7169                 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7170                 spec.specdata1 = getmajor(va->va_rdev);
7171                 spec.specdata2 = getminor(va->va_rdev);
7172                 specp = &spec;
7173                 break;
7174 
7175         case VFIFO:
7176                 type = NF4FIFO;
7177                 break;
7178         case VSOCK:
7179                 type = NF4SOCK;
7180                 break;
7181 
7182         default:
7183                 return (EINVAL);
7184         }
7185 
7186         error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7187         if (error) {
7188                 return (error);
7189         }
7190 
7191         /*
7192          * This might not be needed any more; special case to deal
7193          * with problematic v2/v3 servers.  Since create was unable
7194          * to set group correctly, not sure what hope setattr has.
7195          */
7196         if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7197                 va->va_mask = AT_GID;
7198                 (void) nfs4setattr(vp, va, 0, cr, NULL);
7199         }
7200 
7201         /*
7202          * If vnode is a device create special vnode
7203          */
7204         if (ISVDEV(vp->v_type)) {
7205                 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7206                 VN_RELE(vp);
7207         } else {
7208                 *vpp = vp;
7209         }
7210         return (error);
7211 }
7212 
7213 /*
7214  * Remove requires that the current fh be the target directory.
7215  * After the operation, the current fh is unchanged.
7216  * The compound op structure is:
7217  *      PUTFH(targetdir), REMOVE
7218  *
7219  * Weirdness: if the vnode to be removed is open
7220  * we rename it instead of removing it and nfs_inactive
7221  * will remove the new name.
7222  */
7223 /* ARGSUSED */
7224 static int
7225 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7226 {
7227         COMPOUND4args_clnt args;
7228         COMPOUND4res_clnt res, *resp = NULL;
7229         REMOVE4res *rm_res;
7230         nfs_argop4 argop[3];
7231         nfs_resop4 *resop;
7232         vnode_t *vp;
7233         char *tmpname;
7234         int doqueue;
7235         mntinfo4_t *mi;
7236         rnode4_t *rp;
7237         rnode4_t *drp;
7238         int needrecov = 0;
7239         nfs4_recov_state_t recov_state;
7240         int isopen;
7241         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7242         dirattr_info_t dinfo;
7243 
7244         if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7245                 return (EPERM);
7246         drp = VTOR4(dvp);
7247         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7248                 return (EINTR);
7249 
7250         e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7251         if (e.error) {
7252                 nfs_rw_exit(&drp->r_rwlock);
7253                 return (e.error);
7254         }
7255 
7256         if (vp->v_type == VDIR) {
7257                 VN_RELE(vp);
7258                 nfs_rw_exit(&drp->r_rwlock);
7259                 return (EISDIR);
7260         }
7261 
7262         /*
7263          * First just remove the entry from the name cache, as it
7264          * is most likely the only entry for this vp.
7265          */
7266         dnlc_remove(dvp, nm);
7267 
7268         rp = VTOR4(vp);
7269 
7270         /*
7271          * For regular file types, check to see if the file is open by looking
7272          * at the open streams.
7273          * For all other types, check the reference count on the vnode.  Since
7274          * they are not opened OTW they never have an open stream.
7275          *
7276          * If the file is open, rename it to .nfsXXXX.
7277          */
7278         if (vp->v_type != VREG) {
7279                 /*
7280                  * If the file has a v_count > 1 then there may be more than one
7281                  * entry in the name cache due multiple links or an open file,
7282                  * but we don't have the real reference count so flush all
7283                  * possible entries.
7284                  */
7285                 if (vp->v_count > 1)
7286                         dnlc_purge_vp(vp);
7287 
7288                 /*
7289                  * Now we have the real reference count.
7290                  */
7291                 isopen = vp->v_count > 1;
7292         } else {
7293                 mutex_enter(&rp->r_os_lock);
7294                 isopen = list_head(&rp->r_open_streams) != NULL;
7295                 mutex_exit(&rp->r_os_lock);
7296         }
7297 
7298         mutex_enter(&rp->r_statelock);
7299         if (isopen &&
7300             (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7301                 mutex_exit(&rp->r_statelock);
7302                 tmpname = newname();
7303                 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7304                 if (e.error)
7305                         kmem_free(tmpname, MAXNAMELEN);
7306                 else {
7307                         mutex_enter(&rp->r_statelock);
7308                         if (rp->r_unldvp == NULL) {
7309                                 VN_HOLD(dvp);
7310                                 rp->r_unldvp = dvp;
7311                                 if (rp->r_unlcred != NULL)
7312                                         crfree(rp->r_unlcred);
7313                                 crhold(cr);
7314                                 rp->r_unlcred = cr;
7315                                 rp->r_unlname = tmpname;
7316                         } else {
7317                                 kmem_free(rp->r_unlname, MAXNAMELEN);
7318                                 rp->r_unlname = tmpname;
7319                         }
7320                         mutex_exit(&rp->r_statelock);
7321                 }
7322                 VN_RELE(vp);
7323                 nfs_rw_exit(&drp->r_rwlock);
7324                 return (e.error);
7325         }
7326         /*
7327          * Actually remove the file/dir
7328          */
7329         mutex_exit(&rp->r_statelock);
7330 
7331         /*
7332          * We need to flush any dirty pages which happen to
7333          * be hanging around before removing the file.
7334          * This shouldn't happen very often since in NFSv4
7335          * we should be close to open consistent.
7336          */
7337         if (nfs4_has_pages(vp) &&
7338             ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7339                 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7340                 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7341                         mutex_enter(&rp->r_statelock);
7342                         if (!rp->r_error)
7343                                 rp->r_error = e.error;
7344                         mutex_exit(&rp->r_statelock);
7345                 }
7346         }
7347 
7348         mi = VTOMI4(dvp);
7349 
7350         (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7351         recov_state.rs_flags = 0;
7352         recov_state.rs_num_retry_despite_err = 0;
7353 
7354 recov_retry:
7355         /*
7356          * Remove ops: putfh dir; remove
7357          */
7358         args.ctag = TAG_REMOVE;
7359         args.array_len = 3;
7360         args.array = argop;
7361 
7362         e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7363         if (e.error) {
7364                 nfs_rw_exit(&drp->r_rwlock);
7365                 VN_RELE(vp);
7366                 return (e.error);
7367         }
7368 
7369         /* putfh directory */
7370         argop[0].argop = OP_CPUTFH;
7371         argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7372 
7373         /* remove */
7374         argop[1].argop = OP_CREMOVE;
7375         argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7376 
7377         /* getattr dir */
7378         argop[2].argop = OP_GETATTR;
7379         argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7380         argop[2].nfs_argop4_u.opgetattr.mi = mi;
7381 
7382         doqueue = 1;
7383         dinfo.di_time_call = gethrtime();
7384         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7385 
7386         PURGE_ATTRCACHE4(vp);
7387 
7388         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7389         if (e.error)
7390                 PURGE_ATTRCACHE4(dvp);
7391 
7392         if (needrecov) {
7393                 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7394                     NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7395                         if (!e.error)
7396                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
7397                                     (caddr_t)&res);
7398                         nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7399                             needrecov);
7400                         goto recov_retry;
7401                 }
7402         }
7403 
7404         /*
7405          * Matching nfs4_end_op() for start_op() above.
7406          * There is a path in the code below which calls
7407          * nfs4_purge_stale_fh(), which may generate otw calls through
7408          * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7409          * here to avoid nfs4_start_op() deadlock.
7410          */
7411         nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7412 
7413         if (!e.error) {
7414                 resp = &res;
7415 
7416                 if (res.status) {
7417                         e.error = geterrno4(res.status);
7418                         PURGE_ATTRCACHE4(dvp);
7419                         nfs4_purge_stale_fh(e.error, dvp, cr);
7420                 } else {
7421                         resop = &res.array[1];      /* remove res */
7422                         rm_res = &resop->nfs_resop4_u.opremove;
7423 
7424                         dinfo.di_garp =
7425                             &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7426                         dinfo.di_cred = cr;
7427 
7428                         /* Update directory attr, readdir and dnlc caches */
7429                         nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7430                             &dinfo);
7431                 }
7432         }
7433         nfs_rw_exit(&drp->r_rwlock);
7434         if (resp)
7435                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7436 
7437         if (e.error == 0) {
7438                 vnode_t *tvp;
7439                 rnode4_t *trp;
7440                 trp = VTOR4(vp);
7441                 tvp = vp;
7442                 if (IS_SHADOW(vp, trp))
7443                         tvp = RTOV4(trp);
7444                 vnevent_remove(tvp, dvp, nm, ct);
7445         }
7446         VN_RELE(vp);
7447         return (e.error);
7448 }
7449 
7450 /*
7451  * Link requires that the current fh be the target directory and the
7452  * saved fh be the source fh. After the operation, the current fh is unchanged.
7453  * Thus the compound op structure is:
7454  *      PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7455  *      GETATTR(file)
7456  */
7457 /* ARGSUSED */
7458 static int
7459 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7460     caller_context_t *ct, int flags)
7461 {
7462         COMPOUND4args_clnt args;
7463         COMPOUND4res_clnt res, *resp = NULL;
7464         LINK4res *ln_res;
7465         int argoplist_size  = 7 * sizeof (nfs_argop4);
7466         nfs_argop4 *argop;
7467         nfs_resop4 *resop;
7468         vnode_t *realvp, *nvp;
7469         int doqueue;
7470         mntinfo4_t *mi;
7471         rnode4_t *tdrp;
7472         bool_t needrecov = FALSE;
7473         nfs4_recov_state_t recov_state;
7474         hrtime_t t;
7475         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7476         dirattr_info_t dinfo;
7477 
7478         ASSERT(*tnm != '\0');
7479         ASSERT(tdvp->v_type == VDIR);
7480         ASSERT(nfs4_consistent_type(tdvp));
7481         ASSERT(nfs4_consistent_type(svp));
7482 
7483         if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7484                 return (EPERM);
7485         if (VOP_REALVP(svp, &realvp, ct) == 0) {
7486                 svp = realvp;
7487                 ASSERT(nfs4_consistent_type(svp));
7488         }
7489 
7490         tdrp = VTOR4(tdvp);
7491         mi = VTOMI4(svp);
7492 
7493         if (!(mi->mi_flags & MI4_LINK)) {
7494                 return (EOPNOTSUPP);
7495         }
7496         recov_state.rs_flags = 0;
7497         recov_state.rs_num_retry_despite_err = 0;
7498 
7499         if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7500                 return (EINTR);
7501 
7502 recov_retry:
7503         argop = kmem_alloc(argoplist_size, KM_SLEEP);
7504 
7505         args.ctag = TAG_LINK;
7506 
7507         /*
7508          * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7509          * restorefh; getattr(fl)
7510          */
7511         args.array_len = 7;
7512         args.array = argop;
7513 
7514         e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7515         if (e.error) {
7516                 kmem_free(argop, argoplist_size);
7517                 nfs_rw_exit(&tdrp->r_rwlock);
7518                 return (e.error);
7519         }
7520 
7521         /* 0. putfh file */
7522         argop[0].argop = OP_CPUTFH;
7523         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7524 
7525         /* 1. save current fh to free up the space for the dir */
7526         argop[1].argop = OP_SAVEFH;
7527 
7528         /* 2. putfh targetdir */
7529         argop[2].argop = OP_CPUTFH;
7530         argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7531 
7532         /* 3. link: current_fh is targetdir, saved_fh is source */
7533         argop[3].argop = OP_CLINK;
7534         argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7535 
7536         /* 4. Get attributes of dir */
7537         argop[4].argop = OP_GETATTR;
7538         argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7539         argop[4].nfs_argop4_u.opgetattr.mi = mi;
7540 
7541         /* 5. If link was successful, restore current vp to file */
7542         argop[5].argop = OP_RESTOREFH;
7543 
7544         /* 6. Get attributes of linked object */
7545         argop[6].argop = OP_GETATTR;
7546         argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7547         argop[6].nfs_argop4_u.opgetattr.mi = mi;
7548 
7549         dnlc_remove(tdvp, tnm);
7550 
7551         doqueue = 1;
7552         t = gethrtime();
7553 
7554         rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7555 
7556         needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7557         if (e.error != 0 && !needrecov) {
7558                 PURGE_ATTRCACHE4(tdvp);
7559                 PURGE_ATTRCACHE4(svp);
7560                 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7561                 goto out;
7562         }
7563 
7564         if (needrecov) {
7565                 bool_t abort;
7566 
7567                 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7568                     NULL, NULL, OP_LINK, NULL, NULL, NULL);
7569                 if (abort == FALSE) {
7570                         nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7571                             needrecov);
7572                         kmem_free(argop, argoplist_size);
7573                         if (!e.error)
7574                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
7575                                     (caddr_t)&res);
7576                         goto recov_retry;
7577                 } else {
7578                         if (e.error != 0) {
7579                                 PURGE_ATTRCACHE4(tdvp);
7580                                 PURGE_ATTRCACHE4(svp);
7581                                 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7582                                     &recov_state, needrecov);
7583                                 goto out;
7584                         }
7585                         /* fall through for res.status case */
7586                 }
7587         }
7588 
7589         nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7590 
7591         resp = &res;
7592         if (res.status) {
7593                 /* If link succeeded, then don't return error */
7594                 e.error = geterrno4(res.status);
7595                 if (res.array_len <= 4) {
7596                         /*
7597                          * Either Putfh, Savefh, Putfh dir, or Link failed
7598                          */
7599                         PURGE_ATTRCACHE4(svp);
7600                         PURGE_ATTRCACHE4(tdvp);
7601                         if (e.error == EOPNOTSUPP) {
7602                                 mutex_enter(&mi->mi_lock);
7603                                 mi->mi_flags &= ~MI4_LINK;
7604                                 mutex_exit(&mi->mi_lock);
7605                         }
7606                         /* Remap EISDIR to EPERM for non-root user for SVVS */
7607                         /* XXX-LP */
7608                         if (e.error == EISDIR && crgetuid(cr) != 0)
7609                                 e.error = EPERM;
7610                         goto out;
7611                 }
7612         }
7613 
7614         /* either no error or one of the postop getattr failed */
7615 
7616         /*
7617          * XXX - if LINK succeeded, but no attrs were returned for link
7618          * file, purge its cache.
7619          *
7620          * XXX Perform a simplified version of wcc checking. Instead of
7621          * have another getattr to get pre-op, just purge cache if
7622          * any of the ops prior to and including the getattr failed.
7623          * If the getattr succeeded then update the attrcache accordingly.
7624          */
7625 
7626         /*
7627          * update cache with link file postattrs.
7628          * Note: at this point resop points to link res.
7629          */
7630         resop = &res.array[3];      /* link res */
7631         ln_res = &resop->nfs_resop4_u.oplink;
7632         if (res.status == NFS4_OK)
7633                 e.error = nfs4_update_attrcache(res.status,
7634                     &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7635                     t, svp, cr);
7636 
7637         /*
7638          * Call makenfs4node to create the new shadow vp for tnm.
7639          * We pass NULL attrs because we just cached attrs for
7640          * the src object.  All we're trying to accomplish is to
7641          * to create the new shadow vnode.
7642          */
7643         nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7644             tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7645 
7646         /* Update target cache attribute, readdir and dnlc caches */
7647         dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7648         dinfo.di_time_call = t;
7649         dinfo.di_cred = cr;
7650 
7651         nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7652         ASSERT(nfs4_consistent_type(tdvp));
7653         ASSERT(nfs4_consistent_type(svp));
7654         ASSERT(nfs4_consistent_type(nvp));
7655         VN_RELE(nvp);
7656 
7657         if (!e.error) {
7658                 vnode_t *tvp;
7659                 rnode4_t *trp;
7660                 /*
7661                  * Notify the source file of this link operation.
7662                  */
7663                 trp = VTOR4(svp);
7664                 tvp = svp;
7665                 if (IS_SHADOW(svp, trp))
7666                         tvp = RTOV4(trp);
7667                 vnevent_link(tvp, ct);
7668         }
7669 out:
7670         kmem_free(argop, argoplist_size);
7671         if (resp)
7672                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7673 
7674         nfs_rw_exit(&tdrp->r_rwlock);
7675 
7676         return (e.error);
7677 }
7678 
7679 /* ARGSUSED */
7680 static int
7681 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7682     caller_context_t *ct, int flags)
7683 {
7684         vnode_t *realvp;
7685 
7686         if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7687                 return (EPERM);
7688         if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7689                 ndvp = realvp;
7690 
7691         return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7692 }
7693 
7694 /*
7695  * nfs4rename does the real work of renaming in NFS Version 4.
7696  *
7697  * A file handle is considered volatile for renaming purposes if either
7698  * of the volatile bits are turned on. However, the compound may differ
7699  * based on the likelihood of the filehandle to change during rename.
7700  */
7701 static int
7702 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7703     caller_context_t *ct)
7704 {
7705         int error;
7706         mntinfo4_t *mi;
7707         vnode_t *nvp = NULL;
7708         vnode_t *ovp = NULL;
7709         char *tmpname = NULL;
7710         rnode4_t *rp;
7711         rnode4_t *odrp;
7712         rnode4_t *ndrp;
7713         int did_link = 0;
7714         int do_link = 1;
7715         nfsstat4 stat = NFS4_OK;
7716 
7717         ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7718         ASSERT(nfs4_consistent_type(odvp));
7719         ASSERT(nfs4_consistent_type(ndvp));
7720 
7721         if (onm[0] == '.' && (onm[1] == '\0' ||
7722             (onm[1] == '.' && onm[2] == '\0')))
7723                 return (EINVAL);
7724 
7725         if (nnm[0] == '.' && (nnm[1] == '\0' ||
7726             (nnm[1] == '.' && nnm[2] == '\0')))
7727                 return (EINVAL);
7728 
7729         odrp = VTOR4(odvp);
7730         ndrp = VTOR4(ndvp);
7731         if ((intptr_t)odrp < (intptr_t)ndrp) {
7732                 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7733                         return (EINTR);
7734                 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7735                         nfs_rw_exit(&odrp->r_rwlock);
7736                         return (EINTR);
7737                 }
7738         } else {
7739                 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7740                         return (EINTR);
7741                 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7742                         nfs_rw_exit(&ndrp->r_rwlock);
7743                         return (EINTR);
7744                 }
7745         }
7746 
7747         /*
7748          * Lookup the target file.  If it exists, it needs to be
7749          * checked to see whether it is a mount point and whether
7750          * it is active (open).
7751          */
7752         error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7753         if (!error) {
7754                 int     isactive;
7755 
7756                 ASSERT(nfs4_consistent_type(nvp));
7757                 /*
7758                  * If this file has been mounted on, then just
7759                  * return busy because renaming to it would remove
7760                  * the mounted file system from the name space.
7761                  */
7762                 if (vn_ismntpt(nvp)) {
7763                         VN_RELE(nvp);
7764                         nfs_rw_exit(&odrp->r_rwlock);
7765                         nfs_rw_exit(&ndrp->r_rwlock);
7766                         return (EBUSY);
7767                 }
7768 
7769                 /*
7770                  * First just remove the entry from the name cache, as it
7771                  * is most likely the only entry for this vp.
7772                  */
7773                 dnlc_remove(ndvp, nnm);
7774 
7775                 rp = VTOR4(nvp);
7776 
7777                 if (nvp->v_type != VREG) {
7778                         /*
7779                          * Purge the name cache of all references to this vnode
7780                          * so that we can check the reference count to infer
7781                          * whether it is active or not.
7782                          */
7783                         if (nvp->v_count > 1)
7784                                 dnlc_purge_vp(nvp);
7785 
7786                         isactive = nvp->v_count > 1;
7787                 } else {
7788                         mutex_enter(&rp->r_os_lock);
7789                         isactive = list_head(&rp->r_open_streams) != NULL;
7790                         mutex_exit(&rp->r_os_lock);
7791                 }
7792 
7793                 /*
7794                  * If the vnode is active and is not a directory,
7795                  * arrange to rename it to a
7796                  * temporary file so that it will continue to be
7797                  * accessible.  This implements the "unlink-open-file"
7798                  * semantics for the target of a rename operation.
7799                  * Before doing this though, make sure that the
7800                  * source and target files are not already the same.
7801                  */
7802                 if (isactive && nvp->v_type != VDIR) {
7803                         /*
7804                          * Lookup the source name.
7805                          */
7806                         error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7807 
7808                         /*
7809                          * The source name *should* already exist.
7810                          */
7811                         if (error) {
7812                                 VN_RELE(nvp);
7813                                 nfs_rw_exit(&odrp->r_rwlock);
7814                                 nfs_rw_exit(&ndrp->r_rwlock);
7815                                 return (error);
7816                         }
7817 
7818                         ASSERT(nfs4_consistent_type(ovp));
7819 
7820                         /*
7821                          * Compare the two vnodes.  If they are the same,
7822                          * just release all held vnodes and return success.
7823                          */
7824                         if (VN_CMP(ovp, nvp)) {
7825                                 VN_RELE(ovp);
7826                                 VN_RELE(nvp);
7827                                 nfs_rw_exit(&odrp->r_rwlock);
7828                                 nfs_rw_exit(&ndrp->r_rwlock);
7829                                 return (0);
7830                         }
7831 
7832                         /*
7833                          * Can't mix and match directories and non-
7834                          * directories in rename operations.  We already
7835                          * know that the target is not a directory.  If
7836                          * the source is a directory, return an error.
7837                          */
7838                         if (ovp->v_type == VDIR) {
7839                                 VN_RELE(ovp);
7840                                 VN_RELE(nvp);
7841                                 nfs_rw_exit(&odrp->r_rwlock);
7842                                 nfs_rw_exit(&ndrp->r_rwlock);
7843                                 return (ENOTDIR);
7844                         }
7845 link_call:
7846                         /*
7847                          * The target file exists, is not the same as
7848                          * the source file, and is active.  We first
7849                          * try to Link it to a temporary filename to
7850                          * avoid having the server removing the file
7851                          * completely (which could cause data loss to
7852                          * the user's POV in the event the Rename fails
7853                          * -- see bug 1165874).
7854                          */
7855                         /*
7856                          * The do_link and did_link booleans are
7857                          * introduced in the event we get NFS4ERR_FILE_OPEN
7858                          * returned for the Rename.  Some servers can
7859                          * not Rename over an Open file, so they return
7860                          * this error.  The client needs to Remove the
7861                          * newly created Link and do two Renames, just
7862                          * as if the server didn't support LINK.
7863                          */
7864                         tmpname = newname();
7865                         error = 0;
7866 
7867                         if (do_link) {
7868                                 error = nfs4_link(ndvp, nvp, tmpname, cr,
7869                                     NULL, 0);
7870                         }
7871                         if (error == EOPNOTSUPP || !do_link) {
7872                                 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7873                                     cr, NULL, 0);
7874                                 did_link = 0;
7875                         } else {
7876                                 did_link = 1;
7877                         }
7878                         if (error) {
7879                                 kmem_free(tmpname, MAXNAMELEN);
7880                                 VN_RELE(ovp);
7881                                 VN_RELE(nvp);
7882                                 nfs_rw_exit(&odrp->r_rwlock);
7883                                 nfs_rw_exit(&ndrp->r_rwlock);
7884                                 return (error);
7885                         }
7886 
7887                         mutex_enter(&rp->r_statelock);
7888                         if (rp->r_unldvp == NULL) {
7889                                 VN_HOLD(ndvp);
7890                                 rp->r_unldvp = ndvp;
7891                                 if (rp->r_unlcred != NULL)
7892                                         crfree(rp->r_unlcred);
7893                                 crhold(cr);
7894                                 rp->r_unlcred = cr;
7895                                 rp->r_unlname = tmpname;
7896                         } else {
7897                                 if (rp->r_unlname)
7898                                         kmem_free(rp->r_unlname, MAXNAMELEN);
7899                                 rp->r_unlname = tmpname;
7900                         }
7901                         mutex_exit(&rp->r_statelock);
7902                 }
7903 
7904                 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7905 
7906                 ASSERT(nfs4_consistent_type(nvp));
7907         }
7908 
7909         if (ovp == NULL) {
7910                 /*
7911                  * When renaming directories to be a subdirectory of a
7912                  * different parent, the dnlc entry for ".." will no
7913                  * longer be valid, so it must be removed.
7914                  *
7915                  * We do a lookup here to determine whether we are renaming
7916                  * a directory and we need to check if we are renaming
7917                  * an unlinked file.  This might have already been done
7918                  * in previous code, so we check ovp == NULL to avoid
7919                  * doing it twice.
7920                  */
7921                 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7922                 /*
7923                  * The source name *should* already exist.
7924                  */
7925                 if (error) {
7926                         nfs_rw_exit(&odrp->r_rwlock);
7927                         nfs_rw_exit(&ndrp->r_rwlock);
7928                         if (nvp) {
7929                                 VN_RELE(nvp);
7930                         }
7931                         return (error);
7932                 }
7933                 ASSERT(ovp != NULL);
7934                 ASSERT(nfs4_consistent_type(ovp));
7935         }
7936 
7937         /*
7938          * Is the object being renamed a dir, and if so, is
7939          * it being renamed to a child of itself?  The underlying
7940          * fs should ultimately return EINVAL for this case;
7941          * however, buggy beta non-Solaris NFSv4 servers at
7942          * interop testing events have allowed this behavior,
7943          * and it caused our client to panic due to a recursive
7944          * mutex_enter in fn_move.
7945          *
7946          * The tedious locking in fn_move could be changed to
7947          * deal with this case, and the client could avoid the
7948          * panic; however, the client would just confuse itself
7949          * later and misbehave.  A better way to handle the broken
7950          * server is to detect this condition and return EINVAL
7951          * without ever sending the the bogus rename to the server.
7952          * We know the rename is invalid -- just fail it now.
7953          */
7954         if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7955                 VN_RELE(ovp);
7956                 nfs_rw_exit(&odrp->r_rwlock);
7957                 nfs_rw_exit(&ndrp->r_rwlock);
7958                 if (nvp) {
7959                         VN_RELE(nvp);
7960                 }
7961                 return (EINVAL);
7962         }
7963 
7964         (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7965 
7966         /*
7967          * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7968          * possible for the filehandle to change due to the rename.
7969          * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7970          * the fh will not change because of the rename, but we still need
7971          * to update its rnode entry with the new name for
7972          * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7973          * has no effect on these for now, but for future improvements,
7974          * we might want to use it too to simplify handling of files
7975          * that are open with that flag on. (XXX)
7976          */
7977         mi = VTOMI4(odvp);
7978         if (NFS4_VOLATILE_FH(mi))
7979                 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7980                     &stat);
7981         else
7982                 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7983                     &stat);
7984 
7985         ASSERT(nfs4_consistent_type(odvp));
7986         ASSERT(nfs4_consistent_type(ndvp));
7987         ASSERT(nfs4_consistent_type(ovp));
7988 
7989         if (stat == NFS4ERR_FILE_OPEN && did_link) {
7990                 do_link = 0;
7991                 /*
7992                  * Before the 'link_call' code, we did a nfs4_lookup
7993                  * that puts a VN_HOLD on nvp.  After the nfs4_link
7994                  * call we call VN_RELE to match that hold.  We need
7995                  * to place an additional VN_HOLD here since we will
7996                  * be hitting that VN_RELE again.
7997                  */
7998                 VN_HOLD(nvp);
7999 
8000                 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
8001 
8002                 /* Undo the unlinked file naming stuff we just did */
8003                 mutex_enter(&rp->r_statelock);
8004                 if (rp->r_unldvp) {
8005                         VN_RELE(ndvp);
8006                         rp->r_unldvp = NULL;
8007                         if (rp->r_unlcred != NULL)
8008                                 crfree(rp->r_unlcred);
8009                         rp->r_unlcred = NULL;
8010                         /* rp->r_unlanme points to tmpname */
8011                         if (rp->r_unlname)
8012                                 kmem_free(rp->r_unlname, MAXNAMELEN);
8013                         rp->r_unlname = NULL;
8014                 }
8015                 mutex_exit(&rp->r_statelock);
8016 
8017                 if (nvp) {
8018                         VN_RELE(nvp);
8019                 }
8020                 goto link_call;
8021         }
8022 
8023         if (error) {
8024                 VN_RELE(ovp);
8025                 nfs_rw_exit(&odrp->r_rwlock);
8026                 nfs_rw_exit(&ndrp->r_rwlock);
8027                 if (nvp) {
8028                         VN_RELE(nvp);
8029                 }
8030                 return (error);
8031         }
8032 
8033         /*
8034          * when renaming directories to be a subdirectory of a
8035          * different parent, the dnlc entry for ".." will no
8036          * longer be valid, so it must be removed
8037          */
8038         rp = VTOR4(ovp);
8039         if (ndvp != odvp) {
8040                 if (ovp->v_type == VDIR) {
8041                         dnlc_remove(ovp, "..");
8042                         if (rp->r_dir != NULL)
8043                                 nfs4_purge_rddir_cache(ovp);
8044                 }
8045         }
8046 
8047         /*
8048          * If we are renaming the unlinked file, update the
8049          * r_unldvp and r_unlname as needed.
8050          */
8051         mutex_enter(&rp->r_statelock);
8052         if (rp->r_unldvp != NULL) {
8053                 if (strcmp(rp->r_unlname, onm) == 0) {
8054                         (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8055                         rp->r_unlname[MAXNAMELEN - 1] = '\0';
8056                         if (ndvp != rp->r_unldvp) {
8057                                 VN_RELE(rp->r_unldvp);
8058                                 rp->r_unldvp = ndvp;
8059                                 VN_HOLD(ndvp);
8060                         }
8061                 }
8062         }
8063         mutex_exit(&rp->r_statelock);
8064 
8065         /*
8066          * Notify the rename vnevents to source vnode, and to the target
8067          * vnode if it already existed.
8068          */
8069         if (error == 0) {
8070                 vnode_t *tvp;
8071                 rnode4_t *trp;
8072                 /*
8073                  * Notify the vnode. Each links is represented by
8074                  * a different vnode, in nfsv4.
8075                  */
8076                 if (nvp) {
8077                         trp = VTOR4(nvp);
8078                         tvp = nvp;
8079                         if (IS_SHADOW(nvp, trp))
8080                                 tvp = RTOV4(trp);
8081                         vnevent_rename_dest(tvp, ndvp, nnm, ct);
8082                 }
8083 
8084                 /*
8085                  * if the source and destination directory are not the
8086                  * same notify the destination directory.
8087                  */
8088                 if (VTOR4(odvp) != VTOR4(ndvp)) {
8089                         trp = VTOR4(ndvp);
8090                         tvp = ndvp;
8091                         if (IS_SHADOW(ndvp, trp))
8092                                 tvp = RTOV4(trp);
8093                         vnevent_rename_dest_dir(tvp, ct);
8094                 }
8095 
8096                 trp = VTOR4(ovp);
8097                 tvp = ovp;
8098                 if (IS_SHADOW(ovp, trp))
8099                         tvp = RTOV4(trp);
8100                 vnevent_rename_src(tvp, odvp, onm, ct);
8101         }
8102 
8103         if (nvp) {
8104                 VN_RELE(nvp);
8105         }
8106         VN_RELE(ovp);
8107 
8108         nfs_rw_exit(&odrp->r_rwlock);
8109         nfs_rw_exit(&ndrp->r_rwlock);
8110 
8111         return (error);
8112 }
8113 
8114 /*
8115  * When the parent directory has changed, sv_dfh must be updated
8116  */
8117 static void
8118 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8119 {
8120         svnode_t *sv = VTOSV(vp);
8121         nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8122         nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8123 
8124         sfh4_hold(new_dfh);
8125         sv->sv_dfh = new_dfh;
8126         sfh4_rele(&old_dfh);
8127 }
8128 
8129 /*
8130  * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8131  * when it is known that the filehandle is persistent through rename.
8132  *
8133  * Rename requires that the current fh be the target directory and the
8134  * saved fh be the source directory. After the operation, the current fh
8135  * is unchanged.
8136  * The compound op structure for persistent fh rename is:
8137  *      PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8138  * Rather than bother with the directory postop args, we'll simply
8139  * update that a change occurred in the cache, so no post-op getattrs.
8140  */
8141 static int
8142 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8143     vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8144 {
8145         COMPOUND4args_clnt args;
8146         COMPOUND4res_clnt res, *resp = NULL;
8147         nfs_argop4 *argop;
8148         nfs_resop4 *resop;
8149         int doqueue, argoplist_size;
8150         mntinfo4_t *mi;
8151         rnode4_t *odrp = VTOR4(odvp);
8152         rnode4_t *ndrp = VTOR4(ndvp);
8153         RENAME4res *rn_res;
8154         bool_t needrecov;
8155         nfs4_recov_state_t recov_state;
8156         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8157         dirattr_info_t dinfo, *dinfop;
8158 
8159         ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8160 
8161         recov_state.rs_flags = 0;
8162         recov_state.rs_num_retry_despite_err = 0;
8163 
8164         /*
8165          * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8166          *
8167          * If source/target are different dirs, then append putfh(src); getattr
8168          */
8169         args.array_len = (odvp == ndvp) ? 5 : 7;
8170         argoplist_size = args.array_len * sizeof (nfs_argop4);
8171         args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8172 
8173 recov_retry:
8174         *statp = NFS4_OK;
8175 
8176         /* No need to Lookup the file, persistent fh */
8177         args.ctag = TAG_RENAME;
8178 
8179         mi = VTOMI4(odvp);
8180         e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8181         if (e.error) {
8182                 kmem_free(argop, argoplist_size);
8183                 return (e.error);
8184         }
8185 
8186         /* 0: putfh source directory */
8187         argop[0].argop = OP_CPUTFH;
8188         argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8189 
8190         /* 1: Save source fh to free up current for target */
8191         argop[1].argop = OP_SAVEFH;
8192 
8193         /* 2: putfh targetdir */
8194         argop[2].argop = OP_CPUTFH;
8195         argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8196 
8197         /* 3: current_fh is targetdir, saved_fh is sourcedir */
8198         argop[3].argop = OP_CRENAME;
8199         argop[3].nfs_argop4_u.opcrename.coldname = onm;
8200         argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8201 
8202         /* 4: getattr (targetdir) */
8203         argop[4].argop = OP_GETATTR;
8204         argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8205         argop[4].nfs_argop4_u.opgetattr.mi = mi;
8206 
8207         if (ndvp != odvp) {
8208 
8209                 /* 5: putfh (sourcedir) */
8210                 argop[5].argop = OP_CPUTFH;
8211                 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8212 
8213                 /* 6: getattr (sourcedir) */
8214                 argop[6].argop = OP_GETATTR;
8215                 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8216                 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8217         }
8218 
8219         dnlc_remove(odvp, onm);
8220         dnlc_remove(ndvp, nnm);
8221 
8222         doqueue = 1;
8223         dinfo.di_time_call = gethrtime();
8224         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8225 
8226         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8227         if (e.error) {
8228                 PURGE_ATTRCACHE4(odvp);
8229                 PURGE_ATTRCACHE4(ndvp);
8230         } else {
8231                 *statp = res.status;
8232         }
8233 
8234         if (needrecov) {
8235                 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8236                     OP_RENAME, NULL, NULL, NULL) == FALSE) {
8237                         nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8238                         if (!e.error)
8239                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
8240                                     (caddr_t)&res);
8241                         goto recov_retry;
8242                 }
8243         }
8244 
8245         if (!e.error) {
8246                 resp = &res;
8247                 /*
8248                  * as long as OP_RENAME
8249                  */
8250                 if (res.status != NFS4_OK && res.array_len <= 4) {
8251                         e.error = geterrno4(res.status);
8252                         PURGE_ATTRCACHE4(odvp);
8253                         PURGE_ATTRCACHE4(ndvp);
8254                         /*
8255                          * System V defines rename to return EEXIST, not
8256                          * ENOTEMPTY if the target directory is not empty.
8257                          * Over the wire, the error is NFSERR_ENOTEMPTY
8258                          * which geterrno4 maps to ENOTEMPTY.
8259                          */
8260                         if (e.error == ENOTEMPTY)
8261                                 e.error = EEXIST;
8262                 } else {
8263 
8264                         resop = &res.array[3];      /* rename res */
8265                         rn_res = &resop->nfs_resop4_u.oprename;
8266 
8267                         if (res.status == NFS4_OK) {
8268                                 /*
8269                                  * Update target attribute, readdir and dnlc
8270                                  * caches.
8271                                  */
8272                                 dinfo.di_garp =
8273                                     &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8274                                 dinfo.di_cred = cr;
8275                                 dinfop = &dinfo;
8276                         } else
8277                                 dinfop = NULL;
8278 
8279                         nfs4_update_dircaches(&rn_res->target_cinfo,
8280                             ndvp, NULL, NULL, dinfop);
8281 
8282                         /*
8283                          * Update source attribute, readdir and dnlc caches
8284                          *
8285                          */
8286                         if (ndvp != odvp) {
8287                                 update_parentdir_sfh(renvp, ndvp);
8288 
8289                                 if (dinfop)
8290                                         dinfo.di_garp =
8291                                             &(res.array[6].nfs_resop4_u.
8292                                             opgetattr.ga_res);
8293 
8294                                 nfs4_update_dircaches(&rn_res->source_cinfo,
8295                                     odvp, NULL, NULL, dinfop);
8296                         }
8297 
8298                         fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8299                             nnm);
8300                 }
8301         }
8302 
8303         if (resp)
8304                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8305         nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8306         kmem_free(argop, argoplist_size);
8307 
8308         return (e.error);
8309 }
8310 
8311 /*
8312  * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8313  * it is possible for the filehandle to change due to the rename.
8314  *
8315  * The compound req in this case includes a post-rename lookup and getattr
8316  * to ensure that we have the correct fh and attributes for the object.
8317  *
8318  * Rename requires that the current fh be the target directory and the
8319  * saved fh be the source directory. After the operation, the current fh
8320  * is unchanged.
8321  *
8322  * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8323  * update the filehandle for the renamed object.  We also get the old
8324  * filehandle for historical reasons; this should be taken out sometime.
8325  * This results in a rather cumbersome compound...
8326  *
8327  *    PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8328  *    PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8329  *
8330  */
8331 static int
8332 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8333     vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8334 {
8335         COMPOUND4args_clnt args;
8336         COMPOUND4res_clnt res, *resp = NULL;
8337         int argoplist_size;
8338         nfs_argop4 *argop;
8339         nfs_resop4 *resop;
8340         int doqueue;
8341         mntinfo4_t *mi;
8342         rnode4_t *odrp = VTOR4(odvp);   /* old directory */
8343         rnode4_t *ndrp = VTOR4(ndvp);   /* new directory */
8344         rnode4_t *orp = VTOR4(ovp);     /* object being renamed */
8345         RENAME4res *rn_res;
8346         GETFH4res *ngf_res;
8347         bool_t needrecov;
8348         nfs4_recov_state_t recov_state;
8349         hrtime_t t;
8350         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8351         dirattr_info_t dinfo, *dinfop = &dinfo;
8352 
8353         ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8354 
8355         recov_state.rs_flags = 0;
8356         recov_state.rs_num_retry_despite_err = 0;
8357 
8358 recov_retry:
8359         *statp = NFS4_OK;
8360 
8361         /*
8362          * There is a window between the RPC and updating the path and
8363          * filehandle stored in the rnode.  Lock out the FHEXPIRED recovery
8364          * code, so that it doesn't try to use the old path during that
8365          * window.
8366          */
8367         mutex_enter(&orp->r_statelock);
8368         while (orp->r_flags & R4RECEXPFH) {
8369                 klwp_t *lwp = ttolwp(curthread);
8370 
8371                 if (lwp != NULL)
8372                         lwp->lwp_nostop++;
8373                 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8374                         mutex_exit(&orp->r_statelock);
8375                         if (lwp != NULL)
8376                                 lwp->lwp_nostop--;
8377                         return (EINTR);
8378                 }
8379                 if (lwp != NULL)
8380                         lwp->lwp_nostop--;
8381         }
8382         orp->r_flags |= R4RECEXPFH;
8383         mutex_exit(&orp->r_statelock);
8384 
8385         mi = VTOMI4(odvp);
8386 
8387         args.ctag = TAG_RENAME_VFH;
8388         args.array_len = (odvp == ndvp) ? 10 : 12;
8389         argoplist_size  = args.array_len * sizeof (nfs_argop4);
8390         argop = kmem_alloc(argoplist_size, KM_SLEEP);
8391 
8392         /*
8393          * Rename ops:
8394          *    PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8395          *    PUTFH(targetdir), RENAME, GETATTR(targetdir)
8396          *    LOOKUP(trgt), GETFH(new), GETATTR,
8397          *
8398          *    if (odvp != ndvp)
8399          *      add putfh(sourcedir), getattr(sourcedir) }
8400          */
8401         args.array = argop;
8402 
8403         e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8404             &recov_state, NULL);
8405         if (e.error) {
8406                 kmem_free(argop, argoplist_size);
8407                 mutex_enter(&orp->r_statelock);
8408                 orp->r_flags &= ~R4RECEXPFH;
8409                 cv_broadcast(&orp->r_cv);
8410                 mutex_exit(&orp->r_statelock);
8411                 return (e.error);
8412         }
8413 
8414         /* 0: putfh source directory */
8415         argop[0].argop = OP_CPUTFH;
8416         argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8417 
8418         /* 1: Save source fh to free up current for target */
8419         argop[1].argop = OP_SAVEFH;
8420 
8421         /* 2: Lookup pre-rename fh of renamed object */
8422         argop[2].argop = OP_CLOOKUP;
8423         argop[2].nfs_argop4_u.opclookup.cname = onm;
8424 
8425         /* 3: getfh fh of renamed object (before rename) */
8426         argop[3].argop = OP_GETFH;
8427 
8428         /* 4: putfh targetdir */
8429         argop[4].argop = OP_CPUTFH;
8430         argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8431 
8432         /* 5: current_fh is targetdir, saved_fh is sourcedir */
8433         argop[5].argop = OP_CRENAME;
8434         argop[5].nfs_argop4_u.opcrename.coldname = onm;
8435         argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8436 
8437         /* 6: getattr of target dir (post op attrs) */
8438         argop[6].argop = OP_GETATTR;
8439         argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8440         argop[6].nfs_argop4_u.opgetattr.mi = mi;
8441 
8442         /* 7: Lookup post-rename fh of renamed object */
8443         argop[7].argop = OP_CLOOKUP;
8444         argop[7].nfs_argop4_u.opclookup.cname = nnm;
8445 
8446         /* 8: getfh fh of renamed object (after rename) */
8447         argop[8].argop = OP_GETFH;
8448 
8449         /* 9: getattr of renamed object */
8450         argop[9].argop = OP_GETATTR;
8451         argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8452         argop[9].nfs_argop4_u.opgetattr.mi = mi;
8453 
8454         /*
8455          * If source/target dirs are different, then get new post-op
8456          * attrs for source dir also.
8457          */
8458         if (ndvp != odvp) {
8459                 /* 10: putfh (sourcedir) */
8460                 argop[10].argop = OP_CPUTFH;
8461                 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8462 
8463                 /* 11: getattr (sourcedir) */
8464                 argop[11].argop = OP_GETATTR;
8465                 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8466                 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8467         }
8468 
8469         dnlc_remove(odvp, onm);
8470         dnlc_remove(ndvp, nnm);
8471 
8472         doqueue = 1;
8473         t = gethrtime();
8474         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8475 
8476         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8477         if (e.error) {
8478                 PURGE_ATTRCACHE4(odvp);
8479                 PURGE_ATTRCACHE4(ndvp);
8480                 if (!needrecov) {
8481                         nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8482                             &recov_state, needrecov);
8483                         goto out;
8484                 }
8485         } else {
8486                 *statp = res.status;
8487         }
8488 
8489         if (needrecov) {
8490                 bool_t abort;
8491 
8492                 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8493                     OP_RENAME, NULL, NULL, NULL);
8494                 if (abort == FALSE) {
8495                         nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8496                             &recov_state, needrecov);
8497                         kmem_free(argop, argoplist_size);
8498                         if (!e.error)
8499                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
8500                                     (caddr_t)&res);
8501                         mutex_enter(&orp->r_statelock);
8502                         orp->r_flags &= ~R4RECEXPFH;
8503                         cv_broadcast(&orp->r_cv);
8504                         mutex_exit(&orp->r_statelock);
8505                         goto recov_retry;
8506                 } else {
8507                         if (e.error != 0) {
8508                                 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8509                                     &recov_state, needrecov);
8510                                 goto out;
8511                         }
8512                         /* fall through for res.status case */
8513                 }
8514         }
8515 
8516         resp = &res;
8517         /*
8518          * If OP_RENAME (or any prev op) failed, then return an error.
8519          * OP_RENAME is index 5, so if array len <= 6 we return an error.
8520          */
8521         if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8522                 /*
8523                  * Error in an op other than last Getattr
8524                  */
8525                 e.error = geterrno4(res.status);
8526                 PURGE_ATTRCACHE4(odvp);
8527                 PURGE_ATTRCACHE4(ndvp);
8528                 /*
8529                  * System V defines rename to return EEXIST, not
8530                  * ENOTEMPTY if the target directory is not empty.
8531                  * Over the wire, the error is NFSERR_ENOTEMPTY
8532                  * which geterrno4 maps to ENOTEMPTY.
8533                  */
8534                 if (e.error == ENOTEMPTY)
8535                         e.error = EEXIST;
8536                 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8537                     needrecov);
8538                 goto out;
8539         }
8540 
8541         /* rename results */
8542         rn_res = &res.array[5].nfs_resop4_u.oprename;
8543 
8544         if (res.status == NFS4_OK) {
8545                 /* Update target attribute, readdir and dnlc caches */
8546                 dinfo.di_garp =
8547                     &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8548                 dinfo.di_cred = cr;
8549                 dinfo.di_time_call = t;
8550         } else
8551                 dinfop = NULL;
8552 
8553         /* Update source cache attribute, readdir and dnlc caches */
8554         nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8555 
8556         /* Update source cache attribute, readdir and dnlc caches */
8557         if (ndvp != odvp) {
8558                 update_parentdir_sfh(ovp, ndvp);
8559 
8560                 /*
8561                  * If dinfop is non-NULL, then compound succeded, so
8562                  * set di_garp to attrs for source dir.  dinfop is only
8563                  * set to NULL when compound fails.
8564                  */
8565                 if (dinfop)
8566                         dinfo.di_garp =
8567                             &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8568                 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8569                     dinfop);
8570         }
8571 
8572         /*
8573          * Update the rnode with the new component name and args,
8574          * and if the file handle changed, also update it with the new fh.
8575          * This is only necessary if the target object has an rnode
8576          * entry and there is no need to create one for it.
8577          */
8578         resop = &res.array[8];      /* getfh new res */
8579         ngf_res = &resop->nfs_resop4_u.opgetfh;
8580 
8581         /*
8582          * Update the path and filehandle for the renamed object.
8583          */
8584         nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8585 
8586         nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8587 
8588         if (res.status == NFS4_OK) {
8589                 resop++;        /* getattr res */
8590                 e.error = nfs4_update_attrcache(res.status,
8591                     &resop->nfs_resop4_u.opgetattr.ga_res,
8592                     t, ovp, cr);
8593         }
8594 
8595 out:
8596         kmem_free(argop, argoplist_size);
8597         if (resp)
8598                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8599         mutex_enter(&orp->r_statelock);
8600         orp->r_flags &= ~R4RECEXPFH;
8601         cv_broadcast(&orp->r_cv);
8602         mutex_exit(&orp->r_statelock);
8603 
8604         return (e.error);
8605 }
8606 
8607 /* ARGSUSED */
8608 static int
8609 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8610     caller_context_t *ct, int flags, vsecattr_t *vsecp)
8611 {
8612         int error;
8613         vnode_t *vp;
8614 
8615         if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8616                 return (EPERM);
8617         /*
8618          * As ".." has special meaning and rather than send a mkdir
8619          * over the wire to just let the server freak out, we just
8620          * short circuit it here and return EEXIST
8621          */
8622         if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8623                 return (EEXIST);
8624 
8625         /*
8626          * Decision to get the right gid and setgid bit of the
8627          * new directory is now made in call_nfs4_create_req.
8628          */
8629         va->va_mask |= AT_MODE;
8630         error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8631         if (error)
8632                 return (error);
8633 
8634         *vpp = vp;
8635         return (0);
8636 }
8637 
8638 
8639 /*
8640  * rmdir is using the same remove v4 op as does remove.
8641  * Remove requires that the current fh be the target directory.
8642  * After the operation, the current fh is unchanged.
8643  * The compound op structure is:
8644  *      PUTFH(targetdir), REMOVE
8645  */
8646 /*ARGSUSED4*/
8647 static int
8648 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8649     caller_context_t *ct, int flags)
8650 {
8651         int need_end_op = FALSE;
8652         COMPOUND4args_clnt args;
8653         COMPOUND4res_clnt res, *resp = NULL;
8654         REMOVE4res *rm_res;
8655         nfs_argop4 argop[3];
8656         nfs_resop4 *resop;
8657         vnode_t *vp;
8658         int doqueue;
8659         mntinfo4_t *mi;
8660         rnode4_t *drp;
8661         bool_t needrecov = FALSE;
8662         nfs4_recov_state_t recov_state;
8663         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8664         dirattr_info_t dinfo, *dinfop;
8665 
8666         if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8667                 return (EPERM);
8668         /*
8669          * As ".." has special meaning and rather than send a rmdir
8670          * over the wire to just let the server freak out, we just
8671          * short circuit it here and return EEXIST
8672          */
8673         if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8674                 return (EEXIST);
8675 
8676         drp = VTOR4(dvp);
8677         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8678                 return (EINTR);
8679 
8680         /*
8681          * Attempt to prevent a rmdir(".") from succeeding.
8682          */
8683         e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8684         if (e.error) {
8685                 nfs_rw_exit(&drp->r_rwlock);
8686                 return (e.error);
8687         }
8688         if (vp == cdir) {
8689                 VN_RELE(vp);
8690                 nfs_rw_exit(&drp->r_rwlock);
8691                 return (EINVAL);
8692         }
8693 
8694         /*
8695          * Since nfsv4 remove op works on both files and directories,
8696          * check that the removed object is indeed a directory.
8697          */
8698         if (vp->v_type != VDIR) {
8699                 VN_RELE(vp);
8700                 nfs_rw_exit(&drp->r_rwlock);
8701                 return (ENOTDIR);
8702         }
8703 
8704         /*
8705          * First just remove the entry from the name cache, as it
8706          * is most likely an entry for this vp.
8707          */
8708         dnlc_remove(dvp, nm);
8709 
8710         /*
8711          * If there vnode reference count is greater than one, then
8712          * there may be additional references in the DNLC which will
8713          * need to be purged.  First, trying removing the entry for
8714          * the parent directory and see if that removes the additional
8715          * reference(s).  If that doesn't do it, then use dnlc_purge_vp
8716          * to completely remove any references to the directory which
8717          * might still exist in the DNLC.
8718          */
8719         if (vp->v_count > 1) {
8720                 dnlc_remove(vp, "..");
8721                 if (vp->v_count > 1)
8722                         dnlc_purge_vp(vp);
8723         }
8724 
8725         mi = VTOMI4(dvp);
8726         recov_state.rs_flags = 0;
8727         recov_state.rs_num_retry_despite_err = 0;
8728 
8729 recov_retry:
8730         args.ctag = TAG_RMDIR;
8731 
8732         /*
8733          * Rmdir ops: putfh dir; remove
8734          */
8735         args.array_len = 3;
8736         args.array = argop;
8737 
8738         e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8739         if (e.error) {
8740                 nfs_rw_exit(&drp->r_rwlock);
8741                 return (e.error);
8742         }
8743         need_end_op = TRUE;
8744 
8745         /* putfh directory */
8746         argop[0].argop = OP_CPUTFH;
8747         argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8748 
8749         /* remove */
8750         argop[1].argop = OP_CREMOVE;
8751         argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8752 
8753         /* getattr (postop attrs for dir that contained removed dir) */
8754         argop[2].argop = OP_GETATTR;
8755         argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8756         argop[2].nfs_argop4_u.opgetattr.mi = mi;
8757 
8758         dinfo.di_time_call = gethrtime();
8759         doqueue = 1;
8760         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8761 
8762         PURGE_ATTRCACHE4(vp);
8763 
8764         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8765         if (e.error) {
8766                 PURGE_ATTRCACHE4(dvp);
8767         }
8768 
8769         if (needrecov) {
8770                 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8771                     NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8772                         if (!e.error)
8773                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
8774                                     (caddr_t)&res);
8775 
8776                         nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8777                             needrecov);
8778                         need_end_op = FALSE;
8779                         goto recov_retry;
8780                 }
8781         }
8782 
8783         if (!e.error) {
8784                 resp = &res;
8785 
8786                 /*
8787                  * Only return error if first 2 ops (OP_REMOVE or earlier)
8788                  * failed.
8789                  */
8790                 if (res.status != NFS4_OK && res.array_len <= 2) {
8791                         e.error = geterrno4(res.status);
8792                         PURGE_ATTRCACHE4(dvp);
8793                         nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8794                             &recov_state, needrecov);
8795                         need_end_op = FALSE;
8796                         nfs4_purge_stale_fh(e.error, dvp, cr);
8797                         /*
8798                          * System V defines rmdir to return EEXIST, not
8799                          * ENOTEMPTY if the directory is not empty.  Over
8800                          * the wire, the error is NFSERR_ENOTEMPTY which
8801                          * geterrno4 maps to ENOTEMPTY.
8802                          */
8803                         if (e.error == ENOTEMPTY)
8804                                 e.error = EEXIST;
8805                 } else {
8806                         resop = &res.array[1];      /* remove res */
8807                         rm_res = &resop->nfs_resop4_u.opremove;
8808 
8809                         if (res.status == NFS4_OK) {
8810                                 resop = &res.array[2];      /* dir attrs */
8811                                 dinfo.di_garp =
8812                                     &resop->nfs_resop4_u.opgetattr.ga_res;
8813                                 dinfo.di_cred = cr;
8814                                 dinfop = &dinfo;
8815                         } else
8816                                 dinfop = NULL;
8817 
8818                         /* Update dir attribute, readdir and dnlc caches */
8819                         nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8820                             dinfop);
8821 
8822                         /* destroy rddir cache for dir that was removed */
8823                         if (VTOR4(vp)->r_dir != NULL)
8824                                 nfs4_purge_rddir_cache(vp);
8825                 }
8826         }
8827 
8828         if (need_end_op)
8829                 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8830 
8831         nfs_rw_exit(&drp->r_rwlock);
8832 
8833         if (resp)
8834                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8835 
8836         if (e.error == 0) {
8837                 vnode_t *tvp;
8838                 rnode4_t *trp;
8839                 trp = VTOR4(vp);
8840                 tvp = vp;
8841                 if (IS_SHADOW(vp, trp))
8842                         tvp = RTOV4(trp);
8843                 vnevent_rmdir(tvp, dvp, nm, ct);
8844         }
8845 
8846         VN_RELE(vp);
8847 
8848         return (e.error);
8849 }
8850 
8851 /* ARGSUSED */
8852 static int
8853 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8854     caller_context_t *ct, int flags)
8855 {
8856         int error;
8857         vnode_t *vp;
8858         rnode4_t *rp;
8859         char *contents;
8860         mntinfo4_t *mi = VTOMI4(dvp);
8861 
8862         if (nfs_zone() != mi->mi_zone)
8863                 return (EPERM);
8864         if (!(mi->mi_flags & MI4_SYMLINK))
8865                 return (EOPNOTSUPP);
8866 
8867         error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8868         if (error)
8869                 return (error);
8870 
8871         ASSERT(nfs4_consistent_type(vp));
8872         rp = VTOR4(vp);
8873         if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8874 
8875                 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8876 
8877                 if (contents != NULL) {
8878                         mutex_enter(&rp->r_statelock);
8879                         if (rp->r_symlink.contents == NULL) {
8880                                 rp->r_symlink.len = strlen(tnm);
8881                                 bcopy(tnm, contents, rp->r_symlink.len);
8882                                 rp->r_symlink.contents = contents;
8883                                 rp->r_symlink.size = MAXPATHLEN;
8884                                 mutex_exit(&rp->r_statelock);
8885                         } else {
8886                                 mutex_exit(&rp->r_statelock);
8887                                 kmem_free((void *)contents, MAXPATHLEN);
8888                         }
8889                 }
8890         }
8891         VN_RELE(vp);
8892 
8893         return (error);
8894 }
8895 
8896 
8897 /*
8898  * Read directory entries.
8899  * There are some weird things to look out for here.  The uio_loffset
8900  * field is either 0 or it is the offset returned from a previous
8901  * readdir.  It is an opaque value used by the server to find the
8902  * correct directory block to read. The count field is the number
8903  * of blocks to read on the server.  This is advisory only, the server
8904  * may return only one block's worth of entries.  Entries may be compressed
8905  * on the server.
8906  */
8907 /* ARGSUSED */
8908 static int
8909 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8910         caller_context_t *ct, int flags)
8911 {
8912         int error;
8913         uint_t count;
8914         rnode4_t *rp;
8915         rddir4_cache *rdc;
8916         rddir4_cache *rrdc;
8917 
8918         if (nfs_zone() != VTOMI4(vp)->mi_zone)
8919                 return (EIO);
8920         rp = VTOR4(vp);
8921 
8922         ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8923 
8924         /*
8925          * Make sure that the directory cache is valid.
8926          */
8927         if (rp->r_dir != NULL) {
8928                 if (nfs_disable_rddir_cache != 0) {
8929                         /*
8930                          * Setting nfs_disable_rddir_cache in /etc/system
8931                          * allows interoperability with servers that do not
8932                          * properly update the attributes of directories.
8933                          * Any cached information gets purged before an
8934                          * access is made to it.
8935                          */
8936                         nfs4_purge_rddir_cache(vp);
8937                 }
8938 
8939                 error = nfs4_validate_caches(vp, cr);
8940                 if (error)
8941                         return (error);
8942         }
8943 
8944         count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8945 
8946         /*
8947          * Short circuit last readdir which always returns 0 bytes.
8948          * This can be done after the directory has been read through
8949          * completely at least once.  This will set r_direof which
8950          * can be used to find the value of the last cookie.
8951          */
8952         mutex_enter(&rp->r_statelock);
8953         if (rp->r_direof != NULL &&
8954             uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8955                 mutex_exit(&rp->r_statelock);
8956 #ifdef DEBUG
8957                 nfs4_readdir_cache_shorts++;
8958 #endif
8959                 if (eofp)
8960                         *eofp = 1;
8961                 return (0);
8962         }
8963 
8964         /*
8965          * Look for a cache entry.  Cache entries are identified
8966          * by the NFS cookie value and the byte count requested.
8967          */
8968         rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8969 
8970         /*
8971          * If rdc is NULL then the lookup resulted in an unrecoverable error.
8972          */
8973         if (rdc == NULL) {
8974                 mutex_exit(&rp->r_statelock);
8975                 return (EINTR);
8976         }
8977 
8978         /*
8979          * Check to see if we need to fill this entry in.
8980          */
8981         if (rdc->flags & RDDIRREQ) {
8982                 rdc->flags &= ~RDDIRREQ;
8983                 rdc->flags |= RDDIR;
8984                 mutex_exit(&rp->r_statelock);
8985 
8986                 /*
8987                  * Do the readdir.
8988                  */
8989                 nfs4readdir(vp, rdc, cr);
8990 
8991                 /*
8992                  * Reacquire the lock, so that we can continue
8993                  */
8994                 mutex_enter(&rp->r_statelock);
8995                 /*
8996                  * The entry is now complete
8997                  */
8998                 rdc->flags &= ~RDDIR;
8999         }
9000 
9001         ASSERT(!(rdc->flags & RDDIR));
9002 
9003         /*
9004          * If an error occurred while attempting
9005          * to fill the cache entry, mark the entry invalid and
9006          * just return the error.
9007          */
9008         if (rdc->error) {
9009                 error = rdc->error;
9010                 rdc->flags |= RDDIRREQ;
9011                 rddir4_cache_rele(rp, rdc);
9012                 mutex_exit(&rp->r_statelock);
9013                 return (error);
9014         }
9015 
9016         /*
9017          * The cache entry is complete and good,
9018          * copyout the dirent structs to the calling
9019          * thread.
9020          */
9021         error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9022 
9023         /*
9024          * If no error occurred during the copyout,
9025          * update the offset in the uio struct to
9026          * contain the value of the next NFS 4 cookie
9027          * and set the eof value appropriately.
9028          */
9029         if (!error) {
9030                 uiop->uio_loffset = rdc->nfs4_ncookie;
9031                 if (eofp)
9032                         *eofp = rdc->eof;
9033         }
9034 
9035         /*
9036          * Decide whether to do readahead.  Don't if we
9037          * have already read to the end of directory.
9038          */
9039         if (rdc->eof) {
9040                 /*
9041                  * Make the entry the direof only if it is cached
9042                  */
9043                 if (rdc->flags & RDDIRCACHED)
9044                         rp->r_direof = rdc;
9045                 rddir4_cache_rele(rp, rdc);
9046                 mutex_exit(&rp->r_statelock);
9047                 return (error);
9048         }
9049 
9050         /* Determine if a readdir readahead should be done */
9051         if (!(rp->r_flags & R4LOOKUP)) {
9052                 rddir4_cache_rele(rp, rdc);
9053                 mutex_exit(&rp->r_statelock);
9054                 return (error);
9055         }
9056 
9057         /*
9058          * Now look for a readahead entry.
9059          *
9060          * Check to see whether we found an entry for the readahead.
9061          * If so, we don't need to do anything further, so free the new
9062          * entry if one was allocated.  Otherwise, allocate a new entry, add
9063          * it to the cache, and then initiate an asynchronous readdir
9064          * operation to fill it.
9065          */
9066         rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9067 
9068         /*
9069          * A readdir cache entry could not be obtained for the readahead.  In
9070          * this case we skip the readahead and return.
9071          */
9072         if (rrdc == NULL) {
9073                 rddir4_cache_rele(rp, rdc);
9074                 mutex_exit(&rp->r_statelock);
9075                 return (error);
9076         }
9077 
9078         /*
9079          * Check to see if we need to fill this entry in.
9080          */
9081         if (rrdc->flags & RDDIRREQ) {
9082                 rrdc->flags &= ~RDDIRREQ;
9083                 rrdc->flags |= RDDIR;
9084                 rddir4_cache_rele(rp, rdc);
9085                 mutex_exit(&rp->r_statelock);
9086 #ifdef DEBUG
9087                 nfs4_readdir_readahead++;
9088 #endif
9089                 /*
9090                  * Do the readdir.
9091                  */
9092                 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9093                 return (error);
9094         }
9095 
9096         rddir4_cache_rele(rp, rrdc);
9097         rddir4_cache_rele(rp, rdc);
9098         mutex_exit(&rp->r_statelock);
9099         return (error);
9100 }
9101 
9102 static int
9103 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9104 {
9105         int error;
9106         rnode4_t *rp;
9107 
9108         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9109 
9110         rp = VTOR4(vp);
9111 
9112         /*
9113          * Obtain the readdir results for the caller.
9114          */
9115         nfs4readdir(vp, rdc, cr);
9116 
9117         mutex_enter(&rp->r_statelock);
9118         /*
9119          * The entry is now complete
9120          */
9121         rdc->flags &= ~RDDIR;
9122 
9123         error = rdc->error;
9124         if (error)
9125                 rdc->flags |= RDDIRREQ;
9126         rddir4_cache_rele(rp, rdc);
9127         mutex_exit(&rp->r_statelock);
9128 
9129         return (error);
9130 }
9131 
9132 /*
9133  * Read directory entries.
9134  * There are some weird things to look out for here.  The uio_loffset
9135  * field is either 0 or it is the offset returned from a previous
9136  * readdir.  It is an opaque value used by the server to find the
9137  * correct directory block to read. The count field is the number
9138  * of blocks to read on the server.  This is advisory only, the server
9139  * may return only one block's worth of entries.  Entries may be compressed
9140  * on the server.
9141  *
9142  * Generates the following compound request:
9143  * 1. If readdir offset is zero and no dnlc entry for parent exists,
9144  *    must include a Lookupp as well. In this case, send:
9145  *    { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9146  * 2. Otherwise just do: { Putfh <fh>; Readdir }
9147  *
9148  * Get complete attributes and filehandles for entries if this is the
9149  * first read of the directory. Otherwise, just get fileid's.
9150  */
9151 static void
9152 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9153 {
9154         COMPOUND4args_clnt args;
9155         COMPOUND4res_clnt res;
9156         READDIR4args *rargs;
9157         READDIR4res_clnt *rd_res;
9158         bitmap4 rd_bitsval;
9159         nfs_argop4 argop[5];
9160         nfs_resop4 *resop;
9161         rnode4_t *rp = VTOR4(vp);
9162         mntinfo4_t *mi = VTOMI4(vp);
9163         int doqueue;
9164         u_longlong_t nodeid, pnodeid;   /* id's of dir and its parents */
9165         vnode_t *dvp;
9166         nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9167         int num_ops, res_opcnt;
9168         bool_t needrecov = FALSE;
9169         nfs4_recov_state_t recov_state;
9170         hrtime_t t;
9171         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9172 
9173         ASSERT(nfs_zone() == mi->mi_zone);
9174         ASSERT(rdc->flags & RDDIR);
9175         ASSERT(rdc->entries == NULL);
9176 
9177         /*
9178          * If rp were a stub, it should have triggered and caused
9179          * a mount for us to get this far.
9180          */
9181         ASSERT(!RP_ISSTUB(rp));
9182 
9183         num_ops = 2;
9184         if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9185                 /*
9186                  * Since nfsv4 readdir may not return entries for "." and "..",
9187                  * the client must recreate them:
9188                  * To find the correct nodeid, do the following:
9189                  * For current node, get nodeid from dnlc.
9190                  * - if current node is rootvp, set pnodeid to nodeid.
9191                  * - else if parent is in the dnlc, get its nodeid from there.
9192                  * - else add LOOKUPP+GETATTR to compound.
9193                  */
9194                 nodeid = rp->r_attr.va_nodeid;
9195                 if (vp->v_flag & VROOT) {
9196                         pnodeid = nodeid;       /* root of mount point */
9197                 } else {
9198                         dvp = dnlc_lookup(vp, "..");
9199                         if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9200                                 /* parent in dnlc cache - no need for otw */
9201                                 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9202                         } else {
9203                                 /*
9204                                  * parent not in dnlc cache,
9205                                  * do lookupp to get its id
9206                                  */
9207                                 num_ops = 5;
9208                                 pnodeid = 0; /* set later by getattr parent */
9209                         }
9210                         if (dvp)
9211                                 VN_RELE(dvp);
9212                 }
9213         }
9214         recov_state.rs_flags = 0;
9215         recov_state.rs_num_retry_despite_err = 0;
9216 
9217         /* Save the original mount point security flavor */
9218         (void) save_mnt_secinfo(mi->mi_curr_serv);
9219 
9220 recov_retry:
9221         args.ctag = TAG_READDIR;
9222 
9223         args.array = argop;
9224         args.array_len = num_ops;
9225 
9226         if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9227             &recov_state, NULL)) {
9228                 /*
9229                  * If readdir a node that is a stub for a crossed mount point,
9230                  * keep the original secinfo flavor for the current file
9231                  * system, not the crossed one.
9232                  */
9233                 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9234                 rdc->error = e.error;
9235                 return;
9236         }
9237 
9238         /*
9239          * Determine which attrs to request for dirents.  This code
9240          * must be protected by nfs4_start/end_fop because of r_server
9241          * (which will change during failover recovery).
9242          *
9243          */
9244         if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9245                 /*
9246                  * Get all vattr attrs plus filehandle and rdattr_error
9247                  */
9248                 rd_bitsval = NFS4_VATTR_MASK |
9249                     FATTR4_RDATTR_ERROR_MASK |
9250                     FATTR4_FILEHANDLE_MASK;
9251 
9252                 if (rp->r_flags & R4READDIRWATTR) {
9253                         mutex_enter(&rp->r_statelock);
9254                         rp->r_flags &= ~R4READDIRWATTR;
9255                         mutex_exit(&rp->r_statelock);
9256                 }
9257         } else {
9258                 servinfo4_t *svp = rp->r_server;
9259 
9260                 /*
9261                  * Already read directory. Use readdir with
9262                  * no attrs (except for mounted_on_fileid) for updates.
9263                  */
9264                 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9265 
9266                 /*
9267                  * request mounted on fileid if supported, else request
9268                  * fileid.  maybe we should verify that fileid is supported
9269                  * and request something else if not.
9270                  */
9271                 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9272                 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9273                         rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9274                 nfs_rw_exit(&svp->sv_lock);
9275         }
9276 
9277         /* putfh directory fh */
9278         argop[0].argop = OP_CPUTFH;
9279         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9280 
9281         argop[1].argop = OP_READDIR;
9282         rargs = &argop[1].nfs_argop4_u.opreaddir;
9283         /*
9284          * 1 and 2 are reserved for client "." and ".." entry offset.
9285          * cookie 0 should be used over-the-wire to start reading at
9286          * the beginning of the directory excluding "." and "..".
9287          */
9288         if (rdc->nfs4_cookie == 0 ||
9289             rdc->nfs4_cookie == 1 ||
9290             rdc->nfs4_cookie == 2) {
9291                 rargs->cookie = (nfs_cookie4)0;
9292                 rargs->cookieverf = 0;
9293         } else {
9294                 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9295                 mutex_enter(&rp->r_statelock);
9296                 rargs->cookieverf = rp->r_cookieverf4;
9297                 mutex_exit(&rp->r_statelock);
9298         }
9299         rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9300         rargs->maxcount = mi->mi_tsize;
9301         rargs->attr_request = rd_bitsval;
9302         rargs->rdc = rdc;
9303         rargs->dvp = vp;
9304         rargs->mi = mi;
9305         rargs->cr = cr;
9306 
9307 
9308         /*
9309          * If count < than the minimum required, we return no entries
9310          * and fail with EINVAL
9311          */
9312         if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9313                 rdc->error = EINVAL;
9314                 goto out;
9315         }
9316 
9317         if (args.array_len == 5) {
9318                 /*
9319                  * Add lookupp and getattr for parent nodeid.
9320                  */
9321                 argop[2].argop = OP_LOOKUPP;
9322 
9323                 argop[3].argop = OP_GETFH;
9324 
9325                 /* getattr parent */
9326                 argop[4].argop = OP_GETATTR;
9327                 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9328                 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9329         }
9330 
9331         doqueue = 1;
9332 
9333         if (mi->mi_io_kstats) {
9334                 mutex_enter(&mi->mi_lock);
9335                 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9336                 mutex_exit(&mi->mi_lock);
9337         }
9338 
9339         /* capture the time of this call */
9340         rargs->t = t = gethrtime();
9341 
9342         rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9343 
9344         if (mi->mi_io_kstats) {
9345                 mutex_enter(&mi->mi_lock);
9346                 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9347                 mutex_exit(&mi->mi_lock);
9348         }
9349 
9350         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9351 
9352         /*
9353          * If RPC error occurred and it isn't an error that
9354          * triggers recovery, then go ahead and fail now.
9355          */
9356         if (e.error != 0 && !needrecov) {
9357                 rdc->error = e.error;
9358                 goto out;
9359         }
9360 
9361         if (needrecov) {
9362                 bool_t abort;
9363 
9364                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9365                     "nfs4readdir: initiating recovery.\n"));
9366 
9367                 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9368                     NULL, OP_READDIR, NULL, NULL, NULL);
9369                 if (abort == FALSE) {
9370                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9371                             &recov_state, needrecov);
9372                         if (!e.error)
9373                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
9374                                     (caddr_t)&res);
9375                         if (rdc->entries != NULL) {
9376                                 kmem_free(rdc->entries, rdc->entlen);
9377                                 rdc->entries = NULL;
9378                         }
9379                         goto recov_retry;
9380                 }
9381 
9382                 if (e.error != 0) {
9383                         rdc->error = e.error;
9384                         goto out;
9385                 }
9386 
9387                 /* fall through for res.status case */
9388         }
9389 
9390         res_opcnt = res.array_len;
9391 
9392         /*
9393          * If compound failed first 2 ops (PUTFH+READDIR), then return
9394          * failure here.  Subsequent ops are for filling out dot-dot
9395          * dirent, and if they fail, we still want to give the caller
9396          * the dirents returned by (the successful) READDIR op, so we need
9397          * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9398          *
9399          * One example where PUTFH+READDIR ops would succeed but
9400          * LOOKUPP+GETATTR would fail would be a dir that has r perm
9401          * but lacks x.  In this case, a POSIX server's VOP_READDIR
9402          * would succeed; however, VOP_LOOKUP(..) would fail since no
9403          * x perm.  We need to come up with a non-vendor-specific way
9404          * for a POSIX server to return d_ino from dotdot's dirent if
9405          * client only requests mounted_on_fileid, and just say the
9406          * LOOKUPP succeeded and fill out the GETATTR.  However, if
9407          * client requested any mandatory attrs, server would be required
9408          * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9409          * for dotdot.
9410          */
9411 
9412         if (res.status) {
9413                 if (res_opcnt <= 2) {
9414                         e.error = geterrno4(res.status);
9415                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9416                             &recov_state, needrecov);
9417                         nfs4_purge_stale_fh(e.error, vp, cr);
9418                         rdc->error = e.error;
9419                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9420                         if (rdc->entries != NULL) {
9421                                 kmem_free(rdc->entries, rdc->entlen);
9422                                 rdc->entries = NULL;
9423                         }
9424                         /*
9425                          * If readdir a node that is a stub for a
9426                          * crossed mount point, keep the original
9427                          * secinfo flavor for the current file system,
9428                          * not the crossed one.
9429                          */
9430                         (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9431                         return;
9432                 }
9433         }
9434 
9435         resop = &res.array[1];      /* readdir res */
9436         rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9437 
9438         mutex_enter(&rp->r_statelock);
9439         rp->r_cookieverf4 = rd_res->cookieverf;
9440         mutex_exit(&rp->r_statelock);
9441 
9442         /*
9443          * For "." and ".." entries
9444          * e.g.
9445          *      seek(cookie=0) -> "." entry with d_off = 1
9446          *      seek(cookie=1) -> ".." entry with d_off = 2
9447          */
9448         if (cookie == (nfs_cookie4) 0) {
9449                 if (rd_res->dotp)
9450                         rd_res->dotp->d_ino = nodeid;
9451                 if (rd_res->dotdotp)
9452                         rd_res->dotdotp->d_ino = pnodeid;
9453         }
9454         if (cookie == (nfs_cookie4) 1) {
9455                 if (rd_res->dotdotp)
9456                         rd_res->dotdotp->d_ino = pnodeid;
9457         }
9458 
9459 
9460         /* LOOKUPP+GETATTR attemped */
9461         if (args.array_len == 5 && rd_res->dotdotp) {
9462                 if (res.status == NFS4_OK && res_opcnt == 5) {
9463                         nfs_fh4 *fhp;
9464                         nfs4_sharedfh_t *sfhp;
9465                         vnode_t *pvp;
9466                         nfs4_ga_res_t *garp;
9467 
9468                         resop++;        /* lookupp */
9469                         resop++;        /* getfh   */
9470                         fhp = &resop->nfs_resop4_u.opgetfh.object;
9471 
9472                         resop++;        /* getattr of parent */
9473 
9474                         /*
9475                          * First, take care of finishing the
9476                          * readdir results.
9477                          */
9478                         garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9479                         /*
9480                          * The d_ino of .. must be the inode number
9481                          * of the mounted filesystem.
9482                          */
9483                         if (garp->n4g_va.va_mask & AT_NODEID)
9484                                 rd_res->dotdotp->d_ino =
9485                                     garp->n4g_va.va_nodeid;
9486 
9487 
9488                         /*
9489                          * Next, create the ".." dnlc entry
9490                          */
9491                         sfhp = sfh4_get(fhp, mi);
9492                         if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9493                                 dnlc_update(vp, "..", pvp);
9494                                 VN_RELE(pvp);
9495                         }
9496                         sfh4_rele(&sfhp);
9497                 }
9498         }
9499 
9500         if (mi->mi_io_kstats) {
9501                 mutex_enter(&mi->mi_lock);
9502                 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9503                 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9504                 mutex_exit(&mi->mi_lock);
9505         }
9506 
9507         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9508 
9509 out:
9510         /*
9511          * If readdir a node that is a stub for a crossed mount point,
9512          * keep the original secinfo flavor for the current file system,
9513          * not the crossed one.
9514          */
9515         (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9516 
9517         nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9518 }
9519 
9520 
9521 static int
9522 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9523 {
9524         rnode4_t *rp = VTOR4(bp->b_vp);
9525         int count;
9526         int error;
9527         cred_t *cred_otw = NULL;
9528         offset_t offset;
9529         nfs4_open_stream_t *osp = NULL;
9530         bool_t first_time = TRUE;       /* first time getting otw cred */
9531         bool_t last_time = FALSE;       /* last time getting otw cred */
9532 
9533         ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9534 
9535         DTRACE_IO1(start, struct buf *, bp);
9536         offset = ldbtob(bp->b_lblkno);
9537 
9538         if (bp->b_flags & B_READ) {
9539         read_again:
9540                 /*
9541                  * Releases the osp, if it is provided.
9542                  * Puts a hold on the cred_otw and the new osp (if found).
9543                  */
9544                 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9545                     &first_time, &last_time);
9546                 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9547                     offset, bp->b_bcount, &bp->b_resid, cred_otw,
9548                     readahead, NULL);
9549                 crfree(cred_otw);
9550                 if (!error) {
9551                         if (bp->b_resid) {
9552                                 /*
9553                                  * Didn't get it all because we hit EOF,
9554                                  * zero all the memory beyond the EOF.
9555                                  */
9556                                 /* bzero(rdaddr + */
9557                                 bzero(bp->b_un.b_addr +
9558                                     bp->b_bcount - bp->b_resid, bp->b_resid);
9559                         }
9560                         mutex_enter(&rp->r_statelock);
9561                         if (bp->b_resid == bp->b_bcount &&
9562                             offset >= rp->r_size) {
9563                                 /*
9564                                  * We didn't read anything at all as we are
9565                                  * past EOF.  Return an error indicator back
9566                                  * but don't destroy the pages (yet).
9567                                  */
9568                                 error = NFS_EOF;
9569                         }
9570                         mutex_exit(&rp->r_statelock);
9571                 } else if (error == EACCES && last_time == FALSE) {
9572                                 goto read_again;
9573                 }
9574         } else {
9575                 if (!(rp->r_flags & R4STALE)) {
9576 write_again:
9577                         /*
9578                          * Releases the osp, if it is provided.
9579                          * Puts a hold on the cred_otw and the new
9580                          * osp (if found).
9581                          */
9582                         cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9583                             &first_time, &last_time);
9584                         mutex_enter(&rp->r_statelock);
9585                         count = MIN(bp->b_bcount, rp->r_size - offset);
9586                         mutex_exit(&rp->r_statelock);
9587                         if (count < 0)
9588                                 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9589 #ifdef DEBUG
9590                         if (count == 0) {
9591                                 zoneid_t zoneid = getzoneid();
9592 
9593                                 zcmn_err(zoneid, CE_WARN,
9594                                     "nfs4_bio: zero length write at %lld",
9595                                     offset);
9596                                 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9597                                     "b_bcount=%ld, file size=%lld",
9598                                     rp->r_flags, (long)bp->b_bcount,
9599                                     rp->r_size);
9600                                 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9601                                 if (nfs4_bio_do_stop)
9602                                         debug_enter("nfs4_bio");
9603                         }
9604 #endif
9605                         error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9606                             count, cred_otw, stab_comm);
9607                         if (error == EACCES && last_time == FALSE) {
9608                                 crfree(cred_otw);
9609                                 goto write_again;
9610                         }
9611                         bp->b_error = error;
9612                         if (error && error != EINTR &&
9613                             !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9614                                 /*
9615                                  * Don't print EDQUOT errors on the console.
9616                                  * Don't print asynchronous EACCES errors.
9617                                  * Don't print EFBIG errors.
9618                                  * Print all other write errors.
9619                                  */
9620                                 if (error != EDQUOT && error != EFBIG &&
9621                                     (error != EACCES ||
9622                                     !(bp->b_flags & B_ASYNC)))
9623                                         nfs4_write_error(bp->b_vp,
9624                                             error, cred_otw);
9625                                 /*
9626                                  * Update r_error and r_flags as appropriate.
9627                                  * If the error was ESTALE, then mark the
9628                                  * rnode as not being writeable and save
9629                                  * the error status.  Otherwise, save any
9630                                  * errors which occur from asynchronous
9631                                  * page invalidations.  Any errors occurring
9632                                  * from other operations should be saved
9633                                  * by the caller.
9634                                  */
9635                                 mutex_enter(&rp->r_statelock);
9636                                 if (error == ESTALE) {
9637                                         rp->r_flags |= R4STALE;
9638                                         if (!rp->r_error)
9639                                                 rp->r_error = error;
9640                                 } else if (!rp->r_error &&
9641                                     (bp->b_flags &
9642                                     (B_INVAL|B_FORCE|B_ASYNC)) ==
9643                                     (B_INVAL|B_FORCE|B_ASYNC)) {
9644                                         rp->r_error = error;
9645                                 }
9646                                 mutex_exit(&rp->r_statelock);
9647                         }
9648                         crfree(cred_otw);
9649                 } else {
9650                         error = rp->r_error;
9651                         /*
9652                          * A close may have cleared r_error, if so,
9653                          * propagate ESTALE error return properly
9654                          */
9655                         if (error == 0)
9656                                 error = ESTALE;
9657                 }
9658         }
9659 
9660         if (error != 0 && error != NFS_EOF)
9661                 bp->b_flags |= B_ERROR;
9662 
9663         if (osp)
9664                 open_stream_rele(osp, rp);
9665 
9666         DTRACE_IO1(done, struct buf *, bp);
9667 
9668         return (error);
9669 }
9670 
9671 /* ARGSUSED */
9672 int
9673 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9674 {
9675         return (EREMOTE);
9676 }
9677 
9678 /* ARGSUSED2 */
9679 int
9680 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9681 {
9682         rnode4_t *rp = VTOR4(vp);
9683 
9684         if (!write_lock) {
9685                 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9686                 return (V_WRITELOCK_FALSE);
9687         }
9688 
9689         if ((rp->r_flags & R4DIRECTIO) ||
9690             (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9691                 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9692                 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9693                         return (V_WRITELOCK_FALSE);
9694                 nfs_rw_exit(&rp->r_rwlock);
9695         }
9696 
9697         (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9698         return (V_WRITELOCK_TRUE);
9699 }
9700 
9701 /* ARGSUSED */
9702 void
9703 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9704 {
9705         rnode4_t *rp = VTOR4(vp);
9706 
9707         nfs_rw_exit(&rp->r_rwlock);
9708 }
9709 
9710 /* ARGSUSED */
9711 static int
9712 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9713 {
9714         if (nfs_zone() != VTOMI4(vp)->mi_zone)
9715                 return (EIO);
9716 
9717         /*
9718          * Because we stuff the readdir cookie into the offset field
9719          * someone may attempt to do an lseek with the cookie which
9720          * we want to succeed.
9721          */
9722         if (vp->v_type == VDIR)
9723                 return (0);
9724         if (*noffp < 0)
9725                 return (EINVAL);
9726         return (0);
9727 }
9728 
9729 
9730 /*
9731  * Return all the pages from [off..off+len) in file
9732  */
9733 /* ARGSUSED */
9734 static int
9735 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9736     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9737         enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9738 {
9739         rnode4_t *rp;
9740         int error;
9741         mntinfo4_t *mi;
9742 
9743         if (nfs_zone() != VTOMI4(vp)->mi_zone)
9744                 return (EIO);
9745         rp = VTOR4(vp);
9746         if (IS_SHADOW(vp, rp))
9747                 vp = RTOV4(rp);
9748 
9749         if (vp->v_flag & VNOMAP)
9750                 return (ENOSYS);
9751 
9752         if (protp != NULL)
9753                 *protp = PROT_ALL;
9754 
9755         /*
9756          * Now validate that the caches are up to date.
9757          */
9758         if (error = nfs4_validate_caches(vp, cr))
9759                 return (error);
9760 
9761         mi = VTOMI4(vp);
9762 retry:
9763         mutex_enter(&rp->r_statelock);
9764 
9765         /*
9766          * Don't create dirty pages faster than they
9767          * can be cleaned so that the system doesn't
9768          * get imbalanced.  If the async queue is
9769          * maxed out, then wait for it to drain before
9770          * creating more dirty pages.  Also, wait for
9771          * any threads doing pagewalks in the vop_getattr
9772          * entry points so that they don't block for
9773          * long periods.
9774          */
9775         if (rw == S_CREATE) {
9776                 while ((mi->mi_max_threads != 0 &&
9777                     rp->r_awcount > 2 * mi->mi_max_threads) ||
9778                     rp->r_gcount > 0)
9779                         cv_wait(&rp->r_cv, &rp->r_statelock);
9780         }
9781 
9782         /*
9783          * If we are getting called as a side effect of an nfs_write()
9784          * operation the local file size might not be extended yet.
9785          * In this case we want to be able to return pages of zeroes.
9786          */
9787         if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9788                 NFS4_DEBUG(nfs4_pageio_debug,
9789                     (CE_NOTE, "getpage beyond EOF: off=%lld, "
9790                     "len=%llu, size=%llu, attrsize =%llu", off,
9791                     (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9792                 mutex_exit(&rp->r_statelock);
9793                 return (EFAULT);                /* beyond EOF */
9794         }
9795 
9796         mutex_exit(&rp->r_statelock);
9797 
9798         if (len <= PAGESIZE) {
9799                 error = nfs4_getapage(vp, off, len, protp, pl, plsz,
9800                     seg, addr, rw, cr);
9801                 NFS4_DEBUG(nfs4_pageio_debug && error,
9802                     (CE_NOTE, "getpage error %d; off=%lld, "
9803                     "len=%lld", error, off, (u_longlong_t)len));
9804         } else {
9805                 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9806                     pl, plsz, seg, addr, rw, cr);
9807                 NFS4_DEBUG(nfs4_pageio_debug && error,
9808                     (CE_NOTE, "getpages error %d; off=%lld, "
9809                     "len=%lld", error, off, (u_longlong_t)len));
9810         }
9811 
9812         switch (error) {
9813         case NFS_EOF:
9814                 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9815                 goto retry;
9816         case ESTALE:
9817                 nfs4_purge_stale_fh(error, vp, cr);
9818         }
9819 
9820         return (error);
9821 }
9822 
9823 /*
9824  * Called from pvn_getpages or nfs4_getpage to get a particular page.
9825  */
9826 /* ARGSUSED */
9827 static int
9828 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9829     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9830     enum seg_rw rw, cred_t *cr)
9831 {
9832         rnode4_t *rp;
9833         uint_t bsize;
9834         struct buf *bp;
9835         page_t *pp;
9836         u_offset_t lbn;
9837         u_offset_t io_off;
9838         u_offset_t blkoff;
9839         u_offset_t rablkoff;
9840         size_t io_len;
9841         uint_t blksize;
9842         int error;
9843         int readahead;
9844         int readahead_issued = 0;
9845         int ra_window; /* readahead window */
9846         page_t *pagefound;
9847         page_t *savepp;
9848 
9849         if (nfs_zone() != VTOMI4(vp)->mi_zone)
9850                 return (EIO);
9851 
9852         rp = VTOR4(vp);
9853         ASSERT(!IS_SHADOW(vp, rp));
9854         bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9855 
9856 reread:
9857         bp = NULL;
9858         pp = NULL;
9859         pagefound = NULL;
9860 
9861         if (pl != NULL)
9862                 pl[0] = NULL;
9863 
9864         error = 0;
9865         lbn = off / bsize;
9866         blkoff = lbn * bsize;
9867 
9868         /*
9869          * Queueing up the readahead before doing the synchronous read
9870          * results in a significant increase in read throughput because
9871          * of the increased parallelism between the async threads and
9872          * the process context.
9873          */
9874         if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9875             rw != S_CREATE &&
9876             !(vp->v_flag & VNOCACHE)) {
9877                 mutex_enter(&rp->r_statelock);
9878 
9879                 /*
9880                  * Calculate the number of readaheads to do.
9881                  * a) No readaheads at offset = 0.
9882                  * b) Do maximum(nfs4_nra) readaheads when the readahead
9883                  *    window is closed.
9884                  * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9885                  *    upon how far the readahead window is open or close.
9886                  * d) No readaheads if rp->r_nextr is not within the scope
9887                  *    of the readahead window (random i/o).
9888                  */
9889 
9890                 if (off == 0)
9891                         readahead = 0;
9892                 else if (blkoff == rp->r_nextr)
9893                         readahead = nfs4_nra;
9894                 else if (rp->r_nextr > blkoff &&
9895                     ((ra_window = (rp->r_nextr - blkoff) / bsize)
9896                     <= (nfs4_nra - 1)))
9897                         readahead = nfs4_nra - ra_window;
9898                 else
9899                         readahead = 0;
9900 
9901                 rablkoff = rp->r_nextr;
9902                 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9903                         mutex_exit(&rp->r_statelock);
9904                         if (nfs4_async_readahead(vp, rablkoff + bsize,
9905                             addr + (rablkoff + bsize - off),
9906                             seg, cr, nfs4_readahead) < 0) {
9907                                 mutex_enter(&rp->r_statelock);
9908                                 break;
9909                         }
9910                         readahead--;
9911                         rablkoff += bsize;
9912                         /*
9913                          * Indicate that we did a readahead so
9914                          * readahead offset is not updated
9915                          * by the synchronous read below.
9916                          */
9917                         readahead_issued = 1;
9918                         mutex_enter(&rp->r_statelock);
9919                         /*
9920                          * set readahead offset to
9921                          * offset of last async readahead
9922                          * request.
9923                          */
9924                         rp->r_nextr = rablkoff;
9925                 }
9926                 mutex_exit(&rp->r_statelock);
9927         }
9928 
9929 again:
9930         if ((pagefound = page_exists(vp, off)) == NULL) {
9931                 if (pl == NULL) {
9932                         (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9933                             nfs4_readahead);
9934                 } else if (rw == S_CREATE) {
9935                         /*
9936                          * Block for this page is not allocated, or the offset
9937                          * is beyond the current allocation size, or we're
9938                          * allocating a swap slot and the page was not found,
9939                          * so allocate it and return a zero page.
9940                          */
9941                         if ((pp = page_create_va(vp, off,
9942                             PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9943                                 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9944                         io_len = PAGESIZE;
9945                         mutex_enter(&rp->r_statelock);
9946                         rp->r_nextr = off + PAGESIZE;
9947                         mutex_exit(&rp->r_statelock);
9948                 } else {
9949                         /*
9950                          * Need to go to server to get a block
9951                          */
9952                         mutex_enter(&rp->r_statelock);
9953                         if (blkoff < rp->r_size &&
9954                             blkoff + bsize > rp->r_size) {
9955                                 /*
9956                                  * If less than a block left in
9957                                  * file read less than a block.
9958                                  */
9959                                 if (rp->r_size <= off) {
9960                                         /*
9961                                          * Trying to access beyond EOF,
9962                                          * set up to get at least one page.
9963                                          */
9964                                         blksize = off + PAGESIZE - blkoff;
9965                                 } else
9966                                         blksize = rp->r_size - blkoff;
9967                         } else if ((off == 0) ||
9968                             (off != rp->r_nextr && !readahead_issued)) {
9969                                 blksize = PAGESIZE;
9970                                 blkoff = off; /* block = page here */
9971                         } else
9972                                 blksize = bsize;
9973                         mutex_exit(&rp->r_statelock);
9974 
9975                         pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9976                             &io_len, blkoff, blksize, 0);
9977 
9978                         /*
9979                          * Some other thread has entered the page,
9980                          * so just use it.
9981                          */
9982                         if (pp == NULL)
9983                                 goto again;
9984 
9985                         /*
9986                          * Now round the request size up to page boundaries.
9987                          * This ensures that the entire page will be
9988                          * initialized to zeroes if EOF is encountered.
9989                          */
9990                         io_len = ptob(btopr(io_len));
9991 
9992                         bp = pageio_setup(pp, io_len, vp, B_READ);
9993                         ASSERT(bp != NULL);
9994 
9995                         /*
9996                          * pageio_setup should have set b_addr to 0.  This
9997                          * is correct since we want to do I/O on a page
9998                          * boundary.  bp_mapin will use this addr to calculate
9999                          * an offset, and then set b_addr to the kernel virtual
10000                          * address it allocated for us.
10001                          */
10002                         ASSERT(bp->b_un.b_addr == 0);
10003 
10004                         bp->b_edev = 0;
10005                         bp->b_dev = 0;
10006                         bp->b_lblkno = lbtodb(io_off);
10007                         bp->b_file = vp;
10008                         bp->b_offset = (offset_t)off;
10009                         bp_mapin(bp);
10010 
10011                         /*
10012                          * If doing a write beyond what we believe is EOF,
10013                          * don't bother trying to read the pages from the
10014                          * server, we'll just zero the pages here.  We
10015                          * don't check that the rw flag is S_WRITE here
10016                          * because some implementations may attempt a
10017                          * read access to the buffer before copying data.
10018                          */
10019                         mutex_enter(&rp->r_statelock);
10020                         if (io_off >= rp->r_size && seg == segkmap) {
10021                                 mutex_exit(&rp->r_statelock);
10022                                 bzero(bp->b_un.b_addr, io_len);
10023                         } else {
10024                                 mutex_exit(&rp->r_statelock);
10025                                 error = nfs4_bio(bp, NULL, cr, FALSE);
10026                         }
10027 
10028                         /*
10029                          * Unmap the buffer before freeing it.
10030                          */
10031                         bp_mapout(bp);
10032                         pageio_done(bp);
10033 
10034                         savepp = pp;
10035                         do {
10036                                 pp->p_fsdata = C_NOCOMMIT;
10037                         } while ((pp = pp->p_next) != savepp);
10038 
10039                         if (error == NFS_EOF) {
10040                                 /*
10041                                  * If doing a write system call just return
10042                                  * zeroed pages, else user tried to get pages
10043                                  * beyond EOF, return error.  We don't check
10044                                  * that the rw flag is S_WRITE here because
10045                                  * some implementations may attempt a read
10046                                  * access to the buffer before copying data.
10047                                  */
10048                                 if (seg == segkmap)
10049                                         error = 0;
10050                                 else
10051                                         error = EFAULT;
10052                         }
10053 
10054                         if (!readahead_issued && !error) {
10055                                 mutex_enter(&rp->r_statelock);
10056                                 rp->r_nextr = io_off + io_len;
10057                                 mutex_exit(&rp->r_statelock);
10058                         }
10059                 }
10060         }
10061 
10062 out:
10063         if (pl == NULL)
10064                 return (error);
10065 
10066         if (error) {
10067                 if (pp != NULL)
10068                         pvn_read_done(pp, B_ERROR);
10069                 return (error);
10070         }
10071 
10072         if (pagefound) {
10073                 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10074 
10075                 /*
10076                  * Page exists in the cache, acquire the appropriate lock.
10077                  * If this fails, start all over again.
10078                  */
10079                 if ((pp = page_lookup(vp, off, se)) == NULL) {
10080 #ifdef DEBUG
10081                         nfs4_lostpage++;
10082 #endif
10083                         goto reread;
10084                 }
10085                 pl[0] = pp;
10086                 pl[1] = NULL;
10087                 return (0);
10088         }
10089 
10090         if (pp != NULL)
10091                 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10092 
10093         return (error);
10094 }
10095 
10096 static void
10097 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10098     cred_t *cr)
10099 {
10100         int error;
10101         page_t *pp;
10102         u_offset_t io_off;
10103         size_t io_len;
10104         struct buf *bp;
10105         uint_t bsize, blksize;
10106         rnode4_t *rp = VTOR4(vp);
10107         page_t *savepp;
10108 
10109         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10110 
10111         bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10112 
10113         mutex_enter(&rp->r_statelock);
10114         if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10115                 /*
10116                  * If less than a block left in file read less
10117                  * than a block.
10118                  */
10119                 blksize = rp->r_size - blkoff;
10120         } else
10121                 blksize = bsize;
10122         mutex_exit(&rp->r_statelock);
10123 
10124         pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10125             &io_off, &io_len, blkoff, blksize, 1);
10126         /*
10127          * The isra flag passed to the kluster function is 1, we may have
10128          * gotten a return value of NULL for a variety of reasons (# of free
10129          * pages < minfree, someone entered the page on the vnode etc). In all
10130          * cases, we want to punt on the readahead.
10131          */
10132         if (pp == NULL)
10133                 return;
10134 
10135         /*
10136          * Now round the request size up to page boundaries.
10137          * This ensures that the entire page will be
10138          * initialized to zeroes if EOF is encountered.
10139          */
10140         io_len = ptob(btopr(io_len));
10141 
10142         bp = pageio_setup(pp, io_len, vp, B_READ);
10143         ASSERT(bp != NULL);
10144 
10145         /*
10146          * pageio_setup should have set b_addr to 0.  This is correct since
10147          * we want to do I/O on a page boundary. bp_mapin() will use this addr
10148          * to calculate an offset, and then set b_addr to the kernel virtual
10149          * address it allocated for us.
10150          */
10151         ASSERT(bp->b_un.b_addr == 0);
10152 
10153         bp->b_edev = 0;
10154         bp->b_dev = 0;
10155         bp->b_lblkno = lbtodb(io_off);
10156         bp->b_file = vp;
10157         bp->b_offset = (offset_t)blkoff;
10158         bp_mapin(bp);
10159 
10160         /*
10161          * If doing a write beyond what we believe is EOF, don't bother trying
10162          * to read the pages from the server, we'll just zero the pages here.
10163          * We don't check that the rw flag is S_WRITE here because some
10164          * implementations may attempt a read access to the buffer before
10165          * copying data.
10166          */
10167         mutex_enter(&rp->r_statelock);
10168         if (io_off >= rp->r_size && seg == segkmap) {
10169                 mutex_exit(&rp->r_statelock);
10170                 bzero(bp->b_un.b_addr, io_len);
10171                 error = 0;
10172         } else {
10173                 mutex_exit(&rp->r_statelock);
10174                 error = nfs4_bio(bp, NULL, cr, TRUE);
10175                 if (error == NFS_EOF)
10176                         error = 0;
10177         }
10178 
10179         /*
10180          * Unmap the buffer before freeing it.
10181          */
10182         bp_mapout(bp);
10183         pageio_done(bp);
10184 
10185         savepp = pp;
10186         do {
10187                 pp->p_fsdata = C_NOCOMMIT;
10188         } while ((pp = pp->p_next) != savepp);
10189 
10190         pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10191 
10192         /*
10193          * In case of error set readahead offset
10194          * to the lowest offset.
10195          * pvn_read_done() calls VN_DISPOSE to destroy the pages
10196          */
10197         if (error && rp->r_nextr > io_off) {
10198                 mutex_enter(&rp->r_statelock);
10199                 if (rp->r_nextr > io_off)
10200                         rp->r_nextr = io_off;
10201                 mutex_exit(&rp->r_statelock);
10202         }
10203 }
10204 
10205 /*
10206  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10207  * If len == 0, do from off to EOF.
10208  *
10209  * The normal cases should be len == 0 && off == 0 (entire vp list) or
10210  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10211  * (from pageout).
10212  */
10213 /* ARGSUSED */
10214 static int
10215 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10216         caller_context_t *ct)
10217 {
10218         int error;
10219         rnode4_t *rp;
10220 
10221         ASSERT(cr != NULL);
10222 
10223         if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10224                 return (EIO);
10225 
10226         rp = VTOR4(vp);
10227         if (IS_SHADOW(vp, rp))
10228                 vp = RTOV4(rp);
10229 
10230         /*
10231          * XXX - Why should this check be made here?
10232          */
10233         if (vp->v_flag & VNOMAP)
10234                 return (ENOSYS);
10235 
10236         if (len == 0 && !(flags & B_INVAL) &&
10237             (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10238                 return (0);
10239 
10240         mutex_enter(&rp->r_statelock);
10241         rp->r_count++;
10242         mutex_exit(&rp->r_statelock);
10243         error = nfs4_putpages(vp, off, len, flags, cr);
10244         mutex_enter(&rp->r_statelock);
10245         rp->r_count--;
10246         cv_broadcast(&rp->r_cv);
10247         mutex_exit(&rp->r_statelock);
10248 
10249         return (error);
10250 }
10251 
10252 /*
10253  * Write out a single page, possibly klustering adjacent dirty pages.
10254  */
10255 int
10256 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10257     int flags, cred_t *cr)
10258 {
10259         u_offset_t io_off;
10260         u_offset_t lbn_off;
10261         u_offset_t lbn;
10262         size_t io_len;
10263         uint_t bsize;
10264         int error;
10265         rnode4_t *rp;
10266 
10267         ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10268         ASSERT(pp != NULL);
10269         ASSERT(cr != NULL);
10270         ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10271 
10272         rp = VTOR4(vp);
10273         ASSERT(rp->r_count > 0);
10274         ASSERT(!IS_SHADOW(vp, rp));
10275 
10276         bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10277         lbn = pp->p_offset / bsize;
10278         lbn_off = lbn * bsize;
10279 
10280         /*
10281          * Find a kluster that fits in one block, or in
10282          * one page if pages are bigger than blocks.  If
10283          * there is less file space allocated than a whole
10284          * page, we'll shorten the i/o request below.
10285          */
10286         pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10287             roundup(bsize, PAGESIZE), flags);
10288 
10289         /*
10290          * pvn_write_kluster shouldn't have returned a page with offset
10291          * behind the original page we were given.  Verify that.
10292          */
10293         ASSERT((pp->p_offset / bsize) >= lbn);
10294 
10295         /*
10296          * Now pp will have the list of kept dirty pages marked for
10297          * write back.  It will also handle invalidation and freeing
10298          * of pages that are not dirty.  Check for page length rounding
10299          * problems.
10300          */
10301         if (io_off + io_len > lbn_off + bsize) {
10302                 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10303                 io_len = lbn_off + bsize - io_off;
10304         }
10305         /*
10306          * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10307          * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10308          * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10309          * progress and the r_size has not been made consistent with the
10310          * new size of the file. When the uiomove() completes the r_size is
10311          * updated and the R4MODINPROGRESS flag is cleared.
10312          *
10313          * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10314          * consistent value of r_size. Without this handshaking, it is
10315          * possible that nfs4_bio() picks  up the old value of r_size
10316          * before the uiomove() in writerp4() completes. This will result
10317          * in the write through nfs4_bio() being dropped.
10318          *
10319          * More precisely, there is a window between the time the uiomove()
10320          * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10321          * operation intervenes in this window, the page will be picked up,
10322          * because it is dirty (it will be unlocked, unless it was
10323          * pagecreate'd). When the page is picked up as dirty, the dirty
10324          * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10325          * checked. This will still be the old size. Therefore the page will
10326          * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10327          * the page will be found to be clean and the write will be dropped.
10328          */
10329         if (rp->r_flags & R4MODINPROGRESS) {
10330                 mutex_enter(&rp->r_statelock);
10331                 if ((rp->r_flags & R4MODINPROGRESS) &&
10332                     rp->r_modaddr + MAXBSIZE > io_off &&
10333                     rp->r_modaddr < io_off + io_len) {
10334                         page_t *plist;
10335                         /*
10336                          * A write is in progress for this region of the file.
10337                          * If we did not detect R4MODINPROGRESS here then this
10338                          * path through nfs_putapage() would eventually go to
10339                          * nfs4_bio() and may not write out all of the data
10340                          * in the pages. We end up losing data. So we decide
10341                          * to set the modified bit on each page in the page
10342                          * list and mark the rnode with R4DIRTY. This write
10343                          * will be restarted at some later time.
10344                          */
10345                         plist = pp;
10346                         while (plist != NULL) {
10347                                 pp = plist;
10348                                 page_sub(&plist, pp);
10349                                 hat_setmod(pp);
10350                                 page_io_unlock(pp);
10351                                 page_unlock(pp);
10352                         }
10353                         rp->r_flags |= R4DIRTY;
10354                         mutex_exit(&rp->r_statelock);
10355                         if (offp)
10356                                 *offp = io_off;
10357                         if (lenp)
10358                                 *lenp = io_len;
10359                         return (0);
10360                 }
10361                 mutex_exit(&rp->r_statelock);
10362         }
10363 
10364         if (flags & B_ASYNC) {
10365                 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10366                     nfs4_sync_putapage);
10367         } else
10368                 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10369 
10370         if (offp)
10371                 *offp = io_off;
10372         if (lenp)
10373                 *lenp = io_len;
10374         return (error);
10375 }
10376 
10377 static int
10378 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10379     int flags, cred_t *cr)
10380 {
10381         int error;
10382         rnode4_t *rp;
10383 
10384         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10385 
10386         flags |= B_WRITE;
10387 
10388         error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10389 
10390         rp = VTOR4(vp);
10391 
10392         if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10393             error == EACCES) &&
10394             (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10395                 if (!(rp->r_flags & R4OUTOFSPACE)) {
10396                         mutex_enter(&rp->r_statelock);
10397                         rp->r_flags |= R4OUTOFSPACE;
10398                         mutex_exit(&rp->r_statelock);
10399                 }
10400                 flags |= B_ERROR;
10401                 pvn_write_done(pp, flags);
10402                 /*
10403                  * If this was not an async thread, then try again to
10404                  * write out the pages, but this time, also destroy
10405                  * them whether or not the write is successful.  This
10406                  * will prevent memory from filling up with these
10407                  * pages and destroying them is the only alternative
10408                  * if they can't be written out.
10409                  *
10410                  * Don't do this if this is an async thread because
10411                  * when the pages are unlocked in pvn_write_done,
10412                  * some other thread could have come along, locked
10413                  * them, and queued for an async thread.  It would be
10414                  * possible for all of the async threads to be tied
10415                  * up waiting to lock the pages again and they would
10416                  * all already be locked and waiting for an async
10417                  * thread to handle them.  Deadlock.
10418                  */
10419                 if (!(flags & B_ASYNC)) {
10420                         error = nfs4_putpage(vp, io_off, io_len,
10421                             B_INVAL | B_FORCE, cr, NULL);
10422                 }
10423         } else {
10424                 if (error)
10425                         flags |= B_ERROR;
10426                 else if (rp->r_flags & R4OUTOFSPACE) {
10427                         mutex_enter(&rp->r_statelock);
10428                         rp->r_flags &= ~R4OUTOFSPACE;
10429                         mutex_exit(&rp->r_statelock);
10430                 }
10431                 pvn_write_done(pp, flags);
10432                 if (freemem < desfree)
10433                         (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10434                             NFS4_WRITE_NOWAIT);
10435         }
10436 
10437         return (error);
10438 }
10439 
10440 #ifdef DEBUG
10441 int nfs4_force_open_before_mmap = 0;
10442 #endif
10443 
10444 /* ARGSUSED */
10445 static int
10446 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10447     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10448     caller_context_t *ct)
10449 {
10450         struct segvn_crargs vn_a;
10451         int error = 0;
10452         rnode4_t *rp = VTOR4(vp);
10453         mntinfo4_t *mi = VTOMI4(vp);
10454 
10455         if (nfs_zone() != VTOMI4(vp)->mi_zone)
10456                 return (EIO);
10457 
10458         if (vp->v_flag & VNOMAP)
10459                 return (ENOSYS);
10460 
10461         if (off < 0 || (off + len) < 0)
10462                 return (ENXIO);
10463 
10464         if (vp->v_type != VREG)
10465                 return (ENODEV);
10466 
10467         /*
10468          * If the file is delegated to the client don't do anything.
10469          * If the file is not delegated, then validate the data cache.
10470          */
10471         mutex_enter(&rp->r_statev4_lock);
10472         if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10473                 mutex_exit(&rp->r_statev4_lock);
10474                 error = nfs4_validate_caches(vp, cr);
10475                 if (error)
10476                         return (error);
10477         } else {
10478                 mutex_exit(&rp->r_statev4_lock);
10479         }
10480 
10481         /*
10482          * Check to see if the vnode is currently marked as not cachable.
10483          * This means portions of the file are locked (through VOP_FRLOCK).
10484          * In this case the map request must be refused.  We use
10485          * rp->r_lkserlock to avoid a race with concurrent lock requests.
10486          *
10487          * Atomically increment r_inmap after acquiring r_rwlock. The
10488          * idea here is to acquire r_rwlock to block read/write and
10489          * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10490          * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10491          * and we can prevent the deadlock that would have occurred
10492          * when nfs4_addmap() would have acquired it out of order.
10493          *
10494          * Since we are not protecting r_inmap by any lock, we do not
10495          * hold any lock when we decrement it. We atomically decrement
10496          * r_inmap after we release r_lkserlock.
10497          */
10498 
10499         if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10500                 return (EINTR);
10501         atomic_add_int(&rp->r_inmap, 1);
10502         nfs_rw_exit(&rp->r_rwlock);
10503 
10504         if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10505                 atomic_add_int(&rp->r_inmap, -1);
10506                 return (EINTR);
10507         }
10508 
10509 
10510         if (vp->v_flag & VNOCACHE) {
10511                 error = EAGAIN;
10512                 goto done;
10513         }
10514 
10515         /*
10516          * Don't allow concurrent locks and mapping if mandatory locking is
10517          * enabled.
10518          */
10519         if (flk_has_remote_locks(vp)) {
10520                 struct vattr va;
10521                 va.va_mask = AT_MODE;
10522                 error = nfs4getattr(vp, &va, cr);
10523                 if (error != 0)
10524                         goto done;
10525                 if (MANDLOCK(vp, va.va_mode)) {
10526                         error = EAGAIN;
10527                         goto done;
10528                 }
10529         }
10530 
10531         /*
10532          * It is possible that the rnode has a lost lock request that we
10533          * are still trying to recover, and that the request conflicts with
10534          * this map request.
10535          *
10536          * An alternative approach would be for nfs4_safemap() to consider
10537          * queued lock requests when deciding whether to set or clear
10538          * VNOCACHE.  This would require the frlock code path to call
10539          * nfs4_safemap() after enqueing a lost request.
10540          */
10541         if (nfs4_map_lost_lock_conflict(vp)) {
10542                 error = EAGAIN;
10543                 goto done;
10544         }
10545 
10546         as_rangelock(as);
10547         error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10548         if (error != 0) {
10549                 as_rangeunlock(as);
10550                 goto done;
10551         }
10552 
10553         if (vp->v_type == VREG) {
10554                 /*
10555                  * We need to retrieve the open stream
10556                  */
10557                 nfs4_open_stream_t      *osp = NULL;
10558                 nfs4_open_owner_t       *oop = NULL;
10559 
10560                 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10561                 if (oop != NULL) {
10562                         /* returns with 'os_sync_lock' held */
10563                         osp = find_open_stream(oop, rp);
10564                         open_owner_rele(oop);
10565                 }
10566                 if (osp == NULL) {
10567 #ifdef DEBUG
10568                         if (nfs4_force_open_before_mmap) {
10569                                 error = EIO;
10570                                 goto done;
10571                         }
10572 #endif
10573                         /* returns with 'os_sync_lock' held */
10574                         error = open_and_get_osp(vp, cr, &osp);
10575                         if (osp == NULL) {
10576                                 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10577                                     "nfs4_map: we tried to OPEN the file "
10578                                     "but again no osp, so fail with EIO"));
10579                                 goto done;
10580                         }
10581                 }
10582 
10583                 if (osp->os_failed_reopen) {
10584                         mutex_exit(&osp->os_sync_lock);
10585                         open_stream_rele(osp, rp);
10586                         NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10587                             "nfs4_map: os_failed_reopen set on "
10588                             "osp %p, cr %p, rp %s", (void *)osp,
10589                             (void *)cr, rnode4info(rp)));
10590                         error = EIO;
10591                         goto done;
10592                 }
10593                 mutex_exit(&osp->os_sync_lock);
10594                 open_stream_rele(osp, rp);
10595         }
10596 
10597         vn_a.vp = vp;
10598         vn_a.offset = off;
10599         vn_a.type = (flags & MAP_TYPE);
10600         vn_a.prot = (uchar_t)prot;
10601         vn_a.maxprot = (uchar_t)maxprot;
10602         vn_a.flags = (flags & ~MAP_TYPE);
10603         vn_a.cred = cr;
10604         vn_a.amp = NULL;
10605         vn_a.szc = 0;
10606         vn_a.lgrp_mem_policy_flags = 0;
10607 
10608         error = as_map(as, *addrp, len, segvn_create, &vn_a);
10609         as_rangeunlock(as);
10610 
10611 done:
10612         nfs_rw_exit(&rp->r_lkserlock);
10613         atomic_add_int(&rp->r_inmap, -1);
10614         return (error);
10615 }
10616 
10617 /*
10618  * We're most likely dealing with a kernel module that likes to READ
10619  * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10620  * officially OPEN the file to create the necessary client state
10621  * for bookkeeping of os_mmap_read/write counts.
10622  *
10623  * Since VOP_MAP only passes in a pointer to the vnode rather than
10624  * a double pointer, we can't handle the case where nfs4open_otw()
10625  * returns a different vnode than the one passed into VOP_MAP (since
10626  * VOP_DELMAP will not see the vnode nfs4open_otw used).  In this case,
10627  * we return NULL and let nfs4_map() fail.  Note: the only case where
10628  * this should happen is if the file got removed and replaced with the
10629  * same name on the server (in addition to the fact that we're trying
10630  * to VOP_MAP withouth VOP_OPENing the file in the first place).
10631  */
10632 static int
10633 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10634 {
10635         rnode4_t                *rp, *drp;
10636         vnode_t                 *dvp, *open_vp;
10637         char                    file_name[MAXNAMELEN];
10638         int                     just_created;
10639         nfs4_open_stream_t      *osp;
10640         nfs4_open_owner_t       *oop;
10641         int                     error;
10642 
10643         *ospp = NULL;
10644         open_vp = map_vp;
10645 
10646         rp = VTOR4(open_vp);
10647         if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10648                 return (error);
10649         drp = VTOR4(dvp);
10650 
10651         if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10652                 VN_RELE(dvp);
10653                 return (EINTR);
10654         }
10655 
10656         if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10657                 nfs_rw_exit(&drp->r_rwlock);
10658                 VN_RELE(dvp);
10659                 return (error);
10660         }
10661 
10662         mutex_enter(&rp->r_statev4_lock);
10663         if (rp->created_v4) {
10664                 rp->created_v4 = 0;
10665                 mutex_exit(&rp->r_statev4_lock);
10666 
10667                 dnlc_update(dvp, file_name, open_vp);
10668                 /* This is needed so we don't bump the open ref count */
10669                 just_created = 1;
10670         } else {
10671                 mutex_exit(&rp->r_statev4_lock);
10672                 just_created = 0;
10673         }
10674 
10675         VN_HOLD(map_vp);
10676 
10677         error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10678             just_created);
10679         if (error) {
10680                 nfs_rw_exit(&drp->r_rwlock);
10681                 VN_RELE(dvp);
10682                 VN_RELE(map_vp);
10683                 return (error);
10684         }
10685 
10686         nfs_rw_exit(&drp->r_rwlock);
10687         VN_RELE(dvp);
10688 
10689         /*
10690          * If nfs4open_otw() returned a different vnode then "undo"
10691          * the open and return failure to the caller.
10692          */
10693         if (!VN_CMP(open_vp, map_vp)) {
10694                 nfs4_error_t e;
10695 
10696                 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10697                     "open returned a different vnode"));
10698                 /*
10699                  * If there's an error, ignore it,
10700                  * and let VOP_INACTIVE handle it.
10701                  */
10702                 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10703                     CLOSE_NORM, 0, 0, 0);
10704                 VN_RELE(map_vp);
10705                 return (EIO);
10706         }
10707 
10708         VN_RELE(map_vp);
10709 
10710         oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10711         if (!oop) {
10712                 nfs4_error_t e;
10713 
10714                 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10715                     "no open owner"));
10716                 /*
10717                  * If there's an error, ignore it,
10718                  * and let VOP_INACTIVE handle it.
10719                  */
10720                 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10721                     CLOSE_NORM, 0, 0, 0);
10722                 return (EIO);
10723         }
10724         osp = find_open_stream(oop, rp);
10725         open_owner_rele(oop);
10726         *ospp = osp;
10727         return (0);
10728 }
10729 
10730 /*
10731  * Please be aware that when this function is called, the address space write
10732  * a_lock is held.  Do not put over the wire calls in this function.
10733  */
10734 /* ARGSUSED */
10735 static int
10736 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10737     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10738     caller_context_t *ct)
10739 {
10740         rnode4_t                *rp;
10741         int                     error = 0;
10742         mntinfo4_t              *mi;
10743 
10744         mi = VTOMI4(vp);
10745         rp = VTOR4(vp);
10746 
10747         if (nfs_zone() != mi->mi_zone)
10748                 return (EIO);
10749         if (vp->v_flag & VNOMAP)
10750                 return (ENOSYS);
10751 
10752         /*
10753          * Don't need to update the open stream first, since this
10754          * mmap can't add any additional share access that isn't
10755          * already contained in the open stream (for the case where we
10756          * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10757          * take into account os_mmap_read[write] counts).
10758          */
10759         atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10760 
10761         if (vp->v_type == VREG) {
10762                 /*
10763                  * We need to retrieve the open stream and update the counts.
10764                  * If there is no open stream here, something is wrong.
10765                  */
10766                 nfs4_open_stream_t      *osp = NULL;
10767                 nfs4_open_owner_t       *oop = NULL;
10768 
10769                 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10770                 if (oop != NULL) {
10771                         /* returns with 'os_sync_lock' held */
10772                         osp = find_open_stream(oop, rp);
10773                         open_owner_rele(oop);
10774                 }
10775                 if (osp == NULL) {
10776                         NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10777                             "nfs4_addmap: we should have an osp"
10778                             "but we don't, so fail with EIO"));
10779                         error = EIO;
10780                         goto out;
10781                 }
10782 
10783                 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10784                     " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10785 
10786                 /*
10787                  * Update the map count in the open stream.
10788                  * This is necessary in the case where we
10789                  * open/mmap/close/, then the server reboots, and we
10790                  * attempt to reopen.  If the mmap doesn't add share
10791                  * access then we send an invalid reopen with
10792                  * access = NONE.
10793                  *
10794                  * We need to specifically check each PROT_* so a mmap
10795                  * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10796                  * read and write access.  A simple comparison of prot
10797                  * to ~PROT_WRITE to determine read access is insufficient
10798                  * since prot can be |= with PROT_USER, etc.
10799                  */
10800 
10801                 /*
10802                  * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10803                  */
10804                 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10805                         osp->os_mmap_write += btopr(len);
10806                 if (maxprot & PROT_READ)
10807                         osp->os_mmap_read += btopr(len);
10808                 if (maxprot & PROT_EXEC)
10809                         osp->os_mmap_read += btopr(len);
10810                 /*
10811                  * Ensure that os_mmap_read gets incremented, even if
10812                  * maxprot were to look like PROT_NONE.
10813                  */
10814                 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10815                     !(maxprot & PROT_EXEC))
10816                         osp->os_mmap_read += btopr(len);
10817                 osp->os_mapcnt += btopr(len);
10818                 mutex_exit(&osp->os_sync_lock);
10819                 open_stream_rele(osp, rp);
10820         }
10821 
10822 out:
10823         /*
10824          * If we got an error, then undo our
10825          * incrementing of 'r_mapcnt'.
10826          */
10827 
10828         if (error) {
10829                 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10830                 ASSERT(rp->r_mapcnt >= 0);
10831         }
10832         return (error);
10833 }
10834 
10835 /* ARGSUSED */
10836 static int
10837 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10838 {
10839 
10840         return (VTOR4(vp1) == VTOR4(vp2));
10841 }
10842 
10843 /* ARGSUSED */
10844 static int
10845 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10846     offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10847     caller_context_t *ct)
10848 {
10849         int rc;
10850         u_offset_t start, end;
10851         rnode4_t *rp;
10852         int error = 0, intr = INTR4(vp);
10853         nfs4_error_t e;
10854 
10855         if (nfs_zone() != VTOMI4(vp)->mi_zone)
10856                 return (EIO);
10857 
10858         /* check for valid cmd parameter */
10859         if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10860                 return (EINVAL);
10861 
10862         /* Verify l_type. */
10863         switch (bfp->l_type) {
10864         case F_RDLCK:
10865                 if (cmd != F_GETLK && !(flag & FREAD))
10866                         return (EBADF);
10867                 break;
10868         case F_WRLCK:
10869                 if (cmd != F_GETLK && !(flag & FWRITE))
10870                         return (EBADF);
10871                 break;
10872         case F_UNLCK:
10873                 intr = 0;
10874                 break;
10875 
10876         default:
10877                 return (EINVAL);
10878         }
10879 
10880         /* check the validity of the lock range */
10881         if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10882                 return (rc);
10883         if (rc = flk_check_lock_data(start, end, MAXEND))
10884                 return (rc);
10885 
10886         /*
10887          * If the filesystem is mounted using local locking, pass the
10888          * request off to the local locking code.
10889          */
10890         if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10891                 if (cmd == F_SETLK || cmd == F_SETLKW) {
10892                         /*
10893                          * For complete safety, we should be holding
10894                          * r_lkserlock.  However, we can't call
10895                          * nfs4_safelock and then fs_frlock while
10896                          * holding r_lkserlock, so just invoke
10897                          * nfs4_safelock and expect that this will
10898                          * catch enough of the cases.
10899                          */
10900                         if (!nfs4_safelock(vp, bfp, cr))
10901                                 return (EAGAIN);
10902                 }
10903                 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10904         }
10905 
10906         rp = VTOR4(vp);
10907 
10908         /*
10909          * Check whether the given lock request can proceed, given the
10910          * current file mappings.
10911          */
10912         if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10913                 return (EINTR);
10914         if (cmd == F_SETLK || cmd == F_SETLKW) {
10915                 if (!nfs4_safelock(vp, bfp, cr)) {
10916                         rc = EAGAIN;
10917                         goto done;
10918                 }
10919         }
10920 
10921         /*
10922          * Flush the cache after waiting for async I/O to finish.  For new
10923          * locks, this is so that the process gets the latest bits from the
10924          * server.  For unlocks, this is so that other clients see the
10925          * latest bits once the file has been unlocked.  If currently dirty
10926          * pages can't be flushed, then don't allow a lock to be set.  But
10927          * allow unlocks to succeed, to avoid having orphan locks on the
10928          * server.
10929          */
10930         if (cmd != F_GETLK) {
10931                 mutex_enter(&rp->r_statelock);
10932                 while (rp->r_count > 0) {
10933                         if (intr) {
10934                                 klwp_t *lwp = ttolwp(curthread);
10935 
10936                                 if (lwp != NULL)
10937                                         lwp->lwp_nostop++;
10938                                 if (cv_wait_sig(&rp->r_cv,
10939                                     &rp->r_statelock) == 0) {
10940                                         if (lwp != NULL)
10941                                                 lwp->lwp_nostop--;
10942                                         rc = EINTR;
10943                                         break;
10944                                 }
10945                                 if (lwp != NULL)
10946                                         lwp->lwp_nostop--;
10947                                 } else
10948                                         cv_wait(&rp->r_cv, &rp->r_statelock);
10949                 }
10950                 mutex_exit(&rp->r_statelock);
10951                 if (rc != 0)
10952                         goto done;
10953                 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10954                 if (error) {
10955                         if (error == ENOSPC || error == EDQUOT) {
10956                                 mutex_enter(&rp->r_statelock);
10957                                 if (!rp->r_error)
10958                                         rp->r_error = error;
10959                                 mutex_exit(&rp->r_statelock);
10960                         }
10961                         if (bfp->l_type != F_UNLCK) {
10962                                 rc = ENOLCK;
10963                                 goto done;
10964                         }
10965                 }
10966         }
10967 
10968         /*
10969          * Call the lock manager to do the real work of contacting
10970          * the server and obtaining the lock.
10971          */
10972         nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10973             cr, &e, NULL, NULL);
10974         rc = e.error;
10975 
10976         if (rc == 0)
10977                 nfs4_lockcompletion(vp, cmd);
10978 
10979 done:
10980         nfs_rw_exit(&rp->r_lkserlock);
10981 
10982         return (rc);
10983 }
10984 
10985 /*
10986  * Free storage space associated with the specified vnode.  The portion
10987  * to be freed is specified by bfp->l_start and bfp->l_len (already
10988  * normalized to a "whence" of 0).
10989  *
10990  * This is an experimental facility whose continued existence is not
10991  * guaranteed.  Currently, we only support the special case
10992  * of l_len == 0, meaning free to end of file.
10993  */
10994 /* ARGSUSED */
10995 static int
10996 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10997     offset_t offset, cred_t *cr, caller_context_t *ct)
10998 {
10999         int error;
11000 
11001         if (nfs_zone() != VTOMI4(vp)->mi_zone)
11002                 return (EIO);
11003         ASSERT(vp->v_type == VREG);
11004         if (cmd != F_FREESP)
11005                 return (EINVAL);
11006 
11007         error = convoff(vp, bfp, 0, offset);
11008         if (!error) {
11009                 ASSERT(bfp->l_start >= 0);
11010                 if (bfp->l_len == 0) {
11011                         struct vattr va;
11012 
11013                         va.va_mask = AT_SIZE;
11014                         va.va_size = bfp->l_start;
11015                         error = nfs4setattr(vp, &va, 0, cr, NULL);
11016 
11017                         if (error == 0 && bfp->l_start == 0)
11018                                 vnevent_truncate(vp, ct);
11019                 } else
11020                         error = EINVAL;
11021         }
11022 
11023         return (error);
11024 }
11025 
11026 /* ARGSUSED */
11027 int
11028 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11029 {
11030         rnode4_t *rp;
11031         rp = VTOR4(vp);
11032 
11033         if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11034                 vp = RTOV4(rp);
11035         }
11036         *vpp = vp;
11037         return (0);
11038 }
11039 
11040 /*
11041  * Setup and add an address space callback to do the work of the delmap call.
11042  * The callback will (and must be) deleted in the actual callback function.
11043  *
11044  * This is done in order to take care of the problem that we have with holding
11045  * the address space's a_lock for a long period of time (e.g. if the NFS server
11046  * is down).  Callbacks will be executed in the address space code while the
11047  * a_lock is not held.  Holding the address space's a_lock causes things such
11048  * as ps and fork to hang because they are trying to acquire this lock as well.
11049  */
11050 /* ARGSUSED */
11051 static int
11052 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11053     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11054     caller_context_t *ct)
11055 {
11056         int                     caller_found;
11057         int                     error;
11058         rnode4_t                *rp;
11059         nfs4_delmap_args_t      *dmapp;
11060         nfs4_delmapcall_t       *delmap_call;
11061 
11062         if (vp->v_flag & VNOMAP)
11063                 return (ENOSYS);
11064 
11065         /*
11066          * A process may not change zones if it has NFS pages mmap'ed
11067          * in, so we can't legitimately get here from the wrong zone.
11068          */
11069         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11070 
11071         rp = VTOR4(vp);
11072 
11073         /*
11074          * The way that the address space of this process deletes its mapping
11075          * of this file is via the following call chains:
11076          * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11077          * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11078          *
11079          * With the use of address space callbacks we are allowed to drop the
11080          * address space lock, a_lock, while executing the NFS operations that
11081          * need to go over the wire.  Returning EAGAIN to the caller of this
11082          * function is what drives the execution of the callback that we add
11083          * below.  The callback will be executed by the address space code
11084          * after dropping the a_lock.  When the callback is finished, since
11085          * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11086          * is called again on the same segment to finish the rest of the work
11087          * that needs to happen during unmapping.
11088          *
11089          * This action of calling back into the segment driver causes
11090          * nfs4_delmap() to get called again, but since the callback was
11091          * already executed at this point, it already did the work and there
11092          * is nothing left for us to do.
11093          *
11094          * To Summarize:
11095          * - The first time nfs4_delmap is called by the current thread is when
11096          * we add the caller associated with this delmap to the delmap caller
11097          * list, add the callback, and return EAGAIN.
11098          * - The second time in this call chain when nfs4_delmap is called we
11099          * will find this caller in the delmap caller list and realize there
11100          * is no more work to do thus removing this caller from the list and
11101          * returning the error that was set in the callback execution.
11102          */
11103         caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11104         if (caller_found) {
11105                 /*
11106                  * 'error' is from the actual delmap operations.  To avoid
11107                  * hangs, we need to handle the return of EAGAIN differently
11108                  * since this is what drives the callback execution.
11109                  * In this case, we don't want to return EAGAIN and do the
11110                  * callback execution because there are none to execute.
11111                  */
11112                 if (error == EAGAIN)
11113                         return (0);
11114                 else
11115                         return (error);
11116         }
11117 
11118         /* current caller was not in the list */
11119         delmap_call = nfs4_init_delmapcall();
11120 
11121         mutex_enter(&rp->r_statelock);
11122         list_insert_tail(&rp->r_indelmap, delmap_call);
11123         mutex_exit(&rp->r_statelock);
11124 
11125         dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11126 
11127         dmapp->vp = vp;
11128         dmapp->off = off;
11129         dmapp->addr = addr;
11130         dmapp->len = len;
11131         dmapp->prot = prot;
11132         dmapp->maxprot = maxprot;
11133         dmapp->flags = flags;
11134         dmapp->cr = cr;
11135         dmapp->caller = delmap_call;
11136 
11137         error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11138             AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11139 
11140         return (error ? error : EAGAIN);
11141 }
11142 
11143 static nfs4_delmapcall_t *
11144 nfs4_init_delmapcall()
11145 {
11146         nfs4_delmapcall_t       *delmap_call;
11147 
11148         delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11149         delmap_call->call_id = curthread;
11150         delmap_call->error = 0;
11151 
11152         return (delmap_call);
11153 }
11154 
11155 static void
11156 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11157 {
11158         kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11159 }
11160 
11161 /*
11162  * Searches for the current delmap caller (based on curthread) in the list of
11163  * callers.  If it is found, we remove it and free the delmap caller.
11164  * Returns:
11165  *      0 if the caller wasn't found
11166  *      1 if the caller was found, removed and freed.  *errp will be set
11167  *      to what the result of the delmap was.
11168  */
11169 static int
11170 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11171 {
11172         nfs4_delmapcall_t       *delmap_call;
11173 
11174         /*
11175          * If the list doesn't exist yet, we create it and return
11176          * that the caller wasn't found.  No list = no callers.
11177          */
11178         mutex_enter(&rp->r_statelock);
11179         if (!(rp->r_flags & R4DELMAPLIST)) {
11180                 /* The list does not exist */
11181                 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11182                     offsetof(nfs4_delmapcall_t, call_node));
11183                 rp->r_flags |= R4DELMAPLIST;
11184                 mutex_exit(&rp->r_statelock);
11185                 return (0);
11186         } else {
11187                 /* The list exists so search it */
11188                 for (delmap_call = list_head(&rp->r_indelmap);
11189                     delmap_call != NULL;
11190                     delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11191                         if (delmap_call->call_id == curthread) {
11192                                 /* current caller is in the list */
11193                                 *errp = delmap_call->error;
11194                                 list_remove(&rp->r_indelmap, delmap_call);
11195                                 mutex_exit(&rp->r_statelock);
11196                                 nfs4_free_delmapcall(delmap_call);
11197                                 return (1);
11198                         }
11199                 }
11200         }
11201         mutex_exit(&rp->r_statelock);
11202         return (0);
11203 }
11204 
11205 /*
11206  * Remove some pages from an mmap'd vnode.  Just update the
11207  * count of pages.  If doing close-to-open, then flush and
11208  * commit all of the pages associated with this file.
11209  * Otherwise, start an asynchronous page flush to write out
11210  * any dirty pages.  This will also associate a credential
11211  * with the rnode which can be used to write the pages.
11212  */
11213 /* ARGSUSED */
11214 static void
11215 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11216 {
11217         nfs4_error_t            e = { 0, NFS4_OK, RPC_SUCCESS };
11218         rnode4_t                *rp;
11219         mntinfo4_t              *mi;
11220         nfs4_delmap_args_t      *dmapp = (nfs4_delmap_args_t *)arg;
11221 
11222         rp = VTOR4(dmapp->vp);
11223         mi = VTOMI4(dmapp->vp);
11224 
11225         atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11226         ASSERT(rp->r_mapcnt >= 0);
11227 
11228         /*
11229          * Initiate a page flush and potential commit if there are
11230          * pages, the file system was not mounted readonly, the segment
11231          * was mapped shared, and the pages themselves were writeable.
11232          */
11233         if (nfs4_has_pages(dmapp->vp) &&
11234             !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11235             dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11236                 mutex_enter(&rp->r_statelock);
11237                 rp->r_flags |= R4DIRTY;
11238                 mutex_exit(&rp->r_statelock);
11239                 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11240                     dmapp->len, dmapp->cr);
11241                 if (!e.error) {
11242                         mutex_enter(&rp->r_statelock);
11243                         e.error = rp->r_error;
11244                         rp->r_error = 0;
11245                         mutex_exit(&rp->r_statelock);
11246                 }
11247         } else
11248                 e.error = 0;
11249 
11250         if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11251                 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11252                     B_INVAL, dmapp->cr, NULL);
11253 
11254         if (e.error) {
11255                 e.stat = puterrno4(e.error);
11256                 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11257                     OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11258                 dmapp->caller->error = e.error;
11259         }
11260 
11261         /* Check to see if we need to close the file */
11262 
11263         if (dmapp->vp->v_type == VREG) {
11264                 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11265                     CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11266 
11267                 if (e.error != 0 || e.stat != NFS4_OK) {
11268                         /*
11269                          * Since it is possible that e.error == 0 and
11270                          * e.stat != NFS4_OK (and vice versa),
11271                          * we do the proper checking in order to get both
11272                          * e.error and e.stat reporting the correct info.
11273                          */
11274                         if (e.stat == NFS4_OK)
11275                                 e.stat = puterrno4(e.error);
11276                         if (e.error == 0)
11277                                 e.error = geterrno4(e.stat);
11278 
11279                         nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11280                             OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11281                         dmapp->caller->error = e.error;
11282                 }
11283         }
11284 
11285         (void) as_delete_callback(as, arg);
11286         kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11287 }
11288 
11289 
11290 static uint_t
11291 fattr4_maxfilesize_to_bits(uint64_t ll)
11292 {
11293         uint_t l = 1;
11294 
11295         if (ll == 0) {
11296                 return (0);
11297         }
11298 
11299         if (ll & 0xffffffff00000000) {
11300                 l += 32; ll >>= 32;
11301         }
11302         if (ll & 0xffff0000) {
11303                 l += 16; ll >>= 16;
11304         }
11305         if (ll & 0xff00) {
11306                 l += 8; ll >>= 8;
11307         }
11308         if (ll & 0xf0) {
11309                 l += 4; ll >>= 4;
11310         }
11311         if (ll & 0xc) {
11312                 l += 2; ll >>= 2;
11313         }
11314         if (ll & 0x2) {
11315                 l += 1;
11316         }
11317         return (l);
11318 }
11319 
11320 static int
11321 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11322 {
11323         vnode_t *avp = NULL;
11324         int error;
11325 
11326         if ((error = nfs4lookup_xattr(vp, "", &avp,
11327             LOOKUP_XATTR, cr)) == 0)
11328                 error = do_xattr_exists_check(avp, valp, cr);
11329         if (avp)
11330                 VN_RELE(avp);
11331 
11332         return (error);
11333 }
11334 
11335 /* ARGSUSED */
11336 int
11337 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11338         caller_context_t *ct)
11339 {
11340         int error;
11341         hrtime_t t;
11342         rnode4_t *rp;
11343         nfs4_ga_res_t gar;
11344         nfs4_ga_ext_res_t ger;
11345 
11346         gar.n4g_ext_res = &ger;
11347 
11348         if (nfs_zone() != VTOMI4(vp)->mi_zone)
11349                 return (EIO);
11350         if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11351                 *valp = MAXPATHLEN;
11352                 return (0);
11353         }
11354         if (cmd == _PC_ACL_ENABLED) {
11355                 *valp = _ACL_ACE_ENABLED;
11356                 return (0);
11357         }
11358 
11359         rp = VTOR4(vp);
11360         if (cmd == _PC_XATTR_EXISTS) {
11361                 /*
11362                  * The existence of the xattr directory is not sufficient
11363                  * for determining whether generic user attributes exists.
11364                  * The attribute directory could only be a transient directory
11365                  * used for Solaris sysattr support.  Do a small readdir
11366                  * to verify if the only entries are sysattrs or not.
11367                  *
11368                  * pc4_xattr_valid can be only be trusted when r_xattr_dir
11369                  * is NULL.  Once the xadir vp exists, we can create xattrs,
11370                  * and we don't have any way to update the "base" object's
11371                  * pc4_xattr_exists from the xattr or xadir.  Maybe FEM
11372                  * could help out.
11373                  */
11374                 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11375                     rp->r_xattr_dir == NULL) {
11376                         return (nfs4_have_xattrs(vp, valp, cr));
11377                 }
11378         } else {  /* OLD CODE */
11379                 if (ATTRCACHE4_VALID(vp)) {
11380                         mutex_enter(&rp->r_statelock);
11381                         if (rp->r_pathconf.pc4_cache_valid) {
11382                                 error = 0;
11383                                 switch (cmd) {
11384                                 case _PC_FILESIZEBITS:
11385                                         *valp =
11386                                             rp->r_pathconf.pc4_filesizebits;
11387                                         break;
11388                                 case _PC_LINK_MAX:
11389                                         *valp =
11390                                             rp->r_pathconf.pc4_link_max;
11391                                         break;
11392                                 case _PC_NAME_MAX:
11393                                         *valp =
11394                                             rp->r_pathconf.pc4_name_max;
11395                                         break;
11396                                 case _PC_CHOWN_RESTRICTED:
11397                                         *valp =
11398                                             rp->r_pathconf.pc4_chown_restricted;
11399                                         break;
11400                                 case _PC_NO_TRUNC:
11401                                         *valp =
11402                                             rp->r_pathconf.pc4_no_trunc;
11403                                         break;
11404                                 default:
11405                                         error = EINVAL;
11406                                         break;
11407                                 }
11408                                 mutex_exit(&rp->r_statelock);
11409 #ifdef DEBUG
11410                                 nfs4_pathconf_cache_hits++;
11411 #endif
11412                                 return (error);
11413                         }
11414                         mutex_exit(&rp->r_statelock);
11415                 }
11416         }
11417 #ifdef DEBUG
11418         nfs4_pathconf_cache_misses++;
11419 #endif
11420 
11421         t = gethrtime();
11422 
11423         error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11424 
11425         if (error) {
11426                 mutex_enter(&rp->r_statelock);
11427                 rp->r_pathconf.pc4_cache_valid = FALSE;
11428                 rp->r_pathconf.pc4_xattr_valid = FALSE;
11429                 mutex_exit(&rp->r_statelock);
11430                 return (error);
11431         }
11432 
11433         /* interpret the max filesize */
11434         gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11435             fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11436 
11437         /* Store the attributes we just received */
11438         nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11439 
11440         switch (cmd) {
11441         case _PC_FILESIZEBITS:
11442                 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11443                 break;
11444         case _PC_LINK_MAX:
11445                 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11446                 break;
11447         case _PC_NAME_MAX:
11448                 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11449                 break;
11450         case _PC_CHOWN_RESTRICTED:
11451                 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11452                 break;
11453         case _PC_NO_TRUNC:
11454                 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11455                 break;
11456         case _PC_XATTR_EXISTS:
11457                 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11458                         if (error = nfs4_have_xattrs(vp, valp, cr))
11459                                 return (error);
11460                 }
11461                 break;
11462         default:
11463                 return (EINVAL);
11464         }
11465 
11466         return (0);
11467 }
11468 
11469 /*
11470  * Called by async thread to do synchronous pageio. Do the i/o, wait
11471  * for it to complete, and cleanup the page list when done.
11472  */
11473 static int
11474 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11475     int flags, cred_t *cr)
11476 {
11477         int error;
11478 
11479         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11480 
11481         error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11482         if (flags & B_READ)
11483                 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11484         else
11485                 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11486         return (error);
11487 }
11488 
11489 /* ARGSUSED */
11490 static int
11491 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11492         int flags, cred_t *cr, caller_context_t *ct)
11493 {
11494         int error;
11495         rnode4_t *rp;
11496 
11497         if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11498                 return (EIO);
11499 
11500         if (pp == NULL)
11501                 return (EINVAL);
11502 
11503         rp = VTOR4(vp);
11504         mutex_enter(&rp->r_statelock);
11505         rp->r_count++;
11506         mutex_exit(&rp->r_statelock);
11507 
11508         if (flags & B_ASYNC) {
11509                 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11510                     nfs4_sync_pageio);
11511         } else
11512                 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11513         mutex_enter(&rp->r_statelock);
11514         rp->r_count--;
11515         cv_broadcast(&rp->r_cv);
11516         mutex_exit(&rp->r_statelock);
11517         return (error);
11518 }
11519 
11520 /* ARGSUSED */
11521 static void
11522 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11523         caller_context_t *ct)
11524 {
11525         int error;
11526         rnode4_t *rp;
11527         page_t *plist;
11528         page_t *pptr;
11529         offset3 offset;
11530         count3 len;
11531         k_sigset_t smask;
11532 
11533         /*
11534          * We should get called with fl equal to either B_FREE or
11535          * B_INVAL.  Any other value is illegal.
11536          *
11537          * The page that we are either supposed to free or destroy
11538          * should be exclusive locked and its io lock should not
11539          * be held.
11540          */
11541         ASSERT(fl == B_FREE || fl == B_INVAL);
11542         ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11543 
11544         rp = VTOR4(vp);
11545 
11546         /*
11547          * If the page doesn't need to be committed or we shouldn't
11548          * even bother attempting to commit it, then just make sure
11549          * that the p_fsdata byte is clear and then either free or
11550          * destroy the page as appropriate.
11551          */
11552         if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11553                 pp->p_fsdata = C_NOCOMMIT;
11554                 if (fl == B_FREE)
11555                         page_free(pp, dn);
11556                 else
11557                         page_destroy(pp, dn);
11558                 return;
11559         }
11560 
11561         /*
11562          * If there is a page invalidation operation going on, then
11563          * if this is one of the pages being destroyed, then just
11564          * clear the p_fsdata byte and then either free or destroy
11565          * the page as appropriate.
11566          */
11567         mutex_enter(&rp->r_statelock);
11568         if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11569                 mutex_exit(&rp->r_statelock);
11570                 pp->p_fsdata = C_NOCOMMIT;
11571                 if (fl == B_FREE)
11572                         page_free(pp, dn);
11573                 else
11574                         page_destroy(pp, dn);
11575                 return;
11576         }
11577 
11578         /*
11579          * If we are freeing this page and someone else is already
11580          * waiting to do a commit, then just unlock the page and
11581          * return.  That other thread will take care of commiting
11582          * this page.  The page can be freed sometime after the
11583          * commit has finished.  Otherwise, if the page is marked
11584          * as delay commit, then we may be getting called from
11585          * pvn_write_done, one page at a time.   This could result
11586          * in one commit per page, so we end up doing lots of small
11587          * commits instead of fewer larger commits.  This is bad,
11588          * we want do as few commits as possible.
11589          */
11590         if (fl == B_FREE) {
11591                 if (rp->r_flags & R4COMMITWAIT) {
11592                         page_unlock(pp);
11593                         mutex_exit(&rp->r_statelock);
11594                         return;
11595                 }
11596                 if (pp->p_fsdata == C_DELAYCOMMIT) {
11597                         pp->p_fsdata = C_COMMIT;
11598                         page_unlock(pp);
11599                         mutex_exit(&rp->r_statelock);
11600                         return;
11601                 }
11602         }
11603 
11604         /*
11605          * Check to see if there is a signal which would prevent an
11606          * attempt to commit the pages from being successful.  If so,
11607          * then don't bother with all of the work to gather pages and
11608          * generate the unsuccessful RPC.  Just return from here and
11609          * let the page be committed at some later time.
11610          */
11611         sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11612         if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11613                 sigunintr(&smask);
11614                 page_unlock(pp);
11615                 mutex_exit(&rp->r_statelock);
11616                 return;
11617         }
11618         sigunintr(&smask);
11619 
11620         /*
11621          * We are starting to need to commit pages, so let's try
11622          * to commit as many as possible at once to reduce the
11623          * overhead.
11624          *
11625          * Set the `commit inprogress' state bit.  We must
11626          * first wait until any current one finishes.  Then
11627          * we initialize the c_pages list with this page.
11628          */
11629         while (rp->r_flags & R4COMMIT) {
11630                 rp->r_flags |= R4COMMITWAIT;
11631                 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11632                 rp->r_flags &= ~R4COMMITWAIT;
11633         }
11634         rp->r_flags |= R4COMMIT;
11635         mutex_exit(&rp->r_statelock);
11636         ASSERT(rp->r_commit.c_pages == NULL);
11637         rp->r_commit.c_pages = pp;
11638         rp->r_commit.c_commbase = (offset3)pp->p_offset;
11639         rp->r_commit.c_commlen = PAGESIZE;
11640 
11641         /*
11642          * Gather together all other pages which can be committed.
11643          * They will all be chained off r_commit.c_pages.
11644          */
11645         nfs4_get_commit(vp);
11646 
11647         /*
11648          * Clear the `commit inprogress' status and disconnect
11649          * the list of pages to be committed from the rnode.
11650          * At this same time, we also save the starting offset
11651          * and length of data to be committed on the server.
11652          */
11653         plist = rp->r_commit.c_pages;
11654         rp->r_commit.c_pages = NULL;
11655         offset = rp->r_commit.c_commbase;
11656         len = rp->r_commit.c_commlen;
11657         mutex_enter(&rp->r_statelock);
11658         rp->r_flags &= ~R4COMMIT;
11659         cv_broadcast(&rp->r_commit.c_cv);
11660         mutex_exit(&rp->r_statelock);
11661 
11662         if (curproc == proc_pageout || curproc == proc_fsflush ||
11663             nfs_zone() != VTOMI4(vp)->mi_zone) {
11664                 nfs4_async_commit(vp, plist, offset, len,
11665                     cr, do_nfs4_async_commit);
11666                 return;
11667         }
11668 
11669         /*
11670          * Actually generate the COMMIT op over the wire operation.
11671          */
11672         error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11673 
11674         /*
11675          * If we got an error during the commit, just unlock all
11676          * of the pages.  The pages will get retransmitted to the
11677          * server during a putpage operation.
11678          */
11679         if (error) {
11680                 while (plist != NULL) {
11681                         pptr = plist;
11682                         page_sub(&plist, pptr);
11683                         page_unlock(pptr);
11684                 }
11685                 return;
11686         }
11687 
11688         /*
11689          * We've tried as hard as we can to commit the data to stable
11690          * storage on the server.  We just unlock the rest of the pages
11691          * and clear the commit required state.  They will be put
11692          * onto the tail of the cachelist if they are nolonger
11693          * mapped.
11694          */
11695         while (plist != pp) {
11696                 pptr = plist;
11697                 page_sub(&plist, pptr);
11698                 pptr->p_fsdata = C_NOCOMMIT;
11699                 page_unlock(pptr);
11700         }
11701 
11702         /*
11703          * It is possible that nfs4_commit didn't return error but
11704          * some other thread has modified the page we are going
11705          * to free/destroy.
11706          *    In this case we need to rewrite the page. Do an explicit check
11707          * before attempting to free/destroy the page. If modified, needs to
11708          * be rewritten so unlock the page and return.
11709          */
11710         if (hat_ismod(pp)) {
11711                 pp->p_fsdata = C_NOCOMMIT;
11712                 page_unlock(pp);
11713                 return;
11714         }
11715 
11716         /*
11717          * Now, as appropriate, either free or destroy the page
11718          * that we were called with.
11719          */
11720         pp->p_fsdata = C_NOCOMMIT;
11721         if (fl == B_FREE)
11722                 page_free(pp, dn);
11723         else
11724                 page_destroy(pp, dn);
11725 }
11726 
11727 /*
11728  * Commit requires that the current fh be the file written to.
11729  * The compound op structure is:
11730  *      PUTFH(file), COMMIT
11731  */
11732 static int
11733 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11734 {
11735         COMPOUND4args_clnt args;
11736         COMPOUND4res_clnt res;
11737         COMMIT4res *cm_res;
11738         nfs_argop4 argop[2];
11739         nfs_resop4 *resop;
11740         int doqueue;
11741         mntinfo4_t *mi;
11742         rnode4_t *rp;
11743         cred_t *cred_otw = NULL;
11744         bool_t needrecov = FALSE;
11745         nfs4_recov_state_t recov_state;
11746         nfs4_open_stream_t *osp = NULL;
11747         bool_t first_time = TRUE;       /* first time getting OTW cred */
11748         bool_t last_time = FALSE;       /* last time getting OTW cred */
11749         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11750 
11751         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11752 
11753         rp = VTOR4(vp);
11754 
11755         mi = VTOMI4(vp);
11756         recov_state.rs_flags = 0;
11757         recov_state.rs_num_retry_despite_err = 0;
11758 get_commit_cred:
11759         /*
11760          * Releases the osp, if a valid open stream is provided.
11761          * Puts a hold on the cred_otw and the new osp (if found).
11762          */
11763         cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11764             &first_time, &last_time);
11765         args.ctag = TAG_COMMIT;
11766 recov_retry:
11767         /*
11768          * Commit ops: putfh file; commit
11769          */
11770         args.array_len = 2;
11771         args.array = argop;
11772 
11773         e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11774             &recov_state, NULL);
11775         if (e.error) {
11776                 crfree(cred_otw);
11777                 if (osp != NULL)
11778                         open_stream_rele(osp, rp);
11779                 return (e.error);
11780         }
11781 
11782         /* putfh directory */
11783         argop[0].argop = OP_CPUTFH;
11784         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11785 
11786         /* commit */
11787         argop[1].argop = OP_COMMIT;
11788         argop[1].nfs_argop4_u.opcommit.offset = offset;
11789         argop[1].nfs_argop4_u.opcommit.count = count;
11790 
11791         doqueue = 1;
11792         rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11793 
11794         needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11795         if (!needrecov && e.error) {
11796                 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11797                     needrecov);
11798                 crfree(cred_otw);
11799                 if (e.error == EACCES && last_time == FALSE)
11800                         goto get_commit_cred;
11801                 if (osp != NULL)
11802                         open_stream_rele(osp, rp);
11803                 return (e.error);
11804         }
11805 
11806         if (needrecov) {
11807                 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11808                     NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11809                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11810                             &recov_state, needrecov);
11811                         if (!e.error)
11812                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
11813                                     (caddr_t)&res);
11814                         goto recov_retry;
11815                 }
11816                 if (e.error) {
11817                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11818                             &recov_state, needrecov);
11819                         crfree(cred_otw);
11820                         if (osp != NULL)
11821                                 open_stream_rele(osp, rp);
11822                         return (e.error);
11823                 }
11824                 /* fall through for res.status case */
11825         }
11826 
11827         if (res.status) {
11828                 e.error = geterrno4(res.status);
11829                 if (e.error == EACCES && last_time == FALSE) {
11830                         crfree(cred_otw);
11831                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11832                             &recov_state, needrecov);
11833                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11834                         goto get_commit_cred;
11835                 }
11836                 /*
11837                  * Can't do a nfs4_purge_stale_fh here because this
11838                  * can cause a deadlock.  nfs4_commit can
11839                  * be called from nfs4_dispose which can be called
11840                  * indirectly via pvn_vplist_dirty.  nfs4_purge_stale_fh
11841                  * can call back to pvn_vplist_dirty.
11842                  */
11843                 if (e.error == ESTALE) {
11844                         mutex_enter(&rp->r_statelock);
11845                         rp->r_flags |= R4STALE;
11846                         if (!rp->r_error)
11847                                 rp->r_error = e.error;
11848                         mutex_exit(&rp->r_statelock);
11849                         PURGE_ATTRCACHE4(vp);
11850                 } else {
11851                         mutex_enter(&rp->r_statelock);
11852                         if (!rp->r_error)
11853                                 rp->r_error = e.error;
11854                         mutex_exit(&rp->r_statelock);
11855                 }
11856         } else {
11857                 ASSERT(rp->r_flags & R4HAVEVERF);
11858                 resop = &res.array[1];      /* commit res */
11859                 cm_res = &resop->nfs_resop4_u.opcommit;
11860                 mutex_enter(&rp->r_statelock);
11861                 if (cm_res->writeverf == rp->r_writeverf) {
11862                         mutex_exit(&rp->r_statelock);
11863                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11864                         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11865                             &recov_state, needrecov);
11866                         crfree(cred_otw);
11867                         if (osp != NULL)
11868                                 open_stream_rele(osp, rp);
11869                         return (0);
11870                 }
11871                 nfs4_set_mod(vp);
11872                 rp->r_writeverf = cm_res->writeverf;
11873                 mutex_exit(&rp->r_statelock);
11874                 e.error = NFS_VERF_MISMATCH;
11875         }
11876 
11877         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11878         nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11879         crfree(cred_otw);
11880         if (osp != NULL)
11881                 open_stream_rele(osp, rp);
11882 
11883         return (e.error);
11884 }
11885 
11886 static void
11887 nfs4_set_mod(vnode_t *vp)
11888 {
11889         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11890 
11891         /* make sure we're looking at the master vnode, not a shadow */
11892         pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11893 }
11894 
11895 /*
11896  * This function is used to gather a page list of the pages which
11897  * can be committed on the server.
11898  *
11899  * The calling thread must have set R4COMMIT.  This bit is used to
11900  * serialize access to the commit structure in the rnode.  As long
11901  * as the thread has set R4COMMIT, then it can manipulate the commit
11902  * structure without requiring any other locks.
11903  *
11904  * When this function is called from nfs4_dispose() the page passed
11905  * into nfs4_dispose() will be SE_EXCL locked, and so this function
11906  * will skip it. This is not a problem since we initially add the
11907  * page to the r_commit page list.
11908  *
11909  */
11910 static void
11911 nfs4_get_commit(vnode_t *vp)
11912 {
11913         rnode4_t *rp;
11914         page_t *pp;
11915         kmutex_t *vphm;
11916 
11917         rp = VTOR4(vp);
11918 
11919         ASSERT(rp->r_flags & R4COMMIT);
11920 
11921         /* make sure we're looking at the master vnode, not a shadow */
11922 
11923         if (IS_SHADOW(vp, rp))
11924                 vp = RTOV4(rp);
11925 
11926         vphm = page_vnode_mutex(vp);
11927         mutex_enter(vphm);
11928 
11929         /*
11930          * If there are no pages associated with this vnode, then
11931          * just return.
11932          */
11933         if ((pp = vp->v_pages) == NULL) {
11934                 mutex_exit(vphm);
11935                 return;
11936         }
11937 
11938         /*
11939          * Step through all of the pages associated with this vnode
11940          * looking for pages which need to be committed.
11941          */
11942         do {
11943                 /* Skip marker pages. */
11944                 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11945                         continue;
11946 
11947                 /*
11948                  * First short-cut everything (without the page_lock)
11949                  * and see if this page does not need to be committed
11950                  * or is modified if so then we'll just skip it.
11951                  */
11952                 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11953                         continue;
11954 
11955                 /*
11956                  * Attempt to lock the page.  If we can't, then
11957                  * someone else is messing with it or we have been
11958                  * called from nfs4_dispose and this is the page that
11959                  * nfs4_dispose was called with.. anyway just skip it.
11960                  */
11961                 if (!page_trylock(pp, SE_EXCL))
11962                         continue;
11963 
11964                 /*
11965                  * Lets check again now that we have the page lock.
11966                  */
11967                 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11968                         page_unlock(pp);
11969                         continue;
11970                 }
11971 
11972                 /* this had better not be a free page */
11973                 ASSERT(PP_ISFREE(pp) == 0);
11974 
11975                 /*
11976                  * The page needs to be committed and we locked it.
11977                  * Update the base and length parameters and add it
11978                  * to r_pages.
11979                  */
11980                 if (rp->r_commit.c_pages == NULL) {
11981                         rp->r_commit.c_commbase = (offset3)pp->p_offset;
11982                         rp->r_commit.c_commlen = PAGESIZE;
11983                 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11984                         rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11985                             (offset3)pp->p_offset + rp->r_commit.c_commlen;
11986                         rp->r_commit.c_commbase = (offset3)pp->p_offset;
11987                 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11988                     <= pp->p_offset) {
11989                         rp->r_commit.c_commlen = (offset3)pp->p_offset -
11990                             rp->r_commit.c_commbase + PAGESIZE;
11991                 }
11992                 page_add(&rp->r_commit.c_pages, pp);
11993         } while ((pp = pp->p_vpnext) != vp->v_pages);
11994 
11995         mutex_exit(vphm);
11996 }
11997 
11998 /*
11999  * This routine is used to gather together a page list of the pages
12000  * which are to be committed on the server.  This routine must not
12001  * be called if the calling thread holds any locked pages.
12002  *
12003  * The calling thread must have set R4COMMIT.  This bit is used to
12004  * serialize access to the commit structure in the rnode.  As long
12005  * as the thread has set R4COMMIT, then it can manipulate the commit
12006  * structure without requiring any other locks.
12007  */
12008 static void
12009 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
12010 {
12011 
12012         rnode4_t *rp;
12013         page_t *pp;
12014         u_offset_t end;
12015         u_offset_t off;
12016         ASSERT(len != 0);
12017         rp = VTOR4(vp);
12018         ASSERT(rp->r_flags & R4COMMIT);
12019 
12020         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12021 
12022         /* make sure we're looking at the master vnode, not a shadow */
12023 
12024         if (IS_SHADOW(vp, rp))
12025                 vp = RTOV4(rp);
12026 
12027         /*
12028          * If there are no pages associated with this vnode, then
12029          * just return.
12030          */
12031         if ((pp = vp->v_pages) == NULL)
12032                 return;
12033         /*
12034          * Calculate the ending offset.
12035          */
12036         end = soff + len;
12037         for (off = soff; off < end; off += PAGESIZE) {
12038                 /*
12039                  * Lookup each page by vp, offset.
12040                  */
12041                 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12042                         continue;
12043                 /*
12044                  * If this page does not need to be committed or is
12045                  * modified, then just skip it.
12046                  */
12047                 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12048                         page_unlock(pp);
12049                         continue;
12050                 }
12051 
12052                 ASSERT(PP_ISFREE(pp) == 0);
12053                 /*
12054                  * The page needs to be committed and we locked it.
12055                  * Update the base and length parameters and add it
12056                  * to r_pages.
12057                  */
12058                 if (rp->r_commit.c_pages == NULL) {
12059                         rp->r_commit.c_commbase = (offset3)pp->p_offset;
12060                         rp->r_commit.c_commlen = PAGESIZE;
12061                 } else {
12062                         rp->r_commit.c_commlen = (offset3)pp->p_offset -
12063                             rp->r_commit.c_commbase + PAGESIZE;
12064                 }
12065                 page_add(&rp->r_commit.c_pages, pp);
12066         }
12067 }
12068 
12069 /*
12070  * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12071  * Flushes and commits data to the server.
12072  */
12073 static int
12074 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12075 {
12076         int error;
12077         verifier4 write_verf;
12078         rnode4_t *rp = VTOR4(vp);
12079 
12080         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12081 
12082         /*
12083          * Flush the data portion of the file and then commit any
12084          * portions which need to be committed.  This may need to
12085          * be done twice if the server has changed state since
12086          * data was last written.  The data will need to be
12087          * rewritten to the server and then a new commit done.
12088          *
12089          * In fact, this may need to be done several times if the
12090          * server is having problems and crashing while we are
12091          * attempting to do this.
12092          */
12093 
12094 top:
12095         /*
12096          * Do a flush based on the poff and plen arguments.  This
12097          * will synchronously write out any modified pages in the
12098          * range specified by (poff, plen). This starts all of the
12099          * i/o operations which will be waited for in the next
12100          * call to nfs4_putpage
12101          */
12102 
12103         mutex_enter(&rp->r_statelock);
12104         write_verf = rp->r_writeverf;
12105         mutex_exit(&rp->r_statelock);
12106 
12107         error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12108         if (error == EAGAIN)
12109                 error = 0;
12110 
12111         /*
12112          * Do a flush based on the poff and plen arguments.  This
12113          * will synchronously write out any modified pages in the
12114          * range specified by (poff, plen) and wait until all of
12115          * the asynchronous i/o's in that range are done as well.
12116          */
12117         if (!error)
12118                 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12119 
12120         if (error)
12121                 return (error);
12122 
12123         mutex_enter(&rp->r_statelock);
12124         if (rp->r_writeverf != write_verf) {
12125                 mutex_exit(&rp->r_statelock);
12126                 goto top;
12127         }
12128         mutex_exit(&rp->r_statelock);
12129 
12130         /*
12131          * Now commit any pages which might need to be committed.
12132          * If the error, NFS_VERF_MISMATCH, is returned, then
12133          * start over with the flush operation.
12134          */
12135         error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12136 
12137         if (error == NFS_VERF_MISMATCH)
12138                 goto top;
12139 
12140         return (error);
12141 }
12142 
12143 /*
12144  * nfs4_commit_vp()  will wait for other pending commits and
12145  * will either commit the whole file or a range, plen dictates
12146  * if we commit whole file. a value of zero indicates the whole
12147  * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12148  */
12149 static int
12150 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12151     cred_t *cr, int wait_on_writes)
12152 {
12153         rnode4_t *rp;
12154         page_t *plist;
12155         offset3 offset;
12156         count3 len;
12157 
12158         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12159 
12160         rp = VTOR4(vp);
12161 
12162         /*
12163          *  before we gather commitable pages make
12164          *  sure there are no outstanding async writes
12165          */
12166         if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12167                 mutex_enter(&rp->r_statelock);
12168                 while (rp->r_count > 0) {
12169                         cv_wait(&rp->r_cv, &rp->r_statelock);
12170                 }
12171                 mutex_exit(&rp->r_statelock);
12172         }
12173 
12174         /*
12175          * Set the `commit inprogress' state bit.  We must
12176          * first wait until any current one finishes.
12177          */
12178         mutex_enter(&rp->r_statelock);
12179         while (rp->r_flags & R4COMMIT) {
12180                 rp->r_flags |= R4COMMITWAIT;
12181                 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12182                 rp->r_flags &= ~R4COMMITWAIT;
12183         }
12184         rp->r_flags |= R4COMMIT;
12185         mutex_exit(&rp->r_statelock);
12186 
12187         /*
12188          * Gather all of the pages which need to be
12189          * committed.
12190          */
12191         if (plen == 0)
12192                 nfs4_get_commit(vp);
12193         else
12194                 nfs4_get_commit_range(vp, poff, plen);
12195 
12196         /*
12197          * Clear the `commit inprogress' bit and disconnect the
12198          * page list which was gathered by nfs4_get_commit.
12199          */
12200         plist = rp->r_commit.c_pages;
12201         rp->r_commit.c_pages = NULL;
12202         offset = rp->r_commit.c_commbase;
12203         len = rp->r_commit.c_commlen;
12204         mutex_enter(&rp->r_statelock);
12205         rp->r_flags &= ~R4COMMIT;
12206         cv_broadcast(&rp->r_commit.c_cv);
12207         mutex_exit(&rp->r_statelock);
12208 
12209         /*
12210          * If any pages need to be committed, commit them and
12211          * then unlock them so that they can be freed some
12212          * time later.
12213          */
12214         if (plist == NULL)
12215                 return (0);
12216 
12217         /*
12218          * No error occurred during the flush portion
12219          * of this operation, so now attempt to commit
12220          * the data to stable storage on the server.
12221          *
12222          * This will unlock all of the pages on the list.
12223          */
12224         return (nfs4_sync_commit(vp, plist, offset, len, cr));
12225 }
12226 
12227 static int
12228 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12229     cred_t *cr)
12230 {
12231         int error;
12232         page_t *pp;
12233 
12234         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12235 
12236         error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12237 
12238         /*
12239          * If we got an error, then just unlock all of the pages
12240          * on the list.
12241          */
12242         if (error) {
12243                 while (plist != NULL) {
12244                         pp = plist;
12245                         page_sub(&plist, pp);
12246                         page_unlock(pp);
12247                 }
12248                 return (error);
12249         }
12250         /*
12251          * We've tried as hard as we can to commit the data to stable
12252          * storage on the server.  We just unlock the pages and clear
12253          * the commit required state.  They will get freed later.
12254          */
12255         while (plist != NULL) {
12256                 pp = plist;
12257                 page_sub(&plist, pp);
12258                 pp->p_fsdata = C_NOCOMMIT;
12259                 page_unlock(pp);
12260         }
12261 
12262         return (error);
12263 }
12264 
12265 static void
12266 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12267     cred_t *cr)
12268 {
12269 
12270         (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12271 }
12272 
12273 /*ARGSUSED*/
12274 static int
12275 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12276         caller_context_t *ct)
12277 {
12278         int             error = 0;
12279         mntinfo4_t      *mi;
12280         vattr_t         va;
12281         vsecattr_t      nfsace4_vsap;
12282 
12283         mi = VTOMI4(vp);
12284         if (nfs_zone() != mi->mi_zone)
12285                 return (EIO);
12286         if (mi->mi_flags & MI4_ACL) {
12287                 /* if we have a delegation, return it */
12288                 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12289                         (void) nfs4delegreturn(VTOR4(vp),
12290                             NFS4_DR_REOPEN|NFS4_DR_PUSH);
12291 
12292                 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12293                     NFS4_ACL_SET);
12294                 if (error) /* EINVAL */
12295                         return (error);
12296 
12297                 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12298                         /*
12299                          * These are aclent_t type entries.
12300                          */
12301                         error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12302                             vp->v_type == VDIR, FALSE);
12303                         if (error)
12304                                 return (error);
12305                 } else {
12306                         /*
12307                          * These are ace_t type entries.
12308                          */
12309                         error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12310                             FALSE);
12311                         if (error)
12312                                 return (error);
12313                 }
12314                 bzero(&va, sizeof (va));
12315                 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12316                 vs_ace4_destroy(&nfsace4_vsap);
12317                 return (error);
12318         }
12319         return (ENOSYS);
12320 }
12321 
12322 /* ARGSUSED */
12323 int
12324 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12325         caller_context_t *ct)
12326 {
12327         int             error;
12328         mntinfo4_t      *mi;
12329         nfs4_ga_res_t   gar;
12330         rnode4_t        *rp = VTOR4(vp);
12331 
12332         mi = VTOMI4(vp);
12333         if (nfs_zone() != mi->mi_zone)
12334                 return (EIO);
12335 
12336         bzero(&gar, sizeof (gar));
12337         gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12338 
12339         /*
12340          * vsecattr->vsa_mask holds the original acl request mask.
12341          * This is needed when determining what to return.
12342          * (See: nfs4_create_getsecattr_return())
12343          */
12344         error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12345         if (error) /* EINVAL */
12346                 return (error);
12347 
12348         /*
12349          * If this is a referral stub, don't try to go OTW for an ACL
12350          */
12351         if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12352                 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12353 
12354         if (mi->mi_flags & MI4_ACL) {
12355                 /*
12356                  * Check if the data is cached and the cache is valid.  If it
12357                  * is we don't go over the wire.
12358                  */
12359                 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12360                         mutex_enter(&rp->r_statelock);
12361                         if (rp->r_secattr != NULL) {
12362                                 error = nfs4_create_getsecattr_return(
12363                                     rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12364                                     rp->r_attr.va_gid,
12365                                     vp->v_type == VDIR);
12366                                 if (!error) { /* error == 0 - Success! */
12367                                         mutex_exit(&rp->r_statelock);
12368                                         return (error);
12369                                 }
12370                         }
12371                         mutex_exit(&rp->r_statelock);
12372                 }
12373 
12374                 /*
12375                  * The getattr otw call will always get both the acl, in
12376                  * the form of a list of nfsace4's, and the number of acl
12377                  * entries; independent of the value of gar.n4g_vsa.vsa_mask.
12378                  */
12379                 gar.n4g_va.va_mask = AT_ALL;
12380                 error =  nfs4_getattr_otw(vp, &gar, cr, 1);
12381                 if (error) {
12382                         vs_ace4_destroy(&gar.n4g_vsa);
12383                         if (error == ENOTSUP || error == EOPNOTSUPP)
12384                                 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12385                         return (error);
12386                 }
12387 
12388                 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12389                         /*
12390                          * No error was returned, but according to the response
12391                          * bitmap, neither was an acl.
12392                          */
12393                         vs_ace4_destroy(&gar.n4g_vsa);
12394                         error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12395                         return (error);
12396                 }
12397 
12398                 /*
12399                  * Update the cache with the ACL.
12400                  */
12401                 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12402 
12403                 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12404                     vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12405                     vp->v_type == VDIR);
12406                 vs_ace4_destroy(&gar.n4g_vsa);
12407                 if ((error) && (vsecattr->vsa_mask &
12408                     (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12409                     (error != EACCES)) {
12410                         error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12411                 }
12412                 return (error);
12413         }
12414         error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12415         return (error);
12416 }
12417 
12418 /*
12419  * The function returns:
12420  *      - 0 (zero) if the passed in "acl_mask" is a valid request.
12421  *      - EINVAL if the passed in "acl_mask" is an invalid request.
12422  *
12423  * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12424  * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12425  *
12426  * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12427  * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12428  * - We have a count field set without the corresponding acl field set. (e.g. -
12429  * VSA_ACECNT is set, but VSA_ACE is not)
12430  */
12431 static int
12432 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12433 {
12434         /* Shortcut the masks that are always valid. */
12435         if (acl_mask == (VSA_ACE | VSA_ACECNT))
12436                 return (0);
12437         if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12438                 return (0);
12439 
12440         if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12441                 /*
12442                  * We can't have any VSA_ACL type stuff in the mask now.
12443                  */
12444                 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12445                     VSA_DFACLCNT))
12446                         return (EINVAL);
12447 
12448                 if (op == NFS4_ACL_SET) {
12449                         if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12450                                 return (EINVAL);
12451                 }
12452         }
12453 
12454         if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12455                 /*
12456                  * We can't have any VSA_ACE type stuff in the mask now.
12457                  */
12458                 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12459                         return (EINVAL);
12460 
12461                 if (op == NFS4_ACL_SET) {
12462                         if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12463                                 return (EINVAL);
12464 
12465                         if ((acl_mask & VSA_DFACLCNT) &&
12466                             !(acl_mask & VSA_DFACL))
12467                                 return (EINVAL);
12468                 }
12469         }
12470         return (0);
12471 }
12472 
12473 /*
12474  * The theory behind creating the correct getsecattr return is simply this:
12475  * "Don't return anything that the caller is not expecting to have to free."
12476  */
12477 static int
12478 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12479     uid_t uid, gid_t gid, int isdir)
12480 {
12481         int error = 0;
12482         /* Save the mask since the translators modify it. */
12483         uint_t  orig_mask = vsap->vsa_mask;
12484 
12485         if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12486                 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12487 
12488                 if (error)
12489                         return (error);
12490 
12491                 /*
12492                  * If the caller only asked for the ace count (VSA_ACECNT)
12493                  * don't give them the full acl (VSA_ACE), free it.
12494                  */
12495                 if (!orig_mask & VSA_ACE) {
12496                         if (vsap->vsa_aclentp != NULL) {
12497                                 kmem_free(vsap->vsa_aclentp,
12498                                     vsap->vsa_aclcnt * sizeof (ace_t));
12499                                 vsap->vsa_aclentp = NULL;
12500                         }
12501                 }
12502                 vsap->vsa_mask = orig_mask;
12503 
12504         } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12505             VSA_DFACLCNT)) {
12506                 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12507                     isdir, FALSE);
12508 
12509                 if (error)
12510                         return (error);
12511 
12512                 /*
12513                  * If the caller only asked for the acl count (VSA_ACLCNT)
12514                  * and/or the default acl count (VSA_DFACLCNT) don't give them
12515                  * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12516                  */
12517                 if (!orig_mask & VSA_ACL) {
12518                         if (vsap->vsa_aclentp != NULL) {
12519                                 kmem_free(vsap->vsa_aclentp,
12520                                     vsap->vsa_aclcnt * sizeof (aclent_t));
12521                                 vsap->vsa_aclentp = NULL;
12522                         }
12523                 }
12524 
12525                 if (!orig_mask & VSA_DFACL) {
12526                         if (vsap->vsa_dfaclentp != NULL) {
12527                                 kmem_free(vsap->vsa_dfaclentp,
12528                                     vsap->vsa_dfaclcnt * sizeof (aclent_t));
12529                                 vsap->vsa_dfaclentp = NULL;
12530                         }
12531                 }
12532                 vsap->vsa_mask = orig_mask;
12533         }
12534         return (0);
12535 }
12536 
12537 /* ARGSUSED */
12538 int
12539 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12540     caller_context_t *ct)
12541 {
12542         int error;
12543 
12544         if (nfs_zone() != VTOMI4(vp)->mi_zone)
12545                 return (EIO);
12546         /*
12547          * check for valid cmd parameter
12548          */
12549         if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12550                 return (EINVAL);
12551 
12552         /*
12553          * Check access permissions
12554          */
12555         if ((cmd & F_SHARE) &&
12556             (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12557             (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12558                 return (EBADF);
12559 
12560         /*
12561          * If the filesystem is mounted using local locking, pass the
12562          * request off to the local share code.
12563          */
12564         if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12565                 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12566 
12567         switch (cmd) {
12568         case F_SHARE:
12569         case F_UNSHARE:
12570                 /*
12571                  * This will be properly implemented later,
12572                  * see RFE: 4823948 .
12573                  */
12574                 error = EAGAIN;
12575                 break;
12576 
12577         case F_HASREMOTELOCKS:
12578                 /*
12579                  * NFS client can't store remote locks itself
12580                  */
12581                 shr->s_access = 0;
12582                 error = 0;
12583                 break;
12584 
12585         default:
12586                 error = EINVAL;
12587                 break;
12588         }
12589 
12590         return (error);
12591 }
12592 
12593 /*
12594  * Common code called by directory ops to update the attrcache
12595  */
12596 static int
12597 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12598     hrtime_t t, vnode_t *vp, cred_t *cr)
12599 {
12600         int error = 0;
12601 
12602         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12603 
12604         if (status != NFS4_OK) {
12605                 /* getattr not done or failed */
12606                 PURGE_ATTRCACHE4(vp);
12607                 return (error);
12608         }
12609 
12610         if (garp) {
12611                 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12612         } else {
12613                 PURGE_ATTRCACHE4(vp);
12614         }
12615         return (error);
12616 }
12617 
12618 /*
12619  * Update directory caches for directory modification ops (link, rename, etc.)
12620  * When dinfo is NULL, manage dircaches in the old way.
12621  */
12622 static void
12623 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12624     dirattr_info_t *dinfo)
12625 {
12626         rnode4_t        *drp = VTOR4(dvp);
12627 
12628         ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12629 
12630         /* Purge rddir cache for dir since it changed */
12631         if (drp->r_dir != NULL)
12632                 nfs4_purge_rddir_cache(dvp);
12633 
12634         /*
12635          * If caller provided dinfo, then use it to manage dir caches.
12636          */
12637         if (dinfo != NULL) {
12638                 if (vp != NULL) {
12639                         mutex_enter(&VTOR4(vp)->r_statev4_lock);
12640                         if (!VTOR4(vp)->created_v4) {
12641                                 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12642                                 dnlc_update(dvp, nm, vp);
12643                         } else {
12644                                 /*
12645                                  * XXX don't update if the created_v4 flag is
12646                                  * set
12647                                  */
12648                                 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12649                                 NFS4_DEBUG(nfs4_client_state_debug,
12650                                     (CE_NOTE, "nfs4_update_dircaches: "
12651                                     "don't update dnlc: created_v4 flag"));
12652                         }
12653                 }
12654 
12655                 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12656                     dinfo->di_cred, FALSE, cinfo);
12657 
12658                 return;
12659         }
12660 
12661         /*
12662          * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12663          * Since caller modified dir but didn't receive post-dirmod-op dir
12664          * attrs, the dir's attrs must be purged.
12665          *
12666          * XXX this check and dnlc update/purge should really be atomic,
12667          * XXX but can't use rnode statelock because it'll deadlock in
12668          * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12669          * XXX does occur.
12670          *
12671          * XXX We also may want to check that atomic is true in the
12672          * XXX change_info struct. If it is not, the change_info may
12673          * XXX reflect changes by more than one clients which means that
12674          * XXX our cache may not be valid.
12675          */
12676         PURGE_ATTRCACHE4(dvp);
12677         if (drp->r_change == cinfo->before) {
12678                 /* no changes took place in the directory prior to our link */
12679                 if (vp != NULL) {
12680                         mutex_enter(&VTOR4(vp)->r_statev4_lock);
12681                         if (!VTOR4(vp)->created_v4) {
12682                                 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12683                                 dnlc_update(dvp, nm, vp);
12684                         } else {
12685                                 /*
12686                                  * XXX dont' update if the created_v4 flag
12687                                  * is set
12688                                  */
12689                                 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12690                                 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12691                                     "nfs4_update_dircaches: don't"
12692                                     " update dnlc: created_v4 flag"));
12693                         }
12694                 }
12695         } else {
12696                 /* Another client modified directory - purge its dnlc cache */
12697                 dnlc_purge_vp(dvp);
12698         }
12699 }
12700 
12701 /*
12702  * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12703  * file.
12704  *
12705  * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12706  * file (ie: client recovery) and otherwise set to FALSE.
12707  *
12708  * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12709  * initiated) calling functions.
12710  *
12711  * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12712  * of resending a 'lost' open request.
12713  *
12714  * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12715  * server that hands out BAD_SEQID on open confirm.
12716  *
12717  * Errors are returned via the nfs4_error_t parameter.
12718  */
12719 void
12720 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12721     bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12722     bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12723 {
12724         COMPOUND4args_clnt args;
12725         COMPOUND4res_clnt res;
12726         nfs_argop4 argop[2];
12727         nfs_resop4 *resop;
12728         int doqueue = 1;
12729         mntinfo4_t *mi;
12730         OPEN_CONFIRM4args *open_confirm_args;
12731         int needrecov;
12732 
12733         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12734 #if DEBUG
12735         mutex_enter(&oop->oo_lock);
12736         ASSERT(oop->oo_seqid_inuse);
12737         mutex_exit(&oop->oo_lock);
12738 #endif
12739 
12740 recov_retry_confirm:
12741         nfs4_error_zinit(ep);
12742         *retry_open = FALSE;
12743 
12744         if (resend)
12745                 args.ctag = TAG_OPEN_CONFIRM_LOST;
12746         else
12747                 args.ctag = TAG_OPEN_CONFIRM;
12748 
12749         args.array_len = 2;
12750         args.array = argop;
12751 
12752         /* putfh target fh */
12753         argop[0].argop = OP_CPUTFH;
12754         argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12755 
12756         argop[1].argop = OP_OPEN_CONFIRM;
12757         open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12758 
12759         (*seqid) += 1;
12760         open_confirm_args->seqid = *seqid;
12761         open_confirm_args->open_stateid = *stateid;
12762 
12763         mi = VTOMI4(vp);
12764 
12765         rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12766 
12767         if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12768                 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12769         }
12770 
12771         needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12772         if (!needrecov && ep->error)
12773                 return;
12774 
12775         if (needrecov) {
12776                 bool_t abort = FALSE;
12777 
12778                 if (reopening_file == FALSE) {
12779                         nfs4_bseqid_entry_t *bsep = NULL;
12780 
12781                         if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12782                                 bsep = nfs4_create_bseqid_entry(oop, NULL,
12783                                     vp, 0, args.ctag,
12784                                     open_confirm_args->seqid);
12785 
12786                         abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12787                             NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12788                         if (bsep) {
12789                                 kmem_free(bsep, sizeof (*bsep));
12790                                 if (num_bseqid_retryp &&
12791                                     --(*num_bseqid_retryp) == 0)
12792                                         abort = TRUE;
12793                         }
12794                 }
12795                 if ((ep->error == ETIMEDOUT ||
12796                     res.status == NFS4ERR_RESOURCE) &&
12797                     abort == FALSE && resend == FALSE) {
12798                         if (!ep->error)
12799                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
12800                                     (caddr_t)&res);
12801 
12802                         delay(SEC_TO_TICK(confirm_retry_sec));
12803                         goto recov_retry_confirm;
12804                 }
12805                 /* State may have changed so retry the entire OPEN op */
12806                 if (abort == FALSE)
12807                         *retry_open = TRUE;
12808                 else
12809                         *retry_open = FALSE;
12810                 if (!ep->error)
12811                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12812                 return;
12813         }
12814 
12815         if (res.status) {
12816                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12817                 return;
12818         }
12819 
12820         resop = &res.array[1];  /* open confirm res */
12821         bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12822             stateid, sizeof (*stateid));
12823 
12824         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12825 }
12826 
12827 /*
12828  * Return the credentials associated with a client state object.  The
12829  * caller is responsible for freeing the credentials.
12830  */
12831 
12832 static cred_t *
12833 state_to_cred(nfs4_open_stream_t *osp)
12834 {
12835         cred_t *cr;
12836 
12837         /*
12838          * It's ok to not lock the open stream and open owner to get
12839          * the oo_cred since this is only written once (upon creation)
12840          * and will not change.
12841          */
12842         cr = osp->os_open_owner->oo_cred;
12843         crhold(cr);
12844 
12845         return (cr);
12846 }
12847 
12848 /*
12849  * nfs4_find_sysid
12850  *
12851  * Find the sysid for the knetconfig associated with the given mi.
12852  */
12853 static struct lm_sysid *
12854 nfs4_find_sysid(mntinfo4_t *mi)
12855 {
12856         ASSERT(nfs_zone() == mi->mi_zone);
12857 
12858         /*
12859          * Switch from RDMA knconf to original mount knconf
12860          */
12861         return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12862             mi->mi_curr_serv->sv_hostname, NULL));
12863 }
12864 
12865 #ifdef DEBUG
12866 /*
12867  * Return a string version of the call type for easy reading.
12868  */
12869 static char *
12870 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12871 {
12872         switch (ctype) {
12873         case NFS4_LCK_CTYPE_NORM:
12874                 return ("NORMAL");
12875         case NFS4_LCK_CTYPE_RECLAIM:
12876                 return ("RECLAIM");
12877         case NFS4_LCK_CTYPE_RESEND:
12878                 return ("RESEND");
12879         case NFS4_LCK_CTYPE_REINSTATE:
12880                 return ("REINSTATE");
12881         default:
12882                 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12883                     "type %d", ctype);
12884                 return ("");
12885         }
12886 }
12887 #endif
12888 
12889 /*
12890  * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12891  * Unlock requests don't have an over-the-wire locktype, so we just return
12892  * something non-threatening.
12893  */
12894 
12895 static nfs_lock_type4
12896 flk_to_locktype(int cmd, int l_type)
12897 {
12898         ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12899 
12900         switch (l_type) {
12901         case F_UNLCK:
12902                 return (READ_LT);
12903         case F_RDLCK:
12904                 if (cmd == F_SETLK)
12905                         return (READ_LT);
12906                 else
12907                         return (READW_LT);
12908         case F_WRLCK:
12909                 if (cmd == F_SETLK)
12910                         return (WRITE_LT);
12911                 else
12912                         return (WRITEW_LT);
12913         }
12914         panic("flk_to_locktype");
12915         /*NOTREACHED*/
12916 }
12917 
12918 /*
12919  * Do some preliminary checks for nfs4frlock.
12920  */
12921 static int
12922 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12923     u_offset_t offset)
12924 {
12925         int error = 0;
12926 
12927         /*
12928          * If we are setting a lock, check that the file is opened
12929          * with the correct mode.
12930          */
12931         if (cmd == F_SETLK || cmd == F_SETLKW) {
12932                 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12933                     (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12934                         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12935                             "nfs4frlock_validate_args: file was opened with "
12936                             "incorrect mode"));
12937                         return (EBADF);
12938                 }
12939         }
12940 
12941         /* Convert the offset. It may need to be restored before returning. */
12942         if (error = convoff(vp, flk, 0, offset)) {
12943                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12944                     "nfs4frlock_validate_args: convoff  =>  error= %d\n",
12945                     error));
12946                 return (error);
12947         }
12948 
12949         return (error);
12950 }
12951 
12952 /*
12953  * Set the flock64's lm_sysid for nfs4frlock.
12954  */
12955 static int
12956 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12957 {
12958         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12959 
12960         /* Find the lm_sysid */
12961         *lspp = nfs4_find_sysid(VTOMI4(vp));
12962 
12963         if (*lspp == NULL) {
12964                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12965                     "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12966                 return (ENOLCK);
12967         }
12968 
12969         flk->l_sysid = lm_sysidt(*lspp);
12970 
12971         return (0);
12972 }
12973 
12974 /*
12975  * Do the remaining preliminary setup for nfs4frlock.
12976  */
12977 static void
12978 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12979     flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12980     cred_t **cred_otw)
12981 {
12982         /*
12983          * set tick_delay to the base delay time.
12984          * (nfs4_base_wait_time is in msecs)
12985          */
12986 
12987         *tick_delayp = drv_usectohz(nfs4_base_wait_time * 1000);
12988 
12989         /*
12990          * If lock is relative to EOF, we need the newest length of the
12991          * file. Therefore invalidate the ATTR_CACHE.
12992          */
12993 
12994         *whencep = flk->l_whence;
12995 
12996         if (*whencep == 2)              /* SEEK_END */
12997                 PURGE_ATTRCACHE4(vp);
12998 
12999         recov_statep->rs_flags = 0;
13000         recov_statep->rs_num_retry_despite_err = 0;
13001         *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
13002 }
13003 
13004 /*
13005  * Initialize and allocate the data structures necessary for
13006  * the nfs4frlock call.
13007  * Allocates argsp's op array, frees up the saved_rqstpp if there is one.
13008  */
13009 static void
13010 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
13011     nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13012     bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13013     bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13014 {
13015         int             argoplist_size;
13016         int             num_ops = 2;
13017 
13018         *retry = FALSE;
13019         *did_start_fop = FALSE;
13020         *skip_get_err = FALSE;
13021         lost_rqstp->lr_op = 0;
13022         argoplist_size  = num_ops * sizeof (nfs_argop4);
13023         /* fill array with zero */
13024         *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13025 
13026         *argspp = argsp;
13027         *respp = NULL;
13028 
13029         argsp->array_len = num_ops;
13030         argsp->array = *argopp;
13031 
13032         /* initialize in case of error; will get real value down below */
13033         argsp->ctag = TAG_NONE;
13034 
13035         if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13036                 *op_hintp = OH_LOCKU;
13037         else
13038                 *op_hintp = OH_OTHER;
13039 }
13040 
13041 /*
13042  * Call the nfs4_start_fop() for nfs4frlock, if necessary.  Assign
13043  * the proper nfs4_server_t for this instance of nfs4frlock.
13044  * Returns 0 (success) or an errno value.
13045  */
13046 static int
13047 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13048     nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13049     bool_t *did_start_fop, bool_t *startrecovp)
13050 {
13051         int error = 0;
13052         rnode4_t *rp;
13053 
13054         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13055 
13056         if (ctype == NFS4_LCK_CTYPE_NORM) {
13057                 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13058                     recov_statep, startrecovp);
13059                 if (error)
13060                         return (error);
13061                 *did_start_fop = TRUE;
13062         } else {
13063                 *did_start_fop = FALSE;
13064                 *startrecovp = FALSE;
13065         }
13066 
13067         if (!error) {
13068                 rp = VTOR4(vp);
13069 
13070                 /* If the file failed recovery, just quit. */
13071                 mutex_enter(&rp->r_statelock);
13072                 if (rp->r_flags & R4RECOVERR) {
13073                         error = EIO;
13074                 }
13075                 mutex_exit(&rp->r_statelock);
13076         }
13077 
13078         return (error);
13079 }
13080 
13081 /*
13082  * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request.  A
13083  * resend nfs4frlock call is initiated by the recovery framework.
13084  * Acquires the lop and oop seqid synchronization.
13085  */
13086 static void
13087 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13088     COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13089     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13090     LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13091 {
13092         mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13093         int error;
13094 
13095         NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13096             (CE_NOTE,
13097             "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13098         ASSERT(resend_rqstp != NULL);
13099         ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13100             resend_rqstp->lr_op == OP_LOCKU);
13101 
13102         *oopp = resend_rqstp->lr_oop;
13103         if (resend_rqstp->lr_oop) {
13104                 open_owner_hold(resend_rqstp->lr_oop);
13105                 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13106                 ASSERT(error == 0);     /* recov thread always succeeds */
13107         }
13108 
13109         /* Must resend this lost lock/locku request. */
13110         ASSERT(resend_rqstp->lr_lop != NULL);
13111         *lopp = resend_rqstp->lr_lop;
13112         lock_owner_hold(resend_rqstp->lr_lop);
13113         error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13114         ASSERT(error == 0);     /* recov thread always succeeds */
13115 
13116         *ospp = resend_rqstp->lr_osp;
13117         if (*ospp)
13118                 open_stream_hold(resend_rqstp->lr_osp);
13119 
13120         if (resend_rqstp->lr_op == OP_LOCK) {
13121                 LOCK4args *lock_args;
13122 
13123                 argop->argop = OP_LOCK;
13124                 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13125                 lock_args->locktype = resend_rqstp->lr_locktype;
13126                 lock_args->reclaim =
13127                     (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13128                 lock_args->offset = resend_rqstp->lr_flk->l_start;
13129                 lock_args->length = resend_rqstp->lr_flk->l_len;
13130                 if (lock_args->length == 0)
13131                         lock_args->length = ~lock_args->length;
13132                 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13133                     mi2clientid(mi), &lock_args->locker);
13134 
13135                 switch (resend_rqstp->lr_ctype) {
13136                 case NFS4_LCK_CTYPE_RESEND:
13137                         argsp->ctag = TAG_LOCK_RESEND;
13138                         break;
13139                 case NFS4_LCK_CTYPE_REINSTATE:
13140                         argsp->ctag = TAG_LOCK_REINSTATE;
13141                         break;
13142                 case NFS4_LCK_CTYPE_RECLAIM:
13143                         argsp->ctag = TAG_LOCK_RECLAIM;
13144                         break;
13145                 default:
13146                         argsp->ctag = TAG_LOCK_UNKNOWN;
13147                         break;
13148                 }
13149         } else {
13150                 LOCKU4args *locku_args;
13151                 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13152 
13153                 argop->argop = OP_LOCKU;
13154                 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13155                 locku_args->locktype = READ_LT;
13156                 locku_args->seqid = lop->lock_seqid + 1;
13157                 mutex_enter(&lop->lo_lock);
13158                 locku_args->lock_stateid = lop->lock_stateid;
13159                 mutex_exit(&lop->lo_lock);
13160                 locku_args->offset = resend_rqstp->lr_flk->l_start;
13161                 locku_args->length = resend_rqstp->lr_flk->l_len;
13162                 if (locku_args->length == 0)
13163                         locku_args->length = ~locku_args->length;
13164 
13165                 switch (resend_rqstp->lr_ctype) {
13166                 case NFS4_LCK_CTYPE_RESEND:
13167                         argsp->ctag = TAG_LOCKU_RESEND;
13168                         break;
13169                 case NFS4_LCK_CTYPE_REINSTATE:
13170                         argsp->ctag = TAG_LOCKU_REINSTATE;
13171                         break;
13172                 default:
13173                         argsp->ctag = TAG_LOCK_UNKNOWN;
13174                         break;
13175                 }
13176         }
13177 }
13178 
13179 /*
13180  * Setup the LOCKT4 arguments.
13181  */
13182 static void
13183 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13184     LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13185     rnode4_t *rp)
13186 {
13187         LOCKT4args *lockt_args;
13188 
13189         ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13190         ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13191         argop->argop = OP_LOCKT;
13192         argsp->ctag = TAG_LOCKT;
13193         lockt_args = &argop->nfs_argop4_u.oplockt;
13194 
13195         /*
13196          * The locktype will be READ_LT unless it's
13197          * a write lock. We do this because the Solaris
13198          * system call allows the combination of
13199          * F_UNLCK and F_GETLK* and so in that case the
13200          * unlock is mapped to a read.
13201          */
13202         if (flk->l_type == F_WRLCK)
13203                 lockt_args->locktype = WRITE_LT;
13204         else
13205                 lockt_args->locktype = READ_LT;
13206 
13207         lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13208         /* set the lock owner4 args */
13209         nfs4_setlockowner_args(&lockt_args->owner, rp,
13210             ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13211             flk->l_pid);
13212         lockt_args->offset = flk->l_start;
13213         lockt_args->length = flk->l_len;
13214         if (flk->l_len == 0)
13215                 lockt_args->length = ~lockt_args->length;
13216 
13217         *lockt_argsp = lockt_args;
13218 }
13219 
13220 /*
13221  * If the client is holding a delegation, and the open stream to be used
13222  * with this lock request is a delegation open stream, then re-open the stream.
13223  * Sets the nfs4_error_t to all zeros unless the open stream has already
13224  * failed a reopen or we couldn't find the open stream.  NFS4ERR_DELAY
13225  * means the caller should retry (like a recovery retry).
13226  */
13227 static void
13228 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13229 {
13230         open_delegation_type4   dt;
13231         bool_t                  reopen_needed, force;
13232         nfs4_open_stream_t      *osp;
13233         open_claim_type4        oclaim;
13234         rnode4_t                *rp = VTOR4(vp);
13235         mntinfo4_t              *mi = VTOMI4(vp);
13236 
13237         ASSERT(nfs_zone() == mi->mi_zone);
13238 
13239         nfs4_error_zinit(ep);
13240 
13241         mutex_enter(&rp->r_statev4_lock);
13242         dt = rp->r_deleg_type;
13243         mutex_exit(&rp->r_statev4_lock);
13244 
13245         if (dt != OPEN_DELEGATE_NONE) {
13246                 nfs4_open_owner_t       *oop;
13247 
13248                 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13249                 if (!oop) {
13250                         ep->stat = NFS4ERR_IO;
13251                         return;
13252                 }
13253                 /* returns with 'os_sync_lock' held */
13254                 osp = find_open_stream(oop, rp);
13255                 if (!osp) {
13256                         open_owner_rele(oop);
13257                         ep->stat = NFS4ERR_IO;
13258                         return;
13259                 }
13260 
13261                 if (osp->os_failed_reopen) {
13262                         NFS4_DEBUG((nfs4_open_stream_debug ||
13263                             nfs4_client_lock_debug), (CE_NOTE,
13264                             "nfs4frlock_check_deleg: os_failed_reopen set "
13265                             "for osp %p, cr %p, rp %s", (void *)osp,
13266                             (void *)cr, rnode4info(rp)));
13267                         mutex_exit(&osp->os_sync_lock);
13268                         open_stream_rele(osp, rp);
13269                         open_owner_rele(oop);
13270                         ep->stat = NFS4ERR_IO;
13271                         return;
13272                 }
13273 
13274                 /*
13275                  * Determine whether a reopen is needed.  If this
13276                  * is a delegation open stream, then send the open
13277                  * to the server to give visibility to the open owner.
13278                  * Even if it isn't a delegation open stream, we need
13279                  * to check if the previous open CLAIM_DELEGATE_CUR
13280                  * was sufficient.
13281                  */
13282 
13283                 reopen_needed = osp->os_delegation ||
13284                     ((lt == F_RDLCK &&
13285                     !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13286                     (lt == F_WRLCK &&
13287                     !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13288 
13289                 mutex_exit(&osp->os_sync_lock);
13290                 open_owner_rele(oop);
13291 
13292                 if (reopen_needed) {
13293                         /*
13294                          * Always use CLAIM_PREVIOUS after server reboot.
13295                          * The server will reject CLAIM_DELEGATE_CUR if
13296                          * it is used during the grace period.
13297                          */
13298                         mutex_enter(&mi->mi_lock);
13299                         if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13300                                 oclaim = CLAIM_PREVIOUS;
13301                                 force = TRUE;
13302                         } else {
13303                                 oclaim = CLAIM_DELEGATE_CUR;
13304                                 force = FALSE;
13305                         }
13306                         mutex_exit(&mi->mi_lock);
13307 
13308                         nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13309                         if (ep->error == EAGAIN) {
13310                                 nfs4_error_zinit(ep);
13311                                 ep->stat = NFS4ERR_DELAY;
13312                         }
13313                 }
13314                 open_stream_rele(osp, rp);
13315                 osp = NULL;
13316         }
13317 }
13318 
13319 /*
13320  * Setup the LOCKU4 arguments.
13321  * Returns errors via the nfs4_error_t.
13322  * NFS4_OK              no problems.  *go_otwp is TRUE if call should go
13323  *                      over-the-wire.  The caller must release the
13324  *                      reference on *lopp.
13325  * NFS4ERR_DELAY        caller should retry (like recovery retry)
13326  * (other)              unrecoverable error.
13327  */
13328 static void
13329 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13330     LOCKU4args **locku_argsp, flock64_t *flk,
13331     nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13332     vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13333     bool_t *skip_get_err, bool_t *go_otwp)
13334 {
13335         nfs4_lock_owner_t       *lop = NULL;
13336         LOCKU4args              *locku_args;
13337         pid_t                   pid;
13338         bool_t                  is_spec = FALSE;
13339         rnode4_t                *rp = VTOR4(vp);
13340 
13341         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13342         ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13343 
13344         nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13345         if (ep->error || ep->stat)
13346                 return;
13347 
13348         argop->argop = OP_LOCKU;
13349         if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13350                 argsp->ctag = TAG_LOCKU_REINSTATE;
13351         else
13352                 argsp->ctag = TAG_LOCKU;
13353         locku_args = &argop->nfs_argop4_u.oplocku;
13354         *locku_argsp = locku_args;
13355 
13356         /*
13357          * XXX what should locku_args->locktype be?
13358          * setting to ALWAYS be READ_LT so at least
13359          * it is a valid locktype.
13360          */
13361 
13362         locku_args->locktype = READ_LT;
13363 
13364         pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13365             flk->l_pid;
13366 
13367         /*
13368          * Get the lock owner stateid.  If no lock owner
13369          * exists, return success.
13370          */
13371         lop = find_lock_owner(rp, pid, LOWN_ANY);
13372         *lopp = lop;
13373         if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13374                 is_spec = TRUE;
13375         if (!lop || is_spec) {
13376                 /*
13377                  * No lock owner so no locks to unlock.
13378                  * Return success.  If there was a failed
13379                  * reclaim earlier, the lock might still be
13380                  * registered with the local locking code,
13381                  * so notify it of the unlock.
13382                  *
13383                  * If the lockowner is using a special stateid,
13384                  * then the original lock request (that created
13385                  * this lockowner) was never successful, so we
13386                  * have no lock to undo OTW.
13387                  */
13388                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13389                     "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13390                     "(%ld) so return success", (long)pid));
13391 
13392                 if (ctype == NFS4_LCK_CTYPE_NORM)
13393                         flk->l_pid = curproc->p_pid;
13394                 nfs4_register_lock_locally(vp, flk, flag, offset);
13395                 /*
13396                  * Release our hold and NULL out so final_cleanup
13397                  * doesn't try to end a lock seqid sync we
13398                  * never started.
13399                  */
13400                 if (is_spec) {
13401                         lock_owner_rele(lop);
13402                         *lopp = NULL;
13403                 }
13404                 *skip_get_err = TRUE;
13405                 *go_otwp = FALSE;
13406                 return;
13407         }
13408 
13409         ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13410         if (ep->error == EAGAIN) {
13411                 lock_owner_rele(lop);
13412                 *lopp = NULL;
13413                 return;
13414         }
13415 
13416         mutex_enter(&lop->lo_lock);
13417         locku_args->lock_stateid = lop->lock_stateid;
13418         mutex_exit(&lop->lo_lock);
13419         locku_args->seqid = lop->lock_seqid + 1;
13420 
13421         /* leave the ref count on lop, rele after RPC call */
13422 
13423         locku_args->offset = flk->l_start;
13424         locku_args->length = flk->l_len;
13425         if (flk->l_len == 0)
13426                 locku_args->length = ~locku_args->length;
13427 
13428         *go_otwp = TRUE;
13429 }
13430 
13431 /*
13432  * Setup the LOCK4 arguments.
13433  *
13434  * Returns errors via the nfs4_error_t.
13435  * NFS4_OK              no problems
13436  * NFS4ERR_DELAY        caller should retry (like recovery retry)
13437  * (other)              unrecoverable error
13438  */
13439 static void
13440 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13441     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13442     nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13443     flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13444 {
13445         LOCK4args               *lock_args;
13446         nfs4_open_owner_t       *oop = NULL;
13447         nfs4_open_stream_t      *osp = NULL;
13448         nfs4_lock_owner_t       *lop = NULL;
13449         pid_t                   pid;
13450         rnode4_t                *rp = VTOR4(vp);
13451 
13452         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13453 
13454         nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13455         if (ep->error || ep->stat != NFS4_OK)
13456                 return;
13457 
13458         argop->argop = OP_LOCK;
13459         if (ctype == NFS4_LCK_CTYPE_NORM)
13460                 argsp->ctag = TAG_LOCK;
13461         else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13462                 argsp->ctag = TAG_RELOCK;
13463         else
13464                 argsp->ctag = TAG_LOCK_REINSTATE;
13465         lock_args = &argop->nfs_argop4_u.oplock;
13466         lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13467         lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13468         /*
13469          * Get the lock owner.  If no lock owner exists,
13470          * create a 'temporary' one and grab the open seqid
13471          * synchronization (which puts a hold on the open
13472          * owner and open stream).
13473          * This also grabs the lock seqid synchronization.
13474          */
13475         pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13476         ep->stat =
13477             nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13478 
13479         if (ep->stat != NFS4_OK)
13480                 goto out;
13481 
13482         nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13483             &lock_args->locker);
13484 
13485         lock_args->offset = flk->l_start;
13486         lock_args->length = flk->l_len;
13487         if (flk->l_len == 0)
13488                 lock_args->length = ~lock_args->length;
13489         *lock_argsp = lock_args;
13490 out:
13491         *oopp = oop;
13492         *ospp = osp;
13493         *lopp = lop;
13494 }
13495 
13496 /*
13497  * After we get the reply from the server, record the proper information
13498  * for possible resend lock requests.
13499  *
13500  * Allocates memory for the saved_rqstp if we have a lost lock to save.
13501  */
13502 static void
13503 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13504     nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13505     nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13506     nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13507 {
13508         bool_t unlock = (flk->l_type == F_UNLCK);
13509 
13510         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13511         ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13512             ctype == NFS4_LCK_CTYPE_REINSTATE);
13513 
13514         if (error != 0 && !unlock) {
13515                 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13516                     nfs4_client_lock_debug), (CE_NOTE,
13517                     "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13518                     " for lop %p", (void *)lop));
13519                 ASSERT(lop != NULL);
13520                 mutex_enter(&lop->lo_lock);
13521                 lop->lo_pending_rqsts = 1;
13522                 mutex_exit(&lop->lo_lock);
13523         }
13524 
13525         lost_rqstp->lr_putfirst = FALSE;
13526         lost_rqstp->lr_op = 0;
13527 
13528         /*
13529          * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13530          * recovery purposes so that the lock request that was sent
13531          * can be saved and re-issued later.  Ditto for EIO from a forced
13532          * unmount.  This is done to have the client's local locking state
13533          * match the v4 server's state; that is, the request was
13534          * potentially received and accepted by the server but the client
13535          * thinks it was not.
13536          */
13537         if (error == ETIMEDOUT || error == EINTR ||
13538             NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13539                 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13540                     nfs4_client_lock_debug), (CE_NOTE,
13541                     "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13542                     "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13543                     (void *)lop, (void *)oop, (void *)osp));
13544                 if (unlock)
13545                         lost_rqstp->lr_op = OP_LOCKU;
13546                 else {
13547                         lost_rqstp->lr_op = OP_LOCK;
13548                         lost_rqstp->lr_locktype = locktype;
13549                 }
13550                 /*
13551                  * Objects are held and rele'd via the recovery code.
13552                  * See nfs4_save_lost_rqst.
13553                  */
13554                 lost_rqstp->lr_vp = vp;
13555                 lost_rqstp->lr_dvp = NULL;
13556                 lost_rqstp->lr_oop = oop;
13557                 lost_rqstp->lr_osp = osp;
13558                 lost_rqstp->lr_lop = lop;
13559                 lost_rqstp->lr_cr = cr;
13560                 switch (ctype) {
13561                 case NFS4_LCK_CTYPE_NORM:
13562                         flk->l_pid = ttoproc(curthread)->p_pid;
13563                         lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13564                         break;
13565                 case NFS4_LCK_CTYPE_REINSTATE:
13566                         lost_rqstp->lr_putfirst = TRUE;
13567                         lost_rqstp->lr_ctype = ctype;
13568                         break;
13569                 default:
13570                         break;
13571                 }
13572                 lost_rqstp->lr_flk = flk;
13573         }
13574 }
13575 
13576 /*
13577  * Update lop's seqid.  Also update the seqid stored in a resend request,
13578  * if any.  (Some recovery errors increment the seqid, and we may have to
13579  * send the resend request again.)
13580  */
13581 
13582 static void
13583 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13584     nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13585 {
13586         if (lock_args) {
13587                 if (lock_args->locker.new_lock_owner == TRUE)
13588                         nfs4_get_and_set_next_open_seqid(oop, tag_type);
13589                 else {
13590                         ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13591                         nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13592                 }
13593         } else if (locku_args) {
13594                 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13595                 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13596         }
13597 }
13598 
13599 /*
13600  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13601  * COMPOUND4 args/res for calls that need to retry.
13602  * Switches the *cred_otwp to base_cr.
13603  */
13604 static void
13605 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13606     nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13607     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13608     nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13609     nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13610 {
13611         nfs4_open_owner_t       *oop = *oopp;
13612         nfs4_open_stream_t      *osp = *ospp;
13613         nfs4_lock_owner_t       *lop = *lopp;
13614         nfs_argop4              *argop = (*argspp)->array;
13615 
13616         if (*did_start_fop) {
13617                 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13618                     needrecov);
13619                 *did_start_fop = FALSE;
13620         }
13621         ASSERT((*argspp)->array_len == 2);
13622         if (argop[1].argop == OP_LOCK)
13623                 nfs4args_lock_free(&argop[1]);
13624         else if (argop[1].argop == OP_LOCKT)
13625                 nfs4args_lockt_free(&argop[1]);
13626         kmem_free(argop, 2 * sizeof (nfs_argop4));
13627         if (!error)
13628                 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13629         *argspp = NULL;
13630         *respp = NULL;
13631 
13632         if (lop) {
13633                 nfs4_end_lock_seqid_sync(lop);
13634                 lock_owner_rele(lop);
13635                 *lopp = NULL;
13636         }
13637 
13638         /* need to free up the reference on osp for lock args */
13639         if (osp != NULL) {
13640                 open_stream_rele(osp, VTOR4(vp));
13641                 *ospp = NULL;
13642         }
13643 
13644         /* need to free up the reference on oop for lock args */
13645         if (oop != NULL) {
13646                 nfs4_end_open_seqid_sync(oop);
13647                 open_owner_rele(oop);
13648                 *oopp = NULL;
13649         }
13650 
13651         crfree(*cred_otwp);
13652         *cred_otwp = base_cr;
13653         crhold(*cred_otwp);
13654 }
13655 
13656 /*
13657  * Function to process the client's recovery for nfs4frlock.
13658  * Returns TRUE if we should retry the lock request; FALSE otherwise.
13659  *
13660  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13661  * COMPOUND4 args/res for calls that need to retry.
13662  *
13663  * Note: the rp's r_lkserlock is *not* dropped during this path.
13664  */
13665 static bool_t
13666 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13667     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13668     LOCK4args *lock_args, LOCKU4args *locku_args,
13669     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13670     nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13671     nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13672     bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13673 {
13674         nfs4_open_owner_t       *oop = *oopp;
13675         nfs4_open_stream_t      *osp = *ospp;
13676         nfs4_lock_owner_t       *lop = *lopp;
13677 
13678         bool_t abort, retry;
13679 
13680         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13681         ASSERT((*argspp) != NULL);
13682         ASSERT((*respp) != NULL);
13683         if (lock_args || locku_args)
13684                 ASSERT(lop != NULL);
13685 
13686         NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13687             (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13688 
13689         retry = TRUE;
13690         abort = FALSE;
13691         if (needrecov) {
13692                 nfs4_bseqid_entry_t *bsep = NULL;
13693                 nfs_opnum4 op;
13694 
13695                 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13696 
13697                 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13698                         seqid4 seqid;
13699 
13700                         if (lock_args) {
13701                                 if (lock_args->locker.new_lock_owner == TRUE)
13702                                         seqid = lock_args->locker.locker4_u.
13703                                             open_owner.open_seqid;
13704                                 else
13705                                         seqid = lock_args->locker.locker4_u.
13706                                             lock_owner.lock_seqid;
13707                         } else if (locku_args) {
13708                                 seqid = locku_args->seqid;
13709                         } else {
13710                                 seqid = 0;
13711                         }
13712 
13713                         bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13714                             flk->l_pid, (*argspp)->ctag, seqid);
13715                 }
13716 
13717                 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13718                     (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13719                     lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13720                     NULL, op, bsep, NULL, NULL);
13721 
13722                 if (bsep)
13723                         kmem_free(bsep, sizeof (*bsep));
13724         }
13725 
13726         /*
13727          * Return that we do not want to retry the request for 3 cases:
13728          * 1. If we received EINTR or are bailing out because of a forced
13729          *    unmount, we came into this code path just for the sake of
13730          *    initiating recovery, we now need to return the error.
13731          * 2. If we have aborted recovery.
13732          * 3. We received NFS4ERR_BAD_SEQID.
13733          */
13734         if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13735             abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13736                 retry = FALSE;
13737 
13738         if (*did_start_fop == TRUE) {
13739                 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13740                     needrecov);
13741                 *did_start_fop = FALSE;
13742         }
13743 
13744         if (retry == TRUE) {
13745                 nfs_argop4      *argop;
13746 
13747                 argop = (*argspp)->array;
13748                 ASSERT((*argspp)->array_len == 2);
13749 
13750                 if (argop[1].argop == OP_LOCK)
13751                         nfs4args_lock_free(&argop[1]);
13752                 else if (argop[1].argop == OP_LOCKT)
13753                         nfs4args_lockt_free(&argop[1]);
13754                 kmem_free(argop, 2 * sizeof (nfs_argop4));
13755                 if (!ep->error)
13756                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13757                 *respp = NULL;
13758                 *argspp = NULL;
13759         }
13760 
13761         if (lop != NULL) {
13762                 nfs4_end_lock_seqid_sync(lop);
13763                 lock_owner_rele(lop);
13764         }
13765 
13766         *lopp = NULL;
13767 
13768         /* need to free up the reference on osp for lock args */
13769         if (osp != NULL) {
13770                 open_stream_rele(osp, rp);
13771                 *ospp = NULL;
13772         }
13773 
13774         /* need to free up the reference on oop for lock args */
13775         if (oop != NULL) {
13776                 nfs4_end_open_seqid_sync(oop);
13777                 open_owner_rele(oop);
13778                 *oopp = NULL;
13779         }
13780 
13781         return (retry);
13782 }
13783 
13784 /*
13785  * Handles the successful reply from the server for nfs4frlock.
13786  */
13787 static void
13788 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13789     vnode_t *vp, int flag, u_offset_t offset,
13790     nfs4_lost_rqst_t *resend_rqstp)
13791 {
13792         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13793         if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13794             (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13795                 if (ctype == NFS4_LCK_CTYPE_NORM) {
13796                         flk->l_pid = ttoproc(curthread)->p_pid;
13797                         /*
13798                          * We do not register lost locks locally in
13799                          * the 'resend' case since the user/application
13800                          * doesn't think we have the lock.
13801                          */
13802                         ASSERT(!resend_rqstp);
13803                         nfs4_register_lock_locally(vp, flk, flag, offset);
13804                 }
13805         }
13806 }
13807 
13808 /*
13809  * Handle the DENIED reply from the server for nfs4frlock.
13810  * Returns TRUE if we should retry the request; FALSE otherwise.
13811  *
13812  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13813  * COMPOUND4 args/res for calls that need to retry.  Can also
13814  * drop and regrab the r_lkserlock.
13815  */
13816 static bool_t
13817 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13818     LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13819     nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13820     vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13821     nfs4_recov_state_t *recov_statep, int needrecov,
13822     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13823     clock_t *tick_delayp, short *whencep, int *errorp,
13824     nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13825     bool_t *skip_get_err)
13826 {
13827         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13828 
13829         if (lock_args) {
13830                 nfs4_open_owner_t       *oop = *oopp;
13831                 nfs4_open_stream_t      *osp = *ospp;
13832                 nfs4_lock_owner_t       *lop = *lopp;
13833                 int                     intr;
13834 
13835                 /*
13836                  * Blocking lock needs to sleep and retry from the request.
13837                  *
13838                  * Do not block and wait for 'resend' or 'reinstate'
13839                  * lock requests, just return the error.
13840                  *
13841                  * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13842                  */
13843                 if (cmd == F_SETLKW) {
13844                         rnode4_t *rp = VTOR4(vp);
13845                         nfs_argop4 *argop = (*argspp)->array;
13846 
13847                         ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13848 
13849                         nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13850                             recov_statep, needrecov);
13851                         *did_start_fop = FALSE;
13852                         ASSERT((*argspp)->array_len == 2);
13853                         if (argop[1].argop == OP_LOCK)
13854                                 nfs4args_lock_free(&argop[1]);
13855                         else if (argop[1].argop == OP_LOCKT)
13856                                 nfs4args_lockt_free(&argop[1]);
13857                         kmem_free(argop, 2 * sizeof (nfs_argop4));
13858                         if (*respp)
13859                                 (void) xdr_free(xdr_COMPOUND4res_clnt,
13860                                     (caddr_t)*respp);
13861                         *argspp = NULL;
13862                         *respp = NULL;
13863                         nfs4_end_lock_seqid_sync(lop);
13864                         lock_owner_rele(lop);
13865                         *lopp = NULL;
13866                         if (osp != NULL) {
13867                                 open_stream_rele(osp, rp);
13868                                 *ospp = NULL;
13869                         }
13870                         if (oop != NULL) {
13871                                 nfs4_end_open_seqid_sync(oop);
13872                                 open_owner_rele(oop);
13873                                 *oopp = NULL;
13874                         }
13875 
13876                         nfs_rw_exit(&rp->r_lkserlock);
13877 
13878                         intr = nfs4_block_and_wait(tick_delayp, rp);
13879 
13880                         if (intr) {
13881                                 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13882                                     RW_WRITER, FALSE);
13883                                 *errorp = EINTR;
13884                                 return (FALSE);
13885                         }
13886 
13887                         (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13888                             RW_WRITER, FALSE);
13889 
13890                         /*
13891                          * Make sure we are still safe to lock with
13892                          * regards to mmapping.
13893                          */
13894                         if (!nfs4_safelock(vp, flk, cr)) {
13895                                 *errorp = EAGAIN;
13896                                 return (FALSE);
13897                         }
13898 
13899                         return (TRUE);
13900                 }
13901                 if (ctype == NFS4_LCK_CTYPE_NORM)
13902                         *errorp = EAGAIN;
13903                 *skip_get_err = TRUE;
13904                 flk->l_whence = 0;
13905                 *whencep = 0;
13906                 return (FALSE);
13907         } else if (lockt_args) {
13908                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13909                     "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13910 
13911                 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13912                     flk, lockt_args);
13913 
13914                 /* according to NLM code */
13915                 *errorp = 0;
13916                 *whencep = 0;
13917                 *skip_get_err = TRUE;
13918                 return (FALSE);
13919         }
13920         return (FALSE);
13921 }
13922 
13923 /*
13924  * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13925  */
13926 static void
13927 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13928 {
13929         switch (resp->status) {
13930         case NFS4ERR_ACCESS:
13931         case NFS4ERR_ADMIN_REVOKED:
13932         case NFS4ERR_BADHANDLE:
13933         case NFS4ERR_BAD_RANGE:
13934         case NFS4ERR_BAD_SEQID:
13935         case NFS4ERR_BAD_STATEID:
13936         case NFS4ERR_BADXDR:
13937         case NFS4ERR_DEADLOCK:
13938         case NFS4ERR_DELAY:
13939         case NFS4ERR_EXPIRED:
13940         case NFS4ERR_FHEXPIRED:
13941         case NFS4ERR_GRACE:
13942         case NFS4ERR_INVAL:
13943         case NFS4ERR_ISDIR:
13944         case NFS4ERR_LEASE_MOVED:
13945         case NFS4ERR_LOCK_NOTSUPP:
13946         case NFS4ERR_LOCK_RANGE:
13947         case NFS4ERR_MOVED:
13948         case NFS4ERR_NOFILEHANDLE:
13949         case NFS4ERR_NO_GRACE:
13950         case NFS4ERR_OLD_STATEID:
13951         case NFS4ERR_OPENMODE:
13952         case NFS4ERR_RECLAIM_BAD:
13953         case NFS4ERR_RECLAIM_CONFLICT:
13954         case NFS4ERR_RESOURCE:
13955         case NFS4ERR_SERVERFAULT:
13956         case NFS4ERR_STALE:
13957         case NFS4ERR_STALE_CLIENTID:
13958         case NFS4ERR_STALE_STATEID:
13959                 return;
13960         default:
13961                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13962                     "nfs4frlock_results_default: got unrecognizable "
13963                     "res.status %d", resp->status));
13964                 *errorp = NFS4ERR_INVAL;
13965         }
13966 }
13967 
13968 /*
13969  * The lock request was successful, so update the client's state.
13970  */
13971 static void
13972 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13973     LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13974     vnode_t *vp, flock64_t *flk, cred_t *cr,
13975     nfs4_lost_rqst_t *resend_rqstp)
13976 {
13977         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13978 
13979         if (lock_args) {
13980                 LOCK4res *lock_res;
13981 
13982                 lock_res = &resop->nfs_resop4_u.oplock;
13983                 /* update the stateid with server's response */
13984 
13985                 if (lock_args->locker.new_lock_owner == TRUE) {
13986                         mutex_enter(&lop->lo_lock);
13987                         lop->lo_just_created = NFS4_PERM_CREATED;
13988                         mutex_exit(&lop->lo_lock);
13989                 }
13990 
13991                 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13992 
13993                 /*
13994                  * If the lock was the result of a resending a lost
13995                  * request, we've synched up the stateid and seqid
13996                  * with the server, but now the server might be out of sync
13997                  * with what the application thinks it has for locks.
13998                  * Clean that up here.  It's unclear whether we should do
13999                  * this even if the filesystem has been forcibly unmounted.
14000                  * For most servers, it's probably wasted effort, but
14001                  * RFC3530 lets servers require that unlocks exactly match
14002                  * the locks that are held.
14003                  */
14004                 if (resend_rqstp != NULL &&
14005                     resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
14006                         nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
14007                 } else {
14008                         flk->l_whence = 0;
14009                 }
14010         } else if (locku_args) {
14011                 LOCKU4res *locku_res;
14012 
14013                 locku_res = &resop->nfs_resop4_u.oplocku;
14014 
14015                 /* Update the stateid with the server's response */
14016                 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14017         } else if (lockt_args) {
14018                 /* Switch the lock type to express success, see fcntl */
14019                 flk->l_type = F_UNLCK;
14020                 flk->l_whence = 0;
14021         }
14022 }
14023 
14024 /*
14025  * Do final cleanup before exiting nfs4frlock.
14026  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14027  * COMPOUND4 args/res for calls that haven't already.
14028  */
14029 static void
14030 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14031     COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14032     nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14033     nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14034     short whence, u_offset_t offset, struct lm_sysid *ls,
14035     int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14036     bool_t did_start_fop, bool_t skip_get_err,
14037     cred_t *cred_otw, cred_t *cred)
14038 {
14039         mntinfo4_t      *mi = VTOMI4(vp);
14040         rnode4_t        *rp = VTOR4(vp);
14041         int             error = *errorp;
14042         nfs_argop4      *argop;
14043         int     do_flush_pages = 0;
14044 
14045         ASSERT(nfs_zone() == mi->mi_zone);
14046         /*
14047          * The client recovery code wants the raw status information,
14048          * so don't map the NFS status code to an errno value for
14049          * non-normal call types.
14050          */
14051         if (ctype == NFS4_LCK_CTYPE_NORM) {
14052                 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14053                         *errorp = geterrno4(resp->status);
14054                 if (did_start_fop == TRUE)
14055                         nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14056                             needrecov);
14057 
14058                 /*
14059                  * We've established a new lock on the server, so invalidate
14060                  * the pages associated with the vnode to get the most up to
14061                  * date pages from the server after acquiring the lock. We
14062                  * want to be sure that the read operation gets the newest data.
14063                  * N.B.
14064                  * We used to do this in nfs4frlock_results_ok but that doesn't
14065                  * work since VOP_PUTPAGE can call nfs4_commit which calls
14066                  * nfs4_start_fop. We flush the pages below after calling
14067                  * nfs4_end_fop above
14068                  * The flush of the page cache must be done after
14069                  * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14070                  */
14071                 if (!error && resp && resp->status == NFS4_OK)
14072                         do_flush_pages = 1;
14073         }
14074         if (argsp) {
14075                 ASSERT(argsp->array_len == 2);
14076                 argop = argsp->array;
14077                 if (argop[1].argop == OP_LOCK)
14078                         nfs4args_lock_free(&argop[1]);
14079                 else if (argop[1].argop == OP_LOCKT)
14080                         nfs4args_lockt_free(&argop[1]);
14081                 kmem_free(argop, 2 * sizeof (nfs_argop4));
14082                 if (resp)
14083                         (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14084         }
14085 
14086         /* free the reference on the lock owner */
14087         if (lop != NULL) {
14088                 nfs4_end_lock_seqid_sync(lop);
14089                 lock_owner_rele(lop);
14090         }
14091 
14092         /* need to free up the reference on osp for lock args */
14093         if (osp != NULL)
14094                 open_stream_rele(osp, rp);
14095 
14096         /* need to free up the reference on oop for lock args */
14097         if (oop != NULL) {
14098                 nfs4_end_open_seqid_sync(oop);
14099                 open_owner_rele(oop);
14100         }
14101 
14102         if (do_flush_pages)
14103                 nfs4_flush_pages(vp, cred);
14104 
14105         (void) convoff(vp, flk, whence, offset);
14106 
14107         lm_rel_sysid(ls);
14108 
14109         /*
14110          * Record debug information in the event we get EINVAL.
14111          */
14112         mutex_enter(&mi->mi_lock);
14113         if (*errorp == EINVAL && (lock_args || locku_args) &&
14114             (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14115                 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14116                         zcmn_err(getzoneid(), CE_NOTE,
14117                             "%s operation failed with "
14118                             "EINVAL probably since the server, %s,"
14119                             " doesn't support POSIX style locking",
14120                             lock_args ? "LOCK" : "LOCKU",
14121                             mi->mi_curr_serv->sv_hostname);
14122                         mi->mi_flags |= MI4_LOCK_DEBUG;
14123                 }
14124         }
14125         mutex_exit(&mi->mi_lock);
14126 
14127         if (cred_otw)
14128                 crfree(cred_otw);
14129 }
14130 
14131 /*
14132  * This calls the server and the local locking code.
14133  *
14134  * Client locks are registerred locally by oring the sysid with
14135  * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14136  * We need to distinguish between the two to avoid collision in case one
14137  * machine is used as both client and server.
14138  *
14139  * Blocking lock requests will continually retry to acquire the lock
14140  * forever.
14141  *
14142  * The ctype is defined as follows:
14143  * NFS4_LCK_CTYPE_NORM: normal lock request.
14144  *
14145  * NFS4_LCK_CTYPE_RECLAIM:  bypass the usual calls for synchronizing with client
14146  * recovery, get the pid from flk instead of curproc, and don't reregister
14147  * the lock locally.
14148  *
14149  * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14150  * that we will use the information passed in via resend_rqstp to setup the
14151  * lock/locku request.  This resend is the exact same request as the 'lost
14152  * lock', and is initiated by the recovery framework. A successful resend
14153  * request can initiate one or more reinstate requests.
14154  *
14155  * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14156  * does not trigger additional reinstate requests.  This lock call type is
14157  * set for setting the v4 server's locking state back to match what the
14158  * client's local locking state is in the event of a received 'lost lock'.
14159  *
14160  * Errors are returned via the nfs4_error_t parameter.
14161  */
14162 void
14163 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14164     int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14165     nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14166 {
14167         COMPOUND4args_clnt      args, *argsp = NULL;
14168         COMPOUND4res_clnt       res, *resp = NULL;
14169         nfs_argop4      *argop;
14170         nfs_resop4      *resop;
14171         rnode4_t        *rp;
14172         int             doqueue = 1;
14173         clock_t         tick_delay;  /* delay in clock ticks */
14174         struct lm_sysid *ls;
14175         LOCK4args       *lock_args = NULL;
14176         LOCKU4args      *locku_args = NULL;
14177         LOCKT4args      *lockt_args = NULL;
14178         nfs4_open_owner_t *oop = NULL;
14179         nfs4_open_stream_t *osp = NULL;
14180         nfs4_lock_owner_t *lop = NULL;
14181         bool_t          needrecov = FALSE;
14182         nfs4_recov_state_t recov_state;
14183         short           whence;
14184         nfs4_op_hint_t  op_hint;
14185         nfs4_lost_rqst_t lost_rqst;
14186         bool_t          retry = FALSE;
14187         bool_t          did_start_fop = FALSE;
14188         bool_t          skip_get_err = FALSE;
14189         cred_t          *cred_otw = NULL;
14190         bool_t          recovonly;      /* just queue request */
14191         int             frc_no_reclaim = 0;
14192 #ifdef DEBUG
14193         char *name;
14194 #endif
14195 
14196         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14197 
14198 #ifdef DEBUG
14199         name = fn_name(VTOSV(vp)->sv_name);
14200         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14201             "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14202             "length %"PRIu64", pid %d, sysid %d, call type %s, "
14203             "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14204             flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14205             flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14206             resend_rqstp ? "TRUE" : "FALSE"));
14207         kmem_free(name, MAXNAMELEN);
14208 #endif
14209 
14210         nfs4_error_zinit(ep);
14211         ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14212         if (ep->error)
14213                 return;
14214         ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14215         if (ep->error)
14216                 return;
14217         nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14218             vp, cr, &cred_otw);
14219 
14220 recov_retry:
14221         nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14222             &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14223         rp = VTOR4(vp);
14224 
14225         ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14226             &did_start_fop, &recovonly);
14227 
14228         if (ep->error)
14229                 goto out;
14230 
14231         if (recovonly) {
14232                 /*
14233                  * Leave the request for the recovery system to deal with.
14234                  */
14235                 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14236                 ASSERT(cmd != F_GETLK);
14237                 ASSERT(flk->l_type == F_UNLCK);
14238 
14239                 nfs4_error_init(ep, EINTR);
14240                 needrecov = TRUE;
14241                 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14242                 if (lop != NULL) {
14243                         nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14244                             NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14245                         (void) nfs4_start_recovery(ep,
14246                             VTOMI4(vp), vp, NULL, NULL,
14247                             (lost_rqst.lr_op == OP_LOCK ||
14248                             lost_rqst.lr_op == OP_LOCKU) ?
14249                             &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14250                         lock_owner_rele(lop);
14251                         lop = NULL;
14252                 }
14253                 flk->l_pid = curproc->p_pid;
14254                 nfs4_register_lock_locally(vp, flk, flag, offset);
14255                 goto out;
14256         }
14257 
14258         /* putfh directory fh */
14259         argop[0].argop = OP_CPUTFH;
14260         argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14261 
14262         /*
14263          * Set up the over-the-wire arguments and get references to the
14264          * open owner, etc.
14265          */
14266 
14267         if (ctype == NFS4_LCK_CTYPE_RESEND ||
14268             ctype == NFS4_LCK_CTYPE_REINSTATE) {
14269                 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14270                     &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14271         } else {
14272                 bool_t go_otw = TRUE;
14273 
14274                 ASSERT(resend_rqstp == NULL);
14275 
14276                 switch (cmd) {
14277                 case F_GETLK:
14278                 case F_O_GETLK:
14279                         nfs4frlock_setup_lockt_args(ctype, &argop[1],
14280                             &lockt_args, argsp, flk, rp);
14281                         break;
14282                 case F_SETLKW:
14283                 case F_SETLK:
14284                         if (flk->l_type == F_UNLCK)
14285                                 nfs4frlock_setup_locku_args(ctype,
14286                                     &argop[1], &locku_args, flk,
14287                                     &lop, ep, argsp,
14288                                     vp, flag, offset, cr,
14289                                     &skip_get_err, &go_otw);
14290                         else
14291                                 nfs4frlock_setup_lock_args(ctype,
14292                                     &lock_args, &oop, &osp, &lop, &argop[1],
14293                                     argsp, flk, cmd, vp, cr, ep);
14294 
14295                         if (ep->error)
14296                                 goto out;
14297 
14298                         switch (ep->stat) {
14299                         case NFS4_OK:
14300                                 break;
14301                         case NFS4ERR_DELAY:
14302                                 /* recov thread never gets this error */
14303                                 ASSERT(resend_rqstp == NULL);
14304                                 ASSERT(did_start_fop);
14305 
14306                                 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14307                                     &recov_state, TRUE);
14308                                 did_start_fop = FALSE;
14309                                 if (argop[1].argop == OP_LOCK)
14310                                         nfs4args_lock_free(&argop[1]);
14311                                 else if (argop[1].argop == OP_LOCKT)
14312                                         nfs4args_lockt_free(&argop[1]);
14313                                 kmem_free(argop, 2 * sizeof (nfs_argop4));
14314                                 argsp = NULL;
14315                                 goto recov_retry;
14316                         default:
14317                                 ep->error = EIO;
14318                                 goto out;
14319                         }
14320                         break;
14321                 default:
14322                         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14323                             "nfs4_frlock: invalid cmd %d", cmd));
14324                         ep->error = EINVAL;
14325                         goto out;
14326                 }
14327 
14328                 if (!go_otw)
14329                         goto out;
14330         }
14331 
14332         /* XXX should we use the local reclock as a cache ? */
14333         /*
14334          * Unregister the lock with the local locking code before
14335          * contacting the server.  This avoids a potential race where
14336          * another process gets notified that it has been granted a lock
14337          * before we can unregister ourselves locally.
14338          */
14339         if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14340                 if (ctype == NFS4_LCK_CTYPE_NORM)
14341                         flk->l_pid = ttoproc(curthread)->p_pid;
14342                 nfs4_register_lock_locally(vp, flk, flag, offset);
14343         }
14344 
14345         /*
14346          * Send the server the lock request.  Continually loop with a delay
14347          * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14348          */
14349         resp = &res;
14350 
14351         NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14352             (CE_NOTE,
14353             "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14354             rnode4info(rp)));
14355 
14356         if (lock_args && frc_no_reclaim) {
14357                 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14358                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14359                     "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14360                 lock_args->reclaim = FALSE;
14361                 if (did_reclaimp)
14362                         *did_reclaimp = 0;
14363         }
14364 
14365         /*
14366          * Do the OTW call.
14367          */
14368         rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14369 
14370         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14371             "nfs4frlock: error %d, status %d", ep->error, resp->status));
14372 
14373         needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14374         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14375             "nfs4frlock: needrecov %d", needrecov));
14376 
14377         if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14378                 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14379                     args.ctag);
14380 
14381         /*
14382          * Check if one of these mutually exclusive error cases has
14383          * happened:
14384          *   need to swap credentials due to access error
14385          *   recovery is needed
14386          *   different error (only known case is missing Kerberos ticket)
14387          */
14388 
14389         if ((ep->error == EACCES ||
14390             (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14391             cred_otw != cr) {
14392                 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14393                     &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14394                     cr, &cred_otw);
14395                 goto recov_retry;
14396         }
14397 
14398         if (needrecov) {
14399                 /*
14400                  * LOCKT requests don't need to recover from lost
14401                  * requests since they don't create/modify state.
14402                  */
14403                 if ((ep->error == EINTR ||
14404                     NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14405                     lockt_args)
14406                         goto out;
14407                 /*
14408                  * Do not attempt recovery for requests initiated by
14409                  * the recovery framework.  Let the framework redrive them.
14410                  */
14411                 if (ctype != NFS4_LCK_CTYPE_NORM)
14412                         goto out;
14413                 else {
14414                         ASSERT(resend_rqstp == NULL);
14415                 }
14416 
14417                 nfs4frlock_save_lost_rqst(ctype, ep->error,
14418                     flk_to_locktype(cmd, flk->l_type),
14419                     oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14420 
14421                 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14422                     &resp, lock_args, locku_args, &oop, &osp, &lop,
14423                     rp, vp, &recov_state, op_hint, &did_start_fop,
14424                     cmd != F_GETLK ? &lost_rqst : NULL, flk);
14425 
14426                 if (retry) {
14427                         ASSERT(oop == NULL);
14428                         ASSERT(osp == NULL);
14429                         ASSERT(lop == NULL);
14430                         goto recov_retry;
14431                 }
14432                 goto out;
14433         }
14434 
14435         /*
14436          * Bail out if have reached this point with ep->error set. Can
14437          * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14438          * This happens if Kerberos ticket has expired or has been
14439          * destroyed.
14440          */
14441         if (ep->error != 0)
14442                 goto out;
14443 
14444         /*
14445          * Process the reply.
14446          */
14447         switch (resp->status) {
14448         case NFS4_OK:
14449                 resop = &resp->array[1];
14450                 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14451                     resend_rqstp);
14452                 /*
14453                  * Have a successful lock operation, now update state.
14454                  */
14455                 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14456                     resop, lop, vp, flk, cr, resend_rqstp);
14457                 break;
14458 
14459         case NFS4ERR_DENIED:
14460                 resop = &resp->array[1];
14461                 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14462                     &oop, &osp, &lop, cmd, vp, flk, op_hint,
14463                     &recov_state, needrecov, &argsp, &resp,
14464                     &tick_delay, &whence, &ep->error, resop, cr,
14465                     &did_start_fop, &skip_get_err);
14466 
14467                 if (retry) {
14468                         ASSERT(oop == NULL);
14469                         ASSERT(osp == NULL);
14470                         ASSERT(lop == NULL);
14471                         goto recov_retry;
14472                 }
14473                 break;
14474         /*
14475          * If the server won't let us reclaim, fall-back to trying to lock
14476          * the file from scratch. Code elsewhere will check the changeinfo
14477          * to ensure the file hasn't been changed.
14478          */
14479         case NFS4ERR_NO_GRACE:
14480                 if (lock_args && lock_args->reclaim == TRUE) {
14481                         ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14482                         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14483                             "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14484                         frc_no_reclaim = 1;
14485                         /* clean up before retrying */
14486                         needrecov = 0;
14487                         (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14488                             lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14489                             &recov_state, op_hint, &did_start_fop, NULL, flk);
14490                         goto recov_retry;
14491                 }
14492                 /* FALLTHROUGH */
14493 
14494         default:
14495                 nfs4frlock_results_default(resp, &ep->error);
14496                 break;
14497         }
14498 out:
14499         /*
14500          * Process and cleanup from error.  Make interrupted unlock
14501          * requests look successful, since they will be handled by the
14502          * client recovery code.
14503          */
14504         nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14505             needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14506             lock_args, locku_args, did_start_fop,
14507             skip_get_err, cred_otw, cr);
14508 
14509         if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14510             (cmd == F_SETLK || cmd == F_SETLKW))
14511                 ep->error = 0;
14512 }
14513 
14514 /*
14515  * nfs4_safelock:
14516  *
14517  * Return non-zero if the given lock request can be handled without
14518  * violating the constraints on concurrent mapping and locking.
14519  */
14520 
14521 static int
14522 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14523 {
14524         rnode4_t *rp = VTOR4(vp);
14525         struct vattr va;
14526         int error;
14527 
14528         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14529         ASSERT(rp->r_mapcnt >= 0);
14530         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14531             "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14532             "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14533             bfp->l_start, bfp->l_len, rp->r_mapcnt));
14534 
14535         if (rp->r_mapcnt == 0)
14536                 return (1);             /* always safe if not mapped */
14537 
14538         /*
14539          * If the file is already mapped and there are locks, then they
14540          * should be all safe locks.  So adding or removing a lock is safe
14541          * as long as the new request is safe (i.e., whole-file, meaning
14542          * length and starting offset are both zero).
14543          */
14544 
14545         if (bfp->l_start != 0 || bfp->l_len != 0) {
14546                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14547                     "cannot lock a memory mapped file unless locking the "
14548                     "entire file: start %"PRIx64", len %"PRIx64,
14549                     bfp->l_start, bfp->l_len));
14550                 return (0);
14551         }
14552 
14553         /* mandatory locking and mapping don't mix */
14554         va.va_mask = AT_MODE;
14555         error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14556         if (error != 0) {
14557                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14558                     "getattr error %d", error));
14559                 return (0);             /* treat errors conservatively */
14560         }
14561         if (MANDLOCK(vp, va.va_mode)) {
14562                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14563                     "cannot mandatory lock and mmap a file"));
14564                 return (0);
14565         }
14566 
14567         return (1);
14568 }
14569 
14570 
14571 /*
14572  * Register the lock locally within Solaris.
14573  * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14574  * recording locks locally.
14575  *
14576  * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14577  * are registered locally.
14578  */
14579 void
14580 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14581     u_offset_t offset)
14582 {
14583         int oldsysid;
14584         int error;
14585 #ifdef DEBUG
14586         char *name;
14587 #endif
14588 
14589         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14590 
14591 #ifdef DEBUG
14592         name = fn_name(VTOSV(vp)->sv_name);
14593         NFS4_DEBUG(nfs4_client_lock_debug,
14594             (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14595             "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14596             name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14597             flk->l_sysid));
14598         kmem_free(name, MAXNAMELEN);
14599 #endif
14600 
14601         /* register the lock with local locking */
14602         oldsysid = flk->l_sysid;
14603         flk->l_sysid |= LM_SYSID_CLIENT;
14604         error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14605 #ifdef DEBUG
14606         if (error != 0) {
14607                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14608                     "nfs4_register_lock_locally: could not register with"
14609                     " local locking"));
14610                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14611                     "error %d, vp 0x%p, pid %d, sysid 0x%x",
14612                     error, (void *)vp, flk->l_pid, flk->l_sysid));
14613                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14614                     "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14615                     flk->l_type, flk->l_start, flk->l_len));
14616                 (void) reclock(vp, flk, 0, flag, offset, NULL);
14617                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14618                     "blocked by pid %d sysid 0x%x type %d "
14619                     "off 0x%" PRIx64 " len 0x%" PRIx64,
14620                     flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14621                     flk->l_len));
14622         }
14623 #endif
14624         flk->l_sysid = oldsysid;
14625 }
14626 
14627 /*
14628  * nfs4_lockrelease:
14629  *
14630  * Release any locks on the given vnode that are held by the current
14631  * process.  Also removes the lock owner (if one exists) from the rnode's
14632  * list.
14633  */
14634 static int
14635 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14636 {
14637         flock64_t ld;
14638         int ret, error;
14639         rnode4_t *rp;
14640         nfs4_lock_owner_t *lop;
14641         nfs4_recov_state_t recov_state;
14642         mntinfo4_t *mi;
14643         bool_t possible_orphan = FALSE;
14644         bool_t recovonly;
14645 
14646         ASSERT((uintptr_t)vp > KERNELBASE);
14647         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14648 
14649         rp = VTOR4(vp);
14650         mi = VTOMI4(vp);
14651 
14652         /*
14653          * If we have not locked anything then we can
14654          * just return since we have no work to do.
14655          */
14656         if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14657                 return (0);
14658         }
14659 
14660         /*
14661          * We need to comprehend that another thread may
14662          * kick off recovery and the lock_owner we have stashed
14663          * in lop might be invalid so we should NOT cache it
14664          * locally!
14665          */
14666         recov_state.rs_flags = 0;
14667         recov_state.rs_num_retry_despite_err = 0;
14668         error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14669             &recovonly);
14670         if (error) {
14671                 mutex_enter(&rp->r_statelock);
14672                 rp->r_flags |= R4LODANGLERS;
14673                 mutex_exit(&rp->r_statelock);
14674                 return (error);
14675         }
14676 
14677         lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14678 
14679         /*
14680          * Check if the lock owner might have a lock (request was sent but
14681          * no response was received).  Also check if there are any remote
14682          * locks on the file.  (In theory we shouldn't have to make this
14683          * second check if there's no lock owner, but for now we'll be
14684          * conservative and do it anyway.)  If either condition is true,
14685          * send an unlock for the entire file to the server.
14686          *
14687          * Note that no explicit synchronization is needed here.  At worst,
14688          * flk_has_remote_locks() will return a false positive, in which case
14689          * the unlock call wastes time but doesn't harm correctness.
14690          */
14691 
14692         if (lop) {
14693                 mutex_enter(&lop->lo_lock);
14694                 possible_orphan = lop->lo_pending_rqsts;
14695                 mutex_exit(&lop->lo_lock);
14696                 lock_owner_rele(lop);
14697         }
14698 
14699         nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14700 
14701         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14702             "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14703             "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14704             (void *)lop));
14705 
14706         if (possible_orphan || flk_has_remote_locks(vp)) {
14707                 ld.l_type = F_UNLCK;    /* set to unlock entire file */
14708                 ld.l_whence = 0;        /* unlock from start of file */
14709                 ld.l_start = 0;
14710                 ld.l_len = 0;           /* do entire file */
14711 
14712                 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14713                     cr, NULL);
14714 
14715                 if (ret != 0) {
14716                         /*
14717                          * If VOP_FRLOCK fails, make sure we unregister
14718                          * local locks before we continue.
14719                          */
14720                         ld.l_pid = ttoproc(curthread)->p_pid;
14721                         nfs4_register_lock_locally(vp, &ld, flag, offset);
14722                         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14723                             "nfs4_lockrelease: lock release error on vp"
14724                             " %p: error %d.\n", (void *)vp, ret));
14725                 }
14726         }
14727 
14728         recov_state.rs_flags = 0;
14729         recov_state.rs_num_retry_despite_err = 0;
14730         error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14731             &recovonly);
14732         if (error) {
14733                 mutex_enter(&rp->r_statelock);
14734                 rp->r_flags |= R4LODANGLERS;
14735                 mutex_exit(&rp->r_statelock);
14736                 return (error);
14737         }
14738 
14739         /*
14740          * So, here we're going to need to retrieve the lock-owner
14741          * again (in case recovery has done a switch-a-roo) and
14742          * remove it because we can.
14743          */
14744         lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14745 
14746         if (lop) {
14747                 nfs4_rnode_remove_lock_owner(rp, lop);
14748                 lock_owner_rele(lop);
14749         }
14750 
14751         nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14752         return (0);
14753 }
14754 
14755 /*
14756  * Wait for 'tick_delay' clock ticks.
14757  * Implement exponential backoff until hit the lease_time of this nfs4_server.
14758  *
14759  * The client should retry to acquire the lock faster than the lease period.
14760  * We use roughly half of the lease time to use a similar calculation as it is
14761  * used in nfs4_renew_lease_thread().
14762  *
14763  * XXX For future improvements, should implement a waiting queue scheme.
14764  */
14765 static int
14766 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14767 {
14768         long max_msec_delay = 1 * 1000;         /* 1 sec */
14769         nfs4_server_t *sp;
14770         mntinfo4_t *mi = VTOMI4(RTOV4(rp));
14771 
14772         /* wait tick_delay clock ticks or siginteruptus */
14773         if (delay_sig(*tick_delay)) {
14774                 return (EINTR);
14775         }
14776 
14777         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14778             "reissue the lock request: blocked for %ld clock ticks: %ld "
14779             "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14780 
14781         /*
14782          * Get the current lease time and propagation time for the server
14783          * associated with the given file. Note that both times could
14784          * change immediately after this section.
14785          */
14786         nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
14787         sp = find_nfs4_server(mi);
14788         if (sp != NULL) {
14789                 if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED)) {
14790                         max_msec_delay = sp->s_lease_time * 1000 / 2 -
14791                                         (3 * sp->propagation_delay.tv_sec *
14792                                         1000);
14793                 }
14794                 mutex_exit(&sp->s_lock);
14795                 nfs4_server_rele(sp);
14796         }
14797         nfs_rw_exit(&mi->mi_recovlock);
14798 
14799         max_msec_delay = MAX(max_msec_delay, nfs4_base_wait_time);
14800         *tick_delay = MIN(drv_usectohz(max_msec_delay * 1000), *tick_delay * 2);
14801         return (0);
14802 }
14803 
14804 void
14805 nfs4_vnops_init(void)
14806 {
14807 }
14808 
14809 void
14810 nfs4_vnops_fini(void)
14811 {
14812 }
14813 
14814 /*
14815  * Return a reference to the directory (parent) vnode for a given vnode,
14816  * using the saved pathname information and the directory file handle.  The
14817  * caller is responsible for disposing of the reference.
14818  * Returns zero or an errno value.
14819  *
14820  * Caller should set need_start_op to FALSE if it is the recovery
14821  * thread, or if a start_fop has already been done.  Otherwise, TRUE.
14822  */
14823 int
14824 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14825 {
14826         svnode_t *svnp;
14827         vnode_t *dvp = NULL;
14828         servinfo4_t *svp;
14829         nfs4_fname_t *mfname;
14830         int error;
14831 
14832         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14833 
14834         if (vp->v_flag & VROOT) {
14835                 nfs4_sharedfh_t *sfh;
14836                 nfs_fh4 fh;
14837                 mntinfo4_t *mi;
14838 
14839                 ASSERT(vp->v_type == VREG);
14840 
14841                 mi = VTOMI4(vp);
14842                 svp = mi->mi_curr_serv;
14843                 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14844                 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14845                 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14846                 sfh = sfh4_get(&fh, VTOMI4(vp));
14847                 nfs_rw_exit(&svp->sv_lock);
14848                 mfname = mi->mi_fname;
14849                 fn_hold(mfname);
14850                 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14851                 sfh4_rele(&sfh);
14852 
14853                 if (dvp->v_type == VNON)
14854                         dvp->v_type = VDIR;
14855                 *dvpp = dvp;
14856                 return (0);
14857         }
14858 
14859         svnp = VTOSV(vp);
14860 
14861         if (svnp == NULL) {
14862                 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14863                     "shadow node is NULL"));
14864                 return (EINVAL);
14865         }
14866 
14867         if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14868                 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14869                     "shadow node name or dfh val == NULL"));
14870                 return (EINVAL);
14871         }
14872 
14873         error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14874             (int)need_start_op);
14875         if (error != 0) {
14876                 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14877                     "nfs4_make_dotdot returned %d", error));
14878                 return (error);
14879         }
14880         if (!dvp) {
14881                 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14882                     "nfs4_make_dotdot returned a NULL dvp"));
14883                 return (EIO);
14884         }
14885         if (dvp->v_type == VNON)
14886                 dvp->v_type = VDIR;
14887         ASSERT(dvp->v_type == VDIR);
14888         if (VTOR4(vp)->r_flags & R4ISXATTR) {
14889                 mutex_enter(&dvp->v_lock);
14890                 dvp->v_flag |= V_XATTRDIR;
14891                 mutex_exit(&dvp->v_lock);
14892         }
14893         *dvpp = dvp;
14894         return (0);
14895 }
14896 
14897 /*
14898  * Copy the (final) component name of vp to fnamep.  maxlen is the maximum
14899  * length that fnamep can accept, including the trailing null.
14900  * Returns 0 if okay, returns an errno value if there was a problem.
14901  */
14902 
14903 int
14904 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14905 {
14906         char *fn;
14907         int err = 0;
14908         servinfo4_t *svp;
14909         svnode_t *shvp;
14910 
14911         /*
14912          * If the file being opened has VROOT set, then this is
14913          * a "file" mount.  sv_name will not be interesting, so
14914          * go back to the servinfo4 to get the original mount
14915          * path and strip off all but the final edge.  Otherwise
14916          * just return the name from the shadow vnode.
14917          */
14918 
14919         if (vp->v_flag & VROOT) {
14920 
14921                 svp = VTOMI4(vp)->mi_curr_serv;
14922                 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14923 
14924                 fn = strrchr(svp->sv_path, '/');
14925                 if (fn == NULL)
14926                         err = EINVAL;
14927                 else
14928                         fn++;
14929         } else {
14930                 shvp = VTOSV(vp);
14931                 fn = fn_name(shvp->sv_name);
14932         }
14933 
14934         if (err == 0)
14935                 if (strlen(fn) < maxlen)
14936                         (void) strcpy(fnamep, fn);
14937                 else
14938                         err = ENAMETOOLONG;
14939 
14940         if (vp->v_flag & VROOT)
14941                 nfs_rw_exit(&svp->sv_lock);
14942         else
14943                 kmem_free(fn, MAXNAMELEN);
14944 
14945         return (err);
14946 }
14947 
14948 /*
14949  * Bookkeeping for a close that doesn't need to go over the wire.
14950  * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14951  * it is left at 1.
14952  */
14953 void
14954 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14955 {
14956         rnode4_t                *rp;
14957         mntinfo4_t              *mi;
14958 
14959         mi = VTOMI4(vp);
14960         rp = VTOR4(vp);
14961 
14962         NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14963             "rp=%p osp=%p", (void *)rp, (void *)osp));
14964         ASSERT(nfs_zone() == mi->mi_zone);
14965         ASSERT(mutex_owned(&osp->os_sync_lock));
14966         ASSERT(*have_lockp);
14967 
14968         if (!osp->os_valid ||
14969             osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14970                 return;
14971         }
14972 
14973         /*
14974          * This removes the reference obtained at OPEN; ie,
14975          * when the open stream structure was created.
14976          *
14977          * We don't have to worry about calling 'open_stream_rele'
14978          * since we our currently holding a reference to this
14979          * open stream which means the count can not go to 0 with
14980          * this decrement.
14981          */
14982         ASSERT(osp->os_ref_count >= 2);
14983         osp->os_ref_count--;
14984         osp->os_valid = 0;
14985         mutex_exit(&osp->os_sync_lock);
14986         *have_lockp = 0;
14987 
14988         nfs4_dec_state_ref_count(mi);
14989 }
14990 
14991 /*
14992  * Close all remaining open streams on the rnode.  These open streams
14993  * could be here because:
14994  * - The close attempted at either close or delmap failed
14995  * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14996  * - Someone did mknod on a regular file but never opened it
14997  */
14998 int
14999 nfs4close_all(vnode_t *vp, cred_t *cr)
15000 {
15001         nfs4_open_stream_t *osp;
15002         int error;
15003         nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
15004         rnode4_t *rp;
15005 
15006         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15007 
15008         error = 0;
15009         rp = VTOR4(vp);
15010 
15011         /*
15012          * At this point, all we know is that the last time
15013          * someone called vn_rele, the count was 1.  Since then,
15014          * the vnode could have been re-activated.  We want to
15015          * loop through the open streams and close each one, but
15016          * we have to be careful since once we release the rnode
15017          * hash bucket lock, someone else is free to come in and
15018          * re-activate the rnode and add new open streams.  The
15019          * strategy is take the rnode hash bucket lock, verify that
15020          * the count is still 1, grab the open stream off the
15021          * head of the list and mark it invalid, then release the
15022          * rnode hash bucket lock and proceed with that open stream.
15023          * This is ok because nfs4close_one() will acquire the proper
15024          * open/create to close/destroy synchronization for open
15025          * streams, and will ensure that if someone has reopened
15026          * the open stream after we've dropped the hash bucket lock
15027          * then we'll just simply return without destroying the
15028          * open stream.
15029          * Repeat until the list is empty.
15030          */
15031 
15032         for (;;) {
15033 
15034                 /* make sure vnode hasn't been reactivated */
15035                 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15036                 mutex_enter(&vp->v_lock);
15037                 if (vp->v_count > 1) {
15038                         mutex_exit(&vp->v_lock);
15039                         rw_exit(&rp->r_hashq->r_lock);
15040                         break;
15041                 }
15042                 /*
15043                  * Grabbing r_os_lock before releasing v_lock prevents
15044                  * a window where the rnode/open stream could get
15045                  * reactivated (and os_force_close set to 0) before we
15046                  * had a chance to set os_force_close to 1.
15047                  */
15048                 mutex_enter(&rp->r_os_lock);
15049                 mutex_exit(&vp->v_lock);
15050 
15051                 osp = list_head(&rp->r_open_streams);
15052                 if (!osp) {
15053                         /* nothing left to CLOSE OTW, so return */
15054                         mutex_exit(&rp->r_os_lock);
15055                         rw_exit(&rp->r_hashq->r_lock);
15056                         break;
15057                 }
15058 
15059                 mutex_enter(&rp->r_statev4_lock);
15060                 /* the file can't still be mem mapped */
15061                 ASSERT(rp->r_mapcnt == 0);
15062                 if (rp->created_v4)
15063                         rp->created_v4 = 0;
15064                 mutex_exit(&rp->r_statev4_lock);
15065 
15066                 /*
15067                  * Grab a ref on this open stream; nfs4close_one
15068                  * will mark it as invalid
15069                  */
15070                 mutex_enter(&osp->os_sync_lock);
15071                 osp->os_ref_count++;
15072                 osp->os_force_close = 1;
15073                 mutex_exit(&osp->os_sync_lock);
15074                 mutex_exit(&rp->r_os_lock);
15075                 rw_exit(&rp->r_hashq->r_lock);
15076 
15077                 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15078 
15079                 /* Update error if it isn't already non-zero */
15080                 if (error == 0) {
15081                         if (e.error)
15082                                 error = e.error;
15083                         else if (e.stat)
15084                                 error = geterrno4(e.stat);
15085                 }
15086 
15087 #ifdef  DEBUG
15088                 nfs4close_all_cnt++;
15089 #endif
15090                 /* Release the ref on osp acquired above. */
15091                 open_stream_rele(osp, rp);
15092 
15093                 /* Proceed to the next open stream, if any */
15094         }
15095         return (error);
15096 }
15097 
15098 /*
15099  * nfs4close_one - close one open stream for a file if needed.
15100  *
15101  * "close_type" indicates which close path this is:
15102  * CLOSE_NORM: close initiated via VOP_CLOSE.
15103  * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15104  * CLOSE_FORCE: close initiated via VOP_INACTIVE.  This path forces
15105  *      the close and release of client state for this open stream
15106  *      (unless someone else has the open stream open).
15107  * CLOSE_RESEND: indicates the request is a replay of an earlier request
15108  *      (e.g., due to abort because of a signal).
15109  * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15110  *
15111  * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15112  * recovery.  Instead, the caller is expected to deal with retries.
15113  *
15114  * The caller can either pass in the osp ('provided_osp') or not.
15115  *
15116  * 'access_bits' represents the access we are closing/downgrading.
15117  *
15118  * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP.  'len' is the
15119  * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15120  * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15121  *
15122  * Errors are returned via the nfs4_error_t.
15123  */
15124 void
15125 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15126     int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15127     nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15128     uint_t mmap_flags)
15129 {
15130         nfs4_open_owner_t *oop;
15131         nfs4_open_stream_t *osp = NULL;
15132         int retry = 0;
15133         int num_retries = NFS4_NUM_RECOV_RETRIES;
15134         rnode4_t *rp;
15135         mntinfo4_t *mi;
15136         nfs4_recov_state_t recov_state;
15137         cred_t *cred_otw = NULL;
15138         bool_t recovonly = FALSE;
15139         int isrecov;
15140         int force_close;
15141         int close_failed = 0;
15142         int did_dec_count = 0;
15143         int did_start_op = 0;
15144         int did_force_recovlock = 0;
15145         int did_start_seqid_sync = 0;
15146         int have_sync_lock = 0;
15147 
15148         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15149 
15150         NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15151             "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15152             (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15153             len, maxprot, mmap_flags, access_bits));
15154 
15155         nfs4_error_zinit(ep);
15156         rp = VTOR4(vp);
15157         mi = VTOMI4(vp);
15158         isrecov = (close_type == CLOSE_RESEND ||
15159             close_type == CLOSE_AFTER_RESEND);
15160 
15161         /*
15162          * First get the open owner.
15163          */
15164         if (!provided_osp) {
15165                 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15166         } else {
15167                 oop = provided_osp->os_open_owner;
15168                 ASSERT(oop != NULL);
15169                 open_owner_hold(oop);
15170         }
15171 
15172         if (!oop) {
15173                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15174                     "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15175                     "close type %d", (void *)rp, (void *)mi, (void *)cr,
15176                     (void *)provided_osp, close_type));
15177                 ep->error = EIO;
15178                 goto out;
15179         }
15180 
15181         cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15182 recov_retry:
15183         osp = NULL;
15184         close_failed = 0;
15185         force_close = (close_type == CLOSE_FORCE);
15186         retry = 0;
15187         did_start_op = 0;
15188         did_force_recovlock = 0;
15189         did_start_seqid_sync = 0;
15190         have_sync_lock = 0;
15191         recovonly = FALSE;
15192         recov_state.rs_flags = 0;
15193         recov_state.rs_num_retry_despite_err = 0;
15194 
15195         /*
15196          * Second synchronize with recovery.
15197          */
15198         if (!isrecov) {
15199                 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15200                     &recov_state, &recovonly);
15201                 if (!ep->error) {
15202                         did_start_op = 1;
15203                 } else {
15204                         close_failed = 1;
15205                         /*
15206                          * If we couldn't get start_fop, but have to
15207                          * cleanup state, then at least acquire the
15208                          * mi_recovlock so we can synchronize with
15209                          * recovery.
15210                          */
15211                         if (close_type == CLOSE_FORCE) {
15212                                 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15213                                     RW_READER, FALSE);
15214                                 did_force_recovlock = 1;
15215                         } else
15216                                 goto out;
15217                 }
15218         }
15219 
15220         /*
15221          * We cannot attempt to get the open seqid sync if nfs4_start_fop
15222          * set 'recovonly' to TRUE since most likely this is due to
15223          * reovery being active (MI4_RECOV_ACTIV).  If recovery is active,
15224          * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15225          * to retry, causing us to loop until recovery finishes.  Plus we
15226          * don't need protection over the open seqid since we're not going
15227          * OTW, hence don't need to use the seqid.
15228          */
15229         if (recovonly == FALSE) {
15230                 /* need to grab the open owner sync before 'os_sync_lock' */
15231                 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15232                 if (ep->error == EAGAIN) {
15233                         ASSERT(!isrecov);
15234                         if (did_start_op)
15235                                 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15236                                     &recov_state, TRUE);
15237                         if (did_force_recovlock)
15238                                 nfs_rw_exit(&mi->mi_recovlock);
15239                         goto recov_retry;
15240                 }
15241                 did_start_seqid_sync = 1;
15242         }
15243 
15244         /*
15245          * Third get an open stream and acquire 'os_sync_lock' to
15246          * sychronize the opening/creating of an open stream with the
15247          * closing/destroying of an open stream.
15248          */
15249         if (!provided_osp) {
15250                 /* returns with 'os_sync_lock' held */
15251                 osp = find_open_stream(oop, rp);
15252                 if (!osp) {
15253                         ep->error = EIO;
15254                         goto out;
15255                 }
15256         } else {
15257                 osp = provided_osp;
15258                 open_stream_hold(osp);
15259                 mutex_enter(&osp->os_sync_lock);
15260         }
15261         have_sync_lock = 1;
15262 
15263         ASSERT(oop == osp->os_open_owner);
15264 
15265         /*
15266          * Fourth, do any special pre-OTW CLOSE processing
15267          * based on the specific close type.
15268          */
15269         if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15270             !did_dec_count) {
15271                 ASSERT(osp->os_open_ref_count > 0);
15272                 osp->os_open_ref_count--;
15273                 did_dec_count = 1;
15274                 if (osp->os_open_ref_count == 0)
15275                         osp->os_final_close = 1;
15276         }
15277 
15278         if (close_type == CLOSE_FORCE) {
15279                 /* see if somebody reopened the open stream. */
15280                 if (!osp->os_force_close) {
15281                         NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15282                             "nfs4close_one: skip CLOSE_FORCE as osp %p "
15283                             "was reopened, vp %p", (void *)osp, (void *)vp));
15284                         ep->error = 0;
15285                         ep->stat = NFS4_OK;
15286                         goto out;
15287                 }
15288 
15289                 if (!osp->os_final_close && !did_dec_count) {
15290                         osp->os_open_ref_count--;
15291                         did_dec_count = 1;
15292                 }
15293 
15294                 /*
15295                  * We can't depend on os_open_ref_count being 0 due to the
15296                  * way executables are opened (VN_RELE to match a VOP_OPEN).
15297                  */
15298 #ifdef  NOTYET
15299                 ASSERT(osp->os_open_ref_count == 0);
15300 #endif
15301                 if (osp->os_open_ref_count != 0) {
15302                         NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15303                             "nfs4close_one: should panic here on an "
15304                             "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15305                             "since this is probably the exec problem."));
15306 
15307                         osp->os_open_ref_count = 0;
15308                 }
15309 
15310                 /*
15311                  * There is the possibility that nfs4close_one()
15312                  * for close_type == CLOSE_DELMAP couldn't find the
15313                  * open stream, thus couldn't decrement its os_mapcnt;
15314                  * therefore we can't use this ASSERT yet.
15315                  */
15316 #ifdef  NOTYET
15317                 ASSERT(osp->os_mapcnt == 0);
15318 #endif
15319                 osp->os_mapcnt = 0;
15320         }
15321 
15322         if (close_type == CLOSE_DELMAP && !did_dec_count) {
15323                 ASSERT(osp->os_mapcnt >= btopr(len));
15324 
15325                 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15326                         osp->os_mmap_write -= btopr(len);
15327                 if (maxprot & PROT_READ)
15328                         osp->os_mmap_read -= btopr(len);
15329                 if (maxprot & PROT_EXEC)
15330                         osp->os_mmap_read -= btopr(len);
15331                 /* mirror the PROT_NONE check in nfs4_addmap() */
15332                 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15333                     !(maxprot & PROT_EXEC))
15334                         osp->os_mmap_read -= btopr(len);
15335                 osp->os_mapcnt -= btopr(len);
15336                 did_dec_count = 1;
15337         }
15338 
15339         if (recovonly) {
15340                 nfs4_lost_rqst_t lost_rqst;
15341 
15342                 /* request should not already be in recovery queue */
15343                 ASSERT(lrp == NULL);
15344                 nfs4_error_init(ep, EINTR);
15345                 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15346                     osp, cred_otw, vp);
15347                 mutex_exit(&osp->os_sync_lock);
15348                 have_sync_lock = 0;
15349                 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15350                     lost_rqst.lr_op == OP_CLOSE ?
15351                     &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15352                 close_failed = 1;
15353                 force_close = 0;
15354                 goto close_cleanup;
15355         }
15356 
15357         /*
15358          * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15359          * we stopped operating on the open owner's <old oo_name, old seqid>
15360          * space, which means we stopped operating on the open stream
15361          * too.  So don't go OTW (as the seqid is likely bad, and the
15362          * stateid could be stale, potentially triggering a false
15363          * setclientid), and just clean up the client's internal state.
15364          */
15365         if (osp->os_orig_oo_name != oop->oo_name) {
15366                 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15367                     (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15368                     "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15369                     "oo_name %" PRIx64")",
15370                     (void *)osp, (void *)oop, osp->os_orig_oo_name,
15371                     oop->oo_name));
15372                 close_failed = 1;
15373         }
15374 
15375         /* If the file failed recovery, just quit. */
15376         mutex_enter(&rp->r_statelock);
15377         if (rp->r_flags & R4RECOVERR) {
15378                 close_failed = 1;
15379         }
15380         mutex_exit(&rp->r_statelock);
15381 
15382         /*
15383          * If the force close path failed to obtain start_fop
15384          * then skip the OTW close and just remove the state.
15385          */
15386         if (close_failed)
15387                 goto close_cleanup;
15388 
15389         /*
15390          * Fifth, check to see if there are still mapped pages or other
15391          * opens using this open stream.  If there are then we can't
15392          * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15393          */
15394         if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15395                 nfs4_lost_rqst_t        new_lost_rqst;
15396                 bool_t                  needrecov = FALSE;
15397                 cred_t                  *odg_cred_otw = NULL;
15398                 seqid4                  open_dg_seqid = 0;
15399 
15400                 if (osp->os_delegation) {
15401                         /*
15402                          * If this open stream was never OPENed OTW then we
15403                          * surely can't DOWNGRADE it (especially since the
15404                          * osp->open_stateid is really a delegation stateid
15405                          * when os_delegation is 1).
15406                          */
15407                         if (access_bits & FREAD)
15408                                 osp->os_share_acc_read--;
15409                         if (access_bits & FWRITE)
15410                                 osp->os_share_acc_write--;
15411                         osp->os_share_deny_none--;
15412                         nfs4_error_zinit(ep);
15413                         goto out;
15414                 }
15415                 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15416                     lrp, ep, &odg_cred_otw, &open_dg_seqid);
15417                 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15418                 if (needrecov && !isrecov) {
15419                         bool_t abort;
15420                         nfs4_bseqid_entry_t *bsep = NULL;
15421 
15422                         if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15423                                 bsep = nfs4_create_bseqid_entry(oop, NULL,
15424                                     vp, 0,
15425                                     lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15426                                     open_dg_seqid);
15427 
15428                         nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15429                             oop, osp, odg_cred_otw, vp, access_bits, 0);
15430                         mutex_exit(&osp->os_sync_lock);
15431                         have_sync_lock = 0;
15432                         abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15433                             new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15434                             &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15435                             bsep, NULL, NULL);
15436                         if (odg_cred_otw)
15437                                 crfree(odg_cred_otw);
15438                         if (bsep)
15439                                 kmem_free(bsep, sizeof (*bsep));
15440 
15441                         if (abort == TRUE)
15442                                 goto out;
15443 
15444                         if (did_start_seqid_sync) {
15445                                 nfs4_end_open_seqid_sync(oop);
15446                                 did_start_seqid_sync = 0;
15447                         }
15448                         open_stream_rele(osp, rp);
15449 
15450                         if (did_start_op)
15451                                 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15452                                     &recov_state, FALSE);
15453                         if (did_force_recovlock)
15454                                 nfs_rw_exit(&mi->mi_recovlock);
15455 
15456                         goto recov_retry;
15457                 } else {
15458                         if (odg_cred_otw)
15459                                 crfree(odg_cred_otw);
15460                 }
15461                 goto out;
15462         }
15463 
15464         /*
15465          * If this open stream was created as the results of an open
15466          * while holding a delegation, then just release it; no need
15467          * to do an OTW close.  Otherwise do a "normal" OTW close.
15468          */
15469         if (osp->os_delegation) {
15470                 nfs4close_notw(vp, osp, &have_sync_lock);
15471                 nfs4_error_zinit(ep);
15472                 goto out;
15473         }
15474 
15475         /*
15476          * If this stream is not valid, we're done.
15477          */
15478         if (!osp->os_valid) {
15479                 nfs4_error_zinit(ep);
15480                 goto out;
15481         }
15482 
15483         /*
15484          * Last open or mmap ref has vanished, need to do an OTW close.
15485          * First check to see if a close is still necessary.
15486          */
15487         if (osp->os_failed_reopen) {
15488                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15489                     "don't close OTW osp %p since reopen failed.",
15490                     (void *)osp));
15491                 /*
15492                  * Reopen of the open stream failed, hence the
15493                  * stateid of the open stream is invalid/stale, and
15494                  * sending this OTW would incorrectly cause another
15495                  * round of recovery.  In this case, we need to set
15496                  * the 'os_valid' bit to 0 so another thread doesn't
15497                  * come in and re-open this open stream before
15498                  * this "closing" thread cleans up state (decrementing
15499                  * the nfs4_server_t's state_ref_count and decrementing
15500                  * the os_ref_count).
15501                  */
15502                 osp->os_valid = 0;
15503                 /*
15504                  * This removes the reference obtained at OPEN; ie,
15505                  * when the open stream structure was created.
15506                  *
15507                  * We don't have to worry about calling 'open_stream_rele'
15508                  * since we our currently holding a reference to this
15509                  * open stream which means the count can not go to 0 with
15510                  * this decrement.
15511                  */
15512                 ASSERT(osp->os_ref_count >= 2);
15513                 osp->os_ref_count--;
15514                 nfs4_error_zinit(ep);
15515                 close_failed = 0;
15516                 goto close_cleanup;
15517         }
15518 
15519         ASSERT(osp->os_ref_count > 1);
15520 
15521         /*
15522          * Sixth, try the CLOSE OTW.
15523          */
15524         nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15525             close_type, ep, &have_sync_lock);
15526 
15527         if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15528                 /*
15529                  * Let the recovery thread be responsible for
15530                  * removing the state for CLOSE.
15531                  */
15532                 close_failed = 1;
15533                 force_close = 0;
15534                 retry = 0;
15535         }
15536 
15537         /* See if we need to retry with a different cred */
15538         if ((ep->error == EACCES ||
15539             (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15540             cred_otw != cr) {
15541                 crfree(cred_otw);
15542                 cred_otw = cr;
15543                 crhold(cred_otw);
15544                 retry = 1;
15545         }
15546 
15547         if (ep->error || ep->stat)
15548                 close_failed = 1;
15549 
15550         if (retry && !isrecov && num_retries-- > 0) {
15551                 if (have_sync_lock) {
15552                         mutex_exit(&osp->os_sync_lock);
15553                         have_sync_lock = 0;
15554                 }
15555                 if (did_start_seqid_sync) {
15556                         nfs4_end_open_seqid_sync(oop);
15557                         did_start_seqid_sync = 0;
15558                 }
15559                 open_stream_rele(osp, rp);
15560 
15561                 if (did_start_op)
15562                         nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15563                             &recov_state, FALSE);
15564                 if (did_force_recovlock)
15565                         nfs_rw_exit(&mi->mi_recovlock);
15566                 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15567                     "nfs4close_one: need to retry the close "
15568                     "operation"));
15569                 goto recov_retry;
15570         }
15571 close_cleanup:
15572         /*
15573          * Seventh and lastly, process our results.
15574          */
15575         if (close_failed && force_close) {
15576                 /*
15577                  * It's ok to drop and regrab the 'os_sync_lock' since
15578                  * nfs4close_notw() will recheck to make sure the
15579                  * "close"/removal of state should happen.
15580                  */
15581                 if (!have_sync_lock) {
15582                         mutex_enter(&osp->os_sync_lock);
15583                         have_sync_lock = 1;
15584                 }
15585                 /*
15586                  * This is last call, remove the ref on the open
15587                  * stream created by open and clean everything up.
15588                  */
15589                 osp->os_pending_close = 0;
15590                 nfs4close_notw(vp, osp, &have_sync_lock);
15591                 nfs4_error_zinit(ep);
15592         }
15593 
15594         if (!close_failed) {
15595                 if (have_sync_lock) {
15596                         osp->os_pending_close = 0;
15597                         mutex_exit(&osp->os_sync_lock);
15598                         have_sync_lock = 0;
15599                 } else {
15600                         mutex_enter(&osp->os_sync_lock);
15601                         osp->os_pending_close = 0;
15602                         mutex_exit(&osp->os_sync_lock);
15603                 }
15604                 if (did_start_op && recov_state.rs_sp != NULL) {
15605                         mutex_enter(&recov_state.rs_sp->s_lock);
15606                         nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15607                         mutex_exit(&recov_state.rs_sp->s_lock);
15608                 } else {
15609                         nfs4_dec_state_ref_count(mi);
15610                 }
15611                 nfs4_error_zinit(ep);
15612         }
15613 
15614 out:
15615         if (have_sync_lock)
15616                 mutex_exit(&osp->os_sync_lock);
15617         if (did_start_op)
15618                 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15619                     recovonly ? TRUE : FALSE);
15620         if (did_force_recovlock)
15621                 nfs_rw_exit(&mi->mi_recovlock);
15622         if (cred_otw)
15623                 crfree(cred_otw);
15624         if (osp)
15625                 open_stream_rele(osp, rp);
15626         if (oop) {
15627                 if (did_start_seqid_sync)
15628                         nfs4_end_open_seqid_sync(oop);
15629                 open_owner_rele(oop);
15630         }
15631 }
15632 
15633 /*
15634  * Convert information returned by the server in the LOCK4denied
15635  * structure to the form required by fcntl.
15636  */
15637 static void
15638 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15639 {
15640         nfs4_lo_name_t *lo;
15641 
15642 #ifdef  DEBUG
15643         if (denied_to_flk_debug) {
15644                 lockt_denied_debug = lockt_denied;
15645                 debug_enter("lockt_denied");
15646         }
15647 #endif
15648 
15649         flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15650         flk->l_whence = 0;   /* aka SEEK_SET */
15651         flk->l_start = lockt_denied->offset;
15652         flk->l_len = lockt_denied->length;
15653 
15654         /*
15655          * If the blocking clientid matches our client id, then we can
15656          * interpret the lockowner (since we built it).  If not, then
15657          * fabricate a sysid and pid.  Note that the l_sysid field
15658          * in *flk already has the local sysid.
15659          */
15660 
15661         if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15662 
15663                 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15664                         lo = (nfs4_lo_name_t *)
15665                             lockt_denied->owner.owner_val;
15666 
15667                         flk->l_pid = lo->ln_pid;
15668                 } else {
15669                         NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15670                             "denied_to_flk: bad lock owner length\n"));
15671 
15672                         flk->l_pid = lo_to_pid(&lockt_denied->owner);
15673                 }
15674         } else {
15675                 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15676                 "denied_to_flk: foreign clientid\n"));
15677 
15678                 /*
15679                  * Construct a new sysid which should be different from
15680                  * sysids of other systems.
15681                  */
15682 
15683                 flk->l_sysid++;
15684                 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15685         }
15686 }
15687 
15688 static pid_t
15689 lo_to_pid(lock_owner4 *lop)
15690 {
15691         pid_t pid = 0;
15692         uchar_t *cp;
15693         int i;
15694 
15695         cp = (uchar_t *)&lop->clientid;
15696 
15697         for (i = 0; i < sizeof (lop->clientid); i++)
15698                 pid += (pid_t)*cp++;
15699 
15700         cp = (uchar_t *)lop->owner_val;
15701 
15702         for (i = 0; i < lop->owner_len; i++)
15703                 pid += (pid_t)*cp++;
15704 
15705         return (pid);
15706 }
15707 
15708 /*
15709  * Given a lock pointer, returns the length of that lock.
15710  * "end" is the last locked offset the "l_len" covers from
15711  * the start of the lock.
15712  */
15713 static off64_t
15714 lock_to_end(flock64_t *lock)
15715 {
15716         off64_t lock_end;
15717 
15718         if (lock->l_len == 0)
15719                 lock_end = (off64_t)MAXEND;
15720         else
15721                 lock_end = lock->l_start + lock->l_len - 1;
15722 
15723         return (lock_end);
15724 }
15725 
15726 /*
15727  * Given the end of a lock, it will return you the length "l_len" for that lock.
15728  */
15729 static off64_t
15730 end_to_len(off64_t start, off64_t end)
15731 {
15732         off64_t lock_len;
15733 
15734         ASSERT(end >= start);
15735         if (end == MAXEND)
15736                 lock_len = 0;
15737         else
15738                 lock_len = end - start + 1;
15739 
15740         return (lock_len);
15741 }
15742 
15743 /*
15744  * On given end for a lock it determines if it is the last locked offset
15745  * or not, if so keeps it as is, else adds one to return the length for
15746  * valid start.
15747  */
15748 static off64_t
15749 start_check(off64_t x)
15750 {
15751         if (x == MAXEND)
15752                 return (x);
15753         else
15754                 return (x + 1);
15755 }
15756 
15757 /*
15758  * See if these two locks overlap, and if so return 1;
15759  * otherwise, return 0.
15760  */
15761 static int
15762 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15763 {
15764         off64_t llfp_end, curfp_end;
15765 
15766         llfp_end = lock_to_end(llfp);
15767         curfp_end = lock_to_end(curfp);
15768 
15769         if (((llfp_end >= curfp->l_start) &&
15770             (llfp->l_start <= curfp->l_start)) ||
15771             ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15772                 return (1);
15773         return (0);
15774 }
15775 
15776 /*
15777  * Determine what the intersecting lock region is, and add that to the
15778  * 'nl_llpp' locklist in increasing order (by l_start).
15779  */
15780 static void
15781 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15782     locklist_t **nl_llpp, vnode_t *vp)
15783 {
15784         locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15785         off64_t lost_flp_end, local_flp_end, len, start;
15786 
15787         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15788 
15789         if (!locks_intersect(lost_flp, local_flp))
15790                 return;
15791 
15792         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15793             "locks intersect"));
15794 
15795         lost_flp_end = lock_to_end(lost_flp);
15796         local_flp_end = lock_to_end(local_flp);
15797 
15798         /* Find the starting point of the intersecting region */
15799         if (local_flp->l_start > lost_flp->l_start)
15800                 start = local_flp->l_start;
15801         else
15802                 start = lost_flp->l_start;
15803 
15804         /* Find the lenght of the intersecting region */
15805         if (lost_flp_end < local_flp_end)
15806                 len = end_to_len(start, lost_flp_end);
15807         else
15808                 len = end_to_len(start, local_flp_end);
15809 
15810         /*
15811          * Prepare the flock structure for the intersection found and insert
15812          * it into the new list in increasing l_start order. This list contains
15813          * intersections of locks registered by the client with the local host
15814          * and the lost lock.
15815          * The lock type of this lock is the same as that of the local_flp.
15816          */
15817         intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15818         intersect_llp->ll_flock.l_start = start;
15819         intersect_llp->ll_flock.l_len = len;
15820         intersect_llp->ll_flock.l_type = local_flp->l_type;
15821         intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15822         intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15823         intersect_llp->ll_flock.l_whence = 0;        /* aka SEEK_SET */
15824         intersect_llp->ll_vp = vp;
15825 
15826         tmp_fllp = *nl_llpp;
15827         cur_fllp = NULL;
15828         while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15829             intersect_llp->ll_flock.l_start) {
15830                         cur_fllp = tmp_fllp;
15831                         tmp_fllp = tmp_fllp->ll_next;
15832         }
15833         if (cur_fllp == NULL) {
15834                 /* first on the list */
15835                 intersect_llp->ll_next = *nl_llpp;
15836                 *nl_llpp = intersect_llp;
15837         } else {
15838                 intersect_llp->ll_next = cur_fllp->ll_next;
15839                 cur_fllp->ll_next = intersect_llp;
15840         }
15841 
15842         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15843             "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15844             intersect_llp->ll_flock.l_start,
15845             intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15846             intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15847 }
15848 
15849 /*
15850  * Our local locking current state is potentially different than
15851  * what the NFSv4 server thinks we have due to a lost lock that was
15852  * resent and then received.  We need to reset our "NFSv4" locking
15853  * state to match the current local locking state for this pid since
15854  * that is what the user/application sees as what the world is.
15855  *
15856  * We cannot afford to drop the open/lock seqid sync since then we can
15857  * get confused about what the current local locking state "is" versus
15858  * "was".
15859  *
15860  * If we are unable to fix up the locks, we send SIGLOST to the affected
15861  * process.  This is not done if the filesystem has been forcibly
15862  * unmounted, in case the process has already exited and a new process
15863  * exists with the same pid.
15864  */
15865 static void
15866 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15867     nfs4_lock_owner_t *lop)
15868 {
15869         locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15870         mntinfo4_t *mi = VTOMI4(vp);
15871         const int cmd = F_SETLK;
15872         off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15873         flock64_t ul_fl;
15874 
15875         NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15876             "nfs4_reinstitute_local_lock_state"));
15877 
15878         /*
15879          * Find active locks for this vp from the local locking code.
15880          * Scan through this list and find out the locks that intersect with
15881          * the lost lock. Once we find the lock that intersects, add the
15882          * intersection area as a new lock to a new list "ri_llp". The lock
15883          * type of the intersection region lock added to ri_llp is the same
15884          * as that found in the active lock list, "list". The intersecting
15885          * region locks are added to ri_llp in increasing l_start order.
15886          */
15887         ASSERT(nfs_zone() == mi->mi_zone);
15888 
15889         locks = flk_active_locks_for_vp(vp);
15890         ri_llp = NULL;
15891 
15892         for (llp = locks; llp != NULL; llp = llp->ll_next) {
15893                 ASSERT(llp->ll_vp == vp);
15894                 /*
15895                  * Pick locks that belong to this pid/lockowner
15896                  */
15897                 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15898                         continue;
15899 
15900                 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15901         }
15902 
15903         /*
15904          * Now we have the list of intersections with the lost lock. These are
15905          * the locks that were/are active before the server replied to the
15906          * last/lost lock. Issue these locks to the server here. Playing these
15907          * locks to the server will re-establish aur current local locking state
15908          * with the v4 server.
15909          * If we get an error, send SIGLOST to the application for that lock.
15910          */
15911 
15912         for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15913                 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15914                     "nfs4_reinstitute_local_lock_state: need to issue "
15915                     "flock: [%"PRIx64" - %"PRIx64"] : %s",
15916                     llp->ll_flock.l_start,
15917                     llp->ll_flock.l_start + llp->ll_flock.l_len,
15918                     llp->ll_flock.l_type == F_RDLCK ? "READ" :
15919                     llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15920                 /*
15921                  * No need to relock what we already have
15922                  */
15923                 if (llp->ll_flock.l_type == lost_flp->l_type)
15924                         continue;
15925 
15926                 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15927         }
15928 
15929         /*
15930          * Now keeping the start of the lost lock as our reference parse the
15931          * newly created ri_llp locklist to find the ranges that we have locked
15932          * with the v4 server but not in the current local locking. We need
15933          * to unlock these ranges.
15934          * These ranges can also be reffered to as those ranges, where the lost
15935          * lock does not overlap with the locks in the ri_llp but are locked
15936          * since the server replied to the lost lock.
15937          */
15938         cur_start = lost_flp->l_start;
15939         lost_flp_end = lock_to_end(lost_flp);
15940 
15941         ul_fl.l_type = F_UNLCK;
15942         ul_fl.l_whence = 0;     /* aka SEEK_SET */
15943         ul_fl.l_sysid = lost_flp->l_sysid;
15944         ul_fl.l_pid = lost_flp->l_pid;
15945 
15946         for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15947                 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15948 
15949                 if (llp->ll_flock.l_start <= cur_start) {
15950                         cur_start = start_check(llp_ll_flock_end);
15951                         continue;
15952                 }
15953                 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15954                     "nfs4_reinstitute_local_lock_state: "
15955                     "UNLOCK [%"PRIx64" - %"PRIx64"]",
15956                     cur_start, llp->ll_flock.l_start));
15957 
15958                 ul_fl.l_start = cur_start;
15959                 ul_fl.l_len = end_to_len(cur_start,
15960                     (llp->ll_flock.l_start - 1));
15961 
15962                 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15963                 cur_start = start_check(llp_ll_flock_end);
15964         }
15965 
15966         /*
15967          * In the case where the lost lock ends after all intersecting locks,
15968          * unlock the last part of the lost lock range.
15969          */
15970         if (cur_start != start_check(lost_flp_end)) {
15971                 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15972                     "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15973                     "lost lock region [%"PRIx64" - %"PRIx64"]",
15974                     cur_start, lost_flp->l_start + lost_flp->l_len));
15975 
15976                 ul_fl.l_start = cur_start;
15977                 /*
15978                  * Is it an to-EOF lock? if so unlock till the end
15979                  */
15980                 if (lost_flp->l_len == 0)
15981                         ul_fl.l_len = 0;
15982                 else
15983                         ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15984 
15985                 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15986         }
15987 
15988         if (locks != NULL)
15989                 flk_free_locklist(locks);
15990 
15991         /* Free up our newly created locklist */
15992         for (llp = ri_llp; llp != NULL; ) {
15993                 tmp_llp = llp->ll_next;
15994                 kmem_free(llp, sizeof (locklist_t));
15995                 llp = tmp_llp;
15996         }
15997 
15998         /*
15999          * Now return back to the original calling nfs4frlock()
16000          * and let us naturally drop our seqid syncs.
16001          */
16002 }
16003 
16004 /*
16005  * Create a lost state record for the given lock reinstantiation request
16006  * and push it onto the lost state queue.
16007  */
16008 static void
16009 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
16010     nfs4_lock_owner_t *lop)
16011 {
16012         nfs4_lost_rqst_t req;
16013         nfs_lock_type4 locktype;
16014         nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
16015 
16016         ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
16017 
16018         locktype = flk_to_locktype(cmd, flk->l_type);
16019         nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
16020             NULL, NULL, lop, flk, &req, cr, vp);
16021         (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
16022             (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
16023             &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
16024             NULL, NULL, NULL);
16025 }