1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/cred.h> 37 #include <sys/time.h> 38 #include <sys/vnode.h> 39 #include <sys/vfs.h> 40 #include <sys/file.h> 41 #include <sys/filio.h> 42 #include <sys/uio.h> 43 #include <sys/buf.h> 44 #include <sys/mman.h> 45 #include <sys/pathname.h> 46 #include <sys/dirent.h> 47 #include <sys/debug.h> 48 #include <sys/vmsystm.h> 49 #include <sys/fcntl.h> 50 #include <sys/flock.h> 51 #include <sys/swap.h> 52 #include <sys/errno.h> 53 #include <sys/strsubr.h> 54 #include <sys/sysmacros.h> 55 #include <sys/kmem.h> 56 #include <sys/cmn_err.h> 57 #include <sys/pathconf.h> 58 #include <sys/utsname.h> 59 #include <sys/dnlc.h> 60 #include <sys/acl.h> 61 #include <sys/systeminfo.h> 62 #include <sys/policy.h> 63 #include <sys/sdt.h> 64 #include <sys/list.h> 65 #include <sys/stat.h> 66 67 #include <rpc/types.h> 68 #include <rpc/auth.h> 69 #include <rpc/clnt.h> 70 71 #include <nfs/nfs.h> 72 #include <nfs/nfs_clnt.h> 73 #include <nfs/nfs_acl.h> 74 #include <nfs/lm.h> 75 #include <nfs/nfs4.h> 76 #include <nfs/nfs4_kprot.h> 77 #include <nfs/rnode4.h> 78 #include <nfs/nfs4_clnt.h> 79 80 #include <vm/hat.h> 81 #include <vm/as.h> 82 #include <vm/page.h> 83 #include <vm/pvn.h> 84 #include <vm/seg.h> 85 #include <vm/seg_map.h> 86 #include <vm/seg_kpm.h> 87 #include <vm/seg_vn.h> 88 89 #include <fs/fs_subr.h> 90 91 #include <sys/ddi.h> 92 #include <sys/int_fmtio.h> 93 94 typedef struct { 95 nfs4_ga_res_t *di_garp; 96 cred_t *di_cred; 97 hrtime_t di_time_call; 98 } dirattr_info_t; 99 100 typedef enum nfs4_acl_op { 101 NFS4_ACL_GET, 102 NFS4_ACL_SET 103 } nfs4_acl_op_t; 104 105 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 106 char *, dirattr_info_t *); 107 108 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 109 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 110 nfs4_error_t *, int *); 111 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 112 cred_t *); 113 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 114 stable_how4 *); 115 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 116 cred_t *, bool_t, struct uio *); 117 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 118 vsecattr_t *); 119 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 120 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 121 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 122 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 123 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 124 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 125 int, vnode_t **, cred_t *); 126 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 127 cred_t *, int, int, enum createmode4, int); 128 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 129 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 130 vnode_t *, char *, cred_t *, nfsstat4 *); 131 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 132 vnode_t *, char *, cred_t *, nfsstat4 *); 133 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 134 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 135 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 136 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 137 page_t *[], size_t, struct seg *, caddr_t, 138 enum seg_rw, cred_t *); 139 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 140 cred_t *); 141 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 142 int, cred_t *); 143 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 144 int, cred_t *); 145 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 146 static void nfs4_set_mod(vnode_t *); 147 static void nfs4_get_commit(vnode_t *); 148 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 149 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 150 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 151 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 152 cred_t *); 153 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 154 cred_t *); 155 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 156 hrtime_t, vnode_t *, cred_t *); 157 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 158 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 159 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 160 u_offset_t); 161 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 162 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 163 static cred_t *state_to_cred(nfs4_open_stream_t *); 164 static int vtoname(vnode_t *, char *, ssize_t); 165 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 166 static pid_t lo_to_pid(lock_owner4 *); 167 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 168 cred_t *, nfs4_lock_owner_t *); 169 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 170 nfs4_lock_owner_t *); 171 static nfs4_open_stream_t *open_and_get_osp(vnode_t *, cred_t *, mntinfo4_t *); 172 static void nfs4_delmap_callback(struct as *, void *, uint_t); 173 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 174 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 175 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 176 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 177 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 178 uid_t, gid_t, int); 179 180 /* 181 * Routines that implement the setting of v4 args for the misc. ops 182 */ 183 static void nfs4args_lock_free(nfs_argop4 *); 184 static void nfs4args_lockt_free(nfs_argop4 *); 185 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 186 int, rnode4_t *, cred_t *, bitmap4, int *, 187 nfs4_stateid_types_t *); 188 static void nfs4args_setattr_free(nfs_argop4 *); 189 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 190 bitmap4); 191 static void nfs4args_verify_free(nfs_argop4 *); 192 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 193 WRITE4args **, nfs4_stateid_types_t *); 194 195 /* 196 * These are the vnode ops functions that implement the vnode interface to 197 * the networked file system. See more comments below at nfs4_vnodeops. 198 */ 199 static int nfs4_open(vnode_t **, int, cred_t *); 200 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *); 201 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 202 caller_context_t *); 203 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 204 caller_context_t *); 205 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 206 static int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *); 207 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 208 caller_context_t *); 209 static int nfs4_access(vnode_t *, int, int, cred_t *); 210 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *); 211 static int nfs4_fsync(vnode_t *, int, cred_t *); 212 static void nfs4_inactive(vnode_t *, cred_t *); 213 static int nfs4_lookup(vnode_t *, char *, vnode_t **, 214 struct pathname *, int, vnode_t *, cred_t *); 215 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 216 int, vnode_t **, cred_t *, int); 217 static int nfs4_remove(vnode_t *, char *, cred_t *); 218 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *); 219 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 220 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, 221 vnode_t **, cred_t *); 222 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *); 223 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 224 cred_t *); 225 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *); 226 static int nfs4_fid(vnode_t *, fid_t *); 227 static int nfs4_rwlock(vnode_t *, int, caller_context_t *); 228 static void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 229 static int nfs4_seek(vnode_t *, offset_t, offset_t *); 230 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 231 page_t *[], size_t, struct seg *, caddr_t, 232 enum seg_rw, cred_t *); 233 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *); 234 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, 235 size_t, uchar_t, uchar_t, uint_t, cred_t *); 236 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, 237 size_t, uchar_t, uchar_t, uint_t, cred_t *); 238 static int nfs4_cmp(vnode_t *, vnode_t *); 239 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 240 struct flk_callback *, cred_t *); 241 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 242 cred_t *, caller_context_t *); 243 static int nfs4_realvp(vnode_t *, vnode_t **); 244 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, 245 size_t, uint_t, uint_t, uint_t, cred_t *); 246 static int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *); 247 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 248 cred_t *); 249 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *); 250 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 251 static int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 252 static int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *); 253 254 /* 255 * Used for nfs4_commit_vp() to indicate if we should 256 * wait on pending writes. 257 */ 258 #define NFS4_WRITE_NOWAIT 0 259 #define NFS4_WRITE_WAIT 1 260 261 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 262 263 /* 264 * Error flags used to pass information about certain special errors 265 * which need to be handled specially. 266 */ 267 #define NFS_EOF -98 268 #define NFS_VERF_MISMATCH -97 269 270 /* 271 * Flags used to differentiate between which operation drove the 272 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 273 */ 274 #define NFS4_CLOSE_OP 0x1 275 #define NFS4_DELMAP_OP 0x2 276 #define NFS4_INACTIVE_OP 0x3 277 278 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 279 280 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 281 #define ALIGN64(x, ptr, sz) \ 282 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 283 if (x) { \ 284 x = sizeof (uint64_t) - (x); \ 285 sz -= (x); \ 286 ptr += (x); \ 287 } 288 289 #ifdef DEBUG 290 int nfs4_client_attr_debug = 0; 291 int nfs4_client_state_debug = 0; 292 int nfs4_client_shadow_debug = 0; 293 int nfs4_client_lock_debug = 0; 294 int nfs4_seqid_sync = 0; 295 int nfs4_client_map_debug = 0; 296 static int nfs4_pageio_debug = 0; 297 int nfs4_client_inactive_debug = 0; 298 int nfs4_client_recov_debug = 0; 299 int nfs4_client_recov_stub_debug = 0; 300 int nfs4_client_failover_debug = 0; 301 int nfs4_client_call_debug = 0; 302 int nfs4_client_lookup_debug = 0; 303 int nfs4_client_zone_debug = 0; 304 int nfs4_lost_rqst_debug = 0; 305 int nfs4_rdattrerr_debug = 0; 306 int nfs4_open_stream_debug = 0; 307 308 int nfs4read_error_inject; 309 310 static int nfs4_create_misses = 0; 311 312 static int nfs4_readdir_cache_shorts = 0; 313 static int nfs4_readdir_readahead = 0; 314 315 static int nfs4_bio_do_stop = 0; 316 317 static int nfs4_lostpage = 0; /* number of times we lost original page */ 318 319 int nfs4_mmap_debug = 0; 320 321 static int nfs4_pathconf_cache_hits = 0; 322 static int nfs4_pathconf_cache_misses = 0; 323 324 int nfs4close_all_cnt; 325 int nfs4close_one_debug = 0; 326 int nfs4close_notw_debug = 0; 327 328 int denied_to_flk_debug = 0; 329 void *lockt_denied_debug; 330 331 #endif 332 333 /* 334 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 335 * or NFS4ERR_RESOURCE. 336 */ 337 static int confirm_retry_sec = 30; 338 339 static int nfs4_lookup_neg_cache = 1; 340 341 /* 342 * number of pages to read ahead 343 * optimized for 100 base-T. 344 */ 345 static int nfs4_nra = 4; 346 347 static int nfs4_do_symlink_cache = 1; 348 349 static int nfs4_pathconf_disable_cache = 0; 350 351 /* 352 * These are the vnode ops routines which implement the vnode interface to 353 * the networked file system. These routines just take their parameters, 354 * make them look networkish by putting the right info into interface structs, 355 * and then calling the appropriate remote routine(s) to do the work. 356 * 357 * Note on directory name lookup cacheing: If we detect a stale fhandle, 358 * we purge the directory cache relative to that vnode. This way, the 359 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 360 * more details on rnode locking. 361 */ 362 363 struct vnodeops *nfs4_vnodeops; 364 365 const fs_operation_def_t nfs4_vnodeops_template[] = { 366 VOPNAME_OPEN, nfs4_open, 367 VOPNAME_CLOSE, nfs4_close, 368 VOPNAME_READ, nfs4_read, 369 VOPNAME_WRITE, nfs4_write, 370 VOPNAME_IOCTL, nfs4_ioctl, 371 VOPNAME_GETATTR, nfs4_getattr, 372 VOPNAME_SETATTR, nfs4_setattr, 373 VOPNAME_ACCESS, nfs4_access, 374 VOPNAME_LOOKUP, nfs4_lookup, 375 VOPNAME_CREATE, nfs4_create, 376 VOPNAME_REMOVE, nfs4_remove, 377 VOPNAME_LINK, nfs4_link, 378 VOPNAME_RENAME, nfs4_rename, 379 VOPNAME_MKDIR, nfs4_mkdir, 380 VOPNAME_RMDIR, nfs4_rmdir, 381 VOPNAME_READDIR, nfs4_readdir, 382 VOPNAME_SYMLINK, nfs4_symlink, 383 VOPNAME_READLINK, nfs4_readlink, 384 VOPNAME_FSYNC, nfs4_fsync, 385 VOPNAME_INACTIVE, (fs_generic_func_p) nfs4_inactive, 386 VOPNAME_FID, nfs4_fid, 387 VOPNAME_RWLOCK, nfs4_rwlock, 388 VOPNAME_RWUNLOCK, (fs_generic_func_p) nfs4_rwunlock, 389 VOPNAME_SEEK, nfs4_seek, 390 VOPNAME_FRLOCK, nfs4_frlock, 391 VOPNAME_SPACE, nfs4_space, 392 VOPNAME_REALVP, nfs4_realvp, 393 VOPNAME_GETPAGE, nfs4_getpage, 394 VOPNAME_PUTPAGE, nfs4_putpage, 395 VOPNAME_MAP, (fs_generic_func_p) nfs4_map, 396 VOPNAME_ADDMAP, (fs_generic_func_p) nfs4_addmap, 397 VOPNAME_DELMAP, nfs4_delmap, 398 VOPNAME_DUMP, nfs_dump, /* there is no separate nfs4_dump */ 399 VOPNAME_PATHCONF, nfs4_pathconf, 400 VOPNAME_PAGEIO, nfs4_pageio, 401 VOPNAME_DISPOSE, (fs_generic_func_p) nfs4_dispose, 402 VOPNAME_SETSECATTR, nfs4_setsecattr, 403 VOPNAME_GETSECATTR, nfs4_getsecattr, 404 VOPNAME_SHRLOCK, nfs4_shrlock, 405 NULL, NULL 406 }; 407 408 /* 409 * The following are subroutines and definitions to set args or get res 410 * for the different nfsv4 ops 411 */ 412 413 void 414 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 415 { 416 int i; 417 418 for (i = 0; i < arglen; i++) { 419 if (argop[i].argop == OP_LOOKUP) 420 kmem_free( 421 argop[i].nfs_argop4_u.oplookup.objname.utf8string_val, 422 argop[i].nfs_argop4_u.oplookup.objname.utf8string_len); 423 } 424 } 425 426 static void 427 nfs4args_lock_free(nfs_argop4 *argop) 428 { 429 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 430 431 if (locker->new_lock_owner == TRUE) { 432 open_to_lock_owner4 *open_owner; 433 434 open_owner = &locker->locker4_u.open_owner; 435 if (open_owner->lock_owner.owner_val != NULL) { 436 kmem_free(open_owner->lock_owner.owner_val, 437 open_owner->lock_owner.owner_len); 438 } 439 } 440 } 441 442 static void 443 nfs4args_lockt_free(nfs_argop4 *argop) 444 { 445 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 446 447 if (lowner->owner_val != NULL) { 448 kmem_free(lowner->owner_val, lowner->owner_len); 449 } 450 } 451 452 static void 453 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 454 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 455 nfs4_stateid_types_t *sid_types) 456 { 457 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 458 mntinfo4_t *mi; 459 460 argop->argop = OP_SETATTR; 461 /* 462 * The stateid is set to 0 if client is not modifying the size 463 * and otherwise to whatever nfs4_get_stateid() returns. 464 * 465 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 466 * state struct could be found for the process/file pair. We may 467 * want to change this in the future (by OPENing the file). See 468 * bug # 4474852. 469 */ 470 if (vap->va_mask & AT_SIZE) { 471 472 ASSERT(rp != NULL); 473 mi = VTOMI4(RTOV4(rp)); 474 475 argop->nfs_argop4_u.opsetattr.stateid = 476 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 477 OP_SETATTR, sid_types, FALSE); 478 } else { 479 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 480 sizeof (stateid4)); 481 } 482 483 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 484 if (*error) 485 bzero(attr, sizeof (*attr)); 486 } 487 488 static void 489 nfs4args_setattr_free(nfs_argop4 *argop) 490 { 491 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 492 } 493 494 static int 495 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 496 bitmap4 supp) 497 { 498 fattr4 *attr; 499 int error = 0; 500 501 argop->argop = op; 502 switch (op) { 503 case OP_VERIFY: 504 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 505 break; 506 case OP_NVERIFY: 507 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 508 break; 509 default: 510 return (EINVAL); 511 } 512 if (!error) 513 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 514 if (error) 515 bzero(attr, sizeof (*attr)); 516 return (error); 517 } 518 519 static void 520 nfs4args_verify_free(nfs_argop4 *argop) 521 { 522 switch (argop->argop) { 523 case OP_VERIFY: 524 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 525 break; 526 case OP_NVERIFY: 527 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 528 break; 529 default: 530 break; 531 } 532 } 533 534 static void 535 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 536 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 537 { 538 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 539 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 540 541 argop->argop = OP_WRITE; 542 wargs->stable = stable; 543 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 544 mi, OP_WRITE, sid_tp); 545 wargs->mblk = NULL; 546 *wargs_pp = wargs; 547 } 548 549 void 550 nfs4args_copen_free(OPEN4cargs *open_args) 551 { 552 if (open_args->owner.owner_val) { 553 kmem_free(open_args->owner.owner_val, 554 open_args->owner.owner_len); 555 } 556 if ((open_args->opentype == OPEN4_CREATE) && 557 (open_args->mode != EXCLUSIVE4)) { 558 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 559 } 560 } 561 562 /* 563 * XXX: This is referenced in modstubs.s 564 */ 565 struct vnodeops * 566 nfs4_getvnodeops(void) 567 { 568 return (nfs4_vnodeops); 569 } 570 571 /* 572 * The OPEN operation opens a regular file. 573 * 574 * ARGSUSED 575 */ 576 static int 577 nfs4_open(vnode_t **vpp, int flag, cred_t *cr) 578 { 579 vnode_t *dvp = NULL; 580 rnode4_t *rp; 581 int error; 582 int just_been_created; 583 char fn[MAXNAMELEN]; 584 585 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 586 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 587 return (EIO); 588 rp = VTOR4(*vpp); 589 590 /* 591 * Check to see if opening something besides a regular file; 592 * if so skip the OTW call 593 */ 594 if ((*vpp)->v_type != VREG) { 595 error = nfs4_open_non_reg_file(vpp, flag, cr); 596 return (error); 597 } 598 599 /* 600 * XXX - would like a check right here to know if the file is 601 * executable or not, so as to skip OTW 602 */ 603 604 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) 605 return (error); 606 607 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 608 return (error); 609 610 /* 611 * See if this file has just been CREATEd. 612 * If so, clear the flag and update the dnlc, which was previously 613 * skipped in nfs4_create. 614 * XXX need better serilization on this. 615 * XXX move this into the nf4open_otw call, after we have 616 * XXX acquired the open owner seqid sync. 617 */ 618 mutex_enter(&rp->r_statev4_lock); 619 if (rp->created_v4) { 620 rp->created_v4 = 0; 621 mutex_exit(&rp->r_statev4_lock); 622 623 dnlc_update(dvp, fn, *vpp); 624 /* This is needed so we don't bump the open ref count */ 625 just_been_created = 1; 626 } else { 627 mutex_exit(&rp->r_statev4_lock); 628 just_been_created = 0; 629 } 630 631 /* 632 * If caller specified O_TRUNC/FTRUNC, then be sure to set 633 * FWRITE (to drive successful setattr(size=0) after open) 634 */ 635 if (flag & FTRUNC) 636 flag |= FWRITE; 637 638 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 639 just_been_created); 640 641 if (!error && !((*vpp)->v_flag & VROOT)) 642 dnlc_update(dvp, fn, *vpp); 643 644 /* release the hold from vtodv */ 645 VN_RELE(dvp); 646 647 /* exchange the shadow for the master vnode, if needed */ 648 649 if (error == 0 && IS_SHADOW(*vpp, rp)) 650 sv_exchange(vpp); 651 652 return (error); 653 } 654 655 /* 656 * See if there's a "lost open" request to be saved and recovered. 657 */ 658 static void 659 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 660 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 661 vnode_t *dvp, OPEN4cargs *open_args) 662 { 663 vfs_t *vfsp; 664 char *srccfp; 665 666 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 667 668 if (error != ETIMEDOUT && error != EINTR && 669 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 670 lost_rqstp->lr_op = 0; 671 return; 672 } 673 674 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 675 "nfs4open_save_lost_rqst: error %d", error)); 676 677 lost_rqstp->lr_op = OP_OPEN; 678 /* 679 * The vp (if it is not NULL) and dvp are held and rele'd via 680 * the recovery code. See nfs4_save_lost_rqst. 681 */ 682 lost_rqstp->lr_vp = vp; 683 lost_rqstp->lr_dvp = dvp; 684 lost_rqstp->lr_oop = oop; 685 lost_rqstp->lr_osp = NULL; 686 lost_rqstp->lr_lop = NULL; 687 lost_rqstp->lr_cr = cr; 688 lost_rqstp->lr_flk = NULL; 689 lost_rqstp->lr_oacc = open_args->share_access; 690 lost_rqstp->lr_odeny = open_args->share_deny; 691 lost_rqstp->lr_oclaim = open_args->claim; 692 if (open_args->claim == CLAIM_DELEGATE_CUR) { 693 lost_rqstp->lr_ostateid = 694 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 695 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 696 } else { 697 srccfp = open_args->open_claim4_u.cfile; 698 } 699 lost_rqstp->lr_ofile.utf8string_len = 0; 700 lost_rqstp->lr_ofile.utf8string_val = NULL; 701 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 702 lost_rqstp->lr_putfirst = FALSE; 703 } 704 705 struct nfs4_excl_time { 706 uint32 seconds; 707 uint32 nseconds; 708 }; 709 710 /* 711 * The OPEN operation creates and/or opens a regular file 712 * 713 * ARGSUSED 714 */ 715 static int 716 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 717 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 718 enum createmode4 createmode, int file_just_been_created) 719 { 720 rnode4_t *rp; 721 rnode4_t *drp = VTOR4(dvp); 722 vnode_t *vp = NULL; 723 vnode_t *vpi = *vpp; 724 bool_t needrecov = FALSE; 725 726 int doqueue = 1; 727 728 COMPOUND4args_clnt args; 729 COMPOUND4res_clnt res; 730 nfs_argop4 *argop; 731 nfs_resop4 *resop; 732 int argoplist_size; 733 int idx_open, idx_fattr; 734 735 GETFH4res *gf_res = NULL; 736 OPEN4res *op_res = NULL; 737 nfs4_ga_res_t *garp; 738 fattr4 *attr = NULL; 739 struct nfs4_excl_time verf; 740 bool_t did_excl_setup = FALSE; 741 int created_osp; 742 743 OPEN4cargs *open_args; 744 nfs4_open_owner_t *oop = NULL; 745 nfs4_open_stream_t *osp = NULL; 746 seqid4 seqid = 0; 747 bool_t retry_open = FALSE; 748 nfs4_recov_state_t recov_state; 749 nfs4_lost_rqst_t lost_rqst; 750 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 751 hrtime_t t; 752 int acc = 0; 753 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 754 cred_t *ncr = NULL; 755 756 nfs4_sharedfh_t *otw_sfh; 757 nfs4_sharedfh_t *orig_sfh; 758 int fh_differs = 0; 759 int numops, setgid_flag; 760 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 761 762 /* 763 * Make sure we properly deal with setting the right gid on 764 * a newly created file to reflect the parent's setgid bit 765 */ 766 setgid_flag = 0; 767 if (create_flag && in_va) { 768 769 /* 770 * If the parent's directory has the setgid bit set 771 * _and_ the client was able to get a valid mapping 772 * for the parent dir's owner_group, we want to 773 * append NVERIFY(owner_group == dva.va_gid) and 774 * SETATTR to the CREATE compound. 775 */ 776 mutex_enter(&drp->r_statelock); 777 if (drp->r_attr.va_mode & VSGID && 778 drp->r_attr.va_gid != GID_NOBODY) { 779 in_va->va_gid = drp->r_attr.va_gid; 780 setgid_flag = 1; 781 } 782 mutex_exit(&drp->r_statelock); 783 } 784 785 /* 786 * Normal/non-create compound: 787 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 788 * 789 * Open(create) compound no setgid: 790 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 791 * RESTOREFH + GETATTR 792 * 793 * Open(create) setgid: 794 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 795 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 796 * NVERIFY(grp) + SETATTR 797 */ 798 if (setgid_flag) { 799 numops = 10; 800 idx_open = 1; 801 idx_fattr = 3; 802 } else if (create_flag) { 803 numops = 7; 804 idx_open = 2; 805 idx_fattr = 4; 806 } else { 807 numops = 4; 808 idx_open = 1; 809 idx_fattr = 3; 810 } 811 812 args.array_len = numops; 813 argoplist_size = numops * sizeof (nfs_argop4); 814 argop = kmem_alloc(argoplist_size, KM_SLEEP); 815 816 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 817 "open %s open flag 0x%x cred %p", file_name, open_flag, 818 (void *)cr)); 819 820 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 821 if (create_flag) { 822 /* 823 * We are to create a file. Initialize the passed in vnode 824 * pointer. 825 */ 826 vpi = NULL; 827 } else { 828 /* 829 * Check to see if the client owns a read delegation and is 830 * trying to open for write. If so, then return the delegation 831 * to avoid the server doing a cb_recall and returning DELAY. 832 * NB - we don't use the statev4_lock here because we'd have 833 * to drop the lock anyway and the result would be stale. 834 */ 835 if ((open_flag & FWRITE) && 836 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 837 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 838 839 /* 840 * If the file has a delegation, then do an access check up 841 * front. This avoids having to an access check later after 842 * we've already done start_op, which could deadlock. 843 */ 844 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 845 if (open_flag & FREAD && 846 nfs4_access(vpi, VREAD, 0, cr) == 0) 847 acc |= VREAD; 848 if (open_flag & FWRITE && 849 nfs4_access(vpi, VWRITE, 0, cr) == 0) 850 acc |= VWRITE; 851 } 852 } 853 854 drp = VTOR4(dvp); 855 856 recov_state.rs_flags = 0; 857 recov_state.rs_num_retry_despite_err = 0; 858 cred_otw = cr; 859 860 recov_retry: 861 fh_differs = 0; 862 nfs4_error_zinit(&e); 863 864 /* argop is empty here */ 865 866 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 867 if (ncr != NULL) 868 crfree(ncr); 869 kmem_free(argop, argoplist_size); 870 return (EINTR); 871 } 872 873 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 874 if (e.error) { 875 nfs_rw_exit(&drp->r_rwlock); 876 if (ncr != NULL) 877 crfree(ncr); 878 kmem_free(argop, argoplist_size); 879 return (e.error); 880 } 881 882 args.ctag = TAG_OPEN; 883 args.array_len = numops; 884 args.array = argop; 885 886 /* putfh directory fh */ 887 argop[0].argop = OP_CPUTFH; 888 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 889 890 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 891 argop[idx_open].argop = OP_COPEN; 892 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 893 open_args->claim = CLAIM_NULL; 894 895 /* name of file */ 896 open_args->open_claim4_u.cfile = file_name; 897 open_args->owner.owner_len = 0; 898 open_args->owner.owner_val = NULL; 899 900 if (create_flag) { 901 /* CREATE a file */ 902 open_args->opentype = OPEN4_CREATE; 903 open_args->mode = createmode; 904 if (createmode == EXCLUSIVE4) { 905 if (did_excl_setup == FALSE) { 906 verf.seconds = nfs_atoi(hw_serial); 907 if (verf.seconds != 0) 908 verf.nseconds = newnum(); 909 else { 910 timestruc_t now; 911 912 gethrestime(&now); 913 verf.seconds = now.tv_sec; 914 verf.nseconds = now.tv_nsec; 915 } 916 /* 917 * Since the server will use this value for the 918 * mtime, make sure that it can't overflow. Zero 919 * out the MSB. The actual value does not matter 920 * here, only its uniqeness. 921 */ 922 verf.seconds &= INT32_MAX; 923 did_excl_setup = TRUE; 924 } 925 926 /* Now copy over verifier to OPEN4args. */ 927 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 928 } else { 929 int v_error; 930 bitmap4 supp_attrs; 931 servinfo4_t *svp; 932 933 attr = &open_args->createhow4_u.createattrs; 934 935 svp = drp->r_server; 936 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 937 supp_attrs = svp->sv_supp_attrs; 938 nfs_rw_exit(&svp->sv_lock); 939 940 /* GUARDED4 or UNCHECKED4 */ 941 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 942 supp_attrs); 943 if (v_error) { 944 bzero(attr, sizeof (*attr)); 945 nfs4args_copen_free(open_args); 946 nfs_rw_exit(&drp->r_rwlock); 947 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 948 &recov_state, FALSE); 949 if (ncr != NULL) 950 crfree(ncr); 951 kmem_free(argop, argoplist_size); 952 return (v_error); 953 } 954 } 955 } else { 956 /* NO CREATE */ 957 open_args->opentype = OPEN4_NOCREATE; 958 } 959 960 if (recov_state.rs_sp != NULL) { 961 mutex_enter(&recov_state.rs_sp->s_lock); 962 open_args->owner.clientid = recov_state.rs_sp->clientid; 963 mutex_exit(&recov_state.rs_sp->s_lock); 964 } else { 965 /* XXX should we just fail here? */ 966 open_args->owner.clientid = 0; 967 } 968 969 /* 970 * This increments oop's ref count or creates a temporary 'just_created' 971 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 972 * completes. 973 */ 974 mutex_enter(&VTOMI4(dvp)->mi_lock); 975 976 /* See if a permanent or just created open owner exists */ 977 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 978 if (!oop) { 979 /* 980 * This open owner does not exist so create a temporary 981 * just created one. 982 */ 983 oop = create_open_owner(cr, VTOMI4(dvp)); 984 ASSERT(oop != NULL); 985 } 986 mutex_exit(&VTOMI4(dvp)->mi_lock); 987 988 /* this length never changes, do alloc before seqid sync */ 989 open_args->owner.owner_len = sizeof (oop->oo_name); 990 open_args->owner.owner_val = 991 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 992 993 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 994 if (e.error == EAGAIN) { 995 open_owner_rele(oop); 996 nfs4args_copen_free(open_args); 997 nfs_rw_exit(&drp->r_rwlock); 998 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 999 if (ncr != NULL) { 1000 crfree(ncr); 1001 ncr = NULL; 1002 } 1003 goto recov_retry; 1004 } 1005 1006 /* Check to see if we need to do the OTW call */ 1007 if (!create_flag) { 1008 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1009 file_just_been_created, &e.error, acc, &recov_state)) { 1010 1011 /* 1012 * The OTW open is not necessary. Either 1013 * the open can succeed without it (eg. 1014 * delegation, error == 0) or the open 1015 * must fail due to an access failure 1016 * (error != 0). In either case, tidy 1017 * up and return. 1018 */ 1019 1020 nfs4_end_open_seqid_sync(oop); 1021 open_owner_rele(oop); 1022 nfs4args_copen_free(open_args); 1023 nfs_rw_exit(&drp->r_rwlock); 1024 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1025 if (ncr != NULL) 1026 crfree(ncr); 1027 kmem_free(argop, argoplist_size); 1028 return (e.error); 1029 } 1030 } 1031 1032 bcopy(&oop->oo_name, open_args->owner.owner_val, 1033 open_args->owner.owner_len); 1034 1035 seqid = nfs4_get_open_seqid(oop) + 1; 1036 open_args->seqid = seqid; 1037 open_args->share_access = 0; 1038 if (open_flag & FREAD) 1039 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1040 if (open_flag & FWRITE) 1041 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1042 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1043 1044 1045 1046 /* 1047 * getfh w/sanity check for idx_open/idx_fattr 1048 */ 1049 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1050 argop[idx_open + 1].argop = OP_GETFH; 1051 1052 /* getattr */ 1053 argop[idx_fattr].argop = OP_GETATTR; 1054 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1055 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1056 1057 if (setgid_flag) { 1058 vattr_t _v; 1059 servinfo4_t *svp; 1060 bitmap4 supp_attrs; 1061 1062 svp = drp->r_server; 1063 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1064 supp_attrs = svp->sv_supp_attrs; 1065 nfs_rw_exit(&svp->sv_lock); 1066 1067 /* 1068 * For setgid case, we need to: 1069 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1070 */ 1071 argop[4].argop = OP_SAVEFH; 1072 1073 argop[5].argop = OP_CPUTFH; 1074 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1075 1076 argop[6].argop = OP_GETATTR; 1077 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1078 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1079 1080 argop[7].argop = OP_RESTOREFH; 1081 1082 /* 1083 * nverify 1084 */ 1085 _v.va_mask = AT_GID; 1086 _v.va_gid = in_va->va_gid; 1087 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1088 supp_attrs))) { 1089 1090 /* 1091 * setattr 1092 * 1093 * We _know_ we're not messing with AT_SIZE or 1094 * AT_XTIME, so no need for stateid or flags. 1095 * Also we specify NULL rp since we're only 1096 * interested in setting owner_group attributes. 1097 */ 1098 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1099 supp_attrs, &e.error, 0); 1100 if (e.error) 1101 nfs4args_verify_free(&argop[8]); 1102 } 1103 1104 if (e.error) { 1105 /* 1106 * XXX - Revisit the last argument to nfs4_end_op() 1107 * once 5020486 is fixed. 1108 */ 1109 nfs4_end_open_seqid_sync(oop); 1110 open_owner_rele(oop); 1111 nfs4args_copen_free(open_args); 1112 nfs_rw_exit(&drp->r_rwlock); 1113 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1114 if (ncr != NULL) 1115 crfree(ncr); 1116 kmem_free(argop, argoplist_size); 1117 return (e.error); 1118 } 1119 } else if (create_flag) { 1120 /* 1121 * For setgid case, we need to: 1122 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1123 */ 1124 argop[1].argop = OP_SAVEFH; 1125 1126 argop[5].argop = OP_RESTOREFH; 1127 1128 argop[6].argop = OP_GETATTR; 1129 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1130 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1131 } 1132 1133 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1134 "nfs4open_otw: %s call, nm %s, rp %s", 1135 needrecov ? "recov" : "first", file_name, 1136 rnode4info(VTOR4(dvp)))); 1137 1138 t = gethrtime(); 1139 1140 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1141 1142 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1143 nfs4_set_open_seqid(seqid, oop, args.ctag); 1144 1145 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1146 1147 if (e.error || needrecov) { 1148 bool_t abort = FALSE; 1149 1150 if (needrecov) { 1151 nfs4_bseqid_entry_t *bsep = NULL; 1152 1153 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1154 cred_otw, vpi, dvp, open_args); 1155 1156 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1157 bsep = nfs4_create_bseqid_entry(oop, NULL, 1158 vpi, 0, args.ctag, open_args->seqid); 1159 num_bseqid_retry--; 1160 } 1161 1162 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1163 NULL, lost_rqst.lr_op == OP_OPEN ? 1164 &lost_rqst : NULL, OP_OPEN, bsep); 1165 1166 if (bsep) 1167 kmem_free(bsep, sizeof (*bsep)); 1168 /* give up if we keep getting BAD_SEQID */ 1169 if (num_bseqid_retry == 0) 1170 abort = TRUE; 1171 if (abort == TRUE && e.error == 0) 1172 e.error = geterrno4(res.status); 1173 } 1174 nfs4_end_open_seqid_sync(oop); 1175 open_owner_rele(oop); 1176 nfs_rw_exit(&drp->r_rwlock); 1177 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1178 nfs4args_copen_free(open_args); 1179 if (setgid_flag) { 1180 nfs4args_verify_free(&argop[8]); 1181 nfs4args_setattr_free(&argop[9]); 1182 } 1183 if (!e.error) 1184 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1185 if (ncr != NULL) { 1186 crfree(ncr); 1187 ncr = NULL; 1188 } 1189 if (!needrecov || abort == TRUE || e.error == EINTR || 1190 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1191 kmem_free(argop, argoplist_size); 1192 return (e.error); 1193 } 1194 goto recov_retry; 1195 } 1196 1197 /* 1198 * Will check and update lease after checking the rflag for 1199 * OPEN_CONFIRM in the successful OPEN call. 1200 */ 1201 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1202 1203 /* 1204 * XXX what if we're crossing mount points from server1:/drp 1205 * to server2:/drp/rp. 1206 */ 1207 1208 /* Signal our end of use of the open seqid */ 1209 nfs4_end_open_seqid_sync(oop); 1210 1211 /* 1212 * This will destroy the open owner if it was just created, 1213 * and no one else has put a reference on it. 1214 */ 1215 open_owner_rele(oop); 1216 if (create_flag && (createmode != EXCLUSIVE4) && 1217 res.status == NFS4ERR_BADOWNER) 1218 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1219 1220 e.error = geterrno4(res.status); 1221 nfs4args_copen_free(open_args); 1222 if (setgid_flag) { 1223 nfs4args_verify_free(&argop[8]); 1224 nfs4args_setattr_free(&argop[9]); 1225 } 1226 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1227 nfs_rw_exit(&drp->r_rwlock); 1228 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1229 /* 1230 * If the reply is NFS4ERR_ACCESS, it may be because 1231 * we are root (no root net access). If the real uid 1232 * is not root, then retry with the real uid instead. 1233 */ 1234 if (ncr != NULL) { 1235 crfree(ncr); 1236 ncr = NULL; 1237 } 1238 if (res.status == NFS4ERR_ACCESS && 1239 (ncr = crnetadjust(cred_otw)) != NULL) { 1240 cred_otw = ncr; 1241 goto recov_retry; 1242 } 1243 kmem_free(argop, argoplist_size); 1244 return (e.error); 1245 } 1246 1247 resop = &res.array[idx_open]; /* open res */ 1248 op_res = &resop->nfs_resop4_u.opopen; 1249 1250 #ifdef DEBUG 1251 /* 1252 * verify attrset bitmap 1253 */ 1254 if (create_flag && 1255 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1256 /* make sure attrset returned is what we asked for */ 1257 /* XXX Ignore this 'error' for now */ 1258 if (attr->attrmask != op_res->attrset) 1259 /* EMPTY */; 1260 } 1261 #endif 1262 1263 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1264 mutex_enter(&VTOMI4(dvp)->mi_lock); 1265 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1266 mutex_exit(&VTOMI4(dvp)->mi_lock); 1267 } 1268 1269 resop = &res.array[idx_open + 1]; /* getfh res */ 1270 gf_res = &resop->nfs_resop4_u.opgetfh; 1271 1272 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1273 1274 /* 1275 * The open stateid has been updated on the server but not 1276 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1277 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1278 * WRITE call. That, however, will use the old stateid, so go ahead 1279 * and upate the open stateid now, before any call to makenfs4node. 1280 */ 1281 if (vpi) { 1282 nfs4_open_stream_t *tmp_osp; 1283 rnode4_t *tmp_rp = VTOR4(vpi); 1284 1285 tmp_osp = find_open_stream(oop, tmp_rp); 1286 if (tmp_osp) { 1287 tmp_osp->open_stateid = op_res->stateid; 1288 mutex_exit(&tmp_osp->os_sync_lock); 1289 open_stream_rele(tmp_osp, tmp_rp); 1290 } 1291 1292 /* 1293 * We must determine if the file handle given by the otw open 1294 * is the same as the file handle which was passed in with 1295 * *vpp. This case can be reached if the file we are trying 1296 * to open has been removed and another file has been created 1297 * having the same file name. The passed in vnode is released 1298 * later. 1299 */ 1300 orig_sfh = VTOR4(vpi)->r_fh; 1301 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1302 } 1303 1304 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1305 1306 if (create_flag || fh_differs) { 1307 int rnode_err = 0; 1308 1309 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1310 dvp, fn_get(VTOSV(dvp)->sv_name, file_name)); 1311 1312 if (e.error) 1313 PURGE_ATTRCACHE4(vp); 1314 /* 1315 * For the newly created vp case, make sure the rnode 1316 * isn't bad before using it. 1317 */ 1318 mutex_enter(&(VTOR4(vp))->r_statelock); 1319 if (VTOR4(vp)->r_flags & R4RECOVERR) 1320 rnode_err = EIO; 1321 mutex_exit(&(VTOR4(vp))->r_statelock); 1322 1323 if (rnode_err) { 1324 nfs4_end_open_seqid_sync(oop); 1325 nfs4args_copen_free(open_args); 1326 if (setgid_flag) { 1327 nfs4args_verify_free(&argop[8]); 1328 nfs4args_setattr_free(&argop[9]); 1329 } 1330 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1331 nfs_rw_exit(&drp->r_rwlock); 1332 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1333 needrecov); 1334 open_owner_rele(oop); 1335 VN_RELE(vp); 1336 if (ncr != NULL) 1337 crfree(ncr); 1338 sfh4_rele(&otw_sfh); 1339 kmem_free(argop, argoplist_size); 1340 return (EIO); 1341 } 1342 } else { 1343 vp = vpi; 1344 } 1345 sfh4_rele(&otw_sfh); 1346 1347 /* 1348 * It seems odd to get a full set of attrs and then not update 1349 * the object's attrcache in the non-create case. Create case uses 1350 * the attrs since makenfs4node checks to see if the attrs need to 1351 * be updated (and then updates them). The non-create case should 1352 * update attrs also. 1353 */ 1354 if (! create_flag && ! fh_differs && !e.error) { 1355 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1356 } 1357 1358 nfs4_error_zinit(&e); 1359 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1360 /* This does not do recovery for vp explicitly. */ 1361 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1362 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1363 1364 if (e.error || e.stat) { 1365 nfs4_end_open_seqid_sync(oop); 1366 nfs4args_copen_free(open_args); 1367 if (setgid_flag) { 1368 nfs4args_verify_free(&argop[8]); 1369 nfs4args_setattr_free(&argop[9]); 1370 } 1371 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1372 nfs_rw_exit(&drp->r_rwlock); 1373 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1374 needrecov); 1375 open_owner_rele(oop); 1376 if (create_flag || fh_differs) { 1377 /* rele the makenfs4node */ 1378 VN_RELE(vp); 1379 } 1380 if (ncr != NULL) { 1381 crfree(ncr); 1382 ncr = NULL; 1383 } 1384 if (retry_open == TRUE) { 1385 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1386 "nfs4open_otw: retry the open since OPEN " 1387 "CONFIRM failed with error %d stat %d", 1388 e.error, e.stat)); 1389 if (create_flag && createmode == GUARDED4) { 1390 NFS4_DEBUG(nfs4_client_recov_debug, 1391 (CE_NOTE, "nfs4open_otw: switch " 1392 "createmode from GUARDED4 to " 1393 "UNCHECKED4")); 1394 createmode = UNCHECKED4; 1395 } 1396 goto recov_retry; 1397 } 1398 if (!e.error) { 1399 if (create_flag && (createmode != EXCLUSIVE4) && 1400 e.stat == NFS4ERR_BADOWNER) 1401 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1402 1403 e.error = geterrno4(e.stat); 1404 } 1405 kmem_free(argop, argoplist_size); 1406 return (e.error); 1407 } 1408 } 1409 1410 rp = VTOR4(vp); 1411 1412 mutex_enter(&rp->r_statev4_lock); 1413 if (create_flag) 1414 rp->created_v4 = 1; 1415 mutex_exit(&rp->r_statev4_lock); 1416 1417 mutex_enter(&oop->oo_lock); 1418 /* Doesn't matter if 'oo_just_created' already was set as this */ 1419 oop->oo_just_created = NFS4_PERM_CREATED; 1420 if (oop->oo_cred_otw) 1421 crfree(oop->oo_cred_otw); 1422 oop->oo_cred_otw = cred_otw; 1423 crhold(oop->oo_cred_otw); 1424 mutex_exit(&oop->oo_lock); 1425 1426 /* returns with 'os_sync_lock' held */ 1427 osp = find_or_create_open_stream(oop, rp, &created_osp); 1428 if (!osp) { 1429 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1430 "nfs4open_otw: failed to create an open stream")); 1431 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1432 "signal our end of use of the open seqid")); 1433 1434 nfs4_end_open_seqid_sync(oop); 1435 open_owner_rele(oop); 1436 nfs4args_copen_free(open_args); 1437 if (setgid_flag) { 1438 nfs4args_verify_free(&argop[8]); 1439 nfs4args_setattr_free(&argop[9]); 1440 } 1441 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1442 nfs_rw_exit(&drp->r_rwlock); 1443 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1444 if (create_flag || fh_differs) 1445 VN_RELE(vp); 1446 if (ncr != NULL) 1447 crfree(ncr); 1448 1449 kmem_free(argop, argoplist_size); 1450 return (EINVAL); 1451 1452 } 1453 1454 osp->open_stateid = op_res->stateid; 1455 1456 if (open_flag & FREAD) 1457 osp->os_share_acc_read++; 1458 if (open_flag & FWRITE) 1459 osp->os_share_acc_write++; 1460 osp->os_share_deny_none++; 1461 1462 /* 1463 * Need to reset this bitfield for the possible case where we were 1464 * going to OTW CLOSE the file, got a non-recoverable error, and before 1465 * we could retry the CLOSE, OPENed the file again. 1466 */ 1467 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1468 osp->os_final_close = 0; 1469 osp->os_force_close = 0; 1470 #ifdef DEBUG 1471 if (osp->os_failed_reopen) 1472 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1473 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1474 (void *)osp, (void *)cr, rnode4info(rp))); 1475 #endif 1476 osp->os_failed_reopen = 0; 1477 1478 mutex_exit(&osp->os_sync_lock); 1479 1480 nfs4_end_open_seqid_sync(oop); 1481 1482 if (created_osp && recov_state.rs_sp != NULL) { 1483 mutex_enter(&recov_state.rs_sp->s_lock); 1484 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1485 mutex_exit(&recov_state.rs_sp->s_lock); 1486 } 1487 1488 /* get rid of our reference to find oop */ 1489 open_owner_rele(oop); 1490 1491 open_stream_rele(osp, rp); 1492 1493 /* accept delegation, if any */ 1494 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1495 1496 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1497 1498 if (createmode == EXCLUSIVE4 && 1499 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1500 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1501 " EXCLUSIVE4: sending a SETATTR")); 1502 /* 1503 * If doing an exclusive create, then generate 1504 * a SETATTR to set the initial attributes. 1505 * Try to set the mtime and the atime to the 1506 * server's current time. It is somewhat 1507 * expected that these fields will be used to 1508 * store the exclusive create cookie. If not, 1509 * server implementors will need to know that 1510 * a SETATTR will follow an exclusive create 1511 * and the cookie should be destroyed if 1512 * appropriate. 1513 * 1514 * The AT_GID and AT_SIZE bits are turned off 1515 * so that the SETATTR request will not attempt 1516 * to process these. The gid will be set 1517 * separately if appropriate. The size is turned 1518 * off because it is assumed that a new file will 1519 * be created empty and if the file wasn't empty, 1520 * then the exclusive create will have failed 1521 * because the file must have existed already. 1522 * Therefore, no truncate operation is needed. 1523 */ 1524 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1525 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1526 1527 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1528 if (e.error) { 1529 /* 1530 * Couldn't correct the attributes of 1531 * the newly created file and the 1532 * attributes are wrong. Remove the 1533 * file and return an error to the 1534 * application. 1535 */ 1536 /* XXX will this take care of client state ? */ 1537 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1538 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1539 " remove file", e.error)); 1540 VN_RELE(vp); 1541 (void) nfs4_remove(dvp, file_name, cr); 1542 nfs_rw_exit(&drp->r_rwlock); 1543 goto skip_rwlock_exit; 1544 } 1545 } 1546 1547 /* 1548 * If we created or found the correct vnode, due to create_flag or 1549 * fh_differs being set, then update directory cache attribute, readdir 1550 * and dnlc caches. 1551 */ 1552 if (create_flag || fh_differs) { 1553 dirattr_info_t dinfo, *dinfop; 1554 1555 /* 1556 * Make sure getattr succeeded before using results. 1557 * note: op 7 is getattr(dir) for both flavors of 1558 * open(create). 1559 */ 1560 if (create_flag && res.status == NFS4_OK) { 1561 dinfo.di_time_call = t; 1562 dinfo.di_cred = cr; 1563 dinfo.di_garp = 1564 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1565 dinfop = &dinfo; 1566 } else { 1567 dinfop = NULL; 1568 } 1569 1570 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1571 dinfop); 1572 } 1573 nfs_rw_exit(&drp->r_rwlock); 1574 skip_rwlock_exit: 1575 1576 /* 1577 * If the page cache for this file was flushed from actions 1578 * above, it was done asynchronously and if that is true, 1579 * there is a need to wait here for it to complete. This must 1580 * be done outside of start_fop/end_fop. 1581 */ 1582 (void) nfs4_waitfor_purge_complete(vp); 1583 1584 /* 1585 * It is implicit that we are in the open case (create_flag == 0) since 1586 * fh_differs can only be set to a non-zero value in the open case. 1587 */ 1588 if (fh_differs != 0 && vpi != NULL) 1589 VN_RELE(vpi); 1590 1591 /* 1592 * Be sure to set *vpp to the correct value before returning. 1593 */ 1594 *vpp = vp; 1595 1596 nfs4args_copen_free(open_args); 1597 if (setgid_flag) { 1598 nfs4args_verify_free(&argop[8]); 1599 nfs4args_setattr_free(&argop[9]); 1600 } 1601 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1602 1603 if (ncr) 1604 crfree(ncr); 1605 kmem_free(argop, argoplist_size); 1606 return (e.error); 1607 } 1608 1609 /* 1610 * Reopen an open instance. cf. nfs4open_otw(). 1611 * 1612 * Errors are returned by the nfs4_error_t parameter. 1613 * - ep->error contains an errno value or zero. 1614 * - if it is zero, ep->stat is set to an NFS status code, if any. 1615 * If the file could not be reopened, but the caller should continue, the 1616 * file is marked dead and no error values are returned. If the caller 1617 * should stop recovering open files and start over, either the ep->error 1618 * value or ep->stat will indicate an error (either something that requires 1619 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1620 * filehandles) may be handled silently by this routine. 1621 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1622 * will be started, so the caller should not do it. 1623 * 1624 * Gotos: 1625 * - kill_file : reopen failed in such a fashion to constitute marking the 1626 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1627 * is for cases where recovery is not possible. 1628 * - failed_reopen : same as above, except that the file has already been 1629 * marked dead, so no need to do it again. 1630 * - bailout : reopen failed but we are able to recover and retry the reopen - 1631 * either within this function immediatley or via the calling function. 1632 */ 1633 1634 void 1635 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1636 open_claim_type4 claim, bool_t frc_use_claim_previous, 1637 bool_t is_recov) 1638 { 1639 COMPOUND4args_clnt args; 1640 COMPOUND4res_clnt res; 1641 nfs_argop4 argop[4]; 1642 nfs_resop4 *resop; 1643 OPEN4res *op_res = NULL; 1644 OPEN4cargs *open_args; 1645 GETFH4res *gf_res; 1646 rnode4_t *rp = VTOR4(vp); 1647 int doqueue = 1; 1648 cred_t *cr = NULL, *cred_otw = NULL; 1649 nfs4_open_owner_t *oop = NULL; 1650 seqid4 seqid; 1651 nfs4_ga_res_t *garp; 1652 char fn[MAXNAMELEN]; 1653 nfs4_recov_state_t recov = {NULL, 0}; 1654 nfs4_lost_rqst_t lost_rqst; 1655 mntinfo4_t *mi = VTOMI4(vp); 1656 bool_t abort; 1657 char *failed_msg = ""; 1658 int fh_different; 1659 hrtime_t t; 1660 nfs4_bseqid_entry_t *bsep = NULL; 1661 1662 ASSERT(nfs4_consistent_type(vp)); 1663 ASSERT(nfs_zone() == mi->mi_zone); 1664 1665 nfs4_error_zinit(ep); 1666 1667 /* this is the cred used to find the open owner */ 1668 cr = state_to_cred(osp); 1669 if (cr == NULL) { 1670 failed_msg = "Couldn't reopen: no cred"; 1671 goto kill_file; 1672 } 1673 /* use this cred for OTW operations */ 1674 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1675 1676 top: 1677 nfs4_error_zinit(ep); 1678 1679 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1680 /* File system has been unmounted, quit */ 1681 ep->error = EIO; 1682 failed_msg = "Couldn't reopen: file system has been unmounted"; 1683 goto kill_file; 1684 } 1685 1686 oop = osp->os_open_owner; 1687 1688 ASSERT(oop != NULL); 1689 if (oop == NULL) { /* be defensive in non-DEBUG */ 1690 failed_msg = "can't reopen: no open owner"; 1691 goto kill_file; 1692 } 1693 open_owner_hold(oop); 1694 1695 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1696 if (ep->error) { 1697 open_owner_rele(oop); 1698 oop = NULL; 1699 goto bailout; 1700 } 1701 1702 /* 1703 * If the rnode has a delegation and the delegation has been 1704 * recovered and the server didn't request a recall and the caller 1705 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1706 * recovery) and the rnode hasn't been marked dead, then install 1707 * the delegation stateid in the open stream. Otherwise, proceed 1708 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1709 */ 1710 mutex_enter(&rp->r_statev4_lock); 1711 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1712 !rp->r_deleg_return_pending && 1713 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1714 !rp->r_deleg_needs_recall && 1715 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1716 !(rp->r_flags & R4RECOVERR)) { 1717 mutex_enter(&osp->os_sync_lock); 1718 osp->os_delegation = 1; 1719 osp->open_stateid = rp->r_deleg_stateid; 1720 mutex_exit(&osp->os_sync_lock); 1721 mutex_exit(&rp->r_statev4_lock); 1722 goto bailout; 1723 } 1724 mutex_exit(&rp->r_statev4_lock); 1725 1726 /* 1727 * If the file failed recovery, just quit. This failure need not 1728 * affect other reopens, so don't return an error. 1729 */ 1730 mutex_enter(&rp->r_statelock); 1731 if (rp->r_flags & R4RECOVERR) { 1732 mutex_exit(&rp->r_statelock); 1733 ep->error = 0; 1734 goto failed_reopen; 1735 } 1736 mutex_exit(&rp->r_statelock); 1737 1738 /* 1739 * argop is empty here 1740 * 1741 * PUTFH, OPEN, GETATTR 1742 */ 1743 args.ctag = TAG_REOPEN; 1744 args.array_len = 4; 1745 args.array = argop; 1746 1747 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1748 "nfs4_reopen: file is type %d, id %s", 1749 vp->v_type, rnode4info(VTOR4(vp)))); 1750 1751 argop[0].argop = OP_CPUTFH; 1752 1753 if (claim != CLAIM_PREVIOUS) { 1754 /* 1755 * if this is a file mount then 1756 * use the mntinfo parentfh 1757 */ 1758 argop[0].nfs_argop4_u.opcputfh.sfh = 1759 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1760 VTOSV(vp)->sv_dfh; 1761 } else { 1762 /* putfh fh to reopen */ 1763 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1764 } 1765 1766 argop[1].argop = OP_COPEN; 1767 open_args = &argop[1].nfs_argop4_u.opcopen; 1768 open_args->claim = claim; 1769 1770 if (claim == CLAIM_NULL) { 1771 1772 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1773 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1774 "failed for vp 0x%p for CLAIM_NULL with %m", 1775 (void *)vp); 1776 failed_msg = "Couldn't reopen: vtoname failed for " 1777 "CLAIM_NULL"; 1778 /* nothing allocated yet */ 1779 goto kill_file; 1780 } 1781 1782 open_args->open_claim4_u.cfile = fn; 1783 } else if (claim == CLAIM_PREVIOUS) { 1784 1785 /* 1786 * We have two cases to deal with here: 1787 * 1) We're being called to reopen files in order to satisfy 1788 * a lock operation request which requires us to explicitly 1789 * reopen files which were opened under a delegation. If 1790 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1791 * that case, frc_use_claim_previous is TRUE and we must 1792 * use the rnode's current delegation type (r_deleg_type). 1793 * 2) We're reopening files during some form of recovery. 1794 * In this case, frc_use_claim_previous is FALSE and we 1795 * use the delegation type appropriate for recovery 1796 * (r_deleg_needs_recovery). 1797 */ 1798 mutex_enter(&rp->r_statev4_lock); 1799 open_args->open_claim4_u.delegate_type = 1800 frc_use_claim_previous ? 1801 rp->r_deleg_type : 1802 rp->r_deleg_needs_recovery; 1803 mutex_exit(&rp->r_statev4_lock); 1804 1805 } else if (claim == CLAIM_DELEGATE_CUR) { 1806 1807 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1808 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1809 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1810 "with %m", (void *)vp); 1811 failed_msg = "Couldn't reopen: vtoname failed for " 1812 "CLAIM_DELEGATE_CUR"; 1813 /* nothing allocated yet */ 1814 goto kill_file; 1815 } 1816 1817 mutex_enter(&rp->r_statev4_lock); 1818 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1819 rp->r_deleg_stateid; 1820 mutex_exit(&rp->r_statev4_lock); 1821 1822 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1823 } 1824 open_args->opentype = OPEN4_NOCREATE; 1825 open_args->owner.clientid = mi2clientid(mi); 1826 open_args->owner.owner_len = sizeof (oop->oo_name); 1827 open_args->owner.owner_val = 1828 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1829 bcopy(&oop->oo_name, open_args->owner.owner_val, 1830 open_args->owner.owner_len); 1831 open_args->share_access = 0; 1832 open_args->share_deny = 0; 1833 1834 mutex_enter(&osp->os_sync_lock); 1835 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1836 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1837 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1838 (void *)osp, (void *)rp, osp->os_share_acc_read, 1839 osp->os_share_acc_write, osp->os_open_ref_count, 1840 osp->os_mmap_read, osp->os_mmap_write, claim)); 1841 1842 if (osp->os_share_acc_read || osp->os_mmap_read) 1843 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1844 if (osp->os_share_acc_write || osp->os_mmap_write) 1845 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1846 if (osp->os_share_deny_read) 1847 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1848 if (osp->os_share_deny_write) 1849 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1850 mutex_exit(&osp->os_sync_lock); 1851 1852 seqid = nfs4_get_open_seqid(oop) + 1; 1853 open_args->seqid = seqid; 1854 1855 /* Construct the getfh part of the compound */ 1856 argop[2].argop = OP_GETFH; 1857 1858 /* Construct the getattr part of the compound */ 1859 argop[3].argop = OP_GETATTR; 1860 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1861 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1862 1863 t = gethrtime(); 1864 1865 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1866 1867 if (ep->error) { 1868 if (!is_recov && !frc_use_claim_previous && 1869 (ep->error == EINTR || ep->error == ETIMEDOUT || 1870 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1871 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1872 cred_otw, vp, NULL, open_args); 1873 abort = nfs4_start_recovery(ep, 1874 VTOMI4(vp), vp, NULL, NULL, 1875 lost_rqst.lr_op == OP_OPEN ? 1876 &lost_rqst : NULL, OP_OPEN, NULL); 1877 nfs4args_copen_free(open_args); 1878 goto bailout; 1879 } 1880 1881 nfs4args_copen_free(open_args); 1882 1883 if (ep->error == EACCES && cred_otw != cr) { 1884 crfree(cred_otw); 1885 cred_otw = cr; 1886 crhold(cred_otw); 1887 nfs4_end_open_seqid_sync(oop); 1888 open_owner_rele(oop); 1889 oop = NULL; 1890 goto top; 1891 } 1892 if (ep->error == ETIMEDOUT) 1893 goto bailout; 1894 failed_msg = "Couldn't reopen: rpc error"; 1895 goto kill_file; 1896 } 1897 1898 if (nfs4_need_to_bump_seqid(&res)) 1899 nfs4_set_open_seqid(seqid, oop, args.ctag); 1900 1901 switch (res.status) { 1902 case NFS4_OK: 1903 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1904 mutex_enter(&rp->r_statelock); 1905 rp->r_delay_interval = 0; 1906 mutex_exit(&rp->r_statelock); 1907 } 1908 break; 1909 case NFS4ERR_BAD_SEQID: 1910 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1911 args.ctag, open_args->seqid); 1912 1913 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1914 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1915 NULL, OP_OPEN, bsep); 1916 1917 nfs4args_copen_free(open_args); 1918 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1919 nfs4_end_open_seqid_sync(oop); 1920 open_owner_rele(oop); 1921 oop = NULL; 1922 kmem_free(bsep, sizeof (*bsep)); 1923 1924 goto kill_file; 1925 case NFS4ERR_NO_GRACE: 1926 nfs4args_copen_free(open_args); 1927 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1928 nfs4_end_open_seqid_sync(oop); 1929 open_owner_rele(oop); 1930 oop = NULL; 1931 if (claim == CLAIM_PREVIOUS) { 1932 /* 1933 * Retry as a plain open. We don't need to worry about 1934 * checking the changeinfo: it is acceptable for a 1935 * client to re-open a file and continue processing 1936 * (in the absence of locks). 1937 */ 1938 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1939 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1940 "will retry as CLAIM_NULL")); 1941 claim = CLAIM_NULL; 1942 nfs4_mi_kstat_inc_no_grace(mi); 1943 goto top; 1944 } 1945 failed_msg = 1946 "Couldn't reopen: tried reclaim outside grace period. "; 1947 goto kill_file; 1948 case NFS4ERR_GRACE: 1949 nfs4_set_grace_wait(mi); 1950 nfs4args_copen_free(open_args); 1951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1952 nfs4_end_open_seqid_sync(oop); 1953 open_owner_rele(oop); 1954 oop = NULL; 1955 ep->error = nfs4_wait_for_grace(mi, &recov); 1956 if (ep->error != 0) 1957 goto bailout; 1958 goto top; 1959 case NFS4ERR_DELAY: 1960 nfs4_set_delay_wait(vp); 1961 nfs4args_copen_free(open_args); 1962 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1963 nfs4_end_open_seqid_sync(oop); 1964 open_owner_rele(oop); 1965 oop = NULL; 1966 ep->error = nfs4_wait_for_delay(vp, &recov); 1967 nfs4_mi_kstat_inc_delay(mi); 1968 if (ep->error != 0) 1969 goto bailout; 1970 goto top; 1971 case NFS4ERR_FHEXPIRED: 1972 /* recover filehandle and retry */ 1973 abort = nfs4_start_recovery(ep, 1974 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL); 1975 nfs4args_copen_free(open_args); 1976 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1977 nfs4_end_open_seqid_sync(oop); 1978 open_owner_rele(oop); 1979 oop = NULL; 1980 if (abort == FALSE) 1981 goto top; 1982 failed_msg = "Couldn't reopen: recovery aborted"; 1983 goto kill_file; 1984 case NFS4ERR_RESOURCE: 1985 case NFS4ERR_STALE_CLIENTID: 1986 case NFS4ERR_WRONGSEC: 1987 case NFS4ERR_EXPIRED: 1988 /* 1989 * Do not mark the file dead and let the calling 1990 * function initiate recovery. 1991 */ 1992 nfs4args_copen_free(open_args); 1993 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1994 nfs4_end_open_seqid_sync(oop); 1995 open_owner_rele(oop); 1996 oop = NULL; 1997 goto bailout; 1998 case NFS4ERR_ACCESS: 1999 if (cred_otw != cr) { 2000 crfree(cred_otw); 2001 cred_otw = cr; 2002 crhold(cred_otw); 2003 nfs4args_copen_free(open_args); 2004 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2005 nfs4_end_open_seqid_sync(oop); 2006 open_owner_rele(oop); 2007 oop = NULL; 2008 goto top; 2009 } 2010 /* fall through */ 2011 default: 2012 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2013 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2014 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2015 rnode4info(VTOR4(vp)))); 2016 failed_msg = "Couldn't reopen: NFSv4 error"; 2017 nfs4args_copen_free(open_args); 2018 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2019 goto kill_file; 2020 } 2021 2022 resop = &res.array[1]; /* open res */ 2023 op_res = &resop->nfs_resop4_u.opopen; 2024 2025 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2026 2027 /* 2028 * Check if the path we reopened really is the same 2029 * file. We could end up in a situation where the file 2030 * was removed and a new file created with the same name. 2031 */ 2032 resop = &res.array[2]; 2033 gf_res = &resop->nfs_resop4_u.opgetfh; 2034 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2035 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2036 if (fh_different) { 2037 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2038 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2039 /* Oops, we don't have the same file */ 2040 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2041 failed_msg = "Couldn't reopen: Persistent " 2042 "file handle changed"; 2043 else 2044 failed_msg = "Couldn't reopen: Volatile " 2045 "(no expire on open) file handle changed"; 2046 2047 nfs4args_copen_free(open_args); 2048 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2049 nfs_rw_exit(&mi->mi_fh_lock); 2050 goto kill_file; 2051 2052 } else { 2053 /* 2054 * We have volatile file handles that don't compare. 2055 * If the fids are the same then we assume that the 2056 * file handle expired but the rnode still refers to 2057 * the same file object. 2058 * 2059 * First check that we have fids or not. 2060 * If we don't we have a dumb server so we will 2061 * just assume every thing is ok for now. 2062 */ 2063 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2064 rp->r_attr.va_mask & AT_NODEID && 2065 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2066 /* 2067 * We have fids, but they don't 2068 * compare. So kill the file. 2069 */ 2070 failed_msg = 2071 "Couldn't reopen: file handle changed" 2072 " due to mismatched fids"; 2073 nfs4args_copen_free(open_args); 2074 (void) xdr_free(xdr_COMPOUND4res_clnt, 2075 (caddr_t)&res); 2076 nfs_rw_exit(&mi->mi_fh_lock); 2077 goto kill_file; 2078 } else { 2079 /* 2080 * We have volatile file handles that refers 2081 * to the same file (at least they have the 2082 * same fid) or we don't have fids so we 2083 * can't tell. :(. We'll be a kind and accepting 2084 * client so we'll update the rnode's file 2085 * handle with the otw handle. 2086 * 2087 * We need to drop mi->mi_fh_lock since 2088 * sh4_update acquires it. Since there is 2089 * only one recovery thread there is no 2090 * race. 2091 */ 2092 nfs_rw_exit(&mi->mi_fh_lock); 2093 sfh4_update(rp->r_fh, &gf_res->object); 2094 } 2095 } 2096 } else { 2097 nfs_rw_exit(&mi->mi_fh_lock); 2098 } 2099 2100 ASSERT(nfs4_consistent_type(vp)); 2101 2102 /* 2103 * If the server wanted an OPEN_CONFIRM but that fails, just start 2104 * over. Presumably if there is a persistent error it will show up 2105 * when we resend the OPEN. 2106 */ 2107 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2108 bool_t retry_open = FALSE; 2109 2110 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2111 cred_otw, is_recov, &retry_open, 2112 oop, FALSE, ep, NULL); 2113 if (ep->error || ep->stat) { 2114 nfs4args_copen_free(open_args); 2115 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2116 nfs4_end_open_seqid_sync(oop); 2117 open_owner_rele(oop); 2118 oop = NULL; 2119 goto top; 2120 } 2121 } 2122 2123 mutex_enter(&osp->os_sync_lock); 2124 osp->open_stateid = op_res->stateid; 2125 osp->os_delegation = 0; 2126 /* 2127 * Need to reset this bitfield for the possible case where we were 2128 * going to OTW CLOSE the file, got a non-recoverable error, and before 2129 * we could retry the CLOSE, OPENed the file again. 2130 */ 2131 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2132 osp->os_final_close = 0; 2133 osp->os_force_close = 0; 2134 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2135 osp->os_dc_openacc = open_args->share_access; 2136 mutex_exit(&osp->os_sync_lock); 2137 2138 nfs4_end_open_seqid_sync(oop); 2139 2140 /* accept delegation, if any */ 2141 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2142 2143 nfs4args_copen_free(open_args); 2144 2145 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2146 2147 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2148 2149 ASSERT(nfs4_consistent_type(vp)); 2150 2151 open_owner_rele(oop); 2152 crfree(cr); 2153 crfree(cred_otw); 2154 return; 2155 2156 kill_file: 2157 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2158 failed_reopen: 2159 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2160 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2161 (void *)osp, (void *)cr, rnode4info(rp))); 2162 mutex_enter(&osp->os_sync_lock); 2163 osp->os_failed_reopen = 1; 2164 mutex_exit(&osp->os_sync_lock); 2165 bailout: 2166 if (oop != NULL) { 2167 nfs4_end_open_seqid_sync(oop); 2168 open_owner_rele(oop); 2169 } 2170 if (cr != NULL) 2171 crfree(cr); 2172 if (cred_otw != NULL) 2173 crfree(cred_otw); 2174 } 2175 2176 /* for . and .. OPENs */ 2177 /* ARGSUSED */ 2178 static int 2179 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2180 { 2181 rnode4_t *rp; 2182 nfs4_ga_res_t gar; 2183 2184 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2185 2186 /* 2187 * If close-to-open consistency checking is turned off or 2188 * if there is no cached data, we can avoid 2189 * the over the wire getattr. Otherwise, force a 2190 * call to the server to get fresh attributes and to 2191 * check caches. This is required for close-to-open 2192 * consistency. 2193 */ 2194 rp = VTOR4(*vpp); 2195 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2196 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2197 return (0); 2198 2199 gar.n4g_va.va_mask = AT_ALL; 2200 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2201 } 2202 2203 /* 2204 * CLOSE a file 2205 */ 2206 static int 2207 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 2208 { 2209 rnode4_t *rp; 2210 int error = 0; 2211 int r_error = 0; 2212 int n4error = 0; 2213 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2214 2215 /* 2216 * Remove client state for this (lockowner, file) pair. 2217 * Issue otw v4 call to have the server do the same. 2218 */ 2219 2220 rp = VTOR4(vp); 2221 2222 /* 2223 * zone_enter(2) prevents processes from changing zones with NFS files 2224 * open; if we happen to get here from the wrong zone we can't do 2225 * anything over the wire. 2226 */ 2227 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2228 /* 2229 * We could attempt to clean up locks, except we're sure 2230 * that the current process didn't acquire any locks on 2231 * the file: any attempt to lock a file belong to another zone 2232 * will fail, and one can't lock an NFS file and then change 2233 * zones, as that fails too. 2234 * 2235 * Returning an error here is the sane thing to do. A 2236 * subsequent call to VN_RELE() which translates to a 2237 * nfs4_inactive() will clean up state: if the zone of the 2238 * vnode's origin is still alive and kicking, the inactive 2239 * thread will handle the request (from the correct zone), and 2240 * everything (minus the OTW close call) should be OK. If the 2241 * zone is going away nfs4_async_inactive() will throw away 2242 * delegations, open streams and cached pages inline. 2243 */ 2244 return (EIO); 2245 } 2246 2247 /* 2248 * If we are using local locking for this filesystem, then 2249 * release all of the SYSV style record locks. Otherwise, 2250 * we are doing network locking and we need to release all 2251 * of the network locks. All of the locks held by this 2252 * process on this file are released no matter what the 2253 * incoming reference count is. 2254 */ 2255 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2256 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2257 cleanshares(vp, ttoproc(curthread)->p_pid); 2258 } else 2259 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2260 2261 if (e.error) 2262 return (e.error); 2263 2264 if (count > 1) 2265 return (0); 2266 2267 /* 2268 * If the file has been `unlinked', then purge the 2269 * DNLC so that this vnode will get reycled quicker 2270 * and the .nfs* file on the server will get removed. 2271 */ 2272 if (rp->r_unldvp != NULL) 2273 dnlc_purge_vp(vp); 2274 2275 /* 2276 * If the file was open for write and there are pages, 2277 * do a synchronous flush and commit of all of the 2278 * dirty and uncommitted pages. 2279 */ 2280 ASSERT(!e.error); 2281 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2282 error = nfs4_putpage_commit(vp, 0, 0, cr); 2283 2284 mutex_enter(&rp->r_statelock); 2285 r_error = rp->r_error; 2286 rp->r_error = 0; 2287 mutex_exit(&rp->r_statelock); 2288 2289 /* 2290 * If this file type is one for which no explicit 'open' was 2291 * done, then bail now (ie. no need for protocol 'close'). If 2292 * there was an error w/the vm subsystem, return _that_ error, 2293 * otherwise, return any errors that may've been reported via 2294 * the rnode. 2295 */ 2296 if (vp->v_type != VREG) 2297 return (error ? error : r_error); 2298 2299 /* 2300 * The sync putpage commit may have failed above, but since 2301 * we're working w/a regular file, we need to do the protocol 2302 * 'close' (nfs4close_one will figure out if an otw close is 2303 * needed or not). Report any errors _after_ doing the protocol 2304 * 'close'. 2305 */ 2306 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2307 n4error = e.error ? e.error : geterrno4(e.stat); 2308 2309 /* 2310 * Error reporting prio (Hi -> Lo) 2311 * 2312 * i) nfs4_putpage_commit (error) 2313 * ii) rnode's (r_error) 2314 * iii) nfs4close_one (n4error) 2315 */ 2316 return (error ? error : (r_error ? r_error : n4error)); 2317 } 2318 2319 /* 2320 * Initialize *lost_rqstp. 2321 */ 2322 2323 static void 2324 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2325 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2326 vnode_t *vp) 2327 { 2328 if (error != ETIMEDOUT && error != EINTR && 2329 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2330 lost_rqstp->lr_op = 0; 2331 return; 2332 } 2333 2334 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2335 "nfs4close_save_lost_rqst: error %d", error)); 2336 2337 lost_rqstp->lr_op = OP_CLOSE; 2338 /* 2339 * The vp is held and rele'd via the recovery code. 2340 * See nfs4_save_lost_rqst. 2341 */ 2342 lost_rqstp->lr_vp = vp; 2343 lost_rqstp->lr_dvp = NULL; 2344 lost_rqstp->lr_oop = oop; 2345 lost_rqstp->lr_osp = osp; 2346 ASSERT(osp != NULL); 2347 ASSERT(mutex_owned(&osp->os_sync_lock)); 2348 osp->os_pending_close = 1; 2349 lost_rqstp->lr_lop = NULL; 2350 lost_rqstp->lr_cr = cr; 2351 lost_rqstp->lr_flk = NULL; 2352 lost_rqstp->lr_putfirst = FALSE; 2353 } 2354 2355 /* 2356 * Assumes you already have the open seqid sync grabbed as well as the 2357 * 'os_sync_lock'. Note: this will release the open seqid sync and 2358 * 'os_sync_lock' if client recovery starts. Calling functions have to 2359 * be prepared to handle this. 2360 * 2361 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2362 * was needed and was started, and that the calling function should retry 2363 * this function; otherwise it is returned as 0. 2364 * 2365 * Errors are returned via the nfs4_error_t parameter. 2366 */ 2367 static void 2368 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2369 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2370 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2371 { 2372 COMPOUND4args_clnt args; 2373 COMPOUND4res_clnt res; 2374 CLOSE4args *close_args; 2375 nfs_resop4 *resop; 2376 nfs_argop4 argop[3]; 2377 int doqueue = 1; 2378 mntinfo4_t *mi; 2379 seqid4 seqid; 2380 vnode_t *vp; 2381 bool_t needrecov = FALSE; 2382 nfs4_lost_rqst_t lost_rqst; 2383 hrtime_t t; 2384 2385 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2386 2387 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2388 2389 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2390 2391 /* Only set this to 1 if recovery is started */ 2392 *recov = 0; 2393 2394 /* do the OTW call to close the file */ 2395 2396 if (close_type == CLOSE_RESEND) 2397 args.ctag = TAG_CLOSE_LOST; 2398 else if (close_type == CLOSE_AFTER_RESEND) 2399 args.ctag = TAG_CLOSE_UNDO; 2400 else 2401 args.ctag = TAG_CLOSE; 2402 2403 args.array_len = 3; 2404 args.array = argop; 2405 2406 vp = RTOV4(rp); 2407 2408 mi = VTOMI4(vp); 2409 2410 /* putfh target fh */ 2411 argop[0].argop = OP_CPUTFH; 2412 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2413 2414 argop[1].argop = OP_GETATTR; 2415 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2416 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2417 2418 argop[2].argop = OP_CLOSE; 2419 close_args = &argop[2].nfs_argop4_u.opclose; 2420 2421 seqid = nfs4_get_open_seqid(oop) + 1; 2422 2423 close_args->seqid = seqid; 2424 close_args->open_stateid = osp->open_stateid; 2425 2426 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2427 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2428 rnode4info(rp))); 2429 2430 t = gethrtime(); 2431 2432 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2433 2434 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2435 nfs4_set_open_seqid(seqid, oop, args.ctag); 2436 } 2437 2438 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2439 if (ep->error && !needrecov) { 2440 /* 2441 * if there was an error and no recovery is to be done 2442 * then then set up the file to flush its cache if 2443 * needed for the next caller. 2444 */ 2445 mutex_enter(&rp->r_statelock); 2446 PURGE_ATTRCACHE4_LOCKED(rp); 2447 rp->r_flags &= ~R4WRITEMODIFIED; 2448 mutex_exit(&rp->r_statelock); 2449 return; 2450 } 2451 2452 if (needrecov) { 2453 bool_t abort; 2454 nfs4_bseqid_entry_t *bsep = NULL; 2455 2456 if (close_type != CLOSE_RESEND) 2457 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2458 osp, cred_otw, vp); 2459 2460 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2461 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2462 0, args.ctag, close_args->seqid); 2463 2464 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2465 "nfs4close_otw: initiating recovery. error %d " 2466 "res.status %d", ep->error, res.status)); 2467 2468 /* 2469 * Drop the 'os_sync_lock' here so we don't hit 2470 * a potential recursive mutex_enter via an 2471 * 'open_stream_hold()'. 2472 */ 2473 mutex_exit(&osp->os_sync_lock); 2474 *have_sync_lockp = 0; 2475 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2476 (close_type != CLOSE_RESEND && 2477 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2478 OP_CLOSE, bsep); 2479 2480 /* drop open seq sync, and let the calling function regrab it */ 2481 nfs4_end_open_seqid_sync(oop); 2482 *did_start_seqid_syncp = 0; 2483 2484 if (bsep) 2485 kmem_free(bsep, sizeof (*bsep)); 2486 /* 2487 * For signals, the caller wants to quit, so don't say to 2488 * retry. For forced unmount, if it's a user thread, it 2489 * wants to quit. If it's a recovery thread, the retry 2490 * will happen higher-up on the call stack. Either way, 2491 * don't say to retry. 2492 */ 2493 if (abort == FALSE && ep->error != EINTR && 2494 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2495 close_type != CLOSE_RESEND && 2496 close_type != CLOSE_AFTER_RESEND) 2497 *recov = 1; 2498 else 2499 *recov = 0; 2500 2501 if (!ep->error) 2502 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2503 return; 2504 } 2505 2506 if (res.status) { 2507 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2508 return; 2509 } 2510 2511 mutex_enter(&rp->r_statev4_lock); 2512 rp->created_v4 = 0; 2513 mutex_exit(&rp->r_statev4_lock); 2514 2515 resop = &res.array[2]; 2516 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2517 osp->os_valid = 0; 2518 2519 /* 2520 * This removes the reference obtained at OPEN; ie, when the 2521 * open stream structure was created. 2522 * 2523 * We don't have to worry about calling 'open_stream_rele' 2524 * since we our currently holding a reference to the open 2525 * stream which means the count cannot go to 0 with this 2526 * decrement. 2527 */ 2528 ASSERT(osp->os_ref_count >= 2); 2529 osp->os_ref_count--; 2530 2531 if (!ep->error) 2532 nfs4_attr_cache(vp, 2533 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2534 t, cred_otw, TRUE, NULL); 2535 2536 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2537 " returning %d", ep->error)); 2538 2539 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2540 } 2541 2542 /* ARGSUSED */ 2543 static int 2544 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2545 caller_context_t *ct) 2546 { 2547 rnode4_t *rp; 2548 u_offset_t off; 2549 offset_t diff; 2550 uint_t on; 2551 uint_t n; 2552 caddr_t base; 2553 uint_t flags; 2554 int error; 2555 mntinfo4_t *mi; 2556 2557 rp = VTOR4(vp); 2558 2559 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2560 2561 if (IS_SHADOW(vp, rp)) 2562 vp = RTOV4(rp); 2563 2564 if (vp->v_type != VREG) 2565 return (EISDIR); 2566 2567 mi = VTOMI4(vp); 2568 2569 if (nfs_zone() != mi->mi_zone) 2570 return (EIO); 2571 2572 if (uiop->uio_resid == 0) 2573 return (0); 2574 2575 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2576 return (EINVAL); 2577 2578 mutex_enter(&rp->r_statelock); 2579 if (rp->r_flags & R4RECOVERRP) 2580 error = (rp->r_error ? rp->r_error : EIO); 2581 else 2582 error = 0; 2583 mutex_exit(&rp->r_statelock); 2584 if (error) 2585 return (error); 2586 2587 /* 2588 * Bypass VM if caching has been disabled (e.g., locking) or if 2589 * using client-side direct I/O and the file is not mmap'd and 2590 * there are no cached pages. 2591 */ 2592 if ((vp->v_flag & VNOCACHE) || 2593 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2594 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2595 size_t resid = 0; 2596 2597 return (nfs4read(vp, NULL, uiop->uio_loffset, 2598 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2599 } 2600 2601 error = 0; 2602 2603 do { 2604 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2605 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2606 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2607 2608 if (error = nfs4_validate_caches(vp, cr)) 2609 break; 2610 2611 mutex_enter(&rp->r_statelock); 2612 diff = rp->r_size - uiop->uio_loffset; 2613 mutex_exit(&rp->r_statelock); 2614 if (diff <= 0) 2615 break; 2616 if (diff < n) 2617 n = (uint_t)diff; 2618 2619 if (vpm_enable) { 2620 /* 2621 * Copy data. 2622 */ 2623 error = vpm_data_copy(vp, off + on, n, uiop, 2624 1, NULL, 0, S_READ); 2625 2626 } else { 2627 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2628 S_READ); 2629 2630 error = uiomove(base + on, n, UIO_READ, uiop); 2631 } 2632 2633 if (!error) { 2634 /* 2635 * If read a whole block or read to eof, 2636 * won't need this buffer again soon. 2637 */ 2638 mutex_enter(&rp->r_statelock); 2639 if (n + on == MAXBSIZE || 2640 uiop->uio_loffset == rp->r_size) 2641 flags = SM_DONTNEED; 2642 else 2643 flags = 0; 2644 mutex_exit(&rp->r_statelock); 2645 if (vpm_enable) { 2646 error = vpm_sync_pages(vp, off, n, flags); 2647 } else { 2648 error = segmap_release(segkmap, base, flags); 2649 } 2650 } else { 2651 if (vpm_enable) { 2652 (void) vpm_sync_pages(vp, off, n, 0); 2653 } else { 2654 (void) segmap_release(segkmap, base, 0); 2655 } 2656 } 2657 } while (!error && uiop->uio_resid > 0); 2658 2659 return (error); 2660 } 2661 2662 /* ARGSUSED */ 2663 static int 2664 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2665 caller_context_t *ct) 2666 { 2667 rlim64_t limit = uiop->uio_llimit; 2668 rnode4_t *rp; 2669 u_offset_t off; 2670 caddr_t base; 2671 uint_t flags; 2672 int remainder; 2673 size_t n; 2674 int on; 2675 int error; 2676 int resid; 2677 u_offset_t offset; 2678 mntinfo4_t *mi; 2679 uint_t bsize; 2680 2681 rp = VTOR4(vp); 2682 2683 if (IS_SHADOW(vp, rp)) 2684 vp = RTOV4(rp); 2685 2686 if (vp->v_type != VREG) 2687 return (EISDIR); 2688 2689 mi = VTOMI4(vp); 2690 2691 if (nfs_zone() != mi->mi_zone) 2692 return (EIO); 2693 2694 if (uiop->uio_resid == 0) 2695 return (0); 2696 2697 mutex_enter(&rp->r_statelock); 2698 if (rp->r_flags & R4RECOVERRP) 2699 error = (rp->r_error ? rp->r_error : EIO); 2700 else 2701 error = 0; 2702 mutex_exit(&rp->r_statelock); 2703 if (error) 2704 return (error); 2705 2706 if (ioflag & FAPPEND) { 2707 struct vattr va; 2708 2709 /* 2710 * Must serialize if appending. 2711 */ 2712 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2713 nfs_rw_exit(&rp->r_rwlock); 2714 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2715 INTR(vp))) 2716 return (EINTR); 2717 } 2718 2719 va.va_mask = AT_SIZE; 2720 error = nfs4getattr(vp, &va, cr); 2721 if (error) 2722 return (error); 2723 uiop->uio_loffset = va.va_size; 2724 } 2725 2726 offset = uiop->uio_loffset + uiop->uio_resid; 2727 2728 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2729 return (EINVAL); 2730 2731 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2732 limit = MAXOFFSET_T; 2733 2734 /* 2735 * Check to make sure that the process will not exceed 2736 * its limit on file size. It is okay to write up to 2737 * the limit, but not beyond. Thus, the write which 2738 * reaches the limit will be short and the next write 2739 * will return an error. 2740 */ 2741 remainder = 0; 2742 if (offset > uiop->uio_llimit) { 2743 remainder = offset - uiop->uio_llimit; 2744 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2745 if (uiop->uio_resid <= 0) { 2746 proc_t *p = ttoproc(curthread); 2747 2748 uiop->uio_resid += remainder; 2749 mutex_enter(&p->p_lock); 2750 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2751 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2752 mutex_exit(&p->p_lock); 2753 return (EFBIG); 2754 } 2755 } 2756 2757 /* update the change attribute, if we have a write delegation */ 2758 2759 mutex_enter(&rp->r_statev4_lock); 2760 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2761 rp->r_deleg_change++; 2762 2763 mutex_exit(&rp->r_statev4_lock); 2764 2765 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2766 return (EINTR); 2767 2768 /* 2769 * Bypass VM if caching has been disabled (e.g., locking) or if 2770 * using client-side direct I/O and the file is not mmap'd and 2771 * there are no cached pages. 2772 */ 2773 if ((vp->v_flag & VNOCACHE) || 2774 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2775 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2776 size_t bufsize; 2777 int count; 2778 u_offset_t org_offset; 2779 stable_how4 stab_comm; 2780 nfs4_fwrite: 2781 if (rp->r_flags & R4STALE) { 2782 resid = uiop->uio_resid; 2783 offset = uiop->uio_loffset; 2784 error = rp->r_error; 2785 goto bottom; 2786 } 2787 2788 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2789 base = kmem_alloc(bufsize, KM_SLEEP); 2790 do { 2791 if (ioflag & FDSYNC) 2792 stab_comm = DATA_SYNC4; 2793 else 2794 stab_comm = FILE_SYNC4; 2795 resid = uiop->uio_resid; 2796 offset = uiop->uio_loffset; 2797 count = MIN(uiop->uio_resid, bufsize); 2798 org_offset = uiop->uio_loffset; 2799 error = uiomove(base, count, UIO_WRITE, uiop); 2800 if (!error) { 2801 error = nfs4write(vp, base, org_offset, 2802 count, cr, &stab_comm); 2803 if (!error) { 2804 mutex_enter(&rp->r_statelock); 2805 if (rp->r_size < uiop->uio_loffset) 2806 rp->r_size = uiop->uio_loffset; 2807 mutex_exit(&rp->r_statelock); 2808 } 2809 } 2810 } while (!error && uiop->uio_resid > 0); 2811 kmem_free(base, bufsize); 2812 goto bottom; 2813 } 2814 2815 bsize = vp->v_vfsp->vfs_bsize; 2816 2817 do { 2818 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2819 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2820 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2821 2822 resid = uiop->uio_resid; 2823 offset = uiop->uio_loffset; 2824 2825 if (rp->r_flags & R4STALE) { 2826 error = rp->r_error; 2827 break; 2828 } 2829 2830 /* 2831 * Don't create dirty pages faster than they 2832 * can be cleaned so that the system doesn't 2833 * get imbalanced. If the async queue is 2834 * maxed out, then wait for it to drain before 2835 * creating more dirty pages. Also, wait for 2836 * any threads doing pagewalks in the vop_getattr 2837 * entry points so that they don't block for 2838 * long periods. 2839 */ 2840 mutex_enter(&rp->r_statelock); 2841 while ((mi->mi_max_threads != 0 && 2842 rp->r_awcount > 2 * mi->mi_max_threads) || 2843 rp->r_gcount > 0) 2844 cv_wait(&rp->r_cv, &rp->r_statelock); 2845 mutex_exit(&rp->r_statelock); 2846 2847 if (vpm_enable) { 2848 /* 2849 * It will use kpm mappings, so no need to 2850 * pass an address. 2851 */ 2852 error = writerp4(rp, NULL, n, uiop, 0); 2853 } else { 2854 if (segmap_kpm) { 2855 int pon = uiop->uio_loffset & PAGEOFFSET; 2856 size_t pn = MIN(PAGESIZE - pon, 2857 uiop->uio_resid); 2858 int pagecreate; 2859 2860 mutex_enter(&rp->r_statelock); 2861 pagecreate = (pon == 0) && (pn == PAGESIZE || 2862 uiop->uio_loffset + pn >= rp->r_size); 2863 mutex_exit(&rp->r_statelock); 2864 2865 base = segmap_getmapflt(segkmap, vp, off + on, 2866 pn, !pagecreate, S_WRITE); 2867 2868 error = writerp4(rp, base + pon, n, uiop, 2869 pagecreate); 2870 2871 } else { 2872 base = segmap_getmapflt(segkmap, vp, off + on, 2873 n, 0, S_READ); 2874 error = writerp4(rp, base + on, n, uiop, 0); 2875 } 2876 } 2877 2878 if (!error) { 2879 if (mi->mi_flags & MI4_NOAC) 2880 flags = SM_WRITE; 2881 else if ((uiop->uio_loffset % bsize) == 0 || 2882 IS_SWAPVP(vp)) { 2883 /* 2884 * Have written a whole block. 2885 * Start an asynchronous write 2886 * and mark the buffer to 2887 * indicate that it won't be 2888 * needed again soon. 2889 */ 2890 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2891 } else 2892 flags = 0; 2893 if ((ioflag & (FSYNC|FDSYNC)) || 2894 (rp->r_flags & R4OUTOFSPACE)) { 2895 flags &= ~SM_ASYNC; 2896 flags |= SM_WRITE; 2897 } 2898 if (vpm_enable) { 2899 error = vpm_sync_pages(vp, off, n, flags); 2900 } else { 2901 error = segmap_release(segkmap, base, flags); 2902 } 2903 } else { 2904 if (vpm_enable) { 2905 (void) vpm_sync_pages(vp, off, n, 0); 2906 } else { 2907 (void) segmap_release(segkmap, base, 0); 2908 } 2909 /* 2910 * In the event that we got an access error while 2911 * faulting in a page for a write-only file just 2912 * force a write. 2913 */ 2914 if (error == EACCES) 2915 goto nfs4_fwrite; 2916 } 2917 } while (!error && uiop->uio_resid > 0); 2918 2919 bottom: 2920 if (error) { 2921 uiop->uio_resid = resid + remainder; 2922 uiop->uio_loffset = offset; 2923 } else { 2924 uiop->uio_resid += remainder; 2925 2926 mutex_enter(&rp->r_statev4_lock); 2927 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 2928 gethrestime(&rp->r_attr.va_mtime); 2929 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 2930 } 2931 mutex_exit(&rp->r_statev4_lock); 2932 } 2933 2934 nfs_rw_exit(&rp->r_lkserlock); 2935 2936 return (error); 2937 } 2938 2939 /* 2940 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 2941 */ 2942 static int 2943 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 2944 int flags, cred_t *cr) 2945 { 2946 struct buf *bp; 2947 int error; 2948 page_t *savepp; 2949 uchar_t fsdata; 2950 stable_how4 stab_comm; 2951 2952 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 2953 bp = pageio_setup(pp, len, vp, flags); 2954 ASSERT(bp != NULL); 2955 2956 /* 2957 * pageio_setup should have set b_addr to 0. This 2958 * is correct since we want to do I/O on a page 2959 * boundary. bp_mapin will use this addr to calculate 2960 * an offset, and then set b_addr to the kernel virtual 2961 * address it allocated for us. 2962 */ 2963 ASSERT(bp->b_un.b_addr == 0); 2964 2965 bp->b_edev = 0; 2966 bp->b_dev = 0; 2967 bp->b_lblkno = lbtodb(off); 2968 bp->b_file = vp; 2969 bp->b_offset = (offset_t)off; 2970 bp_mapin(bp); 2971 2972 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 2973 freemem > desfree) 2974 stab_comm = UNSTABLE4; 2975 else 2976 stab_comm = FILE_SYNC4; 2977 2978 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 2979 2980 bp_mapout(bp); 2981 pageio_done(bp); 2982 2983 if (stab_comm == UNSTABLE4) 2984 fsdata = C_DELAYCOMMIT; 2985 else 2986 fsdata = C_NOCOMMIT; 2987 2988 savepp = pp; 2989 do { 2990 pp->p_fsdata = fsdata; 2991 } while ((pp = pp->p_next) != savepp); 2992 2993 return (error); 2994 } 2995 2996 /* 2997 */ 2998 static int 2999 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3000 { 3001 nfs4_open_owner_t *oop; 3002 nfs4_open_stream_t *osp; 3003 rnode4_t *rp = VTOR4(vp); 3004 mntinfo4_t *mi = VTOMI4(vp); 3005 int reopen_needed; 3006 3007 ASSERT(nfs_zone() == mi->mi_zone); 3008 3009 3010 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3011 if (!oop) 3012 return (EIO); 3013 3014 /* returns with 'os_sync_lock' held */ 3015 osp = find_open_stream(oop, rp); 3016 if (!osp) { 3017 open_owner_rele(oop); 3018 return (EIO); 3019 } 3020 3021 if (osp->os_failed_reopen) { 3022 mutex_exit(&osp->os_sync_lock); 3023 open_stream_rele(osp, rp); 3024 open_owner_rele(oop); 3025 return (EIO); 3026 } 3027 3028 /* 3029 * Determine whether a reopen is needed. If this 3030 * is a delegation open stream, then the os_delegation bit 3031 * should be set. 3032 */ 3033 3034 reopen_needed = osp->os_delegation; 3035 3036 mutex_exit(&osp->os_sync_lock); 3037 open_owner_rele(oop); 3038 3039 if (reopen_needed) { 3040 nfs4_error_zinit(ep); 3041 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3042 mutex_enter(&osp->os_sync_lock); 3043 if (ep->error || ep->stat || osp->os_failed_reopen) { 3044 mutex_exit(&osp->os_sync_lock); 3045 open_stream_rele(osp, rp); 3046 return (EIO); 3047 } 3048 mutex_exit(&osp->os_sync_lock); 3049 } 3050 open_stream_rele(osp, rp); 3051 3052 return (0); 3053 } 3054 3055 /* 3056 * Write to file. Writes to remote server in largest size 3057 * chunks that the server can handle. Write is synchronous. 3058 */ 3059 static int 3060 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3061 stable_how4 *stab_comm) 3062 { 3063 mntinfo4_t *mi; 3064 COMPOUND4args_clnt args; 3065 COMPOUND4res_clnt res; 3066 WRITE4args *wargs; 3067 WRITE4res *wres; 3068 nfs_argop4 argop[2]; 3069 nfs_resop4 *resop; 3070 int tsize; 3071 stable_how4 stable; 3072 rnode4_t *rp; 3073 int doqueue = 1; 3074 bool_t needrecov; 3075 nfs4_recov_state_t recov_state; 3076 nfs4_stateid_types_t sid_types; 3077 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3078 3079 rp = VTOR4(vp); 3080 mi = VTOMI4(vp); 3081 3082 ASSERT(nfs_zone() == mi->mi_zone); 3083 3084 stable = *stab_comm; 3085 *stab_comm = FILE_SYNC4; 3086 3087 needrecov = FALSE; 3088 recov_state.rs_flags = 0; 3089 recov_state.rs_num_retry_despite_err = 0; 3090 nfs4_init_stateid_types(&sid_types); 3091 3092 recov_retry: 3093 args.ctag = TAG_WRITE; 3094 args.array_len = 2; 3095 args.array = argop; 3096 3097 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3098 &recov_state, NULL); 3099 if (e.error) 3100 return (e.error); 3101 3102 /* 0. putfh target fh */ 3103 argop[0].argop = OP_CPUTFH; 3104 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3105 3106 /* 1. write */ 3107 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3108 3109 do { 3110 3111 wargs->offset = (offset4)offset; 3112 wargs->data_val = base; 3113 3114 if (mi->mi_io_kstats) { 3115 mutex_enter(&mi->mi_lock); 3116 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3117 mutex_exit(&mi->mi_lock); 3118 } 3119 3120 if ((vp->v_flag & VNOCACHE) || 3121 (rp->r_flags & R4DIRECTIO) || 3122 (mi->mi_flags & MI4_DIRECTIO)) 3123 tsize = MIN(mi->mi_stsize, count); 3124 else 3125 tsize = MIN(mi->mi_curwrite, count); 3126 wargs->data_len = (uint_t)tsize; 3127 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3128 3129 if (mi->mi_io_kstats) { 3130 mutex_enter(&mi->mi_lock); 3131 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3132 mutex_exit(&mi->mi_lock); 3133 } 3134 3135 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3136 if (e.error && !needrecov) { 3137 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3138 &recov_state, needrecov); 3139 return (e.error); 3140 } 3141 3142 3143 /* 3144 * Do handling of OLD_STATEID outside 3145 * of the normal recovery framework. 3146 * 3147 * If write receives a BAD stateid error while using a 3148 * delegation stateid, retry using the open stateid (if it 3149 * exists). If it doesn't have an open stateid, reopen the 3150 * file first, then retry. 3151 */ 3152 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3153 sid_types.cur_sid_type != SPEC_SID) { 3154 nfs4_save_stateid(&wargs->stateid, &sid_types); 3155 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3156 &recov_state, needrecov); 3157 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3158 goto recov_retry; 3159 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3160 sid_types.cur_sid_type == DEL_SID) { 3161 nfs4_save_stateid(&wargs->stateid, &sid_types); 3162 mutex_enter(&rp->r_statev4_lock); 3163 rp->r_deleg_return_pending = TRUE; 3164 mutex_exit(&rp->r_statev4_lock); 3165 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3166 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3167 &recov_state, needrecov); 3168 (void) xdr_free(xdr_COMPOUND4res_clnt, 3169 (caddr_t)&res); 3170 return (EIO); 3171 } 3172 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3173 &recov_state, needrecov); 3174 /* hold needed for nfs4delegreturn_thread */ 3175 VN_HOLD(vp); 3176 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3177 NFS4_DR_DISCARD), FALSE); 3178 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3179 goto recov_retry; 3180 } 3181 3182 if (needrecov) { 3183 bool_t abort; 3184 3185 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3186 "nfs4write: client got error %d, res.status %d" 3187 ", so start recovery", e.error, res.status)); 3188 3189 abort = nfs4_start_recovery(&e, 3190 VTOMI4(vp), vp, NULL, &wargs->stateid, 3191 NULL, OP_WRITE, NULL); 3192 if (!e.error) { 3193 e.error = geterrno4(res.status); 3194 (void) xdr_free(xdr_COMPOUND4res_clnt, 3195 (caddr_t)&res); 3196 } 3197 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3198 &recov_state, needrecov); 3199 if (abort == FALSE) 3200 goto recov_retry; 3201 return (e.error); 3202 } 3203 3204 if (res.status) { 3205 e.error = geterrno4(res.status); 3206 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3207 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3208 &recov_state, needrecov); 3209 return (e.error); 3210 } 3211 3212 resop = &res.array[1]; /* write res */ 3213 wres = &resop->nfs_resop4_u.opwrite; 3214 3215 if ((int)wres->count > tsize) { 3216 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3217 3218 zcmn_err(getzoneid(), CE_WARN, 3219 "nfs4write: server wrote %u, requested was %u", 3220 (int)wres->count, tsize); 3221 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3222 &recov_state, needrecov); 3223 return (EIO); 3224 } 3225 if (wres->committed == UNSTABLE4) { 3226 *stab_comm = UNSTABLE4; 3227 if (wargs->stable == DATA_SYNC4 || 3228 wargs->stable == FILE_SYNC4) { 3229 (void) xdr_free(xdr_COMPOUND4res_clnt, 3230 (caddr_t)&res); 3231 zcmn_err(getzoneid(), CE_WARN, 3232 "nfs4write: server %s did not commit " 3233 "to stable storage", 3234 rp->r_server->sv_hostname); 3235 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3236 &recov_state, needrecov); 3237 return (EIO); 3238 } 3239 } 3240 3241 tsize = (int)wres->count; 3242 count -= tsize; 3243 base += tsize; 3244 offset += tsize; 3245 if (mi->mi_io_kstats) { 3246 mutex_enter(&mi->mi_lock); 3247 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3248 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3249 tsize; 3250 mutex_exit(&mi->mi_lock); 3251 } 3252 lwp_stat_update(LWP_STAT_OUBLK, 1); 3253 mutex_enter(&rp->r_statelock); 3254 if (rp->r_flags & R4HAVEVERF) { 3255 if (rp->r_writeverf != wres->writeverf) { 3256 nfs4_set_mod(vp); 3257 rp->r_writeverf = wres->writeverf; 3258 } 3259 } else { 3260 rp->r_writeverf = wres->writeverf; 3261 rp->r_flags |= R4HAVEVERF; 3262 } 3263 PURGE_ATTRCACHE4_LOCKED(rp); 3264 rp->r_flags |= R4WRITEMODIFIED; 3265 gethrestime(&rp->r_attr.va_mtime); 3266 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3267 mutex_exit(&rp->r_statelock); 3268 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3269 } while (count); 3270 3271 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, needrecov); 3272 3273 return (e.error); 3274 } 3275 3276 /* 3277 * Read from a file. Reads data in largest chunks our interface can handle. 3278 */ 3279 static int 3280 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3281 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3282 { 3283 mntinfo4_t *mi; 3284 COMPOUND4args_clnt args; 3285 COMPOUND4res_clnt res; 3286 READ4args *rargs; 3287 nfs_argop4 argop[2]; 3288 int tsize; 3289 int doqueue; 3290 rnode4_t *rp; 3291 int data_len; 3292 bool_t is_eof; 3293 bool_t needrecov = FALSE; 3294 nfs4_recov_state_t recov_state; 3295 nfs4_stateid_types_t sid_types; 3296 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3297 3298 rp = VTOR4(vp); 3299 mi = VTOMI4(vp); 3300 doqueue = 1; 3301 3302 ASSERT(nfs_zone() == mi->mi_zone); 3303 3304 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3305 3306 args.array_len = 2; 3307 args.array = argop; 3308 3309 nfs4_init_stateid_types(&sid_types); 3310 3311 recov_state.rs_flags = 0; 3312 recov_state.rs_num_retry_despite_err = 0; 3313 3314 recov_retry: 3315 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3316 &recov_state, NULL); 3317 if (e.error) 3318 return (e.error); 3319 3320 /* putfh target fh */ 3321 argop[0].argop = OP_CPUTFH; 3322 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3323 3324 /* read */ 3325 argop[1].argop = OP_READ; 3326 rargs = &argop[1].nfs_argop4_u.opread; 3327 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3328 OP_READ, &sid_types, async); 3329 3330 do { 3331 if (mi->mi_io_kstats) { 3332 mutex_enter(&mi->mi_lock); 3333 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3334 mutex_exit(&mi->mi_lock); 3335 } 3336 3337 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3338 "nfs4read: %s call, rp %s", 3339 needrecov ? "recov" : "first", 3340 rnode4info(rp))); 3341 3342 if ((vp->v_flag & VNOCACHE) || 3343 (rp->r_flags & R4DIRECTIO) || 3344 (mi->mi_flags & MI4_DIRECTIO)) 3345 tsize = MIN(mi->mi_tsize, count); 3346 else 3347 tsize = MIN(mi->mi_curread, count); 3348 rargs->offset = (offset4)offset; 3349 rargs->count = (count4)tsize; 3350 rargs->res_data_val_alt = NULL; 3351 rargs->res_mblk = NULL; 3352 rargs->res_uiop = NULL; 3353 rargs->res_maxsize = 0; 3354 if (uiop) 3355 rargs->res_uiop = uiop; 3356 else 3357 rargs->res_data_val_alt = base; 3358 rargs->res_maxsize = tsize; 3359 3360 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3361 #ifdef DEBUG 3362 if (nfs4read_error_inject) { 3363 res.status = nfs4read_error_inject; 3364 nfs4read_error_inject = 0; 3365 } 3366 #endif 3367 3368 if (mi->mi_io_kstats) { 3369 mutex_enter(&mi->mi_lock); 3370 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3371 mutex_exit(&mi->mi_lock); 3372 } 3373 3374 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3375 if (e.error != 0 && !needrecov) { 3376 nfs4_end_fop(mi, vp, NULL, OH_READ, 3377 &recov_state, needrecov); 3378 return (e.error); 3379 } 3380 3381 /* 3382 * Do proper retry for OLD and BAD stateid errors outside 3383 * of the normal recovery framework. There are two differences 3384 * between async and sync reads. The first is that we allow 3385 * retry on BAD_STATEID for async reads, but not sync reads. 3386 * The second is that we mark the file dead for a failed 3387 * attempt with a special stateid for sync reads, but just 3388 * return EIO for async reads. 3389 * 3390 * If a sync read receives a BAD stateid error while using a 3391 * delegation stateid, retry using the open stateid (if it 3392 * exists). If it doesn't have an open stateid, reopen the 3393 * file first, then retry. 3394 */ 3395 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3396 res.status == NFS4ERR_BAD_STATEID) && async) { 3397 nfs4_end_fop(mi, vp, NULL, OH_READ, 3398 &recov_state, needrecov); 3399 if (sid_types.cur_sid_type == SPEC_SID) { 3400 (void) xdr_free(xdr_COMPOUND4res_clnt, 3401 (caddr_t)&res); 3402 return (EIO); 3403 } 3404 nfs4_save_stateid(&rargs->stateid, &sid_types); 3405 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3406 goto recov_retry; 3407 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3408 !async && sid_types.cur_sid_type != SPEC_SID) { 3409 nfs4_save_stateid(&rargs->stateid, &sid_types); 3410 nfs4_end_fop(mi, vp, NULL, OH_READ, 3411 &recov_state, needrecov); 3412 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3413 goto recov_retry; 3414 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3415 sid_types.cur_sid_type == DEL_SID) { 3416 nfs4_save_stateid(&rargs->stateid, &sid_types); 3417 mutex_enter(&rp->r_statev4_lock); 3418 rp->r_deleg_return_pending = TRUE; 3419 mutex_exit(&rp->r_statev4_lock); 3420 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3421 nfs4_end_fop(mi, vp, NULL, OH_READ, 3422 &recov_state, needrecov); 3423 (void) xdr_free(xdr_COMPOUND4res_clnt, 3424 (caddr_t)&res); 3425 return (EIO); 3426 } 3427 nfs4_end_fop(mi, vp, NULL, OH_READ, 3428 &recov_state, needrecov); 3429 /* hold needed for nfs4delegreturn_thread */ 3430 VN_HOLD(vp); 3431 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3432 NFS4_DR_DISCARD), FALSE); 3433 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3434 goto recov_retry; 3435 } 3436 if (needrecov) { 3437 bool_t abort; 3438 3439 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3440 "nfs4read: initiating recovery\n")); 3441 3442 abort = nfs4_start_recovery(&e, 3443 mi, vp, NULL, &rargs->stateid, 3444 NULL, OP_READ, NULL); 3445 nfs4_end_fop(mi, vp, NULL, OH_READ, 3446 &recov_state, needrecov); 3447 /* 3448 * Do not retry if we got OLD_STATEID using a special 3449 * stateid. This avoids looping with a broken server. 3450 */ 3451 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3452 sid_types.cur_sid_type == SPEC_SID) 3453 abort = TRUE; 3454 3455 if (abort == FALSE) { 3456 /* 3457 * Need to retry all possible stateids in 3458 * case the recovery error wasn't stateid 3459 * related or the stateids have become 3460 * stale (server reboot). 3461 */ 3462 nfs4_init_stateid_types(&sid_types); 3463 (void) xdr_free(xdr_COMPOUND4res_clnt, 3464 (caddr_t)&res); 3465 goto recov_retry; 3466 } 3467 3468 if (!e.error) { 3469 e.error = geterrno4(res.status); 3470 (void) xdr_free(xdr_COMPOUND4res_clnt, 3471 (caddr_t)&res); 3472 } 3473 return (e.error); 3474 } 3475 3476 if (res.status) { 3477 e.error = geterrno4(res.status); 3478 nfs4_end_fop(mi, vp, NULL, OH_READ, 3479 &recov_state, needrecov); 3480 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3481 return (e.error); 3482 } 3483 3484 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3485 count -= data_len; 3486 if (base) 3487 base += data_len; 3488 offset += data_len; 3489 if (mi->mi_io_kstats) { 3490 mutex_enter(&mi->mi_lock); 3491 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3492 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3493 mutex_exit(&mi->mi_lock); 3494 } 3495 lwp_stat_update(LWP_STAT_INBLK, 1); 3496 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3497 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3498 3499 } while (count && !is_eof); 3500 3501 *residp = count; 3502 3503 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3504 3505 return (e.error); 3506 } 3507 3508 /* ARGSUSED */ 3509 static int 3510 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3511 { 3512 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3513 return (EIO); 3514 switch (cmd) { 3515 case _FIODIRECTIO: 3516 return (nfs4_directio(vp, (int)arg, cr)); 3517 default: 3518 return (ENOTTY); 3519 } 3520 } 3521 3522 static int 3523 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 3524 { 3525 int error; 3526 rnode4_t *rp = VTOR4(vp); 3527 3528 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3529 return (EIO); 3530 /* 3531 * If it has been specified that the return value will 3532 * just be used as a hint, and we are only being asked 3533 * for size, fsid or rdevid, then return the client's 3534 * notion of these values without checking to make sure 3535 * that the attribute cache is up to date. 3536 * The whole point is to avoid an over the wire GETATTR 3537 * call. 3538 */ 3539 if (flags & ATTR_HINT) { 3540 if (vap->va_mask == 3541 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3542 mutex_enter(&rp->r_statelock); 3543 if (vap->va_mask | AT_SIZE) 3544 vap->va_size = rp->r_size; 3545 if (vap->va_mask | AT_FSID) 3546 vap->va_fsid = rp->r_attr.va_fsid; 3547 if (vap->va_mask | AT_RDEV) 3548 vap->va_rdev = rp->r_attr.va_rdev; 3549 mutex_exit(&rp->r_statelock); 3550 return (0); 3551 } 3552 } 3553 3554 /* 3555 * Only need to flush pages if asking for the mtime 3556 * and if there any dirty pages or any outstanding 3557 * asynchronous (write) requests for this file. 3558 */ 3559 if (vap->va_mask & AT_MTIME) { 3560 rp = VTOR4(vp); 3561 if (nfs4_has_pages(vp)) { 3562 mutex_enter(&rp->r_statev4_lock); 3563 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3564 mutex_exit(&rp->r_statev4_lock); 3565 if (rp->r_flags & R4DIRTY || 3566 rp->r_awcount > 0) { 3567 mutex_enter(&rp->r_statelock); 3568 rp->r_gcount++; 3569 mutex_exit(&rp->r_statelock); 3570 error = 3571 nfs4_putpage(vp, (u_offset_t)0, 3572 0, 0, cr); 3573 mutex_enter(&rp->r_statelock); 3574 if (error && (error == ENOSPC || 3575 error == EDQUOT)) { 3576 if (!rp->r_error) 3577 rp->r_error = error; 3578 } 3579 if (--rp->r_gcount == 0) 3580 cv_broadcast(&rp->r_cv); 3581 mutex_exit(&rp->r_statelock); 3582 } 3583 } else { 3584 mutex_exit(&rp->r_statev4_lock); 3585 } 3586 } 3587 } 3588 return (nfs4getattr(vp, vap, cr)); 3589 } 3590 3591 int 3592 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3593 { 3594 /* 3595 * If these are the only two bits cleared 3596 * on the server then return 0 (OK) else 3597 * return 1 (BAD). 3598 */ 3599 on_client &= ~(S_ISUID|S_ISGID); 3600 if (on_client == from_server) 3601 return (0); 3602 else 3603 return (1); 3604 } 3605 3606 /*ARGSUSED4*/ 3607 static int 3608 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3609 caller_context_t *ct) 3610 { 3611 if (vap->va_mask & AT_NOSET) 3612 return (EINVAL); 3613 3614 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3615 return (EIO); 3616 3617 /* 3618 * Don't call secpolicy_vnode_setattr, the client cannot 3619 * use its cached attributes to make security decisions 3620 * as the server may be faking mode bits or mapping uid/gid. 3621 * Always just let the server to the checking. 3622 * If we provide the ability to remove basic priviledges 3623 * to setattr (e.g. basic without chmod) then we will 3624 * need to add a check here before calling the server. 3625 */ 3626 3627 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3628 } 3629 3630 /* 3631 * To replace the "guarded" version 3 setattr, we use two types of compound 3632 * setattr requests: 3633 * 1. The "normal" setattr, used when the size of the file isn't being 3634 * changed - { Putfh <fh>; Setattr; Getattr }/ 3635 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3636 * with only ctime as the argument. If the server ctime differs from 3637 * what is cached on the client, the verify will fail, but we would 3638 * already have the ctime from the preceding getattr, so just set it 3639 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3640 * Setattr; Getattr }. 3641 * 3642 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3643 * this setattr and NULL if they are not. 3644 */ 3645 static int 3646 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3647 vsecattr_t *vsap) 3648 { 3649 COMPOUND4args_clnt args; 3650 COMPOUND4res_clnt res, *resp = NULL; 3651 nfs4_ga_res_t *garp = NULL; 3652 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3653 nfs_argop4 argop[5]; 3654 int verify_argop = -1; 3655 int setattr_argop = 1; 3656 nfs_resop4 *resop; 3657 vattr_t va; 3658 rnode4_t *rp; 3659 int doqueue = 1; 3660 uint_t mask = vap->va_mask; 3661 mode_t omode; 3662 vsecattr_t *vsp; 3663 timestruc_t ctime; 3664 bool_t needrecov = FALSE; 3665 nfs4_recov_state_t recov_state; 3666 nfs4_stateid_types_t sid_types; 3667 stateid4 stateid; 3668 hrtime_t t; 3669 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3670 servinfo4_t *svp; 3671 bitmap4 supp_attrs; 3672 3673 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3674 rp = VTOR4(vp); 3675 nfs4_init_stateid_types(&sid_types); 3676 3677 /* 3678 * Only need to flush pages if there are any pages and 3679 * if the file is marked as dirty in some fashion. The 3680 * file must be flushed so that we can accurately 3681 * determine the size of the file and the cached data 3682 * after the SETATTR returns. A file is considered to 3683 * be dirty if it is either marked with R4DIRTY, has 3684 * outstanding i/o's active, or is mmap'd. In this 3685 * last case, we can't tell whether there are dirty 3686 * pages, so we flush just to be sure. 3687 */ 3688 if (nfs4_has_pages(vp) && 3689 ((rp->r_flags & R4DIRTY) || 3690 rp->r_count > 0 || 3691 rp->r_mapcnt > 0)) { 3692 ASSERT(vp->v_type != VCHR); 3693 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr); 3694 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3695 mutex_enter(&rp->r_statelock); 3696 if (!rp->r_error) 3697 rp->r_error = e.error; 3698 mutex_exit(&rp->r_statelock); 3699 } 3700 } 3701 3702 if (mask & AT_SIZE) { 3703 /* 3704 * Verification setattr compound for non-deleg AT_SIZE: 3705 * { Putfh; Getattr; Verify; Setattr; Getattr } 3706 * Set ctime local here (outside the do_again label) 3707 * so that subsequent retries (after failed VERIFY) 3708 * will use ctime from GETATTR results (from failed 3709 * verify compound) as VERIFY arg. 3710 * If file has delegation, then VERIFY(time_metadata) 3711 * is of little added value, so don't bother. 3712 */ 3713 mutex_enter(&rp->r_statev4_lock); 3714 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3715 rp->r_deleg_return_pending) { 3716 numops = 5; 3717 ctime = rp->r_attr.va_ctime; 3718 } 3719 mutex_exit(&rp->r_statev4_lock); 3720 } 3721 3722 recov_state.rs_flags = 0; 3723 recov_state.rs_num_retry_despite_err = 0; 3724 3725 args.ctag = TAG_SETATTR; 3726 do_again: 3727 recov_retry: 3728 setattr_argop = numops - 2; 3729 3730 args.array = argop; 3731 args.array_len = numops; 3732 3733 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3734 if (e.error) 3735 return (e.error); 3736 3737 3738 /* putfh target fh */ 3739 argop[0].argop = OP_CPUTFH; 3740 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3741 3742 if (numops == 5) { 3743 /* 3744 * We only care about the ctime, but need to get mtime 3745 * and size for proper cache update. 3746 */ 3747 /* getattr */ 3748 argop[1].argop = OP_GETATTR; 3749 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3750 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3751 3752 /* verify - set later in loop */ 3753 verify_argop = 2; 3754 } 3755 3756 /* setattr */ 3757 svp = rp->r_server; 3758 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3759 supp_attrs = svp->sv_supp_attrs; 3760 nfs_rw_exit(&svp->sv_lock); 3761 3762 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3763 supp_attrs, &e.error, &sid_types); 3764 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3765 if (e.error) { 3766 /* req time field(s) overflow - return immediately */ 3767 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3768 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3769 opsetattr.obj_attributes); 3770 return (e.error); 3771 } 3772 omode = rp->r_attr.va_mode; 3773 3774 /* getattr */ 3775 argop[numops-1].argop = OP_GETATTR; 3776 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3777 /* 3778 * If we are setting the ACL (indicated only by vsap != NULL), request 3779 * the ACL in this getattr. The ACL returned from this getattr will be 3780 * used in updating the ACL cache. 3781 */ 3782 if (vsap != NULL) 3783 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3784 FATTR4_ACL_MASK; 3785 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3786 3787 /* 3788 * setattr iterates if the object size is set and the cached ctime 3789 * does not match the file ctime. In that case, verify the ctime first. 3790 */ 3791 3792 do { 3793 if (verify_argop != -1) { 3794 /* 3795 * Verify that the ctime match before doing setattr. 3796 */ 3797 va.va_mask = AT_CTIME; 3798 va.va_ctime = ctime; 3799 svp = rp->r_server; 3800 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3801 supp_attrs = svp->sv_supp_attrs; 3802 nfs_rw_exit(&svp->sv_lock); 3803 e.error = nfs4args_verify(&argop[verify_argop], &va, 3804 OP_VERIFY, supp_attrs); 3805 if (e.error) { 3806 /* req time field(s) overflow - return */ 3807 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3808 needrecov); 3809 break; 3810 } 3811 } 3812 3813 doqueue = 1; 3814 3815 t = gethrtime(); 3816 3817 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3818 3819 /* 3820 * Purge the access cache and ACL cache if changing either the 3821 * owner of the file, the group owner, or the mode. These may 3822 * change the access permissions of the file, so purge old 3823 * information and start over again. 3824 */ 3825 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3826 (void) nfs4_access_purge_rp(rp); 3827 if (rp->r_secattr != NULL) { 3828 mutex_enter(&rp->r_statelock); 3829 vsp = rp->r_secattr; 3830 rp->r_secattr = NULL; 3831 mutex_exit(&rp->r_statelock); 3832 if (vsp != NULL) 3833 nfs4_acl_free_cache(vsp); 3834 } 3835 } 3836 3837 /* 3838 * If res.array_len == numops, then everything succeeded, 3839 * except for possibly the final getattr. If only the 3840 * last getattr failed, give up, and don't try recovery. 3841 */ 3842 if (res.array_len == numops) { 3843 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3844 needrecov); 3845 if (! e.error) 3846 resp = &res; 3847 break; 3848 } 3849 3850 /* 3851 * if either rpc call failed or completely succeeded - done 3852 */ 3853 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3854 if (e.error) { 3855 PURGE_ATTRCACHE4(vp); 3856 if (!needrecov) { 3857 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3858 needrecov); 3859 break; 3860 } 3861 } 3862 3863 /* 3864 * Do proper retry for OLD_STATEID outside of the normal 3865 * recovery framework. 3866 */ 3867 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3868 sid_types.cur_sid_type != SPEC_SID && 3869 sid_types.cur_sid_type != NO_SID) { 3870 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3871 needrecov); 3872 nfs4_save_stateid(&stateid, &sid_types); 3873 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3874 opsetattr.obj_attributes); 3875 if (verify_argop != -1) { 3876 nfs4args_verify_free(&argop[verify_argop]); 3877 verify_argop = -1; 3878 } 3879 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3880 goto recov_retry; 3881 } 3882 3883 if (needrecov) { 3884 bool_t abort; 3885 3886 abort = nfs4_start_recovery(&e, 3887 VTOMI4(vp), vp, NULL, NULL, NULL, 3888 OP_SETATTR, NULL); 3889 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3890 needrecov); 3891 /* 3892 * Do not retry if we failed with OLD_STATEID using 3893 * a special stateid. This is done to avoid looping 3894 * with a broken server. 3895 */ 3896 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3897 (sid_types.cur_sid_type == SPEC_SID || 3898 sid_types.cur_sid_type == NO_SID)) 3899 abort = TRUE; 3900 if (!e.error) { 3901 if (res.status == NFS4ERR_BADOWNER) 3902 nfs4_log_badowner(VTOMI4(vp), 3903 OP_SETATTR); 3904 3905 e.error = geterrno4(res.status); 3906 (void) xdr_free(xdr_COMPOUND4res_clnt, 3907 (caddr_t)&res); 3908 } 3909 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3910 opsetattr.obj_attributes); 3911 if (verify_argop != -1) { 3912 nfs4args_verify_free(&argop[verify_argop]); 3913 verify_argop = -1; 3914 } 3915 if (abort == FALSE) { 3916 /* 3917 * Need to retry all possible stateids in 3918 * case the recovery error wasn't stateid 3919 * related or the stateids have become 3920 * stale (server reboot). 3921 */ 3922 nfs4_init_stateid_types(&sid_types); 3923 goto recov_retry; 3924 } 3925 return (e.error); 3926 } 3927 3928 /* 3929 * Need to call nfs4_end_op before nfs4getattr to 3930 * avoid potential nfs4_start_op deadlock. See RFE 3931 * 4777612. Calls to nfs4_invalidate_pages() and 3932 * nfs4_purge_stale_fh() might also generate over the 3933 * wire calls which my cause nfs4_start_op() deadlock. 3934 */ 3935 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3936 3937 /* 3938 * Check to update lease. 3939 */ 3940 resp = &res; 3941 if (res.status == NFS4_OK) { 3942 break; 3943 } 3944 3945 /* 3946 * Check if verify failed to see if try again 3947 */ 3948 if ((verify_argop == -1) || (res.array_len != 3)) { 3949 /* 3950 * can't continue... 3951 */ 3952 if (res.status == NFS4ERR_BADOWNER) 3953 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 3954 3955 e.error = geterrno4(res.status); 3956 } else { 3957 /* 3958 * When the verify request fails, the client ctime is 3959 * not in sync with the server. This is the same as 3960 * the version 3 "not synchronized" error, and we 3961 * handle it in a similar manner (XXX do we need to???). 3962 * Use the ctime returned in the first getattr for 3963 * the input to the next verify. 3964 * If we couldn't get the attributes, then we give up 3965 * because we can't complete the operation as required. 3966 */ 3967 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 3968 } 3969 if (e.error) { 3970 PURGE_ATTRCACHE4(vp); 3971 nfs4_purge_stale_fh(e.error, vp, cr); 3972 } else { 3973 /* 3974 * retry with a new verify value 3975 */ 3976 ctime = garp->n4g_va.va_ctime; 3977 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3978 resp = NULL; 3979 } 3980 if (!e.error) { 3981 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3982 opsetattr.obj_attributes); 3983 if (verify_argop != -1) { 3984 nfs4args_verify_free(&argop[verify_argop]); 3985 verify_argop = -1; 3986 } 3987 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3988 goto do_again; 3989 } 3990 } while (!e.error); 3991 3992 if (e.error) { 3993 /* 3994 * If we are here, rfs4call has an irrecoverable error - return 3995 */ 3996 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3997 opsetattr.obj_attributes); 3998 if (verify_argop != -1) { 3999 nfs4args_verify_free(&argop[verify_argop]); 4000 verify_argop = -1; 4001 } 4002 if (resp) 4003 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4004 return (e.error); 4005 } 4006 4007 4008 4009 /* 4010 * If changing the size of the file, invalidate 4011 * any local cached data which is no longer part 4012 * of the file. We also possibly invalidate the 4013 * last page in the file. We could use 4014 * pvn_vpzero(), but this would mark the page as 4015 * modified and require it to be written back to 4016 * the server for no particularly good reason. 4017 * This way, if we access it, then we bring it 4018 * back in. A read should be cheaper than a 4019 * write. 4020 */ 4021 if (mask & AT_SIZE) { 4022 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4023 } 4024 4025 /* either no error or one of the postop getattr failed */ 4026 4027 /* 4028 * XXX Perform a simplified version of wcc checking. Instead of 4029 * have another getattr to get pre-op, just purge cache if 4030 * any of the ops prior to and including the getattr failed. 4031 * If the getattr succeeded then update the attrcache accordingly. 4032 */ 4033 4034 garp = NULL; 4035 if (res.status == NFS4_OK) { 4036 /* 4037 * Last getattr 4038 */ 4039 resop = &res.array[numops - 1]; 4040 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4041 } 4042 /* 4043 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4044 * rather than filling it. See the function itself for details. 4045 */ 4046 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4047 if (garp != NULL) { 4048 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4049 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4050 vs_ace4_destroy(&garp->n4g_vsa); 4051 } else { 4052 if (vsap != NULL) { 4053 /* 4054 * The ACL was supposed to be set and to be 4055 * returned in the last getattr of this 4056 * compound, but for some reason the getattr 4057 * result doesn't contain the ACL. In this 4058 * case, purge the ACL cache. 4059 */ 4060 if (rp->r_secattr != NULL) { 4061 mutex_enter(&rp->r_statelock); 4062 vsp = rp->r_secattr; 4063 rp->r_secattr = NULL; 4064 mutex_exit(&rp->r_statelock); 4065 if (vsp != NULL) 4066 nfs4_acl_free_cache(vsp); 4067 } 4068 } 4069 } 4070 } 4071 4072 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4073 /* 4074 * Set the size, rather than relying on getting it updated 4075 * via a GETATTR. With delegations the client tries to 4076 * suppress GETATTR calls. 4077 */ 4078 mutex_enter(&rp->r_statelock); 4079 rp->r_size = vap->va_size; 4080 mutex_exit(&rp->r_statelock); 4081 } 4082 4083 /* 4084 * Can free up request args and res 4085 */ 4086 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4087 opsetattr.obj_attributes); 4088 if (verify_argop != -1) { 4089 nfs4args_verify_free(&argop[verify_argop]); 4090 verify_argop = -1; 4091 } 4092 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4093 4094 /* 4095 * Some servers will change the mode to clear the setuid 4096 * and setgid bits when changing the uid or gid. The 4097 * client needs to compensate appropriately. 4098 */ 4099 if (mask & (AT_UID | AT_GID)) { 4100 int terror, do_setattr; 4101 4102 do_setattr = 0; 4103 va.va_mask = AT_MODE; 4104 terror = nfs4getattr(vp, &va, cr); 4105 if (!terror && 4106 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4107 (!(mask & AT_MODE) && va.va_mode != omode))) { 4108 va.va_mask = AT_MODE; 4109 if (mask & AT_MODE) { 4110 /* 4111 * We asked the mode to be changed and what 4112 * we just got from the server in getattr is 4113 * not what we wanted it to be, so set it now. 4114 */ 4115 va.va_mode = vap->va_mode; 4116 do_setattr = 1; 4117 } else { 4118 /* 4119 * We did not ask the mode to be changed, 4120 * Check to see that the server just cleared 4121 * I_SUID and I_GUID from it. If not then 4122 * set mode to omode with UID/GID cleared. 4123 */ 4124 if (nfs4_compare_modes(va.va_mode, omode)) { 4125 omode &= ~(S_ISUID|S_ISGID); 4126 va.va_mode = omode; 4127 do_setattr = 1; 4128 } 4129 } 4130 4131 if (do_setattr) 4132 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4133 } 4134 } 4135 4136 return (e.error); 4137 } 4138 4139 /* ARGSUSED */ 4140 static int 4141 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr) 4142 { 4143 COMPOUND4args_clnt args; 4144 COMPOUND4res_clnt res; 4145 int doqueue; 4146 uint32_t acc, resacc, argacc; 4147 rnode4_t *rp; 4148 cred_t *cred, *ncr, *ncrfree = NULL; 4149 nfs4_access_type_t cacc; 4150 int num_ops; 4151 nfs_argop4 argop[3]; 4152 nfs_resop4 *resop; 4153 bool_t needrecov = FALSE, do_getattr; 4154 nfs4_recov_state_t recov_state; 4155 int rpc_error; 4156 hrtime_t t; 4157 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4158 mntinfo4_t *mi = VTOMI4(vp); 4159 4160 if (nfs_zone() != mi->mi_zone) 4161 return (EIO); 4162 4163 acc = 0; 4164 if (mode & VREAD) 4165 acc |= ACCESS4_READ; 4166 if (mode & VWRITE) { 4167 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4168 return (EROFS); 4169 if (vp->v_type == VDIR) 4170 acc |= ACCESS4_DELETE; 4171 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4172 } 4173 if (mode & VEXEC) { 4174 if (vp->v_type == VDIR) 4175 acc |= ACCESS4_LOOKUP; 4176 else 4177 acc |= ACCESS4_EXECUTE; 4178 } 4179 4180 if (VTOR4(vp)->r_acache != NULL) { 4181 e.error = nfs4_validate_caches(vp, cr); 4182 if (e.error) 4183 return (e.error); 4184 } 4185 4186 rp = VTOR4(vp); 4187 if (vp->v_type == VDIR) { 4188 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4189 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4190 } else { 4191 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4192 ACCESS4_EXECUTE; 4193 } 4194 recov_state.rs_flags = 0; 4195 recov_state.rs_num_retry_despite_err = 0; 4196 4197 cred = cr; 4198 /* 4199 * ncr and ncrfree both initially 4200 * point to the memory area returned 4201 * by crnetadjust(); 4202 * ncrfree not NULL when exiting means 4203 * that we need to release it 4204 */ 4205 ncr = crnetadjust(cred); 4206 ncrfree = ncr; 4207 4208 tryagain: 4209 cacc = nfs4_access_check(rp, acc, cred); 4210 if (cacc == NFS4_ACCESS_ALLOWED) { 4211 if (ncrfree != NULL) 4212 crfree(ncrfree); 4213 return (0); 4214 } 4215 if (cacc == NFS4_ACCESS_DENIED) { 4216 /* 4217 * If the cred can be adjusted, try again 4218 * with the new cred. 4219 */ 4220 if (ncr != NULL) { 4221 cred = ncr; 4222 ncr = NULL; 4223 goto tryagain; 4224 } 4225 if (ncrfree != NULL) 4226 crfree(ncrfree); 4227 return (EACCES); 4228 } 4229 4230 recov_retry: 4231 /* 4232 * Don't take with r_statev4_lock here. r_deleg_type could 4233 * change as soon as lock is released. Since it is an int, 4234 * there is no atomicity issue. 4235 */ 4236 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4237 num_ops = do_getattr ? 3 : 2; 4238 4239 args.ctag = TAG_ACCESS; 4240 4241 args.array_len = num_ops; 4242 args.array = argop; 4243 4244 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4245 &recov_state, NULL)) { 4246 if (ncrfree != NULL) 4247 crfree(ncrfree); 4248 return (e.error); 4249 } 4250 4251 /* putfh target fh */ 4252 argop[0].argop = OP_CPUTFH; 4253 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4254 4255 /* access */ 4256 argop[1].argop = OP_ACCESS; 4257 argop[1].nfs_argop4_u.opaccess.access = argacc; 4258 4259 /* getattr */ 4260 if (do_getattr) { 4261 argop[2].argop = OP_GETATTR; 4262 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4263 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4264 } 4265 4266 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4267 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4268 rnode4info(VTOR4(vp)))); 4269 4270 doqueue = 1; 4271 t = gethrtime(); 4272 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4273 rpc_error = e.error; 4274 4275 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4276 if (needrecov) { 4277 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4278 "nfs4_access: initiating recovery\n")); 4279 4280 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4281 NULL, OP_ACCESS, NULL) == FALSE) { 4282 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4283 &recov_state, needrecov); 4284 if (!e.error) 4285 (void) xdr_free(xdr_COMPOUND4res_clnt, 4286 (caddr_t)&res); 4287 goto recov_retry; 4288 } 4289 } 4290 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4291 4292 if (e.error) 4293 goto out; 4294 4295 if (res.status) { 4296 e.error = geterrno4(res.status); 4297 /* 4298 * This might generate over the wire calls throught 4299 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4300 * here to avoid a deadlock. 4301 */ 4302 nfs4_purge_stale_fh(e.error, vp, cr); 4303 goto out; 4304 } 4305 resop = &res.array[1]; /* access res */ 4306 4307 resacc = resop->nfs_resop4_u.opaccess.access; 4308 4309 if (do_getattr) { 4310 resop++; /* getattr res */ 4311 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4312 t, cr, FALSE, NULL); 4313 } 4314 4315 if (!e.error) { 4316 nfs4_access_cache(rp, argacc, resacc, cred); 4317 /* 4318 * we just cached results with cred; if cred is the 4319 * adjusted credentials from crnetadjust, we do not want 4320 * to release them before exiting: hence setting ncrfree 4321 * to NULL 4322 */ 4323 if (cred != cr) 4324 ncrfree = NULL; 4325 /* XXX check the supported bits too? */ 4326 if ((acc & resacc) != acc) { 4327 /* 4328 * The following code implements the semantic 4329 * that a setuid root program has *at least* the 4330 * permissions of the user that is running the 4331 * program. See rfs3call() for more portions 4332 * of the implementation of this functionality. 4333 */ 4334 /* XXX-LP */ 4335 if (ncr != NULL) { 4336 (void) xdr_free(xdr_COMPOUND4res_clnt, 4337 (caddr_t)&res); 4338 cred = ncr; 4339 ncr = NULL; 4340 goto tryagain; 4341 } 4342 e.error = EACCES; 4343 } 4344 } 4345 4346 out: 4347 if (!rpc_error) 4348 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4349 4350 if (ncrfree != NULL) 4351 crfree(ncrfree); 4352 4353 return (e.error); 4354 } 4355 4356 static int 4357 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr) 4358 { 4359 COMPOUND4args_clnt args; 4360 COMPOUND4res_clnt res; 4361 int doqueue; 4362 rnode4_t *rp; 4363 nfs_argop4 argop[3]; 4364 nfs_resop4 *resop; 4365 READLINK4res *lr_res; 4366 nfs4_ga_res_t *garp; 4367 uint_t len; 4368 char *linkdata; 4369 bool_t needrecov = FALSE; 4370 nfs4_recov_state_t recov_state; 4371 hrtime_t t; 4372 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4373 4374 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4375 return (EIO); 4376 /* 4377 * Can't readlink anything other than a symbolic link. 4378 */ 4379 if (vp->v_type != VLNK) 4380 return (EINVAL); 4381 4382 rp = VTOR4(vp); 4383 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4384 e.error = nfs4_validate_caches(vp, cr); 4385 if (e.error) 4386 return (e.error); 4387 mutex_enter(&rp->r_statelock); 4388 if (rp->r_symlink.contents != NULL) { 4389 e.error = uiomove(rp->r_symlink.contents, 4390 rp->r_symlink.len, UIO_READ, uiop); 4391 mutex_exit(&rp->r_statelock); 4392 return (e.error); 4393 } 4394 mutex_exit(&rp->r_statelock); 4395 } 4396 recov_state.rs_flags = 0; 4397 recov_state.rs_num_retry_despite_err = 0; 4398 4399 recov_retry: 4400 args.array_len = 3; 4401 args.array = argop; 4402 args.ctag = TAG_READLINK; 4403 4404 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4405 if (e.error) { 4406 return (e.error); 4407 } 4408 4409 /* 0. putfh symlink fh */ 4410 argop[0].argop = OP_CPUTFH; 4411 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4412 4413 /* 1. readlink */ 4414 argop[1].argop = OP_READLINK; 4415 4416 /* 2. getattr */ 4417 argop[2].argop = OP_GETATTR; 4418 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4419 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4420 4421 doqueue = 1; 4422 4423 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4424 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4425 rnode4info(VTOR4(vp)))); 4426 4427 t = gethrtime(); 4428 4429 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4430 4431 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4432 if (needrecov) { 4433 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4434 "nfs4_readlink: initiating recovery\n")); 4435 4436 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4437 NULL, OP_READLINK, NULL) == FALSE) { 4438 if (!e.error) 4439 (void) xdr_free(xdr_COMPOUND4res_clnt, 4440 (caddr_t)&res); 4441 4442 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4443 needrecov); 4444 goto recov_retry; 4445 } 4446 } 4447 4448 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4449 4450 if (e.error) 4451 return (e.error); 4452 4453 /* 4454 * There is an path in the code below which calls 4455 * nfs4_purge_stale_fh(), which may generate otw calls through 4456 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4457 * here to avoid nfs4_start_op() deadlock. 4458 */ 4459 4460 if (res.status && (res.array_len < args.array_len)) { 4461 /* 4462 * either Putfh or Link failed 4463 */ 4464 e.error = geterrno4(res.status); 4465 nfs4_purge_stale_fh(e.error, vp, cr); 4466 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4467 return (e.error); 4468 } 4469 4470 resop = &res.array[1]; /* readlink res */ 4471 lr_res = &resop->nfs_resop4_u.opreadlink; 4472 4473 /* 4474 * treat symlink names as data 4475 */ 4476 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4477 if (linkdata != NULL) { 4478 int uio_len = len - 1; 4479 /* len includes null byte, which we won't uiomove */ 4480 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4481 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4482 mutex_enter(&rp->r_statelock); 4483 if (rp->r_symlink.contents == NULL) { 4484 rp->r_symlink.contents = linkdata; 4485 rp->r_symlink.len = uio_len; 4486 rp->r_symlink.size = len; 4487 mutex_exit(&rp->r_statelock); 4488 } else { 4489 mutex_exit(&rp->r_statelock); 4490 kmem_free(linkdata, len); 4491 } 4492 } else { 4493 kmem_free(linkdata, len); 4494 } 4495 } 4496 if (res.status == NFS4_OK) { 4497 resop++; /* getattr res */ 4498 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4499 } 4500 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4501 4502 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4503 4504 /* 4505 * The over the wire error for attempting to readlink something 4506 * other than a symbolic link is ENXIO. However, we need to 4507 * return EINVAL instead of ENXIO, so we map it here. 4508 */ 4509 return (e.error == ENXIO ? EINVAL : e.error); 4510 } 4511 4512 /* 4513 * Flush local dirty pages to stable storage on the server. 4514 * 4515 * If FNODSYNC is specified, then there is nothing to do because 4516 * metadata changes are not cached on the client before being 4517 * sent to the server. 4518 */ 4519 static int 4520 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr) 4521 { 4522 int error; 4523 4524 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4525 return (0); 4526 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4527 return (EIO); 4528 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4529 if (!error) 4530 error = VTOR4(vp)->r_error; 4531 return (error); 4532 } 4533 4534 /* 4535 * Weirdness: if the file was removed or the target of a rename 4536 * operation while it was open, it got renamed instead. Here we 4537 * remove the renamed file. 4538 */ 4539 static void 4540 nfs4_inactive(vnode_t *vp, cred_t *cr) 4541 { 4542 rnode4_t *rp; 4543 4544 ASSERT(vp != DNLC_NO_VNODE); 4545 4546 rp = VTOR4(vp); 4547 4548 if (IS_SHADOW(vp, rp)) { 4549 sv_inactive(vp); 4550 return; 4551 } 4552 4553 /* 4554 * If this is coming from the wrong zone, we let someone in the right 4555 * zone take care of it asynchronously. We can get here due to 4556 * VN_RELE() being called from pageout() or fsflush(). This call may 4557 * potentially turn into an expensive no-op if, for instance, v_count 4558 * gets incremented in the meantime, but it's still correct. 4559 */ 4560 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4561 nfs4_async_inactive(vp, cr); 4562 return; 4563 } 4564 4565 /* 4566 * Some of the cleanup steps might require over-the-wire 4567 * operations. Since VOP_INACTIVE can get called as a result of 4568 * other over-the-wire operations (e.g., an attribute cache update 4569 * can lead to a DNLC purge), doing those steps now would lead to a 4570 * nested call to the recovery framework, which can deadlock. So 4571 * do any over-the-wire cleanups asynchronously, in a separate 4572 * thread. 4573 */ 4574 4575 mutex_enter(&rp->r_os_lock); 4576 mutex_enter(&rp->r_statelock); 4577 mutex_enter(&rp->r_statev4_lock); 4578 4579 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4580 mutex_exit(&rp->r_statev4_lock); 4581 mutex_exit(&rp->r_statelock); 4582 mutex_exit(&rp->r_os_lock); 4583 nfs4_async_inactive(vp, cr); 4584 return; 4585 } 4586 4587 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4588 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4589 mutex_exit(&rp->r_statev4_lock); 4590 mutex_exit(&rp->r_statelock); 4591 mutex_exit(&rp->r_os_lock); 4592 nfs4_async_inactive(vp, cr); 4593 return; 4594 } 4595 4596 if (rp->r_unldvp != NULL) { 4597 mutex_exit(&rp->r_statev4_lock); 4598 mutex_exit(&rp->r_statelock); 4599 mutex_exit(&rp->r_os_lock); 4600 nfs4_async_inactive(vp, cr); 4601 return; 4602 } 4603 mutex_exit(&rp->r_statev4_lock); 4604 mutex_exit(&rp->r_statelock); 4605 mutex_exit(&rp->r_os_lock); 4606 4607 rp4_addfree(rp, cr); 4608 } 4609 4610 /* 4611 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4612 * various bits of state. The caller must not refer to vp after this call. 4613 */ 4614 4615 void 4616 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4617 { 4618 rnode4_t *rp = VTOR4(vp); 4619 nfs4_recov_state_t recov_state; 4620 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4621 vnode_t *unldvp; 4622 char *unlname; 4623 cred_t *unlcred; 4624 COMPOUND4args_clnt args; 4625 COMPOUND4res_clnt res, *resp; 4626 nfs_argop4 argop[2]; 4627 int doqueue; 4628 #ifdef DEBUG 4629 char *name; 4630 #endif 4631 4632 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4633 ASSERT(!IS_SHADOW(vp, rp)); 4634 4635 #ifdef DEBUG 4636 name = fn_name(VTOSV(vp)->sv_name); 4637 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4638 "release vnode %s", name)); 4639 kmem_free(name, MAXNAMELEN); 4640 #endif 4641 4642 if (vp->v_type == VREG) { 4643 bool_t recov_failed = FALSE; 4644 4645 e.error = nfs4close_all(vp, cr); 4646 if (e.error) { 4647 /* Check to see if recovery failed */ 4648 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4649 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4650 recov_failed = TRUE; 4651 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4652 if (!recov_failed) { 4653 mutex_enter(&rp->r_statelock); 4654 if (rp->r_flags & R4RECOVERR) 4655 recov_failed = TRUE; 4656 mutex_exit(&rp->r_statelock); 4657 } 4658 if (recov_failed) { 4659 NFS4_DEBUG(nfs4_client_recov_debug, 4660 (CE_NOTE, "nfs4_inactive_otw: " 4661 "close failed (recovery failure)")); 4662 } 4663 } 4664 } 4665 4666 redo: 4667 if (rp->r_unldvp == NULL) { 4668 rp4_addfree(rp, cr); 4669 return; 4670 } 4671 4672 /* 4673 * Save the vnode pointer for the directory where the 4674 * unlinked-open file got renamed, then set it to NULL 4675 * to prevent another thread from getting here before 4676 * we're done with the remove. While we have the 4677 * statelock, make local copies of the pertinent rnode 4678 * fields. If we weren't to do this in an atomic way, the 4679 * the unl* fields could become inconsistent with respect 4680 * to each other due to a race condition between this 4681 * code and nfs_remove(). See bug report 1034328. 4682 */ 4683 mutex_enter(&rp->r_statelock); 4684 if (rp->r_unldvp == NULL) { 4685 mutex_exit(&rp->r_statelock); 4686 rp4_addfree(rp, cr); 4687 return; 4688 } 4689 4690 unldvp = rp->r_unldvp; 4691 rp->r_unldvp = NULL; 4692 unlname = rp->r_unlname; 4693 rp->r_unlname = NULL; 4694 unlcred = rp->r_unlcred; 4695 rp->r_unlcred = NULL; 4696 mutex_exit(&rp->r_statelock); 4697 4698 /* 4699 * If there are any dirty pages left, then flush 4700 * them. This is unfortunate because they just 4701 * may get thrown away during the remove operation, 4702 * but we have to do this for correctness. 4703 */ 4704 if (nfs4_has_pages(vp) && 4705 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4706 ASSERT(vp->v_type != VCHR); 4707 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 4708 if (e.error) { 4709 mutex_enter(&rp->r_statelock); 4710 if (!rp->r_error) 4711 rp->r_error = e.error; 4712 mutex_exit(&rp->r_statelock); 4713 } 4714 } 4715 4716 recov_state.rs_flags = 0; 4717 recov_state.rs_num_retry_despite_err = 0; 4718 recov_retry_remove: 4719 /* 4720 * Do the remove operation on the renamed file 4721 */ 4722 args.ctag = TAG_INACTIVE; 4723 4724 /* 4725 * Remove ops: putfh dir; remove 4726 */ 4727 args.array_len = 2; 4728 args.array = argop; 4729 4730 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4731 if (e.error) { 4732 kmem_free(unlname, MAXNAMELEN); 4733 crfree(unlcred); 4734 VN_RELE(unldvp); 4735 /* 4736 * Try again; this time around r_unldvp will be NULL, so we'll 4737 * just call rp4_addfree() and return. 4738 */ 4739 goto redo; 4740 } 4741 4742 /* putfh directory */ 4743 argop[0].argop = OP_CPUTFH; 4744 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4745 4746 /* remove */ 4747 argop[1].argop = OP_CREMOVE; 4748 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4749 4750 doqueue = 1; 4751 resp = &res; 4752 4753 #if 0 /* notyet */ 4754 /* 4755 * Can't do this yet. We may be being called from 4756 * dnlc_purge_XXX while that routine is holding a 4757 * mutex lock to the nc_rele list. The calls to 4758 * nfs3_cache_wcc_data may result in calls to 4759 * dnlc_purge_XXX. This will result in a deadlock. 4760 */ 4761 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4762 if (e.error) { 4763 PURGE_ATTRCACHE4(unldvp); 4764 resp = NULL; 4765 } else if (res.status) { 4766 e.error = geterrno4(res.status); 4767 PURGE_ATTRCACHE4(unldvp); 4768 /* 4769 * This code is inactive right now 4770 * but if made active there should 4771 * be a nfs4_end_op() call before 4772 * nfs4_purge_stale_fh to avoid start_op() 4773 * deadlock. See BugId: 4948726 4774 */ 4775 nfs4_purge_stale_fh(error, unldvp, cr); 4776 } else { 4777 nfs_resop4 *resop; 4778 REMOVE4res *rm_res; 4779 4780 resop = &res.array[1]; 4781 rm_res = &resop->nfs_resop4_u.opremove; 4782 /* 4783 * Update directory cache attribute, 4784 * readdir and dnlc caches. 4785 */ 4786 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4787 } 4788 #else 4789 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4790 4791 PURGE_ATTRCACHE4(unldvp); 4792 #endif 4793 4794 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4795 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4796 NULL, NULL, OP_REMOVE, NULL) == FALSE) { 4797 if (!e.error) 4798 (void) xdr_free(xdr_COMPOUND4res_clnt, 4799 (caddr_t)&res); 4800 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4801 &recov_state, TRUE); 4802 goto recov_retry_remove; 4803 } 4804 } 4805 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4806 4807 /* 4808 * Release stuff held for the remove 4809 */ 4810 VN_RELE(unldvp); 4811 if (!e.error && resp) 4812 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4813 4814 kmem_free(unlname, MAXNAMELEN); 4815 crfree(unlcred); 4816 goto redo; 4817 } 4818 4819 /* 4820 * Remote file system operations having to do with directory manipulation. 4821 */ 4822 /* ARGSUSED3 */ 4823 static int 4824 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4825 int flags, vnode_t *rdir, cred_t *cr) 4826 { 4827 int error; 4828 vnode_t *vp, *avp = NULL; 4829 rnode4_t *drp; 4830 4831 *vpp = NULL; 4832 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4833 return (EPERM); 4834 /* 4835 * if LOOKUP_XATTR, must replace dvp (object) with 4836 * object's attrdir before continuing with lookup 4837 */ 4838 if (flags & LOOKUP_XATTR) { 4839 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4840 if (error) 4841 return (error); 4842 4843 dvp = avp; 4844 4845 /* 4846 * If lookup is for "", just return dvp now. The attrdir 4847 * has already been activated (from nfs4lookup_xattr), and 4848 * the caller will RELE the original dvp -- not 4849 * the attrdir. So, set vpp and return. 4850 * Currently, when the LOOKUP_XATTR flag is 4851 * passed to VOP_LOOKUP, the name is always empty, and 4852 * shortcircuiting here avoids 3 unneeded lock/unlock 4853 * pairs. 4854 * 4855 * If a non-empty name was provided, then it is the 4856 * attribute name, and it will be looked up below. 4857 */ 4858 if (*nm == '\0') { 4859 *vpp = dvp; 4860 return (0); 4861 } 4862 4863 /* 4864 * The vfs layer never sends a name when asking for the 4865 * attrdir, so we should never get here (unless of course 4866 * name is passed at some time in future -- at which time 4867 * we'll blow up here). 4868 */ 4869 ASSERT(0); 4870 } 4871 4872 drp = VTOR4(dvp); 4873 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4874 return (EINTR); 4875 4876 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4877 nfs_rw_exit(&drp->r_rwlock); 4878 4879 /* 4880 * If vnode is a device, create special vnode. 4881 */ 4882 if (!error && ISVDEV((*vpp)->v_type)) { 4883 vp = *vpp; 4884 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4885 VN_RELE(vp); 4886 } 4887 4888 return (error); 4889 } 4890 4891 /* ARGSUSED */ 4892 static int 4893 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4894 { 4895 int error; 4896 rnode4_t *drp; 4897 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 4898 mntinfo4_t *mi; 4899 4900 mi = VTOMI4(dvp); 4901 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR)) 4902 return (EINVAL); 4903 4904 drp = VTOR4(dvp); 4905 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4906 return (EINTR); 4907 4908 mutex_enter(&drp->r_statelock); 4909 /* 4910 * If the server doesn't support xattrs just return EINVAL 4911 */ 4912 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 4913 mutex_exit(&drp->r_statelock); 4914 nfs_rw_exit(&drp->r_rwlock); 4915 return (EINVAL); 4916 } 4917 4918 /* 4919 * If there is a cached xattr directory entry, 4920 * use it as long as the attributes are valid. If the 4921 * attributes are not valid, take the simple approach and 4922 * free the cached value and re-fetch a new value. 4923 * 4924 * We don't negative entry cache for now, if we did we 4925 * would need to check if the file has changed on every 4926 * lookup. But xattrs don't exist very often and failing 4927 * an openattr is not much more expensive than and NVERIFY or GETATTR 4928 * so do an openattr over the wire for now. 4929 */ 4930 if (drp->r_xattr_dir != NULL) { 4931 if (ATTRCACHE4_VALID(dvp)) { 4932 VN_HOLD(drp->r_xattr_dir); 4933 *vpp = drp->r_xattr_dir; 4934 mutex_exit(&drp->r_statelock); 4935 nfs_rw_exit(&drp->r_rwlock); 4936 return (0); 4937 } 4938 VN_RELE(drp->r_xattr_dir); 4939 drp->r_xattr_dir = NULL; 4940 } 4941 mutex_exit(&drp->r_statelock); 4942 4943 error = nfs4openattr(dvp, vpp, cflag, cr); 4944 4945 nfs_rw_exit(&drp->r_rwlock); 4946 4947 return (error); 4948 } 4949 4950 static int 4951 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 4952 { 4953 int error; 4954 rnode4_t *drp; 4955 4956 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 4957 4958 /* 4959 * If lookup is for "", just return dvp. Don't need 4960 * to send it over the wire, look it up in the dnlc, 4961 * or perform any access checks. 4962 */ 4963 if (*nm == '\0') { 4964 VN_HOLD(dvp); 4965 *vpp = dvp; 4966 return (0); 4967 } 4968 4969 /* 4970 * Can't do lookups in non-directories. 4971 */ 4972 if (dvp->v_type != VDIR) 4973 return (ENOTDIR); 4974 4975 /* 4976 * If lookup is for ".", just return dvp. Don't need 4977 * to send it over the wire or look it up in the dnlc, 4978 * just need to check access. 4979 */ 4980 if (nm[0] == '.' && nm[1] == '\0') { 4981 error = nfs4_access(dvp, VEXEC, 0, cr); 4982 if (error) 4983 return (error); 4984 VN_HOLD(dvp); 4985 *vpp = dvp; 4986 return (0); 4987 } 4988 4989 drp = VTOR4(dvp); 4990 if (!(drp->r_flags & R4LOOKUP)) { 4991 mutex_enter(&drp->r_statelock); 4992 drp->r_flags |= R4LOOKUP; 4993 mutex_exit(&drp->r_statelock); 4994 } 4995 4996 *vpp = NULL; 4997 /* 4998 * Lookup this name in the DNLC. If there is no entry 4999 * lookup over the wire. 5000 */ 5001 if (!skipdnlc) 5002 *vpp = dnlc_lookup(dvp, nm); 5003 if (*vpp == NULL) { 5004 /* 5005 * We need to go over the wire to lookup the name. 5006 */ 5007 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5008 } 5009 5010 /* 5011 * We hit on the dnlc 5012 */ 5013 if (*vpp != DNLC_NO_VNODE || 5014 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5015 /* 5016 * But our attrs may not be valid. 5017 */ 5018 if (ATTRCACHE4_VALID(dvp)) { 5019 error = nfs4_waitfor_purge_complete(dvp); 5020 if (error) { 5021 VN_RELE(*vpp); 5022 *vpp = NULL; 5023 return (error); 5024 } 5025 5026 /* 5027 * If after the purge completes, check to make sure 5028 * our attrs are still valid. 5029 */ 5030 if (ATTRCACHE4_VALID(dvp)) { 5031 /* 5032 * If we waited for a purge we may have 5033 * lost our vnode so look it up again. 5034 */ 5035 VN_RELE(*vpp); 5036 *vpp = dnlc_lookup(dvp, nm); 5037 if (*vpp == NULL) 5038 return (nfs4lookupnew_otw(dvp, 5039 nm, vpp, cr)); 5040 5041 /* 5042 * The access cache should almost always hit 5043 */ 5044 error = nfs4_access(dvp, VEXEC, 0, cr); 5045 5046 if (error) { 5047 VN_RELE(*vpp); 5048 *vpp = NULL; 5049 return (error); 5050 } 5051 if (*vpp == DNLC_NO_VNODE) { 5052 VN_RELE(*vpp); 5053 *vpp = NULL; 5054 return (ENOENT); 5055 } 5056 return (0); 5057 } 5058 } 5059 } 5060 5061 ASSERT(*vpp != NULL); 5062 5063 /* 5064 * We may have gotten here we have one of the following cases: 5065 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5066 * need to validate them. 5067 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5068 * must validate. 5069 * 5070 * Go to the server and check if the directory has changed, if 5071 * it hasn't we are done and can use the dnlc entry. 5072 */ 5073 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5074 } 5075 5076 /* 5077 * Go to the server and check if the directory has changed, if 5078 * it hasn't we are done and can use the dnlc entry. If it 5079 * has changed we get a new copy of its attributes and check 5080 * the access for VEXEC, then relookup the filename and 5081 * get its filehandle and attributes. 5082 * 5083 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5084 * if the NVERIFY failed we must 5085 * purge the caches 5086 * cache new attributes (will set r_time_attr_inval) 5087 * cache new access 5088 * recheck VEXEC access 5089 * add name to dnlc, possibly negative 5090 * if LOOKUP succeeded 5091 * cache new attributes 5092 * else 5093 * set a new r_time_attr_inval for dvp 5094 * check to make sure we have access 5095 * 5096 * The vpp returned is the vnode passed in if the directory is valid, 5097 * a new vnode if successful lookup, or NULL on error. 5098 */ 5099 static int 5100 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5101 { 5102 COMPOUND4args_clnt args; 5103 COMPOUND4res_clnt res; 5104 fattr4 *ver_fattr; 5105 fattr4_change dchange; 5106 int32_t *ptr; 5107 int argoplist_size = 7 * sizeof (nfs_argop4); 5108 nfs_argop4 *argop; 5109 int doqueue; 5110 mntinfo4_t *mi; 5111 nfs4_recov_state_t recov_state; 5112 hrtime_t t; 5113 int isdotdot; 5114 vnode_t *nvp; 5115 nfs_fh4 *fhp; 5116 nfs4_sharedfh_t *sfhp; 5117 nfs4_access_type_t cacc; 5118 rnode4_t *nrp; 5119 rnode4_t *drp = VTOR4(dvp); 5120 nfs4_ga_res_t *garp = NULL; 5121 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5122 5123 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5124 ASSERT(nm != NULL); 5125 ASSERT(nm[0] != '\0'); 5126 ASSERT(dvp->v_type == VDIR); 5127 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5128 ASSERT(*vpp != NULL); 5129 5130 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5131 isdotdot = 1; 5132 args.ctag = TAG_LOOKUP_VPARENT; 5133 } else { 5134 /* 5135 * Do not allow crossing of server mount points. The 5136 * only visible entries in a SRVSTUB dir are . and .. 5137 * This code handles the non-.. case. We can't even get 5138 * this far if looking up ".". 5139 */ 5140 if (VTOR4(dvp)->r_flags & R4SRVSTUB) { 5141 VN_RELE(*vpp); 5142 *vpp = NULL; 5143 return (ENOENT); 5144 } 5145 isdotdot = 0; 5146 args.ctag = TAG_LOOKUP_VALID; 5147 } 5148 5149 mi = VTOMI4(dvp); 5150 recov_state.rs_flags = 0; 5151 recov_state.rs_num_retry_despite_err = 0; 5152 5153 nvp = NULL; 5154 5155 /* Save the original mount point security information */ 5156 (void) save_mnt_secinfo(mi->mi_curr_serv); 5157 5158 recov_retry: 5159 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5160 &recov_state, NULL); 5161 if (e.error) { 5162 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5163 VN_RELE(*vpp); 5164 *vpp = NULL; 5165 return (e.error); 5166 } 5167 5168 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5169 5170 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5171 args.array_len = 7; 5172 args.array = argop; 5173 5174 /* 0. putfh file */ 5175 argop[0].argop = OP_CPUTFH; 5176 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5177 5178 /* 1. nverify the change info */ 5179 argop[1].argop = OP_NVERIFY; 5180 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5181 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5182 ver_fattr->attrlist4 = (char *)&dchange; 5183 ptr = (int32_t *)&dchange; 5184 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5185 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5186 5187 /* 2. getattr directory */ 5188 argop[2].argop = OP_GETATTR; 5189 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5190 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5191 5192 /* 3. access directory */ 5193 argop[3].argop = OP_ACCESS; 5194 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5195 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5196 5197 /* 4. lookup name */ 5198 if (isdotdot) { 5199 argop[4].argop = OP_LOOKUPP; 5200 } else { 5201 argop[4].argop = OP_CLOOKUP; 5202 argop[4].nfs_argop4_u.opclookup.cname = nm; 5203 } 5204 5205 /* 5. resulting file handle */ 5206 argop[5].argop = OP_GETFH; 5207 5208 /* 6. resulting file attributes */ 5209 argop[6].argop = OP_GETATTR; 5210 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5211 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5212 5213 doqueue = 1; 5214 t = gethrtime(); 5215 5216 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5217 5218 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5219 /* 5220 * For WRONGSEC of a non-dotdot case, send secinfo directly 5221 * from this thread, do not go thru the recovery thread since 5222 * we need the nm information. 5223 * 5224 * Not doing dotdot case because there is no specification 5225 * for (PUTFH, SECINFO "..") yet. 5226 */ 5227 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5228 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5229 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5230 &recov_state, FALSE); 5231 } else { 5232 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5233 &recov_state, TRUE); 5234 } 5235 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5236 kmem_free(argop, argoplist_size); 5237 if (!e.error) 5238 goto recov_retry; 5239 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5240 VN_RELE(*vpp); 5241 *vpp = NULL; 5242 return (e.error); 5243 } 5244 5245 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5246 OP_LOOKUP, NULL) == FALSE) { 5247 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5248 &recov_state, TRUE); 5249 5250 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5251 kmem_free(argop, argoplist_size); 5252 goto recov_retry; 5253 } 5254 } 5255 5256 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5257 5258 if (e.error || res.array_len == 0) { 5259 /* 5260 * If e.error isn't set, then reply has no ops (or we couldn't 5261 * be here). The only legal way to reply without an op array 5262 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5263 * be in the reply for all other status values. 5264 * 5265 * For valid replies without an ops array, return ENOTSUP 5266 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5267 * return EIO -- don't trust status. 5268 */ 5269 if (e.error == 0) 5270 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5271 ENOTSUP : EIO; 5272 VN_RELE(*vpp); 5273 *vpp = NULL; 5274 kmem_free(argop, argoplist_size); 5275 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5276 return (e.error); 5277 } 5278 5279 if (res.status != NFS4ERR_SAME) { 5280 e.error = geterrno4(res.status); 5281 5282 /* 5283 * The NVERIFY "failed" so the directory has changed 5284 * First make sure PUTFH succeeded and NVERIFY "failed" 5285 * cleanly. 5286 */ 5287 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5288 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5289 nfs4_purge_stale_fh(e.error, dvp, cr); 5290 VN_RELE(*vpp); 5291 *vpp = NULL; 5292 goto exit; 5293 } 5294 5295 /* 5296 * We know the NVERIFY "failed" so we must: 5297 * purge the caches (access and indirectly dnlc if needed) 5298 */ 5299 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5300 5301 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5302 nfs4_purge_stale_fh(e.error, dvp, cr); 5303 VN_RELE(*vpp); 5304 *vpp = NULL; 5305 goto exit; 5306 } 5307 5308 /* 5309 * Install new cached attributes for the directory 5310 */ 5311 nfs4_attr_cache(dvp, 5312 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5313 t, cr, FALSE, NULL); 5314 5315 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5316 nfs4_purge_stale_fh(e.error, dvp, cr); 5317 VN_RELE(*vpp); 5318 *vpp = NULL; 5319 e.error = geterrno4(res.status); 5320 goto exit; 5321 } 5322 5323 /* 5324 * Now we know the directory is valid, 5325 * cache new directory access 5326 */ 5327 nfs4_access_cache(drp, 5328 args.array[3].nfs_argop4_u.opaccess.access, 5329 res.array[3].nfs_resop4_u.opaccess.access, cr); 5330 5331 /* 5332 * recheck VEXEC access 5333 */ 5334 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5335 if (cacc != NFS4_ACCESS_ALLOWED) { 5336 /* 5337 * Directory permissions might have been revoked 5338 */ 5339 if (cacc == NFS4_ACCESS_DENIED) { 5340 e.error = EACCES; 5341 VN_RELE(*vpp); 5342 *vpp = NULL; 5343 goto exit; 5344 } 5345 5346 /* 5347 * Somehow we must not have asked for enough 5348 * so try a singleton ACCESS, should never happen. 5349 */ 5350 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5351 if (e.error) { 5352 VN_RELE(*vpp); 5353 *vpp = NULL; 5354 goto exit; 5355 } 5356 } 5357 5358 e.error = geterrno4(res.status); 5359 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5360 /* 5361 * The lookup failed, probably no entry 5362 */ 5363 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5364 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5365 } else { 5366 /* 5367 * Might be some other error, so remove 5368 * the dnlc entry to make sure we start all 5369 * over again, next time. 5370 */ 5371 dnlc_remove(dvp, nm); 5372 } 5373 VN_RELE(*vpp); 5374 *vpp = NULL; 5375 goto exit; 5376 } 5377 5378 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5379 /* 5380 * The file exists but we can't get its fh for 5381 * some unknown reason. Remove it from the dnlc 5382 * and error out to be safe. 5383 */ 5384 dnlc_remove(dvp, nm); 5385 VN_RELE(*vpp); 5386 *vpp = NULL; 5387 goto exit; 5388 } 5389 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5390 if (fhp->nfs_fh4_len == 0) { 5391 /* 5392 * The file exists but a bogus fh 5393 * some unknown reason. Remove it from the dnlc 5394 * and error out to be safe. 5395 */ 5396 e.error = ENOENT; 5397 dnlc_remove(dvp, nm); 5398 VN_RELE(*vpp); 5399 *vpp = NULL; 5400 goto exit; 5401 } 5402 sfhp = sfh4_get(fhp, mi); 5403 5404 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5405 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5406 5407 /* 5408 * Make the new rnode 5409 */ 5410 if (isdotdot) { 5411 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5412 if (e.error) { 5413 sfh4_rele(&sfhp); 5414 VN_RELE(*vpp); 5415 *vpp = NULL; 5416 goto exit; 5417 } 5418 /* 5419 * XXX if nfs4_make_dotdot uses an existing rnode 5420 * XXX it doesn't update the attributes. 5421 * XXX for now just save them again to save an OTW 5422 */ 5423 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5424 } else { 5425 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5426 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5427 /* 5428 * If v_type == VNON, then garp was NULL because 5429 * the last op in the compound failed and makenfs4node 5430 * could not find the vnode for sfhp. It created 5431 * a new vnode, so we have nothing to purge here. 5432 */ 5433 if (nvp->v_type == VNON) { 5434 vattr_t vattr; 5435 5436 vattr.va_mask = AT_TYPE; 5437 /* 5438 * N.B. We've already called nfs4_end_fop above. 5439 */ 5440 e.error = nfs4getattr(nvp, &vattr, cr); 5441 if (e.error) { 5442 sfh4_rele(&sfhp); 5443 VN_RELE(*vpp); 5444 *vpp = NULL; 5445 VN_RELE(nvp); 5446 goto exit; 5447 } 5448 nvp->v_type = vattr.va_type; 5449 } 5450 } 5451 sfh4_rele(&sfhp); 5452 5453 nrp = VTOR4(nvp); 5454 mutex_enter(&nrp->r_statev4_lock); 5455 if (!nrp->created_v4) { 5456 mutex_exit(&nrp->r_statev4_lock); 5457 dnlc_update(dvp, nm, nvp); 5458 } else 5459 mutex_exit(&nrp->r_statev4_lock); 5460 5461 VN_RELE(*vpp); 5462 *vpp = nvp; 5463 } else { 5464 hrtime_t now; 5465 hrtime_t delta = 0; 5466 5467 e.error = 0; 5468 5469 /* 5470 * Because the NVERIFY "succeeded" we know that the 5471 * directory attributes are still valid 5472 * so update r_time_attr_inval 5473 */ 5474 now = gethrtime(); 5475 mutex_enter(&drp->r_statelock); 5476 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5477 delta = now - drp->r_time_attr_saved; 5478 if (delta < mi->mi_acdirmin) 5479 delta = mi->mi_acdirmin; 5480 else if (delta > mi->mi_acdirmax) 5481 delta = mi->mi_acdirmax; 5482 } 5483 drp->r_time_attr_inval = now + delta; 5484 mutex_exit(&drp->r_statelock); 5485 dnlc_update(dvp, nm, *vpp); 5486 5487 /* 5488 * Even though we have a valid directory attr cache 5489 * and dnlc entry, we may not have access. 5490 * This should almost always hit the cache. 5491 */ 5492 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5493 if (e.error) { 5494 VN_RELE(*vpp); 5495 *vpp = NULL; 5496 } 5497 5498 if (*vpp == DNLC_NO_VNODE) { 5499 VN_RELE(*vpp); 5500 *vpp = NULL; 5501 e.error = ENOENT; 5502 } 5503 } 5504 5505 exit: 5506 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5507 kmem_free(argop, argoplist_size); 5508 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5509 return (e.error); 5510 } 5511 5512 /* 5513 * We need to go over the wire to lookup the name, but 5514 * while we are there verify the directory has not 5515 * changed but if it has, get new attributes and check access 5516 * 5517 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5518 * NVERIFY GETATTR ACCESS 5519 * 5520 * With the results: 5521 * if the NVERIFY failed we must purge the caches, add new attributes, 5522 * and cache new access. 5523 * set a new r_time_attr_inval 5524 * add name to dnlc, possibly negative 5525 * if LOOKUP succeeded 5526 * cache new attributes 5527 */ 5528 static int 5529 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5530 { 5531 COMPOUND4args_clnt args; 5532 COMPOUND4res_clnt res; 5533 fattr4 *ver_fattr; 5534 fattr4_change dchange; 5535 int32_t *ptr; 5536 nfs4_ga_res_t *garp = NULL; 5537 int argoplist_size = 9 * sizeof (nfs_argop4); 5538 nfs_argop4 *argop; 5539 int doqueue; 5540 mntinfo4_t *mi; 5541 nfs4_recov_state_t recov_state; 5542 hrtime_t t; 5543 int isdotdot; 5544 vnode_t *nvp; 5545 nfs_fh4 *fhp; 5546 nfs4_sharedfh_t *sfhp; 5547 nfs4_access_type_t cacc; 5548 rnode4_t *nrp; 5549 rnode4_t *drp = VTOR4(dvp); 5550 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5551 5552 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5553 ASSERT(nm != NULL); 5554 ASSERT(nm[0] != '\0'); 5555 ASSERT(dvp->v_type == VDIR); 5556 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5557 ASSERT(*vpp == NULL); 5558 5559 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5560 isdotdot = 1; 5561 args.ctag = TAG_LOOKUP_PARENT; 5562 } else { 5563 /* 5564 * Do not allow crossing of server mount points. The 5565 * only visible entries in a SRVSTUB dir are . and .. 5566 * This code handles the non-.. case. We can't even get 5567 * this far if looking up ".". 5568 */ 5569 if (VTOR4(dvp)->r_flags & R4SRVSTUB) 5570 return (ENOENT); 5571 5572 isdotdot = 0; 5573 args.ctag = TAG_LOOKUP; 5574 } 5575 5576 mi = VTOMI4(dvp); 5577 recov_state.rs_flags = 0; 5578 recov_state.rs_num_retry_despite_err = 0; 5579 5580 nvp = NULL; 5581 5582 /* Save the original mount point security information */ 5583 (void) save_mnt_secinfo(mi->mi_curr_serv); 5584 5585 recov_retry: 5586 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5587 &recov_state, NULL); 5588 if (e.error) { 5589 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5590 return (e.error); 5591 } 5592 5593 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5594 5595 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5596 args.array_len = 9; 5597 args.array = argop; 5598 5599 /* 0. putfh file */ 5600 argop[0].argop = OP_CPUTFH; 5601 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5602 5603 /* 1. savefh for the nverify */ 5604 argop[1].argop = OP_SAVEFH; 5605 5606 /* 2. lookup name */ 5607 if (isdotdot) { 5608 argop[2].argop = OP_LOOKUPP; 5609 } else { 5610 argop[2].argop = OP_CLOOKUP; 5611 argop[2].nfs_argop4_u.opclookup.cname = nm; 5612 } 5613 5614 /* 3. resulting file handle */ 5615 argop[3].argop = OP_GETFH; 5616 5617 /* 4. resulting file attributes */ 5618 argop[4].argop = OP_GETATTR; 5619 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5620 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5621 5622 /* 5. restorefh back the directory for the nverify */ 5623 argop[5].argop = OP_RESTOREFH; 5624 5625 /* 6. nverify the change info */ 5626 argop[6].argop = OP_NVERIFY; 5627 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5628 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5629 ver_fattr->attrlist4 = (char *)&dchange; 5630 ptr = (int32_t *)&dchange; 5631 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5632 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5633 5634 /* 7. getattr directory */ 5635 argop[7].argop = OP_GETATTR; 5636 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5637 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5638 5639 /* 8. access directory */ 5640 argop[8].argop = OP_ACCESS; 5641 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5642 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5643 5644 doqueue = 1; 5645 t = gethrtime(); 5646 5647 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5648 5649 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5650 /* 5651 * For WRONGSEC of a non-dotdot case, send secinfo directly 5652 * from this thread, do not go thru the recovery thread since 5653 * we need the nm information. 5654 * 5655 * Not doing dotdot case because there is no specification 5656 * for (PUTFH, SECINFO "..") yet. 5657 */ 5658 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5659 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5660 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5661 &recov_state, FALSE); 5662 } else { 5663 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5664 &recov_state, TRUE); 5665 } 5666 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5667 kmem_free(argop, argoplist_size); 5668 if (!e.error) 5669 goto recov_retry; 5670 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5671 return (e.error); 5672 } 5673 5674 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5675 OP_LOOKUP, NULL) == FALSE) { 5676 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5677 &recov_state, TRUE); 5678 5679 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5680 kmem_free(argop, argoplist_size); 5681 goto recov_retry; 5682 } 5683 } 5684 5685 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5686 5687 if (e.error || res.array_len == 0) { 5688 /* 5689 * If e.error isn't set, then reply has no ops (or we couldn't 5690 * be here). The only legal way to reply without an op array 5691 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5692 * be in the reply for all other status values. 5693 * 5694 * For valid replies without an ops array, return ENOTSUP 5695 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5696 * return EIO -- don't trust status. 5697 */ 5698 if (e.error == 0) 5699 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5700 ENOTSUP : EIO; 5701 5702 kmem_free(argop, argoplist_size); 5703 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5704 return (e.error); 5705 } 5706 5707 e.error = geterrno4(res.status); 5708 5709 /* 5710 * The PUTFH and SAVEFH may have failed. 5711 */ 5712 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5713 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5714 nfs4_purge_stale_fh(e.error, dvp, cr); 5715 goto exit; 5716 } 5717 5718 /* 5719 * Check if the file exists, if it does delay entering 5720 * into the dnlc until after we update the directory 5721 * attributes so we don't cause it to get purged immediately. 5722 */ 5723 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5724 /* 5725 * The lookup failed, probably no entry 5726 */ 5727 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5728 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5729 } 5730 goto exit; 5731 } 5732 5733 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5734 /* 5735 * The file exists but we can't get its fh for 5736 * some unknown reason. Error out to be safe. 5737 */ 5738 goto exit; 5739 } 5740 5741 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5742 if (fhp->nfs_fh4_len == 0) { 5743 /* 5744 * The file exists but a bogus fh 5745 * some unknown reason. Error out to be safe. 5746 */ 5747 e.error = EIO; 5748 goto exit; 5749 } 5750 sfhp = sfh4_get(fhp, mi); 5751 5752 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5753 sfh4_rele(&sfhp); 5754 e.error = EIO; 5755 goto exit; 5756 } 5757 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5758 5759 /* 5760 * The RESTOREFH may have failed 5761 */ 5762 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5763 sfh4_rele(&sfhp); 5764 e.error = EIO; 5765 goto exit; 5766 } 5767 5768 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5769 /* 5770 * First make sure the NVERIFY failed as we expected, 5771 * if it didn't then be conservative and error out 5772 * as we can't trust the directory. 5773 */ 5774 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5775 sfh4_rele(&sfhp); 5776 e.error = EIO; 5777 goto exit; 5778 } 5779 5780 /* 5781 * We know the NVERIFY "failed" so the directory has changed, 5782 * so we must: 5783 * purge the caches (access and indirectly dnlc if needed) 5784 */ 5785 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5786 5787 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5788 sfh4_rele(&sfhp); 5789 goto exit; 5790 } 5791 nfs4_attr_cache(dvp, 5792 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5793 t, cr, FALSE, NULL); 5794 5795 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5796 nfs4_purge_stale_fh(e.error, dvp, cr); 5797 sfh4_rele(&sfhp); 5798 e.error = geterrno4(res.status); 5799 goto exit; 5800 } 5801 5802 /* 5803 * Now we know the directory is valid, 5804 * cache new directory access 5805 */ 5806 nfs4_access_cache(drp, 5807 args.array[8].nfs_argop4_u.opaccess.access, 5808 res.array[8].nfs_resop4_u.opaccess.access, cr); 5809 5810 /* 5811 * recheck VEXEC access 5812 */ 5813 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5814 if (cacc != NFS4_ACCESS_ALLOWED) { 5815 /* 5816 * Directory permissions might have been revoked 5817 */ 5818 if (cacc == NFS4_ACCESS_DENIED) { 5819 sfh4_rele(&sfhp); 5820 e.error = EACCES; 5821 goto exit; 5822 } 5823 5824 /* 5825 * Somehow we must not have asked for enough 5826 * so try a singleton ACCESS should never happen 5827 */ 5828 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5829 if (e.error) { 5830 sfh4_rele(&sfhp); 5831 goto exit; 5832 } 5833 } 5834 5835 e.error = geterrno4(res.status); 5836 } else { 5837 hrtime_t now; 5838 hrtime_t delta = 0; 5839 5840 e.error = 0; 5841 5842 /* 5843 * Because the NVERIFY "succeeded" we know that the 5844 * directory attributes are still valid 5845 * so update r_time_attr_inval 5846 */ 5847 now = gethrtime(); 5848 mutex_enter(&drp->r_statelock); 5849 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5850 delta = now - drp->r_time_attr_saved; 5851 if (delta < mi->mi_acdirmin) 5852 delta = mi->mi_acdirmin; 5853 else if (delta > mi->mi_acdirmax) 5854 delta = mi->mi_acdirmax; 5855 } 5856 drp->r_time_attr_inval = now + delta; 5857 mutex_exit(&drp->r_statelock); 5858 5859 /* 5860 * Even though we have a valid directory attr cache, 5861 * we may not have access. 5862 * This should almost always hit the cache. 5863 */ 5864 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5865 if (e.error) { 5866 sfh4_rele(&sfhp); 5867 goto exit; 5868 } 5869 } 5870 5871 /* 5872 * Now we have successfully completed the lookup, if the 5873 * directory has changed we now have the valid attributes. 5874 * We also know we have directory access. 5875 * Create the new rnode and insert it in the dnlc. 5876 */ 5877 if (isdotdot) { 5878 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5879 if (e.error) { 5880 sfh4_rele(&sfhp); 5881 goto exit; 5882 } 5883 /* 5884 * XXX if nfs4_make_dotdot uses an existing rnode 5885 * XXX it doesn't update the attributes. 5886 * XXX for now just save them again to save an OTW 5887 */ 5888 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5889 } else { 5890 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5891 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5892 } 5893 sfh4_rele(&sfhp); 5894 5895 nrp = VTOR4(nvp); 5896 mutex_enter(&nrp->r_statev4_lock); 5897 if (!nrp->created_v4) { 5898 mutex_exit(&nrp->r_statev4_lock); 5899 dnlc_update(dvp, nm, nvp); 5900 } else 5901 mutex_exit(&nrp->r_statev4_lock); 5902 5903 *vpp = nvp; 5904 5905 exit: 5906 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5907 kmem_free(argop, argoplist_size); 5908 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5909 return (e.error); 5910 } 5911 5912 #ifdef DEBUG 5913 void 5914 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 5915 { 5916 uint_t i, len; 5917 zoneid_t zoneid = getzoneid(); 5918 char *s; 5919 5920 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 5921 for (i = 0; i < argcnt; i++) { 5922 nfs_argop4 *op = &argbase[i]; 5923 switch (op->argop) { 5924 case OP_CPUTFH: 5925 case OP_PUTFH: 5926 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 5927 break; 5928 case OP_PUTROOTFH: 5929 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 5930 break; 5931 case OP_CLOOKUP: 5932 s = op->nfs_argop4_u.opclookup.cname; 5933 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5934 break; 5935 case OP_LOOKUP: 5936 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 5937 &len, NULL); 5938 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5939 kmem_free(s, len); 5940 break; 5941 case OP_LOOKUPP: 5942 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 5943 break; 5944 case OP_GETFH: 5945 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 5946 break; 5947 case OP_GETATTR: 5948 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 5949 break; 5950 case OP_OPENATTR: 5951 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 5952 break; 5953 default: 5954 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 5955 op->argop); 5956 break; 5957 } 5958 } 5959 } 5960 #endif 5961 5962 /* 5963 * nfs4lookup_setup - constructs a multi-lookup compound request. 5964 * 5965 * Given the path "nm1/nm2/.../nmn", the following compound requests 5966 * may be created: 5967 * 5968 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 5969 * is faster, for now. 5970 * 5971 * l4_getattrs indicates the type of compound requested. 5972 * 5973 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 5974 * 5975 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 5976 * 5977 * total number of ops is n + 1. 5978 * 5979 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 5980 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 5981 * before the last component, and only get attributes 5982 * for the last component. Note that the second-to-last 5983 * pathname component is XATTR_RPATH, which does NOT go 5984 * over-the-wire as a lookup. 5985 * 5986 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 5987 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 5988 * 5989 * and total number of ops is n + 5. 5990 * 5991 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 5992 * attribute directory: create lookups plus an OPENATTR 5993 * replacing the last lookup. Note that the last pathname 5994 * component is XATTR_RPATH, which does NOT go over-the-wire 5995 * as a lookup. 5996 * 5997 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 5998 * Openattr; Getfh; Getattr } 5999 * 6000 * and total number of ops is n + 5. 6001 * 6002 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6003 * nodes too. 6004 * 6005 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6006 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6007 * 6008 * and total number of ops is 3*n + 1. 6009 * 6010 * All cases: returns the index in the arg array of the final LOOKUP op, or 6011 * -1 if no LOOKUPs were used. 6012 */ 6013 int 6014 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6015 { 6016 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6017 nfs_argop4 *argbase, *argop; 6018 int arglen, argcnt; 6019 int n = 1; /* number of components */ 6020 int nga = 1; /* number of Getattr's in request */ 6021 char c = '\0', *s, *p; 6022 int lookup_idx = -1; 6023 int argoplist_size; 6024 6025 /* set lookuparg response result to 0 */ 6026 lookupargp->resp->status = NFS4_OK; 6027 6028 /* skip leading "/" or "." e.g. ".//./" if there is */ 6029 for (; ; nm++) { 6030 if (*nm != '/' && *nm != '.') 6031 break; 6032 6033 /* ".." is counted as 1 component */ 6034 if (*nm == '.' && *(nm + 1) == '.') 6035 break; 6036 } 6037 6038 /* 6039 * Find n = number of components - nm must be null terminated 6040 * Skip "." components. 6041 */ 6042 if (*nm != '\0') { 6043 for (n = 1, s = nm; *s != '\0'; s++) { 6044 if ((*s == '/') && (*(s + 1) != '/') && 6045 (*(s + 1) != '\0') && 6046 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6047 *(s + 2) == '\0'))) 6048 n++; 6049 } 6050 } else 6051 n = 0; 6052 6053 /* 6054 * nga is number of components that need Getfh+Getattr 6055 */ 6056 switch (l4_getattrs) { 6057 case LKP4_NO_ATTRIBUTES: 6058 nga = 0; 6059 break; 6060 case LKP4_ALL_ATTRIBUTES: 6061 nga = n; 6062 /* 6063 * Always have at least 1 getfh, getattr pair 6064 */ 6065 if (nga == 0) 6066 nga++; 6067 break; 6068 case LKP4_LAST_ATTRDIR: 6069 case LKP4_LAST_NAMED_ATTR: 6070 nga = n+1; 6071 break; 6072 } 6073 6074 /* 6075 * If change to use the filehandle attr instead of getfh 6076 * the following line can be deleted. 6077 */ 6078 nga *= 2; 6079 6080 /* 6081 * calculate number of ops in request as 6082 * header + trailer + lookups + getattrs 6083 */ 6084 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6085 6086 argoplist_size = arglen * sizeof (nfs_argop4); 6087 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6088 lookupargp->argsp->array = argop; 6089 6090 argcnt = lookupargp->header_len; 6091 argop += argcnt; 6092 6093 /* 6094 * loop and create a lookup op and possibly getattr/getfh for 6095 * each component. Skip "." components. 6096 */ 6097 for (s = nm; *s != '\0'; s = p) { 6098 /* 6099 * Set up a pathname struct for each component if needed 6100 */ 6101 while (*s == '/') 6102 s++; 6103 if (*s == '\0') 6104 break; 6105 for (p = s; (*p != '/') && (*p != '\0'); p++); 6106 c = *p; 6107 *p = '\0'; 6108 6109 if (s[0] == '.' && s[1] == '\0') { 6110 *p = c; 6111 continue; 6112 } 6113 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6114 strcmp(s, XATTR_RPATH) == 0) { 6115 /* getfh XXX may not be needed in future */ 6116 argop->argop = OP_GETFH; 6117 argop++; 6118 argcnt++; 6119 6120 /* getattr */ 6121 argop->argop = OP_GETATTR; 6122 argop->nfs_argop4_u.opgetattr.attr_request = 6123 lookupargp->ga_bits; 6124 argop->nfs_argop4_u.opgetattr.mi = 6125 lookupargp->mi; 6126 argop++; 6127 argcnt++; 6128 6129 /* openattr */ 6130 argop->argop = OP_OPENATTR; 6131 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6132 strcmp(s, XATTR_RPATH) == 0) { 6133 /* openattr */ 6134 argop->argop = OP_OPENATTR; 6135 argop++; 6136 argcnt++; 6137 6138 /* getfh XXX may not be needed in future */ 6139 argop->argop = OP_GETFH; 6140 argop++; 6141 argcnt++; 6142 6143 /* getattr */ 6144 argop->argop = OP_GETATTR; 6145 argop->nfs_argop4_u.opgetattr.attr_request = 6146 lookupargp->ga_bits; 6147 argop->nfs_argop4_u.opgetattr.mi = 6148 lookupargp->mi; 6149 argop++; 6150 argcnt++; 6151 *p = c; 6152 continue; 6153 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6154 /* lookupp */ 6155 argop->argop = OP_LOOKUPP; 6156 } else { 6157 /* lookup */ 6158 argop->argop = OP_LOOKUP; 6159 (void) str_to_utf8(s, 6160 &argop->nfs_argop4_u.oplookup.objname); 6161 } 6162 lookup_idx = argcnt; 6163 argop++; 6164 argcnt++; 6165 6166 *p = c; 6167 6168 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6169 /* getfh XXX may not be needed in future */ 6170 argop->argop = OP_GETFH; 6171 argop++; 6172 argcnt++; 6173 6174 /* getattr */ 6175 argop->argop = OP_GETATTR; 6176 argop->nfs_argop4_u.opgetattr.attr_request = 6177 lookupargp->ga_bits; 6178 argop->nfs_argop4_u.opgetattr.mi = 6179 lookupargp->mi; 6180 argop++; 6181 argcnt++; 6182 } 6183 } 6184 6185 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6186 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6187 if (needgetfh) { 6188 /* stick in a post-lookup getfh */ 6189 argop->argop = OP_GETFH; 6190 argcnt++; 6191 argop++; 6192 } 6193 /* post-lookup getattr */ 6194 argop->argop = OP_GETATTR; 6195 argop->nfs_argop4_u.opgetattr.attr_request = 6196 lookupargp->ga_bits; 6197 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6198 argcnt++; 6199 } 6200 argcnt += lookupargp->trailer_len; /* actual op count */ 6201 lookupargp->argsp->array_len = argcnt; 6202 lookupargp->arglen = arglen; 6203 6204 #ifdef DEBUG 6205 if (nfs4_client_lookup_debug) 6206 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6207 #endif 6208 6209 return (lookup_idx); 6210 } 6211 6212 static int 6213 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6214 { 6215 COMPOUND4args_clnt args; 6216 COMPOUND4res_clnt res; 6217 GETFH4res *gf_res = NULL; 6218 nfs_argop4 argop[4]; 6219 nfs_resop4 *resop = NULL; 6220 nfs4_sharedfh_t *sfhp; 6221 hrtime_t t; 6222 nfs4_error_t e; 6223 6224 rnode4_t *drp; 6225 int doqueue = 1; 6226 vnode_t *vp; 6227 int needrecov = 0; 6228 nfs4_recov_state_t recov_state; 6229 6230 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6231 6232 *avp = NULL; 6233 recov_state.rs_flags = 0; 6234 recov_state.rs_num_retry_despite_err = 0; 6235 6236 recov_retry: 6237 /* COMPOUND: putfh, openattr, getfh, getattr */ 6238 args.array_len = 4; 6239 args.array = argop; 6240 args.ctag = TAG_OPENATTR; 6241 6242 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6243 if (e.error) 6244 return (e.error); 6245 6246 drp = VTOR4(dvp); 6247 6248 /* putfh */ 6249 argop[0].argop = OP_CPUTFH; 6250 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6251 6252 /* openattr */ 6253 argop[1].argop = OP_OPENATTR; 6254 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6255 6256 /* getfh */ 6257 argop[2].argop = OP_GETFH; 6258 6259 /* getattr */ 6260 argop[3].argop = OP_GETATTR; 6261 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6262 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6263 6264 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6265 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6266 rnode4info(drp))); 6267 6268 t = gethrtime(); 6269 6270 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6271 6272 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6273 if (needrecov) { 6274 bool_t abort; 6275 6276 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6277 "nfs4openattr: initiating recovery\n")); 6278 6279 abort = nfs4_start_recovery(&e, 6280 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6281 OP_OPENATTR, NULL); 6282 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6283 if (!e.error) { 6284 e.error = geterrno4(res.status); 6285 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6286 } 6287 if (abort == FALSE) 6288 goto recov_retry; 6289 return (e.error); 6290 } 6291 6292 if (e.error) { 6293 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6294 return (e.error); 6295 } 6296 6297 if (res.status) { 6298 /* 6299 * If OTW errro is NOTSUPP, then it should be 6300 * translated to EINVAL. All Solaris file system 6301 * implementations return EINVAL to the syscall layer 6302 * when the attrdir cannot be created due to an 6303 * implementation restriction or noxattr mount option. 6304 */ 6305 if (res.status == NFS4ERR_NOTSUPP) { 6306 mutex_enter(&drp->r_statelock); 6307 if (drp->r_xattr_dir) 6308 VN_RELE(drp->r_xattr_dir); 6309 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6310 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6311 mutex_exit(&drp->r_statelock); 6312 6313 e.error = EINVAL; 6314 } else { 6315 e.error = geterrno4(res.status); 6316 } 6317 6318 if (e.error) { 6319 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6320 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6321 needrecov); 6322 return (e.error); 6323 } 6324 } 6325 6326 resop = &res.array[0]; /* putfh res */ 6327 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6328 6329 resop = &res.array[1]; /* openattr res */ 6330 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6331 6332 resop = &res.array[2]; /* getfh res */ 6333 gf_res = &resop->nfs_resop4_u.opgetfh; 6334 if (gf_res->object.nfs_fh4_len == 0) { 6335 *avp = NULL; 6336 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6337 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6338 return (ENOENT); 6339 } 6340 6341 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6342 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6343 dvp->v_vfsp, t, cr, dvp, 6344 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH)); 6345 sfh4_rele(&sfhp); 6346 6347 if (e.error) 6348 PURGE_ATTRCACHE4(vp); 6349 6350 mutex_enter(&vp->v_lock); 6351 vp->v_flag |= V_XATTRDIR; 6352 mutex_exit(&vp->v_lock); 6353 6354 *avp = vp; 6355 6356 mutex_enter(&drp->r_statelock); 6357 if (drp->r_xattr_dir) 6358 VN_RELE(drp->r_xattr_dir); 6359 VN_HOLD(vp); 6360 drp->r_xattr_dir = vp; 6361 6362 /* 6363 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6364 * NULL. xattrs could be created at any time, and we have no 6365 * way to update pc4_xattr_exists in the base object if/when 6366 * it happens. 6367 */ 6368 drp->r_pathconf.pc4_xattr_valid = 0; 6369 6370 mutex_exit(&drp->r_statelock); 6371 6372 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6373 6374 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6375 6376 return (0); 6377 } 6378 6379 /* ARGSUSED */ 6380 static int 6381 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6382 int mode, vnode_t **vpp, cred_t *cr, int flags) 6383 { 6384 int error; 6385 vnode_t *vp = NULL; 6386 rnode4_t *rp; 6387 struct vattr vattr; 6388 rnode4_t *drp; 6389 vnode_t *tempvp; 6390 enum createmode4 createmode; 6391 bool_t must_trunc = FALSE; 6392 6393 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6394 return (EPERM); 6395 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6396 return (EINVAL); 6397 } 6398 6399 /* . and .. have special meaning in the protocol, reject them. */ 6400 6401 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6402 return (EISDIR); 6403 6404 drp = VTOR4(dvp); 6405 6406 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6407 return (EINTR); 6408 6409 top: 6410 /* 6411 * We make a copy of the attributes because the caller does not 6412 * expect us to change what va points to. 6413 */ 6414 vattr = *va; 6415 6416 /* 6417 * If the pathname is "", then dvp is the root vnode of 6418 * a remote file mounted over a local directory. 6419 * All that needs to be done is access 6420 * checking and truncation. Note that we avoid doing 6421 * open w/ create because the parent directory might 6422 * be in pseudo-fs and the open would fail. 6423 */ 6424 if (*nm == '\0') { 6425 error = 0; 6426 VN_HOLD(dvp); 6427 vp = dvp; 6428 must_trunc = TRUE; 6429 } else { 6430 /* 6431 * We need to go over the wire, just to be sure whether the 6432 * file exists or not. Using the DNLC can be dangerous in 6433 * this case when making a decision regarding existence. 6434 */ 6435 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6436 } 6437 6438 if (exclusive) 6439 createmode = EXCLUSIVE4; 6440 else 6441 createmode = GUARDED4; 6442 6443 /* 6444 * error would be set if the file does not exist on the 6445 * server, so lets go create it. 6446 */ 6447 if (error) { 6448 goto create_otw; 6449 } 6450 6451 /* 6452 * File does exist on the server 6453 */ 6454 if (exclusive == EXCL) 6455 error = EEXIST; 6456 else if (vp->v_type == VDIR && (mode & VWRITE)) 6457 error = EISDIR; 6458 else { 6459 /* 6460 * If vnode is a device, create special vnode. 6461 */ 6462 if (ISVDEV(vp->v_type)) { 6463 tempvp = vp; 6464 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6465 VN_RELE(tempvp); 6466 } 6467 if (!(error = VOP_ACCESS(vp, mode, 0, cr))) { 6468 if ((vattr.va_mask & AT_SIZE) && 6469 vp->v_type == VREG) { 6470 rp = VTOR4(vp); 6471 /* 6472 * Check here for large file handled 6473 * by LF-unaware process (as 6474 * ufs_create() does) 6475 */ 6476 if (!(flags & FOFFMAX)) { 6477 mutex_enter(&rp->r_statelock); 6478 if (rp->r_size > MAXOFF32_T) 6479 error = EOVERFLOW; 6480 mutex_exit(&rp->r_statelock); 6481 } 6482 6483 /* if error is set then we need to return */ 6484 if (error) { 6485 nfs_rw_exit(&drp->r_rwlock); 6486 VN_RELE(vp); 6487 return (error); 6488 } 6489 6490 if (must_trunc) { 6491 vattr.va_mask = AT_SIZE; 6492 error = nfs4setattr(vp, &vattr, 0, cr, 6493 NULL); 6494 } else { 6495 /* 6496 * we know we have a regular file that already 6497 * exists and we may end up truncating the file 6498 * as a result of the open_otw, so flush out 6499 * any dirty pages for this file first. 6500 */ 6501 if (nfs4_has_pages(vp) && 6502 ((rp->r_flags & R4DIRTY) || 6503 rp->r_count > 0 || 6504 rp->r_mapcnt > 0)) { 6505 error = nfs4_putpage(vp, 6506 (offset_t)0, 0, 0, cr); 6507 if (error && (error == ENOSPC || 6508 error == EDQUOT)) { 6509 mutex_enter( 6510 &rp->r_statelock); 6511 if (!rp->r_error) 6512 rp->r_error = 6513 error; 6514 mutex_exit( 6515 &rp->r_statelock); 6516 } 6517 } 6518 vattr.va_mask = (AT_SIZE | 6519 AT_TYPE | AT_MODE); 6520 vattr.va_type = VREG; 6521 createmode = UNCHECKED4; 6522 goto create_otw; 6523 } 6524 } 6525 } 6526 } 6527 nfs_rw_exit(&drp->r_rwlock); 6528 if (error) { 6529 VN_RELE(vp); 6530 } else { 6531 *vpp = vp; 6532 } 6533 return (error); 6534 6535 create_otw: 6536 dnlc_remove(dvp, nm); 6537 6538 ASSERT(vattr.va_mask & AT_TYPE); 6539 6540 /* 6541 * If not a regular file let nfs4mknod() handle it. 6542 */ 6543 if (vattr.va_type != VREG) { 6544 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6545 nfs_rw_exit(&drp->r_rwlock); 6546 return (error); 6547 } 6548 6549 /* 6550 * It _is_ a regular file. 6551 */ 6552 ASSERT(vattr.va_mask & AT_MODE); 6553 if (MANDMODE(vattr.va_mode)) { 6554 nfs_rw_exit(&drp->r_rwlock); 6555 return (EACCES); 6556 } 6557 6558 /* 6559 * If this happens to be a mknod of a regular file, then flags will 6560 * have neither FREAD or FWRITE. However, we must set at least one 6561 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6562 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6563 * set (based on openmode specified by app). 6564 */ 6565 if ((flags & (FREAD|FWRITE)) == 0) 6566 flags |= (FREAD|FWRITE); 6567 6568 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6569 6570 if (vp != NULL) { 6571 /* if create was successful, throw away the file's pages */ 6572 if (!error && (vattr.va_mask & AT_SIZE)) 6573 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6574 cr); 6575 /* release the lookup hold */ 6576 VN_RELE(vp); 6577 vp = NULL; 6578 } 6579 6580 /* 6581 * validate that we opened a regular file. This handles a misbehaving 6582 * server that returns an incorrect FH. 6583 */ 6584 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6585 error = EISDIR; 6586 VN_RELE(*vpp); 6587 } 6588 6589 /* 6590 * If this is not an exclusive create, then the CREATE 6591 * request will be made with the GUARDED mode set. This 6592 * means that the server will return EEXIST if the file 6593 * exists. The file could exist because of a retransmitted 6594 * request. In this case, we recover by starting over and 6595 * checking to see whether the file exists. This second 6596 * time through it should and a CREATE request will not be 6597 * sent. 6598 * 6599 * This handles the problem of a dangling CREATE request 6600 * which contains attributes which indicate that the file 6601 * should be truncated. This retransmitted request could 6602 * possibly truncate valid data in the file if not caught 6603 * by the duplicate request mechanism on the server or if 6604 * not caught by other means. The scenario is: 6605 * 6606 * Client transmits CREATE request with size = 0 6607 * Client times out, retransmits request. 6608 * Response to the first request arrives from the server 6609 * and the client proceeds on. 6610 * Client writes data to the file. 6611 * The server now processes retransmitted CREATE request 6612 * and truncates file. 6613 * 6614 * The use of the GUARDED CREATE request prevents this from 6615 * happening because the retransmitted CREATE would fail 6616 * with EEXIST and would not truncate the file. 6617 */ 6618 if (error == EEXIST && exclusive == NONEXCL) { 6619 #ifdef DEBUG 6620 nfs4_create_misses++; 6621 #endif 6622 goto top; 6623 } 6624 nfs_rw_exit(&drp->r_rwlock); 6625 return (error); 6626 } 6627 6628 /* 6629 * Create compound (for mkdir, mknod, symlink): 6630 * { Putfh <dfh>; Create; Getfh; Getattr } 6631 * It's okay if setattr failed to set gid - this is not considered 6632 * an error, but purge attrs in that case. 6633 */ 6634 static int 6635 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6636 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6637 { 6638 int need_end_op = FALSE; 6639 COMPOUND4args_clnt args; 6640 COMPOUND4res_clnt res, *resp = NULL; 6641 nfs_argop4 *argop; 6642 nfs_resop4 *resop; 6643 int doqueue; 6644 mntinfo4_t *mi; 6645 rnode4_t *drp = VTOR4(dvp); 6646 change_info4 *cinfo; 6647 GETFH4res *gf_res; 6648 struct vattr vattr; 6649 vnode_t *vp; 6650 fattr4 *crattr; 6651 bool_t needrecov = FALSE; 6652 nfs4_recov_state_t recov_state; 6653 nfs4_sharedfh_t *sfhp = NULL; 6654 hrtime_t t; 6655 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6656 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6657 dirattr_info_t dinfo, *dinfop; 6658 servinfo4_t *svp; 6659 bitmap4 supp_attrs; 6660 6661 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6662 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6663 6664 mi = VTOMI4(dvp); 6665 6666 /* 6667 * Make sure we properly deal with setting the right gid 6668 * on a new directory to reflect the parent's setgid bit 6669 */ 6670 setgid_flag = 0; 6671 if (type == NF4DIR) { 6672 struct vattr dva; 6673 6674 va->va_mode &= ~VSGID; 6675 dva.va_mask = AT_MODE | AT_GID; 6676 if (VOP_GETATTR(dvp, &dva, 0, cr) == 0) { 6677 6678 /* 6679 * If the parent's directory has the setgid bit set 6680 * _and_ the client was able to get a valid mapping 6681 * for the parent dir's owner_group, we want to 6682 * append NVERIFY(owner_group == dva.va_gid) and 6683 * SETTATTR to the CREATE compound. 6684 */ 6685 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6686 setgid_flag = 1; 6687 va->va_mode |= VSGID; 6688 if (dva.va_gid != GID_NOBODY) { 6689 va->va_mask |= AT_GID; 6690 va->va_gid = dva.va_gid; 6691 } 6692 } 6693 } 6694 } 6695 6696 /* 6697 * Create ops: 6698 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6699 * 5:restorefh(dir) 6:getattr(dir) 6700 * 6701 * if (setgid) 6702 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6703 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6704 * 8:nverify 9:setattr 6705 */ 6706 if (setgid_flag) { 6707 numops = 10; 6708 idx_create = 1; 6709 idx_fattr = 3; 6710 } else { 6711 numops = 7; 6712 idx_create = 2; 6713 idx_fattr = 4; 6714 } 6715 6716 ASSERT(nfs_zone() == mi->mi_zone); 6717 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6718 return (EINTR); 6719 } 6720 recov_state.rs_flags = 0; 6721 recov_state.rs_num_retry_despite_err = 0; 6722 6723 argoplist_size = numops * sizeof (nfs_argop4); 6724 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6725 6726 recov_retry: 6727 if (type == NF4LNK) 6728 args.ctag = TAG_SYMLINK; 6729 else if (type == NF4DIR) 6730 args.ctag = TAG_MKDIR; 6731 else 6732 args.ctag = TAG_MKNOD; 6733 6734 args.array_len = numops; 6735 args.array = argop; 6736 6737 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6738 nfs_rw_exit(&drp->r_rwlock); 6739 kmem_free(argop, argoplist_size); 6740 return (e.error); 6741 } 6742 need_end_op = TRUE; 6743 6744 6745 /* 0: putfh directory */ 6746 argop[0].argop = OP_CPUTFH; 6747 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6748 6749 /* 1/2: Create object */ 6750 argop[idx_create].argop = OP_CCREATE; 6751 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6752 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6753 if (type == NF4LNK) { 6754 /* 6755 * symlink, treat name as data 6756 */ 6757 ASSERT(data != NULL); 6758 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6759 (char *)data; 6760 } 6761 if (type == NF4BLK || type == NF4CHR) { 6762 ASSERT(data != NULL); 6763 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6764 *((specdata4 *)data); 6765 } 6766 6767 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6768 6769 svp = drp->r_server; 6770 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6771 supp_attrs = svp->sv_supp_attrs; 6772 nfs_rw_exit(&svp->sv_lock); 6773 6774 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6775 nfs_rw_exit(&drp->r_rwlock); 6776 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6777 e.error = EINVAL; 6778 kmem_free(argop, argoplist_size); 6779 return (e.error); 6780 } 6781 6782 /* 2/3: getfh fh of created object */ 6783 ASSERT(idx_create + 1 == idx_fattr - 1); 6784 argop[idx_create + 1].argop = OP_GETFH; 6785 6786 /* 3/4: getattr of new object */ 6787 argop[idx_fattr].argop = OP_GETATTR; 6788 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6789 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6790 6791 if (setgid_flag) { 6792 vattr_t _v; 6793 6794 argop[4].argop = OP_SAVEFH; 6795 6796 argop[5].argop = OP_CPUTFH; 6797 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6798 6799 argop[6].argop = OP_GETATTR; 6800 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6801 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6802 6803 argop[7].argop = OP_RESTOREFH; 6804 6805 /* 6806 * nverify 6807 * 6808 * XXX - Revisit the last argument to nfs4_end_op() 6809 * once 5020486 is fixed. 6810 */ 6811 _v.va_mask = AT_GID; 6812 _v.va_gid = va->va_gid; 6813 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6814 supp_attrs)) { 6815 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6816 nfs_rw_exit(&drp->r_rwlock); 6817 nfs4_fattr4_free(crattr); 6818 kmem_free(argop, argoplist_size); 6819 return (e.error); 6820 } 6821 6822 /* 6823 * setattr 6824 * 6825 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6826 * so no need for stateid or flags. Also we specify NULL 6827 * rp since we're only interested in setting owner_group 6828 * attributes. 6829 */ 6830 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6831 &e.error, 0); 6832 6833 if (e.error) { 6834 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6835 nfs_rw_exit(&drp->r_rwlock); 6836 nfs4_fattr4_free(crattr); 6837 nfs4args_verify_free(&argop[8]); 6838 kmem_free(argop, argoplist_size); 6839 return (e.error); 6840 } 6841 } else { 6842 argop[1].argop = OP_SAVEFH; 6843 6844 argop[5].argop = OP_RESTOREFH; 6845 6846 argop[6].argop = OP_GETATTR; 6847 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6848 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6849 } 6850 6851 dnlc_remove(dvp, nm); 6852 6853 doqueue = 1; 6854 t = gethrtime(); 6855 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 6856 6857 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 6858 if (e.error) { 6859 PURGE_ATTRCACHE4(dvp); 6860 if (!needrecov) 6861 goto out; 6862 } 6863 6864 if (needrecov) { 6865 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 6866 OP_CREATE, NULL) == FALSE) { 6867 nfs4_end_op(mi, dvp, NULL, &recov_state, 6868 needrecov); 6869 need_end_op = FALSE; 6870 nfs4_fattr4_free(crattr); 6871 if (setgid_flag) { 6872 nfs4args_verify_free(&argop[8]); 6873 nfs4args_setattr_free(&argop[9]); 6874 } 6875 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6876 goto recov_retry; 6877 } 6878 } 6879 6880 resp = &res; 6881 6882 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 6883 6884 if (res.status == NFS4ERR_BADOWNER) 6885 nfs4_log_badowner(mi, OP_CREATE); 6886 6887 e.error = geterrno4(res.status); 6888 6889 /* 6890 * This check is left over from when create was implemented 6891 * using a setattr op (instead of createattrs). If the 6892 * putfh/create/getfh failed, the error was returned. If 6893 * setattr/getattr failed, we keep going. 6894 * 6895 * It might be better to get rid of the GETFH also, and just 6896 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 6897 * Then if any of the operations failed, we could return the 6898 * error now, and remove much of the error code below. 6899 */ 6900 if (res.array_len <= idx_fattr) { 6901 /* 6902 * Either Putfh, Create or Getfh failed. 6903 */ 6904 PURGE_ATTRCACHE4(dvp); 6905 /* 6906 * nfs4_purge_stale_fh() may generate otw calls through 6907 * nfs4_invalidate_pages. Hence the need to call 6908 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 6909 */ 6910 nfs4_end_op(mi, dvp, NULL, &recov_state, 6911 needrecov); 6912 need_end_op = FALSE; 6913 nfs4_purge_stale_fh(e.error, dvp, cr); 6914 goto out; 6915 } 6916 } 6917 6918 resop = &res.array[idx_create]; /* create res */ 6919 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 6920 6921 resop = &res.array[idx_create + 1]; /* getfh res */ 6922 gf_res = &resop->nfs_resop4_u.opgetfh; 6923 6924 sfhp = sfh4_get(&gf_res->object, mi); 6925 if (e.error) { 6926 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 6927 fn_get(VTOSV(dvp)->sv_name, nm)); 6928 if (vp->v_type == VNON) { 6929 vattr.va_mask = AT_TYPE; 6930 /* 6931 * Need to call nfs4_end_op before nfs4getattr to avoid 6932 * potential nfs4_start_op deadlock. See RFE 4777612. 6933 */ 6934 nfs4_end_op(mi, dvp, NULL, &recov_state, 6935 needrecov); 6936 need_end_op = FALSE; 6937 e.error = nfs4getattr(vp, &vattr, cr); 6938 if (e.error) { 6939 VN_RELE(vp); 6940 *vpp = NULL; 6941 goto out; 6942 } 6943 vp->v_type = vattr.va_type; 6944 } 6945 e.error = 0; 6946 } else { 6947 *vpp = vp = makenfs4node(sfhp, 6948 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 6949 dvp->v_vfsp, t, cr, 6950 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 6951 } 6952 6953 /* 6954 * If compound succeeded, then update dir attrs 6955 */ 6956 if (res.status == NFS4_OK) { 6957 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 6958 dinfo.di_cred = cr; 6959 dinfo.di_time_call = t; 6960 dinfop = &dinfo; 6961 } else 6962 dinfop = NULL; 6963 6964 /* Update directory cache attribute, readdir and dnlc caches */ 6965 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 6966 6967 out: 6968 if (sfhp != NULL) 6969 sfh4_rele(&sfhp); 6970 nfs_rw_exit(&drp->r_rwlock); 6971 nfs4_fattr4_free(crattr); 6972 if (setgid_flag) { 6973 nfs4args_verify_free(&argop[8]); 6974 nfs4args_setattr_free(&argop[9]); 6975 } 6976 if (resp) 6977 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 6978 if (need_end_op) 6979 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6980 6981 kmem_free(argop, argoplist_size); 6982 return (e.error); 6983 } 6984 6985 /* ARGSUSED */ 6986 static int 6987 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6988 int mode, vnode_t **vpp, cred_t *cr) 6989 { 6990 int error; 6991 vnode_t *vp; 6992 nfs_ftype4 type; 6993 specdata4 spec, *specp = NULL; 6994 6995 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6996 6997 switch (va->va_type) { 6998 case VCHR: 6999 case VBLK: 7000 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7001 spec.specdata1 = getmajor(va->va_rdev); 7002 spec.specdata2 = getminor(va->va_rdev); 7003 specp = &spec; 7004 break; 7005 7006 case VFIFO: 7007 type = NF4FIFO; 7008 break; 7009 case VSOCK: 7010 type = NF4SOCK; 7011 break; 7012 7013 default: 7014 return (EINVAL); 7015 } 7016 7017 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7018 if (error) { 7019 return (error); 7020 } 7021 7022 /* 7023 * This might not be needed any more; special case to deal 7024 * with problematic v2/v3 servers. Since create was unable 7025 * to set group correctly, not sure what hope setattr has. 7026 */ 7027 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7028 va->va_mask = AT_GID; 7029 (void) nfs4setattr(vp, va, 0, cr, NULL); 7030 } 7031 7032 /* 7033 * If vnode is a device create special vnode 7034 */ 7035 if (ISVDEV(vp->v_type)) { 7036 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7037 VN_RELE(vp); 7038 } else { 7039 *vpp = vp; 7040 } 7041 return (error); 7042 } 7043 7044 /* 7045 * Remove requires that the current fh be the target directory. 7046 * After the operation, the current fh is unchanged. 7047 * The compound op structure is: 7048 * PUTFH(targetdir), REMOVE 7049 * 7050 * Weirdness: if the vnode to be removed is open 7051 * we rename it instead of removing it and nfs_inactive 7052 * will remove the new name. 7053 */ 7054 static int 7055 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr) 7056 { 7057 COMPOUND4args_clnt args; 7058 COMPOUND4res_clnt res, *resp = NULL; 7059 REMOVE4res *rm_res; 7060 nfs_argop4 argop[3]; 7061 nfs_resop4 *resop; 7062 vnode_t *vp; 7063 char *tmpname; 7064 int doqueue; 7065 mntinfo4_t *mi; 7066 rnode4_t *rp; 7067 rnode4_t *drp; 7068 int needrecov = 0; 7069 nfs4_recov_state_t recov_state; 7070 int isopen; 7071 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7072 dirattr_info_t dinfo; 7073 7074 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7075 return (EPERM); 7076 drp = VTOR4(dvp); 7077 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7078 return (EINTR); 7079 7080 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7081 if (e.error) { 7082 nfs_rw_exit(&drp->r_rwlock); 7083 return (e.error); 7084 } 7085 7086 if (vp->v_type == VDIR) { 7087 VN_RELE(vp); 7088 nfs_rw_exit(&drp->r_rwlock); 7089 return (EISDIR); 7090 } 7091 7092 /* 7093 * First just remove the entry from the name cache, as it 7094 * is most likely the only entry for this vp. 7095 */ 7096 dnlc_remove(dvp, nm); 7097 7098 rp = VTOR4(vp); 7099 7100 /* 7101 * For regular file types, check to see if the file is open by looking 7102 * at the open streams. 7103 * For all other types, check the reference count on the vnode. Since 7104 * they are not opened OTW they never have an open stream. 7105 * 7106 * If the file is open, rename it to .nfsXXXX. 7107 */ 7108 if (vp->v_type != VREG) { 7109 /* 7110 * If the file has a v_count > 1 then there may be more than one 7111 * entry in the name cache due multiple links or an open file, 7112 * but we don't have the real reference count so flush all 7113 * possible entries. 7114 */ 7115 if (vp->v_count > 1) 7116 dnlc_purge_vp(vp); 7117 7118 /* 7119 * Now we have the real reference count. 7120 */ 7121 isopen = vp->v_count > 1; 7122 } else { 7123 mutex_enter(&rp->r_os_lock); 7124 isopen = list_head(&rp->r_open_streams) != NULL; 7125 mutex_exit(&rp->r_os_lock); 7126 } 7127 7128 mutex_enter(&rp->r_statelock); 7129 if (isopen && 7130 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7131 mutex_exit(&rp->r_statelock); 7132 tmpname = newname(); 7133 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr); 7134 if (e.error) 7135 kmem_free(tmpname, MAXNAMELEN); 7136 else { 7137 mutex_enter(&rp->r_statelock); 7138 if (rp->r_unldvp == NULL) { 7139 VN_HOLD(dvp); 7140 rp->r_unldvp = dvp; 7141 if (rp->r_unlcred != NULL) 7142 crfree(rp->r_unlcred); 7143 crhold(cr); 7144 rp->r_unlcred = cr; 7145 rp->r_unlname = tmpname; 7146 } else { 7147 kmem_free(rp->r_unlname, MAXNAMELEN); 7148 rp->r_unlname = tmpname; 7149 } 7150 mutex_exit(&rp->r_statelock); 7151 } 7152 VN_RELE(vp); 7153 nfs_rw_exit(&drp->r_rwlock); 7154 return (e.error); 7155 } 7156 /* 7157 * Actually remove the file/dir 7158 */ 7159 mutex_exit(&rp->r_statelock); 7160 7161 /* 7162 * We need to flush any dirty pages which happen to 7163 * be hanging around before removing the file. 7164 * This shouldn't happen very often since in NFSv4 7165 * we should be close to open consistent. 7166 */ 7167 if (nfs4_has_pages(vp) && 7168 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7169 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 7170 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7171 mutex_enter(&rp->r_statelock); 7172 if (!rp->r_error) 7173 rp->r_error = e.error; 7174 mutex_exit(&rp->r_statelock); 7175 } 7176 } 7177 7178 mi = VTOMI4(dvp); 7179 7180 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7181 recov_state.rs_flags = 0; 7182 recov_state.rs_num_retry_despite_err = 0; 7183 7184 recov_retry: 7185 /* 7186 * Remove ops: putfh dir; remove 7187 */ 7188 args.ctag = TAG_REMOVE; 7189 args.array_len = 3; 7190 args.array = argop; 7191 7192 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7193 if (e.error) { 7194 nfs_rw_exit(&drp->r_rwlock); 7195 VN_RELE(vp); 7196 return (e.error); 7197 } 7198 7199 /* putfh directory */ 7200 argop[0].argop = OP_CPUTFH; 7201 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7202 7203 /* remove */ 7204 argop[1].argop = OP_CREMOVE; 7205 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7206 7207 /* getattr dir */ 7208 argop[2].argop = OP_GETATTR; 7209 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7210 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7211 7212 doqueue = 1; 7213 dinfo.di_time_call = gethrtime(); 7214 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7215 7216 PURGE_ATTRCACHE4(vp); 7217 7218 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7219 if (e.error) 7220 PURGE_ATTRCACHE4(dvp); 7221 7222 if (needrecov) { 7223 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7224 NULL, NULL, NULL, OP_REMOVE, NULL) == FALSE) { 7225 if (!e.error) 7226 (void) xdr_free(xdr_COMPOUND4res_clnt, 7227 (caddr_t)&res); 7228 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7229 needrecov); 7230 goto recov_retry; 7231 } 7232 } 7233 7234 /* 7235 * Matching nfs4_end_op() for start_op() above. 7236 * There is a path in the code below which calls 7237 * nfs4_purge_stale_fh(), which may generate otw calls through 7238 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7239 * here to avoid nfs4_start_op() deadlock. 7240 */ 7241 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7242 7243 if (!e.error) { 7244 resp = &res; 7245 7246 if (res.status) { 7247 e.error = geterrno4(res.status); 7248 PURGE_ATTRCACHE4(dvp); 7249 nfs4_purge_stale_fh(e.error, dvp, cr); 7250 } else { 7251 resop = &res.array[1]; /* remove res */ 7252 rm_res = &resop->nfs_resop4_u.opremove; 7253 7254 dinfo.di_garp = 7255 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7256 dinfo.di_cred = cr; 7257 7258 /* Update directory attr, readdir and dnlc caches */ 7259 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7260 &dinfo); 7261 } 7262 } 7263 nfs_rw_exit(&drp->r_rwlock); 7264 if (resp) 7265 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7266 7267 VN_RELE(vp); 7268 return (e.error); 7269 } 7270 7271 /* 7272 * Link requires that the current fh be the target directory and the 7273 * saved fh be the source fh. After the operation, the current fh is unchanged. 7274 * Thus the compound op structure is: 7275 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7276 * GETATTR(file) 7277 */ 7278 static int 7279 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr) 7280 { 7281 COMPOUND4args_clnt args; 7282 COMPOUND4res_clnt res, *resp = NULL; 7283 LINK4res *ln_res; 7284 int argoplist_size = 7 * sizeof (nfs_argop4); 7285 nfs_argop4 *argop; 7286 nfs_resop4 *resop; 7287 vnode_t *realvp, *nvp; 7288 int doqueue; 7289 mntinfo4_t *mi; 7290 rnode4_t *tdrp; 7291 bool_t needrecov = FALSE; 7292 nfs4_recov_state_t recov_state; 7293 hrtime_t t; 7294 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7295 dirattr_info_t dinfo; 7296 7297 ASSERT(*tnm != '\0'); 7298 ASSERT(tdvp->v_type == VDIR); 7299 ASSERT(nfs4_consistent_type(tdvp)); 7300 ASSERT(nfs4_consistent_type(svp)); 7301 7302 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7303 return (EPERM); 7304 if (VOP_REALVP(svp, &realvp) == 0) { 7305 svp = realvp; 7306 ASSERT(nfs4_consistent_type(svp)); 7307 } 7308 7309 tdrp = VTOR4(tdvp); 7310 mi = VTOMI4(svp); 7311 7312 if (!(mi->mi_flags & MI4_LINK)) { 7313 return (EOPNOTSUPP); 7314 } 7315 recov_state.rs_flags = 0; 7316 recov_state.rs_num_retry_despite_err = 0; 7317 7318 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7319 return (EINTR); 7320 7321 recov_retry: 7322 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7323 7324 args.ctag = TAG_LINK; 7325 7326 /* 7327 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7328 * restorefh; getattr(fl) 7329 */ 7330 args.array_len = 7; 7331 args.array = argop; 7332 7333 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7334 if (e.error) { 7335 kmem_free(argop, argoplist_size); 7336 nfs_rw_exit(&tdrp->r_rwlock); 7337 return (e.error); 7338 } 7339 7340 /* 0. putfh file */ 7341 argop[0].argop = OP_CPUTFH; 7342 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7343 7344 /* 1. save current fh to free up the space for the dir */ 7345 argop[1].argop = OP_SAVEFH; 7346 7347 /* 2. putfh targetdir */ 7348 argop[2].argop = OP_CPUTFH; 7349 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7350 7351 /* 3. link: current_fh is targetdir, saved_fh is source */ 7352 argop[3].argop = OP_CLINK; 7353 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7354 7355 /* 4. Get attributes of dir */ 7356 argop[4].argop = OP_GETATTR; 7357 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7358 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7359 7360 /* 5. If link was successful, restore current vp to file */ 7361 argop[5].argop = OP_RESTOREFH; 7362 7363 /* 6. Get attributes of linked object */ 7364 argop[6].argop = OP_GETATTR; 7365 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7366 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7367 7368 dnlc_remove(tdvp, tnm); 7369 7370 doqueue = 1; 7371 t = gethrtime(); 7372 7373 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7374 7375 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7376 if (e.error != 0 && !needrecov) { 7377 PURGE_ATTRCACHE4(tdvp); 7378 PURGE_ATTRCACHE4(svp); 7379 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7380 goto out; 7381 } 7382 7383 if (needrecov) { 7384 bool_t abort; 7385 7386 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7387 NULL, NULL, OP_LINK, NULL); 7388 if (abort == FALSE) { 7389 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7390 needrecov); 7391 kmem_free(argop, argoplist_size); 7392 if (!e.error) 7393 (void) xdr_free(xdr_COMPOUND4res_clnt, 7394 (caddr_t)&res); 7395 goto recov_retry; 7396 } else { 7397 if (e.error != 0) { 7398 PURGE_ATTRCACHE4(tdvp); 7399 PURGE_ATTRCACHE4(svp); 7400 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7401 &recov_state, needrecov); 7402 goto out; 7403 } 7404 /* fall through for res.status case */ 7405 } 7406 } 7407 7408 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7409 7410 resp = &res; 7411 if (res.status) { 7412 /* If link succeeded, then don't return error */ 7413 e.error = geterrno4(res.status); 7414 if (res.array_len <= 4) { 7415 /* 7416 * Either Putfh, Savefh, Putfh dir, or Link failed 7417 */ 7418 PURGE_ATTRCACHE4(svp); 7419 PURGE_ATTRCACHE4(tdvp); 7420 if (e.error == EOPNOTSUPP) { 7421 mutex_enter(&mi->mi_lock); 7422 mi->mi_flags &= ~MI4_LINK; 7423 mutex_exit(&mi->mi_lock); 7424 } 7425 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7426 /* XXX-LP */ 7427 if (e.error == EISDIR && crgetuid(cr) != 0) 7428 e.error = EPERM; 7429 goto out; 7430 } 7431 } 7432 7433 /* either no error or one of the postop getattr failed */ 7434 7435 /* 7436 * XXX - if LINK succeeded, but no attrs were returned for link 7437 * file, purge its cache. 7438 * 7439 * XXX Perform a simplified version of wcc checking. Instead of 7440 * have another getattr to get pre-op, just purge cache if 7441 * any of the ops prior to and including the getattr failed. 7442 * If the getattr succeeded then update the attrcache accordingly. 7443 */ 7444 7445 /* 7446 * update cache with link file postattrs. 7447 * Note: at this point resop points to link res. 7448 */ 7449 resop = &res.array[3]; /* link res */ 7450 ln_res = &resop->nfs_resop4_u.oplink; 7451 if (res.status == NFS4_OK) { 7452 e.error = nfs4_update_attrcache(res.status, 7453 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7454 t, svp, cr); 7455 } 7456 7457 /* 7458 * Call makenfs4node to create the new shadow vp for tnm. 7459 * We pass NULL attrs because we just cached attrs for 7460 * the src object. All we're trying to accomplish is to 7461 * to create the new shadow vnode. 7462 */ 7463 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7464 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm)); 7465 7466 /* Update target cache attribute, readdir and dnlc caches */ 7467 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7468 dinfo.di_time_call = t; 7469 dinfo.di_cred = cr; 7470 7471 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7472 ASSERT(nfs4_consistent_type(tdvp)); 7473 ASSERT(nfs4_consistent_type(svp)); 7474 ASSERT(nfs4_consistent_type(nvp)); 7475 VN_RELE(nvp); 7476 7477 out: 7478 kmem_free(argop, argoplist_size); 7479 if (resp) 7480 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7481 7482 nfs_rw_exit(&tdrp->r_rwlock); 7483 7484 return (e.error); 7485 } 7486 7487 static int 7488 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7489 { 7490 vnode_t *realvp; 7491 7492 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7493 return (EPERM); 7494 if (VOP_REALVP(ndvp, &realvp) == 0) 7495 ndvp = realvp; 7496 7497 return (nfs4rename(odvp, onm, ndvp, nnm, cr)); 7498 } 7499 7500 /* 7501 * nfs4rename does the real work of renaming in NFS Version 4. 7502 * 7503 * A file handle is considered volatile for renaming purposes if either 7504 * of the volatile bits are turned on. However, the compound may differ 7505 * based on the likelihood of the filehandle to change during rename. 7506 */ 7507 static int 7508 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7509 { 7510 int error; 7511 mntinfo4_t *mi; 7512 vnode_t *nvp; 7513 vnode_t *ovp = NULL; 7514 char *tmpname = NULL; 7515 rnode4_t *rp; 7516 rnode4_t *odrp; 7517 rnode4_t *ndrp; 7518 int did_link = 0; 7519 int do_link = 1; 7520 nfsstat4 stat = NFS4_OK; 7521 7522 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7523 ASSERT(nfs4_consistent_type(odvp)); 7524 ASSERT(nfs4_consistent_type(ndvp)); 7525 7526 if (onm[0] == '.' && (onm[1] == '\0' || 7527 (onm[1] == '.' && onm[2] == '\0'))) 7528 return (EINVAL); 7529 7530 if (nnm[0] == '.' && (nnm[1] == '\0' || 7531 (nnm[1] == '.' && nnm[2] == '\0'))) 7532 return (EINVAL); 7533 7534 odrp = VTOR4(odvp); 7535 ndrp = VTOR4(ndvp); 7536 if ((intptr_t)odrp < (intptr_t)ndrp) { 7537 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7538 return (EINTR); 7539 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7540 nfs_rw_exit(&odrp->r_rwlock); 7541 return (EINTR); 7542 } 7543 } else { 7544 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7545 return (EINTR); 7546 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7547 nfs_rw_exit(&ndrp->r_rwlock); 7548 return (EINTR); 7549 } 7550 } 7551 7552 /* 7553 * Lookup the target file. If it exists, it needs to be 7554 * checked to see whether it is a mount point and whether 7555 * it is active (open). 7556 */ 7557 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7558 if (!error) { 7559 int isactive; 7560 7561 ASSERT(nfs4_consistent_type(nvp)); 7562 /* 7563 * If this file has been mounted on, then just 7564 * return busy because renaming to it would remove 7565 * the mounted file system from the name space. 7566 */ 7567 if (vn_ismntpt(nvp)) { 7568 VN_RELE(nvp); 7569 nfs_rw_exit(&odrp->r_rwlock); 7570 nfs_rw_exit(&ndrp->r_rwlock); 7571 return (EBUSY); 7572 } 7573 7574 /* 7575 * First just remove the entry from the name cache, as it 7576 * is most likely the only entry for this vp. 7577 */ 7578 dnlc_remove(ndvp, nnm); 7579 7580 rp = VTOR4(nvp); 7581 7582 if (nvp->v_type != VREG) { 7583 /* 7584 * Purge the name cache of all references to this vnode 7585 * so that we can check the reference count to infer 7586 * whether it is active or not. 7587 */ 7588 if (nvp->v_count > 1) 7589 dnlc_purge_vp(nvp); 7590 7591 isactive = nvp->v_count > 1; 7592 } else { 7593 mutex_enter(&rp->r_os_lock); 7594 isactive = list_head(&rp->r_open_streams) != NULL; 7595 mutex_exit(&rp->r_os_lock); 7596 } 7597 7598 /* 7599 * If the vnode is active and is not a directory, 7600 * arrange to rename it to a 7601 * temporary file so that it will continue to be 7602 * accessible. This implements the "unlink-open-file" 7603 * semantics for the target of a rename operation. 7604 * Before doing this though, make sure that the 7605 * source and target files are not already the same. 7606 */ 7607 if (isactive && nvp->v_type != VDIR) { 7608 /* 7609 * Lookup the source name. 7610 */ 7611 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7612 7613 /* 7614 * The source name *should* already exist. 7615 */ 7616 if (error) { 7617 VN_RELE(nvp); 7618 nfs_rw_exit(&odrp->r_rwlock); 7619 nfs_rw_exit(&ndrp->r_rwlock); 7620 return (error); 7621 } 7622 7623 ASSERT(nfs4_consistent_type(ovp)); 7624 7625 /* 7626 * Compare the two vnodes. If they are the same, 7627 * just release all held vnodes and return success. 7628 */ 7629 if (VN_CMP(ovp, nvp)) { 7630 VN_RELE(ovp); 7631 VN_RELE(nvp); 7632 nfs_rw_exit(&odrp->r_rwlock); 7633 nfs_rw_exit(&ndrp->r_rwlock); 7634 return (0); 7635 } 7636 7637 /* 7638 * Can't mix and match directories and non- 7639 * directories in rename operations. We already 7640 * know that the target is not a directory. If 7641 * the source is a directory, return an error. 7642 */ 7643 if (ovp->v_type == VDIR) { 7644 VN_RELE(ovp); 7645 VN_RELE(nvp); 7646 nfs_rw_exit(&odrp->r_rwlock); 7647 nfs_rw_exit(&ndrp->r_rwlock); 7648 return (ENOTDIR); 7649 } 7650 link_call: 7651 /* 7652 * The target file exists, is not the same as 7653 * the source file, and is active. We first 7654 * try to Link it to a temporary filename to 7655 * avoid having the server removing the file 7656 * completely (which could cause data loss to 7657 * the user's POV in the event the Rename fails 7658 * -- see bug 1165874). 7659 */ 7660 /* 7661 * The do_link and did_link booleans are 7662 * introduced in the event we get NFS4ERR_FILE_OPEN 7663 * returned for the Rename. Some servers can 7664 * not Rename over an Open file, so they return 7665 * this error. The client needs to Remove the 7666 * newly created Link and do two Renames, just 7667 * as if the server didn't support LINK. 7668 */ 7669 tmpname = newname(); 7670 error = 0; 7671 7672 if (do_link) { 7673 error = nfs4_link(ndvp, nvp, tmpname, cr); 7674 } 7675 if (error == EOPNOTSUPP || !do_link) { 7676 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7677 cr); 7678 did_link = 0; 7679 } else { 7680 did_link = 1; 7681 } 7682 if (error) { 7683 kmem_free(tmpname, MAXNAMELEN); 7684 VN_RELE(ovp); 7685 VN_RELE(nvp); 7686 nfs_rw_exit(&odrp->r_rwlock); 7687 nfs_rw_exit(&ndrp->r_rwlock); 7688 return (error); 7689 } 7690 7691 mutex_enter(&rp->r_statelock); 7692 if (rp->r_unldvp == NULL) { 7693 VN_HOLD(ndvp); 7694 rp->r_unldvp = ndvp; 7695 if (rp->r_unlcred != NULL) 7696 crfree(rp->r_unlcred); 7697 crhold(cr); 7698 rp->r_unlcred = cr; 7699 rp->r_unlname = tmpname; 7700 } else { 7701 if (rp->r_unlname) 7702 kmem_free(rp->r_unlname, MAXNAMELEN); 7703 rp->r_unlname = tmpname; 7704 } 7705 mutex_exit(&rp->r_statelock); 7706 } 7707 7708 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7709 7710 ASSERT(nfs4_consistent_type(nvp)); 7711 VN_RELE(nvp); 7712 } 7713 7714 if (ovp == NULL) { 7715 /* 7716 * When renaming directories to be a subdirectory of a 7717 * different parent, the dnlc entry for ".." will no 7718 * longer be valid, so it must be removed. 7719 * 7720 * We do a lookup here to determine whether we are renaming 7721 * a directory and we need to check if we are renaming 7722 * an unlinked file. This might have already been done 7723 * in previous code, so we check ovp == NULL to avoid 7724 * doing it twice. 7725 */ 7726 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7727 /* 7728 * The source name *should* already exist. 7729 */ 7730 if (error) { 7731 nfs_rw_exit(&odrp->r_rwlock); 7732 nfs_rw_exit(&ndrp->r_rwlock); 7733 return (error); 7734 } 7735 ASSERT(ovp != NULL); 7736 ASSERT(nfs4_consistent_type(ovp)); 7737 } 7738 7739 /* 7740 * Is the object being renamed a dir, and if so, is 7741 * it being renamed to a child of itself? The underlying 7742 * fs should ultimately return EINVAL for this case; 7743 * however, buggy beta non-Solaris NFSv4 servers at 7744 * interop testing events have allowed this behavior, 7745 * and it caused our client to panic due to a recursive 7746 * mutex_enter in fn_move. 7747 * 7748 * The tedious locking in fn_move could be changed to 7749 * deal with this case, and the client could avoid the 7750 * panic; however, the client would just confuse itself 7751 * later and misbehave. A better way to handle the broken 7752 * server is to detect this condition and return EINVAL 7753 * without ever sending the the bogus rename to the server. 7754 * We know the rename is invalid -- just fail it now. 7755 */ 7756 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7757 VN_RELE(ovp); 7758 nfs_rw_exit(&odrp->r_rwlock); 7759 nfs_rw_exit(&ndrp->r_rwlock); 7760 return (EINVAL); 7761 } 7762 7763 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7764 7765 /* 7766 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7767 * possible for the filehandle to change due to the rename. 7768 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7769 * the fh will not change because of the rename, but we still need 7770 * to update its rnode entry with the new name for 7771 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7772 * has no effect on these for now, but for future improvements, 7773 * we might want to use it too to simplify handling of files 7774 * that are open with that flag on. (XXX) 7775 */ 7776 mi = VTOMI4(odvp); 7777 if (NFS4_VOLATILE_FH(mi)) { 7778 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7779 &stat); 7780 } else { 7781 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7782 &stat); 7783 } 7784 ASSERT(nfs4_consistent_type(odvp)); 7785 ASSERT(nfs4_consistent_type(ndvp)); 7786 ASSERT(nfs4_consistent_type(ovp)); 7787 7788 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7789 do_link = 0; 7790 /* 7791 * Before the 'link_call' code, we did a nfs4_lookup 7792 * that puts a VN_HOLD on nvp. After the nfs4_link 7793 * call we call VN_RELE to match that hold. We need 7794 * to place an additional VN_HOLD here since we will 7795 * be hitting that VN_RELE again. 7796 */ 7797 VN_HOLD(nvp); 7798 7799 (void) nfs4_remove(ndvp, tmpname, cr); 7800 7801 /* Undo the unlinked file naming stuff we just did */ 7802 mutex_enter(&rp->r_statelock); 7803 if (rp->r_unldvp) { 7804 VN_RELE(ndvp); 7805 rp->r_unldvp = NULL; 7806 if (rp->r_unlcred != NULL) 7807 crfree(rp->r_unlcred); 7808 rp->r_unlcred = NULL; 7809 /* rp->r_unlanme points to tmpname */ 7810 if (rp->r_unlname) 7811 kmem_free(rp->r_unlname, MAXNAMELEN); 7812 rp->r_unlname = NULL; 7813 } 7814 mutex_exit(&rp->r_statelock); 7815 7816 goto link_call; 7817 } 7818 7819 if (error) { 7820 VN_RELE(ovp); 7821 nfs_rw_exit(&odrp->r_rwlock); 7822 nfs_rw_exit(&ndrp->r_rwlock); 7823 return (error); 7824 } 7825 7826 /* 7827 * when renaming directories to be a subdirectory of a 7828 * different parent, the dnlc entry for ".." will no 7829 * longer be valid, so it must be removed 7830 */ 7831 rp = VTOR4(ovp); 7832 if (ndvp != odvp) { 7833 if (ovp->v_type == VDIR) { 7834 dnlc_remove(ovp, ".."); 7835 if (rp->r_dir != NULL) 7836 nfs4_purge_rddir_cache(ovp); 7837 } 7838 } 7839 7840 /* 7841 * If we are renaming the unlinked file, update the 7842 * r_unldvp and r_unlname as needed. 7843 */ 7844 mutex_enter(&rp->r_statelock); 7845 if (rp->r_unldvp != NULL) { 7846 if (strcmp(rp->r_unlname, onm) == 0) { 7847 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 7848 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 7849 if (ndvp != rp->r_unldvp) { 7850 VN_RELE(rp->r_unldvp); 7851 rp->r_unldvp = ndvp; 7852 VN_HOLD(ndvp); 7853 } 7854 } 7855 } 7856 mutex_exit(&rp->r_statelock); 7857 7858 VN_RELE(ovp); 7859 7860 nfs_rw_exit(&odrp->r_rwlock); 7861 nfs_rw_exit(&ndrp->r_rwlock); 7862 7863 return (error); 7864 } 7865 7866 /* 7867 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 7868 * when it is known that the filehandle is persistent through rename. 7869 * 7870 * Rename requires that the current fh be the target directory and the 7871 * saved fh be the source directory. After the operation, the current fh 7872 * is unchanged. 7873 * The compound op structure for persistent fh rename is: 7874 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 7875 * Rather than bother with the directory postop args, we'll simply 7876 * update that a change occured in the cache, so no post-op getattrs. 7877 */ 7878 static int 7879 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 7880 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7881 { 7882 COMPOUND4args_clnt args; 7883 COMPOUND4res_clnt res, *resp = NULL; 7884 nfs_argop4 *argop; 7885 nfs_resop4 *resop; 7886 int doqueue, argoplist_size; 7887 mntinfo4_t *mi; 7888 rnode4_t *odrp = VTOR4(odvp); 7889 rnode4_t *ndrp = VTOR4(ndvp); 7890 RENAME4res *rn_res; 7891 bool_t needrecov; 7892 nfs4_recov_state_t recov_state; 7893 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7894 dirattr_info_t dinfo, *dinfop; 7895 7896 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7897 7898 recov_state.rs_flags = 0; 7899 recov_state.rs_num_retry_despite_err = 0; 7900 7901 /* 7902 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 7903 * 7904 * If source/target are different dirs, then append putfh(src); getattr 7905 */ 7906 args.array_len = (odvp == ndvp) ? 5 : 7; 7907 argoplist_size = args.array_len * sizeof (nfs_argop4); 7908 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 7909 7910 recov_retry: 7911 *statp = NFS4_OK; 7912 7913 /* No need to Lookup the file, persistent fh */ 7914 args.ctag = TAG_RENAME; 7915 7916 mi = VTOMI4(odvp); 7917 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 7918 if (e.error) { 7919 kmem_free(argop, argoplist_size); 7920 return (e.error); 7921 } 7922 7923 /* 0: putfh source directory */ 7924 argop[0].argop = OP_CPUTFH; 7925 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 7926 7927 /* 1: Save source fh to free up current for target */ 7928 argop[1].argop = OP_SAVEFH; 7929 7930 /* 2: putfh targetdir */ 7931 argop[2].argop = OP_CPUTFH; 7932 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7933 7934 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 7935 argop[3].argop = OP_CRENAME; 7936 argop[3].nfs_argop4_u.opcrename.coldname = onm; 7937 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 7938 7939 /* 4: getattr (targetdir) */ 7940 argop[4].argop = OP_GETATTR; 7941 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7942 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7943 7944 if (ndvp != odvp) { 7945 7946 /* 5: putfh (sourcedir) */ 7947 argop[5].argop = OP_CPUTFH; 7948 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7949 7950 /* 6: getattr (sourcedir) */ 7951 argop[6].argop = OP_GETATTR; 7952 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7953 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7954 } 7955 7956 dnlc_remove(odvp, onm); 7957 dnlc_remove(ndvp, nnm); 7958 7959 doqueue = 1; 7960 dinfo.di_time_call = gethrtime(); 7961 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7962 7963 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7964 if (e.error) { 7965 PURGE_ATTRCACHE4(odvp); 7966 PURGE_ATTRCACHE4(ndvp); 7967 } else { 7968 *statp = res.status; 7969 } 7970 7971 if (needrecov) { 7972 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 7973 OP_RENAME, NULL) == FALSE) { 7974 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 7975 if (!e.error) 7976 (void) xdr_free(xdr_COMPOUND4res_clnt, 7977 (caddr_t)&res); 7978 goto recov_retry; 7979 } 7980 } 7981 7982 if (!e.error) { 7983 resp = &res; 7984 /* 7985 * as long as OP_RENAME 7986 */ 7987 if (res.status != NFS4_OK && res.array_len <= 4) { 7988 e.error = geterrno4(res.status); 7989 PURGE_ATTRCACHE4(odvp); 7990 PURGE_ATTRCACHE4(ndvp); 7991 /* 7992 * System V defines rename to return EEXIST, not 7993 * ENOTEMPTY if the target directory is not empty. 7994 * Over the wire, the error is NFSERR_ENOTEMPTY 7995 * which geterrno4 maps to ENOTEMPTY. 7996 */ 7997 if (e.error == ENOTEMPTY) 7998 e.error = EEXIST; 7999 } else { 8000 8001 resop = &res.array[3]; /* rename res */ 8002 rn_res = &resop->nfs_resop4_u.oprename; 8003 8004 if (res.status == NFS4_OK) { 8005 /* 8006 * Update target attribute, readdir and dnlc 8007 * caches. 8008 */ 8009 dinfo.di_garp = 8010 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8011 dinfo.di_cred = cr; 8012 dinfop = &dinfo; 8013 } else 8014 dinfop = NULL; 8015 8016 nfs4_update_dircaches(&rn_res->target_cinfo, 8017 ndvp, NULL, NULL, dinfop); 8018 8019 /* 8020 * Update source attribute, readdir and dnlc caches 8021 * 8022 */ 8023 if (ndvp != odvp) { 8024 if (dinfop) 8025 dinfo.di_garp = 8026 &(res.array[6].nfs_resop4_u. 8027 opgetattr.ga_res); 8028 8029 nfs4_update_dircaches(&rn_res->source_cinfo, 8030 odvp, NULL, NULL, dinfop); 8031 } 8032 8033 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8034 nnm); 8035 } 8036 } 8037 8038 if (resp) 8039 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8040 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8041 kmem_free(argop, argoplist_size); 8042 8043 return (e.error); 8044 } 8045 8046 /* 8047 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8048 * it is possible for the filehandle to change due to the rename. 8049 * 8050 * The compound req in this case includes a post-rename lookup and getattr 8051 * to ensure that we have the correct fh and attributes for the object. 8052 * 8053 * Rename requires that the current fh be the target directory and the 8054 * saved fh be the source directory. After the operation, the current fh 8055 * is unchanged. 8056 * 8057 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8058 * update the filehandle for the renamed object. We also get the old 8059 * filehandle for historical reasons; this should be taken out sometime. 8060 * This results in a rather cumbersome compound... 8061 * 8062 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8063 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8064 * 8065 */ 8066 static int 8067 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8068 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8069 { 8070 COMPOUND4args_clnt args; 8071 COMPOUND4res_clnt res, *resp = NULL; 8072 int argoplist_size; 8073 nfs_argop4 *argop; 8074 nfs_resop4 *resop; 8075 int doqueue; 8076 mntinfo4_t *mi; 8077 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8078 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8079 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8080 RENAME4res *rn_res; 8081 GETFH4res *ngf_res; 8082 bool_t needrecov; 8083 nfs4_recov_state_t recov_state; 8084 hrtime_t t; 8085 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8086 dirattr_info_t dinfo, *dinfop = &dinfo; 8087 8088 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8089 8090 recov_state.rs_flags = 0; 8091 recov_state.rs_num_retry_despite_err = 0; 8092 8093 recov_retry: 8094 *statp = NFS4_OK; 8095 8096 /* 8097 * There is a window between the RPC and updating the path and 8098 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8099 * code, so that it doesn't try to use the old path during that 8100 * window. 8101 */ 8102 mutex_enter(&orp->r_statelock); 8103 while (orp->r_flags & R4RECEXPFH) { 8104 klwp_t *lwp = ttolwp(curthread); 8105 8106 if (lwp != NULL) 8107 lwp->lwp_nostop++; 8108 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8109 mutex_exit(&orp->r_statelock); 8110 if (lwp != NULL) 8111 lwp->lwp_nostop--; 8112 return (EINTR); 8113 } 8114 if (lwp != NULL) 8115 lwp->lwp_nostop--; 8116 } 8117 orp->r_flags |= R4RECEXPFH; 8118 mutex_exit(&orp->r_statelock); 8119 8120 mi = VTOMI4(odvp); 8121 8122 args.ctag = TAG_RENAME_VFH; 8123 args.array_len = (odvp == ndvp) ? 10 : 12; 8124 argoplist_size = args.array_len * sizeof (nfs_argop4); 8125 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8126 8127 /* 8128 * Rename ops: 8129 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8130 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8131 * LOOKUP(trgt), GETFH(new), GETATTR, 8132 * 8133 * if (odvp != ndvp) 8134 * add putfh(sourcedir), getattr(sourcedir) } 8135 */ 8136 args.array = argop; 8137 8138 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8139 &recov_state, NULL); 8140 if (e.error) { 8141 kmem_free(argop, argoplist_size); 8142 mutex_enter(&orp->r_statelock); 8143 orp->r_flags &= ~R4RECEXPFH; 8144 cv_broadcast(&orp->r_cv); 8145 mutex_exit(&orp->r_statelock); 8146 return (e.error); 8147 } 8148 8149 /* 0: putfh source directory */ 8150 argop[0].argop = OP_CPUTFH; 8151 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8152 8153 /* 1: Save source fh to free up current for target */ 8154 argop[1].argop = OP_SAVEFH; 8155 8156 /* 2: Lookup pre-rename fh of renamed object */ 8157 argop[2].argop = OP_CLOOKUP; 8158 argop[2].nfs_argop4_u.opclookup.cname = onm; 8159 8160 /* 3: getfh fh of renamed object (before rename) */ 8161 argop[3].argop = OP_GETFH; 8162 8163 /* 4: putfh targetdir */ 8164 argop[4].argop = OP_CPUTFH; 8165 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8166 8167 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8168 argop[5].argop = OP_CRENAME; 8169 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8170 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8171 8172 /* 6: getattr of target dir (post op attrs) */ 8173 argop[6].argop = OP_GETATTR; 8174 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8175 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8176 8177 /* 7: Lookup post-rename fh of renamed object */ 8178 argop[7].argop = OP_CLOOKUP; 8179 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8180 8181 /* 8: getfh fh of renamed object (after rename) */ 8182 argop[8].argop = OP_GETFH; 8183 8184 /* 9: getattr of renamed object */ 8185 argop[9].argop = OP_GETATTR; 8186 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8187 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8188 8189 /* 8190 * If source/target dirs are different, then get new post-op 8191 * attrs for source dir also. 8192 */ 8193 if (ndvp != odvp) { 8194 /* 10: putfh (sourcedir) */ 8195 argop[10].argop = OP_CPUTFH; 8196 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8197 8198 /* 11: getattr (sourcedir) */ 8199 argop[11].argop = OP_GETATTR; 8200 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8201 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8202 } 8203 8204 dnlc_remove(odvp, onm); 8205 dnlc_remove(ndvp, nnm); 8206 8207 doqueue = 1; 8208 t = gethrtime(); 8209 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8210 8211 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8212 if (e.error) { 8213 PURGE_ATTRCACHE4(odvp); 8214 PURGE_ATTRCACHE4(ndvp); 8215 if (!needrecov) { 8216 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8217 &recov_state, needrecov); 8218 goto out; 8219 } 8220 } else { 8221 *statp = res.status; 8222 } 8223 8224 if (needrecov) { 8225 bool_t abort; 8226 8227 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8228 OP_RENAME, NULL); 8229 if (abort == FALSE) { 8230 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8231 &recov_state, needrecov); 8232 kmem_free(argop, argoplist_size); 8233 if (!e.error) 8234 (void) xdr_free(xdr_COMPOUND4res_clnt, 8235 (caddr_t)&res); 8236 mutex_enter(&orp->r_statelock); 8237 orp->r_flags &= ~R4RECEXPFH; 8238 cv_broadcast(&orp->r_cv); 8239 mutex_exit(&orp->r_statelock); 8240 goto recov_retry; 8241 } else { 8242 if (e.error != 0) { 8243 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8244 &recov_state, needrecov); 8245 goto out; 8246 } 8247 /* fall through for res.status case */ 8248 } 8249 } 8250 8251 resp = &res; 8252 /* 8253 * If OP_RENAME (or any prev op) failed, then return an error. 8254 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8255 */ 8256 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8257 /* 8258 * Error in an op other than last Getattr 8259 */ 8260 e.error = geterrno4(res.status); 8261 PURGE_ATTRCACHE4(odvp); 8262 PURGE_ATTRCACHE4(ndvp); 8263 /* 8264 * System V defines rename to return EEXIST, not 8265 * ENOTEMPTY if the target directory is not empty. 8266 * Over the wire, the error is NFSERR_ENOTEMPTY 8267 * which geterrno4 maps to ENOTEMPTY. 8268 */ 8269 if (e.error == ENOTEMPTY) 8270 e.error = EEXIST; 8271 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8272 needrecov); 8273 goto out; 8274 } 8275 8276 /* rename results */ 8277 rn_res = &res.array[5].nfs_resop4_u.oprename; 8278 8279 if (res.status == NFS4_OK) { 8280 /* Update target attribute, readdir and dnlc caches */ 8281 dinfo.di_garp = 8282 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8283 dinfo.di_cred = cr; 8284 dinfo.di_time_call = t; 8285 } else 8286 dinfop = NULL; 8287 8288 /* Update source cache attribute, readdir and dnlc caches */ 8289 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8290 8291 /* Update source cache attribute, readdir and dnlc caches */ 8292 if (ndvp != odvp) { 8293 8294 /* 8295 * If dinfop is non-NULL, then compound succeded, so 8296 * set di_garp to attrs for source dir. dinfop is only 8297 * set to NULL when compound fails. 8298 */ 8299 if (dinfop) 8300 dinfo.di_garp = 8301 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8302 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8303 dinfop); 8304 } 8305 8306 /* 8307 * Update the rnode with the new component name and args, 8308 * and if the file handle changed, also update it with the new fh. 8309 * This is only necessary if the target object has an rnode 8310 * entry and there is no need to create one for it. 8311 */ 8312 resop = &res.array[8]; /* getfh new res */ 8313 ngf_res = &resop->nfs_resop4_u.opgetfh; 8314 8315 /* 8316 * Update the path and filehandle for the renamed object. 8317 */ 8318 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8319 8320 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8321 8322 if (res.status == NFS4_OK) { 8323 resop++; /* getattr res */ 8324 e.error = nfs4_update_attrcache(res.status, 8325 &resop->nfs_resop4_u.opgetattr.ga_res, 8326 t, ovp, cr); 8327 } 8328 8329 out: 8330 kmem_free(argop, argoplist_size); 8331 if (resp) 8332 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8333 mutex_enter(&orp->r_statelock); 8334 orp->r_flags &= ~R4RECEXPFH; 8335 cv_broadcast(&orp->r_cv); 8336 mutex_exit(&orp->r_statelock); 8337 8338 return (e.error); 8339 } 8340 8341 static int 8342 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr) 8343 { 8344 int error; 8345 vnode_t *vp; 8346 8347 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8348 return (EPERM); 8349 /* 8350 * As ".." has special meaning and rather than send a mkdir 8351 * over the wire to just let the server freak out, we just 8352 * short circuit it here and return EEXIST 8353 */ 8354 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8355 return (EEXIST); 8356 8357 /* 8358 * Decision to get the right gid and setgid bit of the 8359 * new directory is now made in call_nfs4_create_req. 8360 */ 8361 va->va_mask |= AT_MODE; 8362 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8363 if (error) 8364 return (error); 8365 8366 *vpp = vp; 8367 return (0); 8368 } 8369 8370 8371 /* 8372 * rmdir is using the same remove v4 op as does remove. 8373 * Remove requires that the current fh be the target directory. 8374 * After the operation, the current fh is unchanged. 8375 * The compound op structure is: 8376 * PUTFH(targetdir), REMOVE 8377 */ 8378 static int 8379 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr) 8380 { 8381 int need_end_op = FALSE; 8382 COMPOUND4args_clnt args; 8383 COMPOUND4res_clnt res, *resp = NULL; 8384 REMOVE4res *rm_res; 8385 nfs_argop4 argop[3]; 8386 nfs_resop4 *resop; 8387 vnode_t *vp; 8388 int doqueue; 8389 mntinfo4_t *mi; 8390 rnode4_t *drp; 8391 bool_t needrecov = FALSE; 8392 nfs4_recov_state_t recov_state; 8393 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8394 dirattr_info_t dinfo, *dinfop; 8395 8396 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8397 return (EPERM); 8398 /* 8399 * As ".." has special meaning and rather than send a rmdir 8400 * over the wire to just let the server freak out, we just 8401 * short circuit it here and return EEXIST 8402 */ 8403 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8404 return (EEXIST); 8405 8406 drp = VTOR4(dvp); 8407 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8408 return (EINTR); 8409 8410 /* 8411 * Attempt to prevent a rmdir(".") from succeeding. 8412 */ 8413 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8414 if (e.error) { 8415 nfs_rw_exit(&drp->r_rwlock); 8416 return (e.error); 8417 } 8418 if (vp == cdir) { 8419 VN_RELE(vp); 8420 nfs_rw_exit(&drp->r_rwlock); 8421 return (EINVAL); 8422 } 8423 8424 /* 8425 * Since nfsv4 remove op works on both files and directories, 8426 * check that the removed object is indeed a directory. 8427 */ 8428 if (vp->v_type != VDIR) { 8429 VN_RELE(vp); 8430 nfs_rw_exit(&drp->r_rwlock); 8431 return (ENOTDIR); 8432 } 8433 8434 /* 8435 * First just remove the entry from the name cache, as it 8436 * is most likely an entry for this vp. 8437 */ 8438 dnlc_remove(dvp, nm); 8439 8440 /* 8441 * If there vnode reference count is greater than one, then 8442 * there may be additional references in the DNLC which will 8443 * need to be purged. First, trying removing the entry for 8444 * the parent directory and see if that removes the additional 8445 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8446 * to completely remove any references to the directory which 8447 * might still exist in the DNLC. 8448 */ 8449 if (vp->v_count > 1) { 8450 dnlc_remove(vp, ".."); 8451 if (vp->v_count > 1) 8452 dnlc_purge_vp(vp); 8453 } 8454 8455 mi = VTOMI4(dvp); 8456 recov_state.rs_flags = 0; 8457 recov_state.rs_num_retry_despite_err = 0; 8458 8459 recov_retry: 8460 args.ctag = TAG_RMDIR; 8461 8462 /* 8463 * Rmdir ops: putfh dir; remove 8464 */ 8465 args.array_len = 3; 8466 args.array = argop; 8467 8468 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8469 if (e.error) { 8470 nfs_rw_exit(&drp->r_rwlock); 8471 return (e.error); 8472 } 8473 need_end_op = TRUE; 8474 8475 /* putfh directory */ 8476 argop[0].argop = OP_CPUTFH; 8477 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8478 8479 /* remove */ 8480 argop[1].argop = OP_CREMOVE; 8481 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8482 8483 /* getattr (postop attrs for dir that contained removed dir) */ 8484 argop[2].argop = OP_GETATTR; 8485 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8486 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8487 8488 dinfo.di_time_call = gethrtime(); 8489 doqueue = 1; 8490 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8491 8492 PURGE_ATTRCACHE4(vp); 8493 8494 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8495 if (e.error) { 8496 PURGE_ATTRCACHE4(dvp); 8497 } 8498 8499 if (needrecov) { 8500 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8501 NULL, OP_REMOVE, NULL) == FALSE) { 8502 if (!e.error) 8503 (void) xdr_free(xdr_COMPOUND4res_clnt, 8504 (caddr_t)&res); 8505 8506 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8507 needrecov); 8508 need_end_op = FALSE; 8509 goto recov_retry; 8510 } 8511 } 8512 8513 if (!e.error) { 8514 resp = &res; 8515 8516 /* 8517 * Only return error if first 2 ops (OP_REMOVE or earlier) 8518 * failed. 8519 */ 8520 if (res.status != NFS4_OK && res.array_len <= 2) { 8521 e.error = geterrno4(res.status); 8522 PURGE_ATTRCACHE4(dvp); 8523 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8524 &recov_state, needrecov); 8525 need_end_op = FALSE; 8526 nfs4_purge_stale_fh(e.error, dvp, cr); 8527 /* 8528 * System V defines rmdir to return EEXIST, not 8529 * ENOTEMPTY if the directory is not empty. Over 8530 * the wire, the error is NFSERR_ENOTEMPTY which 8531 * geterrno4 maps to ENOTEMPTY. 8532 */ 8533 if (e.error == ENOTEMPTY) 8534 e.error = EEXIST; 8535 } else { 8536 resop = &res.array[1]; /* remove res */ 8537 rm_res = &resop->nfs_resop4_u.opremove; 8538 8539 if (res.status == NFS4_OK) { 8540 resop = &res.array[2]; /* dir attrs */ 8541 dinfo.di_garp = 8542 &resop->nfs_resop4_u.opgetattr.ga_res; 8543 dinfo.di_cred = cr; 8544 dinfop = &dinfo; 8545 } else 8546 dinfop = NULL; 8547 8548 /* Update dir attribute, readdir and dnlc caches */ 8549 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8550 dinfop); 8551 8552 /* destroy rddir cache for dir that was removed */ 8553 if (VTOR4(vp)->r_dir != NULL) 8554 nfs4_purge_rddir_cache(vp); 8555 } 8556 } 8557 8558 if (need_end_op) 8559 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8560 8561 nfs_rw_exit(&drp->r_rwlock); 8562 8563 if (resp) 8564 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8565 8566 VN_RELE(vp); 8567 8568 return (e.error); 8569 } 8570 8571 static int 8572 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr) 8573 { 8574 int error; 8575 vnode_t *vp; 8576 rnode4_t *rp; 8577 char *contents; 8578 mntinfo4_t *mi = VTOMI4(dvp); 8579 8580 if (nfs_zone() != mi->mi_zone) 8581 return (EPERM); 8582 if (!(mi->mi_flags & MI4_SYMLINK)) 8583 return (EOPNOTSUPP); 8584 8585 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8586 if (error) { 8587 return (error); 8588 } 8589 8590 ASSERT(nfs4_consistent_type(vp)); 8591 rp = VTOR4(vp); 8592 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8593 8594 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8595 8596 if (contents != NULL) { 8597 mutex_enter(&rp->r_statelock); 8598 if (rp->r_symlink.contents == NULL) { 8599 rp->r_symlink.len = strlen(tnm); 8600 bcopy(tnm, contents, rp->r_symlink.len); 8601 rp->r_symlink.contents = contents; 8602 rp->r_symlink.size = MAXPATHLEN; 8603 mutex_exit(&rp->r_statelock); 8604 } else { 8605 mutex_exit(&rp->r_statelock); 8606 kmem_free((void *)contents, MAXPATHLEN); 8607 } 8608 } 8609 } 8610 VN_RELE(vp); 8611 8612 return (error); 8613 } 8614 8615 8616 /* 8617 * Read directory entries. 8618 * There are some weird things to look out for here. The uio_loffset 8619 * field is either 0 or it is the offset returned from a previous 8620 * readdir. It is an opaque value used by the server to find the 8621 * correct directory block to read. The count field is the number 8622 * of blocks to read on the server. This is advisory only, the server 8623 * may return only one block's worth of entries. Entries may be compressed 8624 * on the server. 8625 */ 8626 static int 8627 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp) 8628 { 8629 int error; 8630 uint_t count; 8631 rnode4_t *rp; 8632 rddir4_cache *rdc; 8633 rddir4_cache *rrdc; 8634 8635 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8636 return (EIO); 8637 rp = VTOR4(vp); 8638 8639 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8640 8641 /* 8642 * Make sure that the directory cache is valid. 8643 */ 8644 if (rp->r_dir != NULL) { 8645 if (nfs_disable_rddir_cache != 0) { 8646 /* 8647 * Setting nfs_disable_rddir_cache in /etc/system 8648 * allows interoperability with servers that do not 8649 * properly update the attributes of directories. 8650 * Any cached information gets purged before an 8651 * access is made to it. 8652 */ 8653 nfs4_purge_rddir_cache(vp); 8654 } 8655 8656 error = nfs4_validate_caches(vp, cr); 8657 if (error) 8658 return (error); 8659 } 8660 8661 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8662 8663 /* 8664 * Short circuit last readdir which always returns 0 bytes. 8665 * This can be done after the directory has been read through 8666 * completely at least once. This will set r_direof which 8667 * can be used to find the value of the last cookie. 8668 */ 8669 mutex_enter(&rp->r_statelock); 8670 if (rp->r_direof != NULL && 8671 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8672 mutex_exit(&rp->r_statelock); 8673 #ifdef DEBUG 8674 nfs4_readdir_cache_shorts++; 8675 #endif 8676 if (eofp) 8677 *eofp = 1; 8678 return (0); 8679 } 8680 8681 /* 8682 * Look for a cache entry. Cache entries are identified 8683 * by the NFS cookie value and the byte count requested. 8684 */ 8685 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8686 8687 /* 8688 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8689 */ 8690 if (rdc == NULL) { 8691 mutex_exit(&rp->r_statelock); 8692 return (EINTR); 8693 } 8694 8695 /* 8696 * Check to see if we need to fill this entry in. 8697 */ 8698 if (rdc->flags & RDDIRREQ) { 8699 rdc->flags &= ~RDDIRREQ; 8700 rdc->flags |= RDDIR; 8701 mutex_exit(&rp->r_statelock); 8702 8703 /* 8704 * Do the readdir. 8705 */ 8706 nfs4readdir(vp, rdc, cr); 8707 8708 /* 8709 * Reaquire the lock, so that we can continue 8710 */ 8711 mutex_enter(&rp->r_statelock); 8712 /* 8713 * The entry is now complete 8714 */ 8715 rdc->flags &= ~RDDIR; 8716 } 8717 8718 ASSERT(!(rdc->flags & RDDIR)); 8719 8720 /* 8721 * If an error occurred while attempting 8722 * to fill the cache entry, mark the entry invalid and 8723 * just return the error. 8724 */ 8725 if (rdc->error) { 8726 error = rdc->error; 8727 rdc->flags |= RDDIRREQ; 8728 rddir4_cache_rele(rp, rdc); 8729 mutex_exit(&rp->r_statelock); 8730 return (error); 8731 } 8732 8733 /* 8734 * The cache entry is complete and good, 8735 * copyout the dirent structs to the calling 8736 * thread. 8737 */ 8738 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8739 8740 /* 8741 * If no error occurred during the copyout, 8742 * update the offset in the uio struct to 8743 * contain the value of the next NFS 4 cookie 8744 * and set the eof value appropriately. 8745 */ 8746 if (!error) { 8747 uiop->uio_loffset = rdc->nfs4_ncookie; 8748 if (eofp) 8749 *eofp = rdc->eof; 8750 } 8751 8752 /* 8753 * Decide whether to do readahead. Don't if we 8754 * have already read to the end of directory. 8755 */ 8756 if (rdc->eof) { 8757 /* 8758 * Make the entry the direof only if it is cached 8759 */ 8760 if (rdc->flags & RDDIRCACHED) 8761 rp->r_direof = rdc; 8762 rddir4_cache_rele(rp, rdc); 8763 mutex_exit(&rp->r_statelock); 8764 return (error); 8765 } 8766 8767 /* Determine if a readdir readahead should be done */ 8768 if (!(rp->r_flags & R4LOOKUP)) { 8769 rddir4_cache_rele(rp, rdc); 8770 mutex_exit(&rp->r_statelock); 8771 return (error); 8772 } 8773 8774 /* 8775 * Now look for a readahead entry. 8776 * 8777 * Check to see whether we found an entry for the readahead. 8778 * If so, we don't need to do anything further, so free the new 8779 * entry if one was allocated. Otherwise, allocate a new entry, add 8780 * it to the cache, and then initiate an asynchronous readdir 8781 * operation to fill it. 8782 */ 8783 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 8784 8785 /* 8786 * A readdir cache entry could not be obtained for the readahead. In 8787 * this case we skip the readahead and return. 8788 */ 8789 if (rrdc == NULL) { 8790 rddir4_cache_rele(rp, rdc); 8791 mutex_exit(&rp->r_statelock); 8792 return (error); 8793 } 8794 8795 /* 8796 * Check to see if we need to fill this entry in. 8797 */ 8798 if (rrdc->flags & RDDIRREQ) { 8799 rrdc->flags &= ~RDDIRREQ; 8800 rrdc->flags |= RDDIR; 8801 rddir4_cache_rele(rp, rdc); 8802 mutex_exit(&rp->r_statelock); 8803 #ifdef DEBUG 8804 nfs4_readdir_readahead++; 8805 #endif 8806 /* 8807 * Do the readdir. 8808 */ 8809 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 8810 return (error); 8811 } 8812 8813 rddir4_cache_rele(rp, rrdc); 8814 rddir4_cache_rele(rp, rdc); 8815 mutex_exit(&rp->r_statelock); 8816 return (error); 8817 } 8818 8819 static int 8820 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8821 { 8822 int error; 8823 rnode4_t *rp; 8824 8825 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 8826 8827 rp = VTOR4(vp); 8828 8829 /* 8830 * Obtain the readdir results for the caller. 8831 */ 8832 nfs4readdir(vp, rdc, cr); 8833 8834 mutex_enter(&rp->r_statelock); 8835 /* 8836 * The entry is now complete 8837 */ 8838 rdc->flags &= ~RDDIR; 8839 8840 error = rdc->error; 8841 if (error) 8842 rdc->flags |= RDDIRREQ; 8843 rddir4_cache_rele(rp, rdc); 8844 mutex_exit(&rp->r_statelock); 8845 8846 return (error); 8847 } 8848 8849 static void 8850 nfs4readdir_stub(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8851 { 8852 int stublength; 8853 dirent64_t *dp; 8854 u_longlong_t nodeid, pnodeid; 8855 vnode_t *dotdotvp = NULL; 8856 rnode4_t *rp = VTOR4(vp); 8857 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8858 8859 rdc->error = 0; 8860 rdc->entries = 0; 8861 rdc->actlen = rdc->entlen = 0; 8862 rdc->eof = TRUE; 8863 8864 /* Check for EOF case for readdir of stub */ 8865 if (cookie != 0 && cookie != 1) 8866 return; 8867 8868 nodeid = rp->r_attr.va_nodeid; 8869 if (vp->v_flag & VROOT) { 8870 pnodeid = nodeid; /* root of mount point */ 8871 } else { 8872 if (rdc->error = nfs4_lookup(vp, "..", &dotdotvp, 0, 0, 0, cr)) 8873 return; 8874 pnodeid = VTOR4(dotdotvp)->r_attr.va_nodeid; 8875 VN_RELE(dotdotvp); 8876 } 8877 8878 stublength = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8879 rdc->entries = kmem_alloc(stublength, KM_SLEEP); 8880 rdc->entlen = rdc->buflen = stublength; 8881 rdc->eof = TRUE; 8882 8883 dp = (dirent64_t *)rdc->entries; 8884 8885 if (rdc->nfs4_cookie == (nfs_cookie4)0) { 8886 bcopy(nfs4_dot_entries, rdc->entries, 8887 DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2)); 8888 dp->d_ino = nodeid; 8889 dp = (struct dirent64 *)(((char *)dp) + DIRENT64_RECLEN(1)); 8890 dp->d_ino = pnodeid; 8891 rdc->actlen = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8892 } else { /* for ".." entry */ 8893 bcopy(nfs4_dot_dot_entry, rdc->entries, DIRENT64_RECLEN(2)); 8894 dp->d_ino = pnodeid; 8895 rdc->actlen = DIRENT64_RECLEN(2); 8896 } 8897 rdc->nfs4_ncookie = rdc->actlen; 8898 } 8899 8900 /* 8901 * Read directory entries. 8902 * There are some weird things to look out for here. The uio_loffset 8903 * field is either 0 or it is the offset returned from a previous 8904 * readdir. It is an opaque value used by the server to find the 8905 * correct directory block to read. The count field is the number 8906 * of blocks to read on the server. This is advisory only, the server 8907 * may return only one block's worth of entries. Entries may be compressed 8908 * on the server. 8909 * 8910 * Generates the following compound request: 8911 * 1. If readdir offset is zero and no dnlc entry for parent exists, 8912 * must include a Lookupp as well. In this case, send: 8913 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 8914 * 2. Otherwise just do: { Putfh <fh>; Readdir } 8915 * 8916 * Get complete attributes and filehandles for entries if this is the 8917 * first read of the directory. Otherwise, just get fileid's. 8918 */ 8919 static void 8920 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8921 { 8922 COMPOUND4args_clnt args; 8923 COMPOUND4res_clnt res; 8924 READDIR4args *rargs; 8925 READDIR4res_clnt *rd_res; 8926 bitmap4 rd_bitsval; 8927 nfs_argop4 argop[5]; 8928 nfs_resop4 *resop; 8929 rnode4_t *rp = VTOR4(vp); 8930 mntinfo4_t *mi = VTOMI4(vp); 8931 int doqueue; 8932 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 8933 vnode_t *dvp; 8934 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8935 int num_ops, res_opcnt; 8936 bool_t needrecov = FALSE; 8937 nfs4_recov_state_t recov_state; 8938 hrtime_t t; 8939 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8940 8941 ASSERT(nfs_zone() == mi->mi_zone); 8942 ASSERT(rdc->flags & RDDIR); 8943 ASSERT(rdc->entries == NULL); 8944 8945 if (rp->r_flags & R4SRVSTUB) { 8946 nfs4readdir_stub(vp, rdc, cr); 8947 return; 8948 } 8949 8950 num_ops = 2; 8951 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 8952 /* 8953 * Since nfsv4 readdir may not return entries for "." and "..", 8954 * the client must recreate them: 8955 * To find the correct nodeid, do the following: 8956 * For current node, get nodeid from dnlc. 8957 * - if current node is rootvp, set pnodeid to nodeid. 8958 * - else if parent is in the dnlc, get its nodeid from there. 8959 * - else add LOOKUPP+GETATTR to compound. 8960 */ 8961 nodeid = rp->r_attr.va_nodeid; 8962 if (vp->v_flag & VROOT) { 8963 pnodeid = nodeid; /* root of mount point */ 8964 } else { 8965 dvp = dnlc_lookup(vp, ".."); 8966 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 8967 /* parent in dnlc cache - no need for otw */ 8968 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 8969 } else { 8970 /* 8971 * parent not in dnlc cache, 8972 * do lookupp to get its id 8973 */ 8974 num_ops = 5; 8975 pnodeid = 0; /* set later by getattr parent */ 8976 } 8977 if (dvp) 8978 VN_RELE(dvp); 8979 } 8980 } 8981 recov_state.rs_flags = 0; 8982 recov_state.rs_num_retry_despite_err = 0; 8983 8984 /* Save the original mount point security flavor */ 8985 (void) save_mnt_secinfo(mi->mi_curr_serv); 8986 8987 recov_retry: 8988 args.ctag = TAG_READDIR; 8989 8990 args.array = argop; 8991 args.array_len = num_ops; 8992 8993 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 8994 &recov_state, NULL)) { 8995 /* 8996 * If readdir a node that is a stub for a crossed mount point, 8997 * keep the original secinfo flavor for the current file 8998 * system, not the crossed one. 8999 */ 9000 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9001 rdc->error = e.error; 9002 return; 9003 } 9004 9005 /* 9006 * Determine which attrs to request for dirents. This code 9007 * must be protected by nfs4_start/end_fop because of r_server 9008 * (which will change during failover recovery). 9009 * 9010 */ 9011 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9012 /* 9013 * Get all vattr attrs plus filehandle and rdattr_error 9014 */ 9015 rd_bitsval = NFS4_VATTR_MASK | 9016 FATTR4_RDATTR_ERROR_MASK | 9017 FATTR4_FILEHANDLE_MASK; 9018 9019 if (rp->r_flags & R4READDIRWATTR) { 9020 mutex_enter(&rp->r_statelock); 9021 rp->r_flags &= ~R4READDIRWATTR; 9022 mutex_exit(&rp->r_statelock); 9023 } 9024 } else { 9025 servinfo4_t *svp = rp->r_server; 9026 9027 /* 9028 * Already read directory. Use readdir with 9029 * no attrs (except for mounted_on_fileid) for updates. 9030 */ 9031 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9032 9033 /* 9034 * request mounted on fileid if supported, else request 9035 * fileid. maybe we should verify that fileid is supported 9036 * and request something else if not. 9037 */ 9038 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9039 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9040 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9041 nfs_rw_exit(&svp->sv_lock); 9042 } 9043 9044 /* putfh directory fh */ 9045 argop[0].argop = OP_CPUTFH; 9046 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9047 9048 argop[1].argop = OP_READDIR; 9049 rargs = &argop[1].nfs_argop4_u.opreaddir; 9050 /* 9051 * 1 and 2 are reserved for client "." and ".." entry offset. 9052 * cookie 0 should be used over-the-wire to start reading at 9053 * the beginning of the directory excluding "." and "..". 9054 */ 9055 if (rdc->nfs4_cookie == 0 || 9056 rdc->nfs4_cookie == 1 || 9057 rdc->nfs4_cookie == 2) { 9058 rargs->cookie = (nfs_cookie4)0; 9059 rargs->cookieverf = 0; 9060 } else { 9061 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9062 mutex_enter(&rp->r_statelock); 9063 rargs->cookieverf = rp->r_cookieverf4; 9064 mutex_exit(&rp->r_statelock); 9065 } 9066 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9067 rargs->maxcount = mi->mi_tsize; 9068 rargs->attr_request = rd_bitsval; 9069 rargs->rdc = rdc; 9070 rargs->dvp = vp; 9071 rargs->mi = mi; 9072 rargs->cr = cr; 9073 9074 9075 /* 9076 * If count < than the minimum required, we return no entries 9077 * and fail with EINVAL 9078 */ 9079 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9080 rdc->error = EINVAL; 9081 goto out; 9082 } 9083 9084 if (args.array_len == 5) { 9085 /* 9086 * Add lookupp and getattr for parent nodeid. 9087 */ 9088 argop[2].argop = OP_LOOKUPP; 9089 9090 argop[3].argop = OP_GETFH; 9091 9092 /* getattr parent */ 9093 argop[4].argop = OP_GETATTR; 9094 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9095 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9096 } 9097 9098 doqueue = 1; 9099 9100 if (mi->mi_io_kstats) { 9101 mutex_enter(&mi->mi_lock); 9102 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9103 mutex_exit(&mi->mi_lock); 9104 } 9105 9106 /* capture the time of this call */ 9107 rargs->t = t = gethrtime(); 9108 9109 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9110 9111 if (mi->mi_io_kstats) { 9112 mutex_enter(&mi->mi_lock); 9113 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9114 mutex_exit(&mi->mi_lock); 9115 } 9116 9117 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9118 9119 /* 9120 * If RPC error occurred and it isn't an error that 9121 * triggers recovery, then go ahead and fail now. 9122 */ 9123 if (e.error != 0 && !needrecov) { 9124 rdc->error = e.error; 9125 goto out; 9126 } 9127 9128 if (needrecov) { 9129 bool_t abort; 9130 9131 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9132 "nfs4readdir: initiating recovery.\n")); 9133 9134 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9135 NULL, OP_READDIR, NULL); 9136 if (abort == FALSE) { 9137 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9138 &recov_state, needrecov); 9139 if (!e.error) 9140 (void) xdr_free(xdr_COMPOUND4res_clnt, 9141 (caddr_t)&res); 9142 if (rdc->entries != NULL) { 9143 kmem_free(rdc->entries, rdc->entlen); 9144 rdc->entries = NULL; 9145 } 9146 goto recov_retry; 9147 } 9148 9149 if (e.error != 0) { 9150 rdc->error = e.error; 9151 goto out; 9152 } 9153 9154 /* fall through for res.status case */ 9155 } 9156 9157 res_opcnt = res.array_len; 9158 9159 /* 9160 * If compound failed first 2 ops (PUTFH+READDIR), then return 9161 * failure here. Subsequent ops are for filling out dot-dot 9162 * dirent, and if they fail, we still want to give the caller 9163 * the dirents returned by (the successful) READDIR op, so we need 9164 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9165 * 9166 * One example where PUTFH+READDIR ops would succeed but 9167 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9168 * but lacks x. In this case, a POSIX server's VOP_READDIR 9169 * would succeed; however, VOP_LOOKUP(..) would fail since no 9170 * x perm. We need to come up with a non-vendor-specific way 9171 * for a POSIX server to return d_ino from dotdot's dirent if 9172 * client only requests mounted_on_fileid, and just say the 9173 * LOOKUPP succeeded and fill out the GETATTR. However, if 9174 * client requested any mandatory attrs, server would be required 9175 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9176 * for dotdot. 9177 */ 9178 9179 if (res.status) { 9180 if (res_opcnt <= 2) { 9181 e.error = geterrno4(res.status); 9182 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9183 &recov_state, needrecov); 9184 nfs4_purge_stale_fh(e.error, vp, cr); 9185 rdc->error = e.error; 9186 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9187 if (rdc->entries != NULL) { 9188 kmem_free(rdc->entries, rdc->entlen); 9189 rdc->entries = NULL; 9190 } 9191 /* 9192 * If readdir a node that is a stub for a 9193 * crossed mount point, keep the original 9194 * secinfo flavor for the current file system, 9195 * not the crossed one. 9196 */ 9197 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9198 return; 9199 } 9200 } 9201 9202 resop = &res.array[1]; /* readdir res */ 9203 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9204 9205 mutex_enter(&rp->r_statelock); 9206 rp->r_cookieverf4 = rd_res->cookieverf; 9207 mutex_exit(&rp->r_statelock); 9208 9209 /* 9210 * For "." and ".." entries 9211 * e.g. 9212 * seek(cookie=0) -> "." entry with d_off = 1 9213 * seek(cookie=1) -> ".." entry with d_off = 2 9214 */ 9215 if (cookie == (nfs_cookie4) 0) { 9216 if (rd_res->dotp) 9217 rd_res->dotp->d_ino = nodeid; 9218 if (rd_res->dotdotp) 9219 rd_res->dotdotp->d_ino = pnodeid; 9220 } 9221 if (cookie == (nfs_cookie4) 1) { 9222 if (rd_res->dotdotp) 9223 rd_res->dotdotp->d_ino = pnodeid; 9224 } 9225 9226 9227 /* LOOKUPP+GETATTR attemped */ 9228 if (args.array_len == 5 && rd_res->dotdotp) { 9229 if (res.status == NFS4_OK && res_opcnt == 5) { 9230 nfs_fh4 *fhp; 9231 nfs4_sharedfh_t *sfhp; 9232 vnode_t *pvp; 9233 nfs4_ga_res_t *garp; 9234 9235 resop++; /* lookupp */ 9236 resop++; /* getfh */ 9237 fhp = &resop->nfs_resop4_u.opgetfh.object; 9238 9239 resop++; /* getattr of parent */ 9240 9241 /* 9242 * First, take care of finishing the 9243 * readdir results. 9244 */ 9245 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9246 /* 9247 * The d_ino of .. must be the inode number 9248 * of the mounted filesystem. 9249 */ 9250 if (garp->n4g_va.va_mask & AT_NODEID) 9251 rd_res->dotdotp->d_ino = 9252 garp->n4g_va.va_nodeid; 9253 9254 9255 /* 9256 * Next, create the ".." dnlc entry 9257 */ 9258 sfhp = sfh4_get(fhp, mi); 9259 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9260 dnlc_update(vp, "..", pvp); 9261 VN_RELE(pvp); 9262 } 9263 sfh4_rele(&sfhp); 9264 } 9265 } 9266 9267 if (mi->mi_io_kstats) { 9268 mutex_enter(&mi->mi_lock); 9269 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9270 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9271 mutex_exit(&mi->mi_lock); 9272 } 9273 9274 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9275 9276 out: 9277 /* 9278 * If readdir a node that is a stub for a crossed mount point, 9279 * keep the original secinfo flavor for the current file system, 9280 * not the crossed one. 9281 */ 9282 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9283 9284 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9285 } 9286 9287 9288 static int 9289 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9290 { 9291 rnode4_t *rp = VTOR4(bp->b_vp); 9292 int count; 9293 int error; 9294 cred_t *cred_otw = NULL; 9295 offset_t offset; 9296 nfs4_open_stream_t *osp = NULL; 9297 bool_t first_time = TRUE; /* first time getting otw cred */ 9298 bool_t last_time = FALSE; /* last time getting otw cred */ 9299 9300 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9301 9302 DTRACE_IO1(start, struct buf *, bp); 9303 offset = ldbtob(bp->b_lblkno); 9304 9305 if (bp->b_flags & B_READ) { 9306 read_again: 9307 /* 9308 * Releases the osp, if it is provided. 9309 * Puts a hold on the cred_otw and the new osp (if found). 9310 */ 9311 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9312 &first_time, &last_time); 9313 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9314 offset, bp->b_bcount, 9315 &bp->b_resid, cred_otw, 9316 readahead, NULL); 9317 crfree(cred_otw); 9318 if (!error) { 9319 if (bp->b_resid) { 9320 /* 9321 * Didn't get it all because we hit EOF, 9322 * zero all the memory beyond the EOF. 9323 */ 9324 /* bzero(rdaddr + */ 9325 bzero(bp->b_un.b_addr + 9326 bp->b_bcount - bp->b_resid, bp->b_resid); 9327 } 9328 mutex_enter(&rp->r_statelock); 9329 if (bp->b_resid == bp->b_bcount && 9330 offset >= rp->r_size) { 9331 /* 9332 * We didn't read anything at all as we are 9333 * past EOF. Return an error indicator back 9334 * but don't destroy the pages (yet). 9335 */ 9336 error = NFS_EOF; 9337 } 9338 mutex_exit(&rp->r_statelock); 9339 } else if (error == EACCES && last_time == FALSE) { 9340 goto read_again; 9341 } 9342 } else { 9343 if (!(rp->r_flags & R4STALE)) { 9344 write_again: 9345 /* 9346 * Releases the osp, if it is provided. 9347 * Puts a hold on the cred_otw and the new 9348 * osp (if found). 9349 */ 9350 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9351 &first_time, &last_time); 9352 mutex_enter(&rp->r_statelock); 9353 count = MIN(bp->b_bcount, rp->r_size - offset); 9354 mutex_exit(&rp->r_statelock); 9355 if (count < 0) 9356 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9357 #ifdef DEBUG 9358 if (count == 0) { 9359 zoneid_t zoneid = getzoneid(); 9360 9361 zcmn_err(zoneid, CE_WARN, 9362 "nfs4_bio: zero length write at %lld", 9363 offset); 9364 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9365 "b_bcount=%ld, file size=%lld", 9366 rp->r_flags, (long)bp->b_bcount, 9367 rp->r_size); 9368 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9369 if (nfs4_bio_do_stop) 9370 debug_enter("nfs4_bio"); 9371 } 9372 #endif 9373 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9374 count, cred_otw, stab_comm); 9375 if (error == EACCES && last_time == FALSE) { 9376 crfree(cred_otw); 9377 goto write_again; 9378 } 9379 bp->b_error = error; 9380 if (error && error != EINTR && 9381 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9382 /* 9383 * Don't print EDQUOT errors on the console. 9384 * Don't print asynchronous EACCES errors. 9385 * Don't print EFBIG errors. 9386 * Print all other write errors. 9387 */ 9388 if (error != EDQUOT && error != EFBIG && 9389 (error != EACCES || 9390 !(bp->b_flags & B_ASYNC))) 9391 nfs4_write_error(bp->b_vp, 9392 error, cred_otw); 9393 /* 9394 * Update r_error and r_flags as appropriate. 9395 * If the error was ESTALE, then mark the 9396 * rnode as not being writeable and save 9397 * the error status. Otherwise, save any 9398 * errors which occur from asynchronous 9399 * page invalidations. Any errors occurring 9400 * from other operations should be saved 9401 * by the caller. 9402 */ 9403 mutex_enter(&rp->r_statelock); 9404 if (error == ESTALE) { 9405 rp->r_flags |= R4STALE; 9406 if (!rp->r_error) 9407 rp->r_error = error; 9408 } else if (!rp->r_error && 9409 (bp->b_flags & 9410 (B_INVAL|B_FORCE|B_ASYNC)) == 9411 (B_INVAL|B_FORCE|B_ASYNC)) { 9412 rp->r_error = error; 9413 } 9414 mutex_exit(&rp->r_statelock); 9415 } 9416 crfree(cred_otw); 9417 } else 9418 error = rp->r_error; 9419 } 9420 9421 if (error != 0 && error != NFS_EOF) 9422 bp->b_flags |= B_ERROR; 9423 9424 if (osp) 9425 open_stream_rele(osp, rp); 9426 9427 DTRACE_IO1(done, struct buf *, bp); 9428 9429 return (error); 9430 } 9431 9432 /* ARGSUSED */ 9433 static int 9434 nfs4_fid(vnode_t *vp, fid_t *fidp) 9435 { 9436 return (EREMOTE); 9437 } 9438 9439 /* ARGSUSED2 */ 9440 static int 9441 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9442 { 9443 rnode4_t *rp = VTOR4(vp); 9444 9445 if (!write_lock) { 9446 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9447 return (V_WRITELOCK_FALSE); 9448 } 9449 9450 if ((rp->r_flags & R4DIRECTIO) || 9451 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9452 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9453 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9454 return (V_WRITELOCK_FALSE); 9455 nfs_rw_exit(&rp->r_rwlock); 9456 } 9457 9458 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9459 return (V_WRITELOCK_TRUE); 9460 } 9461 9462 /* ARGSUSED */ 9463 static void 9464 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9465 { 9466 rnode4_t *rp = VTOR4(vp); 9467 9468 nfs_rw_exit(&rp->r_rwlock); 9469 } 9470 9471 /* ARGSUSED */ 9472 static int 9473 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 9474 { 9475 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9476 return (EIO); 9477 9478 /* 9479 * Because we stuff the readdir cookie into the offset field 9480 * someone may attempt to do an lseek with the cookie which 9481 * we want to succeed. 9482 */ 9483 if (vp->v_type == VDIR) 9484 return (0); 9485 if (*noffp < 0) 9486 return (EINVAL); 9487 return (0); 9488 } 9489 9490 9491 /* 9492 * Return all the pages from [off..off+len) in file 9493 */ 9494 static int 9495 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9496 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9497 enum seg_rw rw, cred_t *cr) 9498 { 9499 rnode4_t *rp; 9500 int error; 9501 mntinfo4_t *mi; 9502 9503 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9504 return (EIO); 9505 rp = VTOR4(vp); 9506 if (IS_SHADOW(vp, rp)) 9507 vp = RTOV4(rp); 9508 9509 if (vp->v_flag & VNOMAP) 9510 return (ENOSYS); 9511 9512 if (protp != NULL) 9513 *protp = PROT_ALL; 9514 9515 /* 9516 * Now validate that the caches are up to date. 9517 */ 9518 if (error = nfs4_validate_caches(vp, cr)) 9519 return (error); 9520 9521 mi = VTOMI4(vp); 9522 retry: 9523 mutex_enter(&rp->r_statelock); 9524 9525 /* 9526 * Don't create dirty pages faster than they 9527 * can be cleaned so that the system doesn't 9528 * get imbalanced. If the async queue is 9529 * maxed out, then wait for it to drain before 9530 * creating more dirty pages. Also, wait for 9531 * any threads doing pagewalks in the vop_getattr 9532 * entry points so that they don't block for 9533 * long periods. 9534 */ 9535 if (rw == S_CREATE) { 9536 while ((mi->mi_max_threads != 0 && 9537 rp->r_awcount > 2 * mi->mi_max_threads) || 9538 rp->r_gcount > 0) 9539 cv_wait(&rp->r_cv, &rp->r_statelock); 9540 } 9541 9542 /* 9543 * If we are getting called as a side effect of an nfs_write() 9544 * operation the local file size might not be extended yet. 9545 * In this case we want to be able to return pages of zeroes. 9546 */ 9547 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9548 NFS4_DEBUG(nfs4_pageio_debug, 9549 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9550 "len=%llu, size=%llu, attrsize =%llu", off, 9551 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9552 mutex_exit(&rp->r_statelock); 9553 return (EFAULT); /* beyond EOF */ 9554 } 9555 9556 mutex_exit(&rp->r_statelock); 9557 9558 if (len <= PAGESIZE) { 9559 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9560 seg, addr, rw, cr); 9561 NFS4_DEBUG(nfs4_pageio_debug && error, 9562 (CE_NOTE, "getpage error %d; off=%lld, " 9563 "len=%lld", error, off, (u_longlong_t)len)); 9564 } else { 9565 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9566 pl, plsz, seg, addr, rw, cr); 9567 NFS4_DEBUG(nfs4_pageio_debug && error, 9568 (CE_NOTE, "getpages error %d; off=%lld, " 9569 "len=%lld", error, off, (u_longlong_t)len)); 9570 } 9571 9572 switch (error) { 9573 case NFS_EOF: 9574 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9575 goto retry; 9576 case ESTALE: 9577 nfs4_purge_stale_fh(error, vp, cr); 9578 } 9579 9580 return (error); 9581 } 9582 9583 /* 9584 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9585 */ 9586 /* ARGSUSED */ 9587 static int 9588 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9589 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9590 enum seg_rw rw, cred_t *cr) 9591 { 9592 rnode4_t *rp; 9593 uint_t bsize; 9594 struct buf *bp; 9595 page_t *pp; 9596 u_offset_t lbn; 9597 u_offset_t io_off; 9598 u_offset_t blkoff; 9599 u_offset_t rablkoff; 9600 size_t io_len; 9601 uint_t blksize; 9602 int error; 9603 int readahead; 9604 int readahead_issued = 0; 9605 int ra_window; /* readahead window */ 9606 page_t *pagefound; 9607 page_t *savepp; 9608 9609 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9610 return (EIO); 9611 9612 rp = VTOR4(vp); 9613 ASSERT(!IS_SHADOW(vp, rp)); 9614 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9615 9616 reread: 9617 bp = NULL; 9618 pp = NULL; 9619 pagefound = NULL; 9620 9621 if (pl != NULL) 9622 pl[0] = NULL; 9623 9624 error = 0; 9625 lbn = off / bsize; 9626 blkoff = lbn * bsize; 9627 9628 /* 9629 * Queueing up the readahead before doing the synchronous read 9630 * results in a significant increase in read throughput because 9631 * of the increased parallelism between the async threads and 9632 * the process context. 9633 */ 9634 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9635 rw != S_CREATE && 9636 !(vp->v_flag & VNOCACHE)) { 9637 mutex_enter(&rp->r_statelock); 9638 9639 /* 9640 * Calculate the number of readaheads to do. 9641 * a) No readaheads at offset = 0. 9642 * b) Do maximum(nfs4_nra) readaheads when the readahead 9643 * window is closed. 9644 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9645 * upon how far the readahead window is open or close. 9646 * d) No readaheads if rp->r_nextr is not within the scope 9647 * of the readahead window (random i/o). 9648 */ 9649 9650 if (off == 0) 9651 readahead = 0; 9652 else if (blkoff == rp->r_nextr) 9653 readahead = nfs4_nra; 9654 else if (rp->r_nextr > blkoff && 9655 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9656 <= (nfs4_nra - 1))) 9657 readahead = nfs4_nra - ra_window; 9658 else 9659 readahead = 0; 9660 9661 rablkoff = rp->r_nextr; 9662 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9663 mutex_exit(&rp->r_statelock); 9664 if (nfs4_async_readahead(vp, rablkoff + bsize, 9665 addr + (rablkoff + bsize - off), 9666 seg, cr, nfs4_readahead) < 0) { 9667 mutex_enter(&rp->r_statelock); 9668 break; 9669 } 9670 readahead--; 9671 rablkoff += bsize; 9672 /* 9673 * Indicate that we did a readahead so 9674 * readahead offset is not updated 9675 * by the synchronous read below. 9676 */ 9677 readahead_issued = 1; 9678 mutex_enter(&rp->r_statelock); 9679 /* 9680 * set readahead offset to 9681 * offset of last async readahead 9682 * request. 9683 */ 9684 rp->r_nextr = rablkoff; 9685 } 9686 mutex_exit(&rp->r_statelock); 9687 } 9688 9689 again: 9690 if ((pagefound = page_exists(vp, off)) == NULL) { 9691 if (pl == NULL) { 9692 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9693 nfs4_readahead); 9694 } else if (rw == S_CREATE) { 9695 /* 9696 * Block for this page is not allocated, or the offset 9697 * is beyond the current allocation size, or we're 9698 * allocating a swap slot and the page was not found, 9699 * so allocate it and return a zero page. 9700 */ 9701 if ((pp = page_create_va(vp, off, 9702 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9703 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9704 io_len = PAGESIZE; 9705 mutex_enter(&rp->r_statelock); 9706 rp->r_nextr = off + PAGESIZE; 9707 mutex_exit(&rp->r_statelock); 9708 } else { 9709 /* 9710 * Need to go to server to get a block 9711 */ 9712 mutex_enter(&rp->r_statelock); 9713 if (blkoff < rp->r_size && 9714 blkoff + bsize > rp->r_size) { 9715 /* 9716 * If less than a block left in 9717 * file read less than a block. 9718 */ 9719 if (rp->r_size <= off) { 9720 /* 9721 * Trying to access beyond EOF, 9722 * set up to get at least one page. 9723 */ 9724 blksize = off + PAGESIZE - blkoff; 9725 } else 9726 blksize = rp->r_size - blkoff; 9727 } else if ((off == 0) || 9728 (off != rp->r_nextr && !readahead_issued)) { 9729 blksize = PAGESIZE; 9730 blkoff = off; /* block = page here */ 9731 } else 9732 blksize = bsize; 9733 mutex_exit(&rp->r_statelock); 9734 9735 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9736 &io_len, blkoff, blksize, 0); 9737 9738 /* 9739 * Some other thread has entered the page, 9740 * so just use it. 9741 */ 9742 if (pp == NULL) 9743 goto again; 9744 9745 /* 9746 * Now round the request size up to page boundaries. 9747 * This ensures that the entire page will be 9748 * initialized to zeroes if EOF is encountered. 9749 */ 9750 io_len = ptob(btopr(io_len)); 9751 9752 bp = pageio_setup(pp, io_len, vp, B_READ); 9753 ASSERT(bp != NULL); 9754 9755 /* 9756 * pageio_setup should have set b_addr to 0. This 9757 * is correct since we want to do I/O on a page 9758 * boundary. bp_mapin will use this addr to calculate 9759 * an offset, and then set b_addr to the kernel virtual 9760 * address it allocated for us. 9761 */ 9762 ASSERT(bp->b_un.b_addr == 0); 9763 9764 bp->b_edev = 0; 9765 bp->b_dev = 0; 9766 bp->b_lblkno = lbtodb(io_off); 9767 bp->b_file = vp; 9768 bp->b_offset = (offset_t)off; 9769 bp_mapin(bp); 9770 9771 /* 9772 * If doing a write beyond what we believe is EOF, 9773 * don't bother trying to read the pages from the 9774 * server, we'll just zero the pages here. We 9775 * don't check that the rw flag is S_WRITE here 9776 * because some implementations may attempt a 9777 * read access to the buffer before copying data. 9778 */ 9779 mutex_enter(&rp->r_statelock); 9780 if (io_off >= rp->r_size && seg == segkmap) { 9781 mutex_exit(&rp->r_statelock); 9782 bzero(bp->b_un.b_addr, io_len); 9783 } else { 9784 mutex_exit(&rp->r_statelock); 9785 error = nfs4_bio(bp, NULL, cr, FALSE); 9786 } 9787 9788 /* 9789 * Unmap the buffer before freeing it. 9790 */ 9791 bp_mapout(bp); 9792 pageio_done(bp); 9793 9794 savepp = pp; 9795 do { 9796 pp->p_fsdata = C_NOCOMMIT; 9797 } while ((pp = pp->p_next) != savepp); 9798 9799 if (error == NFS_EOF) { 9800 /* 9801 * If doing a write system call just return 9802 * zeroed pages, else user tried to get pages 9803 * beyond EOF, return error. We don't check 9804 * that the rw flag is S_WRITE here because 9805 * some implementations may attempt a read 9806 * access to the buffer before copying data. 9807 */ 9808 if (seg == segkmap) 9809 error = 0; 9810 else 9811 error = EFAULT; 9812 } 9813 9814 if (!readahead_issued && !error) { 9815 mutex_enter(&rp->r_statelock); 9816 rp->r_nextr = io_off + io_len; 9817 mutex_exit(&rp->r_statelock); 9818 } 9819 } 9820 } 9821 9822 out: 9823 if (pl == NULL) 9824 return (error); 9825 9826 if (error) { 9827 if (pp != NULL) 9828 pvn_read_done(pp, B_ERROR); 9829 return (error); 9830 } 9831 9832 if (pagefound) { 9833 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 9834 9835 /* 9836 * Page exists in the cache, acquire the appropriate lock. 9837 * If this fails, start all over again. 9838 */ 9839 if ((pp = page_lookup(vp, off, se)) == NULL) { 9840 #ifdef DEBUG 9841 nfs4_lostpage++; 9842 #endif 9843 goto reread; 9844 } 9845 pl[0] = pp; 9846 pl[1] = NULL; 9847 return (0); 9848 } 9849 9850 if (pp != NULL) 9851 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 9852 9853 return (error); 9854 } 9855 9856 static void 9857 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 9858 cred_t *cr) 9859 { 9860 int error; 9861 page_t *pp; 9862 u_offset_t io_off; 9863 size_t io_len; 9864 struct buf *bp; 9865 uint_t bsize, blksize; 9866 rnode4_t *rp = VTOR4(vp); 9867 page_t *savepp; 9868 9869 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9870 9871 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9872 9873 mutex_enter(&rp->r_statelock); 9874 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 9875 /* 9876 * If less than a block left in file read less 9877 * than a block. 9878 */ 9879 blksize = rp->r_size - blkoff; 9880 } else 9881 blksize = bsize; 9882 mutex_exit(&rp->r_statelock); 9883 9884 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 9885 &io_off, &io_len, blkoff, blksize, 1); 9886 /* 9887 * The isra flag passed to the kluster function is 1, we may have 9888 * gotten a return value of NULL for a variety of reasons (# of free 9889 * pages < minfree, someone entered the page on the vnode etc). In all 9890 * cases, we want to punt on the readahead. 9891 */ 9892 if (pp == NULL) 9893 return; 9894 9895 /* 9896 * Now round the request size up to page boundaries. 9897 * This ensures that the entire page will be 9898 * initialized to zeroes if EOF is encountered. 9899 */ 9900 io_len = ptob(btopr(io_len)); 9901 9902 bp = pageio_setup(pp, io_len, vp, B_READ); 9903 ASSERT(bp != NULL); 9904 9905 /* 9906 * pageio_setup should have set b_addr to 0. This is correct since 9907 * we want to do I/O on a page boundary. bp_mapin() will use this addr 9908 * to calculate an offset, and then set b_addr to the kernel virtual 9909 * address it allocated for us. 9910 */ 9911 ASSERT(bp->b_un.b_addr == 0); 9912 9913 bp->b_edev = 0; 9914 bp->b_dev = 0; 9915 bp->b_lblkno = lbtodb(io_off); 9916 bp->b_file = vp; 9917 bp->b_offset = (offset_t)blkoff; 9918 bp_mapin(bp); 9919 9920 /* 9921 * If doing a write beyond what we believe is EOF, don't bother trying 9922 * to read the pages from the server, we'll just zero the pages here. 9923 * We don't check that the rw flag is S_WRITE here because some 9924 * implementations may attempt a read access to the buffer before 9925 * copying data. 9926 */ 9927 mutex_enter(&rp->r_statelock); 9928 if (io_off >= rp->r_size && seg == segkmap) { 9929 mutex_exit(&rp->r_statelock); 9930 bzero(bp->b_un.b_addr, io_len); 9931 error = 0; 9932 } else { 9933 mutex_exit(&rp->r_statelock); 9934 error = nfs4_bio(bp, NULL, cr, TRUE); 9935 if (error == NFS_EOF) 9936 error = 0; 9937 } 9938 9939 /* 9940 * Unmap the buffer before freeing it. 9941 */ 9942 bp_mapout(bp); 9943 pageio_done(bp); 9944 9945 savepp = pp; 9946 do { 9947 pp->p_fsdata = C_NOCOMMIT; 9948 } while ((pp = pp->p_next) != savepp); 9949 9950 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 9951 9952 /* 9953 * In case of error set readahead offset 9954 * to the lowest offset. 9955 * pvn_read_done() calls VN_DISPOSE to destroy the pages 9956 */ 9957 if (error && rp->r_nextr > io_off) { 9958 mutex_enter(&rp->r_statelock); 9959 if (rp->r_nextr > io_off) 9960 rp->r_nextr = io_off; 9961 mutex_exit(&rp->r_statelock); 9962 } 9963 } 9964 9965 /* 9966 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 9967 * If len == 0, do from off to EOF. 9968 * 9969 * The normal cases should be len == 0 && off == 0 (entire vp list) or 9970 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 9971 * (from pageout). 9972 */ 9973 static int 9974 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 9975 { 9976 int error; 9977 rnode4_t *rp; 9978 9979 ASSERT(cr != NULL); 9980 9981 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 9982 return (EIO); 9983 9984 rp = VTOR4(vp); 9985 if (IS_SHADOW(vp, rp)) 9986 vp = RTOV4(rp); 9987 9988 /* 9989 * XXX - Why should this check be made here? 9990 */ 9991 if (vp->v_flag & VNOMAP) 9992 return (ENOSYS); 9993 9994 if (len == 0 && !(flags & B_INVAL) && 9995 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 9996 return (0); 9997 9998 mutex_enter(&rp->r_statelock); 9999 rp->r_count++; 10000 mutex_exit(&rp->r_statelock); 10001 error = nfs4_putpages(vp, off, len, flags, cr); 10002 mutex_enter(&rp->r_statelock); 10003 rp->r_count--; 10004 cv_broadcast(&rp->r_cv); 10005 mutex_exit(&rp->r_statelock); 10006 10007 return (error); 10008 } 10009 10010 /* 10011 * Write out a single page, possibly klustering adjacent dirty pages. 10012 */ 10013 int 10014 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10015 int flags, cred_t *cr) 10016 { 10017 u_offset_t io_off; 10018 u_offset_t lbn_off; 10019 u_offset_t lbn; 10020 size_t io_len; 10021 uint_t bsize; 10022 int error; 10023 rnode4_t *rp; 10024 10025 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10026 ASSERT(pp != NULL); 10027 ASSERT(cr != NULL); 10028 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10029 10030 rp = VTOR4(vp); 10031 ASSERT(rp->r_count > 0); 10032 ASSERT(!IS_SHADOW(vp, rp)); 10033 10034 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10035 lbn = pp->p_offset / bsize; 10036 lbn_off = lbn * bsize; 10037 10038 /* 10039 * Find a kluster that fits in one block, or in 10040 * one page if pages are bigger than blocks. If 10041 * there is less file space allocated than a whole 10042 * page, we'll shorten the i/o request below. 10043 */ 10044 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10045 roundup(bsize, PAGESIZE), flags); 10046 10047 /* 10048 * pvn_write_kluster shouldn't have returned a page with offset 10049 * behind the original page we were given. Verify that. 10050 */ 10051 ASSERT((pp->p_offset / bsize) >= lbn); 10052 10053 /* 10054 * Now pp will have the list of kept dirty pages marked for 10055 * write back. It will also handle invalidation and freeing 10056 * of pages that are not dirty. Check for page length rounding 10057 * problems. 10058 */ 10059 if (io_off + io_len > lbn_off + bsize) { 10060 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10061 io_len = lbn_off + bsize - io_off; 10062 } 10063 /* 10064 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10065 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10066 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10067 * progress and the r_size has not been made consistent with the 10068 * new size of the file. When the uiomove() completes the r_size is 10069 * updated and the R4MODINPROGRESS flag is cleared. 10070 * 10071 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10072 * consistent value of r_size. Without this handshaking, it is 10073 * possible that nfs4_bio() picks up the old value of r_size 10074 * before the uiomove() in writerp4() completes. This will result 10075 * in the write through nfs4_bio() being dropped. 10076 * 10077 * More precisely, there is a window between the time the uiomove() 10078 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10079 * operation intervenes in this window, the page will be picked up, 10080 * because it is dirty (it will be unlocked, unless it was 10081 * pagecreate'd). When the page is picked up as dirty, the dirty 10082 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10083 * checked. This will still be the old size. Therefore the page will 10084 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10085 * the page will be found to be clean and the write will be dropped. 10086 */ 10087 if (rp->r_flags & R4MODINPROGRESS) { 10088 mutex_enter(&rp->r_statelock); 10089 if ((rp->r_flags & R4MODINPROGRESS) && 10090 rp->r_modaddr + MAXBSIZE > io_off && 10091 rp->r_modaddr < io_off + io_len) { 10092 page_t *plist; 10093 /* 10094 * A write is in progress for this region of the file. 10095 * If we did not detect R4MODINPROGRESS here then this 10096 * path through nfs_putapage() would eventually go to 10097 * nfs4_bio() and may not write out all of the data 10098 * in the pages. We end up losing data. So we decide 10099 * to set the modified bit on each page in the page 10100 * list and mark the rnode with R4DIRTY. This write 10101 * will be restarted at some later time. 10102 */ 10103 plist = pp; 10104 while (plist != NULL) { 10105 pp = plist; 10106 page_sub(&plist, pp); 10107 hat_setmod(pp); 10108 page_io_unlock(pp); 10109 page_unlock(pp); 10110 } 10111 rp->r_flags |= R4DIRTY; 10112 mutex_exit(&rp->r_statelock); 10113 if (offp) 10114 *offp = io_off; 10115 if (lenp) 10116 *lenp = io_len; 10117 return (0); 10118 } 10119 mutex_exit(&rp->r_statelock); 10120 } 10121 10122 if (flags & B_ASYNC) { 10123 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10124 nfs4_sync_putapage); 10125 } else 10126 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10127 10128 if (offp) 10129 *offp = io_off; 10130 if (lenp) 10131 *lenp = io_len; 10132 return (error); 10133 } 10134 10135 static int 10136 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10137 int flags, cred_t *cr) 10138 { 10139 int error; 10140 rnode4_t *rp; 10141 10142 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10143 10144 flags |= B_WRITE; 10145 10146 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10147 10148 rp = VTOR4(vp); 10149 10150 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10151 error == EACCES) && 10152 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10153 if (!(rp->r_flags & R4OUTOFSPACE)) { 10154 mutex_enter(&rp->r_statelock); 10155 rp->r_flags |= R4OUTOFSPACE; 10156 mutex_exit(&rp->r_statelock); 10157 } 10158 flags |= B_ERROR; 10159 pvn_write_done(pp, flags); 10160 /* 10161 * If this was not an async thread, then try again to 10162 * write out the pages, but this time, also destroy 10163 * them whether or not the write is successful. This 10164 * will prevent memory from filling up with these 10165 * pages and destroying them is the only alternative 10166 * if they can't be written out. 10167 * 10168 * Don't do this if this is an async thread because 10169 * when the pages are unlocked in pvn_write_done, 10170 * some other thread could have come along, locked 10171 * them, and queued for an async thread. It would be 10172 * possible for all of the async threads to be tied 10173 * up waiting to lock the pages again and they would 10174 * all already be locked and waiting for an async 10175 * thread to handle them. Deadlock. 10176 */ 10177 if (!(flags & B_ASYNC)) { 10178 error = nfs4_putpage(vp, io_off, io_len, 10179 B_INVAL | B_FORCE, cr); 10180 } 10181 } else { 10182 if (error) 10183 flags |= B_ERROR; 10184 else if (rp->r_flags & R4OUTOFSPACE) { 10185 mutex_enter(&rp->r_statelock); 10186 rp->r_flags &= ~R4OUTOFSPACE; 10187 mutex_exit(&rp->r_statelock); 10188 } 10189 pvn_write_done(pp, flags); 10190 if (freemem < desfree) 10191 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10192 NFS4_WRITE_NOWAIT); 10193 } 10194 10195 return (error); 10196 } 10197 10198 #ifdef DEBUG 10199 int nfs4_force_open_before_mmap = 0; 10200 #endif 10201 10202 static int 10203 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10204 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10205 { 10206 struct segvn_crargs vn_a; 10207 int error = 0; 10208 rnode4_t *rp = VTOR4(vp); 10209 mntinfo4_t *mi = VTOMI4(vp); 10210 10211 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10212 return (EIO); 10213 10214 if (vp->v_flag & VNOMAP) 10215 return (ENOSYS); 10216 10217 if (off < 0 || (off + len) < 0) 10218 return (ENXIO); 10219 10220 if (vp->v_type != VREG) 10221 return (ENODEV); 10222 10223 /* 10224 * If the file is delegated to the client don't do anything. 10225 * If the file is not delegated, then validate the data cache. 10226 */ 10227 mutex_enter(&rp->r_statev4_lock); 10228 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10229 mutex_exit(&rp->r_statev4_lock); 10230 error = nfs4_validate_caches(vp, cr); 10231 if (error) 10232 return (error); 10233 } else { 10234 mutex_exit(&rp->r_statev4_lock); 10235 } 10236 10237 /* 10238 * Check to see if the vnode is currently marked as not cachable. 10239 * This means portions of the file are locked (through VOP_FRLOCK). 10240 * In this case the map request must be refused. We use 10241 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10242 */ 10243 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 10244 return (EINTR); 10245 10246 if (vp->v_flag & VNOCACHE) { 10247 error = EAGAIN; 10248 goto done; 10249 } 10250 10251 /* 10252 * Don't allow concurrent locks and mapping if mandatory locking is 10253 * enabled. 10254 */ 10255 if (flk_has_remote_locks(vp)) { 10256 struct vattr va; 10257 va.va_mask = AT_MODE; 10258 error = nfs4getattr(vp, &va, cr); 10259 if (error != 0) 10260 goto done; 10261 if (MANDLOCK(vp, va.va_mode)) { 10262 error = EAGAIN; 10263 goto done; 10264 } 10265 } 10266 10267 /* 10268 * It is possible that the rnode has a lost lock request that we 10269 * are still trying to recover, and that the request conflicts with 10270 * this map request. 10271 * 10272 * An alternative approach would be for nfs4_safemap() to consider 10273 * queued lock requests when deciding whether to set or clear 10274 * VNOCACHE. This would require the frlock code path to call 10275 * nfs4_safemap() after enqueing a lost request. 10276 */ 10277 if (nfs4_map_lost_lock_conflict(vp)) { 10278 error = EAGAIN; 10279 goto done; 10280 } 10281 10282 as_rangelock(as); 10283 if (!(flags & MAP_FIXED)) { 10284 map_addr(addrp, len, off, 1, flags); 10285 if (*addrp == NULL) { 10286 as_rangeunlock(as); 10287 error = ENOMEM; 10288 goto done; 10289 } 10290 } else { 10291 /* 10292 * User specified address - blow away any previous mappings 10293 */ 10294 (void) as_unmap(as, *addrp, len); 10295 } 10296 10297 if (vp->v_type == VREG) { 10298 /* 10299 * We need to retrieve the open stream 10300 */ 10301 nfs4_open_stream_t *osp = NULL; 10302 nfs4_open_owner_t *oop = NULL; 10303 10304 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10305 if (oop != NULL) { 10306 /* returns with 'os_sync_lock' held */ 10307 osp = find_open_stream(oop, rp); 10308 open_owner_rele(oop); 10309 } 10310 if (osp == NULL) { 10311 #ifdef DEBUG 10312 if (nfs4_force_open_before_mmap) { 10313 error = EIO; 10314 goto done; 10315 } 10316 #endif 10317 /* returns with 'os_sync_lock' held */ 10318 osp = open_and_get_osp(vp, cr, mi); 10319 if (osp == NULL) { 10320 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10321 "nfs4_map: we tried to OPEN the file " 10322 "but again no osp, so fail with EIO")); 10323 error = EIO; 10324 goto done; 10325 } 10326 } 10327 10328 if (osp->os_failed_reopen) { 10329 mutex_exit(&osp->os_sync_lock); 10330 open_stream_rele(osp, rp); 10331 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10332 "nfs4_map: os_failed_reopen set on " 10333 "osp %p, cr %p, rp %s", (void *)osp, 10334 (void *)cr, rnode4info(rp))); 10335 error = EIO; 10336 goto done; 10337 } 10338 mutex_exit(&osp->os_sync_lock); 10339 open_stream_rele(osp, rp); 10340 } 10341 10342 vn_a.vp = vp; 10343 vn_a.offset = off; 10344 vn_a.type = (flags & MAP_TYPE); 10345 vn_a.prot = (uchar_t)prot; 10346 vn_a.maxprot = (uchar_t)maxprot; 10347 vn_a.flags = (flags & ~MAP_TYPE); 10348 vn_a.cred = cr; 10349 vn_a.amp = NULL; 10350 vn_a.szc = 0; 10351 vn_a.lgrp_mem_policy_flags = 0; 10352 10353 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10354 as_rangeunlock(as); 10355 10356 done: 10357 nfs_rw_exit(&rp->r_lkserlock); 10358 return (error); 10359 } 10360 10361 /* 10362 * We're most likely dealing with a kernel module that likes to READ 10363 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10364 * officially OPEN the file to create the necessary client state 10365 * for bookkeeping of os_mmap_read/write counts. 10366 * 10367 * Since VOP_MAP only passes in a pointer to the vnode rather than 10368 * a double pointer, we can't handle the case where nfs4open_otw() 10369 * returns a different vnode than the one passed into VOP_MAP (since 10370 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10371 * we return NULL and let nfs4_map() fail. Note: the only case where 10372 * this should happen is if the file got removed and replaced with the 10373 * same name on the server (in addition to the fact that we're trying 10374 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10375 */ 10376 static nfs4_open_stream_t * 10377 open_and_get_osp(vnode_t *map_vp, cred_t *cr, mntinfo4_t *mi) 10378 { 10379 rnode4_t *rp, *drp; 10380 vnode_t *dvp, *open_vp; 10381 char *file_name; 10382 int just_created; 10383 nfs4_sharedfh_t *sfh; 10384 nfs4_open_stream_t *osp; 10385 nfs4_open_owner_t *oop; 10386 10387 open_vp = map_vp; 10388 sfh = (open_vp->v_flag & VROOT) ? mi->mi_srvparentfh : 10389 VTOSV(open_vp)->sv_dfh; 10390 drp = r4find_unlocked(sfh, open_vp->v_vfsp); 10391 if (!drp) 10392 return (NULL); 10393 10394 file_name = fn_name(VTOSV(open_vp)->sv_name); 10395 10396 rp = VTOR4(open_vp); 10397 dvp = RTOV4(drp); 10398 mutex_enter(&rp->r_statev4_lock); 10399 if (rp->created_v4) { 10400 rp->created_v4 = 0; 10401 mutex_exit(&rp->r_statev4_lock); 10402 10403 dnlc_update(dvp, file_name, open_vp); 10404 /* This is needed so we don't bump the open ref count */ 10405 just_created = 1; 10406 } else { 10407 mutex_exit(&rp->r_statev4_lock); 10408 just_created = 0; 10409 } 10410 10411 VN_HOLD(map_vp); 10412 10413 if (nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10414 just_created)) { 10415 kmem_free(file_name, MAXNAMELEN); 10416 VN_RELE(dvp); 10417 VN_RELE(map_vp); 10418 return (NULL); 10419 } 10420 10421 kmem_free(file_name, MAXNAMELEN); 10422 VN_RELE(dvp); 10423 10424 /* 10425 * If nfs4open_otw() returned a different vnode then "undo" 10426 * the open and return failure to the caller. 10427 */ 10428 if (!VN_CMP(open_vp, map_vp)) { 10429 nfs4_error_t e; 10430 10431 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10432 "open returned a different vnode")); 10433 /* 10434 * If there's an error, ignore it, 10435 * and let VOP_INACTIVE handle it. 10436 */ 10437 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10438 CLOSE_NORM, 0, 0, 0); 10439 VN_RELE(map_vp); 10440 return (NULL); 10441 } 10442 10443 VN_RELE(map_vp); 10444 10445 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10446 if (!oop) { 10447 nfs4_error_t e; 10448 10449 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10450 "no open owner")); 10451 /* 10452 * If there's an error, ignore it, 10453 * and let VOP_INACTIVE handle it. 10454 */ 10455 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10456 CLOSE_NORM, 0, 0, 0); 10457 return (NULL); 10458 } 10459 osp = find_open_stream(oop, rp); 10460 open_owner_rele(oop); 10461 return (osp); 10462 } 10463 10464 /* 10465 * Please be aware that when this function is called, the address space write 10466 * a_lock is held. Do not put over the wire calls in this function. 10467 */ 10468 /* ARGSUSED */ 10469 static int 10470 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10471 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10472 { 10473 rnode4_t *rp; 10474 int error = 0; 10475 mntinfo4_t *mi; 10476 10477 mi = VTOMI4(vp); 10478 rp = VTOR4(vp); 10479 10480 if (nfs_zone() != mi->mi_zone) 10481 return (EIO); 10482 if (vp->v_flag & VNOMAP) 10483 return (ENOSYS); 10484 10485 /* 10486 * Need to hold rwlock while incrementing the mapcnt so that 10487 * mmap'ing can be serialized with writes so that the caching 10488 * can be handled correctly. 10489 * 10490 * Don't need to update the open stream first, since this 10491 * mmap can't add any additional share access that isn't 10492 * already contained in the open stream (for the case where we 10493 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10494 * take into account os_mmap_read[write] counts). 10495 */ 10496 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 10497 return (EINTR); 10498 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10499 nfs_rw_exit(&rp->r_rwlock); 10500 10501 if (vp->v_type == VREG) { 10502 /* 10503 * We need to retrieve the open stream and update the counts. 10504 * If there is no open stream here, something is wrong. 10505 */ 10506 nfs4_open_stream_t *osp = NULL; 10507 nfs4_open_owner_t *oop = NULL; 10508 10509 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10510 if (oop != NULL) { 10511 /* returns with 'os_sync_lock' held */ 10512 osp = find_open_stream(oop, rp); 10513 open_owner_rele(oop); 10514 } 10515 if (osp == NULL) { 10516 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10517 "nfs4_addmap: we should have an osp" 10518 "but we don't, so fail with EIO")); 10519 error = EIO; 10520 goto out; 10521 } 10522 10523 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10524 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10525 10526 /* 10527 * Update the map count in the open stream. 10528 * This is necessary in the case where we 10529 * open/mmap/close/, then the server reboots, and we 10530 * attempt to reopen. If the mmap doesn't add share 10531 * access then we send an invalid reopen with 10532 * access = NONE. 10533 * 10534 * We need to specifically check each PROT_* so a mmap 10535 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10536 * read and write access. A simple comparison of prot 10537 * to ~PROT_WRITE to determine read access is insufficient 10538 * since prot can be |= with PROT_USER, etc. 10539 */ 10540 10541 /* 10542 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10543 */ 10544 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10545 osp->os_mmap_write += btopr(len); 10546 if (maxprot & PROT_READ) 10547 osp->os_mmap_read += btopr(len); 10548 if (maxprot & PROT_EXEC) 10549 osp->os_mmap_read += btopr(len); 10550 /* 10551 * Ensure that os_mmap_read gets incremented, even if 10552 * maxprot were to look like PROT_NONE. 10553 */ 10554 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10555 !(maxprot & PROT_EXEC)) 10556 osp->os_mmap_read += btopr(len); 10557 osp->os_mapcnt += btopr(len); 10558 mutex_exit(&osp->os_sync_lock); 10559 open_stream_rele(osp, rp); 10560 } 10561 10562 out: 10563 /* 10564 * If we got an error, then undo our 10565 * incrementing of 'r_mapcnt'. 10566 */ 10567 10568 if (error) { 10569 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10570 ASSERT(rp->r_mapcnt >= 0); 10571 } 10572 return (error); 10573 } 10574 10575 static int 10576 nfs4_cmp(vnode_t *vp1, vnode_t *vp2) 10577 { 10578 10579 return (VTOR4(vp1) == VTOR4(vp2)); 10580 } 10581 10582 static int 10583 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10584 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr) 10585 { 10586 int rc; 10587 u_offset_t start, end; 10588 rnode4_t *rp; 10589 int error = 0, intr = INTR4(vp); 10590 nfs4_error_t e; 10591 10592 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10593 return (EIO); 10594 10595 /* check for valid cmd parameter */ 10596 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10597 return (EINVAL); 10598 10599 /* Verify l_type. */ 10600 switch (bfp->l_type) { 10601 case F_RDLCK: 10602 if (cmd != F_GETLK && !(flag & FREAD)) 10603 return (EBADF); 10604 break; 10605 case F_WRLCK: 10606 if (cmd != F_GETLK && !(flag & FWRITE)) 10607 return (EBADF); 10608 break; 10609 case F_UNLCK: 10610 intr = 0; 10611 break; 10612 10613 default: 10614 return (EINVAL); 10615 } 10616 10617 /* check the validity of the lock range */ 10618 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10619 return (rc); 10620 if (rc = flk_check_lock_data(start, end, MAXEND)) 10621 return (rc); 10622 10623 /* 10624 * If the filesystem is mounted using local locking, pass the 10625 * request off to the local locking code. 10626 */ 10627 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10628 if (cmd == F_SETLK || cmd == F_SETLKW) { 10629 /* 10630 * For complete safety, we should be holding 10631 * r_lkserlock. However, we can't call 10632 * nfs4_safelock and then fs_frlock while 10633 * holding r_lkserlock, so just invoke 10634 * nfs4_safelock and expect that this will 10635 * catch enough of the cases. 10636 */ 10637 if (!nfs4_safelock(vp, bfp, cr)) 10638 return (EAGAIN); 10639 } 10640 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 10641 } 10642 10643 rp = VTOR4(vp); 10644 10645 /* 10646 * Check whether the given lock request can proceed, given the 10647 * current file mappings. 10648 */ 10649 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10650 return (EINTR); 10651 if (cmd == F_SETLK || cmd == F_SETLKW) { 10652 if (!nfs4_safelock(vp, bfp, cr)) { 10653 rc = EAGAIN; 10654 goto done; 10655 } 10656 } 10657 10658 /* 10659 * Flush the cache after waiting for async I/O to finish. For new 10660 * locks, this is so that the process gets the latest bits from the 10661 * server. For unlocks, this is so that other clients see the 10662 * latest bits once the file has been unlocked. If currently dirty 10663 * pages can't be flushed, then don't allow a lock to be set. But 10664 * allow unlocks to succeed, to avoid having orphan locks on the 10665 * server. 10666 */ 10667 if (cmd != F_GETLK) { 10668 mutex_enter(&rp->r_statelock); 10669 while (rp->r_count > 0) { 10670 if (intr) { 10671 klwp_t *lwp = ttolwp(curthread); 10672 10673 if (lwp != NULL) 10674 lwp->lwp_nostop++; 10675 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock) == 0) { 10676 if (lwp != NULL) 10677 lwp->lwp_nostop--; 10678 rc = EINTR; 10679 break; 10680 } 10681 if (lwp != NULL) 10682 lwp->lwp_nostop--; 10683 } else 10684 cv_wait(&rp->r_cv, &rp->r_statelock); 10685 } 10686 mutex_exit(&rp->r_statelock); 10687 if (rc != 0) 10688 goto done; 10689 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr); 10690 if (error) { 10691 if (error == ENOSPC || error == EDQUOT) { 10692 mutex_enter(&rp->r_statelock); 10693 if (!rp->r_error) 10694 rp->r_error = error; 10695 mutex_exit(&rp->r_statelock); 10696 } 10697 if (bfp->l_type != F_UNLCK) { 10698 rc = ENOLCK; 10699 goto done; 10700 } 10701 } 10702 } 10703 10704 /* 10705 * Call the lock manager to do the real work of contacting 10706 * the server and obtaining the lock. 10707 */ 10708 10709 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10710 cr, &e, NULL, NULL); 10711 rc = e.error; 10712 10713 if (rc == 0) 10714 nfs4_lockcompletion(vp, cmd); 10715 10716 done: 10717 nfs_rw_exit(&rp->r_lkserlock); 10718 10719 return (rc); 10720 } 10721 10722 /* 10723 * Free storage space associated with the specified vnode. The portion 10724 * to be freed is specified by bfp->l_start and bfp->l_len (already 10725 * normalized to a "whence" of 0). 10726 * 10727 * This is an experimental facility whose continued existence is not 10728 * guaranteed. Currently, we only support the special case 10729 * of l_len == 0, meaning free to end of file. 10730 */ 10731 /* ARGSUSED */ 10732 static int 10733 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10734 offset_t offset, cred_t *cr, caller_context_t *ct) 10735 { 10736 int error; 10737 10738 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10739 return (EIO); 10740 ASSERT(vp->v_type == VREG); 10741 if (cmd != F_FREESP) 10742 return (EINVAL); 10743 10744 error = convoff(vp, bfp, 0, offset); 10745 if (!error) { 10746 ASSERT(bfp->l_start >= 0); 10747 if (bfp->l_len == 0) { 10748 struct vattr va; 10749 10750 va.va_mask = AT_SIZE; 10751 va.va_size = bfp->l_start; 10752 error = nfs4setattr(vp, &va, 0, cr, NULL); 10753 } else 10754 error = EINVAL; 10755 } 10756 10757 return (error); 10758 } 10759 10760 /* ARGSUSED */ 10761 static int 10762 nfs4_realvp(vnode_t *vp, vnode_t **vpp) 10763 { 10764 return (EINVAL); 10765 } 10766 10767 /* 10768 * Setup and add an address space callback to do the work of the delmap call. 10769 * The callback will (and must be) deleted in the actual callback function. 10770 * 10771 * This is done in order to take care of the problem that we have with holding 10772 * the address space's a_lock for a long period of time (e.g. if the NFS server 10773 * is down). Callbacks will be executed in the address space code while the 10774 * a_lock is not held. Holding the address space's a_lock causes things such 10775 * as ps and fork to hang because they are trying to acquire this lock as well. 10776 */ 10777 /* ARGSUSED */ 10778 static int 10779 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10780 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 10781 { 10782 int caller_found; 10783 int error; 10784 rnode4_t *rp; 10785 nfs4_delmap_args_t *dmapp; 10786 nfs4_delmapcall_t *delmap_call; 10787 10788 if (vp->v_flag & VNOMAP) 10789 return (ENOSYS); 10790 10791 /* 10792 * A process may not change zones if it has NFS pages mmap'ed 10793 * in, so we can't legitimately get here from the wrong zone. 10794 */ 10795 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10796 10797 rp = VTOR4(vp); 10798 10799 /* 10800 * The way that the address space of this process deletes its mapping 10801 * of this file is via the following call chains: 10802 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10803 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10804 * 10805 * With the use of address space callbacks we are allowed to drop the 10806 * address space lock, a_lock, while executing the NFS operations that 10807 * need to go over the wire. Returning EAGAIN to the caller of this 10808 * function is what drives the execution of the callback that we add 10809 * below. The callback will be executed by the address space code 10810 * after dropping the a_lock. When the callback is finished, since 10811 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 10812 * is called again on the same segment to finish the rest of the work 10813 * that needs to happen during unmapping. 10814 * 10815 * This action of calling back into the segment driver causes 10816 * nfs4_delmap() to get called again, but since the callback was 10817 * already executed at this point, it already did the work and there 10818 * is nothing left for us to do. 10819 * 10820 * To Summarize: 10821 * - The first time nfs4_delmap is called by the current thread is when 10822 * we add the caller associated with this delmap to the delmap caller 10823 * list, add the callback, and return EAGAIN. 10824 * - The second time in this call chain when nfs4_delmap is called we 10825 * will find this caller in the delmap caller list and realize there 10826 * is no more work to do thus removing this caller from the list and 10827 * returning the error that was set in the callback execution. 10828 */ 10829 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 10830 if (caller_found) { 10831 /* 10832 * 'error' is from the actual delmap operations. To avoid 10833 * hangs, we need to handle the return of EAGAIN differently 10834 * since this is what drives the callback execution. 10835 * In this case, we don't want to return EAGAIN and do the 10836 * callback execution because there are none to execute. 10837 */ 10838 if (error == EAGAIN) 10839 return (0); 10840 else 10841 return (error); 10842 } 10843 10844 /* current caller was not in the list */ 10845 delmap_call = nfs4_init_delmapcall(); 10846 10847 mutex_enter(&rp->r_statelock); 10848 list_insert_tail(&rp->r_indelmap, delmap_call); 10849 mutex_exit(&rp->r_statelock); 10850 10851 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 10852 10853 dmapp->vp = vp; 10854 dmapp->off = off; 10855 dmapp->addr = addr; 10856 dmapp->len = len; 10857 dmapp->prot = prot; 10858 dmapp->maxprot = maxprot; 10859 dmapp->flags = flags; 10860 dmapp->cr = cr; 10861 dmapp->caller = delmap_call; 10862 10863 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 10864 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 10865 10866 return (error ? error : EAGAIN); 10867 } 10868 10869 static nfs4_delmapcall_t * 10870 nfs4_init_delmapcall() 10871 { 10872 nfs4_delmapcall_t *delmap_call; 10873 10874 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 10875 delmap_call->call_id = curthread; 10876 delmap_call->error = 0; 10877 10878 return (delmap_call); 10879 } 10880 10881 static void 10882 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 10883 { 10884 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 10885 } 10886 10887 /* 10888 * Searches for the current delmap caller (based on curthread) in the list of 10889 * callers. If it is found, we remove it and free the delmap caller. 10890 * Returns: 10891 * 0 if the caller wasn't found 10892 * 1 if the caller was found, removed and freed. *errp will be set 10893 * to what the result of the delmap was. 10894 */ 10895 static int 10896 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 10897 { 10898 nfs4_delmapcall_t *delmap_call; 10899 10900 /* 10901 * If the list doesn't exist yet, we create it and return 10902 * that the caller wasn't found. No list = no callers. 10903 */ 10904 mutex_enter(&rp->r_statelock); 10905 if (!(rp->r_flags & R4DELMAPLIST)) { 10906 /* The list does not exist */ 10907 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 10908 offsetof(nfs4_delmapcall_t, call_node)); 10909 rp->r_flags |= R4DELMAPLIST; 10910 mutex_exit(&rp->r_statelock); 10911 return (0); 10912 } else { 10913 /* The list exists so search it */ 10914 for (delmap_call = list_head(&rp->r_indelmap); 10915 delmap_call != NULL; 10916 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 10917 if (delmap_call->call_id == curthread) { 10918 /* current caller is in the list */ 10919 *errp = delmap_call->error; 10920 list_remove(&rp->r_indelmap, delmap_call); 10921 mutex_exit(&rp->r_statelock); 10922 nfs4_free_delmapcall(delmap_call); 10923 return (1); 10924 } 10925 } 10926 } 10927 mutex_exit(&rp->r_statelock); 10928 return (0); 10929 } 10930 10931 /* 10932 * Remove some pages from an mmap'd vnode. Just update the 10933 * count of pages. If doing close-to-open, then flush and 10934 * commit all of the pages associated with this file. 10935 * Otherwise, start an asynchronous page flush to write out 10936 * any dirty pages. This will also associate a credential 10937 * with the rnode which can be used to write the pages. 10938 */ 10939 /* ARGSUSED */ 10940 static void 10941 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 10942 { 10943 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 10944 rnode4_t *rp; 10945 mntinfo4_t *mi; 10946 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 10947 10948 rp = VTOR4(dmapp->vp); 10949 mi = VTOMI4(dmapp->vp); 10950 10951 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 10952 ASSERT(rp->r_mapcnt >= 0); 10953 10954 /* 10955 * Initiate a page flush and potential commit if there are 10956 * pages, the file system was not mounted readonly, the segment 10957 * was mapped shared, and the pages themselves were writeable. 10958 */ 10959 if (nfs4_has_pages(dmapp->vp) && 10960 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 10961 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 10962 mutex_enter(&rp->r_statelock); 10963 rp->r_flags |= R4DIRTY; 10964 mutex_exit(&rp->r_statelock); 10965 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 10966 dmapp->len, dmapp->cr); 10967 if (!e.error) { 10968 mutex_enter(&rp->r_statelock); 10969 e.error = rp->r_error; 10970 rp->r_error = 0; 10971 mutex_exit(&rp->r_statelock); 10972 } 10973 } else 10974 e.error = 0; 10975 10976 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 10977 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 10978 B_INVAL, dmapp->cr); 10979 10980 if (e.error) { 10981 e.stat = puterrno4(e.error); 10982 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 10983 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 10984 dmapp->caller->error = e.error; 10985 } 10986 10987 /* Check to see if we need to close the file */ 10988 10989 if (dmapp->vp->v_type == VREG) { 10990 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 10991 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 10992 10993 if (e.error != 0 || e.stat != NFS4_OK) { 10994 /* 10995 * Since it is possible that e.error == 0 and 10996 * e.stat != NFS4_OK (and vice versa), 10997 * we do the proper checking in order to get both 10998 * e.error and e.stat reporting the correct info. 10999 */ 11000 if (e.stat == NFS4_OK) 11001 e.stat = puterrno4(e.error); 11002 if (e.error == 0) 11003 e.error = geterrno4(e.stat); 11004 11005 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11006 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11007 dmapp->caller->error = e.error; 11008 } 11009 } 11010 11011 (void) as_delete_callback(as, arg); 11012 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11013 } 11014 11015 11016 static uint_t 11017 fattr4_maxfilesize_to_bits(uint64_t ll) 11018 { 11019 uint_t l = 1; 11020 11021 if (ll == 0) { 11022 return (0); 11023 } 11024 11025 if (ll & 0xffffffff00000000) { 11026 l += 32; ll >>= 32; 11027 } 11028 if (ll & 0xffff0000) { 11029 l += 16; ll >>= 16; 11030 } 11031 if (ll & 0xff00) { 11032 l += 8; ll >>= 8; 11033 } 11034 if (ll & 0xf0) { 11035 l += 4; ll >>= 4; 11036 } 11037 if (ll & 0xc) { 11038 l += 2; ll >>= 2; 11039 } 11040 if (ll & 0x2) { 11041 l += 1; 11042 } 11043 return (l); 11044 } 11045 11046 static int 11047 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 11048 { 11049 int error; 11050 hrtime_t t; 11051 rnode4_t *rp; 11052 nfs4_ga_res_t gar; 11053 nfs4_ga_ext_res_t ger; 11054 11055 gar.n4g_ext_res = &ger; 11056 11057 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11058 return (EIO); 11059 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11060 *valp = MAXPATHLEN; 11061 return (0); 11062 } 11063 if (cmd == _PC_ACL_ENABLED) { 11064 *valp = _ACL_ACE_ENABLED; 11065 return (0); 11066 } 11067 11068 rp = VTOR4(vp); 11069 if (cmd == _PC_XATTR_EXISTS) { 11070 /* 11071 * Eventually should attempt small client readdir before 11072 * going otw with GETATTR(FATTR4_NAMED_ATTR). For now 11073 * just drive the OTW getattr. This is required because 11074 * _PC_XATTR_EXISTS can only return true if attributes 11075 * exist -- simply checking for existance of the attrdir 11076 * is not sufficient. 11077 * 11078 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11079 * is NULL. Once the xadir vp exists, we can create xattrs, 11080 * and we don't have any way to update the "base" object's 11081 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11082 * could help out. 11083 */ 11084 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11085 rp->r_xattr_dir == NULL) { 11086 *valp = rp->r_pathconf.pc4_xattr_exists; 11087 return (0); 11088 } 11089 } else { /* OLD CODE */ 11090 if (ATTRCACHE4_VALID(vp)) { 11091 mutex_enter(&rp->r_statelock); 11092 if (rp->r_pathconf.pc4_cache_valid) { 11093 error = 0; 11094 switch (cmd) { 11095 case _PC_FILESIZEBITS: 11096 *valp = 11097 rp->r_pathconf.pc4_filesizebits; 11098 break; 11099 case _PC_LINK_MAX: 11100 *valp = 11101 rp->r_pathconf.pc4_link_max; 11102 break; 11103 case _PC_NAME_MAX: 11104 *valp = 11105 rp->r_pathconf.pc4_name_max; 11106 break; 11107 case _PC_CHOWN_RESTRICTED: 11108 *valp = 11109 rp->r_pathconf.pc4_chown_restricted; 11110 break; 11111 case _PC_NO_TRUNC: 11112 *valp = 11113 rp->r_pathconf.pc4_no_trunc; 11114 break; 11115 default: 11116 error = EINVAL; 11117 break; 11118 } 11119 mutex_exit(&rp->r_statelock); 11120 #ifdef DEBUG 11121 nfs4_pathconf_cache_hits++; 11122 #endif 11123 return (error); 11124 } 11125 mutex_exit(&rp->r_statelock); 11126 } 11127 } 11128 #ifdef DEBUG 11129 nfs4_pathconf_cache_misses++; 11130 #endif 11131 11132 t = gethrtime(); 11133 11134 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11135 11136 if (error) { 11137 mutex_enter(&rp->r_statelock); 11138 rp->r_pathconf.pc4_cache_valid = FALSE; 11139 rp->r_pathconf.pc4_xattr_valid = FALSE; 11140 mutex_exit(&rp->r_statelock); 11141 return (error); 11142 } 11143 11144 /* interpret the max filesize */ 11145 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11146 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11147 11148 /* Store the attributes we just received */ 11149 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11150 11151 switch (cmd) { 11152 case _PC_FILESIZEBITS: 11153 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11154 break; 11155 case _PC_LINK_MAX: 11156 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11157 break; 11158 case _PC_NAME_MAX: 11159 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11160 break; 11161 case _PC_CHOWN_RESTRICTED: 11162 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11163 break; 11164 case _PC_NO_TRUNC: 11165 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11166 break; 11167 case _PC_XATTR_EXISTS: 11168 *valp = gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists; 11169 break; 11170 default: 11171 return (EINVAL); 11172 } 11173 11174 return (0); 11175 } 11176 11177 /* 11178 * Called by async thread to do synchronous pageio. Do the i/o, wait 11179 * for it to complete, and cleanup the page list when done. 11180 */ 11181 static int 11182 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11183 int flags, cred_t *cr) 11184 { 11185 int error; 11186 11187 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11188 11189 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11190 if (flags & B_READ) 11191 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11192 else 11193 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11194 return (error); 11195 } 11196 11197 static int 11198 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11199 int flags, cred_t *cr) 11200 { 11201 int error; 11202 rnode4_t *rp; 11203 11204 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11205 return (EIO); 11206 11207 if (pp == NULL) 11208 return (EINVAL); 11209 11210 rp = VTOR4(vp); 11211 mutex_enter(&rp->r_statelock); 11212 rp->r_count++; 11213 mutex_exit(&rp->r_statelock); 11214 11215 if (flags & B_ASYNC) { 11216 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11217 nfs4_sync_pageio); 11218 } else 11219 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11220 mutex_enter(&rp->r_statelock); 11221 rp->r_count--; 11222 cv_broadcast(&rp->r_cv); 11223 mutex_exit(&rp->r_statelock); 11224 return (error); 11225 } 11226 11227 static void 11228 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr) 11229 { 11230 int error; 11231 rnode4_t *rp; 11232 page_t *plist; 11233 page_t *pptr; 11234 offset3 offset; 11235 count3 len; 11236 k_sigset_t smask; 11237 11238 /* 11239 * We should get called with fl equal to either B_FREE or 11240 * B_INVAL. Any other value is illegal. 11241 * 11242 * The page that we are either supposed to free or destroy 11243 * should be exclusive locked and its io lock should not 11244 * be held. 11245 */ 11246 ASSERT(fl == B_FREE || fl == B_INVAL); 11247 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11248 11249 rp = VTOR4(vp); 11250 11251 /* 11252 * If the page doesn't need to be committed or we shouldn't 11253 * even bother attempting to commit it, then just make sure 11254 * that the p_fsdata byte is clear and then either free or 11255 * destroy the page as appropriate. 11256 */ 11257 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11258 pp->p_fsdata = C_NOCOMMIT; 11259 if (fl == B_FREE) 11260 page_free(pp, dn); 11261 else 11262 page_destroy(pp, dn); 11263 return; 11264 } 11265 11266 /* 11267 * If there is a page invalidation operation going on, then 11268 * if this is one of the pages being destroyed, then just 11269 * clear the p_fsdata byte and then either free or destroy 11270 * the page as appropriate. 11271 */ 11272 mutex_enter(&rp->r_statelock); 11273 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11274 mutex_exit(&rp->r_statelock); 11275 pp->p_fsdata = C_NOCOMMIT; 11276 if (fl == B_FREE) 11277 page_free(pp, dn); 11278 else 11279 page_destroy(pp, dn); 11280 return; 11281 } 11282 11283 /* 11284 * If we are freeing this page and someone else is already 11285 * waiting to do a commit, then just unlock the page and 11286 * return. That other thread will take care of commiting 11287 * this page. The page can be freed sometime after the 11288 * commit has finished. Otherwise, if the page is marked 11289 * as delay commit, then we may be getting called from 11290 * pvn_write_done, one page at a time. This could result 11291 * in one commit per page, so we end up doing lots of small 11292 * commits instead of fewer larger commits. This is bad, 11293 * we want do as few commits as possible. 11294 */ 11295 if (fl == B_FREE) { 11296 if (rp->r_flags & R4COMMITWAIT) { 11297 page_unlock(pp); 11298 mutex_exit(&rp->r_statelock); 11299 return; 11300 } 11301 if (pp->p_fsdata == C_DELAYCOMMIT) { 11302 pp->p_fsdata = C_COMMIT; 11303 page_unlock(pp); 11304 mutex_exit(&rp->r_statelock); 11305 return; 11306 } 11307 } 11308 11309 /* 11310 * Check to see if there is a signal which would prevent an 11311 * attempt to commit the pages from being successful. If so, 11312 * then don't bother with all of the work to gather pages and 11313 * generate the unsuccessful RPC. Just return from here and 11314 * let the page be committed at some later time. 11315 */ 11316 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11317 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11318 sigunintr(&smask); 11319 page_unlock(pp); 11320 mutex_exit(&rp->r_statelock); 11321 return; 11322 } 11323 sigunintr(&smask); 11324 11325 /* 11326 * We are starting to need to commit pages, so let's try 11327 * to commit as many as possible at once to reduce the 11328 * overhead. 11329 * 11330 * Set the `commit inprogress' state bit. We must 11331 * first wait until any current one finishes. Then 11332 * we initialize the c_pages list with this page. 11333 */ 11334 while (rp->r_flags & R4COMMIT) { 11335 rp->r_flags |= R4COMMITWAIT; 11336 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11337 rp->r_flags &= ~R4COMMITWAIT; 11338 } 11339 rp->r_flags |= R4COMMIT; 11340 mutex_exit(&rp->r_statelock); 11341 ASSERT(rp->r_commit.c_pages == NULL); 11342 rp->r_commit.c_pages = pp; 11343 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11344 rp->r_commit.c_commlen = PAGESIZE; 11345 11346 /* 11347 * Gather together all other pages which can be committed. 11348 * They will all be chained off r_commit.c_pages. 11349 */ 11350 nfs4_get_commit(vp); 11351 11352 /* 11353 * Clear the `commit inprogress' status and disconnect 11354 * the list of pages to be committed from the rnode. 11355 * At this same time, we also save the starting offset 11356 * and length of data to be committed on the server. 11357 */ 11358 plist = rp->r_commit.c_pages; 11359 rp->r_commit.c_pages = NULL; 11360 offset = rp->r_commit.c_commbase; 11361 len = rp->r_commit.c_commlen; 11362 mutex_enter(&rp->r_statelock); 11363 rp->r_flags &= ~R4COMMIT; 11364 cv_broadcast(&rp->r_commit.c_cv); 11365 mutex_exit(&rp->r_statelock); 11366 11367 if (curproc == proc_pageout || curproc == proc_fsflush || 11368 nfs_zone() != VTOMI4(vp)->mi_zone) { 11369 nfs4_async_commit(vp, plist, offset, len, 11370 cr, do_nfs4_async_commit); 11371 return; 11372 } 11373 11374 /* 11375 * Actually generate the COMMIT op over the wire operation. 11376 */ 11377 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11378 11379 /* 11380 * If we got an error during the commit, just unlock all 11381 * of the pages. The pages will get retransmitted to the 11382 * server during a putpage operation. 11383 */ 11384 if (error) { 11385 while (plist != NULL) { 11386 pptr = plist; 11387 page_sub(&plist, pptr); 11388 page_unlock(pptr); 11389 } 11390 return; 11391 } 11392 11393 /* 11394 * We've tried as hard as we can to commit the data to stable 11395 * storage on the server. We just unlock the rest of the pages 11396 * and clear the commit required state. They will be put 11397 * onto the tail of the cachelist if they are nolonger 11398 * mapped. 11399 */ 11400 while (plist != pp) { 11401 pptr = plist; 11402 page_sub(&plist, pptr); 11403 pptr->p_fsdata = C_NOCOMMIT; 11404 page_unlock(pptr); 11405 } 11406 11407 /* 11408 * It is possible that nfs4_commit didn't return error but 11409 * some other thread has modified the page we are going 11410 * to free/destroy. 11411 * In this case we need to rewrite the page. Do an explicit check 11412 * before attempting to free/destroy the page. If modified, needs to 11413 * be rewritten so unlock the page and return. 11414 */ 11415 if (hat_ismod(pp)) { 11416 pp->p_fsdata = C_NOCOMMIT; 11417 page_unlock(pp); 11418 return; 11419 } 11420 11421 /* 11422 * Now, as appropriate, either free or destroy the page 11423 * that we were called with. 11424 */ 11425 pp->p_fsdata = C_NOCOMMIT; 11426 if (fl == B_FREE) 11427 page_free(pp, dn); 11428 else 11429 page_destroy(pp, dn); 11430 } 11431 11432 /* 11433 * Commit requires that the current fh be the file written to. 11434 * The compound op structure is: 11435 * PUTFH(file), COMMIT 11436 */ 11437 static int 11438 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11439 { 11440 COMPOUND4args_clnt args; 11441 COMPOUND4res_clnt res; 11442 COMMIT4res *cm_res; 11443 nfs_argop4 argop[2]; 11444 nfs_resop4 *resop; 11445 int doqueue; 11446 mntinfo4_t *mi; 11447 rnode4_t *rp; 11448 cred_t *cred_otw = NULL; 11449 bool_t needrecov = FALSE; 11450 nfs4_recov_state_t recov_state; 11451 nfs4_open_stream_t *osp = NULL; 11452 bool_t first_time = TRUE; /* first time getting OTW cred */ 11453 bool_t last_time = FALSE; /* last time getting OTW cred */ 11454 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11455 11456 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11457 11458 rp = VTOR4(vp); 11459 11460 mi = VTOMI4(vp); 11461 recov_state.rs_flags = 0; 11462 recov_state.rs_num_retry_despite_err = 0; 11463 get_commit_cred: 11464 /* 11465 * Releases the osp, if a valid open stream is provided. 11466 * Puts a hold on the cred_otw and the new osp (if found). 11467 */ 11468 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11469 &first_time, &last_time); 11470 args.ctag = TAG_COMMIT; 11471 recov_retry: 11472 /* 11473 * Commit ops: putfh file; commit 11474 */ 11475 args.array_len = 2; 11476 args.array = argop; 11477 11478 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11479 &recov_state, NULL); 11480 if (e.error) { 11481 crfree(cred_otw); 11482 if (osp != NULL) 11483 open_stream_rele(osp, rp); 11484 return (e.error); 11485 } 11486 11487 /* putfh directory */ 11488 argop[0].argop = OP_CPUTFH; 11489 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11490 11491 /* commit */ 11492 argop[1].argop = OP_COMMIT; 11493 argop[1].nfs_argop4_u.opcommit.offset = offset; 11494 argop[1].nfs_argop4_u.opcommit.count = count; 11495 11496 doqueue = 1; 11497 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11498 11499 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11500 if (!needrecov && e.error) { 11501 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11502 needrecov); 11503 crfree(cred_otw); 11504 if (e.error == EACCES && last_time == FALSE) 11505 goto get_commit_cred; 11506 if (osp != NULL) 11507 open_stream_rele(osp, rp); 11508 return (e.error); 11509 } 11510 11511 if (needrecov) { 11512 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11513 NULL, OP_COMMIT, NULL) == FALSE) { 11514 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11515 &recov_state, needrecov); 11516 if (!e.error) 11517 (void) xdr_free(xdr_COMPOUND4res_clnt, 11518 (caddr_t)&res); 11519 goto recov_retry; 11520 } 11521 if (e.error) { 11522 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11523 &recov_state, needrecov); 11524 crfree(cred_otw); 11525 if (osp != NULL) 11526 open_stream_rele(osp, rp); 11527 return (e.error); 11528 } 11529 /* fall through for res.status case */ 11530 } 11531 11532 if (res.status) { 11533 e.error = geterrno4(res.status); 11534 if (e.error == EACCES && last_time == FALSE) { 11535 crfree(cred_otw); 11536 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11537 &recov_state, needrecov); 11538 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11539 goto get_commit_cred; 11540 } 11541 /* 11542 * Can't do a nfs4_purge_stale_fh here because this 11543 * can cause a deadlock. nfs4_commit can 11544 * be called from nfs4_dispose which can be called 11545 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11546 * can call back to pvn_vplist_dirty. 11547 */ 11548 if (e.error == ESTALE) { 11549 mutex_enter(&rp->r_statelock); 11550 rp->r_flags |= R4STALE; 11551 if (!rp->r_error) 11552 rp->r_error = e.error; 11553 mutex_exit(&rp->r_statelock); 11554 PURGE_ATTRCACHE4(vp); 11555 } else { 11556 mutex_enter(&rp->r_statelock); 11557 if (!rp->r_error) 11558 rp->r_error = e.error; 11559 mutex_exit(&rp->r_statelock); 11560 } 11561 } else { 11562 ASSERT(rp->r_flags & R4HAVEVERF); 11563 resop = &res.array[1]; /* commit res */ 11564 cm_res = &resop->nfs_resop4_u.opcommit; 11565 mutex_enter(&rp->r_statelock); 11566 if (cm_res->writeverf == rp->r_writeverf) { 11567 mutex_exit(&rp->r_statelock); 11568 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11569 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11570 &recov_state, needrecov); 11571 crfree(cred_otw); 11572 if (osp != NULL) 11573 open_stream_rele(osp, rp); 11574 return (0); 11575 } 11576 nfs4_set_mod(vp); 11577 rp->r_writeverf = cm_res->writeverf; 11578 mutex_exit(&rp->r_statelock); 11579 e.error = NFS_VERF_MISMATCH; 11580 } 11581 11582 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11583 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11584 crfree(cred_otw); 11585 if (osp != NULL) 11586 open_stream_rele(osp, rp); 11587 11588 return (e.error); 11589 } 11590 11591 static void 11592 nfs4_set_mod(vnode_t *vp) 11593 { 11594 page_t *pp; 11595 kmutex_t *vphm; 11596 rnode4_t *rp; 11597 11598 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11599 11600 /* make sure we're looking at the master vnode, not a shadow */ 11601 11602 rp = VTOR4(vp); 11603 if (IS_SHADOW(vp, rp)) 11604 vp = RTOV4(rp); 11605 11606 vphm = page_vnode_mutex(vp); 11607 mutex_enter(vphm); 11608 /* 11609 * If there are no pages associated with this vnode, then 11610 * just return. 11611 */ 11612 if ((pp = vp->v_pages) == NULL) { 11613 mutex_exit(vphm); 11614 return; 11615 } 11616 11617 do { 11618 if (pp->p_fsdata != C_NOCOMMIT) { 11619 hat_setmod(pp); 11620 pp->p_fsdata = C_NOCOMMIT; 11621 } 11622 } while ((pp = pp->p_vpnext) != vp->v_pages); 11623 mutex_exit(vphm); 11624 } 11625 11626 /* 11627 * This function is used to gather a page list of the pages which 11628 * can be committed on the server. 11629 * 11630 * The calling thread must have set R4COMMIT. This bit is used to 11631 * serialize access to the commit structure in the rnode. As long 11632 * as the thread has set R4COMMIT, then it can manipulate the commit 11633 * structure without requiring any other locks. 11634 * 11635 * When this function is called from nfs4_dispose() the page passed 11636 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11637 * will skip it. This is not a problem since we initially add the 11638 * page to the r_commit page list. 11639 * 11640 */ 11641 static void 11642 nfs4_get_commit(vnode_t *vp) 11643 { 11644 rnode4_t *rp; 11645 page_t *pp; 11646 kmutex_t *vphm; 11647 11648 rp = VTOR4(vp); 11649 11650 ASSERT(rp->r_flags & R4COMMIT); 11651 11652 /* make sure we're looking at the master vnode, not a shadow */ 11653 11654 if (IS_SHADOW(vp, rp)) 11655 vp = RTOV4(rp); 11656 11657 vphm = page_vnode_mutex(vp); 11658 mutex_enter(vphm); 11659 11660 /* 11661 * If there are no pages associated with this vnode, then 11662 * just return. 11663 */ 11664 if ((pp = vp->v_pages) == NULL) { 11665 mutex_exit(vphm); 11666 return; 11667 } 11668 11669 /* 11670 * Step through all of the pages associated with this vnode 11671 * looking for pages which need to be committed. 11672 */ 11673 do { 11674 /* 11675 * First short-cut everything (without the page_lock) 11676 * and see if this page does not need to be committed 11677 * or is modified if so then we'll just skip it. 11678 */ 11679 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11680 continue; 11681 11682 /* 11683 * Attempt to lock the page. If we can't, then 11684 * someone else is messing with it or we have been 11685 * called from nfs4_dispose and this is the page that 11686 * nfs4_dispose was called with.. anyway just skip it. 11687 */ 11688 if (!page_trylock(pp, SE_EXCL)) 11689 continue; 11690 11691 /* 11692 * Lets check again now that we have the page lock. 11693 */ 11694 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11695 page_unlock(pp); 11696 continue; 11697 } 11698 11699 /* this had better not be a free page */ 11700 ASSERT(PP_ISFREE(pp) == 0); 11701 11702 /* 11703 * The page needs to be committed and we locked it. 11704 * Update the base and length parameters and add it 11705 * to r_pages. 11706 */ 11707 if (rp->r_commit.c_pages == NULL) { 11708 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11709 rp->r_commit.c_commlen = PAGESIZE; 11710 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11711 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11712 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11713 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11714 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11715 <= pp->p_offset) { 11716 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11717 rp->r_commit.c_commbase + PAGESIZE; 11718 } 11719 page_add(&rp->r_commit.c_pages, pp); 11720 } while ((pp = pp->p_vpnext) != vp->v_pages); 11721 11722 mutex_exit(vphm); 11723 } 11724 11725 /* 11726 * This routine is used to gather together a page list of the pages 11727 * which are to be committed on the server. This routine must not 11728 * be called if the calling thread holds any locked pages. 11729 * 11730 * The calling thread must have set R4COMMIT. This bit is used to 11731 * serialize access to the commit structure in the rnode. As long 11732 * as the thread has set R4COMMIT, then it can manipulate the commit 11733 * structure without requiring any other locks. 11734 */ 11735 static void 11736 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 11737 { 11738 11739 rnode4_t *rp; 11740 page_t *pp; 11741 u_offset_t end; 11742 u_offset_t off; 11743 ASSERT(len != 0); 11744 rp = VTOR4(vp); 11745 ASSERT(rp->r_flags & R4COMMIT); 11746 11747 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11748 11749 /* make sure we're looking at the master vnode, not a shadow */ 11750 11751 if (IS_SHADOW(vp, rp)) 11752 vp = RTOV4(rp); 11753 11754 /* 11755 * If there are no pages associated with this vnode, then 11756 * just return. 11757 */ 11758 if ((pp = vp->v_pages) == NULL) 11759 return; 11760 /* 11761 * Calculate the ending offset. 11762 */ 11763 end = soff + len; 11764 for (off = soff; off < end; off += PAGESIZE) { 11765 /* 11766 * Lookup each page by vp, offset. 11767 */ 11768 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 11769 continue; 11770 /* 11771 * If this page does not need to be committed or is 11772 * modified, then just skip it. 11773 */ 11774 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11775 page_unlock(pp); 11776 continue; 11777 } 11778 11779 ASSERT(PP_ISFREE(pp) == 0); 11780 /* 11781 * The page needs to be committed and we locked it. 11782 * Update the base and length parameters and add it 11783 * to r_pages. 11784 */ 11785 if (rp->r_commit.c_pages == NULL) { 11786 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11787 rp->r_commit.c_commlen = PAGESIZE; 11788 } else { 11789 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11790 rp->r_commit.c_commbase + PAGESIZE; 11791 } 11792 page_add(&rp->r_commit.c_pages, pp); 11793 } 11794 } 11795 11796 /* 11797 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 11798 * Flushes and commits data to the server. 11799 */ 11800 static int 11801 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 11802 { 11803 int error; 11804 verifier4 write_verf; 11805 rnode4_t *rp = VTOR4(vp); 11806 11807 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11808 11809 /* 11810 * Flush the data portion of the file and then commit any 11811 * portions which need to be committed. This may need to 11812 * be done twice if the server has changed state since 11813 * data was last written. The data will need to be 11814 * rewritten to the server and then a new commit done. 11815 * 11816 * In fact, this may need to be done several times if the 11817 * server is having problems and crashing while we are 11818 * attempting to do this. 11819 */ 11820 11821 top: 11822 /* 11823 * Do a flush based on the poff and plen arguments. This 11824 * will synchronously write out any modified pages in the 11825 * range specified by (poff, plen). This starts all of the 11826 * i/o operations which will be waited for in the next 11827 * call to nfs4_putpage 11828 */ 11829 11830 mutex_enter(&rp->r_statelock); 11831 write_verf = rp->r_writeverf; 11832 mutex_exit(&rp->r_statelock); 11833 11834 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr); 11835 if (error == EAGAIN) 11836 error = 0; 11837 11838 /* 11839 * Do a flush based on the poff and plen arguments. This 11840 * will synchronously write out any modified pages in the 11841 * range specified by (poff, plen) and wait until all of 11842 * the asynchronous i/o's in that range are done as well. 11843 */ 11844 if (!error) 11845 error = nfs4_putpage(vp, poff, plen, 0, cr); 11846 11847 if (error) 11848 return (error); 11849 11850 mutex_enter(&rp->r_statelock); 11851 if (rp->r_writeverf != write_verf) { 11852 mutex_exit(&rp->r_statelock); 11853 goto top; 11854 } 11855 mutex_exit(&rp->r_statelock); 11856 11857 /* 11858 * Now commit any pages which might need to be committed. 11859 * If the error, NFS_VERF_MISMATCH, is returned, then 11860 * start over with the flush operation. 11861 */ 11862 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 11863 11864 if (error == NFS_VERF_MISMATCH) 11865 goto top; 11866 11867 return (error); 11868 } 11869 11870 /* 11871 * nfs4_commit_vp() will wait for other pending commits and 11872 * will either commit the whole file or a range, plen dictates 11873 * if we commit whole file. a value of zero indicates the whole 11874 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 11875 */ 11876 static int 11877 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 11878 cred_t *cr, int wait_on_writes) 11879 { 11880 rnode4_t *rp; 11881 page_t *plist; 11882 offset3 offset; 11883 count3 len; 11884 11885 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11886 11887 rp = VTOR4(vp); 11888 11889 /* 11890 * before we gather commitable pages make 11891 * sure there are no outstanding async writes 11892 */ 11893 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 11894 mutex_enter(&rp->r_statelock); 11895 while (rp->r_count > 0) { 11896 cv_wait(&rp->r_cv, &rp->r_statelock); 11897 } 11898 mutex_exit(&rp->r_statelock); 11899 } 11900 11901 /* 11902 * Set the `commit inprogress' state bit. We must 11903 * first wait until any current one finishes. 11904 */ 11905 mutex_enter(&rp->r_statelock); 11906 while (rp->r_flags & R4COMMIT) { 11907 rp->r_flags |= R4COMMITWAIT; 11908 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11909 rp->r_flags &= ~R4COMMITWAIT; 11910 } 11911 rp->r_flags |= R4COMMIT; 11912 mutex_exit(&rp->r_statelock); 11913 11914 /* 11915 * Gather all of the pages which need to be 11916 * committed. 11917 */ 11918 if (plen == 0) 11919 nfs4_get_commit(vp); 11920 else 11921 nfs4_get_commit_range(vp, poff, plen); 11922 11923 /* 11924 * Clear the `commit inprogress' bit and disconnect the 11925 * page list which was gathered by nfs4_get_commit. 11926 */ 11927 plist = rp->r_commit.c_pages; 11928 rp->r_commit.c_pages = NULL; 11929 offset = rp->r_commit.c_commbase; 11930 len = rp->r_commit.c_commlen; 11931 mutex_enter(&rp->r_statelock); 11932 rp->r_flags &= ~R4COMMIT; 11933 cv_broadcast(&rp->r_commit.c_cv); 11934 mutex_exit(&rp->r_statelock); 11935 11936 /* 11937 * If any pages need to be committed, commit them and 11938 * then unlock them so that they can be freed some 11939 * time later. 11940 */ 11941 if (plist == NULL) 11942 return (0); 11943 11944 /* 11945 * No error occurred during the flush portion 11946 * of this operation, so now attempt to commit 11947 * the data to stable storage on the server. 11948 * 11949 * This will unlock all of the pages on the list. 11950 */ 11951 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 11952 } 11953 11954 static int 11955 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11956 cred_t *cr) 11957 { 11958 int error; 11959 page_t *pp; 11960 11961 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11962 11963 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 11964 11965 /* 11966 * If we got an error, then just unlock all of the pages 11967 * on the list. 11968 */ 11969 if (error) { 11970 while (plist != NULL) { 11971 pp = plist; 11972 page_sub(&plist, pp); 11973 page_unlock(pp); 11974 } 11975 return (error); 11976 } 11977 /* 11978 * We've tried as hard as we can to commit the data to stable 11979 * storage on the server. We just unlock the pages and clear 11980 * the commit required state. They will get freed later. 11981 */ 11982 while (plist != NULL) { 11983 pp = plist; 11984 page_sub(&plist, pp); 11985 pp->p_fsdata = C_NOCOMMIT; 11986 page_unlock(pp); 11987 } 11988 11989 return (error); 11990 } 11991 11992 static void 11993 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11994 cred_t *cr) 11995 { 11996 11997 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 11998 } 11999 12000 /*ARGSUSED*/ 12001 static int 12002 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 12003 { 12004 int error = 0; 12005 mntinfo4_t *mi; 12006 vattr_t va; 12007 vsecattr_t nfsace4_vsap; 12008 12009 mi = VTOMI4(vp); 12010 if (nfs_zone() != mi->mi_zone) 12011 return (EIO); 12012 if (mi->mi_flags & MI4_ACL) { 12013 /* if we have a delegation, return it */ 12014 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12015 (void) nfs4delegreturn(VTOR4(vp), 12016 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12017 12018 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12019 NFS4_ACL_SET); 12020 if (error) /* EINVAL */ 12021 return (error); 12022 12023 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12024 /* 12025 * These are aclent_t type entries. 12026 */ 12027 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12028 vp->v_type == VDIR, FALSE); 12029 if (error) 12030 return (error); 12031 } else { 12032 /* 12033 * These are ace_t type entries. 12034 */ 12035 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12036 FALSE); 12037 if (error) 12038 return (error); 12039 } 12040 bzero(&va, sizeof (va)); 12041 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12042 vs_ace4_destroy(&nfsace4_vsap); 12043 return (error); 12044 } 12045 return (ENOSYS); 12046 } 12047 12048 static int 12049 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 12050 { 12051 int error; 12052 mntinfo4_t *mi; 12053 nfs4_ga_res_t gar; 12054 rnode4_t *rp = VTOR4(vp); 12055 12056 mi = VTOMI4(vp); 12057 if (nfs_zone() != mi->mi_zone) 12058 return (EIO); 12059 12060 bzero(&gar, sizeof (gar)); 12061 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12062 12063 /* 12064 * vsecattr->vsa_mask holds the original acl request mask. 12065 * This is needed when determining what to return. 12066 * (See: nfs4_create_getsecattr_return()) 12067 */ 12068 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12069 if (error) /* EINVAL */ 12070 return (error); 12071 12072 if (mi->mi_flags & MI4_ACL) { 12073 /* 12074 * Check if the data is cached and the cache is valid. If it 12075 * is we don't go over the wire. 12076 */ 12077 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12078 mutex_enter(&rp->r_statelock); 12079 if (rp->r_secattr != NULL) { 12080 error = nfs4_create_getsecattr_return( 12081 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12082 rp->r_attr.va_gid, 12083 vp->v_type == VDIR); 12084 if (!error) { /* error == 0 - Success! */ 12085 mutex_exit(&rp->r_statelock); 12086 return (error); 12087 } 12088 } 12089 mutex_exit(&rp->r_statelock); 12090 } 12091 12092 /* 12093 * The getattr otw call will always get both the acl, in 12094 * the form of a list of nfsace4's, and the number of acl 12095 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12096 */ 12097 gar.n4g_va.va_mask = AT_ALL; 12098 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12099 if (error) { 12100 vs_ace4_destroy(&gar.n4g_vsa); 12101 if (error == ENOTSUP || error == EOPNOTSUPP) 12102 error = fs_fab_acl(vp, vsecattr, flag, cr); 12103 return (error); 12104 } 12105 12106 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12107 /* 12108 * No error was returned, but according to the response 12109 * bitmap, neither was an acl. 12110 */ 12111 vs_ace4_destroy(&gar.n4g_vsa); 12112 error = fs_fab_acl(vp, vsecattr, flag, cr); 12113 return (error); 12114 } 12115 12116 /* 12117 * Update the cache with the ACL. 12118 */ 12119 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12120 12121 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12122 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12123 vp->v_type == VDIR); 12124 vs_ace4_destroy(&gar.n4g_vsa); 12125 if ((error) && (vsecattr->vsa_mask & 12126 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12127 (error != EACCES)) { 12128 error = fs_fab_acl(vp, vsecattr, flag, cr); 12129 } 12130 return (error); 12131 } 12132 error = fs_fab_acl(vp, vsecattr, flag, cr); 12133 return (error); 12134 } 12135 12136 /* 12137 * The function returns: 12138 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12139 * - EINVAL if the passed in "acl_mask" is an invalid request. 12140 * 12141 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12142 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12143 * 12144 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12145 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12146 * - We have a count field set without the corresponding acl field set. (e.g. - 12147 * VSA_ACECNT is set, but VSA_ACE is not) 12148 */ 12149 static int 12150 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12151 { 12152 /* Shortcut the masks that are always valid. */ 12153 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12154 return (0); 12155 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12156 return (0); 12157 12158 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12159 /* 12160 * We can't have any VSA_ACL type stuff in the mask now. 12161 */ 12162 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12163 VSA_DFACLCNT)) 12164 return (EINVAL); 12165 12166 if (op == NFS4_ACL_SET) { 12167 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12168 return (EINVAL); 12169 } 12170 } 12171 12172 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12173 /* 12174 * We can't have any VSA_ACE type stuff in the mask now. 12175 */ 12176 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12177 return (EINVAL); 12178 12179 if (op == NFS4_ACL_SET) { 12180 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12181 return (EINVAL); 12182 12183 if ((acl_mask & VSA_DFACLCNT) && 12184 !(acl_mask & VSA_DFACL)) 12185 return (EINVAL); 12186 } 12187 } 12188 return (0); 12189 } 12190 12191 /* 12192 * The theory behind creating the correct getsecattr return is simply this: 12193 * "Don't return anything that the caller is not expecting to have to free." 12194 */ 12195 static int 12196 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12197 uid_t uid, gid_t gid, int isdir) 12198 { 12199 int error = 0; 12200 /* Save the mask since the translators modify it. */ 12201 uint_t orig_mask = vsap->vsa_mask; 12202 12203 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12204 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12205 FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12206 12207 if (error) 12208 return (error); 12209 12210 /* 12211 * If the caller only asked for the ace count (VSA_ACECNT) 12212 * don't give them the full acl (VSA_ACE), free it. 12213 */ 12214 if (!orig_mask & VSA_ACE) { 12215 if (vsap->vsa_aclentp != NULL) { 12216 kmem_free(vsap->vsa_aclentp, 12217 vsap->vsa_aclcnt * sizeof (ace_t)); 12218 vsap->vsa_aclentp = NULL; 12219 } 12220 } 12221 vsap->vsa_mask = orig_mask; 12222 12223 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12224 VSA_DFACLCNT)) { 12225 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12226 isdir, FALSE, 12227 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12228 12229 if (error) 12230 return (error); 12231 12232 /* 12233 * If the caller only asked for the acl count (VSA_ACLCNT) 12234 * and/or the default acl count (VSA_DFACLCNT) don't give them 12235 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12236 */ 12237 if (!orig_mask & VSA_ACL) { 12238 if (vsap->vsa_aclentp != NULL) { 12239 kmem_free(vsap->vsa_aclentp, 12240 vsap->vsa_aclcnt * sizeof (aclent_t)); 12241 vsap->vsa_aclentp = NULL; 12242 } 12243 } 12244 12245 if (!orig_mask & VSA_DFACL) { 12246 if (vsap->vsa_dfaclentp != NULL) { 12247 kmem_free(vsap->vsa_dfaclentp, 12248 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12249 vsap->vsa_dfaclentp = NULL; 12250 } 12251 } 12252 vsap->vsa_mask = orig_mask; 12253 } 12254 return (0); 12255 } 12256 12257 static int 12258 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr) 12259 { 12260 int error; 12261 12262 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12263 return (EIO); 12264 /* 12265 * check for valid cmd parameter 12266 */ 12267 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12268 return (EINVAL); 12269 12270 /* 12271 * Check access permissions 12272 */ 12273 if ((cmd & F_SHARE) && 12274 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12275 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12276 return (EBADF); 12277 12278 /* 12279 * If the filesystem is mounted using local locking, pass the 12280 * request off to the local share code. 12281 */ 12282 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12283 return (fs_shrlock(vp, cmd, shr, flag, cr)); 12284 12285 switch (cmd) { 12286 case F_SHARE: 12287 case F_UNSHARE: 12288 /* 12289 * This will be properly implemented later, 12290 * see RFE: 4823948 . 12291 */ 12292 error = EAGAIN; 12293 break; 12294 12295 case F_HASREMOTELOCKS: 12296 /* 12297 * NFS client can't store remote locks itself 12298 */ 12299 shr->s_access = 0; 12300 error = 0; 12301 break; 12302 12303 default: 12304 error = EINVAL; 12305 break; 12306 } 12307 12308 return (error); 12309 } 12310 12311 /* 12312 * Common code called by directory ops to update the attrcache 12313 */ 12314 static int 12315 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12316 hrtime_t t, vnode_t *vp, cred_t *cr) 12317 { 12318 int error = 0; 12319 12320 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12321 12322 if (status != NFS4_OK) { 12323 /* getattr not done or failed */ 12324 PURGE_ATTRCACHE4(vp); 12325 return (error); 12326 } 12327 12328 if (garp) { 12329 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12330 } else { 12331 PURGE_ATTRCACHE4(vp); 12332 } 12333 return (error); 12334 } 12335 12336 /* 12337 * Update directory caches for directory modification ops (link, rename, etc.) 12338 * When dinfo is NULL, manage dircaches in the old way. 12339 */ 12340 static void 12341 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12342 dirattr_info_t *dinfo) 12343 { 12344 rnode4_t *drp = VTOR4(dvp); 12345 12346 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12347 12348 /* Purge rddir cache for dir since it changed */ 12349 if (drp->r_dir != NULL) 12350 nfs4_purge_rddir_cache(dvp); 12351 12352 /* 12353 * If caller provided dinfo, then use it to manage dir caches. 12354 */ 12355 if (dinfo != NULL) { 12356 if (vp != NULL) { 12357 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12358 if (!VTOR4(vp)->created_v4) { 12359 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12360 dnlc_update(dvp, nm, vp); 12361 } else { 12362 /* 12363 * XXX don't update if the created_v4 flag is 12364 * set 12365 */ 12366 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12367 NFS4_DEBUG(nfs4_client_state_debug, 12368 (CE_NOTE, "nfs4_update_dircaches: " 12369 "don't update dnlc: created_v4 flag")); 12370 } 12371 } 12372 12373 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12374 dinfo->di_cred, FALSE, cinfo); 12375 12376 return; 12377 } 12378 12379 /* 12380 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12381 * Since caller modified dir but didn't receive post-dirmod-op dir 12382 * attrs, the dir's attrs must be purged. 12383 * 12384 * XXX this check and dnlc update/purge should really be atomic, 12385 * XXX but can't use rnode statelock because it'll deadlock in 12386 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12387 * XXX does occur. 12388 * 12389 * XXX We also may want to check that atomic is true in the 12390 * XXX change_info struct. If it is not, the change_info may 12391 * XXX reflect changes by more than one clients which means that 12392 * XXX our cache may not be valid. 12393 */ 12394 PURGE_ATTRCACHE4(dvp); 12395 if (drp->r_change == cinfo->before) { 12396 /* no changes took place in the directory prior to our link */ 12397 if (vp != NULL) { 12398 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12399 if (!VTOR4(vp)->created_v4) { 12400 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12401 dnlc_update(dvp, nm, vp); 12402 } else { 12403 /* 12404 * XXX dont' update if the created_v4 flag 12405 * is set 12406 */ 12407 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12408 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12409 "nfs4_update_dircaches: don't" 12410 " update dnlc: created_v4 flag")); 12411 } 12412 } 12413 } else { 12414 /* Another client modified directory - purge its dnlc cache */ 12415 dnlc_purge_vp(dvp); 12416 } 12417 } 12418 12419 /* 12420 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12421 * file. 12422 * 12423 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12424 * file (ie: client recovery) and otherwise set to FALSE. 12425 * 12426 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12427 * initiated) calling functions. 12428 * 12429 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12430 * of resending a 'lost' open request. 12431 * 12432 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12433 * server that hands out BAD_SEQID on open confirm. 12434 * 12435 * Errors are returned via the nfs4_error_t parameter. 12436 */ 12437 void 12438 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12439 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12440 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12441 { 12442 COMPOUND4args_clnt args; 12443 COMPOUND4res_clnt res; 12444 nfs_argop4 argop[2]; 12445 nfs_resop4 *resop; 12446 int doqueue = 1; 12447 mntinfo4_t *mi; 12448 OPEN_CONFIRM4args *open_confirm_args; 12449 int needrecov; 12450 12451 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12452 #if DEBUG 12453 mutex_enter(&oop->oo_lock); 12454 ASSERT(oop->oo_seqid_inuse); 12455 mutex_exit(&oop->oo_lock); 12456 #endif 12457 12458 recov_retry_confirm: 12459 nfs4_error_zinit(ep); 12460 *retry_open = FALSE; 12461 12462 if (resend) 12463 args.ctag = TAG_OPEN_CONFIRM_LOST; 12464 else 12465 args.ctag = TAG_OPEN_CONFIRM; 12466 12467 args.array_len = 2; 12468 args.array = argop; 12469 12470 /* putfh target fh */ 12471 argop[0].argop = OP_CPUTFH; 12472 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12473 12474 argop[1].argop = OP_OPEN_CONFIRM; 12475 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12476 12477 (*seqid) += 1; 12478 open_confirm_args->seqid = *seqid; 12479 open_confirm_args->open_stateid = *stateid; 12480 12481 mi = VTOMI4(vp); 12482 12483 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12484 12485 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12486 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12487 } 12488 12489 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12490 if (!needrecov && ep->error) 12491 return; 12492 12493 if (needrecov) { 12494 bool_t abort = FALSE; 12495 12496 if (reopening_file == FALSE) { 12497 nfs4_bseqid_entry_t *bsep = NULL; 12498 12499 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12500 bsep = nfs4_create_bseqid_entry(oop, NULL, 12501 vp, 0, args.ctag, 12502 open_confirm_args->seqid); 12503 12504 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, 12505 NULL, NULL, NULL, OP_OPEN_CONFIRM, bsep); 12506 if (bsep) { 12507 kmem_free(bsep, sizeof (*bsep)); 12508 if (num_bseqid_retryp && 12509 --(*num_bseqid_retryp) == 0) 12510 abort = TRUE; 12511 } 12512 } 12513 if ((ep->error == ETIMEDOUT || 12514 res.status == NFS4ERR_RESOURCE) && 12515 abort == FALSE && resend == FALSE) { 12516 if (!ep->error) 12517 (void) xdr_free(xdr_COMPOUND4res_clnt, 12518 (caddr_t)&res); 12519 12520 delay(SEC_TO_TICK(confirm_retry_sec)); 12521 goto recov_retry_confirm; 12522 } 12523 /* State may have changed so retry the entire OPEN op */ 12524 if (abort == FALSE) 12525 *retry_open = TRUE; 12526 else 12527 *retry_open = FALSE; 12528 if (!ep->error) 12529 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12530 return; 12531 } 12532 12533 if (res.status) { 12534 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12535 return; 12536 } 12537 12538 resop = &res.array[1]; /* open confirm res */ 12539 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12540 stateid, sizeof (*stateid)); 12541 12542 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12543 } 12544 12545 /* 12546 * Return the credentials associated with a client state object. The 12547 * caller is responsible for freeing the credentials. 12548 */ 12549 12550 static cred_t * 12551 state_to_cred(nfs4_open_stream_t *osp) 12552 { 12553 cred_t *cr; 12554 12555 /* 12556 * It's ok to not lock the open stream and open owner to get 12557 * the oo_cred since this is only written once (upon creation) 12558 * and will not change. 12559 */ 12560 cr = osp->os_open_owner->oo_cred; 12561 crhold(cr); 12562 12563 return (cr); 12564 } 12565 12566 /* 12567 * nfs4_find_sysid 12568 * 12569 * Find the sysid for the knetconfig associated with the given mi. 12570 */ 12571 static struct lm_sysid * 12572 nfs4_find_sysid(mntinfo4_t *mi) 12573 { 12574 ASSERT(nfs_zone() == mi->mi_zone); 12575 12576 /* 12577 * Switch from RDMA knconf to original mount knconf 12578 */ 12579 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12580 mi->mi_curr_serv->sv_hostname, NULL)); 12581 } 12582 12583 #ifdef DEBUG 12584 /* 12585 * Return a string version of the call type for easy reading. 12586 */ 12587 static char * 12588 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12589 { 12590 switch (ctype) { 12591 case NFS4_LCK_CTYPE_NORM: 12592 return ("NORMAL"); 12593 case NFS4_LCK_CTYPE_RECLAIM: 12594 return ("RECLAIM"); 12595 case NFS4_LCK_CTYPE_RESEND: 12596 return ("RESEND"); 12597 case NFS4_LCK_CTYPE_REINSTATE: 12598 return ("REINSTATE"); 12599 default: 12600 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12601 "type %d", ctype); 12602 return (""); 12603 } 12604 } 12605 #endif 12606 12607 /* 12608 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12609 * Unlock requests don't have an over-the-wire locktype, so we just return 12610 * something non-threatening. 12611 */ 12612 12613 static nfs_lock_type4 12614 flk_to_locktype(int cmd, int l_type) 12615 { 12616 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12617 12618 switch (l_type) { 12619 case F_UNLCK: 12620 return (READ_LT); 12621 case F_RDLCK: 12622 if (cmd == F_SETLK) 12623 return (READ_LT); 12624 else 12625 return (READW_LT); 12626 case F_WRLCK: 12627 if (cmd == F_SETLK) 12628 return (WRITE_LT); 12629 else 12630 return (WRITEW_LT); 12631 } 12632 panic("flk_to_locktype"); 12633 /*NOTREACHED*/ 12634 } 12635 12636 /* 12637 * Do some preliminary checks for nfs4frlock. 12638 */ 12639 static int 12640 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12641 u_offset_t offset) 12642 { 12643 int error = 0; 12644 12645 /* 12646 * If we are setting a lock, check that the file is opened 12647 * with the correct mode. 12648 */ 12649 if (cmd == F_SETLK || cmd == F_SETLKW) { 12650 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12651 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12652 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12653 "nfs4frlock_validate_args: file was opened with " 12654 "incorrect mode")); 12655 return (EBADF); 12656 } 12657 } 12658 12659 /* Convert the offset. It may need to be restored before returning. */ 12660 if (error = convoff(vp, flk, 0, offset)) { 12661 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12662 "nfs4frlock_validate_args: convoff => error= %d\n", 12663 error)); 12664 return (error); 12665 } 12666 12667 return (error); 12668 } 12669 12670 /* 12671 * Set the flock64's lm_sysid for nfs4frlock. 12672 */ 12673 static int 12674 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12675 { 12676 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12677 12678 /* Find the lm_sysid */ 12679 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12680 12681 if (*lspp == NULL) { 12682 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12683 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12684 return (ENOLCK); 12685 } 12686 12687 flk->l_sysid = lm_sysidt(*lspp); 12688 12689 return (0); 12690 } 12691 12692 /* 12693 * Do the remaining preliminary setup for nfs4frlock. 12694 */ 12695 static void 12696 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12697 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12698 cred_t **cred_otw) 12699 { 12700 /* 12701 * set tick_delay to the base delay time. 12702 * (NFS4_BASE_WAIT_TIME is in secs) 12703 */ 12704 12705 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12706 12707 /* 12708 * If lock is relative to EOF, we need the newest length of the 12709 * file. Therefore invalidate the ATTR_CACHE. 12710 */ 12711 12712 *whencep = flk->l_whence; 12713 12714 if (*whencep == 2) /* SEEK_END */ 12715 PURGE_ATTRCACHE4(vp); 12716 12717 recov_statep->rs_flags = 0; 12718 recov_statep->rs_num_retry_despite_err = 0; 12719 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12720 } 12721 12722 /* 12723 * Initialize and allocate the data structures necessary for 12724 * the nfs4frlock call. 12725 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 12726 */ 12727 static void 12728 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 12729 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 12730 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 12731 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 12732 { 12733 int argoplist_size; 12734 int num_ops = 2; 12735 12736 *retry = FALSE; 12737 *did_start_fop = FALSE; 12738 *skip_get_err = FALSE; 12739 lost_rqstp->lr_op = 0; 12740 argoplist_size = num_ops * sizeof (nfs_argop4); 12741 /* fill array with zero */ 12742 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 12743 12744 *argspp = argsp; 12745 *respp = NULL; 12746 12747 argsp->array_len = num_ops; 12748 argsp->array = *argopp; 12749 12750 /* initialize in case of error; will get real value down below */ 12751 argsp->ctag = TAG_NONE; 12752 12753 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 12754 *op_hintp = OH_LOCKU; 12755 else 12756 *op_hintp = OH_OTHER; 12757 } 12758 12759 /* 12760 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 12761 * the proper nfs4_server_t for this instance of nfs4frlock. 12762 * Returns 0 (success) or an errno value. 12763 */ 12764 static int 12765 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 12766 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 12767 bool_t *did_start_fop, bool_t *startrecovp) 12768 { 12769 int error = 0; 12770 rnode4_t *rp; 12771 12772 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12773 12774 if (ctype == NFS4_LCK_CTYPE_NORM) { 12775 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 12776 recov_statep, startrecovp); 12777 if (error) 12778 return (error); 12779 *did_start_fop = TRUE; 12780 } else { 12781 *did_start_fop = FALSE; 12782 *startrecovp = FALSE; 12783 } 12784 12785 if (!error) { 12786 rp = VTOR4(vp); 12787 12788 /* If the file failed recovery, just quit. */ 12789 mutex_enter(&rp->r_statelock); 12790 if (rp->r_flags & R4RECOVERR) { 12791 error = EIO; 12792 } 12793 mutex_exit(&rp->r_statelock); 12794 } 12795 12796 return (error); 12797 } 12798 12799 /* 12800 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 12801 * resend nfs4frlock call is initiated by the recovery framework. 12802 * Acquires the lop and oop seqid synchronization. 12803 */ 12804 static void 12805 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 12806 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 12807 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 12808 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 12809 { 12810 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 12811 int error; 12812 12813 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 12814 (CE_NOTE, 12815 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 12816 ASSERT(resend_rqstp != NULL); 12817 ASSERT(resend_rqstp->lr_op == OP_LOCK || 12818 resend_rqstp->lr_op == OP_LOCKU); 12819 12820 *oopp = resend_rqstp->lr_oop; 12821 if (resend_rqstp->lr_oop) { 12822 open_owner_hold(resend_rqstp->lr_oop); 12823 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 12824 ASSERT(error == 0); /* recov thread always succeeds */ 12825 } 12826 12827 /* Must resend this lost lock/locku request. */ 12828 ASSERT(resend_rqstp->lr_lop != NULL); 12829 *lopp = resend_rqstp->lr_lop; 12830 lock_owner_hold(resend_rqstp->lr_lop); 12831 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 12832 ASSERT(error == 0); /* recov thread always succeeds */ 12833 12834 *ospp = resend_rqstp->lr_osp; 12835 if (*ospp) 12836 open_stream_hold(resend_rqstp->lr_osp); 12837 12838 if (resend_rqstp->lr_op == OP_LOCK) { 12839 LOCK4args *lock_args; 12840 12841 argop->argop = OP_LOCK; 12842 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 12843 lock_args->locktype = resend_rqstp->lr_locktype; 12844 lock_args->reclaim = 12845 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 12846 lock_args->offset = resend_rqstp->lr_flk->l_start; 12847 lock_args->length = resend_rqstp->lr_flk->l_len; 12848 if (lock_args->length == 0) 12849 lock_args->length = ~lock_args->length; 12850 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 12851 mi2clientid(mi), &lock_args->locker); 12852 12853 switch (resend_rqstp->lr_ctype) { 12854 case NFS4_LCK_CTYPE_RESEND: 12855 argsp->ctag = TAG_LOCK_RESEND; 12856 break; 12857 case NFS4_LCK_CTYPE_REINSTATE: 12858 argsp->ctag = TAG_LOCK_REINSTATE; 12859 break; 12860 case NFS4_LCK_CTYPE_RECLAIM: 12861 argsp->ctag = TAG_LOCK_RECLAIM; 12862 break; 12863 default: 12864 argsp->ctag = TAG_LOCK_UNKNOWN; 12865 break; 12866 } 12867 } else { 12868 LOCKU4args *locku_args; 12869 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 12870 12871 argop->argop = OP_LOCKU; 12872 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 12873 locku_args->locktype = READ_LT; 12874 locku_args->seqid = lop->lock_seqid + 1; 12875 mutex_enter(&lop->lo_lock); 12876 locku_args->lock_stateid = lop->lock_stateid; 12877 mutex_exit(&lop->lo_lock); 12878 locku_args->offset = resend_rqstp->lr_flk->l_start; 12879 locku_args->length = resend_rqstp->lr_flk->l_len; 12880 if (locku_args->length == 0) 12881 locku_args->length = ~locku_args->length; 12882 12883 switch (resend_rqstp->lr_ctype) { 12884 case NFS4_LCK_CTYPE_RESEND: 12885 argsp->ctag = TAG_LOCKU_RESEND; 12886 break; 12887 case NFS4_LCK_CTYPE_REINSTATE: 12888 argsp->ctag = TAG_LOCKU_REINSTATE; 12889 break; 12890 default: 12891 argsp->ctag = TAG_LOCK_UNKNOWN; 12892 break; 12893 } 12894 } 12895 } 12896 12897 /* 12898 * Setup the LOCKT4 arguments. 12899 */ 12900 static void 12901 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12902 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 12903 rnode4_t *rp) 12904 { 12905 LOCKT4args *lockt_args; 12906 12907 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 12908 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12909 argop->argop = OP_LOCKT; 12910 argsp->ctag = TAG_LOCKT; 12911 lockt_args = &argop->nfs_argop4_u.oplockt; 12912 12913 /* 12914 * The locktype will be READ_LT unless it's 12915 * a write lock. We do this because the Solaris 12916 * system call allows the combination of 12917 * F_UNLCK and F_GETLK* and so in that case the 12918 * unlock is mapped to a read. 12919 */ 12920 if (flk->l_type == F_WRLCK) 12921 lockt_args->locktype = WRITE_LT; 12922 else 12923 lockt_args->locktype = READ_LT; 12924 12925 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 12926 /* set the lock owner4 args */ 12927 nfs4_setlockowner_args(&lockt_args->owner, rp, 12928 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 12929 flk->l_pid); 12930 lockt_args->offset = flk->l_start; 12931 lockt_args->length = flk->l_len; 12932 if (flk->l_len == 0) 12933 lockt_args->length = ~lockt_args->length; 12934 12935 *lockt_argsp = lockt_args; 12936 } 12937 12938 /* 12939 * If the client is holding a delegation, and the open stream to be used 12940 * with this lock request is a delegation open stream, then re-open the stream. 12941 * Sets the nfs4_error_t to all zeros unless the open stream has already 12942 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 12943 * means the caller should retry (like a recovery retry). 12944 */ 12945 static void 12946 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 12947 { 12948 open_delegation_type4 dt; 12949 bool_t reopen_needed, force; 12950 nfs4_open_stream_t *osp; 12951 open_claim_type4 oclaim; 12952 rnode4_t *rp = VTOR4(vp); 12953 mntinfo4_t *mi = VTOMI4(vp); 12954 12955 ASSERT(nfs_zone() == mi->mi_zone); 12956 12957 nfs4_error_zinit(ep); 12958 12959 mutex_enter(&rp->r_statev4_lock); 12960 dt = rp->r_deleg_type; 12961 mutex_exit(&rp->r_statev4_lock); 12962 12963 if (dt != OPEN_DELEGATE_NONE) { 12964 nfs4_open_owner_t *oop; 12965 12966 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 12967 if (!oop) { 12968 ep->stat = NFS4ERR_IO; 12969 return; 12970 } 12971 /* returns with 'os_sync_lock' held */ 12972 osp = find_open_stream(oop, rp); 12973 if (!osp) { 12974 open_owner_rele(oop); 12975 ep->stat = NFS4ERR_IO; 12976 return; 12977 } 12978 12979 if (osp->os_failed_reopen) { 12980 NFS4_DEBUG((nfs4_open_stream_debug || 12981 nfs4_client_lock_debug), (CE_NOTE, 12982 "nfs4frlock_check_deleg: os_failed_reopen set " 12983 "for osp %p, cr %p, rp %s", (void *)osp, 12984 (void *)cr, rnode4info(rp))); 12985 mutex_exit(&osp->os_sync_lock); 12986 open_stream_rele(osp, rp); 12987 open_owner_rele(oop); 12988 ep->stat = NFS4ERR_IO; 12989 return; 12990 } 12991 12992 /* 12993 * Determine whether a reopen is needed. If this 12994 * is a delegation open stream, then send the open 12995 * to the server to give visibility to the open owner. 12996 * Even if it isn't a delegation open stream, we need 12997 * to check if the previous open CLAIM_DELEGATE_CUR 12998 * was sufficient. 12999 */ 13000 13001 reopen_needed = osp->os_delegation || 13002 ((lt == F_RDLCK && 13003 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13004 (lt == F_WRLCK && 13005 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13006 13007 mutex_exit(&osp->os_sync_lock); 13008 open_owner_rele(oop); 13009 13010 if (reopen_needed) { 13011 /* 13012 * Always use CLAIM_PREVIOUS after server reboot. 13013 * The server will reject CLAIM_DELEGATE_CUR if 13014 * it is used during the grace period. 13015 */ 13016 mutex_enter(&mi->mi_lock); 13017 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13018 oclaim = CLAIM_PREVIOUS; 13019 force = TRUE; 13020 } else { 13021 oclaim = CLAIM_DELEGATE_CUR; 13022 force = FALSE; 13023 } 13024 mutex_exit(&mi->mi_lock); 13025 13026 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13027 if (ep->error == EAGAIN) { 13028 nfs4_error_zinit(ep); 13029 ep->stat = NFS4ERR_DELAY; 13030 } 13031 } 13032 open_stream_rele(osp, rp); 13033 osp = NULL; 13034 } 13035 } 13036 13037 /* 13038 * Setup the LOCKU4 arguments. 13039 * Returns errors via the nfs4_error_t. 13040 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13041 * over-the-wire. The caller must release the 13042 * reference on *lopp. 13043 * NFS4ERR_DELAY caller should retry (like recovery retry) 13044 * (other) unrecoverable error. 13045 */ 13046 static void 13047 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13048 LOCKU4args **locku_argsp, flock64_t *flk, 13049 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13050 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13051 bool_t *skip_get_err, bool_t *go_otwp) 13052 { 13053 nfs4_lock_owner_t *lop = NULL; 13054 LOCKU4args *locku_args; 13055 pid_t pid; 13056 bool_t is_spec = FALSE; 13057 rnode4_t *rp = VTOR4(vp); 13058 13059 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13060 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13061 13062 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13063 if (ep->error || ep->stat) 13064 return; 13065 13066 argop->argop = OP_LOCKU; 13067 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13068 argsp->ctag = TAG_LOCKU_REINSTATE; 13069 else 13070 argsp->ctag = TAG_LOCKU; 13071 locku_args = &argop->nfs_argop4_u.oplocku; 13072 *locku_argsp = locku_args; 13073 13074 /* 13075 * XXX what should locku_args->locktype be? 13076 * setting to ALWAYS be READ_LT so at least 13077 * it is a valid locktype. 13078 */ 13079 13080 locku_args->locktype = READ_LT; 13081 13082 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13083 flk->l_pid; 13084 13085 /* 13086 * Get the lock owner stateid. If no lock owner 13087 * exists, return success. 13088 */ 13089 lop = find_lock_owner(rp, pid, LOWN_ANY); 13090 *lopp = lop; 13091 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13092 is_spec = TRUE; 13093 if (!lop || is_spec) { 13094 /* 13095 * No lock owner so no locks to unlock. 13096 * Return success. If there was a failed 13097 * reclaim earlier, the lock might still be 13098 * registered with the local locking code, 13099 * so notify it of the unlock. 13100 * 13101 * If the lockowner is using a special stateid, 13102 * then the original lock request (that created 13103 * this lockowner) was never successful, so we 13104 * have no lock to undo OTW. 13105 */ 13106 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13107 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13108 "(%ld) so return success", (long)pid)); 13109 13110 if (ctype == NFS4_LCK_CTYPE_NORM) 13111 flk->l_pid = curproc->p_pid; 13112 nfs4_register_lock_locally(vp, flk, flag, offset); 13113 /* 13114 * Release our hold and NULL out so final_cleanup 13115 * doesn't try to end a lock seqid sync we 13116 * never started. 13117 */ 13118 if (is_spec) { 13119 lock_owner_rele(lop); 13120 *lopp = NULL; 13121 } 13122 *skip_get_err = TRUE; 13123 *go_otwp = FALSE; 13124 return; 13125 } 13126 13127 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13128 if (ep->error == EAGAIN) { 13129 lock_owner_rele(lop); 13130 *lopp = NULL; 13131 return; 13132 } 13133 13134 mutex_enter(&lop->lo_lock); 13135 locku_args->lock_stateid = lop->lock_stateid; 13136 mutex_exit(&lop->lo_lock); 13137 locku_args->seqid = lop->lock_seqid + 1; 13138 13139 /* leave the ref count on lop, rele after RPC call */ 13140 13141 locku_args->offset = flk->l_start; 13142 locku_args->length = flk->l_len; 13143 if (flk->l_len == 0) 13144 locku_args->length = ~locku_args->length; 13145 13146 *go_otwp = TRUE; 13147 } 13148 13149 /* 13150 * Setup the LOCK4 arguments. 13151 * 13152 * Returns errors via the nfs4_error_t. 13153 * NFS4_OK no problems 13154 * NFS4ERR_DELAY caller should retry (like recovery retry) 13155 * (other) unrecoverable error 13156 */ 13157 static void 13158 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13159 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13160 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13161 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13162 { 13163 LOCK4args *lock_args; 13164 nfs4_open_owner_t *oop = NULL; 13165 nfs4_open_stream_t *osp = NULL; 13166 nfs4_lock_owner_t *lop = NULL; 13167 pid_t pid; 13168 rnode4_t *rp = VTOR4(vp); 13169 13170 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13171 13172 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13173 if (ep->error || ep->stat != NFS4_OK) 13174 return; 13175 13176 argop->argop = OP_LOCK; 13177 if (ctype == NFS4_LCK_CTYPE_NORM) 13178 argsp->ctag = TAG_LOCK; 13179 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13180 argsp->ctag = TAG_RELOCK; 13181 else 13182 argsp->ctag = TAG_LOCK_REINSTATE; 13183 lock_args = &argop->nfs_argop4_u.oplock; 13184 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13185 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13186 /* 13187 * Get the lock owner. If no lock owner exists, 13188 * create a 'temporary' one and grab the open seqid 13189 * synchronization (which puts a hold on the open 13190 * owner and open stream). 13191 * This also grabs the lock seqid synchronization. 13192 */ 13193 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13194 ep->stat = 13195 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13196 13197 if (ep->stat != NFS4_OK) 13198 goto out; 13199 13200 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13201 &lock_args->locker); 13202 13203 lock_args->offset = flk->l_start; 13204 lock_args->length = flk->l_len; 13205 if (flk->l_len == 0) 13206 lock_args->length = ~lock_args->length; 13207 *lock_argsp = lock_args; 13208 out: 13209 *oopp = oop; 13210 *ospp = osp; 13211 *lopp = lop; 13212 } 13213 13214 /* 13215 * After we get the reply from the server, record the proper information 13216 * for possible resend lock requests. 13217 * 13218 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13219 */ 13220 static void 13221 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13222 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13223 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13224 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13225 { 13226 bool_t unlock = (flk->l_type == F_UNLCK); 13227 13228 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13229 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13230 ctype == NFS4_LCK_CTYPE_REINSTATE); 13231 13232 if (error != 0 && !unlock) { 13233 NFS4_DEBUG((nfs4_lost_rqst_debug || 13234 nfs4_client_lock_debug), (CE_NOTE, 13235 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13236 " for lop %p", (void *)lop)); 13237 ASSERT(lop != NULL); 13238 mutex_enter(&lop->lo_lock); 13239 lop->lo_pending_rqsts = 1; 13240 mutex_exit(&lop->lo_lock); 13241 } 13242 13243 lost_rqstp->lr_putfirst = FALSE; 13244 lost_rqstp->lr_op = 0; 13245 13246 /* 13247 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13248 * recovery purposes so that the lock request that was sent 13249 * can be saved and re-issued later. Ditto for EIO from a forced 13250 * unmount. This is done to have the client's local locking state 13251 * match the v4 server's state; that is, the request was 13252 * potentially received and accepted by the server but the client 13253 * thinks it was not. 13254 */ 13255 if (error == ETIMEDOUT || error == EINTR || 13256 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13257 NFS4_DEBUG((nfs4_lost_rqst_debug || 13258 nfs4_client_lock_debug), (CE_NOTE, 13259 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13260 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13261 (void *)lop, (void *)oop, (void *)osp)); 13262 if (unlock) 13263 lost_rqstp->lr_op = OP_LOCKU; 13264 else { 13265 lost_rqstp->lr_op = OP_LOCK; 13266 lost_rqstp->lr_locktype = locktype; 13267 } 13268 /* 13269 * Objects are held and rele'd via the recovery code. 13270 * See nfs4_save_lost_rqst. 13271 */ 13272 lost_rqstp->lr_vp = vp; 13273 lost_rqstp->lr_dvp = NULL; 13274 lost_rqstp->lr_oop = oop; 13275 lost_rqstp->lr_osp = osp; 13276 lost_rqstp->lr_lop = lop; 13277 lost_rqstp->lr_cr = cr; 13278 switch (ctype) { 13279 case NFS4_LCK_CTYPE_NORM: 13280 flk->l_pid = ttoproc(curthread)->p_pid; 13281 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13282 break; 13283 case NFS4_LCK_CTYPE_REINSTATE: 13284 lost_rqstp->lr_putfirst = TRUE; 13285 lost_rqstp->lr_ctype = ctype; 13286 break; 13287 default: 13288 break; 13289 } 13290 lost_rqstp->lr_flk = flk; 13291 } 13292 } 13293 13294 /* 13295 * Update lop's seqid. Also update the seqid stored in a resend request, 13296 * if any. (Some recovery errors increment the seqid, and we may have to 13297 * send the resend request again.) 13298 */ 13299 13300 static void 13301 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13302 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13303 { 13304 if (lock_args) { 13305 if (lock_args->locker.new_lock_owner == TRUE) 13306 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13307 else { 13308 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13309 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13310 } 13311 } else if (locku_args) { 13312 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13313 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13314 } 13315 } 13316 13317 /* 13318 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13319 * COMPOUND4 args/res for calls that need to retry. 13320 * Switches the *cred_otwp to base_cr. 13321 */ 13322 static void 13323 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13324 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13325 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13326 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13327 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13328 { 13329 nfs4_open_owner_t *oop = *oopp; 13330 nfs4_open_stream_t *osp = *ospp; 13331 nfs4_lock_owner_t *lop = *lopp; 13332 nfs_argop4 *argop = (*argspp)->array; 13333 13334 if (*did_start_fop) { 13335 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13336 needrecov); 13337 *did_start_fop = FALSE; 13338 } 13339 ASSERT((*argspp)->array_len == 2); 13340 if (argop[1].argop == OP_LOCK) 13341 nfs4args_lock_free(&argop[1]); 13342 else if (argop[1].argop == OP_LOCKT) 13343 nfs4args_lockt_free(&argop[1]); 13344 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13345 if (!error) 13346 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13347 *argspp = NULL; 13348 *respp = NULL; 13349 13350 if (lop) { 13351 nfs4_end_lock_seqid_sync(lop); 13352 lock_owner_rele(lop); 13353 *lopp = NULL; 13354 } 13355 13356 /* need to free up the reference on osp for lock args */ 13357 if (osp != NULL) { 13358 open_stream_rele(osp, VTOR4(vp)); 13359 *ospp = NULL; 13360 } 13361 13362 /* need to free up the reference on oop for lock args */ 13363 if (oop != NULL) { 13364 nfs4_end_open_seqid_sync(oop); 13365 open_owner_rele(oop); 13366 *oopp = NULL; 13367 } 13368 13369 crfree(*cred_otwp); 13370 *cred_otwp = base_cr; 13371 crhold(*cred_otwp); 13372 } 13373 13374 /* 13375 * Function to process the client's recovery for nfs4frlock. 13376 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13377 * 13378 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13379 * COMPOUND4 args/res for calls that need to retry. 13380 * 13381 * Note: the rp's r_lkserlock is *not* dropped during this path. 13382 */ 13383 static bool_t 13384 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13385 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13386 LOCK4args *lock_args, LOCKU4args *locku_args, 13387 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13388 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13389 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13390 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13391 { 13392 nfs4_open_owner_t *oop = *oopp; 13393 nfs4_open_stream_t *osp = *ospp; 13394 nfs4_lock_owner_t *lop = *lopp; 13395 13396 bool_t abort, retry; 13397 13398 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13399 ASSERT((*argspp) != NULL); 13400 ASSERT((*respp) != NULL); 13401 if (lock_args || locku_args) 13402 ASSERT(lop != NULL); 13403 13404 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13405 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13406 13407 retry = TRUE; 13408 abort = FALSE; 13409 if (needrecov) { 13410 nfs4_bseqid_entry_t *bsep = NULL; 13411 nfs_opnum4 op; 13412 13413 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13414 13415 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13416 seqid4 seqid; 13417 13418 if (lock_args) { 13419 if (lock_args->locker.new_lock_owner == TRUE) 13420 seqid = lock_args->locker.locker4_u. 13421 open_owner.open_seqid; 13422 else 13423 seqid = lock_args->locker.locker4_u. 13424 lock_owner.lock_seqid; 13425 } else if (locku_args) { 13426 seqid = locku_args->seqid; 13427 } else { 13428 seqid = 0; 13429 } 13430 13431 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13432 flk->l_pid, (*argspp)->ctag, seqid); 13433 } 13434 13435 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13436 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13437 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13438 NULL, op, bsep); 13439 13440 if (bsep) 13441 kmem_free(bsep, sizeof (*bsep)); 13442 } 13443 13444 /* 13445 * Return that we do not want to retry the request for 3 cases: 13446 * 1. If we received EINTR or are bailing out because of a forced 13447 * unmount, we came into this code path just for the sake of 13448 * initiating recovery, we now need to return the error. 13449 * 2. If we have aborted recovery. 13450 * 3. We received NFS4ERR_BAD_SEQID. 13451 */ 13452 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13453 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13454 retry = FALSE; 13455 13456 if (*did_start_fop == TRUE) { 13457 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13458 needrecov); 13459 *did_start_fop = FALSE; 13460 } 13461 13462 if (retry == TRUE) { 13463 nfs_argop4 *argop; 13464 13465 argop = (*argspp)->array; 13466 ASSERT((*argspp)->array_len == 2); 13467 13468 if (argop[1].argop == OP_LOCK) 13469 nfs4args_lock_free(&argop[1]); 13470 else if (argop[1].argop == OP_LOCKT) 13471 nfs4args_lockt_free(&argop[1]); 13472 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13473 if (!ep->error) 13474 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13475 *respp = NULL; 13476 *argspp = NULL; 13477 } 13478 13479 if (lop != NULL) { 13480 nfs4_end_lock_seqid_sync(lop); 13481 lock_owner_rele(lop); 13482 } 13483 13484 *lopp = NULL; 13485 13486 /* need to free up the reference on osp for lock args */ 13487 if (osp != NULL) { 13488 open_stream_rele(osp, rp); 13489 *ospp = NULL; 13490 } 13491 13492 /* need to free up the reference on oop for lock args */ 13493 if (oop != NULL) { 13494 nfs4_end_open_seqid_sync(oop); 13495 open_owner_rele(oop); 13496 *oopp = NULL; 13497 } 13498 13499 return (retry); 13500 } 13501 13502 /* 13503 * Handles the succesful reply from the server for nfs4frlock. 13504 */ 13505 static void 13506 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13507 vnode_t *vp, int flag, u_offset_t offset, 13508 nfs4_lost_rqst_t *resend_rqstp) 13509 { 13510 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13511 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13512 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13513 if (ctype == NFS4_LCK_CTYPE_NORM) { 13514 flk->l_pid = ttoproc(curthread)->p_pid; 13515 /* 13516 * We do not register lost locks locally in 13517 * the 'resend' case since the user/application 13518 * doesn't think we have the lock. 13519 */ 13520 ASSERT(!resend_rqstp); 13521 nfs4_register_lock_locally(vp, flk, flag, offset); 13522 } 13523 } 13524 } 13525 13526 /* 13527 * Handle the DENIED reply from the server for nfs4frlock. 13528 * Returns TRUE if we should retry the request; FALSE otherwise. 13529 * 13530 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13531 * COMPOUND4 args/res for calls that need to retry. Can also 13532 * drop and regrab the r_lkserlock. 13533 */ 13534 static bool_t 13535 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13536 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13537 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13538 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13539 nfs4_recov_state_t *recov_statep, int needrecov, 13540 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13541 clock_t *tick_delayp, short *whencep, int *errorp, 13542 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13543 bool_t *skip_get_err) 13544 { 13545 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13546 13547 if (lock_args) { 13548 nfs4_open_owner_t *oop = *oopp; 13549 nfs4_open_stream_t *osp = *ospp; 13550 nfs4_lock_owner_t *lop = *lopp; 13551 int intr; 13552 13553 /* 13554 * Blocking lock needs to sleep and retry from the request. 13555 * 13556 * Do not block and wait for 'resend' or 'reinstate' 13557 * lock requests, just return the error. 13558 * 13559 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13560 */ 13561 if (cmd == F_SETLKW) { 13562 rnode4_t *rp = VTOR4(vp); 13563 nfs_argop4 *argop = (*argspp)->array; 13564 13565 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13566 13567 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13568 recov_statep, needrecov); 13569 *did_start_fop = FALSE; 13570 ASSERT((*argspp)->array_len == 2); 13571 if (argop[1].argop == OP_LOCK) 13572 nfs4args_lock_free(&argop[1]); 13573 else if (argop[1].argop == OP_LOCKT) 13574 nfs4args_lockt_free(&argop[1]); 13575 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13576 if (*respp) 13577 (void) xdr_free(xdr_COMPOUND4res_clnt, 13578 (caddr_t)*respp); 13579 *argspp = NULL; 13580 *respp = NULL; 13581 nfs4_end_lock_seqid_sync(lop); 13582 lock_owner_rele(lop); 13583 *lopp = NULL; 13584 if (osp != NULL) { 13585 open_stream_rele(osp, rp); 13586 *ospp = NULL; 13587 } 13588 if (oop != NULL) { 13589 nfs4_end_open_seqid_sync(oop); 13590 open_owner_rele(oop); 13591 *oopp = NULL; 13592 } 13593 13594 nfs_rw_exit(&rp->r_lkserlock); 13595 13596 intr = nfs4_block_and_wait(tick_delayp, rp); 13597 13598 if (intr) { 13599 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13600 RW_WRITER, FALSE); 13601 *errorp = EINTR; 13602 return (FALSE); 13603 } 13604 13605 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13606 RW_WRITER, FALSE); 13607 13608 /* 13609 * Make sure we are still safe to lock with 13610 * regards to mmapping. 13611 */ 13612 if (!nfs4_safelock(vp, flk, cr)) { 13613 *errorp = EAGAIN; 13614 return (FALSE); 13615 } 13616 13617 return (TRUE); 13618 } 13619 if (ctype == NFS4_LCK_CTYPE_NORM) 13620 *errorp = EAGAIN; 13621 *skip_get_err = TRUE; 13622 flk->l_whence = 0; 13623 *whencep = 0; 13624 return (FALSE); 13625 } else if (lockt_args) { 13626 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13627 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13628 13629 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13630 flk, lockt_args); 13631 13632 /* according to NLM code */ 13633 *errorp = 0; 13634 *whencep = 0; 13635 *skip_get_err = TRUE; 13636 return (FALSE); 13637 } 13638 return (FALSE); 13639 } 13640 13641 /* 13642 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13643 */ 13644 static void 13645 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13646 { 13647 switch (resp->status) { 13648 case NFS4ERR_ACCESS: 13649 case NFS4ERR_ADMIN_REVOKED: 13650 case NFS4ERR_BADHANDLE: 13651 case NFS4ERR_BAD_RANGE: 13652 case NFS4ERR_BAD_SEQID: 13653 case NFS4ERR_BAD_STATEID: 13654 case NFS4ERR_BADXDR: 13655 case NFS4ERR_DEADLOCK: 13656 case NFS4ERR_DELAY: 13657 case NFS4ERR_EXPIRED: 13658 case NFS4ERR_FHEXPIRED: 13659 case NFS4ERR_GRACE: 13660 case NFS4ERR_INVAL: 13661 case NFS4ERR_ISDIR: 13662 case NFS4ERR_LEASE_MOVED: 13663 case NFS4ERR_LOCK_NOTSUPP: 13664 case NFS4ERR_LOCK_RANGE: 13665 case NFS4ERR_MOVED: 13666 case NFS4ERR_NOFILEHANDLE: 13667 case NFS4ERR_NO_GRACE: 13668 case NFS4ERR_OLD_STATEID: 13669 case NFS4ERR_OPENMODE: 13670 case NFS4ERR_RECLAIM_BAD: 13671 case NFS4ERR_RECLAIM_CONFLICT: 13672 case NFS4ERR_RESOURCE: 13673 case NFS4ERR_SERVERFAULT: 13674 case NFS4ERR_STALE: 13675 case NFS4ERR_STALE_CLIENTID: 13676 case NFS4ERR_STALE_STATEID: 13677 return; 13678 default: 13679 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13680 "nfs4frlock_results_default: got unrecognizable " 13681 "res.status %d", resp->status)); 13682 *errorp = NFS4ERR_INVAL; 13683 } 13684 } 13685 13686 /* 13687 * The lock request was successful, so update the client's state. 13688 */ 13689 static void 13690 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13691 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13692 vnode_t *vp, flock64_t *flk, cred_t *cr, 13693 nfs4_lost_rqst_t *resend_rqstp) 13694 { 13695 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13696 13697 if (lock_args) { 13698 LOCK4res *lock_res; 13699 13700 lock_res = &resop->nfs_resop4_u.oplock; 13701 /* update the stateid with server's response */ 13702 13703 if (lock_args->locker.new_lock_owner == TRUE) { 13704 mutex_enter(&lop->lo_lock); 13705 lop->lo_just_created = NFS4_PERM_CREATED; 13706 mutex_exit(&lop->lo_lock); 13707 } 13708 13709 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13710 13711 /* 13712 * If the lock was the result of a resending a lost 13713 * request, we've synched up the stateid and seqid 13714 * with the server, but now the server might be out of sync 13715 * with what the application thinks it has for locks. 13716 * Clean that up here. It's unclear whether we should do 13717 * this even if the filesystem has been forcibly unmounted. 13718 * For most servers, it's probably wasted effort, but 13719 * RFC3530 lets servers require that unlocks exactly match 13720 * the locks that are held. 13721 */ 13722 if (resend_rqstp != NULL && 13723 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13724 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 13725 } else { 13726 flk->l_whence = 0; 13727 } 13728 } else if (locku_args) { 13729 LOCKU4res *locku_res; 13730 13731 locku_res = &resop->nfs_resop4_u.oplocku; 13732 13733 /* Update the stateid with the server's response */ 13734 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 13735 } else if (lockt_args) { 13736 /* Switch the lock type to express success, see fcntl */ 13737 flk->l_type = F_UNLCK; 13738 flk->l_whence = 0; 13739 } 13740 } 13741 13742 /* 13743 * Do final cleanup before exiting nfs4frlock. 13744 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13745 * COMPOUND4 args/res for calls that haven't already. 13746 */ 13747 static void 13748 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 13749 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 13750 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 13751 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13752 short whence, u_offset_t offset, struct lm_sysid *ls, 13753 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 13754 bool_t did_start_fop, bool_t skip_get_err, 13755 cred_t *cred_otw, cred_t *cred) 13756 { 13757 mntinfo4_t *mi = VTOMI4(vp); 13758 rnode4_t *rp = VTOR4(vp); 13759 int error = *errorp; 13760 nfs_argop4 *argop; 13761 13762 ASSERT(nfs_zone() == mi->mi_zone); 13763 /* 13764 * The client recovery code wants the raw status information, 13765 * so don't map the NFS status code to an errno value for 13766 * non-normal call types. 13767 */ 13768 if (ctype == NFS4_LCK_CTYPE_NORM) { 13769 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 13770 *errorp = geterrno4(resp->status); 13771 if (did_start_fop == TRUE) 13772 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 13773 needrecov); 13774 13775 if (!error && resp && resp->status == NFS4_OK) { 13776 /* 13777 * We've established a new lock on the server, so invalidate 13778 * the pages associated with the vnode to get the most up to 13779 * date pages from the server after acquiring the lock. We 13780 * want to be sure that the read operation gets the newest data. 13781 * N.B. 13782 * We used to do this in nfs4frlock_results_ok but that doesn't 13783 * work since VOP_PUTPAGE can call nfs4_commit which calls 13784 * nfs4_start_fop. We flush the pages below after calling 13785 * nfs4_end_fop above 13786 */ 13787 int error; 13788 13789 error = VOP_PUTPAGE(vp, (u_offset_t)0, 13790 0, B_INVAL, cred); 13791 13792 if (error && (error == ENOSPC || error == EDQUOT)) { 13793 rnode4_t *rp = VTOR4(vp); 13794 13795 mutex_enter(&rp->r_statelock); 13796 if (!rp->r_error) 13797 rp->r_error = error; 13798 mutex_exit(&rp->r_statelock); 13799 } 13800 } 13801 } 13802 if (argsp) { 13803 ASSERT(argsp->array_len == 2); 13804 argop = argsp->array; 13805 if (argop[1].argop == OP_LOCK) 13806 nfs4args_lock_free(&argop[1]); 13807 else if (argop[1].argop == OP_LOCKT) 13808 nfs4args_lockt_free(&argop[1]); 13809 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13810 if (resp) 13811 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 13812 } 13813 13814 /* free the reference on the lock owner */ 13815 if (lop != NULL) { 13816 nfs4_end_lock_seqid_sync(lop); 13817 lock_owner_rele(lop); 13818 } 13819 13820 /* need to free up the reference on osp for lock args */ 13821 if (osp != NULL) 13822 open_stream_rele(osp, rp); 13823 13824 /* need to free up the reference on oop for lock args */ 13825 if (oop != NULL) { 13826 nfs4_end_open_seqid_sync(oop); 13827 open_owner_rele(oop); 13828 } 13829 13830 (void) convoff(vp, flk, whence, offset); 13831 13832 lm_rel_sysid(ls); 13833 13834 /* 13835 * Record debug information in the event we get EINVAL. 13836 */ 13837 mutex_enter(&mi->mi_lock); 13838 if (*errorp == EINVAL && (lock_args || locku_args) && 13839 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 13840 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 13841 zcmn_err(getzoneid(), CE_NOTE, 13842 "%s operation failed with " 13843 "EINVAL probably since the server, %s," 13844 " doesn't support POSIX style locking", 13845 lock_args ? "LOCK" : "LOCKU", 13846 mi->mi_curr_serv->sv_hostname); 13847 mi->mi_flags |= MI4_LOCK_DEBUG; 13848 } 13849 } 13850 mutex_exit(&mi->mi_lock); 13851 13852 if (cred_otw) 13853 crfree(cred_otw); 13854 } 13855 13856 /* 13857 * This calls the server and the local locking code. 13858 * 13859 * Client locks are registerred locally by oring the sysid with 13860 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 13861 * We need to distinguish between the two to avoid collision in case one 13862 * machine is used as both client and server. 13863 * 13864 * Blocking lock requests will continually retry to acquire the lock 13865 * forever. 13866 * 13867 * The ctype is defined as follows: 13868 * NFS4_LCK_CTYPE_NORM: normal lock request. 13869 * 13870 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 13871 * recovery, get the pid from flk instead of curproc, and don't reregister 13872 * the lock locally. 13873 * 13874 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 13875 * that we will use the information passed in via resend_rqstp to setup the 13876 * lock/locku request. This resend is the exact same request as the 'lost 13877 * lock', and is initiated by the recovery framework. A successful resend 13878 * request can initiate one or more reinstate requests. 13879 * 13880 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 13881 * does not trigger additional reinstate requests. This lock call type is 13882 * set for setting the v4 server's locking state back to match what the 13883 * client's local locking state is in the event of a received 'lost lock'. 13884 * 13885 * Errors are returned via the nfs4_error_t parameter. 13886 */ 13887 void 13888 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 13889 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 13890 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 13891 { 13892 COMPOUND4args_clnt args, *argsp = NULL; 13893 COMPOUND4res_clnt res, *resp = NULL; 13894 nfs_argop4 *argop; 13895 nfs_resop4 *resop; 13896 rnode4_t *rp; 13897 int doqueue = 1; 13898 clock_t tick_delay; /* delay in clock ticks */ 13899 struct lm_sysid *ls; 13900 LOCK4args *lock_args = NULL; 13901 LOCKU4args *locku_args = NULL; 13902 LOCKT4args *lockt_args = NULL; 13903 nfs4_open_owner_t *oop = NULL; 13904 nfs4_open_stream_t *osp = NULL; 13905 nfs4_lock_owner_t *lop = NULL; 13906 bool_t needrecov = FALSE; 13907 nfs4_recov_state_t recov_state; 13908 short whence; 13909 nfs4_op_hint_t op_hint; 13910 nfs4_lost_rqst_t lost_rqst; 13911 bool_t retry = FALSE; 13912 bool_t did_start_fop = FALSE; 13913 bool_t skip_get_err = FALSE; 13914 cred_t *cred_otw = NULL; 13915 bool_t recovonly; /* just queue request */ 13916 int frc_no_reclaim = 0; 13917 #ifdef DEBUG 13918 char *name; 13919 #endif 13920 13921 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13922 13923 #ifdef DEBUG 13924 name = fn_name(VTOSV(vp)->sv_name); 13925 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 13926 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 13927 "length %"PRIu64", pid %d, sysid %d, call type %s, " 13928 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 13929 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 13930 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 13931 resend_rqstp ? "TRUE" : "FALSE")); 13932 kmem_free(name, MAXNAMELEN); 13933 #endif 13934 13935 nfs4_error_zinit(ep); 13936 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 13937 if (ep->error) 13938 return; 13939 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 13940 if (ep->error) 13941 return; 13942 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 13943 vp, cr, &cred_otw); 13944 13945 recov_retry: 13946 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 13947 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 13948 rp = VTOR4(vp); 13949 13950 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 13951 &did_start_fop, &recovonly); 13952 13953 if (ep->error) 13954 goto out; 13955 13956 if (recovonly) { 13957 /* 13958 * Leave the request for the recovery system to deal with. 13959 */ 13960 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13961 ASSERT(cmd != F_GETLK); 13962 ASSERT(flk->l_type == F_UNLCK); 13963 13964 nfs4_error_init(ep, EINTR); 13965 needrecov = TRUE; 13966 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 13967 if (lop != NULL) { 13968 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 13969 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 13970 (void) nfs4_start_recovery(ep, 13971 VTOMI4(vp), vp, NULL, NULL, 13972 (lost_rqst.lr_op == OP_LOCK || 13973 lost_rqst.lr_op == OP_LOCKU) ? 13974 &lost_rqst : NULL, OP_LOCKU, NULL); 13975 lock_owner_rele(lop); 13976 lop = NULL; 13977 } 13978 flk->l_pid = curproc->p_pid; 13979 nfs4_register_lock_locally(vp, flk, flag, offset); 13980 goto out; 13981 } 13982 13983 /* putfh directory fh */ 13984 argop[0].argop = OP_CPUTFH; 13985 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 13986 13987 /* 13988 * Set up the over-the-wire arguments and get references to the 13989 * open owner, etc. 13990 */ 13991 13992 if (ctype == NFS4_LCK_CTYPE_RESEND || 13993 ctype == NFS4_LCK_CTYPE_REINSTATE) { 13994 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 13995 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 13996 } else { 13997 bool_t go_otw = TRUE; 13998 13999 ASSERT(resend_rqstp == NULL); 14000 14001 switch (cmd) { 14002 case F_GETLK: 14003 case F_O_GETLK: 14004 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14005 &lockt_args, argsp, flk, rp); 14006 break; 14007 case F_SETLKW: 14008 case F_SETLK: 14009 if (flk->l_type == F_UNLCK) 14010 nfs4frlock_setup_locku_args(ctype, 14011 &argop[1], &locku_args, flk, 14012 &lop, ep, argsp, 14013 vp, flag, offset, cr, 14014 &skip_get_err, &go_otw); 14015 else 14016 nfs4frlock_setup_lock_args(ctype, 14017 &lock_args, &oop, &osp, &lop, &argop[1], 14018 argsp, flk, cmd, vp, cr, ep); 14019 14020 if (ep->error) 14021 goto out; 14022 14023 switch (ep->stat) { 14024 case NFS4_OK: 14025 break; 14026 case NFS4ERR_DELAY: 14027 /* recov thread never gets this error */ 14028 ASSERT(resend_rqstp == NULL); 14029 ASSERT(did_start_fop); 14030 14031 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14032 &recov_state, TRUE); 14033 did_start_fop = FALSE; 14034 if (argop[1].argop == OP_LOCK) 14035 nfs4args_lock_free(&argop[1]); 14036 else if (argop[1].argop == OP_LOCKT) 14037 nfs4args_lockt_free(&argop[1]); 14038 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14039 argsp = NULL; 14040 goto recov_retry; 14041 default: 14042 ep->error = EIO; 14043 goto out; 14044 } 14045 break; 14046 default: 14047 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14048 "nfs4_frlock: invalid cmd %d", cmd)); 14049 ep->error = EINVAL; 14050 goto out; 14051 } 14052 14053 if (!go_otw) 14054 goto out; 14055 } 14056 14057 /* XXX should we use the local reclock as a cache ? */ 14058 /* 14059 * Unregister the lock with the local locking code before 14060 * contacting the server. This avoids a potential race where 14061 * another process gets notified that it has been granted a lock 14062 * before we can unregister ourselves locally. 14063 */ 14064 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14065 if (ctype == NFS4_LCK_CTYPE_NORM) 14066 flk->l_pid = ttoproc(curthread)->p_pid; 14067 nfs4_register_lock_locally(vp, flk, flag, offset); 14068 } 14069 14070 /* 14071 * Send the server the lock request. Continually loop with a delay 14072 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14073 */ 14074 resp = &res; 14075 14076 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14077 (CE_NOTE, 14078 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14079 rnode4info(rp))); 14080 14081 if (lock_args && frc_no_reclaim) { 14082 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14083 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14084 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14085 lock_args->reclaim = FALSE; 14086 if (did_reclaimp) 14087 *did_reclaimp = 0; 14088 } 14089 14090 /* 14091 * Do the OTW call. 14092 */ 14093 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14094 14095 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14096 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14097 14098 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14099 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14100 "nfs4frlock: needrecov %d", needrecov)); 14101 14102 if (ep->error != 0 && !needrecov && ep->error != EACCES) 14103 goto out; 14104 14105 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14106 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14107 args.ctag); 14108 14109 if ((ep->error == EACCES || 14110 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14111 cred_otw != cr) { 14112 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14113 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14114 cr, &cred_otw); 14115 goto recov_retry; 14116 } 14117 14118 if (needrecov) { 14119 /* 14120 * LOCKT requests don't need to recover from lost 14121 * requests since they don't create/modify state. 14122 */ 14123 if ((ep->error == EINTR || 14124 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14125 lockt_args) 14126 goto out; 14127 /* 14128 * Do not attempt recovery for requests initiated by 14129 * the recovery framework. Let the framework redrive them. 14130 */ 14131 if (ctype != NFS4_LCK_CTYPE_NORM) 14132 goto out; 14133 else { 14134 ASSERT(resend_rqstp == NULL); 14135 } 14136 14137 nfs4frlock_save_lost_rqst(ctype, ep->error, 14138 flk_to_locktype(cmd, flk->l_type), 14139 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14140 14141 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14142 &resp, lock_args, locku_args, &oop, &osp, &lop, 14143 rp, vp, &recov_state, op_hint, &did_start_fop, 14144 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14145 14146 if (retry) { 14147 ASSERT(oop == NULL); 14148 ASSERT(osp == NULL); 14149 ASSERT(lop == NULL); 14150 goto recov_retry; 14151 } 14152 goto out; 14153 } 14154 14155 /* 14156 * Process the reply. 14157 */ 14158 switch (resp->status) { 14159 case NFS4_OK: 14160 resop = &resp->array[1]; 14161 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14162 resend_rqstp); 14163 /* 14164 * Have a successful lock operation, now update state. 14165 */ 14166 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14167 resop, lop, vp, flk, cr, resend_rqstp); 14168 break; 14169 14170 case NFS4ERR_DENIED: 14171 resop = &resp->array[1]; 14172 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14173 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14174 &recov_state, needrecov, &argsp, &resp, 14175 &tick_delay, &whence, &ep->error, resop, cr, 14176 &did_start_fop, &skip_get_err); 14177 14178 if (retry) { 14179 ASSERT(oop == NULL); 14180 ASSERT(osp == NULL); 14181 ASSERT(lop == NULL); 14182 goto recov_retry; 14183 } 14184 break; 14185 /* 14186 * If the server won't let us reclaim, fall-back to trying to lock 14187 * the file from scratch. Code elsewhere will check the changeinfo 14188 * to ensure the file hasn't been changed. 14189 */ 14190 case NFS4ERR_NO_GRACE: 14191 if (lock_args && lock_args->reclaim == TRUE) { 14192 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14193 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14194 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14195 frc_no_reclaim = 1; 14196 /* clean up before retrying */ 14197 needrecov = 0; 14198 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14199 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14200 &recov_state, op_hint, &did_start_fop, NULL, flk); 14201 goto recov_retry; 14202 } 14203 /* FALLTHROUGH */ 14204 14205 default: 14206 nfs4frlock_results_default(resp, &ep->error); 14207 break; 14208 } 14209 out: 14210 /* 14211 * Process and cleanup from error. Make interrupted unlock 14212 * requests look successful, since they will be handled by the 14213 * client recovery code. 14214 */ 14215 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14216 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14217 lock_args, locku_args, did_start_fop, 14218 skip_get_err, cred_otw, cr); 14219 14220 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14221 (cmd == F_SETLK || cmd == F_SETLKW)) 14222 ep->error = 0; 14223 } 14224 14225 /* 14226 * nfs4_safelock: 14227 * 14228 * Return non-zero if the given lock request can be handled without 14229 * violating the constraints on concurrent mapping and locking. 14230 */ 14231 14232 static int 14233 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14234 { 14235 rnode4_t *rp = VTOR4(vp); 14236 struct vattr va; 14237 int error; 14238 14239 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14240 ASSERT(rp->r_mapcnt >= 0); 14241 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14242 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14243 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14244 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14245 14246 if (rp->r_mapcnt == 0) 14247 return (1); /* always safe if not mapped */ 14248 14249 /* 14250 * If the file is already mapped and there are locks, then they 14251 * should be all safe locks. So adding or removing a lock is safe 14252 * as long as the new request is safe (i.e., whole-file, meaning 14253 * length and starting offset are both zero). 14254 */ 14255 14256 if (bfp->l_start != 0 || bfp->l_len != 0) { 14257 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14258 "cannot lock a memory mapped file unless locking the " 14259 "entire file: start %"PRIx64", len %"PRIx64, 14260 bfp->l_start, bfp->l_len)); 14261 return (0); 14262 } 14263 14264 /* mandatory locking and mapping don't mix */ 14265 va.va_mask = AT_MODE; 14266 error = VOP_GETATTR(vp, &va, 0, cr); 14267 if (error != 0) { 14268 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14269 "getattr error %d", error)); 14270 return (0); /* treat errors conservatively */ 14271 } 14272 if (MANDLOCK(vp, va.va_mode)) { 14273 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14274 "cannot mandatory lock and mmap a file")); 14275 return (0); 14276 } 14277 14278 return (1); 14279 } 14280 14281 14282 /* 14283 * Register the lock locally within Solaris. 14284 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14285 * recording locks locally. 14286 * 14287 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14288 * are registered locally. 14289 */ 14290 void 14291 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14292 u_offset_t offset) 14293 { 14294 int oldsysid; 14295 int error; 14296 #ifdef DEBUG 14297 char *name; 14298 #endif 14299 14300 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14301 14302 #ifdef DEBUG 14303 name = fn_name(VTOSV(vp)->sv_name); 14304 NFS4_DEBUG(nfs4_client_lock_debug, 14305 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14306 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14307 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14308 flk->l_sysid)); 14309 kmem_free(name, MAXNAMELEN); 14310 #endif 14311 14312 /* register the lock with local locking */ 14313 oldsysid = flk->l_sysid; 14314 flk->l_sysid |= LM_SYSID_CLIENT; 14315 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14316 #ifdef DEBUG 14317 if (error != 0) { 14318 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14319 "nfs4_register_lock_locally: could not register with" 14320 " local locking")); 14321 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14322 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14323 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14324 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14325 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14326 flk->l_type, flk->l_start, flk->l_len)); 14327 (void) reclock(vp, flk, 0, flag, offset, NULL); 14328 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14329 "blocked by pid %d sysid 0x%x type %d " 14330 "off 0x%" PRIx64 " len 0x%" PRIx64, 14331 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14332 flk->l_len)); 14333 } 14334 #endif 14335 flk->l_sysid = oldsysid; 14336 } 14337 14338 /* 14339 * nfs4_lockrelease: 14340 * 14341 * Release any locks on the given vnode that are held by the current 14342 * process. Also removes the lock owner (if one exists) from the rnode's 14343 * list. 14344 */ 14345 static int 14346 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14347 { 14348 flock64_t ld; 14349 int ret, error; 14350 rnode4_t *rp; 14351 nfs4_lock_owner_t *lop; 14352 nfs4_recov_state_t recov_state; 14353 mntinfo4_t *mi; 14354 bool_t possible_orphan = FALSE; 14355 bool_t recovonly; 14356 14357 ASSERT((uintptr_t)vp > KERNELBASE); 14358 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14359 14360 rp = VTOR4(vp); 14361 mi = VTOMI4(vp); 14362 14363 /* 14364 * If we have not locked anything then we can 14365 * just return since we have no work to do. 14366 */ 14367 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14368 return (0); 14369 } 14370 14371 /* 14372 * We need to comprehend that another thread may 14373 * kick off recovery and the lock_owner we have stashed 14374 * in lop might be invalid so we should NOT cache it 14375 * locally! 14376 */ 14377 recov_state.rs_flags = 0; 14378 recov_state.rs_num_retry_despite_err = 0; 14379 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14380 &recovonly); 14381 if (error) { 14382 mutex_enter(&rp->r_statelock); 14383 rp->r_flags |= R4LODANGLERS; 14384 mutex_exit(&rp->r_statelock); 14385 return (error); 14386 } 14387 14388 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14389 14390 /* 14391 * Check if the lock owner might have a lock (request was sent but 14392 * no response was received). Also check if there are any remote 14393 * locks on the file. (In theory we shouldn't have to make this 14394 * second check if there's no lock owner, but for now we'll be 14395 * conservative and do it anyway.) If either condition is true, 14396 * send an unlock for the entire file to the server. 14397 * 14398 * Note that no explicit synchronization is needed here. At worst, 14399 * flk_has_remote_locks() will return a false positive, in which case 14400 * the unlock call wastes time but doesn't harm correctness. 14401 */ 14402 14403 if (lop) { 14404 mutex_enter(&lop->lo_lock); 14405 possible_orphan = lop->lo_pending_rqsts; 14406 mutex_exit(&lop->lo_lock); 14407 lock_owner_rele(lop); 14408 } 14409 14410 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14411 14412 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14413 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14414 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14415 (void *)lop)); 14416 14417 if (possible_orphan || flk_has_remote_locks(vp)) { 14418 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14419 ld.l_whence = 0; /* unlock from start of file */ 14420 ld.l_start = 0; 14421 ld.l_len = 0; /* do entire file */ 14422 14423 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr); 14424 14425 if (ret != 0) { 14426 /* 14427 * If VOP_FRLOCK fails, make sure we unregister 14428 * local locks before we continue. 14429 */ 14430 ld.l_pid = ttoproc(curthread)->p_pid; 14431 nfs4_register_lock_locally(vp, &ld, flag, offset); 14432 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14433 "nfs4_lockrelease: lock release error on vp" 14434 " %p: error %d.\n", (void *)vp, ret)); 14435 } 14436 } 14437 14438 recov_state.rs_flags = 0; 14439 recov_state.rs_num_retry_despite_err = 0; 14440 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14441 &recovonly); 14442 if (error) { 14443 mutex_enter(&rp->r_statelock); 14444 rp->r_flags |= R4LODANGLERS; 14445 mutex_exit(&rp->r_statelock); 14446 return (error); 14447 } 14448 14449 /* 14450 * So, here we're going to need to retrieve the lock-owner 14451 * again (in case recovery has done a switch-a-roo) and 14452 * remove it because we can. 14453 */ 14454 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14455 14456 if (lop) { 14457 nfs4_rnode_remove_lock_owner(rp, lop); 14458 lock_owner_rele(lop); 14459 } 14460 14461 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14462 return (0); 14463 } 14464 14465 /* 14466 * Wait for 'tick_delay' clock ticks. 14467 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14468 * NOTE: lock_lease_time is in seconds. 14469 * 14470 * XXX For future improvements, should implement a waiting queue scheme. 14471 */ 14472 static int 14473 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14474 { 14475 long milliseconds_delay; 14476 time_t lock_lease_time; 14477 14478 /* wait tick_delay clock ticks or siginteruptus */ 14479 if (delay_sig(*tick_delay)) { 14480 return (EINTR); 14481 } 14482 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14483 "reissue the lock request: blocked for %ld clock ticks: %ld " 14484 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14485 14486 /* get the lease time */ 14487 lock_lease_time = r2lease_time(rp); 14488 14489 /* drv_hztousec converts ticks to microseconds */ 14490 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14491 if (milliseconds_delay < lock_lease_time * 1000) { 14492 *tick_delay = 2 * *tick_delay; 14493 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14494 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14495 } 14496 return (0); 14497 } 14498 14499 14500 void 14501 nfs4_vnops_init(void) 14502 { 14503 } 14504 14505 void 14506 nfs4_vnops_fini(void) 14507 { 14508 } 14509 14510 /* 14511 * Return a reference to the directory (parent) vnode for a given vnode, 14512 * using the saved pathname information and the directory file handle. The 14513 * caller is responsible for disposing of the reference. 14514 * Returns zero or an errno value. 14515 * 14516 * Caller should set need_start_op to FALSE if it is the recovery 14517 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14518 */ 14519 int 14520 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14521 { 14522 svnode_t *svnp; 14523 vnode_t *dvp = NULL; 14524 servinfo4_t *svp; 14525 nfs4_fname_t *mfname; 14526 int error; 14527 14528 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14529 14530 if (vp->v_flag & VROOT) { 14531 nfs4_sharedfh_t *sfh; 14532 nfs_fh4 fh; 14533 mntinfo4_t *mi; 14534 14535 ASSERT(vp->v_type == VREG); 14536 14537 mi = VTOMI4(vp); 14538 svp = mi->mi_curr_serv; 14539 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14540 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14541 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14542 sfh = sfh4_get(&fh, VTOMI4(vp)); 14543 nfs_rw_exit(&svp->sv_lock); 14544 mfname = mi->mi_fname; 14545 fn_hold(mfname); 14546 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14547 sfh4_rele(&sfh); 14548 14549 if (dvp->v_type == VNON) 14550 dvp->v_type = VDIR; 14551 *dvpp = dvp; 14552 return (0); 14553 } 14554 14555 svnp = VTOSV(vp); 14556 14557 if (svnp == NULL) { 14558 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14559 "shadow node is NULL")); 14560 return (EINVAL); 14561 } 14562 14563 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14564 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14565 "shadow node name or dfh val == NULL")); 14566 return (EINVAL); 14567 } 14568 14569 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14570 (int)need_start_op); 14571 if (error != 0) { 14572 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14573 "nfs4_make_dotdot returned %d", error)); 14574 return (error); 14575 } 14576 if (!dvp) { 14577 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14578 "nfs4_make_dotdot returned a NULL dvp")); 14579 return (EIO); 14580 } 14581 if (dvp->v_type == VNON) 14582 dvp->v_type = VDIR; 14583 ASSERT(dvp->v_type == VDIR); 14584 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14585 mutex_enter(&dvp->v_lock); 14586 dvp->v_flag |= V_XATTRDIR; 14587 mutex_exit(&dvp->v_lock); 14588 } 14589 *dvpp = dvp; 14590 return (0); 14591 } 14592 14593 /* 14594 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14595 * length that fnamep can accept, including the trailing null. 14596 * Returns 0 if okay, returns an errno value if there was a problem. 14597 */ 14598 14599 int 14600 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14601 { 14602 char *fn; 14603 int err = 0; 14604 servinfo4_t *svp; 14605 svnode_t *shvp; 14606 14607 /* 14608 * If the file being opened has VROOT set, then this is 14609 * a "file" mount. sv_name will not be interesting, so 14610 * go back to the servinfo4 to get the original mount 14611 * path and strip off all but the final edge. Otherwise 14612 * just return the name from the shadow vnode. 14613 */ 14614 14615 if (vp->v_flag & VROOT) { 14616 14617 svp = VTOMI4(vp)->mi_curr_serv; 14618 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14619 14620 fn = strrchr(svp->sv_path, '/'); 14621 if (fn == NULL) 14622 err = EINVAL; 14623 else 14624 fn++; 14625 } else { 14626 shvp = VTOSV(vp); 14627 fn = fn_name(shvp->sv_name); 14628 } 14629 14630 if (err == 0) 14631 if (strlen(fn) < maxlen) 14632 (void) strcpy(fnamep, fn); 14633 else 14634 err = ENAMETOOLONG; 14635 14636 if (vp->v_flag & VROOT) 14637 nfs_rw_exit(&svp->sv_lock); 14638 else 14639 kmem_free(fn, MAXNAMELEN); 14640 14641 return (err); 14642 } 14643 14644 /* 14645 * Bookkeeping for a close that doesn't need to go over the wire. 14646 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14647 * it is left at 1. 14648 */ 14649 void 14650 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14651 { 14652 rnode4_t *rp; 14653 mntinfo4_t *mi; 14654 14655 mi = VTOMI4(vp); 14656 rp = VTOR4(vp); 14657 14658 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14659 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14660 ASSERT(nfs_zone() == mi->mi_zone); 14661 ASSERT(mutex_owned(&osp->os_sync_lock)); 14662 ASSERT(*have_lockp); 14663 14664 if (!osp->os_valid || 14665 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14666 return; 14667 } 14668 14669 /* 14670 * This removes the reference obtained at OPEN; ie, 14671 * when the open stream structure was created. 14672 * 14673 * We don't have to worry about calling 'open_stream_rele' 14674 * since we our currently holding a reference to this 14675 * open stream which means the count can not go to 0 with 14676 * this decrement. 14677 */ 14678 ASSERT(osp->os_ref_count >= 2); 14679 osp->os_ref_count--; 14680 osp->os_valid = 0; 14681 mutex_exit(&osp->os_sync_lock); 14682 *have_lockp = 0; 14683 14684 nfs4_dec_state_ref_count(mi); 14685 } 14686 14687 /* 14688 * Close all remaining open streams on the rnode. These open streams 14689 * could be here because: 14690 * - The close attempted at either close or delmap failed 14691 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14692 * - Someone did mknod on a regular file but never opened it 14693 */ 14694 int 14695 nfs4close_all(vnode_t *vp, cred_t *cr) 14696 { 14697 nfs4_open_stream_t *osp; 14698 int error; 14699 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14700 rnode4_t *rp; 14701 14702 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14703 14704 error = 0; 14705 rp = VTOR4(vp); 14706 14707 /* 14708 * At this point, all we know is that the last time 14709 * someone called vn_rele, the count was 1. Since then, 14710 * the vnode could have been re-activated. We want to 14711 * loop through the open streams and close each one, but 14712 * we have to be careful since once we release the rnode 14713 * hash bucket lock, someone else is free to come in and 14714 * re-activate the rnode and add new open streams. The 14715 * strategy is take the rnode hash bucket lock, verify that 14716 * the count is still 1, grab the open stream off the 14717 * head of the list and mark it invalid, then release the 14718 * rnode hash bucket lock and proceed with that open stream. 14719 * This is ok because nfs4close_one() will acquire the proper 14720 * open/create to close/destroy synchronization for open 14721 * streams, and will ensure that if someone has reopened 14722 * the open stream after we've dropped the hash bucket lock 14723 * then we'll just simply return without destroying the 14724 * open stream. 14725 * Repeat until the list is empty. 14726 */ 14727 14728 for (;;) { 14729 14730 /* make sure vnode hasn't been reactivated */ 14731 rw_enter(&rp->r_hashq->r_lock, RW_READER); 14732 mutex_enter(&vp->v_lock); 14733 if (vp->v_count > 1) { 14734 mutex_exit(&vp->v_lock); 14735 rw_exit(&rp->r_hashq->r_lock); 14736 break; 14737 } 14738 /* 14739 * Grabbing r_os_lock before releasing v_lock prevents 14740 * a window where the rnode/open stream could get 14741 * reactivated (and os_force_close set to 0) before we 14742 * had a chance to set os_force_close to 1. 14743 */ 14744 mutex_enter(&rp->r_os_lock); 14745 mutex_exit(&vp->v_lock); 14746 14747 osp = list_head(&rp->r_open_streams); 14748 if (!osp) { 14749 /* nothing left to CLOSE OTW, so return */ 14750 mutex_exit(&rp->r_os_lock); 14751 rw_exit(&rp->r_hashq->r_lock); 14752 break; 14753 } 14754 14755 mutex_enter(&rp->r_statev4_lock); 14756 /* the file can't still be mem mapped */ 14757 ASSERT(rp->r_mapcnt == 0); 14758 if (rp->created_v4) 14759 rp->created_v4 = 0; 14760 mutex_exit(&rp->r_statev4_lock); 14761 14762 /* 14763 * Grab a ref on this open stream; nfs4close_one 14764 * will mark it as invalid 14765 */ 14766 mutex_enter(&osp->os_sync_lock); 14767 osp->os_ref_count++; 14768 osp->os_force_close = 1; 14769 mutex_exit(&osp->os_sync_lock); 14770 mutex_exit(&rp->r_os_lock); 14771 rw_exit(&rp->r_hashq->r_lock); 14772 14773 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 14774 14775 /* Update error if it isn't already non-zero */ 14776 if (error == 0) { 14777 if (e.error) 14778 error = e.error; 14779 else if (e.stat) 14780 error = geterrno4(e.stat); 14781 } 14782 14783 #ifdef DEBUG 14784 nfs4close_all_cnt++; 14785 #endif 14786 /* Release the ref on osp acquired above. */ 14787 open_stream_rele(osp, rp); 14788 14789 /* Proceed to the next open stream, if any */ 14790 } 14791 return (error); 14792 } 14793 14794 /* 14795 * nfs4close_one - close one open stream for a file if needed. 14796 * 14797 * "close_type" indicates which close path this is: 14798 * CLOSE_NORM: close initiated via VOP_CLOSE. 14799 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 14800 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 14801 * the close and release of client state for this open stream 14802 * (unless someone else has the open stream open). 14803 * CLOSE_RESEND: indicates the request is a replay of an earlier request 14804 * (e.g., due to abort because of a signal). 14805 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 14806 * 14807 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 14808 * recovery. Instead, the caller is expected to deal with retries. 14809 * 14810 * The caller can either pass in the osp ('provided_osp') or not. 14811 * 14812 * 'access_bits' represents the access we are closing/downgrading. 14813 * 14814 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 14815 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 14816 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 14817 * 14818 * Errors are returned via the nfs4_error_t. 14819 */ 14820 void 14821 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 14822 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 14823 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 14824 uint_t mmap_flags) 14825 { 14826 nfs4_open_owner_t *oop; 14827 nfs4_open_stream_t *osp = NULL; 14828 int retry = 0; 14829 int num_retries = NFS4_NUM_RECOV_RETRIES; 14830 rnode4_t *rp; 14831 mntinfo4_t *mi; 14832 nfs4_recov_state_t recov_state; 14833 cred_t *cred_otw = NULL; 14834 bool_t recovonly = FALSE; 14835 int isrecov; 14836 int force_close; 14837 int close_failed = 0; 14838 int did_dec_count = 0; 14839 int did_start_op = 0; 14840 int did_force_recovlock = 0; 14841 int did_start_seqid_sync = 0; 14842 int have_sync_lock = 0; 14843 14844 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14845 14846 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 14847 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 14848 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 14849 len, maxprot, mmap_flags, access_bits)); 14850 14851 nfs4_error_zinit(ep); 14852 rp = VTOR4(vp); 14853 mi = VTOMI4(vp); 14854 isrecov = (close_type == CLOSE_RESEND || 14855 close_type == CLOSE_AFTER_RESEND); 14856 14857 /* 14858 * First get the open owner. 14859 */ 14860 if (!provided_osp) { 14861 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 14862 } else { 14863 oop = provided_osp->os_open_owner; 14864 ASSERT(oop != NULL); 14865 open_owner_hold(oop); 14866 } 14867 14868 if (!oop) { 14869 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 14870 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 14871 "close type %d", (void *)rp, (void *)mi, (void *)cr, 14872 (void *)provided_osp, close_type)); 14873 ep->error = EIO; 14874 goto out; 14875 } 14876 14877 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 14878 recov_retry: 14879 osp = NULL; 14880 close_failed = 0; 14881 force_close = (close_type == CLOSE_FORCE); 14882 retry = 0; 14883 did_start_op = 0; 14884 did_force_recovlock = 0; 14885 did_start_seqid_sync = 0; 14886 have_sync_lock = 0; 14887 recovonly = FALSE; 14888 recov_state.rs_flags = 0; 14889 recov_state.rs_num_retry_despite_err = 0; 14890 14891 /* 14892 * Second synchronize with recovery. 14893 */ 14894 if (!isrecov) { 14895 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 14896 &recov_state, &recovonly); 14897 if (!ep->error) { 14898 did_start_op = 1; 14899 } else { 14900 close_failed = 1; 14901 /* 14902 * If we couldn't get start_fop, but have to 14903 * cleanup state, then at least acquire the 14904 * mi_recovlock so we can synchronize with 14905 * recovery. 14906 */ 14907 if (close_type == CLOSE_FORCE) { 14908 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 14909 RW_READER, FALSE); 14910 did_force_recovlock = 1; 14911 } else 14912 goto out; 14913 } 14914 } 14915 14916 /* 14917 * We cannot attempt to get the open seqid sync if nfs4_start_fop 14918 * set 'recovonly' to TRUE since most likely this is due to 14919 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 14920 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 14921 * to retry, causing us to loop until recovery finishes. Plus we 14922 * don't need protection over the open seqid since we're not going 14923 * OTW, hence don't need to use the seqid. 14924 */ 14925 if (recovonly == FALSE) { 14926 /* need to grab the open owner sync before 'os_sync_lock' */ 14927 ep->error = nfs4_start_open_seqid_sync(oop, mi); 14928 if (ep->error == EAGAIN) { 14929 ASSERT(!isrecov); 14930 if (did_start_op) 14931 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 14932 &recov_state, TRUE); 14933 if (did_force_recovlock) 14934 nfs_rw_exit(&mi->mi_recovlock); 14935 goto recov_retry; 14936 } 14937 did_start_seqid_sync = 1; 14938 } 14939 14940 /* 14941 * Third get an open stream and acquire 'os_sync_lock' to 14942 * sychronize the opening/creating of an open stream with the 14943 * closing/destroying of an open stream. 14944 */ 14945 if (!provided_osp) { 14946 /* returns with 'os_sync_lock' held */ 14947 osp = find_open_stream(oop, rp); 14948 if (!osp) { 14949 ep->error = EIO; 14950 goto out; 14951 } 14952 } else { 14953 osp = provided_osp; 14954 open_stream_hold(osp); 14955 mutex_enter(&osp->os_sync_lock); 14956 } 14957 have_sync_lock = 1; 14958 14959 ASSERT(oop == osp->os_open_owner); 14960 14961 /* 14962 * Fourth, do any special pre-OTW CLOSE processing 14963 * based on the specific close type. 14964 */ 14965 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 14966 !did_dec_count) { 14967 ASSERT(osp->os_open_ref_count > 0); 14968 osp->os_open_ref_count--; 14969 did_dec_count = 1; 14970 if (osp->os_open_ref_count == 0) 14971 osp->os_final_close = 1; 14972 } 14973 14974 if (close_type == CLOSE_FORCE) { 14975 /* see if somebody reopened the open stream. */ 14976 if (!osp->os_force_close) { 14977 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14978 "nfs4close_one: skip CLOSE_FORCE as osp %p " 14979 "was reopened, vp %p", (void *)osp, (void *)vp)); 14980 ep->error = 0; 14981 ep->stat = NFS4_OK; 14982 goto out; 14983 } 14984 14985 if (!osp->os_final_close && !did_dec_count) { 14986 osp->os_open_ref_count--; 14987 did_dec_count = 1; 14988 } 14989 14990 /* 14991 * We can't depend on os_open_ref_count being 0 due to the 14992 * way executables are opened (VN_RELE to match a VOP_OPEN). 14993 */ 14994 #ifdef NOTYET 14995 ASSERT(osp->os_open_ref_count == 0); 14996 #endif 14997 if (osp->os_open_ref_count != 0) { 14998 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14999 "nfs4close_one: should panic here on an " 15000 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15001 "since this is probably the exec problem.")); 15002 15003 osp->os_open_ref_count = 0; 15004 } 15005 15006 /* 15007 * There is the possibility that nfs4close_one() 15008 * for close_type == CLOSE_DELMAP couldn't find the 15009 * open stream, thus couldn't decrement its os_mapcnt; 15010 * therefore we can't use this ASSERT yet. 15011 */ 15012 #ifdef NOTYET 15013 ASSERT(osp->os_mapcnt == 0); 15014 #endif 15015 osp->os_mapcnt = 0; 15016 } 15017 15018 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15019 ASSERT(osp->os_mapcnt >= btopr(len)); 15020 15021 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15022 osp->os_mmap_write -= btopr(len); 15023 if (maxprot & PROT_READ) 15024 osp->os_mmap_read -= btopr(len); 15025 if (maxprot & PROT_EXEC) 15026 osp->os_mmap_read -= btopr(len); 15027 /* mirror the PROT_NONE check in nfs4_addmap() */ 15028 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15029 !(maxprot & PROT_EXEC)) 15030 osp->os_mmap_read -= btopr(len); 15031 osp->os_mapcnt -= btopr(len); 15032 did_dec_count = 1; 15033 } 15034 15035 if (recovonly) { 15036 nfs4_lost_rqst_t lost_rqst; 15037 15038 /* request should not already be in recovery queue */ 15039 ASSERT(lrp == NULL); 15040 nfs4_error_init(ep, EINTR); 15041 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15042 osp, cred_otw, vp); 15043 mutex_exit(&osp->os_sync_lock); 15044 have_sync_lock = 0; 15045 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15046 lost_rqst.lr_op == OP_CLOSE ? 15047 &lost_rqst : NULL, OP_CLOSE, NULL); 15048 close_failed = 1; 15049 force_close = 0; 15050 goto close_cleanup; 15051 } 15052 15053 /* 15054 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15055 * we stopped operating on the open owner's <old oo_name, old seqid> 15056 * space, which means we stopped operating on the open stream 15057 * too. So don't go OTW (as the seqid is likely bad, and the 15058 * stateid could be stale, potentially triggering a false 15059 * setclientid), and just clean up the client's internal state. 15060 */ 15061 if (osp->os_orig_oo_name != oop->oo_name) { 15062 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15063 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15064 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15065 "oo_name %" PRIx64")", 15066 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15067 oop->oo_name)); 15068 close_failed = 1; 15069 } 15070 15071 /* If the file failed recovery, just quit. */ 15072 mutex_enter(&rp->r_statelock); 15073 if (rp->r_flags & R4RECOVERR) { 15074 close_failed = 1; 15075 } 15076 mutex_exit(&rp->r_statelock); 15077 15078 /* 15079 * If the force close path failed to obtain start_fop 15080 * then skip the OTW close and just remove the state. 15081 */ 15082 if (close_failed) 15083 goto close_cleanup; 15084 15085 /* 15086 * Fifth, check to see if there are still mapped pages or other 15087 * opens using this open stream. If there are then we can't 15088 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15089 */ 15090 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15091 nfs4_lost_rqst_t new_lost_rqst; 15092 bool_t needrecov = FALSE; 15093 cred_t *odg_cred_otw = NULL; 15094 seqid4 open_dg_seqid = 0; 15095 15096 if (osp->os_delegation) { 15097 /* 15098 * If this open stream was never OPENed OTW then we 15099 * surely can't DOWNGRADE it (especially since the 15100 * osp->open_stateid is really a delegation stateid 15101 * when os_delegation is 1). 15102 */ 15103 if (access_bits & FREAD) 15104 osp->os_share_acc_read--; 15105 if (access_bits & FWRITE) 15106 osp->os_share_acc_write--; 15107 osp->os_share_deny_none--; 15108 nfs4_error_zinit(ep); 15109 goto out; 15110 } 15111 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15112 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15113 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15114 if (needrecov && !isrecov) { 15115 bool_t abort; 15116 nfs4_bseqid_entry_t *bsep = NULL; 15117 15118 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15119 bsep = nfs4_create_bseqid_entry(oop, NULL, 15120 vp, 0, 15121 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15122 open_dg_seqid); 15123 15124 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15125 oop, osp, odg_cred_otw, vp, access_bits, 0); 15126 mutex_exit(&osp->os_sync_lock); 15127 have_sync_lock = 0; 15128 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15129 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15130 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15131 bsep); 15132 if (odg_cred_otw) 15133 crfree(odg_cred_otw); 15134 if (bsep) 15135 kmem_free(bsep, sizeof (*bsep)); 15136 15137 if (abort == TRUE) 15138 goto out; 15139 15140 if (did_start_seqid_sync) { 15141 nfs4_end_open_seqid_sync(oop); 15142 did_start_seqid_sync = 0; 15143 } 15144 open_stream_rele(osp, rp); 15145 15146 if (did_start_op) 15147 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15148 &recov_state, FALSE); 15149 if (did_force_recovlock) 15150 nfs_rw_exit(&mi->mi_recovlock); 15151 15152 goto recov_retry; 15153 } else { 15154 if (odg_cred_otw) 15155 crfree(odg_cred_otw); 15156 } 15157 goto out; 15158 } 15159 15160 /* 15161 * If this open stream was created as the results of an open 15162 * while holding a delegation, then just release it; no need 15163 * to do an OTW close. Otherwise do a "normal" OTW close. 15164 */ 15165 if (osp->os_delegation) { 15166 nfs4close_notw(vp, osp, &have_sync_lock); 15167 nfs4_error_zinit(ep); 15168 goto out; 15169 } 15170 15171 /* 15172 * If this stream is not valid, we're done. 15173 */ 15174 if (!osp->os_valid) { 15175 nfs4_error_zinit(ep); 15176 goto out; 15177 } 15178 15179 /* 15180 * Last open or mmap ref has vanished, need to do an OTW close. 15181 * First check to see if a close is still necessary. 15182 */ 15183 if (osp->os_failed_reopen) { 15184 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15185 "don't close OTW osp %p since reopen failed.", 15186 (void *)osp)); 15187 /* 15188 * Reopen of the open stream failed, hence the 15189 * stateid of the open stream is invalid/stale, and 15190 * sending this OTW would incorrectly cause another 15191 * round of recovery. In this case, we need to set 15192 * the 'os_valid' bit to 0 so another thread doesn't 15193 * come in and re-open this open stream before 15194 * this "closing" thread cleans up state (decrementing 15195 * the nfs4_server_t's state_ref_count and decrementing 15196 * the os_ref_count). 15197 */ 15198 osp->os_valid = 0; 15199 /* 15200 * This removes the reference obtained at OPEN; ie, 15201 * when the open stream structure was created. 15202 * 15203 * We don't have to worry about calling 'open_stream_rele' 15204 * since we our currently holding a reference to this 15205 * open stream which means the count can not go to 0 with 15206 * this decrement. 15207 */ 15208 ASSERT(osp->os_ref_count >= 2); 15209 osp->os_ref_count--; 15210 nfs4_error_zinit(ep); 15211 close_failed = 0; 15212 goto close_cleanup; 15213 } 15214 15215 ASSERT(osp->os_ref_count > 1); 15216 15217 /* 15218 * Sixth, try the CLOSE OTW. 15219 */ 15220 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15221 close_type, ep, &have_sync_lock); 15222 15223 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15224 /* 15225 * Let the recovery thread be responsible for 15226 * removing the state for CLOSE. 15227 */ 15228 close_failed = 1; 15229 force_close = 0; 15230 retry = 0; 15231 } 15232 15233 /* See if we need to retry with a different cred */ 15234 if ((ep->error == EACCES || 15235 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15236 cred_otw != cr) { 15237 crfree(cred_otw); 15238 cred_otw = cr; 15239 crhold(cred_otw); 15240 retry = 1; 15241 } 15242 15243 if (ep->error || ep->stat) 15244 close_failed = 1; 15245 15246 if (retry && !isrecov && num_retries-- > 0) { 15247 if (have_sync_lock) { 15248 mutex_exit(&osp->os_sync_lock); 15249 have_sync_lock = 0; 15250 } 15251 if (did_start_seqid_sync) { 15252 nfs4_end_open_seqid_sync(oop); 15253 did_start_seqid_sync = 0; 15254 } 15255 open_stream_rele(osp, rp); 15256 15257 if (did_start_op) 15258 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15259 &recov_state, FALSE); 15260 if (did_force_recovlock) 15261 nfs_rw_exit(&mi->mi_recovlock); 15262 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15263 "nfs4close_one: need to retry the close " 15264 "operation")); 15265 goto recov_retry; 15266 } 15267 close_cleanup: 15268 /* 15269 * Seventh and lastly, process our results. 15270 */ 15271 if (close_failed && force_close) { 15272 /* 15273 * It's ok to drop and regrab the 'os_sync_lock' since 15274 * nfs4close_notw() will recheck to make sure the 15275 * "close"/removal of state should happen. 15276 */ 15277 if (!have_sync_lock) { 15278 mutex_enter(&osp->os_sync_lock); 15279 have_sync_lock = 1; 15280 } 15281 /* 15282 * This is last call, remove the ref on the open 15283 * stream created by open and clean everything up. 15284 */ 15285 osp->os_pending_close = 0; 15286 nfs4close_notw(vp, osp, &have_sync_lock); 15287 nfs4_error_zinit(ep); 15288 } 15289 15290 if (!close_failed) { 15291 if (have_sync_lock) { 15292 osp->os_pending_close = 0; 15293 mutex_exit(&osp->os_sync_lock); 15294 have_sync_lock = 0; 15295 } else { 15296 mutex_enter(&osp->os_sync_lock); 15297 osp->os_pending_close = 0; 15298 mutex_exit(&osp->os_sync_lock); 15299 } 15300 if (did_start_op && recov_state.rs_sp != NULL) { 15301 mutex_enter(&recov_state.rs_sp->s_lock); 15302 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15303 mutex_exit(&recov_state.rs_sp->s_lock); 15304 } else { 15305 nfs4_dec_state_ref_count(mi); 15306 } 15307 nfs4_error_zinit(ep); 15308 } 15309 15310 out: 15311 if (have_sync_lock) 15312 mutex_exit(&osp->os_sync_lock); 15313 if (did_start_op) 15314 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15315 recovonly ? TRUE : FALSE); 15316 if (did_force_recovlock) 15317 nfs_rw_exit(&mi->mi_recovlock); 15318 if (cred_otw) 15319 crfree(cred_otw); 15320 if (osp) 15321 open_stream_rele(osp, rp); 15322 if (oop) { 15323 if (did_start_seqid_sync) 15324 nfs4_end_open_seqid_sync(oop); 15325 open_owner_rele(oop); 15326 } 15327 } 15328 15329 /* 15330 * Convert information returned by the server in the LOCK4denied 15331 * structure to the form required by fcntl. 15332 */ 15333 static void 15334 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15335 { 15336 nfs4_lo_name_t *lo; 15337 15338 #ifdef DEBUG 15339 if (denied_to_flk_debug) { 15340 lockt_denied_debug = lockt_denied; 15341 debug_enter("lockt_denied"); 15342 } 15343 #endif 15344 15345 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15346 flk->l_whence = 0; /* aka SEEK_SET */ 15347 flk->l_start = lockt_denied->offset; 15348 flk->l_len = lockt_denied->length; 15349 15350 /* 15351 * If the blocking clientid matches our client id, then we can 15352 * interpret the lockowner (since we built it). If not, then 15353 * fabricate a sysid and pid. Note that the l_sysid field 15354 * in *flk already has the local sysid. 15355 */ 15356 15357 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15358 15359 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15360 lo = (nfs4_lo_name_t *) 15361 lockt_denied->owner.owner_val; 15362 15363 flk->l_pid = lo->ln_pid; 15364 } else { 15365 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15366 "denied_to_flk: bad lock owner length\n")); 15367 15368 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15369 } 15370 } else { 15371 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15372 "denied_to_flk: foreign clientid\n")); 15373 15374 /* 15375 * Construct a new sysid which should be different from 15376 * sysids of other systems. 15377 */ 15378 15379 flk->l_sysid++; 15380 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15381 } 15382 } 15383 15384 static pid_t 15385 lo_to_pid(lock_owner4 *lop) 15386 { 15387 pid_t pid = 0; 15388 uchar_t *cp; 15389 int i; 15390 15391 cp = (uchar_t *)&lop->clientid; 15392 15393 for (i = 0; i < sizeof (lop->clientid); i++) 15394 pid += (pid_t)*cp++; 15395 15396 cp = (uchar_t *)lop->owner_val; 15397 15398 for (i = 0; i < lop->owner_len; i++) 15399 pid += (pid_t)*cp++; 15400 15401 return (pid); 15402 } 15403 15404 /* 15405 * Given a lock pointer, returns the length of that lock. 15406 * "end" is the last locked offset the "l_len" covers from 15407 * the start of the lock. 15408 */ 15409 static off64_t 15410 lock_to_end(flock64_t *lock) 15411 { 15412 off64_t lock_end; 15413 15414 if (lock->l_len == 0) 15415 lock_end = (off64_t)MAXEND; 15416 else 15417 lock_end = lock->l_start + lock->l_len - 1; 15418 15419 return (lock_end); 15420 } 15421 15422 /* 15423 * Given the end of a lock, it will return you the length "l_len" for that lock. 15424 */ 15425 static off64_t 15426 end_to_len(off64_t start, off64_t end) 15427 { 15428 off64_t lock_len; 15429 15430 ASSERT(end >= start); 15431 if (end == MAXEND) 15432 lock_len = 0; 15433 else 15434 lock_len = end - start + 1; 15435 15436 return (lock_len); 15437 } 15438 15439 /* 15440 * On given end for a lock it determines if it is the last locked offset 15441 * or not, if so keeps it as is, else adds one to return the length for 15442 * valid start. 15443 */ 15444 static off64_t 15445 start_check(off64_t x) 15446 { 15447 if (x == MAXEND) 15448 return (x); 15449 else 15450 return (x + 1); 15451 } 15452 15453 /* 15454 * See if these two locks overlap, and if so return 1; 15455 * otherwise, return 0. 15456 */ 15457 static int 15458 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15459 { 15460 off64_t llfp_end, curfp_end; 15461 15462 llfp_end = lock_to_end(llfp); 15463 curfp_end = lock_to_end(curfp); 15464 15465 if (((llfp_end >= curfp->l_start) && 15466 (llfp->l_start <= curfp->l_start)) || 15467 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15468 return (1); 15469 return (0); 15470 } 15471 15472 /* 15473 * Determine what the interseting lock region is, and add that to the 15474 * 'nl_llpp' locklist in increasing order (by l_start). 15475 */ 15476 static void 15477 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15478 locklist_t **nl_llpp, vnode_t *vp) 15479 { 15480 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15481 off64_t lost_flp_end, local_flp_end, len, start; 15482 15483 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15484 15485 if (!locks_intersect(lost_flp, local_flp)) 15486 return; 15487 15488 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15489 "locks intersect")); 15490 15491 lost_flp_end = lock_to_end(lost_flp); 15492 local_flp_end = lock_to_end(local_flp); 15493 15494 /* Find the starting point of the intersecting region */ 15495 if (local_flp->l_start > lost_flp->l_start) 15496 start = local_flp->l_start; 15497 else 15498 start = lost_flp->l_start; 15499 15500 /* Find the lenght of the intersecting region */ 15501 if (lost_flp_end < local_flp_end) 15502 len = end_to_len(start, lost_flp_end); 15503 else 15504 len = end_to_len(start, local_flp_end); 15505 15506 /* 15507 * Prepare the flock structure for the intersection found and insert 15508 * it into the new list in increasing l_start order. This list contains 15509 * intersections of locks registered by the client with the local host 15510 * and the lost lock. 15511 * The lock type of this lock is the same as that of the local_flp. 15512 */ 15513 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15514 intersect_llp->ll_flock.l_start = start; 15515 intersect_llp->ll_flock.l_len = len; 15516 intersect_llp->ll_flock.l_type = local_flp->l_type; 15517 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15518 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15519 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15520 intersect_llp->ll_vp = vp; 15521 15522 tmp_fllp = *nl_llpp; 15523 cur_fllp = NULL; 15524 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15525 intersect_llp->ll_flock.l_start) { 15526 cur_fllp = tmp_fllp; 15527 tmp_fllp = tmp_fllp->ll_next; 15528 } 15529 if (cur_fllp == NULL) { 15530 /* first on the list */ 15531 intersect_llp->ll_next = *nl_llpp; 15532 *nl_llpp = intersect_llp; 15533 } else { 15534 intersect_llp->ll_next = cur_fllp->ll_next; 15535 cur_fllp->ll_next = intersect_llp; 15536 } 15537 15538 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15539 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15540 intersect_llp->ll_flock.l_start, 15541 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15542 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15543 } 15544 15545 /* 15546 * Our local locking current state is potentially different than 15547 * what the NFSv4 server thinks we have due to a lost lock that was 15548 * resent and then received. We need to reset our "NFSv4" locking 15549 * state to match the current local locking state for this pid since 15550 * that is what the user/application sees as what the world is. 15551 * 15552 * We cannot afford to drop the open/lock seqid sync since then we can 15553 * get confused about what the current local locking state "is" versus 15554 * "was". 15555 * 15556 * If we are unable to fix up the locks, we send SIGLOST to the affected 15557 * process. This is not done if the filesystem has been forcibly 15558 * unmounted, in case the process has already exited and a new process 15559 * exists with the same pid. 15560 */ 15561 static void 15562 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15563 nfs4_lock_owner_t *lop) 15564 { 15565 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15566 mntinfo4_t *mi = VTOMI4(vp); 15567 const int cmd = F_SETLK; 15568 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15569 flock64_t ul_fl; 15570 15571 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15572 "nfs4_reinstitute_local_lock_state")); 15573 15574 /* 15575 * Find active locks for this vp from the local locking code. 15576 * Scan through this list and find out the locks that intersect with 15577 * the lost lock. Once we find the lock that intersects, add the 15578 * intersection area as a new lock to a new list "ri_llp". The lock 15579 * type of the intersection region lock added to ri_llp is the same 15580 * as that found in the active lock list, "list". The intersecting 15581 * region locks are added to ri_llp in increasing l_start order. 15582 */ 15583 ASSERT(nfs_zone() == mi->mi_zone); 15584 15585 locks = flk_active_locks_for_vp(vp); 15586 ri_llp = NULL; 15587 15588 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15589 ASSERT(llp->ll_vp == vp); 15590 /* 15591 * Pick locks that belong to this pid/lockowner 15592 */ 15593 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15594 continue; 15595 15596 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15597 } 15598 15599 /* 15600 * Now we have the list of intersections with the lost lock. These are 15601 * the locks that were/are active before the server replied to the 15602 * last/lost lock. Issue these locks to the server here. Playing these 15603 * locks to the server will re-establish aur current local locking state 15604 * with the v4 server. 15605 * If we get an error, send SIGLOST to the application for that lock. 15606 */ 15607 15608 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15609 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15610 "nfs4_reinstitute_local_lock_state: need to issue " 15611 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15612 llp->ll_flock.l_start, 15613 llp->ll_flock.l_start + llp->ll_flock.l_len, 15614 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15615 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15616 /* 15617 * No need to relock what we already have 15618 */ 15619 if (llp->ll_flock.l_type == lost_flp->l_type) 15620 continue; 15621 15622 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15623 } 15624 15625 /* 15626 * Now keeping the start of the lost lock as our reference parse the 15627 * newly created ri_llp locklist to find the ranges that we have locked 15628 * with the v4 server but not in the current local locking. We need 15629 * to unlock these ranges. 15630 * These ranges can also be reffered to as those ranges, where the lost 15631 * lock does not overlap with the locks in the ri_llp but are locked 15632 * since the server replied to the lost lock. 15633 */ 15634 cur_start = lost_flp->l_start; 15635 lost_flp_end = lock_to_end(lost_flp); 15636 15637 ul_fl.l_type = F_UNLCK; 15638 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15639 ul_fl.l_sysid = lost_flp->l_sysid; 15640 ul_fl.l_pid = lost_flp->l_pid; 15641 15642 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15643 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15644 15645 if (llp->ll_flock.l_start <= cur_start) { 15646 cur_start = start_check(llp_ll_flock_end); 15647 continue; 15648 } 15649 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15650 "nfs4_reinstitute_local_lock_state: " 15651 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15652 cur_start, llp->ll_flock.l_start)); 15653 15654 ul_fl.l_start = cur_start; 15655 ul_fl.l_len = end_to_len(cur_start, 15656 (llp->ll_flock.l_start - 1)); 15657 15658 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15659 cur_start = start_check(llp_ll_flock_end); 15660 } 15661 15662 /* 15663 * In the case where the lost lock ends after all intersecting locks, 15664 * unlock the last part of the lost lock range. 15665 */ 15666 if (cur_start != start_check(lost_flp_end)) { 15667 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15668 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15669 "lost lock region [%"PRIx64" - %"PRIx64"]", 15670 cur_start, lost_flp->l_start + lost_flp->l_len)); 15671 15672 ul_fl.l_start = cur_start; 15673 /* 15674 * Is it an to-EOF lock? if so unlock till the end 15675 */ 15676 if (lost_flp->l_len == 0) 15677 ul_fl.l_len = 0; 15678 else 15679 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15680 15681 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15682 } 15683 15684 if (locks != NULL) 15685 flk_free_locklist(locks); 15686 15687 /* Free up our newly created locklist */ 15688 for (llp = ri_llp; llp != NULL; ) { 15689 tmp_llp = llp->ll_next; 15690 kmem_free(llp, sizeof (locklist_t)); 15691 llp = tmp_llp; 15692 } 15693 15694 /* 15695 * Now return back to the original calling nfs4frlock() 15696 * and let us naturally drop our seqid syncs. 15697 */ 15698 } 15699 15700 /* 15701 * Create a lost state record for the given lock reinstantiation request 15702 * and push it onto the lost state queue. 15703 */ 15704 static void 15705 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15706 nfs4_lock_owner_t *lop) 15707 { 15708 nfs4_lost_rqst_t req; 15709 nfs_lock_type4 locktype; 15710 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15711 15712 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15713 15714 locktype = flk_to_locktype(cmd, flk->l_type); 15715 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15716 NULL, NULL, lop, flk, &req, cr, vp); 15717 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15718 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15719 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15720 NULL); 15721 } 15722