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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 29 * All Rights Reserved 30 */ 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/cred.h> 38 #include <sys/time.h> 39 #include <sys/vnode.h> 40 #include <sys/vfs.h> 41 #include <sys/file.h> 42 #include <sys/filio.h> 43 #include <sys/uio.h> 44 #include <sys/buf.h> 45 #include <sys/mman.h> 46 #include <sys/pathname.h> 47 #include <sys/dirent.h> 48 #include <sys/debug.h> 49 #include <sys/vmsystm.h> 50 #include <sys/fcntl.h> 51 #include <sys/flock.h> 52 #include <sys/swap.h> 53 #include <sys/errno.h> 54 #include <sys/strsubr.h> 55 #include <sys/sysmacros.h> 56 #include <sys/kmem.h> 57 #include <sys/cmn_err.h> 58 #include <sys/pathconf.h> 59 #include <sys/utsname.h> 60 #include <sys/dnlc.h> 61 #include <sys/acl.h> 62 #include <sys/systeminfo.h> 63 #include <sys/policy.h> 64 #include <sys/sdt.h> 65 #include <sys/list.h> 66 #include <sys/stat.h> 67 68 #include <rpc/types.h> 69 #include <rpc/auth.h> 70 #include <rpc/clnt.h> 71 72 #include <nfs/nfs.h> 73 #include <nfs/nfs_clnt.h> 74 #include <nfs/nfs_acl.h> 75 #include <nfs/lm.h> 76 #include <nfs/nfs4.h> 77 #include <nfs/nfs4_kprot.h> 78 #include <nfs/rnode4.h> 79 #include <nfs/nfs4_clnt.h> 80 81 #include <vm/hat.h> 82 #include <vm/as.h> 83 #include <vm/page.h> 84 #include <vm/pvn.h> 85 #include <vm/seg.h> 86 #include <vm/seg_map.h> 87 #include <vm/seg_kpm.h> 88 #include <vm/seg_vn.h> 89 90 #include <fs/fs_subr.h> 91 92 #include <sys/ddi.h> 93 #include <sys/int_fmtio.h> 94 95 typedef struct { 96 nfs4_ga_res_t *di_garp; 97 cred_t *di_cred; 98 hrtime_t di_time_call; 99 } dirattr_info_t; 100 101 typedef enum nfs4_acl_op { 102 NFS4_ACL_GET, 103 NFS4_ACL_SET 104 } nfs4_acl_op_t; 105 106 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 107 char *, dirattr_info_t *); 108 109 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 110 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 111 nfs4_error_t *, int *); 112 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 113 cred_t *); 114 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 115 stable_how4 *); 116 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 117 cred_t *, bool_t, struct uio *); 118 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 119 vsecattr_t *); 120 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 121 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 122 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 123 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 124 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 125 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 126 int, vnode_t **, cred_t *); 127 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 128 cred_t *, int, int, enum createmode4, int); 129 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 130 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 131 vnode_t *, char *, cred_t *, nfsstat4 *); 132 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 133 vnode_t *, char *, cred_t *, nfsstat4 *); 134 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 135 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 136 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 137 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 138 page_t *[], size_t, struct seg *, caddr_t, 139 enum seg_rw, cred_t *); 140 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 141 cred_t *); 142 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 143 int, cred_t *); 144 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 145 int, cred_t *); 146 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 147 static void nfs4_set_mod(vnode_t *); 148 static void nfs4_get_commit(vnode_t *); 149 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 150 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 151 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 152 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 153 cred_t *); 154 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 155 cred_t *); 156 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 157 hrtime_t, vnode_t *, cred_t *); 158 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 159 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 160 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 161 u_offset_t); 162 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 163 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 164 static cred_t *state_to_cred(nfs4_open_stream_t *); 165 static int vtoname(vnode_t *, char *, ssize_t); 166 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 167 static pid_t lo_to_pid(lock_owner4 *); 168 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 169 cred_t *, nfs4_lock_owner_t *); 170 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 171 nfs4_lock_owner_t *); 172 static nfs4_open_stream_t *open_and_get_osp(vnode_t *, cred_t *, mntinfo4_t *); 173 static void nfs4_delmap_callback(struct as *, void *, uint_t); 174 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 175 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 176 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 177 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 178 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 179 uid_t, gid_t, int); 180 181 /* 182 * Routines that implement the setting of v4 args for the misc. ops 183 */ 184 static void nfs4args_lock_free(nfs_argop4 *); 185 static void nfs4args_lockt_free(nfs_argop4 *); 186 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 187 int, rnode4_t *, cred_t *, bitmap4, int *, 188 nfs4_stateid_types_t *); 189 static void nfs4args_setattr_free(nfs_argop4 *); 190 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 191 bitmap4); 192 static void nfs4args_verify_free(nfs_argop4 *); 193 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 194 WRITE4args **, nfs4_stateid_types_t *); 195 196 /* 197 * These are the vnode ops functions that implement the vnode interface to 198 * the networked file system. See more comments below at nfs4_vnodeops. 199 */ 200 static int nfs4_open(vnode_t **, int, cred_t *); 201 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *); 202 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 203 caller_context_t *); 204 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 205 caller_context_t *); 206 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 207 static int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *); 208 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 209 caller_context_t *); 210 static int nfs4_access(vnode_t *, int, int, cred_t *); 211 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *); 212 static int nfs4_fsync(vnode_t *, int, cred_t *); 213 static void nfs4_inactive(vnode_t *, cred_t *); 214 static int nfs4_lookup(vnode_t *, char *, vnode_t **, 215 struct pathname *, int, vnode_t *, cred_t *); 216 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 217 int, vnode_t **, cred_t *, int); 218 static int nfs4_remove(vnode_t *, char *, cred_t *); 219 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *); 220 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 221 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, 222 vnode_t **, cred_t *); 223 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *); 224 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 225 cred_t *); 226 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *); 227 static int nfs4_fid(vnode_t *, fid_t *); 228 static int nfs4_rwlock(vnode_t *, int, caller_context_t *); 229 static void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 230 static int nfs4_seek(vnode_t *, offset_t, offset_t *); 231 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 232 page_t *[], size_t, struct seg *, caddr_t, 233 enum seg_rw, cred_t *); 234 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *); 235 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, 236 size_t, uchar_t, uchar_t, uint_t, cred_t *); 237 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, 238 size_t, uchar_t, uchar_t, uint_t, cred_t *); 239 static int nfs4_cmp(vnode_t *, vnode_t *); 240 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 241 struct flk_callback *, cred_t *); 242 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 243 cred_t *, caller_context_t *); 244 static int nfs4_realvp(vnode_t *, vnode_t **); 245 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, 246 size_t, uint_t, uint_t, uint_t, cred_t *); 247 static int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *); 248 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 249 cred_t *); 250 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *); 251 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 252 static int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 253 static int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *); 254 255 /* 256 * Used for nfs4_commit_vp() to indicate if we should 257 * wait on pending writes. 258 */ 259 #define NFS4_WRITE_NOWAIT 0 260 #define NFS4_WRITE_WAIT 1 261 262 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 263 264 /* 265 * Error flags used to pass information about certain special errors 266 * which need to be handled specially. 267 */ 268 #define NFS_EOF -98 269 #define NFS_VERF_MISMATCH -97 270 271 /* 272 * Flags used to differentiate between which operation drove the 273 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 274 */ 275 #define NFS4_CLOSE_OP 0x1 276 #define NFS4_DELMAP_OP 0x2 277 #define NFS4_INACTIVE_OP 0x3 278 279 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 280 281 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 282 #define ALIGN64(x, ptr, sz) \ 283 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 284 if (x) { \ 285 x = sizeof (uint64_t) - (x); \ 286 sz -= (x); \ 287 ptr += (x); \ 288 } 289 290 #ifdef DEBUG 291 int nfs4_client_attr_debug = 0; 292 int nfs4_client_state_debug = 0; 293 int nfs4_client_shadow_debug = 0; 294 int nfs4_client_lock_debug = 0; 295 int nfs4_seqid_sync = 0; 296 int nfs4_client_map_debug = 0; 297 static int nfs4_pageio_debug = 0; 298 int nfs4_client_inactive_debug = 0; 299 int nfs4_client_recov_debug = 0; 300 int nfs4_client_recov_stub_debug = 0; 301 int nfs4_client_failover_debug = 0; 302 int nfs4_client_call_debug = 0; 303 int nfs4_client_lookup_debug = 0; 304 int nfs4_client_zone_debug = 0; 305 int nfs4_lost_rqst_debug = 0; 306 int nfs4_rdattrerr_debug = 0; 307 int nfs4_open_stream_debug = 0; 308 309 int nfs4read_error_inject; 310 311 static int nfs4_create_misses = 0; 312 313 static int nfs4_readdir_cache_shorts = 0; 314 static int nfs4_readdir_readahead = 0; 315 316 static int nfs4_bio_do_stop = 0; 317 318 static int nfs4_lostpage = 0; /* number of times we lost original page */ 319 320 int nfs4_mmap_debug = 0; 321 322 static int nfs4_pathconf_cache_hits = 0; 323 static int nfs4_pathconf_cache_misses = 0; 324 325 int nfs4close_all_cnt; 326 int nfs4close_one_debug = 0; 327 int nfs4close_notw_debug = 0; 328 329 int denied_to_flk_debug = 0; 330 void *lockt_denied_debug; 331 332 #endif 333 334 /* 335 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 336 * or NFS4ERR_RESOURCE. 337 */ 338 static int confirm_retry_sec = 30; 339 340 static int nfs4_lookup_neg_cache = 1; 341 342 /* 343 * number of pages to read ahead 344 * optimized for 100 base-T. 345 */ 346 static int nfs4_nra = 4; 347 348 static int nfs4_do_symlink_cache = 1; 349 350 static int nfs4_pathconf_disable_cache = 0; 351 352 /* 353 * These are the vnode ops routines which implement the vnode interface to 354 * the networked file system. These routines just take their parameters, 355 * make them look networkish by putting the right info into interface structs, 356 * and then calling the appropriate remote routine(s) to do the work. 357 * 358 * Note on directory name lookup cacheing: If we detect a stale fhandle, 359 * we purge the directory cache relative to that vnode. This way, the 360 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 361 * more details on rnode locking. 362 */ 363 364 struct vnodeops *nfs4_vnodeops; 365 366 const fs_operation_def_t nfs4_vnodeops_template[] = { 367 VOPNAME_OPEN, nfs4_open, 368 VOPNAME_CLOSE, nfs4_close, 369 VOPNAME_READ, nfs4_read, 370 VOPNAME_WRITE, nfs4_write, 371 VOPNAME_IOCTL, nfs4_ioctl, 372 VOPNAME_GETATTR, nfs4_getattr, 373 VOPNAME_SETATTR, nfs4_setattr, 374 VOPNAME_ACCESS, nfs4_access, 375 VOPNAME_LOOKUP, nfs4_lookup, 376 VOPNAME_CREATE, nfs4_create, 377 VOPNAME_REMOVE, nfs4_remove, 378 VOPNAME_LINK, nfs4_link, 379 VOPNAME_RENAME, nfs4_rename, 380 VOPNAME_MKDIR, nfs4_mkdir, 381 VOPNAME_RMDIR, nfs4_rmdir, 382 VOPNAME_READDIR, nfs4_readdir, 383 VOPNAME_SYMLINK, nfs4_symlink, 384 VOPNAME_READLINK, nfs4_readlink, 385 VOPNAME_FSYNC, nfs4_fsync, 386 VOPNAME_INACTIVE, (fs_generic_func_p) nfs4_inactive, 387 VOPNAME_FID, nfs4_fid, 388 VOPNAME_RWLOCK, nfs4_rwlock, 389 VOPNAME_RWUNLOCK, (fs_generic_func_p) nfs4_rwunlock, 390 VOPNAME_SEEK, nfs4_seek, 391 VOPNAME_FRLOCK, nfs4_frlock, 392 VOPNAME_SPACE, nfs4_space, 393 VOPNAME_REALVP, nfs4_realvp, 394 VOPNAME_GETPAGE, nfs4_getpage, 395 VOPNAME_PUTPAGE, nfs4_putpage, 396 VOPNAME_MAP, (fs_generic_func_p) nfs4_map, 397 VOPNAME_ADDMAP, (fs_generic_func_p) nfs4_addmap, 398 VOPNAME_DELMAP, nfs4_delmap, 399 VOPNAME_DUMP, nfs_dump, /* there is no separate nfs4_dump */ 400 VOPNAME_PATHCONF, nfs4_pathconf, 401 VOPNAME_PAGEIO, nfs4_pageio, 402 VOPNAME_DISPOSE, (fs_generic_func_p) nfs4_dispose, 403 VOPNAME_SETSECATTR, nfs4_setsecattr, 404 VOPNAME_GETSECATTR, nfs4_getsecattr, 405 VOPNAME_SHRLOCK, nfs4_shrlock, 406 NULL, NULL 407 }; 408 409 /* 410 * The following are subroutines and definitions to set args or get res 411 * for the different nfsv4 ops 412 */ 413 414 void 415 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 416 { 417 int i; 418 419 for (i = 0; i < arglen; i++) { 420 if (argop[i].argop == OP_LOOKUP) 421 kmem_free( 422 argop[i].nfs_argop4_u.oplookup.objname.utf8string_val, 423 argop[i].nfs_argop4_u.oplookup.objname.utf8string_len); 424 } 425 } 426 427 static void 428 nfs4args_lock_free(nfs_argop4 *argop) 429 { 430 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 431 432 if (locker->new_lock_owner == TRUE) { 433 open_to_lock_owner4 *open_owner; 434 435 open_owner = &locker->locker4_u.open_owner; 436 if (open_owner->lock_owner.owner_val != NULL) { 437 kmem_free(open_owner->lock_owner.owner_val, 438 open_owner->lock_owner.owner_len); 439 } 440 } 441 } 442 443 static void 444 nfs4args_lockt_free(nfs_argop4 *argop) 445 { 446 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 447 448 if (lowner->owner_val != NULL) { 449 kmem_free(lowner->owner_val, lowner->owner_len); 450 } 451 } 452 453 static void 454 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 455 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 456 nfs4_stateid_types_t *sid_types) 457 { 458 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 459 mntinfo4_t *mi; 460 461 argop->argop = OP_SETATTR; 462 /* 463 * The stateid is set to 0 if client is not modifying the size 464 * and otherwise to whatever nfs4_get_stateid() returns. 465 * 466 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 467 * state struct could be found for the process/file pair. We may 468 * want to change this in the future (by OPENing the file). See 469 * bug # 4474852. 470 */ 471 if (vap->va_mask & AT_SIZE) { 472 473 ASSERT(rp != NULL); 474 mi = VTOMI4(RTOV4(rp)); 475 476 argop->nfs_argop4_u.opsetattr.stateid = 477 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 478 OP_SETATTR, sid_types, FALSE); 479 } else { 480 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 481 sizeof (stateid4)); 482 } 483 484 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 485 if (*error) 486 bzero(attr, sizeof (*attr)); 487 } 488 489 static void 490 nfs4args_setattr_free(nfs_argop4 *argop) 491 { 492 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 493 } 494 495 static int 496 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 497 bitmap4 supp) 498 { 499 fattr4 *attr; 500 int error = 0; 501 502 argop->argop = op; 503 switch (op) { 504 case OP_VERIFY: 505 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 506 break; 507 case OP_NVERIFY: 508 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 509 break; 510 default: 511 return (EINVAL); 512 /*NOTREACHED*/ 513 break; 514 } 515 if (!error) 516 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 517 if (error) 518 bzero(attr, sizeof (*attr)); 519 return (error); 520 } 521 522 static void 523 nfs4args_verify_free(nfs_argop4 *argop) 524 { 525 switch (argop->argop) { 526 case OP_VERIFY: 527 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 528 break; 529 case OP_NVERIFY: 530 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 531 break; 532 default: 533 break; 534 } 535 } 536 537 static void 538 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 539 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 540 { 541 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 542 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 543 544 argop->argop = OP_WRITE; 545 wargs->stable = stable; 546 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 547 mi, OP_WRITE, sid_tp); 548 wargs->mblk = NULL; 549 *wargs_pp = wargs; 550 } 551 552 void 553 nfs4args_copen_free(OPEN4cargs *open_args) 554 { 555 if (open_args->owner.owner_val) { 556 kmem_free(open_args->owner.owner_val, 557 open_args->owner.owner_len); 558 } 559 if ((open_args->opentype == OPEN4_CREATE) && 560 (open_args->mode != EXCLUSIVE4)) { 561 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 562 } 563 } 564 565 /* 566 * XXX: This is referenced in modstubs.s 567 */ 568 struct vnodeops * 569 nfs4_getvnodeops(void) 570 { 571 return (nfs4_vnodeops); 572 } 573 574 /* 575 * The OPEN operation opens a regular file. 576 * 577 * ARGSUSED 578 */ 579 static int 580 nfs4_open(vnode_t **vpp, int flag, cred_t *cr) 581 { 582 vnode_t *dvp = NULL; 583 rnode4_t *rp; 584 int error; 585 int just_been_created; 586 char fn[MAXNAMELEN]; 587 588 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 589 if (curproc->p_zone != VTOMI4(*vpp)->mi_zone) 590 return (EIO); 591 rp = VTOR4(*vpp); 592 593 /* 594 * Check to see if opening something besides a regular file; 595 * if so skip the OTW call 596 */ 597 if ((*vpp)->v_type != VREG) { 598 error = nfs4_open_non_reg_file(vpp, flag, cr); 599 return (error); 600 } 601 602 /* 603 * XXX - would like a check right here to know if the file is 604 * executable or not, so as to skip OTW 605 */ 606 607 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) 608 return (error); 609 610 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 611 return (error); 612 613 /* 614 * See if this file has just been CREATEd. 615 * If so, clear the flag and update the dnlc, which was previously 616 * skipped in nfs4_create. 617 * XXX need better serilization on this. 618 * XXX move this into the nf4open_otw call, after we have 619 * XXX acquired the open owner seqid sync. 620 */ 621 mutex_enter(&rp->r_statev4_lock); 622 if (rp->created_v4) { 623 rp->created_v4 = 0; 624 dnlc_update(dvp, fn, *vpp); 625 /* This is needed so we don't bump the open ref count */ 626 just_been_created = 1; 627 } else { 628 just_been_created = 0; 629 } 630 mutex_exit(&rp->r_statev4_lock); 631 632 /* 633 * If caller specified O_TRUNC/FTRUNC, then be sure to set 634 * FWRITE (to drive successful setattr(size=0) after open) 635 */ 636 if (flag & FTRUNC) 637 flag |= FWRITE; 638 639 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 640 just_been_created); 641 642 if (!error && !((*vpp)->v_flag & VROOT)) 643 dnlc_update(dvp, fn, *vpp); 644 645 /* release the hold from vtodv */ 646 VN_RELE(dvp); 647 648 /* exchange the shadow for the master vnode, if needed */ 649 650 if (error == 0 && IS_SHADOW(*vpp, rp)) 651 sv_exchange(vpp); 652 653 return (error); 654 } 655 656 /* 657 * See if there's a "lost open" request to be saved and recovered. 658 */ 659 static void 660 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 661 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 662 vnode_t *dvp, OPEN4cargs *open_args) 663 { 664 vfs_t *vfsp; 665 char *srccfp; 666 667 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 668 669 if (error != ETIMEDOUT && error != EINTR && 670 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 671 lost_rqstp->lr_op = 0; 672 return; 673 } 674 675 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 676 "nfs4open_save_lost_rqst: error %d", error)); 677 678 lost_rqstp->lr_op = OP_OPEN; 679 /* 680 * The vp (if it is not NULL) and dvp are held and rele'd via 681 * the recovery code. See nfs4_save_lost_rqst. 682 */ 683 lost_rqstp->lr_vp = vp; 684 lost_rqstp->lr_dvp = dvp; 685 lost_rqstp->lr_oop = oop; 686 lost_rqstp->lr_osp = NULL; 687 lost_rqstp->lr_lop = NULL; 688 lost_rqstp->lr_cr = cr; 689 lost_rqstp->lr_flk = NULL; 690 lost_rqstp->lr_oacc = open_args->share_access; 691 lost_rqstp->lr_odeny = open_args->share_deny; 692 lost_rqstp->lr_oclaim = open_args->claim; 693 if (open_args->claim == CLAIM_DELEGATE_CUR) { 694 lost_rqstp->lr_ostateid = 695 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 696 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 697 } else { 698 srccfp = open_args->open_claim4_u.cfile; 699 } 700 lost_rqstp->lr_ofile.utf8string_len = 0; 701 lost_rqstp->lr_ofile.utf8string_val = NULL; 702 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 703 lost_rqstp->lr_putfirst = FALSE; 704 } 705 706 struct nfs4_excl_time { 707 uint32 seconds; 708 uint32 nseconds; 709 }; 710 711 /* 712 * The OPEN operation creates and/or opens a regular file 713 * 714 * ARGSUSED 715 */ 716 static int 717 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 718 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 719 enum createmode4 createmode, int file_just_been_created) 720 { 721 rnode4_t *rp; 722 rnode4_t *drp = VTOR4(dvp); 723 vnode_t *vp = NULL; 724 vnode_t *vpi = *vpp; 725 bool_t needrecov = FALSE; 726 727 int doqueue = 1; 728 729 COMPOUND4args_clnt args; 730 COMPOUND4res_clnt res; 731 nfs_argop4 *argop; 732 nfs_resop4 *resop; 733 int argoplist_size; 734 int idx_open, idx_fattr; 735 736 GETFH4res *gf_res = NULL; 737 OPEN4res *op_res = NULL; 738 nfs4_ga_res_t *garp; 739 fattr4 *attr = NULL; 740 struct nfs4_excl_time verf; 741 bool_t did_excl_setup = FALSE; 742 int created_osp; 743 744 OPEN4cargs *open_args; 745 nfs4_open_owner_t *oop = NULL; 746 nfs4_open_stream_t *osp = NULL; 747 seqid4 seqid = 0; 748 bool_t retry_open = FALSE; 749 nfs4_recov_state_t recov_state; 750 nfs4_lost_rqst_t lost_rqst; 751 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 752 hrtime_t t; 753 int acc = 0; 754 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 755 cred_t *ncr = NULL; 756 757 nfs4_sharedfh_t *otw_sfh; 758 nfs4_sharedfh_t *orig_sfh; 759 int fh_differs = 0; 760 int numops, setgid_flag; 761 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 762 763 /* 764 * Make sure we properly deal with setting the right gid on 765 * a newly created file to reflect the parent's setgid bit 766 */ 767 setgid_flag = 0; 768 if (create_flag && in_va) { 769 770 /* 771 * If the parent's directory has the setgid bit set 772 * _and_ the client was able to get a valid mapping 773 * for the parent dir's owner_group, we want to 774 * append NVERIFY(owner_group == dva.va_gid) and 775 * SETATTR to the CREATE compound. 776 */ 777 mutex_enter(&drp->r_statelock); 778 if (drp->r_attr.va_mode & VSGID && 779 drp->r_attr.va_gid != GID_NOBODY) { 780 in_va->va_gid = drp->r_attr.va_gid; 781 setgid_flag = 1; 782 } 783 mutex_exit(&drp->r_statelock); 784 } 785 786 /* 787 * Normal/non-create compound: 788 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 789 * 790 * Open(create) compound no setgid: 791 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 792 * RESTOREFH + GETATTR 793 * 794 * Open(create) setgid: 795 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 796 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 797 * NVERIFY(grp) + SETATTR 798 */ 799 if (setgid_flag) { 800 numops = 10; 801 idx_open = 1; 802 idx_fattr = 3; 803 } else if (create_flag) { 804 numops = 7; 805 idx_open = 2; 806 idx_fattr = 4; 807 } else { 808 numops = 4; 809 idx_open = 1; 810 idx_fattr = 3; 811 } 812 813 args.array_len = numops; 814 argoplist_size = numops * sizeof (nfs_argop4); 815 argop = kmem_alloc(argoplist_size, KM_SLEEP); 816 817 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 818 "open %s open flag 0x%x cred %p", file_name, open_flag, 819 (void *)cr)); 820 821 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 822 if (create_flag) { 823 /* 824 * We are to create a file. Initialize the passed in vnode 825 * pointer. 826 */ 827 vpi = NULL; 828 } else { 829 /* 830 * Check to see if the client owns a read delegation and is 831 * trying to open for write. If so, then return the delegation 832 * to avoid the server doing a cb_recall and returning DELAY. 833 * NB - we don't use the statev4_lock here because we'd have 834 * to drop the lock anyway and the result would be stale. 835 */ 836 if ((open_flag & FWRITE) && 837 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 838 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 839 840 /* 841 * If the file has a delegation, then do an access check up 842 * front. This avoids having to an access check later after 843 * we've already done start_op, which could deadlock. 844 */ 845 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 846 if (open_flag & FREAD && 847 nfs4_access(vpi, VREAD, 0, cr) == 0) 848 acc |= VREAD; 849 if (open_flag & FWRITE && 850 nfs4_access(vpi, VWRITE, 0, cr) == 0) 851 acc |= VWRITE; 852 } 853 } 854 855 drp = VTOR4(dvp); 856 857 recov_state.rs_flags = 0; 858 recov_state.rs_num_retry_despite_err = 0; 859 cred_otw = cr; 860 861 recov_retry: 862 fh_differs = 0; 863 nfs4_error_zinit(&e); 864 865 /* argop is empty here */ 866 867 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 868 if (ncr != NULL) 869 crfree(ncr); 870 kmem_free(argop, argoplist_size); 871 return (EINTR); 872 } 873 874 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 875 if (e.error) { 876 nfs_rw_exit(&drp->r_rwlock); 877 if (ncr != NULL) 878 crfree(ncr); 879 kmem_free(argop, argoplist_size); 880 return (e.error); 881 } 882 883 args.ctag = TAG_OPEN; 884 args.array_len = numops; 885 args.array = argop; 886 887 /* putfh directory fh */ 888 argop[0].argop = OP_CPUTFH; 889 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 890 891 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 892 argop[idx_open].argop = OP_COPEN; 893 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 894 open_args->claim = CLAIM_NULL; 895 896 /* name of file */ 897 open_args->open_claim4_u.cfile = file_name; 898 open_args->owner.owner_len = 0; 899 open_args->owner.owner_val = NULL; 900 901 if (create_flag) { 902 /* CREATE a file */ 903 open_args->opentype = OPEN4_CREATE; 904 open_args->mode = createmode; 905 if (createmode == EXCLUSIVE4) { 906 if (did_excl_setup == FALSE) { 907 verf.seconds = nfs_atoi(hw_serial); 908 if (verf.seconds != 0) 909 verf.nseconds = newnum(); 910 else { 911 timestruc_t now; 912 913 gethrestime(&now); 914 verf.seconds = now.tv_sec; 915 verf.nseconds = now.tv_nsec; 916 } 917 /* 918 * Since the server will use this value for the 919 * mtime, make sure that it can't overflow. Zero 920 * out the MSB. The actual value does not matter 921 * here, only its uniqeness. 922 */ 923 verf.seconds &= INT32_MAX; 924 did_excl_setup = TRUE; 925 } 926 927 /* Now copy over verifier to OPEN4args. */ 928 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 929 } else { 930 int v_error; 931 bitmap4 supp_attrs; 932 servinfo4_t *svp; 933 934 attr = &open_args->createhow4_u.createattrs; 935 936 svp = drp->r_server; 937 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 938 supp_attrs = svp->sv_supp_attrs; 939 nfs_rw_exit(&svp->sv_lock); 940 941 /* GUARDED4 or UNCHECKED4 */ 942 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 943 supp_attrs); 944 if (v_error) { 945 bzero(attr, sizeof (*attr)); 946 nfs4args_copen_free(open_args); 947 nfs_rw_exit(&drp->r_rwlock); 948 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 949 &recov_state, FALSE); 950 if (ncr != NULL) 951 crfree(ncr); 952 kmem_free(argop, argoplist_size); 953 return (v_error); 954 } 955 } 956 } else { 957 /* NO CREATE */ 958 open_args->opentype = OPEN4_NOCREATE; 959 } 960 961 if (recov_state.rs_sp != NULL) { 962 mutex_enter(&recov_state.rs_sp->s_lock); 963 open_args->owner.clientid = recov_state.rs_sp->clientid; 964 mutex_exit(&recov_state.rs_sp->s_lock); 965 } else { 966 /* XXX should we just fail here? */ 967 open_args->owner.clientid = 0; 968 } 969 970 /* 971 * This increments oop's ref count or creates a temporary 'just_created' 972 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 973 * completes. 974 */ 975 mutex_enter(&VTOMI4(dvp)->mi_lock); 976 977 /* See if a permanent or just created open owner exists */ 978 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 979 if (!oop) { 980 /* 981 * This open owner does not exist so create a temporary 982 * just created one. 983 */ 984 oop = create_open_owner(cr, VTOMI4(dvp)); 985 ASSERT(oop != NULL); 986 } 987 mutex_exit(&VTOMI4(dvp)->mi_lock); 988 989 /* this length never changes, do alloc before seqid sync */ 990 open_args->owner.owner_len = sizeof (oop->oo_name); 991 open_args->owner.owner_val = 992 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 993 994 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 995 if (e.error == EAGAIN) { 996 open_owner_rele(oop); 997 nfs4args_copen_free(open_args); 998 nfs_rw_exit(&drp->r_rwlock); 999 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1000 if (ncr != NULL) { 1001 crfree(ncr); 1002 ncr = NULL; 1003 } 1004 goto recov_retry; 1005 } 1006 1007 /* Check to see if we need to do the OTW call */ 1008 if (!create_flag) { 1009 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1010 file_just_been_created, &e.error, acc, &recov_state)) { 1011 1012 /* 1013 * The OTW open is not necessary. Either 1014 * the open can succeed without it (eg. 1015 * delegation, error == 0) or the open 1016 * must fail due to an access failure 1017 * (error != 0). In either case, tidy 1018 * up and return. 1019 */ 1020 1021 nfs4_end_open_seqid_sync(oop); 1022 open_owner_rele(oop); 1023 nfs4args_copen_free(open_args); 1024 nfs_rw_exit(&drp->r_rwlock); 1025 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1026 if (ncr != NULL) 1027 crfree(ncr); 1028 kmem_free(argop, argoplist_size); 1029 return (e.error); 1030 } 1031 } 1032 1033 bcopy(&oop->oo_name, open_args->owner.owner_val, 1034 open_args->owner.owner_len); 1035 1036 seqid = nfs4_get_open_seqid(oop) + 1; 1037 open_args->seqid = seqid; 1038 open_args->share_access = 0; 1039 if (open_flag & FREAD) 1040 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1041 if (open_flag & FWRITE) 1042 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1043 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1044 1045 1046 1047 /* 1048 * getfh w/sanity check for idx_open/idx_fattr 1049 */ 1050 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1051 argop[idx_open + 1].argop = OP_GETFH; 1052 1053 /* getattr */ 1054 argop[idx_fattr].argop = OP_GETATTR; 1055 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1056 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1057 1058 if (setgid_flag) { 1059 vattr_t _v; 1060 servinfo4_t *svp; 1061 bitmap4 supp_attrs; 1062 1063 svp = drp->r_server; 1064 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1065 supp_attrs = svp->sv_supp_attrs; 1066 nfs_rw_exit(&svp->sv_lock); 1067 1068 /* 1069 * For setgid case, we need to: 1070 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1071 */ 1072 argop[4].argop = OP_SAVEFH; 1073 1074 argop[5].argop = OP_CPUTFH; 1075 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1076 1077 argop[6].argop = OP_GETATTR; 1078 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1079 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1080 1081 argop[7].argop = OP_RESTOREFH; 1082 1083 /* 1084 * nverify 1085 */ 1086 _v.va_mask = AT_GID; 1087 _v.va_gid = in_va->va_gid; 1088 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1089 supp_attrs))) { 1090 1091 /* 1092 * setattr 1093 * 1094 * We _know_ we're not messing with AT_SIZE or 1095 * AT_XTIME, so no need for stateid or flags. 1096 * Also we specify NULL rp since we're only 1097 * interested in setting owner_group attributes. 1098 */ 1099 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1100 supp_attrs, &e.error, 0); 1101 if (e.error) 1102 nfs4args_verify_free(&argop[8]); 1103 } 1104 1105 if (e.error) { 1106 /* 1107 * XXX - Revisit the last argument to nfs4_end_op() 1108 * once 5020486 is fixed. 1109 */ 1110 nfs4_end_open_seqid_sync(oop); 1111 open_owner_rele(oop); 1112 nfs4args_copen_free(open_args); 1113 nfs_rw_exit(&drp->r_rwlock); 1114 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1115 if (ncr != NULL) 1116 crfree(ncr); 1117 kmem_free(argop, argoplist_size); 1118 return (e.error); 1119 } 1120 } else if (create_flag) { 1121 /* 1122 * For setgid case, we need to: 1123 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1124 */ 1125 argop[1].argop = OP_SAVEFH; 1126 1127 argop[5].argop = OP_RESTOREFH; 1128 1129 argop[6].argop = OP_GETATTR; 1130 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1131 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1132 } 1133 1134 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1135 "nfs4open_otw: %s call, nm %s, rp %s", 1136 needrecov ? "recov" : "first", file_name, 1137 rnode4info(VTOR4(dvp)))); 1138 1139 t = gethrtime(); 1140 1141 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1142 1143 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1144 nfs4_set_open_seqid(seqid, oop, args.ctag); 1145 1146 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1147 1148 if (e.error || needrecov) { 1149 bool_t abort = FALSE; 1150 1151 if (needrecov) { 1152 nfs4_bseqid_entry_t *bsep = NULL; 1153 1154 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1155 cred_otw, vpi, dvp, open_args); 1156 1157 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1158 bsep = nfs4_create_bseqid_entry(oop, NULL, 1159 vpi, 0, args.ctag, open_args->seqid); 1160 num_bseqid_retry--; 1161 } 1162 1163 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1164 NULL, lost_rqst.lr_op == OP_OPEN ? 1165 &lost_rqst : NULL, OP_OPEN, bsep); 1166 1167 if (bsep) 1168 kmem_free(bsep, sizeof (*bsep)); 1169 /* give up if we keep getting BAD_SEQID */ 1170 if (num_bseqid_retry == 0) 1171 abort = TRUE; 1172 if (abort == TRUE && e.error == 0) 1173 e.error = geterrno4(res.status); 1174 } 1175 nfs4_end_open_seqid_sync(oop); 1176 open_owner_rele(oop); 1177 nfs_rw_exit(&drp->r_rwlock); 1178 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1179 nfs4args_copen_free(open_args); 1180 if (setgid_flag) { 1181 nfs4args_verify_free(&argop[8]); 1182 nfs4args_setattr_free(&argop[9]); 1183 } 1184 if (!e.error) 1185 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1186 if (ncr != NULL) { 1187 crfree(ncr); 1188 ncr = NULL; 1189 } 1190 if (!needrecov || abort == TRUE || e.error == EINTR || 1191 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1192 kmem_free(argop, argoplist_size); 1193 return (e.error); 1194 } 1195 goto recov_retry; 1196 } 1197 1198 /* 1199 * Will check and update lease after checking the rflag for 1200 * OPEN_CONFIRM in the successful OPEN call. 1201 */ 1202 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1203 1204 /* 1205 * XXX what if we're crossing mount points from server1:/drp 1206 * to server2:/drp/rp. 1207 */ 1208 1209 /* Signal our end of use of the open seqid */ 1210 nfs4_end_open_seqid_sync(oop); 1211 1212 /* 1213 * This will destroy the open owner if it was just created, 1214 * and no one else has put a reference on it. 1215 */ 1216 open_owner_rele(oop); 1217 if (create_flag && (createmode != EXCLUSIVE4) && 1218 res.status == NFS4ERR_BADOWNER) 1219 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1220 1221 e.error = geterrno4(res.status); 1222 nfs4args_copen_free(open_args); 1223 if (setgid_flag) { 1224 nfs4args_verify_free(&argop[8]); 1225 nfs4args_setattr_free(&argop[9]); 1226 } 1227 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1228 nfs_rw_exit(&drp->r_rwlock); 1229 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1230 /* 1231 * If the reply is NFS4ERR_ACCESS, it may be because 1232 * we are root (no root net access). If the real uid 1233 * is not root, then retry with the real uid instead. 1234 */ 1235 if (ncr != NULL) { 1236 crfree(ncr); 1237 ncr = NULL; 1238 } 1239 if (res.status == NFS4ERR_ACCESS && 1240 (ncr = crnetadjust(cred_otw)) != NULL) { 1241 cred_otw = ncr; 1242 goto recov_retry; 1243 } 1244 kmem_free(argop, argoplist_size); 1245 return (e.error); 1246 } 1247 1248 resop = &res.array[idx_open]; /* open res */ 1249 op_res = &resop->nfs_resop4_u.opopen; 1250 1251 #ifdef DEBUG 1252 /* 1253 * verify attrset bitmap 1254 */ 1255 if (create_flag && 1256 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1257 /* make sure attrset returned is what we asked for */ 1258 /* XXX Ignore this 'error' for now */ 1259 if (attr->attrmask != op_res->attrset) 1260 /* EMPTY */; 1261 } 1262 #endif 1263 1264 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1265 mutex_enter(&VTOMI4(dvp)->mi_lock); 1266 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1267 mutex_exit(&VTOMI4(dvp)->mi_lock); 1268 } 1269 1270 resop = &res.array[idx_open + 1]; /* getfh res */ 1271 gf_res = &resop->nfs_resop4_u.opgetfh; 1272 1273 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1274 1275 /* 1276 * The open stateid has been updated on the server but not 1277 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1278 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1279 * WRITE call. That, however, will use the old stateid, so go ahead 1280 * and upate the open stateid now, before any call to makenfs4node. 1281 */ 1282 if (vpi) { 1283 nfs4_open_stream_t *tmp_osp; 1284 rnode4_t *tmp_rp = VTOR4(vpi); 1285 1286 tmp_osp = find_open_stream(oop, tmp_rp); 1287 if (tmp_osp) { 1288 tmp_osp->open_stateid = op_res->stateid; 1289 mutex_exit(&tmp_osp->os_sync_lock); 1290 open_stream_rele(tmp_osp, tmp_rp); 1291 } 1292 1293 /* 1294 * We must determine if the file handle given by the otw open 1295 * is the same as the file handle which was passed in with 1296 * *vpp. This case can be reached if the file we are trying 1297 * to open has been removed and another file has been created 1298 * having the same file name. The passed in vnode is released 1299 * later. 1300 */ 1301 orig_sfh = VTOR4(vpi)->r_fh; 1302 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1303 } 1304 1305 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1306 1307 if (create_flag || fh_differs) { 1308 int rnode_err = 0; 1309 1310 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1311 dvp, fn_get(VTOSV(dvp)->sv_name, file_name)); 1312 1313 if (e.error) 1314 PURGE_ATTRCACHE4(vp); 1315 /* 1316 * For the newly created vp case, make sure the rnode 1317 * isn't bad before using it. 1318 */ 1319 mutex_enter(&(VTOR4(vp))->r_statelock); 1320 if (VTOR4(vp)->r_flags & R4RECOVERR) 1321 rnode_err = EIO; 1322 mutex_exit(&(VTOR4(vp))->r_statelock); 1323 1324 if (rnode_err) { 1325 nfs4_end_open_seqid_sync(oop); 1326 nfs4args_copen_free(open_args); 1327 if (setgid_flag) { 1328 nfs4args_verify_free(&argop[8]); 1329 nfs4args_setattr_free(&argop[9]); 1330 } 1331 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1332 nfs_rw_exit(&drp->r_rwlock); 1333 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1334 needrecov); 1335 open_owner_rele(oop); 1336 VN_RELE(vp); 1337 if (ncr != NULL) 1338 crfree(ncr); 1339 sfh4_rele(&otw_sfh); 1340 kmem_free(argop, argoplist_size); 1341 return (EIO); 1342 } 1343 } else { 1344 vp = vpi; 1345 } 1346 sfh4_rele(&otw_sfh); 1347 1348 /* 1349 * It seems odd to get a full set of attrs and then not update 1350 * the object's attrcache in the non-create case. Create case uses 1351 * the attrs since makenfs4node checks to see if the attrs need to 1352 * be updated (and then updates them). The non-create case should 1353 * update attrs also. 1354 */ 1355 if (! create_flag && ! fh_differs && !e.error) { 1356 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1357 } 1358 1359 nfs4_error_zinit(&e); 1360 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1361 /* This does not do recovery for vp explicitly. */ 1362 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1363 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1364 1365 if (e.error || e.stat) { 1366 nfs4_end_open_seqid_sync(oop); 1367 nfs4args_copen_free(open_args); 1368 if (setgid_flag) { 1369 nfs4args_verify_free(&argop[8]); 1370 nfs4args_setattr_free(&argop[9]); 1371 } 1372 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1373 nfs_rw_exit(&drp->r_rwlock); 1374 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1375 needrecov); 1376 open_owner_rele(oop); 1377 if (create_flag || fh_differs) { 1378 /* rele the makenfs4node */ 1379 VN_RELE(vp); 1380 } 1381 if (ncr != NULL) { 1382 crfree(ncr); 1383 ncr = NULL; 1384 } 1385 if (retry_open == TRUE) { 1386 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1387 "nfs4open_otw: retry the open since OPEN " 1388 "CONFIRM failed with error %d stat %d", 1389 e.error, e.stat)); 1390 if (create_flag && createmode == GUARDED4) { 1391 NFS4_DEBUG(nfs4_client_recov_debug, 1392 (CE_NOTE, "nfs4open_otw: switch " 1393 "createmode from GUARDED4 to " 1394 "UNCHECKED4")); 1395 createmode = UNCHECKED4; 1396 } 1397 goto recov_retry; 1398 } 1399 if (!e.error) { 1400 if (create_flag && (createmode != EXCLUSIVE4) && 1401 e.stat == NFS4ERR_BADOWNER) 1402 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1403 1404 e.error = geterrno4(e.stat); 1405 } 1406 kmem_free(argop, argoplist_size); 1407 return (e.error); 1408 } 1409 } 1410 1411 rp = VTOR4(vp); 1412 1413 mutex_enter(&rp->r_statev4_lock); 1414 if (create_flag) 1415 rp->created_v4 = 1; 1416 mutex_exit(&rp->r_statev4_lock); 1417 1418 mutex_enter(&oop->oo_lock); 1419 /* Doesn't matter if 'oo_just_created' already was set as this */ 1420 oop->oo_just_created = NFS4_PERM_CREATED; 1421 if (oop->oo_cred_otw) 1422 crfree(oop->oo_cred_otw); 1423 oop->oo_cred_otw = cred_otw; 1424 crhold(oop->oo_cred_otw); 1425 mutex_exit(&oop->oo_lock); 1426 1427 /* returns with 'os_sync_lock' held */ 1428 osp = find_or_create_open_stream(oop, rp, &created_osp); 1429 if (!osp) { 1430 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1431 "nfs4open_otw: failed to create an open stream")); 1432 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1433 "signal our end of use of the open seqid")); 1434 1435 nfs4_end_open_seqid_sync(oop); 1436 open_owner_rele(oop); 1437 nfs4args_copen_free(open_args); 1438 if (setgid_flag) { 1439 nfs4args_verify_free(&argop[8]); 1440 nfs4args_setattr_free(&argop[9]); 1441 } 1442 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1443 nfs_rw_exit(&drp->r_rwlock); 1444 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1445 if (create_flag || fh_differs) 1446 VN_RELE(vp); 1447 if (ncr != NULL) 1448 crfree(ncr); 1449 1450 kmem_free(argop, argoplist_size); 1451 return (EINVAL); 1452 1453 } 1454 1455 osp->open_stateid = op_res->stateid; 1456 1457 if (open_flag & FREAD) 1458 osp->os_share_acc_read++; 1459 if (open_flag & FWRITE) 1460 osp->os_share_acc_write++; 1461 osp->os_share_deny_none++; 1462 1463 /* 1464 * Need to reset this bitfield for the possible case where we were 1465 * going to OTW CLOSE the file, got a non-recoverable error, and before 1466 * we could retry the CLOSE, OPENed the file again. 1467 */ 1468 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1469 osp->os_final_close = 0; 1470 osp->os_force_close = 0; 1471 #ifdef DEBUG 1472 if (osp->os_failed_reopen) 1473 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1474 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1475 (void *)osp, (void *)cr, rnode4info(rp))); 1476 #endif 1477 osp->os_failed_reopen = 0; 1478 1479 mutex_exit(&osp->os_sync_lock); 1480 1481 nfs4_end_open_seqid_sync(oop); 1482 1483 if (created_osp && recov_state.rs_sp != NULL) { 1484 mutex_enter(&recov_state.rs_sp->s_lock); 1485 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1486 mutex_exit(&recov_state.rs_sp->s_lock); 1487 } 1488 1489 /* get rid of our reference to find oop */ 1490 open_owner_rele(oop); 1491 1492 open_stream_rele(osp, rp); 1493 1494 /* accept delegation, if any */ 1495 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1496 1497 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1498 1499 if (createmode == EXCLUSIVE4 && 1500 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1501 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1502 " EXCLUSIVE4: sending a SETATTR")); 1503 /* 1504 * If doing an exclusive create, then generate 1505 * a SETATTR to set the initial attributes. 1506 * Try to set the mtime and the atime to the 1507 * server's current time. It is somewhat 1508 * expected that these fields will be used to 1509 * store the exclusive create cookie. If not, 1510 * server implementors will need to know that 1511 * a SETATTR will follow an exclusive create 1512 * and the cookie should be destroyed if 1513 * appropriate. 1514 * 1515 * The AT_GID and AT_SIZE bits are turned off 1516 * so that the SETATTR request will not attempt 1517 * to process these. The gid will be set 1518 * separately if appropriate. The size is turned 1519 * off because it is assumed that a new file will 1520 * be created empty and if the file wasn't empty, 1521 * then the exclusive create will have failed 1522 * because the file must have existed already. 1523 * Therefore, no truncate operation is needed. 1524 */ 1525 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1526 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1527 1528 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1529 if (e.error) { 1530 /* 1531 * Couldn't correct the attributes of 1532 * the newly created file and the 1533 * attributes are wrong. Remove the 1534 * file and return an error to the 1535 * application. 1536 */ 1537 /* XXX will this take care of client state ? */ 1538 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1539 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1540 " remove file", e.error)); 1541 VN_RELE(vp); 1542 (void) nfs4_remove(dvp, file_name, cr); 1543 nfs_rw_exit(&drp->r_rwlock); 1544 goto skip_rwlock_exit; 1545 } 1546 } 1547 1548 /* 1549 * If we created or found the correct vnode, due to create_flag or 1550 * fh_differs being set, then update directory cache attribute, readdir 1551 * and dnlc caches. 1552 */ 1553 if (create_flag || fh_differs) { 1554 dirattr_info_t dinfo, *dinfop; 1555 1556 /* 1557 * Make sure getattr succeeded before using results. 1558 * note: op 7 is getattr(dir) for both flavors of 1559 * open(create). 1560 */ 1561 if (create_flag && res.status == NFS4_OK) { 1562 dinfo.di_time_call = t; 1563 dinfo.di_cred = cr; 1564 dinfo.di_garp = 1565 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1566 dinfop = &dinfo; 1567 } else { 1568 dinfop = NULL; 1569 } 1570 1571 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1572 dinfop); 1573 } 1574 nfs_rw_exit(&drp->r_rwlock); 1575 skip_rwlock_exit: 1576 1577 /* 1578 * If the page cache for this file was flushed from actions 1579 * above, it was done asynchronously and if that is true, 1580 * there is a need to wait here for it to complete. This must 1581 * be done outside of start_fop/end_fop. 1582 */ 1583 (void) nfs4_waitfor_purge_complete(vp); 1584 1585 /* 1586 * It is implicit that we are in the open case (create_flag == 0) since 1587 * fh_differs can only be set to a non-zero value in the open case. 1588 */ 1589 if (fh_differs != 0 && vpi != NULL) 1590 VN_RELE(vpi); 1591 1592 /* 1593 * Be sure to set *vpp to the correct value before returning. 1594 */ 1595 *vpp = vp; 1596 1597 nfs4args_copen_free(open_args); 1598 if (setgid_flag) { 1599 nfs4args_verify_free(&argop[8]); 1600 nfs4args_setattr_free(&argop[9]); 1601 } 1602 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1603 1604 if (ncr) 1605 crfree(ncr); 1606 kmem_free(argop, argoplist_size); 1607 return (e.error); 1608 } 1609 1610 /* 1611 * Reopen an open instance. cf. nfs4open_otw(). 1612 * 1613 * Errors are returned by the nfs4_error_t parameter. 1614 * - ep->error contains an errno value or zero. 1615 * - if it is zero, ep->stat is set to an NFS status code, if any. 1616 * If the file could not be reopened, but the caller should continue, the 1617 * file is marked dead and no error values are returned. If the caller 1618 * should stop recovering open files and start over, either the ep->error 1619 * value or ep->stat will indicate an error (either something that requires 1620 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1621 * filehandles) may be handled silently by this routine. 1622 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1623 * will be started, so the caller should not do it. 1624 * 1625 * Gotos: 1626 * - kill_file : reopen failed in such a fashion to constitute marking the 1627 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1628 * is for cases where recovery is not possible. 1629 * - failed_reopen : same as above, except that the file has already been 1630 * marked dead, so no need to do it again. 1631 * - bailout : reopen failed but we are able to recover and retry the reopen - 1632 * either within this function immediatley or via the calling function. 1633 */ 1634 1635 void 1636 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1637 open_claim_type4 claim, bool_t frc_use_claim_previous, 1638 bool_t is_recov) 1639 { 1640 COMPOUND4args_clnt args; 1641 COMPOUND4res_clnt res; 1642 nfs_argop4 argop[4]; 1643 nfs_resop4 *resop; 1644 OPEN4res *op_res = NULL; 1645 OPEN4cargs *open_args; 1646 GETFH4res *gf_res; 1647 rnode4_t *rp = VTOR4(vp); 1648 int doqueue = 1; 1649 cred_t *cr = NULL, *cred_otw = NULL; 1650 nfs4_open_owner_t *oop = NULL; 1651 seqid4 seqid; 1652 nfs4_ga_res_t *garp; 1653 char fn[MAXNAMELEN]; 1654 nfs4_recov_state_t recov = {NULL, 0}; 1655 nfs4_lost_rqst_t lost_rqst; 1656 mntinfo4_t *mi = VTOMI4(vp); 1657 bool_t abort; 1658 char *failed_msg = ""; 1659 int fh_different; 1660 hrtime_t t; 1661 nfs4_bseqid_entry_t *bsep = NULL; 1662 1663 ASSERT(nfs4_consistent_type(vp)); 1664 ASSERT(curproc->p_zone == mi->mi_zone); 1665 1666 nfs4_error_zinit(ep); 1667 1668 /* this is the cred used to find the open owner */ 1669 cr = state_to_cred(osp); 1670 if (cr == NULL) { 1671 failed_msg = "Couldn't reopen: no cred"; 1672 goto kill_file; 1673 } 1674 /* use this cred for OTW operations */ 1675 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1676 1677 top: 1678 nfs4_error_zinit(ep); 1679 1680 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1681 /* File system has been unmounted, quit */ 1682 ep->error = EIO; 1683 failed_msg = "Couldn't reopen: file system has been unmounted"; 1684 goto kill_file; 1685 } 1686 1687 oop = osp->os_open_owner; 1688 1689 ASSERT(oop != NULL); 1690 if (oop == NULL) { /* be defensive in non-DEBUG */ 1691 failed_msg = "can't reopen: no open owner"; 1692 goto kill_file; 1693 } 1694 open_owner_hold(oop); 1695 1696 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1697 if (ep->error) { 1698 open_owner_rele(oop); 1699 oop = NULL; 1700 goto bailout; 1701 } 1702 1703 /* 1704 * If the rnode has a delegation and the delegation has been 1705 * recovered and the server didn't request a recall and the caller 1706 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1707 * recovery) and the rnode hasn't been marked dead, then install 1708 * the delegation stateid in the open stream. Otherwise, proceed 1709 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1710 */ 1711 mutex_enter(&rp->r_statev4_lock); 1712 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1713 !rp->r_deleg_return_pending && 1714 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1715 !rp->r_deleg_needs_recall && 1716 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1717 !(rp->r_flags & R4RECOVERR)) { 1718 mutex_enter(&osp->os_sync_lock); 1719 osp->os_delegation = 1; 1720 osp->open_stateid = rp->r_deleg_stateid; 1721 mutex_exit(&osp->os_sync_lock); 1722 mutex_exit(&rp->r_statev4_lock); 1723 goto bailout; 1724 } 1725 mutex_exit(&rp->r_statev4_lock); 1726 1727 /* 1728 * If the file failed recovery, just quit. This failure need not 1729 * affect other reopens, so don't return an error. 1730 */ 1731 mutex_enter(&rp->r_statelock); 1732 if (rp->r_flags & R4RECOVERR) { 1733 mutex_exit(&rp->r_statelock); 1734 ep->error = 0; 1735 goto failed_reopen; 1736 } 1737 mutex_exit(&rp->r_statelock); 1738 1739 /* 1740 * argop is empty here 1741 * 1742 * PUTFH, OPEN, GETATTR 1743 */ 1744 args.ctag = TAG_REOPEN; 1745 args.array_len = 4; 1746 args.array = argop; 1747 1748 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1749 "nfs4_reopen: file is type %d, id %s", 1750 vp->v_type, rnode4info(VTOR4(vp)))); 1751 1752 argop[0].argop = OP_CPUTFH; 1753 1754 if (claim != CLAIM_PREVIOUS) { 1755 /* 1756 * if this is a file mount then 1757 * use the mntinfo parentfh 1758 */ 1759 argop[0].nfs_argop4_u.opcputfh.sfh = 1760 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1761 VTOSV(vp)->sv_dfh; 1762 } else { 1763 /* putfh fh to reopen */ 1764 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1765 } 1766 1767 argop[1].argop = OP_COPEN; 1768 open_args = &argop[1].nfs_argop4_u.opcopen; 1769 open_args->claim = claim; 1770 1771 if (claim == CLAIM_NULL) { 1772 1773 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1774 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1775 "failed for vp 0x%p for CLAIM_NULL with %m", 1776 (void *)vp); 1777 failed_msg = "Couldn't reopen: vtoname failed for " 1778 "CLAIM_NULL"; 1779 /* nothing allocated yet */ 1780 goto kill_file; 1781 } 1782 1783 open_args->open_claim4_u.cfile = fn; 1784 } else if (claim == CLAIM_PREVIOUS) { 1785 1786 /* 1787 * We have two cases to deal with here: 1788 * 1) We're being called to reopen files in order to satisfy 1789 * a lock operation request which requires us to explicitly 1790 * reopen files which were opened under a delegation. If 1791 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1792 * that case, frc_use_claim_previous is TRUE and we must 1793 * use the rnode's current delegation type (r_deleg_type). 1794 * 2) We're reopening files during some form of recovery. 1795 * In this case, frc_use_claim_previous is FALSE and we 1796 * use the delegation type appropriate for recovery 1797 * (r_deleg_needs_recovery). 1798 */ 1799 mutex_enter(&rp->r_statev4_lock); 1800 open_args->open_claim4_u.delegate_type = 1801 frc_use_claim_previous ? 1802 rp->r_deleg_type : 1803 rp->r_deleg_needs_recovery; 1804 mutex_exit(&rp->r_statev4_lock); 1805 1806 } else if (claim == CLAIM_DELEGATE_CUR) { 1807 1808 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1809 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1810 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1811 "with %m", (void *)vp); 1812 failed_msg = "Couldn't reopen: vtoname failed for " 1813 "CLAIM_DELEGATE_CUR"; 1814 /* nothing allocated yet */ 1815 goto kill_file; 1816 } 1817 1818 mutex_enter(&rp->r_statev4_lock); 1819 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1820 rp->r_deleg_stateid; 1821 mutex_exit(&rp->r_statev4_lock); 1822 1823 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1824 } 1825 open_args->opentype = OPEN4_NOCREATE; 1826 open_args->owner.clientid = mi2clientid(mi); 1827 open_args->owner.owner_len = sizeof (oop->oo_name); 1828 open_args->owner.owner_val = 1829 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1830 bcopy(&oop->oo_name, open_args->owner.owner_val, 1831 open_args->owner.owner_len); 1832 open_args->share_access = 0; 1833 open_args->share_deny = 0; 1834 1835 mutex_enter(&osp->os_sync_lock); 1836 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1837 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1838 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1839 (void *)osp, (void *)rp, osp->os_share_acc_read, 1840 osp->os_share_acc_write, osp->os_open_ref_count, 1841 osp->os_mmap_read, osp->os_mmap_write, claim)); 1842 1843 if (osp->os_share_acc_read || osp->os_mmap_read) 1844 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1845 if (osp->os_share_acc_write || osp->os_mmap_write) 1846 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1847 if (osp->os_share_deny_read) 1848 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1849 if (osp->os_share_deny_write) 1850 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1851 mutex_exit(&osp->os_sync_lock); 1852 1853 seqid = nfs4_get_open_seqid(oop) + 1; 1854 open_args->seqid = seqid; 1855 1856 /* Construct the getfh part of the compound */ 1857 argop[2].argop = OP_GETFH; 1858 1859 /* Construct the getattr part of the compound */ 1860 argop[3].argop = OP_GETATTR; 1861 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1862 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1863 1864 t = gethrtime(); 1865 1866 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1867 1868 if (ep->error) { 1869 if (!is_recov && !frc_use_claim_previous && 1870 (ep->error == EINTR || ep->error == ETIMEDOUT || 1871 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1872 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1873 cred_otw, vp, NULL, open_args); 1874 abort = nfs4_start_recovery(ep, 1875 VTOMI4(vp), vp, NULL, NULL, 1876 lost_rqst.lr_op == OP_OPEN ? 1877 &lost_rqst : NULL, OP_OPEN, NULL); 1878 nfs4args_copen_free(open_args); 1879 goto bailout; 1880 } 1881 1882 nfs4args_copen_free(open_args); 1883 1884 if (ep->error == EACCES && cred_otw != cr) { 1885 crfree(cred_otw); 1886 cred_otw = cr; 1887 crhold(cred_otw); 1888 nfs4_end_open_seqid_sync(oop); 1889 open_owner_rele(oop); 1890 oop = NULL; 1891 goto top; 1892 } 1893 if (ep->error == ETIMEDOUT) 1894 goto bailout; 1895 failed_msg = "Couldn't reopen: rpc error"; 1896 goto kill_file; 1897 } 1898 1899 if (nfs4_need_to_bump_seqid(&res)) 1900 nfs4_set_open_seqid(seqid, oop, args.ctag); 1901 1902 switch (res.status) { 1903 case NFS4_OK: 1904 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1905 mutex_enter(&rp->r_statelock); 1906 rp->r_delay_interval = 0; 1907 mutex_exit(&rp->r_statelock); 1908 } 1909 break; 1910 case NFS4ERR_BAD_SEQID: 1911 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1912 args.ctag, open_args->seqid); 1913 1914 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1915 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1916 NULL, OP_OPEN, bsep); 1917 1918 nfs4args_copen_free(open_args); 1919 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1920 nfs4_end_open_seqid_sync(oop); 1921 open_owner_rele(oop); 1922 oop = NULL; 1923 kmem_free(bsep, sizeof (*bsep)); 1924 1925 goto kill_file; 1926 case NFS4ERR_NO_GRACE: 1927 nfs4args_copen_free(open_args); 1928 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1929 nfs4_end_open_seqid_sync(oop); 1930 open_owner_rele(oop); 1931 oop = NULL; 1932 if (claim == CLAIM_PREVIOUS) { 1933 /* 1934 * Retry as a plain open. We don't need to worry about 1935 * checking the changeinfo: it is acceptable for a 1936 * client to re-open a file and continue processing 1937 * (in the absence of locks). 1938 */ 1939 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1940 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1941 "will retry as CLAIM_NULL")); 1942 claim = CLAIM_NULL; 1943 nfs4_mi_kstat_inc_no_grace(mi); 1944 goto top; 1945 } 1946 failed_msg = 1947 "Couldn't reopen: tried reclaim outside grace period. "; 1948 goto kill_file; 1949 case NFS4ERR_GRACE: 1950 nfs4_set_grace_wait(mi); 1951 nfs4args_copen_free(open_args); 1952 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1953 nfs4_end_open_seqid_sync(oop); 1954 open_owner_rele(oop); 1955 oop = NULL; 1956 ep->error = nfs4_wait_for_grace(mi, &recov); 1957 if (ep->error != 0) 1958 goto bailout; 1959 goto top; 1960 case NFS4ERR_DELAY: 1961 nfs4_set_delay_wait(vp); 1962 nfs4args_copen_free(open_args); 1963 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1964 nfs4_end_open_seqid_sync(oop); 1965 open_owner_rele(oop); 1966 oop = NULL; 1967 ep->error = nfs4_wait_for_delay(vp, &recov); 1968 nfs4_mi_kstat_inc_delay(mi); 1969 if (ep->error != 0) 1970 goto bailout; 1971 goto top; 1972 case NFS4ERR_FHEXPIRED: 1973 /* recover filehandle and retry */ 1974 abort = nfs4_start_recovery(ep, 1975 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL); 1976 nfs4args_copen_free(open_args); 1977 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1978 nfs4_end_open_seqid_sync(oop); 1979 open_owner_rele(oop); 1980 oop = NULL; 1981 if (abort == FALSE) 1982 goto top; 1983 failed_msg = "Couldn't reopen: recovery aborted"; 1984 goto kill_file; 1985 case NFS4ERR_RESOURCE: 1986 case NFS4ERR_STALE_CLIENTID: 1987 case NFS4ERR_WRONGSEC: 1988 case NFS4ERR_EXPIRED: 1989 /* 1990 * Do not mark the file dead and let the calling 1991 * function initiate recovery. 1992 */ 1993 nfs4args_copen_free(open_args); 1994 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1995 nfs4_end_open_seqid_sync(oop); 1996 open_owner_rele(oop); 1997 oop = NULL; 1998 goto bailout; 1999 case NFS4ERR_ACCESS: 2000 if (cred_otw != cr) { 2001 crfree(cred_otw); 2002 cred_otw = cr; 2003 crhold(cred_otw); 2004 nfs4args_copen_free(open_args); 2005 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2006 nfs4_end_open_seqid_sync(oop); 2007 open_owner_rele(oop); 2008 oop = NULL; 2009 goto top; 2010 } 2011 /* fall through */ 2012 default: 2013 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2014 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2015 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2016 rnode4info(VTOR4(vp)))); 2017 failed_msg = "Couldn't reopen: NFSv4 error"; 2018 nfs4args_copen_free(open_args); 2019 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2020 goto kill_file; 2021 } 2022 2023 resop = &res.array[1]; /* open res */ 2024 op_res = &resop->nfs_resop4_u.opopen; 2025 2026 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2027 2028 /* 2029 * Check if the path we reopened really is the same 2030 * file. We could end up in a situation where the file 2031 * was removed and a new file created with the same name. 2032 */ 2033 resop = &res.array[2]; 2034 gf_res = &resop->nfs_resop4_u.opgetfh; 2035 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2036 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2037 if (fh_different) { 2038 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2039 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2040 /* Oops, we don't have the same file */ 2041 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2042 failed_msg = "Couldn't reopen: Persistent " 2043 "file handle changed"; 2044 else 2045 failed_msg = "Couldn't reopen: Volatile " 2046 "(no expire on open) file handle changed"; 2047 2048 nfs4args_copen_free(open_args); 2049 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2050 nfs_rw_exit(&mi->mi_fh_lock); 2051 goto kill_file; 2052 2053 } else { 2054 /* 2055 * We have volatile file handles that don't compare. 2056 * If the fids are the same then we assume that the 2057 * file handle expired but the rnode still refers to 2058 * the same file object. 2059 * 2060 * First check that we have fids or not. 2061 * If we don't we have a dumb server so we will 2062 * just assume every thing is ok for now. 2063 */ 2064 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2065 rp->r_attr.va_mask & AT_NODEID && 2066 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2067 /* 2068 * We have fids, but they don't 2069 * compare. So kill the file. 2070 */ 2071 failed_msg = 2072 "Couldn't reopen: file handle changed" 2073 " due to mismatched fids"; 2074 nfs4args_copen_free(open_args); 2075 (void) xdr_free(xdr_COMPOUND4res_clnt, 2076 (caddr_t)&res); 2077 nfs_rw_exit(&mi->mi_fh_lock); 2078 goto kill_file; 2079 } else { 2080 /* 2081 * We have volatile file handles that refers 2082 * to the same file (at least they have the 2083 * same fid) or we don't have fids so we 2084 * can't tell. :(. We'll be a kind and accepting 2085 * client so we'll update the rnode's file 2086 * handle with the otw handle. 2087 * 2088 * We need to drop mi->mi_fh_lock since 2089 * sh4_update acquires it. Since there is 2090 * only one recovery thread there is no 2091 * race. 2092 */ 2093 nfs_rw_exit(&mi->mi_fh_lock); 2094 sfh4_update(rp->r_fh, &gf_res->object); 2095 } 2096 } 2097 } else { 2098 nfs_rw_exit(&mi->mi_fh_lock); 2099 } 2100 2101 ASSERT(nfs4_consistent_type(vp)); 2102 2103 /* 2104 * If the server wanted an OPEN_CONFIRM but that fails, just start 2105 * over. Presumably if there is a persistent error it will show up 2106 * when we resend the OPEN. 2107 */ 2108 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2109 bool_t retry_open = FALSE; 2110 2111 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2112 cred_otw, is_recov, &retry_open, 2113 oop, FALSE, ep, NULL); 2114 if (ep->error || ep->stat) { 2115 nfs4args_copen_free(open_args); 2116 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2117 nfs4_end_open_seqid_sync(oop); 2118 open_owner_rele(oop); 2119 oop = NULL; 2120 goto top; 2121 } 2122 } 2123 2124 mutex_enter(&osp->os_sync_lock); 2125 osp->open_stateid = op_res->stateid; 2126 osp->os_delegation = 0; 2127 /* 2128 * Need to reset this bitfield for the possible case where we were 2129 * going to OTW CLOSE the file, got a non-recoverable error, and before 2130 * we could retry the CLOSE, OPENed the file again. 2131 */ 2132 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2133 osp->os_final_close = 0; 2134 osp->os_force_close = 0; 2135 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2136 osp->os_dc_openacc = open_args->share_access; 2137 mutex_exit(&osp->os_sync_lock); 2138 2139 nfs4_end_open_seqid_sync(oop); 2140 2141 /* accept delegation, if any */ 2142 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2143 2144 nfs4args_copen_free(open_args); 2145 2146 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2147 2148 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2149 2150 ASSERT(nfs4_consistent_type(vp)); 2151 2152 open_owner_rele(oop); 2153 crfree(cr); 2154 crfree(cred_otw); 2155 return; 2156 2157 kill_file: 2158 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2159 failed_reopen: 2160 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2161 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2162 (void *)osp, (void *)cr, rnode4info(rp))); 2163 mutex_enter(&osp->os_sync_lock); 2164 osp->os_failed_reopen = 1; 2165 mutex_exit(&osp->os_sync_lock); 2166 bailout: 2167 if (oop != NULL) { 2168 nfs4_end_open_seqid_sync(oop); 2169 open_owner_rele(oop); 2170 } 2171 if (cr != NULL) 2172 crfree(cr); 2173 if (cred_otw != NULL) 2174 crfree(cred_otw); 2175 } 2176 2177 /* for . and .. OPENs */ 2178 /* ARGSUSED */ 2179 static int 2180 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2181 { 2182 rnode4_t *rp; 2183 nfs4_ga_res_t gar; 2184 2185 ASSERT(curproc->p_zone == VTOMI4(*vpp)->mi_zone); 2186 2187 /* 2188 * If close-to-open consistency checking is turned off or 2189 * if there is no cached data, we can avoid 2190 * the over the wire getattr. Otherwise, force a 2191 * call to the server to get fresh attributes and to 2192 * check caches. This is required for close-to-open 2193 * consistency. 2194 */ 2195 rp = VTOR4(*vpp); 2196 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2197 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2198 return (0); 2199 2200 gar.n4g_va.va_mask = AT_ALL; 2201 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2202 } 2203 2204 /* 2205 * CLOSE a file 2206 */ 2207 static int 2208 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 2209 { 2210 rnode4_t *rp; 2211 int pc_err = 0; 2212 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2213 2214 /* 2215 * Remove client state for this (lockowner, file) pair. 2216 * Issue otw v4 call to have the server do the same. 2217 */ 2218 2219 rp = VTOR4(vp); 2220 2221 /* 2222 * zone_enter(2) prevents processes from changing zones with NFS files 2223 * open; if we happen to get here from the wrong zone we can't do 2224 * anything over the wire. 2225 */ 2226 if (VTOMI4(vp)->mi_zone != curproc->p_zone) { 2227 /* 2228 * We could attempt to clean up locks, except we're sure 2229 * that the current process didn't acquire any locks on 2230 * the file: any attempt to lock a file belong to another zone 2231 * will fail, and one can't lock an NFS file and then change 2232 * zones, as that fails too. 2233 * 2234 * Returning an error here is the sane thing to do. A 2235 * subsequent call to VN_RELE() which translates to a 2236 * nfs4_inactive() will clean up state: if the zone of the 2237 * vnode's origin is still alive and kicking, the inactive 2238 * thread will handle the request (from the correct zone), and 2239 * everything (minus the OTW close call) should be OK. If the 2240 * zone is going away nfs4_async_inactive() will throw away 2241 * delegations, open streams and cached pages inline. 2242 */ 2243 return (EIO); 2244 } 2245 2246 /* 2247 * If we are using local locking for this filesystem, then 2248 * release all of the SYSV style record locks. Otherwise, 2249 * we are doing network locking and we need to release all 2250 * of the network locks. All of the locks held by this 2251 * process on this file are released no matter what the 2252 * incoming reference count is. 2253 */ 2254 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2255 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2256 cleanshares(vp, ttoproc(curthread)->p_pid); 2257 } else 2258 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2259 2260 if (e.error) 2261 return (e.error); 2262 2263 if (count > 1) 2264 return (0); 2265 2266 /* 2267 * If the file has been `unlinked', then purge the 2268 * DNLC so that this vnode will get reycled quicker 2269 * and the .nfs* file on the server will get removed. 2270 */ 2271 if (rp->r_unldvp != NULL) 2272 dnlc_purge_vp(vp); 2273 2274 /* 2275 * If the file was open for write and there are pages, 2276 * do a synchronous flush and commit of all of the 2277 * dirty and uncommitted pages. 2278 */ 2279 ASSERT(!e.error); 2280 if ((flag & FWRITE) && nfs4_has_pages(vp)) { 2281 pc_err = nfs4_putpage_commit(vp, 0, 0, cr); 2282 } 2283 2284 mutex_enter(&rp->r_statelock); 2285 e.error = rp->r_error; 2286 rp->r_error = 0; 2287 mutex_exit(&rp->r_statelock); 2288 2289 /* Check to see if we need to close the file */ 2290 2291 if (vp->v_type != VREG) 2292 return (pc_err ? pc_err : e.error); 2293 2294 /* Let nfs4close_one figure out if an OTW close is needed. */ 2295 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2296 2297 if (pc_err) 2298 return (pc_err); 2299 2300 return (e.error ? e.error : geterrno4(e.stat)); 2301 } 2302 2303 /* 2304 * Initialize *lost_rqstp. 2305 */ 2306 2307 static void 2308 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2309 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2310 vnode_t *vp) 2311 { 2312 if (error != ETIMEDOUT && error != EINTR && 2313 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2314 lost_rqstp->lr_op = 0; 2315 return; 2316 } 2317 2318 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2319 "nfs4close_save_lost_rqst: error %d", error)); 2320 2321 lost_rqstp->lr_op = OP_CLOSE; 2322 /* 2323 * The vp is held and rele'd via the recovery code. 2324 * See nfs4_save_lost_rqst. 2325 */ 2326 lost_rqstp->lr_vp = vp; 2327 lost_rqstp->lr_dvp = NULL; 2328 lost_rqstp->lr_oop = oop; 2329 lost_rqstp->lr_osp = osp; 2330 ASSERT(osp != NULL); 2331 ASSERT(mutex_owned(&osp->os_sync_lock)); 2332 osp->os_pending_close = 1; 2333 lost_rqstp->lr_lop = NULL; 2334 lost_rqstp->lr_cr = cr; 2335 lost_rqstp->lr_flk = NULL; 2336 lost_rqstp->lr_putfirst = FALSE; 2337 } 2338 2339 /* 2340 * Assumes you already have the open seqid sync grabbed as well as the 2341 * 'os_sync_lock'. Note: this will release the open seqid sync and 2342 * 'os_sync_lock' if client recovery starts. Calling functions have to 2343 * be prepared to handle this. 2344 * 2345 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2346 * was needed and was started, and that the calling function should retry 2347 * this function; otherwise it is returned as 0. 2348 * 2349 * Errors are returned via the nfs4_error_t parameter. 2350 */ 2351 static void 2352 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2353 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2354 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2355 { 2356 COMPOUND4args_clnt args; 2357 COMPOUND4res_clnt res; 2358 CLOSE4args *close_args; 2359 nfs_resop4 *resop; 2360 nfs_argop4 argop[3]; 2361 int doqueue = 1; 2362 mntinfo4_t *mi; 2363 seqid4 seqid; 2364 vnode_t *vp; 2365 bool_t needrecov = FALSE; 2366 nfs4_lost_rqst_t lost_rqst; 2367 hrtime_t t; 2368 2369 ASSERT(curproc->p_zone == VTOMI4(RTOV4(rp))->mi_zone); 2370 2371 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2372 2373 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2374 2375 /* Only set this to 1 if recovery is started */ 2376 *recov = 0; 2377 2378 /* do the OTW call to close the file */ 2379 2380 if (close_type == CLOSE_RESEND) 2381 args.ctag = TAG_CLOSE_LOST; 2382 else if (close_type == CLOSE_AFTER_RESEND) 2383 args.ctag = TAG_CLOSE_UNDO; 2384 else 2385 args.ctag = TAG_CLOSE; 2386 2387 args.array_len = 3; 2388 args.array = argop; 2389 2390 vp = RTOV4(rp); 2391 2392 mi = VTOMI4(vp); 2393 2394 /* putfh target fh */ 2395 argop[0].argop = OP_CPUTFH; 2396 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2397 2398 argop[1].argop = OP_GETATTR; 2399 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2400 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2401 2402 argop[2].argop = OP_CLOSE; 2403 close_args = &argop[2].nfs_argop4_u.opclose; 2404 2405 seqid = nfs4_get_open_seqid(oop) + 1; 2406 2407 close_args->seqid = seqid; 2408 close_args->open_stateid = osp->open_stateid; 2409 2410 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2411 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2412 rnode4info(rp))); 2413 2414 t = gethrtime(); 2415 2416 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2417 2418 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2419 nfs4_set_open_seqid(seqid, oop, args.ctag); 2420 } 2421 2422 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2423 if (ep->error && !needrecov) { 2424 /* 2425 * if there was an error and no recovery is to be done 2426 * then then set up the file to flush its cache if 2427 * needed for the next caller. 2428 */ 2429 mutex_enter(&rp->r_statelock); 2430 PURGE_ATTRCACHE4_LOCKED(rp); 2431 rp->r_flags &= ~R4WRITEMODIFIED; 2432 mutex_exit(&rp->r_statelock); 2433 return; 2434 } 2435 2436 if (needrecov) { 2437 bool_t abort; 2438 nfs4_bseqid_entry_t *bsep = NULL; 2439 2440 if (close_type != CLOSE_RESEND) 2441 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2442 osp, cred_otw, vp); 2443 2444 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2445 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2446 0, args.ctag, close_args->seqid); 2447 2448 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2449 "nfs4close_otw: initiating recovery. error %d " 2450 "res.status %d", ep->error, res.status)); 2451 2452 /* 2453 * Drop the 'os_sync_lock' here so we don't hit 2454 * a potential recursive mutex_enter via an 2455 * 'open_stream_hold()'. 2456 */ 2457 mutex_exit(&osp->os_sync_lock); 2458 *have_sync_lockp = 0; 2459 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2460 (close_type != CLOSE_RESEND && 2461 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2462 OP_CLOSE, bsep); 2463 2464 /* drop open seq sync, and let the calling function regrab it */ 2465 nfs4_end_open_seqid_sync(oop); 2466 *did_start_seqid_syncp = 0; 2467 2468 if (bsep) 2469 kmem_free(bsep, sizeof (*bsep)); 2470 /* 2471 * For signals, the caller wants to quit, so don't say to 2472 * retry. For forced unmount, if it's a user thread, it 2473 * wants to quit. If it's a recovery thread, the retry 2474 * will happen higher-up on the call stack. Either way, 2475 * don't say to retry. 2476 */ 2477 if (abort == FALSE && ep->error != EINTR && 2478 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2479 close_type != CLOSE_RESEND && 2480 close_type != CLOSE_AFTER_RESEND) 2481 *recov = 1; 2482 else 2483 *recov = 0; 2484 2485 if (!ep->error) 2486 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2487 return; 2488 } 2489 2490 if (res.status) { 2491 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2492 return; 2493 } 2494 2495 mutex_enter(&rp->r_statev4_lock); 2496 rp->created_v4 = 0; 2497 mutex_exit(&rp->r_statev4_lock); 2498 2499 resop = &res.array[2]; 2500 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2501 osp->os_valid = 0; 2502 2503 /* 2504 * This removes the reference obtained at OPEN; ie, when the 2505 * open stream structure was created. 2506 * 2507 * We don't have to worry about calling 'open_stream_rele' 2508 * since we our currently holding a reference to the open 2509 * stream which means the count cannot go to 0 with this 2510 * decrement. 2511 */ 2512 ASSERT(osp->os_ref_count >= 2); 2513 osp->os_ref_count--; 2514 2515 if (!ep->error) 2516 nfs4_attr_cache(vp, 2517 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2518 t, cred_otw, TRUE, NULL); 2519 2520 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2521 " returning %d", ep->error)); 2522 2523 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2524 } 2525 2526 /* ARGSUSED */ 2527 static int 2528 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2529 caller_context_t *ct) 2530 { 2531 rnode4_t *rp; 2532 u_offset_t off; 2533 offset_t diff; 2534 uint_t on; 2535 uint_t n; 2536 caddr_t base; 2537 uint_t flags; 2538 int error; 2539 mntinfo4_t *mi; 2540 2541 rp = VTOR4(vp); 2542 2543 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2544 2545 if (IS_SHADOW(vp, rp)) 2546 vp = RTOV4(rp); 2547 2548 if (vp->v_type != VREG) 2549 return (EISDIR); 2550 2551 mi = VTOMI4(vp); 2552 2553 if (curproc->p_zone != mi->mi_zone) 2554 return (EIO); 2555 2556 if (uiop->uio_resid == 0) 2557 return (0); 2558 2559 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2560 return (EINVAL); 2561 2562 mutex_enter(&rp->r_statelock); 2563 if (rp->r_flags & R4RECOVERRP) 2564 error = (rp->r_error ? rp->r_error : EIO); 2565 else 2566 error = 0; 2567 mutex_exit(&rp->r_statelock); 2568 if (error) 2569 return (error); 2570 2571 /* 2572 * Bypass VM if caching has been disabled (e.g., locking) or if 2573 * using client-side direct I/O and the file is not mmap'd and 2574 * there are no cached pages. 2575 */ 2576 if ((vp->v_flag & VNOCACHE) || 2577 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2578 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2579 size_t resid = 0; 2580 2581 return (nfs4read(vp, NULL, uiop->uio_loffset, 2582 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2583 } 2584 2585 error = 0; 2586 2587 do { 2588 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2589 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2590 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2591 2592 if (error = nfs4_validate_caches(vp, cr)) 2593 break; 2594 2595 mutex_enter(&rp->r_statelock); 2596 diff = rp->r_size - uiop->uio_loffset; 2597 mutex_exit(&rp->r_statelock); 2598 if (diff <= 0) 2599 break; 2600 if (diff < n) 2601 n = (uint_t)diff; 2602 2603 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, S_READ); 2604 2605 error = uiomove(base + on, n, UIO_READ, uiop); 2606 2607 if (!error) { 2608 /* 2609 * If read a whole block or read to eof, 2610 * won't need this buffer again soon. 2611 */ 2612 mutex_enter(&rp->r_statelock); 2613 if (n + on == MAXBSIZE || 2614 uiop->uio_loffset == rp->r_size) 2615 flags = SM_DONTNEED; 2616 else 2617 flags = 0; 2618 mutex_exit(&rp->r_statelock); 2619 error = segmap_release(segkmap, base, flags); 2620 } else 2621 (void) segmap_release(segkmap, base, 0); 2622 } while (!error && uiop->uio_resid > 0); 2623 2624 return (error); 2625 } 2626 2627 /* ARGSUSED */ 2628 static int 2629 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2630 caller_context_t *ct) 2631 { 2632 rlim64_t limit = uiop->uio_llimit; 2633 rnode4_t *rp; 2634 u_offset_t off; 2635 caddr_t base; 2636 uint_t flags; 2637 int remainder; 2638 size_t n; 2639 int on; 2640 int error; 2641 int resid; 2642 u_offset_t offset; 2643 mntinfo4_t *mi; 2644 uint_t bsize; 2645 2646 rp = VTOR4(vp); 2647 2648 if (IS_SHADOW(vp, rp)) 2649 vp = RTOV4(rp); 2650 2651 if (vp->v_type != VREG) 2652 return (EISDIR); 2653 2654 mi = VTOMI4(vp); 2655 2656 if (curproc->p_zone != mi->mi_zone) 2657 return (EIO); 2658 2659 if (uiop->uio_resid == 0) 2660 return (0); 2661 2662 mutex_enter(&rp->r_statelock); 2663 if (rp->r_flags & R4RECOVERRP) 2664 error = (rp->r_error ? rp->r_error : EIO); 2665 else 2666 error = 0; 2667 mutex_exit(&rp->r_statelock); 2668 if (error) 2669 return (error); 2670 2671 if (ioflag & FAPPEND) { 2672 struct vattr va; 2673 2674 /* 2675 * Must serialize if appending. 2676 */ 2677 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2678 nfs_rw_exit(&rp->r_rwlock); 2679 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2680 INTR(vp))) 2681 return (EINTR); 2682 } 2683 2684 va.va_mask = AT_SIZE; 2685 error = nfs4getattr(vp, &va, cr); 2686 if (error) 2687 return (error); 2688 uiop->uio_loffset = va.va_size; 2689 } 2690 2691 offset = uiop->uio_loffset + uiop->uio_resid; 2692 2693 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2694 return (EINVAL); 2695 2696 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2697 limit = MAXOFFSET_T; 2698 2699 /* 2700 * Check to make sure that the process will not exceed 2701 * its limit on file size. It is okay to write up to 2702 * the limit, but not beyond. Thus, the write which 2703 * reaches the limit will be short and the next write 2704 * will return an error. 2705 */ 2706 remainder = 0; 2707 if (offset > uiop->uio_llimit) { 2708 remainder = offset - uiop->uio_llimit; 2709 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2710 if (uiop->uio_resid <= 0) { 2711 proc_t *p = ttoproc(curthread); 2712 2713 uiop->uio_resid += remainder; 2714 mutex_enter(&p->p_lock); 2715 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2716 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2717 mutex_exit(&p->p_lock); 2718 return (EFBIG); 2719 } 2720 } 2721 2722 /* update the change attribute, if we have a write delegation */ 2723 2724 mutex_enter(&rp->r_statev4_lock); 2725 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2726 rp->r_deleg_change++; 2727 2728 mutex_exit(&rp->r_statev4_lock); 2729 2730 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2731 return (EINTR); 2732 2733 /* 2734 * Bypass VM if caching has been disabled (e.g., locking) or if 2735 * using client-side direct I/O and the file is not mmap'd and 2736 * there are no cached pages. 2737 */ 2738 if ((vp->v_flag & VNOCACHE) || 2739 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2740 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2741 size_t bufsize; 2742 int count; 2743 u_offset_t org_offset; 2744 stable_how4 stab_comm; 2745 nfs4_fwrite: 2746 if (rp->r_flags & R4STALE) { 2747 resid = uiop->uio_resid; 2748 offset = uiop->uio_loffset; 2749 error = rp->r_error; 2750 goto bottom; 2751 } 2752 2753 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2754 base = kmem_alloc(bufsize, KM_SLEEP); 2755 do { 2756 if (ioflag & FDSYNC) 2757 stab_comm = DATA_SYNC4; 2758 else 2759 stab_comm = FILE_SYNC4; 2760 resid = uiop->uio_resid; 2761 offset = uiop->uio_loffset; 2762 count = MIN(uiop->uio_resid, bufsize); 2763 org_offset = uiop->uio_loffset; 2764 error = uiomove(base, count, UIO_WRITE, uiop); 2765 if (!error) { 2766 error = nfs4write(vp, base, org_offset, 2767 count, cr, &stab_comm); 2768 if (!error) { 2769 mutex_enter(&rp->r_statelock); 2770 if (rp->r_size < uiop->uio_loffset) 2771 rp->r_size = uiop->uio_loffset; 2772 mutex_exit(&rp->r_statelock); 2773 } 2774 } 2775 } while (!error && uiop->uio_resid > 0); 2776 kmem_free(base, bufsize); 2777 goto bottom; 2778 } 2779 2780 bsize = vp->v_vfsp->vfs_bsize; 2781 2782 do { 2783 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2784 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2785 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2786 2787 resid = uiop->uio_resid; 2788 offset = uiop->uio_loffset; 2789 2790 if (rp->r_flags & R4STALE) { 2791 error = rp->r_error; 2792 break; 2793 } 2794 2795 /* 2796 * Don't create dirty pages faster than they 2797 * can be cleaned so that the system doesn't 2798 * get imbalanced. If the async queue is 2799 * maxed out, then wait for it to drain before 2800 * creating more dirty pages. Also, wait for 2801 * any threads doing pagewalks in the vop_getattr 2802 * entry points so that they don't block for 2803 * long periods. 2804 */ 2805 mutex_enter(&rp->r_statelock); 2806 while ((mi->mi_max_threads != 0 && 2807 rp->r_awcount > 2 * mi->mi_max_threads) || 2808 rp->r_gcount > 0) 2809 cv_wait(&rp->r_cv, &rp->r_statelock); 2810 mutex_exit(&rp->r_statelock); 2811 2812 if (segmap_kpm) { 2813 int pon = uiop->uio_loffset & PAGEOFFSET; 2814 size_t pn = MIN(PAGESIZE - pon, uiop->uio_resid); 2815 int pagecreate; 2816 2817 mutex_enter(&rp->r_statelock); 2818 pagecreate = (pon == 0) && (pn == PAGESIZE || 2819 uiop->uio_loffset + pn >= rp->r_size); 2820 mutex_exit(&rp->r_statelock); 2821 2822 base = segmap_getmapflt(segkmap, vp, off + on, 2823 pn, !pagecreate, S_WRITE); 2824 2825 error = writerp4(rp, base + pon, n, uiop, pagecreate); 2826 2827 } else { 2828 base = segmap_getmapflt(segkmap, vp, off + on, 2829 n, 0, S_READ); 2830 error = writerp4(rp, base + on, n, uiop, 0); 2831 } 2832 2833 if (!error) { 2834 if (mi->mi_flags & MI4_NOAC) 2835 flags = SM_WRITE; 2836 else if ((uiop->uio_loffset % bsize) == 0 || 2837 IS_SWAPVP(vp)) { 2838 /* 2839 * Have written a whole block. 2840 * Start an asynchronous write 2841 * and mark the buffer to 2842 * indicate that it won't be 2843 * needed again soon. 2844 */ 2845 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2846 } else 2847 flags = 0; 2848 if ((ioflag & (FSYNC|FDSYNC)) || 2849 (rp->r_flags & R4OUTOFSPACE)) { 2850 flags &= ~SM_ASYNC; 2851 flags |= SM_WRITE; 2852 } 2853 error = segmap_release(segkmap, base, flags); 2854 } else { 2855 (void) segmap_release(segkmap, base, 0); 2856 /* 2857 * In the event that we got an access error while 2858 * faulting in a page for a write-only file just 2859 * force a write. 2860 */ 2861 if (error == EACCES) 2862 goto nfs4_fwrite; 2863 } 2864 } while (!error && uiop->uio_resid > 0); 2865 2866 bottom: 2867 if (error) { 2868 uiop->uio_resid = resid + remainder; 2869 uiop->uio_loffset = offset; 2870 } else { 2871 uiop->uio_resid += remainder; 2872 2873 mutex_enter(&rp->r_statev4_lock); 2874 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 2875 gethrestime(&rp->r_attr.va_mtime); 2876 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 2877 } 2878 mutex_exit(&rp->r_statev4_lock); 2879 } 2880 2881 nfs_rw_exit(&rp->r_lkserlock); 2882 2883 return (error); 2884 } 2885 2886 /* 2887 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 2888 */ 2889 static int 2890 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 2891 int flags, cred_t *cr) 2892 { 2893 struct buf *bp; 2894 int error; 2895 page_t *savepp; 2896 uchar_t fsdata; 2897 stable_how4 stab_comm; 2898 2899 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 2900 bp = pageio_setup(pp, len, vp, flags); 2901 ASSERT(bp != NULL); 2902 2903 /* 2904 * pageio_setup should have set b_addr to 0. This 2905 * is correct since we want to do I/O on a page 2906 * boundary. bp_mapin will use this addr to calculate 2907 * an offset, and then set b_addr to the kernel virtual 2908 * address it allocated for us. 2909 */ 2910 ASSERT(bp->b_un.b_addr == 0); 2911 2912 bp->b_edev = 0; 2913 bp->b_dev = 0; 2914 bp->b_lblkno = lbtodb(off); 2915 bp->b_file = vp; 2916 bp->b_offset = (offset_t)off; 2917 bp_mapin(bp); 2918 2919 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 2920 freemem > desfree) 2921 stab_comm = UNSTABLE4; 2922 else 2923 stab_comm = FILE_SYNC4; 2924 2925 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 2926 2927 bp_mapout(bp); 2928 pageio_done(bp); 2929 2930 if (stab_comm == UNSTABLE4) 2931 fsdata = C_DELAYCOMMIT; 2932 else 2933 fsdata = C_NOCOMMIT; 2934 2935 savepp = pp; 2936 do { 2937 pp->p_fsdata = fsdata; 2938 } while ((pp = pp->p_next) != savepp); 2939 2940 return (error); 2941 } 2942 2943 /* 2944 */ 2945 static int 2946 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 2947 { 2948 nfs4_open_owner_t *oop; 2949 nfs4_open_stream_t *osp; 2950 rnode4_t *rp = VTOR4(vp); 2951 mntinfo4_t *mi = VTOMI4(vp); 2952 int reopen_needed; 2953 2954 ASSERT(curproc->p_zone == mi->mi_zone); 2955 2956 2957 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 2958 if (!oop) 2959 return (EIO); 2960 2961 /* returns with 'os_sync_lock' held */ 2962 osp = find_open_stream(oop, rp); 2963 if (!osp) { 2964 open_owner_rele(oop); 2965 return (EIO); 2966 } 2967 2968 if (osp->os_failed_reopen) { 2969 mutex_exit(&osp->os_sync_lock); 2970 open_stream_rele(osp, rp); 2971 open_owner_rele(oop); 2972 return (EIO); 2973 } 2974 2975 /* 2976 * Determine whether a reopen is needed. If this 2977 * is a delegation open stream, then the os_delegation bit 2978 * should be set. 2979 */ 2980 2981 reopen_needed = osp->os_delegation; 2982 2983 mutex_exit(&osp->os_sync_lock); 2984 open_owner_rele(oop); 2985 2986 if (reopen_needed) { 2987 nfs4_error_zinit(ep); 2988 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 2989 mutex_enter(&osp->os_sync_lock); 2990 if (ep->error || ep->stat || osp->os_failed_reopen) { 2991 mutex_exit(&osp->os_sync_lock); 2992 open_stream_rele(osp, rp); 2993 return (EIO); 2994 } 2995 mutex_exit(&osp->os_sync_lock); 2996 } 2997 open_stream_rele(osp, rp); 2998 2999 return (0); 3000 } 3001 3002 /* 3003 * Write to file. Writes to remote server in largest size 3004 * chunks that the server can handle. Write is synchronous. 3005 */ 3006 static int 3007 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3008 stable_how4 *stab_comm) 3009 { 3010 mntinfo4_t *mi; 3011 COMPOUND4args_clnt args; 3012 COMPOUND4res_clnt res; 3013 WRITE4args *wargs; 3014 WRITE4res *wres; 3015 nfs_argop4 argop[2]; 3016 nfs_resop4 *resop; 3017 int tsize; 3018 stable_how4 stable; 3019 rnode4_t *rp; 3020 int doqueue = 1; 3021 bool_t needrecov; 3022 nfs4_recov_state_t recov_state; 3023 nfs4_stateid_types_t sid_types; 3024 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3025 3026 rp = VTOR4(vp); 3027 mi = VTOMI4(vp); 3028 3029 ASSERT(curproc->p_zone == mi->mi_zone); 3030 3031 stable = *stab_comm; 3032 *stab_comm = FILE_SYNC4; 3033 3034 needrecov = FALSE; 3035 recov_state.rs_flags = 0; 3036 recov_state.rs_num_retry_despite_err = 0; 3037 nfs4_init_stateid_types(&sid_types); 3038 3039 recov_retry: 3040 args.ctag = TAG_WRITE; 3041 args.array_len = 2; 3042 args.array = argop; 3043 3044 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3045 &recov_state, NULL); 3046 if (e.error) 3047 return (e.error); 3048 3049 /* 0. putfh target fh */ 3050 argop[0].argop = OP_CPUTFH; 3051 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3052 3053 /* 1. write */ 3054 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3055 3056 do { 3057 3058 wargs->offset = (offset4)offset; 3059 wargs->data_val = base; 3060 3061 if (mi->mi_io_kstats) { 3062 mutex_enter(&mi->mi_lock); 3063 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3064 mutex_exit(&mi->mi_lock); 3065 } 3066 3067 if ((vp->v_flag & VNOCACHE) || 3068 (rp->r_flags & R4DIRECTIO) || 3069 (mi->mi_flags & MI4_DIRECTIO)) 3070 tsize = MIN(mi->mi_stsize, count); 3071 else 3072 tsize = MIN(mi->mi_curwrite, count); 3073 wargs->data_len = (uint_t)tsize; 3074 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3075 3076 if (mi->mi_io_kstats) { 3077 mutex_enter(&mi->mi_lock); 3078 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3079 mutex_exit(&mi->mi_lock); 3080 } 3081 3082 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3083 if (e.error && !needrecov) { 3084 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3085 &recov_state, needrecov); 3086 return (e.error); 3087 } 3088 3089 3090 /* 3091 * Do handling of OLD_STATEID outside 3092 * of the normal recovery framework. 3093 * 3094 * If write receives a BAD stateid error while using a 3095 * delegation stateid, retry using the open stateid (if it 3096 * exists). If it doesn't have an open stateid, reopen the 3097 * file first, then retry. 3098 */ 3099 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3100 sid_types.cur_sid_type != SPEC_SID) { 3101 nfs4_save_stateid(&wargs->stateid, &sid_types); 3102 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3103 &recov_state, needrecov); 3104 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3105 goto recov_retry; 3106 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3107 sid_types.cur_sid_type == DEL_SID) { 3108 nfs4_save_stateid(&wargs->stateid, &sid_types); 3109 mutex_enter(&rp->r_statev4_lock); 3110 rp->r_deleg_return_pending = TRUE; 3111 mutex_exit(&rp->r_statev4_lock); 3112 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3113 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3114 &recov_state, needrecov); 3115 (void) xdr_free(xdr_COMPOUND4res_clnt, 3116 (caddr_t)&res); 3117 return (EIO); 3118 } 3119 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3120 &recov_state, needrecov); 3121 /* hold needed for nfs4delegreturn_thread */ 3122 VN_HOLD(vp); 3123 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3124 NFS4_DR_DISCARD), FALSE); 3125 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3126 goto recov_retry; 3127 } 3128 3129 if (needrecov) { 3130 bool_t abort; 3131 3132 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3133 "nfs4write: client got error %d, res.status %d" 3134 ", so start recovery", e.error, res.status)); 3135 3136 abort = nfs4_start_recovery(&e, 3137 VTOMI4(vp), vp, NULL, &wargs->stateid, 3138 NULL, OP_WRITE, NULL); 3139 if (!e.error) { 3140 e.error = geterrno4(res.status); 3141 (void) xdr_free(xdr_COMPOUND4res_clnt, 3142 (caddr_t)&res); 3143 } 3144 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3145 &recov_state, needrecov); 3146 if (abort == FALSE) 3147 goto recov_retry; 3148 return (e.error); 3149 } 3150 3151 if (res.status) { 3152 e.error = geterrno4(res.status); 3153 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3154 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3155 &recov_state, needrecov); 3156 return (e.error); 3157 } 3158 3159 resop = &res.array[1]; /* write res */ 3160 wres = &resop->nfs_resop4_u.opwrite; 3161 3162 if ((int)wres->count > tsize) { 3163 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3164 3165 zcmn_err(getzoneid(), CE_WARN, 3166 "nfs4write: server wrote %u, requested was %u", 3167 (int)wres->count, tsize); 3168 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3169 &recov_state, needrecov); 3170 return (EIO); 3171 } 3172 if (wres->committed == UNSTABLE4) { 3173 *stab_comm = UNSTABLE4; 3174 if (wargs->stable == DATA_SYNC4 || 3175 wargs->stable == FILE_SYNC4) { 3176 (void) xdr_free(xdr_COMPOUND4res_clnt, 3177 (caddr_t)&res); 3178 zcmn_err(getzoneid(), CE_WARN, 3179 "nfs4write: server %s did not commit " 3180 "to stable storage", 3181 rp->r_server->sv_hostname); 3182 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3183 &recov_state, needrecov); 3184 return (EIO); 3185 } 3186 } 3187 3188 tsize = (int)wres->count; 3189 count -= tsize; 3190 base += tsize; 3191 offset += tsize; 3192 if (mi->mi_io_kstats) { 3193 mutex_enter(&mi->mi_lock); 3194 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3195 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3196 tsize; 3197 mutex_exit(&mi->mi_lock); 3198 } 3199 lwp_stat_update(LWP_STAT_OUBLK, 1); 3200 mutex_enter(&rp->r_statelock); 3201 if (rp->r_flags & R4HAVEVERF) { 3202 if (rp->r_writeverf != wres->writeverf) { 3203 nfs4_set_mod(vp); 3204 rp->r_writeverf = wres->writeverf; 3205 } 3206 } else { 3207 rp->r_writeverf = wres->writeverf; 3208 rp->r_flags |= R4HAVEVERF; 3209 } 3210 PURGE_ATTRCACHE4_LOCKED(rp); 3211 rp->r_flags |= R4WRITEMODIFIED; 3212 gethrestime(&rp->r_attr.va_mtime); 3213 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3214 mutex_exit(&rp->r_statelock); 3215 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3216 } while (count); 3217 3218 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, needrecov); 3219 3220 return (e.error); 3221 } 3222 3223 /* 3224 * Read from a file. Reads data in largest chunks our interface can handle. 3225 */ 3226 static int 3227 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3228 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3229 { 3230 mntinfo4_t *mi; 3231 COMPOUND4args_clnt args; 3232 COMPOUND4res_clnt res; 3233 READ4args *rargs; 3234 nfs_argop4 argop[2]; 3235 int tsize; 3236 int doqueue; 3237 rnode4_t *rp; 3238 int data_len; 3239 bool_t is_eof; 3240 bool_t needrecov = FALSE; 3241 nfs4_recov_state_t recov_state; 3242 nfs4_stateid_types_t sid_types; 3243 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3244 3245 rp = VTOR4(vp); 3246 mi = VTOMI4(vp); 3247 doqueue = 1; 3248 3249 ASSERT(curproc->p_zone == mi->mi_zone); 3250 3251 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3252 3253 args.array_len = 2; 3254 args.array = argop; 3255 3256 nfs4_init_stateid_types(&sid_types); 3257 3258 recov_state.rs_flags = 0; 3259 recov_state.rs_num_retry_despite_err = 0; 3260 3261 recov_retry: 3262 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3263 &recov_state, NULL); 3264 if (e.error) 3265 return (e.error); 3266 3267 /* putfh target fh */ 3268 argop[0].argop = OP_CPUTFH; 3269 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3270 3271 /* read */ 3272 argop[1].argop = OP_READ; 3273 rargs = &argop[1].nfs_argop4_u.opread; 3274 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3275 OP_READ, &sid_types, async); 3276 3277 do { 3278 if (mi->mi_io_kstats) { 3279 mutex_enter(&mi->mi_lock); 3280 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3281 mutex_exit(&mi->mi_lock); 3282 } 3283 3284 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3285 "nfs4read: %s call, rp %s", 3286 needrecov ? "recov" : "first", 3287 rnode4info(rp))); 3288 3289 if ((vp->v_flag & VNOCACHE) || 3290 (rp->r_flags & R4DIRECTIO) || 3291 (mi->mi_flags & MI4_DIRECTIO)) 3292 tsize = MIN(mi->mi_tsize, count); 3293 else 3294 tsize = MIN(mi->mi_curread, count); 3295 rargs->offset = (offset4)offset; 3296 rargs->count = (count4)tsize; 3297 rargs->res_data_val_alt = NULL; 3298 rargs->res_mblk = NULL; 3299 rargs->res_uiop = NULL; 3300 rargs->res_maxsize = 0; 3301 if (uiop) 3302 rargs->res_uiop = uiop; 3303 else 3304 rargs->res_data_val_alt = base; 3305 rargs->res_maxsize = tsize; 3306 3307 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3308 #ifdef DEBUG 3309 if (nfs4read_error_inject) { 3310 res.status = nfs4read_error_inject; 3311 nfs4read_error_inject = 0; 3312 } 3313 #endif 3314 3315 if (mi->mi_io_kstats) { 3316 mutex_enter(&mi->mi_lock); 3317 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3318 mutex_exit(&mi->mi_lock); 3319 } 3320 3321 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3322 if (e.error != 0 && !needrecov) { 3323 nfs4_end_fop(mi, vp, NULL, OH_READ, 3324 &recov_state, needrecov); 3325 return (e.error); 3326 } 3327 3328 /* 3329 * Do proper retry for OLD and BAD stateid errors outside 3330 * of the normal recovery framework. There are two differences 3331 * between async and sync reads. The first is that we allow 3332 * retry on BAD_STATEID for async reads, but not sync reads. 3333 * The second is that we mark the file dead for a failed 3334 * attempt with a special stateid for sync reads, but just 3335 * return EIO for async reads. 3336 * 3337 * If a sync read receives a BAD stateid error while using a 3338 * delegation stateid, retry using the open stateid (if it 3339 * exists). If it doesn't have an open stateid, reopen the 3340 * file first, then retry. 3341 */ 3342 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3343 res.status == NFS4ERR_BAD_STATEID) && async) { 3344 nfs4_end_fop(mi, vp, NULL, OH_READ, 3345 &recov_state, needrecov); 3346 if (sid_types.cur_sid_type == SPEC_SID) { 3347 (void) xdr_free(xdr_COMPOUND4res_clnt, 3348 (caddr_t)&res); 3349 return (EIO); 3350 } 3351 nfs4_save_stateid(&rargs->stateid, &sid_types); 3352 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3353 goto recov_retry; 3354 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3355 !async && sid_types.cur_sid_type != SPEC_SID) { 3356 nfs4_save_stateid(&rargs->stateid, &sid_types); 3357 nfs4_end_fop(mi, vp, NULL, OH_READ, 3358 &recov_state, needrecov); 3359 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3360 goto recov_retry; 3361 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3362 sid_types.cur_sid_type == DEL_SID) { 3363 nfs4_save_stateid(&rargs->stateid, &sid_types); 3364 mutex_enter(&rp->r_statev4_lock); 3365 rp->r_deleg_return_pending = TRUE; 3366 mutex_exit(&rp->r_statev4_lock); 3367 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3368 nfs4_end_fop(mi, vp, NULL, OH_READ, 3369 &recov_state, needrecov); 3370 (void) xdr_free(xdr_COMPOUND4res_clnt, 3371 (caddr_t)&res); 3372 return (EIO); 3373 } 3374 nfs4_end_fop(mi, vp, NULL, OH_READ, 3375 &recov_state, needrecov); 3376 /* hold needed for nfs4delegreturn_thread */ 3377 VN_HOLD(vp); 3378 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3379 NFS4_DR_DISCARD), FALSE); 3380 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3381 goto recov_retry; 3382 } 3383 if (needrecov) { 3384 bool_t abort; 3385 3386 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3387 "nfs4read: initiating recovery\n")); 3388 3389 abort = nfs4_start_recovery(&e, 3390 mi, vp, NULL, &rargs->stateid, 3391 NULL, OP_READ, NULL); 3392 nfs4_end_fop(mi, vp, NULL, OH_READ, 3393 &recov_state, needrecov); 3394 /* 3395 * Do not retry if we got OLD_STATEID using a special 3396 * stateid. This avoids looping with a broken server. 3397 */ 3398 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3399 sid_types.cur_sid_type == SPEC_SID) 3400 abort = TRUE; 3401 3402 if (abort == FALSE) { 3403 /* 3404 * Need to retry all possible stateids in 3405 * case the recovery error wasn't stateid 3406 * related or the stateids have become 3407 * stale (server reboot). 3408 */ 3409 nfs4_init_stateid_types(&sid_types); 3410 (void) xdr_free(xdr_COMPOUND4res_clnt, 3411 (caddr_t)&res); 3412 goto recov_retry; 3413 } 3414 3415 if (!e.error) { 3416 e.error = geterrno4(res.status); 3417 (void) xdr_free(xdr_COMPOUND4res_clnt, 3418 (caddr_t)&res); 3419 } 3420 return (e.error); 3421 } 3422 3423 if (res.status) { 3424 e.error = geterrno4(res.status); 3425 nfs4_end_fop(mi, vp, NULL, OH_READ, 3426 &recov_state, needrecov); 3427 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3428 return (e.error); 3429 } 3430 3431 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3432 count -= data_len; 3433 if (base) 3434 base += data_len; 3435 offset += data_len; 3436 if (mi->mi_io_kstats) { 3437 mutex_enter(&mi->mi_lock); 3438 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3439 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3440 mutex_exit(&mi->mi_lock); 3441 } 3442 lwp_stat_update(LWP_STAT_INBLK, 1); 3443 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3444 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3445 3446 } while (count && !is_eof); 3447 3448 *residp = count; 3449 3450 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3451 3452 return (e.error); 3453 } 3454 3455 /* ARGSUSED */ 3456 static int 3457 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3458 { 3459 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3460 return (EIO); 3461 switch (cmd) { 3462 case _FIODIRECTIO: 3463 return (nfs4_directio(vp, (int)arg, cr)); 3464 default: 3465 return (ENOTTY); 3466 } 3467 } 3468 3469 static int 3470 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 3471 { 3472 int error; 3473 rnode4_t *rp = VTOR4(vp); 3474 3475 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3476 return (EIO); 3477 /* 3478 * If it has been specified that the return value will 3479 * just be used as a hint, and we are only being asked 3480 * for size, fsid or rdevid, then return the client's 3481 * notion of these values without checking to make sure 3482 * that the attribute cache is up to date. 3483 * The whole point is to avoid an over the wire GETATTR 3484 * call. 3485 */ 3486 if (flags & ATTR_HINT) { 3487 if (vap->va_mask == 3488 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3489 mutex_enter(&rp->r_statelock); 3490 if (vap->va_mask | AT_SIZE) 3491 vap->va_size = rp->r_size; 3492 if (vap->va_mask | AT_FSID) 3493 vap->va_fsid = rp->r_attr.va_fsid; 3494 if (vap->va_mask | AT_RDEV) 3495 vap->va_rdev = rp->r_attr.va_rdev; 3496 mutex_exit(&rp->r_statelock); 3497 return (0); 3498 } 3499 } 3500 3501 /* 3502 * Only need to flush pages if asking for the mtime 3503 * and if there any dirty pages or any outstanding 3504 * asynchronous (write) requests for this file. 3505 */ 3506 if (vap->va_mask & AT_MTIME) { 3507 rp = VTOR4(vp); 3508 if (nfs4_has_pages(vp)) { 3509 mutex_enter(&rp->r_statev4_lock); 3510 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3511 mutex_exit(&rp->r_statev4_lock); 3512 if (rp->r_flags & R4DIRTY || 3513 rp->r_awcount > 0) { 3514 mutex_enter(&rp->r_statelock); 3515 rp->r_gcount++; 3516 mutex_exit(&rp->r_statelock); 3517 error = 3518 nfs4_putpage(vp, (u_offset_t)0, 3519 0, 0, cr); 3520 mutex_enter(&rp->r_statelock); 3521 if (error && (error == ENOSPC || 3522 error == EDQUOT)) { 3523 if (!rp->r_error) 3524 rp->r_error = error; 3525 } 3526 if (--rp->r_gcount == 0) 3527 cv_broadcast(&rp->r_cv); 3528 mutex_exit(&rp->r_statelock); 3529 } 3530 } else { 3531 mutex_exit(&rp->r_statev4_lock); 3532 } 3533 } 3534 } 3535 return (nfs4getattr(vp, vap, cr)); 3536 } 3537 3538 int 3539 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3540 { 3541 /* 3542 * If these are the only two bits cleared 3543 * on the server then return 0 (OK) else 3544 * return 1 (BAD). 3545 */ 3546 on_client &= ~(S_ISUID|S_ISGID); 3547 if (on_client == from_server) 3548 return (0); 3549 else 3550 return (1); 3551 } 3552 3553 /*ARGSUSED4*/ 3554 static int 3555 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3556 caller_context_t *ct) 3557 { 3558 if (vap->va_mask & AT_NOSET) 3559 return (EINVAL); 3560 3561 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3562 return (EIO); 3563 3564 /* 3565 * Don't call secpolicy_vnode_setattr, the client cannot 3566 * use its cached attributes to make security decisions 3567 * as the server may be faking mode bits or mapping uid/gid. 3568 * Always just let the server to the checking. 3569 * If we provide the ability to remove basic priviledges 3570 * to setattr (e.g. basic without chmod) then we will 3571 * need to add a check here before calling the server. 3572 */ 3573 3574 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3575 } 3576 3577 /* 3578 * To replace the "guarded" version 3 setattr, we use two types of compound 3579 * setattr requests: 3580 * 1. The "normal" setattr, used when the size of the file isn't being 3581 * changed - { Putfh <fh>; Setattr; Getattr }/ 3582 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3583 * with only ctime as the argument. If the server ctime differs from 3584 * what is cached on the client, the verify will fail, but we would 3585 * already have the ctime from the preceding getattr, so just set it 3586 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3587 * Setattr; Getattr }. 3588 * 3589 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3590 * this setattr and NULL if they are not. 3591 */ 3592 static int 3593 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3594 vsecattr_t *vsap) 3595 { 3596 COMPOUND4args_clnt args; 3597 COMPOUND4res_clnt res, *resp = NULL; 3598 nfs4_ga_res_t *garp = NULL; 3599 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3600 nfs_argop4 argop[5]; 3601 int verify_argop = -1; 3602 int setattr_argop = 1; 3603 nfs_resop4 *resop; 3604 vattr_t va; 3605 rnode4_t *rp; 3606 int doqueue = 1; 3607 uint_t mask = vap->va_mask; 3608 mode_t omode; 3609 vsecattr_t *vsp; 3610 timestruc_t ctime; 3611 bool_t needrecov = FALSE; 3612 nfs4_recov_state_t recov_state; 3613 nfs4_stateid_types_t sid_types; 3614 stateid4 stateid; 3615 hrtime_t t; 3616 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3617 servinfo4_t *svp; 3618 bitmap4 supp_attrs; 3619 3620 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 3621 rp = VTOR4(vp); 3622 nfs4_init_stateid_types(&sid_types); 3623 3624 /* 3625 * Only need to flush pages if there are any pages and 3626 * if the file is marked as dirty in some fashion. The 3627 * file must be flushed so that we can accurately 3628 * determine the size of the file and the cached data 3629 * after the SETATTR returns. A file is considered to 3630 * be dirty if it is either marked with R4DIRTY, has 3631 * outstanding i/o's active, or is mmap'd. In this 3632 * last case, we can't tell whether there are dirty 3633 * pages, so we flush just to be sure. 3634 */ 3635 if (nfs4_has_pages(vp) && 3636 ((rp->r_flags & R4DIRTY) || 3637 rp->r_count > 0 || 3638 rp->r_mapcnt > 0)) { 3639 ASSERT(vp->v_type != VCHR); 3640 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr); 3641 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3642 mutex_enter(&rp->r_statelock); 3643 if (!rp->r_error) 3644 rp->r_error = e.error; 3645 mutex_exit(&rp->r_statelock); 3646 } 3647 } 3648 3649 if (mask & AT_SIZE) { 3650 /* 3651 * Verification setattr compound for non-deleg AT_SIZE: 3652 * { Putfh; Getattr; Verify; Setattr; Getattr } 3653 * Set ctime local here (outside the do_again label) 3654 * so that subsequent retries (after failed VERIFY) 3655 * will use ctime from GETATTR results (from failed 3656 * verify compound) as VERIFY arg. 3657 * If file has delegation, then VERIFY(time_metadata) 3658 * is of little added value, so don't bother. 3659 */ 3660 mutex_enter(&rp->r_statev4_lock); 3661 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3662 rp->r_deleg_return_pending) { 3663 numops = 5; 3664 ctime = rp->r_attr.va_ctime; 3665 } 3666 mutex_exit(&rp->r_statev4_lock); 3667 } 3668 3669 recov_state.rs_flags = 0; 3670 recov_state.rs_num_retry_despite_err = 0; 3671 3672 args.ctag = TAG_SETATTR; 3673 do_again: 3674 recov_retry: 3675 setattr_argop = numops - 2; 3676 3677 args.array = argop; 3678 args.array_len = numops; 3679 3680 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3681 if (e.error) 3682 return (e.error); 3683 3684 3685 /* putfh target fh */ 3686 argop[0].argop = OP_CPUTFH; 3687 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3688 3689 if (numops == 5) { 3690 /* 3691 * We only care about the ctime, but need to get mtime 3692 * and size for proper cache update. 3693 */ 3694 /* getattr */ 3695 argop[1].argop = OP_GETATTR; 3696 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3697 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3698 3699 /* verify - set later in loop */ 3700 verify_argop = 2; 3701 } 3702 3703 /* setattr */ 3704 svp = rp->r_server; 3705 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3706 supp_attrs = svp->sv_supp_attrs; 3707 nfs_rw_exit(&svp->sv_lock); 3708 3709 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3710 supp_attrs, &e.error, &sid_types); 3711 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3712 if (e.error) { 3713 /* req time field(s) overflow - return immediately */ 3714 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3715 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3716 opsetattr.obj_attributes); 3717 return (e.error); 3718 } 3719 omode = rp->r_attr.va_mode; 3720 3721 /* getattr */ 3722 argop[numops-1].argop = OP_GETATTR; 3723 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3724 /* 3725 * If we are setting the ACL (indicated only by vsap != NULL), request 3726 * the ACL in this getattr. The ACL returned from this getattr will be 3727 * used in updating the ACL cache. 3728 */ 3729 if (vsap != NULL) 3730 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3731 FATTR4_ACL_MASK; 3732 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3733 3734 /* 3735 * setattr iterates if the object size is set and the cached ctime 3736 * does not match the file ctime. In that case, verify the ctime first. 3737 */ 3738 3739 do { 3740 if (verify_argop != -1) { 3741 /* 3742 * Verify that the ctime match before doing setattr. 3743 */ 3744 va.va_mask = AT_CTIME; 3745 va.va_ctime = ctime; 3746 svp = rp->r_server; 3747 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3748 supp_attrs = svp->sv_supp_attrs; 3749 nfs_rw_exit(&svp->sv_lock); 3750 e.error = nfs4args_verify(&argop[verify_argop], &va, 3751 OP_VERIFY, supp_attrs); 3752 if (e.error) { 3753 /* req time field(s) overflow - return */ 3754 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3755 needrecov); 3756 break; 3757 } 3758 } 3759 3760 doqueue = 1; 3761 3762 t = gethrtime(); 3763 3764 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3765 3766 /* 3767 * Purge the access cache and ACL cache if changing either the 3768 * owner of the file, the group owner, or the mode. These may 3769 * change the access permissions of the file, so purge old 3770 * information and start over again. 3771 */ 3772 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3773 (void) nfs4_access_purge_rp(rp); 3774 if (rp->r_secattr != NULL) { 3775 mutex_enter(&rp->r_statelock); 3776 vsp = rp->r_secattr; 3777 rp->r_secattr = NULL; 3778 mutex_exit(&rp->r_statelock); 3779 if (vsp != NULL) 3780 nfs4_acl_free_cache(vsp); 3781 } 3782 } 3783 3784 /* 3785 * If res.array_len == numops, then everything succeeded, 3786 * except for possibly the final getattr. If only the 3787 * last getattr failed, give up, and don't try recovery. 3788 */ 3789 if (res.array_len == numops) { 3790 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3791 needrecov); 3792 if (! e.error) 3793 resp = &res; 3794 break; 3795 } 3796 3797 /* 3798 * if either rpc call failed or completely succeeded - done 3799 */ 3800 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3801 if (e.error) { 3802 PURGE_ATTRCACHE4(vp); 3803 if (!needrecov) { 3804 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3805 needrecov); 3806 break; 3807 } 3808 } 3809 3810 /* 3811 * Do proper retry for OLD_STATEID outside of the normal 3812 * recovery framework. 3813 */ 3814 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3815 sid_types.cur_sid_type != SPEC_SID && 3816 sid_types.cur_sid_type != NO_SID) { 3817 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3818 needrecov); 3819 nfs4_save_stateid(&stateid, &sid_types); 3820 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3821 opsetattr.obj_attributes); 3822 if (verify_argop != -1) { 3823 nfs4args_verify_free(&argop[verify_argop]); 3824 verify_argop = -1; 3825 } 3826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3827 goto recov_retry; 3828 } 3829 3830 if (needrecov) { 3831 bool_t abort; 3832 3833 abort = nfs4_start_recovery(&e, 3834 VTOMI4(vp), vp, NULL, NULL, NULL, 3835 OP_SETATTR, NULL); 3836 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3837 needrecov); 3838 /* 3839 * Do not retry if we failed with OLD_STATEID using 3840 * a special stateid. This is done to avoid looping 3841 * with a broken server. 3842 */ 3843 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3844 (sid_types.cur_sid_type == SPEC_SID || 3845 sid_types.cur_sid_type == NO_SID)) 3846 abort = TRUE; 3847 if (!e.error) { 3848 if (res.status == NFS4ERR_BADOWNER) 3849 nfs4_log_badowner(VTOMI4(vp), 3850 OP_SETATTR); 3851 3852 e.error = geterrno4(res.status); 3853 (void) xdr_free(xdr_COMPOUND4res_clnt, 3854 (caddr_t)&res); 3855 } 3856 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3857 opsetattr.obj_attributes); 3858 if (verify_argop != -1) { 3859 nfs4args_verify_free(&argop[verify_argop]); 3860 verify_argop = -1; 3861 } 3862 if (abort == FALSE) { 3863 /* 3864 * Need to retry all possible stateids in 3865 * case the recovery error wasn't stateid 3866 * related or the stateids have become 3867 * stale (server reboot). 3868 */ 3869 nfs4_init_stateid_types(&sid_types); 3870 goto recov_retry; 3871 } 3872 return (e.error); 3873 } 3874 3875 /* 3876 * Need to call nfs4_end_op before nfs4getattr to 3877 * avoid potential nfs4_start_op deadlock. See RFE 3878 * 4777612. Calls to nfs4_invalidate_pages() and 3879 * nfs4_purge_stale_fh() might also generate over the 3880 * wire calls which my cause nfs4_start_op() deadlock. 3881 */ 3882 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3883 3884 /* 3885 * Check to update lease. 3886 */ 3887 resp = &res; 3888 if (res.status == NFS4_OK) { 3889 break; 3890 } 3891 3892 /* 3893 * Check if verify failed to see if try again 3894 */ 3895 if ((verify_argop == -1) || (res.array_len != 3)) { 3896 /* 3897 * can't continue... 3898 */ 3899 if (res.status == NFS4ERR_BADOWNER) 3900 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 3901 3902 e.error = geterrno4(res.status); 3903 } else { 3904 /* 3905 * When the verify request fails, the client ctime is 3906 * not in sync with the server. This is the same as 3907 * the version 3 "not synchronized" error, and we 3908 * handle it in a similar manner (XXX do we need to???). 3909 * Use the ctime returned in the first getattr for 3910 * the input to the next verify. 3911 * If we couldn't get the attributes, then we give up 3912 * because we can't complete the operation as required. 3913 */ 3914 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 3915 } 3916 if (e.error) { 3917 PURGE_ATTRCACHE4(vp); 3918 nfs4_purge_stale_fh(e.error, vp, cr); 3919 } else { 3920 /* 3921 * retry with a new verify value 3922 */ 3923 ctime = garp->n4g_va.va_ctime; 3924 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3925 resp = NULL; 3926 } 3927 if (!e.error) { 3928 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3929 opsetattr.obj_attributes); 3930 if (verify_argop != -1) { 3931 nfs4args_verify_free(&argop[verify_argop]); 3932 verify_argop = -1; 3933 } 3934 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3935 goto do_again; 3936 } 3937 } while (!e.error); 3938 3939 if (e.error) { 3940 /* 3941 * If we are here, rfs4call has an irrecoverable error - return 3942 */ 3943 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3944 opsetattr.obj_attributes); 3945 if (verify_argop != -1) { 3946 nfs4args_verify_free(&argop[verify_argop]); 3947 verify_argop = -1; 3948 } 3949 if (resp) 3950 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 3951 return (e.error); 3952 } 3953 3954 3955 3956 /* 3957 * If changing the size of the file, invalidate 3958 * any local cached data which is no longer part 3959 * of the file. We also possibly invalidate the 3960 * last page in the file. We could use 3961 * pvn_vpzero(), but this would mark the page as 3962 * modified and require it to be written back to 3963 * the server for no particularly good reason. 3964 * This way, if we access it, then we bring it 3965 * back in. A read should be cheaper than a 3966 * write. 3967 */ 3968 if (mask & AT_SIZE) { 3969 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 3970 } 3971 3972 /* either no error or one of the postop getattr failed */ 3973 3974 /* 3975 * XXX Perform a simplified version of wcc checking. Instead of 3976 * have another getattr to get pre-op, just purge cache if 3977 * any of the ops prior to and including the getattr failed. 3978 * If the getattr succeeded then update the attrcache accordingly. 3979 */ 3980 3981 garp = NULL; 3982 if (res.status == NFS4_OK) { 3983 /* 3984 * Last getattr 3985 */ 3986 resop = &res.array[numops - 1]; 3987 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 3988 } 3989 /* 3990 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 3991 * rather than filling it. See the function itself for details. 3992 */ 3993 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 3994 if (garp != NULL) { 3995 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 3996 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 3997 vs_ace4_destroy(&garp->n4g_vsa); 3998 } else { 3999 if (vsap != NULL) { 4000 /* 4001 * The ACL was supposed to be set and to be 4002 * returned in the last getattr of this 4003 * compound, but for some reason the getattr 4004 * result doesn't contain the ACL. In this 4005 * case, purge the ACL cache. 4006 */ 4007 if (rp->r_secattr != NULL) { 4008 mutex_enter(&rp->r_statelock); 4009 vsp = rp->r_secattr; 4010 rp->r_secattr = NULL; 4011 mutex_exit(&rp->r_statelock); 4012 if (vsp != NULL) 4013 nfs4_acl_free_cache(vsp); 4014 } 4015 } 4016 } 4017 } 4018 4019 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4020 /* 4021 * Set the size, rather than relying on getting it updated 4022 * via a GETATTR. With delegations the client tries to 4023 * suppress GETATTR calls. 4024 */ 4025 mutex_enter(&rp->r_statelock); 4026 rp->r_size = vap->va_size; 4027 mutex_exit(&rp->r_statelock); 4028 } 4029 4030 /* 4031 * Can free up request args and res 4032 */ 4033 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4034 opsetattr.obj_attributes); 4035 if (verify_argop != -1) { 4036 nfs4args_verify_free(&argop[verify_argop]); 4037 verify_argop = -1; 4038 } 4039 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4040 4041 /* 4042 * Some servers will change the mode to clear the setuid 4043 * and setgid bits when changing the uid or gid. The 4044 * client needs to compensate appropriately. 4045 */ 4046 if (mask & (AT_UID | AT_GID)) { 4047 int terror, do_setattr; 4048 4049 do_setattr = 0; 4050 va.va_mask = AT_MODE; 4051 terror = nfs4getattr(vp, &va, cr); 4052 if (!terror && 4053 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4054 (!(mask & AT_MODE) && va.va_mode != omode))) { 4055 va.va_mask = AT_MODE; 4056 if (mask & AT_MODE) { 4057 /* 4058 * We asked the mode to be changed and what 4059 * we just got from the server in getattr is 4060 * not what we wanted it to be, so set it now. 4061 */ 4062 va.va_mode = vap->va_mode; 4063 do_setattr = 1; 4064 } else { 4065 /* 4066 * We did not ask the mode to be changed, 4067 * Check to see that the server just cleared 4068 * I_SUID and I_GUID from it. If not then 4069 * set mode to omode with UID/GID cleared. 4070 */ 4071 if (nfs4_compare_modes(va.va_mode, omode)) { 4072 omode &= ~(S_ISUID|S_ISGID); 4073 va.va_mode = omode; 4074 do_setattr = 1; 4075 } 4076 } 4077 4078 if (do_setattr) 4079 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4080 } 4081 } 4082 4083 return (e.error); 4084 } 4085 4086 /* ARGSUSED */ 4087 static int 4088 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr) 4089 { 4090 COMPOUND4args_clnt args; 4091 COMPOUND4res_clnt res; 4092 int doqueue; 4093 uint32_t acc, resacc, argacc; 4094 rnode4_t *rp; 4095 cred_t *cred, *ncr; 4096 nfs4_access_type_t cacc; 4097 int num_ops; 4098 nfs_argop4 argop[3]; 4099 nfs_resop4 *resop; 4100 bool_t needrecov = FALSE, do_getattr; 4101 nfs4_recov_state_t recov_state; 4102 int rpc_error; 4103 hrtime_t t; 4104 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4105 mntinfo4_t *mi = VTOMI4(vp); 4106 4107 if (curproc->p_zone != mi->mi_zone) 4108 return (EIO); 4109 4110 acc = 0; 4111 if (mode & VREAD) 4112 acc |= ACCESS4_READ; 4113 if (mode & VWRITE) { 4114 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4115 return (EROFS); 4116 if (vp->v_type == VDIR) 4117 acc |= ACCESS4_DELETE; 4118 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4119 } 4120 if (mode & VEXEC) { 4121 if (vp->v_type == VDIR) 4122 acc |= ACCESS4_LOOKUP; 4123 else 4124 acc |= ACCESS4_EXECUTE; 4125 } 4126 4127 if (VTOR4(vp)->r_acache != NULL) { 4128 e.error = nfs4_validate_caches(vp, cr); 4129 if (e.error) 4130 return (e.error); 4131 } 4132 4133 rp = VTOR4(vp); 4134 if (vp->v_type == VDIR) { 4135 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4136 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4137 } else { 4138 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4139 ACCESS4_EXECUTE; 4140 } 4141 recov_state.rs_flags = 0; 4142 recov_state.rs_num_retry_despite_err = 0; 4143 4144 cred = cr; 4145 ncr = crnetadjust(cred); 4146 4147 tryagain: 4148 cacc = nfs4_access_check(rp, acc, cred); 4149 if (cacc == NFS4_ACCESS_ALLOWED) 4150 return (0); 4151 if (cacc == NFS4_ACCESS_DENIED) { 4152 /* 4153 * If the cred can be adjusted, try again 4154 * with the new cred. 4155 */ 4156 if (ncr != NULL) { 4157 cred = ncr; 4158 ncr = NULL; 4159 goto tryagain; 4160 } 4161 return (EACCES); 4162 } 4163 4164 recov_retry: 4165 /* 4166 * Don't take with r_statev4_lock here. r_deleg_type could 4167 * change as soon as lock is released. Since it is an int, 4168 * there is no atomicity issue. 4169 */ 4170 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4171 num_ops = do_getattr ? 3 : 2; 4172 4173 args.ctag = TAG_ACCESS; 4174 4175 args.array_len = num_ops; 4176 args.array = argop; 4177 4178 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4179 &recov_state, NULL)) { 4180 return (e.error); 4181 } 4182 4183 /* putfh target fh */ 4184 argop[0].argop = OP_CPUTFH; 4185 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4186 4187 /* access */ 4188 argop[1].argop = OP_ACCESS; 4189 argop[1].nfs_argop4_u.opaccess.access = argacc; 4190 4191 /* getattr */ 4192 if (do_getattr) { 4193 argop[2].argop = OP_GETATTR; 4194 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4195 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4196 } 4197 4198 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4199 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4200 rnode4info(VTOR4(vp)))); 4201 4202 doqueue = 1; 4203 t = gethrtime(); 4204 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4205 rpc_error = e.error; 4206 4207 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4208 if (needrecov) { 4209 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4210 "nfs4_access: initiating recovery\n")); 4211 4212 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4213 NULL, OP_ACCESS, NULL) == FALSE) { 4214 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4215 &recov_state, needrecov); 4216 if (!e.error) 4217 (void) xdr_free(xdr_COMPOUND4res_clnt, 4218 (caddr_t)&res); 4219 goto recov_retry; 4220 } 4221 } 4222 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4223 4224 if (e.error) 4225 goto out; 4226 4227 if (res.status) { 4228 e.error = geterrno4(res.status); 4229 /* 4230 * This might generate over the wire calls throught 4231 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4232 * here to avoid a deadlock. 4233 */ 4234 nfs4_purge_stale_fh(e.error, vp, cr); 4235 goto out; 4236 } 4237 resop = &res.array[1]; /* access res */ 4238 4239 resacc = resop->nfs_resop4_u.opaccess.access; 4240 4241 if (do_getattr) { 4242 resop++; /* getattr res */ 4243 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4244 t, cr, FALSE, NULL); 4245 } 4246 4247 if (!e.error) { 4248 nfs4_access_cache(rp, argacc, resacc, cred); 4249 /* XXX check the supported bits too? */ 4250 if ((acc & resacc) != acc) { 4251 /* 4252 * The following code implements the semantic 4253 * that a setuid root program has *at least* the 4254 * permissions of the user that is running the 4255 * program. See rfs3call() for more portions 4256 * of the implementation of this functionality. 4257 */ 4258 /* XXX-LP */ 4259 if (ncr != NULL) { 4260 (void) xdr_free(xdr_COMPOUND4res_clnt, 4261 (caddr_t)&res); 4262 cred = ncr; 4263 ncr = NULL; 4264 goto tryagain; 4265 } 4266 e.error = EACCES; 4267 } 4268 } 4269 4270 out: 4271 if (!rpc_error) 4272 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4273 4274 if (cred != cr) 4275 crfree(cred); 4276 4277 return (e.error); 4278 } 4279 4280 static int 4281 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr) 4282 { 4283 COMPOUND4args_clnt args; 4284 COMPOUND4res_clnt res; 4285 int doqueue; 4286 rnode4_t *rp; 4287 nfs_argop4 argop[3]; 4288 nfs_resop4 *resop; 4289 READLINK4res *lr_res; 4290 nfs4_ga_res_t *garp; 4291 uint_t len; 4292 char *linkdata; 4293 bool_t needrecov = FALSE; 4294 nfs4_recov_state_t recov_state; 4295 hrtime_t t; 4296 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4297 4298 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 4299 return (EIO); 4300 /* 4301 * Can't readlink anything other than a symbolic link. 4302 */ 4303 if (vp->v_type != VLNK) 4304 return (EINVAL); 4305 4306 rp = VTOR4(vp); 4307 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4308 e.error = nfs4_validate_caches(vp, cr); 4309 if (e.error) 4310 return (e.error); 4311 mutex_enter(&rp->r_statelock); 4312 if (rp->r_symlink.contents != NULL) { 4313 e.error = uiomove(rp->r_symlink.contents, 4314 rp->r_symlink.len, UIO_READ, uiop); 4315 mutex_exit(&rp->r_statelock); 4316 return (e.error); 4317 } 4318 mutex_exit(&rp->r_statelock); 4319 } 4320 recov_state.rs_flags = 0; 4321 recov_state.rs_num_retry_despite_err = 0; 4322 4323 recov_retry: 4324 args.array_len = 3; 4325 args.array = argop; 4326 args.ctag = TAG_READLINK; 4327 4328 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4329 if (e.error) { 4330 return (e.error); 4331 } 4332 4333 /* 0. putfh symlink fh */ 4334 argop[0].argop = OP_CPUTFH; 4335 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4336 4337 /* 1. readlink */ 4338 argop[1].argop = OP_READLINK; 4339 4340 /* 2. getattr */ 4341 argop[2].argop = OP_GETATTR; 4342 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4343 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4344 4345 doqueue = 1; 4346 4347 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4348 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4349 rnode4info(VTOR4(vp)))); 4350 4351 t = gethrtime(); 4352 4353 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4354 4355 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4356 if (needrecov) { 4357 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4358 "nfs4_readlink: initiating recovery\n")); 4359 4360 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4361 NULL, OP_READLINK, NULL) == FALSE) { 4362 if (!e.error) 4363 (void) xdr_free(xdr_COMPOUND4res_clnt, 4364 (caddr_t)&res); 4365 4366 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4367 needrecov); 4368 goto recov_retry; 4369 } 4370 } 4371 4372 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4373 4374 if (e.error) 4375 return (e.error); 4376 4377 /* 4378 * There is an path in the code below which calls 4379 * nfs4_purge_stale_fh(), which may generate otw calls through 4380 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4381 * here to avoid nfs4_start_op() deadlock. 4382 */ 4383 4384 if (res.status && (res.array_len < args.array_len)) { 4385 /* 4386 * either Putfh or Link failed 4387 */ 4388 e.error = geterrno4(res.status); 4389 nfs4_purge_stale_fh(e.error, vp, cr); 4390 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4391 return (e.error); 4392 } 4393 4394 resop = &res.array[1]; /* readlink res */ 4395 lr_res = &resop->nfs_resop4_u.opreadlink; 4396 4397 /* 4398 * treat symlink names as data 4399 */ 4400 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4401 if (linkdata != NULL) { 4402 int uio_len = len - 1; 4403 /* len includes null byte, which we won't uiomove */ 4404 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4405 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4406 mutex_enter(&rp->r_statelock); 4407 if (rp->r_symlink.contents == NULL) { 4408 rp->r_symlink.contents = linkdata; 4409 rp->r_symlink.len = uio_len; 4410 rp->r_symlink.size = len; 4411 mutex_exit(&rp->r_statelock); 4412 } else { 4413 mutex_exit(&rp->r_statelock); 4414 kmem_free(linkdata, len); 4415 } 4416 } else { 4417 kmem_free(linkdata, len); 4418 } 4419 } 4420 if (res.status == NFS4_OK) { 4421 resop++; /* getattr res */ 4422 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4423 } 4424 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4425 4426 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4427 4428 /* 4429 * The over the wire error for attempting to readlink something 4430 * other than a symbolic link is ENXIO. However, we need to 4431 * return EINVAL instead of ENXIO, so we map it here. 4432 */ 4433 return (e.error == ENXIO ? EINVAL : e.error); 4434 } 4435 4436 /* 4437 * Flush local dirty pages to stable storage on the server. 4438 * 4439 * If FNODSYNC is specified, then there is nothing to do because 4440 * metadata changes are not cached on the client before being 4441 * sent to the server. 4442 */ 4443 static int 4444 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr) 4445 { 4446 int error; 4447 4448 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4449 return (0); 4450 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 4451 return (EIO); 4452 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4453 if (!error) 4454 error = VTOR4(vp)->r_error; 4455 return (error); 4456 } 4457 4458 /* 4459 * Weirdness: if the file was removed or the target of a rename 4460 * operation while it was open, it got renamed instead. Here we 4461 * remove the renamed file. 4462 */ 4463 static void 4464 nfs4_inactive(vnode_t *vp, cred_t *cr) 4465 { 4466 rnode4_t *rp; 4467 4468 ASSERT(vp != DNLC_NO_VNODE); 4469 4470 rp = VTOR4(vp); 4471 4472 if (IS_SHADOW(vp, rp)) { 4473 sv_inactive(vp); 4474 return; 4475 } 4476 4477 /* 4478 * If this is coming from the wrong zone, we let someone in the right 4479 * zone take care of it asynchronously. We can get here due to 4480 * VN_RELE() being called from pageout() or fsflush(). This call may 4481 * potentially turn into an expensive no-op if, for instance, v_count 4482 * gets incremented in the meantime, but it's still correct. 4483 */ 4484 if (curproc->p_zone != VTOMI4(vp)->mi_zone) { 4485 nfs4_async_inactive(vp, cr); 4486 return; 4487 } 4488 4489 /* 4490 * Some of the cleanup steps might require over-the-wire 4491 * operations. Since VOP_INACTIVE can get called as a result of 4492 * other over-the-wire operations (e.g., an attribute cache update 4493 * can lead to a DNLC purge), doing those steps now would lead to a 4494 * nested call to the recovery framework, which can deadlock. So 4495 * do any over-the-wire cleanups asynchronously, in a separate 4496 * thread. 4497 */ 4498 4499 mutex_enter(&rp->r_os_lock); 4500 mutex_enter(&rp->r_statelock); 4501 mutex_enter(&rp->r_statev4_lock); 4502 4503 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4504 mutex_exit(&rp->r_statev4_lock); 4505 mutex_exit(&rp->r_statelock); 4506 mutex_exit(&rp->r_os_lock); 4507 nfs4_async_inactive(vp, cr); 4508 return; 4509 } 4510 4511 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4512 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4513 mutex_exit(&rp->r_statev4_lock); 4514 mutex_exit(&rp->r_statelock); 4515 mutex_exit(&rp->r_os_lock); 4516 nfs4_async_inactive(vp, cr); 4517 return; 4518 } 4519 4520 if (rp->r_unldvp != NULL) { 4521 mutex_exit(&rp->r_statev4_lock); 4522 mutex_exit(&rp->r_statelock); 4523 mutex_exit(&rp->r_os_lock); 4524 nfs4_async_inactive(vp, cr); 4525 return; 4526 } 4527 mutex_exit(&rp->r_statev4_lock); 4528 mutex_exit(&rp->r_statelock); 4529 mutex_exit(&rp->r_os_lock); 4530 4531 rp4_addfree(rp, cr); 4532 } 4533 4534 /* 4535 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4536 * various bits of state. The caller must not refer to vp after this call. 4537 */ 4538 4539 void 4540 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4541 { 4542 rnode4_t *rp = VTOR4(vp); 4543 nfs4_recov_state_t recov_state; 4544 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4545 vnode_t *unldvp; 4546 char *unlname; 4547 cred_t *unlcred; 4548 COMPOUND4args_clnt args; 4549 COMPOUND4res_clnt res, *resp; 4550 nfs_argop4 argop[2]; 4551 int doqueue; 4552 #ifdef DEBUG 4553 char *name; 4554 #endif 4555 4556 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 4557 ASSERT(!IS_SHADOW(vp, rp)); 4558 4559 #ifdef DEBUG 4560 name = fn_name(VTOSV(vp)->sv_name); 4561 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4562 "release vnode %s", name)); 4563 kmem_free(name, MAXNAMELEN); 4564 #endif 4565 4566 if (vp->v_type == VREG) { 4567 bool_t recov_failed = FALSE; 4568 4569 e.error = nfs4close_all(vp, cr); 4570 if (e.error) { 4571 /* Check to see if recovery failed */ 4572 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4573 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4574 recov_failed = TRUE; 4575 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4576 if (!recov_failed) { 4577 mutex_enter(&rp->r_statelock); 4578 if (rp->r_flags & R4RECOVERR) 4579 recov_failed = TRUE; 4580 mutex_exit(&rp->r_statelock); 4581 } 4582 if (recov_failed) { 4583 NFS4_DEBUG(nfs4_client_recov_debug, 4584 (CE_NOTE, "nfs4_inactive_otw: " 4585 "close failed (recovery failure)")); 4586 } 4587 } 4588 } 4589 4590 redo: 4591 if (rp->r_unldvp == NULL) { 4592 rp4_addfree(rp, cr); 4593 return; 4594 } 4595 4596 /* 4597 * Save the vnode pointer for the directory where the 4598 * unlinked-open file got renamed, then set it to NULL 4599 * to prevent another thread from getting here before 4600 * we're done with the remove. While we have the 4601 * statelock, make local copies of the pertinent rnode 4602 * fields. If we weren't to do this in an atomic way, the 4603 * the unl* fields could become inconsistent with respect 4604 * to each other due to a race condition between this 4605 * code and nfs_remove(). See bug report 1034328. 4606 */ 4607 mutex_enter(&rp->r_statelock); 4608 if (rp->r_unldvp == NULL) { 4609 mutex_exit(&rp->r_statelock); 4610 rp4_addfree(rp, cr); 4611 return; 4612 } 4613 4614 unldvp = rp->r_unldvp; 4615 rp->r_unldvp = NULL; 4616 unlname = rp->r_unlname; 4617 rp->r_unlname = NULL; 4618 unlcred = rp->r_unlcred; 4619 rp->r_unlcred = NULL; 4620 mutex_exit(&rp->r_statelock); 4621 4622 /* 4623 * If there are any dirty pages left, then flush 4624 * them. This is unfortunate because they just 4625 * may get thrown away during the remove operation, 4626 * but we have to do this for correctness. 4627 */ 4628 if (nfs4_has_pages(vp) && 4629 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4630 ASSERT(vp->v_type != VCHR); 4631 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 4632 if (e.error) { 4633 mutex_enter(&rp->r_statelock); 4634 if (!rp->r_error) 4635 rp->r_error = e.error; 4636 mutex_exit(&rp->r_statelock); 4637 } 4638 } 4639 4640 recov_state.rs_flags = 0; 4641 recov_state.rs_num_retry_despite_err = 0; 4642 recov_retry_remove: 4643 /* 4644 * Do the remove operation on the renamed file 4645 */ 4646 args.ctag = TAG_INACTIVE; 4647 4648 /* 4649 * Remove ops: putfh dir; remove 4650 */ 4651 args.array_len = 2; 4652 args.array = argop; 4653 4654 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4655 if (e.error) { 4656 kmem_free(unlname, MAXNAMELEN); 4657 crfree(unlcred); 4658 VN_RELE(unldvp); 4659 /* 4660 * Try again; this time around r_unldvp will be NULL, so we'll 4661 * just call rp4_addfree() and return. 4662 */ 4663 goto redo; 4664 } 4665 4666 /* putfh directory */ 4667 argop[0].argop = OP_CPUTFH; 4668 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4669 4670 /* remove */ 4671 argop[1].argop = OP_CREMOVE; 4672 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4673 4674 doqueue = 1; 4675 resp = &res; 4676 4677 #if 0 /* notyet */ 4678 /* 4679 * Can't do this yet. We may be being called from 4680 * dnlc_purge_XXX while that routine is holding a 4681 * mutex lock to the nc_rele list. The calls to 4682 * nfs3_cache_wcc_data may result in calls to 4683 * dnlc_purge_XXX. This will result in a deadlock. 4684 */ 4685 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4686 if (e.error) { 4687 PURGE_ATTRCACHE4(unldvp); 4688 resp = NULL; 4689 } else if (res.status) { 4690 e.error = geterrno4(res.status); 4691 PURGE_ATTRCACHE4(unldvp); 4692 /* 4693 * This code is inactive right now 4694 * but if made active there should 4695 * be a nfs4_end_op() call before 4696 * nfs4_purge_stale_fh to avoid start_op() 4697 * deadlock. See BugId: 4948726 4698 */ 4699 nfs4_purge_stale_fh(error, unldvp, cr); 4700 } else { 4701 nfs_resop4 *resop; 4702 REMOVE4res *rm_res; 4703 4704 resop = &res.array[1]; 4705 rm_res = &resop->nfs_resop4_u.opremove; 4706 /* 4707 * Update directory cache attribute, 4708 * readdir and dnlc caches. 4709 */ 4710 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4711 } 4712 #else 4713 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4714 4715 PURGE_ATTRCACHE4(unldvp); 4716 #endif 4717 4718 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4719 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4720 NULL, NULL, OP_REMOVE, NULL) == FALSE) { 4721 if (!e.error) 4722 (void) xdr_free(xdr_COMPOUND4res_clnt, 4723 (caddr_t)&res); 4724 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4725 &recov_state, TRUE); 4726 goto recov_retry_remove; 4727 } 4728 } 4729 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4730 4731 /* 4732 * Release stuff held for the remove 4733 */ 4734 VN_RELE(unldvp); 4735 if (!e.error && resp) 4736 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4737 4738 kmem_free(unlname, MAXNAMELEN); 4739 crfree(unlcred); 4740 goto redo; 4741 } 4742 4743 /* 4744 * Remote file system operations having to do with directory manipulation. 4745 */ 4746 /* ARGSUSED3 */ 4747 static int 4748 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4749 int flags, vnode_t *rdir, cred_t *cr) 4750 { 4751 int error; 4752 vnode_t *vp, *avp = NULL; 4753 rnode4_t *drp; 4754 4755 *vpp = NULL; 4756 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 4757 return (EPERM); 4758 /* 4759 * if LOOKUP_XATTR, must replace dvp (object) with 4760 * object's attrdir before continuing with lookup 4761 */ 4762 if (flags & LOOKUP_XATTR) { 4763 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4764 if (error) 4765 return (error); 4766 4767 dvp = avp; 4768 4769 /* 4770 * If lookup is for "", just return dvp now. The attrdir 4771 * has already been activated (from nfs4lookup_xattr), and 4772 * the caller will RELE the original dvp -- not 4773 * the attrdir. So, set vpp and return. 4774 * Currently, when the LOOKUP_XATTR flag is 4775 * passed to VOP_LOOKUP, the name is always empty, and 4776 * shortcircuiting here avoids 3 unneeded lock/unlock 4777 * pairs. 4778 * 4779 * If a non-empty name was provided, then it is the 4780 * attribute name, and it will be looked up below. 4781 */ 4782 if (*nm == '\0') { 4783 *vpp = dvp; 4784 return (0); 4785 } 4786 4787 /* 4788 * The vfs layer never sends a name when asking for the 4789 * attrdir, so we should never get here (unless of course 4790 * name is passed at some time in future -- at which time 4791 * we'll blow up here). 4792 */ 4793 ASSERT(0); 4794 } 4795 4796 drp = VTOR4(dvp); 4797 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4798 return (EINTR); 4799 4800 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4801 nfs_rw_exit(&drp->r_rwlock); 4802 4803 /* 4804 * If vnode is a device, create special vnode. 4805 */ 4806 if (!error && ISVDEV((*vpp)->v_type)) { 4807 vp = *vpp; 4808 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4809 VN_RELE(vp); 4810 } 4811 4812 return (error); 4813 } 4814 4815 /* ARGSUSED */ 4816 static int 4817 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4818 { 4819 int error; 4820 rnode4_t *drp; 4821 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 4822 mntinfo4_t *mi; 4823 4824 mi = VTOMI4(dvp); 4825 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR)) 4826 return (EINVAL); 4827 4828 drp = VTOR4(dvp); 4829 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4830 return (EINTR); 4831 4832 mutex_enter(&drp->r_statelock); 4833 /* 4834 * If the server doesn't support xattrs just return EINVAL 4835 */ 4836 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 4837 mutex_exit(&drp->r_statelock); 4838 nfs_rw_exit(&drp->r_rwlock); 4839 return (EINVAL); 4840 } 4841 4842 /* 4843 * If there is a cached xattr directory entry, 4844 * use it as long as the attributes are valid. If the 4845 * attributes are not valid, take the simple approach and 4846 * free the cached value and re-fetch a new value. 4847 * 4848 * We don't negative entry cache for now, if we did we 4849 * would need to check if the file has changed on every 4850 * lookup. But xattrs don't exist very often and failing 4851 * an openattr is not much more expensive than and NVERIFY or GETATTR 4852 * so do an openattr over the wire for now. 4853 */ 4854 if (drp->r_xattr_dir != NULL) { 4855 if (ATTRCACHE4_VALID(dvp)) { 4856 VN_HOLD(drp->r_xattr_dir); 4857 *vpp = drp->r_xattr_dir; 4858 mutex_exit(&drp->r_statelock); 4859 nfs_rw_exit(&drp->r_rwlock); 4860 return (0); 4861 } 4862 VN_RELE(drp->r_xattr_dir); 4863 drp->r_xattr_dir = NULL; 4864 } 4865 mutex_exit(&drp->r_statelock); 4866 4867 error = nfs4openattr(dvp, vpp, cflag, cr); 4868 4869 nfs_rw_exit(&drp->r_rwlock); 4870 4871 return (error); 4872 } 4873 4874 static int 4875 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 4876 { 4877 int error; 4878 rnode4_t *drp; 4879 4880 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 4881 4882 /* 4883 * If lookup is for "", just return dvp. Don't need 4884 * to send it over the wire, look it up in the dnlc, 4885 * or perform any access checks. 4886 */ 4887 if (*nm == '\0') { 4888 VN_HOLD(dvp); 4889 *vpp = dvp; 4890 return (0); 4891 } 4892 4893 /* 4894 * Can't do lookups in non-directories. 4895 */ 4896 if (dvp->v_type != VDIR) 4897 return (ENOTDIR); 4898 4899 /* 4900 * If lookup is for ".", just return dvp. Don't need 4901 * to send it over the wire or look it up in the dnlc, 4902 * just need to check access. 4903 */ 4904 if (nm[0] == '.' && nm[1] == '\0') { 4905 error = nfs4_access(dvp, VEXEC, 0, cr); 4906 if (error) 4907 return (error); 4908 VN_HOLD(dvp); 4909 *vpp = dvp; 4910 return (0); 4911 } 4912 4913 drp = VTOR4(dvp); 4914 if (!(drp->r_flags & R4LOOKUP)) { 4915 mutex_enter(&drp->r_statelock); 4916 drp->r_flags |= R4LOOKUP; 4917 mutex_exit(&drp->r_statelock); 4918 } 4919 4920 *vpp = NULL; 4921 /* 4922 * Lookup this name in the DNLC. If there is no entry 4923 * lookup over the wire. 4924 */ 4925 if (!skipdnlc) 4926 *vpp = dnlc_lookup(dvp, nm); 4927 if (*vpp == NULL) { 4928 /* 4929 * We need to go over the wire to lookup the name. 4930 */ 4931 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 4932 } 4933 4934 /* 4935 * We hit on the dnlc 4936 */ 4937 if (*vpp != DNLC_NO_VNODE || 4938 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 4939 /* 4940 * But our attrs may not be valid. 4941 */ 4942 if (ATTRCACHE4_VALID(dvp)) { 4943 error = nfs4_waitfor_purge_complete(dvp); 4944 if (error) { 4945 VN_RELE(*vpp); 4946 *vpp = NULL; 4947 return (error); 4948 } 4949 4950 /* 4951 * If after the purge completes, check to make sure 4952 * our attrs are still valid. 4953 */ 4954 if (ATTRCACHE4_VALID(dvp)) { 4955 /* 4956 * If we waited for a purge we may have 4957 * lost our vnode so look it up again. 4958 */ 4959 VN_RELE(*vpp); 4960 *vpp = dnlc_lookup(dvp, nm); 4961 if (*vpp == NULL) 4962 return (nfs4lookupnew_otw(dvp, 4963 nm, vpp, cr)); 4964 4965 /* 4966 * The access cache should almost always hit 4967 */ 4968 error = nfs4_access(dvp, VEXEC, 0, cr); 4969 4970 if (error) { 4971 VN_RELE(*vpp); 4972 *vpp = NULL; 4973 return (error); 4974 } 4975 if (*vpp == DNLC_NO_VNODE) { 4976 VN_RELE(*vpp); 4977 *vpp = NULL; 4978 return (ENOENT); 4979 } 4980 return (0); 4981 } 4982 } 4983 } 4984 4985 ASSERT(*vpp != NULL); 4986 4987 /* 4988 * We may have gotten here we have one of the following cases: 4989 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 4990 * need to validate them. 4991 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 4992 * must validate. 4993 * 4994 * Go to the server and check if the directory has changed, if 4995 * it hasn't we are done and can use the dnlc entry. 4996 */ 4997 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 4998 } 4999 5000 /* 5001 * Go to the server and check if the directory has changed, if 5002 * it hasn't we are done and can use the dnlc entry. If it 5003 * has changed we get a new copy of its attributes and check 5004 * the access for VEXEC, then relookup the filename and 5005 * get its filehandle and attributes. 5006 * 5007 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5008 * if the NVERIFY failed we must 5009 * purge the caches 5010 * cache new attributes (will set r_time_attr_inval) 5011 * cache new access 5012 * recheck VEXEC access 5013 * add name to dnlc, possibly negative 5014 * if LOOKUP succeeded 5015 * cache new attributes 5016 * else 5017 * set a new r_time_attr_inval for dvp 5018 * check to make sure we have access 5019 * 5020 * The vpp returned is the vnode passed in if the directory is valid, 5021 * a new vnode if successful lookup, or NULL on error. 5022 */ 5023 static int 5024 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5025 { 5026 COMPOUND4args_clnt args; 5027 COMPOUND4res_clnt res; 5028 fattr4 *ver_fattr; 5029 fattr4_change dchange; 5030 int32_t *ptr; 5031 int argoplist_size = 7 * sizeof (nfs_argop4); 5032 nfs_argop4 *argop; 5033 int doqueue; 5034 mntinfo4_t *mi; 5035 nfs4_recov_state_t recov_state; 5036 hrtime_t t; 5037 int isdotdot; 5038 vnode_t *nvp; 5039 nfs_fh4 *fhp; 5040 nfs4_sharedfh_t *sfhp; 5041 nfs4_access_type_t cacc; 5042 rnode4_t *nrp; 5043 rnode4_t *drp = VTOR4(dvp); 5044 nfs4_ga_res_t *garp = NULL; 5045 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5046 5047 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 5048 ASSERT(nm != NULL); 5049 ASSERT(nm[0] != '\0'); 5050 ASSERT(dvp->v_type == VDIR); 5051 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5052 ASSERT(*vpp != NULL); 5053 5054 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5055 isdotdot = 1; 5056 args.ctag = TAG_LOOKUP_VPARENT; 5057 } else { 5058 /* 5059 * Do not allow crossing of server mount points. The 5060 * only visible entries in a SRVSTUB dir are . and .. 5061 * This code handles the non-.. case. We can't even get 5062 * this far if looking up ".". 5063 */ 5064 if (VTOR4(dvp)->r_flags & R4SRVSTUB) { 5065 VN_RELE(*vpp); 5066 *vpp = NULL; 5067 return (ENOENT); 5068 } 5069 isdotdot = 0; 5070 args.ctag = TAG_LOOKUP_VALID; 5071 } 5072 5073 mi = VTOMI4(dvp); 5074 recov_state.rs_flags = 0; 5075 recov_state.rs_num_retry_despite_err = 0; 5076 5077 nvp = NULL; 5078 5079 /* Save the original mount point security information */ 5080 (void) save_mnt_secinfo(mi->mi_curr_serv); 5081 5082 recov_retry: 5083 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5084 &recov_state, NULL); 5085 if (e.error) { 5086 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5087 VN_RELE(*vpp); 5088 *vpp = NULL; 5089 return (e.error); 5090 } 5091 5092 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5093 5094 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5095 args.array_len = 7; 5096 args.array = argop; 5097 5098 /* 0. putfh file */ 5099 argop[0].argop = OP_CPUTFH; 5100 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5101 5102 /* 1. nverify the change info */ 5103 argop[1].argop = OP_NVERIFY; 5104 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5105 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5106 ver_fattr->attrlist4 = (char *)&dchange; 5107 ptr = (int32_t *)&dchange; 5108 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5109 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5110 5111 /* 2. getattr directory */ 5112 argop[2].argop = OP_GETATTR; 5113 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5114 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5115 5116 /* 3. access directory */ 5117 argop[3].argop = OP_ACCESS; 5118 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5119 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5120 5121 /* 4. lookup name */ 5122 if (isdotdot) { 5123 argop[4].argop = OP_LOOKUPP; 5124 } else { 5125 argop[4].argop = OP_CLOOKUP; 5126 argop[4].nfs_argop4_u.opclookup.cname = nm; 5127 } 5128 5129 /* 5. resulting file handle */ 5130 argop[5].argop = OP_GETFH; 5131 5132 /* 6. resulting file attributes */ 5133 argop[6].argop = OP_GETATTR; 5134 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5135 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5136 5137 doqueue = 1; 5138 t = gethrtime(); 5139 5140 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5141 5142 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5143 /* 5144 * For WRONGSEC of a non-dotdot case, send secinfo directly 5145 * from this thread, do not go thru the recovery thread since 5146 * we need the nm information. 5147 * 5148 * Not doing dotdot case because there is no specification 5149 * for (PUTFH, SECINFO "..") yet. 5150 */ 5151 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5152 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5153 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5154 &recov_state, FALSE); 5155 } else { 5156 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5157 &recov_state, TRUE); 5158 } 5159 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5160 kmem_free(argop, argoplist_size); 5161 if (!e.error) 5162 goto recov_retry; 5163 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5164 VN_RELE(*vpp); 5165 *vpp = NULL; 5166 return (e.error); 5167 } 5168 5169 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5170 OP_LOOKUP, NULL) == FALSE) { 5171 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5172 &recov_state, TRUE); 5173 5174 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5175 kmem_free(argop, argoplist_size); 5176 goto recov_retry; 5177 } 5178 } 5179 5180 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5181 5182 if (e.error || res.array_len == 0) { 5183 /* 5184 * If e.error isn't set, then reply has no ops (or we couldn't 5185 * be here). The only legal way to reply without an op array 5186 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5187 * be in the reply for all other status values. 5188 * 5189 * For valid replies without an ops array, return ENOTSUP 5190 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5191 * return EIO -- don't trust status. 5192 */ 5193 if (e.error == 0) 5194 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5195 ENOTSUP : EIO; 5196 VN_RELE(*vpp); 5197 *vpp = NULL; 5198 kmem_free(argop, argoplist_size); 5199 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5200 return (e.error); 5201 } 5202 5203 if (res.status != NFS4ERR_SAME) { 5204 e.error = geterrno4(res.status); 5205 5206 /* 5207 * The NVERIFY "failed" so the directory has changed 5208 * First make sure PUTFH succeeded and NVERIFY "failed" 5209 * cleanly. 5210 */ 5211 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5212 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5213 nfs4_purge_stale_fh(e.error, dvp, cr); 5214 VN_RELE(*vpp); 5215 *vpp = NULL; 5216 goto exit; 5217 } 5218 5219 /* 5220 * We know the NVERIFY "failed" so we must: 5221 * purge the caches (access and indirectly dnlc if needed) 5222 */ 5223 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5224 5225 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5226 nfs4_purge_stale_fh(e.error, dvp, cr); 5227 VN_RELE(*vpp); 5228 *vpp = NULL; 5229 goto exit; 5230 } 5231 5232 /* 5233 * Install new cached attributes for the directory 5234 */ 5235 nfs4_attr_cache(dvp, 5236 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5237 t, cr, FALSE, NULL); 5238 5239 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5240 nfs4_purge_stale_fh(e.error, dvp, cr); 5241 VN_RELE(*vpp); 5242 *vpp = NULL; 5243 e.error = geterrno4(res.status); 5244 goto exit; 5245 } 5246 5247 /* 5248 * Now we know the directory is valid, 5249 * cache new directory access 5250 */ 5251 nfs4_access_cache(drp, 5252 args.array[3].nfs_argop4_u.opaccess.access, 5253 res.array[3].nfs_resop4_u.opaccess.access, cr); 5254 5255 /* 5256 * recheck VEXEC access 5257 */ 5258 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5259 if (cacc != NFS4_ACCESS_ALLOWED) { 5260 /* 5261 * Directory permissions might have been revoked 5262 */ 5263 if (cacc == NFS4_ACCESS_DENIED) { 5264 e.error = EACCES; 5265 VN_RELE(*vpp); 5266 *vpp = NULL; 5267 goto exit; 5268 } 5269 5270 /* 5271 * Somehow we must not have asked for enough 5272 * so try a singleton ACCESS, should never happen. 5273 */ 5274 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5275 if (e.error) { 5276 VN_RELE(*vpp); 5277 *vpp = NULL; 5278 goto exit; 5279 } 5280 } 5281 5282 e.error = geterrno4(res.status); 5283 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5284 /* 5285 * The lookup failed, probably no entry 5286 */ 5287 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5288 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5289 } else { 5290 /* 5291 * Might be some other error, so remove 5292 * the dnlc entry to make sure we start all 5293 * over again, next time. 5294 */ 5295 dnlc_remove(dvp, nm); 5296 } 5297 VN_RELE(*vpp); 5298 *vpp = NULL; 5299 goto exit; 5300 } 5301 5302 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5303 /* 5304 * The file exists but we can't get its fh for 5305 * some unknown reason. Remove it from the dnlc 5306 * and error out to be safe. 5307 */ 5308 dnlc_remove(dvp, nm); 5309 VN_RELE(*vpp); 5310 *vpp = NULL; 5311 goto exit; 5312 } 5313 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5314 if (fhp->nfs_fh4_len == 0) { 5315 /* 5316 * The file exists but a bogus fh 5317 * some unknown reason. Remove it from the dnlc 5318 * and error out to be safe. 5319 */ 5320 e.error = ENOENT; 5321 dnlc_remove(dvp, nm); 5322 VN_RELE(*vpp); 5323 *vpp = NULL; 5324 goto exit; 5325 } 5326 sfhp = sfh4_get(fhp, mi); 5327 5328 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5329 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5330 5331 /* 5332 * Make the new rnode 5333 */ 5334 if (isdotdot) { 5335 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5336 if (e.error) { 5337 sfh4_rele(&sfhp); 5338 VN_RELE(*vpp); 5339 *vpp = NULL; 5340 goto exit; 5341 } 5342 /* 5343 * XXX if nfs4_make_dotdot uses an existing rnode 5344 * XXX it doesn't update the attributes. 5345 * XXX for now just save them again to save an OTW 5346 */ 5347 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5348 } else { 5349 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5350 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5351 /* 5352 * If v_type == VNON, then garp was NULL because 5353 * the last op in the compound failed and makenfs4node 5354 * could not find the vnode for sfhp. It created 5355 * a new vnode, so we have nothing to purge here. 5356 */ 5357 if (nvp->v_type == VNON) { 5358 vattr_t vattr; 5359 5360 vattr.va_mask = AT_TYPE; 5361 /* 5362 * N.B. We've already called nfs4_end_fop above. 5363 */ 5364 e.error = nfs4getattr(nvp, &vattr, cr); 5365 if (e.error) { 5366 sfh4_rele(&sfhp); 5367 VN_RELE(*vpp); 5368 *vpp = NULL; 5369 VN_RELE(nvp); 5370 goto exit; 5371 } 5372 nvp->v_type = vattr.va_type; 5373 } 5374 } 5375 sfh4_rele(&sfhp); 5376 5377 nrp = VTOR4(nvp); 5378 mutex_enter(&nrp->r_statev4_lock); 5379 if (!nrp->created_v4) 5380 dnlc_update(dvp, nm, nvp); 5381 mutex_exit(&nrp->r_statev4_lock); 5382 5383 VN_RELE(*vpp); 5384 *vpp = nvp; 5385 } else { 5386 hrtime_t now; 5387 hrtime_t delta = 0; 5388 5389 e.error = 0; 5390 5391 /* 5392 * Because the NVERIFY "succeeded" we know that the 5393 * directory attributes are still valid 5394 * so update r_time_attr_inval 5395 */ 5396 now = gethrtime(); 5397 mutex_enter(&drp->r_statelock); 5398 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5399 delta = now - drp->r_time_attr_saved; 5400 if (delta < mi->mi_acdirmin) 5401 delta = mi->mi_acdirmin; 5402 else if (delta > mi->mi_acdirmax) 5403 delta = mi->mi_acdirmax; 5404 } 5405 drp->r_time_attr_inval = now + delta; 5406 mutex_exit(&drp->r_statelock); 5407 dnlc_update(dvp, nm, *vpp); 5408 5409 /* 5410 * Even though we have a valid directory attr cache 5411 * and dnlc entry, we may not have access. 5412 * This should almost always hit the cache. 5413 */ 5414 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5415 if (e.error) { 5416 VN_RELE(*vpp); 5417 *vpp = NULL; 5418 } 5419 5420 if (*vpp == DNLC_NO_VNODE) { 5421 VN_RELE(*vpp); 5422 *vpp = NULL; 5423 e.error = ENOENT; 5424 } 5425 } 5426 5427 exit: 5428 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5429 kmem_free(argop, argoplist_size); 5430 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5431 return (e.error); 5432 } 5433 5434 /* 5435 * We need to go over the wire to lookup the name, but 5436 * while we are there verify the directory has not 5437 * changed but if it has, get new attributes and check access 5438 * 5439 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5440 * NVERIFY GETATTR ACCESS 5441 * 5442 * With the results: 5443 * if the NVERIFY failed we must purge the caches, add new attributes, 5444 * and cache new access. 5445 * set a new r_time_attr_inval 5446 * add name to dnlc, possibly negative 5447 * if LOOKUP succeeded 5448 * cache new attributes 5449 */ 5450 static int 5451 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5452 { 5453 COMPOUND4args_clnt args; 5454 COMPOUND4res_clnt res; 5455 fattr4 *ver_fattr; 5456 fattr4_change dchange; 5457 int32_t *ptr; 5458 nfs4_ga_res_t *garp = NULL; 5459 int argoplist_size = 9 * sizeof (nfs_argop4); 5460 nfs_argop4 *argop; 5461 int doqueue; 5462 mntinfo4_t *mi; 5463 nfs4_recov_state_t recov_state; 5464 hrtime_t t; 5465 int isdotdot; 5466 vnode_t *nvp; 5467 nfs_fh4 *fhp; 5468 nfs4_sharedfh_t *sfhp; 5469 nfs4_access_type_t cacc; 5470 rnode4_t *nrp; 5471 rnode4_t *drp = VTOR4(dvp); 5472 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5473 5474 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 5475 ASSERT(nm != NULL); 5476 ASSERT(nm[0] != '\0'); 5477 ASSERT(dvp->v_type == VDIR); 5478 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5479 ASSERT(*vpp == NULL); 5480 5481 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5482 isdotdot = 1; 5483 args.ctag = TAG_LOOKUP_PARENT; 5484 } else { 5485 /* 5486 * Do not allow crossing of server mount points. The 5487 * only visible entries in a SRVSTUB dir are . and .. 5488 * This code handles the non-.. case. We can't even get 5489 * this far if looking up ".". 5490 */ 5491 if (VTOR4(dvp)->r_flags & R4SRVSTUB) 5492 return (ENOENT); 5493 5494 isdotdot = 0; 5495 args.ctag = TAG_LOOKUP; 5496 } 5497 5498 mi = VTOMI4(dvp); 5499 recov_state.rs_flags = 0; 5500 recov_state.rs_num_retry_despite_err = 0; 5501 5502 nvp = NULL; 5503 5504 /* Save the original mount point security information */ 5505 (void) save_mnt_secinfo(mi->mi_curr_serv); 5506 5507 recov_retry: 5508 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5509 &recov_state, NULL); 5510 if (e.error) { 5511 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5512 return (e.error); 5513 } 5514 5515 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5516 5517 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5518 args.array_len = 9; 5519 args.array = argop; 5520 5521 /* 0. putfh file */ 5522 argop[0].argop = OP_CPUTFH; 5523 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5524 5525 /* 1. savefh for the nverify */ 5526 argop[1].argop = OP_SAVEFH; 5527 5528 /* 2. lookup name */ 5529 if (isdotdot) { 5530 argop[2].argop = OP_LOOKUPP; 5531 } else { 5532 argop[2].argop = OP_CLOOKUP; 5533 argop[2].nfs_argop4_u.opclookup.cname = nm; 5534 } 5535 5536 /* 3. resulting file handle */ 5537 argop[3].argop = OP_GETFH; 5538 5539 /* 4. resulting file attributes */ 5540 argop[4].argop = OP_GETATTR; 5541 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5542 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5543 5544 /* 5. restorefh back the directory for the nverify */ 5545 argop[5].argop = OP_RESTOREFH; 5546 5547 /* 6. nverify the change info */ 5548 argop[6].argop = OP_NVERIFY; 5549 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5550 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5551 ver_fattr->attrlist4 = (char *)&dchange; 5552 ptr = (int32_t *)&dchange; 5553 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5554 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5555 5556 /* 7. getattr directory */ 5557 argop[7].argop = OP_GETATTR; 5558 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5559 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5560 5561 /* 8. access directory */ 5562 argop[8].argop = OP_ACCESS; 5563 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5564 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5565 5566 doqueue = 1; 5567 t = gethrtime(); 5568 5569 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5570 5571 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5572 /* 5573 * For WRONGSEC of a non-dotdot case, send secinfo directly 5574 * from this thread, do not go thru the recovery thread since 5575 * we need the nm information. 5576 * 5577 * Not doing dotdot case because there is no specification 5578 * for (PUTFH, SECINFO "..") yet. 5579 */ 5580 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5581 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5582 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5583 &recov_state, FALSE); 5584 } else { 5585 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5586 &recov_state, TRUE); 5587 } 5588 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5589 kmem_free(argop, argoplist_size); 5590 if (!e.error) 5591 goto recov_retry; 5592 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5593 return (e.error); 5594 } 5595 5596 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5597 OP_LOOKUP, NULL) == FALSE) { 5598 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5599 &recov_state, TRUE); 5600 5601 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5602 kmem_free(argop, argoplist_size); 5603 goto recov_retry; 5604 } 5605 } 5606 5607 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5608 5609 if (e.error || res.array_len == 0) { 5610 /* 5611 * If e.error isn't set, then reply has no ops (or we couldn't 5612 * be here). The only legal way to reply without an op array 5613 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5614 * be in the reply for all other status values. 5615 * 5616 * For valid replies without an ops array, return ENOTSUP 5617 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5618 * return EIO -- don't trust status. 5619 */ 5620 if (e.error == 0) 5621 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5622 ENOTSUP : EIO; 5623 5624 kmem_free(argop, argoplist_size); 5625 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5626 return (e.error); 5627 } 5628 5629 e.error = geterrno4(res.status); 5630 5631 /* 5632 * The PUTFH and SAVEFH may have failed. 5633 */ 5634 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5635 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5636 nfs4_purge_stale_fh(e.error, dvp, cr); 5637 goto exit; 5638 } 5639 5640 /* 5641 * Check if the file exists, if it does delay entering 5642 * into the dnlc until after we update the directory 5643 * attributes so we don't cause it to get purged immediately. 5644 */ 5645 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5646 /* 5647 * The lookup failed, probably no entry 5648 */ 5649 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5650 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5651 } 5652 goto exit; 5653 } 5654 5655 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5656 /* 5657 * The file exists but we can't get its fh for 5658 * some unknown reason. Error out to be safe. 5659 */ 5660 goto exit; 5661 } 5662 5663 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5664 if (fhp->nfs_fh4_len == 0) { 5665 /* 5666 * The file exists but a bogus fh 5667 * some unknown reason. Error out to be safe. 5668 */ 5669 e.error = EIO; 5670 goto exit; 5671 } 5672 sfhp = sfh4_get(fhp, mi); 5673 5674 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5675 sfh4_rele(&sfhp); 5676 e.error = EIO; 5677 goto exit; 5678 } 5679 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5680 5681 /* 5682 * The RESTOREFH may have failed 5683 */ 5684 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5685 sfh4_rele(&sfhp); 5686 e.error = EIO; 5687 goto exit; 5688 } 5689 5690 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5691 /* 5692 * First make sure the NVERIFY failed as we expected, 5693 * if it didn't then be conservative and error out 5694 * as we can't trust the directory. 5695 */ 5696 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5697 sfh4_rele(&sfhp); 5698 e.error = EIO; 5699 goto exit; 5700 } 5701 5702 /* 5703 * We know the NVERIFY "failed" so the directory has changed, 5704 * so we must: 5705 * purge the caches (access and indirectly dnlc if needed) 5706 */ 5707 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5708 5709 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5710 sfh4_rele(&sfhp); 5711 goto exit; 5712 } 5713 nfs4_attr_cache(dvp, 5714 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5715 t, cr, FALSE, NULL); 5716 5717 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5718 nfs4_purge_stale_fh(e.error, dvp, cr); 5719 sfh4_rele(&sfhp); 5720 e.error = geterrno4(res.status); 5721 goto exit; 5722 } 5723 5724 /* 5725 * Now we know the directory is valid, 5726 * cache new directory access 5727 */ 5728 nfs4_access_cache(drp, 5729 args.array[8].nfs_argop4_u.opaccess.access, 5730 res.array[8].nfs_resop4_u.opaccess.access, cr); 5731 5732 /* 5733 * recheck VEXEC access 5734 */ 5735 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5736 if (cacc != NFS4_ACCESS_ALLOWED) { 5737 /* 5738 * Directory permissions might have been revoked 5739 */ 5740 if (cacc == NFS4_ACCESS_DENIED) { 5741 sfh4_rele(&sfhp); 5742 e.error = EACCES; 5743 goto exit; 5744 } 5745 5746 /* 5747 * Somehow we must not have asked for enough 5748 * so try a singleton ACCESS should never happen 5749 */ 5750 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5751 if (e.error) { 5752 sfh4_rele(&sfhp); 5753 goto exit; 5754 } 5755 } 5756 5757 e.error = geterrno4(res.status); 5758 } else { 5759 hrtime_t now; 5760 hrtime_t delta = 0; 5761 5762 e.error = 0; 5763 5764 /* 5765 * Because the NVERIFY "succeeded" we know that the 5766 * directory attributes are still valid 5767 * so update r_time_attr_inval 5768 */ 5769 now = gethrtime(); 5770 mutex_enter(&drp->r_statelock); 5771 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5772 delta = now - drp->r_time_attr_saved; 5773 if (delta < mi->mi_acdirmin) 5774 delta = mi->mi_acdirmin; 5775 else if (delta > mi->mi_acdirmax) 5776 delta = mi->mi_acdirmax; 5777 } 5778 drp->r_time_attr_inval = now + delta; 5779 mutex_exit(&drp->r_statelock); 5780 5781 /* 5782 * Even though we have a valid directory attr cache, 5783 * we may not have access. 5784 * This should almost always hit the cache. 5785 */ 5786 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5787 if (e.error) { 5788 sfh4_rele(&sfhp); 5789 goto exit; 5790 } 5791 } 5792 5793 /* 5794 * Now we have successfully completed the lookup, if the 5795 * directory has changed we now have the valid attributes. 5796 * We also know we have directory access. 5797 * Create the new rnode and insert it in the dnlc. 5798 */ 5799 if (isdotdot) { 5800 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5801 if (e.error) { 5802 sfh4_rele(&sfhp); 5803 goto exit; 5804 } 5805 /* 5806 * XXX if nfs4_make_dotdot uses an existing rnode 5807 * XXX it doesn't update the attributes. 5808 * XXX for now just save them again to save an OTW 5809 */ 5810 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5811 } else { 5812 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5813 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5814 } 5815 sfh4_rele(&sfhp); 5816 5817 nrp = VTOR4(nvp); 5818 mutex_enter(&nrp->r_statev4_lock); 5819 if (!nrp->created_v4) 5820 dnlc_update(dvp, nm, nvp); 5821 mutex_exit(&nrp->r_statev4_lock); 5822 5823 *vpp = nvp; 5824 5825 exit: 5826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5827 kmem_free(argop, argoplist_size); 5828 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5829 return (e.error); 5830 } 5831 5832 #ifdef DEBUG 5833 void 5834 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 5835 { 5836 uint_t i, len; 5837 zoneid_t zoneid = getzoneid(); 5838 char *s; 5839 5840 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 5841 for (i = 0; i < argcnt; i++) { 5842 nfs_argop4 *op = &argbase[i]; 5843 switch (op->argop) { 5844 case OP_CPUTFH: 5845 case OP_PUTFH: 5846 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 5847 break; 5848 case OP_PUTROOTFH: 5849 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 5850 break; 5851 case OP_CLOOKUP: 5852 s = op->nfs_argop4_u.opclookup.cname; 5853 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5854 break; 5855 case OP_LOOKUP: 5856 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 5857 &len, NULL); 5858 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5859 kmem_free(s, len); 5860 break; 5861 case OP_LOOKUPP: 5862 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 5863 break; 5864 case OP_GETFH: 5865 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 5866 break; 5867 case OP_GETATTR: 5868 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 5869 break; 5870 case OP_OPENATTR: 5871 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 5872 break; 5873 default: 5874 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 5875 op->argop); 5876 break; 5877 } 5878 } 5879 } 5880 #endif 5881 5882 /* 5883 * nfs4lookup_setup - constructs a multi-lookup compound request. 5884 * 5885 * Given the path "nm1/nm2/.../nmn", the following compound requests 5886 * may be created: 5887 * 5888 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 5889 * is faster, for now. 5890 * 5891 * l4_getattrs indicates the type of compound requested. 5892 * 5893 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 5894 * 5895 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 5896 * 5897 * total number of ops is n + 1. 5898 * 5899 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 5900 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 5901 * before the last component, and only get attributes 5902 * for the last component. Note that the second-to-last 5903 * pathname component is XATTR_RPATH, which does NOT go 5904 * over-the-wire as a lookup. 5905 * 5906 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 5907 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 5908 * 5909 * and total number of ops is n + 5. 5910 * 5911 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 5912 * attribute directory: create lookups plus an OPENATTR 5913 * replacing the last lookup. Note that the last pathname 5914 * component is XATTR_RPATH, which does NOT go over-the-wire 5915 * as a lookup. 5916 * 5917 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 5918 * Openattr; Getfh; Getattr } 5919 * 5920 * and total number of ops is n + 5. 5921 * 5922 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 5923 * nodes too. 5924 * 5925 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 5926 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 5927 * 5928 * and total number of ops is 3*n + 1. 5929 * 5930 * All cases: returns the index in the arg array of the final LOOKUP op, or 5931 * -1 if no LOOKUPs were used. 5932 */ 5933 int 5934 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 5935 { 5936 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 5937 nfs_argop4 *argbase, *argop; 5938 int arglen, argcnt; 5939 int n = 1; /* number of components */ 5940 int nga = 1; /* number of Getattr's in request */ 5941 char c = '\0', *s, *p; 5942 int lookup_idx = -1; 5943 int argoplist_size; 5944 5945 /* set lookuparg response result to 0 */ 5946 lookupargp->resp->status = NFS4_OK; 5947 5948 /* skip leading "/" or "." e.g. ".//./" if there is */ 5949 for (; ; nm++) { 5950 if (*nm != '/' && *nm != '.') 5951 break; 5952 5953 /* ".." is counted as 1 component */ 5954 if (*nm == '.' && *(nm + 1) == '.') 5955 break; 5956 } 5957 5958 /* 5959 * Find n = number of components - nm must be null terminated 5960 * Skip "." components. 5961 */ 5962 if (*nm != '\0') { 5963 for (n = 1, s = nm; *s != '\0'; s++) { 5964 if ((*s == '/') && (*(s + 1) != '/') && 5965 (*(s + 1) != '\0') && 5966 !(*(s + 1) == '.' && (*(s + 2) == '/' || 5967 *(s + 2) == '\0'))) 5968 n++; 5969 } 5970 } else 5971 n = 0; 5972 5973 /* 5974 * nga is number of components that need Getfh+Getattr 5975 */ 5976 switch (l4_getattrs) { 5977 case LKP4_NO_ATTRIBUTES: 5978 nga = 0; 5979 break; 5980 case LKP4_ALL_ATTRIBUTES: 5981 nga = n; 5982 /* 5983 * Always have at least 1 getfh, getattr pair 5984 */ 5985 if (nga == 0) 5986 nga++; 5987 break; 5988 case LKP4_LAST_ATTRDIR: 5989 case LKP4_LAST_NAMED_ATTR: 5990 nga = n+1; 5991 break; 5992 } 5993 5994 /* 5995 * If change to use the filehandle attr instead of getfh 5996 * the following line can be deleted. 5997 */ 5998 nga *= 2; 5999 6000 /* 6001 * calculate number of ops in request as 6002 * header + trailer + lookups + getattrs 6003 */ 6004 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6005 6006 argoplist_size = arglen * sizeof (nfs_argop4); 6007 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6008 lookupargp->argsp->array = argop; 6009 6010 argcnt = lookupargp->header_len; 6011 argop += argcnt; 6012 6013 /* 6014 * loop and create a lookup op and possibly getattr/getfh for 6015 * each component. Skip "." components. 6016 */ 6017 for (s = nm; *s != '\0'; s = p) { 6018 /* 6019 * Set up a pathname struct for each component if needed 6020 */ 6021 while (*s == '/') 6022 s++; 6023 if (*s == '\0') 6024 break; 6025 for (p = s; (*p != '/') && (*p != '\0'); p++); 6026 c = *p; 6027 *p = '\0'; 6028 6029 if (s[0] == '.' && s[1] == '\0') { 6030 *p = c; 6031 continue; 6032 } 6033 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6034 strcmp(s, XATTR_RPATH) == 0) { 6035 /* getfh XXX may not be needed in future */ 6036 argop->argop = OP_GETFH; 6037 argop++; 6038 argcnt++; 6039 6040 /* getattr */ 6041 argop->argop = OP_GETATTR; 6042 argop->nfs_argop4_u.opgetattr.attr_request = 6043 lookupargp->ga_bits; 6044 argop->nfs_argop4_u.opgetattr.mi = 6045 lookupargp->mi; 6046 argop++; 6047 argcnt++; 6048 6049 /* openattr */ 6050 argop->argop = OP_OPENATTR; 6051 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6052 strcmp(s, XATTR_RPATH) == 0) { 6053 /* openattr */ 6054 argop->argop = OP_OPENATTR; 6055 argop++; 6056 argcnt++; 6057 6058 /* getfh XXX may not be needed in future */ 6059 argop->argop = OP_GETFH; 6060 argop++; 6061 argcnt++; 6062 6063 /* getattr */ 6064 argop->argop = OP_GETATTR; 6065 argop->nfs_argop4_u.opgetattr.attr_request = 6066 lookupargp->ga_bits; 6067 argop->nfs_argop4_u.opgetattr.mi = 6068 lookupargp->mi; 6069 argop++; 6070 argcnt++; 6071 *p = c; 6072 continue; 6073 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6074 /* lookupp */ 6075 argop->argop = OP_LOOKUPP; 6076 } else { 6077 /* lookup */ 6078 argop->argop = OP_LOOKUP; 6079 (void) str_to_utf8(s, 6080 &argop->nfs_argop4_u.oplookup.objname); 6081 } 6082 lookup_idx = argcnt; 6083 argop++; 6084 argcnt++; 6085 6086 *p = c; 6087 6088 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6089 /* getfh XXX may not be needed in future */ 6090 argop->argop = OP_GETFH; 6091 argop++; 6092 argcnt++; 6093 6094 /* getattr */ 6095 argop->argop = OP_GETATTR; 6096 argop->nfs_argop4_u.opgetattr.attr_request = 6097 lookupargp->ga_bits; 6098 argop->nfs_argop4_u.opgetattr.mi = 6099 lookupargp->mi; 6100 argop++; 6101 argcnt++; 6102 } 6103 } 6104 6105 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6106 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6107 if (needgetfh) { 6108 /* stick in a post-lookup getfh */ 6109 argop->argop = OP_GETFH; 6110 argcnt++; 6111 argop++; 6112 } 6113 /* post-lookup getattr */ 6114 argop->argop = OP_GETATTR; 6115 argop->nfs_argop4_u.opgetattr.attr_request = 6116 lookupargp->ga_bits; 6117 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6118 argcnt++; 6119 } 6120 argcnt += lookupargp->trailer_len; /* actual op count */ 6121 lookupargp->argsp->array_len = argcnt; 6122 lookupargp->arglen = arglen; 6123 6124 #ifdef DEBUG 6125 if (nfs4_client_lookup_debug) 6126 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6127 #endif 6128 6129 return (lookup_idx); 6130 } 6131 6132 static int 6133 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6134 { 6135 COMPOUND4args_clnt args; 6136 COMPOUND4res_clnt res; 6137 GETFH4res *gf_res = NULL; 6138 nfs_argop4 argop[4]; 6139 nfs_resop4 *resop = NULL; 6140 nfs4_sharedfh_t *sfhp; 6141 hrtime_t t; 6142 nfs4_error_t e; 6143 6144 rnode4_t *drp; 6145 int doqueue = 1; 6146 vnode_t *vp; 6147 int needrecov = 0; 6148 nfs4_recov_state_t recov_state; 6149 6150 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 6151 6152 *avp = NULL; 6153 recov_state.rs_flags = 0; 6154 recov_state.rs_num_retry_despite_err = 0; 6155 6156 recov_retry: 6157 /* COMPOUND: putfh, openattr, getfh, getattr */ 6158 args.array_len = 4; 6159 args.array = argop; 6160 args.ctag = TAG_OPENATTR; 6161 6162 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6163 if (e.error) 6164 return (e.error); 6165 6166 drp = VTOR4(dvp); 6167 6168 /* putfh */ 6169 argop[0].argop = OP_CPUTFH; 6170 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6171 6172 /* openattr */ 6173 argop[1].argop = OP_OPENATTR; 6174 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6175 6176 /* getfh */ 6177 argop[2].argop = OP_GETFH; 6178 6179 /* getattr */ 6180 argop[3].argop = OP_GETATTR; 6181 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6182 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6183 6184 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6185 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6186 rnode4info(drp))); 6187 6188 t = gethrtime(); 6189 6190 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6191 6192 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6193 if (needrecov) { 6194 bool_t abort; 6195 6196 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6197 "nfs4openattr: initiating recovery\n")); 6198 6199 abort = nfs4_start_recovery(&e, 6200 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6201 OP_OPENATTR, NULL); 6202 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6203 if (!e.error) { 6204 e.error = geterrno4(res.status); 6205 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6206 } 6207 if (abort == FALSE) 6208 goto recov_retry; 6209 return (e.error); 6210 } 6211 6212 if (e.error) { 6213 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6214 return (e.error); 6215 } 6216 6217 if (res.status) { 6218 /* 6219 * If OTW errro is NOTSUPP, then it should be 6220 * translated to EINVAL. All Solaris file system 6221 * implementations return EINVAL to the syscall layer 6222 * when the attrdir cannot be created due to an 6223 * implementation restriction or noxattr mount option. 6224 */ 6225 if (res.status == NFS4ERR_NOTSUPP) { 6226 mutex_enter(&drp->r_statelock); 6227 if (drp->r_xattr_dir) 6228 VN_RELE(drp->r_xattr_dir); 6229 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6230 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6231 mutex_exit(&drp->r_statelock); 6232 6233 e.error = EINVAL; 6234 } else { 6235 e.error = geterrno4(res.status); 6236 } 6237 6238 if (e.error) { 6239 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6240 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6241 needrecov); 6242 return (e.error); 6243 } 6244 } 6245 6246 resop = &res.array[0]; /* putfh res */ 6247 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6248 6249 resop = &res.array[1]; /* openattr res */ 6250 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6251 6252 resop = &res.array[2]; /* getfh res */ 6253 gf_res = &resop->nfs_resop4_u.opgetfh; 6254 if (gf_res->object.nfs_fh4_len == 0) { 6255 *avp = NULL; 6256 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6257 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6258 return (ENOENT); 6259 } 6260 6261 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6262 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6263 dvp->v_vfsp, t, cr, dvp, 6264 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH)); 6265 sfh4_rele(&sfhp); 6266 6267 if (e.error) 6268 PURGE_ATTRCACHE4(vp); 6269 6270 mutex_enter(&vp->v_lock); 6271 vp->v_flag |= V_XATTRDIR; 6272 mutex_exit(&vp->v_lock); 6273 6274 *avp = vp; 6275 6276 mutex_enter(&drp->r_statelock); 6277 if (drp->r_xattr_dir) 6278 VN_RELE(drp->r_xattr_dir); 6279 VN_HOLD(vp); 6280 drp->r_xattr_dir = vp; 6281 6282 /* 6283 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6284 * NULL. xattrs could be created at any time, and we have no 6285 * way to update pc4_xattr_exists in the base object if/when 6286 * it happens. 6287 */ 6288 drp->r_pathconf.pc4_xattr_valid = 0; 6289 6290 mutex_exit(&drp->r_statelock); 6291 6292 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6293 6294 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6295 6296 return (0); 6297 } 6298 6299 /* ARGSUSED */ 6300 static int 6301 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6302 int mode, vnode_t **vpp, cred_t *cr, int flags) 6303 { 6304 int error; 6305 vnode_t *vp = NULL; 6306 rnode4_t *rp; 6307 struct vattr vattr; 6308 rnode4_t *drp; 6309 vnode_t *tempvp; 6310 enum createmode4 createmode; 6311 bool_t must_trunc = FALSE; 6312 6313 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 6314 return (EPERM); 6315 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6316 return (EINVAL); 6317 } 6318 6319 /* . and .. have special meaning in the protocol, reject them. */ 6320 6321 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6322 return (EISDIR); 6323 6324 drp = VTOR4(dvp); 6325 6326 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6327 return (EINTR); 6328 6329 top: 6330 /* 6331 * We make a copy of the attributes because the caller does not 6332 * expect us to change what va points to. 6333 */ 6334 vattr = *va; 6335 6336 /* 6337 * If the pathname is "", then dvp is the root vnode of 6338 * a remote file mounted over a local directory. 6339 * All that needs to be done is access 6340 * checking and truncation. Note that we avoid doing 6341 * open w/ create because the parent directory might 6342 * be in pseudo-fs and the open would fail. 6343 */ 6344 if (*nm == '\0') { 6345 error = 0; 6346 VN_HOLD(dvp); 6347 vp = dvp; 6348 must_trunc = TRUE; 6349 } else { 6350 /* 6351 * We need to go over the wire, just to be sure whether the 6352 * file exists or not. Using the DNLC can be dangerous in 6353 * this case when making a decision regarding existence. 6354 */ 6355 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6356 } 6357 6358 if (exclusive) 6359 createmode = EXCLUSIVE4; 6360 else 6361 createmode = GUARDED4; 6362 6363 /* 6364 * error would be set if the file does not exist on the 6365 * server, so lets go create it. 6366 */ 6367 if (error) { 6368 goto create_otw; 6369 } 6370 6371 /* 6372 * File does exist on the server 6373 */ 6374 if (exclusive == EXCL) 6375 error = EEXIST; 6376 else if (vp->v_type == VDIR && (mode & VWRITE)) 6377 error = EISDIR; 6378 else { 6379 /* 6380 * If vnode is a device, create special vnode. 6381 */ 6382 if (ISVDEV(vp->v_type)) { 6383 tempvp = vp; 6384 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6385 VN_RELE(tempvp); 6386 } 6387 if (!(error = VOP_ACCESS(vp, mode, 0, cr))) { 6388 if ((vattr.va_mask & AT_SIZE) && 6389 vp->v_type == VREG) { 6390 rp = VTOR4(vp); 6391 /* 6392 * Check here for large file handled 6393 * by LF-unaware process (as 6394 * ufs_create() does) 6395 */ 6396 if (!(flags & FOFFMAX)) { 6397 mutex_enter(&rp->r_statelock); 6398 if (rp->r_size > MAXOFF32_T) 6399 error = EOVERFLOW; 6400 mutex_exit(&rp->r_statelock); 6401 } 6402 6403 /* if error is set then we need to return */ 6404 if (error) { 6405 nfs_rw_exit(&drp->r_rwlock); 6406 VN_RELE(vp); 6407 return (error); 6408 } 6409 6410 if (must_trunc) { 6411 vattr.va_mask = AT_SIZE; 6412 error = nfs4setattr(vp, &vattr, 0, cr, 6413 NULL); 6414 } else { 6415 /* 6416 * we know we have a regular file that already 6417 * exists and we may end up truncating the file 6418 * as a result of the open_otw, so flush out 6419 * any dirty pages for this file first. 6420 */ 6421 if (nfs4_has_pages(vp) && 6422 ((rp->r_flags & R4DIRTY) || 6423 rp->r_count > 0 || 6424 rp->r_mapcnt > 0)) { 6425 error = nfs4_putpage(vp, 6426 (offset_t)0, 0, 0, cr); 6427 if (error && (error == ENOSPC || 6428 error == EDQUOT)) { 6429 mutex_enter( 6430 &rp->r_statelock); 6431 if (!rp->r_error) 6432 rp->r_error = 6433 error; 6434 mutex_exit( 6435 &rp->r_statelock); 6436 } 6437 } 6438 vattr.va_mask = (AT_SIZE | 6439 AT_TYPE | AT_MODE); 6440 vattr.va_type = VREG; 6441 createmode = UNCHECKED4; 6442 goto create_otw; 6443 } 6444 } 6445 } 6446 } 6447 nfs_rw_exit(&drp->r_rwlock); 6448 if (error) { 6449 VN_RELE(vp); 6450 } else { 6451 *vpp = vp; 6452 } 6453 return (error); 6454 6455 create_otw: 6456 dnlc_remove(dvp, nm); 6457 6458 ASSERT(vattr.va_mask & AT_TYPE); 6459 6460 /* 6461 * If not a regular file let nfs4mknod() handle it. 6462 */ 6463 if (vattr.va_type != VREG) { 6464 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6465 nfs_rw_exit(&drp->r_rwlock); 6466 return (error); 6467 } 6468 6469 /* 6470 * It _is_ a regular file. 6471 */ 6472 ASSERT(vattr.va_mask & AT_MODE); 6473 if (MANDMODE(vattr.va_mode)) { 6474 nfs_rw_exit(&drp->r_rwlock); 6475 return (EACCES); 6476 } 6477 6478 /* 6479 * If this happens to be a mknod of a regular file, then flags will 6480 * have neither FREAD or FWRITE. However, we must set at least one 6481 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6482 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6483 * set (based on openmode specified by app). 6484 */ 6485 if ((flags & (FREAD|FWRITE)) == 0) 6486 flags |= (FREAD|FWRITE); 6487 6488 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6489 6490 if (vp != NULL) { 6491 /* if create was successful, throw away the file's pages */ 6492 if (!error && (vattr.va_mask & AT_SIZE)) 6493 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6494 cr); 6495 /* release the lookup hold */ 6496 VN_RELE(vp); 6497 vp = NULL; 6498 } 6499 6500 /* 6501 * validate that we opened a regular file. This handles a misbehaving 6502 * server that returns an incorrect FH. 6503 */ 6504 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6505 error = EISDIR; 6506 VN_RELE(*vpp); 6507 } 6508 6509 /* 6510 * If this is not an exclusive create, then the CREATE 6511 * request will be made with the GUARDED mode set. This 6512 * means that the server will return EEXIST if the file 6513 * exists. The file could exist because of a retransmitted 6514 * request. In this case, we recover by starting over and 6515 * checking to see whether the file exists. This second 6516 * time through it should and a CREATE request will not be 6517 * sent. 6518 * 6519 * This handles the problem of a dangling CREATE request 6520 * which contains attributes which indicate that the file 6521 * should be truncated. This retransmitted request could 6522 * possibly truncate valid data in the file if not caught 6523 * by the duplicate request mechanism on the server or if 6524 * not caught by other means. The scenario is: 6525 * 6526 * Client transmits CREATE request with size = 0 6527 * Client times out, retransmits request. 6528 * Response to the first request arrives from the server 6529 * and the client proceeds on. 6530 * Client writes data to the file. 6531 * The server now processes retransmitted CREATE request 6532 * and truncates file. 6533 * 6534 * The use of the GUARDED CREATE request prevents this from 6535 * happening because the retransmitted CREATE would fail 6536 * with EEXIST and would not truncate the file. 6537 */ 6538 if (error == EEXIST && exclusive == NONEXCL) { 6539 #ifdef DEBUG 6540 nfs4_create_misses++; 6541 #endif 6542 goto top; 6543 } 6544 nfs_rw_exit(&drp->r_rwlock); 6545 return (error); 6546 } 6547 6548 /* 6549 * Create compound (for mkdir, mknod, symlink): 6550 * { Putfh <dfh>; Create; Getfh; Getattr } 6551 * It's okay if setattr failed to set gid - this is not considered 6552 * an error, but purge attrs in that case. 6553 */ 6554 static int 6555 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6556 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6557 { 6558 int need_end_op = FALSE; 6559 COMPOUND4args_clnt args; 6560 COMPOUND4res_clnt res, *resp = NULL; 6561 nfs_argop4 *argop; 6562 nfs_resop4 *resop; 6563 int doqueue; 6564 mntinfo4_t *mi; 6565 rnode4_t *drp = VTOR4(dvp); 6566 change_info4 *cinfo; 6567 GETFH4res *gf_res; 6568 struct vattr vattr; 6569 vnode_t *vp; 6570 fattr4 *crattr; 6571 bool_t needrecov = FALSE; 6572 nfs4_recov_state_t recov_state; 6573 nfs4_sharedfh_t *sfhp = NULL; 6574 hrtime_t t; 6575 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6576 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6577 dirattr_info_t dinfo, *dinfop; 6578 servinfo4_t *svp; 6579 bitmap4 supp_attrs; 6580 6581 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6582 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6583 6584 mi = VTOMI4(dvp); 6585 6586 /* 6587 * Make sure we properly deal with setting the right gid 6588 * on a new directory to reflect the parent's setgid bit 6589 */ 6590 setgid_flag = 0; 6591 if (type == NF4DIR) { 6592 struct vattr dva; 6593 6594 va->va_mode &= ~VSGID; 6595 dva.va_mask = AT_MODE | AT_GID; 6596 if (VOP_GETATTR(dvp, &dva, 0, cr) == 0) { 6597 6598 /* 6599 * If the parent's directory has the setgid bit set 6600 * _and_ the client was able to get a valid mapping 6601 * for the parent dir's owner_group, we want to 6602 * append NVERIFY(owner_group == dva.va_gid) and 6603 * SETTATTR to the CREATE compound. 6604 */ 6605 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6606 setgid_flag = 1; 6607 va->va_mode |= VSGID; 6608 if (dva.va_gid != GID_NOBODY) { 6609 va->va_mask |= AT_GID; 6610 va->va_gid = dva.va_gid; 6611 } 6612 } 6613 } 6614 } 6615 6616 /* 6617 * Create ops: 6618 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6619 * 5:restorefh(dir) 6:getattr(dir) 6620 * 6621 * if (setgid) 6622 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6623 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6624 * 8:nverify 9:setattr 6625 */ 6626 if (setgid_flag) { 6627 numops = 10; 6628 idx_create = 1; 6629 idx_fattr = 3; 6630 } else { 6631 numops = 7; 6632 idx_create = 2; 6633 idx_fattr = 4; 6634 } 6635 6636 ASSERT(curproc->p_zone == mi->mi_zone); 6637 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6638 return (EINTR); 6639 } 6640 recov_state.rs_flags = 0; 6641 recov_state.rs_num_retry_despite_err = 0; 6642 6643 argoplist_size = numops * sizeof (nfs_argop4); 6644 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6645 6646 recov_retry: 6647 if (type == NF4LNK) 6648 args.ctag = TAG_SYMLINK; 6649 else if (type == NF4DIR) 6650 args.ctag = TAG_MKDIR; 6651 else 6652 args.ctag = TAG_MKNOD; 6653 6654 args.array_len = numops; 6655 args.array = argop; 6656 6657 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6658 nfs_rw_exit(&drp->r_rwlock); 6659 kmem_free(argop, argoplist_size); 6660 return (e.error); 6661 } 6662 need_end_op = TRUE; 6663 6664 6665 /* 0: putfh directory */ 6666 argop[0].argop = OP_CPUTFH; 6667 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6668 6669 /* 1/2: Create object */ 6670 argop[idx_create].argop = OP_CCREATE; 6671 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6672 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6673 if (type == NF4LNK) { 6674 /* 6675 * symlink, treat name as data 6676 */ 6677 ASSERT(data != NULL); 6678 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6679 (char *)data; 6680 } 6681 if (type == NF4BLK || type == NF4CHR) { 6682 ASSERT(data != NULL); 6683 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6684 *((specdata4 *)data); 6685 } 6686 6687 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6688 6689 svp = drp->r_server; 6690 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6691 supp_attrs = svp->sv_supp_attrs; 6692 nfs_rw_exit(&svp->sv_lock); 6693 6694 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6695 nfs_rw_exit(&drp->r_rwlock); 6696 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6697 e.error = EINVAL; 6698 kmem_free(argop, argoplist_size); 6699 return (e.error); 6700 } 6701 6702 /* 2/3: getfh fh of created object */ 6703 ASSERT(idx_create + 1 == idx_fattr - 1); 6704 argop[idx_create + 1].argop = OP_GETFH; 6705 6706 /* 3/4: getattr of new object */ 6707 argop[idx_fattr].argop = OP_GETATTR; 6708 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6709 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6710 6711 if (setgid_flag) { 6712 vattr_t _v; 6713 6714 argop[4].argop = OP_SAVEFH; 6715 6716 argop[5].argop = OP_CPUTFH; 6717 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6718 6719 argop[6].argop = OP_GETATTR; 6720 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6721 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6722 6723 argop[7].argop = OP_RESTOREFH; 6724 6725 /* 6726 * nverify 6727 * 6728 * XXX - Revisit the last argument to nfs4_end_op() 6729 * once 5020486 is fixed. 6730 */ 6731 _v.va_mask = AT_GID; 6732 _v.va_gid = va->va_gid; 6733 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6734 supp_attrs)) { 6735 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6736 nfs_rw_exit(&drp->r_rwlock); 6737 nfs4_fattr4_free(crattr); 6738 kmem_free(argop, argoplist_size); 6739 return (e.error); 6740 } 6741 6742 /* 6743 * setattr 6744 * 6745 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6746 * so no need for stateid or flags. Also we specify NULL 6747 * rp since we're only interested in setting owner_group 6748 * attributes. 6749 */ 6750 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6751 &e.error, 0); 6752 6753 if (e.error) { 6754 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6755 nfs_rw_exit(&drp->r_rwlock); 6756 nfs4_fattr4_free(crattr); 6757 nfs4args_verify_free(&argop[8]); 6758 kmem_free(argop, argoplist_size); 6759 return (e.error); 6760 } 6761 } else { 6762 argop[1].argop = OP_SAVEFH; 6763 6764 argop[5].argop = OP_RESTOREFH; 6765 6766 argop[6].argop = OP_GETATTR; 6767 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6768 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6769 } 6770 6771 dnlc_remove(dvp, nm); 6772 6773 doqueue = 1; 6774 t = gethrtime(); 6775 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 6776 6777 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 6778 if (e.error) { 6779 PURGE_ATTRCACHE4(dvp); 6780 if (!needrecov) 6781 goto out; 6782 } 6783 6784 if (needrecov) { 6785 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 6786 OP_CREATE, NULL) == FALSE) { 6787 nfs4_end_op(mi, dvp, NULL, &recov_state, 6788 needrecov); 6789 need_end_op = FALSE; 6790 nfs4_fattr4_free(crattr); 6791 if (setgid_flag) { 6792 nfs4args_verify_free(&argop[8]); 6793 nfs4args_setattr_free(&argop[9]); 6794 } 6795 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6796 goto recov_retry; 6797 } 6798 } 6799 6800 resp = &res; 6801 6802 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 6803 6804 if (res.status == NFS4ERR_BADOWNER) 6805 nfs4_log_badowner(mi, OP_CREATE); 6806 6807 e.error = geterrno4(res.status); 6808 6809 /* 6810 * This check is left over from when create was implemented 6811 * using a setattr op (instead of createattrs). If the 6812 * putfh/create/getfh failed, the error was returned. If 6813 * setattr/getattr failed, we keep going. 6814 * 6815 * It might be better to get rid of the GETFH also, and just 6816 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 6817 * Then if any of the operations failed, we could return the 6818 * error now, and remove much of the error code below. 6819 */ 6820 if (res.array_len <= idx_fattr) { 6821 /* 6822 * Either Putfh, Create or Getfh failed. 6823 */ 6824 PURGE_ATTRCACHE4(dvp); 6825 /* 6826 * nfs4_purge_stale_fh() may generate otw calls through 6827 * nfs4_invalidate_pages. Hence the need to call 6828 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 6829 */ 6830 nfs4_end_op(mi, dvp, NULL, &recov_state, 6831 needrecov); 6832 need_end_op = FALSE; 6833 nfs4_purge_stale_fh(e.error, dvp, cr); 6834 goto out; 6835 } 6836 } 6837 6838 resop = &res.array[idx_create]; /* create res */ 6839 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 6840 6841 resop = &res.array[idx_create + 1]; /* getfh res */ 6842 gf_res = &resop->nfs_resop4_u.opgetfh; 6843 6844 sfhp = sfh4_get(&gf_res->object, mi); 6845 if (e.error) { 6846 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 6847 fn_get(VTOSV(dvp)->sv_name, nm)); 6848 if (vp->v_type == VNON) { 6849 vattr.va_mask = AT_TYPE; 6850 /* 6851 * Need to call nfs4_end_op before nfs4getattr to avoid 6852 * potential nfs4_start_op deadlock. See RFE 4777612. 6853 */ 6854 nfs4_end_op(mi, dvp, NULL, &recov_state, 6855 needrecov); 6856 need_end_op = FALSE; 6857 e.error = nfs4getattr(vp, &vattr, cr); 6858 if (e.error) { 6859 VN_RELE(vp); 6860 *vpp = NULL; 6861 goto out; 6862 } 6863 vp->v_type = vattr.va_type; 6864 } 6865 e.error = 0; 6866 } else { 6867 *vpp = vp = makenfs4node(sfhp, 6868 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 6869 dvp->v_vfsp, t, cr, 6870 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 6871 } 6872 6873 /* 6874 * If compound succeeded, then update dir attrs 6875 */ 6876 if (res.status == NFS4_OK) { 6877 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 6878 dinfo.di_cred = cr; 6879 dinfo.di_time_call = t; 6880 dinfop = &dinfo; 6881 } else 6882 dinfop = NULL; 6883 6884 /* Update directory cache attribute, readdir and dnlc caches */ 6885 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 6886 6887 out: 6888 if (sfhp != NULL) 6889 sfh4_rele(&sfhp); 6890 nfs_rw_exit(&drp->r_rwlock); 6891 nfs4_fattr4_free(crattr); 6892 if (setgid_flag) { 6893 nfs4args_verify_free(&argop[8]); 6894 nfs4args_setattr_free(&argop[9]); 6895 } 6896 if (resp) 6897 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 6898 if (need_end_op) 6899 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6900 6901 kmem_free(argop, argoplist_size); 6902 return (e.error); 6903 } 6904 6905 /* ARGSUSED */ 6906 static int 6907 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6908 int mode, vnode_t **vpp, cred_t *cr) 6909 { 6910 int error; 6911 vnode_t *vp; 6912 nfs_ftype4 type; 6913 specdata4 spec, *specp = NULL; 6914 6915 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 6916 6917 switch (va->va_type) { 6918 case VCHR: 6919 case VBLK: 6920 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 6921 spec.specdata1 = getmajor(va->va_rdev); 6922 spec.specdata2 = getminor(va->va_rdev); 6923 specp = &spec; 6924 break; 6925 6926 case VFIFO: 6927 type = NF4FIFO; 6928 break; 6929 case VSOCK: 6930 type = NF4SOCK; 6931 break; 6932 6933 default: 6934 return (EINVAL); 6935 } 6936 6937 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 6938 if (error) { 6939 return (error); 6940 } 6941 6942 /* 6943 * This might not be needed any more; special case to deal 6944 * with problematic v2/v3 servers. Since create was unable 6945 * to set group correctly, not sure what hope setattr has. 6946 */ 6947 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 6948 va->va_mask = AT_GID; 6949 (void) nfs4setattr(vp, va, 0, cr, NULL); 6950 } 6951 6952 /* 6953 * If vnode is a device create special vnode 6954 */ 6955 if (ISVDEV(vp->v_type)) { 6956 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6957 VN_RELE(vp); 6958 } else { 6959 *vpp = vp; 6960 } 6961 return (error); 6962 } 6963 6964 /* 6965 * Remove requires that the current fh be the target directory. 6966 * After the operation, the current fh is unchanged. 6967 * The compound op structure is: 6968 * PUTFH(targetdir), REMOVE 6969 * 6970 * Weirdness: if the vnode to be removed is open 6971 * we rename it instead of removing it and nfs_inactive 6972 * will remove the new name. 6973 */ 6974 static int 6975 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr) 6976 { 6977 COMPOUND4args_clnt args; 6978 COMPOUND4res_clnt res, *resp = NULL; 6979 REMOVE4res *rm_res; 6980 nfs_argop4 argop[3]; 6981 nfs_resop4 *resop; 6982 vnode_t *vp; 6983 char *tmpname; 6984 int doqueue; 6985 mntinfo4_t *mi; 6986 rnode4_t *rp; 6987 rnode4_t *drp; 6988 int needrecov = 0; 6989 nfs4_recov_state_t recov_state; 6990 int isopen; 6991 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6992 dirattr_info_t dinfo; 6993 6994 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 6995 return (EPERM); 6996 drp = VTOR4(dvp); 6997 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6998 return (EINTR); 6999 7000 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7001 if (e.error) { 7002 nfs_rw_exit(&drp->r_rwlock); 7003 return (e.error); 7004 } 7005 7006 if (vp->v_type == VDIR) { 7007 VN_RELE(vp); 7008 nfs_rw_exit(&drp->r_rwlock); 7009 return (EISDIR); 7010 } 7011 7012 /* 7013 * First just remove the entry from the name cache, as it 7014 * is most likely the only entry for this vp. 7015 */ 7016 dnlc_remove(dvp, nm); 7017 7018 rp = VTOR4(vp); 7019 7020 /* 7021 * For regular file types, check to see if the file is open by looking 7022 * at the open streams. 7023 * For all other types, check the reference count on the vnode. Since 7024 * they are not opened OTW they never have an open stream. 7025 * 7026 * If the file is open, rename it to .nfsXXXX. 7027 */ 7028 if (vp->v_type != VREG) { 7029 /* 7030 * If the file has a v_count > 1 then there may be more than one 7031 * entry in the name cache due multiple links or an open file, 7032 * but we don't have the real reference count so flush all 7033 * possible entries. 7034 */ 7035 if (vp->v_count > 1) 7036 dnlc_purge_vp(vp); 7037 7038 /* 7039 * Now we have the real reference count. 7040 */ 7041 isopen = vp->v_count > 1; 7042 } else { 7043 mutex_enter(&rp->r_os_lock); 7044 isopen = list_head(&rp->r_open_streams) != NULL; 7045 mutex_exit(&rp->r_os_lock); 7046 } 7047 7048 mutex_enter(&rp->r_statelock); 7049 if (isopen && 7050 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7051 mutex_exit(&rp->r_statelock); 7052 tmpname = newname(); 7053 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr); 7054 if (e.error) 7055 kmem_free(tmpname, MAXNAMELEN); 7056 else { 7057 mutex_enter(&rp->r_statelock); 7058 if (rp->r_unldvp == NULL) { 7059 VN_HOLD(dvp); 7060 rp->r_unldvp = dvp; 7061 if (rp->r_unlcred != NULL) 7062 crfree(rp->r_unlcred); 7063 crhold(cr); 7064 rp->r_unlcred = cr; 7065 rp->r_unlname = tmpname; 7066 } else { 7067 kmem_free(rp->r_unlname, MAXNAMELEN); 7068 rp->r_unlname = tmpname; 7069 } 7070 mutex_exit(&rp->r_statelock); 7071 } 7072 VN_RELE(vp); 7073 nfs_rw_exit(&drp->r_rwlock); 7074 return (e.error); 7075 } 7076 /* 7077 * Actually remove the file/dir 7078 */ 7079 mutex_exit(&rp->r_statelock); 7080 7081 /* 7082 * We need to flush any dirty pages which happen to 7083 * be hanging around before removing the file. 7084 * This shouldn't happen very often since in NFSv4 7085 * we should be close to open consistent. 7086 */ 7087 if (nfs4_has_pages(vp) && 7088 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7089 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 7090 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7091 mutex_enter(&rp->r_statelock); 7092 if (!rp->r_error) 7093 rp->r_error = e.error; 7094 mutex_exit(&rp->r_statelock); 7095 } 7096 } 7097 7098 mi = VTOMI4(dvp); 7099 7100 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7101 recov_state.rs_flags = 0; 7102 recov_state.rs_num_retry_despite_err = 0; 7103 7104 recov_retry: 7105 /* 7106 * Remove ops: putfh dir; remove 7107 */ 7108 args.ctag = TAG_REMOVE; 7109 args.array_len = 3; 7110 args.array = argop; 7111 7112 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7113 if (e.error) { 7114 nfs_rw_exit(&drp->r_rwlock); 7115 VN_RELE(vp); 7116 return (e.error); 7117 } 7118 7119 /* putfh directory */ 7120 argop[0].argop = OP_CPUTFH; 7121 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7122 7123 /* remove */ 7124 argop[1].argop = OP_CREMOVE; 7125 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7126 7127 /* getattr dir */ 7128 argop[2].argop = OP_GETATTR; 7129 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7130 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7131 7132 doqueue = 1; 7133 dinfo.di_time_call = gethrtime(); 7134 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7135 7136 PURGE_ATTRCACHE4(vp); 7137 7138 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7139 if (e.error) 7140 PURGE_ATTRCACHE4(dvp); 7141 7142 if (needrecov) { 7143 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7144 NULL, NULL, NULL, OP_REMOVE, NULL) == FALSE) { 7145 if (!e.error) 7146 (void) xdr_free(xdr_COMPOUND4res_clnt, 7147 (caddr_t)&res); 7148 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7149 needrecov); 7150 goto recov_retry; 7151 } 7152 } 7153 7154 /* 7155 * Matching nfs4_end_op() for start_op() above. 7156 * There is a path in the code below which calls 7157 * nfs4_purge_stale_fh(), which may generate otw calls through 7158 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7159 * here to avoid nfs4_start_op() deadlock. 7160 */ 7161 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7162 7163 if (!e.error) { 7164 resp = &res; 7165 7166 if (res.status) { 7167 e.error = geterrno4(res.status); 7168 PURGE_ATTRCACHE4(dvp); 7169 nfs4_purge_stale_fh(e.error, dvp, cr); 7170 } else { 7171 resop = &res.array[1]; /* remove res */ 7172 rm_res = &resop->nfs_resop4_u.opremove; 7173 7174 dinfo.di_garp = 7175 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7176 dinfo.di_cred = cr; 7177 7178 /* Update directory attr, readdir and dnlc caches */ 7179 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7180 &dinfo); 7181 } 7182 } 7183 nfs_rw_exit(&drp->r_rwlock); 7184 if (resp) 7185 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7186 7187 VN_RELE(vp); 7188 return (e.error); 7189 } 7190 7191 /* 7192 * Link requires that the current fh be the target directory and the 7193 * saved fh be the source fh. After the operation, the current fh is unchanged. 7194 * Thus the compound op structure is: 7195 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7196 * GETATTR(file) 7197 */ 7198 static int 7199 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr) 7200 { 7201 COMPOUND4args_clnt args; 7202 COMPOUND4res_clnt res, *resp = NULL; 7203 LINK4res *ln_res; 7204 int argoplist_size = 7 * sizeof (nfs_argop4); 7205 nfs_argop4 *argop; 7206 nfs_resop4 *resop; 7207 vnode_t *realvp, *nvp; 7208 int doqueue; 7209 mntinfo4_t *mi; 7210 rnode4_t *tdrp; 7211 bool_t needrecov = FALSE; 7212 nfs4_recov_state_t recov_state; 7213 hrtime_t t; 7214 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7215 dirattr_info_t dinfo; 7216 7217 ASSERT(*tnm != '\0'); 7218 ASSERT(tdvp->v_type == VDIR); 7219 ASSERT(nfs4_consistent_type(tdvp)); 7220 ASSERT(nfs4_consistent_type(svp)); 7221 7222 if (curproc->p_zone != VTOMI4(tdvp)->mi_zone) 7223 return (EPERM); 7224 if (VOP_REALVP(svp, &realvp) == 0) { 7225 svp = realvp; 7226 ASSERT(nfs4_consistent_type(svp)); 7227 } 7228 7229 tdrp = VTOR4(tdvp); 7230 mi = VTOMI4(svp); 7231 7232 if (!(mi->mi_flags & MI4_LINK)) { 7233 return (EOPNOTSUPP); 7234 } 7235 recov_state.rs_flags = 0; 7236 recov_state.rs_num_retry_despite_err = 0; 7237 7238 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7239 return (EINTR); 7240 7241 recov_retry: 7242 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7243 7244 args.ctag = TAG_LINK; 7245 7246 /* 7247 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7248 * restorefh; getattr(fl) 7249 */ 7250 args.array_len = 7; 7251 args.array = argop; 7252 7253 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7254 if (e.error) { 7255 kmem_free(argop, argoplist_size); 7256 nfs_rw_exit(&tdrp->r_rwlock); 7257 return (e.error); 7258 } 7259 7260 /* 0. putfh file */ 7261 argop[0].argop = OP_CPUTFH; 7262 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7263 7264 /* 1. save current fh to free up the space for the dir */ 7265 argop[1].argop = OP_SAVEFH; 7266 7267 /* 2. putfh targetdir */ 7268 argop[2].argop = OP_CPUTFH; 7269 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7270 7271 /* 3. link: current_fh is targetdir, saved_fh is source */ 7272 argop[3].argop = OP_CLINK; 7273 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7274 7275 /* 4. Get attributes of dir */ 7276 argop[4].argop = OP_GETATTR; 7277 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7278 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7279 7280 /* 5. If link was successful, restore current vp to file */ 7281 argop[5].argop = OP_RESTOREFH; 7282 7283 /* 6. Get attributes of linked object */ 7284 argop[6].argop = OP_GETATTR; 7285 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7286 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7287 7288 dnlc_remove(tdvp, tnm); 7289 7290 doqueue = 1; 7291 t = gethrtime(); 7292 7293 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7294 7295 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7296 if (e.error != 0 && !needrecov) { 7297 PURGE_ATTRCACHE4(tdvp); 7298 PURGE_ATTRCACHE4(svp); 7299 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7300 goto out; 7301 } 7302 7303 if (needrecov) { 7304 bool_t abort; 7305 7306 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7307 NULL, NULL, OP_LINK, NULL); 7308 if (abort == FALSE) { 7309 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7310 needrecov); 7311 kmem_free(argop, argoplist_size); 7312 if (!e.error) 7313 (void) xdr_free(xdr_COMPOUND4res_clnt, 7314 (caddr_t)&res); 7315 goto recov_retry; 7316 } else { 7317 if (e.error != 0) { 7318 PURGE_ATTRCACHE4(tdvp); 7319 PURGE_ATTRCACHE4(svp); 7320 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7321 &recov_state, needrecov); 7322 goto out; 7323 } 7324 /* fall through for res.status case */ 7325 } 7326 } 7327 7328 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7329 7330 resp = &res; 7331 if (res.status) { 7332 /* If link succeeded, then don't return error */ 7333 e.error = geterrno4(res.status); 7334 if (res.array_len <= 4) { 7335 /* 7336 * Either Putfh, Savefh, Putfh dir, or Link failed 7337 */ 7338 PURGE_ATTRCACHE4(svp); 7339 PURGE_ATTRCACHE4(tdvp); 7340 if (e.error == EOPNOTSUPP) { 7341 mutex_enter(&mi->mi_lock); 7342 mi->mi_flags &= ~MI4_LINK; 7343 mutex_exit(&mi->mi_lock); 7344 } 7345 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7346 /* XXX-LP */ 7347 if (e.error == EISDIR && crgetuid(cr) != 0) 7348 e.error = EPERM; 7349 goto out; 7350 } 7351 } 7352 7353 /* either no error or one of the postop getattr failed */ 7354 7355 /* 7356 * XXX - if LINK succeeded, but no attrs were returned for link 7357 * file, purge its cache. 7358 * 7359 * XXX Perform a simplified version of wcc checking. Instead of 7360 * have another getattr to get pre-op, just purge cache if 7361 * any of the ops prior to and including the getattr failed. 7362 * If the getattr succeeded then update the attrcache accordingly. 7363 */ 7364 7365 /* 7366 * update cache with link file postattrs. 7367 * Note: at this point resop points to link res. 7368 */ 7369 resop = &res.array[3]; /* link res */ 7370 ln_res = &resop->nfs_resop4_u.oplink; 7371 if (res.status == NFS4_OK) { 7372 e.error = nfs4_update_attrcache(res.status, 7373 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7374 t, svp, cr); 7375 } 7376 7377 /* 7378 * Call makenfs4node to create the new shadow vp for tnm. 7379 * We pass NULL attrs because we just cached attrs for 7380 * the src object. All we're trying to accomplish is to 7381 * to create the new shadow vnode. 7382 */ 7383 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7384 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm)); 7385 7386 /* Update target cache attribute, readdir and dnlc caches */ 7387 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7388 dinfo.di_time_call = t; 7389 dinfo.di_cred = cr; 7390 7391 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7392 ASSERT(nfs4_consistent_type(tdvp)); 7393 ASSERT(nfs4_consistent_type(svp)); 7394 ASSERT(nfs4_consistent_type(nvp)); 7395 VN_RELE(nvp); 7396 7397 out: 7398 kmem_free(argop, argoplist_size); 7399 if (resp) 7400 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7401 7402 nfs_rw_exit(&tdrp->r_rwlock); 7403 7404 return (e.error); 7405 } 7406 7407 static int 7408 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7409 { 7410 vnode_t *realvp; 7411 7412 if (curproc->p_zone != VTOMI4(odvp)->mi_zone) 7413 return (EPERM); 7414 if (VOP_REALVP(ndvp, &realvp) == 0) 7415 ndvp = realvp; 7416 7417 return (nfs4rename(odvp, onm, ndvp, nnm, cr)); 7418 } 7419 7420 /* 7421 * nfs4rename does the real work of renaming in NFS Version 4. 7422 * 7423 * A file handle is considered volatile for renaming purposes if either 7424 * of the volatile bits are turned on. However, the compound may differ 7425 * based on the likelihood of the filehandle to change during rename. 7426 */ 7427 static int 7428 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7429 { 7430 int error; 7431 mntinfo4_t *mi; 7432 vnode_t *nvp; 7433 vnode_t *ovp = NULL; 7434 char *tmpname = NULL; 7435 rnode4_t *rp; 7436 rnode4_t *odrp; 7437 rnode4_t *ndrp; 7438 int did_link = 0; 7439 int do_link = 1; 7440 nfsstat4 stat = NFS4_OK; 7441 7442 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 7443 ASSERT(nfs4_consistent_type(odvp)); 7444 ASSERT(nfs4_consistent_type(ndvp)); 7445 7446 if (onm[0] == '.' && (onm[1] == '\0' || 7447 (onm[1] == '.' && onm[2] == '\0'))) 7448 return (EINVAL); 7449 7450 if (nnm[0] == '.' && (nnm[1] == '\0' || 7451 (nnm[1] == '.' && nnm[2] == '\0'))) 7452 return (EINVAL); 7453 7454 odrp = VTOR4(odvp); 7455 ndrp = VTOR4(ndvp); 7456 if ((intptr_t)odrp < (intptr_t)ndrp) { 7457 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7458 return (EINTR); 7459 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7460 nfs_rw_exit(&odrp->r_rwlock); 7461 return (EINTR); 7462 } 7463 } else { 7464 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7465 return (EINTR); 7466 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7467 nfs_rw_exit(&ndrp->r_rwlock); 7468 return (EINTR); 7469 } 7470 } 7471 7472 /* 7473 * Lookup the target file. If it exists, it needs to be 7474 * checked to see whether it is a mount point and whether 7475 * it is active (open). 7476 */ 7477 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7478 if (!error) { 7479 int isactive; 7480 7481 ASSERT(nfs4_consistent_type(nvp)); 7482 /* 7483 * If this file has been mounted on, then just 7484 * return busy because renaming to it would remove 7485 * the mounted file system from the name space. 7486 */ 7487 if (vn_ismntpt(nvp)) { 7488 VN_RELE(nvp); 7489 nfs_rw_exit(&odrp->r_rwlock); 7490 nfs_rw_exit(&ndrp->r_rwlock); 7491 return (EBUSY); 7492 } 7493 7494 /* 7495 * First just remove the entry from the name cache, as it 7496 * is most likely the only entry for this vp. 7497 */ 7498 dnlc_remove(ndvp, nnm); 7499 7500 rp = VTOR4(nvp); 7501 7502 if (nvp->v_type != VREG) { 7503 /* 7504 * Purge the name cache of all references to this vnode 7505 * so that we can check the reference count to infer 7506 * whether it is active or not. 7507 */ 7508 if (nvp->v_count > 1) 7509 dnlc_purge_vp(nvp); 7510 7511 isactive = nvp->v_count > 1; 7512 } else { 7513 mutex_enter(&rp->r_os_lock); 7514 isactive = list_head(&rp->r_open_streams) != NULL; 7515 mutex_exit(&rp->r_os_lock); 7516 } 7517 7518 /* 7519 * If the vnode is active and is not a directory, 7520 * arrange to rename it to a 7521 * temporary file so that it will continue to be 7522 * accessible. This implements the "unlink-open-file" 7523 * semantics for the target of a rename operation. 7524 * Before doing this though, make sure that the 7525 * source and target files are not already the same. 7526 */ 7527 if (isactive && nvp->v_type != VDIR) { 7528 /* 7529 * Lookup the source name. 7530 */ 7531 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7532 7533 /* 7534 * The source name *should* already exist. 7535 */ 7536 if (error) { 7537 VN_RELE(nvp); 7538 nfs_rw_exit(&odrp->r_rwlock); 7539 nfs_rw_exit(&ndrp->r_rwlock); 7540 return (error); 7541 } 7542 7543 ASSERT(nfs4_consistent_type(ovp)); 7544 7545 /* 7546 * Compare the two vnodes. If they are the same, 7547 * just release all held vnodes and return success. 7548 */ 7549 if (VN_CMP(ovp, nvp)) { 7550 VN_RELE(ovp); 7551 VN_RELE(nvp); 7552 nfs_rw_exit(&odrp->r_rwlock); 7553 nfs_rw_exit(&ndrp->r_rwlock); 7554 return (0); 7555 } 7556 7557 /* 7558 * Can't mix and match directories and non- 7559 * directories in rename operations. We already 7560 * know that the target is not a directory. If 7561 * the source is a directory, return an error. 7562 */ 7563 if (ovp->v_type == VDIR) { 7564 VN_RELE(ovp); 7565 VN_RELE(nvp); 7566 nfs_rw_exit(&odrp->r_rwlock); 7567 nfs_rw_exit(&ndrp->r_rwlock); 7568 return (ENOTDIR); 7569 } 7570 link_call: 7571 /* 7572 * The target file exists, is not the same as 7573 * the source file, and is active. We first 7574 * try to Link it to a temporary filename to 7575 * avoid having the server removing the file 7576 * completely (which could cause data loss to 7577 * the user's POV in the event the Rename fails 7578 * -- see bug 1165874). 7579 */ 7580 /* 7581 * The do_link and did_link booleans are 7582 * introduced in the event we get NFS4ERR_FILE_OPEN 7583 * returned for the Rename. Some servers can 7584 * not Rename over an Open file, so they return 7585 * this error. The client needs to Remove the 7586 * newly created Link and do two Renames, just 7587 * as if the server didn't support LINK. 7588 */ 7589 tmpname = newname(); 7590 error = 0; 7591 7592 if (do_link) { 7593 error = nfs4_link(ndvp, nvp, tmpname, cr); 7594 } 7595 if (error == EOPNOTSUPP || !do_link) { 7596 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7597 cr); 7598 did_link = 0; 7599 } else { 7600 did_link = 1; 7601 } 7602 if (error) { 7603 kmem_free(tmpname, MAXNAMELEN); 7604 VN_RELE(ovp); 7605 VN_RELE(nvp); 7606 nfs_rw_exit(&odrp->r_rwlock); 7607 nfs_rw_exit(&ndrp->r_rwlock); 7608 return (error); 7609 } 7610 7611 mutex_enter(&rp->r_statelock); 7612 if (rp->r_unldvp == NULL) { 7613 VN_HOLD(ndvp); 7614 rp->r_unldvp = ndvp; 7615 if (rp->r_unlcred != NULL) 7616 crfree(rp->r_unlcred); 7617 crhold(cr); 7618 rp->r_unlcred = cr; 7619 rp->r_unlname = tmpname; 7620 } else { 7621 if (rp->r_unlname) 7622 kmem_free(rp->r_unlname, MAXNAMELEN); 7623 rp->r_unlname = tmpname; 7624 } 7625 mutex_exit(&rp->r_statelock); 7626 } 7627 7628 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7629 7630 ASSERT(nfs4_consistent_type(nvp)); 7631 VN_RELE(nvp); 7632 } 7633 7634 if (ovp == NULL) { 7635 /* 7636 * When renaming directories to be a subdirectory of a 7637 * different parent, the dnlc entry for ".." will no 7638 * longer be valid, so it must be removed. 7639 * 7640 * We do a lookup here to determine whether we are renaming 7641 * a directory and we need to check if we are renaming 7642 * an unlinked file. This might have already been done 7643 * in previous code, so we check ovp == NULL to avoid 7644 * doing it twice. 7645 */ 7646 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7647 /* 7648 * The source name *should* already exist. 7649 */ 7650 if (error) { 7651 nfs_rw_exit(&odrp->r_rwlock); 7652 nfs_rw_exit(&ndrp->r_rwlock); 7653 return (error); 7654 } 7655 ASSERT(ovp != NULL); 7656 ASSERT(nfs4_consistent_type(ovp)); 7657 } 7658 7659 /* 7660 * Is the object being renamed a dir, and if so, is 7661 * it being renamed to a child of itself? The underlying 7662 * fs should ultimately return EINVAL for this case; 7663 * however, buggy beta non-Solaris NFSv4 servers at 7664 * interop testing events have allowed this behavior, 7665 * and it caused our client to panic due to a recursive 7666 * mutex_enter in fn_move. 7667 * 7668 * The tedious locking in fn_move could be changed to 7669 * deal with this case, and the client could avoid the 7670 * panic; however, the client would just confuse itself 7671 * later and misbehave. A better way to handle the broken 7672 * server is to detect this condition and return EINVAL 7673 * without ever sending the the bogus rename to the server. 7674 * We know the rename is invalid -- just fail it now. 7675 */ 7676 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7677 VN_RELE(ovp); 7678 nfs_rw_exit(&odrp->r_rwlock); 7679 nfs_rw_exit(&ndrp->r_rwlock); 7680 return (EINVAL); 7681 } 7682 7683 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7684 7685 /* 7686 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7687 * possible for the filehandle to change due to the rename. 7688 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7689 * the fh will not change because of the rename, but we still need 7690 * to update its rnode entry with the new name for 7691 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7692 * has no effect on these for now, but for future improvements, 7693 * we might want to use it too to simplify handling of files 7694 * that are open with that flag on. (XXX) 7695 */ 7696 mi = VTOMI4(odvp); 7697 if (NFS4_VOLATILE_FH(mi)) { 7698 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7699 &stat); 7700 } else { 7701 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7702 &stat); 7703 } 7704 ASSERT(nfs4_consistent_type(odvp)); 7705 ASSERT(nfs4_consistent_type(ndvp)); 7706 ASSERT(nfs4_consistent_type(ovp)); 7707 7708 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7709 do_link = 0; 7710 /* 7711 * Before the 'link_call' code, we did a nfs4_lookup 7712 * that puts a VN_HOLD on nvp. After the nfs4_link 7713 * call we call VN_RELE to match that hold. We need 7714 * to place an additional VN_HOLD here since we will 7715 * be hitting that VN_RELE again. 7716 */ 7717 VN_HOLD(nvp); 7718 7719 (void) nfs4_remove(ndvp, tmpname, cr); 7720 7721 /* Undo the unlinked file naming stuff we just did */ 7722 mutex_enter(&rp->r_statelock); 7723 if (rp->r_unldvp) { 7724 VN_RELE(ndvp); 7725 rp->r_unldvp = NULL; 7726 if (rp->r_unlcred != NULL) 7727 crfree(rp->r_unlcred); 7728 rp->r_unlcred = NULL; 7729 /* rp->r_unlanme points to tmpname */ 7730 if (rp->r_unlname) 7731 kmem_free(rp->r_unlname, MAXNAMELEN); 7732 rp->r_unlname = NULL; 7733 } 7734 mutex_exit(&rp->r_statelock); 7735 7736 goto link_call; 7737 } 7738 7739 if (error) { 7740 VN_RELE(ovp); 7741 nfs_rw_exit(&odrp->r_rwlock); 7742 nfs_rw_exit(&ndrp->r_rwlock); 7743 return (error); 7744 } 7745 7746 /* 7747 * when renaming directories to be a subdirectory of a 7748 * different parent, the dnlc entry for ".." will no 7749 * longer be valid, so it must be removed 7750 */ 7751 rp = VTOR4(ovp); 7752 if (ndvp != odvp) { 7753 if (ovp->v_type == VDIR) { 7754 dnlc_remove(ovp, ".."); 7755 if (rp->r_dir != NULL) 7756 nfs4_purge_rddir_cache(ovp); 7757 } 7758 } 7759 7760 /* 7761 * If we are renaming the unlinked file, update the 7762 * r_unldvp and r_unlname as needed. 7763 */ 7764 mutex_enter(&rp->r_statelock); 7765 if (rp->r_unldvp != NULL) { 7766 if (strcmp(rp->r_unlname, onm) == 0) { 7767 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 7768 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 7769 if (ndvp != rp->r_unldvp) { 7770 VN_RELE(rp->r_unldvp); 7771 rp->r_unldvp = ndvp; 7772 VN_HOLD(ndvp); 7773 } 7774 } 7775 } 7776 mutex_exit(&rp->r_statelock); 7777 7778 VN_RELE(ovp); 7779 7780 nfs_rw_exit(&odrp->r_rwlock); 7781 nfs_rw_exit(&ndrp->r_rwlock); 7782 7783 return (error); 7784 } 7785 7786 /* 7787 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 7788 * when it is known that the filehandle is persistent through rename. 7789 * 7790 * Rename requires that the current fh be the target directory and the 7791 * saved fh be the source directory. After the operation, the current fh 7792 * is unchanged. 7793 * The compound op structure for persistent fh rename is: 7794 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 7795 * Rather than bother with the directory postop args, we'll simply 7796 * update that a change occured in the cache, so no post-op getattrs. 7797 */ 7798 static int 7799 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 7800 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7801 { 7802 COMPOUND4args_clnt args; 7803 COMPOUND4res_clnt res, *resp = NULL; 7804 nfs_argop4 *argop; 7805 nfs_resop4 *resop; 7806 int doqueue, argoplist_size; 7807 mntinfo4_t *mi; 7808 rnode4_t *odrp = VTOR4(odvp); 7809 rnode4_t *ndrp = VTOR4(ndvp); 7810 RENAME4res *rn_res; 7811 bool_t needrecov; 7812 nfs4_recov_state_t recov_state; 7813 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7814 dirattr_info_t dinfo, *dinfop; 7815 7816 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 7817 7818 recov_state.rs_flags = 0; 7819 recov_state.rs_num_retry_despite_err = 0; 7820 7821 /* 7822 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 7823 * 7824 * If source/target are different dirs, then append putfh(src); getattr 7825 */ 7826 args.array_len = (odvp == ndvp) ? 5 : 7; 7827 argoplist_size = args.array_len * sizeof (nfs_argop4); 7828 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 7829 7830 recov_retry: 7831 *statp = NFS4_OK; 7832 7833 /* No need to Lookup the file, persistent fh */ 7834 args.ctag = TAG_RENAME; 7835 7836 mi = VTOMI4(odvp); 7837 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 7838 if (e.error) { 7839 kmem_free(argop, argoplist_size); 7840 return (e.error); 7841 } 7842 7843 /* 0: putfh source directory */ 7844 argop[0].argop = OP_CPUTFH; 7845 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 7846 7847 /* 1: Save source fh to free up current for target */ 7848 argop[1].argop = OP_SAVEFH; 7849 7850 /* 2: putfh targetdir */ 7851 argop[2].argop = OP_CPUTFH; 7852 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7853 7854 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 7855 argop[3].argop = OP_CRENAME; 7856 argop[3].nfs_argop4_u.opcrename.coldname = onm; 7857 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 7858 7859 /* 4: getattr (targetdir) */ 7860 argop[4].argop = OP_GETATTR; 7861 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7862 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7863 7864 if (ndvp != odvp) { 7865 7866 /* 5: putfh (sourcedir) */ 7867 argop[5].argop = OP_CPUTFH; 7868 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7869 7870 /* 6: getattr (sourcedir) */ 7871 argop[6].argop = OP_GETATTR; 7872 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7873 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7874 } 7875 7876 dnlc_remove(odvp, onm); 7877 dnlc_remove(ndvp, nnm); 7878 7879 doqueue = 1; 7880 dinfo.di_time_call = gethrtime(); 7881 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7882 7883 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7884 if (e.error) { 7885 PURGE_ATTRCACHE4(odvp); 7886 PURGE_ATTRCACHE4(ndvp); 7887 } else { 7888 *statp = res.status; 7889 } 7890 7891 if (needrecov) { 7892 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 7893 OP_RENAME, NULL) == FALSE) { 7894 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 7895 if (!e.error) 7896 (void) xdr_free(xdr_COMPOUND4res_clnt, 7897 (caddr_t)&res); 7898 goto recov_retry; 7899 } 7900 } 7901 7902 if (!e.error) { 7903 resp = &res; 7904 /* 7905 * as long as OP_RENAME 7906 */ 7907 if (res.status != NFS4_OK && res.array_len <= 4) { 7908 e.error = geterrno4(res.status); 7909 PURGE_ATTRCACHE4(odvp); 7910 PURGE_ATTRCACHE4(ndvp); 7911 /* 7912 * System V defines rename to return EEXIST, not 7913 * ENOTEMPTY if the target directory is not empty. 7914 * Over the wire, the error is NFSERR_ENOTEMPTY 7915 * which geterrno4 maps to ENOTEMPTY. 7916 */ 7917 if (e.error == ENOTEMPTY) 7918 e.error = EEXIST; 7919 } else { 7920 7921 resop = &res.array[3]; /* rename res */ 7922 rn_res = &resop->nfs_resop4_u.oprename; 7923 7924 if (res.status == NFS4_OK) { 7925 /* 7926 * Update target attribute, readdir and dnlc 7927 * caches. 7928 */ 7929 dinfo.di_garp = 7930 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7931 dinfo.di_cred = cr; 7932 dinfop = &dinfo; 7933 } else 7934 dinfop = NULL; 7935 7936 nfs4_update_dircaches(&rn_res->target_cinfo, 7937 ndvp, NULL, NULL, dinfop); 7938 7939 /* 7940 * Update source attribute, readdir and dnlc caches 7941 * 7942 */ 7943 if (ndvp != odvp) { 7944 if (dinfop) 7945 dinfo.di_garp = 7946 &(res.array[6].nfs_resop4_u. 7947 opgetattr.ga_res); 7948 7949 nfs4_update_dircaches(&rn_res->source_cinfo, 7950 odvp, NULL, NULL, dinfop); 7951 } 7952 7953 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 7954 nnm); 7955 } 7956 } 7957 7958 if (resp) 7959 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7960 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 7961 kmem_free(argop, argoplist_size); 7962 7963 return (e.error); 7964 } 7965 7966 /* 7967 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 7968 * it is possible for the filehandle to change due to the rename. 7969 * 7970 * The compound req in this case includes a post-rename lookup and getattr 7971 * to ensure that we have the correct fh and attributes for the object. 7972 * 7973 * Rename requires that the current fh be the target directory and the 7974 * saved fh be the source directory. After the operation, the current fh 7975 * is unchanged. 7976 * 7977 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 7978 * update the filehandle for the renamed object. We also get the old 7979 * filehandle for historical reasons; this should be taken out sometime. 7980 * This results in a rather cumbersome compound... 7981 * 7982 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 7983 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 7984 * 7985 */ 7986 static int 7987 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 7988 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7989 { 7990 COMPOUND4args_clnt args; 7991 COMPOUND4res_clnt res, *resp = NULL; 7992 int argoplist_size; 7993 nfs_argop4 *argop; 7994 nfs_resop4 *resop; 7995 int doqueue; 7996 mntinfo4_t *mi; 7997 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 7998 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 7999 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8000 RENAME4res *rn_res; 8001 GETFH4res *ngf_res; 8002 bool_t needrecov; 8003 nfs4_recov_state_t recov_state; 8004 hrtime_t t; 8005 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8006 dirattr_info_t dinfo, *dinfop = &dinfo; 8007 8008 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 8009 8010 recov_state.rs_flags = 0; 8011 recov_state.rs_num_retry_despite_err = 0; 8012 8013 recov_retry: 8014 *statp = NFS4_OK; 8015 8016 /* 8017 * There is a window between the RPC and updating the path and 8018 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8019 * code, so that it doesn't try to use the old path during that 8020 * window. 8021 */ 8022 mutex_enter(&orp->r_statelock); 8023 while (orp->r_flags & R4RECEXPFH) { 8024 klwp_t *lwp = ttolwp(curthread); 8025 8026 if (lwp != NULL) 8027 lwp->lwp_nostop++; 8028 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8029 mutex_exit(&orp->r_statelock); 8030 if (lwp != NULL) 8031 lwp->lwp_nostop--; 8032 return (EINTR); 8033 } 8034 if (lwp != NULL) 8035 lwp->lwp_nostop--; 8036 } 8037 orp->r_flags |= R4RECEXPFH; 8038 mutex_exit(&orp->r_statelock); 8039 8040 mi = VTOMI4(odvp); 8041 8042 args.ctag = TAG_RENAME_VFH; 8043 args.array_len = (odvp == ndvp) ? 10 : 12; 8044 argoplist_size = args.array_len * sizeof (nfs_argop4); 8045 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8046 8047 /* 8048 * Rename ops: 8049 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8050 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8051 * LOOKUP(trgt), GETFH(new), GETATTR, 8052 * 8053 * if (odvp != ndvp) 8054 * add putfh(sourcedir), getattr(sourcedir) } 8055 */ 8056 args.array = argop; 8057 8058 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8059 &recov_state, NULL); 8060 if (e.error) { 8061 kmem_free(argop, argoplist_size); 8062 mutex_enter(&orp->r_statelock); 8063 orp->r_flags &= ~R4RECEXPFH; 8064 cv_broadcast(&orp->r_cv); 8065 mutex_exit(&orp->r_statelock); 8066 return (e.error); 8067 } 8068 8069 /* 0: putfh source directory */ 8070 argop[0].argop = OP_CPUTFH; 8071 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8072 8073 /* 1: Save source fh to free up current for target */ 8074 argop[1].argop = OP_SAVEFH; 8075 8076 /* 2: Lookup pre-rename fh of renamed object */ 8077 argop[2].argop = OP_CLOOKUP; 8078 argop[2].nfs_argop4_u.opclookup.cname = onm; 8079 8080 /* 3: getfh fh of renamed object (before rename) */ 8081 argop[3].argop = OP_GETFH; 8082 8083 /* 4: putfh targetdir */ 8084 argop[4].argop = OP_CPUTFH; 8085 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8086 8087 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8088 argop[5].argop = OP_CRENAME; 8089 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8090 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8091 8092 /* 6: getattr of target dir (post op attrs) */ 8093 argop[6].argop = OP_GETATTR; 8094 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8095 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8096 8097 /* 7: Lookup post-rename fh of renamed object */ 8098 argop[7].argop = OP_CLOOKUP; 8099 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8100 8101 /* 8: getfh fh of renamed object (after rename) */ 8102 argop[8].argop = OP_GETFH; 8103 8104 /* 9: getattr of renamed object */ 8105 argop[9].argop = OP_GETATTR; 8106 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8107 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8108 8109 /* 8110 * If source/target dirs are different, then get new post-op 8111 * attrs for source dir also. 8112 */ 8113 if (ndvp != odvp) { 8114 /* 10: putfh (sourcedir) */ 8115 argop[10].argop = OP_CPUTFH; 8116 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8117 8118 /* 11: getattr (sourcedir) */ 8119 argop[11].argop = OP_GETATTR; 8120 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8121 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8122 } 8123 8124 dnlc_remove(odvp, onm); 8125 dnlc_remove(ndvp, nnm); 8126 8127 doqueue = 1; 8128 t = gethrtime(); 8129 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8130 8131 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8132 if (e.error) { 8133 PURGE_ATTRCACHE4(odvp); 8134 PURGE_ATTRCACHE4(ndvp); 8135 if (!needrecov) { 8136 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8137 &recov_state, needrecov); 8138 goto out; 8139 } 8140 } else { 8141 *statp = res.status; 8142 } 8143 8144 if (needrecov) { 8145 bool_t abort; 8146 8147 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8148 OP_RENAME, NULL); 8149 if (abort == FALSE) { 8150 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8151 &recov_state, needrecov); 8152 kmem_free(argop, argoplist_size); 8153 if (!e.error) 8154 (void) xdr_free(xdr_COMPOUND4res_clnt, 8155 (caddr_t)&res); 8156 mutex_enter(&orp->r_statelock); 8157 orp->r_flags &= ~R4RECEXPFH; 8158 cv_broadcast(&orp->r_cv); 8159 mutex_exit(&orp->r_statelock); 8160 goto recov_retry; 8161 } else { 8162 if (e.error != 0) { 8163 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8164 &recov_state, needrecov); 8165 goto out; 8166 } 8167 /* fall through for res.status case */ 8168 } 8169 } 8170 8171 resp = &res; 8172 /* 8173 * If OP_RENAME (or any prev op) failed, then return an error. 8174 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8175 */ 8176 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8177 /* 8178 * Error in an op other than last Getattr 8179 */ 8180 e.error = geterrno4(res.status); 8181 PURGE_ATTRCACHE4(odvp); 8182 PURGE_ATTRCACHE4(ndvp); 8183 /* 8184 * System V defines rename to return EEXIST, not 8185 * ENOTEMPTY if the target directory is not empty. 8186 * Over the wire, the error is NFSERR_ENOTEMPTY 8187 * which geterrno4 maps to ENOTEMPTY. 8188 */ 8189 if (e.error == ENOTEMPTY) 8190 e.error = EEXIST; 8191 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8192 needrecov); 8193 goto out; 8194 } 8195 8196 /* rename results */ 8197 rn_res = &res.array[5].nfs_resop4_u.oprename; 8198 8199 if (res.status == NFS4_OK) { 8200 /* Update target attribute, readdir and dnlc caches */ 8201 dinfo.di_garp = 8202 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8203 dinfo.di_cred = cr; 8204 dinfo.di_time_call = t; 8205 } else 8206 dinfop = NULL; 8207 8208 /* Update source cache attribute, readdir and dnlc caches */ 8209 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8210 8211 /* Update source cache attribute, readdir and dnlc caches */ 8212 if (ndvp != odvp) { 8213 8214 /* 8215 * If dinfop is non-NULL, then compound succeded, so 8216 * set di_garp to attrs for source dir. dinfop is only 8217 * set to NULL when compound fails. 8218 */ 8219 if (dinfop) 8220 dinfo.di_garp = 8221 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8222 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8223 dinfop); 8224 } 8225 8226 /* 8227 * Update the rnode with the new component name and args, 8228 * and if the file handle changed, also update it with the new fh. 8229 * This is only necessary if the target object has an rnode 8230 * entry and there is no need to create one for it. 8231 */ 8232 resop = &res.array[8]; /* getfh new res */ 8233 ngf_res = &resop->nfs_resop4_u.opgetfh; 8234 8235 /* 8236 * Update the path and filehandle for the renamed object. 8237 */ 8238 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8239 8240 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8241 8242 if (res.status == NFS4_OK) { 8243 resop++; /* getattr res */ 8244 e.error = nfs4_update_attrcache(res.status, 8245 &resop->nfs_resop4_u.opgetattr.ga_res, 8246 t, ovp, cr); 8247 } 8248 8249 out: 8250 kmem_free(argop, argoplist_size); 8251 if (resp) 8252 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8253 mutex_enter(&orp->r_statelock); 8254 orp->r_flags &= ~R4RECEXPFH; 8255 cv_broadcast(&orp->r_cv); 8256 mutex_exit(&orp->r_statelock); 8257 8258 return (e.error); 8259 } 8260 8261 static int 8262 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr) 8263 { 8264 int error; 8265 vnode_t *vp; 8266 8267 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 8268 return (EPERM); 8269 /* 8270 * As ".." has special meaning and rather than send a mkdir 8271 * over the wire to just let the server freak out, we just 8272 * short circuit it here and return EEXIST 8273 */ 8274 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8275 return (EEXIST); 8276 8277 /* 8278 * Decision to get the right gid and setgid bit of the 8279 * new directory is now made in call_nfs4_create_req. 8280 */ 8281 va->va_mask |= AT_MODE; 8282 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8283 if (error) 8284 return (error); 8285 8286 *vpp = vp; 8287 return (0); 8288 } 8289 8290 8291 /* 8292 * rmdir is using the same remove v4 op as does remove. 8293 * Remove requires that the current fh be the target directory. 8294 * After the operation, the current fh is unchanged. 8295 * The compound op structure is: 8296 * PUTFH(targetdir), REMOVE 8297 */ 8298 static int 8299 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr) 8300 { 8301 int need_end_op = FALSE; 8302 COMPOUND4args_clnt args; 8303 COMPOUND4res_clnt res, *resp = NULL; 8304 REMOVE4res *rm_res; 8305 nfs_argop4 argop[3]; 8306 nfs_resop4 *resop; 8307 vnode_t *vp; 8308 int doqueue; 8309 mntinfo4_t *mi; 8310 rnode4_t *drp; 8311 bool_t needrecov = FALSE; 8312 nfs4_recov_state_t recov_state; 8313 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8314 dirattr_info_t dinfo, *dinfop; 8315 8316 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 8317 return (EPERM); 8318 /* 8319 * As ".." has special meaning and rather than send a rmdir 8320 * over the wire to just let the server freak out, we just 8321 * short circuit it here and return EEXIST 8322 */ 8323 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8324 return (EEXIST); 8325 8326 drp = VTOR4(dvp); 8327 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8328 return (EINTR); 8329 8330 /* 8331 * Attempt to prevent a rmdir(".") from succeeding. 8332 */ 8333 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8334 if (e.error) { 8335 nfs_rw_exit(&drp->r_rwlock); 8336 return (e.error); 8337 } 8338 if (vp == cdir) { 8339 VN_RELE(vp); 8340 nfs_rw_exit(&drp->r_rwlock); 8341 return (EINVAL); 8342 } 8343 8344 /* 8345 * Since nfsv4 remove op works on both files and directories, 8346 * check that the removed object is indeed a directory. 8347 */ 8348 if (vp->v_type != VDIR) { 8349 VN_RELE(vp); 8350 nfs_rw_exit(&drp->r_rwlock); 8351 return (ENOTDIR); 8352 } 8353 8354 /* 8355 * First just remove the entry from the name cache, as it 8356 * is most likely an entry for this vp. 8357 */ 8358 dnlc_remove(dvp, nm); 8359 8360 /* 8361 * If there vnode reference count is greater than one, then 8362 * there may be additional references in the DNLC which will 8363 * need to be purged. First, trying removing the entry for 8364 * the parent directory and see if that removes the additional 8365 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8366 * to completely remove any references to the directory which 8367 * might still exist in the DNLC. 8368 */ 8369 if (vp->v_count > 1) { 8370 dnlc_remove(vp, ".."); 8371 if (vp->v_count > 1) 8372 dnlc_purge_vp(vp); 8373 } 8374 8375 mi = VTOMI4(dvp); 8376 recov_state.rs_flags = 0; 8377 recov_state.rs_num_retry_despite_err = 0; 8378 8379 recov_retry: 8380 args.ctag = TAG_RMDIR; 8381 8382 /* 8383 * Rmdir ops: putfh dir; remove 8384 */ 8385 args.array_len = 3; 8386 args.array = argop; 8387 8388 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8389 if (e.error) { 8390 nfs_rw_exit(&drp->r_rwlock); 8391 return (e.error); 8392 } 8393 need_end_op = TRUE; 8394 8395 /* putfh directory */ 8396 argop[0].argop = OP_CPUTFH; 8397 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8398 8399 /* remove */ 8400 argop[1].argop = OP_CREMOVE; 8401 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8402 8403 /* getattr (postop attrs for dir that contained removed dir) */ 8404 argop[2].argop = OP_GETATTR; 8405 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8406 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8407 8408 dinfo.di_time_call = gethrtime(); 8409 doqueue = 1; 8410 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8411 8412 PURGE_ATTRCACHE4(vp); 8413 8414 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8415 if (e.error) { 8416 PURGE_ATTRCACHE4(dvp); 8417 } 8418 8419 if (needrecov) { 8420 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8421 NULL, OP_REMOVE, NULL) == FALSE) { 8422 if (!e.error) 8423 (void) xdr_free(xdr_COMPOUND4res_clnt, 8424 (caddr_t)&res); 8425 8426 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8427 needrecov); 8428 need_end_op = FALSE; 8429 goto recov_retry; 8430 } 8431 } 8432 8433 if (!e.error) { 8434 resp = &res; 8435 8436 /* 8437 * Only return error if first 2 ops (OP_REMOVE or earlier) 8438 * failed. 8439 */ 8440 if (res.status != NFS4_OK && res.array_len <= 2) { 8441 e.error = geterrno4(res.status); 8442 PURGE_ATTRCACHE4(dvp); 8443 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8444 &recov_state, needrecov); 8445 need_end_op = FALSE; 8446 nfs4_purge_stale_fh(e.error, dvp, cr); 8447 /* 8448 * System V defines rmdir to return EEXIST, not 8449 * ENOTEMPTY if the directory is not empty. Over 8450 * the wire, the error is NFSERR_ENOTEMPTY which 8451 * geterrno4 maps to ENOTEMPTY. 8452 */ 8453 if (e.error == ENOTEMPTY) 8454 e.error = EEXIST; 8455 } else { 8456 resop = &res.array[1]; /* remove res */ 8457 rm_res = &resop->nfs_resop4_u.opremove; 8458 8459 if (res.status == NFS4_OK) { 8460 resop = &res.array[2]; /* dir attrs */ 8461 dinfo.di_garp = 8462 &resop->nfs_resop4_u.opgetattr.ga_res; 8463 dinfo.di_cred = cr; 8464 dinfop = &dinfo; 8465 } else 8466 dinfop = NULL; 8467 8468 /* Update dir attribute, readdir and dnlc caches */ 8469 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8470 dinfop); 8471 8472 /* destroy rddir cache for dir that was removed */ 8473 if (VTOR4(vp)->r_dir != NULL) 8474 nfs4_purge_rddir_cache(vp); 8475 } 8476 } 8477 8478 if (need_end_op) 8479 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8480 8481 nfs_rw_exit(&drp->r_rwlock); 8482 8483 if (resp) 8484 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8485 8486 VN_RELE(vp); 8487 8488 return (e.error); 8489 } 8490 8491 static int 8492 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr) 8493 { 8494 int error; 8495 vnode_t *vp; 8496 rnode4_t *rp; 8497 char *contents; 8498 mntinfo4_t *mi = VTOMI4(dvp); 8499 8500 if (curproc->p_zone != mi->mi_zone) 8501 return (EPERM); 8502 if (!(mi->mi_flags & MI4_SYMLINK)) 8503 return (EOPNOTSUPP); 8504 8505 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8506 if (error) { 8507 return (error); 8508 } 8509 8510 ASSERT(nfs4_consistent_type(vp)); 8511 rp = VTOR4(vp); 8512 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8513 8514 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8515 8516 if (contents != NULL) { 8517 mutex_enter(&rp->r_statelock); 8518 if (rp->r_symlink.contents == NULL) { 8519 rp->r_symlink.len = strlen(tnm); 8520 bcopy(tnm, contents, rp->r_symlink.len); 8521 rp->r_symlink.contents = contents; 8522 rp->r_symlink.size = MAXPATHLEN; 8523 mutex_exit(&rp->r_statelock); 8524 } else { 8525 mutex_exit(&rp->r_statelock); 8526 kmem_free((void *)contents, MAXPATHLEN); 8527 } 8528 } 8529 } 8530 VN_RELE(vp); 8531 8532 return (error); 8533 } 8534 8535 8536 /* 8537 * Read directory entries. 8538 * There are some weird things to look out for here. The uio_loffset 8539 * field is either 0 or it is the offset returned from a previous 8540 * readdir. It is an opaque value used by the server to find the 8541 * correct directory block to read. The count field is the number 8542 * of blocks to read on the server. This is advisory only, the server 8543 * may return only one block's worth of entries. Entries may be compressed 8544 * on the server. 8545 */ 8546 static int 8547 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp) 8548 { 8549 int error; 8550 uint_t count; 8551 rnode4_t *rp; 8552 rddir4_cache *rdc; 8553 rddir4_cache *rrdc; 8554 8555 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 8556 return (EIO); 8557 rp = VTOR4(vp); 8558 8559 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8560 8561 /* 8562 * Make sure that the directory cache is valid. 8563 */ 8564 if (rp->r_dir != NULL) { 8565 if (nfs_disable_rddir_cache != 0) { 8566 /* 8567 * Setting nfs_disable_rddir_cache in /etc/system 8568 * allows interoperability with servers that do not 8569 * properly update the attributes of directories. 8570 * Any cached information gets purged before an 8571 * access is made to it. 8572 */ 8573 nfs4_purge_rddir_cache(vp); 8574 } 8575 8576 error = nfs4_validate_caches(vp, cr); 8577 if (error) 8578 return (error); 8579 } 8580 8581 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8582 8583 /* 8584 * Short circuit last readdir which always returns 0 bytes. 8585 * This can be done after the directory has been read through 8586 * completely at least once. This will set r_direof which 8587 * can be used to find the value of the last cookie. 8588 */ 8589 mutex_enter(&rp->r_statelock); 8590 if (rp->r_direof != NULL && 8591 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8592 mutex_exit(&rp->r_statelock); 8593 #ifdef DEBUG 8594 nfs4_readdir_cache_shorts++; 8595 #endif 8596 if (eofp) 8597 *eofp = 1; 8598 return (0); 8599 } 8600 8601 /* 8602 * Look for a cache entry. Cache entries are identified 8603 * by the NFS cookie value and the byte count requested. 8604 */ 8605 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8606 8607 /* 8608 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8609 */ 8610 if (rdc == NULL) { 8611 mutex_exit(&rp->r_statelock); 8612 return (EINTR); 8613 } 8614 8615 /* 8616 * Check to see if we need to fill this entry in. 8617 */ 8618 if (rdc->flags & RDDIRREQ) { 8619 rdc->flags &= ~RDDIRREQ; 8620 rdc->flags |= RDDIR; 8621 mutex_exit(&rp->r_statelock); 8622 8623 /* 8624 * Do the readdir. 8625 */ 8626 nfs4readdir(vp, rdc, cr); 8627 8628 /* 8629 * Reaquire the lock, so that we can continue 8630 */ 8631 mutex_enter(&rp->r_statelock); 8632 /* 8633 * The entry is now complete 8634 */ 8635 rdc->flags &= ~RDDIR; 8636 } 8637 8638 ASSERT(!(rdc->flags & RDDIR)); 8639 8640 /* 8641 * If an error occurred while attempting 8642 * to fill the cache entry, mark the entry invalid and 8643 * just return the error. 8644 */ 8645 if (rdc->error) { 8646 error = rdc->error; 8647 rdc->flags |= RDDIRREQ; 8648 rddir4_cache_rele(rp, rdc); 8649 mutex_exit(&rp->r_statelock); 8650 return (error); 8651 } 8652 8653 /* 8654 * The cache entry is complete and good, 8655 * copyout the dirent structs to the calling 8656 * thread. 8657 */ 8658 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8659 8660 /* 8661 * If no error occurred during the copyout, 8662 * update the offset in the uio struct to 8663 * contain the value of the next NFS 4 cookie 8664 * and set the eof value appropriately. 8665 */ 8666 if (!error) { 8667 uiop->uio_loffset = rdc->nfs4_ncookie; 8668 if (eofp) 8669 *eofp = rdc->eof; 8670 } 8671 8672 /* 8673 * Decide whether to do readahead. Don't if we 8674 * have already read to the end of directory. 8675 */ 8676 if (rdc->eof) { 8677 /* 8678 * Make the entry the direof only if it is cached 8679 */ 8680 if (rdc->flags & RDDIRCACHED) 8681 rp->r_direof = rdc; 8682 rddir4_cache_rele(rp, rdc); 8683 mutex_exit(&rp->r_statelock); 8684 return (error); 8685 } 8686 8687 /* Determine if a readdir readahead should be done */ 8688 if (!(rp->r_flags & R4LOOKUP)) { 8689 rddir4_cache_rele(rp, rdc); 8690 mutex_exit(&rp->r_statelock); 8691 return (error); 8692 } 8693 8694 /* 8695 * Now look for a readahead entry. 8696 * 8697 * Check to see whether we found an entry for the readahead. 8698 * If so, we don't need to do anything further, so free the new 8699 * entry if one was allocated. Otherwise, allocate a new entry, add 8700 * it to the cache, and then initiate an asynchronous readdir 8701 * operation to fill it. 8702 */ 8703 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 8704 8705 /* 8706 * A readdir cache entry could not be obtained for the readahead. In 8707 * this case we skip the readahead and return. 8708 */ 8709 if (rrdc == NULL) { 8710 rddir4_cache_rele(rp, rdc); 8711 mutex_exit(&rp->r_statelock); 8712 return (error); 8713 } 8714 8715 /* 8716 * Check to see if we need to fill this entry in. 8717 */ 8718 if (rrdc->flags & RDDIRREQ) { 8719 rrdc->flags &= ~RDDIRREQ; 8720 rrdc->flags |= RDDIR; 8721 rddir4_cache_rele(rp, rdc); 8722 mutex_exit(&rp->r_statelock); 8723 #ifdef DEBUG 8724 nfs4_readdir_readahead++; 8725 #endif 8726 /* 8727 * Do the readdir. 8728 */ 8729 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 8730 return (error); 8731 } 8732 8733 rddir4_cache_rele(rp, rrdc); 8734 rddir4_cache_rele(rp, rdc); 8735 mutex_exit(&rp->r_statelock); 8736 return (error); 8737 } 8738 8739 static int 8740 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8741 { 8742 int error; 8743 rnode4_t *rp; 8744 8745 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 8746 8747 rp = VTOR4(vp); 8748 8749 /* 8750 * Obtain the readdir results for the caller. 8751 */ 8752 nfs4readdir(vp, rdc, cr); 8753 8754 mutex_enter(&rp->r_statelock); 8755 /* 8756 * The entry is now complete 8757 */ 8758 rdc->flags &= ~RDDIR; 8759 8760 error = rdc->error; 8761 if (error) 8762 rdc->flags |= RDDIRREQ; 8763 rddir4_cache_rele(rp, rdc); 8764 mutex_exit(&rp->r_statelock); 8765 8766 return (error); 8767 } 8768 8769 static void 8770 nfs4readdir_stub(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8771 { 8772 int stublength; 8773 dirent64_t *dp; 8774 u_longlong_t nodeid, pnodeid; 8775 vnode_t *dotdotvp = NULL; 8776 rnode4_t *rp = VTOR4(vp); 8777 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8778 8779 rdc->error = 0; 8780 rdc->entries = 0; 8781 rdc->actlen = rdc->entlen = 0; 8782 rdc->eof = TRUE; 8783 8784 /* Check for EOF case for readdir of stub */ 8785 if (cookie != 0 && cookie != 1) 8786 return; 8787 8788 nodeid = rp->r_attr.va_nodeid; 8789 if (vp->v_flag & VROOT) { 8790 pnodeid = nodeid; /* root of mount point */ 8791 } else { 8792 if (rdc->error = nfs4_lookup(vp, "..", &dotdotvp, 0, 0, 0, cr)) 8793 return; 8794 pnodeid = VTOR4(dotdotvp)->r_attr.va_nodeid; 8795 VN_RELE(dotdotvp); 8796 } 8797 8798 stublength = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8799 rdc->entries = kmem_alloc(stublength, KM_SLEEP); 8800 rdc->entlen = rdc->buflen = stublength; 8801 rdc->eof = TRUE; 8802 8803 dp = (dirent64_t *)rdc->entries; 8804 8805 if (rdc->nfs4_cookie == (nfs_cookie4)0) { 8806 bcopy(nfs4_dot_entries, rdc->entries, 8807 DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2)); 8808 dp->d_ino = nodeid; 8809 dp = (struct dirent64 *)(((char *)dp) + DIRENT64_RECLEN(1)); 8810 dp->d_ino = pnodeid; 8811 rdc->actlen = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8812 } else { /* for ".." entry */ 8813 bcopy(nfs4_dot_dot_entry, rdc->entries, DIRENT64_RECLEN(2)); 8814 dp->d_ino = pnodeid; 8815 rdc->actlen = DIRENT64_RECLEN(2); 8816 } 8817 rdc->nfs4_ncookie = rdc->actlen; 8818 } 8819 8820 /* 8821 * Read directory entries. 8822 * There are some weird things to look out for here. The uio_loffset 8823 * field is either 0 or it is the offset returned from a previous 8824 * readdir. It is an opaque value used by the server to find the 8825 * correct directory block to read. The count field is the number 8826 * of blocks to read on the server. This is advisory only, the server 8827 * may return only one block's worth of entries. Entries may be compressed 8828 * on the server. 8829 * 8830 * Generates the following compound request: 8831 * 1. If readdir offset is zero and no dnlc entry for parent exists, 8832 * must include a Lookupp as well. In this case, send: 8833 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 8834 * 2. Otherwise just do: { Putfh <fh>; Readdir } 8835 * 8836 * Get complete attributes and filehandles for entries if this is the 8837 * first read of the directory. Otherwise, just get fileid's. 8838 */ 8839 static void 8840 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8841 { 8842 COMPOUND4args_clnt args; 8843 COMPOUND4res_clnt res; 8844 READDIR4args *rargs; 8845 READDIR4res_clnt *rd_res; 8846 bitmap4 rd_bitsval; 8847 nfs_argop4 argop[5]; 8848 nfs_resop4 *resop; 8849 rnode4_t *rp = VTOR4(vp); 8850 mntinfo4_t *mi = VTOMI4(vp); 8851 int doqueue; 8852 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 8853 vnode_t *dvp; 8854 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8855 int num_ops, res_opcnt; 8856 bool_t needrecov = FALSE; 8857 nfs4_recov_state_t recov_state; 8858 hrtime_t t; 8859 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8860 8861 ASSERT(curproc->p_zone == mi->mi_zone); 8862 ASSERT(rdc->flags & RDDIR); 8863 ASSERT(rdc->entries == NULL); 8864 8865 if (rp->r_flags & R4SRVSTUB) { 8866 nfs4readdir_stub(vp, rdc, cr); 8867 return; 8868 } 8869 8870 num_ops = 2; 8871 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 8872 /* 8873 * Since nfsv4 readdir may not return entries for "." and "..", 8874 * the client must recreate them: 8875 * To find the correct nodeid, do the following: 8876 * For current node, get nodeid from dnlc. 8877 * - if current node is rootvp, set pnodeid to nodeid. 8878 * - else if parent is in the dnlc, get its nodeid from there. 8879 * - else add LOOKUPP+GETATTR to compound. 8880 */ 8881 nodeid = rp->r_attr.va_nodeid; 8882 if (vp->v_flag & VROOT) { 8883 pnodeid = nodeid; /* root of mount point */ 8884 } else { 8885 dvp = dnlc_lookup(vp, ".."); 8886 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 8887 /* parent in dnlc cache - no need for otw */ 8888 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 8889 } else { 8890 /* 8891 * parent not in dnlc cache, 8892 * do lookupp to get its id 8893 */ 8894 num_ops = 5; 8895 pnodeid = 0; /* set later by getattr parent */ 8896 } 8897 if (dvp) 8898 VN_RELE(dvp); 8899 } 8900 } 8901 recov_state.rs_flags = 0; 8902 recov_state.rs_num_retry_despite_err = 0; 8903 8904 /* Save the original mount point security flavor */ 8905 (void) save_mnt_secinfo(mi->mi_curr_serv); 8906 8907 recov_retry: 8908 args.ctag = TAG_READDIR; 8909 8910 args.array = argop; 8911 args.array_len = num_ops; 8912 8913 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 8914 &recov_state, NULL)) { 8915 /* 8916 * If readdir a node that is a stub for a crossed mount point, 8917 * keep the original secinfo flavor for the current file 8918 * system, not the crossed one. 8919 */ 8920 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 8921 rdc->error = e.error; 8922 return; 8923 } 8924 8925 /* 8926 * Determine which attrs to request for dirents. This code 8927 * must be protected by nfs4_start/end_fop because of r_server 8928 * (which will change during failover recovery). 8929 * 8930 */ 8931 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 8932 /* 8933 * Get all vattr attrs plus filehandle and rdattr_error 8934 */ 8935 rd_bitsval = NFS4_VATTR_MASK | 8936 FATTR4_RDATTR_ERROR_MASK | 8937 FATTR4_FILEHANDLE_MASK; 8938 8939 if (rp->r_flags & R4READDIRWATTR) { 8940 mutex_enter(&rp->r_statelock); 8941 rp->r_flags &= ~R4READDIRWATTR; 8942 mutex_exit(&rp->r_statelock); 8943 } 8944 } else { 8945 servinfo4_t *svp = rp->r_server; 8946 8947 /* 8948 * Already read directory. Use readdir with 8949 * no attrs (except for mounted_on_fileid) for updates. 8950 */ 8951 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 8952 8953 /* 8954 * request mounted on fileid if supported, else request 8955 * fileid. maybe we should verify that fileid is supported 8956 * and request something else if not. 8957 */ 8958 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 8959 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 8960 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 8961 nfs_rw_exit(&svp->sv_lock); 8962 } 8963 8964 /* putfh directory fh */ 8965 argop[0].argop = OP_CPUTFH; 8966 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 8967 8968 argop[1].argop = OP_READDIR; 8969 rargs = &argop[1].nfs_argop4_u.opreaddir; 8970 /* 8971 * 1 and 2 are reserved for client "." and ".." entry offset. 8972 * cookie 0 should be used over-the-wire to start reading at 8973 * the beginning of the directory excluding "." and "..". 8974 */ 8975 if (rdc->nfs4_cookie == 0 || 8976 rdc->nfs4_cookie == 1 || 8977 rdc->nfs4_cookie == 2) { 8978 rargs->cookie = (nfs_cookie4)0; 8979 rargs->cookieverf = 0; 8980 } else { 8981 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 8982 mutex_enter(&rp->r_statelock); 8983 rargs->cookieverf = rp->r_cookieverf4; 8984 mutex_exit(&rp->r_statelock); 8985 } 8986 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 8987 rargs->maxcount = mi->mi_tsize; 8988 rargs->attr_request = rd_bitsval; 8989 rargs->rdc = rdc; 8990 rargs->dvp = vp; 8991 rargs->mi = mi; 8992 rargs->cr = cr; 8993 8994 8995 /* 8996 * If count < than the minimum required, we return no entries 8997 * and fail with EINVAL 8998 */ 8999 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9000 rdc->error = EINVAL; 9001 goto out; 9002 } 9003 9004 if (args.array_len == 5) { 9005 /* 9006 * Add lookupp and getattr for parent nodeid. 9007 */ 9008 argop[2].argop = OP_LOOKUPP; 9009 9010 argop[3].argop = OP_GETFH; 9011 9012 /* getattr parent */ 9013 argop[4].argop = OP_GETATTR; 9014 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9015 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9016 } 9017 9018 doqueue = 1; 9019 9020 if (mi->mi_io_kstats) { 9021 mutex_enter(&mi->mi_lock); 9022 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9023 mutex_exit(&mi->mi_lock); 9024 } 9025 9026 /* capture the time of this call */ 9027 rargs->t = t = gethrtime(); 9028 9029 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9030 9031 if (mi->mi_io_kstats) { 9032 mutex_enter(&mi->mi_lock); 9033 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9034 mutex_exit(&mi->mi_lock); 9035 } 9036 9037 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9038 9039 /* 9040 * If RPC error occurred and it isn't an error that 9041 * triggers recovery, then go ahead and fail now. 9042 */ 9043 if (e.error != 0 && !needrecov) { 9044 rdc->error = e.error; 9045 goto out; 9046 } 9047 9048 if (needrecov) { 9049 bool_t abort; 9050 9051 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9052 "nfs4readdir: initiating recovery.\n")); 9053 9054 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9055 NULL, OP_READDIR, NULL); 9056 if (abort == FALSE) { 9057 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9058 &recov_state, needrecov); 9059 if (!e.error) 9060 (void) xdr_free(xdr_COMPOUND4res_clnt, 9061 (caddr_t)&res); 9062 if (rdc->entries != NULL) { 9063 kmem_free(rdc->entries, rdc->entlen); 9064 rdc->entries = NULL; 9065 } 9066 goto recov_retry; 9067 } 9068 9069 if (e.error != 0) { 9070 rdc->error = e.error; 9071 goto out; 9072 } 9073 9074 /* fall through for res.status case */ 9075 } 9076 9077 res_opcnt = res.array_len; 9078 9079 /* 9080 * If compound failed first 2 ops (PUTFH+READDIR), then return 9081 * failure here. Subsequent ops are for filling out dot-dot 9082 * dirent, and if they fail, we still want to give the caller 9083 * the dirents returned by (the successful) READDIR op, so we need 9084 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9085 * 9086 * One example where PUTFH+READDIR ops would succeed but 9087 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9088 * but lacks x. In this case, a POSIX server's VOP_READDIR 9089 * would succeed; however, VOP_LOOKUP(..) would fail since no 9090 * x perm. We need to come up with a non-vendor-specific way 9091 * for a POSIX server to return d_ino from dotdot's dirent if 9092 * client only requests mounted_on_fileid, and just say the 9093 * LOOKUPP succeeded and fill out the GETATTR. However, if 9094 * client requested any mandatory attrs, server would be required 9095 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9096 * for dotdot. 9097 */ 9098 9099 if (res.status) { 9100 if (res_opcnt <= 2) { 9101 e.error = geterrno4(res.status); 9102 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9103 &recov_state, needrecov); 9104 nfs4_purge_stale_fh(e.error, vp, cr); 9105 rdc->error = e.error; 9106 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9107 if (rdc->entries != NULL) { 9108 kmem_free(rdc->entries, rdc->entlen); 9109 rdc->entries = NULL; 9110 } 9111 /* 9112 * If readdir a node that is a stub for a 9113 * crossed mount point, keep the original 9114 * secinfo flavor for the current file system, 9115 * not the crossed one. 9116 */ 9117 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9118 return; 9119 } 9120 } 9121 9122 resop = &res.array[1]; /* readdir res */ 9123 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9124 9125 mutex_enter(&rp->r_statelock); 9126 rp->r_cookieverf4 = rd_res->cookieverf; 9127 mutex_exit(&rp->r_statelock); 9128 9129 /* 9130 * For "." and ".." entries 9131 * e.g. 9132 * seek(cookie=0) -> "." entry with d_off = 1 9133 * seek(cookie=1) -> ".." entry with d_off = 2 9134 */ 9135 if (cookie == (nfs_cookie4) 0) { 9136 if (rd_res->dotp) 9137 rd_res->dotp->d_ino = nodeid; 9138 if (rd_res->dotdotp) 9139 rd_res->dotdotp->d_ino = pnodeid; 9140 } 9141 if (cookie == (nfs_cookie4) 1) { 9142 if (rd_res->dotdotp) 9143 rd_res->dotdotp->d_ino = pnodeid; 9144 } 9145 9146 9147 /* LOOKUPP+GETATTR attemped */ 9148 if (args.array_len == 5 && rd_res->dotdotp) { 9149 if (res.status == NFS4_OK && res_opcnt == 5) { 9150 nfs_fh4 *fhp; 9151 nfs4_sharedfh_t *sfhp; 9152 vnode_t *pvp; 9153 nfs4_ga_res_t *garp; 9154 9155 resop++; /* lookupp */ 9156 resop++; /* getfh */ 9157 fhp = &resop->nfs_resop4_u.opgetfh.object; 9158 9159 resop++; /* getattr of parent */ 9160 9161 /* 9162 * First, take care of finishing the 9163 * readdir results. 9164 */ 9165 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9166 /* 9167 * The d_ino of .. must be the inode number 9168 * of the mounted filesystem. 9169 */ 9170 if (garp->n4g_va.va_mask & AT_NODEID) 9171 rd_res->dotdotp->d_ino = 9172 garp->n4g_va.va_nodeid; 9173 9174 9175 /* 9176 * Next, create the ".." dnlc entry 9177 */ 9178 sfhp = sfh4_get(fhp, mi); 9179 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9180 dnlc_update(vp, "..", pvp); 9181 VN_RELE(pvp); 9182 } 9183 sfh4_rele(&sfhp); 9184 } 9185 } 9186 9187 if (mi->mi_io_kstats) { 9188 mutex_enter(&mi->mi_lock); 9189 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9190 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9191 mutex_exit(&mi->mi_lock); 9192 } 9193 9194 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9195 9196 out: 9197 /* 9198 * If readdir a node that is a stub for a crossed mount point, 9199 * keep the original secinfo flavor for the current file system, 9200 * not the crossed one. 9201 */ 9202 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9203 9204 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9205 } 9206 9207 9208 static int 9209 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9210 { 9211 rnode4_t *rp = VTOR4(bp->b_vp); 9212 int count; 9213 int error; 9214 cred_t *cred_otw = NULL; 9215 offset_t offset; 9216 nfs4_open_stream_t *osp = NULL; 9217 bool_t first_time = TRUE; /* first time getting otw cred */ 9218 bool_t last_time = FALSE; /* last time getting otw cred */ 9219 9220 ASSERT(curproc->p_zone == VTOMI4(bp->b_vp)->mi_zone); 9221 9222 DTRACE_IO1(start, struct buf *, bp); 9223 offset = ldbtob(bp->b_lblkno); 9224 9225 if (bp->b_flags & B_READ) { 9226 read_again: 9227 /* 9228 * Releases the osp, if it is provided. 9229 * Puts a hold on the cred_otw and the new osp (if found). 9230 */ 9231 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9232 &first_time, &last_time); 9233 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9234 offset, bp->b_bcount, 9235 &bp->b_resid, cred_otw, 9236 readahead, NULL); 9237 crfree(cred_otw); 9238 if (!error) { 9239 if (bp->b_resid) { 9240 /* 9241 * Didn't get it all because we hit EOF, 9242 * zero all the memory beyond the EOF. 9243 */ 9244 /* bzero(rdaddr + */ 9245 bzero(bp->b_un.b_addr + 9246 bp->b_bcount - bp->b_resid, bp->b_resid); 9247 } 9248 mutex_enter(&rp->r_statelock); 9249 if (bp->b_resid == bp->b_bcount && 9250 offset >= rp->r_size) { 9251 /* 9252 * We didn't read anything at all as we are 9253 * past EOF. Return an error indicator back 9254 * but don't destroy the pages (yet). 9255 */ 9256 error = NFS_EOF; 9257 } 9258 mutex_exit(&rp->r_statelock); 9259 } else if (error == EACCES && last_time == FALSE) { 9260 goto read_again; 9261 } 9262 } else { 9263 if (!(rp->r_flags & R4STALE)) { 9264 write_again: 9265 /* 9266 * Releases the osp, if it is provided. 9267 * Puts a hold on the cred_otw and the new 9268 * osp (if found). 9269 */ 9270 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9271 &first_time, &last_time); 9272 mutex_enter(&rp->r_statelock); 9273 count = MIN(bp->b_bcount, rp->r_size - offset); 9274 mutex_exit(&rp->r_statelock); 9275 if (count < 0) 9276 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9277 #ifdef DEBUG 9278 if (count == 0) { 9279 zoneid_t zoneid = getzoneid(); 9280 9281 zcmn_err(zoneid, CE_WARN, 9282 "nfs4_bio: zero length write at %lld", 9283 offset); 9284 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9285 "b_bcount=%ld, file size=%lld", 9286 rp->r_flags, (long)bp->b_bcount, 9287 rp->r_size); 9288 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9289 if (nfs4_bio_do_stop) 9290 debug_enter("nfs4_bio"); 9291 } 9292 #endif 9293 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9294 count, cred_otw, stab_comm); 9295 if (error == EACCES && last_time == FALSE) { 9296 crfree(cred_otw); 9297 goto write_again; 9298 } 9299 bp->b_error = error; 9300 if (error && error != EINTR && 9301 !(bp->b_vp->v_vfsp->vfs_flag && VFS_UNMOUNTED)) { 9302 /* 9303 * Don't print EDQUOT errors on the console. 9304 * Don't print asynchronous EACCES errors. 9305 * Don't print EFBIG errors. 9306 * Print all other write errors. 9307 */ 9308 if (error != EDQUOT && error != EFBIG && 9309 (error != EACCES || 9310 !(bp->b_flags & B_ASYNC))) 9311 nfs4_write_error(bp->b_vp, 9312 error, cred_otw); 9313 /* 9314 * Update r_error and r_flags as appropriate. 9315 * If the error was ESTALE, then mark the 9316 * rnode as not being writeable and save 9317 * the error status. Otherwise, save any 9318 * errors which occur from asynchronous 9319 * page invalidations. Any errors occurring 9320 * from other operations should be saved 9321 * by the caller. 9322 */ 9323 mutex_enter(&rp->r_statelock); 9324 if (error == ESTALE) { 9325 rp->r_flags |= R4STALE; 9326 if (!rp->r_error) 9327 rp->r_error = error; 9328 } else if (!rp->r_error && 9329 (bp->b_flags & 9330 (B_INVAL|B_FORCE|B_ASYNC)) == 9331 (B_INVAL|B_FORCE|B_ASYNC)) { 9332 rp->r_error = error; 9333 } 9334 mutex_exit(&rp->r_statelock); 9335 } 9336 crfree(cred_otw); 9337 } else 9338 error = rp->r_error; 9339 } 9340 9341 if (error != 0 && error != NFS_EOF) 9342 bp->b_flags |= B_ERROR; 9343 9344 if (osp) 9345 open_stream_rele(osp, rp); 9346 9347 DTRACE_IO1(done, struct buf *, bp); 9348 9349 return (error); 9350 } 9351 9352 /* ARGSUSED */ 9353 static int 9354 nfs4_fid(vnode_t *vp, fid_t *fidp) 9355 { 9356 return (EREMOTE); 9357 } 9358 9359 /* ARGSUSED2 */ 9360 static int 9361 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9362 { 9363 rnode4_t *rp = VTOR4(vp); 9364 9365 if (!write_lock) { 9366 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9367 return (V_WRITELOCK_FALSE); 9368 } 9369 9370 if ((rp->r_flags & R4DIRECTIO) || 9371 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9372 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9373 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9374 return (V_WRITELOCK_FALSE); 9375 nfs_rw_exit(&rp->r_rwlock); 9376 } 9377 9378 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9379 return (V_WRITELOCK_TRUE); 9380 } 9381 9382 /* ARGSUSED */ 9383 static void 9384 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9385 { 9386 rnode4_t *rp = VTOR4(vp); 9387 9388 nfs_rw_exit(&rp->r_rwlock); 9389 } 9390 9391 /* ARGSUSED */ 9392 static int 9393 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 9394 { 9395 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9396 return (EIO); 9397 9398 /* 9399 * Because we stuff the readdir cookie into the offset field 9400 * someone may attempt to do an lseek with the cookie which 9401 * we want to succeed. 9402 */ 9403 if (vp->v_type == VDIR) 9404 return (0); 9405 if (*noffp < 0) 9406 return (EINVAL); 9407 return (0); 9408 } 9409 9410 9411 /* 9412 * Return all the pages from [off..off+len) in file 9413 */ 9414 static int 9415 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9416 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9417 enum seg_rw rw, cred_t *cr) 9418 { 9419 rnode4_t *rp; 9420 int error; 9421 mntinfo4_t *mi; 9422 9423 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9424 return (EIO); 9425 rp = VTOR4(vp); 9426 if (IS_SHADOW(vp, rp)) 9427 vp = RTOV4(rp); 9428 9429 if (vp->v_flag & VNOMAP) 9430 return (ENOSYS); 9431 9432 if (protp != NULL) 9433 *protp = PROT_ALL; 9434 9435 /* 9436 * Now validate that the caches are up to date. 9437 */ 9438 if (error = nfs4_validate_caches(vp, cr)) 9439 return (error); 9440 9441 mi = VTOMI4(vp); 9442 retry: 9443 mutex_enter(&rp->r_statelock); 9444 9445 /* 9446 * Don't create dirty pages faster than they 9447 * can be cleaned so that the system doesn't 9448 * get imbalanced. If the async queue is 9449 * maxed out, then wait for it to drain before 9450 * creating more dirty pages. Also, wait for 9451 * any threads doing pagewalks in the vop_getattr 9452 * entry points so that they don't block for 9453 * long periods. 9454 */ 9455 if (rw == S_CREATE) { 9456 while ((mi->mi_max_threads != 0 && 9457 rp->r_awcount > 2 * mi->mi_max_threads) || 9458 rp->r_gcount > 0) 9459 cv_wait(&rp->r_cv, &rp->r_statelock); 9460 } 9461 9462 /* 9463 * If we are getting called as a side effect of an nfs_write() 9464 * operation the local file size might not be extended yet. 9465 * In this case we want to be able to return pages of zeroes. 9466 */ 9467 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9468 NFS4_DEBUG(nfs4_pageio_debug, 9469 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9470 "len=%llu, size=%llu, attrsize =%llu", off, 9471 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9472 mutex_exit(&rp->r_statelock); 9473 return (EFAULT); /* beyond EOF */ 9474 } 9475 9476 mutex_exit(&rp->r_statelock); 9477 9478 if (len <= PAGESIZE) { 9479 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9480 seg, addr, rw, cr); 9481 NFS4_DEBUG(nfs4_pageio_debug && error, 9482 (CE_NOTE, "getpage error %d; off=%lld, " 9483 "len=%lld", error, off, (u_longlong_t)len)); 9484 } else { 9485 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9486 pl, plsz, seg, addr, rw, cr); 9487 NFS4_DEBUG(nfs4_pageio_debug && error, 9488 (CE_NOTE, "getpages error %d; off=%lld, " 9489 "len=%lld", error, off, (u_longlong_t)len)); 9490 } 9491 9492 switch (error) { 9493 case NFS_EOF: 9494 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9495 goto retry; 9496 case ESTALE: 9497 nfs4_purge_stale_fh(error, vp, cr); 9498 } 9499 9500 return (error); 9501 } 9502 9503 /* 9504 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9505 */ 9506 /* ARGSUSED */ 9507 static int 9508 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9509 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9510 enum seg_rw rw, cred_t *cr) 9511 { 9512 rnode4_t *rp; 9513 uint_t bsize; 9514 struct buf *bp; 9515 page_t *pp; 9516 u_offset_t lbn; 9517 u_offset_t io_off; 9518 u_offset_t blkoff; 9519 u_offset_t rablkoff; 9520 size_t io_len; 9521 uint_t blksize; 9522 int error; 9523 int readahead; 9524 int readahead_issued = 0; 9525 int ra_window; /* readahead window */ 9526 page_t *pagefound; 9527 page_t *savepp; 9528 9529 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9530 return (EIO); 9531 9532 rp = VTOR4(vp); 9533 ASSERT(!IS_SHADOW(vp, rp)); 9534 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9535 9536 reread: 9537 bp = NULL; 9538 pp = NULL; 9539 pagefound = NULL; 9540 9541 if (pl != NULL) 9542 pl[0] = NULL; 9543 9544 error = 0; 9545 lbn = off / bsize; 9546 blkoff = lbn * bsize; 9547 9548 /* 9549 * Queueing up the readahead before doing the synchronous read 9550 * results in a significant increase in read throughput because 9551 * of the increased parallelism between the async threads and 9552 * the process context. 9553 */ 9554 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9555 rw != S_CREATE && 9556 !(vp->v_flag & VNOCACHE)) { 9557 mutex_enter(&rp->r_statelock); 9558 9559 /* 9560 * Calculate the number of readaheads to do. 9561 * a) No readaheads at offset = 0. 9562 * b) Do maximum(nfs4_nra) readaheads when the readahead 9563 * window is closed. 9564 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9565 * upon how far the readahead window is open or close. 9566 * d) No readaheads if rp->r_nextr is not within the scope 9567 * of the readahead window (random i/o). 9568 */ 9569 9570 if (off == 0) 9571 readahead = 0; 9572 else if (blkoff == rp->r_nextr) 9573 readahead = nfs4_nra; 9574 else if (rp->r_nextr > blkoff && 9575 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9576 <= (nfs4_nra - 1))) 9577 readahead = nfs4_nra - ra_window; 9578 else 9579 readahead = 0; 9580 9581 rablkoff = rp->r_nextr; 9582 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9583 mutex_exit(&rp->r_statelock); 9584 if (nfs4_async_readahead(vp, rablkoff + bsize, 9585 addr + (rablkoff + bsize - off), 9586 seg, cr, nfs4_readahead) < 0) { 9587 mutex_enter(&rp->r_statelock); 9588 break; 9589 } 9590 readahead--; 9591 rablkoff += bsize; 9592 /* 9593 * Indicate that we did a readahead so 9594 * readahead offset is not updated 9595 * by the synchronous read below. 9596 */ 9597 readahead_issued = 1; 9598 mutex_enter(&rp->r_statelock); 9599 /* 9600 * set readahead offset to 9601 * offset of last async readahead 9602 * request. 9603 */ 9604 rp->r_nextr = rablkoff; 9605 } 9606 mutex_exit(&rp->r_statelock); 9607 } 9608 9609 again: 9610 if ((pagefound = page_exists(vp, off)) == NULL) { 9611 if (pl == NULL) { 9612 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9613 nfs4_readahead); 9614 } else if (rw == S_CREATE) { 9615 /* 9616 * Block for this page is not allocated, or the offset 9617 * is beyond the current allocation size, or we're 9618 * allocating a swap slot and the page was not found, 9619 * so allocate it and return a zero page. 9620 */ 9621 if ((pp = page_create_va(vp, off, 9622 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9623 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9624 io_len = PAGESIZE; 9625 mutex_enter(&rp->r_statelock); 9626 rp->r_nextr = off + PAGESIZE; 9627 mutex_exit(&rp->r_statelock); 9628 } else { 9629 /* 9630 * Need to go to server to get a block 9631 */ 9632 mutex_enter(&rp->r_statelock); 9633 if (blkoff < rp->r_size && 9634 blkoff + bsize > rp->r_size) { 9635 /* 9636 * If less than a block left in 9637 * file read less than a block. 9638 */ 9639 if (rp->r_size <= off) { 9640 /* 9641 * Trying to access beyond EOF, 9642 * set up to get at least one page. 9643 */ 9644 blksize = off + PAGESIZE - blkoff; 9645 } else 9646 blksize = rp->r_size - blkoff; 9647 } else if ((off == 0) || 9648 (off != rp->r_nextr && !readahead_issued)) { 9649 blksize = PAGESIZE; 9650 blkoff = off; /* block = page here */ 9651 } else 9652 blksize = bsize; 9653 mutex_exit(&rp->r_statelock); 9654 9655 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9656 &io_len, blkoff, blksize, 0); 9657 9658 /* 9659 * Some other thread has entered the page, 9660 * so just use it. 9661 */ 9662 if (pp == NULL) 9663 goto again; 9664 9665 /* 9666 * Now round the request size up to page boundaries. 9667 * This ensures that the entire page will be 9668 * initialized to zeroes if EOF is encountered. 9669 */ 9670 io_len = ptob(btopr(io_len)); 9671 9672 bp = pageio_setup(pp, io_len, vp, B_READ); 9673 ASSERT(bp != NULL); 9674 9675 /* 9676 * pageio_setup should have set b_addr to 0. This 9677 * is correct since we want to do I/O on a page 9678 * boundary. bp_mapin will use this addr to calculate 9679 * an offset, and then set b_addr to the kernel virtual 9680 * address it allocated for us. 9681 */ 9682 ASSERT(bp->b_un.b_addr == 0); 9683 9684 bp->b_edev = 0; 9685 bp->b_dev = 0; 9686 bp->b_lblkno = lbtodb(io_off); 9687 bp->b_file = vp; 9688 bp->b_offset = (offset_t)off; 9689 bp_mapin(bp); 9690 9691 /* 9692 * If doing a write beyond what we believe is EOF, 9693 * don't bother trying to read the pages from the 9694 * server, we'll just zero the pages here. We 9695 * don't check that the rw flag is S_WRITE here 9696 * because some implementations may attempt a 9697 * read access to the buffer before copying data. 9698 */ 9699 mutex_enter(&rp->r_statelock); 9700 if (io_off >= rp->r_size && seg == segkmap) { 9701 mutex_exit(&rp->r_statelock); 9702 bzero(bp->b_un.b_addr, io_len); 9703 } else { 9704 mutex_exit(&rp->r_statelock); 9705 error = nfs4_bio(bp, NULL, cr, FALSE); 9706 } 9707 9708 /* 9709 * Unmap the buffer before freeing it. 9710 */ 9711 bp_mapout(bp); 9712 pageio_done(bp); 9713 9714 savepp = pp; 9715 do { 9716 pp->p_fsdata = C_NOCOMMIT; 9717 } while ((pp = pp->p_next) != savepp); 9718 9719 if (error == NFS_EOF) { 9720 /* 9721 * If doing a write system call just return 9722 * zeroed pages, else user tried to get pages 9723 * beyond EOF, return error. We don't check 9724 * that the rw flag is S_WRITE here because 9725 * some implementations may attempt a read 9726 * access to the buffer before copying data. 9727 */ 9728 if (seg == segkmap) 9729 error = 0; 9730 else 9731 error = EFAULT; 9732 } 9733 9734 if (!readahead_issued && !error) { 9735 mutex_enter(&rp->r_statelock); 9736 rp->r_nextr = io_off + io_len; 9737 mutex_exit(&rp->r_statelock); 9738 } 9739 } 9740 } 9741 9742 out: 9743 if (pl == NULL) 9744 return (error); 9745 9746 if (error) { 9747 if (pp != NULL) 9748 pvn_read_done(pp, B_ERROR); 9749 return (error); 9750 } 9751 9752 if (pagefound) { 9753 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 9754 9755 /* 9756 * Page exists in the cache, acquire the appropriate lock. 9757 * If this fails, start all over again. 9758 */ 9759 if ((pp = page_lookup(vp, off, se)) == NULL) { 9760 #ifdef DEBUG 9761 nfs4_lostpage++; 9762 #endif 9763 goto reread; 9764 } 9765 pl[0] = pp; 9766 pl[1] = NULL; 9767 return (0); 9768 } 9769 9770 if (pp != NULL) 9771 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 9772 9773 return (error); 9774 } 9775 9776 static void 9777 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 9778 cred_t *cr) 9779 { 9780 int error; 9781 page_t *pp; 9782 u_offset_t io_off; 9783 size_t io_len; 9784 struct buf *bp; 9785 uint_t bsize, blksize; 9786 rnode4_t *rp = VTOR4(vp); 9787 page_t *savepp; 9788 9789 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 9790 9791 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9792 9793 mutex_enter(&rp->r_statelock); 9794 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 9795 /* 9796 * If less than a block left in file read less 9797 * than a block. 9798 */ 9799 blksize = rp->r_size - blkoff; 9800 } else 9801 blksize = bsize; 9802 mutex_exit(&rp->r_statelock); 9803 9804 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 9805 &io_off, &io_len, blkoff, blksize, 1); 9806 /* 9807 * The isra flag passed to the kluster function is 1, we may have 9808 * gotten a return value of NULL for a variety of reasons (# of free 9809 * pages < minfree, someone entered the page on the vnode etc). In all 9810 * cases, we want to punt on the readahead. 9811 */ 9812 if (pp == NULL) 9813 return; 9814 9815 /* 9816 * Now round the request size up to page boundaries. 9817 * This ensures that the entire page will be 9818 * initialized to zeroes if EOF is encountered. 9819 */ 9820 io_len = ptob(btopr(io_len)); 9821 9822 bp = pageio_setup(pp, io_len, vp, B_READ); 9823 ASSERT(bp != NULL); 9824 9825 /* 9826 * pageio_setup should have set b_addr to 0. This is correct since 9827 * we want to do I/O on a page boundary. bp_mapin() will use this addr 9828 * to calculate an offset, and then set b_addr to the kernel virtual 9829 * address it allocated for us. 9830 */ 9831 ASSERT(bp->b_un.b_addr == 0); 9832 9833 bp->b_edev = 0; 9834 bp->b_dev = 0; 9835 bp->b_lblkno = lbtodb(io_off); 9836 bp->b_file = vp; 9837 bp->b_offset = (offset_t)blkoff; 9838 bp_mapin(bp); 9839 9840 /* 9841 * If doing a write beyond what we believe is EOF, don't bother trying 9842 * to read the pages from the server, we'll just zero the pages here. 9843 * We don't check that the rw flag is S_WRITE here because some 9844 * implementations may attempt a read access to the buffer before 9845 * copying data. 9846 */ 9847 mutex_enter(&rp->r_statelock); 9848 if (io_off >= rp->r_size && seg == segkmap) { 9849 mutex_exit(&rp->r_statelock); 9850 bzero(bp->b_un.b_addr, io_len); 9851 error = 0; 9852 } else { 9853 mutex_exit(&rp->r_statelock); 9854 error = nfs4_bio(bp, NULL, cr, TRUE); 9855 if (error == NFS_EOF) 9856 error = 0; 9857 } 9858 9859 /* 9860 * Unmap the buffer before freeing it. 9861 */ 9862 bp_mapout(bp); 9863 pageio_done(bp); 9864 9865 savepp = pp; 9866 do { 9867 pp->p_fsdata = C_NOCOMMIT; 9868 } while ((pp = pp->p_next) != savepp); 9869 9870 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 9871 9872 /* 9873 * In case of error set readahead offset 9874 * to the lowest offset. 9875 * pvn_read_done() calls VN_DISPOSE to destroy the pages 9876 */ 9877 if (error && rp->r_nextr > io_off) { 9878 mutex_enter(&rp->r_statelock); 9879 if (rp->r_nextr > io_off) 9880 rp->r_nextr = io_off; 9881 mutex_exit(&rp->r_statelock); 9882 } 9883 } 9884 9885 /* 9886 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 9887 * If len == 0, do from off to EOF. 9888 * 9889 * The normal cases should be len == 0 && off == 0 (entire vp list) or 9890 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 9891 * (from pageout). 9892 */ 9893 static int 9894 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 9895 { 9896 int error; 9897 rnode4_t *rp; 9898 9899 ASSERT(cr != NULL); 9900 9901 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI4(vp)->mi_zone) 9902 return (EIO); 9903 9904 rp = VTOR4(vp); 9905 if (IS_SHADOW(vp, rp)) 9906 vp = RTOV4(rp); 9907 9908 /* 9909 * XXX - Why should this check be made here? 9910 */ 9911 if (vp->v_flag & VNOMAP) 9912 return (ENOSYS); 9913 9914 if (len == 0 && !(flags & B_INVAL) && 9915 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 9916 return (0); 9917 9918 mutex_enter(&rp->r_statelock); 9919 rp->r_count++; 9920 mutex_exit(&rp->r_statelock); 9921 error = nfs4_putpages(vp, off, len, flags, cr); 9922 mutex_enter(&rp->r_statelock); 9923 rp->r_count--; 9924 cv_broadcast(&rp->r_cv); 9925 mutex_exit(&rp->r_statelock); 9926 9927 return (error); 9928 } 9929 9930 /* 9931 * Write out a single page, possibly klustering adjacent dirty pages. 9932 */ 9933 int 9934 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 9935 int flags, cred_t *cr) 9936 { 9937 u_offset_t io_off; 9938 u_offset_t lbn_off; 9939 u_offset_t lbn; 9940 size_t io_len; 9941 uint_t bsize; 9942 int error; 9943 rnode4_t *rp; 9944 9945 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 9946 ASSERT(pp != NULL); 9947 ASSERT(cr != NULL); 9948 ASSERT((flags & B_ASYNC) || curproc->p_zone == VTOMI4(vp)->mi_zone); 9949 9950 rp = VTOR4(vp); 9951 ASSERT(rp->r_count > 0); 9952 ASSERT(!IS_SHADOW(vp, rp)); 9953 9954 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9955 lbn = pp->p_offset / bsize; 9956 lbn_off = lbn * bsize; 9957 9958 /* 9959 * Find a kluster that fits in one block, or in 9960 * one page if pages are bigger than blocks. If 9961 * there is less file space allocated than a whole 9962 * page, we'll shorten the i/o request below. 9963 */ 9964 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 9965 roundup(bsize, PAGESIZE), flags); 9966 9967 /* 9968 * pvn_write_kluster shouldn't have returned a page with offset 9969 * behind the original page we were given. Verify that. 9970 */ 9971 ASSERT((pp->p_offset / bsize) >= lbn); 9972 9973 /* 9974 * Now pp will have the list of kept dirty pages marked for 9975 * write back. It will also handle invalidation and freeing 9976 * of pages that are not dirty. Check for page length rounding 9977 * problems. 9978 */ 9979 if (io_off + io_len > lbn_off + bsize) { 9980 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 9981 io_len = lbn_off + bsize - io_off; 9982 } 9983 /* 9984 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 9985 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 9986 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 9987 * progress and the r_size has not been made consistent with the 9988 * new size of the file. When the uiomove() completes the r_size is 9989 * updated and the R4MODINPROGRESS flag is cleared. 9990 * 9991 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 9992 * consistent value of r_size. Without this handshaking, it is 9993 * possible that nfs4_bio() picks up the old value of r_size 9994 * before the uiomove() in writerp4() completes. This will result 9995 * in the write through nfs4_bio() being dropped. 9996 * 9997 * More precisely, there is a window between the time the uiomove() 9998 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 9999 * operation intervenes in this window, the page will be picked up, 10000 * because it is dirty (it will be unlocked, unless it was 10001 * pagecreate'd). When the page is picked up as dirty, the dirty 10002 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10003 * checked. This will still be the old size. Therefore the page will 10004 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10005 * the page will be found to be clean and the write will be dropped. 10006 */ 10007 if (rp->r_flags & R4MODINPROGRESS) { 10008 mutex_enter(&rp->r_statelock); 10009 if ((rp->r_flags & R4MODINPROGRESS) && 10010 rp->r_modaddr + MAXBSIZE > io_off && 10011 rp->r_modaddr < io_off + io_len) { 10012 page_t *plist; 10013 /* 10014 * A write is in progress for this region of the file. 10015 * If we did not detect R4MODINPROGRESS here then this 10016 * path through nfs_putapage() would eventually go to 10017 * nfs4_bio() and may not write out all of the data 10018 * in the pages. We end up losing data. So we decide 10019 * to set the modified bit on each page in the page 10020 * list and mark the rnode with R4DIRTY. This write 10021 * will be restarted at some later time. 10022 */ 10023 plist = pp; 10024 while (plist != NULL) { 10025 pp = plist; 10026 page_sub(&plist, pp); 10027 hat_setmod(pp); 10028 page_io_unlock(pp); 10029 page_unlock(pp); 10030 } 10031 rp->r_flags |= R4DIRTY; 10032 mutex_exit(&rp->r_statelock); 10033 if (offp) 10034 *offp = io_off; 10035 if (lenp) 10036 *lenp = io_len; 10037 return (0); 10038 } 10039 mutex_exit(&rp->r_statelock); 10040 } 10041 10042 if (flags & B_ASYNC) { 10043 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10044 nfs4_sync_putapage); 10045 } else 10046 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10047 10048 if (offp) 10049 *offp = io_off; 10050 if (lenp) 10051 *lenp = io_len; 10052 return (error); 10053 } 10054 10055 static int 10056 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10057 int flags, cred_t *cr) 10058 { 10059 int error; 10060 rnode4_t *rp; 10061 10062 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 10063 10064 flags |= B_WRITE; 10065 10066 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10067 10068 rp = VTOR4(vp); 10069 10070 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10071 error == EACCES) && 10072 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10073 if (!(rp->r_flags & R4OUTOFSPACE)) { 10074 mutex_enter(&rp->r_statelock); 10075 rp->r_flags |= R4OUTOFSPACE; 10076 mutex_exit(&rp->r_statelock); 10077 } 10078 flags |= B_ERROR; 10079 pvn_write_done(pp, flags); 10080 /* 10081 * If this was not an async thread, then try again to 10082 * write out the pages, but this time, also destroy 10083 * them whether or not the write is successful. This 10084 * will prevent memory from filling up with these 10085 * pages and destroying them is the only alternative 10086 * if they can't be written out. 10087 * 10088 * Don't do this if this is an async thread because 10089 * when the pages are unlocked in pvn_write_done, 10090 * some other thread could have come along, locked 10091 * them, and queued for an async thread. It would be 10092 * possible for all of the async threads to be tied 10093 * up waiting to lock the pages again and they would 10094 * all already be locked and waiting for an async 10095 * thread to handle them. Deadlock. 10096 */ 10097 if (!(flags & B_ASYNC)) { 10098 error = nfs4_putpage(vp, io_off, io_len, 10099 B_INVAL | B_FORCE, cr); 10100 } 10101 } else { 10102 if (error) 10103 flags |= B_ERROR; 10104 else if (rp->r_flags & R4OUTOFSPACE) { 10105 mutex_enter(&rp->r_statelock); 10106 rp->r_flags &= ~R4OUTOFSPACE; 10107 mutex_exit(&rp->r_statelock); 10108 } 10109 pvn_write_done(pp, flags); 10110 if (freemem < desfree) 10111 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10112 NFS4_WRITE_NOWAIT); 10113 } 10114 10115 return (error); 10116 } 10117 10118 #ifdef DEBUG 10119 int nfs4_force_open_before_mmap = 0; 10120 #endif 10121 10122 static int 10123 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10124 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10125 { 10126 struct segvn_crargs vn_a; 10127 int error = 0; 10128 rnode4_t *rp = VTOR4(vp); 10129 mntinfo4_t *mi = VTOMI4(vp); 10130 10131 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10132 return (EIO); 10133 10134 if (vp->v_flag & VNOMAP) 10135 return (ENOSYS); 10136 10137 if (off < 0 || (off + len) < 0) 10138 return (ENXIO); 10139 10140 if (vp->v_type != VREG) 10141 return (ENODEV); 10142 10143 /* 10144 * If the file is delegated to the client don't do anything. 10145 * If the file is not delegated, then validate the data cache. 10146 */ 10147 mutex_enter(&rp->r_statev4_lock); 10148 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10149 mutex_exit(&rp->r_statev4_lock); 10150 error = nfs4_validate_caches(vp, cr); 10151 if (error) 10152 return (error); 10153 } else { 10154 mutex_exit(&rp->r_statev4_lock); 10155 } 10156 10157 /* 10158 * Check to see if the vnode is currently marked as not cachable. 10159 * This means portions of the file are locked (through VOP_FRLOCK). 10160 * In this case the map request must be refused. We use 10161 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10162 */ 10163 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 10164 return (EINTR); 10165 10166 if (vp->v_flag & VNOCACHE) { 10167 error = EAGAIN; 10168 goto done; 10169 } 10170 10171 /* 10172 * Don't allow concurrent locks and mapping if mandatory locking is 10173 * enabled. 10174 */ 10175 if (flk_has_remote_locks(vp)) { 10176 struct vattr va; 10177 va.va_mask = AT_MODE; 10178 error = nfs4getattr(vp, &va, cr); 10179 if (error != 0) 10180 goto done; 10181 if (MANDLOCK(vp, va.va_mode)) { 10182 error = EAGAIN; 10183 goto done; 10184 } 10185 } 10186 10187 /* 10188 * It is possible that the rnode has a lost lock request that we 10189 * are still trying to recover, and that the request conflicts with 10190 * this map request. 10191 * 10192 * An alternative approach would be for nfs4_safemap() to consider 10193 * queued lock requests when deciding whether to set or clear 10194 * VNOCACHE. This would require the frlock code path to call 10195 * nfs4_safemap() after enqueing a lost request. 10196 */ 10197 if (nfs4_map_lost_lock_conflict(vp)) { 10198 error = EAGAIN; 10199 goto done; 10200 } 10201 10202 as_rangelock(as); 10203 if (!(flags & MAP_FIXED)) { 10204 map_addr(addrp, len, off, 1, flags); 10205 if (*addrp == NULL) { 10206 as_rangeunlock(as); 10207 error = ENOMEM; 10208 goto done; 10209 } 10210 } else { 10211 /* 10212 * User specified address - blow away any previous mappings 10213 */ 10214 (void) as_unmap(as, *addrp, len); 10215 } 10216 10217 if (vp->v_type == VREG) { 10218 /* 10219 * We need to retrieve the open stream 10220 */ 10221 nfs4_open_stream_t *osp = NULL; 10222 nfs4_open_owner_t *oop = NULL; 10223 10224 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10225 if (oop != NULL) { 10226 /* returns with 'os_sync_lock' held */ 10227 osp = find_open_stream(oop, rp); 10228 open_owner_rele(oop); 10229 } 10230 if (osp == NULL) { 10231 #ifdef DEBUG 10232 if (nfs4_force_open_before_mmap) { 10233 error = EIO; 10234 goto done; 10235 } 10236 #endif 10237 /* returns with 'os_sync_lock' held */ 10238 osp = open_and_get_osp(vp, cr, mi); 10239 if (osp == NULL) { 10240 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10241 "nfs4_map: we tried to OPEN the file " 10242 "but again no osp, so fail with EIO")); 10243 error = EIO; 10244 goto done; 10245 } 10246 } 10247 10248 if (osp->os_failed_reopen) { 10249 mutex_exit(&osp->os_sync_lock); 10250 open_stream_rele(osp, rp); 10251 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10252 "nfs4_map: os_failed_reopen set on " 10253 "osp %p, cr %p, rp %s", (void *)osp, 10254 (void *)cr, rnode4info(rp))); 10255 error = EIO; 10256 goto done; 10257 } 10258 mutex_exit(&osp->os_sync_lock); 10259 open_stream_rele(osp, rp); 10260 } 10261 10262 vn_a.vp = vp; 10263 vn_a.offset = off; 10264 vn_a.type = (flags & MAP_TYPE); 10265 vn_a.prot = (uchar_t)prot; 10266 vn_a.maxprot = (uchar_t)maxprot; 10267 vn_a.flags = (flags & ~MAP_TYPE); 10268 vn_a.cred = cr; 10269 vn_a.amp = NULL; 10270 vn_a.szc = 0; 10271 vn_a.lgrp_mem_policy_flags = 0; 10272 10273 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10274 as_rangeunlock(as); 10275 10276 done: 10277 nfs_rw_exit(&rp->r_lkserlock); 10278 return (error); 10279 } 10280 10281 /* 10282 * We're most likely dealing with a kernel module that likes to READ 10283 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10284 * officially OPEN the file to create the necessary client state 10285 * for bookkeeping of os_mmap_read/write counts. 10286 * 10287 * Since VOP_MAP only passes in a pointer to the vnode rather than 10288 * a double pointer, we can't handle the case where nfs4open_otw() 10289 * returns a different vnode than the one passed into VOP_MAP (since 10290 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10291 * we return NULL and let nfs4_map() fail. Note: the only case where 10292 * this should happen is if the file got removed and replaced with the 10293 * same name on the server (in addition to the fact that we're trying 10294 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10295 */ 10296 static nfs4_open_stream_t * 10297 open_and_get_osp(vnode_t *map_vp, cred_t *cr, mntinfo4_t *mi) 10298 { 10299 rnode4_t *rp, *drp; 10300 vnode_t *dvp, *open_vp; 10301 char *file_name; 10302 int just_created; 10303 nfs4_sharedfh_t *sfh; 10304 nfs4_open_stream_t *osp; 10305 nfs4_open_owner_t *oop; 10306 10307 open_vp = map_vp; 10308 sfh = (open_vp->v_flag & VROOT) ? mi->mi_srvparentfh : 10309 VTOSV(open_vp)->sv_dfh; 10310 drp = r4find_unlocked(sfh, open_vp->v_vfsp); 10311 if (!drp) 10312 return (NULL); 10313 10314 file_name = fn_name(VTOSV(open_vp)->sv_name); 10315 10316 rp = VTOR4(open_vp); 10317 dvp = RTOV4(drp); 10318 mutex_enter(&rp->r_statev4_lock); 10319 if (rp->created_v4) { 10320 rp->created_v4 = 0; 10321 dnlc_update(dvp, file_name, open_vp); 10322 /* This is needed so we don't bump the open ref count */ 10323 just_created = 1; 10324 } else { 10325 just_created = 0; 10326 } 10327 mutex_exit(&rp->r_statev4_lock); 10328 10329 VN_HOLD(map_vp); 10330 10331 if (nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10332 just_created)) { 10333 kmem_free(file_name, MAXNAMELEN); 10334 VN_RELE(dvp); 10335 VN_RELE(map_vp); 10336 return (NULL); 10337 } 10338 10339 kmem_free(file_name, MAXNAMELEN); 10340 VN_RELE(dvp); 10341 10342 /* 10343 * If nfs4open_otw() returned a different vnode then "undo" 10344 * the open and return failure to the caller. 10345 */ 10346 if (!VN_CMP(open_vp, map_vp)) { 10347 nfs4_error_t e; 10348 10349 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10350 "open returned a different vnode")); 10351 /* 10352 * If there's an error, ignore it, 10353 * and let VOP_INACTIVE handle it. 10354 */ 10355 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10356 CLOSE_NORM, 0, 0, 0); 10357 VN_RELE(map_vp); 10358 return (NULL); 10359 } 10360 10361 VN_RELE(map_vp); 10362 10363 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10364 if (!oop) { 10365 nfs4_error_t e; 10366 10367 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10368 "no open owner")); 10369 /* 10370 * If there's an error, ignore it, 10371 * and let VOP_INACTIVE handle it. 10372 */ 10373 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10374 CLOSE_NORM, 0, 0, 0); 10375 return (NULL); 10376 } 10377 osp = find_open_stream(oop, rp); 10378 open_owner_rele(oop); 10379 return (osp); 10380 } 10381 10382 /* 10383 * Please be aware that when this function is called, the address space write 10384 * a_lock is held. Do not put over the wire calls in this function. 10385 */ 10386 /* ARGSUSED */ 10387 static int 10388 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10389 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10390 { 10391 rnode4_t *rp; 10392 int error = 0; 10393 mntinfo4_t *mi; 10394 10395 mi = VTOMI4(vp); 10396 rp = VTOR4(vp); 10397 10398 if (curproc->p_zone != mi->mi_zone) 10399 return (EIO); 10400 if (vp->v_flag & VNOMAP) 10401 return (ENOSYS); 10402 10403 /* 10404 * Need to hold rwlock while incrementing the mapcnt so that 10405 * mmap'ing can be serialized with writes so that the caching 10406 * can be handled correctly. 10407 * 10408 * Don't need to update the open stream first, since this 10409 * mmap can't add any additional share access that isn't 10410 * already contained in the open stream (for the case where we 10411 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10412 * take into account os_mmap_read[write] counts). 10413 */ 10414 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 10415 return (EINTR); 10416 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10417 nfs_rw_exit(&rp->r_rwlock); 10418 10419 if (vp->v_type == VREG) { 10420 /* 10421 * We need to retrieve the open stream and update the counts. 10422 * If there is no open stream here, something is wrong. 10423 */ 10424 nfs4_open_stream_t *osp = NULL; 10425 nfs4_open_owner_t *oop = NULL; 10426 10427 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10428 if (oop != NULL) { 10429 /* returns with 'os_sync_lock' held */ 10430 osp = find_open_stream(oop, rp); 10431 open_owner_rele(oop); 10432 } 10433 if (osp == NULL) { 10434 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10435 "nfs4_addmap: we should have an osp" 10436 "but we don't, so fail with EIO")); 10437 error = EIO; 10438 goto out; 10439 } 10440 10441 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10442 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10443 10444 /* 10445 * Update the map count in the open stream. 10446 * This is necessary in the case where we 10447 * open/mmap/close/, then the server reboots, and we 10448 * attempt to reopen. If the mmap doesn't add share 10449 * access then we send an invalid reopen with 10450 * access = NONE. 10451 * 10452 * We need to specifically check each PROT_* so a mmap 10453 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10454 * read and write access. A simple comparison of prot 10455 * to ~PROT_WRITE to determine read access is insufficient 10456 * since prot can be |= with PROT_USER, etc. 10457 */ 10458 10459 /* 10460 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10461 */ 10462 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10463 osp->os_mmap_write += btopr(len); 10464 if (maxprot & PROT_READ) 10465 osp->os_mmap_read += btopr(len); 10466 if (maxprot & PROT_EXEC) 10467 osp->os_mmap_read += btopr(len); 10468 /* 10469 * Ensure that os_mmap_read gets incremented, even if 10470 * maxprot were to look like PROT_NONE. 10471 */ 10472 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10473 !(maxprot & PROT_EXEC)) 10474 osp->os_mmap_read += btopr(len); 10475 osp->os_mapcnt += btopr(len); 10476 mutex_exit(&osp->os_sync_lock); 10477 open_stream_rele(osp, rp); 10478 } 10479 10480 out: 10481 /* 10482 * If we got an error, then undo our 10483 * incrementing of 'r_mapcnt'. 10484 */ 10485 10486 if (error) { 10487 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10488 ASSERT(rp->r_mapcnt >= 0); 10489 } 10490 return (error); 10491 } 10492 10493 static int 10494 nfs4_cmp(vnode_t *vp1, vnode_t *vp2) 10495 { 10496 10497 return (VTOR4(vp1) == VTOR4(vp2)); 10498 } 10499 10500 static int 10501 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10502 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr) 10503 { 10504 int rc; 10505 u_offset_t start, end; 10506 rnode4_t *rp; 10507 int error = 0, intr = INTR4(vp); 10508 nfs4_error_t e; 10509 10510 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10511 return (EIO); 10512 10513 /* check for valid cmd parameter */ 10514 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10515 return (EINVAL); 10516 10517 /* Verify l_type. */ 10518 switch (bfp->l_type) { 10519 case F_RDLCK: 10520 if (cmd != F_GETLK && !(flag & FREAD)) 10521 return (EBADF); 10522 break; 10523 case F_WRLCK: 10524 if (cmd != F_GETLK && !(flag & FWRITE)) 10525 return (EBADF); 10526 break; 10527 case F_UNLCK: 10528 intr = 0; 10529 break; 10530 10531 default: 10532 return (EINVAL); 10533 } 10534 10535 /* check the validity of the lock range */ 10536 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10537 return (rc); 10538 if (rc = flk_check_lock_data(start, end, MAXEND)) 10539 return (rc); 10540 10541 /* 10542 * If the filesystem is mounted using local locking, pass the 10543 * request off to the local locking code. 10544 */ 10545 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10546 if (cmd == F_SETLK || cmd == F_SETLKW) { 10547 /* 10548 * For complete safety, we should be holding 10549 * r_lkserlock. However, we can't call 10550 * nfs4_safelock and then fs_frlock while 10551 * holding r_lkserlock, so just invoke 10552 * nfs4_safelock and expect that this will 10553 * catch enough of the cases. 10554 */ 10555 if (!nfs4_safelock(vp, bfp, cr)) 10556 return (EAGAIN); 10557 } 10558 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 10559 } 10560 10561 rp = VTOR4(vp); 10562 10563 /* 10564 * Check whether the given lock request can proceed, given the 10565 * current file mappings. 10566 */ 10567 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10568 return (EINTR); 10569 if (cmd == F_SETLK || cmd == F_SETLKW) { 10570 if (!nfs4_safelock(vp, bfp, cr)) { 10571 rc = EAGAIN; 10572 goto done; 10573 } 10574 } 10575 10576 /* 10577 * Flush the cache after waiting for async I/O to finish. For new 10578 * locks, this is so that the process gets the latest bits from the 10579 * server. For unlocks, this is so that other clients see the 10580 * latest bits once the file has been unlocked. If currently dirty 10581 * pages can't be flushed, then don't allow a lock to be set. But 10582 * allow unlocks to succeed, to avoid having orphan locks on the 10583 * server. 10584 */ 10585 if (cmd != F_GETLK) { 10586 mutex_enter(&rp->r_statelock); 10587 while (rp->r_count > 0) { 10588 if (intr) { 10589 klwp_t *lwp = ttolwp(curthread); 10590 10591 if (lwp != NULL) 10592 lwp->lwp_nostop++; 10593 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock) == 0) { 10594 if (lwp != NULL) 10595 lwp->lwp_nostop--; 10596 rc = EINTR; 10597 break; 10598 } 10599 if (lwp != NULL) 10600 lwp->lwp_nostop--; 10601 } else 10602 cv_wait(&rp->r_cv, &rp->r_statelock); 10603 } 10604 mutex_exit(&rp->r_statelock); 10605 if (rc != 0) 10606 goto done; 10607 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr); 10608 if (error) { 10609 if (error == ENOSPC || error == EDQUOT) { 10610 mutex_enter(&rp->r_statelock); 10611 if (!rp->r_error) 10612 rp->r_error = error; 10613 mutex_exit(&rp->r_statelock); 10614 } 10615 if (bfp->l_type != F_UNLCK) { 10616 rc = ENOLCK; 10617 goto done; 10618 } 10619 } 10620 } 10621 10622 /* 10623 * Call the lock manager to do the real work of contacting 10624 * the server and obtaining the lock. 10625 */ 10626 10627 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10628 cr, &e, NULL, NULL); 10629 rc = e.error; 10630 10631 if (rc == 0) 10632 nfs4_lockcompletion(vp, cmd); 10633 10634 done: 10635 nfs_rw_exit(&rp->r_lkserlock); 10636 10637 return (rc); 10638 } 10639 10640 /* 10641 * Free storage space associated with the specified vnode. The portion 10642 * to be freed is specified by bfp->l_start and bfp->l_len (already 10643 * normalized to a "whence" of 0). 10644 * 10645 * This is an experimental facility whose continued existence is not 10646 * guaranteed. Currently, we only support the special case 10647 * of l_len == 0, meaning free to end of file. 10648 */ 10649 /* ARGSUSED */ 10650 static int 10651 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10652 offset_t offset, cred_t *cr, caller_context_t *ct) 10653 { 10654 int error; 10655 10656 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10657 return (EIO); 10658 ASSERT(vp->v_type == VREG); 10659 if (cmd != F_FREESP) 10660 return (EINVAL); 10661 10662 error = convoff(vp, bfp, 0, offset); 10663 if (!error) { 10664 ASSERT(bfp->l_start >= 0); 10665 if (bfp->l_len == 0) { 10666 struct vattr va; 10667 10668 va.va_mask = AT_SIZE; 10669 va.va_size = bfp->l_start; 10670 error = nfs4setattr(vp, &va, 0, cr, NULL); 10671 } else 10672 error = EINVAL; 10673 } 10674 10675 return (error); 10676 } 10677 10678 /* ARGSUSED */ 10679 static int 10680 nfs4_realvp(vnode_t *vp, vnode_t **vpp) 10681 { 10682 return (EINVAL); 10683 } 10684 10685 /* 10686 * Setup and add an address space callback to do the work of the delmap call. 10687 * The callback will (and must be) deleted in the actual callback function. 10688 * 10689 * This is done in order to take care of the problem that we have with holding 10690 * the address space's a_lock for a long period of time (e.g. if the NFS server 10691 * is down). Callbacks will be executed in the address space code while the 10692 * a_lock is not held. Holding the address space's a_lock causes things such 10693 * as ps and fork to hang because they are trying to acquire this lock as well. 10694 */ 10695 /* ARGSUSED */ 10696 static int 10697 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10698 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 10699 { 10700 int caller_found; 10701 int error; 10702 rnode4_t *rp; 10703 nfs4_delmap_args_t *dmapp; 10704 nfs4_delmapcall_t *delmap_call; 10705 10706 if (vp->v_flag & VNOMAP) 10707 return (ENOSYS); 10708 10709 /* 10710 * A process may not change zones if it has NFS pages mmap'ed 10711 * in, so we can't legitimately get here from the wrong zone. 10712 */ 10713 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 10714 10715 rp = VTOR4(vp); 10716 10717 /* 10718 * The way that the address space of this process deletes its mapping 10719 * of this file is via the following call chains: 10720 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10721 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10722 * 10723 * With the use of address space callbacks we are allowed to drop the 10724 * address space lock, a_lock, while executing the NFS operations that 10725 * need to go over the wire. Returning EAGAIN to the caller of this 10726 * function is what drives the execution of the callback that we add 10727 * below. The callback will be executed by the address space code 10728 * after dropping the a_lock. When the callback is finished, since 10729 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 10730 * is called again on the same segment to finish the rest of the work 10731 * that needs to happen during unmapping. 10732 * 10733 * This action of calling back into the segment driver causes 10734 * nfs4_delmap() to get called again, but since the callback was 10735 * already executed at this point, it already did the work and there 10736 * is nothing left for us to do. 10737 * 10738 * To Summarize: 10739 * - The first time nfs4_delmap is called by the current thread is when 10740 * we add the caller associated with this delmap to the delmap caller 10741 * list, add the callback, and return EAGAIN. 10742 * - The second time in this call chain when nfs4_delmap is called we 10743 * will find this caller in the delmap caller list and realize there 10744 * is no more work to do thus removing this caller from the list and 10745 * returning the error that was set in the callback execution. 10746 */ 10747 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 10748 if (caller_found) { 10749 /* 10750 * 'error' is from the actual delmap operations. To avoid 10751 * hangs, we need to handle the return of EAGAIN differently 10752 * since this is what drives the callback execution. 10753 * In this case, we don't want to return EAGAIN and do the 10754 * callback execution because there are none to execute. 10755 */ 10756 if (error == EAGAIN) 10757 return (0); 10758 else 10759 return (error); 10760 } 10761 10762 /* current caller was not in the list */ 10763 delmap_call = nfs4_init_delmapcall(); 10764 10765 mutex_enter(&rp->r_statelock); 10766 list_insert_tail(&rp->r_indelmap, delmap_call); 10767 mutex_exit(&rp->r_statelock); 10768 10769 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 10770 10771 dmapp->vp = vp; 10772 dmapp->off = off; 10773 dmapp->addr = addr; 10774 dmapp->len = len; 10775 dmapp->prot = prot; 10776 dmapp->maxprot = maxprot; 10777 dmapp->flags = flags; 10778 dmapp->cr = cr; 10779 dmapp->caller = delmap_call; 10780 10781 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 10782 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 10783 10784 return (error ? error : EAGAIN); 10785 } 10786 10787 static nfs4_delmapcall_t * 10788 nfs4_init_delmapcall() 10789 { 10790 nfs4_delmapcall_t *delmap_call; 10791 10792 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 10793 delmap_call->call_id = curthread; 10794 delmap_call->error = 0; 10795 10796 return (delmap_call); 10797 } 10798 10799 static void 10800 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 10801 { 10802 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 10803 } 10804 10805 /* 10806 * Searches for the current delmap caller (based on curthread) in the list of 10807 * callers. If it is found, we remove it and free the delmap caller. 10808 * Returns: 10809 * 0 if the caller wasn't found 10810 * 1 if the caller was found, removed and freed. *errp will be set 10811 * to what the result of the delmap was. 10812 */ 10813 static int 10814 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 10815 { 10816 nfs4_delmapcall_t *delmap_call; 10817 10818 /* 10819 * If the list doesn't exist yet, we create it and return 10820 * that the caller wasn't found. No list = no callers. 10821 */ 10822 mutex_enter(&rp->r_statelock); 10823 if (!(rp->r_flags & R4DELMAPLIST)) { 10824 /* The list does not exist */ 10825 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 10826 offsetof(nfs4_delmapcall_t, call_node)); 10827 rp->r_flags |= R4DELMAPLIST; 10828 mutex_exit(&rp->r_statelock); 10829 return (0); 10830 } else { 10831 /* The list exists so search it */ 10832 for (delmap_call = list_head(&rp->r_indelmap); 10833 delmap_call != NULL; 10834 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 10835 if (delmap_call->call_id == curthread) { 10836 /* current caller is in the list */ 10837 *errp = delmap_call->error; 10838 list_remove(&rp->r_indelmap, delmap_call); 10839 mutex_exit(&rp->r_statelock); 10840 nfs4_free_delmapcall(delmap_call); 10841 return (1); 10842 } 10843 } 10844 } 10845 mutex_exit(&rp->r_statelock); 10846 return (0); 10847 } 10848 10849 /* 10850 * Remove some pages from an mmap'd vnode. Just update the 10851 * count of pages. If doing close-to-open, then flush and 10852 * commit all of the pages associated with this file. 10853 * Otherwise, start an asynchronous page flush to write out 10854 * any dirty pages. This will also associate a credential 10855 * with the rnode which can be used to write the pages. 10856 */ 10857 /* ARGSUSED */ 10858 static void 10859 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 10860 { 10861 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 10862 rnode4_t *rp; 10863 mntinfo4_t *mi; 10864 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 10865 10866 rp = VTOR4(dmapp->vp); 10867 mi = VTOMI4(dmapp->vp); 10868 10869 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 10870 ASSERT(rp->r_mapcnt >= 0); 10871 10872 /* 10873 * Initiate a page flush and potential commit if there are 10874 * pages, the file system was not mounted readonly, the segment 10875 * was mapped shared, and the pages themselves were writeable. 10876 */ 10877 if (nfs4_has_pages(dmapp->vp) && 10878 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 10879 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 10880 mutex_enter(&rp->r_statelock); 10881 rp->r_flags |= R4DIRTY; 10882 mutex_exit(&rp->r_statelock); 10883 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 10884 dmapp->len, dmapp->cr); 10885 if (!e.error) { 10886 mutex_enter(&rp->r_statelock); 10887 e.error = rp->r_error; 10888 rp->r_error = 0; 10889 mutex_exit(&rp->r_statelock); 10890 } 10891 } else 10892 e.error = 0; 10893 10894 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 10895 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 10896 B_INVAL, dmapp->cr); 10897 10898 if (e.error) { 10899 e.stat = puterrno4(e.error); 10900 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 10901 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 10902 dmapp->caller->error = e.error; 10903 } 10904 10905 /* Check to see if we need to close the file */ 10906 10907 if (dmapp->vp->v_type == VREG) { 10908 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 10909 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 10910 10911 if (e.error != 0 || e.stat != NFS4_OK) { 10912 /* 10913 * Since it is possible that e.error == 0 and 10914 * e.stat != NFS4_OK (and vice versa), 10915 * we do the proper checking in order to get both 10916 * e.error and e.stat reporting the correct info. 10917 */ 10918 if (e.stat == NFS4_OK) 10919 e.stat = puterrno4(e.error); 10920 if (e.error == 0) 10921 e.error = geterrno4(e.stat); 10922 10923 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 10924 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 10925 dmapp->caller->error = e.error; 10926 } 10927 } 10928 10929 (void) as_delete_callback(as, arg); 10930 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 10931 } 10932 10933 10934 static uint_t 10935 fattr4_maxfilesize_to_bits(uint64_t ll) 10936 { 10937 uint_t l = 1; 10938 10939 if (ll == 0) { 10940 return (0); 10941 } 10942 10943 if (ll & 0xffffffff00000000) { 10944 l += 32; ll >>= 32; 10945 } 10946 if (ll & 0xffff0000) { 10947 l += 16; ll >>= 16; 10948 } 10949 if (ll & 0xff00) { 10950 l += 8; ll >>= 8; 10951 } 10952 if (ll & 0xf0) { 10953 l += 4; ll >>= 4; 10954 } 10955 if (ll & 0xc) { 10956 l += 2; ll >>= 2; 10957 } 10958 if (ll & 0x2) { 10959 l += 1; 10960 } 10961 return (l); 10962 } 10963 10964 static int 10965 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 10966 { 10967 int error; 10968 hrtime_t t; 10969 rnode4_t *rp; 10970 nfs4_ga_res_t gar; 10971 nfs4_ga_ext_res_t ger; 10972 10973 gar.n4g_ext_res = &ger; 10974 10975 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10976 return (EIO); 10977 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 10978 *valp = MAXPATHLEN; 10979 return (0); 10980 } 10981 if (cmd == _PC_ACL_ENABLED) { 10982 *valp = _ACL_ACE_ENABLED; 10983 return (0); 10984 } 10985 10986 rp = VTOR4(vp); 10987 if (cmd == _PC_XATTR_EXISTS) { 10988 /* 10989 * Eventually should attempt small client readdir before 10990 * going otw with GETATTR(FATTR4_NAMED_ATTR). For now 10991 * just drive the OTW getattr. This is required because 10992 * _PC_XATTR_EXISTS can only return true if attributes 10993 * exist -- simply checking for existance of the attrdir 10994 * is not sufficient. 10995 * 10996 * pc4_xattr_valid can be only be trusted when r_xattr_dir 10997 * is NULL. Once the xadir vp exists, we can create xattrs, 10998 * and we don't have any way to update the "base" object's 10999 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11000 * could help out. 11001 */ 11002 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11003 rp->r_xattr_dir == NULL) { 11004 *valp = rp->r_pathconf.pc4_xattr_exists; 11005 return (0); 11006 } 11007 } else { /* OLD CODE */ 11008 if (ATTRCACHE4_VALID(vp)) { 11009 mutex_enter(&rp->r_statelock); 11010 if (rp->r_pathconf.pc4_cache_valid) { 11011 error = 0; 11012 switch (cmd) { 11013 case _PC_FILESIZEBITS: 11014 *valp = 11015 rp->r_pathconf.pc4_filesizebits; 11016 break; 11017 case _PC_LINK_MAX: 11018 *valp = 11019 rp->r_pathconf.pc4_link_max; 11020 break; 11021 case _PC_NAME_MAX: 11022 *valp = 11023 rp->r_pathconf.pc4_name_max; 11024 break; 11025 case _PC_CHOWN_RESTRICTED: 11026 *valp = 11027 rp->r_pathconf.pc4_chown_restricted; 11028 break; 11029 case _PC_NO_TRUNC: 11030 *valp = 11031 rp->r_pathconf.pc4_no_trunc; 11032 break; 11033 default: 11034 error = EINVAL; 11035 break; 11036 } 11037 mutex_exit(&rp->r_statelock); 11038 #ifdef DEBUG 11039 nfs4_pathconf_cache_hits++; 11040 #endif 11041 return (error); 11042 } 11043 mutex_exit(&rp->r_statelock); 11044 } 11045 } 11046 #ifdef DEBUG 11047 nfs4_pathconf_cache_misses++; 11048 #endif 11049 11050 t = gethrtime(); 11051 11052 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11053 11054 if (error) { 11055 mutex_enter(&rp->r_statelock); 11056 rp->r_pathconf.pc4_cache_valid = FALSE; 11057 rp->r_pathconf.pc4_xattr_valid = FALSE; 11058 mutex_exit(&rp->r_statelock); 11059 return (error); 11060 } 11061 11062 /* interpret the max filesize */ 11063 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11064 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11065 11066 /* Store the attributes we just received */ 11067 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11068 11069 switch (cmd) { 11070 case _PC_FILESIZEBITS: 11071 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11072 break; 11073 case _PC_LINK_MAX: 11074 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11075 break; 11076 case _PC_NAME_MAX: 11077 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11078 break; 11079 case _PC_CHOWN_RESTRICTED: 11080 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11081 break; 11082 case _PC_NO_TRUNC: 11083 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11084 break; 11085 case _PC_XATTR_EXISTS: 11086 *valp = gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists; 11087 break; 11088 default: 11089 return (EINVAL); 11090 } 11091 11092 return (0); 11093 } 11094 11095 /* 11096 * Called by async thread to do synchronous pageio. Do the i/o, wait 11097 * for it to complete, and cleanup the page list when done. 11098 */ 11099 static int 11100 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11101 int flags, cred_t *cr) 11102 { 11103 int error; 11104 11105 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11106 11107 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11108 if (flags & B_READ) 11109 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11110 else 11111 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11112 return (error); 11113 } 11114 11115 static int 11116 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11117 int flags, cred_t *cr) 11118 { 11119 int error; 11120 rnode4_t *rp; 11121 11122 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI4(vp)->mi_zone) 11123 return (EIO); 11124 11125 if (pp == NULL) 11126 return (EINVAL); 11127 11128 rp = VTOR4(vp); 11129 mutex_enter(&rp->r_statelock); 11130 rp->r_count++; 11131 mutex_exit(&rp->r_statelock); 11132 11133 if (flags & B_ASYNC) { 11134 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11135 nfs4_sync_pageio); 11136 } else 11137 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11138 mutex_enter(&rp->r_statelock); 11139 rp->r_count--; 11140 cv_broadcast(&rp->r_cv); 11141 mutex_exit(&rp->r_statelock); 11142 return (error); 11143 } 11144 11145 static void 11146 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr) 11147 { 11148 int error; 11149 rnode4_t *rp; 11150 page_t *plist; 11151 page_t *pptr; 11152 offset3 offset; 11153 count3 len; 11154 k_sigset_t smask; 11155 11156 /* 11157 * We should get called with fl equal to either B_FREE or 11158 * B_INVAL. Any other value is illegal. 11159 * 11160 * The page that we are either supposed to free or destroy 11161 * should be exclusive locked and its io lock should not 11162 * be held. 11163 */ 11164 ASSERT(fl == B_FREE || fl == B_INVAL); 11165 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11166 11167 rp = VTOR4(vp); 11168 11169 /* 11170 * If the page doesn't need to be committed or we shouldn't 11171 * even bother attempting to commit it, then just make sure 11172 * that the p_fsdata byte is clear and then either free or 11173 * destroy the page as appropriate. 11174 */ 11175 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11176 pp->p_fsdata = C_NOCOMMIT; 11177 if (fl == B_FREE) 11178 page_free(pp, dn); 11179 else 11180 page_destroy(pp, dn); 11181 return; 11182 } 11183 11184 /* 11185 * If there is a page invalidation operation going on, then 11186 * if this is one of the pages being destroyed, then just 11187 * clear the p_fsdata byte and then either free or destroy 11188 * the page as appropriate. 11189 */ 11190 mutex_enter(&rp->r_statelock); 11191 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11192 mutex_exit(&rp->r_statelock); 11193 pp->p_fsdata = C_NOCOMMIT; 11194 if (fl == B_FREE) 11195 page_free(pp, dn); 11196 else 11197 page_destroy(pp, dn); 11198 return; 11199 } 11200 11201 /* 11202 * If we are freeing this page and someone else is already 11203 * waiting to do a commit, then just unlock the page and 11204 * return. That other thread will take care of commiting 11205 * this page. The page can be freed sometime after the 11206 * commit has finished. Otherwise, if the page is marked 11207 * as delay commit, then we may be getting called from 11208 * pvn_write_done, one page at a time. This could result 11209 * in one commit per page, so we end up doing lots of small 11210 * commits instead of fewer larger commits. This is bad, 11211 * we want do as few commits as possible. 11212 */ 11213 if (fl == B_FREE) { 11214 if (rp->r_flags & R4COMMITWAIT) { 11215 page_unlock(pp); 11216 mutex_exit(&rp->r_statelock); 11217 return; 11218 } 11219 if (pp->p_fsdata == C_DELAYCOMMIT) { 11220 pp->p_fsdata = C_COMMIT; 11221 page_unlock(pp); 11222 mutex_exit(&rp->r_statelock); 11223 return; 11224 } 11225 } 11226 11227 /* 11228 * Check to see if there is a signal which would prevent an 11229 * attempt to commit the pages from being successful. If so, 11230 * then don't bother with all of the work to gather pages and 11231 * generate the unsuccessful RPC. Just return from here and 11232 * let the page be committed at some later time. 11233 */ 11234 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11235 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11236 sigunintr(&smask); 11237 page_unlock(pp); 11238 mutex_exit(&rp->r_statelock); 11239 return; 11240 } 11241 sigunintr(&smask); 11242 11243 /* 11244 * We are starting to need to commit pages, so let's try 11245 * to commit as many as possible at once to reduce the 11246 * overhead. 11247 * 11248 * Set the `commit inprogress' state bit. We must 11249 * first wait until any current one finishes. Then 11250 * we initialize the c_pages list with this page. 11251 */ 11252 while (rp->r_flags & R4COMMIT) { 11253 rp->r_flags |= R4COMMITWAIT; 11254 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11255 rp->r_flags &= ~R4COMMITWAIT; 11256 } 11257 rp->r_flags |= R4COMMIT; 11258 mutex_exit(&rp->r_statelock); 11259 ASSERT(rp->r_commit.c_pages == NULL); 11260 rp->r_commit.c_pages = pp; 11261 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11262 rp->r_commit.c_commlen = PAGESIZE; 11263 11264 /* 11265 * Gather together all other pages which can be committed. 11266 * They will all be chained off r_commit.c_pages. 11267 */ 11268 nfs4_get_commit(vp); 11269 11270 /* 11271 * Clear the `commit inprogress' status and disconnect 11272 * the list of pages to be committed from the rnode. 11273 * At this same time, we also save the starting offset 11274 * and length of data to be committed on the server. 11275 */ 11276 plist = rp->r_commit.c_pages; 11277 rp->r_commit.c_pages = NULL; 11278 offset = rp->r_commit.c_commbase; 11279 len = rp->r_commit.c_commlen; 11280 mutex_enter(&rp->r_statelock); 11281 rp->r_flags &= ~R4COMMIT; 11282 cv_broadcast(&rp->r_commit.c_cv); 11283 mutex_exit(&rp->r_statelock); 11284 11285 if (curproc == proc_pageout || curproc == proc_fsflush || 11286 curproc->p_zone != VTOMI4(vp)->mi_zone) { 11287 nfs4_async_commit(vp, plist, offset, len, 11288 cr, do_nfs4_async_commit); 11289 return; 11290 } 11291 11292 /* 11293 * Actually generate the COMMIT op over the wire operation. 11294 */ 11295 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11296 11297 /* 11298 * If we got an error during the commit, just unlock all 11299 * of the pages. The pages will get retransmitted to the 11300 * server during a putpage operation. 11301 */ 11302 if (error) { 11303 while (plist != NULL) { 11304 pptr = plist; 11305 page_sub(&plist, pptr); 11306 page_unlock(pptr); 11307 } 11308 return; 11309 } 11310 11311 /* 11312 * We've tried as hard as we can to commit the data to stable 11313 * storage on the server. We just unlock the rest of the pages 11314 * and clear the commit required state. They will be put 11315 * onto the tail of the cachelist if they are nolonger 11316 * mapped. 11317 */ 11318 while (plist != pp) { 11319 pptr = plist; 11320 page_sub(&plist, pptr); 11321 pptr->p_fsdata = C_NOCOMMIT; 11322 page_unlock(pptr); 11323 } 11324 11325 /* 11326 * It is possible that nfs4_commit didn't return error but 11327 * some other thread has modified the page we are going 11328 * to free/destroy. 11329 * In this case we need to rewrite the page. Do an explicit check 11330 * before attempting to free/destroy the page. If modified, needs to 11331 * be rewritten so unlock the page and return. 11332 */ 11333 if (hat_ismod(pp)) { 11334 pp->p_fsdata = C_NOCOMMIT; 11335 page_unlock(pp); 11336 return; 11337 } 11338 11339 /* 11340 * Now, as appropriate, either free or destroy the page 11341 * that we were called with. 11342 */ 11343 pp->p_fsdata = C_NOCOMMIT; 11344 if (fl == B_FREE) 11345 page_free(pp, dn); 11346 else 11347 page_destroy(pp, dn); 11348 } 11349 11350 /* 11351 * Commit requires that the current fh be the file written to. 11352 * The compound op structure is: 11353 * PUTFH(file), COMMIT 11354 */ 11355 static int 11356 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11357 { 11358 COMPOUND4args_clnt args; 11359 COMPOUND4res_clnt res; 11360 COMMIT4res *cm_res; 11361 nfs_argop4 argop[2]; 11362 nfs_resop4 *resop; 11363 int doqueue; 11364 mntinfo4_t *mi; 11365 rnode4_t *rp; 11366 cred_t *cred_otw = NULL; 11367 bool_t needrecov = FALSE; 11368 nfs4_recov_state_t recov_state; 11369 nfs4_open_stream_t *osp = NULL; 11370 bool_t first_time = TRUE; /* first time getting OTW cred */ 11371 bool_t last_time = FALSE; /* last time getting OTW cred */ 11372 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11373 11374 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11375 11376 rp = VTOR4(vp); 11377 11378 mi = VTOMI4(vp); 11379 recov_state.rs_flags = 0; 11380 recov_state.rs_num_retry_despite_err = 0; 11381 get_commit_cred: 11382 /* 11383 * Releases the osp, if a valid open stream is provided. 11384 * Puts a hold on the cred_otw and the new osp (if found). 11385 */ 11386 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11387 &first_time, &last_time); 11388 args.ctag = TAG_COMMIT; 11389 recov_retry: 11390 /* 11391 * Commit ops: putfh file; commit 11392 */ 11393 args.array_len = 2; 11394 args.array = argop; 11395 11396 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11397 &recov_state, NULL); 11398 if (e.error) { 11399 crfree(cred_otw); 11400 if (osp != NULL) 11401 open_stream_rele(osp, rp); 11402 return (e.error); 11403 } 11404 11405 /* putfh directory */ 11406 argop[0].argop = OP_CPUTFH; 11407 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11408 11409 /* commit */ 11410 argop[1].argop = OP_COMMIT; 11411 argop[1].nfs_argop4_u.opcommit.offset = offset; 11412 argop[1].nfs_argop4_u.opcommit.count = count; 11413 11414 doqueue = 1; 11415 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11416 11417 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11418 if (!needrecov && e.error) { 11419 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11420 needrecov); 11421 crfree(cred_otw); 11422 if (e.error == EACCES && last_time == FALSE) 11423 goto get_commit_cred; 11424 if (osp != NULL) 11425 open_stream_rele(osp, rp); 11426 return (e.error); 11427 } 11428 11429 if (needrecov) { 11430 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11431 NULL, OP_COMMIT, NULL) == FALSE) { 11432 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11433 &recov_state, needrecov); 11434 if (!e.error) 11435 (void) xdr_free(xdr_COMPOUND4res_clnt, 11436 (caddr_t)&res); 11437 goto recov_retry; 11438 } 11439 if (e.error) { 11440 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11441 &recov_state, needrecov); 11442 crfree(cred_otw); 11443 if (osp != NULL) 11444 open_stream_rele(osp, rp); 11445 return (e.error); 11446 } 11447 /* fall through for res.status case */ 11448 } 11449 11450 if (res.status) { 11451 e.error = geterrno4(res.status); 11452 if (e.error == EACCES && last_time == FALSE) { 11453 crfree(cred_otw); 11454 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11455 &recov_state, needrecov); 11456 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11457 goto get_commit_cred; 11458 } 11459 /* 11460 * Can't do a nfs4_purge_stale_fh here because this 11461 * can cause a deadlock. nfs4_commit can 11462 * be called from nfs4_dispose which can be called 11463 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11464 * can call back to pvn_vplist_dirty. 11465 */ 11466 if (e.error == ESTALE) { 11467 mutex_enter(&rp->r_statelock); 11468 rp->r_flags |= R4STALE; 11469 if (!rp->r_error) 11470 rp->r_error = e.error; 11471 mutex_exit(&rp->r_statelock); 11472 PURGE_ATTRCACHE4(vp); 11473 } else { 11474 mutex_enter(&rp->r_statelock); 11475 if (!rp->r_error) 11476 rp->r_error = e.error; 11477 mutex_exit(&rp->r_statelock); 11478 } 11479 } else { 11480 ASSERT(rp->r_flags & R4HAVEVERF); 11481 resop = &res.array[1]; /* commit res */ 11482 cm_res = &resop->nfs_resop4_u.opcommit; 11483 mutex_enter(&rp->r_statelock); 11484 if (cm_res->writeverf == rp->r_writeverf) { 11485 mutex_exit(&rp->r_statelock); 11486 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11487 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11488 &recov_state, needrecov); 11489 crfree(cred_otw); 11490 if (osp != NULL) 11491 open_stream_rele(osp, rp); 11492 return (0); 11493 } 11494 nfs4_set_mod(vp); 11495 rp->r_writeverf = cm_res->writeverf; 11496 mutex_exit(&rp->r_statelock); 11497 e.error = NFS_VERF_MISMATCH; 11498 } 11499 11500 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11501 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11502 crfree(cred_otw); 11503 if (osp != NULL) 11504 open_stream_rele(osp, rp); 11505 11506 return (e.error); 11507 } 11508 11509 static void 11510 nfs4_set_mod(vnode_t *vp) 11511 { 11512 page_t *pp; 11513 kmutex_t *vphm; 11514 rnode4_t *rp; 11515 11516 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11517 11518 /* make sure we're looking at the master vnode, not a shadow */ 11519 11520 rp = VTOR4(vp); 11521 if (IS_SHADOW(vp, rp)) 11522 vp = RTOV4(rp); 11523 11524 vphm = page_vnode_mutex(vp); 11525 mutex_enter(vphm); 11526 /* 11527 * If there are no pages associated with this vnode, then 11528 * just return. 11529 */ 11530 if ((pp = vp->v_pages) == NULL) { 11531 mutex_exit(vphm); 11532 return; 11533 } 11534 11535 do { 11536 if (pp->p_fsdata != C_NOCOMMIT) { 11537 hat_setmod(pp); 11538 pp->p_fsdata = C_NOCOMMIT; 11539 } 11540 } while ((pp = pp->p_vpnext) != vp->v_pages); 11541 mutex_exit(vphm); 11542 } 11543 11544 /* 11545 * This function is used to gather a page list of the pages which 11546 * can be committed on the server. 11547 * 11548 * The calling thread must have set R4COMMIT. This bit is used to 11549 * serialize access to the commit structure in the rnode. As long 11550 * as the thread has set R4COMMIT, then it can manipulate the commit 11551 * structure without requiring any other locks. 11552 * 11553 * When this function is called from nfs4_dispose() the page passed 11554 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11555 * will skip it. This is not a problem since we initially add the 11556 * page to the r_commit page list. 11557 * 11558 */ 11559 static void 11560 nfs4_get_commit(vnode_t *vp) 11561 { 11562 rnode4_t *rp; 11563 page_t *pp; 11564 kmutex_t *vphm; 11565 11566 rp = VTOR4(vp); 11567 11568 ASSERT(rp->r_flags & R4COMMIT); 11569 11570 /* make sure we're looking at the master vnode, not a shadow */ 11571 11572 if (IS_SHADOW(vp, rp)) 11573 vp = RTOV4(rp); 11574 11575 vphm = page_vnode_mutex(vp); 11576 mutex_enter(vphm); 11577 11578 /* 11579 * If there are no pages associated with this vnode, then 11580 * just return. 11581 */ 11582 if ((pp = vp->v_pages) == NULL) { 11583 mutex_exit(vphm); 11584 return; 11585 } 11586 11587 /* 11588 * Step through all of the pages associated with this vnode 11589 * looking for pages which need to be committed. 11590 */ 11591 do { 11592 /* 11593 * First short-cut everything (without the page_lock) 11594 * and see if this page does not need to be committed 11595 * or is modified if so then we'll just skip it. 11596 */ 11597 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11598 continue; 11599 11600 /* 11601 * Attempt to lock the page. If we can't, then 11602 * someone else is messing with it or we have been 11603 * called from nfs4_dispose and this is the page that 11604 * nfs4_dispose was called with.. anyway just skip it. 11605 */ 11606 if (!page_trylock(pp, SE_EXCL)) 11607 continue; 11608 11609 /* 11610 * Lets check again now that we have the page lock. 11611 */ 11612 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11613 page_unlock(pp); 11614 continue; 11615 } 11616 11617 /* this had better not be a free page */ 11618 ASSERT(PP_ISFREE(pp) == 0); 11619 11620 /* 11621 * The page needs to be committed and we locked it. 11622 * Update the base and length parameters and add it 11623 * to r_pages. 11624 */ 11625 if (rp->r_commit.c_pages == NULL) { 11626 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11627 rp->r_commit.c_commlen = PAGESIZE; 11628 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11629 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11630 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11631 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11632 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11633 <= pp->p_offset) { 11634 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11635 rp->r_commit.c_commbase + PAGESIZE; 11636 } 11637 page_add(&rp->r_commit.c_pages, pp); 11638 } while ((pp = pp->p_vpnext) != vp->v_pages); 11639 11640 mutex_exit(vphm); 11641 } 11642 11643 /* 11644 * This routine is used to gather together a page list of the pages 11645 * which are to be committed on the server. This routine must not 11646 * be called if the calling thread holds any locked pages. 11647 * 11648 * The calling thread must have set R4COMMIT. This bit is used to 11649 * serialize access to the commit structure in the rnode. As long 11650 * as the thread has set R4COMMIT, then it can manipulate the commit 11651 * structure without requiring any other locks. 11652 */ 11653 static void 11654 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 11655 { 11656 11657 rnode4_t *rp; 11658 page_t *pp; 11659 u_offset_t end; 11660 u_offset_t off; 11661 ASSERT(len != 0); 11662 rp = VTOR4(vp); 11663 ASSERT(rp->r_flags & R4COMMIT); 11664 11665 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11666 11667 /* make sure we're looking at the master vnode, not a shadow */ 11668 11669 if (IS_SHADOW(vp, rp)) 11670 vp = RTOV4(rp); 11671 11672 /* 11673 * If there are no pages associated with this vnode, then 11674 * just return. 11675 */ 11676 if ((pp = vp->v_pages) == NULL) 11677 return; 11678 /* 11679 * Calculate the ending offset. 11680 */ 11681 end = soff + len; 11682 for (off = soff; off < end; off += PAGESIZE) { 11683 /* 11684 * Lookup each page by vp, offset. 11685 */ 11686 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 11687 continue; 11688 /* 11689 * If this page does not need to be committed or is 11690 * modified, then just skip it. 11691 */ 11692 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11693 page_unlock(pp); 11694 continue; 11695 } 11696 11697 ASSERT(PP_ISFREE(pp) == 0); 11698 /* 11699 * The page needs to be committed and we locked it. 11700 * Update the base and length parameters and add it 11701 * to r_pages. 11702 */ 11703 if (rp->r_commit.c_pages == NULL) { 11704 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11705 rp->r_commit.c_commlen = PAGESIZE; 11706 } else { 11707 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11708 rp->r_commit.c_commbase + PAGESIZE; 11709 } 11710 page_add(&rp->r_commit.c_pages, pp); 11711 } 11712 } 11713 11714 /* 11715 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 11716 * Flushes and commits data to the server. 11717 */ 11718 static int 11719 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 11720 { 11721 int error; 11722 verifier4 write_verf; 11723 rnode4_t *rp = VTOR4(vp); 11724 11725 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11726 11727 /* 11728 * Flush the data portion of the file and then commit any 11729 * portions which need to be committed. This may need to 11730 * be done twice if the server has changed state since 11731 * data was last written. The data will need to be 11732 * rewritten to the server and then a new commit done. 11733 * 11734 * In fact, this may need to be done several times if the 11735 * server is having problems and crashing while we are 11736 * attempting to do this. 11737 */ 11738 11739 top: 11740 /* 11741 * Do a flush based on the poff and plen arguments. This 11742 * will synchronously write out any modified pages in the 11743 * range specified by (poff, plen). This starts all of the 11744 * i/o operations which will be waited for in the next 11745 * call to nfs4_putpage 11746 */ 11747 11748 mutex_enter(&rp->r_statelock); 11749 write_verf = rp->r_writeverf; 11750 mutex_exit(&rp->r_statelock); 11751 11752 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr); 11753 if (error == EAGAIN) 11754 error = 0; 11755 11756 /* 11757 * Do a flush based on the poff and plen arguments. This 11758 * will synchronously write out any modified pages in the 11759 * range specified by (poff, plen) and wait until all of 11760 * the asynchronous i/o's in that range are done as well. 11761 */ 11762 if (!error) 11763 error = nfs4_putpage(vp, poff, plen, 0, cr); 11764 11765 if (error) 11766 return (error); 11767 11768 mutex_enter(&rp->r_statelock); 11769 if (rp->r_writeverf != write_verf) { 11770 mutex_exit(&rp->r_statelock); 11771 goto top; 11772 } 11773 mutex_exit(&rp->r_statelock); 11774 11775 /* 11776 * Now commit any pages which might need to be committed. 11777 * If the error, NFS_VERF_MISMATCH, is returned, then 11778 * start over with the flush operation. 11779 */ 11780 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 11781 11782 if (error == NFS_VERF_MISMATCH) 11783 goto top; 11784 11785 return (error); 11786 } 11787 11788 /* 11789 * nfs4_commit_vp() will wait for other pending commits and 11790 * will either commit the whole file or a range, plen dictates 11791 * if we commit whole file. a value of zero indicates the whole 11792 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 11793 */ 11794 static int 11795 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 11796 cred_t *cr, int wait_on_writes) 11797 { 11798 rnode4_t *rp; 11799 page_t *plist; 11800 offset3 offset; 11801 count3 len; 11802 11803 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11804 11805 rp = VTOR4(vp); 11806 11807 /* 11808 * before we gather commitable pages make 11809 * sure there are no outstanding async writes 11810 */ 11811 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 11812 mutex_enter(&rp->r_statelock); 11813 while (rp->r_count > 0) { 11814 cv_wait(&rp->r_cv, &rp->r_statelock); 11815 } 11816 mutex_exit(&rp->r_statelock); 11817 } 11818 11819 /* 11820 * Set the `commit inprogress' state bit. We must 11821 * first wait until any current one finishes. 11822 */ 11823 mutex_enter(&rp->r_statelock); 11824 while (rp->r_flags & R4COMMIT) { 11825 rp->r_flags |= R4COMMITWAIT; 11826 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11827 rp->r_flags &= ~R4COMMITWAIT; 11828 } 11829 rp->r_flags |= R4COMMIT; 11830 mutex_exit(&rp->r_statelock); 11831 11832 /* 11833 * Gather all of the pages which need to be 11834 * committed. 11835 */ 11836 if (plen == 0) 11837 nfs4_get_commit(vp); 11838 else 11839 nfs4_get_commit_range(vp, poff, plen); 11840 11841 /* 11842 * Clear the `commit inprogress' bit and disconnect the 11843 * page list which was gathered by nfs4_get_commit. 11844 */ 11845 plist = rp->r_commit.c_pages; 11846 rp->r_commit.c_pages = NULL; 11847 offset = rp->r_commit.c_commbase; 11848 len = rp->r_commit.c_commlen; 11849 mutex_enter(&rp->r_statelock); 11850 rp->r_flags &= ~R4COMMIT; 11851 cv_broadcast(&rp->r_commit.c_cv); 11852 mutex_exit(&rp->r_statelock); 11853 11854 /* 11855 * If any pages need to be committed, commit them and 11856 * then unlock them so that they can be freed some 11857 * time later. 11858 */ 11859 if (plist == NULL) 11860 return (0); 11861 11862 /* 11863 * No error occurred during the flush portion 11864 * of this operation, so now attempt to commit 11865 * the data to stable storage on the server. 11866 * 11867 * This will unlock all of the pages on the list. 11868 */ 11869 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 11870 } 11871 11872 static int 11873 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11874 cred_t *cr) 11875 { 11876 int error; 11877 page_t *pp; 11878 11879 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11880 11881 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 11882 11883 /* 11884 * If we got an error, then just unlock all of the pages 11885 * on the list. 11886 */ 11887 if (error) { 11888 while (plist != NULL) { 11889 pp = plist; 11890 page_sub(&plist, pp); 11891 page_unlock(pp); 11892 } 11893 return (error); 11894 } 11895 /* 11896 * We've tried as hard as we can to commit the data to stable 11897 * storage on the server. We just unlock the pages and clear 11898 * the commit required state. They will get freed later. 11899 */ 11900 while (plist != NULL) { 11901 pp = plist; 11902 page_sub(&plist, pp); 11903 pp->p_fsdata = C_NOCOMMIT; 11904 page_unlock(pp); 11905 } 11906 11907 return (error); 11908 } 11909 11910 static void 11911 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11912 cred_t *cr) 11913 { 11914 11915 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 11916 } 11917 11918 /*ARGSUSED*/ 11919 static int 11920 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 11921 { 11922 int error = 0; 11923 mntinfo4_t *mi; 11924 vattr_t va; 11925 vsecattr_t nfsace4_vsap; 11926 11927 mi = VTOMI4(vp); 11928 if (curproc->p_zone != mi->mi_zone) 11929 return (EIO); 11930 if (mi->mi_flags & MI4_ACL) { 11931 /* if we have a delegation, return it */ 11932 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 11933 (void) nfs4delegreturn(VTOR4(vp), 11934 NFS4_DR_REOPEN|NFS4_DR_PUSH); 11935 11936 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 11937 NFS4_ACL_SET); 11938 if (error) /* EINVAL */ 11939 return (error); 11940 11941 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 11942 /* 11943 * These are aclent_t type entries. 11944 */ 11945 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 11946 vp->v_type == VDIR, FALSE); 11947 if (error) 11948 return (error); 11949 } else { 11950 /* 11951 * These are ace_t type entries. 11952 */ 11953 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 11954 vp->v_type == VDIR, FALSE); 11955 if (error) 11956 return (error); 11957 } 11958 bzero(&va, sizeof (va)); 11959 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 11960 vs_ace4_destroy(&nfsace4_vsap); 11961 return (error); 11962 } 11963 return (ENOSYS); 11964 } 11965 11966 static int 11967 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 11968 { 11969 int error; 11970 mntinfo4_t *mi; 11971 nfs4_ga_res_t gar; 11972 rnode4_t *rp = VTOR4(vp); 11973 11974 mi = VTOMI4(vp); 11975 if (curproc->p_zone != mi->mi_zone) 11976 return (EIO); 11977 11978 bzero(&gar, sizeof (gar)); 11979 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 11980 11981 /* 11982 * vsecattr->vsa_mask holds the original acl request mask. 11983 * This is needed when determining what to return. 11984 * (See: nfs4_create_getsecattr_return()) 11985 */ 11986 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 11987 if (error) /* EINVAL */ 11988 return (error); 11989 11990 if (mi->mi_flags & MI4_ACL) { 11991 /* 11992 * Check if the data is cached and the cache is valid. If it 11993 * is we don't go over the wire. 11994 */ 11995 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 11996 mutex_enter(&rp->r_statelock); 11997 if (rp->r_secattr != NULL) { 11998 error = nfs4_create_getsecattr_return( 11999 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12000 rp->r_attr.va_gid, 12001 vp->v_type == VDIR); 12002 if (!error) { /* error == 0 - Success! */ 12003 mutex_exit(&rp->r_statelock); 12004 return (error); 12005 } 12006 } 12007 mutex_exit(&rp->r_statelock); 12008 } 12009 12010 /* 12011 * The getattr otw call will always get both the acl, in 12012 * the form of a list of nfsace4's, and the number of acl 12013 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12014 */ 12015 gar.n4g_va.va_mask = AT_ALL; 12016 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12017 if (error) { 12018 vs_ace4_destroy(&gar.n4g_vsa); 12019 if (error == ENOTSUP || error == EOPNOTSUPP) 12020 error = fs_fab_acl(vp, vsecattr, flag, cr); 12021 return (error); 12022 } 12023 12024 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12025 /* 12026 * No error was returned, but according to the response 12027 * bitmap, neither was an acl. 12028 */ 12029 vs_ace4_destroy(&gar.n4g_vsa); 12030 error = fs_fab_acl(vp, vsecattr, flag, cr); 12031 return (error); 12032 } 12033 12034 /* 12035 * Update the cache with the ACL. 12036 */ 12037 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12038 12039 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12040 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12041 vp->v_type == VDIR); 12042 vs_ace4_destroy(&gar.n4g_vsa); 12043 if ((error) && (vsecattr->vsa_mask & 12044 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12045 (error != EACCES)) { 12046 error = fs_fab_acl(vp, vsecattr, flag, cr); 12047 } 12048 return (error); 12049 } 12050 error = fs_fab_acl(vp, vsecattr, flag, cr); 12051 return (error); 12052 } 12053 12054 /* 12055 * The function returns: 12056 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12057 * - EINVAL if the passed in "acl_mask" is an invalid request. 12058 * 12059 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12060 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12061 * 12062 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12063 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12064 * - We have a count field set without the corresponding acl field set. (e.g. - 12065 * VSA_ACECNT is set, but VSA_ACE is not) 12066 */ 12067 static int 12068 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12069 { 12070 /* Shortcut the masks that are always valid. */ 12071 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12072 return (0); 12073 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12074 return (0); 12075 12076 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12077 /* 12078 * We can't have any VSA_ACL type stuff in the mask now. 12079 */ 12080 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12081 VSA_DFACLCNT)) 12082 return (EINVAL); 12083 12084 if (op == NFS4_ACL_SET) { 12085 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12086 return (EINVAL); 12087 } 12088 } 12089 12090 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12091 /* 12092 * We can't have any VSA_ACE type stuff in the mask now. 12093 */ 12094 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12095 return (EINVAL); 12096 12097 if (op == NFS4_ACL_SET) { 12098 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12099 return (EINVAL); 12100 12101 if ((acl_mask & VSA_DFACLCNT) && 12102 !(acl_mask & VSA_DFACL)) 12103 return (EINVAL); 12104 } 12105 } 12106 return (0); 12107 } 12108 12109 /* 12110 * The theory behind creating the correct getsecattr return is simply this: 12111 * "Don't return anything that the caller is not expecting to have to free." 12112 */ 12113 static int 12114 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12115 uid_t uid, gid_t gid, int isdir) 12116 { 12117 int error = 0; 12118 /* Save the mask since the translators modify it. */ 12119 uint_t orig_mask = vsap->vsa_mask; 12120 12121 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12122 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12123 isdir, FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12124 12125 if (error) 12126 return (error); 12127 12128 /* 12129 * If the caller only asked for the ace count (VSA_ACECNT) 12130 * don't give them the full acl (VSA_ACE), free it. 12131 */ 12132 if (!orig_mask & VSA_ACE) { 12133 if (vsap->vsa_aclentp != NULL) { 12134 kmem_free(vsap->vsa_aclentp, 12135 vsap->vsa_aclcnt * sizeof (ace_t)); 12136 vsap->vsa_aclentp = NULL; 12137 } 12138 } 12139 vsap->vsa_mask = orig_mask; 12140 12141 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12142 VSA_DFACLCNT)) { 12143 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12144 isdir, FALSE, 12145 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12146 12147 if (error) 12148 return (error); 12149 12150 /* 12151 * If the caller only asked for the acl count (VSA_ACLCNT) 12152 * and/or the default acl count (VSA_DFACLCNT) don't give them 12153 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12154 */ 12155 if (!orig_mask & VSA_ACL) { 12156 if (vsap->vsa_aclentp != NULL) { 12157 kmem_free(vsap->vsa_aclentp, 12158 vsap->vsa_aclcnt * sizeof (aclent_t)); 12159 vsap->vsa_aclentp = NULL; 12160 } 12161 } 12162 12163 if (!orig_mask & VSA_DFACL) { 12164 if (vsap->vsa_dfaclentp != NULL) { 12165 kmem_free(vsap->vsa_dfaclentp, 12166 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12167 vsap->vsa_dfaclentp = NULL; 12168 } 12169 } 12170 vsap->vsa_mask = orig_mask; 12171 } 12172 return (0); 12173 } 12174 12175 static int 12176 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr) 12177 { 12178 int error; 12179 12180 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 12181 return (EIO); 12182 /* 12183 * check for valid cmd parameter 12184 */ 12185 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12186 return (EINVAL); 12187 12188 /* 12189 * Check access permissions 12190 */ 12191 if ((cmd & F_SHARE) && 12192 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12193 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12194 return (EBADF); 12195 12196 /* 12197 * If the filesystem is mounted using local locking, pass the 12198 * request off to the local share code. 12199 */ 12200 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12201 return (fs_shrlock(vp, cmd, shr, flag, cr)); 12202 12203 switch (cmd) { 12204 case F_SHARE: 12205 case F_UNSHARE: 12206 /* 12207 * This will be properly implemented later, 12208 * see RFE: 4823948 . 12209 */ 12210 error = EAGAIN; 12211 break; 12212 12213 case F_HASREMOTELOCKS: 12214 /* 12215 * NFS client can't store remote locks itself 12216 */ 12217 shr->s_access = 0; 12218 error = 0; 12219 break; 12220 12221 default: 12222 error = EINVAL; 12223 break; 12224 } 12225 12226 return (error); 12227 } 12228 12229 /* 12230 * Common code called by directory ops to update the attrcache 12231 */ 12232 static int 12233 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12234 hrtime_t t, vnode_t *vp, cred_t *cr) 12235 { 12236 int error = 0; 12237 12238 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12239 12240 if (status != NFS4_OK) { 12241 /* getattr not done or failed */ 12242 PURGE_ATTRCACHE4(vp); 12243 return (error); 12244 } 12245 12246 if (garp) { 12247 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12248 } else { 12249 PURGE_ATTRCACHE4(vp); 12250 } 12251 return (error); 12252 } 12253 12254 /* 12255 * Update directory caches for directory modification ops (link, rename, etc.) 12256 * When dinfo is NULL, manage dircaches in the old way. 12257 */ 12258 static void 12259 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12260 dirattr_info_t *dinfo) 12261 { 12262 rnode4_t *drp = VTOR4(dvp); 12263 12264 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 12265 12266 /* Purge rddir cache for dir since it changed */ 12267 if (drp->r_dir != NULL) 12268 nfs4_purge_rddir_cache(dvp); 12269 12270 /* 12271 * If caller provided dinfo, then use it to manage dir caches. 12272 */ 12273 if (dinfo != NULL) { 12274 if (vp != NULL) { 12275 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12276 if (!VTOR4(vp)->created_v4) { 12277 dnlc_update(dvp, nm, vp); 12278 } else { 12279 /* 12280 * XXX don't update if the created_v4 flag is 12281 * set 12282 */ 12283 NFS4_DEBUG(nfs4_client_state_debug, 12284 (CE_NOTE, "nfs4_update_dircaches: " 12285 "don't update dnlc: created_v4 flag")); 12286 } 12287 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12288 } 12289 12290 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12291 dinfo->di_cred, FALSE, cinfo); 12292 12293 return; 12294 } 12295 12296 /* 12297 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12298 * Since caller modified dir but didn't receive post-dirmod-op dir 12299 * attrs, the dir's attrs must be purged. 12300 * 12301 * XXX this check and dnlc update/purge should really be atomic, 12302 * XXX but can't use rnode statelock because it'll deadlock in 12303 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12304 * XXX does occur. 12305 * 12306 * XXX We also may want to check that atomic is true in the 12307 * XXX change_info struct. If it is not, the change_info may 12308 * XXX reflect changes by more than one clients which means that 12309 * XXX our cache may not be valid. 12310 */ 12311 PURGE_ATTRCACHE4(dvp); 12312 if (drp->r_change == cinfo->before) { 12313 /* no changes took place in the directory prior to our link */ 12314 if (vp != NULL) { 12315 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12316 if (!VTOR4(vp)->created_v4) { 12317 dnlc_update(dvp, nm, vp); 12318 } else { 12319 /* 12320 * XXX dont' update if the created_v4 flag 12321 * is set 12322 */ 12323 12324 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12325 "nfs4_update_dircaches: don't" 12326 " update dnlc: created_v4 flag")); 12327 } 12328 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12329 } 12330 } else { 12331 /* Another client modified directory - purge its dnlc cache */ 12332 dnlc_purge_vp(dvp); 12333 } 12334 } 12335 12336 /* 12337 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12338 * file. 12339 * 12340 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12341 * file (ie: client recovery) and otherwise set to FALSE. 12342 * 12343 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12344 * initiated) calling functions. 12345 * 12346 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12347 * of resending a 'lost' open request. 12348 * 12349 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12350 * server that hands out BAD_SEQID on open confirm. 12351 * 12352 * Errors are returned via the nfs4_error_t parameter. 12353 */ 12354 void 12355 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12356 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12357 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12358 { 12359 COMPOUND4args_clnt args; 12360 COMPOUND4res_clnt res; 12361 nfs_argop4 argop[2]; 12362 nfs_resop4 *resop; 12363 int doqueue = 1; 12364 mntinfo4_t *mi; 12365 OPEN_CONFIRM4args *open_confirm_args; 12366 int needrecov; 12367 12368 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12369 #if DEBUG 12370 mutex_enter(&oop->oo_lock); 12371 ASSERT(oop->oo_seqid_inuse); 12372 mutex_exit(&oop->oo_lock); 12373 #endif 12374 12375 recov_retry_confirm: 12376 nfs4_error_zinit(ep); 12377 *retry_open = FALSE; 12378 12379 if (resend) 12380 args.ctag = TAG_OPEN_CONFIRM_LOST; 12381 else 12382 args.ctag = TAG_OPEN_CONFIRM; 12383 12384 args.array_len = 2; 12385 args.array = argop; 12386 12387 /* putfh target fh */ 12388 argop[0].argop = OP_CPUTFH; 12389 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12390 12391 argop[1].argop = OP_OPEN_CONFIRM; 12392 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12393 12394 (*seqid) += 1; 12395 open_confirm_args->seqid = *seqid; 12396 open_confirm_args->open_stateid = *stateid; 12397 12398 mi = VTOMI4(vp); 12399 12400 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12401 12402 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12403 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12404 } 12405 12406 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12407 if (!needrecov && ep->error) 12408 return; 12409 12410 if (needrecov) { 12411 bool_t abort = FALSE; 12412 12413 if (reopening_file == FALSE) { 12414 nfs4_bseqid_entry_t *bsep = NULL; 12415 12416 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12417 bsep = nfs4_create_bseqid_entry(oop, NULL, 12418 vp, 0, args.ctag, 12419 open_confirm_args->seqid); 12420 12421 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, 12422 NULL, NULL, NULL, OP_OPEN_CONFIRM, bsep); 12423 if (bsep) { 12424 kmem_free(bsep, sizeof (*bsep)); 12425 if (num_bseqid_retryp && 12426 --(*num_bseqid_retryp) == 0) 12427 abort = TRUE; 12428 } 12429 } 12430 if ((ep->error == ETIMEDOUT || 12431 res.status == NFS4ERR_RESOURCE) && 12432 abort == FALSE && resend == FALSE) { 12433 if (!ep->error) 12434 (void) xdr_free(xdr_COMPOUND4res_clnt, 12435 (caddr_t)&res); 12436 12437 delay(SEC_TO_TICK(confirm_retry_sec)); 12438 goto recov_retry_confirm; 12439 } 12440 /* State may have changed so retry the entire OPEN op */ 12441 if (abort == FALSE) 12442 *retry_open = TRUE; 12443 else 12444 *retry_open = FALSE; 12445 if (!ep->error) 12446 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12447 return; 12448 } 12449 12450 if (res.status) { 12451 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12452 return; 12453 } 12454 12455 resop = &res.array[1]; /* open confirm res */ 12456 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12457 stateid, sizeof (*stateid)); 12458 12459 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12460 } 12461 12462 /* 12463 * Return the credentials associated with a client state object. The 12464 * caller is responsible for freeing the credentials. 12465 */ 12466 12467 static cred_t * 12468 state_to_cred(nfs4_open_stream_t *osp) 12469 { 12470 cred_t *cr; 12471 12472 /* 12473 * It's ok to not lock the open stream and open owner to get 12474 * the oo_cred since this is only written once (upon creation) 12475 * and will not change. 12476 */ 12477 cr = osp->os_open_owner->oo_cred; 12478 crhold(cr); 12479 12480 return (cr); 12481 } 12482 12483 /* 12484 * nfs4_find_sysid 12485 * 12486 * Find the sysid for the knetconfig associated with the given mi. 12487 */ 12488 static struct lm_sysid * 12489 nfs4_find_sysid(mntinfo4_t *mi) 12490 { 12491 ASSERT(curproc->p_zone == mi->mi_zone); 12492 12493 /* 12494 * Switch from RDMA knconf to original mount knconf 12495 */ 12496 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12497 mi->mi_curr_serv->sv_hostname, NULL)); 12498 } 12499 12500 #ifdef DEBUG 12501 /* 12502 * Return a string version of the call type for easy reading. 12503 */ 12504 static char * 12505 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12506 { 12507 switch (ctype) { 12508 case NFS4_LCK_CTYPE_NORM: 12509 return ("NORMAL"); 12510 case NFS4_LCK_CTYPE_RECLAIM: 12511 return ("RECLAIM"); 12512 case NFS4_LCK_CTYPE_RESEND: 12513 return ("RESEND"); 12514 case NFS4_LCK_CTYPE_REINSTATE: 12515 return ("REINSTATE"); 12516 default: 12517 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12518 "type %d", ctype); 12519 return (""); 12520 } 12521 } 12522 #endif 12523 12524 /* 12525 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12526 * Unlock requests don't have an over-the-wire locktype, so we just return 12527 * something non-threatening. 12528 */ 12529 12530 static nfs_lock_type4 12531 flk_to_locktype(int cmd, int l_type) 12532 { 12533 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12534 12535 switch (l_type) { 12536 case F_UNLCK: 12537 return (READ_LT); 12538 case F_RDLCK: 12539 if (cmd == F_SETLK) 12540 return (READ_LT); 12541 else 12542 return (READW_LT); 12543 case F_WRLCK: 12544 if (cmd == F_SETLK) 12545 return (WRITE_LT); 12546 else 12547 return (WRITEW_LT); 12548 } 12549 panic("flk_to_locktype"); 12550 /*NOTREACHED*/ 12551 } 12552 12553 /* 12554 * Do some preliminary checks for nfs4frlock. 12555 */ 12556 static int 12557 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12558 u_offset_t offset) 12559 { 12560 int error = 0; 12561 12562 /* 12563 * If we are setting a lock, check that the file is opened 12564 * with the correct mode. 12565 */ 12566 if (cmd == F_SETLK || cmd == F_SETLKW) { 12567 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12568 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12569 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12570 "nfs4frlock_validate_args: file was opened with " 12571 "incorrect mode")); 12572 return (EBADF); 12573 } 12574 } 12575 12576 /* Convert the offset. It may need to be restored before returning. */ 12577 if (error = convoff(vp, flk, 0, offset)) { 12578 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12579 "nfs4frlock_validate_args: convoff => error= %d\n", 12580 error)); 12581 return (error); 12582 } 12583 12584 return (error); 12585 } 12586 12587 /* 12588 * Set the flock64's lm_sysid for nfs4frlock. 12589 */ 12590 static int 12591 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12592 { 12593 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12594 12595 /* Find the lm_sysid */ 12596 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12597 12598 if (*lspp == NULL) { 12599 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12600 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12601 return (ENOLCK); 12602 } 12603 12604 flk->l_sysid = lm_sysidt(*lspp); 12605 12606 return (0); 12607 } 12608 12609 /* 12610 * Do the remaining preliminary setup for nfs4frlock. 12611 */ 12612 static void 12613 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12614 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12615 cred_t **cred_otw) 12616 { 12617 /* 12618 * set tick_delay to the base delay time. 12619 * (NFS4_BASE_WAIT_TIME is in secs) 12620 */ 12621 12622 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12623 12624 /* 12625 * If lock is relative to EOF, we need the newest length of the 12626 * file. Therefore invalidate the ATTR_CACHE. 12627 */ 12628 12629 *whencep = flk->l_whence; 12630 12631 if (*whencep == 2) /* SEEK_END */ 12632 PURGE_ATTRCACHE4(vp); 12633 12634 recov_statep->rs_flags = 0; 12635 recov_statep->rs_num_retry_despite_err = 0; 12636 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12637 } 12638 12639 /* 12640 * Initialize and allocate the data structures necessary for 12641 * the nfs4frlock call. 12642 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 12643 */ 12644 static void 12645 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 12646 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 12647 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 12648 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 12649 { 12650 int argoplist_size; 12651 int num_ops = 2; 12652 12653 *retry = FALSE; 12654 *did_start_fop = FALSE; 12655 *skip_get_err = FALSE; 12656 lost_rqstp->lr_op = 0; 12657 argoplist_size = num_ops * sizeof (nfs_argop4); 12658 /* fill array with zero */ 12659 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 12660 12661 *argspp = argsp; 12662 *respp = NULL; 12663 12664 argsp->array_len = num_ops; 12665 argsp->array = *argopp; 12666 12667 /* initialize in case of error; will get real value down below */ 12668 argsp->ctag = TAG_NONE; 12669 12670 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 12671 *op_hintp = OH_LOCKU; 12672 else 12673 *op_hintp = OH_OTHER; 12674 } 12675 12676 /* 12677 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 12678 * the proper nfs4_server_t for this instance of nfs4frlock. 12679 * Returns 0 (success) or an errno value. 12680 */ 12681 static int 12682 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 12683 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 12684 bool_t *did_start_fop, bool_t *startrecovp) 12685 { 12686 int error = 0; 12687 rnode4_t *rp; 12688 12689 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12690 12691 if (ctype == NFS4_LCK_CTYPE_NORM) { 12692 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 12693 recov_statep, startrecovp); 12694 if (error) 12695 return (error); 12696 *did_start_fop = TRUE; 12697 } else { 12698 *did_start_fop = FALSE; 12699 *startrecovp = FALSE; 12700 } 12701 12702 if (!error) { 12703 rp = VTOR4(vp); 12704 12705 /* If the file failed recovery, just quit. */ 12706 mutex_enter(&rp->r_statelock); 12707 if (rp->r_flags & R4RECOVERR) { 12708 error = EIO; 12709 } 12710 mutex_exit(&rp->r_statelock); 12711 } 12712 12713 return (error); 12714 } 12715 12716 /* 12717 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 12718 * resend nfs4frlock call is initiated by the recovery framework. 12719 * Acquires the lop and oop seqid synchronization. 12720 */ 12721 static void 12722 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 12723 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 12724 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 12725 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 12726 { 12727 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 12728 int error; 12729 12730 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 12731 (CE_NOTE, 12732 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 12733 ASSERT(resend_rqstp != NULL); 12734 ASSERT(resend_rqstp->lr_op == OP_LOCK || 12735 resend_rqstp->lr_op == OP_LOCKU); 12736 12737 *oopp = resend_rqstp->lr_oop; 12738 if (resend_rqstp->lr_oop) { 12739 open_owner_hold(resend_rqstp->lr_oop); 12740 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 12741 ASSERT(error == 0); /* recov thread always succeeds */ 12742 } 12743 12744 /* Must resend this lost lock/locku request. */ 12745 ASSERT(resend_rqstp->lr_lop != NULL); 12746 *lopp = resend_rqstp->lr_lop; 12747 lock_owner_hold(resend_rqstp->lr_lop); 12748 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 12749 ASSERT(error == 0); /* recov thread always succeeds */ 12750 12751 *ospp = resend_rqstp->lr_osp; 12752 if (*ospp) 12753 open_stream_hold(resend_rqstp->lr_osp); 12754 12755 if (resend_rqstp->lr_op == OP_LOCK) { 12756 LOCK4args *lock_args; 12757 12758 argop->argop = OP_LOCK; 12759 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 12760 lock_args->locktype = resend_rqstp->lr_locktype; 12761 lock_args->reclaim = 12762 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 12763 lock_args->offset = resend_rqstp->lr_flk->l_start; 12764 lock_args->length = resend_rqstp->lr_flk->l_len; 12765 if (lock_args->length == 0) 12766 lock_args->length = ~lock_args->length; 12767 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 12768 mi2clientid(mi), &lock_args->locker); 12769 12770 switch (resend_rqstp->lr_ctype) { 12771 case NFS4_LCK_CTYPE_RESEND: 12772 argsp->ctag = TAG_LOCK_RESEND; 12773 break; 12774 case NFS4_LCK_CTYPE_REINSTATE: 12775 argsp->ctag = TAG_LOCK_REINSTATE; 12776 break; 12777 case NFS4_LCK_CTYPE_RECLAIM: 12778 argsp->ctag = TAG_LOCK_RECLAIM; 12779 break; 12780 default: 12781 argsp->ctag = TAG_LOCK_UNKNOWN; 12782 break; 12783 } 12784 } else { 12785 LOCKU4args *locku_args; 12786 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 12787 12788 argop->argop = OP_LOCKU; 12789 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 12790 locku_args->locktype = READ_LT; 12791 locku_args->seqid = lop->lock_seqid + 1; 12792 mutex_enter(&lop->lo_lock); 12793 locku_args->lock_stateid = lop->lock_stateid; 12794 mutex_exit(&lop->lo_lock); 12795 locku_args->offset = resend_rqstp->lr_flk->l_start; 12796 locku_args->length = resend_rqstp->lr_flk->l_len; 12797 if (locku_args->length == 0) 12798 locku_args->length = ~locku_args->length; 12799 12800 switch (resend_rqstp->lr_ctype) { 12801 case NFS4_LCK_CTYPE_RESEND: 12802 argsp->ctag = TAG_LOCKU_RESEND; 12803 break; 12804 case NFS4_LCK_CTYPE_REINSTATE: 12805 argsp->ctag = TAG_LOCKU_REINSTATE; 12806 break; 12807 default: 12808 argsp->ctag = TAG_LOCK_UNKNOWN; 12809 break; 12810 } 12811 } 12812 } 12813 12814 /* 12815 * Setup the LOCKT4 arguments. 12816 */ 12817 static void 12818 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12819 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 12820 rnode4_t *rp) 12821 { 12822 LOCKT4args *lockt_args; 12823 12824 ASSERT(curproc->p_zone == VTOMI4(RTOV4(rp))->mi_zone); 12825 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12826 argop->argop = OP_LOCKT; 12827 argsp->ctag = TAG_LOCKT; 12828 lockt_args = &argop->nfs_argop4_u.oplockt; 12829 12830 /* 12831 * The locktype will be READ_LT unless it's 12832 * a write lock. We do this because the Solaris 12833 * system call allows the combination of 12834 * F_UNLCK and F_GETLK* and so in that case the 12835 * unlock is mapped to a read. 12836 */ 12837 if (flk->l_type == F_WRLCK) 12838 lockt_args->locktype = WRITE_LT; 12839 else 12840 lockt_args->locktype = READ_LT; 12841 12842 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 12843 /* set the lock owner4 args */ 12844 nfs4_setlockowner_args(&lockt_args->owner, rp, 12845 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 12846 flk->l_pid); 12847 lockt_args->offset = flk->l_start; 12848 lockt_args->length = flk->l_len; 12849 if (flk->l_len == 0) 12850 lockt_args->length = ~lockt_args->length; 12851 12852 *lockt_argsp = lockt_args; 12853 } 12854 12855 /* 12856 * If the client is holding a delegation, and the open stream to be used 12857 * with this lock request is a delegation open stream, then re-open the stream. 12858 * Sets the nfs4_error_t to all zeros unless the open stream has already 12859 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 12860 * means the caller should retry (like a recovery retry). 12861 */ 12862 static void 12863 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 12864 { 12865 open_delegation_type4 dt; 12866 bool_t reopen_needed, force; 12867 nfs4_open_stream_t *osp; 12868 open_claim_type4 oclaim; 12869 rnode4_t *rp = VTOR4(vp); 12870 mntinfo4_t *mi = VTOMI4(vp); 12871 12872 ASSERT(curproc->p_zone == mi->mi_zone); 12873 12874 nfs4_error_zinit(ep); 12875 12876 mutex_enter(&rp->r_statev4_lock); 12877 dt = rp->r_deleg_type; 12878 mutex_exit(&rp->r_statev4_lock); 12879 12880 if (dt != OPEN_DELEGATE_NONE) { 12881 nfs4_open_owner_t *oop; 12882 12883 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 12884 if (!oop) { 12885 ep->stat = NFS4ERR_IO; 12886 return; 12887 } 12888 /* returns with 'os_sync_lock' held */ 12889 osp = find_open_stream(oop, rp); 12890 if (!osp) { 12891 open_owner_rele(oop); 12892 ep->stat = NFS4ERR_IO; 12893 return; 12894 } 12895 12896 if (osp->os_failed_reopen) { 12897 NFS4_DEBUG((nfs4_open_stream_debug || 12898 nfs4_client_lock_debug), (CE_NOTE, 12899 "nfs4frlock_check_deleg: os_failed_reopen set " 12900 "for osp %p, cr %p, rp %s", (void *)osp, 12901 (void *)cr, rnode4info(rp))); 12902 mutex_exit(&osp->os_sync_lock); 12903 open_stream_rele(osp, rp); 12904 open_owner_rele(oop); 12905 ep->stat = NFS4ERR_IO; 12906 return; 12907 } 12908 12909 /* 12910 * Determine whether a reopen is needed. If this 12911 * is a delegation open stream, then send the open 12912 * to the server to give visibility to the open owner. 12913 * Even if it isn't a delegation open stream, we need 12914 * to check if the previous open CLAIM_DELEGATE_CUR 12915 * was sufficient. 12916 */ 12917 12918 reopen_needed = osp->os_delegation || 12919 ((lt == F_RDLCK && 12920 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 12921 (lt == F_WRLCK && 12922 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 12923 12924 mutex_exit(&osp->os_sync_lock); 12925 open_owner_rele(oop); 12926 12927 if (reopen_needed) { 12928 /* 12929 * Always use CLAIM_PREVIOUS after server reboot. 12930 * The server will reject CLAIM_DELEGATE_CUR if 12931 * it is used during the grace period. 12932 */ 12933 mutex_enter(&mi->mi_lock); 12934 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 12935 oclaim = CLAIM_PREVIOUS; 12936 force = TRUE; 12937 } else { 12938 oclaim = CLAIM_DELEGATE_CUR; 12939 force = FALSE; 12940 } 12941 mutex_exit(&mi->mi_lock); 12942 12943 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 12944 if (ep->error == EAGAIN) { 12945 nfs4_error_zinit(ep); 12946 ep->stat = NFS4ERR_DELAY; 12947 } 12948 } 12949 open_stream_rele(osp, rp); 12950 osp = NULL; 12951 } 12952 } 12953 12954 /* 12955 * Setup the LOCKU4 arguments. 12956 * Returns errors via the nfs4_error_t. 12957 * NFS4_OK no problems. *go_otwp is TRUE if call should go 12958 * over-the-wire. The caller must release the 12959 * reference on *lopp. 12960 * NFS4ERR_DELAY caller should retry (like recovery retry) 12961 * (other) unrecoverable error. 12962 */ 12963 static void 12964 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12965 LOCKU4args **locku_argsp, flock64_t *flk, 12966 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 12967 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 12968 bool_t *skip_get_err, bool_t *go_otwp) 12969 { 12970 nfs4_lock_owner_t *lop = NULL; 12971 LOCKU4args *locku_args; 12972 pid_t pid; 12973 bool_t is_spec = FALSE; 12974 rnode4_t *rp = VTOR4(vp); 12975 12976 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12977 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12978 12979 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 12980 if (ep->error || ep->stat) 12981 return; 12982 12983 argop->argop = OP_LOCKU; 12984 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 12985 argsp->ctag = TAG_LOCKU_REINSTATE; 12986 else 12987 argsp->ctag = TAG_LOCKU; 12988 locku_args = &argop->nfs_argop4_u.oplocku; 12989 *locku_argsp = locku_args; 12990 12991 /* 12992 * XXX what should locku_args->locktype be? 12993 * setting to ALWAYS be READ_LT so at least 12994 * it is a valid locktype. 12995 */ 12996 12997 locku_args->locktype = READ_LT; 12998 12999 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13000 flk->l_pid; 13001 13002 /* 13003 * Get the lock owner stateid. If no lock owner 13004 * exists, return success. 13005 */ 13006 lop = find_lock_owner(rp, pid, LOWN_ANY); 13007 *lopp = lop; 13008 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13009 is_spec = TRUE; 13010 if (!lop || is_spec) { 13011 /* 13012 * No lock owner so no locks to unlock. 13013 * Return success. If there was a failed 13014 * reclaim earlier, the lock might still be 13015 * registered with the local locking code, 13016 * so notify it of the unlock. 13017 * 13018 * If the lockowner is using a special stateid, 13019 * then the original lock request (that created 13020 * this lockowner) was never successful, so we 13021 * have no lock to undo OTW. 13022 */ 13023 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13024 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13025 "(%ld) so return success", (long)pid)); 13026 13027 if (ctype == NFS4_LCK_CTYPE_NORM) 13028 flk->l_pid = curproc->p_pid; 13029 nfs4_register_lock_locally(vp, flk, flag, offset); 13030 /* 13031 * Release our hold and NULL out so final_cleanup 13032 * doesn't try to end a lock seqid sync we 13033 * never started. 13034 */ 13035 if (is_spec) { 13036 lock_owner_rele(lop); 13037 *lopp = NULL; 13038 } 13039 *skip_get_err = TRUE; 13040 *go_otwp = FALSE; 13041 return; 13042 } 13043 13044 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13045 if (ep->error == EAGAIN) { 13046 lock_owner_rele(lop); 13047 *lopp = NULL; 13048 return; 13049 } 13050 13051 mutex_enter(&lop->lo_lock); 13052 locku_args->lock_stateid = lop->lock_stateid; 13053 mutex_exit(&lop->lo_lock); 13054 locku_args->seqid = lop->lock_seqid + 1; 13055 13056 /* leave the ref count on lop, rele after RPC call */ 13057 13058 locku_args->offset = flk->l_start; 13059 locku_args->length = flk->l_len; 13060 if (flk->l_len == 0) 13061 locku_args->length = ~locku_args->length; 13062 13063 *go_otwp = TRUE; 13064 } 13065 13066 /* 13067 * Setup the LOCK4 arguments. 13068 * 13069 * Returns errors via the nfs4_error_t. 13070 * NFS4_OK no problems 13071 * NFS4ERR_DELAY caller should retry (like recovery retry) 13072 * (other) unrecoverable error 13073 */ 13074 static void 13075 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13076 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13077 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13078 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13079 { 13080 LOCK4args *lock_args; 13081 nfs4_open_owner_t *oop = NULL; 13082 nfs4_open_stream_t *osp = NULL; 13083 nfs4_lock_owner_t *lop = NULL; 13084 pid_t pid; 13085 rnode4_t *rp = VTOR4(vp); 13086 13087 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13088 13089 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13090 if (ep->error || ep->stat != NFS4_OK) 13091 return; 13092 13093 argop->argop = OP_LOCK; 13094 if (ctype == NFS4_LCK_CTYPE_NORM) 13095 argsp->ctag = TAG_LOCK; 13096 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13097 argsp->ctag = TAG_RELOCK; 13098 else 13099 argsp->ctag = TAG_LOCK_REINSTATE; 13100 lock_args = &argop->nfs_argop4_u.oplock; 13101 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13102 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13103 /* 13104 * Get the lock owner. If no lock owner exists, 13105 * create a 'temporary' one and grab the open seqid 13106 * synchronization (which puts a hold on the open 13107 * owner and open stream). 13108 * This also grabs the lock seqid synchronization. 13109 */ 13110 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13111 ep->stat = 13112 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13113 13114 if (ep->stat != NFS4_OK) 13115 goto out; 13116 13117 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13118 &lock_args->locker); 13119 13120 lock_args->offset = flk->l_start; 13121 lock_args->length = flk->l_len; 13122 if (flk->l_len == 0) 13123 lock_args->length = ~lock_args->length; 13124 *lock_argsp = lock_args; 13125 out: 13126 *oopp = oop; 13127 *ospp = osp; 13128 *lopp = lop; 13129 } 13130 13131 /* 13132 * After we get the reply from the server, record the proper information 13133 * for possible resend lock requests. 13134 * 13135 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13136 */ 13137 static void 13138 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13139 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13140 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13141 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13142 { 13143 bool_t unlock = (flk->l_type == F_UNLCK); 13144 13145 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13146 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13147 ctype == NFS4_LCK_CTYPE_REINSTATE); 13148 13149 if (error != 0 && !unlock) { 13150 NFS4_DEBUG((nfs4_lost_rqst_debug || 13151 nfs4_client_lock_debug), (CE_NOTE, 13152 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13153 " for lop %p", (void *)lop)); 13154 ASSERT(lop != NULL); 13155 mutex_enter(&lop->lo_lock); 13156 lop->lo_pending_rqsts = 1; 13157 mutex_exit(&lop->lo_lock); 13158 } 13159 13160 lost_rqstp->lr_putfirst = FALSE; 13161 lost_rqstp->lr_op = 0; 13162 13163 /* 13164 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13165 * recovery purposes so that the lock request that was sent 13166 * can be saved and re-issued later. Ditto for EIO from a forced 13167 * unmount. This is done to have the client's local locking state 13168 * match the v4 server's state; that is, the request was 13169 * potentially received and accepted by the server but the client 13170 * thinks it was not. 13171 */ 13172 if (error == ETIMEDOUT || error == EINTR || 13173 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13174 NFS4_DEBUG((nfs4_lost_rqst_debug || 13175 nfs4_client_lock_debug), (CE_NOTE, 13176 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13177 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13178 (void *)lop, (void *)oop, (void *)osp)); 13179 if (unlock) 13180 lost_rqstp->lr_op = OP_LOCKU; 13181 else { 13182 lost_rqstp->lr_op = OP_LOCK; 13183 lost_rqstp->lr_locktype = locktype; 13184 } 13185 /* 13186 * Objects are held and rele'd via the recovery code. 13187 * See nfs4_save_lost_rqst. 13188 */ 13189 lost_rqstp->lr_vp = vp; 13190 lost_rqstp->lr_dvp = NULL; 13191 lost_rqstp->lr_oop = oop; 13192 lost_rqstp->lr_osp = osp; 13193 lost_rqstp->lr_lop = lop; 13194 lost_rqstp->lr_cr = cr; 13195 switch (ctype) { 13196 case NFS4_LCK_CTYPE_NORM: 13197 flk->l_pid = ttoproc(curthread)->p_pid; 13198 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13199 break; 13200 case NFS4_LCK_CTYPE_REINSTATE: 13201 lost_rqstp->lr_putfirst = TRUE; 13202 lost_rqstp->lr_ctype = ctype; 13203 break; 13204 default: 13205 break; 13206 } 13207 lost_rqstp->lr_flk = flk; 13208 } 13209 } 13210 13211 /* 13212 * Update lop's seqid. Also update the seqid stored in a resend request, 13213 * if any. (Some recovery errors increment the seqid, and we may have to 13214 * send the resend request again.) 13215 */ 13216 13217 static void 13218 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13219 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13220 { 13221 if (lock_args) { 13222 if (lock_args->locker.new_lock_owner == TRUE) 13223 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13224 else { 13225 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13226 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13227 } 13228 } else if (locku_args) { 13229 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13230 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13231 } 13232 } 13233 13234 /* 13235 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13236 * COMPOUND4 args/res for calls that need to retry. 13237 * Switches the *cred_otwp to base_cr. 13238 */ 13239 static void 13240 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13241 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13242 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13243 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13244 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13245 { 13246 nfs4_open_owner_t *oop = *oopp; 13247 nfs4_open_stream_t *osp = *ospp; 13248 nfs4_lock_owner_t *lop = *lopp; 13249 nfs_argop4 *argop = (*argspp)->array; 13250 13251 if (*did_start_fop) { 13252 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13253 needrecov); 13254 *did_start_fop = FALSE; 13255 } 13256 ASSERT((*argspp)->array_len == 2); 13257 if (argop[1].argop == OP_LOCK) 13258 nfs4args_lock_free(&argop[1]); 13259 else if (argop[1].argop == OP_LOCKT) 13260 nfs4args_lockt_free(&argop[1]); 13261 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13262 if (!error) 13263 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13264 *argspp = NULL; 13265 *respp = NULL; 13266 13267 if (lop) { 13268 nfs4_end_lock_seqid_sync(lop); 13269 lock_owner_rele(lop); 13270 *lopp = NULL; 13271 } 13272 13273 /* need to free up the reference on osp for lock args */ 13274 if (osp != NULL) { 13275 open_stream_rele(osp, VTOR4(vp)); 13276 *ospp = NULL; 13277 } 13278 13279 /* need to free up the reference on oop for lock args */ 13280 if (oop != NULL) { 13281 nfs4_end_open_seqid_sync(oop); 13282 open_owner_rele(oop); 13283 *oopp = NULL; 13284 } 13285 13286 crfree(*cred_otwp); 13287 *cred_otwp = base_cr; 13288 crhold(*cred_otwp); 13289 } 13290 13291 /* 13292 * Function to process the client's recovery for nfs4frlock. 13293 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13294 * 13295 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13296 * COMPOUND4 args/res for calls that need to retry. 13297 * 13298 * Note: the rp's r_lkserlock is *not* dropped during this path. 13299 */ 13300 static bool_t 13301 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13302 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13303 LOCK4args *lock_args, LOCKU4args *locku_args, 13304 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13305 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13306 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13307 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13308 { 13309 nfs4_open_owner_t *oop = *oopp; 13310 nfs4_open_stream_t *osp = *ospp; 13311 nfs4_lock_owner_t *lop = *lopp; 13312 13313 bool_t abort, retry; 13314 13315 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13316 ASSERT((*argspp) != NULL); 13317 ASSERT((*respp) != NULL); 13318 if (lock_args || locku_args) 13319 ASSERT(lop != NULL); 13320 13321 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13322 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13323 13324 retry = TRUE; 13325 abort = FALSE; 13326 if (needrecov) { 13327 nfs4_bseqid_entry_t *bsep = NULL; 13328 nfs_opnum4 op; 13329 13330 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13331 13332 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13333 seqid4 seqid; 13334 13335 if (lock_args) { 13336 if (lock_args->locker.new_lock_owner == TRUE) 13337 seqid = lock_args->locker.locker4_u. 13338 open_owner.open_seqid; 13339 else 13340 seqid = lock_args->locker.locker4_u. 13341 lock_owner.lock_seqid; 13342 } else if (locku_args) { 13343 seqid = locku_args->seqid; 13344 } else { 13345 seqid = 0; 13346 } 13347 13348 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13349 flk->l_pid, (*argspp)->ctag, seqid); 13350 } 13351 13352 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13353 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13354 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13355 NULL, op, bsep); 13356 13357 if (bsep) 13358 kmem_free(bsep, sizeof (*bsep)); 13359 } 13360 13361 /* 13362 * Return that we do not want to retry the request for 3 cases: 13363 * 1. If we received EINTR or are bailing out because of a forced 13364 * unmount, we came into this code path just for the sake of 13365 * initiating recovery, we now need to return the error. 13366 * 2. If we have aborted recovery. 13367 * 3. We received NFS4ERR_BAD_SEQID. 13368 */ 13369 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13370 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13371 retry = FALSE; 13372 13373 if (*did_start_fop == TRUE) { 13374 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13375 needrecov); 13376 *did_start_fop = FALSE; 13377 } 13378 13379 if (retry == TRUE) { 13380 nfs_argop4 *argop; 13381 13382 argop = (*argspp)->array; 13383 ASSERT((*argspp)->array_len == 2); 13384 13385 if (argop[1].argop == OP_LOCK) 13386 nfs4args_lock_free(&argop[1]); 13387 else if (argop[1].argop == OP_LOCKT) 13388 nfs4args_lockt_free(&argop[1]); 13389 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13390 if (!ep->error) 13391 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13392 *respp = NULL; 13393 *argspp = NULL; 13394 } 13395 13396 if (lop != NULL) { 13397 nfs4_end_lock_seqid_sync(lop); 13398 lock_owner_rele(lop); 13399 } 13400 13401 *lopp = NULL; 13402 13403 /* need to free up the reference on osp for lock args */ 13404 if (osp != NULL) { 13405 open_stream_rele(osp, rp); 13406 *ospp = NULL; 13407 } 13408 13409 /* need to free up the reference on oop for lock args */ 13410 if (oop != NULL) { 13411 nfs4_end_open_seqid_sync(oop); 13412 open_owner_rele(oop); 13413 *oopp = NULL; 13414 } 13415 13416 return (retry); 13417 } 13418 13419 /* 13420 * Handles the succesful reply from the server for nfs4frlock. 13421 */ 13422 static void 13423 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13424 vnode_t *vp, int flag, u_offset_t offset, 13425 nfs4_lost_rqst_t *resend_rqstp) 13426 { 13427 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13428 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13429 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13430 if (ctype == NFS4_LCK_CTYPE_NORM) { 13431 flk->l_pid = ttoproc(curthread)->p_pid; 13432 /* 13433 * We do not register lost locks locally in 13434 * the 'resend' case since the user/application 13435 * doesn't think we have the lock. 13436 */ 13437 ASSERT(!resend_rqstp); 13438 nfs4_register_lock_locally(vp, flk, flag, offset); 13439 } 13440 } 13441 } 13442 13443 /* 13444 * Handle the DENIED reply from the server for nfs4frlock. 13445 * Returns TRUE if we should retry the request; FALSE otherwise. 13446 * 13447 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13448 * COMPOUND4 args/res for calls that need to retry. Can also 13449 * drop and regrab the r_lkserlock. 13450 */ 13451 static bool_t 13452 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13453 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13454 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13455 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13456 nfs4_recov_state_t *recov_statep, int needrecov, 13457 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13458 clock_t *tick_delayp, short *whencep, int *errorp, 13459 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13460 bool_t *skip_get_err) 13461 { 13462 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13463 13464 if (lock_args) { 13465 nfs4_open_owner_t *oop = *oopp; 13466 nfs4_open_stream_t *osp = *ospp; 13467 nfs4_lock_owner_t *lop = *lopp; 13468 int intr; 13469 13470 /* 13471 * Blocking lock needs to sleep and retry from the request. 13472 * 13473 * Do not block and wait for 'resend' or 'reinstate' 13474 * lock requests, just return the error. 13475 * 13476 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13477 */ 13478 if (cmd == F_SETLKW) { 13479 rnode4_t *rp = VTOR4(vp); 13480 nfs_argop4 *argop = (*argspp)->array; 13481 13482 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13483 13484 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13485 recov_statep, needrecov); 13486 *did_start_fop = FALSE; 13487 ASSERT((*argspp)->array_len == 2); 13488 if (argop[1].argop == OP_LOCK) 13489 nfs4args_lock_free(&argop[1]); 13490 else if (argop[1].argop == OP_LOCKT) 13491 nfs4args_lockt_free(&argop[1]); 13492 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13493 if (*respp) 13494 (void) xdr_free(xdr_COMPOUND4res_clnt, 13495 (caddr_t)*respp); 13496 *argspp = NULL; 13497 *respp = NULL; 13498 nfs4_end_lock_seqid_sync(lop); 13499 lock_owner_rele(lop); 13500 *lopp = NULL; 13501 if (osp != NULL) { 13502 open_stream_rele(osp, rp); 13503 *ospp = NULL; 13504 } 13505 if (oop != NULL) { 13506 nfs4_end_open_seqid_sync(oop); 13507 open_owner_rele(oop); 13508 *oopp = NULL; 13509 } 13510 13511 nfs_rw_exit(&rp->r_lkserlock); 13512 13513 intr = nfs4_block_and_wait(tick_delayp, rp); 13514 13515 if (intr) { 13516 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13517 RW_WRITER, FALSE); 13518 *errorp = EINTR; 13519 return (FALSE); 13520 } 13521 13522 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13523 RW_WRITER, FALSE); 13524 13525 /* 13526 * Make sure we are still safe to lock with 13527 * regards to mmapping. 13528 */ 13529 if (!nfs4_safelock(vp, flk, cr)) { 13530 *errorp = EAGAIN; 13531 return (FALSE); 13532 } 13533 13534 return (TRUE); 13535 } 13536 if (ctype == NFS4_LCK_CTYPE_NORM) 13537 *errorp = EAGAIN; 13538 *skip_get_err = TRUE; 13539 flk->l_whence = 0; 13540 *whencep = 0; 13541 return (FALSE); 13542 } else if (lockt_args) { 13543 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13544 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13545 13546 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13547 flk, lockt_args); 13548 13549 /* according to NLM code */ 13550 *errorp = 0; 13551 *whencep = 0; 13552 *skip_get_err = TRUE; 13553 return (FALSE); 13554 } 13555 return (FALSE); 13556 } 13557 13558 /* 13559 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13560 */ 13561 static void 13562 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13563 { 13564 switch (resp->status) { 13565 case NFS4ERR_ACCESS: 13566 case NFS4ERR_ADMIN_REVOKED: 13567 case NFS4ERR_BADHANDLE: 13568 case NFS4ERR_BAD_RANGE: 13569 case NFS4ERR_BAD_SEQID: 13570 case NFS4ERR_BAD_STATEID: 13571 case NFS4ERR_BADXDR: 13572 case NFS4ERR_DEADLOCK: 13573 case NFS4ERR_DELAY: 13574 case NFS4ERR_EXPIRED: 13575 case NFS4ERR_FHEXPIRED: 13576 case NFS4ERR_GRACE: 13577 case NFS4ERR_INVAL: 13578 case NFS4ERR_ISDIR: 13579 case NFS4ERR_LEASE_MOVED: 13580 case NFS4ERR_LOCK_NOTSUPP: 13581 case NFS4ERR_LOCK_RANGE: 13582 case NFS4ERR_MOVED: 13583 case NFS4ERR_NOFILEHANDLE: 13584 case NFS4ERR_NO_GRACE: 13585 case NFS4ERR_OLD_STATEID: 13586 case NFS4ERR_OPENMODE: 13587 case NFS4ERR_RECLAIM_BAD: 13588 case NFS4ERR_RECLAIM_CONFLICT: 13589 case NFS4ERR_RESOURCE: 13590 case NFS4ERR_SERVERFAULT: 13591 case NFS4ERR_STALE: 13592 case NFS4ERR_STALE_CLIENTID: 13593 case NFS4ERR_STALE_STATEID: 13594 return; 13595 default: 13596 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13597 "nfs4frlock_results_default: got unrecognizable " 13598 "res.status %d", resp->status)); 13599 *errorp = NFS4ERR_INVAL; 13600 } 13601 } 13602 13603 /* 13604 * The lock request was successful, so update the client's state. 13605 */ 13606 static void 13607 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13608 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13609 vnode_t *vp, flock64_t *flk, cred_t *cr, 13610 nfs4_lost_rqst_t *resend_rqstp) 13611 { 13612 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13613 13614 if (lock_args) { 13615 LOCK4res *lock_res; 13616 13617 lock_res = &resop->nfs_resop4_u.oplock; 13618 /* update the stateid with server's response */ 13619 13620 if (lock_args->locker.new_lock_owner == TRUE) { 13621 mutex_enter(&lop->lo_lock); 13622 lop->lo_just_created = NFS4_PERM_CREATED; 13623 mutex_exit(&lop->lo_lock); 13624 } 13625 13626 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13627 13628 /* 13629 * If the lock was the result of a resending a lost 13630 * request, we've synched up the stateid and seqid 13631 * with the server, but now the server might be out of sync 13632 * with what the application thinks it has for locks. 13633 * Clean that up here. It's unclear whether we should do 13634 * this even if the filesystem has been forcibly unmounted. 13635 * For most servers, it's probably wasted effort, but 13636 * RFC3530 lets servers require that unlocks exactly match 13637 * the locks that are held. 13638 */ 13639 if (resend_rqstp != NULL && 13640 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13641 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 13642 } else { 13643 flk->l_whence = 0; 13644 } 13645 } else if (locku_args) { 13646 LOCKU4res *locku_res; 13647 13648 locku_res = &resop->nfs_resop4_u.oplocku; 13649 13650 /* Update the stateid with the server's response */ 13651 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 13652 } else if (lockt_args) { 13653 /* Switch the lock type to express success, see fcntl */ 13654 flk->l_type = F_UNLCK; 13655 flk->l_whence = 0; 13656 } 13657 } 13658 13659 /* 13660 * Do final cleanup before exiting nfs4frlock. 13661 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13662 * COMPOUND4 args/res for calls that haven't already. 13663 */ 13664 static void 13665 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 13666 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 13667 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 13668 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13669 short whence, u_offset_t offset, struct lm_sysid *ls, 13670 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 13671 bool_t did_start_fop, bool_t skip_get_err, 13672 cred_t *cred_otw, cred_t *cred) 13673 { 13674 mntinfo4_t *mi = VTOMI4(vp); 13675 rnode4_t *rp = VTOR4(vp); 13676 int error = *errorp; 13677 nfs_argop4 *argop; 13678 13679 ASSERT(curproc->p_zone == mi->mi_zone); 13680 /* 13681 * The client recovery code wants the raw status information, 13682 * so don't map the NFS status code to an errno value for 13683 * non-normal call types. 13684 */ 13685 if (ctype == NFS4_LCK_CTYPE_NORM) { 13686 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 13687 *errorp = geterrno4(resp->status); 13688 if (did_start_fop == TRUE) 13689 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 13690 needrecov); 13691 13692 if (!error && resp && resp->status == NFS4_OK) { 13693 /* 13694 * We've established a new lock on the server, so invalidate 13695 * the pages associated with the vnode to get the most up to 13696 * date pages from the server after acquiring the lock. We 13697 * want to be sure that the read operation gets the newest data. 13698 * N.B. 13699 * We used to do this in nfs4frlock_results_ok but that doesn't 13700 * work since VOP_PUTPAGE can call nfs4_commit which calls 13701 * nfs4_start_fop. We flush the pages below after calling 13702 * nfs4_end_fop above 13703 */ 13704 int error; 13705 13706 error = VOP_PUTPAGE(vp, (u_offset_t)0, 13707 0, B_INVAL, cred); 13708 13709 if (error && (error == ENOSPC || error == EDQUOT)) { 13710 rnode4_t *rp = VTOR4(vp); 13711 13712 mutex_enter(&rp->r_statelock); 13713 if (!rp->r_error) 13714 rp->r_error = error; 13715 mutex_exit(&rp->r_statelock); 13716 } 13717 } 13718 } 13719 if (argsp) { 13720 ASSERT(argsp->array_len == 2); 13721 argop = argsp->array; 13722 if (argop[1].argop == OP_LOCK) 13723 nfs4args_lock_free(&argop[1]); 13724 else if (argop[1].argop == OP_LOCKT) 13725 nfs4args_lockt_free(&argop[1]); 13726 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13727 if (resp) 13728 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 13729 } 13730 13731 /* free the reference on the lock owner */ 13732 if (lop != NULL) { 13733 nfs4_end_lock_seqid_sync(lop); 13734 lock_owner_rele(lop); 13735 } 13736 13737 /* need to free up the reference on osp for lock args */ 13738 if (osp != NULL) 13739 open_stream_rele(osp, rp); 13740 13741 /* need to free up the reference on oop for lock args */ 13742 if (oop != NULL) { 13743 nfs4_end_open_seqid_sync(oop); 13744 open_owner_rele(oop); 13745 } 13746 13747 (void) convoff(vp, flk, whence, offset); 13748 13749 lm_rel_sysid(ls); 13750 13751 /* 13752 * Record debug information in the event we get EINVAL. 13753 */ 13754 mutex_enter(&mi->mi_lock); 13755 if (*errorp == EINVAL && (lock_args || locku_args) && 13756 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 13757 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 13758 zcmn_err(getzoneid(), CE_NOTE, 13759 "%s operation failed with " 13760 "EINVAL probably since the server, %s," 13761 " doesn't support POSIX style locking", 13762 lock_args ? "LOCK" : "LOCKU", 13763 mi->mi_curr_serv->sv_hostname); 13764 mi->mi_flags |= MI4_LOCK_DEBUG; 13765 } 13766 } 13767 mutex_exit(&mi->mi_lock); 13768 13769 if (cred_otw) 13770 crfree(cred_otw); 13771 } 13772 13773 /* 13774 * This calls the server and the local locking code. 13775 * 13776 * Client locks are registerred locally by oring the sysid with 13777 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 13778 * We need to distinguish between the two to avoid collision in case one 13779 * machine is used as both client and server. 13780 * 13781 * Blocking lock requests will continually retry to acquire the lock 13782 * forever. 13783 * 13784 * The ctype is defined as follows: 13785 * NFS4_LCK_CTYPE_NORM: normal lock request. 13786 * 13787 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 13788 * recovery, get the pid from flk instead of curproc, and don't reregister 13789 * the lock locally. 13790 * 13791 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 13792 * that we will use the information passed in via resend_rqstp to setup the 13793 * lock/locku request. This resend is the exact same request as the 'lost 13794 * lock', and is initiated by the recovery framework. A successful resend 13795 * request can initiate one or more reinstate requests. 13796 * 13797 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 13798 * does not trigger additional reinstate requests. This lock call type is 13799 * set for setting the v4 server's locking state back to match what the 13800 * client's local locking state is in the event of a received 'lost lock'. 13801 * 13802 * Errors are returned via the nfs4_error_t parameter. 13803 */ 13804 void 13805 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 13806 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 13807 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 13808 { 13809 COMPOUND4args_clnt args, *argsp = NULL; 13810 COMPOUND4res_clnt res, *resp = NULL; 13811 nfs_argop4 *argop; 13812 nfs_resop4 *resop; 13813 rnode4_t *rp; 13814 int doqueue = 1; 13815 clock_t tick_delay; /* delay in clock ticks */ 13816 struct lm_sysid *ls; 13817 LOCK4args *lock_args = NULL; 13818 LOCKU4args *locku_args = NULL; 13819 LOCKT4args *lockt_args = NULL; 13820 nfs4_open_owner_t *oop = NULL; 13821 nfs4_open_stream_t *osp = NULL; 13822 nfs4_lock_owner_t *lop = NULL; 13823 bool_t needrecov = FALSE; 13824 nfs4_recov_state_t recov_state; 13825 short whence; 13826 nfs4_op_hint_t op_hint; 13827 nfs4_lost_rqst_t lost_rqst; 13828 bool_t retry = FALSE; 13829 bool_t did_start_fop = FALSE; 13830 bool_t skip_get_err = FALSE; 13831 cred_t *cred_otw = NULL; 13832 bool_t recovonly; /* just queue request */ 13833 int frc_no_reclaim = 0; 13834 #ifdef DEBUG 13835 char *name; 13836 #endif 13837 13838 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13839 13840 #ifdef DEBUG 13841 name = fn_name(VTOSV(vp)->sv_name); 13842 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 13843 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 13844 "length %"PRIu64", pid %d, sysid %d, call type %s, " 13845 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 13846 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 13847 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 13848 resend_rqstp ? "TRUE" : "FALSE")); 13849 kmem_free(name, MAXNAMELEN); 13850 #endif 13851 13852 nfs4_error_zinit(ep); 13853 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 13854 if (ep->error) 13855 return; 13856 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 13857 if (ep->error) 13858 return; 13859 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 13860 vp, cr, &cred_otw); 13861 13862 recov_retry: 13863 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 13864 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 13865 rp = VTOR4(vp); 13866 13867 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 13868 &did_start_fop, &recovonly); 13869 13870 if (ep->error) 13871 goto out; 13872 13873 if (recovonly) { 13874 /* 13875 * Leave the request for the recovery system to deal with. 13876 */ 13877 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13878 ASSERT(cmd != F_GETLK); 13879 ASSERT(flk->l_type == F_UNLCK); 13880 13881 nfs4_error_init(ep, EINTR); 13882 needrecov = TRUE; 13883 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 13884 if (lop != NULL) { 13885 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 13886 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 13887 (void) nfs4_start_recovery(ep, 13888 VTOMI4(vp), vp, NULL, NULL, 13889 (lost_rqst.lr_op == OP_LOCK || 13890 lost_rqst.lr_op == OP_LOCKU) ? 13891 &lost_rqst : NULL, OP_LOCKU, NULL); 13892 lock_owner_rele(lop); 13893 lop = NULL; 13894 } 13895 flk->l_pid = curproc->p_pid; 13896 nfs4_register_lock_locally(vp, flk, flag, offset); 13897 goto out; 13898 } 13899 13900 /* putfh directory fh */ 13901 argop[0].argop = OP_CPUTFH; 13902 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 13903 13904 /* 13905 * Set up the over-the-wire arguments and get references to the 13906 * open owner, etc. 13907 */ 13908 13909 if (ctype == NFS4_LCK_CTYPE_RESEND || 13910 ctype == NFS4_LCK_CTYPE_REINSTATE) { 13911 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 13912 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 13913 } else { 13914 bool_t go_otw = TRUE; 13915 13916 ASSERT(resend_rqstp == NULL); 13917 13918 switch (cmd) { 13919 case F_GETLK: 13920 case F_O_GETLK: 13921 nfs4frlock_setup_lockt_args(ctype, &argop[1], 13922 &lockt_args, argsp, flk, rp); 13923 break; 13924 case F_SETLKW: 13925 case F_SETLK: 13926 if (flk->l_type == F_UNLCK) 13927 nfs4frlock_setup_locku_args(ctype, 13928 &argop[1], &locku_args, flk, 13929 &lop, ep, argsp, 13930 vp, flag, offset, cr, 13931 &skip_get_err, &go_otw); 13932 else 13933 nfs4frlock_setup_lock_args(ctype, 13934 &lock_args, &oop, &osp, &lop, &argop[1], 13935 argsp, flk, cmd, vp, cr, ep); 13936 13937 if (ep->error) 13938 goto out; 13939 13940 switch (ep->stat) { 13941 case NFS4_OK: 13942 break; 13943 case NFS4ERR_DELAY: 13944 /* recov thread never gets this error */ 13945 ASSERT(resend_rqstp == NULL); 13946 ASSERT(did_start_fop); 13947 13948 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13949 &recov_state, TRUE); 13950 did_start_fop = FALSE; 13951 if (argop[1].argop == OP_LOCK) 13952 nfs4args_lock_free(&argop[1]); 13953 else if (argop[1].argop == OP_LOCKT) 13954 nfs4args_lockt_free(&argop[1]); 13955 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13956 argsp = NULL; 13957 goto recov_retry; 13958 default: 13959 ep->error = EIO; 13960 goto out; 13961 } 13962 break; 13963 default: 13964 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13965 "nfs4_frlock: invalid cmd %d", cmd)); 13966 ep->error = EINVAL; 13967 goto out; 13968 } 13969 13970 if (!go_otw) 13971 goto out; 13972 } 13973 13974 /* XXX should we use the local reclock as a cache ? */ 13975 /* 13976 * Unregister the lock with the local locking code before 13977 * contacting the server. This avoids a potential race where 13978 * another process gets notified that it has been granted a lock 13979 * before we can unregister ourselves locally. 13980 */ 13981 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 13982 if (ctype == NFS4_LCK_CTYPE_NORM) 13983 flk->l_pid = ttoproc(curthread)->p_pid; 13984 nfs4_register_lock_locally(vp, flk, flag, offset); 13985 } 13986 13987 /* 13988 * Send the server the lock request. Continually loop with a delay 13989 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 13990 */ 13991 resp = &res; 13992 13993 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 13994 (CE_NOTE, 13995 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 13996 rnode4info(rp))); 13997 13998 if (lock_args && frc_no_reclaim) { 13999 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14000 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14001 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14002 lock_args->reclaim = FALSE; 14003 if (did_reclaimp) 14004 *did_reclaimp = 0; 14005 } 14006 14007 /* 14008 * Do the OTW call. 14009 */ 14010 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14011 14012 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14013 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14014 14015 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14016 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14017 "nfs4frlock: needrecov %d", needrecov)); 14018 14019 if (ep->error != 0 && !needrecov && ep->error != EACCES) 14020 goto out; 14021 14022 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14023 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14024 args.ctag); 14025 14026 if ((ep->error == EACCES || 14027 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14028 cred_otw != cr) { 14029 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14030 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14031 cr, &cred_otw); 14032 goto recov_retry; 14033 } 14034 14035 if (needrecov) { 14036 /* 14037 * LOCKT requests don't need to recover from lost 14038 * requests since they don't create/modify state. 14039 */ 14040 if ((ep->error == EINTR || 14041 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14042 lockt_args) 14043 goto out; 14044 /* 14045 * Do not attempt recovery for requests initiated by 14046 * the recovery framework. Let the framework redrive them. 14047 */ 14048 if (ctype != NFS4_LCK_CTYPE_NORM) 14049 goto out; 14050 else { 14051 ASSERT(resend_rqstp == NULL); 14052 } 14053 14054 nfs4frlock_save_lost_rqst(ctype, ep->error, 14055 flk_to_locktype(cmd, flk->l_type), 14056 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14057 14058 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14059 &resp, lock_args, locku_args, &oop, &osp, &lop, 14060 rp, vp, &recov_state, op_hint, &did_start_fop, 14061 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14062 14063 if (retry) { 14064 ASSERT(oop == NULL); 14065 ASSERT(osp == NULL); 14066 ASSERT(lop == NULL); 14067 goto recov_retry; 14068 } 14069 goto out; 14070 } 14071 14072 /* 14073 * Process the reply. 14074 */ 14075 switch (resp->status) { 14076 case NFS4_OK: 14077 resop = &resp->array[1]; 14078 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14079 resend_rqstp); 14080 /* 14081 * Have a successful lock operation, now update state. 14082 */ 14083 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14084 resop, lop, vp, flk, cr, resend_rqstp); 14085 break; 14086 14087 case NFS4ERR_DENIED: 14088 resop = &resp->array[1]; 14089 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14090 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14091 &recov_state, needrecov, &argsp, &resp, 14092 &tick_delay, &whence, &ep->error, resop, cr, 14093 &did_start_fop, &skip_get_err); 14094 14095 if (retry) { 14096 ASSERT(oop == NULL); 14097 ASSERT(osp == NULL); 14098 ASSERT(lop == NULL); 14099 goto recov_retry; 14100 } 14101 break; 14102 /* 14103 * If the server won't let us reclaim, fall-back to trying to lock 14104 * the file from scratch. Code elsewhere will check the changeinfo 14105 * to ensure the file hasn't been changed. 14106 */ 14107 case NFS4ERR_NO_GRACE: 14108 if (lock_args && lock_args->reclaim == TRUE) { 14109 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14110 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14111 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14112 frc_no_reclaim = 1; 14113 /* clean up before retrying */ 14114 needrecov = 0; 14115 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14116 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14117 &recov_state, op_hint, &did_start_fop, NULL, flk); 14118 goto recov_retry; 14119 } 14120 /* FALLTHROUGH */ 14121 14122 default: 14123 nfs4frlock_results_default(resp, &ep->error); 14124 break; 14125 } 14126 out: 14127 /* 14128 * Process and cleanup from error. Make interrupted unlock 14129 * requests look successful, since they will be handled by the 14130 * client recovery code. 14131 */ 14132 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14133 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14134 lock_args, locku_args, did_start_fop, 14135 skip_get_err, cred_otw, cr); 14136 14137 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14138 (cmd == F_SETLK || cmd == F_SETLKW)) 14139 ep->error = 0; 14140 } 14141 14142 /* 14143 * nfs4_safelock: 14144 * 14145 * Return non-zero if the given lock request can be handled without 14146 * violating the constraints on concurrent mapping and locking. 14147 */ 14148 14149 static int 14150 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14151 { 14152 rnode4_t *rp = VTOR4(vp); 14153 struct vattr va; 14154 int error; 14155 14156 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14157 ASSERT(rp->r_mapcnt >= 0); 14158 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14159 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14160 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14161 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14162 14163 if (rp->r_mapcnt == 0) 14164 return (1); /* always safe if not mapped */ 14165 14166 /* 14167 * If the file is already mapped and there are locks, then they 14168 * should be all safe locks. So adding or removing a lock is safe 14169 * as long as the new request is safe (i.e., whole-file, meaning 14170 * length and starting offset are both zero). 14171 */ 14172 14173 if (bfp->l_start != 0 || bfp->l_len != 0) { 14174 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14175 "cannot lock a memory mapped file unless locking the " 14176 "entire file: start %"PRIx64", len %"PRIx64, 14177 bfp->l_start, bfp->l_len)); 14178 return (0); 14179 } 14180 14181 /* mandatory locking and mapping don't mix */ 14182 va.va_mask = AT_MODE; 14183 error = VOP_GETATTR(vp, &va, 0, cr); 14184 if (error != 0) { 14185 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14186 "getattr error %d", error)); 14187 return (0); /* treat errors conservatively */ 14188 } 14189 if (MANDLOCK(vp, va.va_mode)) { 14190 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14191 "cannot mandatory lock and mmap a file")); 14192 return (0); 14193 } 14194 14195 return (1); 14196 } 14197 14198 14199 /* 14200 * Register the lock locally within Solaris. 14201 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14202 * recording locks locally. 14203 * 14204 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14205 * are registered locally. 14206 */ 14207 void 14208 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14209 u_offset_t offset) 14210 { 14211 int oldsysid; 14212 int error; 14213 #ifdef DEBUG 14214 char *name; 14215 #endif 14216 14217 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14218 14219 #ifdef DEBUG 14220 name = fn_name(VTOSV(vp)->sv_name); 14221 NFS4_DEBUG(nfs4_client_lock_debug, 14222 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14223 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14224 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14225 flk->l_sysid)); 14226 kmem_free(name, MAXNAMELEN); 14227 #endif 14228 14229 /* register the lock with local locking */ 14230 oldsysid = flk->l_sysid; 14231 flk->l_sysid |= LM_SYSID_CLIENT; 14232 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14233 #ifdef DEBUG 14234 if (error != 0) { 14235 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14236 "nfs4_register_lock_locally: could not register with" 14237 " local locking")); 14238 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14239 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14240 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14241 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14242 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14243 flk->l_type, flk->l_start, flk->l_len)); 14244 (void) reclock(vp, flk, 0, flag, offset, NULL); 14245 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14246 "blocked by pid %d sysid 0x%x type %d " 14247 "off 0x%" PRIx64 " len 0x%" PRIx64, 14248 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14249 flk->l_len)); 14250 } 14251 #endif 14252 flk->l_sysid = oldsysid; 14253 } 14254 14255 /* 14256 * nfs4_lockrelease: 14257 * 14258 * Release any locks on the given vnode that are held by the current 14259 * process. Also removes the lock owner (if one exists) from the rnode's 14260 * list. 14261 */ 14262 static int 14263 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14264 { 14265 flock64_t ld; 14266 int ret, error; 14267 rnode4_t *rp; 14268 nfs4_lock_owner_t *lop; 14269 nfs4_recov_state_t recov_state; 14270 mntinfo4_t *mi; 14271 bool_t possible_orphan = FALSE; 14272 bool_t recovonly; 14273 14274 ASSERT((uintptr_t)vp > KERNELBASE); 14275 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14276 14277 rp = VTOR4(vp); 14278 mi = VTOMI4(vp); 14279 14280 /* 14281 * If we have not locked anything then we can 14282 * just return since we have no work to do. 14283 */ 14284 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14285 return (0); 14286 } 14287 14288 /* 14289 * We need to comprehend that another thread may 14290 * kick off recovery and the lock_owner we have stashed 14291 * in lop might be invalid so we should NOT cache it 14292 * locally! 14293 */ 14294 recov_state.rs_flags = 0; 14295 recov_state.rs_num_retry_despite_err = 0; 14296 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14297 &recovonly); 14298 if (error) { 14299 mutex_enter(&rp->r_statelock); 14300 rp->r_flags |= R4LODANGLERS; 14301 mutex_exit(&rp->r_statelock); 14302 return (error); 14303 } 14304 14305 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14306 14307 /* 14308 * Check if the lock owner might have a lock (request was sent but 14309 * no response was received). Also check if there are any remote 14310 * locks on the file. (In theory we shouldn't have to make this 14311 * second check if there's no lock owner, but for now we'll be 14312 * conservative and do it anyway.) If either condition is true, 14313 * send an unlock for the entire file to the server. 14314 * 14315 * Note that no explicit synchronization is needed here. At worst, 14316 * flk_has_remote_locks() will return a false positive, in which case 14317 * the unlock call wastes time but doesn't harm correctness. 14318 */ 14319 14320 if (lop) { 14321 mutex_enter(&lop->lo_lock); 14322 possible_orphan = lop->lo_pending_rqsts; 14323 mutex_exit(&lop->lo_lock); 14324 lock_owner_rele(lop); 14325 } 14326 14327 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14328 14329 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14330 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14331 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14332 (void *)lop)); 14333 14334 if (possible_orphan || flk_has_remote_locks(vp)) { 14335 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14336 ld.l_whence = 0; /* unlock from start of file */ 14337 ld.l_start = 0; 14338 ld.l_len = 0; /* do entire file */ 14339 14340 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr); 14341 14342 if (ret != 0) { 14343 /* 14344 * If VOP_FRLOCK fails, make sure we unregister 14345 * local locks before we continue. 14346 */ 14347 ld.l_pid = ttoproc(curthread)->p_pid; 14348 nfs4_register_lock_locally(vp, &ld, flag, offset); 14349 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14350 "nfs4_lockrelease: lock release error on vp" 14351 " %p: error %d.\n", (void *)vp, ret)); 14352 } 14353 } 14354 14355 recov_state.rs_flags = 0; 14356 recov_state.rs_num_retry_despite_err = 0; 14357 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14358 &recovonly); 14359 if (error) { 14360 mutex_enter(&rp->r_statelock); 14361 rp->r_flags |= R4LODANGLERS; 14362 mutex_exit(&rp->r_statelock); 14363 return (error); 14364 } 14365 14366 /* 14367 * So, here we're going to need to retrieve the lock-owner 14368 * again (in case recovery has done a switch-a-roo) and 14369 * remove it because we can. 14370 */ 14371 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14372 14373 if (lop) { 14374 nfs4_rnode_remove_lock_owner(rp, lop); 14375 lock_owner_rele(lop); 14376 } 14377 14378 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14379 return (0); 14380 } 14381 14382 /* 14383 * Wait for 'tick_delay' clock ticks. 14384 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14385 * NOTE: lock_lease_time is in seconds. 14386 * 14387 * XXX For future improvements, should implement a waiting queue scheme. 14388 */ 14389 static int 14390 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14391 { 14392 long milliseconds_delay; 14393 time_t lock_lease_time; 14394 14395 /* wait tick_delay clock ticks or siginteruptus */ 14396 if (delay_sig(*tick_delay)) { 14397 return (EINTR); 14398 } 14399 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14400 "reissue the lock request: blocked for %ld clock ticks: %ld " 14401 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14402 14403 /* get the lease time */ 14404 lock_lease_time = r2lease_time(rp); 14405 14406 /* drv_hztousec converts ticks to microseconds */ 14407 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14408 if (milliseconds_delay < lock_lease_time * 1000) { 14409 *tick_delay = 2 * *tick_delay; 14410 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14411 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14412 } 14413 return (0); 14414 } 14415 14416 14417 void 14418 nfs4_vnops_init(void) 14419 { 14420 } 14421 14422 void 14423 nfs4_vnops_fini(void) 14424 { 14425 } 14426 14427 /* 14428 * Return a reference to the directory (parent) vnode for a given vnode, 14429 * using the saved pathname information and the directory file handle. The 14430 * caller is responsible for disposing of the reference. 14431 * Returns zero or an errno value. 14432 * 14433 * Caller should set need_start_op to FALSE if it is the recovery 14434 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14435 */ 14436 int 14437 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14438 { 14439 svnode_t *svnp; 14440 vnode_t *dvp = NULL; 14441 servinfo4_t *svp; 14442 nfs4_fname_t *mfname; 14443 int error; 14444 14445 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14446 14447 if (vp->v_flag & VROOT) { 14448 nfs4_sharedfh_t *sfh; 14449 nfs_fh4 fh; 14450 mntinfo4_t *mi; 14451 14452 ASSERT(vp->v_type == VREG); 14453 14454 mi = VTOMI4(vp); 14455 svp = mi->mi_curr_serv; 14456 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14457 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14458 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14459 sfh = sfh4_get(&fh, VTOMI4(vp)); 14460 nfs_rw_exit(&svp->sv_lock); 14461 mfname = mi->mi_fname; 14462 fn_hold(mfname); 14463 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14464 sfh4_rele(&sfh); 14465 14466 if (dvp->v_type == VNON) 14467 dvp->v_type = VDIR; 14468 *dvpp = dvp; 14469 return (0); 14470 } 14471 14472 svnp = VTOSV(vp); 14473 14474 if (svnp == NULL) { 14475 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14476 "shadow node is NULL")); 14477 return (EINVAL); 14478 } 14479 14480 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14481 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14482 "shadow node name or dfh val == NULL")); 14483 return (EINVAL); 14484 } 14485 14486 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14487 (int)need_start_op); 14488 if (error != 0) { 14489 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14490 "nfs4_make_dotdot returned %d", error)); 14491 return (error); 14492 } 14493 if (!dvp) { 14494 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14495 "nfs4_make_dotdot returned a NULL dvp")); 14496 return (EIO); 14497 } 14498 if (dvp->v_type == VNON) 14499 dvp->v_type = VDIR; 14500 ASSERT(dvp->v_type == VDIR); 14501 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14502 mutex_enter(&dvp->v_lock); 14503 dvp->v_flag |= V_XATTRDIR; 14504 mutex_exit(&dvp->v_lock); 14505 } 14506 *dvpp = dvp; 14507 return (0); 14508 } 14509 14510 /* 14511 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14512 * length that fnamep can accept, including the trailing null. 14513 * Returns 0 if okay, returns an errno value if there was a problem. 14514 */ 14515 14516 int 14517 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14518 { 14519 char *fn; 14520 int err = 0; 14521 servinfo4_t *svp; 14522 svnode_t *shvp; 14523 14524 /* 14525 * If the file being opened has VROOT set, then this is 14526 * a "file" mount. sv_name will not be interesting, so 14527 * go back to the servinfo4 to get the original mount 14528 * path and strip off all but the final edge. Otherwise 14529 * just return the name from the shadow vnode. 14530 */ 14531 14532 if (vp->v_flag & VROOT) { 14533 14534 svp = VTOMI4(vp)->mi_curr_serv; 14535 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14536 14537 fn = strrchr(svp->sv_path, '/'); 14538 if (fn == NULL) 14539 err = EINVAL; 14540 else 14541 fn++; 14542 } else { 14543 shvp = VTOSV(vp); 14544 fn = fn_name(shvp->sv_name); 14545 } 14546 14547 if (err == 0) 14548 if (strlen(fn) < maxlen) 14549 (void) strcpy(fnamep, fn); 14550 else 14551 err = ENAMETOOLONG; 14552 14553 if (vp->v_flag & VROOT) 14554 nfs_rw_exit(&svp->sv_lock); 14555 else 14556 kmem_free(fn, MAXNAMELEN); 14557 14558 return (err); 14559 } 14560 14561 /* 14562 * If the vnode has pages, run the list and check for 14563 * any that are still dangling. We call this function 14564 * before the OTW CLOSE occurs so we can B_INVAL the 14565 * danglers. 14566 */ 14567 static int 14568 nfs4_dross_pages(vnode_t *vp) 14569 { 14570 page_t *pp; 14571 kmutex_t *vphm; 14572 rnode4_t *rp; 14573 14574 /* make sure we're looking at the master vnode, not a shadow */ 14575 rp = VTOR4(vp); 14576 if (IS_SHADOW(vp, rp)) 14577 vp = RTOV4(rp); 14578 14579 vphm = page_vnode_mutex(vp); 14580 mutex_enter(vphm); 14581 if ((pp = vp->v_pages) != NULL) { 14582 do { 14583 if (pp->p_fsdata != C_NOCOMMIT) { 14584 mutex_exit(vphm); 14585 return (1); 14586 } 14587 } while ((pp = pp->p_vpnext) != vp->v_pages); 14588 } 14589 mutex_exit(vphm); 14590 14591 return (0); 14592 } 14593 14594 /* 14595 * Bookkeeping for a close that doesn't need to go over the wire. 14596 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14597 * it is left at 1. 14598 */ 14599 void 14600 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14601 { 14602 rnode4_t *rp; 14603 mntinfo4_t *mi; 14604 14605 mi = VTOMI4(vp); 14606 rp = VTOR4(vp); 14607 14608 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14609 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14610 ASSERT(curproc->p_zone == mi->mi_zone); 14611 ASSERT(mutex_owned(&osp->os_sync_lock)); 14612 ASSERT(*have_lockp); 14613 14614 if (!osp->os_valid || 14615 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14616 return; 14617 } 14618 14619 /* 14620 * This removes the reference obtained at OPEN; ie, 14621 * when the open stream structure was created. 14622 * 14623 * We don't have to worry about calling 'open_stream_rele' 14624 * since we our currently holding a reference to this 14625 * open stream which means the count can not go to 0 with 14626 * this decrement. 14627 */ 14628 ASSERT(osp->os_ref_count >= 2); 14629 osp->os_ref_count--; 14630 osp->os_valid = 0; 14631 mutex_exit(&osp->os_sync_lock); 14632 *have_lockp = 0; 14633 14634 nfs4_dec_state_ref_count(mi); 14635 } 14636 14637 /* 14638 * Close all remaining open streams on the rnode. These open streams 14639 * could be here because: 14640 * - The close attempted at either close or delmap failed 14641 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14642 * - Someone did mknod on a regular file but never opened it 14643 */ 14644 int 14645 nfs4close_all(vnode_t *vp, cred_t *cr) 14646 { 14647 nfs4_open_stream_t *osp; 14648 int error; 14649 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14650 rnode4_t *rp; 14651 14652 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14653 14654 error = 0; 14655 rp = VTOR4(vp); 14656 14657 /* 14658 * At this point, all we know is that the last time 14659 * someone called vn_rele, the count was 1. Since then, 14660 * the vnode could have been re-activated. We want to 14661 * loop through the open streams and close each one, but 14662 * we have to be careful since once we release the rnode 14663 * hash bucket lock, someone else is free to come in and 14664 * re-activate the rnode and add new open streams. The 14665 * strategy is take the rnode hash bucket lock, verify that 14666 * the count is still 1, grab the open stream off the 14667 * head of the list and mark it invalid, then release the 14668 * rnode hash bucket lock and proceed with that open stream. 14669 * This is ok because nfs4close_one() will acquire the proper 14670 * open/create to close/destroy synchronization for open 14671 * streams, and will ensure that if someone has reopened 14672 * the open stream after we've dropped the hash bucket lock 14673 * then we'll just simply return without destroying the 14674 * open stream. 14675 * Repeat until the list is empty. 14676 */ 14677 14678 for (;;) { 14679 14680 /* make sure vnode hasn't been reactivated */ 14681 rw_enter(&rp->r_hashq->r_lock, RW_READER); 14682 mutex_enter(&vp->v_lock); 14683 if (vp->v_count > 1) { 14684 mutex_exit(&vp->v_lock); 14685 rw_exit(&rp->r_hashq->r_lock); 14686 break; 14687 } 14688 /* 14689 * Grabbing r_os_lock before releasing v_lock prevents 14690 * a window where the rnode/open stream could get 14691 * reactivated (and os_force_close set to 0) before we 14692 * had a chance to set os_force_close to 1. 14693 */ 14694 mutex_enter(&rp->r_os_lock); 14695 mutex_exit(&vp->v_lock); 14696 14697 osp = list_head(&rp->r_open_streams); 14698 if (!osp) { 14699 /* nothing left to CLOSE OTW, so return */ 14700 mutex_exit(&rp->r_os_lock); 14701 rw_exit(&rp->r_hashq->r_lock); 14702 break; 14703 } 14704 14705 mutex_enter(&rp->r_statev4_lock); 14706 /* the file can't still be mem mapped */ 14707 ASSERT(rp->r_mapcnt == 0); 14708 if (rp->created_v4) 14709 rp->created_v4 = 0; 14710 mutex_exit(&rp->r_statev4_lock); 14711 14712 /* 14713 * Grab a ref on this open stream; nfs4close_one 14714 * will mark it as invalid 14715 */ 14716 mutex_enter(&osp->os_sync_lock); 14717 osp->os_ref_count++; 14718 osp->os_force_close = 1; 14719 mutex_exit(&osp->os_sync_lock); 14720 mutex_exit(&rp->r_os_lock); 14721 rw_exit(&rp->r_hashq->r_lock); 14722 14723 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 14724 14725 /* Update error if it isn't already non-zero */ 14726 if (error == 0) { 14727 if (e.error) 14728 error = e.error; 14729 else if (e.stat) 14730 error = geterrno4(e.stat); 14731 } 14732 14733 #ifdef DEBUG 14734 nfs4close_all_cnt++; 14735 #endif 14736 /* Release the ref on osp acquired above. */ 14737 open_stream_rele(osp, rp); 14738 14739 /* Proceed to the next open stream, if any */ 14740 } 14741 return (error); 14742 } 14743 14744 /* 14745 * nfs4close_one - close one open stream for a file if needed. 14746 * 14747 * "close_type" indicates which close path this is: 14748 * CLOSE_NORM: close initiated via VOP_CLOSE. 14749 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 14750 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 14751 * the close and release of client state for this open stream 14752 * (unless someone else has the open stream open). 14753 * CLOSE_RESEND: indicates the request is a replay of an earlier request 14754 * (e.g., due to abort because of a signal). 14755 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 14756 * 14757 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 14758 * recovery. Instead, the caller is expected to deal with retries. 14759 * 14760 * The caller can either pass in the osp ('provided_osp') or not. 14761 * 14762 * 'access_bits' represents the access we are closing/downgrading. 14763 * 14764 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 14765 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 14766 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 14767 * 14768 * Errors are returned via the nfs4_error_t. 14769 */ 14770 void 14771 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 14772 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 14773 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 14774 uint_t mmap_flags) 14775 { 14776 nfs4_open_owner_t *oop; 14777 nfs4_open_stream_t *osp = NULL; 14778 int retry = 0; 14779 int num_retries = NFS4_NUM_RECOV_RETRIES; 14780 rnode4_t *rp; 14781 mntinfo4_t *mi; 14782 nfs4_recov_state_t recov_state; 14783 cred_t *cred_otw = NULL; 14784 bool_t recovonly = FALSE; 14785 int isrecov; 14786 int force_close; 14787 int close_failed = 0; 14788 int did_dec_count = 0; 14789 int did_start_op = 0; 14790 int did_force_recovlock = 0; 14791 int did_start_seqid_sync = 0; 14792 int have_sync_lock = 0; 14793 14794 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14795 14796 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 14797 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 14798 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 14799 len, maxprot, mmap_flags, access_bits)); 14800 14801 nfs4_error_zinit(ep); 14802 rp = VTOR4(vp); 14803 mi = VTOMI4(vp); 14804 isrecov = (close_type == CLOSE_RESEND || 14805 close_type == CLOSE_AFTER_RESEND); 14806 14807 /* 14808 * First get the open owner. 14809 */ 14810 if (!provided_osp) { 14811 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 14812 } else { 14813 oop = provided_osp->os_open_owner; 14814 ASSERT(oop != NULL); 14815 open_owner_hold(oop); 14816 } 14817 14818 if (!oop) { 14819 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 14820 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 14821 "close type %d", (void *)rp, (void *)mi, (void *)cr, 14822 (void *)provided_osp, close_type)); 14823 ep->error = EIO; 14824 goto out; 14825 } 14826 14827 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 14828 recov_retry: 14829 osp = NULL; 14830 close_failed = 0; 14831 force_close = (close_type == CLOSE_FORCE); 14832 retry = 0; 14833 did_start_op = 0; 14834 did_force_recovlock = 0; 14835 did_start_seqid_sync = 0; 14836 have_sync_lock = 0; 14837 recovonly = FALSE; 14838 recov_state.rs_flags = 0; 14839 recov_state.rs_num_retry_despite_err = 0; 14840 14841 /* 14842 * Second synchronize with recovery. 14843 */ 14844 if (!isrecov) { 14845 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 14846 &recov_state, &recovonly); 14847 if (!ep->error) { 14848 did_start_op = 1; 14849 } else { 14850 close_failed = 1; 14851 /* 14852 * If we couldn't get start_fop, but have to 14853 * cleanup state, then at least acquire the 14854 * mi_recovlock so we can synchronize with 14855 * recovery. 14856 */ 14857 if (close_type == CLOSE_FORCE) { 14858 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 14859 RW_READER, FALSE); 14860 did_force_recovlock = 1; 14861 } else 14862 goto out; 14863 } 14864 } 14865 14866 /* 14867 * We cannot attempt to get the open seqid sync if nfs4_start_fop 14868 * set 'recovonly' to TRUE since most likely this is due to 14869 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 14870 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 14871 * to retry, causing us to loop until recovery finishes. Plus we 14872 * don't need protection over the open seqid since we're not going 14873 * OTW, hence don't need to use the seqid. 14874 */ 14875 if (recovonly == FALSE) { 14876 /* need to grab the open owner sync before 'os_sync_lock' */ 14877 ep->error = nfs4_start_open_seqid_sync(oop, mi); 14878 if (ep->error == EAGAIN) { 14879 ASSERT(!isrecov); 14880 if (did_start_op) 14881 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 14882 &recov_state, TRUE); 14883 if (did_force_recovlock) 14884 nfs_rw_exit(&mi->mi_recovlock); 14885 goto recov_retry; 14886 } 14887 did_start_seqid_sync = 1; 14888 } 14889 14890 /* 14891 * Third get an open stream and acquire 'os_sync_lock' to 14892 * sychronize the opening/creating of an open stream with the 14893 * closing/destroying of an open stream. 14894 */ 14895 if (!provided_osp) { 14896 /* returns with 'os_sync_lock' held */ 14897 osp = find_open_stream(oop, rp); 14898 if (!osp) { 14899 ep->error = EIO; 14900 goto out; 14901 } 14902 } else { 14903 osp = provided_osp; 14904 open_stream_hold(osp); 14905 mutex_enter(&osp->os_sync_lock); 14906 } 14907 have_sync_lock = 1; 14908 14909 ASSERT(oop == osp->os_open_owner); 14910 14911 /* 14912 * Fourth, do any special pre-OTW CLOSE processing 14913 * based on the specific close type. 14914 */ 14915 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 14916 !did_dec_count) { 14917 ASSERT(osp->os_open_ref_count > 0); 14918 osp->os_open_ref_count--; 14919 did_dec_count = 1; 14920 if (osp->os_open_ref_count == 0) 14921 osp->os_final_close = 1; 14922 } 14923 14924 if (close_type == CLOSE_FORCE) { 14925 /* see if somebody reopened the open stream. */ 14926 if (!osp->os_force_close) { 14927 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14928 "nfs4close_one: skip CLOSE_FORCE as osp %p " 14929 "was reopened, vp %p", (void *)osp, (void *)vp)); 14930 ep->error = 0; 14931 ep->stat = NFS4_OK; 14932 goto out; 14933 } 14934 14935 if (!osp->os_final_close && !did_dec_count) { 14936 osp->os_open_ref_count--; 14937 did_dec_count = 1; 14938 } 14939 14940 /* 14941 * We can't depend on os_open_ref_count being 0 due to the 14942 * way executables are opened (VN_RELE to match a VOP_OPEN). 14943 */ 14944 #ifdef NOTYET 14945 ASSERT(osp->os_open_ref_count == 0); 14946 #endif 14947 if (osp->os_open_ref_count != 0) { 14948 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14949 "nfs4close_one: should panic here on an " 14950 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 14951 "since this is probably the exec problem.")); 14952 14953 osp->os_open_ref_count = 0; 14954 } 14955 14956 /* 14957 * There is the possibility that nfs4close_one() 14958 * for close_type == CLOSE_DELMAP couldn't find the 14959 * open stream, thus couldn't decrement its os_mapcnt; 14960 * therefore we can't use this ASSERT yet. 14961 */ 14962 #ifdef NOTYET 14963 ASSERT(osp->os_mapcnt == 0); 14964 #endif 14965 osp->os_mapcnt = 0; 14966 } 14967 14968 if (close_type == CLOSE_DELMAP && !did_dec_count) { 14969 ASSERT(osp->os_mapcnt >= btopr(len)); 14970 14971 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 14972 osp->os_mmap_write -= btopr(len); 14973 if (maxprot & PROT_READ) 14974 osp->os_mmap_read -= btopr(len); 14975 if (maxprot & PROT_EXEC) 14976 osp->os_mmap_read -= btopr(len); 14977 /* mirror the PROT_NONE check in nfs4_addmap() */ 14978 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 14979 !(maxprot & PROT_EXEC)) 14980 osp->os_mmap_read -= btopr(len); 14981 osp->os_mapcnt -= btopr(len); 14982 did_dec_count = 1; 14983 } 14984 14985 if (recovonly) { 14986 nfs4_lost_rqst_t lost_rqst; 14987 14988 /* request should not already be in recovery queue */ 14989 ASSERT(lrp == NULL); 14990 nfs4_error_init(ep, EINTR); 14991 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 14992 osp, cred_otw, vp); 14993 mutex_exit(&osp->os_sync_lock); 14994 have_sync_lock = 0; 14995 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 14996 lost_rqst.lr_op == OP_CLOSE ? 14997 &lost_rqst : NULL, OP_CLOSE, NULL); 14998 close_failed = 1; 14999 force_close = 0; 15000 goto close_cleanup; 15001 } 15002 15003 /* 15004 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15005 * we stopped operating on the open owner's <old oo_name, old seqid> 15006 * space, which means we stopped operating on the open stream 15007 * too. So don't go OTW (as the seqid is likely bad, and the 15008 * stateid could be stale, potentially triggering a false 15009 * setclientid), and just clean up the client's internal state. 15010 */ 15011 if (osp->os_orig_oo_name != oop->oo_name) { 15012 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15013 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15014 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15015 "oo_name %" PRIx64")", 15016 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15017 oop->oo_name)); 15018 close_failed = 1; 15019 } 15020 15021 /* If the file failed recovery, just quit. */ 15022 mutex_enter(&rp->r_statelock); 15023 if (rp->r_flags & R4RECOVERR) { 15024 close_failed = 1; 15025 } 15026 mutex_exit(&rp->r_statelock); 15027 15028 /* 15029 * If the force close path failed to obtain start_fop 15030 * then skip the OTW close and just remove the state. 15031 */ 15032 if (close_failed) 15033 goto close_cleanup; 15034 15035 /* 15036 * Fifth, check to see if there are still mapped pages or other 15037 * opens using this open stream. If there are then we can't 15038 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15039 */ 15040 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15041 nfs4_lost_rqst_t new_lost_rqst; 15042 bool_t needrecov = FALSE; 15043 cred_t *odg_cred_otw = NULL; 15044 seqid4 open_dg_seqid = 0; 15045 15046 if (osp->os_delegation) { 15047 /* 15048 * If this open stream was never OPENed OTW then we 15049 * surely can't DOWNGRADE it (especially since the 15050 * osp->open_stateid is really a delegation stateid 15051 * when os_delegation is 1). 15052 */ 15053 if (access_bits & FREAD) 15054 osp->os_share_acc_read--; 15055 if (access_bits & FWRITE) 15056 osp->os_share_acc_write--; 15057 osp->os_share_deny_none--; 15058 nfs4_error_zinit(ep); 15059 goto out; 15060 } 15061 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15062 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15063 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15064 if (needrecov && !isrecov) { 15065 bool_t abort; 15066 nfs4_bseqid_entry_t *bsep = NULL; 15067 15068 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15069 bsep = nfs4_create_bseqid_entry(oop, NULL, 15070 vp, 0, 15071 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15072 open_dg_seqid); 15073 15074 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15075 oop, osp, odg_cred_otw, vp, access_bits, 0); 15076 mutex_exit(&osp->os_sync_lock); 15077 have_sync_lock = 0; 15078 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15079 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15080 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15081 bsep); 15082 if (odg_cred_otw) 15083 crfree(odg_cred_otw); 15084 if (bsep) 15085 kmem_free(bsep, sizeof (*bsep)); 15086 15087 if (abort == TRUE) 15088 goto out; 15089 15090 if (did_start_seqid_sync) { 15091 nfs4_end_open_seqid_sync(oop); 15092 did_start_seqid_sync = 0; 15093 } 15094 open_stream_rele(osp, rp); 15095 15096 if (did_start_op) 15097 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15098 &recov_state, FALSE); 15099 if (did_force_recovlock) 15100 nfs_rw_exit(&mi->mi_recovlock); 15101 15102 goto recov_retry; 15103 } else { 15104 if (odg_cred_otw) 15105 crfree(odg_cred_otw); 15106 } 15107 goto out; 15108 } 15109 15110 /* 15111 * If this open stream was created as the results of an open 15112 * while holding a delegation, then just release it; no need 15113 * to do an OTW close. Otherwise do a "normal" OTW close. 15114 */ 15115 if (osp->os_delegation) { 15116 nfs4close_notw(vp, osp, &have_sync_lock); 15117 nfs4_error_zinit(ep); 15118 goto out; 15119 } 15120 15121 /* 15122 * If this stream is not valid, we're done. 15123 */ 15124 if (!osp->os_valid) { 15125 nfs4_error_zinit(ep); 15126 goto out; 15127 } 15128 15129 /* 15130 * Last open or mmap ref has vanished, need to do an OTW close. 15131 * First check to see if a close is still necessary. 15132 */ 15133 if (osp->os_failed_reopen) { 15134 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15135 "don't close OTW osp %p since reopen failed.", 15136 (void *)osp)); 15137 /* 15138 * Reopen of the open stream failed, hence the 15139 * stateid of the open stream is invalid/stale, and 15140 * sending this OTW would incorrectly cause another 15141 * round of recovery. In this case, we need to set 15142 * the 'os_valid' bit to 0 so another thread doesn't 15143 * come in and re-open this open stream before 15144 * this "closing" thread cleans up state (decrementing 15145 * the nfs4_server_t's state_ref_count and decrementing 15146 * the os_ref_count). 15147 */ 15148 osp->os_valid = 0; 15149 /* 15150 * This removes the reference obtained at OPEN; ie, 15151 * when the open stream structure was created. 15152 * 15153 * We don't have to worry about calling 'open_stream_rele' 15154 * since we our currently holding a reference to this 15155 * open stream which means the count can not go to 0 with 15156 * this decrement. 15157 */ 15158 ASSERT(osp->os_ref_count >= 2); 15159 osp->os_ref_count--; 15160 nfs4_error_zinit(ep); 15161 close_failed = 0; 15162 goto close_cleanup; 15163 } 15164 15165 ASSERT(osp->os_ref_count > 1); 15166 15167 if (!(vp->v_vfsp->vfs_flag & VFS_RDONLY) && 15168 nfs4_dross_pages(vp)) { 15169 nfs4_invalidate_pages(vp, 0, cred_otw); 15170 } 15171 15172 /* 15173 * Sixth, try the CLOSE OTW. 15174 */ 15175 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15176 close_type, ep, &have_sync_lock); 15177 15178 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15179 /* 15180 * Let the recovery thread be responsible for 15181 * removing the state for CLOSE. 15182 */ 15183 close_failed = 1; 15184 force_close = 0; 15185 retry = 0; 15186 } 15187 15188 /* See if we need to retry with a different cred */ 15189 if ((ep->error == EACCES || 15190 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15191 cred_otw != cr) { 15192 crfree(cred_otw); 15193 cred_otw = cr; 15194 crhold(cred_otw); 15195 retry = 1; 15196 } 15197 15198 if (ep->error || ep->stat) 15199 close_failed = 1; 15200 15201 if (retry && !isrecov && num_retries-- > 0) { 15202 if (have_sync_lock) { 15203 mutex_exit(&osp->os_sync_lock); 15204 have_sync_lock = 0; 15205 } 15206 if (did_start_seqid_sync) { 15207 nfs4_end_open_seqid_sync(oop); 15208 did_start_seqid_sync = 0; 15209 } 15210 open_stream_rele(osp, rp); 15211 15212 if (did_start_op) 15213 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15214 &recov_state, FALSE); 15215 if (did_force_recovlock) 15216 nfs_rw_exit(&mi->mi_recovlock); 15217 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15218 "nfs4close_one: need to retry the close " 15219 "operation")); 15220 goto recov_retry; 15221 } 15222 close_cleanup: 15223 /* 15224 * Seventh and lastly, process our results. 15225 */ 15226 if (close_failed && force_close) { 15227 /* 15228 * It's ok to drop and regrab the 'os_sync_lock' since 15229 * nfs4close_notw() will recheck to make sure the 15230 * "close"/removal of state should happen. 15231 */ 15232 if (!have_sync_lock) { 15233 mutex_enter(&osp->os_sync_lock); 15234 have_sync_lock = 1; 15235 } 15236 /* 15237 * This is last call, remove the ref on the open 15238 * stream created by open and clean everything up. 15239 */ 15240 osp->os_pending_close = 0; 15241 nfs4close_notw(vp, osp, &have_sync_lock); 15242 nfs4_error_zinit(ep); 15243 } 15244 15245 if (!close_failed) { 15246 if (have_sync_lock) { 15247 osp->os_pending_close = 0; 15248 mutex_exit(&osp->os_sync_lock); 15249 have_sync_lock = 0; 15250 } else { 15251 mutex_enter(&osp->os_sync_lock); 15252 osp->os_pending_close = 0; 15253 mutex_exit(&osp->os_sync_lock); 15254 } 15255 if (did_start_op && recov_state.rs_sp != NULL) { 15256 mutex_enter(&recov_state.rs_sp->s_lock); 15257 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15258 mutex_exit(&recov_state.rs_sp->s_lock); 15259 } else { 15260 nfs4_dec_state_ref_count(mi); 15261 } 15262 nfs4_error_zinit(ep); 15263 } 15264 15265 out: 15266 if (have_sync_lock) 15267 mutex_exit(&osp->os_sync_lock); 15268 if (did_start_op) 15269 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15270 recovonly ? TRUE : FALSE); 15271 if (did_force_recovlock) 15272 nfs_rw_exit(&mi->mi_recovlock); 15273 if (cred_otw) 15274 crfree(cred_otw); 15275 if (osp) 15276 open_stream_rele(osp, rp); 15277 if (oop) { 15278 if (did_start_seqid_sync) 15279 nfs4_end_open_seqid_sync(oop); 15280 open_owner_rele(oop); 15281 } 15282 } 15283 15284 /* 15285 * Convert information returned by the server in the LOCK4denied 15286 * structure to the form required by fcntl. 15287 */ 15288 static void 15289 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15290 { 15291 nfs4_lo_name_t *lo; 15292 15293 #ifdef DEBUG 15294 if (denied_to_flk_debug) { 15295 lockt_denied_debug = lockt_denied; 15296 debug_enter("lockt_denied"); 15297 } 15298 #endif 15299 15300 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15301 flk->l_whence = 0; /* aka SEEK_SET */ 15302 flk->l_start = lockt_denied->offset; 15303 flk->l_len = lockt_denied->length; 15304 15305 /* 15306 * If the blocking clientid matches our client id, then we can 15307 * interpret the lockowner (since we built it). If not, then 15308 * fabricate a sysid and pid. Note that the l_sysid field 15309 * in *flk already has the local sysid. 15310 */ 15311 15312 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15313 15314 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15315 lo = (nfs4_lo_name_t *) 15316 lockt_denied->owner.owner_val; 15317 15318 flk->l_pid = lo->ln_pid; 15319 } else { 15320 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15321 "denied_to_flk: bad lock owner length\n")); 15322 15323 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15324 } 15325 } else { 15326 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15327 "denied_to_flk: foreign clientid\n")); 15328 15329 /* 15330 * Construct a new sysid which should be different from 15331 * sysids of other systems. 15332 */ 15333 15334 flk->l_sysid++; 15335 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15336 } 15337 } 15338 15339 static pid_t 15340 lo_to_pid(lock_owner4 *lop) 15341 { 15342 pid_t pid = 0; 15343 uchar_t *cp; 15344 int i; 15345 15346 cp = (uchar_t *)&lop->clientid; 15347 15348 for (i = 0; i < sizeof (lop->clientid); i++) 15349 pid += (pid_t)*cp++; 15350 15351 cp = (uchar_t *)lop->owner_val; 15352 15353 for (i = 0; i < lop->owner_len; i++) 15354 pid += (pid_t)*cp++; 15355 15356 return (pid); 15357 } 15358 15359 /* 15360 * Given a lock pointer, returns the length of that lock. 15361 * "end" is the last locked offset the "l_len" covers from 15362 * the start of the lock. 15363 */ 15364 static off64_t 15365 lock_to_end(flock64_t *lock) 15366 { 15367 off64_t lock_end; 15368 15369 if (lock->l_len == 0) 15370 lock_end = (off64_t)MAXEND; 15371 else 15372 lock_end = lock->l_start + lock->l_len - 1; 15373 15374 return (lock_end); 15375 } 15376 15377 /* 15378 * Given the end of a lock, it will return you the length "l_len" for that lock. 15379 */ 15380 static off64_t 15381 end_to_len(off64_t start, off64_t end) 15382 { 15383 off64_t lock_len; 15384 15385 ASSERT(end >= start); 15386 if (end == MAXEND) 15387 lock_len = 0; 15388 else 15389 lock_len = end - start + 1; 15390 15391 return (lock_len); 15392 } 15393 15394 /* 15395 * On given end for a lock it determines if it is the last locked offset 15396 * or not, if so keeps it as is, else adds one to return the length for 15397 * valid start. 15398 */ 15399 static off64_t 15400 start_check(off64_t x) 15401 { 15402 if (x == MAXEND) 15403 return (x); 15404 else 15405 return (x + 1); 15406 } 15407 15408 /* 15409 * See if these two locks overlap, and if so return 1; 15410 * otherwise, return 0. 15411 */ 15412 static int 15413 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15414 { 15415 off64_t llfp_end, curfp_end; 15416 15417 llfp_end = lock_to_end(llfp); 15418 curfp_end = lock_to_end(curfp); 15419 15420 if (((llfp_end >= curfp->l_start) && 15421 (llfp->l_start <= curfp->l_start)) || 15422 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15423 return (1); 15424 return (0); 15425 } 15426 15427 /* 15428 * Determine what the interseting lock region is, and add that to the 15429 * 'nl_llpp' locklist in increasing order (by l_start). 15430 */ 15431 static void 15432 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15433 locklist_t **nl_llpp, vnode_t *vp) 15434 { 15435 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15436 off64_t lost_flp_end, local_flp_end, len, start; 15437 15438 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15439 15440 if (!locks_intersect(lost_flp, local_flp)) 15441 return; 15442 15443 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15444 "locks intersect")); 15445 15446 lost_flp_end = lock_to_end(lost_flp); 15447 local_flp_end = lock_to_end(local_flp); 15448 15449 /* Find the starting point of the intersecting region */ 15450 if (local_flp->l_start > lost_flp->l_start) 15451 start = local_flp->l_start; 15452 else 15453 start = lost_flp->l_start; 15454 15455 /* Find the lenght of the intersecting region */ 15456 if (lost_flp_end < local_flp_end) 15457 len = end_to_len(start, lost_flp_end); 15458 else 15459 len = end_to_len(start, local_flp_end); 15460 15461 /* 15462 * Prepare the flock structure for the intersection found and insert 15463 * it into the new list in increasing l_start order. This list contains 15464 * intersections of locks registered by the client with the local host 15465 * and the lost lock. 15466 * The lock type of this lock is the same as that of the local_flp. 15467 */ 15468 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15469 intersect_llp->ll_flock.l_start = start; 15470 intersect_llp->ll_flock.l_len = len; 15471 intersect_llp->ll_flock.l_type = local_flp->l_type; 15472 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15473 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15474 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15475 intersect_llp->ll_vp = vp; 15476 15477 tmp_fllp = *nl_llpp; 15478 cur_fllp = NULL; 15479 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15480 intersect_llp->ll_flock.l_start) { 15481 cur_fllp = tmp_fllp; 15482 tmp_fllp = tmp_fllp->ll_next; 15483 } 15484 if (cur_fllp == NULL) { 15485 /* first on the list */ 15486 intersect_llp->ll_next = *nl_llpp; 15487 *nl_llpp = intersect_llp; 15488 } else { 15489 intersect_llp->ll_next = cur_fllp->ll_next; 15490 cur_fllp->ll_next = intersect_llp; 15491 } 15492 15493 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15494 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15495 intersect_llp->ll_flock.l_start, 15496 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15497 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15498 } 15499 15500 /* 15501 * Our local locking current state is potentially different than 15502 * what the NFSv4 server thinks we have due to a lost lock that was 15503 * resent and then received. We need to reset our "NFSv4" locking 15504 * state to match the current local locking state for this pid since 15505 * that is what the user/application sees as what the world is. 15506 * 15507 * We cannot afford to drop the open/lock seqid sync since then we can 15508 * get confused about what the current local locking state "is" versus 15509 * "was". 15510 * 15511 * If we are unable to fix up the locks, we send SIGLOST to the affected 15512 * process. This is not done if the filesystem has been forcibly 15513 * unmounted, in case the process has already exited and a new process 15514 * exists with the same pid. 15515 */ 15516 static void 15517 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15518 nfs4_lock_owner_t *lop) 15519 { 15520 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15521 mntinfo4_t *mi = VTOMI4(vp); 15522 const int cmd = F_SETLK; 15523 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15524 flock64_t ul_fl; 15525 15526 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15527 "nfs4_reinstitute_local_lock_state")); 15528 15529 /* 15530 * Find active locks for this vp from the local locking code. 15531 * Scan through this list and find out the locks that intersect with 15532 * the lost lock. Once we find the lock that intersects, add the 15533 * intersection area as a new lock to a new list "ri_llp". The lock 15534 * type of the intersection region lock added to ri_llp is the same 15535 * as that found in the active lock list, "list". The intersecting 15536 * region locks are added to ri_llp in increasing l_start order. 15537 */ 15538 ASSERT(curproc->p_zone == mi->mi_zone); 15539 15540 locks = flk_active_locks_for_vp(vp); 15541 ri_llp = NULL; 15542 15543 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15544 ASSERT(llp->ll_vp == vp); 15545 /* 15546 * Pick locks that belong to this pid/lockowner 15547 */ 15548 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15549 continue; 15550 15551 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15552 } 15553 15554 /* 15555 * Now we have the list of intersections with the lost lock. These are 15556 * the locks that were/are active before the server replied to the 15557 * last/lost lock. Issue these locks to the server here. Playing these 15558 * locks to the server will re-establish aur current local locking state 15559 * with the v4 server. 15560 * If we get an error, send SIGLOST to the application for that lock. 15561 */ 15562 15563 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15564 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15565 "nfs4_reinstitute_local_lock_state: need to issue " 15566 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15567 llp->ll_flock.l_start, 15568 llp->ll_flock.l_start + llp->ll_flock.l_len, 15569 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15570 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15571 /* 15572 * No need to relock what we already have 15573 */ 15574 if (llp->ll_flock.l_type == lost_flp->l_type) 15575 continue; 15576 15577 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15578 } 15579 15580 /* 15581 * Now keeping the start of the lost lock as our reference parse the 15582 * newly created ri_llp locklist to find the ranges that we have locked 15583 * with the v4 server but not in the current local locking. We need 15584 * to unlock these ranges. 15585 * These ranges can also be reffered to as those ranges, where the lost 15586 * lock does not overlap with the locks in the ri_llp but are locked 15587 * since the server replied to the lost lock. 15588 */ 15589 cur_start = lost_flp->l_start; 15590 lost_flp_end = lock_to_end(lost_flp); 15591 15592 ul_fl.l_type = F_UNLCK; 15593 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15594 ul_fl.l_sysid = lost_flp->l_sysid; 15595 ul_fl.l_pid = lost_flp->l_pid; 15596 15597 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15598 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15599 15600 if (llp->ll_flock.l_start <= cur_start) { 15601 cur_start = start_check(llp_ll_flock_end); 15602 continue; 15603 } 15604 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15605 "nfs4_reinstitute_local_lock_state: " 15606 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15607 cur_start, llp->ll_flock.l_start)); 15608 15609 ul_fl.l_start = cur_start; 15610 ul_fl.l_len = end_to_len(cur_start, 15611 (llp->ll_flock.l_start - 1)); 15612 15613 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15614 cur_start = start_check(llp_ll_flock_end); 15615 } 15616 15617 /* 15618 * In the case where the lost lock ends after all intersecting locks, 15619 * unlock the last part of the lost lock range. 15620 */ 15621 if (cur_start != start_check(lost_flp_end)) { 15622 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15623 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15624 "lost lock region [%"PRIx64" - %"PRIx64"]", 15625 cur_start, lost_flp->l_start + lost_flp->l_len)); 15626 15627 ul_fl.l_start = cur_start; 15628 /* 15629 * Is it an to-EOF lock? if so unlock till the end 15630 */ 15631 if (lost_flp->l_len == 0) 15632 ul_fl.l_len = 0; 15633 else 15634 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15635 15636 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15637 } 15638 15639 if (locks != NULL) 15640 flk_free_locklist(locks); 15641 15642 /* Free up our newly created locklist */ 15643 for (llp = ri_llp; llp != NULL; ) { 15644 tmp_llp = llp->ll_next; 15645 kmem_free(llp, sizeof (locklist_t)); 15646 llp = tmp_llp; 15647 } 15648 15649 /* 15650 * Now return back to the original calling nfs4frlock() 15651 * and let us naturally drop our seqid syncs. 15652 */ 15653 } 15654 15655 /* 15656 * Create a lost state record for the given lock reinstantiation request 15657 * and push it onto the lost state queue. 15658 */ 15659 static void 15660 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15661 nfs4_lock_owner_t *lop) 15662 { 15663 nfs4_lost_rqst_t req; 15664 nfs_lock_type4 locktype; 15665 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15666 15667 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 15668 15669 locktype = flk_to_locktype(cmd, flk->l_type); 15670 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15671 NULL, NULL, lop, flk, &req, cr, vp); 15672 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15673 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15674 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15675 NULL); 15676 } 15677